@principles/pd-cli 1.73.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 (298) hide show
  1. package/README.md +90 -0
  2. package/dist/commands/artifact.d.ts +14 -0
  3. package/dist/commands/artifact.d.ts.map +1 -0
  4. package/dist/commands/artifact.js +67 -0
  5. package/dist/commands/artifact.js.map +1 -0
  6. package/dist/commands/candidate.d.ts +83 -0
  7. package/dist/commands/candidate.d.ts.map +1 -0
  8. package/dist/commands/candidate.js +891 -0
  9. package/dist/commands/candidate.js.map +1 -0
  10. package/dist/commands/central-sync.d.ts +10 -0
  11. package/dist/commands/central-sync.d.ts.map +1 -0
  12. package/dist/commands/central-sync.js +32 -0
  13. package/dist/commands/central-sync.js.map +1 -0
  14. package/dist/commands/console.d.ts +9 -0
  15. package/dist/commands/console.d.ts.map +1 -0
  16. package/dist/commands/console.js +114 -0
  17. package/dist/commands/console.js.map +1 -0
  18. package/dist/commands/context.d.ts +7 -0
  19. package/dist/commands/context.d.ts.map +1 -0
  20. package/dist/commands/context.js +55 -0
  21. package/dist/commands/context.js.map +1 -0
  22. package/dist/commands/demo-story-a.d.ts +12 -0
  23. package/dist/commands/demo-story-a.d.ts.map +1 -0
  24. package/dist/commands/demo-story-a.js +175 -0
  25. package/dist/commands/demo-story-a.js.map +1 -0
  26. package/dist/commands/diagnose.d.ts +35 -0
  27. package/dist/commands/diagnose.d.ts.map +1 -0
  28. package/dist/commands/diagnose.js +390 -0
  29. package/dist/commands/diagnose.js.map +1 -0
  30. package/dist/commands/evolution-tasks-list.d.ts +15 -0
  31. package/dist/commands/evolution-tasks-list.d.ts.map +1 -0
  32. package/dist/commands/evolution-tasks-list.js +34 -0
  33. package/dist/commands/evolution-tasks-list.js.map +1 -0
  34. package/dist/commands/evolution-tasks-show.d.ts +14 -0
  35. package/dist/commands/evolution-tasks-show.d.ts.map +1 -0
  36. package/dist/commands/evolution-tasks-show.js +52 -0
  37. package/dist/commands/evolution-tasks-show.js.map +1 -0
  38. package/dist/commands/flow.d.ts +7 -0
  39. package/dist/commands/flow.d.ts.map +1 -0
  40. package/dist/commands/flow.js +57 -0
  41. package/dist/commands/flow.js.map +1 -0
  42. package/dist/commands/health.d.ts +16 -0
  43. package/dist/commands/health.d.ts.map +1 -0
  44. package/dist/commands/health.js +150 -0
  45. package/dist/commands/health.js.map +1 -0
  46. package/dist/commands/history.d.ts +11 -0
  47. package/dist/commands/history.d.ts.map +1 -0
  48. package/dist/commands/history.js +50 -0
  49. package/dist/commands/history.js.map +1 -0
  50. package/dist/commands/legacy-cleanup.d.ts +27 -0
  51. package/dist/commands/legacy-cleanup.d.ts.map +1 -0
  52. package/dist/commands/legacy-cleanup.js +171 -0
  53. package/dist/commands/legacy-cleanup.js.map +1 -0
  54. package/dist/commands/legacy-import.d.ts +7 -0
  55. package/dist/commands/legacy-import.d.ts.map +1 -0
  56. package/dist/commands/legacy-import.js +86 -0
  57. package/dist/commands/legacy-import.js.map +1 -0
  58. package/dist/commands/pain-record.d.ts +10 -0
  59. package/dist/commands/pain-record.d.ts.map +1 -0
  60. package/dist/commands/pain-record.js +162 -0
  61. package/dist/commands/pain-record.js.map +1 -0
  62. package/dist/commands/proven-channel-baseline.d.ts +12 -0
  63. package/dist/commands/proven-channel-baseline.d.ts.map +1 -0
  64. package/dist/commands/proven-channel-baseline.js +97 -0
  65. package/dist/commands/proven-channel-baseline.js.map +1 -0
  66. package/dist/commands/remediation-output.d.ts +40 -0
  67. package/dist/commands/remediation-output.d.ts.map +1 -0
  68. package/dist/commands/remediation-output.js +23 -0
  69. package/dist/commands/remediation-output.js.map +1 -0
  70. package/dist/commands/run.d.ts +10 -0
  71. package/dist/commands/run.d.ts.map +1 -0
  72. package/dist/commands/run.js +68 -0
  73. package/dist/commands/run.js.map +1 -0
  74. package/dist/commands/runtime-activation.d.ts +11 -0
  75. package/dist/commands/runtime-activation.d.ts.map +1 -0
  76. package/dist/commands/runtime-activation.js +150 -0
  77. package/dist/commands/runtime-activation.js.map +1 -0
  78. package/dist/commands/runtime-canary.d.ts +30 -0
  79. package/dist/commands/runtime-canary.d.ts.map +1 -0
  80. package/dist/commands/runtime-canary.js +343 -0
  81. package/dist/commands/runtime-canary.js.map +1 -0
  82. package/dist/commands/runtime-diagnostics-export.d.ts +20 -0
  83. package/dist/commands/runtime-diagnostics-export.d.ts.map +1 -0
  84. package/dist/commands/runtime-diagnostics-export.js +177 -0
  85. package/dist/commands/runtime-diagnostics-export.js.map +1 -0
  86. package/dist/commands/runtime-features.d.ts +26 -0
  87. package/dist/commands/runtime-features.d.ts.map +1 -0
  88. package/dist/commands/runtime-features.js +70 -0
  89. package/dist/commands/runtime-features.js.map +1 -0
  90. package/dist/commands/runtime-gfi-snapshot.d.ts +7 -0
  91. package/dist/commands/runtime-gfi-snapshot.d.ts.map +1 -0
  92. package/dist/commands/runtime-gfi-snapshot.js +101 -0
  93. package/dist/commands/runtime-gfi-snapshot.js.map +1 -0
  94. package/dist/commands/runtime-health-snapshot.d.ts +7 -0
  95. package/dist/commands/runtime-health-snapshot.d.ts.map +1 -0
  96. package/dist/commands/runtime-health-snapshot.js +93 -0
  97. package/dist/commands/runtime-health-snapshot.js.map +1 -0
  98. package/dist/commands/runtime-idle-trigger.d.ts +12 -0
  99. package/dist/commands/runtime-idle-trigger.d.ts.map +1 -0
  100. package/dist/commands/runtime-idle-trigger.js +102 -0
  101. package/dist/commands/runtime-idle-trigger.js.map +1 -0
  102. package/dist/commands/runtime-internalization-enqueue-successors.d.ts +9 -0
  103. package/dist/commands/runtime-internalization-enqueue-successors.d.ts.map +1 -0
  104. package/dist/commands/runtime-internalization-enqueue-successors.js +393 -0
  105. package/dist/commands/runtime-internalization-enqueue-successors.js.map +1 -0
  106. package/dist/commands/runtime-internalization-integrity-repair.d.ts +9 -0
  107. package/dist/commands/runtime-internalization-integrity-repair.d.ts.map +1 -0
  108. package/dist/commands/runtime-internalization-integrity-repair.js +54 -0
  109. package/dist/commands/runtime-internalization-integrity-repair.js.map +1 -0
  110. package/dist/commands/runtime-internalization-integrity.d.ts +7 -0
  111. package/dist/commands/runtime-internalization-integrity.d.ts.map +1 -0
  112. package/dist/commands/runtime-internalization-integrity.js +53 -0
  113. package/dist/commands/runtime-internalization-integrity.js.map +1 -0
  114. package/dist/commands/runtime-internalization-queue.d.ts +7 -0
  115. package/dist/commands/runtime-internalization-queue.d.ts.map +1 -0
  116. package/dist/commands/runtime-internalization-queue.js +85 -0
  117. package/dist/commands/runtime-internalization-queue.js.map +1 -0
  118. package/dist/commands/runtime-internalization-run-once.d.ts +12 -0
  119. package/dist/commands/runtime-internalization-run-once.d.ts.map +1 -0
  120. package/dist/commands/runtime-internalization-run-once.js +546 -0
  121. package/dist/commands/runtime-internalization-run-once.js.map +1 -0
  122. package/dist/commands/runtime-internalization-wake-once.d.ts +8 -0
  123. package/dist/commands/runtime-internalization-wake-once.d.ts.map +1 -0
  124. package/dist/commands/runtime-internalization-wake-once.js +72 -0
  125. package/dist/commands/runtime-internalization-wake-once.js.map +1 -0
  126. package/dist/commands/runtime-pain-flood-simulation.d.ts +10 -0
  127. package/dist/commands/runtime-pain-flood-simulation.d.ts.map +1 -0
  128. package/dist/commands/runtime-pain-flood-simulation.js +104 -0
  129. package/dist/commands/runtime-pain-flood-simulation.js.map +1 -0
  130. package/dist/commands/runtime-pruning.d.ts +45 -0
  131. package/dist/commands/runtime-pruning.d.ts.map +1 -0
  132. package/dist/commands/runtime-pruning.js +355 -0
  133. package/dist/commands/runtime-pruning.js.map +1 -0
  134. package/dist/commands/runtime-recovery.d.ts +9 -0
  135. package/dist/commands/runtime-recovery.d.ts.map +1 -0
  136. package/dist/commands/runtime-recovery.js +94 -0
  137. package/dist/commands/runtime-recovery.js.map +1 -0
  138. package/dist/commands/runtime-synthetic-baseline.d.ts +7 -0
  139. package/dist/commands/runtime-synthetic-baseline.d.ts.map +1 -0
  140. package/dist/commands/runtime-synthetic-baseline.js +59 -0
  141. package/dist/commands/runtime-synthetic-baseline.js.map +1 -0
  142. package/dist/commands/runtime-uat.d.ts +52 -0
  143. package/dist/commands/runtime-uat.d.ts.map +1 -0
  144. package/dist/commands/runtime-uat.js +274 -0
  145. package/dist/commands/runtime-uat.js.map +1 -0
  146. package/dist/commands/runtime.d.ts +20 -0
  147. package/dist/commands/runtime.d.ts.map +1 -0
  148. package/dist/commands/runtime.js +256 -0
  149. package/dist/commands/runtime.js.map +1 -0
  150. package/dist/commands/samples-list.d.ts +11 -0
  151. package/dist/commands/samples-list.d.ts.map +1 -0
  152. package/dist/commands/samples-list.js +37 -0
  153. package/dist/commands/samples-list.js.map +1 -0
  154. package/dist/commands/samples-review.d.ts +14 -0
  155. package/dist/commands/samples-review.d.ts.map +1 -0
  156. package/dist/commands/samples-review.js +22 -0
  157. package/dist/commands/samples-review.js.map +1 -0
  158. package/dist/commands/task.d.ts +14 -0
  159. package/dist/commands/task.d.ts.map +1 -0
  160. package/dist/commands/task.js +92 -0
  161. package/dist/commands/task.js.map +1 -0
  162. package/dist/commands/trace.d.ts +19 -0
  163. package/dist/commands/trace.d.ts.map +1 -0
  164. package/dist/commands/trace.js +154 -0
  165. package/dist/commands/trace.js.map +1 -0
  166. package/dist/commands/trajectory.d.ts +11 -0
  167. package/dist/commands/trajectory.d.ts.map +1 -0
  168. package/dist/commands/trajectory.js +47 -0
  169. package/dist/commands/trajectory.js.map +1 -0
  170. package/dist/index.d.ts +9 -0
  171. package/dist/index.d.ts.map +1 -0
  172. package/dist/index.js +736 -0
  173. package/dist/index.js.map +1 -0
  174. package/dist/legacy/legacy-import.d.ts +15 -0
  175. package/dist/legacy/legacy-import.d.ts.map +1 -0
  176. package/dist/legacy/legacy-import.js +141 -0
  177. package/dist/legacy/legacy-import.js.map +1 -0
  178. package/dist/legacy/session-history-import.d.ts +26 -0
  179. package/dist/legacy/session-history-import.d.ts.map +1 -0
  180. package/dist/legacy/session-history-import.js +151 -0
  181. package/dist/legacy/session-history-import.js.map +1 -0
  182. package/dist/principle-tree-ledger-adapter.d.ts +12 -0
  183. package/dist/principle-tree-ledger-adapter.d.ts.map +1 -0
  184. package/dist/principle-tree-ledger-adapter.js +12 -0
  185. package/dist/principle-tree-ledger-adapter.js.map +1 -0
  186. package/dist/resolve-workspace.d.ts +12 -0
  187. package/dist/resolve-workspace.d.ts.map +1 -0
  188. package/dist/resolve-workspace.js +20 -0
  189. package/dist/resolve-workspace.js.map +1 -0
  190. package/dist/services/demo-story-a-runner.d.ts +8 -0
  191. package/dist/services/demo-story-a-runner.d.ts.map +1 -0
  192. package/dist/services/demo-story-a-runner.js +369 -0
  193. package/dist/services/demo-story-a-runner.js.map +1 -0
  194. package/dist/services/feature-flag-loader.d.ts +6 -0
  195. package/dist/services/feature-flag-loader.d.ts.map +1 -0
  196. package/dist/services/feature-flag-loader.js +54 -0
  197. package/dist/services/feature-flag-loader.js.map +1 -0
  198. package/dist/services/pain-flood-simulation-runner.d.ts +10 -0
  199. package/dist/services/pain-flood-simulation-runner.d.ts.map +1 -0
  200. package/dist/services/pain-flood-simulation-runner.js +289 -0
  201. package/dist/services/pain-flood-simulation-runner.js.map +1 -0
  202. package/dist/services/proven-channel-baseline-runner.d.ts +12 -0
  203. package/dist/services/proven-channel-baseline-runner.d.ts.map +1 -0
  204. package/dist/services/proven-channel-baseline-runner.js +114 -0
  205. package/dist/services/proven-channel-baseline-runner.js.map +1 -0
  206. package/dist/services/synthetic-baseline-runner.d.ts +8 -0
  207. package/dist/services/synthetic-baseline-runner.d.ts.map +1 -0
  208. package/dist/services/synthetic-baseline-runner.js +251 -0
  209. package/dist/services/synthetic-baseline-runner.js.map +1 -0
  210. package/package.json +35 -0
  211. package/src/commands/artifact.ts +82 -0
  212. package/src/commands/candidate.ts +1117 -0
  213. package/src/commands/central-sync.ts +44 -0
  214. package/src/commands/console.ts +121 -0
  215. package/src/commands/context.ts +72 -0
  216. package/src/commands/demo-story-a.ts +195 -0
  217. package/src/commands/diagnose.ts +452 -0
  218. package/src/commands/evolution-tasks-list.ts +44 -0
  219. package/src/commands/evolution-tasks-show.ts +60 -0
  220. package/src/commands/flow.ts +60 -0
  221. package/src/commands/health.ts +189 -0
  222. package/src/commands/history.ts +63 -0
  223. package/src/commands/legacy-cleanup.ts +206 -0
  224. package/src/commands/legacy-import.ts +104 -0
  225. package/src/commands/pain-record.ts +167 -0
  226. package/src/commands/proven-channel-baseline.ts +113 -0
  227. package/src/commands/remediation-output.ts +66 -0
  228. package/src/commands/run.ts +89 -0
  229. package/src/commands/runtime-activation.ts +176 -0
  230. package/src/commands/runtime-canary.ts +371 -0
  231. package/src/commands/runtime-diagnostics-export.ts +229 -0
  232. package/src/commands/runtime-features.ts +103 -0
  233. package/src/commands/runtime-gfi-snapshot.ts +135 -0
  234. package/src/commands/runtime-health-snapshot.ts +106 -0
  235. package/src/commands/runtime-internalization-enqueue-successors.ts +479 -0
  236. package/src/commands/runtime-internalization-integrity-repair.ts +69 -0
  237. package/src/commands/runtime-internalization-integrity.ts +63 -0
  238. package/src/commands/runtime-internalization-queue.ts +106 -0
  239. package/src/commands/runtime-internalization-run-once.ts +658 -0
  240. package/src/commands/runtime-internalization-wake-once.ts +87 -0
  241. package/src/commands/runtime-pain-flood-simulation.ts +121 -0
  242. package/src/commands/runtime-pruning.ts +438 -0
  243. package/src/commands/runtime-recovery.ts +107 -0
  244. package/src/commands/runtime-synthetic-baseline.ts +70 -0
  245. package/src/commands/runtime-uat.ts +339 -0
  246. package/src/commands/runtime.ts +281 -0
  247. package/src/commands/samples-list.ts +43 -0
  248. package/src/commands/samples-review.ts +32 -0
  249. package/src/commands/task.ts +130 -0
  250. package/src/commands/trace.ts +174 -0
  251. package/src/commands/trajectory.ts +64 -0
  252. package/src/index.ts +829 -0
  253. package/src/legacy/legacy-import.ts +179 -0
  254. package/src/legacy/session-history-import.ts +231 -0
  255. package/src/principle-tree-ledger-adapter.ts +13 -0
  256. package/src/resolve-workspace.ts +20 -0
  257. package/src/services/demo-story-a-runner.ts +472 -0
  258. package/src/services/feature-flag-loader.ts +73 -0
  259. package/src/services/pain-flood-simulation-runner.ts +354 -0
  260. package/src/services/proven-channel-baseline-runner.ts +150 -0
  261. package/src/services/synthetic-baseline-runner.ts +291 -0
  262. package/tests/commands/candidate-audit-repair.test.ts +338 -0
  263. package/tests/commands/candidate-intake.test.ts +589 -0
  264. package/tests/commands/candidate-internalization-backfill.test.ts +480 -0
  265. package/tests/commands/candidate-internalize.test.ts +272 -0
  266. package/tests/commands/candidate-route.test.ts +328 -0
  267. package/tests/commands/candidate-show.test.ts +95 -0
  268. package/tests/commands/cli-command-tree.test.ts +64 -0
  269. package/tests/commands/context.test.ts +114 -0
  270. package/tests/commands/demo-story-a.test.ts +255 -0
  271. package/tests/commands/diagnose.test.ts +792 -0
  272. package/tests/commands/health.test.ts +330 -0
  273. package/tests/commands/pain-record.test.ts +316 -0
  274. package/tests/commands/plugin-config-resolution-cutover.test.ts +220 -0
  275. package/tests/commands/proven-channel-baseline.test.ts +441 -0
  276. package/tests/commands/runtime-activation.test.ts +168 -0
  277. package/tests/commands/runtime-canary.test.ts +369 -0
  278. package/tests/commands/runtime-diagnostics-export.test.ts +170 -0
  279. package/tests/commands/runtime-features.test.ts +114 -0
  280. package/tests/commands/runtime-health-snapshot.test.ts +357 -0
  281. package/tests/commands/runtime-internalization-enqueue-successors.test.ts +803 -0
  282. package/tests/commands/runtime-internalization-integrity-repair.test.ts +169 -0
  283. package/tests/commands/runtime-internalization-integrity.test.ts +102 -0
  284. package/tests/commands/runtime-internalization-queue.test.ts +252 -0
  285. package/tests/commands/runtime-internalization-run-once.test.ts +1318 -0
  286. package/tests/commands/runtime-internalization-wake-once.test.ts +170 -0
  287. package/tests/commands/runtime-internalization.test.ts +52 -0
  288. package/tests/commands/runtime-pain-flood-simulation.test.ts +418 -0
  289. package/tests/commands/runtime-pruning.test.ts +693 -0
  290. package/tests/commands/runtime-recovery.test.ts +96 -0
  291. package/tests/commands/runtime-synthetic-baseline.test.ts +249 -0
  292. package/tests/commands/runtime-uat.test.ts +397 -0
  293. package/tests/commands/runtime.test.ts +262 -0
  294. package/tests/commands/trace.test.ts +314 -0
  295. package/tests/e2e/candidate-intake-e2e.test.ts +316 -0
  296. package/tests/services/feature-flag-loader.test.ts +207 -0
  297. package/tests/services/proven-channel-baseline-runner.test.ts +30 -0
  298. package/tsconfig.json +26 -0
@@ -0,0 +1,64 @@
1
+ /**
2
+ * CLI command tree structure tests — verify command placement.
3
+ *
4
+ * These tests ensure that commands are registered at the correct path in the CLI tree.
5
+ */
6
+ import { describe, it, expect } from 'vitest';
7
+ import { execFileSync } from 'node:child_process';
8
+
9
+ function runPdHelp(args: string[]): string {
10
+ try {
11
+ return execFileSync('node', ['packages/pd-cli/dist/index.js', ...args], {
12
+ encoding: 'utf8',
13
+ cwd: 'D:/Code/principles',
14
+ });
15
+ } catch (err: unknown) {
16
+ if (err && typeof err === 'object' && 'stdout' in err) {
17
+ return String((err as { stdout: unknown }).stdout);
18
+ }
19
+ throw err;
20
+ }
21
+ }
22
+
23
+ describe('CLI command tree structure', () => {
24
+ it('uat command exists under runtime (pd runtime uat --help)', () => {
25
+ const output = runPdHelp(['runtime', 'uat', '--help']);
26
+ // Should contain UAT-specific options
27
+ expect(output).toContain('--workspace');
28
+ expect(output).toContain('--count');
29
+ expect(output).toContain('--min-success-rate');
30
+ expect(output).toContain('--json');
31
+ });
32
+
33
+ it('uat command description mentions Runtime V2 chain UAT', () => {
34
+ const output = runPdHelp(['runtime', 'uat', '--help']);
35
+ expect(output).toContain('UAT');
36
+ });
37
+
38
+ it('runtime subcommand list includes uat (pd runtime --help)', () => {
39
+ const output = runPdHelp(['runtime', '--help']);
40
+ // Should list 'uat' as a subcommand
41
+ expect(output).toMatch(/uat\s/);
42
+ });
43
+
44
+ it('pruning subcommand list does NOT include uat (pd runtime pruning --help)', () => {
45
+ const output = runPdHelp(['runtime', 'pruning', '--help']);
46
+ // Should only have report, explain, review
47
+ expect(output).toContain('report');
48
+ expect(output).toContain('explain');
49
+ expect(output).toContain('review');
50
+ // Should NOT have uat
51
+ expect(output).not.toMatch(/uat\s/);
52
+ });
53
+
54
+ it('health snapshot command exists under runtime health (pd runtime health snapshot --help)', () => {
55
+ const output = runPdHelp(['runtime', 'health', 'snapshot', '--help']);
56
+ expect(output).toContain('--workspace');
57
+ expect(output).toContain('--json');
58
+ });
59
+
60
+ it('runtime subcommand list includes health (pd runtime --help)', () => {
61
+ const output = runPdHelp(['runtime', '--help']);
62
+ expect(output).toMatch(/health\s/);
63
+ });
64
+ });
@@ -0,0 +1,114 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+
3
+ const { MockSqliteConnection, MockSqliteTaskStore, MockSqliteRunStore } = vi.hoisted(() => {
4
+ class MockSqliteConnection {
5
+ close = vi.fn();
6
+ }
7
+ class MockSqliteTaskStore {}
8
+ class MockSqliteRunStore {}
9
+ return { MockSqliteConnection, MockSqliteTaskStore, MockSqliteRunStore };
10
+ }, { validateType: true });
11
+
12
+ vi.mock('../../src/resolve-workspace.js', () => ({
13
+ resolveWorkspaceDir: vi.fn().mockReturnValue('/tmp/fake-workspace'),
14
+ }));
15
+
16
+ const contextAssemblerInstance = {
17
+ assemble: vi.fn().mockResolvedValue({
18
+ contextId: 'ctx-001',
19
+ contextHash: 'abc123def4567890',
20
+ workspaceDir: '/tmp/fake-workspace',
21
+ sourceRefs: ['pain-001'],
22
+ conversationWindow: [],
23
+ ambiguityNotes: [],
24
+ diagnosisTarget: { taskId: 'diag-001' },
25
+ fullTrace: [
26
+ { taskId: 'source-task-001', sourcePainId: 'pain-001', runs: [] },
27
+ ],
28
+ }),
29
+ };
30
+
31
+ vi.mock('@principles/core', () => {
32
+ return {
33
+ SqliteConnection: vi.fn().mockImplementation(function () {
34
+ return new MockSqliteConnection();
35
+ }),
36
+ SqliteTaskStore: vi.fn().mockImplementation(function () {
37
+ return new MockSqliteTaskStore();
38
+ }),
39
+ SqliteRunStore: vi.fn().mockImplementation(function () {
40
+ return new MockSqliteRunStore();
41
+ }),
42
+ SqliteHistoryQuery: vi.fn().mockImplementation(function () { return {}; }),
43
+ SqliteContextAssembler: vi.fn().mockImplementation(function () {
44
+ return contextAssemblerInstance;
45
+ }),
46
+ SqliteTrajectoryLocator: vi.fn().mockImplementation(function () { return {}; }),
47
+ SqliteSourceTraceLocator: vi.fn().mockImplementation(function () { return {}; }),
48
+ };
49
+ });
50
+
51
+ import { handleContextBuild } from '../../src/commands/context.js';
52
+
53
+ describe('pd context build', () => {
54
+ beforeEach(() => {
55
+ vi.clearAllMocks();
56
+ contextAssemblerInstance.assemble.mockResolvedValue({
57
+ contextId: 'ctx-001',
58
+ contextHash: 'abc123def4567890',
59
+ workspaceDir: '/tmp/fake-workspace',
60
+ sourceRefs: ['pain-001'],
61
+ conversationWindow: [],
62
+ ambiguityNotes: [],
63
+ diagnosisTarget: { taskId: 'diag-001' },
64
+ fullTrace: [
65
+ { taskId: 'source-task-001', sourcePainId: 'pain-001', runs: [] },
66
+ ],
67
+ });
68
+ });
69
+
70
+ it('PRI-189: wires SourceTraceLocator into SqliteContextAssembler', async () => {
71
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
72
+
73
+ await handleContextBuild('diag-001', { json: true, workspace: '/tmp/fake-workspace' });
74
+
75
+ const { SqliteTrajectoryLocator, SqliteSourceTraceLocator, SqliteContextAssembler } =
76
+ await import('@principles/core');
77
+
78
+ expect(SqliteTrajectoryLocator).toHaveBeenCalledTimes(1);
79
+ expect(SqliteSourceTraceLocator).toHaveBeenCalledTimes(1);
80
+ expect(SqliteContextAssembler).toHaveBeenCalledTimes(1);
81
+
82
+ const assemblerCall = vi.mocked(SqliteContextAssembler).mock.calls[0];
83
+ const optionsArg = assemblerCall[3];
84
+ expect(optionsArg).toBeDefined();
85
+ expect(optionsArg).toHaveProperty('sourceTraceLocator');
86
+
87
+ consoleSpy.mockRestore();
88
+ });
89
+
90
+ it('PRI-189: context build --json outputs source-aligned fullTrace', async () => {
91
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
92
+
93
+ await handleContextBuild('diag-001', { json: true, workspace: '/tmp/fake-workspace' });
94
+
95
+ const logOutput = consoleSpy.mock.calls[0][0];
96
+ const parsed = JSON.parse(logOutput);
97
+ expect(parsed.fullTrace).toBeDefined();
98
+ expect(parsed.fullTrace.length).toBeGreaterThan(0);
99
+ expect(parsed.fullTrace[0].sourcePainId).toBe('pain-001');
100
+
101
+ consoleSpy.mockRestore();
102
+ });
103
+
104
+ it('PRI-189: context build human-readable output includes sourceRefs', async () => {
105
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
106
+
107
+ await handleContextBuild('diag-001', { json: false, workspace: '/tmp/fake-workspace' });
108
+
109
+ const allOutput = consoleSpy.mock.calls.map(c => c.join(' ')).join('\n');
110
+ expect(allOutput).toContain('pain-001');
111
+
112
+ consoleSpy.mockRestore();
113
+ });
114
+ });
@@ -0,0 +1,255 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import * as fs from 'fs';
3
+ import * as path from 'path';
4
+ import * as os from 'os';
5
+ import Database from 'better-sqlite3';
6
+ import { handleDemoStoryA, cleanupTempWorkspace } from '../../src/commands/demo-story-a.js';
7
+
8
+ describe('pd demo story-a CLI', () => {
9
+ let stdoutSpy: ReturnType<typeof vi.spyOn>;
10
+ let stderrSpy: ReturnType<typeof vi.spyOn>;
11
+ let originalExitCode: number | undefined;
12
+
13
+ beforeEach(() => {
14
+ stdoutSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
15
+ stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
16
+ originalExitCode = process.exitCode;
17
+ process.exitCode = undefined;
18
+ });
19
+
20
+ afterEach(() => {
21
+ stdoutSpy.mockRestore();
22
+ stderrSpy.mockRestore();
23
+ process.exitCode = originalExitCode;
24
+ });
25
+
26
+ it('outputs valid JSON in --json mode', async () => {
27
+ await handleDemoStoryA({ json: true });
28
+
29
+ expect(stdoutSpy).toHaveBeenCalled();
30
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
31
+ const parsed = JSON.parse(output);
32
+
33
+ expect(parsed.status).toBe('passed');
34
+ expect(parsed.stages).toHaveLength(6);
35
+ expect(parsed.channelOutcomes).toHaveLength(3);
36
+ expect(parsed.narrative).toBeDefined();
37
+ expect(parsed.isRuntimeV2Exclusive).toBe(true);
38
+ });
39
+
40
+ it('outputs text mode with stage summary', async () => {
41
+ await handleDemoStoryA({});
42
+
43
+ expect(stdoutSpy).toHaveBeenCalled();
44
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
45
+ expect(output).toContain('Story A\' Demo');
46
+ expect(output).toContain('OVERALL');
47
+ expect(output).toContain('evidence_seed');
48
+ expect(output).toContain('principle_proposal');
49
+ expect(output).toContain('owner_review');
50
+ expect(output).toContain('activation');
51
+ expect(output).toContain('follow_up_observation');
52
+ expect(output).toContain('rollback_proof');
53
+ expect(output).toContain('prompt');
54
+ expect(output).toContain('code_tool_hook');
55
+ expect(output).toContain('defer_archive');
56
+ });
57
+
58
+ it('JSON output contains no forbidden Quiet/Gone terms', async () => {
59
+ await handleDemoStoryA({ json: true });
60
+
61
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
62
+ const forbidden = [
63
+ 'skill', 'model_training', 'Nocturnal', 'nocturnal',
64
+ 'idle', 'night', 'Trainer', 'trainer',
65
+ 'sleep_reflection', 'sleep-cycle', 'Phase 1C', 'Phase 1D',
66
+ 'Attribution', 'PRRR', 'BALM', 'LRAS', 'GAP',
67
+ 'MissionScheduler', 'WorkspaceLearningSummary', 'Probation',
68
+ ];
69
+ for (const term of forbidden) {
70
+ expect(output, `JSON output contains forbidden term: "${term}"`).not.toContain(term);
71
+ }
72
+ });
73
+
74
+ it('sets exitCode 1 when demo fails with unknown channel', async () => {
75
+ await handleDemoStoryA({ channels: 'invalid_channel' });
76
+
77
+ expect(process.exitCode).toBe(1);
78
+ expect(stderrSpy).toHaveBeenCalled();
79
+ });
80
+
81
+ it('rejects --channels "" with exitCode 1 before workspace creation', async () => {
82
+ await handleDemoStoryA({ channels: '' });
83
+
84
+ expect(process.exitCode).toBe(1);
85
+ expect(stderrSpy).toHaveBeenCalled();
86
+ const output = stderrSpy.mock.calls.map(c => c[0]).join('');
87
+ expect(output).toContain('No channels specified');
88
+ });
89
+
90
+ it('rejects --channels "" in JSON mode with structured empty_channels error', async () => {
91
+ await handleDemoStoryA({ channels: '', json: true });
92
+
93
+ expect(process.exitCode).toBe(1);
94
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
95
+ const parsed = JSON.parse(output);
96
+ expect(parsed.status).toBe('failed');
97
+ expect(parsed.inputValidationFailure.reason).toBe('empty_channels');
98
+ });
99
+
100
+ it('temp workspace is cleaned up after run', async () => {
101
+ // Run with default (temp) workspace — verify the temp dir is gone after
102
+ await handleDemoStoryA({ json: true });
103
+
104
+ // If a temp dir was created, it should have been cleaned up.
105
+ // We verify indirectly: the command completes without error.
106
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
107
+ expect(output).toContain('passed');
108
+ });
109
+
110
+ it('explicit workspace is NOT cleaned up', async () => {
111
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-test-explicit-'));
112
+
113
+ try {
114
+ await handleDemoStoryA({ workspace: tmpDir, json: true });
115
+
116
+ // Workspace dir should still exist (not cleaned up)
117
+ expect(fs.existsSync(tmpDir)).toBe(true);
118
+ } finally {
119
+ fs.rmSync(tmpDir, { recursive: true, force: true });
120
+ }
121
+ });
122
+
123
+ it('explicit workspace produces readable state.db with artifacts and activations', async () => {
124
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-test-state-'));
125
+
126
+ try {
127
+ await handleDemoStoryA({ workspace: tmpDir, json: true });
128
+
129
+ const stateDb = path.join(tmpDir, '.pd', 'state.db');
130
+ expect(fs.existsSync(stateDb)).toBe(true);
131
+
132
+ const db = new Database(stateDb, { readonly: true });
133
+
134
+ const artifacts = db.prepare('SELECT artifact_id, artifact_kind FROM pi_artifacts').all() as { artifact_id: string; artifact_kind: string }[];
135
+ expect(artifacts.length).toBeGreaterThanOrEqual(2);
136
+ const kinds = artifacts.map(a => a.artifact_kind);
137
+ expect(kinds).toContain('principle');
138
+ expect(kinds).toContain('rule');
139
+
140
+ const activations = db.prepare('SELECT activation_id, channel FROM activations').all() as { activation_id: string; channel: string }[];
141
+ expect(activations.length).toBeGreaterThanOrEqual(3);
142
+ const channels = activations.map(a => a.channel);
143
+ expect(channels).toContain('prompt');
144
+ expect(channels).toContain('code_tool_hook');
145
+ expect(channels).toContain('defer_archive');
146
+
147
+ db.close();
148
+ } finally {
149
+ fs.rmSync(tmpDir, { recursive: true, force: true });
150
+ }
151
+ });
152
+
153
+ it('default 3-channel run returns overall=passed with code_tool_hook fully activated', async () => {
154
+ await handleDemoStoryA({ json: true });
155
+
156
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
157
+ const parsed = JSON.parse(output);
158
+
159
+ expect(parsed.status).toBe('passed');
160
+
161
+ const cthOutcome = (parsed.channelOutcomes as { channel: string; status: string; activationDecision: { decision: string; activationId: string } }[])
162
+ .find(o => o.channel === 'code_tool_hook');
163
+ expect(cthOutcome).toBeDefined();
164
+ expect(cthOutcome!.status).toBe('passed');
165
+ expect(cthOutcome!.activationDecision.decision).toBe('activated');
166
+ expect(cthOutcome!.activationDecision.activationId).toMatch(/^act_code_/);
167
+ });
168
+
169
+ it('enforcementObserved is true only after full activation with sandbox verification', async () => {
170
+ await handleDemoStoryA({ json: true });
171
+
172
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
173
+ const parsed = JSON.parse(output);
174
+
175
+ const followUpStage = (parsed.stages as { name: string; evidence: Record<string, unknown> }[])
176
+ .find(s => s.name === 'follow_up_observation');
177
+ expect(followUpStage).toBeDefined();
178
+
179
+ const observations = (followUpStage!.evidence as { observations: Record<string, unknown>[] }).observations;
180
+ const cthObs = observations.find(o => o.channel === 'code_tool_hook');
181
+ expect(cthObs).toBeDefined();
182
+ expect(cthObs!.enforcementObserved).toBe(true);
183
+ expect(cthObs!.ruleActivated).toBe(true);
184
+ expect(cthObs!.sandboxVerified).toBe(true);
185
+ expect(cthObs!.dangerousPathBlocked).toContain('verified by sandbox');
186
+ expect(cthObs!.safePathAllowed).toContain('verified by sandbox');
187
+ });
188
+
189
+ it('--json output is exactly one parseable JSON object on stdout', async () => {
190
+ await handleDemoStoryA({ json: true });
191
+
192
+ // console.log may be called multiple times; join and verify single JSON parse
193
+ const raw = stdoutSpy.mock.calls.map(c => c[0]).join('');
194
+ const parsed = JSON.parse(raw);
195
+ expect(parsed.status).toBeDefined();
196
+ expect(typeof parsed.generatedAt).toBe('string');
197
+
198
+ // Verify stderr is empty (no mixed output)
199
+ const stderrOutput = stderrSpy.mock.calls.map(c => c[0]).join('');
200
+ expect(stderrOutput).toBe('');
201
+ });
202
+
203
+ it('marks simulated stages with simulated=true', async () => {
204
+ await handleDemoStoryA({ json: true });
205
+
206
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
207
+ const parsed = JSON.parse(output);
208
+
209
+ const stages = parsed.stages as { name: string; evidence: Record<string, unknown> }[];
210
+ const evidenceSeed = stages.find(s => s.name === 'evidence_seed');
211
+ expect(evidenceSeed!.evidence.simulated).toBe(true);
212
+ expect(evidenceSeed!.evidence.simulatedNote).toBeDefined();
213
+
214
+ const ownerReview = stages.find(s => s.name === 'owner_review');
215
+ expect(ownerReview!.evidence.simulated).toBe(true);
216
+ });
217
+
218
+ it('marks real stages with simulated=false', async () => {
219
+ await handleDemoStoryA({ json: true });
220
+
221
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
222
+ const parsed = JSON.parse(output);
223
+
224
+ const stages = parsed.stages as { name: string; evidence: Record<string, unknown> }[];
225
+ const activation = stages.find(s => s.name === 'activation');
226
+ expect(activation!.evidence.simulated).toBe(false);
227
+
228
+ const followUp = stages.find(s => s.name === 'follow_up_observation');
229
+ expect(followUp!.evidence.simulated).toBe(false);
230
+ });
231
+
232
+ it('narrative contains [SIMULATED] and [REAL] stage markers', async () => {
233
+ await handleDemoStoryA({ json: true });
234
+
235
+ const output = stdoutSpy.mock.calls.map(c => c[0]).join('');
236
+ const parsed = JSON.parse(output);
237
+
238
+ expect(parsed.narrative).toContain('[SIMULATED]');
239
+ expect(parsed.narrative).toContain('[REAL]');
240
+ });
241
+ });
242
+
243
+ describe('cleanupTempWorkspace', () => {
244
+ it('logs warning on cleanup failure', () => {
245
+ const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
246
+ const mockRm = vi.fn(() => { throw new Error('permission denied'); });
247
+
248
+ cleanupTempWorkspace('/tmp/nonexistent', mockRm);
249
+
250
+ expect(stderrSpy).toHaveBeenCalledWith(
251
+ expect.stringContaining('cleanup warning'),
252
+ );
253
+ stderrSpy.mockRestore();
254
+ });
255
+ });