From c524a0c98e19e4cddb86214cffe9930f7993344a Mon Sep 17 00:00:00 2001 From: Dino Hughes Date: Tue, 24 Nov 2026 08:32:27 +0000 Subject: [PATCH] Stall clock skips time at a hard stop; stall notices give the real wait A session that asked through the server and waited hours for the answer was flagged as stalled seconds after the operator answered: its wait start predated the question. Clear the wait start while the item is at a hard stop or has an open question, so the clock starts at the answer. Planners at a plan question are no longer flagged either. Stall notices now give the wait's real length and say the session did not ask through the server. Co-Authored-By: Claude Opus 5.5 --- docs/session-signals.md | 4 +- internal/controlrun/answered_test.go | 63 ++++++++++++++++++++++++++++ internal/controlrun/dialog_test.go | 2 +- internal/controlrun/view.go | 8 +++- internal/reconcile/decide.go | 10 ++--- internal/reconcile/decide_test.go | 5 ++- internal/reconcile/session.go | 34 ++++++++------- internal/reconcile/text_test.go | 18 ++++---- internal/reconcile/view.go | 7 +++- 9 files changed, 118 insertions(+), 33 deletions(-) create mode 100644 internal/controlrun/answered_test.go diff --git a/docs/session-signals.md b/docs/session-signals.md index 463c06d..007777f 100644 --- a/docs/session-signals.md +++ b/docs/session-signals.md @@ -9,11 +9,11 @@ Agent state is never scraped from the pane: task state and questions are server A session is **idle** when `waiting`, or `working` with `background` set. Only idle sessions get wake-up pointers. A `waiting` window the harness marks `dialog` is held at a dialog typed input would answer, such as a trust prompt whose default exits: it is never idle, so nothing is typed into it, but its wait counts toward a stall, whose notice says to answer it in the window. -**Stalls.** Limited, queued and background-only sessions never stall. A dispatched session that has not claimed its task 5 minutes after it started is noticed once, and one that ended before claiming is an early death. A session waiting 10 minutes on an open task with no hard stop or question has stopped outside the protocol; the operator is notified once, naming the sign-in for a signed-out harness. A denial at most 10 minutes older than the wait makes the stall a Permission hard stop naming the tool, input and reason. +**Stalls.** Limited, queued and background-only sessions never stall. A session unclaimed after 5 minutes is noticed once; one ended before claiming is an early death. A session waiting 10 minutes on an open task, outside a hard stop or question, has left the protocol: the operator is told once, with the real wait, naming a signed-out harness's sign-in. Time at a hard stop or question, a planner's too, never counts. A denial at most 10 minutes older than the wait makes the stall a Permission stop naming the tool, input and reason. **Hung session.** `status` gives each live window its last activity: the modification time of its harness transcript, whose path the state hook keeps in the run's state file from the hook events, else the pane log's, for a run with no known transcript or a missing file; neither leaves it unknown. The pane log is only the fallback: a live harness redraws its screen every second, so its log never goes quiet. A `working` session, in a turn, of a planner or a task an executor holds, with activity `QuietWait` (60 minutes) old gets one notice per attempt and last activity time, which the attempt keeps as it keeps a stall's start, so only new activity then another quiet hour sends a second. It names the task, host, quiet time and last 20 screen lines, which `status` sends for a working window only once it is that quiet, and offers `engine attach`, `engine tell`, `engine task stop` and `engine task move`. Nothing is stopped. Idle, limited, dialog-held, background-only and unknown-activity sessions are never flagged, nor is a session whose state is unknown. -**Server outage.** Each pass saves its start before it reads hosts, and its end. A pass starting more than 5 minutes after the saved time follows an outage of the server, with no outage on a first pass, when nothing is saved. It clears the wait and idle starts of every live attempt, so stalls count from that pass, and wakes each idle session of a `running` task once for the gap, typing when the server was unavailable and asking it to retry, with their request keys, the engine commands that failed in that time. Sessions waiting on the merge queue, at a hard stop or at a dialog are not woken. The wake goes through the typed-text log, and the attempt keeps the gap it was woken for, so a pass that repeats sends it once; a failed wake is tried again. +**Server outage.** Each pass saves its start before it reads hosts, and its end. A pass starting more than 5 minutes after the saved time follows an outage of the server, with no outage on a first pass, when nothing is saved. It clears every live attempt's wait and idle starts, so stalls count from that pass, and wakes each idle session of a `running` task once for the gap, typing when the server was unavailable and asking it to retry, with their request keys, the engine commands that failed in that time. Sessions waiting on the merge queue, at a hard stop or at a dialog are not woken. The wake goes through the typed-text log, and the attempt keeps the gap it was woken for, so a pass that repeats sends it once; a failed wake is tried again. **Vanish.** A window gone from a host that answered ends an unfinished task's current attempt by the first rule that applies: diff --git a/internal/controlrun/answered_test.go b/internal/controlrun/answered_test.go new file mode 100644 index 0000000..b8a7692 --- /dev/null +++ b/internal/controlrun/answered_test.go @@ -0,0 +1,63 @@ +package controlrun + +import ( + "strings" + "testing" + "time" + + "github.com/dinoderek/engine/internal/control" + "github.com/dinoderek/engine/internal/reconcile" + "github.com/dinoderek/engine/internal/session" +) + +// A session that asks, then waits hours for the answer, is not stalled the +// moment the operator answers: the wait at the hard stop never counts, so +// the stall clock starts at the answer, and its notice gives the real wait. +func TestAWaitAtAnAnsweredQuestionIsNoStall(t *testing.T) { + c, h, _, _, task := setup(t) + pass(t, c) + pass(t, c) + c.Wake = func(reconcile.Session, string) error { return nil } + now := time.Date(2026, 11, 7, 9, 0, 0, 0, time.UTC) + c.Service.Now = func() time.Time { return now } + h.status.Windows[0].State = session.StateWaiting + st, _ := snapshotOf(c.Service.Store) + attempt := st.Items[task.ID].Attempt + worker := control.Actor{Name: attempt, Role: "worker", Attempt: attempt} + out, err := c.Service.Execute(worker, control.Request{Op: "ask", Key: control.Secret(), ID: task.ID, Body: "Which tint?"}) + if err != nil { + t.Fatal(err) + } + // Passes follow each other within OutageGap, or the wait would restart. + for range 10 { + pass(t, c) + now = now.Add(4 * time.Minute) + } + execute(t, c.Service, control.Request{Op: "resolve", ReplyTo: out.Updates[0].ID, Body: "Keep the tint"}) + pass(t, c) + if n := stallNotices(t, c, task.ID); len(n) != 0 { + t.Fatalf("stalled right after the answer: %q", n) + } + for range 3 { + now = now.Add(4 * time.Minute) + pass(t, c) + } + if n := stallNotices(t, c, task.ID); len(n) != 1 || !strings.Contains(n[0], "has waited for input for 12m without asking through the server") { + t.Errorf("notices %q, want one stall timed from the answer", n) + } +} + +func stallNotices(t *testing.T, c *Coordinator, id string) []string { + t.Helper() + st, err := snapshotOf(c.Service.Store) + if err != nil { + t.Fatal(err) + } + var out []string + for _, u := range st.Updates { + if u.Item == id && strings.Contains(u.Body, "waited for input") { + out = append(out, u.Body) + } + } + return out +} diff --git a/internal/controlrun/dialog_test.go b/internal/controlrun/dialog_test.go index 6ad3fe4..26de7e1 100644 --- a/internal/controlrun/dialog_test.go +++ b/internal/controlrun/dialog_test.go @@ -37,7 +37,7 @@ func TestAPlannerHeldAtADialogStallsAndIsNeverTyped(t *testing.T) { later = later.Add(4 * time.Minute) pass(t, c) } - if n := dialogNotices(t, c, plan.ID); len(n) != 1 || !strings.Contains(n[0], "has waited for input for 10m0s: its harness says it is likely held at a dialog in its window") { + if n := dialogNotices(t, c, plan.ID); len(n) != 1 || !strings.Contains(n[0], ": its harness says it is likely held at a dialog in its window") { t.Errorf("notices %q, want one stall naming the dialog", n) } if woke != 0 { diff --git a/internal/controlrun/view.go b/internal/controlrun/view.go index 9a0fbea..fb68d44 100644 --- a/internal/controlrun/view.go +++ b/internal/controlrun/view.go @@ -198,7 +198,13 @@ func (c *Coordinator) keepObservations(v reconcile.View) error { continue } item, _ := tx.Item(t.ID) - tx.PutAttempt(kept(at, *t.Session, v.Now, coordinating(tx, item))) + at = kept(at, *t.Session, v.Now, coordinating(tx, item)) + if t.AtHardStop() { + // The wait is the protocol's: its stall clock starts once + // the operator answers. + at.WaitingSince = time.Time{} + } + tx.PutAttempt(at) } return nil }) diff --git a/internal/reconcile/decide.go b/internal/reconcile/decide.go index 84d6aab..48b64f5 100644 --- a/internal/reconcile/decide.go +++ b/internal/reconcile/decide.go @@ -65,8 +65,8 @@ func planStep(v View) ([]Action, bool) { if acts := limit(v, p); len(acts) > 0 { return acts, true } - if planner.Waited(v.Now, StallWait) && !planner.Flagged(planner.WaitingSince) { - return stall(p, planner.WaitingSince, WaitText(*planner, "the planner "+planner.Name+" has waited for input for "+StallWait.String())), true + if !p.AtHardStop() && planner.Waited(v.Now, StallWait) && !planner.Flagged(planner.WaitingSince) { + return stall(p, planner.WaitingSince, WaitText(*planner, "the planner "+planner.Name, v.Now)), true } return quiet(v, p), true case len(v.Tasks) == 0 && planner != nil: @@ -240,7 +240,7 @@ func releaseMoved(v View) []Action { } // stalls flags a session that has not claimed in time, that died before -// claiming, or that waits outside a hard stop: once per wait, and once per +// claiming, or that waits with no hard stop raised: once per wait, and once per // session that died. A limited session gets its limit step instead. func stalls(v View) []Action { var acts []Action @@ -269,8 +269,8 @@ func stalls(v View) []Action { text = DialogText(text) } acts = append(acts, stall(t, s.Spawned, text)...) - case t.claimed() && t.state() != "needs-human" && t.Question == 0 && s.Waited(v.Now, StallWait) && !s.Flagged(s.WaitingSince): - acts = append(acts, stall(t, s.WaitingSince, WaitText(*s, fmt.Sprintf("%s has waited for input for %s, outside a hard stop", s.Name, StallWait)))...) + case t.claimed() && !t.AtHardStop() && s.Waited(v.Now, StallWait) && !s.Flagged(s.WaitingSince): + acts = append(acts, stall(t, s.WaitingSince, WaitText(*s, s.Name, v.Now))...) } } return acts diff --git a/internal/reconcile/decide_test.go b/internal/reconcile/decide_test.go index f713289..87b91e9 100644 --- a/internal/reconcile/decide_test.go +++ b/internal/reconcile/decide_test.go @@ -123,7 +123,8 @@ func TestDecide(t *testing.T) { {"a planner harness signed out here is a notice, not a start", withFleet(view(plan("approved")), signedOut(fleet(2), "claude")), []string{"notice p1 planner-signin/2/claude"}}, {"a live planner is left to work", view(with(plan("approved"), working())), nil}, {"an unknown planner is left alone", view(with(plan("approved"), Session{Unknown: true})), nil}, - {"a planner waiting outside a hard stop is flagged", view(with(plan("approved"), waiting(StallWait))), []string{fmt.Sprintf("notice p1 stall/a1/%d", unix(-StallWait))}}, + {"a planner waiting with no question is flagged", view(with(plan("approved"), waiting(StallWait))), []string{fmt.Sprintf("notice p1 stall/a1/%d", unix(-StallWait))}}, + {"a planner waiting on its plan question is not flagged", view(question(with(plan("approved"), waiting(StallWait)), 5)), nil}, {"a planned plan's lint failure is a notice", withLint(view(plan("approved"), task(2, "todo"))), []string{"notice p1 lint/2"}}, {"a later plan's lint failure is a notice too", withLint(view(plan("planned"), task(2, "todo"))), []string{"notice p1 lint/2"}}, {"a plan that passes lint is planned", view(plan("approved"), task(2, "todo")), []string{"state p1 planned"}}, @@ -143,7 +144,7 @@ func TestDecide(t *testing.T) { {"an operator question parks a vanished task", view(plan("planned"), vanished(task(2, "running"), 7)), nil}, // Stalls and the claim wait. - {"a held session waiting outside a hard stop is flagged", view(plan("planned"), with(task(2, "running"), waiting(StallWait))), + {"a held session waiting with no hard stop raised is flagged", view(plan("planned"), with(task(2, "running"), waiting(StallWait))), []string{fmt.Sprintf("notice t2 stall/a2/%d", unix(-StallWait))}}, {"a flagged wait is not flagged again", view(plan("planned"), with(task(2, "running"), flagged(waiting(StallWait)))), nil}, {"a short wait is not flagged", view(plan("planned"), with(task(2, "running"), waiting(StallWait-time.Second))), nil}, diff --git a/internal/reconcile/session.go b/internal/reconcile/session.go index 91a1ca4..4022515 100644 --- a/internal/reconcile/session.go +++ b/internal/reconcile/session.go @@ -14,7 +14,8 @@ import ( const ( // ClaimWait is how long a started session has to claim its task. ClaimWait = control.ClaimWait - // StallWait is how long a session may wait for input outside a hard stop. + // StallWait is how long a session may wait for input with no hard stop + // raised. StallWait = 10 * time.Minute // DenialLead is how long before a wait was first seen a permission // denial may have been logged and still be the wait's cause: a session @@ -153,15 +154,16 @@ func (s Session) Denied() bool { return s.Denial != nil && !s.WaitingSince.IsZero() && !s.Denial.At.Before(s.WaitingSince.Add(-DenialLead)) } -// WaitText is a waiting session's stall text: plain, unless its host says -// its harness is signed out, which names the sign-in, or the wait follows a -// permission denial. Then it is the Permission hard stop the session did -// not raise, naming the tool, its exact input and why. A signed-out wait -// leads with the sign-in and names the denial too. A session held at a -// dialog says so instead. -func WaitText(s Session, plain string) string { +// WaitText is a waiting session's stall text, naming who waits and for how +// long by now: plain, unless its host says its harness is signed out, which +// names the sign-in, or the wait follows a permission denial. Then it is the +// Permission hard stop the session did not raise, naming the tool, its exact +// input and why. A signed-out wait leads with the sign-in and names the +// denial too. A session held at a dialog says so instead. +func WaitText(s Session, who string, now time.Time) string { + waited := fmt.Sprintf("%s has waited for input for %s", who, waitedFor(now.Sub(s.WaitingSince))) if s.AtDialog() { - return DialogText(strings.TrimSuffix(plain, ", outside a hard stop")) + return DialogText(waited) } denial := "" if s.Denied() { @@ -173,19 +175,23 @@ func WaitText(s Session, plain string) string { denial = fmt.Sprintf("%s %s was %s", d.Tool, deniedInput(d.Input), why) } if s.SignedOut { - text := fmt.Sprintf("%s, while %s. Sign it in there, then type continue in the window", - strings.TrimSuffix(plain, ", outside a hard stop"), signedOutText(s.Host, s.Harness)) + text := fmt.Sprintf("%s, while %s. Sign it in there, then type continue in the window", waited, signedOutText(s.Host, s.Harness)) if denial != "" { text += ". It also follows a denied permission request, the Permission hard stop (hard-stops.md): " + denial } return text } if denial == "" { - return plain + return waited + " without asking through the server: it may be waiting on you in its window" } - return fmt.Sprintf("%s has waited for input for %s after a denied permission request, the Permission hard stop (hard-stops.md): "+ + return fmt.Sprintf("%s after a denied permission request, the Permission hard stop (hard-stops.md): "+ "%s. Allow it with a permission rule in the session, or do it by hand, then tell the session to carry on", - s.Name, StallWait, denial) + waited, denial) +} + +// waitedFor is a wait's length to the minute, as 10m or 6h16m. +func waitedFor(d time.Duration) string { + return strings.TrimSuffix(d.Round(time.Minute).String(), "0s") } // DialogText is a stall text for a session held at a dialog: the operator diff --git a/internal/reconcile/text_test.go b/internal/reconcile/text_test.go index 8eeb408..b0e64e0 100644 --- a/internal/reconcile/text_test.go +++ b/internal/reconcile/text_test.go @@ -31,9 +31,9 @@ func TestActionTextsAndMarks(t *testing.T) { {"a wait after a denial is the Permission stop", view(plan("planned"), with(task(2, "running"), denied)), []string{"Permission hard stop", "Bash `gh pr merge 3` was denied"}, func(a Action) bool { return a.Stalled.Equal(denied.WaitingSince) && a.Attempt == "a2" }}, {"a wait while signed out names the sign-in", view(plan("planned"), with(task(2, "running"), signedOutWait())), - []string{"demo-2 has waited for input for 10m0s, while claude is signed out on devbox", "engine setup --host devbox", "type continue"}, nil}, + []string{"demo-2 has waited for input for 10m, while claude is signed out on devbox", "engine setup --host devbox", "type continue"}, nil}, {"a planner waiting while signed out keeps its name", view(with(plan("approved"), signedOutWait())), - []string{"the planner demo-1 has waited for input for 10m0s, while claude is signed out on devbox"}, nil}, + []string{"the planner demo-1 has waited for input for 10m, while claude is signed out on devbox"}, nil}, {"a signed-out wait leads with the sign-in and names the denial", view(plan("planned"), with(task(2, "running"), deniedAndSignedOut())), []string{"while claude is signed out on devbox", "It also follows a denied permission request, the Permission hard stop (hard-stops.md): Bash `gh pr merge 3` was denied"}, func(a Action) bool { return strings.Index(a.Text, "signed out") < strings.Index(a.Text, "Permission") }}, @@ -42,11 +42,15 @@ func TestActionTextsAndMarks(t *testing.T) { {"the planner sign-in notice says it waits", withFleet(view(plan("approved")), signedOut(fleet(2), "claude")), []string{"The planner waits: claude is signed out on devbox", "starts once the harness is signed in"}, nil}, {"a plain wait says how long", view(plan("planned"), with(task(2, "running"), waiting(StallWait))), - []string{"demo-2 has waited for input for 10m0s, outside a hard stop"}, nil}, + []string{"demo-2 has waited for input for 10m without asking through the server: it may be waiting on you in its window"}, nil}, + {"a long wait says its own length, not the threshold", view(plan("planned"), with(task(2, "running"), waiting(6*time.Hour+16*time.Minute+20*time.Second))), + []string{"demo-2 has waited for input for 6h16m without asking"}, nil}, + {"a planner's plain wait says how long", view(with(plan("approved"), waiting(StallWait+time.Minute))), + []string{"the planner demo-1 has waited for input for 11m without asking through the server"}, nil}, {"a planner at a dialog says to answer it", view(with(plan("approved"), atDialog(StallWait))), - []string{"the planner demo-1 has waited for input for 10m0s: its harness says it is likely held at a dialog in its window. Answer it there"}, nil}, + []string{"the planner demo-1 has waited for input for 10m: its harness says it is likely held at a dialog in its window. Answer it there"}, nil}, {"a claimed task at a dialog says to answer it", view(plan("planned"), with(task(2, "running"), atDialog(StallWait))), - []string{"demo-2 has waited for input for 10m0s: its harness says it is likely held at a dialog"}, nil}, + []string{"demo-2 has waited for input for 10m: its harness says it is likely held at a dialog"}, nil}, {"an unclaimed task at a dialog says to answer it", view(plan("planned"), with(task(2, "todo"), atDialog(time.Minute))), []string{"demo-2 has not claimed the task 5m0s after it started: its harness says it is likely held at a dialog in its window"}, nil}, {"the claim wait says so", view(plan("planned"), with(task(2, "todo"), working())), @@ -82,8 +86,8 @@ func TestActionTextsAndMarks(t *testing.T) { func TestAnOldDenialIsAPlainWait(t *testing.T) { s := waiting(StallWait) s.Denial = &hostops.Denial{At: s.WaitingSince.Add(-DenialLead - time.Second), Tool: "Bash"} - if WaitText(s, "plain") != "plain" { - t.Error(WaitText(s, "plain")) + if got := WaitText(s, "demo-2", t0); strings.Contains(got, "Permission") || !strings.Contains(got, "without asking") { + t.Error(got) } } diff --git a/internal/reconcile/view.go b/internal/reconcile/view.go index 3b53b08..fbff97e 100644 --- a/internal/reconcile/view.go +++ b/internal/reconcile/view.go @@ -202,11 +202,16 @@ func (v View) moveHeld(t Item) bool { return ok && (h.Unreachable != "" || h.Status.RunsError != "") } +// AtHardStop reports whether the item waits on the operator: it is in +// needs-human, or has its own unresolved question. A session's wait there +// is the protocol's, so it never counts toward a stall. +func (t Item) AtHardStop() bool { return t.State == "needs-human" || t.Question > 0 } + // Wakeable reports whether a pointer at new updates may be typed into the // item's session: the session is idle, so typing cuts into no turn, and the // item is at no hard stop. func (t Item) Wakeable() bool { - return t.Session != nil && t.Session.Idle() && t.State != "needs-human" && t.Question == 0 && !t.Stopped + return t.Session != nil && t.Session.Idle() && !t.AtHardStop() && !t.Stopped } // observed returns the item's session, or a zero one that is neither live