@agentmemory/agentmemory 0.7.0 → 0.7.3

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.
Files changed (218) hide show
  1. package/AGENTS.md +2 -2
  2. package/README.md +76 -82
  3. package/dist/cli.mjs +99 -32
  4. package/dist/cli.mjs.map +1 -1
  5. package/dist/index.mjs +4 -2
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/{src-QxitMPfJ.mjs → src-sYZDDbiA.mjs} +5 -3
  8. package/dist/src-sYZDDbiA.mjs.map +1 -0
  9. package/dist/standalone.mjs +1 -1
  10. package/dist/standalone.mjs.map +1 -1
  11. package/package.json +11 -1
  12. package/plugin/.claude-plugin/plugin.json +1 -1
  13. package/plugin/scripts/notification.d.mts +1 -0
  14. package/plugin/scripts/notification.mjs.map +1 -0
  15. package/plugin/scripts/post-tool-failure.d.mts +1 -0
  16. package/plugin/scripts/post-tool-failure.mjs.map +1 -0
  17. package/plugin/scripts/post-tool-use.d.mts +1 -0
  18. package/plugin/scripts/post-tool-use.mjs.map +1 -0
  19. package/plugin/scripts/pre-compact.d.mts +1 -0
  20. package/plugin/scripts/pre-compact.mjs.map +1 -0
  21. package/plugin/scripts/pre-tool-use.d.mts +1 -0
  22. package/plugin/scripts/pre-tool-use.mjs.map +1 -0
  23. package/plugin/scripts/prompt-submit.d.mts +1 -0
  24. package/plugin/scripts/prompt-submit.mjs.map +1 -0
  25. package/plugin/scripts/session-end.d.mts +1 -0
  26. package/plugin/scripts/session-end.mjs.map +1 -0
  27. package/plugin/scripts/session-start.d.mts +1 -0
  28. package/plugin/scripts/session-start.mjs.map +1 -0
  29. package/plugin/scripts/stop.d.mts +1 -0
  30. package/plugin/scripts/stop.mjs.map +1 -0
  31. package/plugin/scripts/subagent-start.d.mts +1 -0
  32. package/plugin/scripts/subagent-start.mjs.map +1 -0
  33. package/plugin/scripts/subagent-stop.d.mts +1 -0
  34. package/plugin/scripts/subagent-stop.mjs.map +1 -0
  35. package/plugin/scripts/task-completed.d.mts +1 -0
  36. package/plugin/scripts/task-completed.mjs.map +1 -0
  37. package/.claude-plugin/marketplace.json +0 -14
  38. package/.github/workflows/ci.yml +0 -22
  39. package/.github/workflows/publish.yml +0 -28
  40. package/assets/banner.png +0 -0
  41. package/assets/demo.gif +0 -0
  42. package/assets/demo.mp4 +0 -0
  43. package/benchmark/QUALITY.md +0 -73
  44. package/benchmark/REAL-EMBEDDINGS.md +0 -67
  45. package/benchmark/SCALE.md +0 -110
  46. package/benchmark/dataset.ts +0 -293
  47. package/benchmark/quality-eval.ts +0 -643
  48. package/benchmark/real-embeddings-eval.ts +0 -405
  49. package/benchmark/scale-eval.ts +0 -398
  50. package/dist/src-QxitMPfJ.mjs.map +0 -1
  51. package/src/auth.ts +0 -12
  52. package/src/cli.ts +0 -159
  53. package/src/config.ts +0 -221
  54. package/src/eval/metrics-store.ts +0 -65
  55. package/src/eval/quality.ts +0 -51
  56. package/src/eval/schemas.ts +0 -124
  57. package/src/eval/self-correct.ts +0 -28
  58. package/src/eval/validator.ts +0 -31
  59. package/src/functions/actions.ts +0 -288
  60. package/src/functions/audit.ts +0 -61
  61. package/src/functions/auto-forget.ts +0 -169
  62. package/src/functions/branch-aware.ts +0 -169
  63. package/src/functions/cascade.ts +0 -80
  64. package/src/functions/checkpoints.ts +0 -209
  65. package/src/functions/claude-bridge.ts +0 -161
  66. package/src/functions/compress.ts +0 -194
  67. package/src/functions/consolidate.ts +0 -212
  68. package/src/functions/consolidation-pipeline.ts +0 -258
  69. package/src/functions/context.ts +0 -169
  70. package/src/functions/crystallize.ts +0 -293
  71. package/src/functions/dedup.ts +0 -57
  72. package/src/functions/diagnostics.ts +0 -785
  73. package/src/functions/enrich.ts +0 -132
  74. package/src/functions/evict.ts +0 -163
  75. package/src/functions/export-import.ts +0 -508
  76. package/src/functions/facets.ts +0 -248
  77. package/src/functions/file-index.ts +0 -106
  78. package/src/functions/flow-compress.ts +0 -214
  79. package/src/functions/frontier.ts +0 -196
  80. package/src/functions/governance.ts +0 -131
  81. package/src/functions/graph-retrieval.ts +0 -277
  82. package/src/functions/graph.ts +0 -275
  83. package/src/functions/leases.ts +0 -216
  84. package/src/functions/lessons.ts +0 -253
  85. package/src/functions/mesh.ts +0 -434
  86. package/src/functions/migrate.ts +0 -165
  87. package/src/functions/observe.ts +0 -144
  88. package/src/functions/obsidian-export.ts +0 -310
  89. package/src/functions/patterns.ts +0 -138
  90. package/src/functions/privacy.ts +0 -39
  91. package/src/functions/profile.ts +0 -155
  92. package/src/functions/query-expansion.ts +0 -186
  93. package/src/functions/relations.ts +0 -237
  94. package/src/functions/remember.ts +0 -162
  95. package/src/functions/retention.ts +0 -235
  96. package/src/functions/routines.ts +0 -289
  97. package/src/functions/search.ts +0 -80
  98. package/src/functions/sentinels.ts +0 -417
  99. package/src/functions/signals.ts +0 -186
  100. package/src/functions/sketches.ts +0 -274
  101. package/src/functions/sliding-window.ts +0 -257
  102. package/src/functions/smart-search.ts +0 -115
  103. package/src/functions/snapshot.ts +0 -219
  104. package/src/functions/summarize.ts +0 -155
  105. package/src/functions/team.ts +0 -147
  106. package/src/functions/temporal-graph.ts +0 -476
  107. package/src/functions/timeline.ts +0 -138
  108. package/src/functions/verify.ts +0 -117
  109. package/src/health/monitor.ts +0 -110
  110. package/src/health/thresholds.ts +0 -73
  111. package/src/hooks/notification.ts +0 -52
  112. package/src/hooks/post-tool-failure.ts +0 -58
  113. package/src/hooks/post-tool-use.ts +0 -62
  114. package/src/hooks/pre-compact.ts +0 -60
  115. package/src/hooks/pre-tool-use.ts +0 -72
  116. package/src/hooks/prompt-submit.ts +0 -46
  117. package/src/hooks/session-end.ts +0 -71
  118. package/src/hooks/session-start.ts +0 -48
  119. package/src/hooks/stop.ts +0 -39
  120. package/src/hooks/subagent-start.ts +0 -49
  121. package/src/hooks/subagent-stop.ts +0 -54
  122. package/src/hooks/task-completed.ts +0 -54
  123. package/src/index.ts +0 -342
  124. package/src/mcp/in-memory-kv.ts +0 -61
  125. package/src/mcp/server.ts +0 -1455
  126. package/src/mcp/standalone.ts +0 -177
  127. package/src/mcp/tools-registry.ts +0 -769
  128. package/src/mcp/transport.ts +0 -91
  129. package/src/prompts/compression.ts +0 -67
  130. package/src/prompts/consolidation.ts +0 -48
  131. package/src/prompts/graph-extraction.ts +0 -35
  132. package/src/prompts/summary.ts +0 -38
  133. package/src/prompts/xml.ts +0 -26
  134. package/src/providers/agent-sdk.ts +0 -34
  135. package/src/providers/anthropic.ts +0 -35
  136. package/src/providers/circuit-breaker.ts +0 -82
  137. package/src/providers/embedding/cohere.ts +0 -46
  138. package/src/providers/embedding/gemini.ts +0 -54
  139. package/src/providers/embedding/index.ts +0 -39
  140. package/src/providers/embedding/local.ts +0 -52
  141. package/src/providers/embedding/openai.ts +0 -45
  142. package/src/providers/embedding/openrouter.ts +0 -51
  143. package/src/providers/embedding/voyage.ts +0 -46
  144. package/src/providers/fallback-chain.ts +0 -31
  145. package/src/providers/index.ts +0 -84
  146. package/src/providers/openrouter.ts +0 -71
  147. package/src/providers/resilient.ts +0 -37
  148. package/src/state/hybrid-search.ts +0 -295
  149. package/src/state/index-persistence.ts +0 -63
  150. package/src/state/keyed-mutex.ts +0 -18
  151. package/src/state/kv.ts +0 -33
  152. package/src/state/schema.ts +0 -71
  153. package/src/state/search-index.ts +0 -245
  154. package/src/state/stemmer.ts +0 -104
  155. package/src/state/synonyms.ts +0 -63
  156. package/src/state/vector-index.ts +0 -130
  157. package/src/telemetry/setup.ts +0 -116
  158. package/src/triggers/api.ts +0 -1904
  159. package/src/triggers/events.ts +0 -71
  160. package/src/types.ts +0 -769
  161. package/src/version.ts +0 -1
  162. package/src/viewer/index.html +0 -2497
  163. package/src/viewer/server.ts +0 -207
  164. package/src/xenova.d.ts +0 -3
  165. package/test/actions.test.ts +0 -490
  166. package/test/audit.test.ts +0 -108
  167. package/test/auto-forget.test.ts +0 -188
  168. package/test/cascade.test.ts +0 -277
  169. package/test/checkpoints.test.ts +0 -493
  170. package/test/circuit-breaker.test.ts +0 -107
  171. package/test/claude-bridge.test.ts +0 -178
  172. package/test/confidence.test.ts +0 -247
  173. package/test/consistency.test.ts +0 -61
  174. package/test/consolidation-pipeline.test.ts +0 -251
  175. package/test/crystallize.test.ts +0 -521
  176. package/test/diagnostics.test.ts +0 -638
  177. package/test/embedding-provider.test.ts +0 -49
  178. package/test/enrich.test.ts +0 -209
  179. package/test/eval.test.ts +0 -300
  180. package/test/export-import.test.ts +0 -251
  181. package/test/facets.test.ts +0 -448
  182. package/test/fallback-chain.test.ts +0 -93
  183. package/test/frontier.test.ts +0 -485
  184. package/test/governance.test.ts +0 -147
  185. package/test/graph-retrieval.test.ts +0 -186
  186. package/test/graph.test.ts +0 -160
  187. package/test/helpers/mocks.ts +0 -40
  188. package/test/hybrid-search.test.ts +0 -145
  189. package/test/index-persistence.test.ts +0 -124
  190. package/test/integration.test.ts +0 -265
  191. package/test/leases.test.ts +0 -399
  192. package/test/mcp-prompts.test.ts +0 -218
  193. package/test/mcp-resources.test.ts +0 -286
  194. package/test/mcp-standalone.test.ts +0 -113
  195. package/test/mesh.test.ts +0 -700
  196. package/test/privacy.test.ts +0 -87
  197. package/test/profile.test.ts +0 -161
  198. package/test/query-expansion.test.ts +0 -154
  199. package/test/relations.test.ts +0 -198
  200. package/test/retention.test.ts +0 -245
  201. package/test/routines.test.ts +0 -497
  202. package/test/schema-fingerprint.test.ts +0 -81
  203. package/test/schema.test.ts +0 -42
  204. package/test/search-index.test.ts +0 -128
  205. package/test/sentinels.test.ts +0 -626
  206. package/test/signals.test.ts +0 -410
  207. package/test/sketches.test.ts +0 -549
  208. package/test/sliding-window.test.ts +0 -199
  209. package/test/smart-search.test.ts +0 -169
  210. package/test/snapshot.test.ts +0 -165
  211. package/test/team.test.ts +0 -156
  212. package/test/temporal-graph.test.ts +0 -378
  213. package/test/timeline.test.ts +0 -148
  214. package/test/vector-index.test.ts +0 -79
  215. package/test/verify.test.ts +0 -209
  216. package/test/xml.test.ts +0 -65
  217. package/tsconfig.json +0 -22
  218. package/tsdown.config.ts +0 -62
@@ -1,521 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from "vitest";
2
-
3
- vi.mock("iii-sdk", () => ({
4
- getContext: () => ({
5
- logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn() },
6
- }),
7
- }));
8
-
9
- import { registerCrystallizeFunction } from "../src/functions/crystallize.js";
10
- import type { Action, Crystal, MemoryProvider } from "../src/types.js";
11
-
12
- function mockKV() {
13
- const store = new Map<string, Map<string, unknown>>();
14
- return {
15
- get: async <T>(scope: string, key: string): Promise<T | null> => {
16
- return (store.get(scope)?.get(key) as T) ?? null;
17
- },
18
- set: async <T>(scope: string, key: string, data: T): Promise<T> => {
19
- if (!store.has(scope)) store.set(scope, new Map());
20
- store.get(scope)!.set(key, data);
21
- return data;
22
- },
23
- delete: async (scope: string, key: string): Promise<void> => {
24
- store.get(scope)?.delete(key);
25
- },
26
- list: async <T>(scope: string): Promise<T[]> => {
27
- const entries = store.get(scope);
28
- return entries ? (Array.from(entries.values()) as T[]) : [];
29
- },
30
- };
31
- }
32
-
33
- function mockSdk() {
34
- const functions = new Map<string, Function>();
35
- return {
36
- registerFunction: (opts: { id: string }, handler: Function) => {
37
- functions.set(opts.id, handler);
38
- },
39
- registerTrigger: () => {},
40
- trigger: async (id: string, data: unknown) => {
41
- const fn = functions.get(id);
42
- if (!fn) throw new Error(`No function: ${id}`);
43
- return fn(data);
44
- },
45
- };
46
- }
47
-
48
- function mockProvider(): MemoryProvider {
49
- return {
50
- name: "test",
51
- compress: vi.fn(),
52
- summarize: vi.fn().mockResolvedValue(
53
- '{"narrative":"test","keyOutcomes":["done"],"filesAffected":["a.ts"],"lessons":["learned"]}',
54
- ),
55
- };
56
- }
57
-
58
- function makeAction(overrides: Partial<Action> & { id: string }): Action {
59
- return {
60
- title: "Test action",
61
- description: "A test action",
62
- status: "done",
63
- priority: 5,
64
- createdAt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString(),
65
- updatedAt: new Date().toISOString(),
66
- createdBy: "agent-1",
67
- tags: [],
68
- sourceObservationIds: [],
69
- sourceMemoryIds: [],
70
- ...overrides,
71
- };
72
- }
73
-
74
- describe("Crystallize Functions", () => {
75
- let sdk: ReturnType<typeof mockSdk>;
76
- let kv: ReturnType<typeof mockKV>;
77
- let provider: MemoryProvider;
78
-
79
- beforeEach(() => {
80
- sdk = mockSdk();
81
- kv = mockKV();
82
- provider = mockProvider();
83
- registerCrystallizeFunction(sdk as never, kv as never, provider);
84
- });
85
-
86
- describe("mem::crystallize", () => {
87
- it("crystallizes completed actions with valid JSON response", async () => {
88
- const action = makeAction({ id: "act_1", title: "Fix bug", status: "done" });
89
- await kv.set("mem:actions", action.id, action);
90
-
91
- const result = (await sdk.trigger("mem::crystallize", {
92
- actionIds: ["act_1"],
93
- project: "webapp",
94
- sessionId: "sess_1",
95
- })) as { success: boolean; crystal: Crystal };
96
-
97
- expect(result.success).toBe(true);
98
- expect(result.crystal.id).toMatch(/^crys_/);
99
- expect(result.crystal.narrative).toBe("test");
100
- expect(result.crystal.keyOutcomes).toEqual(["done"]);
101
- expect(result.crystal.filesAffected).toEqual(["a.ts"]);
102
- expect(result.crystal.lessons).toEqual(["learned"]);
103
- expect(result.crystal.sourceActionIds).toEqual(["act_1"]);
104
- expect(result.crystal.project).toBe("webapp");
105
- expect(result.crystal.sessionId).toBe("sess_1");
106
- expect(result.crystal.createdAt).toBeDefined();
107
- });
108
-
109
- it("marks source actions with crystallizedInto", async () => {
110
- const action = makeAction({ id: "act_mark", status: "done" });
111
- await kv.set("mem:actions", action.id, action);
112
-
113
- const result = (await sdk.trigger("mem::crystallize", {
114
- actionIds: ["act_mark"],
115
- })) as { success: boolean; crystal: Crystal };
116
-
117
- expect(result.success).toBe(true);
118
-
119
- const updated = await kv.get<Action>("mem:actions", "act_mark");
120
- expect(updated!.crystallizedInto).toBe(result.crystal.id);
121
- });
122
-
123
- it("falls back to raw text when provider returns non-JSON", async () => {
124
- (provider.summarize as ReturnType<typeof vi.fn>).mockResolvedValue(
125
- "Just a plain text summary with no JSON.",
126
- );
127
-
128
- const action = makeAction({ id: "act_nojson", status: "done" });
129
- await kv.set("mem:actions", action.id, action);
130
-
131
- const result = (await sdk.trigger("mem::crystallize", {
132
- actionIds: ["act_nojson"],
133
- })) as { success: boolean; crystal: Crystal };
134
-
135
- expect(result.success).toBe(true);
136
- expect(result.crystal.narrative).toBe(
137
- "Just a plain text summary with no JSON.",
138
- );
139
- expect(result.crystal.keyOutcomes).toEqual([]);
140
- expect(result.crystal.filesAffected).toEqual([]);
141
- expect(result.crystal.lessons).toEqual([]);
142
- });
143
-
144
- it("returns error for non-existent action", async () => {
145
- const result = (await sdk.trigger("mem::crystallize", {
146
- actionIds: ["act_ghost"],
147
- })) as { success: boolean; error: string };
148
-
149
- expect(result.success).toBe(false);
150
- expect(result.error).toContain("action not found: act_ghost");
151
- });
152
-
153
- it("returns error for non-done action", async () => {
154
- const action = makeAction({ id: "act_pending", status: "pending" });
155
- await kv.set("mem:actions", action.id, action);
156
-
157
- const result = (await sdk.trigger("mem::crystallize", {
158
- actionIds: ["act_pending"],
159
- })) as { success: boolean; error: string };
160
-
161
- expect(result.success).toBe(false);
162
- expect(result.error).toContain('status "pending"');
163
- });
164
-
165
- it("returns error for empty actionIds", async () => {
166
- const result = (await sdk.trigger("mem::crystallize", {
167
- actionIds: [],
168
- })) as { success: boolean; error: string };
169
-
170
- expect(result.success).toBe(false);
171
- expect(result.error).toContain("actionIds is required");
172
- });
173
-
174
- it("returns error when actionIds is missing", async () => {
175
- const result = (await sdk.trigger("mem::crystallize", {})) as {
176
- success: boolean;
177
- error: string;
178
- };
179
-
180
- expect(result.success).toBe(false);
181
- expect(result.error).toContain("actionIds is required");
182
- });
183
-
184
- it("accepts cancelled actions", async () => {
185
- const action = makeAction({ id: "act_cancel", status: "cancelled" });
186
- await kv.set("mem:actions", action.id, action);
187
-
188
- const result = (await sdk.trigger("mem::crystallize", {
189
- actionIds: ["act_cancel"],
190
- })) as { success: boolean; crystal: Crystal };
191
-
192
- expect(result.success).toBe(true);
193
- expect(result.crystal.sourceActionIds).toEqual(["act_cancel"]);
194
- });
195
-
196
- it("crystallizes multiple actions in one call", async () => {
197
- const a1 = makeAction({ id: "act_m1", status: "done", title: "First" });
198
- const a2 = makeAction({ id: "act_m2", status: "done", title: "Second" });
199
- await kv.set("mem:actions", a1.id, a1);
200
- await kv.set("mem:actions", a2.id, a2);
201
-
202
- const result = (await sdk.trigger("mem::crystallize", {
203
- actionIds: ["act_m1", "act_m2"],
204
- })) as { success: boolean; crystal: Crystal };
205
-
206
- expect(result.success).toBe(true);
207
- expect(result.crystal.sourceActionIds).toEqual(["act_m1", "act_m2"]);
208
- });
209
-
210
- it("returns failure when provider throws", async () => {
211
- (provider.summarize as ReturnType<typeof vi.fn>).mockRejectedValue(
212
- new Error("API down"),
213
- );
214
-
215
- const action = makeAction({ id: "act_fail", status: "done" });
216
- await kv.set("mem:actions", action.id, action);
217
-
218
- const result = (await sdk.trigger("mem::crystallize", {
219
- actionIds: ["act_fail"],
220
- })) as { success: boolean; error: string };
221
-
222
- expect(result.success).toBe(false);
223
- expect(result.error).toContain("crystallization failed");
224
- expect(result.error).toContain("API down");
225
- });
226
- });
227
-
228
- describe("mem::crystal-list", () => {
229
- beforeEach(async () => {
230
- const c1: Crystal = {
231
- id: "crys_1",
232
- narrative: "First crystal",
233
- keyOutcomes: [],
234
- filesAffected: [],
235
- lessons: [],
236
- sourceActionIds: ["act_1"],
237
- project: "alpha",
238
- sessionId: "sess_a",
239
- createdAt: new Date("2025-01-01").toISOString(),
240
- };
241
- const c2: Crystal = {
242
- id: "crys_2",
243
- narrative: "Second crystal",
244
- keyOutcomes: [],
245
- filesAffected: [],
246
- lessons: [],
247
- sourceActionIds: ["act_2"],
248
- project: "beta",
249
- sessionId: "sess_b",
250
- createdAt: new Date("2025-02-01").toISOString(),
251
- };
252
- const c3: Crystal = {
253
- id: "crys_3",
254
- narrative: "Third crystal",
255
- keyOutcomes: [],
256
- filesAffected: [],
257
- lessons: [],
258
- sourceActionIds: ["act_3"],
259
- project: "alpha",
260
- sessionId: "sess_a",
261
- createdAt: new Date("2025-03-01").toISOString(),
262
- };
263
- await kv.set("mem:crystals", c1.id, c1);
264
- await kv.set("mem:crystals", c2.id, c2);
265
- await kv.set("mem:crystals", c3.id, c3);
266
- });
267
-
268
- it("returns all crystals sorted by createdAt desc", async () => {
269
- const result = (await sdk.trigger("mem::crystal-list", {})) as {
270
- success: boolean;
271
- crystals: Crystal[];
272
- };
273
-
274
- expect(result.success).toBe(true);
275
- expect(result.crystals.length).toBe(3);
276
- expect(result.crystals[0].id).toBe("crys_3");
277
- expect(result.crystals[1].id).toBe("crys_2");
278
- expect(result.crystals[2].id).toBe("crys_1");
279
- });
280
-
281
- it("filters by project", async () => {
282
- const result = (await sdk.trigger("mem::crystal-list", {
283
- project: "alpha",
284
- })) as { success: boolean; crystals: Crystal[] };
285
-
286
- expect(result.success).toBe(true);
287
- expect(result.crystals.length).toBe(2);
288
- expect(result.crystals.every((c) => c.project === "alpha")).toBe(true);
289
- });
290
-
291
- it("filters by sessionId", async () => {
292
- const result = (await sdk.trigger("mem::crystal-list", {
293
- sessionId: "sess_b",
294
- })) as { success: boolean; crystals: Crystal[] };
295
-
296
- expect(result.success).toBe(true);
297
- expect(result.crystals.length).toBe(1);
298
- expect(result.crystals[0].id).toBe("crys_2");
299
- });
300
-
301
- it("respects limit", async () => {
302
- const result = (await sdk.trigger("mem::crystal-list", {
303
- limit: 1,
304
- })) as { success: boolean; crystals: Crystal[] };
305
-
306
- expect(result.success).toBe(true);
307
- expect(result.crystals.length).toBe(1);
308
- expect(result.crystals[0].id).toBe("crys_3");
309
- });
310
- });
311
-
312
- describe("mem::crystal-get", () => {
313
- it("returns crystal by id", async () => {
314
- const crystal: Crystal = {
315
- id: "crys_get_1",
316
- narrative: "Found it",
317
- keyOutcomes: ["yes"],
318
- filesAffected: ["b.ts"],
319
- lessons: ["test"],
320
- sourceActionIds: ["act_x"],
321
- createdAt: new Date().toISOString(),
322
- };
323
- await kv.set("mem:crystals", crystal.id, crystal);
324
-
325
- const result = (await sdk.trigger("mem::crystal-get", {
326
- crystalId: "crys_get_1",
327
- })) as { success: boolean; crystal: Crystal };
328
-
329
- expect(result.success).toBe(true);
330
- expect(result.crystal.id).toBe("crys_get_1");
331
- expect(result.crystal.narrative).toBe("Found it");
332
- });
333
-
334
- it("returns error for non-existent crystal", async () => {
335
- const result = (await sdk.trigger("mem::crystal-get", {
336
- crystalId: "crys_missing",
337
- })) as { success: boolean; error: string };
338
-
339
- expect(result.success).toBe(false);
340
- expect(result.error).toContain("crystal not found");
341
- });
342
-
343
- it("returns error when crystalId is missing", async () => {
344
- const result = (await sdk.trigger("mem::crystal-get", {})) as {
345
- success: boolean;
346
- error: string;
347
- };
348
-
349
- expect(result.success).toBe(false);
350
- expect(result.error).toContain("crystalId is required");
351
- });
352
- });
353
-
354
- describe("mem::auto-crystallize", () => {
355
- it("returns group summaries in dryRun mode", async () => {
356
- const action = makeAction({
357
- id: "act_dry",
358
- status: "done",
359
- project: "proj",
360
- });
361
- await kv.set("mem:actions", action.id, action);
362
-
363
- const result = (await sdk.trigger("mem::auto-crystallize", {
364
- dryRun: true,
365
- })) as {
366
- success: boolean;
367
- dryRun: boolean;
368
- groupCount: number;
369
- groups: { groupKey: string; actionCount: number; actionIds: string[] }[];
370
- crystalIds: string[];
371
- };
372
-
373
- expect(result.success).toBe(true);
374
- expect(result.dryRun).toBe(true);
375
- expect(result.groupCount).toBe(1);
376
- expect(result.groups[0].actionIds).toContain("act_dry");
377
- expect(result.crystalIds).toEqual([]);
378
- });
379
-
380
- it("groups by parentId when present", async () => {
381
- const parent = makeAction({
382
- id: "act_parent",
383
- status: "done",
384
- parentId: undefined,
385
- });
386
- const child1 = makeAction({
387
- id: "act_child1",
388
- status: "done",
389
- parentId: "act_parent",
390
- });
391
- const child2 = makeAction({
392
- id: "act_child2",
393
- status: "done",
394
- parentId: "act_parent",
395
- });
396
- await kv.set("mem:actions", parent.id, parent);
397
- await kv.set("mem:actions", child1.id, child1);
398
- await kv.set("mem:actions", child2.id, child2);
399
-
400
- const result = (await sdk.trigger("mem::auto-crystallize", {
401
- dryRun: true,
402
- })) as {
403
- success: boolean;
404
- groups: { groupKey: string; actionCount: number; actionIds: string[] }[];
405
- };
406
-
407
- expect(result.success).toBe(true);
408
- const parentGroup = result.groups.find((g) => g.groupKey === "act_parent");
409
- expect(parentGroup).toBeDefined();
410
- expect(parentGroup!.actionCount).toBe(2);
411
- });
412
-
413
- it("groups by project when no parentId", async () => {
414
- const a1 = makeAction({ id: "act_proj1", status: "done", project: "webapp" });
415
- const a2 = makeAction({ id: "act_proj2", status: "done", project: "webapp" });
416
- const a3 = makeAction({ id: "act_proj3", status: "done", project: "api" });
417
- await kv.set("mem:actions", a1.id, a1);
418
- await kv.set("mem:actions", a2.id, a2);
419
- await kv.set("mem:actions", a3.id, a3);
420
-
421
- const result = (await sdk.trigger("mem::auto-crystallize", {
422
- dryRun: true,
423
- })) as {
424
- success: boolean;
425
- groups: { groupKey: string; actionCount: number }[];
426
- };
427
-
428
- expect(result.success).toBe(true);
429
- const webGroup = result.groups.find((g) => g.groupKey === "webapp");
430
- const apiGroup = result.groups.find((g) => g.groupKey === "api");
431
- expect(webGroup).toBeDefined();
432
- expect(webGroup!.actionCount).toBe(2);
433
- expect(apiGroup).toBeDefined();
434
- expect(apiGroup!.actionCount).toBe(1);
435
- });
436
-
437
- it("skips already-crystallized actions", async () => {
438
- const action = makeAction({
439
- id: "act_already",
440
- status: "done",
441
- crystallizedInto: "crys_existing",
442
- });
443
- await kv.set("mem:actions", action.id, action);
444
-
445
- const result = (await sdk.trigger("mem::auto-crystallize", {
446
- dryRun: true,
447
- })) as { success: boolean; groupCount: number };
448
-
449
- expect(result.success).toBe(true);
450
- expect(result.groupCount).toBe(0);
451
- });
452
-
453
- it("skips actions newer than threshold", async () => {
454
- const recentAction = makeAction({
455
- id: "act_recent",
456
- status: "done",
457
- createdAt: new Date().toISOString(),
458
- });
459
- await kv.set("mem:actions", recentAction.id, recentAction);
460
-
461
- const result = (await sdk.trigger("mem::auto-crystallize", {
462
- olderThanDays: 7,
463
- dryRun: true,
464
- })) as { success: boolean; groupCount: number };
465
-
466
- expect(result.success).toBe(true);
467
- expect(result.groupCount).toBe(0);
468
- });
469
-
470
- it("creates crystals for each group in non-dryRun mode", async () => {
471
- const a1 = makeAction({ id: "act_auto1", status: "done", project: "proj1" });
472
- const a2 = makeAction({ id: "act_auto2", status: "done", project: "proj2" });
473
- await kv.set("mem:actions", a1.id, a1);
474
- await kv.set("mem:actions", a2.id, a2);
475
-
476
- const result = (await sdk.trigger("mem::auto-crystallize", {})) as {
477
- success: boolean;
478
- groupCount: number;
479
- crystalIds: string[];
480
- };
481
-
482
- expect(result.success).toBe(true);
483
- expect(result.groupCount).toBe(2);
484
- expect(result.crystalIds.length).toBe(2);
485
- expect(result.crystalIds[0]).toMatch(/^crys_/);
486
- expect(result.crystalIds[1]).toMatch(/^crys_/);
487
- });
488
-
489
- it("filters by project when specified", async () => {
490
- const a1 = makeAction({ id: "act_fp1", status: "done", project: "keep" });
491
- const a2 = makeAction({ id: "act_fp2", status: "done", project: "skip" });
492
- await kv.set("mem:actions", a1.id, a1);
493
- await kv.set("mem:actions", a2.id, a2);
494
-
495
- const result = (await sdk.trigger("mem::auto-crystallize", {
496
- project: "keep",
497
- dryRun: true,
498
- })) as {
499
- success: boolean;
500
- groupCount: number;
501
- groups: { groupKey: string; actionCount: number }[];
502
- };
503
-
504
- expect(result.success).toBe(true);
505
- expect(result.groupCount).toBe(1);
506
- expect(result.groups[0].groupKey).toBe("keep");
507
- });
508
-
509
- it("returns empty when no qualifying actions exist", async () => {
510
- const result = (await sdk.trigger("mem::auto-crystallize", {})) as {
511
- success: boolean;
512
- groupCount: number;
513
- crystalIds: string[];
514
- };
515
-
516
- expect(result.success).toBe(true);
517
- expect(result.groupCount).toBe(0);
518
- expect(result.crystalIds).toEqual([]);
519
- });
520
- });
521
- });