opencode-swarm-plugin 0.19.0 → 0.20.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/.beads/issues.jsonl +3 -0
- package/package.json +1 -1
- package/src/agent-mail.ts +7 -7
- package/src/index.ts +8 -0
- package/src/pattern-maturity.test.ts +1160 -0
- package/src/streams/events.ts +22 -0
- package/src/swarm-mail.ts +7 -7
package/.beads/issues.jsonl
CHANGED
|
@@ -581,6 +581,9 @@
|
|
|
581
581
|
{"id":"opencode-swarm-plugin-a2v","title":"Docker integration test infrastructure","description":"Create Docker-based integration test environment with real beads CLI and Agent Mail server for testing all plugin modules without mocks","status":"closed","priority":1,"issue_type":"epic","created_at":"2025-12-07T19:23:43.835677-08:00","updated_at":"2025-12-08T08:35:23.531219-08:00","closed_at":"2025-12-08T08:35:23.531219-08:00"}
|
|
582
582
|
{"id":"opencode-swarm-plugin-a347","title":"High priority ready bead","description":"","status":"closed","priority":0,"issue_type":"task","created_at":"2025-12-08T08:36:17.512906-08:00","updated_at":"2025-12-08T08:36:19.481274-08:00","closed_at":"2025-12-08T08:36:19.481274-08:00"}
|
|
583
583
|
{"id":"opencode-swarm-plugin-a3fa","title":"Lifecycle test bead","description":"50% complete","status":"closed","priority":2,"issue_type":"task","created_at":"2025-12-08T08:05:34.668388-08:00","updated_at":"2025-12-08T08:05:34.745926-08:00","closed_at":"2025-12-08T08:05:34.745926-08:00"}
|
|
584
|
+
{"id":"opencode-swarm-plugin-a544q","title":"Pattern Maturity Tests + Type Consolidation","description":"Add comprehensive tests for pattern-maturity.ts and consolidate duplicated AgentMailState/SwarmMailState types into a shared MailSessionState type","status":"closed","priority":1,"issue_type":"epic","created_at":"2025-12-13T17:34:47.29781-08:00","updated_at":"2025-12-13T17:38:45.880092-08:00","closed_at":"2025-12-13T17:38:45.880092-08:00"}
|
|
585
|
+
{"id":"opencode-swarm-plugin-a544q.1","title":"Add comprehensive tests for pattern-maturity.ts","description":"","status":"closed","priority":2,"issue_type":"task","created_at":"2025-12-13T17:34:47.364337-08:00","updated_at":"2025-12-13T17:38:44.260206-08:00","closed_at":"2025-12-13T17:38:44.260206-08:00","dependencies":[{"issue_id":"opencode-swarm-plugin-a544q.1","depends_on_id":"opencode-swarm-plugin-a544q","type":"parent-child","created_at":"2025-12-13T17:34:47.365765-08:00","created_by":"daemon"}]}
|
|
586
|
+
{"id":"opencode-swarm-plugin-a544q.2","title":"Extract shared MailSessionState type to streams/events.ts","description":"","status":"closed","priority":2,"issue_type":"task","created_at":"2025-12-13T17:34:47.424639-08:00","updated_at":"2025-12-13T17:38:45.133011-08:00","closed_at":"2025-12-13T17:38:45.133011-08:00","dependencies":[{"issue_id":"opencode-swarm-plugin-a544q.2","depends_on_id":"opencode-swarm-plugin-a544q","type":"parent-child","created_at":"2025-12-13T17:34:47.425534-08:00","created_by":"daemon"}]}
|
|
584
587
|
{"id":"opencode-swarm-plugin-a6lo","title":"Thread link test bead","description":"","status":"closed","priority":2,"issue_type":"task","created_at":"2025-12-08T11:11:24.861491-08:00","updated_at":"2025-12-08T11:11:26.310192-08:00","closed_at":"2025-12-08T11:11:26.310192-08:00"}
|
|
585
588
|
{"id":"opencode-swarm-plugin-a6rl7","title":"Mandate from Heaven: Agent Voting System","description":"Build a collective intelligence system where agents file and vote on ideas, tips, lore, snippets, and feature requests. High-consensus items become 'mandates' that influence future agent behavior via semantic memory.","status":"closed","priority":1,"issue_type":"epic","created_at":"2025-12-13T14:56:58.000088-08:00","updated_at":"2025-12-13T15:51:03.366671-08:00","closed_at":"2025-12-13T15:51:03.366671-08:00"}
|
|
586
589
|
{"id":"opencode-swarm-plugin-a6rl7.1","title":"Core schemas and types for mandate system","description":"","status":"closed","priority":0,"issue_type":"task","created_at":"2025-12-13T14:56:58.053657-08:00","updated_at":"2025-12-13T14:59:07.218098-08:00","closed_at":"2025-12-13T14:59:07.218098-08:00","dependencies":[{"issue_id":"opencode-swarm-plugin-a6rl7.1","depends_on_id":"opencode-swarm-plugin-a6rl7","type":"parent-child","created_at":"2025-12-13T14:56:58.055878-08:00","created_by":"daemon"}]}
|
package/package.json
CHANGED
package/src/agent-mail.ts
CHANGED
|
@@ -31,6 +31,7 @@ import { tool } from "@opencode-ai/plugin";
|
|
|
31
31
|
import { z } from "zod";
|
|
32
32
|
import { isToolAvailable, warnMissingTool } from "./tool-availability";
|
|
33
33
|
import { getRateLimiter, type RateLimiter } from "./rate-limiter";
|
|
34
|
+
import type { MailSessionState } from "./streams/events";
|
|
34
35
|
|
|
35
36
|
// ============================================================================
|
|
36
37
|
// Configuration
|
|
@@ -92,13 +93,12 @@ const RECOVERY_CONFIG = {
|
|
|
92
93
|
// Types
|
|
93
94
|
// ============================================================================
|
|
94
95
|
|
|
95
|
-
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
96
|
+
/**
|
|
97
|
+
* Agent Mail session state
|
|
98
|
+
* @deprecated Use MailSessionState from streams/events.ts instead
|
|
99
|
+
* This is kept for backward compatibility and re-exported as an alias
|
|
100
|
+
*/
|
|
101
|
+
export type AgentMailState = MailSessionState;
|
|
102
102
|
|
|
103
103
|
// ============================================================================
|
|
104
104
|
// Module-level state (keyed by sessionID)
|
package/src/index.ts
CHANGED
|
@@ -301,6 +301,14 @@ export {
|
|
|
301
301
|
type SwarmMailState,
|
|
302
302
|
} from "./swarm-mail";
|
|
303
303
|
|
|
304
|
+
/**
|
|
305
|
+
* Re-export shared types from streams/events
|
|
306
|
+
*
|
|
307
|
+
* Includes:
|
|
308
|
+
* - MailSessionState - Shared session state type for Agent Mail and Swarm Mail
|
|
309
|
+
*/
|
|
310
|
+
export { type MailSessionState } from "./streams/events";
|
|
311
|
+
|
|
304
312
|
/**
|
|
305
313
|
* Re-export structured module
|
|
306
314
|
*
|
|
@@ -0,0 +1,1160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comprehensive tests for pattern-maturity.ts
|
|
3
|
+
*
|
|
4
|
+
* Tests behavior of maturity state transitions, decay calculations,
|
|
5
|
+
* and storage operations. Focuses on observable behavior over internal state.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, test, expect, beforeEach } from "vitest";
|
|
8
|
+
import {
|
|
9
|
+
calculateDecayedCounts,
|
|
10
|
+
calculateMaturityState,
|
|
11
|
+
createPatternMaturity,
|
|
12
|
+
updatePatternMaturity,
|
|
13
|
+
promotePattern,
|
|
14
|
+
deprecatePattern,
|
|
15
|
+
getMaturityMultiplier,
|
|
16
|
+
formatMaturityForPrompt,
|
|
17
|
+
formatPatternsWithMaturityForPrompt,
|
|
18
|
+
InMemoryMaturityStorage,
|
|
19
|
+
type MaturityFeedback,
|
|
20
|
+
type PatternMaturity,
|
|
21
|
+
DEFAULT_MATURITY_CONFIG,
|
|
22
|
+
} from "./pattern-maturity";
|
|
23
|
+
|
|
24
|
+
// ============================================================================
|
|
25
|
+
// Test Fixtures
|
|
26
|
+
// ============================================================================
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create a feedback event with defaults
|
|
30
|
+
*/
|
|
31
|
+
function createFeedback(
|
|
32
|
+
overrides: Partial<MaturityFeedback> = {},
|
|
33
|
+
): MaturityFeedback {
|
|
34
|
+
return {
|
|
35
|
+
pattern_id: "test-pattern",
|
|
36
|
+
type: "helpful",
|
|
37
|
+
timestamp: new Date().toISOString(),
|
|
38
|
+
weight: 1,
|
|
39
|
+
...overrides,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create feedback events at specific days ago
|
|
45
|
+
*/
|
|
46
|
+
function createFeedbackAt(
|
|
47
|
+
daysAgo: number,
|
|
48
|
+
type: "helpful" | "harmful",
|
|
49
|
+
weight = 1,
|
|
50
|
+
): MaturityFeedback {
|
|
51
|
+
const timestamp = new Date();
|
|
52
|
+
timestamp.setDate(timestamp.getDate() - daysAgo);
|
|
53
|
+
return createFeedback({ type, timestamp: timestamp.toISOString(), weight });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ============================================================================
|
|
57
|
+
// calculateDecayedCounts Tests
|
|
58
|
+
// ============================================================================
|
|
59
|
+
|
|
60
|
+
describe("calculateDecayedCounts", () => {
|
|
61
|
+
test("returns zero counts for empty feedback", () => {
|
|
62
|
+
const result = calculateDecayedCounts([]);
|
|
63
|
+
expect(result).toEqual({ decayedHelpful: 0, decayedHarmful: 0 });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("counts recent helpful feedback at full weight", () => {
|
|
67
|
+
const events = [createFeedback({ type: "helpful" })];
|
|
68
|
+
const result = calculateDecayedCounts(events);
|
|
69
|
+
// Recent feedback should be ~1 (minor decay allowed)
|
|
70
|
+
expect(result.decayedHelpful).toBeGreaterThan(0.99);
|
|
71
|
+
expect(result.decayedHarmful).toBe(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("counts recent harmful feedback at full weight", () => {
|
|
75
|
+
const events = [createFeedback({ type: "harmful" })];
|
|
76
|
+
const result = calculateDecayedCounts(events);
|
|
77
|
+
expect(result.decayedHelpful).toBe(0);
|
|
78
|
+
expect(result.decayedHarmful).toBeGreaterThan(0.99);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("applies decay to old feedback", () => {
|
|
82
|
+
const oldEvent = createFeedbackAt(90, "helpful"); // one half-life
|
|
83
|
+
const recentEvent = createFeedbackAt(0, "helpful");
|
|
84
|
+
|
|
85
|
+
const result = calculateDecayedCounts([oldEvent, recentEvent]);
|
|
86
|
+
|
|
87
|
+
// 90-day old feedback should be ~0.5x (one half-life)
|
|
88
|
+
// Recent feedback should be ~1.0x
|
|
89
|
+
// Total should be ~1.5
|
|
90
|
+
expect(result.decayedHelpful).toBeGreaterThan(1.4);
|
|
91
|
+
expect(result.decayedHelpful).toBeLessThan(1.6);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("handles mixed feedback types", () => {
|
|
95
|
+
const events = [
|
|
96
|
+
createFeedbackAt(0, "helpful"),
|
|
97
|
+
createFeedbackAt(0, "helpful"),
|
|
98
|
+
createFeedbackAt(0, "harmful"),
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
const result = calculateDecayedCounts(events);
|
|
102
|
+
expect(result.decayedHelpful).toBeGreaterThan(1.9);
|
|
103
|
+
expect(result.decayedHarmful).toBeGreaterThan(0.99);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("respects weight parameter", () => {
|
|
107
|
+
const fullWeight = createFeedback({ type: "helpful", weight: 1 });
|
|
108
|
+
const halfWeight = createFeedback({ type: "helpful", weight: 0.5 });
|
|
109
|
+
|
|
110
|
+
const result = calculateDecayedCounts([fullWeight, halfWeight]);
|
|
111
|
+
|
|
112
|
+
// ~1.0 + ~0.5 = ~1.5
|
|
113
|
+
expect(result.decayedHelpful).toBeGreaterThan(1.4);
|
|
114
|
+
expect(result.decayedHelpful).toBeLessThan(1.6);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("uses custom config half-life", () => {
|
|
118
|
+
const event = createFeedbackAt(45, "helpful"); // half of 90-day half-life
|
|
119
|
+
const config = { ...DEFAULT_MATURITY_CONFIG, halfLifeDays: 45 };
|
|
120
|
+
|
|
121
|
+
const result = calculateDecayedCounts([event], config);
|
|
122
|
+
|
|
123
|
+
// At custom half-life, should be ~0.5
|
|
124
|
+
expect(result.decayedHelpful).toBeGreaterThan(0.4);
|
|
125
|
+
expect(result.decayedHelpful).toBeLessThan(0.6);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("uses custom now parameter for decay calculation", () => {
|
|
129
|
+
const event = createFeedback({
|
|
130
|
+
type: "helpful",
|
|
131
|
+
timestamp: "2024-01-01T00:00:00Z",
|
|
132
|
+
});
|
|
133
|
+
const now = new Date("2024-04-01T00:00:00Z"); // 90 days later
|
|
134
|
+
|
|
135
|
+
const result = calculateDecayedCounts(
|
|
136
|
+
[event],
|
|
137
|
+
DEFAULT_MATURITY_CONFIG,
|
|
138
|
+
now,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// Should be decayed by one half-life
|
|
142
|
+
expect(result.decayedHelpful).toBeGreaterThan(0.4);
|
|
143
|
+
expect(result.decayedHelpful).toBeLessThan(0.6);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// ============================================================================
|
|
148
|
+
// calculateMaturityState Tests
|
|
149
|
+
// ============================================================================
|
|
150
|
+
|
|
151
|
+
describe("calculateMaturityState", () => {
|
|
152
|
+
test("returns candidate with no feedback", () => {
|
|
153
|
+
const state = calculateMaturityState([]);
|
|
154
|
+
expect(state).toBe("candidate");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("returns candidate with insufficient feedback", () => {
|
|
158
|
+
// minFeedback = 3, so 2 events should be candidate
|
|
159
|
+
const events = [
|
|
160
|
+
createFeedbackAt(0, "helpful"),
|
|
161
|
+
createFeedbackAt(0, "helpful"),
|
|
162
|
+
];
|
|
163
|
+
const state = calculateMaturityState(events);
|
|
164
|
+
expect(state).toBe("candidate");
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("returns established with enough neutral feedback", () => {
|
|
168
|
+
// 3 helpful, 0 harmful = established (not enough for proven)
|
|
169
|
+
const events = [
|
|
170
|
+
createFeedbackAt(0, "helpful"),
|
|
171
|
+
createFeedbackAt(0, "helpful"),
|
|
172
|
+
createFeedbackAt(0, "helpful"),
|
|
173
|
+
];
|
|
174
|
+
const state = calculateMaturityState(events);
|
|
175
|
+
expect(state).toBe("established");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("returns proven with strong positive feedback", () => {
|
|
179
|
+
// minHelpful = 5, maxHarmful = 15%
|
|
180
|
+
// 6 helpful, 1 harmful = 14% harmful, should be proven
|
|
181
|
+
const events = [
|
|
182
|
+
...Array(6)
|
|
183
|
+
.fill(null)
|
|
184
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
185
|
+
createFeedbackAt(0, "harmful"),
|
|
186
|
+
];
|
|
187
|
+
const state = calculateMaturityState(events);
|
|
188
|
+
expect(state).toBe("proven");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("returns deprecated with high harmful ratio", () => {
|
|
192
|
+
// deprecationThreshold = 30%
|
|
193
|
+
// 2 helpful, 3 harmful = 60% harmful
|
|
194
|
+
const events = [
|
|
195
|
+
createFeedbackAt(0, "helpful"),
|
|
196
|
+
createFeedbackAt(0, "helpful"),
|
|
197
|
+
createFeedbackAt(0, "harmful"),
|
|
198
|
+
createFeedbackAt(0, "harmful"),
|
|
199
|
+
createFeedbackAt(0, "harmful"),
|
|
200
|
+
];
|
|
201
|
+
const state = calculateMaturityState(events);
|
|
202
|
+
expect(state).toBe("deprecated");
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("proven requires minimum helpful count", () => {
|
|
206
|
+
// 4 helpful is below minHelpful (5), even with low harmful ratio
|
|
207
|
+
const events = [
|
|
208
|
+
...Array(4)
|
|
209
|
+
.fill(null)
|
|
210
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
211
|
+
createFeedbackAt(0, "harmful"),
|
|
212
|
+
];
|
|
213
|
+
const state = calculateMaturityState(events);
|
|
214
|
+
expect(state).toBe("established"); // not proven
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("proven requires low harmful ratio", () => {
|
|
218
|
+
// 5 helpful, 2 harmful = 28% harmful (above maxHarmful of 15%)
|
|
219
|
+
const events = [
|
|
220
|
+
...Array(5)
|
|
221
|
+
.fill(null)
|
|
222
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
223
|
+
createFeedbackAt(0, "harmful"),
|
|
224
|
+
createFeedbackAt(0, "harmful"),
|
|
225
|
+
];
|
|
226
|
+
const state = calculateMaturityState(events);
|
|
227
|
+
expect(state).toBe("established"); // not proven due to harmful ratio
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("deprecation takes priority over proven", () => {
|
|
231
|
+
// Even with high helpful count, high harmful ratio = deprecated
|
|
232
|
+
const events = [
|
|
233
|
+
...Array(10)
|
|
234
|
+
.fill(null)
|
|
235
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
236
|
+
...Array(8)
|
|
237
|
+
.fill(null)
|
|
238
|
+
.map(() => createFeedbackAt(0, "harmful")), // 44% harmful
|
|
239
|
+
];
|
|
240
|
+
const state = calculateMaturityState(events);
|
|
241
|
+
expect(state).toBe("deprecated");
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("uses custom config thresholds", () => {
|
|
245
|
+
const config = {
|
|
246
|
+
...DEFAULT_MATURITY_CONFIG,
|
|
247
|
+
minFeedback: 2,
|
|
248
|
+
minHelpful: 3,
|
|
249
|
+
maxHarmful: 0.2,
|
|
250
|
+
deprecationThreshold: 0.4,
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// 3 helpful, 0 harmful = proven with custom config
|
|
254
|
+
const events = [
|
|
255
|
+
createFeedbackAt(0, "helpful"),
|
|
256
|
+
createFeedbackAt(0, "helpful"),
|
|
257
|
+
createFeedbackAt(0, "helpful"),
|
|
258
|
+
];
|
|
259
|
+
const state = calculateMaturityState(events, config);
|
|
260
|
+
expect(state).toBe("proven");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("accounts for decay in state calculation", () => {
|
|
264
|
+
// Old helpful feedback decays, shifting ratios
|
|
265
|
+
// 3 events at 180 days = 2 half-lives = ~0.25x each = ~0.75 total helpful
|
|
266
|
+
// 1 event at 0 days = ~1.0x harmful
|
|
267
|
+
// Total = ~1.75, harmful ratio = 1.0/1.75 = ~57%
|
|
268
|
+
// BUT total < minFeedback (3), so state should be candidate
|
|
269
|
+
const events = [
|
|
270
|
+
createFeedbackAt(180, "helpful"), // heavily decayed (~0.25x)
|
|
271
|
+
createFeedbackAt(180, "helpful"), // heavily decayed (~0.25x)
|
|
272
|
+
createFeedbackAt(180, "helpful"), // heavily decayed (~0.25x)
|
|
273
|
+
createFeedbackAt(0, "harmful"), // recent, full weight (~1.0x)
|
|
274
|
+
];
|
|
275
|
+
|
|
276
|
+
const state = calculateMaturityState(events);
|
|
277
|
+
// Total decayed feedback < minFeedback threshold
|
|
278
|
+
expect(state).toBe("candidate");
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// ============================================================================
|
|
283
|
+
// createPatternMaturity Tests
|
|
284
|
+
// ============================================================================
|
|
285
|
+
|
|
286
|
+
describe("createPatternMaturity", () => {
|
|
287
|
+
test("creates initial maturity in candidate state", () => {
|
|
288
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
289
|
+
expect(maturity.pattern_id).toBe("test-pattern");
|
|
290
|
+
expect(maturity.state).toBe("candidate");
|
|
291
|
+
expect(maturity.helpful_count).toBe(0);
|
|
292
|
+
expect(maturity.harmful_count).toBe(0);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test("sets last_validated timestamp", () => {
|
|
296
|
+
const before = new Date();
|
|
297
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
298
|
+
const after = new Date();
|
|
299
|
+
|
|
300
|
+
const validated = new Date(maturity.last_validated);
|
|
301
|
+
expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
302
|
+
expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test("does not set promoted_at or deprecated_at initially", () => {
|
|
306
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
307
|
+
expect(maturity.promoted_at).toBeUndefined();
|
|
308
|
+
expect(maturity.deprecated_at).toBeUndefined();
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
// ============================================================================
|
|
313
|
+
// updatePatternMaturity Tests
|
|
314
|
+
// ============================================================================
|
|
315
|
+
|
|
316
|
+
describe("updatePatternMaturity", () => {
|
|
317
|
+
test("updates state based on feedback", () => {
|
|
318
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
319
|
+
const events = [
|
|
320
|
+
...Array(6)
|
|
321
|
+
.fill(null)
|
|
322
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
323
|
+
createFeedbackAt(0, "harmful"),
|
|
324
|
+
];
|
|
325
|
+
|
|
326
|
+
const updated = updatePatternMaturity(maturity, events);
|
|
327
|
+
expect(updated.state).toBe("proven");
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
test("updates helpful and harmful counts", () => {
|
|
331
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
332
|
+
const events = [
|
|
333
|
+
createFeedbackAt(0, "helpful"),
|
|
334
|
+
createFeedbackAt(0, "helpful"),
|
|
335
|
+
createFeedbackAt(0, "harmful"),
|
|
336
|
+
];
|
|
337
|
+
|
|
338
|
+
const updated = updatePatternMaturity(maturity, events);
|
|
339
|
+
expect(updated.helpful_count).toBe(2);
|
|
340
|
+
expect(updated.harmful_count).toBe(1);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("sets promoted_at on first transition to proven", () => {
|
|
344
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
345
|
+
const events = [
|
|
346
|
+
...Array(6)
|
|
347
|
+
.fill(null)
|
|
348
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
349
|
+
];
|
|
350
|
+
|
|
351
|
+
const updated = updatePatternMaturity(maturity, events);
|
|
352
|
+
expect(updated.promoted_at).toBeDefined();
|
|
353
|
+
expect(new Date(updated.promoted_at!).getTime()).toBeLessThanOrEqual(
|
|
354
|
+
Date.now(),
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test("does not update promoted_at if already proven", () => {
|
|
359
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
360
|
+
const events = [
|
|
361
|
+
...Array(6)
|
|
362
|
+
.fill(null)
|
|
363
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
364
|
+
];
|
|
365
|
+
|
|
366
|
+
const first = updatePatternMaturity(maturity, events);
|
|
367
|
+
const promotedAt = first.promoted_at;
|
|
368
|
+
|
|
369
|
+
// Add more helpful feedback
|
|
370
|
+
const moreEvents = [
|
|
371
|
+
...events,
|
|
372
|
+
createFeedbackAt(0, "helpful"),
|
|
373
|
+
createFeedbackAt(0, "helpful"),
|
|
374
|
+
];
|
|
375
|
+
const second = updatePatternMaturity(first, moreEvents);
|
|
376
|
+
|
|
377
|
+
expect(second.promoted_at).toBe(promotedAt); // unchanged
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
test("sets deprecated_at on first transition to deprecated", () => {
|
|
381
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
382
|
+
const events = [
|
|
383
|
+
createFeedbackAt(0, "helpful"),
|
|
384
|
+
createFeedbackAt(0, "harmful"),
|
|
385
|
+
createFeedbackAt(0, "harmful"),
|
|
386
|
+
createFeedbackAt(0, "harmful"),
|
|
387
|
+
];
|
|
388
|
+
|
|
389
|
+
const updated = updatePatternMaturity(maturity, events);
|
|
390
|
+
expect(updated.deprecated_at).toBeDefined();
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test("does not update deprecated_at if already deprecated", () => {
|
|
394
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
395
|
+
const events = [
|
|
396
|
+
createFeedbackAt(0, "helpful"),
|
|
397
|
+
createFeedbackAt(0, "harmful"),
|
|
398
|
+
createFeedbackAt(0, "harmful"),
|
|
399
|
+
createFeedbackAt(0, "harmful"),
|
|
400
|
+
];
|
|
401
|
+
|
|
402
|
+
const first = updatePatternMaturity(maturity, events);
|
|
403
|
+
const deprecatedAt = first.deprecated_at;
|
|
404
|
+
|
|
405
|
+
const moreEvents = [...events, createFeedbackAt(0, "harmful")];
|
|
406
|
+
const second = updatePatternMaturity(first, moreEvents);
|
|
407
|
+
|
|
408
|
+
expect(second.deprecated_at).toBe(deprecatedAt); // unchanged
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
test("updates last_validated timestamp", () => {
|
|
412
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
413
|
+
const events = [createFeedbackAt(0, "helpful")];
|
|
414
|
+
|
|
415
|
+
const before = new Date();
|
|
416
|
+
const updated = updatePatternMaturity(maturity, events);
|
|
417
|
+
const after = new Date();
|
|
418
|
+
|
|
419
|
+
const validated = new Date(updated.last_validated);
|
|
420
|
+
expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
421
|
+
expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
test("handles state transitions: candidate -> established", () => {
|
|
425
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
426
|
+
const events = [
|
|
427
|
+
createFeedbackAt(0, "helpful"),
|
|
428
|
+
createFeedbackAt(0, "helpful"),
|
|
429
|
+
createFeedbackAt(0, "helpful"),
|
|
430
|
+
];
|
|
431
|
+
|
|
432
|
+
const updated = updatePatternMaturity(maturity, events);
|
|
433
|
+
expect(maturity.state).toBe("candidate");
|
|
434
|
+
expect(updated.state).toBe("established");
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test("handles state transitions: established -> proven", () => {
|
|
438
|
+
let maturity = createPatternMaturity("test-pattern");
|
|
439
|
+
// First get to established
|
|
440
|
+
maturity = updatePatternMaturity(maturity, [
|
|
441
|
+
createFeedbackAt(0, "helpful"),
|
|
442
|
+
createFeedbackAt(0, "helpful"),
|
|
443
|
+
createFeedbackAt(0, "helpful"),
|
|
444
|
+
]);
|
|
445
|
+
expect(maturity.state).toBe("established");
|
|
446
|
+
|
|
447
|
+
// Then add enough for proven
|
|
448
|
+
const provenEvents = [
|
|
449
|
+
...Array(6)
|
|
450
|
+
.fill(null)
|
|
451
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
452
|
+
];
|
|
453
|
+
const updated = updatePatternMaturity(maturity, provenEvents);
|
|
454
|
+
expect(updated.state).toBe("proven");
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("handles state transitions: proven -> deprecated", () => {
|
|
458
|
+
let maturity = createPatternMaturity("test-pattern");
|
|
459
|
+
// First get to proven
|
|
460
|
+
maturity = updatePatternMaturity(maturity, [
|
|
461
|
+
...Array(6)
|
|
462
|
+
.fill(null)
|
|
463
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
464
|
+
]);
|
|
465
|
+
expect(maturity.state).toBe("proven");
|
|
466
|
+
|
|
467
|
+
// Then add lots of harmful feedback
|
|
468
|
+
const deprecatedEvents = [
|
|
469
|
+
...Array(6)
|
|
470
|
+
.fill(null)
|
|
471
|
+
.map(() => createFeedbackAt(0, "helpful")),
|
|
472
|
+
...Array(8)
|
|
473
|
+
.fill(null)
|
|
474
|
+
.map(() => createFeedbackAt(0, "harmful")),
|
|
475
|
+
];
|
|
476
|
+
const updated = updatePatternMaturity(maturity, deprecatedEvents);
|
|
477
|
+
expect(updated.state).toBe("deprecated");
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
test("handles empty feedback array", () => {
|
|
481
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
482
|
+
const updated = updatePatternMaturity(maturity, []);
|
|
483
|
+
expect(updated.state).toBe("candidate");
|
|
484
|
+
expect(updated.helpful_count).toBe(0);
|
|
485
|
+
expect(updated.harmful_count).toBe(0);
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// ============================================================================
|
|
490
|
+
// promotePattern Tests
|
|
491
|
+
// ============================================================================
|
|
492
|
+
|
|
493
|
+
describe("promotePattern", () => {
|
|
494
|
+
test("promotes candidate to proven", () => {
|
|
495
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
496
|
+
const promoted = promotePattern(maturity);
|
|
497
|
+
expect(promoted.state).toBe("proven");
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
test("promotes established to proven", () => {
|
|
501
|
+
const maturity: PatternMaturity = {
|
|
502
|
+
pattern_id: "test-pattern",
|
|
503
|
+
state: "established",
|
|
504
|
+
helpful_count: 3,
|
|
505
|
+
harmful_count: 0,
|
|
506
|
+
last_validated: new Date().toISOString(),
|
|
507
|
+
};
|
|
508
|
+
const promoted = promotePattern(maturity);
|
|
509
|
+
expect(promoted.state).toBe("proven");
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
test("sets promoted_at timestamp", () => {
|
|
513
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
514
|
+
const before = new Date();
|
|
515
|
+
const promoted = promotePattern(maturity);
|
|
516
|
+
const after = new Date();
|
|
517
|
+
|
|
518
|
+
expect(promoted.promoted_at).toBeDefined();
|
|
519
|
+
const promotedAt = new Date(promoted.promoted_at!);
|
|
520
|
+
expect(promotedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
521
|
+
expect(promotedAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
test("updates last_validated timestamp", () => {
|
|
525
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
526
|
+
const before = new Date();
|
|
527
|
+
const promoted = promotePattern(maturity);
|
|
528
|
+
const after = new Date();
|
|
529
|
+
|
|
530
|
+
const validated = new Date(promoted.last_validated);
|
|
531
|
+
expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
532
|
+
expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
test("throws error when promoting deprecated pattern", () => {
|
|
536
|
+
const maturity: PatternMaturity = {
|
|
537
|
+
pattern_id: "test-pattern",
|
|
538
|
+
state: "deprecated",
|
|
539
|
+
helpful_count: 1,
|
|
540
|
+
harmful_count: 3,
|
|
541
|
+
last_validated: new Date().toISOString(),
|
|
542
|
+
deprecated_at: new Date().toISOString(),
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
expect(() => promotePattern(maturity)).toThrow(
|
|
546
|
+
"Cannot promote a deprecated pattern",
|
|
547
|
+
);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
test("returns unchanged maturity when already proven", () => {
|
|
551
|
+
const maturity: PatternMaturity = {
|
|
552
|
+
pattern_id: "test-pattern",
|
|
553
|
+
state: "proven",
|
|
554
|
+
helpful_count: 10,
|
|
555
|
+
harmful_count: 1,
|
|
556
|
+
last_validated: new Date().toISOString(),
|
|
557
|
+
promoted_at: "2024-01-01T00:00:00Z",
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
const promoted = promotePattern(maturity);
|
|
561
|
+
expect(promoted).toBe(maturity); // same reference
|
|
562
|
+
expect(promoted.state).toBe("proven");
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
// ============================================================================
|
|
567
|
+
// deprecatePattern Tests
|
|
568
|
+
// ============================================================================
|
|
569
|
+
|
|
570
|
+
describe("deprecatePattern", () => {
|
|
571
|
+
test("deprecates candidate pattern", () => {
|
|
572
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
573
|
+
const deprecated = deprecatePattern(maturity);
|
|
574
|
+
expect(deprecated.state).toBe("deprecated");
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test("deprecates established pattern", () => {
|
|
578
|
+
const maturity: PatternMaturity = {
|
|
579
|
+
pattern_id: "test-pattern",
|
|
580
|
+
state: "established",
|
|
581
|
+
helpful_count: 3,
|
|
582
|
+
harmful_count: 1,
|
|
583
|
+
last_validated: new Date().toISOString(),
|
|
584
|
+
};
|
|
585
|
+
const deprecated = deprecatePattern(maturity);
|
|
586
|
+
expect(deprecated.state).toBe("deprecated");
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
test("deprecates proven pattern", () => {
|
|
590
|
+
const maturity: PatternMaturity = {
|
|
591
|
+
pattern_id: "test-pattern",
|
|
592
|
+
state: "proven",
|
|
593
|
+
helpful_count: 10,
|
|
594
|
+
harmful_count: 1,
|
|
595
|
+
last_validated: new Date().toISOString(),
|
|
596
|
+
promoted_at: new Date().toISOString(),
|
|
597
|
+
};
|
|
598
|
+
const deprecated = deprecatePattern(maturity);
|
|
599
|
+
expect(deprecated.state).toBe("deprecated");
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
test("sets deprecated_at timestamp", () => {
|
|
603
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
604
|
+
const before = new Date();
|
|
605
|
+
const deprecated = deprecatePattern(maturity);
|
|
606
|
+
const after = new Date();
|
|
607
|
+
|
|
608
|
+
expect(deprecated.deprecated_at).toBeDefined();
|
|
609
|
+
const deprecatedAt = new Date(deprecated.deprecated_at!);
|
|
610
|
+
expect(deprecatedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
611
|
+
expect(deprecatedAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
test("updates last_validated timestamp", () => {
|
|
615
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
616
|
+
const before = new Date();
|
|
617
|
+
const deprecated = deprecatePattern(maturity);
|
|
618
|
+
const after = new Date();
|
|
619
|
+
|
|
620
|
+
const validated = new Date(deprecated.last_validated);
|
|
621
|
+
expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
622
|
+
expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
test("returns unchanged maturity when already deprecated", () => {
|
|
626
|
+
const maturity: PatternMaturity = {
|
|
627
|
+
pattern_id: "test-pattern",
|
|
628
|
+
state: "deprecated",
|
|
629
|
+
helpful_count: 1,
|
|
630
|
+
harmful_count: 3,
|
|
631
|
+
last_validated: new Date().toISOString(),
|
|
632
|
+
deprecated_at: "2024-01-01T00:00:00Z",
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
const deprecated = deprecatePattern(maturity);
|
|
636
|
+
expect(deprecated).toBe(maturity); // same reference
|
|
637
|
+
expect(deprecated.state).toBe("deprecated");
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
test("accepts optional reason parameter", () => {
|
|
641
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
642
|
+
// Reason is accepted but not stored (parameter prefixed with _)
|
|
643
|
+
const deprecated = deprecatePattern(maturity, "test reason");
|
|
644
|
+
expect(deprecated.state).toBe("deprecated");
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
// ============================================================================
|
|
649
|
+
// getMaturityMultiplier Tests
|
|
650
|
+
// ============================================================================
|
|
651
|
+
|
|
652
|
+
describe("getMaturityMultiplier", () => {
|
|
653
|
+
test("returns 0.5 for candidate", () => {
|
|
654
|
+
expect(getMaturityMultiplier("candidate")).toBe(0.5);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
test("returns 1.0 for established", () => {
|
|
658
|
+
expect(getMaturityMultiplier("established")).toBe(1.0);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
test("returns 1.5 for proven", () => {
|
|
662
|
+
expect(getMaturityMultiplier("proven")).toBe(1.5);
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
test("returns 0 for deprecated", () => {
|
|
666
|
+
expect(getMaturityMultiplier("deprecated")).toBe(0);
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
// ============================================================================
|
|
671
|
+
// formatMaturityForPrompt Tests
|
|
672
|
+
// ============================================================================
|
|
673
|
+
|
|
674
|
+
describe("formatMaturityForPrompt", () => {
|
|
675
|
+
test("shows limited data for insufficient observations", () => {
|
|
676
|
+
const maturity: PatternMaturity = {
|
|
677
|
+
pattern_id: "test",
|
|
678
|
+
state: "candidate",
|
|
679
|
+
helpful_count: 2,
|
|
680
|
+
harmful_count: 0,
|
|
681
|
+
last_validated: new Date().toISOString(),
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
685
|
+
expect(formatted).toBe("[LIMITED DATA - 2 observations]");
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
test("shows singular observation for count of 1", () => {
|
|
689
|
+
const maturity: PatternMaturity = {
|
|
690
|
+
pattern_id: "test",
|
|
691
|
+
state: "candidate",
|
|
692
|
+
helpful_count: 1,
|
|
693
|
+
harmful_count: 0,
|
|
694
|
+
last_validated: new Date().toISOString(),
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
698
|
+
expect(formatted).toBe("[LIMITED DATA - 1 observation]");
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
test("shows candidate with observation count when >= 3", () => {
|
|
702
|
+
const maturity: PatternMaturity = {
|
|
703
|
+
pattern_id: "test",
|
|
704
|
+
state: "candidate",
|
|
705
|
+
helpful_count: 3,
|
|
706
|
+
harmful_count: 0,
|
|
707
|
+
last_validated: new Date().toISOString(),
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
711
|
+
expect(formatted).toBe("[CANDIDATE - 3 observations, needs more data]");
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
test("shows established with percentages", () => {
|
|
715
|
+
const maturity: PatternMaturity = {
|
|
716
|
+
pattern_id: "test",
|
|
717
|
+
state: "established",
|
|
718
|
+
helpful_count: 7,
|
|
719
|
+
harmful_count: 3,
|
|
720
|
+
last_validated: new Date().toISOString(),
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
724
|
+
expect(formatted).toBe(
|
|
725
|
+
"[ESTABLISHED - 70% helpful, 30% harmful from 10 observations]",
|
|
726
|
+
);
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
test("shows proven with helpful percentage", () => {
|
|
730
|
+
const maturity: PatternMaturity = {
|
|
731
|
+
pattern_id: "test",
|
|
732
|
+
state: "proven",
|
|
733
|
+
helpful_count: 9,
|
|
734
|
+
harmful_count: 1,
|
|
735
|
+
last_validated: new Date().toISOString(),
|
|
736
|
+
promoted_at: new Date().toISOString(),
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
740
|
+
expect(formatted).toBe("[PROVEN - 90% helpful from 10 observations]");
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
test("shows deprecated with harmful percentage", () => {
|
|
744
|
+
const maturity: PatternMaturity = {
|
|
745
|
+
pattern_id: "test",
|
|
746
|
+
state: "deprecated",
|
|
747
|
+
helpful_count: 2,
|
|
748
|
+
harmful_count: 8,
|
|
749
|
+
last_validated: new Date().toISOString(),
|
|
750
|
+
deprecated_at: new Date().toISOString(),
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
754
|
+
expect(formatted).toBe("[DEPRECATED - 80% harmful, avoid using]");
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
test("rounds percentages correctly", () => {
|
|
758
|
+
const maturity: PatternMaturity = {
|
|
759
|
+
pattern_id: "test",
|
|
760
|
+
state: "established",
|
|
761
|
+
helpful_count: 2,
|
|
762
|
+
harmful_count: 1,
|
|
763
|
+
last_validated: new Date().toISOString(),
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
767
|
+
// 2/3 = 66.666... rounds to 67%, 1/3 = 33.333... rounds to 33%
|
|
768
|
+
expect(formatted).toBe(
|
|
769
|
+
"[ESTABLISHED - 67% helpful, 33% harmful from 3 observations]",
|
|
770
|
+
);
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
test("handles zero counts edge case", () => {
|
|
774
|
+
const maturity: PatternMaturity = {
|
|
775
|
+
pattern_id: "test",
|
|
776
|
+
state: "candidate",
|
|
777
|
+
helpful_count: 0,
|
|
778
|
+
harmful_count: 0,
|
|
779
|
+
last_validated: new Date().toISOString(),
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
const formatted = formatMaturityForPrompt(maturity);
|
|
783
|
+
expect(formatted).toBe("[LIMITED DATA - 0 observations]");
|
|
784
|
+
});
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
// ============================================================================
|
|
788
|
+
// formatPatternsWithMaturityForPrompt Tests
|
|
789
|
+
// ============================================================================
|
|
790
|
+
|
|
791
|
+
describe("formatPatternsWithMaturityForPrompt", () => {
|
|
792
|
+
test("formats empty map", () => {
|
|
793
|
+
const patterns = new Map<string, PatternMaturity>();
|
|
794
|
+
const formatted = formatPatternsWithMaturityForPrompt(patterns);
|
|
795
|
+
expect(formatted).toBe("");
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
test("groups patterns by maturity state", () => {
|
|
799
|
+
const patterns = new Map<string, PatternMaturity>();
|
|
800
|
+
|
|
801
|
+
patterns.set("Proven pattern", {
|
|
802
|
+
pattern_id: "p1",
|
|
803
|
+
state: "proven",
|
|
804
|
+
helpful_count: 9,
|
|
805
|
+
harmful_count: 1,
|
|
806
|
+
last_validated: new Date().toISOString(),
|
|
807
|
+
promoted_at: new Date().toISOString(),
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
patterns.set("Established pattern", {
|
|
811
|
+
pattern_id: "p2",
|
|
812
|
+
state: "established",
|
|
813
|
+
helpful_count: 5,
|
|
814
|
+
harmful_count: 2,
|
|
815
|
+
last_validated: new Date().toISOString(),
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
patterns.set("Candidate pattern", {
|
|
819
|
+
pattern_id: "p3",
|
|
820
|
+
state: "candidate",
|
|
821
|
+
helpful_count: 3,
|
|
822
|
+
harmful_count: 0,
|
|
823
|
+
last_validated: new Date().toISOString(),
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
patterns.set("Deprecated pattern", {
|
|
827
|
+
pattern_id: "p4",
|
|
828
|
+
state: "deprecated",
|
|
829
|
+
helpful_count: 1,
|
|
830
|
+
harmful_count: 5,
|
|
831
|
+
last_validated: new Date().toISOString(),
|
|
832
|
+
deprecated_at: new Date().toISOString(),
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
const formatted = formatPatternsWithMaturityForPrompt(patterns);
|
|
836
|
+
|
|
837
|
+
expect(formatted).toContain("## Proven Patterns");
|
|
838
|
+
expect(formatted).toContain("- Proven pattern");
|
|
839
|
+
expect(formatted).toContain("## Established Patterns");
|
|
840
|
+
expect(formatted).toContain("- Established pattern");
|
|
841
|
+
expect(formatted).toContain("## Candidate Patterns");
|
|
842
|
+
expect(formatted).toContain("- Candidate pattern");
|
|
843
|
+
expect(formatted).toContain("## Deprecated Patterns");
|
|
844
|
+
expect(formatted).toContain("- Deprecated pattern");
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
test("omits sections with no patterns", () => {
|
|
848
|
+
const patterns = new Map<string, PatternMaturity>();
|
|
849
|
+
|
|
850
|
+
patterns.set("Only proven", {
|
|
851
|
+
pattern_id: "p1",
|
|
852
|
+
state: "proven",
|
|
853
|
+
helpful_count: 10,
|
|
854
|
+
harmful_count: 0,
|
|
855
|
+
last_validated: new Date().toISOString(),
|
|
856
|
+
promoted_at: new Date().toISOString(),
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
const formatted = formatPatternsWithMaturityForPrompt(patterns);
|
|
860
|
+
|
|
861
|
+
expect(formatted).toContain("## Proven Patterns");
|
|
862
|
+
expect(formatted).not.toContain("## Established Patterns");
|
|
863
|
+
expect(formatted).not.toContain("## Candidate Patterns");
|
|
864
|
+
expect(formatted).not.toContain("## Deprecated Patterns");
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
test("includes pattern maturity labels", () => {
|
|
868
|
+
const patterns = new Map<string, PatternMaturity>();
|
|
869
|
+
|
|
870
|
+
patterns.set("Test pattern", {
|
|
871
|
+
pattern_id: "p1",
|
|
872
|
+
state: "proven",
|
|
873
|
+
helpful_count: 9,
|
|
874
|
+
harmful_count: 1,
|
|
875
|
+
last_validated: new Date().toISOString(),
|
|
876
|
+
promoted_at: new Date().toISOString(),
|
|
877
|
+
});
|
|
878
|
+
|
|
879
|
+
const formatted = formatPatternsWithMaturityForPrompt(patterns);
|
|
880
|
+
expect(formatted).toContain("[PROVEN - 90% helpful from 10 observations]");
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
test("maintains multiple patterns in same section", () => {
|
|
884
|
+
const patterns = new Map<string, PatternMaturity>();
|
|
885
|
+
|
|
886
|
+
patterns.set("Pattern A", {
|
|
887
|
+
pattern_id: "p1",
|
|
888
|
+
state: "proven",
|
|
889
|
+
helpful_count: 10,
|
|
890
|
+
harmful_count: 0,
|
|
891
|
+
last_validated: new Date().toISOString(),
|
|
892
|
+
promoted_at: new Date().toISOString(),
|
|
893
|
+
});
|
|
894
|
+
|
|
895
|
+
patterns.set("Pattern B", {
|
|
896
|
+
pattern_id: "p2",
|
|
897
|
+
state: "proven",
|
|
898
|
+
helpful_count: 8,
|
|
899
|
+
harmful_count: 1,
|
|
900
|
+
last_validated: new Date().toISOString(),
|
|
901
|
+
promoted_at: new Date().toISOString(),
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
const formatted = formatPatternsWithMaturityForPrompt(patterns);
|
|
905
|
+
expect(formatted).toContain("- Pattern A");
|
|
906
|
+
expect(formatted).toContain("- Pattern B");
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
test("formats section headers correctly", () => {
|
|
910
|
+
const patterns = new Map<string, PatternMaturity>();
|
|
911
|
+
|
|
912
|
+
patterns.set("Test", {
|
|
913
|
+
pattern_id: "p1",
|
|
914
|
+
state: "deprecated",
|
|
915
|
+
helpful_count: 0,
|
|
916
|
+
harmful_count: 5,
|
|
917
|
+
last_validated: new Date().toISOString(),
|
|
918
|
+
deprecated_at: new Date().toISOString(),
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
const formatted = formatPatternsWithMaturityForPrompt(patterns);
|
|
922
|
+
expect(formatted).toContain(
|
|
923
|
+
"## Deprecated Patterns\n\nAVOID these patterns - they have poor track records:",
|
|
924
|
+
);
|
|
925
|
+
});
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
// ============================================================================
|
|
929
|
+
// InMemoryMaturityStorage Tests
|
|
930
|
+
// ============================================================================
|
|
931
|
+
|
|
932
|
+
describe("InMemoryMaturityStorage", () => {
|
|
933
|
+
let storage: InMemoryMaturityStorage;
|
|
934
|
+
|
|
935
|
+
beforeEach(() => {
|
|
936
|
+
storage = new InMemoryMaturityStorage();
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
describe("store and get", () => {
|
|
940
|
+
test("stores and retrieves maturity by pattern ID", async () => {
|
|
941
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
942
|
+
await storage.store(maturity);
|
|
943
|
+
|
|
944
|
+
const retrieved = await storage.get("test-pattern");
|
|
945
|
+
expect(retrieved).toEqual(maturity);
|
|
946
|
+
});
|
|
947
|
+
|
|
948
|
+
test("returns null for non-existent pattern", async () => {
|
|
949
|
+
const retrieved = await storage.get("non-existent");
|
|
950
|
+
expect(retrieved).toBeNull();
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
test("overwrites existing maturity on store", async () => {
|
|
954
|
+
const maturity1 = createPatternMaturity("test-pattern");
|
|
955
|
+
await storage.store(maturity1);
|
|
956
|
+
|
|
957
|
+
const maturity2 = { ...maturity1, helpful_count: 5 };
|
|
958
|
+
await storage.store(maturity2);
|
|
959
|
+
|
|
960
|
+
const retrieved = await storage.get("test-pattern");
|
|
961
|
+
expect(retrieved?.helpful_count).toBe(5);
|
|
962
|
+
});
|
|
963
|
+
});
|
|
964
|
+
|
|
965
|
+
describe("getAll", () => {
|
|
966
|
+
test("returns empty array when no maturities stored", async () => {
|
|
967
|
+
const all = await storage.getAll();
|
|
968
|
+
expect(all).toEqual([]);
|
|
969
|
+
});
|
|
970
|
+
|
|
971
|
+
test("returns all stored maturities", async () => {
|
|
972
|
+
const m1 = createPatternMaturity("pattern-1");
|
|
973
|
+
const m2 = createPatternMaturity("pattern-2");
|
|
974
|
+
const m3 = createPatternMaturity("pattern-3");
|
|
975
|
+
|
|
976
|
+
await storage.store(m1);
|
|
977
|
+
await storage.store(m2);
|
|
978
|
+
await storage.store(m3);
|
|
979
|
+
|
|
980
|
+
const all = await storage.getAll();
|
|
981
|
+
expect(all).toHaveLength(3);
|
|
982
|
+
expect(all).toContainEqual(m1);
|
|
983
|
+
expect(all).toContainEqual(m2);
|
|
984
|
+
expect(all).toContainEqual(m3);
|
|
985
|
+
});
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
describe("getByState", () => {
|
|
989
|
+
test("returns empty array when no patterns match state", async () => {
|
|
990
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
991
|
+
await storage.store(maturity);
|
|
992
|
+
|
|
993
|
+
const proven = await storage.getByState("proven");
|
|
994
|
+
expect(proven).toEqual([]);
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
test("returns only patterns matching state", async () => {
|
|
998
|
+
const candidate: PatternMaturity = {
|
|
999
|
+
pattern_id: "p1",
|
|
1000
|
+
state: "candidate",
|
|
1001
|
+
helpful_count: 1,
|
|
1002
|
+
harmful_count: 0,
|
|
1003
|
+
last_validated: new Date().toISOString(),
|
|
1004
|
+
};
|
|
1005
|
+
|
|
1006
|
+
const proven: PatternMaturity = {
|
|
1007
|
+
pattern_id: "p2",
|
|
1008
|
+
state: "proven",
|
|
1009
|
+
helpful_count: 10,
|
|
1010
|
+
harmful_count: 0,
|
|
1011
|
+
last_validated: new Date().toISOString(),
|
|
1012
|
+
promoted_at: new Date().toISOString(),
|
|
1013
|
+
};
|
|
1014
|
+
|
|
1015
|
+
const deprecated: PatternMaturity = {
|
|
1016
|
+
pattern_id: "p3",
|
|
1017
|
+
state: "deprecated",
|
|
1018
|
+
helpful_count: 1,
|
|
1019
|
+
harmful_count: 5,
|
|
1020
|
+
last_validated: new Date().toISOString(),
|
|
1021
|
+
deprecated_at: new Date().toISOString(),
|
|
1022
|
+
};
|
|
1023
|
+
|
|
1024
|
+
await storage.store(candidate);
|
|
1025
|
+
await storage.store(proven);
|
|
1026
|
+
await storage.store(deprecated);
|
|
1027
|
+
|
|
1028
|
+
const provenResults = await storage.getByState("proven");
|
|
1029
|
+
expect(provenResults).toHaveLength(1);
|
|
1030
|
+
expect(provenResults[0]).toEqual(proven);
|
|
1031
|
+
|
|
1032
|
+
const candidateResults = await storage.getByState("candidate");
|
|
1033
|
+
expect(candidateResults).toHaveLength(1);
|
|
1034
|
+
expect(candidateResults[0]).toEqual(candidate);
|
|
1035
|
+
});
|
|
1036
|
+
|
|
1037
|
+
test("handles multiple patterns with same state", async () => {
|
|
1038
|
+
const p1: PatternMaturity = {
|
|
1039
|
+
pattern_id: "p1",
|
|
1040
|
+
state: "proven",
|
|
1041
|
+
helpful_count: 10,
|
|
1042
|
+
harmful_count: 0,
|
|
1043
|
+
last_validated: new Date().toISOString(),
|
|
1044
|
+
promoted_at: new Date().toISOString(),
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
const p2: PatternMaturity = {
|
|
1048
|
+
pattern_id: "p2",
|
|
1049
|
+
state: "proven",
|
|
1050
|
+
helpful_count: 8,
|
|
1051
|
+
harmful_count: 1,
|
|
1052
|
+
last_validated: new Date().toISOString(),
|
|
1053
|
+
promoted_at: new Date().toISOString(),
|
|
1054
|
+
};
|
|
1055
|
+
|
|
1056
|
+
await storage.store(p1);
|
|
1057
|
+
await storage.store(p2);
|
|
1058
|
+
|
|
1059
|
+
const proven = await storage.getByState("proven");
|
|
1060
|
+
expect(proven).toHaveLength(2);
|
|
1061
|
+
});
|
|
1062
|
+
});
|
|
1063
|
+
|
|
1064
|
+
describe("storeFeedback and getFeedback", () => {
|
|
1065
|
+
test("stores and retrieves feedback for pattern", async () => {
|
|
1066
|
+
const feedback = createFeedback({ pattern_id: "test-pattern" });
|
|
1067
|
+
await storage.storeFeedback(feedback);
|
|
1068
|
+
|
|
1069
|
+
const retrieved = await storage.getFeedback("test-pattern");
|
|
1070
|
+
expect(retrieved).toHaveLength(1);
|
|
1071
|
+
expect(retrieved[0]).toEqual(feedback);
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
test("returns empty array for pattern with no feedback", async () => {
|
|
1075
|
+
const feedback = await storage.getFeedback("non-existent");
|
|
1076
|
+
expect(feedback).toEqual([]);
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
test("stores multiple feedback events for same pattern", async () => {
|
|
1080
|
+
const f1 = createFeedback({
|
|
1081
|
+
pattern_id: "test-pattern",
|
|
1082
|
+
type: "helpful",
|
|
1083
|
+
});
|
|
1084
|
+
const f2 = createFeedback({
|
|
1085
|
+
pattern_id: "test-pattern",
|
|
1086
|
+
type: "harmful",
|
|
1087
|
+
});
|
|
1088
|
+
const f3 = createFeedback({
|
|
1089
|
+
pattern_id: "test-pattern",
|
|
1090
|
+
type: "helpful",
|
|
1091
|
+
});
|
|
1092
|
+
|
|
1093
|
+
await storage.storeFeedback(f1);
|
|
1094
|
+
await storage.storeFeedback(f2);
|
|
1095
|
+
await storage.storeFeedback(f3);
|
|
1096
|
+
|
|
1097
|
+
const retrieved = await storage.getFeedback("test-pattern");
|
|
1098
|
+
expect(retrieved).toHaveLength(3);
|
|
1099
|
+
});
|
|
1100
|
+
|
|
1101
|
+
test("filters feedback by pattern ID", async () => {
|
|
1102
|
+
const f1 = createFeedback({ pattern_id: "pattern-1" });
|
|
1103
|
+
const f2 = createFeedback({ pattern_id: "pattern-2" });
|
|
1104
|
+
const f3 = createFeedback({ pattern_id: "pattern-1" });
|
|
1105
|
+
|
|
1106
|
+
await storage.storeFeedback(f1);
|
|
1107
|
+
await storage.storeFeedback(f2);
|
|
1108
|
+
await storage.storeFeedback(f3);
|
|
1109
|
+
|
|
1110
|
+
const pattern1Feedback = await storage.getFeedback("pattern-1");
|
|
1111
|
+
expect(pattern1Feedback).toHaveLength(2);
|
|
1112
|
+
|
|
1113
|
+
const pattern2Feedback = await storage.getFeedback("pattern-2");
|
|
1114
|
+
expect(pattern2Feedback).toHaveLength(1);
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
test("preserves feedback event data", async () => {
|
|
1118
|
+
const feedback = createFeedback({
|
|
1119
|
+
pattern_id: "test-pattern",
|
|
1120
|
+
type: "helpful",
|
|
1121
|
+
weight: 0.75,
|
|
1122
|
+
timestamp: "2024-01-01T00:00:00Z",
|
|
1123
|
+
});
|
|
1124
|
+
|
|
1125
|
+
await storage.storeFeedback(feedback);
|
|
1126
|
+
const retrieved = await storage.getFeedback("test-pattern");
|
|
1127
|
+
|
|
1128
|
+
expect(retrieved[0].type).toBe("helpful");
|
|
1129
|
+
expect(retrieved[0].weight).toBe(0.75);
|
|
1130
|
+
expect(retrieved[0].timestamp).toBe("2024-01-01T00:00:00Z");
|
|
1131
|
+
});
|
|
1132
|
+
});
|
|
1133
|
+
|
|
1134
|
+
describe("integration: full workflow", () => {
|
|
1135
|
+
test("supports complete maturity tracking workflow", async () => {
|
|
1136
|
+
// Create and store initial maturity
|
|
1137
|
+
const maturity = createPatternMaturity("test-pattern");
|
|
1138
|
+
await storage.store(maturity);
|
|
1139
|
+
|
|
1140
|
+
// Add feedback events
|
|
1141
|
+
const feedback1 = createFeedback({ type: "helpful" });
|
|
1142
|
+
const feedback2 = createFeedback({ type: "helpful" });
|
|
1143
|
+
const feedback3 = createFeedback({ type: "helpful" });
|
|
1144
|
+
await storage.storeFeedback(feedback1);
|
|
1145
|
+
await storage.storeFeedback(feedback2);
|
|
1146
|
+
await storage.storeFeedback(feedback3);
|
|
1147
|
+
|
|
1148
|
+
// Retrieve and update maturity
|
|
1149
|
+
const current = await storage.get("test-pattern");
|
|
1150
|
+
const feedbackEvents = await storage.getFeedback("test-pattern");
|
|
1151
|
+
const updated = updatePatternMaturity(current!, feedbackEvents);
|
|
1152
|
+
await storage.store(updated);
|
|
1153
|
+
|
|
1154
|
+
// Verify final state
|
|
1155
|
+
const final = await storage.get("test-pattern");
|
|
1156
|
+
expect(final?.state).toBe("established");
|
|
1157
|
+
expect(final?.helpful_count).toBe(3);
|
|
1158
|
+
});
|
|
1159
|
+
});
|
|
1160
|
+
});
|
package/src/streams/events.ts
CHANGED
|
@@ -174,6 +174,28 @@ export type TaskProgressEvent = z.infer<typeof TaskProgressEventSchema>;
|
|
|
174
174
|
export type TaskCompletedEvent = z.infer<typeof TaskCompletedEventSchema>;
|
|
175
175
|
export type TaskBlockedEvent = z.infer<typeof TaskBlockedEventSchema>;
|
|
176
176
|
|
|
177
|
+
// ============================================================================
|
|
178
|
+
// Session State Types
|
|
179
|
+
// ============================================================================
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Shared session state for Agent Mail and Swarm Mail
|
|
183
|
+
*
|
|
184
|
+
* Common fields for tracking agent coordination session across both
|
|
185
|
+
* the MCP-based implementation (agent-mail) and the embedded event-sourced
|
|
186
|
+
* implementation (swarm-mail).
|
|
187
|
+
*/
|
|
188
|
+
export interface MailSessionState {
|
|
189
|
+
/** Project key (usually absolute path) */
|
|
190
|
+
projectKey: string;
|
|
191
|
+
/** Agent name for this session */
|
|
192
|
+
agentName: string;
|
|
193
|
+
/** Active reservation IDs */
|
|
194
|
+
reservations: number[];
|
|
195
|
+
/** Session start timestamp (ISO-8601) */
|
|
196
|
+
startedAt: string;
|
|
197
|
+
}
|
|
198
|
+
|
|
177
199
|
// ============================================================================
|
|
178
200
|
// Event Helpers
|
|
179
201
|
// ============================================================================
|
package/src/swarm-mail.ts
CHANGED
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
checkSwarmHealth,
|
|
29
29
|
} from "./streams/swarm-mail";
|
|
30
30
|
import { getActiveReservations } from "./streams/projections";
|
|
31
|
+
import type { MailSessionState } from "./streams/events";
|
|
31
32
|
import {
|
|
32
33
|
existsSync,
|
|
33
34
|
mkdirSync,
|
|
@@ -47,13 +48,12 @@ interface ToolContext {
|
|
|
47
48
|
sessionID: string;
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
51
|
+
/**
|
|
52
|
+
* Swarm Mail session state
|
|
53
|
+
* @deprecated Use MailSessionState from streams/events.ts instead
|
|
54
|
+
* This is kept for backward compatibility and re-exported as an alias
|
|
55
|
+
*/
|
|
56
|
+
export type SwarmMailState = MailSessionState;
|
|
57
57
|
|
|
58
58
|
/** Init tool arguments */
|
|
59
59
|
interface InitArgs {
|