notesoup 0.0.0-stage → 0.39.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 +91 -2
- package/dist/src/boot/render.d.ts +16 -0
- package/dist/src/boot/render.js +95 -0
- package/dist/src/boot/snapshot.d.ts +32 -0
- package/dist/src/boot/snapshot.js +36 -0
- package/dist/src/boot/text.d.ts +21 -0
- package/dist/src/boot/text.js +52 -0
- package/dist/src/budget/constants.d.ts +9 -0
- package/dist/src/budget/constants.js +9 -0
- package/dist/src/budget/policy.d.ts +19 -0
- package/dist/src/budget/policy.js +36 -0
- package/dist/src/budget/text.d.ts +8 -0
- package/dist/src/budget/text.js +10 -0
- package/dist/src/claude/helper.d.ts +15 -0
- package/dist/src/claude/helper.js +87 -0
- package/dist/src/dream/doctor.d.ts +2 -0
- package/dist/src/dream/doctor.js +160 -0
- package/dist/src/dream/gates.d.ts +10 -0
- package/dist/src/dream/gates.js +26 -0
- package/dist/src/dream/git.d.ts +21 -0
- package/dist/src/dream/git.js +71 -0
- package/dist/src/dream/jail.d.ts +11 -0
- package/dist/src/dream/jail.js +71 -0
- package/dist/src/dream/lock.d.ts +31 -0
- package/dist/src/dream/lock.js +99 -0
- package/dist/src/dream/playbook.d.ts +1 -0
- package/dist/src/dream/playbook.js +2 -0
- package/dist/src/dream/report.d.ts +7 -0
- package/dist/src/dream/report.js +43 -0
- package/dist/src/dream/result.d.ts +9 -0
- package/dist/src/dream/result.js +1 -0
- package/dist/src/dream/settings.d.ts +10 -0
- package/dist/src/dream/settings.js +14 -0
- package/dist/src/history/history.d.ts +151 -0
- package/dist/src/history/history.js +195 -0
- package/dist/src/history/query.d.ts +75 -0
- package/dist/src/history/query.js +143 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/notes/address.d.ts +31 -0
- package/dist/src/notes/address.js +123 -0
- package/dist/src/notes/constants.d.ts +3 -0
- package/dist/src/notes/constants.js +3 -0
- package/dist/src/notes/errors.d.ts +11 -0
- package/dist/src/notes/errors.js +13 -0
- package/dist/src/notes/frontmatter.d.ts +44 -0
- package/dist/src/notes/frontmatter.js +145 -0
- package/dist/src/notes/fs-error.d.ts +2 -0
- package/dist/src/notes/fs-error.js +5 -0
- package/dist/src/notes/identity.d.ts +10 -0
- package/dist/src/notes/identity.js +33 -0
- package/dist/src/notes/index.d.ts +5 -0
- package/dist/src/notes/index.js +3 -0
- package/dist/src/notes/paths.d.ts +21 -0
- package/dist/src/notes/paths.js +113 -0
- package/dist/src/notes/store.d.ts +104 -0
- package/dist/src/notes/store.js +478 -0
- package/dist/src/pi/boot.d.ts +24 -0
- package/dist/src/pi/boot.js +56 -0
- package/dist/src/pi/budget.d.ts +13 -0
- package/dist/src/pi/budget.js +178 -0
- package/dist/src/pi/dream/cli.d.ts +10 -0
- package/dist/src/pi/dream/cli.js +140 -0
- package/dist/src/pi/dream/runner.d.ts +27 -0
- package/dist/src/pi/dream/runner.js +90 -0
- package/dist/src/pi/dream/settings.d.ts +7 -0
- package/dist/src/pi/dream/settings.js +17 -0
- package/dist/src/pi/entries.d.ts +7 -0
- package/dist/src/pi/entries.js +7 -0
- package/dist/src/pi/estimate.d.ts +16 -0
- package/dist/src/pi/estimate.js +99 -0
- package/dist/src/pi/extension.d.ts +10 -0
- package/dist/src/pi/extension.js +24 -0
- package/dist/src/pi/history-tools.d.ts +3 -0
- package/dist/src/pi/history-tools.js +10 -0
- package/dist/src/pi/history.d.ts +4 -0
- package/dist/src/pi/history.js +150 -0
- package/dist/src/pi/notes/adapter.d.ts +10 -0
- package/dist/src/pi/notes/adapter.js +27 -0
- package/dist/src/pi/notes/tools.d.ts +3 -0
- package/dist/src/pi/notes/tools.js +15 -0
- package/dist/src/pi/reset/artifacts.d.ts +24 -0
- package/dist/src/pi/reset/artifacts.js +84 -0
- package/dist/src/pi/reset/committed.d.ts +7 -0
- package/dist/src/pi/reset/committed.js +28 -0
- package/dist/src/pi/reset/lifecycle.d.ts +101 -0
- package/dist/src/pi/reset/lifecycle.js +277 -0
- package/dist/src/pi/reset/text.d.ts +5 -0
- package/dist/src/pi/reset/text.js +6 -0
- package/dist/src/pi/runtime.d.ts +3 -0
- package/dist/src/pi/runtime.js +206 -0
- package/dist/src/pi/session-reader.d.ts +5 -0
- package/dist/src/pi/session-reader.js +1 -0
- package/dist/src/pi/thresholds.d.ts +17 -0
- package/dist/src/pi/thresholds.js +33 -0
- package/dist/src/pi/tool-names.d.ts +11 -0
- package/dist/src/pi/tool-names.js +11 -0
- package/dist/src/pi/tool-result.d.ts +15 -0
- package/dist/src/pi/tool-result.js +29 -0
- package/dist/src/pi/window.d.ts +41 -0
- package/dist/src/pi/window.js +127 -0
- package/dist/src/settings.d.ts +9 -0
- package/dist/src/settings.js +18 -0
- package/dist/src/text-match.d.ts +5 -0
- package/dist/src/text-match.js +16 -0
- package/dist/src/tools/history.d.ts +67 -0
- package/dist/src/tools/history.js +251 -0
- package/dist/src/tools/notes.d.ts +282 -0
- package/dist/src/tools/notes.js +378 -0
- package/dist/src/tools/output.d.ts +23 -0
- package/dist/src/tools/output.js +39 -0
- package/dist/src/tools/result.d.ts +79 -0
- package/dist/src/tools/result.js +130 -0
- package/dist/src/tools/schema.d.ts +18 -0
- package/dist/src/tools/schema.js +31 -0
- package/dist/test/agent-loop.test.d.ts +1 -0
- package/dist/test/agent-loop.test.js +1497 -0
- package/dist/test/boot.integration.test.d.ts +1 -0
- package/dist/test/boot.integration.test.js +294 -0
- package/dist/test/budget-settings.integration.test.d.ts +1 -0
- package/dist/test/budget-settings.integration.test.js +144 -0
- package/dist/test/claude-mod.test.d.ts +1 -0
- package/dist/test/claude-mod.test.js +404 -0
- package/dist/test/dependencies.test.d.ts +1 -0
- package/dist/test/dependencies.test.js +101 -0
- package/dist/test/doctor.test.d.ts +1 -0
- package/dist/test/doctor.test.js +37 -0
- package/dist/test/dream-skill.test.d.ts +1 -0
- package/dist/test/dream-skill.test.js +72 -0
- package/dist/test/dream.test.d.ts +1 -0
- package/dist/test/dream.test.js +136 -0
- package/dist/test/helpers/dependency-graph.d.ts +5 -0
- package/dist/test/helpers/dependency-graph.js +73 -0
- package/dist/test/helpers/extension-test-environment.d.ts +1 -0
- package/dist/test/helpers/extension-test-environment.js +9 -0
- package/dist/test/helpers/extension.d.ts +119 -0
- package/dist/test/helpers/extension.js +368 -0
- package/dist/test/helpers/notes.d.ts +6 -0
- package/dist/test/helpers/notes.js +13 -0
- package/dist/test/history.integration.test.d.ts +1 -0
- package/dist/test/history.integration.test.js +556 -0
- package/dist/test/host-neutral.test.d.ts +1 -0
- package/dist/test/host-neutral.test.js +300 -0
- package/dist/test/notes-library.test.d.ts +1 -0
- package/dist/test/notes-library.test.js +214 -0
- package/dist/test/notes.integration.test.d.ts +1 -0
- package/dist/test/notes.integration.test.js +497 -0
- package/dist/test/notes.test.d.ts +1 -0
- package/dist/test/notes.test.js +528 -0
- package/dist/test/reset-lifecycle.test.d.ts +1 -0
- package/dist/test/reset-lifecycle.test.js +605 -0
- package/docs/architecture.md +63 -0
- package/docs/claude-code-mod.md +9 -0
- package/docs/notes-library.md +74 -0
- package/docs/reset-lifecycle.md +75 -0
- package/docs/sdk-integration.md +39 -0
- package/docs/skills.md +30 -0
- package/docs/tool-results.md +88 -0
- package/mods/notesoup/.claude-plugin/plugin.json +8 -0
- package/mods/notesoup/dist/claude/helper.js +9324 -0
- package/mods/notesoup/hooks/hooks.json +4 -0
- package/mods/notesoup/hooks/register.ts +241 -0
- package/mods/notesoup/playbook.md +34 -0
- package/mods/notesoup/skills/dream/SKILL.md +12 -0
- package/mods/pi-context/dist/claude/helper.js +9324 -0
- package/package.json +89 -5
- package/playbook.md +34 -0
- package/skills/dream/SKILL.md +12 -0
- package/skills/memory/SKILL.md +59 -0
- package/src/boot/render.ts +115 -0
- package/src/boot/snapshot.ts +46 -0
- package/src/boot/text.ts +66 -0
- package/src/budget/constants.ts +9 -0
- package/src/budget/policy.ts +36 -0
- package/src/budget/text.ts +11 -0
- package/src/claude/helper.ts +94 -0
- package/src/dream/doctor.ts +117 -0
- package/src/dream/gates.ts +26 -0
- package/src/dream/git.ts +73 -0
- package/src/dream/jail.ts +62 -0
- package/src/dream/lock.ts +82 -0
- package/src/dream/playbook.ts +3 -0
- package/src/dream/report.ts +36 -0
- package/src/dream/result.ts +2 -0
- package/src/dream/settings.ts +16 -0
- package/src/history/history.ts +270 -0
- package/src/history/query.ts +147 -0
- package/src/index.ts +1 -0
- package/src/notes/address.ts +110 -0
- package/src/notes/constants.ts +3 -0
- package/src/notes/errors.ts +15 -0
- package/src/notes/frontmatter.ts +166 -0
- package/src/notes/fs-error.ts +5 -0
- package/src/notes/identity.ts +40 -0
- package/src/notes/index.ts +24 -0
- package/src/notes/paths.ts +109 -0
- package/src/notes/store.ts +497 -0
- package/src/pi/boot.ts +88 -0
- package/src/pi/budget.ts +178 -0
- package/src/pi/dream/cli.ts +92 -0
- package/src/pi/dream/runner.ts +91 -0
- package/src/pi/dream/settings.ts +17 -0
- package/src/pi/entries.ts +7 -0
- package/src/pi/estimate.ts +116 -0
- package/src/pi/extension.ts +27 -0
- package/src/pi/history-tools.ts +12 -0
- package/src/pi/history.ts +162 -0
- package/src/pi/notes/adapter.ts +33 -0
- package/src/pi/notes/tools.ts +18 -0
- package/src/pi/reset/artifacts.ts +100 -0
- package/src/pi/reset/committed.ts +26 -0
- package/src/pi/reset/lifecycle.ts +342 -0
- package/src/pi/reset/text.ts +10 -0
- package/src/pi/runtime.ts +200 -0
- package/src/pi/session-reader.ts +6 -0
- package/src/pi/thresholds.ts +50 -0
- package/src/pi/tool-names.ts +13 -0
- package/src/pi/tool-result.ts +41 -0
- package/src/pi/window.ts +133 -0
- package/src/settings.ts +22 -0
- package/src/text-match.ts +14 -0
- package/src/tools/history.ts +273 -0
- package/src/tools/notes.ts +394 -0
- package/src/tools/output.ts +41 -0
- package/src/tools/result.ts +169 -0
- package/src/tools/schema.ts +31 -0
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OWNER: notesoup (adopted).
|
|
3
|
+
* STATUS: tracked acceptance spec for the real-file notes store and its five tools.
|
|
4
|
+
* CLAIM: notes live as markdown files under $PI_NOTES_HOME with harness-owned frontmatter;
|
|
5
|
+
* the five tools (notes_write/update/read/list/search) are the only note surface, and the
|
|
6
|
+
* boot index reads the physical store across scopes.
|
|
7
|
+
* HERMETIC: every test points PI_NOTES_HOME at its own temp root; no real ~/.agents is touched.
|
|
8
|
+
*/
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import test from "node:test";
|
|
14
|
+
import { Check } from "typebox/value";
|
|
15
|
+
import { parseNote } from "../src/notes/frontmatter.js";
|
|
16
|
+
import { projectKey } from "../src/notes/paths.js";
|
|
17
|
+
import { listNotes, physicalPath, scopeDir } from "./helpers/notes.js";
|
|
18
|
+
import { call, context, explicitBoot, makeExtension, manager, resultData, resultError, resultRead } from "./helpers/extension.js";
|
|
19
|
+
import { installExtensionTestHooks } from "./helpers/extension-test-environment.js";
|
|
20
|
+
const testEnvironment = installExtensionTestHooks("notesoup-notes");
|
|
21
|
+
function freshRoot() {
|
|
22
|
+
return testEnvironment.newNotesRoot();
|
|
23
|
+
}
|
|
24
|
+
function assertReceiptIdentity(value, address, scope, ctx) {
|
|
25
|
+
assert.equal(value.address, address);
|
|
26
|
+
assert.equal(value.project_key, scope === "project" ? projectKey(ctx.cwd) : undefined);
|
|
27
|
+
}
|
|
28
|
+
test("exactly the five notes tools are registered; the legacy five are gone", () => {
|
|
29
|
+
const captured = makeExtension(manager());
|
|
30
|
+
for (const name of ["notes_write", "notes_update", "notes_read", "notes_list", "notes_search"]) {
|
|
31
|
+
assert.ok(captured.tools.get(name), `${name} is registered`);
|
|
32
|
+
}
|
|
33
|
+
for (const legacy of ["notes_write_file", "notes_append_to_file", "notes_read_file", "notes_search_contents", "notes_list_files"]) {
|
|
34
|
+
assert.equal(captured.tools.get(legacy), undefined, `${legacy} is unregistered`);
|
|
35
|
+
}
|
|
36
|
+
assert.equal(captured.tools.get("notes_write")?.executionMode, "sequential");
|
|
37
|
+
assert.equal(captured.tools.get("notes_update")?.executionMode, "sequential");
|
|
38
|
+
assert.equal(captured.tools.get("notes_read")?.executionMode, undefined);
|
|
39
|
+
});
|
|
40
|
+
test("notes and history tools are grouped under their own namespaces", () => {
|
|
41
|
+
const captured = makeExtension(manager());
|
|
42
|
+
for (const name of ["notes_write", "notes_update", "notes_read", "notes_list", "notes_search"]) {
|
|
43
|
+
assert.equal(captured.tools.get(name)?.namespace?.name, "notes", `${name} is in notes`);
|
|
44
|
+
}
|
|
45
|
+
for (const name of ["history_windows", "history_list", "history_read", "history_search"]) {
|
|
46
|
+
assert.equal(captured.tools.get(name)?.namespace?.name, "history", `${name} is in history`);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
test("every notes tool declares a result schema that accepts its own structured payload", async () => {
|
|
50
|
+
const session = manager();
|
|
51
|
+
const captured = makeExtension(session);
|
|
52
|
+
const ctx = context(session);
|
|
53
|
+
await call(captured, "notes_write", { address: "schema.md", content: "needle body" }, ctx);
|
|
54
|
+
const delivered = [
|
|
55
|
+
["notes_write", await call(captured, "notes_write", { address: "schema.md", content: "needle body again" }, ctx)],
|
|
56
|
+
["notes_update", await call(captured, "notes_update", { address: "schema.md", edits: [{ oldText: "again", newText: "once" }] }, ctx)],
|
|
57
|
+
["notes_read", await call(captured, "notes_read", { address: "schema.md" }, ctx)],
|
|
58
|
+
["notes_list", await call(captured, "notes_list", {}, ctx)],
|
|
59
|
+
["notes_search", await call(captured, "notes_search", { query: "needle" }, ctx)],
|
|
60
|
+
["notes_read", await call(captured, "notes_read", { address: "absent.md" }, ctx)],
|
|
61
|
+
];
|
|
62
|
+
for (const [name, result] of delivered) {
|
|
63
|
+
const schema = captured.tools.get(name).outputSchema;
|
|
64
|
+
assert.ok(schema, `${name} registers an outputSchema`);
|
|
65
|
+
assert.equal(Check(schema, result.structuredContent), true, `${name} structured payload matches its declared schema`);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
test("note schemas drop status/stale and expose crumpled plus wastebasket", () => {
|
|
69
|
+
const captured = makeExtension(manager());
|
|
70
|
+
const schema = (name) => captured.tools.get(name).parameters;
|
|
71
|
+
const write = schema("notes_write");
|
|
72
|
+
assert.equal(Check(write, { address: "a.md", content: "x" }), true);
|
|
73
|
+
assert.equal(Check(write, { address: "a.md", content: "x", stale: true }), false, "notes_write no longer accepts stale");
|
|
74
|
+
assert.equal(Check(write, { address: "a.md", content: "x", crumpled: true }), false, "notes_write does not accept crumpled");
|
|
75
|
+
const edit = schema("notes_update");
|
|
76
|
+
assert.equal(Check(edit, { address: "a.md", crumpled: true }), true);
|
|
77
|
+
assert.equal(Check(edit, { address: "a.md", stale: true }), false, "notes_update no longer accepts stale");
|
|
78
|
+
assert.equal(Check(edit, { address: "a.md", status: "archived" }), false, "status is gone");
|
|
79
|
+
for (const name of ["notes_list", "notes_search"]) {
|
|
80
|
+
const base = name === "notes_search" ? { query: "x" } : {};
|
|
81
|
+
assert.equal(Check(schema(name), { ...base, wastebasket: true }), true, `${name} accepts wastebasket`);
|
|
82
|
+
assert.equal(Check(schema(name), { ...base, status: "active" }), false, `${name} rejects status`);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
test("write lands a real markdown file with harness frontmatter and a pure body", async () => {
|
|
86
|
+
const root = freshRoot();
|
|
87
|
+
const session = manager();
|
|
88
|
+
const captured = makeExtension(session);
|
|
89
|
+
const ctx = context(session);
|
|
90
|
+
const sessionId = session.getSessionId();
|
|
91
|
+
const result = resultData(await call(captured, "notes_write", { address: "a/b.md", content: "hello" }, ctx));
|
|
92
|
+
assert.deepEqual(Object.keys(result).sort(), ["address", "outcome"]);
|
|
93
|
+
const file = physicalPath("session", "a/b.md", ctx);
|
|
94
|
+
assert.equal(file, join(root, "pi", "session", sessionId, "a", "b.md"));
|
|
95
|
+
assert.ok(existsSync(file), "the note is a real file under the session scope dir");
|
|
96
|
+
const raw = readFileSync(file, "utf8");
|
|
97
|
+
assert.match(raw, /^---\n/, "the file opens with frontmatter");
|
|
98
|
+
assert.match(raw, /\n---\n\nhello$/, "frontmatter is followed by a blank line and the exact body");
|
|
99
|
+
for (const [key, value] of [["origin", "self"], ["accessCount", "0"]]) {
|
|
100
|
+
assert.match(raw, new RegExp(`^${key}: ${value}$`, "m"), `frontmatter carries ${key}=${value}`);
|
|
101
|
+
}
|
|
102
|
+
assert.equal(/^scope:/m.test(raw), false, "scope is derived from the file home, never persisted");
|
|
103
|
+
assert.equal(/^status:/m.test(raw), false, "status is gone from persisted notes");
|
|
104
|
+
assert.equal(/^stale:/m.test(raw), false, "the boolean stale flag is gone from persisted notes");
|
|
105
|
+
assert.equal(/^crumpledAt:/m.test(raw), false, "a fresh note is uncrumpled");
|
|
106
|
+
for (const key of ["createdAt", "updatedAt", "lastAccessed"]) {
|
|
107
|
+
assert.match(raw, new RegExp(`^${key}: \\d{4}-\\d{2}-\\d{2}T`, "m"), `frontmatter renders ${key} via localIso`);
|
|
108
|
+
}
|
|
109
|
+
assert.equal(parseNote(raw).meta.project, projectKey(ctx.cwd), "a newly-created session note records its existing project key");
|
|
110
|
+
assertReceiptIdentity(result, "a/b.md", "session", ctx);
|
|
111
|
+
assert.equal(result.outcome, "created");
|
|
112
|
+
assert.equal(existsSync(join(scopeDir("session", ctx), ".session.json")), false, "ownership is stored in note frontmatter, not a sidecar");
|
|
113
|
+
// A leading YAML block in user content is stripped from the body.
|
|
114
|
+
await call(captured, "notes_write", { address: "stripped.md", content: "---\nscope: human\nnonsense: true\n---\nreal body" }, ctx);
|
|
115
|
+
const stripped = readFileSync(physicalPath("session", "stripped.md", ctx), "utf8");
|
|
116
|
+
assert.match(stripped, /\n---\n\nreal body$/, "the injected block is not part of the body");
|
|
117
|
+
assert.equal(stripped.includes("nonsense"), false, "the injected block never reaches the file");
|
|
118
|
+
});
|
|
119
|
+
test("session-note project ownership is per note, persistent across sessions, and not reassigned by cwd", async () => {
|
|
120
|
+
freshRoot();
|
|
121
|
+
const cwdA = join(testEnvironment.cwd, "project-a");
|
|
122
|
+
const cwdB = join(testEnvironment.cwd, "project-b");
|
|
123
|
+
mkdirSync(cwdA, { recursive: true });
|
|
124
|
+
mkdirSync(cwdB, { recursive: true });
|
|
125
|
+
const projectA = projectKey(cwdA);
|
|
126
|
+
const projectB = projectKey(cwdB);
|
|
127
|
+
assert.notEqual(projectA, projectB);
|
|
128
|
+
const firstSession = manager();
|
|
129
|
+
const firstCaptured = makeExtension(firstSession);
|
|
130
|
+
const firstCtx = context(firstSession, undefined, undefined, true, cwdA);
|
|
131
|
+
await call(firstCaptured, "notes_write", { address: "first.md", content: "first project session" }, firstCtx);
|
|
132
|
+
const firstFile = physicalPath("session", "first.md", firstCtx);
|
|
133
|
+
assert.equal(parseNote(readFileSync(firstFile, "utf8")).meta.project, projectA);
|
|
134
|
+
const secondSession = manager();
|
|
135
|
+
const secondCaptured = makeExtension(secondSession);
|
|
136
|
+
const secondCtx = context(secondSession, undefined, undefined, true, cwdA);
|
|
137
|
+
await call(secondCaptured, "notes_write", { address: "second.md", content: "same project, another session" }, secondCtx);
|
|
138
|
+
const secondFile = physicalPath("session", "second.md", secondCtx);
|
|
139
|
+
assert.equal(parseNote(readFileSync(secondFile, "utf8")).meta.project, projectA, "another session in the same project carries the matching key");
|
|
140
|
+
const thirdSession = manager();
|
|
141
|
+
const thirdCaptured = makeExtension(thirdSession);
|
|
142
|
+
const thirdCtx = context(thirdSession, undefined, undefined, true, cwdB);
|
|
143
|
+
await call(thirdCaptured, "notes_write", { address: "third.md", content: "different project" }, thirdCtx);
|
|
144
|
+
const thirdFile = physicalPath("session", "third.md", thirdCtx);
|
|
145
|
+
assert.equal(parseNote(readFileSync(thirdFile, "utf8")).meta.project, projectB);
|
|
146
|
+
const projectASessions = [firstFile, secondFile, thirdFile].filter((file) => parseNote(readFileSync(file, "utf8")).meta.project === projectA);
|
|
147
|
+
assert.deepEqual(projectASessions.sort(), [firstFile, secondFile].sort(), "exact frontmatter project matching recognizes only sessions from the same project");
|
|
148
|
+
const movedContext = context(firstSession, undefined, undefined, true, cwdB);
|
|
149
|
+
await call(firstCaptured, "notes_write", { address: "first.md", content: "overwritten from another cwd" }, movedContext);
|
|
150
|
+
assert.equal(parseNote(readFileSync(firstFile, "utf8")).meta.project, projectA, "overwriting an existing note does not silently reassign it");
|
|
151
|
+
await call(firstCaptured, "notes_update", { address: "first.md", edits: [{ oldText: "overwritten", newText: "edited" }] }, movedContext);
|
|
152
|
+
assert.equal(parseNote(readFileSync(firstFile, "utf8")).meta.project, projectA, "editing an existing note preserves its original project key");
|
|
153
|
+
await call(firstCaptured, "notes_read", { address: "first.md" }, movedContext);
|
|
154
|
+
assert.equal(parseNote(readFileSync(firstFile, "utf8")).meta.project, projectA, "reading preserves existing project ownership");
|
|
155
|
+
await call(firstCaptured, "notes_write", { address: "new-from-project-b.md", content: "new note" }, movedContext);
|
|
156
|
+
assert.equal(parseNote(readFileSync(physicalPath("session", "new-from-project-b.md", movedContext), "utf8")).meta.project, projectB, "only a newly-created session note uses the current project key");
|
|
157
|
+
await call(firstCaptured, "notes_write", { address: "@project/project-note.md", content: "project home note" }, movedContext);
|
|
158
|
+
assert.equal(parseNote(readFileSync(physicalPath("project", "project-note.md", movedContext), "utf8")).meta.project, undefined, "project-home notes do not receive session ownership metadata");
|
|
159
|
+
assert.equal((await listNotes(movedContext, { scope: "session" })).length, 2, "project ownership remains frontmatter, not a separate note");
|
|
160
|
+
});
|
|
161
|
+
test("linked git worktrees share the main checkout's project key", () => {
|
|
162
|
+
freshRoot();
|
|
163
|
+
// realpath keeps the fixture's gitdir pointer and the asserted paths on one spelling
|
|
164
|
+
// (macOS temp roots live under the /var -> /private/var symlink).
|
|
165
|
+
const root = realpathSync(testEnvironment.cwd);
|
|
166
|
+
const main = join(root, "repo");
|
|
167
|
+
mkdirSync(main, { recursive: true });
|
|
168
|
+
const git = (args) => {
|
|
169
|
+
execFileSync("git", ["-c", "user.email=test@test", "-c", "user.name=test", ...args], { cwd: main, stdio: "ignore" });
|
|
170
|
+
};
|
|
171
|
+
git(["init", "-q"]);
|
|
172
|
+
git(["commit", "-q", "--allow-empty", "-m", "init"]);
|
|
173
|
+
const worktree = join(root, "wt");
|
|
174
|
+
git(["worktree", "add", "-q", "--detach", worktree]);
|
|
175
|
+
assert.equal(projectKey(worktree), projectKey(main), "a linked worktree resolves to the main checkout's key");
|
|
176
|
+
assert.equal(projectKey(join(worktree, "gone", "deeper")), projectKey(main), "a nonexistent subdirectory still resolves through its worktree");
|
|
177
|
+
assert.equal(scopeDir("project", context(manager(), undefined, undefined, true, worktree)), scopeDir("project", context(manager(), undefined, undefined, true, main)), "@project uses the same physical home from both checkouts");
|
|
178
|
+
});
|
|
179
|
+
test("unrecognized metadata remains ordinary frontmatter; invalid project ownership stays unknown", async () => {
|
|
180
|
+
freshRoot();
|
|
181
|
+
const session = manager();
|
|
182
|
+
const captured = makeExtension(session);
|
|
183
|
+
const ctx = context(session);
|
|
184
|
+
const legacyFile = physicalPath("session", "legacy.md", ctx);
|
|
185
|
+
mkdirSync(scopeDir("session", ctx), { recursive: true });
|
|
186
|
+
writeFileSync(legacyFile, `---
|
|
187
|
+
origin: self
|
|
188
|
+
created_at: 2026-01-01T00:00:00.000+00:00
|
|
189
|
+
updated_at: 2026-01-01T00:00:00.000+00:00
|
|
190
|
+
last_accessed: 2026-01-01T00:00:00.000+00:00
|
|
191
|
+
access_count: 0
|
|
192
|
+
source_window: old-window
|
|
193
|
+
recurrence_count: 2
|
|
194
|
+
recurrence_windows: old-window
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
legacy body`);
|
|
198
|
+
const parsed = parseNote(readFileSync(legacyFile, "utf8"), Date.parse("2026-02-01T00:00:00Z"));
|
|
199
|
+
assert.equal(parsed.meta.createdAt, Date.parse("2026-02-01T00:00:00Z"), "missing canonical timestamp takes the normal default");
|
|
200
|
+
assert.equal(parsed.meta.created_at, "2026-01-01T00:00:00.000+00:00", "unrecognized fields remain ordinary extras");
|
|
201
|
+
assert.equal((await listNotes(ctx, { scope: "session" }))[0]?.address, "legacy.md");
|
|
202
|
+
assert.match(resultRead(await call(captured, "notes_read", { address: "legacy.md" }, ctx)).content, /legacy body$/);
|
|
203
|
+
await call(captured, "notes_update", { address: "legacy.md", edits: [{ oldText: "legacy body", newText: "edited body" }] }, ctx);
|
|
204
|
+
await call(captured, "notes_write", { address: "legacy.md", content: "overwritten body" }, ctx);
|
|
205
|
+
const rewritten = parseNote(readFileSync(legacyFile, "utf8"));
|
|
206
|
+
assert.equal(rewritten.body, "overwritten body");
|
|
207
|
+
for (const key of ["created_at", "updated_at", "last_accessed", "access_count", "source_window", "recurrence_count", "recurrence_windows"]) {
|
|
208
|
+
assert.deepEqual(rewritten.meta[key], parsed.meta[key], `${key} is preserved as unrecognized frontmatter, not migrated`);
|
|
209
|
+
}
|
|
210
|
+
const invalidFile = physicalPath("session", "invalid.md", ctx);
|
|
211
|
+
writeFileSync(invalidFile, `---
|
|
212
|
+
origin: self
|
|
213
|
+
createdAt: 2026-01-01T00:00:00.000+00:00
|
|
214
|
+
updatedAt: 2026-01-01T00:00:00.000+00:00
|
|
215
|
+
lastAccessed: 2026-01-01T00:00:00.000+00:00
|
|
216
|
+
accessCount: 0
|
|
217
|
+
project: 17
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
invalid owner`);
|
|
221
|
+
assert.equal(parseNote(readFileSync(invalidFile, "utf8")).meta.project, 17);
|
|
222
|
+
assert.notEqual(parseNote(readFileSync(invalidFile, "utf8")).meta.project, projectKey(ctx.cwd), "invalid ownership does not match the current project key");
|
|
223
|
+
await call(captured, "notes_write", { address: "invalid.md", content: "still invalid" }, ctx);
|
|
224
|
+
assert.equal(parseNote(readFileSync(invalidFile, "utf8")).meta.project, 17, "an invalid value remains unknown and is not replaced with cwd-derived ownership");
|
|
225
|
+
assert.equal(existsSync(join(scopeDir("session", ctx), ".session.json")), false, "new notes use no ownership sidecar");
|
|
226
|
+
});
|
|
227
|
+
test("edit is body-scoped with named failures and a replace_all escape hatch", async () => {
|
|
228
|
+
freshRoot();
|
|
229
|
+
const session = manager();
|
|
230
|
+
const captured = makeExtension(session);
|
|
231
|
+
const ctx = context(session);
|
|
232
|
+
await call(captured, "notes_write", { address: "edit.md", content: "alpha\nbeta\nbeta\ngamma" }, ctx);
|
|
233
|
+
const ambiguous = resultError(await call(captured, "notes_update", { address: "edit.md", edits: [{ oldText: "beta", newText: "B" }] }, ctx));
|
|
234
|
+
assert.match(ambiguous.message, /occurs 2 times/);
|
|
235
|
+
assert.equal(ambiguous.code, "ambiguous_edit");
|
|
236
|
+
assert.deepEqual(ambiguous.details?.line_numbers, [2, 3], "the multi-match error carries every match line number");
|
|
237
|
+
const missing = resultError(await call(captured, "notes_update", { address: "edit.md", edits: [{ oldText: "absent", newText: "x" }] }, ctx));
|
|
238
|
+
assert.equal(missing.code, "no_match");
|
|
239
|
+
assert.equal(missing.details?.edit_index, 0, "a zero-match anchor names the failing edit index");
|
|
240
|
+
const all = resultData(await call(captured, "notes_update", { address: "edit.md", edits: [{ oldText: "beta", newText: "B" }], replace_all: true }, ctx));
|
|
241
|
+
assert.equal(all.applied, 1);
|
|
242
|
+
assert.equal(all.address, "edit.md");
|
|
243
|
+
assertReceiptIdentity(all, "edit.md", "session", ctx);
|
|
244
|
+
assert.equal(all.change_kind, "body");
|
|
245
|
+
assert.equal(resultRead(await call(captured, "notes_read", { address: "edit.md" }, ctx)).content, "alpha\nB\nB\ngamma", "replace_all replaces every occurrence");
|
|
246
|
+
// An anchor that occurs only in frontmatter is not matched: edits are body-only.
|
|
247
|
+
const frontmatterOnly = resultError(await call(captured, "notes_update", { address: "edit.md", edits: [{ oldText: "origin", newText: "x" }] }, ctx));
|
|
248
|
+
assert.equal(frontmatterOnly.details?.edit_index, 0, "a frontmatter-only anchor is not a body match");
|
|
249
|
+
});
|
|
250
|
+
test("nothing-to-do, not-found, atomic batches, and replace_all zero-match are named", async () => {
|
|
251
|
+
freshRoot();
|
|
252
|
+
const session = manager();
|
|
253
|
+
const captured = makeExtension(session);
|
|
254
|
+
const ctx = context(session);
|
|
255
|
+
const nameOnly = resultError(await call(captured, "notes_update", { address: "edit.md" }, ctx));
|
|
256
|
+
assert.match(nameOnly.message, /nothing to do/, "neither edits nor setters is a named error");
|
|
257
|
+
await call(captured, "notes_write", { address: "edit.md", content: "alpha\nbeta" }, ctx);
|
|
258
|
+
const empty = resultError(await call(captured, "notes_update", { address: "edit.md", edits: [] }, ctx));
|
|
259
|
+
assert.match(empty.message, /nothing to do/, "an empty edits list with no setters is also nothing to do");
|
|
260
|
+
const editMissing = resultError(await call(captured, "notes_update", { address: "missing.md", crumpled: true }, ctx));
|
|
261
|
+
assert.equal(editMissing.message, "note not found");
|
|
262
|
+
const readMissing = resultError(await call(captured, "notes_read", { address: "missing.md" }, ctx));
|
|
263
|
+
assert.equal(readMissing.message, "note not found");
|
|
264
|
+
assert.equal(readMissing.code, "not_found");
|
|
265
|
+
assert.equal(readMissing.details, undefined, "a plain refusal carries no edit details");
|
|
266
|
+
const file = physicalPath("session", "edit.md", ctx);
|
|
267
|
+
const before = readFileSync(file, "utf8");
|
|
268
|
+
const failed = resultError(await call(captured, "notes_update", { address: "edit.md", edits: [{ oldText: "alpha", newText: "A" }, { oldText: "absent", newText: "x" }] }, ctx));
|
|
269
|
+
assert.equal(failed.details?.edit_index, 1, "the failing edit is named");
|
|
270
|
+
assert.equal(readFileSync(file, "utf8"), before, "a failing batch leaves the file byte-identical, frontmatter included");
|
|
271
|
+
const applied = resultData(await call(captured, "notes_update", { address: "edit.md", edits: [{ oldText: "alpha", newText: "A" }, { oldText: "beta", newText: "B" }] }, ctx));
|
|
272
|
+
assert.equal(applied.applied, 2);
|
|
273
|
+
assert.equal(resultRead(await call(captured, "notes_read", { address: "edit.md" }, ctx)).content, "A\nB");
|
|
274
|
+
const zero = resultError(await call(captured, "notes_update", { address: "edit.md", edits: [{ oldText: "zzz", newText: "y" }], replace_all: true }, ctx));
|
|
275
|
+
assert.equal(zero.details?.edit_index, 0, "replace_all with zero matches is the same zero-match error, not a silent no-op");
|
|
276
|
+
});
|
|
277
|
+
test("notes_update rename_to moves a note and refuses combinations and live targets", async () => {
|
|
278
|
+
freshRoot();
|
|
279
|
+
const session = manager();
|
|
280
|
+
const captured = makeExtension(session);
|
|
281
|
+
const ctx = context(session);
|
|
282
|
+
await call(captured, "notes_write", { address: "move-me.md", content: "body" }, ctx);
|
|
283
|
+
const combined = resultError(await call(captured, "notes_update", { address: "move-me.md", rename_to: "moved.md", edits: [{ oldText: "body", newText: "x" }] }, ctx));
|
|
284
|
+
assert.match(combined.message, /rename_to is used alone/, "rename_to rejects being combined with edits");
|
|
285
|
+
await call(captured, "notes_write", { address: "occupied.md", content: "live target" }, ctx);
|
|
286
|
+
const conflict = resultError(await call(captured, "notes_update", { address: "move-me.md", rename_to: "occupied.md" }, ctx));
|
|
287
|
+
assert.match(conflict.message, /live note/, "a live target refuses the move");
|
|
288
|
+
const renamed = resultData(await call(captured, "notes_update", { address: "move-me.md", rename_to: "moved.md" }, ctx));
|
|
289
|
+
assertReceiptIdentity(renamed, "moved.md", "session", ctx);
|
|
290
|
+
assert.equal(renamed.rename_from, "move-me.md");
|
|
291
|
+
assert.equal(renamed.change_kind, "file");
|
|
292
|
+
assert.equal(renamed.address, "moved.md");
|
|
293
|
+
assert.equal(renamed.replaced_crumpled_target, false);
|
|
294
|
+
const gone = resultError(await call(captured, "notes_read", { address: "move-me.md" }, ctx));
|
|
295
|
+
assert.equal(gone.message, "note not found", "the old address is gone");
|
|
296
|
+
assert.equal(resultRead(await call(captured, "notes_read", { address: "moved.md" }, ctx)).content, "body");
|
|
297
|
+
// Empty filler values are not a combination: a model passing every parameter still gets its crumple.
|
|
298
|
+
const crumpled = resultData(await call(captured, "notes_update", { address: "moved.md", crumpled: true, edits: [], origin: "self", rename_to: "", replace_all: false }, ctx));
|
|
299
|
+
assert.equal(crumpled.address, "moved.md", "an empty rename_to is ignored as if omitted");
|
|
300
|
+
const basket = resultData(await call(captured, "notes_list", { wastebasket: true }, ctx));
|
|
301
|
+
assert.equal(basket.files.some((file) => file.address === "moved.md"), true, "the note was crumpled");
|
|
302
|
+
const fillerRename = resultData(await call(captured, "notes_update", { address: "moved.md", rename_to: "moved-again.md", edits: [], replace_all: false }, ctx));
|
|
303
|
+
assert.equal(fillerRename.address, "moved-again.md", "empty edits and replace_all: false ride along with a rename");
|
|
304
|
+
const realCombo = resultError(await call(captured, "notes_update", { address: "moved-again.md", rename_to: "nope.md", replace_all: true }, ctx));
|
|
305
|
+
assert.match(realCombo.message, /rename_to is used alone/, "replace_all: true is a real combination and refuses");
|
|
306
|
+
});
|
|
307
|
+
test("write and update receipts distinguish outcomes and no-op edits", async () => {
|
|
308
|
+
freshRoot();
|
|
309
|
+
const session = manager();
|
|
310
|
+
const captured = makeExtension(session);
|
|
311
|
+
const ctx = context(session);
|
|
312
|
+
const alias = resultData(await call(captured, "notes_write", { address: "@self/alias", content: "body" }, ctx));
|
|
313
|
+
assertReceiptIdentity(alias, "@self/alias.md", "agent", ctx);
|
|
314
|
+
assert.equal(alias.outcome, "created");
|
|
315
|
+
assert.equal(resultData(await call(captured, "notes_write", { address: "@agents/anonymous/alias.md", content: "new body" }, ctx)).outcome, "overwrote");
|
|
316
|
+
await call(captured, "notes_update", { address: "@self/alias", crumpled: true }, ctx);
|
|
317
|
+
assert.equal(resultData(await call(captured, "notes_write", { address: "@self/alias", content: "restored" }, ctx)).outcome, "uncrumpled");
|
|
318
|
+
const noChange = resultData(await call(captured, "notes_update", { address: "@self/alias", edits: [{ oldText: "restored", newText: "restored" }], crumpled: false }, ctx));
|
|
319
|
+
assert.deepEqual([noChange.applied, noChange.change_kind, noChange.diff], [0, "none", ""]);
|
|
320
|
+
const mixed = resultData(await call(captured, "notes_update", { address: "@self/alias", edits: [{ oldText: "restored", newText: "restored" }, { oldText: "restored", newText: "changed" }] }, ctx));
|
|
321
|
+
assert.equal(mixed.applied, 1);
|
|
322
|
+
assert.equal(mixed.change_kind, "body");
|
|
323
|
+
});
|
|
324
|
+
test("empty results distinguish hidden notes and unavailable homes", async () => {
|
|
325
|
+
freshRoot();
|
|
326
|
+
const session = manager();
|
|
327
|
+
const captured = makeExtension(session);
|
|
328
|
+
const ctx = context(session);
|
|
329
|
+
const empty = resultData(await call(captured, "notes_list", { pattern: "@human/**" }, ctx));
|
|
330
|
+
assert.deepEqual(empty, { files: [], more: 0, crumpled_excluded: 0, homes_unavailable: [] });
|
|
331
|
+
await call(captured, "notes_write", { address: "@human/hidden.md", content: "needle" }, ctx);
|
|
332
|
+
await call(captured, "notes_update", { address: "@human/hidden.md", crumpled: true }, ctx);
|
|
333
|
+
const hidden = resultData(await call(captured, "notes_search", { query: "needle", pattern: "@human/**" }, ctx));
|
|
334
|
+
assert.equal(hidden.crumpled_excluded, 1);
|
|
335
|
+
const broken = scopeDir("project", ctx);
|
|
336
|
+
mkdirSync(join(broken, ".."), { recursive: true });
|
|
337
|
+
writeFileSync(broken, "not a directory");
|
|
338
|
+
const partial = resultData(await call(captured, "notes_list", { pattern: "**" }, ctx));
|
|
339
|
+
assert.deepEqual(partial.homes_unavailable, ["@project"]);
|
|
340
|
+
assert.deepEqual(partial.files, []);
|
|
341
|
+
const searched = resultData(await call(captured, "notes_search", { query: "needle", pattern: "**" }, ctx));
|
|
342
|
+
assert.deepEqual(searched.homes_unavailable, ["@project"]);
|
|
343
|
+
writeFileSync(join(process.env.PI_NOTES_HOME, "agents"), "not a directory");
|
|
344
|
+
const namespace = resultData(await call(captured, "notes_list", { pattern: "@agents/*/note.md" }, ctx));
|
|
345
|
+
assert.deepEqual(namespace.homes_unavailable, ["@agents"]);
|
|
346
|
+
});
|
|
347
|
+
test("notes receipts report resolved identity across scopes", async () => {
|
|
348
|
+
freshRoot();
|
|
349
|
+
const session = manager();
|
|
350
|
+
const captured = makeExtension(session);
|
|
351
|
+
const ctx = context(session);
|
|
352
|
+
const notes = [
|
|
353
|
+
{ address: "session.md", body: "session needle", scope: "session" },
|
|
354
|
+
{ address: "@project/project.md", body: "project needle", scope: "project" },
|
|
355
|
+
{ address: "@human/human.md", body: "human needle", scope: "human" },
|
|
356
|
+
];
|
|
357
|
+
for (const note of notes) {
|
|
358
|
+
const written = resultData(await call(captured, "notes_write", { address: note.address, content: note.body }, ctx));
|
|
359
|
+
assertReceiptIdentity(written, note.address, note.scope, ctx);
|
|
360
|
+
const edited = resultData(await call(captured, "notes_update", { address: note.address, edits: [{ oldText: "needle", newText: "match" }] }, ctx));
|
|
361
|
+
assertReceiptIdentity(edited, note.address, note.scope, ctx);
|
|
362
|
+
const rawRead = await call(captured, "notes_read", { address: note.address }, ctx);
|
|
363
|
+
const read = resultRead(rawRead);
|
|
364
|
+
assertReceiptIdentity(resultData(rawRead), note.address, note.scope, ctx);
|
|
365
|
+
assert.equal(read.content, note.body.replace("needle", "match"), "the window carries the body only");
|
|
366
|
+
assert.equal("scope" in read.metadata, false, "scope is derivable from the address and never echoed");
|
|
367
|
+
assert.equal(read.metadata.project !== undefined, note.scope === "session", "stored project ownership is exposed as metadata");
|
|
368
|
+
assert.equal(rawRead.content[0].type === "text" && rawRead.content[0].text.includes("---"), false, "the rendered read carries no frontmatter block");
|
|
369
|
+
}
|
|
370
|
+
const listed = resultData(await call(captured, "notes_list", { pattern: "**" }, ctx));
|
|
371
|
+
assert.deepEqual(listed.files.map((file) => file.address).sort(), notes.map((note) => note.address).sort(), "notes_list returns each full address");
|
|
372
|
+
for (const row of listed.files)
|
|
373
|
+
assertReceiptIdentity(row, row.address, notes.find((note) => note.address === row.address).scope, ctx);
|
|
374
|
+
const searched = resultData(await call(captured, "notes_search", { query: "match", pattern: "**" }, ctx));
|
|
375
|
+
assert.deepEqual(searched.files.map((file) => file.address), notes.map((note) => note.address).sort(), "notes_search returns each full address");
|
|
376
|
+
for (const row of searched.files)
|
|
377
|
+
assertReceiptIdentity(row, row.address, notes.find((note) => note.address === row.address).scope, ctx);
|
|
378
|
+
});
|
|
379
|
+
test("list and search merge scopes and carry addresses; the path jail rejects escapes", async () => {
|
|
380
|
+
freshRoot();
|
|
381
|
+
const session = manager();
|
|
382
|
+
const captured = makeExtension(session);
|
|
383
|
+
const ctx = context(session);
|
|
384
|
+
await call(captured, "notes_write", { address: "one.md", content: "needle one" }, ctx);
|
|
385
|
+
await call(captured, "notes_write", { address: "@project/two.md", content: "needle two" }, ctx);
|
|
386
|
+
await call(captured, "notes_write", { address: "@human/three.md", content: "needle three" }, ctx);
|
|
387
|
+
const listed = resultData(await call(captured, "notes_list", {}, ctx));
|
|
388
|
+
assert.deepEqual([...listed.files].map((file) => file.address).sort(), ["@human/three.md", "@project/two.md", "one.md"], "every merged row carries its full address");
|
|
389
|
+
for (const row of listed.files) {
|
|
390
|
+
assert.deepEqual(Object.keys(row).sort(), ["address", "updated_at", ...(row.address.startsWith("@project/") ? ["project_key"] : [])].sort());
|
|
391
|
+
}
|
|
392
|
+
const scoped = resultData(await call(captured, "notes_list", { pattern: "@human/**" }, ctx));
|
|
393
|
+
assert.deepEqual(scoped.files.map((file) => file.address), ["@human/three.md"], "an address-pattern filter narrows the set");
|
|
394
|
+
const searched = resultData(await call(captured, "notes_search", { query: "needle" }, ctx));
|
|
395
|
+
assert.equal(searched.files.length, 3, "literal search finds matches in every scope");
|
|
396
|
+
assert.deepEqual([...searched.files].map((file) => file.address).sort(), ["@human/three.md", "@project/two.md", "one.md"]);
|
|
397
|
+
for (const row of [...listed.files, ...searched.files])
|
|
398
|
+
assert.equal("scope" in row, false, "scope is derivable from the address and never echoed");
|
|
399
|
+
assert.equal(searched.files.every((file) => file.matches_total === 1), true);
|
|
400
|
+
const hit = searched.files[0].matches[0];
|
|
401
|
+
assert.equal(hit.line, 1);
|
|
402
|
+
assert.equal(hit.offset_chars, 0, "the offset counts into the note body, so it does not include serialized frontmatter");
|
|
403
|
+
assert.equal(hit.truncated, false);
|
|
404
|
+
assert.deepEqual(Object.keys(hit).sort(), ["line", "offset_chars", "text", "truncated"]);
|
|
405
|
+
assert.equal(resultRead(await call(captured, "notes_read", { address: searched.files[0].address, offset_chars: hit.offset_chars }, ctx)).content.startsWith("needle"), true, "the search offset starts a read at the match");
|
|
406
|
+
const escaped = ["../evil", "/abs", "a\\b"];
|
|
407
|
+
for (const tool of ["notes_write", "notes_update", "notes_read"]) {
|
|
408
|
+
for (const path of escaped) {
|
|
409
|
+
assert.equal(resultError(await call(captured, tool, { address: path, content: "x", edits: [{ oldText: "a", newText: "b" }] }, ctx)).code, "invalid_address");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
assert.equal(resultError(await call(captured, "notes_search", { query: "" }, ctx)).code, "invalid_query");
|
|
413
|
+
assert.equal(resultError(await call(captured, "notes_read", { address: "one.md", offset_chars: 99999 }, ctx)).code, "invalid_offset");
|
|
414
|
+
assert.equal(resultError(await call(captured, "notes_list", { pattern: "bad\\glob" }, ctx)).code, "invalid_pattern");
|
|
415
|
+
assert.equal(resultError(await call(captured, "notes_search", { query: "needle", pattern: "bad\\glob" }, ctx)).code, "invalid_pattern");
|
|
416
|
+
});
|
|
417
|
+
test("a read offsets the body in code points, not bytes or frontmatter", async () => {
|
|
418
|
+
freshRoot();
|
|
419
|
+
const session = manager();
|
|
420
|
+
const captured = makeExtension(session);
|
|
421
|
+
const ctx = context(session);
|
|
422
|
+
await call(captured, "notes_write", { address: "emoji.md", content: "🐑字\nneedle tail" }, ctx);
|
|
423
|
+
const search = resultData(await call(captured, "notes_search", { query: "needle", pattern: "emoji.md" }, ctx));
|
|
424
|
+
const match = search.files[0].matches[0];
|
|
425
|
+
assert.equal(match.line, 2);
|
|
426
|
+
assert.equal(match.offset_chars, 3, "the emoji and CJK line above contribute two code points plus the newline, not their bytes");
|
|
427
|
+
const atMatch = resultRead(await call(captured, "notes_read", { address: "emoji.md", offset_chars: match.offset_chars }, ctx));
|
|
428
|
+
assert.equal(atMatch.offset_chars, match.offset_chars);
|
|
429
|
+
assert.equal(atMatch.text.startsWith("needle"), true, "the offset lands on the match even with multi-byte characters above it");
|
|
430
|
+
assert.equal(atMatch.total_chars, Array.from("🐑字\nneedle tail").length);
|
|
431
|
+
const negative = resultRead(await call(captured, "notes_read", { address: "emoji.md", offset_chars: -4 }, ctx));
|
|
432
|
+
assert.equal(negative.text, "tail", "a negative offset counts back from the end of the body");
|
|
433
|
+
assert.equal(negative.next_offset_chars, null, "a tail read has no continuation");
|
|
434
|
+
assert.equal(negative.limited_by, null);
|
|
435
|
+
const start = resultRead(await call(captured, "notes_read", { address: "emoji.md", limit_chars: 3 }, ctx));
|
|
436
|
+
assert.equal(start.text, "🐑字\n");
|
|
437
|
+
assert.equal(start.next_offset_chars, 3);
|
|
438
|
+
assert.equal(start.limited_by, "limit", "the requested count is what stopped this window");
|
|
439
|
+
assert.equal(resultRead(await call(captured, "notes_read", { address: "emoji.md", offset_chars: 14 }, ctx)).text, "", "the exact end is an empty window, not a refusal");
|
|
440
|
+
});
|
|
441
|
+
test("a read keeps its body window verbatim under the new presentation", async () => {
|
|
442
|
+
freshRoot();
|
|
443
|
+
const session = manager();
|
|
444
|
+
const captured = makeExtension(session);
|
|
445
|
+
const ctx = context(session);
|
|
446
|
+
await call(captured, "notes_write", { address: "present.md", content: "0123456789" }, ctx);
|
|
447
|
+
const limited = await call(captured, "notes_read", { address: "present.md", limit_chars: 4 }, ctx);
|
|
448
|
+
const text = limited.content[0].type === "text" ? limited.content[0].text : "";
|
|
449
|
+
assert.equal(text.includes("READ WINDOW"), false, "the old READ WINDOW block is gone");
|
|
450
|
+
assert.equal(text.includes("chars: ["), false, "no parsed range block stands between the header and the body");
|
|
451
|
+
assert.equal(text.endsWith("\n\n[6 more characters. Use offset_chars=4 to continue.]"), true, "a limit-stopped window names the resume cursor");
|
|
452
|
+
const header = text.slice(0, text.indexOf("\n\n"));
|
|
453
|
+
assert.match(header, /^present\.md\norigin self \| created \d{4}-/, "identity and metadata lead, body first in the window itself");
|
|
454
|
+
const whole = await call(captured, "notes_read", { address: "present.md" }, ctx);
|
|
455
|
+
const wholeText = whole.content[0].type === "text" ? whole.content[0].text : "";
|
|
456
|
+
assert.equal(wholeText.endsWith("\n\n0123456789"), true, "a complete window carries no continuation footer");
|
|
457
|
+
assert.equal(wholeText.includes("more characters"), false);
|
|
458
|
+
});
|
|
459
|
+
test("@ addresses select one home, reject illegal sigils, and never fall back", async () => {
|
|
460
|
+
const root = freshRoot();
|
|
461
|
+
const session = manager();
|
|
462
|
+
const captured = makeExtension(session);
|
|
463
|
+
const ctx = context(session);
|
|
464
|
+
await call(captured, "notes_write", { address: "same.md", content: "session" }, ctx);
|
|
465
|
+
await call(captured, "notes_write", { address: "@project/same.md", content: "project" }, ctx);
|
|
466
|
+
await call(captured, "notes_write", { address: "@human/same.md", content: "human" }, ctx);
|
|
467
|
+
assert.ok(existsSync(physicalPath("project", "same.md", ctx)), "@project writes to the current project home");
|
|
468
|
+
assert.ok(existsSync(physicalPath("human", "same.md", ctx)), "@human writes to the human home");
|
|
469
|
+
assert.match(resultRead(await call(captured, "notes_read", { address: "same.md" }, ctx)).content, /session$/);
|
|
470
|
+
assert.equal(resultError(await call(captured, "notes_read", { address: "@project/missing.md" }, ctx)).message, "note not found");
|
|
471
|
+
assert.match(resultError(await call(captured, "notes_read", { address: "@glboal/same.md" }, ctx)).message, /no @ for this session.*@project\/.*@human\//);
|
|
472
|
+
assert.equal(resultError(await call(captured, "notes_write", { address: "bad@name.md", content: "no" }, ctx)).code, "invalid_address");
|
|
473
|
+
assert.equal(existsSync(join(root, "human", "bad@name.md")), false, "a bad sigil creates nothing anywhere");
|
|
474
|
+
});
|
|
475
|
+
test("Pi adapter defaults to anonymous agent identity", async () => {
|
|
476
|
+
const root = freshRoot();
|
|
477
|
+
const session = manager();
|
|
478
|
+
const captured = makeExtension(session);
|
|
479
|
+
const ctx = context(session);
|
|
480
|
+
await withAgent(undefined, async () => {
|
|
481
|
+
await call(captured, "notes_write", { address: "@self/private.md", content: "anonymous agent note" }, ctx);
|
|
482
|
+
const listed = resultData(await call(captured, "notes_list", {}, ctx));
|
|
483
|
+
assert.deepEqual(listed.files.map((row) => row.address), ["@self/private.md"]);
|
|
484
|
+
const explicit = resultData(await call(captured, "notes_list", { pattern: "@agents/*/private.md" }, ctx));
|
|
485
|
+
assert.deepEqual(explicit.files.map((row) => row.address), ["@self/private.md"], "explicit-id patterns still match the current home");
|
|
486
|
+
assert.equal(existsSync(join(root, "agents", "anonymous", "private.md")), true);
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
test("Pi adapter resolves agent and switched model identity on each notes call and boot", async () => {
|
|
490
|
+
const root = freshRoot();
|
|
491
|
+
const session = manager();
|
|
492
|
+
const captured = makeExtension(session);
|
|
493
|
+
const ctx = context(session, undefined, undefined, true, testEnvironment.cwd, true, "provider/First.Model");
|
|
494
|
+
await withAgent("Test Agent", async () => {
|
|
495
|
+
await call(captured, "notes_write", { address: "@self/private.md", content: "agent note" }, ctx);
|
|
496
|
+
await call(captured, "notes_write", { address: "@model/private.md", content: "first model note" }, ctx);
|
|
497
|
+
ctx.model = context(session, undefined, undefined, true, testEnvironment.cwd, true, "other/Second.Model").model;
|
|
498
|
+
assert.equal(resultError(await call(captured, "notes_read", { address: "@model/private.md" }, ctx)).message, "note not found", "switched model does not fall back to previous home");
|
|
499
|
+
await call(captured, "notes_write", { address: "@model/private.md", content: "second model note" }, ctx);
|
|
500
|
+
const listed = resultData(await call(captured, "notes_list", {}, ctx));
|
|
501
|
+
assert.deepEqual(listed.files.map((row) => row.address).sort(), ["@model/private.md", "@self/private.md"]);
|
|
502
|
+
const boot = await explicitBoot(ctx, "test-window", undefined);
|
|
503
|
+
assert.ok(boot.includes("@model/private.md"));
|
|
504
|
+
assert.equal(boot.includes("@models/first-model/private.md"), false);
|
|
505
|
+
assert.match(resultRead(await call(captured, "notes_read", { address: "@models/first-model/private.md" }, ctx)).content, /first model note$/);
|
|
506
|
+
const refused = resultError(await call(captured, "notes_update", { address: "@models/first-model/private.md", crumpled: true }, ctx));
|
|
507
|
+
assert.match(refused.message, /not your home/);
|
|
508
|
+
assert.match(readFileSync(join(root, "models/first-model/private.md"), "utf8"), /first model note$/);
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
async function withAgent(name, run) {
|
|
512
|
+
const previous = process.env.PI_NOTES_AGENT;
|
|
513
|
+
if (name === undefined)
|
|
514
|
+
delete process.env.PI_NOTES_AGENT;
|
|
515
|
+
else
|
|
516
|
+
process.env.PI_NOTES_AGENT = name;
|
|
517
|
+
try {
|
|
518
|
+
// A plain `return run()` would run the finally block the moment the promise pends,
|
|
519
|
+
// restoring the env while the callback still has awaits ahead of it.
|
|
520
|
+
await run();
|
|
521
|
+
}
|
|
522
|
+
finally {
|
|
523
|
+
if (previous === undefined)
|
|
524
|
+
delete process.env.PI_NOTES_AGENT;
|
|
525
|
+
else
|
|
526
|
+
process.env.PI_NOTES_AGENT = previous;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|