principles-disciple 1.32.0 → 1.34.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 (37) hide show
  1. package/openclaw.plugin.json +4 -4
  2. package/package.json +1 -1
  3. package/src/core/correction-cue-learner.ts +203 -0
  4. package/src/core/correction-types.ts +88 -0
  5. package/src/core/evolution-logger.ts +3 -3
  6. package/src/core/init.ts +67 -0
  7. package/src/service/correction-observer-types.ts +58 -0
  8. package/src/service/correction-observer-workflow-manager.ts +218 -0
  9. package/src/service/evolution-worker.ts +172 -146
  10. package/src/service/nocturnal-service.ts +4 -1
  11. package/src/service/subagent-workflow/index.ts +14 -0
  12. package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +3 -1
  13. package/tests/service/evolution-worker.nocturnal.test.ts +14 -1
  14. package/tests/service/evolution-worker.timeout.test.ts +350 -0
  15. package/tests/commands/implementation-lifecycle.test.ts +0 -362
  16. package/tests/core/detection-funnel.test.ts +0 -63
  17. package/tests/core/evolution-e2e.test.ts +0 -58
  18. package/tests/core/evolution-engine-gate-integration.test.ts +0 -543
  19. package/tests/core/evolution-engine.test.ts +0 -562
  20. package/tests/core/evolution-reducer.test.ts +0 -180
  21. package/tests/core/evolution-user-stories.e2e.test.ts +0 -249
  22. package/tests/core/local-worker-routing.test.ts +0 -757
  23. package/tests/core/rule-host.test.ts +0 -389
  24. package/tests/core/trajectory-correction-pain.test.ts +0 -180
  25. package/tests/hooks/gate-edit-verification.test.ts +0 -435
  26. package/tests/hooks/llm.test.ts +0 -308
  27. package/tests/hooks/progressive-trust-gate.test.ts +0 -277
  28. package/tests/hooks/prompt.test.ts +0 -1473
  29. package/tests/index.integration.test.ts +0 -179
  30. package/tests/index.shadow-routing.integration.test.ts +0 -140
  31. package/tests/service/evolution-worker.test.ts +0 -462
  32. package/tests/service/nocturnal-service.test.ts +0 -577
  33. package/tests/service/nocturnal-workflow-manager.test.ts +0 -441
  34. package/tests/tools/critique-prompt.test.ts +0 -260
  35. package/tests/tools/deep-reflect.test.ts +0 -232
  36. package/tests/tools/model-index.test.ts +0 -246
  37. package/tests/ui/app.test.tsx +0 -114
@@ -1,435 +0,0 @@
1
- /**
2
- * P-03: 精确匹配前验证原则 - Edit Verification Tests
3
- *
4
- * 测试 gate.ts 中的编辑验证功能:
5
- * - 精确匹配
6
- * - 模糊匹配
7
- * - 二进制文件跳过
8
- * - 错误消息生成
9
- */
10
-
11
- import { describe, it, expect, vi, beforeEach } from 'vitest';
12
- import { handleBeforeToolCall } from '../../src/hooks/gate.js';
13
- import * as fs from 'fs';
14
- import * as path from 'path';
15
- import { WorkspaceContext } from '../../src/core/workspace-context.js';
16
-
17
- vi.mock('fs');
18
- vi.mock('../../src/core/workspace-context.js');
19
-
20
- // Mock fs.statSync
21
- const mockStatSync = vi.fn();
22
- vi.mocked(fs.statSync).mockImplementation(mockStatSync);
23
-
24
- describe('P-03 Edit Verification', () => {
25
- const workspaceDir = '/mock/workspace';
26
-
27
- const mockEvent = {
28
- toolName: 'edit',
29
- params: {
30
- file_path: 'src/example.ts',
31
- oldText: 'const x = 1;',
32
- newText: 'const x = 2;',
33
- },
34
- };
35
-
36
- const mockWctx = {
37
- workspaceDir,
38
- resolve: vi.fn().mockImplementation((p) => {
39
- if (p === 'src/example.ts') return path.join(workspaceDir, 'src/example.ts');
40
- return p;
41
- }),
42
- trust: {
43
- getScore: vi.fn().mockReturnValue(75), // Stage 3 (Developer)
44
- getStage: vi.fn().mockReturnValue(3),
45
- },
46
- config: {
47
- get: vi.fn().mockReturnValue({
48
- limits: { stage_2_max_lines: 50, stage_3_max_lines: 300 }
49
- }),
50
- },
51
- };
52
-
53
- const mockCtx = {
54
- workspaceDir,
55
- logger: console,
56
- };
57
-
58
- beforeEach(() => {
59
- vi.clearAllMocks();
60
- vi.mocked(WorkspaceContext.fromHookContext).mockReturnValue(mockWctx as any);
61
- // Mock PROFILE.json to disable Progressive Gate
62
- vi.mocked(fs.existsSync).mockImplementation((p: any) => {
63
- if (typeof p === 'string' && p.includes('PROFILE.json')) {
64
- return true;
65
- }
66
- if (typeof p === 'string' && p.includes('src/example.ts')) {
67
- return true;
68
- }
69
- return false;
70
- });
71
- vi.mocked(fs.readFileSync).mockImplementation((filePath: any) => {
72
- if (typeof filePath === 'string' && filePath.includes('PROFILE.json')) {
73
- return JSON.stringify({
74
- progressive_gate: { enabled: false }, // Disable Progressive Gate for P-03 tests
75
- });
76
- }
77
- return 'mock content'; // Default for source files
78
- });
79
- // Mock fs.statSync to return file stats
80
- mockStatSync.mockReturnValue({ size: 1000 }); // 1KB file by default
81
- });
82
-
83
- describe('Exact Match Verification', () => {
84
- it('should pass when oldText exactly matches current content', () => {
85
- const fileContent = 'function hello() {\n const x = 1;\n console.log("hello");\n}\n';
86
-
87
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
88
-
89
- const result = handleBeforeToolCall(mockEvent as any, { ...mockCtx, ...mockWctx } as any);
90
-
91
- expect(result).toBeUndefined(); // No blocking
92
- expect(fs.readFileSync).toHaveBeenCalledWith(
93
- path.join(workspaceDir, 'src/example.ts'),
94
- 'utf-8'
95
- );
96
- });
97
-
98
- it('should pass when oldText matches with proper newlines', () => {
99
- const fileContent = 'line 1\nline 2\nline 3\n';
100
- const eventWithNewlines = {
101
- ...mockEvent,
102
- params: {
103
- ...mockEvent.params,
104
- oldText: 'line 2\n',
105
- },
106
- };
107
-
108
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
109
-
110
- const result = handleBeforeToolCall(eventWithNewlines as any, { ...mockCtx, ...mockWctx } as any);
111
-
112
- expect(result).toBeUndefined();
113
- });
114
-
115
- it('should pass when oldText matches with proper tabs', () => {
116
- const fileContent = 'function test() {\n\tconsole.log("test");\n}\n';
117
- const eventWithTabs = {
118
- ...mockEvent,
119
- params: {
120
- ...mockEvent.params,
121
- oldText: '\tconsole.log("test");\n',
122
- },
123
- };
124
-
125
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
126
-
127
- const result = handleBeforeToolCall(eventWithTabs as any, { ...mockCtx, ...mockWctx } as any);
128
-
129
- expect(result).toBeUndefined();
130
- });
131
- });
132
-
133
- describe('Fuzzy Matching', () => {
134
- it('should fuzzy match with extra spaces', () => {
135
- const fileContent = 'const x = 1;\n'; // Extra spaces
136
- const event = {
137
- ...mockEvent,
138
- params: {
139
- ...mockEvent.params,
140
- oldText: 'const x = 1;',
141
- },
142
- };
143
-
144
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
145
-
146
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
147
-
148
- // Fuzzy match should auto-correct oldText
149
- expect(result).toBeDefined();
150
- expect(result?.params?.oldText).toBe(fileContent.trim());
151
- expect(result?.params?.old_string).toBe(fileContent.trim());
152
- });
153
-
154
- it('should fuzzy match with different indentation', () => {
155
- const fileContent = 'function hello() {\n console.log("hello");\n}\n'; // 4 spaces
156
- const event = {
157
- ...mockEvent,
158
- params: {
159
- ...mockEvent.params,
160
- oldText: 'function hello() {\n\tconsole.log("hello");\n}', // Tab
161
- },
162
- };
163
-
164
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
165
-
166
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
167
-
168
- expect(result?.params?.oldText).toContain(' console.log("hello")');
169
- });
170
-
171
- it.skip('should fuzzy match with trailing whitespace', () => {
172
- const fileContent = 'const x = 1; \n'; // Trailing spaces
173
- const event = {
174
- ...mockEvent,
175
- params: {
176
- ...mockEvent.params,
177
- oldText: 'const x = 1;',
178
- },
179
- };
180
-
181
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
182
-
183
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
184
-
185
- expect(result).toBeDefined();
186
- });
187
-
188
- it('should NOT fuzzy match when <80% of lines match', () => {
189
- const fileContent = 'function hello() {\n console.log("hello");\n return true;\n}\n';
190
- const event = {
191
- ...mockEvent,
192
- params: {
193
- ...mockEvent.params,
194
- oldText: 'completely different text that has no relation', // No match
195
- },
196
- };
197
-
198
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
199
-
200
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
201
-
202
- expect(result?.block).toBe(true);
203
- expect(result?.blockReason).toContain('Edit verification failed');
204
- });
205
-
206
- it.skip('should extract actual text for fuzzy match', () => {
207
- const fileContent = 'const x = 1;\n';
208
- const event = {
209
- ...mockEvent,
210
- params: {
211
- ...mockEvent.params,
212
- oldText: 'const x = 1', // Missing semicolon
213
- },
214
- };
215
-
216
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
217
-
218
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
219
-
220
- // Should pass (fuzzy match found and corrected)
221
- expect(result).toBeDefined();
222
- expect(result?.block).toBeFalsy();
223
- });
224
- });
225
-
226
- describe('Error Cases', () => {
227
- it('should block when oldText not found (no fuzzy match)', () => {
228
- const fileContent = 'function hello() {\n console.log("hello");\n}\n';
229
- const event = {
230
- ...mockEvent,
231
- params: {
232
- ...mockEvent.params,
233
- oldText: 'completely different code',
234
- },
235
- };
236
-
237
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
238
-
239
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
240
-
241
- expect(result).toBeDefined();
242
- expect(result?.block).toBe(true);
243
- expect(result?.blockReason).toContain('[P-03 Violation]');
244
- });
245
-
246
- it('should block when file does not exist', () => {
247
- vi.mocked(fs.readFileSync).mockImplementation(() => {
248
- throw new Error('ENOENT: no such file');
249
- });
250
-
251
- const result = handleBeforeToolCall(mockEvent as any, { ...mockCtx, ...mockWctx } as any);
252
-
253
- expect(result).toBeUndefined(); // Let it fail naturally
254
- });
255
-
256
- it('should block when oldText is empty', () => {
257
- const event = {
258
- ...mockEvent,
259
- params: {
260
- file_path: 'src/example.ts',
261
- oldText: '', // Empty oldText
262
- newText: 'new content',
263
- },
264
- };
265
-
266
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
267
-
268
- expect(result).toBeUndefined(); // Let it fail naturally
269
- });
270
-
271
- it('should provide helpful error messages', () => {
272
- const fileContent = 'function hello() {\n console.log("hello");\n}\n';
273
- const event = {
274
- ...mockEvent,
275
- params: {
276
- ...mockEvent.params,
277
- oldText: 'wrong text',
278
- },
279
- };
280
-
281
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
282
-
283
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
284
-
285
- expect(result?.blockReason).toContain('[P-03 Violation]');
286
- expect(result?.blockReason).toContain('Solution:');
287
- });
288
- });
289
-
290
- describe('Edge Cases', () => {
291
- it('should skip verification for binary files (PNG)', () => {
292
- const event = {
293
- ...mockEvent,
294
- params: {
295
- ...mockEvent.params,
296
- file_path: 'assets/image.png',
297
- },
298
- };
299
-
300
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
301
-
302
- expect(result).toBeUndefined(); // Binary files should skip verification
303
- });
304
-
305
- it('should skip verification for binary files (PDF)', () => {
306
- const event = {
307
- ...mockEvent,
308
- params: {
309
- ...mockEvent.params,
310
- file_path: 'docs/report.pdf',
311
- },
312
- };
313
-
314
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
315
-
316
- expect(result).toBeUndefined(); // Binary files should skip verification
317
- });
318
-
319
- it('should skip verification for binary files (ZIP)', () => {
320
- const event = {
321
- ...mockEvent,
322
- params: {
323
- ...mockEvent.params,
324
- file_path: 'dist/package.zip',
325
- },
326
- };
327
-
328
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
329
-
330
- expect(result).toBeUndefined(); // Binary files should skip verification
331
- });
332
-
333
- it('should handle special characters correctly', () => {
334
- const fileContent = 'const emoji = "😀🎉";\nconst unicode = "中文";\nconst symbols = "@#$%^&*";\n';
335
- const event = {
336
- ...mockEvent,
337
- params: {
338
- ...mockEvent.params,
339
- oldText: 'const emoji = "😀🎉";',
340
- },
341
- };
342
-
343
- vi.mocked(fs.readFileSync).mockReturnValue(fileContent);
344
-
345
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
346
-
347
- expect(result).toBeUndefined();
348
- });
349
-
350
- it('should handle concurrent edits gracefully', () => {
351
- // First call: file contains 'const x = 1;'
352
- vi.mocked(fs.readFileSync).mockReturnValue('const x = 1;\n');
353
-
354
- const event1 = { ...mockEvent, params: { ...mockEvent.params, oldText: 'const x = 1;' } };
355
- const result1 = handleBeforeToolCall(event1 as any, { ...mockCtx, ...mockWctx } as any);
356
-
357
- // Second call: file still contains 'const x = 1;' (but trying to edit 'const x = 2;' which doesn't exist)
358
- const event2 = { ...mockEvent, params: { ...mockEvent.params, oldText: 'const x = 2;' } };
359
- const result2 = handleBeforeToolCall(event2 as any, { ...mockCtx, ...mockWctx } as any);
360
-
361
- expect(result1).toBeUndefined(); // First edit matches exactly
362
- expect(result2?.block).toBe(true); // Second edit doesn't match
363
- });
364
- });
365
-
366
- describe('Parameter Extraction', () => {
367
- it('should extract filePath from file_path parameter', () => {
368
- const event = {
369
- ...mockEvent,
370
- params: {
371
- file_path: 'src/test.ts',
372
- oldText: 'old',
373
- newText: 'new',
374
- },
375
- };
376
-
377
- vi.mocked(fs.readFileSync).mockReturnValue('old\n');
378
-
379
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
380
-
381
- expect(result).toBeUndefined();
382
- });
383
-
384
- it('should extract filePath from path parameter', () => {
385
- const event = {
386
- ...mockEvent,
387
- params: {
388
- path: 'src/test.ts', // Using 'path' instead of 'file_path'
389
- oldText: 'old',
390
- newText: 'new',
391
- },
392
- };
393
-
394
- vi.mocked(fs.readFileSync).mockReturnValue('old\n');
395
-
396
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
397
-
398
- expect(result).toBeUndefined();
399
- });
400
-
401
- it('should extract filePath from file parameter', () => {
402
- const event = {
403
- ...mockEvent,
404
- params: {
405
- file: 'src/test.ts', // Using 'file' instead of 'file_path'
406
- oldText: 'old',
407
- newText: 'new',
408
- },
409
- };
410
-
411
- vi.mocked(fs.readFileSync).mockReturnValue('old\n');
412
-
413
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
414
-
415
- expect(result).toBeUndefined();
416
- });
417
-
418
- it('should extract oldText from old_string parameter', () => {
419
- const event = {
420
- ...mockEvent,
421
- params: {
422
- file_path: 'src/test.ts',
423
- old_string: 'old', // Using 'old_string' instead of 'oldText'
424
- newText: 'new',
425
- },
426
- };
427
-
428
- vi.mocked(fs.readFileSync).mockReturnValue('old\n');
429
-
430
- const result = handleBeforeToolCall(event as any, { ...mockCtx, ...mockWctx } as any);
431
-
432
- expect(result).toBeUndefined();
433
- });
434
- });
435
- });