Repository navigation
feat: run auto-compact on --plain, ACP, and subagent loops - #1066
will-lamerton merged 3 commits into
Conversation
will-lamerton
left a comment
There was a problem hiding this comment.
Thanks for this, the core design is right. Insertion points mirror the TUI exactly (after the assistant message, before tool results), so the tool-call pairing invariant is preserved by the same summariseWithLLM boundary walk. I ran the branch locally: tsc --noEmit is clean and the four touched spec files pass.
Two things I'd like changed before merge, plus a coverage gap.
1. Compaction is uncancellable in the two loops where cancel matters.
--plain checks abortSignal.aborted at source/plain/conversation.ts:206 and ACP at source/acp/acp-conversation.ts:156 / 233. Both new maybeAutoCompact calls land past those checks. With the default strategy: 'llm' that's a full extra model round trip nothing can interrupt, so an ACP session/cancel from the editor now stalls for a summariser turn. summariseWithLLM already accepts a signal; performAutoCompact just never passes one. Please thread an optional signal through maybeAutoCompact -> performAutoCompact -> summariseWithLLM, plus an early if (signal?.aborted) return messages;.
2. The subagent gate measures history the model never receives.
subagent-executor.ts caps the payload into modelMessages but calls maybeAutoCompact(messages.slice(1), ...) on the full uncapped array. Once a run exceeds sessions.maxMessages, the gate counts messages already dropped from the request, so compaction fires on a context the provider never sees. Either compact the capped view, or cap after compacting, so both operate on the same set.
3. Subagent auto-compact has no test.
The new subagent test covers capMessagesForModel only. The headline claim for subagents is untested, and it's the one path that differs structurally from the TUI (system row spliced out and back in). Worth a test.
Smaller points, happy to take these as follow-ups:
- Two divergent system-message guards in the subagent: the capping block handles a non-system
messages[0], the compaction block silently no-ops on it. Derive the split once and reuse it. messages = [systemMessage, ...compacted]rebinds a function parameter, so the caller'sconst messages(line 166) andinitSubagentSession(441) keep the pre-compaction array. Benign today, easy to trip later.- The bare
catch { return messages; }inmaybeAutoCompactlogs nothing. Given #1048 is "compaction wasn't running and nobody noticed", alogger.debugin the catch seems worth it, and the structured-logging box is unticked on the checklist. - No user-visible signal during compaction.
--plainalready haswriteStatusfor empty-turn retries; an LLM-strategy compaction currently reads as a hang. conversation-loop.tsx:595-633still carries its own inline copy of the gate. This PR creates the shared helper and leaves the TUI as the one caller not using it, which will drift. It needs only an optionalonNotifyto absorb the TUI too.- The plain call sits inside
if (hasAssistantPayload)while the TUI runs the gate unconditionally, so a run alternating empty turns and nudges never compacts.
Test hygiene:
- The subagent test writes
getAppConfig().sessions!.maxMessages = 3in place and restores only whenoriginalMaxMessages !== undefined.reloadAppConfig()infinallyis safer. - Neither new test in
auto-compact.spec.tsresets its session overrides. They're last in the file so it passes today, but the next test appended inheritsenabled: false/strategy: 'mechanical'.t.teardown(resetAutoCompactSession)fixes it. t.true(secondHistory.length < filler.length)andt.true(last.length < 8)pass for reasons other than the one under test. Asserting the compacted shape would fail for the right reason.- Uncovered branches: the
catchfallback inmaybeAutoCompact, and thetoolsDisabled ? undefined : toolsgating.
Nit: new spec imports use .js extensions while their neighbours in the same files are extensionless, and they reach into models-dev-client directly rather than the @/models/index barrel that auto-compact.ts uses. Biome excludes *.spec.ts so it won't auto-fix.
Changeset and docs are accurate, including the "/compact is still TUI-only" caveat.
…tory The shared-helper refactor in #1066 switched the chat loop to maybeAutoCompact, which derives the provider and model from the client and swallows any failure. That dropped the loop's own currentProvider / currentModel on the floor and turned a lookup failure into a silent no-op, the exact failure mode #1048 was about. maybeAutoCompact now takes both as explicit overrides and the TUI passes them. The compaction gate in the --plain, ACP, and subagent loops now measures the same capped view the model receives, matching the request instead of counting rows the cap already dropped. All three only adopt the result when compaction actually ran: maybeAutoCompact returns its input untouched otherwise, so writing it back was permanently discarding history the cap only meant to hide from a single request. Also tightens the plain and ACP compaction assertions, which passed as long as the concatenated history was shorter than the filler rather than checking the verbose turn was compressed out and the recent turn kept. Restores the 6 conversation-loop specs that regressed on main.
Description
autoCompactonly ran in the TUI.--plain/nanocoder run, ACP, and subagents ignored the token budget and kept stuffing tool results until the provider 400'd.Those three loops now call the existing
performAutoCompacthelper after each assistant turn (same gate as the TUI, no chat-queue notify). Subagents also pass history throughcapMessagesForModeland keep the system row out of the compact input so system tokens are not counted twice. No second compressor, no new config key.Closes #1048
Type of Change
Changeset
pnpm changeset) describing this change for the changelogDocs-only or internal chores need no changeset (or run
pnpm changeset --emptyto note that intentionally).Testing
Automated Tests
.spec.ts/tsxfilespnpm test:allcompletes successfully)Manual Testing
Checklist