Skip to content

fix(acp): closed sessions start fresh after restart (#107487) - #130601

Closed
MoerAI wants to merge 2 commits into
openclaw:mainfrom
MoerAI:fix/acp-close-durable-reset
Closed

MoerAI wants to merge 2 commits into
openclaw:mainfrom
MoerAI:fix/acp-close-durable-reset

Conversation

@MoerAI

@MoerAI MoerAI commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes #107487

What Problem This Solves

Summary

Fixes an issue where users who ran /acp close on a persistent ACPX session could have that same backend session resume after OpenClaw restarted. The command reported the session closed, but the persistent ACPX record remained reusable.

Why This Change Was Made

Root Cause

  • src/auto-reply/reply/commands-acp/lifecycle.ts closed manual sessions without requesting discardPersistentState.
  • The manager already has the canonical unsupported-close recovery: it calls prepareFreshSession when an ACP agent cannot implement session/close.
  • extensions/acpx/src/runtime.ts implemented prepareFreshSession only with a process-local hidden-key set. That prevented reuse in the current process but did not survive restart.
  • Pinned [email protected] already defines the durable contract: successful discard writes closed: true plus acpx.reset_on_next_ensure: true, and the next ensure refuses to reuse that record.

Execution path:

/acp close -> AcpSessionManager.closeSession -> unsupported backend close -> AcpxRuntime.prepareFreshSession -> persisted reset marker

Changes

  • Manual /acp close now requests persistent-state discard.
  • The ACPX reset-aware store persists the same reset_on_next_ensure marker used by successful upstream discard close, while retaining the existing process-local hide for the current process.
  • Manager recovery preserves best-effort missing-plugin cleanup but propagates an invoked preparation failure before clearing metadata.
  • Added exact command-intent coverage and a restart-boundary regression that creates a second runtime instance over the same store.
  • No public API, config, protocol, dependency, SQLite schema, or schema-version change.

Production delta is +48/-17 (+31 net). The growth is limited to two owner boundaries that cannot be expressed by existing state: the private durable ACPX reset marker path and precise propagation of an invoked preparation failure while preserving existing missing-plugin cleanup. Test delta is +86/-7.

User Impact

After /acp close, the next persistent ACP turn starts a fresh backend session even when the agent does not support session/close and OpenClaw restarts between the close and the next turn.

Evidence

Real Behavior Proof

I ran the production AcpSessionManager, /acp close handler, AcpxRuntime, and real file session store against an external ACP stdio agent in three separate Node 24 OS processes (seed -> close -> resume). The agent advertised session/resume but intentionally did not advertise session/close. No runtime or manager mocks were used.

State boundary Current main before This PR after
Stored session after /acp close fake-session-001, acpx: {} fake-session-001, acpx.reset_on_next_ensure: true
First ACP method in a new OpenClaw process session/resume(fake-session-001) session/new -> fake-session-002
User-visible close result Reported closed with no durable reset Reported closed and surfaced the unsupported session/close notice

Before:

close record: {"acp_session_id":"fake-session-001","closed":true,"acpx":{}}
restart:       {"method":"session/resume","sessionId":"fake-session-001"}

After:

close record: {"acp_session_id":"fake-session-001","closed":true,"acpx":{"reset_on_next_ensure":true}}
restart:       {"method":"session/new","sessionId":"fake-session-002"}

This covers the merge-risk: session-state boundary in the original execution order and across a real process restart.

Review failure-path proof used the same production setup with the actual ACPX sessions directory and record made read-only before close:

close reply: ACP error (ACP_TURN_FAILED): EACCES: permission denied
next: Retry, or use `/acp cancel` and send the message again.
record:      {"acp_session_id":"fake-session-001","closed":true,"acpx":{}}
restart:     {"method":"session/resume","sessionId":"fake-session-001"}

The failed durable write is visible and actionable, metadata is not cleared, and no reset marker is claimed. The old identity remains resumable because the close was not acknowledged.

Test

  • Failing first:
    • command regression received discardPersistentState: undefined
    • restart regression observed zero durable store writes
  • Review failing first:
    • a rejected prepareFreshSession still resolved close with metaCleared: true
    • after the fix, manager close rejects before its metadata-clear write
  • pnpm test src/auto-reply/reply/commands-acp.test.ts src/acp/control-plane/manager.test.ts src/gateway/server.sessions.reset-cleanup.test.ts extensions/acpx/src/runtime.test.ts -- --reporter=dot — 224 passed
  • pnpm test src/acp/control-plane/manager.runtime-resume-state.test.ts src/acp/control-plane/manager.test.ts src/auto-reply/reply/commands-acp.test.ts extensions/acpx/src/runtime.test.ts -- --reporter=dot — 207 passed
  • pnpm test:extension acpx — 253 passed
  • pnpm build — passed
  • node scripts/check-changed.mjs -- extensions/acpx/src/runtime.ts extensions/acpx/src/runtime.test.ts src/acp/control-plane/manager.runtime-resume-state.ts src/acp/control-plane/manager.runtime-resume-state.test.ts src/acp/control-plane/manager.test.ts src/auto-reply/reply/commands-acp/lifecycle.ts src/auto-reply/reply/commands-acp.test.ts — passed
  • Fresh Codex autoreview of the original repair and the review remediation — clean, no accepted/actionable findings

Codex contract check:

Risk

  • Session state: bounded to explicit manual close. Real multi-process proof shows the closed identity cannot resume after restart.
  • Compatibility: no format migration or new field was introduced; this persists the existing marker already owned and consumed by pinned ACPX.

Changelog

Not added: repository policy keeps CHANGELOG.md release-generated for normal PRs.

Notes

  • AI-assisted: yes. The bug was independently reproduced on current main, fixed test-first, and fully validated by MoerAI.
  • Prior PR fix(acp): /acp close leaves acpx session resumable, next message resumes the closed session #111628 identified the same durable marker seam and was self-closed by its author. This PR independently revalidated the current-main defect and the pinned dependency contract, then reproduced the fix through the real multi-process path.
  • Codex P1 review addressed: a real durable-store write failure now stops metadata clearing and returns an actionable ACP error instead of acknowledging close.

@clawsweeper

clawsweeper Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@MoerAI
MoerAI marked this pull request as ready for review August 27, 2026 02:17

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 018846e8cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread extensions/acpx/src/runtime.ts Resolved
@MoerAI

MoerAI commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Addressed the exact-head Codex P1 in 55fc654817726289ccf024cca9b07fe994d483bf.

  • durable preparation write failures now stop metadata clearing and surface an actionable ACP error
  • real read-only file-store proof added to the PR body
  • 207 focused tests and 22 gateway reset sibling tests pass
  • full build and seven-file check:changed pass
  • fresh Codex autoreview reports no findings

@clawsweeper

clawsweeper Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@vincentkoc

Copy link
Copy Markdown
Member

maintainer update: the durable owner-boundary repair is now upstream at openclaw/acpx#522.

the latest head here correctly stops metadata clearing when the local write fails. it still should not land as written, because extensions/acpx/src/runtime.ts synthesizes ACPX-private reset_on_next_ensure state and tryPrepareFreshManagerRuntimeSession() passes only the bare OpenClaw session key. generated one-shot ACPX records use a distinct exact acpxRecordId, so that path can retire the wrong record.

ACPX #522 adds prepareFreshSession(handle) at the persistence owner. it retires the exact record, waits for active-turn finalization, propagates owner/store failures, survives a real three-process restart test, and does not call remote session/close.

please keep this PR open. after the ACPX change is reviewed, merged, and published, this contributor branch can be updated to consume the upstream API with the exact runtime handle. @MoerAI's report and implementation direction remain credited.

@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 27, 2026
@clawsweeper

clawsweeper Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge. Reviewed August 26, 2026, 11:43 PM ET / August 27, 2026, 03:43 UTC.

ClawSweeper review

What this changes

The PR makes manual ACP close discard persistent state and adds a durable ACPX reset marker intended to force a fresh backend session after restart.

Regression provenance

Possible regression — probable (reproduction; reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Needs maintainer review before merge - 4 items remain

Keep this PR open, but block the current head: it repairs the reported current-main gap while retiring ACPX state through the wrong identifier. The member review already identifies the correct upstream owner-boundary path.

Priority: P1
Reviewed head: 55fc654817726289ccf024cca9b07fe994d483bf

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The PR has strong focused behavior evidence, but the current persistence implementation does not preserve ACPX's exact record identity.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (logs): The PR body contains after-fix three-process production-manager, ACPX-runtime, file-store, and external-ACP-agent traces; it proves the equality case, while the distinct-record-ID defect remains a separate correctness blocker.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Verified Sufficient (logs): The PR body contains after-fix three-process production-manager, ACPX-runtime, file-store, and external-ACP-agent traces; it proves the equality case, while the distinct-record-ID defect remains a separate correctness blocker.
Evidence reviewed 7 items Current-main gap: Current main closes the command session without requesting persistent-state discard, so the reported lifecycle remains unfixed there.
Exact record identity is available: The shared runtime handle distinguishes the OpenClaw session key from the backend-local ACPX record identifier.
Wrong key reaches the new persistence write: The added method loads and writes with only sessionKey, whereas normal ACPX handle operations select handle.acpxRecordId before falling back to sessionKey.
Findings 1 actionable finding [P1] Retire the exact ACPX record
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs extensions/acpx/src/runtime.test.ts src/acp/control-plane/manager.runtime-resume-state.test.ts

Result: PASS (completed)

[test] starting test/vitest/vitest.acp.config.ts

 RUN  v4.1.11 /tmp/clawsweeper-live-proof-130601-NBNsqE/target

 ✓  acp  src/acp/control-plane/manager.runtime-resume-state.test.ts (4 tests) 109ms

 Test Files  1 passed (1)
      Tests  4 passed (4)
   Start at  03:44:48
   Duration  1.49s (transform 1.16s, setup 987ms, import 31ms, tests 109ms, environment 0ms)

[test] starting test/vitest/vitest.extension-acpx.config.ts

 RUN  v4.1.11 /tmp/clawsweeper-live-proof-130601-NBNsqE/target

 ✓  extension-acpx  extensions/acpx/src/runtime.test.ts (100 tests) 491ms

 Test Files  1 passed (1)
      Tests  100 passed (100)
   Start at  03:44:50
   Duration  4.10s (transform 2.66s, setup 956ms, import 2.29s, tests 491ms, environment 0ms)

[test] passed 2 Vitest shards in 12.71s
[test] starting test/vitest/vitest.acp.config.ts

 RUN  v4.1.11 /tmp/clawsweeper-live-proof-130601-NBNsqE/target

 ✓  acp  src/acp/control-plane/manager.runtime-resume-state.test.ts (4 tests) 56ms

 Test Files  1 passed (1)
      Tests  4 passed (4)
   Start at  03:45:01
   Duration  761ms (transform 373ms, setup 458ms, import 13ms, tests 56ms, environment 0ms)

[test] starting test/vitest/vitest.extension-acpx.config.ts

 RUN  v4.1.11 /tmp/clawsweeper-live-proof-130601-NBNsqE/target

 ✓  extension-acpx  extensions/acpx/src/runtime.test.ts (100 tests) 447ms

 Test Files  1 passed (1)
      Tests  100 passed (100)
   Start at  03:45:03
   Duration  2.28s (transform 876ms, setup 436ms, import 1.17s, tests 447ms, environment 0ms)

[test] passed 2 Vitest shards in 9.95s

Assertions:

  • PASS expect_output: Tests

How this fits together

ACP close flows from the command handler through the session manager to the ACPX runtime and its persistent session record. That record determines whether the next persistent turn resumes an old backend session or starts a fresh one.

flowchart LR
  A[ACP close command] --> B[Session manager]
  B --> C[ACPX runtime]
  C --> D[Persistent ACPX record]
  D --> E[Next persistent turn]
  E --> F[Resume old session or start fresh]
Loading

Before merge

  • Retire the exact ACPX record (P1) - prepareFreshSession receives only sessionKey, but normal ACPX operations use the backend-local acpxRecordId when it differs. This can mark the wrong record (or none) fresh and then let the manager erase metadata while the real record remains resumable; carry the exact handle to ACPX's owner API and cover distinct IDs.
  • Resolve merge risk (P1) - Merging this head can clear manager metadata after marking no record or a different ACPX record fresh, leaving the actual closed session resumable after restart.
  • Resolve merge risk (P1) - The +31 net production lines duplicate ACPX-owned persistence behavior; the upstream handle-based API should replace this wrapper implementation rather than extend it.
  • Complete next step (P2) - The requested repair depends on the already-directed ACPX API publication and a cross-owner call-shape update; it is not safe to automate against the current package contract.

Findings

  • [P1] Retire the exact ACPX record — extensions/acpx/src/runtime.ts:383-392
Agent review details

Security

None.

PR surface

Source +31, Tests +79. Total +110 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 3 48 17 +31
Tests 4 86 7 +79
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 134 24 +110

Review metrics

Metric Value Why it matters
Production versus test delta production +31 net; tests +79 net The positive production delta is currently a duplicate persistence implementation that the ACPX owner API should absorb.

Stored data model

Persistent data-model change detected: serialized state: extensions/acpx/src/runtime.test.ts, vector/embedding metadata: src/acp/control-plane/manager.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Adopt the ACPX owner API (recommended)
    After ACPX publishes its handle-based API, update this PR to retire the exact record and add distinct-identifier restart coverage before merge.
  2. Pause for the upstream release
    Keep this PR open as credited source work until the upstream ACPX repair is reviewed, merged, and published.

Technical review

Best possible solution:

After ACPX publishes its handle-based preparation API, pass the exact runtime handle through recovery, delete the local record-marker synthesis, and prove a restart with distinct sessionKey and acpxRecordId values.

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

Yes. The PR body provides before-and-after three-process ACP traces, and current-main source confirms the command omits persistent discard; the remaining untested case is a distinct ACPX record ID.

Is this the best way to solve the issue?

No. The local wrapper should not synthesize ACPX-private state from a bare OpenClaw key; the upstream owner API must receive the exact runtime handle.

Full review comments:

  • [P1] Retire the exact ACPX record — extensions/acpx/src/runtime.ts:383-392
    prepareFreshSession receives only sessionKey, but normal ACPX operations use the backend-local acpxRecordId when it differs. This can mark the wrong record (or none) fresh and then let the manager erase metadata while the real record remains resumable; carry the exact handle to ACPX's owner API and cover distinct IDs.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.99

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P1: The unresolved path can silently resume a session that an operator explicitly closed.
  • add merge-risk: 🚨 session-state: The new durable-reset path may retire the wrong ACPX record and leave the actual record resumable after restart.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body contains after-fix three-process production-manager, ACPX-runtime, file-store, and external-ACP-agent traces; it proves the equality case, while the distinct-record-ID defect remains a separate correctness blocker.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (logs): The PR body contains after-fix three-process production-manager, ACPX-runtime, file-store, and external-ACP-agent traces; it proves the equality case, while the distinct-record-ID defect remains a separate correctness blocker.

Label justifications:

  • P1: The unresolved path can silently resume a session that an operator explicitly closed.
  • merge-risk: 🚨 session-state: The new durable-reset path may retire the wrong ACPX record and leave the actual record resumable after restart.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (logs): The PR body contains after-fix three-process production-manager, ACPX-runtime, file-store, and external-ACP-agent traces; it proves the equality case, while the distinct-record-ID defect remains a separate correctness blocker.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body contains after-fix three-process production-manager, ACPX-runtime, file-store, and external-ACP-agent traces; it proves the equality case, while the distinct-record-ID defect remains a separate correctness blocker.

Evidence

What I checked:

Likely related people:

  • vincentkoc: The current MEMBER comment identifies the exact-record defect and the ACPX owner API to consume. (role: reviewer and upstream repair owner; confidence: high; commits: c8427919b91b, 300b09b33ff8; files: extensions/acpx/src/runtime.ts)
  • Peter Steinberger: Recent merged ACPX runtime and complete-runtime-contract changes cover this adapter and its control-plane seam. (role: recent area contributor; confidence: high; commits: 5ce012619463, 465043ca859d; files: extensions/acpx/src/runtime.ts, src/acp/control-plane/manager.close-session.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Consume the published ACPX handle-based preparation API and remove the local record mutation.
  • Add a three-process restart regression where sessionKey and acpxRecordId differ.

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.

@MoerAI

MoerAI commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — confirmed. I’ll keep #130601 open and will not extend the local ACPX-private persistence path further.

After openclaw/acpx#522 is reviewed, merged, and published, I’ll update this branch to pass the exact runtime handle, remove the local marker synthesis, and add three-process restart proof where sessionKey and acpxRecordId differ.

@MoerAI

MoerAI commented Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

Closing this OpenClaw-side implementation in favor of the persistence-owner
repair in openclaw/acpx#522, per the maintainer review.

This branch synthesizes ACPX-private reset state and addresses records by the
OpenClaw session key, which can target the wrong generated record.
openclaw/acpx#522 instead retires the exact acpxRecordId, waits for active
turn finalization, and propagates durable owner/store failures.

The linked OpenClaw issue should remain open until the owner repair is
integrated and the original restart reproducer passes. A new OpenClaw patch
would only be warranted if that integrated owner path still fails the
current-main reproduction.

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

Labels

extensions: acpx merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: /acp close does not clear acpx FileSessionStore — stale acp_session_id causes unwanted session/resume instead of session/new

2 participants