@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.
- package/README.md +90 -0
- package/dist/commands/artifact.d.ts +14 -0
- package/dist/commands/artifact.d.ts.map +1 -0
- package/dist/commands/artifact.js +67 -0
- package/dist/commands/artifact.js.map +1 -0
- package/dist/commands/candidate.d.ts +83 -0
- package/dist/commands/candidate.d.ts.map +1 -0
- package/dist/commands/candidate.js +891 -0
- package/dist/commands/candidate.js.map +1 -0
- package/dist/commands/central-sync.d.ts +10 -0
- package/dist/commands/central-sync.d.ts.map +1 -0
- package/dist/commands/central-sync.js +32 -0
- package/dist/commands/central-sync.js.map +1 -0
- package/dist/commands/console.d.ts +9 -0
- package/dist/commands/console.d.ts.map +1 -0
- package/dist/commands/console.js +114 -0
- package/dist/commands/console.js.map +1 -0
- package/dist/commands/context.d.ts +7 -0
- package/dist/commands/context.d.ts.map +1 -0
- package/dist/commands/context.js +55 -0
- package/dist/commands/context.js.map +1 -0
- package/dist/commands/demo-story-a.d.ts +12 -0
- package/dist/commands/demo-story-a.d.ts.map +1 -0
- package/dist/commands/demo-story-a.js +175 -0
- package/dist/commands/demo-story-a.js.map +1 -0
- package/dist/commands/diagnose.d.ts +35 -0
- package/dist/commands/diagnose.d.ts.map +1 -0
- package/dist/commands/diagnose.js +390 -0
- package/dist/commands/diagnose.js.map +1 -0
- package/dist/commands/evolution-tasks-list.d.ts +15 -0
- package/dist/commands/evolution-tasks-list.d.ts.map +1 -0
- package/dist/commands/evolution-tasks-list.js +34 -0
- package/dist/commands/evolution-tasks-list.js.map +1 -0
- package/dist/commands/evolution-tasks-show.d.ts +14 -0
- package/dist/commands/evolution-tasks-show.d.ts.map +1 -0
- package/dist/commands/evolution-tasks-show.js +52 -0
- package/dist/commands/evolution-tasks-show.js.map +1 -0
- package/dist/commands/flow.d.ts +7 -0
- package/dist/commands/flow.d.ts.map +1 -0
- package/dist/commands/flow.js +57 -0
- package/dist/commands/flow.js.map +1 -0
- package/dist/commands/health.d.ts +16 -0
- package/dist/commands/health.d.ts.map +1 -0
- package/dist/commands/health.js +150 -0
- package/dist/commands/health.js.map +1 -0
- package/dist/commands/history.d.ts +11 -0
- package/dist/commands/history.d.ts.map +1 -0
- package/dist/commands/history.js +50 -0
- package/dist/commands/history.js.map +1 -0
- package/dist/commands/legacy-cleanup.d.ts +27 -0
- package/dist/commands/legacy-cleanup.d.ts.map +1 -0
- package/dist/commands/legacy-cleanup.js +171 -0
- package/dist/commands/legacy-cleanup.js.map +1 -0
- package/dist/commands/legacy-import.d.ts +7 -0
- package/dist/commands/legacy-import.d.ts.map +1 -0
- package/dist/commands/legacy-import.js +86 -0
- package/dist/commands/legacy-import.js.map +1 -0
- package/dist/commands/pain-record.d.ts +10 -0
- package/dist/commands/pain-record.d.ts.map +1 -0
- package/dist/commands/pain-record.js +162 -0
- package/dist/commands/pain-record.js.map +1 -0
- package/dist/commands/proven-channel-baseline.d.ts +12 -0
- package/dist/commands/proven-channel-baseline.d.ts.map +1 -0
- package/dist/commands/proven-channel-baseline.js +97 -0
- package/dist/commands/proven-channel-baseline.js.map +1 -0
- package/dist/commands/remediation-output.d.ts +40 -0
- package/dist/commands/remediation-output.d.ts.map +1 -0
- package/dist/commands/remediation-output.js +23 -0
- package/dist/commands/remediation-output.js.map +1 -0
- package/dist/commands/run.d.ts +10 -0
- package/dist/commands/run.d.ts.map +1 -0
- package/dist/commands/run.js +68 -0
- package/dist/commands/run.js.map +1 -0
- package/dist/commands/runtime-activation.d.ts +11 -0
- package/dist/commands/runtime-activation.d.ts.map +1 -0
- package/dist/commands/runtime-activation.js +150 -0
- package/dist/commands/runtime-activation.js.map +1 -0
- package/dist/commands/runtime-canary.d.ts +30 -0
- package/dist/commands/runtime-canary.d.ts.map +1 -0
- package/dist/commands/runtime-canary.js +343 -0
- package/dist/commands/runtime-canary.js.map +1 -0
- package/dist/commands/runtime-diagnostics-export.d.ts +20 -0
- package/dist/commands/runtime-diagnostics-export.d.ts.map +1 -0
- package/dist/commands/runtime-diagnostics-export.js +177 -0
- package/dist/commands/runtime-diagnostics-export.js.map +1 -0
- package/dist/commands/runtime-features.d.ts +26 -0
- package/dist/commands/runtime-features.d.ts.map +1 -0
- package/dist/commands/runtime-features.js +70 -0
- package/dist/commands/runtime-features.js.map +1 -0
- package/dist/commands/runtime-gfi-snapshot.d.ts +7 -0
- package/dist/commands/runtime-gfi-snapshot.d.ts.map +1 -0
- package/dist/commands/runtime-gfi-snapshot.js +101 -0
- package/dist/commands/runtime-gfi-snapshot.js.map +1 -0
- package/dist/commands/runtime-health-snapshot.d.ts +7 -0
- package/dist/commands/runtime-health-snapshot.d.ts.map +1 -0
- package/dist/commands/runtime-health-snapshot.js +93 -0
- package/dist/commands/runtime-health-snapshot.js.map +1 -0
- package/dist/commands/runtime-idle-trigger.d.ts +12 -0
- package/dist/commands/runtime-idle-trigger.d.ts.map +1 -0
- package/dist/commands/runtime-idle-trigger.js +102 -0
- package/dist/commands/runtime-idle-trigger.js.map +1 -0
- package/dist/commands/runtime-internalization-enqueue-successors.d.ts +9 -0
- package/dist/commands/runtime-internalization-enqueue-successors.d.ts.map +1 -0
- package/dist/commands/runtime-internalization-enqueue-successors.js +393 -0
- package/dist/commands/runtime-internalization-enqueue-successors.js.map +1 -0
- package/dist/commands/runtime-internalization-integrity-repair.d.ts +9 -0
- package/dist/commands/runtime-internalization-integrity-repair.d.ts.map +1 -0
- package/dist/commands/runtime-internalization-integrity-repair.js +54 -0
- package/dist/commands/runtime-internalization-integrity-repair.js.map +1 -0
- package/dist/commands/runtime-internalization-integrity.d.ts +7 -0
- package/dist/commands/runtime-internalization-integrity.d.ts.map +1 -0
- package/dist/commands/runtime-internalization-integrity.js +53 -0
- package/dist/commands/runtime-internalization-integrity.js.map +1 -0
- package/dist/commands/runtime-internalization-queue.d.ts +7 -0
- package/dist/commands/runtime-internalization-queue.d.ts.map +1 -0
- package/dist/commands/runtime-internalization-queue.js +85 -0
- package/dist/commands/runtime-internalization-queue.js.map +1 -0
- package/dist/commands/runtime-internalization-run-once.d.ts +12 -0
- package/dist/commands/runtime-internalization-run-once.d.ts.map +1 -0
- package/dist/commands/runtime-internalization-run-once.js +546 -0
- package/dist/commands/runtime-internalization-run-once.js.map +1 -0
- package/dist/commands/runtime-internalization-wake-once.d.ts +8 -0
- package/dist/commands/runtime-internalization-wake-once.d.ts.map +1 -0
- package/dist/commands/runtime-internalization-wake-once.js +72 -0
- package/dist/commands/runtime-internalization-wake-once.js.map +1 -0
- package/dist/commands/runtime-pain-flood-simulation.d.ts +10 -0
- package/dist/commands/runtime-pain-flood-simulation.d.ts.map +1 -0
- package/dist/commands/runtime-pain-flood-simulation.js +104 -0
- package/dist/commands/runtime-pain-flood-simulation.js.map +1 -0
- package/dist/commands/runtime-pruning.d.ts +45 -0
- package/dist/commands/runtime-pruning.d.ts.map +1 -0
- package/dist/commands/runtime-pruning.js +355 -0
- package/dist/commands/runtime-pruning.js.map +1 -0
- package/dist/commands/runtime-recovery.d.ts +9 -0
- package/dist/commands/runtime-recovery.d.ts.map +1 -0
- package/dist/commands/runtime-recovery.js +94 -0
- package/dist/commands/runtime-recovery.js.map +1 -0
- package/dist/commands/runtime-synthetic-baseline.d.ts +7 -0
- package/dist/commands/runtime-synthetic-baseline.d.ts.map +1 -0
- package/dist/commands/runtime-synthetic-baseline.js +59 -0
- package/dist/commands/runtime-synthetic-baseline.js.map +1 -0
- package/dist/commands/runtime-uat.d.ts +52 -0
- package/dist/commands/runtime-uat.d.ts.map +1 -0
- package/dist/commands/runtime-uat.js +274 -0
- package/dist/commands/runtime-uat.js.map +1 -0
- package/dist/commands/runtime.d.ts +20 -0
- package/dist/commands/runtime.d.ts.map +1 -0
- package/dist/commands/runtime.js +256 -0
- package/dist/commands/runtime.js.map +1 -0
- package/dist/commands/samples-list.d.ts +11 -0
- package/dist/commands/samples-list.d.ts.map +1 -0
- package/dist/commands/samples-list.js +37 -0
- package/dist/commands/samples-list.js.map +1 -0
- package/dist/commands/samples-review.d.ts +14 -0
- package/dist/commands/samples-review.d.ts.map +1 -0
- package/dist/commands/samples-review.js +22 -0
- package/dist/commands/samples-review.js.map +1 -0
- package/dist/commands/task.d.ts +14 -0
- package/dist/commands/task.d.ts.map +1 -0
- package/dist/commands/task.js +92 -0
- package/dist/commands/task.js.map +1 -0
- package/dist/commands/trace.d.ts +19 -0
- package/dist/commands/trace.d.ts.map +1 -0
- package/dist/commands/trace.js +154 -0
- package/dist/commands/trace.js.map +1 -0
- package/dist/commands/trajectory.d.ts +11 -0
- package/dist/commands/trajectory.d.ts.map +1 -0
- package/dist/commands/trajectory.js +47 -0
- package/dist/commands/trajectory.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +736 -0
- package/dist/index.js.map +1 -0
- package/dist/legacy/legacy-import.d.ts +15 -0
- package/dist/legacy/legacy-import.d.ts.map +1 -0
- package/dist/legacy/legacy-import.js +141 -0
- package/dist/legacy/legacy-import.js.map +1 -0
- package/dist/legacy/session-history-import.d.ts +26 -0
- package/dist/legacy/session-history-import.d.ts.map +1 -0
- package/dist/legacy/session-history-import.js +151 -0
- package/dist/legacy/session-history-import.js.map +1 -0
- package/dist/principle-tree-ledger-adapter.d.ts +12 -0
- package/dist/principle-tree-ledger-adapter.d.ts.map +1 -0
- package/dist/principle-tree-ledger-adapter.js +12 -0
- package/dist/principle-tree-ledger-adapter.js.map +1 -0
- package/dist/resolve-workspace.d.ts +12 -0
- package/dist/resolve-workspace.d.ts.map +1 -0
- package/dist/resolve-workspace.js +20 -0
- package/dist/resolve-workspace.js.map +1 -0
- package/dist/services/demo-story-a-runner.d.ts +8 -0
- package/dist/services/demo-story-a-runner.d.ts.map +1 -0
- package/dist/services/demo-story-a-runner.js +369 -0
- package/dist/services/demo-story-a-runner.js.map +1 -0
- package/dist/services/feature-flag-loader.d.ts +6 -0
- package/dist/services/feature-flag-loader.d.ts.map +1 -0
- package/dist/services/feature-flag-loader.js +54 -0
- package/dist/services/feature-flag-loader.js.map +1 -0
- package/dist/services/pain-flood-simulation-runner.d.ts +10 -0
- package/dist/services/pain-flood-simulation-runner.d.ts.map +1 -0
- package/dist/services/pain-flood-simulation-runner.js +289 -0
- package/dist/services/pain-flood-simulation-runner.js.map +1 -0
- package/dist/services/proven-channel-baseline-runner.d.ts +12 -0
- package/dist/services/proven-channel-baseline-runner.d.ts.map +1 -0
- package/dist/services/proven-channel-baseline-runner.js +114 -0
- package/dist/services/proven-channel-baseline-runner.js.map +1 -0
- package/dist/services/synthetic-baseline-runner.d.ts +8 -0
- package/dist/services/synthetic-baseline-runner.d.ts.map +1 -0
- package/dist/services/synthetic-baseline-runner.js +251 -0
- package/dist/services/synthetic-baseline-runner.js.map +1 -0
- package/package.json +35 -0
- package/src/commands/artifact.ts +82 -0
- package/src/commands/candidate.ts +1117 -0
- package/src/commands/central-sync.ts +44 -0
- package/src/commands/console.ts +121 -0
- package/src/commands/context.ts +72 -0
- package/src/commands/demo-story-a.ts +195 -0
- package/src/commands/diagnose.ts +452 -0
- package/src/commands/evolution-tasks-list.ts +44 -0
- package/src/commands/evolution-tasks-show.ts +60 -0
- package/src/commands/flow.ts +60 -0
- package/src/commands/health.ts +189 -0
- package/src/commands/history.ts +63 -0
- package/src/commands/legacy-cleanup.ts +206 -0
- package/src/commands/legacy-import.ts +104 -0
- package/src/commands/pain-record.ts +167 -0
- package/src/commands/proven-channel-baseline.ts +113 -0
- package/src/commands/remediation-output.ts +66 -0
- package/src/commands/run.ts +89 -0
- package/src/commands/runtime-activation.ts +176 -0
- package/src/commands/runtime-canary.ts +371 -0
- package/src/commands/runtime-diagnostics-export.ts +229 -0
- package/src/commands/runtime-features.ts +103 -0
- package/src/commands/runtime-gfi-snapshot.ts +135 -0
- package/src/commands/runtime-health-snapshot.ts +106 -0
- package/src/commands/runtime-internalization-enqueue-successors.ts +479 -0
- package/src/commands/runtime-internalization-integrity-repair.ts +69 -0
- package/src/commands/runtime-internalization-integrity.ts +63 -0
- package/src/commands/runtime-internalization-queue.ts +106 -0
- package/src/commands/runtime-internalization-run-once.ts +658 -0
- package/src/commands/runtime-internalization-wake-once.ts +87 -0
- package/src/commands/runtime-pain-flood-simulation.ts +121 -0
- package/src/commands/runtime-pruning.ts +438 -0
- package/src/commands/runtime-recovery.ts +107 -0
- package/src/commands/runtime-synthetic-baseline.ts +70 -0
- package/src/commands/runtime-uat.ts +339 -0
- package/src/commands/runtime.ts +281 -0
- package/src/commands/samples-list.ts +43 -0
- package/src/commands/samples-review.ts +32 -0
- package/src/commands/task.ts +130 -0
- package/src/commands/trace.ts +174 -0
- package/src/commands/trajectory.ts +64 -0
- package/src/index.ts +829 -0
- package/src/legacy/legacy-import.ts +179 -0
- package/src/legacy/session-history-import.ts +231 -0
- package/src/principle-tree-ledger-adapter.ts +13 -0
- package/src/resolve-workspace.ts +20 -0
- package/src/services/demo-story-a-runner.ts +472 -0
- package/src/services/feature-flag-loader.ts +73 -0
- package/src/services/pain-flood-simulation-runner.ts +354 -0
- package/src/services/proven-channel-baseline-runner.ts +150 -0
- package/src/services/synthetic-baseline-runner.ts +291 -0
- package/tests/commands/candidate-audit-repair.test.ts +338 -0
- package/tests/commands/candidate-intake.test.ts +589 -0
- package/tests/commands/candidate-internalization-backfill.test.ts +480 -0
- package/tests/commands/candidate-internalize.test.ts +272 -0
- package/tests/commands/candidate-route.test.ts +328 -0
- package/tests/commands/candidate-show.test.ts +95 -0
- package/tests/commands/cli-command-tree.test.ts +64 -0
- package/tests/commands/context.test.ts +114 -0
- package/tests/commands/demo-story-a.test.ts +255 -0
- package/tests/commands/diagnose.test.ts +792 -0
- package/tests/commands/health.test.ts +330 -0
- package/tests/commands/pain-record.test.ts +316 -0
- package/tests/commands/plugin-config-resolution-cutover.test.ts +220 -0
- package/tests/commands/proven-channel-baseline.test.ts +441 -0
- package/tests/commands/runtime-activation.test.ts +168 -0
- package/tests/commands/runtime-canary.test.ts +369 -0
- package/tests/commands/runtime-diagnostics-export.test.ts +170 -0
- package/tests/commands/runtime-features.test.ts +114 -0
- package/tests/commands/runtime-health-snapshot.test.ts +357 -0
- package/tests/commands/runtime-internalization-enqueue-successors.test.ts +803 -0
- package/tests/commands/runtime-internalization-integrity-repair.test.ts +169 -0
- package/tests/commands/runtime-internalization-integrity.test.ts +102 -0
- package/tests/commands/runtime-internalization-queue.test.ts +252 -0
- package/tests/commands/runtime-internalization-run-once.test.ts +1318 -0
- package/tests/commands/runtime-internalization-wake-once.test.ts +170 -0
- package/tests/commands/runtime-internalization.test.ts +52 -0
- package/tests/commands/runtime-pain-flood-simulation.test.ts +418 -0
- package/tests/commands/runtime-pruning.test.ts +693 -0
- package/tests/commands/runtime-recovery.test.ts +96 -0
- package/tests/commands/runtime-synthetic-baseline.test.ts +249 -0
- package/tests/commands/runtime-uat.test.ts +397 -0
- package/tests/commands/runtime.test.ts +262 -0
- package/tests/commands/trace.test.ts +314 -0
- package/tests/e2e/candidate-intake-e2e.test.ts +316 -0
- package/tests/services/feature-flag-loader.test.ts +207 -0
- package/tests/services/proven-channel-baseline-runner.test.ts +30 -0
- package/tsconfig.json +26 -0
|
@@ -0,0 +1,891 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pd candidate commands — Principle candidate inspection, intake, audit, repair.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* pd candidate list --task-id <taskId> --workspace <path> [--json]
|
|
6
|
+
* pd candidate show <candidateId> --workspace <path> [--json]
|
|
7
|
+
* pd candidate intake --candidate-id <id> [--workspace <path>] [--json] [--dry-run]
|
|
8
|
+
* pd candidate audit --workspace <path> [--json]
|
|
9
|
+
* pd candidate repair --candidate-id <id> --workspace <path> [--json]
|
|
10
|
+
* pd candidate route --candidate-id <id> --workspace <path> [--json]
|
|
11
|
+
*/
|
|
12
|
+
import { randomUUID } from 'crypto';
|
|
13
|
+
import * as path from 'path';
|
|
14
|
+
import { RuntimeStateManager, SqliteConnection, candidateList, candidateShow, CandidateIntakeService, CandidateIntakeError, loadLedger, getLedgerFilePathPublic, decideInternalizationRoute, computeBridgeDecision, buildDreamerTaskSeed, } from '@principles/core/runtime-v2';
|
|
15
|
+
import { PrincipleTreeLedgerAdapter } from '../principle-tree-ledger-adapter.js';
|
|
16
|
+
import { resolveWorkspaceDir } from '../resolve-workspace.js';
|
|
17
|
+
import { createRemediationResult, remediationAction } from './remediation-output.js';
|
|
18
|
+
async function updateCandidateStatus(opts) {
|
|
19
|
+
const { stateManager, candidateId, targetStatus, expectedCurrentStatus } = opts;
|
|
20
|
+
const db = stateManager.connection;
|
|
21
|
+
const now = new Date().toISOString();
|
|
22
|
+
if (targetStatus === 'consumed') {
|
|
23
|
+
const result = db.getDb().prepare('UPDATE principle_candidates SET status = ?, consumed_at = ? WHERE candidate_id = ? AND status = ?').run(targetStatus, now, candidateId, expectedCurrentStatus ?? targetStatus);
|
|
24
|
+
if (result.changes === 0) {
|
|
25
|
+
throw new Error(`Guarded transition failed: candidate ${candidateId} is not in expected status '${expectedCurrentStatus ?? targetStatus}'`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const result = db.getDb().prepare('UPDATE principle_candidates SET status = ? WHERE candidate_id = ? AND status = ?').run(targetStatus, candidateId, expectedCurrentStatus ?? targetStatus);
|
|
30
|
+
if (result.changes === 0) {
|
|
31
|
+
throw new Error(`Guarded transition failed: candidate ${candidateId} is not in expected status '${expectedCurrentStatus ?? targetStatus}'`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Ensure consumed_at is set for a consumed candidate.
|
|
37
|
+
* Returns the consumed_at value (existing or newly written), or null if candidate not found.
|
|
38
|
+
*/
|
|
39
|
+
async function ensureConsumedAt(stateManager, candidateId) {
|
|
40
|
+
const db = stateManager.connection;
|
|
41
|
+
const row = db.getDb().prepare('SELECT consumed_at FROM principle_candidates WHERE candidate_id = ?').get(candidateId);
|
|
42
|
+
if (!row)
|
|
43
|
+
return null;
|
|
44
|
+
if (row.consumed_at)
|
|
45
|
+
return row.consumed_at;
|
|
46
|
+
const now = new Date().toISOString();
|
|
47
|
+
db.getDb().prepare('UPDATE principle_candidates SET consumed_at = ? WHERE candidate_id = ?').run(now, candidateId);
|
|
48
|
+
return now;
|
|
49
|
+
}
|
|
50
|
+
function resolveCandidateRecommendation(candidate, stateManager, candidateId) {
|
|
51
|
+
if (candidate.sourceRecommendationJson) {
|
|
52
|
+
try {
|
|
53
|
+
const parsed = JSON.parse(candidate.sourceRecommendationJson);
|
|
54
|
+
if (parsed?.kind) {
|
|
55
|
+
return {
|
|
56
|
+
kind: parsed.kind,
|
|
57
|
+
description: parsed.description ?? candidate.description ?? '',
|
|
58
|
+
triggerPattern: parsed.triggerPattern,
|
|
59
|
+
action: parsed.action,
|
|
60
|
+
abstractedPrinciple: parsed.abstractedPrinciple,
|
|
61
|
+
usedFallback: false,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch { /* fall through to column fallback */ }
|
|
66
|
+
}
|
|
67
|
+
const row = stateManager.connection.getDb().prepare('SELECT recommendation_kind, trigger_pattern, action, abstracted_principle FROM principle_candidates WHERE candidate_id = ?').get(candidateId);
|
|
68
|
+
if (row) {
|
|
69
|
+
return {
|
|
70
|
+
kind: row.recommendation_kind,
|
|
71
|
+
description: candidate.description || '',
|
|
72
|
+
triggerPattern: row.trigger_pattern ?? undefined,
|
|
73
|
+
action: row.action ?? undefined,
|
|
74
|
+
abstractedPrinciple: row.abstracted_principle ?? undefined,
|
|
75
|
+
usedFallback: true,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
kind: 'defer',
|
|
80
|
+
description: candidate.description || 'No recommendation data available',
|
|
81
|
+
usedFallback: false,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// ── List ───────────────────────────────────────────────────────────────────────
|
|
85
|
+
export async function handleCandidateList(opts) {
|
|
86
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
87
|
+
const stateManager = new RuntimeStateManager({ workspaceDir });
|
|
88
|
+
try {
|
|
89
|
+
await stateManager.initialize();
|
|
90
|
+
const result = await candidateList({
|
|
91
|
+
taskId: opts.taskId,
|
|
92
|
+
stateManager,
|
|
93
|
+
});
|
|
94
|
+
if (opts.json) {
|
|
95
|
+
console.log(JSON.stringify(result, null, 2));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (result.candidates.length === 0) {
|
|
99
|
+
console.log(`No candidates found for task: ${opts.taskId}`);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
console.log(`\nPrinciple Candidates for Task: ${opts.taskId}\n`);
|
|
103
|
+
console.log(` Total: ${result.candidates.length}\n`);
|
|
104
|
+
for (const candidate of result.candidates) {
|
|
105
|
+
console.log(` Candidate: ${candidate.candidateId}`);
|
|
106
|
+
console.log(` Title: ${candidate.title}`);
|
|
107
|
+
console.log(` Artifact: ${candidate.artifactId}`);
|
|
108
|
+
console.log(` Source Run: ${candidate.sourceRunId}`);
|
|
109
|
+
console.log(` Confidence: ${candidate.confidence ?? 'N/A'}`);
|
|
110
|
+
console.log(` Status: ${candidate.status}`);
|
|
111
|
+
console.log(` Description: ${candidate.description.substring(0, 100)}${candidate.description.length > 100 ? '...' : ''}`);
|
|
112
|
+
console.log('');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
await stateManager.close();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
export async function handleCandidateInternalize(opts) {
|
|
120
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
121
|
+
const stateManager = new RuntimeStateManager({ workspaceDir });
|
|
122
|
+
try {
|
|
123
|
+
await stateManager.initialize();
|
|
124
|
+
const candidate = await stateManager.getCandidate(opts.candidateId);
|
|
125
|
+
if (!candidate) {
|
|
126
|
+
const result = {
|
|
127
|
+
candidateId: opts.candidateId,
|
|
128
|
+
route: 'unknown',
|
|
129
|
+
status: 'no_task_created',
|
|
130
|
+
reason: `Candidate not found: ${opts.candidateId}`,
|
|
131
|
+
};
|
|
132
|
+
if (opts.json) {
|
|
133
|
+
console.log(JSON.stringify(result, null, 2));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
console.error(`Candidate not found: ${opts.candidateId}`);
|
|
137
|
+
}
|
|
138
|
+
process.exit(1);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const recommendation = resolveCandidateRecommendation(candidate, stateManager, opts.candidateId);
|
|
142
|
+
const decision = decideInternalizationRoute(recommendation);
|
|
143
|
+
const bridgeInput = {
|
|
144
|
+
candidateId: opts.candidateId,
|
|
145
|
+
recommendationKind: recommendation.kind,
|
|
146
|
+
route: decision.route,
|
|
147
|
+
ready: decision.ready,
|
|
148
|
+
};
|
|
149
|
+
const bridgeDecision = computeBridgeDecision(bridgeInput);
|
|
150
|
+
if (bridgeDecision.decision !== 'seeded') {
|
|
151
|
+
const reason = bridgeDecision.decision === 'already_exists'
|
|
152
|
+
? `Task ${bridgeDecision.taskId} already exists`
|
|
153
|
+
: bridgeDecision.reason;
|
|
154
|
+
const result = {
|
|
155
|
+
candidateId: opts.candidateId,
|
|
156
|
+
route: decision.route,
|
|
157
|
+
status: 'no_task_created',
|
|
158
|
+
reason,
|
|
159
|
+
};
|
|
160
|
+
if (opts.json) {
|
|
161
|
+
console.log(JSON.stringify(result, null, 2));
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
console.log(`\nCandidate Internalize: ${opts.candidateId}\n`);
|
|
165
|
+
console.log(` Route: ${decision.route}`);
|
|
166
|
+
console.log(` Ready: ${decision.ready}`);
|
|
167
|
+
console.log(` Reason: ${decision.reason}`);
|
|
168
|
+
console.log('');
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const { channel } = bridgeDecision;
|
|
173
|
+
const { taskId } = bridgeDecision;
|
|
174
|
+
if (opts.dryRun) {
|
|
175
|
+
const result = {
|
|
176
|
+
candidateId: opts.candidateId,
|
|
177
|
+
route: decision.route,
|
|
178
|
+
channel,
|
|
179
|
+
status: 'dry_run',
|
|
180
|
+
reason: 'Dry-run mode — no task created',
|
|
181
|
+
};
|
|
182
|
+
if (opts.json) {
|
|
183
|
+
console.log(JSON.stringify(result, null, 2));
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
console.log(`\nCandidate Internalize (dry-run): ${opts.candidateId}\n`);
|
|
187
|
+
console.log(` Route: ${decision.route}`);
|
|
188
|
+
console.log(` Channel: ${channel}`);
|
|
189
|
+
console.log(` Would create: dreamer PI task`);
|
|
190
|
+
console.log('');
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const existingTask = await stateManager.getTask(taskId);
|
|
195
|
+
if (existingTask) {
|
|
196
|
+
const result = {
|
|
197
|
+
candidateId: opts.candidateId,
|
|
198
|
+
route: decision.route,
|
|
199
|
+
taskId: existingTask.taskId,
|
|
200
|
+
channel,
|
|
201
|
+
status: 'existing',
|
|
202
|
+
reason: 'Task already exists for this candidate+channel combination',
|
|
203
|
+
};
|
|
204
|
+
if (opts.json) {
|
|
205
|
+
console.log(JSON.stringify(result, null, 2));
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
console.log(`\nCandidate Internalize: ${opts.candidateId}\n`);
|
|
209
|
+
console.log(` Route: ${decision.route}`);
|
|
210
|
+
console.log(` Channel: ${channel}`);
|
|
211
|
+
console.log(` Task: ${existingTask.taskId} (existing)`);
|
|
212
|
+
console.log('');
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const seed = buildDreamerTaskSeed(bridgeInput);
|
|
217
|
+
if ('decision' in seed) {
|
|
218
|
+
const result = {
|
|
219
|
+
candidateId: opts.candidateId,
|
|
220
|
+
route: decision.route,
|
|
221
|
+
status: 'no_task_created',
|
|
222
|
+
reason: seed.reason,
|
|
223
|
+
};
|
|
224
|
+
if (opts.json) {
|
|
225
|
+
console.log(JSON.stringify(result, null, 2));
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
console.error(`Bridge seed failed: ${seed.reason}`);
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const task = await stateManager.createTask({
|
|
233
|
+
taskId: seed.taskId,
|
|
234
|
+
taskKind: seed.taskKind,
|
|
235
|
+
status: seed.status,
|
|
236
|
+
attemptCount: seed.attemptCount,
|
|
237
|
+
maxAttempts: seed.maxAttempts,
|
|
238
|
+
diagnosticJson: seed.diagnosticJson,
|
|
239
|
+
});
|
|
240
|
+
const result = {
|
|
241
|
+
candidateId: opts.candidateId,
|
|
242
|
+
route: decision.route,
|
|
243
|
+
taskId: task.taskId,
|
|
244
|
+
channel,
|
|
245
|
+
status: 'created',
|
|
246
|
+
};
|
|
247
|
+
if (opts.json) {
|
|
248
|
+
console.log(JSON.stringify(result, null, 2));
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
console.log(`\nCandidate Internalize: ${opts.candidateId}\n`);
|
|
252
|
+
console.log(` Route: ${decision.route}`);
|
|
253
|
+
console.log(` Channel: ${channel}`);
|
|
254
|
+
console.log(` Task: ${task.taskId} (created)`);
|
|
255
|
+
console.log('');
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
await stateManager.close();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// ── Show ───────────────────────────────────────────────────────────────────────
|
|
263
|
+
export async function handleCandidateShow(opts) {
|
|
264
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
265
|
+
const stateManager = new RuntimeStateManager({ workspaceDir });
|
|
266
|
+
try {
|
|
267
|
+
await stateManager.initialize();
|
|
268
|
+
const ledgerAdapter = new PrincipleTreeLedgerAdapter({ stateDir: path.join(workspaceDir, '.state') });
|
|
269
|
+
const result = await candidateShow({
|
|
270
|
+
candidateId: opts.candidateId,
|
|
271
|
+
stateManager,
|
|
272
|
+
ledgerAdapter,
|
|
273
|
+
});
|
|
274
|
+
if (!result) {
|
|
275
|
+
console.error(`Candidate not found: ${opts.candidateId}`);
|
|
276
|
+
process.exit(1);
|
|
277
|
+
}
|
|
278
|
+
if (opts.json) {
|
|
279
|
+
console.log(JSON.stringify(result, null, 2));
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
console.log(`\nPrinciple Candidate: ${result.candidateId}\n`);
|
|
283
|
+
console.log(` Title: ${result.title}`);
|
|
284
|
+
console.log(` Description: ${result.description}`);
|
|
285
|
+
console.log(` Artifact: ${result.artifactId}`);
|
|
286
|
+
console.log(` Task: ${result.taskId}`);
|
|
287
|
+
console.log(` Source Run: ${result.sourceRunId}`);
|
|
288
|
+
console.log(` Confidence: ${result.confidence ?? 'N/A'}`);
|
|
289
|
+
console.log(` Status: ${result.status}`);
|
|
290
|
+
console.log(` Created: ${result.createdAt}`);
|
|
291
|
+
if (result.ledgerEntryId) {
|
|
292
|
+
console.log(` Ledger Entry: ${result.ledgerEntryId}`);
|
|
293
|
+
}
|
|
294
|
+
console.log('');
|
|
295
|
+
}
|
|
296
|
+
finally {
|
|
297
|
+
await stateManager.close();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// ── Intake ───────────────────────────────────────────────────────────────────
|
|
301
|
+
/**
|
|
302
|
+
* pd candidate intake --candidate-id <id> [--workspace <path>] [--json] [--dry-run]
|
|
303
|
+
*
|
|
304
|
+
* Intakes a principle candidate into the ledger.
|
|
305
|
+
* Wires together CandidateIntakeService + PrincipleTreeLedgerAdapter.
|
|
306
|
+
* Updates candidate status to 'consumed' (with consumed_at) after successful ledger write.
|
|
307
|
+
* If ledger write succeeds but DB update fails, exits non-zero with clear error.
|
|
308
|
+
*/
|
|
309
|
+
export async function handleCandidateIntake(opts) {
|
|
310
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
311
|
+
const stateManager = new RuntimeStateManager({ workspaceDir });
|
|
312
|
+
try {
|
|
313
|
+
await stateManager.initialize();
|
|
314
|
+
const ledgerAdapter = new PrincipleTreeLedgerAdapter({ stateDir: path.join(workspaceDir, '.state') });
|
|
315
|
+
const service = new CandidateIntakeService({ stateManager, ledgerAdapter });
|
|
316
|
+
if (opts.dryRun) {
|
|
317
|
+
const candidate = await stateManager.getCandidate(opts.candidateId);
|
|
318
|
+
if (!candidate) {
|
|
319
|
+
console.error(`Candidate not found: ${opts.candidateId}`);
|
|
320
|
+
process.exit(1);
|
|
321
|
+
}
|
|
322
|
+
const artifact = await stateManager.getArtifact(candidate.artifactId);
|
|
323
|
+
if (!artifact) {
|
|
324
|
+
console.error(`Artifact not found for candidate: ${opts.candidateId}`);
|
|
325
|
+
process.exit(1);
|
|
326
|
+
}
|
|
327
|
+
let recommendation = {};
|
|
328
|
+
try {
|
|
329
|
+
const parsed = JSON.parse(artifact.contentJson || '{}');
|
|
330
|
+
recommendation = parsed.recommendation || parsed;
|
|
331
|
+
}
|
|
332
|
+
catch (err) {
|
|
333
|
+
console.warn(`Warning: could not parse artifact content as JSON — using defaults. ${err instanceof Error ? err.message : String(err)}`);
|
|
334
|
+
}
|
|
335
|
+
const entry = {
|
|
336
|
+
id: randomUUID(),
|
|
337
|
+
title: recommendation.title || candidate.title,
|
|
338
|
+
text: recommendation.text || candidate.description || '',
|
|
339
|
+
triggerPattern: recommendation.triggerPattern || '',
|
|
340
|
+
action: recommendation.action || '',
|
|
341
|
+
status: 'probation',
|
|
342
|
+
evaluability: 'weak_heuristic',
|
|
343
|
+
sourceRef: `candidate://${opts.candidateId}`,
|
|
344
|
+
artifactRef: `artifact://${candidate.artifactId}`,
|
|
345
|
+
taskRef: candidate.taskId ? `task://${candidate.taskId}` : undefined,
|
|
346
|
+
createdAt: new Date().toISOString(),
|
|
347
|
+
};
|
|
348
|
+
if (opts.json) {
|
|
349
|
+
console.log(JSON.stringify(entry, null, 2));
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
console.log(`Dry-run: would write entry for candidate ${opts.candidateId}`);
|
|
353
|
+
console.log(JSON.stringify(entry, null, 2));
|
|
354
|
+
}
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
// Normal intake: ledger write first
|
|
358
|
+
const entry = await service.intake(opts.candidateId);
|
|
359
|
+
// Check if already consumed before this call
|
|
360
|
+
const candidate = await stateManager.getCandidate(opts.candidateId);
|
|
361
|
+
if (candidate?.status === 'consumed') {
|
|
362
|
+
const infoMessage = `Candidate ${opts.candidateId} was already consumed. Ledger entry: ${entry.id}`;
|
|
363
|
+
if (opts.json) {
|
|
364
|
+
console.log(JSON.stringify({
|
|
365
|
+
candidateId: opts.candidateId,
|
|
366
|
+
ledgerEntryId: entry.id,
|
|
367
|
+
status: 'already_consumed',
|
|
368
|
+
message: infoMessage,
|
|
369
|
+
}, null, 2));
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
console.log(infoMessage);
|
|
373
|
+
}
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
// Update DB status — this must succeed; if it fails, exit non-zero
|
|
377
|
+
try {
|
|
378
|
+
await updateCandidateStatus({ stateManager, candidateId: opts.candidateId, targetStatus: 'consumed', expectedCurrentStatus: 'pending' });
|
|
379
|
+
}
|
|
380
|
+
catch (err) {
|
|
381
|
+
const msg = `Ledger write succeeded (entry ${entry.id}) but DB status update failed: ${err instanceof Error ? err.message : String(err)}. ` +
|
|
382
|
+
`Candidate ${opts.candidateId} may be in inconsistent state.`;
|
|
383
|
+
console.error(`ERROR: ${msg}`);
|
|
384
|
+
process.exit(1);
|
|
385
|
+
}
|
|
386
|
+
const result = {
|
|
387
|
+
candidateId: opts.candidateId,
|
|
388
|
+
ledgerEntryId: entry.id,
|
|
389
|
+
status: 'consumed',
|
|
390
|
+
};
|
|
391
|
+
if (opts.json) {
|
|
392
|
+
console.log(JSON.stringify(result, null, 2));
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
console.log(`\nPrinciple Candidate Intake: ${opts.candidateId}\n`);
|
|
396
|
+
console.log(` Candidate: ${opts.candidateId}`);
|
|
397
|
+
console.log(` Title: ${entry.title}`);
|
|
398
|
+
console.log(` Ledger Entry: ${entry.id}`);
|
|
399
|
+
console.log(` Status: consumed\n`);
|
|
400
|
+
console.log('Intake complete.\n');
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
catch (err) {
|
|
404
|
+
if (err instanceof CandidateIntakeError || err.name === 'CandidateIntakeError') {
|
|
405
|
+
const e = err;
|
|
406
|
+
console.error(`Intake failed [${e.code ?? 'unknown'}]: ${e.message}`);
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
console.error(`Intake failed: ${String(err)}`);
|
|
410
|
+
}
|
|
411
|
+
process.exit(1);
|
|
412
|
+
}
|
|
413
|
+
finally {
|
|
414
|
+
await stateManager.close();
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
// ── Audit ─────────────────────────────────────────────────────────────────────
|
|
418
|
+
/**
|
|
419
|
+
* pd candidate audit --workspace <path> [--json]
|
|
420
|
+
*
|
|
421
|
+
* Reads workspace/.pd/state.db principle_candidates and
|
|
422
|
+
* the workspace ledger (same file used by OpenClaw plugin).
|
|
423
|
+
* Checks each consumed candidate has a ledger entry.
|
|
424
|
+
* Exits non-zero if any consumed candidate is missing from ledger.
|
|
425
|
+
*/
|
|
426
|
+
export async function handleCandidateAudit(opts) {
|
|
427
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
428
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
429
|
+
let conn;
|
|
430
|
+
try {
|
|
431
|
+
const dbPath = path.join(workspaceDir, '.pd', 'state.db');
|
|
432
|
+
const ledgerStateDir = path.join(workspaceDir, '.state');
|
|
433
|
+
const ledgerPath = getLedgerFilePathPublic(ledgerStateDir);
|
|
434
|
+
conn = new SqliteConnection({ workspaceDir, readonly: true });
|
|
435
|
+
const db = conn.getDb();
|
|
436
|
+
const consumedRows = db.prepare("SELECT candidate_id FROM principle_candidates WHERE status = 'consumed'").all();
|
|
437
|
+
const consumedIds = consumedRows.map(r => r.candidate_id);
|
|
438
|
+
const ledger = loadLedger(ledgerStateDir);
|
|
439
|
+
const ledgerPrinciples = ledger.tree.principles;
|
|
440
|
+
const missingLedgerEntryIds = [];
|
|
441
|
+
for (const candidateId of consumedIds) {
|
|
442
|
+
const found = Object.values(ledgerPrinciples).some((p) => p.derivedFromPainIds.includes(candidateId));
|
|
443
|
+
if (!found) {
|
|
444
|
+
missingLedgerEntryIds.push(candidateId);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const result = {
|
|
448
|
+
status: missingLedgerEntryIds.length === 0 ? 'ok' : 'degraded',
|
|
449
|
+
consumedCount: consumedIds.length,
|
|
450
|
+
missingLedgerEntryIds,
|
|
451
|
+
checkedLedgerPath: ledgerPath,
|
|
452
|
+
checkedDbPath: dbPath,
|
|
453
|
+
};
|
|
454
|
+
if (opts.json) {
|
|
455
|
+
console.log(JSON.stringify(result, null, 2));
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
console.log(`\nCandidate Audit Results\n`);
|
|
459
|
+
console.log(` consumedCount: ${result.consumedCount}`);
|
|
460
|
+
console.log(` checkedLedgerPath: ${result.checkedLedgerPath}`);
|
|
461
|
+
console.log(` checkedDbPath: ${result.checkedDbPath}`);
|
|
462
|
+
console.log(` status: ${result.status}`);
|
|
463
|
+
if (result.missingLedgerEntryIds.length > 0) {
|
|
464
|
+
console.log(`\n MISSING LEDGER ENTRIES (${result.missingLedgerEntryIds.length}):`);
|
|
465
|
+
result.missingLedgerEntryIds.forEach(id => console.log(` - ${id}`));
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
console.log(`\n All consumed candidates have ledger entries.`);
|
|
469
|
+
}
|
|
470
|
+
console.log('');
|
|
471
|
+
}
|
|
472
|
+
if (result.status === 'degraded') {
|
|
473
|
+
process.exit(1);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
catch (err) {
|
|
477
|
+
console.error(`Audit failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
478
|
+
process.exit(1);
|
|
479
|
+
}
|
|
480
|
+
finally {
|
|
481
|
+
try {
|
|
482
|
+
conn?.close();
|
|
483
|
+
}
|
|
484
|
+
catch { /* best-effort close */ }
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
// ── Repair ─────────────────────────────────────────────────────────────────────
|
|
488
|
+
/**
|
|
489
|
+
* pd candidate repair --candidate-id <id> --workspace <path> [--json]
|
|
490
|
+
*
|
|
491
|
+
* Handles consumed but missing ledger entries.
|
|
492
|
+
* Re-calls CandidateIntakeService.intake() to write ledger entry.
|
|
493
|
+
* Does not regenerate candidate; does not update status (already consumed).
|
|
494
|
+
* Fills consumed_at if empty.
|
|
495
|
+
*/
|
|
496
|
+
export async function handleCandidateRepair(opts) {
|
|
497
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
498
|
+
const stateManager = new RuntimeStateManager({ workspaceDir });
|
|
499
|
+
try {
|
|
500
|
+
await stateManager.initialize();
|
|
501
|
+
// Verify candidate exists and is consumed
|
|
502
|
+
const candidate = await stateManager.getCandidate(opts.candidateId);
|
|
503
|
+
if (!candidate) {
|
|
504
|
+
console.error(`Candidate not found: ${opts.candidateId}`);
|
|
505
|
+
process.exit(1);
|
|
506
|
+
}
|
|
507
|
+
if (candidate.status !== 'consumed') {
|
|
508
|
+
console.error(`Candidate ${opts.candidateId} is not consumed (status=${candidate.status}). Repair only handles consumed candidates.`);
|
|
509
|
+
process.exit(1);
|
|
510
|
+
}
|
|
511
|
+
const ledgerAdapter = new PrincipleTreeLedgerAdapter({ stateDir: path.join(workspaceDir, '.state') });
|
|
512
|
+
const service = new CandidateIntakeService({ stateManager, ledgerAdapter });
|
|
513
|
+
// Check if already in ledger
|
|
514
|
+
const existing = ledgerAdapter.existsForCandidate(opts.candidateId);
|
|
515
|
+
if (existing) {
|
|
516
|
+
const consumedAt = await ensureConsumedAt(stateManager, opts.candidateId);
|
|
517
|
+
const result = {
|
|
518
|
+
candidateId: opts.candidateId,
|
|
519
|
+
status: 'already_consistent',
|
|
520
|
+
message: `Candidate ${opts.candidateId} already has ledger entry.`,
|
|
521
|
+
ledgerEntryId: existing.id,
|
|
522
|
+
consumedAt,
|
|
523
|
+
};
|
|
524
|
+
if (opts.json) {
|
|
525
|
+
console.log(JSON.stringify(result, null, 2));
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
console.log(`\nCandidate ${opts.candidateId} already has ledger entry: ${existing.id}\n`);
|
|
529
|
+
console.log('No repair needed.\n');
|
|
530
|
+
}
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
// Re-intake to restore ledger entry
|
|
534
|
+
const entry = await service.intake(opts.candidateId);
|
|
535
|
+
const consumedAt = await ensureConsumedAt(stateManager, opts.candidateId);
|
|
536
|
+
const result = {
|
|
537
|
+
candidateId: opts.candidateId,
|
|
538
|
+
status: 'repaired',
|
|
539
|
+
ledgerEntryId: entry.id,
|
|
540
|
+
consumedAt,
|
|
541
|
+
message: `Ledger entry restored for consumed candidate ${opts.candidateId}.`,
|
|
542
|
+
};
|
|
543
|
+
if (opts.json) {
|
|
544
|
+
console.log(JSON.stringify(result, null, 2));
|
|
545
|
+
}
|
|
546
|
+
else {
|
|
547
|
+
console.log(`\nCandidate Repair: ${opts.candidateId}\n`);
|
|
548
|
+
console.log(` Status: repaired`);
|
|
549
|
+
console.log(` Ledger Entry: ${entry.id}\n`);
|
|
550
|
+
console.log('Repair complete.\n');
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
catch (err) {
|
|
554
|
+
if (err instanceof CandidateIntakeError || err.name === 'CandidateIntakeError') {
|
|
555
|
+
const e = err;
|
|
556
|
+
console.error(`Repair failed [${e.code ?? 'unknown'}]: ${e.message}`);
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
console.error(`Repair failed: ${String(err)}`);
|
|
560
|
+
}
|
|
561
|
+
process.exit(1);
|
|
562
|
+
}
|
|
563
|
+
finally {
|
|
564
|
+
await stateManager.close();
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function createBackfillRemediationResult(output) {
|
|
568
|
+
const actions = output.results
|
|
569
|
+
.filter((result) => result.status === 'would_create' || result.status === 'created' || result.status === 'would_intake_and_create' || result.status === 'intake_failed' || result.status === 'intake_succeeded_existing_task')
|
|
570
|
+
.map((result) => {
|
|
571
|
+
if (result.status === 'would_intake_and_create') {
|
|
572
|
+
return remediationAction({
|
|
573
|
+
action: 'would_intake_and_create_dreamer_task',
|
|
574
|
+
targetId: result.candidateId,
|
|
575
|
+
previousState: 'pending_candidate_without_dreamer',
|
|
576
|
+
nextState: 'dreamer_task_would_be_created',
|
|
577
|
+
reason: result.reason ?? `Backfill would intake pending candidate and create dreamer task${result.taskId ? ` ${result.taskId}` : ''}`,
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
if (result.status === 'intake_failed') {
|
|
581
|
+
return remediationAction({
|
|
582
|
+
action: 'intake_failed',
|
|
583
|
+
targetId: result.candidateId,
|
|
584
|
+
previousState: 'pending_candidate',
|
|
585
|
+
nextState: 'pending_candidate_intake_failed',
|
|
586
|
+
reason: result.reason ?? `Backfill intake failed for pending candidate`,
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
if (result.status === 'intake_succeeded_existing_task') {
|
|
590
|
+
return remediationAction({
|
|
591
|
+
action: 'intake_succeeded_existing_task',
|
|
592
|
+
targetId: result.candidateId,
|
|
593
|
+
previousState: 'pending_candidate',
|
|
594
|
+
nextState: 'consumed_candidate_with_existing_dreamer',
|
|
595
|
+
reason: result.reason ?? `Backfill intake succeeded but dreamer task already exists`,
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
return remediationAction({
|
|
599
|
+
action: result.status === 'created' ? 'create_dreamer_task' : 'would_create_dreamer_task',
|
|
600
|
+
targetId: result.candidateId,
|
|
601
|
+
previousState: 'consumed_candidate_without_dreamer',
|
|
602
|
+
nextState: result.status === 'created' ? 'dreamer_task_created' : 'dreamer_task_would_be_created',
|
|
603
|
+
reason: result.reason ?? `Backfill ${result.status === 'created' ? 'created' : 'would create'} dreamer task${result.taskId ? ` ${result.taskId}` : ''}`,
|
|
604
|
+
});
|
|
605
|
+
});
|
|
606
|
+
return createRemediationResult({
|
|
607
|
+
mode: output.mode === 'confirm' ? 'confirm' : 'dry_run',
|
|
608
|
+
repairedCount: output.created,
|
|
609
|
+
skippedCount: output.alreadyHaveTask + output.deferred + output.errors + output.intakeFailed,
|
|
610
|
+
actions,
|
|
611
|
+
warnings: output.errors > 0 ? [`${output.errors} candidate(s) could not be backfilled.`] : (output.intakeFailed > 0 ? [`${output.intakeFailed} pending candidate(s) intake failed.`] : []),
|
|
612
|
+
status: (output.errors > 0 || output.intakeFailed > 0) && output.created === 0 ? 'error' : undefined,
|
|
613
|
+
safeToConfirm: output.mode === 'dry-run' && (output.missingDreamerTask > 0 || output.totalPending > 0) && output.errors === 0,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
export async function handleCandidateInternalizationBackfill(opts) {
|
|
617
|
+
if (opts.dryRun && opts.confirm) {
|
|
618
|
+
console.error('Error: --dry-run and --confirm are mutually exclusive');
|
|
619
|
+
process.exit(1);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
623
|
+
const isConfirm = opts.confirm ?? false;
|
|
624
|
+
const stateManager = new RuntimeStateManager({ workspaceDir, readonly: !isConfirm });
|
|
625
|
+
try {
|
|
626
|
+
await stateManager.initialize();
|
|
627
|
+
const db = stateManager.connection.getDb();
|
|
628
|
+
const consumedRows = db.prepare("SELECT candidate_id FROM principle_candidates WHERE status = 'consumed'").all();
|
|
629
|
+
let pendingRows = [];
|
|
630
|
+
if (opts.includePending) {
|
|
631
|
+
pendingRows = db.prepare("SELECT candidate_id FROM principle_candidates WHERE status = 'pending'").all();
|
|
632
|
+
}
|
|
633
|
+
const output = {
|
|
634
|
+
mode: isConfirm ? 'confirm' : 'dry-run',
|
|
635
|
+
totalConsumed: consumedRows.length,
|
|
636
|
+
totalPending: pendingRows.length,
|
|
637
|
+
missingDreamerTask: 0,
|
|
638
|
+
alreadyHaveTask: 0,
|
|
639
|
+
deferred: 0,
|
|
640
|
+
created: 0,
|
|
641
|
+
intakeSucceeded: 0,
|
|
642
|
+
intakeFailed: 0,
|
|
643
|
+
errors: 0,
|
|
644
|
+
results: [],
|
|
645
|
+
};
|
|
646
|
+
for (const row of consumedRows) {
|
|
647
|
+
const candidateId = row.candidate_id;
|
|
648
|
+
const candidate = await stateManager.getCandidate(candidateId);
|
|
649
|
+
if (!candidate) {
|
|
650
|
+
output.errors++;
|
|
651
|
+
output.results.push({ candidateId, route: 'unknown', status: 'error', reason: 'Candidate not found in DB', statusBefore: 'consumed', statusAfter: 'consumed', intakeDecision: 'not_needed', seedDecision: 'skipped', nextAction: 'Investigate why consumed candidate is missing from DB' });
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
const recommendation = resolveCandidateRecommendation(candidate, stateManager, candidateId);
|
|
655
|
+
const decision = decideInternalizationRoute(recommendation);
|
|
656
|
+
const bridgeInput = {
|
|
657
|
+
candidateId,
|
|
658
|
+
recommendationKind: recommendation.kind,
|
|
659
|
+
route: decision.route,
|
|
660
|
+
ready: decision.ready,
|
|
661
|
+
};
|
|
662
|
+
const bridgeDecision = computeBridgeDecision(bridgeInput);
|
|
663
|
+
if (bridgeDecision.decision !== 'seeded') {
|
|
664
|
+
output.deferred++;
|
|
665
|
+
const reason = bridgeDecision.decision === 'already_exists'
|
|
666
|
+
? `Task ${bridgeDecision.taskId} already exists`
|
|
667
|
+
: bridgeDecision.reason;
|
|
668
|
+
output.results.push({ candidateId, route: decision.route, status: 'deferred', reason, statusBefore: 'consumed', statusAfter: 'consumed', intakeDecision: 'not_needed', seedDecision: 'skipped' });
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
const { channel } = bridgeDecision;
|
|
672
|
+
const { taskId } = bridgeDecision;
|
|
673
|
+
const existingTask = await stateManager.getTask(taskId);
|
|
674
|
+
if (existingTask) {
|
|
675
|
+
if (isConfirm && existingTask.diagnosticJson) {
|
|
676
|
+
try {
|
|
677
|
+
const diagObj = JSON.parse(existingTask.diagnosticJson);
|
|
678
|
+
if (!diagObj.candidateId) {
|
|
679
|
+
diagObj.candidateId = candidateId;
|
|
680
|
+
await stateManager.updateTaskDiagnosticJson(taskId, JSON.stringify(diagObj));
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
catch { /* best-effort */ }
|
|
684
|
+
}
|
|
685
|
+
output.alreadyHaveTask++;
|
|
686
|
+
output.results.push({ candidateId, route: decision.route, status: 'existing', taskId: existingTask.taskId, channel, statusBefore: 'consumed', statusAfter: 'consumed', intakeDecision: 'not_needed', seedDecision: 'existing' });
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
output.missingDreamerTask++;
|
|
690
|
+
if (!isConfirm) {
|
|
691
|
+
output.results.push({ candidateId, route: decision.route, status: 'would_create', taskId, channel, statusBefore: 'consumed', statusAfter: 'consumed', intakeDecision: 'not_needed', seedDecision: 'would_seed' });
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
try {
|
|
695
|
+
const seed = buildDreamerTaskSeed(bridgeInput);
|
|
696
|
+
if ('decision' in seed) {
|
|
697
|
+
output.errors++;
|
|
698
|
+
output.results.push({ candidateId, route: decision.route, status: 'error', reason: seed.reason, statusBefore: 'consumed', statusAfter: 'consumed', intakeDecision: 'not_needed', seedDecision: 'skipped', nextAction: 'Investigate bridge seed failure' });
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
const task = await stateManager.createTask({
|
|
702
|
+
taskId: seed.taskId,
|
|
703
|
+
taskKind: seed.taskKind,
|
|
704
|
+
status: seed.status,
|
|
705
|
+
attemptCount: seed.attemptCount,
|
|
706
|
+
maxAttempts: seed.maxAttempts,
|
|
707
|
+
diagnosticJson: seed.diagnosticJson,
|
|
708
|
+
});
|
|
709
|
+
output.created++;
|
|
710
|
+
output.results.push({ candidateId, route: decision.route, status: 'created', taskId: task.taskId, channel, statusBefore: 'consumed', statusAfter: 'consumed', intakeDecision: 'not_needed', seedDecision: 'seeded' });
|
|
711
|
+
}
|
|
712
|
+
catch (err) {
|
|
713
|
+
output.errors++;
|
|
714
|
+
output.results.push({ candidateId, route: decision.route, status: 'error', reason: err instanceof Error ? err.message : String(err), statusBefore: 'consumed', statusAfter: 'consumed', intakeDecision: 'not_needed', seedDecision: 'skipped', nextAction: 'Investigate dreamer task creation failure' });
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
const ledgerAdapter = new PrincipleTreeLedgerAdapter({ stateDir: path.join(workspaceDir, '.state') });
|
|
718
|
+
const intakeService = new CandidateIntakeService({ stateManager, ledgerAdapter });
|
|
719
|
+
for (const row of pendingRows) {
|
|
720
|
+
const candidateId = row.candidate_id;
|
|
721
|
+
const candidate = await stateManager.getCandidate(candidateId);
|
|
722
|
+
if (!candidate) {
|
|
723
|
+
output.errors++;
|
|
724
|
+
output.results.push({ candidateId, route: 'unknown', status: 'error', reason: 'Pending candidate not found in DB', statusBefore: 'pending', statusAfter: 'pending', intakeDecision: 'skipped', seedDecision: 'skipped', nextAction: 'Investigate why pending candidate is missing from DB' });
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
const recommendation = resolveCandidateRecommendation(candidate, stateManager, candidateId);
|
|
728
|
+
const decision = decideInternalizationRoute(recommendation);
|
|
729
|
+
const bridgeInput = {
|
|
730
|
+
candidateId,
|
|
731
|
+
recommendationKind: recommendation.kind,
|
|
732
|
+
route: decision.route,
|
|
733
|
+
ready: decision.ready,
|
|
734
|
+
};
|
|
735
|
+
const bridgeDecision = computeBridgeDecision(bridgeInput);
|
|
736
|
+
if (bridgeDecision.decision !== 'seeded') {
|
|
737
|
+
output.deferred++;
|
|
738
|
+
const reason = bridgeDecision.decision === 'already_exists'
|
|
739
|
+
? `Task ${bridgeDecision.taskId} already exists`
|
|
740
|
+
: bridgeDecision.reason;
|
|
741
|
+
output.results.push({ candidateId, route: decision.route, status: 'deferred', reason, statusBefore: 'pending', statusAfter: 'pending', intakeDecision: 'skipped', seedDecision: 'skipped', nextAction: 'Investigate why bridge decision is not seeded' });
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
const { channel } = bridgeDecision;
|
|
745
|
+
const { taskId } = bridgeDecision;
|
|
746
|
+
if (!isConfirm) {
|
|
747
|
+
output.missingDreamerTask++;
|
|
748
|
+
output.results.push({ candidateId, route: decision.route, status: 'would_intake_and_create', taskId, channel, statusBefore: 'pending', statusAfter: 'pending', intakeDecision: 'would_intake', seedDecision: 'would_seed', nextAction: 'Run with --confirm to intake and seed' });
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
const intakeEntry = await intakeService.intake(candidateId).catch((intakeErr) => {
|
|
752
|
+
output.intakeFailed++;
|
|
753
|
+
const intakeReason = intakeErr instanceof CandidateIntakeError
|
|
754
|
+
? `Intake failed [${intakeErr.code ?? 'unknown'}]: ${intakeErr.message}`
|
|
755
|
+
: `Intake failed: ${intakeErr instanceof Error ? intakeErr.message : String(intakeErr)}`;
|
|
756
|
+
output.results.push({ candidateId, route: decision.route, status: 'intake_failed', reason: intakeReason, statusBefore: 'pending', statusAfter: 'pending', intakeDecision: 'intake_failed', seedDecision: 'skipped', nextAction: 'Fix intake issue and re-run backfill' });
|
|
757
|
+
return null;
|
|
758
|
+
});
|
|
759
|
+
if (!intakeEntry) {
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
try {
|
|
763
|
+
await updateCandidateStatus({ stateManager, candidateId, targetStatus: 'consumed', expectedCurrentStatus: 'pending' });
|
|
764
|
+
}
|
|
765
|
+
catch (statusErr) {
|
|
766
|
+
const statusMsg = `Ledger write succeeded (entry ${intakeEntry.id}) but DB status update failed: ${statusErr instanceof Error ? statusErr.message : String(statusErr)}`;
|
|
767
|
+
output.intakeFailed++;
|
|
768
|
+
output.results.push({ candidateId, route: decision.route, status: 'intake_failed', reason: statusMsg, statusBefore: 'pending', statusAfter: 'pending', intakeDecision: 'intake_failed', seedDecision: 'skipped', nextAction: 'Candidate may be in inconsistent state — check ledger and DB manually' });
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
output.intakeSucceeded++;
|
|
772
|
+
const existingTask = await stateManager.getTask(taskId);
|
|
773
|
+
if (existingTask) {
|
|
774
|
+
output.alreadyHaveTask++;
|
|
775
|
+
output.results.push({ candidateId, route: decision.route, status: 'intake_succeeded_existing_task', taskId: existingTask.taskId, channel, statusBefore: 'pending', statusAfter: 'consumed', intakeDecision: 'intake_succeeded', seedDecision: 'existing', reason: `Intake succeeded but dreamer task ${existingTask.taskId} already exists` });
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
try {
|
|
779
|
+
const seed = buildDreamerTaskSeed(bridgeInput);
|
|
780
|
+
if ('decision' in seed) {
|
|
781
|
+
output.errors++;
|
|
782
|
+
output.results.push({ candidateId, route: decision.route, status: 'error', reason: seed.reason, statusBefore: 'pending', statusAfter: 'consumed', intakeDecision: 'intake_succeeded', seedDecision: 'skipped', nextAction: 'Intake succeeded but dreamer seed failed — candidate is consumed, re-run backfill for consumed candidates' });
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
const task = await stateManager.createTask({
|
|
786
|
+
taskId: seed.taskId,
|
|
787
|
+
taskKind: seed.taskKind,
|
|
788
|
+
status: seed.status,
|
|
789
|
+
attemptCount: seed.attemptCount,
|
|
790
|
+
maxAttempts: seed.maxAttempts,
|
|
791
|
+
diagnosticJson: seed.diagnosticJson,
|
|
792
|
+
});
|
|
793
|
+
output.created++;
|
|
794
|
+
output.results.push({ candidateId, route: decision.route, status: 'created', taskId: task.taskId, channel, statusBefore: 'pending', statusAfter: 'consumed', intakeDecision: 'intake_succeeded', seedDecision: 'seeded' });
|
|
795
|
+
}
|
|
796
|
+
catch (err) {
|
|
797
|
+
output.errors++;
|
|
798
|
+
output.results.push({ candidateId, route: decision.route, status: 'error', reason: err instanceof Error ? err.message : String(err), statusBefore: 'pending', statusAfter: 'consumed', intakeDecision: 'intake_succeeded', seedDecision: 'skipped', nextAction: 'Intake succeeded but dreamer task creation failed — candidate is consumed, re-run backfill for consumed candidates' });
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
if (opts.json) {
|
|
802
|
+
console.log(JSON.stringify({ ...createBackfillRemediationResult(output), details: output }, null, 2));
|
|
803
|
+
}
|
|
804
|
+
else {
|
|
805
|
+
const remediation = createBackfillRemediationResult(output);
|
|
806
|
+
console.log(`\nCandidate Internalization Backfill (${output.mode})\n`);
|
|
807
|
+
console.log(` status: ${remediation.status}`);
|
|
808
|
+
console.log(` safe_to_confirm: ${remediation.safeToConfirm}`);
|
|
809
|
+
console.log(` total_consumed: ${output.totalConsumed}`);
|
|
810
|
+
if (opts.includePending) {
|
|
811
|
+
console.log(` total_pending: ${output.totalPending}`);
|
|
812
|
+
}
|
|
813
|
+
console.log(` missing_dreamer: ${output.missingDreamerTask}`);
|
|
814
|
+
console.log(` already_have_task: ${output.alreadyHaveTask}`);
|
|
815
|
+
console.log(` deferred: ${output.deferred}`);
|
|
816
|
+
if (isConfirm) {
|
|
817
|
+
console.log(` created: ${output.created}`);
|
|
818
|
+
console.log(` errors: ${output.errors}`);
|
|
819
|
+
if (opts.includePending) {
|
|
820
|
+
console.log(` intake_succeeded: ${output.intakeSucceeded}`);
|
|
821
|
+
console.log(` intake_failed: ${output.intakeFailed}`);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
for (const r of output.results) {
|
|
825
|
+
const prefix = r.statusBefore === 'pending' ? '[pending]' : '[consumed]';
|
|
826
|
+
console.log(` ${prefix} ${r.candidateId}: ${r.status} (${r.route})${r.taskId ? ` → ${r.taskId}` : ''}${r.reason ? ` — ${r.reason}` : ''}`);
|
|
827
|
+
}
|
|
828
|
+
if (!isConfirm && (output.missingDreamerTask > 0 || output.totalPending > 0)) {
|
|
829
|
+
console.log(`\n (use --confirm to create missing dreamer tasks${opts.includePending ? ' and intake pending candidates' : ''})`);
|
|
830
|
+
}
|
|
831
|
+
console.log('');
|
|
832
|
+
}
|
|
833
|
+
if ((output.missingDreamerTask > 0 || output.totalPending > 0) && !isConfirm) {
|
|
834
|
+
process.exitCode = 1;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
finally {
|
|
838
|
+
await stateManager.close();
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* pd candidate route --candidate-id <id> --workspace <path> [--json]
|
|
843
|
+
*
|
|
844
|
+
* Read-only: shows which internalization pipeline route a candidate will enter,
|
|
845
|
+
* whether it's ready, and what fields are missing.
|
|
846
|
+
*/
|
|
847
|
+
export async function handleCandidateRoute(opts) {
|
|
848
|
+
const workspaceDir = resolveWorkspaceDir(opts.workspace);
|
|
849
|
+
const stateManager = new RuntimeStateManager({ workspaceDir });
|
|
850
|
+
try {
|
|
851
|
+
await stateManager.initialize();
|
|
852
|
+
const candidate = await stateManager.getCandidate(opts.candidateId);
|
|
853
|
+
if (!candidate) {
|
|
854
|
+
console.error(`Candidate not found: ${opts.candidateId}`);
|
|
855
|
+
process.exit(1);
|
|
856
|
+
return; // unreachable in production, needed for test mocks
|
|
857
|
+
}
|
|
858
|
+
const recommendation = resolveCandidateRecommendation(candidate, stateManager, opts.candidateId);
|
|
859
|
+
const decision = decideInternalizationRoute(recommendation);
|
|
860
|
+
const result = {
|
|
861
|
+
candidateId: opts.candidateId,
|
|
862
|
+
recommendationKind: recommendation.kind,
|
|
863
|
+
route: decision.route,
|
|
864
|
+
ready: decision.ready,
|
|
865
|
+
missingFields: decision.missingFields,
|
|
866
|
+
reason: decision.reason,
|
|
867
|
+
nextAction: decision.nextAction,
|
|
868
|
+
...(recommendation.usedFallback && { _meta: { source: 'column_fallback' } }),
|
|
869
|
+
};
|
|
870
|
+
if (opts.json) {
|
|
871
|
+
console.log(JSON.stringify(result, null, 2));
|
|
872
|
+
}
|
|
873
|
+
else {
|
|
874
|
+
console.log(`\nCandidate Route: ${result.candidateId}\n`);
|
|
875
|
+
console.log(` Kind: ${result.recommendationKind}`);
|
|
876
|
+
console.log(` Route: ${result.route}`);
|
|
877
|
+
console.log(` Ready: ${result.ready}`);
|
|
878
|
+
console.log(` Missing Fields: ${result.missingFields.length > 0 ? result.missingFields.join(', ') : '(none)'}`);
|
|
879
|
+
console.log(` Reason: ${result.reason}`);
|
|
880
|
+
console.log(` Next Action: ${result.nextAction}`);
|
|
881
|
+
if (recommendation.usedFallback) {
|
|
882
|
+
console.log(` Source: column_fallback (source_recommendation_json unavailable)`);
|
|
883
|
+
}
|
|
884
|
+
console.log('');
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
finally {
|
|
888
|
+
await stateManager.close();
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
//# sourceMappingURL=candidate.js.map
|