Repository navigation
fix(gateway): settle terminal runs before session mutations - #166784
Conversation
Let manual compaction and history edits join existing terminal writers with a bounded wait outside lifecycle locks. Stop retaining session execution admission for metadata-only chat title generation. Preserve active and queued work exclusion and revalidate session ownership after settlement.
|
🦞👀 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, 8:30 PM ET / October 8, 2026, 00:30 UTC (Revision 2). ClawSweeper reviewWhat this changesThe PR lets compaction, fork, rewind, and branch switching wait for completed reply cleanup while removing background session naming from the active-turn gate. Merge readiness✅ Ready for maintainer review This remains a useful fix: current main still lets background title generation block session mutations. No actionable introduced defect was found, and the recorded live evidence supports the changed behavior. Priority: P2 Review scores
Verification
How this fits togetherGateway session methods receive requests to compact or edit conversation history. They coordinate with reply writers and session lifecycle ownership before changing transcripts, while automatic naming updates session metadata separately. flowchart TD
A[Session mutation request] --> B[Check competing live work]
B -->|Live work| C[Refuse with retry guidance]
B -->|Completed writers| D[Wait for captured cleanup]
D --> E[Recheck ownership under lifecycle lock]
E --> F[Compact or edit history]
G[Background title generation] --> H[Guarded metadata update]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +76, Tests +476, Docs +2. Total +554 across 8 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep terminal cleanup with its existing owners and allow history mutations only after settlement and fresh lifecycle checks, with metadata-only naming independent of turn admission. Do we have a high-confidence way to reproduce the issue? Yes: request sessions.compact immediately after a first reply while background naming is pending; current-main source establishes the conflicting admission, and the body records real before/after runs. This reviewer did not execute the reproduction. Is this the best way to solve the issue? Yes: removing metadata-only admission and joining captured terminal owners uses existing lifecycle contracts without adding a parallel owner or changing live-turn policy. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 4f4d61a73b5e. 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 (1 earlier review cycle)
|
|
Merged via squash.
|
…#166784) * fix(gateway): settle terminal runs before session mutations Let manual compaction and history edits join existing terminal writers with a bounded wait outside lifecycle locks. Stop retaining session execution admission for metadata-only chat title generation. Preserve active and queued work exclusion and revalidate session ownership after settlement. * fix(gateway): scope terminal settlement to captured owners
What Problem This Solves
Running
/compactfrom the Control UI right after a reply finishes fails with "has an active run; retry after it finishes". The UI callssessions.compactdirectly. On main 9aa7455 the call is refused for 2.2–4.9 s after the chatfinaland the lifecycleend, on both the built-in OpenClaw runtime and the Codex runtime, with and without tools. A user who types/compactas soon as the answer appears sees an error for work that is already finished.The same window applies to fork, rewind, and branch switching from a message, which share the active-run gate.
User Impact
/compact, fork, rewind, and branch switching right after a reply now succeed on the first attempt instead of erroring.Why This Change Was Made
The window was background session-title generation. After the foreground reply released, the title job acquired a session work admission and held it for the whole naming model call, and compaction counted that admission as an active run. Naming only patches metadata through the title writer, which already checks the exact session identity and existing name before writing, so it does not need a turn admission. Title generation now holds no session lease. It still waits for
turn.releasedbefore starting and keeps the Gateway shutdown custody thatrunWithGatewayIndependentRootWorkContinuationprovides. This also subsumes #162689: rollover can no longer drain a title lease because none exists, and that PR's rollover regression still passes unchanged.Terminal publication can also legitimately precede the last transcript writes and owner cleanup of a finished run. A small shared helper,
waitForTerminalSessionRunSettlement, handles this. It captures only the owners that have already published a terminal outcome: chat runs with an observed terminal, plus committed reply operations, including retained failed replies that still own cleanup. It awaits their existing settlement promises within the existing 15 s session-admission budget, outside the lifecycle locks. Any competing admission or nonterminal owner is refused immediately, so a live turn is never waited on. The caller's own in-band execution is excluded by exact identity while other terminal writers are still awaited. Compaction, fork, rewind, and branch switching call it. The ownership, active-run, worker-inference, and queued-work checks that follow are unchanged. Reset, delete, and archive keep their existing drains.Known residual: the chat
finalevent is published a few milliseconds before the run's terminal bookkeeping is recorded. A client that callssessions.compactwithin about 70 ms of the final can still get one "active run" refusal; a retry about 100 ms later succeeds. That ordering is tracked as a follow-up, not hidden here.This is complementary to #166163, which changed the in-band
/compactchat command's handling of a truly active turn.Evidence
Live (dev Gateway built from this head, fresh state, real OpenAI provider;
sessions.compactpolled every 100 ms from the chatfinal):Tests (
--maxWorkers=1, isolated TMPDIR):With only the production files restored to base, the regressions fail (8 failed / 6 passed, 49.4 s). That includes both review findings: the live-turn wait and the skipped terminal writer during an in-band command. Types, scoped lint, format, docs, and the import-cycle check (0) pass. Autoreview is clean through P2. The earlier CI failure in
subagent-control.test.tswas a main fixture issue fixed separately by #166807.Production +129/-53, tests +496/-20, docs +2 (
docs/concepts/compaction.md).