Skip to content

Fix backend-agent initialization for server-backed repositories#199

Closed
madmansn0w wants to merge 3 commits into
abhigyanpatwari:mainfrom
madmansn0w:fix/backend-agent-server-mode
Closed

Fix backend-agent initialization for server-backed repositories#199
madmansn0w wants to merge 3 commits into
abhigyanpatwari:mainfrom
madmansn0w:fix/backend-agent-server-mode

Conversation

@madmansn0w

Copy link
Copy Markdown

Summary

This PR fixes the web app flow for server-backed repositories.

Previously, when connecting the web UI to a locally served GitNexus backend, the app still attempted to use the browser-local agent / embedding pipeline path. That caused failures such as:

  • Database not ready. Please load a repository first.
  • stalled Initializing Agent
  • browser worker embedding startup running against a repo that was never loaded into the local WASM Kuzu instance

Root cause

The web app had two different repository modes, but the initialization path was still mixing them:

  1. Browser-ingested repo mode

    • load repo into browser worker
    • initialize local agent
    • start browser embedding pipeline
  2. Server-backed repo mode

    • fetch graph/file contents from backend
    • should initialize backend agent
    • should use backend-backed search/query capabilities

However, server-backed repositories were still calling the local initialization path in some cases, and the initial server connect flow also had a race where backend agent initialization could happen before serverBaseUrl state was available.

Changes

  • added explicit isServerMode state to app state
  • added initializeBackendAgent(...) to app state
  • switched server connect flow to initialize the backend agent instead of the local browser agent
  • passed the backend URL directly during initial server connection to avoid a React state timing race
  • updated repo switching to use backend-agent initialization in server mode
  • prevented browser-local embedding pipeline startup in server mode
  • preserved browser embedding behavior for locally ingested repositories

Result

The web UI can now connect to a served backend repository without trying to run the browser-local embedding/database pipeline against a non-local repo.

This fixes the frontend/server-mode integration issue that produced:

  • Database not ready. Please load a repository first.

Validation

Manually validated:

  • connected web UI to local GitNexus server
  • loaded a served repository successfully
  • confirmed embeddings are available for the served repo
  • confirmed AI chat path progresses past the previous DB-not-ready failure

Follow-ups / known issues

Not fixed in this PR:

  • backend context-builder queries currently hit 404 for some /api/query-backed helper calls
  • provider/auth configuration can still fail with 401 User not found depending on local LLM settings

@vercel

vercel Bot commented Mar 6, 2026

Copy link
Copy Markdown

@madmansn0w is attempting to deploy a commit to the NexusCore Team on Vercel.

A member of the Team first needs to authorize it.

@reversTeam reversTeam left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough PR, @madmansn0w! The problem statement and root-cause analysis are really clear. A few observations:

What looks good:

  • The isServerMode flag is a clean way to bifurcate the two paths.
  • Passing baseUrl directly into handleServerConnect to avoid the React state timing race is the right call.
  • Guarding startEmbeddings with isServerMode early-return is simple and effective.
  • The dependency arrays on useCallback are correctly updated throughout.

Issues / suggestions:

  1. Inconsistent indentation in initializeBackendAgent — The function body (lines starting at const api = apiRef.current) is indented at 2 spaces instead of the 4 spaces used by the rest of useAppState.tsx. This looks like it was accidentally de-indented during editing.

  2. Inconsistent indentation in switchRepo — Around the initializeBackendAgent call inside switchRepo, the indentation is mixed (some lines at 10 spaces, the closing brace at 8). Should be consistently 6 spaces to match the surrounding try block.

  3. Duplicate setServerBaseUrl(baseUrl) call — In the auto-connect flow (useEffect), setServerBaseUrl(baseUrl) is called on line ~94 inside handleServerConnect, and then again on line ~94 of the .then() callback. The second call is redundant since handleServerConnect already sets it.

  4. setEmbeddingStatus('ready') in switchRepo — When switching repos in server mode, you set embedding status to 'ready' directly. This is a reasonable shortcut since the backend handles search, but it could be confusing if other code checks embeddingStatus === 'ready' to mean "local embeddings are loaded." A comment explaining why this is safe would help future readers.

  5. Lock file churn — The package-lock.json diff is large (~1000 lines) with esbuild version bumps. If these dependency updates aren't intentional, consider reverting the lock file to keep the diff focused.

  6. No tests — Understandable for a React state hook, but the PR mentions known follow-up issues (404s on /api/query, 401 on auth). It would be good to track those as separate issues.

Overall this is a solid fix for a real pain point. The approach is sound — just needs the formatting cleanup and the duplicate setServerBaseUrl removed.

@madmansn0w

Copy link
Copy Markdown
Author

Addressed in this update:

  • Fixed indentation in initializeBackendAgent and switchRepo blocks in useAppState.tsx.
  • Removed the redundant setServerBaseUrl(baseUrl) in auto-connect flow.
  • Fixed onboarding server-connect call to pass both required args to handleServerConnect.
  • Added a clarifying comment that embeddingStatus = 'ready' in server mode means backend search is ready (not local embeddings loaded).
  • Reverted unintended dependency/lockfile churn to keep this PR focused.

Tracked follow-ups requested in review:

@magyargergo

Copy link
Copy Markdown
Collaborator

⚠️ Upcoming Prettier formatting — rebase instructions

PR #563 adds Prettier as the code formatter for the repo. When it merges, the bulk format commit will touch ~350 files (style-only: whitespace, quotes, trailing commas). Your branch will likely conflict.

After #563 merges, rebase your branch:

git fetch origin
git checkout <your-branch>
git rebase origin/main

# Conflicts will be formatting-only — accept your version:
git checkout --theirs .
git add .
git rebase --continue

# Then re-format your branch to match the new style:
npx prettier --write .
git add -A
git commit -m "style: apply prettier formatting"
git push --force-with-lease

New setup step: Run npm install at the repo root (not just in gitnexus/) to get prettier + activate the pre-commit hook. The hook auto-formats staged files on every commit going forward.

@magyargergo

Copy link
Copy Markdown
Collaborator

Please submit a new PR if this is still relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants