Skip to content

Account for system prompt growth when sizing compaction - #496

Open
AshishKumar4 wants to merge 1 commit into
mainfrom
fix/compaction-prompt-growth
Open

AshishKumar4 wants to merge 1 commit into
mainfrom
fix/compaction-prompt-growth

Conversation

@AshishKumar4

@AshishKumar4 AshishKumar4 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

What does this change?

Compaction sizing starts from the tokens the provider reported for the last measured step and
estimates only the messages added since (agent.ts, the contextTokens computation). That assumed
the system prompt was unchanged between turns. It is not: the prompt is rebuilt every turn from the
workspace's gadget list, connectable vendors and output formats, and #267 adds the ambient catalogs.
Growth there never entered the estimate, so a chat near its window could skip compaction and send a
prompt the provider rejects. Because a failed request records no usage, the stale baseline then
repeats on the next turn.

The step's system prompt length is now stored beside its token total
(StoredChatMetadata.measuredSystemPromptChars, written in addChatMessages, cleared at both
totalTokens reset sites, stripped from client deliveries in chatMetaForClient). The next turn
adds the growth against the current length. A shrink is not credited: the usage is exact and the
four-characters-per-token ratio is a heuristic, so subtracting could under-count; over-counting only
compacts earlier, and the next measured step re-baselines. A step measured before the length was
recorded prices the prompt as unchanged, which is today's behavior.

The estimate moves out of runAgent into estimateContextTokens in agent-compaction.ts.

Why is this obviously correct and trivially verifiable?

The helper reproduces the previous estimate exactly (same measured gate, same post-measurement
filter including the measured record's tool results) and adds one non-negative term. The unit tests
cover the unmeasured path, the measured path, growth, shrink, and a measurement taken before the
length was recorded. The stored field is written only with totalTokens, cleared wherever
totalTokens is, and never reaches clients.

Run: pnpm --filter @gadgets/workshop-backend exec vitest run __tests__/agent-compaction.test.ts

Checklist

Checking every item does not guarantee acceptance. Maintainers determine whether
a pull request meets the contribution policy.

  • This is a small, concrete change; it is not a feature, refactor, or low-value cleanup.
  • I understand that maintainers decide whether the change is obviously correct and trivially verifiable.
  • I have read and followed the contribution guidelines.

Devin Review

Compaction sizing starts from the tokens the provider reported for the last
measured step and estimates only the messages added since. That assumed the
system prompt was unchanged between turns, but it is rebuilt every turn from
the workspace: the gadget list, the connectable vendors, the output formats,
and (with #267) the ambient catalogs. Anything that grew there could keep the
estimate under the threshold and send a prompt past the model's window.

The step's system prompt length is now recorded beside its token total
(StoredChatMetadata.measuredSystemPromptChars, cleared wherever totalTokens is)
and the next turn adds the growth against the current length. A shrink is not
credited: the usage is exact and the ratio a heuristic, so subtracting could
under-count and skip a compaction the provider then refuses, with no successful
step left to correct the baseline. Over-counting only compacts earlier, and the
next measured step re-baselines. A step measured before the length was recorded
prices the prompt as unchanged, as before. The estimate moves into
estimateContextTokens, which the unit tests cover.
@github-actions github-actions Bot added the kernel Changes to the Workshop kernel label Sep 14, 2026
@github-actions

Copy link
Copy Markdown

Preview: pr496-fix-compactio-7981be8d

https://pr496-fix-compactio-7981be8d-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.

✅ 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

LGTM!

github run

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant