-
Notifications
You must be signed in to change notification settings - Fork 46
fix(sessions): infer adapter from model id on local task start #2937
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
ashish921998
wants to merge
4
commits into
PostHog:main
Choose a base branch
from
ashish921998:fix/local-codex-model-fallback
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.
+317
−9
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9f6d458
fix(sessions): infer adapter from model id on local task start
ashish921998 a661f8b
Merge branch 'main' into fix/local-codex-model-fallback
ashish921998 0392cdd
fix(sessions): address review - use canonical Adapter type and safe m…
ashish921998 e6f52e8
Merge branch 'main' into fix/local-codex-model-fallback
ashish921998 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import type { AgentSession } from "@posthog/shared"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { SessionService, type SessionServiceDeps } from "./sessionService"; | ||
|
|
||
| const TASK_ID = "task-1"; | ||
| const RUN_ID = "run-1"; | ||
| const REPO = "/repo"; | ||
|
|
||
| const noopLog = { | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| debug: vi.fn(), | ||
| }; | ||
|
|
||
| function makeSession(overrides: Partial<AgentSession> = {}): AgentSession { | ||
| return { | ||
| taskRunId: RUN_ID, | ||
| taskId: TASK_ID, | ||
| taskTitle: "Task", | ||
| channel: "agent-event:run-1", | ||
| events: [], | ||
| startedAt: Date.now(), | ||
| status: "error", | ||
| isPromptPending: false, | ||
| isCompacting: false, | ||
| pendingPermissions: new Map(), | ||
| promptStartedAt: null, | ||
| pausedDurationMs: 0, | ||
| messageQueue: [], | ||
| optimisticItems: [], | ||
| initialPrompt: [{ type: "text", text: "do the thing" }], | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function createService(session?: AgentSession) { | ||
| const sessions: Record<string, AgentSession> = {}; | ||
| if (session) sessions[session.taskRunId] = session; | ||
|
|
||
| const store = { | ||
| getSessions: () => sessions, | ||
| getSessionByTaskId: (taskId: string) => | ||
| Object.values(sessions).find((s) => s.taskId === taskId), | ||
| setSession: (s: AgentSession) => { | ||
| sessions[s.taskRunId] = s; | ||
| }, | ||
| updateSession: (taskRunId: string, updates: Partial<AgentSession>) => { | ||
| const s = sessions[taskRunId]; | ||
| if (s) Object.assign(s, updates); | ||
| }, | ||
| clearTailOptimisticItems: vi.fn(), | ||
| appendOptimisticItem: vi.fn(), | ||
| replaceOptimisticWithEvent: vi.fn(), | ||
| clearMessageQueue: vi.fn(), | ||
| }; | ||
|
|
||
| const deps = { | ||
| store, | ||
| log: noopLog, | ||
| notifyPromptComplete: vi.fn(), | ||
| notifyPermissionRequest: vi.fn(), | ||
| getPersistedConfigOptions: () => undefined, | ||
| setPersistedConfigOptions: vi.fn(), | ||
| trpc: { | ||
| agent: { | ||
| onSessionIdleKilled: { | ||
| subscribe: () => ({ unsubscribe: vi.fn() }), | ||
| }, | ||
| cancel: { | ||
| mutate: vi.fn().mockResolvedValue(undefined), | ||
| }, | ||
| getPreviewConfigOptions: { | ||
| query: vi.fn().mockResolvedValue([]), | ||
| }, | ||
| }, | ||
| }, | ||
| } as unknown as SessionServiceDeps; | ||
|
|
||
| const service = new SessionService(deps); | ||
|
|
||
| // Replace the private helpers clearSessionError delegates to so the test can | ||
| // assert the session-intent args in isolation, without standing up the full | ||
| // agent.start/createTaskRun/subscribe chain inside createNewLocalSession. | ||
| const createSpy = vi.fn().mockResolvedValue(undefined); | ||
| const teardownSpy = vi.fn().mockResolvedValue(undefined); | ||
| const authSpy = vi.fn().mockResolvedValue({ | ||
| apiHost: "https://us.posthog.com", | ||
| projectId: 1, | ||
| client: {}, | ||
| }); | ||
| // biome-ignore lint/suspicious/noExplicitAny: spy on private methods | ||
| const anyService = service as any; | ||
| anyService.createNewLocalSession = createSpy; | ||
| anyService.teardownSession = teardownSpy; | ||
| anyService.getAuthCredentials = authSpy; | ||
|
|
||
| return { service, createSpy, teardownSpy, sessions }; | ||
| } | ||
|
|
||
| describe("clearSessionError preserves session intent on retry", () => { | ||
| it("forwards the caller overrides (adapter/model/executionMode/reasoningLevel)", async () => { | ||
| const { service, createSpy } = createService(makeSession()); | ||
|
|
||
| await service.clearSessionError(TASK_ID, REPO, { | ||
| adapter: "codex", | ||
| model: "gpt-5.5", | ||
| executionMode: "auto", | ||
| reasoningLevel: "high", | ||
| }); | ||
|
|
||
| expect(createSpy).toHaveBeenCalledTimes(1); | ||
| // createNewLocalSession(taskId, taskTitle, repoPath, auth, initialPrompt, | ||
| // executionMode, adapter, model, reasoningLevel) | ||
| const [, , , , , executionMode, adapter, model, reasoningLevel] = | ||
| createSpy.mock.calls[0]; | ||
| expect(adapter).toBe("codex"); | ||
| expect(model).toBe("gpt-5.5"); | ||
| expect(executionMode).toBe("auto"); | ||
| expect(reasoningLevel).toBe("high"); | ||
| }); | ||
|
|
||
| it("recovers adapter/model/executionMode from a previously-live session when no overrides are given", async () => { | ||
| const session = makeSession({ | ||
| status: "error", | ||
| idleKilled: true, | ||
| adapter: "codex", | ||
| configOptions: [ | ||
| { | ||
| id: "model", | ||
| name: "Model", | ||
| type: "select", | ||
| currentValue: "gpt-5.5", | ||
| options: [], | ||
| category: "model", | ||
| }, | ||
| { | ||
| id: "mode", | ||
| name: "Approval Preset", | ||
| type: "select", | ||
| currentValue: "auto", | ||
| options: [], | ||
| category: "mode", | ||
| }, | ||
| { | ||
| id: "reasoning_effort", | ||
| name: "Reasoning Level", | ||
| type: "select", | ||
| currentValue: "high", | ||
| options: [], | ||
| category: "thought_level", | ||
| }, | ||
| ], | ||
| }); | ||
| const { service, createSpy } = createService(session); | ||
|
|
||
| await service.clearSessionError(TASK_ID, REPO); | ||
|
|
||
| expect(createSpy).toHaveBeenCalledTimes(1); | ||
| const [, , , , , executionMode, adapter, model, reasoningLevel] = | ||
| createSpy.mock.calls[0]; | ||
| expect(adapter).toBe("codex"); | ||
| expect(model).toBe("gpt-5.5"); | ||
| expect(executionMode).toBe("auto"); | ||
| expect(reasoningLevel).toBe("high"); | ||
| }); | ||
|
|
||
| it("keeps the retry placeholder when fresh session creation fails", async () => { | ||
| const session = makeSession(); | ||
| const { service, createSpy, teardownSpy, sessions } = | ||
| createService(session); | ||
| createSpy.mockRejectedValueOnce(new Error("still broken")); | ||
|
|
||
| await expect(service.clearSessionError(TASK_ID, REPO)).rejects.toThrow( | ||
| "still broken", | ||
| ); | ||
|
|
||
| expect(teardownSpy).not.toHaveBeenCalled(); | ||
| expect(sessions[RUN_ID]).toBe(session); | ||
| }); | ||
|
|
||
| it("recovers a Codex placeholder with no explicit model as Codex default", async () => { | ||
| const session = makeSession({ | ||
| adapter: "codex", | ||
| configOptions: [], | ||
| }); | ||
| const { service, createSpy } = createService(session); | ||
|
|
||
| await service.clearSessionError(TASK_ID, REPO); | ||
|
|
||
| expect(createSpy).toHaveBeenCalledTimes(1); | ||
| const [, , , , , , adapter, model] = createSpy.mock.calls[0]; | ||
| expect(adapter).toBe("codex"); | ||
| expect(model).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
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,25 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { inferAdapterFromModelId } from "./modelAdapter"; | ||
|
|
||
| describe("inferAdapterFromModelId", () => { | ||
| it.each(["gpt-5.5", "gpt-5.4", "o3", "o4-mini", "codex-mini"])( | ||
| "maps OpenAI model %s to Codex", | ||
| (model) => { | ||
| expect(inferAdapterFromModelId(model)).toBe("codex"); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(["claude-opus-4-8", "claude-sonnet-4-5"])( | ||
| "maps Anthropic model %s to Claude", | ||
| (model) => { | ||
| expect(inferAdapterFromModelId(model)).toBe("claude"); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([undefined, null, "", "fable", "custom-model"])( | ||
| "leaves ambiguous model %s unchanged", | ||
| (model) => { | ||
| expect(inferAdapterFromModelId(model)).toBeUndefined(); | ||
| }, | ||
| ); | ||
| }); |
Oops, something went wrong.
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.
currentValuefromgetConfigOptionByCategoryis typed asstring, so casting it directly toExecutionModeis unsafe — any persisted value that doesn't match the union (e.g. a stale or renamed mode) would pass the cast silently and reachagent.start.mutateas an invalidpermissionMode. The same pattern used forreasoningLevel— accept it asstringand let the downstream call validate or ignore it — is safer here too.