Skip to content
Open
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
75 changes: 53 additions & 22 deletions apps/server/src/provider/CodexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as CodexSchema from "effect-codex-app-server/schema";
import * as CodexErrors from "effect-codex-app-server/errors";

import type {
CodexProbeTimeouts,
CodexSettings,
CustomModelSetting,
ServerProvider,
Expand All @@ -31,7 +32,11 @@ import type {
ServerProviderModel,
ServerProviderSkill,
} from "@t3tools/contracts";
import { PREFERRED_DEFAULT_CODEX_MODELS, ServerSettingsError } from "@t3tools/contracts";
import {
DEFAULT_CODEX_PROBE_TIMEOUTS,
PREFERRED_DEFAULT_CODEX_MODELS,
ServerSettingsError,
} from "@t3tools/contracts";

import {
codexModelFamily,
Expand All @@ -58,6 +63,7 @@ import {
type CodexResetCreditsSummary,
} from "./codexUsageLimits.ts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
import packageJson from "../../package.json" with { type: "json" };
const isCodexAppServerSpawnError = Schema.is(CodexErrors.CodexAppServerSpawnError);

Expand Down Expand Up @@ -137,17 +143,19 @@ export const codexRoutesSpendNativeQuota = (
account.requiresOpenaiAuth && account.account?.type !== "apiKey" && routesToNativeOpenai(config);

const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const;
const CODEX_CATALOG_REFRESH_TIMEOUT_MS = 3_000;
const CODEX_CONFIG_PROBE_TIMEOUT_MS = 3_000;
const CODEX_CATALOG_REFRESH_POLL = "100 millis" as const;
const CODEX_CLIENT_NAME = "T3 Code";
// Handshake plus account/models/skills requests can exceed the shared 10s probe budget.
const CODEX_AUTH_PROBE_TIMEOUT_MS = 20_000;
const CODEX_DISCOVERY_MARGIN_MS = 250;
// `codex-cli 0.159.0`, `codex-cli 0.160.0-alpha.1` or the npm CLI's bare `0.1.4161006`.
// Keep a prerelease suffix so compatibility stays unknown for it, as it does for `initialize`.
const CODEX_CLI_VERSION_PATTERN = /(?:^|\s)v?(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)(?=\s|$)/u;

export const readCodexProbeTimeouts = (host: ProviderHost.ProviderHost["Service"]) =>
host.settings.get.pipe(
Effect.map((settings) => settings.codexProbeTimeouts),
Effect.orElseSucceed(() => DEFAULT_CODEX_PROBE_TIMEOUTS),
);

const CODEX_PRESENTATION = {
displayName: "Codex",
showInteractionModeToggle: true,
Expand Down Expand Up @@ -575,6 +583,7 @@ const requestCodexChatGptModels = Effect.fn("requestCodexChatGptModels")(functio
readonly codexHome: string;
readonly spawnedAtMs: number;
readonly deadlineMs: number;
readonly catalogRefreshMs: number;
}) {
const models = yield* withCodexDiscoveryDeadline(
requestAllCodexModels(input.client),
Expand All @@ -589,7 +598,7 @@ const requestCodexChatGptModels = Effect.fn("requestCodexChatGptModels")(functio
}),
),
input.deadlineMs,
CODEX_CATALOG_REFRESH_TIMEOUT_MS,
input.catalogRefreshMs,
);
if (refreshed === undefined) {
yield* Effect.logWarning(
Expand Down Expand Up @@ -734,7 +743,9 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
readonly cwd: string;
readonly environment?: NodeJS.ProcessEnv;
readonly skipNativeUsage?: boolean;
readonly timeouts?: CodexProbeTimeouts;
}) {
const timeouts = input.timeouts ?? DEFAULT_CODEX_PROBE_TIMEOUTS;
const spawnedAtMs = DateTime.toEpochMillis(yield* DateTime.now);
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const processScope = yield* Scope.make();
Expand Down Expand Up @@ -777,13 +788,14 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
codexHome: initialize.codexHome,
spawnedAtMs,
deadlineMs,
catalogRefreshMs: timeouts.catalogRefresh,
})
: withCodexDiscoveryDeadline(requestAllCodexModels(client), deadlineMs),
// The shared catalog must exclude config from the server's launch project.
withCodexDiscoveryDeadline(
client.request("config/read", { includeLayers: false }),
deadlineMs,
CODEX_CONFIG_PROBE_TIMEOUT_MS,
timeouts.config,
).pipe(
Effect.catch(() => Effect.logDebug("Codex config read failed.").pipe(Effect.as(undefined))),
Effect.flatMap((config) => {
Expand Down Expand Up @@ -864,20 +876,35 @@ const probeCodexCliVersion = Effect.fn("probeCodexCliVersion")(
),
);

export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(function* (input: {
readonly binaryPath: string;
readonly homePath?: string;
readonly launchArgs?: string;
readonly cwd: string;
readonly environment?: NodeJS.ProcessEnv;
}) {
const { client } = yield* withCodexAppServerClient({
...input,
environment: providerProbeEnvironment(input.environment ?? process.env),
});
const skillsResponse = yield* client.request("skills/list", { cwds: [input.cwd] });
return parseCodexSkillsListResponse(skillsResponse, input.cwd);
});
export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(
function* (input: {
readonly binaryPath: string;
readonly homePath?: string;
readonly launchArgs?: string;
readonly cwd: string;
readonly environment?: NodeJS.ProcessEnv;
readonly timeouts?: CodexProbeTimeouts;
}) {
const { client } = yield* withCodexAppServerClient({
...input,
environment: providerProbeEnvironment(input.environment ?? process.env),
});
const skillsResponse = yield* client.request("skills/list", { cwds: [input.cwd] });
return parseCodexSkillsListResponse(skillsResponse, input.cwd);
},
(effect, input) =>
effect.pipe(
Effect.timeout(
Duration.millis(
resolveProviderProbeTimeoutMs(
"codex",
(input.timeouts ?? DEFAULT_CODEX_PROBE_TIMEOUTS).skills,
input.environment,
),
),
),
),
);

// Long enough for a burst of workspace switches; short because the helper holds the user's
// binary open, and Windows refuses to overwrite or delete a running executable.
Expand Down Expand Up @@ -1019,18 +1046,21 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
readonly cwd: string;
readonly environment?: NodeJS.ProcessEnv;
readonly skipNativeUsage?: boolean;
readonly timeouts?: CodexProbeTimeouts;
}) => Effect.Effect<
CodexAppServerProviderSnapshot,
CodexErrors.CodexAppServerError,
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path | Scope.Scope
> = probeCodexAppServerProvider,
environment?: NodeJS.ProcessEnv,
managedAuth?: ServerProvider["auth"],
timeouts?: CodexProbeTimeouts,
): Effect.fn.Return<
ServerProviderDraft,
ServerSettingsError,
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
> {
const resolvedTimeouts = timeouts ?? DEFAULT_CODEX_PROBE_TIMEOUTS;
const resolvedEnvironment = providerProbeEnvironment(environment ?? process.env);
const checkedAt = DateTime.formatIso(yield* DateTime.now);
const emptyModels = emptyCodexModelsFromSettings(codexSettings);
Expand All @@ -1054,7 +1084,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu

const probeTimeoutMs = resolveProviderProbeTimeoutMs(
"codex",
CODEX_AUTH_PROBE_TIMEOUT_MS,
resolvedTimeouts.startup,
resolvedEnvironment,
);
const probeResult = yield* probe({
Expand All @@ -1065,6 +1095,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
cwd: process.cwd(),
environment: resolvedEnvironment,
...(managedAuth ? { skipNativeUsage: true } : {}),
timeouts: resolvedTimeouts,
}).pipe(
// Process teardown can outlast the work budget without discarding a completed snapshot.
Effect.timeoutOption(Duration.millis(probeTimeoutMs)),
Expand Down
60 changes: 40 additions & 20 deletions apps/server/src/provider/Drivers/CodexDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
checkCodexProviderStatus,
makePendingCodexProvider,
makeCodexSkillsProbe,
readCodexProbeTimeouts,
withCodexAppServerClient,
} from "../CodexProvider.ts";
import { resolveCodexLaunchArgs } from "../codexLaunchArgs.ts";
Expand Down Expand Up @@ -177,6 +178,8 @@ export const CodexDriver: ProviderDriver<
config,
});
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const providerHost = yield* ProviderHost.ProviderHost;
const probeTimeouts = readCodexProbeTimeouts(providerHost);
const resetCreditCoordinator = yield* ResetCreditCoordinator.ResetCreditCoordinator;
const fileSystem = yield* FileSystem.FileSystem;
const pathService = yield* Path.Path;
Expand Down Expand Up @@ -271,7 +274,11 @@ export const CodexDriver: ProviderDriver<
const checkProvider = modelCatalog.refreshInBackground.pipe(
Effect.andThen(
Effect.zipWith(
checkCodexProviderStatus(effectiveConfig, undefined, processEnv),
probeTimeouts.pipe(
Effect.flatMap((timeouts) =>
checkCodexProviderStatus(effectiveConfig, undefined, processEnv, undefined, timeouts),
),
),
currentCatalog,
(draft, catalog) => stampIdentity(applyCodexModelCatalog(draft, catalog)),
{ concurrent: true },
Expand Down Expand Up @@ -329,13 +336,23 @@ export const CodexDriver: ProviderDriver<
? snapshot.getSnapshot
: Effect.all([
snapshot.getSnapshot,
probeSkills({
binaryPath: effectiveConfig.binaryPath,
homePath: effectiveConfig.homePath,
launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv),
cwd,
environment: processEnv,
}).pipe(Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv))),
probeTimeouts.pipe(
Effect.flatMap((timeouts) =>
probeSkills({
binaryPath: effectiveConfig.binaryPath,
homePath: effectiveConfig.homePath,
launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv),
cwd,
environment: processEnv,
}).pipe(
Effect.timeout(
Duration.millis(
resolveProviderProbeTimeoutMs("codex", timeouts.skills, processEnv),
),
),
),
),
),
]).pipe(
Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills })),
Effect.mapError(
Expand All @@ -357,18 +374,21 @@ export const CodexDriver: ProviderDriver<
resetCreditCoordinator
.redeem(accountKey, (idempotencyKey) =>
Effect.gen(function* () {
const { client } = yield* withCodexAppServerClient({
binaryPath: effectiveConfig.binaryPath,
homePath: effectiveConfig.homePath,
launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv),
cwd: process.cwd(),
environment: processEnv,
});
const response = yield* client.request("account/rateLimitResetCredit/consume", {
idempotencyKey,
});
return response.outcome;
}).pipe(Effect.scoped, Effect.timeout("20 seconds")),
const timeouts = yield* probeTimeouts;
return yield* Effect.gen(function* () {
const { client } = yield* withCodexAppServerClient({
binaryPath: effectiveConfig.binaryPath,
homePath: effectiveConfig.homePath,
launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv),
cwd: process.cwd(),
environment: processEnv,
});
const response = yield* client.request("account/rateLimitResetCredit/consume", {
idempotencyKey,
});
return response.outcome;
}).pipe(Effect.scoped, Effect.timeout(Duration.millis(timeouts.resetCredit)));
}),
)
.pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
Expand Down
20 changes: 13 additions & 7 deletions apps/server/src/provider/Drivers/CodexManagedProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
checkCodexProviderStatus,
makePendingCodexProvider,
probeCodexSkillsForCwd,
readCodexProbeTimeouts,
} from "../CodexProvider.ts";
import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
Expand Down Expand Up @@ -132,6 +133,7 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu
undefined,
effective.environment,
managedAuth,
yield* readCodexProbeTimeouts(host),
);
const credentials = yield* runtime.auth.access;
// Another server can replace the saved profile while the native probe runs.
Expand Down Expand Up @@ -316,13 +318,17 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fu
),
),
onSuccess: (effective) =>
probeCodexSkillsForCwd({
binaryPath: effective.config.binaryPath,
homePath: effective.config.homePath,
launchArgs: effective.config.launchArgs,
cwd,
environment: effective.environment,
}).pipe(
readCodexProbeTimeouts(host).pipe(
Effect.flatMap((timeouts) =>
probeCodexSkillsForCwd({
binaryPath: effective.config.binaryPath,
homePath: effective.config.homePath,
launchArgs: effective.config.launchArgs,
cwd,
environment: effective.environment,
timeouts,
}),
),
Effect.flatMap((skills) =>
snapshot.getSnapshot.pipe(Effect.map((draft) => ({ ...draft, skills }))),
),
Expand Down
Loading
Loading