Skip to content

Keep long chats smooth by re-checking generated images only in changed turns - #52675

Merged
brennanb2025 merged 3 commits into
mainfrom
brennanb2025/generated-image-turn-memo
Nov 30, 2026
Merged

brennanb2025 merged 3 commits into
mainfrom
brennanb2025/generated-image-turn-memo

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

In a long chat, every time the agent's reply moved forward, the app re-checked every earlier finished turn to decide where generated pictures belong. On phones this made long chats stutter while an agent was working. Now finished turns remember their answer, and only the turn that actually changed is checked again, including in bot chats. Pictures and bot notes show exactly as before.

Files Added Deleted Net
Test 0 0 0 0
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​123 $\color{#cf222e}{\Huge{\mathbf{−}}}$​27 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​96

What Changed

nativeChatGeneratedImageAnswers now splits settled rows by turn and keeps each turn's projection in a WeakMap keyed by the turn's first row. A turn is re-projected only when its rows' identities or the markdown image-source resolver change; otherwise its earlier replacements are reused. The per-turn classification logic itself is unchanged, just moved into projectTurn.

foldBotItemNotes rebuilt hidden item-mail rows and merged note rows as new objects on every call, which would make bot-chat turns miss that cache every time. Hidden rows are now cached per source row, and a folded row equal to the one built last time (compared by reference a few levels down, never by serializing) is reused.

Why

The projection ran over the whole history on every live update (desktop on each saved row, mobile on every transcript update). Reusing the result for unchanged turns keeps the same output while removing the repeated work; settled rows are immutable, so row identity is a safe cache key.

Linked Issue

Tracked in STA-14945.

Visual Proof

N/A: no visual change; images are placed exactly as before.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below
  • Every added test fails without this change and avoids the test anti-patterns

Platform-independent code shared by desktop and mobile; checked on macOS with a script that calls the real functions.

  • Old and new output compared on 4,500 randomized transcript states for each function (appends, row replacements, rows moving between turns, removals, settle changes, sub-agent producers, item mail, owner moves, worker links): identical.
  • Per-update time with the last turn streaming (p50 / p99): 12,000 rows 1.24 ms / 9.8 ms → 0.15 ms / 0.91 ms; 40,000 rows 4.59 ms / 12.9 ms → 0.50 ms / 3.1 ms. Bot chat with item mail and folded notes in every turn, 40,000 rows: image step 12 ms → 1.3 ms p50, note folding unchanged (about 8 ms), rows with a new identity per update 6,001 → 1.
  • No test added: output is unchanged, so a behavior test would pass with or without this change.

Performance

Less work per live update: only changed turns are re-classified; the remaining per-update cost is one identity walk over the rows. Memory is one small cache entry per settled turn and per folded row, released with its rows.

Risk

Level: Low. If a cache were wrong, a generated image or bot note could show stale; both reuse a result only when every input row (or every compared part) is the same object as before. Plain revert.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Shared code: desktop and mobile both use it. No platform, SSH or saved-data impact.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

Unchanged history now reaches the generated-image projection with the same rows,
so bot chats hit its per-turn cache too.
@brennanb2025
brennanb2025 marked this pull request as ready for review November 30, 2026 01:11
@brennanb2025

brennanb2025 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Performance scan (e6e02ff): pass, no new costs.

  • Trigger: rebuilding a chat transcript (desktop on each saved row change; mobile on each transcript update), once per open chat, on the renderer / app JS thread.
  • Before: every rebuild re-classified every settled turn for generated images. After: only turns with a replaced row are re-classified; the rest is one identity check per row.
  • Bot chats: note folding now hands back the same row objects for unchanged history (rows with a new identity per update: 6,001 → 1 at 40,000 rows), so those chats get the same saving. Folding cost is unchanged.
  • Memory: one weakly held entry per settled turn and per folded row, released with the rows. No timers, IPC, disk, subprocesses or idle wakeups added.
  • Measured, last turn streaming, p50: 12,000 rows 1.24 → 0.15 ms; 40,000 rows 4.59 → 0.50 ms; 40,000-row bot chat 12 → 1.3 ms.
  • Not changed here: folding a long run of consecutive notes copies the run's growing list each rebuild (quadratic in run length); unchanged by this PR.

@brennanb2025

brennanb2025 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Status (e6e02ff): ready for merge once CI is green.

Problem: in long chats, every transcript update re-checked all finished turns to place generated images, which made long chats stutter on phones while an agent worked.

Change: finished turns keep their result and only changed turns are re-checked. Bot chats get the same saving because unchanged note rows now stay the same objects. Nothing looks different.

Verified:

  • Old and new output identical across 4,500 randomized transcript states for each changed function.
  • Per-update time, p50: 12,000 rows 1.24 → 0.15 ms; 40,000 rows 4.59 → 0.50 ms; 40,000-row bot chat 12 → 1.3 ms.
  • Regression audit, code review and performance scan on this head: no findings.

Benchmark: head=e6e02ff499a79ac0a890e907a381f40d6ff45b93 gate=PASS corpus=synthetic 40,000-row transcripts (2,000 turns, last turn streaming; plain and bot chats with item mail and folded notes), real image-projection and note-folding functions, base and head interleaved 3x: image step p50 12.6 → 1.6 ms (plain), 31 → 2.0 ms (bot); note folding unchanged within noise.

Deferred: quadratic copying when folding very long runs of consecutive bot notes (pre-existing, unchanged here).

No screenshots: there is no visual change.

@greptile-apps

greptile-apps Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; no actionable issue was found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issue was found.

Summary

The PR reuses generated-image results for unchanged settled turns. It also preserves hidden and folded bot-row identities so bot chats can use that cache.

  • Settled chat turns reuse image placements while their rows stay the same.
  • Bot chats keep folded note rows stable when their contents do not change.

Reviews (1) · Last reviewed commit: "perf(bots): compare folded rows through ..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes add caching for hidden and folded bot note rows. Folded rows are reused when their contents match a prior result. Generated image answer projection now operates per turn, validates cached projections against turn rows and the sources callback, and applies replacements to a copied messages array when needed.

Merge Risk: Minimal · up to e6e02

This change speeds up long chat updates by reusing earlier image and bot-note work for unchanged turns. No concrete defect was found, and the visible output should be unchanged. Merge once CI is green.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: finished chat turns are re-checked for generated images only when they change, which keeps long chats smooth. It matches the changeset's per-turn projection caching i…
Description check Passed The description follows the repository template and fills in the main sections: ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk, and the upstream boun…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the main change: finished chat turns are re-checked for generated images only when they change, which keeps long chats smooth. It matches the changeset's per-turn projection caching in nativeChatGeneratedImageAnswers and the stated objective. It does not mention the bot-note caching in foldBotItemNotes, which is a secondary part of the change.

Full details: Description check

Explanation

The description follows the repository template and fills in the main sections: ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk, and the upstream boundary checkbox. It explains the mechanism, the alternatives, and how to back out the change. The Linked Issue section cites STA-14945 rather than a GitHub issue reference ("Fixes #" is left blank), and the Risk level is set to Low. The AI Disclosure section is left out, which the template marks as optional for StablyAI team members. The Testing section gives evidence of a local manual test and a comparison of old and new output. The lint, typecheck, test, and build checklist item is unchecked, and the description does not say whether those checks passed.

Resolution

Consider linking a GitHub issue in the Linked Issue section (the template asks for an actual issue reference) and confirm whether pnpm lint, typecheck, test, and build passed, or leave a note that CI will cover them.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aeff72a0-1434-4ef0-a567-a2a33c1285d7
Commits

Reviewing files that changed from the base of the PR and between 2e0a6a2 and e6e02ff.

Files selected for processing: 2

  • src/shared/bots/bot-item-note-runs.ts
  • src/shared/native-chat/transcript/images/native-chat-generated-image-answers.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@brennanb2025
brennanb2025 merged commit 164f836 into main Nov 30, 2026
88 of 89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant