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
@@ -14,6 +14,8 @@ import { readFile, stat } from 'node:fs/promises';
14
14
  import { relative, resolve, sep } from 'node:path';
15
15
  import { promisify } from 'node:util';
16
16
 
17
+ import { CLI } from '../name.mjs';
18
+
17
19
  const exec = promisify(execFile);
18
20
 
19
21
  export const CHECK_MARKER = 'simt-check';
@@ -48,11 +50,32 @@ export function checkDefect(body) {
48
50
  return null;
49
51
  }
50
52
 
53
+ /**
54
+ * What a runnable check enforces, in the reader's terms. The bare checker id is
55
+ * vocabulary for the manifest author; the projected region is read by a repository
56
+ * that has no manifest and no checker registry.
57
+ */
58
+ function checkSummary(check) {
59
+ const arg = check.arg;
60
+ switch (check.name) {
61
+ case 'max-file-lines':
62
+ return `by \`${CLI} check\`: more than ${DEFAULT_WARN} lines warns, more than ${DEFAULT_ERROR} fails (\`limits.fileLines\`)`;
63
+ case 'forbid-regex':
64
+ return `added lines matching \`${arg}\` are rejected`;
65
+ case 'forbidden-path-edit':
66
+ return `\`${arg}\` is not to be edited`;
67
+ case 'required-file':
68
+ return `\`${arg}\` must exist`;
69
+ default:
70
+ return check.name;
71
+ }
72
+ }
73
+
51
74
  /**
52
75
  * Replace each runnable check with one `Enforced:` line. A body with no marker
53
76
  * is returned unchanged, so a rule nobody marked does not move a byte.
54
77
  *
55
- * @returns {{body: string, enforced: {name: string}[], changed: boolean}}
78
+ * @returns {{body: string, enforced: {name: string, summary: string}[], changed: boolean}}
56
79
  */
57
80
  export function foldChecks(body) {
58
81
  if (checkDefect(body) !== null || !HAS_MARKER.test(body)) {
@@ -73,8 +96,9 @@ export function foldChecks(body) {
73
96
  }
74
97
  const indent = ITEM_START.exec(unit)?.[1] ?? '';
75
98
  const bullet = ITEM_START.test(unit) ? '- ' : '';
76
- kept.push(`${indent}${bullet}Enforced: ${parsed.name}`);
77
- enforced.push({ name: parsed.name });
99
+ const summary = checkSummary(parsed);
100
+ kept.push(`${indent}${bullet}Enforced: ${summary}`);
101
+ enforced.push({ name: parsed.name, summary });
78
102
  }
79
103
  const next = kept.join('\n');
80
104
  return { body: next, enforced, changed: next !== body };
@@ -130,15 +154,24 @@ async function runOne(root, check, manifest) {
130
154
  return forbiddenPath(check, diff);
131
155
  }
132
156
 
157
+ const TEXT_EXTENSIONS = new Set(['.md', '.txt', '.doc', '.docx', '.rst', '.adoc', '.tex']);
158
+
159
+ function isTextFile(rel) {
160
+ const segments = rel.split('/');
161
+ if (segments.includes('doc') || segments.includes('docs')) return true;
162
+ const dot = rel.lastIndexOf('.');
163
+ return dot !== -1 && TEXT_EXTENSIONS.has(rel.slice(dot).toLowerCase());
164
+ }
165
+
133
166
  async function fileLines(root, check, manifest) {
134
167
  const limits = manifest?.limits?.fileLines ?? {};
135
- const generated = limits.generated;
136
- if (generated !== undefined && !Array.isArray(generated)) {
168
+ const exclude = limits.exclude;
169
+ if (exclude !== undefined && !Array.isArray(exclude)) {
137
170
  return [{
138
171
  file: 'simt.json',
139
172
  status: 'invalid',
140
- detail: 'limits.fileLines.generated must be a list of paths',
141
- remedy: 'name each generated path, or remove the key',
173
+ detail: 'limits.fileLines.exclude must be a list of paths',
174
+ remedy: 'name each excluded path, or remove the key',
142
175
  }];
143
176
  }
144
177
  const warnAt = Number.isInteger(limits.warn) ? limits.warn : DEFAULT_WARN;
@@ -146,13 +179,15 @@ async function fileLines(root, check, manifest) {
146
179
  const files = await tracked(root);
147
180
  if (files === null) return [];
148
181
  const findings = [];
149
- const prefixes = Array.isArray(generated) ? generated.filter((g) => typeof g === 'string' && g !== '') : [];
182
+ const prefixes = Array.isArray(exclude) ? exclude.filter((g) => typeof g === 'string' && g !== '') : [];
183
+ const skipText = limits.excludeTextFiles === true;
150
184
  for (const rel of files) {
151
185
  // A committed dependency is not a file this repository wrote. Counting it
152
186
  // fails every install of this package, because the package's own sources
153
187
  // are longer than the ceiling the consumer declared.
154
188
  if (rel.split('/').includes('node_modules')) continue;
155
189
  if (exempt(rel, prefixes)) continue;
190
+ if (skipText && isTextFile(rel)) continue;
156
191
  let text;
157
192
  try {
158
193
  text = await readFile(resolve(root, rel));
@@ -170,7 +205,7 @@ async function fileLines(root, check, manifest) {
170
205
  file: rel,
171
206
  status: 'file-lines',
172
207
  detail: `${lines} lines, over the ceiling of ${errorAt}`,
173
- remedy: 'split the file, or name its generator in limits.fileLines.generated',
208
+ remedy: 'split the file, or name it in limits.fileLines.exclude',
174
209
  });
175
210
  } else if (lines > warnAt) {
176
211
  findings.push({
@@ -4,34 +4,45 @@
4
4
  // A rule line or list item may carry `<!-- simt-variants: <slot> <spec> -->`.
5
5
  // When unlocked, projection uses the baseline variant (`a`, or the line's own
6
6
  // prose) and strips the marker so `simt-` annotations never reach a reader.
7
- // When `simt-dev.lock.json` records a locked winner for `<ruleId>.<slot>`,
7
+ // When `simt.lock.json` records a locked winner for `<ruleId>.<slot>`,
8
8
  // projection deterministically renders that winning variant.
9
9
 
10
10
  import { readFile } from 'node:fs/promises';
11
11
  import { join } from 'node:path';
12
12
 
13
+ import { LOCK_FILE, LOCK_LEGACY } from '../name.mjs';
14
+
13
15
  export const VARIANTS_MARKER = 'simt-variants';
14
- const LOCK_FILE_NAME = 'simt-dev.lock.json';
15
16
 
16
17
  /**
17
- * Read the locked `variants` map from `simt-dev.lock.json` in `root`, or `{}` when absent.
18
+ * Read the locked `variants` map from `simt.lock.json` in `root`, or `{}` when absent.
19
+ * A lock under the legacy name is read until the next write migrates it (ADR-110); a
20
+ * lock that exists but does not parse is damage, and damage is not repaired by an
21
+ * older file.
18
22
  */
19
23
  export async function readLockedVariants(root) {
20
24
  if (!root) return {};
21
- try {
22
- const raw = await readFile(join(root, LOCK_FILE_NAME), 'utf8');
23
- const parsed = JSON.parse(raw);
24
- if (
25
- parsed &&
26
- typeof parsed === 'object' &&
27
- parsed.variants &&
28
- typeof parsed.variants === 'object' &&
29
- !Array.isArray(parsed.variants)
30
- ) {
31
- return parsed.variants;
25
+ for (const name of [LOCK_FILE, LOCK_LEGACY]) {
26
+ let raw;
27
+ try {
28
+ raw = await readFile(join(root, name), 'utf8');
29
+ } catch {
30
+ continue; // no lock under this name; try the next one
31
+ }
32
+ try {
33
+ const parsed = JSON.parse(raw);
34
+ if (
35
+ parsed &&
36
+ typeof parsed === 'object' &&
37
+ parsed.variants &&
38
+ typeof parsed.variants === 'object' &&
39
+ !Array.isArray(parsed.variants)
40
+ ) {
41
+ return parsed.variants;
42
+ }
43
+ } catch {
44
+ return {};
32
45
  }
33
- } catch {
34
- // Missing or unreadable lockfile falls back to baseline variants.
35
46
  }
36
47
  return {};
37
48
  }
package/src/lease.mjs ADDED
@@ -0,0 +1,195 @@
1
+ // File leases: one path, one session, for the length of a TTL (plan 0003 R54,
2
+ // ADR-101).
3
+ //
4
+ // The data is deliberately dumb: one JSON file beside the index, absolute expiry
5
+ // timestamps, no lock. Sessions claim paths with `simt lease <paths>` and the
6
+ // pre-edit hook reads the same file to refuse an edit from anyone who is not the
7
+ // holder. Two writers racing loses one write rather than corrupting the file, and a
8
+ // lost write is a claim the next run re-makes — anything stronger would be a
9
+ // coordination system built around a file that a TTL already cleans up.
10
+ //
11
+ // What is NOT tolerated is an unreadable file: `readLeases` reports that as an error
12
+ // rather than answering "no leases", because a claim file that stopped parsing is the
13
+ // one state in which silently forgetting every holder would hand a routing-around
14
+ // path to the session that broke it. Which way that fails is the caller's decision —
15
+ // the hook refuses edits, the command refuses to write (ADR-101 rulings 4).
16
+
17
+ import { existsSync, realpathSync } from 'node:fs';
18
+ import { mkdir, open, rename, stat, unlink, writeFile } from 'node:fs/promises';
19
+ import { dirname, join, relative, resolve, sep } from 'node:path';
20
+
21
+ import { localRel, readStateFile } from './state.mjs';
22
+
23
+ /** Where the claims live, in every repository: beside the index and the ledger. */
24
+ export const LEASE_FILE = localRel('leases.json');
25
+ const LEASE_FILE_LEGACY = '.simt/leases.json';
26
+
27
+ /** The schema the file is written with; a reader that does not know it refuses. */
28
+ const LEASE_SCHEMA = 1;
29
+
30
+ /**
31
+ * How long a claim lives when the caller does not say.
32
+ *
33
+ * An hour: longer than one focused task step, short enough that a session which
34
+ * crashed mid-edit does not hold a path for the rest of the day.
35
+ */
36
+ export const DEFAULT_TTL_SECONDS = 3600;
37
+
38
+ /**
39
+ * The one spelling a path has for lease matching: repository-relative, symlinks
40
+ * resolved, case-folded when the repository's git compares case-insensitively, or
41
+ * null when the path is not inside the repository at all.
42
+ *
43
+ * A payload that reaches a file through a symlinked directory spells it one way,
44
+ * the claim recorded for the file spells it the other, and on a case-insensitive
45
+ * volume the two spellings that differ in case are the same file (review 0003
46
+ * Q16). Both sides of a comparison go through this, so a claim and an edit meet
47
+ * in the same spelling whatever road each one took to get there.
48
+ *
49
+ * The real path of the deepest existing ancestor is joined with the remainder,
50
+ * because `realpath` on a file that does not exist yet — the normal state of a
51
+ * write payload — would refuse. The root itself leases nothing — there is no
52
+ * edit that "is" the repository — so it answers null like a path that escapes it.
53
+ */
54
+ export function canonicalLeasePath(root, input, { ignoreCase = false } = {}) {
55
+ const base = resolve(root, String(input ?? ''));
56
+ let anchor = base;
57
+ while (anchor !== resolve(anchor, '..')) {
58
+ if (existsSync(anchor)) break;
59
+ anchor = dirname(anchor);
60
+ }
61
+ let realAnchor;
62
+ try {
63
+ realAnchor = realpathSync(anchor);
64
+ } catch {
65
+ realAnchor = anchor;
66
+ }
67
+ const rest = relative(anchor, base);
68
+ const abs = rest === '' ? realAnchor : join(realAnchor, rest);
69
+ let realRoot;
70
+ try {
71
+ realRoot = realpathSync(resolve(root));
72
+ } catch {
73
+ realRoot = resolve(root);
74
+ }
75
+ const rel = relative(realRoot, abs);
76
+ if (rel === '' || rel === '..' || rel.startsWith(`..${sep}`)) return null;
77
+ const slashed = rel.split(sep).join('/');
78
+ return ignoreCase ? slashed.toLowerCase() : slashed;
79
+ }
80
+
81
+ /**
82
+ * Read the claims.
83
+ *
84
+ * Missing is `{ leases: [] }` — a repository that never leased anything is the
85
+ * default state, not a failure. Everything else (permissions, a half-written file, a
86
+ * hand edit that no longer parses) comes back as `error` with no leases, and the
87
+ * caller decides which way that fails.
88
+ *
89
+ * @returns {Promise<{path: string, leases: object[], error: string|null}>}
90
+ */
91
+ export async function readLeases(root) {
92
+ const path = join(root, LEASE_FILE);
93
+ // The pre-move path is a fallback for one release (review 0003 Q19).
94
+ let text;
95
+ try {
96
+ ({ text } = await readStateFile(root, LEASE_FILE, [LEASE_FILE_LEGACY]));
97
+ } catch (error) {
98
+ return { path, leases: [], error: `${LEASE_FILE} could not be read (${error.message})` };
99
+ }
100
+ if (text === null) return { path, leases: [], error: null };
101
+ try {
102
+ const parsed = JSON.parse(text);
103
+ if (parsed === null || typeof parsed !== 'object' || !Array.isArray(parsed.leases)) {
104
+ return { path, leases: [], error: `${LEASE_FILE} is not a lease document (no leases array)` };
105
+ }
106
+ // A malformed entry is dropped rather than trusted: an expiry that is not a number
107
+ // would otherwise live forever, which is the opposite of what a TTL promises.
108
+ const leases = parsed.leases.filter(
109
+ (l) => l !== null && typeof l === 'object' && typeof l.path === 'string' &&
110
+ typeof l.session === 'string' && Number.isFinite(l.expiresAt),
111
+ );
112
+ return { path, leases, error: null };
113
+ } catch (error) {
114
+ return { path, leases: [], error: `${LEASE_FILE} is not valid JSON (${error.message})` };
115
+ }
116
+ }
117
+
118
+ /**
119
+ * The claims still live at `now`, sorted by path so two reads of the same file
120
+ * answer in the same order.
121
+ *
122
+ * Expiry is evaluated by the reader, never swept: the hook that decides an edit is
123
+ * the moment a TTL has to be honoured, and a sweeper would be a second process whose
124
+ * absence would otherwise change nothing.
125
+ */
126
+ export function activeLeases({ leases } = { leases: [] }, now = Date.now()) {
127
+ return leases.filter((l) => l.expiresAt > now).sort((a, b) => a.path.localeCompare(b.path));
128
+ }
129
+
130
+ /**
131
+ * Replace the claim file wholesale, sorted by path for deterministic bytes.
132
+ *
133
+ * Not journaled for `undo`: the file is local coordination state like the check
134
+ * cache, and restoring yesterday's holders would undo claims other sessions are
135
+ * relying on right now.
136
+ */
137
+ export async function writeLeases(root, leases) {
138
+ const path = join(root, LEASE_FILE);
139
+ const sorted = [...leases].sort((a, b) => a.path.localeCompare(b.path));
140
+ const body = `${JSON.stringify({ schemaVersion: LEASE_SCHEMA, leases: sorted }, null, 2)}\n`;
141
+ await mkdir(dirname(path), { recursive: true });
142
+ // Atomic against a concurrent writer and a concurrent reader: a half-written
143
+ // claim file fails closed for every edit (review 0003 Q20).
144
+ const tmp = `${path}.tmp-${process.pid}`;
145
+ await writeFile(tmp, body, 'utf8');
146
+ await rename(tmp, path);
147
+ return path;
148
+ }
149
+
150
+ /** A lock is stale when its holder died holding it, not when it is merely slow. */
151
+ const LOCK_STALE_MS = 10_000;
152
+ const LOCK_WAIT_MS = 10_000;
153
+
154
+ /**
155
+ * Run `fn` with the claim file held: an `O_EXCL` lockfile (stale ones reaped),
156
+ * the read-modify-write re-done inside, the lock released afterwards.
157
+ *
158
+ * Twelve concurrent claims stored seven before this: every claimant read the
159
+ * same file, each wrote back only what it had seen. Re-reading under the lock
160
+ * closes that race (review 0003 Q20).
161
+ */
162
+ export async function withLeaseLock(root, fn) {
163
+ const lockPath = join(root, `${LEASE_FILE}.lock`);
164
+ await mkdir(dirname(lockPath), { recursive: true });
165
+ const deadline = Date.now() + LOCK_WAIT_MS;
166
+ for (;;) {
167
+ try {
168
+ const fd = await open(lockPath, 'wx');
169
+ await fd.close();
170
+ break;
171
+ } catch (error) {
172
+ if (error.code !== 'EEXIST') throw error;
173
+ let stale = false;
174
+ try {
175
+ const st = await stat(lockPath);
176
+ stale = Date.now() - st.mtimeMs > LOCK_STALE_MS;
177
+ } catch {
178
+ stale = true;
179
+ }
180
+ if (stale) {
181
+ await unlink(lockPath).catch(() => {});
182
+ continue;
183
+ }
184
+ if (Date.now() > deadline) {
185
+ throw new Error(`could not take the lease lock; remove ${lockPath} if no lease run is active`);
186
+ }
187
+ await new Promise((resolve) => setTimeout(resolve, 25));
188
+ }
189
+ }
190
+ try {
191
+ return await fn();
192
+ } finally {
193
+ await unlink(lockPath).catch(() => {});
194
+ }
195
+ }
@@ -7,26 +7,31 @@
7
7
 
8
8
  import { createHash } from 'node:crypto';
9
9
  import { execFile } from 'node:child_process';
10
- import { appendFile, mkdir, mkdtemp, readFile, rm } from 'node:fs/promises';
10
+ import { appendFile, mkdir, mkdtemp, rm } from 'node:fs/promises';
11
11
  import { tmpdir } from 'node:os';
12
12
  import { join } from 'node:path';
13
13
  import { promisify } from 'node:util';
14
14
 
15
15
  import {
16
- LESSONS_FILE,
17
16
  errorSignature,
18
17
  firstErrorLine,
19
18
  normaliseErrorLine,
20
19
  recordFailThenFix,
21
20
  } from '../memory/lessons.mjs';
21
+ import { TREE_EXCLUDES, localRel, readStateFile } from '../state.mjs';
22
22
 
23
23
  const exec = promisify(execFile);
24
24
 
25
- const LEDGER_FILE = '.simt/ledger.jsonl';
25
+ export const LEDGER_FILE = localRel('ledger.jsonl');
26
+ const LEDGER_FILE_LEGACY = '.simt/ledger.jsonl';
26
27
 
27
28
  /** Phrases that claim a run happened. `unverified` and `not verified` are removed
28
- * first, because both contain the word and neither is a claim. */
29
- const CLAIM = /\btests?\s+pass(?:ed)?\b|\bverified\b/i;
29
+ * first, because both contain the word and neither is a claim. Trailers
30
+ * (Signed-off-by and friends) are ignored, because they describe the commit,
31
+ * not the run (review 0003 Q18). */
32
+ const CLAIM =
33
+ /\btests?\s+pass(?:ed|ing)?\b|\btest\s+suite\s+(?:is\s+)?green\b|\bsuite\s+is\s+green\b|\bnpm\s+test\s+pass(?:es|ed)?\b|\bverified\b/i;
34
+ const TRAILER = /^(signed-off-by|co-authored-by|reviewed-by|acked-by|tested-by|fixes|closes)\s*:/i;
30
35
 
31
36
  /**
32
37
  * The tree of everything in the worktree, without staging any of it.
@@ -39,14 +44,12 @@ export async function treeId(root) {
39
44
  const index = join(dir, 'index');
40
45
  const env = { ...process.env, GIT_INDEX_FILE: index };
41
46
  try {
42
- // The ledger is a record of the tree, so it cannot be part of the tree it names.
43
- // `git add -A` would take it once the file exists, and the line would prove a
44
- // tree that stopped existing when the line was written.
45
- await exec(
46
- 'git',
47
- ['add', '-A', '--', '.', `:(exclude)${LEDGER_FILE}`, `:(exclude)${LESSONS_FILE}`],
48
- { cwd: root, env },
49
- );
47
+ // Machine-local state is a record of the tree, so it cannot be part of the
48
+ // tree it names. `git add -A` would take it once the file exists, and the
49
+ // line would prove a tree that stopped existing when the line was written
50
+ // (review 0003 Q19).
51
+ const excludes = TREE_EXCLUDES.map((p) => `:(exclude)${p}`);
52
+ await exec('git', ['add', '-A', '--', '.', ...excludes], { cwd: root, env });
50
53
  const { stdout } = await exec('git', ['write-tree'], { cwd: root, env });
51
54
  return stdout.trim();
52
55
  } finally {
@@ -58,12 +61,31 @@ export async function treeId(root) {
58
61
  * Append one run. The output itself is not stored: the digest is enough to notice a
59
62
  * change, and a ledger that kept logs would keep whatever the command printed.
60
63
  */
61
- export async function recordRun(root, { cmd, exit, ms, output }) {
64
+ /** The staged tree (`git write-tree` on the real index): what a commit-msg
65
+ * check must match, because the commit records the index, not the worktree.
66
+ * Null when git cannot answer; the caller then falls back to the worktree tree. */
67
+ export async function indexTreeId(root) {
68
+ try {
69
+ const { stdout } = await exec('git', ['write-tree'], { cwd: root });
70
+ const tree = String(stdout ?? '').trim();
71
+ return /^[0-9a-f]{40}$/.test(tree) ? tree : null;
72
+ } catch {
73
+ return null;
74
+ }
75
+ }
76
+
77
+ export async function recordRun(root, { cmd, exit, ms, output, gate = null, kind = null }) {
62
78
  const { entries: prior } = await readLedger(root).catch(() => ({ entries: [] }));
79
+ const worktreeTree = await treeId(root);
80
+ const indexTree = await indexTreeId(root);
63
81
  const line = {
64
- v: 1,
82
+ v: 2,
65
83
  ts: new Date().toISOString().replace(/\.\d{3}Z$/, 'Z'),
66
- tree: await treeId(root),
84
+ tree: worktreeTree,
85
+ worktreeTree,
86
+ ...(indexTree !== null ? { indexTree } : {}),
87
+ ...(typeof gate === 'string' ? { gate } : {}),
88
+ ...(typeof kind === 'string' ? { kind } : {}),
67
89
  cmd,
68
90
  exit,
69
91
  ms,
@@ -76,7 +98,7 @@ export async function recordRun(root, { cmd, exit, ms, output }) {
76
98
  }
77
99
  : {}),
78
100
  };
79
- await mkdir(join(root, '.simt'), { recursive: true });
101
+ await mkdir(join(root, '.simt', 'local'), { recursive: true });
80
102
  await appendFile(join(root, LEDGER_FILE), `${JSON.stringify(line)}\n`);
81
103
 
82
104
  if (exit === 0 && typeof cmd === 'string') {
@@ -100,13 +122,9 @@ export async function recordRun(root, { cmd, exit, ms, output }) {
100
122
  /** Every well-formed line. A line that does not parse is returned as an error so a
101
123
  * damaged ledger cannot look empty. */
102
124
  export async function readLedger(root) {
103
- let text;
104
- try {
105
- text = await readFile(join(root, LEDGER_FILE), 'utf8');
106
- } catch (error) {
107
- if (error.code === 'ENOENT') return { entries: [], errors: [] };
108
- throw error;
109
- }
125
+ // The pre-move path is a fallback for one release (review 0003 Q19).
126
+ const { text } = await readStateFile(root, LEDGER_FILE, [LEDGER_FILE_LEGACY]);
127
+ if (text === null) return { entries: [], errors: [] };
110
128
  const entries = [];
111
129
  const errors = [];
112
130
  for (const [i, raw] of text.split('\n').entries()) {
@@ -125,8 +143,56 @@ export async function readLedger(root) {
125
143
  return { entries, errors };
126
144
  }
127
145
 
146
+ /** Whether the entry counts as a test-kind run (review 0003 Q18). v1 lines
147
+ * carry no kind, so a command naming test counts; v2 lines use the recorded kind. */
148
+ function isTestKind(entry) {
149
+ if (typeof entry?.kind === 'string') return entry.kind === 'test';
150
+ return typeof entry?.cmd === 'string' && /\btest\b/i.test(entry.cmd);
151
+ }
152
+
153
+ /**
154
+ * The surviving passing entry for one gate on one staged tree, or null.
155
+ *
156
+ * A later failure for the same gate on the same tree cancels the pass, the
157
+ * way `hasTestEvidence` cancels a test pass — a gate that passed, then
158
+ * failed, proves nothing about the tree. Entries without an `indexTree`
159
+ * predate the staged-tree record and never match: a pass on an unknown tree
160
+ * is not evidence for this one (review 0003 Q23).
161
+ */
162
+ export function passEntryForGate(entries, gate, tree) {
163
+ const scoped = (entries ?? []).filter((e) => e?.gate === gate && e?.indexTree === tree);
164
+ for (let i = 0; i < scoped.length; i += 1) {
165
+ if (scoped[i].exit !== 0) continue;
166
+ const laterFailure = scoped.slice(i + 1).some((later) => later.exit !== 0);
167
+ if (!laterFailure) return scoped[i];
168
+ }
169
+ return null;
170
+ }
171
+
172
+ /** A passing test-kind entry for `tree` with no later failure of the same
173
+ * command on the same tree. v1 lines (no `kind`) keep today's reading: any
174
+ * passing entry for the tree counts, so old ledgers do not go red. */
175
+ export function hasTestEvidence(entries, tree) {
176
+ const scoped = entries.filter((e) => e.tree === tree || e.worktreeTree === tree || e.indexTree === tree);
177
+ for (let i = 0; i < scoped.length; i += 1) {
178
+ const candidate = scoped[i];
179
+ if (candidate.exit !== 0) continue;
180
+ const legacy = candidate.v !== 2 || candidate.kind === undefined || candidate.kind === null;
181
+ if (!legacy && !isTestKind(candidate)) continue;
182
+ const laterFailure = scoped
183
+ .slice(i + 1)
184
+ .some((later) => later.cmd === candidate.cmd && later.exit !== 0);
185
+ if (!laterFailure) return true;
186
+ }
187
+ return false;
188
+ }
189
+
128
190
  export function claimsVerification(message) {
129
- const text = String(message).replace(/\bunverified\b/gi, ' ').replace(/\bnot\s+verified\b/gi, ' ');
191
+ const lines = String(message).split('\n').filter((line) => !TRAILER.test(line.trim()));
192
+ const text = lines
193
+ .join('\n')
194
+ .replace(/\bunverified\b/gi, ' ')
195
+ .replace(/\bnot\s+verified\b/gi, ' ');
130
196
  return CLAIM.test(text);
131
197
  }
132
198
 
@@ -16,7 +16,7 @@
16
16
  import { readFile } from 'node:fs/promises';
17
17
  import { join } from 'node:path';
18
18
 
19
- import { DECISIONS_DIR, loadDecisions, redactEstate, redactInternalPaths } from '../decisions/index.mjs';
19
+ import { DECISIONS_DIR, loadDecisionsCached, redactEstate, redactInternalPaths } from '../decisions/index.mjs';
20
20
  import { planNext } from '../plan/index.mjs';
21
21
  import { loadMdcRules } from '../projection/mdc.mjs';
22
22
  import { desiredRegion, loadRules } from '../projection/rules.mjs';
@@ -122,7 +122,9 @@ export async function listResources(root = process.cwd()) {
122
122
  const [rules, mdc, { decisions }, region, plan] = await Promise.all([
123
123
  loadRules(null),
124
124
  loadMdcRules(),
125
- loadDecisions({ all: true }),
125
+ // Q55: the shipped records through the shared cache (root locates the cache
126
+ // file; the directory stays the package's own, which is what is served).
127
+ loadDecisionsCached({ root, dir: DECISIONS_DIR, all: true }),
126
128
  kernelRegion(root),
127
129
  planNext(root, null),
128
130
  ]);
@@ -211,7 +213,7 @@ export async function readResource(uri, root = process.cwd()) {
211
213
  // `loadDecisions` reads the index, not the records: it reports status, currency and
212
214
  // byte count, and keeps no body. Reading the file the index named is one read of a
213
215
  // record the reader has already asked for by id.
214
- const { decisions } = await loadDecisions({ all: true });
216
+ const { decisions } = await loadDecisionsCached({ root, dir: DECISIONS_DIR, all: true });
215
217
  const decision = decisions.find((candidate) => candidate.id === parsed.id);
216
218
  if (decision === undefined) return null;
217
219
  const raw = await readFile(join(DECISIONS_DIR, decision.file), 'utf8');