Skip to content

fix(sessions): let agents archive their session after completing a turn - #114522

Merged
steipete merged 1 commit into
mainfrom
codex/fix-agent-session-self-archive
Jul 27, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/fix-agent-session-self-archive

Conversation

@steipete

@steipete steipete commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where users asking an agent to archive its current non-main session receive Cannot archive a session with an active run because the model-selected archive tool call runs inside that same active agent turn.

Why This Change Was Made

The session-lifecycle maintainer explicitly requested this product behavior: special-case agent self-archive, finish the current response, and then archive the session. Schedule only a non-main self-archive after the initiating admission and any competing work have completed; guard immediate metadata and deferred archival with lifecycle-locked session identity and revision; retry genuinely transient Gateway failures with shutdown-safe backoff; and cancel requests if a session is reset. Preserve upstream plugin-ownership authorization, main-session protection, ordinary active-run validation, and generated native Gateway protocol compatibility.

User Impact

Users can ask an agent to archive its own session. The agent completes its final response and transcript, and the session subsequently appears under Archived without accidentally archiving a reset replacement.

Evidence

  • Reproduced the original failure against a real running Gateway on Blacksmith Testbox, with a deterministic OpenAI-compatible streaming Responses fixture, an actual model-selected sessions tool call, and persisted sessions.list: the baseline produced the active-run error and left archived: false.
  • Replayed the exact live scenario on the current-main-compatible implementation: one successful sessions tool call, status: scheduled, followed by archived: true, hasActiveRun: false, and status: done.
  • Passed 166 focused regressions, including the newly landed upstream plugin-ownership cases: pnpm test packages/gateway-protocol/src/index.test.ts src/agents/tools/sessions-tool.test.ts src/agents/tools/sessions-tool.self-archive.test.ts src/sessions/session-lifecycle-admission.test.ts src/gateway/server.sessions.patch-expected-identity.test.ts src/gateway/server.sessions.delete-lifecycle.test.ts src/gateway/server.sessions.reset-cleanup.test.ts src/gateway/server.sessions.plugin-ownership.test.ts.
  • Regenerated the additive native Swift Gateway protocol model using the canonical schema generator; Android/Kotlin outputs are unchanged.
  • Passed remote pnpm check:changed, including formatting, production and test type checks, lint, protocol and plugin compatibility, schema, database-first, and runtime import-cycle guards.
  • Fresh independent .agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main --stream-engine-output: clean; no actionable findings.
  • Maintainer decision: the session-lifecycle owner requested and approved deferred self-archive, including waiting for competing work, cancellation on reset, and preserving the completed response.
  • AI-assisted; the issue was reproduced, race conditions and transient failures were regression-tested, and live behavior was independently verified.
  • Current hosted release-gate blocker is independently verified on the unchanged main base: apps/.i18n/native-source.json contains 5,317 source entries while both apps/.i18n/native/zh-CN.json and apps/.i18n/native/zh-TW.json contain only 5,290. These existing localization artifacts are outside this session-lifecycle change; the canonical session and generated-protocol checks pass.

@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui gateway Gateway runtime agents Agent runtime and tooling size: L maintainer Maintainer-authored PR labels Jul 27, 2026
@steipete
steipete force-pushed the codex/fix-agent-session-self-archive branch from fc78129 to 9e1eb14 Compare July 27, 2026 10:46
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jul 27, 2026
@clawsweeper

clawsweeper Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 27, 2026, 7:32 AM ET / 11:32 UTC.

ClawSweeper review

What this changes

The PR lets an agent schedule archival of its own non-main session until its active turn and competing session work complete, using identity-checked Gateway patches to avoid archiving a reset replacement.

Merge readiness

⚠️ Ready for maintainer review - 4 items remain

Keep this PR open for the protected session-lifecycle review. The branch has credible real-Gateway proof and focused regression coverage for deferred self-archive, but it changes delayed session mutation and public Gateway patch semantics, so the lifecycle owner should confirm the final rebased behavior before merge.

Priority: P2
Reviewed head: bb3e1b4a6c24119b09ecd9766c783528d2cb3ecb
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Strong real behavior evidence and focused regression coverage support a good patch, with protected lifecycle semantics still requiring maintainer confirmation.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR body documents a real running-Gateway before/after scenario with a model-selected sessions tool call and persisted session state; it directly demonstrates the reported behavior and its after-fix result.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body documents a real running-Gateway before/after scenario with a model-selected sessions tool call and persisted session state; it directly demonstrates the reported behavior and its after-fix result.
Evidence reviewed 5 items Deferred lifecycle behavior: The proposed sessions tool schedules self-archive only after the initiating admission releases and guards the later mutation with the session identity and lifecycle revision; the accompanying self-archive tests cover completion, competing work, reset cancellation, and retry behavior.
Gateway compare-and-swap contract: The protocol schema adds optional non-empty expectedSessionId and expectedLifecycleRevision fields, while the Gateway mutation handler rejects a stale session identity before applying the patch.
Regression coverage: The PR adds protocol validation, Gateway stale-identity, lifecycle-admission, and tool self-archive tests; its supplied evidence reports a real Gateway reproduction of the former active-run rejection and an after-fix scheduled-to-archived result.
Findings None None.
Security None None.

How this fits together

The sessions tool runs inside an agent turn and asks the Gateway to change persisted session metadata. This change defers a self-requested archive until lifecycle admissions release, then sends a compare-and-swap patch so reset or replacement sessions remain protected.

flowchart TD
  A[Agent session turn] --> B[Sessions tool]
  B --> C{Self-archive of non-main session?}
  C -- No --> D[Normal Gateway session patch]
  C -- Yes --> E[Wait for admitted session work]
  E --> F[Identity-checked Gateway patch]
  F --> G[Archived session state]
  D --> G
Loading

Decision needed

Question Recommendation
Does the session-lifecycle owner approve the final rebased semantics for a self-archive request that waits through competing work, cancels on reset, and retries transient Gateway failures? Approve protected lifecycle semantics: Confirm the deferred archive, cancellation, retry, and identity-guard behavior as the supported session lifecycle contract, then allow normal merge gates to evaluate the rebased head.

Why: This is a protected session-state change whose safety depends on intended lifecycle policy rather than a mechanically provable local patch defect.

Before merge

  • Resolve merge risk (P1) - The deferred operation survives the initiating tool call; a lifecycle-owner review should confirm that cancellation, retry, shutdown, competing-admission, and reset/replacement behavior remain the intended session-state contract after rebasing.
  • Resolve merge risk (P1) - Two optional public Gateway patch parameters are additive, but generated native-client compatibility and stale-client behavior should be checked against the final merge head rather than only the submitted base.
  • Complete next step (P2) - The remaining action is protected lifecycle-owner confirmation of the final rebased state-machine contract, not an autonomous code repair.
Agent review details

Security

None.

PR surface

Source +196, Tests +777, Other +8. Total +981 across 9 files.

View PR surface stats
Area Files Added Removed Net
Source 4 206 10 +196
Tests 4 777 0 +777
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 8 0 +8
Total 9 991 10 +981

Review metrics

Metric Value Why it matters
Gateway patch contract 2 optional parameters added, 0 removed The public sessions.patch schema gains identity guards that native protocol clients must continue to serialize compatibly.

Stored data model

Persistent data-model change detected: serialized state: packages/gateway-protocol/src/schema/sessions.ts, serialized state: src/agents/tools/sessions-tool.self-archive.test.ts, serialized state: src/agents/tools/sessions-tool.ts, serialized state: src/gateway/server-methods/sessions-mutations.ts, serialized state: src/sessions/session-lifecycle-admission.test.ts, serialized state: src/sessions/session-lifecycle-admission.ts, and 5 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Confirm lifecycle semantics before merge (recommended)
    Have the protected session-lifecycle reviewer validate reset, shutdown, retry, and competing-admission behavior on the rebased head before merging.
  2. Accept the documented delayed-mutation contract
    Maintain the current implementation if the owner explicitly accepts deferred archival and the additive Gateway identity fields as the public behavior.

Technical review

Best possible solution:

Land a rebased version only after the session-lifecycle owner confirms the delayed archive state machine and the additive Gateway protocol fields preserve existing ordinary session patches.

Do we have a high-confidence way to reproduce the issue?

Yes. The PR supplies a concrete real-Gateway path: an agent-selected sessions tool call attempts to archive its own active non-main session, which previously returned the active-run error; the after-fix run reports scheduled and then archived state.

Is this the best way to solve the issue?

Yes, subject to protected lifecycle review. Deferring only non-main self-archive and using an identity-checked Gateway mutation is narrower and safer than weakening the existing active-run protection for all archive requests.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against a62abee4271a.

Labels

Label justifications:

  • P2: This is a bounded session-lifecycle improvement with meaningful user impact but no evidence of an urgent current outage.
  • merge-risk: 🚨 compatibility: The PR adds two optional public Gateway sessions.patch parameters and generated native protocol fields.
  • merge-risk: 🚨 session-state: A delayed archive can affect persisted session identity, reset handling, and lifecycle ordering after the initiating turn exits.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body documents a real running-Gateway before/after scenario with a model-selected sessions tool call and persisted session state; it directly demonstrates the reported behavior and its after-fix result.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body documents a real running-Gateway before/after scenario with a model-selected sessions tool call and persisted session state; it directly demonstrates the reported behavior and its after-fix result.

Evidence

What I checked:

  • Deferred lifecycle behavior: The proposed sessions tool schedules self-archive only after the initiating admission releases and guards the later mutation with the session identity and lifecycle revision; the accompanying self-archive tests cover completion, competing work, reset cancellation, and retry behavior. (src/agents/tools/sessions-tool.ts, bb3e1b4a6c24)
  • Gateway compare-and-swap contract: The protocol schema adds optional non-empty expectedSessionId and expectedLifecycleRevision fields, while the Gateway mutation handler rejects a stale session identity before applying the patch. (packages/gateway-protocol/src/schema/sessions.ts, bb3e1b4a6c24)
  • Regression coverage: The PR adds protocol validation, Gateway stale-identity, lifecycle-admission, and tool self-archive tests; its supplied evidence reports a real Gateway reproduction of the former active-run rejection and an after-fix scheduled-to-archived result. (src/agents/tools/sessions-tool.self-archive.test.ts, bb3e1b4a6c24)
  • Review continuity: The prior two ClawSweeper cycles found no discrete patch defect; the current head is the latest force-pushed revision reviewed for the same deferred self-archive design. (src/sessions/session-lifecycle-admission.ts, bb3e1b4a6c24)
  • Protected review state: The PR carries the repository's maintainer label and is marked ready for maintainer look, so cleanup automation must not close it despite positive proof and test evidence. (bb3e1b4a6c24)

Likely related people:

  • steipete: Authored the current deferred self-archive implementation and its focused lifecycle, Gateway, protocol, and regression-test changes; no separate current-main history owner could be established from the available read-only evidence. (role: session-lifecycle implementation contributor; confidence: low; commits: bb3e1b4a6c24; files: src/agents/tools/sessions-tool.ts, src/sessions/session-lifecycle-admission.ts, src/gateway/server-methods/sessions-mutations.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (2 earlier review cycles)
  • reviewed 2026-07-27T10:51:45.498Z sha 9e1eb14bcc348b4030fcf13d3abab9b77be71259 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-27T11:24:11.582Z sha bb3e1b4 :: needs maintainer review before merge. :: none

@steipete
steipete force-pushed the codex/fix-agent-session-self-archive branch from 9e1eb14 to bb3e1b4 Compare July 27, 2026 11:13
@clawsweeper clawsweeper Bot added the merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. label Jul 27, 2026
@steipete
steipete merged commit 91f0449 into main Jul 27, 2026
266 of 271 checks passed
@steipete
steipete deleted the codex/fix-agent-session-self-archive branch July 27, 2026 11:46
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Labels

agents Agent runtime and tooling app: web-ui App: web-ui gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant