-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Prevent Codex duplicate completions #8125
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
Closed
Buzzwavemed
wants to merge
1
commit into
RooCodeInc:main
from
Buzzwavemed:fix-codex-duplicate-completion
Closed
Changes from all commits
Commits
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,132 @@ | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from "vitest" | ||
| import type { Anthropic } from "@anthropic-ai/sdk" | ||
|
|
||
| import type { ApiStreamChunk } from "../../transform/stream" | ||
| import type { ApiHandlerCreateMessageMetadata } from "../../index" | ||
| import type { ApiHandlerOptions } from "../../../shared/api" | ||
| import { OpenAiNativeHandler } from "../openai-native" | ||
|
|
||
| const createSessionMock = vi.hoisted(() => vi.fn()) | ||
|
|
||
| vi.mock("openai", () => { | ||
| return { | ||
| __esModule: true, | ||
| default: vi.fn().mockImplementation(() => ({ | ||
| responses: { | ||
| create: vi.fn(), | ||
| }, | ||
| })), | ||
| } | ||
| }) | ||
|
|
||
| vi.mock("../../../integrations/codex/run", () => ({ | ||
| CodexCliSession: { | ||
| create: createSessionMock, | ||
| }, | ||
| })) | ||
|
|
||
| import { CodexHandler } from "../codex" | ||
|
|
||
| const systemPrompt = "You are Codex." | ||
|
|
||
| const defaultOptions: ApiHandlerOptions = { | ||
| apiModelId: "gpt-5-codex", | ||
| openAiNativeApiKey: "test-api-key", | ||
| } | ||
|
|
||
| const metadata: ApiHandlerCreateMessageMetadata = { | ||
| taskId: "task-123", | ||
| } | ||
|
|
||
| const makeSession = (chunks: ApiStreamChunk[]) => ({ | ||
| runTurn: vi.fn().mockResolvedValue(asyncGeneratorFromChunks(chunks)), | ||
| shutdown: vi.fn().mockResolvedValue(undefined), | ||
| }) | ||
|
|
||
| function asyncGeneratorFromChunks(chunks: ApiStreamChunk[]): AsyncGenerator<ApiStreamChunk> { | ||
| return (async function* () { | ||
| for (const chunk of chunks) { | ||
| yield chunk | ||
| } | ||
| })() | ||
| } | ||
|
|
||
| describe("CodexHandler", () => { | ||
| let handler: CodexHandler | ||
| let fallbackSpy: ReturnType<typeof vi.spyOn> | ||
|
|
||
| beforeEach(() => { | ||
| createSessionMock.mockReset() | ||
| fallbackSpy = vi.spyOn(OpenAiNativeHandler.prototype, "createMessage").mockImplementation(async function* () { | ||
| yield { type: "text", text: "[fallback]" } | ||
| }) | ||
| handler = new CodexHandler(defaultOptions) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| fallbackSpy.mockRestore() | ||
| }) | ||
|
|
||
| it("omits duplicate prefix chunks before streaming new Codex output", async () => { | ||
| const messages: Anthropic.Messages.MessageParam[] = [ | ||
| { role: "user", content: "Initial request" }, | ||
| { role: "assistant", content: "Task completed successfully." }, | ||
| { role: "user", content: "Please continue" }, | ||
| ] | ||
|
|
||
| createSessionMock.mockResolvedValueOnce( | ||
| makeSession([ | ||
| { type: "text", text: "Task completed successfully." }, | ||
| { type: "text", text: "Here are the next steps." }, | ||
| { type: "usage", inputTokens: 10, outputTokens: 5 }, | ||
| ]), | ||
| ) | ||
|
|
||
| const stream = handler.createMessage(systemPrompt, messages, metadata) | ||
| const chunks: ApiStreamChunk[] = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| const textChunks = chunks.filter((chunk) => chunk.type === "text") | ||
| expect(textChunks).toHaveLength(1) | ||
| expect(textChunks[0].text).toBe("Here are the next steps.") | ||
| const usageChunk = chunks.find((chunk) => chunk.type === "usage") | ||
| expect(usageChunk).toBeTruthy() | ||
| expect(fallbackSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great test coverage! Could we add an additional test case for partial duplicates? For example, when the Codex CLI returns only part of the previous message repeated. This would ensure the sharedPrefixLength logic handles all edge cases. |
||
|
|
||
| it("replays the turn without the prior assistant message when Codex repeats itself", async () => { | ||
| const messages: Anthropic.Messages.MessageParam[] = [ | ||
| { role: "user", content: "Initial request" }, | ||
| { role: "assistant", content: "Task completed successfully." }, | ||
| { role: "user", content: "Please continue" }, | ||
| ] | ||
|
|
||
| createSessionMock | ||
| .mockResolvedValueOnce( | ||
| makeSession([ | ||
| { type: "text", text: "Task completed successfully." }, | ||
| { type: "usage", inputTokens: 10, outputTokens: 2 }, | ||
| ]), | ||
| ) | ||
| .mockResolvedValueOnce( | ||
| makeSession([ | ||
| { type: "text", text: "Continuing with the follow-up work." }, | ||
| { type: "usage", inputTokens: 12, outputTokens: 6 }, | ||
| ]), | ||
| ) | ||
|
|
||
| const stream = handler.createMessage(systemPrompt, messages, metadata) | ||
| const chunks: ApiStreamChunk[] = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| const textChunks = chunks.filter((chunk) => chunk.type === "text") | ||
| expect(textChunks).toHaveLength(1) | ||
| expect(textChunks[0].text).toBe("Continuing with the follow-up work.") | ||
| expect(createSessionMock).toHaveBeenCalledTimes(2) | ||
| expect(fallbackSpy).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.
The makeSession helper is useful. If other Codex-related tests need similar mocking in the future, would it be beneficial to extract this to a shared test utility file like
__tests__/helpers/codex-mocks.ts?