How the Codex Plugin Handles Authentication and Availability Checks
The Codex plugin validates CLI availability and authentication status via getCodexAvailability and getCodexAuthStatus in plugins/codex/scripts/lib/codex.mjs, combining these checks into a single ready flag to gate downstream operations.
The openai/codex-plugin-cc repository provides the reference implementation for integrating Codex into creative workflows. Understanding how the Codex plugin handles authentication and availability checks ensures your application can detect missing binaries and expired sessions before executing expensive operations.
Validating Codex CLI Availability
The plugin verifies that the Codex binary is present and executable through the getCodexAvailability function defined in plugins/codex/scripts/lib/codex.mjs. This function executes the Codex CLI with the --version flag and captures the output.
Successful execution returns an object confirming the installation:
{ available: true, version: "<detected-version>" }
If the binary is missing, not executable, or exits with a non-zero status, the function catches the error and returns:
{ available: false, error: <error-message> }
This check acts as a gatekeeper, ensuring that subsequent logic only runs when the Codex CLI is actually installed on the host system.
Checking Authentication Status with getCodexAuthStatus
Once availability is confirmed, the plugin interrogates the user's session using getCodexAuthStatus, implemented in the same codex.mjs module. The function invokes codex auth status --json (or an equivalent sub-command) to retrieve structured authentication data.
The returned object contains the following fields:
loggedIn: Boolean indicating whether an active session exists.requiresOpenaiAuth: Boolean indicating if the provider requires OpenAI authentication.authMethod: Enumeration of"chatgpt","apiKey", ornull.source: Origin identifier, either"cli"or"app-server".detail: Human-readable description of the authentication state.
For self-hosted Codex instances that do not require OpenAI authentication, requiresOpenaiAuth is false and the detail field explains that authentication is unnecessary. When the session has expired, the detail field contains specific remediation instructions such as "authentication expired; run codex login", enabling the plugin to suggest commands like codex login --device-auth.
Combining Checks into a Ready State
Higher-level workflows in plugins/codex/scripts/codex-companion.mjs aggregate these validations to produce a unified ready flag. The logic combines Node.js status, Codex availability, and authentication state:
ready: nodeStatus.available && codexStatus.available && authStatus.loggedIn,
When ready evaluates to false but the Codex binary is present, the UI presents targeted next-step instructions—such as triggering device authentication—rather than failing with generic errors. Unit tests in tests/runtime.test.mjs verify these behaviors against the mock Codex server defined in tests/fake-codex-fixture.mjs.
Practical Implementation Examples
The following pattern demonstrates how to integrate these checks into your validation workflow:
import { getCodexAvailability, getCodexAuthStatus } from './lib/codex.mjs';
async function isCodexReady(cwd) {
const availability = await getCodexAvailability(cwd);
if (!availability.available) {
console.error('Codex CLI not found:', availability.error);
return false;
}
const auth = await getCodexAuthStatus(cwd);
if (!auth.loggedIn && auth.requiresOpenaiAuth) {
console.error('Codex not authenticated:', auth.detail);
return false;
}
return true;
}
For comprehensive status reporting, the companion script aggregates multiple concurrent checks:
import { getCodexAuthStatus } from './lib/codex.mjs';
import { getCodexAvailability } from './lib/codex.mjs';
const [nodeStatus, codexStatus, authStatus] = await Promise.all([
getNodeStatus(),
getCodexAvailability(cwd),
getCodexAuthStatus(cwd),
]);
const report = {
ready: nodeStatus.available && codexStatus.available && authStatus.loggedIn,
node: nodeStatus,
codex: codexStatus,
auth: authStatus,
};
Summary
getCodexAvailabilityinplugins/codex/scripts/lib/codex.mjsexecutes the Codex CLI with--versionto confirm binary presence, returning structured metadata including the detected version string.getCodexAuthStatusqueriescodex auth status --jsonto determine session validity, distinguishing between ChatGPT sessions, API keys, and self-hosted configurations that bypass OpenAI authentication.- Both checks are combined into a single
readyboolean incodex-companion.mjsto gate downstream operations and surface actionable remediation guidance when authentication expires. - The test suite in
tests/runtime.test.mjsvalidates these behaviors using the mock server implementation intests/fake-codex-fixture.mjs.
Frequently Asked Questions
How does the Codex plugin detect if the CLI is installed?
The plugin calls getCodexAvailability, which executes the Codex binary with the --version flag. A successful run returns { available: true, version: "x.x.x" }, while missing binaries or permission errors return { available: false, error: "..." }, allowing applications to distinguish between installation and execution failures.
What information does the authentication status check return?
According to the source code in plugins/codex/scripts/lib/codex.mjs, getCodexAuthStatus returns an object containing loggedIn (boolean), requiresOpenaiAuth (boolean), authMethod (string enum), source (origin string), and detail (human-readable description). This structure supports multiple authentication backends including ChatGPT sessions, API keys, and self-hosted instances.
Where does the plugin handle session expiration?
Session expiration errors are captured in getCodexAuthStatus within plugins/codex/scripts/lib/codex.mjs. These errors populate the detail field of the returned object, which higher-level scripts like codex-companion.mjs surface to users with specific remediation commands such as codex login --device-auth.
Can availability and authentication checks run in parallel?
Yes. While getCodexAuthStatus requires the binary to be present, the reference implementation in codex-companion.mjs demonstrates running getCodexAvailability and getCodexAuthStatus concurrently via Promise.all alongside other system checks, then combining the results into a single ready flag that gates subsequent operations.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →