Skip to content

fix(context-engine): isolate corrupt outbox recovery rows - #180772

Closed
xiaopan-xydt wants to merge 3 commits into
openclaw:mainfrom
xiaopan-xydt:fix/context-outbox-corrupt-row-180193
Closed

xiaopan-xydt wants to merge 3 commits into
openclaw:mainfrom
xiaopan-xydt:fix/context-outbox-corrupt-row-180193

Conversation

@xiaopan-xydt

@xiaopan-xydt xiaopan-xydt commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #180193

What Problem This Solves

One undecodable context-engine outbox row could block every later turn in the same session. Recovery now terminates only the JSON-corrupt row, excludes it from pending selection, and continues with the next ready turn. Valid JSON with a missing or unknown state deliberately remains pending, preserving the compatibility contract established by #126593.

  • Problem: recovery called JSON.parse() on each persisted payload. A truncated row threw before later rows were inspected, and even a caught decode failure would leave payload_state = 'admitted', so pending selection returned the same unreadable row on every run.
  • Solution: distinguish a JSON syntax failure from decoded-but-unsupported state. Only the syntax failure becomes payload_state = 'corrupt' with a durable error reason; decoded rows without a recognized state remain pending and inspectable.
  • What changed: recovery and pending selection agree on the terminal corrupt state, while missing/unknown states retain their old retry eligibility. The regression covers recovery, pending selection, pre-run drain, commit of a later ready turn, lease status, and preserved corrupt bytes.
  • What did NOT change: valid accepted and ready rows keep FIFO behavior; blocked rows remain terminal; database, lease, and cancellation errors still propagate.

Why This Change Was Made

The malformed bytes invalidate one queue entry, not the remaining session history. Catching the parse error alone was insufficient because the untouched row still won the next pending-selection query, so the later turn was never committed and pre-run preparation still degraded the lease.

  • Best fix: make only the JSON-corrupt state terminal in the existing shared pending-selection owner while retaining the original payload for operators.
  • Alternative: delete the row or skip it only inside recovery. Deleting discards diagnostic evidence; an in-memory skip fixes one pass but repeats the blockage after restart.
  • Risk / non-goals: this does not reconstruct the damaged turn or change the outbox schema. Missing and unknown states were explicitly preserved after review because older rows intentionally retain retry eligibility.

User Impact

A damaged queue payload no longer turns into a permanent same-session outage. The affected turn is terminally reported, later healthy turns can advance, and operators can still inspect the original bytes. Existing rows with incomplete state metadata keep their prior pending behavior.

Evidence

The regression runs the production recovery kernel against the real agent SQLite schema, then exercises pending selection and the pre-run drain.

$ OPENCLAW_VITEST_RUNTIME=node node scripts/run-vitest.mjs run \
    --config test/vitest/vitest.agents-support.config.ts \
    src/agents/harness/context-engine-turn-outbox.test.ts --maxWorkers=1
✓ 21 tests passed

Included cases:

✓ terminalizes a corrupt row and continues a later ready turn before the next run
✓ keeps a decoded missing payload pending during recovery
✓ keeps a decoded unknown payload pending during recovery

Base-red against the pre-change kernel with the new regressions:

FAIL terminalizes a corrupt row and continues a later ready turn before the next run
SyntaxError: Expected double-quoted property name in JSON at position 20
FAIL keeps a decoded missing payload pending during recovery
FAIL keeps a decoded unknown payload pending during recovery
TypeError: Cannot destructure property 'admission' of 'boundary' as it is undefined.
Test Files  1 failed (1)
Tests  3 failed | 18 skipped (21)

Formatting and whitespace:

$ pnpm exec oxfmt --check src/agents/harness/context-engine-turn-outbox.kernel.worker.ts src/agents/harness/context-engine-turn-outbox.test.ts
All matched files use the correct format.
$ git diff --check
<no output>

What was not tested: a live multi-session Gateway with a manually corrupted production database. The regression uses the shipped recovery/drain entry points and a real SQLite outbox, but runs inside the repository test harness. Full tsgo type checking is unavailable on this host because the downloaded Linux binary requires a newer glibc; CI will run the repository type checks.

AI Assistance

This PR is AI-assisted. Used rg, git diff, direct SQLite inspection, and focused/full Vitest runs. The implementer understands the code: only JSON decode failure is terminal, missing/unknown states remain pending, and existing FIFO and lease semantics remain in the shared owner.

@clawsweeper

clawsweeper Bot commented Oct 22, 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.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Oct 22, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. 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. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 22, 2026
@clawsweeper

clawsweeper Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

This PR retains undecodable outbox rows as terminal corrupt records so later healthy turns can advance.

Example: Session session-a contains truncated payload {"state":"admitted", before session-a:later-ready.

  • Before: Recovery throws on the truncated payload and repeatedly prevents later work from advancing.
  • After: Recovery preserves the damaged bytes, records a corrupt marker, and allows the later ready turn to commit.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused patch resolves prior findings and has useful integration coverage, but supplied tests do not satisfy the required real-behavior and persisted-state upgrade proof.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: One undecodable durable turn prevents later healthy turns in the same session from advancing through the configured context engine.
Reason: Containing an unusable row restores the existing terminal-marker recovery contract without changing healthy work, adding settings, or expanding the public API.

Merge readiness

⛔ Blocked before merge - 2 items remain

This PR addresses a current recovery bug and resolves both previous findings; no introduced correctness defect was found.

Priority: P2
Reviewed head: 3004e07dfb0ef8f8e54996e2987093ccd010e776

Before merge

  • Add real behavior proof - The supplied Vitest output exercises the real SQLite recovery kernel, pending selection, and pre-run drain with a mocked engine commit; attach redacted terminal output or logs from a running Gateway showing a configured durable engine advancing beyond malformed bytes and assembling subsequent context, removing IPs, API keys, phone numbers, and non-public endpoints. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.
  • Add data-model compatibility proof - The review found that existing stored data may not work after upgrade. Show that existing data still loads and works with this change.

Findings

None.

Tests

  • Missing end-to-end proof: Show the malformed-row scenario through a running Gateway and configured durable engine, including subsequent context assembly; the supplied output also does not demonstrate the regression failing on the base.
Agent review details

How this fits together

The context-engine outbox stores durable turn advancements in the agent database; pre-run recovery prepares pending work, and the drain submits ready turns to the selected engine before context assembly.

flowchart TD
  A[Next agent run] --> B[Worker recovery]
  B --> C[Agent database outbox]
  B --> D[Retained corrupt record]
  C --> E[Pending turn drain]
  E --> F[Context engine commit]
  F --> G[Context assembly]
Loading

Technical review

Best possible solution:

Keep malformed-row isolation in the existing recovery worker and shared pending predicate while preserving healthy FIFO work and diagnostic bytes.

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

Current main directly exposes the reported path: truncated payload_json throws during recovery, and the pre-run caller catches the failure and degrades the lease; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The patch repairs the existing recovery and selection owner without adding a competing queue or deleting damaged evidence, and the latest head preserves the established missing/unknown-state contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 062bbe5f9474.

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

PR surface

Source +53, Tests +91. Total +144 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 58 5 +53
Tests 1 91 0 +91
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 149 5 +144

Labels

Label changes:

No label changes.

Label justifications:

  • P2: The repair addresses persistent same-session context-engine degradation triggered by an already damaged outbox payload, with limited blast radius.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rank-up moves

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

  • Provide Gateway evidence of malformed-row isolation followed by a real durable-engine commit and subsequent context assembly.
  • Show an upgrade from v2026.10.6 preserving settings, valid pending work, and original damaged bytes, followed by successful recovery after reopening.

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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-10-22T16:38:18.284Z sha d31239d :: needs real behavior proof before merge. :: [P2] Exclude skipped corrupt rows from pending selection
  • reviewed 2026-10-22T17:07:59.883Z sha b09e6ac :: needs real behavior proof before merge. :: [P2] Preserve pending eligibility for missing and unknown states

Reviewed October 22, 2026, 2:03 PM ET / 18:03 UTC (Revision 3).

@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 22, 2026
@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 22, 2026
@xiaopan-xydt

Copy link
Copy Markdown
Contributor Author

Closing as not pursued. The remaining proof requires a running Gateway with a durable context engine plus persisted-state upgrade validation, which is not cost-effective for the current batch. The tests and review history remain available in the PR.

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

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S 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.

[Bug]: Corrupt outbox payload row permanently blocks recovery of later valid turn advancements

1 participant