Repository navigation
perf(mecatui): release settled answers' markdown from the render cache - #2641
Merged
Merged
Conversation
The assistant markdown cache kept glamour's output for every answer of the session next to the whole-block entry that already holds the rendered card, so memory grew by roughly 35 KB per answer and never came back. Keep a single markdown entry, for the most recently rendered block: only the final card can still change, and a settled answer is served by its whole-block entry. Mark answers without a reasoning summary as expand-invariant so a conversation-detail flip reuses their entry instead of re-running glamour for each of them. Add BenchmarkLongSessionRetainedHeap (retained heap per turn) and BenchmarkExpandToggle (detail-flip cost with and without reasoning) so the trade-off can be reproduced. Refs #2409 Co-Authored-By: Claude Opus 5.5 <[email protected]>
jbeda
approved these changes
Nov 2, 2026
2 of 5 tasks
Open
3 of 5 tasks
Merged
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mecatui's memory grows with every turn and never comes back. Part of the growth is the assistant markdown cache: it kept glamour's output for every answer of the session (~35 KB of ANSI for a ~1 KB answer), next to the whole-block entry that already holds the rendered card.
AppendAssistant,ReviseAssistant,AppendReasoningandEndReasoningStreamall target the last card), so that's the only entry worth keeping; a settled answer is served by its whole-block entry.renderPassescan't make that call up front the way it does for tool cards (whether an answer has reasoning isn't in the block metadata), so the entry declares it andrenderedBlockmasks the expand axis on the probe; admission is still one key comparison.Output is byte-identical; no goldens change.
Issue relationship
Relates to #2409. This is point 1 of the proposal. Point 2, bounding the whole-block entries to the viewport, needs the virtualized transcript (#1273); so does the issue's "memory flat after warm-up" criterion.
Type of change
Before | After
Real binary:
mecatui connectagainstmecated serve --mock-script400 scripted turns of a ~1 KB markdown answer (heading, list, Go fence, table) typed into a real
mecatuiin tmux at 120×40. Live heap comes fromGODEBUG=gctrace=1(heap after the last GC); RSS fromps. Samemecatedbinary for both; the reproduction script is at the end.Growth over 400 turns: live heap +36 MB → +22 MB, RSS +87 MB → +58 MB. The final screens of the two runs are identical apart from the port in the header.
Benchmarks (new,
markdown_cache_bench_test.go), benchstat,-count 10, Apple M3 ProExpandToggleflips F9 over a 200-turn session;glamour/opcounts markdown renders per flip.The trade-off. A flip over answers that carry a reasoning summary now re-runs glamour for each of them (200 renders above, ~1.3 ms each). That is roughly what a resize already costs, and with models that always emit reasoning it is the common case. The virtualized transcript (#1273) removes it by re-rendering only visible blocks. Answers without reasoning get a flip about 5× cheaper than before.
Test plan
Baseline checks
task lint): 0 issues in the root module and every other module exceptenvironment/microvm, where gosec flagslaunch_ownership_darwin.go:754(darwin-only, untouched, also reported onorigin/main)task test): not run separately; the race suite below is a supersettask test:race):cmd/mecatui/uipasses. 28 tests fail locally in untouched packages (cmd/mecated,cmd/mecatui,internal/app,internal/adapter/{forker,server},internal/executionexecutor,environment/microvm/gitexec,deploy/mecatl-execution-kind); every one of them fails the same way on a cleanorigin/maincheckout. After the review fixes,task lintandgo test -race ./cmd/mecatui/...were re-run on the final commits: same lint result, and everycmd/mecatuipackage passes except the same two root-package tests that fail onorigin/main(TestConnectUsesSavedServerCAForLazyTLSRPC,TestStartupCompatibilityEmbeddedRejected)go run ./cmd/mecademo): tool call, permission ask and approval, resultNew tests in
render_cache_test.go, each checked to fail when the change it guards is reverted:TestSettledAssistantMarkdownIsReleased: after five answers the markdown cache holds one entry, for the tail block. Fails withclearremoved (5 entries).TestExpandToggleKeepsAnswersWithoutReasoning: a detail flip re-renders the two prompts and the settled answer with reasoning, not the plain answer; the first flip re-runs glamour exactly once (the released markdown); both directions equal a fresh render. Fails with the invariance disabled (4 re-renders instead of 3).Changes
cmd/mecatui/ui/block_cache.gostoreMarkdownkeeps a single entry;expandInvarianton whole-block entries, masked inrenderedBlock; type docs state the rulecmd/mecatui/ui/render.gocmd/mecatui/ui/render_cache_test.gocmd/mecatui/ui/markdown_cache_bench_test.goBenchmarkLongSessionRetainedHeap,BenchmarkExpandToggleUser-facing change
Lower memory growth in long sessions. F9 is faster when answers have no reasoning summary, and in long sessions where they do it costs about as much as a resize.
Special notes for reviewers
markdownAtdoc comment, so whichever of the two lands second needs a one-comment rebase. Once both land, the single markdown entry could fold intostreamMarkdown, which already tracks the live block (follow-up).map(cleared on store) rather than a struct field, to avoid churn in six existing test assertions.Reproduction script (real binary)
script.pywrites the mock script;run.sh MECATED MECATUI LABEL TURNS OUTDIRdrives one binary and appendslabel,turn,live_heap_MB,rss_MBtoOUTDIR/results.csv.Benchmarks:
go test ./cmd/mecatui/ui/ -run '^$' -bench LongSessionRetainedHeap -benchtime 300x -benchmem -count 10and-bench ExpandToggle -benchtime 20x, on this branch and onorigin/mainwithmarkdown_cache_bench_test.gocopied in.🤖 Generated with Claude Code