Skip to content

Add conditional architecture detection for CODEX_ARCH environment variable#595

Closed
aeltorio wants to merge 2 commits into
icebear0828:devfrom
sctg-development:auto-arch-detection
Closed

Add conditional architecture detection for CODEX_ARCH environment variable#595
aeltorio wants to merge 2 commits into
icebear0828:devfrom
sctg-development:auto-arch-detection

Conversation

@aeltorio
Copy link
Copy Markdown
Contributor

Summary

This PR adds architecture detection logic for the CODEX_ARCH environment variable in the Docker entrypoint script, but only when the variable is not already set or is empty. This change provides better flexibility for container deployments while maintaining backward compatibility. The main objective is for using the Docker image in multi-architecture Kubernetes environments.

Changes Made

  • File Modified: docker-entrypoint.sh
  • Lines Added: 14 lines of conditional architecture detection logic

Technical Details

Before

The script had no architecture detection, which could cause issues in environments where architecture-specific behavior was needed.

After

The script now includes conditional architecture detection that:

  1. Checks if CODEX_ARCH is already set: Uses [ -z "${CODEX_ARCH}" ] to test if the variable is empty or unset
  2. Auto-detects architecture only when needed: If CODEX_ARCH is not set, uses uname -p to detect the system architecture
  3. Maps common architectures: Converts aarch64arm64 and x86_64x64 for consistency
  4. Preserves existing values: If CODEX_ARCH is already set to a non-empty value, the script leaves it unchanged

Architecture Mapping

  • aarch64arm64
  • x86_64x64
  • Other architectures → raw uname -p output

Benefits

  1. Flexibility: Allows users to override the architecture detection by setting CODEX_ARCH in their environment
  2. Backward Compatibility: Existing deployments without CODEX_ARCH set will continue to work with auto-detection
  3. Consistency: Standardizes architecture names (arm64/x64) for easier handling in downstream code
  4. Debian Bookworm Support: Specifically tested and working on Debian Bookworm systems

Testing

The implementation has been tested with three scenarios:

  1. Unset variable: Auto-detects and sets CODEX_ARCH appropriately
  2. Preset variable: Preserves the existing CODEX_ARCH value
  3. Empty string: Treats empty string as unset and auto-detects

Use Cases

This change is particularly useful for:

  • Multi-architecture Docker builds
  • Multi-architecture Kubernetes environments
  • CI/CD pipelines that need to override architecture detection
  • Development environments with specific architecture requirements
  • Cloud deployments where architecture needs to be explicitly controlled

Impact

  • Breaking Changes: None - fully backward compatible
  • Performance: Negligible - adds one conditional check at startup
  • Dependencies: None - uses standard shell commands (uname, export)

The change follows the principle of "convention over configuration" while still allowing explicit configuration when needed.

Copy link
Copy Markdown
Owner

@icebear0828 icebear0828 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uname -p 在 Alpine 上返回 unknown,建议改用 uname -m(Linux/macOS 上一致返回 aarch64/x86_64)。

-  UNAME_ARCH=$(uname -p)
+  UNAME_ARCH=$(uname -m)

其余 LGTM。

Copy link
Copy Markdown
Owner

@icebear0828 icebear0828 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! A few things need to be addressed before merge.

Bug: else branch passes raw uname -m output into CODEX_ARCH (docker-entrypoint.sh)

On any arch that isn't aarch64 or x86_64 (e.g. armv7l, riscv64), CODEX_ARCH gets set to the raw kernel string. This value ends up in the User-Agent header sent to chatgpt.com, which may cause fingerprint mismatches or silent request failures.

Please either fail fast with a clear error on unrecognized architectures, or drop to a known safe default:

else
  echo "[Error] Unsupported architecture: $UNAME_ARCH (expected aarch64 or x86_64)" >&2
  exit 1
fi

Incorrect flag in PR description

The description says uname -p, but the code uses uname -m. These are different — uname -p returns "unknown" on most Linux systems. Please correct the description.


Clarify what CODEX_ARCH actually controls

The PR description implies this affects native addon loading, but native/index.js uses process.arch (Node.js built-in), not CODEX_ARCH. In practice CODEX_ARCH only affects the User-Agent string and the /health endpoint output. Please update the description to reflect this so reviewers aren't misled.


No tests

Please add automated tests (e.g. bats) covering the three mapping scenarios: aarch64→arm64, x86_64→x64, and the unrecognized-arch path (whichever behavior you choose above).

icebear0828 added a commit that referenced this pull request May 27, 2026
Maps aarch64→arm64, x86_64→x64; passes unknown arches through.
Respects pre-set CODEX_ARCH. Includes 5 shell-level tests using
mock uname via PATH injection.

Addresses #595.
@icebear0828
Copy link
Copy Markdown
Owner

Thanks! The arch auto-detection + 5 shell-level tests have been landed on dev (f18a879). Covers aarch64→arm64, x86_64→x64, unknown passthrough, preset CODEX_ARCH, and empty CODEX_ARCH.

icebear0828 added a commit that referenced this pull request May 27, 2026
- CHANGELOG: CORS allowlist, Docker arch detection, OS cert store, output backfill
- README/README_EN: add @aeltorio, @williamjameshandley, @FlavienKlr to contributors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants