@principles/pd-cli 1.116.0 → 1.118.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.
@@ -134,11 +134,25 @@ function setupDefaultMocks(): void {
134
134
  mockGetCandidate.mockImplementation((id: string) =>
135
135
  Promise.resolve({
136
136
  candidateId: id,
137
+ taskId: `diag-task-${id}`,
137
138
  description: `candidate ${id}`,
138
139
  sourceRecommendationJson: JSON.stringify({ kind: 'principle', description: `candidate ${id}` }),
139
140
  }),
140
141
  );
141
- mockGetTask.mockResolvedValue(null);
142
+ // PRI-435: getTask must return a diagnostician task with sourcePainId when called
143
+ // with candidate.taskId (diag-task-*), and null for dreamer task lookups.
144
+ mockGetTask.mockImplementation((taskId: string) => {
145
+ if (taskId.startsWith('diag-task-')) {
146
+ return Promise.resolve({
147
+ taskId,
148
+ taskKind: 'diagnostician',
149
+ status: 'completed',
150
+ diagnosticJson: JSON.stringify({ sourcePainId: `pain-${taskId}` }),
151
+ });
152
+ }
153
+ // Dreamer task lookup → null (no existing dreamer task)
154
+ return Promise.resolve(null);
155
+ });
142
156
  mockCreateTask.mockImplementation((input: { taskId: string }) =>
143
157
  Promise.resolve({ taskId: input.taskId }),
144
158
  );
@@ -260,7 +274,20 @@ describe('pd candidate internalization backfill', () => {
260
274
  if (sql.includes("'pending'")) return [{ candidate_id: 'cand-pending-1' }];
261
275
  return [];
262
276
  });
263
- mockGetTask.mockResolvedValue({ taskId: 'dreamer-cand-pending-1-prompt' });
277
+ // PRI-435: getTask returns diagnostician task for diag-task-* lookups,
278
+ // and dreamer task for dreamer-cand-* lookups (testing idempotency).
279
+ mockGetTask.mockImplementation((taskId: string) => {
280
+ if (taskId.startsWith('diag-task-')) {
281
+ return Promise.resolve({
282
+ taskId,
283
+ taskKind: 'diagnostician',
284
+ status: 'completed',
285
+ diagnosticJson: JSON.stringify({ sourcePainId: `pain-${taskId}` }),
286
+ });
287
+ }
288
+ // Dreamer task already exists → idempotency check
289
+ return Promise.resolve({ taskId: 'dreamer-cand-pending-1-prompt' });
290
+ });
264
291
 
265
292
  await handleCandidateInternalizationBackfill({ workspace: WS, includePending: true, confirm: true, json: true });
266
293
 
@@ -396,11 +423,24 @@ describe('Commander wiring for backfill --include-pending', () => {
396
423
  mockGetCandidate.mockImplementation((id: string) =>
397
424
  Promise.resolve({
398
425
  candidateId: id,
426
+ taskId: `diag-task-${id}`,
399
427
  description: `candidate ${id}`,
400
428
  sourceRecommendationJson: JSON.stringify({ kind: 'principle', description: `candidate ${id}` }),
401
429
  }),
402
430
  );
403
- mockGetTask.mockResolvedValue(null);
431
+ // PRI-435: getTask returns diagnostician task with sourcePainId for diag-task-* lookups,
432
+ // null for dreamer task lookups (no existing dreamer task).
433
+ mockGetTask.mockImplementation((taskId: string) => {
434
+ if (taskId.startsWith('diag-task-')) {
435
+ return Promise.resolve({
436
+ taskId,
437
+ taskKind: 'diagnostician',
438
+ status: 'completed',
439
+ diagnosticJson: JSON.stringify({ sourcePainId: `pain-${taskId}` }),
440
+ });
441
+ }
442
+ return Promise.resolve(null);
443
+ });
404
444
  });
405
445
 
406
446
  afterEach(() => {
@@ -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
- mockStateManager.getTask.mockResolvedValue(null);
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
- mockStateManager.getTask.mockResolvedValue({
115
- taskId: 'dreamer-cand-001-prompt',
116
- taskKind: 'dreamer',
117
- status: 'pending',
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(() => {});