package runner import ( "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "context" "encoding/json" "testing" "time" ) func TestDirectQuestionRenewedLease(t *testing.T) { for _, kind := range []string{"claude", "codex", "ask_person"} { t.Run(kind, func(t *testing.T) { b := directToolFixture(t) if kind == "claude" { j, _, session, _ := directFixture(t, "claude") mustJournal(t, j.BeginTurn(context.Background(), session, directTurn())) mustJournal(t, j.consumeTurn(context.Background(), session.AttemptKey, session.ID, "hello")) b.Journal = j b.Session = session b.Agent.Provider = "claude" b.Agent.AgentID = session.AttemptKey b.Agent.ProviderSessionID = session.ID } deadline := time.Now().Add(20 * time.Millisecond) l := &directAnswerLauncher{} relay := DirectQuestionRelay{Binding: b, Launcher: l, Current: func(context.Context, runnerproto.Question) (time.Time, error) { return deadline, nil }} ask := func(ctx context.Context, q runnerproto.Question) (runnerproto.QuestionAnswer, error) { deadline = time.Now().Add(time.Second) select { case <-ctx.Done(): return runnerproto.QuestionAnswer{}, ctx.Err() case <-time.After(40 * time.Millisecond): } return runnerproto.QuestionAnswer{Behavior: "deny"}, nil } ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() if kind == "ask_person" { _, err := relay.Ask(ctx, "request", json.RawMessage(`{"question":"Which?"}`), ask) mustJournal(t, err) return } raw := json.RawMessage(`{"id":7,"method":"item/commandExecution/requestApproval","params":{"threadId":"session","turnId":"provider-turn","itemId":"item","command":"printf hello","cwd":"/workspace"}}`) if kind == "claude" { raw = json.RawMessage(`{"tool_name":"Write","input":{"file_path":"/outside","content":"x"}}`) } ev := provider.Event{Kind: "request", RequestID: "7", SessionID: b.Session.ID, TurnID: b.TurnID, Payload: raw} mustJournal(t, relay.Handle(ctx, ev, ask)) if len(l.answers) != 1 { t.Fatal("renewed answer not delivered") } }) } } func TestDirectAskRetriesAndCadence(t *testing.T) { b := directToolFixture(t) s, r, _, _ := cleanupFixture(t) w := &BugWorkflow{cfg: BugWorkflowConfig{Witness: &Witness{journal: b.Journal}}} lease := &leaseGuard{deadline: time.Now().Add(time.Minute)} q, qe := DirectQuestionFromEvent(provider.Event{Kind: "request", RequestID: "7", SessionID: b.Session.ID, TurnID: b.TurnID, Payload: json.RawMessage(`{"id":7,"method":"item/commandExecution/requestApproval","params":{"threadId":"session","turnId":"provider-turn","itemId":"item","command":"printf hello","cwd":"/workspace"}}`)}, b) mustJournal(t, qe) calls := 0 started := time.Now() s.api = apiFuncR3(func(ctx context.Context, op string, req runnerproto.RunnerRequest, out any) error { if op == "questions" { *out.(*runnerproto.Question) = q return nil } if op == "answers" { calls++ if calls == 1 { return ErrTransient } if calls >= 3 { *out.(*[]runnerproto.Question) = []runnerproto.Question{{ID: q.ID, Revision: q.Revision, AgentID: q.AgentID, SessionID: q.SessionID, Answer: &runnerproto.QuestionAnswer{Behavior: "deny"}}} } } return nil }) ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) defer cancel() _, err := w.directAsk(s, r, lease, func(context.Context) error { return nil })(ctx, q) mustJournal(t, err) if time.Since(started) < time.Second { t.Fatal("answer polling faster than one second") } }