diff --git a/packages/coding-agent/src/core/session-manager.ts b/packages/coding-agent/src/core/session-manager.ts index a7edb634..49b0f422 100644 --- a/packages/coding-agent/src/core/session-manager.ts +++ b/packages/coding-agent/src/core/session-manager.ts @@ -36,6 +36,7 @@ export interface SessionHeader { } export interface NewSessionOptions { + id?: string; parentSession?: string; } @@ -721,7 +722,7 @@ export class SessionManager { } newSession(options?: NewSessionOptions): string | undefined { - this.sessionId = randomUUID(); + this.sessionId = options?.id ?? randomUUID(); const timestamp = new Date().toISOString(); const header: SessionHeader = { type: "session", diff --git a/packages/coding-agent/test/session-manager/custom-session-id.test.ts b/packages/coding-agent/test/session-manager/custom-session-id.test.ts new file mode 100644 index 00000000..8cded2d2 --- /dev/null +++ b/packages/coding-agent/test/session-manager/custom-session-id.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest"; +import { SessionManager } from "../../src/core/session-manager.js"; + +describe("SessionManager.newSession with custom id", () => { + it("uses the provided id instead of generating one", () => { + const session = SessionManager.inMemory(); + session.newSession({ id: "my-custom-id" }); + expect(session.getSessionId()).toBe("my-custom-id"); + }); + + it("generates a random id when no id is provided", () => { + const session = SessionManager.inMemory(); + session.newSession(); + const id = session.getSessionId(); + expect(id).toBeDefined(); + expect(id).not.toBe(""); + }); + + it("generates a random id when options is provided without id", () => { + const session = SessionManager.inMemory(); + session.newSession({ parentSession: "parent.jsonl" }); + const id = session.getSessionId(); + expect(id).toBeDefined(); + expect(id).not.toBe(""); + }); + + it("includes the custom id in the session header", () => { + const session = SessionManager.inMemory(); + session.newSession({ id: "header-test-id" }); + + const header = session.getHeader(); + expect(header).not.toBeNull(); + expect(header!.id).toBe("header-test-id"); + }); +});