Skip to content

Commit b5aa4ec

Browse files
imsobearclaude
andauthored
perf(ios): virtualize the chat transcript on ChatLayout (#2178)
## Why The transcript was an eager `VStack` in a `ScrollView`. Keeping the rows in view still while older pages go in took layer after layer of patches, and the iOS 27 paging jump (#2148) came out of them. Past 400 rows it switched to a lazy stack, and scrolling down from there froze for seconds. The transcript is now a `UICollectionView` laid out by [ChatLayout](https://github.com/ekazaev/ChatLayout), which keeps the position itself, with each row a SwiftUI view in its own cell. This replaces #2164's patches, which are closed unmerged. ## Invariants - Only the rows near the screen are built and measured. An older page goes in as soon as it is fetched, and the rows in view do not move. - A row that grows after it showed (a picture, a card filling in) moves nothing in view. On the bottom, the newest row stays on the bottom while it grows. - Back to bottom, a jump to a quoted message or the unread line, and the status-bar tap (to the oldest loaded message, as before) land where they did. ## Scope - New dependency: ChatLayout 2.5.2 via SPM, pinned to the exact version, MIT license. It is a single custom `UICollectionViewLayout` with no transitive dependencies. - Removed: the `placingOlder` hold, the `ScrollHandle` offset shifts, `eagerRowLimit` and the lazy switch, the `defaultScrollAnchor` roles, `DistanceFromBottom`/`BottomMarker`, and the iOS 17 legacy path. `TranscriptStore`, the row views and `ChatActions` are unchanged; older pages now go in when they are fetched. - Rejected: an inverted (flipped) list. It was spiked and worked, but VoiceOver order, edge effects and insets all need flip workarounds. ChatLayout keeps everything native. - After a chat opens, the rows just above the screen are laid out and drawn once off screen, one per frame, so the first fling up does not build several cards in one frame. - Left out: the thread overlay keeps its own list. ## Merge danger Two-way door; a revert restores the old list. Blast radius: every chat transcript (main Chat, topic chats, task chats). ## Evidence - New hosted tests: a row above the screen that grows moves nothing in view, a row below the screen that grows moves nothing in view, and the newest row stays on the bottom while it grows in steps. An older page lands without moving the rows in view (`PagingUpTests`, updated). The scroll, store, cache, share and thread suites pass on an iOS 27.2 simulator. - `ROME_PERF=paging` / `pagingdown` against #2164's head, same simulator: a mid-screen row drifted 0 pt in every hold (11 holds paging up, 9 coming down, against #2164's 0 pt kept rows). An older page goes in in 4–11 ms including layout, against 45–78 ms. Scrolling down past 520 messages has a worst frame of 43–103 ms, against 150 ms. Flinging up has a hitch rate of 10–14 ms/s, against 18–23, and a worst frame of 60–67 ms, against 110–125. - Checked on the simulator: context-menu previews and Reply, share-mode selection, keyboard and composer insets with interactive dismissal, back to bottom, the typing row, task chats starting at the top, a live Dynamic Type change, and dark mode. Light snapshots of the chat screens match `main`, except for animation frames. VoiceOver order was not checked with VoiceOver; without a flip it follows the rows' order. - Tried on a phone against a dev stack. - A question or widget card keeps its unsent answers and its sent lock when its cell is reused: the list keeps each card's surface, known by its row and its tool use id, which repeats across turns (`WidgetSurfacesTests`). Each cell takes its row's identity, so a reused cell starts fresh before iOS 18 too; no iOS 17 runtime was at hand to watch that on. ## Follow-up (not blocking) - Each list update hands the environment and footer to every visible cell, so each streamed token redraws the rows in view, not only the streaming one. `main` redraws every row per token, so this is no regression, and the hand-off is also what brings fresh row closures to cells on screen. Measure with `ROME_PERF=stream`, then pass only the keys the rows read. - An unfolded long reply folds again once its cell is reused. - A jump to a row merged in the same update as the jump can be dropped without a retry. - A card coming back into view reads its binds again, which can reset an unsent edit in a field the app fills. Read once per open, and again after a press. - `ThreadOverlay` keeps its own SwiftUI `ScrollView`; moving the thread onto `TranscriptList` would leave one list. - The cached row heights are never pruned within a chat's lifetime. Fixes #2141 Fixes #2148 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 78beb24 commit b5aa4ec

19 files changed

Lines changed: 1257 additions & 762 deletions

‎ios/CLAUDE.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ The native iOS app (SwiftUI, iOS 17). It is an Xcode project, not a pnpm workspa
3535

3636
**A literal reaches the catalog only where Swift types it as `LocalizedStringKey` or `String(localized:)`.** `Text("Done")` is looked up; `Text(flag ? "Running" : tone.label)` is a `String` and shows as written, and so does a literal passed to a `String` parameter. Give a view's text parameters the type `LocalizedStringKey`, and wrap literals headed for a `String` in `String(localized:)`.
3737

38-
**A view at the end of a `LazyVStack` is not proof that the bottom is on screen.** The stack keeps it alive while it is off screen, so its `onAppear` and `onDisappear` lag behind the scroll, and a list that trusts them pulls the guardian back to the bottom. `TranscriptList` reads the scroll phase and `onScrollVisibilityChange` instead, and before iOS 18 the marker's frame against the visible area.
38+
**A SwiftUI view in a `UIHostingConfiguration` cell does not inherit the SwiftUI environment around the collection view.** The transcript's rows read many environment values (`openTaskChat`, `assetLoader`, `shareTurns`), and they arrive empty in a cell. `TranscriptList` hands `context.environment` to every cell. For the same reason a row cannot read a coordinate space named outside the list; the list reports positions itself, as it does for the unread line.
39+
40+
**A hosted cell's default self-sizing came out a line short for wrapped text.** Rows overlapped the ones below them. `TranscriptCell` measures its content at the list's width itself.
3941

4042
**Message times are whole seconds.** Rows written in the same second tie, so sorting by time alone gives a random order that can change on each load. `TranscriptStore` orders rows the way the server does: by turn, then time, then the order they arrived.
4143

42-
**Two `onGeometryChange` readings of the same type on one view do not both fire.** The second never ran in `TranscriptList`. Read both values in one closure.
44+
**Two `onGeometryChange` readings of the same type on one view do not both fire.** The second never runs. Read both values in one closure.
4345

4446
**Mac Catalyst runs the app but cannot capture a system sheet.** Snapshot runs draw the Tasks sheet in a stand-in frame; the sheet on a phone is the system one.
4547

@@ -51,7 +53,7 @@ The native iOS app (SwiftUI, iOS 17). It is an Xcode project, not a pnpm workspa
5153

5254
**The swipe home starts on the transcript and reads as a drag away from the bottom.** So does Notification Center pulled down over the app. Rows refetched on return then land below the screen, which looks like a stale chat. `TranscriptList` goes by whether the bottom was on screen when the app resigned active, and a tapped push always shows the newest rows.
5355

54-
**A fling's glide wins over `ScrollViewReader.scrollTo`.** The tap reaches the button and `scrollTo` runs, but the list keeps decelerating the other way, so a tap mid-glide seems to do nothing. A Simulator tap after a slow drag does not show it; a real fling does. `ScrollHandle` stops the `UIScrollView`'s deceleration before scrolling, and older pages wait until the jump lands, because content that grows under the scroll animation moves its target.
56+
**A fling's glide wins over a programmatic scroll.** The tap reaches the button and the scroll starts, but the list keeps decelerating the other way, so a tap mid-glide seems to do nothing. A Simulator tap after a slow drag does not show it; a real fling does. `TranscriptList` stops the deceleration before it scrolls to the bottom or to a quoted message.
5557

5658
**A page that stays mounted keeps its animations running.** Chat stays in the tree, faded out, under the other sections, and a `TimelineView(.animation)` draws every frame there for no one. Pass `paused: offScreen`; see [`PERFORMANCE.md`](PERFORMANCE.md).
5759

‎ios/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Debug builds read these environment variables (set them in the scheme's Run opti
113113
| `ROME_DEMO=update-offered` | Opens the demo data on a Rome behind its latest release but new enough for the app: the banner and the drawer's line offer the update. |
114114
| `ROME_DEMO=visitor` | Opens the demo data as a dashboard visitor of a shared Rome. |
115115
| `ROME_DEMO=many-apps` | Opens the demo data with a screenful of apps, for the Apps grid and the widget. |
116-
| `ROME_PERF=<scenario>` | With `ROME_DEMO=long-chat`: runs one scenario for about 20 s and logs frame hitches and how often rows, screens and parses ran (`PerfProbe`). Scenarios: `idle`, `idle-apps`, `stream`, `stream-apps`, `taskstream`, `sections`, `settings`, `scroll`, `chats`. The result is in the log (category `Perf`) and in `tmp/perf-<scenario>.txt` in the app's container. |
116+
| `ROME_PERF=<scenario>` | With `ROME_DEMO=long-chat`: runs one scenario for about 20 s and logs frame hitches and how often rows, screens and parses ran (`PerfProbe`). Scenarios: `idle`, `idle-apps`, `stream`, `stream-apps`, `taskstream`, `sections`, `settings`, `scroll`, `chats`, `paging`, twelve flings up through older pages, and `pagingdown`, ten flings back down after paging past 520 messages (add `ROME_DEMO_PAIRS=300`, a deeper chat with pictures and cards). Both report `holdDriftPt`, how far a row mid-screen moved while each fling rested, and the time each older page took to go in. The result is in the log (category `Perf`) and in `tmp/perf-<scenario>.txt` in the app's container. |
117117
| `ROME_LOCAL_ORIGIN`, `ROME_LOCAL_USER`, `ROME_LOCAL_PASSWORD` | Signs in to a Rome on this Mac with a local test guardian instead of Rome Cloud. |
118118

119119
**A chat with every card state.** To look at the app's cards and lines against a dev stack, fill a chat with copies of the main Chat's cards (tasks, questions and approvals in every state the stack holds, forwarded results, a connect card, model switches and stored model errors). Test data only, on a dev stack, never a real instance. Make a chat (`POST /api/concierge/chats` with a name such as `zz-all-card-states`), then run the script inside the dev container, never from the host:

‎ios/Rome.xcodeproj/project.pbxproj‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
A10000000000000000000611 /* A2UISwiftCore in Frameworks */ = {isa = PBXBuildFile; productRef = A10000000000000000000601 /* A2UISwiftCore */; };
1212
A10000000000000000000612 /* A2UISwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = A10000000000000000000602 /* A2UISwiftUI */; };
1313
A10000000000000000000613 /* BasicCatalog in Frameworks */ = {isa = PBXBuildFile; productRef = A10000000000000000000603 /* BasicCatalog */; };
14+
A10000000000000000000621 /* ChatLayout in Frameworks */ = {isa = PBXBuildFile; productRef = A10000000000000000000622 /* ChatLayout */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXContainerItemProxy section */
@@ -106,6 +107,7 @@
106107
A10000000000000000000611 /* A2UISwiftCore in Frameworks */,
107108
A10000000000000000000612 /* A2UISwiftUI in Frameworks */,
108109
A10000000000000000000613 /* BasicCatalog in Frameworks */,
110+
A10000000000000000000621 /* ChatLayout in Frameworks */,
109111
);
110112
runOnlyForDeploymentPostprocessing = 0;
111113
};
@@ -174,6 +176,7 @@
174176
A10000000000000000000601 /* A2UISwiftCore */,
175177
A10000000000000000000602 /* A2UISwiftUI */,
176178
A10000000000000000000603 /* BasicCatalog */,
179+
A10000000000000000000622 /* ChatLayout */,
177180
);
178181
productName = Rome;
179182
productReference = A10000000000000000000011 /* Rome.app */;
@@ -259,6 +262,7 @@
259262
minimizedProjectReferenceProxies = 1;
260263
packageReferences = (
261264
A10000000000000000000600 /* XCRemoteSwiftPackageReference "a2ui" */,
265+
A10000000000000000000620 /* XCRemoteSwiftPackageReference "ChatLayout" */,
262266
);
263267
preferredProjectObjectVersion = 77;
264268
productRefGroup = A10000000000000000000003 /* Products */;
@@ -563,6 +567,14 @@
563567
minimumVersion = 0.10.0;
564568
};
565569
};
570+
A10000000000000000000620 /* XCRemoteSwiftPackageReference "ChatLayout" */ = {
571+
isa = XCRemoteSwiftPackageReference;
572+
repositoryURL = "https://github.com/ekazaev/ChatLayout";
573+
requirement = {
574+
kind = exactVersion;
575+
version = 2.5.2;
576+
};
577+
};
566578
/* End XCRemoteSwiftPackageReference section */
567579

568580
/* Begin XCSwiftPackageProductDependency section */
@@ -581,6 +593,11 @@
581593
package = A10000000000000000000600 /* XCRemoteSwiftPackageReference "a2ui" */;
582594
productName = BasicCatalog;
583595
};
596+
A10000000000000000000622 /* ChatLayout */ = {
597+
isa = XCSwiftPackageProductDependency;
598+
package = A10000000000000000000620 /* XCRemoteSwiftPackageReference "ChatLayout" */;
599+
productName = ChatLayout;
600+
};
584601
/* End XCSwiftPackageProductDependency section */
585602
};
586603
rootObject = A10000000000000000000001 /* Project object */;

‎ios/Rome.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved‎

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ios/Rome/Demo/DemoBackend.swift‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,46 @@ final class DemoBackend: RomeBackend {
8080
private func lengthenMainChat() {
8181
let start = Date.now.addingTimeInterval(-2 * 86400)
8282
var earlier: [ChatMessage] = []
83-
for index in 0..<60 {
84-
let at = start.addingTimeInterval(Double(index) * 600)
83+
// `ROME_DEMO_PAIRS` makes it deeper and mixed like a real chat, for
84+
// paging through many pages (`ROME_PERF=paging`): long Markdown,
85+
// pictures that load after they show, and approval cards that fill in
86+
// once their record comes.
87+
let pairs = ProcessInfo.processInfo.environment["ROME_DEMO_PAIRS"].flatMap(Int.init)
88+
let picture = assets["projects/demo/trip/hotel.jpg"]
89+
// A deeper history starts before the oldest demo message (3 days ago),
90+
// so its pages go in above the rows, as a real chat's do.
91+
let first = pairs.map { Date.now.addingTimeInterval(-4 * 86400 - Double($0) * 600) } ?? start
92+
for index in 0..<(pairs ?? 60) {
93+
let at = first.addingTimeInterval(Double(index) * 600)
8594
earlier.append(user("long-u\(index)", "第 \(index + 1) 个问题:今天有什么要注意的?", at: at))
86-
earlier.append(agent("第 \(index + 1) 个回答:没有新的安排,照常进行。", at: at.addingTimeInterval(30)))
95+
guard pairs != nil else {
96+
earlier.append(agent("第 \(index + 1) 个回答:没有新的安排,照常进行。", at: at.addingTimeInterval(30)))
97+
continue
98+
}
99+
switch index % 6 {
100+
case 0:
101+
let items = (1...(3 + index % 7)).map { "\($0). 下午三点和设计团队过首页文案,记得带上上周的反馈。" }.joined(separator: "\n")
102+
earlier.append(agent("## 第 \(index + 1) 个回答\n\n\(items)\n\n| 事项 | 时间 | 谁 |\n|---|---|---|\n| 首页文案 | 15:00 | 设计 |\n| 周报 | 17:30 | 我 |", at: at.addingTimeInterval(30)))
103+
case 2:
104+
// A path of its own, so the picture is not cached and loads late.
105+
let path = "projects/demo/deep/pic-\(index).jpg"
106+
assets[path] = picture
107+
earlier.append(agent("第 \(index + 1) 个回答:这是现场的照片。\n\n![现场](/api/projects/asset/pic.jpg?path=\(path.replacingOccurrences(of: "/", with: "%2F")))\n\n看起来没问题。", at: at.addingTimeInterval(30)))
108+
case 3:
109+
let id = "ap-deep-\(index)"
110+
approvals[id] = ApprovalRecord(id: id, status: index % 12 == 3 ? "approved" : "pending")
111+
earlier.append(parts([.object([
112+
"type": .string("approval_card"), "approvalId": .string(id), "actionName": .string("calendar:create_event"), "status": .string("pending"),
113+
"preview": .object(["kind": .string("generic"), "title": .string("添加日程"), "summary": .string(""), "fields": .array([
114+
.object(["label": .string("标题"), "value": .string("第 \(index + 1) 次例会")]),
115+
.object(["label": .string("时间"), "value": .string("明天 15:00–15:30")]),
116+
])]),
117+
])], at: at.addingTimeInterval(30), session: conciergeChatID))
118+
case 5:
119+
earlier.append(agent("第 \(index + 1) 个回答:上午的会议改到了下午,地点不变。会后需要把讨论的要点整理成文档发给大家,截止时间是周五。", at: at.addingTimeInterval(30)))
120+
default:
121+
earlier.append(agent("第 \(index + 1) 个回答:没有新的安排,照常进行。", at: at.addingTimeInterval(30)))
122+
}
87123
}
88124
let digest = (1...24).map { "\($0). 今天的邮件摘要第 \($0) 条:项目进度、会议时间和需要你回复的事项。" }.joined(separator: "\n")
89125
messages[conciergeChatID] = earlier + (messages[conciergeChatID] ?? []) + [agent(digest, at: .now.addingTimeInterval(-60))]

0 commit comments

Comments
 (0)