Skip to content

fix(worktrees): route OpenCode calls through workspaces#521

Open
shantur wants to merge 1 commit into
devfrom
worktree-missing-sessions
Open

fix(worktrees): route OpenCode calls through workspaces#521
shantur wants to merge 1 commit into
devfrom
worktree-missing-sessions

Conversation

@shantur
Copy link
Copy Markdown
Collaborator

@shantur shantur commented Jun 2, 2026

Summary

This fixes #518 by moving CodeNomad worktree execution from directory-header routing to OpenCode experimental workspace routing.

OpenCode changed existing-session routing so session routes can prefer the session's stored directory over x-opencode-directory. That made the old CodeNomad worktree proxy model unreliable for sessions that should execute in a worktree. This PR switches CodeNomad to resolve an OpenCode workspace ID for each CodeNomad worktree and pass that workspace ID on worktree-scoped OpenCode calls.

What changed

  • Start OpenCode with OPENCODE_EXPERIMENTAL_WORKSPACES=true.
  • Change the OpenCode server base URL from /workspaces/:id/worktrees/root/instance to /workspaces/:id/instance.
  • Add a root OpenCode client helper in packages/ui/src/stores/opencode-client.ts.
  • Add OpenCode workspace sync/cache helpers in packages/ui/src/stores/opencode-workspaces.ts.
  • Sync OpenCode workspaces after CodeNomad worktree hydration and after worktree create/delete flows.
  • Map CodeNomad worktree slugs/directories to OpenCode workspace.id values discovered by experimental.workspace.syncList and experimental.workspace.list.
  • Replace all OpenCode worktree clients with the root client plus explicit workspace payloads where the active session/worktree requires it.
  • Route session, permission/question, file browser reads, SDK git status, and prompt/action calls through root OpenCode client + workspace ID.
  • Keep CodeNomad local git worktree server APIs intact; those still need filesystem directories for local git operations.

Review fix

  • Fixed right-panel file saves so they no longer write to the root workspace after reading from a selected worktree.
  • The existing CodeNomad file-content API now accepts an optional worktree query parameter.
  • Right-panel saves pass worktreeSlugForViewer(), and the server resolves that slug to the same worktree directory used by local git worktree APIs before writing.
  • Root saves continue to use the original root workspace path.

Removed old routing

  • Removed /workspaces/:id/worktrees/:slug/instance OpenCode proxy routes.
  • Removed directory override proxy support using /__dir/<encoded>.
  • Removed proxy injection of x-opencode-directory.
  • Removed the remaining background-process completion prompt x-opencode-directory header.
  • Removed getOrCreateWorktreeClient, getOrCreateWorktreeClientWithDirectoryOverride, and worktree proxy path helpers from the UI worktree store.
  • Simplified sdkManager.createClient because clients are no longer keyed by worktree slug.

Why this fixes #518

Worktree sessions can now stay visible under the root project session listing while worktree execution is selected through OpenCode's workspace routing model. CodeNomad no longer depends on x-opencode-directory to override existing session directories, so sessions should not disappear from the root-directory list or execute in the wrong directory because of stale session directory fallback behavior.

Fixes #518

Validation

  • npm run typecheck --workspace @codenomad/ui
  • npm run typecheck --workspace @neuralnomads/codenomad
  • git diff --check

Notes

Some touched files are already oversized and were not refactored as part of this migration: packages/server/src/server/http-server.ts, packages/ui/src/stores/instances.ts, packages/ui/src/stores/session-api.ts, packages/ui/src/stores/session-state.ts, packages/ui/src/components/instance/shell/right-panel/RightPanel.tsx, packages/ui/src/stores/session-events.ts, and packages/server/src/background-processes/manager.ts.

@shantur
Copy link
Copy Markdown
Collaborator Author

shantur commented Jun 2, 2026

@CodeNomadBot

I want you to perform the gatekeeper checks for latest PR state

check for regressions
check if the PR could be better implemented in any other way
Be the PR gatekeeper
Be ruthless about code quality

@codenomadbot
Copy link
Copy Markdown
Contributor

codenomadbot Bot commented Jun 2, 2026

Gatekeeper review for latest PR state.

Request understood: review PR #521 ruthlessly for regressions, implementation quality, and whether the fix should be implemented differently. Assumption: scope is the current single-commit PR diff against origin/dev.

Findings: no blocking code findings found in the PR diff.

Review notes:

  • The implementation is intentionally narrow: createSession now creates parent sessions through getRootClient(instanceId), then persists the inherited worktree slug via setWorktreeSlugForParentSession(..., { currentSlug: "root" }).
  • That matches the stated bug: root-directory session list hydration can find the parent session after restart, while later operations still use getWorktreeSlugForSession(...) and route through the worktree client once metadata is persisted.
  • The currentSlug: "root" change is important and correct because the metadata update must address the newly created root-directory session. Keeping the previous currentSlug: worktreeSlug would be a likely regression for non-root worktrees.
  • I did not find a smaller/cleaner implementation that preserves both requirements. Creating directly in the worktree fixes routing but preserves the restart visibility bug; creating in root without metadata would break inherited worktree routing. This split is the minimal bridge between those constraints.

Validation:

  • git diff --check origin/dev...HEAD passed.
  • npm run typecheck --workspace @codenomad/ui could not complete in this checkout because dependencies/types are missing, with errors like missing solid-js, solid-js/jsx-runtime, and @opencode-ai/sdk/v2/client. This looks environmental/dependency-install related rather than caused by the one-line behavioral change, but it means I cannot independently confirm the PR's stated typecheck result from this workspace.

Residual risk:

  • There is no targeted automated test for the exact restart scenario: create a new parent session while a non-root worktree session is active, restart/reload, confirm the new parent remains visible in the root session list, and confirm subsequent operations still route to the stored worktree slug. The code path is coherent, but this is the regression-prone behavior and deserves a focused test if the current test harness can exercise it.

Gatekeeper verdict: no code-quality blocker found. I would not request implementation changes for this diff, but I would want either CI typecheck confirmation or a clean local dependency install before merge, plus preferably a focused regression test for #518.

--
Yours,
CodeNomadBot

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

PR builds are available as GitHub Actions artifacts:

https://github.com/NeuralNomadsAI/CodeNomad/actions/runs/26833236698

Artifacts expire in 7 days.
Artifacts:

  • pr-521-cbb0e2226d8250b881cb1d2de3febf948427fd1b-tauri-macos
  • pr-521-cbb0e2226d8250b881cb1d2de3febf948427fd1b-tauri-linux
  • pr-521-cbb0e2226d8250b881cb1d2de3febf948427fd1b-tauri-windows
  • pr-521-cbb0e2226d8250b881cb1d2de3febf948427fd1b-tauri-macos-arm64
  • pr-521-cbb0e2226d8250b881cb1d2de3febf948427fd1b-electron-macos
  • pr-521-cbb0e2226d8250b881cb1d2de3febf948427fd1b-electron-windows
  • pr-521-cbb0e2226d8250b881cb1d2de3febf948427fd1b-electron-linux

@shantur shantur closed this Jun 2, 2026
@shantur shantur force-pushed the worktree-missing-sessions branch from cbb0e22 to b3594d2 Compare June 2, 2026 17:37
@shantur shantur changed the title fix(sessions): keep worktree sessions visible after restart fix(worktrees): route OpenCode calls through workspaces Jun 2, 2026
@shantur shantur reopened this Jun 2, 2026
@shantur
Copy link
Copy Markdown
Collaborator Author

shantur commented Jun 2, 2026

@CodeNomadBot

PR is changed completely go through it again.
I want you to perform the gatekeeper checks for latest PR state

check for regressions
check if the PR could be better implemented in any other way
Be the PR gatekeeper
Be ruthless about code quality

@codenomadbot
Copy link
Copy Markdown
Contributor

codenomadbot Bot commented Jun 2, 2026

Gatekeeper review for the changed PR state.

Request understood: the previous review is obsolete; review the current PR #521 diff against origin/dev ruthlessly for regressions, implementation quality, and better alternatives. Assumption: scope is the current single commit 4111d141 and the workspace-routing migration described in the PR body.

Findings:

  1. P1 blocker: file browser saves still write to the root workspace while reads now come from the selected OpenCode workspace.

    packages/ui/src/components/instance/shell/right-panel/RightPanel.tsx:497, :521, :552, and :705 correctly pass the OpenCode workspace payload for file list/read operations based on worktreeSlugForViewer(). But saveBrowserFile still calls serverApi.writeWorkspaceFile(props.instanceId, path, content) at RightPanel.tsx:581, and that API writes /api/workspaces/:id/files/content rooted at the CodeNomad workspace root (packages/ui/src/lib/api-client.ts:351). There is no worktree slug or OpenCode workspace passed on save.

    Repro path: select a non-root worktree in the right panel, open foo.ts, edit it, and save. The editor loaded foo.ts from the non-root OpenCode workspace, but the save writes foo.ts in the root workspace path. Best case the wrong file is modified; worst case a same-path root file is silently overwritten while the worktree file remains unchanged. This directly violates the PR's claim that file browser calls are routed through the active worktree/workspace.

    Required fix: make writes use the same routing basis as reads. Either add a worktree-aware server write endpoint, route save through an OpenCode file write API with the same workspace payload if available, or otherwise carry worktreeSlugForViewer() through the server write path. Reads and writes must be symmetric.

Implementation review notes:

  • The broad direction is reasonable: dropping x-opencode-directory and moving existing-session operations to explicit OpenCode workspace IDs matches the stated OpenCode behavior change.
  • The new opencode-workspaces.ts cache is a sensible thin adapter, but it currently silently falls back to root when workspace resolution fails. For execution paths (promptAsync, shell, commands, permission/question replies), that fallback can be dangerous because it turns a worktree operation into a root operation. Consider failing loudly for non-root slugs when a workspace ID cannot be resolved, at least for mutating/execution calls.
  • The migration removed per-permission/question stored worktree slug maps and now derives workspace from session metadata at response time. That is cleaner if metadata is stable, but it makes stale/missing metadata more consequential. This reinforces the need to avoid silent root fallback.

Validation:

  • git diff --check origin/dev...HEAD passed.
  • npm run typecheck --workspace @codenomad/ui failed in this checkout because dependencies/types are missing, including solid-js, solid-js/jsx-runtime, and @opencode-ai/sdk/v2/client.
  • npm run typecheck --workspace @neuralnomads/codenomad failed in this checkout because @types/node is missing.
  • These typecheck failures appear environmental for this local workspace, but they prevent me from independently confirming the PR's stated typecheck results.

Gatekeeper verdict: do not merge yet. The workspace-routing approach is directionally plausible, but the file browser save path is currently inconsistent with the new read routing and can write to the wrong directory for non-root worktrees.

--
Yours,
CodeNomadBot

Migrate CodeNomad worktree execution from per-worktree OpenCode proxy clients and x-opencode-directory headers to OpenCode experimental workspace routing.

OpenCode is now started with OPENCODE_EXPERIMENTAL_WORKSPACES=true and the root instance proxy path. The UI syncs OpenCode workspaces after worktree hydration and maps CodeNomad worktree slugs to OpenCode workspace IDs.

Session, file, git status, permission, and question calls now use the root OpenCode client and pass the resolved workspace ID when the active session belongs to a worktree. Worktree metadata updates also use the root client.

The obsolete worktree instance proxy routes, directory override handling, and x-opencode-directory forwarding were removed so existing-session routing no longer depends on stale directory headers.

Validated with npm run typecheck --workspace @codenomad/ui, npm run typecheck --workspace @neuralnomads/codenomad, and git diff --check.
@shantur shantur force-pushed the worktree-missing-sessions branch from 4111d14 to 0d24ce9 Compare June 2, 2026 20:22
@shantur
Copy link
Copy Markdown
Collaborator Author

shantur commented Jun 2, 2026

@CodeNomadBot

Check now

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

PR builds are available as GitHub Actions artifacts:

https://github.com/NeuralNomadsAI/CodeNomad/actions/runs/26845457913

Artifacts expire in 7 days.
Artifacts:

  • pr-521-4111d14114296e2934156f8289b4c85f07dd7081-electron-windows
  • pr-521-4111d14114296e2934156f8289b4c85f07dd7081-electron-linux

@codenomadbot
Copy link
Copy Markdown
Contributor

codenomadbot Bot commented Jun 2, 2026

Re-check for latest PR state after the file-save fix.

Request understood: verify the previous blocker and run another gatekeeper pass on the current PR state. Assumption: scope is the latest local PR head 0d24ce9d against origin/dev.

Findings: no blocking findings found in this pass.

What changed relative to the previous blocker:

  • The right panel now calls serverApi.writeWorkspaceFile(props.instanceId, path, content, { worktree: worktreeSlugForViewer() }) in packages/ui/src/components/instance/shell/right-panel/RightPanel.tsx:581.
  • serverApi.writeWorkspaceFile now serializes non-root worktrees into the worktree query parameter in packages/ui/src/lib/api-client.ts:351.
  • The server file-content route now accepts worktree, resolves it via the same resolveGitWorktreeDirectory(...) helper used by the local worktree git APIs, and writes through WorkspaceManager.writeFileInDirectory(...) for non-root worktrees in packages/server/src/server/routes/workspaces.ts:155.
  • WorkspaceManager.writeFileInDirectory(...) uses FileSystemBrowser rooted at the resolved worktree directory in packages/server/src/workspaces/manager.ts:111, so writes remain restricted to the target directory.

Gatekeeper assessment:

  • The previously reported P1 mixed-directory save bug is fixed. Reads still use OpenCode workspace routing, and saves now use the corresponding CodeNomad worktree slug on the server write path.
  • I did not find a new blocker in the changed file-save path.
  • The broader residual risk remains the silent root fallback when OpenCode workspace ID resolution fails for non-root worktrees. That is not introduced by this follow-up fix, but for mutating/execution calls it is still worth hardening before or soon after merge.

Validation:

  • git diff --check origin/dev...HEAD passed.
  • npm run typecheck --workspace @codenomad/ui still cannot complete in this checkout because dependency/type packages are missing, including solid-js, solid-js/jsx-runtime, and @opencode-ai/sdk/v2/client.
  • npm run typecheck --workspace @neuralnomads/codenomad still cannot complete because @types/node is missing.
  • These failures appear environmental for this local workspace, but I cannot independently confirm the PR's stated typecheck results from here.

Verdict: the previously blocking file-save issue is resolved. I would still require CI or a clean dependency-installed typecheck before merge, but I do not have a remaining code-level blocker from this re-check.

--
Yours,
CodeNomadBot

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

PR builds are available as GitHub Actions artifacts:

https://github.com/NeuralNomadsAI/CodeNomad/actions/runs/26845817287

Artifacts expire in 7 days.
Artifacts:

  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-macos
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-linux
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-windows
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-electron-macos
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-macos-arm64
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-electron-windows
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-electron-linux

1 similar comment
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

PR builds are available as GitHub Actions artifacts:

https://github.com/NeuralNomadsAI/CodeNomad/actions/runs/26845817287

Artifacts expire in 7 days.
Artifacts:

  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-macos
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-linux
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-windows
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-electron-macos
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-tauri-macos-arm64
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-electron-windows
  • pr-521-0d24ce9d6def4e672aa9b63ebe22905cdca08ad5-electron-linux

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.

[Bug]: Losing sessions on codenomad worktrees on restarts

1 participant