peaks-loop 4.1.26 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/CHANGELOG.md +160 -0
  3. package/contracts/readonly-argv-whitelist.json +116 -0
  4. package/dist/cli/commands/code-auto-compact-command.js +15 -3
  5. package/dist/cli/commands/core/session-info-command.js +3 -4
  6. package/dist/cli/commands/core/skill-presence-housekeeping-commands.js +7 -1
  7. package/dist/cli/commands/dashboard-long-run.js +2 -1
  8. package/dist/cli/commands/doctor/invoke-from-code.js +6 -1
  9. package/dist/cli/commands/job-state-root.js +4 -2
  10. package/dist/cli/commands/lease-metrics-commands-support.js +2 -1
  11. package/dist/cli/commands/loop-eval-capture-index.js +2 -1
  12. package/dist/cli/commands/loop-eval-eval-command.js +2 -1
  13. package/dist/cli/commands/mut-commands-helpers.d.ts +9 -28
  14. package/dist/cli/commands/mut-commands-helpers.js +53 -80
  15. package/dist/cli/commands/mut-failure-diagnostic.d.ts +48 -0
  16. package/dist/cli/commands/mut-failure-diagnostic.js +147 -0
  17. package/dist/cli/commands/outer-cache-commands.js +3 -2
  18. package/dist/cli/commands/playwright-session-store.js +5 -2
  19. package/dist/cli/commands/project-context-command.js +7 -2
  20. package/dist/cli/commands/qa-archive-screenshots-command.js +2 -1
  21. package/dist/cli/commands/request-transition-preflight.js +2 -2
  22. package/dist/cli/commands/slice-decompose-artifacts.js +10 -2
  23. package/dist/cli/commands/verdict-aggregate-command.js +4 -3
  24. package/dist/cli/commands/workspace/init-actions.js +5 -0
  25. package/dist/cli/commands/workspace/init-context.js +1 -0
  26. package/dist/cli/commands/worktree-lease-effects.d.ts +13 -0
  27. package/dist/cli/commands/worktree-lease-effects.js +23 -0
  28. package/dist/cli/commands/worktree-lease-gc-command.d.ts +3 -4
  29. package/dist/cli/commands/worktree-lease-gc-command.js +59 -58
  30. package/dist/cli/commands/worktree-lease-git.d.ts +13 -18
  31. package/dist/cli/commands/worktree-lease-git.js +21 -44
  32. package/dist/cli/commands/worktree-lease-list-command.js +1 -1
  33. package/dist/cli/commands/worktree-lease-load.d.ts +19 -7
  34. package/dist/cli/commands/worktree-lease-load.js +37 -9
  35. package/dist/cli/commands/worktree-lease-release-command.d.ts +4 -5
  36. package/dist/cli/commands/worktree-lease-release-command.js +60 -47
  37. package/dist/cli/commands/worktree-lease-renew-command.d.ts +5 -4
  38. package/dist/cli/commands/worktree-lease-renew-command.js +36 -32
  39. package/dist/cli/commands/worktree-lease-session.d.ts +7 -5
  40. package/dist/cli/commands/worktree-lease-session.js +9 -6
  41. package/dist/cli/commands/worktree-lease-spawn-command.d.ts +4 -3
  42. package/dist/cli/commands/worktree-lease-spawn-command.js +38 -46
  43. package/dist/cli/commands/worktree-lease-status-command.js +1 -1
  44. package/dist/cli/commands/worktree-lease-store.d.ts +3 -2
  45. package/dist/cli/commands/worktree-lease-store.js +3 -2
  46. package/dist/services/artifacts/prerequisite-path-resolver.js +5 -1
  47. package/dist/services/audit/enforcers/lint-output-style.js +5 -1
  48. package/dist/services/audit/enforcers/lint-rd-handoff-coverage.js +9 -4
  49. package/dist/services/audit-independent/handoff-missing-refusal.js +16 -1
  50. package/dist/services/audit-independent/judged-declaration-check.js +5 -1
  51. package/dist/services/audit-independent/perf-audit-service.d.ts +2 -4
  52. package/dist/services/audit-independent/perf-audit-service.js +4 -5
  53. package/dist/services/audit-independent/security-audit-artifact.js +5 -1
  54. package/dist/services/capability-audit-service/runner.js +4 -1
  55. package/dist/services/capability-baseline/store.js +2 -1
  56. package/dist/services/capability-guard-runner/contracts/J05.js +4 -2
  57. package/dist/services/capability-guard-runner/contracts/J12.js +2 -2
  58. package/dist/services/code/auto-compact-artifacts.js +42 -10
  59. package/dist/services/code/auto-compact-checkpoint-content.d.ts +49 -0
  60. package/dist/services/code/auto-compact-checkpoint-content.js +157 -0
  61. package/dist/services/code/auto-compact-envelopes.d.ts +11 -2
  62. package/dist/services/code/auto-compact-envelopes.js +18 -4
  63. package/dist/services/code/auto-compact-orchestrator.js +1 -1
  64. package/dist/services/code/emit-handoff.js +8 -1
  65. package/dist/services/code/job-shape-decision.js +13 -2
  66. package/dist/services/code/job-shape-provisional.js +9 -1
  67. package/dist/services/code/post-compact-detector.js +9 -1
  68. package/dist/services/code/step-08-session-readers.js +14 -5
  69. package/dist/services/compact/request-transition-hook.js +6 -1
  70. package/dist/services/compact/suggest-service.js +8 -1
  71. package/dist/services/context/post-compact-reinjection.js +15 -14
  72. package/dist/services/context/spillover-store.js +2 -1
  73. package/dist/services/dispatch/contract-store.d.ts +11 -4
  74. package/dist/services/dispatch/contract-store.js +21 -8
  75. package/dist/services/doctor/doctor-service/checks/workspace-layout.js +8 -3
  76. package/dist/services/doctor/doctor-service/index.js +2 -1
  77. package/dist/services/final-review/final-review-evidence-collect.js +5 -1
  78. package/dist/services/fresh-context/fresh-context-block.js +5 -1
  79. package/dist/services/hooks/auto-compact-hook-install.d.ts +13 -3
  80. package/dist/services/hooks/auto-compact-hook-install.js +13 -3
  81. package/dist/services/hooks/worktree-authorization-gate.d.ts +1 -1
  82. package/dist/services/hooks/worktree-authorization-gate.js +14 -11
  83. package/dist/services/loop/evaluator-dispatcher.js +7 -7
  84. package/dist/services/loop/monotonic-runner.js +2 -1
  85. package/dist/services/loop/spec-service.js +10 -9
  86. package/dist/services/mcp/context-tool.d.ts +3 -0
  87. package/dist/services/mcp/context-tool.js +135 -0
  88. package/dist/services/mcp/surface.js +7 -1
  89. package/dist/services/mcp/tools.js +3 -1
  90. package/dist/services/memory/project-context-service.d.ts +10 -1
  91. package/dist/services/memory/project-context-service.js +13 -6
  92. package/dist/services/observability/jsonl-store.js +4 -1
  93. package/dist/services/polyrepo/polyrepo-dispatcher.js +5 -1
  94. package/dist/services/prd/prd-blocks-checker.js +12 -6
  95. package/dist/services/prd/project-scan-body-tables.d.ts +33 -0
  96. package/dist/services/prd/project-scan-body-tables.js +185 -0
  97. package/dist/services/prd/project-scan-bootstrap-service.d.ts +16 -3
  98. package/dist/services/prd/project-scan-bootstrap-service.js +127 -231
  99. package/dist/services/prd/project-scan-content.d.ts +24 -0
  100. package/dist/services/prd/project-scan-content.js +207 -0
  101. package/dist/services/prd/project-scan-existing-state.d.ts +22 -0
  102. package/dist/services/prd/project-scan-existing-state.js +61 -0
  103. package/dist/services/prd/project-scan-field-refresh.d.ts +87 -0
  104. package/dist/services/prd/project-scan-field-refresh.js +217 -0
  105. package/dist/services/prd/project-scan-frontmatter.d.ts +32 -0
  106. package/dist/services/prd/project-scan-frontmatter.js +34 -0
  107. package/dist/services/prd/project-scan-library-versions.d.ts +60 -0
  108. package/dist/services/prd/project-scan-library-versions.js +135 -0
  109. package/dist/services/prd/project-scan-techstack.d.ts +34 -0
  110. package/dist/services/prd/project-scan-techstack.js +167 -0
  111. package/dist/services/reviewer/reviewer-artifacts.js +2 -1
  112. package/dist/services/sc/sc-service.js +8 -8
  113. package/dist/services/scan/api-diff-recorded.d.ts +2 -3
  114. package/dist/services/scan/api-diff-recorded.js +5 -5
  115. package/dist/services/session/binding-status-service.js +2 -1
  116. package/dist/services/session/binding-store.js +6 -7
  117. package/dist/services/session/caller-binding-service.js +5 -2
  118. package/dist/services/session/getSessionDir.d.ts +4 -0
  119. package/dist/services/session/getSessionDir.js +38 -13
  120. package/dist/services/session/session-binding-bridge.d.ts +3 -6
  121. package/dist/services/session/session-binding-bridge.js +13 -13
  122. package/dist/services/session/session-checkpoint-service.js +22 -3
  123. package/dist/services/session/session-manager.js +16 -20
  124. package/dist/services/skills/presence-lease-resolver.js +3 -2
  125. package/dist/services/skills/presence-lease-service.js +7 -15
  126. package/dist/services/skills/presence-lease-store.d.ts +26 -0
  127. package/dist/services/skills/presence-lease-store.js +60 -5
  128. package/dist/services/skills/skill-presence-service.js +13 -20
  129. package/dist/services/skills/skill-statusline-model.js +7 -2
  130. package/dist/services/slice/slice-archive-service.js +4 -1
  131. package/dist/services/standards/missing-standards-detector.js +2 -1
  132. package/dist/services/test-cache/test-cache-service.js +2 -1
  133. package/dist/services/workflow/artifact-paths.js +6 -2
  134. package/dist/services/workflow/pipeline-verify-gate-support.d.ts +1 -1
  135. package/dist/services/workflow/pipeline-verify-gate-support.js +23 -11
  136. package/dist/services/workflow/workflow-graph-store-helpers.js +12 -3
  137. package/dist/services/workflow/workflow-state-store.js +11 -4
  138. package/dist/services/workspace/migrate-1-4-1-service.js +2 -1
  139. package/dist/services/workspace/reconcile-migrate.js +2 -2
  140. package/dist/services/workspace/reconcile-service.d.ts +4 -6
  141. package/dist/services/workspace/reconcile-service.js +11 -11
  142. package/dist/services/workspace/workspace-service.js +5 -5
  143. package/dist/services/worktree/dispatch-provenance.d.ts +1 -1
  144. package/dist/services/worktree/dispatch-provenance.js +9 -2
  145. package/dist/services/worktree/host-worktree-reconciler.d.ts +1 -1
  146. package/dist/services/worktree/host-worktree-reconciler.js +5 -3
  147. package/dist/shared/runtime-root.js +6 -1
  148. package/package.json +6 -5
  149. package/skills/peaks-code/SKILL.md +1 -1
  150. package/skills/peaks-code/references/project-scan-checklist.md +3 -1
  151. package/skills/peaks-mut/SKILL.md +6 -2
  152. package/dist/services/worktree/git-worktree-parser.d.ts +0 -8
  153. package/dist/services/worktree/git-worktree-parser.js +0 -55
  154. package/dist/services/worktree/worktree-lease-types.d.ts +0 -52
  155. package/dist/services/worktree/worktree-lease-types.js +0 -9
  156. package/dist/services/worktree/worktree-lease.d.ts +0 -116
  157. package/dist/services/worktree/worktree-lease.js +0 -252
@@ -119,7 +119,7 @@ function buildSessionHistory(projectRoot) {
119
119
  body += `\n${MANAGED_BLOCK_END}`;
120
120
  return body;
121
121
  }
122
- export async function generateProjectContext(projectRoot) {
122
+ export async function generateProjectContext(projectRoot, options = {}) {
123
123
  const peaksDir = join(projectRoot, '.peaks');
124
124
  if (!existsSync(peaksDir)) {
125
125
  mkdirSync(peaksDir, { recursive: true });
@@ -152,11 +152,18 @@ export async function generateProjectContext(projectRoot) {
152
152
  }
153
153
  writeFileSync(contextPath, content, 'utf8');
154
154
  // After writing PROJECT.md, also bootstrap `.peaks/project-scan/`
155
- // (project-scan.md + 4 bundled audit/business templates). Idempotent
156
- // — re-running this call does not overwrite existing files (unless
157
- // the caller passes `force` / `forceTemplates`, which we do NOT do
158
- // here; peaks workspace init owns the force variants).
159
- const projectScan = await bootstrapProjectScan({ projectRoot });
155
+ // (project-scan.md + 4 bundled audit/business templates). The 4 templates
156
+ // are sediment-protected (skipped when present). The scan's OWN machine
157
+ // fields are refreshed by default, because this command already regenerates
158
+ // `.peaks/PROJECT.md` unconditionally on every run: a frozen
159
+ // `libraryVersions` map makes peaks-rd's breaking-change cross-check pass on
160
+ // stale input. `refreshMachineFields` rewrites only `capturedAt` /
161
+ // `techStack` / `libraryVersions` and preserves every other byte, so the
162
+ // hand-authored prose survives. Pass `refreshProjectScan: false` to skip.
163
+ const projectScan = await bootstrapProjectScan({
164
+ projectRoot,
165
+ refreshMachineFields: options.refreshProjectScan !== false
166
+ });
160
167
  return {
161
168
  path: contextPath,
162
169
  content,
@@ -16,6 +16,7 @@
16
16
  */
17
17
  import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync } from 'node:fs';
18
18
  import { join } from 'node:path';
19
+ import { defaultStateRoot } from 'peaks-loop-workspace-runtime';
19
20
  import { getSessionDir, tryGetSessionDir } from '../session/getSessionDir.js';
20
21
  export const METRICS_DIR = 'metrics';
21
22
  export const METRICS_FILENAME = 'slices.jsonl';
@@ -91,7 +92,9 @@ export function readMetricLines(projectRoot, sessionId) {
91
92
  * caller decides mtime ordering (see `pruneMetricsFiles`).
92
93
  */
93
94
  export function listSessionDirsWithMetrics(projectRoot) {
94
- const runtimeRoot = join(projectRoot, '.peaks', '_runtime');
95
+ // The bare runtime root, READ-only (existsSync + readdirSync): the seam's
96
+ // state-root entry, whose separators both accept.
97
+ const runtimeRoot = defaultStateRoot(projectRoot);
95
98
  if (!existsSync(runtimeRoot)) {
96
99
  return [];
97
100
  }
@@ -24,12 +24,16 @@
24
24
  */
25
25
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
26
26
  import { dirname, join } from 'node:path';
27
+ import { defaultStateRoot, sessionRuntimeDir } from 'peaks-loop-workspace-runtime';
27
28
  import { isUnsafePathInput } from '../../shared/path-safety.js';
28
29
  /** Compute the child-side path where the artifact should be mirrored.
29
30
  * Mirrors the parent shape exactly: `.peaks/_runtime/<sid>/<role>/`. */
30
31
  function childArtifactPath(childRoot, sid, role, sourcePath) {
31
32
  const filename = sourcePath.split(/[\\/]/).pop() ?? 'artifact.md';
32
- return join(childRoot, '.peaks', '_runtime', sid, role, filename);
33
+ // The child project has the same runtime layout as this one; only the root
34
+ // differs, so the state root is resolved against the CHILD.
35
+ const sessionDir = sessionRuntimeDir({ stateRoot: defaultStateRoot(childRoot), sessionId: sid });
36
+ return join(sessionDir, role, filename);
33
37
  }
34
38
  function ensureDirFor(file) {
35
39
  mkdirSync(dirname(file), { recursive: true });
@@ -16,6 +16,7 @@
16
16
  */
17
17
  import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs';
18
18
  import { join, resolve } from 'node:path';
19
+ import { defaultStateRoot } from 'peaks-loop-workspace-runtime';
19
20
  const BLOCK_HEADINGS = [
20
21
  {
21
22
  block: 1,
@@ -43,24 +44,29 @@ const BLOCK_HEADINGS = [
43
44
  }
44
45
  ];
45
46
  const MIN_CONTENT_LENGTH = 50;
47
+ /**
48
+ * The runtime root's non-session `prd/` child. Named once because this module
49
+ * is the reader the runtime-layout registry points at for that entry.
50
+ */
51
+ const PRD_RUNTIME_DIR = 'prd';
46
52
  export function findPrdArtifact(projectRoot, requestId) {
53
+ const stateRoot = defaultStateRoot(projectRoot);
47
54
  // Look under .peaks/_runtime/<session>/prd/requests/<rid>.md
48
55
  // Use the first hit (the most common layout is single-session).
49
56
  const candidates = [
50
- join(projectRoot, '.peaks', '_runtime', 'prd', 'requests', `${requestId}.md`),
51
- join(projectRoot, '.peaks', '_runtime', 'change', requestId, 'prd', 'requests', `${requestId}.md`)
57
+ join(stateRoot, PRD_RUNTIME_DIR, 'requests', `${requestId}.md`),
58
+ join(stateRoot, 'change', requestId, PRD_RUNTIME_DIR, 'requests', `${requestId}.md`)
52
59
  ];
53
60
  for (const c of candidates) {
54
61
  if (existsSync(c))
55
62
  return c;
56
63
  }
57
64
  // Fall back to a glob-like search through .peaks/_runtime/*/prd/requests/.
58
- const runtimeRoot = join(projectRoot, '.peaks', '_runtime');
59
- if (!existsSync(runtimeRoot))
65
+ if (!existsSync(stateRoot))
60
66
  return null;
61
- for (const sessionOrChange of readdirSync(runtimeRoot)) {
67
+ for (const sessionOrChange of readdirSync(stateRoot)) {
62
68
  for (const sub of ['prd/requests', 'change']) {
63
- const dir = join(runtimeRoot, sessionOrChange, sub, requestId);
69
+ const dir = join(stateRoot, sessionOrChange, sub, requestId);
64
70
  if (existsSync(dir) && statSync(dir).isFile())
65
71
  return dir;
66
72
  }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Keeping the body tables of `.peaks/project-scan/project-scan.md` in step with
3
+ * the frontmatter a refresh just rewrote.
4
+ *
5
+ * WHY THIS EXISTS — a measured defect, not a guess. `peaks-rd`'s own reference
6
+ * (`references/library-version-awareness.md`) tells RD to read the `## Library
7
+ * versions` TABLE, while `readProjectScan` reads the FRONTMATTER. A refresh that
8
+ * only touched the frontmatter left the two disagreeing: measured 2026-11-16 on
9
+ * this repository's own scan — 32 names in the frontmatter, 21 rows in the
10
+ * table, 11 names (including `typescript`) present in only one of them. RD would
11
+ * then cross-check a breaking-change table against rows that never moved.
12
+ *
13
+ * WHAT MAY BE REWRITTEN. Only the version CELL of a row whose package is in the
14
+ * refreshed map, plus whole rows appended for packages the table lacks. The
15
+ * Notes column, the header, the row order and every other prose table are
16
+ * untouched — those are sediment.
17
+ */
18
+ export declare function refreshLibraryVersionsTable(body: string, versions: Readonly<Record<string, string>>): string;
19
+ /**
20
+ * Fill the `## Tech stack` table's EMPTY value cells from `techStack`.
21
+ *
22
+ * WHY IT ONLY FILLS, NEVER OVERWRITES. These cells are prose, not a machine
23
+ * record: this repository's own scan reads
24
+ * `| Build | \`tsc -p tsconfig.build.json\` (root, via \`pnpm build\`) + per-package ... |`.
25
+ * An earlier version of this function replaced that with the bare derived
26
+ * `tsc -p tsconfig.build.json`, and did the same to the Language, Package manager
27
+ * and Node runtime rows — measured 2026-11-16, on the very file this slice
28
+ * exists to protect. The derived value is strictly less informative than what an
29
+ * author writes here, so it may only take a cell that is empty or a placeholder.
30
+ * The machine-readable copy is the FRONTMATTER, which is refreshed in full; this
31
+ * table is for humans and stays theirs.
32
+ */
33
+ export declare function refreshTechStackTable(body: string, techStack: Readonly<Record<string, string>>): string;
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Keeping the body tables of `.peaks/project-scan/project-scan.md` in step with
3
+ * the frontmatter a refresh just rewrote.
4
+ *
5
+ * WHY THIS EXISTS — a measured defect, not a guess. `peaks-rd`'s own reference
6
+ * (`references/library-version-awareness.md`) tells RD to read the `## Library
7
+ * versions` TABLE, while `readProjectScan` reads the FRONTMATTER. A refresh that
8
+ * only touched the frontmatter left the two disagreeing: measured 2026-11-16 on
9
+ * this repository's own scan — 32 names in the frontmatter, 21 rows in the
10
+ * table, 11 names (including `typescript`) present in only one of them. RD would
11
+ * then cross-check a breaking-change table against rows that never moved.
12
+ *
13
+ * WHAT MAY BE REWRITTEN. Only the version CELL of a row whose package is in the
14
+ * refreshed map, plus whole rows appended for packages the table lacks. The
15
+ * Notes column, the header, the row order and every other prose table are
16
+ * untouched — those are sediment.
17
+ */
18
+ /** One body table a refresher may touch, and the column that holds its version. */
19
+ const VERSION_CELL_INDEX = 1;
20
+ /** A markdown table row, split into its raw cells (`null` when it is not a row). */
21
+ function cellsOf(line) {
22
+ const trimmed = line.trim();
23
+ if (!trimmed.startsWith('|') || !trimmed.endsWith('|'))
24
+ return null;
25
+ return trimmed.slice(1, -1).split('|');
26
+ }
27
+ /** Re-join cells into a row, preserving the padding style of the original. */
28
+ function joinCells(cells) {
29
+ return `|${cells.join('|')}|`;
30
+ }
31
+ /** The package name a row names, or `null` when the row is not a package row. */
32
+ function packageNameOf(cells) {
33
+ const first = (cells[0] ?? '').trim();
34
+ const backticked = /^`([^`]+)`$/.exec(first);
35
+ if (backticked !== null)
36
+ return backticked[1] ?? null;
37
+ // The create path writes rows without backticks; accept a bare name too.
38
+ return first === '' || first.includes(' ') ? null : first;
39
+ }
40
+ /** True for the `|---|---|` separator row. */
41
+ function isSeparator(cells) {
42
+ return cells.every((cell) => /^\s*:?-{2,}:?\s*$/.test(cell));
43
+ }
44
+ /** The table's first and last line index under the heading, or `null`. */
45
+ function tableBounds(lines, headingAt) {
46
+ let first = -1;
47
+ for (let i = headingAt + 1; i < lines.length; i += 1) {
48
+ if (cellsOf(lines[i] ?? '') !== null) {
49
+ first = i;
50
+ break;
51
+ }
52
+ // A later heading before any table row means there is no table here.
53
+ if (/^#{1,6}\s/.test((lines[i] ?? '').trim()))
54
+ return null;
55
+ }
56
+ if (first < 0)
57
+ return null;
58
+ let last = first;
59
+ while (last + 1 < lines.length && cellsOf(lines[last + 1] ?? '') !== null)
60
+ last += 1;
61
+ return { first, last };
62
+ }
63
+ /** Rewrite one row's version cell, preserving its backtick style. */
64
+ function rewriteVersionCell(cells, spec) {
65
+ const next = [...cells];
66
+ const previous = (next[VERSION_CELL_INDEX] ?? '').trim();
67
+ const backticked = /^`.*`$/.test(previous);
68
+ next[VERSION_CELL_INDEX] = backticked ? ` \`${spec}\` ` : ` ${spec} `;
69
+ return joinCells(next);
70
+ }
71
+ /** Rewrite a single body line against `versions`, or leave it alone. */
72
+ function rewriteRow(line, versions) {
73
+ const cells = cellsOf(line);
74
+ if (cells === null)
75
+ return null;
76
+ if (isSeparator(cells))
77
+ return { line, name: null, width: cells.length };
78
+ const name = packageNameOf(cells);
79
+ if (name === null)
80
+ return { line, name: null, width: cells.length };
81
+ const spec = versions[name];
82
+ return {
83
+ line: spec === undefined ? line : rewriteVersionCell(cells, spec),
84
+ name,
85
+ width: cells.length
86
+ };
87
+ }
88
+ /** A row for a package the table does not carry, shaped like its neighbours. */
89
+ function appendRow(name, spec, width) {
90
+ const row = Array.from({ length: width }, () => ' ');
91
+ row[0] = ` \`${name}\` `;
92
+ row[VERSION_CELL_INDEX] = ` \`${spec}\` `;
93
+ return joinCells(row);
94
+ }
95
+ export function refreshLibraryVersionsTable(body, versions) {
96
+ const lines = body.split('\n');
97
+ const headingAt = lines.findIndex((line) => /^##\s+Library versions\s*$/.test(line.trim()));
98
+ if (headingAt < 0)
99
+ return body;
100
+ const bounds = tableBounds(lines, headingAt);
101
+ if (bounds === null)
102
+ return body;
103
+ const { first, last } = bounds;
104
+ const seen = new Set();
105
+ const rewritten = [];
106
+ let width = VERSION_CELL_INDEX + 1;
107
+ for (let i = first; i <= last; i += 1) {
108
+ const result = rewriteRow(lines[i] ?? '', versions);
109
+ if (result === null)
110
+ continue;
111
+ width = Math.max(width, result.width);
112
+ if (result.name !== null)
113
+ seen.add(result.name);
114
+ rewritten.push(result.line);
115
+ }
116
+ for (const name of Object.keys(versions).sort()) {
117
+ if (!seen.has(name))
118
+ rewritten.push(appendRow(name, versions[name] ?? '', width));
119
+ }
120
+ return [...lines.slice(0, first), ...rewritten, ...lines.slice(last + 1)].join('\n');
121
+ }
122
+ /**
123
+ * Rewrite the `## Tech stack` table's value cells from `techStack`, matched by
124
+ * the row's own label. A label with no derived value keeps whatever the author
125
+ * wrote — the table labels are prose ("Node runtime", "Build"), not machine keys,
126
+ * so this matches on a small label→key map rather than guessing.
127
+ */
128
+ const TECH_STACK_LABELS = [
129
+ [/^language$/i, 'language'],
130
+ [/^package manager$/i, 'packageManager'],
131
+ [/^node runtime$/i, 'runtime'],
132
+ [/^build$/i, 'buildTool']
133
+ ];
134
+ /** Cell text that carries no information — safe to fill. */
135
+ function isPlaceholderCell(text) {
136
+ const stripped = text.replace(/[`*_]/g, '').trim();
137
+ return stripped === '' || /^\(?(empty|unknown|n\/a|—|-)\)?$/i.test(stripped);
138
+ }
139
+ /**
140
+ * Fill the `## Tech stack` table's EMPTY value cells from `techStack`.
141
+ *
142
+ * WHY IT ONLY FILLS, NEVER OVERWRITES. These cells are prose, not a machine
143
+ * record: this repository's own scan reads
144
+ * `| Build | \`tsc -p tsconfig.build.json\` (root, via \`pnpm build\`) + per-package ... |`.
145
+ * An earlier version of this function replaced that with the bare derived
146
+ * `tsc -p tsconfig.build.json`, and did the same to the Language, Package manager
147
+ * and Node runtime rows — measured 2026-11-16, on the very file this slice
148
+ * exists to protect. The derived value is strictly less informative than what an
149
+ * author writes here, so it may only take a cell that is empty or a placeholder.
150
+ * The machine-readable copy is the FRONTMATTER, which is refreshed in full; this
151
+ * table is for humans and stays theirs.
152
+ */
153
+ export function refreshTechStackTable(body, techStack) {
154
+ const lines = body.split('\n');
155
+ const headingAt = lines.findIndex((line) => /^##\s+Tech stack\s*$/.test(line.trim()));
156
+ if (headingAt < 0)
157
+ return body;
158
+ let last = headingAt;
159
+ while (last + 1 < lines.length) {
160
+ const next = lines[last + 1] ?? '';
161
+ if (/^#{1,6}\s/.test(next.trim()) && last > headingAt)
162
+ break;
163
+ if (next.trim() === '' && last > headingAt + 1)
164
+ break;
165
+ last += 1;
166
+ }
167
+ const rewritten = lines.slice(headingAt, last + 1).map((line) => {
168
+ const cells = cellsOf(line);
169
+ if (cells === null || isSeparator(cells))
170
+ return line;
171
+ const label = (cells[0] ?? '').trim();
172
+ const match = TECH_STACK_LABELS.find(([pattern]) => pattern.test(label));
173
+ if (match === undefined)
174
+ return line;
175
+ const value = techStack[match[1]];
176
+ if (value === undefined)
177
+ return line;
178
+ if (!isPlaceholderCell(cells[1] ?? ''))
179
+ return line;
180
+ const next = [...cells];
181
+ next[1] = ` ${value} `;
182
+ return joinCells(next);
183
+ });
184
+ return [...lines.slice(0, headingAt), ...rewritten, ...lines.slice(last + 1)].join('\n');
185
+ }
@@ -11,7 +11,10 @@
11
11
  * `src/services/workspace/templates/project-scan/`) into
12
12
  * `.peaks/project-scan/` (AC9)
13
13
  * - idempotent: skips re-writes when the existing file is at
14
- * schemaVersion:1 (AC3); --force / --force-templates override
14
+ * schemaVersion:1 (AC3); `refreshMachineFields` rewrites only the
15
+ * machine-derivable fields of such a file (see
16
+ * `project-scan-field-refresh.ts`), and `forceTemplates` overrides the
17
+ * 4 bundled templates' skip
15
18
  *
16
19
  * Called by:
17
20
  * - `peaks project context` (via `generateProjectContext`)
@@ -24,8 +27,14 @@
24
27
  */
25
28
  export type BootstrapProjectScanOptions = {
26
29
  readonly projectRoot: string;
27
- /** Override the existing-file skip and rewrite project-scan.md. */
28
- readonly force?: boolean;
30
+ /**
31
+ * Rewrite the machine-derivable frontmatter fields (`capturedAt`,
32
+ * `techStack`, `libraryVersions`) of an existing `schemaVersion: 1`
33
+ * project-scan.md, preserving every other byte. A file that was never
34
+ * populated (empty `architecture` AND empty `techStack`) is refreshed even
35
+ * without this option — it is a shell, not sediment.
36
+ */
37
+ readonly refreshMachineFields?: boolean;
29
38
  /** Override the bundled-template skip and overwrite audit/business files. */
30
39
  readonly forceTemplates?: boolean;
31
40
  };
@@ -34,6 +43,8 @@ export type BootstrapProjectScanEnvelope = {
34
43
  readonly created: boolean;
35
44
  /** True iff project-scan.md existed and was skipped (idempotent). */
36
45
  readonly skipped: boolean;
46
+ /** True iff only the machine-derivable frontmatter fields were rewritten. */
47
+ readonly refreshed: boolean;
37
48
  /** Count of files written to .peaks/project-scan/ (1 project-scan.md + N templates). */
38
49
  readonly templatesBooted: number;
39
50
  /** Count of files already present and skipped (sediment-preserved). */
@@ -42,6 +53,8 @@ export type BootstrapProjectScanEnvelope = {
42
53
  readonly projectScanPath: string;
43
54
  /** Resolved archetype (when existing-project path was taken). */
44
55
  readonly archetype?: string;
56
+ /** Why an existing project-scan.md could not be machine-refreshed. */
57
+ readonly refreshWarning?: string;
45
58
  /** Total wall-time of the bootstrap call. */
46
59
  readonly durationMs: number;
47
60
  };