oficina / equipo Public

fix: let person answers wait across renewed runner leases

codex agent, operated by paul@keymail.dev pushed by claude agent, operated by paul@keymail.dev a42c4ac7952bbab345a39246453fdfc237ee2958 parent 56ca88b
3 files changed, +94 −7
  • internal/runner/direct_questions.go +2 −4
  • internal/runner/direct_wait_test.go +89 −0
  • internal/runner/direct_workflow.go +3 −3
diff --git a/internal/runner/direct_questions.go b/internal/runner/direct_questions.go
index bf800682..cff26b9a 100644
--- a/internal/runner/direct_questions.go
+++ b/internal/runner/direct_questions.go
@@ -81,8 +81,7 @@ func (r *DirectQuestionRelay) Handle(ctx context.Context, ev provider.Event, ask
if e != nil || q.ExpiresAt.IsZero() || !time.Now().Before(q.ExpiresAt) {
return runnerproto.ErrInvalid
}
- waitCtx, cancel := context.WithDeadline(ctx, q.ExpiresAt)
- defer cancel()
+ waitCtx := ctx
answer, e := ask(waitCtx, q)
if e != nil {
return e
@@ -221,8 +220,7 @@ func (r *DirectQuestionRelay) Ask(ctx context.Context, id string, args json.RawM
if err != nil || expiry.IsZero() || !time.Now().Before(expiry) {
return nil, runnerproto.ErrInvalid
}
- ctx, cancel := context.WithDeadline(ctx, expiry)
- defer cancel()
+ q.ExpiresAt = expiry
answer, err := ask(ctx, q)
if err != nil {
return nil, err
diff --git a/internal/runner/direct_wait_test.go b/internal/runner/direct_wait_test.go
new file mode 100644
index 00000000..a8f38cb1
--- /dev/null
+++ b/internal/runner/direct_wait_test.go
@@ -0,0 +1,89 @@
+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")
+ }
+}
diff --git a/internal/runner/direct_workflow.go b/internal/runner/direct_workflow.go
index 95f8934d..4d6f54fa 100644
--- a/internal/runner/direct_workflow.go
+++ b/internal/runner/direct_workflow.go
@@ -150,7 +150,7 @@ func (w *BugWorkflow) directModel(ctx context.Context, s *Supervisor, r *attempt
uploadDone := make(chan struct{})
go func() {
defer close(uploadDone)
- ticker := time.NewTicker(100 * time.Millisecond)
+ ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
@@ -295,14 +295,14 @@ func (w *BugWorkflow) directAsk(s *Supervisor, r *attemptState, l *leaseGuard, c
if err := s.api.Call(ctx, "questions", request, &saved); err != nil {
return runnerproto.QuestionAnswer{}, err
}
- ticker := time.NewTicker(100 * time.Millisecond)
+ ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
if err := current(ctx); err != nil {
return runnerproto.QuestionAnswer{}, err
}
var answers []runnerproto.Question
- if err := s.api.Call(ctx, "answers", r.request("answers"), &answers); err != nil {
+ if err := retryAuthority(ctx, func() error { return s.api.Call(ctx, "answers", r.request("answers"), &answers) }); err != nil {
return runnerproto.QuestionAnswer{}, err
}
for _, answer := range answers {