-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(session): gate terminal session-end write behind an explicit final flag #1288
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
DanielCarmingham
wants to merge
5
commits into
rohitg00:main
Choose a base branch
from
DanielCarmingham:pr/per-turn-session-end
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4c99ba5
fix(session): gate terminal session-end write behind an explicit fina…
DanielCarmingham 6a0f40d
fix(session): add final:true to the remaining genuine session-end cal…
DanielCarmingham 31b48a8
fix(hermes): send final=true on genuine session end (#745)
DanielCarmingham c975828
docs(readme): document the final flag on POST /agentmemory/session/end
DanielCarmingham a89cb78
docs(session): trim the final-flag comments to their rationale
DanielCarmingham 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,99 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
|
|
||
| vi.mock("../src/logger.js", () => ({ | ||
| logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| import { registerApiTriggers } from "../src/triggers/api.js"; | ||
| import { KV } from "../src/state/schema.js"; | ||
| import { mockKV, mockSdk } from "./helpers/mocks.js"; | ||
| import type { Session } from "../src/types.js"; | ||
|
|
||
| // #745: Claude Code fires Stop at the end of EVERY assistant turn, not only | ||
| // at genuine session end, and the Stop hook posts to the same | ||
| // /agentmemory/session/end endpoint as the real SessionEnd hook, with the | ||
| // same payload shape. Writing endedAt + status:"completed" unconditionally | ||
| // there marked every live session terminated on every turn, producing | ||
| // phantom "abandoned session" diagnostics. The terminal write is now gated | ||
| // on an explicit `final: true` flag that only the genuine SessionEnd hook | ||
| // sends; the per-turn Stop hook does not, and event::session::stopped keeps | ||
| // firing unconditionally on both so summarize/graph/consolidation still run | ||
| // every turn. | ||
| describe("api::session::end final flag (#745)", () => { | ||
| function seedSession(kv: ReturnType<typeof mockKV>, id = "s1") { | ||
| return kv.set(KV.sessions, id, { | ||
| id, | ||
| project: "p", | ||
| cwd: "/tmp", | ||
| startedAt: new Date().toISOString(), | ||
| status: "active", | ||
| observationCount: 3, | ||
| } satisfies Session); | ||
| } | ||
|
|
||
| it("a post without `final` does not write endedAt/status but still fans out event::session::stopped", async () => { | ||
| const kv = mockKV(); | ||
| await seedSession(kv); | ||
| const sdk = mockSdk(); | ||
| const stopped = vi.fn(async () => ({ success: true })); | ||
| sdk.registerFunction("event::session::stopped", stopped); | ||
| registerApiTriggers(sdk as never, kv as never); | ||
|
|
||
| const handler = sdk.fns.get("api::session::end")!; | ||
| const res = (await handler({ body: { sessionId: "s1" } } as never)) as { | ||
| status_code: number; | ||
| }; | ||
| expect(res.status_code).toBe(200); | ||
|
|
||
| const session = await kv.get<Session>(KV.sessions, "s1"); | ||
| expect(session?.status).toBe("active"); | ||
| expect(session?.endedAt).toBeUndefined(); | ||
|
|
||
| // Fan-out is fire-and-forget (not awaited by the handler); flush microtasks. | ||
| await new Promise((r) => setTimeout(r, 0)); | ||
| expect(stopped).toHaveBeenCalledWith({ sessionId: "s1" }); | ||
| }); | ||
|
|
||
| it("a post with final: true writes endedAt/status and still fans out event::session::stopped", async () => { | ||
| const kv = mockKV(); | ||
| await seedSession(kv); | ||
| const sdk = mockSdk(); | ||
| const stopped = vi.fn(async () => ({ success: true })); | ||
| sdk.registerFunction("event::session::stopped", stopped); | ||
| registerApiTriggers(sdk as never, kv as never); | ||
|
|
||
| const handler = sdk.fns.get("api::session::end")!; | ||
| const res = (await handler({ | ||
| body: { sessionId: "s1", final: true }, | ||
| } as never)) as { status_code: number }; | ||
| expect(res.status_code).toBe(200); | ||
|
|
||
| const session = await kv.get<Session>(KV.sessions, "s1"); | ||
| expect(session?.status).toBe("completed"); | ||
| expect(session?.endedAt).toBeDefined(); | ||
|
|
||
| await new Promise((r) => setTimeout(r, 0)); | ||
| expect(stopped).toHaveBeenCalledWith({ sessionId: "s1" }); | ||
| }); | ||
|
|
||
| it.each([["true" /* string */], [1], [{}], [[]], [null]])( | ||
| "a non-boolean final (%j) does not trigger the terminal write", | ||
| async (finalValue) => { | ||
| const kv = mockKV(); | ||
| await seedSession(kv); | ||
| const sdk = mockSdk(); | ||
| sdk.registerFunction("event::session::stopped", async () => ({ success: true })); | ||
| registerApiTriggers(sdk as never, kv as never); | ||
|
|
||
| const handler = sdk.fns.get("api::session::end")!; | ||
| const res = (await handler({ | ||
| body: { sessionId: "s1", final: finalValue }, | ||
| } as never)) as { status_code: number }; | ||
| expect(res.status_code).toBe(200); | ||
|
|
||
| const session = await kv.get<Session>(KV.sessions, "s1"); | ||
| expect(session?.status).toBe("active"); | ||
| expect(session?.endedAt).toBeUndefined(); | ||
| }, | ||
| ); | ||
| }); | ||
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
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.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 16655
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 50377
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 29841
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 27118
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 50377
Mock
iii-sdkthrough Vitest.src/triggers/api.tsimports and calls the runtimeTriggerAction.Void()fromiii-sdk. The localmockSdk()andmockKV()helpers do not mock this module. Addvi.mock("iii-sdk")with the required SDK and KV method mocks.🤖 Prompt for AI Agents
Source: Coding guidelines