@principles/pd-cli 1.119.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.
Files changed (35) hide show
  1. package/dist/commands/__tests__/legacy-cleanup.test.d.ts +18 -0
  2. package/dist/commands/__tests__/legacy-cleanup.test.d.ts.map +1 -0
  3. package/dist/commands/__tests__/legacy-cleanup.test.js +459 -0
  4. package/dist/commands/__tests__/legacy-cleanup.test.js.map +1 -0
  5. package/dist/commands/__tests__/rulecode-flag-wiring.test.d.ts +21 -0
  6. package/dist/commands/__tests__/rulecode-flag-wiring.test.d.ts.map +1 -0
  7. package/dist/commands/__tests__/rulecode-flag-wiring.test.js +179 -0
  8. package/dist/commands/__tests__/rulecode-flag-wiring.test.js.map +1 -0
  9. package/dist/commands/__tests__/rulecode-handler.test.d.ts +16 -0
  10. package/dist/commands/__tests__/rulecode-handler.test.d.ts.map +1 -0
  11. package/dist/commands/__tests__/rulecode-handler.test.js +285 -0
  12. package/dist/commands/__tests__/rulecode-handler.test.js.map +1 -0
  13. package/dist/commands/legacy-cleanup.d.ts +72 -6
  14. package/dist/commands/legacy-cleanup.d.ts.map +1 -1
  15. package/dist/commands/legacy-cleanup.js +243 -23
  16. package/dist/commands/legacy-cleanup.js.map +1 -1
  17. package/dist/commands/rulecode.d.ts +85 -0
  18. package/dist/commands/rulecode.d.ts.map +1 -0
  19. package/dist/commands/rulecode.js +356 -0
  20. package/dist/commands/rulecode.js.map +1 -0
  21. package/dist/commands/runtime-internalization-run-rulehost.d.ts.map +1 -1
  22. package/dist/commands/runtime-internalization-run-rulehost.js +4 -7
  23. package/dist/commands/runtime-internalization-run-rulehost.js.map +1 -1
  24. package/dist/index.js +30 -9
  25. package/dist/index.js.map +1 -1
  26. package/package.json +1 -1
  27. package/scripts/llm-dogfood.ts +8 -12
  28. package/src/commands/__tests__/legacy-cleanup.test.ts +596 -0
  29. package/src/commands/__tests__/rulecode-flag-wiring.test.ts +230 -0
  30. package/src/commands/__tests__/rulecode-handler.test.ts +369 -0
  31. package/src/commands/legacy-cleanup.ts +335 -27
  32. package/src/commands/rulecode.ts +434 -0
  33. package/src/commands/runtime-internalization-run-rulehost.ts +3 -8
  34. package/src/index.ts +31 -9
  35. package/tests/commands/cli-command-tree.test.ts +40 -0
@@ -9,14 +9,26 @@
9
9
  * - .state/.diagnostician_report_* (archives)
10
10
  * - ~/.openclaw/cron/jobs.json (removes pd-empathy-optimizer cron jobs)
11
11
  *
12
+ * PRI-439 Phase 6: V1 Artificer artifact cleanup.
13
+ * - Identifies V1 artifacts: task_kind=artificer + artifact_kind=principle + no implementationCode
14
+ * - Preserves V2 artifacts (with implementationCode), pain, Dreamer, Philosopher, Scribe
15
+ * - --apply deletes activations → approvals → pi_artifacts (in that order, no cascade)
16
+ * - No V1 runtime reader — V1 artifacts are removed, not interpreted
17
+ *
12
18
  * Usage:
13
- * pd legacy cleanup --workspace <path> --dry-run
14
- * pd legacy cleanup --workspace <path> --apply
19
+ * pd legacy cleanup --workspace <path> # dry-run (default)
20
+ * pd legacy cleanup --workspace <path> --dry-run # explicit dry-run
21
+ * pd legacy cleanup --workspace <path> --apply # apply cleanup
22
+ * pd legacy cleanup --workspace <path> --apply --json # apply with JSON output
15
23
  */
16
24
 
17
25
  import * as fs from 'fs';
18
26
  import * as path from 'path';
19
27
  import * as os from 'os';
28
+ import type { Database } from 'better-sqlite3';
29
+ import { RuntimeStateManager } from '@principles/core/runtime-v2';
30
+
31
+ // ── Types ────────────────────────────────────────────────────────────────────
20
32
 
21
33
  interface CleanupTarget {
22
34
  path: string;
@@ -41,6 +53,193 @@ interface TaskRecord {
41
53
  [key: string]: unknown;
42
54
  }
43
55
 
56
+ /**
57
+ * A V1 Artificer artifact identified for cleanup.
58
+ * - artifactId: pi_artifacts.artifact_id
59
+ * - sourceTaskId: pi_artifacts.source_task_id (references tasks.task_id with task_kind='artificer')
60
+ * - approvalCount: number of approvals referencing this artifact
61
+ * - activationCount: number of activations referencing this artifact
62
+ */
63
+ export interface V1ArtifactTarget {
64
+ artifactId: string;
65
+ sourceTaskId: string;
66
+ createdAt: string;
67
+ approvalCount: number;
68
+ activationCount: number;
69
+ }
70
+
71
+ export interface LegacyCleanupOptions {
72
+ workspacePath: string;
73
+ /** Dry-run mode (default). Mutually exclusive with `apply`. */
74
+ dryRun?: boolean;
75
+ /** Apply mode. Mutually exclusive with `dryRun`. */
76
+ apply?: boolean;
77
+ /** Output raw JSON */
78
+ json?: boolean;
79
+ }
80
+
81
+ export interface LegacyCleanupResult {
82
+ status: 'ok' | 'partial' | 'failed';
83
+ mode: 'dry-run' | 'apply';
84
+ fileTargets: CleanupTarget[];
85
+ v1Artifacts: V1ArtifactTarget[];
86
+ appliedFiles: number;
87
+ appliedV1Artifacts: number;
88
+ appliedApprovals: number;
89
+ appliedActivations: number;
90
+ errors: string[];
91
+ reason?: string;
92
+ nextAction?: string;
93
+ }
94
+
95
+ // ── Pure logic: V1 artifact identification ──────────────────────────────────
96
+
97
+ /**
98
+ * Returns true if the parsed content_json represents a V1 Artificer output
99
+ * (i.e., no valid non-empty `implementationCode` string).
100
+ *
101
+ * V1 = plan-only acceptance path (removed in PRI-439).
102
+ * V2 = unified ArtificerRuleOutput with mandatory implementationCode.
103
+ *
104
+ * Returns false for:
105
+ * - V2 artifacts (with non-empty implementationCode string)
106
+ * - Invalid JSON (skip — do not delete corrupted artifacts)
107
+ * - null/non-object JSON
108
+ */
109
+ export function isV1ArtificerArtifact(contentJson: string): boolean {
110
+ let parsed: unknown;
111
+ try {
112
+ parsed = JSON.parse(contentJson);
113
+ } catch {
114
+ return false; // invalid JSON — skip, do not delete
115
+ }
116
+
117
+ if (typeof parsed !== 'object' || parsed === null) {
118
+ return false; // null or non-object — skip
119
+ }
120
+
121
+ // V1 = no implementationCode field OR implementationCode is not a non-empty string
122
+ if (!Object.hasOwn(parsed, 'implementationCode')) {
123
+ return true; // V1: field absent
124
+ }
125
+
126
+ const code = (parsed as Record<string, unknown>).implementationCode;
127
+ if (typeof code !== 'string') {
128
+ return true; // V1: field present but not a string (e.g., null, number)
129
+ }
130
+ if (code.trim() === '') {
131
+ return true; // V1: empty or whitespace-only
132
+ }
133
+
134
+ return false; // V2: has non-empty implementationCode string
135
+ }
136
+
137
+ // ── DB integration: find V1 Artificer artifacts ─────────────────────────────
138
+
139
+ interface V1ArtifactRow {
140
+ artifact_id: string;
141
+ source_task_id: string;
142
+ created_at: string;
143
+ content_json: string;
144
+ approval_count: number;
145
+ activation_count: number;
146
+ }
147
+
148
+ /**
149
+ * Queries the SQLite DB for V1 Artificer artifacts.
150
+ *
151
+ * V1 criteria:
152
+ * - pi_artifacts.artifact_kind = 'principle'
153
+ * - tasks.task_kind = 'artificer' (joined via source_task_id)
154
+ * - content_json lacks a non-empty implementationCode (checked in JS via isV1ArtificerArtifact)
155
+ *
156
+ * Returns the list of V1 artifacts with their approval/activation counts.
157
+ */
158
+ export function findV1ArtificerArtifacts(db: Database): V1ArtifactTarget[] {
159
+ // Check if pi_artifacts table exists (graceful degradation for fresh workspaces)
160
+ const tableExists = db.prepare(
161
+ "SELECT name FROM sqlite_master WHERE type='table' AND name='pi_artifacts'"
162
+ ).get() as { name: string } | undefined;
163
+ if (!tableExists) {
164
+ return [];
165
+ }
166
+
167
+ const rows = db.prepare(`
168
+ SELECT
169
+ a.artifact_id,
170
+ a.source_task_id,
171
+ a.created_at,
172
+ a.content_json,
173
+ (SELECT COUNT(*) FROM approvals p WHERE p.artifact_id = a.artifact_id) AS approval_count,
174
+ (SELECT COUNT(*) FROM activations act WHERE act.artifact_id = a.artifact_id) AS activation_count
175
+ FROM pi_artifacts a
176
+ JOIN tasks t ON a.source_task_id = t.task_id
177
+ WHERE a.artifact_kind = 'principle' AND t.task_kind = 'artificer'
178
+ `).all() as V1ArtifactRow[];
179
+
180
+ const targets: V1ArtifactTarget[] = [];
181
+ for (const row of rows) {
182
+ if (isV1ArtificerArtifact(row.content_json)) {
183
+ targets.push({
184
+ artifactId: row.artifact_id,
185
+ sourceTaskId: row.source_task_id,
186
+ createdAt: row.created_at,
187
+ approvalCount: row.approval_count,
188
+ activationCount: row.activation_count,
189
+ });
190
+ }
191
+ }
192
+ return targets;
193
+ }
194
+
195
+ /**
196
+ * Applies V1 artifact cleanup: deletes activations → approvals → pi_artifacts.
197
+ * Order matters: delete dependents first to avoid orphan references (no FK cascade).
198
+ *
199
+ * Wrapped in a SQLite transaction so the 3 deletions are atomic — if any fails,
200
+ * all roll back and no orphan rows are left behind.
201
+ *
202
+ * Returns counts of deleted rows per table.
203
+ */
204
+ function applyV1ArtifactCleanup(
205
+ db: Database,
206
+ artifactIds: string[]
207
+ ): { deletedArtifacts: number; deletedApprovals: number; deletedActivations: number } {
208
+ if (artifactIds.length === 0) {
209
+ return { deletedArtifacts: 0, deletedApprovals: 0, deletedActivations: 0 };
210
+ }
211
+
212
+ const placeholders = artifactIds.map(() => '?').join(', ');
213
+
214
+ // Transaction ensures atomicity: all 3 deletions succeed or all roll back
215
+ const cleanupTransaction = db.transaction(() => {
216
+ // 1. Delete activations first (dependents)
217
+ const delActivations = db.prepare(
218
+ `DELETE FROM activations WHERE artifact_id IN (${placeholders})`
219
+ ).run(...artifactIds);
220
+
221
+ // 2. Delete approvals (dependents)
222
+ const delApprovals = db.prepare(
223
+ `DELETE FROM approvals WHERE artifact_id IN (${placeholders})`
224
+ ).run(...artifactIds);
225
+
226
+ // 3. Delete pi_artifacts (principal)
227
+ const delArtifacts = db.prepare(
228
+ `DELETE FROM pi_artifacts WHERE artifact_id IN (${placeholders})`
229
+ ).run(...artifactIds);
230
+
231
+ return {
232
+ deletedArtifacts: delArtifacts.changes,
233
+ deletedApprovals: delApprovals.changes,
234
+ deletedActivations: delActivations.changes,
235
+ };
236
+ });
237
+
238
+ return cleanupTransaction();
239
+ }
240
+
241
+ // ── File-system cleanup (existing functionality) ────────────────────────────
242
+
44
243
  function glob(pattern: string): string[] {
45
244
  const results: string[] = [];
46
245
  const baseDir = path.dirname(pattern);
@@ -160,28 +359,46 @@ function findLegacyTargets(workspacePath: string): CleanupTarget[] {
160
359
  return targets;
161
360
  }
162
361
 
163
- export async function handleLegacyCleanup(
164
- workspacePath: string,
165
- dryRun: boolean
166
- ): Promise<{ targets: CleanupTarget[]; applied: number }> {
167
- const targets = findLegacyTargets(workspacePath);
168
- let applied = 0;
362
+ // ── Main handler ────────────────────────────────────────────────────────────
169
363
 
170
- if (dryRun) {
171
- console.log(`\n=== DRY RUN: Would process ${targets.length} target(s) ===`);
172
- for (const t of targets) {
173
- console.log(` ${t.action}: ${t.path}`);
174
- console.log(` Reason: ${t.reason}`);
175
- if (t.archivePath) {
176
- console.log(` Archive: ${t.archivePath}`);
177
- }
178
- }
179
- if (targets.length === 0) {
180
- console.log(' No legacy artifacts found.');
364
+ export async function handleLegacyCleanup(opts: LegacyCleanupOptions): Promise<LegacyCleanupResult> {
365
+ // CLI gate rule 4: --dry-run and --apply are mutually exclusive
366
+ if (opts.dryRun && opts.apply) {
367
+ const result: LegacyCleanupResult = {
368
+ status: 'failed',
369
+ mode: 'dry-run',
370
+ fileTargets: [],
371
+ v1Artifacts: [],
372
+ appliedFiles: 0,
373
+ appliedV1Artifacts: 0,
374
+ appliedApprovals: 0,
375
+ appliedActivations: 0,
376
+ errors: [],
377
+ reason: '--dry-run and --apply are mutually exclusive',
378
+ nextAction: 'Specify either --dry-run or --apply, not both',
379
+ };
380
+ if (opts.json) {
381
+ console.log(JSON.stringify(result, null, 2));
382
+ } else {
383
+ console.error('Error: --dry-run and --apply are mutually exclusive');
181
384
  }
182
- } else {
183
- console.log(`\n=== Applying ${targets.length} cleanup(s) ===`);
184
- for (const t of targets) {
385
+ process.exitCode = 1;
386
+ return result;
387
+ }
388
+
389
+ // Default to dry-run if neither flag is set (CLI gate rule 4: default to dry-run)
390
+ // Apply mode if --apply is true OR --dry-run is explicitly false
391
+ const isDryRun = opts.apply === true ? false : opts.dryRun !== false;
392
+
393
+ const { workspacePath } = opts;
394
+ const errors: string[] = [];
395
+
396
+ // ── File cleanup (existing functionality) ──
397
+ const fileTargets = findLegacyTargets(workspacePath);
398
+ let appliedFiles = 0;
399
+
400
+ if (!isDryRun) {
401
+ for (const t of fileTargets) {
185
402
  try {
186
403
  if (t.action === 'archive' && t.archivePath) {
187
404
  const archiveDir = path.dirname(t.archivePath);
@@ -190,17 +407,108 @@ export async function handleLegacyCleanup(
190
407
  }
191
408
  fs.copyFileSync(t.path, t.archivePath);
192
409
  fs.unlinkSync(t.path);
193
- console.log(` Archived: ${t.path} -> ${t.archivePath}`);
194
410
  } else {
195
411
  fs.unlinkSync(t.path);
196
- console.log(` Removed: ${t.path}`);
197
412
  }
198
- applied++;
413
+ appliedFiles++;
199
414
  } catch (err) {
200
- console.error(` ERROR processing ${t.path}: ${String(err)}`);
415
+ errors.push(`File cleanup error for ${t.path}: ${String(err)}`);
416
+ }
417
+ }
418
+ }
419
+
420
+ // ── V1 Artificer artifact cleanup (PRI-439 Phase 6) ──
421
+ let v1Artifacts: V1ArtifactTarget[] = [];
422
+ let appliedV1Artifacts = 0;
423
+ let appliedApprovals = 0;
424
+ let appliedActivations = 0;
425
+
426
+ const dbPath = path.join(workspacePath, '.pd', 'state.db');
427
+ if (fs.existsSync(dbPath)) {
428
+ let stateManager: RuntimeStateManager | null = null;
429
+ try {
430
+ stateManager = new RuntimeStateManager({ workspaceDir: workspacePath });
431
+ await stateManager.initialize();
432
+ const db = stateManager.connection.getDb();
433
+
434
+ v1Artifacts = findV1ArtificerArtifacts(db);
435
+
436
+ if (!isDryRun && v1Artifacts.length > 0) {
437
+ const artifactIds = v1Artifacts.map(t => t.artifactId);
438
+ const deleted = applyV1ArtifactCleanup(db, artifactIds);
439
+ appliedV1Artifacts = deleted.deletedArtifacts;
440
+ appliedApprovals = deleted.deletedApprovals;
441
+ appliedActivations = deleted.deletedActivations;
442
+ }
443
+ } catch (err) {
444
+ errors.push(`V1 artifact cleanup error: ${String(err)}`);
445
+ } finally {
446
+ if (stateManager) {
447
+ await stateManager.close();
448
+ }
449
+ }
450
+ }
451
+
452
+ // ── Build result ──
453
+ const status: LegacyCleanupResult['status'] = errors.length > 0 ? 'partial' : 'ok';
454
+ const result: LegacyCleanupResult = {
455
+ status,
456
+ mode: isDryRun ? 'dry-run' : 'apply',
457
+ fileTargets,
458
+ v1Artifacts,
459
+ appliedFiles,
460
+ appliedV1Artifacts,
461
+ appliedApprovals,
462
+ appliedActivations,
463
+ errors,
464
+ };
465
+
466
+ if (status === 'partial') {
467
+ result.reason = `${errors.length} error(s) occurred during cleanup`;
468
+ result.nextAction = 'Review errors array and re-run after fixing issues';
469
+ }
470
+
471
+ // ── Output ──
472
+ if (opts.json) {
473
+ console.log(JSON.stringify(result, null, 2));
474
+ } else {
475
+ const modeLabel = isDryRun ? 'DRY RUN' : 'APPLY';
476
+ console.log(`\n=== ${modeLabel}: Legacy cleanup ===`);
477
+
478
+ console.log(`\n── File cleanup (${fileTargets.length} target(s)) ──`);
479
+ if (fileTargets.length === 0) {
480
+ console.log(' No legacy files found.');
481
+ }
482
+ for (const t of fileTargets) {
483
+ console.log(` ${t.action}: ${t.path}`);
484
+ console.log(` Reason: ${t.reason}`);
485
+ if (t.archivePath) {
486
+ console.log(` Archive: ${t.archivePath}`);
487
+ }
488
+ }
489
+ if (!isDryRun) {
490
+ console.log(` Applied: ${appliedFiles} file(s)`);
491
+ }
492
+
493
+ console.log(`\n── V1 Artificer artifacts (${v1Artifacts.length} found) ──`);
494
+ if (v1Artifacts.length === 0) {
495
+ console.log(' No V1 Artificer artifacts found.');
496
+ }
497
+ for (const a of v1Artifacts) {
498
+ console.log(` ${a.artifactId} (task: ${a.sourceTaskId})`);
499
+ console.log(` approvals: ${a.approvalCount}, activations: ${a.activationCount}`);
500
+ }
501
+ if (!isDryRun) {
502
+ console.log(` Applied: ${appliedV1Artifacts} artifact(s), ${appliedApprovals} approval(s), ${appliedActivations} activation(s)`);
503
+ }
504
+
505
+ if (errors.length > 0) {
506
+ console.log(`\n── Errors (${errors.length}) ──`);
507
+ for (const e of errors) {
508
+ console.log(` ${e}`);
201
509
  }
202
510
  }
203
511
  }
204
512
 
205
- return { targets, applied };
513
+ return result;
206
514
  }