Skip to content

fix(sessions): avoid first-turn refusals from retired Usage executors - #170572

Merged
steipete merged 1 commit into
mainfrom
fix/usage-cache-executor-lifetime
Oct 13, 2026
Merged

steipete merged 1 commit into
mainfrom
fix/usage-cache-executor-lifetime

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes a new session's first turn being refused with Agent database execution admission is closed when Usage writer preparation retains an executor that retires before consumption.

User Impact

An existing store's Usage refresh acquires its current executor at writer admission. Session replacement and the first turn can proceed against the original physical store. A replacement file, revoked refresh authority, or cancellation still refuses the write. No operator action or migration is required.

Why This Change Was Made

The cache retained an execution reference before awaited work. Capture of an existing-store executor now happens inside the existing writer reservation, after checking the refresh authority, with the originally observed physical identity pinned. Missing stores still retain their creation witness before yielding; existing writers are still prepared before the scan, preserving the cold scan/prune fix in #170308. Incognito and maintenance paths are unchanged. There are no retries or changes to timeouts, schema, durability, retention, FIFO ordering, or authority.

Source attribution: the early cache-writer capture originated in #163287 (4a66195396d4) and survived the process-local reservation change in #169974 and existing-store preparation in #170308. The native failure also reproduced on #170464's warmed parent, so reverting #170464 does not repair it. This is source attribution plus a current-main deterministic negative control, not a claimed historical first-red bisect.

Evidence

The new regression forces retirement before Usage acquisition, then verifies session replacement and independently reads the committed session and rollup. It fails on unpatched main f9af58c66f79 with the exact admission-closed error (333 ms case; 50.89 s wrapper). Companion cases retain refusal and unchanged file bytes after physical replacement or authority revocation.

  • pnpm test src/infra/session-cost-usage-cache.admission.test.ts src/infra/session-cost-usage-cache.worker.test.ts src/state/openclaw-agent-execution.idle.test.ts src/state/openclaw-agent-execution.close-wedge.test.ts src/gateway/server.sessions.create.auth-policy.test.ts --maxWorkers=1: 51/51 passed, 194.75 s wrapper. Includes the original Gateway initial-turn case and cancellation, pending-open, FIFO, missing-store creation, drainage, idle retention, and native-close recovery contracts. The three new cases took 1.178, 1.544, and 1.056 s.
  • Core production types and the owning infra test graph passed. Changed-file typed lint, boundary lint, formatting, and whitespace checks passed. The changed-check plan was inspected; broad checks are delegated to PR CI under the narrow-local proof policy.
  • Isolated P2 review: scoped-clean, no actionable findings.

CI seconds are not available at PR creation. The first implementation used a creation constraint for an existing file and failed three pending-open assertions; using the existing file constraint restored them without changing those tests. The new replacement control initially expected the wrong refusal text and now asserts the owner's exact existing-only identity error. No failing run is relabeled green.

@clawsweeper

clawsweeper Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T03:24:34.081593Z c81588f PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@clawsweeper

clawsweeper Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR acquires existing-store Usage executors at writer admission while preserving the originally observed file identity.

Example: Usage prepares the main agent's store, then its executor retires before acquisition.

  • Before: Acquisition fails with Agent database execution admission is closed.
  • After: Acquisition uses a current executor for the same file, allowing session created-after-retirement and a 7-token rollup to commit.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A useful, focused repair with meaningful native database regression coverage and no actionable findings; behavioral evidence remains harness-based.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor proof gate; supplied native-worker regressions exercise retirement recovery and refusal controls, but do not demonstrate the changed scenario through a shipped Gateway entry point.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A new session's first turn can be refused because background Usage preparation holds a retired database executor.
Reason: The maintainer-authored change restores a core session workflow through its existing owner without adding configuration or altering product defaults.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR addresses a distinct first-turn failure still present on fetched main, with no actionable correctness findings.

Priority: P1
Reviewed head: c81588fedd0b0609b2467e2f16e049cfc7cf3f76

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: No supplied shipped Gateway scenario forces retirement between Usage preparation and writer admission.
Agent review details

How this fits together

The Usage cache writer sits between background transcript aggregation and the shared agent-database execution owner; it accepts rollup or prune requests and commits derived cache data through the existing writer queue.

flowchart TD
  A[Usage refresh] --> B[Capture original file identity]
  B --> C[Existing writer queue]
  C --> D[Check refresh authority]
  D --> E[Acquire current executor]
  E --> F[Agent database worker]
  F --> G[Session SQLite store]
Loading

Technical review

Best possible solution:

Acquire the current executor inside the existing writer reservation while retaining original-file constraints, creation witnesses, and live authority checks.

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

The source and added deterministic retirement regression establish a concrete reproduction path; the author reports the exact baseline failure, but no target code was executed during this read-only review.

Is this the best way to solve the issue?

The patch repairs capture timing at the existing owner rather than retrying failed writes, and preserves foreground ordering, cold-store preparation, physical identity, cancellation, and revocation checks.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 2c7872bdad12.

Provenance checked

  • Existing-store executor capture changes intended behavior with a stated reason (4a66195: Move Usage cache writes off the Gateway thread while retaining acquisition and cleanup custody.)
  • Missing-store creation witness keeps the original intent (6d479f5: Retain the originally observed absence so first session creation can join the same owner.)
  • Preparation before Usage scanning keeps the original intent (e4f4631: Remove the cold scan/prune dependency cycle by preparing the existing writer before scanning.)
  • Process-local refresh exclusion keeps the original intent (23dee30: Let the Gateway own derived refresh exclusion without durable lock-discovery and cleanup SQL.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

PR surface

Source +18, Tests +76. Total +94 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 27 9 +18
Tests 1 76 0 +76
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 103 9 +94

Review metrics

Metric Value Why it matters
Production versus test growth Production +27/-9; tests +76/-0 Production growth pins physical identity while moving capture to admission; tests exercise committed results and two refusal controls without new production test seams.

Labels

Label changes:

  • add P1: The reported failure blocks an ordinary new session's first turn before useful agent execution.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P1: The reported failure blocks an ordinary new session's first turn before useful agent execution.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 12, 2026, 11:27 PM ET / October 13, 2026, 03:27 UTC.

@steipete
steipete merged commit 9cf79e2 into main Oct 13, 2026
111 checks passed
@steipete
steipete deleted the fix/usage-cache-executor-lifetime branch October 13, 2026 03:35
@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant