Skip to content

fix(agent): fold large unchanged subtrees in the full-screen fallback - #1797

Open
Varshith-Kali wants to merge 12 commits into
e2e-inc:mainfrom
Varshith-Kali:fix-1788-mcp-fold-unchanged-subtrees
Open

Varshith-Kali wants to merge 12 commits into
e2e-inc:mainfrom
Varshith-Kali:fix-1788-mcp-fold-unchanged-subtrees

Conversation

@Varshith-Kali

@Varshith-Kali Varshith-Kali commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor
  • When an action changes most of the screen, the presenter sends the whole tree again, repeating subtrees the model already holds.
  • The fallback now folds a subtree of 10 or more nodes to its root line with the node count and the revision it was last seen at, when every node in it reads as before with the same node ids.
  • The first screen still goes out whole, observe is untouched, and a truncated listing never folds.
  • A subtree holding the focused node stays listed, and a subtree that lost a node stays listed.

Folded line, from the new test:

 #n2 navigation "Sidebar" (12 nodes inside, unchanged since revision b1)

Issue

Fixes #1788

Verified

Ran it locally: yes

  • packages/e2e unit tests for the presenter, the full unit suite, and the MCP run_code integration suite with E2E_CHROMIUM_SANDBOX=0
  • The two new folding tests fail on main (no folding) and pass here
main this branch
562 chars, 24 lines: the 12-node sidebar repeats in full 427 chars, 14 lines: the sidebar folds to one line, a 24.0% cut on this 23-node fixture

View guided diff

@github-actions github-actions Bot added the size:L Up to 499 changed lines, not counting tests, fixtures, or the lockfile label Nov 14, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

Comment thread packages/e2e/src/agent/screen-update.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

Requires human review: Auto-approval blocked by 1 unresolved issue from previous reviews.

View guided diff | Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 6 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The sidebar folding works well in the new tests, and keeping the folded subtree tied to the screen that last listed it in full is a nice touch. A few things before this can land:

Must-fix

  • A fold can point at a screen the agent no longer has. A truncated full screen records nothing and folds nothing, so the old record survives it while the screen it names gets elided. Repro with the presenter plus compactScreenHistory: b1 opening screen, b2 folds the sidebar against b1, b3 is a truncated full screen (b1 and b2 get elided at the default budget), b4 is a normal fallback. b4 prints #n2 navigation "Sidebar" (12 nodes inside, unchanged since revision b1) while b1 already reads [earlier screen elided ...]. The agent's observe only returns changes, so it can't get the subtree back. Suggested fix: after each full render, keep only the records that render listed or folded and drop the rest (a truncated render clears them all). Each fold then depends on exactly one screen, and the transitive lookup in compactScreenHistory can go.
  • skills/e2e/references/agent.md (and the plugin copy) says "observe still returns the complete tree". For the built-in agent that page describes, observe returns what changed. Only the MCP observe returns the whole tree. Please fix the wording and wrap the line.
  • The changeset describes internals (which revision a fold names, transcript elision). One user-facing sentence is enough, e.g. "When an action changes most of the screen, large subtrees that did not change are shown as one line instead of being repeated."

Nits

  • transcript-compaction.ts parses the fold line with its own regex. Exporting the pattern from screen-update.ts, like FULL_SCREEN_PATTERN, would keep the two in sync.
  • hasFocus repeats the state-bracket regex from withoutFocus.
  • These checks can be removed without any test failing: the comparison with the previous screen in subtreeUnchanged / previousSubtreeIds, the truncated listing recording nothing, pruning records for removed nodes, entry.indent > 0, and the early return for screenshot results whose screens are all kept. Either add tests or drop the code (the digest already covers ids and lines).
  • A digest is computed for every node on every full render. Only subtrees with 10 or more nodes can fold, so only those need one.

Heads-up: whether folding should apply to the built-in agent at all, or only to MCP (what #1788 asks for), is still a maintainer call. MCP-only would drop the transcript-compaction.ts changes and the first must-fix entirely, so hold off on bigger rework until that's settled.

Automated review by Claude Code on behalf of the maintainers.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. All three must-fix items are addressed in 6a17583, plus the nits below.

Must-fix 1, the dangling fold: you were right, and it reproduced against the current head even after 81a99b6. The gap 81a99b6 left is the cross-pass case: b3 carries no fold, so nothing preserves b1 when b3 is compacted, and b4 then folds against a revision that is already elided. I wrote that exact b1/b2/b3/b4 sequence as a regression test first and watched it fail, then implemented your suggested fix: a full render now replaces the fold record with exactly what it listed in full or folded, so a truncated render clears it entirely and b4 lists the sidebar in full again. The new test covers the presenter plus compactScreenHistory end to end.

One place I did not follow the suggestion literally: I kept the transitive lookup in compactScreenHistory. A preserved screen can still contain a fold naming an older screen (a subtree folded inside a folded subtree is the case the existing transitive test pins), so dropping it would leave the same dangling pointer one level down.

Must-fix 2: the skill page now says observe returns what changed since the last screen, and only the MCP observe returns the complete tree every time, in both skills/e2e/references/agent.md and the synced plugin copy, with the line wrapped.

Must-fix 3: the changeset is now your one sentence, nothing else.

Nits: the fold-line pattern is exported from screen-update.ts as FOLD_DEPENDENCY_PATTERN and imported by transcript-compaction.ts; hasFocus now shares the state-bracket parse with withoutFocus through a stateBracket helper; digests are computed only for subtrees with 10 or more nodes.

On the dead checks: I dropped the previous-screen comparison in subtreeUnchanged (with previousSubtreeIds and the now-unread ShownScreen.indent field) because the digest already covers ids and lines, and added a test showing a fold the old check wrongly suppressed when an intervening change was undone. I kept the other four checks and added a test pinning each: the truncated render clearing the record, pruning the record when a subtree leaves the screen, never folding a top-level root, and the screenshot early return not spending the elision budget.

No change on the heads-up: transcript-compaction.ts stays as is, no MCP-only rework.

Verified: focused suites 45/45 green, full e2e unit suite 3667 passed (6 skipped), oxlint and tsc clean, pnpm plugins:check clean.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@okwasniewski

Copy link
Copy Markdown
Member

[claude-opus-5-5] automated review on behalf of @okwasniewski

Re-reviewed at ccd1d61a7. All three must-fixes and every nit are done. The cross-pass dangling fold is fixed: a full render now replaces the records, and the b1-b4 test fails without the fix. The agent.md wording matches the code, and the changeset is user-facing. 3672 unit tests pass, and typecheck and lint are clean. I broke the fold and compaction code 15 ways and a test failed for 13 of them.

One more case to fix before this goes to human review:

  1. A fold can point at text removed by the context-overflow retry. shrinkForOverflow (tool-loop.ts) keeps a depended-on screen but cuts every text to 16,384 chars. The presenter doesn't know about the cut, so later screens keep folding against it. Repro: b1 has about 900 nodes with the sidebar at the end, b2 folds against b1, the overflow retry cuts b1's sidebar, and b3 still names b1. Fix: either have the overflow retry leave depended-on screens uncut, or have it clear the presenter's fold records so the next screen renders in full. Add a test for this.
  2. Missing test: no test covers keeping a depended-on screen when that screen arrived as a plain-text tool result (elideResultScreen, transcript-compaction.ts). Every preservation test uses the opening prompt or a screenshot result, so if that keep-check were removed, no test would fail. Repro: the sidebar is listed again in a text result at b2 and folded at b3.

Nits:

  • screen-update.ts: index > 0 is dead because entry.indent > 0 already covers it.
  • transcript-compaction.ts screenRevision has its own regex for the "Current screen" heading. Export that pattern from screen-update.ts instead.
  • Export FOLD_DEPENDENCY_PATTERN without /g, so a later .test() or .exec() call isn't stateful.
  • agent.md: subtrees fold when unchanged since the screen that last listed them in full, not "since the last screen".

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 9 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

All four items in this review are addressed in a4d54ff, pushed. Focused suites 56/56 green; full unit 3670 passed with 1 pre-existing failure (a doctor CLI timing test that fails identically on the clean head).

  1. Overflow retry: I went with clearing the presenter fold records. The loop now calls onOverflowShrink when it shrinks, and the act executor drops the step presenter records, so the next full render lists everything in full. I chose this over leaving depended-on screens uncut because the retry still needs something to cut: a single huge screen would leave it nothing to shrink. New test: b1 with a ~900-node screen, b2 folds against b1, the retry cuts b1, b3 renders in full. It fails pre-fix (b3 still folded against the clipped screen).

  2. Text-result keep: new test in transcript-compaction.test.ts, sidebar listed again in a text result at b2 and folded at b3. Verified it discriminates: removing the keep-check makes it fail.

Nits: dead index check dropped; the screen-revision pattern is exported from screen-update.ts and shared; FOLD_DEPENDENCY_PATTERN is exported without /g (foldDependencies builds its own global copy for matchAll); agent.md now says subtrees fold when unchanged since the screen that last listed them in full, in both copies.

The built-in vs MCP-only scope call is untouched, as before.

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[claude-opus-5-5] automated review on behalf of @okwasniewski

Re-reviewed at a4d54ff50. The built-in agent is fixed: the records clear inside the overflow retry before the retried request goes out, the clear is per step, and the new test fails without the fix. The plain-text keep check now has a test too. 3676 unit tests pass, and typecheck and lint are clean. I broke the fix 8 ways and a test failed for 6 of them. The 2 that got through are harmless.

One case is left:

  1. A custom loop built from the public pieces still folds against cut text. createGrammarTools without a screen option makes its own presenter (primitives.ts:915, options.screen ?? new ScreenPresenter()), and only default-agent.ts passes onOverflowShrink. createToolLoopExecutor plus createGrammarTools from @e2e-dev/e2e/agent is the documented way to build a custom brain (docs/executors.mdx). Repro at this head, with your foldingBigScreen: the first observe lists the sidebar at b2, the overflow retry cuts b2, and the next observe still prints navigation "Sidebar" (12 nodes inside, unchanged since revision b2. ScreenPresenter isn't exported, so those users can't use the hook either.

    This also adds onOverflowShrink to the exported ToolLoopExecutorOptions as an undocumented public option that exists only for internal plumbing. Suggested fix: drop the callback. Keep a per-context presenter registry that primitives.ts can reach (the way presenterFor works in default-agent.ts), make it the createGrammarTools default, and have tool-loop.ts forget the folds itself when it shrinks. That covers both paths and adds no public API. Add a custom-loop + createGrammarTools case next to the new test in tool-loop-overflow.test.ts.

Optional, and older than this PR: the clip notice says "observe again for what is missing", but observe returns a diff, so on an unchanged screen the model can't get the cut part back. If you reset the whole presenter on shrink (shown as well as the fold records), the next look renders in full, and that fixes this case and the fold case together. That call is up to the maintainer, so it doesn't block this PR.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Fixed in 66e5682, as you suggested: the callback is gone.

The presenter registry moved next to ScreenPresenter in screen-update.ts, keyed per step context. createGrammarTools now defaults to the step shared presenter instead of making its own, so a custom loop built from the public pieces gets the same presenter the built-in agent uses, and the tool loop forgets the folds itself when it shrinks. That covers both paths and adds no public API: onOverflowShrink is dropped from ToolLoopExecutorOptions, and ScreenPresenter stays unexported.

Added a custom-loop case next to the built-in fold test in tool-loop-overflow.test.ts: a createToolLoopExecutor plus createGrammarTools loop over the same folding screen. It failed before the fix (the second observe still folded the sidebar against cut b2) and passes now, with the sidebar listed in full. The built-in fold test still passes, and the focused agent tests, typecheck, and lint are clean.

Left the optional whole-presenter reset alone, as you said that is the maintainer call.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

Comment thread packages/e2e/src/agent/primitives.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[claude-opus-5-5] automated review on behalf of @okwasniewski

Re-reviewed at c1db818b7. Last round's asks are done. onOverflowShrink is gone, the registry lives next to ScreenPresenter, createGrammarTools defaults to the step presenter, and the tool loop clears folds on shrink. The public API is unchanged vs main. The WeakMap is keyed by the per-step context, so nothing leaks. The full unit suite passes (3677), and lint and typecheck are clean. Reverting the clear or the shared default fails the new tests.

One regression is left:

  1. Two custom loops that share one context now see a screen they were never shown (primitives.ts:917, screen-update.ts:330-342).
    • Repro: a custom runStep that calls loopA.runStep(ctx) and then loopB.runStep(ctx), each with tools: (c) => createGrammarTools(c).
    • This PR: loop B's first observe returns Screen unchanged since revision b2 ..., a screen loop B's model never saw.
    • Main: loop B gets the full Current screen (revision b4, ...).
    • The same thing happens with any custom executor that reruns a loop inside one step. Presenter state has to match one transcript, and a loop run owns the transcript, not the step context.
    • Fix: have LoopRun bind a fresh presenter for its context at the start of run(), before tools() and buildPrompt, and clear it on shrink through its own reference. presenterFor stays as the fallback for tools used outside a loop. Add the two-loop case as a test.

Nits:

  • default-agent.ts:146 passes screen: presenterFor(context), which is now the default. Removing it changes nothing, so drop it, along with the line about it in the registry doc at screen-update.ts:327.
  • The comment at tool-loop.ts:568-572 repeats the clearPresenterFolds JSDoc. Cut it to one line or remove it.

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Fixed the two-loop regression and both nits at b816f35.

LoopRun now binds a fresh presenter for its context at the start of run(), before tools() and buildPrompt, through a new bindPresenter in screen-update.ts, and clears its folds on overflow shrink through its own reference. presenterFor stays as the fallback for tools used outside a loop. The two-loop case is covered by a new test in tool-loop-overflow.test.ts: one custom runStep runs two createGrammarTools loops against the same step context on an unchanged screen. It failed before the fix, with loop B's first observe returning "Screen unchanged since revision b4 (re-observed as revision b6)", and passes now, with loop B getting the full Current screen listing and the sidebar in full.

Nits: dropped the redundant screen option from default-agent.ts and the line about it from the registry doc, and cut the tool-loop.ts overflow comment to one line. The now unused clearPresenterFolds helper is removed.

Verified: focused agent and tool-loop files 146 tests pass, full unit suite 3673 passed with 6 skipped and 0 failed (baseline at c1db818 was 3672 passed, so the delta is the one new test), typecheck and lint clean, dead-code check clean.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 4 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

View guided diff | Re-trigger cubic

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Automated review.

Re-reviewed at b816f354b. The per-run binding works: tool-loop.ts:314 binds before tools()/buildPrompt, and :575 clears folds through the run's own reference. The two-loop test fails without the bind. Nested loops are fine. The registry is a WeakMap with no new public exports, the folding/compaction chain is correct as far as I traced, and 13 of 16 key-line mutants are killed.

Must-fix: grammar tools used outside a loop now share screen state. That use is documented (docs/executors.mdx:101, the public.ts header). Nothing undoes bindPresenter (screen-update.ts:351), and the presenterFor fallback (primitives.ts:917) creates and stores one shared presenter for every toolset built from the context. Repro, PR vs merge-base:

  • A custom runStep builds createGrammarTools(ctx) twice, once per model call, and calls observe on each. On the PR the second call gets Screen unchanged since revision b2 (re-observed as revision b4), a screen its model never saw. On main it gets the full Current screen (revision b4, ...).
  • loop.runStep(ctx) followed by createGrammarTools(ctx).observe in the same step shows the same "unchanged since b2" output. Main shows the full screen.

Suggested fix:

  • In run(), save the previous binding and restore it (or delete the entry) in finally.
  • In verbKit, use options.screen ?? boundPresenter(context) ?? new ScreenPresenter(), where boundPresenter only reads.
  • Keep the creating presenterFor for the opening prompt inside a run.
  • Add tests for both cases above.

Nits:

  • tool-loop.ts:310-313: the comment repeats the bindPresenter JSDoc. Cut it to one line.
  • primitives.ts:915-916 and the screen JSDoc at :230-232 ("the step's shared presenter") will be wrong after the fix. Reword them.
  • Untested: a marker line inside a subtree that closes a span (screen-update.ts:452). Add a test so a fold can't swallow lines past it.

Still a maintainer call: whether folding should apply to every agent or only to MCP.

…e2e-inc#1788)

When an action changes most of the screen, the presenter sends the whole tree again, repeating subtrees the model already holds. A persistent sidebar can repeat hundreds of unchanged nodes on every page transition. The fallback now folds a subtree of 10 or more nodes to its root line with the node count and the revision it was last seen at, when every node in it reads as before with the same node ids. The first screen still goes out whole, observe is untouched, and a truncated listing never folds. A subtree holding the focused node stays listed.
Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
…ugh elision

A folded subtree used to name the last observation's revision, but that observation may have been rendered as a diff that never listed the subtree, or may itself have folded it. The presenter now remembers, per subtree root, the revision that last listed it in full and a digest of what it read then. A fold line names that revision, and only when the subtree still reads the same: a subtree that changed in between is listed in full again, which puts it on record at the new revision.

compactScreenHistory preserves every screen a surviving folded screen's fold lines depend on, following the dependency transitively, so the elision notice stays true. The stale-bytes budget now counts only the screens that may be elided.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
The fold-line change updated skills/e2e/references/agent.md but the
generated plugins/e2e copy was not re-synced, so pnpm plugins:check
failed on the PR. Regenerated with pnpm plugins:sync.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
…ndant previous-screen check

A truncated full screen recorded nothing and folded nothing, so the old fold record survived it while the screen it named could already be elided from the transcript: the next fallback then folded against a revision the model no longer holds. A full render now replaces the record with exactly what it listed in full or folded, so a truncated render clears it entirely and a later fold can never name a screen that render did not show.

The comparison with the previous screen in the fold check is gone: the digest already covers the subtree node ids and lines, and the check could suppress a valid fold when an intervening change was undone. The focused node guard stays, as its own check. Digests are now computed only for subtrees large enough to fold, the fold-line pattern is exported from screen-update.ts for the transcript elision to share, and hasFocus shares the state-bracket parse with withoutFocus.

The skill reference now says observe returns what changed for the built-in agent, with the whole tree only for the MCP observe, and the changeset is one user-facing sentence.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
The screenshot-result test claimed the kept b1 and b4 screens spend none
of the elision budget, but with keepStaleBytes 0 any elidable screen
triggers elision, so counting the kept screens in the budget produced
the same output. Pass a budget that exactly covers the elidable b2 and
b3 screens instead: the transcript stays untouched only when the kept
screens spend none of it. The keepStaleBytes 0 pass still pins the
screenshot result returning untouched while the elidable screens go.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
…result keep

The overflow retry clips every screen to its head, including one a
surviving fold line names, so the next screen kept folding against text
the model no longer holds. The loop now tells the act executor when it
shrinks, and the executor drops the step presenter's fold records, so
the next full render lists everything in full. Clearing the records
instead of leaving depended-on screens uncut keeps the retry able to
shrink: a single huge screen would otherwise leave it nothing to cut.
The same change adds the missing test for a depended-on screen that
arrived as a plain-text tool result, drops the dead index check in the
fold, shares the screen-revision pattern instead of duplicating its
regex, exports the fold pattern without /g so later .test() calls stay
stateless, and fixes the agent.md fold wording.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
…on overflow shrink

A custom loop built from createGrammarTools made its own presenter, so the overflow retry that cut the screens its folds named never cleared them: the next observe still folded the sidebar against a cut revision. The presenter registry now lives beside ScreenPresenter, createGrammarTools defaults to the step's shared presenter, and the loop forgets the folds itself when it shrinks, which covers the built-in agent and a custom loop alike and drops the onOverflowShrink callback from the public options. A custom-loop case next to the built-in fold test fails before this change and passes after it.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
The GrammarToolSetup screen doc still said a tool set without screen gets a fresh presenter per step. Since the shared-presenter change, the default is presenterFor(context), the step's shared presenter, the same one the opening prompt uses. The old wording also implied the first look returns the whole screen, which no longer holds once the opening prompt has shown it.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
Two custom loops sharing one step context shared one presenter, so the second loop's first observe reported the first loop's screen unchanged, a screen its model never saw. A loop run now binds a fresh presenter for its context at the start of run, before tools and buildPrompt, and clears its folds on overflow shrink through its own reference. presenterFor stays as the fallback for tools used outside a loop. Adds the two-loop case to the overflow tests, and drops the now redundant screen option in the built-in agent.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
Grammar tools used outside a loop shared the presenter's state: bindPresenter was never undone, and presenterFor created and stored one shared presenter for every toolset built from the context. A second toolset's first observe reported the first toolset's screen unchanged, a screen its model never saw, and the same happened for tools built after a loop run. A loop run now saves the previous binding and restores it in finally, and verbKit reads the bound presenter without creating one, falling back to a fresh presenter of its own per toolset. presenterFor stays as the fallback for the opening prompt inside a run. Adds both repro cases to the overflow tests, plus a fold test proving a marker line inside a subtree keeps the lines past it listed in full. Also cuts the bindPresenter comment in run to one line and rewords the screen option docs, which named a shared presenter that no longer exists.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali
Varshith-Kali force-pushed the fix-1788-mcp-fold-unchanged-subtrees branch from b816f35 to 7a72306 Compare November 15, 2026 19:47
@github-actions github-actions Bot added size:XL Up to 999 changed lines, not counting tests, fixtures, or the lockfile and removed size:L Up to 499 changed lines, not counting tests, fixtures, or the lockfile labels Nov 15, 2026
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Fixed the shared-presenter regression and the three nits, and rebased onto current main.

The must-fix: a loop run now saves the previous presenter binding and restores it in a finally block in run(), so tools built outside a loop never inherit a run's presenter. verbKit reads the bound presenter without creating one (boundPresenter), and falls back to a fresh presenter per toolset when no run is in progress. presenterFor stays as the fallback for the opening prompt inside a run. Both repro cases from the review are now tests in tool-loop-overflow.test.ts: two toolsets built outside a loop each get their own first screen, and tools built after a loop run get theirs too. Both failed on the old head with the exact unchanged-since-b2 output from the review, and pass now.

Nits: the bind comment in run() is one line; the screen option doc and the verbKit comment no longer call it the step's shared presenter; and the marker-line fold case has a test in agent-screen-update.test.ts, where a fold over the eleven links before the marker leaves the marker line and the twelfth link listed in full.

Also rebased onto current main, including the core move to @e2e-dev/core: the fix commits are unchanged in content, only the import paths moved. Full unit suite: 3708 passed, 6 skipped; typecheck and lint clean on core and e2e.

The scope question on folding for every agent vs MCP only is untouched, still yours to call.

@okwasniewski okwasniewski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Automated review.

Thanks, re-reviewed at 7a72306a7. All earlier asks are done: reverting each fix (fresh fallback in verbKit, restore in finally, marker pop in spanEnds, clearFoldRecords) makes its new test fail. The rebase onto core is clean, public API unchanged, CI green.

One remaining issue:

Concurrent runs on one context leave a stale binding (tool-loop.ts:311-317, restorePresenter). Each run restores the binding it saw at start, so when two runs overlap, the last one to end puts back a presenter whose run is over:

  • A starts (saves nothing, binds pA), B starts (saves pA, binds pB), A ends (deletes), B ends (re-binds pA).

Repro: a custom runStep doing Promise.all([loopA.runStep(ctx), loopB.runStep(ctx)]) (both on createGrammarTools), then createGrammarTools(ctx).observe returns Screen unchanged since revision ... instead of the full screen. On main that toolset always got a fresh presenter. Relatedly, default-agent.ts:164 reads presenterFor(context) after await context.observe(), so an overlapping run's opening prompt can use the other run's presenter.

Suggested fix: a per-context stack (bind pushes, finally removes this run's own presenter by identity, boundPresenter reads the top), and read the bound presenter in the built-in buildPrompt before the first await. Please add tests for Promise.all of two runs and for a loop nested inside another loop's tool on the same context (today no test fails if a run reuses an outer binding instead of binding fresh).

Nits:

  • primitives.ts:230: the screen JSDoc still says the built-in agent shares it with the opening prompt; only e2e mcp passes it now.
  • primitives.ts:901 and screen-update.ts:336 say "the step's presenter"; it is the loop run's presenter now.

Still a maintainer call: fold for every agent vs MCP only.

…eave no stale binding

A loop run restored the single binding it saw at start, so when two runs
overlapped the last one to end put back a presenter whose run was over, and
tools built afterwards read that run's screen as unchanged. The per-context
binding is now a stack: bind pushes a fresh presenter, the run removes its
own presenter by identity when it ends, and boundPresenter reads the top.
A run that ends while another run is still bound leaves that run's
presenter in place, and a run that ends after an overlapping run never puts
back a presenter whose run is over.

The built-in agent also reads its run's presenter in buildPrompt before the
first await, so an overlapping run's opening prompt cannot pick up the other
run's presenter. The screen option doc now says only e2e mcp passes a
presenter, and the stale step's presenter wording is corrected to the loop
run's presenter.

Tests: a Promise.all of two loop runs followed by an observe on tools built
afterwards, and a loop nested inside another loop's tool on the same
context. The concurrent test fails on the old code (the later observe
returns Screen unchanged since revision b2); the nested test fails if a run
reuses the outer binding instead of binding fresh.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Fixed, thanks for the catch.

The per-context binding is now a stack (screen-update.ts): bind pushes a fresh presenter, the run removes its own presenter by identity in finally, and boundPresenter reads the top. A run that ends while another run is still bound leaves that run's presenter in place, and a run that ends after an overlapping run never puts back a presenter whose run is over, so tools built after both runs find no binding and get a fresh presenter. presenterFor now returns the bound top or a fresh unstored presenter.

The built-in agent reads its run's presenter in buildPrompt before the first await in both the act and assertion loops (default-agent.ts), so an overlapping run's opening prompt cannot pick up the other run's presenter. openingPrompt takes the presenter as a parameter now.

Tests (tool-loop-overflow.test.ts): a Promise.all of two loop runs on one context followed by an observe on tools built afterwards, and a loop nested inside another loop's tool on the same context. The concurrent test fails on the old code (the later observe returns 'Screen unchanged since revision b2'); the nested test fails if a run reuses the outer binding instead of binding fresh. Both pass with the fix.

Nits: the screen option doc in primitives.ts now says only e2e mcp passes a presenter, and the 'step's presenter' wording in primitives.ts and screen-update.ts now says the loop run's presenter.

The fold-scope question (every agent vs MCP only) is still yours to call, so I left that untouched.

Verified: full e2e unit suite passes (221 files, 3710 tests, 6 skipped), core and e2e typecheck clean, oxlint clean on the changed files. New head: eb3d233.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 5 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/core/src/agent/default-agent.ts">

<violation number="1" location="packages/core/src/agent/default-agent.ts:85">
P2: The reordering that keeps an overlapping run's opening prompt on its own presenter has no regression test. The concurrent-run test uses custom `buildPrompt` functions, so the built-in act loop's `buildPrompt` is never exercised with two overlapping runs. Add a test with two `createBuiltInAgent` runs on one context whose `observe` resolves at different times, and check each opening prompt.</violation>
</file>

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

Requires human review: Auto-approval skipped because cubic reviewed only this push, not the earlier force-push. Comment @cubic review to review the whole pull request.

View guided diff | Re-trigger cubic

// this context may bind its own presenter while this one waits on
// observe, and the opening prompt must read this run's, not the other
// run's.
const presenter = boundPresenter(context) ?? presenterFor(context);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The reordering that keeps an overlapping run's opening prompt on its own presenter has no regression test. The concurrent-run test uses custom buildPrompt functions, so the built-in act loop's buildPrompt is never exercised with two overlapping runs. Add a test with two createBuiltInAgent runs on one context whose observe resolves at different times, and check each opening prompt.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/core/src/agent/default-agent.ts, line 85:

<comment>The reordering that keeps an overlapping run's opening prompt on its own presenter has no regression test. The concurrent-run test uses custom `buildPrompt` functions, so the built-in act loop's `buildPrompt` is never exercised with two overlapping runs. Add a test with two `createBuiltInAgent` runs on one context whose `observe` resolves at different times, and check each opening prompt.</comment>

<file context>
@@ -78,6 +78,11 @@ function createActLoop(options: BuiltInAgentOptions): StepExecutor {
+      // this context may bind its own presenter while this one waits on
+      // observe, and the opening prompt must read this run's, not the other
+      // run's.
+      const presenter = boundPresenter(context) ?? presenterFor(context);
       // The opening look is tree-only, unless no listed interactive node
       // exists at all: then the opening prompt carries a screenshot, because the tree describes a canvas, a game,
</file context>

@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

CI note on head eb3d233: the only failing test job is test (24.8.0), and its single failure is unrelated to this change. It is tests/executor.test.ts, acp, preserves cancellation while waiting for the previous turn to stop, failing in the test's own temp-dir cleanup with ENOTEMPTY on /tmp/e2e-acp-test-L4wBzp (rmSync in the test cleanup at executor.test.ts:25). This PR touches only the agent loop presenter files, not packages/acp, and the same job passes on the other Node versions. The spec gate failure follows from that one job. Looks like a flaky cleanup race on the runner; a re-run should clear it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL Up to 999 changed lines, not counting tests, fixtures, or the lockfile

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web]: MCP full-screen fallback repeats unchanged navigation subtrees

2 participants