@niwa-labs/harness-zcode 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 +8 -2
- package/package.json +20 -4
- package/scripts/gen-zcode.ts +384 -0
- package/src/config.ts +33 -0
- package/src/control.ts +1478 -0
- package/src/dispatch.ts +211 -0
- package/src/fixtures.ts +175 -0
- package/src/format.ts +153 -0
- package/src/index.ts +116 -0
- package/src/paths.ts +21 -0
- package/src/protocol/generated/zcode-protocol.ts +393 -0
- package/src/protocol/index.ts +230 -0
- package/src/reading.ts +506 -0
- package/src/reverse.ts +170 -0
- package/src/skills.ts +6 -0
- package/src/tools.ts +45 -0
- package/test/conformance.test.ts +29 -0
- package/test/control.test.ts +983 -0
- package/test/fixtures/zcode/captured-0.17.3.ndjson +23 -0
- package/test/fixtures/zcode/session-messages-0.17.3.json +241 -0
- package/test/live/control.test.ts +195 -0
- package/test/mode.test.ts +100 -0
- package/test/profile.test.ts +20 -0
- package/test/protocol.test.ts +437 -0
- package/test/reading.test.ts +332 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
// The ZCode reading side against small synthetic databases in a temp dir: what is a record and
|
|
2
|
+
// what is not, the half-written tail, the sessions spawned inside, locators, and the coverage.
|
|
3
|
+
|
|
4
|
+
import { afterEach, beforeEach, expect, test } from "bun:test";
|
|
5
|
+
import { mkdirSync, rmSync } from "node:fs";
|
|
6
|
+
import { dirname } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { Database } from "bun:sqlite";
|
|
9
|
+
|
|
10
|
+
import { timestamp } from "@niwa-labs/core";
|
|
11
|
+
import { LocatorSchema, harnessId, sessionId } from "@niwa-labs/harness";
|
|
12
|
+
import type { SessionRecord } from "@niwa-labs/harness";
|
|
13
|
+
import { tempDirSync } from "@niwa-labs/testing";
|
|
14
|
+
|
|
15
|
+
import { assistant, createDatabase, user, writeSession } from "../src/fixtures.ts";
|
|
16
|
+
import type { FixtureMessage } from "../src/fixtures.ts";
|
|
17
|
+
import { PART_TYPES } from "../src/format.ts";
|
|
18
|
+
import { dbPathOf } from "../src/paths.ts";
|
|
19
|
+
import { ZcodeReading, directoryOf, itemsOf } from "../src/reading.ts";
|
|
20
|
+
|
|
21
|
+
let home: string;
|
|
22
|
+
let db: Database;
|
|
23
|
+
const reading = () => new ZcodeReading({ harness: harnessId("zcode-test"), home });
|
|
24
|
+
const id = (native: string) => sessionId(harnessId("zcode-test"), native);
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
home = tempDirSync("zcode-reading");
|
|
28
|
+
mkdirSync(dirname(dbPathOf(home)), { recursive: true });
|
|
29
|
+
db = createDatabase(dbPathOf(home));
|
|
30
|
+
});
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
db.close();
|
|
33
|
+
rmSync(home, { recursive: true, force: true });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
async function all(iter: AsyncIterable<SessionRecord>): Promise<SessionRecord[]> {
|
|
37
|
+
const out: SessionRecord[] = [];
|
|
38
|
+
for await (const record of iter) out.push(record);
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const tool = (id: string, status: string, extra: Record<string, unknown> = {}): FixtureMessage => ({
|
|
43
|
+
id,
|
|
44
|
+
data: { role: "assistant", time: { created: 2000, completed: 2100 } },
|
|
45
|
+
parts: [{ type: "tool", callID: `call_${id}`, tool: "Bash", state: { status, input: { command: "ls" }, output: "a.txt", time: { start: 2000, end: 2010 }, ...extra } }],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("lists the sessions, last changed first, without the deleted ones, and filters them", async () => {
|
|
49
|
+
writeSession(db, "sess_a", [user("m1", "first")], { directory: "/work/a", at: 1000, title: " The first\nsession " });
|
|
50
|
+
writeSession(db, "sess_b", [user("m2", "second")], { directory: "/work/b", at: 5000 });
|
|
51
|
+
writeSession(db, "sess_gone", [user("m3", "gone")], { at: 9000 });
|
|
52
|
+
db.run("update session set time_deleted = 1 where id = 'sess_gone'");
|
|
53
|
+
db.run("update session set title_source = 'default', title = 'Placeholder Request' where id = 'sess_b'");
|
|
54
|
+
|
|
55
|
+
const listed = [];
|
|
56
|
+
for await (const info of reading().list()) listed.push(info);
|
|
57
|
+
expect(listed.map((info) => String(info.id.native))).toEqual(["sess_b", "sess_a"]);
|
|
58
|
+
expect(listed[1]).toMatchObject({ title: "The first session", cwd: "/work/a", startedAt: 1000, updatedAt: 1010 });
|
|
59
|
+
expect(listed[0]?.title).toBeUndefined();
|
|
60
|
+
|
|
61
|
+
const only = async (filter: Parameters<ZcodeReading["list"]>[0]) => {
|
|
62
|
+
const out: string[] = [];
|
|
63
|
+
for await (const info of reading().list(filter)) out.push(String(info.id.native));
|
|
64
|
+
return out;
|
|
65
|
+
};
|
|
66
|
+
expect(await only({ cwd: "/work/a" })).toEqual(["sess_a"]);
|
|
67
|
+
expect(await only({ since: timestamp(4000) })).toEqual(["sess_b"]);
|
|
68
|
+
expect(await only({ natives: ["sess_a", "sess_nope"] })).toEqual(["sess_a"]);
|
|
69
|
+
expect(await only({ natives: Array.from({ length: 600 }, (_, i) => (i === 0 ? "sess_b" : `x${i}`)) })).toEqual(["sess_b"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("a database that is not there lists nothing and reads as unavailable", async () => {
|
|
73
|
+
rmSync(dbPathOf(home));
|
|
74
|
+
expect(await all(reading().read(id("sess_a")))).toEqual([]);
|
|
75
|
+
expect((await reading().coverage(id("sess_a"))).status).toBe("unavailable");
|
|
76
|
+
const listed = [];
|
|
77
|
+
for await (const info of reading().list()) listed.push(info);
|
|
78
|
+
expect(listed).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("maps text, thoughts, tool calls and results; the harness's own messages and markers are not records", async () => {
|
|
82
|
+
writeSession(db, "sess_a", [
|
|
83
|
+
{ id: "m0", data: { role: "assistant", time: { created: 1, completed: 1 } }, parts: [{ type: "timeline", timelineType: "model_change" }] },
|
|
84
|
+
user("m1", "do it"),
|
|
85
|
+
{
|
|
86
|
+
id: "m1b",
|
|
87
|
+
data: { role: "user", metadata: { visibility: "model-only" }, time: { created: 1 } },
|
|
88
|
+
parts: [{ type: "text", text: "<system-reminder>hidden</system-reminder>" }],
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: "m2",
|
|
92
|
+
data: { role: "assistant", time: { created: 2, completed: 3 }, modelId: "GLM-5.3-Flash", providerId: "builtin:zai" },
|
|
93
|
+
parts: [
|
|
94
|
+
{ type: "step-start" },
|
|
95
|
+
{ type: "reasoning", text: "weighing it", time: { start: 2200, end: 2300 } },
|
|
96
|
+
{ type: "text", text: "synthetic", synthetic: true },
|
|
97
|
+
{ type: "text", text: "on it", time: { start: 2500 } },
|
|
98
|
+
{ type: "step-finish", tokens: { input: 1 } },
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
tool("m3", "completed"),
|
|
102
|
+
tool("m4", "error", { error: "no such file" }),
|
|
103
|
+
]);
|
|
104
|
+
const records = await all(reading().read(id("sess_a")));
|
|
105
|
+
expect(records.map((record) => record.kind)).toEqual(["user", "thinking", "assistant", "tool-call", "tool-result", "tool-call", "tool-result"]);
|
|
106
|
+
// The reminder ZCode keeps for the model alone and the synthetic part are not read, so nothing in the text is inserted.
|
|
107
|
+
expect(records[0]).toMatchObject({ kind: "user", text: "do it", inserted: [], incomplete: false });
|
|
108
|
+
expect(records[1]).toMatchObject({ kind: "thinking", text: "weighing it", redacted: false, at: 2200, incomplete: false });
|
|
109
|
+
expect(records[2]).toMatchObject({ kind: "assistant", text: "on it", model: "builtin:zai/GLM-5.3-Flash", at: 2500 });
|
|
110
|
+
expect(records[3]).toMatchObject({ kind: "tool-call", callId: "call_m3", name: "Bash", input: { command: "ls" }, at: 2000 });
|
|
111
|
+
expect(records[4]).toMatchObject({ kind: "tool-result", callId: "call_m3", output: "a.txt", ok: true, at: 2010 });
|
|
112
|
+
expect(records[6]).toMatchObject({ kind: "tool-result", output: "no such file", ok: false });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("a call is named by its category and its target, in ZCode's tool names", async () => {
|
|
116
|
+
const part = (callID: string, tool: string, input: object) => ({ type: "tool", callID, tool, state: { status: "completed", input, output: "", time: { start: 1, end: 2 } } });
|
|
117
|
+
writeSession(db, "sess_t", [
|
|
118
|
+
{
|
|
119
|
+
id: "m1",
|
|
120
|
+
data: { role: "assistant", time: { created: 2000, completed: 2100 } },
|
|
121
|
+
parts: [part("c1", "Bash", { command: "ls" }), part("c2", "Write", { file_path: "/w/a.ts", content: "" }), part("c3", "mcp__niwa__session_send", {})],
|
|
122
|
+
},
|
|
123
|
+
]);
|
|
124
|
+
const calls = (await all(reading().read(id("sess_t")))).flatMap((r) => (r.kind === "tool-call" ? [r] : []));
|
|
125
|
+
expect(calls.map((c) => [c.category, c.target])).toEqual([
|
|
126
|
+
["shell", "ls"],
|
|
127
|
+
["edit", "/w/a.ts"],
|
|
128
|
+
["mcp", undefined],
|
|
129
|
+
]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("a finished TodoWrite call leaves its checklist after its result, and a locator resumes past it", async () => {
|
|
133
|
+
const todos = [
|
|
134
|
+
{ content: "a", status: "completed" },
|
|
135
|
+
{ content: "b", status: "pending" },
|
|
136
|
+
];
|
|
137
|
+
writeSession(db, "sess_c", [
|
|
138
|
+
{
|
|
139
|
+
id: "m1",
|
|
140
|
+
data: { role: "assistant", time: { created: 2000, completed: 2100 } },
|
|
141
|
+
parts: [{ type: "tool", callID: "t1", tool: "TodoWrite", state: { status: "completed", input: { todos }, output: "", time: { start: 1, end: 2 } } }],
|
|
142
|
+
},
|
|
143
|
+
user("m2", "next"),
|
|
144
|
+
]);
|
|
145
|
+
const records = await all(reading().read(id("sess_c")));
|
|
146
|
+
expect(records.map((r) => r.kind)).toEqual(["tool-call", "tool-result", "checklist", "user"]);
|
|
147
|
+
expect(records[2]).toMatchObject({ callId: "t1", items: [{ text: "a", state: "done" }, { text: "b", state: "pending" }] });
|
|
148
|
+
expect((await all(reading().read(id("sess_c"), records[2]!.locator))).map((r) => r.kind)).toEqual(["user"]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("only the tail of a running session is incomplete; an older unfinished message is whole", async () => {
|
|
152
|
+
writeSession(db, "sess_a", [
|
|
153
|
+
user("m1", "go"),
|
|
154
|
+
assistant("m2", "killed in the middle", false),
|
|
155
|
+
user("m3", "go on"),
|
|
156
|
+
tool("m4", "completed"),
|
|
157
|
+
{ id: "m5", data: { role: "assistant", time: { created: 5 } }, parts: [
|
|
158
|
+
{ type: "tool", callID: "c1", tool: "Bash", state: { status: "completed", input: {}, output: "ok" } },
|
|
159
|
+
{ type: "tool", callID: "c2", tool: "Bash", state: { status: "running", input: {} } },
|
|
160
|
+
{ type: "tool", callID: "c3", tool: "Bash", state: { status: "completed", input: {}, output: "late" } },
|
|
161
|
+
] },
|
|
162
|
+
]);
|
|
163
|
+
const records = await all(reading().read(id("sess_a")));
|
|
164
|
+
const marks = records.map((record) => `${record.kind}:${record.incomplete}`);
|
|
165
|
+
expect(marks.slice(0, 5)).toEqual(["user:false", "assistant:false", "user:false", "tool-call:false", "tool-result:false"]);
|
|
166
|
+
// The running message is the tail: from the running tool on, nothing is whole, the late result included.
|
|
167
|
+
expect(marks.slice(5)).toEqual(["tool-call:true", "tool-result:true", "tool-call:true", "tool-call:true", "tool-result:true"]);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("a thought still being written at the tail is incomplete, and one in an older message is whole", async () => {
|
|
171
|
+
writeSession(db, "sess_a", [
|
|
172
|
+
user("m1", "go"),
|
|
173
|
+
{ id: "m2", data: { role: "assistant", time: { created: 2 } }, parts: [{ type: "reasoning", text: "old", time: { start: 2 } }] },
|
|
174
|
+
user("m3", "go on"),
|
|
175
|
+
{ id: "m4", data: { role: "assistant", time: { created: 4 } }, parts: [{ type: "reasoning", text: "live", time: { start: 4 } }] },
|
|
176
|
+
]);
|
|
177
|
+
const records = await all(reading().read(id("sess_a")));
|
|
178
|
+
expect(records.map((record) => `${record.kind}:${record.incomplete}`)).toEqual(["user:false", "thinking:false", "user:false", "thinking:true"]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a call left running in an older message does not hold back what came after it", async () => {
|
|
182
|
+
writeSession(db, "sess_a", [
|
|
183
|
+
user("m1", "go"),
|
|
184
|
+
{ id: "m2", data: { role: "assistant", time: { created: 2 } }, parts: [{ type: "tool", callID: "c1", tool: "Bash", state: { status: "running", input: {} } }] },
|
|
185
|
+
user("m3", "go on"),
|
|
186
|
+
assistant("m4", "done"),
|
|
187
|
+
]);
|
|
188
|
+
const records = await all(reading().read(id("sess_a")));
|
|
189
|
+
// The abandoned call is delivered as it stands, with no result invented for it, and nothing after it is incomplete.
|
|
190
|
+
expect(records.map((record) => `${record.kind}:${record.incomplete}`)).toEqual(["user:false", "tool-call:false", "user:false", "assistant:false"]);
|
|
191
|
+
expect((await reading().coverage(id("sess_a"), records[1]!.locator)).status).toBe("not-scanned");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("the tail marker and the rows come from one snapshot: a message written in between is not taken for a finished one", async () => {
|
|
195
|
+
// ZCode's own database is in WAL mode: a reader holds a snapshot and a writer is not held up by it.
|
|
196
|
+
db.run("pragma journal_mode = wal");
|
|
197
|
+
writeSession(db, "sess_a", [user("m1", "one"), assistant("m2", "two")]);
|
|
198
|
+
const writer = new Database(dbPathOf(home));
|
|
199
|
+
const reader = new Database(dbPathOf(home), { readonly: true });
|
|
200
|
+
// The write lands right after the reader asked which message is the last.
|
|
201
|
+
const spying = new Proxy(reader, {
|
|
202
|
+
get(target, key) {
|
|
203
|
+
if (key !== "query") return Reflect.get(target, key).bind?.(target) ?? Reflect.get(target, key);
|
|
204
|
+
return (sql: string) => {
|
|
205
|
+
const query = target.query(sql);
|
|
206
|
+
if (!sql.includes("max(sequence)")) return query;
|
|
207
|
+
return {
|
|
208
|
+
get: (...args: never[]) => {
|
|
209
|
+
const found = query.get(...args);
|
|
210
|
+
writer.query("insert into message (id, session_id, time_created, time_updated, data, sequence) values ('m3', 'sess_a', 3, 3, ?, 2)").run(JSON.stringify({ role: "assistant", time: { created: 3 } }));
|
|
211
|
+
writer.query("insert into part (id, message_id, session_id, time_created, time_updated, data, sequence) values ('m3p', 'm3', 'sess_a', 3, 3, ?, 0)").run(JSON.stringify({ type: "text", text: "half" }));
|
|
212
|
+
return found;
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
try {
|
|
219
|
+
const seen = [...itemsOf(spying, harnessId("zcode-test"), "sess_a", undefined)];
|
|
220
|
+
// What the reader had asked about is all it gives: the half-written message is for the next read.
|
|
221
|
+
expect(seen.map((item) => item.record.kind)).toEqual(["user", "assistant"]);
|
|
222
|
+
expect(seen.every((item) => !item.record.incomplete)).toBe(true);
|
|
223
|
+
} finally {
|
|
224
|
+
reader.close();
|
|
225
|
+
writer.close();
|
|
226
|
+
}
|
|
227
|
+
const next = await all(reading().read(id("sess_a")));
|
|
228
|
+
expect(next.map((record) => `${record.kind}:${record.incomplete}`)).toEqual(["user:false", "assistant:false", "assistant:true"]);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("a session spawned inside sits after the part that existed when it was created, whichever way it names its parent", async () => {
|
|
232
|
+
writeSession(db, "sess_a", [user("m1", "go"), assistant("m2", "spawning"), assistant("m3", "later")], { at: 1000 });
|
|
233
|
+
writeSession(db, "sess_sub1", [user("s1", "work")], { parent: "sess_a", at: 1015 });
|
|
234
|
+
db.run("create table session_task_link (id text primary key, parent_session_id text, child_session_id text not null)");
|
|
235
|
+
writeSession(db, "sess_actor", [user("s2", "work")], { at: 1015 });
|
|
236
|
+
db.run("insert into session_task_link values ('l1', 'sess_a', 'sess_actor')");
|
|
237
|
+
// Created before anything was said: it comes first.
|
|
238
|
+
writeSession(db, "sess_early", [user("s3", "work")], { parent: "sess_a", at: 5 });
|
|
239
|
+
|
|
240
|
+
const records = await all(reading().read(id("sess_a")));
|
|
241
|
+
expect(records.map((record) => (record.kind === "nested" ? `nested:${String(record.child.native)}` : record.kind))).toEqual([
|
|
242
|
+
"nested:sess_early",
|
|
243
|
+
"user",
|
|
244
|
+
"assistant",
|
|
245
|
+
"nested:sess_actor",
|
|
246
|
+
"nested:sess_sub1",
|
|
247
|
+
"assistant",
|
|
248
|
+
]);
|
|
249
|
+
const nested = records.find((record) => record.kind === "nested" && record.child.native === "sess_sub1");
|
|
250
|
+
expect(await reading().raw(id("sess_a"), nested!.locator)).toEqual({ kind: "child", session: "sess_sub1" });
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("reading from a locator gives what follows, also after the session grew", async () => {
|
|
254
|
+
writeSession(db, "sess_a", [user("m1", "one"), tool("m2", "completed")]);
|
|
255
|
+
const first = await all(reading().read(id("sess_a")));
|
|
256
|
+
expect(first.map((record) => record.kind)).toEqual(["user", "tool-call", "tool-result"]);
|
|
257
|
+
// Half a part's records were delivered: the call, not yet the result.
|
|
258
|
+
expect((await all(reading().read(id("sess_a"), first[1]!.locator))).map((record) => record.kind)).toEqual(["tool-result"]);
|
|
259
|
+
|
|
260
|
+
db.query("insert into message (id, session_id, time_created, time_updated, data, sequence) values ('m3', 'sess_a', 3, 3, ?, 2)").run(JSON.stringify(user("m3", "").data));
|
|
261
|
+
db.query("insert into part (id, message_id, session_id, time_created, time_updated, data, sequence) values ('m3p', 'm3', 'sess_a', 3, 3, ?, 0)").run(JSON.stringify({ type: "text", text: "two" }));
|
|
262
|
+
const after = await all(reading().read(id("sess_a"), first[2]!.locator));
|
|
263
|
+
expect(after.map((record) => (record.kind === "user" ? record.text : record.kind))).toEqual(["two"]);
|
|
264
|
+
expect((await reading().coverage(id("sess_a"), after[0]!.locator)).status).toBe("empty");
|
|
265
|
+
await expect(all(reading().read(id("sess_a"), LocatorSchema.parse("elsewhere:1")))).rejects.toThrow("not a locator of this side");
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("an unfamiliar or broken part is unknown with what it was, and raw gives the rows whole", async () => {
|
|
269
|
+
writeSession(db, "sess_a", [{ id: "m1", data: { role: "assistant", time: { created: 1, completed: 1 } }, parts: [{ type: "frobnicate", a: 1 }, "{broken"] }]);
|
|
270
|
+
const records = await all(reading().read(id("sess_a")));
|
|
271
|
+
expect(records.map((record) => record.kind)).toEqual(["unknown", "unknown"]);
|
|
272
|
+
expect(records[0]).toMatchObject({ raw: { type: "frobnicate", a: 1 } });
|
|
273
|
+
expect(records[1]).toMatchObject({ raw: "{broken" });
|
|
274
|
+
const raw = await reading().raw(id("sess_a"), records[1]!.locator);
|
|
275
|
+
expect(raw).toMatchObject({ kind: "part", part: "{broken", message: { role: "assistant" } });
|
|
276
|
+
expect(reading().rawSchema.safeParse(raw).success).toBe(true);
|
|
277
|
+
await expect(reading().raw(id("sess_a"), LocatorSchema.parse("zcode:v1:9:9:0"))).rejects.toThrow("no record");
|
|
278
|
+
await expect(reading().raw(id("sess_nope"), records[0]!.locator)).rejects.toThrow("not in the database");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
/** One row of each part type ZCode writes, as small as it can be, and the records the reading side makes of it. */
|
|
282
|
+
const PART_ROWS: Record<(typeof PART_TYPES)[number], { part: Record<string, unknown>; kinds: SessionRecord["kind"][] }> = {
|
|
283
|
+
text: { part: { type: "text", text: "said" }, kinds: ["assistant"] },
|
|
284
|
+
reasoning: { part: { type: "reasoning", text: "thinking" }, kinds: ["thinking"] },
|
|
285
|
+
file: { part: { type: "file", mime: "image/png", url: "file:///a.png" }, kinds: ["unknown"] },
|
|
286
|
+
agent: { part: { type: "agent", name: "reviewer" }, kinds: ["unknown"] },
|
|
287
|
+
compaction: { part: { type: "compaction", auto: true }, kinds: ["compaction"] },
|
|
288
|
+
timeline: { part: { type: "timeline", timelineType: "model_change", display: "separator" }, kinds: [] },
|
|
289
|
+
subtask: { part: { type: "subtask", prompt: "p", description: "d", agent: "build" }, kinds: ["unknown"] },
|
|
290
|
+
retry: { part: { type: "retry", attempt: 1, error: {}, time: { created: 1 } }, kinds: ["unknown"] },
|
|
291
|
+
"step-start": { part: { type: "step-start" }, kinds: [] },
|
|
292
|
+
"step-finish": { part: { type: "step-finish", reason: "stop", cost: 0, tokens: {} }, kinds: [] },
|
|
293
|
+
snapshot: { part: { type: "snapshot", snapshot: "abc" }, kinds: ["unknown"] },
|
|
294
|
+
patch: { part: { type: "patch", hash: "abc", files: ["a.ts"] }, kinds: ["unknown"] },
|
|
295
|
+
tool: { part: { type: "tool", callID: "call_1", tool: "Bash", state: { status: "completed", input: {}, output: "o", time: { start: 1, end: 2 } } }, kinds: ["tool-call", "tool-result"] },
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
test("every part type ZCode writes has a deliberate mapping: a record, none, or the part kept whole", async () => {
|
|
299
|
+
expect(Object.keys(PART_ROWS).sort()).toEqual([...PART_TYPES].sort());
|
|
300
|
+
for (const [type, { part, kinds }] of Object.entries(PART_ROWS)) {
|
|
301
|
+
writeSession(db, `sess_${type}`, [{ id: `m_${type}`, data: { role: "assistant", time: { created: 1, completed: 1 } }, parts: [part] }]);
|
|
302
|
+
const records = await all(reading().read(id(`sess_${type}`)));
|
|
303
|
+
expect(records.map((record) => record.kind), type).toEqual(kinds);
|
|
304
|
+
// A part kept whole keeps what it was.
|
|
305
|
+
if (kinds[0] === "unknown") expect(records[0], type).toMatchObject({ raw: part });
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("the compaction record carries the summary the compaction left", async () => {
|
|
310
|
+
writeSession(db, "sess_a", [
|
|
311
|
+
{ id: "ms", data: { role: "user", summary: { body: "the story so far" } }, parts: [{ type: "text", text: "continued from..." }] },
|
|
312
|
+
{ id: "mb", data: { role: "assistant", time: { created: 1, completed: 1 } }, parts: [{ type: "compaction", summaryMessageId: "ms" }, { type: "compaction", summaryMessageId: "missing" }] },
|
|
313
|
+
]);
|
|
314
|
+
const records = await all(reading().read(id("sess_a")));
|
|
315
|
+
expect(records).toMatchObject([{ kind: "compaction", summary: "the story so far" }, { kind: "compaction", summary: "" }]);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test("coverage says not-scanned for records unread, and unavailable for a session that is not there", async () => {
|
|
319
|
+
writeSession(db, "sess_a", [user("m1", "one")]);
|
|
320
|
+
writeSession(db, "sess_hidden", [{ id: "h1", data: { role: "user", metadata: { visibility: "model-only" } }, parts: [{ type: "text", text: "x" }] }]);
|
|
321
|
+
expect((await reading().coverage(id("sess_a"))).status).toBe("not-scanned");
|
|
322
|
+
// Only what the harness injected: no record, so nothing to scan.
|
|
323
|
+
expect((await reading().coverage(id("sess_hidden"))).status).toBe("empty");
|
|
324
|
+
expect((await reading().coverage(id("sess_nope"))).status).toBe("unavailable");
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("the directory of a session is the one the database has", () => {
|
|
328
|
+
writeSession(db, "sess_a", [user("m1", "one")], { directory: "/work/here" });
|
|
329
|
+
expect(directoryOf(home, "sess_a")).toBe("/work/here");
|
|
330
|
+
expect(directoryOf(home, "sess_nope")).toBeUndefined();
|
|
331
|
+
expect(directoryOf(`${home}/missing`, "sess_a")).toBeUndefined();
|
|
332
|
+
});
|