@north-light/crouter 0.3.302 → 0.3.303
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/dist/core/__tests__/bash-guard.test.d.ts +1 -0
- package/dist/core/__tests__/bash-guard.test.js +190 -0
- package/dist/core/__tests__/fixtures/fake-engine.d.ts +6 -0
- package/dist/core/__tests__/fixtures/fake-engine.js +51 -11
- package/dist/core/__tests__/seam/broker-provider-retry.test.js +57 -0
- package/dist/core/bash-guard.d.ts +6 -0
- package/dist/core/bash-guard.js +393 -0
- package/dist/core/runtime/broker/fault-retry.d.ts +4 -0
- package/dist/core/runtime/broker/fault-retry.js +66 -6
- package/dist/core/shell-segments.d.ts +42 -0
- package/dist/core/shell-segments.js +169 -0
- package/dist/core/substrate/surface-match.d.ts +0 -9
- package/dist/core/substrate/surface-match.js +4 -160
- package/dist/pi-extensions/canvas-bash-valve.d.ts +0 -3
- package/dist/pi-extensions/canvas-bash-valve.js +2 -34
- package/package.json +1 -1
- package/runtime.lock.json +5 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Run with: node --conditions=crtr-src --import tsx/esm --test src/core/__tests__/bash-guard.test.ts
|
|
2
|
+
//
|
|
3
|
+
// BUG REGRESSION: a node ran
|
|
4
|
+
// cd star-theory-2 && HOME="$(mktemp -d …)" godot …; rm -rf "$HOME" …
|
|
5
|
+
// The `HOME=` prefix applied only to `godot`, so the trailing `rm -rf "$HOME"`
|
|
6
|
+
// expanded to the real home directory and deleted for ~108s before another node
|
|
7
|
+
// killed it. The guard refuses that command text before bash ever sees it.
|
|
8
|
+
import assert from 'node:assert/strict';
|
|
9
|
+
import { homedir, userInfo } from 'node:os';
|
|
10
|
+
import { test } from 'node:test';
|
|
11
|
+
import { bashCommandRefusal } from '../bash-guard.js';
|
|
12
|
+
const HOME = homedir();
|
|
13
|
+
const CWD = `${HOME}/Code/cli/crouter`;
|
|
14
|
+
function refusal(command, cwd = CWD) {
|
|
15
|
+
return bashCommandRefusal(command, cwd);
|
|
16
|
+
}
|
|
17
|
+
function assertRefused(command, expectedTarget, cwd = CWD) {
|
|
18
|
+
const message = refusal(command, cwd);
|
|
19
|
+
assert.notEqual(message, null, `expected refusal for: ${command}`);
|
|
20
|
+
assert.ok(message.includes(expectedTarget), `refusal for ${command} should name ${expectedTarget}, got: ${message}`);
|
|
21
|
+
}
|
|
22
|
+
function assertAllowed(command, cwd = CWD) {
|
|
23
|
+
assert.equal(refusal(command, cwd), null, `expected to allow: ${command}`);
|
|
24
|
+
}
|
|
25
|
+
// The incident
|
|
26
|
+
test('the incident command is refused and names the segment and resolved target', () => {
|
|
27
|
+
const command = 'cd star-theory-2 && HOME="$(mktemp -d /tmp/st2-legend-proof.XXXXXX)" godot --headless --path . ' +
|
|
28
|
+
'--script "$CRTR_CONTEXT_DIR/legend_proof.gd"; status=$?; rm -rf "$HOME" 2>/dev/null || true; exit $status';
|
|
29
|
+
const message = refusal(command);
|
|
30
|
+
assert.notEqual(message, null);
|
|
31
|
+
assert.ok(message.includes('rm -rf "$HOME"'), `should quote the offending segment: ${message}`);
|
|
32
|
+
assert.ok(message.includes(HOME), `should name the resolved target: ${message}`);
|
|
33
|
+
});
|
|
34
|
+
// Home, however it is spelled
|
|
35
|
+
test('every textual spelling of the home directory is refused', () => {
|
|
36
|
+
for (const target of ['~', '~/', '"$HOME"', '$HOME', '${HOME}', '"${HOME}/"', HOME, `${HOME}/`, '~/*', '"$HOME"/*']) {
|
|
37
|
+
assertRefused(`rm -rf ${target}`, HOME);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
// System roots
|
|
41
|
+
test('system roots are refused', () => {
|
|
42
|
+
for (const target of ['/', '/Users', '/home', '/private', '/var', '/etc', '/tmp', '/usr', '/opt', '/System', '/Library', '/Applications', '/private/var']) {
|
|
43
|
+
assertRefused(`rm -rf ${target}`, target);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
test('the canvas home is refused', () => {
|
|
47
|
+
assertRefused('rm -rf ~/.crouter', `${HOME}/.crouter`);
|
|
48
|
+
});
|
|
49
|
+
// Ancestors of the working directory
|
|
50
|
+
test('a strict ancestor of the working directory is refused', () => {
|
|
51
|
+
assertRefused(`rm -rf ${HOME}/Code/cli`, `${HOME}/Code/cli`);
|
|
52
|
+
assertRefused('rm -rf ..', `${HOME}/Code/cli`);
|
|
53
|
+
});
|
|
54
|
+
test('a relative target is read against every directory the line could be in', () => {
|
|
55
|
+
assertRefused('cd packages && rm -rf ../..', `${HOME}/Code/cli`);
|
|
56
|
+
// The guard cannot know a `cd` succeeded: after a failed one bash stays put,
|
|
57
|
+
// so `..` really is the ancestor and the refusal must still fire.
|
|
58
|
+
assertRefused('cd definitely-does-not-exist; rm -rf ..', `${HOME}/Code/cli`);
|
|
59
|
+
// Nowhere knowable to resolve from, so nothing relative is refused.
|
|
60
|
+
assertAllowed('cd "$SOMEWHERE" && rm -rf ..');
|
|
61
|
+
assertRefused('cd "$SOMEWHERE" && rm -rf ~', HOME);
|
|
62
|
+
});
|
|
63
|
+
test('the working directory itself still removes — a node may delete its own merged worktree', () => {
|
|
64
|
+
const worktree = `${HOME}/.crouter/canvas/worktrees/node-1`;
|
|
65
|
+
assertAllowed('rm -rf .', worktree);
|
|
66
|
+
assertAllowed(`rm -rf ${worktree}`, worktree);
|
|
67
|
+
});
|
|
68
|
+
// Recursive flag spellings
|
|
69
|
+
test('every recursive flag spelling is refused', () => {
|
|
70
|
+
for (const flags of ['-rf', '-fr', '-r', '-R', '-Rf', '-rvf', '--recursive -f', '-f --recursive', '-f -r', '-r --', '-rf --']) {
|
|
71
|
+
assertRefused(`rm ${flags} ~`, HOME);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
test('a non-recursive rm is not this guard\u2019s business', () => {
|
|
75
|
+
assertAllowed('rm ~');
|
|
76
|
+
assertAllowed('rm -f ~/.zshrc');
|
|
77
|
+
});
|
|
78
|
+
// Prefixes that hide the verb
|
|
79
|
+
test('a command prefix does not hide the rm', () => {
|
|
80
|
+
for (const prefix of ['sudo', 'sudo -E', 'command', 'nice -n 10', 'nohup', 'time', 'env', 'doas']) {
|
|
81
|
+
assertRefused(`${prefix} rm -rf ~`, HOME);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
test('a leading VAR=value assignment does not hide the rm', () => {
|
|
85
|
+
assertRefused('FOO=bar rm -rf ~', HOME);
|
|
86
|
+
});
|
|
87
|
+
// The distinction the incident turned on: an `export` changes $HOME for the
|
|
88
|
+
// rest of the line, a `HOME=… cmd` PREFIX changes it for that one command.
|
|
89
|
+
test('a reassigned HOME is honoured for the rest of the line', () => {
|
|
90
|
+
assertAllowed('export HOME=/tmp/st2home_5e && rm -rf "$HOME"');
|
|
91
|
+
assertAllowed('export HOME=/tmp/st2home_5e\nfind "$HOME" -name settings.cfg -delete');
|
|
92
|
+
assertAllowed('cd star-theory-2 && export HOME=/tmp/x && rm -rf "$HOME" && mkdir -p "$HOME"');
|
|
93
|
+
assertAllowed('HOME=/tmp/x\nrm -rf ~');
|
|
94
|
+
// Reassigned to something the guard cannot read: unknowable, so allowed.
|
|
95
|
+
assertAllowed('export HOME="$(mktemp -d)"; rm -rf "$HOME"');
|
|
96
|
+
});
|
|
97
|
+
test('a HOME= prefix on one command does not carry to the next', () => {
|
|
98
|
+
assertRefused('HOME=/tmp/x godot --headless; rm -rf "$HOME"', HOME);
|
|
99
|
+
});
|
|
100
|
+
test('a HOME= prefix does apply to its own command', () => {
|
|
101
|
+
assertAllowed('HOME=/tmp/x rm -rf "$HOME"');
|
|
102
|
+
});
|
|
103
|
+
test('a backgrounded assignment runs in a subshell and does not carry', () => {
|
|
104
|
+
assertRefused('HOME=/tmp/x & rm -rf "$HOME"', HOME);
|
|
105
|
+
assertRefused('export HOME=/tmp/x & rm -rf "$HOME"', HOME);
|
|
106
|
+
assertAllowed('export HOME=/tmp/x && rm -rf "$HOME"');
|
|
107
|
+
});
|
|
108
|
+
test('an assignment to a builtin that exports it persists', () => {
|
|
109
|
+
assertAllowed('HOME=/tmp/x export HOME; rm -rf "$HOME"');
|
|
110
|
+
});
|
|
111
|
+
test('$USER and $LOGNAME expand — a per-user path spelled that way still resolves', () => {
|
|
112
|
+
// Anchored on the working directory rather than the home path, whose per-user
|
|
113
|
+
// segment is not the username on every platform this runs on.
|
|
114
|
+
const user = userInfo().username;
|
|
115
|
+
const parent = `/srv/people/${user}`;
|
|
116
|
+
assertRefused('rm -rf /srv/people/$USER', parent, `${parent}/project`);
|
|
117
|
+
assertRefused('rm -rf "/srv/people/${LOGNAME}"', parent, `${parent}/project`);
|
|
118
|
+
});
|
|
119
|
+
test('a redirection is not a target', () => {
|
|
120
|
+
assertAllowed('rm -rf ./dist 2>/dev/null');
|
|
121
|
+
assertAllowed('rm -rf ./dist > /dev/null 2>&1');
|
|
122
|
+
assertRefused('rm -rf ~ 2>/dev/null', HOME);
|
|
123
|
+
});
|
|
124
|
+
// Segment separators
|
|
125
|
+
test('the rm is found in any shell segment', () => {
|
|
126
|
+
for (const command of ['ls && rm -rf ~', 'ls; rm -rf ~', 'ls || rm -rf ~', 'ls | rm -rf ~', 'ls &\nrm -rf ~']) {
|
|
127
|
+
assertRefused(command, HOME);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
// Must still run
|
|
131
|
+
test('ordinary removals still run', () => {
|
|
132
|
+
assertAllowed('rm -rf "$d"');
|
|
133
|
+
assertAllowed('rm -rf node_modules');
|
|
134
|
+
assertAllowed('rm -rf /tmp/foo.XXXX');
|
|
135
|
+
assertAllowed('rm -rf "$CRTR_CONTEXT_DIR/jobs/x"');
|
|
136
|
+
assertAllowed('rm -rf ~/Code/cli/crouter/dist');
|
|
137
|
+
assertAllowed('rm -rf ./dist .dist-build-*');
|
|
138
|
+
assertAllowed('rm -rf "$HOME/.cache/crouter/termrender"');
|
|
139
|
+
assertAllowed('rm -rf ~/.crouter/canvas/scratch/old');
|
|
140
|
+
assertAllowed('rm -rf /tmp/crtr-build.1234');
|
|
141
|
+
});
|
|
142
|
+
test('a quoted mention of a dangerous command is data, not a command', () => {
|
|
143
|
+
assertAllowed('echo "rm -rf ~"');
|
|
144
|
+
assertAllowed("git commit -m 'never rm -rf $HOME'");
|
|
145
|
+
});
|
|
146
|
+
test('a heredoc body is data, not a command', () => {
|
|
147
|
+
assertAllowed('cat > note.md <<\'EOF\'\nrm -rf ~\nEOF');
|
|
148
|
+
});
|
|
149
|
+
// Other operations that can destroy a home or system tree
|
|
150
|
+
test('moving a protected path away is refused', () => {
|
|
151
|
+
assertRefused('mv ~ /tmp/old-home', HOME);
|
|
152
|
+
assertRefused('mv /etc /tmp/etc', '/etc');
|
|
153
|
+
});
|
|
154
|
+
test('a recursive permission or ownership change on a protected path is refused', () => {
|
|
155
|
+
assertRefused('chmod -R 777 /', '/');
|
|
156
|
+
assertRefused('sudo chown -R root ~', HOME);
|
|
157
|
+
assertRefused('chgrp -R staff /usr', '/usr');
|
|
158
|
+
assertAllowed('chmod -R 755 ./scripts');
|
|
159
|
+
assertAllowed('chmod 600 ~/.ssh/config');
|
|
160
|
+
});
|
|
161
|
+
test('a find that deletes or executes over a protected path is refused', () => {
|
|
162
|
+
assertRefused('find ~ -name "*.log" -delete', HOME);
|
|
163
|
+
assertRefused('find / -name core -exec rm -f {} \\;', '/');
|
|
164
|
+
assertAllowed('find ~ -name "*.log"');
|
|
165
|
+
assertAllowed('find . -name "*.log" -delete');
|
|
166
|
+
});
|
|
167
|
+
test('find’s leading global options do not hide its start path', () => {
|
|
168
|
+
assertRefused('find -H ~ -delete', HOME);
|
|
169
|
+
assertRefused('find -- ~ -delete', HOME);
|
|
170
|
+
assertRefused('find -L -x /etc -delete', '/etc');
|
|
171
|
+
assertRefused('find -f ~ -delete', HOME);
|
|
172
|
+
assertAllowed('find -H ./logs -delete');
|
|
173
|
+
});
|
|
174
|
+
test('writing a raw disk or reformatting one is refused', () => {
|
|
175
|
+
assertRefused('dd if=/dev/zero of=/dev/disk0 bs=1m', '/dev/disk0');
|
|
176
|
+
assertRefused('dd if=/dev/zero of=/dev/rdisk3 bs=1m', '/dev/rdisk3');
|
|
177
|
+
assertRefused('sudo mkfs.ext4 /dev/sda1', 'mkfs.ext4');
|
|
178
|
+
assertRefused('diskutil eraseDisk JHFS+ Blank /dev/disk2', 'diskutil eraseDisk');
|
|
179
|
+
assertAllowed('dd if=/dev/zero of=./blob bs=1m count=1');
|
|
180
|
+
// A character device is ordinary output, not a disk.
|
|
181
|
+
assertAllowed('dd if=/dev/zero of=/dev/null bs=1m count=1');
|
|
182
|
+
assertAllowed('dd if=./blob of=/dev/stdout');
|
|
183
|
+
});
|
|
184
|
+
// The long-sleep refusal moved here with the rest of the bash refusals.
|
|
185
|
+
test('a long leading sleep is still refused, a short one still runs', () => {
|
|
186
|
+
assert.notEqual(refusal('sleep 300'), null);
|
|
187
|
+
assert.notEqual(refusal('sleep 5m'), null);
|
|
188
|
+
assert.equal(refusal('sleep 5'), null);
|
|
189
|
+
assert.equal(refusal('sleep "$n"'), null);
|
|
190
|
+
});
|
|
@@ -148,6 +148,7 @@ declare class FakeSession {
|
|
|
148
148
|
/** Compiled-seam graceful-teardown vehicle: a real active turn that only settles
|
|
149
149
|
* after abort(), so the broker's bounded abort wait is observable. */
|
|
150
150
|
private heldTurn;
|
|
151
|
+
private retryAbort;
|
|
151
152
|
private readonly listeners;
|
|
152
153
|
/** Idle watcher sends received while bindExtensions is still running. They
|
|
153
154
|
* start naturally as soon as broker.ts subscribes, without a retry loop that
|
|
@@ -195,6 +196,9 @@ declare class FakeSession {
|
|
|
195
196
|
};
|
|
196
197
|
get isStreaming(): boolean;
|
|
197
198
|
getSessionStats(): Record<string, unknown>;
|
|
199
|
+
getContextUsage(): {
|
|
200
|
+
tokens: number;
|
|
201
|
+
} | undefined;
|
|
198
202
|
private syncTreeSnapshot;
|
|
199
203
|
/** Reads the crashed process's last `fake-pi.tree.json` (if any) and rebuilds
|
|
200
204
|
* this process's tree from it. Best-effort: a first boot (no snapshot yet)
|
|
@@ -238,6 +242,7 @@ declare class FakeSession {
|
|
|
238
242
|
deliverAs?: string;
|
|
239
243
|
}): Promise<void>;
|
|
240
244
|
abort(): Promise<void>;
|
|
245
|
+
abortRetry(): void;
|
|
241
246
|
/** In-place tree rewind (broker `navigate_tree`). Mirrors the real SDK's
|
|
242
247
|
* contract: the session file is unchanged, the live history is truncated,
|
|
243
248
|
* and the navigated-to user message's text comes back as `editorText`. The
|
|
@@ -274,6 +279,7 @@ interface FakeModel {
|
|
|
274
279
|
provider: string;
|
|
275
280
|
id: string;
|
|
276
281
|
api?: string;
|
|
282
|
+
contextWindow?: number;
|
|
277
283
|
}
|
|
278
284
|
/** The fake `ModelRuntime` — the runtime-method subset broker.ts's
|
|
279
285
|
* `registryOf` facade (and its model-less construction mask) actually reads. */
|
|
@@ -298,6 +298,7 @@ class FakeSession {
|
|
|
298
298
|
/** Compiled-seam graceful-teardown vehicle: a real active turn that only settles
|
|
299
299
|
* after abort(), so the broker's bounded abort wait is observable. */
|
|
300
300
|
heldTurn;
|
|
301
|
+
retryAbort;
|
|
301
302
|
// The broker's fan-out subscribers (broker.ts `session.subscribe(...)`) and the
|
|
302
303
|
// accumulating message history the catch-up snapshot serializes (G3).
|
|
303
304
|
listeners = new Set();
|
|
@@ -458,6 +459,15 @@ class FakeSession {
|
|
|
458
459
|
getSessionStats() {
|
|
459
460
|
return { messages: 0, tokens: 0 };
|
|
460
461
|
}
|
|
462
|
+
getContextUsage() {
|
|
463
|
+
try {
|
|
464
|
+
const tokens = Number(readFileSync(join(this.dir, 'fake-pi.context-tokens'), 'utf8'));
|
|
465
|
+
return Number.isFinite(tokens) ? { tokens } : undefined;
|
|
466
|
+
}
|
|
467
|
+
catch {
|
|
468
|
+
return { tokens: 1000 };
|
|
469
|
+
}
|
|
470
|
+
}
|
|
461
471
|
syncTreeSnapshot() {
|
|
462
472
|
try {
|
|
463
473
|
writeFileSync(join(this.dir, 'fake-pi.tree.json'), JSON.stringify({
|
|
@@ -571,6 +581,9 @@ class FakeSession {
|
|
|
571
581
|
this.bashAbort?.abort();
|
|
572
582
|
return this.bashRun ?? Promise.resolve();
|
|
573
583
|
}
|
|
584
|
+
abortRetry() {
|
|
585
|
+
this.retryAbort?.();
|
|
586
|
+
}
|
|
574
587
|
/** In-place tree rewind (broker `navigate_tree`). Mirrors the real SDK's
|
|
575
588
|
* contract: the session file is unchanged, the live history is truncated,
|
|
576
589
|
* and the navigated-to user message's text comes back as `editorText`. The
|
|
@@ -938,7 +951,7 @@ class FakeSession {
|
|
|
938
951
|
getBranch: () => this.tree.getBranch(),
|
|
939
952
|
getEntries: () => this.tree.getEntries(),
|
|
940
953
|
},
|
|
941
|
-
getContextUsage: () => (
|
|
954
|
+
getContextUsage: () => this.getContextUsage(),
|
|
942
955
|
// Wired to the broker's shutdownHandler: the stophook calls ctx.shutdown()
|
|
943
956
|
// in exactly its done / idle-release / refresh branches → disposeAndExit.
|
|
944
957
|
shutdown: () => {
|
|
@@ -1068,8 +1081,9 @@ class FakeSession {
|
|
|
1068
1081
|
this.emit({ type: 'agent_settled' });
|
|
1069
1082
|
break;
|
|
1070
1083
|
case 'stop': {
|
|
1071
|
-
// Match pi's lifecycle:
|
|
1072
|
-
//
|
|
1084
|
+
// Match pi's lifecycle: message_end reaches broker fallback policy
|
|
1085
|
+
// before the stophook sees agent_end while the run is active; the
|
|
1086
|
+
// subscriber receives agent_settled after it clears.
|
|
1073
1087
|
this.streaming = true;
|
|
1074
1088
|
const stopMessages = [
|
|
1075
1089
|
{
|
|
@@ -1097,6 +1111,7 @@ class FakeSession {
|
|
|
1097
1111
|
content: [{ type: 'text', text: cmd.text ?? '' }],
|
|
1098
1112
|
},
|
|
1099
1113
|
];
|
|
1114
|
+
this.emit({ type: 'message_end', message: stopMessages[0] });
|
|
1100
1115
|
await this.fire('agent_end', { messages: stopMessages }, ctx);
|
|
1101
1116
|
// Every attempt reaches the broker subscriber while streaming; only the
|
|
1102
1117
|
// following settled event may publish its fault or arm a retry timer.
|
|
@@ -1174,16 +1189,29 @@ class FakeSession {
|
|
|
1174
1189
|
writeFileSync(join(this.dir, 'fake-pi.overflow.json'), JSON.stringify({ outcome: cmd.outcome ?? 'uncompacted' }));
|
|
1175
1190
|
break;
|
|
1176
1191
|
}
|
|
1177
|
-
case 'auto_retry':
|
|
1178
|
-
//
|
|
1179
|
-
//
|
|
1192
|
+
case 'auto_retry': {
|
|
1193
|
+
// Pi emits agent_end for the failed attempt, then auto_retry_start, and
|
|
1194
|
+
// only then creates the AbortController for its delay. The capacity
|
|
1195
|
+
// fallback must defer abortRetry one microtask to cancel this retry.
|
|
1196
|
+
const retryMessages = [{
|
|
1197
|
+
role: 'assistant', stopReason: 'error', errorMessage: cmd.errorMessage ?? 'rate limit exceeded', content: [],
|
|
1198
|
+
}];
|
|
1199
|
+
this.streaming = true;
|
|
1200
|
+
await this.fire('agent_end', { messages: retryMessages }, ctx);
|
|
1201
|
+
this.emit({ type: 'agent_end', messages: retryMessages, willRetry: true });
|
|
1180
1202
|
this.emit({ type: 'auto_retry_start', errorMessage: cmd.errorMessage ?? 'rate limit exceeded' });
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
// receipt for a rejected target (which intentionally never changes model).
|
|
1203
|
+
let aborted = false;
|
|
1204
|
+
this.retryAbort = () => { aborted = true; };
|
|
1184
1205
|
await new Promise((resolve) => setImmediate(resolve));
|
|
1185
|
-
|
|
1206
|
+
this.retryAbort = undefined;
|
|
1207
|
+
if (aborted)
|
|
1208
|
+
this.emit({ type: 'auto_retry_end', success: false, finalError: 'Retry cancelled' });
|
|
1209
|
+
this.streaming = false;
|
|
1210
|
+
await this.fire('agent_settled', { type: 'agent_settled' }, ctx);
|
|
1211
|
+
this.emit({ type: 'agent_settled' });
|
|
1212
|
+
writeFileSync(join(this.dir, 'fake-pi.auto-retry.json'), JSON.stringify({ at: Date.now(), aborted }));
|
|
1186
1213
|
break;
|
|
1214
|
+
}
|
|
1187
1215
|
case 'display':
|
|
1188
1216
|
// Paint through the broker's REAL uiContext exactly as an extension does.
|
|
1189
1217
|
// These are LEVELS with no pi getter, so the broker is their only holder
|
|
@@ -1221,6 +1249,18 @@ class FakeSession {
|
|
|
1221
1249
|
}
|
|
1222
1250
|
}
|
|
1223
1251
|
}
|
|
1252
|
+
function fakeModel(provider, id) {
|
|
1253
|
+
try {
|
|
1254
|
+
const capacities = JSON.parse(readFileSync(join(nodeDirFromEnv(), 'fake-pi.model-capacities.json'), 'utf8'));
|
|
1255
|
+
const contextWindow = capacities[`${provider}/${id}`];
|
|
1256
|
+
return typeof contextWindow === 'number' && Number.isFinite(contextWindow)
|
|
1257
|
+
? { provider, id, contextWindow }
|
|
1258
|
+
: { provider, id };
|
|
1259
|
+
}
|
|
1260
|
+
catch {
|
|
1261
|
+
return { provider, id };
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1224
1264
|
/** The real pi SDK's model-less sentinel (Agent.DEFAULT_MODEL). The fake mirrors
|
|
1225
1265
|
* it so `session.model` is NEVER literally `undefined` after a model-less boot,
|
|
1226
1266
|
* matching prod — see the FakeSession constructor. */
|
|
@@ -1292,7 +1332,7 @@ export async function createAgentSessionServices(options) {
|
|
|
1292
1332
|
// A minimal model stub for any spec — the set_model regression test drives
|
|
1293
1333
|
// the broker's findModelSpec → session.setModel path through it. Nothing is
|
|
1294
1334
|
// findable during a zero-auth window.
|
|
1295
|
-
getModel: (provider, id) => (zeroAuth() ? undefined :
|
|
1335
|
+
getModel: (provider, id) => (zeroAuth() ? undefined : fakeModel(provider, id)),
|
|
1296
1336
|
// Always-authenticated single fake model — the M-9 no-boot-model branch
|
|
1297
1337
|
// (broker.ts) picks `available[0]` and must never see an empty list here —
|
|
1298
1338
|
// EXCEPT during a zero-auth window, when nothing is available/registered.
|
|
@@ -43,6 +43,33 @@ function seedPool(provider, cooling) {
|
|
|
43
43
|
rateLimitedUntil: cooling ? Date.now() + 60_000 : 0, lastAttemptAt: 0, lastRateLimitedAt: 0,
|
|
44
44
|
}]));
|
|
45
45
|
}
|
|
46
|
+
function modelRoute(spec) {
|
|
47
|
+
return { ultra: spec, strong: spec, medium: spec, light: spec };
|
|
48
|
+
}
|
|
49
|
+
function seedCapacityFallbackRoutes() {
|
|
50
|
+
const userConfigDir = join(h.agentHome, '.crouter');
|
|
51
|
+
const userConfigPath = join(userConfigDir, 'config.json');
|
|
52
|
+
mkdirSync(userConfigDir, { recursive: true });
|
|
53
|
+
writeFileSync(userConfigPath, JSON.stringify({
|
|
54
|
+
kinds: { 'test/capacity-fallback': { whenToUse: 'Capacity fallback regression.', model: 'anthropic/medium' } },
|
|
55
|
+
modelRoutes: {
|
|
56
|
+
primary: { family: 'anthropic', credentialSource: 'user', providerId: 'fake-primary', models: modelRoute('fake-primary/primary') },
|
|
57
|
+
fallback: { family: 'anthropic', credentialSource: 'northlight', providerId: 'fake-small', models: modelRoute('fake-small/small') },
|
|
58
|
+
largerFallback: { family: 'anthropic', credentialSource: 'third', providerId: 'fake-large', models: modelRoute('fake-large/large') },
|
|
59
|
+
},
|
|
60
|
+
modelRouting: { credentialSourceOrder: ['user', 'northlight', 'third'], familyOrder: ['anthropic'], strengthFallback: ['medium'] },
|
|
61
|
+
}));
|
|
62
|
+
return userConfigPath;
|
|
63
|
+
}
|
|
64
|
+
function seedCapacityFixture(id, largeContextWindow = 999) {
|
|
65
|
+
const dir = join(h.home, 'nodes', id);
|
|
66
|
+
writeFileSync(join(dir, 'fake-pi.context-tokens'), '1000');
|
|
67
|
+
writeFileSync(join(dir, 'fake-pi.model-capacities.json'), JSON.stringify({
|
|
68
|
+
'fake-primary/primary': 2000,
|
|
69
|
+
'fake-small/small': 999,
|
|
70
|
+
'fake-large/large': largeContextWindow,
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
46
73
|
// The node's recorded model (fake-pi.model.json, written by the fake's
|
|
47
74
|
// recordModel) — `null` for a model-less boot. A disk read, so it survives the
|
|
48
75
|
// broker exiting after its kickoff turn idle-releases.
|
|
@@ -395,6 +422,36 @@ test('a recovered compact-and-retry commits nothing, while a definitive failure
|
|
|
395
422
|
const notices = h.inbox(parent).filter((entry) => /exceeded its model context window/i.test(entry.label));
|
|
396
423
|
assert.equal(notices.length, 1, 'the parent receives exactly one structured overflow notice');
|
|
397
424
|
});
|
|
425
|
+
test('provider-retry and model-not-found fallback both terminate when every available target has a smaller known context window', { timeout: 30_000 }, async () => {
|
|
426
|
+
const userConfigPath = seedCapacityFallbackRoutes();
|
|
427
|
+
try {
|
|
428
|
+
const parent = h.spawnRoot('capacity fallback parent');
|
|
429
|
+
const providerRetry = await h.spawnHeadlessChild(parent, 'provider retry capacity fallback', { kind: 'test/capacity-fallback' });
|
|
430
|
+
await h.waitFor(() => agentStartCount(providerRetry) >= 1, { label: 'provider retry capacity fixture started' });
|
|
431
|
+
seedCapacityFixture(providerRetry, 2000);
|
|
432
|
+
h.fakeCmd(providerRetry, { cmd: 'auto_retry', errorMessage: 'rate limit exceeded' });
|
|
433
|
+
await h.waitFor(() => model(providerRetry)?.provider === 'fake-large' ? true : null, { label: 'provider retry skips the too-small target for the large enough target' });
|
|
434
|
+
assert.equal(model(providerRetry).provider, 'fake-large', 'a large enough fallback remains eligible after the smaller route is rejected');
|
|
435
|
+
const exhaustedProviderRetry = await h.spawnHeadlessChild(parent, 'exhausted provider retry capacity fallback', { kind: 'test/capacity-fallback' });
|
|
436
|
+
await h.waitFor(() => agentStartCount(exhaustedProviderRetry) >= 1, { label: 'exhausted provider retry capacity fixture started' });
|
|
437
|
+
seedCapacityFixture(exhaustedProviderRetry);
|
|
438
|
+
h.fakeCmd(exhaustedProviderRetry, { cmd: 'auto_retry', errorMessage: 'rate limit exceeded' });
|
|
439
|
+
const providerRetryFault = await h.waitFor(() => readFault(exhaustedProviderRetry)?.kind === 'context-overflow' ? readFault(exhaustedProviderRetry) : null, { label: 'provider retry rejects every too-small fallback target' });
|
|
440
|
+
assert.equal(providerRetryFault.retry.disposition, 'fatal', 'all known-too-small provider retry targets produce a fatal context-overflow fault');
|
|
441
|
+
assert.match(providerRetryFault.message, /1000 tokens/, 'the terminal fault identifies the current conversation size');
|
|
442
|
+
assert.equal(JSON.parse(readFileSync(join(h.home, 'nodes', exhaustedProviderRetry, 'fake-pi.auto-retry.json'), 'utf8')).aborted, true, 'the retry delay is cancelled after Pi creates its retry controller');
|
|
443
|
+
const modelNotFound = await h.spawnHeadlessChild(parent, 'model-not-found capacity fallback', { kind: 'test/capacity-fallback' });
|
|
444
|
+
await h.waitFor(() => agentStartCount(modelNotFound) >= 1, { label: 'model-not-found capacity fixture started' });
|
|
445
|
+
seedCapacityFixture(modelNotFound);
|
|
446
|
+
h.fakeCmd(modelNotFound, { cmd: 'stop', reason: 'error', errorMessage: 'Model not found' });
|
|
447
|
+
const modelNotFoundFault = await h.waitFor(() => readFault(modelNotFound)?.kind === 'context-overflow' ? readFault(modelNotFound) : null, { label: 'model-not-found rejects the too-small fallback target' });
|
|
448
|
+
assert.equal(modelNotFoundFault.retry.disposition, 'fatal', 'all known-too-small model-not-found targets produce a fatal context-overflow fault');
|
|
449
|
+
assert.match(modelNotFoundFault.message, /1000 tokens/, 'the terminal fault survives ordinary settlement staging');
|
|
450
|
+
}
|
|
451
|
+
finally {
|
|
452
|
+
rmSync(userConfigPath, { force: true });
|
|
453
|
+
}
|
|
454
|
+
});
|
|
398
455
|
// Two real logical launches keep both user paths non-vacuous: the picker first
|
|
399
456
|
// proves an explicit concrete choice pins and clears routing, while Alt+M runs
|
|
400
457
|
// directly against still-present logical intent before Shift+Tab and replay.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Seconds a leading `sleep ...` will block for, or null when the command does
|
|
2
|
+
* not open with a sleep (or its duration isn't statically knowable). */
|
|
3
|
+
export declare function leadingSleepSeconds(command: string): number | null;
|
|
4
|
+
/** The refusal this command earns before it runs, or null when it may run.
|
|
5
|
+
* `cwd` is the directory the command will execute in. */
|
|
6
|
+
export declare function bashCommandRefusal(command: string, cwd: string, home?: string): string | null;
|