The arg0 trickThe arg0 trick
While reading the Codex source code, I noticed a lot of arg0 dispatch near the program entry point. Turns out this is a classic Unix/Linux programming trick.
The idea is simple: ship one executable, then create multiple links with different names pointing to it. When the program is started through different names, the kernel still loads the same executable, but the program may receive a different argv[0]. The program can then branch on argv[0] and run different internal logic.
Classic tools like Vim, gzip, and BusyBox have used this pattern.
For example, on my Ubuntu machine, vim, vi, view, and vimdiff all eventually point to vim.basic:
/usr/bin/vi
-> /etc/alternatives/vi
-> /usr/bin/vim.basic
/usr/bin/view
-> /etc/alternatives/view
-> /usr/bin/vim.basic
/usr/bin/vimdiff
-> /etc/alternatives/vimdiff
-> /usr/bin/vim.basic/etc/alternatives is the Debian/Ubuntu alternatives system, so there are two layers of symlinks here.
Codex does something a bit more dynamic.
At runtime, Codex creates a temporary directory like:
~/.codex/tmp/arg0/codex-arg0XXXXInside that directory, it creates several links pointing back to itself, for example:
apply_patch -> codex
applypatch -> codex
codex-linux-sandbox -> codex
codex-execve-wrapper -> codexThen Codex prepends this temporary arg0 directory to PATH.
After that, when Codex or its child processes run commands like apply_patch or codex-linux-sandbox, the shell resolves them to these temporary symlinks first. Execution re-enters the same Codex binary, but with a different argv[0], so Codex can dispatch to the corresponding internal code path.
Small trick, very Unix. One binary, many personalities.
Relevant Vim documentation: https://github.com/vim/vim/blob/1ffc1aaa4317979b7159123b19f3af2bd65f9bea/runtime/doc/starting.txt#L94-L122