package runner import ( "amadan.net/oficina/equipo/internal/runner/containment" "amadan.net/oficina/equipo/internal/runner/provider" "context" "fmt" "strings" "testing" ) func TestIdlePreflightStorageBounded(t *testing.T) { j, l, _, p := directFixture(t, "codex") ctx := context.Background() _, err := j.db.Exec("DELETE FROM direct_attempts") mustJournal(t, err) for i := 0; i < 12; i++ { l.AttemptKey = fmt.Sprintf("codex-preflight-%d", i) l.Selection.ConnectionID = "" p.record.AttemptKey = l.AttemptKey p.record.ID = fmt.Sprintf("%032x", i+1) mustJournal(t, j.recordDirectLaunch(ctx, l, p.record)) mustJournal(t, j.AppendFrame(ctx, l.AttemptKey, p.record.ID, 1, []byte(`{"id":1,"result":{}}`))) _, err = j.db.Exec("UPDATE direct_attempts SET contained=1 WHERE attempt=?", l.AttemptKey) mustJournal(t, err) mustJournal(t, j.releaseDirectFrames(ctx)) mustJournal(t, j.prunePreflights(ctx, func(context.Context, containment.Record) error { return nil })) var frames, attempts int mustJournal(t, j.db.QueryRow("SELECT count(*) FROM direct_frames").Scan(&frames)) mustJournal(t, j.db.QueryRow("SELECT count(*) FROM direct_attempts").Scan(&attempts)) if frames != 0 || attempts != 0 { t.Fatalf("idle poll %d retained %d frames and %d attempts", i, frames, attempts) } } } func TestDirectBudgetDrainsWithoutReset(t *testing.T) { j, l, _, p := directFixture(t, "codex") ctx := context.Background() j.maxBytes = 20 if err := j.AppendFrame(ctx, l.AttemptKey, p.ID(), 1, []byte(`{"message":"`+strings.Repeat("x", 30)+`"}`)); err == nil { t.Fatal("overrun accepted") } j.maxBytes = 1024 if err := j.AdmissionAllowed(ctx); err != nil { t.Fatalf("budget overrun latched journal: %v", err) } mustJournal(t, j.AppendFrame(ctx, l.AttemptKey, p.ID(), 1, []byte(`{"ok":true}`))) } func TestDirectPreflightCache(t *testing.T) { r := &Runtime{} calls := 0 probe := func() error { calls++; return nil } for i := 0; i < 12; i++ { mustJournal(t, r.cachedPreflight(context.Background(), "binding", "same", probe)) } if calls != 1 { t.Fatalf("idle polls/claim reran preflight %d times", calls) } for _, key := range []string{"account-changed", "policy-changed", "binary-changed", "home-changed"} { mustJournal(t, r.cachedPreflight(context.Background(), "binding", key, probe)) } if calls != 5 { t.Fatal("changed input did not invalidate cache") } if err := r.cachedPreflight(context.Background(), "binding", "bad", func() error { return provider.ErrPolicy }); err == nil { t.Fatal("failed preflight cached") } mustJournal(t, r.cachedPreflight(context.Background(), "binding", "bad", probe)) if calls != 6 { t.Fatal("failure retained") } } func TestPendingBudgetsDoNotLatch(t *testing.T) { for _, kind := range []string{"event", "direct-event", "tool"} { t.Run(kind, func(t *testing.T) { j, _, s, _ := directFixture(t, "codex") ctx := context.Background() j.maxBytes = 1 var err error switch kind { case "event": err = j.Append(ctx, journalEvent("budget", 1)) case "direct-event": _, err = j.ObserveDirect(ctx, s, provider.Event{Kind: "timeline", SessionID: s.ID, Payload: []byte(`{"message":"hello"}`)}) case "tool": err = j.appendTool(ctx, ToolSubmission{RequestID: "budget"}, "hash") } if err == nil { t.Fatal("overrun accepted") } j.maxBytes = MaxJournalBytes if err = j.AdmissionAllowed(ctx); err != nil { t.Fatal("budget refusal latched storage", err) } }) } }