design: Design questions need a runner at 39 and a start check
Follow-up review of F2.
1. Below 39 a question's forget revision was dropped and the question
still delivered, so a downgraded runner could resume the old
transcript. Now a question about a Design step is never delivered
below 39: AskCommand.design marks it, and Encode refuses a Design
question (or any with a forget revision) below 39, as it refuses a
Design dispatch. The instance does not send one there: Ask refuses
it as not askable (ErrNotAskable, existing words); one waiting
behind another when its runner is below 39 ends as session_gone and
the next goes; one sent but not delivered when the runner reconnects
below 39 ends the same way, its command voided
(fenceDesignQuestions on Connect).
2. A queued question's cancel could race its start (questions ran on
the generic lease). Each question attempt now asks the instance
before it runs (question_start, protocol 39: Link.ConfirmAsk,
Engineer.ConfirmAsk in answer; nothing is sent below 39). The
instance grants only the question's current, sent, unfinished
attempt on the runner that holds its session (authorizeQuestion).
A forget replaces a waiting attempt with the next in the same
transaction (askAgainAfterForget), which carries the new forget
revision, so the replaced attempt is refused at its start and the
question is answered by the new one, fresh.
Tests (RED first):
go test ./internal/wire/ -run TestDowngradeDesignFeedback
RED: "a design ask at 38: <nil>"
go test ./internal/equipo/chat/ -run 'TestDesignQuestionStartCheck|TestDesignQuestionBelow39'
RED: asks not marked Design; "asked at 38: <nil>"
go test ./internal/runner/thin/ -run 'TestAskStartCheckedFirst|TestAskStartCheckThroughTheLink'
RED: "a refused question ran"
GREEN: all; thin -run 'TestAskStart|TestDesignAskAfterForget|Ask|Kept'
Mutations (each restored):
start check without the attempt -> "a replaced attempt's start
after a forget: \"\"" FAIL
Ask without designAskable -> "asked at 38: <nil>" FAIL
nextQuestion without designAskable -> "waiting question at 38:
queued" FAIL
no Connect fence -> "undelivered question after a
reconnect at 38: queued" FAIL
Design flag never set -> asks FAIL; the fence misses it
answer without ConfirmAsk -> "a refused question ran" FAIL
Link.ConfirmAsk sent below 39 -> "at 38: runner holds no
confirmed lease" FAIL
Link.ConfirmAsk without its purpose -> "check {... Purpose: ...}" FAIL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
15 files changed,
+382
−24
internal/equipo/chat/questions.go+104 −2internal/equipo/chat/runner.go+3 −0internal/equipo/chat/runners.go+6 −0internal/equipo/chat/taste_dispatch_test.go+115 −0internal/runner/thin/ask.go+11 −0internal/runner/thin/design_step_test.go+49 −0internal/runner/thin/engineer.go+4 −0internal/runner/thin/forget_session_linux_test.go+26 −0internal/runner/thin/link.go+14 −0internal/runner/thin/run.go+1 −1internal/wire/design.go+8 −8internal/wire/design_feedback_test.go+25 −9internal/wire/frames.go+7 −0internal/wire/questions.go+4 −0internal/wire/runners.go+5 −4
diff --git a/internal/equipo/chat/questions.go b/internal/equipo/chat/questions.go| index 2d28a4b9..4178c099 100644 |
| --- a/internal/equipo/chat/questions.go |
| +++ b/internal/equipo/chat/questions.go |
| @@ -187,6 +187,13 @@ func (s *Store) Ask(ctx context.Context, person, taskID string, in wire.AskQuest |
| if !ok || target.Person != person { |
| return ErrNotAskable |
| } |
| + // A Design step's session is asked only through a runner at 39, |
| + // which fences it by the forget revision (designAskable). |
| + if ok, err := t.designAskable(target, target.RunnerID); err != nil { |
| + return err |
| + } else if !ok { |
| + return ErrNotAskable |
| + } |
| // The session lives on the runner that ran the step: offline, the |
| // person is told so now, naming it (ask again when it is back). |
| if online, err := t.runnerOnline(person, target.RunnerID); err != nil { |
| @@ -261,20 +268,115 @@ func (t *txn) nextQuestion(k taskRecord) error { |
| // Asked before questions were frozen: the step as it stands. |
| base, candidate = askBase(k, s), s.CandidateSHA |
| } |
| + runner := questionRunner(q, s) |
| + if ok, err := t.designAskable(s, runner); err != nil { |
| + return err |
| + } else if !ok { |
| + // Its runner is below 39 now: never sent (Encode would refuse |
| + // it); it ends as its session not available, and the next goes. |
| + q.State, q.Reason = wire.QuestionFailed, "session_gone" |
| + if err := t.saveQuestion(&q); err != nil { |
| + return err |
| + } |
| + return t.nextQuestion(k) |
| + } |
| + design, err := t.designStep(s) |
| + if err != nil { |
| + return err |
| + } |
| forget, err := t.askForgetRevision(k, s) |
| if err != nil { |
| return err |
| } |
| cmd := wire.AskCommand{Person: s.Person, TaskID: k.ID, StepID: s.ID, QuestionID: q.ID, Attempt: q.Attempt, Specialist: s.Specialist, Repo: p.Repo, Branch: k.Branch, BaseSHA: base, CandidateSHA: candidate, Title: k.Title, Text: q.Text, |
| - EngAttempt: q.EngAttempt, EngAccount: q.EngAccount, EngSession: q.EngSession, ForgetRevision: forget} |
| + EngAttempt: q.EngAttempt, EngAccount: q.EngAccount, EngSession: q.EngSession, ForgetRevision: forget, Design: design} |
| // Asked of the runner that holds the session, never another. |
| - if err := t.commandTo(questionRunner(q, s), s.Person, wire.FrameAsk, cmd); err != nil { |
| + if err := t.commandTo(runner, s.Person, wire.FrameAsk, cmd); err != nil { |
| return err |
| } |
| q.Dispatched = true |
| return t.saveQuestion(&q) |
| } |
| +// designAskable reports whether a question about step s may go to |
| +// runner: always for any step but Design's; for Design's, only to a |
| +// runner whose connection speaks 39, which resumes the step's session |
| +// only at the forget revision it was kept at (a runner below 39 keeps no |
| +// such fence). |
| +func (t *txn) designAskable(s stepRecord, runner string) (bool, error) { |
| + design, err := t.designStep(s) |
| + if err != nil || !design { |
| + return err == nil, err |
| + } |
| + var sess runnerSessionRecord |
| + if err := t.db.Where("runner_id = ?", runner).Limit(1).Find(&sess).Error; err != nil { |
| + return false, err |
| + } |
| + return sess.Protocol >= wire.ProtocolDesignFeedback, nil |
| +} |
| + |
| +// fenceDesignQuestions ends the questions about Design steps sent to a |
| +// runner that reconnects below 39 before it received them (Encode |
| +// refuses them there): each ends as its session not available, its |
| +// command voided, and the task's next question goes. |
| +func (t *txn) fenceDesignQuestions(l Lease) error { |
| + if l.Protocol >= wire.ProtocolDesignFeedback { |
| + return nil |
| + } |
| + var cmds []commandRecord |
| + if err := t.db.Where("runner_id = ? AND kind = ? AND acked_at IS NULL", l.Runner.RunnerID, wire.FrameAsk).Find(&cmds).Error; err != nil { |
| + return err |
| + } |
| + for _, c := range cmds { |
| + var a wire.AskCommand |
| + if json.Unmarshal([]byte(c.Body), &a) != nil || !a.Design { |
| + continue |
| + } |
| + var q questionRecord |
| + if err := t.db.Where("id = ?", a.QuestionID).Limit(1).Find(&q).Error; err != nil { |
| + return err |
| + } |
| + if err := t.db.Model(&commandRecord{}).Where("seq = ?", c.Seq).Update("acked_at", t.now()).Error; err != nil { |
| + return err |
| + } |
| + if q.ID == "" || q.Attempt != a.Attempt || wire.QuestionTerminal(q.State) { |
| + continue |
| + } |
| + q.State, q.Reason = wire.QuestionFailed, "session_gone" |
| + if err := t.saveQuestion(&q); err != nil { |
| + return err |
| + } |
| + k, err := t.loadTask(q.TaskID) |
| + if err != nil { |
| + return err |
| + } |
| + if err := t.nextQuestion(k); err != nil { |
| + return err |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// authorizeQuestion is a question attempt's start check |
| +// (wire.LeasePurposeAsk): granted ("") only for the question's current |
| +// attempt, sent and not ended, on the runner that holds its session. A |
| +// forget replaces a waiting attempt with the next (askAgainAfterForget), |
| +// so the replaced one is refused here and never resumes a session |
| +// holding a forgotten note. |
| +func (t *txn) authorizeQuestion(l Lease, id string, attempt int64) (string, error) { |
| + q, err := t.ownQuestion(l, id) |
| + if errors.Is(err, ErrNotFound) || errors.Is(err, ErrRefused) { |
| + return "withdrawn", nil |
| + } |
| + if err != nil { |
| + return "", err |
| + } |
| + if q.Attempt != attempt || !q.Dispatched || wire.QuestionTerminal(q.State) { |
| + return "withdrawn", nil |
| + } |
| + return "", nil |
| +} |
| + |
| // askForgetRevision is the forget revision a question about step s |
| // carries: the task creator's design notebook's, for a Design step (the |
| // runner resumes its kept session only at the same one); 0 for any |
diff --git a/internal/equipo/chat/runner.go b/internal/equipo/chat/runner.go| index 8fa8aa2b..41061bbb 100644 |
| --- a/internal/equipo/chat/runner.go |
| +++ b/internal/equipo/chat/runner.go |
| @@ -145,6 +145,9 @@ func (s *Store) Connect(ctx context.Context, p RunnerPrincipal, process string, |
| if err := t.fenceDesign(lease); err != nil { |
| return err |
| } |
| + if err := t.fenceDesignQuestions(lease); err != nil { |
| + return err |
| + } |
| // A primary's detected addresses, for Research's site inventory; |
| // its steps waiting for the inventory are looked at again. |
| if role == wire.RolePrimary { |
diff --git a/internal/equipo/chat/runners.go b/internal/equipo/chat/runners.go| index 21a168b7..a8c28657 100644 |
| --- a/internal/equipo/chat/runners.go |
| +++ b/internal/equipo/chat/runners.go |
| @@ -933,6 +933,12 @@ func (s *Store) Authorize(ctx context.Context, l Lease, stepID string, attempt i |
| if _, err := t.current(l); err != nil { |
| return err |
| } |
| + if purpose == wire.LeasePurposeAsk { |
| + // A question's start: stepID names the question. |
| + r, err := t.authorizeQuestion(l, stepID, attempt) |
| + refused = r |
| + return err |
| + } |
| var st stepRecord |
| if err := t.db.Where("id = ? AND person = ?", stepID, l.Runner.Person).First(&st).Error; errors.Is(err, gorm.ErrRecordNotFound) { |
| refused = "withdrawn" |
diff --git a/internal/equipo/chat/taste_dispatch_test.go b/internal/equipo/chat/taste_dispatch_test.go| index b1ad9cd7..ebc621de 100644 |
| --- a/internal/equipo/chat/taste_dispatch_test.go |
| +++ b/internal/equipo/chat/taste_dispatch_test.go |
| @@ -2,6 +2,7 @@ package chat |
| import ( |
| "encoding/json" |
| + "errors" |
| "strings" |
| "testing" |
| "time" |
| @@ -753,3 +754,117 @@ func TestStaleReplacementsSpendNoRun(t *testing.T) { |
| t.Fatalf("after a stale advanced start: %d runs", k.DesignRuns) |
| } |
| } |
| + |
| +// askableRound is roundOfThree with its maker step's session kept for |
| +// questions, and the rig able to take runner connections. |
| +func askableRound(t *testing.T) (*hkRig, Lease, string, stepRecord) { |
| + t.Helper() |
| + r, l, task, s, _ := roundOfThree(t) |
| + r.exec("UPDATE chat_steps SET askable = 1, account = 'claude-a', session_id = 'ses_1' WHERE id = ?", s.ID) |
| + r.exec("CREATE TABLE IF NOT EXISTS runners (id text PRIMARY KEY, person text, role text, label text, removing_at datetime, tainted_at datetime, revoked_at datetime)") |
| + r.exec("INSERT OR IGNORE INTO runners(id, person, role) VALUES ('RUNNER1', ?, ?)", paul, wire.RolePrimary) |
| + return r, l, task, r.stepNow(s.ID) |
| +} |
| + |
| +// questionState is q's state, reason and attempt. |
| +func (r *hkRig) questionState(q string) (string, string, int64) { |
| + r.t.Helper() |
| + got, err := r.s.Question(r.ctx, paul, q) |
| + r.must(err) |
| + return got.State, got.Reason, got.Attempt |
| +} |
| + |
| +// TestDesignQuestionStartCheck (follow-up review of F2): a question's |
| +// attempt runs only after the instance grants its start |
| +// (question_start) for that very attempt: once a forget replaced it |
| +// with the next attempt (which carries the new forget revision), the old |
| +// one is refused, so it never resumes a session holding a forgotten |
| +// note, even when the cancel has not reached the runner yet. A question |
| +// about a Design step is marked Design for the wire. |
| +func TestDesignQuestionStartCheck(t *testing.T) { |
| + r, l, task, _ := askableRound(t) |
| + q, err := r.s.Ask(r.ctx, paul, task, wire.AskQuestion{Text: "Why serif?", IdempotencyKey: "k1", Specialist: wire.SpecialistEngineering}) |
| + r.must(err) |
| + if asks := r.askCommands(q.ID); len(asks) != 1 || !asks[0].Design { |
| + t.Fatalf("asks %+v", asks) |
| + } |
| + start := func(l Lease, attempt int64) string { |
| + t.Helper() |
| + refused, err := r.s.Authorize(r.ctx, l, q.ID, attempt, wire.LeasePurposeAsk, "", "") |
| + r.must(err) |
| + return refused |
| + } |
| + if got := start(l, 1); got != "" { |
| + t.Fatalf("the current attempt's start: %q", got) |
| + } |
| + r.must(r.s.AddTaste(r.ctx, paul, "", "One.")) |
| + r.must(r.s.ForgetTaste(r.ctx, paul, r.tasteRows(paul)[0].ID)) |
| + if got := start(l, 1); got != "withdrawn" { |
| + t.Fatalf("a replaced attempt's start after a forget: %q", got) |
| + } |
| + if got := start(l, 2); got != "" { |
| + t.Fatalf("the new attempt's start: %q", got) |
| + } |
| + other := l |
| + other.Runner.RunnerID = "RUNNER2" |
| + if got, err := r.s.Authorize(r.ctx, other, q.ID, 2, wire.LeasePurposeAsk, "", ""); err == nil && got == "" { |
| + t.Fatal("another runner started the question") |
| + } |
| + if _, err := r.s.CancelQuestion(r.ctx, paul, q.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + r.must(r.tx(func(tx *txn) error { |
| + return tx.answer(l, &wire.AnswerReport{TaskID: task, QuestionID: q.ID, Attempt: 2, State: wire.QuestionCancelled, Reason: "cancelled"}) |
| + })) |
| + if got := start(l, 2); got != "withdrawn" { |
| + t.Fatalf("an ended question's start: %q", got) |
| + } |
| +} |
| + |
| +// TestDesignQuestionBelow39 (follow-up review of F2): a question about |
| +// a Design step is never delivered to a runner below 39 (it keeps no |
| +// session fence): asked then, it is refused as not askable; one waiting |
| +// behind another when the runner is below 39 ends as session_gone; one |
| +// sent but not yet delivered when the runner reconnects below 39 ends |
| +// the same way, its command voided. |
| +func TestDesignQuestionBelow39(t *testing.T) { |
| + r, l, task, _ := askableRound(t) |
| + r.setHello(wire.ProtocolDesign, designHello(wire.ToolHealthy, claudeSub)) |
| + if _, err := r.s.Ask(r.ctx, paul, task, wire.AskQuestion{Text: "Why?", IdempotencyKey: "k0", Specialist: wire.SpecialistEngineering}); !errors.Is(err, ErrNotAskable) { |
| + t.Fatalf("asked at 38: %v", err) |
| + } |
| + r.setHello(wire.ProtocolDesignFeedback, designHello(wire.ToolHealthy, claudeSub)) |
| + q1, err := r.s.Ask(r.ctx, paul, task, wire.AskQuestion{Text: "First?", IdempotencyKey: "k1", Specialist: wire.SpecialistEngineering}) |
| + r.must(err) |
| + q2, err := r.s.Ask(r.ctx, paul, task, wire.AskQuestion{Text: "Second?", IdempotencyKey: "k2", Specialist: wire.SpecialistEngineering}) |
| + r.must(err) |
| + if len(r.askCommands(q2.ID)) != 0 { |
| + t.Fatal("the second question went before the first ended") |
| + } |
| + // The runner is at 38 when the first ends: the second is not sent. |
| + r.setHello(wire.ProtocolDesign, designHello(wire.ToolHealthy, claudeSub)) |
| + r.must(r.tx(func(tx *txn) error { |
| + return tx.answer(l, &wire.AnswerReport{TaskID: task, QuestionID: q1.ID, Attempt: 1, State: wire.QuestionAnswered, Text: "Because."}) |
| + })) |
| + if state, reason, _ := r.questionState(q2.ID); state != wire.QuestionFailed || reason != "session_gone" || len(r.askCommands(q2.ID)) != 0 { |
| + t.Fatalf("waiting question at 38: %s %s, %d asks", state, reason, len(r.askCommands(q2.ID))) |
| + } |
| + // Sent at 39, not delivered, then the runner reconnects at 38. |
| + r.setHello(wire.ProtocolDesignFeedback, designHello(wire.ToolHealthy, claudeSub)) |
| + q3, err := r.s.Ask(r.ctx, paul, task, wire.AskQuestion{Text: "Third?", IdempotencyKey: "k3", Specialist: wire.SpecialistEngineering}) |
| + r.must(err) |
| + if len(r.askCommands(q3.ID)) != 1 { |
| + t.Fatal("the third question was not sent") |
| + } |
| + old := designHello(wire.ToolHealthy, claudeSub) |
| + old.Protocol = wire.ProtocolDesign |
| + if _, err := r.s.Connect(r.ctx, l.Runner, "p2", old, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if state, reason, _ := r.questionState(q3.ID); state != wire.QuestionFailed || reason != "session_gone" { |
| + t.Fatalf("undelivered question after a reconnect at 38: %s %s", state, reason) |
| + } |
| + if n := r.count("SELECT count(*) FROM runner_commands WHERE kind = ? AND acked_at IS NULL AND body LIKE ?", wire.FrameAsk, `%"question_id":"`+q3.ID+`"%`); n != 0 { |
| + t.Fatalf("the undelivered ask is still served: %d", n) |
| + } |
| +} |
diff --git a/internal/runner/thin/ask.go b/internal/runner/thin/ask.go| index f0b7454e..101b37c2 100644 |
| --- a/internal/runner/thin/ask.go |
| +++ b/internal/runner/thin/ask.go |
| @@ -859,6 +859,17 @@ func (e *Engineer) answer(ctx context.Context, a askRow, progress func(wire.Answ |
| if _, err := os.Stat(keptTranscript(kept.Dir, kept.SessionID)); err != nil { |
| return fail("session_gone") |
| } |
| + if e.ConfirmAsk != nil { |
| + // The instance starts only the question's current attempt: one a |
| + // forget replaced never runs (wire.LeasePurposeAsk). |
| + if err := e.waitLease(ctx, func(ctx context.Context) error { return e.ConfirmAsk(ctx, a.QuestionID, a.Attempt) }); err != nil { |
| + rep.State, rep.Reason = wire.QuestionCancelled, "superseded" |
| + if !errors.Is(err, ErrWithdrawn) { |
| + rep.Reason = "cancelled" |
| + } |
| + return rep |
| + } |
| + } |
| bs, err := runner.LoadBindings(e.O.Runner.AccountBindingsPath) |
| if err != nil { |
| return fail("account_unavailable") |
diff --git a/internal/runner/thin/design_step_test.go b/internal/runner/thin/design_step_test.go| index e02b4e21..57da1afa 100644 |
| --- a/internal/runner/thin/design_step_test.go |
| +++ b/internal/runner/thin/design_step_test.go |
| @@ -708,3 +708,52 @@ func TestDesignLaunchAskedAsLaunch(t *testing.T) { |
| t.Fatalf("launched: namespace %q %v", ns, err) |
| } |
| } |
| + |
| +// TestAskStartCheckThroughTheLink (follow-up review of F2): a question's |
| +// start check is a question_start lease naming the question and its |
| +// attempt at protocol 39; below 39 nothing is sent and it is granted |
| +// (an older instance has no such check). |
| +func TestAskStartCheckThroughTheLink(t *testing.T) { |
| + for _, protocol := range []int{wire.ProtocolDesign, wire.ProtocolDesignFeedback} { |
| + inst := newFakeInstance(t) |
| + link, _, _, _ := startLink(t, inst.url, &stubCoordinator{}) |
| + c := <-inst.conns |
| + read(t, c, wire.FrameHello) |
| + welcome, _ := json.Marshal(wire.Frame{ID: "w-d", Type: wire.FrameWelcome, Data: json.RawMessage(`{"generation":3,"runner_id":"RUNNER1","protocol":` + itoa(int64(protocol)) + `,"role":"primary"}`)}) |
| + write(t, c, welcome) |
| + read(t, c, wire.FrameSync) |
| + if protocol < wire.ProtocolDesignFeedback { |
| + at := time.Now() |
| + if err := link.ConfirmAsk(context.Background(), "q_1", 2); err != nil || time.Since(at) > 2*time.Second { |
| + t.Fatalf("at %d: %v", protocol, err) |
| + } |
| + continue |
| + } |
| + done := make(chan error, 1) |
| + go func() { done <- link.ConfirmAsk(context.Background(), "q_1", 2) }() |
| + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| + for { |
| + _, raw, err := c.Read(ctx) |
| + if err != nil { |
| + t.Fatalf("no start check: %v", err) |
| + } |
| + f, _, _ := wire.ParseFrame(raw) |
| + if f.Type != wire.FrameLease { |
| + continue |
| + } |
| + var check wire.LeaseCheck |
| + json.Unmarshal(f.Data, &check) |
| + if check.Purpose != wire.LeasePurposeAsk || check.StepID != "q_1" || check.Attempt != 2 { |
| + t.Fatalf("check %+v", check) |
| + } |
| + no, _ := wire.NewFrame("r-"+f.ID, wire.FrameRefused, wire.Refused{ID: f.ID, Reason: "withdrawn"}) |
| + rawNo, _ := json.Marshal(no) |
| + write(t, c, rawNo) |
| + break |
| + } |
| + cancel() |
| + if err := <-done; !errors.Is(err, ErrWithdrawn) { |
| + t.Fatalf("a refusal: %v", err) |
| + } |
| + } |
| +} |
diff --git a/internal/runner/thin/engineer.go b/internal/runner/thin/engineer.go| index 0022067b..77841055 100644 |
| --- a/internal/runner/thin/engineer.go |
| +++ b/internal/runner/thin/engineer.go |
| @@ -109,6 +109,10 @@ type Engineer struct { |
| // so an attempt held after its start never launches with notes that |
| // changed while it waited. nil: the plain check (lease). |
| ConfirmLaunch func(ctx context.Context, step string, attempt int64) error |
| + // ConfirmAsk is a question attempt's start check (protocol 39, |
| + // purpose question_start): the instance grants only the question's |
| + // current attempt. nil: no check. |
| + ConfirmAsk func(ctx context.Context, question string, attempt int64) error |
| // Devbox is the paired CARLOS devbox (nil: none); it wins over |
| // Preview's Carloku temporary sites. |
| Devbox *DevboxOptions |
diff --git a/internal/runner/thin/forget_session_linux_test.go b/internal/runner/thin/forget_session_linux_test.go| index 2a4080d9..a86305ff 100644 |
| --- a/internal/runner/thin/forget_session_linux_test.go |
| +++ b/internal/runner/thin/forget_session_linux_test.go |
| @@ -1,6 +1,7 @@ |
| package thin |
| import ( |
| + "context" |
| "encoding/json" |
| "os" |
| "strings" |
| @@ -161,3 +162,28 @@ func TestDesignAskAfterForgetAnswersFresh(t *testing.T) { |
| t.Fatalf("the fresh answer's transcript was kept with the step's: %d files, was %d", len(again), len(files)) |
| } |
| } |
| + |
| +// TestAskStartCheckedFirst (follow-up review of F2): a question's |
| +// attempt asks the instance to start it (question_start) before it runs; |
| +// refused (a forget replaced the attempt), it ends superseded without |
| +// running, so the kept session is never resumed for it. |
| +func TestAskStartCheckedFirst(t *testing.T) { |
| + r, rep, _ := keptRound(t, 1, 1) |
| + var asked []string |
| + r.e.ConfirmAsk = func(_ context.Context, q string, attempt int64) error { |
| + asked = append(asked, q+"/"+itoa(attempt)) |
| + return ErrWithdrawn |
| + } |
| + a := askRow{AskCommand: wire.AskCommand{Person: connPerson, TaskID: "tk1", StepID: "ds1", QuestionID: "q1", Attempt: 2, Specialist: wire.SpecialistEngineering, Repo: "amadan.net/oficina/sample", Branch: "equipo/tk1-design", BaseSHA: rep.BaseSHA, CandidateSHA: rep.CandidateSHA, Title: "Redesign the shop", Text: "Why this one?", ForgetRevision: 1, Design: true}} |
| + got := r.e.ask(r.ctx, a, func(wire.AnswerReport) { t.Error("a refused question ran") }).Report |
| + if got.State != wire.QuestionCancelled || got.Reason != "superseded" || got.Text != "" { |
| + t.Fatalf("report %+v", got) |
| + } |
| + if len(asked) != 1 || asked[0] != "q1/2" { |
| + t.Fatalf("start checks %v", asked) |
| + } |
| + r.e.ConfirmAsk = func(context.Context, string, int64) error { return nil } |
| + if got := r.e.ask(r.ctx, a, func(wire.AnswerReport) {}).Report; got.State != wire.QuestionAnswered { |
| + t.Fatalf("granted: %+v", got) |
| + } |
| +} |
diff --git a/internal/runner/thin/link.go b/internal/runner/thin/link.go| index dfc0f490..38e7b8ba 100644 |
| --- a/internal/runner/thin/link.go |
| +++ b/internal/runner/thin/link.go |
| @@ -1294,3 +1294,17 @@ func (l *Link) ConfirmLaunch(ctx context.Context, step string, attempt int64) er |
| } |
| return l.lease(ctx, wire.LeaseCheck{StepID: step, Attempt: attempt, Purpose: wire.LeasePurposeLaunch}) |
| } |
| + |
| +// ConfirmAsk is a question attempt's start check right before it runs |
| +// (ProtocolDesignFeedback, purpose question_start, StepID the question): |
| +// the instance grants only its current attempt. Below protocol 39 nothing |
| +// is sent and it is granted: an older instance has no such check. |
| +func (l *Link) ConfirmAsk(ctx context.Context, question string, attempt int64) error { |
| + l.mu.Lock() |
| + proto := l.proto |
| + l.mu.Unlock() |
| + if proto < wire.ProtocolDesignFeedback { |
| + return nil |
| + } |
| + return l.lease(ctx, wire.LeaseCheck{StepID: question, Attempt: attempt, Purpose: wire.LeasePurposeAsk}) |
| +} |
diff --git a/internal/runner/thin/run.go b/internal/runner/thin/run.go| index 8a3ae2de..df89f608 100644 |
| --- a/internal/runner/thin/run.go |
| +++ b/internal/runner/thin/run.go |
| @@ -377,7 +377,7 @@ func Run(ctx context.Context, c runner.Config, o Options) error { |
| if err = engine.Start(ctx); err != nil { |
| return err |
| } |
| - engineer := &Engineer{DiffSecrets: []string{creds.Bearer.Token}, O: o.Claude, Codex: o.Codex, Pi: piOptions(c, o.Pi), OpenCode: opencodeOptions(c, o.OpenCode), Clones: clones, Forge: fg, CI: hostedCIOf(fg), Authorize: link.Confirm, AuthorizeStep: link.ConfirmStep, ConfirmMerge: link.ConfirmMerge, ConfirmPreview: link.ConfirmPreview, ConfirmRender: link.ConfirmRender, ConfirmLaunch: link.ConfirmLaunch, Preview: preview, Devbox: devbox, Site: site, ConfirmPublish: link.ConfirmPublish, ConfirmMint: link.ConfirmMint, SiteChanged: func() { caps.Stale(); link.HelloSoon() }, ConfirmPublication: link.ConfirmPublication, Owners: withPerson(coordinatorPersons(c)(), creds.Bearer.Person), MaxStep: o.MaxStep, Note: o.StepNote, Spoken: func() int { p, _ := link.Spoken(); return p }, Healthy: conns.Usable, Conns: conns, Delta: link.Delta, Activity: link.Activity, StepActivity: link.StepActivity, Retention: o.KeptRetention, Host: c.MachineLabel, Merged: func(string) { index.Kick() }} |
| + engineer := &Engineer{DiffSecrets: []string{creds.Bearer.Token}, O: o.Claude, Codex: o.Codex, Pi: piOptions(c, o.Pi), OpenCode: opencodeOptions(c, o.OpenCode), Clones: clones, Forge: fg, CI: hostedCIOf(fg), Authorize: link.Confirm, AuthorizeStep: link.ConfirmStep, ConfirmMerge: link.ConfirmMerge, ConfirmPreview: link.ConfirmPreview, ConfirmRender: link.ConfirmRender, ConfirmLaunch: link.ConfirmLaunch, ConfirmAsk: link.ConfirmAsk, Preview: preview, Devbox: devbox, Site: site, ConfirmPublish: link.ConfirmPublish, ConfirmMint: link.ConfirmMint, SiteChanged: func() { caps.Stale(); link.HelloSoon() }, ConfirmPublication: link.ConfirmPublication, Owners: withPerson(coordinatorPersons(c)(), creds.Bearer.Person), MaxStep: o.MaxStep, Note: o.StepNote, Spoken: func() int { p, _ := link.Spoken(); return p }, Healthy: conns.Usable, Conns: conns, Delta: link.Delta, Activity: link.Activity, StepActivity: link.StepActivity, Retention: o.KeptRetention, Host: c.MachineLabel, Merged: func(string) { index.Kick() }} |
| engineer.Renderer = renderer |
| link.Forget = engineer.RemoveKept |
| link.TaskEnded = engineer.PreviewTaskEnded |
diff --git a/internal/wire/design.go b/internal/wire/design.go| index 193fa5fc..0807d7b1 100644 |
| --- a/internal/wire/design.go |
| +++ b/internal/wire/design.go |
| @@ -723,9 +723,9 @@ func designFeedbackDispatch(d *DispatchCommand) bool { |
| // ProtocolDesignFeedback. A Design dispatch, and any dispatch with taste |
| // or a round request, is refused (designFeedbackDispatch), and so are an |
| // ask_design_taste call (an older instance has no discovery card) and a |
| -// design_launch check (the runner sends a plain one there instead). A |
| -// question's forget revision is dropped (an older runner keeps no |
| -// fence). A |
| +// design_launch or question_start check (the runner sends a plain one, |
| +// or none, there instead), and a question about a Design step (an older |
| +// runner keeps no session fence). A |
| // report's pack digest, its option directions and pages and its |
| // captures' labels and render facts are dropped (its names and captures |
| // stay valid under ValidOptions), and so are propose_specialist's |
| @@ -738,12 +738,12 @@ func downgradeDesignFeedback(body any, protocol int) (bool, error) { |
| return false, fmt.Errorf("%w: a Design dispatch at protocol %d", ErrProtocol, protocol) |
| } |
| case *AskCommand: |
| - changed := v.ForgetRevision != 0 |
| - v.ForgetRevision = 0 |
| - return changed, nil |
| + if v.Design || v.ForgetRevision != 0 { |
| + return false, fmt.Errorf("%w: a question about a Design step at protocol %d", ErrProtocol, protocol) |
| + } |
| case *LeaseCheck: |
| - if v.Purpose == LeasePurposeLaunch { |
| - return false, fmt.Errorf("%w: a launch check at protocol %d", ErrProtocol, protocol) |
| + if v.Purpose == LeasePurposeLaunch || v.Purpose == LeasePurposeAsk { |
| + return false, fmt.Errorf("%w: a %s check at protocol %d", ErrProtocol, v.Purpose, protocol) |
| } |
| case *ToolCall: |
| if v.Name == ToolAskDesignTaste { |
diff --git a/internal/wire/design_feedback_test.go b/internal/wire/design_feedback_test.go| index fab7a889..92c6e26e 100644 |
| --- a/internal/wire/design_feedback_test.go |
| +++ b/internal/wire/design_feedback_test.go |
| @@ -307,15 +307,31 @@ func TestDowngradeDesignFeedback(t *testing.T) { |
| if same, err := wire.Encode(old, wire.ProtocolDesign); err != nil || !bytes.Equal(same.Data, old.Data) { |
| t.Fatalf("an old report changed at 38: %v", err) |
| } |
| - // A question loses its forget revision at 38 (an older runner keeps |
| - // no fence) and keeps the rest; at 39 it goes as it is. |
| - askFrame, _ := wire.NewFrame("a", wire.FrameAsk, wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", StepID: "s_1", QuestionID: "q_1", Attempt: 1, Specialist: wire.SpecialistEngineering, ForgetRevision: 3}) |
| - if same, err := wire.Encode(askFrame, wire.ProtocolDesignFeedback); err != nil || !bytes.Equal(same.Data, askFrame.Data) { |
| - t.Fatalf("an ask changed at 39: %v", err) |
| - } |
| - got, err = wire.Encode(askFrame, wire.ProtocolDesign) |
| - if err != nil || strings.Contains(string(got.Data), "forget_revision") || !strings.Contains(string(got.Data), `"question_id":"q_1"`) { |
| - t.Fatalf("an ask at 38: %s %v", got.Data, err) |
| + // A question about a Design step (design, or a forget revision) is |
| + // refused at 38, never delivered: an older runner keeps no fence and |
| + // would resume a transcript holding forgotten notes. Engineering's own |
| + // question goes at 38 as before, and so does each at 39; so is a |
| + // question's start check (question_start) refused at 38. |
| + plain := wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", StepID: "s_1", QuestionID: "q_1", Attempt: 1, Specialist: wire.SpecialistEngineering} |
| + design, forgot := plain, plain |
| + design.Design = true |
| + forgot.ForgetRevision = 3 |
| + for name, a := range map[string]wire.AskCommand{"design": design, "forget revision": forgot} { |
| + f, _ := wire.NewFrame("a", wire.FrameAsk, a) |
| + if same, err := wire.Encode(f, wire.ProtocolDesignFeedback); err != nil || !bytes.Equal(same.Data, f.Data) { |
| + t.Fatalf("a %s ask changed at 39: %v", name, err) |
| + } |
| + if _, err := wire.Encode(f, wire.ProtocolDesign); !errors.Is(err, wire.ErrProtocol) { |
| + t.Fatalf("a %s ask at 38: %v", name, err) |
| + } |
| + } |
| + pf, _ := wire.NewFrame("a", wire.FrameAsk, plain) |
| + if same, err := wire.Encode(pf, wire.ProtocolDesign); err != nil || !bytes.Equal(same.Data, pf.Data) { |
| + t.Fatalf("Engineering's ask at 38: %v", err) |
| + } |
| + start, _ := wire.NewFrame("l", wire.FrameLease, wire.LeaseCheck{Generation: 1, StepID: "q_1", Attempt: 1, Purpose: wire.LeasePurposeAsk}) |
| + if _, err := wire.Encode(start, wire.ProtocolDesign); !errors.Is(err, wire.ErrProtocol) { |
| + t.Fatalf("a question's start check at 38: %v", err) |
| } |
| // propose_specialist loses its feels at 38 and keeps the rest. |
| got, err = wire.Encode(frames[5], wire.ProtocolDesign) |
diff --git a/internal/wire/frames.go b/internal/wire/frames.go| index f877f89b..7e768309 100644 |
| --- a/internal/wire/frames.go |
| +++ b/internal/wire/frames.go |
| @@ -471,6 +471,13 @@ const LeasePurposeRender = "page_render" |
| // ProtocolDesignFeedback the runner sends a plain start check instead. |
| const LeasePurposeLaunch = "design_launch" |
| +// LeasePurposeAsk is a LeaseCheck's Purpose right before a question's |
| +// attempt runs (ProtocolDesignFeedback); StepID names the question. The |
| +// instance grants it only for the question's current attempt, so an |
| +// attempt a forget replaced (askAgainAfterForget) never runs. Below |
| +// ProtocolDesignFeedback no such check is sent. |
| +const LeasePurposeAsk = "question_start" |
| + |
| // LeasePurposeMerge is a LeaseCheck's Purpose right before a hub merge |
| // request. |
| const LeasePurposeMerge = "merge_request" |
diff --git a/internal/wire/questions.go b/internal/wire/questions.go| index cee11bdb..d1942b72 100644 |
| --- a/internal/wire/questions.go |
| +++ b/internal/wire/questions.go |
| @@ -119,6 +119,10 @@ type AskCommand struct { |
| // answers from a fresh session (a resumed session keeps every note |
| // it was given). Encode drops it below ProtocolDesignFeedback. |
| ForgetRevision int64 `json:"forget_revision,omitempty"` |
| + // ProtocolDesignFeedback: Design is set for a question about a |
| + // Design step. Encode refuses such a question (and any with a |
| + // forget revision) below it: an older runner keeps no fence. |
| + Design bool `json:"design,omitempty"` |
| } |
| // CancelAskCommand stops one attempt of a question. |
diff --git a/internal/wire/runners.go b/internal/wire/runners.go| index 894bdeeb..cd524d1f 100644 |
| --- a/internal/wire/runners.go |
| +++ b/internal/wire/runners.go |
| @@ -777,10 +777,11 @@ var ErrProtocol = errors.New("wire: frame not in this protocol") |
| // a bound dispatch, a render check, a render_file chunk and a proposal |
| // for one project are refused and a report's options and render records |
| // dropped (downgradeDesign). Below ProtocolDesignFeedback a Design |
| -// dispatch, a dispatch with taste or a round request, a design_launch |
| -// check and an ask_design_taste call are refused, and a report's |
| -// directions, pages, labels and render facts, a proposal's feels and a |
| -// question's forget revision dropped (downgradeDesignFeedback). |
| +// dispatch, a dispatch with taste or a round request, a question about |
| +// a Design step, a design_launch or question_start check and an |
| +// ask_design_taste call are refused, and a report's directions, pages, |
| +// labels and render facts and a proposal's feels dropped |
| +// (downgradeDesignFeedback). |
| func Encode(f Frame, protocol int) (Frame, error) { |
| if protocol >= ProtocolCurrent { |
| return f, nil |