recallnest 2.6.0 → 3.0.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +191 -0
  2. package/README.md +131 -14
  3. package/README_CN.md +121 -17
  4. package/config.json.example +8 -1
  5. package/docs/api-reference.md +1 -1
  6. package/integrations/README.md +1 -1
  7. package/integrations/agy/README.md +73 -0
  8. package/integrations/agy/setup.sh +75 -0
  9. package/integrations/setup-all.sh +1 -1
  10. package/package.json +7 -5
  11. package/src/__tests__/access-tracker-novelty.test.ts +22 -2
  12. package/src/__tests__/activity-counter.test.ts +38 -38
  13. package/src/__tests__/age-label.test.ts +33 -0
  14. package/src/__tests__/cluster-consolidation.test.ts +313 -22
  15. package/src/__tests__/consolidation-engine.test.ts +90 -0
  16. package/src/__tests__/contracts.test.ts +2 -2
  17. package/src/__tests__/data-checkup.test.ts +31 -3
  18. package/src/__tests__/dream-pipeline.test.ts +41 -1
  19. package/src/__tests__/dream-scope-isolation.test.ts +386 -0
  20. package/src/__tests__/dream-write-batching.test.ts +211 -0
  21. package/src/__tests__/env-config-invariant.test.ts +80 -0
  22. package/src/__tests__/env-config.test.ts +60 -0
  23. package/src/__tests__/ingest-scope-prefix.test.ts +69 -0
  24. package/src/__tests__/maintenance-locks.test.ts +8 -0
  25. package/src/__tests__/memory-evolution-b2-b3-e1.test.ts +17 -0
  26. package/src/__tests__/memory-output.test.ts +9 -0
  27. package/src/__tests__/memory-utility.test.ts +256 -0
  28. package/src/__tests__/openai-contract.test.ts +513 -0
  29. package/src/__tests__/promotion-distinct-sources.test.ts +162 -0
  30. package/src/__tests__/retrieve-audit-revision.test.ts +255 -0
  31. package/src/__tests__/source-heartbeat.test.ts +6 -2
  32. package/src/__tests__/store-patch-metadata.test.ts +30 -0
  33. package/src/__tests__/synthesis-contract.test.ts +252 -0
  34. package/src/__tests__/synthesis-promotion.test.ts +369 -0
  35. package/src/__tests__/trusted-memory.test.ts +28 -0
  36. package/src/__tests__/utility-weight.test.ts +57 -0
  37. package/src/__tests__/verbatim-self-recall.test.ts +279 -0
  38. package/src/__tests__/version-manager.test.ts +35 -0
  39. package/src/access-tracker.ts +10 -1
  40. package/src/activity-counter.ts +69 -11
  41. package/src/age-label.ts +29 -0
  42. package/src/audit-log.ts +35 -0
  43. package/src/auto-gc.ts +97 -30
  44. package/src/cli.ts +224 -10
  45. package/src/consolidation-engine.ts +317 -123
  46. package/src/data-checkup.ts +17 -3
  47. package/src/dream-pipeline.ts +90 -12
  48. package/src/embedder.ts +20 -5
  49. package/src/env-config.ts +53 -0
  50. package/src/forget-engine.ts +8 -2
  51. package/src/gateway-server.ts +272 -0
  52. package/src/ingest.ts +23 -5
  53. package/src/llm-client.ts +86 -25
  54. package/src/mcp-server.ts +2 -1
  55. package/src/mcp-tools-advanced.ts +7 -2
  56. package/src/mcp-tools-core.ts +11 -1
  57. package/src/mcp-tools-governance.ts +45 -3
  58. package/src/memory-evolution.ts +16 -0
  59. package/src/memory-output.ts +12 -8
  60. package/src/memory-promotion.ts +280 -0
  61. package/src/memory-store-port.ts +32 -3
  62. package/src/memory-utility.ts +314 -0
  63. package/src/recall-ledger.ts +76 -0
  64. package/src/retriever.ts +82 -9
  65. package/src/scope-policy.ts +22 -1
  66. package/src/session-output.ts +4 -3
  67. package/src/skill-promotion.ts +152 -1
  68. package/src/store.ts +231 -71
  69. package/src/synthesis-contract.ts +286 -0
  70. package/src/version-manager.ts +83 -0
  71. package/src/workflow-observation-schema.ts +26 -0
  72. package/integrations/gemini-cli/README.md +0 -57
  73. package/integrations/gemini-cli/setup.sh +0 -64
  74. /package/integrations/{gemini-cli/gemini-md-snippet.md → agy/agy-md-snippet.md} +0 -0
@@ -0,0 +1,513 @@
1
+ /**
2
+ * OpenAI-compatible HTTP contract tests.
3
+ *
4
+ * Why these exist (v3.0.0): the `openai` SDK moved from v4 to v7, which replaced the
5
+ * bundled `node-fetch` transport with the platform's built-in `fetch`. Every existing
6
+ * embedder/LLM test stubs `client.embeddings.create` or `client.chat.completions.create`
7
+ * with a plain async function, so **none of them touch a socket** — a transport-level
8
+ * regression (wrong URL, dropped field, mis-typed error) would pass all of them.
9
+ *
10
+ * These tests drive the real `Embedder` / `LLMClient` classes through the real SDK over
11
+ * a real loopback HTTP server, and assert both directions of the contract:
12
+ *
13
+ * request — what recallnest actually puts on the wire for each provider profile
14
+ * response — how it parses success, and how it fails on error and timeout
15
+ *
16
+ * No network egress, no vendor credentials, no paid calls: every endpoint is a
17
+ * `node:http` server bound to 127.0.0.1 on an ephemeral port, and every API key is a
18
+ * literal test string.
19
+ */
20
+
21
+ import { afterEach, describe, expect, it } from "bun:test";
22
+ import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
23
+ import { AddressInfo } from "node:net";
24
+ import OpenAI from "openai";
25
+
26
+ import { Embedder } from "../embedder.js";
27
+ import { LLMClient } from "../llm-client.js";
28
+
29
+ // ---------------------------------------------------------------------------
30
+ // Mock endpoint
31
+ // ---------------------------------------------------------------------------
32
+
33
+ interface CapturedRequest {
34
+ method: string;
35
+ url: string;
36
+ body: Record<string, unknown> | null;
37
+ hasAuthorization: boolean;
38
+ }
39
+
40
+ interface MockEndpoint {
41
+ /** Base URL including the `/v1` suffix, matching how providers are configured. */
42
+ baseURL: string;
43
+ requests: CapturedRequest[];
44
+ close(): Promise<void>;
45
+ }
46
+
47
+ type Responder = (req: CapturedRequest, res: ServerResponse) => void;
48
+
49
+ /** Never answers — used to exercise timeout paths without a real slow vendor. */
50
+ const HANG: Responder = () => {};
51
+
52
+ async function startMockEndpoint(respond: Responder): Promise<MockEndpoint> {
53
+ const requests: CapturedRequest[] = [];
54
+
55
+ const server: Server = createServer((req: IncomingMessage, res: ServerResponse) => {
56
+ let raw = "";
57
+ req.on("data", (chunk) => {
58
+ raw += chunk;
59
+ });
60
+ req.on("end", () => {
61
+ let body: Record<string, unknown> | null = null;
62
+ if (raw) {
63
+ try {
64
+ body = JSON.parse(raw) as Record<string, unknown>;
65
+ } catch {
66
+ body = null;
67
+ }
68
+ }
69
+ const captured: CapturedRequest = {
70
+ method: req.method ?? "",
71
+ url: req.url ?? "",
72
+ body,
73
+ hasAuthorization: Boolean(req.headers.authorization),
74
+ };
75
+ requests.push(captured);
76
+ respond(captured, res);
77
+ });
78
+ });
79
+
80
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
81
+ const { port } = server.address() as AddressInfo;
82
+
83
+ return {
84
+ baseURL: `http://127.0.0.1:${port}/v1`,
85
+ requests,
86
+ async close() {
87
+ // Hanging requests keep sockets open; closeAllConnections lets close() settle.
88
+ server.closeAllConnections?.();
89
+ await new Promise<void>((resolve) => server.close(() => resolve()));
90
+ },
91
+ };
92
+ }
93
+
94
+ /** Deterministic vector of the exact width the embedder was configured for. */
95
+ function vec(dimensions: number, seed = 1): number[] {
96
+ return Array.from({ length: dimensions }, (_, i) => Number(((seed + i) % 97) / 100));
97
+ }
98
+
99
+ function embeddingResponse(vectors: number[][]): string {
100
+ return JSON.stringify({
101
+ object: "list",
102
+ data: vectors.map((embedding, index) => ({ object: "embedding", index, embedding })),
103
+ model: "mock-embedding-model",
104
+ usage: { prompt_tokens: 4, total_tokens: 4 },
105
+ });
106
+ }
107
+
108
+ function chatResponse(content: string): string {
109
+ return JSON.stringify({
110
+ id: "chatcmpl-mock",
111
+ object: "chat.completion",
112
+ created: 1_700_000_000,
113
+ model: "mock-chat-model",
114
+ choices: [{ index: 0, message: { role: "assistant", content }, finish_reason: "stop" }],
115
+ usage: {
116
+ prompt_tokens: 11,
117
+ completion_tokens: 7,
118
+ total_tokens: 18,
119
+ prompt_tokens_details: { cached_tokens: 3 },
120
+ completion_tokens_details: { reasoning_tokens: 2 },
121
+ },
122
+ });
123
+ }
124
+
125
+ const okJson = (payload: string): Responder => (_req, res) => {
126
+ res.writeHead(200, { "content-type": "application/json" });
127
+ res.end(payload);
128
+ };
129
+
130
+ const errorJson = (status: number, message: string): Responder => (_req, res) => {
131
+ res.writeHead(status, { "content-type": "application/json" });
132
+ res.end(JSON.stringify({ error: { message, type: "invalid_request_error" } }));
133
+ };
134
+
135
+ /** Endpoints opened by the running test, closed in afterEach even when it fails. */
136
+ let openEndpoints: MockEndpoint[] = [];
137
+
138
+ async function endpoint(respond: Responder): Promise<MockEndpoint> {
139
+ const created = await startMockEndpoint(respond);
140
+ openEndpoints.push(created);
141
+ return created;
142
+ }
143
+
144
+ afterEach(async () => {
145
+ const pending = openEndpoints;
146
+ openEndpoints = [];
147
+ await Promise.all(pending.map((e) => e.close()));
148
+ });
149
+
150
+ /**
151
+ * Swap in a client built exactly like the production one, plus a short timeout and no
152
+ * SDK-level retries. Embedder has no timeout knob of its own, so this is the only way to
153
+ * exercise the timeout branch in bounded time; `maxRetries: 0` isolates recallnest's own
154
+ * retry policy from the SDK's so the request count is unambiguous.
155
+ */
156
+ function useBoundedClient(embedder: Embedder, baseURL: string, timeoutMs: number): void {
157
+ (embedder as unknown as { client: OpenAI }).client = new OpenAI({
158
+ apiKey: "test-key-not-a-real-credential",
159
+ baseURL,
160
+ timeout: timeoutMs,
161
+ maxRetries: 0,
162
+ });
163
+ }
164
+
165
+ // ---------------------------------------------------------------------------
166
+ // Embeddings
167
+ // ---------------------------------------------------------------------------
168
+
169
+ describe("OpenAI-compatible embeddings contract", () => {
170
+ it("sends the default OpenAI request shape and parses the vector back", async () => {
171
+ const mock = await endpoint(okJson(embeddingResponse([[0.1, 0.2, 0.3]])));
172
+
173
+ const embedder = new Embedder({
174
+ provider: "openai-compatible",
175
+ apiKey: "test-key-not-a-real-credential",
176
+ model: "text-embedding-3-small",
177
+ baseURL: mock.baseURL,
178
+ dimensions: 3,
179
+ });
180
+
181
+ await expect(embedder.embedPassage("hello world")).resolves.toEqual([0.1, 0.2, 0.3]);
182
+
183
+ expect(mock.requests).toHaveLength(1);
184
+ const [request] = mock.requests;
185
+ expect(request.method).toBe("POST");
186
+ expect(request.url).toBe("/v1/embeddings");
187
+ expect(request.hasAuthorization).toBe(true);
188
+ expect(request.body).toMatchObject({
189
+ model: "text-embedding-3-small",
190
+ input: "hello world",
191
+ // Forced by buildPayload so the SDK does not take its base64 decode path.
192
+ encoding_format: "float",
193
+ });
194
+ // A plain OpenAI endpoint must not receive provider-specific extensions.
195
+ expect(request.body).not.toHaveProperty("task");
196
+ expect(request.body).not.toHaveProperty("normalized");
197
+ });
198
+
199
+ it("sends Jina's task/normalized/dimensions extensions and distinguishes query from passage", async () => {
200
+ const jinaVector = vec(1024, 4);
201
+ const mock = await endpoint(okJson(embeddingResponse([jinaVector])));
202
+
203
+ // Mirrors config.json.example's shipped Jina profile.
204
+ const embedder = new Embedder({
205
+ provider: "openai-compatible",
206
+ apiKey: "test-key-not-a-real-credential",
207
+ model: "jina-embeddings-v5-text-small",
208
+ baseURL: mock.baseURL,
209
+ dimensions: 1024,
210
+ taskQuery: "retrieval.query",
211
+ taskPassage: "retrieval.passage",
212
+ normalized: true,
213
+ });
214
+
215
+ await expect(embedder.embedQuery("who is on call")).resolves.toEqual(jinaVector);
216
+ await expect(embedder.embedPassage("the on-call rota lives in ops")).resolves.toEqual(jinaVector);
217
+
218
+ expect(mock.requests).toHaveLength(2);
219
+ const [queryRequest, passageRequest] = mock.requests;
220
+
221
+ expect(queryRequest.url).toBe("/v1/embeddings");
222
+ expect(queryRequest.body).toMatchObject({
223
+ model: "jina-embeddings-v5-text-small",
224
+ task: "retrieval.query",
225
+ normalized: true,
226
+ dimensions: 1024,
227
+ encoding_format: "float",
228
+ });
229
+
230
+ // Asymmetric embedding is the whole point of the task fields — pin the difference.
231
+ expect(passageRequest.body).toMatchObject({ task: "retrieval.passage", normalized: true });
232
+ expect(queryRequest.body?.task).not.toBe(passageRequest.body?.task);
233
+ });
234
+
235
+ it("omits dimensions for a Qwen-compatible endpoint that was not given an override", async () => {
236
+ const qwenVector = vec(1024, 7);
237
+ const mock = await endpoint(okJson(embeddingResponse([qwenVector])));
238
+
239
+ // Qwen3 embedding via a local OpenAI-compatible runner: known model, no override,
240
+ // no task fields. Providers that reject unknown fields must not receive any.
241
+ const embedder = new Embedder({
242
+ provider: "openai-compatible",
243
+ apiKey: "test-key-not-a-real-credential",
244
+ model: "ai/qwen3-embedding",
245
+ baseURL: mock.baseURL,
246
+ });
247
+
248
+ await expect(embedder.embedPassage("qwen passage")).resolves.toEqual(qwenVector);
249
+ expect(embedder.dimensions).toBe(1024);
250
+
251
+ const [request] = mock.requests;
252
+ expect(request.body).toMatchObject({ model: "ai/qwen3-embedding", input: "qwen passage" });
253
+ expect(request.body).not.toHaveProperty("dimensions");
254
+ expect(request.body).not.toHaveProperty("task");
255
+ expect(request.body).not.toHaveProperty("normalized");
256
+ });
257
+
258
+ it("batches several passages into one request and preserves input order", async () => {
259
+ const mock = await endpoint(okJson(embeddingResponse([[1, 0, 0], [0, 1, 0], [0, 0, 1]])));
260
+
261
+ const embedder = new Embedder({
262
+ provider: "openai-compatible",
263
+ apiKey: "test-key-not-a-real-credential",
264
+ model: "text-embedding-3-small",
265
+ baseURL: mock.baseURL,
266
+ dimensions: 3,
267
+ chunking: false,
268
+ });
269
+
270
+ await expect(embedder.embedBatchPassage(["alpha", "beta", "gamma"])).resolves.toEqual([
271
+ [1, 0, 0],
272
+ [0, 1, 0],
273
+ [0, 0, 1],
274
+ ]);
275
+
276
+ expect(mock.requests).toHaveLength(1);
277
+ expect(mock.requests[0]?.body?.input).toEqual(["alpha", "beta", "gamma"]);
278
+ });
279
+
280
+ it("surfaces a provider error instead of returning a silent empty vector", async () => {
281
+ const mock = await endpoint(errorJson(400, "input exceeds maximum context length"));
282
+
283
+ const embedder = new Embedder({
284
+ provider: "openai-compatible",
285
+ apiKey: "test-key-not-a-real-credential",
286
+ model: "text-embedding-3-small",
287
+ baseURL: mock.baseURL,
288
+ dimensions: 3,
289
+ });
290
+ // maxRetries: 0 — a 400 is not retryable, but pinning it makes the count meaningful.
291
+ useBoundedClient(embedder, mock.baseURL, 5_000);
292
+
293
+ await expect(embedder.embedPassage("too long")).rejects.toThrow(/context length/);
294
+
295
+ // A non-transient error must not be retried by recallnest's own retry loop.
296
+ expect(mock.requests).toHaveLength(1);
297
+ });
298
+
299
+ it("does not retry an authentication failure", async () => {
300
+ const mock = await endpoint(errorJson(401, "invalid api key"));
301
+
302
+ const embedder = new Embedder({
303
+ provider: "openai-compatible",
304
+ apiKey: "test-key-not-a-real-credential",
305
+ model: "text-embedding-3-small",
306
+ baseURL: mock.baseURL,
307
+ dimensions: 3,
308
+ });
309
+ useBoundedClient(embedder, mock.baseURL, 5_000);
310
+
311
+ await expect(embedder.embedPassage("hello")).rejects.toThrow(/invalid api key/);
312
+ expect(mock.requests).toHaveLength(1);
313
+ });
314
+
315
+ it("does not turn a rate-limit reply into a chunking storm", async () => {
316
+ const mock = await endpoint(errorJson(429, "Rate limit exceeded, please retry later"));
317
+
318
+ // Regression guard. The context-error branch is gated on
319
+ // /context|too long|exceed|length/i, which also matches rate-limit wording, and the
320
+ // chunker returns short text unchanged — so re-embedding "the chunk" reproduced the
321
+ // same 429 and chunked again. Measured before the fix: >61,000 requests in 5s against
322
+ // an endpoint that was asking us to slow down. Bounded failure is the contract.
323
+ const embedder = new Embedder({
324
+ provider: "openai-compatible",
325
+ apiKey: "test-key-not-a-real-credential",
326
+ model: "text-embedding-3-small",
327
+ baseURL: mock.baseURL,
328
+ dimensions: 3,
329
+ });
330
+ useBoundedClient(embedder, mock.baseURL, 5_000);
331
+
332
+ await expect(embedder.embedPassage("short text")).rejects.toThrow();
333
+ expect(mock.requests.length).toBeLessThanOrEqual(3);
334
+ }, 15_000);
335
+
336
+ it("classifies a request timeout as transient and retries before giving up", async () => {
337
+ const mock = await endpoint(HANG);
338
+
339
+ const embedder = new Embedder({
340
+ provider: "openai-compatible",
341
+ apiKey: "test-key-not-a-real-credential",
342
+ model: "text-embedding-3-small",
343
+ baseURL: mock.baseURL,
344
+ dimensions: 3,
345
+ });
346
+ useBoundedClient(embedder, mock.baseURL, 120);
347
+
348
+ await expect(embedder.embedPassage("never answered")).rejects.toThrow();
349
+
350
+ // EMBEDDING_RETRY_DELAYS_MS has two entries, so a transient failure is attempted
351
+ // three times total. Seeing 3 requests is what proves the SDK's timeout error was
352
+ // recognised as transient — a misclassification would show up here as 1.
353
+ expect(mock.requests).toHaveLength(3);
354
+ }, 15_000);
355
+
356
+ it("raises the SDK's own timeout error type that the transient classifier keys on", async () => {
357
+ const mock = await endpoint(HANG);
358
+
359
+ // Guards the classifier's premise directly: embedder.ts tests for
360
+ // OpenAI.APIConnectionTimeoutError by identity, so an SDK upgrade that renamed or
361
+ // stopped exporting that class would silently turn timeouts into fatal errors.
362
+ const client = new OpenAI({
363
+ apiKey: "test-key-not-a-real-credential",
364
+ baseURL: mock.baseURL,
365
+ timeout: 120,
366
+ maxRetries: 0,
367
+ });
368
+
369
+ let caught: unknown;
370
+ try {
371
+ await client.embeddings.create({ model: "text-embedding-3-small", input: "x" });
372
+ } catch (error) {
373
+ caught = error;
374
+ }
375
+
376
+ expect(caught).toBeInstanceOf(OpenAI.APIConnectionTimeoutError);
377
+ expect(caught).toBeInstanceOf(OpenAI.APIConnectionError);
378
+ }, 15_000);
379
+ });
380
+
381
+ // ---------------------------------------------------------------------------
382
+ // Chat completions
383
+ // ---------------------------------------------------------------------------
384
+
385
+ describe("OpenAI-compatible chat completions contract", () => {
386
+ function qwenClient(baseURL: string, timeoutMs = 5_000): LLMClient {
387
+ // Mirrors DEFAULT_LLM_CONFIG (DashScope's OpenAI-compatible mode) with the base URL
388
+ // pointed at the loopback mock.
389
+ return new LLMClient({
390
+ apiKey: "test-key-not-a-real-credential",
391
+ model: "qwen-turbo",
392
+ baseURL,
393
+ timeoutMs,
394
+ temperature: 0.1,
395
+ });
396
+ }
397
+
398
+ it("sends the Qwen-compatible request shape and parses content plus usage details", async () => {
399
+ const mock = await endpoint(okJson(chatResponse(" a reusable conclusion ")));
400
+
401
+ const result = await qwenClient(mock.baseURL).chatLongDetailed("system rules", "user text", {
402
+ tokenLimit: { parameter: "max_tokens", value: 256 },
403
+ });
404
+
405
+ expect(result).not.toBeNull();
406
+ expect(result?.content).toBe("a reusable conclusion");
407
+ expect(result?.finishReason).toBe("stop");
408
+ expect(result?.responseModel).toBe("mock-chat-model");
409
+ expect(result?.usage).toEqual({
410
+ promptTokens: 11,
411
+ completionTokens: 7,
412
+ totalTokens: 18,
413
+ cachedPromptTokens: 3,
414
+ reasoningTokens: 2,
415
+ });
416
+
417
+ expect(mock.requests).toHaveLength(1);
418
+ const [request] = mock.requests;
419
+ expect(request.url).toBe("/v1/chat/completions");
420
+ expect(request.hasAuthorization).toBe(true);
421
+ expect(request.body).toMatchObject({
422
+ model: "qwen-turbo",
423
+ temperature: 0.1,
424
+ max_tokens: 256,
425
+ messages: [
426
+ { role: "system", content: "system rules" },
427
+ { role: "user", content: "user text" },
428
+ ],
429
+ });
430
+ });
431
+
432
+ it("passes JSON mode and max_completion_tokens through unchanged", async () => {
433
+ const mock = await endpoint(okJson(chatResponse('{"has":true}')));
434
+
435
+ await qwenClient(mock.baseURL).chatLongDetailed("system", "user", {
436
+ tokenLimit: { parameter: "max_completion_tokens", value: 400 },
437
+ responseFormat: { type: "json_object" },
438
+ enableThinking: false,
439
+ });
440
+
441
+ const [request] = mock.requests;
442
+ expect(request.body).toMatchObject({
443
+ response_format: { type: "json_object" },
444
+ max_completion_tokens: 400,
445
+ enable_thinking: false,
446
+ });
447
+ // The two token-limit parameters are mutually exclusive on real endpoints.
448
+ expect(request.body).not.toHaveProperty("max_tokens");
449
+ });
450
+
451
+ it("propagates a provider error and records it on the circuit breaker", async () => {
452
+ const mock = await endpoint(errorJson(500, "upstream model overloaded"));
453
+
454
+ // maxRetries: 0 keeps the SDK from retrying the 5xx, so the assertion below counts
455
+ // recallnest's behaviour rather than the SDK's retry policy.
456
+ const client = new LLMClient({
457
+ apiKey: "test-key-not-a-real-credential",
458
+ model: "qwen-turbo",
459
+ baseURL: mock.baseURL,
460
+ timeoutMs: 5_000,
461
+ temperature: 0.1,
462
+ maxRetries: 0,
463
+ });
464
+
465
+ await expect(client.chatLongDetailed("system", "user")).rejects.toThrow(/overloaded/);
466
+ expect(mock.requests).toHaveLength(1);
467
+ expect(client.breaker.canAttempt()).toBe(true); // one failure is below the trip threshold
468
+ });
469
+
470
+ it("aborts a hanging endpoint within its own timeout budget", async () => {
471
+ const mock = await endpoint(HANG);
472
+
473
+ // chatLongDetailed arms its AbortController at timeoutMs * 2.
474
+ const client = new LLMClient({
475
+ apiKey: "test-key-not-a-real-credential",
476
+ model: "qwen-turbo",
477
+ baseURL: mock.baseURL,
478
+ timeoutMs: 100,
479
+ temperature: 0.1,
480
+ maxRetries: 0,
481
+ });
482
+
483
+ const startedAt = Date.now();
484
+ await expect(client.chatLongDetailed("system", "user")).rejects.toThrow();
485
+ const elapsed = Date.now() - startedAt;
486
+
487
+ // Bounded failure is the contract: the request must not inherit the SDK's
488
+ // ten-minute default timeout when recallnest asked for 100ms.
489
+ expect(elapsed).toBeLessThan(10_000);
490
+ expect(mock.requests).toHaveLength(1);
491
+ }, 20_000);
492
+
493
+ it("returns null instead of calling the endpoint once the breaker is open", async () => {
494
+ const mock = await endpoint(okJson(chatResponse("should not be reached")));
495
+
496
+ const client = new LLMClient(
497
+ {
498
+ apiKey: "test-key-not-a-real-credential",
499
+ model: "qwen-turbo",
500
+ baseURL: mock.baseURL,
501
+ timeoutMs: 5_000,
502
+ temperature: 0.1,
503
+ },
504
+ { failureThreshold: 1, resetTimeoutMs: 60_000 },
505
+ );
506
+
507
+ client.breaker.recordFailure();
508
+ expect(client.breaker.canAttempt()).toBe(false);
509
+
510
+ await expect(client.chatLongDetailed("system", "user")).resolves.toBeNull();
511
+ expect(mock.requests).toHaveLength(0);
512
+ });
513
+ });
@@ -0,0 +1,162 @@
1
+ import { describe, expect, it } from "bun:test";
2
+
3
+ import {
4
+ countDistinctSources,
5
+ extractSourceKey,
6
+ judgeDistinctSources,
7
+ scanForPromotions,
8
+ UNKNOWN_SOURCE_PREFIX,
9
+ } from "../skill-promotion.js";
10
+ import type { MemoryEntry, MemorySearchResult } from "../store.js";
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Mock(与 skill-promotion.test.ts 同款生产语义:listPage 恒无向量,靠 getVectors 回填)
14
+ // ---------------------------------------------------------------------------
15
+
16
+ let seq = 0;
17
+ function makeCase(srcTag: string | null, sim = 1.0, overrides: Partial<MemoryEntry> = {}): MemoryEntry {
18
+ seq += 1;
19
+ const complement = Math.sqrt(1 - sim * sim);
20
+ return {
21
+ id: `c-${String(seq).padStart(4, "0")}`,
22
+ text: `Docker 部署时缺系统库导致构建失败 #${seq}`,
23
+ vector: [sim, complement, 0],
24
+ category: "cases",
25
+ scope: "memory:pivot",
26
+ importance: 0.7,
27
+ timestamp: Date.UTC(2026, 7, 20) + seq * 1000,
28
+ metadata: JSON.stringify({
29
+ ...(srcTag ? { tags: [srcTag, "pinned"] } : {}),
30
+ evolution: {
31
+ status: "active", version: 1, accessCount: 0, lastAccessedAt: null,
32
+ supersededBy: null, consolidatedInto: null, sourceMemories: [],
33
+ validFrom: Date.now(), validUntil: null,
34
+ },
35
+ }),
36
+ ...overrides,
37
+ };
38
+ }
39
+
40
+ function createMockStore(entries: MemoryEntry[]) {
41
+ return {
42
+ async listPage(opts: { category?: string; limit?: number; offset?: number } = {}): Promise<MemoryEntry[]> {
43
+ const { category, limit = 1000, offset = 0 } = opts;
44
+ const filtered = category ? entries.filter(e => e.category === category) : entries;
45
+ return filtered.slice(offset, offset + limit).map(e => ({ ...e, vector: [] }));
46
+ },
47
+ async getVectors(ids: string[]): Promise<Map<string, number[]>> {
48
+ const m = new Map<string, number[]>();
49
+ for (const id of ids) {
50
+ const e = entries.find(x => x.id === id);
51
+ if (e?.vector?.length) m.set(id, e.vector);
52
+ }
53
+ return m;
54
+ },
55
+ async vectorSearch(): Promise<MemorySearchResult[]> { return []; },
56
+ };
57
+ }
58
+
59
+ // ---------------------------------------------------------------------------
60
+ // 来源识别
61
+ // ---------------------------------------------------------------------------
62
+
63
+ describe("source identity", () => {
64
+ it("prefers evidenceMemories over the src: tag", () => {
65
+ const key = extractSourceKey({
66
+ metadata: JSON.stringify({ evidenceMemories: ["m2", "m1"], tags: ["src:aaaa1111"] }),
67
+ scope: "memory:pivot",
68
+ timestamp: Date.now(),
69
+ });
70
+ // 同一批源不管顺序都是同一个来源
71
+ expect(key).toBe("ev:m1|m2");
72
+ });
73
+
74
+ it("falls back to the src: tag that §5.3 requires on pivot writes", () => {
75
+ const key = extractSourceKey({
76
+ metadata: JSON.stringify({ tags: ["pinned", "src:cac4f155", "2026-08-23"] }),
77
+ scope: "memory:pivot",
78
+ timestamp: Date.now(),
79
+ });
80
+ expect(key).toBe("src:cac4f155");
81
+ });
82
+
83
+ it("marks the day-bucket fallback as unknown so callers can abstain", () => {
84
+ const key = extractSourceKey({ metadata: "{}", scope: "project:x", timestamp: Date.UTC(2026, 7, 23) });
85
+ expect(key.startsWith(UNKNOWN_SOURCE_PREFIX)).toBe(true);
86
+ expect(key).toBe("day:project:x:2026-08-23");
87
+ });
88
+
89
+ it("counts three records from one session as one source", () => {
90
+ const members = [1, 2, 3].map(() => makeCase("src:aaaa1111"));
91
+ expect(countDistinctSources(members)).toBe(1);
92
+ });
93
+ });
94
+
95
+ describe("judgeDistinctSources", () => {
96
+ it("rejects a cluster that is one experience repeated", () => {
97
+ const verdict = judgeDistinctSources([1, 2, 3].map(() => makeCase("src:aaaa1111")), 2);
98
+ expect(verdict.status).toBe("rejected");
99
+ });
100
+
101
+ it("accepts a cluster spanning two different sessions", () => {
102
+ const verdict = judgeDistinctSources(
103
+ [makeCase("src:aaaa1111"), makeCase("src:aaaa1111"), makeCase("src:bbbb2222")],
104
+ 2,
105
+ );
106
+ expect(verdict).toEqual({ status: "ok", distinctSources: 2 });
107
+ });
108
+
109
+ it("abstains — never rejects — when no record carries a real source pointer", () => {
110
+ // 全库 cases 只有 1.2% 带 src:,一律硬判会把「同一天解决的三个不同问题」误伤掉。
111
+ expect(judgeDistinctSources([1, 2, 3].map(() => makeCase(null)), 2)).toEqual({ status: "abstained" });
112
+ });
113
+
114
+ it("is disabled at minDistinctSources=1 (the pre-P2 behaviour)", () => {
115
+ const verdict = judgeDistinctSources([1, 2, 3].map(() => makeCase("src:aaaa1111")), 1);
116
+ expect(verdict.status).toBe("ok");
117
+ });
118
+ });
119
+
120
+ // ---------------------------------------------------------------------------
121
+ // 改前 / 改后对照 —— 「同一个坑踩三次」的具体例子
122
+ // ---------------------------------------------------------------------------
123
+
124
+ describe("promotion scan: same pothole three times vs two real tasks", () => {
125
+ it("promotes it under the old rule and rejects it under the new one", async () => {
126
+ const samePothole = [1, 2, 3].map(() => makeCase("src:aaaa1111", 1.0));
127
+ const store = createMockStore(samePothole);
128
+
129
+ const before = await scanForPromotions(store as never, "memory:pivot", { minDistinctSources: 1 });
130
+ expect(before.candidates.filter(c => c.type === "case_to_pattern").length).toBe(1);
131
+ expect(before.rejectedSingleSource).toBe(0);
132
+
133
+ const after = await scanForPromotions(store as never, "memory:pivot", { minDistinctSources: 2 });
134
+ expect(after.candidates.filter(c => c.type === "case_to_pattern").length).toBe(0);
135
+ expect(after.rejectedSingleSource).toBe(1);
136
+ });
137
+
138
+ it("keeps promoting a cluster that really spans different tasks", async () => {
139
+ // 审计原话:两个完全不同的任务(Alpine 装 lxml vs Docker 部署 Django)撞上同类子问题
140
+ // (容器缺系统库),才诱导出一条可迁移的规律。
141
+ const crossTask = [
142
+ makeCase("src:aaaa1111", 1.0),
143
+ makeCase("src:bbbb2222", 1.0),
144
+ makeCase("src:cccc3333", 1.0),
145
+ ];
146
+ const store = createMockStore(crossTask);
147
+
148
+ const result = await scanForPromotions(store as never, "memory:pivot", { minDistinctSources: 2 });
149
+ const candidate = result.candidates.find(c => c.type === "case_to_pattern");
150
+ expect(candidate).toBeDefined();
151
+ expect(candidate!.distinctSources).toBe(3);
152
+ expect(result.rejectedSingleSource).toBe(0);
153
+ });
154
+
155
+ it("abstains (and says so) when the scope has no source pointers at all", async () => {
156
+ const store = createMockStore([1, 2, 3].map(() => makeCase(null, 1.0)));
157
+ const result = await scanForPromotions(store as never, "project:legacy", { minDistinctSources: 2 });
158
+ expect(result.candidates.filter(c => c.type === "case_to_pattern").length).toBe(1);
159
+ expect(result.abstainedUnknownSource).toBe(1);
160
+ expect(result.rejectedSingleSource).toBe(0);
161
+ });
162
+ });