| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 5 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func TestDirectQuestionRenewedLease(t *testing.T) { |
| 13 | for _, kind := range []string{"claude", "codex", "ask_person"} { |
| 14 | t.Run(kind, func(t *testing.T) { |
| 15 | b := directToolFixture(t) |
| 16 | if kind == "claude" { |
| 17 | j, _, session, _ := directFixture(t, "claude") |
| 18 | mustJournal(t, j.BeginTurn(context.Background(), session, directTurn())) |
| 19 | mustJournal(t, j.consumeTurn(context.Background(), session.AttemptKey, session.ID, "hello")) |
| 20 | b.Journal = j |
| 21 | b.Session = session |
| 22 | b.Agent.Provider = "claude" |
| 23 | b.Agent.AgentID = session.AttemptKey |
| 24 | b.Agent.ProviderSessionID = session.ID |
| 25 | } |
| 26 | deadline := time.Now().Add(20 * time.Millisecond) |
| 27 | l := &directAnswerLauncher{} |
| 28 | relay := DirectQuestionRelay{Binding: b, Launcher: l, Current: func(context.Context, runnerproto.Question) (time.Time, error) { return deadline, nil }} |
| 29 | ask := func(ctx context.Context, q runnerproto.Question) (runnerproto.QuestionAnswer, error) { |
| 30 | deadline = time.Now().Add(time.Second) |
| 31 | select { |
| 32 | case <-ctx.Done(): |
| 33 | return runnerproto.QuestionAnswer{}, ctx.Err() |
| 34 | case <-time.After(40 * time.Millisecond): |
| 35 | } |
| 36 | return runnerproto.QuestionAnswer{Behavior: "deny"}, nil |
| 37 | } |
| 38 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 39 | defer cancel() |
| 40 | if kind == "ask_person" { |
| 41 | _, err := relay.Ask(ctx, "request", json.RawMessage(`{"question":"Which?"}`), ask) |
| 42 | mustJournal(t, err) |
| 43 | return |
| 44 | } |
| 45 | raw := json.RawMessage(`{"id":7,"method":"item/commandExecution/requestApproval","params":{"threadId":"session","turnId":"provider-turn","itemId":"item","command":"printf hello","cwd":"/workspace"}}`) |
| 46 | if kind == "claude" { |
| 47 | raw = json.RawMessage(`{"tool_name":"Write","input":{"file_path":"/outside","content":"x"}}`) |
| 48 | } |
| 49 | ev := provider.Event{Kind: "request", RequestID: "7", SessionID: b.Session.ID, TurnID: b.TurnID, Payload: raw} |
| 50 | mustJournal(t, relay.Handle(ctx, ev, ask)) |
| 51 | if len(l.answers) != 1 { |
| 52 | t.Fatal("renewed answer not delivered") |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | func TestDirectAskRetriesAndCadence(t *testing.T) { |
| 58 | b := directToolFixture(t) |
| 59 | s, r, _, _ := cleanupFixture(t) |
| 60 | w := &BugWorkflow{cfg: BugWorkflowConfig{Witness: &Witness{journal: b.Journal}}} |
| 61 | lease := &leaseGuard{deadline: time.Now().Add(time.Minute)} |
| 62 | 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) |
| 63 | mustJournal(t, qe) |
| 64 | calls := 0 |
| 65 | started := time.Now() |
| 66 | s.api = apiFuncR3(func(ctx context.Context, op string, req runnerproto.RunnerRequest, out any) error { |
| 67 | if op == "questions" { |
| 68 | *out.(*runnerproto.Question) = q |
| 69 | return nil |
| 70 | } |
| 71 | if op == "answers" { |
| 72 | calls++ |
| 73 | if calls == 1 { |
| 74 | return ErrTransient |
| 75 | } |
| 76 | if calls >= 3 { |
| 77 | *out.(*[]runnerproto.Question) = []runnerproto.Question{{ID: q.ID, Revision: q.Revision, AgentID: q.AgentID, SessionID: q.SessionID, Answer: &runnerproto.QuestionAnswer{Behavior: "deny"}}} |
| 78 | } |
| 79 | } |
| 80 | return nil |
| 81 | }) |
| 82 | ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) |
| 83 | defer cancel() |
| 84 | _, err := w.directAsk(s, r, lease, func(context.Context) error { return nil })(ctx, q) |
| 85 | mustJournal(t, err) |
| 86 | if time.Since(started) < time.Second { |
| 87 | t.Fatal("answer polling faster than one second") |
| 88 | } |
| 89 | } |
| 90 | |