Skip to content

fix(agents): stop silent compaction failures - #125302

Merged
steipete merged 3 commits into
mainfrom
steipete/compaction-outcome-contract
Aug 18, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/compaction-outcome-contract

Conversation

@steipete

@steipete steipete commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where automatic session compaction could fail without any recorded reason, causing sessions to keep growing until hard context overflow. Expired or missing model credentials were also collapsed into indistinguishable skips, and successful session-loop compaction never supplied the post-compaction token snapshot that session persistence expects.

Why This Change Was Made

Replace the parallel optional compaction-end fields with one closed completed/skipped/failed/aborted outcome owned by the session producer. Completed outcomes carry tokens before and after; skipped and failed outcomes carry actionable reasons; retry state exists only on completed outcomes. The embedded subscriber forwards the complete event, records the token snapshot, and projects failure reasons without changing the existing public completed/willRetry fields, so no Gateway protocol version or coordinated UI migration is required.

Automatic and manual compaction now capture the exact operation's abort controller, use that controller's signal to classify rejected work as aborted, and clear the shared controller only when it still owns that exact operation. This preserves cancellation through provider or extension rejection instead of misreporting it as failure.

Compaction summaries are now capped at 16,000 UTF-16 code units at the persistence boundary, using the same helper as the safeguard path. This is an intentional model-context bound: the summary is replayed on every subsequent request, so provider token limits alone are not a durable size guarantee. Memory-flush rotation now also requires completed === true instead of treating every compaction end as success.

Provenance: the reasonless automatic-auth/result shape was carried forward by #107948; the impossible tokensAfter read was introduced by f3e8a8a; the max-attempt guidance added in #123484 was produced but never forwarded.

User Impact

Operators now get a recorded reason when automatic compaction fails or cannot authenticate, cancelled automatic or manual compactions remain visible as benign aborts even when work rejects, overflow exhaustion is observable, and successful compaction immediately refreshes persisted context totals. Oversized summaries cannot silently consume unbounded context on every later turn.

Evidence

Final local proof on head 883a9981d9baa611622a459163343665d2c37dc0:

  • Rejected manual-cancellation regression: before the producer fix, src/agents/sessions/agent-session-compaction.test.ts failed because the observable compaction end was outcome: "failed" with reason: "Compaction failed: synthetic manual cancellation rejection"; after the fix the original promise still rejects with the synthetic error while the observable end is a reasonless outcome: "aborted", completed: false, willRetry: false, with no compaction entry/count/token snapshot.
  • Rejected automatic-cancellation coverage remains reasonless outcome: "aborted", completed: false, willRetry: false, with no compaction entry/count/token snapshot.
  • node scripts/run-vitest.mjs src/agents/sessions/agent-session-compaction.test.ts — 1 file, 11 tests passed.
  • node scripts/run-vitest.mjs src/agents/sessions/agent-session-loop-correctness.test.ts -t 'preserves cancellation' — 2 tests passed; 24 unrelated tests skipped by the filter.
  • node scripts/run-vitest.mjs src/agents/embedded-agent-subscribe.handlers.compaction.test.ts — 1 file, 13 tests passed.
  • node_modules/.bin/oxfmt src/agents/sessions/agent-session-compaction.ts src/agents/sessions/agent-session-compaction.test.ts — clean.
  • git diff --check — clean.
  • node scripts/check-changed.mjs --dry-run -- src/agents/sessions/agent-session-compaction.ts src/agents/sessions/agent-session-compaction.test.ts — selected only core and coreTests.
  • node scripts/check-changed.mjs -- src/agents/sessions/agent-session-compaction.ts src/agents/sessions/agent-session-compaction.test.ts — green, including core/core-test typecheck, lint, formatting, architecture, database, schema-version, dead-export, and import-cycle guards.
  • .claude/skills/autoreview/scripts/autoreview --mode uncommitted — clean one-pass review, TruffleHog clean, no accepted/actionable findings, overall correctness 0.99.
  • Exact-head pull_request CI — run 32080848415 green; openclaw/ci-gate green for 883a9981d9baa611622a459163343665d2c37dc0.
  • No separate Testbox lease was launched; the focused owner-boundary proof, exact local changed gate, and exact-head hosted CI are the current check path.

Changed LOC: production +204/-177 (net +27); tests/support +493/-179 (net +314); tooling +0/-1. The manual-abort follow-up itself is production +8/-4 (net +4) and test +58/-0. The tooling change is the required shrink-only deletion from config/assertion-safety-baseline.txt. There are no unrelated paths, changelog, release/version, generated API/protocol, lockfile, dependency, or other config changes.

Pre-fix evidence covered the missing automatic-auth reason, missing tokensAfter, an uncapped 30K summary, a dropped max-attempt reason, false memory-flush rotation on incomplete compaction, unclassified auth guidance, and rejected automatic and manual cancellation being projected as failure.

Live-proof gap: live-provider injection was not used because deterministically forcing provider 5xx, expired credentials, repeated hard overflow, rejected cancellation, and an oversized summary in one real account would require intentionally invalid credentials and costly context growth; owner-boundary regressions exercise those exact real AgentSession/subscriber/store paths.

AI-assisted: yes. The maintainer reviewed the complete diff and understands the ownership changes.

@clawsweeper

clawsweeper Bot commented Aug 17, 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 agents Agent runtime and tooling size: L maintainer Maintainer-authored PR labels Aug 17, 2026
@steipete
steipete marked this pull request as ready for review August 17, 2026 14:40
@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. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 17, 2026
@clawsweeper

clawsweeper Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 17, 2026, 7:47 PM ET / 23:47 UTC.

ClawSweeper review

What this changes

The PR replaces ambiguous session-compaction end fields with completed, skipped, failed, or aborted outcomes, then records token snapshots and failure reasons through session persistence and recovery paths.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep open for explicit maintainer review. The PR is still needed: current main retains the ambiguous compaction-end fields, while this branch establishes a closed producer-owned outcome and its subscriber projection; no blocking correctness finding was found.

Priority: P1
Reviewed head: 883a9981d9baa611622a459163343665d2c37dc0
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) The patch has a coherent owner-boundary design, focused regression coverage, and exact-head CI evidence, with only the protected maintainer merge decision remaining.
Proof confidence 🌊 off-meta tidepool Not applicable: The external-contributor proof gate does not apply to this maintainer-owned PR; its supplied exact-head evidence includes focused owner-boundary regressions and green CI.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The external-contributor proof gate does not apply to this maintainer-owned PR; its supplied exact-head evidence includes focused owner-boundary regressions and green CI.
Evidence reviewed 7 items Closed owner contract: The PR head makes completion, skip, failure, and abort mutually exclusive outcomes at the session producer, carrying token facts only for successful compaction.
Subscriber projection: The embedded-session subscriber increments counts and persists tokens only for completed outcomes, while preserving reasoned incomplete outcomes for logs and callbacks.
Current-main comparison: Current main still defines compaction completion through parallel result, aborted, willRetry, and optional errorMessage fields, so the PR is not already implemented.
Findings None None.
Security None None.

How this fits together

Agent sessions compact growing transcripts to avoid context overflow. The session producer emits a compaction result, and the embedded-session subscriber records it for persistence, retries, logs, and memory-flush rotation.

flowchart LR
  A[Agent transcript] --> B[Session compaction]
  B --> C[Closed compaction outcome]
  C --> D[Embedded session subscriber]
  D --> E[Session persistence]
  D --> F[Retry and memory-flush decisions]
Loading

Decision needed

Question Recommendation
Should the session owner approve this internal compaction-outcome contract after reviewing its current-main merge result? Approve after merge-result validation: Accept the closed outcome contract after the focused compaction and subscriber checks pass on the current-main merge result.

Why: The PR carries the protected maintainer label and changes session lifecycle and persistence behavior, so the final merge decision requires maintainer intent despite the clean review.

Before merge

  • Resolve merge risk (P1) - This changes the internal event contract consumed by session persistence and memory-flush rotation; the branch is behind current main, so the final merge result should receive the focused session checks before landing.
Agent review details

Security

None.

PR surface

Source +27, Tests +314, Other -1. Total +340 across 21 files.

View PR surface stats
Area Files Added Removed Net
Source 11 205 178 +27
Tests 9 492 178 +314
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 0 1 -1
Total 21 697 357 +340

Review metrics

Metric Value Why it matters
Production versus test delta production +204/-177 (net +27); tests/support +493/-179 (net +314); tooling -1 The production growth establishes the closed producer/subscriber outcome boundary; the larger test delta covers the affected lifecycle projections.

Stored data model

Persistent data-model change detected: serialized state: src/agents/sessions/agent-session-compaction.test.ts, serialized state: src/agents/sessions/agent-session-compaction.ts, serialized state: src/agents/sessions/agent-session-loop-correctness.test.ts, serialized state: src/agents/sessions/agent-session-types.ts, unknown-data-model-change: src/agents/sessions/agent-session-compaction.test.ts, unknown-data-model-change: src/agents/sessions/agent-session-compaction.ts, and 1 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Validate the current-main merge result (recommended)
    Rebase or validate GitHub’s clean merge result with the focused session-compaction and embedded-subscriber tests before merge.
  2. Accept the reviewed session-contract risk
    Merge with the existing exact-head proof if the maintainer accepts the remaining base-drift risk.

Technical review

Best possible solution:

Keep the producer-owned closed outcome contract, then validate the clean merge result against current main with the focused compaction and subscriber suites before landing.

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

Yes: the checked-in focused regressions exercise the producer-to-subscriber path for auth skips, rejected cancellation, successful token projection, and bounded persisted summaries; they were not executed in this read-only review.

Is this the best way to solve the issue?

Yes: moving outcome truth to the session producer and projecting it once in the subscriber removes the prior parallel optional fields rather than teaching downstream consumers to infer completion.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P1: Silent compaction failures can leave active agent sessions growing until context overflow.
  • merge-risk: 🚨 session-state: The PR changes compaction lifecycle facts consumed by session counters, persisted token snapshots, retries, and memory-flush rotation.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🌊 off-meta tidepool and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The external-contributor proof gate does not apply to this maintainer-owned PR; its supplied exact-head evidence includes focused owner-boundary regressions and green CI.

Evidence

What I checked:

Likely related people:

  • steipete: Authored the current compaction-outcome commits and the earlier merged session-compaction hardening work referenced by this PR. (role: feature owner and recent area contributor; confidence: high; commits: 65fca84d7c68, 883a9981d9ba; files: src/agents/sessions/agent-session-compaction.ts, src/agents/embedded-agent-subscribe.handlers.compaction.ts)

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 (7 earlier review cycles)
  • reviewed 2026-08-17T15:03:57.815Z sha 391fcb2 :: needs real behavior proof before merge. :: none
  • reviewed 2026-08-17T15:27:32.092Z sha 391fcb2 :: needs real behavior proof before merge. :: none
  • reviewed 2026-08-17T16:19:56.188Z sha 391fcb2 :: needs changes before merge. :: [P2] Classify cancelled automatic compactions as aborted
  • reviewed 2026-08-17T16:54:34.657Z sha 391fcb2 :: found issues before merge. :: [P2] Preserve abort outcomes in the automatic catch
  • reviewed 2026-08-17T21:59:42.819Z sha c08d8da :: needs changes before merge. :: [P2] Preserve manual abort outcomes after rejected compaction
  • reviewed 2026-08-17T22:42:14.348Z sha c08d8da :: needs changes before merge. :: [P2] Preserve manual abort outcomes after rejected compaction
  • reviewed 2026-08-17T23:39:41.534Z sha 883a998 :: needs maintainer review before merge. :: none

@steipete
steipete marked this pull request as draft August 17, 2026 15:47
@steipete
steipete marked this pull request as ready for review August 17, 2026 15:47
@clawsweeper clawsweeper Bot added status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 17, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Addressed the remaining ClawSweeper manual-cancellation finding at exact head 883a9981d9baa611622a459163343665d2c37dc0.

Manual compaction now classifies an ordinary rejection as aborted when its captured controller is signalled and clears the shared controller only while it still owns it. The new real-session producer-to-subscriber regression fails pre-fix as failed with a reason and passes as reasonless aborted, while the original promise still rejects with the synthetic error and no compaction count/token snapshot/entry is recorded.

Focused proof: compaction 11/11, cancellation loop 2/2, subscriber handler 13/13, exact changed gate green, and fresh uncommitted autoreview clean. Exact-head CI is being refreshed now.

@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed 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 17, 2026
@steipete
steipete merged commit 3cc5558 into main Aug 18, 2026
232 checks passed
@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

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: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: XL 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