principles-disciple 1.122.0 → 1.124.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.
@@ -0,0 +1,720 @@
1
+ /**
2
+ * PRI-436: SQLite is the sole RuleHost source — TDD vertical slices
3
+ *
4
+ * Tests verify through public interfaces:
5
+ * - Real SQLite store (SqliteConnection + SqliteActivationStateStore)
6
+ * - Real RuleHost.evaluate()
7
+ * - No mocking of private internals
8
+ *
9
+ * ERR risk mitigation:
10
+ * - ERR-024/ERR-048: tests exercise the production RuleHost.evaluate() → SQLite read chain
11
+ * - ERR-073: tests verify call-site behavior equivalence, not just reader happy path
12
+ */
13
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
14
+ import * as fs from 'fs';
15
+ import * as os from 'os';
16
+ import * as path from 'path';
17
+ import { SqliteConnection, SqliteActivationStateStore } from '@principles/core/runtime-v2';
18
+ import type { RuleHostInput } from '@principles/core/runtime-v2';
19
+ import { RuleHost } from '../../src/core/rule-host.js';
20
+
21
+ // ── Test helpers ───────────────────────────────────────────────────────────
22
+
23
+ const RULE_ID = 'R_TEST_SQLITE_001';
24
+ const ARTIFACT_ID = 'art-rule-sqlite-001';
25
+ const ACTIVATION_ID = `act_code_${RULE_ID}`;
26
+
27
+ const BLOCKING_CODE = `
28
+ function evaluate(input, helpers) {
29
+ var p = input.action.normalizedPath || '';
30
+ if (p.startsWith('/etc')) {
31
+ return { decision: 'block', matched: true, reason: 'Blocked: system directory' };
32
+ }
33
+ return { decision: 'allow', matched: false, reason: 'Not matched' };
34
+ }
35
+ var meta = { name: 'test-sqlite-rule', version: '1', ruleId: '${RULE_ID}', coversCondition: 'all' };
36
+ `;
37
+
38
+ let tempWorkspaceDir: string;
39
+ let tempStateDir: string;
40
+ let sqliteConn: SqliteConnection;
41
+
42
+ function setupTempDirs(): void {
43
+ const baseTmp = os.tmpdir();
44
+ tempWorkspaceDir = fs.mkdtempSync(path.join(baseTmp, 'pd-rulehost-sqlite-'));
45
+ tempStateDir = path.join(tempWorkspaceDir, '.principles');
46
+ fs.mkdirSync(tempStateDir, { recursive: true });
47
+ }
48
+
49
+ function insertRuleArtifact(overrides?: {
50
+ artifactId?: string;
51
+ ruleId?: string;
52
+ contentJson?: string;
53
+ validationStatus?: string;
54
+ sourceTaskId?: string;
55
+ }): void {
56
+ const artifactId = overrides?.artifactId ?? ARTIFACT_ID;
57
+ const ruleId = overrides?.ruleId ?? RULE_ID;
58
+ const validationStatus = overrides?.validationStatus ?? 'validated';
59
+ const sourceTaskId = overrides?.sourceTaskId ?? 'task-test-001';
60
+ const db = sqliteConn.getDb();
61
+ const now = new Date().toISOString();
62
+
63
+ const contentJson = overrides?.contentJson ?? JSON.stringify({
64
+ principleId: 'P_TEST_001',
65
+ ruleId,
66
+ implementationCode: BLOCKING_CODE,
67
+ goldenTrace: {
68
+ traceId: 'trace-test-001',
69
+ cases: [
70
+ { caseId: 'case-neg-1', kind: 'negative', toolName: 'write_file', params: { path: '/etc/passwd' }, expectedDecision: 'block' },
71
+ { caseId: 'case-pos-1', kind: 'positive', toolName: 'write_file', params: { path: '/safe/file.txt' }, expectedDecision: 'allow' },
72
+ ],
73
+ createdAt: now,
74
+ version: 1,
75
+ },
76
+ ruleHostGateDecision: 'accepted_shadow',
77
+ affectedTools: ['write_file'],
78
+ painReasonSummary: 'Test: prevent writing to system directories',
79
+ });
80
+
81
+ db.prepare(`
82
+ INSERT INTO pi_artifacts (artifact_id, artifact_kind, source_task_id, source_principle_id, source_rule_id, lineage_artifact_ids, validation_status, content_json, created_at, updated_at)
83
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
84
+ `).run(
85
+ artifactId,
86
+ 'rule',
87
+ sourceTaskId,
88
+ 'P_TEST_001',
89
+ ruleId,
90
+ '[]',
91
+ validationStatus,
92
+ contentJson,
93
+ now,
94
+ now,
95
+ );
96
+ }
97
+
98
+ async function insertCodeToolHookActivation(overrides?: {
99
+ activationId?: string;
100
+ artifactId?: string;
101
+ ruleId?: string;
102
+ deactivatedAt?: string | null;
103
+ }): Promise<void> {
104
+ const activationId = overrides?.activationId ?? ACTIVATION_ID;
105
+ const artifactId = overrides?.artifactId ?? ARTIFACT_ID;
106
+ const ruleId = overrides?.ruleId ?? RULE_ID;
107
+ const store = new SqliteActivationStateStore(sqliteConn);
108
+ const now = new Date().toISOString();
109
+
110
+ await store.recordActivation({
111
+ activationId,
112
+ idempotencyKey: `${artifactId}::code_tool_hook`,
113
+ artifactId,
114
+ channel: 'code_tool_hook',
115
+ action: 'code_tool_hook_shadow_activate',
116
+ targetRef: `impl://${ruleId}`,
117
+ activatedAt: now,
118
+ deactivatedAt: overrides?.deactivatedAt ?? null,
119
+ });
120
+ }
121
+
122
+ function makeInput(normalizedPath: string): RuleHostInput {
123
+ return {
124
+ action: {
125
+ toolName: 'write_file',
126
+ normalizedPath,
127
+ paramsSummary: { path: normalizedPath },
128
+ },
129
+ workspace: {
130
+ isRiskPath: false,
131
+ planStatus: 'NONE',
132
+ hasPlanFile: false,
133
+ },
134
+ session: {
135
+ sessionId: 'test-session',
136
+ currentGfi: 0,
137
+ recentThinking: false,
138
+ },
139
+ evolution: {
140
+ epTier: 1,
141
+ },
142
+ derived: {
143
+ estimatedLineChanges: 1,
144
+ bashRisk: 'safe' as const,
145
+ },
146
+ };
147
+ }
148
+
149
+ // ── Setup / Teardown ───────────────────────────────────────────────────────
150
+
151
+ beforeEach(() => {
152
+ setupTempDirs();
153
+ sqliteConn = new SqliteConnection(tempWorkspaceDir);
154
+ sqliteConn.getDb();
155
+ });
156
+
157
+ afterEach(() => {
158
+ try { sqliteConn?.close(); } catch { /* best-effort */ }
159
+ try { fs.rmSync(tempWorkspaceDir, { recursive: true, force: true }); } catch { /* Windows */ }
160
+ });
161
+
162
+ // ── Slice 1: SQLite-only RuleHost executes exactly once ────────────────────
163
+
164
+ describe('PRI-436 Slice 1: SQLite-only RuleHost executes exactly once', () => {
165
+ it('single SQLite activation produces block for matching path', async () => {
166
+ insertRuleArtifact();
167
+ await insertCodeToolHookActivation();
168
+
169
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
170
+ const result = ruleHost.evaluate(makeInput('/etc/passwd'));
171
+
172
+ expect(result).toBeDefined();
173
+ expect(result?.decision).toBe('block');
174
+ expect(result?.matched).toBe(true);
175
+ expect(result?.ruleId).toBe(RULE_ID);
176
+ });
177
+
178
+ it('single SQLite activation allows non-matching path', async () => {
179
+ insertRuleArtifact();
180
+ await insertCodeToolHookActivation();
181
+
182
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
183
+ const result = ruleHost.evaluate(makeInput('/safe/project/file.txt'));
184
+
185
+ // No match → undefined (no opinion) or allow
186
+ expect(result?.decision ?? 'allow').toBe('allow');
187
+ });
188
+
189
+ it('no SQLite activation → no opinion (undefined)', async () => {
190
+ // Artifact exists but no activation
191
+ insertRuleArtifact();
192
+
193
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
194
+ const result = ruleHost.evaluate(makeInput('/etc/passwd'));
195
+
196
+ expect(result).toBeUndefined();
197
+ });
198
+
199
+ it('deactivated SQLite activation → no opinion (undefined)', async () => {
200
+ insertRuleArtifact();
201
+ await insertCodeToolHookActivation({ deactivatedAt: new Date().toISOString() });
202
+
203
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
204
+ const result = ruleHost.evaluate(makeInput('/etc/passwd'));
205
+
206
+ expect(result).toBeUndefined();
207
+ });
208
+ });
209
+
210
+ // ── Slice 2: Legacy filesystem file exists but is never read/compiled ──────
211
+
212
+ const LEGACY_RULE_ID = 'R_TEST_DUAL_001';
213
+ const LEGACY_IMPL_ID = 'impl_legacy_001';
214
+ const SQLITE_RULE_ID = 'R_TEST_DUAL_001';
215
+ const SQLITE_ARTIFACT_ID = 'art-rule-dual-001';
216
+ const SQLITE_ACTIVATION_ID = 'act_code_R_TEST_DUAL_001';
217
+
218
+ const LEGACY_BLOCK_CODE = `
219
+ function evaluate(input, helpers) {
220
+ var p = input.action.normalizedPath || '';
221
+ if (p.startsWith('/etc')) {
222
+ return { decision: 'block', matched: true, reason: 'LEGACY_BLOCK' };
223
+ }
224
+ return { decision: 'allow', matched: false, reason: 'Not matched' };
225
+ }
226
+ var meta = { name: 'legacy-rule', version: '1', ruleId: '${LEGACY_RULE_ID}', coversCondition: 'all' };
227
+ `;
228
+
229
+ const SQLITE_BLOCK_CODE = `
230
+ function evaluate(input, helpers) {
231
+ var p = input.action.normalizedPath || '';
232
+ if (p.startsWith('/etc')) {
233
+ return { decision: 'block', matched: true, reason: 'SQLITE_BLOCK' };
234
+ }
235
+ return { decision: 'allow', matched: false, reason: 'Not matched' };
236
+ }
237
+ var meta = { name: 'sqlite-rule', version: '2', ruleId: '${SQLITE_RULE_ID}', coversCondition: 'all' };
238
+ `;
239
+
240
+ /**
241
+ * Write a filesystem ledger (principle_training_state.json) with an active
242
+ * code implementation. Also writes the implementation source file to disk.
243
+ */
244
+ function writeLegacyFilesystemLedger(stateDir: string): void {
245
+ const ledgerPath = path.join(stateDir, 'principle_training_state.json');
246
+ const implSourcePath = path.join(stateDir, 'principles', 'implementations', LEGACY_IMPL_ID, 'entry.js');
247
+
248
+ // Write the implementation source file
249
+ fs.mkdirSync(path.dirname(implSourcePath), { recursive: true });
250
+ fs.writeFileSync(implSourcePath, LEGACY_BLOCK_CODE, 'utf-8');
251
+
252
+ // Write the manifest
253
+ const manifestPath = path.join(stateDir, 'principles', 'implementations', LEGACY_IMPL_ID, 'manifest.json');
254
+ fs.writeFileSync(manifestPath, JSON.stringify({
255
+ implId: LEGACY_IMPL_ID,
256
+ entryFile: 'entry.js',
257
+ version: '1',
258
+ createdAt: new Date().toISOString(),
259
+ updatedAt: new Date().toISOString(),
260
+ }), 'utf-8');
261
+
262
+ // Write the ledger
263
+ const now = new Date().toISOString();
264
+ const ledger = {
265
+ _tree: {
266
+ principles: {},
267
+ rules: {},
268
+ implementations: {
269
+ [LEGACY_IMPL_ID]: {
270
+ id: LEGACY_IMPL_ID,
271
+ ruleId: LEGACY_RULE_ID,
272
+ type: 'code',
273
+ path: implSourcePath,
274
+ version: '1',
275
+ coversCondition: 'all',
276
+ coveragePercentage: 100,
277
+ lifecycleState: 'active',
278
+ createdAt: now,
279
+ updatedAt: now,
280
+ },
281
+ },
282
+ metrics: {},
283
+ lastUpdated: now,
284
+ },
285
+ };
286
+ fs.writeFileSync(ledgerPath, JSON.stringify(ledger, null, 2), 'utf-8');
287
+ }
288
+
289
+ describe('PRI-436 Slice 2: Legacy filesystem file is never read/compiled', () => {
290
+ it('conflicting legacy ledger exists → SQLite version wins (filesystem never read)', async () => {
291
+ // Create conflicting filesystem ledger with LEGACY_BLOCK code
292
+ writeLegacyFilesystemLedger(tempStateDir);
293
+
294
+ // Create SQLite activation with SQLITE_BLOCK code
295
+ insertRuleArtifact({
296
+ artifactId: SQLITE_ARTIFACT_ID,
297
+ ruleId: SQLITE_RULE_ID,
298
+ contentJson: JSON.stringify({
299
+ principleId: 'P_TEST_DUAL_001',
300
+ ruleId: SQLITE_RULE_ID,
301
+ implementationCode: SQLITE_BLOCK_CODE,
302
+ goldenTrace: {
303
+ traceId: 'trace-dual-001',
304
+ cases: [
305
+ { caseId: 'case-neg-1', kind: 'negative', toolName: 'write_file', params: { path: '/etc/passwd' }, expectedDecision: 'block' },
306
+ { caseId: 'case-pos-1', kind: 'positive', toolName: 'write_file', params: { path: '/safe/file.txt' }, expectedDecision: 'allow' },
307
+ ],
308
+ createdAt: new Date().toISOString(),
309
+ version: 1,
310
+ },
311
+ ruleHostGateDecision: 'accepted_shadow',
312
+ affectedTools: ['write_file'],
313
+ painReasonSummary: 'Test: prevent writing to system directories',
314
+ }),
315
+ });
316
+ await insertCodeToolHookActivation({
317
+ activationId: SQLITE_ACTIVATION_ID,
318
+ artifactId: SQLITE_ARTIFACT_ID,
319
+ ruleId: SQLITE_RULE_ID,
320
+ });
321
+
322
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
323
+ const result = ruleHost.evaluate(makeInput('/etc/passwd'));
324
+
325
+ expect(result).toBeDefined();
326
+ expect(result?.decision).toBe('block');
327
+ expect(result?.matched).toBe(true);
328
+ // SQLite version must win — filesystem legacy code must NOT be read
329
+ expect(result?.reason).toBe('SQLITE_BLOCK');
330
+ expect(result?.reason).not.toBe('LEGACY_BLOCK');
331
+ });
332
+
333
+ it('legacy ledger exists but no SQLite activation → no opinion (undefined)', async () => {
334
+ // Filesystem ledger exists but RuleHost should not read it
335
+ writeLegacyFilesystemLedger(tempStateDir);
336
+
337
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
338
+ const result = ruleHost.evaluate(makeInput('/etc/passwd'));
339
+
340
+ // No SQLite activation → no opinion, even though filesystem ledger has an active impl
341
+ expect(result).toBeUndefined();
342
+ });
343
+ });
344
+
345
+ // ── Slice 3: Duplicate active DB rows execute zero times + structured unhealthy evidence ───
346
+
347
+ const DUP_RULE_ID = 'R_TEST_DUP_001';
348
+ const DUP_ARTIFACT_ID_A = 'art-dup-a-001';
349
+ const DUP_ARTIFACT_ID_B = 'art-dup-b-001';
350
+ const DUP_ACTIVATION_ID_A = 'act_code_dup_a_001';
351
+ const DUP_ACTIVATION_ID_B = 'act_code_dup_b_001';
352
+
353
+ const DUP_BLOCK_CODE_A = `
354
+ function evaluate(input, helpers) {
355
+ return { decision: 'block', matched: true, reason: 'DUP_BLOCK_A' };
356
+ }
357
+ var meta = { name: 'dup-rule-a', version: '1', ruleId: '${DUP_RULE_ID}', coversCondition: 'all' };
358
+ `;
359
+
360
+ const DUP_BLOCK_CODE_B = `
361
+ function evaluate(input, helpers) {
362
+ return { decision: 'block', matched: true, reason: 'DUP_BLOCK_B' };
363
+ }
364
+ var meta = { name: 'dup-rule-b', version: '2', ruleId: '${DUP_RULE_ID}', coversCondition: 'all' };
365
+ `;
366
+
367
+ describe('PRI-436 Slice 3: Duplicate active DB rows execute zero times + structured unhealthy evidence', () => {
368
+ it('two active activations for the same rule → zero executions (undefined) + structured warn', async () => {
369
+ // Insert two artifacts for the same rule (different artifactIds, different code)
370
+ insertRuleArtifact({
371
+ artifactId: DUP_ARTIFACT_ID_A,
372
+ ruleId: DUP_RULE_ID,
373
+ sourceTaskId: 'task-dup-a-001',
374
+ contentJson: JSON.stringify({
375
+ principleId: 'P_TEST_DUP_001',
376
+ ruleId: DUP_RULE_ID,
377
+ implementationCode: DUP_BLOCK_CODE_A,
378
+ goldenTrace: { traceId: 'trace-dup-a', cases: [], createdAt: new Date().toISOString(), version: 1 },
379
+ ruleHostGateDecision: 'accepted_shadow',
380
+ affectedTools: ['write_file'],
381
+ painReasonSummary: 'Test: duplicate A',
382
+ }),
383
+ });
384
+ insertRuleArtifact({
385
+ artifactId: DUP_ARTIFACT_ID_B,
386
+ ruleId: DUP_RULE_ID,
387
+ sourceTaskId: 'task-dup-b-001',
388
+ contentJson: JSON.stringify({
389
+ principleId: 'P_TEST_DUP_001',
390
+ ruleId: DUP_RULE_ID,
391
+ implementationCode: DUP_BLOCK_CODE_B,
392
+ goldenTrace: { traceId: 'trace-dup-b', cases: [], createdAt: new Date().toISOString(), version: 1 },
393
+ ruleHostGateDecision: 'accepted_shadow',
394
+ affectedTools: ['write_file'],
395
+ painReasonSummary: 'Test: duplicate B',
396
+ }),
397
+ });
398
+
399
+ // Insert two active activations targeting the same rule (same target_ref)
400
+ await insertCodeToolHookActivation({
401
+ activationId: DUP_ACTIVATION_ID_A,
402
+ artifactId: DUP_ARTIFACT_ID_A,
403
+ ruleId: DUP_RULE_ID,
404
+ });
405
+ await insertCodeToolHookActivation({
406
+ activationId: DUP_ACTIVATION_ID_B,
407
+ artifactId: DUP_ARTIFACT_ID_B,
408
+ ruleId: DUP_RULE_ID,
409
+ });
410
+
411
+ // Spy logger to capture structured warn evidence
412
+ const warnCalls: string[] = [];
413
+ const spyLogger: { warn: (_message: string) => void } = {
414
+ warn: (message: string) => { warnCalls.push(message); },
415
+ };
416
+
417
+ const ruleHost = new RuleHost(tempStateDir, spyLogger, { workspaceDir: tempWorkspaceDir });
418
+ const result = ruleHost.evaluate(makeInput('/etc/passwd'));
419
+
420
+ // Zero executions for the duplicate rule → no opinion (undefined)
421
+ expect(result).toBeUndefined();
422
+
423
+ // Structured unhealthy evidence emitted via logger.warn
424
+ expect(warnCalls.length).toBeGreaterThan(0);
425
+ const dupWarn = warnCalls.find(m => m.toLowerCase().includes('duplicate'));
426
+ expect(dupWarn).toBeDefined();
427
+ // Evidence must identify the conflicting rule and activations
428
+ expect(dupWarn).toContain(DUP_RULE_ID);
429
+ expect(dupWarn).toContain(DUP_ACTIVATION_ID_A);
430
+ expect(dupWarn).toContain(DUP_ACTIVATION_ID_B);
431
+ });
432
+
433
+ it('non-duplicate rule still executes when another rule has duplicates', async () => {
434
+ // Rule with duplicate activations (should be skipped)
435
+ insertRuleArtifact({
436
+ artifactId: DUP_ARTIFACT_ID_A,
437
+ ruleId: DUP_RULE_ID,
438
+ sourceTaskId: 'task-dup-a-002',
439
+ contentJson: JSON.stringify({
440
+ principleId: 'P_TEST_DUP_001',
441
+ ruleId: DUP_RULE_ID,
442
+ implementationCode: DUP_BLOCK_CODE_A,
443
+ goldenTrace: { traceId: 'trace-dup-a', cases: [], createdAt: new Date().toISOString(), version: 1 },
444
+ ruleHostGateDecision: 'accepted_shadow',
445
+ affectedTools: ['write_file'],
446
+ painReasonSummary: 'Test: duplicate A',
447
+ }),
448
+ });
449
+ insertRuleArtifact({
450
+ artifactId: DUP_ARTIFACT_ID_B,
451
+ ruleId: DUP_RULE_ID,
452
+ sourceTaskId: 'task-dup-b-002',
453
+ contentJson: JSON.stringify({
454
+ principleId: 'P_TEST_DUP_001',
455
+ ruleId: DUP_RULE_ID,
456
+ implementationCode: DUP_BLOCK_CODE_B,
457
+ goldenTrace: { traceId: 'trace-dup-b', cases: [], createdAt: new Date().toISOString(), version: 1 },
458
+ ruleHostGateDecision: 'accepted_shadow',
459
+ affectedTools: ['write_file'],
460
+ painReasonSummary: 'Test: duplicate B',
461
+ }),
462
+ });
463
+ await insertCodeToolHookActivation({
464
+ activationId: DUP_ACTIVATION_ID_A,
465
+ artifactId: DUP_ARTIFACT_ID_A,
466
+ ruleId: DUP_RULE_ID,
467
+ });
468
+ await insertCodeToolHookActivation({
469
+ activationId: DUP_ACTIVATION_ID_B,
470
+ artifactId: DUP_ARTIFACT_ID_B,
471
+ ruleId: DUP_RULE_ID,
472
+ });
473
+
474
+ // Non-duplicate rule (should execute normally)
475
+ const OTHER_RULE_ID = 'R_TEST_OTHER_001';
476
+ const OTHER_ARTIFACT_ID = 'art-other-001';
477
+ const OTHER_ACTIVATION_ID = 'act_code_other_001';
478
+ const OTHER_BLOCK_CODE = `
479
+ function evaluate(input, helpers) {
480
+ var p = input.action.normalizedPath || '';
481
+ if (p.startsWith('/etc')) {
482
+ return { decision: 'block', matched: true, reason: 'OTHER_BLOCK' };
483
+ }
484
+ return { decision: 'allow', matched: false, reason: 'Not matched' };
485
+ }
486
+ var meta = { name: 'other-rule', version: '1', ruleId: '${OTHER_RULE_ID}', coversCondition: 'all' };
487
+ `;
488
+ insertRuleArtifact({
489
+ artifactId: OTHER_ARTIFACT_ID,
490
+ ruleId: OTHER_RULE_ID,
491
+ sourceTaskId: 'task-other-001',
492
+ contentJson: JSON.stringify({
493
+ principleId: 'P_TEST_OTHER_001',
494
+ ruleId: OTHER_RULE_ID,
495
+ implementationCode: OTHER_BLOCK_CODE,
496
+ goldenTrace: { traceId: 'trace-other', cases: [], createdAt: new Date().toISOString(), version: 1 },
497
+ ruleHostGateDecision: 'accepted_shadow',
498
+ affectedTools: ['write_file'],
499
+ painReasonSummary: 'Test: other rule',
500
+ }),
501
+ });
502
+ await insertCodeToolHookActivation({
503
+ activationId: OTHER_ACTIVATION_ID,
504
+ artifactId: OTHER_ARTIFACT_ID,
505
+ ruleId: OTHER_RULE_ID,
506
+ });
507
+
508
+ const warnCalls: string[] = [];
509
+ const spyLogger: { warn: (_message: string) => void } = {
510
+ warn: (message: string) => { warnCalls.push(message); },
511
+ };
512
+
513
+ const ruleHost = new RuleHost(tempStateDir, spyLogger, { workspaceDir: tempWorkspaceDir });
514
+ const result = ruleHost.evaluate(makeInput('/etc/passwd'));
515
+
516
+ // Non-duplicate rule still executes and blocks
517
+ expect(result).toBeDefined();
518
+ expect(result?.decision).toBe('block');
519
+ expect(result?.matched).toBe(true);
520
+ expect(result?.reason).toBe('OTHER_BLOCK');
521
+ expect(result?.ruleId).toBe(OTHER_RULE_ID);
522
+
523
+ // Duplicate rule evidence still emitted
524
+ const dupWarn = warnCalls.find(m => m.toLowerCase().includes('duplicate'));
525
+ expect(dupWarn).toBeDefined();
526
+ expect(dupWarn).toContain(DUP_RULE_ID);
527
+ });
528
+ });
529
+
530
+ // ── Slice 4: edit/reactivate selects only new version; deactivate removes effect immediately ─
531
+
532
+ const EDIT_RULE_ID = 'R_TEST_EDIT_001';
533
+ const EDIT_ARTIFACT_ID_V1 = 'art-edit-v1-001';
534
+ const EDIT_ARTIFACT_ID_V2 = 'art-edit-v2-001';
535
+ const EDIT_ACTIVATION_ID_V1 = 'act_code_edit_v1_001';
536
+ const EDIT_ACTIVATION_ID_V2 = 'act_code_edit_v2_001';
537
+
538
+ const EDIT_BLOCK_CODE_V1 = `
539
+ function evaluate(input, helpers) {
540
+ var p = input.action.normalizedPath || '';
541
+ if (p.startsWith('/etc')) {
542
+ return { decision: 'block', matched: true, reason: 'EDIT_V1_BLOCK' };
543
+ }
544
+ return { decision: 'allow', matched: false, reason: 'Not matched' };
545
+ }
546
+ var meta = { name: 'edit-rule-v1', version: '1', ruleId: '${EDIT_RULE_ID}', coversCondition: 'all' };
547
+ `;
548
+
549
+ const EDIT_BLOCK_CODE_V2 = `
550
+ function evaluate(input, helpers) {
551
+ var p = input.action.normalizedPath || '';
552
+ if (p.startsWith('/etc')) {
553
+ return { decision: 'block', matched: true, reason: 'EDIT_V2_BLOCK' };
554
+ }
555
+ return { decision: 'allow', matched: false, reason: 'Not matched' };
556
+ }
557
+ var meta = { name: 'edit-rule-v2', version: '2', ruleId: '${EDIT_RULE_ID}', coversCondition: 'all' };
558
+ `;
559
+
560
+ describe('PRI-436 Slice 4: edit/reactivate selects only new version; deactivate removes effect immediately', () => {
561
+ it('edit: old version deactivated + new version active → only new version executes', async () => {
562
+ // v1: insert artifact + activate
563
+ insertRuleArtifact({
564
+ artifactId: EDIT_ARTIFACT_ID_V1,
565
+ ruleId: EDIT_RULE_ID,
566
+ sourceTaskId: 'task-edit-v1-001',
567
+ contentJson: JSON.stringify({
568
+ principleId: 'P_TEST_EDIT_001',
569
+ ruleId: EDIT_RULE_ID,
570
+ implementationCode: EDIT_BLOCK_CODE_V1,
571
+ goldenTrace: { traceId: 'trace-edit-v1', cases: [], createdAt: new Date().toISOString(), version: 1 },
572
+ ruleHostGateDecision: 'accepted_shadow',
573
+ affectedTools: ['write_file'],
574
+ painReasonSummary: 'Test: edit v1',
575
+ }),
576
+ });
577
+ await insertCodeToolHookActivation({
578
+ activationId: EDIT_ACTIVATION_ID_V1,
579
+ artifactId: EDIT_ARTIFACT_ID_V1,
580
+ ruleId: EDIT_RULE_ID,
581
+ });
582
+
583
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
584
+
585
+ // v1 is active → v1 executes
586
+ const resultV1 = ruleHost.evaluate(makeInput('/etc/passwd'));
587
+ expect(resultV1?.decision).toBe('block');
588
+ expect(resultV1?.reason).toBe('EDIT_V1_BLOCK');
589
+
590
+ // Edit: deactivate v1, insert v2, activate v2
591
+ const store = new SqliteActivationStateStore(sqliteConn);
592
+ await store.deactivateActivation(EDIT_ACTIVATION_ID_V1, new Date().toISOString());
593
+
594
+ insertRuleArtifact({
595
+ artifactId: EDIT_ARTIFACT_ID_V2,
596
+ ruleId: EDIT_RULE_ID,
597
+ sourceTaskId: 'task-edit-v2-001',
598
+ contentJson: JSON.stringify({
599
+ principleId: 'P_TEST_EDIT_001',
600
+ ruleId: EDIT_RULE_ID,
601
+ implementationCode: EDIT_BLOCK_CODE_V2,
602
+ goldenTrace: { traceId: 'trace-edit-v2', cases: [], createdAt: new Date().toISOString(), version: 2 },
603
+ ruleHostGateDecision: 'accepted_shadow',
604
+ affectedTools: ['write_file'],
605
+ painReasonSummary: 'Test: edit v2',
606
+ }),
607
+ });
608
+ await insertCodeToolHookActivation({
609
+ activationId: EDIT_ACTIVATION_ID_V2,
610
+ artifactId: EDIT_ARTIFACT_ID_V2,
611
+ ruleId: EDIT_RULE_ID,
612
+ });
613
+
614
+ // Only v2 should execute (v1 is deactivated)
615
+ const resultV2 = ruleHost.evaluate(makeInput('/etc/passwd'));
616
+ expect(resultV2?.decision).toBe('block');
617
+ expect(resultV2?.reason).toBe('EDIT_V2_BLOCK');
618
+ expect(resultV2?.reason).not.toBe('EDIT_V1_BLOCK');
619
+ });
620
+
621
+ it('deactivate removes effect immediately (same RuleHost instance, no restart)', async () => {
622
+ insertRuleArtifact({
623
+ artifactId: EDIT_ARTIFACT_ID_V1,
624
+ ruleId: EDIT_RULE_ID,
625
+ sourceTaskId: 'task-deact-001',
626
+ contentJson: JSON.stringify({
627
+ principleId: 'P_TEST_EDIT_001',
628
+ ruleId: EDIT_RULE_ID,
629
+ implementationCode: EDIT_BLOCK_CODE_V1,
630
+ goldenTrace: { traceId: 'trace-deact', cases: [], createdAt: new Date().toISOString(), version: 1 },
631
+ ruleHostGateDecision: 'accepted_shadow',
632
+ affectedTools: ['write_file'],
633
+ painReasonSummary: 'Test: deactivate',
634
+ }),
635
+ });
636
+ await insertCodeToolHookActivation({
637
+ activationId: EDIT_ACTIVATION_ID_V1,
638
+ artifactId: EDIT_ARTIFACT_ID_V1,
639
+ ruleId: EDIT_RULE_ID,
640
+ });
641
+
642
+ const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
643
+
644
+ // Rule is active → blocks
645
+ const resultBefore = ruleHost.evaluate(makeInput('/etc/passwd'));
646
+ expect(resultBefore?.decision).toBe('block');
647
+ expect(resultBefore?.reason).toBe('EDIT_V1_BLOCK');
648
+
649
+ // Deactivate
650
+ const store = new SqliteActivationStateStore(sqliteConn);
651
+ await store.deactivateActivation(EDIT_ACTIVATION_ID_V1, new Date().toISOString());
652
+
653
+ // Same RuleHost instance → no effect immediately (no restart needed)
654
+ const resultAfter = ruleHost.evaluate(makeInput('/etc/passwd'));
655
+ expect(resultAfter).toBeUndefined();
656
+ });
657
+ });
658
+
659
+ // ── Slice 5: Restart preserves the one active version ──────────────────────
660
+
661
+ const RESTART_RULE_ID = 'R_TEST_RESTART_001';
662
+ const RESTART_ARTIFACT_ID = 'art-restart-001';
663
+ const RESTART_ACTIVATION_ID = 'act_code_restart_001';
664
+
665
+ const RESTART_BLOCK_CODE = `
666
+ function evaluate(input, helpers) {
667
+ var p = input.action.normalizedPath || '';
668
+ if (p.startsWith('/etc')) {
669
+ return { decision: 'block', matched: true, reason: 'RESTART_BLOCK' };
670
+ }
671
+ return { decision: 'allow', matched: false, reason: 'Not matched' };
672
+ }
673
+ var meta = { name: 'restart-rule', version: '1', ruleId: '${RESTART_RULE_ID}', coversCondition: 'all' };
674
+ `;
675
+
676
+ describe('PRI-436 Slice 5: Restart preserves the one active version', () => {
677
+ it('new RuleHost instance (simulated restart) loads the same active version from SQLite', async () => {
678
+ insertRuleArtifact({
679
+ artifactId: RESTART_ARTIFACT_ID,
680
+ ruleId: RESTART_RULE_ID,
681
+ sourceTaskId: 'task-restart-001',
682
+ contentJson: JSON.stringify({
683
+ principleId: 'P_TEST_RESTART_001',
684
+ ruleId: RESTART_RULE_ID,
685
+ implementationCode: RESTART_BLOCK_CODE,
686
+ goldenTrace: { traceId: 'trace-restart', cases: [], createdAt: new Date().toISOString(), version: 1 },
687
+ ruleHostGateDecision: 'accepted_shadow',
688
+ affectedTools: ['write_file'],
689
+ painReasonSummary: 'Test: restart persistence',
690
+ }),
691
+ });
692
+ await insertCodeToolHookActivation({
693
+ activationId: RESTART_ACTIVATION_ID,
694
+ artifactId: RESTART_ARTIFACT_ID,
695
+ ruleId: RESTART_RULE_ID,
696
+ });
697
+
698
+ // Instance 1 (original process)
699
+ const ruleHost1 = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
700
+ const result1 = ruleHost1.evaluate(makeInput('/etc/passwd'));
701
+ expect(result1?.decision).toBe('block');
702
+ expect(result1?.reason).toBe('RESTART_BLOCK');
703
+ expect(result1?.ruleId).toBe(RESTART_RULE_ID);
704
+
705
+ // Instance 2 (simulated restart — new RuleHost, same SQLite state)
706
+ const ruleHost2 = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
707
+ const result2 = ruleHost2.evaluate(makeInput('/etc/passwd'));
708
+ expect(result2?.decision).toBe('block');
709
+ expect(result2?.reason).toBe('RESTART_BLOCK');
710
+ expect(result2?.ruleId).toBe(RESTART_RULE_ID);
711
+
712
+ // Deactivate, then instance 3 (restart after deactivation)
713
+ const store = new SqliteActivationStateStore(sqliteConn);
714
+ await store.deactivateActivation(RESTART_ACTIVATION_ID, new Date().toISOString());
715
+
716
+ const ruleHost3 = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
717
+ const result3 = ruleHost3.evaluate(makeInput('/etc/passwd'));
718
+ expect(result3).toBeUndefined();
719
+ });
720
+ });