@aragon-agent/cli 0.6.62 → 0.6.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +70 -2
- package/README.md +143 -27
- package/dist/agent/controller.d.ts +66 -14
- package/dist/agent/controller.js +291 -81
- package/dist/agent/headless.d.ts +16 -0
- package/dist/agent/headless.js +62 -3
- package/dist/agent/team-notices.d.ts +57 -7
- package/dist/agent/team-notices.js +93 -10
- package/dist/app/App.js +199 -77
- package/dist/cli.js +4 -1
- package/dist/commands/builtins.js +2 -1
- package/dist/commands/catalog.js +1 -1
- package/dist/compaction/archive.d.ts +27 -1
- package/dist/compaction/archive.js +8 -0
- package/dist/compaction/budget.d.ts +73 -0
- package/dist/compaction/budget.js +89 -0
- package/dist/compaction/child.js +6 -1
- package/dist/compaction/command.js +147 -1
- package/dist/compaction/compactor.d.ts +132 -15
- package/dist/compaction/compactor.js +288 -95
- package/dist/compaction/context-command.js +21 -0
- package/dist/compaction/memory-agent-prompt.d.ts +32 -0
- package/dist/compaction/memory-agent-prompt.js +76 -0
- package/dist/compaction/memory-agent-tools.d.ts +88 -0
- package/dist/compaction/memory-agent-tools.js +338 -0
- package/dist/compaction/memory-agent.d.ts +114 -0
- package/dist/compaction/memory-agent.js +220 -0
- package/dist/compaction/prune.d.ts +129 -0
- package/dist/compaction/prune.js +386 -0
- package/dist/compaction/retry-plan.d.ts +103 -0
- package/dist/compaction/retry-plan.js +136 -0
- package/dist/compaction/summarize-ladder.d.ts +149 -0
- package/dist/compaction/summarize-ladder.js +273 -0
- package/dist/compaction/wiring.d.ts +19 -0
- package/dist/compaction/wiring.js +65 -1
- package/dist/config/cli-commands.d.ts +2 -2
- package/dist/config/cli-commands.js +27 -0
- package/dist/config/env.d.ts +3 -1
- package/dist/config/env.js +44 -2
- package/dist/exec/events.d.ts +30 -0
- package/dist/exec/index.js +2 -0
- package/dist/exec/runner.js +16 -1
- package/dist/gene/commit-prepare.js +27 -1
- package/dist/gene/curator/curator.d.ts +15 -0
- package/dist/gene/curator/curator.js +26 -2
- package/dist/gene/curator/scripted-lane.d.ts +12 -0
- package/dist/gene/curator/scripted-lane.js +101 -0
- package/dist/gene/curator/task-context.d.ts +21 -0
- package/dist/gene/curator/task-context.js +33 -0
- package/dist/gene/curator/task-runner.d.ts +10 -1
- package/dist/gene/curator/task-runner.js +39 -4
- package/dist/gene/curator/tool-support.js +11 -4
- package/dist/gene/curator/toolkit.d.ts +13 -0
- package/dist/gene/curator/toolkit.js +23 -1
- package/dist/gene/curator/tools-verify.js +2 -2
- package/dist/gene/curator/tools-write.js +6 -1
- package/dist/gene/curator/validate/evidence-tokens.js +133 -22
- package/dist/gene/curator/validate/knowledge.d.ts +41 -1
- package/dist/gene/curator/validate/knowledge.js +74 -11
- package/dist/gene/curator/validate/prompts.d.ts +10 -2
- package/dist/gene/curator/validate/prompts.js +2 -2
- package/dist/gene/learn/draft-guard.d.ts +17 -0
- package/dist/gene/learn/draft-guard.js +113 -0
- package/dist/gene/learn/learn-prompt.d.ts +22 -1
- package/dist/gene/learn/learn-prompt.js +27 -2
- package/dist/gene/learn/learner.js +20 -2
- package/dist/gene/queue.d.ts +16 -0
- package/dist/gene/queue.js +44 -1
- package/dist/governance/store/run-store.d.ts +20 -0
- package/dist/governance/store/run-store.js +54 -8
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.js +1 -0
- package/dist/host/interaction.d.ts +8 -17
- package/dist/host/interaction.js +50 -72
- package/dist/host/team-wake-driver.d.ts +90 -0
- package/dist/host/team-wake-driver.js +114 -0
- package/dist/logging/install.d.ts +59 -1
- package/dist/logging/install.js +15 -0
- package/dist/session/validate-session.js +11 -0
- package/dist/team/console-tools.d.ts +12 -0
- package/dist/team/console-tools.js +20 -3
- package/dist/team/console-view.js +11 -1
- package/dist/team/notices.d.ts +81 -4
- package/dist/team/notices.js +187 -22
- package/dist/team/prompt.js +1 -1
- package/dist/team/runtime.d.ts +67 -24
- package/dist/team/runtime.js +250 -31
- package/dist/team/stall-watch.d.ts +54 -0
- package/dist/team/stall-watch.js +119 -0
- package/dist/team/task-tool.js +2 -2
- package/dist/team/wake.d.ts +176 -0
- package/dist/team/wake.js +444 -0
- package/package.json +81 -81
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-operation spend ceiling (context-compaction-v3 W3).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY: `src/compaction/**` is inside the glyph scanner's scope.
|
|
5
|
+
*
|
|
6
|
+
* WHY PER OPERATION AND NOT PER SESSION (D-8). A session-level cap turns the
|
|
7
|
+
* seventh compaction of a long run into an unexplained cliff: the feature simply
|
|
8
|
+
* stops working, with nothing about the moment that explains it. Per-operation
|
|
9
|
+
* keeps every refusal local, visible on the card that caused it, and repeatable.
|
|
10
|
+
*
|
|
11
|
+
* TWO UNITS, TWO MECHANISMS:
|
|
12
|
+
*
|
|
13
|
+
* - TOKENS are charged from the provider's REAL `usage`, and the projection is
|
|
14
|
+
* used only to refuse a request that is obviously already over. Charging the
|
|
15
|
+
* projection would bill a request that never happened.
|
|
16
|
+
* - DOLLARS need a price table. When the summarizer model is not priced the
|
|
17
|
+
* dollar gate is switched off and says so (`priceUnknown`), because a
|
|
18
|
+
* fake `$0.00` on a feature that is spending money is the same class of lie
|
|
19
|
+
* the fast tier's RV-11 records (D-9).
|
|
20
|
+
*
|
|
21
|
+
* `0` MEANS UNLIMITED IN BOTH UNITS, and that is why the arithmetic never has to
|
|
22
|
+
* special-case "no budget": a zero limit is never reached by a growing total.
|
|
23
|
+
*/
|
|
24
|
+
/** Tokens a usage entry actually costs; cache reads and writes count as input. */
|
|
25
|
+
export function chargedTokens(usage) {
|
|
26
|
+
return usage.inputTokens + usage.outputTokens
|
|
27
|
+
+ (usage.cacheReadTokens ?? 0) + (usage.cacheWriteTokens ?? 0);
|
|
28
|
+
}
|
|
29
|
+
export class CompactionBudget {
|
|
30
|
+
limits;
|
|
31
|
+
spentTokens = 0;
|
|
32
|
+
spentUsd = 0;
|
|
33
|
+
priceUnknown = false;
|
|
34
|
+
stopped;
|
|
35
|
+
/**
|
|
36
|
+
* THE DEDUPE KEY IS THE USAGE OBJECT'S IDENTITY, not a turn counter.
|
|
37
|
+
*
|
|
38
|
+
* One `callOnce` can produce several REAL billable calls inside core's
|
|
39
|
+
* `withRetry`, and each of them hands back its own usage object. A counter
|
|
40
|
+
* would charge the retries as one; object identity charges what was spent and
|
|
41
|
+
* still refuses to charge the same object twice (the single-shot path forwards
|
|
42
|
+
* the same usage to two callbacks).
|
|
43
|
+
*/
|
|
44
|
+
charged = new WeakSet();
|
|
45
|
+
constructor(limits) {
|
|
46
|
+
this.limits = limits;
|
|
47
|
+
}
|
|
48
|
+
/** Whether a request of this projected size would cross the token ceiling. */
|
|
49
|
+
wouldExceed(projectedTokens) {
|
|
50
|
+
if (this.limits.tokens <= 0)
|
|
51
|
+
return false;
|
|
52
|
+
return this.spentTokens + Math.max(0, projectedTokens) > this.limits.tokens;
|
|
53
|
+
}
|
|
54
|
+
/** Record what a real call cost. Never throws: a budget cannot fail a compaction. */
|
|
55
|
+
charge(input) {
|
|
56
|
+
if (!this.charged.has(input.usage)) {
|
|
57
|
+
this.charged.add(input.usage);
|
|
58
|
+
this.spentTokens += chargedTokens(input.usage);
|
|
59
|
+
if (!input.pricingUnknown)
|
|
60
|
+
this.spentUsd += Math.max(0, input.costUsd);
|
|
61
|
+
}
|
|
62
|
+
if (input.pricingUnknown)
|
|
63
|
+
this.priceUnknown = true;
|
|
64
|
+
this.stopped = this.evaluate();
|
|
65
|
+
}
|
|
66
|
+
/** The ceiling that has been crossed, if any; `undefined` while work may continue. */
|
|
67
|
+
get exhausted() {
|
|
68
|
+
return this.evaluate();
|
|
69
|
+
}
|
|
70
|
+
snapshot() {
|
|
71
|
+
return {
|
|
72
|
+
limitTokens: this.limits.tokens,
|
|
73
|
+
spentTokens: this.spentTokens,
|
|
74
|
+
limitUsd: this.limits.usd,
|
|
75
|
+
spentUsd: Math.round(this.spentUsd * 10_000) / 10_000,
|
|
76
|
+
priceUnknown: this.priceUnknown,
|
|
77
|
+
...(this.stopped === undefined ? {} : { stop: this.stopped }),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** Both gates, in a fixed order, so one snapshot has one `stop` value. */
|
|
81
|
+
evaluate() {
|
|
82
|
+
if (this.limits.tokens > 0 && this.spentTokens >= this.limits.tokens)
|
|
83
|
+
return 'budget_tokens';
|
|
84
|
+
if (this.limits.usd > 0 && !this.priceUnknown && this.spentUsd >= this.limits.usd) {
|
|
85
|
+
return 'budget_usd';
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
}
|
package/dist/compaction/child.js
CHANGED
|
@@ -131,7 +131,12 @@ export function createChildContextManager(req, deps) {
|
|
|
131
131
|
},
|
|
132
132
|
// MECHANISM B - the instance bound (RV-2 / DH-16). The SAME bound the
|
|
133
133
|
// lead's own manager runs under (main-agent parity).
|
|
134
|
-
|
|
134
|
+
//
|
|
135
|
+
// `agentic: false` IS THE THIRD BOUND (v3 §4.5). A child has no card and no
|
|
136
|
+
// `notify`, so a multi-turn compaction inside it is spend nobody can watch
|
|
137
|
+
// and turns nobody can see; prune and one JSON call are enough for a
|
|
138
|
+
// background worker.
|
|
139
|
+
{ maxPerRun: COMPACTION_LIMITS.maxPerRun, agentic: false });
|
|
135
140
|
return {
|
|
136
141
|
// THERE IS NO `/compact` FOR A CHILD, so nothing is ever queued and the
|
|
137
142
|
// manual branch of `shouldCompact` is unreachable by construction.
|
|
@@ -112,6 +112,21 @@ export function formatCompactionStatus(ctx) {
|
|
|
112
112
|
`(${formatTokens(relief.charsRemoved)} chars) in ` +
|
|
113
113
|
`${relief.compactions} ${relief.compactions === 1 ? 'compaction' : 'compactions'}`);
|
|
114
114
|
}
|
|
115
|
+
// THE v3 SURFACES (W1 / W3 / W4). Rendered only when they have something to say,
|
|
116
|
+
// so a session that has never compacted reads exactly as it did before.
|
|
117
|
+
const prune = snapshot.prune;
|
|
118
|
+
if (prune && prune.actions > 0) {
|
|
119
|
+
lines.push(` Prune: ${prune.actions} bodies, ${formatTokens(prune.chars)} chars rewritten`);
|
|
120
|
+
}
|
|
121
|
+
const budget = snapshot.budget;
|
|
122
|
+
if (budget) {
|
|
123
|
+
lines.push(` Budget: ${formatBudgetLine(budget)}`);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
lines.push(` Budget: ${budgetHeadline(config.budgetTokens, config.budgetUsd)}`);
|
|
127
|
+
}
|
|
128
|
+
lines.push(` Agentic: ${config.agentic ? 'on' : 'off'}` +
|
|
129
|
+
(snapshot.agent ? ` (${agentLine(snapshot.agent)})` : ''));
|
|
115
130
|
const archives = archiveSummary(ctx);
|
|
116
131
|
if (archives)
|
|
117
132
|
lines.push(` Archive ${archives}`);
|
|
@@ -252,6 +267,16 @@ function formatCompactionShow(ctx, raw) {
|
|
|
252
267
|
lines.push(` Clipped: ${entry.tailRelief.messages} tool results in the retained turns ` +
|
|
253
268
|
`(${formatTokens(entry.tailRelief.charsRemoved)} chars)`);
|
|
254
269
|
}
|
|
270
|
+
// THE v3 SECTIONS, printed only when this archive has them (W1 / W3 / W4).
|
|
271
|
+
// A v1 archive has none of the three and renders exactly as it did before.
|
|
272
|
+
if (entry.prune) {
|
|
273
|
+
lines.push(` Pruned: ${entry.prune.actions.length} bodies `
|
|
274
|
+
+ `(${formatTokens(entry.prune.actions.reduce((sum, a) => sum + a.removedChars, 0))} chars)`);
|
|
275
|
+
}
|
|
276
|
+
if (entry.budget)
|
|
277
|
+
lines.push(` Budget: ${formatBudgetLine(entry.budget)}`);
|
|
278
|
+
if (entry.agent)
|
|
279
|
+
lines.push(` Agent: ${agentLine(entry.agent)}`);
|
|
255
280
|
lines.push(` File: ${entry.path}`);
|
|
256
281
|
if (entry.summary) {
|
|
257
282
|
lines.push('');
|
|
@@ -259,7 +284,99 @@ function formatCompactionShow(ctx, raw) {
|
|
|
259
284
|
}
|
|
260
285
|
return lines.join('\n');
|
|
261
286
|
}
|
|
262
|
-
|
|
287
|
+
/**
|
|
288
|
+
* `/compact budget ...` (W3): `budget <tokens>`, `budget usd <amount>`, `off`.
|
|
289
|
+
*
|
|
290
|
+
* RETURNS THE TOAST TEXT, OR `null` AFTER PRINTING ITS OWN REFUSAL. The applied
|
|
291
|
+
* value is reported, never the requested one: `clampCompactionConfig` may move a
|
|
292
|
+
* number, and reporting what was typed would make the clamp invisible.
|
|
293
|
+
*/
|
|
294
|
+
function applyBudgetVerb(ctx, raw) {
|
|
295
|
+
const controller = ctx.controller;
|
|
296
|
+
const words = raw.split(/\s+/).filter(Boolean);
|
|
297
|
+
const usd = (words[0] ?? '').toLowerCase() === 'usd';
|
|
298
|
+
const amount = (usd ? words[1] : words[0]) ?? '';
|
|
299
|
+
const off = amount === '' || /^(0|off|none|unlimited)$/i.test(amount);
|
|
300
|
+
const parsed = off ? 0 : Number(amount.replace(/[$,]/g, ''));
|
|
301
|
+
if (!Number.isFinite(parsed) || parsed < 0) {
|
|
302
|
+
ctx.notify('warn', 'Usage: /compact budget <tokens|off> | /compact budget usd <amount|off>');
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
const patch = usd
|
|
306
|
+
? { budgetUsd: parsed } : { budgetTokens: Math.round(parsed) };
|
|
307
|
+
const applied = controller.setCompactionConfig(patch);
|
|
308
|
+
ctx.persistConfig({ compaction: patch });
|
|
309
|
+
if (usd) {
|
|
310
|
+
return applied.budgetUsd > 0
|
|
311
|
+
? `Compaction budget: $${applied.budgetUsd.toFixed(2)} per operation.`
|
|
312
|
+
: 'Compaction USD budget: unlimited.';
|
|
313
|
+
}
|
|
314
|
+
return applied.budgetTokens > 0
|
|
315
|
+
? `Compaction budget: ${formatTokens(applied.budgetTokens)} tokens per operation.`
|
|
316
|
+
: 'Compaction token budget: unlimited.';
|
|
317
|
+
}
|
|
318
|
+
const USAGE = 'Usage: /compact [status | spend | budget [usd] <n|off> | agent on|off | prune on|off '
|
|
319
|
+
+ '| on | off | threshold <n> | keep <n> | history | show <n> | <instructions>]';
|
|
320
|
+
/** The `Budget:` value for a snapshot: spend against both ceilings (W3). */
|
|
321
|
+
function formatBudgetLine(budget) {
|
|
322
|
+
const tokens = budget.limitTokens > 0
|
|
323
|
+
? `${formatTokens(budget.spentTokens)} / ${formatTokens(budget.limitTokens)} tokens`
|
|
324
|
+
: `${formatTokens(budget.spentTokens)} tokens (no limit)`;
|
|
325
|
+
const usd = budget.limitUsd <= 0
|
|
326
|
+
? 'no USD limit'
|
|
327
|
+
: budget.priceUnknown
|
|
328
|
+
? `USD n/a (no price table for this model)`
|
|
329
|
+
: `${formatCost(budget.spentUsd)} / ${formatCost(budget.limitUsd)}`;
|
|
330
|
+
const stop = budget.stop === undefined ? '' : ` - stopped: ${budget.stop}`;
|
|
331
|
+
return `${tokens} - ${usd}${stop}`;
|
|
332
|
+
}
|
|
333
|
+
/** The `Budget:` value before the first compaction: what the NEXT one may spend. */
|
|
334
|
+
function budgetHeadline(tokens, usd) {
|
|
335
|
+
const parts = [
|
|
336
|
+
tokens > 0 ? `${formatTokens(tokens)} tokens per operation` : 'no token limit',
|
|
337
|
+
usd > 0 ? `${formatCost(usd)} per operation` : 'no USD limit',
|
|
338
|
+
];
|
|
339
|
+
return `${parts.join(' - ')} (nothing spent yet)`;
|
|
340
|
+
}
|
|
341
|
+
/** `4 turns, 9 tool calls` for the `Agentic:` row (W4). */
|
|
342
|
+
function agentLine(agent) {
|
|
343
|
+
const parts = [
|
|
344
|
+
`${agent.turns} ${agent.turns === 1 ? 'turn' : 'turns'}`,
|
|
345
|
+
`${agent.toolCalls} tool ${agent.toolCalls === 1 ? 'call' : 'calls'}`,
|
|
346
|
+
`${agent.submissions} ${agent.submissions === 1 ? 'submission' : 'submissions'}`,
|
|
347
|
+
];
|
|
348
|
+
return `last: ${parts.join(', ')}`;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* `/compact spend` - the session's cumulative spend and the last operation's ledger.
|
|
352
|
+
*
|
|
353
|
+
* TWO DIFFERENT QUESTIONS, and answering only the first is what made a budget
|
|
354
|
+
* feel like a black box: "what has compaction cost this session" (a total) and
|
|
355
|
+
* "what did the last one cost against its ceiling" (a snapshot).
|
|
356
|
+
*/
|
|
357
|
+
function formatCompactionSpend(ctx) {
|
|
358
|
+
const snapshot = ctx.controller.getCompactionSnapshot();
|
|
359
|
+
const config = ctx.controller.getCompactionConfig();
|
|
360
|
+
const pricing = snapshot.pricingUnknown
|
|
361
|
+
? `unknown (no price table for ${snapshot.model || 'the summarizer'})`
|
|
362
|
+
: formatCost(compactionCost(ctx));
|
|
363
|
+
const lines = [
|
|
364
|
+
'compaction spend (this session)',
|
|
365
|
+
` Tokens: ${formatTokens(snapshot.usage.inputTokens)} in / `
|
|
366
|
+
+ `${formatTokens(snapshot.usage.outputTokens)} out`,
|
|
367
|
+
` Cost: ${pricing}`,
|
|
368
|
+
` Calls: ${snapshot.compactions} ${snapshot.compactions === 1 ? 'attempt' : 'attempts'}`,
|
|
369
|
+
` Limits: ${budgetHeadline(config.budgetTokens, config.budgetUsd)}`,
|
|
370
|
+
];
|
|
371
|
+
if (snapshot.budget) {
|
|
372
|
+
lines.push(` Last: ${formatBudgetLine(snapshot.budget)}`);
|
|
373
|
+
}
|
|
374
|
+
if (snapshot.prune && snapshot.prune.actions > 0) {
|
|
375
|
+
lines.push(` Pruned: ${snapshot.prune.actions} bodies, `
|
|
376
|
+
+ `${formatTokens(snapshot.prune.chars)} chars (free)`);
|
|
377
|
+
}
|
|
378
|
+
return lines.join('\n');
|
|
379
|
+
}
|
|
263
380
|
/**
|
|
264
381
|
* The `/compact` handler.
|
|
265
382
|
*
|
|
@@ -324,6 +441,35 @@ export async function runCompactCommand(ctx) {
|
|
|
324
441
|
ctx.toast('success', `Keeping ${applied.keepRecentTurns} recent turns verbatim.`);
|
|
325
442
|
return;
|
|
326
443
|
}
|
|
444
|
+
// --- THE v3 VERBS, ALL BEFORE THE FREE-TEXT FALL-THROUGH (P1-2) -----------
|
|
445
|
+
// `/compact spend` used to reach the fall-through, which took the word "spend"
|
|
446
|
+
// as summarizer instructions and compacted the conversation. Every new verb is
|
|
447
|
+
// therefore handled here, above `// --- Compact now`.
|
|
448
|
+
if (verb === 'spend') {
|
|
449
|
+
ctx.notify('info', formatCompactionSpend(ctx));
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (verb === 'budget') {
|
|
453
|
+
const applied = applyBudgetVerb(ctx, arg.slice((rawVerb ?? '').length).trim());
|
|
454
|
+
if (applied !== null)
|
|
455
|
+
ctx.toast('success', applied);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (verb === 'agent' || verb === 'prune') {
|
|
459
|
+
const on = value === 'on' || value === 'true' || value === '1';
|
|
460
|
+
const off = value === 'off' || value === 'false' || value === '0';
|
|
461
|
+
if (!on && !off) {
|
|
462
|
+
ctx.notify('warn', `Usage: /compact ${verb} on|off`);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const patch = verb === 'agent' ? { agentic: on } : { prune: on };
|
|
466
|
+
const applied = controller.setCompactionConfig(patch);
|
|
467
|
+
ctx.persistConfig({ compaction: patch });
|
|
468
|
+
ctx.toast('success', verb === 'agent'
|
|
469
|
+
? `Multi-turn compaction ${applied.agentic ? 'on' : 'off'}.`
|
|
470
|
+
: `Redundancy pruning ${applied.prune ? 'on' : 'off'}.`);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
327
473
|
// BEFORE THE FREE-TEXT FALL-THROUGH, so a user cannot accidentally summarize
|
|
328
474
|
// with the word "history".
|
|
329
475
|
if (verb === 'history') {
|
|
@@ -5,8 +5,10 @@ import type { CliConfig } from '@aragon-agent/tui/model/config/schema';
|
|
|
5
5
|
import type { NoticeLevel } from '@aragon-agent/tui/model/agent/reducer';
|
|
6
6
|
import { resolveFastTier } from '@aragon-agent/tui/model/fast/resolve';
|
|
7
7
|
import { ContextMeter } from '@aragon-agent/tui/model/compaction/meter';
|
|
8
|
+
import type { AgentTurn, AgentTurnRequest } from './memory-agent.js';
|
|
9
|
+
import { type SummarizerChoice } from './summarize-ladder.js';
|
|
8
10
|
import { type CompactionIdentity } from './memory-identity.js';
|
|
9
|
-
import type { CompactionEvent, Pressure } from '@aragon-agent/tui/model/compaction/types';
|
|
11
|
+
import type { ArchivePruneAction, CompactionEvent, Pressure } from '@aragon-agent/tui/model/compaction/types';
|
|
10
12
|
export interface CompactorDeps {
|
|
11
13
|
mainRole?: ModelRole;
|
|
12
14
|
resolveFastCandidate?: () => ReturnType<typeof resolveFastTier>;
|
|
@@ -36,6 +38,17 @@ export interface CompactorDeps {
|
|
|
36
38
|
isPricedModel: (ref: Pick<ModelRef, 'providerId' | 'modelId'>) => boolean;
|
|
37
39
|
/** The compactor's OWN fail-fast transport (D-10). Supplied by the wiring. */
|
|
38
40
|
complete: (providerId: string, request: LLMRequest) => Promise<AssistantMessage>;
|
|
41
|
+
/**
|
|
42
|
+
* One agent turn for the multi-turn compaction agent (W4 / P1-6).
|
|
43
|
+
*
|
|
44
|
+
* OPTIONAL, AND ABSENT MEANS `agentic` CANNOT RUN. The wiring supplies it as
|
|
45
|
+
* `registry.stream` + `consumeStream`; a test that scripts the transport can
|
|
46
|
+
* supply its own, which is what makes AC-9..AC-12 possible without a network.
|
|
47
|
+
* When it is absent the compactor falls back to the single-shot JSON call
|
|
48
|
+
* (D-14) even with `compaction.agentic` on - a degraded path that still
|
|
49
|
+
* compacts, disclosed on the record as `agent.fallback: 'single-shot'`.
|
|
50
|
+
*/
|
|
51
|
+
runAgentTurn?: (request: AgentTurnRequest) => Promise<AgentTurn>;
|
|
39
52
|
emit: (event: CompactionEvent) => void;
|
|
40
53
|
notify: (level: NoticeLevel, text: string) => void;
|
|
41
54
|
/**
|
|
@@ -79,14 +92,25 @@ export interface CompactorOptions {
|
|
|
79
92
|
* value, so every existing construction site is unchanged.
|
|
80
93
|
*/
|
|
81
94
|
maxPerRun?: number;
|
|
95
|
+
/**
|
|
96
|
+
* Force the multi-turn agent OFF for this compactor (v3 §4.5).
|
|
97
|
+
*
|
|
98
|
+
* A CHILD HAS NO CARD (its `notify` is a no-op and it emits one log line), so
|
|
99
|
+
* the turns and tool calls of a multi-turn compaction are cost with nobody to
|
|
100
|
+
* watch it - while the bill joins the parent's session. `false` here keeps
|
|
101
|
+
* `compaction.subagents` meaning "does the child compact at all" rather than
|
|
102
|
+
* "how large is the child's bill". Prune and the single-shot JSON call remain,
|
|
103
|
+
* which is everything a background worker needs.
|
|
104
|
+
*/
|
|
105
|
+
agentic?: boolean;
|
|
82
106
|
}
|
|
83
|
-
/**
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
107
|
+
/**
|
|
108
|
+
* The summarizer the next compaction would use.
|
|
109
|
+
*
|
|
110
|
+
* DECLARED IN THE LADDER because that is where it is consumed, and re-exported
|
|
111
|
+
* here because `resolveSummarizer()` and the retry ladder's callers name it.
|
|
112
|
+
*/
|
|
113
|
+
export type { SummarizerChoice };
|
|
90
114
|
export declare class Compactor implements ContextManager {
|
|
91
115
|
private readonly deps;
|
|
92
116
|
private readonly log;
|
|
@@ -101,6 +125,11 @@ export declare class Compactor implements ContextManager {
|
|
|
101
125
|
private costUsd;
|
|
102
126
|
private consecutiveNoProgress;
|
|
103
127
|
private consecutiveTransportFailures;
|
|
128
|
+
private consecutiveSemanticFailures;
|
|
129
|
+
private prunedActions;
|
|
130
|
+
private prunedChars;
|
|
131
|
+
/** The operation the engine has most recently settled, for `/compact status`. */
|
|
132
|
+
private settled;
|
|
104
133
|
private selfDisabled;
|
|
105
134
|
private selfDisabledReason;
|
|
106
135
|
private pendingManual;
|
|
@@ -113,6 +142,10 @@ export declare class Compactor implements ContextManager {
|
|
|
113
142
|
private identity;
|
|
114
143
|
private legacyFailureNotified;
|
|
115
144
|
private readonly maxPerRun;
|
|
145
|
+
/** `undefined` = follow the config; `false` = this instance never runs the agent. */
|
|
146
|
+
private readonly agenticOverride;
|
|
147
|
+
/** The retry ladder, built on first use (see `get ladder`). */
|
|
148
|
+
private ladderCache;
|
|
116
149
|
constructor(deps: CompactorDeps, opts?: CompactorOptions);
|
|
117
150
|
onRunStart(): void;
|
|
118
151
|
onRunEnd(): void;
|
|
@@ -127,19 +160,52 @@ export declare class Compactor implements ContextManager {
|
|
|
127
160
|
shouldCompact(probe: CompactionProbe): boolean;
|
|
128
161
|
compact(ctx: CompactionContext): Promise<CompactionOutcome>;
|
|
129
162
|
private prepareCandidate;
|
|
163
|
+
/**
|
|
164
|
+
* Stage 0: rewrite redundant bodies in the band about to be replaced (W1).
|
|
165
|
+
*
|
|
166
|
+
* ZERO MODEL COST, AND ZERO EVENTS WHEN IT FINDS NOTHING: the phase is emitted
|
|
167
|
+
* only when at least one body actually changed, so a session with
|
|
168
|
+
* `compaction.prune` off - or one with nothing redundant - produces the same
|
|
169
|
+
* stream it produced before this feature existed.
|
|
170
|
+
*/
|
|
171
|
+
private pruneStage;
|
|
172
|
+
/** The prior memory this history carries, when its identity still verifies. */
|
|
173
|
+
private priorMemory;
|
|
174
|
+
/**
|
|
175
|
+
* The free win: when pruning alone already clears the threshold, do not
|
|
176
|
+
* summarize at all.
|
|
177
|
+
*
|
|
178
|
+
* IT MUST STILL SATISFY `checkCandidate`, which is why the candidate goes
|
|
179
|
+
* through the same gate the summarized one does - a prune-only splice that
|
|
180
|
+
* reclaims nothing (`no_token_reclaim`) or that leaves the history above the
|
|
181
|
+
* window (`protected_memory_too_large`) is not a win, and falling through to
|
|
182
|
+
* the summary is the honest answer.
|
|
183
|
+
*/
|
|
184
|
+
private pruneOnlyCandidate;
|
|
130
185
|
private selectInput;
|
|
131
186
|
private checkMinimumCandidate;
|
|
187
|
+
/**
|
|
188
|
+
* The digest for whichever path will send it, delegated to the ladder.
|
|
189
|
+
*
|
|
190
|
+
* the code that picks the path: `summarize-ladder.ts` owns the ask, the classify
|
|
191
|
+
* and the ask-again loop, and this file keeps the ORDER of the stages. Two
|
|
192
|
+
* ceilings act on that loop (the attempt count and the operation ledger) and
|
|
193
|
+
* both are checked there, before a request is sent.
|
|
194
|
+
*/
|
|
132
195
|
private digestFor;
|
|
133
|
-
|
|
196
|
+
/** Whether the multi-turn path is both configured and wired. */
|
|
197
|
+
private agentic;
|
|
134
198
|
/**
|
|
135
|
-
*
|
|
199
|
+
* The retry ladder, built on first use out of this compactor's own primitives.
|
|
136
200
|
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* endpoint: against the same one the switch is a no-op that also rebuilds the digest,
|
|
140
|
-
* and the retry should simply be the same request again.
|
|
201
|
+
* and a class field initializer runs before the constructor's parameter
|
|
202
|
+
* session where `compaction.enabled` is false, which never needs it.
|
|
141
203
|
*/
|
|
142
|
-
private
|
|
204
|
+
private get ladder();
|
|
205
|
+
/** The one agent-turn port, refused rather than asserted when it is absent. */
|
|
206
|
+
private runAgentTurn;
|
|
207
|
+
/** The retry ladder (W2): its table, its attempts and its ceilings are the ladder's. */
|
|
208
|
+
private summarize;
|
|
143
209
|
private callOnce;
|
|
144
210
|
private checkCandidate;
|
|
145
211
|
private isCurrent;
|
|
@@ -169,6 +235,8 @@ export declare class Compactor implements ContextManager {
|
|
|
169
235
|
*/
|
|
170
236
|
private relaxedFields;
|
|
171
237
|
private finishRecord;
|
|
238
|
+
/** The prune tally in record shape, with the per-kind counters the card shows. */
|
|
239
|
+
private pruneRecord;
|
|
172
240
|
/** Commit only the engine/host verdict; candidates have no authority before this point. */
|
|
173
241
|
settleOperation(verdict: {
|
|
174
242
|
applied: boolean;
|
|
@@ -190,6 +258,15 @@ export declare class Compactor implements ContextManager {
|
|
|
190
258
|
* summarizer, which proves the path works, so the transport streak ends.
|
|
191
259
|
*/
|
|
192
260
|
private noteFailure;
|
|
261
|
+
/**
|
|
262
|
+
* The third counter (W2): "the model answered, and we could not use the answer".
|
|
263
|
+
*
|
|
264
|
+
* SEPARATE FROM `stuckLimit` ON PURPOSE. Sharing one counter meant two unusable
|
|
265
|
+
* deltas - the common shape of a bad summarizer on a long band - paused
|
|
266
|
+
* auto-compaction for the rest of the run, even though every one of those
|
|
267
|
+
* attempts proved the transport, the key and the model all worked.
|
|
268
|
+
*/
|
|
269
|
+
private noteSemanticFailure;
|
|
193
270
|
private noteTransportFailure;
|
|
194
271
|
private noteNoProgress;
|
|
195
272
|
private selfDisable;
|
|
@@ -197,9 +274,39 @@ export declare class Compactor implements ContextManager {
|
|
|
197
274
|
messages?: readonly Message[];
|
|
198
275
|
systemPrompt?: string;
|
|
199
276
|
}): Pressure;
|
|
277
|
+
/**
|
|
278
|
+
* Fold one applied operation's pruning into the session totals (W1).
|
|
279
|
+
*
|
|
280
|
+
* COUNTED ONLY WHEN THE SPLICE LANDED, for the same reason `tokensReclaimed`
|
|
281
|
+
* is: a candidate the engine refused reclaimed nothing, so reporting its saving
|
|
282
|
+
* would be a number the history never benefited from.
|
|
283
|
+
*/
|
|
284
|
+
private applyPruneTotals;
|
|
285
|
+
/** The last operation's budget snapshot and agent effort, for `/compact status`. */
|
|
286
|
+
private lastSettled;
|
|
287
|
+
/**
|
|
288
|
+
* The settled operation's prune detail, for the archive (W1).
|
|
289
|
+
*
|
|
290
|
+
* THE RECORD CARRIES COUNTS ONLY, because it is dispatched into the reducer and
|
|
291
|
+
* retained in view state; the per-action detail (`messageIndex`, `sourceId`,
|
|
292
|
+
* `removedChars`) is what makes an audit of a pruned body possible and belongs
|
|
293
|
+
* on disk, not in React state (RV-3's rule for the dropped history).
|
|
294
|
+
*/
|
|
295
|
+
lastPruneActions(): ArchivePruneAction[];
|
|
200
296
|
lastMeasured(): Pressure | null;
|
|
201
297
|
recordApplied(tokensBefore: number, tokensAfter: number): void;
|
|
202
298
|
sessionTotals(): {
|
|
299
|
+
agent?: {
|
|
300
|
+
turns: number;
|
|
301
|
+
toolCalls: number;
|
|
302
|
+
submissions: number;
|
|
303
|
+
fallback?: "single-shot";
|
|
304
|
+
} | undefined;
|
|
305
|
+
budget?: import("./budget.js").BudgetSnapshot | undefined;
|
|
306
|
+
prune: {
|
|
307
|
+
actions: number;
|
|
308
|
+
chars: number;
|
|
309
|
+
};
|
|
203
310
|
selfDisabledReason?: string | undefined;
|
|
204
311
|
compactions: number;
|
|
205
312
|
applied: number;
|
|
@@ -219,5 +326,15 @@ export declare class Compactor implements ContextManager {
|
|
|
219
326
|
private meter;
|
|
220
327
|
private mainRef;
|
|
221
328
|
private settingsKey;
|
|
329
|
+
/**
|
|
330
|
+
* The ONE accounting entry for spend (P1-5).
|
|
331
|
+
*
|
|
332
|
+
* `onUsage` is the only callback every real call funnels through - the agent's
|
|
333
|
+
* turns, the single-shot fallback and core's own `withRetry` attempts - so the
|
|
334
|
+
* BUDGET IS CHARGED HERE and nowhere else. Putting the charge inside
|
|
335
|
+
* `runMemoryAgent` instead would have left the D-14 fallback un-metered, which
|
|
336
|
+
* would have made "default unlimited, adjustable" a promise that only held for
|
|
337
|
+
* models that support tools.
|
|
338
|
+
*/
|
|
222
339
|
private noteUsage;
|
|
223
340
|
}
|