diff --git a/docs/knowledge/findings/spa-transition-hides-body-behind-overlay.md b/docs/knowledge/findings/spa-transition-hides-body-behind-overlay.md index a42f5725..4dfa13aa 100644 --- a/docs/knowledge/findings/spa-transition-hides-body-behind-overlay.md +++ b/docs/knowledge/findings/spa-transition-hides-body-behind-overlay.md @@ -5,7 +5,7 @@ description: On a signed-in site, a client-side transition from one app to anoth tags: [benchmark, spa, snapshot, jev-browse, cuttle-pw, orchestrator] status: stable stale_after: "2027-03-19T00:00:00+00:00" -generated: { by: claude-code/claude-opus-5, at: "2026-09-19T14:05:00+01:00" } +generated: { by: claude-code/claude-opus-5, at: "2026-09-19T14:28:00+00:00" } sources: - id: bench resource: "benchmark harness runs on 2026-09-19 (merged main at 9ca9386) against a signed-in site: the same 12-click flow as the orchestrator-effort benchmark, humanize on, run logs of every arm (session record, no durable link)" @@ -37,10 +37,13 @@ Two traps, then: 1. **For an orchestrator**: a near-empty `innerText` after a navigation is not the page. Take a snapshot or reload before concluding the page is broken. -2. **For jev-browse**: its done-check trusts URL and title while the DOM can - still hold the previous page or an overlay. Pre-existing, not introduced - by the speed work; a fix would confirm on snapshot content, not on - URL+title. +2. **For jev-browse**: its done-check trusted URL and title while the DOM + could still hold the previous page or an overlay. Pre-existing, not + introduced by the speed work; closed by + [#108](https://github.com/glim-sh/cuttle/pull/108), where `settle` takes + a read whose URL moved but whose element handles still equal the action + page's as unsettled and re-reads until they move or the settle deadline + passes, so the judgement sees the page that landed. Where this bit the benchmarks: [orchestrator effort is the browse-time lever](/findings/orchestrator-effort-is-the-browse-time-lever.md), diff --git a/packages/cuttle/internal/jev/decide.go b/packages/cuttle/internal/jev/decide.go index 6a2ebb92..25c296b4 100644 --- a/packages/cuttle/internal/jev/decide.go +++ b/packages/cuttle/internal/jev/decide.go @@ -350,6 +350,14 @@ func isRef(key string) bool { return key != noneKey && key != backKey && key != enterKey && !strings.HasPrefix(key, typeKeyPrefix) } +// noneDoneThreshold is the bar `done` must clear when the pick is `none`. The +// model saying that no action makes progress AND that the page is more likely +// than not the target is one reading - the run is on the page and there is +// nothing left to do on it - so even odds is enough where a pick that keeps +// going would need doneThreshold. Without it a run that reached the target +// ended blocked on it, with done at 0.6 in its own step log. +const noneDoneThreshold = 0.5 + // decision is one step's worth of judgement, already collapsed from the // parallel answers. type decision struct { @@ -359,6 +367,14 @@ type decision struct { Confidence float64 } +// finished reports whether the answers end the run as done. The lower bar a +// `none` gets yields to a blocked answer that clears the full one: "nothing to +// do here" under a confident "reaching the task needs what the agent cannot do" +// is a wall, not the goal, and the per-step switch reads it as blocked. +func (dec decision) finished() bool { + return dec.Done >= doneThreshold || (dec.Key == noneKey && dec.Done >= noneDoneThreshold && dec.Blocked < doneThreshold) +} + // answered reads one question's answer. A question id that came back missing is // a protocol failure, not a verdict: read as a zero value it says "not done, not // blocked, nothing worth clicking" and would end the run as though the page had diff --git a/packages/cuttle/internal/jev/loop.go b/packages/cuttle/internal/jev/loop.go index b04d2ad8..7358ba62 100644 --- a/packages/cuttle/internal/jev/loop.go +++ b/packages/cuttle/internal/jev/loop.go @@ -29,7 +29,8 @@ const ( // action. There are no fixed sleeps anywhere in the loop: a navigation costs one // read, an in-page change that is already still costs two reads and the one poll // between them, and one that never stops changing - a spinner, a polling widget - -// costs the deadline and then gets acted on anyway. +// or a navigation whose body never follows its address costs the deadline and +// then gets acted on anyway. const ( settlePoll = 250 * time.Millisecond settleDeadline = 5 * time.Second @@ -266,7 +267,7 @@ func (l *loop) run(ctx context.Context) (int, error) { var err error // The page the last action was taken on; the first step has none, and // settles the long way. - snap, err = l.settle(ctx, snap.URL) + snap, err = l.settle(ctx, snap) if err != nil { return ExitError, err } @@ -289,7 +290,7 @@ func (l *loop) run(ctx context.Context) (int, error) { } switch { - case dec.Done >= doneThreshold: + case dec.finished(): l.report(step, snap, dec, "done") return l.done(ctx, snap) case dec.Blocked >= doneThreshold: @@ -322,7 +323,7 @@ func (l *loop) run(ctx context.Context) (int, error) { // // That read is also the first look at where the last action landed, and a // budget that ran out ON the goal page is a task done, not one given up on. - if final, err := l.settle(ctx, snap.URL); err == nil { + if final, err := l.settle(ctx, snap); err == nil { snap = final if snap.Modal != "" { return l.parked(ctx, snap), nil @@ -330,7 +331,7 @@ func (l *loop) run(ctx context.Context) (int, error) { candidates := l.offer(snap) // A failed judgement here costs only the upgrade: the budget did run out. // It is not a step of its own, so it prints none: the outcome says done. - if dec, err := decide(ctx, l.transport, l.state(snap, candidates), group(candidates)); err == nil && dec.Done >= doneThreshold { + if dec, err := decide(ctx, l.transport, l.state(snap, candidates), group(candidates)); err == nil && dec.finished() { return l.done(ctx, snap) } } @@ -379,11 +380,17 @@ func (l *loop) state(snap Snapshot, candidates []candidate) state { // settle reads the page until it stops changing: two consecutive snapshots with // the same identity and the same handles. It is the only thing between an action // and the next decision, and it replaces the fixed sleep a loop would otherwise -// need after every click. from is the URL the action was taken on: a read that -// has left it is a navigation, and the driver only answers one once the new +// need after every click. from is the page the action was taken on: a read that +// has left its URL is a navigation, and the driver only answers one once the new // document - or an SPA's new route - is there, so it is taken without a second, -// confirming read. An empty from always takes the two reads. -func (l *loop) settle(ctx context.Context, from string) (Snapshot, error) { +// confirming read - unless it still carries from's own handles. A client-side +// transition between two apps of a site changes the URL and the title first and +// swaps the body in seconds later, and a read in between is the old page under +// the new address: judged as it stands, its url and title say the target is +// reached about elements that belong to the page before it. Such a read is not +// settled, and is re-read until the handles move or the deadline passes. A zero +// from always takes the two reads. +func (l *loop) settle(ctx context.Context, from Snapshot) (Snapshot, error) { deadline := l.now().Add(settleDeadline) var prev Snapshot settled := false @@ -397,10 +404,11 @@ func (l *loop) settle(ctx context.Context, from string) (Snapshot, error) { if snap.Modal != "" { return snap, nil } - if from != "" && snap.URL != "" && snap.URL != from { + moved := from.URL != "" && snap.URL != "" && snap.URL != from.URL + switch { + case moved && snap.handles() != from.handles(): return snap, nil - } - if settled && prev.signature() == snap.signature() { + case !moved && settled && prev.signature() == snap.signature(): return snap, nil } prev, settled = snap, true @@ -471,7 +479,7 @@ func (l *loop) act(ctx context.Context, snap Snapshot, chosen candidate) error { entry := Step{URL: snap.URL, Action: chosen.Label} failure := l.perform(ctx, snap, chosen.Key) if failure != nil { - fresh, err := l.settle(ctx, snap.URL) + fresh, err := l.settle(ctx, snap) if err != nil { return err } @@ -499,7 +507,7 @@ func (l *loop) perform(ctx context.Context, snap Snapshot, key string) error { if out, err := l.Driver(ctx, "go-back"); err != nil { return driverErr(out, err) } - return l.remintAfterBack(ctx, snap.URL) + return l.remintAfterBack(ctx, snap) case key == enterKey: out, err := l.Driver(ctx, "press", "Enter") return driverErr(out, err) @@ -530,7 +538,7 @@ func (l *loop) perform(ctx context.Context, snap Snapshot, key string) error { // does not recover; only a fresh `goto` re-mints working refs. `back` is offered // on every page, so without this one back would brick every later click. Re-check // the defect when that pin moves, and delete this once it is fixed upstream. -func (l *loop) remintAfterBack(ctx context.Context, from string) error { +func (l *loop) remintAfterBack(ctx context.Context, from Snapshot) error { landed, err := l.settle(ctx, from) if err != nil { return err diff --git a/packages/cuttle/internal/jev/loop_test.go b/packages/cuttle/internal/jev/loop_test.go index 16f15507..414ab480 100644 --- a/packages/cuttle/internal/jev/loop_test.go +++ b/packages/cuttle/internal/jev/loop_test.go @@ -450,6 +450,33 @@ func TestRunHoldsTheNoulsToTheThreshold(t *testing.T) { } } +// A `none` with done above even odds is the run standing on the target with +// nothing left to do, not a run that is stuck: it ends done. Below even odds +// `none` means what it says - and so does a confident blocked, which the lower +// bar must not talk over: that is a wall at the target's address, not the goal. +func TestRunEndsDoneOnANoneThatRatesThePageAsLikelyDone(t *testing.T) { + for _, tc := range []struct { + done, blocked float64 + wantCode int + }{ + {0.6, 0, ExitDone}, + {0.4, 0, ExitBlocked}, + {0.6, 0.9, ExitBlocked}, + } { + tr := &scriptedTransport{rounds: []map[string]answer{{ + questionDone: {Noul: tc.done}, questionBlocked: {Noul: tc.blocked}, "pick0": {Choice: noneKey, Confidence: 1}, + }}} + d := &fakeDriver{pages: []string{readFixture(t, "signin.snapshot")}} + res := runLoop(t, d, Options{transport: tr, MaxSteps: 1}) + if res.err != nil || res.code != tc.wantCode { + t.Errorf("done=%.1f blocked=%.1f with none: got code %d, err %v, want %d:\n%s%s", tc.done, tc.blocked, res.code, res.err, tc.wantCode, res.stdout, res.stderr) + } + if d.actions() != nil { + t.Errorf("done=%.1f blocked=%.1f with none: the loop acted anyway: %q", tc.done, tc.blocked, d.actions()) + } + } +} + // fakeClock makes the settle deadline decidable without waiting for it: sleeping // is what moves time here, so a test that never sleeps never ages. type fakeClock struct{ t time.Time } @@ -477,7 +504,7 @@ func TestSettleWaitsUntilTwoReadsAgree(t *testing.T) { if err != nil { t.Fatalf("newLoop: %v", err) } - snap, err := l.settle(context.Background(), "") + snap, err := l.settle(context.Background(), Snapshot{}) if err != nil { t.Fatalf("settle: %v", err) } @@ -499,19 +526,24 @@ func TestSettleWaitsUntilTwoReadsAgree(t *testing.T) { // is a whole driver spawn per step. A read still on that page takes the two. func TestSettleTakesOneReadAfterANavigation(t *testing.T) { signin := readFixture(t, "signin.snapshot") - from := ParseSnapshot(signin).URL + from := ParseSnapshot(signin) other := "### Page\n- Page URL: http://x/next\n### Snapshot\n- button \"b\" [ref=e1]\n" + // The sign-in page's own elements under the next page's address and title: + // what a client-side transition shows between changing the URL and swapping + // the body in. + stale := strings.NewReplacer(from.URL, "http://x/next", from.Title, "Next").Replace(signin) for _, tc := range []struct { name string - from string + from Snapshot reads []string wantReads int wantURL string }{ {"navigated", from, []string{other, signin}, 1, "http://x/next"}, {"navigation lands on the second read", from, []string{signin, other, signin}, 2, "http://x/next"}, - {"same page", from, []string{signin, signin}, 2, from}, - {"no page to compare with", "", []string{other, other}, 2, "http://x/next"}, + {"same page", from, []string{signin, signin}, 2, from.URL}, + {"no page to compare with", Snapshot{}, []string{other, other}, 2, "http://x/next"}, + {"the address moved before the body", from, []string{stale, stale, other}, 3, "http://x/next"}, } { t.Run(tc.name, func(t *testing.T) { clock := newFakeClock() @@ -533,6 +565,60 @@ func TestSettleTakesOneReadAfterANavigation(t *testing.T) { } } +// A client-side transition that changes the URL and the title and then never +// swaps the body in is judged anyway - at the deadline, the way a spinner is. +func TestSettleGivesUpOnABodyThatNeverFollowsItsAddress(t *testing.T) { + signin := readFixture(t, "signin.snapshot") + from := ParseSnapshot(signin) + stale := strings.NewReplacer(from.URL, "http://x/next", from.Title, "Next").Replace(signin) + clock := newFakeClock() + d := &fakeDriver{reads: []string{stale}} + opts := Options{Task: "t", MaxSteps: 1, transport: &scriptedTransport{}, Driver: d.run} + clock.wire(&opts) + l, err := newLoop(opts) + if err != nil { + t.Fatalf("newLoop: %v", err) + } + snap, err := l.settle(context.Background(), from) + if err != nil { + t.Fatalf("settle: %v", err) + } + if snap.URL != "http://x/next" || !clock.reached(settleDeadline) { + t.Errorf("settled on %q after %s, want the moved page at the %s deadline", snap.URL, clock, settleDeadline) + } +} + +// The done judgement gets no page text: it reads the url, the title and the +// elements. A read taken while a client-side transition has changed the first +// two and not yet the body would hand it the previous page's elements under the +// target's address, and the address is what it would believe. The judgement has +// to receive the page that landed. +func TestRunJudgesDoneOnTheElementsThatLanded(t *testing.T) { + signin := readFixture(t, "signin.snapshot") + from := ParseSnapshot(signin) + stale := strings.NewReplacer(from.URL, "http://x/next", from.Title, "Next").Replace(signin) + landed := "### Page\n- Page URL: http://x/next\n- Page Title: Next\n### Snapshot\n- button \"b\" [ref=e1]\n" + tr := &scriptedTransport{rounds: []map[string]answer{ + {"pick0": {Choice: "f2e11", Confidence: 0.9}}, + {questionDone: {Noul: 0.95}}, + }} + d := &fakeDriver{reads: []string{signin, signin, stale, landed}} + res := runLoop(t, d, Options{transport: tr}) + if res.err != nil || res.code != ExitDone { + t.Fatalf("run: code %d, err %v:\n%s", res.code, res.err, res.stderr) + } + if len(tr.requests) != 2 { + t.Fatalf("requests: got %d, want the pick and the judgement of where it landed", len(tr.requests)) + } + st, _ := tr.requests[1].State.(state) + if st.Page.URL != "http://x/next" || len(st.Elements) != 1 || st.Elements[0].Label != "b" { + t.Errorf("the judgement received page %q with elements %+v, want the landed page's own button", st.Page.URL, st.Elements) + } + if d.read != 4 { + t.Errorf("took %d reads, want the two that settled the start page, the stale one and the one that landed", d.read) + } +} + // A caller that counts the processes behind each Driver call - a lease renew // rides inside a click - gets that count as the step's spawns, not one per call. func TestRunCountsSpawnsFromTheCaller(t *testing.T) { @@ -590,7 +676,7 @@ func TestSettleGivesUpAtTheDeadline(t *testing.T) { if err != nil { t.Fatalf("newLoop: %v", err) } - if _, err := l.settle(context.Background(), ""); err != nil { + if _, err := l.settle(context.Background(), Snapshot{}); err != nil { t.Fatalf("settle: %v", err) } if !clock.reached(settleDeadline) { diff --git a/packages/cuttle/internal/jev/snapshot.go b/packages/cuttle/internal/jev/snapshot.go index 687b8fac..87ad9a02 100644 --- a/packages/cuttle/internal/jev/snapshot.go +++ b/packages/cuttle/internal/jev/snapshot.go @@ -72,9 +72,12 @@ func (s Snapshot) element(ref string) (Element, bool) { // the page plus every handle on it. A navigation, a re-render that mints new // refs, and a control appearing or disappearing all change it; a spinner // rotating does not. -func (s Snapshot) signature() string { +func (s Snapshot) signature() string { return s.URL + s.handles() } + +// handles is every element on the page, without its identity: the same string +// for the same DOM whatever the address bar says. +func (s Snapshot) handles() string { var b strings.Builder - b.WriteString(s.URL) for _, el := range s.Elements { b.WriteString("\x00") b.WriteString(el.Ref)