notesoup 0.53.2 → 0.54.1

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.
Files changed (186) hide show
  1. package/README.md +6 -4
  2. package/dist/plugins/claude/src/helper.d.ts +2 -1
  3. package/dist/plugins/claude/src/helper.js +21 -24
  4. package/dist/plugins/claude/src/history.d.ts +3 -14
  5. package/dist/plugins/claude/src/history.js +2 -298
  6. package/dist/plugins/claude/src/text.d.ts +0 -1
  7. package/dist/plugins/claude/src/text.js +0 -1
  8. package/dist/plugins/codex/src/history.d.ts +3 -8
  9. package/dist/plugins/codex/src/history.js +2 -238
  10. package/dist/plugins/codex/src/hooks.js +1 -1
  11. package/dist/plugins/codex/src/identity.d.ts +1 -1
  12. package/dist/plugins/codex/src/identity.js +3 -3
  13. package/dist/plugins/codex/src/mcp.js +9 -8
  14. package/dist/plugins/codex/src/text.d.ts +1 -1
  15. package/dist/plugins/codex/src/text.js +2 -2
  16. package/dist/plugins/codex/src/transcript.d.ts +3 -39
  17. package/dist/plugins/codex/src/transcript.js +2 -313
  18. package/dist/plugins/dsh/engine.js +2 -2
  19. package/dist/plugins/dsh/history.d.ts +3 -0
  20. package/dist/plugins/dsh/history.js +15 -0
  21. package/dist/plugins/dsh/tools.js +12 -12
  22. package/dist/plugins/dsh/window.d.ts +1 -0
  23. package/dist/plugins/dsh/window.js +7 -3
  24. package/dist/plugins/pi/history-tools.d.ts +1 -1
  25. package/dist/plugins/pi/history-tools.js +9 -5
  26. package/dist/plugins/pi/history.d.ts +2 -3
  27. package/dist/plugins/pi/history.js +4 -147
  28. package/dist/plugins/pi/notes/tools.js +8 -6
  29. package/dist/plugins/pi/render.js +7 -4
  30. package/dist/plugins/pi/runtime.js +2 -2
  31. package/dist/plugins/pi/window.js +21 -7
  32. package/dist/src/boot/render.js +9 -11
  33. package/dist/src/boot/snapshot.d.ts +7 -7
  34. package/dist/src/boot/snapshot.js +17 -17
  35. package/dist/src/boot/text.d.ts +5 -3
  36. package/dist/src/boot/text.js +18 -10
  37. package/dist/src/dream/git.d.ts +2 -11
  38. package/dist/src/dream/git.js +6 -23
  39. package/dist/src/dream/lock.d.ts +2 -2
  40. package/dist/src/dream/lock.js +3 -3
  41. package/dist/src/history/claude.d.ts +16 -0
  42. package/dist/src/history/claude.js +336 -0
  43. package/dist/src/history/codex-transcript.d.ts +39 -0
  44. package/dist/src/history/codex-transcript.js +315 -0
  45. package/dist/src/history/codex.d.ts +8 -0
  46. package/dist/src/history/codex.js +238 -0
  47. package/dist/src/history/corpus.d.ts +35 -0
  48. package/dist/src/history/corpus.js +54 -0
  49. package/dist/src/history/history.d.ts +4 -8
  50. package/dist/src/history/history.js +3 -11
  51. package/dist/src/history/local.d.ts +17 -0
  52. package/dist/src/history/local.js +101 -0
  53. package/dist/src/history/pi.d.ts +36 -0
  54. package/dist/src/history/pi.js +202 -0
  55. package/dist/src/history/query.d.ts +15 -5
  56. package/dist/src/history/query.js +42 -13
  57. package/dist/src/index.d.ts +6 -3
  58. package/dist/src/index.js +6 -4
  59. package/dist/src/notes/address.d.ts +8 -8
  60. package/dist/src/notes/address.js +13 -13
  61. package/dist/src/notes/errors.d.ts +1 -1
  62. package/dist/src/notes/frontmatter.js +2 -2
  63. package/dist/src/notes/index.d.ts +1 -1
  64. package/dist/src/notes/index.js +1 -1
  65. package/dist/src/notes/paths.d.ts +6 -6
  66. package/dist/src/notes/paths.js +9 -9
  67. package/dist/src/notes/{project-home.d.ts → project-namespace.d.ts} +7 -7
  68. package/dist/src/notes/{project-home.js → project-namespace.js} +67 -25
  69. package/dist/src/notes/store.d.ts +4 -4
  70. package/dist/src/notes/store.js +55 -52
  71. package/dist/src/tools/dream.d.ts +21 -3
  72. package/dist/src/tools/dream.js +54 -16
  73. package/dist/src/tools/history.d.ts +7 -19
  74. package/dist/src/tools/history.js +80 -93
  75. package/dist/src/tools/notes-context.d.ts +10 -0
  76. package/dist/src/tools/notes-context.js +27 -0
  77. package/dist/src/tools/notes.d.ts +69 -20
  78. package/dist/src/tools/notes.js +40 -31
  79. package/dist/test/boot.integration.test.js +2 -3
  80. package/dist/test/claude-history.test.js +47 -34
  81. package/dist/test/claude-mod.test.js +96 -50
  82. package/dist/test/codex-history.test.js +4 -10
  83. package/dist/test/codex-plugin.test.js +50 -12
  84. package/dist/test/context-edit-budget.test.js +121 -0
  85. package/dist/test/dependencies.test.js +73 -6
  86. package/dist/test/dream-tools.test.js +153 -8
  87. package/dist/test/dsh.test.js +88 -19
  88. package/dist/test/helpers/dsh.d.ts +4 -1
  89. package/dist/test/helpers/dsh.js +20 -4
  90. package/dist/test/helpers/extension.d.ts +5 -1
  91. package/dist/test/helpers/extension.js +33 -2
  92. package/dist/test/helpers/history-corpus.d.ts +7 -0
  93. package/dist/test/helpers/history-corpus.js +26 -0
  94. package/dist/test/history-corpus.test.js +307 -0
  95. package/dist/test/history.integration.test.js +40 -48
  96. package/dist/test/host-neutral.test.js +20 -33
  97. package/dist/test/model-facing-text.test.js +14 -8
  98. package/dist/test/notes-cwd.test.d.ts +1 -0
  99. package/dist/test/notes-cwd.test.js +114 -0
  100. package/dist/test/notes-library.test.js +4 -4
  101. package/dist/test/notes-query-namespaces.test.d.ts +1 -0
  102. package/dist/test/{notes-query-homes.test.js → notes-query-namespaces.test.js} +13 -13
  103. package/dist/test/notes.integration.test.js +73 -24
  104. package/dist/test/notes.test.js +51 -21
  105. package/dist/test/pi-render.test.js +33 -1
  106. package/dist/test/project-namespace.test.d.ts +1 -0
  107. package/dist/test/project-namespace.test.js +324 -0
  108. package/docs/architecture.md +19 -12
  109. package/docs/claude-code-mod.md +7 -5
  110. package/docs/codex.md +6 -6
  111. package/docs/dsh.md +9 -9
  112. package/docs/history-sessions.md +52 -0
  113. package/docs/notes-library.md +9 -5
  114. package/docs/skills.md +3 -3
  115. package/docs/tool-results.md +14 -8
  116. package/package.json +4 -6
  117. package/playbook.md +6 -6
  118. package/plugins/claude/.claude-plugin/plugin.json +1 -1
  119. package/plugins/claude/dist/claude/helper.js +2164 -1341
  120. package/plugins/claude/hooks/register.ts +19 -11
  121. package/plugins/claude/playbook.md +6 -6
  122. package/plugins/claude/skills/memory/SKILL.md +9 -7
  123. package/plugins/claude/src/helper.ts +22 -25
  124. package/plugins/claude/src/history.ts +3 -257
  125. package/plugins/claude/src/text.ts +0 -1
  126. package/plugins/codex/README.md +1 -1
  127. package/plugins/codex/dist/codex/notesoup.js +1582 -969
  128. package/plugins/codex/src/history.ts +3 -193
  129. package/plugins/codex/src/hooks.ts +1 -1
  130. package/plugins/codex/src/identity.ts +3 -3
  131. package/plugins/codex/src/mcp.ts +9 -8
  132. package/plugins/codex/src/text.ts +2 -2
  133. package/plugins/codex/src/transcript.ts +3 -275
  134. package/plugins/dsh/engine.ts +2 -2
  135. package/plugins/dsh/history.ts +17 -0
  136. package/plugins/dsh/tools.ts +12 -12
  137. package/plugins/dsh/window.ts +8 -3
  138. package/plugins/paseo/OVERVIEW.md +4 -4
  139. package/plugins/paseo/README.md +1 -1
  140. package/plugins/paseo/client/brain-empty-card.tsx +3 -4
  141. package/plugins/paseo/client/markdown.tsx +159 -0
  142. package/plugins/paseo/client/notes-op-card.tsx +35 -34
  143. package/plugins/paseo/client/notes-op-transformer.ts +17 -68
  144. package/plugins/paseo/client/paper-card.tsx +8 -19
  145. package/plugins/paseo/client/paper-palette.ts +40 -0
  146. package/plugins/paseo/client/sticky-card.tsx +6 -4
  147. package/plugins/paseo/package.json +3 -0
  148. package/plugins/paseo/paseo-plugin.json +2 -2
  149. package/plugins/paseo/shared/notes-op.ts +13 -9
  150. package/plugins/paseo/test/notes-op.test.ts +52 -73
  151. package/plugins/paseo/test/paper-palette.test.ts +54 -0
  152. package/plugins/pi/history-tools.ts +11 -5
  153. package/plugins/pi/history.ts +8 -157
  154. package/plugins/pi/notes/tools.ts +8 -6
  155. package/plugins/pi/render.ts +6 -4
  156. package/plugins/pi/runtime.ts +2 -2
  157. package/plugins/pi/window.ts +22 -7
  158. package/skills/memory/SKILL.md +9 -7
  159. package/src/boot/render.ts +10 -13
  160. package/src/boot/snapshot.ts +20 -20
  161. package/src/boot/text.ts +18 -10
  162. package/src/dream/git.ts +7 -24
  163. package/src/dream/lock.ts +3 -3
  164. package/src/history/claude.ts +286 -0
  165. package/src/history/codex-transcript.ts +276 -0
  166. package/src/history/codex.ts +193 -0
  167. package/src/history/corpus.ts +69 -0
  168. package/src/history/history.ts +5 -12
  169. package/src/history/local.ts +93 -0
  170. package/src/history/pi.ts +199 -0
  171. package/src/history/query.ts +45 -15
  172. package/src/index.ts +8 -5
  173. package/src/notes/address.ts +13 -13
  174. package/src/notes/errors.ts +1 -1
  175. package/src/notes/frontmatter.ts +2 -2
  176. package/src/notes/index.ts +1 -1
  177. package/src/notes/paths.ts +9 -9
  178. package/src/notes/{project-home.ts → project-namespace.ts} +59 -28
  179. package/src/notes/store.ts +61 -58
  180. package/src/tools/dream.ts +63 -16
  181. package/src/tools/history.ts +71 -101
  182. package/src/tools/notes-context.ts +29 -0
  183. package/src/tools/notes.ts +39 -29
  184. package/dist/test/project-home.test.js +0 -134
  185. /package/dist/test/{notes-query-homes.test.d.ts → context-edit-budget.test.d.ts} +0 -0
  186. /package/dist/test/{project-home.test.d.ts → history-corpus.test.d.ts} +0 -0
@@ -1,7 +1,7 @@
1
1
  import { type NotesIdentity } from "./identity.js";
2
2
  import { type NoteMeta, type Origin } from "./frontmatter.js";
3
3
  import { type Scope } from "./paths.js";
4
- import { type ProjectReceipt } from "./project-home.js";
4
+ import { type ProjectReceipt } from "./project-namespace.js";
5
5
  export type { NoteMeta, Origin, Scope };
6
6
  export type NoteRow = {
7
7
  address: string;
@@ -36,7 +36,7 @@ export type EditOptions = {
36
36
  replaceAll?: boolean;
37
37
  renameTo?: string;
38
38
  };
39
- /** Omitted/null/empty glob or bare @default selects current homes; explicit globs match full addresses.
39
+ /** Omitted/null/empty glob or bare @default selects current notebooks; explicit globs match full addresses.
40
40
  * Only the current session also matches its bare-path alias (e.g. *.md or deep/**).
41
41
  */
42
42
  export type NotesQuery = {
@@ -76,9 +76,9 @@ export type NoteRenameResult = {
76
76
  meta: NoteMeta;
77
77
  replacedCrumpledTarget: boolean;
78
78
  };
79
- /** A home, or one unreadable note inside it, that a status-reporting query could not read. */
79
+ /** A notebook, or one unreadable note inside it, that a status-reporting query could not read. */
80
80
  export type NoteQueryStatus = {
81
- homesUnavailable: string[];
81
+ namespacesUnavailable: string[];
82
82
  };
83
83
  export type NoteQueryResult<T> = {
84
84
  rows: T[];
@@ -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, HOME_HEADS } from "./address.js";
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-home.js";
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 home that has never been created is normal. Other failures must reach the
64
- // boot snapshot boundary instead of masquerading as an empty home.
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 homesForPattern(pattern, identity, status) {
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", home: "sessions", current: identity.sessionId },
84
- { scope: "project", home: "project", current: identity.projectKey },
85
- { scope: "agent", home: "agents", current: identity.agent },
86
- { scope: "model", home: "models", current: identity.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.home, identity.home);
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.homesUnavailable.push(`@${family.home === "project" ? "projects" : family.home}`);
96
+ status.namespacesUnavailable.push(`@${family.dir === "project" ? "projects" : family.dir}`);
97
97
  return [];
98
98
  }
99
- // Include the current home even when missing or a non-directory, and only once.
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 = HOME_HEADS.get(rawHead);
108
+ const head = NAMESPACE_HEADS.get(rawHead);
109
109
  if (head === undefined || tail.length === 0)
110
110
  return [];
111
- if (head.home === undefined)
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.home === head.home), false);
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 homes = [];
120
+ const namespaces = [];
121
121
  for (const scope of SCOPE_ORDER) {
122
122
  if (scope === "human")
123
- homes.push({ scope });
123
+ namespaces.push({ scope });
124
124
  else
125
- homes.push(...await enumerate(families.find((family) => family.scope === scope), true));
125
+ namespaces.push(...await enumerate(families.find((family) => family.scope === scope), true));
126
126
  }
127
- return homes;
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 homes are read-only unless dreamed over; agent/model homes allow their owner. */
222
- function assertWritableHome(scope, who, identity) {
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; its other sessions are checked per note by assertDreamSessionNote.
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 home = scope === "agent" ? `@agents/${who}/` : `@models/${who}/`;
236
- throw new NoteError("invalid_scope", `${home} is someone else's home; you can write to this session, @project/, @human/, @self/, or @model/.`);
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
- /** A dream reaches another session's note only when that note was born in the dreamed project. */
239
- function assertDreamSessionNote(scope, who, identity, raw, address) {
240
- if (scope !== "session" || who === undefined || identity.dreaming === undefined)
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
- if (raw !== undefined && parseNote(raw).meta.project === identity.dreaming)
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
- assertWritableHome(scope, destination.who, identity);
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
- assertDreamSessionNote(scope, destination.who, identity, existingRaw, address);
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 homes, and every read during a dream, leave no access stamp.
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
- assertWritableHome(from.scope, from.who, identity);
315
- assertWritableHome(scope, destination.who, identity);
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
- assertDreamSessionNote(from.scope, from.who, identity, raw, address);
330
+ assertDreamNote(from.scope, from.who, identity, raw, address);
328
331
  if (differentPaths)
329
- assertDreamSessionNote(scope, destination.who, identity, targetRaw, stableOptions.renameTo);
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
- assertWritableHome(target.scope, target.who, identity);
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
- assertDreamSessionNote(target.scope, target.who, identity, raw, address);
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 homes = await homesForPattern(pattern, identity, status);
433
- for (const home of homes) {
434
- const scope = home.scope;
435
- const root = scopeDir(scope, identity, home.who);
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.homesUnavailable.push(addressFor(identity, scope, "", home.who).replace(/\/$/, "") || "session");
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, home.who))
448
- && !(scope === "session" && home.who === undefined && !pattern.startsWith("@") && matcher.test(path)))
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, home.who);
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 home's readable notes still list.
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.homesUnavailable.push(address);
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 ?? { homesUnavailable: [] } };
484
+ return { rows, status: status ?? { namespacesUnavailable: [] } };
482
485
  }
483
486
  function listWithStatus(options = {}) {
484
- return listRows(options, { homesUnavailable: [] });
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 ?? { homesUnavailable: [] } };
517
+ return { rows, status: status ?? { namespacesUnavailable: [] } };
515
518
  }
516
519
  function searchWithStatus(queries, options = {}) {
517
- return searchRows(queries, options, { homesUnavailable: [] });
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
- homes_unavailable: Type.TArray<Type.TString>;
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. Takes the project's dream lock and commits a Git baseline of the notes home.";
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
- homes_unavailable: Type.TArray<Type.TString>;
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 {};
@@ -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 { projectHomes } from "../notes/project-home.js";
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
- if (state.lock)
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. Takes the project's dream lock and commits a Git baseline of the notes home.",
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
- return failure("already_dreaming", `Already dreaming over ${state.project}; call dream_end before opening another project.`);
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 && !projectHomes(identity.home).some(home => home.name === project)) {
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, project));
64
+ const lock = acquireDreamLock(dreamLockPath(identity.home));
40
65
  if (!lock)
41
- return failure("already_dreaming", `Another dream holds ${dreamLockPath(identity.home, project)}. If none is running, delete that file by hand.`);
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.homesUnavailable);
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, { homesUnavailable: unavailable }, renderListData);
84
+ const listed = snapshot(rows.map(row => listedFile(row, identity)), undefined, { namespacesUnavailable: unavailable }, renderListData);
55
85
  if (!listed.ok) {
56
- releaseDreamLock(lock);
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
- releaseDreamLock(lock);
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
- Object.assign(state, { project, lock });
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
- releaseDreamLock(lock);
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({ address: Type.String(), project_key: Type.Optional(Type.String()) }, { additionalProperties: false });
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.",
@@ -2,7 +2,7 @@
2
2
  import { Type, type Static } from "typebox";
3
3
  import { type Operation } from "./result.js";
4
4
  import { type HistoryPage } from "../history/query.js";
5
- import { type HistoryProjection } from "../history/history.js";
5
+ import { type HistoryContext } from "../history/corpus.js";
6
6
  /** One read: identity, tool summary and a flat document slice. */
7
7
  declare const HistoryReadDataSchema: Type.TObject<{
8
8
  text: Type.TString;
@@ -16,29 +16,17 @@ declare const HistoryReadDataSchema: Type.TObject<{
16
16
  tool_status: Type.TOptional<Type.TUnion<[Type.TLiteral<"ok">, Type.TLiteral<"error">, Type.TLiteral<"unfinished">]>>;
17
17
  output_truncated: Type.TOptional<Type.TLiteral<true>>;
18
18
  full_output_path: Type.TOptional<Type.TString>;
19
+ session: Type.TOptional<Type.TString>;
20
+ notice: Type.TOptional<Type.TString>;
19
21
  seq: Type.TInteger;
20
- window_id: Type.TString;
21
22
  role: Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"context">]>;
22
23
  created_at: Type.TUnion<[Type.TString, Type.TNull]>;
23
24
  }>;
24
25
  type HistoryReadData = Static<typeof HistoryReadDataSchema>;
25
- declare const HistoryWindowsDataSchema: Type.TObject<{
26
- session_id: Type.TString;
27
- windows: Type.TArray<Type.TObject<{
28
- window_id: Type.TString;
29
- created_at: Type.TUnion<[Type.TString, Type.TNull]>;
30
- first_seq: Type.TUnion<[Type.TInteger, Type.TNull]>;
31
- last_seq: Type.TUnion<[Type.TInteger, Type.TNull]>;
32
- }>>;
33
- more: Type.TInteger;
34
- }>;
35
- type HistoryWindowsData = Static<typeof HistoryWindowsDataSchema>;
36
26
  /** Pure host decoration, measured during selection and applied unchanged by the caller at delivery. */
37
27
  export type HistoryTextWrapper = (text: string) => string;
38
- export declare const PREVIOUS_WINDOW = "@previous";
39
- export declare const historyWindowsParameters: Type.TObject<{}>;
40
- export declare const historyWindows: Operation<typeof historyWindowsParameters, HistoryWindowsData, [HistoryProjection, string, HistoryTextWrapper?]>;
41
28
  export declare const historyListParameters: Type.TObject<{
29
+ session: Type.TOptional<Type.TString>;
42
30
  query: Type.TOptional<Type.TUnion<[Type.TString, Type.TArray<Type.TString>]>>;
43
31
  roles: Type.TOptional<Type.TArray<Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"context">]>>>;
44
32
  before_seq: Type.TOptional<Type.TInteger>;
@@ -46,15 +34,15 @@ export declare const historyListParameters: Type.TObject<{
46
34
  since: Type.TOptional<Type.TString>;
47
35
  until: Type.TOptional<Type.TString>;
48
36
  from: Type.TOptional<Type.TUnion<[Type.TLiteral<"newest">, Type.TLiteral<"oldest">]>>;
49
- window_id: Type.TOptional<Type.TString>;
50
37
  limit: Type.TOptional<Type.TInteger>;
51
38
  max_chars_per_item: Type.TOptional<Type.TInteger>;
52
39
  }>;
53
- export declare const historyList: Operation<typeof historyListParameters, HistoryPage, [HistoryProjection, HistoryTextWrapper?]>;
40
+ export declare const historyList: Operation<typeof historyListParameters, HistoryPage, [HistoryContext, HistoryTextWrapper?]>;
54
41
  export declare const historyReadParameters: Type.TObject<{
55
42
  offset_chars: Type.TOptional<Type.TInteger>;
56
43
  max_chars: Type.TOptional<Type.TInteger>;
44
+ session: Type.TOptional<Type.TString>;
57
45
  seq: Type.TInteger;
58
46
  }>;
59
- export declare const historyRead: Operation<typeof historyReadParameters, HistoryReadData, [HistoryProjection, HistoryTextWrapper?]>;
47
+ export declare const historyRead: Operation<typeof historyReadParameters, HistoryReadData, [HistoryContext, HistoryTextWrapper?]>;
60
48
  export {};