@principles/pd-cli 1.102.0 → 1.103.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/runtime-internalization-integrity.d.ts.map +1 -1
- package/dist/commands/runtime-internalization-integrity.js +40 -1
- package/dist/commands/runtime-internalization-integrity.js.map +1 -1
- package/dist/services/mainline-snapshot-assembler.d.ts +35 -0
- package/dist/services/mainline-snapshot-assembler.d.ts.map +1 -0
- package/dist/services/mainline-snapshot-assembler.js +399 -0
- package/dist/services/mainline-snapshot-assembler.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/runtime-internalization-integrity.ts +40 -1
- package/src/services/mainline-snapshot-assembler.ts +544 -0
- package/tests/commands/runtime-internalization-integrity.test.ts +37 -0
- package/tests/services/mainline-snapshot-assembler.test.ts +425 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MainlineSnapshot assembler tests (PRI-394)
|
|
3
|
+
*
|
|
4
|
+
* These tests seed real SQLite temp workspaces through RuntimeStateManager and
|
|
5
|
+
* the production DiagnosticianCommitter, then call `assembleMainlineSnapshot`
|
|
6
|
+
* and judge the result with core's `assertMainlineContract`.
|
|
7
|
+
*
|
|
8
|
+
* Hard rule: no hand-filled snapshots. Every test exercises the real read path.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
12
|
+
import * as fs from 'node:fs';
|
|
13
|
+
import * as path from 'node:path';
|
|
14
|
+
import * as os from 'node:os';
|
|
15
|
+
import {
|
|
16
|
+
RuntimeStateManager,
|
|
17
|
+
SqliteDiagnosticianCommitter,
|
|
18
|
+
serializePITaskMetadata,
|
|
19
|
+
assertMainlineContract,
|
|
20
|
+
EMPTY_CONTEXT_SENTINEL,
|
|
21
|
+
} from '@principles/core/runtime-v2';
|
|
22
|
+
import type {
|
|
23
|
+
DiagnosticianOutputV1,
|
|
24
|
+
RuntimeReadinessSnapshot,
|
|
25
|
+
} from '@principles/core/runtime-v2';
|
|
26
|
+
import { assembleMainlineSnapshot } from '../../src/services/mainline-snapshot-assembler.js';
|
|
27
|
+
|
|
28
|
+
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
29
|
+
|
|
30
|
+
function mkTmpDir(): string {
|
|
31
|
+
return fs.mkdtempSync(path.join(os.tmpdir(), 'pri-394-assembler-test-'));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function rmTmpDir(dir: string): void {
|
|
35
|
+
try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function healthyReadiness(): RuntimeReadinessSnapshot {
|
|
39
|
+
return {
|
|
40
|
+
configDoctorProfile: 'openclaw.default',
|
|
41
|
+
runtimeProbeProfile: 'openclaw.default',
|
|
42
|
+
configSource: '.pd/config.yaml',
|
|
43
|
+
probeConfigSource: '.pd/config.yaml',
|
|
44
|
+
diagnosticianReady: true,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function validDiagnosticianOutput(painId: string): DiagnosticianOutputV1 {
|
|
49
|
+
return {
|
|
50
|
+
valid: true,
|
|
51
|
+
diagnosisId: `diag-${painId}`,
|
|
52
|
+
summary: 'Repeated edits without reading instructions first',
|
|
53
|
+
rootCause: 'Assumption: agent assumes it knows the workspace conventions',
|
|
54
|
+
violatedPrinciples: [],
|
|
55
|
+
evidence: [{ sourceRef: 'session://test', note: 'edited README before reading' }],
|
|
56
|
+
recommendations: [{
|
|
57
|
+
kind: 'principle',
|
|
58
|
+
description: 'Read AGENTS.md and PLAN.md before editing protected files',
|
|
59
|
+
abstractedPrinciple: 'Read workspace instructions before protected edits',
|
|
60
|
+
}],
|
|
61
|
+
confidence: 0.9,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function diagnosticianDiagnosticJson(painId: string): string {
|
|
66
|
+
return JSON.stringify({
|
|
67
|
+
sourcePainId: painId,
|
|
68
|
+
reasonSummary: 'Repeated edits without reading instructions first',
|
|
69
|
+
source: 'test',
|
|
70
|
+
severity: 'high',
|
|
71
|
+
sessionIdHint: null,
|
|
72
|
+
agentIdHint: null,
|
|
73
|
+
provenance: 'automatic_hook',
|
|
74
|
+
provenanceReason: 'automatic hook',
|
|
75
|
+
evidence: [{ sourceRef: 'session://test', note: 'edited README before reading' }],
|
|
76
|
+
workspaceDir: null,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function seedDiagnosisTask(sm: RuntimeStateManager, painId: string): Promise<string> {
|
|
81
|
+
const taskId = `diagnostician-${painId}`;
|
|
82
|
+
await sm.createTask({
|
|
83
|
+
taskId,
|
|
84
|
+
taskKind: 'diagnostician',
|
|
85
|
+
inputRef: painId,
|
|
86
|
+
status: 'pending',
|
|
87
|
+
attemptCount: 0,
|
|
88
|
+
maxAttempts: 3,
|
|
89
|
+
diagnosticJson: diagnosticianDiagnosticJson(painId),
|
|
90
|
+
});
|
|
91
|
+
return taskId;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function runDiagnosisToSucceeded(
|
|
95
|
+
sm: RuntimeStateManager,
|
|
96
|
+
taskId: string,
|
|
97
|
+
output: DiagnosticianOutputV1,
|
|
98
|
+
): Promise<{ runId: string; artifactId: string }> {
|
|
99
|
+
const committer = new SqliteDiagnosticianCommitter(sm.connection);
|
|
100
|
+
await sm.acquireLease({ taskId, owner: 'test-owner', durationMs: 60_000, runtimeKind: 'openclaw' });
|
|
101
|
+
const runs = await sm.getRunsByTask(taskId);
|
|
102
|
+
const runId = runs[0]?.runId;
|
|
103
|
+
if (!runId) throw new Error(`No run created for task ${taskId}`);
|
|
104
|
+
const commitResult = await committer.commit({
|
|
105
|
+
runId,
|
|
106
|
+
taskId,
|
|
107
|
+
output,
|
|
108
|
+
idempotencyKey: `idem-${taskId}`,
|
|
109
|
+
});
|
|
110
|
+
await sm.markTaskSucceeded(taskId, `artifact://${commitResult.artifactId}`);
|
|
111
|
+
return { runId, artifactId: commitResult.artifactId };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function seedDreamerTask(
|
|
115
|
+
sm: RuntimeStateManager,
|
|
116
|
+
candidateId: string,
|
|
117
|
+
dependencyTaskId: string,
|
|
118
|
+
inputArtifactRef: string,
|
|
119
|
+
): Promise<string> {
|
|
120
|
+
const taskId = `dreamer-${candidateId}-prompt`;
|
|
121
|
+
const piEnvelope = JSON.parse(serializePITaskMetadata({
|
|
122
|
+
dependencyTaskIds: [dependencyTaskId],
|
|
123
|
+
channel: 'prompt',
|
|
124
|
+
timeoutMs: 300_000,
|
|
125
|
+
inputArtifactRefs: [{ artifactType: 'diagnostician_output', ref: inputArtifactRef }],
|
|
126
|
+
outputArtifactRefs: [],
|
|
127
|
+
}));
|
|
128
|
+
const diagnosticJson = JSON.stringify({ ...piEnvelope, candidateId });
|
|
129
|
+
await sm.createTask({
|
|
130
|
+
taskId,
|
|
131
|
+
taskKind: 'dreamer',
|
|
132
|
+
status: 'pending',
|
|
133
|
+
attemptCount: 0,
|
|
134
|
+
maxAttempts: 3,
|
|
135
|
+
diagnosticJson,
|
|
136
|
+
});
|
|
137
|
+
return taskId;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function seedPhilosopherTask(sm: RuntimeStateManager, dreamerTaskId: string): Promise<string> {
|
|
141
|
+
const taskId = `philosopher-${dreamerTaskId}`;
|
|
142
|
+
const diagnosticJson = serializePITaskMetadata({
|
|
143
|
+
dependencyTaskIds: [dreamerTaskId],
|
|
144
|
+
channel: 'prompt',
|
|
145
|
+
timeoutMs: 300_000,
|
|
146
|
+
inputArtifactRefs: [{ artifactType: 'principle', ref: `pi-artifact://${dreamerTaskId}` }],
|
|
147
|
+
outputArtifactRefs: [],
|
|
148
|
+
});
|
|
149
|
+
await sm.createTask({
|
|
150
|
+
taskId,
|
|
151
|
+
taskKind: 'philosopher',
|
|
152
|
+
status: 'pending',
|
|
153
|
+
attemptCount: 0,
|
|
154
|
+
maxAttempts: 3,
|
|
155
|
+
diagnosticJson,
|
|
156
|
+
});
|
|
157
|
+
return taskId;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function seedPhilosopherPiArtifact(
|
|
161
|
+
sm: RuntimeStateManager,
|
|
162
|
+
philosopherTaskId: string,
|
|
163
|
+
): Promise<void> {
|
|
164
|
+
await sm.acquireLease({ taskId: philosopherTaskId, owner: 'test-owner', durationMs: 60_000, runtimeKind: 'openclaw' });
|
|
165
|
+
const runs = await sm.getRunsByTask(philosopherTaskId);
|
|
166
|
+
const runId = runs[0]?.runId;
|
|
167
|
+
if (!runId) throw new Error(`No run created for task ${philosopherTaskId}`);
|
|
168
|
+
await sm.piArtifactStore.upsertArtifact({
|
|
169
|
+
artifactId: `pi-art-${philosopherTaskId}-${runId}`,
|
|
170
|
+
artifactKind: 'principle',
|
|
171
|
+
sourceTaskId: philosopherTaskId,
|
|
172
|
+
lineageArtifactIds: [],
|
|
173
|
+
validationStatus: 'pending',
|
|
174
|
+
contentJson: JSON.stringify({
|
|
175
|
+
principleCandidate: {
|
|
176
|
+
principleId: `prin-${philosopherTaskId}`,
|
|
177
|
+
title: 'Read workspace instructions first',
|
|
178
|
+
confidence: 0.9,
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
createdAt: new Date().toISOString(),
|
|
182
|
+
updatedAt: new Date().toISOString(),
|
|
183
|
+
});
|
|
184
|
+
await sm.markTaskSucceeded(philosopherTaskId, `pi-artifact://${philosopherTaskId}`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function seedFullChain(sm: RuntimeStateManager, painId: string): Promise<void> {
|
|
188
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
189
|
+
const { artifactId } = await runDiagnosisToSucceeded(sm, taskId, validDiagnosticianOutput(painId));
|
|
190
|
+
const candidates = await sm.getCandidatesByTaskId(taskId);
|
|
191
|
+
expect(candidates.length).toBeGreaterThan(0);
|
|
192
|
+
const candidate = candidates[0];
|
|
193
|
+
expect(candidate).toBeDefined();
|
|
194
|
+
|
|
195
|
+
const dreamerTaskId = await seedDreamerTask(sm, candidate!.candidateId, taskId, `artifact://${artifactId}`);
|
|
196
|
+
await sm.acquireLease({ taskId: dreamerTaskId, owner: 'test-owner', durationMs: 60_000, runtimeKind: 'openclaw' });
|
|
197
|
+
await sm.markTaskSucceeded(dreamerTaskId, `dreamer://${dreamerTaskId}`);
|
|
198
|
+
|
|
199
|
+
const philosopherTaskId = await seedPhilosopherTask(sm, dreamerTaskId);
|
|
200
|
+
await seedPhilosopherPiArtifact(sm, philosopherTaskId);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ── Tests ────────────────────────────────────────────────────────────────────
|
|
204
|
+
|
|
205
|
+
describe('assembleMainlineSnapshot', () => {
|
|
206
|
+
let workspaceDir = '';
|
|
207
|
+
let sm: RuntimeStateManager;
|
|
208
|
+
|
|
209
|
+
beforeEach(() => {
|
|
210
|
+
workspaceDir = mkTmpDir();
|
|
211
|
+
sm = new RuntimeStateManager({ workspaceDir });
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
afterEach(async () => {
|
|
215
|
+
try { await sm.close(); } catch { /* ignore */ }
|
|
216
|
+
rmTmpDir(workspaceDir);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('returns a snapshot with degraded readiness when no readiness snapshot is provided', async () => {
|
|
220
|
+
await sm.initialize();
|
|
221
|
+
const painId = 'pain-empty';
|
|
222
|
+
await seedDiagnosisTask(sm, painId);
|
|
223
|
+
|
|
224
|
+
const { snapshot, warnings } = await assembleMainlineSnapshot({ workspaceDir, painId });
|
|
225
|
+
|
|
226
|
+
expect(snapshot.readiness.diagnosticianReady).toBe(false);
|
|
227
|
+
expect(warnings.length).toBe(0);
|
|
228
|
+
const verdict = assertMainlineContract(snapshot);
|
|
229
|
+
expect(verdict.stages.some((s) => s.stage === 'diagnostician_readiness' && s.status === 'violation')).toBe(true);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('malformed artifact content_json does not crash; contract reports violation with reason + nextAction', async () => {
|
|
233
|
+
await sm.initialize();
|
|
234
|
+
const painId = 'pain-malformed-artifact';
|
|
235
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
236
|
+
await sm.acquireLease({ taskId, owner: 'test-owner', durationMs: 60_000, runtimeKind: 'openclaw' });
|
|
237
|
+
const runs = await sm.getRunsByTask(taskId);
|
|
238
|
+
const runId = runs[0]?.runId;
|
|
239
|
+
if (!runId) throw new Error(`No run created for task ${taskId}`);
|
|
240
|
+
await sm.markTaskSucceeded(taskId);
|
|
241
|
+
|
|
242
|
+
// Directly insert an artifact with malformed content_json (bypasses committer validation).
|
|
243
|
+
const artifactId = 'malformed-artifact-001';
|
|
244
|
+
sm.connection.getDb().prepare(
|
|
245
|
+
`INSERT INTO artifacts (artifact_id, run_id, task_id, artifact_kind, content_json, created_at)
|
|
246
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
247
|
+
).run(artifactId, runId, taskId, 'diagnostician_output', 'not-json{{{', new Date().toISOString());
|
|
248
|
+
|
|
249
|
+
const { snapshot, warnings } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
250
|
+
|
|
251
|
+
expect(warnings.some((w) => w.includes('malformed'))).toBe(true);
|
|
252
|
+
const verdict = assertMainlineContract(snapshot);
|
|
253
|
+
expect(verdict.overall).toBe('violation');
|
|
254
|
+
const violationStages = verdict.stages.filter((s) => s.status === 'violation');
|
|
255
|
+
expect(violationStages.length).toBeGreaterThan(0);
|
|
256
|
+
for (const s of violationStages) {
|
|
257
|
+
expect(s.reason.length).toBeGreaterThan(0);
|
|
258
|
+
expect(s.nextAction && s.nextAction.length > 0).toBe(true);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('missing succeeded run reports diagnosis_task violation', async () => {
|
|
263
|
+
await sm.initialize();
|
|
264
|
+
const painId = 'pain-no-run';
|
|
265
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
266
|
+
// Task marked succeeded but no run exists.
|
|
267
|
+
await sm.updateTask(taskId, { status: 'succeeded' });
|
|
268
|
+
|
|
269
|
+
const { snapshot } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
270
|
+
|
|
271
|
+
const verdict = assertMainlineContract(snapshot);
|
|
272
|
+
expect(verdict.overall).toBe('violation');
|
|
273
|
+
expect(verdict.stages.some((s) => s.stage === 'diagnosis_task' && s.status === 'violation')).toBe(true);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('missing dreamer task reports dreamer_task_lineage violation', async () => {
|
|
277
|
+
await sm.initialize();
|
|
278
|
+
const painId = 'pain-no-dreamer';
|
|
279
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
280
|
+
await runDiagnosisToSucceeded(sm, taskId, validDiagnosticianOutput(painId));
|
|
281
|
+
|
|
282
|
+
const { snapshot } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
283
|
+
|
|
284
|
+
const verdict = assertMainlineContract(snapshot);
|
|
285
|
+
expect(verdict.overall).toBe('violation');
|
|
286
|
+
expect(verdict.stages.some((s) => s.stage === 'dreamer_task_lineage' && s.status === 'violation')).toBe(true);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('empty dreamer context reports dreamer_context violation', async () => {
|
|
290
|
+
await sm.initialize();
|
|
291
|
+
const painId = 'pain-empty-context';
|
|
292
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
293
|
+
const { artifactId } = await runDiagnosisToSucceeded(sm, taskId, validDiagnosticianOutput(painId));
|
|
294
|
+
const candidates = await sm.getCandidatesByTaskId(taskId);
|
|
295
|
+
const candidate = candidates[0];
|
|
296
|
+
expect(candidate).toBeDefined();
|
|
297
|
+
|
|
298
|
+
// Sever the diagnosis task resultRef so the dreamer cannot build context,
|
|
299
|
+
// while keeping lineage intact so dreamer_task_lineage passes.
|
|
300
|
+
await sm.updateTask(taskId, { resultRef: null, outputRef: undefined });
|
|
301
|
+
|
|
302
|
+
const dreamerTaskId = `dreamer-${candidate!.candidateId}-prompt`;
|
|
303
|
+
const piEnvelope = JSON.parse(serializePITaskMetadata({
|
|
304
|
+
dependencyTaskIds: [taskId],
|
|
305
|
+
channel: 'prompt',
|
|
306
|
+
timeoutMs: 300_000,
|
|
307
|
+
inputArtifactRefs: [{ artifactType: 'diagnostician_output', ref: `artifact://${artifactId}` }],
|
|
308
|
+
outputArtifactRefs: [],
|
|
309
|
+
}));
|
|
310
|
+
const diagnosticJson = JSON.stringify({ ...piEnvelope, candidateId: candidate!.candidateId });
|
|
311
|
+
await sm.createTask({
|
|
312
|
+
taskId: dreamerTaskId,
|
|
313
|
+
taskKind: 'dreamer',
|
|
314
|
+
status: 'pending',
|
|
315
|
+
attemptCount: 0,
|
|
316
|
+
maxAttempts: 3,
|
|
317
|
+
diagnosticJson,
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
const { snapshot } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
321
|
+
|
|
322
|
+
expect(snapshot.chain.dreamerContext?.contextHash).toBe(EMPTY_CONTEXT_SENTINEL);
|
|
323
|
+
const verdict = assertMainlineContract(snapshot);
|
|
324
|
+
expect(verdict.stages.some((s) => s.stage === 'dreamer_context' && s.status === 'violation')).toBe(true);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('valid seeded temp SQLite product path generates snapshot that passes assertMainlineContract', async () => {
|
|
328
|
+
await sm.initialize();
|
|
329
|
+
const painId = 'pain-ok';
|
|
330
|
+
await seedFullChain(sm, painId);
|
|
331
|
+
|
|
332
|
+
const { snapshot, warnings } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
333
|
+
|
|
334
|
+
expect(warnings).toEqual([]);
|
|
335
|
+
expect(snapshot.chain.painId).toBe(painId);
|
|
336
|
+
expect(snapshot.chain.diagnosisTask).not.toBeNull();
|
|
337
|
+
expect(snapshot.chain.diagnosticianArtifact).not.toBeNull();
|
|
338
|
+
expect(snapshot.chain.candidate).not.toBeNull();
|
|
339
|
+
expect(snapshot.chain.dreamerTask).not.toBeNull();
|
|
340
|
+
expect(snapshot.chain.dreamerContext).not.toBeNull();
|
|
341
|
+
expect(snapshot.chain.successor).not.toBeNull();
|
|
342
|
+
expect(snapshot.chain.principle).not.toBeNull();
|
|
343
|
+
|
|
344
|
+
const verdict = assertMainlineContract(snapshot);
|
|
345
|
+
expect(verdict.overall).toBe('ok');
|
|
346
|
+
expect(verdict.stages.every((s) => s.status === 'ok')).toBe(true);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('consumed candidate without dreamer task reports auto_consumption violation', async () => {
|
|
350
|
+
await sm.initialize();
|
|
351
|
+
const painId = 'pain-consumed-orphan';
|
|
352
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
353
|
+
await runDiagnosisToSucceeded(sm, taskId, validDiagnosticianOutput(painId));
|
|
354
|
+
const candidates = await sm.getCandidatesByTaskId(taskId);
|
|
355
|
+
const candidate = candidates[0];
|
|
356
|
+
expect(candidate).toBeDefined();
|
|
357
|
+
await sm.updateCandidateStatus(candidate!.candidateId, { status: 'consumed' });
|
|
358
|
+
|
|
359
|
+
const { snapshot } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
360
|
+
|
|
361
|
+
expect(snapshot.consumedCandidatesMissingDreamer).toContain(candidate!.candidateId);
|
|
362
|
+
const verdict = assertMainlineContract(snapshot);
|
|
363
|
+
expect(verdict.stages.some((s) => s.stage === 'auto_consumption' && s.status === 'violation')).toBe(true);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it('malformed artifact row (missing content_json) does not crash; warnings include reason', async () => {
|
|
367
|
+
await sm.initialize();
|
|
368
|
+
const painId = 'pain-malformed-artifact-row';
|
|
369
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
370
|
+
await sm.acquireLease({ taskId, owner: 'test-owner', durationMs: 60_000, runtimeKind: 'openclaw' });
|
|
371
|
+
const runs = await sm.getRunsByTask(taskId);
|
|
372
|
+
const runId = runs[0]?.runId;
|
|
373
|
+
if (!runId) throw new Error(`No run created for task ${taskId}`);
|
|
374
|
+
await sm.markTaskSucceeded(taskId);
|
|
375
|
+
|
|
376
|
+
const artifactId = 'malformed-row-artifact';
|
|
377
|
+
sm.connection.getDb().prepare(
|
|
378
|
+
`INSERT INTO artifacts (artifact_id, run_id, task_id, artifact_kind, content_json, created_at)
|
|
379
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
380
|
+
).run(artifactId, runId, taskId, 'diagnostician_output', '', new Date().toISOString());
|
|
381
|
+
|
|
382
|
+
const { snapshot, warnings } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
383
|
+
|
|
384
|
+
expect(warnings.some((w) => w.includes('Malformed diagnostician artifact row'))).toBe(true);
|
|
385
|
+
expect(snapshot.chain.diagnosticianArtifact).toBeNull();
|
|
386
|
+
const verdict = assertMainlineContract(snapshot);
|
|
387
|
+
expect(verdict.overall).toBe('violation');
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it('malformed consumed candidate row (missing candidate_id) does not produce undefined orphan; warnings include reason', async () => {
|
|
391
|
+
await sm.initialize();
|
|
392
|
+
const painId = 'pain-consumed-malformed';
|
|
393
|
+
const taskId = await seedDiagnosisTask(sm, painId);
|
|
394
|
+
await runDiagnosisToSucceeded(sm, taskId, validDiagnosticianOutput(painId));
|
|
395
|
+
const candidates = await sm.getCandidatesByTaskId(taskId);
|
|
396
|
+
const candidate = candidates[0];
|
|
397
|
+
expect(candidate).toBeDefined();
|
|
398
|
+
await sm.updateCandidateStatus(candidate!.candidateId, { status: 'consumed' });
|
|
399
|
+
|
|
400
|
+
// Insert a row with empty candidate_id to exercise the malformed-row path.
|
|
401
|
+
const db = sm.connection.getDb();
|
|
402
|
+
db.prepare(
|
|
403
|
+
`INSERT INTO principle_candidates (candidate_id, task_id, artifact_id, source_run_id, title, description, idempotency_key, status, created_at, recommendation_kind)
|
|
404
|
+
VALUES ('', ?, ?, ?, 'malformed', '', ?, 'consumed', ?, 'principle')`,
|
|
405
|
+
).run(taskId, candidate!.artifactId, candidate!.sourceRunId, `idemp-${Date.now()}`, new Date().toISOString());
|
|
406
|
+
|
|
407
|
+
const { snapshot, warnings } = await assembleMainlineSnapshot({ workspaceDir, painId, readiness: healthyReadiness() });
|
|
408
|
+
|
|
409
|
+
expect(warnings.some((w) => w.includes('Malformed consumed candidate row'))).toBe(true);
|
|
410
|
+
for (const id of snapshot.consumedCandidatesMissingDreamer) {
|
|
411
|
+
expect(typeof id).toBe('string');
|
|
412
|
+
expect(id.length).toBeGreaterThan(0);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('resolves painId from latest diagnostician task when not provided', async () => {
|
|
417
|
+
await sm.initialize();
|
|
418
|
+
const painId = 'pain-latest';
|
|
419
|
+
await seedFullChain(sm, painId);
|
|
420
|
+
|
|
421
|
+
const { resolvedPainId } = await assembleMainlineSnapshot({ workspaceDir, readiness: healthyReadiness() });
|
|
422
|
+
|
|
423
|
+
expect(resolvedPainId).toBe(painId);
|
|
424
|
+
});
|
|
425
|
+
});
|