simt-dev 0.6.2 → 0.7.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.
Files changed (170) hide show
  1. package/CHANGELOG.md +83 -1
  2. package/README.md +46 -17
  3. package/agents/simt-agent.md +84 -0
  4. package/bin/simt.mjs +19 -6
  5. package/docs/decisions/ADR-009-skill-layout.md +1 -1
  6. package/docs/decisions/ADR-086-the-stable-prefix-comes-first-and-the-lock-records-both-digests.md +1 -1
  7. package/docs/decisions/ADR-100-a-staged-diff-is-the-last-gate-before-history-and-it-runs-inside-the-tool.md +94 -0
  8. package/docs/decisions/ADR-101-a-file-lease-belongs-to-one-session-and-the-pre-edit-hook-refuses-another.md +95 -0
  9. package/docs/decisions/ADR-102-the-two-queued-review-rows-are-closed-by-ruling.md +49 -0
  10. package/docs/decisions/ADR-103-co-projected-duplicates-merge-down.md +74 -0
  11. package/docs/decisions/ADR-104-the-pre-tool-use-policy-reads-the-command-the-way-a-shell-reads-it.md +76 -0
  12. package/docs/decisions/ADR-105-the-json-ceiling-shrinks-arrays-and-never-cuts-bytes.md +44 -0
  13. package/docs/decisions/ADR-106-the-harness-hook-runs-through-a-repo-shim.md +46 -0
  14. package/docs/decisions/ADR-107-an-install-can-name-one-harness-and-the-skipped-ones-are-named.md +51 -0
  15. package/docs/decisions/ADR-108-an-adoption-builds-the-index-and-a-command-refreshes-it-on-read.md +59 -0
  16. package/docs/decisions/ADR-109-simt-dev-is-its-own-consumer-and-the-guards-that-kept-it-from-being-one-are-retired.md +47 -0
  17. package/docs/decisions/ADR-110-the-lock-file-follows-the-manifest-name-and-the-old-name-is-read-until-the-next-write.md +43 -0
  18. package/docs/decisions/ADR-111-the-tool-ships-one-agent-rendered-per-harness-and-installs-it-with-init-and-a-command.md +75 -0
  19. package/docs/decisions/ADR-112-the-registry-is-what-simt-dev-ships-and-a-repo-owns-its-own-skills.md +81 -0
  20. package/docs/decisions/ADR-113-a-pre-push-that-carries-only-documentation-skips-the-test-gate-and-says-so.md +87 -0
  21. package/docs/decisions/ADR-114-the-bench-pairs-at-the-task-level-a-missing-cost-is-not-zero-and-an-unasked-cross-group-compare-is-refused.md +74 -0
  22. package/docs/decisions/ADR-115-the-index-stamps-its-schema-version-migrates-older-databases-in-place-and-serves-newer-ones-from-a-sidecar.md +69 -0
  23. package/docs/decisions/ADR-116-rust-support-reaches-the-example-path-the-indent-and-the-bench-runner.md +100 -0
  24. package/docs/decisions/README.md +53 -0
  25. package/package.json +4 -2
  26. package/rules/core.md +2 -2
  27. package/rules/mdc/preview-postflight.mdc +5 -6
  28. package/rules/mdc/test-gate.mdc +2 -0
  29. package/rules/stacks/android.md +10 -11
  30. package/rules/stacks/cloudflare-workers.md +1 -1
  31. package/rules/stacks/connectiq.md +2 -2
  32. package/rules/stacks/container.md +2 -0
  33. package/rules/stacks/dart.md +1 -1
  34. package/rules/stacks/deno.md +1 -1
  35. package/rules/stacks/dotnet.md +1 -1
  36. package/rules/stacks/elixir.md +1 -1
  37. package/rules/stacks/go.md +1 -1
  38. package/rules/stacks/jvm.md +4 -3
  39. package/rules/stacks/node.md +2 -2
  40. package/rules/stacks/python.md +7 -6
  41. package/rules/stacks/react-native.md +3 -5
  42. package/rules/stacks/react-web.md +9 -9
  43. package/rules/stacks/ruby.md +1 -1
  44. package/rules/stacks/rust.md +1 -1
  45. package/rules/stacks/swift.md +1 -1
  46. package/rules/stacks/typescript.md +2 -2
  47. package/rules/stacks/uv.md +3 -2
  48. package/skills/simt-commands/SKILL.md +70 -0
  49. package/src/advice/advice.mjs +7 -3
  50. package/src/agenthooks/decide.mjs +238 -15
  51. package/src/agenthooks/opencode.mjs +72 -0
  52. package/src/agenthooks/policy.mjs +81 -5
  53. package/src/agenthooks/prompt.mjs +6 -2
  54. package/src/agenthooks/render.mjs +186 -19
  55. package/src/agenthooks/shell.mjs +523 -0
  56. package/src/agenthooks/shim.mjs +55 -0
  57. package/src/agents/render.mjs +277 -0
  58. package/src/argv.mjs +62 -9
  59. package/src/bench/ablate.mjs +31 -14
  60. package/src/bench/mine.mjs +48 -1
  61. package/src/bench/probes.mjs +5 -6
  62. package/src/bench/report.mjs +243 -80
  63. package/src/bench/run.mjs +12 -11
  64. package/src/bench/variants.mjs +112 -40
  65. package/src/brief/assemble.mjs +6 -0
  66. package/src/ci/workflow.mjs +16 -7
  67. package/src/commands/agent.mjs +206 -0
  68. package/src/commands/agenthook.mjs +133 -17
  69. package/src/commands/bench.mjs +2 -1
  70. package/src/commands/ci.mjs +28 -3
  71. package/src/commands/compare.mjs +8 -7
  72. package/src/commands/completions.mjs +27 -0
  73. package/src/commands/decide.mjs +20 -6
  74. package/src/commands/doctor.mjs +367 -0
  75. package/src/commands/evidence.mjs +13 -3
  76. package/src/commands/explain.mjs +52 -0
  77. package/src/commands/gate.mjs +85 -19
  78. package/src/commands/graph.mjs +18 -5
  79. package/src/commands/help.mjs +96 -26
  80. package/src/commands/hooks.mjs +258 -67
  81. package/src/commands/ignore.mjs +152 -0
  82. package/src/commands/impact.mjs +4 -2
  83. package/src/commands/index.mjs +69 -102
  84. package/src/commands/init.mjs +239 -37
  85. package/src/commands/lease.mjs +304 -0
  86. package/src/commands/lessons.mjs +77 -9
  87. package/src/commands/map.mjs +33 -18
  88. package/src/commands/mcp.mjs +2 -2
  89. package/src/commands/pack.mjs +9 -0
  90. package/src/commands/plan.mjs +13 -1
  91. package/src/commands/plugin.mjs +17 -2
  92. package/src/commands/prbody.mjs +45 -0
  93. package/src/commands/repoindex.mjs +4 -0
  94. package/src/commands/rules.mjs +219 -105
  95. package/src/commands/shared.mjs +36 -5
  96. package/src/commands/skills.mjs +329 -206
  97. package/src/commands/skillscompile.mjs +8 -7
  98. package/src/commands/skillsinstall.mjs +112 -30
  99. package/src/commands/slice.mjs +712 -0
  100. package/src/commands/status.mjs +62 -0
  101. package/src/commands/sync.mjs +146 -36
  102. package/src/commands/tests.mjs +5 -2
  103. package/src/commands/undo.mjs +114 -0
  104. package/src/commands/upgrade.mjs +103 -9
  105. package/src/commands/why.mjs +10 -4
  106. package/src/compare/hooksstate.mjs +23 -1
  107. package/src/compare/learn.mjs +82 -57
  108. package/src/compare/parallel.mjs +50 -0
  109. package/src/compare/row.mjs +26 -1
  110. package/src/completions.mjs +196 -0
  111. package/src/decisions/dir.mjs +84 -0
  112. package/src/decisions/index.mjs +109 -11
  113. package/src/diff.mjs +254 -0
  114. package/src/distill/distill.mjs +3 -1
  115. package/src/ecosystem/node-runner.mjs +144 -0
  116. package/src/explain.mjs +270 -0
  117. package/src/federation/config.mjs +44 -0
  118. package/src/graph/load.mjs +52 -24
  119. package/src/hooks/capability.mjs +14 -1
  120. package/src/hooks/dispatcher.mjs +441 -9
  121. package/src/hooks/gates.mjs +14 -3
  122. package/src/hooks/resolve.mjs +23 -3
  123. package/src/hooks/shim.mjs +12 -7
  124. package/src/identity.mjs +67 -14
  125. package/src/ignore.mjs +133 -0
  126. package/src/index/extract.mjs +192 -11
  127. package/src/index/persist.mjs +372 -55
  128. package/src/index/store.mjs +263 -60
  129. package/src/index/tests.mjs +6 -35
  130. package/src/journal.mjs +545 -0
  131. package/src/kernel/card.mjs +37 -12
  132. package/src/kernel/checks.mjs +44 -9
  133. package/src/kernel/variants.mjs +27 -16
  134. package/src/lease.mjs +195 -0
  135. package/src/ledger/index.mjs +91 -25
  136. package/src/mcp/resources.mjs +5 -3
  137. package/src/mcp/server.mjs +86 -13
  138. package/src/mcp/tools.mjs +340 -264
  139. package/src/memory/decide.mjs +98 -38
  140. package/src/memory/lessons.mjs +180 -33
  141. package/src/name.mjs +9 -0
  142. package/src/output/index.mjs +82 -12
  143. package/src/package-version.mjs +49 -1
  144. package/src/packs/manage.mjs +13 -4
  145. package/src/plan/index.mjs +37 -8
  146. package/src/plan/template.mjs +27 -5
  147. package/src/plugins.mjs +33 -8
  148. package/src/prbody.mjs +253 -0
  149. package/src/projection/baselines.mjs +1 -1
  150. package/src/projection/disclosure.mjs +2 -2
  151. package/src/projection/loaders.mjs +41 -1
  152. package/src/projection/lock.mjs +21 -14
  153. package/src/projection/manifest.mjs +82 -21
  154. package/src/projection/markers.mjs +43 -3
  155. package/src/projection/mdc.mjs +34 -7
  156. package/src/projection/project.mjs +162 -18
  157. package/src/projection/rules.mjs +54 -3
  158. package/src/projection/shared.mjs +4 -1
  159. package/src/projection/surfaces.mjs +9 -1
  160. package/src/repo-root.mjs +69 -0
  161. package/src/resolve/body.mjs +4 -0
  162. package/src/resolve/fingerprint.mjs +5 -0
  163. package/src/resolve/local.mjs +58 -0
  164. package/src/review/critic.mjs +4 -2
  165. package/src/secrets.mjs +374 -0
  166. package/src/state.mjs +76 -0
  167. package/src/status.mjs +157 -0
  168. package/src/toolwords.mjs +5 -0
  169. package/src/tsconfig.mjs +39 -99
  170. package/templates/plan/tracker.json +1 -1
@@ -7,6 +7,8 @@ import { execFile } from 'node:child_process';
7
7
  import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises';
8
8
  import { join } from 'node:path';
9
9
  import { promisify } from 'node:util';
10
+ import { recordCreatedFile } from '../journal.mjs';
11
+ import { decisionsDir } from '../decisions/dir.mjs';
10
12
 
11
13
  const exec = promisify(execFile);
12
14
 
@@ -335,15 +337,19 @@ function renderDecisionScaffold({
335
337
  ].join('\n');
336
338
  }
337
339
 
338
- /** Scaffold a new ADR in `<root>/docs/decisions` with structured frontmatter. */
340
+ /**
341
+ * Scaffold a new ADR in the repository's decisions directory with structured frontmatter.
342
+ *
343
+ * The number is claimed with `O_EXCL`: two concurrent scaffolds that both read
344
+ * the same maximum must not land on the same number, and a check-then-write
345
+ * would let the second silently overwrite the first. On collision the number is
346
+ * re-read and the next free one taken, so the retry always terminates.
347
+ */
339
348
  export async function scaffoldDecision(root, options = {}) {
340
- const decisionsDir = options.decisionsDir ?? join(root, 'docs', 'decisions');
341
- const { number, id } = await nextDecisionNumber(decisionsDir);
349
+ const relDir = options.decisionsDir ?? (await decisionsDir(root));
350
+ const absDir = join(root, relDir);
342
351
  const title = String(options.title ?? 'Untitled decision').trim() || 'Untitled decision';
343
352
  const slug = slugifyTitle(title);
344
- const file = `${id}-${slug}.md`;
345
- const relPath = ['docs', 'decisions', file].join('/');
346
- const fullPath = join(decisionsDir, file);
347
353
  const affects = Array.isArray(options.affects)
348
354
  ? options.affects.map((p) => String(p).trim()).filter(Boolean)
349
355
  : await diffAffectedPaths(root);
@@ -351,34 +357,58 @@ export async function scaffoldDecision(root, options = {}) {
351
357
  const { entries: supersedes, errors } = parseSupersedesSpec(options.supersedes);
352
358
  if (errors.length > 0) return { ok: false, errors };
353
359
 
354
- const text = renderDecisionScaffold({
355
- id,
356
- title,
357
- date: options.date,
358
- status: options.status,
359
- affects,
360
- supersedes,
361
- });
362
-
363
- if (options.write) {
364
- await mkdir(decisionsDir, { recursive: true });
365
- await writeFile(fullPath, text, 'utf8');
360
+ // A dry run names the number without claiming it: the file must not exist
361
+ // afterwards, so no exclusive create happens here.
362
+ if (!options.write) {
363
+ const { number, id } = await nextDecisionNumber(absDir);
364
+ const file = `${id}-${slug}.md`;
365
+ return {
366
+ ok: true,
367
+ id,
368
+ number,
369
+ title,
370
+ slug,
371
+ file,
372
+ relPath: [relDir, file].join('/'),
373
+ path: join(absDir, file),
374
+ affects,
375
+ supersedes,
376
+ text: renderDecisionScaffold({ id, title, date: options.date, status: options.status, affects, supersedes }),
377
+ written: false,
378
+ };
366
379
  }
367
380
 
368
- return {
369
- ok: true,
370
- id,
371
- number,
372
- title,
373
- slug,
374
- file,
375
- relPath,
376
- path: fullPath,
377
- affects,
378
- supersedes,
379
- text,
380
- written: Boolean(options.write),
381
- };
381
+ await mkdir(absDir, { recursive: true });
382
+ for (let attempt = 0; attempt < 100; attempt += 1) {
383
+ const { number, id } = await nextDecisionNumber(absDir);
384
+ const file = `${id}-${slug}.md`;
385
+ const fullPath = join(absDir, file);
386
+ const text = renderDecisionScaffold({ id, title, date: options.date, status: options.status, affects, supersedes });
387
+ try {
388
+ await writeFile(fullPath, text, { flag: 'wx' });
389
+ } catch (error) {
390
+ if (error.code !== 'EEXIST') throw error;
391
+ continue;
392
+ }
393
+ // The exclusive create is the journal entry's `before: null`: the file did
394
+ // not exist a moment ago, by construction rather than by re-reading.
395
+ if (options.journal) recordCreatedFile(root, options.journal, fullPath, text);
396
+ return {
397
+ ok: true,
398
+ id,
399
+ number,
400
+ title,
401
+ slug,
402
+ file,
403
+ relPath: [relDir, file].join('/'),
404
+ path: fullPath,
405
+ affects,
406
+ supersedes,
407
+ text,
408
+ written: true,
409
+ };
410
+ }
411
+ return { ok: false, errors: ['could not claim a free ADR number after 100 attempts'] };
382
412
  }
383
413
 
384
414
  /** Extract partial supersession claims stated in prose (`Status:` and header paragraphs). */
@@ -410,8 +440,15 @@ function prosePartialSupersessions(body) {
410
440
  return claims;
411
441
  }
412
442
 
413
- /** Lint a single ADR document for structured supersession conformance (plan 0003 R34). */
414
- export function lintDecision(text, { file = 'ADR-000.md', allowHistorical = false } = {}) {
443
+ /**
444
+ * Lint a single ADR document for structured supersession conformance (plan 0003 R34).
445
+ *
446
+ * `historicalCutoff` exempts the first N records from the frontmatter requirement:
447
+ * they predate it, and demanding it retroactively would fail a history nobody can
448
+ * rewrite without rewriting history. Q55: the number lives in the repository's
449
+ * config (`decisions.historicalCutoff`, resolved by the caller), never in code.
450
+ */
451
+ export function lintDecision(text, { file = 'ADR-000.md', historicalCutoff = 0 } = {}) {
415
452
  const fileMatch = file !== 'ADR-000.md' ? /^(ADR-\d{3,})/i.exec(file) : null;
416
453
  const headMatch = /^#\s+(ADR-\d{3,})\b/m.exec(String(text ?? ''));
417
454
  const id = (fileMatch?.[1] ?? headMatch?.[1] ?? /^(ADR-\d{3,})/i.exec(file)?.[1] ?? '').toUpperCase() || null;
@@ -427,7 +464,7 @@ export function lintDecision(text, { file = 'ADR-000.md', allowHistorical = fals
427
464
  remedy: 'fix simt-supersedes frontmatter to use [{ adr: "ADR-NNN", ruling: N }]',
428
465
  }));
429
466
 
430
- if (allowHistorical && !fm.hasFrontmatter && num !== null && num > 0 && num <= 96) {
467
+ if (!fm.hasFrontmatter && num !== null && num > 0 && num <= historicalCutoff) {
431
468
  return { ok: findings.length === 0, findings };
432
469
  }
433
470
 
@@ -450,13 +487,36 @@ export function lintDecision(text, { file = 'ADR-000.md', allowHistorical = fals
450
487
  return { ok: findings.length === 0, findings };
451
488
  }
452
489
 
453
- /** Lint all ADR records in a decisions directory. */
454
- export async function lintDecisions(decisionsDir, { allowHistorical = false } = {}) {
490
+ /**
491
+ * Lint all ADR records in a decisions directory.
492
+ *
493
+ * Duplicate ids are a lint finding, not a silent keep-first: two files claiming
494
+ * one number means `byId` answers for only one of them, and the reader of the
495
+ * other gets a record the index cannot see. The kept record is the first in
496
+ * numeric filename order — the same one the loader keeps — so lint and load
497
+ * never name different winners.
498
+ */
499
+ export async function lintDecisions(decisionsDir, { historicalCutoff = 0 } = {}) {
455
500
  const names = (await readdir(decisionsDir).catch(() => [])).filter((n) => /^ADR-\d{3,}-.*\.md$/i.test(n)).sort();
456
501
  const findings = [];
502
+ const seen = new Map();
457
503
  for (const file of names) {
504
+ const id = /^(ADR-\d{3,})/i.exec(file)?.[1]?.toUpperCase() ?? null;
505
+ if (id !== null) {
506
+ if (seen.has(id)) {
507
+ findings.push({
508
+ file,
509
+ id,
510
+ status: 'duplicate-decision-id',
511
+ detail: `decision id ${id} is claimed by ${seen.get(id)} and ${file}; the index only sees the first`,
512
+ remedy: 'renumber the newer record to a free number, then rerun the check',
513
+ });
514
+ continue;
515
+ }
516
+ seen.set(id, file);
517
+ }
458
518
  const text = await readFile(join(decisionsDir, file), 'utf8');
459
- findings.push(...lintDecision(text, { file, allowHistorical }).findings);
519
+ findings.push(...lintDecision(text, { file, historicalCutoff }).findings);
460
520
  }
461
521
  return { ok: findings.length === 0, findings };
462
522
  }
@@ -13,20 +13,64 @@
13
13
 
14
14
  import { createHash } from 'node:crypto';
15
15
  import { execFile } from 'node:child_process';
16
- import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
16
+ import { mkdir, open, readdir, readFile, writeFile } from 'node:fs/promises';
17
17
  import { dirname, join } from 'node:path';
18
18
  import { promisify } from 'node:util';
19
19
 
20
20
  import { postDeliveryJson } from '../agenthooks/render.mjs';
21
21
  import { lessonPaths } from '../index/govern.mjs';
22
+ import { recordCreatedFile } from '../journal.mjs';
23
+ import { redactText } from '../secrets.mjs';
24
+ import { localRel, readStateFile } from '../state.mjs';
22
25
  import { splitFrontmatter } from './decide.mjs';
23
26
 
24
27
  const exec = promisify(execFile);
25
28
 
26
- export const LESSONS_FILE = '.simt/lessons.jsonl';
29
+ export const LESSONS_FILE = localRel('lessons.jsonl');
30
+ const LESSONS_FILE_LEGACY = '.simt/lessons.jsonl';
31
+ const LEDGER_REL = localRel('ledger.jsonl');
32
+ const LEDGER_REL_LEGACY = '.simt/ledger.jsonl';
27
33
  export const LESSONS_DIR = 'docs/lessons';
28
34
  export const LESSON_KEYS = new Set(['simt-trigger', 'simt-evidence', 'simt-expires']);
29
- const DEFAULT_EXPIRES = '2027-04-01';
35
+
36
+ /**
37
+ * A stored `fixDiff` is evidence, not an archive: the diff that proved the fix,
38
+ * capped so one generated-file revert cannot bloat every read of the ledger.
39
+ * Truncation keeps the head (the hunk headers that name the files) and says so.
40
+ */
41
+ const FIX_DIFF_BUDGET = 20000;
42
+
43
+ /**
44
+ * The default expiry is relative to the promotion, not a date in the source:
45
+ * a fixed date rots (every lesson written today expires on someone else's
46
+ * schedule) and a missing one lives forever because nothing reads the field.
47
+ * Half a year is long enough to recur and short enough to re-earn.
48
+ */
49
+ const EXPIRY_DAYS = 180;
50
+
51
+ export function defaultExpires(from = new Date()) {
52
+ const at = new Date(from);
53
+ at.setUTCDate(at.getUTCDate() + EXPIRY_DAYS);
54
+ return at.toISOString().slice(0, 10);
55
+ }
56
+
57
+ /**
58
+ * Whether a `simt-expires` value has passed. Unparseable values are NOT expired:
59
+ * a malformed date is a data error the reader should see, and hiding the lesson
60
+ * would turn it into a silent one (review 0003 Q36).
61
+ *
62
+ * @param {string|null|undefined} expires
63
+ * @param {Date} [now]
64
+ * @returns {boolean}
65
+ */
66
+ export function isExpired(expires, now = new Date()) {
67
+ if (expires === null || expires === undefined || String(expires).trim() === '') return false;
68
+ const at = new Date(String(expires).trim());
69
+ if (Number.isNaN(at.getTime())) return false;
70
+ const today = new Date(now);
71
+ today.setUTCHours(0, 0, 0, 0);
72
+ return at.getTime() < today.getTime();
73
+ }
30
74
 
31
75
  const STOP_WORDS = new Set([
32
76
  'the', 'and', 'for', 'with', 'that', 'this', 'from', 'into', 'when', 'were',
@@ -143,13 +187,14 @@ function slugify(text) {
143
187
  * @returns {Promise<{candidates: Array<object>, errors: string[]}>}
144
188
  */
145
189
  export async function readLessonCandidates(root) {
190
+ // The pre-move path is a fallback for one release (review 0003 Q19).
146
191
  let text;
147
192
  try {
148
- text = await readFile(join(root, LESSONS_FILE), 'utf8');
193
+ ({ text } = await readStateFile(root, LESSONS_FILE, [LESSONS_FILE_LEGACY]));
149
194
  } catch (error) {
150
- if (error.code === 'ENOENT') return { candidates: [], errors: [] };
151
195
  throw error;
152
196
  }
197
+ if (text === null) return { candidates: [], errors: [] };
153
198
  const candidates = [];
154
199
  const errors = [];
155
200
  for (const [i, raw] of text.split('\n').entries()) {
@@ -175,15 +220,69 @@ async function writeLessonCandidates(root, candidates) {
175
220
  await writeFile(full, body ? `${body}\n` : '', 'utf8');
176
221
  }
177
222
 
223
+ /**
224
+ * Append one record with `O_APPEND`: the ledger is written by concurrent
225
+ * processes (gates, hooks, the CLI), and a read-modify-write loses one of two
226
+ * simultaneous appends. The position is the kernel's, not a length read earlier.
227
+ *
228
+ * @param {string} root
229
+ * @param {object} record
230
+ */
231
+ async function appendCandidateLine(root, record) {
232
+ const full = join(root, LESSONS_FILE);
233
+ await mkdir(dirname(full), { recursive: true });
234
+ const handle = await open(full, 'a');
235
+ try {
236
+ await handle.appendFile(`${JSON.stringify(record)}\n`, 'utf8');
237
+ } finally {
238
+ await handle.close();
239
+ }
240
+ }
241
+
242
+ /**
243
+ * The candidate id is a content hash, not a counter. Ids were `c-${length+1}`:
244
+ * two processes appending concurrently computed the same next index, and a
245
+ * hand-edited or truncated file renumbered everything after it. The hash covers
246
+ * the fields that make a candidate distinct, so the same candidate always maps
247
+ * to the same id and two different ones never share it (review 0003 Q36).
248
+ *
249
+ * @param {{kind: string, signature: string, evidence: string[], cmd: string|null}} parts
250
+ * @returns {string}
251
+ */
252
+ export function lessonIdFor({ kind, signature, evidence = [], cmd = null }) {
253
+ const digest = createHash('sha256')
254
+ .update([kind, signature, ...evidence, cmd ?? ''].join('\n'), 'utf8')
255
+ .digest('hex');
256
+ return `l-${digest.slice(0, 12)}`;
257
+ }
258
+
259
+ /**
260
+ * Truncate an unbounded diff to the evidence budget, keeping the head: the hunk
261
+ * headers that name the files are the part a reader acts on.
262
+ *
263
+ * @param {string} diff
264
+ * @returns {string}
265
+ */
266
+ function capFixDiff(diff) {
267
+ const text = String(diff ?? '');
268
+ if (text.length <= FIX_DIFF_BUDGET) return text;
269
+ return `${text.slice(0, FIX_DIFF_BUDGET)}\n… (truncated to ${FIX_DIFF_BUDGET} chars)`;
270
+ }
271
+
178
272
  function sameEvidence(a = [], b = []) {
179
273
  if (a.length !== b.length) return false;
180
274
  return a.every((v, i) => v === b[i]);
181
275
  }
182
276
 
183
277
  /**
184
- * Append one deterministic candidate to `.simt/lessons.jsonl`, deduplicating by
278
+ * Append one deterministic candidate to the lessons ledger, deduplicating by
185
279
  * `(kind, signature, evidence)`.
186
280
  *
281
+ * The stored text is redacted before anything else: candidates keep the raw
282
+ * commands that failed, and a command can carry a token. Redaction runs before
283
+ * the signature is hashed so the same failure redacts to the same signature
284
+ * (review 0003 Q36).
285
+ *
187
286
  * @param {string} root
188
287
  * @param {object} candidate
189
288
  * @returns {Promise<object>}
@@ -191,15 +290,22 @@ function sameEvidence(a = [], b = []) {
191
290
  export async function appendLessonCandidate(root, candidate) {
192
291
  const { candidates } = await readLessonCandidates(root);
193
292
  const kind = String(candidate.kind ?? 'fail-then-fix');
194
- const errorLine = normaliseErrorLine(candidate.errorLine ?? firstErrorLine(candidate.output ?? candidate.summary ?? ''));
293
+ // Redact first: the signature must be a function of the stored bytes, not of
294
+ // a secret nobody can reproduce (and must never be stored).
295
+ const errorLine = redactText(
296
+ normaliseErrorLine(candidate.errorLine ?? firstErrorLine(candidate.output ?? candidate.summary ?? '')),
297
+ );
195
298
  const signature = candidate.signature ?? errorSignature(errorLine);
196
- const evidence = Array.isArray(candidate.evidence) ? candidate.evidence.map(String).filter(Boolean) : [];
299
+ const evidence = Array.isArray(candidate.evidence)
300
+ ? candidate.evidence.map((e) => redactText(String(e)).trim()).filter(Boolean)
301
+ : [];
302
+ const cmd = candidate.cmd === null || candidate.cmd === undefined ? null : redactText(String(candidate.cmd));
197
303
 
198
304
  const existing = candidates.find(
199
305
  (c) =>
200
306
  c.kind === kind &&
201
307
  c.signature === signature &&
202
- (sameEvidence(c.evidence ?? [], evidence) || (c.cmd && candidate.cmd && c.cmd === candidate.cmd && c.evidence?.[0] === evidence[0])),
308
+ (sameEvidence(c.evidence ?? [], evidence) || (c.cmd && cmd && c.cmd === cmd && c.evidence?.[0] === evidence[0])),
203
309
  );
204
310
  if (existing) return existing;
205
311
 
@@ -208,28 +314,26 @@ export async function appendLessonCandidate(root, candidate) {
208
314
  : [];
209
315
  const keywords = Array.isArray(candidate.keywords) && candidate.keywords.length > 0
210
316
  ? [...new Set(candidate.keywords.map(String).filter(Boolean))]
211
- : extractKeywords(`${errorLine} ${candidate.cmd ?? ''}`, paths);
317
+ : extractKeywords(`${errorLine} ${cmd ?? ''}`, paths);
212
318
 
213
- const nextIndex = candidates.length + 1;
214
319
  const record = {
215
320
  v: 1,
216
- id: candidate.id ?? `c-${String(nextIndex).padStart(4, '0')}`,
321
+ id: candidate.id ?? lessonIdFor({ kind, signature, evidence, cmd }),
217
322
  ts: candidate.ts ?? new Date().toISOString().replace(/\.\d{3}Z$/, 'Z'),
218
323
  kind,
219
324
  signature,
220
325
  errorLine,
221
- summary: String(candidate.summary ?? (errorLine || `${kind} candidate`)).trim(),
222
- cmd: candidate.cmd ?? null,
326
+ summary: redactText(String(candidate.summary ?? (errorLine || `${kind} candidate`))).trim(),
327
+ cmd,
223
328
  paths,
224
329
  keywords,
225
330
  evidence,
226
- fixDiff: String(candidate.fixDiff ?? ''),
331
+ fixDiff: capFixDiff(redactText(candidate.fixDiff ?? '')),
227
332
  status: candidate.status ?? 'candidate',
228
333
  promotedTo: candidate.promotedTo ?? null,
229
334
  };
230
335
 
231
- candidates.push(record);
232
- await writeLessonCandidates(root, candidates);
336
+ await appendCandidateLine(root, record);
233
337
  return record;
234
338
  }
235
339
 
@@ -306,8 +410,8 @@ export async function captureFromLedger(root, entries = null) {
306
410
  let list = entries;
307
411
  if (!Array.isArray(list)) {
308
412
  try {
309
- const raw = await readFile(join(root, '.simt', 'ledger.jsonl'), 'utf8');
310
- list = raw
413
+ const { text } = await readStateFile(root, LEDGER_REL, [LEDGER_REL_LEGACY]);
414
+ list = (text ?? '')
311
415
  .split('\n')
312
416
  .map((s) => s.trim())
313
417
  .filter(Boolean)
@@ -481,12 +585,13 @@ export async function recordHookDenial(root, denial) {
481
585
  * @returns {string}
482
586
  */
483
587
  export function renderLessonMarkdown(candidate, options = {}) {
588
+ // No `**` fallback: a trigger with no paths is a promotion the caller
589
+ // refused, not a lesson about everything. `promoteLesson` rejects those;
590
+ // this renders exactly the paths it was given (review 0003 Q36).
484
591
  const paths =
485
592
  Array.isArray(options.paths) && options.paths.length > 0
486
593
  ? options.paths
487
- : Array.isArray(candidate.paths) && candidate.paths.length > 0
488
- ? candidate.paths
489
- : ['**'];
594
+ : (Array.isArray(candidate.paths) ? candidate.paths : []);
490
595
  const keywords =
491
596
  Array.isArray(options.keywords) && options.keywords.length > 0
492
597
  ? options.keywords
@@ -494,7 +599,7 @@ export function renderLessonMarkdown(candidate, options = {}) {
494
599
  ? candidate.keywords
495
600
  : extractKeywords(candidate.errorLine ?? candidate.summary ?? '', paths);
496
601
  const evidence = Array.isArray(candidate.evidence) ? candidate.evidence : [];
497
- const expires = options.expires ?? DEFAULT_EXPIRES;
602
+ const expires = options.expires ?? defaultExpires();
498
603
  const title = String(options.title ?? candidate.summary ?? candidate.errorLine ?? 'Lesson').trim();
499
604
 
500
605
  const pathsLiteral = paths.map((p) => JSON.stringify(String(p))).join(', ');
@@ -528,7 +633,7 @@ export function renderLessonMarkdown(candidate, options = {}) {
528
633
  * @param {string} text
529
634
  * @returns {object}
530
635
  */
531
- export function parseLessonFile(id, text) {
636
+ export function parseLessonFile(id, text, now = new Date()) {
532
637
  const { front: rawFront, body: rawBody } = splitFrontmatter(text);
533
638
  const front = rawFront ?? '';
534
639
  const body = rawBody.trim();
@@ -546,6 +651,7 @@ export function parseLessonFile(id, text) {
546
651
  : [];
547
652
  const expMatch = /^simt-expires:\s*(\S+)/m.exec(front);
548
653
  const expires = expMatch ? expMatch[1].trim() : null;
654
+ const expired = isExpired(expires, now);
549
655
 
550
656
  const titleMatch = /^#\s+(.+)$/m.exec(body);
551
657
  const title = titleMatch ? titleMatch[1].trim() : id;
@@ -566,16 +672,23 @@ export function parseLessonFile(id, text) {
566
672
  keywords,
567
673
  evidence,
568
674
  expires,
675
+ expired,
569
676
  };
570
677
  }
571
678
 
572
679
  /**
573
- * List all promoted lessons in `docs/lessons/*.md`.
680
+ * List the promoted lessons in `docs/lessons/*.md` that are still in force.
681
+ *
682
+ * `simt-expires` is read here, once, so every consumer — recall, `why`, the
683
+ * hook, the bench, `lessons list` — agrees on what has expired. Nothing was
684
+ * reading the field, so expired lessons kept firing indefinitely (review 0003
685
+ * Q36). Pass `includeExpired` to see the retired ones with `expired: true`.
574
686
  *
575
687
  * @param {string} root
688
+ * @param {{includeExpired?: boolean, now?: Date}} [options]
576
689
  * @returns {Promise<Array<object>>}
577
690
  */
578
- export async function listPromotedLessons(root) {
691
+ export async function listPromotedLessons(root, { includeExpired = false, now = new Date() } = {}) {
579
692
  const dir = join(root, LESSONS_DIR);
580
693
  let names = [];
581
694
  try {
@@ -587,7 +700,8 @@ export async function listPromotedLessons(root) {
587
700
  for (const name of names) {
588
701
  const text = await readFile(join(dir, name), 'utf8');
589
702
  const id = name.replace(/\.md$/, '');
590
- lessons.push(parseLessonFile(id, text));
703
+ const lesson = parseLessonFile(id, text, now);
704
+ if (!lesson.expired || includeExpired) lessons.push(lesson);
591
705
  }
592
706
  return lessons;
593
707
  }
@@ -612,17 +726,20 @@ async function nextLessonNumber(root) {
612
726
  }
613
727
 
614
728
  /**
615
- * Promote a lesson candidate from `.simt/lessons.jsonl` to `docs/lessons/NNNN-<slug>.md`.
729
+ * Promote a lesson candidate from the lessons ledger to `docs/lessons/NNNN-<slug>.md`.
730
+ *
731
+ * Capture is not promotion's business: `promoteLesson` used to scan the ledger
732
+ * first, so a preview (`review` without `--yes`) wrote to the store it was
733
+ * only reading. Capture is an explicit `lessons capture` now (review 0003 Q36).
616
734
  *
617
735
  * @param {string} root
618
- * @param {{id?: string, apply?: boolean, paths?: string[], expires?: string, title?: string}} [options]
736
+ * @param {{id?: string, apply?: boolean, paths?: string[], expires?: string, title?: string, journal?: object[]}} [options]
619
737
  * @returns {Promise<{ok: boolean, error?: string, candidate?: object, path?: string, markdown?: string, written?: boolean}>}
620
738
  */
621
739
  export async function promoteLesson(root, options = {}) {
622
- await captureFromLedger(root);
623
740
  const { candidates } = await readLessonCandidates(root);
624
741
  if (candidates.length === 0) {
625
- return { ok: false, error: 'no lesson candidates in .simt/lessons.jsonl' };
742
+ return { ok: false, error: `no lesson candidates in ${LESSONS_FILE}; capture some with: lessons capture` };
626
743
  }
627
744
 
628
745
  let target = null;
@@ -633,19 +750,48 @@ export async function promoteLesson(root, options = {}) {
633
750
  if (!target) {
634
751
  return { ok: false, error: `no lesson candidate matching "${options.id}"` };
635
752
  }
753
+ if (target.status === 'promoted') {
754
+ return { ok: false, error: `lesson candidate ${target.id} is already promoted to ${target.promotedTo}` };
755
+ }
636
756
  } else {
637
- target = candidates.find((c) => c.status !== 'promoted') ?? candidates[0];
757
+ // A promoted candidate is done: falling back to it re-promotes the same
758
+ // lesson under a new number, which is how one candidate became 0001/0002/0003.
759
+ target = candidates.find((c) => c.status !== 'promoted') ?? null;
760
+ if (!target) {
761
+ return { ok: false, error: 'nothing to promote: every candidate is already promoted' };
762
+ }
763
+ }
764
+
765
+ // No `**` default: a trigger that watches every path fires on every failure,
766
+ // which is the same as firing on none. A pathless candidate needs `--paths`.
767
+ const paths = Array.isArray(options.paths) && options.paths.length > 0
768
+ ? options.paths
769
+ : (Array.isArray(target.paths) ? target.paths : []);
770
+ if (paths.length === 0) {
771
+ return { ok: false, error: `lesson candidate ${target.id} names no paths; pass --paths to say what it governs` };
638
772
  }
639
773
 
640
774
  const num = await nextLessonNumber(root);
641
775
  const slug = slugify(options.title ?? target.errorLine ?? target.summary ?? target.kind);
642
776
  const relPath = `${LESSONS_DIR}/${num}-${slug}.md`;
643
- const markdown = renderLessonMarkdown(target, options);
777
+ const markdown = renderLessonMarkdown(target, { ...options, paths, expires: options.expires ?? defaultExpires() });
778
+
779
+ // The `--diff` before-side, read before anything is written so the diff is true
780
+ // in preview and in apply alike (R50). Always a creation in practice — the
781
+ // number is fresh — but a hand-placed file at the same path would be
782
+ // overwritten, and a diff that claimed a creation there would lie about it.
783
+ let before = null;
784
+ try {
785
+ before = await readFile(join(root, relPath), 'utf8');
786
+ } catch (error) {
787
+ if (error.code !== 'ENOENT') throw error;
788
+ }
644
789
 
645
790
  if (options.apply) {
646
791
  const fullPath = join(root, relPath);
647
792
  await mkdir(dirname(fullPath), { recursive: true });
648
793
  await writeFile(fullPath, markdown, 'utf8');
794
+ if (Array.isArray(options.journal)) recordCreatedFile(root, options.journal, join(root, relPath), markdown);
649
795
  target.status = 'promoted';
650
796
  target.promotedTo = relPath;
651
797
  await writeLessonCandidates(root, candidates);
@@ -656,6 +802,7 @@ export async function promoteLesson(root, options = {}) {
656
802
  candidate: target,
657
803
  path: relPath,
658
804
  markdown,
805
+ before,
659
806
  written: Boolean(options.apply),
660
807
  };
661
808
  }
package/src/name.mjs CHANGED
@@ -32,6 +32,15 @@ export const MANIFEST_FILE = 'simt.json';
32
32
  /** What the manifest file was called, and still is in every adopted repo. */
33
33
  export const MANIFEST_LEGACY = 'simt-dev.json';
34
34
 
35
+ /** The projection lock: the input and output digests a frozen check compares, and the
36
+ * locked `variants` winners `simt bench variants` records (ADR-086). */
37
+ export const LOCK_FILE = 'simt.lock.json';
38
+
39
+ /** What the lock was called. Read, not written: an adopted repo's lock under the old
40
+ * name may hold variant winners nothing can regenerate, so it is read until the next
41
+ * write migrates it (ADR-110). */
42
+ export const LOCK_LEGACY = 'simt-dev.lock.json';
43
+
35
44
  /**
36
45
  * The namespace the generated region writes into a repository's files: the begin and end
37
46
  * markers, and the provenance header inside them. It follows the command, like the