@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.
@@ -0,0 +1,88 @@
1
+ // Pages and search: a page is written whole under a name, refused when it changed since it was read; a
2
+ // search ranks the notes that hold and the pages by words, on the host, and never finds what was erased.
3
+
4
+ import { afterEach, expect, test } from "bun:test";
5
+
6
+ import { rank, words } from "../src/search.ts";
7
+ import { bob, closeAll, memoryHost, owner, rin } from "./fixtures/host.ts";
8
+
9
+ afterEach(closeAll);
10
+
11
+ type Hit = { kind: string; id: string; title?: string; text: string; project?: string; score: number; seq: number };
12
+ type Page = { page: string; title: string; text: string; revision: number; seq: number };
13
+
14
+ test("words are letters and digits of any script, and a query word finds the longer words it starts", () => {
15
+ expect(words("Деплой в 9:30, v2 — CI/CD!")).toEqual(["деплой", "30", "v2", "ci", "cd"]);
16
+ const texts = [
17
+ { text: "The deployment runs at night.", value: "a" },
18
+ { text: "Tea, not coffee.", value: "b" },
19
+ { text: "Deploy after the deploy check, deploy again.", value: "c" },
20
+ ];
21
+ expect(rank("deploy", texts, 10).map((hit) => hit.value)).toEqual(["c", "a"]);
22
+ expect(rank("de", texts, 10)).toEqual([]);
23
+ expect(rank("tea coffee", texts, 1).map((hit) => hit.value)).toEqual(["b"]);
24
+ });
25
+
26
+ test("a page is written whole, and a write over a change made since it was read is refused", async () => {
27
+ const host = await memoryHost();
28
+ expect(await host.as(rin, "memory.page.write", { page: "projects/billing", title: "Billing", text: "Invoices are made nightly.", project: "/work/billing", revision: 0 })).toEqual({
29
+ page: "projects/billing",
30
+ revision: 1,
31
+ });
32
+ await expect(host.as(rin, "memory.page.write", { page: "projects/billing", title: "Billing", text: "Again.", revision: 0 })).rejects.toThrow(/revision 1, not 0/);
33
+ const first = await host.as<Page>(rin, "memory.page.get", { page: "projects/billing" });
34
+ await host.as(rin, "memory.page.write", { page: "projects/billing", title: "Billing", text: "Invoices are made hourly.", revision: 1 });
35
+ expect(await host.as<Page>(rin, "memory.page.get", { page: "projects/billing" })).toMatchObject({ revision: 2, text: "Invoices are made hourly." });
36
+ expect(await host.as<Page>(rin, "memory.page.get", { page: "projects/billing", at: first.seq })).toMatchObject({ revision: 1, text: "Invoices are made nightly." });
37
+ await expect(host.as(bob, "memory.page.get", { memory: "rin", page: "projects/billing" })).rejects.toThrow(/only its own memory/);
38
+ await expect(host.as(rin, "memory.page.get", { page: "nothing" })).rejects.toThrow(/no page/);
39
+ await expect(host.as(rin, "memory.page.write", { page: "big", title: "Big", text: "x".repeat(16 * 1024 + 1) })).rejects.toThrow();
40
+ });
41
+
42
+ type Listed = { memory: string; pages: Array<{ page: string; title: string; revision: number; project?: string; text?: string }> };
43
+
44
+ test("the pages of a memory are listed by name at their latest revision, without their text", async () => {
45
+ const host = await memoryHost();
46
+ await host.as(rin, "memory.page.write", { page: "projects/billing", title: "Billing", text: "Invoices are made nightly.", project: "/work/billing" });
47
+ await host.as(rin, "memory.page.write", { page: "projects/billing", title: "Billing, again", text: "Invoices are made hourly.", project: "/work/billing" });
48
+ await host.as(rin, "memory.page.write", { page: "about", title: "About", text: "What this memory is for." });
49
+ await host.as(rin, "memory.page.write", { page: "projects/payroll", title: "Payroll", text: "Runs monthly.", project: "/work/payroll" });
50
+ await host.as(bob, "memory.page.write", { page: "bobs", title: "Bob's page", text: "Not rin's." });
51
+
52
+ const own = await host.as<Listed>(rin, "memory.page.list", {});
53
+ expect(own.memory).toBe("rin");
54
+ expect(own.pages.map((one) => [one.page, one.title, one.revision])).toEqual([
55
+ ["about", "About", 1],
56
+ ["projects/billing", "Billing, again", 2],
57
+ ["projects/payroll", "Payroll", 1],
58
+ ]);
59
+ expect(own.pages.some((one) => "text" in one)).toBe(false);
60
+
61
+ // A project is asked for with the pages of no project: the general ones.
62
+ expect((await host.as<Listed>(rin, "memory.page.list", { project: "/work/billing" })).pages.map((one) => one.page)).toEqual(["about", "projects/billing"]);
63
+ expect((await host.as<Listed>(owner, "memory.page.list", { memory: "bob" })).pages.map((one) => one.page)).toEqual(["bobs"]);
64
+ await expect(host.as(bob, "memory.page.list", { memory: "rin" })).rejects.toThrow(/only its own memory/);
65
+ await expect(host.as(owner, "memory.page.list", {})).rejects.toThrow(/name the memory/);
66
+ });
67
+
68
+ test("a search finds the notes that hold and the pages, best first, and nothing that was taken back or erased", async () => {
69
+ const host = await memoryHost();
70
+ const release = await host.as<{ note: string }>(rin, "memory.note", { text: "Releases go out on Fridays after the release check." });
71
+ const taken = await host.as<{ note: string }>(rin, "memory.note", { text: "Releases go out on Mondays." });
72
+ const erased = await host.as<{ note: string }>(owner, "memory.note", { memory: "rin", text: "My release password is hunter2.", about: "owner", source: { kind: "owner" } });
73
+ await host.as(rin, "memory.note", { text: "Billing invoices are made nightly.", project: "/work/billing" });
74
+ await host.as(rin, "memory.page.write", { page: "release", title: "How a release is made", text: "Tag, build, then publish." });
75
+ await host.as(rin, "memory.retract", { note: taken.note });
76
+ await host.as(rin, "memory.erase", { note: erased.note });
77
+
78
+ const { hits } = await host.as<{ hits: Hit[] }>(rin, "memory.search", { query: "release" });
79
+ expect(hits.map((hit) => hit.id)).toEqual([release.note, "release"]);
80
+ expect(hits[1]).toMatchObject({ kind: "page", title: "How a release is made", text: "Tag, build, then publish." });
81
+ expect((await host.as<{ hits: Hit[] }>(rin, "memory.search", { query: "hunter2 password" })).hits).toEqual([]);
82
+ expect((await host.as<{ hits: Hit[] }>(rin, "memory.search", { query: "invoices" })).hits).toHaveLength(1);
83
+ expect((await host.as<{ hits: Hit[] }>(rin, "memory.search", { query: "invoices", project: "/work/other" })).hits).toEqual([]);
84
+ // As it stood before the retraction, the Monday note still held.
85
+ const before = (await host.as<{ written: number }>(rin, "memory.get", { note: taken.note })).written;
86
+ expect((await host.as<{ hits: Hit[] }>(rin, "memory.search", { query: "mondays", at: before })).hits.map((hit) => hit.id)).toEqual([taken.note]);
87
+ await expect(host.as(bob, "memory.search", { memory: "rin", query: "release" })).rejects.toThrow(/only its own memory/);
88
+ });
@@ -0,0 +1,84 @@
1
+ // The top layer: the notes a memory shows first, lifted by hand or by consolidation, for a project and
2
+ // within a size; the briefing of an agent carries it for the project the agent works on.
3
+
4
+ import { afterEach, expect, test } from "bun:test";
5
+
6
+ import { AgentAttached } from "@niwa-labs/agent/records";
7
+
8
+ import { agentPromptSections, bob, closeAll, memoryHost, owner, rin } from "./fixtures/host.ts";
9
+
10
+ afterEach(closeAll);
11
+
12
+ type Top = { memory: string; text: string; notes: string[]; left: number };
13
+
14
+ async function noted(host: Awaited<ReturnType<typeof memoryHost>>, input: Record<string, unknown>, who = rin): Promise<string> {
15
+ return (await host.as<{ note: string }>(who, "memory.note", input)).note;
16
+ }
17
+
18
+ test("the top layer of a project holds the lifted notes about the owner, for the project and in general", async () => {
19
+ const host = await memoryHost();
20
+ const tea = await noted(host, { memory: "rin", text: "I drink tea, not coffee.", about: "owner", source: { kind: "owner" } }, owner);
21
+ const nightly = await noted(host, { text: "Invoices are made nightly.", project: "/work/billing" });
22
+ const general = await noted(host, { text: "Reviews come before merges." });
23
+ const elsewhere = await noted(host, { text: "The site is built with Astro.", project: "/work/site" });
24
+ const unlifted = await noted(host, { text: "Not shown first." });
25
+ for (const note of [nightly, general, elsewhere]) expect(await host.as(rin, "memory.lift", { note })).toEqual({ note, top: true });
26
+ await host.as(owner, "memory.lift", { memory: "rin", note: tea });
27
+
28
+ const top = await host.as<Top>(rin, "memory.top", { project: "/work/billing" });
29
+ expect(top.notes).toEqual([tea, nightly, general]);
30
+ expect(top.text).toBe(
31
+ ["About the owner:", `- I drink tea, not coffee. (${tea})`, "For /work/billing:", `- Invoices are made nightly. (${nightly})`, "In general:", `- Reviews come before merges. (${general})`].join("\n"),
32
+ );
33
+ expect(top.notes).not.toContain(unlifted);
34
+ // Without a project, the top layer is the owner and what holds in general.
35
+ expect((await host.as<Top>(rin, "memory.top", {})).notes).toEqual([tea, general]);
36
+
37
+ await host.as(rin, "memory.drop", { note: general });
38
+ await host.as(rin, "memory.retract", { note: nightly });
39
+ expect((await host.as<Top>(rin, "memory.top", { project: "/work/billing" })).notes).toEqual([tea]);
40
+ await expect(host.as(bob, "memory.lift", { memory: "rin", note: tea })).rejects.toThrow(/only its own memory/);
41
+ });
42
+
43
+ test("the top layer stays within its size, and says how many lifted notes it left out", async () => {
44
+ const host = await memoryHost({ limits: { top: 120 } });
45
+ const notes: string[] = [];
46
+ for (const text of ["First note that is lifted.", "Second note that is lifted.", "Third note that is lifted.", "Fourth note that is lifted."]) {
47
+ const note = await noted(host, { text });
48
+ await host.as(rin, "memory.lift", { note });
49
+ notes.push(note);
50
+ }
51
+ const top = await host.as<Top>(rin, "memory.top", {});
52
+ expect(top.text.length).toBeLessThanOrEqual(120);
53
+ expect(top.notes.length + top.left).toBe(4);
54
+ expect(top.left).toBeGreaterThan(0);
55
+ // The newest are shown first: they are what the agent knows best now.
56
+ expect(top.notes[0]).toBe(notes[3]!);
57
+ });
58
+
59
+ test("an agent's briefing carries the top layer of its memory for the project it works on", async () => {
60
+ const host = await memoryHost({}, { agents: [{ name: "rin", harness: "h", prompt: "p" }, { name: "lead", harness: "h", prompt: "p", project: { cwd: "/work/billing" } }] });
61
+ const billing = await noted(host, { memory: "lead", text: "Invoices are made nightly.", project: "/work/billing" }, owner);
62
+ const site = await noted(host, { memory: "rin", text: "The site is built with Astro.", project: "/work/site" }, owner);
63
+ await host.as(owner, "memory.lift", { memory: "lead", note: billing });
64
+ await host.as(owner, "memory.lift", { memory: "rin", note: site });
65
+
66
+ const lead = await host.as<{ text: string }>(owner, "agent.briefing", { agent: "lead" });
67
+ expect(lead.text).toBe(`What it remembers:\nFor /work/billing:\n- Invoices are made nightly. (${billing})`);
68
+ // Rin has no project of her own: the session attached to her last says where she works.
69
+ expect((await host.as<{ text: string }>(owner, "agent.briefing", { agent: "rin" })).text).toBe("");
70
+ await host.host.emit(AgentAttached, { agent: "rin", session: "h n-1", cwd: "/work/site" });
71
+ expect((await host.as<{ text: string }>(owner, "agent.briefing", { agent: "rin" })).text).toContain("The site is built with Astro.");
72
+ // Another agent asking for Rin's briefing does not get her memory.
73
+ expect((await host.as<{ text: string }>(bob, "agent.briefing", { agent: "rin" })).text).toBe("");
74
+ expect((await host.as<{ text: string }>(rin, "agent.briefing", { agent: "rin" })).text).toContain("Astro");
75
+ });
76
+
77
+ test("the top layer is also a section of the prompt of the agent's sessions", async () => {
78
+ const host = await memoryHost({}, { agents: [{ name: "lead", harness: "h", prompt: "p", project: { cwd: "/work/billing" } }] });
79
+ const billing = await noted(host, { memory: "lead", text: "Invoices are made nightly.", project: "/work/billing" }, owner);
80
+ const prompt = () => host.as<{ sections: Array<{ id: string; text: string }> }>(owner, agentPromptSections.name, { agent: "lead" });
81
+ expect((await prompt()).sections).toEqual([]);
82
+ await host.as(owner, "memory.lift", { memory: "lead", note: billing });
83
+ expect((await prompt()).sections).toEqual([{ id: "memory:top", text: `For /work/billing:\n- Invoices are made nightly. (${billing})` }]);
84
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "lib": [
5
+ "ESNext",
6
+ "DOM",
7
+ "DOM.Iterable",
8
+ "DOM.AsyncIterable"
9
+ ],
10
+ "jsx": "react-jsx"
11
+ },
12
+ "include": [
13
+ "src"
14
+ ]
15
+ }