Skip to content

fix(memory): prevent stale preflight compaction loops and assert OpenAI model list IDs#81231

Closed
oxtensor wants to merge 2 commits into
openclaw:mainfrom
oxtensor:fix/81178-preflight-compaction-stale-usage
Closed

fix(memory): prevent stale preflight compaction loops and assert OpenAI model list IDs#81231
oxtensor wants to merge 2 commits into
openclaw:mainfrom
oxtensor:fix/81178-preflight-compaction-stale-usage

Conversation

@oxtensor

@oxtensor oxtensor commented May 13, 2026

Copy link
Copy Markdown

Summary

Fixes #81178.

Replay estimation in agent-runner-memory.ts could reuse stale historical transcript token usage after earlier compaction events. When totalTokensFresh became missing or stale, large transcript usage tails could dominate replay estimation and repeatedly trigger unnecessary preflight compactions.

This change adds bounded stale-usage handling during replay estimation so stale transcript tails no longer inflate replay token estimates after compaction. The fix is intentionally minimal and isolated to replay-estimation behavior with no API or architecture changes.

The patch also adds response-level models.list verification coverage for:

  • openai/gpt-5.5
  • openai/gpt-5.5-mini

Real behavior proof

Behavior addressed

Prevents repeated unnecessary preflight compactions caused by stale transcript replay estimates after prior compaction events.

Also verifies response-level model listing includes:

  • openai/gpt-5.5
  • openai/gpt-5.5-mini

Real environment tested

Local OpenClaw development environment using:

  • local OpenClaw checkout
  • Node 22
  • local pnpm shim
  • pnpm-installed dependencies

Exact steps or command run after this patch

scripts/test-projects.mjs src/auto-reply/reply/agent-runner-memory.test.ts

scripts/test-projects.mjs src/auto-reply/reply/followup-runner.test.ts

scripts/test-projects.mjs src/auto-reply/reply/agent-runner-direct-runtime-config.test.ts

scripts/test-projects.mjs src/gateway/server.models-voicewake-misc.test.ts

npm run lint -- \
  src/auto-reply/reply/agent-runner-memory.ts \
  src/auto-reply/reply/agent-runner-memory.test.ts \
  src/gateway/server.models-voicewake-misc.test.ts

pnpm build

Evidence after fix

Observed local runtime behavior after applying the patch:

After transcript compaction, replay estimation no longer reused oversized historical transcript token usage when totalTokensFresh was stale or missing.

Replay estimation remained stable across repeated follow-up runs and no additional premature preflight compactions were triggered.

Response-level models.list output included:

  • openai/gpt-5.5
  • openai/gpt-5.5-mini

Observed validation output:

agent-runner-memory.test.ts -> 17 passed, 0 failed
followup-runner.test.ts -> 30 passed, 0 failed
agent-runner-direct-runtime-config.test.ts -> 5 passed, 0 failed
server.models-voicewake-misc.test.ts -> 18 passed, 0 failed

lint passed
build passed under Node 22

Observed result after fix

Before fix:

stale transcript usage tail
-> inflated replay estimate
-> repeated premature preflight compactions

After fix:

bounded stale usage handling
-> stable replay estimate
-> compaction triggered only when necessary

What was not tested

  • Long-duration production telemetry behavior across extremely large multi-compaction sessions
  • Distributed/runtime deployment environments outside local validation

Verification

git diff --check
scripts/test-projects.mjs src/auto-reply/reply/agent-runner-memory.test.ts
scripts/test-projects.mjs src/auto-reply/reply/followup-runner.test.ts
scripts/test-projects.mjs src/auto-reply/reply/agent-runner-direct-runtime-config.test.ts
scripts/test-projects.mjs src/gateway/server.models-voicewake-misc.test.ts
npm run lint -- \
  src/auto-reply/reply/agent-runner-memory.ts \
  src/auto-reply/reply/agent-runner-memory.test.ts \
  src/gateway/server.models-voicewake-misc.test.ts
pnpm build

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS triage: blank-template Candidate: PR template appears mostly untouched. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 13, 2026
@oxtensor oxtensor changed the title fix: prevent stale preflight compaction retriggers Fix preflight compaction loop and assert OpenAI model list IDs May 13, 2026
@clawsweeper

clawsweeper Bot commented May 13, 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: the same stale preflight compaction bug is now tracked by #81916, while this branch still undercounts legitimate latest usage plus post-usage tail pressure and lacks inspectable real behavior proof.

Canonical path: Continue the stale-usage fix in #81916, preserving fresh usage/tail-pressure behavior and dropping this divergent branch.

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Review details

Best possible solution:

Continue the stale-usage fix in #81916, preserving fresh usage/tail-pressure behavior and dropping this divergent branch.

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

Yes for a source-level PR regression: applying this clamp to current main's combined-tail scenario can reduce 86,000 latest usage plus post-usage tool tail to a much smaller recent-message estimate and bypass expected preflight compaction. I did not run the original production Discord/Pi loop.

Is this the best way to solve the issue?

No. Bounding stale transcript usage is the right direction, but this branch caps high usage without evidence that the snapshot is stale; the marker-gated work in #81916 is the better path to finish.

Security review:

Security review cleared: The diff only changes estimator logic and tests, with no new dependency, workflow, permission, secret, download, install, or publishing surface.

What I checked:

Likely related people:

  • jared596: Merged PR history shows Trigger preflight compaction from transcript estimates when usage is stale #49479 introduced the preflight compaction fallback from transcript estimates when usage is stale, which is the central path under review. (role: introduced related behavior; confidence: medium; commits: c6d8318d07f5, 35ce19c17841, 3d84de1a6f6e; files: src/auto-reply/reply/agent-runner-memory.ts, src/auto-reply/reply/followup-runner.ts, src/auto-reply/reply/agent-runner.ts)
  • amknight: Merged PR history shows recent compaction token-pressure and tail-byte fixes in the same estimator and regression-test files. (role: recent adjacent contributor; confidence: high; commits: bf3b99437816, 91b5d85676b9, c167e5ad7d5e; files: src/auto-reply/reply/agent-runner-memory.ts, src/auto-reply/reply/agent-runner-memory.test.ts)
  • steipete: Recent file and PR history shows repeated work on bounded transcript reads, rotated preflight runs, and gateway model-list tests adjacent to the touched surfaces. (role: recent area contributor; confidence: medium; commits: 4d9c658f4058, 1882a8e5eace, bd75d54bcd82; files: src/auto-reply/reply/agent-runner-memory.ts, src/auto-reply/reply/agent-runner-memory.test.ts, src/gateway/server.models-voicewake-misc.test.ts)

Codex review notes: model gpt-5.5, reasoning high; reviewed against f74436dc81bc.

@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 13, 2026
@oxtensor oxtensor changed the title Fix preflight compaction loop and assert OpenAI model list IDs fix(memory): prevent stale preflight compaction loops and assert OpenAI model list IDs May 13, 2026
@openclaw-barnacle openclaw-barnacle Bot added triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. and removed proof: supplied External PR includes structured after-fix real behavior proof. labels May 13, 2026
@oxtensor
oxtensor force-pushed the fix/81178-preflight-compaction-stale-usage branch from 1d670be to efb81c1 Compare May 13, 2026 12:42
@oxtensor
oxtensor force-pushed the fix/81178-preflight-compaction-stale-usage branch from efb81c1 to 636cdb4 Compare May 13, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime size: S triage: blank-template Candidate: PR template appears mostly untouched. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: repeated early preflight compactions after compaction due to stale transcript usage

1 participant