@north-light/crouter 0.3.201 → 0.3.203

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.
@@ -1,44 +1,45 @@
1
1
  import { defineLeaf } from '../../core/command.js';
2
2
  import { usage } from '../../core/errors.js';
3
- import { parseFrontmatterGeneric } from '../../core/frontmatter.js';
4
- import { readText, writeText, pathExists } from '../../core/fs-utils.js';
5
- import { MEMORY_KINDS, MEMORY_SCOPES, VISIBILITY_RUNGS, resolveWriteTarget, memoryFilePath, buildOrigin, coerceGate, coerceAppliesTo, coerceReadWhen, serializeMemoryDoc, } from './shared.js';
3
+ import { writeText, pathExists } from '../../core/fs-utils.js';
4
+ import { appendHistoryRecord, buildHistoryRecord, historyLogPathFor, } from '../../core/memory/history.js';
5
+ import { DOC_RATIONALE_CONSTRAINT, GUIDE_DOC_LINKS, GUIDE_PREDICATE_VOCABULARY, GUIDE_ROUTING_LINE, GUIDE_VISIBILITY_RUNGS, MEMORY_SCOPES, resolveWriteTarget, memoryFilePath, buildOrigin, coerceGate, coerceAppliesTo, coerceReadWhen, overlayParam, serializeMemoryDoc, } from './shared.js';
6
6
  // A topic's memory set stays usable only when authors treat cohesion as a creation
7
7
  // gate; otherwise small corrections accumulate as overlapping leaves whose routing
8
8
  // lines cannot tell a future reader which one owns the topic.
9
9
  export const writeLeaf = defineLeaf({
10
10
  name: 'write',
11
- description: 'create or update a memory document',
12
- whenToUse: 'you are recording a new knowledge document or preference, or revising one that already exists — an existing <name> at the resolved scope is updated in place, so revision is also this leaf.',
11
+ description: 'create a memory document',
12
+ whenToUse: 'you are recording a new knowledge document or preference that does not exist yet. Creation only — a name already taken at the resolved scope is an error, because revising a stored document goes through `crtr memory edit`, which requires a rationale and records the revision.',
13
13
  help: {
14
14
  name: 'memory write',
15
- summary: 'create or update memory/<name>.md at the resolved scope from frontmatter flags + a stdin body',
15
+ summary: 'create memory/<name>.md at the resolved scope from frontmatter flags + a stdin body',
16
16
  guide: 'Every frontmatter flag decides who sees this doc, when, and at what context cost. Each rung up is paid by every future agent at every boot or read, so default each rung down.\n\n' +
17
17
  'Pick the kind. knowledge is consulted for facts or procedures; preference directs behavior. The kind choice is about how the doc is used, not about how long it is.\n\n' +
18
18
  'Store reusable current truth, not session notes. Useful memories are non-obvious procedures, gotchas, durable preferences, and cross-repo conventions. Do not store chat summaries, implementation history, or facts already recorded in the repo.\n\n' +
19
- 'The visibility rungs `none`, `name`, `preview`, and `content` move from least to most loaded: `none` keeps the doc out of auto-load and on-read surfaces, `name` is the bare doc tag only, `preview` is the name + envelope + routing line (`when-and-why-to-read`), and `content` inlines the whole body when the body is short enough to justify it. Each axis is independent; usually one carries a real rung and the other is `none`. When a doc fits in a single sentence — a one-line preference or a one-sentence knowledge fact — skip `preview` and use `name` or `content` directly: the routing line would run longer than the doc itself. Sentence-length `content` docs are correct; never pad a memory to be more verbose than the rule or fact it carries.\n\n' +
19
+ GUIDE_VISIBILITY_RUNGS + '\n\n' +
20
20
  'Choose the scope. `project` is for facts any agent in one repo needs. `user` is for person-wide facts and preferences that should follow the user everywhere. `profile` is for the profile’s bundle of dirs: cross-repo conventions, how the pieces relate, or the user’s stance toward that body of work. `node` is scratch memory only this running node sees; it rides into this node’s knowledge block and dies with the node. When unsure, choose the narrowest scope that will still reach the next agent who needs it.\n\n' +
21
21
  'A project root INDEX is the workspace front door. Name the physical doc INDEX, target the exact project with --dir, and use kind knowledge, system-prompt-visibility none, file-read-visibility content, and applies-to ".". Its envelope name comes from the owning project directory unless the document already carries an explicit name. Keep only the project constraints, key commands, architecture orientation, and conventions that differ from defaults. `crtr memory lint` validates this exact contract for every project managed by the selected profile.\n\n' +
22
22
  'Choose the hook — boot vs file context. System-prompt visibility is the boot catalog; file-read visibility fires on the applies-to routes. Put code-specific knowledge in the owning project store, give it the narrowest real file glob, and keep it out of boot when the file read is the useful trigger. Knowledge about a person or process usually has no file boundary, so set file-read-visibility none and route it through boot instead.\n\n' +
23
- 'Write the routing line (--when-and-why-to-read) first, before storing anything. The test for its because-clause: if it can be derived by paraphrasing the doc’s advice, it is a restatement, not a payoff — a real payoff names a consequence in the reader’s world that the document itself never asserts. Bad: "because only genuine first principles belong in taste memory." Bad: "because keeping the test loop fast and free of speculative tests protects the development pace" — the doc’s rule as an outcome, derivable straight from its advice. Good: "because identifying throughlines in user taste lets future decisions be made faster and with less re-litigation." Someone mid-task who has not read the doc must be able to decide from this line alone whether the read is worth it. If you cannot name the concrete situation that triggers it, you do not yet understand the memory — ask the user one sharp question instead of improvising.\n\n' +
24
- 'Gate and read-when share the same predicate language: a field map is AND-ed across fields; dotted fields resolve nested values; field matchers may be scalar, array, or object. Scalar matchers do exact equality, with arrays matching any element. Array matchers do membership or intersection. Object matchers accept `eq`, `ne`, `in`, `nin`, `exists`, `contains`, `containsAll`, `containsAny`, `matches`, `imatches`, `gt`, `gte`, `lt`, and `lte`. Combinators are `all`, `any`, and `not`; sibling field matchers next to them are AND-ed in. An empty condition is inert. An unknown op never matches.\n\n' +
25
- 'Link related docs with `[[canonical/name]]`. A doc link in any body is a first-class cross-reference to another memory document by its exact canonical name — the same identifier `crtr memory read` takes (a directory INDEX is linked by its bare directory name). Links are pointers, never transclusion: the linked body is loaded only when a reader follows it, so a link costs nothing until needed. `crtr memory lint` fails on a link that resolves to no document, and `crtr memory read` lists a doc’s resolvable links alongside its body. There is no alias or label form.\n\n' +
23
+ 'Write the routing line (--when-and-why-to-read) first, before storing anything. ' + GUIDE_ROUTING_LINE + '\n\n' +
24
+ GUIDE_PREDICATE_VOCABULARY + '\n\n' +
25
+ GUIDE_DOC_LINKS + '\n\n' +
26
26
  'When a doc grows long or information-rich, nest it into a graph instead of letting it become a scroll. The main doc at the topic’s path keeps the high-level, most load-bearing information, most important first; depth splits into reference docs under the topic’s directory (`area/topic/...`), each pointed at with a `[[link]]`. Split by subject: a leaf earns its link by covering a different subject a task might need on its own; a leaf of offloaded “further evidence”, examples, or references is never followed, so supporting material either sits in the main doc next to the point it supports or gets cut. The main doc is the entry point a reader can act from alone; a reference leaf is loaded only when the task needs that depth. Save reference leaves at `none` visibility on both axes — the link from the main doc is how they are found, so any higher rung just double-charges every boot or read for depth the graph already routes. Keep every doc as short as its job allows; `crtr memory lint` caps body length by rung and its findings carry the split guidance.\n\n' +
27
27
  'A directory INDEX earns existence only where the aggregate view beats the sum of per-file rungs. Aggregated child routing is automatic: `crtr memory read` of a directory INDEX appends every member’s routing line (`[[name]]: <when-and-why-to-read>`), so never author that list in the body. Two shapes pass: synthesis — an operating guide or the cluster’s mechanics, ordering, conditions, and relationships, content no single child can carry — and a pure gate — one preview line covering a cluster with one shared routing condition, the body adding nothing the automatic list does not. Otherwise skip the INDEX and let each doc’s own rung route it: frontmatter routing lines are self-maintaining, while an INDEX body describing its children is a hand-maintained copy that drifts. Never write an INDEX body that restates child names, paraphrases their routing lines, fronts a small directory of sharply named docs, or summarizes children a reader should open.\n\n' +
28
- 'Find before write. Prefer slightly expanding an existing document, nesting genuinely separate depth under its topic, and updating the existing `when-and-why-to-read` (plus its INDEX router when present) over creating another similar memory. A new document earns its own identity only when it has a distinct read trigger and a coherent body whose merge into the existing document would make it harder to route or use. Group related docs with path names (area/topic). Provenance is automatic on create and preserved on update. Run `crtr memory lint` after authoring.\n\n' +
29
- '--rationale is the gap this doc exists to close — the observed agent failure that prompted it, captured from user signal (a correction, a mistake you watched happen) rather than inferred from the doc’s own content. If the rationale is guessable from reading the doc, it is not the real one — a guessable gap is one agents do not actually fall into. Omit the flag when you have no observed gap to record.',
28
+ 'Find before write. Prefer slightly expanding an existing document with `crtr memory edit`, nesting genuinely separate depth under its topic, and updating the existing `when-and-why-to-read` (plus its INDEX router when present) over creating another similar memory. A new document earns its own identity only when it has a distinct read trigger and a coherent body whose merge into the existing document would make it harder to route or use. Group related docs with path names (area/topic). Provenance is stamped here and preserved by every later revision. Run `crtr memory lint` after authoring.\n\n' +
29
+ '--rationale is the gap this doc exists to close — the observed agent failure that prompted it, captured from user signal (a correction, a mistake you watched happen) rather than inferred from the doc’s own content. If the rationale is guessable from reading the doc, it is not the real one — a guessable gap is one agents do not actually fall into. Omit the flag when you have no observed gap to record.\n\n' +
30
+ 'Revise an existing doc with `crtr memory edit`.',
30
31
  params: [
31
- { kind: 'positional', name: 'name', required: true, constraint: 'Path-derived identity: one segment, or several joined with `/` to nest the document under a directory and group it with related docs → memory/<name>.md at the resolved scope. Updated in place if it already exists, otherwise created.' },
32
- { kind: 'flag', name: 'kind', type: 'enum', choices: [...MEMORY_KINDS], required: true, constraint: 'Document kind.' },
33
- { kind: 'flag', name: 'when-and-why-to-read', type: 'string', required: false, constraint: 'ONE routing sentence: "When <circumstance>, this <kind> should be read because <broader downstream payoff>." WHY is the reader’s payoff — the consequence they secure for their task by reading — NEVER the doc summary, its rule, or that rule reworded as an outcome (a benefit-shaped restatement still fails). Rendered verbatim as the preview; required when creating.' },
34
- { kind: 'flag', name: 'short-form', type: 'string', required: false, constraint: 'Frontmatter short-form — a very abbreviated version of the content, the hook shown in `crtr memory list`.' },
35
- { kind: 'flag', name: 'system-prompt-visibility', type: 'enum', choices: [...VISIBILITY_RUNGS], required: false, constraint: 'Rung controlling how much of this document auto-loads into the system prompt / CLI help. Required when creating — there is no kind default; pick a rung explicitly.' },
36
- { kind: 'flag', name: 'file-read-visibility', type: 'enum', choices: [...VISIBILITY_RUNGS], required: false, constraint: 'Rung controlling how much surfaces through file context. Required when creating — there is no kind default. Every value except none also requires at least one applies-to route in the resulting document.' },
37
- { kind: 'flag', name: 'gate', type: 'string', required: false, constraint: 'Frontmatter gate — YAML/JSON object predicate over node config using the same field/matcher vocabulary described in the guide.' },
38
- { kind: 'flag', name: 'applies-to', type: 'string', required: false, repeatable: true, constraint: 'One explicit file-context route per occurrence. In a project store, `.` fires when cwd/profile mounts that workspace during first-message assembly AND on any file read beneath the store\u2019s owning dir. Any other value is a glob matched after an actual file read against the absolute path, basename, and path relative to the owning project root. There is no positional fallback from where the memory file lives. Required whenever file-read-visibility is not none.' },
39
- { kind: 'flag', name: 'read-when', type: 'string', required: false, constraint: 'Frontmatter read-when — YAML/JSON object predicate over a read file’s own frontmatter using the same field/matcher vocabulary described in the guide.' },
40
- { kind: 'flag', name: 'slash', type: 'bool', required: false, default: false, constraint: 'Presence flags this doc invocable as a pi slash command (`/<name>`, `/` in a nested name rendered as `:`) — the doc body becomes the command’s injected prompt. Default false: most docs are consulted, not invoked.' },
41
- { kind: 'flag', name: 'rationale', type: 'string', required: false, constraint: 'Frontmatter rationale — the observed agent failure that made this doc necessary. Maintainer-facing only: never ships in any delivered surface (boot render, on-read injection, `memory read` content), visible only via `memory read --frontmatter`. Omitting this flag on an update PRESERVES an existing rationale unchanged.' },
32
+ { kind: 'positional', name: 'name', required: true, constraint: 'Path-derived identity: one segment, or several joined with `/` to nest the document under a directory and group it with related docs → memory/<name>.md at the resolved scope. The name must be free at that scope: an existing one is an error naming `crtr memory edit`.' },
33
+ overlayParam('kind', { required: true }),
34
+ overlayParam('when-and-why-to-read', { required: true }),
35
+ overlayParam('short-form'),
36
+ overlayParam('system-prompt-visibility', { required: true }, 'There is no kind default; pick a rung explicitly.'),
37
+ overlayParam('file-read-visibility', { required: true }, 'There is no kind default; pick a rung explicitly.'),
38
+ overlayParam('gate'),
39
+ overlayParam('applies-to'),
40
+ overlayParam('read-when'),
41
+ overlayParam('slash'),
42
+ { kind: 'flag', name: 'rationale', type: 'string', required: false, constraint: DOC_RATIONALE_CONSTRAINT },
42
43
  { kind: 'flag', name: 'scope', type: 'enum', choices: [...MEMORY_SCOPES], required: false, constraint: 'Target scope. Default: project when inside a project, else user. `project` resolves to the NEAREST ancestor `.crouter/` walking up from cwd — in a nested workspace that can be a parent’s store, not the dir you are standing in; pass --dir to pin the exact project directory. `profile` requires a selected profile (CRTR_PROFILE_ID) or an explicit --profile. `node` writes to the this-node store (`nodes/<CRTR_NODE_ID>/context/memory/`) — the nearest scope, seen only by this running node, requires a node context.' },
43
44
  { kind: 'flag', name: 'dir', type: 'string', required: false, constraint: 'Exact project directory to write under — targets `<dir>/.crouter/memory/` regardless of cwd or ancestor stores, scaffolding `.crouter/` there if absent. THE way to place a doc in a specific project’s store (e.g. another project in your profile’s purview) without cd’ing there, and the only way to target a dir shadowed by an ancestor store. Implies --scope project; rejects --scope user/profile.' },
44
45
  { kind: 'flag', name: 'profile', type: 'string', required: false, constraint: 'Profile id or name to write under, for --scope profile. Default: the process CRTR_PROFILE_ID (the node\u2019s selected profile). Resolved through the same profile lookup as `crtr profile show`. Rejected (usage error) unless --scope profile is also selected \u2014 never silently ignored for another scope.' },
@@ -48,14 +49,15 @@ export const writeLeaf = defineLeaf({
48
49
  { name: 'name', type: 'string', required: true, constraint: 'The path-derived document name written.' },
49
50
  { name: 'kind', type: 'string', required: true, constraint: 'Kind recorded in frontmatter.' },
50
51
  { name: 'scope', type: 'string', required: true, constraint: 'Scope the document was written to: user, project, profile, or node.' },
51
- { name: 'path', type: 'string', required: true, constraint: 'Absolute path to the written document — edit this file directly for later body tweaks instead of re-running write.' },
52
- { name: 'created', type: 'boolean', required: true, constraint: 'true when a new document was created, false when an existing one was updated in place.' },
53
- { name: 'frontmatter', type: 'object[]', required: true, constraint: 'The frontmatter options selected by this invocation, in document field order. Each: {key, value}. Preserved fields omitted from an update are not repeated.' },
54
- { name: 'follow_up', type: 'string', required: true, constraint: 'Concrete next commands — read it back or list the inventory.' },
52
+ { name: 'path', type: 'string', required: true, constraint: 'Absolute path to the written document.' },
53
+ { name: 'created', type: 'boolean', required: true, constraint: 'Always true on success — this leaf only creates. A name already taken at the resolved scope fails with a usage error naming `crtr memory edit`.' },
54
+ { name: 'frontmatter', type: 'object[]', required: true, constraint: 'The frontmatter options selected by this invocation, in document field order. Each: {key, value}.' },
55
+ { name: 'log_path', type: 'string', required: true, constraint: 'Absolute path to the document’s append-only revision log, opened here with a `create` record.' },
56
+ { name: 'follow_up', type: 'string', required: true, constraint: 'Concrete next commands — read it back, revise it, or list the inventory.' },
55
57
  ],
56
58
  outputKind: 'object',
57
59
  effects: [
58
- 'Creates or overwrites memory/<name>.md at the resolved scope with the given frontmatter fields + stdin body.',
60
+ 'Creates memory/<name>.md at the resolved scope with the given frontmatter fields + stdin body, and opens memory/.history/<name>.jsonl with a `create` record.',
59
61
  ],
60
62
  },
61
63
  run: async (input) => {
@@ -67,35 +69,20 @@ export const writeLeaf = defineLeaf({
67
69
  const dirArg = input['dir'];
68
70
  const { scope, memoryDir } = resolveWriteTarget(scopeArg, profileArg, dirArg);
69
71
  const path = memoryFilePath(memoryDir, name);
70
- const created = !pathExists(path);
71
- // CREATE requires the read-routing line that becomes the preview — without
72
- // it every preview renders empty. UPDATE inherits it from the existing doc.
73
- if (created && input['whenAndWhyToRead'] === undefined) {
74
- throw usage(`creating ${name} requires --when-and-why-to-read: one read-routing sentence "When <circumstance>, this ${kind} should be read because <broader downstream payoff>." WHY is the reader’s payoff — what the read secures for their task — not the document summary, its rule, or that rule reworded as an outcome.`);
72
+ // Create-only. Revision goes through `edit`, which requires a rationale and
73
+ // records the change — a write that still overwrote would be a standing
74
+ // bypass of the audit trail. The error is the migration path.
75
+ if (pathExists(path)) {
76
+ throw usage(`${name} already exists at ${scope} scope — revise it with \`crtr memory edit ${name} --rationale "<why this revision>"\`, which records the change. \`write\` only creates.`, { memory: name, scope, path });
75
77
  }
76
- // CREATE requires BOTH visibility rungs as an explicit, case-by-case call —
77
- // there is no kind default to lean on. UPDATE inherits whatever the existing
78
- // doc already carries.
79
- if (created && input['systemPromptVisibility'] === undefined) {
80
- throw usage(`creating ${name} requires --system-prompt-visibility: pick a rung explicitly (none|name|preview|content) — there is no kind default. See \`crtr memory write -h\` for what each rung means.`);
81
- }
82
- if (created && input['fileReadVisibility'] === undefined) {
83
- throw usage(`creating ${name} requires --file-read-visibility: pick a rung explicitly (none|name|preview|content) — there is no kind default. See \`crtr memory write -h\` for what each rung means.`);
84
- }
85
- // In-place update: start from the existing frontmatter (preserving fields
86
- // not passed this time), then overlay the provided ones. Create: start clean.
87
- const frontmatter = created
88
- ? {}
89
- : { ...(parseFrontmatterGeneric(readText(path)).data ?? {}) };
90
- // kind is required, always set. Optionals only overlay when provided so an
91
- // update never erases a field the caller did not mention.
78
+ const frontmatter = {};
92
79
  frontmatter['kind'] = kind;
93
- // Provenance is runtime-stamped ONCE, at creation — who/when/where authored
94
- // this doc. An update inherits the existing `origin` via the spread above and
95
- // never overwrites it, so it always points at the conversation that created
96
- // the doc, not the last one to touch it. `crtr memory origin <name>` derefs it.
97
- if (created)
98
- frontmatter['origin'] = buildOrigin();
80
+ // Provenance is runtime-stamped ONCE, here — who/when/where authored this
81
+ // doc. Every later revision preserves it, so it always points at the
82
+ // conversation that created the doc, not the last one to touch it.
83
+ // `crtr memory origin <name>` derefs it.
84
+ const origin = buildOrigin();
85
+ frontmatter['origin'] = origin;
99
86
  const setIf = (key, value) => {
100
87
  if (value !== undefined)
101
88
  frontmatter[key] = value;
@@ -115,6 +102,8 @@ export const writeLeaf = defineLeaf({
115
102
  if (input['slash'] === true)
116
103
  frontmatter['slash'] = true;
117
104
  setIf('rationale', input['rationale']);
105
+ // One field consumers read for recency, live from the first byte.
106
+ frontmatter['last-updated'] = origin['created'];
118
107
  const applies = frontmatter['applies-to'];
119
108
  const routes = typeof applies === 'string'
120
109
  ? [applies]
@@ -127,10 +116,13 @@ export const writeLeaf = defineLeaf({
127
116
  if (scope !== 'project' && routes.some((route) => route.trim() === '.')) {
128
117
  throw usage('applies-to "." is a project-store workspace mount route; use a real file glob or choose file-read-visibility none for user, profile, and node memory');
129
118
  }
130
- writeText(path, serializeMemoryDoc(frontmatter, body));
131
- // Confirm exactly the frontmatter choices made by this invocation. On an
132
- // update, inherited fields stay in the file but do not masquerade as newly
133
- // selected options in the result or its collapsed viewer preview.
119
+ const after = serializeMemoryDoc(frontmatter, body);
120
+ writeText(path, after);
121
+ const logPath = historyLogPathFor(memoryDir, path);
122
+ appendHistoryRecord(logPath, buildHistoryRecord({ op: 'create', before: '', after }));
123
+ // Confirm exactly the frontmatter choices made by this invocation — the
124
+ // runtime-stamped fields do not masquerade as selected options in the
125
+ // result or its collapsed viewer preview.
134
126
  const selectedKeys = [
135
127
  'kind',
136
128
  ...(input['whenAndWhyToRead'] !== undefined ? ['when-and-why-to-read'] : []),
@@ -149,9 +141,10 @@ export const writeLeaf = defineLeaf({
149
141
  kind,
150
142
  scope,
151
143
  path,
152
- created,
144
+ created: true,
153
145
  frontmatter: selectedFrontmatter,
154
- follow_up: `Read it back with \`crtr memory read ${name}\`, edit ${path} directly for a later body-only tweak, or browse the inventory with \`crtr memory list\`.`,
146
+ log_path: logPath,
147
+ follow_up: `Read it back with \`crtr memory read ${name}\`, revise it later with \`crtr memory edit ${name} --rationale "<why>"\`, or browse the inventory with \`crtr memory list\`.`,
155
148
  };
156
149
  },
157
150
  });
@@ -1,10 +1,13 @@
1
1
  // `crtr memory` subtree — the document substrate (knowledge and preferences)
2
- // accessed via the CLI. Flat leaves: list, read, find, write, lint.
2
+ // accessed via the CLI. Flat leaves: list, read, find, write, edit, history,
3
+ // delete, origin, lint.
3
4
  import { defineBranch } from '../core/command.js';
4
5
  import { listLeaf } from './memory/list.js';
5
6
  import { readLeaf } from './memory/read.js';
6
7
  import { findLeaf } from './memory/find.js';
7
8
  import { writeLeaf } from './memory/write.js';
9
+ import { editLeaf } from './memory/edit.js';
10
+ import { historyLeaf } from './memory/history.js';
8
11
  import { deleteLeaf } from './memory/delete.js';
9
12
  import { originLeaf } from './memory/origin.js';
10
13
  import { lintLeaf } from './memory/lint.js';
@@ -19,8 +22,8 @@ export function registerMemory() {
19
22
  help: {
20
23
  name: 'memory',
21
24
  summary: 'list, read, search, and write memory documents — knowledge and preferences',
22
- model: 'Documents have path-derived identities and resolve across layered scopes in precedence order: node > project stack > profile > user > builtin. Browse the inventory with `list` to see what is stored; address a document directly with `read`/`write` once you know its name. A directory may carry an `INDEX.md` with the same frontmatter schema as any doc — it renders as one boot-catalog entry whose system-prompt rung caps that subtree. File context is separate and explicit: `applies-to: "."` opens project context with the workspace; other globs fire after matching reads.',
25
+ model: 'Documents have path-derived identities and resolve across layered scopes in precedence order: node > project stack > profile > user > builtin. Browse the inventory with `list` to see what is stored; address a document directly with `read` once you know its name. One verb per operation: `write` creates, `edit` revises (every revision carries a rationale and is recorded), `delete` removes, and `history` reads how a document reached its current state. A directory may carry an `INDEX.md` with the same frontmatter schema as any doc — it renders as one boot-catalog entry whose system-prompt rung caps that subtree. File context is separate and explicit: `applies-to: "."` opens project context with the workspace; other globs fire after matching reads.',
23
26
  },
24
- children: [listLeaf, readLeaf, findLeaf, writeLeaf, deleteLeaf, originLeaf, lintLeaf],
27
+ children: [listLeaf, readLeaf, findLeaf, writeLeaf, editLeaf, historyLeaf, deleteLeaf, originLeaf, lintLeaf],
25
28
  });
26
29
  }
@@ -0,0 +1,54 @@
1
+ /** Sidecar tree name under a store's `memory/` dir. Dot-prefixed and holding
2
+ * only `.jsonl`, so every substrate scan (which filters to `*.md`, and in
3
+ * `lint`'s case skips dot-dirs outright) passes it over without an exclusion
4
+ * rule of its own. */
5
+ export declare const HISTORY_DIR = ".history";
6
+ /** File operations only — deliberately not a review lifecycle. */
7
+ export type HistoryOp = 'create' | 'edit' | 'delete';
8
+ export interface HistoryRecord {
9
+ op: HistoryOp;
10
+ at: string;
11
+ /** Authoring node id; absent for a human at a bare CLI. */
12
+ node?: string;
13
+ cwd: string;
14
+ /** Why this revision happened. Required on `edit`, absent otherwise. */
15
+ rationale?: string;
16
+ /** True when the body was declared a verbatim payload from the principal. */
17
+ verbatim?: boolean;
18
+ /** Complete document text before the mutation; empty for `create`. */
19
+ before: string;
20
+ /** Complete document text after the mutation; empty for `delete`. */
21
+ after: string;
22
+ }
23
+ /** The log path for a document, derived from its PHYSICAL path so the history
24
+ * tree mirrors the doc tree exactly (`memory/area/topic.md` →
25
+ * `memory/.history/area/topic.jsonl`). `memoryRoot` is the store's `memory/`
26
+ * dir the doc lives under. */
27
+ export declare function historyLogPathFor(memoryRoot: string, docPath: string): string;
28
+ /** Stamp the runtime-owned fields of a record. Field insertion order IS the
29
+ * serialized key order — the on-disk shape consumers read. */
30
+ export declare function buildHistoryRecord(input: {
31
+ op: HistoryOp;
32
+ rationale?: string;
33
+ verbatim?: boolean;
34
+ before: string;
35
+ after: string;
36
+ }): HistoryRecord;
37
+ /** Append one record. Single write of one line — concurrent appends interleave
38
+ * whole records, and every record stands alone, so no lock is needed. */
39
+ export declare function appendHistoryRecord(logPath: string, record: HistoryRecord): void;
40
+ /** Every record in a log, oldest first (append order). Revision numbers are
41
+ * 1-based positions in this array. */
42
+ export declare function readHistoryRecords(logPath: string): HistoryRecord[];
43
+ export interface ChangeSummary {
44
+ body_changed: boolean;
45
+ /** Frontmatter field names whose values differ between the two texts. */
46
+ frontmatter_changed: string[];
47
+ }
48
+ /** What one record changed, derived from its own two texts. `last-updated` is
49
+ * excluded: the runtime stamps it on every edit, so listing it says nothing. */
50
+ export declare function summarizeChange(before: string, after: string): ChangeSummary;
51
+ /** Unified diff between two document texts, computed at read time. Dependency
52
+ * free (an LCS over lines) — memory docs are lint-capped small, so the
53
+ * quadratic table is never a cost. */
54
+ export declare function unifiedDiff(before: string, after: string, context?: number): string;
@@ -0,0 +1,202 @@
1
+ // Per-document revision history for the memory substrate: one append-only
2
+ // JSONL sidecar per doc at `<store>/memory/.history/<name>.jsonl`, mirroring
3
+ // the doc tree segment for segment. Every record carries the COMPLETE document
4
+ // text before and after the mutation, so a reader never reconstructs a chain,
5
+ // an interleaved concurrent append stays fully valid, and an out-of-band direct
6
+ // file edit is visible as a record whose `before` differs from the prior
7
+ // record's `after`. Diffs and revision numbers are derived at read time, never
8
+ // stored.
9
+ //
10
+ // The raw file is the consumer contract: an external reader (a product surface
11
+ // reading a store off disk) parses one JSON object per line and computes its
12
+ // own diffs. Nothing here shells out to git.
13
+ import { appendFileSync } from 'node:fs';
14
+ import { dirname, join, relative } from 'node:path';
15
+ import { ensureDir, pathExists, readText } from '../fs-utils.js';
16
+ import { general } from '../errors.js';
17
+ import { parseFrontmatterGeneric } from '../frontmatter.js';
18
+ /** Sidecar tree name under a store's `memory/` dir. Dot-prefixed and holding
19
+ * only `.jsonl`, so every substrate scan (which filters to `*.md`, and in
20
+ * `lint`'s case skips dot-dirs outright) passes it over without an exclusion
21
+ * rule of its own. */
22
+ export const HISTORY_DIR = '.history';
23
+ /** The log path for a document, derived from its PHYSICAL path so the history
24
+ * tree mirrors the doc tree exactly (`memory/area/topic.md` →
25
+ * `memory/.history/area/topic.jsonl`). `memoryRoot` is the store's `memory/`
26
+ * dir the doc lives under. */
27
+ export function historyLogPathFor(memoryRoot, docPath) {
28
+ const rel = relative(memoryRoot, docPath);
29
+ if (rel === '' || rel.startsWith('..')) {
30
+ throw general(`memory document ${docPath} is not inside its store root ${memoryRoot}`);
31
+ }
32
+ return join(memoryRoot, HISTORY_DIR, rel.replace(/\.md$/i, '') + '.jsonl');
33
+ }
34
+ /** Stamp the runtime-owned fields of a record. Field insertion order IS the
35
+ * serialized key order — the on-disk shape consumers read. */
36
+ export function buildHistoryRecord(input) {
37
+ const env = process.env;
38
+ const record = {
39
+ op: input.op,
40
+ at: new Date().toISOString(),
41
+ ...(env['CRTR_NODE_ID'] ? { node: env['CRTR_NODE_ID'] } : {}),
42
+ cwd: env['CRTR_NODE_CWD'] ?? process.cwd(),
43
+ ...(input.rationale !== undefined ? { rationale: input.rationale } : {}),
44
+ ...(input.verbatim === true ? { verbatim: true } : {}),
45
+ before: input.before,
46
+ after: input.after,
47
+ };
48
+ return record;
49
+ }
50
+ /** Append one record. Single write of one line — concurrent appends interleave
51
+ * whole records, and every record stands alone, so no lock is needed. */
52
+ export function appendHistoryRecord(logPath, record) {
53
+ ensureDir(dirname(logPath));
54
+ appendFileSync(logPath, JSON.stringify(record) + '\n', 'utf8');
55
+ }
56
+ /** Every record in a log, oldest first (append order). Revision numbers are
57
+ * 1-based positions in this array. */
58
+ export function readHistoryRecords(logPath) {
59
+ if (!pathExists(logPath))
60
+ return [];
61
+ const lines = readText(logPath).split('\n');
62
+ const records = [];
63
+ for (let i = 0; i < lines.length; i += 1) {
64
+ const line = lines[i];
65
+ if (line.trim() === '')
66
+ continue;
67
+ try {
68
+ records.push(JSON.parse(line));
69
+ }
70
+ catch {
71
+ throw general(`corrupt history record at ${logPath}:${i + 1} — the line is not valid JSON`, {
72
+ next: 'Inspect that line directly; the surrounding records are self-contained and still readable.',
73
+ });
74
+ }
75
+ }
76
+ return records;
77
+ }
78
+ /** What one record changed, derived from its own two texts. `last-updated` is
79
+ * excluded: the runtime stamps it on every edit, so listing it says nothing. */
80
+ export function summarizeChange(before, after) {
81
+ const b = parseFrontmatterGeneric(before);
82
+ const a = parseFrontmatterGeneric(after);
83
+ const bData = b.data ?? {};
84
+ const aData = a.data ?? {};
85
+ const keys = new Set([...Object.keys(bData), ...Object.keys(aData)]);
86
+ keys.delete('last-updated');
87
+ const changed = [...keys]
88
+ .filter((k) => JSON.stringify(bData[k]) !== JSON.stringify(aData[k]))
89
+ .sort((x, y) => x.localeCompare(y));
90
+ return { body_changed: b.body !== a.body, frontmatter_changed: changed };
91
+ }
92
+ /** Unified diff between two document texts, computed at read time. Dependency
93
+ * free (an LCS over lines) — memory docs are lint-capped small, so the
94
+ * quadratic table is never a cost. */
95
+ export function unifiedDiff(before, after, context = 3) {
96
+ const { lines: a, terminated: aTerm } = toLines(before);
97
+ const { lines: b, terminated: bTerm } = toLines(after);
98
+ // Diff on keys, not raw text: an unterminated final line is a DIFFERENT line
99
+ // from the same text terminated, so dropping a trailing newline is a real
100
+ // change rather than an empty diff.
101
+ const ops = diffOps(diffKeys(a, aTerm), diffKeys(b, bTerm));
102
+ // Each changed op claims `context` ops on either side; overlapping claims
103
+ // merge into one hunk.
104
+ const ranges = [];
105
+ for (let i = 0; i < ops.length; i += 1) {
106
+ if (ops[i].tag === ' ')
107
+ continue;
108
+ const from = Math.max(0, i - context);
109
+ const to = Math.min(ops.length, i + context + 1);
110
+ const last = ranges[ranges.length - 1];
111
+ if (last !== undefined && from <= last.to)
112
+ last.to = Math.max(last.to, to);
113
+ else
114
+ ranges.push({ from, to });
115
+ }
116
+ if (ranges.length === 0)
117
+ return '';
118
+ // Line numbers each op sits at, in the before and after texts.
119
+ const aAt = [];
120
+ const bAt = [];
121
+ let aLine = 0;
122
+ let bLine = 0;
123
+ for (const op of ops) {
124
+ aAt.push(aLine);
125
+ bAt.push(bLine);
126
+ if (op.tag !== '+')
127
+ aLine += 1;
128
+ if (op.tag !== '-')
129
+ bLine += 1;
130
+ }
131
+ const out = [];
132
+ for (const { from, to } of ranges) {
133
+ const slice = ops.slice(from, to);
134
+ const aCount = slice.filter((op) => op.tag !== '+').length;
135
+ const bCount = slice.filter((op) => op.tag !== '-').length;
136
+ // A side contributing no lines is `0,0` — an empty file has no line 1.
137
+ out.push(`@@ -${aCount === 0 ? 0 : aAt[from] + 1},${aCount} +${bCount === 0 ? 0 : bAt[from] + 1},${bCount} @@`);
138
+ for (let i = from; i < to; i += 1) {
139
+ const op = ops[i];
140
+ out.push(op.tag + (op.tag === '+' ? b[op.index] : a[op.index]));
141
+ const endsA = op.tag !== '+' && !aTerm && aAt[i] === a.length - 1;
142
+ const endsB = op.tag !== '-' && !bTerm && bAt[i] === b.length - 1;
143
+ if (endsA || endsB)
144
+ out.push(NO_NEWLINE_MARKER);
145
+ }
146
+ }
147
+ return out.join('\n');
148
+ }
149
+ const NO_NEWLINE_MARKER = '\';
150
+ // U+0000 cannot occur in a memory document, so it can never collide with text.
151
+ const NO_NEWLINE_KEY = '\u0000no-newline-at-eof';
152
+ function diffKeys(lines, terminated) {
153
+ if (terminated || lines.length === 0)
154
+ return lines;
155
+ return lines.map((line, i) => (i === lines.length - 1 ? line + NO_NEWLINE_KEY : line));
156
+ }
157
+ /** Split into diffable lines. A final `\n` TERMINATES the last line rather than
158
+ * opening an empty one, so `foo\n` is one line; a text without it is flagged so
159
+ * the diff can mark it, the way a unified patch must. */
160
+ function toLines(text) {
161
+ if (text === '')
162
+ return { lines: [], terminated: true };
163
+ const terminated = text.endsWith('\n');
164
+ const lines = text.split('\n');
165
+ if (terminated)
166
+ lines.pop();
167
+ return { lines, terminated };
168
+ }
169
+ function diffOps(a, b) {
170
+ const n = a.length;
171
+ const m = b.length;
172
+ // lcs[i][j] = length of the longest common subsequence of a[i..] and b[j..].
173
+ const lcs = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
174
+ for (let i = n - 1; i >= 0; i -= 1) {
175
+ for (let j = m - 1; j >= 0; j -= 1) {
176
+ lcs[i][j] = a[i] === b[j] ? lcs[i + 1][j + 1] + 1 : Math.max(lcs[i + 1][j], lcs[i][j + 1]);
177
+ }
178
+ }
179
+ const ops = [];
180
+ let i = 0;
181
+ let j = 0;
182
+ while (i < n && j < m) {
183
+ if (a[i] === b[j]) {
184
+ ops.push({ tag: ' ', index: i });
185
+ i += 1;
186
+ j += 1;
187
+ }
188
+ else if (lcs[i + 1][j] >= lcs[i][j + 1]) {
189
+ ops.push({ tag: '-', index: i });
190
+ i += 1;
191
+ }
192
+ else {
193
+ ops.push({ tag: '+', index: j });
194
+ j += 1;
195
+ }
196
+ }
197
+ for (; i < n; i += 1)
198
+ ops.push({ tag: '-', index: i });
199
+ for (; j < m; j += 1)
200
+ ops.push({ tag: '+', index: j });
201
+ return ops;
202
+ }
@@ -26,6 +26,11 @@ export interface MemoryDoc {
26
26
  scope: MemoryScope;
27
27
  /** Absolute path to the resolved .md file. */
28
28
  path: string;
29
+ /** Absolute path of the `memory/` dir this doc loaded from — the store root
30
+ * its identity is relative to. Sidecar trees that mirror the doc tree (the
31
+ * revision-history log) are derived from `path` relative to this. For a
32
+ * plugin doc it is that plugin's own memory dir. */
33
+ root: string;
29
34
  /** Raw, uncoerced frontmatter record (null when the doc has no frontmatter). */
30
35
  frontmatter: Record<string, unknown> | null;
31
36
  /** Document body, with the frontmatter block stripped. */
@@ -64,6 +69,15 @@ export interface MemoryResolutionOpts {
64
69
  * persona resolution, which stay on the flat ancestor+profile stack. */
65
70
  includeDescendants?: boolean;
66
71
  }
72
+ /** The native (non-plugin) store dirs in resolution precedence — node >
73
+ * project stack > profile > user, builtin excluded as a read-only corpus.
74
+ * Addresses a store directly when the document itself may be absent, which is
75
+ * how a DELETED doc's revision log is still found: history outlives the doc,
76
+ * so its lookup cannot go through document resolution. */
77
+ export declare function nativeMemoryStoresInPrecedence(scope?: MemoryScope, includeDescendants?: boolean): {
78
+ scope: MemoryScope;
79
+ memoryDir: string;
80
+ }[];
67
81
  /** Canonical, unambiguous identifier for a memory document: `<scope>/<name>`. */
68
82
  export declare function memoryDocId(doc: MemoryDoc): string;
69
83
  /** Whose memory view is being resolved: the workspace dir, profile, and node
@@ -105,6 +119,13 @@ export declare function listProjectMemoryDocs(startDir?: string, profileId?: str
105
119
  * native docs are emitted before enabled-plugin docs, so native wins on the
106
120
  * caller's first-wins dedup. */
107
121
  export declare function listAllMemoryDocs(scope?: MemoryScope, quiet?: boolean, includeDescendants?: boolean): MemoryDoc[];
122
+ /** Resolve a document NAME against a `.history` tree, which mirrors the doc
123
+ * tree segment for segment with `.jsonl` in place of `.md`. Reuses the doc
124
+ * resolution rules so a log outlives its doc under the same name the doc had:
125
+ * numeric prefixes stay prefix-blind (`00-topic.md` logged at
126
+ * `.history/00-topic.jsonl` still answers to `topic`) and a bare directory
127
+ * name falls back to its INDEX log. Returns null when nothing resolves. */
128
+ export declare function resolveHistoryLogPath(historyDir: string, segments: string[]): string | null;
108
129
  export interface MemoryDocSnapshot {
109
130
  /** Every document in default scope precedence order, loaded once. */
110
131
  docs: readonly MemoryDoc[];