Skip to content

fix(cli): treat post-turn compaction failures as non-fatal (fixes #94688) - #94698

Closed
zenglingbiao wants to merge 3 commits into
openclaw:mainfrom
zenglingbiao:fix/issue-94688-cli-compaction-nonfatal
Closed

zenglingbiao wants to merge 3 commits into
openclaw:mainfrom
zenglingbiao:fix/issue-94688-cli-compaction-nonfatal

Conversation

@zenglingbiao

@zenglingbiao zenglingbiao commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: In Codex/OpenAI fixed sessions, post-turn CLI compaction can fail with a Connection error after the assistant reply has already been generated and written to the session transcript. The runCliTurnCompactionLifecycle function throws on compaction failure, which causes the entire turn to be treated as a failure — discarding the already-generated reply.
  • Root Cause: runCliTurnCompactionLifecycle in src/agents/command/cli-compaction.ts throws on both native harness compaction failure (line 623) and context engine compaction failure (line 666). Compaction runs AFTER model generation, so the assistant reply already exists when the throw occurs.
  • Fix: Replace the two throw new Error(...) calls with log.warn(...) + return params.sessionEntry. The warning is logged for operators, but the turn proceeds and delivers the already-generated reply.
  • What changed:
    • src/agents/command/cli-compaction.ts — two throw sites converted to warn+return
  • What did NOT change (scope boundary):
    • Embedded agent runner compaction — already handles this correctly via promptErrorSource = "compaction"
    • Compaction retry/failover logic (native→context-engine fallback preserved)
    • The logged warning preserves observability for operators

Reproduction

  1. Run a Codex fixed session with a model (e.g. openai/gpt-5.4)
  2. Have the model generate a reply successfully
  3. Trigger a post-turn CLI compaction failure (e.g. connection error)
  4. Before this PR: CLI transcript compaction failed error is thrown → turn fails, reply discarded
  5. After this PR: Compaction failure is logged as a warning → turn succeeds, reply is delivered

Real behavior proof

Behavior or issue addressed (94688): Post-turn CLI compaction failures no longer abort the turn after the assistant reply was generated. The two throw sites (native harness line 623, context engine line 666) now return the session entry with a warning log instead of throwing.

Real environment tested: Linux, Node v22.22.0, OpenClaw 2026.6.8 built from source at upstream/main commit 7207072. A standalone node --import tsx script drives the real runCliTurnCompactionLifecycle function against a real SessionManager, real JSONL session file, and real JSON store — the same dependencies used in production. The script injects a hung context-engine compaction to trigger the exact failure path described in the issue.

Exact steps or command run after this patch: ran the proof script twice — once on upstream/main (before fix) and once on the PR branch (after fix) — each creating a real temporary session file and store, then calling runCliTurnCompactionLifecycle with timeoutSeconds=1 to force a compaction timeout.

Evidence before fix:

$ git checkout upstream/main && node --import tsx proof.mjs

===== PROOF: Post-turn CLI compaction failure is non-fatal =====
Session file: /tmp/openclaw-proof-94688-KzN8Kp/session.jsonl
Store path:   /tmp/openclaw-proof-94688-KzN8Kp/sessions.json
Config:       timeoutSeconds=1

[agents/cli-compaction] CLI transcript compaction failed for claude-cli/opus: Compaction timed out
RESULT: FAIL — compaction timeout caused throw:
   CLI transcript compaction failed for claude-cli/opus: Compaction timed out
  The already-generated assistant reply is DISCARDED.
  compactCalls.length = 1
exit: 1

Evidence after fix:

$ git checkout fix/issue-94688-cli-compaction-nonfatal && node --import tsx proof.mjs

===== PROOF: Post-turn CLI compaction failure is non-fatal =====
Session file: /tmp/openclaw-proof-94688-ijl3gX/session.jsonl
Store path:   /tmp/openclaw-proof-94688-ijl3gX/sessions.json
Config:       timeoutSeconds=1

[agents/cli-compaction] CLI transcript compaction failed for claude-cli/opus: Compaction timed out
[agents/cli-compaction] CLI transcript compaction failed for claude-cli/opus: Compaction timed out (reply already generated; continuing without compaction)
RESULT: PASS — compaction timed out but session entry was returned
  The already-generated assistant reply is preserved.
  compactCalls.length = 1
  compactCalls[0]?.abortSignal?.aborted = true
exit: 0

Observed result after fix: Before the fix, runCliTurnCompactionLifecycle throws CLI transcript compaction failed — the turn fails and the already-generated reply is discarded. After the fix, the same compaction timeout returns the session entry with a warning log — the turn succeeds and the reply is preserved. The proof script exercises the real production function against real file-based dependencies (SessionManager, JSONL transcript, JSON store), not mocked tests.

What was not tested: Live Codex app-server connection failure was not driven — this requires a running Codex instance with model credentials. The proof script exercises the exact same code path (context-engine compaction timeout) against real file dependencies.

Repro confirmation: The proof script at openclaw-issue-94688-evidence/proof.mjs is checked into the evidence directory. Run git checkout upstream/main && node --import tsx proof.mjs to see FAIL (throws, exit 1). Run git checkout <PR-branch> && node --import tsx proof.mjs to see PASS (returns session entry, exit 0). The colocated regression tests in src/agents/command/cli-compaction.test.ts additionally cover the same paths.

Risk / Mitigation

  • Risk: Skipping compaction when it fails means the session context is not reduced, potentially causing context overflow on the next turn.
    Mitigation: The next turn will attempt compaction again. The embedded agent runner independently handles overflow-triggered compaction. The warning log preserves visibility for operators.
  • Risk: The native harness fallback path (fallbackToContextEngine) is unchanged — only terminal failures are made non-fatal. Recoverable failures still proceed through the fallback path.

Change Type (select all)

  • Bug fix

Scope (select all touched areas)

  • agents
  • cli

Regression Test Plan

  • node --import tsx proof.mjs (standalone behavioral proof — exercises real function against real files)
  • node scripts/run-vitest.mjs src/agents/command/cli-compaction.test.ts (38 tests including updated throw→return assertions)
  • pnpm build && node openclaw.mjs --version (build verification)

Review Findings Addressed

N/A (initial submission with updated standalone-script proof)

Linked Issue/PR

Fixes #94688

…nclaw#94688)

The assistant reply is already generated and written to the session
transcript before post-turn CLI compaction runs. Treating compaction
failures as fatal would discard the already-generated reply. Instead,
log a warning and continue so the reply is delivered.
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S proof: supplied External PR includes structured after-fix real behavior proof. labels Jun 18, 2026
@clawsweeper

clawsweeper Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

Close as superseded: this PR addresses the right P1 bug, but it changes the generic compaction helper without knowing whether the completed turn has deliverable content, while the open member-authored lifecycle fix in #94928 is a cleaner canonical landing path.

Root-cause cluster
Relationship: superseded
Canonical: #94928
Summary: Several open PRs target the same post-turn compaction failure; the member-authored caller-level PR is the strongest canonical landing path because it preserves reply delivery only when content is deliverable.

Members:

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

Canonical path: Close this source PR and review #94928 as the canonical lifecycle-owner fix, then close sibling candidates after one fix lands.

So I’m closing this here and keeping the remaining discussion on #94928.

Review details

Best possible solution:

Close this source PR and review #94928 as the canonical lifecycle-owner fix, then close sibling candidates after one fix lands.

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

Yes. Source inspection shows current main persists the CLI transcript, awaits post-turn compaction, and reaches final delivery only afterward; forcing runCliTurnCompactionLifecycle to throw reproduces the skipped-delivery path.

Is this the best way to solve the issue?

No. This PR is a plausible symptom fix, but the best fix is the caller-level lifecycle guard in #94928 because it can check deliverable outbound content and keep no-reply failures fatal.

Security review:

Security review cleared: Security review cleared: the diff changes TypeScript compaction control flow and tests only, with no dependency, workflow, secret, permission, package, or supply-chain surface changed.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • lin-hongkuan: Current-line blame for both the helper throw sites and caller ordering maps to recent main work on the affected agent runtime files. (role: recent area contributor; confidence: high; commits: 67118d5ab912; files: src/agents/command/cli-compaction.ts, src/agents/agent-command.ts)
  • joshavant: GitHub commit metadata shows this person as committer for the recent main commit that currently owns the affected lines. (role: recent committer; confidence: medium; commits: 67118d5ab912; files: src/agents/command/cli-compaction.ts, src/agents/agent-command.ts)
  • obviyus: History search ties CLI transcript persistence, the durable-reply side of this failure path, to this contributor. (role: introduced related durable transcript behavior; confidence: medium; commits: b8ef507cc082; files: src/agents/agent-command.ts, src/agents/command/attempt-execution.ts)
  • openperf: The open member-authored canonical PR implements the reply-gated caller-level fix for the same linked issue. (role: canonical candidate fix author; confidence: medium; commits: e85004a14428; files: src/agents/agent-command.ts, src/agents/agent-command.compaction-rotation.test.ts)

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

@clawsweeper clawsweeper Bot added 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. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jun 18, 2026
@zenglingbiao

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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. and removed 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 Jun 19, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jun 19, 2026
@clawsweeper clawsweeper Bot mentioned this pull request Jun 20, 2026
2 tasks done
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jun 21, 2026
@clawsweeper clawsweeper Bot added status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Jun 21, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 22, 2026
@clawsweeper clawsweeper Bot closed this Jun 28, 2026
@clawsweeper

clawsweeper Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

🦞✅
ClawSweeper autoclose is complete.

Reason: structured ClawSweeper close marker: close-required (sha=52b9bcde66f060dd948431c3215ad2f83ded07f3)

Closed:

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

Labels

agents Agent runtime and tooling merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. 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. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex/OpenAI fixed session can generate the reply successfully, then fail in post-turn compaction and surface the whole turn as UNAVAILABLE

1 participant