Skip to content

feat: run auto-compact on --plain, ACP, and subagent loops - #1066

Merged
will-lamerton merged 3 commits into
Nano-Collective:mainfrom
Dhirenderchoudhary:fix/1048-headless-auto-compact
Aug 30, 2026
Merged

will-lamerton merged 3 commits into
Nano-Collective:mainfrom
Dhirenderchoudhary:fix/1048-headless-auto-compact

Conversation

@Dhirenderchoudhary

@Dhirenderchoudhary Dhirenderchoudhary commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

autoCompact only 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 performAutoCompact helper after each assistant turn (same gate as the TUI, no chat-queue notify). Subagents also pass history through capMessagesForModel and 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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Changeset

  • Added a changeset (pnpm changeset) describing this change for the changelog

Docs-only or internal chores need no changeset (or run pnpm changeset --empty to note that intentionally).

Testing

Automated Tests

  • New features include passing tests in .spec.ts/tsx files
  • All existing tests pass (pnpm test:all completes successfully)
  • Tests cover both success and error scenarios

Manual Testing

  • Tested with Ollama
  • Tested with OpenRouter
  • Tested with OpenAI-compatible API
  • Tested MCP integration (if applicable)

Checklist

  • If this was for an open issue, I was assigned to it
  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)
  • Appropriate logging added using structured logging (see CONTRIBUTING.md)

@will-lamerton will-lamerton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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's const messages (line 166) and initSubagentSession (441) keep the pre-compaction array. Benign today, easy to trip later.
  • The bare catch { return messages; } in maybeAutoCompact logs nothing. Given #1048 is "compaction wasn't running and nobody noticed", a logger.debug in the catch seems worth it, and the structured-logging box is unticked on the checklist.
  • No user-visible signal during compaction. --plain already has writeStatus for empty-turn retries; an LLM-strategy compaction currently reads as a hang.
  • conversation-loop.tsx:595-633 still 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 optional onNotify to 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 = 3 in place and restores only when originalMaxMessages !== undefined. reloadAppConfig() in finally is safer.
  • Neither new test in auto-compact.spec.ts resets its session overrides. They're last in the file so it passes today, but the next test appended inherits enabled: false / strategy: 'mechanical'. t.teardown(resetAutoCompactSession) fixes it.
  • t.true(secondHistory.length < filler.length) and t.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 catch fallback in maybeAutoCompact, and the toolsDisabled ? undefined : tools gating.

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.

@will-lamerton
will-lamerton merged commit 549b430 into Nano-Collective:main Aug 30, 2026
10 of 11 checks passed
will-lamerton added a commit that referenced this pull request Aug 30, 2026
…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.
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.

[Feature] Auto-compact on headless, ACP, and subagent loops

2 participants