@niwa-labs/memory 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 +107 -2
- package/package.json +33 -4
- package/src/cockpit/actions.ts +95 -0
- package/src/cockpit/index.tsx +23 -0
- package/src/cockpit/notes.ts +78 -0
- package/src/cockpit/page.tsx +191 -0
- package/src/consolidate.ts +113 -0
- package/src/context.ts +41 -0
- package/src/fold.ts +308 -0
- package/src/holds.ts +13 -0
- package/src/index.ts +775 -0
- package/src/records.ts +538 -0
- package/src/same.ts +15 -0
- package/src/search.ts +48 -0
- package/src/share-question.ts +27 -0
- package/src/top.ts +44 -0
- package/test/cockpit-actions.test.ts +53 -0
- package/test/cockpit-notes.test.ts +89 -0
- package/test/consolidate.test.ts +171 -0
- package/test/context.test.ts +241 -0
- package/test/corroboration.test.ts +61 -0
- package/test/delegate-context.test.ts +16 -0
- package/test/fixtures/host.ts +229 -0
- package/test/intake.test.ts +69 -0
- package/test/notes.test.ts +124 -0
- package/test/projects.test.ts +37 -0
- package/test/recovery.test.ts +174 -0
- package/test/search.test.ts +88 -0
- package/test/top.test.ts +84 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// A host with the base stack, a stand-in for the agents module and the memory module: what the checks of
|
|
2
|
+
// the module share. The memory is given a binding the check steers, so a call made from the check speaks
|
|
3
|
+
// as the address the check names.
|
|
4
|
+
|
|
5
|
+
import { action, actionName, actionRef, owner, recordId } from "@niwa-labs/core";
|
|
6
|
+
import type { Action, ActionContext, ActionName, Address, OperationId, RecordType, StoredRecord } from "@niwa-labs/core";
|
|
7
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import type { CallerBinding, Plugin } from "@niwa-labs/plugin";
|
|
10
|
+
import { AGENT_BRIEFING, AGENT_PROMPT_SECTION, AGENT_TYPES, BriefingSectionOutput, PromptSectionInput, PromptSectionOutput, agentBriefing, agentGet } from "@niwa-labs/agent/records";
|
|
11
|
+
import type { AgentDefinitionInput } from "@niwa-labs/agent/records";
|
|
12
|
+
import { baseStack, createNiwa, gate, tempHome, testClock } from "@niwa-labs/testing";
|
|
13
|
+
import type { Host, TestClock } from "@niwa-labs/testing";
|
|
14
|
+
|
|
15
|
+
import { memory } from "../../src/index.ts";
|
|
16
|
+
import type { MemoryOptions } from "../../src/index.ts";
|
|
17
|
+
|
|
18
|
+
/** What the stand-in composes of the prompt of an agent's session: the sections plugins offer, as the module reads them at a start. */
|
|
19
|
+
export const agentPromptSections = actionRef("agent.prompt.sections", {
|
|
20
|
+
description: "The sections plugins offer to the prompt of an agent's sessions, as the stand-in reads them at a start.",
|
|
21
|
+
input: PromptSectionInput,
|
|
22
|
+
output: z.object({ sections: z.array(z.object({ id: z.string(), text: z.string() })) }),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The agents module as far as the memory reads it: it declares the agents' records, answers `agent.get`
|
|
27
|
+
* for the agents it is given, and writes a briefing of the sections plugins offer, as the module does.
|
|
28
|
+
*/
|
|
29
|
+
export function agentsStandIn(agents: readonly AgentDefinitionInput[] = [], hold?: Promise<void>): Plugin {
|
|
30
|
+
return plugin("agents", (api) => {
|
|
31
|
+
api.declare(...AGENT_TYPES);
|
|
32
|
+
api.action(
|
|
33
|
+
action(agentGet, {
|
|
34
|
+
recovery: "repeat",
|
|
35
|
+
effect: "read",
|
|
36
|
+
world: "niwa",
|
|
37
|
+
run: async ({ agent }) => {
|
|
38
|
+
// A check holds the answer back to act while a caller waits for the agent's definition.
|
|
39
|
+
await hold;
|
|
40
|
+
const definition = agents.find((one) => one.name === agent);
|
|
41
|
+
if (definition === undefined) throw new Error(`there is no agent "${agent}"`);
|
|
42
|
+
const address = { kind: "agent" as const, id: agent };
|
|
43
|
+
return { name: agent, address, source: "config" as const, status: "asleep" as const, definition: { hands: [], tools: "all" as const, ...definition }, queued: 0, attached: [] };
|
|
44
|
+
},
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
api.action(
|
|
48
|
+
action(agentPromptSections, {
|
|
49
|
+
recovery: "repeat",
|
|
50
|
+
effect: "read",
|
|
51
|
+
world: "niwa",
|
|
52
|
+
run: async ({ agent }, ctx) => {
|
|
53
|
+
const sections: Array<{ id: string; text: string }> = [];
|
|
54
|
+
for (const { plugin: by, value } of api.offers(AGENT_PROMPT_SECTION)) {
|
|
55
|
+
if (value.agent !== undefined && value.agent !== agent) continue;
|
|
56
|
+
const written = await ctx.step.outcome<unknown, unknown>(actionName(value.action), { agent });
|
|
57
|
+
const parsed = written.status === "completed" ? PromptSectionOutput.safeParse(written.value) : undefined;
|
|
58
|
+
if (parsed?.success === true && parsed.data.text.trim() !== "") sections.push({ id: `${String(by)}:${value.id}`, text: parsed.data.text.trim() });
|
|
59
|
+
}
|
|
60
|
+
return { sections };
|
|
61
|
+
},
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
api.action(
|
|
65
|
+
action(agentBriefing, {
|
|
66
|
+
recovery: "repeat",
|
|
67
|
+
effect: "read",
|
|
68
|
+
world: "niwa",
|
|
69
|
+
run: async ({ agent }, ctx) => {
|
|
70
|
+
const sections: string[] = [];
|
|
71
|
+
for (const { value } of api.offers(AGENT_BRIEFING)) {
|
|
72
|
+
const written = await ctx.step.outcome<unknown, unknown>(actionName(value.action), { agent });
|
|
73
|
+
const parsed = written.status === "completed" ? BriefingSectionOutput.safeParse(written.value) : undefined;
|
|
74
|
+
if (parsed?.success === true && parsed.data.text.trim() !== "") sections.push(`${value.title}:\n${parsed.data.text.trim()}`);
|
|
75
|
+
}
|
|
76
|
+
return { text: sections.join("\n\n") };
|
|
77
|
+
},
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The module with its actions kept as it registers them, so a check can run one under an operation of its own. */
|
|
84
|
+
function capturing(made: Plugin, afterClaim?: () => Promise<void>): { plugin: Plugin; actions: Map<string, Action<any, any>> } {
|
|
85
|
+
const actions = new Map<string, Action<any, any>>();
|
|
86
|
+
const wrapped = plugin(
|
|
87
|
+
made.name,
|
|
88
|
+
(api) => {
|
|
89
|
+
const spy = new Proxy(api, {
|
|
90
|
+
get(target, key) {
|
|
91
|
+
if (key === "action") {
|
|
92
|
+
return (one: Action<any, any>) => {
|
|
93
|
+
actions.set(one.name, one);
|
|
94
|
+
if (one.name !== "memory.consolidate" || afterClaim === undefined) return target.action(one);
|
|
95
|
+
target.action({ ...one, run: (input, ctx) => one.run(input, {
|
|
96
|
+
...ctx,
|
|
97
|
+
async emit(type, data) {
|
|
98
|
+
const written = await ctx.emit(type, data);
|
|
99
|
+
if (type.name === "memory.consolidation.started") await afterClaim();
|
|
100
|
+
return written;
|
|
101
|
+
},
|
|
102
|
+
}) });
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const value: unknown = Reflect.get(target, key);
|
|
106
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
made.setup(spy);
|
|
110
|
+
},
|
|
111
|
+
{ version: made.version },
|
|
112
|
+
);
|
|
113
|
+
return { plugin: wrapped, actions };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface MemoryHost {
|
|
117
|
+
readonly host: Host;
|
|
118
|
+
/**
|
|
119
|
+
* Run an action as the host does for an operation, and as it is run again after a cut: the records it writes are
|
|
120
|
+
* given the ids of the operation (`<operation>/emit/<n>`), so a repeat meets what the first run wrote. `cutAfter`
|
|
121
|
+
* stops the run after that many records, as a crash between writing a record and saving the result does.
|
|
122
|
+
*/
|
|
123
|
+
again<O = unknown>(name: string, input: unknown, operation: string, cutAfter?: number): Promise<O>;
|
|
124
|
+
readonly home: string;
|
|
125
|
+
readonly clock: TestClock;
|
|
126
|
+
/** Call an action as an address: an agent, the owner; undefined calls it as the host, bound to nobody. */
|
|
127
|
+
as<O = unknown>(who: Address | undefined, name: string, input: unknown): Promise<O>;
|
|
128
|
+
records(): Promise<StoredRecord[]>;
|
|
129
|
+
close(): Promise<void>;
|
|
130
|
+
/** Close the host and keep its home, for a host that is opened on the same home later, which then removes it. */
|
|
131
|
+
leave(): Promise<Home>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A home a host runs in, and the way to remove it. */
|
|
135
|
+
type Home = Awaited<ReturnType<typeof tempHome>>;
|
|
136
|
+
|
|
137
|
+
const open: MemoryHost[] = [];
|
|
138
|
+
|
|
139
|
+
/** Close every host a check opened. */
|
|
140
|
+
export async function closeAll(): Promise<void> {
|
|
141
|
+
for (const host of open.splice(0)) await host.close();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export const rin: Address = { kind: "agent", id: "rin" };
|
|
145
|
+
export const bob: Address = { kind: "agent", id: "bob" };
|
|
146
|
+
export { owner };
|
|
147
|
+
|
|
148
|
+
/** A host with the memory module; the options are the module's, `agents` the agents the stand-in knows. */
|
|
149
|
+
export async function memoryHost(options: Omit<MemoryOptions, "bind"> = {}, more: { agents?: AgentDefinitionInput[]; after?: Plugin[]; afterClaim?: () => Promise<void>; hold?: Promise<void>; home?: Home; without?: boolean } = {}): Promise<MemoryHost> {
|
|
150
|
+
const home = more.home ?? (await tempHome());
|
|
151
|
+
const clock = testClock(Date.UTC(2026, 10, 1));
|
|
152
|
+
const niwa = createNiwa({ home: home.path, clock });
|
|
153
|
+
let speaking: Address | undefined;
|
|
154
|
+
// The check speaks through the host; a client with the owner's token speaks as the owner.
|
|
155
|
+
const bind: CallerBinding = (caller) => {
|
|
156
|
+
if (caller.kind === "host") return speaking;
|
|
157
|
+
if (caller.kind === "client" && caller.principal?.kind === "owner") return owner;
|
|
158
|
+
return undefined;
|
|
159
|
+
};
|
|
160
|
+
const { plugin: module, actions } = capturing(memory({ ...options, bind }), more.afterClaim);
|
|
161
|
+
niwa.use(...baseStack().plugins, agentsStandIn(more.agents, more.hold), ...(more.without === true ? [] : [module]), ...(more.after ?? []));
|
|
162
|
+
await niwa.serve();
|
|
163
|
+
let closed = false;
|
|
164
|
+
const made: MemoryHost = {
|
|
165
|
+
host: niwa,
|
|
166
|
+
home: home.path,
|
|
167
|
+
clock,
|
|
168
|
+
async as<O>(who: Address | undefined, name: string, input: unknown): Promise<O> {
|
|
169
|
+
speaking = who;
|
|
170
|
+
try {
|
|
171
|
+
return (await niwa.call(actionName(name) as ActionName, input as never)) as O;
|
|
172
|
+
} finally {
|
|
173
|
+
speaking = undefined;
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
async again<O>(name: string, input: unknown, operation: string, cutAfter = Number.POSITIVE_INFINITY): Promise<O> {
|
|
177
|
+
const found = actions.get(name);
|
|
178
|
+
if (found === undefined) throw new Error(`the memory has no action ${name}`);
|
|
179
|
+
let ordinal = 0;
|
|
180
|
+
const ctx = {
|
|
181
|
+
operationId: operation as OperationId,
|
|
182
|
+
caller: { kind: "host" },
|
|
183
|
+
emit: async <T>(type: RecordType<T>, data: T): Promise<StoredRecord> => {
|
|
184
|
+
if (ordinal >= cutAfter) throw new Error("cut");
|
|
185
|
+
ordinal += 1;
|
|
186
|
+
return niwa.emit(type, data, { id: recordId(`${operation}/emit/${ordinal}`) });
|
|
187
|
+
},
|
|
188
|
+
} as unknown as ActionContext;
|
|
189
|
+
return (await found.run(found.input.parse(input), ctx)) as O;
|
|
190
|
+
},
|
|
191
|
+
records: () => niwa.records.read(),
|
|
192
|
+
async close() {
|
|
193
|
+
if (closed) return;
|
|
194
|
+
closed = true;
|
|
195
|
+
await niwa.close();
|
|
196
|
+
await home.cleanup();
|
|
197
|
+
},
|
|
198
|
+
async leave() {
|
|
199
|
+
if (closed) return home;
|
|
200
|
+
closed = true;
|
|
201
|
+
await niwa.close();
|
|
202
|
+
return home;
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
open.push(made);
|
|
206
|
+
return made;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The data of the records of a type, in order. */
|
|
210
|
+
export const dataOf = (records: readonly StoredRecord[], type: string): unknown[] => records.filter((one) => one.type === type).map((one) => one.data);
|
|
211
|
+
|
|
212
|
+
/** Wait until the journal holds as many records of a type. */
|
|
213
|
+
export async function until(host: MemoryHost, type: string, count = 1, ms = 10_000): Promise<StoredRecord[]> {
|
|
214
|
+
for (let waited = 0; waited < ms; waited += 20) {
|
|
215
|
+
const all = await host.records();
|
|
216
|
+
if (all.filter((one) => one.type === type).length >= count) return all;
|
|
217
|
+
await Bun.sleep(20);
|
|
218
|
+
}
|
|
219
|
+
throw new Error(`fewer than ${String(count)} ${type} were written`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The crash test holds a real client call immediately after admission of its claim.
|
|
223
|
+
if (import.meta.main) {
|
|
224
|
+
const path = process.argv[2]!;
|
|
225
|
+
await memoryHost({}, {
|
|
226
|
+
home: {path, cleanup: async () => {}},
|
|
227
|
+
afterClaim: () => gate(path, "finish-consolidation").wait(),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Intake: the digest of a session attached to an agent is taken into the agent's memory, as where it is
|
|
2
|
+
// and what it tells of, not as a copy; a search then finds what the session did.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { AgentAttached, AgentDetached, AgentTurnQueued } from "@niwa-labs/agent/records";
|
|
7
|
+
|
|
8
|
+
import { closeAll, dataOf, memoryHost, rin, until } from "./fixtures/host.ts";
|
|
9
|
+
|
|
10
|
+
afterEach(closeAll);
|
|
11
|
+
|
|
12
|
+
const digest = (agent: string, turn: string, text: string, session = "claude s-1") => ({
|
|
13
|
+
agent,
|
|
14
|
+
turn,
|
|
15
|
+
kind: "fact" as const,
|
|
16
|
+
author: { kind: "niwa" as const, by: "workflow" },
|
|
17
|
+
text,
|
|
18
|
+
source: { kind: "session-digest" as const, session, from: "l-1", upto: "l-9" },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("the digest of an attached session is taken into the agent's memory as a pointer, with the session's project", async () => {
|
|
22
|
+
const host = await memoryHost();
|
|
23
|
+
await host.host.emit(AgentAttached, { agent: "rin", session: "claude s-1", cwd: "/work/billing" });
|
|
24
|
+
const told = await host.host.emit(AgentTurnQueued, digest("rin", "fact-a1b2c3", "The session paused. What it did:\nThe owner: move invoices to hourly"));
|
|
25
|
+
// A fact with no digest behind it is not taken.
|
|
26
|
+
await host.host.emit(AgentTurnQueued, { agent: "rin", turn: "fact-d4e5f6", kind: "fact", author: { kind: "owner" }, text: "CI is red" });
|
|
27
|
+
const records = await until(host, "memory.digest.taken");
|
|
28
|
+
expect(dataOf(records, "memory.digest.taken")).toEqual([
|
|
29
|
+
{ memory: "rin", turn: "fact-a1b2c3", record: told.id, session: "claude s-1", from: "l-1", upto: "l-9", project: "/work/billing" },
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
type Hit = { kind: string; id: string; title?: string; text: string; project?: string };
|
|
33
|
+
const { hits } = await host.as<{ hits: Hit[] }>(rin, "memory.search", { query: "hourly invoices" });
|
|
34
|
+
expect(hits).toHaveLength(1);
|
|
35
|
+
expect(hits[0]).toMatchObject({ kind: "session", id: "fact-a1b2c3", title: "claude s-1", project: "/work/billing" });
|
|
36
|
+
expect(hits[0]!.text).toContain("move invoices to hourly");
|
|
37
|
+
expect((await host.as<{ hits: Hit[] }>(rin, "memory.search", { query: "invoices", project: "/work/site" })).hits).toEqual([]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("each agent takes its own digests, and the oldest beyond the limit are no longer searched", async () => {
|
|
41
|
+
const host = await memoryHost({ limits: { sessions: 2 } });
|
|
42
|
+
await host.host.emit(AgentTurnQueued, digest("bob", "fact-000001", "The owner: rename the billing module", "claude s-9"));
|
|
43
|
+
for (const [n, word] of ["alpha", "beta", "gamma"].entries()) await host.host.emit(AgentTurnQueued, digest("rin", `fact-00010${String(n)}`, `The owner: talk about ${word}`));
|
|
44
|
+
await until(host, "memory.digest.taken", 4);
|
|
45
|
+
type Hits = { hits: Array<{ id: string }> };
|
|
46
|
+
expect((await host.as<Hits>(rin, "memory.search", { query: "billing" })).hits).toEqual([]);
|
|
47
|
+
expect((await host.as<Hits>({ kind: "agent", id: "bob" }, "memory.search", { query: "billing" })).hits.map((hit) => hit.id)).toEqual(["fact-000001"]);
|
|
48
|
+
expect((await host.as<Hits>(rin, "memory.search", { query: "alpha" })).hits).toEqual([]);
|
|
49
|
+
expect((await host.as<Hits>(rin, "memory.search", { query: "gamma" })).hits.map((hit) => hit.id)).toEqual(["fact-000102"]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("the project of a digest is where its session was attached when the digest was told, however the directory is spelled", async () => {
|
|
53
|
+
// The records are written before the memory is on the host, so the memory takes them in later, in order.
|
|
54
|
+
const before = await memoryHost({}, { without: true });
|
|
55
|
+
await before.host.emit(AgentAttached, { agent: "rin", session: "claude s-1", cwd: "/work//billing/./" });
|
|
56
|
+
const told = await before.host.emit(AgentTurnQueued, digest("rin", "fact-a1b2c3", "The session paused. What it did:\nThe owner: move invoices to hourly"));
|
|
57
|
+
await before.host.emit(AgentAttached, { agent: "rin", session: "claude s-1", cwd: "/work/shop" });
|
|
58
|
+
await before.host.emit(AgentTurnQueued, digest("rin", "fact-d4e5f6", "The session paused. What it did:\nThe owner: rename the shop"));
|
|
59
|
+
await before.host.emit(AgentDetached, { agent: "rin", session: "claude s-1" });
|
|
60
|
+
await before.host.emit(AgentTurnQueued, digest("rin", "fact-0a0b0c", "The session ended. What it did:\nThe owner: nothing more"));
|
|
61
|
+
const host = await memoryHost({}, { home: await before.leave() });
|
|
62
|
+
const records = await until(host, "memory.digest.taken", 3);
|
|
63
|
+
expect(dataOf(records, "memory.digest.taken")).toMatchObject([
|
|
64
|
+
{ record: told.id, project: "/work/billing" },
|
|
65
|
+
{ turn: "fact-d4e5f6", project: "/work/shop" },
|
|
66
|
+
{ turn: "fact-0a0b0c" },
|
|
67
|
+
]);
|
|
68
|
+
expect((dataOf(records, "memory.digest.taken")[2] as { project?: string }).project).toBeUndefined();
|
|
69
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Notes: an agent writes to its own memory and reaches no other; the owner reaches every memory, and only
|
|
2
|
+
// his word is his. A note can be confirmed, corrected, taken back and erased, and read as it stood earlier.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { connectAsOwner } from "@niwa-labs/testing";
|
|
7
|
+
|
|
8
|
+
import { bob, closeAll, dataOf, memoryHost, owner, rin } from "./fixtures/host.ts";
|
|
9
|
+
|
|
10
|
+
afterEach(closeAll);
|
|
11
|
+
|
|
12
|
+
type Noted = { memory: string; note: string };
|
|
13
|
+
type View = { note: string; text: string; detail?: string; status: string; confirmed: boolean; revision: number; seq: number; written: number; source: { kind: string }; by: unknown };
|
|
14
|
+
|
|
15
|
+
test("an agent writes a note to its own memory, and the owner reads it by naming the memory", async () => {
|
|
16
|
+
const host = await memoryHost();
|
|
17
|
+
const noted = await host.as<Noted>(rin, "memory.note", { text: "The release goes out on Fridays.", detail: "Said in the planning session." });
|
|
18
|
+
expect(noted.memory).toBe("rin");
|
|
19
|
+
expect(noted.note).toMatch(/^note-[0-9a-f]{12}$/);
|
|
20
|
+
const written = dataOf(await host.records(), "memory.note.written");
|
|
21
|
+
expect(written).toEqual([
|
|
22
|
+
{ memory: "rin", note: noted.note, text: "The release goes out on Fridays.", detail: "Said in the planning session.", about: "work", source: { kind: "inferred" }, by: rin },
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const seen = await host.as<View>(owner, "memory.get", { memory: "rin", note: noted.note });
|
|
26
|
+
expect(seen).toMatchObject({ text: "The release goes out on Fridays.", status: "live", confirmed: false, revision: 1 });
|
|
27
|
+
expect((await host.as<{ notes: View[] }>(rin, "memory.list", {})).notes.map((one) => one.note)).toEqual([noted.note]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("an agent reaches only its own memory, and the owner and the host must name the one they mean", async () => {
|
|
31
|
+
const host = await memoryHost();
|
|
32
|
+
const noted = await host.as<Noted>(rin, "memory.note", { text: "Bob's tests are slow." });
|
|
33
|
+
await expect(host.as(bob, "memory.get", { memory: "rin", note: noted.note })).rejects.toThrow(/only its own memory/);
|
|
34
|
+
await expect(host.as(bob, "memory.note", { memory: "rin", text: "Something else." })).rejects.toThrow(/only its own memory/);
|
|
35
|
+
await expect(host.as(owner, "memory.note", { text: "No memory named." })).rejects.toThrow(/name the memory/);
|
|
36
|
+
await expect(host.as(undefined, "memory.list", {})).rejects.toThrow(/name the memory/);
|
|
37
|
+
expect((await host.as<{ notes: View[] }>(undefined, "memory.list", { memory: "rin" })).notes).toHaveLength(1);
|
|
38
|
+
expect((await host.as<{ notes: View[] }>(bob, "memory.list", {})).notes).toEqual([]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("only the owner's own word has the owner as its source, and it is confirmed as it is written", async () => {
|
|
42
|
+
const host = await memoryHost();
|
|
43
|
+
await expect(host.as(rin, "memory.note", { text: "He prefers tea.", about: "owner", source: { kind: "owner" } })).rejects.toThrow(/only the owner/);
|
|
44
|
+
await expect(host.as(undefined, "memory.note", { memory: "rin", text: "He prefers tea.", about: "owner", source: { kind: "owner" } })).rejects.toThrow(/only the owner/);
|
|
45
|
+
const noted = await host.as<Noted>(owner, "memory.note", { memory: "rin", text: "I prefer tea.", about: "owner", source: { kind: "owner" } });
|
|
46
|
+
expect(await host.as<View>(rin, "memory.get", { note: noted.note })).toMatchObject({ confirmed: true, by: owner, source: { kind: "owner" } });
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("the owner confirms a note; a correction by the agent leaves it unconfirmed, one by the owner is his word", async () => {
|
|
50
|
+
const host = await memoryHost();
|
|
51
|
+
const { note } = await host.as<Noted>(rin, "memory.note", { text: "He wants short answers.", about: "owner", source: { kind: "session", session: "claude s-1", upto: "l-9" } });
|
|
52
|
+
await expect(host.as(rin, "memory.confirm", { memory: "rin", note })).rejects.toThrow(/only the owner/);
|
|
53
|
+
expect(await host.as(owner, "memory.confirm", { memory: "rin", note })).toEqual({ note, confirmed: true });
|
|
54
|
+
expect(await host.as<View>(rin, "memory.get", { note })).toMatchObject({ confirmed: true });
|
|
55
|
+
|
|
56
|
+
expect(await host.as(rin, "memory.correct", { note, text: "He wants short answers with the reason." })).toEqual({ note, revision: 2 });
|
|
57
|
+
expect(await host.as<View>(rin, "memory.get", { note })).toMatchObject({ confirmed: false, revision: 2, text: "He wants short answers with the reason." });
|
|
58
|
+
await host.as(owner, "memory.correct", { memory: "rin", note, text: "I want short answers." });
|
|
59
|
+
expect(await host.as<View>(rin, "memory.get", { note })).toMatchObject({ confirmed: true, revision: 3, text: "I want short answers." });
|
|
60
|
+
await expect(host.as(bob, "memory.correct", { memory: "rin", note, text: "x" })).rejects.toThrow(/only its own memory/);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("a memory is read as it stood at a position of the journal", async () => {
|
|
64
|
+
const host = await memoryHost();
|
|
65
|
+
const { note } = await host.as<Noted>(rin, "memory.note", { text: "CI runs on every push." });
|
|
66
|
+
const before = await host.as<View>(rin, "memory.get", { note });
|
|
67
|
+
await host.as(rin, "memory.correct", { note, text: "CI runs on every push to main." });
|
|
68
|
+
expect(await host.as<View>(rin, "memory.get", { note, at: before.seq })).toMatchObject({ text: "CI runs on every push.", revision: 1 });
|
|
69
|
+
expect(await host.as<View>(rin, "memory.get", { note })).toMatchObject({ text: "CI runs on every push to main.", revision: 2 });
|
|
70
|
+
await expect(host.as(rin, "memory.get", { note, at: before.written - 1 })).rejects.toThrow(/no note/);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("a retracted note leaves the list; an erased one keeps nothing of its text", async () => {
|
|
74
|
+
const host = await memoryHost();
|
|
75
|
+
const kept = await host.as<Noted>(rin, "memory.note", { text: "Deploys need a review." });
|
|
76
|
+
const taken = await host.as<Noted>(rin, "memory.note", { text: "Deploys need two reviews.", detail: "A guess." });
|
|
77
|
+
const gone = await host.as<Noted>(owner, "memory.note", { memory: "rin", text: "My phone number is 555.", about: "owner", source: { kind: "owner" } });
|
|
78
|
+
await host.as(rin, "memory.retract", { note: taken.note, reason: "one review is enough" });
|
|
79
|
+
await host.as(owner, "memory.erase", { memory: "rin", note: gone.note });
|
|
80
|
+
expect((await host.as<{ notes: View[] }>(rin, "memory.list", {})).notes.map((one) => one.note)).toEqual([kept.note]);
|
|
81
|
+
expect(await host.as<View>(rin, "memory.get", { note: taken.note })).toMatchObject({ status: "retracted", text: "Deploys need two reviews." });
|
|
82
|
+
const erased = await host.as<View>(rin, "memory.get", { note: gone.note });
|
|
83
|
+
expect(erased).toMatchObject({ status: "erased", text: "" });
|
|
84
|
+
expect(erased.detail).toBeUndefined();
|
|
85
|
+
// An erased note stays erased: it cannot be corrected back into the memory.
|
|
86
|
+
await expect(host.as(owner, "memory.correct", { memory: "rin", note: gone.note, text: "Back." })).rejects.toThrow(/erased/);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a client with the owner's token speaks as the owner", async () => {
|
|
90
|
+
const host = await memoryHost();
|
|
91
|
+
const client = await connectAsOwner(host.home);
|
|
92
|
+
try {
|
|
93
|
+
const noted = (await client.call("memory.note" as never, { memory: "rin", text: "I am on leave in May.", about: "owner", source: { kind: "owner" } } as never)) as Noted;
|
|
94
|
+
expect(await host.as<View>(rin, "memory.get", { note: noted.note })).toMatchObject({ confirmed: true, by: owner });
|
|
95
|
+
} finally {
|
|
96
|
+
await client.close();
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("an erased note leaves no trace in what the memory shows, and the same words make a new note", async () => {
|
|
101
|
+
const host = await memoryHost();
|
|
102
|
+
const { note } = await host.as<Noted>(owner, "memory.note", { memory: "rin", text: "My door code is 4711.", about: "owner", source: { kind: "owner" } });
|
|
103
|
+
await host.as(rin, "memory.lift", { note });
|
|
104
|
+
await host.as(owner, "memory.erase", { memory: "rin", note });
|
|
105
|
+
expect((await host.as<{ notes: string[] }>(rin, "memory.top", {})).notes).toEqual([]);
|
|
106
|
+
expect((await host.as<{ hits: unknown[] }>(rin, "memory.search", { query: "door code 4711" })).hits).toEqual([]);
|
|
107
|
+
expect((await host.as<{ items: unknown[] }>(rin, "memory.context", { memory: "rin", query: "door code", purpose: "to let a courier in" })).items).toEqual([]);
|
|
108
|
+
const again = await host.as<Noted & { corroborated: boolean }>(owner, "memory.note", { memory: "rin", text: "My door code is 4711.", about: "owner", source: { kind: "owner" } });
|
|
109
|
+
expect(again.corroborated).toBe(false);
|
|
110
|
+
expect(again.note).not.toBe(note);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("the notes a memory listed at a position are those that held then, and an erased one is in none", async () => {
|
|
114
|
+
const host = await memoryHost();
|
|
115
|
+
const first = await host.as<Noted>(rin, "memory.note", { text: "Releases go out on Fridays." });
|
|
116
|
+
const at = Number((await host.records()).at(-1)!.seq);
|
|
117
|
+
const second = await host.as<Noted>(rin, "memory.note", { text: "Releases are announced in the channel." });
|
|
118
|
+
const listed = async (input: Record<string, unknown>) => (await host.as<{ notes: View[] }>(rin, "memory.list", input)).notes.map((one) => one.note);
|
|
119
|
+
expect(await listed({})).toEqual([second.note, first.note]);
|
|
120
|
+
expect(await listed({ at })).toEqual([first.note]);
|
|
121
|
+
expect(await listed({ at: 0 })).toEqual([]);
|
|
122
|
+
await host.as(owner, "memory.erase", { memory: "rin", note: first.note });
|
|
123
|
+
expect(await listed({ at })).toEqual([]);
|
|
124
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Projects are directories, and one directory is one project however it is spelled: a path with `..`, `.`,
|
|
2
|
+
// doubled or trailing slashes is read as the directory it names, and a relative path names none.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import type { Address } from "@niwa-labs/core";
|
|
7
|
+
|
|
8
|
+
import { closeAll, memoryHost, owner, rin } from "./fixtures/host.ts";
|
|
9
|
+
|
|
10
|
+
afterEach(closeAll);
|
|
11
|
+
|
|
12
|
+
type Listed = { notes: Array<{ note: string; project?: string }> };
|
|
13
|
+
type Context = { items: Array<{ note: string; project?: string }>; withheld: Array<{ note: string; reason: string }> };
|
|
14
|
+
|
|
15
|
+
const lead: Address = { kind: "agent", id: "lead" };
|
|
16
|
+
const AGENTS = [
|
|
17
|
+
{ name: "rin", harness: "h", prompt: "p" },
|
|
18
|
+
{ name: "bob", harness: "h", prompt: "p" },
|
|
19
|
+
{ name: "lead", harness: "h", prompt: "p", project: { cwd: "/work/billing/" } },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
test("a project spelled with `..` is the directory it names, not the one it starts in", async () => {
|
|
23
|
+
const host = await memoryHost({}, { agents: AGENTS });
|
|
24
|
+
const shop = await host.as<{ note: string }>(rin, "memory.note", { text: "Orders are checked nightly.", project: "/work/billing/../shop" });
|
|
25
|
+
const api = await host.as<{ note: string }>(rin, "memory.note", { text: "Invoices are made nightly.", project: "/work//billing/./api/" });
|
|
26
|
+
const told = await host.as<Context>(undefined, "memory.context", { memory: "rin", query: "nightly", purpose: "to plan", for: lead });
|
|
27
|
+
expect(told.items.map((item) => [item.note, item.project])).toEqual([[api.note, "/work/billing/api"]]);
|
|
28
|
+
const listed = await host.as<Listed>(rin, "memory.list", { project: "/work/shop/" });
|
|
29
|
+
expect(listed.notes.map((one) => [one.note, one.project])).toEqual([[shop.note, "/work/shop"]]);
|
|
30
|
+
expect((await host.as<Listed>(rin, "memory.list", { project: "/work/billing/api/../api" })).notes.map((one) => one.note)).toEqual([api.note]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("a relative directory is no project", async () => {
|
|
34
|
+
const host = await memoryHost();
|
|
35
|
+
await expect(host.as(rin, "memory.note", { text: "Orders are checked nightly.", project: "work/shop" })).rejects.toThrow(/absolute/);
|
|
36
|
+
await expect(host.as(owner, "memory.list", { memory: "rin", project: "../shop" })).rejects.toThrow(/absolute/);
|
|
37
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// An operation made again after its record was written but before its result was saved finds what it wrote and
|
|
2
|
+
// answers as the first run did, instead of failing against its own work.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { record } from "@niwa-labs/core";
|
|
7
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { connectAsOwner, eventually, gate, spawnHost, tempHome } from "@niwa-labs/testing";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { memory } from "../src/index.ts";
|
|
12
|
+
import { ConsolidationStarted, Consolidated, memoryConsolidate } from "../src/records.ts";
|
|
13
|
+
import type { Address } from "@niwa-labs/core";
|
|
14
|
+
|
|
15
|
+
import { closeAll, dataOf, memoryHost, rin, until } from "./fixtures/host.ts";
|
|
16
|
+
|
|
17
|
+
afterEach(closeAll);
|
|
18
|
+
|
|
19
|
+
const WHO = { memory: "rin" };
|
|
20
|
+
|
|
21
|
+
test("a page written again under the same operation answers the revision it wrote", async () => {
|
|
22
|
+
const host = await memoryHost();
|
|
23
|
+
const write = { ...WHO, page: "billing", title: "Billing", text: "How billing works.", revision: 0 };
|
|
24
|
+
expect(await host.again("memory.page.write", write, "op-page-1")).toEqual({ page: "billing", revision: 1 });
|
|
25
|
+
expect(await host.again("memory.page.write", write, "op-page-1")).toEqual({ page: "billing", revision: 1 });
|
|
26
|
+
expect(dataOf(await host.records(), "memory.page.written")).toHaveLength(1);
|
|
27
|
+
// Another operation is still refused for a revision that is out of date.
|
|
28
|
+
await expect(host.again("memory.page.write", write, "op-page-2")).rejects.toThrow(/revision 1, not 0/);
|
|
29
|
+
await host.again("memory.page.write", { ...write, revision: 1, text: "Revised" }, "advance");
|
|
30
|
+
await expect(host.again("memory.page.write", write, "op-page-2")).rejects.toThrow(/revision 1, not 0/);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("a consolidation made again answers the plan it recorded, not a new empty one", async () => {
|
|
34
|
+
const host = await memoryHost();
|
|
35
|
+
const first = (await host.as<{ note: string }>(rin, "memory.note", { text: "Releases go out on Fridays.", source: { kind: "session", session: "claude s-1", upto: "l-1" } })).note;
|
|
36
|
+
const second = (await host.as<{ note: string }>(rin, "memory.note", { text: "Releases go out on Thursdays.", source: { kind: "session", session: "claude s-2", upto: "l-1" } })).note;
|
|
37
|
+
await host.as(rin, "memory.correct", { note: second, text: "Releases go out on Fridays." });
|
|
38
|
+
const plan = await host.again<{ merged: unknown[] }>("memory.consolidate", WHO, "op-consolidate-1");
|
|
39
|
+
expect(plan.merged).toEqual([{ note: second, into: first }]);
|
|
40
|
+
expect(await host.again("memory.consolidate", WHO, "op-consolidate-1")).toEqual(plan);
|
|
41
|
+
expect(dataOf(await host.records(), "memory.consolidated")).toHaveLength(1);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("a correction made again answers the revision it made", async () => {
|
|
45
|
+
const host = await memoryHost();
|
|
46
|
+
const { note } = await host.as<{ note: string }>(rin, "memory.note", { text: "Releases go out on Fridays." });
|
|
47
|
+
const correct = { ...WHO, note, text: "Releases go out on Thursdays." };
|
|
48
|
+
expect(await host.again("memory.correct", correct, "op-correct-1")).toEqual({ note, revision: 2 });
|
|
49
|
+
expect(await host.again("memory.correct", correct, "op-correct-1")).toEqual({ note, revision: 2 });
|
|
50
|
+
expect(dataOf(await host.records(), "memory.note.corrected")).toHaveLength(1);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("a note written again under the same operation is the note it wrote, not a copy of it", async () => {
|
|
54
|
+
const host = await memoryHost();
|
|
55
|
+
const write = { ...WHO, text: "Releases go out on Fridays." };
|
|
56
|
+
const first = await host.again<{ note: string; corroborated: boolean }>("memory.note", write, "op-note-1");
|
|
57
|
+
expect(first.corroborated).toBe(false);
|
|
58
|
+
expect(await host.again("memory.note", write, "op-note-1")).toEqual(first);
|
|
59
|
+
// The same words under another operation are one more source of it.
|
|
60
|
+
expect(await host.again("memory.note", { ...write, source: { kind: "session", session: "claude s-1", upto: "l-1" } }, "op-note-2")).toEqual({ ...first, corroborated: true });
|
|
61
|
+
const corroborated = { ...WHO, text: "Releases go out on Fridays.", source: { kind: "session", session: "claude s-3", upto: "l-1" } };
|
|
62
|
+
const joined = await host.again("memory.note", corroborated, "op-note-3");
|
|
63
|
+
expect(await host.again("memory.note", corroborated, "op-note-3")).toEqual(joined);
|
|
64
|
+
expect(dataOf(await host.records(), "memory.note.corroborated")).toHaveLength(2);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("a context request cut after some of its questions is made again without writing them twice or colliding", async () => {
|
|
68
|
+
const host = await memoryHost();
|
|
69
|
+
const client: Address = { kind: "client", id: "reader" };
|
|
70
|
+
const notes: string[] = [];
|
|
71
|
+
for (const month of ["May", "June", "July"]) notes.push((await host.as<{ note: string }>(rin, "memory.note", { text: `He is on leave in ${month}.`, about: "owner" })).note);
|
|
72
|
+
const ask = { memory: "rin", query: "leave", purpose: "to plan", for: client, limit: 5 };
|
|
73
|
+
await expect(host.again("memory.context", ask, "op-context-1", 2)).rejects.toThrow(/cut/);
|
|
74
|
+
expect(dataOf(await host.records(), "memory.share.wanted")).toHaveLength(2);
|
|
75
|
+
const told = await host.again<{ withheld: Array<{ note: string; status: string }> }>("memory.context", ask, "op-context-1");
|
|
76
|
+
expect(told.withheld.map((one) => one.status)).toEqual(["asked", "asked", "asked"]);
|
|
77
|
+
const wanted = dataOf(await host.records(), "memory.share.wanted") as Array<{ note: string }>;
|
|
78
|
+
expect(wanted.map((one) => one.note).sort()).toEqual([...notes].sort());
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
for (const transport of ["host", "wire"] as const) {
|
|
82
|
+
test(`an unkeyed ${transport} consolidation is finished under its original operation before the next nightly run`, async () => {
|
|
83
|
+
const cut = Promise.withResolvers<void>();
|
|
84
|
+
const first = await memoryHost({}, { afterClaim: () => cut.promise });
|
|
85
|
+
const { note } = await first.as<{ note: string }>(rin, "memory.note", { text: "Releases go out on Fridays." });
|
|
86
|
+
await first.as({ kind: "owner" }, "memory.confirm", { ...WHO, note });
|
|
87
|
+
const client = transport === "wire" ? await connectAsOwner(first.home) : first.host;
|
|
88
|
+
const interrupted = client.call(memoryConsolidate, WHO);
|
|
89
|
+
try {
|
|
90
|
+
const claim = (await until(first, "memory.consolidation.started")).find(row => row.type === "memory.consolidation.started")!;
|
|
91
|
+
const { operation } = claim.data as { operation: string };
|
|
92
|
+
expect(String(claim.id)).toBe(`${operation}/emit/1`);
|
|
93
|
+
await expect(first.host.call(memoryConsolidate, WHO)).rejects.toThrow(/consolidation/);
|
|
94
|
+
expect(dataOf(await first.records(), "memory.consolidated")).toHaveLength(0);
|
|
95
|
+
cut.reject(new Error("cut after claim"));
|
|
96
|
+
await expect(interrupted).rejects.toThrow(/cut after claim/);
|
|
97
|
+
if (transport === "wire") await client.close();
|
|
98
|
+
const night = record("test.night", z.object({}));
|
|
99
|
+
const nightly = plugin("nightly", api => {
|
|
100
|
+
api.declare(night);
|
|
101
|
+
api.on(night, async (_, step) => { await step(memoryConsolidate, WHO, { key: "nightly" }); });
|
|
102
|
+
});
|
|
103
|
+
const resumed = await memoryHost({}, { home: await first.leave(), after: [nightly] });
|
|
104
|
+
await resumed.host.emit(night, {});
|
|
105
|
+
const rows = (await until(resumed, "memory.consolidated", 2)).filter(row => row.type === "memory.consolidated");
|
|
106
|
+
expect(String(rows[0]!.id)).toBe(`${operation}/emit/2`);
|
|
107
|
+
expect(rows[0]!.data).toMatchObject({ operation, lifted: [note] });
|
|
108
|
+
expect(rows[1]!.data).toMatchObject({ lifted: [] });
|
|
109
|
+
expect((rows[1]!.data as { operation: string }).operation).not.toBe(operation);
|
|
110
|
+
expect(await resumed.again("memory.consolidate", WHO, operation)).toMatchObject({ lifted: [note] });
|
|
111
|
+
expect(dataOf(await resumed.records(), "memory.consolidated")).toHaveLength(2);
|
|
112
|
+
} finally {
|
|
113
|
+
cut.resolve();
|
|
114
|
+
await interrupted.catch(() => {});
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
test("only one page written against revision zero becomes revision one", async () => {
|
|
120
|
+
const host = await memoryHost();
|
|
121
|
+
const write = { ...WHO, page: "billing", title: "Billing", revision: 0 };
|
|
122
|
+
const results = await Promise.allSettled(["one", "two"].map(text =>
|
|
123
|
+
host.again("memory.page.write", { ...write, text }, text)));
|
|
124
|
+
expect(results.filter(one => one.status === "fulfilled")).toHaveLength(1);
|
|
125
|
+
expect(dataOf(await host.records(), "memory.page.written")).toHaveLength(1);
|
|
126
|
+
await host.close();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("replacing memory leaves an active consolidation with the writer that claimed it", async () => {
|
|
130
|
+
const claimed = Promise.withResolvers<void>();
|
|
131
|
+
const release = Promise.withResolvers<void>();
|
|
132
|
+
const live = await memoryHost({}, {afterClaim: async () => {
|
|
133
|
+
claimed.resolve();
|
|
134
|
+
await release.promise;
|
|
135
|
+
}});
|
|
136
|
+
const first = live.host.call(memoryConsolidate, WHO);
|
|
137
|
+
try {
|
|
138
|
+
await claimed.promise;
|
|
139
|
+
live.host.use(memory());
|
|
140
|
+
await expect(live.host.call(memoryConsolidate, WHO)).rejects.toThrow(/consolidation/);
|
|
141
|
+
expect(dataOf(await live.records(), "memory.consolidated")).toHaveLength(0);
|
|
142
|
+
release.resolve();
|
|
143
|
+
await first;
|
|
144
|
+
expect(dataOf(await live.records(), "memory.consolidated")).toHaveLength(1);
|
|
145
|
+
} finally { release.resolve(); await first.catch(() => {}); }
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("a new host process finishes an unkeyed consolidation interrupted at its claim", async () => {
|
|
149
|
+
const home = await tempHome();
|
|
150
|
+
const script = fileURLToPath(new URL("./fixtures/host.ts", import.meta.url));
|
|
151
|
+
let host = spawnHost(script, home.path);
|
|
152
|
+
let client;
|
|
153
|
+
try {
|
|
154
|
+
await host.ready();
|
|
155
|
+
client = await connectAsOwner(home.path);
|
|
156
|
+
const first = client;
|
|
157
|
+
const pending = first.call(memoryConsolidate, WHO).catch(() => undefined);
|
|
158
|
+
await eventually(async () => (await first.records.read({types: [ConsolidationStarted]})).length === 1);
|
|
159
|
+
const [claim] = await first.records.read({types: [ConsolidationStarted]});
|
|
160
|
+
const {operation} = ConsolidationStarted.schema.parse(claim!.data);
|
|
161
|
+
await host.kill();
|
|
162
|
+
await first.close();
|
|
163
|
+
await pending;
|
|
164
|
+
gate(home.path, "finish-consolidation").open();
|
|
165
|
+
host = spawnHost(script, home.path);
|
|
166
|
+
await host.ready();
|
|
167
|
+
client = await connectAsOwner(home.path);
|
|
168
|
+
await client.call(memoryConsolidate, WHO);
|
|
169
|
+
const receipts = await client.records.read({types: [Consolidated]});
|
|
170
|
+
expect(receipts).toHaveLength(2);
|
|
171
|
+
expect(String(receipts[0]!.id)).toBe(`${operation}/emit/2`);
|
|
172
|
+
expect(Consolidated.schema.parse(receipts[1]!.data).operation).not.toBe(operation);
|
|
173
|
+
} finally { await client?.close(); await host.kill(); await home.cleanup(); }
|
|
174
|
+
}, 20_000);
|