mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-27 16:57:44 +02:00
feat(shared-ai): wire compactor into runPlannerLoop (M2.2)
PlannerLoopInput grows an optional compactor:
compactor?: {
maxContextTokens: number;
threshold?: number; // default 0.92, matches Claude Code wU2
compact: (messages) => Promise<{ messages, compactedTurns }>;
}
Before each LLM call the loop checks whether promptTokens+completion
has crossed threshold × maxContextTokens. If yes AND we haven't
compacted this run yet, the callback runs, its returned messages
REPLACE the live history, and compactionsDone flips to 1 so a
runaway tool can't re-trigger.
Design choices:
- Fires at most ONCE per loop run. If the fresh (compacted)
history hits the threshold again in the same run, the LLM
round budget will hit first; better to terminate than to
recursively compact a summary.
- No reminder emitted automatically — the caller can wire
that via reminderChannel by reading compactionsDone from
LoopState (next PR; compactionsDone isn't exposed yet to
keep the state surface small).
- compactor callback is injectable, not hardcoded to
compactHistory() from compact.ts. Lets mana-ai route the
compactor LLM call to a cheaper model (Haiku) without
changing the loop.
- Zero maxContextTokens → skip silently (same contract as
shouldCompact()).
Also cleaned up the isParallelSafe non-null-assertion warning by
hoisting the predicate to a local with proper narrowing.
5 new loop tests: below-threshold no-op, single-fire replacement,
once-per-run idempotency, zero-cap bail, no-op when compactor
returns 0 turns. 76 shared-ai tests total, green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
aab1e3045b
commit
3d8214a147
2 changed files with 236 additions and 2 deletions
|
|
@ -144,6 +144,35 @@ export interface PlannerLoopInput {
|
|||
* constant-time lookups are expected (registry hit, name-prefix check).
|
||||
*/
|
||||
readonly isParallelSafe?: (toolName: string) => boolean;
|
||||
/**
|
||||
* Context-window compactor wiring (Claude-Code `wU2` pattern).
|
||||
*
|
||||
* When set AND usage crosses the threshold, the loop replaces the
|
||||
* middle of the message history with a compact summary before the
|
||||
* next LLM call. The compact summary is persisted in the returned
|
||||
* `messages` — unlike reminders, this IS part of the canonical
|
||||
* history because raw turns got dropped.
|
||||
*
|
||||
* Contract:
|
||||
* - `maxContextTokens`: provider ceiling; compactor skips when unset
|
||||
* (matches `shouldCompact()`'s safe-bail behaviour).
|
||||
* - `compact`: async callback that performs the compaction. Pass
|
||||
* `compactHistory` from this package or an adapter that uses a
|
||||
* cheaper model (e.g. Haiku) for the compactor's LLM call.
|
||||
* - `threshold`: optional override, default 0.92.
|
||||
*
|
||||
* Compaction fires at MOST once per loop run — once a round has been
|
||||
* compacted, we don't re-trigger until the next run, even if the
|
||||
* fresh history hits the threshold again (defence-in-depth against
|
||||
* a runaway tool that keeps bloating turns).
|
||||
*/
|
||||
readonly compactor?: {
|
||||
readonly maxContextTokens: number;
|
||||
readonly threshold?: number;
|
||||
readonly compact: (
|
||||
messages: readonly ChatMessage[]
|
||||
) => Promise<{ readonly messages: readonly ChatMessage[]; readonly compactedTurns: number }>;
|
||||
};
|
||||
}
|
||||
|
||||
/** Max concurrent tool executions per round. Mirrors Claude Code's gW5
|
||||
|
|
@ -206,10 +235,30 @@ export async function runPlannerLoop(opts: {
|
|||
let rounds = 0;
|
||||
let promptTokens = 0;
|
||||
let completionTokens = 0;
|
||||
let compactionsDone = 0;
|
||||
|
||||
while (rounds < maxRounds) {
|
||||
rounds++;
|
||||
|
||||
// Context-window compactor (Claude-Code `wU2`): check BEFORE the
|
||||
// next LLM call whether the previous round's usage crossed the
|
||||
// threshold; if so, replace the middle of `messages` with a
|
||||
// compact summary. Fire at most once per loop run so a runaway
|
||||
// tool can't keep re-triggering.
|
||||
if (input.compactor && compactionsDone === 0) {
|
||||
const total = promptTokens + completionTokens;
|
||||
const cap = input.compactor.maxContextTokens;
|
||||
const threshold = input.compactor.threshold ?? 0.92;
|
||||
if (cap > 0 && total > 0 && total / cap >= threshold) {
|
||||
const compactResult = await input.compactor.compact(messages);
|
||||
if (compactResult.compactedTurns > 0) {
|
||||
messages.length = 0;
|
||||
for (const m of compactResult.messages) messages.push(m);
|
||||
compactionsDone++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Per-round reminder injection: ask the channel for transient
|
||||
// hints, wrap each in <reminder> tags, and prepend them as system
|
||||
// messages to THIS request only. Nothing gets pushed to `messages`
|
||||
|
|
@ -277,10 +326,11 @@ export async function runPlannerLoop(opts: {
|
|||
// In both modes we append to `messages` in the LLM's original
|
||||
// call order, not completion order, so the debug-log stays linear.
|
||||
const calls = response.toolCalls;
|
||||
const parallelSafePredicate = input.isParallelSafe;
|
||||
const allParallelSafe =
|
||||
!!input.isParallelSafe &&
|
||||
!!parallelSafePredicate &&
|
||||
calls.length > 1 &&
|
||||
calls.every((c) => input.isParallelSafe!(c.name));
|
||||
calls.every((c) => parallelSafePredicate(c.name));
|
||||
|
||||
if (allParallelSafe) {
|
||||
for (let i = 0; i < calls.length; i += PARALLEL_TOOL_BATCH_SIZE) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue