Skip to content

Fix compaction summary to use session max-output-token budget - #80

Merged
ernestrc merged 5 commits into
unstablebuild:mainfrom
santhiprakash:fix/77-compaction-max-output-tokens
Sep 20, 2026
Merged

ernestrc merged 5 commits into
unstablebuild:mainfrom
santhiprakash:fix/77-compaction-max-output-tokens

Conversation

@santhiprakash

Copy link
Copy Markdown
Contributor

Problem

agent.Summarize builds its completion request with only Messages set and never populates llmapi.Request.MaxOutputTokens. The provider client then applies its own fallback (params.MaxTokens = 8192 for Anthropic, config.MaxCompletionTokens for OpenAI, the provider config limit for Gemini) — much smaller than the budget the session is configured with. On a long conversation the summary stream stops mid-sentence, and the truncated fragment is then persisted as the post-compaction resume prompt. #78 shows the model looping on the unterminated <summary> block.

Compare the chat path at cmd/rune-agent/agent/agent.go:802, which already sets MaxOutputTokens: a.MaxOutputTokens().

Triage

The session value lives on Agent (SetMaxOutputTokens / MaxOutputTokens, set by /max_tokens, validated against the bound model by llmarg.ValidateMaxOutputTokens at cmd/rune-agent/extension/command_adapter.go:482). agent.compact (agent.go:1642) does not pass it through to CompactDialogue, and CompactDialogue (agent.go:1893) does not pass it to Summarize. The chat path is the only site that wires it correctly.

Fix

Plumb the session's max-output-token value through to the summarize request:

  • Add WithMaxOutputTokens(int) to the existing compactOptions (alongside WithCompactHooks).
  • CompactDialogue reads copts.maxOutputTokens and passes it to Summarize.
  • Summarize now takes a maxOutputTokens int parameter and sets summaryReq.MaxOutputTokens when non-zero. A value of 0 leaves the provider fallback default in place, matching today's behaviour for callers that don't pass a budget.
  • agent.compact passes WithMaxOutputTokens(a.MaxOutputTokens()) so the session value reaches the summarize request.
  • The agentshell /compact command keeps the prior behaviour — the compact service is resolved against a separate alias and there is no session value to pass through.

Per the issue, I did not clamp the value against a provider-specific ceiling. The session value is already validated against the bound model, and each provider client applies its own ceiling (effectiveMaxOutputTokens at internal/llm/gemini/client.go:160).

Verification

Regression test TestCompactDialoguePlumbsMaxOutputTokens in cmd/rune-agent/agent/agent_test.go (table-driven across WithMaxOutputTokens set / unset / explicit zero). Sabotage-verified: reverting only the if maxOutputTokens > 0 block in Summarize fails the "set option" subtest with expected 8192, actual 0.

$ go test -race -count=1 ./cmd/rune-agent/agent/
ok  	unstable.build/rune/cmd/rune-agent/agent	2.677s

Notes / Risks

  • CompactDialogue and Summarize are exported; Summarize's new maxOutputTokens int parameter is a breaking signature change for any out-of-tree caller. The only internal caller is CompactDialogue, updated here.
  • The agentshell /compact path is intentionally untouched. If the maintainer wants that path plumbed as well, follow-up is straightforward — pass the shell's session config through a new WithMaxOutputTokens call.
  • DCO trailer included on the commit; go build/go vet clean for the touched package.

Fixes #77

Plumb the session-level MaxOutputTokens value through CompactDialogue and
Summarize so long summaries are not truncated mid-sentence by the
provider's smaller default cap. The chat path at agent.go:802 already
sets MaxOutputTokens on its request; Summarize and CompactDialogue were
the two missing call sites.

A new WithMaxOutputTokens option carries the budget from the session
into CompactDialogue, and Summarize now sets MaxOutputTokens on the
request when the value is non-zero. A value of 0 (or unset) preserves
the existing provider-fallback behaviour. Agent.compact passes the
session value through; agentshell /compact keeps the prior behaviour
since the compact service is resolved against a separate alias.

Regression test TestCompactDialoguePlumbsMaxOutputTokens asserts the
captured request carries the configured budget, leaves the field at
zero when no option is supplied, and does not coerce an explicit zero
into a positive value.

Fixes unstablebuild#77

Signed-off-by: Santhi Prakash <[email protected]>
@santhiprakash

Copy link
Copy Markdown
Contributor Author

Note: the four CI workflows (Linux / Lint / Security / macOS) are showing action_required — this is the standard first-time fork-contributor workflow-approval gate. A maintainer needs to click Approve and run on each of the four workflow runs at https://github.com/unstablebuild/rune/pull/80/checks before CI will start. Not an author-side failure.

Pre-flight verification I ran locally before pushing:

$ go vet ./cmd/rune-agent/agent/                  # clean
$ go build ./cmd/rune-agent/agent/                # clean
$ go test -race -count=1 ./cmd/rune-agent/agent/  # ok 2.677s
$ go test -race -count=1 -run TestCompactDialoguePlumbsMaxOutputTokens ./cmd/rune-agent/agent/  # 3/3 sub-tests pass

Sabotage-verified: reverting only the if maxOutputTokens > 0 block in Summarize fails the new "set option" subtest with expected 8192, actual 0.

make generate and make lint need mockgen / protoc / golangci-lint which aren't in my sandbox; the repo's pre-submit check is documented as make generate && make lint && make test, so please let me know if any of those surface an issue I should fix.

Comment thread cmd/rune-agent/agent/agent.go Outdated
// a structured summary. It returns the cleaned summary text. When
// maxOutputTokens is greater than zero it is set on the request so the
// provider's smaller default cap does not truncate long summaries.
func Summarize(ctx context.Context, svc llmapi.Service, model llmapi.ModelEntry, messages []llmapi.Message, maxOutputTokens int) (string, error) {

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.

nit: can we wrap this function so it doesn't cross 90 characters wide?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Wrapped the signature in the latest push.


_, _, err := CompactDialogue(
context.Background(), svc, llmapi.ModelEntry{}, store, d,
WithMaxOutputTokens(8192),

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.

This test is a good addition but it's insufficient. We should also ensure that we can't, by mistake, drop WithMaxOutputTokens(8192) from agent.go, and suddently regress to exactly where we were prior to this PR.

I think you should be able to reuse the TestAutoCompact harness: call ag.SetMaxOutputTokens(8192) before ag.Run(...), then assert svc.requests[0].MaxOutputTokens == 8192 (request 0 is the summarize request).

Comment thread cmd/rune-agent/agent/agent.go Outdated
compactedMsgs, archivedID, err := CompactDialogue(ctx, summarizeSvc, a.config.Model, a.store, d)
compactedMsgs, archivedID, err := CompactDialogue(
ctx, summarizeSvc, a.config.Model, a.store, d,
WithMaxOutputTokens(a.MaxOutputTokens()),

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.

for the overwhelming majority of runs a.MaxOutputTokens is 0, which is the default value, unless SetMaxOutputTokens is called, and it is only if the user's config has max_tokens (not the default) or the user sets it via /max_tokens inline command, or chatmaxtokens command, so Summarize skips the assignment and the provider falls back to the default i.e 8192. The commit subject says "Fix compaction summary to use session max-output-token budget," but on a default install there is no session budget to use.

I suggest we either set a summarize default that's higher than 8192, either here with a max() statement, or set a default max_tokens higher in the default config, which would apply to all agent runs, not just summaries.

Address review feedback on PR unstablebuild#80:

- Wrap Summarize signature so it stays within 90 characters.
- Add a model-capped default summary budget (32768) that applies when the
  session has not set an explicit max-output-token override, fixing the
  default-install case where the provider fallback (8192) truncated long
  summaries.
- Add regression tests covering both the explicit session override and the
  default budget path through the auto-compact harness.

Signed-off-by: Santhi Prakash <[email protected]>

@santhiprakash santhiprakash left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Pushed an update addressing the review feedback:

  • Wrapped Summarize so the signature stays within 90 columns.
  • Added a model-capped default summary budget (32768, clamped to the bound
    model's documented ceiling via llmarg.MaxOutputTokens) that applies when
    the session has not set an explicit max_tokens override. This fixes the
    default-install case where the Anthropic 8192 provider fallback truncated
    long summaries.
  • Added TestAutoCompactPlumbsMaxOutputTokens and
    TestAutoCompactUsesDefaultMaxOutputTokens using the TestAutoCompact
    harness to cover both the explicit session override and the default budget
    paths.

Local verification: go vet, go build, and go test -race for
./cmd/rune-agent/agent/ all pass.

@santhiprakash

Copy link
Copy Markdown
Contributor Author

Pushed an update addressing the review feedback:

  • Wrapped Summarize so the signature stays within 90 columns.
  • Added a model-capped default summary budget (32768, clamped to the bound
    model's documented ceiling via llmarg.MaxOutputTokens) that applies when
    the session has not set an explicit max_tokens override. This fixes the
    default-install case where the Anthropic 8192 provider fallback truncated
    long summaries.
  • Added TestAutoCompactPlumbsMaxOutputTokens and
    TestAutoCompactUsesDefaultMaxOutputTokens using the TestAutoCompact
    harness to cover both the explicit session override and the default budget
    paths.

Local verification: go vet, go build, and go test -race for
./cmd/rune-agent/agent/ all pass.

@ernestrc
ernestrc enabled auto-merge (squash) September 20, 2026 18:44
@ernestrc
ernestrc merged commit 11db287 into unstablebuild:main Sep 20, 2026
8 checks passed
@codecov

codecov Bot commented Sep 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@santhiprakash

Copy link
Copy Markdown
Contributor Author

Shipped in 11db287657af9ca7e26e4d9c40658ea5216d0070 — thanks for the review and merge, @ernestrc.

ernestrc added a commit that referenced this pull request Sep 20, 2026
CompactDialogue has two callers. #80 wired the budget through Agent.compact, the
auto-compact path, but agentshell.compactConversation still called it bare, so every
manual /compact fell back to the provider default of 8192 on Anthropic and truncated the
summary mid-sentence.

Signed-off-by: ernestrc <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compaction summary request sends no max-output-token budget and silently falls back to the provider default

2 participants