principles-disciple 1.124.0 → 1.126.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/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/src/core/event-log.ts +13 -0
- package/src/core/rule-host.ts +99 -9
- package/src/core/rule-implementation-runtime.ts +110 -3
- package/src/types/event-types.ts +1 -0
- package/src/utils/io.ts +23 -5
- package/tests/core/rule-host-adversarial-output.test.ts +242 -0
- package/tests/core/rule-host-autocorrect-vm.test.ts +163 -0
- package/tests/core/rule-host-resource-bounds.test.ts +231 -0
- package/tests/core/rule-host-unhealthy-visibility.test.ts +261 -0
- package/tests/core/rule-host-validation.test.ts +315 -0
- package/tests/core/rule-implementation-runtime.test.ts +12 -0
- package/tests/hooks/gate-rule-host-real-pipeline.test.ts +190 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PRI-437: Harden RuleHost execution validation, isolation and activation health
|
|
3
|
+
*
|
|
4
|
+
* TDD vertical slices:
|
|
5
|
+
* 1. Malformed VM results never enforce and create unhealthy evidence
|
|
6
|
+
* 2. Valid decisions work through public before-tool-call hook
|
|
7
|
+
* 3. Infinite loop and memory allocation terminate without taking down host
|
|
8
|
+
* 4. Approved compile failure is visible in health, CLI JSON and Console API
|
|
9
|
+
* 5. Invalid tier/adversarial diagnostics cannot corrupt output
|
|
10
|
+
*
|
|
11
|
+
* Tests verify through public interfaces:
|
|
12
|
+
* - Real SQLite store (SqliteConnection + SqliteActivationStateStore)
|
|
13
|
+
* - Real RuleHost.evaluate()
|
|
14
|
+
* - No mocking of private internals
|
|
15
|
+
*
|
|
16
|
+
* ERR risk mitigation:
|
|
17
|
+
* - ERR-001: no `as` bypass at trust boundary — VM output validated as unknown
|
|
18
|
+
* - ERR-002: no catch-and-degrade — malformed results emit structured unhealthy evidence
|
|
19
|
+
* - ERR-013: Object.hasOwn for untrusted object key checks
|
|
20
|
+
* - ERR-024: validator wired into production path, not just demo/test
|
|
21
|
+
*/
|
|
22
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
23
|
+
import * as fs from 'fs';
|
|
24
|
+
import * as os from 'os';
|
|
25
|
+
import * as path from 'path';
|
|
26
|
+
import { SqliteConnection, SqliteActivationStateStore } from '@principles/core/runtime-v2';
|
|
27
|
+
import type { RuleHostInput } from '@principles/core/runtime-v2';
|
|
28
|
+
import { RuleHost } from '../../src/core/rule-host.js';
|
|
29
|
+
|
|
30
|
+
// ── Test helpers (shared with rule-host-sqlite-source.test.ts pattern) ──────
|
|
31
|
+
|
|
32
|
+
const RULE_ID = 'R_TEST_PRI437_001';
|
|
33
|
+
const ARTIFACT_ID = 'art-pri437-001';
|
|
34
|
+
const ACTIVATION_ID = `act_code_${RULE_ID}`;
|
|
35
|
+
|
|
36
|
+
let tempWorkspaceDir: string;
|
|
37
|
+
let tempStateDir: string;
|
|
38
|
+
let sqliteConn: SqliteConnection;
|
|
39
|
+
|
|
40
|
+
function setupTempDirs(): void {
|
|
41
|
+
const baseTmp = os.tmpdir();
|
|
42
|
+
tempWorkspaceDir = fs.mkdtempSync(path.join(baseTmp, 'pd-rulehost-pri437-'));
|
|
43
|
+
tempStateDir = path.join(tempWorkspaceDir, '.principles');
|
|
44
|
+
fs.mkdirSync(tempStateDir, { recursive: true });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function insertRuleArtifact(overrides?: {
|
|
48
|
+
artifactId?: string;
|
|
49
|
+
ruleId?: string;
|
|
50
|
+
contentJson?: string;
|
|
51
|
+
validationStatus?: string;
|
|
52
|
+
sourceTaskId?: string;
|
|
53
|
+
}): void {
|
|
54
|
+
const artifactId = overrides?.artifactId ?? ARTIFACT_ID;
|
|
55
|
+
const ruleId = overrides?.ruleId ?? RULE_ID;
|
|
56
|
+
const validationStatus = overrides?.validationStatus ?? 'validated';
|
|
57
|
+
const sourceTaskId = overrides?.sourceTaskId ?? 'task-pri437-001';
|
|
58
|
+
const db = sqliteConn.getDb();
|
|
59
|
+
const now = new Date().toISOString();
|
|
60
|
+
|
|
61
|
+
const contentJson = overrides?.contentJson ?? JSON.stringify({
|
|
62
|
+
principleId: 'P_TEST_PRI437',
|
|
63
|
+
ruleId,
|
|
64
|
+
implementationCode: '',
|
|
65
|
+
goldenTrace: {
|
|
66
|
+
traceId: 'trace-pri437',
|
|
67
|
+
cases: [],
|
|
68
|
+
createdAt: now,
|
|
69
|
+
version: 1,
|
|
70
|
+
},
|
|
71
|
+
ruleHostGateDecision: 'accepted_shadow',
|
|
72
|
+
affectedTools: ['write_file'],
|
|
73
|
+
painReasonSummary: 'Test: PRI-437',
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
db.prepare(`
|
|
77
|
+
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)
|
|
78
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
79
|
+
`).run(
|
|
80
|
+
artifactId,
|
|
81
|
+
'rule',
|
|
82
|
+
sourceTaskId,
|
|
83
|
+
'P_TEST_PRI437',
|
|
84
|
+
ruleId,
|
|
85
|
+
'[]',
|
|
86
|
+
validationStatus,
|
|
87
|
+
contentJson,
|
|
88
|
+
now,
|
|
89
|
+
now,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function insertCodeToolHookActivation(overrides?: {
|
|
94
|
+
activationId?: string;
|
|
95
|
+
artifactId?: string;
|
|
96
|
+
ruleId?: string;
|
|
97
|
+
deactivatedAt?: string | null;
|
|
98
|
+
}): Promise<void> {
|
|
99
|
+
const activationId = overrides?.activationId ?? ACTIVATION_ID;
|
|
100
|
+
const artifactId = overrides?.artifactId ?? ARTIFACT_ID;
|
|
101
|
+
const ruleId = overrides?.ruleId ?? RULE_ID;
|
|
102
|
+
const store = new SqliteActivationStateStore(sqliteConn);
|
|
103
|
+
const now = new Date().toISOString();
|
|
104
|
+
|
|
105
|
+
await store.recordActivation({
|
|
106
|
+
activationId,
|
|
107
|
+
idempotencyKey: `${artifactId}::code_tool_hook`,
|
|
108
|
+
artifactId,
|
|
109
|
+
channel: 'code_tool_hook',
|
|
110
|
+
action: 'code_tool_hook_shadow_activate',
|
|
111
|
+
targetRef: `impl://${ruleId}`,
|
|
112
|
+
activatedAt: now,
|
|
113
|
+
deactivatedAt: overrides?.deactivatedAt ?? null,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function makeInput(normalizedPath: string): RuleHostInput {
|
|
118
|
+
return {
|
|
119
|
+
action: {
|
|
120
|
+
toolName: 'write_file',
|
|
121
|
+
normalizedPath,
|
|
122
|
+
paramsSummary: { path: normalizedPath },
|
|
123
|
+
},
|
|
124
|
+
workspace: {
|
|
125
|
+
isRiskPath: false,
|
|
126
|
+
planStatus: 'NONE',
|
|
127
|
+
hasPlanFile: false,
|
|
128
|
+
},
|
|
129
|
+
session: {
|
|
130
|
+
sessionId: 'test-session',
|
|
131
|
+
currentGfi: 0,
|
|
132
|
+
recentThinking: false,
|
|
133
|
+
},
|
|
134
|
+
evolution: {
|
|
135
|
+
epTier: 1,
|
|
136
|
+
},
|
|
137
|
+
derived: {
|
|
138
|
+
estimatedLineChanges: 1,
|
|
139
|
+
bashRisk: 'safe' as const,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function makeContentJson(ruleId: string, code: string): string {
|
|
145
|
+
return JSON.stringify({
|
|
146
|
+
principleId: 'P_TEST_PRI437',
|
|
147
|
+
ruleId,
|
|
148
|
+
implementationCode: code,
|
|
149
|
+
goldenTrace: {
|
|
150
|
+
traceId: 'trace-pri437',
|
|
151
|
+
cases: [],
|
|
152
|
+
createdAt: new Date().toISOString(),
|
|
153
|
+
version: 1,
|
|
154
|
+
},
|
|
155
|
+
ruleHostGateDecision: 'accepted_shadow',
|
|
156
|
+
affectedTools: ['write_file'],
|
|
157
|
+
painReasonSummary: 'Test: PRI-437',
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ── Setup / Teardown ───────────────────────────────────────────────────────
|
|
162
|
+
|
|
163
|
+
beforeEach(() => {
|
|
164
|
+
setupTempDirs();
|
|
165
|
+
sqliteConn = new SqliteConnection(tempWorkspaceDir);
|
|
166
|
+
sqliteConn.getDb();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
afterEach(() => {
|
|
170
|
+
try { sqliteConn?.close(); } catch { /* best-effort */ }
|
|
171
|
+
try { fs.rmSync(tempWorkspaceDir, { recursive: true, force: true }); } catch { /* Windows */ }
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// ── Slice 1: Malformed VM results never enforce and create unhealthy evidence ─
|
|
175
|
+
|
|
176
|
+
describe('PRI-437 Slice 1: Malformed VM results never enforce and create unhealthy evidence', () => {
|
|
177
|
+
it('malformed result with non-string reason does not enforce and emits unhealthy evidence', async () => {
|
|
178
|
+
// RuleCode returns { matched: true, decision: 'block', reason: 42 } — reason is a number, not a string
|
|
179
|
+
const MALFORMED_CODE = `
|
|
180
|
+
function evaluate(input, helpers) {
|
|
181
|
+
return { matched: true, decision: 'block', reason: 42 };
|
|
182
|
+
}
|
|
183
|
+
var meta = { name: 'malformed-rule', version: '1', ruleId: '${RULE_ID}', coversCondition: 'all' };
|
|
184
|
+
`;
|
|
185
|
+
insertRuleArtifact({ contentJson: makeContentJson(RULE_ID, MALFORMED_CODE) });
|
|
186
|
+
await insertCodeToolHookActivation();
|
|
187
|
+
|
|
188
|
+
const warnCalls: string[] = [];
|
|
189
|
+
const spyLogger: { warn: (_message: string) => void } = {
|
|
190
|
+
warn: (message: string) => { warnCalls.push(message); },
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const ruleHost = new RuleHost(tempStateDir, spyLogger, { workspaceDir: tempWorkspaceDir });
|
|
194
|
+
const result = ruleHost.evaluate(makeInput('/etc/passwd'));
|
|
195
|
+
|
|
196
|
+
// Malformed result must NOT enforce — no block returned
|
|
197
|
+
expect(result).toBeUndefined();
|
|
198
|
+
|
|
199
|
+
// Unhealthy evidence must be emitted via logger.warn
|
|
200
|
+
expect(warnCalls.length).toBeGreaterThan(0);
|
|
201
|
+
const unhealthyWarn = warnCalls.find(m =>
|
|
202
|
+
m.toLowerCase().includes('invalid') ||
|
|
203
|
+
m.toLowerCase().includes('malformed') ||
|
|
204
|
+
m.toLowerCase().includes('unhealthy') ||
|
|
205
|
+
m.toLowerCase().includes('validation')
|
|
206
|
+
);
|
|
207
|
+
expect(unhealthyWarn).toBeDefined();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('malformed result with non-boolean matched does not enforce and emits unhealthy evidence', async () => {
|
|
211
|
+
// RuleCode returns { matched: "yes", decision: 'block', reason: 'valid' } — matched is a string
|
|
212
|
+
const MALFORMED_CODE = `
|
|
213
|
+
function evaluate(input, helpers) {
|
|
214
|
+
return { matched: "yes", decision: 'block', reason: 'Blocked: system directory' };
|
|
215
|
+
}
|
|
216
|
+
var meta = { name: 'malformed-matched', version: '1', ruleId: '${RULE_ID}', coversCondition: 'all' };
|
|
217
|
+
`;
|
|
218
|
+
insertRuleArtifact({ contentJson: makeContentJson(RULE_ID, MALFORMED_CODE) });
|
|
219
|
+
await insertCodeToolHookActivation();
|
|
220
|
+
|
|
221
|
+
const warnCalls: string[] = [];
|
|
222
|
+
const spyLogger: { warn: (_message: string) => void } = {
|
|
223
|
+
warn: (message: string) => { warnCalls.push(message); },
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const ruleHost = new RuleHost(tempStateDir, spyLogger, { workspaceDir: tempWorkspaceDir });
|
|
227
|
+
const result = ruleHost.evaluate(makeInput('/etc/passwd'));
|
|
228
|
+
|
|
229
|
+
expect(result).toBeUndefined();
|
|
230
|
+
expect(warnCalls.length).toBeGreaterThan(0);
|
|
231
|
+
const unhealthyWarn = warnCalls.find(m =>
|
|
232
|
+
m.toLowerCase().includes('invalid') ||
|
|
233
|
+
m.toLowerCase().includes('malformed') ||
|
|
234
|
+
m.toLowerCase().includes('unhealthy') ||
|
|
235
|
+
m.toLowerCase().includes('validation')
|
|
236
|
+
);
|
|
237
|
+
expect(unhealthyWarn).toBeDefined();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('malformed result with invalid decision does not enforce and emits unhealthy evidence', async () => {
|
|
241
|
+
// RuleCode returns { matched: true, decision: 'execute', reason: 'valid' } — decision is not one of the four
|
|
242
|
+
const MALFORMED_CODE = `
|
|
243
|
+
function evaluate(input, helpers) {
|
|
244
|
+
return { matched: true, decision: 'execute', reason: 'do it' };
|
|
245
|
+
}
|
|
246
|
+
var meta = { name: 'malformed-decision', version: '1', ruleId: '${RULE_ID}', coversCondition: 'all' };
|
|
247
|
+
`;
|
|
248
|
+
insertRuleArtifact({ contentJson: makeContentJson(RULE_ID, MALFORMED_CODE) });
|
|
249
|
+
await insertCodeToolHookActivation();
|
|
250
|
+
|
|
251
|
+
const warnCalls: string[] = [];
|
|
252
|
+
const spyLogger: { warn: (_message: string) => void } = {
|
|
253
|
+
warn: (message: string) => { warnCalls.push(message); },
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const ruleHost = new RuleHost(tempStateDir, spyLogger, { workspaceDir: tempWorkspaceDir });
|
|
257
|
+
const result = ruleHost.evaluate(makeInput('/etc/passwd'));
|
|
258
|
+
|
|
259
|
+
expect(result).toBeUndefined();
|
|
260
|
+
expect(warnCalls.length).toBeGreaterThan(0);
|
|
261
|
+
const unhealthyWarn = warnCalls.find(m =>
|
|
262
|
+
m.toLowerCase().includes('invalid') ||
|
|
263
|
+
m.toLowerCase().includes('malformed') ||
|
|
264
|
+
m.toLowerCase().includes('unhealthy') ||
|
|
265
|
+
m.toLowerCase().includes('validation')
|
|
266
|
+
);
|
|
267
|
+
expect(unhealthyWarn).toBeDefined();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('null return from evaluate does not enforce and emits unhealthy evidence', async () => {
|
|
271
|
+
const MALFORMED_CODE = `
|
|
272
|
+
function evaluate(input, helpers) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
var meta = { name: 'null-return', version: '1', ruleId: '${RULE_ID}', coversCondition: 'all' };
|
|
276
|
+
`;
|
|
277
|
+
insertRuleArtifact({ contentJson: makeContentJson(RULE_ID, MALFORMED_CODE) });
|
|
278
|
+
await insertCodeToolHookActivation();
|
|
279
|
+
|
|
280
|
+
const warnCalls: string[] = [];
|
|
281
|
+
const spyLogger: { warn: (_message: string) => void } = {
|
|
282
|
+
warn: (message: string) => { warnCalls.push(message); },
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const ruleHost = new RuleHost(tempStateDir, spyLogger, { workspaceDir: tempWorkspaceDir });
|
|
286
|
+
const result = ruleHost.evaluate(makeInput('/etc/passwd'));
|
|
287
|
+
|
|
288
|
+
expect(result).toBeUndefined();
|
|
289
|
+
expect(warnCalls.length).toBeGreaterThan(0);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('valid block result still enforces correctly (no false positive rejection)', async () => {
|
|
293
|
+
const VALID_CODE = `
|
|
294
|
+
function evaluate(input, helpers) {
|
|
295
|
+
var p = input.action.normalizedPath || '';
|
|
296
|
+
if (p.startsWith('/etc')) {
|
|
297
|
+
return { decision: 'block', matched: true, reason: 'Blocked: system directory' };
|
|
298
|
+
}
|
|
299
|
+
return { decision: 'allow', matched: false, reason: 'Not matched' };
|
|
300
|
+
}
|
|
301
|
+
var meta = { name: 'valid-rule', version: '1', ruleId: '${RULE_ID}', coversCondition: 'all' };
|
|
302
|
+
`;
|
|
303
|
+
insertRuleArtifact({ contentJson: makeContentJson(RULE_ID, VALID_CODE) });
|
|
304
|
+
await insertCodeToolHookActivation();
|
|
305
|
+
|
|
306
|
+
const ruleHost = new RuleHost(tempStateDir, console, { workspaceDir: tempWorkspaceDir });
|
|
307
|
+
const result = ruleHost.evaluate(makeInput('/etc/passwd'));
|
|
308
|
+
|
|
309
|
+
expect(result).toBeDefined();
|
|
310
|
+
expect(result?.decision).toBe('block');
|
|
311
|
+
expect(result?.matched).toBe(true);
|
|
312
|
+
expect(result?.reason).toBe('Blocked: system directory');
|
|
313
|
+
expect(result?.ruleId).toBe(RULE_ID);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
@@ -34,4 +34,16 @@ describe('rule-implementation-runtime', () => {
|
|
|
34
34
|
|
|
35
35
|
expect((globalThis as Record<string, unknown>).__pdRuleHostLeak).toBeUndefined();
|
|
36
36
|
});
|
|
37
|
+
|
|
38
|
+
it('contains memory-exhausting evaluation in a resource-limited worker', () => {
|
|
39
|
+
const moduleExports = loadRuleImplementationModule(
|
|
40
|
+
`export function evaluate() {
|
|
41
|
+
const memoryBomb = new Array(100_000_000).fill('x');
|
|
42
|
+
return { decision: 'allow', matched: false, reason: String(memoryBomb.length) };
|
|
43
|
+
}`,
|
|
44
|
+
'rule-memory-limit.js',
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
expect(() => moduleExports.callEvaluate?.({}, {})).toThrow(/worker|memory|heap|timed out|exited without a valid result/i);
|
|
48
|
+
});
|
|
37
49
|
});
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PRI-437 Slice 2: Valid decisions work through public before-tool-call hook
|
|
3
|
+
*
|
|
4
|
+
* PURPOSE: Verify that valid RuleHost decisions (block/allow) flow correctly
|
|
5
|
+
* through the real handleBeforeToolCall public hook with real SQLite activations.
|
|
6
|
+
*
|
|
7
|
+
* This test exercises the FULL public path:
|
|
8
|
+
* real SQLite activation → real RuleHost.evaluate() → validateRuleHostResult()
|
|
9
|
+
* → real handleBeforeToolCall() → block/allow result
|
|
10
|
+
*
|
|
11
|
+
* No mocking of RuleHost, SQLite, or gate internals.
|
|
12
|
+
*
|
|
13
|
+
* ERR risk mitigation:
|
|
14
|
+
* - ERR-024: validator is wired into the production path (verified end-to-end)
|
|
15
|
+
* - ERR-048: activation write (SQLite) connects to read (RuleHost) connects to enforcement (gate)
|
|
16
|
+
* - ERR-002: valid decisions must NOT be silently degraded
|
|
17
|
+
*/
|
|
18
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
19
|
+
import * as fs from 'fs';
|
|
20
|
+
import * as os from 'os';
|
|
21
|
+
import * as path from 'path';
|
|
22
|
+
import { SqliteConnection, SqliteActivationStateStore } from '@principles/core/runtime-v2';
|
|
23
|
+
import { handleBeforeToolCall } from '../../src/hooks/gate.js';
|
|
24
|
+
import type { PluginHookBeforeToolCallEvent, PluginHookToolContext } from '../../src/openclaw-sdk.js';
|
|
25
|
+
import { WorkspaceContext } from '../../src/core/workspace-context.js';
|
|
26
|
+
|
|
27
|
+
// ── Test helpers ───────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
const RULE_ID = 'R_TEST_GATE_002';
|
|
30
|
+
const ARTIFACT_ID = 'art-gate-002';
|
|
31
|
+
const ACTIVATION_ID = `act_code_${RULE_ID}`;
|
|
32
|
+
|
|
33
|
+
const BLOCK_CODE = `
|
|
34
|
+
function evaluate(input, helpers) {
|
|
35
|
+
var p = input.action.normalizedPath || '';
|
|
36
|
+
if (p.indexOf('/etc/') === 0 || p === '/etc') {
|
|
37
|
+
return { decision: 'block', matched: true, reason: 'GATE_BLOCK_002: system directory' };
|
|
38
|
+
}
|
|
39
|
+
return { decision: 'allow', matched: false, reason: 'Not matched' };
|
|
40
|
+
}
|
|
41
|
+
var meta = { name: 'gate-test-rule', version: '1', ruleId: '${RULE_ID}', coversCondition: 'all' };
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
let tempWorkspaceDir: string;
|
|
45
|
+
let tempStateDir: string;
|
|
46
|
+
let sqliteConn: SqliteConnection;
|
|
47
|
+
|
|
48
|
+
function setupTempDirs(): void {
|
|
49
|
+
const baseTmp = os.tmpdir();
|
|
50
|
+
tempWorkspaceDir = fs.mkdtempSync(path.join(baseTmp, 'pd-gate-real-'));
|
|
51
|
+
tempStateDir = path.join(tempWorkspaceDir, '.principles');
|
|
52
|
+
fs.mkdirSync(tempStateDir, { recursive: true });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function insertRuleArtifact(): void {
|
|
56
|
+
const db = sqliteConn.getDb();
|
|
57
|
+
const now = new Date().toISOString();
|
|
58
|
+
const contentJson = JSON.stringify({
|
|
59
|
+
principleId: 'P_TEST_GATE_002',
|
|
60
|
+
ruleId: RULE_ID,
|
|
61
|
+
implementationCode: BLOCK_CODE,
|
|
62
|
+
goldenTrace: {
|
|
63
|
+
traceId: 'trace-gate-002',
|
|
64
|
+
cases: [],
|
|
65
|
+
createdAt: now,
|
|
66
|
+
version: 1,
|
|
67
|
+
},
|
|
68
|
+
ruleHostGateDecision: 'accepted_shadow',
|
|
69
|
+
affectedTools: ['write_file'],
|
|
70
|
+
painReasonSummary: 'Test: block /etc writes via gate',
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
db.prepare(`
|
|
74
|
+
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)
|
|
75
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
76
|
+
`).run(
|
|
77
|
+
ARTIFACT_ID,
|
|
78
|
+
'rule',
|
|
79
|
+
'task-gate-002',
|
|
80
|
+
'P_TEST_GATE_002',
|
|
81
|
+
RULE_ID,
|
|
82
|
+
'[]',
|
|
83
|
+
'validated',
|
|
84
|
+
contentJson,
|
|
85
|
+
now,
|
|
86
|
+
now,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function insertActivation(): Promise<void> {
|
|
91
|
+
const store = new SqliteActivationStateStore(sqliteConn);
|
|
92
|
+
const now = new Date().toISOString();
|
|
93
|
+
await store.recordActivation({
|
|
94
|
+
activationId: ACTIVATION_ID,
|
|
95
|
+
idempotencyKey: `${ARTIFACT_ID}::code_tool_hook`,
|
|
96
|
+
artifactId: ARTIFACT_ID,
|
|
97
|
+
channel: 'code_tool_hook',
|
|
98
|
+
action: 'code_tool_hook_shadow_activate',
|
|
99
|
+
targetRef: `impl://${RULE_ID}`,
|
|
100
|
+
activatedAt: now,
|
|
101
|
+
deactivatedAt: null,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ── Setup / Teardown ───────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
beforeEach(() => {
|
|
108
|
+
setupTempDirs();
|
|
109
|
+
WorkspaceContext.clearCache();
|
|
110
|
+
sqliteConn = new SqliteConnection(tempWorkspaceDir);
|
|
111
|
+
sqliteConn.getDb();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
afterEach(() => {
|
|
115
|
+
WorkspaceContext.clearCache();
|
|
116
|
+
try { sqliteConn?.close(); } catch { /* best-effort */ }
|
|
117
|
+
try { fs.rmSync(tempWorkspaceDir, { recursive: true, force: true }); } catch { /* Windows */ }
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// ── Slice 2: Valid decisions through public hook ───────────────────────────
|
|
121
|
+
|
|
122
|
+
describe('PRI-437 Slice 2: Valid decisions work through public before-tool-call hook', () => {
|
|
123
|
+
it('valid block decision from SQLite activation → handleBeforeToolCall returns block result', async () => {
|
|
124
|
+
// Setup: real SQLite activation with valid blocking code
|
|
125
|
+
insertRuleArtifact();
|
|
126
|
+
await insertActivation();
|
|
127
|
+
|
|
128
|
+
// Exercise the PUBLIC hook with a real event targeting /etc/passwd
|
|
129
|
+
const event: PluginHookBeforeToolCallEvent = {
|
|
130
|
+
toolName: 'write_file',
|
|
131
|
+
params: { file_path: '/etc/passwd', content: 'malicious' },
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const ctx: PluginHookToolContext = {
|
|
135
|
+
workspaceDir: tempWorkspaceDir,
|
|
136
|
+
sessionId: 'test-session-gate-002',
|
|
137
|
+
logger: { warn: () => {}, error: () => {}, info: () => {} },
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const result = handleBeforeToolCall(event, ctx);
|
|
141
|
+
|
|
142
|
+
// Verify: block decision is enforced through the public hook
|
|
143
|
+
expect(result).toBeDefined();
|
|
144
|
+
expect(result?.block).toBe(true);
|
|
145
|
+
expect(result?.blockReason).toContain('GATE_BLOCK_002: system directory');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('valid allow (no match) from SQLite activation → handleBeforeToolCall returns undefined', async () => {
|
|
149
|
+
insertRuleArtifact();
|
|
150
|
+
await insertActivation();
|
|
151
|
+
|
|
152
|
+
// Exercise the PUBLIC hook with a safe path that does NOT match the block rule
|
|
153
|
+
const event: PluginHookBeforeToolCallEvent = {
|
|
154
|
+
toolName: 'write_file',
|
|
155
|
+
params: { file_path: '/safe/project/file.txt', content: 'safe content' },
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const ctx: PluginHookToolContext = {
|
|
159
|
+
workspaceDir: tempWorkspaceDir,
|
|
160
|
+
sessionId: 'test-session-gate-002',
|
|
161
|
+
logger: { warn: () => {}, error: () => {}, info: () => {} },
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const result = handleBeforeToolCall(event, ctx);
|
|
165
|
+
|
|
166
|
+
// Verify: no block (allow passes through)
|
|
167
|
+
expect(result).toBeUndefined();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('no SQLite activation → handleBeforeToolCall returns undefined (no opinion)', async () => {
|
|
171
|
+
// Artifact exists but no activation
|
|
172
|
+
insertRuleArtifact();
|
|
173
|
+
|
|
174
|
+
const event: PluginHookBeforeToolCallEvent = {
|
|
175
|
+
toolName: 'write_file',
|
|
176
|
+
params: { file_path: '/etc/passwd', content: 'malicious' },
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const ctx: PluginHookToolContext = {
|
|
180
|
+
workspaceDir: tempWorkspaceDir,
|
|
181
|
+
sessionId: 'test-session-gate-002',
|
|
182
|
+
logger: { warn: () => {}, error: () => {}, info: () => {} },
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const result = handleBeforeToolCall(event, ctx);
|
|
186
|
+
|
|
187
|
+
// No activation → RuleHost returns undefined → gate allows
|
|
188
|
+
expect(result).toBeUndefined();
|
|
189
|
+
});
|
|
190
|
+
});
|