Repository navigation
fix(sessions): Incognito reset leaves the session's managed worktree behind - #165185
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed October 7, 2026, 2:02 PM ET / 18:02 UTC (Revision 12). ClawSweeper reviewWhat this changesResetting an Incognito session now removes its managed Git checkout safely and returns recovery details when cleanup preserves it. Merge readiness✅ Ready for maintainer review This PR remains necessary: current main and the latest stable release still omit Incognito reset worktree cleanup. The bounded repair has sufficient runtime proof and no blocking findings. Priority: P2 Review scores
Verification
How this fits togetherThe Gateway coordinates session reset and deletion across runtime resources and stored session state. Managed worktree cleanup snapshots the session’s Git checkout before removal and reports any checkout retained for safety. flowchart TD
A[Incognito reset request] --> B[Session lifecycle fence]
B --> C[Delete exact session row]
C --> D[Managed checkout cleanup]
D --> E{Safe to remove?}
E -->|Yes| F[Snapshot and remove checkout]
E -->|No| G[Preserve checkout and report details]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +20, Tests +114. Total +134 across 3 files. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Technical reviewBest possible solution: Use the existing deletion cleanup owner for Incognito reset while preserving snapshot safety and visible recovery details. Do we have a high-confidence way to reproduce the issue? Yes: current-main source exposes the missing cleanup, and the supplied before/after Gateway transcript exercises hidden Incognito spawn followed by reset. This reviewer did not execute a current-main reproduction. Is this the best way to solve the issue? Yes: the patch reuses the established cleanup owner and its safety checks, with additive preservation reporting and focused regressions. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 24d311dcf66d. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (11 earlier review cycles; latest 8 shown)
|
chessing234
left a comment
There was a problem hiding this comment.
Reviewed commit 57b3973e47cb3393d392d9b28f898d5600101a73.
One correctness issue needs correction; see the inline finding.
Validation: source and contract inspection only; runtime tests were not run.
| @@ -1026,6 +1027,11 @@ export async function performGatewaySessionReset(params: { | |||
| return deleted; | |||
| } | |||
| handleSessionStateSessionDeleted(target.canonicalKey, agentId); | |||
| await removeSessionWorktree({ | |||
There was a problem hiding this comment.
[P2] Return preserved-worktree details from Incognito reset
removeSessionWorktree() deliberately preserves the checkout and returns { id, branch, path, reason } when snapshot creation or removal fails. This call discards that result after deleting the Incognito session; the reset result and sessions.reset response then report deletion success without recovery details. A snapshot failure (for example, disk full) leaves the checkout behind without giving the caller its location/reason, and the deleted session cannot be used to retry cleanup. Capture and propagate worktreePreserved through the reset result and Incognito response, as sessions.delete already does, and add a preservation/failure-path regression.
There was a problem hiding this comment.
Fixed in 1a7532e9078.
- The Incognito branch of
performGatewaySessionResetnow keeps the result ofremoveSessionWorktreeand returns it asworktreePreserved(src/gateway/session-reset-service.ts). Thesessions.resethandler adds it to the Incognito response the same waysessions.deletedoes (src/gateway/server-methods/sessions-mutations.ts).sessions.resethas no result schema, so this needs no protocol change. - New test
sessions.reset reports a preserved Incognito worktree like sessions.deleteholds a live run lease on the session's worktree and resets the session. Before the fix it failed because the response had noworktreePreserved. Now it passes, with reasonbusy, and the checkout and its registry record are still there. The existing removal test also asserts that a clean removal returns noworktreePreserved. server.sessions.delete-worktree-lifecycle.test.tspasses (10 tests), and so do the 14 nearby reset, Incognito, and delete test files (121 tests).oxfmt --check,oxlint,git diff --check,tsgocore, and the src test-shard typecheck are clean.
Not verified: the snapshot-failure path through reset. It goes through the same removeSessionWorktree return value that the sessions.delete snapshot-failure test already covers.
🤖 Written and posted by an AI agent (Claude Code) on behalf of @jayzhou2309.
b5792ef to
40946f0
Compare
40946f0 to
b89dde4
Compare
b89dde4 to
109f8b9
Compare
|
Landed in 3d399ac. Thanks @jayzhou2309! Verified on a built Gateway: resetting an Incognito session now removes its worktree checkout, git registration and branch, keeping a snapshot of unsaved work. A durable reset keeps its worktree. |
…behind (openclaw#165185) Remove an Incognito session's managed worktree checkout, git registration, and branch when the session is reset, keeping a snapshot of unsaved work. A durable (non-Incognito) reset keeps its worktree binding as before. Related: openclaw#159304 Co-authored-by: Ayaan Zaidi <[email protected]>
Related: #159304
What Problem This Solves
Fixes: resetting an Incognito session that owns a managed worktree leaves that checkout on disk and live in the worktree registry after the session is gone, while deleting the same session removes it.
User Impact
User impact: an Incognito reset now removes the session's managed worktree the same way an Incognito delete does. Uncommitted work is snapshotted first. A checkout that cannot be removed safely (a live run lease, an owner mismatch, a failed snapshot) is kept, and the reset response reports it as
worktreePreserved, the same waysessions.deletedoes. Durable sessions, and resets of sessions with no worktree, are unchanged.Why This Change Was Made
#159304 lists the teardown steps that
sessions.deleteruns and Incognitosessions.resetskips. #159207 fixed the terminal row. This PR fixes the "physical worktree removal" row only. The controller, partial-persistence, and worker-inference drain rows stay open on the issue.sessions.deletecallsremoveSessionWorktreeafter the row is deleted. The Incognito branch ofperformGatewaySessionResetdeleted the row throughdeleteIncognitoSessionForResetand returned without that call. The fix adds the same call at the same point (afterhandleSessionStateSessionDeleted, inside the reset lifecycle mutation), with reasonsession-reset. The worktree id comes from the entry captured before deletion.deleteIncognitoSessionForResetdeletes only when the stored row, itsworktreeincluded, still equals that entry, so the id always belongs to the deleted row.How an Incognito session gets a worktree. A hidden native subagent spawned from an Incognito parent is itself Incognito and can own a managed worktree.
sessions_spawnwithoutvisible: truepassesworktreetospawnSubagentDirect(src/agents/tools/sessions-spawn-tool.ts:619). The child key gets:subagent:incognito-when the requester is Incognito (src/agents/subagents/spawn/subagent-spawn-child-plan.ts:94). The child row goes to the Incognito store with apendingWorktree(src/agents/subagents/spawn/subagent-spawn-session-patch.ts:152and:207). The child's first turn then creates the checkout and binds it to the row (src/gateway/agent-turn/agent-run-execution-phase.ts:277, which callsprepareSessionWorkspaceForRuninsrc/gateway/server-methods/session-create-project.ts). An earlier version of this description said no production path creates such a session. That was wrong. It was based on the dashboardsessions.createpath, which does reject Incognito withworktree: true("incognito sessions are web-only"). The hidden spawn path does not go through that check. The live run below uses it.When
removeSessionWorktreekeeps the checkout, the reset result carries its{ id, branch, path, reason }asworktreePreserved, and thesessions.resethandler adds it to the Incognito response, assessions.deletedoes.sessions.resethas no result schema, so this needs no protocol change.Evidence
sessions.reset removes an Incognito session worktree like sessions.deletetosrc/gateway/server.sessions.delete-worktree-lifecycle.test.ts. It creates an Incognito session, binds a session-owned managed worktree to its row, and resets it. On upstream/main 989bd3f, the reset succeeds and the row is deleted, but the registry record has noremovedAtand the checkout is still there (1 failed). Withsessions.deletein place ofsessions.reset, the same setup passes. That shows the gap is in reset only.sessions.reset reports a preserved Incognito worktree like sessions.delete, holds a live run lease on the worktree and resets the session. Before that commit the response had noworktreePreserved. After it, the response reports the checkout with reasonbusy, and the checkout and its registry record stay.server.sessions.reset-*files,delete-worktree-lifecycle,create.incognito,incognito-lifetime,archive-terminal, and the twoserver-close.incognito-*files. That is 14 files and 121 tests throughscripts/test-projects.mts.oxfmt --check,oxlint -c .oxlintrc.json, andgit diff --checkare clean on the changed files.tsgopasses for core and for all 21 src test shards.autoreview(Codex) is scoped-clean, with the verdict "patch is correct". A second model checked the lifecycle fence, the captured worktree id, the throw behavior, and snapshot safety, and found no defect.Real behavior proof
Two real Gateways, one built from the merge base (96ccec4) and one from this branch (b89dde4cb61). Each was started with
openclaw gateway run --auth token --port 19789 --bind loopbackon a freshOPENCLAW_STATE_DIRwhose agent workspace is a new Git repo. Calls went throughopenclaw gateway call. Two config entries make the run possible without a paid model:gateway.tools.allow: ["sessions_spawn"], sotools.invokecan call the samesessions_spawntool a model would call. It is on the default HTTP deny list otherwise.models.providers.stub,api: "openai-completions"). It answers every chat completion with a fixed text, so the child's first turn runs tostopReason=stop.Steps, identical on both builds:
sessions.create {agentId: "main", incognito: true}returnsagent:main:dashboard:incognito-<id>.tools.invoke {name: "sessions_spawn", sessionKey: <parent>, args: {task, worktree: true, label: "proof child"}}returnsstatus: "accepted"andchildSessionKey: agent:main:subagent:incognito-<id>. The log showsmanaged worktree preparation kind=managed ... outcome=returnedduring the child's first turn.worktrees.listandgit worktree listbefore the reset.sessions.reset {key: <child>}returns{ok: true, deleted: true}on both.worktrees.list,git worktree list, andgit branchafter the reset.sessions.resetof the Incognito childstate/worktrees/<repo>/proof-childbefore resetgit worktree listin the workspacemainandproof-child [openclaw/proof-child]mainonlyopenclaw/proof-childworktrees.list)ownerKind: session, owner is the child key, noremovedAt, nosnapshotRefremovedAtset,snapshotRef: refs/openclaw/snapshots/<id>Redacted terminal output from this branch:
And from the merge base:
The model in this run is a stub, and the spawn came from
tools.invokerather than a model's tool call. Both reach the samesessions_spawntool and the same native spawn, first-turn, and reset code. The preserved-worktree branch (live run lease) is covered by the second test above, not by this live run.Test cost
Local runs of
src/gateway/server.sessions.delete-worktree-lifecycle.test.tswithnode scripts/run-vitest.mjs, one run each on a busy laptop. Merge base 96ccec4: 8 tests, 66s. This branch: 10 tests, 108s warm. The two new tests take 18.5s and 14.8s when run alone. CI seconds were not measured.AI disclosure: an AI agent (Claude Code) found the gap in source, reproduced it with the test above (plus the
sessions.deletecontrol run), wrote the test and the fix, ranautoreviewand a second-model review, and ran these gates:node scripts/run-vitest.mjson the new test,node --import ./scripts/tsx.mjs scripts/test-projects.mtson the 14 files listed,oxfmt --check,oxlint -c .oxlintrc.json,scripts/run-tsgo.mjs -p tsconfig.core.json,scripts/run-tsgo-core-test-shards.mjs src, andgit diff --check. On 2026-10-07 it measured the test timings above and ran the live Gateway proof on the merge base and this branch, with the local stub model and the config described there.🤖 Written and posted by an AI agent (Claude Code) on behalf of @jayzhou2309.
Maintainer verification
No overlap with Pash/Sarah changes.
Built and exercised the real Gateway in isolated Docker against current main
bcc3b58b7c614209c6ce01358087c40cca4aeaf1and candidate109f8b97f14d0b4246107841fe32fa783868a699. The flow usedsessions.create,tools.invoke→sessions_spawnwith a managed worktree, a completed first turn, andsessions.resetin a new Git workspace.deleted: true, but the Incognito checkout, Git worktree registration, and branch survived.unsaved.txtfrom the snapshot returned its original contents.tsgopassed. A separate realgpt-5-miniCLI turn with low reasoning and a 4096-token output budget returnedLIVE_GATE_OK.The lifecycle reproduction uses a local model stub; the separate provider gate uses the real provider. The patch does not change transcript handling or expand reset deletion beyond the session-owned managed worktree and its branch. The broader issue remains related, not closed by this partial repair.
Merge danger: a one-way filesystem cleanup for affected Incognito resets, mitigated by the existing retained snapshots. Reverting stops future cleanup but does not recreate removed checkouts. Blast radius is Incognito sessions owning managed worktrees; durable reset behavior is unchanged.