From b9f91dada930db69c0b5fd73a25ae5e6a24df809 Mon Sep 17 00:00:00 2001 From: nesquena-hermes Date: Sun, 12 Jul 2026 01:58:02 +0000 Subject: [PATCH 1/6] fix(transparent-stream): defer settled tool-detail DOM + cap per-turn rows (#5966) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reported by @curtisszmania-cytocync. Transparent Stream built a DOM node for every activity event of every settled turn at load, and eagerly materialized each tool row's full detail body — a 15+ prompt reasoning-heavy history produced thousands of subtrees and tipped the tab into a multi-GB freeze. The #5860/#5839 lazy-worklog fix did not cover Transparent Stream (it never collapses). Two-part surgical fix in static/ui.js, mirroring #5860's proven machinery: - Row-detail deferral: a settled+collapsed transparent tool row renders header-only; its heavy .tool-card-detail body builds on first expand (_materializeTransparentToolDetail), recovering the tool call from the scene after an innerHTML cache round-trip. Same anchor-suppressed post-processing as the eager path, so an expanded row is byte-identical. - Per-turn row cap: a settled turn with >CAP+SLACK (30+10) rows renders the last 30 behind a clean 'Show earlier steps (N)' affordance that materializes the prefix in place with viewport compensation. Just-settled turn (keep-open token) and already-revealed turns are exempt; true Trace tool-count is stashed so the label stays honest; Expand-all reveals capped rows; cache-restore re-wires the affordance. Live rendering is untouched. Server-side #5858 (scene serialize-after-messages) already covers the container RSS half on the experimental channel; this is the browser-DOM half. --- static/style.css | 42 +++ static/ui.js | 262 +++++++++++++++++- ...t_issue5966_transparent_stream_lazy_dom.py | 218 +++++++++++++++ 3 files changed, 516 insertions(+), 6 deletions(-) create mode 100644 tests/test_issue5966_transparent_stream_lazy_dom.py diff --git a/static/style.css b/static/style.css index 31e517fb366..f85bac0eb41 100644 --- a/static/style.css +++ b/static/style.css @@ -6260,6 +6260,48 @@ main.main > #mainPlugin{display:none;} .load-older-indicator:hover{opacity:.75;} .message-window-load-earlier{align-self:center;margin:4px auto 14px;padding:7px 14px;border:1px solid var(--border);border-radius:999px;background:var(--surface);box-shadow:var(--shadow-sm);} .message-window-load-earlier:hover{background:var(--hover-bg);border-color:var(--accent);} +/* #5966: "Show earlier steps" affordance for a capped Transparent Stream turn. + Sits inline at the top of the turn's activity run, styled as a quiet pill in + the app's own visual language (matches .message-window-load-earlier) but + left-aligned to the activity rail so it reads as part of the run, not the + transcript. Whole row is the click/tap target; >=44px tall for touch. */ +.transparent-earlier-steps{ + display:inline-flex;align-items:center;gap:7px; + margin:2px 0 8px;padding:7px 13px; + min-height:34px;box-sizing:border-box; + cursor:pointer;user-select:none; + font-size:12.5px;font-weight:500;line-height:1; + color:var(--text-muted,var(--muted)); + background:var(--surface); + border:1px solid var(--border);border-radius:999px; + box-shadow:var(--shadow-sm); + transition:background .15s ease,border-color .15s ease,color .15s ease; +} +.transparent-earlier-steps:hover{ + background:var(--hover-bg); + border-color:var(--accent); + color:var(--accent-text,var(--text)); +} +.transparent-earlier-steps:focus-visible{ + outline:2px solid var(--accent);outline-offset:2px; +} +.transparent-earlier-steps-chevron{ + display:inline-flex;align-items:center;justify-content:center; + color:var(--accent-text,var(--blue)); + transition:transform .15s ease; +} +.transparent-earlier-steps:hover .transparent-earlier-steps-chevron{transform:translateY(-1px);} +.transparent-earlier-steps-label{white-space:nowrap;} +@media (max-width:480px){ + .transparent-earlier-steps{min-height:44px;padding:11px 15px;font-size:13px;} +} +/* Newly-revealed prefix rows fade in gently so the reveal feels intentional, + not a flash of hundreds of rows. Respects reduced-motion. */ +@keyframes transparentEarlierReveal{from{opacity:0;transform:translateY(-2px);}to{opacity:1;transform:none;}} +.transparent-event-row[data-earlier-revealed="1"]{animation:transparentEarlierReveal .18s ease both;} +@media (prefers-reduced-motion:reduce){ + .transparent-earlier-steps-chevron,.transparent-event-row[data-earlier-revealed="1"]{transition:none;animation:none;} +} .message-virtual-spacer{width:100%;pointer-events:none;} .msg-date-sep { display: flex; align-items: center; gap: 10px; diff --git a/static/ui.js b/static/ui.js index 81fd486cf0a..b2c5f821519 100644 --- a/static/ui.js +++ b/static/ui.js @@ -10812,12 +10812,75 @@ function _setTransparentDetailMode(tab, mode){ function _setTransparentCardOpen(card, open){ if(!card) return; const expanded=!!open; - card.classList.toggle('open',expanded); const row=card.closest&&card.closest('.transparent-event-row'); + // #5966: a settled tool row whose detail body was deferred at render must + // materialize it the first time it's opened (before we flip .open so the + // detail exists for the same paint). No-op on live/already-materialized rows. + if(expanded&&row&&row.getAttribute('data-transparent-detail-deferred')==='1'){ + _materializeTransparentToolDetail(row); + } + card.classList.toggle('open',expanded); if(row) row.setAttribute('data-expanded',expanded?'1':'0'); const header=card.querySelector('.tool-card-header,.thinking-card-header'); if(header) header.setAttribute('aria-expanded',expanded?'true':'false'); } +// #5966: build the deferred `.tool-card-detail` for a settled transparent tool +// row on first expand — the lazy counterpart of the eager build in +// _decorateTransparentEventRow. Recovers the tool call from the in-memory stash +// or, after a _sessionHtmlCache innerHTML round-trip drops the JS property, from +// the row's data-anchor-row-id → S.messages scene (the #5839 recovery pattern). +// Idempotent: clears the deferred flag and runs anchor-suppressed post-processing +// (Prism / copy buttons / KaTeX / Mermaid / trees) on just the new subtree so an +// expanded deferred row is byte-identical to the eager path. +function _materializeTransparentToolDetail(row){ + if(!row||row.getAttribute('data-transparent-detail-deferred')!=='1') return false; + const card=row.querySelector('.tool-card'); + if(!card){ row.removeAttribute('data-transparent-detail-deferred'); return false; } + let tc=row._deferredToolCall; + if(!tc){ + tc=_transparentToolCallFromRowDataset(row); + } + if(!tc){ row.removeAttribute('data-transparent-detail-deferred'); return false; } + const status=String(row.getAttribute('data-event-status')||_transparentToolStatus(tc,true)); + row.removeAttribute('data-transparent-detail-deferred'); + row._deferredToolCall=null; + if(!card.querySelector('.tool-card-detail')){ + card.insertAdjacentHTML('beforeend',_transparentToolDetailHtml(tc,status)); + const detail=card.querySelector('.tool-card-detail'); + if(detail&&!detail.querySelector('.transparent-detail-modes')){ + const modes=document.createElement('div'); + modes.className='transparent-detail-modes'; + modes.setAttribute('role','tablist'); + modes.innerHTML=`FullOutput`; + const firstChild=detail.firstChild; + if(firstChild&&firstChild.parentNode===detail) detail.insertBefore(modes, firstChild); + else detail.appendChild(modes); + detail.setAttribute('data-transparent-detail-mode','full'); + } + // Match the eager path's post-processing so highlight/copy/KaTeX/Mermaid land. + if(typeof _postProcessWithAnchorSuppression==='function'){ + requestAnimationFrame(()=>{ try{ _postProcessWithAnchorSuppression(card); }catch(_){ } }); + } + } + return true; +} +// Recover a tool call for a deferred row whose _deferredToolCall JS property was +// dropped by an innerHTML cache round-trip: walk data-anchor-row-id back to the +// owning message's anchor scene and rebuild the tool call from the matching row. +function _transparentToolCallFromRowDataset(row){ + try{ + const rowId=row.getAttribute('data-anchor-row-id')||''; + const turn=row.closest&&row.closest('.assistant-turn'); + const seg=turn&&turn.querySelector('.assistant-segment[data-msg-idx]'); + const idx=seg?Number(seg.getAttribute('data-msg-idx')):NaN; + const msg=Number.isFinite(idx)?S.messages[idx]:null; + const scene=msg&&msg._anchor_activity_scene; + if(!scene||!rowId) return null; + const rows=_anchorSceneRowsForRendering(scene,{settled:true})||[]; + const match=rows.find(r=>String(r.row_id||r.local_id||'')===rowId&&String(r.role||'')==='tool'); + return match?_anchorSceneToolCallFromRow(match,{settled:true}):null; + }catch(_){ return null; } +} function _wireTransparentHeaderToggle(header){ if(!header) return; header.setAttribute('data-transparent-toggle-bound','1'); @@ -10860,7 +10923,13 @@ function _syncTransparentEventControls(turn){ const blocks=_assistantTurnBlocks(turn); if(!blocks) return; const rows=Array.from(blocks.querySelectorAll(':scope > .transparent-event-row,[data-transparent-event-row="1"]')); - const toolCount=rows.filter(row=>row.getAttribute('data-event-type')==='tool').length; + const mountedToolCount=rows.filter(row=>row.getAttribute('data-event-type')==='tool').length; + // #5966: when this turn's earlier steps are capped (some prefix rows are not + // mounted yet), the true tool count is stashed on the turn so the "Trace: N + // tools" label reflects the whole run, not just what's currently in the DOM. + // Falls back to the mounted count for uncapped turns and the live path. + const stashedTotal=Number(turn.getAttribute('data-transparent-total-tool-count')); + const toolCount=(Number.isFinite(stashedTotal)&&stashedTotal>mountedToolCount)?stashedTotal:mountedToolCount; let bar=blocks.querySelector(':scope > .transparent-event-controls'); if(!rows.length){ if(bar) bar.remove(); @@ -10941,6 +11010,23 @@ function _rehydrateTransparentStreamDom(root){ } if(card) _setTransparentCardOpen(card,card.classList.contains('open')); }); + // #5966: re-wire the "Show earlier steps" affordance. Its click handler was + // added via addEventListener and is lost when the session HTML-cache restores + // innerHTML; the DOM + data-earlier-count survive, so rebind by walking back to + // the owning message/segment. _revealTransparentEarlierSteps recovers the rows + // from the scene, so no JS-property stash is needed. + root.querySelectorAll('.transparent-earlier-steps[data-anchor-earlier-steps="1"]').forEach(el=>{ + if(el.getAttribute('data-earlier-rewired')==='1') return; + el.setAttribute('data-earlier-rewired','1'); + const turn=el.closest&&el.closest('.assistant-turn'); + const seg=turn&&turn.querySelector('.assistant-segment[data-msg-idx]'); + const idx=seg?Number(seg.getAttribute('data-msg-idx')):NaN; + const msg=Number.isFinite(idx)?S.messages[idx]:null; + if(!msg||!seg){ return; } + const handler=()=>_revealTransparentEarlierSteps(msg,seg,idx,el); + el.addEventListener('click',handler); + el.addEventListener('keydown',(ev)=>{ if(ev.key==='Enter'||ev.key===' '){ ev.preventDefault(); el.click(); } }); + }); } function _decorateTransparentEventRow(row, opts){ if(!row) return row; @@ -11015,7 +11101,29 @@ function _decorateTransparentEventRow(row, opts){ } } let detail=row.querySelector('.tool-card-detail'); - if(!detail&&card){ + // #5966: on a SETTLED, COLLAPSED tool row, defer building the heavy + // `.tool-card-detail` body (full tool input/output HTML + Prism/KaTeX/Mermaid + // post-processing) until the row is first expanded. A reasoning-heavy history + // can carry thousands of settled tool rows; eagerly materializing every + // detail body at load is the Transparent-Stream analogue of the #5860 + // compact-worklog freeze. The header (name, preview summary, status, chevron) + // is fully built above, so a deferred row looks identical collapsed; the + // chevron affordance is added here so it still reads as expandable. The tool + // call is stashed for _materializeTransparentToolDetail() to build on expand, + // with a data-attr fallback (data-anchor-row-id → S.messages scene) that + // survives the _sessionHtmlCache innerHTML round-trip (#5839 class). Live and + // already-open rows keep the eager build (they're about to be read). + const _deferDetail=!detail&&card&&opts.settled===true&&!card.classList.contains('open'); + if(_deferDetail){ + if(!header.querySelector('.tool-card-toggle')){ + const toggle=document.createElement('span'); + toggle.className='tool-card-toggle'; + toggle.innerHTML=li('chevron-right',12); + header.appendChild(toggle); + } + row._deferredToolCall=tc; + row.setAttribute('data-transparent-detail-deferred','1'); + }else if(!detail&&card){ card.insertAdjacentHTML('beforeend',_transparentToolDetailHtml(tc,status)); detail=row.querySelector('.tool-card-detail'); if(!header.querySelector('.tool-card-toggle')){ @@ -11122,6 +11230,14 @@ function _attachProgressBar(row, opts){ } function _setTransparentRowsExpanded(root, expanded){ const scope=root||document; + // #5966: "Expand all" must include a capped turn's hidden earlier steps — + // reveal them first so expansion genuinely opens the whole run. (Collapse-all + // leaves the cap as-is; it only closes what's mounted.) + if(expanded){ + scope.querySelectorAll('.transparent-earlier-steps[data-anchor-earlier-steps="1"]').forEach(el=>{ + if(typeof el.click==='function') el.click(); + }); + } scope.querySelectorAll('.transparent-event-row .tool-card,.transparent-event-row .thinking-card').forEach(card=>{ _setTransparentCardOpen(card,!!expanded); }); @@ -11910,6 +12026,7 @@ function _anchorSceneTransparentNodeForRow(row, opts){ ts:eventTs, ...meta, live, + settled, }); }else{ node=_anchorSceneNodeForRow(row,{settled}); @@ -12606,6 +12723,7 @@ function _renderSettledAnchorSceneTransparentForMessage(message, segment, rawIdx || '' ); blocks.querySelectorAll('[data-anchor-settled-scene-row="1"],.transparent-event-row[data-anchor-scene-row="1"]').forEach(el=>el.remove()); + blocks.querySelectorAll('.transparent-earlier-steps[data-anchor-earlier-steps="1"]').forEach(el=>el.remove()); blocks.querySelectorAll('.assistant-segment[data-msg-idx]').forEach(node=>{ const idx=Number(node.getAttribute('data-msg-idx')); if(Number.isFinite(idx)&&idxcap+slack){ + startIdx=rows.length-cap; + } + // Stash the TRUE tool-row count so "Trace: N tools" reflects the whole run even + // while the prefix is capped; cleared on full reveal. (uncapped → remove it.) + if(turnEl){ + if(startIdx>0){ + const totalTools=rows.filter(r=>String(r.role||'')==='tool').length; + turnEl.setAttribute('data-transparent-total-tool-count',String(totalTools)); + }else{ + turnEl.removeAttribute('data-transparent-total-tool-count'); + } + } + const renderRowAt=(idx)=>{ const row=rows[idx]; const node=_anchorSceneTransparentNodeForRow(row,{settled:true,finalAnswer,liveTokenFinalPrefixEligible:idx>lastNonTerminalWorkRowIndex}); - if(!node) continue; + if(!node) return null; if(segment.parentElement===blocks) blocks.insertBefore(node,segment); else blocks.appendChild(node); + return node; + }; + let wrote=false; + // The "Show earlier steps" affordance sits ABOVE the retained rows (chronology: + // the hidden steps came first). Insert it before rendering the retained tail so + // it lands at the top of this turn's activity run. + if(startIdx>0){ + const earlier=_buildTransparentEarlierStepsAffordance(startIdx); + if(segment.parentElement===blocks) blocks.insertBefore(earlier,segment); + else blocks.appendChild(earlier); + // Reveal handler: materialize the omitted prefix in place, holding the reader's + // viewport on the clicked affordance (insert grows content ABOVE it). + earlier.addEventListener('click',()=>{ + _revealTransparentEarlierSteps(message,segment,rawIdx,earlier); + }); wrote=true; } + for(let idx=startIdx;idx