Skip to content

fix: let large sessions compact again after 2026.11.10 - #205783

Merged
obviyus merged 2 commits into
openclaw:mainfrom
jredbeard:fix/compaction-prep-capacity-timeout
Nov 26, 2026
Merged

obviyus merged 2 commits into
openclaw:mainfrom
jredbeard:fix/compaction-prep-capacity-timeout

Conversation

@jredbeard

@jredbeard jredbeard commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Root cause: src/config/sessions/session-transcript-worker-readers.ts:366-372 (on main) charged compaction-preparation requests as JSON.stringify(input).length * 2 and passed no request options, so a manual plan carrying Gateway preflight facts was refused by the history pool's 256 MiB pending-bytes cap (packages/worker-runtime/src/worker-task-capacity.ts:2), and every preparation read fell back to the 60 s default in session-transcript-worker-runtime.ts:183.

Fixes #205752

Thanks @jredbeard for the report, the root-cause trace and this fix.

What Problem This Solves

Since 2026.11.10 (#201304), long-running sessions with a lot of history (the report: a Slack channel session at ~835k tokens, ~415 MB since the last compaction, ~1,460 images) can't be compacted. Manual openclaw sessions compact fails with worker task capacity reached [... pool pending bytes ... inputBytes=445943294], and budget compaction can fail with worker task timed out after 60 s on every turn on slower hosts.

Why This Change Was Made

  1. Manual path, refused for size. The Gateway preflight returns per-entry compaction facts for the whole active window and the plan request sends them back to the history worker (so planning does not extract the window twice). The reader charged that request as UTF-16 JSON: about 4.7x the structured clone that actually crosses the thread boundary, and it built a string as large as the window on the Gateway thread. readPreparedCompaction now charges the request by its v8.serialize byte length, the serializer the worker transport uses. The facts still travel and are still charged; the 256 MiB cap is unchanged and still refuses genuinely oversized requests.
  2. Preparation deadline. Preflight, plan, render and finalize reads used the 60 s default worker read deadline regardless of agents.defaults.compaction.timeoutSeconds. The Gateway runner and the embedded compaction owner now pass the existing resolveCompactionTimeoutMs(config) (default 180 s) to those reads, the way readTrajectoryRetention already takes a caller-owned timeoutMs. Caller abort signals still apply.

No config, schema or stored-data changes.

Evidence

Built Gateway, merge base vs this head, seeded session the size of the report (37,801 events, 413.7 MB transcript, 1,500 distinct base64 PNG tool results): #205783 (comment)

  • Manual: main refused twice (worker task capacity reached ... inputBytes=828669298, 7 s); this head compacted twice in 15 s (21,870,587 -> 20,708 tokens).
  • Preflight facts measured 174 MB as a structured clone vs 828 MB charged before. A plan with twice those facts (349 MB) is still refused with the same capacity error on this head; on main the JSON estimate throws RangeError: Invalid string length first.
  • Budget path: at the report's size preparation took ~8 s on this host on both main and this head, so the 60 s timeout did not reproduce here. The timeout change is covered by the integration regression in compact.summary-fallback.test.ts (red on main, green here).
  • Small session: unchanged on both (10,926 -> 1,140 tokens).
  • Regressions: session-transcript-worker-readers.test.ts (plan facts charged at least their clone and under the old estimate) and compact.summary-fallback.test.ts (timeoutSeconds: 600 reaches every preparation read), both red on main.

Production +18/-2, tests +103 against the merge base.

Merge danger

Two-way door: a revert restores the old charge and the 60 s deadline; nothing is written or migrated. Blast radius: compaction-preparation reads only (Gateway manual compaction and embedded compaction).

Compaction preparation runs in the session history worker. Two limits
stopped large, image-heavy sessions from compacting:

- Manual compaction hands the Gateway preflight facts back to the worker
  in the plan request. The request was charged as UTF-16 JSON
  (JSON.stringify(input).length * 2), several times the structured clone
  that actually crosses the thread boundary, so a window whose facts clone
  to ~110 MB was charged ~450 MB and refused outright by the pool's
  256 MiB pending-bytes cap. Charge the preflight source by its
  v8.serialize size instead; the rest of the request keeps the existing
  JSON estimate.
- Every preparation task (preflight, plan, render, finalize) used the
  default 60 s worker read deadline, so the budget path, which re-reads
  and decodes the whole window in one task, timed out on every turn.
  Thread the configured compaction timeout
  (agents.defaults.compaction.timeoutSeconds, via
  resolveCompactionTimeoutMs) into readPreparedCompaction.

Refs openclaw#205752
@clawsweeper

clawsweeper Bot commented Nov 26, 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 gateway Gateway runtime agents Agent runtime and tooling size: S labels Nov 26, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 26, 2026
- Charge every compaction-preparation request by its v8 structured-clone size instead of
  splitting plan facts from a UTF-16 JSON estimate of the rest.
- Narrow the reader regression to the admission charge; the compaction-timeout forwarding
  keeps its integration test.
@clawsweeper clawsweeper Bot added 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-repro Preliminary submission classification; full source review pending. labels Nov 26, 2026
@clawsweeper

clawsweeper Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

This PR changes compaction-facts request sizing and gives preparation reads the existing configured compaction timeout.

Example: A large session produces a 445,943,294-byte JSON estimate with compaction.timeoutSeconds set to 600.

  • Before: The manual plan exceeds the 268,435,456-byte admission cap, and preparation reads independently default to 60 seconds.
  • After: Preflight facts are charged by serialized size, and preparation reads receive a 600,000-millisecond timeout.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The implementation is focused and source-consistent, but required proof does not exercise the proposed fix and integration validation remains incomplete.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🦐 gold shrimp (3/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Large sessions cannot compact manually and repeatedly stall during automatic compaction.
Reason: Both failures remain in owned current-main code, and the focused repair restores an existing capability using the existing timeout setting.

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep open: this PR addresses two concrete compaction failures still present on current main, and no introduced correctness defect was found.

Reviewed head: ba7cb6ac2fa3860ba32dd47a615a08e50af80f68

Before merge

  • Add real behavior proof - The successful production run raised the pool cap and hardcoded a longer timeout, so it does not exercise this PR's serialized-size accounting or configured-timeout propagation; provide redacted terminal output or logs from both changed compaction paths, removing IPs, 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.

Findings

None.

Tests

  • Missing end-to-end proof: Show manual and automatic large-session compaction succeeding with this implementation and unchanged pool limits, including preparation exceeding 60 seconds; the added integration regression also lacks a completed base/head run.
Agent review details

How this fits together

Gateway manual compaction and embedded automatic compaction send transcript preparation requests through the session-history reader to a worker; prepared plans, rendered context, and finalization facts return to the existing compaction owner.

flowchart TD
  A[Manual compaction] --> C[Compaction preparation owner]
  B[Automatic compaction] --> C
  C --> D[Session history reader]
  D --> E[Worker admission and deadline]
  E --> F[Transcript preparation worker]
  F --> G[Summary and guarded persistence]
Loading

Technical review

Best possible solution:

Keep the compaction-specific sizing repair and propagate the existing timeout through preparation while preserving worker admission limits.

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

The reported failures are source-reproducible: manual preflight facts reach JSON-based admission sizing, while preparation inherits a separate 60-second deadline; this read-only review ran no target code.

Is this the best way to solve the issue?

The repair fits the existing owners and addresses both limits without raising shared capacity or introducing configuration; retaining the existing facts-transfer design keeps the fix bounded.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 92d40cbd9821.

Provenance checked

Testing

Proof path: unit tests only.

Security

None.

Evidence

What I checked:

PR surface

Source +34, Tests +109. Total +143 across 8 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 5 37 3 +34
Tests 3 111 2 +109
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 8 148 5 +143

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Root-cause cluster

Relationship: same_root_cause
Canonical: #205752
Summary: This PR implements the sizing and preparation-timeout repair requested by the linked open issue.

Members:

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

Labels

Label changes:

No label changes.

Label justifications:

  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • 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.

  • Capture successful manual and automatic compaction using this implementation with unchanged worker pool limits.
  • Complete the added integration regression and required exact-head CI.

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.

Reviewed November 26, 2026, 1:10 AM ET / 06:10 UTC.

@obviyus

obviyus commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Gateway proof on the exact head 6c77ffc4db vs merge base 83a950cd51, both built in a secretless node:24.22.1 container, scripted local model:

#205783 / #205752 proof (2026-11-26)
base = merge base 83a950cd51 (origin/main ancestor of the PR); cand = 6c77ffc4db (jredbeard ba7cb6ac2f + takeover commit)
Both built with `pnpm build` in the secretless node:24.22.1 container bugloop-205783 (16 CPUs, 48 GiB, no host env, no credentials).
Entry point: built dist Gateway (`gateway run`), `openclaw sessions compact <key> --agent main --json` (manual), `openclaw agent --session-key <key>` (budget turn).
Model: scripted OpenAI-compatible SSE server (mock-model.mjs), contextWindow 200k.

Seed "big" (seed.sh/seed.mts, real transcript batch append through a live base Gateway):
  37,801 transcript events, 413,698,278 transcript bytes, 1,500 distinct base64 PNG tool results (324,528,000 base64 bytes)
  reporter: ~38k events, ~415 MB, ~323 MB images
Seed "small": 120 events, 4 images.

Manual (sessions compact, big):
  base-manual-1  rc=1  7.1s  worker task capacity reached [pool pending bytes: pendingBytes=0 maxPendingBytes=268435456 inputBytes=828669298]
  base-manual-2  rc=1  7.2s  same error, inputBytes=828669298
  cand-manual-1  rc=0 15.0s  compacted 21,870,587 -> 20,708 tokens
  cand-manual-2  rc=0 15.0s  compacted 21,870,587 -> 20,708 tokens

Budget (agent turn over budget, big, default compaction timeout, full CPU):
  base-budget-1  rc=0 14.0s  auto-compaction succeeded, compaction 8.3s
  base-budget-2  rc=0 13.4s  auto-compaction succeeded, compaction 8.1s
  cand-budget-1  rc=0 14.3s  auto-compaction succeeded, compaction 8.8s
  cand-budget-2  rc=0 13.6s  auto-compaction succeeded, compaction 8.2s
  => the reporter's 60 s budget-path timeout did NOT reproduce on this host at reporter size (preparation ~8 s; reporter's
     host took 27 s for preflight alone). Larger natural sessions (2x, 4x) hit ERR_WORKER_OUT_OF_MEMORY in preflight on
     both trees before any timeout, a separate limit. The timeout change is covered by the regression in
     compact.summary-fallback.test.ts (red on base: preflight/plan/finalize reads get timeoutMs undefined; green on cand: 600000).

Controls:
  base-small-manual rc=0 2.0s compacted 10,926 -> 1,140 ; cand-small-manual rc=0 2.0s compacted 10,926 -> 1,140 (unchanged)
  Facts and over-cap (measure.mts, offline real history pool, big seed):
    preflight facts: structured clone 174,314,817 B; old UTF-16 JSON charge 828,403,514 B; cap 268,435,456 B
    plan carrying 2x distinct facts (349,055,778 B clone):
      base: RangeError: Invalid string length (the JSON estimate itself throws on the calling thread)
      cand: WorkerTaskError: worker task capacity reached [pool pending bytes: pendingBytes=0 maxPendingBytes=268435456 inputBytes=349057227]
  Explicit compaction.timeoutSeconds: covered by the integration regression (timeoutSeconds 600 -> every preparation read gets 600000 ms).

Tests (container, cand): session-transcript-worker-readers.test.ts 21/21 pass; compact.summary-fallback.test.ts 17/19 pass,
  the 2 failures ("uses the fallback model's runtime only for unlocked native sessions") fail identically on base in this container.
  Red on base: "charges planning preflight facts by their worker clone" (932500 not < 466250);
  "gives every compaction preparation read the configured compaction timeout" ([preflight, undefined] ...).
oxfmt --check, oxlint --type-aware (7 files): clean. tsgo core and src test shards: pass.

Discarded: earlier runs on a 16-image seed and CPU-throttled budget runs (discarded-throttle/), not evidence.

@obviyus

obviyus commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Maintainer override: the only red is build-artifacts (run 45772411642, job 139751299979): packages/gateway-protocol/src/compiled/protocol.generated/predicates.js is stale; run pnpm gateway:validators:gen. It comes from main, not this PR: running scripts/generate-gateway-validators.mts --check on the CI merge's main parent 7ce6c0299e98 alone throws the same error at the same line, and main's own run on that commit skipped build-artifacts. This PR touches nothing under packages/gateway-protocol. The two openclaw/ci-gate failures follow from that job; every other check passes (75 pass, 0 cancelled).

@obviyus
obviyus merged commit ec31b90 into openclaw:main Nov 26, 2026
136 of 140 checks passed
@obviyus

obviyus commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Merged in ec31b90. Thanks @jredbeard for the report in #205752 and the fix. Manual compaction of very large, image-heavy sessions works again: the worker request is now charged by its real serialized size, so it no longer hits the 256 MiB limit just because of its JSON estimate. Compaction preparation now also uses agents.defaults.compaction.timeoutSeconds (default 180 s) instead of a fixed 60 s. The worker limit itself is unchanged, and no stored data changed.

tideclaw-bot pushed a commit that referenced this pull request Nov 26, 2026
Root cause: `src/config/sessions/session-transcript-worker-readers.ts:366-372` (on main) charged compaction-preparation requests as `JSON.stringify(input).length * 2` and passed no request options, so a manual plan carrying Gateway preflight facts was refused by the history pool's 256 MiB pending-bytes cap (`packages/worker-runtime/src/worker-task-capacity.ts:2`), and every preparation read fell back to the 60 s default in `session-transcript-worker-runtime.ts:183`.

Fixes #205752

Thanks @jredbeard for the report, the root-cause trace and this fix.

## What Problem This Solves

Since 2026.11.10 (#201304), long-running sessions with a lot of history (the report: a Slack channel session at ~835k tokens, ~415 MB since the last compaction, ~1,460 images) can't be compacted. Manual `openclaw sessions compact` fails with `worker task capacity reached [... pool pending bytes ... inputBytes=445943294]`, and budget compaction can fail with `worker task timed out` after 60 s on every turn on slower hosts.

## Why This Change Was Made

1. **Manual path, refused for size.** The Gateway preflight returns per-entry compaction facts for the whole active window and the plan request sends them back to the history worker (so planning does not extract the window twice). The reader charged that request as UTF-16 JSON: about 4.7x the structured clone that actually crosses the thread boundary, and it built a string as large as the window on the Gateway thread. `readPreparedCompaction` now charges the request by its `v8.serialize` byte length, the serializer the worker transport uses. The facts still travel and are still charged; the 256 MiB cap is unchanged and still refuses genuinely oversized requests.
2. **Preparation deadline.** Preflight, plan, render and finalize reads used the 60 s default worker read deadline regardless of `agents.defaults.compaction.timeoutSeconds`. The Gateway runner and the embedded compaction owner now pass the existing `resolveCompactionTimeoutMs(config)` (default 180 s) to those reads, the way `readTrajectoryRetention` already takes a caller-owned `timeoutMs`. Caller abort signals still apply.

No config, schema or stored-data changes.

## Evidence

Built Gateway, merge base vs this head, seeded session the size of the report (37,801 events, 413.7 MB transcript, 1,500 distinct base64 PNG tool results): #205783 (comment)

- Manual: main refused twice (`worker task capacity reached ... inputBytes=828669298`, 7 s); this head compacted twice in 15 s (21,870,587 -> 20,708 tokens).
- Preflight facts measured 174 MB as a structured clone vs 828 MB charged before. A plan with twice those facts (349 MB) is still refused with the same capacity error on this head; on main the JSON estimate throws `RangeError: Invalid string length` first.
- Budget path: at the report's size preparation took ~8 s on this host on both main and this head, so the 60 s timeout did not reproduce here. The timeout change is covered by the integration regression in `compact.summary-fallback.test.ts` (red on main, green here).
- Small session: unchanged on both (10,926 -> 1,140 tokens).
- Regressions: `session-transcript-worker-readers.test.ts` (plan facts charged at least their clone and under the old estimate) and `compact.summary-fallback.test.ts` (`timeoutSeconds: 600` reaches every preparation read), both red on main.

Production +18/-2, tests +103 against the merge base.

## Merge danger

Two-way door: a revert restores the old charge and the 60 s deadline; nothing is written or migrated. Blast radius: compaction-preparation reads only (Gateway manual compaction and embedded compaction).

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit ec31b90)

Release adaptation: Git merged the Gateway import hunk into the release layout cleanly; no production or test behavior was adapted.

Co-authored-by: vincentkoc <[email protected]>
Co-authored-by: RomneyDa <[email protected]>
@jredbeard

Copy link
Copy Markdown
Contributor Author

Awesome - thanks for taking it seriously and merging @obviyus! Glad to help

tideclaw-bot added a commit that referenced this pull request Nov 26, 2026
…one fail open (#205687)

Report private flush output as progress without forwarding content. Time out only the stalled flush, count the failure under the existing policy, and allow compaction and the reply to continue. Let compaction report its own deadline before the reply watchdog backstop.

Release adaptation: preserve the release session-store routing and queued follow-up refresh imports, and omit the unrelated harness artifact spelling refactor. The stall guard uses only the watchdog progress contract; document the reply behaviour. The large-transcript compaction worker fix (#205783) is already in the parent line and remains intact. Companion audit found no later same-path or PR/SHA follow-up on pinned main.

(cherry picked from commit 26f82df)

Co-authored-by: vincentkoc <[email protected]>
Co-authored-by: RomneyDa <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 27, 2026
Root cause: `src/config/sessions/session-transcript-worker-readers.ts:366-372` (on main) charged compaction-preparation requests as `JSON.stringify(input).length * 2` and passed no request options, so a manual plan carrying Gateway preflight facts was refused by the history pool's 256 MiB pending-bytes cap (`packages/worker-runtime/src/worker-task-capacity.ts:2`), and every preparation read fell back to the 60 s default in `session-transcript-worker-runtime.ts:183`.

Fixes openclaw#205752

Thanks @jredbeard for the report, the root-cause trace and this fix.

## What Problem This Solves

Since 2026.11.10 (openclaw#201304), long-running sessions with a lot of history (the report: a Slack channel session at ~835k tokens, ~415 MB since the last compaction, ~1,460 images) can't be compacted. Manual `openclaw sessions compact` fails with `worker task capacity reached [... pool pending bytes ... inputBytes=445943294]`, and budget compaction can fail with `worker task timed out` after 60 s on every turn on slower hosts.

## Why This Change Was Made

1. **Manual path, refused for size.** The Gateway preflight returns per-entry compaction facts for the whole active window and the plan request sends them back to the history worker (so planning does not extract the window twice). The reader charged that request as UTF-16 JSON: about 4.7x the structured clone that actually crosses the thread boundary, and it built a string as large as the window on the Gateway thread. `readPreparedCompaction` now charges the request by its `v8.serialize` byte length, the serializer the worker transport uses. The facts still travel and are still charged; the 256 MiB cap is unchanged and still refuses genuinely oversized requests.
2. **Preparation deadline.** Preflight, plan, render and finalize reads used the 60 s default worker read deadline regardless of `agents.defaults.compaction.timeoutSeconds`. The Gateway runner and the embedded compaction owner now pass the existing `resolveCompactionTimeoutMs(config)` (default 180 s) to those reads, the way `readTrajectoryRetention` already takes a caller-owned `timeoutMs`. Caller abort signals still apply.

No config, schema or stored-data changes.

## Evidence

Built Gateway, merge base vs this head, seeded session the size of the report (37,801 events, 413.7 MB transcript, 1,500 distinct base64 PNG tool results): openclaw#205783 (comment)

- Manual: main refused twice (`worker task capacity reached ... inputBytes=828669298`, 7 s); this head compacted twice in 15 s (21,870,587 -> 20,708 tokens).
- Preflight facts measured 174 MB as a structured clone vs 828 MB charged before. A plan with twice those facts (349 MB) is still refused with the same capacity error on this head; on main the JSON estimate throws `RangeError: Invalid string length` first.
- Budget path: at the report's size preparation took ~8 s on this host on both main and this head, so the 60 s timeout did not reproduce here. The timeout change is covered by the integration regression in `compact.summary-fallback.test.ts` (red on main, green here).
- Small session: unchanged on both (10,926 -> 1,140 tokens).
- Regressions: `session-transcript-worker-readers.test.ts` (plan facts charged at least their clone and under the old estimate) and `compact.summary-fallback.test.ts` (`timeoutSeconds: 600` reaches every preparation read), both red on main.

Production +18/-2, tests +103 against the merge base.

## Merge danger

Two-way door: a revert restores the old charge and the 60 s deadline; nothing is written or migrated. Blast radius: compaction-preparation reads only (Gateway manual compaction and embedded compaction).

Co-authored-by: Ayaan Zaidi <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 28, 2026
Root cause: `src/config/sessions/session-transcript-worker-readers.ts:366-372` (on main) charged compaction-preparation requests as `JSON.stringify(input).length * 2` and passed no request options, so a manual plan carrying Gateway preflight facts was refused by the history pool's 256 MiB pending-bytes cap (`packages/worker-runtime/src/worker-task-capacity.ts:2`), and every preparation read fell back to the 60 s default in `session-transcript-worker-runtime.ts:183`.

Fixes #205752

Thanks @jredbeard for the report, the root-cause trace and this fix.

## What Problem This Solves

Since 2026.11.10 (#201304), long-running sessions with a lot of history (the report: a Slack channel session at ~835k tokens, ~415 MB since the last compaction, ~1,460 images) can't be compacted. Manual `openclaw sessions compact` fails with `worker task capacity reached [... pool pending bytes ... inputBytes=445943294]`, and budget compaction can fail with `worker task timed out` after 60 s on every turn on slower hosts.

## Why This Change Was Made

1. **Manual path, refused for size.** The Gateway preflight returns per-entry compaction facts for the whole active window and the plan request sends them back to the history worker (so planning does not extract the window twice). The reader charged that request as UTF-16 JSON: about 4.7x the structured clone that actually crosses the thread boundary, and it built a string as large as the window on the Gateway thread. `readPreparedCompaction` now charges the request by its `v8.serialize` byte length, the serializer the worker transport uses. The facts still travel and are still charged; the 256 MiB cap is unchanged and still refuses genuinely oversized requests.
2. **Preparation deadline.** Preflight, plan, render and finalize reads used the 60 s default worker read deadline regardless of `agents.defaults.compaction.timeoutSeconds`. The Gateway runner and the embedded compaction owner now pass the existing `resolveCompactionTimeoutMs(config)` (default 180 s) to those reads, the way `readTrajectoryRetention` already takes a caller-owned `timeoutMs`. Caller abort signals still apply.

No config, schema or stored-data changes.

## Evidence

Built Gateway, merge base vs this head, seeded session the size of the report (37,801 events, 413.7 MB transcript, 1,500 distinct base64 PNG tool results): #205783 (comment)

- Manual: main refused twice (`worker task capacity reached ... inputBytes=828669298`, 7 s); this head compacted twice in 15 s (21,870,587 -> 20,708 tokens).
- Preflight facts measured 174 MB as a structured clone vs 828 MB charged before. A plan with twice those facts (349 MB) is still refused with the same capacity error on this head; on main the JSON estimate throws `RangeError: Invalid string length` first.
- Budget path: at the report's size preparation took ~8 s on this host on both main and this head, so the 60 s timeout did not reproduce here. The timeout change is covered by the integration regression in `compact.summary-fallback.test.ts` (red on main, green here).
- Small session: unchanged on both (10,926 -> 1,140 tokens).
- Regressions: `session-transcript-worker-readers.test.ts` (plan facts charged at least their clone and under the old estimate) and `compact.summary-fallback.test.ts` (`timeoutSeconds: 600` reaches every preparation read), both red on main.

Production +18/-2, tests +103 against the merge base.

## Merge danger

Two-way door: a revert restores the old charge and the 60 s deadline; nothing is written or migrated. Blast radius: compaction-preparation reads only (Gateway manual compaction and embedded compaction).

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit ec31b90)
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 impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. 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: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

2 participants