Skip to content
Open
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
72 changes: 71 additions & 1 deletion cmd/agentop/cmd_codex.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ func runCodex(args []string, stdout, stderr io.Writer) int {
if *envPath == "" {
*envPath = defaultCodexEnvPath(home)
}
// Absolute, so enable's record names the file from any directory. A relative --env,
// or a relative CODEX_HOME, is relative to this one, and uninstall reads the record
// from wherever it runs: opened from there, the path names some other file (#1781
// review). configure claude-code makes --settings absolute for the same reason.
if abs, err := filepath.Abs(*envPath); err == nil {
*envPath = abs
}
if *cortexCfgPath == "" {
*cortexCfgPath = filepath.Join(home, cortexCfgRel)
}
Expand Down Expand Up @@ -228,6 +235,32 @@ func codexWanted(cortexCfgPath string) (map[string]string, error) {
return nil, fmt.Errorf("%s has no tls_bridge.ca_dir, so Codex has no CA to trust;\n"+
" requests would fail certificate verification. Enable the TLS bridge first.", cortexCfgPath)
}
return codexValuesFrom(want), nil
}

// codexValues is Cortex's values for the five managed keys, for telling them from the
// user's: what codexWanted gives, without enable's refusals. A bridge turned off since
// enable changes nothing about the values enable wrote, and without them codexIsOurs
// falls back to their shape, which a custom forward_proxy_addr or ca_dir does not have:
// disable and uninstall would then call Cortex's own values the user's (#1783 review).
// OpenCode reads its values the same way. nil when the config cannot be read; a value
// the config does not give is left out, so it matches nothing.
func codexValues(cortexCfgPath string) map[string]string {
want, _, err := wantedFromConfig(cortexCfgPath)
if err != nil {
return nil
}
out := map[string]string{}
for k, v := range codexValuesFrom(want) {
if v != "" {
out[k] = v
}
}
return out
}

// codexValuesFrom puts the values wantedFromConfig derived under Codex's five keys.
func codexValuesFrom(want map[string]string) map[string]string {
proxy := want[envProxy]
return map[string]string{
"HTTP_PROXY": proxy,
Expand All @@ -238,7 +271,7 @@ func codexWanted(cortexCfgPath string) (map[string]string, error) {
// vars resolve to — not the bare ca.crt envCACerts names. See the comment
// at the top of this file for why SSL_CERT_FILE and not Codex's own name.
envSSLCert: want[envSSLCert],
}, nil
}
}

// codexEnvLineRe matches a dotenv assignment as DOTENVY parses it, which is what
Expand Down Expand Up @@ -321,6 +354,43 @@ func (f *codexEnvFile) get(key string) (string, bool) {
return m[2], true
}

// all returns every value the file defines key as, in file order: one for a key defined
// once, more for a duplicate, of which Codex uses the last (dotenvy is last-wins). get
// gives only the first.
func (f *codexEnvFile) all(key string) []string {
var vals []string
for _, line := range f.lines {
if m := codexEnvLineRe.FindStringSubmatch(line); m != nil && m[1] == key {
vals = append(vals, m[2])
}
}
return vals
}

// codexEnvValue is a dotenv value as Codex's loader, dotenvy, reads it, for the shapes a
// path or a proxy URL takes: one pair of single or double quotes stripped, or an unquoted
// value as it stands, surrounding space trimmed. ok is false for a value it might read
// otherwise, one with an escape, a $ substitution, a stray quote or a # that may start a
// comment, so a caller can take the safe side instead of misjudging it. get and all give
// values raw, quotes and all, as the file holds them (#1783 review).
func codexEnvValue(raw string) (string, bool) {
v := strings.TrimSpace(raw)
if n := len(v); n >= 2 && (v[0] == '"' || v[0] == '\'') && v[n-1] == v[0] {
inner, unsafe := v[1:n-1], `"\$`
if v[0] == '\'' {
unsafe = `'` // single quotes are literal to dotenvy
}
if strings.ContainsAny(inner, unsafe) {
return "", false
}
return inner, true
}
if strings.ContainsAny(v, `"'\$#`) {
return "", false
}
return v, true
}

// ambiguous names every key in keys defined more than once in the file.
func (f *codexEnvFile) ambiguous(keys []string) []string {
var out []string
Expand Down
71 changes: 71 additions & 0 deletions cmd/agentop/cmd_codex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,74 @@ func TestConfigureUsage_CodexPersists(t *testing.T) {
}
}
}

// enable records its dotenv file as an absolute path, so the record names the file from
// any directory: a relative --env, or a relative CODEX_HOME, is relative to the one enable
// runs in, and uninstall reads the record from wherever it runs (#1781 review).
func TestCodexEnable_RecordsAnAbsolutePath(t *testing.T) {
for name, args := range map[string][]string{
"--env .env": {"enable", "--yes", "--env", ".env"},
"relative CODEX_HOME": {"enable", "--yes"},
} {
t.Run(name, func(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv(codexHomeEnv, "")
cfg := filepath.Join(home, cortexCfgRel)
if err := os.MkdirAll(filepath.Dir(cfg), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cfg, []byte(strings.Replace(cortexCfg, "CADIR", filepath.Join(home, ".cortex", "ca"), 1)), 0o600); err != nil {
t.Fatal(err)
}
dir := filepath.Join(home, "codexhome")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
t.Chdir(home)
want := filepath.Join(dir, ".env")
if name == "--env .env" {
t.Chdir(dir)
} else {
t.Setenv(codexHomeEnv, "codexhome")
}
stubCodexDaemon(t, dir, &fakeCodexDaemon{})
var out, errb bytes.Buffer
if code := runCodex(args, &out, &errb); code != 0 {
t.Fatalf("enable exit %d:\n%s%s", code, out.String(), errb.String())
}
st, err := readState(filepath.Join(home, codexStateRel))
if err != nil || st == nil {
t.Fatalf("enable's record: %v, %v", st, err)
}
if !filepath.IsAbs(st.Settings) || !samePath(st.Settings, want) {
t.Errorf("the record names %q, want the absolute %s", st.Settings, want)
}
})
}
}

// codexEnvValue reads a value as dotenvy does for a path or a proxy URL, and says when it
// cannot be sure, so its callers can take the safe side.
func TestCodexEnvValue(t *testing.T) {
for _, tc := range []struct {
raw, want string
ok bool
}{
{"/Users/x/.cortex/ca/bundle.crt", "/Users/x/.cortex/ca/bundle.crt", true},
{`"/Users/x/.cortex/my-corp.pem"`, "/Users/x/.cortex/my-corp.pem", true},
{`'/Users/x/.cortex/my-corp.pem'`, "/Users/x/.cortex/my-corp.pem", true},
{` "http://127.0.0.1:47600" `, "http://127.0.0.1:47600", true},
{`'$HOME/ca.pem'`, "$HOME/ca.pem", true}, // single quotes are literal
{`"$HOME/ca.pem"`, "", false}, // substituted
{`"a\"b"`, "", false}, // an escape
{`$HOME/ca.pem`, "", false},
{`/a/b # a comment`, "", false},
{`"/a/b`, "", false}, // unmatched
} {
got, ok := codexEnvValue(tc.raw)
if got != tc.want || ok != tc.ok {
t.Errorf("codexEnvValue(%q) = %q, %v, want %q, %v", tc.raw, got, ok, tc.want, tc.ok)
}
}
}
Loading
Loading