notesoup 0.53.2 → 0.54.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/README.md +2 -2
- package/dist/plugins/claude/src/helper.js +2 -1
- package/dist/plugins/codex/src/hooks.js +1 -1
- package/dist/plugins/codex/src/identity.d.ts +1 -1
- package/dist/plugins/codex/src/identity.js +3 -3
- package/dist/plugins/codex/src/mcp.js +2 -1
- package/dist/plugins/codex/src/text.d.ts +1 -1
- package/dist/plugins/codex/src/text.js +2 -2
- package/dist/plugins/dsh/engine.js +2 -2
- package/dist/plugins/dsh/tools.js +8 -7
- package/dist/plugins/dsh/window.d.ts +1 -0
- package/dist/plugins/dsh/window.js +7 -3
- package/dist/plugins/pi/history-tools.d.ts +1 -1
- package/dist/plugins/pi/history-tools.js +4 -4
- package/dist/plugins/pi/notes/tools.js +8 -6
- package/dist/plugins/pi/render.js +7 -4
- package/dist/plugins/pi/runtime.js +2 -2
- package/dist/plugins/pi/window.js +21 -7
- package/dist/src/boot/render.js +7 -7
- package/dist/src/boot/snapshot.d.ts +7 -7
- package/dist/src/boot/snapshot.js +17 -17
- package/dist/src/boot/text.d.ts +2 -2
- package/dist/src/boot/text.js +7 -5
- package/dist/src/dream/git.d.ts +2 -11
- package/dist/src/dream/git.js +6 -23
- package/dist/src/dream/lock.d.ts +2 -2
- package/dist/src/dream/lock.js +3 -3
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +3 -2
- package/dist/src/notes/address.d.ts +8 -8
- package/dist/src/notes/address.js +13 -13
- package/dist/src/notes/errors.d.ts +1 -1
- package/dist/src/notes/frontmatter.js +2 -2
- package/dist/src/notes/index.d.ts +1 -1
- package/dist/src/notes/index.js +1 -1
- package/dist/src/notes/paths.d.ts +6 -6
- package/dist/src/notes/paths.js +9 -9
- package/dist/src/notes/{project-home.d.ts → project-namespace.d.ts} +7 -7
- package/dist/src/notes/{project-home.js → project-namespace.js} +67 -25
- package/dist/src/notes/store.d.ts +4 -4
- package/dist/src/notes/store.js +55 -52
- package/dist/src/tools/dream.d.ts +21 -3
- package/dist/src/tools/dream.js +54 -16
- package/dist/src/tools/notes-context.d.ts +10 -0
- package/dist/src/tools/notes-context.js +27 -0
- package/dist/src/tools/notes.d.ts +69 -20
- package/dist/src/tools/notes.js +40 -31
- package/dist/test/boot.integration.test.js +1 -1
- package/dist/test/claude-mod.test.js +89 -43
- package/dist/test/codex-plugin.test.js +18 -1
- package/dist/test/context-edit-budget.test.js +121 -0
- package/dist/test/dependencies.test.js +71 -4
- package/dist/test/dream-tools.test.js +153 -8
- package/dist/test/dsh.test.js +62 -15
- package/dist/test/helpers/dsh.d.ts +4 -1
- package/dist/test/helpers/dsh.js +20 -4
- package/dist/test/helpers/extension.d.ts +5 -0
- package/dist/test/helpers/extension.js +33 -2
- package/dist/test/host-neutral.test.js +17 -15
- package/dist/test/model-facing-text.test.js +12 -8
- package/dist/test/notes-cwd.test.js +114 -0
- package/dist/test/notes-library.test.js +4 -4
- package/dist/test/notes-query-namespaces.test.d.ts +1 -0
- package/dist/test/{notes-query-homes.test.js → notes-query-namespaces.test.js} +13 -13
- package/dist/test/notes.integration.test.js +72 -23
- package/dist/test/notes.test.js +50 -20
- package/dist/test/pi-render.test.js +33 -1
- package/dist/test/project-namespace.test.d.ts +1 -0
- package/dist/test/project-namespace.test.js +324 -0
- package/docs/architecture.md +12 -7
- package/docs/codex.md +4 -4
- package/docs/dsh.md +8 -8
- package/docs/notes-library.md +9 -5
- package/docs/skills.md +3 -3
- package/docs/tool-results.md +6 -2
- package/package.json +4 -6
- package/playbook.md +6 -6
- package/plugins/claude/.claude-plugin/plugin.json +1 -1
- package/plugins/claude/dist/claude/helper.js +677 -615
- package/plugins/claude/hooks/register.ts +19 -11
- package/plugins/claude/playbook.md +6 -6
- package/plugins/claude/skills/memory/SKILL.md +4 -4
- package/plugins/claude/src/helper.ts +3 -2
- package/plugins/codex/README.md +1 -1
- package/plugins/codex/dist/codex/notesoup.js +877 -815
- package/plugins/codex/src/hooks.ts +1 -1
- package/plugins/codex/src/identity.ts +3 -3
- package/plugins/codex/src/mcp.ts +2 -1
- package/plugins/codex/src/text.ts +2 -2
- package/plugins/dsh/engine.ts +2 -2
- package/plugins/dsh/tools.ts +8 -7
- package/plugins/dsh/window.ts +8 -3
- package/plugins/paseo/OVERVIEW.md +4 -4
- package/plugins/paseo/README.md +1 -1
- package/plugins/paseo/client/brain-empty-card.tsx +3 -4
- package/plugins/paseo/client/markdown.tsx +179 -0
- package/plugins/paseo/client/notes-op-card.tsx +35 -34
- package/plugins/paseo/client/notes-op-transformer.ts +17 -68
- package/plugins/paseo/client/paper-card.tsx +8 -19
- package/plugins/paseo/client/paper-palette.ts +40 -0
- package/plugins/paseo/client/sticky-card.tsx +6 -4
- package/plugins/paseo/package.json +3 -0
- package/plugins/paseo/paseo-plugin.json +2 -2
- package/plugins/paseo/shared/notes-op.ts +13 -9
- package/plugins/paseo/test/notes-op.test.ts +52 -73
- package/plugins/paseo/test/paper-palette.test.ts +54 -0
- package/plugins/pi/history-tools.ts +4 -4
- package/plugins/pi/notes/tools.ts +8 -6
- package/plugins/pi/render.ts +6 -4
- package/plugins/pi/runtime.ts +2 -2
- package/plugins/pi/window.ts +22 -7
- package/skills/memory/SKILL.md +4 -4
- package/src/boot/render.ts +8 -8
- package/src/boot/snapshot.ts +20 -20
- package/src/boot/text.ts +7 -5
- package/src/dream/git.ts +7 -24
- package/src/dream/lock.ts +3 -3
- package/src/index.ts +4 -3
- package/src/notes/address.ts +13 -13
- package/src/notes/errors.ts +1 -1
- package/src/notes/frontmatter.ts +2 -2
- package/src/notes/index.ts +1 -1
- package/src/notes/paths.ts +9 -9
- package/src/notes/{project-home.ts → project-namespace.ts} +59 -28
- package/src/notes/store.ts +61 -58
- package/src/tools/dream.ts +63 -16
- package/src/tools/notes-context.ts +29 -0
- package/src/tools/notes.ts +39 -29
- package/dist/test/project-home.test.js +0 -134
- /package/dist/test/{notes-query-homes.test.d.ts → context-edit-budget.test.d.ts} +0 -0
- /package/dist/test/{project-home.test.d.ts → notes-cwd.test.d.ts} +0 -0
package/dist/src/notes/store.js
CHANGED
|
@@ -2,14 +2,14 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { mkdir, readdir, readFile, realpath, rename, rm, unlink, writeFile } from "node:fs/promises";
|
|
3
3
|
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import { earliestMatchOffsetChars } from "../text-match.js";
|
|
5
|
-
import { assertAddress, assertGlobPattern, addressFor, canonicalAddressFor, globToRegExp,
|
|
5
|
+
import { assertAddress, assertGlobPattern, addressFor, canonicalAddressFor, globToRegExp, NAMESPACE_HEADS } from "./address.js";
|
|
6
6
|
import { isFilesystemError } from "./fs-error.js";
|
|
7
7
|
import { snapshotNotesIdentity } from "./identity.js";
|
|
8
8
|
import { NoteError } from "./errors.js";
|
|
9
9
|
import { MAX_NOTE_BYTES, MAX_NOTE_PATH_BYTES } from "./constants.js";
|
|
10
10
|
import { isOrigin, localIso, parseNote, serializeNote, stripLeadingFrontmatter } from "./frontmatter.js";
|
|
11
11
|
import { namespaceSlugs, noteFileName, physicalPath, scopeDir, isSessionId, SLUG_PATTERN } from "./paths.js";
|
|
12
|
-
import { projectReceipt } from "./project-
|
|
12
|
+
import { projectReceipt } from "./project-namespace.js";
|
|
13
13
|
// Recency sorts tie-break by address, so two writes sharing one Date.now() tick would silently invert the recent-first contract. Write stamps are strictly increasing per process.
|
|
14
14
|
let lastWriteStamp = 0;
|
|
15
15
|
function writeStamp() {
|
|
@@ -60,8 +60,8 @@ async function walkMarkdown(dir, base = dir) {
|
|
|
60
60
|
entries = await readdir(dir, { withFileTypes: true });
|
|
61
61
|
}
|
|
62
62
|
catch (error) {
|
|
63
|
-
// A
|
|
64
|
-
// boot snapshot boundary instead of masquerading as an empty
|
|
63
|
+
// A notebook that has never been created is normal. Other failures must reach the
|
|
64
|
+
// boot snapshot boundary instead of masquerading as an empty notebook.
|
|
65
65
|
if (errno(error) === "ENOENT")
|
|
66
66
|
return [];
|
|
67
67
|
throw error;
|
|
@@ -76,27 +76,27 @@ async function walkMarkdown(dir, base = dir) {
|
|
|
76
76
|
}
|
|
77
77
|
return paths;
|
|
78
78
|
}
|
|
79
|
-
async function
|
|
79
|
+
async function namespacesForPattern(pattern, identity, status) {
|
|
80
80
|
if (pattern === undefined)
|
|
81
81
|
return SCOPE_ORDER.map((scope) => ({ scope }));
|
|
82
82
|
const families = [
|
|
83
|
-
{ scope: "session",
|
|
84
|
-
{ scope: "project",
|
|
85
|
-
{ scope: "agent",
|
|
86
|
-
{ scope: "model",
|
|
83
|
+
{ scope: "session", dir: "sessions", current: identity.sessionId },
|
|
84
|
+
{ scope: "project", dir: "project", current: identity.projectKey },
|
|
85
|
+
{ scope: "agent", dir: "agents", current: identity.agent },
|
|
86
|
+
{ scope: "model", dir: "models", current: identity.model },
|
|
87
87
|
];
|
|
88
88
|
async function enumerate(family, relative) {
|
|
89
89
|
let names;
|
|
90
90
|
try {
|
|
91
|
-
names = await namespaceSlugs(family.
|
|
91
|
+
names = await namespaceSlugs(family.dir, identity.home);
|
|
92
92
|
}
|
|
93
93
|
catch (error) {
|
|
94
94
|
if (!status || !isFilesystemError(error))
|
|
95
95
|
throw error;
|
|
96
|
-
status.
|
|
96
|
+
status.namespacesUnavailable.push(`@${family.dir === "project" ? "projects" : family.dir}`);
|
|
97
97
|
return [];
|
|
98
98
|
}
|
|
99
|
-
// Include the current
|
|
99
|
+
// Include the current namespace even when missing or a non-directory, and only once.
|
|
100
100
|
return [...new Set([family.current, ...names])].map((who) => ({
|
|
101
101
|
scope: family.scope,
|
|
102
102
|
who: relative && who === family.current ? undefined : who,
|
|
@@ -105,26 +105,26 @@ async function homesForPattern(pattern, identity, status) {
|
|
|
105
105
|
if (pattern.startsWith("@")) {
|
|
106
106
|
const [rawHead, ...tail] = pattern.slice(1).split("/");
|
|
107
107
|
if (!/[*?]/.test(rawHead)) {
|
|
108
|
-
const head =
|
|
108
|
+
const head = NAMESPACE_HEADS.get(rawHead);
|
|
109
109
|
if (head === undefined || tail.length === 0)
|
|
110
110
|
return [];
|
|
111
|
-
if (head.
|
|
111
|
+
if (head.dir === undefined)
|
|
112
112
|
return [{ scope: head.scope }];
|
|
113
113
|
const name = tail[0];
|
|
114
114
|
if (name && !/[*?]/.test(name))
|
|
115
115
|
return [{ scope: head.scope, who: assertWho(name, head.scope) }];
|
|
116
|
-
return enumerate(families.find((family) => family.
|
|
116
|
+
return enumerate(families.find((family) => family.dir === head.dir), false);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
// Unreserved patterns see the full universe, not just the current identities.
|
|
120
|
-
const
|
|
120
|
+
const namespaces = [];
|
|
121
121
|
for (const scope of SCOPE_ORDER) {
|
|
122
122
|
if (scope === "human")
|
|
123
|
-
|
|
123
|
+
namespaces.push({ scope });
|
|
124
124
|
else
|
|
125
|
-
|
|
125
|
+
namespaces.push(...await enumerate(families.find((family) => family.scope === scope), true));
|
|
126
126
|
}
|
|
127
|
-
return
|
|
127
|
+
return namespaces;
|
|
128
128
|
}
|
|
129
129
|
/** Relative pattern heads resolve to canonical names, so they match canonical addresses. */
|
|
130
130
|
function normalizePattern(pattern, identity) {
|
|
@@ -218,11 +218,11 @@ function assertSerializedSize(content) {
|
|
|
218
218
|
function frontmatterOf(meta) {
|
|
219
219
|
return serializeNote(meta, "").slice(0, -2);
|
|
220
220
|
}
|
|
221
|
-
/** Explicit session/project
|
|
222
|
-
function
|
|
221
|
+
/** Explicit session/project namespaces are read-only unless dreamed over; agent/model namespaces allow their owner. */
|
|
222
|
+
function assertWritableNamespace(scope, who, identity) {
|
|
223
223
|
if (who === undefined)
|
|
224
224
|
return;
|
|
225
|
-
// A dreamed project is writable whole;
|
|
225
|
+
// A dreamed project is writable whole; project and session ownership is checked per note by assertDreamNote.
|
|
226
226
|
if (identity.dreaming !== undefined && (scope === "session" || (scope === "project" && who === identity.dreaming)))
|
|
227
227
|
return;
|
|
228
228
|
if (scope === "session")
|
|
@@ -232,16 +232,19 @@ function assertWritableHome(scope, who, identity) {
|
|
|
232
232
|
const current = scope === "agent" ? identity.agent : identity.model;
|
|
233
233
|
if (who === current)
|
|
234
234
|
return;
|
|
235
|
-
const
|
|
236
|
-
throw new NoteError("invalid_scope", `${
|
|
235
|
+
const address = scope === "agent" ? `@agents/${who}/` : `@models/${who}/`;
|
|
236
|
+
throw new NoteError("invalid_scope", `${address} is someone else's notebook; you can write to this session, @project/, @human/, @self/, or @model/.`);
|
|
237
237
|
}
|
|
238
|
-
/**
|
|
239
|
-
function
|
|
240
|
-
if (scope !== "session"
|
|
238
|
+
/** Resolve aliases before checking that a dreamed note belongs to the open project. */
|
|
239
|
+
function assertDreamNote(scope, who, identity, raw, address) {
|
|
240
|
+
if (identity.dreaming === undefined || (scope !== "session" && scope !== "project"))
|
|
241
241
|
return;
|
|
242
|
-
|
|
242
|
+
const project = scope === "project" ? who ?? identity.projectKey
|
|
243
|
+
: raw !== undefined ? parseNote(raw).meta.project
|
|
244
|
+
: who === undefined ? identity.projectKey : undefined;
|
|
245
|
+
if (project === identity.dreaming)
|
|
243
246
|
return;
|
|
244
|
-
throw new NoteError("invalid_scope", raw === undefined
|
|
247
|
+
throw new NoteError("invalid_scope", raw === undefined && scope === "session" && who !== undefined
|
|
245
248
|
? `${address}: a dream edits other sessions' existing notes, it doesn't create new ones there.`
|
|
246
249
|
: `${address} belongs to another project, not the one being dreamed over (${identity.dreaming}).`);
|
|
247
250
|
}
|
|
@@ -253,7 +256,7 @@ export function createNotesStore(input) {
|
|
|
253
256
|
const destination = assertAddress(address);
|
|
254
257
|
assertWritablePath(destination.path);
|
|
255
258
|
const scope = destination.scope;
|
|
256
|
-
|
|
259
|
+
assertWritableNamespace(scope, destination.who, identity);
|
|
257
260
|
// Absent means "keep whatever the note already says": replacing a note must not silently
|
|
258
261
|
// re-attribute a user-written note to the agent that happened to rewrite it.
|
|
259
262
|
const origin = stableOptions.origin === undefined ? undefined : assertOrigin(stableOptions.origin);
|
|
@@ -262,7 +265,7 @@ export function createNotesStore(input) {
|
|
|
262
265
|
const now = writeStamp();
|
|
263
266
|
const cleanBody = stripLeadingFrontmatter(content);
|
|
264
267
|
const existingRaw = await readFileIfExists(path);
|
|
265
|
-
|
|
268
|
+
assertDreamNote(scope, destination.who, identity, existingRaw, address);
|
|
266
269
|
const existing = existingRaw === undefined ? undefined : parseNote(existingRaw, now).meta;
|
|
267
270
|
const outcome = existing === undefined ? "created" : existing.crumpledAt === undefined ? "overwrote" : "uncrumpled";
|
|
268
271
|
const meta = existing ?? {
|
|
@@ -295,7 +298,7 @@ export function createNotesStore(input) {
|
|
|
295
298
|
return undefined;
|
|
296
299
|
const now = Date.now();
|
|
297
300
|
const parsed = parseNote(raw, now);
|
|
298
|
-
// Named
|
|
301
|
+
// Named notebooks, and every read during a dream, leave no access stamp.
|
|
299
302
|
if (identity.dreaming !== undefined || ((scope === "project" || scope === "session") && destination.who !== undefined))
|
|
300
303
|
return { meta: { ...parsed.meta, scope }, body: parsed.body, resolvedScope: scope, accessedBefore: parsed.meta.lastAccessed };
|
|
301
304
|
// A read records its own access, exactly as a write records an edit.
|
|
@@ -311,8 +314,8 @@ export function createNotesStore(input) {
|
|
|
311
314
|
const destination = stableOptions.renameTo === undefined ? from : assertAddress(stableOptions.renameTo);
|
|
312
315
|
assertWritablePath(destination.path);
|
|
313
316
|
const scope = destination.scope;
|
|
314
|
-
|
|
315
|
-
|
|
317
|
+
assertWritableNamespace(from.scope, from.who, identity);
|
|
318
|
+
assertWritableNamespace(scope, destination.who, identity);
|
|
316
319
|
if (operations.length === 0 && stableOptions.origin === undefined && stableOptions.crumpled === undefined && stableOptions.renameTo === undefined) {
|
|
317
320
|
throw new NoteError("nothing_to_do", "Nothing to change: provide edits, an origin, a crumpled state, or a new address.");
|
|
318
321
|
}
|
|
@@ -324,9 +327,9 @@ export function createNotesStore(input) {
|
|
|
324
327
|
if (raw === undefined)
|
|
325
328
|
throw new NoteError("not_found", `No note at ${noteIdentity(identity, address).address}.`);
|
|
326
329
|
const targetRaw = differentPaths ? await readFileIfExists(path) : undefined;
|
|
327
|
-
|
|
330
|
+
assertDreamNote(from.scope, from.who, identity, raw, address);
|
|
328
331
|
if (differentPaths)
|
|
329
|
-
|
|
332
|
+
assertDreamNote(scope, destination.who, identity, targetRaw, stableOptions.renameTo);
|
|
330
333
|
// Case and symlink aliases must not turn write-then-remove into deleting the result.
|
|
331
334
|
const sourceReal = targetRaw === undefined ? undefined : await realpath(fromPath);
|
|
332
335
|
const sameFile = sourceReal !== undefined && sourceReal === await realpath(path);
|
|
@@ -410,13 +413,13 @@ export function createNotesStore(input) {
|
|
|
410
413
|
if (identity.dreaming === undefined)
|
|
411
414
|
throw new NoteError("invalid_scope", "Only a dream can delete notes; begin one first.");
|
|
412
415
|
const target = assertAddress(address);
|
|
413
|
-
|
|
416
|
+
assertWritableNamespace(target.scope, target.who, identity);
|
|
414
417
|
const path = physicalPath(target.scope, target.path, identity, target.who);
|
|
415
418
|
return withPathQueue(path, async () => {
|
|
416
419
|
const raw = await readFileIfExists(path);
|
|
417
420
|
if (raw === undefined)
|
|
418
421
|
throw new NoteError("not_found", `No note at ${noteIdentity(identity, address).address}.`);
|
|
419
|
-
|
|
422
|
+
assertDreamNote(target.scope, target.who, identity, raw, address);
|
|
420
423
|
const { meta } = parseNote(raw);
|
|
421
424
|
if (meta.crumpledAt === undefined)
|
|
422
425
|
throw new NoteError("not_crumpled", `${noteIdentity(identity, address).address} isn't crumpled; crumple it before deleting.`);
|
|
@@ -429,10 +432,10 @@ export function createNotesStore(input) {
|
|
|
429
432
|
const pattern = glob === "@default" ? undefined : glob;
|
|
430
433
|
const normalized = normalizePattern(pattern, identity);
|
|
431
434
|
const matcher = normalized === undefined ? undefined : globToRegExp(normalized);
|
|
432
|
-
const
|
|
433
|
-
for (const
|
|
434
|
-
const scope =
|
|
435
|
-
const root = scopeDir(scope, identity,
|
|
435
|
+
const namespaces = await namespacesForPattern(pattern, identity, status);
|
|
436
|
+
for (const namespace of namespaces) {
|
|
437
|
+
const scope = namespace.scope;
|
|
438
|
+
const root = scopeDir(scope, identity, namespace.who);
|
|
436
439
|
let paths;
|
|
437
440
|
try {
|
|
438
441
|
paths = await walkMarkdown(root);
|
|
@@ -440,24 +443,24 @@ export function createNotesStore(input) {
|
|
|
440
443
|
catch (error) {
|
|
441
444
|
if (!status || !isFilesystemError(error))
|
|
442
445
|
throw error;
|
|
443
|
-
status.
|
|
446
|
+
status.namespacesUnavailable.push(addressFor(identity, scope, "", namespace.who).replace(/\/$/, "") || "session");
|
|
444
447
|
continue;
|
|
445
448
|
}
|
|
446
449
|
for (const path of paths) {
|
|
447
|
-
if (matcher && !matcher.test(canonicalAddressFor(identity, scope, path,
|
|
448
|
-
&& !(scope === "session" &&
|
|
450
|
+
if (matcher && !matcher.test(canonicalAddressFor(identity, scope, path, namespace.who))
|
|
451
|
+
&& !(scope === "session" && namespace.who === undefined && !pattern.startsWith("@") && matcher.test(path)))
|
|
449
452
|
continue;
|
|
450
|
-
const address = addressFor(identity, scope, path,
|
|
453
|
+
const address = addressFor(identity, scope, path, namespace.who);
|
|
451
454
|
const fullPath = join(root, path);
|
|
452
455
|
let raw;
|
|
453
456
|
try {
|
|
454
457
|
raw = await withPathQueue(fullPath, () => readFile(fullPath, "utf8"));
|
|
455
458
|
}
|
|
456
459
|
catch (error) {
|
|
457
|
-
// One unreadable note is named on its own; its
|
|
460
|
+
// One unreadable note is named on its own; its notebook's readable notes still list.
|
|
458
461
|
if (!status || !isFilesystemError(error))
|
|
459
462
|
throw error;
|
|
460
|
-
status.
|
|
463
|
+
status.namespacesUnavailable.push(address);
|
|
461
464
|
continue;
|
|
462
465
|
}
|
|
463
466
|
const { meta, body } = parseNote(raw);
|
|
@@ -478,10 +481,10 @@ export function createNotesStore(input) {
|
|
|
478
481
|
rows.push({ ...row, sizeBytes: Buffer.byteLength(row.body, "utf8") });
|
|
479
482
|
}
|
|
480
483
|
rows.sort((a, b) => b.meta.updatedAt - a.meta.updatedAt || a.address.localeCompare(b.address));
|
|
481
|
-
return { rows, status: status ?? {
|
|
484
|
+
return { rows, status: status ?? { namespacesUnavailable: [] } };
|
|
482
485
|
}
|
|
483
486
|
function listWithStatus(options = {}) {
|
|
484
|
-
return listRows(options, {
|
|
487
|
+
return listRows(options, { namespacesUnavailable: [] });
|
|
485
488
|
}
|
|
486
489
|
async function search(queries, options = {}) {
|
|
487
490
|
return (await searchRows(queries, options)).rows;
|
|
@@ -511,10 +514,10 @@ export function createNotesStore(input) {
|
|
|
511
514
|
rows.push({ address, path, scope, meta, matches });
|
|
512
515
|
}
|
|
513
516
|
rows.sort((a, b) => a.address.localeCompare(b.address));
|
|
514
|
-
return { rows, status: status ?? {
|
|
517
|
+
return { rows, status: status ?? { namespacesUnavailable: [] } };
|
|
515
518
|
}
|
|
516
519
|
function searchWithStatus(queries, options = {}) {
|
|
517
|
-
return searchRows(queries, options, {
|
|
520
|
+
return searchRows(queries, options, { namespacesUnavailable: [] });
|
|
518
521
|
}
|
|
519
522
|
return { write, read, update, rename, forget, list, search, listWithStatus, searchWithStatus };
|
|
520
523
|
}
|
|
@@ -4,9 +4,16 @@ import { type NotesIdentity } from "../notes/index.js";
|
|
|
4
4
|
import { type DreamLock } from "../dream/lock.js";
|
|
5
5
|
import { type Outcome } from "./result.js";
|
|
6
6
|
/** The one open dream per session; the host owns where it lives. */
|
|
7
|
+
type DreamOpening = {
|
|
8
|
+
project: string;
|
|
9
|
+
lock: DreamLock;
|
|
10
|
+
generation: number;
|
|
11
|
+
};
|
|
7
12
|
export type DreamState = {
|
|
8
13
|
project?: string;
|
|
9
14
|
lock?: DreamLock;
|
|
15
|
+
pending?: DreamOpening;
|
|
16
|
+
generation?: number;
|
|
10
17
|
};
|
|
11
18
|
/** Drop an open dream without an end commit, e.g. when its session shuts down. */
|
|
12
19
|
export declare function abandonDream(state: DreamState): void;
|
|
@@ -17,7 +24,7 @@ export declare const dreamBeginParameters: Type.TObject<{
|
|
|
17
24
|
}>;
|
|
18
25
|
export declare const DreamBeginDataSchema: Type.TObject<{
|
|
19
26
|
more: Type.TInteger;
|
|
20
|
-
|
|
27
|
+
namespaces_unavailable: Type.TArray<Type.TString>;
|
|
21
28
|
files: Type.TArray<Type.TObject<{
|
|
22
29
|
updated_at: Type.TString;
|
|
23
30
|
last_accessed: Type.TString;
|
|
@@ -37,13 +44,13 @@ export type DreamBeginData = Static<typeof DreamBeginDataSchema>;
|
|
|
37
44
|
export declare const dreamBegin: {
|
|
38
45
|
readonly name: "dream_begin";
|
|
39
46
|
readonly label: "Dream begin";
|
|
40
|
-
readonly description: "Dream: open one project's notes for consolidation, one project at a time. Lists its @projects/<name>/ notes and every session note born in it, crumpled ones included. Until dream_end, notes_write/notes_update can edit those addresses and reads leave no access stamp.
|
|
47
|
+
readonly description: "Dream: open one project's notes for consolidation, one project at a time. Lists its @projects/<name>/ notes and every session note born in it, crumpled ones included. Until dream_end, notes_write/notes_update can edit those addresses and reads leave no access stamp. Holds the notes home's dream lock through dream_end so Git audits cannot interleave, and commits a baseline of the notes home.";
|
|
41
48
|
readonly parameters: Type.TObject<{
|
|
42
49
|
project: Type.TOptional<Type.TString>;
|
|
43
50
|
}>;
|
|
44
51
|
readonly outputSchema: Type.TUnion<[Type.TObject<{
|
|
45
52
|
more: Type.TInteger;
|
|
46
|
-
|
|
53
|
+
namespaces_unavailable: Type.TArray<Type.TString>;
|
|
47
54
|
files: Type.TArray<Type.TObject<{
|
|
48
55
|
updated_at: Type.TString;
|
|
49
56
|
last_accessed: Type.TString;
|
|
@@ -69,6 +76,11 @@ export declare const dreamForgetParameters: Type.TObject<{
|
|
|
69
76
|
address: Type.TString;
|
|
70
77
|
}>;
|
|
71
78
|
export declare const DreamForgetDataSchema: Type.TObject<{
|
|
79
|
+
project_root: Type.TOptional<Type.TString>;
|
|
80
|
+
project_siblings: Type.TOptional<Type.TArray<Type.TObject<{
|
|
81
|
+
name: Type.TString;
|
|
82
|
+
path: Type.TString;
|
|
83
|
+
}>>>;
|
|
72
84
|
address: Type.TString;
|
|
73
85
|
project_key: Type.TOptional<Type.TString>;
|
|
74
86
|
}>;
|
|
@@ -81,6 +93,11 @@ export declare const dreamForget: {
|
|
|
81
93
|
address: Type.TString;
|
|
82
94
|
}>;
|
|
83
95
|
readonly outputSchema: Type.TUnion<[Type.TObject<{
|
|
96
|
+
project_root: Type.TOptional<Type.TString>;
|
|
97
|
+
project_siblings: Type.TOptional<Type.TArray<Type.TObject<{
|
|
98
|
+
name: Type.TString;
|
|
99
|
+
path: Type.TString;
|
|
100
|
+
}>>>;
|
|
84
101
|
address: Type.TString;
|
|
85
102
|
project_key: Type.TOptional<Type.TString>;
|
|
86
103
|
}>, Type.TObject<{
|
|
@@ -113,3 +130,4 @@ export declare const dreamEnd: {
|
|
|
113
130
|
readonly execute: (_params: Static<typeof dreamEndParameters>, identity: NotesIdentity, state: DreamState) => Promise<Outcome<DreamEndData>>;
|
|
114
131
|
readonly render: (result: Outcome<DreamEndData>) => string;
|
|
115
132
|
};
|
|
133
|
+
export {};
|
package/dist/src/tools/dream.js
CHANGED
|
@@ -1,17 +1,40 @@
|
|
|
1
1
|
/** Dream operations: open one project's notes for consolidation, delete crumpled notes, close. */
|
|
2
2
|
import { Type } from "typebox";
|
|
3
3
|
import { createNotesStore, noteIdentity } from "../notes/index.js";
|
|
4
|
-
import {
|
|
4
|
+
import { projectNamespaces } from "../notes/project-namespace.js";
|
|
5
5
|
import { gitCommit } from "../dream/git.js";
|
|
6
6
|
import { acquireDreamLock, dreamLockPath, releaseDreamLock } from "../dream/lock.js";
|
|
7
7
|
import { failure, renderOutcome, resultSchema, success } from "./result.js";
|
|
8
|
-
import { failureOf, listedFile, NotesListDataSchema, renderListData, snapshot } from "./notes.js";
|
|
8
|
+
import { failureOf, listedFile, NoteIdentitySchema, NotesListDataSchema, renderListData, snapshot } from "./notes.js";
|
|
9
|
+
function currentOpening(state, opening) {
|
|
10
|
+
return state.pending === opening && state.generation === opening.generation;
|
|
11
|
+
}
|
|
12
|
+
function nextGeneration(state) {
|
|
13
|
+
const generation = (state.generation ?? 0) + 1;
|
|
14
|
+
state.generation = generation;
|
|
15
|
+
return generation;
|
|
16
|
+
}
|
|
9
17
|
/** Drop an open dream without an end commit, e.g. when its session shuts down. */
|
|
10
18
|
export function abandonDream(state) {
|
|
11
|
-
|
|
19
|
+
const pending = state.pending;
|
|
20
|
+
if (pending)
|
|
21
|
+
releaseDreamLock(pending.lock);
|
|
22
|
+
if (state.lock && state.lock !== pending?.lock)
|
|
12
23
|
releaseDreamLock(state.lock);
|
|
24
|
+
nextGeneration(state);
|
|
13
25
|
delete state.project;
|
|
14
26
|
delete state.lock;
|
|
27
|
+
delete state.pending;
|
|
28
|
+
}
|
|
29
|
+
/** Release an opening without disturbing a newer opening in the same state. */
|
|
30
|
+
function releaseOpening(state, opening) {
|
|
31
|
+
releaseDreamLock(opening.lock);
|
|
32
|
+
if (currentOpening(state, opening))
|
|
33
|
+
delete state.pending;
|
|
34
|
+
}
|
|
35
|
+
function cancelledOpening(state, opening) {
|
|
36
|
+
releaseOpening(state, opening);
|
|
37
|
+
return failure("cancelled", "The dream opening was cancelled before it completed.");
|
|
15
38
|
}
|
|
16
39
|
/** The identity a store uses while `state` holds an open dream. */
|
|
17
40
|
export function dreamingIdentity(identity, state) {
|
|
@@ -27,18 +50,23 @@ function renderBeginData(data) {
|
|
|
27
50
|
}
|
|
28
51
|
export const dreamBegin = {
|
|
29
52
|
name: "dream_begin", label: "Dream begin",
|
|
30
|
-
description: "Dream: open one project's notes for consolidation, one project at a time. Lists its @projects/<name>/ notes and every session note born in it, crumpled ones included. Until dream_end, notes_write/notes_update can edit those addresses and reads leave no access stamp.
|
|
53
|
+
description: "Dream: open one project's notes for consolidation, one project at a time. Lists its @projects/<name>/ notes and every session note born in it, crumpled ones included. Until dream_end, notes_write/notes_update can edit those addresses and reads leave no access stamp. Holds the notes home's dream lock through dream_end so Git audits cannot interleave, and commits a baseline of the notes home.",
|
|
31
54
|
parameters: dreamBeginParameters, outputSchema: resultSchema(DreamBeginDataSchema),
|
|
32
55
|
async execute(params, identity, state) {
|
|
33
|
-
if (state.project !== undefined)
|
|
34
|
-
|
|
56
|
+
if (state.project !== undefined || state.pending !== undefined) {
|
|
57
|
+
const project = state.project ?? state.pending?.project;
|
|
58
|
+
return failure("already_dreaming", `Already dreaming over ${project}; call dream_end before opening another project.`);
|
|
59
|
+
}
|
|
35
60
|
const project = params.project ?? identity.projectKey;
|
|
36
|
-
if (project !== identity.projectKey && !
|
|
61
|
+
if (project !== identity.projectKey && !projectNamespaces(identity.home).some(namespace => namespace.name === project)) {
|
|
37
62
|
return failure("not_found", `No project named ${JSON.stringify(project)}; notes_list with glob @projects/*/** shows the names.`);
|
|
38
63
|
}
|
|
39
|
-
const lock = acquireDreamLock(dreamLockPath(identity.home
|
|
64
|
+
const lock = acquireDreamLock(dreamLockPath(identity.home));
|
|
40
65
|
if (!lock)
|
|
41
|
-
return failure("already_dreaming", `Another dream holds ${dreamLockPath(identity.home
|
|
66
|
+
return failure("already_dreaming", `Another dream holds ${dreamLockPath(identity.home)}. If none is running, delete that file by hand.`);
|
|
67
|
+
// Publish ownership before the first await. Shutdown can therefore release this exact token.
|
|
68
|
+
const opening = { project, lock, generation: nextGeneration(state) };
|
|
69
|
+
state.pending = opening;
|
|
42
70
|
try {
|
|
43
71
|
const store = createNotesStore({ ...identity, dreaming: project });
|
|
44
72
|
const rows = [];
|
|
@@ -46,33 +74,43 @@ export const dreamBegin = {
|
|
|
46
74
|
for (const crumpled of [false, true]) {
|
|
47
75
|
for (const glob of [`@projects/${project}/**`, "@sessions/*/**"]) {
|
|
48
76
|
const result = await store.listWithStatus({ glob, crumpled });
|
|
77
|
+
if (!currentOpening(state, opening))
|
|
78
|
+
return cancelledOpening(state, opening);
|
|
49
79
|
rows.push(...result.rows.filter(row => row.scope === "project" || row.meta.project === project));
|
|
50
|
-
unavailable.push(...result.status.
|
|
80
|
+
unavailable.push(...result.status.namespacesUnavailable);
|
|
51
81
|
}
|
|
52
82
|
}
|
|
53
83
|
rows.sort((a, b) => b.meta.updatedAt - a.meta.updatedAt || a.address.localeCompare(b.address));
|
|
54
|
-
const listed = snapshot(rows.map(row => listedFile(row, identity)), undefined, {
|
|
84
|
+
const listed = snapshot(rows.map(row => listedFile(row, identity)), undefined, { namespacesUnavailable: unavailable }, renderListData);
|
|
55
85
|
if (!listed.ok) {
|
|
56
|
-
|
|
86
|
+
releaseOpening(state, opening);
|
|
57
87
|
return listed;
|
|
58
88
|
}
|
|
89
|
+
if (!currentOpening(state, opening))
|
|
90
|
+
return cancelledOpening(state, opening);
|
|
59
91
|
const baseline = gitCommit(identity.home, `dream baseline ${project}`);
|
|
60
92
|
if (!baseline.ok) {
|
|
61
|
-
|
|
93
|
+
releaseOpening(state, opening);
|
|
62
94
|
return failure("audit_failed", `Couldn't commit the Git baseline, so nothing could be rolled back; no dream was opened: ${baseline.error}`);
|
|
63
95
|
}
|
|
64
|
-
|
|
96
|
+
if (!currentOpening(state, opening))
|
|
97
|
+
return cancelledOpening(state, opening);
|
|
98
|
+
state.project = opening.project;
|
|
99
|
+
state.lock = opening.lock;
|
|
100
|
+
delete state.pending;
|
|
65
101
|
return success({ project, baseline: baseline.commit, ...listed.data });
|
|
66
102
|
}
|
|
67
103
|
catch (error) {
|
|
68
|
-
|
|
104
|
+
if (!currentOpening(state, opening))
|
|
105
|
+
return cancelledOpening(state, opening);
|
|
106
|
+
releaseOpening(state, opening);
|
|
69
107
|
return failureOf(error);
|
|
70
108
|
}
|
|
71
109
|
},
|
|
72
110
|
render: (result) => renderOutcome(result, renderBeginData),
|
|
73
111
|
};
|
|
74
112
|
export const dreamForgetParameters = Type.Object({ address: Type.String() }, { additionalProperties: false });
|
|
75
|
-
export const DreamForgetDataSchema = Type.Object({
|
|
113
|
+
export const DreamForgetDataSchema = Type.Object({ ...NoteIdentitySchema }, { additionalProperties: false });
|
|
76
114
|
export const dreamForget = {
|
|
77
115
|
name: "dream_forget", label: "Dream forget",
|
|
78
116
|
description: "Dream: permanently delete one crumpled note in the project being dreamed over, or in @human/, @self/, @model/. Crumple it first; live notes refuse.",
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type NotesIdentity } from "../notes/index.js";
|
|
2
|
+
/** Host-supplied facts for one notes tool registration; overrides never mutate this context. */
|
|
3
|
+
export type NotesToolContext = Readonly<{
|
|
4
|
+
identity: NotesIdentity;
|
|
5
|
+
cwd: string;
|
|
6
|
+
}>;
|
|
7
|
+
/** Capture the host's base cwd alongside its default identity. */
|
|
8
|
+
export declare function notesToolContext(identity: NotesIdentity, cwd: string): NotesToolContext;
|
|
9
|
+
/** Resolve the current-project override once, before any address, receipt, or ownership decision. */
|
|
10
|
+
export declare function notesIdentityForCall(context: NotesToolContext, cwd?: string): NotesIdentity;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { NoteError } from "../notes/errors.js";
|
|
4
|
+
import { projectKey, snapshotNotesIdentity } from "../notes/index.js";
|
|
5
|
+
/** Capture the host's base cwd alongside its default identity. */
|
|
6
|
+
export function notesToolContext(identity, cwd) {
|
|
7
|
+
if (typeof cwd !== "string" || cwd.length === 0)
|
|
8
|
+
throw new TypeError("notes tool cwd must be a non-empty string");
|
|
9
|
+
return Object.freeze({ identity: snapshotNotesIdentity(identity), cwd: resolve(cwd) });
|
|
10
|
+
}
|
|
11
|
+
/** Resolve the current-project override once, before any address, receipt, or ownership decision. */
|
|
12
|
+
export function notesIdentityForCall(context, cwd) {
|
|
13
|
+
if (cwd === undefined)
|
|
14
|
+
return context.identity;
|
|
15
|
+
const resolved = resolve(context.cwd, cwd);
|
|
16
|
+
let directory = false;
|
|
17
|
+
try {
|
|
18
|
+
directory = statSync(resolved).isDirectory();
|
|
19
|
+
}
|
|
20
|
+
catch { /* Report one typed refusal below. */ }
|
|
21
|
+
if (!directory)
|
|
22
|
+
throw new NoteError("invalid_cwd", `cwd is not an existing directory: ${JSON.stringify(cwd)} (resolved to ${resolved}).`);
|
|
23
|
+
return snapshotNotesIdentity({
|
|
24
|
+
...context.identity,
|
|
25
|
+
projectKey: projectKey(resolved, context.identity.home),
|
|
26
|
+
});
|
|
27
|
+
}
|