-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: make consolidation timeouts configurable #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ggoldani
wants to merge
1
commit into
rohitg00:main
Choose a base branch
from
ggoldani:fix/configurable-timeouts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import type { MemoryProvider } from "../src/types.js"; | ||
| import { registerConsolidateFunction } from "../src/functions/consolidate.js"; | ||
| import { KV } from "../src/state/schema.js"; | ||
| import type { CompressedObservation, Session } from "../src/types.js"; | ||
|
|
||
| vi.mock("../src/logger.js", () => ({ | ||
| logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| vi.mock("../src/functions/audit.js", () => ({ | ||
| recordAudit: vi.fn(), | ||
| })); | ||
|
|
||
| function makeMockKV() { | ||
| const store = new Map<string, Map<string, unknown>>(); | ||
| return { | ||
| get: async <T>(scope: string, key: string): Promise<T | null> => | ||
| (store.get(scope)?.get(key) as T) ?? null, | ||
| set: async <T>(scope: string, key: string, data: T): Promise<T> => { | ||
| if (!store.has(scope)) store.set(scope, new Map()); | ||
| store.get(scope)!.set(key, data); | ||
| return data; | ||
| }, | ||
| delete: async (scope: string, key: string): Promise<void> => { | ||
| store.get(scope)?.delete(key); | ||
| }, | ||
| list: async <T>(scope: string): Promise<T[]> => { | ||
| const entries = store.get(scope); | ||
| return entries ? (Array.from(entries.values()) as T[]) : []; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function makeMockSdk() { | ||
| const functions = new Map<string, Function>(); | ||
| return { | ||
| registerFunction: (id: string, handler: Function) => { | ||
| functions.set(id, handler); | ||
| }, | ||
| registerTrigger: () => {}, | ||
| trigger: async (id: string, payload: unknown) => { | ||
| const fn = functions.get(id); | ||
| if (!fn) throw new Error(`No function registered: ${id}`); | ||
| return fn(payload); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function makeSession(id: string): Session { | ||
| return { | ||
| id, | ||
| project: "agentmemory", | ||
| cwd: "/tmp/agentmemory", | ||
| startedAt: new Date().toISOString(), | ||
| status: "completed", | ||
| observationCount: 5, | ||
| }; | ||
| } | ||
|
|
||
| function makeObs(id: string, sessionId: string, concept: string): CompressedObservation { | ||
| return { | ||
| id, | ||
| sessionId, | ||
| timestamp: new Date().toISOString(), | ||
| type: "decision", | ||
| title: `obs ${id}`, | ||
| facts: ["fact"], | ||
| narrative: "narrative", | ||
| concepts: [concept], | ||
| files: ["src/index.ts"], | ||
| importance: 8, | ||
| }; | ||
| } | ||
|
|
||
| async function seedObservations( | ||
| kv: ReturnType<typeof makeMockKV>, | ||
| sessionId: string, | ||
| concepts: string[], | ||
| ) { | ||
| for (const concept of concepts) { | ||
| for (let i = 0; i < 3; i++) { | ||
| await kv.set( | ||
| KV.observations(sessionId), | ||
| `${concept}_${i}`, | ||
| makeObs(`${concept}_${i}`, sessionId, concept), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| describe("timeout configurability regressions", () => { | ||
| afterEach(() => { | ||
| delete process.env["AGENTMEMORY_CONSOLIDATION_COMPRESS_TIMEOUT_MS"]; | ||
| delete process.env["AGENTMEMORY_LLM_TIMEOUT_MS"]; | ||
| delete process.env["OPENAI_TIMEOUT_MS"]; | ||
| delete process.env["AGENTMEMORY_INVOCATION_TIMEOUT_MS"]; | ||
| }); | ||
|
|
||
| it("parsePositiveInt accepts digits only, supports bounds, and rejects malformed values", async () => { | ||
| vi.resetModules(); | ||
| const { parsePositiveInt } = await import("../src/config.js"); | ||
| expect(parsePositiveInt("600000")).toBe(600000); | ||
| expect(parsePositiveInt("999", { min: 1_000 })).toBeUndefined(); | ||
| expect(parsePositiveInt("600001", { max: 600_000 })).toBeUndefined(); | ||
| expect(parsePositiveInt("30ms")).toBeUndefined(); | ||
| expect(parsePositiveInt("1_000")).toBeUndefined(); | ||
| expect(parsePositiveInt(undefined)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("resolveTimeoutMs honors precedence and falls back when values are invalid or out of range", async () => { | ||
| vi.resetModules(); | ||
| const { resolveTimeoutMs } = await import("../src/config.js"); | ||
|
|
||
| expect(resolveTimeoutMs(["1500", "2500"], 60_000, { min: 1_000, max: 10_000 })).toBe(1500); | ||
| expect(resolveTimeoutMs(["999", "2500"], 60_000, { min: 1_000, max: 10_000 })).toBe(2500); | ||
| expect(resolveTimeoutMs(["700000"], 60_000, { min: 1_000, max: 600_000 })).toBe(60_000); | ||
| expect(resolveTimeoutMs(["bad", undefined], 60_000, { min: 1_000, max: 600_000 })).toBe(60_000); | ||
| }); | ||
|
|
||
| it("mem::consolidate honors AGENTMEMORY_CONSOLIDATION_COMPRESS_TIMEOUT_MS", async () => { | ||
| vi.useFakeTimers(); | ||
| process.env["AGENTMEMORY_CONSOLIDATION_COMPRESS_TIMEOUT_MS"] = "1000"; | ||
|
|
||
| const sdk = makeMockSdk(); | ||
| const kv = makeMockKV(); | ||
| const provider: MemoryProvider = { | ||
| name: "mock", | ||
| compress: vi.fn().mockImplementation(() => new Promise<string>(() => {})), | ||
| embed: vi.fn().mockResolvedValue(new Float32Array(384)), | ||
| embedBatch: vi.fn().mockResolvedValue([]), | ||
| dimensions: 384, | ||
| compressionModel: "mock", | ||
| }; | ||
|
|
||
| const session = makeSession("sess_timeout"); | ||
| await kv.set(KV.sessions, session.id, session); | ||
| await seedObservations(kv, session.id, ["timeouts"]); | ||
|
|
||
| registerConsolidateFunction(sdk as never, kv as never, provider as never); | ||
| const pending = sdk.trigger("mem::consolidate", { | ||
| project: "agentmemory", | ||
| minObservations: 1, | ||
| }) as Promise<{ consolidated: number; totalObservations: number }>; | ||
|
|
||
| await vi.advanceTimersByTimeAsync(1_000); | ||
| const result = await pending; | ||
|
|
||
| expect(result.totalObservations).toBe(3); | ||
| expect(result.consolidated).toBe(0); | ||
| expect(provider.compress).toHaveBeenCalledOnce(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("mem::consolidate counts timed-out attempts toward the LLM budget", async () => { | ||
| vi.useFakeTimers(); | ||
| process.env["AGENTMEMORY_CONSOLIDATION_COMPRESS_TIMEOUT_MS"] = "1000"; | ||
|
|
||
| const sdk = makeMockSdk(); | ||
| const kv = makeMockKV(); | ||
| const provider: MemoryProvider = { | ||
| name: "mock", | ||
| compress: vi.fn().mockImplementation(() => new Promise<string>(() => {})), | ||
| embed: vi.fn().mockResolvedValue(new Float32Array(384)), | ||
| embedBatch: vi.fn().mockResolvedValue([]), | ||
| dimensions: 384, | ||
| compressionModel: "mock", | ||
| }; | ||
|
|
||
| const session = makeSession("sess_budget"); | ||
| await kv.set(KV.sessions, session.id, session); | ||
| await seedObservations( | ||
| kv, | ||
| session.id, | ||
| Array.from({ length: 12 }, (_, i) => `concept_${i}`), | ||
| ); | ||
|
|
||
| registerConsolidateFunction(sdk as never, kv as never, provider as never); | ||
| const pending = sdk.trigger("mem::consolidate", { | ||
| project: "agentmemory", | ||
| minObservations: 1, | ||
| }) as Promise<{ consolidated: number; totalObservations: number }>; | ||
|
|
||
| await vi.advanceTimersByTimeAsync(10_000); | ||
| const result = await pending; | ||
|
|
||
| expect(result.totalObservations).toBe(36); | ||
| expect(result.consolidated).toBe(0); | ||
| expect(provider.compress).toHaveBeenCalledTimes(10); | ||
| vi.useRealTimers(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore real timers in shared cleanup to prevent cross-test leakage.
If a test fails before Line 152 or Line 190, fake timers remain active and can cascade flaky failures into later tests. Move timer restoration into
afterEachso cleanup always runs.Suggested fix
afterEach(() => { + vi.useRealTimers(); delete process.env["AGENTMEMORY_CONSOLIDATION_COMPRESS_TIMEOUT_MS"]; delete process.env["AGENTMEMORY_LLM_TIMEOUT_MS"]; delete process.env["OPENAI_TIMEOUT_MS"]; delete process.env["AGENTMEMORY_INVOCATION_TIMEOUT_MS"]; });🤖 Prompt for AI Agents