principles-disciple 1.62.0 → 1.64.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.
@@ -1,313 +0,0 @@
1
- /**
2
- * Thinking OS Checkpoint Tests (P-10)
3
- *
4
- * Tests the mandatory deep thinking checkpoint before high-risk operations.
5
- */
6
-
7
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
8
- import { handleBeforeToolCall } from '../../src/hooks/gate.js';
9
- import { recordThinkingCheckpoint, hasRecentThinking, clearSession } from '../../src/core/session-tracker.js';
10
- import { WorkspaceContext } from '../../src/core/workspace-context.js';
11
- import * as riskCalculator from '../../src/core/risk-calculator.js';
12
- import * as evolutionEngine from '../../src/core/evolution-engine.js';
13
- import * as fs from 'fs';
14
- import * as path from 'path';
15
-
16
- vi.mock('../../src/core/workspace-context.js');
17
- vi.mock('../../src/core/risk-calculator.js');
18
- vi.mock('../../src/core/evolution-engine.js', () => ({
19
- checkEvolutionGate: vi.fn().mockReturnValue({ allowed: true, currentTier: 3, reason: undefined }),
20
- getEvolutionEngine: vi.fn().mockReturnValue({
21
- getTier: vi.fn().mockReturnValue(3),
22
- getPoints: vi.fn().mockReturnValue(200),
23
- }),
24
- }));
25
-
26
- const MOCK_SESSION_ID = 'test-thinking-session-001';
27
- // Use os.tmpdir() for cross-platform compatibility
28
- const MOCK_WORKSPACE = require('os').tmpdir() + '/pd-test-thinking-workspace';
29
- const PROFILE_PATH = path.join(MOCK_WORKSPACE, '.principles', 'PROFILE.json');
30
-
31
- // Profile with thinking checkpoint ENABLED for testing
32
- const TEST_PROFILE = {
33
- thinking_checkpoint: {
34
- enabled: true,
35
- window_ms: 5 * 60 * 1000,
36
- high_risk_tools: ['run_shell_command', 'delete_file', 'move_file', 'sessions_spawn', 'write', 'edit'],
37
- },
38
- };
39
-
40
- function createMockContext(overrides = {}) {
41
- return {
42
- sessionId: MOCK_SESSION_ID,
43
- workspaceDir: MOCK_WORKSPACE,
44
- pluginConfig: {},
45
- logger: { info: () => {}, error: () => {}, warn: () => {} },
46
- ...overrides,
47
- };
48
- }
49
-
50
- function createMockEvent(toolName: string, params: Record<string, any> = {}) {
51
- return {
52
- toolName,
53
- params,
54
- };
55
- }
56
-
57
- // Mock evolution object for WorkspaceContext
58
- const mockEvolution = {
59
- getTier: vi.fn().mockReturnValue(3),
60
- getPoints: vi.fn().mockReturnValue(200),
61
- };
62
-
63
- // Mock WorkspaceContext that will be returned from fromHookContext
64
- const mockWctx = {
65
- workspaceDir: MOCK_WORKSPACE,
66
- stateDir: path.join(MOCK_WORKSPACE, '.principles'),
67
- config: {
68
- get: vi.fn().mockImplementation((key) => {
69
- if (key === 'gfi_gate') return {
70
- enabled: true,
71
- thresholds: { low_risk_block: 70, high_risk_block: 40, large_change_block: 50 },
72
- large_change_lines: 50,
73
- ep_tier_multipliers: { '1': 0.5, '2': 0.75, '3': 1.0, '4': 1.5, '5': 2.0 },
74
- bash_safe_patterns: [],
75
- bash_dangerous_patterns: [],
76
- };
77
- return undefined;
78
- }),
79
- },
80
- eventLog: { recordGateBlock: vi.fn(), recordPlanApproval: vi.fn(), recordGfiGateBlock: vi.fn() },
81
- trajectory: { recordGateBlock: vi.fn(), recordTaskOutcome: vi.fn() },
82
- evolution: mockEvolution,
83
- resolve: vi.fn().mockImplementation((key) => {
84
- if (key === 'PROFILE') return path.join(MOCK_WORKSPACE, '.principles', 'PROFILE.json');
85
- if (key === 'PLAN') return path.join(MOCK_WORKSPACE, 'PLAN.md');
86
- return '';
87
- }),
88
- };
89
-
90
- describe('Thinking OS Checkpoint (P-10)', () => {
91
- beforeEach(() => {
92
- vi.clearAllMocks();
93
- vi.useFakeTimers();
94
- clearSession(MOCK_SESSION_ID);
95
- // Create workspace directory and PROFILE.json with checkpoint enabled
96
- fs.mkdirSync(path.dirname(PROFILE_PATH), { recursive: true });
97
- fs.writeFileSync(PROFILE_PATH, JSON.stringify(TEST_PROFILE));
98
- // Mock WorkspaceContext.fromHookContext to return our mock with evolution
99
- vi.mocked(WorkspaceContext.fromHookContext).mockReturnValue(mockWctx as any);
100
- vi.mocked(riskCalculator.assessRiskLevel).mockReturnValue('LOW');
101
- vi.mocked(riskCalculator.estimateLineChanges).mockReturnValue(1);
102
- });
103
-
104
- afterEach(() => {
105
- // Clean up PROFILE.json
106
- if (fs.existsSync(PROFILE_PATH)) {
107
- fs.unlinkSync(PROFILE_PATH);
108
- }
109
- });
110
-
111
- describe('Blocking high-risk operations without thinking', () => {
112
- it('should block write tool without recent thinking', () => {
113
- const result = handleBeforeToolCall(
114
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
115
- createMockContext()
116
- );
117
-
118
- expect(result).toBeDefined();
119
- expect(result?.block).toBe(true);
120
- expect(result?.blockReason).toContain('deep_reflect');
121
- });
122
-
123
- it('should block exec tool without recent thinking', () => {
124
- const result = handleBeforeToolCall(
125
- createMockEvent('run_shell_command', { command: 'ls -la' }),
126
- createMockContext()
127
- );
128
-
129
- expect(result).toBeDefined();
130
- expect(result?.block).toBe(true);
131
- });
132
-
133
- it('should block edit tool without recent thinking', () => {
134
- const result = handleBeforeToolCall(
135
- createMockEvent('edit', { file_path: '/test/file.ts', old_string: 'a', new_string: 'b' }),
136
- createMockContext()
137
- );
138
-
139
- expect(result).toBeDefined();
140
- expect(result?.block).toBe(true);
141
- });
142
-
143
- it('should block sessions_spawn without recent thinking', () => {
144
- const result = handleBeforeToolCall(
145
- createMockEvent('sessions_spawn', { task: 'Use pd-explorer skill' }),
146
- createMockContext()
147
- );
148
-
149
- expect(result).toBeDefined();
150
- expect(result?.block).toBe(true);
151
- });
152
- });
153
-
154
- describe('Allowing operations after thinking', () => {
155
- it('should allow write tool after recording thinking checkpoint', () => {
156
- // Record thinking
157
- recordThinkingCheckpoint(MOCK_SESSION_ID, MOCK_WORKSPACE);
158
-
159
- const result = handleBeforeToolCall(
160
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
161
- createMockContext()
162
- );
163
-
164
- // Should not be blocked by thinking gate (may be blocked by other gates like risk path)
165
- // If blocked, the reason should NOT be about thinking checkpoint
166
- if (result?.block) {
167
- expect(result.blockReason).not.toContain('Thinking OS');
168
- }
169
- });
170
-
171
- it('should allow exec tool after recording thinking checkpoint', () => {
172
- recordThinkingCheckpoint(MOCK_SESSION_ID, MOCK_WORKSPACE);
173
-
174
- const result = handleBeforeToolCall(
175
- createMockEvent('run_shell_command', { command: 'echo hello' }),
176
- createMockContext()
177
- );
178
-
179
- if (result?.block) {
180
- expect(result.blockReason).not.toContain('Thinking OS');
181
- }
182
- });
183
- });
184
-
185
- describe('Session state tracking', () => {
186
- it('should initially have no recent thinking', () => {
187
- expect(hasRecentThinking(MOCK_SESSION_ID)).toBe(false);
188
- });
189
-
190
- it('should have recent thinking after recording checkpoint', () => {
191
- recordThinkingCheckpoint(MOCK_SESSION_ID, MOCK_WORKSPACE);
192
- expect(hasRecentThinking(MOCK_SESSION_ID)).toBe(true);
193
- });
194
-
195
- it('should expire after time window passes', async () => {
196
- recordThinkingCheckpoint(MOCK_SESSION_ID, MOCK_WORKSPACE);
197
- expect(hasRecentThinking(MOCK_SESSION_ID, 1000)).toBe(true);
198
- vi.advanceTimersByTime(150);
199
- expect(hasRecentThinking(MOCK_SESSION_ID, 100)).toBe(false);
200
- });
201
- });
202
-
203
- describe('Non-high-risk tools bypass', () => {
204
- it('should not block read tool', () => {
205
- const result = handleBeforeToolCall(
206
- createMockEvent('read', { file_path: '/test/file.ts' }),
207
- createMockContext()
208
- );
209
- expect(result).toBeUndefined();
210
- });
211
-
212
- it('should not block web_search tool', () => {
213
- const result = handleBeforeToolCall(
214
- createMockEvent('web_search', { query: 'test' }),
215
- createMockContext()
216
- );
217
- expect(result).toBeUndefined();
218
- });
219
- });
220
-
221
- describe('Boundary: thinking_checkpoint config variations', () => {
222
- it('should not crash when PROFILE.json is missing', () => {
223
- // Remove PROFILE.json
224
- if (fs.existsSync(PROFILE_PATH)) {
225
- fs.unlinkSync(PROFILE_PATH);
226
- }
227
- const result = handleBeforeToolCall(
228
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
229
- createMockContext()
230
- );
231
- // Should not crash, should use defaults (checkpoint disabled)
232
- expect(result).toBeUndefined();
233
- });
234
-
235
- it('should not crash when thinking_checkpoint is null', () => {
236
- fs.writeFileSync(PROFILE_PATH, JSON.stringify({ thinking_checkpoint: null }));
237
- const result = handleBeforeToolCall(
238
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
239
- createMockContext()
240
- );
241
- expect(result).toBeUndefined();
242
- });
243
-
244
- it('should not crash when thinking_checkpoint.enabled is null', () => {
245
- fs.writeFileSync(PROFILE_PATH, JSON.stringify({ thinking_checkpoint: { enabled: null } }));
246
- const result = handleBeforeToolCall(
247
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
248
- createMockContext()
249
- );
250
- expect(result).toBeUndefined();
251
- });
252
-
253
- it('should not crash when high_risk_tools is null', () => {
254
- fs.writeFileSync(PROFILE_PATH, JSON.stringify({ thinking_checkpoint: { enabled: true, high_risk_tools: null } }));
255
- const result = handleBeforeToolCall(
256
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
257
- createMockContext()
258
- );
259
- // Should use default high_risk_tools list
260
- expect(result).toBeUndefined();
261
- });
262
-
263
- it('should not block any tool when high_risk_tools is empty array', () => {
264
- fs.writeFileSync(PROFILE_PATH, JSON.stringify({ thinking_checkpoint: { enabled: true, high_risk_tools: [] } }));
265
- const result = handleBeforeToolCall(
266
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
267
- createMockContext()
268
- );
269
- // Empty list = no tools are high risk
270
- expect(result).toBeUndefined();
271
- });
272
-
273
- it('should not crash when thinking_checkpoint is invalid type (string)', () => {
274
- fs.writeFileSync(PROFILE_PATH, JSON.stringify({ thinking_checkpoint: "invalid" }));
275
- const result = handleBeforeToolCall(
276
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
277
- createMockContext()
278
- );
279
- expect(result).toBeUndefined();
280
- });
281
-
282
- it('should not crash when PROFILE.json is malformed JSON', () => {
283
- fs.writeFileSync(PROFILE_PATH, '{ invalid json }');
284
- const result = handleBeforeToolCall(
285
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
286
- createMockContext()
287
- );
288
- // Should fall back to defaults
289
- expect(result).toBeUndefined();
290
- });
291
-
292
- it('should respect custom high_risk_tools list', () => {
293
- fs.writeFileSync(PROFILE_PATH, JSON.stringify({ thinking_checkpoint: { enabled: true, high_risk_tools: ['write'] } }));
294
- const result = handleBeforeToolCall(
295
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
296
- createMockContext()
297
- );
298
- // write is in the list, should be blocked
299
- expect(result).toBeDefined();
300
- expect(result?.block).toBe(true);
301
- });
302
-
303
- it('should not block tool not in custom high_risk_tools list', () => {
304
- fs.writeFileSync(PROFILE_PATH, JSON.stringify({ thinking_checkpoint: { enabled: true, high_risk_tools: ['delete_file'] } }));
305
- const result = handleBeforeToolCall(
306
- createMockEvent('write', { file_path: '/test/file.ts', content: 'test' }),
307
- createMockContext()
308
- );
309
- // write is NOT in the list, should not be blocked
310
- expect(result).toBeUndefined();
311
- });
312
- });
313
- });