@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,87 @@
1
+ /**
2
+ * pd runtime internalization wake-once command handler.
3
+ *
4
+ * Usage:
5
+ * pd runtime internalization wake-once --workspace <path> --dry-run --json
6
+ *
7
+ * This command ONLY supports --dry-run mode. Non-dry-run invocations are rejected
8
+ * before any state interaction (no lease acquisition, no mutation).
9
+ *
10
+ * PRI-73 scope: dry-run inspection only; real lease acquisition is future scope.
11
+ */
12
+ import * as path from 'path';
13
+ import { RuntimeStateManager, InternalizationOrchestrator } from '@principles/core/runtime-v2';
14
+ import type { WakeOnceResult } from '@principles/core/runtime-v2';
15
+ import { resolveWorkspaceDir } from '../resolve-workspace.js';
16
+
17
+ interface WakeOnceOptions {
18
+ workspace?: string;
19
+ dryRun?: boolean;
20
+ json?: boolean;
21
+ }
22
+
23
+ const OWNER = 'pd-cli-internalization-wake-once';
24
+ const RUNTIME_KIND = 'local-worker';
25
+
26
+ function formatTextOutput(result: WakeOnceResult): string {
27
+ switch (result.decision) {
28
+ case 'would_lease':
29
+ return `would_lease: ${result.taskId} (${result.taskKind})`;
30
+ case 'no_ready_tasks':
31
+ return `no_ready_tasks: ${result.reason} (inspected: ${result.inspectedCount})`;
32
+ case 'blocked':
33
+ return `blocked: ${result.taskId} (${result.taskKind}) by [${result.blockedBy.join(', ')}]`;
34
+ case 'dependency_failed':
35
+ return `dependency_failed: ${result.taskId} (${result.taskKind}) failed deps: [${result.failedDependencies.join(', ')}]`;
36
+ case 'lease_conflict':
37
+ return `lease_conflict: ${result.taskId} — ${result.conflictReason}`;
38
+ case 'invalid_task_metadata':
39
+ return `invalid_task_metadata: ${result.taskId} (${result.taskKind})`;
40
+ case 'leased':
41
+ return `leased: ${result.taskId} (${result.taskKind}) attempt ${result.attemptCount}`;
42
+ }
43
+ }
44
+
45
+ export async function handleRuntimeInternalizationWakeOnce(opts: WakeOnceOptions): Promise<void> {
46
+ // REJECT non-dry-run — only dry-run mode is implemented in this issue
47
+ if (!opts.dryRun) {
48
+ console.error('Error: wake-once without --dry-run is not implemented.');
49
+ console.error('Only --dry-run mode is available for operator inspection.');
50
+ process.exit(1);
51
+ return;
52
+ }
53
+
54
+ const workspaceDir = opts.workspace ? path.resolve(opts.workspace) : resolveWorkspaceDir();
55
+
56
+ const stateManager = new RuntimeStateManager({ workspaceDir });
57
+ await stateManager.initialize();
58
+
59
+ try {
60
+ const orchestrator = new InternalizationOrchestrator(
61
+ { stateManager },
62
+ { owner: OWNER, runtimeKind: RUNTIME_KIND, dryRun: true },
63
+ );
64
+
65
+ let result: WakeOnceResult = { decision: 'no_ready_tasks', reason: 'no_candidates', inspectedCount: 0 };
66
+ try {
67
+ result = await orchestrator.wakeOnce();
68
+ } catch (err) {
69
+ console.error('Error: wake-once failed:', err);
70
+ process.exitCode = 1;
71
+ return;
72
+ }
73
+
74
+ if (opts.json) {
75
+ console.log(JSON.stringify(result, null, 2));
76
+ } else {
77
+ console.log(formatTextOutput(result));
78
+ }
79
+
80
+ // Exit code 1 when no task could be leased (operator alert condition)
81
+ if (result.decision === 'no_ready_tasks' || result.decision === 'lease_conflict') {
82
+ process.exitCode = 1;
83
+ }
84
+ } finally {
85
+ await stateManager.close();
86
+ }
87
+ }
@@ -0,0 +1,121 @@
1
+ import * as path from 'path';
2
+ import * as os from 'os';
3
+ import * as fs from 'fs';
4
+ import { runPainFloodSimulation } from '../services/pain-flood-simulation-runner.js';
5
+ import type { PainFloodSimulationSummary } from '@principles/core/runtime-v2';
6
+
7
+ interface CliPainFloodOptions {
8
+ workspace?: string;
9
+ json?: boolean;
10
+ identicalCount?: number;
11
+ similarCount?: number;
12
+ stressCount?: number;
13
+ }
14
+
15
+ function formatTextOutput(summary: PainFloodSimulationSummary): string {
16
+ const lines: string[] = [];
17
+ const icon = summary.status === 'healthy' ? '✓' : summary.status === 'degraded' ? '⚠' : '✗';
18
+
19
+ lines.push('PD Pain Flood Simulation');
20
+ lines.push(`generatedAt: ${summary.generatedAt}`);
21
+ lines.push(`workspaceMode: ${summary.workspaceMode}`);
22
+ lines.push(`OVERALL: ${icon} ${summary.status.toUpperCase()}`);
23
+ lines.push('');
24
+ lines.push(` inputPainCount: ${summary.inputPainCount}`);
25
+ lines.push(` acceptedPainCount: ${summary.acceptedPainCount}`);
26
+ lines.push(` skippedDuplicateCount: ${summary.skippedDuplicateCount}`);
27
+ lines.push(` failedCount: ${summary.failedCount}`);
28
+ lines.push(` candidateCount: ${summary.candidateCount}`);
29
+ lines.push(` taskCount: ${summary.taskCount}`);
30
+ lines.push(` maxEvidencePreviewLength: ${summary.maxEvidencePreviewLength}`);
31
+ lines.push(` contextBudgetSummary: ${summary.contextBudgetSummary}`);
32
+ lines.push('');
33
+
34
+ for (const stage of summary.stages) {
35
+ const stageIcon = stage.status === 'passed' ? '✓' : stage.status === 'skipped' ? '○' : '✗';
36
+ lines.push(` ${stageIcon} ${stage.scenarioName}: ${stage.status}`);
37
+ lines.push(` input=${stage.inputCount} accepted=${stage.acceptedCount} skipped=${stage.skippedCount} failed=${stage.failedCount} tasks=${stage.taskCount} candidates=${stage.candidateCount}`);
38
+ if (stage.reason) {
39
+ lines.push(` reason: ${stage.reason}`);
40
+ }
41
+ }
42
+
43
+ if (summary.recommendedNextIssue) {
44
+ lines.push('');
45
+ lines.push(`Recommended next issue: ${summary.recommendedNextIssue}`);
46
+ }
47
+
48
+ if (summary.reason) {
49
+ lines.push(`reason: ${summary.reason}`);
50
+ }
51
+
52
+ if (summary.nextAction) {
53
+ lines.push(`nextAction: ${summary.nextAction}`);
54
+ }
55
+
56
+ return lines.join('\n');
57
+ }
58
+
59
+ export async function handleRuntimePainFlood(opts: CliPainFloodOptions): Promise<void> {
60
+ if (opts.workspace) {
61
+ if (opts.json) {
62
+ console.log(JSON.stringify({ status: 'error', reason: 'Explicit workspace is not allowed for pain flood simulation — this command mutates state and must run in an auto-created temp workspace only.', nextAction: 'Remove --workspace flag and re-run to use a temp workspace.' }));
63
+ } else {
64
+ console.error('Error: --workspace is not allowed for pain flood simulation. This command mutates state and must run in an auto-created temp workspace only.');
65
+ }
66
+ process.exitCode = 1;
67
+ return;
68
+ }
69
+
70
+ const countFields: { name: 'identicalCount' | 'similarCount' | 'stressCount'; value: number | undefined; min: number }[] = [
71
+ { name: 'identicalCount', value: opts.identicalCount, min: 1 },
72
+ { name: 'similarCount', value: opts.similarCount, min: 1 },
73
+ { name: 'stressCount', value: opts.stressCount, min: 1 },
74
+ ];
75
+
76
+ for (const field of countFields) {
77
+ if (field.value !== undefined) {
78
+ if (!Number.isFinite(field.value) || !Number.isInteger(field.value) || field.value < field.min) {
79
+ const reason = `--${field.name === 'identicalCount' ? 'identical-count' : field.name === 'similarCount' ? 'similar-count' : 'stress-count'} must be a finite integer >= ${field.min}, got ${field.value}`;
80
+ if (opts.json) {
81
+ console.log(JSON.stringify({ status: 'error', reason, nextAction: `Provide a valid --${field.name === 'identicalCount' ? 'identical-count' : field.name === 'similarCount' ? 'similar-count' : 'stress-count'} value` }));
82
+ } else {
83
+ console.error(`Error: ${reason}`);
84
+ }
85
+ process.exitCode = 1;
86
+ return;
87
+ }
88
+ }
89
+ }
90
+
91
+ const workspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-pain-flood-'));
92
+ const workspaceMode = 'temp' as const;
93
+
94
+ try {
95
+ const summary = await runPainFloodSimulation({
96
+ workspaceDir,
97
+ workspaceMode,
98
+ identicalCount: opts.identicalCount,
99
+ similarCount: opts.similarCount,
100
+ stressCount: opts.stressCount,
101
+ });
102
+
103
+ if (opts.json) {
104
+ console.log(JSON.stringify(summary, null, 2));
105
+ } else {
106
+ console.log(formatTextOutput(summary));
107
+ }
108
+
109
+ if (summary.status !== 'healthy') {
110
+ console.error('');
111
+ console.error(`FAIL: status=${summary.status}`);
112
+ process.exitCode = 1;
113
+ }
114
+ } finally {
115
+ try {
116
+ fs.rmSync(workspaceDir, { recursive: true, force: true });
117
+ } catch {
118
+ // ignore cleanup errors on Windows
119
+ }
120
+ }
121
+ }
@@ -0,0 +1,438 @@
1
+ /**
2
+ * pd runtime pruning report command — non-destructive pruning metrics.
3
+ *
4
+ * Usage: pd runtime pruning report [--workspace <path>] [--json]
5
+ *
6
+ * Delegates to PruningReadModel (core).
7
+ */
8
+
9
+ import * as path from 'path';
10
+ import { resolveWorkspaceDir } from '../resolve-workspace.js';
11
+ import { PruningReadModel, appendPruningReview, listPruningReviews, buildMaskedPrincipleSet, removeOrphanReferencesFromLedger } from '@principles/core/runtime-v2';
12
+ import type { PruningReviewDecision, OrphanDerivedCandidate, OrphanDetectionResult } from '@principles/core/runtime-v2';
13
+ import { createRemediationResult, remediationAction } from './remediation-output.js';
14
+ import type { RemediationResult } from './remediation-output.js';
15
+
16
+ interface PruningReportOptions {
17
+ workspace?: string;
18
+ json?: boolean;
19
+ }
20
+
21
+ function outputText(
22
+ summary: ReturnType<PruningReadModel['getHealthSummary']>,
23
+ signals: ReturnType<PruningReadModel['getPrincipleSignals']>,
24
+ workspaceDir: string,
25
+ ): void {
26
+ console.log(`generatedAt: ${summary.generatedAt}`);
27
+ console.log(`workspace: ${workspaceDir}`);
28
+ console.log(`totalPrinciples: ${summary.totalPrinciples}`);
29
+ console.log(`byStatus: ${JSON.stringify(summary.byStatus)}`);
30
+ console.log(`orphanDerivedCandidateCount: ${summary.orphanDerivedCandidateCount}`);
31
+ console.log(`averageAgeDays: ${summary.averageAgeDays}`);
32
+ console.log('');
33
+ console.log(`watchCount: ${summary.watchCount}`);
34
+ console.log(`reviewCount: ${summary.reviewCount}`);
35
+
36
+ if (summary.watchCount > 0) {
37
+ console.log('');
38
+ console.log('── Principles flagged WATCH ──');
39
+ for (const s of signals) {
40
+ if (s.riskLevel === 'watch') {
41
+ console.log(` [${s.status}] ${s.principleId} (age: ${s.ageDays}d, derivedPainCount: ${s.derivedPainCount})`);
42
+ for (const r of s.reasons) {
43
+ console.log(` ↳ ${r}`);
44
+ }
45
+ }
46
+ }
47
+ }
48
+
49
+ if (summary.reviewCount > 0) {
50
+ console.log('');
51
+ console.log('── Principles flagged REVIEW ──');
52
+ for (const s of signals) {
53
+ if (s.riskLevel === 'review') {
54
+ console.log(` [${s.status}] ${s.principleId} (age: ${s.ageDays}d, derivedPainCount: ${s.derivedPainCount})`);
55
+ for (const r of s.reasons) {
56
+ console.log(` ↳ ${r}`);
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ if (summary.watchCount === 0 && summary.reviewCount === 0) {
63
+ console.log('');
64
+ console.log('No watch or review signals. System is healthy.');
65
+ }
66
+
67
+ console.log('');
68
+ console.log('NOTE: This report is read-only. No principles are modified or deleted.');
69
+ }
70
+
71
+ export function handlePruningReport(opts: PruningReportOptions): void {
72
+ const workspaceDir = opts.workspace
73
+ ? path.resolve(opts.workspace)
74
+ : resolveWorkspaceDir();
75
+
76
+ const model = new PruningReadModel({ workspaceDir });
77
+ const signals = model.getPrincipleSignals();
78
+ const summary = model.getHealthSummary();
79
+
80
+ if (opts.json) {
81
+ console.log(JSON.stringify({ generatedAt: summary.generatedAt, workspace: workspaceDir, summary, signals }, null, 2));
82
+ return;
83
+ }
84
+
85
+ outputText(summary, signals, workspaceDir);
86
+ }
87
+
88
+ // ── Pruning explain ────────────────────────────────────────────────────────────
89
+
90
+ export interface PruningExplainOptions {
91
+ principleId: string;
92
+ workspace?: string;
93
+ json?: boolean;
94
+ }
95
+
96
+ export interface PruningReviewOptions {
97
+ principleId: string;
98
+ decision: PruningReviewDecision;
99
+ note?: string;
100
+ reviewer?: string;
101
+ workspace?: string;
102
+ json?: boolean;
103
+ }
104
+
105
+ export function handlePruningReview(opts: PruningReviewOptions): void {
106
+ const workspaceDir = opts.workspace
107
+ ? path.resolve(opts.workspace)
108
+ : resolveWorkspaceDir();
109
+
110
+ const model = new PruningReadModel({ workspaceDir });
111
+ const signals = model.getPrincipleSignals();
112
+
113
+ const signal = signals.find((s) => s.principleId === opts.principleId);
114
+
115
+ if (!signal) {
116
+ if (opts.json) {
117
+ console.log(JSON.stringify({ error: `Principle not found: '${opts.principleId}'` }));
118
+ } else {
119
+ console.error(`Error: Principle not found: '${opts.principleId}'`);
120
+ }
121
+ process.exit(1);
122
+ return; // satisfies TypeScript control flow (process.exit is [noreturn] in tests)
123
+ }
124
+
125
+ if (opts.decision !== 'keep' && opts.decision !== 'defer' && opts.decision !== 'archive-candidate') {
126
+ if (opts.json) {
127
+ console.log(JSON.stringify({ error: `Invalid decision: '${opts.decision}'. Must be one of: keep, defer, archive-candidate` }));
128
+ } else {
129
+ console.error(`Error: Invalid decision: '${opts.decision}'. Must be one of: keep, defer, archive-candidate`);
130
+ }
131
+ process.exit(1);
132
+ return;
133
+ }
134
+
135
+ if (opts.decision === 'archive-candidate' && !opts.note) {
136
+ if (opts.json) {
137
+ console.log(JSON.stringify({ error: 'archive-candidate decision requires --note' }));
138
+ } else {
139
+ console.error('Error: archive-candidate decision requires --note');
140
+ }
141
+ process.exit(1);
142
+ return;
143
+ }
144
+
145
+ const record = appendPruningReview(workspaceDir, {
146
+ principleId: signal.principleId,
147
+ decision: opts.decision,
148
+ note: opts.note ?? '',
149
+ reviewer: opts.reviewer,
150
+ signalSnapshot: signal,
151
+ });
152
+
153
+ if (opts.json) {
154
+ console.log(JSON.stringify({
155
+ reviewId: record.reviewId,
156
+ principleId: record.principleId,
157
+ decision: record.decision,
158
+ reviewer: record.reviewer,
159
+ reviewedAt: record.reviewedAt,
160
+ }));
161
+ return;
162
+ }
163
+
164
+ console.log(`reviewId: ${record.reviewId}`);
165
+ console.log(`principleId: ${record.principleId}`);
166
+ console.log(`decision: ${record.decision}`);
167
+ console.log(`reviewer: ${record.reviewer}`);
168
+ console.log(`reviewedAt: ${record.reviewedAt}`);
169
+ console.log('');
170
+ console.log('NOTE: This audit record does not modify the principle.');
171
+ }
172
+
173
+ export function handlePruningExplain(opts: PruningExplainOptions): void {
174
+ const workspaceDir = opts.workspace
175
+ ? path.resolve(opts.workspace)
176
+ : resolveWorkspaceDir();
177
+
178
+ const model = new PruningReadModel({ workspaceDir });
179
+ const signals = model.getPrincipleSignals();
180
+ const now = new Date().toISOString();
181
+
182
+ const signal = signals.find((s) => s.principleId === opts.principleId);
183
+
184
+ if (!signal) {
185
+ if (opts.json) {
186
+ console.log(JSON.stringify({ error: `Principle not found: '${opts.principleId}'`, workspace: workspaceDir, generatedAt: now }, null, 2));
187
+ } else {
188
+ console.error(`Error: Principle not found: '${opts.principleId}'`);
189
+ }
190
+ process.exit(1);
191
+ return; // unreachable but satisfies TypeScript
192
+ }
193
+
194
+ if (opts.json) {
195
+ console.log(JSON.stringify({ workspace: workspaceDir, principleId: signal.principleId, signal, generatedAt: now }, null, 2));
196
+ return;
197
+ }
198
+
199
+ console.log(`principleId: ${signal.principleId}`);
200
+ console.log(`status: ${signal.status}`);
201
+ console.log(`riskLevel: ${signal.riskLevel}`);
202
+ console.log(`ageDays: ${signal.ageDays}`);
203
+ console.log(`derivedPainCount: ${signal.derivedPainCount}`);
204
+ console.log(`matchedCandidateCount: ${signal.matchedCandidateCount}`);
205
+ console.log(`orphanCandidateCount: ${signal.orphanCandidateCount}`);
206
+ console.log(`reasons:`);
207
+ for (const r of signal.reasons) {
208
+ console.log(` ${r}`);
209
+ }
210
+ console.log('');
211
+ console.log('NOTE: This report is read-only. No principles are modified or deleted.');
212
+ }
213
+
214
+ // ── Pruning rollback ──────────────────────────────────────────────────────────
215
+
216
+ export interface PruningRollbackOptions {
217
+ principleId: string;
218
+ note?: string;
219
+ reviewer?: string;
220
+ workspace?: string;
221
+ json?: boolean;
222
+ }
223
+
224
+ export function handlePruningRollback(opts: PruningRollbackOptions): void {
225
+ const workspaceDir = opts.workspace
226
+ ? path.resolve(opts.workspace)
227
+ : resolveWorkspaceDir();
228
+
229
+ const reviews = listPruningReviews(workspaceDir, { principleId: opts.principleId });
230
+ if (reviews.length === 0) {
231
+ if (opts.json) {
232
+ console.log(JSON.stringify({ error: `No reviews found for principle: '${opts.principleId}'` }));
233
+ } else {
234
+ console.error(`Error: No reviews found for principle: '${opts.principleId}'`);
235
+ }
236
+ process.exit(1);
237
+ return;
238
+ }
239
+
240
+ const maskedIds = buildMaskedPrincipleSet(reviews);
241
+ if (!maskedIds.has(opts.principleId)) {
242
+ if (opts.json) {
243
+ console.log(JSON.stringify({ error: `Principle '${opts.principleId}' is not currently masked (latest decision is not archive-candidate)` }));
244
+ } else {
245
+ console.error(`Error: Principle '${opts.principleId}' is not currently masked (latest decision is not archive-candidate)`);
246
+ }
247
+ process.exit(1);
248
+ return;
249
+ }
250
+
251
+ const model = new PruningReadModel({ workspaceDir });
252
+ const signals = model.getPrincipleSignals();
253
+ const signal = signals.find((s) => s.principleId === opts.principleId) ?? undefined;
254
+
255
+ const record = appendPruningReview(workspaceDir, {
256
+ principleId: opts.principleId,
257
+ decision: 'keep',
258
+ note: opts.note ?? 'Rollback: restore principle injection',
259
+ reviewer: opts.reviewer,
260
+ signalSnapshot: signal,
261
+ });
262
+
263
+ if (opts.json) {
264
+ console.log(JSON.stringify({
265
+ reviewId: record.reviewId,
266
+ principleId: record.principleId,
267
+ decision: record.decision,
268
+ reviewer: record.reviewer,
269
+ reviewedAt: record.reviewedAt,
270
+ }));
271
+ return;
272
+ }
273
+
274
+ console.log(`reviewId: ${record.reviewId}`);
275
+ console.log(`principleId: ${record.principleId}`);
276
+ console.log(`decision: ${record.decision}`);
277
+ console.log(`reviewer: ${record.reviewer}`);
278
+ console.log(`reviewedAt: ${record.reviewedAt}`);
279
+ console.log('');
280
+ console.log('Principle has been restored to injection.');
281
+ }
282
+
283
+ // ── Pruning orphans ───────────────────────────────────────────────────────────
284
+
285
+ export interface PruningOrphansOptions {
286
+ workspace?: string;
287
+ dryRun?: boolean;
288
+ confirm?: boolean;
289
+ json?: boolean;
290
+ }
291
+
292
+ function orphanActions(orphans: OrphanDerivedCandidate[]) {
293
+ return orphans.map((orphan) => remediationAction({
294
+ action: 'remove_orphan_reference',
295
+ targetId: orphan.candidateId,
296
+ previousState: orphan.status ?? 'unknown',
297
+ nextState: 'removed_from_ledger_reference',
298
+ reason: `${orphan.reason} (principle: ${orphan.principleId})`,
299
+ }));
300
+ }
301
+
302
+ export function handlePruningOrphans(opts: PruningOrphansOptions): void {
303
+ if (opts.dryRun && opts.confirm) {
304
+ console.error('Error: --dry-run and --confirm are mutually exclusive');
305
+ process.exit(1);
306
+ }
307
+
308
+ const workspaceDir = opts.workspace
309
+ ? path.resolve(opts.workspace)
310
+ : resolveWorkspaceDir();
311
+
312
+ const model = new PruningReadModel({ workspaceDir });
313
+ const detection: OrphanDetectionResult = model.getOrphanDerivedCandidates();
314
+ const orphans = detection.candidates;
315
+ const isDryRun = !opts.confirm;
316
+
317
+ if (isDryRun) {
318
+ const result: RemediationResult = createRemediationResult({
319
+ mode: 'dry_run',
320
+ repairedCount: 0,
321
+ skippedCount: 0,
322
+ actions: orphanActions(orphans),
323
+ warnings: detection.dbReadable ? [] : ['state.db is unreadable; --confirm will be refused until DB access is restored.'],
324
+ safeToConfirm: detection.dbReadable && orphans.length > 0,
325
+ includeLegacyDryRun: true,
326
+ });
327
+
328
+ if (opts.json) {
329
+ console.log(JSON.stringify({
330
+ ...result,
331
+ orphanDerivedCandidateCount: orphans.length,
332
+ candidates: orphans,
333
+ dbReadable: detection.dbReadable,
334
+ }, null, 2));
335
+ return;
336
+ }
337
+
338
+ console.log(`orphanDerivedCandidateCount: ${orphans.length}`);
339
+ console.log(`dryRun: true`);
340
+ console.log(`dbReadable: ${detection.dbReadable}`);
341
+ console.log('');
342
+ if (!detection.dbReadable) {
343
+ console.log('⚠️ state.db is unreadable — orphan list may include valid candidates.');
344
+ console.log(' --confirm will be refused until the DB is accessible.');
345
+ console.log('');
346
+ }
347
+ if (orphans.length === 0) {
348
+ console.log('No orphan derived candidates found.');
349
+ } else {
350
+ console.log('── Orphan derived candidates ──');
351
+ for (const o of orphans) {
352
+ console.log(` [${o.status ?? 'unknown'}] ${o.candidateId} → principle: ${o.principleId}`);
353
+ console.log(` ↳ ${o.reason}`);
354
+ }
355
+ console.log('');
356
+ console.log('NOTE: This is a dry-run. No data was modified. Use --confirm to remove orphan references.');
357
+ }
358
+ return;
359
+ }
360
+
361
+ if (!detection.dbReadable) {
362
+ const result: RemediationResult = createRemediationResult({
363
+ mode: 'confirm',
364
+ status: 'refused',
365
+ safeToConfirm: false,
366
+ repairedCount: 0,
367
+ skippedCount: orphans.length,
368
+ actions: orphanActions(orphans),
369
+ warnings: ['state.db is unreadable; refusing orphan cleanup because candidates cannot be verified.'],
370
+ includeLegacyDryRun: true,
371
+ });
372
+
373
+ if (opts.json) {
374
+ console.log(JSON.stringify({
375
+ ...result,
376
+ orphanDerivedCandidateCount: orphans.length,
377
+ candidates: orphans,
378
+ dbReadable: false,
379
+ }, null, 2));
380
+ } else {
381
+ console.error('❌ REFUSED: state.db is unreadable — cannot safely confirm orphan cleanup.');
382
+ console.error(' All derivedFromPainIds would appear as orphans when the DB is inaccessible.');
383
+ console.error(' Fix the DB access issue and re-run.');
384
+ }
385
+ process.exit(1);
386
+ return;
387
+ }
388
+
389
+ const orphanIdsByPrinciple = new Map<string, Set<string>>();
390
+ for (const o of orphans) {
391
+ if (!orphanIdsByPrinciple.has(o.principleId)) {
392
+ orphanIdsByPrinciple.set(o.principleId, new Set());
393
+ }
394
+ orphanIdsByPrinciple.get(o.principleId)?.add(o.candidateId);
395
+ }
396
+
397
+ const stateDir = path.join(workspaceDir, '.state');
398
+ const removedFromPrinciples = (() => {
399
+ try {
400
+ return removeOrphanReferencesFromLedger(stateDir, orphanIdsByPrinciple);
401
+ } catch (err) {
402
+ console.error(`❌ Failed to save ledger: ${err instanceof Error ? err.message : String(err)}`);
403
+ process.exit(1);
404
+ return [] as { principleId: string; removedIds: string[] }[];
405
+ }
406
+ })();
407
+
408
+ const removedCount = removedFromPrinciples.reduce((sum, item) => sum + item.removedIds.length, 0);
409
+ const result: RemediationResult = createRemediationResult({
410
+ mode: 'confirm',
411
+ repairedCount: removedCount,
412
+ skippedCount: Math.max(0, orphans.length - removedCount),
413
+ actions: orphanActions(orphans),
414
+ warnings: [],
415
+ includeLegacyDryRun: true,
416
+ });
417
+
418
+ if (opts.json) {
419
+ console.log(JSON.stringify({
420
+ ...result,
421
+ orphanDerivedCandidateCount: orphans.length,
422
+ candidates: orphans,
423
+ dbReadable: true,
424
+ removedFromPrinciples,
425
+ }, null, 2));
426
+ return;
427
+ }
428
+
429
+ console.log(`orphanDerivedCandidateCount: ${orphans.length}`);
430
+ console.log(`dryRun: false`);
431
+ console.log('');
432
+ for (const r of removedFromPrinciples) {
433
+ console.log(`principle: ${r.principleId}`);
434
+ console.log(` removed: ${r.removedIds.join(', ')}`);
435
+ }
436
+ console.log('');
437
+ console.log(`${removedFromPrinciples.length} principles updated. ${orphans.length} orphan references removed.`);
438
+ }