@principles/pd-cli 1.115.0 → 1.117.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/dist/commands/candidate.d.ts +23 -0
- package/dist/commands/candidate.d.ts.map +1 -1
- package/dist/commands/candidate.js +89 -3
- package/dist/commands/candidate.js.map +1 -1
- package/dist/commands/diagnose.d.ts.map +1 -1
- package/dist/commands/diagnose.js +153 -132
- package/dist/commands/diagnose.js.map +1 -1
- package/dist/commands/runtime-features.d.ts.map +1 -1
- package/dist/commands/runtime-features.js +2 -7
- package/dist/commands/runtime-features.js.map +1 -1
- package/dist/commands/runtime-internalization-integrity-repair.d.ts.map +1 -1
- package/dist/commands/runtime-internalization-integrity-repair.js +15 -31
- package/dist/commands/runtime-internalization-integrity-repair.js.map +1 -1
- package/dist/commands/runtime-internalization-run-once.d.ts.map +1 -1
- package/dist/commands/runtime-internalization-run-once.js +246 -326
- package/dist/commands/runtime-internalization-run-once.js.map +1 -1
- package/dist/commands/runtime-recovery.d.ts.map +1 -1
- package/dist/commands/runtime-recovery.js +9 -8
- package/dist/commands/runtime-recovery.js.map +1 -1
- package/dist/services/__tests__/cli-output.test.d.ts +18 -0
- package/dist/services/__tests__/cli-output.test.d.ts.map +1 -0
- package/dist/services/__tests__/cli-output.test.js +103 -0
- package/dist/services/__tests__/cli-output.test.js.map +1 -0
- package/dist/services/__tests__/runtime-adapter-resolver.test.d.ts +18 -0
- package/dist/services/__tests__/runtime-adapter-resolver.test.d.ts.map +1 -0
- package/dist/services/__tests__/runtime-adapter-resolver.test.js +651 -0
- package/dist/services/__tests__/runtime-adapter-resolver.test.js.map +1 -0
- package/dist/services/cli-output.d.ts +61 -0
- package/dist/services/cli-output.d.ts.map +1 -0
- package/dist/services/cli-output.js +72 -0
- package/dist/services/cli-output.js.map +1 -0
- package/dist/services/runtime-adapter-resolver.d.ts +105 -0
- package/dist/services/runtime-adapter-resolver.d.ts.map +1 -0
- package/dist/services/runtime-adapter-resolver.js +188 -0
- package/dist/services/runtime-adapter-resolver.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/candidate.ts +92 -3
- package/src/commands/diagnose.ts +146 -138
- package/src/commands/runtime-features.ts +2 -6
- package/src/commands/runtime-internalization-integrity-repair.ts +16 -28
- package/src/commands/runtime-internalization-run-once.ts +242 -353
- package/src/commands/runtime-recovery.ts +9 -7
- package/src/services/__tests__/cli-output.test.ts +130 -0
- package/src/services/__tests__/runtime-adapter-resolver.test.ts +772 -0
- package/src/services/cli-output.ts +95 -0
- package/src/services/runtime-adapter-resolver.ts +339 -0
- package/tests/commands/candidate-internalization-backfill.test.ts +43 -3
- package/tests/commands/candidate-internalize-lineage.test.ts +521 -0
- package/tests/commands/candidate-internalize.test.ts +31 -5
- package/tests/commands/diagnose.test.ts +7 -3
- package/tests/commands/runtime-internalization-run-once.test.ts +11 -0
- package/tests/commands/runtime-recovery.test.ts +27 -4
- package/tests/services/rulehost-pipeline-e2e.test.ts +40 -7
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PRI-435: Pain-to-dreamer lineage repair.
|
|
3
|
+
*
|
|
4
|
+
* Tests that `pd candidate internalize` resolves `sourcePainId` from the
|
|
5
|
+
* canonical diagnostician task/artifact chain and writes it as a top-level
|
|
6
|
+
* key in the dreamer task's diagnosticJson. Uses real SQLite stores and the
|
|
7
|
+
* real CLI handler — no mocks of private internals.
|
|
8
|
+
*
|
|
9
|
+
* ERR refs considered:
|
|
10
|
+
* - ERR-004: sourcePainId resolved from canonical chain, not invented
|
|
11
|
+
* - ERR-009: missing/malformed sourcePainId fails loud, no mutation
|
|
12
|
+
* - ERR-025: tests exercise the real production path, not isolated helpers
|
|
13
|
+
* - ERR-048: lineage write connected to the read path (findDreamerTaskForPain)
|
|
14
|
+
*/
|
|
15
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
16
|
+
import * as os from 'node:os';
|
|
17
|
+
import * as path from 'node:path';
|
|
18
|
+
import * as fs from 'node:fs';
|
|
19
|
+
import {
|
|
20
|
+
RuntimeStateManager,
|
|
21
|
+
SqliteDiagnosticianCommitter,
|
|
22
|
+
type DiagnosticianOutputV1,
|
|
23
|
+
} from '@principles/core/runtime-v2';
|
|
24
|
+
import { handleCandidateInternalize, handleCandidateInternalizationBackfill } from '../../src/commands/candidate.js';
|
|
25
|
+
|
|
26
|
+
let tmpDir = '';
|
|
27
|
+
|
|
28
|
+
function makeTmpDir(): string {
|
|
29
|
+
const dir = path.join(os.tmpdir(), `pd-pri435-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
|
|
30
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
31
|
+
return dir;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function diagnosticianDiagnosticJson(painId: string): string {
|
|
35
|
+
return JSON.stringify({
|
|
36
|
+
sourcePainId: painId,
|
|
37
|
+
reasonSummary: 'Wrote to /etc/passwd without confirmation',
|
|
38
|
+
source: 'test',
|
|
39
|
+
severity: 'high',
|
|
40
|
+
sessionIdHint: null,
|
|
41
|
+
agentIdHint: null,
|
|
42
|
+
provenance: 'automatic_hook',
|
|
43
|
+
provenanceReason: 'automatic hook',
|
|
44
|
+
evidence: [{ sourceRef: 'session://test', note: 'wrote system file' }],
|
|
45
|
+
workspaceDir: null,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function diagnosticianOutput(painId: string): DiagnosticianOutputV1 {
|
|
50
|
+
return {
|
|
51
|
+
valid: true,
|
|
52
|
+
diagnosisId: `diag-${painId}`,
|
|
53
|
+
summary: 'Wrote to system path without confirmation',
|
|
54
|
+
rootCause: 'Agent did not block system path writes',
|
|
55
|
+
violatedPrinciples: [],
|
|
56
|
+
evidence: [{ sourceRef: 'session://test', note: 'wrote system file' }],
|
|
57
|
+
recommendations: [{
|
|
58
|
+
kind: 'principle',
|
|
59
|
+
description: 'Block writes to system paths without owner confirmation',
|
|
60
|
+
abstractedPrinciple: 'Never write to OS system paths without explicit owner approval',
|
|
61
|
+
}],
|
|
62
|
+
confidence: 0.9,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function seedDiagnosisToCandidate(
|
|
67
|
+
sm: RuntimeStateManager,
|
|
68
|
+
painId: string,
|
|
69
|
+
): Promise<{ taskId: string; candidateId: string }> {
|
|
70
|
+
const taskId = `diagnostician-${painId}`;
|
|
71
|
+
await sm.createTask({
|
|
72
|
+
taskId,
|
|
73
|
+
taskKind: 'diagnostician',
|
|
74
|
+
inputRef: painId,
|
|
75
|
+
status: 'pending',
|
|
76
|
+
attemptCount: 0,
|
|
77
|
+
maxAttempts: 3,
|
|
78
|
+
diagnosticJson: diagnosticianDiagnosticJson(painId),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const committer = new SqliteDiagnosticianCommitter(sm.connection);
|
|
82
|
+
await sm.acquireLease({ taskId, owner: 'test-owner', durationMs: 60_000, runtimeKind: 'openclaw' });
|
|
83
|
+
const runs = await sm.getRunsByTask(taskId);
|
|
84
|
+
const runId = runs[0]?.runId;
|
|
85
|
+
if (!runId) throw new Error(`No run created for task ${taskId}`);
|
|
86
|
+
|
|
87
|
+
const commitResult = await committer.commit({
|
|
88
|
+
runId,
|
|
89
|
+
taskId,
|
|
90
|
+
output: diagnosticianOutput(painId),
|
|
91
|
+
idempotencyKey: `idem-${taskId}`,
|
|
92
|
+
});
|
|
93
|
+
await sm.markTaskSucceeded(taskId, `artifact://${commitResult.artifactId}`);
|
|
94
|
+
|
|
95
|
+
const candidates = await sm.getCandidatesByTaskId(taskId);
|
|
96
|
+
const [candidate] = candidates;
|
|
97
|
+
if (!candidate) throw new Error('No candidate produced by diagnostician committer');
|
|
98
|
+
return { taskId, candidateId: candidate.candidateId };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Seeds a diagnostician task → candidate but writes diagnosticJson WITHOUT
|
|
103
|
+
* the sourcePainId field. This simulates data corruption or a pre-PRI-435
|
|
104
|
+
* diagnostician task that lacks the lineage key. The candidate internalize
|
|
105
|
+
* path must fail loud and produce no dreamer task side effect.
|
|
106
|
+
*/
|
|
107
|
+
async function seedDiagnosisToCandidateWithoutLineage(
|
|
108
|
+
sm: RuntimeStateManager,
|
|
109
|
+
painId: string,
|
|
110
|
+
): Promise<{ taskId: string; candidateId: string }> {
|
|
111
|
+
const taskId = `diagnostician-nolineage-${painId}`;
|
|
112
|
+
const diagnosticJsonWithoutSourcePainId = JSON.stringify({
|
|
113
|
+
// sourcePainId intentionally omitted
|
|
114
|
+
reasonSummary: 'Wrote to /etc/passwd without confirmation',
|
|
115
|
+
source: 'test',
|
|
116
|
+
severity: 'high',
|
|
117
|
+
sessionIdHint: null,
|
|
118
|
+
agentIdHint: null,
|
|
119
|
+
provenance: 'automatic_hook',
|
|
120
|
+
provenanceReason: 'automatic hook',
|
|
121
|
+
evidence: [{ sourceRef: 'session://test', note: 'wrote system file' }],
|
|
122
|
+
workspaceDir: null,
|
|
123
|
+
});
|
|
124
|
+
await sm.createTask({
|
|
125
|
+
taskId,
|
|
126
|
+
taskKind: 'diagnostician',
|
|
127
|
+
inputRef: painId,
|
|
128
|
+
status: 'pending',
|
|
129
|
+
attemptCount: 0,
|
|
130
|
+
maxAttempts: 3,
|
|
131
|
+
diagnosticJson: diagnosticJsonWithoutSourcePainId,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const committer = new SqliteDiagnosticianCommitter(sm.connection);
|
|
135
|
+
await sm.acquireLease({ taskId, owner: 'test-owner', durationMs: 60_000, runtimeKind: 'openclaw' });
|
|
136
|
+
const runs = await sm.getRunsByTask(taskId);
|
|
137
|
+
const runId = runs[0]?.runId;
|
|
138
|
+
if (!runId) throw new Error(`No run created for task ${taskId}`);
|
|
139
|
+
|
|
140
|
+
const commitResult = await committer.commit({
|
|
141
|
+
runId,
|
|
142
|
+
taskId,
|
|
143
|
+
output: diagnosticianOutput(painId),
|
|
144
|
+
idempotencyKey: `idem-${taskId}`,
|
|
145
|
+
});
|
|
146
|
+
await sm.markTaskSucceeded(taskId, `artifact://${commitResult.artifactId}`);
|
|
147
|
+
|
|
148
|
+
const candidates = await sm.getCandidatesByTaskId(taskId);
|
|
149
|
+
const [candidate] = candidates;
|
|
150
|
+
if (!candidate) throw new Error('No candidate produced by diagnostician committer');
|
|
151
|
+
return { taskId, candidateId: candidate.candidateId };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
describe('PRI-435: candidate internalize resolves sourcePainId from diagnostician chain', () => {
|
|
155
|
+
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
|
156
|
+
|
|
157
|
+
beforeEach(() => {
|
|
158
|
+
tmpDir = makeTmpDir();
|
|
159
|
+
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
afterEach(async () => {
|
|
163
|
+
consoleLogSpy.mockRestore();
|
|
164
|
+
if (tmpDir) {
|
|
165
|
+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
166
|
+
tmpDir = '';
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('creates exactly one dreamer task with top-level sourcePainId matching the originating pain', async () => {
|
|
171
|
+
// ── Arrange: pain → diagnostician task (with sourcePainId) → candidate ──
|
|
172
|
+
const painId = 'pain-pri435-001';
|
|
173
|
+
let candidateId = '';
|
|
174
|
+
|
|
175
|
+
{
|
|
176
|
+
const sm = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
177
|
+
await sm.initialize();
|
|
178
|
+
const seeded = await seedDiagnosisToCandidate(sm, painId);
|
|
179
|
+
candidateId = seeded.candidateId;
|
|
180
|
+
await sm.close();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ── Act: run the real CLI handler (not dry-run → creates the task) ──
|
|
184
|
+
await handleCandidateInternalize({
|
|
185
|
+
candidateId,
|
|
186
|
+
workspace: tmpDir,
|
|
187
|
+
json: true,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// ── Assert: exactly one dreamer task with top-level sourcePainId ──
|
|
191
|
+
const sm2 = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
192
|
+
await sm2.initialize();
|
|
193
|
+
const allTasks = await sm2.listTasks();
|
|
194
|
+
const dreamerTasks = allTasks.filter((t) => t.taskKind === 'dreamer');
|
|
195
|
+
expect(dreamerTasks.length, 'exactly one dreamer task').toBe(1);
|
|
196
|
+
|
|
197
|
+
const dreamerTask = dreamerTasks[0];
|
|
198
|
+
expect(dreamerTask.diagnosticJson).toBeDefined();
|
|
199
|
+
const parsed: unknown = JSON.parse(dreamerTask.diagnosticJson as string);
|
|
200
|
+
expect(parsed).not.toBeNull();
|
|
201
|
+
expect(typeof parsed).toBe('object');
|
|
202
|
+
expect(Object.hasOwn(parsed, 'sourcePainId'), 'sourcePainId must be a top-level key').toBe(true);
|
|
203
|
+
const storedPainId = Reflect.get(parsed, 'sourcePainId');
|
|
204
|
+
expect(storedPainId).toBe(painId);
|
|
205
|
+
await sm2.close();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe('PRI-435: candidate internalization backfill (consumed) resolves sourcePainId from diagnostician chain', () => {
|
|
210
|
+
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
|
211
|
+
|
|
212
|
+
beforeEach(() => {
|
|
213
|
+
tmpDir = makeTmpDir();
|
|
214
|
+
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
afterEach(async () => {
|
|
218
|
+
consoleLogSpy.mockRestore();
|
|
219
|
+
if (tmpDir) {
|
|
220
|
+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
221
|
+
tmpDir = '';
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('backfill --confirm creates dreamer task with top-level sourcePainId for consumed candidate missing dreamer task', async () => {
|
|
226
|
+
// ── Arrange: pain → diagnostician task (with sourcePainId) → candidate ──
|
|
227
|
+
// Then mark candidate as 'consumed' WITHOUT creating a dreamer task,
|
|
228
|
+
// simulating the broken pre-PRI-435 state where backfill had to repair lineage.
|
|
229
|
+
const painId = 'pain-pri435-backfill-consumed-001';
|
|
230
|
+
let candidateId = '';
|
|
231
|
+
|
|
232
|
+
{
|
|
233
|
+
const sm = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
234
|
+
await sm.initialize();
|
|
235
|
+
const seeded = await seedDiagnosisToCandidate(sm, painId);
|
|
236
|
+
candidateId = seeded.candidateId;
|
|
237
|
+
|
|
238
|
+
// Mark candidate as consumed without creating a dreamer task — simulates
|
|
239
|
+
// the broken state where consumed candidates lack a dreamer task.
|
|
240
|
+
const updated = await sm.updateCandidateStatus(candidateId, { status: 'consumed' });
|
|
241
|
+
if (!updated) throw new Error(`Failed to mark candidate ${candidateId} as consumed`);
|
|
242
|
+
await sm.close();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ── Act: run the real backfill handler with --confirm ──
|
|
246
|
+
await handleCandidateInternalizationBackfill({
|
|
247
|
+
workspace: tmpDir,
|
|
248
|
+
confirm: true,
|
|
249
|
+
json: true,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// ── Assert: exactly one dreamer task with top-level sourcePainId ──
|
|
253
|
+
const sm2 = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
254
|
+
await sm2.initialize();
|
|
255
|
+
const allTasks = await sm2.listTasks();
|
|
256
|
+
const dreamerTasks = allTasks.filter((t) => t.taskKind === 'dreamer');
|
|
257
|
+
expect(dreamerTasks.length, 'backfill should create exactly one dreamer task').toBe(1);
|
|
258
|
+
|
|
259
|
+
const dreamerTask = dreamerTasks[0];
|
|
260
|
+
expect(dreamerTask.diagnosticJson).toBeDefined();
|
|
261
|
+
const parsed: unknown = JSON.parse(dreamerTask.diagnosticJson as string);
|
|
262
|
+
expect(parsed).not.toBeNull();
|
|
263
|
+
expect(typeof parsed).toBe('object');
|
|
264
|
+
expect(Object.hasOwn(parsed, 'sourcePainId'), 'sourcePainId must be a top-level key').toBe(true);
|
|
265
|
+
const storedPainId = Reflect.get(parsed, 'sourcePainId');
|
|
266
|
+
expect(storedPainId).toBe(painId);
|
|
267
|
+
await sm2.close();
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
describe('PRI-435: candidate internalize fails loud when sourcePainId is missing from diagnostician chain', () => {
|
|
272
|
+
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
|
273
|
+
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
|
|
274
|
+
let exitSpy: ReturnType<typeof vi.spyOn>;
|
|
275
|
+
|
|
276
|
+
beforeEach(() => {
|
|
277
|
+
tmpDir = makeTmpDir();
|
|
278
|
+
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
279
|
+
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
280
|
+
exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as () => never);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
afterEach(async () => {
|
|
284
|
+
consoleLogSpy.mockRestore();
|
|
285
|
+
consoleErrorSpy.mockRestore();
|
|
286
|
+
exitSpy.mockRestore();
|
|
287
|
+
if (tmpDir) {
|
|
288
|
+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
289
|
+
tmpDir = '';
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('does not create a dreamer task and exits non-zero with reason + nextAction when diagnostician task lacks sourcePainId', async () => {
|
|
294
|
+
// ── Arrange: diagnostician task WITHOUT sourcePainId → candidate ──
|
|
295
|
+
const painId = 'pain-pri435-fail-loud-001';
|
|
296
|
+
let candidateId = '';
|
|
297
|
+
|
|
298
|
+
{
|
|
299
|
+
const sm = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
300
|
+
await sm.initialize();
|
|
301
|
+
const seeded = await seedDiagnosisToCandidateWithoutLineage(sm, painId);
|
|
302
|
+
candidateId = seeded.candidateId;
|
|
303
|
+
await sm.close();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// ── Act: run the real CLI handler ──
|
|
307
|
+
await handleCandidateInternalize({
|
|
308
|
+
candidateId,
|
|
309
|
+
workspace: tmpDir,
|
|
310
|
+
json: true,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// ── Assert: process.exit(1) called ──
|
|
314
|
+
expect(exitSpy).toHaveBeenCalledWith(1);
|
|
315
|
+
|
|
316
|
+
// ── Assert: NO dreamer task created (no side effects) ──
|
|
317
|
+
const sm2 = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
318
|
+
await sm2.initialize();
|
|
319
|
+
const allTasks = await sm2.listTasks();
|
|
320
|
+
const dreamerTasks = allTasks.filter((t) => t.taskKind === 'dreamer');
|
|
321
|
+
expect(dreamerTasks.length, 'no dreamer task should be created when sourcePainId is missing').toBe(0);
|
|
322
|
+
await sm2.close();
|
|
323
|
+
|
|
324
|
+
// ── Assert: JSON output contains reason and nextAction ──
|
|
325
|
+
const jsonOutput = consoleLogSpy.mock.calls
|
|
326
|
+
.map((c) => String(c[0]))
|
|
327
|
+
.find((s) => s.includes('"candidateId"') && s.includes('"reason"'));
|
|
328
|
+
expect(jsonOutput, 'JSON error result must be printed').toBeDefined();
|
|
329
|
+
// PRI-435 (CodeRabbit P2): no `as` bypass; strict null check before Object.hasOwn
|
|
330
|
+
expect(typeof jsonOutput).toBe('string');
|
|
331
|
+
if (typeof jsonOutput !== 'string') throw new Error('JSON error result must be a string');
|
|
332
|
+
const parsed: unknown = JSON.parse(jsonOutput);
|
|
333
|
+
const isRecord = parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed);
|
|
334
|
+
expect(isRecord, 'JSON error result must be an object').toBe(true);
|
|
335
|
+
if (!isRecord) throw new Error('Malformed JSON error result');
|
|
336
|
+
expect(Object.hasOwn(parsed, 'reason'), 'reason field must be present').toBe(true);
|
|
337
|
+
expect(Object.hasOwn(parsed, 'nextAction'), 'nextAction field must be present').toBe(true);
|
|
338
|
+
const reason = Reflect.get(parsed, 'reason');
|
|
339
|
+
expect(typeof reason).toBe('string');
|
|
340
|
+
expect(reason.length, 'reason must not be empty').toBeGreaterThan(0);
|
|
341
|
+
const nextAction = Reflect.get(parsed, 'nextAction');
|
|
342
|
+
expect(typeof nextAction).toBe('string');
|
|
343
|
+
expect(nextAction.length, 'nextAction must not be empty').toBeGreaterThan(0);
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
describe('PRI-435: backfill fails loud per-candidate when sourcePainId is missing', () => {
|
|
348
|
+
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
|
349
|
+
|
|
350
|
+
beforeEach(() => {
|
|
351
|
+
tmpDir = makeTmpDir();
|
|
352
|
+
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
afterEach(async () => {
|
|
356
|
+
consoleLogSpy.mockRestore();
|
|
357
|
+
if (tmpDir) {
|
|
358
|
+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
359
|
+
tmpDir = '';
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it('backfill --confirm does not create dreamer task for consumed candidate lacking sourcePainId and reports error with nextAction', async () => {
|
|
364
|
+
// ── Arrange: diagnostician task WITHOUT sourcePainId → candidate (consumed) ──
|
|
365
|
+
const painId = 'pain-pri435-backfill-fail-loud-001';
|
|
366
|
+
let candidateId = '';
|
|
367
|
+
|
|
368
|
+
{
|
|
369
|
+
const sm = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
370
|
+
await sm.initialize();
|
|
371
|
+
const seeded = await seedDiagnosisToCandidateWithoutLineage(sm, painId);
|
|
372
|
+
candidateId = seeded.candidateId;
|
|
373
|
+
const updated = await sm.updateCandidateStatus(candidateId, { status: 'consumed' });
|
|
374
|
+
if (!updated) throw new Error(`Failed to mark candidate ${candidateId} as consumed`);
|
|
375
|
+
await sm.close();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// ── Act: run the real backfill handler with --confirm ──
|
|
379
|
+
await handleCandidateInternalizationBackfill({
|
|
380
|
+
workspace: tmpDir,
|
|
381
|
+
confirm: true,
|
|
382
|
+
json: true,
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
// ── Assert: NO dreamer task created (no side effects) ──
|
|
386
|
+
const sm2 = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
387
|
+
await sm2.initialize();
|
|
388
|
+
const allTasks = await sm2.listTasks();
|
|
389
|
+
const dreamerTasks = allTasks.filter((t) => t.taskKind === 'dreamer');
|
|
390
|
+
expect(dreamerTasks.length, 'no dreamer task should be created when sourcePainId is missing').toBe(0);
|
|
391
|
+
|
|
392
|
+
// ── Assert: JSON output contains an error result with reason + nextAction ──
|
|
393
|
+
const jsonOutput = consoleLogSpy.mock.calls
|
|
394
|
+
.map((c) => String(c[0]))
|
|
395
|
+
.find((s) => s.includes('"results"') && s.includes('"errors"'));
|
|
396
|
+
expect(jsonOutput, 'backfill JSON result must be printed').toBeDefined();
|
|
397
|
+
// PRI-435 (CodeRabbit P2): no `as` bypass; strict null check before Object.hasOwn
|
|
398
|
+
expect(typeof jsonOutput).toBe('string');
|
|
399
|
+
if (typeof jsonOutput !== 'string') throw new Error('backfill JSON result must be a string');
|
|
400
|
+
const parsed: unknown = JSON.parse(jsonOutput);
|
|
401
|
+
const isRecord = parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed);
|
|
402
|
+
expect(isRecord, 'backfill JSON result must be an object').toBe(true);
|
|
403
|
+
if (!isRecord) throw new Error('Malformed backfill JSON result');
|
|
404
|
+
// The backfill JSON shape is { ...remediation, details: { errors, created, results, ... } }
|
|
405
|
+
expect(Object.hasOwn(parsed, 'details'), 'details field must be present').toBe(true);
|
|
406
|
+
const details = Reflect.get(parsed, 'details');
|
|
407
|
+
expect(details).not.toBeNull();
|
|
408
|
+
expect(typeof details).toBe('object');
|
|
409
|
+
expect(Object.hasOwn(details, 'errors'), 'errors count must be present in details').toBe(true);
|
|
410
|
+
const errors = Reflect.get(details, 'errors');
|
|
411
|
+
expect(errors, 'at least one error must be recorded').toBeGreaterThanOrEqual(1);
|
|
412
|
+
expect(Object.hasOwn(details, 'created'), 'created count must be present in details').toBe(true);
|
|
413
|
+
const created = Reflect.get(details, 'created');
|
|
414
|
+
expect(created, 'no dreamer task should be created').toBe(0);
|
|
415
|
+
|
|
416
|
+
const results = Reflect.get(details, 'results');
|
|
417
|
+
expect(Array.isArray(results), 'results must be an array').toBe(true);
|
|
418
|
+
const errorResult = (results as Array<Record<string, unknown>>).find(
|
|
419
|
+
(r) => r.status === 'error' || r.seedDecision === 'skipped',
|
|
420
|
+
);
|
|
421
|
+
expect(errorResult, 'an error/skipped result for the missing-lineage candidate must exist').toBeDefined();
|
|
422
|
+
expect(Object.hasOwn(errorResult as object, 'reason'), 'reason field must be present').toBe(true);
|
|
423
|
+
expect(Object.hasOwn(errorResult as object, 'nextAction'), 'nextAction field must be present').toBe(true);
|
|
424
|
+
const reason = Reflect.get(errorResult as object, 'reason');
|
|
425
|
+
expect(typeof reason).toBe('string');
|
|
426
|
+
expect(reason.length, 'reason must not be empty').toBeGreaterThan(0);
|
|
427
|
+
const nextAction = Reflect.get(errorResult as object, 'nextAction');
|
|
428
|
+
expect(typeof nextAction).toBe('string');
|
|
429
|
+
expect(nextAction.length, 'nextAction must not be empty').toBeGreaterThan(0);
|
|
430
|
+
|
|
431
|
+
await sm2.close();
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* PRI-435 (CodeRabbit P1 regression): Verify that resolveSourcePainIdFromDiagnostician
|
|
437
|
+
* rejects tasks whose taskKind is not 'diagnostician'. This prevents cross-task-chain
|
|
438
|
+
* lineage contamination when candidate.taskId points to a non-diagnostician task.
|
|
439
|
+
*/
|
|
440
|
+
describe('PRI-435: candidate internalize rejects non-diagnostician task for lineage resolution', () => {
|
|
441
|
+
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
|
442
|
+
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
|
|
443
|
+
let exitSpy: ReturnType<typeof vi.spyOn>;
|
|
444
|
+
|
|
445
|
+
beforeEach(() => {
|
|
446
|
+
tmpDir = makeTmpDir();
|
|
447
|
+
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
448
|
+
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
449
|
+
exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as () => never);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
afterEach(async () => {
|
|
453
|
+
consoleLogSpy.mockRestore();
|
|
454
|
+
consoleErrorSpy.mockRestore();
|
|
455
|
+
exitSpy.mockRestore();
|
|
456
|
+
if (tmpDir) {
|
|
457
|
+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
458
|
+
tmpDir = '';
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it('does not create a dreamer task when candidate.taskId points to a non-diagnostician task', async () => {
|
|
463
|
+
const painId = 'pain-pri435-wrong-taskkind-001';
|
|
464
|
+
let candidateId = '';
|
|
465
|
+
|
|
466
|
+
{
|
|
467
|
+
const sm = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
468
|
+
await sm.initialize();
|
|
469
|
+
|
|
470
|
+
// Seed through the normal diagnostician path (creates diagnostician task + candidate)
|
|
471
|
+
const seeded = await seedDiagnosisToCandidate(sm, painId);
|
|
472
|
+
candidateId = seeded.candidateId;
|
|
473
|
+
|
|
474
|
+
// Corrupt the task's task_kind to simulate cross-task-chain pollution.
|
|
475
|
+
// The resolver must reject this even though diagnosticJson has sourcePainId.
|
|
476
|
+
sm.connection
|
|
477
|
+
.getDb()
|
|
478
|
+
.prepare('UPDATE tasks SET task_kind = ? WHERE task_id = ?')
|
|
479
|
+
.run('dreamer', seeded.taskId);
|
|
480
|
+
|
|
481
|
+
await sm.close();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// Act: run the real CLI handler
|
|
485
|
+
await handleCandidateInternalize({
|
|
486
|
+
candidateId,
|
|
487
|
+
workspace: tmpDir,
|
|
488
|
+
json: true,
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
// Assert: process.exit(1) called (fail loud)
|
|
492
|
+
expect(exitSpy).toHaveBeenCalledWith(1);
|
|
493
|
+
|
|
494
|
+
// Assert: NO dreamer task created (no side effects)
|
|
495
|
+
const sm2 = new RuntimeStateManager({ workspaceDir: tmpDir });
|
|
496
|
+
await sm2.initialize();
|
|
497
|
+
const allTasks = await sm2.listTasks();
|
|
498
|
+
// The original diagnostician task was corrupted to 'dreamer' kind, but no NEW
|
|
499
|
+
// dreamer task should be created by the internalize handler.
|
|
500
|
+
const newDreamerTasks = allTasks.filter(
|
|
501
|
+
(t) => t.taskKind === 'dreamer' && !t.taskId.startsWith('diagnostician-'),
|
|
502
|
+
);
|
|
503
|
+
expect(newDreamerTasks.length, 'no new dreamer task should be created when taskKind is wrong').toBe(0);
|
|
504
|
+
await sm2.close();
|
|
505
|
+
|
|
506
|
+
// Assert: JSON output contains reason and nextAction
|
|
507
|
+
const jsonOutput = consoleLogSpy.mock.calls
|
|
508
|
+
.map((c) => String(c[0]))
|
|
509
|
+
.find((s) => s.includes('"candidateId"') && s.includes('"reason"'));
|
|
510
|
+
expect(jsonOutput, 'JSON error result must be printed').toBeDefined();
|
|
511
|
+
// PRI-435 (CodeRabbit P2): no `as` bypass; strict null check before Object.hasOwn
|
|
512
|
+
expect(typeof jsonOutput).toBe('string');
|
|
513
|
+
if (typeof jsonOutput !== 'string') throw new Error('JSON error result must be a string');
|
|
514
|
+
const parsed: unknown = JSON.parse(jsonOutput);
|
|
515
|
+
const isRecord = parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed);
|
|
516
|
+
expect(isRecord, 'JSON error result must be an object').toBe(true);
|
|
517
|
+
if (!isRecord) throw new Error('Malformed JSON error result');
|
|
518
|
+
expect(Object.hasOwn(parsed, 'reason'), 'reason field must be present').toBe(true);
|
|
519
|
+
expect(Object.hasOwn(parsed, 'nextAction'), 'nextAction field must be present').toBe(true);
|
|
520
|
+
});
|
|
521
|
+
});
|
|
@@ -62,7 +62,21 @@ describe('handleCandidateInternalize (PRI-89)', () => {
|
|
|
62
62
|
beforeEach(() => {
|
|
63
63
|
vi.clearAllMocks();
|
|
64
64
|
mockStateManager.getCandidate.mockResolvedValue(null);
|
|
65
|
-
|
|
65
|
+
// PRI-435: getTask must return different values based on taskId:
|
|
66
|
+
// - candidate.taskId ('task-*') → diagnostician task with sourcePainId in diagnosticJson
|
|
67
|
+
// - dreamer task ID ('dreamer-*') → null (no existing dreamer task)
|
|
68
|
+
mockStateManager.getTask.mockImplementation((taskId: string) => {
|
|
69
|
+
if (taskId.startsWith('task-')) {
|
|
70
|
+
return Promise.resolve({
|
|
71
|
+
taskId,
|
|
72
|
+
taskKind: 'diagnostician',
|
|
73
|
+
status: 'completed',
|
|
74
|
+
diagnosticJson: JSON.stringify({ sourcePainId: 'pain-001' }),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
// Dreamer task lookup → null (no existing dreamer task)
|
|
78
|
+
return Promise.resolve(null);
|
|
79
|
+
});
|
|
66
80
|
mockStateManager.createTask.mockResolvedValue({
|
|
67
81
|
taskId: 'dreamer-cand-001-prompt',
|
|
68
82
|
taskKind: 'dreamer',
|
|
@@ -111,10 +125,22 @@ describe('handleCandidateInternalize (PRI-89)', () => {
|
|
|
111
125
|
reason: 'Ready',
|
|
112
126
|
nextAction: 'Proceed',
|
|
113
127
|
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
128
|
+
// PRI-435: getTask returns diagnostician task for 'task-*' (lineage resolution),
|
|
129
|
+
// and existing dreamer task for 'dreamer-*' (idempotency check).
|
|
130
|
+
mockStateManager.getTask.mockImplementation((taskId: string) => {
|
|
131
|
+
if (taskId.startsWith('task-')) {
|
|
132
|
+
return Promise.resolve({
|
|
133
|
+
taskId,
|
|
134
|
+
taskKind: 'diagnostician',
|
|
135
|
+
status: 'completed',
|
|
136
|
+
diagnosticJson: JSON.stringify({ sourcePainId: 'pain-001' }),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return Promise.resolve({
|
|
140
|
+
taskId: 'dreamer-cand-001-prompt',
|
|
141
|
+
taskKind: 'dreamer',
|
|
142
|
+
status: 'pending',
|
|
143
|
+
});
|
|
118
144
|
});
|
|
119
145
|
|
|
120
146
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
@@ -361,7 +361,7 @@ describe('pd diagnose run --runtime routing', () => {
|
|
|
361
361
|
});
|
|
362
362
|
|
|
363
363
|
it('CLI-04: unknown runtime kind exits with error and exit code 1', async () => {
|
|
364
|
-
const
|
|
364
|
+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
365
365
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as () => never);
|
|
366
366
|
|
|
367
367
|
await handleDiagnoseRun({
|
|
@@ -371,10 +371,14 @@ describe('pd diagnose run --runtime routing', () => {
|
|
|
371
371
|
json: true,
|
|
372
372
|
} as DiagnoseRunOptions);
|
|
373
373
|
|
|
374
|
-
expect(
|
|
374
|
+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
|
|
375
|
+
const jsonOutput = JSON.parse(consoleLogSpy.mock.calls[0][0] as string);
|
|
376
|
+
expect(jsonOutput.ok).toBe(false);
|
|
377
|
+
expect(jsonOutput.reason).toBe('unsupported_runtime_kind: invalid-runtime');
|
|
378
|
+
expect(jsonOutput.nextAction).toContain('openclaw-cli');
|
|
375
379
|
expect(exitSpy).toHaveBeenCalledWith(1);
|
|
376
380
|
|
|
377
|
-
|
|
381
|
+
consoleLogSpy.mockRestore();
|
|
378
382
|
exitSpy.mockRestore();
|
|
379
383
|
});
|
|
380
384
|
|
|
@@ -41,6 +41,17 @@ vi.mock('../../src/services/resolve-runtime-from-pd-config.js', () => ({
|
|
|
41
41
|
resolveRuntimeFromPdConfig: mockResolveRuntimeFromPdConfig,
|
|
42
42
|
}));
|
|
43
43
|
|
|
44
|
+
// PRI-431: Mock feature-flag-loader so the shared resolver's L2 dreamer sub-branch
|
|
45
|
+
// doesn't call the real loadEffectiveFeatureFlags (which needs computeEffectiveFlags
|
|
46
|
+
// from @principles/core/runtime-v2 — not included in the mock above).
|
|
47
|
+
vi.mock('../../src/services/feature-flag-loader.js', () => ({
|
|
48
|
+
loadEffectiveFeatureFlags: vi.fn().mockReturnValue({
|
|
49
|
+
flags: {},
|
|
50
|
+
warnings: [],
|
|
51
|
+
configPath: '/fake/workspace/.pd/feature-flags.yaml',
|
|
52
|
+
}),
|
|
53
|
+
}));
|
|
54
|
+
|
|
44
55
|
vi.mock('@principles/core/runtime-v2', () => ({
|
|
45
56
|
RuntimeStateManager: vi.fn().mockImplementation(function () {
|
|
46
57
|
return {
|
|
@@ -52,7 +52,7 @@ describe('pd runtime recovery sweep remediation contract', () => {
|
|
|
52
52
|
|
|
53
53
|
await handleRuntimeRecoverySweep({ workspace: '/fake/workspace', dryRun: true, json: true });
|
|
54
54
|
|
|
55
|
-
const output = JSON.parse(consoleLogSpy.mock.calls[0][0]
|
|
55
|
+
const output = JSON.parse(String(consoleLogSpy.mock.calls[0][0]));
|
|
56
56
|
expect(output).toMatchObject({
|
|
57
57
|
mode: 'dry_run',
|
|
58
58
|
status: 'would_change',
|
|
@@ -71,7 +71,7 @@ describe('pd runtime recovery sweep remediation contract', () => {
|
|
|
71
71
|
|
|
72
72
|
await handleRuntimeRecoverySweep({ workspace: '/fake/workspace', confirm: true, json: true });
|
|
73
73
|
|
|
74
|
-
const output = JSON.parse(consoleLogSpy.mock.calls[0][0]
|
|
74
|
+
const output = JSON.parse(String(consoleLogSpy.mock.calls[0][0]));
|
|
75
75
|
expect(output.mode).toBe('confirm');
|
|
76
76
|
expect(output.status).toBe('changed');
|
|
77
77
|
expect(output.repairedCount).toBe(1);
|
|
@@ -80,12 +80,35 @@ describe('pd runtime recovery sweep remediation contract', () => {
|
|
|
80
80
|
});
|
|
81
81
|
|
|
82
82
|
it('rejects --dry-run and --confirm together before writing (no service created)', async () => {
|
|
83
|
-
|
|
83
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code) => { throw new Error(`process.exit:${code}`); });
|
|
84
|
+
|
|
85
|
+
await expect(
|
|
86
|
+
handleRuntimeRecoverySweep({ workspace: '/fake/workspace', dryRun: true, confirm: true, json: true }),
|
|
87
|
+
).rejects.toThrow('process.exit:1');
|
|
88
|
+
|
|
89
|
+
expect(mockRecoverTask).not.toHaveBeenCalled();
|
|
90
|
+
expect(mockServiceClose).not.toHaveBeenCalled();
|
|
91
|
+
// PRI-432: JSON mode emits structured error to stdout (CLI Operator Gate rule #1).
|
|
92
|
+
const output = JSON.parse(String(consoleLogSpy.mock.calls[0][0]));
|
|
93
|
+
expect(output.ok).toBe(false);
|
|
94
|
+
expect(output.reason).toContain('mutually exclusive');
|
|
95
|
+
expect(output.nextAction).toBeTruthy();
|
|
96
|
+
|
|
97
|
+
exitSpy.mockRestore();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('rejects --dry-run and --confirm together in text mode via stderr', async () => {
|
|
101
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code) => { throw new Error(`process.exit:${code}`); });
|
|
102
|
+
|
|
103
|
+
await expect(
|
|
104
|
+
handleRuntimeRecoverySweep({ workspace: '/fake/workspace', dryRun: true, confirm: true, json: false }),
|
|
105
|
+
).rejects.toThrow('process.exit:1');
|
|
84
106
|
|
|
85
107
|
expect(mockRecoverTask).not.toHaveBeenCalled();
|
|
86
108
|
expect(mockServiceClose).not.toHaveBeenCalled();
|
|
87
|
-
expect(process.exitCode).toBe(1);
|
|
88
109
|
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('mutually exclusive'));
|
|
110
|
+
|
|
111
|
+
exitSpy.mockRestore();
|
|
89
112
|
});
|
|
90
113
|
|
|
91
114
|
it('uses createRecoverySweepService (no RuntimeStateManager)', async () => {
|