Repository navigation
fix(ui): make file-link label disambiguation near-linear - #124258
Conversation
shortestFileLabels rescanned every path list at each suffix depth to find the shortest unambiguous label, which is quadratic in path count. A Markdown message with thousands of distinct file references could stall the chat renderer. Replace the repeated scan with one reversed-segment suffix trie built per render, so each path's label depth is found with one child lookup per depth instead of a comparison against every other path. Output is unchanged. Fixes #124230
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 15, 2026, 5:52 PM ET / 21:52 UTC. ClawSweeper reviewWhat this changesThe branch replaces repeated file-path suffix scans in Control UI Markdown with a per-render suffix trie and adds a structural test that guards near-linear lookup growth. Merge readinessKeep open: current main still uses the repeated all-path scan, and the updated head addresses the prior performance-contract finding with a deterministic owner-level growth guard. The patch is correct; wait for refreshed exact-head checks and normal maintainer handling. Priority: P2 Review scores
Verification
How this fits togetherControl UI Markdown parsing collects file-link targets from a chat message, computes short distinct labels, then renders those labels in message and detail views. The helper affects label derivation only; link parsing, line suffixes, and rendered destinations remain downstream contracts. flowchart LR
A[Chat Markdown] --> B[File link parsing]
B --> C[Collected file paths]
C --> D[Suffix trie label derivation]
D --> E[Distinct short labels]
E --> F[Chat and detail rendering]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +28, Tests +58. Total +86 across 2 files. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep the suffix index local to label derivation, retain the deterministic scaling guard, and merge only after the current head has a clean refreshed merge result and required checks. Do we have a high-confidence way to reproduce the issue? Yes, at source level: current main's nested unique.some scan at ui/src/components/file-kind.ts:125-140 has a clear quadratic high-cardinality path, though this read-only review did not execute it. Is this the best way to solve the issue? Yes. A render-local suffix index repairs the computation at its owning helper while preserving parser-owned line suffixes; a parser cache or consumer guard would relocate rather than remove the repeated work. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against ac6940641ce0. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Add a deterministic performance-contract regression for shortestFileLabels: count Map#get calls via vi.spyOn while the trie runs at two path-count scales and assert the growth stays linear (generous band) rather than the pre-fix O(n^2) full-list rescan's quadratic signature. No wall-clock timing, so it can't flake under CI load; verified locally that it fails deterministically against the pre-fix implementation and passes clean against the fix.
|
Merged via squash.
|
…4258) * fix(ui): make file-link label disambiguation near-linear shortestFileLabels rescanned every path list at each suffix depth to find the shortest unambiguous label, which is quadratic in path count. A Markdown message with thousands of distinct file references could stall the chat renderer. Replace the repeated scan with one reversed-segment suffix trie built per render, so each path's label depth is found with one child lookup per depth instead of a comparison against every other path. Output is unchanged. Fixes openclaw#124230 * test(ui): assert file-label lookup cost stays linear Add a deterministic performance-contract regression for shortestFileLabels: count Map#get calls via vi.spyOn while the trie runs at two path-count scales and assert the growth stays linear (generous band) rather than the pre-fix O(n^2) full-list rescan's quadratic signature. No wall-clock timing, so it can't flake under CI load; verified locally that it fails deterministically against the pre-fix implementation and passes clean against the fix.
What Problem This Solves
shortestFileLabelsinui/src/components/file-kind.tscomputes the shortest unambiguous label for each file link rendered in a Markdown message. For every path, it repeatedly scans the entire path list at each suffix depth to check for collisions. A message with a dense list of distinct file references drives this into roughly quadratic time and can visibly stall the chat renderer.Fixes #124230
Why This Change Was Made
The label routine is the sole owner of this computation (called once per render from
ui/src/components/markdown-parser.ts:463, reached by default fromui/src/components/chat-message-bubble.ts:296andui/src/pages/chat/components/chat-sidebar.ts:521), so the fix stays inside it rather than adding a parser-level guard or a cross-render cache that would just relocate the cost or introduce a new caching boundary. The rewrite replaces the repeated full-list scan with one reversed-segment suffix trie built once per render: each path's minimal unique suffix depth is then found with one child lookup per depth instead of a comparison against every other path. Output is unchanged — same shortest-suffix selection, same separator preservation, same full-path fallback — only the algorithmic complexity changes.User Impact
Chat and file-preview surfaces that render Markdown with file links (default-on chat message bubbles, and the detail panel) no longer risk a renderer stall when a message references a large number of distinct files. Existing labels are byte-for-byte identical; this is a scaling fix, not a behavior change.
Evidence
Reachable cardinality (revised from the earlier benchmark)
Both surfaces that call
shortestFileLabels(chat-message-bubble.ts:296,chat-sidebar.ts:521) render withmodedefaulting to"message"(markdown-render-options.ts:22). For that mode,renderSanitizedMarkdownnever reaches the file-link parse path at all aboveMARKDOWN_PARSE_LIMIT(40,000 characters,ui/src/components/markdown.ts:90,510-515) — text past that length is escaped and rendered as plain text, no markdown parsing, no file-link decoration, no call intoshortestFileLabels. So the true producer-side ceiling onnis not an independent "path count" — it's however many distinct file references fit inside a single 40,000-character message.The original benchmark's 16,000-path row (using the ~27-char-average synthetic paths from the fixture) needs roughly 432,000 characters of message text — over 10x the parse cap. That workload can never reach this function in the shipped product; the earlier "~1500x" framing was measured past the point the code path can be entered. Real repository paths make the reachable ceiling tighter still:
git ls-files '*.ts' '*.tsx' | head -900averages 48.8 characters/path, so 900 of those already total ~43,900 characters, at the cap.Benchmark, re-run at reachable scale
Same methodology as before (
shortestFileLabelscalled directly, all-unique-basename paths, the shape that defeats the pre-fix algorithm's early exit), now bounded to what a single 40,000-character message can actually carry:(3 local runs per size; ranges cover the observed spread.) Corroborating run against 900 real repository
.ts/.tsxpaths (43,877 chars, i.e. already past the cap on their own): pre-fix 105.76 ms, post-fix 1.04 ms.Reframed claim: within the range this code path can actually be entered, the pre-fix implementation costs roughly 75–180 ms — not the 20+ second, 16,000-path figure from the original benchmark, which described a workload the UI cannot produce. That range is still a real, user-visible stall (blocking the render of a single chat message for 100+ ms is noticeable jank), and the quadratic shape means it gets worse the closer a message gets to the 40k cap — so the fix is still warranted, just not for the reason originally stated. The post-fix implementation stays under ~2 ms across the same range: a ~70–170x improvement at reachable cardinality, not the previously claimed ~1500x at an unreachable one.
Performance contract: deterministic, not timing-based
The previous version of this PR only asserted output correctness at high cardinality, so the pre-fix quadratic implementation also passed it. Per review,
ui/src/components/markdown-links.test.tsnow has a dedicated test,keeps per-path lookup cost linear as path count grows (performance contract), that asserts the actual complexity contract instead of wall-clock time:Map#getcall made whileshortestFileLabelsruns (vi.spyOn(Map.prototype, "get"), restored viamockRestore()), at 500 and 4,000 paths (8x).unique.some(...)at every depth, which is O(n²) lookups for this same shape: verified locally at exactly n² (e.g. 4,000,000 lookups at n=2,000), so 8x the paths there costs ~64x the lookups.> small * 4and< small * 16) — comfortably containing the true ~8x linear growth while excluding the ~64x quadratic signature. Verified by temporarily revertingshortestFileLabelsto the pre-fix implementation locally: the test fails deterministically (expected 16000000 to be less than 4000000), then reverted the file back and reran clean.Focused test run against the fix:
node scripts/check-changed.mjs -- ui/src/components/file-kind.ts ui/src/components/markdown-links.test.tspassed after fixing anoxlint(no-extend-native)/unbound-methodfinding in the new test (switched from directly reassigningMap.prototype.gettovi.spyOn): format, lint, typecheck (core + UI), dead-export scan, dependency/patch guards, and the delegated Testbox lint run all green.CI
checks-ui-e2e (6/12)failed on the prior head (c985d533b4c) inui/src/e2e/agent-file-lifecycle.e2e.test.ts("reads and saves the selected agent workspace through an isolated Gateway") — a stale/duplicated file-content assertion ('# Real main instructions\n# Saved thr…'vs'# Saved through real Gateway\n') in a test unrelated to this PR's Control UI markdown/file-label code. That test file is not touched by this diff.This looks like a pre-existing, sitewide E2E flake, not caused by this PR: sibling open PRs from the same day each hit exactly one failing
checks-ui-e2eshard, each a different, unrelated test — #124285 failed shard 11/12 innew-session-page.prompt-attachments.e2e.test.ts(textarea line-count assertion), #124264 failed shard 2/12 insession-management.sidebar.e2e.test.ts(missing "Selected session recovered" text), and this PR failed shard 6/12 inagent-file-lifecycle.e2e.test.ts. Different tests, different shards, no shared code path with this diff — consistent with CI-load-driven flake in the isolated-Gateway E2E lane rather than a regression from this change. Pushing the new commit reruns this lane fresh on the new head.