principles-disciple 1.26.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.
- package/openclaw.plugin.json +4 -4
- package/package.json +1 -1
- package/scripts/diagnose-nocturnal.mjs +139 -2
- package/scripts/seed-nocturnal-scenarios.mjs +377 -0
- package/src/core/nocturnal-trinity.ts +8 -7
- package/src/core/trajectory.ts +74 -10
- package/src/index.ts +2 -0
- package/src/service/evolution-worker.ts +137 -43
- package/src/tools/write-pain-flag.ts +191 -0
- package/templates/langs/en/skills/pd-pain-signal/SKILL.md +34 -20
- package/templates/langs/zh/skills/pd-pain-signal/SKILL.md +34 -20
- package/tests/core/nocturnal-e2e.test.ts +224 -0
- package/tests/core/trajectory-correction-pain.test.ts +180 -0
- package/tests/tools/write-pain-flag.test.ts +240 -0
|
@@ -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
|
+
});
|