Skip to content

fix(gateway): skip abort partial persistence when the final reply is already committed - #132123

Merged
obviyus merged 8 commits into
openclaw:mainfrom
ruel225:fix/abort-transcript-idempotency-131711
Aug 29, 2026
Merged

obviyus merged 8 commits into
openclaw:mainfrom
ruel225:fix/abort-transcript-idempotency-131711

Conversation

@ruel225

@ruel225 ruel225 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Refs #131711.

A run can commit its final assistant reply while its live Gateway buffer still holds the same text. If a late abort is accepted before lifecycle cleanup clears that buffer, abort persistence writes the completed reply again as a gateway-injected abort partial.

Why This Change Was Made

Abort persistence now checks the newest committed assistant row after the SQLite write transaction begins. It skips the append only when both the stored run identity and normalized reply text match the aborted run.

The commit-edge check matters because embedded and worker terminal writers can write synchronously without the process-local writer queue. A queue-level check could read stale state, then append after a terminal row committed.

Text equality alone is not enough. A different run can produce the same text, and a legacy row can have no stored run identity. Both cases still preserve the real abort partial.

This is complementary to #131949. That PR closes one late-abort admission window at the reply-operation owner. This PR keeps the shared persistence boundary idempotent for accepted aborts from RPC, /stop, and placement abandonment.

User Impact

Operators keep one durable copy of a completed reply when a late abort arrives during delayed settlement. Genuine partial replies from another run still remain in history.

Evidence

Real isolated Gateway red/green proof

The same source-lane scenario used a loopback Responses provider, an isolated Gateway, a real SQLite transcript, and a context-engine gate that held post-turn settlement after the final reply committed. The test observed the reply on the Gateway chat stream, confirmed the committed row carried the same run ID, called chat.abort, closed SQLite handles, and reopened the transcript.

  • Exact main 5d3bd15130a28e293b9bbeee61c84dd0f6c162bf: failed with two matching assistant rows after abort.
  • Exact head d96e279fec13ea0e954eca1641ee91ea1a90f969: one matching durable row before abort, one after abort, zero abort-marked duplicates, and one row after reopening SQLite.
{"runId":"pr132123-proof-run","replyVisible":true,"waitBeforeAbort":"timeout","durableRowsBeforeAbort":1,"abort":{"ok":true,"aborted":true,"runIds":["pr132123-proof-run"]},"durableRowsAfterAbort":1,"abortMarkedRowsAfterAbort":0,"reopenedRows":1,"reopenedAbortMarkedRows":0}
OPENCLAW_VITEST_MAX_WORKERS=1 node scripts/run-vitest.mjs run \
  --config test/vitest/vitest.gateway-methods.config.ts \
  --configLoader runner \
  src/gateway/server-methods/chat.abort-live-proof.test.ts

Focused regression coverage

  • 219 tests passed across abort persistence, transaction interleaving, transcript identity, authorization, and session abort siblings.
  • The transaction regression failed on main with two rows and passed on head with one row.
  • Targeted Oxfmt and Oxlint passed.
  • Import-cycle, coercion-helper, database-first, assertion-safety, max-lines, conflict-marker, and test-temp-directory guards passed.

Judged line count: production +69/-2; tests +618/-0. The production growth adds one synchronous commit predicate and the durable run-identity reader. The repair commit removes the contributor branch's earlier queue-level reader and predicate plumbing, so the final path has one transaction-owned decision.

@clawsweeper

clawsweeper Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Aug 28, 2026
@ruel225
ruel225 marked this pull request as ready for review August 28, 2026 21:21
@clawsweeper clawsweeper Bot added merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. 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: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 28, 2026
@clawsweeper

clawsweeper Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed August 29, 2026, 12:39 AM ET / 04:39 UTC.

ClawSweeper review

What this changes

The branch skips a late abort-partial transcript append when the newest terminal assistant row has matching text and run identity.

Regression provenance

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

Merge readiness

⛔ Blocked until stronger real behavior proof is added - 6 items remain

Keep open: the queued predicate can still race the synchronous terminal-row writer, so the duplicate transcript entry remains possible. The prior run-identity fix is sound, but the supplied gateway proof exercises the different-run preservation case rather than the headline same-run deduplication.

Priority: P1
Reviewed head: c4bc2645f7b5289867e2601e9e963c099233c04d

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The PR has strong focused coverage and a useful Gateway trace, but a P1 direct-writer race remains and the live proof misses the central ordering.
Proof confidence 🦐 gold shrimp (3/6) Needs stronger real behavior proof before merge: The supplied terminal verdict comes from an isolated Gateway with real RPC, SQLite, and loopback SSE transport, but its aborted second run never commits a terminal row; it proves cross-run identical text is preserved, not that a committed row from that same stalled run suppresses the abort append. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Needs proof Needs stronger real behavior proof before merge: The supplied terminal verdict comes from an isolated Gateway with real RPC, SQLite, and loopback SSE transport, but its aborted second run never commits a terminal row; it proves cross-run identical text is preserved, not that a committed row from that same stalled run suppresses the abort append. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 5 items Introduced decision: The PR adds the matching-run/text predicate before the abort-partial append.
Race remains reachable: The predicate is awaited before the synchronous SQLite transaction begins, while the embedded terminal writer directly enters a synchronous transcript transaction without the writer queue; it can commit between the read and abort append.
Current main does not contain this repair: Current main still unconditionally appends each aborted partial at this boundary.
Findings 1 actionable finding [P1] Revalidate the predicate inside the abort write transaction
Security None None.

How this fits together

Gateway abort handling persists buffered assistant output into a session transcript. The change consults the durable transcript before recording an abort partial, affecting session history seen by later clients.

flowchart LR
A[Buffered assistant reply] --> B[Chat abort]
B --> C[Queued append predicate]
D[Embedded terminal writer] --> E[SQLite transcript]
C --> E
E --> F{Same run and text?}
F -->|Yes| G[Skip abort partial]
F -->|No| H[Append abort partial]
Loading

Before merge

  • Add real behavior proof - Needs stronger real behavior proof before merge: The supplied terminal verdict comes from an isolated Gateway with real RPC, SQLite, and loopback SSE transport, but its aborted second run never commits a terminal row; it proves cross-run identical text is preserved, not that a committed row from that same stalled run suppresses the abort append. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Revalidate the predicate inside the abort write transaction (P1) - The new predicate reads before appendExpectedSessionTranscriptTurn starts its SQLite transaction. Embedded terminal persistence reaches appendTranscriptMessageSync directly, bypassing that queue, so a matching terminal row can commit after this read and before the abort insert; the abort then still writes the duplicate. Recheck the durable row in the synchronous commit section or give both paths one serialization owner.
  • Resolve merge risk (P1) - A terminal assistant row can commit after the new predicate reads an older transcript state but before the abort transaction, leaving the durable duplicate unchanged.
  • Resolve merge risk (P1) - The claimed real-gateway result does not demonstrate the same-run committed-row skip path.
  • Resolve merge risk (P1) - Production code grows by 109 lines for a bug fix; the required transactional ownership boundary must be simplified or justified before merge.
  • Complete next step (P2) - A bounded P1 repair is clear: the abort decision must share an actual commit boundary with the synchronous terminal writer, followed by an interleaving regression.

Findings

  • [P1] Revalidate the predicate inside the abort write transaction — src/gateway/server-methods/chat-abort-runtime.ts:102-113
Agent review details

Security

None.

PR surface

Source +108, Tests +524. Total +632 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 4 109 1 +108
Tests 3 524 0 +524
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 633 1 +632

Review metrics

Metric Value Why it matters
Production vs test LOC production +109/-1; tests +524/-0 The bug fix adds a new persistence-decision seam and substantial regression/proof coverage; the production growth must resolve the existing direct-writer race.

Stored data model

Persistent data-model change detected: serialized state: src/sessions/transcript-events.ts, vector/embedding metadata: src/sessions/transcript-events.ts. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Revalidate at the write boundary (recommended)
    Move the durable-row check into the abort append’s synchronous commit decision or serialize the direct terminal writer through the same owner, then add an ordering regression.
  2. Pause this implementation
    Do not merge while a direct terminal commit can bypass the queued predicate and preserve the reported duplicate.

Technical review

Best possible solution:

Make the final committed-row check part of the synchronous SQLite transaction that decides the abort append, or place both writer paths under one canonical serialization owner, then prove the direct-terminal-write interleaving cannot append a duplicate.

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

Yes, from source: pause the new predicate after it reads an older row, synchronously append the matching terminal row through the embedded session writer, then allow the abort transaction to proceed; it will append the duplicate.

Is this the best way to solve the issue?

No. The run-scoped comparison fixes the prior text-only loss, but placing it before the transaction does not protect it from the actual synchronous terminal writer.

Full review comments:

  • [P1] Revalidate the predicate inside the abort write transaction — src/gateway/server-methods/chat-abort-runtime.ts:102-113
    The new predicate reads before appendExpectedSessionTranscriptTurn starts its SQLite transaction. Embedded terminal persistence reaches appendTranscriptMessageSync directly, bypassing that queue, so a matching terminal row can commit after this read and before the abort insert; the abort then still writes the duplicate. Recheck the durable row in the synchronous commit section or give both paths one serialization owner.
    Confidence: 0.97

Overall correctness: patch is incorrect
Overall confidence: 0.97

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🦐 gold shrimp, so this older rating label is no longer current.

Label justifications:

  • P1: A reachable late-abort ordering can still persist a second assistant record for an already committed reply.
  • merge-risk: 🚨 session-state: The patch changes whether abort paths suppress or append durable session transcript rows.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The supplied terminal verdict comes from an isolated Gateway with real RPC, SQLite, and loopback SSE transport, but its aborted second run never commits a terminal row; it proves cross-run identical text is preserved, not that a committed row from that same stalled run suppresses the abort append. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs src/gateway/server-methods/chat.abort-persistence.test.ts.
  • [P1] node scripts/run-vitest.mjs src/sessions/transcript-events.test.ts src/config/sessions/session-accessor.test.ts.
  • [P1] node scripts/run-vitest.mjs test/pr132123-abort-transcript-proof.e2e.test.ts.

What I checked:

Likely related people:

  • Peter Steinberger: Authored the available transcript writer-fence and synchronous SQLite lifecycle changes that define the conflicting write boundary. (role: recent session-persistence contributor; confidence: high; commits: 23d1eca4554a, 2de73ebac9e6, c9b346e216a6; files: src/config/sessions/session-accessor.sqlite-transcript-write.ts, src/config/sessions/session-accessor.sqlite-transcript-turn.ts)
  • shad0wca7: Authored the available recent change around preserving run-owned transcript source state. (role: recent run-ownership contributor; confidence: medium; commits: bc39f51a48bc; files: src/agents/session-tool-result-guard.ts, src/sessions/transcript-events.ts)

Rank-up moves

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

  • Rework the check so the direct terminal writer cannot commit between the observation and abort insertion.
  • Add a regression that forces the direct terminal write in that interval.
  • Post a redacted after-fix trace for the same-run committed-then-stalled abort ordering.

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 (4 earlier review cycles)
  • reviewed 2026-08-28T21:36:16.021Z sha a0bf4f2254654fe8e33bf8c73b3ad69c0614b1d5 :: needs real behavior proof before merge. :: [P1] Bind the committed-row check to the aborted run | [P1] Make the commit check and abort append one persistence decision
  • reviewed 2026-08-28T22:30:15.104Z sha 45465a4f7405ead38439ab4cebc07e15c89c3ff0 :: needs real behavior proof before merge. :: [P1] Preserve partials that repeat an earlier reply
  • reviewed 2026-08-28T23:33:32.210Z sha 5efa2fe1ed74be289da5f4b91d4020f416f8b347 :: needs real behavior proof before merge. :: [P1] Preserve partials that repeat an earlier reply
  • reviewed 2026-08-29T01:10:12.681Z sha 296c54795167d87728a1516656c171c349f8245c :: needs real behavior proof before merge. :: [P1] Scope the committed-row predicate to the aborted run

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels Aug 28, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 28, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Aug 29, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 29, 2026
ruel225 and others added 5 commits August 29, 2026 07:03
…already committed

A run that settles and commits its final assistant row keeps the same text
in its live buffer until the lifecycle end event clears it. When settle
stalls, the buffer survives and a late chat.abort re-persists the buffered
text as an injected abort partial, duplicating the committed reply in the
transcript.

Abort persistence now reads the last committed assistant row and skips the
append when it already carries the same text, covering the rpc, stop-command,
and placement-abandon origins at the single persistence boundary.

Co-Authored-By: Claude <[email protected]>
…ter queue

Reading the last committed assistant row outside the append left the skip
decision racing a final commit that could land between the read and the
append, and a declined decision surfaced as a failed placement abandonment
instead of a skipped write.

The transcript turn persistence already owns a writer-queue predicate that
runs before the SQLite transaction; thread the committed-row comparison
through it so the durable read and the append decision are atomic against
every other transcript writer, and report the decline as a skipped append
rather than an error.

Co-Authored-By: Claude <[email protected]>
Adds a real-gateway proof: a first turn commits the reply, a second run
buffers identical text while the provider stream stays open, then a
chat.abort lands. The observed after-fix gateway outcome is a single
committed assistant row, captured through the real client, gateway
process, and SQLite transcript store.

Co-Authored-By: Claude <[email protected]>
The abort decision reads the live run buffer, so the proof now waits for
the gateway broadcast carrying the reply text rather than a fixed delay.

Co-Authored-By: Claude <[email protected]>
ruel225 and others added 3 commits August 29, 2026 07:03
Promise.withResolvers needs an ES2024 lib; the test-root tsconfig targets
ES2023. test/helpers/promise.js already re-exports the canonical deferred.

Co-Authored-By: Claude <[email protected]>
The committed-row skip predicate compared text alone, so a distinct
earlier run that produced byte-identical reply text suppressed the new
run's genuine abort-marked partial. Read the terminal row's stored
__openclaw.runId alongside its text and skip only when both match the
aborted run; rows without an attached identity fail open so the abort
record survives.

Co-Authored-By: Claude <[email protected]>
@obviyus
obviyus force-pushed the fix/abort-transcript-idempotency-131711 branch from c4bc264 to d96e279 Compare August 29, 2026 05:23
@obviyus

obviyus commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks @ruel225!

Proof: On exact current-main baseline 5d3bd15130a, an isolated Gateway showed the final reply, committed one same-run SQLite row, then recorded a second abort-marked row after a late chat.abort. On exact head d96e279fec1, the same scenario kept one row before abort, one row after abort, zero abort-marked duplicates, and one row after closing and reopening SQLite.

  • Bug: A completed reply could remain in the live buffer and be persisted again when a late abort arrived during delayed settlement.
  • Fix: Abort persistence now checks the same run and reply inside the SQLite commit transaction, so synchronous terminal writers cannot race between the check and insert.

Example

Before After
History contains the completed reply and a duplicate abort-marked copy. History keeps one completed reply and no duplicate abort row.

@obviyus
obviyus merged commit dccdfed into openclaw:main Aug 29, 2026
261 checks passed
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 30, 2026
…2123)

Prevent accepted late aborts from duplicating same-run final replies when synchronous transcript writers bypass the process queue.

Co-authored-by: ruel225 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime 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: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: L status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants