| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | "golang.org/x/sys/unix" |
| 15 | ) |
| 16 | |
| 17 | const connPerson = "p@example.com" |
| 18 | |
| 19 | // connectorRig is a runner state, a bindings file with one Codex |
| 20 | // connection of the person's (codex-1, its home under the accounts |
| 21 | // root) and a connector over them that a test can restart. |
| 22 | type connectorRig struct { |
| 23 | t *testing.T |
| 24 | state *State |
| 25 | bindings, root string |
| 26 | home string |
| 27 | conns *Conns |
| 28 | cfg runner.Config |
| 29 | } |
| 30 | |
| 31 | func newConnectorRig(t *testing.T) *connectorRig { |
| 32 | dir := t.TempDir() |
| 33 | r := &connectorRig{t: t, root: filepath.Join(dir, "accounts"), bindings: filepath.Join(dir, "config", "bindings.json")} |
| 34 | for _, d := range []string{r.root, filepath.Dir(r.bindings), filepath.Join(dir, "state")} { |
| 35 | if err := os.MkdirAll(d, 0700); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | } |
| 39 | var err error |
| 40 | if r.state, err = OpenState(context.Background(), filepath.Join(dir, "state")); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | t.Cleanup(func() { r.state.Close() }) |
| 44 | r.cfg = runner.Config{AccountBindingsPath: r.bindings, CodexPath: "/bin/true", ClaudePath: "/bin/true"} |
| 45 | r.conns = &Conns{Runner: r.cfg, State: r.state} |
| 46 | r.add() |
| 47 | return r |
| 48 | } |
| 49 | |
| 50 | // add (again) binds codex-1 in a fresh home. |
| 51 | func (r *connectorRig) add() { |
| 52 | r.home = filepath.Join(r.root, "codex-1") |
| 53 | if err := os.MkdirAll(r.home, 0700); err != nil { |
| 54 | r.t.Fatal(err) |
| 55 | } |
| 56 | identity := map[string]string{"account_id": "a@example.com", "home_id": "h-" + time.Now().Format("150405.000000000"), "provider_version": "1"} |
| 57 | b := []map[string]any{{"id": "codex-1", "local_id": "codex-1", "label": "codex-1", "person_id": connPerson, "provider": "codex", "family": "gpt", "model": "gpt-6-astra", "account_id": "a@example.com", "provider_home": r.home, "kind": "subscription", "expected_identity": identity}} |
| 58 | raw, _ := json.Marshal(b) |
| 59 | if err := os.WriteFile(r.bindings, raw, 0600); err != nil { |
| 60 | r.t.Fatal(err) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // connector is the runner's connector; crashed: its background cleanups |
| 65 | // never run, as if the process stopped right after the frame. |
| 66 | func (r *connectorRig) connector(crashed bool) *Connector { |
| 67 | c := &Connector{Runner: r.cfg, State: r.state, Conns: r.conns, Root: r.root, Owners: []string{connPerson}} |
| 68 | c.spawn = func(f func()) { f() } |
| 69 | if crashed { |
| 70 | c.spawn = func(func()) {} |
| 71 | } |
| 72 | if err := c.Start(context.Background()); err != nil { |
| 73 | r.t.Fatal(err) |
| 74 | } |
| 75 | return c |
| 76 | } |
| 77 | |
| 78 | func (r *connectorRig) bound() bool { |
| 79 | _, ok := r.conns.Binding("codex-1") |
| 80 | return ok |
| 81 | } |
| 82 | |
| 83 | func (r *connectorRig) signedOut() int { |
| 84 | frames, _, err := r.state.Unacked(context.Background(), 0) |
| 85 | if err != nil { |
| 86 | r.t.Fatal(err) |
| 87 | } |
| 88 | n := 0 |
| 89 | for _, f := range frames { |
| 90 | var v wire.ConnectionReport |
| 91 | if f.Type == wire.FrameConnection && json.Unmarshal(f.Data, &v) == nil && v.State == wire.ConnSignedOut { |
| 92 | n++ |
| 93 | } |
| 94 | } |
| 95 | return n |
| 96 | } |
| 97 | |
| 98 | func (r *connectorRig) attempt(id, state string, cancelled int) { |
| 99 | if _, err := r.state.db.Exec("INSERT INTO connect_attempts(id, person, provider, state, local_id, cancelled, created_at) VALUES(?,?,?,?,?,?,?)", id, connPerson, "codex", state, "codex-1", cancelled, now()); err != nil { |
| 100 | r.t.Fatal(err) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Astra round 1, finding 4: a sign-out whose frame was consumed is done |
| 105 | // even when the runner stops before the cleanup ran: the restarted |
| 106 | // connector owes it, does it once, and reports signed_out. |
| 107 | func TestConnectorSignOutSurvivesACrash(t *testing.T) { |
| 108 | r := newConnectorRig(t) |
| 109 | c := r.connector(true) |
| 110 | if err := c.Disconnect(context.Background(), "f1", wire.DisconnectCommand{Connection: "cn_1", Person: connPerson, LocalID: "codex-1"}); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | if !r.bound() { |
| 114 | t.Fatal("the crashed connector signed out anyway") |
| 115 | } |
| 116 | r.connector(false) |
| 117 | if r.bound() || r.signedOut() != 1 { |
| 118 | t.Fatalf("after the restart: bound %v, %d signed_out reports", r.bound(), r.signedOut()) |
| 119 | } |
| 120 | if _, err := os.Stat(r.home); !os.IsNotExist(err) { |
| 121 | t.Fatalf("the home stayed: %v", err) |
| 122 | } |
| 123 | // The same frame again changes nothing; a later start owes nothing. |
| 124 | again := r.connector(false) |
| 125 | if err := again.Disconnect(context.Background(), "f1", wire.DisconnectCommand{Connection: "cn_1", Person: connPerson, LocalID: "codex-1"}); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if r.signedOut() != 1 { |
| 129 | t.Fatalf("%d signed_out reports", r.signedOut()) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Finding 4, cancel: a cancel that finds the attempt's connection saved |
| 134 | // owes its sign-out from the transaction that consumes the frame; a |
| 135 | // connection saved and then found cancelled is owed at startup. Once |
| 136 | // done, the attempt's local id, reused by a later connection, is never |
| 137 | // signed out again. |
| 138 | func TestConnectorCancelSurvivesACrash(t *testing.T) { |
| 139 | r := newConnectorRig(t) |
| 140 | r.attempt("cn_2", "done", 0) |
| 141 | c := r.connector(true) |
| 142 | if err := c.Cancel(context.Background(), "f2", wire.CancelConnectCommand{Connection: "cn_2", Person: connPerson}); err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | if !r.bound() { |
| 146 | t.Fatal("the crashed connector signed out anyway") |
| 147 | } |
| 148 | r.connector(false) |
| 149 | if r.bound() { |
| 150 | t.Fatal("the cancelled connection stood after the restart") |
| 151 | } |
| 152 | // Saved, then the cancel seen, then the stop: owed at startup. |
| 153 | r.add() |
| 154 | r.attempt("cn_3", "done", 1) |
| 155 | r.connector(false) |
| 156 | if r.bound() { |
| 157 | t.Fatal("a connection cancelled as it was saved stood after the restart") |
| 158 | } |
| 159 | // codex-1 again, a new connection: no restart signs it out. |
| 160 | r.add() |
| 161 | r.connector(false) |
| 162 | if !r.bound() { |
| 163 | t.Fatal("a later connection with the same local id was signed out") |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Finding 3: the attempt holds its home's lock while it signs in; a |
| 168 | // cancel that lands as the connection is saved signs it out without |
| 169 | // waiting on that lock. |
| 170 | func TestConnectorLateCancelLetsGoOfTheLock(t *testing.T) { |
| 171 | r := newConnectorRig(t) |
| 172 | r.attempt("cn_4", "done", 1) |
| 173 | c := r.connector(true) |
| 174 | lock, err := os.Open(r.home) |
| 175 | if err != nil { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | if err = unix.Flock(int(lock.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | done := make(chan struct{}) |
| 182 | go func() { |
| 183 | c.standOrGo(wire.ConnectCommand{Connection: "cn_4", Person: connPerson, Provider: "codex"}, "codex-1", lock) |
| 184 | close(done) |
| 185 | }() |
| 186 | select { |
| 187 | case <-done: |
| 188 | case <-time.After(10 * time.Second): |
| 189 | t.Fatal("the late cancel waits on the attempt's own lock") |
| 190 | } |
| 191 | if r.bound() { |
| 192 | t.Fatal("the cancelled connection stood") |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Astra round 2: a stop after the binding went and before the home did |
| 197 | // leaves the home in the cleanup's record; the restarted connector |
| 198 | // deletes it and only then reports signed_out. |
| 199 | func TestConnectorSignOutFinishesTheHomeAfterACrash(t *testing.T) { |
| 200 | r := newConnectorRig(t) |
| 201 | if err := r.state.tx(context.Background(), func(tx *sql.Tx) error { |
| 202 | return owe(tx, cleanup{connection: "cn_5", person: connPerson, local: "codex-1", report: true}) |
| 203 | }); err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | if _, err := r.state.db.Exec("UPDATE connect_cleanups SET home=?, provider='codex' WHERE connection='cn_5'", r.home); err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | if err := os.WriteFile(r.bindings, []byte("[]"), 0600); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | r.connector(false) |
| 213 | if _, err := os.Stat(r.home); !os.IsNotExist(err) || r.signedOut() != 1 { |
| 214 | t.Fatalf("home left %v, %d signed_out reports", err, r.signedOut()) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Round 2: a home that cannot be deleted is never reported signed out; |
| 219 | // the cleanup stays owed and finishes once it can. |
| 220 | func TestConnectorNeverReportsSignedOutWhileTheHomeExists(t *testing.T) { |
| 221 | r := newConnectorRig(t) |
| 222 | c := r.connector(true) |
| 223 | if err := c.Disconnect(context.Background(), "f6", wire.DisconnectCommand{Connection: "cn_6", Person: connPerson, LocalID: "codex-1"}); err != nil { |
| 224 | t.Fatal(err) |
| 225 | } |
| 226 | if err := os.Chmod(r.root, 0500); err != nil { |
| 227 | t.Fatal(err) |
| 228 | } |
| 229 | r.connector(false) |
| 230 | if err := os.Chmod(r.root, 0700); err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | if _, err := os.Stat(r.home); err != nil || r.signedOut() != 0 { |
| 234 | t.Fatalf("home %v, %d signed_out reports while it exists", err, r.signedOut()) |
| 235 | } |
| 236 | r.connector(false) |
| 237 | if _, err := os.Stat(r.home); !os.IsNotExist(err) || r.signedOut() != 1 || r.bound() { |
| 238 | t.Fatalf("home %v, %d reports, bound %v", err, r.signedOut(), r.bound()) |
| 239 | } |
| 240 | } |
| 241 | |