@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,357 @@
|
|
|
1
|
+
// The MiniMax Code reading side against sessions that kept their history in the ledger: the
|
|
2
|
+
// sessions that have no journal of their own, and the ones whose journal holds nothing.
|
|
3
|
+
//
|
|
4
|
+
// The fixtures are the shapes a real ledger holds (small, anonymised): an entry of the history the
|
|
5
|
+
// agent was given, an entry of what the user saw, and the bookkeeping entries between them.
|
|
6
|
+
|
|
7
|
+
import { afterEach, beforeEach, expect, test } from "bun:test";
|
|
8
|
+
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
import { harnessId, sessionId } from "@niwa-labs/harness";
|
|
12
|
+
import type { Locator, SessionRecord } from "@niwa-labs/harness";
|
|
13
|
+
|
|
14
|
+
import { McodeReading } from "../src/mcode/reading.ts";
|
|
15
|
+
import { tempDirSync } from "@niwa-labs/testing";
|
|
16
|
+
|
|
17
|
+
let home: string;
|
|
18
|
+
const reading = () => new McodeReading({ harness: harnessId("mcode-test"), home });
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
home = tempDirSync("mcode-ledger");
|
|
22
|
+
});
|
|
23
|
+
afterEach(() => rmSync(home, { recursive: true, force: true }));
|
|
24
|
+
|
|
25
|
+
/** The directory of a session: it holds the manifest, and maybe a journal and a ledger. */
|
|
26
|
+
function sessionDir(native: string): string {
|
|
27
|
+
const encoded = Buffer.from(native).toString("base64url");
|
|
28
|
+
const dir = join(home, "v2", "sessions", "2026", "10", "07", `12-00-00-000-session_${encoded}`);
|
|
29
|
+
mkdirSync(dir, { recursive: true });
|
|
30
|
+
writeFileSync(
|
|
31
|
+
join(dir, "manifest.json"),
|
|
32
|
+
JSON.stringify({ sessionId: native, createdAtMs: 1000, updatedAtMs: 2000 }),
|
|
33
|
+
);
|
|
34
|
+
return dir;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** An entry of the ledger, as the ledger writes it. */
|
|
38
|
+
const entry = (event: object): string => `${JSON.stringify(event)}\n`;
|
|
39
|
+
|
|
40
|
+
/** The entry that says the session was made. */
|
|
41
|
+
const created = (native: string): string =>
|
|
42
|
+
entry({
|
|
43
|
+
kind: "session.created",
|
|
44
|
+
sessionId: native,
|
|
45
|
+
record: { sessionId: native, workspaceDir: "/work/ledger", sessionType: "root" },
|
|
46
|
+
schemaVersion: 1,
|
|
47
|
+
seq: 1,
|
|
48
|
+
createdAtMs: 1000,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
/** The entry that says the session was snapshotted: bookkeeping, it carries no turn. */
|
|
52
|
+
const snapshot = (seq: number): string =>
|
|
53
|
+
entry({
|
|
54
|
+
kind: "session.snapshot_created",
|
|
55
|
+
sessionId: "mvs_led",
|
|
56
|
+
snapshotId: "snapshot_1",
|
|
57
|
+
schemaVersion: 1,
|
|
58
|
+
seq,
|
|
59
|
+
createdAtMs: 2000,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/** The entry that carries a piece of the history the agent was given. */
|
|
63
|
+
const history = (messages: object[], seq: number): string =>
|
|
64
|
+
entry({
|
|
65
|
+
kind: "message.pi_history_appended",
|
|
66
|
+
sessionId: "mvs_led",
|
|
67
|
+
messages,
|
|
68
|
+
schemaVersion: 1,
|
|
69
|
+
seq,
|
|
70
|
+
createdAtMs: 3000,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
/** The entry that carries what the user saw. */
|
|
74
|
+
const display = (message: object, seq: number): string =>
|
|
75
|
+
entry({
|
|
76
|
+
kind: "message.display_upserted",
|
|
77
|
+
sessionId: "mvs_led",
|
|
78
|
+
message,
|
|
79
|
+
schemaVersion: 1,
|
|
80
|
+
seq,
|
|
81
|
+
createdAtMs: 4000,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
async function all(iter: AsyncIterable<SessionRecord>): Promise<SessionRecord[]> {
|
|
85
|
+
const out: SessionRecord[] = [];
|
|
86
|
+
for await (const r of iter) out.push(r);
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const ID = sessionId(harnessId("mcode-test"), "mvs_led");
|
|
91
|
+
|
|
92
|
+
test("a session with no journal is read from its ledger", async () => {
|
|
93
|
+
const dir = sessionDir("mvs_led");
|
|
94
|
+
writeFileSync(
|
|
95
|
+
join(dir, "ledger.jsonl"),
|
|
96
|
+
created("mvs_led") +
|
|
97
|
+
snapshot(2) +
|
|
98
|
+
history(
|
|
99
|
+
[
|
|
100
|
+
{
|
|
101
|
+
role: "user",
|
|
102
|
+
content: [{ type: "text", text: "how do I read a ledger" }],
|
|
103
|
+
timestamp: 1000,
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
role: "assistant",
|
|
107
|
+
content: [
|
|
108
|
+
{ type: "thinking", thinking: "the ledger holds the turns" },
|
|
109
|
+
{ type: "text", text: "read it line by line" },
|
|
110
|
+
{ type: "toolCall", id: "call_1", name: "read", arguments: { filePath: "/x" } },
|
|
111
|
+
],
|
|
112
|
+
timestamp: 2000,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
role: "toolResult",
|
|
116
|
+
toolCallId: "call_1",
|
|
117
|
+
toolName: "read",
|
|
118
|
+
content: [{ type: "text", text: "the bytes" }],
|
|
119
|
+
isError: false,
|
|
120
|
+
timestamp: 3000,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
3,
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const records = await all(reading().read(ID));
|
|
128
|
+
expect(records.map((r) => r.kind)).toEqual([
|
|
129
|
+
"user",
|
|
130
|
+
"thinking",
|
|
131
|
+
"assistant",
|
|
132
|
+
"tool-call",
|
|
133
|
+
"tool-result",
|
|
134
|
+
]);
|
|
135
|
+
expect(records[1]).toMatchObject({ kind: "thinking", text: "the ledger holds the turns", redacted: false });
|
|
136
|
+
expect(records[0]).toMatchObject({ kind: "user", text: "how do I read a ledger", at: 1000 });
|
|
137
|
+
expect(records[2]).toMatchObject({ kind: "assistant", text: "read it line by line" });
|
|
138
|
+
expect(records[3]).toMatchObject({ kind: "tool-call", callId: "call_1", name: "read" });
|
|
139
|
+
expect(records[4]).toMatchObject({ kind: "tool-result", callId: "call_1", ok: true });
|
|
140
|
+
|
|
141
|
+
// The session states the directory it runs in.
|
|
142
|
+
const infos = [];
|
|
143
|
+
for await (const info of reading().list()) infos.push(info);
|
|
144
|
+
expect(infos[0]?.cwd).toBe("/work/ledger");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("an empty journal falls back to the ledger of the session", async () => {
|
|
148
|
+
const dir = sessionDir("mvs_led");
|
|
149
|
+
writeFileSync(join(dir, "messages.jsonl"), "");
|
|
150
|
+
writeFileSync(
|
|
151
|
+
join(dir, "ledger.jsonl"),
|
|
152
|
+
created("mvs_led") +
|
|
153
|
+
history([{ role: "user", content: [{ type: "text", text: "only in the ledger" }], timestamp: 1000 }], 2),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const records = await all(reading().read(ID));
|
|
157
|
+
expect(records.map((r) => r.kind)).toEqual(["user"]);
|
|
158
|
+
expect(records[0]).toMatchObject({ kind: "user", text: "only in the ledger" });
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("a journal with records in it is never read together with the ledger", async () => {
|
|
162
|
+
const dir = sessionDir("mvs_led");
|
|
163
|
+
writeFileSync(
|
|
164
|
+
join(dir, "messages.jsonl"),
|
|
165
|
+
`${JSON.stringify({
|
|
166
|
+
message_id: "m",
|
|
167
|
+
message: { role: "user", timestamp: 1, content: "from the journal" },
|
|
168
|
+
})}\n`,
|
|
169
|
+
);
|
|
170
|
+
writeFileSync(
|
|
171
|
+
join(dir, "ledger.jsonl"),
|
|
172
|
+
created("mvs_led") +
|
|
173
|
+
history([{ role: "user", content: [{ type: "text", text: "from the ledger" }], timestamp: 1 }], 2),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const records = await all(reading().read(ID));
|
|
177
|
+
expect(records.map((r) => r.kind)).toEqual(["user"]);
|
|
178
|
+
expect(records[0]).toMatchObject({ kind: "user", text: "from the journal" });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a ledger of display entries is read when it holds no history", async () => {
|
|
182
|
+
const dir = sessionDir("mvs_led");
|
|
183
|
+
writeFileSync(
|
|
184
|
+
join(dir, "ledger.jsonl"),
|
|
185
|
+
created("mvs_led") +
|
|
186
|
+
display(
|
|
187
|
+
{
|
|
188
|
+
msg_id: "umsg_1",
|
|
189
|
+
role: "user",
|
|
190
|
+
msg_type: 1,
|
|
191
|
+
msg_content: "what is the weather",
|
|
192
|
+
timestamp: 1000,
|
|
193
|
+
},
|
|
194
|
+
2,
|
|
195
|
+
) +
|
|
196
|
+
display(
|
|
197
|
+
{
|
|
198
|
+
msg_id: "msg_1",
|
|
199
|
+
role: "assistant",
|
|
200
|
+
msg_type: 2,
|
|
201
|
+
msg_content: "it is warm",
|
|
202
|
+
thinking_content: "the answer is short",
|
|
203
|
+
timestamp: 2000,
|
|
204
|
+
tool_calls: [
|
|
205
|
+
{
|
|
206
|
+
tool_name: "bash",
|
|
207
|
+
tool_call_id: "call_9",
|
|
208
|
+
tool_call_args: '{"command":"weather"}',
|
|
209
|
+
tool_call_status: 2,
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
},
|
|
213
|
+
3,
|
|
214
|
+
),
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const records = await all(reading().read(ID));
|
|
218
|
+
expect(records.map((r) => r.kind)).toEqual(["user", "thinking", "assistant", "tool-call"]);
|
|
219
|
+
expect(records[1]).toMatchObject({ kind: "thinking", text: "the answer is short", redacted: false });
|
|
220
|
+
expect(records[0]).toMatchObject({ kind: "user", text: "what is the weather" });
|
|
221
|
+
expect(records[2]).toMatchObject({ kind: "assistant", text: "it is warm" });
|
|
222
|
+
expect(records[3]).toMatchObject({
|
|
223
|
+
kind: "tool-call",
|
|
224
|
+
callId: "call_9",
|
|
225
|
+
name: "bash",
|
|
226
|
+
input: { command: "weather" },
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("a replaced history replaces what came before it", async () => {
|
|
231
|
+
const dir = sessionDir("mvs_led");
|
|
232
|
+
writeFileSync(
|
|
233
|
+
join(dir, "ledger.jsonl"),
|
|
234
|
+
created("mvs_led") +
|
|
235
|
+
history([{ role: "user", content: [{ type: "text", text: "before" }], timestamp: 1000 }], 2) +
|
|
236
|
+
entry({
|
|
237
|
+
kind: "message.pi_history_replaced",
|
|
238
|
+
sessionId: "mvs_led",
|
|
239
|
+
messages: [{ role: "user", content: [{ type: "text", text: "after" }], timestamp: 2000 }],
|
|
240
|
+
schemaVersion: 1,
|
|
241
|
+
seq: 3,
|
|
242
|
+
createdAtMs: 3000,
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const records = await all(reading().read(ID));
|
|
247
|
+
expect(records.map((r) => r.kind)).toEqual(["user"]);
|
|
248
|
+
expect(records[0]).toMatchObject({ kind: "user", text: "after", at: 2000 });
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test("a locator of the ledger resumes exactly, and raw gives the entry whole", async () => {
|
|
252
|
+
const dir = sessionDir("mvs_led");
|
|
253
|
+
writeFileSync(
|
|
254
|
+
join(dir, "ledger.jsonl"),
|
|
255
|
+
created("mvs_led") +
|
|
256
|
+
history(
|
|
257
|
+
[
|
|
258
|
+
{ role: "user", content: [{ type: "text", text: "one" }], timestamp: 1000 },
|
|
259
|
+
{ role: "assistant", content: [{ type: "text", text: "two" }], timestamp: 2000 },
|
|
260
|
+
],
|
|
261
|
+
2,
|
|
262
|
+
),
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
const full = await all(reading().read(ID));
|
|
266
|
+
expect(full).toHaveLength(2);
|
|
267
|
+
for (let i = 0; i < full.length; i += 1) {
|
|
268
|
+
const rest = await all(reading().read(ID, full[i]?.locator));
|
|
269
|
+
expect(rest.map((r) => r.locator)).toEqual(full.slice(i + 1).map((r) => r.locator));
|
|
270
|
+
}
|
|
271
|
+
expect(await reading().coverage(ID, full[1]?.locator)).toEqual({ status: "empty" });
|
|
272
|
+
expect(await reading().coverage(ID)).toEqual({ status: "not-scanned" });
|
|
273
|
+
|
|
274
|
+
const raw = await reading().raw(ID, full[0]?.locator as Locator);
|
|
275
|
+
expect(raw).toMatchObject({ kind: "message.pi_history_appended" });
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test("a line of the ledger that is not JSON is unknown with its text", async () => {
|
|
279
|
+
const dir = sessionDir("mvs_led");
|
|
280
|
+
writeFileSync(
|
|
281
|
+
join(dir, "ledger.jsonl"),
|
|
282
|
+
created("mvs_led") +
|
|
283
|
+
history([{ role: "user", content: [{ type: "text", text: "one" }], timestamp: 1000 }], 2) +
|
|
284
|
+
"not json at all\n",
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
const records = await all(reading().read(ID));
|
|
288
|
+
expect(records.map((r) => r.kind)).toEqual(["user", "unknown"]);
|
|
289
|
+
expect(records[1]).toMatchObject({ raw: "not json at all" });
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("a session with an empty journal and no ledger holds nothing", async () => {
|
|
293
|
+
const dir = sessionDir("mvs_led");
|
|
294
|
+
writeFileSync(join(dir, "messages.jsonl"), "");
|
|
295
|
+
expect(await all(reading().read(ID))).toHaveLength(0);
|
|
296
|
+
expect(await reading().coverage(ID)).toEqual({ status: "empty" });
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test("a ledger that is there but cannot be read fails instead of reading as empty", async () => {
|
|
300
|
+
const dir = sessionDir("mvs_led");
|
|
301
|
+
writeFileSync(join(dir, "messages.jsonl"), "");
|
|
302
|
+
// A directory where the ledger should be: it exists, and it cannot be read as a file.
|
|
303
|
+
mkdirSync(join(dir, "ledger.jsonl"));
|
|
304
|
+
await expect(all(reading().read(ID))).rejects.toThrow();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const message = (role: "user" | "assistant", text: string, at: number) => ({
|
|
308
|
+
role,
|
|
309
|
+
content: [{ type: "text", text }],
|
|
310
|
+
timestamp: at,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test("a read from a checkpoint in the ledger gives only what came after it, across entries", async () => {
|
|
314
|
+
const dir = sessionDir("mvs_led");
|
|
315
|
+
writeFileSync(
|
|
316
|
+
join(dir, "ledger.jsonl"),
|
|
317
|
+
created("mvs_led") +
|
|
318
|
+
history([message("user", "one", 1000)], 2) +
|
|
319
|
+
history([message("assistant", "two", 2000)], 3) +
|
|
320
|
+
history([message("user", "three", 3000)], 4),
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
const full = await all(reading().read(ID));
|
|
324
|
+
expect(full.map((r) => (r.kind === "user" || r.kind === "assistant" ? r.text : r.kind))).toEqual([
|
|
325
|
+
"one",
|
|
326
|
+
"two",
|
|
327
|
+
"three",
|
|
328
|
+
]);
|
|
329
|
+
const second = await all(reading().read(ID, full[1]?.locator));
|
|
330
|
+
expect(second.map((r) => (r.kind === "user" ? r.text : r.kind))).toEqual(["three"]);
|
|
331
|
+
expect(await reading().coverage(ID, full[1]?.locator)).toEqual({ status: "not-scanned" });
|
|
332
|
+
expect(await reading().coverage(ID, full[2]?.locator)).toEqual({ status: "empty" });
|
|
333
|
+
expect(await all(reading().read(ID, full[2]?.locator))).toEqual([]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test("a checkpoint of a ledger that was rewritten starts the read over", async () => {
|
|
337
|
+
const dir = sessionDir("mvs_led");
|
|
338
|
+
writeFileSync(
|
|
339
|
+
join(dir, "ledger.jsonl"),
|
|
340
|
+
created("mvs_led") + history([message("user", "one", 1000)], 2) + history([message("user", "two", 2000)], 3),
|
|
341
|
+
);
|
|
342
|
+
const [, second] = await all(reading().read(ID));
|
|
343
|
+
writeFileSync(
|
|
344
|
+
join(dir, "ledger.jsonl"),
|
|
345
|
+
created("mvs_led") + history([message("user", "other", 1000)], 2) + history([message("user", "again", 2000)], 3),
|
|
346
|
+
);
|
|
347
|
+
const again = await all(reading().read(ID, second?.locator));
|
|
348
|
+
expect(again.map((r) => (r.kind === "user" ? r.text : r.kind))).toEqual(["other", "again"]);
|
|
349
|
+
expect(again.map((r) => r.reanchored)).toEqual([true, undefined]);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test("the coverage of a ledger that cannot be read is failed, not an error", async () => {
|
|
353
|
+
const dir = sessionDir("mvs_led");
|
|
354
|
+
writeFileSync(join(dir, "messages.jsonl"), "");
|
|
355
|
+
mkdirSync(join(dir, "ledger.jsonl"));
|
|
356
|
+
expect((await reading().coverage(ID)).status).toBe("failed");
|
|
357
|
+
});
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
// The MiniMax Code reading side against small synthetic sessions in a temp dir: running, nested,
|
|
2
|
+
// compacted, with a broken line; plus resume by locator and detection of a rewritten file.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, expect, test } from "bun:test";
|
|
5
|
+
import { appendFileSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { Database } from "bun:sqlite";
|
|
9
|
+
|
|
10
|
+
import { harnessId, sessionId } from "@niwa-labs/harness";
|
|
11
|
+
import type { Locator, SessionRecord } from "@niwa-labs/harness";
|
|
12
|
+
|
|
13
|
+
import { McodeReading } from "../src/mcode/reading.ts";
|
|
14
|
+
import { tempDirSync } from "@niwa-labs/testing";
|
|
15
|
+
|
|
16
|
+
let home: string;
|
|
17
|
+
const reading = () => new McodeReading({ harness: harnessId("mcode-test"), home });
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
home = tempDirSync("mcode-reading");
|
|
21
|
+
});
|
|
22
|
+
afterEach(() => rmSync(home, { recursive: true, force: true }));
|
|
23
|
+
|
|
24
|
+
function journalOf(native: string): string {
|
|
25
|
+
const encoded = Buffer.from(native).toString("base64url");
|
|
26
|
+
const dir = join(home, "v2", "sessions", "2026", "10", "07", `12-00-00-000-session_${encoded}`);
|
|
27
|
+
mkdirSync(dir, { recursive: true });
|
|
28
|
+
writeFileSync(
|
|
29
|
+
join(dir, "manifest.json"),
|
|
30
|
+
JSON.stringify({ sessionId: native, createdAtMs: 1000, updatedAtMs: 2000 }),
|
|
31
|
+
);
|
|
32
|
+
return join(dir, "messages.jsonl");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const line = (message: object) => `${JSON.stringify({ message_id: "m", message })}\n`;
|
|
36
|
+
const user = (text: string) => line({ role: "user", timestamp: 1, content: text });
|
|
37
|
+
const assistant = (text: string) =>
|
|
38
|
+
line({ role: "assistant", timestamp: 2, model: "m1", content: [{ type: "text", text }] });
|
|
39
|
+
|
|
40
|
+
async function all(iter: AsyncIterable<SessionRecord>): Promise<SessionRecord[]> {
|
|
41
|
+
const out: SessionRecord[] = [];
|
|
42
|
+
for await (const r of iter) out.push(r);
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const ID = sessionId(harnessId("mcode-test"), "mvs_run1");
|
|
47
|
+
|
|
48
|
+
test("lists a session from the files, with cwd from the preamble", async () => {
|
|
49
|
+
const path = journalOf("mvs_run1");
|
|
50
|
+
writeFileSync(
|
|
51
|
+
path,
|
|
52
|
+
user("<system-reminder>\n<agent-context>\n YOUR WORKSPACE DIRECTORY: /work/x\n</agent-context>\n</system-reminder>\n\nhello"),
|
|
53
|
+
);
|
|
54
|
+
const infos = [];
|
|
55
|
+
for await (const info of reading().list()) infos.push(info);
|
|
56
|
+
expect(infos).toHaveLength(1);
|
|
57
|
+
expect(String(infos[0]?.id.native)).toBe("mvs_run1");
|
|
58
|
+
expect(infos[0]?.cwd).toBe("/work/x");
|
|
59
|
+
expect(infos[0]?.updatedAt).toBe(2000 as never);
|
|
60
|
+
// MiniMax Code names no session: the title is the first message, the preamble cut out.
|
|
61
|
+
expect(infos[0]?.title).toBe("hello");
|
|
62
|
+
const records = await all(reading().read(ID));
|
|
63
|
+
// What the harness inserted is cut out of the text, so nothing in it is marked as inserted.
|
|
64
|
+
expect(records[0]).toMatchObject({ kind: "user", text: "hello", inserted: [] });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("the title is the one the harness keeps in its runtime state, else the first message", async () => {
|
|
68
|
+
writeFileSync(journalOf("mvs_run1"), user("hello"));
|
|
69
|
+
writeFileSync(journalOf("mvs_run2"), user("plan the release"));
|
|
70
|
+
mkdirSync(join(home, "v2", "sqlite"), { recursive: true });
|
|
71
|
+
const db = new Database(join(home, "v2", "sqlite", "runtime-state.sqlite"));
|
|
72
|
+
db.run("create table local_runtime_sessions (session_id text primary key, record_json text not null, title text)");
|
|
73
|
+
db.run("insert into local_runtime_sessions values ('mvs_run1', '{}', 'Greet the owner')");
|
|
74
|
+
db.run("insert into local_runtime_sessions values ('mvs_run2', '{}', null)");
|
|
75
|
+
db.close();
|
|
76
|
+
const titles = new Map<string, string | undefined>();
|
|
77
|
+
for await (const info of reading().list()) titles.set(String(info.id.native), info.title);
|
|
78
|
+
expect(titles.get("mvs_run1")).toBe("Greet the owner");
|
|
79
|
+
expect(titles.get("mvs_run2")).toBe("plan the release");
|
|
80
|
+
|
|
81
|
+
// Asked for some sessions only, the titles are those of the ones asked for.
|
|
82
|
+
const some = new Map<string, string | undefined>();
|
|
83
|
+
for await (const info of reading().list({ natives: ["mvs_run1"] })) some.set(String(info.id.native), info.title);
|
|
84
|
+
expect([...some]).toEqual([["mvs_run1", "Greet the owner"]]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("a runtime state that is missing, has no such table or is not a database gives no title and no error", async () => {
|
|
88
|
+
writeFileSync(journalOf("mvs_run1"), user("hello"));
|
|
89
|
+
const titles = async () => {
|
|
90
|
+
const out = new Map<string, string | undefined>();
|
|
91
|
+
for await (const info of reading().list()) out.set(String(info.id.native), info.title);
|
|
92
|
+
return out;
|
|
93
|
+
};
|
|
94
|
+
// Missing: the first message stands in.
|
|
95
|
+
expect((await titles()).get("mvs_run1")).toBe("hello");
|
|
96
|
+
mkdirSync(join(home, "v2", "sqlite"), { recursive: true });
|
|
97
|
+
const path = join(home, "v2", "sqlite", "runtime-state.sqlite");
|
|
98
|
+
// A database without the table.
|
|
99
|
+
const db = new Database(path);
|
|
100
|
+
db.run("create table other (a text)");
|
|
101
|
+
db.close();
|
|
102
|
+
expect((await titles()).get("mvs_run1")).toBe("hello");
|
|
103
|
+
// A file that is no database.
|
|
104
|
+
writeFileSync(path, "this is not sqlite");
|
|
105
|
+
expect((await titles()).get("mvs_run1")).toBe("hello");
|
|
106
|
+
// It is read, and never written to.
|
|
107
|
+
expect(readFileSync(path, "utf8")).toBe("this is not sqlite");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("a listing can be limited to some sessions", async () => {
|
|
111
|
+
writeFileSync(journalOf("mvs_run1"), user("one"));
|
|
112
|
+
writeFileSync(journalOf("mvs_run2"), user("two"));
|
|
113
|
+
const natives = async (filter?: { natives: string[] }) => {
|
|
114
|
+
const out: string[] = [];
|
|
115
|
+
for await (const info of reading().list(filter)) out.push(String(info.id.native));
|
|
116
|
+
return out.sort();
|
|
117
|
+
};
|
|
118
|
+
expect(await natives({ natives: ["mvs_run2"] })).toEqual(["mvs_run2"]);
|
|
119
|
+
expect(await natives({ natives: [] })).toEqual([]);
|
|
120
|
+
expect(await natives()).toEqual(["mvs_run1", "mvs_run2"]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("maps a running session: text, tool call, tool result, nested child", async () => {
|
|
124
|
+
const path = journalOf("mvs_run1");
|
|
125
|
+
writeFileSync(
|
|
126
|
+
path,
|
|
127
|
+
user("go") +
|
|
128
|
+
line({
|
|
129
|
+
role: "assistant",
|
|
130
|
+
content: [
|
|
131
|
+
{ type: "thinking", thinking: "hm", thinkingSignature: "abc" },
|
|
132
|
+
{ type: "thinking", thinking: "", redacted: true },
|
|
133
|
+
{ type: "text", text: "ok" },
|
|
134
|
+
{ type: "toolCall", id: "c1", name: "task", arguments: { a: 1 } },
|
|
135
|
+
],
|
|
136
|
+
}) +
|
|
137
|
+
line({
|
|
138
|
+
role: "toolResult",
|
|
139
|
+
toolCallId: "c1",
|
|
140
|
+
toolName: "task",
|
|
141
|
+
content: 'done session_id="mvs_child9"',
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
const kinds = (await all(reading().read(ID))).map((r) => r.kind);
|
|
145
|
+
expect(kinds).toEqual(["user", "thinking", "thinking", "assistant", "tool-call", "tool-result", "nested"]);
|
|
146
|
+
const thoughts = (await all(reading().read(ID))).filter((r) => r.kind === "thinking");
|
|
147
|
+
expect(thoughts[0]).toMatchObject({ text: "hm", redacted: false });
|
|
148
|
+
expect(thoughts[1]).toMatchObject({ text: "", redacted: true });
|
|
149
|
+
// The sealed block stays whole behind its locator.
|
|
150
|
+
expect(await reading().raw(ID, thoughts[1]!.locator)).toMatchObject({ message: { role: "assistant" } });
|
|
151
|
+
const nested = (await all(reading().read(ID))).find((r) => r.kind === "nested");
|
|
152
|
+
expect(nested).toMatchObject({ child: { native: "mvs_child9" } });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("a call is named by its category and its target, in MiniMax Code's tool names", async () => {
|
|
156
|
+
const path = journalOf("mvs_run1");
|
|
157
|
+
writeFileSync(
|
|
158
|
+
path,
|
|
159
|
+
line({
|
|
160
|
+
role: "assistant",
|
|
161
|
+
content: [
|
|
162
|
+
{ type: "toolCall", id: "c1", name: "bash", arguments: { command: "make" } },
|
|
163
|
+
{ type: "toolCall", id: "c2", name: "read", arguments: { path: "src/a.ts" } },
|
|
164
|
+
{ type: "toolCall", id: "c3", name: "todowrite", arguments: { todos: [] } },
|
|
165
|
+
{ type: "toolCall", id: "c4", name: "task", arguments: { description: "Check docs", prompt: "…" } },
|
|
166
|
+
],
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
const calls = (await all(reading().read(ID))).flatMap((r) => (r.kind === "tool-call" ? [r] : []));
|
|
170
|
+
expect(calls.map((c) => [c.category, c.target])).toEqual([
|
|
171
|
+
["shell", "make"],
|
|
172
|
+
["read", "src/a.ts"],
|
|
173
|
+
["plan", undefined],
|
|
174
|
+
["subagent", "Check docs"],
|
|
175
|
+
]);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("a todowrite call leaves its checklist without the cancelled items; an unknown state leaves none", async () => {
|
|
179
|
+
const path = journalOf("mvs_run1");
|
|
180
|
+
const todo = (content: string, status: string) => ({ content, status, priority: "high" });
|
|
181
|
+
writeFileSync(
|
|
182
|
+
path,
|
|
183
|
+
line({
|
|
184
|
+
role: "assistant",
|
|
185
|
+
content: [
|
|
186
|
+
{ type: "toolCall", id: "t1", name: "todowrite", arguments: { todos: [todo("a", "completed"), todo("b", "cancelled"), todo("c", "in_progress")] } },
|
|
187
|
+
{ type: "toolCall", id: "t2", name: "todowrite", arguments: { todos: [todo("a", "paused")] } },
|
|
188
|
+
],
|
|
189
|
+
}),
|
|
190
|
+
);
|
|
191
|
+
const records = await all(reading().read(ID));
|
|
192
|
+
expect(records.map((r) => r.kind)).toEqual(["tool-call", "checklist", "tool-call"]);
|
|
193
|
+
expect(records[1]).toMatchObject({ callId: "t1", items: [{ text: "a", state: "done" }, { text: "c", state: "active" }] });
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("compaction is its own kind; an unfamiliar role is unknown with raw kept", async () => {
|
|
197
|
+
const path = journalOf("mvs_run1");
|
|
198
|
+
writeFileSync(
|
|
199
|
+
path,
|
|
200
|
+
line({ role: "compactionSummary", summary: "S" }) + line({ role: "weird", extra: 1 }),
|
|
201
|
+
);
|
|
202
|
+
const [a, b] = await all(reading().read(ID));
|
|
203
|
+
expect(a).toMatchObject({ kind: "compaction", summary: "S" });
|
|
204
|
+
expect(b?.kind).toBe("unknown");
|
|
205
|
+
expect(b).toMatchObject({ raw: { message: { role: "weird", extra: 1 } } });
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("a broken line is unknown with its text, not an error; blank lines are skipped", async () => {
|
|
209
|
+
const path = journalOf("mvs_run1");
|
|
210
|
+
writeFileSync(path, user("a") + "{not json\n\n" + user("b"));
|
|
211
|
+
const records = await all(reading().read(ID));
|
|
212
|
+
expect(records.map((r) => r.kind)).toEqual(["user", "unknown", "user"]);
|
|
213
|
+
expect(records[1]).toMatchObject({ raw: "{not json", incomplete: false });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("H3 a half-written tail is marked incomplete and is delivered again once whole", async () => {
|
|
217
|
+
const path = journalOf("mvs_run1");
|
|
218
|
+
const whole = assistant("second");
|
|
219
|
+
writeFileSync(path, user("first") + whole.slice(0, 20));
|
|
220
|
+
const first = await all(reading().read(ID));
|
|
221
|
+
expect(first.map((r) => r.incomplete)).toEqual([false, true]);
|
|
222
|
+
expect(await reading().coverage(ID, first[0]?.locator)).toEqual({ status: "not-scanned" });
|
|
223
|
+
|
|
224
|
+
appendFileSync(path, whole.slice(20));
|
|
225
|
+
const rest = await all(reading().read(ID, first[0]?.locator));
|
|
226
|
+
expect(rest).toHaveLength(1);
|
|
227
|
+
expect(rest[0]).toMatchObject({ kind: "assistant", text: "second", incomplete: false });
|
|
228
|
+
|
|
229
|
+
// Resuming from the marked tail also reads it again, never past it.
|
|
230
|
+
const again = await all(reading().read(ID, first[1]?.locator));
|
|
231
|
+
expect(again).toHaveLength(1);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test("a locator resumes exactly, inside a multi-record line too", async () => {
|
|
235
|
+
const path = journalOf("mvs_run1");
|
|
236
|
+
writeFileSync(
|
|
237
|
+
path,
|
|
238
|
+
user("u1") +
|
|
239
|
+
line({
|
|
240
|
+
role: "assistant",
|
|
241
|
+
content: [
|
|
242
|
+
{ type: "text", text: "t" },
|
|
243
|
+
{ type: "toolCall", id: "c", name: "n" },
|
|
244
|
+
],
|
|
245
|
+
}) +
|
|
246
|
+
user("u2"),
|
|
247
|
+
);
|
|
248
|
+
const full = await all(reading().read(ID));
|
|
249
|
+
expect(full).toHaveLength(4);
|
|
250
|
+
for (let i = 0; i < full.length; i += 1) {
|
|
251
|
+
const rest = await all(reading().read(ID, full[i]?.locator));
|
|
252
|
+
expect(rest.map((r) => r.locator)).toEqual(full.slice(i + 1).map((r) => r.locator));
|
|
253
|
+
}
|
|
254
|
+
expect(await reading().coverage(ID, full[3]?.locator)).toEqual({ status: "empty" });
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test("a rewritten or truncated file is detected: read restarts, raw refuses the old locator", async () => {
|
|
258
|
+
const path = journalOf("mvs_run1");
|
|
259
|
+
writeFileSync(path, user("one") + user("two"));
|
|
260
|
+
const [, second] = await all(reading().read(ID));
|
|
261
|
+
const old = second?.locator as Locator;
|
|
262
|
+
|
|
263
|
+
writeFileSync(path, user("ONE") + user("TWO") + user("THREE"));
|
|
264
|
+
const restarted = await all(reading().read(ID, old));
|
|
265
|
+
expect(restarted).toHaveLength(3);
|
|
266
|
+
expect(restarted.map((r) => r.reanchored)).toEqual([true, undefined, undefined]);
|
|
267
|
+
expect((await all(reading().read(ID, restarted[0]?.locator))).some((r) => r.reanchored === true)).toBe(false);
|
|
268
|
+
await expect(reading().raw(ID, old)).rejects.toThrow("does not belong");
|
|
269
|
+
|
|
270
|
+
writeFileSync(path, user("ONE"));
|
|
271
|
+
expect(await all(reading().read(ID, old))).toHaveLength(1);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("raw gives the harness's own record whole; coverage states what it does not know", async () => {
|
|
275
|
+
const path = journalOf("mvs_run1");
|
|
276
|
+
writeFileSync(path, user("hi"));
|
|
277
|
+
const [rec] = await all(reading().read(ID));
|
|
278
|
+
expect(await reading().raw(ID, rec?.locator as Locator)).toMatchObject({
|
|
279
|
+
message: { role: "user", content: "hi" },
|
|
280
|
+
});
|
|
281
|
+
const missing = sessionId(harnessId("mcode-test"), "mvs_none");
|
|
282
|
+
expect((await reading().coverage(missing)).status).toBe("unavailable");
|
|
283
|
+
writeFileSync(path, "");
|
|
284
|
+
expect(await reading().coverage(ID)).toEqual({ status: "empty" });
|
|
285
|
+
await expect(reading().raw(ID, "bogus" as Locator)).rejects.toThrow("not of this side");
|
|
286
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// The input of an agent process: a line that did not go out is reported, not dropped.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
import { Writable } from "node:stream";
|
|
5
|
+
|
|
6
|
+
import { inputOpen, writeLine } from "../src/mcode/transport.ts";
|
|
7
|
+
|
|
8
|
+
/** A stream that takes each chunk the way `take` says: it fails it, or lets it through. */
|
|
9
|
+
function stream(take: (chunk: string, done: (error?: Error | null) => void) => void): Writable {
|
|
10
|
+
return new Writable({ write: (chunk, _encoding, done) => take(String(chunk), done) });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test("a line the stream takes is accepted, with a line feed after it", async () => {
|
|
14
|
+
const seen: string[] = [];
|
|
15
|
+
const input = stream((chunk, done) => {
|
|
16
|
+
seen.push(chunk);
|
|
17
|
+
done();
|
|
18
|
+
});
|
|
19
|
+
await writeLine(input, '{"a":1}');
|
|
20
|
+
expect(seen).toEqual(['{"a":1}\n']);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a line the stream fails is rejected with the failure", async () => {
|
|
24
|
+
const input = stream((_chunk, done) => done(new Error("EPIPE")));
|
|
25
|
+
input.on("error", () => undefined);
|
|
26
|
+
await expect(writeLine(input, "x")).rejects.toThrow("EPIPE");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("a closed input is refused at once, not written to", () => {
|
|
30
|
+
const input = stream((_chunk, done) => done());
|
|
31
|
+
input.end();
|
|
32
|
+
expect(inputOpen(input)).toBe(false);
|
|
33
|
+
expect(() => writeLine(input, "x")).toThrow("closed");
|
|
34
|
+
const destroyed = stream((_chunk, done) => done());
|
|
35
|
+
destroyed.destroy();
|
|
36
|
+
expect(() => writeLine(destroyed, "x")).toThrow("closed");
|
|
37
|
+
expect(() => writeLine(undefined, "x")).toThrow("closed");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("a stream that throws on write is refused at once", () => {
|
|
41
|
+
const input = new Writable({
|
|
42
|
+
write: () => undefined,
|
|
43
|
+
});
|
|
44
|
+
input.write = () => {
|
|
45
|
+
throw new Error("broken");
|
|
46
|
+
};
|
|
47
|
+
expect(() => writeLine(input, "x")).toThrow("broken");
|
|
48
|
+
});
|