principles-disciple 1.27.0 → 1.28.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,224 @@
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import * as fs from 'fs';
3
+ import * as os from 'os';
4
+ import * as path from 'path';
5
+ import { TrajectoryDatabase } from '../../src/core/trajectory.js';
6
+ import { NocturnalTrajectoryExtractor } from '../../src/core/nocturnal-trajectory-extractor.js';
7
+ import { detectViolation } from '../../src/core/nocturnal-compliance.js';
8
+
9
+ function safeRmDir(dir: string): void {
10
+ try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
11
+ }
12
+
13
+ // ─────────────────────────────────────────────────────────
14
+ // Phase 4a: Correction rejected → pain event → nocturnal selection
15
+ // ─────────────────────────────────────────────────────────
16
+ describe('Phase 4a: Correction rejected integration', () => {
17
+ let workspaceDir: string;
18
+ let trajectory: TrajectoryDatabase;
19
+
20
+ beforeEach(() => {
21
+ workspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-e2e-correction-'));
22
+ trajectory = new TrajectoryDatabase({ workspaceDir });
23
+ });
24
+
25
+ afterEach(() => {
26
+ trajectory?.dispose();
27
+ safeRmDir(workspaceDir);
28
+ });
29
+
30
+ it('rejected correction creates a pain event with source=correction_rejected', () => {
31
+ // 1. Create session + correction sample
32
+ trajectory.recordSession({ sessionId: 'corr-session', startedAt: new Date().toISOString() });
33
+ const atId = trajectory.recordAssistantTurn({
34
+ sessionId: 'corr-session', runId: 'run-1', provider: 'local', model: 'main',
35
+ rawText: 'Here is my code', sanitizedText: 'Here is my code', usageJson: {}, empathySignalJson: {},
36
+ createdAt: new Date().toISOString(),
37
+ });
38
+ trajectory.recordUserTurn({
39
+ sessionId: 'corr-session', turnIndex: 1, rawText: 'This is wrong!',
40
+ correctionDetected: true, correctionCue: '错了',
41
+ referencesAssistantTurnId: atId, createdAt: new Date().toISOString(),
42
+ });
43
+
44
+ // Verify sample was created
45
+ const samples = trajectory.listCorrectionSamples('pending');
46
+ expect(samples.length).toBe(1);
47
+
48
+ // 2. Reject the sample
49
+ trajectory.reviewCorrectionSample(samples[0].sampleId, 'rejected', 'Bad approach');
50
+
51
+ // 3. Verify pain event was created
52
+ const painEvents = trajectory.listPainEventsForSession('corr-session');
53
+ const correctionPain = painEvents.find(e => e.source === 'correction_rejected');
54
+ expect(correctionPain).toBeDefined();
55
+ expect(correctionPain!.score).toBeGreaterThanOrEqual(0);
56
+ expect(correctionPain!.score).toBeLessThanOrEqual(100);
57
+ });
58
+
59
+ it('approved correction does NOT create a pain event', () => {
60
+ trajectory.recordSession({ sessionId: 'approved-session', startedAt: new Date().toISOString() });
61
+ const atId = trajectory.recordAssistantTurn({
62
+ sessionId: 'approved-session', runId: 'run-2', provider: 'local', model: 'main',
63
+ rawText: 'Good code', sanitizedText: 'Good code', usageJson: {}, empathySignalJson: {},
64
+ createdAt: new Date().toISOString(),
65
+ });
66
+ trajectory.recordUserTurn({
67
+ sessionId: 'approved-session', turnIndex: 1, rawText: 'Looks better',
68
+ correctionDetected: true, correctionCue: '改进',
69
+ referencesAssistantTurnId: atId, createdAt: new Date().toISOString(),
70
+ });
71
+
72
+ const samples = trajectory.listCorrectionSamples('pending');
73
+ expect(samples.length).toBe(1);
74
+
75
+ trajectory.reviewCorrectionSample(samples[0].sampleId, 'approved', 'Good');
76
+
77
+ const painEvents = trajectory.listPainEventsForSession('approved-session');
78
+ const correctionPain = painEvents.find(e => e.source === 'correction_rejected');
79
+ expect(correctionPain).toBeUndefined();
80
+ });
81
+ });
82
+
83
+ // ─────────────────────────────────────────────────────────
84
+ // Phase 4b: Gate block + pain multi-signal test
85
+ // ─────────────────────────────────────────────────────────
86
+ describe('Phase 4b: Multi-signal session selection', () => {
87
+ let workspaceDir: string;
88
+ let trajectory: TrajectoryDatabase;
89
+
90
+ beforeEach(() => {
91
+ workspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-e2e-multisignal-'));
92
+ trajectory = new TrajectoryDatabase({ workspaceDir });
93
+ });
94
+
95
+ afterEach(() => {
96
+ trajectory?.dispose();
97
+ safeRmDir(workspaceDir);
98
+ });
99
+
100
+ it('session with more failures has higher violation density', () => {
101
+ // Create session A: just 1 failure
102
+ trajectory.recordSession({ sessionId: 'session-a-pain-only', startedAt: new Date().toISOString() });
103
+ const atIdA = trajectory.recordAssistantTurn({
104
+ sessionId: 'session-a-pain-only', runId: 'run-a', provider: 'local', model: 'main',
105
+ rawText: 'Code here', sanitizedText: 'Code here', usageJson: {}, empathySignalJson: {},
106
+ createdAt: new Date().toISOString(),
107
+ });
108
+ trajectory.recordUserTurn({
109
+ sessionId: 'session-a-pain-only', turnIndex: 1, rawText: '错了',
110
+ correctionDetected: true, correctionCue: '错了',
111
+ referencesAssistantTurnId: atIdA, createdAt: new Date().toISOString(),
112
+ });
113
+ trajectory.recordToolCall({
114
+ sessionId: 'session-a-pain-only', toolName: 'write', outcome: 'failure',
115
+ errorMessage: 'Write failed', errorType: 'Error', createdAt: new Date().toISOString(),
116
+ });
117
+
118
+ // Create session B: 2 failures
119
+ trajectory.recordSession({ sessionId: 'session-b-multi', startedAt: new Date().toISOString() });
120
+ const atIdB = trajectory.recordAssistantTurn({
121
+ sessionId: 'session-b-multi', runId: 'run-b', provider: 'local', model: 'main',
122
+ rawText: 'Code here', sanitizedText: 'Code here', usageJson: {}, empathySignalJson: {},
123
+ createdAt: new Date().toISOString(),
124
+ });
125
+ trajectory.recordUserTurn({
126
+ sessionId: 'session-b-multi', turnIndex: 1, rawText: '太复杂了',
127
+ correctionDetected: true, correctionCue: '太复杂了',
128
+ referencesAssistantTurnId: atIdB, createdAt: new Date().toISOString(),
129
+ });
130
+ trajectory.recordToolCall({
131
+ sessionId: 'session-b-multi', toolName: 'edit', outcome: 'failure',
132
+ errorMessage: 'Edit failed', errorType: 'Error', createdAt: new Date().toISOString(),
133
+ });
134
+ trajectory.recordToolCall({
135
+ sessionId: 'session-b-multi', toolName: 'write', outcome: 'failure',
136
+ errorMessage: 'Write failed too', errorType: 'Error', createdAt: new Date().toISOString(),
137
+ });
138
+
139
+ // Verify session B has more failure signals
140
+ const extractor = new NocturnalTrajectoryExtractor(trajectory);
141
+ const snapshotA = extractor.getNocturnalSessionSnapshot('session-a-pain-only');
142
+ const snapshotB = extractor.getNocturnalSessionSnapshot('session-b-multi');
143
+
144
+ expect(snapshotA).not.toBeNull();
145
+ expect(snapshotB).not.toBeNull();
146
+
147
+ // Session B should have more violation signals
148
+ const densityA = (snapshotA!.stats.failureCount ?? 0) + (snapshotA!.stats.totalPainEvents ?? 0) * 0.5;
149
+ const densityB = (snapshotB!.stats.failureCount ?? 0) + (snapshotB!.stats.totalPainEvents ?? 0) * 0.5;
150
+ expect(densityB).toBeGreaterThan(densityA);
151
+ });
152
+ });
153
+
154
+ // ─────────────────────────────────────────────────────────
155
+ // Phase 4c: Boundary value test matrix
156
+ // ─────────────────────────────────────────────────────────
157
+ describe('Phase 4c: Boundary value tests', () => {
158
+ let workspaceDir: string;
159
+ let trajectory: TrajectoryDatabase;
160
+
161
+ beforeEach(() => {
162
+ workspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-e2e-boundary-'));
163
+ trajectory = new TrajectoryDatabase({ workspaceDir });
164
+ });
165
+
166
+ afterEach(() => {
167
+ trajectory?.dispose();
168
+ safeRmDir(workspaceDir);
169
+ });
170
+
171
+ it('session with correction cue is listed as candidate', () => {
172
+ trajectory.recordSession({ sessionId: 'single-pain', startedAt: new Date().toISOString() });
173
+ const atIdC = trajectory.recordAssistantTurn({
174
+ sessionId: 'single-pain', runId: 'run-c', provider: 'local', model: 'main',
175
+ rawText: 'Agent response', sanitizedText: 'Agent response', usageJson: {}, empathySignalJson: {},
176
+ createdAt: new Date().toISOString(),
177
+ });
178
+ trajectory.recordUserTurn({
179
+ sessionId: 'single-pain', turnIndex: 1, rawText: '错了',
180
+ correctionDetected: true, correctionCue: '错了',
181
+ referencesAssistantTurnId: atIdC, createdAt: new Date().toISOString(),
182
+ });
183
+
184
+ const extractor = new NocturnalTrajectoryExtractor(trajectory);
185
+ const candidates = extractor.listRecentNocturnalCandidateSessions({ limit: 10, minToolCalls: 0 });
186
+
187
+ const painCandidate = candidates.find(c => c.sessionId === 'single-pain');
188
+ expect(painCandidate).toBeDefined();
189
+ });
190
+
191
+ it('detectViolation returns violated for P_* principles with tool failure', () => {
192
+ trajectory.recordSession({ sessionId: 'violation-session', startedAt: new Date().toISOString() });
193
+ trajectory.recordAssistantTurn({
194
+ sessionId: 'violation-session', runId: 'run-d', provider: 'local', model: 'main',
195
+ rawText: 'Code', sanitizedText: 'Code', usageJson: {}, empathySignalJson: {},
196
+ createdAt: new Date().toISOString(),
197
+ });
198
+ trajectory.recordToolCall({
199
+ sessionId: 'violation-session', toolName: 'write', outcome: 'failure',
200
+ errorMessage: 'Failed', errorType: 'Error', createdAt: new Date().toISOString(),
201
+ });
202
+
203
+ const extractor = new NocturnalTrajectoryExtractor(trajectory);
204
+ const snapshot = extractor.getNocturnalSessionSnapshot('violation-session');
205
+ expect(snapshot).not.toBeNull();
206
+
207
+ // P_* principles should be violated with any failure
208
+ const violation = detectViolation('P_001', {
209
+ sessionId: 'violation-session',
210
+ toolCalls: snapshot!.toolCalls.map(tc => ({
211
+ toolName: tc.toolName, outcome: tc.outcome as 'success' | 'failure' | 'blocked',
212
+ errorMessage: tc.errorMessage ?? undefined,
213
+ })),
214
+ painSignals: snapshot!.painEvents.map(pe => ({
215
+ source: pe.source, score: pe.score, severity: pe.severity as 'mild' | 'moderate' | 'severe' | undefined,
216
+ })),
217
+ gateBlocks: [],
218
+ userCorrections: [],
219
+ planApprovals: [],
220
+ });
221
+
222
+ expect(violation.violated).toBe(true);
223
+ });
224
+ });
@@ -0,0 +1,240 @@
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import * as fs from 'fs';
3
+ import * as os from 'os';
4
+ import * as path from 'path';
5
+ import { createWritePainFlagTool } from '../../src/tools/write-pain-flag.js';
6
+
7
+ function safeRmDir(dir: string): void {
8
+ try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
9
+ }
10
+
11
+ /**
12
+ * Create a mock OpenClawPluginApi for testing
13
+ */
14
+ function createMockApi(workspaceDir: string) {
15
+ const logs: { level: string; message: string }[] = [];
16
+ return {
17
+ config: { workspaceDir },
18
+ logger: {
19
+ info: (m: string) => logs.push({ level: 'info', message: m }),
20
+ warn: (m: string) => logs.push({ level: 'warn', message: m }),
21
+ error: (m: string) => logs.push({ level: 'error', message: m }),
22
+ debug: (m: string) => logs.push({ level: 'debug', message: m }),
23
+ },
24
+ runtime: { subagent: null, agent: null },
25
+ _logs: logs,
26
+ };
27
+ }
28
+
29
+ describe('write_pain_flag tool', () => {
30
+ let workspaceDir: string;
31
+ let stateDir: string;
32
+
33
+ beforeEach(() => {
34
+ workspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-pain-tool-'));
35
+ stateDir = path.join(workspaceDir, '.state');
36
+ fs.mkdirSync(stateDir, { recursive: true });
37
+ });
38
+
39
+ afterEach(() => {
40
+ safeRmDir(workspaceDir);
41
+ });
42
+
43
+ // ─────────────────────────────────────────────────────────
44
+ // Contract: Tool interface matches OpenClaw AgentTool spec
45
+ // ─────────────────────────────────────────────────────────
46
+ it('matches the AgentTool interface (name, description, parameters, execute)', () => {
47
+ const api = createMockApi(workspaceDir) as any;
48
+ const tool = createWritePainFlagTool(api);
49
+
50
+ expect(tool.name).toBe('write_pain_flag');
51
+ expect(tool.description).toBeDefined();
52
+ expect(tool.description).toContain('pain signal');
53
+ expect(tool.parameters).toBeDefined();
54
+ expect(typeof tool.execute).toBe('function');
55
+ });
56
+
57
+ it('has clear, unambiguous description in English', () => {
58
+ const api = createMockApi(workspaceDir) as any;
59
+ const tool = createWritePainFlagTool(api);
60
+
61
+ expect(tool.description).not.toBe('');
62
+ expect(tool.description).toContain('pain signal');
63
+ expect(tool.description).toContain('INSTEAD');
64
+ expect(tool.description).toContain('.pain_flag');
65
+ });
66
+
67
+ // ─────────────────────────────────────────────────────────
68
+ // Contract: Pain flag format (KV, not JSON, not [object Object])
69
+ // ─────────────────────────────────────────────────────────
70
+ it('writes pain flag in correct KV format, never [object Object]', async () => {
71
+ const api = createMockApi(workspaceDir) as any;
72
+ const tool = createWritePainFlagTool(api);
73
+
74
+ const result = await tool.execute('test-1', {
75
+ reason: 'Agent forgot to read file before editing',
76
+ score: 85,
77
+ source: 'manual',
78
+ });
79
+
80
+ // Verify tool returns success
81
+ expect(result.content[0].text).toContain('✅');
82
+ expect(result.content[0].text).toContain('85');
83
+ expect(result.content[0].text).toContain('manual');
84
+
85
+ // Verify file exists and is NOT [object Object]
86
+ const painFlagPath = path.join(stateDir, '.pain_flag');
87
+ expect(fs.existsSync(painFlagPath)).toBe(true);
88
+ const content = fs.readFileSync(painFlagPath, 'utf-8');
89
+ expect(content).not.toContain('[object Object]');
90
+ expect(content).not.toContain('{');
91
+ expect(content).not.toContain('undefined');
92
+
93
+ // Verify KV format — each line is "key: value"
94
+ const lines = content.trim().split('\n').filter(l => l.trim());
95
+ for (const line of lines) {
96
+ expect(line).toMatch(/^[a-z_]+: .+$/);
97
+ }
98
+
99
+ // Verify required fields
100
+ const fields: Record<string, string> = {};
101
+ for (const line of lines) {
102
+ const colonIdx = line.indexOf(':');
103
+ fields[line.substring(0, colonIdx).trim()] = line.substring(colonIdx + 1).trim();
104
+ }
105
+ expect(fields.source).toBe('manual');
106
+ expect(fields.score).toBe('85');
107
+ expect(fields.reason).toBe('Agent forgot to read file before editing');
108
+ expect(fields.time).toBeDefined();
109
+ });
110
+
111
+ // ─────────────────────────────────────────────────────────
112
+ // Contract: Required field validation
113
+ // ─────────────────────────────────────────────────────────
114
+ it('returns clear error when reason is missing', async () => {
115
+ const api = createMockApi(workspaceDir) as any;
116
+ const tool = createWritePainFlagTool(api);
117
+
118
+ const result = await tool.execute('test-2', {});
119
+
120
+ expect(result.content[0].text).toContain('❌');
121
+ expect(result.content[0].text).toContain('reason');
122
+ expect(api._logs.some((l: any) => l.level === 'warn')).toBe(true);
123
+ });
124
+
125
+ it('returns clear error when workspace cannot be resolved', async () => {
126
+ const api = createMockApi('') as any;
127
+ const tool = createWritePainFlagTool(api);
128
+
129
+ const result = await tool.execute('test-3', { reason: 'Test error' });
130
+
131
+ expect(result.content[0].text).toContain('❌');
132
+ expect(result.content[0].text).toContain('workspace');
133
+ expect(api._logs.some((l: any) => l.level === 'error')).toBe(true);
134
+ });
135
+
136
+ // ─────────────────────────────────────────────────────────
137
+ // Atomic write: temp file + rename
138
+ // ─────────────────────────────────────────────────────────
139
+ it('writes atomically (no partial writes on crash)', async () => {
140
+ const api = createMockApi(workspaceDir) as any;
141
+ const tool = createWritePainFlagTool(api);
142
+
143
+ // First write
144
+ await tool.execute('test-4a', { reason: 'First pain signal', score: 50 });
145
+ const painFlagPath = path.join(stateDir, '.pain_flag');
146
+ const content1 = fs.readFileSync(painFlagPath, 'utf-8');
147
+ expect(content1).toContain('First pain signal');
148
+
149
+ // Verify no temp files left
150
+ const files = fs.readdirSync(stateDir);
151
+ const tempFiles = files.filter(f => f.startsWith('.pain_flag.tmp'));
152
+ expect(tempFiles.length).toBe(0);
153
+
154
+ // Second write (overwrites)
155
+ await tool.execute('test-4b', { reason: 'Second pain signal', score: 70 });
156
+ const content2 = fs.readFileSync(painFlagPath, 'utf-8');
157
+ expect(content2).toContain('Second pain signal');
158
+ expect(content2).not.toContain('First pain signal');
159
+
160
+ // Verify no temp files left
161
+ const files2 = fs.readdirSync(stateDir);
162
+ const tempFiles2 = files2.filter(f => f.startsWith('.pain_flag.tmp'));
163
+ expect(tempFiles2.length).toBe(0);
164
+ });
165
+
166
+ // ─────────────────────────────────────────────────────────
167
+ // Score clamping (0-100)
168
+ // ─────────────────────────────────────────────────────────
169
+ it('clamps score to 0-100 range', async () => {
170
+ const api = createMockApi(workspaceDir) as any;
171
+ const tool = createWritePainFlagTool(api);
172
+
173
+ // Score too high
174
+ const result1 = await tool.execute('test-5a', { reason: 'Test', score: 200 });
175
+ expect(result1.content[0].text).toContain('100');
176
+
177
+ // Score too low
178
+ const result2 = await tool.execute('test-5b', { reason: 'Test', score: -50 });
179
+ expect(result2.content[0].text).toContain('0');
180
+
181
+ // Score at boundaries
182
+ const result3 = await tool.execute('test-5c', { reason: 'Test', score: 0 });
183
+ expect(result3.content[0].text).toContain('0');
184
+
185
+ const result4 = await tool.execute('test-5d', { reason: 'Test', score: 100 });
186
+ expect(result4.content[0].text).toContain('100');
187
+ });
188
+
189
+ // ─────────────────────────────────────────────────────────
190
+ // Agent feedback: clear success/failure messages
191
+ // ─────────────────────────────────────────────────────────
192
+ it('provides clear success feedback with all details', async () => {
193
+ const api = createMockApi(workspaceDir) as any;
194
+ const tool = createWritePainFlagTool(api);
195
+
196
+ const result = await tool.execute('test-6', {
197
+ reason: 'Test error for feedback',
198
+ score: 75,
199
+ source: 'tool_failure',
200
+ is_risky: true,
201
+ });
202
+
203
+ const text = result.content[0].text;
204
+ expect(text).toContain('✅');
205
+ expect(text).toContain('Test error for feedback');
206
+ expect(text).toContain('75');
207
+ expect(text).toContain('tool_failure');
208
+ expect(text).toContain('Yes'); // is_risky
209
+ expect(text).toContain('heartbeat');
210
+ });
211
+
212
+ it('provides clear failure feedback with error message', async () => {
213
+ const api = createMockApi(workspaceDir) as any;
214
+ // Simulate workspace resolution failure by removing the config
215
+ (api as any).config = {};
216
+
217
+ const tool = createWritePainFlagTool(api);
218
+ const result = await tool.execute('test-7', { reason: 'Should fail' });
219
+
220
+ expect(result.content[0].text).toContain('❌');
221
+ expect(result.content[0].text).toContain('workspace');
222
+ });
223
+
224
+ // ─────────────────────────────────────────────────────────
225
+ // Default values
226
+ // ─────────────────────────────────────────────────────────
227
+ it('uses correct defaults for optional parameters', async () => {
228
+ const api = createMockApi(workspaceDir) as any;
229
+ const tool = createWritePainFlagTool(api);
230
+
231
+ const result = await tool.execute('test-8', {
232
+ reason: 'Test defaults',
233
+ });
234
+
235
+ const text = result.content[0].text;
236
+ expect(text).toContain('80'); // default score
237
+ expect(text).toContain('manual'); // default source
238
+ expect(text).toContain('No'); // default is_risky
239
+ });
240
+ });