oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "log/slog"
10 "os"
11 "path/filepath"
12 "strconv"
13 "strings"
14 "testing"
15
16 "amadan.net/oficina/equipo/internal/runner"
17 "amadan.net/oficina/equipo/internal/runner/provider"
18 "amadan.net/oficina/equipo/internal/runner/provider/codex"
19 "amadan.net/oficina/equipo/internal/wire"
20)
21
22// codexStartFixture is a Codex source home and a coordinator on it whose
23// start fails before any process: refresh is the test's.
24func codexStartFixture(t *testing.T, stateDir string, refresh func(context.Context, provider.Binding) (provider.Identity, error)) (Coordinator, *bytes.Buffer) {
25 t.Helper()
26 root := t.TempDir()
27 home := filepath.Join(root, "codex-1")
28 id := provider.Identity{AccountID: "person@example.com", HomeID: "home-1", ProviderVersion: codex.Version}
29 raw, _ := json.Marshal(id)
30 if os.Mkdir(home, 0700) != nil || os.WriteFile(filepath.Join(home, provider.IdentityFile), raw, 0600) != nil || os.WriteFile(filepath.Join(home, "auth.json"), []byte(`{"tokens":{}}`), 0600) != nil {
31 t.Fatal("home")
32 }
33 db := filepath.Join(root, "state")
34 if stateDir == "" {
35 stateDir = db
36 }
37 scratch := filepath.Join(root, "scratch")
38 for _, d := range []string{db, scratch} {
39 if err := os.MkdirAll(d, 0700); err != nil {
40 t.Fatal(err)
41 }
42 }
43 state, err := OpenState(context.Background(), db)
44 if err != nil {
45 t.Fatal(err)
46 }
47 t.Cleanup(func() { state.Close() })
48 var logs bytes.Buffer
49 o := ClaudeOptions{Runner: runner.Config{StateDir: stateDir, ScratchRoot: scratch}, State: state, SessionRoot: filepath.Join(root, "sessions"), Refresh: refresh, Logger: slog.New(slog.NewTextHandler(&logs, nil))}
50 b := runner.AccountBinding{ID: "codex-1", LocalID: "codex-1", Provider: "codex", Family: "gpt", Model: "gpt-test", PersonID: "p@example.com", ProviderHome: home, AccountID: id.AccountID, ExpectedIdentity: id, Kind: "subscription"}
51 c, err := OpenCodex(o, CodexOptions{}, "p@example.com", b)
52 if err != nil {
53 t.Fatal(err)
54 }
55 t.Cleanup(c.Close)
56 return c, &logs
57}
58
59func codexTurn(c Coordinator) TurnResult {
60 return c.Turn(context.Background(), TurnInput{Person: "p@example.com", Lane: wire.LaneCoordinator, TurnID: "t1", Attempt: 1, Text: "hello"}, func(string) {})
61}
62
63// A failed start says where it failed, as a fixed step name beside the
64// sanitized error (2026-11-22: staging logged only *errors.errorString).
65func TestCodexStartLogsItsStep(t *testing.T) {
66 ok := func(_ context.Context, b provider.Binding) (provider.Identity, error) { return b.Expected, nil }
67 cases := []struct {
68 name, want, reason string
69 stateDir string
70 refresh func(context.Context, provider.Binding) (provider.Identity, error)
71 }{
72 {name: "refresh refused", want: `err="refresh: provider_identity_mismatch"`, reason: "account_unavailable",
73 refresh: func(context.Context, provider.Binding) (provider.Identity, error) {
74 return provider.Identity{}, provider.ErrIdentity
75 }},
76 {name: "sign in again", want: `err="refresh: account.sign_in_again"`, reason: "sign_in_again",
77 refresh: func(context.Context, provider.Binding) (provider.Identity, error) {
78 return provider.Identity{}, provider.ErrLoginRequired
79 }},
80 // Any other error is still only its type, now with its step.
81 {name: "tool socket", want: `err="tool_socket: *fs.PathError"`, reason: "account_unavailable", refresh: ok,
82 stateDir: filepath.Join(t.TempDir(), "missing")},
83 }
84 for _, tc := range cases {
85 t.Run(tc.name, func(t *testing.T) {
86 c, logs := codexStartFixture(t, tc.stateDir, tc.refresh)
87 res := codexTurn(c)
88 if res.Reason != tc.reason {
89 t.Fatalf("reason %q, want %q", res.Reason, tc.reason)
90 }
91 if !strings.Contains(logs.String(), `msg="codex coordinator start" `+tc.want) {
92 t.Fatalf("log %q, want %s", logs.String(), tc.want)
93 }
94 })
95 }
96}
97
98var errSecretWords = errors.New("secret words /home/someone")
99
100// A step name never carries the error's own words.
101func TestSanitizeStepKeepsOnlyTheCode(t *testing.T) {
102 err := atStep("tool_socket", errSecretWords)
103 if got := sanitize(err); got != "tool_socket: *errors.errorString" {
104 t.Fatalf("logged %q", got)
105 }
106 if got := sanitize(atStep("copy", atStep("refresh", provider.ErrIdentity))); got != "refresh: provider_identity_mismatch" {
107 t.Fatalf("logged %q", got)
108 }
109}
110
111const secretCanary = "secret-canary-sk-0000"
112
113// secretErrors carry the canary in their own text: wrapped around a
114// cancellation, a deadline or a provider code, and joined.
115func secretErrors() []struct {
116 err error
117 want string
118} {
119 return []struct {
120 err error
121 want string
122 }{
123 {fmt.Errorf("%s: %w", secretCanary, context.Canceled), "cancelled"},
124 {fmt.Errorf("%s: %w", secretCanary, context.DeadlineExceeded), "deadline"},
125 {errors.Join(errors.New(secretCanary), context.Canceled), "*errors.errorString; cancelled"},
126 {errors.Join(fmt.Errorf("%s: %w", secretCanary, context.DeadlineExceeded), errors.New(secretCanary)), "deadline; *errors.errorString"},
127 {fmt.Errorf("%s: %w", secretCanary, errors.Join(errors.New(secretCanary), context.Canceled)), "*errors.errorString; cancelled"},
128 }
129}
130
131// The sanitizer emits fixed codes and types only, never an error's own
132// text, wrapped or joined (Astra review of bf4c4ff4).
133func TestSanitizeNeverPrintsErrorText(t *testing.T) {
134 for _, tc := range secretErrors() {
135 got := sanitize(atStep("refresh", tc.err))
136 if strings.Contains(got, secretCanary) || got != "refresh: "+tc.want {
137 t.Errorf("sanitize(%q) = %q, want %q", tc.err, got, "refresh: "+tc.want)
138 }
139 }
140}
141
142// Both coordinators' start logs share the sanitizer: a secret in a
143// refresh error never reaches either log.
144func TestStartLogsNeverPrintErrorText(t *testing.T) {
145 for _, tc := range secretErrors() {
146 refresh := func(context.Context, provider.Binding) (provider.Identity, error) { return provider.Identity{}, tc.err }
147 c, logs := codexStartFixture(t, "", refresh)
148 codexTurn(c)
149 if strings.Contains(logs.String(), secretCanary) || !strings.Contains(logs.String(), `msg="codex coordinator start" err=`+slogValue("refresh: "+tc.want)) {
150 t.Errorf("codex log %q, want refresh: %s", logs.String(), tc.want)
151 }
152 c, logs = claudeStartFixture(t, refresh)
153 codexTurn(c)
154 if strings.Contains(logs.String(), secretCanary) || !strings.Contains(logs.String(), `msg="coordinator start" err=`+slogValue(tc.want)) {
155 t.Errorf("claude log %q, want %s", logs.String(), tc.want)
156 }
157 }
158}
159
160// slogValue is v as the text handler writes it: quoted when it must be.
161func slogValue(v string) string {
162 if strings.ContainsAny(v, " ;\"=") {
163 return strconv.Quote(v)
164 }
165 return v
166}
167
168// claudeStartFixture is a Claude source home and a coordinator on it whose
169// start fails at the test's refresh.
170func claudeStartFixture(t *testing.T, refresh func(context.Context, provider.Binding) (provider.Identity, error)) (Coordinator, *bytes.Buffer) {
171 t.Helper()
172 root := t.TempDir()
173 home := filepath.Join(root, "claude-a")
174 id := provider.Identity{AccountID: "person@example.com", HomeID: "home-a", ProviderVersion: provider.ClaudeVersion}
175 raw, _ := json.Marshal(id)
176 if os.Mkdir(home, 0700) != nil || os.WriteFile(filepath.Join(home, provider.IdentityFile), raw, 0600) != nil || os.WriteFile(filepath.Join(home, ".credentials.json"), []byte(`{}`), 0600) != nil {
177 t.Fatal("home")
178 }
179 db, scratch := filepath.Join(root, "state"), filepath.Join(root, "scratch")
180 for _, d := range []string{db, scratch} {
181 if err := os.MkdirAll(d, 0700); err != nil {
182 t.Fatal(err)
183 }
184 }
185 state, err := OpenState(context.Background(), db)
186 if err != nil {
187 t.Fatal(err)
188 }
189 t.Cleanup(func() { state.Close() })
190 var logs bytes.Buffer
191 o := ClaudeOptions{Runner: runner.Config{StateDir: db, ScratchRoot: scratch}, State: state, SessionRoot: filepath.Join(root, "sessions"), Refresh: refresh, Logger: slog.New(slog.NewTextHandler(&logs, nil))}
192 b := runner.AccountBinding{ID: "claude-a", LocalID: "claude-a", Provider: "claude", Family: "claude", Model: "claude-test", PersonID: "p@example.com", ProviderHome: home, AccountID: id.AccountID, ExpectedIdentity: id, Kind: "subscription"}
193 c, err := OpenClaude(o, "p@example.com", b)
194 if err != nil {
195 t.Fatal(err)
196 }
197 t.Cleanup(c.Close)
198 return c, &logs
199}
200