Repository navigation
fix: ignore unknown session end events #1252
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
Merged
Changes from all commits
Commits
Show all changes
4 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
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,246 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("iii-sdk", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("iii-sdk")>(); | ||
| return { | ||
| ...actual, | ||
| TriggerAction: { | ||
| ...actual.TriggerAction, | ||
| Void: vi.fn(() => ({ type: "void" })), | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| import { registerApiTriggers } from "../src/triggers/api.js"; | ||
| import { registerEventTriggers } from "../src/triggers/events.js"; | ||
| import { KV } from "../src/state/schema.js"; | ||
| import type { Session } from "../src/types.js"; | ||
|
|
||
| type ApiResponse = { | ||
| status_code: number; | ||
| body: Record<string, unknown>; | ||
| }; | ||
|
|
||
| function makeHarness(sessionRows: Array<[string, unknown]> = []) { | ||
| const store = new Map<string, Map<string, unknown>>([ | ||
| [KV.sessions, new Map(sessionRows)], | ||
| [KV.summaries, new Map()], | ||
| ]); | ||
| const functions = new Map<string, Function>(); | ||
| const trigger = vi.fn(async () => undefined); | ||
| const update = vi.fn( | ||
| async ( | ||
| scope: string, | ||
| key: string, | ||
| ops: Array<{ type: "set" | "remove"; path: string; value?: unknown }>, | ||
| ) => { | ||
| const values = store.get(scope) ?? new Map<string, unknown>(); | ||
| store.set(scope, values); | ||
| const existing = values.get(key); | ||
| const next = | ||
| existing && typeof existing === "object" | ||
| ? { ...(existing as Record<string, unknown>) } | ||
| : {}; | ||
| for (const op of ops) { | ||
| if (op.type === "remove") delete next[op.path]; | ||
| else next[op.path] = op.value; | ||
| } | ||
| values.set(key, next); | ||
| return next; | ||
| }, | ||
| ); | ||
| const kv = { | ||
| get: vi.fn(async (scope: string, key: string) => { | ||
| if (typeof key !== "string" || key.length === 0) { | ||
| throw new Error("state::get requires a non-empty key"); | ||
| } | ||
| return store.get(scope)?.get(key) ?? null; | ||
| }), | ||
| set: vi.fn(), | ||
| update, | ||
| delete: vi.fn(), | ||
| list: vi.fn(async (scope: string) => | ||
| Array.from(store.get(scope)?.values() ?? []), | ||
| ), | ||
| }; | ||
| const sdk = { | ||
| registerFunction: (id: string, handler: Function) => functions.set(id, handler), | ||
| registerTrigger: vi.fn(), | ||
| trigger, | ||
| }; | ||
|
|
||
| registerApiTriggers(sdk as never, kv as never); | ||
| registerEventTriggers(sdk as never, kv as never); | ||
|
|
||
| return { functions, kv, store, trigger, update }; | ||
| } | ||
|
|
||
| function session(id: string, status: Session["status"] = "active"): Session { | ||
| return { | ||
| id, | ||
| project: "test-project", | ||
| cwd: "/tmp/test-project", | ||
| startedAt: "2026-08-26T00:00:00.000Z", | ||
| status, | ||
| observationCount: 1, | ||
| }; | ||
| } | ||
|
|
||
| describe("session lifecycle API", () => { | ||
| it("treats ending an unknown session as a no-op without creating a partial row", async () => { | ||
| const harness = makeHarness(); | ||
| const handler = harness.functions.get("api::session::end")!; | ||
|
|
||
| const response = (await handler({ | ||
| body: { sessionId: "agent:dmp-pm:direct:test" }, | ||
| })) as ApiResponse; | ||
|
|
||
| expect(response).toEqual({ | ||
| status_code: 200, | ||
| body: { success: true, ended: false, reason: "not_found" }, | ||
| }); | ||
| expect(harness.update).not.toHaveBeenCalled(); | ||
| expect(harness.store.get(KV.sessions)).toEqual(new Map()); | ||
| expect(harness.trigger).not.toHaveBeenCalledWith( | ||
| expect.objectContaining({ function_id: "event::session::stopped" }), | ||
| ); | ||
| }); | ||
|
|
||
| it("does not treat an existing partial row as a valid session", async () => { | ||
| const sessionId = "agent:dmp-pm:direct:partial"; | ||
| const harness = makeHarness([ | ||
| [sessionId, { status: "completed", endedAt: "2026-08-26T00:00:00.000Z" }], | ||
| ]); | ||
| const handler = harness.functions.get("api::session::end")!; | ||
|
|
||
| const response = (await handler({ body: { sessionId } })) as ApiResponse; | ||
|
|
||
| expect(response.body).toEqual({ | ||
| success: true, | ||
| ended: false, | ||
| reason: "not_found", | ||
| }); | ||
| expect(harness.update).not.toHaveBeenCalled(); | ||
| }); | ||
|
a652 marked this conversation as resolved.
Resolved
|
||
|
|
||
| it("treats ending an already-completed session as a no-op", async () => { | ||
| const sessionId = "agent:dmp-pm:direct:completed"; | ||
| const harness = makeHarness([[sessionId, session(sessionId, "completed")]]); | ||
| const handler = harness.functions.get("api::session::end")!; | ||
|
|
||
| const response = (await handler({ body: { sessionId } })) as ApiResponse; | ||
|
|
||
| expect(response).toEqual({ | ||
| status_code: 200, | ||
| body: { success: true, ended: false, reason: "already_completed" }, | ||
| }); | ||
| expect(harness.update).not.toHaveBeenCalled(); | ||
| expect(harness.trigger).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("ends an existing session and publishes the stopped lifecycle once", async () => { | ||
| const sessionId = "agent:dmp-pm:direct:existing"; | ||
| const harness = makeHarness([[sessionId, session(sessionId)]]); | ||
| const handler = harness.functions.get("api::session::end")!; | ||
|
|
||
| const response = (await handler({ body: { sessionId } })) as ApiResponse; | ||
|
|
||
| expect(response.body).toEqual({ success: true, ended: true }); | ||
| expect(harness.store.get(KV.sessions)?.get(sessionId)).toMatchObject({ | ||
| id: sessionId, | ||
| status: "completed", | ||
| endedAt: expect.any(String), | ||
| }); | ||
| expect(harness.trigger).toHaveBeenCalledTimes(1); | ||
| expect(harness.trigger).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| function_id: "event::session::stopped", | ||
| payload: { sessionId }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("skips malformed rows when listing sessions and never reads an undefined summary key", async () => { | ||
| const valid = session("ses_valid", "completed"); | ||
| const harness = makeHarness([ | ||
| [valid.id, valid], | ||
| ["agent:dmp-pm:direct:partial", { status: "completed", endedAt: "2026-08-26T00:00:00.000Z" }], | ||
| ]); | ||
| const handler = harness.functions.get("api::sessions")!; | ||
|
|
||
| const response = (await handler({ | ||
| headers: {}, | ||
| query_params: { agentId: "*" }, | ||
| })) as ApiResponse; | ||
|
|
||
| expect(response.status_code).toBe(200); | ||
| expect(response.body.sessions).toEqual([valid]); | ||
| expect(harness.kv.get).toHaveBeenCalledWith(KV.summaries, valid.id); | ||
| expect(harness.kv.get).not.toHaveBeenCalledWith(KV.summaries, undefined); | ||
| }); | ||
| }); | ||
|
|
||
| describe("event::session::ended", () => { | ||
| it("ignores an unknown session without creating a partial row", async () => { | ||
| const harness = makeHarness(); | ||
| const handler = harness.functions.get("event::session::ended")!; | ||
|
|
||
| const result = await handler({ sessionId: "agent:dmp-pm:direct:test" }); | ||
|
|
||
| expect(result).toEqual({ success: true, ended: false, reason: "not_found" }); | ||
| expect(harness.update).not.toHaveBeenCalled(); | ||
| expect(harness.store.get(KV.sessions)).toEqual(new Map()); | ||
| }); | ||
|
|
||
| it("ignores a malformed session id", async () => { | ||
| const harness = makeHarness(); | ||
| const handler = harness.functions.get("event::session::ended")!; | ||
|
|
||
| const result = await handler({}); | ||
|
|
||
| expect(result).toEqual({ success: true, ended: false, reason: "not_found" }); | ||
| expect(harness.kv.get).not.toHaveBeenCalled(); | ||
| expect(harness.update).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not treat an existing partial row as a valid session", async () => { | ||
| const sessionId = "agent:dmp-pm:direct:partial"; | ||
| const harness = makeHarness([[sessionId, { status: "active" }]]); | ||
| const handler = harness.functions.get("event::session::ended")!; | ||
|
|
||
| const result = await handler({ sessionId }); | ||
|
|
||
| expect(result).toEqual({ success: true, ended: false, reason: "not_found" }); | ||
| expect(harness.update).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("leaves an already-completed session untouched", async () => { | ||
| const sessionId = "agent:dmp-pm:direct:completed"; | ||
| const harness = makeHarness([[sessionId, session(sessionId, "completed")]]); | ||
| const handler = harness.functions.get("event::session::ended")!; | ||
|
|
||
| const result = await handler({ sessionId }); | ||
|
|
||
| expect(result).toEqual({ | ||
| success: true, | ||
| ended: false, | ||
| reason: "already_completed", | ||
| }); | ||
| expect(harness.update).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("completes an existing active session", async () => { | ||
| const sessionId = "agent:dmp-pm:direct:existing"; | ||
| const harness = makeHarness([[sessionId, session(sessionId)]]); | ||
| const handler = harness.functions.get("event::session::ended")!; | ||
|
|
||
| const result = await handler({ sessionId }); | ||
|
|
||
| expect(result).toEqual({ success: true, ended: true }); | ||
| expect(harness.store.get(KV.sessions)?.get(sessionId)).toMatchObject({ | ||
| id: sessionId, | ||
| status: "completed", | ||
| endedAt: expect.any(String), | ||
| }); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.