-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: recover stale sessions before eviction #387
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
Merged
rohitg00
merged 2 commits into
rohitg00:main
from
LaplaceYoung:fix/308-recover-stale-sessions
May 17, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import type { | ||
| CompressedObservation, | ||
| RawObservation, | ||
| Session, | ||
| } from "../src/types.js"; | ||
| import { registerEvictFunction } from "../src/functions/evict.js"; | ||
| import { KV } from "../src/state/schema.js"; | ||
|
|
||
| vi.mock("../src/logger.js", () => ({ | ||
| logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| type Store = Map<string, Map<string, unknown>>; | ||
| type Handler = (payload: unknown) => unknown | Promise<unknown>; | ||
|
|
||
| function daysAgo(days: number): string { | ||
| return new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString(); | ||
| } | ||
|
|
||
| function makeSession(id: string): Session { | ||
| return { | ||
| id, | ||
| project: "agentmemory", | ||
| cwd: "/repo/agentmemory", | ||
| startedAt: daysAgo(31), | ||
| status: "active", | ||
| observationCount: 1, | ||
| }; | ||
| } | ||
|
|
||
| function makeObservation(sessionId: string): CompressedObservation { | ||
| return { | ||
| id: "obs_1", | ||
| sessionId, | ||
| timestamp: daysAgo(31), | ||
| type: "decision", | ||
| title: "Chose sqlite storage", | ||
| facts: ["Use sqlite for local state"], | ||
| narrative: "The session chose sqlite for local state.", | ||
| concepts: ["sqlite"], | ||
| files: ["src/state/kv.ts"], | ||
| importance: 8, | ||
| }; | ||
| } | ||
|
|
||
| function makeRawObservation(sessionId: string): RawObservation { | ||
| return { | ||
| id: "raw_1", | ||
| sessionId, | ||
| timestamp: daysAgo(31), | ||
| hookType: "post_tool_use", | ||
| toolName: "Edit", | ||
| raw: { file_path: "src/state/kv.ts" }, | ||
| }; | ||
| } | ||
|
|
||
| function mockKV(store: Store, listFailures: Set<string> = new Set()) { | ||
| 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[]> => { | ||
| if (listFailures.has(scope)) { | ||
| throw new Error(`list failed for ${scope}`); | ||
| } | ||
| const entries = store.get(scope); | ||
| return entries ? (Array.from(entries.values()) as T[]) : []; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function mockSdk() { | ||
| const handlers = new Map<string, Handler>(); | ||
| const calls: Array<{ function_id: string; payload: unknown }> = []; | ||
| return { | ||
| calls, | ||
| sdk: { | ||
| registerFunction: (functionId: string, handler: Handler) => { | ||
| handlers.set(functionId, handler); | ||
| }, | ||
| trigger: async (input: { function_id: string; payload: unknown }) => { | ||
| calls.push(input); | ||
| const handler = handlers.get(input.function_id); | ||
| if (!handler) throw new Error(`missing handler: ${input.function_id}`); | ||
| return handler(input.payload); | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function storeForObservations( | ||
| sessionId: string, | ||
| observations: Array<CompressedObservation | RawObservation>, | ||
| ): Store { | ||
| const session = makeSession(sessionId); | ||
| return new Map([ | ||
| [KV.sessions, new Map([[session.id, session]])], | ||
| [KV.summaries, new Map()], | ||
| [ | ||
| KV.observations(session.id), | ||
| new Map(observations.map((observation) => [observation.id, observation])), | ||
| ], | ||
| [KV.config, new Map()], | ||
| [KV.audit, new Map()], | ||
| ]); | ||
| } | ||
|
|
||
| function storeForObservedSession(sessionId: string): Store { | ||
| return storeForObservations(sessionId, [makeObservation(sessionId)]); | ||
| } | ||
|
|
||
| describe("mem::evict stale sessions", () => { | ||
| it("runs session recovery before deleting a stale observed session", async () => { | ||
| const sessionId = "ses_stale"; | ||
| const store = storeForObservedSession(sessionId); | ||
| const kv = mockKV(store); | ||
| const { sdk, calls } = mockSdk(); | ||
|
|
||
| registerEvictFunction(sdk as never, kv as never); | ||
| sdk.registerFunction("event::session::stopped", async (payload) => { | ||
| expect(payload).toEqual({ sessionId }); | ||
| expect(await kv.get(KV.sessions, sessionId)).toMatchObject({ | ||
| id: sessionId, | ||
| }); | ||
| return { success: true }; | ||
| }); | ||
| sdk.registerFunction("mem::consolidate-pipeline", () => ({ | ||
| success: true, | ||
| })); | ||
|
|
||
| const result = (await sdk.trigger({ | ||
| function_id: "mem::evict", | ||
| payload: {}, | ||
| })) as { staleSessions: number }; | ||
|
|
||
| expect(result.staleSessions).toBe(1); | ||
| expect(await kv.get(KV.sessions, sessionId)).toBeNull(); | ||
| const audits = await kv.list<{ | ||
| details: { reason: string }; | ||
| }>(KV.audit); | ||
| expect(audits[0].details.reason).toBe( | ||
| "stale_session_recovered_then_evicted", | ||
| ); | ||
| expect(calls.map((call) => call.function_id)).toContain( | ||
| "event::session::stopped", | ||
| ); | ||
| expect(calls.map((call) => call.function_id)).toContain( | ||
| "mem::consolidate-pipeline", | ||
| ); | ||
| }); | ||
|
|
||
| it("keeps a stale observed session when recovery fails", async () => { | ||
| const sessionId = "ses_unrecovered"; | ||
| const store = storeForObservedSession(sessionId); | ||
| const kv = mockKV(store); | ||
| const { sdk, calls } = mockSdk(); | ||
|
|
||
| registerEvictFunction(sdk as never, kv as never); | ||
| sdk.registerFunction("event::session::stopped", () => ({ | ||
| success: false, | ||
| error: "no_provider", | ||
| })); | ||
|
|
||
| const result = (await sdk.trigger({ | ||
| function_id: "mem::evict", | ||
| payload: {}, | ||
| })) as { staleSessions: number }; | ||
|
|
||
| expect(result.staleSessions).toBe(0); | ||
| expect(await kv.get(KV.sessions, sessionId)).toMatchObject({ | ||
| id: sessionId, | ||
| }); | ||
| expect(calls.map((call) => call.function_id)).toContain( | ||
| "event::session::stopped", | ||
| ); | ||
| expect(calls.map((call) => call.function_id)).not.toContain( | ||
| "mem::consolidate-pipeline", | ||
| ); | ||
| }); | ||
|
|
||
| it("keeps a stale session when observation scanning fails", async () => { | ||
| const sessionId = "ses_scan_failed"; | ||
| const store = storeForObservedSession(sessionId); | ||
| const kv = mockKV(store, new Set([KV.observations(sessionId)])); | ||
| const { sdk, calls } = mockSdk(); | ||
|
|
||
| registerEvictFunction(sdk as never, kv as never); | ||
| sdk.registerFunction("event::session::stopped", () => ({ | ||
| success: true, | ||
| })); | ||
|
|
||
| const result = (await sdk.trigger({ | ||
| function_id: "mem::evict", | ||
| payload: {}, | ||
| })) as { staleSessions: number }; | ||
|
|
||
| expect(result.staleSessions).toBe(0); | ||
| expect(await kv.get(KV.sessions, sessionId)).toMatchObject({ | ||
| id: sessionId, | ||
| }); | ||
| expect(calls.map((call) => call.function_id)).not.toContain( | ||
| "event::session::stopped", | ||
| ); | ||
| }); | ||
|
|
||
| it("keeps a stale session that only has raw observations", async () => { | ||
| const sessionId = "ses_raw_only"; | ||
| const store = storeForObservations(sessionId, [ | ||
| makeRawObservation(sessionId), | ||
| ]); | ||
| const kv = mockKV(store); | ||
| const { sdk, calls } = mockSdk(); | ||
|
|
||
| registerEvictFunction(sdk as never, kv as never); | ||
| sdk.registerFunction("event::session::stopped", () => ({ | ||
| success: true, | ||
| })); | ||
|
|
||
| const result = (await sdk.trigger({ | ||
| function_id: "mem::evict", | ||
| payload: {}, | ||
| })) as { staleSessions: number }; | ||
|
|
||
| expect(result.staleSessions).toBe(0); | ||
| expect(await kv.get(KV.sessions, sessionId)).toMatchObject({ | ||
| id: sessionId, | ||
| }); | ||
| expect(calls.map((call) => call.function_id)).not.toContain( | ||
| "event::session::stopped", | ||
| ); | ||
| }); | ||
| }); |
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.
🧩 Analysis chain
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 38755
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 527
Reverse the compressed-observation recovery gate—it deletes exactly the sessions that need recovery most.
The KV store can contain both
RawObservationandCompressedObservationobjects (confirmed by observe.ts storing both, and replay.ts typing the same location asRawObservation | CompressedObservation).RawObservationhas notitlefield, whileCompressedObservationrequires it.The current check
observations.some((o) => o.title)therefore:falsefor sessions containing only raw observations (wheresession::stoppednever fired—exactly the crash/disconnect cases that need recovery per issue#308)truefor sessions that already have compressed observations (which meansession::stoppedlikely already ran)This is backwards. Sessions with only raw observations are silently deleted without any recovery attempt. Invert the condition to recover sessions without compressed observations:
🤖 Prompt for AI Agents