Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/ai/ai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,40 @@ describe("resolveSDKModel", () => {
// Codex SDK event mapping
// ---------------------------------------------------------------------------

import { mapCodexEvent, mapCodexItem } from "./providers/codex-sdk.ts";
import {
mapCodexEvent,
mapCodexItem,
shouldSkipGitRepoCheck,
} from "./providers/codex-sdk.ts";

describe("shouldSkipGitRepoCheck", () => {
test("keeps the Codex git repo check inside a worktree", async () => {
const probe = async (command: string, args: string[], options: { encoding: "utf8" }) => {
expect(command).toBe("git");
expect(args).toEqual(["-C", "/repo", "rev-parse", "--is-inside-work-tree"]);
expect(options).toEqual({ encoding: "utf8" });
return { stdout: "true\n" };
};

expect(await shouldSkipGitRepoCheck("/repo", probe)).toBe(false);
});

test("skips the Codex git repo check for standalone document sessions", async () => {
const probe = async () => {
throw new Error("not a git repo");
};

expect(await shouldSkipGitRepoCheck("/tmp/plain-session", probe)).toBe(true);
});

test("skips the Codex git repo check when the probe cannot run", async () => {
const probe = async () => {
throw new Error("git unavailable");
};

expect(await shouldSkipGitRepoCheck("/tmp/plain-session", probe)).toBe(true);
});
});

describe("mapCodexEvent", () => {
function offsets() {
Expand Down
39 changes: 37 additions & 2 deletions packages/ai/providers/codex-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import { buildSystemPrompt, buildEffectivePrompt } from "../context.ts";
import { BaseSession } from "../base-session.ts";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import type {
AIProvider,
AIProviderCapabilities,
Expand All @@ -28,6 +30,30 @@ import type {
const PROVIDER_NAME = "codex-sdk";
const DEFAULT_MODEL = "gpt-5.4";

type GitWorkTreeProbe = (
command: string,
args: string[],
options: { encoding: "utf8" },
) => Promise<{ stdout?: string | Buffer }>;

const execFileAsync = promisify(execFile);

export async function shouldSkipGitRepoCheck(
cwd: string,
probe: GitWorkTreeProbe = execFileAsync,
): Promise<boolean> {
try {
const result = probe(
"git",
["-C", cwd, "rev-parse", "--is-inside-work-tree"],
{ encoding: "utf8" },
);
return String((await result).stdout ?? "").trim() !== "true";
} catch {
return true;
}
}

// ---------------------------------------------------------------------------
// Provider
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -57,10 +83,13 @@ export class CodexSDKProvider implements AIProvider {
}

async createSession(options: CreateSessionOptions): Promise<AISession> {
const cwd = options.cwd ?? this.config.cwd ?? process.cwd();
const skipGitRepoCheck = await shouldSkipGitRepoCheck(cwd);
return new CodexSDKSession({
...this.baseConfig(options),
systemPrompt: buildSystemPrompt(options.context),
cwd: options.cwd ?? this.config.cwd ?? process.cwd(),
cwd,
skipGitRepoCheck,
parentSessionId: null,
});
}
Expand All @@ -73,10 +102,13 @@ export class CodexSDKProvider implements AIProvider {
}

async resumeSession(sessionId: string): Promise<AISession> {
const cwd = this.config.cwd ?? process.cwd();
const skipGitRepoCheck = await shouldSkipGitRepoCheck(cwd);
return new CodexSDKSession({
...this.baseConfig(),
systemPrompt: null,
cwd: this.config.cwd ?? process.cwd(),
cwd,
skipGitRepoCheck,
parentSessionId: null,
resumeThreadId: sessionId,
});
Expand Down Expand Up @@ -123,6 +155,7 @@ interface SessionConfig {
maxTurns: number;
sandboxMode: "read-only" | "workspace-write" | "danger-full-access";
cwd: string;
skipGitRepoCheck: boolean;
parentSessionId: string | null;
resumeThreadId?: string;
codexExecutablePath?: string;
Expand Down Expand Up @@ -173,13 +206,15 @@ class CodexSDKSession extends BaseSession {
this._thread = this._codexInstance.resumeThread(this.config.resumeThreadId, {
model: this.config.model,
workingDirectory: this.config.cwd,
skipGitRepoCheck: this.config.skipGitRepoCheck,
sandboxMode: this.config.sandboxMode,
...(this.config.reasoningEffort && { modelReasoningEffort: this.config.reasoningEffort }),
});
} else {
this._thread = this._codexInstance.startThread({
model: this.config.model,
workingDirectory: this.config.cwd,
skipGitRepoCheck: this.config.skipGitRepoCheck,
sandboxMode: this.config.sandboxMode,
...(this.config.reasoningEffort && { modelReasoningEffort: this.config.reasoningEffort }),
});
Expand Down