Skip to content

fix(sessions): preserve active turns during session compaction - #110389

Merged
steipete merged 1 commit into
mainfrom
claude/fix-compact-aborts-live-turn
Jul 18, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/fix-compact-aborts-live-turn

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

sessions compact on a session with an in-flight agent turn killed the live turn (status:"timeout", stopReason:"restart", timeoutPhase:"gateway_draining") — even when compaction then decided there was nothing to do (ok:false, "Nothing to compact (session too small)"). The user's live run was aborted for a compaction that never happened.

Why This Change Was Made

Found by the R5 QA campaign (2× live repro): the lifecycle-mutation prepare step drained queues and interrupted session work admissions (src/gateway/server-methods/sessions-compact.ts) before the no-op verdict was computed much later in src/agents/sessions/agent-session-compaction.ts.

User Impact

  • Definitive no-op compacts (missing session, too-small, already-compacted) are now decided by a canonical preflight before any queue clearing or admission interruption — they can never disturb a live run.
  • Real compaction against a session with active work now refuses with a clear INVALID_REQUEST error instead of silently killing the run — consistent with sessions cleanup preserving active sessions and archive refusing active mutation. No --force added.
  • Idle-session compaction is unchanged; lifecycle fencing still blocks new work during an actual compaction.

Evidence

  • Preflight: src/agents/sessions/manual-compaction-preflight.ts, wired at src/gateway/server-methods/sessions-compaction-runner.ts:54 and before queue clearing at sessions-compact.ts:198; active-work guard at sessions-compact.ts:212,:261; competing-admission detection at src/sessions/session-lifecycle-admission.ts:330.
  • Tests: gateway compaction 20/20, agent compaction 13/13 — covering no-op-with-active-run (run survives), real-compact-refusal, idle-path unchanged, maxLines trimming.
  • node scripts/check-changed.mjs green (types/lint/format/guards).
  • Autoreview (codex gpt-5.6-sol, xhigh): clean — "patch is correct (0.87), no actionable findings".

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime agents Agent runtime and tooling size: M maintainer Maintainer-authored PR labels Jul 18, 2026
@steipete steipete self-assigned this Jul 18, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7a0ad677a6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +212 to +221
blockedByActiveRun =
isCompetingSessionWorkAdmissionActive(storePath, lifecycleIdentities) ||
hasVisibleActiveSessionRun({
context,
requestedKey: key,
canonicalKey: target.canonicalKey,
sessionId,
agentId: requestedAgentId,
defaultAgentId: resolveDefaultAgentId(cfg),
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Include worker inferences in active-run guard

When a cloud-worker turn is active only in context.workerEnvironmentService (hasInferenceForSession(sessionId) true) and has not produced a visible/projected run context, this new guard leaves blockedByActiveRun false and the RPC proceeds to trim or compact the transcript. The removed interruptSessionRunIfActive() path explicitly checked asWorkerInferenceControl(...).hasInferenceForSession, and the worker inference manager keeps those active sessions in its own map, so this misses a real active-run source and can mutate the session while the worker is still streaming/appending.

Useful? React with 👍 / 👎.

@steipete
steipete force-pushed the claude/fix-compact-aborts-live-turn branch 3 times, most recently from 0a21937 to 927fc84 Compare July 18, 2026 04:04
@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 Jul 18, 2026
@clawsweeper

clawsweeper Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 18, 2026, 12:48 AM ET / 04:48 UTC.

Summary
The PR moves manual-compaction planning before lifecycle mutation and refuses compaction when session work is active, with gateway and agent-session coverage for no-op, active, and idle paths.

PR surface: Source +143, Tests +8. Total +151 across 7 files.

Reproducibility: yes. from source: a cloud-worker inference that has not yet created a visible/projected run context bypasses the new guard and can reach transcript mutation. The supplied material does not include an inspectable live after-fix reproduction.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: src/agents/sessions/agent-session-compaction.ts, serialized state: src/agents/sessions/manual-compaction-preflight.ts, serialized state: src/gateway/server-methods/sessions-compact.ts, serialized state: src/gateway/server-methods/sessions-compaction-runner.ts, serialized state: src/sessions/session-lifecycle-admission.ts, unknown-data-model-change: src/agents/sessions/agent-session-compaction.ts, and 2 more. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🦪 silver shellfish
Result: blocked until real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Include worker-only inference ownership in the active-work guard and add a focused regression test.
  • [P1] Add redacted live Gateway evidence showing a no-op compact preserves an active turn and a real compact refuses without interrupting it.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body supplies tests and a prior repro but no inspectable after-fix live Gateway output, recording, terminal artifact, or redacted runtime logs; add proof after fixing the worker-inference gap and redact private data. 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.

Risk before merge

  • [P1] Merging as written can still compact or trim a session whose only active work is a cloud-worker inference, risking transcript/session-state mutation while the turn is streaming.
  • [P1] Existing tests and CI claims do not substitute for redacted after-fix runtime proof that a no-op compact leaves a live turn intact and real compaction is rejected without interrupting it.

Maintainer options:

  1. Cover worker-only active turns (recommended)
    Extend the compaction guard to treat worker inference ownership as active session work and add a regression test before merge.
  2. Pause the behavior change
    Do not merge the new compaction refusal path until its active-work definition covers every runtime that can append to the transcript.

Next step before merge

  • [P1] A narrow mechanical repair is available: include worker-only inference ownership in the compaction active-work predicate and add regression coverage; contributor-supplied real behavior proof remains required after that repair.

Security
Cleared: The reviewed diff changes session-lifecycle behavior and tests without adding dependency, secret, permission, artifact-download, or supply-chain execution paths.

Review findings

  • [P1] Include worker inferences in the active-compaction guard — src/gateway/server-methods/sessions-compact.ts:212
Review details

Best possible solution:

Use one canonical active-work predicate that includes visible runs, lifecycle admissions, and worker inference ownership before any transcript mutation, add a worker-only regression test, then attach redacted live Gateway evidence for the no-op and refusal paths.

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

Yes, from source: a cloud-worker inference that has not yet created a visible/projected run context bypasses the new guard and can reach transcript mutation. The supplied material does not include an inspectable live after-fix reproduction.

Is this the best way to solve the issue?

No. Preflight-before-mutation is the right boundary, but the active-work check must also cover worker inference ownership; otherwise the proposed safety guarantee is incomplete.

Full review comments:

  • [P1] Include worker inferences in the active-compaction guard — src/gateway/server-methods/sessions-compact.ts:212
    A turn that exists only in the worker inference manager has no visible/projected run context, so this guard can allow trimming or compaction while the worker is still streaming and appending. Preserve the former worker-inference check, preferably through the same canonical active-work predicate, and cover that state in the gateway regression test.
    Confidence: 0.93

Overall correctness: patch is incorrect
Overall confidence: 0.9

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

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

Label justifications:

  • P1: The patch targets a real active-turn abort, but an uncovered worker-inference path can still mutate a live session.
  • merge-risk: 🚨 session-state: The changed guard governs whether transcript compaction may run while session work is active, and it currently omits a concrete active-work source.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦪 silver shellfish.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body supplies tests and a prior repro but no inspectable after-fix live Gateway output, recording, terminal artifact, or redacted runtime logs; add proof after fixing the worker-inference gap and redact private data. 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

PR surface:

Source +143, Tests +8. Total +151 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 6 199 56 +143
Tests 1 87 79 +8
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 286 135 +151

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs src/gateway/server.sessions.compaction.test.ts.
  • [P1] node scripts/run-vitest.mjs src/sessions/session-lifecycle-admission.test.ts.

What I checked:

  • Uncovered active-work source: The PR replaces the former interrupt path with visible-run and admission checks, but the supplied maintainer review identifies a remaining cloud-worker inference path: hasInferenceForSession(sessionId) can be true before any visible/projected run context exists, allowing compaction or trimming to proceed. (src/gateway/server-methods/sessions-compact.ts:212, 927fc84ba28c)
  • Concrete review evidence: A prior inline review on this PR explains that the removed active-run interruption path checked worker inference control, while the new guard does not; this is a session-mutation race rather than a test-only concern. (src/gateway/server-methods/sessions-compact.ts:212, 927fc84ba28c)
  • Proof gate remains unmet: The PR body reports focused tests, static checks, and a before-fix QA reproduction, but it contains no inspectable after-fix Gateway transcript, terminal output, recording, or redacted runtime log demonstrating both the no-op and active-work outcomes. (927fc84ba28c)

Likely related people:

  • steipete: Authored the current focused repair commit and is assigned to this PR; broader current-main ownership could not be established from the supplied read-only evidence. (role: recent session-compaction contributor; confidence: low; commits: 927fc84ba28c; files: src/gateway/server-methods/sessions-compact.ts, src/gateway/server-methods/sessions-compaction-runner.ts, src/sessions/session-lifecycle-admission.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.
Review history (1 earlier review cycle)
  • reviewed 2026-07-18T04:06:38.554Z sha 927fc84 :: needs real behavior proof before merge. :: none

@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 Jul 18, 2026
@steipete
steipete force-pushed the claude/fix-compact-aborts-live-turn branch from 927fc84 to 9723183 Compare July 18, 2026 05:37

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9723183683

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +38 to +40
return (
(!persistedRuntime || persistedRuntime === "openclaw") &&
(!contextEngine || contextEngine === "legacy")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor configured runtime policy before preflight

When an unlocked session relies on provider/model agentRuntime policy instead of a persisted entry.agentHarnessId, persistedRuntime is undefined, so this treats the session as legacy OpenClaw whenever the context engine is default/legacy. The caller can then return the OpenClaw preflight no-op (Nothing to compact/Already compacted) before compactEmbeddedAgentSession runs, bypassing the configured native/owner runtime that the compaction path resolves from policy. Please either resolve the same configured harness policy here or only run this preflight when the runtime is known to be OpenClaw.

Useful? React with 👍 / 👎.

@steipete
steipete merged commit 538a382 into main Jul 18, 2026
115 checks passed
@steipete
steipete deleted the claude/fix-compact-aborts-live-turn branch July 18, 2026 05:45
@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 gateway Gateway runtime 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: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M 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.

1 participant