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
1 change: 1 addition & 0 deletions scripts/test-layout/layout-clients.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"claude-translated-tool-restrictions.test.ts": "claude-integration",
"windows-config-root-async-writers.test.ts": "windows",
"windows-config-root-integration-writers.test.ts": "windows",
"windows-cli-config-root-startup.test.ts": "windows",
"t3code-client-lifecycle.test.ts": "clients",
"fix-jsonc-cr-comments.test.ts": "lib",
"fix-jsonc-empty-comma.test.ts": "lib",
Expand Down
47 changes: 5 additions & 42 deletions src/cli/codex-shim-autorestore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { autoRestoreCodexShim, autoRestoreCodexShimAsync } from "../codex/shim";
import { codexShimAutoRestoreEnabled, readConfigDiagnostics } from "../config";
import { isOrchestratedUpdateArgv } from "../update/orchestrated-arguments.mjs";
import { skipsCodexShimAutoRestore } from "./preflight-exemptions";

// Re-exported for existing importers; the list itself lives in a leaf module so a caller that
// only needs it (the config-root startup wait) does not link the shim repair machinery.
export { skipsCodexShimAutoRestore };

export interface CodexShimAutoRestoreCliDeps {
env: NodeJS.ProcessEnv;
Expand All @@ -16,47 +20,6 @@ const DEFAULT_DEPS: CodexShimAutoRestoreCliDeps = {
readConfig: readConfigDiagnostics,
};

/** Identify command namespaces whose execution must not trigger automatic shim repair. */
export function skipsCodexShimAutoRestore(command: string | undefined, args: string[]): boolean {
if (command === "uninstall" || command === "remove") return true;
// Contract discovery and orchestrated update must validate before any repair.
if (command === "capabilities") return true;
if (command === "update" && isOrchestratedUpdateArgv(args)) return true;
if (command === "sync") return true;
if (command === "status" || command === "doctor") return true;
// Explicit orphan-token recovery must not mutate Codex before its guards run.
if (command === "disconnect" && args.includes("--orphaned-token")) return true;
// The launcher owns shim handoff after classifying routing, including native fast paths.
if (command === "codex") return true;
// Exec admission only observes the selected target; it must never repair a shim first.
if (command === "internal" && args[1] === "codex-exec-target") return true;
// `lab` is read-only inspection; it must not trigger shim side effects.
if (command === "lab") return true;
// Local messaging owns its helpers; even malformed usage must not repair/start anything.
if (command === "message") return true;
// `resolve` is read-only inspection for embedding shells: a lookup made to populate
// a consent surface must not trigger a shim repair side effect first.
if (command === "resolve") return true;
// Local model inventory is read-only; mutations and live commands keep preflight.
if (command === "models" || command === "model") {
const subcommand = args[1];
if (!subcommand || subcommand.startsWith("-") || subcommand === "list" || subcommand === "list-custom") return true;
}
// `service status --json` is a passive inventory read for peers such as GOAT: a shim repair
// before dispatch would change the installation it was asked only to describe.
// Flags may precede the verb (`ocx service --json status`), exactly as parseServiceArgs reads it.
if (command === "service" && args.includes("--json")
&& args.slice(1).find(arg => !arg.startsWith("--")) === "status") return true;
// The desktop's approval-bound stop must validate its snapshot before any unrelated
// CLI repair changes the installation it was approved against.
if (command === "stop" && args.some(arg => arg.startsWith("--expect-"))) return true;
// The entire updater-inspection namespace is zero-effect, including malformed
// or future actions. A later `apply` implementation must own its preflight.
if (command === "system" && args[1] === "codex-cli-update") return true;
if (command === "__update-badge") return true;
return command === "codex-shim" && ["install", "status", "uninstall", "remove"].includes(args[1] ?? "");
}

export function maybeAutoRestoreCodexShim(
command: string | undefined,
args: string[],
Expand Down
34 changes: 34 additions & 0 deletions src/cli/config-root-startup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getConfigDir, hardenConfigDir, withConfigDirHardeningSettled } from "../config/paths";
import { WindowsSecretAclSettleTimeoutError, windowsSecretAclApplies } from "../lib/windows-secret-acl";
import { skipsCodexShimAutoRestore } from "./preflight-exemptions";

/**
* Wait once, before a CLI command does its first write, for the optional config-root ACL flight
* (#7815). On Windows, loadConfig() starts an unawaited icacls harden of the config root, and its
* inheritance work can drop a grant a concurrent writer just applied. Admitted writers already
* wait for it themselves; this covers the synchronous writers a CLI command reaches before them.
*
* Skipped where waiting would itself be the side effect or the delay matters:
* - the commands skipsCodexShimAutoRestore exempts from preflight repair (read-only, hot-path
* and validate-first commands). Adding a command there also removes this wait for it;
* - every "internal" helper and every hidden "__*" helper, launched by other processes under their
* own budgets (startup health, tray start/restart/host, version refresh, the GUI update worker);
* - "start", the long-lived server, and "ensure", which the Windows Codex shim runs before every
* launch to start it: the wait would run before Bun.serve and spend up to one ACL deadline of
* fixed budgets (update-restart marker, ensure readiness, Desktop cold start, service start
* timeouts, each Codex launch). The server's synchronous writers are documented residual.
*
* Bounded by one ACL deadline, which also drains timed-out icacls children. A timeout lets the
* command proceed: writers that admit through withConfigDirHardeningSettled still refuse alone.
*/
export async function settleConfigRootForCli(command: string | undefined, args: readonly string[]): Promise<void> {
if (!windowsSecretAclApplies()) return;
if (command === undefined || command === "start" || command === "ensure" || command === "internal"
|| command.startsWith("__") || skipsCodexShimAutoRestore(command, [...args])) return;
hardenConfigDir();
try {
await withConfigDirHardeningSettled(getConfigDir(), () => undefined);
} catch (error) {
if (!(error instanceof WindowsSecretAclSettleTimeoutError)) throw error;
}
}
42 changes: 42 additions & 0 deletions src/cli/preflight-exemptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { isOrchestratedUpdateArgv } from "../update/orchestrated-arguments.mjs";

/** Identify command namespaces whose execution must not trigger automatic shim repair. */
export function skipsCodexShimAutoRestore(command: string | undefined, args: string[]): boolean {
if (command === "uninstall" || command === "remove") return true;
// Contract discovery and orchestrated update must validate before any repair.
if (command === "capabilities") return true;
if (command === "update" && isOrchestratedUpdateArgv(args)) return true;
if (command === "sync") return true;
if (command === "status" || command === "doctor") return true;
// Explicit orphan-token recovery must not mutate Codex before its guards run.
if (command === "disconnect" && args.includes("--orphaned-token")) return true;
// The launcher owns shim handoff after classifying routing, including native fast paths.
if (command === "codex") return true;
// Exec admission only observes the selected target; it must never repair a shim first.
if (command === "internal" && args[1] === "codex-exec-target") return true;
// `lab` is read-only inspection; it must not trigger shim side effects.
if (command === "lab") return true;
// Local messaging owns its helpers; even malformed usage must not repair/start anything.
if (command === "message") return true;
// `resolve` is read-only inspection for embedding shells: a lookup made to populate
// a consent surface must not trigger a shim repair side effect first.
if (command === "resolve") return true;
// Local model inventory is read-only; mutations and live commands keep preflight.
if (command === "models" || command === "model") {
const subcommand = args[1];
if (!subcommand || subcommand.startsWith("-") || subcommand === "list" || subcommand === "list-custom") return true;
}
// `service status --json` is a passive inventory read for peers such as GOAT: a shim repair
// before dispatch would change the installation it was asked only to describe.
// Flags may precede the verb (`ocx service --json status`), exactly as parseServiceArgs reads it.
if (command === "service" && args.includes("--json")
&& args.slice(1).find(arg => !arg.startsWith("--")) === "status") return true;
// The desktop's approval-bound stop must validate its snapshot before any unrelated
// CLI repair changes the installation it was approved against.
if (command === "stop" && args.some(arg => arg.startsWith("--expect-"))) return true;
// The entire updater-inspection namespace is zero-effect, including malformed
// or future actions. A later `apply` implementation must own its preflight.
if (command === "system" && args[1] === "codex-cli-update") return true;
if (command === "__update-badge") return true;
return command === "codex-shim" && ["install", "status", "uninstall", "remove"].includes(args[1] ?? "");
}
4 changes: 4 additions & 0 deletions src/cli/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { findCommand } from "./registry";
import { printUnknownCommand } from "./help-recovery";
import { noteCredentialArgv, redactSecretArgs } from "./secret-args";
import { maybeNoticeVersionSkew } from "./version-skew-notice";
import { settleConfigRootForCli } from "./config-root-startup";
import { isOrchestratedUpdateArgv } from "../update/orchestrated-arguments.mjs";

export interface CliHead {
Expand Down Expand Up @@ -161,6 +162,9 @@ export async function runCli(argv: string[]): Promise<CliHead> {
process.exit(SYNC_EXPECTATION_USAGE_EXIT);
}
}
// Before any preflight or command write: wait once for the config-root ACL flight (#7815).
// Commands skipsCodexShimAutoRestore exempts are skipped there too; see config-root-startup.
await settleConfigRootForCli(head.command, head.args);
await maybeNoticeVersionSkew(head.command, head.args);
await maybeAutoRestoreCodexShimAsync(head.command, head.args);
return head;
Expand Down
Loading
Loading