@principles/pd-cli 1.118.0 → 1.120.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/__tests__/legacy-cleanup.test.d.ts +18 -0
- package/dist/commands/__tests__/legacy-cleanup.test.d.ts.map +1 -0
- package/dist/commands/__tests__/legacy-cleanup.test.js +459 -0
- package/dist/commands/__tests__/legacy-cleanup.test.js.map +1 -0
- package/dist/commands/__tests__/rulecode-flag-wiring.test.d.ts +21 -0
- package/dist/commands/__tests__/rulecode-flag-wiring.test.d.ts.map +1 -0
- package/dist/commands/__tests__/rulecode-flag-wiring.test.js +179 -0
- package/dist/commands/__tests__/rulecode-flag-wiring.test.js.map +1 -0
- package/dist/commands/__tests__/rulecode-handler.test.d.ts +16 -0
- package/dist/commands/__tests__/rulecode-handler.test.d.ts.map +1 -0
- package/dist/commands/__tests__/rulecode-handler.test.js +285 -0
- package/dist/commands/__tests__/rulecode-handler.test.js.map +1 -0
- package/dist/commands/candidate.d.ts +1 -0
- package/dist/commands/candidate.d.ts.map +1 -1
- package/dist/commands/candidate.js +32 -6
- package/dist/commands/candidate.js.map +1 -1
- package/dist/commands/legacy-cleanup.d.ts +72 -6
- package/dist/commands/legacy-cleanup.d.ts.map +1 -1
- package/dist/commands/legacy-cleanup.js +243 -23
- package/dist/commands/legacy-cleanup.js.map +1 -1
- package/dist/commands/rulecode.d.ts +85 -0
- package/dist/commands/rulecode.d.ts.map +1 -0
- package/dist/commands/rulecode.js +356 -0
- package/dist/commands/rulecode.js.map +1 -0
- package/dist/commands/runtime-internalization-run-rulehost.d.ts.map +1 -1
- package/dist/commands/runtime-internalization-run-rulehost.js +4 -7
- package/dist/commands/runtime-internalization-run-rulehost.js.map +1 -1
- package/dist/index.js +30 -9
- package/dist/index.js.map +1 -1
- package/dist/services/rulehost-pipeline-runner.d.ts.map +1 -1
- package/dist/services/rulehost-pipeline-runner.js +31 -15
- package/dist/services/rulehost-pipeline-runner.js.map +1 -1
- package/package.json +1 -1
- package/scripts/llm-dogfood.ts +8 -12
- package/src/commands/__tests__/legacy-cleanup.test.ts +596 -0
- package/src/commands/__tests__/rulecode-flag-wiring.test.ts +230 -0
- package/src/commands/__tests__/rulecode-handler.test.ts +369 -0
- package/src/commands/candidate.ts +29 -7
- package/src/commands/legacy-cleanup.ts +335 -27
- package/src/commands/rulecode.ts +434 -0
- package/src/commands/runtime-internalization-run-rulehost.ts +3 -8
- package/src/index.ts +31 -9
- package/src/services/rulehost-pipeline-runner.ts +36 -18
- package/tests/commands/candidate-internalize-lineage.test.ts +44 -0
- package/tests/commands/cli-command-tree.test.ts +40 -0
- package/tests/commands/runtime.test.ts +9 -3
- package/tests/e2e/cross-package-acceptance.test.ts +1 -1
- package/tests/services/rulehost-pipeline-runner.test.ts +86 -2
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for `pd legacy cleanup` V1 Artificer artifact removal (PRI-439 Phase 6).
|
|
3
|
+
*
|
|
4
|
+
* Integration tests using a real temp workspace + RuntimeStateManager + SQLite DB.
|
|
5
|
+
* Verifies:
|
|
6
|
+
* - V1 artifacts (task_kind=artificer + artifact_kind=principle + no implementationCode) are identified
|
|
7
|
+
* - V2 artifacts (with implementationCode) are preserved
|
|
8
|
+
* - Non-artificer artifacts (dreamer/philosopher/scribe) are preserved
|
|
9
|
+
* - Non-principle artifacts are preserved
|
|
10
|
+
* - dry-run mode: no deletions occur
|
|
11
|
+
* - --apply mode: activations → approvals → pi_artifacts deleted in order
|
|
12
|
+
* - --json output: exactly one parseable JSON object (CLI gate rule 1)
|
|
13
|
+
* - --dry-run and --apply are mutually exclusive (CLI gate rule 4)
|
|
14
|
+
* - Failure paths include structured reason + nextAction (CLI gate rule 6)
|
|
15
|
+
* - Missing DB is handled gracefully (no V1 artifacts, no crash)
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
19
|
+
import { mkdtempSync, rmSync, mkdirSync, existsSync, writeFileSync } from 'node:fs';
|
|
20
|
+
import { join } from 'node:path';
|
|
21
|
+
import { tmpdir } from 'node:os';
|
|
22
|
+
import { RuntimeStateManager } from '@principles/core/runtime-v2';
|
|
23
|
+
import type { Database } from 'better-sqlite3';
|
|
24
|
+
import {
|
|
25
|
+
handleLegacyCleanup,
|
|
26
|
+
findV1ArtificerArtifacts,
|
|
27
|
+
isV1ArtificerArtifact,
|
|
28
|
+
} from '../legacy-cleanup.js';
|
|
29
|
+
|
|
30
|
+
// ── Test helpers ─────────────────────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
interface SeedArtifactInput {
|
|
33
|
+
artifactId: string;
|
|
34
|
+
artifactKind: string;
|
|
35
|
+
sourceTaskId: string;
|
|
36
|
+
contentJson: string;
|
|
37
|
+
validationStatus?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface SeedTaskInput {
|
|
41
|
+
taskId: string;
|
|
42
|
+
taskKind: string;
|
|
43
|
+
status?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface SeedApprovalInput {
|
|
47
|
+
approvalId: string;
|
|
48
|
+
artifactId: string;
|
|
49
|
+
channel: string;
|
|
50
|
+
riskLevel: string;
|
|
51
|
+
status?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface SeedActivationInput {
|
|
55
|
+
activationId: string;
|
|
56
|
+
idempotencyKey: string;
|
|
57
|
+
artifactId: string;
|
|
58
|
+
channel: string;
|
|
59
|
+
action: string;
|
|
60
|
+
targetRef: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function seedTask(db: Database, t: SeedTaskInput): void {
|
|
64
|
+
db.prepare(`
|
|
65
|
+
INSERT INTO tasks (task_id, task_kind, status, created_at, updated_at, attempt_count, max_attempts)
|
|
66
|
+
VALUES (?, ?, ?, ?, ?, 0, 3)
|
|
67
|
+
`).run(t.taskId, t.taskKind, t.status ?? 'succeeded', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function seedArtifact(db: Database, a: SeedArtifactInput): void {
|
|
71
|
+
db.prepare(`
|
|
72
|
+
INSERT INTO pi_artifacts (artifact_id, artifact_kind, source_task_id, lineage_artifact_ids, validation_status, content_json, created_at, updated_at)
|
|
73
|
+
VALUES (?, ?, ?, '[]', ?, ?, ?, ?)
|
|
74
|
+
`).run(a.artifactId, a.artifactKind, a.sourceTaskId, a.validationStatus ?? 'validated', a.contentJson, '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function seedApproval(db: Database, a: SeedApprovalInput): void {
|
|
78
|
+
db.prepare(`
|
|
79
|
+
INSERT INTO approvals (approval_id, artifact_id, channel, risk_level, status, requested_at)
|
|
80
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
81
|
+
`).run(a.approvalId, a.artifactId, a.channel, a.riskLevel, a.status ?? 'approved', '2026-01-01T00:00:00.000Z');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function seedActivation(db: Database, a: SeedActivationInput): void {
|
|
85
|
+
db.prepare(`
|
|
86
|
+
INSERT INTO activations (activation_id, idempotency_key, artifact_id, channel, action, target_ref, activated_at)
|
|
87
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
88
|
+
`).run(a.activationId, a.idempotencyKey, a.artifactId, a.channel, a.action, a.targetRef, '2026-01-01T00:00:00.000Z');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
92
|
+
function countTable(db: Database, table: 'pi_artifacts' | 'approvals' | 'activations', whereClause?: string, params?: unknown[]): number {
|
|
93
|
+
const sql = whereClause
|
|
94
|
+
? `SELECT COUNT(*) as cnt FROM ${table} WHERE ${whereClause}`
|
|
95
|
+
: `SELECT COUNT(*) as cnt FROM ${table}`;
|
|
96
|
+
const row = db.prepare(sql).get(...(params ?? [])) as { cnt: number };
|
|
97
|
+
return row.cnt;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function runHandler<T>(fn: () => Promise<T>): Promise<{ stdout: string; stderr: string; exitCode: number | undefined; result: T | undefined }> {
|
|
101
|
+
const stdoutChunks: string[] = [];
|
|
102
|
+
const stderrChunks: string[] = [];
|
|
103
|
+
const logSpy = vi.spyOn(console, 'log').mockImplementation((...args: unknown[]) => {
|
|
104
|
+
stdoutChunks.push(args.map(String).join(' '));
|
|
105
|
+
});
|
|
106
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
|
|
107
|
+
stderrChunks.push(args.map(String).join(' '));
|
|
108
|
+
});
|
|
109
|
+
process.exitCode = undefined;
|
|
110
|
+
let result: T | undefined;
|
|
111
|
+
try {
|
|
112
|
+
result = await fn();
|
|
113
|
+
} finally {
|
|
114
|
+
logSpy.mockRestore();
|
|
115
|
+
errorSpy.mockRestore();
|
|
116
|
+
}
|
|
117
|
+
const {exitCode} = process;
|
|
118
|
+
process.exitCode = undefined;
|
|
119
|
+
return { stdout: stdoutChunks.join(''), stderr: stderrChunks.join(''), exitCode, result };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ── Test fixtures ────────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
const V1_CONTENT_JSON = JSON.stringify({
|
|
125
|
+
taskId: 'task-v1',
|
|
126
|
+
sourceScribeArtifactId: 'scribe-001',
|
|
127
|
+
// NO implementationCode — this is the V1 plan-only format
|
|
128
|
+
goldenTraceCases: [],
|
|
129
|
+
affectedTools: ['Bash'],
|
|
130
|
+
implementationSummary: 'V1 plan-only output',
|
|
131
|
+
risks: [],
|
|
132
|
+
sourceTrace: { painIds: [], dreamerArtifactId: 'dream-001', philosopherArtifactId: 'phil-001', scribeArtifactId: 'scribe-001' },
|
|
133
|
+
generatedAt: '2026-01-01T00:00:00.000Z',
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const V2_CONTENT_JSON = JSON.stringify({
|
|
137
|
+
taskId: 'task-v2',
|
|
138
|
+
sourceScribeArtifactId: 'scribe-002',
|
|
139
|
+
implementationCode: 'function evaluate(input, helpers) { return { matched: true, decision: "allow", reasons: [] }; }',
|
|
140
|
+
goldenTraceCases: [],
|
|
141
|
+
affectedTools: ['Bash'],
|
|
142
|
+
implementationSummary: 'V2 with implementationCode',
|
|
143
|
+
risks: [],
|
|
144
|
+
sourceTrace: { painIds: [], dreamerArtifactId: 'dream-002', philosopherArtifactId: 'phil-002', scribeArtifactId: 'scribe-002' },
|
|
145
|
+
generatedAt: '2026-01-01T00:00:00.000Z',
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const DREAMER_CONTENT_JSON = JSON.stringify({
|
|
149
|
+
candidateIndex: 0,
|
|
150
|
+
badDecision: 'bad',
|
|
151
|
+
betterDecision: 'good',
|
|
152
|
+
rationale: 'test',
|
|
153
|
+
confidence: 0.9,
|
|
154
|
+
riskLevel: 'low',
|
|
155
|
+
strategicPerspective: 'defensive-programming',
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// ── Tests ────────────────────────────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
describe('isV1ArtificerArtifact (pure logic)', () => {
|
|
161
|
+
it('returns true when implementationCode is absent', () => {
|
|
162
|
+
const json = JSON.stringify({ taskId: 't1', sourceScribeArtifactId: 's1' });
|
|
163
|
+
expect(isV1ArtificerArtifact(json)).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('returns true when implementationCode is empty string', () => {
|
|
167
|
+
const json = JSON.stringify({ taskId: 't1', implementationCode: '' });
|
|
168
|
+
expect(isV1ArtificerArtifact(json)).toBe(true);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('returns true when implementationCode is whitespace-only', () => {
|
|
172
|
+
const json = JSON.stringify({ taskId: 't1', implementationCode: ' ' });
|
|
173
|
+
expect(isV1ArtificerArtifact(json)).toBe(true);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('returns false when implementationCode is a non-empty string', () => {
|
|
177
|
+
const json = JSON.stringify({ taskId: 't1', implementationCode: 'function evaluate() {}' });
|
|
178
|
+
expect(isV1ArtificerArtifact(json)).toBe(false);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('returns true when implementationCode is present but not a string (treated as V1)', () => {
|
|
182
|
+
const json = JSON.stringify({ taskId: 't1', implementationCode: 123 });
|
|
183
|
+
expect(isV1ArtificerArtifact(json)).toBe(true); // non-string = treated as V1 (missing valid code)
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('returns false for invalid JSON (skip, do not delete)', () => {
|
|
187
|
+
expect(isV1ArtificerArtifact('not valid json')).toBe(false);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('returns false for null JSON', () => {
|
|
191
|
+
expect(isV1ArtificerArtifact('null')).toBe(false);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe('findV1ArtificerArtifacts (integration with real DB)', () => {
|
|
196
|
+
let tempWorkspace: string;
|
|
197
|
+
let stateManager: RuntimeStateManager;
|
|
198
|
+
let db: Database;
|
|
199
|
+
|
|
200
|
+
beforeEach(async () => {
|
|
201
|
+
tempWorkspace = mkdtempSync(join(tmpdir(), 'pd-legacy-v1-'));
|
|
202
|
+
stateManager = new RuntimeStateManager({ workspaceDir: tempWorkspace });
|
|
203
|
+
await stateManager.initialize();
|
|
204
|
+
db = stateManager.connection.getDb();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
afterEach(async () => {
|
|
208
|
+
await stateManager.close();
|
|
209
|
+
rmSync(tempWorkspace, { recursive: true, force: true });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('identifies V1 artifacts (artificer + principle + no implementationCode)', async () => {
|
|
213
|
+
seedTask(db, { taskId: 'task-v1-a', taskKind: 'artificer' });
|
|
214
|
+
seedTask(db, { taskId: 'task-v1-b', taskKind: 'artificer' });
|
|
215
|
+
seedArtifact(db, { artifactId: 'art-v1-a', artifactKind: 'principle', sourceTaskId: 'task-v1-a', contentJson: V1_CONTENT_JSON });
|
|
216
|
+
seedArtifact(db, { artifactId: 'art-v1-b', artifactKind: 'principle', sourceTaskId: 'task-v1-b', contentJson: V1_CONTENT_JSON });
|
|
217
|
+
|
|
218
|
+
const targets = findV1ArtificerArtifacts(db);
|
|
219
|
+
expect(targets).toHaveLength(2);
|
|
220
|
+
expect(targets.map(t => t.artifactId).sort()).toEqual(['art-v1-a', 'art-v1-b']);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('skips V2 artifacts (with non-empty implementationCode)', async () => {
|
|
224
|
+
seedTask(db, { taskId: 'task-v2', taskKind: 'artificer' });
|
|
225
|
+
seedArtifact(db, { artifactId: 'art-v2', artifactKind: 'principle', sourceTaskId: 'task-v2', contentJson: V2_CONTENT_JSON });
|
|
226
|
+
|
|
227
|
+
const targets = findV1ArtificerArtifacts(db);
|
|
228
|
+
expect(targets).toHaveLength(0);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('skips non-artificer artifacts (dreamer/philosopher/scribe)', async () => {
|
|
232
|
+
seedTask(db, { taskId: 'task-dream', taskKind: 'dreamer' });
|
|
233
|
+
seedTask(db, { taskId: 'task-phil', taskKind: 'philosopher' });
|
|
234
|
+
seedTask(db, { taskId: 'task-scribe', taskKind: 'scribe' });
|
|
235
|
+
// These artifacts have no implementationCode but are NOT from artificer tasks
|
|
236
|
+
seedArtifact(db, { artifactId: 'art-dream', artifactKind: 'principle', sourceTaskId: 'task-dream', contentJson: DREAMER_CONTENT_JSON });
|
|
237
|
+
seedArtifact(db, { artifactId: 'art-phil', artifactKind: 'principle', sourceTaskId: 'task-phil', contentJson: '{}' });
|
|
238
|
+
seedArtifact(db, { artifactId: 'art-scribe', artifactKind: 'principle', sourceTaskId: 'task-scribe', contentJson: '{}' });
|
|
239
|
+
|
|
240
|
+
const targets = findV1ArtificerArtifacts(db);
|
|
241
|
+
expect(targets).toHaveLength(0);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('skips non-principle artifacts from artificer tasks', async () => {
|
|
245
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
246
|
+
seedArtifact(db, { artifactId: 'art-rule', artifactKind: 'rule', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
247
|
+
|
|
248
|
+
const targets = findV1ArtificerArtifacts(db);
|
|
249
|
+
expect(targets).toHaveLength(0);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('counts approvals and activations per V1 artifact', async () => {
|
|
253
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
254
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
255
|
+
seedApproval(db, { approvalId: 'appr-1', artifactId: 'art-v1', channel: 'prompt', riskLevel: 'low' });
|
|
256
|
+
seedApproval(db, { approvalId: 'appr-2', artifactId: 'art-v1', channel: 'code_tool_hook', riskLevel: 'high' });
|
|
257
|
+
seedActivation(db, { activationId: 'act-1', idempotencyKey: 'idem-1', artifactId: 'art-v1', channel: 'prompt', action: 'prompt', targetRef: 'P_001' });
|
|
258
|
+
|
|
259
|
+
const targets = findV1ArtificerArtifacts(db);
|
|
260
|
+
expect(targets).toHaveLength(1);
|
|
261
|
+
expect(targets[0].approvalCount).toBe(2);
|
|
262
|
+
expect(targets[0].activationCount).toBe(1);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('skips artifacts with corrupted content_json (does not delete)', async () => {
|
|
266
|
+
seedTask(db, { taskId: 'task-corrupt', taskKind: 'artificer' });
|
|
267
|
+
seedArtifact(db, { artifactId: 'art-corrupt', artifactKind: 'principle', sourceTaskId: 'task-corrupt', contentJson: 'not valid json {{{' });
|
|
268
|
+
|
|
269
|
+
const targets = findV1ArtificerArtifacts(db);
|
|
270
|
+
expect(targets).toHaveLength(0);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('returns empty array when DB has no pi_artifacts table', async () => {
|
|
274
|
+
// Drop the table to simulate a fresh/corrupt DB
|
|
275
|
+
db.exec('DROP TABLE pi_artifacts');
|
|
276
|
+
// Recreate it empty (so the query doesn't crash)
|
|
277
|
+
db.exec(`
|
|
278
|
+
CREATE TABLE pi_artifacts (
|
|
279
|
+
artifact_id TEXT PRIMARY KEY,
|
|
280
|
+
artifact_kind TEXT NOT NULL,
|
|
281
|
+
source_task_id TEXT NOT NULL,
|
|
282
|
+
source_principle_id TEXT,
|
|
283
|
+
source_rule_id TEXT,
|
|
284
|
+
lineage_artifact_ids TEXT NOT NULL DEFAULT '[]',
|
|
285
|
+
validation_status TEXT NOT NULL DEFAULT 'pending',
|
|
286
|
+
content_json TEXT NOT NULL,
|
|
287
|
+
created_at TEXT NOT NULL,
|
|
288
|
+
updated_at TEXT NOT NULL
|
|
289
|
+
);
|
|
290
|
+
`);
|
|
291
|
+
const targets = findV1ArtificerArtifacts(db);
|
|
292
|
+
expect(targets).toEqual([]);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
describe('handleLegacyCleanup — V1 artifact cleanup (integration)', () => {
|
|
297
|
+
let tempWorkspace: string;
|
|
298
|
+
|
|
299
|
+
beforeEach(() => {
|
|
300
|
+
tempWorkspace = mkdtempSync(join(tmpdir(), 'pd-legacy-handler-'));
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
afterEach(() => {
|
|
304
|
+
rmSync(tempWorkspace, { recursive: true, force: true });
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
async function seedAndClose(seedFn: (db: Database) => void): Promise<void> {
|
|
308
|
+
const sm = new RuntimeStateManager({ workspaceDir: tempWorkspace });
|
|
309
|
+
await sm.initialize();
|
|
310
|
+
const db = sm.connection.getDb();
|
|
311
|
+
seedFn(db);
|
|
312
|
+
await sm.close();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async function openDb(): Promise<{ sm: RuntimeStateManager; db: Database }> {
|
|
316
|
+
const sm = new RuntimeStateManager({ workspaceDir: tempWorkspace });
|
|
317
|
+
await sm.initialize();
|
|
318
|
+
return { sm, db: sm.connection.getDb() };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
it('dry-run mode: identifies V1 artifacts but does NOT delete anything', async () => {
|
|
322
|
+
await seedAndClose((db) => {
|
|
323
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
324
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
325
|
+
seedApproval(db, { approvalId: 'appr-1', artifactId: 'art-v1', channel: 'prompt', riskLevel: 'low' });
|
|
326
|
+
seedActivation(db, { activationId: 'act-1', idempotencyKey: 'idem-1', artifactId: 'art-v1', channel: 'prompt', action: 'prompt', targetRef: 'P_001' });
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
const { result } = await runHandler(() =>
|
|
330
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: true, json: true })
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
expect(result).toBeDefined();
|
|
334
|
+
if (!result) throw new Error('expected result');
|
|
335
|
+
expect(result.status).toBe('ok');
|
|
336
|
+
expect(result.mode).toBe('dry-run');
|
|
337
|
+
expect(result.v1Artifacts).toHaveLength(1);
|
|
338
|
+
expect(result.v1Artifacts[0].artifactId).toBe('art-v1');
|
|
339
|
+
expect(result.appliedV1Artifacts).toBe(0);
|
|
340
|
+
expect(result.appliedApprovals).toBe(0);
|
|
341
|
+
expect(result.appliedActivations).toBe(0);
|
|
342
|
+
|
|
343
|
+
// Verify nothing was deleted
|
|
344
|
+
const { sm, db } = await openDb();
|
|
345
|
+
expect(countTable(db, 'pi_artifacts')).toBe(1);
|
|
346
|
+
expect(countTable(db, 'approvals')).toBe(1);
|
|
347
|
+
expect(countTable(db, 'activations')).toBe(1);
|
|
348
|
+
await sm.close();
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it('--apply mode: deletes activations → approvals → pi_artifacts for V1 artifacts', async () => {
|
|
352
|
+
await seedAndClose((db) => {
|
|
353
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
354
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
355
|
+
seedApproval(db, { approvalId: 'appr-1', artifactId: 'art-v1', channel: 'prompt', riskLevel: 'low' });
|
|
356
|
+
seedApproval(db, { approvalId: 'appr-2', artifactId: 'art-v1', channel: 'code_tool_hook', riskLevel: 'high' });
|
|
357
|
+
seedActivation(db, { activationId: 'act-1', idempotencyKey: 'idem-1', artifactId: 'art-v1', channel: 'prompt', action: 'prompt', targetRef: 'P_001' });
|
|
358
|
+
seedActivation(db, { activationId: 'act-2', idempotencyKey: 'idem-2', artifactId: 'art-v1', channel: 'code_tool_hook', action: 'block', targetRef: 'rule-001' });
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
const { result } = await runHandler(() =>
|
|
362
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: false, json: true })
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
expect(result).toBeDefined();
|
|
366
|
+
if (!result) throw new Error('expected result');
|
|
367
|
+
expect(result.status).toBe('ok');
|
|
368
|
+
expect(result.mode).toBe('apply');
|
|
369
|
+
expect(result.appliedV1Artifacts).toBe(1);
|
|
370
|
+
expect(result.appliedApprovals).toBe(2);
|
|
371
|
+
expect(result.appliedActivations).toBe(2);
|
|
372
|
+
|
|
373
|
+
// Verify everything was deleted
|
|
374
|
+
const { sm, db } = await openDb();
|
|
375
|
+
expect(countTable(db, 'pi_artifacts')).toBe(0);
|
|
376
|
+
expect(countTable(db, 'approvals')).toBe(0);
|
|
377
|
+
expect(countTable(db, 'activations')).toBe(0);
|
|
378
|
+
await sm.close();
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('--apply mode: preserves V2 artifacts (with implementationCode)', async () => {
|
|
382
|
+
await seedAndClose((db) => {
|
|
383
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
384
|
+
seedTask(db, { taskId: 'task-v2', taskKind: 'artificer' });
|
|
385
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
386
|
+
seedArtifact(db, { artifactId: 'art-v2', artifactKind: 'principle', sourceTaskId: 'task-v2', contentJson: V2_CONTENT_JSON });
|
|
387
|
+
seedApproval(db, { approvalId: 'appr-v1', artifactId: 'art-v1', channel: 'prompt', riskLevel: 'low' });
|
|
388
|
+
seedApproval(db, { approvalId: 'appr-v2', artifactId: 'art-v2', channel: 'prompt', riskLevel: 'low' });
|
|
389
|
+
seedActivation(db, { activationId: 'act-v1', idempotencyKey: 'idem-v1', artifactId: 'art-v1', channel: 'prompt', action: 'prompt', targetRef: 'P_001' });
|
|
390
|
+
seedActivation(db, { activationId: 'act-v2', idempotencyKey: 'idem-v2', artifactId: 'art-v2', channel: 'prompt', action: 'prompt', targetRef: 'P_002' });
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const { result } = await runHandler(() =>
|
|
394
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: false, json: true })
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
expect(result).toBeDefined();
|
|
398
|
+
if (!result) throw new Error('expected result');
|
|
399
|
+
expect(result.appliedV1Artifacts).toBe(1);
|
|
400
|
+
|
|
401
|
+
const { sm, db } = await openDb();
|
|
402
|
+
// V1 deleted, V2 preserved
|
|
403
|
+
expect(countTable(db, 'pi_artifacts')).toBe(1);
|
|
404
|
+
expect(countTable(db, 'approvals')).toBe(1);
|
|
405
|
+
expect(countTable(db, 'activations')).toBe(1);
|
|
406
|
+
const remaining = db.prepare('SELECT artifact_id FROM pi_artifacts').get() as { artifact_id: string };
|
|
407
|
+
expect(remaining.artifact_id).toBe('art-v2');
|
|
408
|
+
await sm.close();
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it('--apply mode: preserves non-artificer artifacts (dreamer/philosopher/scribe)', async () => {
|
|
412
|
+
await seedAndClose((db) => {
|
|
413
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
414
|
+
seedTask(db, { taskId: 'task-dream', taskKind: 'dreamer' });
|
|
415
|
+
seedTask(db, { taskId: 'task-phil', taskKind: 'philosopher' });
|
|
416
|
+
seedTask(db, { taskId: 'task-scribe', taskKind: 'scribe' });
|
|
417
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
418
|
+
seedArtifact(db, { artifactId: 'art-dream', artifactKind: 'principle', sourceTaskId: 'task-dream', contentJson: DREAMER_CONTENT_JSON });
|
|
419
|
+
seedArtifact(db, { artifactId: 'art-phil', artifactKind: 'principle', sourceTaskId: 'task-phil', contentJson: '{}' });
|
|
420
|
+
seedArtifact(db, { artifactId: 'art-scribe', artifactKind: 'principle', sourceTaskId: 'task-scribe', contentJson: '{}' });
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const { result } = await runHandler(() =>
|
|
424
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: false, json: true })
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
expect(result).toBeDefined();
|
|
428
|
+
if (!result) throw new Error('expected result');
|
|
429
|
+
expect(result.appliedV1Artifacts).toBe(1);
|
|
430
|
+
|
|
431
|
+
const { sm, db } = await openDb();
|
|
432
|
+
expect(countTable(db, 'pi_artifacts')).toBe(3); // dreamer, philosopher, scribe preserved
|
|
433
|
+
const remaining = db.prepare('SELECT artifact_id FROM pi_artifacts').all() as { artifact_id: string }[];
|
|
434
|
+
expect(remaining.map(r => r.artifact_id).sort()).toEqual(['art-dream', 'art-phil', 'art-scribe']);
|
|
435
|
+
await sm.close();
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('handles missing DB gracefully (no crash, no V1 artifacts)', async () => {
|
|
439
|
+
// No DB created — just a temp dir with no .pd/state.db
|
|
440
|
+
const { result, exitCode } = await runHandler(() =>
|
|
441
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: true, json: true })
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
expect(result).toBeDefined();
|
|
445
|
+
if (!result) throw new Error('expected result');
|
|
446
|
+
expect(result.status).toBe('ok');
|
|
447
|
+
expect(result.v1Artifacts).toEqual([]);
|
|
448
|
+
expect(result.appliedV1Artifacts).toBe(0);
|
|
449
|
+
expect(exitCode).toBeUndefined();
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it('--json output: exactly one parseable JSON object on stdout (CLI gate rule 1)', async () => {
|
|
453
|
+
await seedAndClose((db) => {
|
|
454
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
455
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
const { stdout } = await runHandler(() =>
|
|
459
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: true, json: true })
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
const parsed = JSON.parse(stdout);
|
|
463
|
+
expect(parsed).toHaveProperty('status');
|
|
464
|
+
expect(parsed).toHaveProperty('mode');
|
|
465
|
+
expect(parsed).toHaveProperty('v1Artifacts');
|
|
466
|
+
expect(parsed).toHaveProperty('appliedV1Artifacts');
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('text mode output: contains key info (not JSON)', async () => {
|
|
470
|
+
await seedAndClose((db) => {
|
|
471
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
472
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
const { stdout } = await runHandler(() =>
|
|
476
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: true, json: false })
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
expect(stdout).toContain('art-v1');
|
|
480
|
+
expect(stdout).toContain('DRY RUN');
|
|
481
|
+
// Should NOT be JSON in text mode
|
|
482
|
+
expect(stdout.startsWith('{')).toBe(false);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it('dry-run and apply are mutually exclusive (CLI gate rule 4)', async () => {
|
|
486
|
+
// This test verifies the handler itself rejects both flags.
|
|
487
|
+
// The CLI registration in index.ts enforces mutual exclusivity at the parser level.
|
|
488
|
+
const { exitCode, stdout } = await runHandler(() =>
|
|
489
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: true, apply: true, json: true })
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
expect(exitCode).toBe(1);
|
|
493
|
+
// JSON mode outputs error to stdout (CLI gate rule 1: JSON mode is strict)
|
|
494
|
+
expect(stdout).toContain('mutually exclusive');
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it('multiple V1 artifacts: all deleted in --apply mode', async () => {
|
|
498
|
+
await seedAndClose((db) => {
|
|
499
|
+
seedTask(db, { taskId: 'task-v1-a', taskKind: 'artificer' });
|
|
500
|
+
seedTask(db, { taskId: 'task-v1-b', taskKind: 'artificer' });
|
|
501
|
+
seedTask(db, { taskId: 'task-v1-c', taskKind: 'artificer' });
|
|
502
|
+
seedArtifact(db, { artifactId: 'art-v1-a', artifactKind: 'principle', sourceTaskId: 'task-v1-a', contentJson: V1_CONTENT_JSON });
|
|
503
|
+
seedArtifact(db, { artifactId: 'art-v1-b', artifactKind: 'principle', sourceTaskId: 'task-v1-b', contentJson: V1_CONTENT_JSON });
|
|
504
|
+
seedArtifact(db, { artifactId: 'art-v1-c', artifactKind: 'principle', sourceTaskId: 'task-v1-c', contentJson: V1_CONTENT_JSON });
|
|
505
|
+
seedApproval(db, { approvalId: 'appr-a', artifactId: 'art-v1-a', channel: 'prompt', riskLevel: 'low' });
|
|
506
|
+
seedApproval(db, { approvalId: 'appr-b', artifactId: 'art-v1-b', channel: 'prompt', riskLevel: 'low' });
|
|
507
|
+
seedActivation(db, { activationId: 'act-a', idempotencyKey: 'idem-a', artifactId: 'art-v1-a', channel: 'prompt', action: 'prompt', targetRef: 'P_001' });
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
const { result } = await runHandler(() =>
|
|
511
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: false, json: true })
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
expect(result).toBeDefined();
|
|
515
|
+
if (!result) throw new Error('expected result');
|
|
516
|
+
expect(result.appliedV1Artifacts).toBe(3);
|
|
517
|
+
expect(result.appliedApprovals).toBe(2);
|
|
518
|
+
expect(result.appliedActivations).toBe(1);
|
|
519
|
+
|
|
520
|
+
const { sm, db } = await openDb();
|
|
521
|
+
expect(countTable(db, 'pi_artifacts')).toBe(0);
|
|
522
|
+
expect(countTable(db, 'approvals')).toBe(0);
|
|
523
|
+
expect(countTable(db, 'activations')).toBe(0);
|
|
524
|
+
await sm.close();
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it('preserves pain artifacts (artifact_kind != principle)', async () => {
|
|
528
|
+
await seedAndClose((db) => {
|
|
529
|
+
seedTask(db, { taskId: 'task-v1', taskKind: 'artificer' });
|
|
530
|
+
seedTask(db, { taskId: 'task-pain', taskKind: 'pain' });
|
|
531
|
+
seedArtifact(db, { artifactId: 'art-v1', artifactKind: 'principle', sourceTaskId: 'task-v1', contentJson: V1_CONTENT_JSON });
|
|
532
|
+
seedArtifact(db, { artifactId: 'art-pain', artifactKind: 'pain', sourceTaskId: 'task-pain', contentJson: '{}' });
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
const { result } = await runHandler(() =>
|
|
536
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: false, json: true })
|
|
537
|
+
);
|
|
538
|
+
|
|
539
|
+
expect(result).toBeDefined();
|
|
540
|
+
if (!result) throw new Error('expected result');
|
|
541
|
+
expect(result.appliedV1Artifacts).toBe(1);
|
|
542
|
+
|
|
543
|
+
const { sm, db } = await openDb();
|
|
544
|
+
expect(countTable(db, 'pi_artifacts')).toBe(1);
|
|
545
|
+
const remaining = db.prepare('SELECT artifact_id, artifact_kind FROM pi_artifacts').get() as { artifact_id: string; artifact_kind: string };
|
|
546
|
+
expect(remaining.artifact_id).toBe('art-pain');
|
|
547
|
+
expect(remaining.artifact_kind).toBe('pain');
|
|
548
|
+
await sm.close();
|
|
549
|
+
});
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
describe('handleLegacyCleanup — file cleanup (existing functionality preserved)', () => {
|
|
553
|
+
let tempWorkspace: string;
|
|
554
|
+
|
|
555
|
+
beforeEach(() => {
|
|
556
|
+
tempWorkspace = mkdtempSync(join(tmpdir(), 'pd-legacy-files-'));
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
afterEach(() => {
|
|
560
|
+
rmSync(tempWorkspace, { recursive: true, force: true });
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
it('dry-run: still detects empathy-optimizer files', async () => {
|
|
564
|
+
const stateDir = join(tempWorkspace, '.state');
|
|
565
|
+
mkdirSync(stateDir, { recursive: true });
|
|
566
|
+
writeFileSync(
|
|
567
|
+
join(stateDir, 'diagnostician_tasks.json'),
|
|
568
|
+
JSON.stringify([{ id: 'diag-1' }])
|
|
569
|
+
);
|
|
570
|
+
|
|
571
|
+
const { result } = await runHandler(() =>
|
|
572
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: true, json: true })
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
expect(result).toBeDefined();
|
|
576
|
+
if (!result) throw new Error('expected result');
|
|
577
|
+
expect(result.fileTargets.length).toBeGreaterThan(0);
|
|
578
|
+
expect(result.fileTargets.some(t => t.path.includes('diagnostician_tasks.json'))).toBe(true);
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
it('--apply: archives empathy-optimizer files', async () => {
|
|
582
|
+
const stateDir = join(tempWorkspace, '.state');
|
|
583
|
+
mkdirSync(stateDir, { recursive: true });
|
|
584
|
+
const diagPath = join(stateDir, 'diagnostician_tasks.json');
|
|
585
|
+
writeFileSync(diagPath, JSON.stringify([{ id: 'diag-1' }]));
|
|
586
|
+
|
|
587
|
+
const { result } = await runHandler(() =>
|
|
588
|
+
handleLegacyCleanup({ workspacePath: tempWorkspace, dryRun: false, json: true })
|
|
589
|
+
);
|
|
590
|
+
|
|
591
|
+
expect(result).toBeDefined();
|
|
592
|
+
if (!result) throw new Error('expected result');
|
|
593
|
+
expect(result.appliedFiles).toBeGreaterThan(0);
|
|
594
|
+
expect(existsSync(diagPath)).toBe(false);
|
|
595
|
+
});
|
|
596
|
+
});
|