@aragon-agent/cli 0.6.62 → 0.6.63
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/README.md +99 -7
- package/dist/agent/headless.js +18 -0
- 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 +1 -1
- package/dist/config/cli-commands.js +16 -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/runner.js +6 -0
- package/dist/logging/install.d.ts +27 -0
- package/dist/session/validate-session.js +11 -0
- package/package.json +3 -3
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { COMPACTION_LIMITS as LIMITS } from '@aragon-agent/tui/model/compaction/limits';
|
|
2
|
+
import { estimatePromptTokens } from '@aragon-agent/core';
|
|
3
|
+
import { mergeMemory, parseMemoryDelta } from './memory.js';
|
|
4
|
+
import { validateMemoryDelta } from './memory-validation.js';
|
|
5
|
+
import { PRIMARY_TOOLS, SECONDARY_TOOLS, executeTool, newToolState, toolSchemas, toolCallsOf, } from './memory-agent-tools.js';
|
|
6
|
+
import { buildMemoryAgentOpening, buildMemoryAgentSystemPrompt, memoryAgentMaxChars, } from './memory-agent-prompt.js';
|
|
7
|
+
/** The structural ceiling on the whole loop for this operation. */
|
|
8
|
+
export function agentDeadlineOf(deps) {
|
|
9
|
+
return Math.min(deps.operation.deadline, deps.now() + LIMITS.agentDeadlineMs);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The first request's projected size (P1-4).
|
|
13
|
+
*
|
|
14
|
+
* `digestFor` projects the WHOLE digest for the single-shot path; this path
|
|
15
|
+
* sends an opening of three lines plus the outline phrasebook, so measuring the
|
|
16
|
+
* digest here would refuse a request the agent never makes - and the window
|
|
17
|
+
* headroom the multi-turn design buys would be spent on a phantom.
|
|
18
|
+
*/
|
|
19
|
+
export function projectAgentOpeningTokens(input) {
|
|
20
|
+
const systemPrompt = buildMemoryAgentSystemPrompt({
|
|
21
|
+
maxChars: memoryAgentMaxChars(),
|
|
22
|
+
hasPrior: !!input.band.prior,
|
|
23
|
+
...(input.instructions ? { instructions: input.instructions } : {}),
|
|
24
|
+
...(input.repairHint ? { repairHint: input.repairHint } : {}),
|
|
25
|
+
});
|
|
26
|
+
const opening = {
|
|
27
|
+
role: 'user', content: buildMemoryAgentOpening(input.band, input.digest),
|
|
28
|
+
};
|
|
29
|
+
// The band map is the LARGEST thing this path can send on its own, so the
|
|
30
|
+
// projection includes one full outline allowance.
|
|
31
|
+
return estimatePromptTokens([opening], systemPrompt)
|
|
32
|
+
+ Math.ceil(LIMITS.outlineMaxLines * 100 / 4);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Fail with the most specific reason the run knows.
|
|
36
|
+
*
|
|
37
|
+
* `turn_limit` IS TRANSLATED WHEN NOTHING WAS EVER SUBMITTED (AC-11). "It used
|
|
38
|
+
* every turn" and "it never submitted" are the same event seen from two sides,
|
|
39
|
+
* and the actionable one is the second: a submission that keeps being rejected
|
|
40
|
+
* is a different repair from an agent that kept reading and forgot to finish.
|
|
41
|
+
*/
|
|
42
|
+
function fail(state, reason, verdict) {
|
|
43
|
+
const reported = reason === 'turn_limit' && state.tools.submissions === 0
|
|
44
|
+
? 'agent_incomplete' : reason;
|
|
45
|
+
return {
|
|
46
|
+
ok: false, reason: reported, turns: state.turns, toolCalls: state.toolCalls,
|
|
47
|
+
...(verdict ? { lastVerdict: verdict } : state.tools.lastVerdict
|
|
48
|
+
? { lastVerdict: state.tools.lastVerdict } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Succeed from whatever `compaction_submit` accepted. */
|
|
52
|
+
function adopt(state, delta) {
|
|
53
|
+
return {
|
|
54
|
+
ok: true, delta, turns: state.turns, toolCalls: state.toolCalls,
|
|
55
|
+
submissions: state.tools.submissions,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The single-shot fallback (D-14): a model with no tool support answers with the
|
|
60
|
+
* JSON body directly. This is the pre-v3 path, kept as the ladder's last rung.
|
|
61
|
+
*/
|
|
62
|
+
function trySingleShot(state, text) {
|
|
63
|
+
if (!text.trim())
|
|
64
|
+
return null;
|
|
65
|
+
try {
|
|
66
|
+
const delta = parseMemoryDelta(text);
|
|
67
|
+
validateMemoryDelta(delta, {
|
|
68
|
+
sourceMap: state.input.digest.sourceMap,
|
|
69
|
+
...(state.input.band.prior ? { prior: state.input.band.prior } : {}),
|
|
70
|
+
hasSummarizableMessages: state.input.digest.coverage.summarizedMessages > 0,
|
|
71
|
+
});
|
|
72
|
+
return adopt(state, delta);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
76
|
+
return fail(state, `invalid_memory_delta: ${message.slice(0, 200)}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Whether another turn may start; the reason string when it may not. */
|
|
80
|
+
function blocked(state) {
|
|
81
|
+
if (state.deps.operation.signal.aborted)
|
|
82
|
+
return 'aborted';
|
|
83
|
+
if (state.deps.now() >= state.deadline)
|
|
84
|
+
return 'agent_timeout';
|
|
85
|
+
if (state.deps.budget.exhausted !== undefined)
|
|
86
|
+
return 'budget_exhausted';
|
|
87
|
+
if (state.turns >= LIMITS.agentMaxTurns)
|
|
88
|
+
return 'turn_limit';
|
|
89
|
+
if (state.toolCalls >= LIMITS.agentMaxToolCalls)
|
|
90
|
+
return 'turn_limit';
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
/** Append the assistant turn and its tool results to the running history. */
|
|
94
|
+
function appendTurn(state, turn, calls, results) {
|
|
95
|
+
state.messages.push({
|
|
96
|
+
role: 'assistant',
|
|
97
|
+
content: [
|
|
98
|
+
...(turn.text ? [{ type: 'text', text: turn.text }] : []),
|
|
99
|
+
...calls.map((call) => ({
|
|
100
|
+
type: 'tool_call',
|
|
101
|
+
toolCallId: call.toolCallId,
|
|
102
|
+
toolName: call.toolName,
|
|
103
|
+
args: call.args,
|
|
104
|
+
})),
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
for (const result of results) {
|
|
108
|
+
state.messages.push({
|
|
109
|
+
role: 'tool_result',
|
|
110
|
+
toolCallId: result.toolCallId,
|
|
111
|
+
content: result.content,
|
|
112
|
+
...(result.isError ? { isError: true } : {}),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Execute the calls of one turn, serially, bounded by `toolCallsPerTurn`. */
|
|
117
|
+
function executeTools(state, turn, calls) {
|
|
118
|
+
const allowance = Math.min(LIMITS.toolCallsPerTurn, Math.max(0, LIMITS.agentMaxToolCalls - state.toolCalls));
|
|
119
|
+
const executed = [];
|
|
120
|
+
const results = [];
|
|
121
|
+
for (const call of calls.slice(0, allowance)) {
|
|
122
|
+
const outcome = executeTool(call.toolName, call.args, {
|
|
123
|
+
input: state.input.band,
|
|
124
|
+
digest: state.input.digest,
|
|
125
|
+
state: state.tools,
|
|
126
|
+
submitBudget: LIMITS.submitAttempts,
|
|
127
|
+
});
|
|
128
|
+
state.toolCalls += 1;
|
|
129
|
+
executed.push(call);
|
|
130
|
+
results.push({
|
|
131
|
+
toolCallId: call.toolCallId, content: outcome.content, isError: outcome.isError,
|
|
132
|
+
});
|
|
133
|
+
if (outcome.unlock)
|
|
134
|
+
state.visible = [...PRIMARY_TOOLS, ...SECONDARY_TOOLS];
|
|
135
|
+
if (state.tools.accepted)
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
// ONLY THE EXECUTED CALLS REACH THE HISTORY. An assistant message that named
|
|
139
|
+
// four calls but carried two tool results is a malformed history, and the
|
|
140
|
+
// transport would reject the next request for a reason that has nothing to do
|
|
141
|
+
// with this compaction.
|
|
142
|
+
appendTurn(state, turn, executed, results);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* One iteration: request, classify the answer, run its tools.
|
|
146
|
+
*
|
|
147
|
+
* RETURNS EITHER A FINAL RESULT OR `null`, and `null` means "another turn may
|
|
148
|
+
* run" - the whole loop condition lives in `blocked()` so no branch here can
|
|
149
|
+
* invent a sixth exit.
|
|
150
|
+
*/
|
|
151
|
+
async function nextTurn(state) {
|
|
152
|
+
const turn = await state.port.runTurn({
|
|
153
|
+
systemPrompt: state.systemPrompt,
|
|
154
|
+
messages: [...state.messages],
|
|
155
|
+
tools: toolSchemas(state.visible),
|
|
156
|
+
maxTokens: LIMITS.agentTurnOutputTokens,
|
|
157
|
+
ref: state.input.choice.ref,
|
|
158
|
+
role: state.input.choice.role,
|
|
159
|
+
signal: state.deps.operation.signal,
|
|
160
|
+
});
|
|
161
|
+
state.turns += 1;
|
|
162
|
+
state.deps.onUsage(turn.usage);
|
|
163
|
+
const calls = toolCallsOf({
|
|
164
|
+
role: 'assistant',
|
|
165
|
+
content: [
|
|
166
|
+
...(turn.text ? [{ type: 'text', text: turn.text }] : []),
|
|
167
|
+
...turn.toolCalls.map((call) => ({
|
|
168
|
+
type: 'tool_call',
|
|
169
|
+
toolCallId: call.toolCallId,
|
|
170
|
+
toolName: call.toolName,
|
|
171
|
+
args: call.args,
|
|
172
|
+
})),
|
|
173
|
+
],
|
|
174
|
+
});
|
|
175
|
+
if (calls.length === 0)
|
|
176
|
+
return trySingleShot(state, turn.text);
|
|
177
|
+
executeTools(state, turn, calls);
|
|
178
|
+
if (state.tools.accepted)
|
|
179
|
+
return adopt(state, state.tools.accepted);
|
|
180
|
+
// A budget or a deadline crossed during the tool batch ends the run BEFORE a
|
|
181
|
+
// further request: the point of the gate is to not spend, not to notice late.
|
|
182
|
+
const reason = blocked(state);
|
|
183
|
+
return reason === null ? null : fail(state, reason);
|
|
184
|
+
}
|
|
185
|
+
/** Run one compaction agent to completion, adoption or a named stop. */
|
|
186
|
+
export async function runMemoryAgent(input, port, deps) {
|
|
187
|
+
const state = {
|
|
188
|
+
input,
|
|
189
|
+
deps,
|
|
190
|
+
port,
|
|
191
|
+
systemPrompt: buildMemoryAgentSystemPrompt({
|
|
192
|
+
maxChars: memoryAgentMaxChars(),
|
|
193
|
+
hasPrior: !!input.band.prior,
|
|
194
|
+
...(input.instructions ? { instructions: input.instructions } : {}),
|
|
195
|
+
...(input.repairHint ? { repairHint: input.repairHint } : {}),
|
|
196
|
+
}),
|
|
197
|
+
tools: newToolState(),
|
|
198
|
+
visible: [...PRIMARY_TOOLS],
|
|
199
|
+
messages: [{
|
|
200
|
+
role: 'user', content: buildMemoryAgentOpening(input.band, input.digest),
|
|
201
|
+
}],
|
|
202
|
+
turns: 0,
|
|
203
|
+
toolCalls: 0,
|
|
204
|
+
deadline: agentDeadlineOf(deps),
|
|
205
|
+
};
|
|
206
|
+
for (;;) {
|
|
207
|
+
const blockedNow = blocked(state);
|
|
208
|
+
if (blockedNow !== null)
|
|
209
|
+
return fail(state, blockedNow);
|
|
210
|
+
const outcome = await nextTurn(state);
|
|
211
|
+
if (outcome !== null)
|
|
212
|
+
return outcome;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** Rebuild the memory from an adopted delta, for the compactor's applying step. */
|
|
216
|
+
export function memoryFromDelta(band, digest, delta) {
|
|
217
|
+
return mergeMemory({
|
|
218
|
+
input: band, delta, sourceMap: digest.sourceMap, coverage: digest.coverage,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The redundancy pass - stage 0 of a compaction (context-compaction-v3 W1).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY: `src/compaction/**` is inside the glyph scanner's scope.
|
|
5
|
+
*
|
|
6
|
+
* WHAT THIS IS FOR. A long autonomous run is mostly REPEATED tool output: the
|
|
7
|
+
* same `bash` command re-run three times, one file read five times, an `npm
|
|
8
|
+
* test` log that appears twice. None of that needs a model to identify, and all
|
|
9
|
+
* of it is what pushes a digest past the summarizer's window - the most common
|
|
10
|
+
* reason a compaction declines. Rewriting those bodies costs no tokens of its
|
|
11
|
+
* own and can make the summary unnecessary altogether.
|
|
12
|
+
*
|
|
13
|
+
* PURE AND TOTAL. No I/O, no clock, no configuration read: every input arrives
|
|
14
|
+
* in `PruneInput`, so the pass is testable against a real session fixture and
|
|
15
|
+
* its output is byte-stable for byte-stable input.
|
|
16
|
+
*
|
|
17
|
+
* IT NEVER DELETES A MESSAGE (D-2). `planPrune` returns a same-length copy in
|
|
18
|
+
* which some BODIES are replaced by a fixed ASCII placeholder; `role`,
|
|
19
|
+
* `toolCallId` and `isError` are untouched, so `validateHistory`'s
|
|
20
|
+
* `tool_call` <-> `tool_result` pairing still holds.
|
|
21
|
+
*
|
|
22
|
+
* THE FIRST AND THE LAST COPY OF ANY REPEATED BODY ALWAYS SURVIVE. That is the
|
|
23
|
+
* rule that makes a stub honest: the earliest copy shows what the run actually
|
|
24
|
+
* produced, the latest shows what is still true, and only the copies in between
|
|
25
|
+
* - which say the same thing again - are replaced.
|
|
26
|
+
*/
|
|
27
|
+
import { type Message } from '@aragon-agent/core';
|
|
28
|
+
/** Why one body was rewritten. Every kind has a test and a card counter. */
|
|
29
|
+
export type PruneKind =
|
|
30
|
+
/** Two `tool_result` bodies normalise to the same text. */
|
|
31
|
+
'duplicate-tool-result'
|
|
32
|
+
/** Two consecutive assistant bodies that repeat one streaming residue. */
|
|
33
|
+
| 'duplicate-assistant-text'
|
|
34
|
+
/** Same tool, same arguments, re-run: the later copy wins. */
|
|
35
|
+
| 'repeat-command-output'
|
|
36
|
+
/** Same file read again later, so the earlier body is superseded. */
|
|
37
|
+
| 'superseded-file-read'
|
|
38
|
+
/** Nothing but whitespace. */
|
|
39
|
+
| 'empty-tool-result'
|
|
40
|
+
/** Only lines matched by `NOISE_PATTERNS`. */
|
|
41
|
+
| 'noise-only-body'
|
|
42
|
+
/** Longer than `pruneBodyChars`, so head and tail are kept and the rest goes. */
|
|
43
|
+
| 'oversized-body';
|
|
44
|
+
export interface PruneAction {
|
|
45
|
+
kind: PruneKind;
|
|
46
|
+
messageIndex: number;
|
|
47
|
+
/** `g{gen}:m{index}`, the id rule the digest and the memory sources already use. */
|
|
48
|
+
sourceId: string;
|
|
49
|
+
/** The copy that was kept, for the deduplicating kinds. */
|
|
50
|
+
keptIndex?: number;
|
|
51
|
+
removedChars: number;
|
|
52
|
+
/** True when the body was clipped rather than replaced wholesale. */
|
|
53
|
+
clipped: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface PruneInput {
|
|
56
|
+
messages: readonly Message[];
|
|
57
|
+
/** `countProtectedPrefix()`: the anchor and the prior memory block. */
|
|
58
|
+
protectedPrefix: number;
|
|
59
|
+
/**
|
|
60
|
+
* = `planCompaction(...).cutIndex` (the v2 fix for P0-1).
|
|
61
|
+
*
|
|
62
|
+
* ONLY THE HEAD IS PRUNABLE. Messages at or after this index are the tail the
|
|
63
|
+
* run continues FROM, and rewriting them would change what the model is about
|
|
64
|
+
* to read rather than what it is about to lose.
|
|
65
|
+
*/
|
|
66
|
+
tailStart: number;
|
|
67
|
+
generation: number;
|
|
68
|
+
/**
|
|
69
|
+
* Message ids the EXISTING memory quotes (P1-3 / AC-20).
|
|
70
|
+
*
|
|
71
|
+
* NOT AN OPTIMISATION. A prune-only compaction keeps its placeholders in the
|
|
72
|
+
* history, and the next compaction's `prior` still cites those messages by an
|
|
73
|
+
* excerpt of their ORIGINAL text. `validateSource` requires an excerpt to be a
|
|
74
|
+
* substring of the message's CURRENT visible text, so rewriting a cited body
|
|
75
|
+
* guarantees `invalid_memory_delta` on every later submission - with no way
|
|
76
|
+
* for the model to see why.
|
|
77
|
+
*/
|
|
78
|
+
citedIds: ReadonlySet<string>;
|
|
79
|
+
}
|
|
80
|
+
export interface PrunePlan {
|
|
81
|
+
/** Same length and same roles as the input; only bodies differ. */
|
|
82
|
+
messages: Message[];
|
|
83
|
+
actions: PruneAction[];
|
|
84
|
+
removedChars: number;
|
|
85
|
+
/** `estimatePromptTokens` before minus after. */
|
|
86
|
+
removedTokens: number;
|
|
87
|
+
/** How many messages the pass looked at. */
|
|
88
|
+
scanned: number;
|
|
89
|
+
/** Messages skipped because the existing memory quotes them. */
|
|
90
|
+
skippedCited: number;
|
|
91
|
+
}
|
|
92
|
+
/** The placeholder for a replaced body. ASCII only, one line, fixed field order. */
|
|
93
|
+
export declare function pruneStub(kind: PruneKind, originalChars: number, keptId?: string): string;
|
|
94
|
+
/**
|
|
95
|
+
* Plan the redundancy pass for one compaction band.
|
|
96
|
+
*
|
|
97
|
+
* ORDER IS THE SEMANTICS. Empty and noise-only bodies go first (their identity is
|
|
98
|
+
* free), then superseded file reads, then repeats, then oversized clipping -
|
|
99
|
+
* because clipping changes the text two bodies are compared by.
|
|
100
|
+
*/
|
|
101
|
+
export declare function planPrune(input: PruneInput): PrunePlan;
|
|
102
|
+
/** The character count of one message body, for callers that report a ratio. */
|
|
103
|
+
export declare function messageBodyChars(message: Message): number;
|
|
104
|
+
/**
|
|
105
|
+
* The ids the EXISTING memory quotes, for `PruneInput.citedIds` (P1-3).
|
|
106
|
+
*
|
|
107
|
+
* READ FROM THE PRIOR MEMORY ITSELF rather than from the digest it will become,
|
|
108
|
+
* because the prune has to happen BEFORE the digest exists. Every reference the
|
|
109
|
+
* validator can check is included: the anchor, the protected user messages, each
|
|
110
|
+
* item's sources, and each clipped tool result's message id.
|
|
111
|
+
*/
|
|
112
|
+
export declare function citedMessageIds(prior?: {
|
|
113
|
+
originalTask: {
|
|
114
|
+
sourceId: string;
|
|
115
|
+
};
|
|
116
|
+
userMessages: Array<{
|
|
117
|
+
id: string;
|
|
118
|
+
}>;
|
|
119
|
+
items: Array<{
|
|
120
|
+
sources: Array<{
|
|
121
|
+
messageId: string;
|
|
122
|
+
}>;
|
|
123
|
+
}>;
|
|
124
|
+
coverage: {
|
|
125
|
+
clippedToolResults: Array<{
|
|
126
|
+
messageId: string;
|
|
127
|
+
}>;
|
|
128
|
+
};
|
|
129
|
+
}): Set<string>;
|