@aragon-agent/cli 0.6.53 → 0.6.55
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 +64 -0
- package/README.md +135 -6
- package/dist/agent/controller.d.ts +98 -0
- package/dist/agent/controller.js +341 -1
- package/dist/agent/redteam-prompt.d.ts +47 -0
- package/dist/agent/redteam-prompt.js +161 -0
- package/dist/agent/redteam-wiring.d.ts +159 -0
- package/dist/agent/redteam-wiring.js +349 -0
- package/dist/agent/system-prompt.d.ts +27 -0
- package/dist/agent/system-prompt.js +10 -0
- package/dist/app/App.d.ts +17 -1
- package/dist/app/App.js +266 -26
- package/dist/app/use-redteam-mode.d.ts +56 -0
- package/dist/app/use-redteam-mode.js +121 -0
- package/dist/cli.js +78 -4
- package/dist/commands/builtins.js +49 -1
- package/dist/commands/catalog.js +5 -0
- package/dist/commands/index-build.js +2 -1
- package/dist/commands/redteam-command.d.ts +22 -0
- package/dist/commands/redteam-command.js +141 -0
- package/dist/commands/registry.d.ts +7 -0
- package/dist/compaction/compactor.d.ts +26 -0
- package/dist/compaction/compactor.js +81 -8
- package/dist/compaction/summarize-call.d.ts +22 -0
- package/dist/compaction/summarize-call.js +29 -0
- package/dist/compaction/wiring.js +22 -3
- package/dist/config/app-paths.d.ts +13 -0
- package/dist/config/app-paths.js +15 -0
- package/dist/config/cli-commands.d.ts +15 -0
- package/dist/config/cli-commands.js +32 -1
- package/dist/config/env.d.ts +3 -1
- package/dist/config/env.js +23 -1
- package/dist/config/load.d.ts +18 -0
- package/dist/config/load.js +19 -1
- package/dist/config/redteam-launch.d.ts +77 -0
- package/dist/config/redteam-launch.js +58 -0
- package/dist/config/store.js +20 -1
- package/dist/config/ui-state.d.ts +44 -0
- package/dist/config/ui-state.js +38 -0
- package/dist/exec/bridge/server.js +9 -2
- package/dist/exec/options.js +16 -2
- package/dist/governance/prompts/orchestrator-block.js +2 -2
- package/dist/host/controller-factory.d.ts +33 -0
- package/dist/host/controller-factory.js +14 -0
- package/dist/host/follow-up-seed.d.ts +109 -0
- package/dist/host/follow-up-seed.js +196 -0
- package/dist/host/follow-up.d.ts +73 -0
- package/dist/host/follow-up.js +123 -0
- package/dist/host/index.d.ts +3 -0
- package/dist/host/index.js +3 -0
- package/dist/host/memory.d.ts +21 -0
- package/dist/host/memory.js +17 -0
- package/dist/host/sessions.d.ts +32 -2
- package/dist/host/sessions.js +40 -2
- package/dist/logging/install.d.ts +1 -0
- package/dist/logging/install.js +4 -0
- package/dist/logging/logger.d.ts +1 -1
- package/dist/memory/block.d.ts +24 -0
- package/dist/memory/block.js +54 -0
- package/dist/memory/command.d.ts +31 -0
- package/dist/memory/command.js +350 -0
- package/dist/memory/format.d.ts +51 -0
- package/dist/memory/format.js +308 -0
- package/dist/memory/paths.d.ts +68 -0
- package/dist/memory/paths.js +172 -0
- package/dist/memory/plan.d.ts +37 -0
- package/dist/memory/plan.js +184 -0
- package/dist/memory/resolve.d.ts +45 -0
- package/dist/memory/resolve.js +54 -0
- package/dist/memory/secrets.d.ts +30 -0
- package/dist/memory/secrets.js +73 -0
- package/dist/memory/service.d.ts +175 -0
- package/dist/memory/service.js +576 -0
- package/dist/memory/store.d.ts +133 -0
- package/dist/memory/store.js +694 -0
- package/dist/memory/tools.d.ts +36 -0
- package/dist/memory/tools.js +527 -0
- package/dist/memory/transfer.d.ts +53 -0
- package/dist/memory/transfer.js +93 -0
- package/dist/session/checkpoint.d.ts +33 -0
- package/dist/session/checkpoint.js +52 -0
- package/dist/session/limits.d.ts +16 -0
- package/dist/session/limits.js +16 -0
- package/dist/session/persist.d.ts +68 -0
- package/dist/session/persist.js +11 -0
- package/dist/session/startup-session.d.ts +17 -2
- package/dist/session/startup-session.js +15 -2
- package/dist/session/store.d.ts +14 -0
- package/dist/session/store.js +18 -0
- package/dist/session/validate-session.js +129 -0
- package/dist/team/runtime.d.ts +11 -0
- package/dist/team/runtime.js +6 -0
- package/dist/team/subagent.d.ts +21 -0
- package/dist/team/subagent.js +7 -0
- package/dist/tools/index.d.ts +45 -2
- package/dist/tools/index.js +53 -3
- package/package.json +3 -3
- package/skills/project-indexer/SKILL.md +275 -147
- package/skills/project-indexer/templates/artifact-hygiene-template.md +14 -0
- package/skills/project-indexer/templates/config-template.md +12 -0
- package/skills/project-indexer/templates/index-template.md +4 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Budget resolution and the context plan (agent-memory spec 6.2 / 6.3, D-14, D-17).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - `src/memory/**` is inside `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* BOTH FUNCTIONS ARE PURE: no clock, no random, no filesystem. Same input, same output -
|
|
7
|
+
* which is what lets the whole decision table be tested by enumeration, and what keeps
|
|
8
|
+
* the system-prompt block stable across turns so the provider's prefix cache survives
|
|
9
|
+
* (I-4). Nothing here reads a use count or a "last read" time (D-14): reading a note must
|
|
10
|
+
* never reshuffle the catalog.
|
|
11
|
+
*
|
|
12
|
+
* ALL LENGTHS ARE BUDGET UNITS (spec 5.8): an ASCII code point weighs 1, anything else
|
|
13
|
+
* weighs `nonAsciiUnits`. That is what stops a Chinese note from costing three times the
|
|
14
|
+
* window share its character count suggests.
|
|
15
|
+
*/
|
|
16
|
+
import { CHARS_PER_TOKEN } from '@aragon-agent/core';
|
|
17
|
+
import { MEMORY_CONTEXT } from '@aragon-agent/tui/model/memory/limits';
|
|
18
|
+
import { compareMemoryRows } from '@aragon-agent/tui/model/memory/search';
|
|
19
|
+
import { clipToMemoryUnits, codePointCount, countMemoryUnits, } from '@aragon-agent/tui/model/memory/validate';
|
|
20
|
+
import { catalogLine } from './block.js';
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the budgets for a model window.
|
|
23
|
+
*
|
|
24
|
+
* `windowTokens` is the window of the model the prompt is being BUILT FOR - read from the
|
|
25
|
+
* config passed to `composeSystemPrompt`, never from `this.config` (I-5) - because a model
|
|
26
|
+
* switch pre-builds the next prompt and must not use the old model's window.
|
|
27
|
+
*
|
|
28
|
+
* The cap is taken from the CONFIG SUM last, so `total` can never exceed what the user
|
|
29
|
+
* configured, even when the floor would otherwise lift it (RV-8).
|
|
30
|
+
*/
|
|
31
|
+
export function resolveMemoryBudgets(cfg, windowTokens) {
|
|
32
|
+
const known = typeof windowTokens === 'number' && Number.isFinite(windowTokens) && windowTokens > 0;
|
|
33
|
+
const windowUnits = known ? windowTokens * CHARS_PER_TOKEN : 0;
|
|
34
|
+
const explicitCeiling = known
|
|
35
|
+
? Math.min(MEMORY_CONTEXT.bodyHardCeilingChars, Math.max(MEMORY_CONTEXT.floorChars, Math.floor(windowUnits * MEMORY_CONTEXT.explicitWindowShare)))
|
|
36
|
+
: MEMORY_CONTEXT.bodyHardCeilingChars;
|
|
37
|
+
const autoloadActive = !known || windowTokens >= MEMORY_CONTEXT.autoloadMinWindowTokens;
|
|
38
|
+
if (!autoloadActive) {
|
|
39
|
+
return { indexChars: 0, bodyChars: 0, perBodyChars: 0, explicitCeiling, total: 0, autoloadActive };
|
|
40
|
+
}
|
|
41
|
+
const sum = cfg.indexMaxChars + cfg.bodyBudgetChars;
|
|
42
|
+
const total = known
|
|
43
|
+
? Math.min(sum, Math.max(MEMORY_CONTEXT.floorChars, Math.floor(windowUnits * MEMORY_CONTEXT.windowShare)))
|
|
44
|
+
: sum;
|
|
45
|
+
const indexChars = Math.min(cfg.indexMaxChars, Math.floor(total * MEMORY_CONTEXT.indexShare));
|
|
46
|
+
const bodyChars = Math.min(cfg.bodyBudgetChars, total - indexChars);
|
|
47
|
+
const perBodyChars = Math.min(cfg.perBodyMaxChars, Math.max(MEMORY_CONTEXT.perBodyFloor, Math.floor(bodyChars / 2)));
|
|
48
|
+
return { indexChars, bodyChars, perBodyChars, explicitCeiling, total, autoloadActive };
|
|
49
|
+
}
|
|
50
|
+
function classify(note, overrides, autoload) {
|
|
51
|
+
if (overrides[note.id] === 'unmount')
|
|
52
|
+
return null;
|
|
53
|
+
if (overrides[note.id] === 'mount')
|
|
54
|
+
return 0;
|
|
55
|
+
if (autoload && note.load === 'pinned')
|
|
56
|
+
return 1;
|
|
57
|
+
if (autoload && note.load === 'auto')
|
|
58
|
+
return 2;
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
function clippedHint(id, shownChars, totalChars) {
|
|
62
|
+
return (`\n[clipped: showing the first ${shownChars} of ${totalChars} characters. ` +
|
|
63
|
+
`Call memory_read with id "${id}" and offset ${shownChars} for the rest.]`);
|
|
64
|
+
}
|
|
65
|
+
/** Load as much of `note` as `limit` units allow; reports whether it was cut. */
|
|
66
|
+
function loadBody(note, limit) {
|
|
67
|
+
const total = countMemoryUnits(note.body);
|
|
68
|
+
if (total <= limit)
|
|
69
|
+
return { text: note.body, clipped: false, units: total, total };
|
|
70
|
+
const cut = clipToMemoryUnits(note.body, limit).trimEnd();
|
|
71
|
+
const shown = codePointCount(cut);
|
|
72
|
+
return {
|
|
73
|
+
text: `${cut}${clippedHint(note.id, shown, codePointCount(note.body))}`,
|
|
74
|
+
clipped: true,
|
|
75
|
+
units: countMemoryUnits(cut),
|
|
76
|
+
total,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const REASON = { 0: 'mount', 1: 'pinned', 2: 'auto' };
|
|
80
|
+
const STATE = { 0: 'body-mount', 1: 'body-pinned', 2: 'body-auto' };
|
|
81
|
+
/** Decide, deterministically, which notes ride in the prompt and at what level (6.3). */
|
|
82
|
+
export function planMemoryContext(input) {
|
|
83
|
+
const { budgets, overrides } = input;
|
|
84
|
+
const autoload = input.autoload && budgets.autoloadActive;
|
|
85
|
+
const notes = input.notes.filter((note) => note.enabled).sort(compareMemoryRows);
|
|
86
|
+
const candidates = [];
|
|
87
|
+
for (const note of notes) {
|
|
88
|
+
const cls = classify(note, overrides, autoload);
|
|
89
|
+
if (cls !== null)
|
|
90
|
+
candidates.push({ note, cls });
|
|
91
|
+
}
|
|
92
|
+
// Class 0 (the user clicked) first, then pinned, then auto. Within the automatic classes the
|
|
93
|
+
// catalog sort key decides; within class 0 the ORDER THE USER MOUNTED THEM IN does (the
|
|
94
|
+
// insertion order of `overrides`). That is what makes "already-mounted notes are unaffected"
|
|
95
|
+
// true: a newly added mount is always last in line, so when the ceiling cannot hold it, it is
|
|
96
|
+
// the one refused - never an older mount quietly pushed out because the new note happens to
|
|
97
|
+
// be more recently edited (spec 6.3 step 3, refined during implementation).
|
|
98
|
+
const mountedAt = new Map(Object.keys(overrides).map((id, index) => [id, index]));
|
|
99
|
+
const ordered = [...candidates].sort((a, b) => a.cls - b.cls ||
|
|
100
|
+
(a.cls === 0 ? (mountedAt.get(a.note.id) ?? 0) - (mountedAt.get(b.note.id) ?? 0) : 0));
|
|
101
|
+
const bodies = [];
|
|
102
|
+
const degraded = [];
|
|
103
|
+
const refusedMounts = [];
|
|
104
|
+
const rowState = new Map();
|
|
105
|
+
let explicitUsed = 0;
|
|
106
|
+
let bodyUsed = 0;
|
|
107
|
+
for (const { note, cls } of ordered) {
|
|
108
|
+
let loaded = null;
|
|
109
|
+
if (cls === 0) {
|
|
110
|
+
// An explicit mount means "the full text"; it is NOT subject to the per-note cap.
|
|
111
|
+
const remaining = budgets.explicitCeiling - explicitUsed;
|
|
112
|
+
if (remaining < MEMORY_CONTEXT.explicitMinRemaining) {
|
|
113
|
+
refusedMounts.push(note.id);
|
|
114
|
+
degraded.push({ id: note.id, why: 'over_budget' });
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
loaded = loadBody(note, remaining);
|
|
118
|
+
explicitUsed += loaded.units;
|
|
119
|
+
bodyUsed += loaded.units;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const attempt = loadBody(note, budgets.perBodyChars);
|
|
124
|
+
if (bodyUsed + attempt.units > budgets.bodyChars) {
|
|
125
|
+
degraded.push({ id: note.id, why: 'over_budget' });
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
loaded = attempt;
|
|
129
|
+
bodyUsed += attempt.units;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (loaded === null)
|
|
133
|
+
continue;
|
|
134
|
+
if (loaded.clipped)
|
|
135
|
+
degraded.push({ id: note.id, why: 'clipped' });
|
|
136
|
+
bodies.push({
|
|
137
|
+
id: note.id,
|
|
138
|
+
scope: note.scope,
|
|
139
|
+
title: note.title,
|
|
140
|
+
text: loaded.text,
|
|
141
|
+
clipped: loaded.clipped,
|
|
142
|
+
reason: REASON[cls],
|
|
143
|
+
loadedUnits: loaded.units,
|
|
144
|
+
totalUnits: loaded.total,
|
|
145
|
+
});
|
|
146
|
+
rowState.set(note.id, STATE[cls]);
|
|
147
|
+
}
|
|
148
|
+
// Everything without a body is a catalog candidate - but only while auto-load is on.
|
|
149
|
+
const catalog = [];
|
|
150
|
+
const catalogLines = [];
|
|
151
|
+
let indexUnits = 0;
|
|
152
|
+
let omitted = 0;
|
|
153
|
+
for (const note of notes) {
|
|
154
|
+
if (!rowState.has(note.id)) {
|
|
155
|
+
rowState.set(note.id, overrides[note.id] === 'unmount' ? 'unmounted' : 'index');
|
|
156
|
+
}
|
|
157
|
+
if (!autoload || rowState.get(note.id).startsWith('body-'))
|
|
158
|
+
continue;
|
|
159
|
+
const line = catalogLine(note);
|
|
160
|
+
const cost = countMemoryUnits(line) + 1;
|
|
161
|
+
if (catalog.length >= MEMORY_CONTEXT.indexMaxEntries || indexUnits + cost > budgets.indexChars) {
|
|
162
|
+
omitted += 1;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
catalog.push({ id: note.id, scope: note.scope, title: note.title, summary: note.summary, load: note.load });
|
|
166
|
+
catalogLines.push(line);
|
|
167
|
+
indexUnits += cost;
|
|
168
|
+
}
|
|
169
|
+
const bodyUnits = bodies.reduce((sum, body) => sum + countMemoryUnits(body.text), 0);
|
|
170
|
+
const total = indexUnits + bodyUnits;
|
|
171
|
+
const softBudget = budgets.autoloadActive ? budgets.bodyChars : budgets.explicitCeiling;
|
|
172
|
+
return {
|
|
173
|
+
bodies,
|
|
174
|
+
catalog,
|
|
175
|
+
catalogLines,
|
|
176
|
+
catalogOmitted: omitted,
|
|
177
|
+
degraded,
|
|
178
|
+
refusedMounts,
|
|
179
|
+
chars: { index: indexUnits, bodies: bodyUnits, total },
|
|
180
|
+
tokensEstimate: Math.ceil(total / CHARS_PER_TOKEN),
|
|
181
|
+
overBudget: degraded.length > 0 || explicitUsed > softBudget,
|
|
182
|
+
rowState,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<n | id | title | id-prefix>` -> one catalog row (agent-memory spec 8.2, RV-7, RV-14).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - `src/memory/**` is inside `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* RESOLUTION HAPPENS ON THE CATALOG ROWS THE CALLER ALREADY HOLDS, NEVER ON THE
|
|
7
|
+
* FILESYSTEM. A reference reaches here from a place we do not trust (a model's tool
|
|
8
|
+
* argument, a typed command), and joining it into a path to "see if it exists" is how
|
|
9
|
+
* `memory_read({id: "con"})` ends up opening the Windows console device. Matching against
|
|
10
|
+
* known ids first means only an id that is already a real file name ever touches the disk.
|
|
11
|
+
*
|
|
12
|
+
* AMBIGUITY IS AN ANSWER, NOT A GUESS. Two notes may share a title and two ids may share a
|
|
13
|
+
* prefix; the result then lists up to five candidates and refuses, because the next step
|
|
14
|
+
* may be a delete.
|
|
15
|
+
*
|
|
16
|
+
* Same shape as `prompts/resolve.ts::resolvePromptRef` but deliberately NOT shared code: the
|
|
17
|
+
* two features should be able to change their reference syntax independently.
|
|
18
|
+
*/
|
|
19
|
+
import type { MemoryCatalogRow } from '@aragon-agent/tui/model/memory/types';
|
|
20
|
+
/** `mem_` plus at least two more characters. The bare `mem_` prefix matches every id. */
|
|
21
|
+
export declare const MIN_ID_PREFIX = 6;
|
|
22
|
+
export type MemoryRefResolution<T extends RefRow> = {
|
|
23
|
+
ok: true;
|
|
24
|
+
row: T;
|
|
25
|
+
} | {
|
|
26
|
+
ok: false;
|
|
27
|
+
code: 'not_found';
|
|
28
|
+
} | {
|
|
29
|
+
ok: false;
|
|
30
|
+
code: 'ambiguous';
|
|
31
|
+
candidates: T[];
|
|
32
|
+
};
|
|
33
|
+
interface RefRow {
|
|
34
|
+
id: string;
|
|
35
|
+
title: string;
|
|
36
|
+
}
|
|
37
|
+
export interface ResolveOptions {
|
|
38
|
+
/** Accept a 1-based position in `rows` (the `/memory list` numbering). Default true. */
|
|
39
|
+
allowIndex?: boolean;
|
|
40
|
+
/** Accept an exact title (case-insensitive). Default true. */
|
|
41
|
+
allowTitle?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Resolve `ref` against `rows` in the order the user sees them. Never throws. */
|
|
44
|
+
export declare function resolveMemoryRef<T extends RefRow = MemoryCatalogRow>(rows: readonly T[], ref: string, options?: ResolveOptions): MemoryRefResolution<T>;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<n | id | title | id-prefix>` -> one catalog row (agent-memory spec 8.2, RV-7, RV-14).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - `src/memory/**` is inside `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* RESOLUTION HAPPENS ON THE CATALOG ROWS THE CALLER ALREADY HOLDS, NEVER ON THE
|
|
7
|
+
* FILESYSTEM. A reference reaches here from a place we do not trust (a model's tool
|
|
8
|
+
* argument, a typed command), and joining it into a path to "see if it exists" is how
|
|
9
|
+
* `memory_read({id: "con"})` ends up opening the Windows console device. Matching against
|
|
10
|
+
* known ids first means only an id that is already a real file name ever touches the disk.
|
|
11
|
+
*
|
|
12
|
+
* AMBIGUITY IS AN ANSWER, NOT A GUESS. Two notes may share a title and two ids may share a
|
|
13
|
+
* prefix; the result then lists up to five candidates and refuses, because the next step
|
|
14
|
+
* may be a delete.
|
|
15
|
+
*
|
|
16
|
+
* Same shape as `prompts/resolve.ts::resolvePromptRef` but deliberately NOT shared code: the
|
|
17
|
+
* two features should be able to change their reference syntax independently.
|
|
18
|
+
*/
|
|
19
|
+
/** `mem_` plus at least two more characters. The bare `mem_` prefix matches every id. */
|
|
20
|
+
export const MIN_ID_PREFIX = 6;
|
|
21
|
+
const MAX_CANDIDATES = 5;
|
|
22
|
+
/** Resolve `ref` against `rows` in the order the user sees them. Never throws. */
|
|
23
|
+
export function resolveMemoryRef(rows, ref, options = {}) {
|
|
24
|
+
const text = ref.trim();
|
|
25
|
+
if (text.length === 0)
|
|
26
|
+
return { ok: false, code: 'not_found' };
|
|
27
|
+
if (options.allowIndex !== false && /^\d{1,4}$/.test(text)) {
|
|
28
|
+
const row = rows[Number.parseInt(text, 10) - 1];
|
|
29
|
+
if (row !== undefined)
|
|
30
|
+
return { ok: true, row };
|
|
31
|
+
}
|
|
32
|
+
const byId = rows.find((row) => row.id === text);
|
|
33
|
+
if (byId !== undefined)
|
|
34
|
+
return { ok: true, row: byId };
|
|
35
|
+
if (options.allowTitle !== false) {
|
|
36
|
+
const wanted = text.toLowerCase();
|
|
37
|
+
const titled = rows.filter((row) => row.title.trim().toLowerCase() === wanted);
|
|
38
|
+
if (titled.length === 1)
|
|
39
|
+
return { ok: true, row: titled[0] };
|
|
40
|
+
if (titled.length > 1)
|
|
41
|
+
return ambiguous(titled);
|
|
42
|
+
}
|
|
43
|
+
if (text.length >= MIN_ID_PREFIX) {
|
|
44
|
+
const prefixed = rows.filter((row) => row.id.startsWith(text));
|
|
45
|
+
if (prefixed.length === 1)
|
|
46
|
+
return { ok: true, row: prefixed[0] };
|
|
47
|
+
if (prefixed.length > 1)
|
|
48
|
+
return ambiguous(prefixed);
|
|
49
|
+
}
|
|
50
|
+
return { ok: false, code: 'not_found' };
|
|
51
|
+
}
|
|
52
|
+
function ambiguous(rows) {
|
|
53
|
+
return { ok: false, code: 'ambiguous', candidates: rows.slice(0, MAX_CANDIDATES) };
|
|
54
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A high-precision secret sniffer for memory text (agent-memory spec 11.2, D-18, T4).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - `src/memory/**` is inside `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* WHY HERE AND NOT `logging/redact.ts`: that module exports `redactText` and two marker
|
|
7
|
+
* constants, but no pattern table, and reworking logging for this feature would be a
|
|
8
|
+
* separate change (V-7). The patterns below are therefore this module's own.
|
|
9
|
+
*
|
|
10
|
+
* HIGH PRECISION ON PURPOSE. A false positive costs "the agent's write is refused" or "the
|
|
11
|
+
* user sees one extra confirmation"; a false negative puts a plaintext key into a file and
|
|
12
|
+
* then into every future prompt. But a sniffer that cries wolf trains people to click
|
|
13
|
+
* through it, so only shapes with a very low false-positive rate are listed and there is
|
|
14
|
+
* NO entropy heuristic (high false-positive rate, and it cannot explain itself).
|
|
15
|
+
*
|
|
16
|
+
* A MATCH NEVER ECHOES THE MATCHED TEXT. `kind` is the whole explanation: printing the
|
|
17
|
+
* fragment would copy the secret into a log line, a tool result or a toast.
|
|
18
|
+
*/
|
|
19
|
+
export interface SecretVerdict {
|
|
20
|
+
hit: boolean;
|
|
21
|
+
/** Which shape matched; present only when `hit`. */
|
|
22
|
+
kind?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Does `text` look like it contains a credential?
|
|
26
|
+
*
|
|
27
|
+
* `knownSecrets` are the user's own configured API keys (same source as the log
|
|
28
|
+
* redactor); any occurrence of one is a hit regardless of its shape.
|
|
29
|
+
*/
|
|
30
|
+
export declare function looksLikeSecret(text: string, knownSecrets?: readonly string[]): SecretVerdict;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A high-precision secret sniffer for memory text (agent-memory spec 11.2, D-18, T4).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - `src/memory/**` is inside `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* WHY HERE AND NOT `logging/redact.ts`: that module exports `redactText` and two marker
|
|
7
|
+
* constants, but no pattern table, and reworking logging for this feature would be a
|
|
8
|
+
* separate change (V-7). The patterns below are therefore this module's own.
|
|
9
|
+
*
|
|
10
|
+
* HIGH PRECISION ON PURPOSE. A false positive costs "the agent's write is refused" or "the
|
|
11
|
+
* user sees one extra confirmation"; a false negative puts a plaintext key into a file and
|
|
12
|
+
* then into every future prompt. But a sniffer that cries wolf trains people to click
|
|
13
|
+
* through it, so only shapes with a very low false-positive rate are listed and there is
|
|
14
|
+
* NO entropy heuristic (high false-positive rate, and it cannot explain itself).
|
|
15
|
+
*
|
|
16
|
+
* A MATCH NEVER ECHOES THE MATCHED TEXT. `kind` is the whole explanation: printing the
|
|
17
|
+
* fragment would copy the secret into a log line, a tool result or a toast.
|
|
18
|
+
*/
|
|
19
|
+
const PATTERNS = [
|
|
20
|
+
{
|
|
21
|
+
kind: 'private-key',
|
|
22
|
+
pattern: /-----BEGIN (?:RSA |EC |OPENSSH |DSA |ENCRYPTED |PGP )?PRIVATE KEY(?: BLOCK)?-----/,
|
|
23
|
+
},
|
|
24
|
+
{ kind: 'aws-access-key', pattern: /\bAKIA[0-9A-Z]{16}\b/ },
|
|
25
|
+
{ kind: 'github-token', pattern: /\bgh[pousr]_[A-Za-z0-9]{36,}\b/ },
|
|
26
|
+
{ kind: 'slack-token', pattern: /\bxox[baprs]-[A-Za-z0-9-]{10,}/ },
|
|
27
|
+
{ kind: 'api-key-like', pattern: /\bsk-[A-Za-z0-9_-]{20,}/ },
|
|
28
|
+
{ kind: 'api-key-like', pattern: /\bAIza[0-9A-Za-z_-]{35}\b/ },
|
|
29
|
+
{ kind: 'jwt', pattern: /\beyJ[A-Za-z0-9_-]{5,}\.eyJ[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]{5,}/ },
|
|
30
|
+
];
|
|
31
|
+
/** `password = hunter2hunter2`, `API_KEY: abcd1234efgh`. Values are checked for placeholders. */
|
|
32
|
+
const ASSIGNMENT = /\b(?:password|passwd|secret|token|api[_-]?key)\b[ \t]*[:=][ \t]*(["']?)([^\s"']{8,})\1/gi;
|
|
33
|
+
/** A value that is plainly a stand-in, not a credential. */
|
|
34
|
+
function isPlaceholder(value) {
|
|
35
|
+
if (/^(.)\1+$/i.test(value))
|
|
36
|
+
return true; // xxxxxxxx, ********
|
|
37
|
+
if (/^<[^>]*>$/.test(value))
|
|
38
|
+
return true; // <your-token>
|
|
39
|
+
if (/^\$\{[^}]*\}$/.test(value) || /^\$[A-Z_][A-Z0-9_]*$/.test(value))
|
|
40
|
+
return true;
|
|
41
|
+
if (/^\{\{[^}]*\}\}$/.test(value))
|
|
42
|
+
return true; // {{ secrets.TOKEN }}
|
|
43
|
+
if (/^[.\u2026]+$/.test(value))
|
|
44
|
+
return true; // ........
|
|
45
|
+
if (/^(?:your|my|example|placeholder|changeme|redacted|todo)[-_a-z0-9]*$/i.test(value))
|
|
46
|
+
return true;
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
/** Shortest configured key worth matching as a literal: shorter ones match ordinary words. */
|
|
50
|
+
const MIN_KNOWN_SECRET = 8;
|
|
51
|
+
/**
|
|
52
|
+
* Does `text` look like it contains a credential?
|
|
53
|
+
*
|
|
54
|
+
* `knownSecrets` are the user's own configured API keys (same source as the log
|
|
55
|
+
* redactor); any occurrence of one is a hit regardless of its shape.
|
|
56
|
+
*/
|
|
57
|
+
export function looksLikeSecret(text, knownSecrets = []) {
|
|
58
|
+
for (const { kind, pattern } of PATTERNS) {
|
|
59
|
+
if (pattern.test(text))
|
|
60
|
+
return { hit: true, kind };
|
|
61
|
+
}
|
|
62
|
+
for (const match of text.matchAll(ASSIGNMENT)) {
|
|
63
|
+
const value = match[2];
|
|
64
|
+
if (value !== undefined && !isPlaceholder(value))
|
|
65
|
+
return { hit: true, kind: 'assignment' };
|
|
66
|
+
}
|
|
67
|
+
for (const secret of knownSecrets) {
|
|
68
|
+
if (secret.length >= MIN_KNOWN_SECRET && text.includes(secret)) {
|
|
69
|
+
return { hit: true, kind: 'configured' };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { hit: false };
|
|
73
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemoryService - the stateful half of agent memory (agent-memory spec 8.3 / 8.4).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - `src/memory/**` is inside `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* The controller is far over the repo's size limit, so session state, change tracking and
|
|
7
|
+
* the prompt block live here and the controller only forwards. THE DECISIONS are not here
|
|
8
|
+
* either: `planMemoryContext` and `resolveMemoryBudgets` are pure and table-tested; the
|
|
9
|
+
* disk is `store.ts`'s. This class holds what neither can: the session's mount overrides,
|
|
10
|
+
* the snapshot of the directory taken at the last sync, whether a change is waiting for the
|
|
11
|
+
* turn to end, and the subscriber list.
|
|
12
|
+
*
|
|
13
|
+
* THE SYSTEM PROMPT CHANGES ONLY WHEN SOMETHING THE USER (OR AN APPROVED TOOL CALL) DID
|
|
14
|
+
* CHANGES IT. `promptKey` hashes exactly those inputs - note contents, overrides, the
|
|
15
|
+
* auto-load switch, the budgets, the project - and nothing else: no clock, no counter, no
|
|
16
|
+
* "last read". That is what lets the block sit in the provider's prefix cache (I-4).
|
|
17
|
+
*
|
|
18
|
+
* CHANGES LAND AT TURN BOUNDARIES (D-13 / I-6). While a turn runs the prompt it captured
|
|
19
|
+
* stays untouched; a mount flips `pending`, the surface says "applies after this turn", and
|
|
20
|
+
* `onTurnEnd` clears the flag. The prompt itself is rebuilt at the START of the next turn
|
|
21
|
+
* (`prepareUserTurn`), which is also where edits made by another process or by hand are
|
|
22
|
+
* picked up - there is no watcher to rely on.
|
|
23
|
+
*/
|
|
24
|
+
import type { CliConfig, MemoryConfig } from '@aragon-agent/tui/model/config/schema';
|
|
25
|
+
import type { MemoryChange, MemoryDraftInput, MemoryMountOverride, MemoryNote, MemoryNoteScope, MemoryResult, MemoryRowView, MemorySessionState, MemorySessionView, MemoryTrashItem } from '@aragon-agent/tui/model/memory/types';
|
|
26
|
+
import { type MemoryTarget, type ProjectIdentity } from './paths.js';
|
|
27
|
+
import { type MemoryWriteOptions } from './store.js';
|
|
28
|
+
export type { MemoryChange };
|
|
29
|
+
export interface MemoryServiceDeps {
|
|
30
|
+
/** The controller's live configuration. */
|
|
31
|
+
getConfig(): CliConfig;
|
|
32
|
+
getCwd(): string;
|
|
33
|
+
/** The context window of the model `config` selects, or `undefined` when unknown. */
|
|
34
|
+
resolveWindowTokens(config: CliConfig): number | undefined;
|
|
35
|
+
/** A turn (or compaction) is in flight. */
|
|
36
|
+
isBusy(): boolean;
|
|
37
|
+
/** The controller's single prompt-rebuild funnel. */
|
|
38
|
+
rebuildPrompt(): void;
|
|
39
|
+
/** Configured API keys, so a note holding one is caught by value. */
|
|
40
|
+
knownSecrets?(): readonly string[];
|
|
41
|
+
now?(): number;
|
|
42
|
+
/** Tests turn the start-up trash sweep off. */
|
|
43
|
+
purgeTrashOnStart?: boolean;
|
|
44
|
+
}
|
|
45
|
+
export declare class MemoryService {
|
|
46
|
+
private readonly deps;
|
|
47
|
+
private session;
|
|
48
|
+
private notes;
|
|
49
|
+
private project;
|
|
50
|
+
private memoryConfig;
|
|
51
|
+
private pending;
|
|
52
|
+
private notice;
|
|
53
|
+
private computed;
|
|
54
|
+
private blockKey;
|
|
55
|
+
private block;
|
|
56
|
+
private lastEmitted;
|
|
57
|
+
private readonly listeners;
|
|
58
|
+
constructor(deps: MemoryServiceDeps);
|
|
59
|
+
getProject(): ProjectIdentity;
|
|
60
|
+
/** Both scopes the current workspace can see, project first. */
|
|
61
|
+
targets(): MemoryTarget[];
|
|
62
|
+
targetOf(scope: MemoryNoteScope): MemoryTarget;
|
|
63
|
+
/** The notes as of the last sync (full text). */
|
|
64
|
+
getNotes(): readonly MemoryNote[];
|
|
65
|
+
/** `true` if any note exists at all, enabled or not. */
|
|
66
|
+
hasNotes(): boolean;
|
|
67
|
+
/** Fresh read of one note by id (CAS source), resolving which scope owns it. */
|
|
68
|
+
getNote(id: string): MemoryResult<MemoryNote>;
|
|
69
|
+
/** Rows for a list: catalog fields plus where each stands in the context. */
|
|
70
|
+
listRows(query?: string): MemoryRowView[];
|
|
71
|
+
private effectiveAutoload;
|
|
72
|
+
/** What `agentWrites` is right now. `off` can only come from configuration. */
|
|
73
|
+
effectiveAgentWrites(): 'ask' | 'auto' | 'off';
|
|
74
|
+
/** Apply a change to session state, deferring the prompt rewrite when a turn is running. */
|
|
75
|
+
private afterChange;
|
|
76
|
+
setOverride(id: string, state: MemoryMountOverride | 'reset'): MemoryChange;
|
|
77
|
+
/** Flip a note's text in or out, choosing the spelling that makes a second press undo it. */
|
|
78
|
+
toggleMount(id: string): MemoryChange;
|
|
79
|
+
setAutoload(value: boolean | null): MemoryChange;
|
|
80
|
+
setAgentWrites(mode: 'ask' | 'auto' | null): MemoryChange;
|
|
81
|
+
/** Hash of everything the block depends on, and nothing else (I-4). */
|
|
82
|
+
promptKey(config: CliConfig): string;
|
|
83
|
+
/**
|
|
84
|
+
* The `<memory>` block for the prompt being built for `config`. `''` = no block.
|
|
85
|
+
*
|
|
86
|
+
* THE WINDOW COMES FROM THE ARGUMENT, never from `this` (I-5): a model switch pre-builds
|
|
87
|
+
* the incoming model's prompt, and budgets sized for the outgoing window would overrun a
|
|
88
|
+
* smaller one.
|
|
89
|
+
*/
|
|
90
|
+
renderBlock(config: CliConfig): string;
|
|
91
|
+
private logRebuilt;
|
|
92
|
+
private planWith;
|
|
93
|
+
/** Plan + key for `config`, memoised on the key. */
|
|
94
|
+
private compute;
|
|
95
|
+
private keyOf;
|
|
96
|
+
/**
|
|
97
|
+
* Re-read the directory (cached by mtime + size), drop overrides for notes that vanished,
|
|
98
|
+
* and refresh the view. Called at the start of every user turn, and after any write.
|
|
99
|
+
*
|
|
100
|
+
* THE ONLY WAY AN OUTSIDE EDIT REACHES THE MODEL: another process, another window and a
|
|
101
|
+
* text editor all write to the same files, and none of them can tell this process.
|
|
102
|
+
*/
|
|
103
|
+
sync(): void;
|
|
104
|
+
private refreshNotes;
|
|
105
|
+
/**
|
|
106
|
+
* The notes on disk RIGHT NOW (cached by mtime + size), both scopes, project first.
|
|
107
|
+
*
|
|
108
|
+
* What the tools read: they must see disk truth, not this service's last snapshot, but
|
|
109
|
+
* a read must not touch session state - so this returns the list and changes nothing.
|
|
110
|
+
*/
|
|
111
|
+
scanFresh(): MemoryNote[];
|
|
112
|
+
/** Make the pending flag visible to surfaces and clear it. The next turn rebuilds. */
|
|
113
|
+
onTurnEnd(): void;
|
|
114
|
+
/** `prompt()` entry safety net: `agent_end` may never have arrived (forced stop, /clear). */
|
|
115
|
+
applyPending(): void;
|
|
116
|
+
/** The working directory changed: recompute which project's notes are visible. */
|
|
117
|
+
setCwd(cwd: string): void;
|
|
118
|
+
updateConfig(next: MemoryConfig): void;
|
|
119
|
+
/**
|
|
120
|
+
* A note file was created, changed or removed through the store. Re-sync, and treat it
|
|
121
|
+
* like any other change to what the prompt would contain.
|
|
122
|
+
*/
|
|
123
|
+
noteChanged(): void;
|
|
124
|
+
/** Options shared by every write the service makes on a surface's behalf. */
|
|
125
|
+
writeOptions(source: 'user' | 'agent' | 'imported', confirmSecret?: boolean): MemoryWriteOptions;
|
|
126
|
+
/**
|
|
127
|
+
* Save from a surface the USER is looking at (editor Save, `/memory save`). The only
|
|
128
|
+
* UI-side write path (I-1): there is no autosave and no draft on disk.
|
|
129
|
+
*/
|
|
130
|
+
saveNote(draft: MemoryDraftInput, options?: {
|
|
131
|
+
id?: string;
|
|
132
|
+
expectedRev?: string;
|
|
133
|
+
confirmSecret?: boolean;
|
|
134
|
+
}): MemoryResult<MemoryNote>;
|
|
135
|
+
private updateExisting;
|
|
136
|
+
/**
|
|
137
|
+
* File validated drafts from an import (P1). Every note is a CREATE under a freshly minted id with
|
|
138
|
+
* `source: 'imported'`, so an import never overwrites anything; a secret is refused rather than
|
|
139
|
+
* confirmed (the file may come from anyone), and a full scope stops the import for that scope.
|
|
140
|
+
* One `noteChanged()` at the end: the whole import is one change to the prompt, not N.
|
|
141
|
+
*/
|
|
142
|
+
importNotes(drafts: readonly MemoryDraftInput[]): {
|
|
143
|
+
added: number;
|
|
144
|
+
skipped: number;
|
|
145
|
+
};
|
|
146
|
+
deleteNote(id: string, expectedRev: string): MemoryResult<{
|
|
147
|
+
trashId: string;
|
|
148
|
+
}>;
|
|
149
|
+
restoreTrashed(scope: MemoryNoteScope, trashId: string): MemoryResult<{
|
|
150
|
+
id: string;
|
|
151
|
+
}>;
|
|
152
|
+
trash(): MemoryTrashItem[];
|
|
153
|
+
/**
|
|
154
|
+
* Session state for the checkpoint. `undefined` when everything is at its default.
|
|
155
|
+
*
|
|
156
|
+
* Authority never travels in a file (agent-memory I-19): `agentWrites: 'auto'` is a switch the
|
|
157
|
+
* user flipped in THIS process, so it is written as `null`. `'ask'` is kept because it can
|
|
158
|
+
* only make the session stricter than its configuration.
|
|
159
|
+
*/
|
|
160
|
+
exportState(): MemorySessionState | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Tolerant restore: anything malformed is dropped, ids that no longer exist go with it.
|
|
163
|
+
* `agentWrites` is restored only as `'ask'`: a session file is user-editable and `--resume`
|
|
164
|
+
* accepts any path, so it must not be able to switch write approval off (I-19).
|
|
165
|
+
*/
|
|
166
|
+
restoreState(raw: unknown): void;
|
|
167
|
+
/** The whole session view. Always delivered whole: a missed update heals on the next. */
|
|
168
|
+
getView(): MemorySessionView;
|
|
169
|
+
subscribe(listener: () => void): () => void;
|
|
170
|
+
/** Tell subscribers if the view differs from the last one they were told. */
|
|
171
|
+
emitIfChanged(): void;
|
|
172
|
+
private now;
|
|
173
|
+
}
|
|
174
|
+
/** What a surface sees when the controller has no memory service, or memory is off. */
|
|
175
|
+
export declare const UNAVAILABLE_VIEW: MemorySessionView;
|