Skip to content

fix: prevent durable SQLite user turns from orphan cleanup - #117393

Merged
RomneyDa merged 1 commit into
mainfrom
fix/sqlite-session-flip-state
Aug 1, 2026
Merged

RomneyDa merged 1 commit into
mainfrom
fix/sqlite-session-flip-state

Conversation

@RomneyDa

@RomneyDa RomneyDa commented Aug 1, 2026

Copy link
Copy Markdown
Member

Related: #116894
Follow-up to #116924

What Problem This Solves

Fixes an intermittent failure where a SQLite-backed agent turn could remove its already-durable current user message as an orphan after restart or queue timing delayed the recorder's persistence lifecycle. The resulting prompt append then failed with Session transcript parent entry was not persisted.

This is separate from #116859; that PR's auth-fixture changes do not touch the session persistence path.

Why This Change Was Made

The SQLite row can be committed before the recorder's runtime-persistence barrier settles. Session retry normalization already waits for that barrier, but first-attempt session-boundary reconciliation did not. The boundary could therefore read the durable keyed user leaf while hasPersisted() was not yet authoritative, remove it, clear suppression, and then collide with the durable idempotency row.

The fix awaits the recorder's existing persistence barrier immediately before orphan reconciliation. It keeps #116924's recorder-confirmed exact-key invariant and does not add retries, timeouts, config, compatibility paths, or schema changes.

User Impact

SQLite-backed sessions no longer intermittently fail an otherwise valid agent turn during restart, queued-turn, plugin-SDK consumer, or concurrent session lifecycle timing.

Evidence

  • Original GitHub failure: https://github.com/openclaw/openclaw/actions/runs/30685646626/job/91330835271
  • Untouched baseline, original proof order on Blacksmith: 2 failures / 36 runs (5.6%).
  • Two-CPU baseline reproduction: failed on iteration 2; failure occurred after the durable run-keyed user row existed and immediately after orphan removal.
  • Focused ordering regression: attempt-session-runtime-prepare.test.ts, 3/3 passed.
  • Runtime build: all 11 build invocations passed.
  • Fixed two-CPU original-order stress: 20/20 passed; warm proof time 38.6–39.0s.
  • AutoReview: clean, no actionable findings (0.99 confidence).
  • Diff: production +1 LOC; tests +21 LOC; no schema/config changes.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS maintainer Maintainer-authored PR labels Aug 1, 2026
@clawsweeper clawsweeper Bot added 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. P1 High-priority user-facing bug, regression, or broken workflow. labels Aug 1, 2026
@clawsweeper

clawsweeper Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 1, 2026, 9:11 AM ET / 13:11 UTC.

ClawSweeper review

What this changes

The PR waits for the user-turn recorder’s runtime-persistence barrier before embedded-agent session reconciliation can classify and remove orphaned transcript entries.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

The patch is a narrow, owner-aligned repair for a recorder/session timing race and source inspection supports its ordering. Keep this member-authored, maintainer-labeled PR open for normal review until its exact-head SQLite proof and remaining CI complete.

Priority: P1
Reviewed head: fabb87349daf08d31b51c66a142aeb28ec629b89

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A small owner-aligned repair with a deterministic regression test and reported real stress proof; merge readiness depends on the still-running exact-head gates.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: This is a member-authored PR, so the external-contributor proof gate does not apply; the body nevertheless reports real SQLite flip stress evidence, but GitHub artifact inspection was blocked by a network failure in this environment.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This is a member-authored PR, so the external-contributor proof gate does not apply; the body nevertheless reports real SQLite flip stress evidence, but GitHub artifact inspection was blocked by a network failure in this environment.
Evidence reviewed 6 items Recorder persistence contract: The recorder stores a runtime-persistence promise, waits for it while handling errors, and only reports persistence after runtime persistence is marked; it is the authoritative lifecycle boundary before orphan classification.
Boundary ownership: Session-boundary reconciliation reads userTurnTranscriptRecorder.hasPersisted() before deciding whether a durable user leaf is current or should be repaired as an orphan.
Proposed ordering repair: The PR waits for the recorder immediately before calling the boundary helper, placing the wait at the consumer that could otherwise invalidate a not-yet-authoritative current turn.
Findings None None.
Security None None.

How this fits together

Embedded agent setup prepares a restored session, consults the user-turn recorder and SQLite transcript state, then reconciles the boundary before constructing the prompt for the model. The recorder owns persistence completion, while boundary reconciliation decides whether a durable user turn is current or orphaned.

flowchart LR
  A[Incoming agent turn] --> B[User-turn recorder]
  B --> C[Runtime persistence barrier]
  B --> D[SQLite transcript row]
  C --> E[Session boundary reconciliation]
  D --> E
  E --> F[Prepared prompt session]
  F --> G[Model run]
Loading

Before merge

  • Resolve merge risk (P1) - The exact current head still has the targeted SQLite flip-proof, build, lint, and test-shard checks in progress; merge only after required gates complete successfully.
  • Complete next step (P2) - This member-authored, maintainer-labeled PR already owns the narrow fix; the remaining action is to review the finished exact-head proof and merge gates, not dispatch a competing repair.
Agent review details

Security

None.

PR surface

Source +1, Tests +21. Total +22 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 1 0 +1
Tests 1 21 0 +21
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 22 0 +22

Review metrics

None.

Root-cause cluster

Relationship: canonical
Canonical: #117393
Summary: This PR is the active canonical candidate for the remaining recorder-settlement timing window; the earlier merged repair covered a related exact-key classification condition.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Complete exact-head lifecycle proof (recommended)
    Wait for the SQLite flip-proof and remaining required checks on this head, then merge if they succeed because the patch preserves the recorder as the single persistence authority.
  2. Pause if the stress proof flakes
    If the targeted proof still fails or stalls, pause this PR and investigate the persistence-promise producer rather than adding retries or timeouts at the boundary consumer.

Technical review

Best possible solution:

Keep the recorder-owned barrier immediately before session-boundary reconciliation, retain the focused ordering regression, and merge only after the exact-head SQLite flip proof confirms the original restart/queue lifecycle no longer fails.

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

Yes—source establishes the race shape: the boundary consults hasPersisted() before orphan repair, while the recorder can still hold an unsettled runtime-persistence promise. The PR’s focused test deterministically holds that promise and verifies reconciliation waits.

Is this the best way to solve the issue?

Yes—the wait belongs immediately before the consumer that classifies or removes transcript entries, using the recorder’s existing authoritative barrier rather than adding a second persistence mechanism, retry, timeout, or config surface.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P1: The reported race can abort valid SQLite-backed agent turns during restart or queued-session recovery.
  • merge-risk: 🚨 session-state: The patch intentionally changes ordering at the boundary that reconciles persisted transcript state and can remove user turns.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a member-authored PR, so the external-contributor proof gate does not apply; the body nevertheless reports real SQLite flip stress evidence, but GitHub artifact inspection was blocked by a network failure in this environment.

Evidence

What I checked:

Likely related people:

  • shakkernerd: The supplied related history identifies this person as author of the merged SQLite persisted-user-turn reconciliation repair that established the exact-key boundary behavior this timing fix preserves. (role: introduced adjacent reconciliation invariant; confidence: medium; commits: 8fc43e187ae9; files: src/agents/embedded-agent-runner/run/attempt-session-boundary.ts)

Rank-up moves

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

  • Wait for check-sqlite-session-flip-proof, build, lint, and required test checks to finish successfully on fabb87349daf08d31b51c66a142aeb28ec629b89.

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 (1 earlier review cycle)
  • reviewed 2026-08-01T12:50:08.354Z sha 06b52a6 :: needs maintainer review before merge. :: none

@RomneyDa RomneyDa self-assigned this Aug 1, 2026
@RomneyDa
RomneyDa force-pushed the fix/sqlite-session-flip-state branch from 06b52a6 to fabb873 Compare August 1, 2026 13:06
@clawsweeper clawsweeper Bot added the merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. label Aug 1, 2026
@RomneyDa
RomneyDa merged commit bba7b93 into main Aug 1, 2026
123 checks passed
@RomneyDa
RomneyDa deleted the fix/sqlite-session-flip-state branch August 1, 2026 13:24
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR 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. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS 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