mirror of
https://github.com/Untrivial-ai/agent-orchestrator.git
synced 2026-11-23 16:21:31 +08:00
fix(codex): align account login executable with sessions (#7577)
This commit is contained in:
1 parent
bf71e3ffec
commit
ca08ac681e
5 files changed
+152
-3
No files matched your search
@@ -23,7 +23,7 @@ func (m *codexAccountManager) openLoginTerminal(ctx context.Context, targetAccou
|
||||
return CodexAccountLoginTerminalStart{}, err
|
||||
}
|
||||
defer release()
|
||||
if m.terminal == nil || m.executable == nil {
|
||||
if m.terminal == nil || m.executable == nil || m.resolveBinary == nil {
|
||||
return CodexAccountLoginTerminalStart{}, apierr.Unavailable("CODEX_LOGIN_TERMINAL_UNAVAILABLE", "Codex login terminal is unavailable")
|
||||
}
|
||||
targetAccountID = strings.TrimSpace(targetAccountID)
|
||||
@@ -80,11 +80,18 @@ func (m *codexAccountManager) openLoginTerminal(ctx context.Context, targetAccou
|
||||
m.clearLoginReservation(id)
|
||||
return CodexAccountLoginTerminalStart{}, apierr.Unavailable("CODEX_LOGIN_TERMINAL_UNAVAILABLE", "Codex login terminal is unavailable")
|
||||
}
|
||||
// Keep account login on the session resolver; PATH may select another install.
|
||||
codex, err := m.resolveBinary(ctx)
|
||||
if err != nil || strings.TrimSpace(codex) == "" {
|
||||
_ = os.RemoveAll(pendingDir)
|
||||
m.clearLoginReservation(id)
|
||||
return CodexAccountLoginTerminalStart{}, apierr.Unavailable("CODEX_LOGIN_TERMINAL_UNAVAILABLE", "Codex executable could not be resolved. Check the Codex installation and try again.")
|
||||
}
|
||||
title := "Add Codex account"
|
||||
if targetAccountID != "" {
|
||||
title = "Sign in to Codex account"
|
||||
}
|
||||
terminal, err := m.terminal.OpenCommandTerminal(ctx, shellterm.OpenCommandTerminalInput{Argv: []string{executable, "codex-login"}, Env: map[string]string{"CODEX_HOME": home}, WorkingDir: home, Title: title})
|
||||
terminal, err := m.terminal.OpenCommandTerminal(ctx, shellterm.OpenCommandTerminalInput{Argv: []string{executable, "codex-login", "--executable", codex}, Env: map[string]string{"CODEX_HOME": home}, WorkingDir: home, Title: title})
|
||||
if err != nil {
|
||||
_ = os.RemoveAll(pendingDir)
|
||||
m.clearLoginReservation(id)
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/aoagents/agent-orchestrator/backend/internal/adapters/agent/binaryutil"
|
||||
agentregistry "github.com/aoagents/agent-orchestrator/backend/internal/adapters/agent/registry"
|
||||
"github.com/aoagents/agent-orchestrator/backend/internal/domain"
|
||||
"github.com/aoagents/agent-orchestrator/backend/internal/httpd/apierr"
|
||||
"github.com/aoagents/agent-orchestrator/backend/internal/ports"
|
||||
"github.com/aoagents/agent-orchestrator/backend/internal/service/shellterm"
|
||||
)
|
||||
|
||||
func TestAccountLoginUsesSessionCodexInsteadOfPATH(t *testing.T) {
|
||||
managedDir := t.TempDir()
|
||||
pathDir := t.TempDir()
|
||||
name := "codex"
|
||||
if runtime.GOOS == "windows" {
|
||||
name += ".cmd"
|
||||
}
|
||||
managedCodex := filepath.Join(managedDir, name)
|
||||
pathCodex := filepath.Join(pathDir, name)
|
||||
for _, path := range []string{managedCodex, pathCodex} {
|
||||
if err := os.WriteFile(path, []byte("fixture"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
t.Setenv("PATH", pathDir)
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Setenv("PATHEXT", ".CMD")
|
||||
}
|
||||
binaryutil.SetManagedBinDir(managedDir)
|
||||
t.Cleanup(func() { binaryutil.SetManagedBinDir("") })
|
||||
if got, err := exec.LookPath("codex"); err != nil || got != pathCodex {
|
||||
t.Fatalf("PATH fixture = %q, %v; want %q", got, err, pathCodex)
|
||||
}
|
||||
|
||||
for _, reauthenticate := range []bool{false, true} {
|
||||
name := "add account"
|
||||
if reauthenticate {
|
||||
name = "reauthenticate"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
svc := NewWithDeps(Deps{
|
||||
Context: t.Context(), CodexAccountRoot: filepath.Join(root, "accounts"),
|
||||
CodexPendingRoot: filepath.Join(root, "pending"), CodexSwitchStagingRoot: filepath.Join(root, "staging"),
|
||||
CodexGlobalHome: filepath.Join(root, "device"),
|
||||
})
|
||||
selected, err := svc.ResolveAgentBinary(t.Context(), "codex")
|
||||
if err != nil || selected != managedCodex {
|
||||
t.Fatalf("session Codex = %q, %v; want %q", selected, err, managedCodex)
|
||||
}
|
||||
manager := svc.codexAccounts
|
||||
manager.executable = func() (string, error) { return "/ao", nil }
|
||||
terminal := &fakeCodexLoginTerminal{result: shellterm.ShellTerminal{HandleID: "account-login"}}
|
||||
svc.SetCodexAccountLoginTerminalOpener(terminal)
|
||||
accountID := ""
|
||||
if reauthenticate {
|
||||
record := commitTestAccountWithCredential(t, manager.catalog, manager.pendingRoot,
|
||||
"b60a377d-da68-4a61-86f2-f31f04c571f2", testOAuthCredential("provider-account", "old-access"),
|
||||
ports.CodexAccountObservation{Authentication: domain.AgentAuthenticationAuthorized, Method: domain.CodexAuthMethodChatGPT})
|
||||
accountID = record.Snapshot.ID
|
||||
}
|
||||
started, err := manager.openLoginTerminal(t.Context(), accountID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if _, err := manager.cancelLogin(context.Background(), started.Operation.OperationID); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
opened := terminal.opened[0]
|
||||
if want := []string{"/ao", "codex-login", "--executable", managedCodex}; !slices.Equal(opened.Argv, want) {
|
||||
t.Fatalf("account-login command = %q; want %q (PATH contains %q)", opened.Argv, want, pathCodex)
|
||||
}
|
||||
if home := opened.Env["CODEX_HOME"]; home == "" || home != opened.WorkingDir || !pathWithin(manager.pendingRoot, home) {
|
||||
t.Fatalf("account login lost its private credential home: %#v", opened)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountLoginDoesNotFallBackWhenSessionCodexCannotBeResolved(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
path string
|
||||
err error
|
||||
}{
|
||||
{name: "missing executable", err: ports.ErrAgentBinaryNotFound},
|
||||
{name: "empty executable"},
|
||||
{name: "blank executable", path: " \t"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
svc := NewWithDeps(Deps{
|
||||
Context: t.Context(), CodexAccountRoot: filepath.Join(root, "accounts"),
|
||||
CodexPendingRoot: filepath.Join(root, "pending"), CodexSwitchStagingRoot: filepath.Join(root, "staging"),
|
||||
CodexGlobalHome: filepath.Join(root, "device"),
|
||||
})
|
||||
adapter := &readinessTestAgent{resolve: func(context.Context) (string, error) { return tt.path, tt.err }}
|
||||
svc.agents = []agentregistry.HarnessAgent{{Harness: domain.HarnessCodex, Agent: adapter}}
|
||||
manager := svc.codexAccounts
|
||||
manager.executable = func() (string, error) { return "/ao", nil }
|
||||
terminal := &fakeCodexLoginTerminal{result: shellterm.ShellTerminal{HandleID: "account-login"}}
|
||||
svc.SetCodexAccountLoginTerminalOpener(terminal)
|
||||
_, err := manager.openLoginTerminal(t.Context(), "")
|
||||
var apiError *apierr.Error
|
||||
if !errors.As(err, &apiError) || apiError.Code != "CODEX_LOGIN_TERMINAL_UNAVAILABLE" {
|
||||
t.Fatalf("unresolved Codex login error = %v; want CODEX_LOGIN_TERMINAL_UNAVAILABLE", err)
|
||||
}
|
||||
if len(terminal.opened) != 0 || manager.cached().ActiveLogin != nil {
|
||||
t.Fatal("unresolved Codex started a terminal or retained a pending login")
|
||||
}
|
||||
if entries, err := os.ReadDir(manager.pendingRoot); !errors.Is(err, os.ErrNotExist) && (err != nil || len(entries) != 0) {
|
||||
t.Fatalf("unresolved Codex left pending files: %v, %v", entries, err)
|
||||
}
|
||||
|
||||
adapter.resolve = func(context.Context) (string, error) { return "/resolved/codex", nil }
|
||||
started, err := manager.openLoginTerminal(t.Context(), "")
|
||||
if err != nil {
|
||||
t.Fatalf("retry after repairing executable resolution: %v", err)
|
||||
}
|
||||
if _, err := manager.cancelLogin(context.Background(), started.Operation.OperationID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -124,6 +124,7 @@ type codexAccountManager struct {
|
||||
processes chan struct{}
|
||||
mutations chan struct{}
|
||||
executable func() (string, error)
|
||||
resolveBinary func(context.Context) (string, error)
|
||||
terminal codexAccountLoginTerminalService
|
||||
globalHome string
|
||||
pendingRoot string
|
||||
|
||||
@@ -261,6 +261,7 @@ func newTestCodexAccountManager(t *testing.T, factory ports.CodexAccountClientFa
|
||||
filepath.Join(root, "switch-staging"), filepath.Join(root, "device-home"),
|
||||
factory, nil)
|
||||
manager.snapshotRevision = 0
|
||||
manager.resolveBinary = func(context.Context) (string, error) { return "/test/bin/codex", nil }
|
||||
if state != nil {
|
||||
if active, ok := state.testDeviceAccount(); ok && active.AccountID != "" {
|
||||
manager.deviceAccountID = active.AccountID
|
||||
@@ -372,7 +373,7 @@ func TestNativeLoginTerminalUsesOnePrivatePendingHomeAndNoName(t *testing.T) {
|
||||
t.Fatalf("terminal opens = %d", len(terminal.opened))
|
||||
}
|
||||
opened := terminal.opened[0]
|
||||
if !slices.Equal(opened.Argv, []string{"/Applications/AO.app/Contents/MacOS/ao", "codex-login"}) {
|
||||
if !slices.Equal(opened.Argv, []string{"/Applications/AO.app/Contents/MacOS/ao", "codex-login", "--executable", "/test/bin/codex"}) {
|
||||
t.Fatalf("argv = %#v", opened.Argv)
|
||||
}
|
||||
home := opened.Env["CODEX_HOME"]
|
||||
|
||||
@@ -153,6 +153,9 @@ func NewWithDeps(deps Deps) *Service {
|
||||
svc.modelGate = deps.ModelGate
|
||||
if deps.CodexAccountRoot != "" && deps.CodexGlobalHome != "" {
|
||||
svc.codexAccounts = newCodexAccountManager(deps.Context, deps.CodexAccountRoot, deps.CodexPendingRoot, deps.CodexSwitchStagingRoot, deps.CodexGlobalHome, deps.CodexAccounts, deps.Logger, deps.CodexOperationGate)
|
||||
svc.codexAccounts.resolveBinary = func(ctx context.Context) (string, error) {
|
||||
return svc.ResolveAgentBinary(ctx, string(domain.HarnessCodex))
|
||||
}
|
||||
if deps.Clock != nil {
|
||||
svc.codexAccounts.now = deps.Clock
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user