Conversation
…5180) /fork previously replaced the current session's runtime in place: the original's kernel was disposed, its resident subagents closed, its cron jobs and heartbeats rebound to the fork, and its identity taken over, so the original vanished from the agents view. Fork is now split into an additive, capability-gated fork_export daemon command (branch export with no teardown) plus client orchestration in DaemonAgentConnection.fork(): export -> create a new daemon session for the forked file -> reattach. The original keeps running with its kernel, subagents, heartbeats, and lease, and stays listed; the fork starts fresh (cold kernel, no subagent registry, no cron), which also keeps the fork as lazy as possible. Legacy fork remains as the fallback for old daemons and in-memory sessions. DAEMON_SCHEMA_REVISION 16 -> 17. Fork transitions are serialized on the connection; the exported file is only removed on a definitive create rejection, and a failed reattach best-effort kills the created fork session.
… into eng-5180 # Conflicts: # packages/coding-agent/CHANGELOG.md
| return this.legacyFork(entryId, options); | ||
| } | ||
| const sourceActiveSessionId = this.activeSessionId; | ||
| const exported = await this.requestData<{ |
There was a problem hiding this comment.
🟠 High agent-connection/daemon-agent-connection.ts:1240
A fork fails after reconnecting to an older daemon instead of falling back to legacyFork. The capability check occurs before requestData, but a recovered request can later throw DaemonCapabilityUnavailableError; catch that post-reconnect error and invoke legacyFork(entryId, options).
🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file @packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts around line 1240:
A fork fails after reconnecting to an older daemon instead of falling back to `legacyFork`. The capability check occurs before `requestData`, but a recovered request can later throw `DaemonCapabilityUnavailableError`; catch that post-reconnect error and invoke `legacyFork(entryId, options)`.
Evidence trail:
packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts:245-248, 1232-1249, 1301-1311, 1519-1533 at REVIEWED_COMMIT; packages/coding-agent/src/modes/daemon/daemon-client.ts:72-84, 282-285, 293-318, 431-460 at REVIEWED_COMMIT; git diff MERGE_BASE REVIEWED_COMMIT -- packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 93f9493. Configure here.
|
|
||
| private legacyFork( | ||
| entryId: string, | ||
| options?: AgentConnectionForkOptions, |
There was a problem hiding this comment.
Legacy naming left in fork path
Low Severity
The in-place fallback is named legacyFork, and related protocol comments and test titles also use “legacy” vocabulary. Project review rules disallow migration-era terms like legacy in names, comments, and test titles even when an older protocol path remains for compatibility.
Additional Locations (2)
Triggered by project rule: Review rules
Reviewed by Cursor Bugbot for commit 93f9493. Configure here.
| // the create; on timeout/transport errors the worker may already be | ||
| // live on that file. | ||
| if (error instanceof Error && this.definitiveRequestErrors.has(error)) { | ||
| await unlink(sessionPath).catch(() => undefined); |
There was a problem hiding this comment.
🟠 High agent-connection/daemon-agent-connection.ts:1377
A definitive create error can cause this client to unlink a fork file that another client has already created and attached, deleting that session's backing JSONL from disk. Because definitiveRequestErrors only describes the response and does not establish ownership of sessionPath, a SessionAlreadyActiveError here can remove a live session; only clean up the export after proving this request created it.
🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file @packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts around line 1377:
A definitive `create` error can cause this client to unlink a fork file that another client has already created and attached, deleting that session's backing JSONL from disk. Because `definitiveRequestErrors` only describes the response and does not establish ownership of `sessionPath`, a `SessionAlreadyActiveError` here can remove a live session; only clean up the export after proving this request created it.
Evidence trail:
packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts:1363-1379, 1623-1632 @ 3d0201634920fb8fc40047316322399af0da8dbd
packages/coding-agent/src/modes/daemon/daemon-supervisor.ts:2233-2269, 2298-2312 @ 3d0201634920fb8fc40047316322399af0da8dbd
packages/coding-agent/src/core/session-lease.ts:20-32, 268-283 @ 3d0201634920fb8fc40047316322399af0da8dbd


Fixes ENG-5180.
What this does
/forkno longer kills the session you fork from. The original keeps running — with its subagents, scheduled heartbeats, and warm state — and stays visible in the agents view. You still land in the new forked chat immediately.What the problem was
Forking used to replace the current session in place: the running session was shut down, its subagents were closed, its scheduled heartbeats were silently handed to the fork, and its identity was taken over. The original survived only as a file on disk and vanished from the session list — "when I do /fork, the original session disappears" (user report).
How it works now
Changes
fork_exportdaemon command plus the client-side fork flow rewrite (+179/−21 source lines across five files).Checks
npm run checkclean; 138 targeted tests pass (108 re-run after merging the newest main).Known cosmetic quirk (pre-existing display behavior, left for a follow-up)
Right after forking, the agents view may briefly group the fork under the original (it is linked as the fork's parent) until the fork's own summary appears; a killed fork also shows as a completed child row when expanding the original. The underlying state is always correct.
Note
Medium Risk
Touches daemon protocol, session identity, leases, and reattach/cleanup. Failure paths are covered, but a bad reattach can still leave the client on an unexpected session.
Overview
In daemon mode,
/forkno longer shuts down the source session. It copies the chosen branch to a new session file, starts a separate worker, and switches the client to it. The original stays listed and running, including subagents, heartbeats, and leases. The fork starts fresh (no copied kernel state).Adds
exportForkBranchonAgentSessionRuntimeand a capability-gatedfork_exportcommand (schema revision 21).DaemonAgentConnection.forkuses export → create → reattach, with cleanup on failed create/reattach, and serializes overlappingfork/switchSessionso they cannot interleave. Falls back to the old in-placeforkwhen the daemon lacks the capability or the session is not persisted.In-process mode still replaces the session in place. Extension docs now distinguish daemon vs in-process fork lifecycle.
Reviewed by Cursor Bugbot for commit 3d02016. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Keep original session running when forking via new
fork_exportdaemon commandfork_exportdaemon command and server capability (protocol schema rev 21) that exports a forked branch to a new session file without stopping the source runtime.DaemonAgentConnection.forknow prefersfork_export: it exports the branch, creates a new worker for the forked session, and reattaches the client to it. The original session stays running and listed. Falls back to legacy in-place fork when the daemon lacks the capability or the session is not persisted.AgentSessionRuntime.exportForkBranchto flush the source session and write a branched session file, returningnullfor in-memory sessions.switchSessiontransitions viawithSessionTransitionto prevent interleaving races.DaemonAgentConnection.forkin daemon-agent-connection.ts for the full error-path cleanup.Macroscope summarized 3d02016.