Skip to content

Re-check compaction after every agent step - #493

Merged
AshishKumar4 merged 1 commit into
mainfrom
agent-step-compaction
Sep 15, 2026
Merged

AshishKumar4 merged 1 commit into
mainfrom
agent-step-compaction

Conversation

@AshishKumar4

@AshishKumar4 AshishKumar4 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

The compaction check ran once, at the start of a turn. A turn whose tool results grew the prompt past the provider limit failed mid-turn with a context error; the user pressed retry, and only that second turn compacted. Robert hit this on Sol at roughly 512K tokens.

runAgent now owns the loop the Overseer used to run around it: load the durable history, run one pass, publish any checkpoint the pass produced, go again until a pass finishes. The Overseer's side is one await runAgent(...) plus two hooks, loadChatHistory and commitChatCompaction, which are the two private helpers it already had. A pass ends early either because it compacted instead of prompting the model, as before, or, new here, because a persisted tool step left the next request over the compaction trigger. That decision sits in shouldStopAfterTurn beside the other stop reasons: the step's measured prompt plus its tool results as the model will see them (not the details copy), or, when the provider reported no usage, a reload regardless so the turn-start estimator measures the whole prompt, the same rule that check already follows. Cancellation, connection requests and pending approvals still end the turn ahead of compaction.

The 30-step cap on a turn is gone. It came from the ai-sdk's stepCountIs(30) and was carried over when the loop moved to pi-agent-core; nothing else needed it.

GPT-5.6 Sol, Luna and Terra also gain a compactionInputBudget of 272K, the point past which OpenAI prices their input higher. Compaction sizes against that instead of the 1.05M window. The window and output limit are unchanged; getModelTokenLimits returns the smaller of the two as inputBudget, so nothing else moves.

Tests: the GPT-5.6 limits in agent-compaction.test.ts, plus the full backend suite and integration. The post-step decision is five lines over two functions the suite already covers (estimateProjectionTokens, shouldCompactChat); the scripted-model harness reports a fixed token count and has no summarizer, so an end-to-end test of it would mean building that, and the incident is the end-to-end check. Build and lint clean.

@github-actions github-actions Bot added kernel Changes to the Workshop kernel workshop/shared Changes to shared Workshop APIs labels Sep 14, 2026
@github-actions

Copy link
Copy Markdown

Preview: pr493-agent-step-compaction

https://pr493-agent-step-compaction-router.cloudflare-os-previews.workers.dev

Dashboard · deleted when this PR closes

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note

This report is out of date. Scroll down for Devin Review's latest report on this PR.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@ask-bonk

ask-bonk Bot commented Sep 14, 2026

Copy link
Copy Markdown
  • P2 agent-compaction.ts:271: UTF-16 length divided by four severely undercounts CJK/high-density Unicode tool results. The next prompt can exceed the provider limit without triggering compaction, and retries repeat the same estimate. Use conservative or tokenizer-backed accounting.
  • P3 agent.ts:3534: Reloading discards CodePreviewManager state. After a failed/no-op edit triggers compaction, the next non-edit tool cannot clear the client’s active-file marker, leaving stale editing UI until the agent stops.

github run

@AshishKumar4

Copy link
Copy Markdown
Contributor Author

On the two Bonk points:

P2, chars/4 undercounting CJK: that is the estimator the turn-start check has always used; the post-step check adds the same estimate to the provider-measured total for the step, so the two checks agree and each step re-anchors on real usage. Not changing the estimator here. #494 caps what one result can contribute, which bounds the exposure.

P3, the active-file marker surviving a reload: real. runAgent now clears it before returning reloadForCompaction, the same thing a non-edit tool start does, so the rerun's fresh preview manager starts from the state the client sees.

@ask-bonk

ask-bonk Bot commented Sep 14, 2026

Copy link
Copy Markdown
  • P3 packages/workshop-shared/src/api.ts:1292: Exported SUGGESTED_MODELS no longer has a direct doc comment; the documentation now attaches to private SUGGESTED_MODEL_CATALOG. This violates the shared public API documentation requirement. Move or duplicate the JSDoc onto the export.

github run

@AshishKumar4

Copy link
Copy Markdown
Contributor Author

The SUGGESTED_MODELS doc: moved onto the export, with the per-field docs on the entry type where they belong.

@ask-bonk

ask-bonk Bot commented Sep 14, 2026

Copy link
Copy Markdown

LGTM!

github run

@AshishKumar4
AshishKumar4 force-pushed the agent-step-compaction branch 2 times, most recently from 5fc3f20 to 970f1ec Compare September 14, 2026 19:35
@ask-bonk

ask-bonk Bot commented Sep 14, 2026

Copy link
Copy Markdown
  • P2 packages/workshop-backend/src/agent.ts:3510: When provider usage is unavailable (totalTokens === 0), the post-step check counts only tool results and drops the existing estimated context. A large result can therefore push the next request over budget without triggering compaction. Maintain a running estimate for zero-usage steps and add coverage for this case.

github run

devin-ai-integration[bot]

This comment was marked as resolved.

@ask-bonk

ask-bonk Bot commented Sep 14, 2026

Copy link
Copy Markdown

LGTM!

github run

@AshishKumar4

Copy link
Copy Markdown
Contributor Author

Bonk P2 (19:37) and Devin on the zero-usage case: both right, and I was wrong to remove it. The turn-start check at agent.ts:2619 already treats measuredTokens > 0 as the condition for trusting provider usage and estimates the whole prompt otherwise, so the post-step check dropping that rule made the two disagree. Restored.

While there, the decision moved out of its own exported helper and into shouldStopAfterTurn beside the other stop reasons, which is where it reads best; the helper's unit test went with it, for the reason in the PR body.

@ask-bonk

ask-bonk Bot commented Sep 14, 2026

Copy link
Copy Markdown

LGTM!

github run

Comment thread packages/workshop-backend/src/agent.ts Outdated
Comment thread packages/workshop-backend/src/agent.ts Outdated
The compaction check ran once, at the start of a turn. A turn whose tool
results grew the prompt past the provider limit then failed mid-turn, the
user saw an error, and compaction only ran after a manual retry.

runAgent now owns the loop the Overseer used to run around it: it loads
the durable history, runs one pass, publishes any checkpoint the pass
produced, and goes again until a pass finishes. A pass ends early either
because it compacted instead of prompting the model (as before) or, new
here, because a persisted tool step left the next request over the
compaction trigger: the step's measured prompt plus its tool results, or,
when the provider reported no usage, a reload regardless so the turn-start
estimator measures the whole prompt. Cancellation, connection requests and
pending approvals still end the turn ahead of compaction.

The 30-step cap on a turn is gone. It came from the ai-sdk's
stepCountIs(30) and was carried over when the loop moved to pi-agent-core;
nothing else needed it.

GPT-5.6 Sol, Luna and Terra gain a `compactionInputBudget` of 272K, the
point past which OpenAI prices their input higher. Compaction sizes
against that instead of the 1.05M window; the window itself is unchanged.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Devin Review found 2 new potential issues.

Devin Review

Comment thread packages/workshop-backend/src/agent.ts
Comment thread packages/workshop-backend/src/agent.ts
@ask-bonk

ask-bonk Bot commented Sep 15, 2026

Copy link
Copy Markdown
  • P2 packages/workshop-backend/src/agent.ts:3550: A failed automatic compaction is retried after every subsequent tool step. Persistent summarization failures therefore add an expensive failed request and full replay per step. Latch the failure for the current runAgent() turn before reloading again.

github run

@AshishKumar4
AshishKumar4 merged commit f8c0558 into main Sep 15, 2026
19 checks passed
@AshishKumar4
AshishKumar4 deleted the agent-step-compaction branch September 15, 2026 21:19
tosaka07 added a commit to tosaka07/cloudflare-os that referenced this pull request Sep 17, 2026
# By Maximo Guk (9) and others
# Via GitHub
* origin: (33 commits)
  Bump the react-and-ui group across 1 directory with 4 updates (cloudflare#515)
  Bump the editor-codemirror group across 1 directory with 5 updates (cloudflare#502)
  Bump the remaining-npm group across 1 directory with 5 updates (cloudflare#501)
  Bump the github-actions group with 2 updates (cloudflare#499)
  Fix blueprint configurator readiness (cloudflare#505)
  fix sizing issues with user search ui (cloudflare#511)
  gatekeeper-confluence: stop reporting the access-token expiry as the credential expiry (cloudflare#509)
  Review Workshop eval trajectory changes (cloudflare#477)
  Compare Workshop evals on pull requests (cloudflare#476)
  Re-check compaction after every agent step (cloudflare#493)
  Write the bundled format blueprints in TypeScript (cloudflare#466)
  add deployment-wide user directory for user search (cloudflare#474)
  Load agent catalogs each turn instead of caching them on the chat (cloudflare#267)
  Updated agent prompt: avoid unnecessary implementation details in response, don't always create gadgets (cloudflare#489)
  Fix Anthropic streams in the local eval target; default evals to GLM 5.3 Flash (cloudflare#495)
  Make spawned agents persistent across restarts (cloudflare#492)
  Bump vitest from 4.1.10 to 4.1.11 (cloudflare#470)
  Restricted data UI - share modal stays usable for restricted workspaces (cloudflare#308)
  Restricted data: govern restricted reads by observer verification (cloudflare#382)
  Rename prohibitAllSharing -> containsRestrictedData (cloudflare#381)
  ...

# Conflicts:
#	packages/workshop-backend/src/user.ts
#	packages/workshop-shared/src/api.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Changes to the Workshop kernel workshop/shared Changes to shared Workshop APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants