[Sprint 4] Cost & Context meta — prune context, cap loops, curate captures
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3588747. -->
Reported by: [ajv009](https://www.drupal.org/user/3653917)
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>Sprint 4 — Cost & Context. This roll-up tracks the four structural causes of token explosion and per-turn cost in migrate-site sessions. The headline cost figure: a single recorded session ran $52.82, of which $40.03 (76%) was cache-read tokens — driven by a context window that grew monotonically from 52K to 249K tokens with no pruning. By session end every orchestrator turn re-read 249K tokens of accumulated history at ~$0.37 per turn in cache reads alone. Sprint 2 reduces total session turns through parallelism; Sprint 4 reduces the cost of each turn by pruning context, capping loops, and curating heavy capture artefacts.</p>
<p>The four contributors are independent and each can be fixed in isolation, but they compound: an unbounded Ralph Loop (4-02) drives unbounded context growth (4-03); dual-browser conflict (4-01) drives 195 sleep-based retry calls per session that pad context with noise; computed-styles bloat (4-04) means 215 MB of capture artefacts for just 5 components — a latent flooder if any agent ever reads a file directly instead of using <code>jq</code>.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<p>See each child issue for repro steps specific to that defect.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Land the four child issues in suggested order. Order rationale:</p>
<ul>
<li><strong>4-01 first</strong> (dual browser systems) — fixing <code>agent-browser</code> vs <code>mcp__chrome-devtools__*</code> contention eliminates the 195 sleep-based retry calls and clarifies which tool each phase uses. Lowest-risk change; unblocks clean testing of the other fixes.</li>
<li><strong>4-02 second</strong> (Ralph Loop completion condition) — adding a stop condition prevents the "running forever with no termination" mode that contributed to context bloat. Fix this before pruning work to avoid pruning across infinite iterations.</li>
<li><strong>4-03 third</strong> (context window prune-per-phase) — the highest-leverage cost fix but requires a clean baseline. Fix 4-01 and 4-02 first.</li>
<li><strong>4-04 last</strong> (computed-styles bloat) — well-scoped, single-script change. The user explicitly deferred this at session time; it is re-raised here at P1 because the 215 MB bloat will compound on every full 28-section migration (~1.2 GB) and the fix is risk-free. See 4-04 Open Questions before implementing.</li>
</ul>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ul>
<li>4-01 — Two browser systems in simultaneous use (P2, Bug Report)</li>
<li>4-02 — Ralph Loop has no completion condition (P2, Bug Report)</li>
<li>4-03 — Context window explodes — $52 cost, 249K tokens (P0, Bug Report)</li>
<li>4-04 — Computed-styles bloat — 215 MB for 5 components (P1, Bug Report; was deferred mid-session, re-raised — see 4-04 Open Questions)</li>
</ul>
<h3 id="summary-ui-changes">User interface changes</h3>
<p>None at the sprint level — see child issues.</p>
<h3 id="summary-api-changes">API changes</h3>
<p>None at the sprint level — see child issues.</p>
<h3 id="summary-data-model-changes">Data model changes</h3>
<p>New <code>state.jsonl</code> phase_complete entries per phase (4-03). New <code>completion_criteria</code> field on Ralph Loop state file (4-02). No schema changes to existing migration artefacts.</p>
<h3 id="summary-definition-of-done">Definition of done</h3>
<ul>
<li>SKILL.md designates which phases use <code>agent-browser</code> and which use <code>mcp__chrome-devtools__*</code> — never both in the same phase.</li>
<li>Sleep-based screenshot retry loops are replaced with <code>mcp__chrome-devtools__wait_for</code> conditions.</li>
<li>Ralph Loop has a <code>max_iterations: 3</code> default safety cap documented in the configuration; supports a <code>completion_criteria</code> parameter checked after each iteration; ZSH glob quoting bug in <code>setup-ralph-loop.sh</code> is fixed for URL arguments.</li>
<li>SKILL.md has a mandatory context-prune step after each phase: summarize to <code>state.jsonl</code>, release detailed per-agent output. <code>phase-verifier</code> / <code>artifact-checker</code> output replaces rather than supplements phase detail.</li>
<li>Cost of a full migration run is below $15 (down from $52.82); orchestrator context stays under 100K tokens between Phase 3 and Phase 7.</li>
<li><code>scripts/capture-component-bundle.mjs</code> limits computed-style capture to ~25 layout-relevant properties; no computed-styles JSON exceeds 1 MB; a 5-component run produces under 5 MB total (down from 215 MB).</li>
</ul>
<h3 id="summary-internal-references">Internal references</h3>
<ul>
<li>Sprint folder: <code>RESEARCH-2026-04-28/99-final/ISSUES/Sprint-4-Cost-and-Context/</code></li>
<li>Sprint README: <code>Sprint-4-Cost-and-Context/README.md</code></li>
<li>Top-level issues README: <code>RESEARCH-2026-04-28/99-final/ISSUES/README.md</code></li>
<li>Cost evidence: <code>RESEARCH-2026-04-28/00-existing-issues/prior-deep-analysis-key-points.md</code> (cost breakdown; context growth timeline; P2 recommendation #12)</li>
<li>Token usage detail: <code>RESEARCH-2026-04-28/06-jamie-demo-old/95q5i-disk-state/token-usage-and-timing.md</code></li>
<li>Theme analysis: <code>RESEARCH-2026-04-28/00-existing-issues/issue-themes.md</code> (Theme 4 — context/cost)</li>
<li>Recommendation source: <code>RESEARCH-2026-04-28/99-final/migrate-site-skill-recommendations.md</code></li>
<li>Consolidated issues: <code>RESEARCH-2026-04-28/99-final/consolidated-issues.md</code> (P0-5, P1-1, P2-4, P2-5)</li>
</ul>
<h3 id="summary-open-questions">Open questions</h3>
<ul>
<li>4-04 (computed-styles bloat) was explicitly deferred by the user at L630 of session 77cf5bb9 ("Deeper issues to ignore: ... computed-styles bloat"). The deferral was session-time prioritization, not permanent. Re-raised here at P1 because the bloat compounds on every future full migration. Confirm clearance before implementation.</li>
<li>Sequencing question: 4-03 (context prune) and Sprint 2 parallelism are complementary — fewer turns × cheaper turns. Either ordering works; this sprint targets the per-turn axis.</li>
</ul>
<h3 id="summary-child-issues">Child issues</h3>
<p>Linked child issues for this sprint (auto-rendered with status badges):</p>
<ul>
<li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/canvas_ai_migrations/issues/3588781" title="Status: Closed (fixed)">#3588781: [Sprint 4] Two browser systems in simultaneous use — screenshot confusion</a></span></li>
<li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/canvas_ai_migrations/issues/3588782" title="Status: Closed (fixed)">#3588782: [Sprint 4] Ralph Loop has no completion condition — manual cancel required</a></span></li>
<li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-4"><a href="https://www.drupal.org/project/canvas_ai_migrations/issues/3588783" title="Status: Postponed">#3588783: [Sprint 4] Context window explodes — $52 cost, 249K tokens by session end</a></span></li>
<li><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/canvas_ai_migrations/issues/3588784" title="Status: Closed (fixed)">#3588784: [Sprint 4] Computed-styles bloat — 215 MB for 5 components</a></span></li>
</ul>
issue
GitLab AI Context
Project: project/canvas_ai_migrations
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/canvas_ai_migrations/-/raw/main/README.md — project overview and setup
- https://git.drupalcode.org/project/canvas_ai_migrations/-/raw/main/CLAUDE.md — Claude Code instructions
Repository: https://git.drupalcode.org/project/canvas_ai_migrations
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD