-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathskill-learner.ts
More file actions
414 lines (357 loc) · 13.7 KB
/
skill-learner.ts
File metadata and controls
414 lines (357 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { readFile, writeFile, mkdir, readdir, rm } from "fs/promises";
import { join } from "path";
import { execFileSync } from "child_process";
import type { AgentTool } from "@mariozechner/pi-agent-core";
import { skillLearnerSchema } from "./shared.js";
import { loadSkillStats, isSkillFlagged } from "../learning/reinforcement.js";
import type { TaskRecord } from "./task-tracker.js";
import yaml from "js-yaml";
// ── Helpers ─────────────────────────────────────────────────────────────
interface TasksStore {
tasks: TaskRecord[];
}
async function loadTasks(gitagentDir: string): Promise<TasksStore> {
const tasksFile = join(gitagentDir, "learning", "tasks.json");
try {
const raw = await readFile(tasksFile, "utf-8");
return JSON.parse(raw) as TasksStore;
} catch {
return { tasks: [] };
}
}
function extractKeywords(text: string): string[] {
return text
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, "")
.split(/\s+/)
.filter((w) => w.length > 2);
}
function jaccardSimilarity(a: string[], b: string[]): number {
const setA = new Set(a);
const setB = new Set(b);
const intersection = [...setA].filter((x) => setB.has(x)).length;
const union = new Set([...setA, ...setB]).size;
return union === 0 ? 0 : intersection / union;
}
// Checks if a step looks project-specific (absolute paths, UUIDs, etc.)
function isProjectSpecific(step: string): boolean {
const patterns = [
/\/[a-zA-Z][\w/.-]{5,}/, // absolute-ish paths
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i, // UUID
/[A-Z][a-z]+(?:[A-Z][a-z]+){2,}/, // PascalCase with 3+ parts (likely project-specific class)
];
return patterns.some((p) => p.test(step));
}
async function getExistingSkillDescriptions(agentDir: string): Promise<Array<{ name: string; keywords: string[] }>> {
const skillsDir = join(agentDir, "skills");
const result: Array<{ name: string; keywords: string[] }> = [];
let entries;
try {
entries = await readdir(skillsDir, { withFileTypes: true });
} catch {
return [];
}
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const skillFile = join(skillsDir, entry.name, "SKILL.md");
try {
const content = await readFile(skillFile, "utf-8");
const fmMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
if (!fmMatch) continue;
const fm = yaml.load(fmMatch[1]) as Record<string, any>;
if (fm.description) {
result.push({
name: fm.name as string,
keywords: extractKeywords(fm.description as string),
});
}
} catch {
continue;
}
}
return result;
}
function gitCommit(agentDir: string, files: string[], message: string): void {
try {
for (const f of files) {
execFileSync("git", ["add", f], { cwd: agentDir, stdio: "pipe" });
}
execFileSync("git", ["commit", "-m", message], {
cwd: agentDir,
stdio: "pipe",
});
} catch {
// Not fatal — file was still written
}
}
// ── Tool factory ────────────────────────────────────────────────────────
export function createSkillLearnerTool(agentDir: string, gitagentDir: string): AgentTool<typeof skillLearnerSchema> {
return {
name: "skill_learner",
label: "skill_learner",
description:
"Learn from successful tasks. Use 'evaluate' to check if a completed task is worth saving as a skill, 'crystallize' to save it, 'status' to list all skills with confidence scores, 'review' to see flagged low-confidence skills, 'update' to modify a skill, 'delete' to remove one.",
parameters: skillLearnerSchema,
execute: async (
_toolCallId: string,
params: {
action: string;
task_id?: string;
skill_name?: string;
skill_description?: string;
instructions?: string;
override_heuristic?: boolean;
},
signal?: AbortSignal,
) => {
if (signal?.aborted) throw new Error("Operation aborted");
switch (params.action) {
case "evaluate": {
if (!params.task_id) throw new Error("task_id is required for evaluate action");
const store = await loadTasks(gitagentDir);
const task = store.tasks.find((t) => t.id === params.task_id);
if (!task) throw new Error(`Task not found: ${params.task_id}`);
if (task.status !== "succeeded") {
return {
content: [{ type: "text", text: `Task ${params.task_id} did not succeed (status: ${task.status}). Only successful tasks can become skills.` }],
details: undefined,
};
}
// Skill-worthiness heuristic
const checks = {
multi_step: task.steps.length >= 3,
non_trivial: task.steps.length >= 2,
novel: true,
generalizable: true,
};
// Check novelty: no existing skill with >0.5 Jaccard similarity
const taskKeywords = extractKeywords(task.objective);
const existingSkills = await getExistingSkillDescriptions(agentDir);
for (const skill of existingSkills) {
if (jaccardSimilarity(taskKeywords, skill.keywords) > 0.5) {
checks.novel = false;
break;
}
}
// Check generalizability: <30% of steps are project-specific
const specificSteps = task.steps.filter((s) => isProjectSpecific(s.description)).length;
checks.generalizable = specificSteps / Math.max(task.steps.length, 1) < 0.3;
const passCount = Object.values(checks).filter(Boolean).length;
const worthy = params.override_heuristic || passCount >= 3 || (checks.multi_step && checks.novel);
const reasons = [
`Multi-step (${task.steps.length} steps): ${checks.multi_step ? "PASS" : "FAIL"}`,
`Non-trivial: ${checks.non_trivial ? "PASS" : "FAIL"}`,
`Novel: ${checks.novel ? "PASS" : "FAIL"}`,
`Generalizable: ${checks.generalizable ? "PASS" : "FAIL"}`,
];
if (worthy) {
return {
content: [{
type: "text",
text: `Task IS worthy of becoming a skill.\n\nChecks:\n${reasons.join("\n")}\n\nCall skill_learner action "crystallize" with this task_id, a skill_name (kebab-case), and a skill_description.`,
}],
details: { worthy: true, checks },
};
}
return {
content: [{
type: "text",
text: `Task is NOT worthy of becoming a skill (${passCount}/4 checks passed).\n\nChecks:\n${reasons.join("\n")}`,
}],
details: { worthy: false, checks },
};
}
case "crystallize": {
if (!params.task_id) throw new Error("task_id is required for crystallize action");
if (!params.skill_name) throw new Error("skill_name is required for crystallize action");
if (!params.skill_description) throw new Error("skill_description is required for crystallize action");
// Validate kebab-case
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(params.skill_name)) {
throw new Error("skill_name must be kebab-case (e.g., deploy-staging)");
}
const store = await loadTasks(gitagentDir);
const task = store.tasks.find((t) => t.id === params.task_id);
if (!task) throw new Error(`Task not found: ${params.task_id}`);
// SUCCESS GATE
if (task.outcome !== "success") {
throw new Error(`Cannot crystallize failed task. Only successful tasks can become skills.`);
}
// Build SKILL.md
const frontmatter: Record<string, any> = {
name: params.skill_name,
description: params.skill_description,
learned_from: `task:${task.id}`,
learned_at: new Date().toISOString(),
confidence: 1.0,
usage_count: 0,
success_count: 0,
failure_count: 0,
negative_examples: [],
};
const stepsSection = task.steps
.map((s, i) => `${i + 1}. ${s.description}`)
.join("\n");
// Collect negative examples from prior failed attempts with same objective
const priorFailed = store.tasks.filter(
(t) => t.status === "failed" && t.objective === task.objective,
);
let whatDidNotWork = "";
if (priorFailed.length > 0) {
const failureReasons = priorFailed
.filter((t) => t.failure_reason)
.map((t) => `- ${t.failure_reason}`)
.join("\n");
if (failureReasons) {
whatDidNotWork = failureReasons;
}
}
let body = `\n## Steps\n${stepsSection}\n\n## What Worked\nThis approach succeeded on attempt #${task.attempts}.\n`;
if (whatDidNotWork) {
body += `\n## What Did NOT Work\n${whatDidNotWork}\n`;
}
const content = `---\n${yaml.dump(frontmatter, { lineWidth: -1, noRefs: true }).trimEnd()}\n---\n${body}`;
// Write skill
const skillDir = join(agentDir, "skills", params.skill_name);
await mkdir(skillDir, { recursive: true });
const skillFile = join(skillDir, "SKILL.md");
await writeFile(skillFile, content, "utf-8");
// Git commit
gitCommit(agentDir, [`skills/${params.skill_name}/SKILL.md`], `Learn skill: ${params.skill_name}`);
return {
content: [{
type: "text",
text: `Skill "${params.skill_name}" crystallized and committed.\nPath: skills/${params.skill_name}/SKILL.md\nConfidence: 1.0\n\nThe skill is now available via /skill:${params.skill_name}`,
}],
details: { skill_name: params.skill_name, path: skillFile },
};
}
case "status": {
const skillsDir = join(agentDir, "skills");
let entries;
try {
entries = await readdir(skillsDir, { withFileTypes: true });
} catch {
return {
content: [{ type: "text", text: "No skills directory found." }],
details: undefined,
};
}
const skills: Array<{ name: string; confidence: number; usage: number; ratio: string }> = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const dir = join(skillsDir, entry.name);
const stats = await loadSkillStats(dir);
// Only include learned skills (those with stats fields)
skills.push({
name: entry.name,
confidence: stats.confidence,
usage: stats.usage_count,
ratio: `${stats.success_count}/${stats.success_count + stats.failure_count}`,
});
}
if (skills.length === 0) {
return {
content: [{ type: "text", text: "No skills found." }],
details: undefined,
};
}
const lines = skills.map((s) =>
` ${s.name}: confidence=${s.confidence}, usage=${s.usage}, success_ratio=${s.ratio}`,
);
return {
content: [{ type: "text", text: `Skills:\n${lines.join("\n")}` }],
details: { skills },
};
}
case "review": {
const skillsDir = join(agentDir, "skills");
let entries;
try {
entries = await readdir(skillsDir, { withFileTypes: true });
} catch {
return {
content: [{ type: "text", text: "No skills directory found." }],
details: undefined,
};
}
const flagged: Array<{ name: string; confidence: number; negatives: string[] }> = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const dir = join(skillsDir, entry.name);
const stats = await loadSkillStats(dir);
if (isSkillFlagged(stats)) {
flagged.push({
name: entry.name,
confidence: stats.confidence,
negatives: stats.negative_examples,
});
}
}
if (flagged.length === 0) {
return {
content: [{ type: "text", text: "No flagged skills (all confidence >= 0.4)." }],
details: undefined,
};
}
const lines = flagged.map((s) => {
let line = ` ${s.name}: confidence=${s.confidence}`;
if (s.negatives.length > 0) {
line += `\n Failures: ${s.negatives.join("; ")}`;
}
return line;
});
return {
content: [{ type: "text", text: `Flagged skills (confidence < 0.4):\n${lines.join("\n")}\n\nConsider updating or deleting these skills.` }],
details: { flagged },
};
}
case "update": {
if (!params.skill_name) throw new Error("skill_name is required for update action");
if (!params.instructions) throw new Error("instructions is required for update action");
const skillFile = join(agentDir, "skills", params.skill_name, "SKILL.md");
let content: string;
try {
content = await readFile(skillFile, "utf-8");
} catch {
throw new Error(`Skill not found: ${params.skill_name}`);
}
const fmMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);
if (!fmMatch) throw new Error("Invalid SKILL.md format");
const frontmatter = yaml.load(fmMatch[1]) as Record<string, any>;
const yamlStr = yaml.dump(frontmatter, { lineWidth: -1, noRefs: true }).trimEnd();
const updated = `---\n${yamlStr}\n---\n${params.instructions}\n`;
await writeFile(skillFile, updated, "utf-8");
gitCommit(agentDir, [`skills/${params.skill_name}/SKILL.md`], `Update skill: ${params.skill_name}`);
return {
content: [{ type: "text", text: `Skill "${params.skill_name}" updated and committed.` }],
details: undefined,
};
}
case "delete": {
if (!params.skill_name) throw new Error("skill_name is required for delete action");
const skillDir = join(agentDir, "skills", params.skill_name);
try {
await rm(skillDir, { recursive: true });
} catch {
throw new Error(`Skill not found: ${params.skill_name}`);
}
try {
execFileSync("git", ["add", "-A"], { cwd: agentDir, stdio: "pipe" });
execFileSync("git", ["commit", "-m", `Delete skill: ${params.skill_name}`], {
cwd: agentDir,
stdio: "pipe",
});
} catch {
// Not fatal
}
return {
content: [{ type: "text", text: `Skill "${params.skill_name}" deleted.` }],
details: undefined,
};
}
default:
throw new Error(`Unknown action: ${params.action}`);
}
},
};
}