rcf-lite 0.28.4 → 0.29.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 (168) hide show
  1. package/CHANGELOG.md +107 -1
  2. package/bin/rcf.js +4 -0
  3. package/fixtures/canary-manifest.json +9 -9
  4. package/guidance/define-draft-shapes.md +89 -0
  5. package/guidance/define-intake.md +97 -0
  6. package/guidance/define-intent.md +107 -0
  7. package/guidance/discover-prototype.md +67 -0
  8. package/guidance/discovery-prototypes.md +69 -0
  9. package/guidance/harness-template.md +34 -0
  10. package/guidance/managed/agent-instructions-block.hash +1 -1
  11. package/guidance/managed/agent-instructions-block.md +34 -0
  12. package/guidance/manifest.json +9 -2
  13. package/package.json +1 -1
  14. package/rcf/adrs/adr-4124-define-freeze-verb.json +30 -0
  15. package/rcf/adrs/adr-4126-persona-tagged-blockers-two-levels.json +40 -0
  16. package/rcf/adrs/adr-4127-readiness-tab-order-and-register.json +40 -0
  17. package/rcf/adrs/adr-4129-questions-harness-driven-loop.json +25 -0
  18. package/rcf/adrs/adr-4130-intake-harness-split-tool-scanned.json +20 -0
  19. package/rcf/adrs/adr-4132-prototypes-in-discovery-as-evidence.json +20 -0
  20. package/rcf/adrs/adr-4133-dag-dependency-depth-layout.json +40 -0
  21. package/rcf/adrs/adr-4134-index-json-route.json +35 -0
  22. package/rcf/adrs/adr-4135-readiness-po-register-and-adapter.json +35 -0
  23. package/rcf/adrs/adr-4136-embedding-host-fixture.json +35 -0
  24. package/rcf/code-nodes/cn-725.json +18 -2
  25. package/rcf/code-nodes/cn-726.json +10 -2
  26. package/rcf/code-nodes/cn-727.json +13 -4
  27. package/rcf/code-nodes/cn-728.json +18 -2
  28. package/rcf/code-nodes/cn-729.json +10 -2
  29. package/rcf/code-nodes/cn-730.json +13 -3
  30. package/rcf/code-nodes/cn-731.json +20 -0
  31. package/rcf/code-nodes/cn-732.json +14 -0
  32. package/rcf/code-nodes/cn-733.json +14 -0
  33. package/rcf/code-nodes/cn-734.json +14 -0
  34. package/rcf/code-nodes/cn-735.json +20 -0
  35. package/rcf/code-nodes/cn-736.json +14 -0
  36. package/rcf/code-nodes/cn-737.json +14 -0
  37. package/rcf/code-nodes/cn-738.json +14 -0
  38. package/rcf/code-nodes/cn-739.json +18 -0
  39. package/rcf/code-nodes/cn-740.json +19 -0
  40. package/rcf/code-nodes/cn-741.json +16 -0
  41. package/rcf/code-nodes/cn-742.json +16 -0
  42. package/rcf/code-nodes/cn-743.json +15 -0
  43. package/rcf/code-nodes/cn-744.json +22 -0
  44. package/rcf/code-nodes/cn-745.json +16 -0
  45. package/rcf/code-nodes/cn-746.json +14 -0
  46. package/rcf/code-nodes/cn-747.json +18 -0
  47. package/rcf/code-nodes/cn-748.json +16 -0
  48. package/rcf/code-nodes/cn-749.json +16 -0
  49. package/rcf/code-nodes/cn-750.json +15 -0
  50. package/rcf/code-nodes/cn-751.json +18 -0
  51. package/rcf/code-nodes/cn-752.json +14 -0
  52. package/rcf/fbs/fbs-196.json +13 -5
  53. package/rcf/fbs/fbs-198.json +42 -0
  54. package/rcf/fbs/fbs-199.json +59 -0
  55. package/rcf/fbs/fbs-200.json +46 -0
  56. package/rcf/fbs/fbs-201.json +49 -0
  57. package/rcf/fbs/fbs-202.json +110 -0
  58. package/rcf/fbs/fbs-203.json +100 -0
  59. package/rcf/requirements/req-002.json +2 -2
  60. package/rcf/requirements/req-173.json +2 -2
  61. package/rcf/requirements/req-174.json +10 -4
  62. package/rcf/requirements/req-175.json +10 -4
  63. package/rcf/requirements/req-176.json +25 -0
  64. package/rcf/requirements/req-180.json +31 -0
  65. package/rcf/requirements/req-181.json +31 -0
  66. package/rcf/requirements/req-182.json +25 -0
  67. package/rcf/requirements/req-186.json +25 -0
  68. package/rcf/requirements/req-187.json +31 -0
  69. package/rcf/requirements/req-188.json +25 -0
  70. package/rcf/tacs/tac-4121-define-ledgers.json +15 -3
  71. package/rcf/tacs/tac-4122-define-gates.json +40 -4
  72. package/rcf/tacs/tac-4123-define-readiness.json +39 -3
  73. package/rcf/tacs/tac-4124-define-freeze.json +28 -0
  74. package/rcf/tacs/tac-4127-readiness-view.json +49 -0
  75. package/rcf/tacs/tac-4128-define-guidance.json +53 -0
  76. package/rcf/tacs/tac-4129-define-questions.json +45 -0
  77. package/rcf/tacs/tac-4130-define-intake-brief.json +35 -0
  78. package/rcf/tacs/tac-4131-build-dag-view.json +39 -0
  79. package/rcf/tacs/tac-4132-id-lookup.json +42 -0
  80. package/rcf/tacs/tac-4133-readiness-po-layer.json +37 -0
  81. package/rcf/tacs/tac-4134-wespa-host-fixture.json +54 -0
  82. package/rcf/test-suites/ts-016.json +3 -3
  83. package/rcf/test-suites/ts-215.json +148 -4
  84. package/rcf/test-suites/ts-216.json +19 -3
  85. package/rcf/test-suites/ts-218.json +78 -0
  86. package/rcf/test-suites/ts-219.json +70 -0
  87. package/rcf/test-suites/ts-220.json +78 -0
  88. package/rcf/test-suites/ts-221.json +78 -0
  89. package/rcf/test-suites/ts-222.json +100 -0
  90. package/rcf/test-suites/ts-223.json +94 -0
  91. package/rcf/test-suites/ts-224.json +70 -0
  92. package/rcf/test-suites/ts-225.json +78 -0
  93. package/rcf/test-suites/ts-226.json +158 -0
  94. package/rcf/test-suites/ts-227.json +38 -0
  95. package/rcf/test-suites/ts-228.json +86 -0
  96. package/rcf/test-suites/ts-229.json +83 -0
  97. package/rcf/user-stories/us-17302.json +50 -0
  98. package/rcf/user-stories/us-17401.json +14 -2
  99. package/rcf/user-stories/us-17402.json +55 -0
  100. package/rcf/user-stories/us-17403.json +60 -0
  101. package/rcf/user-stories/us-17502.json +60 -0
  102. package/rcf/user-stories/us-17503.json +65 -0
  103. package/rcf/user-stories/us-17601.json +58 -0
  104. package/rcf/user-stories/us-18001.json +55 -0
  105. package/rcf/user-stories/us-18002.json +35 -0
  106. package/rcf/user-stories/us-18101.json +35 -0
  107. package/rcf/user-stories/us-18202.json +40 -0
  108. package/rcf/user-stories/us-18601.json +55 -0
  109. package/rcf/user-stories/us-18602.json +50 -0
  110. package/rcf/user-stories/us-18603.json +45 -0
  111. package/rcf/user-stories/us-18701.json +50 -0
  112. package/rcf/user-stories/us-18702.json +50 -0
  113. package/rcf/user-stories/us-18801.json +45 -0
  114. package/rcf/user-stories/us-204.json +59 -0
  115. package/rcf/user-stories/us-205.json +64 -0
  116. package/rcf/user-stories/us-206.json +54 -0
  117. package/releases/releases.yaml +11 -1
  118. package/src/cli/freeze.js +730 -0
  119. package/src/cli/help.js +12 -0
  120. package/src/cli/ledger.js +228 -12
  121. package/src/cli/questions.js +288 -0
  122. package/src/cli/readiness.js +267 -49
  123. package/src/cli/view.js +29 -0
  124. package/src/define/freeze-record.js +30 -15
  125. package/src/define/ledgers.js +210 -11
  126. package/src/intake/index.js +1 -1
  127. package/src/intake/orchestrator.js +117 -0
  128. package/src/mcp/tools.js +354 -0
  129. package/src/query/delta.js +6 -0
  130. package/src/query/gates.js +314 -39
  131. package/src/query/index.js +16 -2
  132. package/src/query/questions.js +438 -0
  133. package/src/query/readiness.js +248 -7
  134. package/src/server/index.js +24 -2
  135. package/src/server/routes.js +68 -1
  136. package/src/setup/identity-seed.js +17 -0
  137. package/src/view/build-dag.js +563 -0
  138. package/src/view/components/badge.js +41 -0
  139. package/src/view/components/diff.js +72 -0
  140. package/src/view/components/doc-row.js +62 -0
  141. package/src/view/components/empty-state.js +35 -0
  142. package/src/view/components/entity-selector.js +137 -0
  143. package/src/view/components/filter-bar.js +97 -0
  144. package/src/view/components/findings-list.js +49 -0
  145. package/src/view/components/index.js +12 -0
  146. package/src/view/components/pill.js +46 -0
  147. package/src/view/components/sub-tab-strip.js +43 -0
  148. package/src/view/components/toast.js +25 -0
  149. package/src/view/components-fixture.js +265 -0
  150. package/src/view/doc-renderers/architecture.js +304 -0
  151. package/src/view/doc-renderers/build.js +160 -0
  152. package/src/view/doc-renderers/index.js +2 -0
  153. package/src/view/doc-renderers/prd.js +35 -6
  154. package/src/view/html-page.js +700 -602
  155. package/src/view/index.js +96 -3
  156. package/src/view/live-client.js +39 -6
  157. package/src/view/needs-work.js +79 -0
  158. package/src/view/page-init.js +2193 -0
  159. package/src/view/readiness/phrasebook.js +115 -0
  160. package/src/view/readiness/po-layer.js +288 -0
  161. package/src/view/readiness/question-adapter.js +329 -0
  162. package/src/view/readiness/stage-legend.js +71 -0
  163. package/src/view/readiness.js +505 -0
  164. package/src/view/style.css +2017 -382
  165. package/src/view/test-host/embed-client.js +121 -0
  166. package/src/view/test-host/fixture.js +183 -0
  167. package/src/view/test-host/lints.js +220 -0
  168. package/src/view/view-index.js +323 -0
package/src/mcp/tools.js CHANGED
@@ -43,6 +43,14 @@ import {
43
43
  } from '../query/index.js';
44
44
  import { assembleBundle } from '../build/index.js';
45
45
  import { hasAgentMarker, SETUP_FUNNEL_INSTRUCTION } from '../setup/agent-setup.js';
46
+ import { readFile } from 'node:fs/promises';
47
+ import { join } from 'node:path';
48
+ import { computeReadiness } from '../query/readiness.js';
49
+ import { computeQuestions, PERSONAS as QUESTIONS_PERSONAS } from '../query/questions.js';
50
+ import { loadFreezeRecord } from '../define/freeze-record.js';
51
+ import { loadAllLedgers, LedgerError } from '../define/ledgers.js';
52
+ import { STAGE_ALIASES } from '../query/gates.js';
53
+ import { main as ledgerCliMain } from '../cli/ledger.js';
46
54
 
47
55
  // ---------------------------------------------------------------------------
48
56
  // Shared output-schema fragments
@@ -448,6 +456,93 @@ const BUILD_OUTPUT_SCHEMA = {
448
456
  ],
449
457
  };
450
458
 
459
+ // REQ-186 (spec 2026-10-01 §1.2, §6): the questions object is wide
460
+ // and recursive in its context field. The output schema stays loose
461
+ // (an object with no additionalProperties: false) so the per-question
462
+ // context carries any of statement, suggestions, decision, reqId,
463
+ // profileField without a schema break per template addition.
464
+ const QUESTIONS_OUTPUT_SCHEMA = {
465
+ type: 'object',
466
+ properties: {
467
+ treeHash: { type: ['string', 'null'] },
468
+ persona: { type: 'string', enum: ['productOwner', 'engineer'] },
469
+ level: { type: 'string', enum: ['intent', 'build'] },
470
+ ok: { type: 'boolean' },
471
+ remaining: { type: 'integer' },
472
+ groups: {
473
+ type: 'array',
474
+ items: {
475
+ type: 'object',
476
+ properties: {
477
+ key: { type: 'string' },
478
+ label: { type: 'string' },
479
+ questionIds: { type: 'array', items: { type: 'string' } },
480
+ },
481
+ required: ['key', 'label', 'questionIds'],
482
+ },
483
+ },
484
+ questions: {
485
+ type: 'array',
486
+ items: {
487
+ type: 'object',
488
+ properties: {
489
+ id: { type: 'string' },
490
+ stage: { type: 'string' },
491
+ check: { type: 'string' },
492
+ itemId: { type: 'string' },
493
+ heading: { type: 'string' },
494
+ ask: { type: 'string' },
495
+ context: { type: 'object' },
496
+ answerKinds: { type: 'array', items: { type: 'string' } },
497
+ writeBack: {
498
+ type: 'array',
499
+ items: {
500
+ type: 'object',
501
+ properties: {
502
+ when: { type: 'string' },
503
+ command: { type: 'string' },
504
+ },
505
+ required: ['when', 'command'],
506
+ },
507
+ },
508
+ blocks: { type: 'string', enum: ['intent', 'build'] },
509
+ },
510
+ required: ['id', 'stage', 'check', 'itemId', 'heading', 'ask', 'answerKinds', 'writeBack', 'blocks'],
511
+ },
512
+ },
513
+ optional: {
514
+ type: 'array',
515
+ items: {
516
+ type: 'object',
517
+ properties: {
518
+ id: { type: 'string' },
519
+ ask: { type: 'string' },
520
+ blocks: { type: 'string', enum: ['build'] },
521
+ },
522
+ required: ['id', 'ask', 'blocks'],
523
+ },
524
+ },
525
+ engineer: {
526
+ type: 'object',
527
+ properties: {
528
+ blockers: { type: 'integer' },
529
+ nextAction: { type: ['object', 'null'] },
530
+ },
531
+ },
532
+ },
533
+ required: ['treeHash', 'persona', 'level', 'ok', 'remaining', 'groups', 'questions', 'optional'],
534
+ };
535
+
536
+ // REQ-186: `rcf_define_readiness` returns the readiness envelope.
537
+ // Kept loose so new fields on readiness do not force an output-schema
538
+ // bump; the parity test over the verb's --json locks the actual shape.
539
+ const READINESS_OUTPUT_SCHEMA = { type: 'object' };
540
+
541
+ // REQ-186: `rcf_define_ledger` wraps the four-ledger handler.
542
+ // Return shape depends on the verb, so stays loose and the handler
543
+ // echoes the verb's own JSON envelope verbatim.
544
+ const LEDGER_OUTPUT_SCHEMA = { type: 'object' };
545
+
451
546
  // ---------------------------------------------------------------------------
452
547
  // Tool definitions (D5-D7, D17)
453
548
  // ---------------------------------------------------------------------------
@@ -661,6 +756,73 @@ const DEFINITIONS = [
661
756
  outputSchema: withErrorPayload(BUILD_OUTPUT_SCHEMA),
662
757
  annotations: { readOnlyHint: true },
663
758
  },
759
+ {
760
+ name: 'rcf_define_questions',
761
+ title: 'Persona question set for the intent-complete loop',
762
+ description: 'Returns the product-owner question set: one plain question per failing product-owner check with the exact write-back command that answers it. The compute is pure: no file under rcf/ is created or modified. Answers land through the write-back the question carries, then readiness recomputes; the loop ends when levels.intentComplete.ok is true. Method: a product-owner session starts with rcf_define_questions {persona: "productOwner"} and ends at the empty question set. {persona: "engineer"} returns the same shape over engineer blockers, but per-check question templates and write-backs for the engineer register are not part of DEFINE step 3 PR 2 (spec section 1.1 defers them to step 4); engineer-persona entries today carry empty answerKinds and writeBack and the harness runs the engineer verb by hand off the raw blocker.',
763
+ inputSchema: {
764
+ type: 'object',
765
+ properties: {
766
+ persona: { type: 'string', enum: ['productOwner', 'engineer'], description: 'Defaults to the register marker in rcf/.identity/profile.md, else productOwner.' },
767
+ stage: { type: 'string', description: 'Narrow to one stage (D1..D8 or short name).' },
768
+ limit: { type: 'integer', minimum: 0, description: 'Cap questions[] length; `remaining` still counts every matching item.' },
769
+ },
770
+ additionalProperties: false,
771
+ },
772
+ outputSchema: withErrorPayload(QUESTIONS_OUTPUT_SCHEMA),
773
+ annotations: { readOnlyHint: true },
774
+ },
775
+ {
776
+ name: 'rcf_define_readiness',
777
+ title: 'DEFINE stage-gate readiness view',
778
+ description: 'Returns the DEFINE readiness object: freezeable verdict, every stage/check state, levels (intentComplete, readyToBuild), personas (productOwner, engineer), delta, coverage and open decisions. The same object the Readiness tab consumes and the verb\'s --json prints. Writes nothing.',
779
+ inputSchema: {
780
+ type: 'object',
781
+ properties: {
782
+ level: { type: 'string', enum: ['intent', 'build'], description: 'Informational echo on the envelope _meta; does not filter the compute.' },
783
+ check: { type: 'string', description: 'Narrow the text summary to one stage (D1..D8 or short name). The structuredContent still carries every stage.' },
784
+ },
785
+ additionalProperties: false,
786
+ },
787
+ outputSchema: withErrorPayload(READINESS_OUTPUT_SCHEMA),
788
+ annotations: { readOnlyHint: true },
789
+ },
790
+ {
791
+ name: 'rcf_define_ledger',
792
+ title: 'Sidecar-ledger CRUD under rcf/define/',
793
+ description: 'Thin wrapper over the `rcf define ledger` handler. verb is one of add | update | resolve | list over one of the four ledgers (brief | decisions | concerns | probes). The command\'s JSON envelope is returned as structuredContent.',
794
+ inputSchema: {
795
+ type: 'object',
796
+ properties: {
797
+ name: { type: 'string', enum: ['brief', 'decisions', 'concerns', 'probes'] },
798
+ verb: { type: 'string', enum: ['add', 'update', 'resolve', 'list'] },
799
+ id: { type: 'integer', minimum: 1, description: 'Required for update and resolve: the statement id.' },
800
+ text: { type: 'string' },
801
+ kind: { type: 'string' },
802
+ source: { type: 'string' },
803
+ from: { type: 'string', description: 'Path to a statement file (brief add --from).' },
804
+ resolvedBy: { type: 'string', description: 'Closed-grammar pointer: REQ-nnn | TAD.entity:<name> | PRD.user:<name> | TAD.system:<name> | TAC-nnnn | omitted:<reason>.' },
805
+ answer: { type: 'string' },
806
+ question: { type: 'string' },
807
+ option: { type: 'array', items: { type: 'string' }, description: 'Letter-prefixed decision options, e.g. ["a:accept", "b:reject"].' },
808
+ default: { type: 'string', description: 'Decision default option letter.' },
809
+ blocks: { type: 'string', description: 'Decision blocks selector, e.g. "brief:7".' },
810
+ scan: { type: 'boolean' },
811
+ findings: { type: 'string' },
812
+ dryRun: { type: 'boolean' },
813
+ reason: { type: 'string', description: 'resolve/add reason (concerns, probes).' },
814
+ req: { type: 'string', description: 'REQ-id for concerns and probes.' },
815
+ concern: { type: 'string', description: 'Concern slug (auth, retention...).' },
816
+ disposition: { type: 'string', enum: ['applied', 'waived'], description: 'Concerns disposition.' },
817
+ finding: { type: 'string', description: 'Probe finding text.' },
818
+ severity: { type: 'string', enum: ['low', 'medium', 'high'], description: 'Probe finding severity.' },
819
+ },
820
+ required: ['name', 'verb'],
821
+ additionalProperties: false,
822
+ },
823
+ outputSchema: withErrorPayload(LEDGER_OUTPUT_SCHEMA),
824
+ annotations: { destructiveHint: false },
825
+ },
664
826
  ];
665
827
 
666
828
  // ---------------------------------------------------------------------------
@@ -849,6 +1011,92 @@ function okResult(envelope) {
849
1011
  };
850
1012
  }
851
1013
 
1014
+ /**
1015
+ * Read `rcf/.identity/profile.md` for the questions / readiness
1016
+ * handlers. Returns null on any error (absence is a finding, not a
1017
+ * tool error).
1018
+ */
1019
+ async function readProfileTextForMcp(projectRoot) {
1020
+ try {
1021
+ return await readFile(join(projectRoot, 'rcf', '.identity', 'profile.md'), 'utf8');
1022
+ } catch {
1023
+ return null;
1024
+ }
1025
+ }
1026
+
1027
+ /**
1028
+ * Pick the ACTIVE register marker from the profile text. The template
1029
+ * mentions all three marker words in its explanation; the chosen
1030
+ * value is the bare word on a line of its own. The last such line
1031
+ * wins; a "Register: engineer" prose line is honoured via the
1032
+ * last-occurrence fallback. Matches the CLI's pickRegister.
1033
+ */
1034
+ function pickRegisterForMcp(text) {
1035
+ if (typeof text !== 'string' || text.length === 0) return 'unstated';
1036
+ const markers = ['productOwner', 'engineer', 'unstated'];
1037
+ const lines = text.split(/\r?\n/);
1038
+ let bareLine = null;
1039
+ for (const raw of lines) {
1040
+ const trimmed = raw.trim();
1041
+ if (markers.includes(trimmed)) bareLine = trimmed;
1042
+ }
1043
+ if (bareLine) return /** @type {any} */ (bareLine);
1044
+ let bestIdx = -1;
1045
+ let best = null;
1046
+ for (const m of markers) {
1047
+ const idx = text.lastIndexOf(m);
1048
+ if (idx > bestIdx) { bestIdx = idx; best = m; }
1049
+ }
1050
+ return /** @type {any} */ (best) ?? 'unstated';
1051
+ }
1052
+
1053
+ /**
1054
+ * Translate the MCP tool arguments for `rcf_define_ledger` into an
1055
+ * argv that `rcf define ledger` understands. Shared vocabulary and
1056
+ * `--json` always appended so the handler can round-trip the envelope.
1057
+ */
1058
+ function buildLedgerArgvFromMcp(args) {
1059
+ const argv = [args.name, args.verb];
1060
+ if (args.verb === 'update' || args.verb === 'resolve') {
1061
+ argv.push(String(args.id ?? ''));
1062
+ }
1063
+ const pushFlag = (name, value) => {
1064
+ if (value === undefined || value === null) return;
1065
+ argv.push(`--${name}`);
1066
+ argv.push(String(value));
1067
+ };
1068
+ const pushBool = (name, value) => {
1069
+ if (value === true) argv.push(`--${name}`);
1070
+ else if (value === false) argv.push(`--no-${name}`);
1071
+ };
1072
+ pushFlag('text', args.text);
1073
+ pushFlag('kind', args.kind);
1074
+ pushFlag('source', args.source);
1075
+ pushFlag('from', args.from);
1076
+ pushFlag('resolved-by', args.resolvedBy);
1077
+ pushFlag('answer', args.answer);
1078
+ pushFlag('question', args.question);
1079
+ pushFlag('default', args.default);
1080
+ pushFlag('blocks', args.blocks);
1081
+ pushFlag('findings', args.findings);
1082
+ pushFlag('reason', args.reason);
1083
+ pushFlag('req', args.req);
1084
+ pushFlag('concern', args.concern);
1085
+ pushFlag('disposition', args.disposition);
1086
+ pushFlag('finding', args.finding);
1087
+ pushFlag('severity', args.severity);
1088
+ if (Array.isArray(args.option)) {
1089
+ for (const o of args.option) {
1090
+ argv.push('--option');
1091
+ argv.push(String(o));
1092
+ }
1093
+ }
1094
+ if (args.scan !== undefined) pushBool('scan', args.scan);
1095
+ if (args.dryRun) argv.push('--dry-run');
1096
+ argv.push('--json');
1097
+ return argv;
1098
+ }
1099
+
852
1100
  /**
853
1101
  * Create the tool registry bound to one project root (D13: the root is
854
1102
  * fixed for the process lifetime).
@@ -1119,6 +1367,112 @@ export function createToolRegistry({ projectRoot, log }) {
1119
1367
  // (reconciliation carry 2, OQ-P7-8 posture).
1120
1368
  return okResult({ ok: true, mode: 'bundle', ...bundle });
1121
1369
  },
1370
+
1371
+ rcf_define_readiness: async (args) => {
1372
+ const { tree, errors } = await walkTree({ projectRoot });
1373
+ const staleErrors = await checkCodeNodeResolution({ projectRoot, tree });
1374
+ const validateErrors = [...errors, ...staleErrors];
1375
+ // Validate --check / --level inputs the same way the CLI does.
1376
+ let checkStage = null;
1377
+ if (typeof args.check === 'string') {
1378
+ if (args.check === 'all') checkStage = 'all';
1379
+ else if (STAGE_ALIASES[args.check]) checkStage = STAGE_ALIASES[args.check];
1380
+ else return usageErrorResult(`readiness: unknown --check '${args.check}'`);
1381
+ }
1382
+ const [freeze, ledgers, testPointers, profileText] = await Promise.all([
1383
+ loadFreezeRecord({ projectRoot }),
1384
+ loadAllLedgers({ projectRoot }),
1385
+ resolveTestPointers({ projectRoot, tree }),
1386
+ readProfileTextForMcp(projectRoot),
1387
+ ]);
1388
+ const result = computeReadiness(tree, {
1389
+ freeze, ledgers, profile: undefined, profileText, testPointers, validateErrors,
1390
+ });
1391
+ const level = args.level ?? null;
1392
+ return okResult({ ...result, _meta: { checkStage, level } });
1393
+ },
1394
+
1395
+ rcf_define_questions: async (args) => {
1396
+ const persona = args.persona;
1397
+ if (persona !== undefined && !QUESTIONS_PERSONAS.includes(persona)) {
1398
+ return usageErrorResult(`questions: unknown persona '${persona}' (expected one of ${QUESTIONS_PERSONAS.join(' | ')})`);
1399
+ }
1400
+ let stageFilter = null;
1401
+ if (typeof args.stage === 'string') {
1402
+ if (!STAGE_ALIASES[args.stage]) {
1403
+ return usageErrorResult(`questions: unknown --stage '${args.stage}'`);
1404
+ }
1405
+ stageFilter = STAGE_ALIASES[args.stage];
1406
+ }
1407
+ const { tree, errors } = await walkTree({ projectRoot });
1408
+ const staleErrors = await checkCodeNodeResolution({ projectRoot, tree });
1409
+ const validateErrors = [...errors, ...staleErrors];
1410
+ const [freeze, ledgers, testPointers, profileText] = await Promise.all([
1411
+ loadFreezeRecord({ projectRoot }),
1412
+ loadAllLedgers({ projectRoot }),
1413
+ resolveTestPointers({ projectRoot, tree }),
1414
+ readProfileTextForMcp(projectRoot),
1415
+ ]);
1416
+ let resolvedPersona = persona;
1417
+ if (resolvedPersona === undefined) {
1418
+ resolvedPersona = pickRegisterForMcp(profileText) === 'engineer' ? 'engineer' : 'productOwner';
1419
+ }
1420
+ const readiness = computeReadiness(tree, {
1421
+ freeze, ledgers, profile: undefined, profileText, testPointers, validateErrors,
1422
+ });
1423
+ const result = computeQuestions(readiness, {
1424
+ tree, ledgers, profileText, persona: resolvedPersona,
1425
+ });
1426
+ let filtered = result.questions;
1427
+ if (stageFilter) filtered = filtered.filter((q) => q.stage === stageFilter);
1428
+ const remaining = filtered.length;
1429
+ if (Number.isInteger(args.limit) && args.limit >= 0) {
1430
+ filtered = filtered.slice(0, args.limit);
1431
+ }
1432
+ const groupMap = new Map();
1433
+ for (const q of filtered) {
1434
+ const key = q.context?.statement?.source ?? q.context?.sourceSpan ?? 'tree';
1435
+ const existing = groupMap.get(key) ?? { key, label: result.groups.find((g) => g.key === key)?.label ?? key, questionIds: [] };
1436
+ existing.questionIds.push(q.id);
1437
+ groupMap.set(key, existing);
1438
+ }
1439
+ return okResult({
1440
+ ...result,
1441
+ questions: filtered,
1442
+ groups: [...groupMap.values()],
1443
+ remaining,
1444
+ });
1445
+ },
1446
+
1447
+ rcf_define_ledger: async (args) => {
1448
+ const argv = buildLedgerArgvFromMcp(args);
1449
+ // Capture the ledger CLI's stdout/stderr; the handler returns JSON
1450
+ // via --json which we parse back for structuredContent.
1451
+ let out = '';
1452
+ let err = '';
1453
+ const stdout = { write: (s) => { out += s; } };
1454
+ const stderr = { write: (s) => { err += s; } };
1455
+ let exitCode;
1456
+ try {
1457
+ exitCode = await ledgerCliMain(argv, { stdout, stderr, cwd: projectRoot });
1458
+ } catch (e) {
1459
+ return unexpectedFailureResult({ kind: 'ioFailure', message: /** @type {Error} */ (e).message, stack: /** @type {Error} */ (e).stack }, log);
1460
+ }
1461
+ if (exitCode === 2) {
1462
+ return usageErrorResult(`ledger: ${err.trim() || 'usage error'}`);
1463
+ }
1464
+ if (exitCode !== 0) {
1465
+ return errorResult([{ kind: 'ioFailure', message: `ledger ${args.verb} failed: ${err.trim() || out.trim()}`, documentId: null, field: null, rule: null }]);
1466
+ }
1467
+ // Try to parse JSON; if the verb printed text, pass the text back.
1468
+ let envelope;
1469
+ try {
1470
+ envelope = JSON.parse(out);
1471
+ } catch {
1472
+ envelope = { ok: true, name: args.name, verb: args.verb, text: out };
1473
+ }
1474
+ return okResult(envelope);
1475
+ },
1122
1476
  };
1123
1477
 
1124
1478
  async function linkHandler(args, removing) {
@@ -177,6 +177,10 @@ function standaloneIds(tree) {
177
177
  * Ledger-brief statement ids above the freeze high-water mark.
178
178
  * @property {number} unchanged count of ids present in both maps with
179
179
  * the same hash (excludes ledgers).
180
+ * @property {Record<string, string>} currentDocHashes
181
+ * The live per-document hashes keyed by id (ledgers appear under
182
+ * `ledger:<name>`). Exposed so the viewer can show a document-level
183
+ * hash diff for every changed/added id (spec section 5 block 6).
180
184
  */
181
185
 
182
186
  /**
@@ -221,6 +225,7 @@ export function computeDelta(tree, freeze, ledgers) {
221
225
  removed: [],
222
226
  briefSince: [],
223
227
  unchanged: 0,
228
+ currentDocHashes: { ...currentHashes },
224
229
  };
225
230
  }
226
231
 
@@ -272,5 +277,6 @@ export function computeDelta(tree, freeze, ledgers) {
272
277
  removed,
273
278
  briefSince,
274
279
  unchanged,
280
+ currentDocHashes: { ...currentHashes },
275
281
  };
276
282
  }