-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlessons.test.ts
More file actions
210 lines (177 loc) · 9.53 KB
/
Copy pathlessons.test.ts
File metadata and controls
210 lines (177 loc) · 9.53 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
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { LessonExtractor } from "../src/history/lesson-extractor.js";
import { EventStore } from "../src/history/event-store.js";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
describe("LessonExtractor", () => {
const testDir = path.join(os.tmpdir(), "refiner-lesson-test-" + Date.now());
beforeEach(() => {
process.env.PROMPT_REFINER_GLOBAL_DIR = testDir;
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
(EventStore as any).instance = null;
});
afterEach(() => {
const store = EventStore.getInstance();
store.close();
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
it("should analyze a linked pair and extract a lesson", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
// Setup linked pair
store.recordPrompt({ id: "p1", repo_id: "test", client: "cli", raw_prompt: "Create login" });
db.prepare("INSERT INTO commits (id, repo_id, sha, message, committed_at) VALUES (?, ?, ?, ?, ?)").run("c1", "test", "sha1", "feat: add login", "2026-04-12T10:00:00Z");
db.prepare("INSERT INTO executions (id, prompt_id, workflow_name, executor_name, status, started_at) VALUES (?, ?, ?, ?, ?, ?)").run("e1", "p1", "test", "test", "completed", "2026-04-12T09:00:00Z");
db.prepare("INSERT INTO execution_commits (execution_id, commit_id) VALUES (?, ?)").run("e1", "c1");
const mockRequestModel = vi.fn().mockResolvedValue(JSON.stringify({
title: "Authentication Best Practice",
summary: "Always use secure headers for login.",
lesson_type: "security",
confidence: "high"
}));
const extractor = new LessonExtractor(mockRequestModel);
await extractor.extractNewLessons();
expect(mockRequestModel).toHaveBeenCalled();
const lesson = db.prepare("SELECT * FROM lessons WHERE prompt_id = ?").get("p1");
expect(lesson).toBeDefined();
expect(lesson.title).toBe("Authentication Best Practice");
});
it("should not learn from failed executions", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({ id: "p-failed", repo_id: "test", client: "cli", raw_prompt: "Broken task" });
db.prepare("INSERT INTO commits (id, repo_id, sha, message, committed_at) VALUES (?, ?, ?, ?, ?)")
.run("c-failed", "test", "sha-failed", "fix: failed attempt", "2026-04-12T10:00:00Z");
db.prepare("INSERT INTO executions (id, prompt_id, workflow_name, executor_name, status, started_at) VALUES (?, ?, ?, ?, ?, ?)")
.run("e-failed", "p-failed", "test", "test", "failed", "2026-04-12T09:00:00Z");
db.prepare("INSERT INTO execution_commits (execution_id, commit_id) VALUES (?, ?)")
.run("e-failed", "c-failed");
const mockRequestModel = vi.fn();
await new LessonExtractor(mockRequestModel).extractNewLessons();
expect(mockRequestModel).not.toHaveBeenCalled();
expect(db.prepare("SELECT * FROM lessons WHERE prompt_id = ?").get("p-failed")).toBeUndefined();
});
it("does not record a lesson when the model is unavailable or returns malformed output", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({ id: "p-model", repo_id: "test", client: "cli", raw_prompt: "Create model test" });
store.recordCommit({
id: "c-model",
repo_id: "test",
sha: "sha-model",
author: "test",
message: "test: model",
committed_at: "2026-04-12T10:00:00Z",
});
store.recordExecution({
id: "e-model",
prompt_id: "p-model",
workflow_name: "test",
executor_name: "test",
status: "completed",
});
store.linkCommitToExecution("e-model", "c-model");
const request = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce("not json");
const extractor = new LessonExtractor(request);
await extractor.extractNewLessons();
await extractor.extractNewLessons();
expect(request).toHaveBeenCalledTimes(2);
expect(db.prepare("SELECT * FROM lessons WHERE prompt_id = ?").get("p-model")).toBeUndefined();
});
it("should extract a lesson from a failed execution via extractFailureLessons", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({ id: "p-fail-test", repo_id: "test", client: "cli", raw_prompt: "Bad task" });
db.prepare("INSERT INTO executions (id, prompt_id, workflow_name, executor_name, status, started_at, result_summary) VALUES (?, ?, ?, ?, ?, ?, ?)")
.run("e-fail-test", "p-fail-test", "test", "test", "failed", "2026-04-12T09:00:00Z", "TypeError: foo is undefined");
const mockRequestModel = vi.fn().mockResolvedValue(JSON.stringify({
title: "Avoid undefined errors",
summary: "Always check for undefined before accessing properties.",
lesson_type: "quality",
confidence: "high"
}));
const extractor = new LessonExtractor(mockRequestModel);
await extractor.extractFailureLessons();
expect(mockRequestModel).toHaveBeenCalled();
const lesson = db.prepare("SELECT * FROM lessons WHERE prompt_id = ?").get("p-fail-test");
expect(lesson).toBeDefined();
expect(lesson.title).toBe("Avoid undefined errors");
expect(lesson.source).toBe("auto-extracted-failure");
});
it("redacts and caps failed execution context before model analysis", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
const fakeToken = "ghp_" + "abcdefghijklmnopqrstuvwxyz123456";
store.recordPrompt({
id: "p-fail-secret",
repo_id: "test",
client: "cli",
raw_prompt: `Use token=secret-value and ${"x".repeat(5000)} ${fakeToken}`,
});
db.prepare("INSERT INTO executions (id, prompt_id, workflow_name, executor_name, status, started_at, result_summary, artifacts_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
.run("e-fail-secret", "p-fail-secret", "test", "test", "failed", "2026-04-12T09:00:00Z", "password=hunter2", "{");
const mockRequestModel = vi.fn().mockResolvedValue(JSON.stringify({
title: "Redacted lesson",
summary: "Avoid leaking secrets.",
lesson_type: "security",
confidence: "high"
}));
await new LessonExtractor(mockRequestModel).extractFailureLessons();
const modelPrompt = mockRequestModel.mock.calls[0][1] as string;
expect(modelPrompt).toContain("[REDACTED]");
expect(modelPrompt).toContain("[truncated]");
expect(modelPrompt).not.toContain("secret-value");
expect(modelPrompt).not.toContain("hunter2");
expect(modelPrompt).not.toContain(fakeToken);
});
it("handles empty artifact summaries during failure lesson extraction", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({ id: "p-fail-empty-artifacts", repo_id: "test", client: "cli", raw_prompt: "Task" });
db.prepare("INSERT INTO executions (id, prompt_id, workflow_name, executor_name, status, started_at, result_summary, artifacts_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
.run("e-fail-empty-artifacts", "p-fail-empty-artifacts", "test", "test", "failed", "2026-04-12T09:00:00Z", "Error", "");
const mockRequestModel = vi.fn().mockResolvedValue(JSON.stringify({
title: "Empty artifacts",
summary: "Handle empty artifacts.",
lesson_type: "quality",
confidence: "medium"
}));
await new LessonExtractor(mockRequestModel).extractFailureLessons();
expect(mockRequestModel.mock.calls[0][1]).toContain("ARTIFACTS / ADDITIONAL CONTEXT:\n{}");
});
it("handles failed executions without repo IDs or result summaries", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({ id: "p-fail-no-repo", client: "cli", raw_prompt: "Task without repo" });
db.prepare("INSERT INTO executions (id, prompt_id, workflow_name, executor_name, status, started_at) VALUES (?, ?, ?, ?, ?, ?)")
.run("e-fail-no-repo", "p-fail-no-repo", "test", "test", "failed", "2026-04-12T09:00:00Z");
const mockRequestModel = vi.fn().mockResolvedValue(JSON.stringify({
title: "No repo",
summary: "Handle missing repo metadata.",
lesson_type: "quality",
confidence: "low"
}));
await new LessonExtractor(mockRequestModel).extractFailureLessons();
const lesson = db.prepare("SELECT * FROM lessons WHERE prompt_id = ?").get("p-fail-no-repo");
expect(lesson.id).toMatch(/^lsn_[a-f0-9]{24}$/);
expect(mockRequestModel.mock.calls[0][1]).toContain("ERROR OR RESULT SUMMARY:\n");
});
it("does not record a failure lesson when the model is unavailable or returns malformed output", async () => {
const store = EventStore.getInstance();
const db = (store as any).db;
store.recordPrompt({ id: "p-fail-model", repo_id: "test", client: "cli", raw_prompt: "Task 2" });
db.prepare("INSERT INTO executions (id, prompt_id, workflow_name, executor_name, status, started_at, result_summary) VALUES (?, ?, ?, ?, ?, ?, ?)")
.run("e-fail-model", "p-fail-model", "test", "test", "failed", "2026-04-12T09:00:00Z", "Error");
const request = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce("not json");
const extractor = new LessonExtractor(request);
await extractor.extractFailureLessons();
await extractor.extractFailureLessons();
expect(request).toHaveBeenCalledTimes(2);
expect(db.prepare("SELECT * FROM lessons WHERE prompt_id = ?").get("p-fail-model")).toBeUndefined();
});
});