@niwa-labs/harness-mcode 0.0.0-stage → 0.0.0
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/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +17 -4
- package/src/index.ts +13 -0
- package/src/mcode/control.ts +1840 -0
- package/src/mcode/durable.ts +445 -0
- package/src/mcode/fixtures.ts +93 -0
- package/src/mcode/format.ts +177 -0
- package/src/mcode/index.ts +50 -0
- package/src/mcode/kind.ts +147 -0
- package/src/mcode/ledger.ts +199 -0
- package/src/mcode/paths.ts +80 -0
- package/src/mcode/protocol.ts +91 -0
- package/src/mcode/reading.ts +1153 -0
- package/src/mcode/reverse.ts +201 -0
- package/src/mcode/skills.ts +6 -0
- package/src/mcode/titles.ts +51 -0
- package/src/mcode/tools.ts +56 -0
- package/src/mcode/transport.ts +31 -0
- package/src/ncode/index.ts +34 -0
- package/src/ncode/launcher.ts +33 -0
- package/src/ncode/protocol.ts +146 -0
- package/src/ncode/skills.ts +6 -0
- package/test/live/ncode-permission.test.ts +82 -0
- package/test/mcode-conformance.test.ts +52 -0
- package/test/mcode-control.test.ts +1616 -0
- package/test/mcode-durable.test.ts +722 -0
- package/test/mcode-ledger.test.ts +357 -0
- package/test/mcode-reading.test.ts +286 -0
- package/test/mcode-transport.test.ts +48 -0
- package/test/profile.test.ts +89 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,1616 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MiniMax Code control side.
|
|
3
|
+
*
|
|
4
|
+
* The live test runs the real `mcode` on the model the check names, and it is skipped unless
|
|
5
|
+
* `NIWA_LIVE=1` and the binary is on PATH (`NIWA_MCODE_LINEAGE=fork` when it is the fork). It proves the whole path: a start that names the
|
|
6
|
+
* session and ends its turn, a message that starts a turn and ends it, a resume that carries a
|
|
7
|
+
* second message, an answer by schema, and no process left behind.
|
|
8
|
+
*
|
|
9
|
+
* The signs of life are a forward-only stream: a watcher sees what happens from the moment it
|
|
10
|
+
* subscribes, and nothing before it. So every step subscribes first and only then triggers the
|
|
11
|
+
* action, and it waits for the first turn that ends — never for a turn number it guessed.
|
|
12
|
+
*
|
|
13
|
+
* Every test disposes its control side, so no child outlives it.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { afterEach, expect, test } from "bun:test";
|
|
17
|
+
import { existsSync } from "node:fs";
|
|
18
|
+
import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
19
|
+
import { homedir } from "node:os";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
Unsupported,
|
|
25
|
+
harnessId,
|
|
26
|
+
modelName,
|
|
27
|
+
promptFromText,
|
|
28
|
+
sessionId,
|
|
29
|
+
type LifeEvent,
|
|
30
|
+
type ModelName,
|
|
31
|
+
type SessionId,
|
|
32
|
+
} from "@niwa-labs/harness";
|
|
33
|
+
import {
|
|
34
|
+
mcodeControl,
|
|
35
|
+
mcodeModelOptionValue,
|
|
36
|
+
servesSteer,
|
|
37
|
+
type McodeSessionControl,
|
|
38
|
+
type MinimaxLineage,
|
|
39
|
+
} from "../src/mcode/control.ts";
|
|
40
|
+
import { homeNameOfProfile, homeOfProfile } from "../src/mcode/paths.ts";
|
|
41
|
+
import { mcode, mcodeKind } from "../src/mcode/index.ts";
|
|
42
|
+
import { ncode, ncodeKind } from "../src/ncode/index.ts";
|
|
43
|
+
import { eventually, tempDir, live } from "@niwa-labs/testing";
|
|
44
|
+
import { resolveProfile } from "@niwa-labs/sandbox";
|
|
45
|
+
|
|
46
|
+
let dir = "";
|
|
47
|
+
let control: McodeSessionControl | undefined;
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await control?.dispose();
|
|
51
|
+
control = undefined;
|
|
52
|
+
if (dir !== "") await rm(dir, { recursive: true, force: true });
|
|
53
|
+
dir = "";
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to the signs of life and wait for the first turn that ends.
|
|
58
|
+
*
|
|
59
|
+
* The subscription is made when this is called, before the action that starts the turn: the
|
|
60
|
+
* stream is forward-only, so a watcher that arrives after the turn ended sees nothing of it.
|
|
61
|
+
*/
|
|
62
|
+
function firstTurnEnded(stream: AsyncIterable<LifeEvent>): Promise<LifeEvent[]> {
|
|
63
|
+
return new Promise<LifeEvent[]>((resolve, reject) => {
|
|
64
|
+
const events: LifeEvent[] = [];
|
|
65
|
+
void (async () => {
|
|
66
|
+
for await (const event of stream) {
|
|
67
|
+
events.push(event);
|
|
68
|
+
if (event.kind === "turn-ended") {
|
|
69
|
+
resolve(events);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
reject(new Error("the session ended before a turn did"));
|
|
74
|
+
})().catch(reject);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The turn of the events, or undefined when the events do not name one. */
|
|
79
|
+
function turnOf(events: readonly LifeEvent[], kind: "turn-started" | "turn-ended"): number | undefined {
|
|
80
|
+
for (const event of events) {
|
|
81
|
+
if (event.kind === kind) return event.turn;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The pids of the processes this process started directly, with their command. */
|
|
87
|
+
function directChildren(): Array<{ pid: number; pgid: number; command: string }> {
|
|
88
|
+
const listed = Bun.spawnSync(["ps", "-e", "-o", "pid=,ppid=,pgid=,command="]).stdout.toString();
|
|
89
|
+
const found: Array<{ pid: number; pgid: number; command: string }> = [];
|
|
90
|
+
for (const line of listed.split("\n")) {
|
|
91
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/.exec(line);
|
|
92
|
+
if (match === null) continue;
|
|
93
|
+
const command = match[4] ?? "";
|
|
94
|
+
// The `ps` that reads the table is itself a child of this process.
|
|
95
|
+
if (command.startsWith("ps ")) continue;
|
|
96
|
+
if (Number(match[2]) !== process.pid) continue;
|
|
97
|
+
found.push({ pid: Number(match[1]), pgid: Number(match[3]), command });
|
|
98
|
+
}
|
|
99
|
+
return found;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The pids of the processes in the process group with this id. */
|
|
103
|
+
function groupMembers(pgid: number): number[] {
|
|
104
|
+
const listed = Bun.spawnSync(["ps", "-e", "-o", "pid=,pgid="]).stdout.toString();
|
|
105
|
+
const found: number[] = [];
|
|
106
|
+
for (const line of listed.split("\n")) {
|
|
107
|
+
const match = /^\s*(\d+)\s+(\d+)\s*$/.exec(line);
|
|
108
|
+
if (match === null) continue;
|
|
109
|
+
if (Number(match[2]) === pgid) found.push(Number(match[1]));
|
|
110
|
+
}
|
|
111
|
+
return found;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const liveLineage = process.env["NIWA_MCODE_LINEAGE"] === "fork" ? "fork" : "upstream";
|
|
115
|
+
const liveProgram = liveLineage === "fork" ? "ncode" : "mcode";
|
|
116
|
+
|
|
117
|
+
test.skipIf(!(live && Bun.which(liveProgram)))(
|
|
118
|
+
"the real agent starts, names the session, ends a turn, sends, resumes, answers by schema and leaves no process",
|
|
119
|
+
async () => {
|
|
120
|
+
dir = await tempDir("mcode-control");
|
|
121
|
+
const before = directChildren();
|
|
122
|
+
control = mcodeControl({
|
|
123
|
+
mode: "full",
|
|
124
|
+
harness: harnessId("mcode-proof"),
|
|
125
|
+
// The handshake refuses a binary of the other lineage: say which one is on the path.
|
|
126
|
+
lineage: liveLineage,
|
|
127
|
+
model: "minimax/MiniMax-M3.1-Flash",
|
|
128
|
+
turnTimeoutMs: 120_000,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// 1. The start names the session and returns; its prompt is the first turn, and that turn
|
|
132
|
+
// ends.
|
|
133
|
+
//
|
|
134
|
+
// The first turn begins inside the start, before there is an id to watch. The side keeps
|
|
135
|
+
// its signs for the first stream of life, so that stream sees it begin and end.
|
|
136
|
+
const { session: id, first } = await control.start({
|
|
137
|
+
prompt: promptFromText("reply with ok"),
|
|
138
|
+
cwd: dir,
|
|
139
|
+
});
|
|
140
|
+
expect(id.native.length).toBeGreaterThan(0);
|
|
141
|
+
expect(id.harness).toBe(harnessId("mcode-proof"));
|
|
142
|
+
expect(first).toMatchObject({ status: "started", turn: 1 });
|
|
143
|
+
expect(turnOf(await firstTurnEnded(control.life(id)), "turn-ended")).toBe(1);
|
|
144
|
+
expect(await control.state(id)).toBe("idle");
|
|
145
|
+
|
|
146
|
+
// The agent this side started is a child of this process, in a group of its own.
|
|
147
|
+
const agent = directChildren().find((child) => !before.some((old) => old.pid === child.pid));
|
|
148
|
+
expect(agent).toBeDefined();
|
|
149
|
+
const pgid = agent?.pgid ?? 0;
|
|
150
|
+
|
|
151
|
+
// 2. A second message: the subscription is made first, the turn it watches is the
|
|
152
|
+
// turn the receipt names, and it ends.
|
|
153
|
+
const sent = firstTurnEnded(control.life(id));
|
|
154
|
+
const receipt = await control.send(id, "reply with ok again");
|
|
155
|
+
expect(receipt.status).toBe("started");
|
|
156
|
+
const sentEvents = await sent;
|
|
157
|
+
// The turn that ended is the turn the receipt named — not a number this test guessed.
|
|
158
|
+
expect(turnOf(sentEvents, "turn-ended")).toBe(receipt.turn);
|
|
159
|
+
expect(turnOf(sentEvents, "turn-started")).toBe(receipt.turn);
|
|
160
|
+
expect(await control.state(id)).toBe("idle");
|
|
161
|
+
|
|
162
|
+
// 3. A resume carries a second message, and that turn ends too. The subscription
|
|
163
|
+
// is made first: the turn may end before the resume returns.
|
|
164
|
+
const resumed = firstTurnEnded(control.life(id));
|
|
165
|
+
await control.resume(id, { prompt: promptFromText("reply with ok once more") });
|
|
166
|
+
const resumedEvents = await resumed;
|
|
167
|
+
expect(turnOf(resumedEvents, "turn-ended")).toBe(turnOf(resumedEvents, "turn-started"));
|
|
168
|
+
expect(await control.state(id)).toBe("idle");
|
|
169
|
+
|
|
170
|
+
// 4. An answer by schema: the value that comes back passed the schema.
|
|
171
|
+
const asked = firstTurnEnded(control.life(id));
|
|
172
|
+
const answer = await control.ask(id, {
|
|
173
|
+
schema: z.object({ word: z.string() }),
|
|
174
|
+
prompt: promptFromText("Give the word ok."),
|
|
175
|
+
});
|
|
176
|
+
expect(turnOf(await asked, "turn-ended")).toBeDefined();
|
|
177
|
+
expect(answer.session).toEqual(id);
|
|
178
|
+
expect(answer.value.word.toLowerCase()).toContain("ok");
|
|
179
|
+
|
|
180
|
+
// 5. An interrupt ends the running turn, and the session stays usable. The
|
|
181
|
+
// subscription is made first, and the interrupt waits for the turn to be running.
|
|
182
|
+
const cut = firstTurnEnded(control.life(id));
|
|
183
|
+
const long = await control.send(id, "count from 1 to 500, one number per line");
|
|
184
|
+
expect(long.status).toBe("started");
|
|
185
|
+
await control.interrupt(id);
|
|
186
|
+
expect(turnOf(await cut, "turn-ended")).toBe(long.turn);
|
|
187
|
+
expect(await control.state(id)).toBe("idle");
|
|
188
|
+
|
|
189
|
+
// 6. No process is left behind: the group this side started is empty, and the
|
|
190
|
+
// agent is not a child of this process any more.
|
|
191
|
+
await control.dispose();
|
|
192
|
+
control = undefined;
|
|
193
|
+
const deadline = Date.now() + 10_000;
|
|
194
|
+
while (groupMembers(pgid).length > 0 && Date.now() < deadline) {
|
|
195
|
+
await Bun.sleep(100);
|
|
196
|
+
}
|
|
197
|
+
expect(groupMembers(pgid)).toEqual([]);
|
|
198
|
+
expect(directChildren().some((child) => !before.some((old) => old.pid === child.pid))).toBe(
|
|
199
|
+
false,
|
|
200
|
+
);
|
|
201
|
+
},
|
|
202
|
+
300_000,
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
test("the control side refuses a session it does not have", async () => {
|
|
206
|
+
dir = await tempDir("mcode-control");
|
|
207
|
+
control = mcodeControl({ mode: "full", harness: harnessId("mcode-proof") });
|
|
208
|
+
let refused: unknown;
|
|
209
|
+
try {
|
|
210
|
+
await control.send(sessionId(harnessId("mcode-proof"), "no-such-session"), "hello");
|
|
211
|
+
} catch (error) {
|
|
212
|
+
refused = error;
|
|
213
|
+
}
|
|
214
|
+
expect(refused).toBeInstanceOf(Error);
|
|
215
|
+
expect((refused as Error).message).toContain("no-such-session");
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("the model option is the one the session advertises", () => {
|
|
219
|
+
// The agent refuses a model it does not advertise, so the advertised one wins.
|
|
220
|
+
const advertised = [
|
|
221
|
+
{ value: "m:minimax:MiniMax-M3.1-Flash:v:", name: "M3.1-Flash" },
|
|
222
|
+
{ value: "m:minimax:MiniMax-M3.1-Flash:v:thinking", name: "M3.1-Flash · thinking" },
|
|
223
|
+
];
|
|
224
|
+
expect(mcodeModelOptionValue("minimax/MiniMax-M3.1-Flash", advertised)).toBe(
|
|
225
|
+
"m:minimax:MiniMax-M3.1-Flash:v:thinking",
|
|
226
|
+
);
|
|
227
|
+
// An exact advertisement is used as it is.
|
|
228
|
+
expect(mcodeModelOptionValue("minimax/MiniMax-M3.1-Flash#thinking", advertised)).toBe(
|
|
229
|
+
"m:minimax:MiniMax-M3.1-Flash:v:thinking",
|
|
230
|
+
);
|
|
231
|
+
// Without an advertisement the id is encoded as it is.
|
|
232
|
+
expect(mcodeModelOptionValue("minimax/MiniMax-M3.1-Flash")).toBe(
|
|
233
|
+
"m:minimax:MiniMax-M3.1-Flash:u",
|
|
234
|
+
);
|
|
235
|
+
// A value the agent named is an option value already: it is not taken for an id and encoded again, advertised or not.
|
|
236
|
+
expect(mcodeModelOptionValue("m:minimax:MiniMax-M3.1-Flash:v:", advertised)).toBe("m:minimax:MiniMax-M3.1-Flash:v:");
|
|
237
|
+
expect(mcodeModelOptionValue("m:minimax:MiniMax-M3.1-Flash:v:thinking")).toBe("m:minimax:MiniMax-M3.1-Flash:v:thinking");
|
|
238
|
+
expect(mcodeModelOptionValue("m:minimax:MiniMax-M3.1-Flash:u")).toBe("m:minimax:MiniMax-M3.1-Flash:u");
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* A stand-in for `mcode acp`: it answers the handshake and every config call, ends every prompt after a short wait,
|
|
243
|
+
* dies on the prompt "die", never ends the prompt "hang" until it is cancelled, and answers a
|
|
244
|
+
* prompt that begins with "give the word" with the JSON `{"word":"ok"}`. Like real mcode it
|
|
245
|
+
* refuses a prompt while another one is active, and answers a cancelled prompt a little later.
|
|
246
|
+
*
|
|
247
|
+
* The params of every `session/new` and `session/load` are written to a file, so a test can read
|
|
248
|
+
* what the agent was asked for.
|
|
249
|
+
*/
|
|
250
|
+
const FAKE_AGENT = `#!/usr/bin/env bun
|
|
251
|
+
const decoder = new TextDecoder();
|
|
252
|
+
const { appendFileSync, existsSync, readFileSync } = await import("node:fs");
|
|
253
|
+
let buffer = "";
|
|
254
|
+
let active;
|
|
255
|
+
let model;
|
|
256
|
+
appendFileSync(${JSON.stringify(join(dir, "argv.log"))}, JSON.stringify(process.argv.slice(2)) + "\\n");
|
|
257
|
+
appendFileSync(${JSON.stringify(join(dir, "env.log"))}, JSON.stringify({ mode: process.env.MCODE_SERVER_MODE, id: process.env.NIWA_ID }) + "\\n");
|
|
258
|
+
const send = (message) => process.stdout.write(JSON.stringify(message) + "\\n");
|
|
259
|
+
for await (const chunk of Bun.stdin.stream()) {
|
|
260
|
+
buffer += decoder.decode(chunk);
|
|
261
|
+
let at = buffer.indexOf("\\n");
|
|
262
|
+
while (at !== -1) {
|
|
263
|
+
const message = JSON.parse(buffer.slice(0, at));
|
|
264
|
+
buffer = buffer.slice(at + 1);
|
|
265
|
+
at = buffer.indexOf("\\n");
|
|
266
|
+
if (message.method === "initialize") {
|
|
267
|
+
// A fork of mcode lists the steer request; the file "steer" makes the fake one. The file
|
|
268
|
+
// "fork" makes the fake the owner's fork, which serves the turn status and the history; the
|
|
269
|
+
// file "status" alone makes it serve the turn status.
|
|
270
|
+
const forked = existsSync(${JSON.stringify(join(dir, "fork"))});
|
|
271
|
+
const methods = [
|
|
272
|
+
...(existsSync(${JSON.stringify(join(dir, "steer"))}) ? ["mcode/session/steer"] : []),
|
|
273
|
+
...(forked || existsSync(${JSON.stringify(join(dir, "status"))}) ? ["mcode/session/turn_status"] : []),
|
|
274
|
+
...(forked ? ["session/history"] : []),
|
|
275
|
+
];
|
|
276
|
+
const meta = methods.length > 0 ? { "minimax-code/extensions": { version: 1, methods, notifications: [] } } : undefined;
|
|
277
|
+
send({ jsonrpc: "2.0", id: message.id, result: { protocolVersion: 1, _meta: meta } });
|
|
278
|
+
}
|
|
279
|
+
else if (message.method === "mcode/session/turn_status") {
|
|
280
|
+
// The file "foreign-turn" is a turn the daemon runs for a client that is gone. The file
|
|
281
|
+
// "release" makes the daemon drop the turn it holds without answering its prompt.
|
|
282
|
+
if (existsSync(${JSON.stringify(join(dir, "release"))})) active = undefined;
|
|
283
|
+
// The file "status-error" makes the request fail, as a build that lists it and cannot serve it.
|
|
284
|
+
if (existsSync(${JSON.stringify(join(dir, "status-error"))})) {
|
|
285
|
+
appendFileSync(${JSON.stringify(join(dir, "status.log"))}, "error\\n");
|
|
286
|
+
send({ jsonrpc: "2.0", id: message.id, error: { code: -32601, message: "Method not found" } });
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const busy = existsSync(${JSON.stringify(join(dir, "foreign-turn"))}) || active !== undefined;
|
|
290
|
+
appendFileSync(${JSON.stringify(join(dir, "status.log"))}, (busy ? "busy" : "idle") + "\\n");
|
|
291
|
+
send({ jsonrpc: "2.0", id: message.id, result: { activeTurn: busy ? { turnId: "t0", busyReason: "turn", locallyOwned: true } : null, latestActivity: null, queue: { pendingCount: 0, paused: false } } });
|
|
292
|
+
}
|
|
293
|
+
else if (message.method === "mcode/session/steer") {
|
|
294
|
+
// The file "steer-refuse" is what the fork answers a steer it did not deliver: invalid params.
|
|
295
|
+
if (existsSync("steer-refuse")) { send({ jsonrpc: "2.0", id: message.id, error: { code: -32602, message: "Steering was not delivered to the active ACP prompt Turn" } }); continue; }
|
|
296
|
+
appendFileSync(${JSON.stringify(join(dir, "steers.log"))}, JSON.stringify(message.params) + "\\n");
|
|
297
|
+
// The file "steer-fail" is a steer the fork took and then failed on: an internal error.
|
|
298
|
+
if (existsSync("steer-fail")) { send({ jsonrpc: "2.0", id: message.id, error: { code: -32603, message: "Internal error", data: "the receipt was not written" } }); continue; }
|
|
299
|
+
if (existsSync("steer-hang")) continue;
|
|
300
|
+
send({ jsonrpc: "2.0", id: message.id, result: { turnId: "t1", mode: "steered" } });
|
|
301
|
+
}
|
|
302
|
+
else if ((message.method === "session/new" || message.method === "session/load") && existsSync(${JSON.stringify(join(dir, "refuse-mode"))})) {
|
|
303
|
+
// The file "refuse-mode" is an old daemon: it cannot take the mode of a session.
|
|
304
|
+
send({ jsonrpc: "2.0", id: message.id, error: { code: -32603, message: "runtime does not support per-session permissionMode; restart mcoded" } });
|
|
305
|
+
}
|
|
306
|
+
else if (message.method === "session/new") {
|
|
307
|
+
appendFileSync(${JSON.stringify(join(dir, "sessions.log"))}, JSON.stringify(message.params) + "\\n");
|
|
308
|
+
send({ jsonrpc: "2.0", id: message.id, result: { sessionId: "fake-1" } });
|
|
309
|
+
}
|
|
310
|
+
else if (message.method === "session/load") {
|
|
311
|
+
appendFileSync(${JSON.stringify(join(dir, "sessions.log"))}, JSON.stringify(message.params) + "\\n");
|
|
312
|
+
// Real mcode answers session/load with no session id: the session is the one asked for.
|
|
313
|
+
// A test may make it name one, written in the file "load-id".
|
|
314
|
+
const named = existsSync(${JSON.stringify(join(dir, "load-id"))})
|
|
315
|
+
? { sessionId: readFileSync(${JSON.stringify(join(dir, "load-id"))}, "utf8") }
|
|
316
|
+
: {};
|
|
317
|
+
send({ jsonrpc: "2.0", id: message.id, result: { modes: {}, configOptions: [], ...named } });
|
|
318
|
+
}
|
|
319
|
+
else if (message.method === "session/prompt") {
|
|
320
|
+
appendFileSync(${JSON.stringify(join(dir, "prompts.log"))}, JSON.stringify(message.params) + "\\n");
|
|
321
|
+
// Like real mcode: a session whose daemon runs a turn for another client refuses a prompt.
|
|
322
|
+
if (existsSync(${JSON.stringify(join(dir, "foreign-turn"))})) { send({ jsonrpc: "2.0", id: message.id, error: { code: -32603, message: "Internal error: MiniMax Code refused the prompt (local_session_busy): Session already has an active Turn. Use queue send to deliver the message after it.", data: { code: "local_session_busy", retryable: false } } }); continue; }
|
|
323
|
+
// Like real mcode: one prompt at a time per session.
|
|
324
|
+
if (active !== undefined) { send({ jsonrpc: "2.0", id: message.id, error: { code: -32602, message: "Invalid params: A prompt is already active for this session." } }); continue; }
|
|
325
|
+
if (message.params.prompt[0].text === "die") process.exit(3);
|
|
326
|
+
// "stall" ends its turn and then stops reading its input for a while, as a stuck agent does.
|
|
327
|
+
if (message.params.prompt[0].text === "stall") {
|
|
328
|
+
send({ jsonrpc: "2.0", id: message.id, result: { stopReason: "end_turn" } });
|
|
329
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 4000);
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
// "orphan" ends its turn and exits, leaving a grandchild that holds the agent's stdout open: the
|
|
333
|
+
// process is gone and its input is closed, but the pipe of its output has not closed yet.
|
|
334
|
+
if (message.params.prompt[0].text === "orphan") {
|
|
335
|
+
send({ jsonrpc: "2.0", id: message.id, result: { stopReason: "end_turn" } });
|
|
336
|
+
Bun.spawn(["sleep", "300"], { stdout: "inherit", stderr: "inherit", stdin: "ignore" });
|
|
337
|
+
setTimeout(() => process.exit(0), 50);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
active = message.id;
|
|
341
|
+
// "hang" never ends on its own; a cancel ends it, a little later, as cancelled.
|
|
342
|
+
if (message.params.prompt[0].text === "hang") continue;
|
|
343
|
+
if (message.params.prompt[0].text.startsWith("give the word")) {
|
|
344
|
+
send({ jsonrpc: "2.0", method: "session/update", params: { sessionId: "fake-1", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: JSON.stringify({ word: "ok" }) } } } });
|
|
345
|
+
}
|
|
346
|
+
// The file "ask-permission" makes the turn ask for permission, with the fork's options, the
|
|
347
|
+
// lasting ones first. The file "only-always" leaves out the one-off ones.
|
|
348
|
+
if (existsSync(${JSON.stringify(join(dir, "ask-permission"))})) {
|
|
349
|
+
const options = [
|
|
350
|
+
{ optionId: "allow-always", name: "Always allow", kind: "allow_always" },
|
|
351
|
+
{ optionId: "deny-always", name: "Always deny", kind: "reject_always" },
|
|
352
|
+
{ optionId: "allow-once", name: "Allow", kind: "allow_once" },
|
|
353
|
+
{ optionId: "deny", name: "Deny", kind: "reject_once" },
|
|
354
|
+
];
|
|
355
|
+
// The file "questionnaire" makes it a question for the person, in the shape a client without form support gets it.
|
|
356
|
+
const questionnaire = [
|
|
357
|
+
{ optionId: "questionnaire:c9:red", name: "red", kind: "allow_once" },
|
|
358
|
+
{ optionId: "questionnaire:c9:blue", name: "blue", kind: "allow_once" },
|
|
359
|
+
{ optionId: "questionnaire:c9:cancel", name: "Cancel", kind: "reject_once" },
|
|
360
|
+
];
|
|
361
|
+
send({ jsonrpc: "2.0", id: "perm-1", method: "session/request_permission", params: { sessionId: "fake-1", toolCall: { toolCallId: "c9", title: existsSync(${JSON.stringify(join(dir, "questionnaire"))}) ? "Which colour?" : "Bash" }, options: existsSync(${JSON.stringify(join(dir, "questionnaire"))}) ? questionnaire : existsSync(${JSON.stringify(join(dir, "only-always"))}) ? options.slice(0, 2) : options } });
|
|
362
|
+
}
|
|
363
|
+
// The file "tool-calls" makes the turn report tool calls; the fork marks them with its decision.
|
|
364
|
+
if (existsSync(${JSON.stringify(join(dir, "tool-calls"))})) {
|
|
365
|
+
const update = (sessionUpdate, toolCallId, _meta) => send({ jsonrpc: "2.0", method: "session/update", params: { sessionId: "fake-1", update: { sessionUpdate, toolCallId, ...(sessionUpdate === "tool_call" ? { title: "Read", kind: "read", status: "pending" } : { status: "completed" }), ...(_meta === undefined ? {} : { _meta }) } } });
|
|
366
|
+
update("tool_call", "c1", { "minimax-code/permission": { decision: "allow", decidedBy: "classifier", ruleKind: null, mode: "auto", reason: "read only" } });
|
|
367
|
+
update("tool_call_update", "c1");
|
|
368
|
+
update("tool_call", "c2", { "minimax-code/permission": { decision: "maybe" } });
|
|
369
|
+
update("tool_call", "c3", { "minimax-code/permission": { decision: "deny", decidedBy: "rule", ruleKind: "builtin", mode: "default", reason: "denied by a rule" } });
|
|
370
|
+
update("tool_call", "c4", { "minimax-code/permission": { decision: "allow", decidedBy: "mode", mode: "bypassPermissions", reason: "bypassPermissions mode" } });
|
|
371
|
+
}
|
|
372
|
+
setTimeout(() => { if (active === message.id) { active = undefined; send({ jsonrpc: "2.0", id: message.id, result: { stopReason: "end_turn" } }); } }, 200);
|
|
373
|
+
} else if (message.method === undefined && message.id === "perm-1") {
|
|
374
|
+
appendFileSync(${JSON.stringify(join(dir, "permission.log"))}, JSON.stringify(message.result) + "\\n");
|
|
375
|
+
} else if (message.method === "session/cancel") {
|
|
376
|
+
// The file "deaf" makes the daemon ignore the cancel: the turn stays held.
|
|
377
|
+
if (existsSync(${JSON.stringify(join(dir, "deaf"))})) continue;
|
|
378
|
+
const cut = active;
|
|
379
|
+
if (cut !== undefined) setTimeout(() => { if (active === cut) { active = undefined; send({ jsonrpc: "2.0", id: cut, result: { stopReason: "cancelled" } }); } }, 300);
|
|
380
|
+
} else if (message.method === "session/set_config_option") {
|
|
381
|
+
appendFileSync(${JSON.stringify(join(dir, "config.log"))}, JSON.stringify(message.params) + "\\n");
|
|
382
|
+
// The file "refuse-effort" makes the agent refuse an effort, as the real one refuses a value its model does not advertise.
|
|
383
|
+
if (message.params.configId === "thinkingEffort" && existsSync(${JSON.stringify(join(dir, "refuse-effort"))})) {
|
|
384
|
+
send({ jsonrpc: "2.0", id: message.id, error: { code: -32602, message: "Invalid params: Thinking effort is not advertised for the selected model: " + message.params.value } });
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (message.params.configId === "model") model = message.params.value;
|
|
388
|
+
// The file "reports-model" makes the agent list the options as they stand, as the protocol has it: the current model among them.
|
|
389
|
+
const options = existsSync(${JSON.stringify(join(dir, "reports-model"))}) ? { configOptions: [{ id: "model", currentValue: model ?? "m:minimax:MiniMax-M3.1-Flash:u" }] } : {};
|
|
390
|
+
send({ jsonrpc: "2.0", id: message.id, result: options });
|
|
391
|
+
} else if (message.id !== undefined) send({ jsonrpc: "2.0", id: message.id, result: {} });
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
`;
|
|
395
|
+
|
|
396
|
+
/** The params of every `session/new` and `session/load` the fake agent was asked for. */
|
|
397
|
+
const sessionRequestsOf = async (): Promise<Array<Record<string, unknown>>> =>
|
|
398
|
+
(await readFile(join(dir, "sessions.log"), "utf8"))
|
|
399
|
+
.trim()
|
|
400
|
+
.split("\n")
|
|
401
|
+
.map((line) => JSON.parse(line) as Record<string, unknown>);
|
|
402
|
+
|
|
403
|
+
/** The params of every config option the fake agent was asked to set. */
|
|
404
|
+
const configCallsOf = async (): Promise<Array<Record<string, unknown>>> =>
|
|
405
|
+
(await readFile(join(dir, "config.log"), "utf8").catch(() => ""))
|
|
406
|
+
.trim()
|
|
407
|
+
.split("\n")
|
|
408
|
+
.filter((line) => line !== "")
|
|
409
|
+
.map((line) => JSON.parse(line) as Record<string, unknown>);
|
|
410
|
+
|
|
411
|
+
async function fakeControl(options?: {
|
|
412
|
+
lineage?: MinimaxLineage;
|
|
413
|
+
permissions?: "allow" | "deny";
|
|
414
|
+
steer?: boolean;
|
|
415
|
+
files?: readonly string[];
|
|
416
|
+
callTimeoutMs?: number;
|
|
417
|
+
cancelGraceMs?: number;
|
|
418
|
+
turnTimeoutMs?: number;
|
|
419
|
+
}): Promise<McodeSessionControl> {
|
|
420
|
+
dir = await tempDir("mcode-fake");
|
|
421
|
+
if (options?.steer === true) await writeFile(join(dir, "steer"), "");
|
|
422
|
+
for (const file of options?.files ?? []) await writeFile(join(dir, file), "");
|
|
423
|
+
// A turn status is the fork's: an agent that serves it is the fork, and this side is set up for it.
|
|
424
|
+
const lineage = options?.lineage ?? (options?.files?.includes("status") === true ? "fork" : undefined);
|
|
425
|
+
if (lineage === "fork") await writeFile(join(dir, "fork"), "");
|
|
426
|
+
const binary = join(dir, "mcode");
|
|
427
|
+
await writeFile(binary, FAKE_AGENT);
|
|
428
|
+
await chmod(binary, 0o755);
|
|
429
|
+
return mcodeControl({
|
|
430
|
+
mode: "full",
|
|
431
|
+
harness: harnessId("mcode-fake"),
|
|
432
|
+
binary,
|
|
433
|
+
...(lineage === undefined ? {} : { lineage }),
|
|
434
|
+
...(options?.permissions === undefined ? {} : { permissions: options.permissions }),
|
|
435
|
+
...(options?.callTimeoutMs === undefined ? {} : { callTimeoutMs: options.callTimeoutMs }),
|
|
436
|
+
...(options?.cancelGraceMs === undefined ? {} : { cancelGraceMs: options.cancelGraceMs }),
|
|
437
|
+
...(options?.turnTimeoutMs === undefined ? {} : { turnTimeoutMs: options.turnTimeoutMs }),
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
test("a message behind a running turn is queued with the turn it will run as, and runs after it", async () => {
|
|
442
|
+
control = await fakeControl();
|
|
443
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
444
|
+
const ended: number[] = [];
|
|
445
|
+
const watching = (async () => {
|
|
446
|
+
for await (const event of control.life(id)) {
|
|
447
|
+
if (event.kind === "turn-ended") {
|
|
448
|
+
ended.push(event.turn);
|
|
449
|
+
if (ended.length === 2) return;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
})();
|
|
453
|
+
const first = await control.send(id, "one");
|
|
454
|
+
const second = await control.send(id, "two");
|
|
455
|
+
expect(first).toMatchObject({ status: "started", turn: 1 });
|
|
456
|
+
expect(second).toMatchObject({ status: "queued", turn: 2 });
|
|
457
|
+
expect(typeof second.at).toBe("number");
|
|
458
|
+
await watching;
|
|
459
|
+
expect(ended).toEqual([1, 2]);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test("a message into the running turn of an mcode that serves steer is delivered into it", async () => {
|
|
463
|
+
control = await fakeControl({ steer: true });
|
|
464
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
465
|
+
const idle = await control.send(id, "one", { into: "turn" });
|
|
466
|
+
expect(idle.status).toBe("started");
|
|
467
|
+
const receipt = await control.send(id, "two", { into: "turn" });
|
|
468
|
+
expect(receipt).toMatchObject({ status: "delivered", turn: 1 });
|
|
469
|
+
const steers = (await readFile(join(dir, "steers.log"), "utf8")).trim().split("\n");
|
|
470
|
+
expect(steers.map((line) => JSON.parse(line))).toEqual([{ sessionId: "fake-1", text: "two" }]);
|
|
471
|
+
// Nothing was queued: the next plain send, after the turn, is the second turn.
|
|
472
|
+
await Bun.sleep(400);
|
|
473
|
+
expect(await control.send(id, "three")).toMatchObject({ status: "started", turn: 2 });
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test("a message into the running turn of an mcode without steer is queued, never refused", async () => {
|
|
477
|
+
control = await fakeControl();
|
|
478
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
479
|
+
expect((await control.send(id, "one", { into: "turn" })).status).toBe("started");
|
|
480
|
+
expect(await control.send(id, "two", { into: "turn" })).toMatchObject({ status: "queued", turn: 2 });
|
|
481
|
+
expect(await Bun.file(join(dir, "steers.log")).exists()).toBe(false);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
test("a steer that mcode refuses falls through to the queue, once", async () => {
|
|
485
|
+
control = await fakeControl({ steer: true, files: ["steer-refuse"] });
|
|
486
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
487
|
+
await control.send(id, "one", { into: "turn" });
|
|
488
|
+
expect(await control.send(id, "two", { into: "turn" })).toMatchObject({ status: "queued", turn: 2 });
|
|
489
|
+
expect(await Bun.file(join(dir, "steers.log")).exists()).toBe(false);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
test("a steer that fails after mcode may have taken it is not sent again and is reported as unknown", async () => {
|
|
493
|
+
control = await fakeControl({ steer: true, files: ["steer-fail"] });
|
|
494
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
495
|
+
await control.send(id, "one", { into: "turn" });
|
|
496
|
+
await expect(control.send(id, "two", { into: "turn" })).rejects.toMatchObject({ code: "InputPossiblySent" });
|
|
497
|
+
// The steer was the only delivery, and nothing was queued: after the turn the next send runs at once.
|
|
498
|
+
const steers = (await readFile(join(dir, "steers.log"), "utf8")).trim().split("\n");
|
|
499
|
+
expect(steers).toHaveLength(1);
|
|
500
|
+
await Bun.sleep(400);
|
|
501
|
+
expect(await control.send(id, "three")).toMatchObject({ status: "started", turn: 2 });
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
test("a steer that gets no reply is not sent again and is reported as unknown", async () => {
|
|
505
|
+
control = await fakeControl({ steer: true, files: ["steer-hang"], callTimeoutMs: 1_000 });
|
|
506
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
507
|
+
await control.send(id, "one", { into: "turn" });
|
|
508
|
+
await expect(control.send(id, "two", { into: "turn" })).rejects.toMatchObject({ code: "InputPossiblySent" });
|
|
509
|
+
// Nothing was queued: after the turn the next send is the second turn, and runs at once.
|
|
510
|
+
await Bun.sleep(1_100);
|
|
511
|
+
expect(await control.send(id, "three")).toMatchObject({ status: "started", turn: 2 });
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
test("malformed extension meta in the initialize reply means no steer, and no throw", () => {
|
|
515
|
+
expect(servesSteer({ _meta: "x" })).toBe(false);
|
|
516
|
+
expect(servesSteer({ _meta: { "minimax-code/extensions": { methods: "mcode/session/steer" } } })).toBe(false);
|
|
517
|
+
expect(servesSteer({ _meta: { "minimax-code/extensions": null } })).toBe(false);
|
|
518
|
+
expect(servesSteer({})).toBe(false);
|
|
519
|
+
expect(servesSteer({ _meta: { "minimax-code/extensions": { version: 1, methods: ["mcode/session/queue/list"], notifications: [] } } })).toBe(false);
|
|
520
|
+
expect(servesSteer({ _meta: { "minimax-code/extensions": { version: 1, methods: ["mcode/session/steer"], notifications: [] } } })).toBe(true);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
test("an interrupt of a session with no running turn is refused", async () => {
|
|
524
|
+
control = await fakeControl();
|
|
525
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
526
|
+
await expect(control.interrupt(id)).rejects.toThrow("no turn is running");
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
test("a child that dies in the first turn: the start has returned, and the stream of life ends", async () => {
|
|
530
|
+
control = await fakeControl();
|
|
531
|
+
const { session: id, first } = await control.start({ prompt: promptFromText("die"), cwd: dir });
|
|
532
|
+
expect(first).toMatchObject({ status: "started", turn: 1 });
|
|
533
|
+
// The death is a sign of life, not a failure of the start: the first stream sees the turn
|
|
534
|
+
// begin, and then the session is over.
|
|
535
|
+
const events: LifeEvent[] = [];
|
|
536
|
+
for await (const event of control.life(id)) events.push(event);
|
|
537
|
+
expect(events[0]).toMatchObject({ kind: "turn-started", turn: 1 });
|
|
538
|
+
expect(await control.state(id)).toBe("unknown");
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
test("the start returns before the first turn ends: a first turn that never ends does not block it", async () => {
|
|
542
|
+
control = await fakeControl();
|
|
543
|
+
const { session: id, first } = await control.start({ prompt: promptFromText("hang"), cwd: dir });
|
|
544
|
+
expect(id.native).toBe("fake-1" as typeof id.native);
|
|
545
|
+
// The prompt became the first turn, delivered like a send; it is still running.
|
|
546
|
+
expect(first).toMatchObject({ status: "started", turn: 1 });
|
|
547
|
+
expect(await control.state(id)).toBe("running");
|
|
548
|
+
// The first stream of life is given the start of that turn, which began before it existed.
|
|
549
|
+
const iterator = control.life(id)[Symbol.asyncIterator]();
|
|
550
|
+
expect((await iterator.next()).value).toMatchObject({ kind: "turn-started", turn: 1 });
|
|
551
|
+
void iterator.return?.();
|
|
552
|
+
// The session takes messages at once: they wait behind the first turn.
|
|
553
|
+
expect(await control.send(id, "two")).toMatchObject({ status: "queued", turn: 2 });
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
test("a question on a busy session waits behind the running turn and is answered after it", async () => {
|
|
557
|
+
control = await fakeControl();
|
|
558
|
+
const { session: id } = await control.start({ prompt: promptFromText("one"), cwd: dir });
|
|
559
|
+
const ended: number[] = [];
|
|
560
|
+
const watching = (async () => {
|
|
561
|
+
for await (const event of control.life(id)) {
|
|
562
|
+
if (event.kind !== "turn-ended") continue;
|
|
563
|
+
ended.push(event.turn);
|
|
564
|
+
if (ended.length === 3) return;
|
|
565
|
+
}
|
|
566
|
+
})();
|
|
567
|
+
expect(await control.send(id, "two")).toMatchObject({ status: "queued", turn: 2 });
|
|
568
|
+
const answer = await control.ask(id, {
|
|
569
|
+
schema: z.object({ word: z.string() }),
|
|
570
|
+
prompt: promptFromText("give the word"),
|
|
571
|
+
});
|
|
572
|
+
expect(answer.value.word).toBe("ok");
|
|
573
|
+
await watching;
|
|
574
|
+
// One turn after the other: the start's turn, the message, then the question.
|
|
575
|
+
expect(ended).toEqual([1, 2, 3]);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
test("a start or a resume that switches tools off is refused by name: mcode has no switch for them", async () => {
|
|
579
|
+
control = await fakeControl();
|
|
580
|
+
await expect(
|
|
581
|
+
control.start({ prompt: promptFromText(""), cwd: dir, visible: { actions: [], disabledTools: ["shell"] } }),
|
|
582
|
+
).rejects.toMatchObject({ code: "Unsupported" });
|
|
583
|
+
await expect(
|
|
584
|
+
control.resume(sessionId(harnessId("mcode"), "nope"), { visible: { actions: [], disabledTools: ["shell"] } }),
|
|
585
|
+
).rejects.toMatchObject({ code: "Unsupported" });
|
|
586
|
+
// No child was asked for a session.
|
|
587
|
+
expect(existsSync(join(dir, "sessions.log"))).toBe(false);
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
test("a start or a resume with a tool allowance is refused by name: mcode's ACP has no tool flag", async () => {
|
|
591
|
+
control = await fakeControl();
|
|
592
|
+
await expect(
|
|
593
|
+
control.start({ prompt: promptFromText(""), cwd: dir, tools: { allow: ["Read"] } }),
|
|
594
|
+
).rejects.toMatchObject({ code: "Unsupported" });
|
|
595
|
+
await expect(
|
|
596
|
+
control.resume(sessionId(harnessId("mcode"), "nope"), { tools: { allow: [] } }),
|
|
597
|
+
).rejects.toMatchObject({ code: "Unsupported" });
|
|
598
|
+
expect(existsSync(join(dir, "sessions.log"))).toBe(false);
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
test("every mcode session is a process of its own, in process mode, with the environment of its session", async () => {
|
|
602
|
+
control = await fakeControl();
|
|
603
|
+
await control.start({ prompt: promptFromText(""), cwd: dir, env: { NIWA_ID: "one" } });
|
|
604
|
+
await control.start({ prompt: promptFromText(""), cwd: dir, env: { NIWA_ID: "two" } });
|
|
605
|
+
const lines = (await readFile(join(dir, "env.log"), "utf8")).trim().split("\n").map((line) => JSON.parse(line));
|
|
606
|
+
expect(lines).toEqual([
|
|
607
|
+
{ mode: "off", id: "one" },
|
|
608
|
+
{ mode: "off", id: "two" },
|
|
609
|
+
]);
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
test("the MCP servers of a start are the mcpServers of the session, with the environment as pairs", async () => {
|
|
613
|
+
control = await fakeControl();
|
|
614
|
+
await control.start({
|
|
615
|
+
prompt: promptFromText(""),
|
|
616
|
+
cwd: dir,
|
|
617
|
+
servers: [
|
|
618
|
+
{
|
|
619
|
+
name: "niwa",
|
|
620
|
+
command: "bun",
|
|
621
|
+
args: ["bin/niwa.ts", "mcp"],
|
|
622
|
+
env: { NIWA_VISIBLE: "session.send" },
|
|
623
|
+
},
|
|
624
|
+
],
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
const requests = await sessionRequestsOf();
|
|
628
|
+
expect(requests).toHaveLength(1);
|
|
629
|
+
// The environment of the server is the one niwa named, and one more: the mark by which a stop
|
|
630
|
+
// finds the server, which mcode starts outside the group of the agent.
|
|
631
|
+
const servers = requests[0]?.["mcpServers"] as Array<{ env: Array<{ name: string; value: string }> }>;
|
|
632
|
+
expect(servers).toHaveLength(1);
|
|
633
|
+
expect(servers[0]).toMatchObject({ name: "niwa", command: "bun", args: ["bin/niwa.ts", "mcp"] });
|
|
634
|
+
expect(servers[0]?.env.find((entry) => entry.name === "NIWA_VISIBLE")?.value).toBe("session.send");
|
|
635
|
+
expect(servers[0]?.env.find((entry) => entry.name === "NIWA_MCP_OWNER")?.value).toMatch(/^[0-9a-f-]{36}$/);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
test("a session with no MCP servers is asked for with no servers", async () => {
|
|
639
|
+
control = await fakeControl();
|
|
640
|
+
await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
641
|
+
|
|
642
|
+
const requests = await sessionRequestsOf();
|
|
643
|
+
expect(requests[0]?.["mcpServers"]).toEqual([]);
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
test("close stops the process of the session, as the contract's release", async () => {
|
|
647
|
+
control = await fakeControl();
|
|
648
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
649
|
+
expect(await control.state(id)).not.toBe("unknown");
|
|
650
|
+
await control.close!(id);
|
|
651
|
+
expect(await control.state(id)).toBe("unknown");
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
test("a stop also stops the MCP servers the session was given, wherever the agent started them", async () => {
|
|
655
|
+
control = await fakeControl();
|
|
656
|
+
const { session: id } = await control.start({
|
|
657
|
+
prompt: promptFromText(""),
|
|
658
|
+
cwd: dir,
|
|
659
|
+
servers: [{ name: "niwa", command: "bun", args: ["bin/niwa.ts", "mcp"], env: {} }],
|
|
660
|
+
});
|
|
661
|
+
const requests = await sessionRequestsOf();
|
|
662
|
+
const servers = requests[0]?.["mcpServers"] as Array<{ env: Array<{ name: string; value: string }> }>;
|
|
663
|
+
const mark = servers[0]?.env.find((entry) => entry.name === "NIWA_MCP_OWNER")?.value ?? "";
|
|
664
|
+
expect(mark).not.toBe("");
|
|
665
|
+
|
|
666
|
+
// What mcode does: a server of the session, in a process group of its own, not the agent's child.
|
|
667
|
+
const server = Bun.spawn(["sleep", "300"], {
|
|
668
|
+
env: { ...process.env, NIWA_MCP_OWNER: mark },
|
|
669
|
+
stdio: ["ignore", "ignore", "ignore"],
|
|
670
|
+
});
|
|
671
|
+
// A server of another session carries another mark and is left alone.
|
|
672
|
+
const other = Bun.spawn(["sleep", "300"], {
|
|
673
|
+
env: { ...process.env, NIWA_MCP_OWNER: "00000000-0000-0000-0000-000000000000" },
|
|
674
|
+
stdio: ["ignore", "ignore", "ignore"],
|
|
675
|
+
});
|
|
676
|
+
try {
|
|
677
|
+
await control.stop(id);
|
|
678
|
+
await Promise.race([server.exited, Bun.sleep(5_000)]);
|
|
679
|
+
expect(server.killed || server.exitCode !== null || server.signalCode !== null).toBe(true);
|
|
680
|
+
expect(other.exitCode).toBeNull();
|
|
681
|
+
expect(other.signalCode).toBeNull();
|
|
682
|
+
} finally {
|
|
683
|
+
other.kill();
|
|
684
|
+
server.kill();
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
test("a session is resumed by a new process: session/load names no session, and the id stays the same", async () => {
|
|
689
|
+
control = await fakeControl();
|
|
690
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
691
|
+
// The process of the session dies with its host; the next control side loads it from disk.
|
|
692
|
+
await control.send(id, "die");
|
|
693
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
694
|
+
expect(await control.state(id)).toBe("unknown");
|
|
695
|
+
// The fake writes its log in its working directory, and a resumed process starts in ours.
|
|
696
|
+
const here = process.cwd();
|
|
697
|
+
process.chdir(dir);
|
|
698
|
+
try {
|
|
699
|
+
await control.resume(id);
|
|
700
|
+
} finally {
|
|
701
|
+
process.chdir(here);
|
|
702
|
+
}
|
|
703
|
+
const requests = await sessionRequestsOf();
|
|
704
|
+
expect(requests[1]?.["sessionId"]).toBe(id.native);
|
|
705
|
+
expect(await control.state(id)).toBe("idle");
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
test("a resumed session whose turn outlived its process is busy: a question waits behind that turn, and no prompt goes in while it runs", async () => {
|
|
709
|
+
control = await fakeControl({ files: ["status"] });
|
|
710
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
711
|
+
await control.send(id, "die");
|
|
712
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
713
|
+
// The daemon runs on the turn of the process that died.
|
|
714
|
+
await writeFile(join(dir, "foreign-turn"), "");
|
|
715
|
+
const here = process.cwd();
|
|
716
|
+
process.chdir(dir);
|
|
717
|
+
try {
|
|
718
|
+
await control.resume(id);
|
|
719
|
+
} finally {
|
|
720
|
+
process.chdir(here);
|
|
721
|
+
}
|
|
722
|
+
expect(await control.state(id)).toBe("running");
|
|
723
|
+
const events: LifeEvent[] = [];
|
|
724
|
+
const watching = (async () => {
|
|
725
|
+
for await (const event of control.life(id)) {
|
|
726
|
+
if (event.kind === "activity") continue;
|
|
727
|
+
events.push(event);
|
|
728
|
+
if (events.length === 4) return;
|
|
729
|
+
}
|
|
730
|
+
})();
|
|
731
|
+
const asked = control.ask(id, { schema: z.object({ word: z.string() }), prompt: promptFromText("give the word") });
|
|
732
|
+
// The question waits behind the turn that runs: a send says so.
|
|
733
|
+
expect(await control.send(id, "after")).toMatchObject({ status: "queued", turn: 3 });
|
|
734
|
+
await Bun.sleep(1_200);
|
|
735
|
+
expect(await control.state(id)).toBe("running");
|
|
736
|
+
// The turn that outlived its process ends; then the question goes out and is answered.
|
|
737
|
+
await rm(join(dir, "foreign-turn"));
|
|
738
|
+
const answer = await asked;
|
|
739
|
+
expect(answer.value.word).toBe("ok");
|
|
740
|
+
await watching;
|
|
741
|
+
expect(events.map((event) => [event.kind, "turn" in event ? event.turn : undefined])).toEqual([
|
|
742
|
+
["turn-started", 1],
|
|
743
|
+
["turn-ended", 1],
|
|
744
|
+
["turn-started", 2],
|
|
745
|
+
["turn-ended", 2],
|
|
746
|
+
]);
|
|
747
|
+
const statuses = (await readFile(join(dir, "status.log"), "utf8")).trim().split("\n");
|
|
748
|
+
expect(statuses[0]).toBe("busy");
|
|
749
|
+
expect(statuses.at(-1)).toBe("idle");
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
test("a cancelled turn the agent does not let go of stays running: what waits goes out only once the agent says no turn is active", async () => {
|
|
753
|
+
control = await fakeControl({ files: ["status", "deaf"], turnTimeoutMs: 600, cancelGraceMs: 500 });
|
|
754
|
+
const { session: id } = await control.start({ prompt: promptFromText("hang"), cwd: dir });
|
|
755
|
+
const details: string[] = [];
|
|
756
|
+
void (async () => {
|
|
757
|
+
for await (const event of control.life(id)) if (event.kind === "activity") details.push(event.detail ?? "");
|
|
758
|
+
})();
|
|
759
|
+
const asked = control.ask(id, { schema: z.object({ word: z.string() }), prompt: promptFromText("give the word") });
|
|
760
|
+
// Past the turn time and the grace of the cancel: the agent still holds the turn.
|
|
761
|
+
await eventually(() => details.includes("the cancelled turn was not let go: it is followed"), 10_000);
|
|
762
|
+
expect(await control.state(id)).toBe("running");
|
|
763
|
+
// The agent lets the turn go; the question goes out after it and is answered.
|
|
764
|
+
await writeFile(join(dir, "release"), "");
|
|
765
|
+
const answer = await asked;
|
|
766
|
+
expect(answer.value.word).toBe("ok");
|
|
767
|
+
// The turn that was cut off for its time did not end well: it is said to have failed.
|
|
768
|
+
expect(details.some((detail) => detail.includes("the turn failed: the turn did not end in time"))).toBe(true);
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
test("a turn the agent never lets go of, not even after its cancel, fails what waits behind it and still takes no prompt", async () => {
|
|
772
|
+
control = await fakeControl({ files: ["status", "deaf"], turnTimeoutMs: 300, cancelGraceMs: 1_000 });
|
|
773
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
774
|
+
await control.send(id, "die");
|
|
775
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
776
|
+
// The daemon holds a turn for a client that is gone, and never lets it go.
|
|
777
|
+
await writeFile(join(dir, "foreign-turn"), "");
|
|
778
|
+
const here = process.cwd();
|
|
779
|
+
process.chdir(dir);
|
|
780
|
+
try {
|
|
781
|
+
await control.resume(id);
|
|
782
|
+
} finally {
|
|
783
|
+
process.chdir(here);
|
|
784
|
+
}
|
|
785
|
+
const startedAt = Date.now();
|
|
786
|
+
let failure: unknown;
|
|
787
|
+
try {
|
|
788
|
+
await control.ask(id, { schema: z.object({ word: z.string() }), prompt: promptFromText("give the word") });
|
|
789
|
+
} catch (error) {
|
|
790
|
+
failure = error;
|
|
791
|
+
}
|
|
792
|
+
// The question is told which turn holds the session, inside the turn time and the grace; it
|
|
793
|
+
// never went in, so the agent did not refuse it.
|
|
794
|
+
expect(String(failure)).toMatch(/holds turn "t0"/);
|
|
795
|
+
expect(String(failure)).not.toMatch(/local_session_busy/);
|
|
796
|
+
expect(Date.now() - startedAt).toBeLessThan(3_000);
|
|
797
|
+
expect(await control.state(id)).toBe("running");
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
test("a resumed session with no active turn is idle, and a resume prompt runs as its turn", async () => {
|
|
801
|
+
control = await fakeControl({ files: ["status"] });
|
|
802
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
803
|
+
await control.send(id, "die");
|
|
804
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
805
|
+
const here = process.cwd();
|
|
806
|
+
process.chdir(dir);
|
|
807
|
+
try {
|
|
808
|
+
await control.resume(id);
|
|
809
|
+
} finally {
|
|
810
|
+
process.chdir(here);
|
|
811
|
+
}
|
|
812
|
+
expect(await control.state(id)).toBe("idle");
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
test("a resume with a prompt into a session whose turn outlived its process waits behind that turn", async () => {
|
|
816
|
+
control = await fakeControl({ files: ["status"] });
|
|
817
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
818
|
+
await control.send(id, "die");
|
|
819
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
820
|
+
await writeFile(join(dir, "foreign-turn"), "");
|
|
821
|
+
setTimeout(() => void rm(join(dir, "foreign-turn")), 1_000);
|
|
822
|
+
const here = process.cwd();
|
|
823
|
+
process.chdir(dir);
|
|
824
|
+
try {
|
|
825
|
+
// Refused with "local_session_busy" if it went in while the turn ran.
|
|
826
|
+
await control.resume(id, { prompt: promptFromText("two") });
|
|
827
|
+
} finally {
|
|
828
|
+
process.chdir(here);
|
|
829
|
+
}
|
|
830
|
+
expect(await control.state(id)).toBe("idle");
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
test("a question behind a turn of ours that the agent holds past its cancel fails once the grace is over, not a second grace later", async () => {
|
|
834
|
+
control = await fakeControl({ files: ["status", "deaf"], turnTimeoutMs: 300, cancelGraceMs: 2_000 });
|
|
835
|
+
const { session: id } = await control.start({ prompt: promptFromText("hang"), cwd: dir });
|
|
836
|
+
const startedAt = Date.now();
|
|
837
|
+
let failure: unknown;
|
|
838
|
+
try {
|
|
839
|
+
await control.ask(id, { schema: z.object({ word: z.string() }), prompt: promptFromText("give the word") });
|
|
840
|
+
} catch (error) {
|
|
841
|
+
failure = error;
|
|
842
|
+
}
|
|
843
|
+
const waited = Date.now() - startedAt;
|
|
844
|
+
expect(String(failure)).toMatch(/holds turn "t0"/);
|
|
845
|
+
// The cancel went at 0.3 s and the grace ended at 2.3 s: the next poll says so, and that is
|
|
846
|
+
// well before a second grace would be over (4.3 s), with room for a loaded machine.
|
|
847
|
+
expect(waited).toBeLessThan(3_600);
|
|
848
|
+
expect(await control.state(id)).toBe("running");
|
|
849
|
+
}, 15_000);
|
|
850
|
+
|
|
851
|
+
/** Resume a session whose process died, from the directory of the fake agent. */
|
|
852
|
+
async function resumeDead(control: McodeSessionControl, options: { prompt?: string; foreignTurn?: boolean } = {}): Promise<SessionId> {
|
|
853
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
854
|
+
await control.send(id, "die");
|
|
855
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
856
|
+
if (options.foreignTurn === true) await writeFile(join(dir, "foreign-turn"), "");
|
|
857
|
+
const here = process.cwd();
|
|
858
|
+
process.chdir(dir);
|
|
859
|
+
try {
|
|
860
|
+
await control.resume(id, options.prompt === undefined ? undefined : { prompt: promptFromText(options.prompt) });
|
|
861
|
+
} finally {
|
|
862
|
+
process.chdir(here);
|
|
863
|
+
}
|
|
864
|
+
return id;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
test("an agent that does not advertise the turn status is never asked for it: a resume goes on as it did", async () => {
|
|
868
|
+
control = await fakeControl();
|
|
869
|
+
// A turn the daemon holds, which an agent without the status cannot tell of.
|
|
870
|
+
const id = await resumeDead(control, { foreignTurn: true });
|
|
871
|
+
expect(await control.state(id)).toBe("idle");
|
|
872
|
+
expect(existsSync(join(dir, "status.log"))).toBe(false);
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
test("an agent that advertises the turn status and fails it is resumed as if it had none: no throw, no turn assumed", async () => {
|
|
876
|
+
control = await fakeControl({ files: ["status", "status-error"] });
|
|
877
|
+
const id = await resumeDead(control, { prompt: "two" });
|
|
878
|
+
expect(await control.state(id)).toBe("idle");
|
|
879
|
+
expect((await readFile(join(dir, "status.log"), "utf8")).trim().split("\n")).toEqual(["error"]);
|
|
880
|
+
});
|
|
881
|
+
|
|
882
|
+
test("a turn that is followed is no longer asked about once the control side is disposed", async () => {
|
|
883
|
+
control = await fakeControl({ files: ["status"] });
|
|
884
|
+
const id = await resumeDead(control, { foreignTurn: true });
|
|
885
|
+
expect(await control.state(id)).toBe("running");
|
|
886
|
+
await Bun.sleep(1_200);
|
|
887
|
+
await control.dispose();
|
|
888
|
+
// A poll that was in flight when the child went may still land: let it.
|
|
889
|
+
await Bun.sleep(700);
|
|
890
|
+
const asked = (await readFile(join(dir, "status.log"), "utf8")).trim().split("\n").length;
|
|
891
|
+
expect(asked).toBeGreaterThan(1);
|
|
892
|
+
await Bun.sleep(1_500);
|
|
893
|
+
expect((await readFile(join(dir, "status.log"), "utf8")).trim().split("\n")).toHaveLength(asked);
|
|
894
|
+
expect(await control.state(id)).toBe("unknown");
|
|
895
|
+
}, 15_000);
|
|
896
|
+
|
|
897
|
+
for (const [named, accepted] of [
|
|
898
|
+
["fake-1", true],
|
|
899
|
+
["another", false],
|
|
900
|
+
] as const) {
|
|
901
|
+
test(`a session/load that names ${named === "fake-1" ? "the same" : "another"} session is ${accepted ? "taken" : "refused"}`, async () => {
|
|
902
|
+
control = await fakeControl();
|
|
903
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
904
|
+
await control.send(id, "die");
|
|
905
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
906
|
+
await writeFile(join(dir, "load-id"), named);
|
|
907
|
+
// The fake reads its files in its working directory, and a resumed process starts in ours.
|
|
908
|
+
const here = process.cwd();
|
|
909
|
+
process.chdir(dir);
|
|
910
|
+
let failure: unknown;
|
|
911
|
+
try {
|
|
912
|
+
await control.resume(id);
|
|
913
|
+
} catch (error) {
|
|
914
|
+
failure = error;
|
|
915
|
+
} finally {
|
|
916
|
+
process.chdir(here);
|
|
917
|
+
}
|
|
918
|
+
if (accepted) {
|
|
919
|
+
expect(failure).toBeUndefined();
|
|
920
|
+
expect(await control.state(id)).toBe("idle");
|
|
921
|
+
} else {
|
|
922
|
+
expect(String(failure)).toMatch(/another session/);
|
|
923
|
+
expect(await control.state(id)).toBe("unknown");
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/** The arguments every spawn of the fake agent was given. */
|
|
929
|
+
const argvOf = async (): Promise<string[][]> =>
|
|
930
|
+
(await readFile(join(dir, "argv.log"), "utf8"))
|
|
931
|
+
.trim()
|
|
932
|
+
.split("\n")
|
|
933
|
+
.map((line) => JSON.parse(line) as string[]);
|
|
934
|
+
|
|
935
|
+
async function fakeKind(
|
|
936
|
+
options: { profile?: string; home?: string; control?: { profile?: string } },
|
|
937
|
+
make: typeof mcode | typeof ncode = mcode,
|
|
938
|
+
) {
|
|
939
|
+
dir = await tempDir("mcode-profile");
|
|
940
|
+
const binary = join(dir, "mcode");
|
|
941
|
+
await writeFile(binary, FAKE_AGENT);
|
|
942
|
+
await chmod(binary, 0o755);
|
|
943
|
+
if (make === ncode) await writeFile(join(dir, "fork"), "");
|
|
944
|
+
const kind = make({ home: join(dir, "home"), ...options, control: { mode: "full", ...options.control, binary } });
|
|
945
|
+
control = kind.control as McodeSessionControl;
|
|
946
|
+
await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
947
|
+
return argvOf();
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
test("without a profile the regular harness is started without one", async () => {
|
|
951
|
+
expect(await fakeKind({})).toEqual([["acp"]]);
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
test("upstream is never given an implicit --profile, even when its home holds a durable history", async () => {
|
|
955
|
+
dir = await tempDir("mcode-upstream-durable");
|
|
956
|
+
const home = join(dir, "home");
|
|
957
|
+
await mkdir(join(home, "durable-history"), { recursive: true });
|
|
958
|
+
await writeFile(join(home, "durable-history", "sessions.g1.db"), "");
|
|
959
|
+
const binary = join(dir, "mcode");
|
|
960
|
+
await writeFile(binary, FAKE_AGENT);
|
|
961
|
+
await chmod(binary, 0o755);
|
|
962
|
+
const kind = mcode({ home, control: { mode: "full", binary } });
|
|
963
|
+
control = kind.control as McodeSessionControl;
|
|
964
|
+
await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
965
|
+
expect(await argvOf()).toEqual([["acp"]]);
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
test("without a profile the fork (a home with a durable history) is started with --profile default", async () => {
|
|
969
|
+
dir = await tempDir("mcode-fork");
|
|
970
|
+
const forkHome = join(dir, "home");
|
|
971
|
+
await mkdir(join(forkHome, "durable-history"), { recursive: true });
|
|
972
|
+
await writeFile(join(forkHome, "durable-history", "sessions.g1.db"), "");
|
|
973
|
+
const binary = join(dir, "mcode");
|
|
974
|
+
await writeFile(binary, FAKE_AGENT);
|
|
975
|
+
await chmod(binary, 0o755);
|
|
976
|
+
await writeFile(join(dir, "fork"), "");
|
|
977
|
+
const kind = ncode({ home: forkHome, control: { mode: "full", binary } });
|
|
978
|
+
control = kind.control as McodeSessionControl;
|
|
979
|
+
await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
980
|
+
expect(await argvOf()).toEqual([["acp", "--profile", "default"]]);
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
test("the profile of the kind is the profile the agent is started with", async () => {
|
|
984
|
+
expect(await fakeKind({ profile: "work" }, ncode)).toEqual([["acp", "--profile", "work"]]);
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
test("a profile named in the control options is the profile of the kind", async () => {
|
|
988
|
+
expect(await fakeKind({ control: { profile: "work" } }, ncode)).toEqual([["acp", "--profile", "work"]]);
|
|
989
|
+
});
|
|
990
|
+
|
|
991
|
+
test("an explicit home and a profile are both kept", async () => {
|
|
992
|
+
const argv = await fakeKind({ home: "/somewhere/else", profile: "work" }, ncode);
|
|
993
|
+
expect(argv).toEqual([["acp", "--profile", "work"]]);
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
test("the published build has no --profile: a start or a resume that names a profile is refused before a process is run", async () => {
|
|
997
|
+
dir = await tempDir("mcode-profile");
|
|
998
|
+
const binary = join(dir, "mcode");
|
|
999
|
+
await writeFile(binary, FAKE_AGENT);
|
|
1000
|
+
await chmod(binary, 0o755);
|
|
1001
|
+
for (const kind of [mcode({ home: join(dir, "home"), profile: "work", control: { mode: "full", binary } })]) {
|
|
1002
|
+
control = kind.control as McodeSessionControl;
|
|
1003
|
+
await expect(control.start({ prompt: promptFromText(""), cwd: dir })).rejects.toMatchObject({ code: "Unsupported" });
|
|
1004
|
+
await expect(control.resume(sessionId(harnessId("mcode"), "mvs_x"))).rejects.toMatchObject({ code: "Unsupported" });
|
|
1005
|
+
expect(existsSync(join(dir, "argv.log"))).toBe(false);
|
|
1006
|
+
}
|
|
1007
|
+
// The fork takes it, and so does an upstream build that names none.
|
|
1008
|
+
expect(await fakeKind({ profile: "work" }, ncode)).toEqual([["acp", "--profile", "work"]]);
|
|
1009
|
+
});
|
|
1010
|
+
|
|
1011
|
+
test("the home follows the profile: default is ~/.minimax, any other is ~/.minimax-<profile>", () => {
|
|
1012
|
+
expect(homeNameOfProfile(undefined)).toBe(".minimax");
|
|
1013
|
+
expect(homeNameOfProfile("default")).toBe(".minimax");
|
|
1014
|
+
expect(homeNameOfProfile("work")).toBe(".minimax-work");
|
|
1015
|
+
});
|
|
1016
|
+
|
|
1017
|
+
test("the home of a profile is under the given base", () => {
|
|
1018
|
+
expect(homeOfProfile("work", "/h")).toBe("/h/.minimax-work");
|
|
1019
|
+
expect(homeOfProfile(undefined, "/h")).toBe("/h/.minimax");
|
|
1020
|
+
});
|
|
1021
|
+
|
|
1022
|
+
test("a turn over its time is cancelled, and what waited behind it goes out only after the agent let the turn go", async () => {
|
|
1023
|
+
control = await fakeControl({ turnTimeoutMs: 300 });
|
|
1024
|
+
const { session: id } = await control.start({ prompt: promptFromText("hang"), cwd: dir });
|
|
1025
|
+
const events: LifeEvent[] = [];
|
|
1026
|
+
const watching = (async () => {
|
|
1027
|
+
for await (const event of control.life(id)) {
|
|
1028
|
+
events.push(event);
|
|
1029
|
+
if (event.kind === "turn-ended" && event.turn === 2) return;
|
|
1030
|
+
}
|
|
1031
|
+
})();
|
|
1032
|
+
expect(await control.send(id, "two")).toMatchObject({ status: "queued", turn: 2 });
|
|
1033
|
+
await watching;
|
|
1034
|
+
const details = events.flatMap((event) => (event.kind === "activity" && event.detail !== undefined ? [event.detail] : []));
|
|
1035
|
+
// The first turn was cancelled for its time and failed; the message behind it was not sent as a
|
|
1036
|
+
// second prompt at once, so the agent took it and ended it.
|
|
1037
|
+
expect(details).toContain("the turn did not end in time: it was cancelled");
|
|
1038
|
+
expect(details).toContain("the turn failed: the turn did not end in time");
|
|
1039
|
+
expect(details.some((detail) => detail.includes("already active"))).toBe(false);
|
|
1040
|
+
expect(await control.state(id)).toBe("idle");
|
|
1041
|
+
});
|
|
1042
|
+
|
|
1043
|
+
test("an effort is the thinkingEffort option of that session, set after the model; the model is not an argument of the process", async () => {
|
|
1044
|
+
control = await fakeControl();
|
|
1045
|
+
await control.start({ prompt: promptFromText(""), cwd: dir, model: "minimax/MiniMax-M3.1-Flash", effort: "xhigh" });
|
|
1046
|
+
|
|
1047
|
+
expect(await configCallsOf()).toEqual([
|
|
1048
|
+
{ sessionId: "fake-1", configId: "permissionMode", value: "bypassPermissions" },
|
|
1049
|
+
{ sessionId: "fake-1", configId: "model", value: "m:minimax:MiniMax-M3.1-Flash:u" },
|
|
1050
|
+
{ sessionId: "fake-1", configId: "thinkingEffort", value: "xhigh" },
|
|
1051
|
+
]);
|
|
1052
|
+
expect((await readFile(join(dir, "argv.log"), "utf8")).trim()).toBe(JSON.stringify(["acp"]));
|
|
1053
|
+
});
|
|
1054
|
+
|
|
1055
|
+
test("the model the agent lists as current after the options are applied is a sign of life, and a resume asks for exactly that value", async () => {
|
|
1056
|
+
control = await fakeControl({ files: ["reports-model"] });
|
|
1057
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir, model: "minimax/MiniMax-M3.1-Flash#thinking" });
|
|
1058
|
+
// The agent names the model the way it keeps it, an option value, not the id the start asked for.
|
|
1059
|
+
const named = await firstModel(control, id);
|
|
1060
|
+
expect(named).toBe(modelName("m:minimax:MiniMax-M3.1-Flash:v:thinking"));
|
|
1061
|
+
|
|
1062
|
+
// The process dies, and the session is taken on again by a new one, which is asked for the model that was named.
|
|
1063
|
+
await control.send(id, "die");
|
|
1064
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
1065
|
+
const here = process.cwd();
|
|
1066
|
+
process.chdir(dir);
|
|
1067
|
+
try {
|
|
1068
|
+
await control.resume(id, { model: named });
|
|
1069
|
+
} finally {
|
|
1070
|
+
process.chdir(here);
|
|
1071
|
+
}
|
|
1072
|
+
expect((await configCallsOf()).filter((call) => call["configId"] === "model").map((call) => call["value"])).toEqual([
|
|
1073
|
+
"m:minimax:MiniMax-M3.1-Flash:v:thinking",
|
|
1074
|
+
"m:minimax:MiniMax-M3.1-Flash:v:thinking",
|
|
1075
|
+
]);
|
|
1076
|
+
expect(await firstModel(control, id)).toBe(named);
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
test("an agent that does not list its options names no model, and nothing is taken for one", async () => {
|
|
1080
|
+
control = await fakeControl();
|
|
1081
|
+
const { session: id } = await control.start({ prompt: promptFromText("one"), cwd: dir, model: "minimax/MiniMax-M3.1-Flash" });
|
|
1082
|
+
const seen: LifeEvent[] = [];
|
|
1083
|
+
for await (const event of control.life(id)) {
|
|
1084
|
+
seen.push(event);
|
|
1085
|
+
if (event.kind === "turn-ended") break;
|
|
1086
|
+
}
|
|
1087
|
+
expect(seen.filter((event) => event.kind === "model")).toEqual([]);
|
|
1088
|
+
});
|
|
1089
|
+
|
|
1090
|
+
/** The first model the first stream of life names. */
|
|
1091
|
+
async function firstModel(side: McodeSessionControl, id: SessionId): Promise<ModelName | undefined> {
|
|
1092
|
+
for await (const event of side.life(id)) {
|
|
1093
|
+
if (event.kind === "model") return event.model;
|
|
1094
|
+
}
|
|
1095
|
+
return undefined;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
for (const lineage of ["upstream", "fork"] as const) {
|
|
1099
|
+
test(`${lineage}: a working mode is the permissionMode of the session: ask is default, auto is auto, full is bypassPermissions`, async () => {
|
|
1100
|
+
control = await fakeControl({ lineage });
|
|
1101
|
+
for (const mode of ["ask", "auto", "full"] as const) {
|
|
1102
|
+
expect((await control.start({ prompt: promptFromText(""), cwd: dir, mode })).mode).toBe(mode);
|
|
1103
|
+
}
|
|
1104
|
+
expect((await configCallsOf()).map((call) => [call["configId"], call["value"]])).toEqual([
|
|
1105
|
+
["permissionMode", "default"],
|
|
1106
|
+
["permissionMode", "auto"],
|
|
1107
|
+
["permissionMode", "bypassPermissions"],
|
|
1108
|
+
]);
|
|
1109
|
+
});
|
|
1110
|
+
|
|
1111
|
+
test(`${lineage}: the instance's own mode is the default of its sessions, and auto when it names none`, async () => {
|
|
1112
|
+
dir = await tempDir("mcode-fake");
|
|
1113
|
+
const binary = join(dir, "mcode");
|
|
1114
|
+
await writeFile(binary, FAKE_AGENT);
|
|
1115
|
+
await chmod(binary, 0o755);
|
|
1116
|
+
if (lineage === "fork") await writeFile(join(dir, "fork"), "");
|
|
1117
|
+
control = mcodeControl({ harness: harnessId("mcode-fake"), binary, lineage });
|
|
1118
|
+
expect((await control.start({ prompt: promptFromText(""), cwd: dir })).mode).toBe("auto");
|
|
1119
|
+
await control.dispose();
|
|
1120
|
+
|
|
1121
|
+
control = mcodeControl({ harness: harnessId("mcode-fake"), binary, lineage, mode: "ask" });
|
|
1122
|
+
expect((await control.start({ prompt: promptFromText(""), cwd: dir })).mode).toBe("ask");
|
|
1123
|
+
expect((await configCallsOf()).map((call) => call["value"])).toEqual(["auto", "default"]);
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
test("upstream takes the working mode only through the config option; the fork takes it in session/new as well", async () => {
|
|
1128
|
+
control = await fakeControl({ lineage: "upstream" });
|
|
1129
|
+
await control.start({ prompt: promptFromText(""), cwd: dir, mode: "auto" });
|
|
1130
|
+
const [upstream] = await sessionRequestsOf();
|
|
1131
|
+
expect(upstream).not.toHaveProperty("_meta");
|
|
1132
|
+
await control.dispose();
|
|
1133
|
+
|
|
1134
|
+
control = await fakeControl({ lineage: "fork" });
|
|
1135
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir, mode: "auto" });
|
|
1136
|
+
await control.start({ prompt: promptFromText(""), cwd: dir, mode: "full" });
|
|
1137
|
+
const requests = await sessionRequestsOf();
|
|
1138
|
+
expect(requests.map((request) => request["_meta"])).toEqual([
|
|
1139
|
+
{ "minimax-code/permissionMode": "auto" },
|
|
1140
|
+
{ "minimax-code/permissionMode": "bypassPermissions" },
|
|
1141
|
+
]);
|
|
1142
|
+
expect((await configCallsOf()).map((call) => call["value"])).toEqual(["auto", "bypassPermissions"]);
|
|
1143
|
+
|
|
1144
|
+
// A resume names the mode again: the fork opens a loaded session in `default` otherwise.
|
|
1145
|
+
await control.send(id, "die");
|
|
1146
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
1147
|
+
const here = process.cwd();
|
|
1148
|
+
process.chdir(dir);
|
|
1149
|
+
try {
|
|
1150
|
+
await control.resume(id, { mode: "auto" });
|
|
1151
|
+
await control.send(id, "die");
|
|
1152
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
1153
|
+
await control.resume(id);
|
|
1154
|
+
} finally {
|
|
1155
|
+
process.chdir(here);
|
|
1156
|
+
}
|
|
1157
|
+
const loaded = (await sessionRequestsOf()).slice(2);
|
|
1158
|
+
expect(loaded.map((request) => request["sessionId"])).toEqual(["fake-1", "fake-1"]);
|
|
1159
|
+
expect(loaded[0]?.["_meta"]).toEqual({ "minimax-code/permissionMode": "auto" });
|
|
1160
|
+
// With no mode named, the session gets the instance's own mode (full here), never the fork's default.
|
|
1161
|
+
expect(loaded[1]?.["_meta"]).toEqual({ "minimax-code/permissionMode": "bypassPermissions" });
|
|
1162
|
+
});
|
|
1163
|
+
|
|
1164
|
+
test("upstream sends no _meta on session/load", async () => {
|
|
1165
|
+
control = await fakeControl({ lineage: "upstream" });
|
|
1166
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir, mode: "auto" });
|
|
1167
|
+
await control.send(id, "die");
|
|
1168
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
1169
|
+
const here = process.cwd();
|
|
1170
|
+
process.chdir(dir);
|
|
1171
|
+
try {
|
|
1172
|
+
await control.resume(id, { mode: "auto" });
|
|
1173
|
+
} finally {
|
|
1174
|
+
process.chdir(here);
|
|
1175
|
+
}
|
|
1176
|
+
expect((await sessionRequestsOf()).at(-1)).not.toHaveProperty("_meta");
|
|
1177
|
+
});
|
|
1178
|
+
|
|
1179
|
+
test("a daemon that cannot take the mode of a session fails the start and the resume with a clear error", async () => {
|
|
1180
|
+
control = await fakeControl({ lineage: "fork", files: ["refuse-mode"] });
|
|
1181
|
+
const started = await control.start({ prompt: promptFromText(""), cwd: dir }).catch((error: unknown) => error);
|
|
1182
|
+
expect(started).toBeInstanceOf(Error);
|
|
1183
|
+
expect((started as Error).message).toContain("restart mcoded");
|
|
1184
|
+
expect((started as Error).message).toContain("cannot take a permission mode per session");
|
|
1185
|
+
// The fake writes its files in its working directory, and a resumed process starts in ours.
|
|
1186
|
+
const here = process.cwd();
|
|
1187
|
+
process.chdir(dir);
|
|
1188
|
+
const resumed = await control
|
|
1189
|
+
.resume(sessionId(harnessId("mcode-fake"), "fake-1"))
|
|
1190
|
+
.catch((error: unknown) => error)
|
|
1191
|
+
.finally(() => process.chdir(here));
|
|
1192
|
+
expect(resumed).toBeInstanceOf(Error);
|
|
1193
|
+
expect((resumed as Error).message).toContain("restart mcoded");
|
|
1194
|
+
});
|
|
1195
|
+
|
|
1196
|
+
/** What niwa answered to the permission requests the fake agent sent in a turn. */
|
|
1197
|
+
async function permissionAnswers(
|
|
1198
|
+
permissions: "allow" | "deny",
|
|
1199
|
+
lineage: MinimaxLineage,
|
|
1200
|
+
files: readonly string[] = [],
|
|
1201
|
+
): Promise<unknown[]> {
|
|
1202
|
+
control = await fakeControl({ lineage, permissions, files: ["ask-permission", ...files] });
|
|
1203
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1204
|
+
const seen = firstTurnEnded(control.life(id));
|
|
1205
|
+
await control.send(id, "go");
|
|
1206
|
+
await seen;
|
|
1207
|
+
for (let i = 0; i < 100 && !existsSync(join(dir, "permission.log")); i += 1) await Bun.sleep(20);
|
|
1208
|
+
return (await readFile(join(dir, "permission.log"), "utf8"))
|
|
1209
|
+
.trim()
|
|
1210
|
+
.split("\n")
|
|
1211
|
+
.map((line) => JSON.parse(line) as unknown);
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
for (const lineage of ["upstream", "fork"] as const) {
|
|
1215
|
+
test(`${lineage}: a permission request is answered with the one-off option, never a lasting one`, async () => {
|
|
1216
|
+
const allowed = await permissionAnswers("allow", lineage);
|
|
1217
|
+
expect(allowed.at(-1)).toEqual({ outcome: { outcome: "selected", optionId: "allow-once" } });
|
|
1218
|
+
});
|
|
1219
|
+
|
|
1220
|
+
test(`${lineage}: a refused permission request picks the one-off reject option`, async () => {
|
|
1221
|
+
const denied = await permissionAnswers("deny", lineage);
|
|
1222
|
+
expect(denied.at(-1)).toEqual({ outcome: { outcome: "selected", optionId: "deny" } });
|
|
1223
|
+
});
|
|
1224
|
+
|
|
1225
|
+
for (const permissions of ["allow", "deny"] as const) {
|
|
1226
|
+
test(`${lineage}: when only lasting options are offered, a ${permissions === "allow" ? "granted" : "refused"} request is cancelled`, async () => {
|
|
1227
|
+
const answers = await permissionAnswers(permissions, lineage, ["only-always"]);
|
|
1228
|
+
expect(answers.at(-1)).toEqual({ outcome: { outcome: "cancelled" } });
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
/** The activity the signs of life of one turn carry. */
|
|
1234
|
+
async function activityOfTurn(lineage: MinimaxLineage) {
|
|
1235
|
+
control = await fakeControl({ lineage, files: ["tool-calls"] });
|
|
1236
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1237
|
+
const seen = firstTurnEnded(control.life(id));
|
|
1238
|
+
await control.send(id, "go");
|
|
1239
|
+
return (await seen).flatMap((event) => (event.kind === "activity" ? [event] : []));
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
test("the fork's permission mark on a tool call is a sign of life with the decision, as it was said, without its free-text reason", async () => {
|
|
1243
|
+
const marked = (await activityOfTurn("fork")).filter((event) => event.permission !== undefined);
|
|
1244
|
+
expect(marked.map((event) => [event.detail, event.permission])).toEqual([
|
|
1245
|
+
["tool_call", { decision: "allow", decidedBy: "classifier", ruleKind: null, mode: "auto" }],
|
|
1246
|
+
["tool_call", { decision: "deny", decidedBy: "rule", ruleKind: "builtin", mode: "default" }],
|
|
1247
|
+
// An older build sends no ruleKind: it reads as null.
|
|
1248
|
+
["tool_call", { decision: "allow", decidedBy: "mode", ruleKind: null, mode: "bypassPermissions" }],
|
|
1249
|
+
]);
|
|
1250
|
+
});
|
|
1251
|
+
|
|
1252
|
+
test("a tool call of the fork with a mark that is not of the shape, or with none, is plain activity", async () => {
|
|
1253
|
+
const toolCalls = (await activityOfTurn("fork")).filter((event) => event.detail?.startsWith("tool_call"));
|
|
1254
|
+
// c1 marked, its update unmarked, c2 malformed, c3 and c4 marked.
|
|
1255
|
+
expect(toolCalls.map((event) => event.permission?.decidedBy)).toEqual(["classifier", undefined, undefined, "rule", "mode"]);
|
|
1256
|
+
});
|
|
1257
|
+
|
|
1258
|
+
test("upstream does not read the permission mark", async () => {
|
|
1259
|
+
const toolCalls = (await activityOfTurn("upstream")).filter((event) => event.detail?.startsWith("tool_call"));
|
|
1260
|
+
expect(toolCalls).toHaveLength(5);
|
|
1261
|
+
expect(toolCalls.every((event) => event.permission === undefined)).toBe(true);
|
|
1262
|
+
});
|
|
1263
|
+
|
|
1264
|
+
test("a binary that cannot run names itself in the failure", async () => {
|
|
1265
|
+
dir = await tempDir("mcode-fake");
|
|
1266
|
+
const binary = join(dir, "ncode-broken");
|
|
1267
|
+
await writeFile(binary, "#!/bin/sh\necho 'could not load its native SQLite dependency' >&2\nexit 1\n");
|
|
1268
|
+
await chmod(binary, 0o755);
|
|
1269
|
+
control = mcodeControl({ harness: harnessId("ncode-fake"), binary, lineage: "fork" });
|
|
1270
|
+
await expect(control.start({ prompt: promptFromText(""), cwd: dir })).rejects.toThrow(
|
|
1271
|
+
new RegExp(`${binary} exited with code 1: could not load its native SQLite dependency`),
|
|
1272
|
+
);
|
|
1273
|
+
});
|
|
1274
|
+
|
|
1275
|
+
test("an effort the agent does not advertise fails the start, and an effort the kind has not got is refused before a process is started", async () => {
|
|
1276
|
+
control = await fakeControl({ files: ["refuse-effort"] });
|
|
1277
|
+
await expect(control.start({ prompt: promptFromText(""), cwd: dir, effort: "low" })).rejects.toThrow(
|
|
1278
|
+
/Thinking effort is not advertised for the selected model: low/,
|
|
1279
|
+
);
|
|
1280
|
+
|
|
1281
|
+
const failure = await control.start({ prompt: promptFromText(""), cwd: dir, effort: "extreme" }).catch((error: unknown) => error);
|
|
1282
|
+
expect(failure).toBeInstanceOf(Unsupported);
|
|
1283
|
+
expect(String(failure)).toContain('effort "extreme" is not one of default, low, medium, high, xhigh, max');
|
|
1284
|
+
expect((await readFile(join(dir, "argv.log"), "utf8")).trim().split("\n")).toHaveLength(1);
|
|
1285
|
+
});
|
|
1286
|
+
|
|
1287
|
+
test.skipIf(!(live && Bun.which("mcode")))(
|
|
1288
|
+
"the real agent accepts an effort on a MiniMax Flash session and ends a tiny turn",
|
|
1289
|
+
async () => {
|
|
1290
|
+
dir = await tempDir("mcode-effort");
|
|
1291
|
+
control = mcodeControl({ mode: "full", harness: harnessId("mcode-proof"), turnTimeoutMs: 120_000 });
|
|
1292
|
+
const { session: id } = await control.start({
|
|
1293
|
+
prompt: promptFromText("reply with ok"),
|
|
1294
|
+
cwd: dir,
|
|
1295
|
+
model: "minimax/MiniMax-M3.1-Flash",
|
|
1296
|
+
effort: "low",
|
|
1297
|
+
});
|
|
1298
|
+
expect(turnOf(await firstTurnEnded(control.life(id)), "turn-ended")).toBe(1);
|
|
1299
|
+
},
|
|
1300
|
+
180_000,
|
|
1301
|
+
);
|
|
1302
|
+
|
|
1303
|
+
test.skipIf(!(live && Bun.which("mcode")))(
|
|
1304
|
+
"the real upstream agent takes the mode auto and ends a tiny turn on MiniMax Flash",
|
|
1305
|
+
async () => {
|
|
1306
|
+
dir = await tempDir("mcode-auto");
|
|
1307
|
+
const binary = process.env["MCODE_BIN"];
|
|
1308
|
+
control = mcodeControl({
|
|
1309
|
+
harness: harnessId("mcode-proof"),
|
|
1310
|
+
turnTimeoutMs: 120_000,
|
|
1311
|
+
stopGraceMs: 500,
|
|
1312
|
+
...(binary === undefined ? {} : { binary }),
|
|
1313
|
+
});
|
|
1314
|
+
const { session: id, mode } = await control.start({
|
|
1315
|
+
prompt: promptFromText("reply with ok"),
|
|
1316
|
+
cwd: dir,
|
|
1317
|
+
model: "minimax/MiniMax-M3.1-Flash",
|
|
1318
|
+
mode: "auto",
|
|
1319
|
+
});
|
|
1320
|
+
expect(mode).toBe("auto");
|
|
1321
|
+
expect(turnOf(await firstTurnEnded(control.life(id)), "turn-ended")).toBe(1);
|
|
1322
|
+
},
|
|
1323
|
+
180_000,
|
|
1324
|
+
);
|
|
1325
|
+
|
|
1326
|
+
// -- what a resume, an exit and a request leave behind -------------------------------------------------
|
|
1327
|
+
|
|
1328
|
+
test("a session that lost its process is resumed in the directory it is resumed with", async () => {
|
|
1329
|
+
control = await fakeControl();
|
|
1330
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1331
|
+
await control.send(id, "die");
|
|
1332
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
1333
|
+
const workspace = join(dir, "workspace");
|
|
1334
|
+
await mkdir(workspace);
|
|
1335
|
+
await control.resume(id, { cwd: workspace });
|
|
1336
|
+
// The fake keeps its files in its own working directory: the one it was started in.
|
|
1337
|
+
const loads = (await readFile(join(workspace, "sessions.log"), "utf8")).trim().split("\n").map((line) => JSON.parse(line) as Record<string, unknown>);
|
|
1338
|
+
expect(loads[0]?.["cwd"]).toBe(workspace);
|
|
1339
|
+
expect(loads[0]?.["sessionId"]).toBe(id.native);
|
|
1340
|
+
expect(await control.state(id)).toBe("idle");
|
|
1341
|
+
});
|
|
1342
|
+
|
|
1343
|
+
test("an agent that dies on its own takes the MCP servers it was given with it", async () => {
|
|
1344
|
+
control = await fakeControl();
|
|
1345
|
+
const { session: id } = await control.start({
|
|
1346
|
+
prompt: promptFromText(""),
|
|
1347
|
+
cwd: dir,
|
|
1348
|
+
servers: [{ name: "niwa", command: "bun", args: ["bin/niwa.ts", "mcp"], env: {} }],
|
|
1349
|
+
});
|
|
1350
|
+
const servers = (await sessionRequestsOf())[0]?.["mcpServers"] as Array<{ env: Array<{ name: string; value: string }> }>;
|
|
1351
|
+
const mark = servers[0]?.env.find((entry) => entry.name === "NIWA_MCP_OWNER")?.value ?? "";
|
|
1352
|
+
const server = Bun.spawn(["sleep", "300"], {
|
|
1353
|
+
env: { ...process.env, NIWA_MCP_OWNER: mark },
|
|
1354
|
+
stdio: ["ignore", "ignore", "ignore"],
|
|
1355
|
+
});
|
|
1356
|
+
try {
|
|
1357
|
+
await control.send(id, "die");
|
|
1358
|
+
await Promise.race([server.exited, Bun.sleep(5_000)]);
|
|
1359
|
+
expect(server.exitCode !== null || server.signalCode !== null).toBe(true);
|
|
1360
|
+
} finally {
|
|
1361
|
+
server.kill();
|
|
1362
|
+
}
|
|
1363
|
+
});
|
|
1364
|
+
|
|
1365
|
+
test("a model that is not advertised is never replaced by another provider's or another variant", () => {
|
|
1366
|
+
const advertised = [
|
|
1367
|
+
{ value: "m:providerA:model:v:fast" },
|
|
1368
|
+
{ value: "m:providerA:model:v:deep" },
|
|
1369
|
+
{ value: "m:providerB:model:v:fast" },
|
|
1370
|
+
];
|
|
1371
|
+
// The variant that was named is the variant that is asked for, or none is.
|
|
1372
|
+
expect(mcodeModelOptionValue("providerB/model#deep", advertised)).toBe("m:providerB:model:v:deep");
|
|
1373
|
+
expect(mcodeModelOptionValue("providerA/model#slow", advertised)).toBe("m:providerA:model:v:slow");
|
|
1374
|
+
// A provider that was named is the provider that is asked for.
|
|
1375
|
+
expect(mcodeModelOptionValue("providerC/model", [{ value: "m:providerA:model:v:fast" }])).toBe("m:providerC:model:u");
|
|
1376
|
+
// Without a variant, a named one of the same provider and model is taken.
|
|
1377
|
+
expect(mcodeModelOptionValue("providerB/model", advertised)).toBe("m:providerB:model:v:fast");
|
|
1378
|
+
// Without a provider, the model alone says which.
|
|
1379
|
+
expect(mcodeModelOptionValue("model", advertised)).toBe("m:providerA:model:v:fast");
|
|
1380
|
+
});
|
|
1381
|
+
|
|
1382
|
+
test("a question for the person that arrives as a permission request is never answered by a policy", async () => {
|
|
1383
|
+
for (const permissions of ["allow", "deny"] as const) {
|
|
1384
|
+
const answers = await permissionAnswers(permissions, "upstream", ["questionnaire"]);
|
|
1385
|
+
expect(answers.at(-1)).toEqual({ outcome: { outcome: "cancelled" } });
|
|
1386
|
+
await control?.dispose();
|
|
1387
|
+
control = undefined;
|
|
1388
|
+
await rm(dir, { recursive: true, force: true });
|
|
1389
|
+
}
|
|
1390
|
+
});
|
|
1391
|
+
|
|
1392
|
+
test("a question for the person is not handed to a policy function as a tool", async () => {
|
|
1393
|
+
const asked: string[] = [];
|
|
1394
|
+
dir = await tempDir("mcode-fake");
|
|
1395
|
+
for (const file of ["ask-permission", "questionnaire"]) await writeFile(join(dir, file), "");
|
|
1396
|
+
const binary = join(dir, "mcode");
|
|
1397
|
+
await writeFile(binary, FAKE_AGENT);
|
|
1398
|
+
await chmod(binary, 0o755);
|
|
1399
|
+
control = mcodeControl({
|
|
1400
|
+
mode: "full",
|
|
1401
|
+
harness: harnessId("mcode-fake"),
|
|
1402
|
+
binary,
|
|
1403
|
+
permissions: async (request) => {
|
|
1404
|
+
asked.push(request.tool);
|
|
1405
|
+
return { behavior: "allow" };
|
|
1406
|
+
},
|
|
1407
|
+
});
|
|
1408
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1409
|
+
const seen = firstTurnEnded(control.life(id));
|
|
1410
|
+
await control.send(id, "go");
|
|
1411
|
+
await seen;
|
|
1412
|
+
for (let i = 0; i < 100 && !existsSync(join(dir, "permission.log")); i += 1) await Bun.sleep(20);
|
|
1413
|
+
expect(asked).toEqual([]);
|
|
1414
|
+
expect((await readFile(join(dir, "permission.log"), "utf8")).trim()).toBe(JSON.stringify({ outcome: { outcome: "cancelled" } }));
|
|
1415
|
+
});
|
|
1416
|
+
|
|
1417
|
+
for (const [label, bound, expected] of [
|
|
1418
|
+
["its home", (home: string) => mcodeKind.bind({ id: "mcode-fake", home }), (home: string) => home],
|
|
1419
|
+
["the home of its profile", () => ncodeKind.bind({ id: "mcode-fake", profile: "work" }), () => homeOfProfile("work")],
|
|
1420
|
+
["the default home", () => mcodeKind.bind({ id: "mcode-fake" }), () => homeOfProfile(undefined)],
|
|
1421
|
+
] as const) {
|
|
1422
|
+
test(`the profile of an installation writes the directory it keeps its sessions in: ${label}`, () => {
|
|
1423
|
+
const home = "/srv/install-home";
|
|
1424
|
+
const profile = resolveProfile(bound(home).profile ?? {}, "mcode-fake", { home: homedir(), env: {} });
|
|
1425
|
+
expect(profile.write).toEqual([expected(home)]);
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
test("a start that is still launching its process when the control side is disposed does not complete", async () => {
|
|
1430
|
+
control = await fakeControl();
|
|
1431
|
+
const before = directChildren();
|
|
1432
|
+
const started = control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1433
|
+
const outcome = started.then(
|
|
1434
|
+
() => "started",
|
|
1435
|
+
(error: unknown) => error,
|
|
1436
|
+
);
|
|
1437
|
+
await control.dispose();
|
|
1438
|
+
const result = await outcome;
|
|
1439
|
+
expect(result).not.toBe("started");
|
|
1440
|
+
expect(result).toMatchObject({ code: "Unavailable" });
|
|
1441
|
+
// Nothing it launched is left running.
|
|
1442
|
+
const deadline = Date.now() + 5_000;
|
|
1443
|
+
const leftover = () => directChildren().filter((child) => !before.some((old) => old.pid === child.pid));
|
|
1444
|
+
while (leftover().length > 0 && Date.now() < deadline) await Bun.sleep(50);
|
|
1445
|
+
expect(leftover()).toEqual([]);
|
|
1446
|
+
});
|
|
1447
|
+
|
|
1448
|
+
test("two resumes of a session that lost its process share one new process", async () => {
|
|
1449
|
+
control = await fakeControl();
|
|
1450
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1451
|
+
await control.send(id, "die");
|
|
1452
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
1453
|
+
const here = process.cwd();
|
|
1454
|
+
process.chdir(dir);
|
|
1455
|
+
try {
|
|
1456
|
+
// The second one carries a prompt: it runs on the process the first one brought up.
|
|
1457
|
+
await Promise.all([control.resume(id), control.resume(id, { prompt: promptFromText("reply with ok") })]);
|
|
1458
|
+
} finally {
|
|
1459
|
+
process.chdir(here);
|
|
1460
|
+
}
|
|
1461
|
+
expect(await argvOf()).toHaveLength(2);
|
|
1462
|
+
expect(await sessionRequestsOf()).toHaveLength(2);
|
|
1463
|
+
// The session is held by the one process that is left, and stopping it leaves nothing running.
|
|
1464
|
+
expect(await control.state(id)).not.toBe("unknown");
|
|
1465
|
+
await control.stop(id);
|
|
1466
|
+
expect(await control.state(id)).toBe("unknown");
|
|
1467
|
+
});
|
|
1468
|
+
|
|
1469
|
+
test("a session whose turn the agent holds past its cancel takes no new work until the turn is over", async () => {
|
|
1470
|
+
control = await fakeControl({ files: ["status", "deaf"], turnTimeoutMs: 300, cancelGraceMs: 600 });
|
|
1471
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1472
|
+
await control.send(id, "die");
|
|
1473
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "unknown"; i += 1) await Bun.sleep(50);
|
|
1474
|
+
await writeFile(join(dir, "foreign-turn"), "");
|
|
1475
|
+
const here = process.cwd();
|
|
1476
|
+
process.chdir(dir);
|
|
1477
|
+
try {
|
|
1478
|
+
await control.resume(id);
|
|
1479
|
+
} finally {
|
|
1480
|
+
process.chdir(here);
|
|
1481
|
+
}
|
|
1482
|
+
const schema = z.object({ word: z.string() });
|
|
1483
|
+
// The question that waited behind the held turn is told which turn holds the session.
|
|
1484
|
+
await expect(control.ask(id, { schema, prompt: promptFromText("give the word") })).rejects.toThrow(/holds turn "t0"/);
|
|
1485
|
+
|
|
1486
|
+
// Work that comes after is refused at once, not queued behind a turn that may never end.
|
|
1487
|
+
const late = Promise.race([
|
|
1488
|
+
control.ask(id, { schema, prompt: promptFromText("give the word") }).then(() => "answered", (error: unknown) => error),
|
|
1489
|
+
Bun.sleep(3_000).then(() => "waiting"),
|
|
1490
|
+
]);
|
|
1491
|
+
expect(await late).toMatchObject({ code: "Unavailable" });
|
|
1492
|
+
await expect(control.send(id, "more")).rejects.toMatchObject({ code: "InputNotSent" });
|
|
1493
|
+
await expect(control.resume(id, { prompt: promptFromText("more") })).rejects.toMatchObject({ code: "Unavailable" });
|
|
1494
|
+
expect(await control.state(id)).toBe("running");
|
|
1495
|
+
|
|
1496
|
+
// The agent lets the turn go: the session takes work again.
|
|
1497
|
+
await rm(join(dir, "foreign-turn"));
|
|
1498
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "idle"; i += 1) await Bun.sleep(50);
|
|
1499
|
+
expect(await control.state(id)).toBe("idle");
|
|
1500
|
+
expect((await control.send(id, "again")).status).toBe("started");
|
|
1501
|
+
});
|
|
1502
|
+
|
|
1503
|
+
test("a binary of the other lineage is refused in the handshake, before a session is opened", async () => {
|
|
1504
|
+
// The published build asked as the fork: it does not serve the fork's requests.
|
|
1505
|
+
control = await fakeControl({ lineage: "upstream", steer: true });
|
|
1506
|
+
const forkSide = mcodeControl({ mode: "full", harness: harnessId("mcode-fake"), binary: join(dir, "mcode"), lineage: "fork" });
|
|
1507
|
+
await expect(forkSide.start({ prompt: promptFromText(""), cwd: dir })).rejects.toMatchObject({ code: "Unavailable" });
|
|
1508
|
+
await expect(forkSide.start({ prompt: promptFromText(""), cwd: dir })).rejects.toThrow(/published build/);
|
|
1509
|
+
await forkSide.dispose();
|
|
1510
|
+
expect(existsSync(join(dir, "sessions.log"))).toBe(false);
|
|
1511
|
+
|
|
1512
|
+
// The fork asked as the published build.
|
|
1513
|
+
await writeFile(join(dir, "fork"), "");
|
|
1514
|
+
const upstreamSide = mcodeControl({ mode: "full", harness: harnessId("mcode-fake"), binary: join(dir, "mcode"), lineage: "upstream" });
|
|
1515
|
+
await expect(upstreamSide.start({ prompt: promptFromText(""), cwd: dir })).rejects.toThrow(/the fork/);
|
|
1516
|
+
await expect(upstreamSide.resume(sessionId(harnessId("mcode-fake"), "mvs_x"), { cwd: dir })).rejects.toMatchObject({ code: "Unavailable" });
|
|
1517
|
+
await upstreamSide.dispose();
|
|
1518
|
+
expect(existsSync(join(dir, "sessions.log"))).toBe(false);
|
|
1519
|
+
});
|
|
1520
|
+
|
|
1521
|
+
test("a message to an agent that has exited but whose output has not closed yet is not sent", async () => {
|
|
1522
|
+
control = await fakeControl();
|
|
1523
|
+
const before = directChildren();
|
|
1524
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1525
|
+
const agent = directChildren().find((child) => !before.some((old) => old.pid === child.pid));
|
|
1526
|
+
expect(agent).toBeDefined();
|
|
1527
|
+
expect((await control.send(id, "orphan")).status).toBe("started");
|
|
1528
|
+
// The process is gone; the grandchild keeps the pipe of its output open, so the session has not
|
|
1529
|
+
// been told it ended.
|
|
1530
|
+
for (let i = 0; i < 100 && directChildren().some((child) => child.pid === agent?.pid); i += 1) await Bun.sleep(50);
|
|
1531
|
+
expect(directChildren().some((child) => child.pid === agent?.pid)).toBe(false);
|
|
1532
|
+
expect(await control.state(id)).not.toBe("unknown");
|
|
1533
|
+
await expect(control.send(id, "again")).rejects.toMatchObject({ code: "InputNotSent" });
|
|
1534
|
+
await control.dispose();
|
|
1535
|
+
control = undefined;
|
|
1536
|
+
});
|
|
1537
|
+
|
|
1538
|
+
test("a large message to an agent that does not read its input is not waited for without end", async () => {
|
|
1539
|
+
control = await fakeControl({ callTimeoutMs: 500 });
|
|
1540
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1541
|
+
await control.send(id, "stall");
|
|
1542
|
+
for (let i = 0; i < 100 && (await control.state(id)) !== "idle"; i += 1) await Bun.sleep(20);
|
|
1543
|
+
const asked = Date.now();
|
|
1544
|
+
// Part of it may be read once the agent reads again, so it is not reported as unsent.
|
|
1545
|
+
await expect(control.send(id, "x".repeat(4_000_000))).rejects.toMatchObject({ code: "InputPossiblySent" });
|
|
1546
|
+
expect(Date.now() - asked).toBeLessThan(3_000);
|
|
1547
|
+
});
|
|
1548
|
+
|
|
1549
|
+
test("a turn the agent answered says it completed, and one an interrupt cut says it failed", async () => {
|
|
1550
|
+
control = await fakeControl();
|
|
1551
|
+
const { session: id } = await control.start({ prompt: promptFromText(""), cwd: dir });
|
|
1552
|
+
const ends: Array<[number, string | undefined]> = [];
|
|
1553
|
+
const watching = (async () => {
|
|
1554
|
+
for await (const event of control.life(id)) {
|
|
1555
|
+
if (event.kind === "turn-ended") {
|
|
1556
|
+
ends.push([event.turn, event.outcome]);
|
|
1557
|
+
if (ends.length === 2) return;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
})();
|
|
1561
|
+
await control.send(id, "one");
|
|
1562
|
+
await eventually(() => ends.length === 1, 10_000);
|
|
1563
|
+
await control.send(id, "hang");
|
|
1564
|
+
await eventually(async () => (await control.state(id)) === "running", 10_000);
|
|
1565
|
+
await control.interrupt(id);
|
|
1566
|
+
await watching;
|
|
1567
|
+
expect(ends).toEqual([
|
|
1568
|
+
[1, "completed"],
|
|
1569
|
+
[2, "failed"],
|
|
1570
|
+
]);
|
|
1571
|
+
});
|
|
1572
|
+
|
|
1573
|
+
test("a turn cut off for its time says it failed", async () => {
|
|
1574
|
+
control = await fakeControl({ files: ["status", "deaf"], turnTimeoutMs: 600, cancelGraceMs: 500 });
|
|
1575
|
+
const { session: id } = await control.start({ prompt: promptFromText("hang"), cwd: dir });
|
|
1576
|
+
const outcomes: Array<string | undefined> = [];
|
|
1577
|
+
void (async () => {
|
|
1578
|
+
for await (const event of control.life(id)) if (event.kind === "turn-ended") outcomes.push(event.outcome);
|
|
1579
|
+
})();
|
|
1580
|
+
await Bun.sleep(1_500);
|
|
1581
|
+
await writeFile(join(dir, "release"), "");
|
|
1582
|
+
await eventually(() => outcomes.length === 1, 10_000);
|
|
1583
|
+
expect(outcomes).toEqual(["failed"]);
|
|
1584
|
+
});
|
|
1585
|
+
|
|
1586
|
+
test("a start tells the caller the session before its first prompt goes out", async () => {
|
|
1587
|
+
control = await fakeControl();
|
|
1588
|
+
const named: string[] = [];
|
|
1589
|
+
let prompted: number | undefined;
|
|
1590
|
+
const { session } = await control.start({
|
|
1591
|
+
prompt: promptFromText("one"),
|
|
1592
|
+
cwd: dir,
|
|
1593
|
+
named: async (id) => {
|
|
1594
|
+
named.push(id.native);
|
|
1595
|
+
prompted = (await readFile(join(dir, "prompts.log"), "utf8").catch(() => "")).split("\n").filter((line) => line !== "").length;
|
|
1596
|
+
},
|
|
1597
|
+
});
|
|
1598
|
+
expect(named).toEqual([session.native]);
|
|
1599
|
+
expect(prompted).toBe(0);
|
|
1600
|
+
});
|
|
1601
|
+
|
|
1602
|
+
test("a start whose session could not be written down gives it no work and stops the process", async () => {
|
|
1603
|
+
control = await fakeControl();
|
|
1604
|
+
const before = directChildren().map((child) => child.pid);
|
|
1605
|
+
await expect(
|
|
1606
|
+
control.start({
|
|
1607
|
+
prompt: promptFromText("one"),
|
|
1608
|
+
cwd: dir,
|
|
1609
|
+
named: async () => {
|
|
1610
|
+
throw new Error("the journal is full");
|
|
1611
|
+
},
|
|
1612
|
+
}),
|
|
1613
|
+
).rejects.toThrow("the journal is full");
|
|
1614
|
+
expect(await readFile(join(dir, "prompts.log"), "utf8").catch(() => "")).toBe("");
|
|
1615
|
+
await eventually(() => directChildren().every((child) => before.includes(child.pid)), 10_000);
|
|
1616
|
+
});
|