From c2e3d16b7efb510c21270be991dda8f780b9a412 Mon Sep 17 00:00:00 2001 From: weishu Date: Thu, 10 Sep 2026 11:28:33 +0800 Subject: [PATCH] feat(native): improve anchored chat scrolling and history loading Add viewport-driven paging with layout acknowledgements, bounded retries, cancellation gates, and epoch-safe history retention. Preserve transcript anchors and expansion state, fix tool-group identity collisions, and serialize Android history coordination on Main. Reduce per-scroll composition and layout work; add native regression tests, CI coverage, and profiling guidance. --- .github/workflows/android.yml | 23 +- .github/workflows/ios.yml | 2 + android/README.md | 5 +- android/app/build.gradle.kts | 5 + .../feature/chat/ChatTranscriptTest.kt | 347 +++++++++++++++ .../kotlin/app/hapi/companion/di/HubGraph.kt | 1 + .../hapi/companion/feature/chat/ChatScreen.kt | 166 +------ .../companion/feature/chat/ChatTranscript.kt | 362 ++++++++++++++++ .../companion/feature/chat/ChatViewModel.kt | 244 ++++++++++- .../chat/blocks/PermissionActionViews.kt | 9 +- .../feature/chat/blocks/ToolGroupBlockView.kt | 14 +- .../hapi/companion/ui/markdown/Markdown.kt | 23 +- .../ui/markdown/MarkdownRenderCache.kt | 43 ++ .../src/main/res/values-zh-rCN/strings.xml | 5 + android/app/src/main/res/values/strings.xml | 5 + .../chat/ChatViewModelInteractionTest.kt | 5 + .../feature/chat/ChatViewModelTest.kt | 246 ++++++++++- .../ui/markdown/MarkdownRenderCacheTest.kt | 23 + .../hapi/data/store/ChatHistoryPagingState.kt | 60 +++ .../app/hapi/data/store/MessageWindowStore.kt | 65 ++- .../hapi/data/store/MessageWindowStores.kt | 3 + .../data/store/ChatHistoryPagingStateTest.kt | 53 +++ .../hapi/data/store/MessageWindowStoreTest.kt | 109 +++++ .../app/hapi/protocol/chat/ToolGroups.kt | 28 +- .../protocol/window/MessageWindowLogic.kt | 2 +- .../protocol/window/MessageWindowState.kt | 3 + .../protocol/chat/ToolGroupIdentityTest.kt | 65 +++ android/gradle/libs.versions.toml | 4 + docs/native-chat-scrolling.md | 187 ++++++++ ios/Hapi.xcodeproj/project.pbxproj | 115 +++++ .../xcshareddata/xcschemes/Hapi.xcscheme | 13 + .../Features/Chat/Blocks/ChatBlockCard.swift | 9 +- .../Chat/Blocks/CodexReviewBlockView.swift | 9 +- .../Chat/Blocks/GeneratedImageBlockView.swift | 2 +- .../Chat/Blocks/PermissionActionViews.swift | 15 +- .../Features/Chat/Blocks/TextBlockViews.swift | 11 +- .../Chat/Blocks/ToolCallBlockView.swift | 8 +- .../Chat/Blocks/ToolGroupBlockView.swift | 13 +- ios/Hapi/Features/Chat/ChatModel.swift | 209 ++++++++- .../Features/Chat/ChatPresentationState.swift | 46 ++ .../Features/Chat/ChatTranscriptView.swift | 290 ++++--------- ios/Hapi/Features/Chat/ChatView.swift | 4 +- ios/Hapi/Models/ChatSession.swift | 11 +- ios/Hapi/Models/HubSession.swift | 3 +- ios/Hapi/Resources/Localizable.xcstrings | 40 ++ ios/HapiTests/ChatHistoryPumpTests.swift | 125 ++++++ ios/HapiTests/TranscriptGeometryTests.swift | 101 +++++ ios/HapiTests/TranscriptLayoutTests.swift | 269 ++++++++++++ .../Chat/ChatHistoryPagingState.swift | 83 ++++ .../Chat/ChatHistoryRequestGate.swift | 13 + .../Stores/MessageWindowController.swift | 53 ++- .../Stores/MessageWindowControllers.swift | 8 +- .../HapiProtocol/Chat/ToolGroups.swift | 37 +- .../Window/MessageWindowLogic.swift | 2 +- .../Window/MessageWindowState.swift | 3 + .../HapiUI/Chat/AnchoredTranscriptList.swift | 408 ++++++++++++++++++ .../HapiUI/Markdown/MarkdownRenderCache.swift | 100 +++++ .../Chat/ChatHistoryPagingStateTests.swift | 83 ++++ .../MessageWindowControllerTests.swift | 150 +++++++ .../ToolGroupIdentityTests.swift | 73 ++++ .../MarkdownRenderCacheTests.swift | 24 ++ ios/README.md | 9 +- ios/scripts/test-transcript.sh | 17 + 63 files changed, 4010 insertions(+), 453 deletions(-) create mode 100644 android/app/src/androidTest/kotlin/app/hapi/companion/feature/chat/ChatTranscriptTest.kt create mode 100644 android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatTranscript.kt create mode 100644 android/app/src/main/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCache.kt create mode 100644 android/app/src/test/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCacheTest.kt create mode 100644 android/core/data/src/main/kotlin/app/hapi/data/store/ChatHistoryPagingState.kt create mode 100644 android/core/data/src/test/kotlin/app/hapi/data/store/ChatHistoryPagingStateTest.kt create mode 100644 android/core/protocol/src/test/kotlin/app/hapi/protocol/chat/ToolGroupIdentityTest.kt create mode 100644 docs/native-chat-scrolling.md create mode 100644 ios/Hapi/Features/Chat/ChatPresentationState.swift create mode 100644 ios/HapiTests/ChatHistoryPumpTests.swift create mode 100644 ios/HapiTests/TranscriptGeometryTests.swift create mode 100644 ios/HapiTests/TranscriptLayoutTests.swift create mode 100644 ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryPagingState.swift create mode 100644 ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryRequestGate.swift create mode 100644 ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift create mode 100644 ios/Packages/HapiKit/Sources/HapiUI/Markdown/MarkdownRenderCache.swift create mode 100644 ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatHistoryPagingStateTests.swift create mode 100644 ios/Packages/HapiKit/Tests/HapiProtocolTests/ToolGroupIdentityTests.swift create mode 100644 ios/Packages/HapiKit/Tests/HapiUITests/MarkdownRenderCacheTests.swift create mode 100755 ios/scripts/test-transcript.sh diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 6b92d7b5..dfcee030 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -37,6 +37,27 @@ jobs: # accepted); AGP auto-installs any missing platform/build-tools. - uses: gradle/actions/setup-gradle@v4 - name: Protocol tests (pure JVM, fixtures-driven) - run: ./gradlew :core:protocol:test + run: ./gradlew :core:protocol:test :core:data:testDebugUnitTest :app:testDebugUnitTest - name: Assemble debug APK run: ./gradlew :app:assembleDebug + + transcript-layout: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: '21' + - uses: gradle/actions/setup-gradle@v4 + - name: Enable KVM + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + - uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: 29 + arch: x86_64 + disable-animations: true + script: cd android && ./gradlew :app:connectedDebugAndroidTest diff --git a/.github/workflows/ios.yml b/.github/workflows/ios.yml index 9e26415a..73676d9e 100644 --- a/.github/workflows/ios.yml +++ b/.github/workflows/ios.yml @@ -38,3 +38,5 @@ jobs: run: xcodebuild -version - name: Build Hapi app for iOS Simulator run: xcodebuild build -project ios/Hapi.xcodeproj -scheme Hapi -destination 'generic/platform=iOS Simulator' CODE_SIGNING_ALLOWED=NO + - name: Transcript layout regression tests + run: ios/scripts/test-transcript.sh diff --git a/android/README.md b/android/README.md index c4839102..93cfbaf1 100644 --- a/android/README.md +++ b/android/README.md @@ -36,6 +36,9 @@ re-runs this suite whenever `android/**` or `shared/fixtures/**` change. ## Building +Native chat scrolling architecture and acceptance checklist: +[Native transcript scrolling](../docs/native-chat-scrolling.md). + Requires an Android SDK for `:app`/`:core:data` (set `ANDROID_HOME` or `android/local.properties` with `sdk.dir=...`). `:core:protocol` alone needs only a JDK. @@ -97,7 +100,7 @@ drops it from the roster. - **M0** — this scaffold: modules, version catalog, CI, placeholder screen. - **M1** — foundations: wire types + modes catalog; auth + `HapiApi` (MockWebServer-tested); `SseEngine` reconnect state machine + versioned patches (gzip streaming verified); pairing UI + `hapicompanion://bind` deep link. -- **M2** — read-only chat: chat pipeline port gated on fixtures all-green; session list; `MessageWindowStore` port; Markdown renderer; read-only chat screen (`LazyColumn(reverseLayout = true)`). +- **M2** — read-only chat: chat pipeline port gated on fixtures all-green; session list; `MessageWindowStore` port; Markdown renderer; read-only chat screen (`LazyColumn` with chronological stable keys). - **M3** — interaction: composer (optimistic send/queue/steer/drafts), permission approvals UX, session controls (mode/model/abort/resume/rename/archive), new session, dictation. - **B-M3ce landed** — voice dictation: mic button in the composer (`RECORD_AUDIO` requested at first use), `MediaRecorder` → m4a/AAC, provider discovery via `GET /api/voice/transcription/providers` on chat entry (first `standard`-capable provider; mic hidden until available, including unconfigured/unreachable hubs), upload through the multipart `POST /api/voice/transcription`, transcript appended at the composer text with a space separator; `DictationController` is a plain seam over recorder + API, JVM-tested with fakes. Slash commands: typing a lone `/token` opens a dropdown merging the session's `metadata.slashCommands` names with the `GET /slash-commands` RPC list (RPC entries win dedupe; exact → prefix → contains filtering), tap inserts `/name ` (the skills `$` trigger is deferred). Session ops: list long-press sheet and chat top-bar overflow gain Rename (`PATCH /sessions/:id`, optimistic name with roll-forward on failure), Delete (confirm; 409-while-active surfaced), and Reopen for inactive sessions (`POST /reopen`; a superseding id reuses the supersede path — window seed + draft move + navigate-replace; 422 missing-metadata formatted); chat shows an inactive-session bar ("send to resume, or Reopen"). - **M4** — FCM push (register → notification actions via expedited WorkManager) + files/git viewer, Scratchlist, usage/storage stats. diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index e32c76c6..caa6d2c8 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -55,6 +55,7 @@ android { versionCode = 1 // Tracks the hapi CLI/hub release train. versionName = "0.28.0" + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } signingConfigs { @@ -159,6 +160,10 @@ dependencies { implementation(libs.androidx.compose.material3) implementation(libs.androidx.compose.ui.tooling.preview) debugImplementation(libs.androidx.compose.ui.tooling) + debugImplementation(libs.androidx.compose.ui.test.manifest) + androidTestImplementation(platform(libs.androidx.compose.bom)) + androidTestImplementation(libs.androidx.compose.ui.test.junit4) + androidTestImplementation(libs.androidx.test.runner) // JVM unit tests (ViewModel combine logic with fake stores). testImplementation(libs.junit) diff --git a/android/app/src/androidTest/kotlin/app/hapi/companion/feature/chat/ChatTranscriptTest.kt b/android/app/src/androidTest/kotlin/app/hapi/companion/feature/chat/ChatTranscriptTest.kt new file mode 100644 index 00000000..e314f71f --- /dev/null +++ b/android/app/src/androidTest/kotlin/app/hapi/companion/feature/chat/ChatTranscriptTest.kt @@ -0,0 +1,347 @@ +package app.hapi.companion.feature.chat + +import androidx.compose.foundation.lazy.LazyListState +import androidx.compose.foundation.gestures.animateScrollBy +import androidx.compose.foundation.layout.height +import androidx.compose.animation.core.tween +import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.Composable +import androidx.compose.runtime.Composer +import androidx.compose.runtime.CompositionTracer +import androidx.compose.runtime.InternalComposeTracingApi +import androidx.compose.runtime.saveable.rememberSaveableStateHolder +import androidx.compose.ui.test.junit4.StateRestorationTester +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.performTouchInput +import androidx.compose.ui.test.swipeDown +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import app.hapi.companion.ui.theme.HapiTheme +import app.hapi.data.store.ChatHistoryPagingState +import app.hapi.protocol.chat.AgentTextBlock +import app.hapi.protocol.chat.AgentReasoningBlock +import app.hapi.companion.ui.markdown.MarkdownRenderCache +import app.hapi.companion.ui.markdown.LocalMarkdownRenderCache +import app.hapi.protocol.chat.VisibleChatBlock +import app.hapi.protocol.chat.ToolGroupBlock +import app.hapi.protocol.chat.ToolGroupingOptions +import app.hapi.protocol.chat.buildVisibleChatBlocks +import app.hapi.companion.feature.chat.blocks.previewToolCall +import java.util.concurrent.atomic.AtomicLong +import java.util.concurrent.atomic.AtomicReference +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test + +class ChatTranscriptTest { + @get:Rule val compose = createComposeRule() + private lateinit var list: LazyListState + private lateinit var scope: CoroutineScope + private val layoutVersion = AtomicLong(-1) + private val needsOlder = AtomicReference(false) + private val viewportReports = AtomicReference>(emptyList()) + private val shown = mutableStateOf(true) + private val jumpToken = mutableStateOf(0L) + private val state = mutableStateOf(makeState(emptyList())) + private val markdown = MarkdownRenderCache() + private val viewportHeight = mutableStateOf(null) + + @OptIn(InternalComposeTracingApi::class) + @Test fun scrollingDoesNotRecomposeTheTranscriptOrRepeatUnchangedDemand() { + val compositions = AtomicLong() + Composer.setTracer(object : CompositionTracer { + override fun isTraceInProgress() = true + override fun traceEventEnd() = Unit + override fun traceEventStart(key: Int, dirty1: Int, dirty2: Int, info: String) { + if (info.startsWith("app.hapi.companion.feature.chat.ChatTranscript (")) compositions.incrementAndGet() + } + }) + try { + mount(rows(0..799)) + assertTrue("Tracer must observe the real transcript, not a test wrapper", compositions.get() > 0) + browse(350, 37) + val before = anchor() + compositions.set(0) + viewportReports.set(emptyList()) + compose.runOnIdle { scope.launch { list.animateScrollBy(1200f, tween(600)) } } + compose.waitForIdle() + assertTrue("The performance probe must actually cross rows", anchor().first != before.first) + val count = compositions.get() + android.util.Log.i("HapiScrollPerf", "800-row scroll: transcript compositions=$count, viewport reports=${viewportReports.get().size}") + assertTrue("Transcript recomposed $count times during a history scroll", count <= 2) + assertTrue("Unchanged viewport demand was republished ${viewportReports.get().size} times", viewportReports.get().size <= 1) + } finally { + Composer.setTracer(null) + } + } + + private fun rows(range: IntRange): List = range.map { + AgentTextBlock( + id = "message-$it", localId = null, createdAt = it.toLong(), invokedAt = it.toLong(), + text = "Message $it\n\n" + ("Variable-height history content. ".repeat(1 + it % 5)), meta = null, + ) + } + + private fun makeState(blocks: List) = ChatUiState( + sessionId = "layout-test", + header = ChatHeaderUi("Layout test", active = false, thinking = false, subtitle = null), + flavor = null, basePath = null, blocks = blocks, permissionOverrides = emptyMap(), + hasMore = true, isLoadingOlder = false, isSyncingTail = false, isInitialLoading = false, + loadFailed = false, warning = null, tailRevision = 0, + ) + + private fun mount(blocks: List, restoration: StateRestorationTester? = null) { + markdown.prepare(blocks.mapNotNull { (it as? AgentTextBlock)?.text ?: (it as? AgentReasoningBlock)?.text }.toSet()) + state.value = makeState(blocks) + val content: @Composable () -> Unit = { + val savedScreens = rememberSaveableStateHolder() + if (shown.value) savedScreens.SaveableStateProvider(state.value.sessionId) { + list = rememberLazyListState() + scope = rememberCoroutineScope() + HapiTheme { + CompositionLocalProvider(LocalMarkdownRenderCache provides markdown) { + ChatTranscript( + state = state.value, paging = ChatHistoryPagingState(), jumpToken = jumpToken.value, + jumpingLatest = false, onViewport = { follows, needs -> + viewportReports.set(viewportReports.get() + follows) + needsOlder.set(needs) + }, + onLayout = { version, _ -> layoutVersion.set(version) }, + onRetryHistory = {}, onJumpToLatest = {}, listState = list, + modifier = viewportHeight.value?.let { Modifier.height(it) } ?: Modifier, + ) + } + } + } + } + if (restoration == null) compose.setContent(content) else restoration.setContent(content) + compose.waitUntil(10_000) { layoutVersion.get() == 0L } + compose.waitForIdle() + } + + private fun browse(index: Int, offset: Int) { + compose.onNodeWithTag("chat-transcript").performTouchInput { swipeDown(durationMillis = 400) } + compose.runOnIdle { scope.launch { list.scrollToItem(index, offset) } } + compose.waitForIdle() + } + + private fun anchor(): Pair = compose.runOnIdle { + list.layoutInfo.visibleItemsInfo.first { it.key != "chat-history-control" }.let { it.key to it.offset } + } + + @Test fun tailStillFollowsTallRowGrowthAppendsAndViewportResize() { + mount(rows(0..39)) + fun assertFollowing() { + compose.waitForIdle() + compose.runOnIdle { assertTrue("Live tail must remain visible", !list.canScrollForward) } + assertEquals(true, viewportReports.get().last()) + } + assertFollowing() + compose.runOnIdle { + val last = state.value.blocks.last() as AgentTextBlock + val text = last.text + "\n\n" + "Growing the live row beyond the viewport. ".repeat(100) + markdown.prepare(setOf(text)) + state.value = state.value.copy( + blocks = state.value.blocks.dropLast(1) + AgentTextBlock( + id = last.id, localId = null, createdAt = last.createdAt, invokedAt = last.invokedAt, + text = text, meta = null, + ), messagesVersion = 1, + ) + } + assertFollowing() + compose.runOnIdle { state.value = state.value.copy(blocks = state.value.blocks + rows(40..41), messagesVersion = 2) } + assertFollowing() + compose.runOnIdle { viewportHeight.value = 360.dp } + assertFollowing() + } + + private fun mountWithExpandedGroup(restoration: StateRestorationTester? = null): ToolGroupBlock { + val tools = (1..12).map { previewToolCall("group-child-$it", "Read", input = mapOf("file_path" to "file-$it.txt")) } + val group = buildVisibleChatBlocks(tools, ToolGroupingOptions(hasMoreMessages = false)) + .filterIsInstance().single() + assertTrue(!group.defaultOpen) + mount(rows(0..24) + group + rows(25..79), restoration) + browse(26, 0) + compose.onNodeWithText("12 tools").performClick() + compose.waitForIdle() + // Anchor to a child row, not an ordinary row whose index could happen + // to survive losing the group's expansion map. + compose.runOnIdle { scope.launch { list.scrollToItem(32, 17) } } + compose.waitForIdle() + assertEquals("group-child-6", anchor().first) + return group + } + + @Test fun navigationReturnPreservesExpandedChildrenAndTheirReadingAnchor() { + mountWithExpandedGroup() + val before = anchor() + compose.runOnIdle { shown.value = false } + compose.waitForIdle() + viewportReports.set(emptyList()) + compose.runOnIdle { shown.value = true } + assertHistoryRestored(before) + } + + @Test fun savedInstanceStateRestoresExpandedChildrenBeforeRestoringTheListIndex() { + val restoration = StateRestorationTester(compose) + mountWithExpandedGroup(restoration) + val before = anchor() + viewportReports.set(emptyList()) + restoration.emulateSavedInstanceStateRestore() + assertHistoryRestored(before) + } + + @Test fun removedGroupsDoNotRestoreStaleExpansionFlags() { + val restoration = StateRestorationTester(compose) + val group = mountWithExpandedGroup(restoration) + compose.runOnIdle { + state.value = state.value.copy(blocks = rows(0..79), messagesVersion = 1) + } + compose.waitForIdle() + restoration.emulateSavedInstanceStateRestore() + compose.runOnIdle { + state.value = state.value.copy(blocks = rows(0..24) + group + rows(25..79), messagesVersion = 2) + } + browse(26, 0) + compose.onNodeWithTag("chat-row-${group.id}").assertIsDisplayed() + compose.onNodeWithTag("chat-row-group-child-1").assertDoesNotExist() + compose.runOnIdle { assertEquals(82, list.layoutInfo.totalItemsCount) } + } + + private fun assertHistoryRestored(before: Pair) { + compose.waitForIdle() + assertEquals(before, anchor()) + assertTrue("Restored viewport must never report tail mode", viewportReports.get().let { + it.isNotEmpty() && it.none { follows -> follows } + }) + compose.onNodeWithTag("chat-latest").assertIsDisplayed() + // A genuinely new command must still work after ignoring the old one. + compose.runOnIdle { jumpToken.value += 1 } + compose.waitForIdle() + compose.runOnIdle { assertTrue(!list.canScrollForward) } + assertEquals(true, viewportReports.get().last()) + } + + @Test fun navigationReturnRestoresHistoryIntentAndDoesNotReplayConsumedJump() { + jumpToken.value = 1 + mount(rows(0..79)) + browse(25, 37) + val before = anchor() + compose.runOnIdle { shown.value = false } + compose.waitForIdle() + viewportReports.set(emptyList()) + compose.runOnIdle { shown.value = true } + assertHistoryRestored(before) + } + + @Test fun savedInstanceStateRestoresReadingPositionAndConsumedJumpTogether() { + val restoration = StateRestorationTester(compose) + jumpToken.value = 3 + mount(rows(0..79), restoration) + browse(25, 37) + val before = anchor() + viewportReports.set(emptyList()) + restoration.emulateSavedInstanceStateRestore() + assertHistoryRestored(before) + } + + @Test fun recreatedViewModelRebasesJumpTokensWithoutTakingOverHistory() { + val restoration = StateRestorationTester(compose) + jumpToken.value = 1 + mount(rows(0..79), restoration) + browse(25, 37) + val before = anchor() + // A new ViewModel starts at zero, independent of saved Compose state. + compose.runOnIdle { jumpToken.value = 0 } + compose.waitForIdle() + viewportReports.set(emptyList()) + restoration.emulateSavedInstanceStateRestore() + assertHistoryRestored(before) + } + + @Test fun prependPreservesLatestReaderPositionWithBoundedWindowTrimming() { + mount(rows(100..179)) + browse(15, 19) + compose.runOnIdle { scope.launch { list.scrollToItem(21, 31) } } + compose.waitForIdle() + val before = anchor() + compose.runOnIdle { + state.value = state.value.copy( + blocks = rows(80..99) + state.value.blocks.dropLast(20), + historyVersion = 1, messagesVersion = 1, + ) + } + compose.waitUntil(10_000) { layoutVersion.get() == 1L } + compose.waitForIdle() + val after = anchor() + assertEquals(before.first, after.first) + assertTrue("Extra anchor displacement: " + (after.second - before.second), kotlin.math.abs(after.second - before.second) <= 1) + } + + @Test fun streamingAndTallRowGrowthDoNotReclaimHistory() { + mount(rows(0..79)) + browse(25, 37) + val before = anchor() + compose.runOnIdle { + val last = state.value.blocks.last() as AgentTextBlock + state.value = state.value.copy( + blocks = state.value.blocks.dropLast(1) + AgentTextBlock( + id = last.id, localId = null, createdAt = last.createdAt, invokedAt = last.invokedAt, + text = last.text + "\n\n" + "Streaming more content. ".repeat(100), meta = null, + ), + messagesVersion = 1, + ) + } + compose.waitForIdle() + assertEquals(before, anchor()) + compose.runOnIdle { + val row = state.value.blocks.first { it.stableId == before.first } as AgentTextBlock + val text = row.text + "\n\n" + "More content in the current row. ".repeat(30) + markdown.prepare(setOf(text)) + state.value = state.value.copy( + blocks = state.value.blocks.map { block -> + if (block !== row) block else AgentTextBlock( + id = row.id, localId = null, createdAt = row.createdAt, invokedAt = row.invokedAt, + text = text, meta = null, + ) + }, messagesVersion = 2, + ) + } + compose.waitForIdle() + assertEquals(before, anchor()) + } + + @Test fun expansionSurvivesCellRecycling() { + val blocks = rows(0..79).toMutableList() + blocks[25] = AgentReasoningBlock( + id = "reasoning", localId = null, createdAt = 25, invokedAt = 25, + text = "Retained reasoning content", meta = null, + ) + mount(blocks) + browse(26, 0) + compose.onNodeWithText("💭 Reasoning ▸").performClick() + compose.onNodeWithText("Retained reasoning content").assertIsDisplayed() + browse(55, 0) + browse(26, 0) + compose.onNodeWithText("Retained reasoning content").assertIsDisplayed() + } + + @Test fun hiddenOnlyPageStillAcknowledgesLayoutAndShortViewportDemandsMore() { + mount(rows(0..0)) + assertTrue(needsOlder.get()) + compose.runOnIdle { state.value = state.value.copy(historyVersion = 1, messagesVersion = 1) } + compose.waitUntil(10_000) { layoutVersion.get() == 1L } + assertTrue(needsOlder.get()) + } +} diff --git a/android/app/src/main/kotlin/app/hapi/companion/di/HubGraph.kt b/android/app/src/main/kotlin/app/hapi/companion/di/HubGraph.kt index 53a6e839..7c331107 100644 --- a/android/app/src/main/kotlin/app/hapi/companion/di/HubGraph.kt +++ b/android/app/src/main/kotlin/app/hapi/companion/di/HubGraph.kt @@ -95,6 +95,7 @@ class HubGraph( api = session.api, scope = scope, snapshots = WindowSnapshots(File(snapshotDir, "windows")), + historyRetentionLimit = app.hapi.protocol.window.OLDER_LOAD_WINDOW_SIZE, ) private val mutableToasts = MutableSharedFlow(extraBufferCapacity = 16) diff --git a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatScreen.kt b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatScreen.kt index cb074c42..2ff32866 100644 --- a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatScreen.kt +++ b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatScreen.kt @@ -10,7 +10,6 @@ import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize @@ -21,9 +20,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn -import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.items -import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack @@ -50,7 +47,6 @@ import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState -import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -82,15 +78,13 @@ import app.hapi.companion.feature.files.FolderGlyph import app.hapi.companion.feature.sessions.DeleteSessionDialog import app.hapi.companion.feature.sessions.RenameSessionDialog import app.hapi.companion.ui.components.AgentFlavorIcon +import app.hapi.companion.ui.markdown.LocalMarkdownRenderCache import app.hapi.companion.ui.markdown.LocalMarkdownLinkHandler import app.hapi.companion.ui.theme.hapi import app.hapi.protocol.chat.VisibleChatBlock import java.io.File import kotlinx.coroutines.launch -/** How close to the oldest rendered block the viewport may get before paging. */ -private const val LOAD_OLDER_PREFETCH_ITEMS = 4 - /** Pending camera capture across rotation/process death: uri + scratch path. */ private val CameraCaptureSaver = listSaver( save = { capture -> @@ -142,6 +136,9 @@ fun ChatScreen( onOpenScratchlist: (() -> Unit)? = null, ) { val state by viewModel.uiState.collectAsState() + val historyPaging by viewModel.historyPaging.collectAsState() + val jumpToken by viewModel.jumpToken.collectAsState() + val jumpingLatest by viewModel.jumpingLatest.collectAsState() val composerState by viewModel.composer.collectAsState() val queuedRows by viewModel.queuedRows.collectAsState() val configState by viewModel.config.collectAsState() @@ -380,6 +377,7 @@ fun ChatScreen( }, ) { padding -> CompositionLocalProvider( + LocalMarkdownRenderCache provides viewModel.markdownCache, LocalChatMedia provides media, LocalMarkdownLinkHandler provides rememberChatLinkHandler(onOpenFile = onOpenFile), LocalChatInteractions provides interactions, @@ -396,8 +394,15 @@ fun ChatScreen( when { state.isInitialLoading -> InitialLoading() state.loadFailed -> LoadFailed(onRetry = viewModel::retry) - state.blocks.isEmpty() -> EmptyChat() - else -> BlockList(state = state, onLoadOlder = viewModel::loadOlder) + state.blocks.isEmpty() && !state.hasMore -> EmptyChat() + else -> ChatTranscript( + state = state, paging = historyPaging, jumpToken = jumpToken, + jumpingLatest = jumpingLatest, + onViewport = viewModel::readingViewportChanged, + onLayout = viewModel::historyLaidOut, + onRetryHistory = viewModel::loadOlder, + onJumpToLatest = viewModel::jumpToLatest, + ) } } } @@ -632,149 +637,6 @@ private fun DegradedBanner(warning: String, onRetry: () -> Unit) { } } -// ------------------------------------------------------------------- list -- - -@Composable -private fun BlockList(state: ChatUiState, onLoadOlder: () -> Unit) { - val listState = rememberLazyListState() - val scope = rememberCoroutineScope() - // Newest-first for reverseLayout: index 0 renders at the bottom. - val reversed = remember(state.blocks) { state.blocks.asReversed() } - - LoadOlderEffect(listState, state, onLoadOlder) - - Box(modifier = Modifier.fillMaxSize()) { - LazyColumn( - state = listState, - reverseLayout = true, - modifier = Modifier.fillMaxSize(), - contentPadding = PaddingValues(horizontal = 12.dp, vertical = 10.dp), - verticalArrangement = Arrangement.spacedBy(10.dp), - ) { - items( - items = reversed, - key = { it.stableId }, - contentType = { it.contentKind }, - ) { block -> - ChatBlockCard(block = block, basePath = state.basePath) - } - if (state.hasMore || state.isLoadingOlder) { - item(key = "older-history", contentType = "older-history") { - OlderHistoryRow(isLoading = state.isLoadingOlder) - } - } - } - - NewMessagesPill( - listState = listState, - reversed = reversed, - sessionId = state.sessionId, - onClick = { scope.launch { listState.animateScrollToItem(0) } }, - modifier = Modifier - .align(Alignment.BottomCenter) - .padding(bottom = 12.dp), - ) - } -} - -/** Sentinel: when the viewport nears the oldest rendered block, page older history. */ -@Composable -private fun LoadOlderEffect(listState: LazyListState, state: ChatUiState, onLoadOlder: () -> Unit) { - val nearOldest by remember(listState) { - derivedStateOf { - val info = listState.layoutInfo - val lastVisible = info.visibleItemsInfo.lastOrNull()?.index ?: return@derivedStateOf false - lastVisible >= info.totalItemsCount - 1 - LOAD_OLDER_PREFETCH_ITEMS - } - } - LaunchedEffect(nearOldest, state.hasMore, state.isLoadingOlder, state.isSyncingTail) { - if (nearOldest && state.hasMore && !state.isLoadingOlder && !state.isSyncingTail) { - onLoadOlder() - } - } -} - -@Composable -private fun OlderHistoryRow(isLoading: Boolean) { - Row( - modifier = Modifier - .fillMaxWidth() - .padding(vertical = 8.dp), - horizontalArrangement = Arrangement.Center, - verticalAlignment = Alignment.CenterVertically, - ) { - if (isLoading) { - CircularProgressIndicator(modifier = Modifier.size(16.dp), strokeWidth = 2.dp) - Spacer(modifier = Modifier.width(8.dp)) - Text( - text = stringResource(R.string.chat_loading_older), - style = MaterialTheme.typography.labelMedium, - color = MaterialTheme.hapi.hint, - ) - } else { - Text( - text = "· · ·", - style = MaterialTheme.typography.labelMedium, - color = MaterialTheme.hapi.hint, - ) - } - } -} - -/** - * "N new messages ↓" pill: appears when new blocks land while the reader is - * scrolled up. At the bottom (reverse-layout index 0, offset 0) the list - * auto-sticks and the pill stays hidden. - */ -@Composable -private fun NewMessagesPill( - listState: LazyListState, - reversed: List, - sessionId: String, - onClick: () -> Unit, - modifier: Modifier = Modifier, -) { - val atBottom by remember(listState) { - derivedStateOf { - listState.firstVisibleItemIndex == 0 && listState.firstVisibleItemScrollOffset == 0 - } - } - var newestSeenId by remember(sessionId) { mutableStateOf(null) } - val newestId = reversed.firstOrNull()?.stableId - - LaunchedEffect(atBottom, newestId) { - if (atBottom) newestSeenId = newestId - } - - val unseenCount = if (atBottom) { - 0 - } else { - val seenId = newestSeenId - if (seenId == null) 0 - else reversed.indexOfFirst { it.stableId == seenId }.coerceAtLeast(0) - } - if (unseenCount == 0) return - - Surface( - color = MaterialTheme.colorScheme.primary, - contentColor = MaterialTheme.colorScheme.onPrimary, - shape = CircleShape, - shadowElevation = 4.dp, - onClick = onClick, - modifier = modifier, - ) { - Text( - text = if (unseenCount == 1) { - stringResource(R.string.chat_new_messages_one) - } else { - stringResource(R.string.chat_new_messages_many, unseenCount) - }, - style = MaterialTheme.typography.labelMedium, - modifier = Modifier.padding(horizontal = 14.dp, vertical = 7.dp), - ) - } -} - // ----------------------------------------------------------------- states -- @Composable diff --git a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatTranscript.kt b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatTranscript.kt new file mode 100644 index 00000000..fcba9a9d --- /dev/null +++ b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatTranscript.kt @@ -0,0 +1,362 @@ +package app.hapi.companion.feature.chat + +import androidx.compose.foundation.interaction.collectIsDraggedAsState +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyListState +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateMapOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.mutableLongStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState +import androidx.compose.runtime.setValue +import androidx.compose.runtime.snapshotFlow +import androidx.compose.runtime.saveable.rememberSaveableStateHolder +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.saveable.mapSaver +import androidx.compose.runtime.snapshots.SnapshotStateMap +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.layout.layout +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import app.hapi.companion.R +import app.hapi.companion.feature.chat.blocks.ToolGroupBlockView +import app.hapi.data.store.ChatHistoryPagingState +import app.hapi.protocol.chat.ToolGroupBlock +import app.hapi.protocol.chat.VisibleChatBlock +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.withContext + +private const val HISTORY_KEY = "chat-history-control" + +private val GroupExpansionSaver = mapSaver( + save = { expanded: SnapshotStateMap -> expanded.toMap() }, + restore = { saved -> + mutableStateMapOf().apply { + saved.forEach { (id, expanded) -> put(id, expanded as Boolean) } + } + }, +) + +private data class TranscriptItem(val block: VisibleChatBlock, val expanded: Boolean? = null) { + val id: String get() = block.stableId +} + +/** Positive-order keys preserve the TOP reading anchor, including tall rows + * whose bottom is still streaming. No prepend invokes scrollToItem. */ +@Composable +internal fun ChatTranscript( + state: ChatUiState, + paging: ChatHistoryPagingState, + jumpToken: Long, + jumpingLatest: Boolean, + onViewport: (Boolean, Boolean) -> Unit, + onLayout: (Long, Boolean) -> Unit, + onRetryHistory: () -> Unit, + onJumpToLatest: () -> Unit, + modifier: Modifier = Modifier, + listState: LazyListState = rememberLazyListState(), +) { + // These flags determine the flattened list's indices, not just row UI. + // Restore them before LazyListState restores a position among the children. + val expandedGroups = rememberSaveable(state.sessionId, saver = GroupExpansionSaver) { + mutableStateMapOf() + } + val rowState = rememberSaveableStateHolder() + val stateKeys = remember(state.sessionId) { mutableSetOf() } + val rows = remember(state.blocks, expandedGroups.toMap()) { + buildList { + for (block in state.blocks) { + if (block is ToolGroupBlock) { + val expanded = expandedGroups[block.id] ?: block.defaultOpen + add(TranscriptItem(block, expanded)) + if (expanded) block.tools.forEach { add(TranscriptItem(it)) } + } else { + add(TranscriptItem(block)) + } + } + } + } + val heights = remember(state.sessionId) { mutableMapOf() } + val density = LocalDensity.current + val estimatedHeight = with(density) { 100.dp.roundToPx() } + val spacing = with(density) { 10.dp.roundToPx() } + val headerHeight = with(density) { 44.dp.roundToPx() } + val isDragged by listState.interactionSource.collectIsDraggedAsState() + // Restore intent with LazyListState, otherwise the first tail effect + // overwrites a saved history position on navigation/activity recreation. + var followsTail by rememberSaveable(state.sessionId) { mutableStateOf(true) } + var consumedJumpToken by rememberSaveable(state.sessionId) { mutableLongStateOf(0) } + var correctingTail by remember(state.sessionId) { mutableStateOf(false) } + var renderedHistoryVersion by remember(state.sessionId) { mutableLongStateOf(-1) } + val currentRows by rememberUpdatedState(rows) + val rowIDs = remember(rows) { rows.mapTo(mutableSetOf()) { it.id } } + val currentIDs by rememberUpdatedState(rowIDs) + val currentState by rememberUpdatedState(state) + val currentJumpToken by rememberUpdatedState(jumpToken) + val reportViewport by rememberUpdatedState(onViewport) + val reportLayout by rememberUpdatedState(onLayout) + + LaunchedEffect(state.sessionId, listState) { + var lastVersion = -1L + var readerWasScrolling = false + var previousIDs = emptySet() + var previousVisibleHeight = 0 + var lastReadingDemand: Pair? = null + snapshotFlow { + val info = listState.layoutInfo + val current = currentRows + val height = info.viewportEndOffset - info.viewportStartOffset + // Wait until visible keys occupy their NEW indices. A same-size + // bounded-window replacement is not identified by count alone. + val matches = info.totalItemsCount == current.size + 1 && + info.visibleItemsInfo.all { item -> + if (item.index == 0) item.key == HISTORY_KEY + else current.getOrNull(item.index - 1)?.id == item.key + } + var distance = listState.firstVisibleItemScrollOffset + for (index in 0 until listState.firstVisibleItemIndex) { + distance += if (index == 0) headerHeight + spacing + else (heights[current.getOrNull(index - 1)?.id] ?: estimatedHeight) + spacing + if (distance > height) break + } + val short = !listState.canScrollBackward && !listState.canScrollForward + LayoutReport( + version = currentState.historyVersion, + matches = matches && renderedHistoryVersion == currentState.historyVersion && + height > 0 && info.visibleItemsInfo.isNotEmpty(), + short = short, + nearTop = !listState.canScrollBackward || distance <= height, + atBottom = !listState.canScrollForward, + scrolling = listState.isScrollInProgress, + ownScroll = correctingTail, + following = followsTail, + ids = currentIDs, + visibleHeight = info.visibleItemsInfo.sumOf { it.size }, + ) + }.collect { report -> + if (!report.matches) return@collect + if (report.scrolling && !report.ownScroll) { + readerWasScrolling = true + followsTail = false + } else if (!report.scrolling && readerWasScrolling) { + followsTail = report.atBottom + readerWasScrolling = false + } + val demand = followsTail to (report.short || (!followsTail && report.nearTop)) + if (demand != lastReadingDemand) { + lastReadingDemand = demand + reportViewport(demand.first, demand.second) + } + if (report.version != lastVersion) { + val progress = report.ids.any { it !in previousIDs } || + (report.ids == previousIDs && report.visibleHeight > previousVisibleHeight + 1) + lastVersion = report.version + reportLayout(report.version, progress) + } + previousIDs = report.ids + previousVisibleHeight = report.visibleHeight + } + } + + LaunchedEffect(isDragged) { + if (isDragged) { + followsTail = false + // Publish history mode at gesture start, before a live update can + // use tail-mode trimming while the reader is moving away. + // snapshotFlow observes followsTail and reports actual demand; + // do not cancel a valid near-top load on every new finger-down. + } + } + LaunchedEffect(state.sessionId, jumpToken) { + if (jumpToken > 0 && jumpToken != consumedJumpToken) followsTail = true + // Tokens are scoped to the ViewModel; it may restart at zero after + // process recreation. Rebase without treating zero as a jump. + consumedJumpToken = jumpToken + } + + // layoutInfo changes on every scroll frame. Reading it in composition + // invalidates the ENTIRE transcript, even while browsing old messages. + // Observe tail geometry in an effect instead; drop geometry observation + // altogether while not following. collectLatest cancels our correction + // immediately when a reader takes over the scroll. + LaunchedEffect(state.sessionId, listState) { + snapshotFlow { + if (!followsTail || isDragged) null else { + val info = listState.layoutInfo + val lastIndex = currentRows.size + TailLayout( + lastIndex = lastIndex, + itemCount = info.totalItemsCount, + viewportHeight = info.viewportSize.height, + lastHeight = info.visibleItemsInfo.lastOrNull()?.takeIf { it.index == lastIndex }?.size, + canScrollForward = listState.canScrollForward, + messagesVersion = currentState.messagesVersion, + jumpToken = currentJumpToken, + ) + } + }.collectLatest { tail -> + if (tail == null || tail.lastIndex == 0 || tail.viewportHeight == 0 || + tail.itemCount != tail.lastIndex + 1 || (!tail.canScrollForward && tail.lastHeight != null)) return@collectLatest + // Geometry notifications can arrive during placement. Queue the + // correction outside that pass; scrollToItem forces remeasurement. + withContext(Dispatchers.Main) { + if (!followsTail || isDragged) return@withContext + correctingTail = true + try { + // Includes the fixed history header. The large offset + // clamps to the end even for a multi-screen last message. + listState.scrollToItem(tail.lastIndex, Int.MAX_VALUE) + } finally { + correctingTail = false + } + } + } + } + LaunchedEffect(rows) { + val live = rows.mapTo(mutableSetOf()) { it.id } + heights.keys.retainAll(live) + expandedGroups.keys.retainAll(state.blocks.filterIsInstance().map { it.id }.toSet()) + // Keep collapsed group children's state too, but evict messages that + // have actually left the bounded protocol window. + val retained = state.blocks.flatMap { block -> + if (block is ToolGroupBlock) listOf(block.id) + block.tools.map { it.id } + else listOf(block.stableId) + }.toSet() + (stateKeys - retained).forEach(rowState::removeState) + stateKeys.retainAll(retained) + stateKeys.addAll(retained) + } + + Box(modifier.fillMaxSize()) { + LazyColumn( + state = listState, + modifier = Modifier.fillMaxSize().testTag("chat-transcript").layout { measurable, constraints -> + // A same-key page can still resize rows. Reading the version + // here forces a measure/placement pass even for a hidden-only + // page; acknowledging composition alone is too early. + val version = state.historyVersion + val placeable = measurable.measure(constraints) + layout(placeable.width, placeable.height) { + placeable.place(0, 0) + renderedHistoryVersion = version + } + }, + contentPadding = PaddingValues(horizontal = 12.dp, vertical = 10.dp), + verticalArrangement = Arrangement.spacedBy(10.dp, Alignment.Bottom), + ) { + item(key = HISTORY_KEY, contentType = HISTORY_KEY) { + HistoryControl(paging.phase, state.hasMore, state.isSyncingTail, onRetryHistory) + } + items(rows, key = { it.id }, contentType = { it.block.contentKind }) { row -> + rowState.SaveableStateProvider(row.id) { + val rowModifier = Modifier + .onSizeChanged { heights[row.id] = it.height } + .testTag("chat-row-" + row.id) + val group = row.block as? ToolGroupBlock + if (group != null) { + ToolGroupBlockView( + block = group, basePath = state.basePath, modifier = rowModifier, + expandedOverride = row.expanded, + onExpandedChange = { expandedGroups[group.id] = it }, + showsTools = false, + ) + } else { + ChatBlockCard(block = row.block, basePath = state.basePath, modifier = rowModifier) + } + } + } + } + if (!followsTail || state.requiresLatestReset || jumpingLatest) { + Surface( + shape = androidx.compose.foundation.shape.CircleShape, + tonalElevation = 4.dp, + modifier = Modifier.align(Alignment.BottomEnd).padding(12.dp), + ) { + TextButton( + onClick = onJumpToLatest, enabled = !jumpingLatest, + modifier = Modifier.testTag("chat-latest"), + ) { + if (jumpingLatest) CircularProgressIndicator(Modifier.size(14.dp), strokeWidth = 2.dp) + Text(stringResource(R.string.chat_back_to_latest)) + } + } + } + } +} + +private data class LayoutReport( + val version: Long, + val matches: Boolean, + val short: Boolean, + val nearTop: Boolean, + val atBottom: Boolean, + val scrolling: Boolean, + val ownScroll: Boolean, + val following: Boolean, + val ids: Set, + val visibleHeight: Int, +) + +private data class TailLayout( + val lastIndex: Int, + val itemCount: Int, + val viewportHeight: Int, + val lastHeight: Int?, + val canScrollForward: Boolean, + val messagesVersion: Long, + val jumpToken: Long, +) + +@Composable +private fun HistoryControl( + phase: ChatHistoryPagingState.Phase, + hasMore: Boolean, + syncing: Boolean, + onClick: () -> Unit, +) { + val busy = phase == ChatHistoryPagingState.Phase.Loading || + phase == ChatHistoryPagingState.Phase.Retrying || phase is ChatHistoryPagingState.Phase.AwaitingLayout + TextButton( + onClick = onClick, enabled = hasMore && !busy && !syncing, + modifier = Modifier.fillMaxWidth().height(44.dp).testTag("chat-history"), + ) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { + if (hasMore && busy) CircularProgressIndicator(Modifier.size(14.dp), strokeWidth = 2.dp) + Text( + stringResource(when { + !hasMore -> R.string.chat_history_beginning + busy -> R.string.chat_loading_older + phase == ChatHistoryPagingState.Phase.Failed -> R.string.chat_history_retry + phase == ChatHistoryPagingState.Phase.Paused -> R.string.chat_history_continue + hasMore -> R.string.chat_history_load + else -> R.string.chat_history_beginning + }), + style = MaterialTheme.typography.labelSmall, + ) + } + } +} diff --git a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt index e81c90ae..25ef1108 100644 --- a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt +++ b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt @@ -1,9 +1,13 @@ package app.hapi.companion.feature.chat +import androidx.annotation.MainThread import app.hapi.companion.feature.chat.attachments.ComposerAttachments import app.hapi.companion.feature.chat.composer.ChatDrafts import app.hapi.companion.feature.chat.composer.SlashCommands import app.hapi.companion.feature.chat.composer.appendTranscript +import app.hapi.companion.ui.markdown.MarkdownRenderCache +import app.hapi.protocol.chat.AgentTextBlock +import app.hapi.protocol.chat.AgentReasoningBlock import app.hapi.companion.feature.sessions.SessionListViewModel import app.hapi.companion.feature.sessions.formatReopenError import app.hapi.data.api.ApiError @@ -16,6 +20,7 @@ import app.hapi.data.store.LastSeenStore import app.hapi.data.store.MachineListStore import app.hapi.data.store.MessageWindowStore import app.hapi.data.store.MessageWindowStores +import app.hapi.data.store.ChatHistoryPagingState import app.hapi.data.store.ScratchlistCreateResult import app.hapi.data.store.SessionDetailStore import app.hapi.data.store.SessionScratchlist @@ -34,6 +39,8 @@ import app.hapi.protocol.chat.normalizeDecryptedMessage import app.hapi.protocol.chat.reduceChatBlocks import app.hapi.protocol.window.MessageStatus import app.hapi.protocol.window.MessageWindowState +import app.hapi.protocol.window.MessageViewMode +import app.hapi.protocol.window.OlderLoadOutcome import app.hapi.protocol.window.WindowMessage import app.hapi.protocol.window.asWindowMessage import app.hapi.protocol.wire.AgentState @@ -50,6 +57,7 @@ import app.hapi.protocol.wire.arrayOrNull import app.hapi.protocol.wire.objOrNull import app.hapi.protocol.wire.stringOrNull import java.util.UUID +import java.util.concurrent.atomic.AtomicBoolean import kotlin.coroutines.cancellation.CancellationException import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope @@ -78,6 +86,9 @@ import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.transform import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import kotlinx.coroutines.yield +import kotlinx.coroutines.flow.asStateFlow import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonPrimitive @@ -127,8 +138,11 @@ data class ChatUiState( val loadFailed: Boolean, /** Tail sync warning — the connection/staleness banner. */ val warning: String?, - /** Bumps on tail-side content changes; drives the new-messages pill. */ + /** Bumps on tail-side content changes. */ val tailRevision: Long, + val historyVersion: Long = 0, + val messagesVersion: Long = 0, + val requiresLatestReset: Boolean = false, ) /** Composer bar state (M3a). */ @@ -316,7 +330,15 @@ class ChatViewModel( private val now: () -> Long = System::currentTimeMillis, /** Web `makeClientSideId('local')` twin; injectable for deterministic tests. */ private val localIdGenerator: () -> String = { "local-${UUID.randomUUID()}" }, + /** Serialized UI event loop; JVM tests inject their UI dispatcher. */ + uiDispatcher: CoroutineDispatcher = Dispatchers.Main, ) { + // The holder's scope is a Default worker scope, NOT the coordinator's + // executor. Share its lifetime, but serialize all history/lifecycle state + // with Compose callbacks. Use queued Main (not Main.immediate), so a + // synchronous completion cannot clear a job before its handle is assigned. + private val uiScope = CoroutineScope(scope.coroutineContext + uiDispatcher) + private val workerContext = scope.coroutineContext.minusKey(Job) private val router = SyncEventRouter(syncTargets) private val subscriptionKey = SseSubscriptionKey.Session(sessionId) @@ -327,6 +349,25 @@ class ChatViewModel( private var initJob: Job? = null private var seenJob: Job? = null private var olderJob: Job? = null + private var jumpJob: Job? = null + private val mutableHistoryPaging = MutableStateFlow(ChatHistoryPagingState()) + val historyPaging: StateFlow = mutableHistoryPaging.asStateFlow() + private val mutableJumpToken = MutableStateFlow(0L) + val jumpToken: StateFlow = mutableJumpToken.asStateFlow() + private val mutableJumpingLatest = MutableStateFlow(false) + val jumpingLatest: StateFlow = mutableJumpingLatest.asStateFlow() + private var historyObserver: Job? = null + private var historyRetryJob: Job? = null + private var historyCancellation: Job? = null + private var windowModeJob: Job? = null + private var historyGate: AtomicBoolean? = null + private var historyDemand = false + private var readerFollowsTail = true + private var historyPumpScheduled = false + private var lastHistoryLayout: Pair? = null + private var lastHistoryEpoch: Long? = null + private var started = false + private var everStarted = false private var draftJob: Job? = null // ------------------------------------------------------------ M3 state -- @@ -370,6 +411,8 @@ class ChatViewModel( // Pipeline memo state — touched only inside the single uiState map stage. private val normalizeCache = HashMap() private var previousGroups: List = emptyList() + internal val markdownCache = MarkdownRenderCache() + private var previousMarkdownSources = emptySet() private class NormalizeCacheEntry(val source: WindowMessage, val normalized: NormalizedMessage?) @@ -471,13 +514,35 @@ class ChatViewModel( // ------------------------------------------------------------ lifecycle -- /** Idempotent; call from the screen's composition, paired with [stop]. */ + @MainThread fun start() { - if (initJob?.isActive == true || sseJob?.isActive == true) return + if (started) return + val preservingHistory = everStarted && (!readerFollowsTail || windowStore.value?.state?.value?.requiresLatestReset == true) + started = true + everStarted = true - initJob = scope.launch { - val store = messageWindows.open(sessionId) - store.activate() + initJob = uiScope.launch { + val store = withContext(workerContext) { + messageWindows.open(sessionId).also { + if (preservingHistory) it.setViewMode(MessageViewMode.History) else it.activate() + } + } windowStore.value = store + historyObserver?.cancel() + historyObserver = uiScope.launch { + store.state.collect { window -> + mutableHistoryPaging.value = mutableHistoryPaging.value.refreshAvailability(window.hasMore) + val epoch = window.epoch + if (epoch != null) { + if (lastHistoryEpoch != null && lastHistoryEpoch != epoch) { + cancelHistory() + mutableJumpToken.value += 1 + } + lastHistoryEpoch = epoch + } + scheduleHistoryPump() + } + } // Subscribe only after the window exists: every routed message // event / gap resync then finds a peekable window, and the @@ -489,14 +554,15 @@ class ChatViewModel( .collect { router.route(subscriptionKey, it) } } - launch { + launch(workerContext) { + if (preservingHistory) return@launch runCatching { store.syncTail() } // Now that sends exist, verify optimistic queued rows against // the hub on every chat open (web queued-state reconciliation). runCatching { store.reconcileQueuedState() } } - launch { restoreDraft() } - loadDetail() + launch(workerContext) { restoreDraft() } + withContext(workerContext) { loadDetail() } } // Badge count + SSE-triggered refetches while this chat is on screen. @@ -517,12 +583,19 @@ class ChatViewModel( } /** Tears the session pipe down (engine keeps the resume cursor). */ + @MainThread fun stop() { + started = false + historyObserver?.cancel() + historyObserver = null sseJob?.cancel() sseJob = null initJob?.cancel() seenJob?.cancel() - olderJob?.cancel() + cancelHistory() + jumpJob?.cancel() + jumpJob = null + mutableJumpingLatest.value = false flushPendingDraft() sseEngine.unsubscribe(subscriptionKey) sessionStore.releaseDetail(sessionId) @@ -554,12 +627,143 @@ class ChatViewModel( } } - /** Top-edge reached: one older page (no-ops while one is in flight). */ + /** Explicit retry/continue. Ordinary paging is driven by viewport coverage. */ + @MainThread fun loadOlder() { + mutableHistoryPaging.value = mutableHistoryPaging.value.resume() + historyDemand = true + scheduleHistoryPump() + } + + @MainThread + fun readingViewportChanged(followsTail: Boolean, needsOlder: Boolean) { + val changed = readerFollowsTail != followsTail + readerFollowsTail = followsTail + historyDemand = needsOlder + val store = windowStore.value + if (changed && !mutableJumpingLatest.value && store != null) { + if (followsTail && store.state.value.requiresLatestReset) { jumpToLatest(); return } + val previous = windowModeJob + windowModeJob = uiScope.launch { + previous?.join() + withContext(workerContext) { + store.setViewMode(if (followsTail) MessageViewMode.Tail else MessageViewMode.History) + } + } + } + if (!needsOlder && (olderJob != null || mutableHistoryPaging.value.phase == ChatHistoryPagingState.Phase.Retrying)) { + cancelHistory() + } + scheduleHistoryPump() + } + + @MainThread + fun historyLaidOut(version: Long, madeProgress: Boolean) { + val old = lastHistoryLayout + lastHistoryLayout = version to (madeProgress || (old?.first == version && old.second)) + acknowledgeHistoryLayout() + } + + private fun acknowledgeHistoryLayout() { + val layout = lastHistoryLayout ?: return + val old = mutableHistoryPaging.value + val next = old.laidOut(layout.first, layout.second) + mutableHistoryPaging.value = next + if (next != old) scheduleHistoryPump() + } + + private fun scheduleHistoryPump() { + if (historyPumpScheduled) return + historyPumpScheduled = true + uiScope.launch { + yield() + historyPumpScheduled = false + pumpHistory() + } + } + + private fun pumpHistory() { val store = windowStore.value ?: return - if (olderJob?.isActive == true) return - olderJob = scope.launch { - runCatching { store.fetchOlder() } + val window = store.state.value + if (!started || mutableJumpingLatest.value || !historyDemand || !window.hasMore || + window.isSyncingTail || window.isLoadingMore || olderJob != null || + mutableHistoryPaging.value.phase != ChatHistoryPagingState.Phase.Idle) return + val request = mutableHistoryPaging.value.begin() + mutableHistoryPaging.value = request + val gate = AtomicBoolean(true) + historyGate = gate + val cancellation = historyCancellation + val modeChange = windowModeJob + olderJob = uiScope.launch { + cancellation?.join() + modeChange?.join() + if (!gate.get()) return@launch + val outcome = withContext(workerContext) { store.fetchOlder(onBeforeApply = { gate.get() }) } + if (mutableHistoryPaging.value.generation != request.generation) return@launch + olderJob = null + historyGate = null + mutableHistoryPaging.value = mutableHistoryPaging.value.received(outcome, request.generation) + acknowledgeHistoryLayout() + // Tail sync may have completed while olderJob blocked observer + // pumps. An invalidated page has no layout callback to wake us. + if (outcome == OlderLoadOutcome.Stopped(OlderLoadOutcome.StopReason.Invalidated) || + outcome == OlderLoadOutcome.Stopped(OlderLoadOutcome.StopReason.EpochReset)) { + scheduleHistoryPump() + } + mutableHistoryPaging.value.retryDelayMillis?.let { milliseconds -> + historyRetryJob = uiScope.launch { + delay(milliseconds) + mutableHistoryPaging.value = mutableHistoryPaging.value.retryElapsed(request.generation) + scheduleHistoryPump() + } + } + } + } + + private fun cancelHistory() { + historyGate?.set(false) + historyGate = null + olderJob?.cancel() + olderJob = null + historyRetryJob?.cancel() + historyRetryJob = null + mutableHistoryPaging.value = mutableHistoryPaging.value.cancel() + lastHistoryLayout = null + windowStore.value?.let { store -> + val previous = historyCancellation + historyCancellation = uiScope.launch { + previous?.join() + withContext(workerContext) { store.cancelOlderLoad() } + } + } + } + + @MainThread + fun jumpToLatest() { + if (!started || mutableJumpingLatest.value) return + val store = windowStore.value ?: return + cancelHistory() + historyDemand = false + mutableJumpingLatest.value = true + val cancellation = historyCancellation + val modeChange = windowModeJob + jumpJob = uiScope.launch { + cancellation?.join() + modeChange?.join() + if (store.state.value.requiresLatestReset) { + withContext(workerContext) { store.syncTail(ensureAfterCurrent = true, allowingHistoryReset = true) } + } + if (!started) return@launch + if (store.state.value.requiresLatestReset) { + mutableJumpingLatest.value = false + jumpJob = null + return@launch + } + withContext(workerContext) { store.setViewMode(MessageViewMode.Tail) } + mutableJumpingLatest.value = false + jumpJob = null + readerFollowsTail = true + mutableJumpToken.value += 1 } } @@ -1444,6 +1648,16 @@ class ChatViewModel( ToolGroupingOptions(hasMoreMessages = window.hasMore, previousGroups = previousGroups), ) previousGroups = visibleBlocks.filterIsInstance() + val sources = visibleBlocks.mapNotNull { block -> + when (block) { + is AgentTextBlock -> block.text + is AgentReasoningBlock -> block.text + else -> null + } + }.toSet() + // buildUiState runs on pipelineDispatcher, before these rows reach UI. + markdownCache.prepare(sources - previousMarkdownSources) + previousMarkdownSources = sources prunePermissionOverrides(agentState, inputs.permissionOverrides) @@ -1466,6 +1680,9 @@ class ChatViewModel( (window.warning != null || inputs.detailLoadFailed), warning = window.warning, tailRevision = window.tailRevision, + historyVersion = window.historyVersion, + messagesVersion = window.messagesVersion, + requiresLatestReset = window.requiresLatestReset, ) } @@ -1548,4 +1765,3 @@ class ChatViewModel( } == true } } - diff --git a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/PermissionActionViews.kt b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/PermissionActionViews.kt index 0a2fd66a..db3d7437 100644 --- a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/PermissionActionViews.kt +++ b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/PermissionActionViews.kt @@ -23,6 +23,7 @@ import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -216,8 +217,8 @@ private fun AskUserQuestionFooter( val questions = remember(tool.id, tool.input) { parseAskUserQuestions(tool.input, cursorDialect) } // Selection state per question index. - val selected = remember(tool.id) { mutableStateOf(mapOf>()) } - val otherText = remember(tool.id) { mutableStateOf(mapOf()) } + val selected = rememberSaveable(tool.id) { mutableStateOf(mapOf>()) } + val otherText = rememberSaveable(tool.id) { mutableStateOf(mapOf()) } var validationError by remember(tool.id) { mutableStateOf(null) } Column( @@ -413,8 +414,8 @@ private fun RequestUserInputFooter( val resolving = override == PermissionRowOverride.Resolving val questions = remember(tool.id, tool.input) { parseRequestUserInputQuestions(tool.input) } - val selected = remember(tool.id) { mutableStateOf(mapOf>()) } - val notes = remember(tool.id) { + val selected = rememberSaveable(tool.id) { mutableStateOf(mapOf>()) } + val notes = rememberSaveable(tool.id) { mutableStateOf(questions.associate { it.id to it.prefill.orEmpty() }) } var validationError by remember(tool.id) { mutableStateOf(null) } diff --git a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/ToolGroupBlockView.kt b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/ToolGroupBlockView.kt index cb4dfa6a..6a81c8db 100644 --- a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/ToolGroupBlockView.kt +++ b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/blocks/ToolGroupBlockView.kt @@ -40,8 +40,14 @@ import app.hapi.protocol.chat.ToolGroupSummary * [ToolCallBlockView]s. Codex exploration groups honor their `defaultOpen`. */ @Composable -fun ToolGroupBlockView(block: ToolGroupBlock, basePath: String?, modifier: Modifier = Modifier) { - var expanded by rememberSaveable(block.id) { mutableStateOf(block.defaultOpen) } +fun ToolGroupBlockView( + block: ToolGroupBlock, basePath: String?, modifier: Modifier = Modifier, + expandedOverride: Boolean? = null, + onExpandedChange: ((Boolean) -> Unit)? = null, + showsTools: Boolean = true, +) { + var localExpanded by rememberSaveable(block.id) { mutableStateOf(block.defaultOpen) } + val expanded = expandedOverride ?: localExpanded val colors = MaterialTheme.hapi val summaryText = remember(block) { groupSummaryText(block) } @@ -54,7 +60,7 @@ fun ToolGroupBlockView(block: ToolGroupBlock, basePath: String?, modifier: Modif Row( modifier = Modifier .fillMaxWidth() - .clickable { expanded = !expanded } + .clickable { if (onExpandedChange != null) onExpandedChange(!expanded) else localExpanded = !expanded } .padding(horizontal = 10.dp, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { @@ -99,7 +105,7 @@ fun ToolGroupBlockView(block: ToolGroupBlock, basePath: String?, modifier: Modif ) } } - if (expanded) { + if (showsTools && expanded) { Column( modifier = Modifier.padding(start = 10.dp, end = 10.dp, bottom = 10.dp), verticalArrangement = Arrangement.spacedBy(6.dp), diff --git a/android/app/src/main/kotlin/app/hapi/companion/ui/markdown/Markdown.kt b/android/app/src/main/kotlin/app/hapi/companion/ui/markdown/Markdown.kt index 3489f14a..8ec07403 100644 --- a/android/app/src/main/kotlin/app/hapi/companion/ui/markdown/Markdown.kt +++ b/android/app/src/main/kotlin/app/hapi/companion/ui/markdown/Markdown.kt @@ -21,6 +21,8 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.getValue +import androidx.compose.runtime.produceState import androidx.compose.runtime.remember import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.Alignment @@ -53,6 +55,8 @@ import app.hapi.companion.ui.theme.hapi import app.hapi.protocol.markdown.HrefDecision import app.hapi.protocol.markdown.HrefPolicy import app.hapi.protocol.markdown.MarkdownTransforms +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import org.commonmark.ext.gfm.strikethrough.Strikethrough import org.commonmark.ext.gfm.tables.TableBlock import org.commonmark.ext.gfm.tables.TableCell @@ -117,17 +121,28 @@ val LocalMarkdownLinkHandler = staticCompositionLocalOf { N */ @Composable fun Markdown(text: String, modifier: Modifier = Modifier) { - val parsed = remember(text) { prepareDocument(text) } + val cache = LocalMarkdownRenderCache.current + val parsed = if (cache == null) { + remember(text) { prepareDocument(text) } + } else { + // Ready hits have no placeholder frame when a LazyColumn cell returns. + // A miss retains the previous revision until background parsing settles. + val prepared by produceState(cache.cached(text), text, cache) { + value = withContext(Dispatchers.Default) { cache.document(text) } + } + cache.cached(text) ?: prepared + } val baseStyle = MaterialTheme.typography.bodyLarge.copy(fontSize = 15.sp, lineHeight = 22.sp) CompositionLocalProvider(LocalTextStyle provides baseStyle) { Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(10.dp)) { - MarkdownBlockChildren(parsed.document, parsed) + if (parsed != null) MarkdownBlockChildren(parsed.document, parsed) + else Text(text) } } } /** Parsed AST plus the task-list markers stripped out of it (identity-keyed). */ -private class ParsedMarkdown(val document: Node, val taskMarkers: Map) +internal class ParsedMarkdown(val document: Node, val taskMarkers: Map) private val TASK_MARKER = Regex("""^\[([ xX])\]\s+""") @@ -136,7 +151,7 @@ private val TASK_MARKER = Regex("""^\[([ xX])\]\s+""") * removed from the first text node and remembered per list item, so the walker * can draw a checkbox prefix (the task extension jar is not needed for this). */ -private fun prepareDocument(text: String): ParsedMarkdown { +internal fun prepareDocument(text: String): ParsedMarkdown { val document = MarkdownTransforms.parse(text) val markers = HashMap() var node: Node? = document diff --git a/android/app/src/main/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCache.kt b/android/app/src/main/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCache.kt new file mode 100644 index 00000000..a1901ffe --- /dev/null +++ b/android/app/src/main/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCache.kt @@ -0,0 +1,43 @@ +package app.hapi.companion.ui.markdown + +import androidx.compose.runtime.staticCompositionLocalOf + +internal val LocalMarkdownRenderCache = staticCompositionLocalOf { null } + +/** Per-chat LRU. Trees are normalized before publication, then read-only. + * Call document/prepare off the UI thread; cached is a synchronous ready hit. */ +internal class MarkdownRenderCache(private val maxCost: Long = 8L * 1024 * 1024) { + private val entries = LinkedHashMap(16, 0.75f, true) + private var cost = 0L + + @Synchronized fun cached(text: String): ParsedMarkdown? = entries[text] + + fun document(text: String): ParsedMarkdown { + cached(text)?.let { return it } + val parsed = prepareDocument(text) + synchronized(this) { + entries[text]?.let { return it } + val size = costOf(text) + if (size <= maxCost) { + entries[text] = parsed + cost += size + while (cost > maxCost || entries.size > 800) { + val iterator = entries.iterator() + val oldest = iterator.next() + cost -= costOf(oldest.key) + iterator.remove() + } + } + } + return parsed + } + + fun prepare(sources: Set) { + for (text in sources) { + if (costOf(text) <= maxCost) document(text) + } + } + + // Approximate AST + source cost, not a heap-size guarantee. + private fun costOf(text: String): Long = maxOf(1, text.length.toLong() * 8) +} diff --git a/android/app/src/main/res/values-zh-rCN/strings.xml b/android/app/src/main/res/values-zh-rCN/strings.xml index b68dbf00..0c6f2cac 100644 --- a/android/app/src/main/res/values-zh-rCN/strings.xml +++ b/android/app/src/main/res/values-zh-rCN/strings.xml @@ -529,4 +529,9 @@ 创建会话前,请升级并重启此机器上的 HAPI runner。 此机器上没有安装受支持的代理。 所选代理在此机器上不可用。 + 回到最新 ↓ + 历史加载失败,点此重试 + 继续加载历史 + 加载更早的消息 + 已到会话起点 diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 12b20dcf..3da49308 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -522,4 +522,9 @@ Upgrade and restart this machine’s HAPI runner before creating sessions. No supported Agents are installed on this machine. The selected Agent is not available on this machine. + Back to latest ↓ + Couldn\'t load history. Tap to retry. + Continue loading history + Load older messages + Beginning of conversation diff --git a/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt b/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt index bcfd1449..9a4a98de 100644 --- a/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt +++ b/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt @@ -424,6 +424,7 @@ private class InteractionHarness( drafts = drafts, scratchlist = scratchlist, pipelineDispatcher = StandardTestDispatcher(testScope.testScheduler), + uiDispatcher = StandardTestDispatcher(testScope.testScheduler), draftSaveDebounceMs = 10, now = { 1_000L }, localIdGenerator = { "local-${++localIdCounter}" }, @@ -506,6 +507,9 @@ class ChatViewModelInteractionTest { harness.api.sendFailures += ApiError(409, code = "session_inactive") harness.api.resumeResult = ResumeSessionResponse(sessionId = IX_SESSION) harness.viewModel.start() + // Entry loading crosses the UI/worker boundary; finish it before + // testing an action that intentionally changes the server snapshot. + harness.sessionStore.calls.first { "loadDetail:$IX_SESSION" in it } harness.viewModel.setComposerText("wake up") harness.viewModel.sendMessage() @@ -902,6 +906,7 @@ class ChatViewModelInteractionTest { val harness = InteractionHarness(this, detail(permissionMode = "default")) harness.api.configFailure = ApiError(409, code = "apply_failed") harness.viewModel.start() + harness.sessionStore.calls.first { "loadDetail:$IX_SESSION" in it } var notice: ChatNotice? = null val collector = launch(start = CoroutineStart.UNDISPATCHED) { diff --git a/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelTest.kt b/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelTest.kt index 9ab8a3c4..256a021e 100644 --- a/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelTest.kt +++ b/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelTest.kt @@ -7,6 +7,8 @@ import app.hapi.data.sse.SseEngine import app.hapi.data.sse.SseRawEvent import app.hapi.data.sse.SseTransport import app.hapi.data.sse.TransportEvent +import app.hapi.data.store.ChatHistoryPagingState +import app.hapi.protocol.window.MessageViewMode import app.hapi.data.store.LastSeenStore import app.hapi.data.store.MachineListStore import app.hapi.data.store.MessageWindowStores @@ -32,10 +34,23 @@ import app.hapi.protocol.wire.SessionSummary import app.hapi.protocol.wire.SessionSummaryMetadata import app.hapi.protocol.wire.SyncEvent import java.io.File +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executors +import java.util.concurrent.atomic.AtomicInteger import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.Job +import kotlinx.coroutines.asCoroutineDispatcher +import kotlinx.coroutines.cancel +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeout import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow @@ -48,6 +63,8 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.runCurrent +import kotlinx.coroutines.CompletableDeferred import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.jsonObject @@ -121,13 +138,25 @@ private class FakeMachineStore : MachineListStore { } /** Scripted [ChatSessionApi]: `latest`/`after` serve [tailResponses] in order, `before` serves [beforePage]. */ -private class FakeMessagesApi : ChatSessionApi { +private open class FakeMessagesApi : ChatSessionApi { val queries = MutableStateFlow>(emptyList()) val tailResponses = ArrayDeque() var beforePage: MessagesResponse? = null + var tailFailure = false + var tailGate: CompletableDeferred? = null + var beforeGate: CompletableDeferred? = null override suspend fun getMessages(sessionId: String, query: MessagesQuery): MessagesResponse { queries.value = queries.value + query + if (query is MessagesQuery.Before) { + val response = beforePage ?: emptyLatest(direction = "before") + beforeGate?.await() + return response + } + if (query !is MessagesQuery.Before) { + tailGate?.await() + if (tailFailure) error("offline") + } return when (query) { is MessagesQuery.Before -> beforePage ?: emptyLatest(direction = "before") // A drained script answers per query direction: an "after" query @@ -273,8 +302,10 @@ private class Harness( testScope: TestScope, transport: SseTransport, val api: FakeMessagesApi = FakeMessagesApi(), + val scope: CoroutineScope = testScope.backgroundScope, + uiDispatcher: CoroutineDispatcher = StandardTestDispatcher(testScope.testScheduler), + pipelineDispatcher: CoroutineDispatcher = StandardTestDispatcher(testScope.testScheduler), ) { - val scope: CoroutineScope = testScope.backgroundScope val sessionStore = FakeSessionStore().apply { detailToLoad = detailSession() } val lastSeenStore = LastSeenStore(scope) val messageWindows = MessageWindowStores(api = api, scope = scope) @@ -294,13 +325,222 @@ private class Harness( sseEngine = engine, syncTargets = StoreSyncTargets(sessionStore, FakeMachineStore(), scope, messageWindows), scope = scope, - pipelineDispatcher = StandardTestDispatcher(testScope.testScheduler), + pipelineDispatcher = pipelineDispatcher, + uiDispatcher = uiDispatcher, ) } // ------------------------------------------------------------------ tests -- +@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) class ChatViewModelTest { + @Test + fun `Default workers cannot mutate paging state outside the UI dispatcher`() = runTest { + val workers = CoroutineScope(SupervisorJob() + Dispatchers.Default) + val ui = Executors.newSingleThreadExecutor { Thread(it, "hapi-history-ui") }.asCoroutineDispatcher() + try { + // Real worker threads and a separate serial UI executor, not a + // shared single-threaded TestDispatcher hiding producer/UI races. + withContext(Dispatchers.Default) { + withTimeout(10_000) { + val inFlight = AtomicInteger() + val peak = AtomicInteger() + val beforeCount = AtomicInteger() + val uiThread = withContext(ui) { Thread.currentThread() } + val requestThreads = CopyOnWriteArrayList() + val api = object : FakeMessagesApi() { + override suspend fun getMessages(sessionId: String, query: MessagesQuery): MessagesResponse { + requestThreads += Thread.currentThread() + if (query is MessagesQuery.Before) { + beforeCount.incrementAndGet() + peak.accumulateAndGet(inFlight.incrementAndGet(), ::maxOf) + try { + delay(1) + val seq = query.beforeSeq - 1 + return MessagesResponse( + listOf(agentMessage("row-$seq", seq, seq * 1000, "Row $seq")), + page("before", seq > 1, nextBeforeAt = seq * 1000, nextBeforeSeq = seq), + ) + } finally { + inFlight.decrementAndGet() + } + } + return if (query is MessagesQuery.Latest) MessagesResponse( + listOf(agentMessage("row-21", 21, 21_000, "Row 21")), + page("latest", true, nextBeforeAt = 21_000, nextBeforeSeq = 21), + ) else emptyLatest(direction = "after") + } + } + val h = Harness(this@runTest, ScriptedTransport(listOf("ok")), api, + scope = workers, uiDispatcher = ui, pipelineDispatcher = Dispatchers.Default) + val transitions = CopyOnWriteArrayList>() + try { + withContext(ui) { + // Unconfined observation records the writer's + // thread rather than hopping to a collector thread. + workers.launch(Dispatchers.Unconfined) { + h.viewModel.historyPaging.collect { + transitions += it.phase to Thread.currentThread() + } + } + h.viewModel.start() + } + h.viewModel.uiState.first { it.blocks.isNotEmpty() && !it.isSyncingTail } + val store = h.messageWindows.open(SESSION_ID) + store.syncTail(ensureAfterCurrent = true) + // Acknowledge the committed version immediately. This + // can beat fetchOlder's completion back to the UI queue. + workers.launch(Dispatchers.Unconfined) { + store.state.map { it.historyVersion }.distinctUntilChanged().collect { version -> + if (version > 0) withContext(ui) { h.viewModel.historyLaidOut(version, true) } + } + } + withContext(ui) { h.viewModel.readingViewportChanged(false, true) } + store.state.first { it.historyVersion == 20L } + h.viewModel.historyPaging.first { + it.phase == ChatHistoryPagingState.Phase.Idle && it.generation >= 20 + } + assertEquals(20, beforeCount.get()) + assertEquals(1, peak.get()) + assertTrue(transitions.any { it.first == ChatHistoryPagingState.Phase.Loading }) + assertTrue(transitions.all { it.second == uiThread }, transitions.toString()) + assertTrue(requestThreads.all { it != uiThread }, "Message work must stay off the UI thread") + } finally { + withContext(ui) { h.viewModel.stop() } + } + } + } + } finally { + workers.cancel() + workers.coroutineContext[Job]?.join() + ui.close() + } + } + + @Test + fun `invalidated older response restarts pending demand after tail sync finished`() = runTest { + val h = Harness(this, ScriptedTransport(listOf("ok"))) + h.api.tailResponses += MessagesResponse( + listOf(agentMessage("new", 10, 10_000, "new")), + page("latest", true, nextBeforeAt = 10_000, nextBeforeSeq = 10), + ) + h.api.beforePage = MessagesResponse( + listOf(agentMessage("stale", 9, 9_000, "stale")), + page("before", true, nextBeforeAt = 9_000, nextBeforeSeq = 9), + ) + val gate = CompletableDeferred() + h.api.beforeGate = gate + h.viewModel.start() + h.viewModel.uiState.first { it.blocks.isNotEmpty() } + runCurrent() + h.viewModel.readingViewportChanged(followsTail = false, needsOlder = true) + runCurrent() + assertEquals(1, h.api.queries.value.filterIsInstance().size) + + // Tail sync poisons the older generation, then finishes while the + // slower HTTP response still owns the coordinator's olderJob. + val store = h.messageWindows.open(SESSION_ID) + store.syncTail(ensureAfterCurrent = true) + runCurrent() + assertEquals(false, store.state.value.isSyncingTail) + assertEquals(ChatHistoryPagingState.Phase.Loading, h.viewModel.historyPaging.value.phase) + h.api.beforePage = MessagesResponse( + listOf(agentMessage("fresh", 8, 8_000, "fresh")), + page("before", false, nextBeforeAt = 8_000, nextBeforeSeq = 8), + ) + h.api.beforeGate = null + gate.complete(Unit) + runCurrent() + + // No new gesture, layout callback, or store event to unstick the pump. + assertEquals(2, h.api.queries.value.filterIsInstance().size) + assertEquals(listOf("fresh", "new"), store.state.value.messages.map { it.id }) + assertTrue(h.viewModel.historyPaging.value.phase is ChatHistoryPagingState.Phase.AwaitingLayout) + h.viewModel.stop() + } + + @Test + fun `viewport demand waits for layout and resumes without another gesture`() = runTest { + val h = Harness(this, ScriptedTransport(listOf("ok"))) + h.api.tailResponses += MessagesResponse( + listOf(agentMessage("new", 10, 10_000, "new")), + page("latest", true, nextBeforeAt = 10_000, nextBeforeSeq = 10), + ) + h.api.beforePage = MessagesResponse( + listOf(agentMessage("older", 9, 9_000, "older")), + page("before", true, nextBeforeAt = 9_000, nextBeforeSeq = 9), + ) + h.viewModel.start() + h.viewModel.uiState.first { it.blocks.isNotEmpty() } + runCurrent() + h.viewModel.readingViewportChanged(followsTail = false, needsOlder = true) + val first = h.viewModel.uiState.first { it.historyVersion == 1L } + runCurrent() + assertEquals(1, h.api.queries.value.filterIsInstance().size) + assertTrue(h.viewModel.historyPaging.value.phase is ChatHistoryPagingState.Phase.AwaitingLayout) + h.api.beforePage = MessagesResponse( + listOf(agentMessage("oldest", 8, 8_000, "oldest")), + page("before", false, nextBeforeAt = 8_000, nextBeforeSeq = 8), + ) + h.viewModel.historyLaidOut(first.historyVersion, madeProgress = true) + h.viewModel.uiState.first { it.historyVersion == 2L } + assertEquals(2, h.api.queries.value.filterIsInstance().size) + h.viewModel.stop() + } + + @Test + fun `return from an overlay keeps history and failed latest sync remains retryable`() = runTest { + val h = Harness(this, ScriptedTransport(listOf("ok"))) + h.api.tailResponses += MessagesResponse( + listOf(agentMessage("old", 1, 1000, "old")), + page("latest", false, nextBeforeAt = 1000, nextBeforeSeq = 1), + ) + h.viewModel.start() + h.viewModel.uiState.first { it.blocks.isNotEmpty() } + runCurrent() + h.viewModel.readingViewportChanged(false, false) + runCurrent() + val store = h.messageWindows.open(SESSION_ID) + val retained = store.state.value.messages + val queries = h.api.queries.value.size + h.viewModel.stop() + runCurrent() + h.viewModel.start() + runCurrent() + assertEquals(MessageViewMode.History, store.state.value.viewMode) + assertEquals(retained, store.state.value.messages) + assertEquals(queries, h.api.queries.value.size) + + // Seed an explicitly truncated window. Failure must not claim latest. + val seed = h.messageWindows.open("seed") + seed.seedFrom(store) + store.seedFrom(seed) + store.setViewMode(MessageViewMode.History) + h.api.tailFailure = true + h.viewModel.jumpToLatest() + runCurrent() + assertEquals(false, h.viewModel.jumpingLatest.value) + assertEquals(0L, h.viewModel.jumpToken.value) + assertTrue(store.state.value.requiresLatestReset) + assertEquals(MessageViewMode.History, store.state.value.viewMode) + + // Cancelling a pending jump cannot leave a permanent spinner or + // complete a newer screen's navigation when its HTTP response returns. + h.api.tailFailure = false + val gate = CompletableDeferred() + h.api.tailGate = gate + h.viewModel.jumpToLatest() + runCurrent() + assertTrue(h.viewModel.jumpingLatest.value) + h.viewModel.stop() + h.viewModel.start() + runCurrent() + assertEquals(false, h.viewModel.jumpingLatest.value) + gate.complete(Unit) + runCurrent() + assertEquals(0L, h.viewModel.jumpToken.value) + h.viewModel.stop() + } @Test fun `blocks flow from the window store through the pipeline`() = runTest { diff --git a/android/app/src/test/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCacheTest.kt b/android/app/src/test/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCacheTest.kt new file mode 100644 index 00000000..ae53966f --- /dev/null +++ b/android/app/src/test/kotlin/app/hapi/companion/ui/markdown/MarkdownRenderCacheTest.kt @@ -0,0 +1,23 @@ +package app.hapi.companion.ui.markdown + +import kotlin.test.assertNotSame +import kotlin.test.assertNull +import kotlin.test.assertSame +import org.junit.Test + +class MarkdownRenderCacheTest { + @Test fun `ready hits survive recycling and eviction is bounded`() { + val cache = MarkdownRenderCache(maxCost = 32) + val first = cache.document("one") + assertSame(first, cache.cached("one")) + cache.document("two") + assertNull(cache.cached("one")) + assertNotSame(first, cache.document("one")) + } + + @Test fun `oversize trees render but do not enter the shared cache`() { + val cache = MarkdownRenderCache(maxCost = 8) + cache.document("long document") + assertNull(cache.cached("long document")) + } +} diff --git a/android/core/data/src/main/kotlin/app/hapi/data/store/ChatHistoryPagingState.kt b/android/core/data/src/main/kotlin/app/hapi/data/store/ChatHistoryPagingState.kt new file mode 100644 index 00000000..948feb82 --- /dev/null +++ b/android/core/data/src/main/kotlin/app/hapi/data/store/ChatHistoryPagingState.kt @@ -0,0 +1,60 @@ +package app.hapi.data.store + +import app.hapi.protocol.window.OlderLoadOutcome + +/** A page remains in flight until its version has been laid out. UI-thread owned. */ +data class ChatHistoryPagingState( + val phase: Phase = Phase.Idle, + val generation: Long = 0, + val failures: Int = 0, + val pagesWithoutProgress: Int = 0, +) { + sealed interface Phase { + data object Idle : Phase + data object Loading : Phase + data class AwaitingLayout(val historyVersion: Long) : Phase + data object Retrying : Phase + data object Failed : Phase + data object Paused : Phase + data object Exhausted : Phase + } + + fun begin(): ChatHistoryPagingState = + if (phase == Phase.Idle) copy(phase = Phase.Loading, generation = generation + 1) else this + + fun received(result: OlderLoadOutcome, request: Long): ChatHistoryPagingState { + if (generation != request || phase != Phase.Loading) return this + return when (result) { + is OlderLoadOutcome.Applied -> copy(phase = Phase.AwaitingLayout(result.historyVersion), failures = 0) + is OlderLoadOutcome.Failed -> copy( + phase = if (failures < 2) Phase.Retrying else Phase.Failed, + failures = failures + 1, + ) + is OlderLoadOutcome.Stopped -> copy(phase = when (result.reason) { + OlderLoadOutcome.StopReason.Exhausted -> Phase.Exhausted + OlderLoadOutcome.StopReason.CursorDidNotAdvance -> Phase.Paused + else -> Phase.Idle + }) + } + } + + val retryDelayMillis: Long? get() = if (phase != Phase.Retrying) null else if (failures == 1) 500 else 1500 + + fun retryElapsed(request: Long): ChatHistoryPagingState = + if (generation == request && phase == Phase.Retrying) copy(phase = Phase.Idle) else this + + fun laidOut(historyVersion: Long, madeProgress: Boolean): ChatHistoryPagingState { + val waiting = phase as? Phase.AwaitingLayout ?: return this + if (historyVersion < waiting.historyVersion) return this + val emptyPages = if (madeProgress) 0 else pagesWithoutProgress + 1 + return copy(phase = if (emptyPages >= 3) Phase.Paused else Phase.Idle, pagesWithoutProgress = emptyPages) + } + + fun resume(): ChatHistoryPagingState = + if (phase == Phase.Failed || phase == Phase.Paused) copy(phase = Phase.Idle, failures = 0, pagesWithoutProgress = 0) else this + + fun cancel(): ChatHistoryPagingState = ChatHistoryPagingState(generation = generation + 1) + + fun refreshAvailability(hasMore: Boolean): ChatHistoryPagingState = + if (hasMore && phase == Phase.Exhausted) copy(phase = Phase.Idle) else this +} diff --git a/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStore.kt b/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStore.kt index 133469f0..f58057eb 100644 --- a/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStore.kt +++ b/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStore.kt @@ -6,6 +6,7 @@ import app.hapi.protocol.window.MessagePosition import app.hapi.protocol.window.MessageStatus import app.hapi.protocol.window.MessageViewMode import app.hapi.protocol.window.MessageWindowLogic +import app.hapi.protocol.window.HISTORY_WINDOW_SIZE import app.hapi.protocol.window.MessageWindowState import app.hapi.protocol.window.OlderLoadOutcome import app.hapi.protocol.window.PAGE_SIZE @@ -18,6 +19,10 @@ import app.hapi.protocol.wire.SyncEvent import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.withContext import kotlinx.coroutines.Deferred import kotlinx.coroutines.async import kotlinx.coroutines.flow.Flow @@ -62,9 +67,11 @@ class MessageWindowStore( private val scope: CoroutineScope, private val snapshots: WindowSnapshots? = null, initialState: MessageWindowState? = null, + private val historyRetentionLimit: Int = HISTORY_WINDOW_SIZE, ) { private val stateMutex = Mutex() - private val _state = MutableStateFlow(initialState ?: MessageWindowLogic.createState(sessionId)) + private val _state = MutableStateFlow((initialState ?: MessageWindowLogic.createState(sessionId)) + .copy(historyRetentionLimit = historyRetentionLimit)) /** The full window state (UI projects what it needs; see [uiState]). */ val state: StateFlow = _state.asStateFlow() @@ -113,7 +120,17 @@ class MessageWindowStore( * trailing run is requested and awaited, so the caller returns only after * a sync that STARTED at or after this call. */ - suspend fun syncTail(ensureAfterCurrent: Boolean = false) { + suspend fun syncTail(ensureAfterCurrent: Boolean = false, allowingHistoryReset: Boolean = false) { + if (allowingHistoryReset) { + val (epoch, pending) = synchronized(controllerLock) { controllerEpoch to running?.job } + pending?.join() + val job = synchronized(controllerLock) { + if (epoch != controllerEpoch) return + startTailSyncLocked(allowingHistoryReset = true) + } + job.join() + return + } val decision: SyncDecision = synchronized(controllerLock) { val current = running when { @@ -138,14 +155,14 @@ class MessageWindowStore( } } - private fun startTailSyncLocked(): Deferred { + private fun startTailSyncLocked(allowingHistoryReset: Boolean = false): Deferred { val epoch = controllerEpoch val run = TailRun(prefersLatest = _state.value.preferLatestOnActivation) // UNDISPATCHED mirrors the web: `runTailSync` executes to its first true // suspension (normally the api call) before `startTailSync` returns, so // `beginTailSync`'s generation bump lands synchronously and an in-flight // run it replaces cannot commit another page in between. - run.job = scope.async(start = CoroutineStart.UNDISPATCHED) { runTailSync() } + run.job = scope.async(start = CoroutineStart.UNDISPATCHED) { runTailSync(allowingHistoryReset) } running = run run.job.invokeOnCompletion { synchronized(controllerLock) { @@ -176,7 +193,7 @@ class MessageWindowStore( private fun isCurrentTailSync(generation: Long): Boolean = _state.value.syncGeneration == generation - private suspend fun runTailSync() { + private suspend fun runTailSync(allowingHistoryReset: Boolean) { val generation = update { MessageWindowLogic.beginTailSync(it) }.syncGeneration try { val initial = _state.value @@ -193,6 +210,12 @@ class MessageWindowStore( if (!isCurrentTailSync(generation)) return update { previous -> if (previous.syncGeneration != generation) return@update previous + // Even truncated history must validate the server epoch: + // a replay gap may have hidden messages-invalidated. + // Preserve its reading anchor only after that validation. + if (!allowingHistoryReset && previous.viewMode == MessageViewMode.History && + previous.requiresLatestReset && previous.epoch != null && + !response.page.reset && response.page.epoch == previous.epoch) return@update previous MessageWindowLogic.applyLatestResponse( previous, response.windowMessages(), @@ -289,18 +312,27 @@ class MessageWindowStore( * synchronous updater) — it must not call back into this store. */ suspend fun fetchOlder(onBeforeApply: ((Long) -> Boolean)? = null): OlderLoadOutcome { - val initial = _state.value - when (val precheck = MessageWindowLogic.olderLoadPrecheck(initial)) { + // Reserve atomically: concurrent consumers must not both pass the + // precheck before either publishes isLoadingMore. + val (initial, precheck) = stateMutex.withLock { + val initial = _state.value + val check = MessageWindowLogic.olderLoadPrecheck(initial) + if (check is MessageWindowLogic.OlderPrecheck.Proceed) { + _state.value = MessageWindowLogic.beginOlderLoad(initial, initial.olderGeneration + 1) + } + initial to check + } + when (precheck) { is MessageWindowLogic.OlderPrecheck.Stop -> return OlderLoadOutcome.Stopped(precheck.reason) is MessageWindowLogic.OlderPrecheck.Proceed -> { val before = precheck.before val generation = initial.olderGeneration + 1 - update { MessageWindowLogic.beginOlderLoad(it, generation) } return try { val response = api.getMessages( sessionId, MessagesQuery.Before(beforeAt = before.at, beforeSeq = before.seq, limit = PAGE_SIZE), ) + currentCoroutineContext().ensureActive() if (_state.value.olderGeneration != generation) { return OlderLoadOutcome.Stopped(OlderLoadOutcome.StopReason.Invalidated) } @@ -311,6 +343,16 @@ class MessageWindowStore( return OlderLoadOutcome.Stopped(OlderLoadOutcome.StopReason.EpochReset) } + if (response.page.hasMore) { + val at = response.page.nextBeforeAt + val seq = response.page.nextBeforeSeq + if (at == null || seq == null || MessagePosition(at, seq) >= before) { + update { MessageWindowLogic.failOlderLoad(it, generation, + "Message history cursor did not advance") } + return OlderLoadOutcome.Stopped(OlderLoadOutcome.StopReason.CursorDidNotAdvance) + } + } + var historyVersion = 0L var addedRenderableCount = 0 var applyRejected = false @@ -336,6 +378,11 @@ class MessageWindowStore( addedRenderableCount = addedRenderableCount, ) } catch (cancellation: CancellationException) { + withContext(NonCancellable) { + update { + if (it.olderGeneration == generation) MessageWindowLogic.cancelOlderLoad(it) else it + } + } throw cancellation } catch (error: Throwable) { if (_state.value.olderGeneration != generation) { @@ -569,7 +616,7 @@ class MessageWindowStore( private suspend fun update(transform: (MessageWindowState) -> MessageWindowState): MessageWindowState = stateMutex.withLock { - val next = transform(_state.value) + val next = transform(_state.value).copy(historyRetentionLimit = historyRetentionLimit) _state.value = next next } diff --git a/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStores.kt b/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStores.kt index 250138f6..05e15be6 100644 --- a/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStores.kt +++ b/android/core/data/src/main/kotlin/app/hapi/data/store/MessageWindowStores.kt @@ -2,6 +2,7 @@ package app.hapi.data.store import app.hapi.data.api.MessagesApi import app.hapi.protocol.window.MessageWindowLogic +import app.hapi.protocol.window.HISTORY_WINDOW_SIZE import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -18,6 +19,7 @@ class MessageWindowStores( private val api: MessagesApi, private val scope: CoroutineScope, private val snapshots: WindowSnapshots? = null, + private val historyRetentionLimit: Int = HISTORY_WINDOW_SIZE, ) { private val mutex = Mutex() private val stores = HashMap() @@ -35,6 +37,7 @@ class MessageWindowStores( scope = scope, snapshots = snapshots, initialState = hydrated, + historyRetentionLimit = historyRetentionLimit, ) } } diff --git a/android/core/data/src/test/kotlin/app/hapi/data/store/ChatHistoryPagingStateTest.kt b/android/core/data/src/test/kotlin/app/hapi/data/store/ChatHistoryPagingStateTest.kt new file mode 100644 index 00000000..97043ab9 --- /dev/null +++ b/android/core/data/src/test/kotlin/app/hapi/data/store/ChatHistoryPagingStateTest.kt @@ -0,0 +1,53 @@ +package app.hapi.data.store + +import app.hapi.protocol.window.OlderLoadOutcome +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import org.junit.Test + +class ChatHistoryPagingStateTest { + @Test fun `trimming revives exhaustion but never bypasses an explicit retry`() { + val exhausted = ChatHistoryPagingState(phase = ChatHistoryPagingState.Phase.Exhausted) + assertEquals(ChatHistoryPagingState.Phase.Idle, exhausted.refreshAvailability(true).phase) + val paused = ChatHistoryPagingState(phase = ChatHistoryPagingState.Phase.Paused) + assertEquals(paused, paused.refreshAvailability(true)) + } + @Test fun `a page waits for layout before continuous demand can load again`() { + var state = ChatHistoryPagingState().begin() + assertEquals(state, state.begin()) + state = state.received(OlderLoadOutcome.Applied(3, true, 2), state.generation) + assertEquals(state, state.begin()) + assertEquals(state, state.laidOut(2, true)) + state = state.laidOut(3, true) + assertEquals(ChatHistoryPagingState.Phase.Loading, state.begin().phase) + } + + @Test fun `two timed retries then manual retry`() { + var state = ChatHistoryPagingState() + for (delay in listOf(500L, 1500L)) { + state = state.begin() + state = state.received(OlderLoadOutcome.Failed(Exception()), state.generation) + assertEquals(delay, state.retryDelayMillis) + state = state.retryElapsed(state.generation) + } + state = state.begin() + state = state.received(OlderLoadOutcome.Failed(Exception()), state.generation) + assertEquals(ChatHistoryPagingState.Phase.Failed, state.phase) + assertEquals(ChatHistoryPagingState.Phase.Idle, state.resume().phase) + } + + @Test fun `hidden pages pause and cancellation poisons stale results`() { + var state = ChatHistoryPagingState() + for (version in 1L..3L) { + state = state.begin() + state = state.received(OlderLoadOutcome.Applied(version, true, 0), state.generation) + state = state.laidOut(version, false) + } + assertEquals(ChatHistoryPagingState.Phase.Paused, state.phase) + val old = state.generation + state = state.cancel().begin() + assertNotEquals(old, state.generation) + assertEquals(state, state.received(OlderLoadOutcome.Applied(100, true, 1), old)) + assertEquals(state, state.retryElapsed(old)) + } +} diff --git a/android/core/data/src/test/kotlin/app/hapi/data/store/MessageWindowStoreTest.kt b/android/core/data/src/test/kotlin/app/hapi/data/store/MessageWindowStoreTest.kt index 9818d26c..acb73144 100644 --- a/android/core/data/src/test/kotlin/app/hapi/data/store/MessageWindowStoreTest.kt +++ b/android/core/data/src/test/kotlin/app/hapi/data/store/MessageWindowStoreTest.kt @@ -4,6 +4,10 @@ import app.hapi.data.api.MessagesApi import app.hapi.data.api.MessagesQuery import app.hapi.protocol.window.MessageStatus import app.hapi.protocol.window.MessageWindowLogic +import app.hapi.protocol.window.MessageWindowState +import app.hapi.protocol.window.MessageViewMode +import app.hapi.protocol.window.MessagePosition +import app.hapi.protocol.window.OlderLoadOutcome import app.hapi.protocol.window.PersistedMessageWindow import app.hapi.protocol.window.asWindowMessage import app.hapi.protocol.window.buildOptimisticMessage @@ -127,6 +131,111 @@ class MessageWindowStoreTest { // ------------------------------------------------------------ tail sync -- + @Test fun `native history retains 800 rows across SSE and preserves policy on clear`() = runTest { + val initial = MessageWindowState( + sessionId = "history", viewMode = MessageViewMode.History, epoch = 1, + messages = (1L..800L).map { agentRow("a-$it", it, it * 1000).asWindowMessage() }, + ) + val api = GatedMessagesApi() + val store = MessageWindowStore("history", api, backgroundScope, + initialState = initial, historyRetentionLimit = 800) + store.ingestSseMessages(listOf(agentRow("new", 801, 801000).asWindowMessage())) + assertEquals(800, store.state.value.messages.size) + assertEquals("a-1", store.state.value.messages.first().id) + assertTrue(store.state.value.requiresLatestReset) + val history = store.state.value + api.release(latestPage(listOf(agentRow("new", 801, 801000)), epoch = 1)) + store.reconcileQueuedState() + assertEquals(listOf(MessagesQuery.Latest(limit = 200)), api.requests) + val validated = store.state.value + assertEquals(history.syncGeneration + 1, validated.syncGeneration) + assertEquals(history.messages, validated.messages) + assertEquals(history.oldestPosition, validated.oldestPosition) + assertEquals(history.newestPosition, validated.newestPosition) + assertEquals(history.hasMore, validated.hasMore) + assertEquals(history.historyVersion, validated.historyVersion) + assertTrue(validated.requiresLatestReset) + assertEquals(false, validated.isSyncingTail) + store.setViewMode(MessageViewMode.Tail) + assertEquals(400, store.state.value.messages.size) + assertNull(store.state.value.epoch) + store.clear() + assertEquals(800, store.state.value.historyRetentionLimit) + } + + @Test fun `gap recovery replaces truncated history on epoch change or explicit reset`() = runTest { + for ((epoch, reset) in listOf(2L to false, 1L to true)) { + val api = GatedMessagesApi() + val store = MessageWindowStore("history", api, backgroundScope, initialState = MessageWindowState( + sessionId = "history", viewMode = MessageViewMode.History, epoch = 1, + requiresLatestReset = true, hasMore = true, + oldestPosition = MessagePosition(1000, 1), newestPosition = MessagePosition(1000, 1), + messages = listOf(agentRow("obsolete", 1, 1000).asWindowMessage()), + )) + // Empty authoritative latest also covers deleted history. + api.release(latestPage(emptyList(), epoch = epoch, reset = reset)) + store.reconcileQueuedState() + assertEquals(1, api.requests.size) + assertEquals(epoch, store.state.value.epoch) + assertTrue(store.state.value.messages.isEmpty()) + assertEquals(false, store.state.value.requiresLatestReset) + assertEquals(false, store.state.value.hasMore) + } + } + + @Test fun `gap recovery drains a fresh epoch check while truncated history sync is in flight`() = runTest { + val api = GatedMessagesApi() + val store = MessageWindowStore("history", api, backgroundScope, initialState = MessageWindowState( + sessionId = "history", viewMode = MessageViewMode.History, epoch = 1, + requiresLatestReset = true, messages = listOf(agentRow("obsolete", 1, 1000).asWindowMessage()), + )) + val first = launch { store.syncTail() } + runCurrent() + val recovery = launch { store.reconcileQueuedState() } + runCurrent() + assertEquals(1, api.requests.size) + api.release(latestPage(emptyList(), epoch = 1)) + first.join() + runCurrent() + assertEquals(listOf(MessagesQuery.Latest(200), MessagesQuery.Latest(200)), api.requests) + assertEquals(listOf("obsolete"), store.state.value.messages.map { it.id }) + assertEquals(false, recovery.isCompleted) + api.release(latestPage(listOf(agentRow("replacement", 2, 2000)), epoch = 2)) + recovery.join() + assertEquals(2L, store.state.value.epoch) + assertEquals(listOf("replacement"), store.state.value.messages.map { it.id }) + } + + @Test fun `older cursor stall is paused without applying an empty page`() = runTest { + val api = GatedMessagesApi() + val initial = MessageWindowState("history", hasMore = true, epoch = 1, + oldestPosition = MessagePosition(1000, 1)) + api.release(MessagesResponse(emptyList(), MessagesPage( + direction = "before", limit = 200, epoch = 1, reset = false, + nextBeforeAt = 1000, nextBeforeSeq = 1, nextAfterAt = null, nextAfterSeq = null, + snapshotHeadAt = null, snapshotHeadSeq = null, hasMore = true, + ))) + val store = MessageWindowStore("history", api, backgroundScope, initialState = initial) + assertEquals(OlderLoadOutcome.Stopped(OlderLoadOutcome.StopReason.CursorDidNotAdvance), store.fetchOlder()) + assertEquals(0, store.state.value.messages.size) + assertTrue(store.state.value.hasMore) + assertEquals(false, store.state.value.isLoadingMore) + } + + @Test fun `cancelled fetch releases reservation and cannot overwrite a new request`() = runTest { + val api = GatedMessagesApi() + val store = MessageWindowStore("history", api, backgroundScope, + initialState = MessageWindowState("history", hasMore = true, epoch = 1, + oldestPosition = MessagePosition(1000, 1))) + val request = backgroundScope.launch { store.fetchOlder() } + runCurrent() + assertTrue(store.state.value.isLoadingMore) + assertEquals(OlderLoadOutcome.Stopped(OlderLoadOutcome.StopReason.Busy), store.fetchOlder()) + request.cancel() + request.join() + assertEquals(false, store.state.value.isLoadingMore) + } + @Test fun `concurrent syncTail calls coalesce into a single run`() = runTest { val api = GatedMessagesApi() diff --git a/android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/ToolGroups.kt b/android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/ToolGroups.kt index 53fec252..dab0624e 100644 --- a/android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/ToolGroups.kt +++ b/android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/ToolGroups.kt @@ -216,16 +216,36 @@ private fun createToolGroupId( tools: List, needsOlderHistory: Boolean, previousGroups: List, + reservedGroupIds: Set, + usedIds: MutableSet, ): String { val firstToolId = tools.firstOrNull()?.id ?: "unknown" val lastToolId = tools.lastOrNull()?.id ?: firstToolId - val previous = previousGroups.firstOrNull { it.firstToolId == firstToolId || it.lastToolId == lastToolId } + // A split may share both old boundaries, but only one resulting group + // can own the old identity. Reserve other old IDs against fresh runs. + val previous = previousGroups.firstOrNull { + (it.firstToolId == firstToolId || it.lastToolId == lastToolId) && it.id !in usedIds + } if (previous != null) { + usedIds.add(previous.id) return previous.id } - return if (needsOlderHistory) "tool-group:$lastToolId" else "tool-group:$firstToolId" + val boundaries = if (needsOlderHistory) listOf(lastToolId, firstToolId) else listOf(firstToolId, lastToolId) + for (boundary in boundaries) { + val candidate = "tool-group:$boundary" + if (candidate !in reservedGroupIds && usedIds.add(candidate)) return candidate + } + val base = "tool-group:${boundaries.first()}" + var suffix = 2 + var candidate = "$base#$suffix" + while (candidate in reservedGroupIds || candidate in usedIds) { + suffix += 1 + candidate = "$base#$suffix" + } + usedIds.add(candidate) + return candidate } fun isToolGroupBlock(block: VisibleChatBlock): Boolean = block is ToolGroupBlock @@ -236,6 +256,8 @@ fun buildVisibleChatBlocks( ): List { val visibleBlocks = mutableListOf() val previousGroups = options.previousGroups + val reservedGroupIds = previousGroups.mapTo(mutableSetOf()) { it.id } + val usedIds = blocks.mapTo(mutableSetOf()) { it.id } var index = 0 while (index < blocks.size) { @@ -279,7 +301,7 @@ fun buildVisibleChatBlocks( } visibleBlocks.add( ToolGroupBlock( - id = createToolGroupId(tools, needsOlderHistory, previousGroups), + id = createToolGroupId(tools, needsOlderHistory, previousGroups, reservedGroupIds, usedIds), createdAt = tools.first().createdAt, invokedAt = tools.first().invokedAt, firstToolId = tools.first().id, diff --git a/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowLogic.kt b/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowLogic.kt index ce048801..447545ac 100644 --- a/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowLogic.kt +++ b/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowLogic.kt @@ -227,7 +227,7 @@ object MessageWindowLogic { val effectiveMode = mode ?: if (previous.viewMode == MessageViewMode.History) TrimMode.Prepend else TrimMode.Append val effectiveLimit = regularLimit - ?: if (previous.viewMode == MessageViewMode.History) HISTORY_WINDOW_SIZE else VISIBLE_WINDOW_SIZE + ?: if (previous.viewMode == MessageViewMode.History) previous.historyRetentionLimit else VISIBLE_WINDOW_SIZE val merged = MessageMerge.mergeMessages(previous.messages, retainedIncoming) val (kept, dropped) = trimPreservingQueued(merged, effectiveLimit, effectiveMode).let { it.kept to it.dropped } var next = previous.withMessages(kept) diff --git a/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowState.kt b/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowState.kt index 8ba31330..06df5ded 100644 --- a/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowState.kt +++ b/android/core/protocol/src/main/kotlin/app/hapi/protocol/window/MessageWindowState.kt @@ -52,6 +52,7 @@ sealed interface OlderLoadOutcome { Invalidated("invalidated"), EpochReset("epoch-reset"), Exhausted("exhausted"), + CursorDidNotAdvance("cursor-did-not-advance"), } } @@ -76,6 +77,8 @@ data class MessageWindowState( val isLoadingMore: Boolean = false, val warning: String? = null, val viewMode: MessageViewMode = MessageViewMode.Tail, + /** Local presentation policy, never persisted or sent to the hub. */ + val historyRetentionLimit: Int = HISTORY_WINDOW_SIZE, /** Bumped whenever the [messages] list instance changes. */ val messagesVersion: Long = 0, /** Bumped per applied older page (scroll-anchoring handle). */ diff --git a/android/core/protocol/src/test/kotlin/app/hapi/protocol/chat/ToolGroupIdentityTest.kt b/android/core/protocol/src/test/kotlin/app/hapi/protocol/chat/ToolGroupIdentityTest.kt new file mode 100644 index 00000000..d8f22d5c --- /dev/null +++ b/android/core/protocol/src/test/kotlin/app/hapi/protocol/chat/ToolGroupIdentityTest.kt @@ -0,0 +1,65 @@ +package app.hapi.protocol.chat + +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals +import org.junit.Test + +class ToolGroupIdentityTest { + private fun tool(id: String) = ToolCallBlock( + id = id, localId = null, createdAt = 0, invokedAt = null, + tool = ChatToolCall(id, "Read", "completed", input = null, createdAt = 0, description = null), + children = emptyList(), meta = null, + ) + + private fun text(id: String = "text") = AgentTextBlock( + id = id, localId = null, createdAt = 0, invokedAt = null, text = "Between tool runs", meta = null, + ) + + private fun groups(blocks: List, more: Boolean = false, previous: List = emptyList()) = + buildVisibleChatBlocks(blocks, ToolGroupingOptions(hasMoreMessages = more, previousGroups = previous)) + .filterIsInstance() + + @Test fun `a split claims its old identity only once and retains all tools`() { + for (more in listOf(false, true)) { + val tools = listOf("a", "b", "c", "d").map(::tool) + val previous = groups(tools, more) + val split = listOf(tools[0], tools[1], text(), tools[2], tools[3]) + val next = groups(split, more, previous) + assertEquals(2, next.size) + assertEquals(2, next.map { it.id }.toSet().size) + assertEquals(1, next.count { it.id == previous.single().id }) + assertEquals(listOf("a", "b", "c", "d"), next.flatMap { it.tools }.map { it.id }) + assertEquals(next.map { it.id }, groups(split, more, next).map { it.id }) + } + } + + @Test fun `fallback cannot collide with an already claimed historical id`() { + val original = groups(listOf(tool("c"), tool("d"))) + val grown = groups(listOf("a", "b", "c", "d").map(::tool), previous = original) + val split = listOf(tool("a"), tool("b"), text(), tool("c"), tool("d")) + val next = groups(split, previous = grown) + assertEquals(original.single().id, next[0].id) + assertNotEquals(next[0].id, next[1].id) + assertEquals(next.map { it.id }, groups(split, previous = next).map { it.id }) + } + + @Test fun `a prepended group cannot steal the surviving groups id`() { + val original = groups(listOf("c", "d", "e").map(::tool)) + val trimmed = groups(listOf(tool("d"), tool("e")), previous = original) + val prepended = listOf(tool("a"), tool("b"), tool("c"), text(), tool("d"), tool("e")) + val next = groups(prepended, more = true, previous = trimmed) + assertEquals(2, next.size) + assertNotEquals(next[0].id, next[1].id) + assertEquals(original.single().id, next[1].id) + assertEquals(next.map { it.id }, groups(prepended, more = true, previous = next).map { it.id }) + } + + @Test fun `both boundary collisions use a stable suffix without dropping any block`() { + val blocks = listOf(text("tool-group:a"), tool("a"), tool("b"), text("tool-group:b")) + val visible = buildVisibleChatBlocks(blocks, ToolGroupingOptions(hasMoreMessages = false)) + val group = visible.filterIsInstance().single() + assertEquals("tool-group:a#2", group.id) + assertEquals(3, visible.size) + assertEquals(group.id, groups(blocks, previous = listOf(group)).single().id) + } +} diff --git a/android/gradle/libs.versions.toml b/android/gradle/libs.versions.toml index 51396699..2f86a88e 100644 --- a/android/gradle/libs.versions.toml +++ b/android/gradle/libs.versions.toml @@ -13,6 +13,7 @@ kotlinxCoroutines = "1.10.2" okhttp = "4.12.0" securityCrypto = "1.1.0" junit = "4.13.2" +androidxTestRunner = "1.6.2" turbine = "1.2.0" # Pairing flow (B-M1d). # datastore 1.1.7 (2025-06) + navigation-compose 2.9.0 (2025-05) + @@ -57,6 +58,9 @@ androidx-lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-p # Compose (versions come from the BOM) androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" } +androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" } +androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" } +androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTestRunner" } androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" } androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" } diff --git a/docs/native-chat-scrolling.md b/docs/native-chat-scrolling.md new file mode 100644 index 00000000..d6dde86f --- /dev/null +++ b/docs/native-chat-scrolling.md @@ -0,0 +1,187 @@ +# Native transcript scrolling + +Native-only presentation policy. No Web, Hub, REST/SSE format, or generated +fixture changes. iOS and Android still reduce the same protocol messages. + +## Behavior + +- Preload when approximately one viewport from the oldest retained row. + Short/hidden-only transcripts also request history, without another gesture. +- One older request at a time. Its `historyVersion` must reach a rendered + layout before another page starts. Layout reports viewport demand first. + If a tail sync invalidates a pending page, its completion rechecks demand + without waiting for a layout acknowledgement that will never arrive. +- Preserve the current visible message ID and its offset, including movement + while HTTP is in flight. Prepending never calls `scrollToItem`. +- Browsing history disables tail following. A fixed-height history control + avoids loading-spinner insertion/removal shifts. “Back to latest” remains + available even when the live tail has been trimmed out of memory. +- Retry failures after 500 ms and 1,500 ms; then require a tap. Three pages + without display progress, or a non-advancing cursor, pause automatic loading + without falsely announcing the beginning of the conversation. +- Leaving the preload band, leaving the screen, or returning to latest cancels + older loading. Generation checks and a synchronous before-apply gate reject + late responses. Epoch invalidation still forces authoritative recovery. +- A same-model return from an overlay preserves history mode. Background + tail refreshes (including replay-gap recovery and trailing sync runs) still + validate the server epoch. Same-epoch responses preserve a truncated history + window; an epoch change or explicit reset replaces obsolete history. + Explicit latest navigation can also replace it. Failed latest refreshes + keep the retry affordance. + +## Implementation + +### Identity across snapshots + +Native tool-group IDs are allocated once per snapshot. When a run splits, +only one resulting group inherits its old ID; new groups reserve existing +groups' historical IDs so a prepend cannot steal a later reading anchor. +Collision fallbacks are deterministic and reused on subsequent renders. +All distinct groups/tools remain present; no random or row-index identities. + +The iOS collection additionally coalesces repeated deliveries of a logical +row (same ID): latest value, first position. The lookup, layout and diffable +snapshot all consume that normalized sequence; duplicate counts are logged. +This is a last-resort guard, not a substitute for distinct tool-group IDs. + +### iOS + +`AnchoredTranscriptList` owns a `UICollectionView` with SwiftUI hosting cells. +A custom layout retains stable-ID height estimates across snapshots, applies +offset adjustments within UIKit layout, and compensates self-sizing changes +above the viewport. Snapshot commits are serialized/coalesced. Viewport resize +keeps the live bottom pinned only while following the tail. +Visible-rectangle queries binary-search the ordered frame array, then visit +only intersecting rows (`O(log n + visible rows)`). Height changes update the +affected suffix's geometry, without allocating attributes for offscreen rows. +Attributes are cached lazily and never mutated after being handed to UIKit; +unchanged IDs/width do not rebuild geometry on a content-only refresh. + +`ChatPresentationState` keeps expansion/form state outside recycled cells, +pruned to retained message/request IDs. Expanded tool groups become individual +display rows, without changing protocol groups. The per-chat Markdown cache +prepares new sources off-main before publication; image decoding/display +preparation also runs off-main. + +### Android + +`ChatTranscript` uses a chronological `LazyColumn`, not `reverseLayout`. +Stable keys preserve its first visible item and partial-row offset. A measured +height cache estimates the top preload distance; unmeasured rows use an +estimate. Layout acknowledgement checks visible keys against their new +indices, including equal-size bounded-window replacements. +Tail-follow intent and the consumed jump token use saved state alongside the +list position, so navigation return/activity recreation does not replay an +old jump-to-latest command or report a false tail viewport to the ViewModel. +No `layoutInfo` reads occur in transcript composition. Tail geometry is +observed through `snapshotFlow` only while following; corrections are queued +outside measurement/placement to avoid reentrant layout. Row heights use +size-change callbacks, not callbacks on every global-position change, and +unchanged viewport demand is not repeatedly sent to the coordinator. +Group expansion flags are saved with them: the flattened children must exist +before restoring an index/offset within a group. Removed groups' flags are +pruned rather than retained indefinitely. + +The history coordinator uses a dedicated queued Main scope sharing the +holder's lifetime, independent of its Default worker scope. Lifecycle, +viewport/layout callbacks, store observation, request completion and retry +transitions are main-thread serialized. Network/store work and the chat/ +Markdown pipeline stay on workers; only the before-apply veto is atomic. + +An explicitly pruned `SaveableStateHolder` retains recycled row interactions, +including collapsed group children. Markdown ASTs are prepared on the pipeline +dispatcher and cached per chat; misses parse off-main. Images retain the +existing asynchronous, cached Coil loader. + +### Bounds + +- Page size: **200** regular messages. +- Tail window: **400**; native history retention: **800**. This matches the + prepend budget, preventing the next SSE event from shrinking 800 rows to + the reference/default history limit of 600. +- Reference/default window policy remains 600 for conformance tests. The + native override is local state, neither persisted nor transmitted. +- Existing queued-message and background-agent buckets retain their separate + rules; 800 is not a cap on every possible display row or on heap bytes. +- Markdown caches: 800 entries and approximately 8 MiB source/AST cost budget + each. Oversized documents render on demand but are not retained in cache. +- Not an offline archive; no cross-launch reading-position persistence. + +## Automated checks + +From the repository root: + +```sh +swift test --package-path ios/Packages/HapiKit +ios/scripts/test-transcript.sh + +cd android +./gradlew :core:protocol:test :core:data:testDebugUnitTest :app:testDebugUnitTest +./gradlew :app:assembleDebug :app:assembleDebugAndroidTest +# Run on a dedicated test emulator; avoid installing into a personal device. +./gradlew :app:connectedDebugAndroidTest +``` + +The iOS script creates/deletes its own simulator. Both native workflows run +layout tests in CI. Coverage includes real UIKit/Compose variable-height rows, +bounded prepends after reader movement, tail growth while reading history, +current-row growth, recycled expansion, short/hidden-only pages, and paging +coordinator/store cancellation, retry, cursor, and retention behavior. +Stateful group-ID tests cover splits, inherited-ID collisions, prepends, and +stable recomputes. UIKit tests also verify that regrouping preserves both +groups and the existing reading anchor, plus duplicate-delivery handling. +Replay-gap tests cover same-epoch retention, epoch/reset replacement, and +trailing validation. App coordinators cover a slow invalidated page returning +after tail sync; Compose tests cover saved-screen and saved-instance-state +restoration with consumed and new jump tokens. +Android also tests Default workers against a separate UI executor across 20 +acknowledged pages (including acknowledgement before request completion), +and restores expanded child-row anchors across navigation and saved state. + +These are deterministic layout regressions, not proof of release-device FPS. + +### Scroll work budgets + +Measured before/after this optimization with dedicated simulators/emulators +and the same 800-row debug regression probes: + +| Probe | Before | After | +| --- | ---: | ---: | +| Android: whole-transcript compositions during a 1,200 px / 600 ms history scroll | 37 | 0 | +| iOS: geometry probes for 200 visible-rectangle queries | 160,000 | 3,864 | +| iOS: attributes allocated during 20 self-sizing updates | 16,000 | 21 | + +`ChatTranscriptTest` uses Compose's tracer (test-only) to ensure the whole +transcript does not recompose every frame; legitimate visible-cell composition +is not suppressed. `TranscriptGeometryTests` compares indexed queries with a +linear reference across variable heights, prepends/trims, width changes and +empty windows. Debug-only counters enforce query/allocation budgets. These +counts are not elapsed-time, total-allocation, or FPS benchmarks. Release-device +frame deadlines and memory still require the profiling checklist below. + +## Release-device acceptance + +Use a release/profile build on iOS 17 and a recent iPhone (60/120 Hz), plus +Android API 26 and a recent Android device, including a lower-end device. + +1. Replay a 10,000+ message session with mixed Markdown, long code/diffs, + images, permissions, and expanded tool groups. Traverse well beyond the + 800-message window repeatedly. +2. Test slow dragging, fast upward flings, reversing direction during a + request, and a response arriving during deceleration. No forced animation, + cancelled momentum, or unexplained reading-position jump. +3. Under 200 ms / 1 s / 3 s latency, packet loss, and offline/reconnect, verify + serial paging, bounded retries, manual recovery, and no “beginning” lie. +4. Stream into the tail while reading a partial tall row. Expand/collapse + tools, load images, rotate, change text size, show/hide the keyboard, and + open/close a media overlay. Text/forms/expansion must survive recycling. +5. Return to latest after history trimming, both online and offline. A failed + refresh must not silently claim that an old retained row is the live tail. +6. Inspect Instruments (Time Profiler / Animation Hitches / Allocations) and + Android Perfetto FrameTimeline / memory profiler. Record frame deadlines, + long main-thread work, and steady-state memory. Compare against baseline; + do not infer smoothness from simulator timings or unit-test duration. + +Static prepend anchor regression tolerance: 1 point on iOS / 1 pixel in the +Compose test. Fling continuity, real keyboard/accessibility interactions, and +release-device frame/memory measurements remain manual acceptance checks. diff --git a/ios/Hapi.xcodeproj/project.pbxproj b/ios/Hapi.xcodeproj/project.pbxproj index 9d0e4ef0..04cd1b68 100644 --- a/ios/Hapi.xcodeproj/project.pbxproj +++ b/ios/Hapi.xcodeproj/project.pbxproj @@ -11,6 +11,9 @@ A100000000000000000000C5 /* HapiClient in Frameworks */ = {isa = PBXBuildFile; productRef = A100000000000000000000C3 /* HapiClient */; }; A100000000000000000000C8 /* HapiUI in Frameworks */ = {isa = PBXBuildFile; productRef = A100000000000000000000C7 /* HapiUI */; }; A200000000000000000000D4 /* HapiNotificationService.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = A200000000000000000000A4 /* HapiNotificationService.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + A300000000000000000000C4 /* HapiProtocol in Frameworks */ = {isa = PBXBuildFile; productRef = A100000000000000000000C2 /* HapiProtocol */; }; + A300000000000000000000C5 /* HapiClient in Frameworks */ = {isa = PBXBuildFile; productRef = A100000000000000000000C3 /* HapiClient */; }; + A300000000000000000000C8 /* HapiUI in Frameworks */ = {isa = PBXBuildFile; productRef = A100000000000000000000C7 /* HapiUI */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -21,6 +24,13 @@ remoteGlobalIDString = A200000000000000000000A5; remoteInfo = HapiNotificationService; }; + A300000000000000000000D2 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = A100000000000000000000A6 /* Project object */; + proxyType = 1; + remoteGlobalIDString = A100000000000000000000A5; + remoteInfo = Hapi; + }; /* End PBXContainerItemProxy section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -40,6 +50,7 @@ /* Begin PBXFileReference section */ A100000000000000000000A4 /* Hapi.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Hapi.app; sourceTree = BUILT_PRODUCTS_DIR; }; A200000000000000000000A4 /* HapiNotificationService.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = HapiNotificationService.appex; sourceTree = BUILT_PRODUCTS_DIR; }; + A300000000000000000000A4 /* HapiTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = HapiTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ /* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */ @@ -76,6 +87,11 @@ path = HapiNotificationService; sourceTree = ""; }; + A300000000000000000000A2 /* HapiTests */ = { + isa = PBXFileSystemSynchronizedRootGroup; + path = HapiTests; + sourceTree = ""; + }; /* End PBXFileSystemSynchronizedRootGroup section */ /* Begin PBXFrameworksBuildPhase section */ @@ -96,6 +112,16 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + A300000000000000000000A8 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + A300000000000000000000C4 /* HapiProtocol in Frameworks */, + A300000000000000000000C5 /* HapiClient in Frameworks */, + A300000000000000000000C8 /* HapiUI in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -104,6 +130,7 @@ children = ( A100000000000000000000A2 /* Hapi */, A200000000000000000000A2 /* HapiNotificationService */, + A300000000000000000000A2 /* HapiTests */, A100000000000000000000A3 /* Products */, ); sourceTree = ""; @@ -113,6 +140,7 @@ children = ( A100000000000000000000A4 /* Hapi.app */, A200000000000000000000A4 /* HapiNotificationService.appex */, + A300000000000000000000A4 /* HapiTests.xctest */, ); name = Products; sourceTree = ""; @@ -169,6 +197,32 @@ productReference = A200000000000000000000A4 /* HapiNotificationService.appex */; productType = "com.apple.product-type.app-extension"; }; + A300000000000000000000A5 /* HapiTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = A300000000000000000000B4 /* Build configuration list for PBXNativeTarget "HapiTests" */; + buildPhases = ( + A300000000000000000000A7 /* Sources */, + A300000000000000000000A8 /* Frameworks */, + A300000000000000000000A9 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + A300000000000000000000D1 /* PBXTargetDependency */, + ); + fileSystemSynchronizedGroups = ( + A300000000000000000000A2 /* HapiTests */, + ); + name = HapiTests; + packageProductDependencies = ( + A100000000000000000000C2 /* HapiProtocol */, + A100000000000000000000C3 /* HapiClient */, + A100000000000000000000C7 /* HapiUI */, + ); + productName = HapiTests; + productReference = A300000000000000000000A4 /* HapiTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -207,6 +261,7 @@ targets = ( A100000000000000000000A5 /* Hapi */, A200000000000000000000A5 /* HapiNotificationService */, + A300000000000000000000A5 /* HapiTests */, ); }; /* End PBXProject section */ @@ -226,6 +281,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + A300000000000000000000A9 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ @@ -243,6 +305,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + A300000000000000000000A7 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ @@ -251,6 +320,11 @@ target = A200000000000000000000A5 /* HapiNotificationService */; targetProxy = A200000000000000000000D2 /* PBXContainerItemProxy */; }; + A300000000000000000000D1 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = A100000000000000000000A5 /* Hapi */; + targetProxy = A300000000000000000000D2 /* PBXContainerItemProxy */; + }; /* End PBXTargetDependency section */ /* Begin XCBuildConfiguration section */ @@ -423,6 +497,38 @@ }; name = Release; }; + A300000000000000000000B5 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + GENERATE_INFOPLIST_FILE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + PRODUCT_BUNDLE_IDENTIFIER = run.hapi.companion.tests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; + SWIFT_VERSION = 6.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Hapi.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Hapi"; + }; + name = Debug; + }; + A300000000000000000000B6 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + GENERATE_INFOPLIST_FILE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + PRODUCT_BUNDLE_IDENTIFIER = run.hapi.companion.tests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; + SWIFT_VERSION = 6.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Hapi.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Hapi"; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -453,6 +559,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + A300000000000000000000B4 /* Build configuration list for PBXNativeTarget "HapiTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A300000000000000000000B5 /* Debug */, + A300000000000000000000B6 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ /* Begin XCLocalSwiftPackageReference section */ diff --git a/ios/Hapi.xcodeproj/xcshareddata/xcschemes/Hapi.xcscheme b/ios/Hapi.xcodeproj/xcshareddata/xcschemes/Hapi.xcscheme index 265f76fb..5bd8bec5 100644 --- a/ios/Hapi.xcodeproj/xcshareddata/xcschemes/Hapi.xcscheme +++ b/ios/Hapi.xcodeproj/xcshareddata/xcschemes/Hapi.xcscheme @@ -29,6 +29,19 @@ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES" shouldAutocreateTestPlan = "YES"> + + + + + + UIImage? { + cache.object(forKey: imageId as NSString) + } } private struct ChatMediaKey: EnvironmentKey { diff --git a/ios/Hapi/Features/Chat/Blocks/CodexReviewBlockView.swift b/ios/Hapi/Features/Chat/Blocks/CodexReviewBlockView.swift index 94d67713..d57ca6bd 100644 --- a/ios/Hapi/Features/Chat/Blocks/CodexReviewBlockView.swift +++ b/ios/Hapi/Features/Chat/Blocks/CodexReviewBlockView.swift @@ -8,9 +8,14 @@ import SwiftUI struct CodexReviewBlockView: View { let block: CodexReviewBlock - @State private var findingsOpen = false + @ChatStoredState private var findingsOpen: Bool @Environment(\.hapiTheme) private var theme + init(block: CodexReviewBlock) { + self.block = block + _findingsOpen = ChatStoredState(wrappedValue: false, id: block.id, field: "findings") + } + var body: some View { let review = block.review VStack(alignment: .leading, spacing: 0) { @@ -33,7 +38,7 @@ struct CodexReviewBlockView: View { if let explanation = review.overallExplanation, !explanation.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { Divider() - MarkdownView(markdown: explanation) + CachedMarkdownView(markdown: explanation) .padding(.horizontal, 12) .padding(.vertical, 8) } diff --git a/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift b/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift index 14927787..11cc776a 100644 --- a/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift +++ b/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift @@ -19,7 +19,7 @@ struct GeneratedImageBlockView: View { Group { if media == nil || failed { placeholder - } else if let image { + } else if let image = image ?? media?.cachedImage(for: block.imageId) { Button { viewerOpen = true } label: { diff --git a/ios/Hapi/Features/Chat/Blocks/PermissionActionViews.swift b/ios/Hapi/Features/Chat/Blocks/PermissionActionViews.swift index af179aef..65a26685 100644 --- a/ios/Hapi/Features/Chat/Blocks/PermissionActionViews.swift +++ b/ios/Hapi/Features/Chat/Blocks/PermissionActionViews.swift @@ -120,8 +120,8 @@ private struct AskUserQuestionFooter: View { private let questions: [AskQuestion] /// Selection state per question index. - @State private var selected: [Int: Set] = [:] - @State private var otherText: [Int: String] = [:] + @ChatStoredState private var selected: [Int: Set] + @ChatStoredState private var otherText: [Int: String] @State private var validationError: String? init(tool: ChatToolCall, requestId: String, interactions: ChatInteractor) { @@ -130,6 +130,8 @@ private struct AskUserQuestionFooter: View { self.interactions = interactions self.cursorDialect = isCursorAskQuestionToolName(tool.name) self.questions = parseAskUserQuestions(tool.input, cursorDialect: cursorDialect) + _selected = ChatStoredState(wrappedValue: [:], id: requestId, field: "ask.selected") + _otherText = ChatStoredState(wrappedValue: [:], id: requestId, field: "ask.other") } var body: some View { @@ -289,8 +291,8 @@ private struct RequestUserInputFooter: View { private let questions: [RequestUserInputQuestion] - @State private var selected: [String: Set] = [:] - @State private var notes: [String: String] + @ChatStoredState private var selected: [String: Set] + @ChatStoredState private var notes: [String: String] @State private var validationError: String? init(tool: ChatToolCall, requestId: String, interactions: ChatInteractor) { @@ -301,10 +303,11 @@ private struct RequestUserInputFooter: View { self.questions = parsed // Duplicate field ids are malformed input; keep the first rather // than trapping. - _notes = State(initialValue: Dictionary( + _selected = ChatStoredState(wrappedValue: [:], id: requestId, field: "input.selected") + _notes = ChatStoredState(wrappedValue: Dictionary( parsed.map { ($0.id, $0.prefill ?? "") }, uniquingKeysWith: { first, _ in first } - )) + ), id: requestId, field: "input.notes") } var body: some View { diff --git a/ios/Hapi/Features/Chat/Blocks/TextBlockViews.swift b/ios/Hapi/Features/Chat/Blocks/TextBlockViews.swift index 80222506..5ac1da2c 100644 --- a/ios/Hapi/Features/Chat/Blocks/TextBlockViews.swift +++ b/ios/Hapi/Features/Chat/Blocks/TextBlockViews.swift @@ -76,7 +76,7 @@ struct AgentTextBlockView: View { let block: AgentTextBlock var body: some View { - MarkdownView(markdown: block.text) + CachedMarkdownView(markdown: block.text) .frame(maxWidth: .infinity, alignment: .leading) } } @@ -85,7 +85,12 @@ struct AgentTextBlockView: View { /// the full reasoning markdown (still subdued — it is meta-content). struct AgentReasoningBlockView: View { let block: AgentReasoningBlock - @State private var expanded = false + @ChatStoredState private var expanded: Bool + + init(block: AgentReasoningBlock) { + self.block = block + _expanded = ChatStoredState(wrappedValue: false, id: block.id, field: "expanded") + } var body: some View { VStack(alignment: .leading, spacing: 4) { @@ -103,7 +108,7 @@ struct AgentReasoningBlockView: View { } .buttonStyle(.plain) if expanded { - MarkdownView(markdown: block.text) + CachedMarkdownView(markdown: block.text) .opacity(0.75) .padding(.leading, 8) } diff --git a/ios/Hapi/Features/Chat/Blocks/ToolCallBlockView.swift b/ios/Hapi/Features/Chat/Blocks/ToolCallBlockView.swift index 2e67789d..a0d0648f 100644 --- a/ios/Hapi/Features/Chat/Blocks/ToolCallBlockView.swift +++ b/ios/Hapi/Features/Chat/Blocks/ToolCallBlockView.swift @@ -13,8 +13,8 @@ struct ToolCallBlockView: View { let block: ToolCallBlock let basePath: String? - @State private var expanded: Bool - @State private var childrenOpen: Bool + @ChatStoredState private var expanded: Bool + @ChatStoredState private var childrenOpen: Bool @Environment(\.hapiTheme) private var theme @Environment(\.chatInteractions) private var interactions @@ -22,8 +22,8 @@ struct ToolCallBlockView: View { self.block = block self.basePath = basePath let pending = block.tool.permission?.status == .pending - _expanded = State(initialValue: pending) - _childrenOpen = State(initialValue: pending) + _expanded = ChatStoredState(wrappedValue: pending, id: block.id, field: "expanded") + _childrenOpen = ChatStoredState(wrappedValue: pending, id: block.id, field: "children") } var body: some View { diff --git a/ios/Hapi/Features/Chat/Blocks/ToolGroupBlockView.swift b/ios/Hapi/Features/Chat/Blocks/ToolGroupBlockView.swift index 994c23d8..66573f6e 100644 --- a/ios/Hapi/Features/Chat/Blocks/ToolGroupBlockView.swift +++ b/ios/Hapi/Features/Chat/Blocks/ToolGroupBlockView.swift @@ -11,18 +11,22 @@ struct ToolGroupBlockView: View { let basePath: String? @State private var expanded: Bool + private let expansion: Binding? + private let showsTools: Bool @Environment(\.hapiTheme) private var theme - init(block: ToolGroupBlock, basePath: String?) { + init(block: ToolGroupBlock, basePath: String?, expansion: Binding? = nil, showsTools: Bool = true) { self.block = block self.basePath = basePath _expanded = State(initialValue: block.defaultOpen) + self.expansion = expansion + self.showsTools = showsTools } var body: some View { VStack(alignment: .leading, spacing: 0) { headerRow - if expanded { + if showsTools && (expansion?.wrappedValue ?? expanded) { VStack(alignment: .leading, spacing: 6) { ForEach(block.tools, id: \.id) { tool in ToolCallBlockView(block: tool, basePath: basePath) @@ -40,7 +44,8 @@ struct ToolGroupBlockView: View { private var headerRow: some View { Button { withAnimation(.easeInOut(duration: 0.15)) { - expanded.toggle() + if let expansion { expansion.wrappedValue.toggle() } + else { expanded.toggle() } } } label: { HStack(spacing: 8) { @@ -106,7 +111,7 @@ struct ToolGroupBlockView: View { .font(.caption2) .foregroundStyle(.red) } else { - Image(systemName: expanded ? "chevron.down" : "chevron.right") + Image(systemName: (expansion?.wrappedValue ?? expanded) ? "chevron.down" : "chevron.right") .font(.caption) .foregroundStyle(.secondary) } diff --git a/ios/Hapi/Features/Chat/ChatModel.swift b/ios/Hapi/Features/Chat/ChatModel.swift index 272ad831..7fae88a8 100644 --- a/ios/Hapi/Features/Chat/ChatModel.swift +++ b/ios/Hapi/Features/Chat/ChatModel.swift @@ -1,6 +1,7 @@ import Foundation import HapiClient import HapiProtocol +import HapiUI import Observation /// Chat top-bar state: title cascade + status dot + meta line. @@ -50,10 +51,16 @@ final class ChatModel { private(set) var loadFailed = false /// Tail-sync warning — the degraded banner. private(set) var warning: String? - /// Bumps on tail-side content changes (drives the new-messages pill). + /// Bumps on tail-side content changes. private(set) var tailRevision = 0 /// Bumps when an older page was prepended (drives scroll re-anchoring). private(set) var historyVersion = 0 + private(set) var historyPaging = ChatHistoryPagingState() + private(set) var followsTail = true + private(set) var jumpToLatestToken = 0 + private(set) var isJumpingToLatest = false + private(set) var hasTrimmedTail = false + var expandedToolGroups: [String: Bool] = [:] /// Transient toast text (interaction failures/notices); auto-dismissed. private(set) var notice: String? @@ -82,6 +89,9 @@ final class ChatModel { let dictation: DictationController @ObservationIgnored private let pipeline = ChatPipeline() @ObservationIgnored let imageLoader: GeneratedImageLoader + @ObservationIgnored let presentationState = ChatPresentationState() + @ObservationIgnored let markdownCache = MarkdownRenderCache() + @ObservationIgnored private var previousMarkdownSources: Set = [] /// Authed thumbnail/viewer loader for scratchlist attachments (A-M4b). @ObservationIgnored let scratchlistAttachments: ScratchlistAttachmentLoader @@ -95,6 +105,15 @@ final class ChatModel { @ObservationIgnored private var pipelineTask: Task? @ObservationIgnored private var statesTask: Task? @ObservationIgnored private var olderTask: Task? + @ObservationIgnored private var jumpTask: Task? + @ObservationIgnored private var historyRetryTask: Task? + @ObservationIgnored private var historyCancellation: Task? + @ObservationIgnored private var windowModeTask: Task? + @ObservationIgnored private var historyGate: ChatHistoryRequestGate? + @ObservationIgnored private var viewportNeedsOlder = false + @ObservationIgnored private var lastHistoryLayout: (version: Int, progress: Bool)? + @ObservationIgnored private var lastEpoch: Int? + @ObservationIgnored private var historyPumpScheduled = false @ObservationIgnored private var isActive = false @ObservationIgnored private var everStarted = false @@ -122,6 +141,7 @@ final class ChatModel { func start() { guard !isActive else { return } + let preservingHistory = everStarted && (!followsTail || hasTrimmedTail) isActive = true if everStarted { // Coming back from a stop (e.g. a full-screen cover fired the @@ -158,7 +178,7 @@ final class ChatModel { self?.scheduleRecompute() } Task { [weak self] in - await chat.start() + await chat.start(preservingHistory: preservingHistory) guard let self, self.isActive, self.chat === chat else { return } guard let controller = chat.windowController else { return } self.statesTask = Task { [weak self] in @@ -180,8 +200,10 @@ final class ChatModel { statesTask = nil pipelineTask?.cancel() pipelineTask = nil - olderTask?.cancel() - olderTask = nil + cancelHistory() + jumpTask?.cancel() + jumpTask = nil + isJumpingToLatest = false noticeTask?.cancel() noticeTask = nil // Discard an in-flight take — the mic must not stay hot off-screen @@ -208,18 +230,139 @@ final class ChatModel { // MARK: - Actions - /// Reader requested older history: one page. Returns whether it started; - /// busy/tail-sync rejections must not change the view's saved anchor. - @discardableResult - func loadOlder() -> Bool { - guard olderTask == nil, let controller = chat.windowController else { return false } - guard let windowState, windowState.hasMore, - !windowState.isLoadingMore, !windowState.isSyncingTail else { return false } + func readingViewportChanged(followsTail: Bool, needsOlder: Bool) { + let changedMode = self.followsTail != followsTail + self.followsTail = followsTail + viewportNeedsOlder = needsOlder + if changedMode, !isJumpingToLatest, let controller = chat.windowController { + if followsTail && hasTrimmedTail { jumpToLatest(); return } + let previous = windowModeTask + windowModeTask = Task { + await previous?.value + await controller.setViewMode(followsTail ? .tail : .history) + } + } + if !needsOlder, olderTask != nil || historyPaging.phase == .retrying { + cancelHistory() + } + scheduleHistoryPump() + } + + func historyLaidOut(version: Int, madeProgress: Bool) { + if lastHistoryLayout?.version == version { + let progress = (lastHistoryLayout?.progress ?? false) || madeProgress + lastHistoryLayout = (version, progress) + } else { + lastHistoryLayout = (version, madeProgress) + } + acknowledgeHistoryLayout() + } + + private func acknowledgeHistoryLayout() { + guard let layout = lastHistoryLayout else { return } + if historyPaging.laidOut(historyVersion: layout.version, madeProgress: layout.progress) { + scheduleHistoryPump() + } + } + + func retryHistory() { + historyPaging.resume() + viewportNeedsOlder = true + scheduleHistoryPump() + } + + private func scheduleHistoryPump() { + guard !historyPumpScheduled else { return } + historyPumpScheduled = true + Task { [weak self] in + // Layout's viewport report must arrive before we re-evaluate + // demand; an inserted page may have filled the entire buffer. + await Task.yield() + guard let self else { return } + self.historyPumpScheduled = false + self.pumpHistory() + } + } + + private func pumpHistory() { + guard isActive, !isJumpingToLatest, viewportNeedsOlder, hasMore, + !isSyncingTail, !isLoadingOlder, olderTask == nil, + let controller = chat.windowController, + let request = historyPaging.begin() else { return } + let gate = ChatHistoryRequestGate() + historyGate = gate + let cancellation = historyCancellation + let modeChange = windowModeTask olderTask = Task { [weak self] in - _ = await controller.fetchOlder() - self?.olderTask = nil + await cancellation?.value + await modeChange?.value + guard !Task.isCancelled, gate.allowsApply else { return } + let outcome = await controller.fetchOlder(onBeforeApply: { _ in gate.allowsApply }) + guard let self, self.historyPaging.generation == request else { return } + self.olderTask = nil + self.historyGate = nil + let delay = self.historyPaging.received(outcome, generation: request) + self.acknowledgeHistoryLayout() + // A completed tail sync may already have tried to pump while + // olderTask was occupied. Stale pages never acknowledge layout. + switch outcome { + case .stopped(.invalidated), .stopped(.epochReset): self.scheduleHistoryPump() + default: break + } + if let delay { + self.historyRetryTask = Task { [weak self] in + try? await Task.sleep(for: .milliseconds(delay)) + guard !Task.isCancelled, let self else { return } + if self.historyPaging.retryElapsed(generation: request) { self.scheduleHistoryPump() } + } + } + } + } + + private func cancelHistory() { + historyGate?.invalidate() + historyGate = nil + olderTask?.cancel() + olderTask = nil + historyRetryTask?.cancel() + historyRetryTask = nil + historyPaging.cancel() + lastHistoryLayout = nil + if let controller = chat.windowController { + let previous = historyCancellation + historyCancellation = Task { + await previous?.value + await controller.cancelOlderLoad() + } + } + } + + func jumpToLatest() { + guard isActive, !isJumpingToLatest, let controller = chat.windowController else { return } + cancelHistory() + viewportNeedsOlder = false + isJumpingToLatest = true + let chat = chat + let cancellation = historyCancellation + let modeChange = windowModeTask + jumpTask = Task { [weak self] in + await cancellation?.value + await modeChange?.value + guard !Task.isCancelled else { return } + if await controller.state.requiresLatestReset { + await controller.syncTail(ensureAfterCurrent: true, allowingHistoryReset: true) + } + guard !Task.isCancelled, let self, self.isActive, self.chat === chat else { return } + self.isJumpingToLatest = false + self.jumpTask = nil + // A failed catch-up must not claim that the retained window is + // the live tail, or trim away the reader's remaining history. + guard await !controller.state.requiresLatestReset else { return } + await controller.setViewMode(.tail) + guard !Task.isCancelled, self.chat === chat else { return } + self.followsTail = true + self.jumpToLatestToken += 1 } - return true } /// Error state → try again (detail + a tail sync past any in-flight run). @@ -270,6 +413,16 @@ final class ChatModel { agentState: detail?.agentState, hasMoreMessages: window.hasMore ) + let sources = Set(visible.flatMap { block -> [String] in + guard case .block(let value) = block else { return [] } + switch value { + case .agentText(let text): return [text.text] + case .agentReasoning(let text): return [text.text] + default: return [] + } + }) + await markdownCache.prepare(Array(sources.subtracting(previousMarkdownSources))) + previousMarkdownSources = sources guard !Task.isCancelled else { return } apply(visible, window: window, detail: detail, summary: summary, machines: machines) } @@ -291,10 +444,35 @@ final class ChatModel { summary: SessionSummary?, machines: [Machine] ) { + if let previousEpoch = lastEpoch, let epoch = window.epoch, previousEpoch != epoch { + cancelHistory() + jumpToLatestToken += 1 + } + if let epoch = window.epoch { lastEpoch = epoch } // Session/machine status events can arrive without new messages. // Do not invalidate the entire lazy transcript for identical output. if blocks != visible { blocks = visible + var liveIDs = Set() + func collect(_ block: ChatBlock) { + liveIDs.insert(block.id) + if case .toolCall(let tool) = block { + if let request = tool.tool.permission?.id { liveIDs.insert(request) } + tool.children.forEach(collect) + } + } + for block in visible { + switch block { + case .block(let value): collect(value) + case .toolGroup(let group): + liveIDs.insert(group.id) + group.tools.forEach { collect(.toolCall($0)) } + } + } + presentationState.prune(to: liveIDs) + for key in expandedToolGroups.keys where !liveIDs.contains(key) { + expandedToolGroups.removeValue(forKey: key) + } } header = Self.buildHeader( sessionId: sessionId, @@ -304,11 +482,14 @@ final class ChatModel { ) basePath = detail?.metadata?.path ?? summary?.metadata?.path hasMore = window.hasMore + historyPaging.refreshAvailability(hasMore: hasMore) isLoadingOlder = window.isLoadingMore isSyncingTail = window.isSyncingTail warning = window.warning tailRevision = window.tailRevision historyVersion = window.historyVersion + hasTrimmedTail = window.requiresLatestReset + scheduleHistoryPump() let isEmpty = visible.isEmpty // syncGeneration 0 = no tail sync has even begun (the moment between diff --git a/ios/Hapi/Features/Chat/ChatPresentationState.swift b/ios/Hapi/Features/Chat/ChatPresentationState.swift new file mode 100644 index 00000000..c9a4aa40 --- /dev/null +++ b/ios/Hapi/Features/Chat/ChatPresentationState.swift @@ -0,0 +1,46 @@ +import Observation +import SwiftUI + +/// User-owned state outlives recycled cells, but not the retained transcript. +@MainActor @Observable +final class ChatPresentationState { + struct Key: Hashable { let id: String; let field: String } + var values: [Key: Any] = [:] + + func prune(to ids: Set) { + for key in values.keys.filter({ !ids.contains($0.id) }) { values.removeValue(forKey: key) } + } +} + +private struct ChatPresentationStateKey: EnvironmentKey { + static let defaultValue: ChatPresentationState? = nil +} + +extension EnvironmentValues { + var chatPresentationState: ChatPresentationState? { + get { self[ChatPresentationStateKey.self] } + set { self[ChatPresentationStateKey.self] = newValue } + } +} + +@MainActor @propertyWrapper +struct ChatStoredState: DynamicProperty { + @Environment(\.chatPresentationState) private var store + @State private var fallback: Value + private let key: ChatPresentationState.Key + + init(wrappedValue: Value, id: String, field: String) { + _fallback = State(initialValue: wrappedValue) + key = .init(id: id, field: field) + } + + var wrappedValue: Value { + get { store?.values[key] as? Value ?? fallback } + nonmutating set { + if let store { store.values[key] = newValue } + else { fallback = newValue } + } + } + + var projectedValue: Binding { Binding(get: { wrappedValue }, set: { wrappedValue = $0 }) } +} diff --git a/ios/Hapi/Features/Chat/ChatTranscriptView.swift b/ios/Hapi/Features/Chat/ChatTranscriptView.swift index 52901024..0d22c45b 100644 --- a/ios/Hapi/Features/Chat/ChatTranscriptView.swift +++ b/ios/Hapi/Features/Chat/ChatTranscriptView.swift @@ -1,218 +1,114 @@ import HapiClient import HapiProtocol +import HapiUI import SwiftUI -/// Scroll state belongs to this mounted transcript. Clearing the message -/// window removes it, so refetched messages open at the latest row again. +/// Presentation-only flattening; protocol groups and their semantics stay intact. +private enum TranscriptRow: Identifiable, Equatable { + case history(ChatHistoryPagingState.Phase, Bool) + case message(VisibleChatBlock) + case group(ToolGroupBlock, Bool) + + static let historyID = "chat-history-control" + var id: String { + switch self { + case .history: Self.historyID + case .message(let block): block.stableId + case .group(let group, _): group.id + } + } +} + struct ChatTranscriptView: View { let model: ChatModel + @Environment(\.self) private var environment - @State private var isAtBottom = true - @State private var isAtTop = false - @State private var tailFollow = ChatTailFollowState() - /// One history request per reader gesture; layout changes cannot arm it. - @State private var automaticHistoryLoadArmed = false - /// Newest block id last seen while at the bottom (new-messages pill). - @State private var newestSeenID: String? - /// First existing block captured when the reader requests older history. - @State private var pendingAnchorID: String? - - private static let bottomSentinelID = "chat-bottom-sentinel" - private static let scrollCoordinateSpace = "chat-viewport" - - // MARK: - Thread - - var body: some View { - GeometryReader { viewport in - ScrollViewReader { scroll in - ScrollView(.vertical) { - LazyVStack(alignment: .leading, spacing: 10) { - if model.hasMore || model.isLoadingOlder { - Button(action: requestOlderPage) { - OlderHistoryRow(isLoading: model.isLoadingOlder) - } - .buttonStyle(.plain) - .disabled(model.isLoadingOlder || model.isSyncingTail) - .accessibilityLabel("Load older messages") - } - ForEach(model.blocks, id: \.stableId) { block in - ChatBlockCard(block: block, basePath: model.basePath) - // maxWidth: .infinity alone is only a proposal; - // wide markdown/code must not size the thread. - .frame(width: max(0, viewport.size.width - 24), alignment: .leading) - } - Color.clear - .frame(height: 1) - .id(Self.bottomSentinelID) - } - .padding(.horizontal, 12) - .padding(.top, 10) - .frame(width: viewport.size.width, alignment: .leading) - .onGeometryChange(for: ChatScrollGeometry.self) { geometry in - let frame = geometry.frame(in: .named(Self.scrollCoordinateSpace)) - return ChatScrollGeometry( - isAtTop: frame.minY >= -40, - isAtBottom: frame.maxY <= viewport.size.height + 40, - isBottomAligned: frame.maxY <= viewport.size.height + 1, - contentHeight: frame.height, - viewportHeight: viewport.size.height - ) - } action: { geometry in - isAtTop = geometry.isAtTop - isAtBottom = geometry.isAtBottom - tailFollow.layoutChanged( - contentHeight: Double(geometry.contentHeight), - viewportHeight: Double(geometry.viewportHeight), - // Even growth within the 40 pt follow threshold - // needs a correction to show the entire last row. - isAtBottom: geometry.isBottomAligned - ) - if geometry.isAtBottom { - automaticHistoryLoadArmed = false - newestSeenID = newestBlockID - } - requestAutomaticHistoryIfNeeded() - } - } - .coordinateSpace(name: Self.scrollCoordinateSpace) - .scrollDismissesKeyboard(.interactively) - .simultaneousGesture( - DragGesture().onChanged { value in - guard abs(value.translation.height) > abs(value.translation.width) else { return } - if !tailFollow.isDragging { - tailFollow.beginDragging() - } - }.onEnded { value in - tailFollow.endDragging(isAtBottom: isAtBottom) - // Ignore horizontal pans inside code and tables. - guard abs(value.translation.height) > abs(value.translation.width) else { return } - automaticHistoryLoadArmed = value.translation.height > 0 - requestAutomaticHistoryIfNeeded() - } - ) - .overlay(alignment: .bottom) { - newMessagesPill - } - .task(id: tailFollow.scrollRequest) { - guard let request = tailFollow.scrollRequest else { return } - // Geometry has measured the new row heights. Coalesce - // layout changes, then issue one correction; a reader - // drag or transcript removal cancels the pending task. - await Task.yield() - guard !Task.isCancelled, tailFollow.scrollRequest == request else { return } - newestSeenID = newestBlockID - scroll.scrollTo(Self.bottomSentinelID, anchor: .bottom) - } - .onDisappear { - tailFollow.endDragging(isAtBottom: isAtBottom) - } - .onChange(of: model.historyVersion) { - reanchorAfterPrepend(scroll: scroll) - } - .onChange(of: newestBlockID, initial: true) { - if tailFollow.isFollowingTail { - newestSeenID = newestBlockID - } - } - .onChange(of: model.isSyncingTail) { - requestAutomaticHistoryIfNeeded() + private var rows: [TranscriptRow] { + var rows: [TranscriptRow] = [.history(model.historyPaging.phase, model.hasMore)] + for block in model.blocks { + if case .toolGroup(let group) = block { + let expanded = model.expandedToolGroups[group.id] ?? group.defaultOpen + rows.append(.group(group, expanded)) + if expanded { + rows.append(contentsOf: group.tools.map { .message(.block(.toolCall($0))) }) } + } else { + rows.append(.message(block)) } } + return rows } - private var newestBlockID: String? { - model.blocks.last?.stableId - } - - private func requestAutomaticHistoryIfNeeded() { - guard automaticHistoryLoadArmed, isAtTop, !isAtBottom else { return } - requestOlderPage() - } - - /// Capture the anchor BEFORE the prepend lands, then ask for the page. - private func requestOlderPage() { - // Paging starts with the history control visible, so the first real - // block is the reading anchor. No per-row geometry/state feedback. - let anchor = model.blocks.first?.stableId - // Busy/rejected requests must not overwrite an in-flight anchor. - if model.loadOlder() { - automaticHistoryLoadArmed = false - pendingAnchorID = anchor + var body: some View { + AnchoredTranscriptList( + items: rows, + historyVersion: model.historyVersion, + jumpToken: model.jumpToLatestToken, + historyControlID: TranscriptRow.historyID, + onViewport: { viewport in + model.readingViewportChanged(followsTail: viewport.followsTail, needsOlder: viewport.needsOlder) + }, + onLayout: { version, progress in model.historyLaidOut(version: version, madeProgress: progress) } + ) { row in + // HostingConfiguration creates a new root: inherit app services. + AnyView(rowView(row).environment(\.self, environment)) + } + .overlay(alignment: .bottomTrailing) { + if !model.followsTail || model.hasTrimmedTail || model.isJumpingToLatest { + Button(action: model.jumpToLatest) { + HStack(spacing: 6) { + if model.isJumpingToLatest { ProgressView().controlSize(.small) } + Text("Back to latest") + Image(systemName: "arrow.down") + } + .font(.footnote.weight(.medium)) + .padding(.horizontal, 14) + .padding(.vertical, 9) + .background(.regularMaterial, in: Capsule()) + } + .disabled(model.isJumpingToLatest) + .accessibilityIdentifier("chat-latest") + .padding(12) + } } - } - - /// `historyVersion` bumped: rows were prepended above the viewport. - /// Restore the captured row once, without retaining a scroll binding. - /// Preservation is approximate to the row, as with lazy history before. - private func reanchorAfterPrepend(scroll: ScrollViewProxy) { - guard let anchor = pendingAnchorID else { return } - pendingAnchorID = nil - guard !isAtBottom else { return } - scroll.scrollTo(anchor, anchor: .top) - } - - // MARK: - New-messages pill - - private var unseenCount: Int { - guard !tailFollow.isFollowingTail, let seen = newestSeenID else { return 0 } - guard let index = model.blocks.lastIndex(where: { $0.stableId == seen }) else { return 0 } - return model.blocks.count - 1 - index } @ViewBuilder - private var newMessagesPill: some View { - let count = unseenCount - if count > 0 { - Button { - newestSeenID = newestBlockID - tailFollow.followTail() - } label: { - Text(count == 1 - ? String(localized: "1 new message ↓") - : String(format: String(localized: "%lld new messages ↓"), Int64(count))) - .font(.footnote.weight(.medium)) - .padding(.horizontal, 14) - .padding(.vertical, 7) - .background(.tint, in: Capsule()) - .foregroundStyle(.white) - .shadow(radius: 3, y: 1) + private func rowView(_ row: TranscriptRow) -> some View { + switch row { + case .history(let phase, let hasMore): + Button(action: model.retryHistory) { + HStack(spacing: 8) { + if !hasMore { + Text("Beginning of conversation") + } else { + switch phase { + case .loading, .retrying, .awaitingLayout: + ProgressView().controlSize(.small) + Text("Loading older messages…") + case .failed: Text("Couldn't load history. Tap to retry.") + case .paused: Text("Continue loading history") + default: Text("Load older messages") + } + } + } + .font(.caption) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, minHeight: 44, maxHeight: 44) + .contentShape(Rectangle()) } .buttonStyle(.plain) - .padding(.bottom, 12) + .disabled(!hasMore || model.isLoadingOlder || model.isSyncingTail) + .accessibilityIdentifier("chat-history") + case .message(let block): + ChatBlockCard(block: block, basePath: model.basePath) + case .group(let group, let expanded): + ToolGroupBlockView( + block: group, + basePath: model.basePath, + expansion: Binding(get: { expanded }, set: { model.expandedToolGroups[group.id] = $0 }), + showsTools: false + ) } } } - -/// Observe row/viewport resizing as well as edges. Scrolling within the -/// middle of the thread still does not publish every point of a drag. -private struct ChatScrollGeometry: Equatable { - let isAtTop: Bool - let isAtBottom: Bool - let isBottomAligned: Bool - let contentHeight: CGFloat - let viewportHeight: CGFloat -} - -/// Centered "· · ·" / spinner row inside the load-older control. -private struct OlderHistoryRow: View { - let isLoading: Bool - - var body: some View { - HStack(spacing: 8) { - if isLoading { - ProgressView() - .controlSize(.small) - Text("Loading older messages…") - .font(.caption) - .foregroundStyle(.secondary) - } else { - Text("· · ·") - .font(.caption) - .foregroundStyle(.secondary) - } - } - .frame(maxWidth: .infinity) - .padding(.vertical, 8) - } -} diff --git a/ios/Hapi/Features/Chat/ChatView.swift b/ios/Hapi/Features/Chat/ChatView.swift index df4539cc..ba35baf4 100644 --- a/ios/Hapi/Features/Chat/ChatView.swift +++ b/ios/Hapi/Features/Chat/ChatView.swift @@ -46,7 +46,7 @@ struct ChatView: View { initialLoading } else if model.loadFailed { loadFailedState - } else if model.blocks.isEmpty { + } else if model.blocks.isEmpty && !model.hasMore { emptyState } else { ChatTranscriptView(model: model) @@ -145,6 +145,8 @@ struct ChatView: View { ) } .environment(\.chatMedia, model.imageLoader) + .environment(\.chatPresentationState, model.presentationState) + .environment(\.hapiMarkdownCache, model.markdownCache) .environment(\.chatInteractions, model.interactor) .onChange(of: model.supersededSessionId) { if let superseding = model.supersededSessionId { diff --git a/ios/Hapi/Models/ChatSession.swift b/ios/Hapi/Models/ChatSession.swift index 08a13938..77273b56 100644 --- a/ios/Hapi/Models/ChatSession.swift +++ b/ios/Hapi/Models/ChatSession.swift @@ -93,12 +93,17 @@ final class ChatSession { /// Open the window, then subscribe: every routed message event finds the /// controller already in place (the Android wiring order). Idempotent. - func start() async { + func start(preservingHistory: Bool = false) async { guard !started, !stopped else { return } started = true registerActive(self) let controller = await windows.open(sessionId: sessionId) - await controller.activate() + guard !stopped else { return } + if preservingHistory { + await controller.setViewMode(.history) + } else { + await controller.activate() + } // A stop() can land while the opens above were suspended; do not // bring the SSE up for a dead chat. guard !stopped else { return } @@ -106,7 +111,7 @@ final class ChatSession { startSessionSSE() // Explicit catch-up on entry (the snapshot may be stale); the SSE // handshake's own gap handling covers everything missed after this. - Task { await controller.syncTail() } + if !preservingHistory { Task { await controller.syncTail() } } } /// Tears the session pipe down; the engine-side resume cursor is saved diff --git a/ios/Hapi/Models/HubSession.swift b/ios/Hapi/Models/HubSession.swift index 6bfb2349..cee14991 100644 --- a/ios/Hapi/Models/HubSession.swift +++ b/ios/Hapi/Models/HubSession.swift @@ -98,7 +98,8 @@ final class HubSession { provider: api, snapshots: WindowSnapshotStore( directory: snapshotDirectory.appendingPathComponent("windows", isDirectory: true) - ) + ), + historyRetentionLimit: MessageWindowConstants.olderLoadWindowSize ) let scratchlist = ScratchlistStore(api: api) self.scratchlist = scratchlist diff --git a/ios/Hapi/Resources/Localizable.xcstrings b/ios/Hapi/Resources/Localizable.xcstrings index 7d9dbd5f..117afccf 100644 --- a/ios/Hapi/Resources/Localizable.xcstrings +++ b/ios/Hapi/Resources/Localizable.xcstrings @@ -691,6 +691,16 @@ } } }, + "Back to latest": { + "localizations": { + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "回到最新" + } + } + } + }, "Background task": { "localizations": { "zh-Hans": { @@ -711,6 +721,16 @@ } } }, + "Beginning of conversation": { + "localizations": { + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "已到会话起点" + } + } + } + }, "Binary file not shown": { "localizations": { "zh-Hans": { @@ -931,6 +951,16 @@ } } }, + "Continue loading history": { + "localizations": { + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "继续加载历史" + } + } + } + }, "Copy Contents": { "localizations": { "zh-Hans": { @@ -1031,6 +1061,16 @@ } } }, + "Couldn't load history. Tap to retry.": { + "localizations": { + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "历史加载失败,点此重试" + } + } + } + }, "Couldn't load the scratchlist": { "localizations": { "zh-Hans": { diff --git a/ios/HapiTests/ChatHistoryPumpTests.swift b/ios/HapiTests/ChatHistoryPumpTests.swift new file mode 100644 index 00000000..b4c8e30c --- /dev/null +++ b/ios/HapiTests/ChatHistoryPumpTests.swift @@ -0,0 +1,125 @@ +import Foundation +import HapiClient +import HapiProtocol +import XCTest +@testable import Hapi + +@MainActor +final class ChatHistoryPumpTests: XCTestCase { + func testInvalidatedOlderResponseRestartsDemandAfterTailSyncFinished() async throws { + let performer = HistoryPumpHTTP() + defer { Task { await performer.releaseOlder() } } + // All REST calls use the fake; the unused SSE pipe targets a closed + // loopback port, never a user's hub. Unique cache/draft namespace. + let hubURL = "http://127.0.0.1:1/history-pump-\(UUID().uuidString)" + let credentials = InMemoryCredentialStore() + let payload = Data(#"{"uid":1,"exp":4102444800,"ns":"test"}"#.utf8).base64EncodedString() + try credentials.store(HubCredentials( + hubUrl: "http://127.0.0.1:1", accessToken: "test", jwt: "e30.\(payload).test" + )) + let hub = try XCTUnwrap(HubSession(hubUrl: hubURL, credentialStore: credentials, performer: performer)) + let model = ChatModel(session: hub, sessionId: "history") + defer { model.stop(); hub.shutdown() } + model.start() + try await eventually { model.hasMore && !model.isSyncingTail && !model.blocks.isEmpty } + let controller = await hub.windows.open(sessionId: "history") + // Drain both ChatSession's entry sync and the interactor's reconcile. + await controller.syncTail(ensureAfterCurrent: true) + try await eventually { !model.isSyncingTail } + model.readingViewportChanged(followsTail: false, needsOlder: true) + try await eventually { await performer.beforeRequests == 1 } + XCTAssertEqual(model.historyPaging.phase, .loading) + + // Invalidate the slow before request, finish sync, and let its state + // reach the real ChatModel pipeline while olderTask still blocks it. + await performer.enableTailUpdate() + await controller.syncTail(ensureAfterCurrent: true) + let revision = await controller.state.tailRevision + try await eventually { model.tailRevision == revision && !model.isSyncingTail } + try await Task.sleep(for: .milliseconds(30)) + XCTAssertEqual(model.historyPaging.phase, .loading) + await performer.releaseOlder() + + // No new viewport, layout acknowledgement, or SSE event. + try await eventually { await performer.beforeRequests == 2 } + try await eventually { model.historyVersion == 1 } + let state = await controller.state + XCTAssertEqual(state.messages.map(\.id), ["fresh", "new", "live"]) + XCTAssertEqual(model.historyPaging.phase, .awaitingLayout(1)) + } + + private func eventually(file: StaticString = #filePath, line: UInt = #line, _ condition: () async -> Bool) async throws { + for _ in 0..<200 { + if await condition() { return } + try await Task.sleep(for: .milliseconds(20)) + } + XCTFail("History coordinator did not reach the expected state", file: file, line: line) + throw URLError(.timedOut) + } +} + +private actor HistoryPumpHTTP: HTTPPerforming { + private(set) var beforeRequests = 0 + private var olderContinuation: CheckedContinuation? + private var sendsTailUpdate = false + + func enableTailUpdate() { sendsTailUpdate = true } + + func releaseOlder() { + olderContinuation?.resume() + olderContinuation = nil + } + + func perform(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) { + let url = request.url! + guard url.lastPathComponent == "messages" else { + return (Data(#"{"error":"unused test endpoint"}"#.utf8), + HTTPURLResponse(url: url, statusCode: 404, httpVersion: nil, headerFields: nil)!) + } + let query = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems ?? [] + let response: MessagesResponse + if query.contains(where: { $0.name == "beforeAt" }) { + beforeRequests += 1 + let stale = beforeRequests == 1 + if stale { + await withCheckedContinuation { olderContinuation = $0 } + } + response = page( + [row(stale ? "stale" : "fresh", seq: stale ? 9 : 8)], + direction: .before, hasMore: false + ) + } else if query.contains(where: { $0.name == "afterAt" }) { + response = page(sendsTailUpdate ? [row("live", seq: 11)] : [], direction: .after, hasMore: false) + } else { + response = page([row("new", seq: 10)], direction: .latest, hasMore: true) + } + return (try JSONEncoder().encode(response), + HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!) + } + + private func row(_ id: String, seq: Int) -> DecryptedMessage { + DecryptedMessage( + id: id, seq: seq, + content: [ + "role": "agent", + "content": ["type": "codex", "data": ["type": "message", "message": .string(id)]], + ], + createdAt: seq * 1000, invokedAt: seq * 1000 + ) + } + + private func page( + _ messages: [DecryptedMessage], direction: MessagesPage.Direction, hasMore: Bool + ) -> MessagesResponse { + MessagesResponse(messages: messages, page: MessagesPage( + direction: direction, limit: 200, epoch: 1, reset: false, + nextBeforeSeq: direction == .after ? nil : messages.first?.seq, + nextBeforeAt: direction == .after ? nil : messages.first?.createdAt, + nextAfterSeq: direction == .after ? (messages.last?.seq ?? 10) : nil, + nextAfterAt: direction == .after ? (messages.last?.createdAt ?? 10000) : nil, + snapshotHeadSeq: messages.last?.seq ?? 10, + snapshotHeadAt: messages.last?.createdAt ?? 10000, + hasMore: hasMore + )) + } +} diff --git a/ios/HapiTests/TranscriptGeometryTests.swift b/ios/HapiTests/TranscriptGeometryTests.swift new file mode 100644 index 00000000..9873392f --- /dev/null +++ b/ios/HapiTests/TranscriptGeometryTests.swift @@ -0,0 +1,101 @@ +import UIKit +import XCTest +@testable import HapiUI + +@MainActor +final class TranscriptGeometryTests: XCTestCase { + func testIndexedQueriesMatchLinearGeometryThroughWindowAndWidthChanges() throws { + let layout = TranscriptLayout() + let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: 390, height: 844), collectionViewLayout: layout) + var ids = (0..<800).map(String.init) + var heights: [String: CGFloat] = [:] + layout.setItems(ids, width: 390) + layout.prepare() + for index in stride(from: 0, to: ids.count, by: 7) { + let height = CGFloat(20 + index % 19 * 87) + heights[ids[index]] = height + let original = try XCTUnwrap(layout.layoutAttributesForItem(at: IndexPath(item: index, section: 0))) + let preferred = original.copy() as! UICollectionViewLayoutAttributes + preferred.size.height = height + _ = layout.invalidationContext(forPreferredLayoutAttributes: preferred, withOriginalAttributes: original) + } + layout.prepare() + + func verify(width: CGFloat) { + var y: CGFloat = 10 + let expected = ids.map { id -> CGRect in + let frame = CGRect(x: 12, y: y, width: width - 24, height: heights[id] ?? 100) + y = frame.maxY + 10 + return frame + } + XCTAssertEqual(layout.collectionViewContentSize.height, y, accuracy: 0.01) + let rects = stride(from: -500, to: Int(y) + 500, by: 377).map { + CGRect(x: 0, y: $0, width: Int(width), height: 844) + } + [.zero, .null, .infinite, CGRect(x: 1000, y: 0, width: 100, height: 100000)] + for rect in rects { + let indices = expected.indices.filter { expected[$0].intersects(rect) } + let actual = layout.layoutAttributesForElements(in: rect) ?? [] + XCTAssertEqual(actual.map(\.indexPath.item), indices) + XCTAssertEqual(actual.map(\.frame), indices.map { expected[$0] }) + } + } + verify(width: 390) + ids = (800..<820).map(String.init) + Array(ids.dropLast(20)) + layout.setItems(ids, width: 390) + layout.prepare() + verify(width: 390) + // Width changes discard stale measurements but retain correct indices. + heights.removeAll() + layout.setItems(ids, width: 430) + layout.prepare() + verify(width: 430) + layout.setItems([], width: 430) + layout.prepare() + XCTAssertTrue(layout.layoutAttributesForElements(in: .infinite)?.isEmpty == true) + XCTAssertNil(layout.layoutAttributesForItem(at: IndexPath(item: 0, section: 0))) + XCTAssertEqual(collection.collectionViewLayout, layout) + } + + #if DEBUG + func testVisibleQueriesVisitOnlyAViewportSizedRange() { + let layout = TranscriptLayout() + layout.setItems((0..<800).map(String.init), width: 390) + layout.prepare() + for step in 0..<200 { + let rect = CGRect(x: 0, y: 1000 + step * 350, width: 390, height: 844) + let visible = layout.layoutAttributesForElements(in: rect) ?? [] + XCTAssertFalse(visible.isEmpty) + XCTAssertTrue(visible.allSatisfy { $0.frame.intersects(rect) }) + XCTAssertLessThanOrEqual(visible.count, 9) + } + print("Transcript geometry: 800 rows / 200 viewport queries / \(layout.visibleQueryProbeCount) probes") + // log2(800) + visible rows + a small boundary allowance, not 800 + // CGRect intersection tests on every frame. + XCTAssertLessThanOrEqual(layout.visibleQueryProbeCount, 200 * 32) + } + + func testMeasuringRowsDoesNotAllocateLayoutAttributesForTheWholeWindow() throws { + let layout = TranscriptLayout() + let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: 390, height: 844), collectionViewLayout: layout) + layout.setItems((0..<800).map(String.init), width: 390) + layout.prepare() + let old = try XCTUnwrap(layout.layoutAttributesForItem(at: IndexPath(item: 300, section: 0))) + let oldFrame = old.frame + let allocations = layout.attributeCreationCount + for index in 0..<20 { + let original = try XCTUnwrap(layout.layoutAttributesForItem(at: IndexPath(item: index, section: 0))) + let preferred = original.copy() as! UICollectionViewLayoutAttributes + preferred.size.height += 20 + _ = layout.invalidationContext(forPreferredLayoutAttributes: preferred, withOriginalAttributes: original) + layout.prepare() + } + let updated = try XCTUnwrap(layout.layoutAttributesForItem(at: IndexPath(item: 300, section: 0))) + XCTAssertEqual(updated.frame.minY, oldFrame.minY + 400, accuracy: 0.01) + XCTAssertEqual(old.frame, oldFrame, "UIKit's previously returned attributes must remain immutable") + XCTAssertEqual(collection.collectionViewLayout, layout) + let created = layout.attributeCreationCount - allocations + print("Transcript geometry: 20 self-sizing updates / \(created) attributes created") + XCTAssertLessThanOrEqual(created, 40) + } + #endif +} diff --git a/ios/HapiTests/TranscriptLayoutTests.swift b/ios/HapiTests/TranscriptLayoutTests.swift new file mode 100644 index 00000000..343df619 --- /dev/null +++ b/ios/HapiTests/TranscriptLayoutTests.swift @@ -0,0 +1,269 @@ +import HapiUI +import Observation +import SwiftUI +import UIKit +import XCTest +@testable import Hapi +@testable import HapiProtocol + +@MainActor +final class TranscriptLayoutTests: XCTestCase { + struct Row: Identifiable, Equatable { + let id: String + var height: CGFloat + } + + @MainActor @Observable + final class Driver { + var rows: [Row] + var version = 0 + var layouts: [Int] = [] + var viewport: TranscriptViewport? + let presentation = ChatPresentationState() + init(_ rows: [Row]) { self.rows = rows } + } + + private struct Harness: View { + var driver: Driver + var body: some View { + AnchoredTranscriptList( + items: driver.rows, historyVersion: driver.version, jumpToken: 0, + historyControlID: "history", + onViewport: { driver.viewport = $0 }, + onLayout: { version, _ in driver.layouts.append(version) } + ) { row in + AnyView(StatefulRow(row: row).environment(\.chatPresentationState, driver.presentation)) + } + } + } + + private struct StatefulRow: View { + let row: Row + @ChatStoredState private var expanded: Bool + init(row: Row) { + self.row = row + _expanded = ChatStoredState(wrappedValue: false, id: row.id, field: "expanded") + } + var body: some View { + Text(row.id).frame(maxWidth: .infinity).frame(height: row.height + (expanded ? 90 : 0)) + } + } + + private func rows(_ range: Range) -> [Row] { + range.map { Row(id: "message-\($0)", height: CGFloat(65 + ($0 % 5) * 29)) } + } + + private func host(_ driver: Driver) -> (UIWindow, UICollectionView) { + let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first! + let window = UIWindow(windowScene: scene) + window.frame = CGRect(x: 0, y: 0, width: 390, height: 844) + let host = UIHostingController(rootView: Harness(driver: driver)) + window.rootViewController = host + window.makeKeyAndVisible() + host.view.frame = window.bounds + host.view.setNeedsLayout() + host.view.layoutIfNeeded() + func find(_ view: UIView) -> UICollectionView? { + if let collection = view as? UICollectionView { return collection } + return view.subviews.lazy.compactMap(find).first + } + return (window, find(host.view)!) + } + + private func settle(_ condition: () -> Bool = { true }) async { + for _ in 0..<100 { + try? await Task.sleep(for: .milliseconds(20)) + if condition() { + try? await Task.sleep(for: .milliseconds(80)) + return + } + } + XCTFail("Transcript did not finish layout") + } + + private func browse(_ view: UICollectionView, index: Int, inset: CGFloat = 31) async { + view.delegate?.scrollViewWillBeginDragging?(view) + view.scrollToItem(at: IndexPath(item: index, section: 0), at: .top, animated: false) + view.contentOffset.y += inset + view.layoutIfNeeded() + view.delegate?.scrollViewDidEndDragging?(view, willDecelerate: false) + await settle() + view.layoutIfNeeded() + } + + private func readingAnchor(_ view: UICollectionView) -> (id: String, y: CGFloat)? { + view.visibleCells + .filter { $0.frame.maxY > view.contentOffset.y && $0.frame.minY < view.contentOffset.y + view.bounds.height && $0.accessibilityIdentifier != "chat-row-history" } + .sorted { $0.frame.minY < $1.frame.minY } + .first.map { ($0.accessibilityIdentifier!, $0.frame.minY - view.contentOffset.y) } + } + + private func anchorY(_ id: String, in view: UICollectionView) -> CGFloat? { + view.visibleCells.first { $0.accessibilityIdentifier == id }.map { $0.frame.minY - view.contentOffset.y } + } + + private func diagnostic(_ driver: Driver, _ view: UICollectionView) -> String { + let cells = view.visibleCells.map { ($0.accessibilityIdentifier ?? "?") + ":" + String(describing: $0.frame.minY - view.contentOffset.y) } + return "offset=\(view.contentOffset.y) height=\(view.contentSize.height) follows=\(String(describing: driver.viewport?.followsTail)) cells=\(cells)" + } + + func testPrependKeepsTheCurrentPartialRowNotTheRequestStartRow() async throws { + let driver = Driver(rows(100..<180)) + let (window, view) = host(driver) + defer { window.isHidden = true } + await settle { driver.layouts.contains(0) } + await browse(view, index: 12) + // The reader moves again while the hypothetical HTTP request waits. + await browse(view, index: 17, inset: 47) + let anchor = try XCTUnwrap(readingAnchor(view)) + XCTAssertLessThanOrEqual(anchor.y, 0) + driver.rows = rows(80..<100) + Array(driver.rows.dropLast(20)) + driver.version = 1 + await settle { driver.layouts.contains(1) } + XCTAssertEqual(try XCTUnwrap(anchorY(anchor.id, in: view), "anchor=\(anchor); \(diagnostic(driver, view))"), anchor.y, accuracy: 1) + XCTAssertEqual(driver.viewport?.followsTail, false) + } + + func testStreamingTailAndRowResizeDoNotMoveHistoryAnchor() async throws { + let driver = Driver(rows(0..<80)) + let (window, view) = host(driver) + defer { window.isHidden = true } + await settle { driver.layouts.contains(0) } + await browse(view, index: 25) + let anchor = try XCTUnwrap(readingAnchor(view)) + driver.rows[79].height += 400 + await settle() + XCTAssertEqual(try XCTUnwrap(anchorY(anchor.id, in: view), "anchor=\(anchor); \(diagnostic(driver, view))"), anchor.y, accuracy: 1) + let id = anchor.id.replacingOccurrences(of: "chat-row-", with: "") + let index = try XCTUnwrap(driver.rows.firstIndex { $0.id == id }) + driver.rows[index].height += 160 + await settle() + XCTAssertEqual(try XCTUnwrap(anchorY(anchor.id, in: view)), anchor.y, accuracy: 1) + let resized = try XCTUnwrap(view.visibleCells.first { $0.accessibilityIdentifier == anchor.id }) + XCTAssertEqual(resized.frame.height, driver.rows[index].height, accuracy: 1) + } + + func testTailFollowsSelfSizingAndViewportResize() async throws { + let driver = Driver(rows(0..<30)) + let (window, view) = host(driver) + defer { window.isHidden = true } + await settle { driver.layouts.contains(0) } + let id = "chat-row-message-29" + XCTAssertEqual(try XCTUnwrap(view.visibleCells.first { $0.accessibilityIdentifier == id }).frame.height, + driver.rows[29].height, accuracy: 1) + driver.rows[29].height += 250 + await settle() + XCTAssertEqual(view.contentOffset.y, view.contentSize.height - view.bounds.height, accuracy: 1) + window.frame.size.height -= 250 // composer / keyboard changes viewport + window.layoutIfNeeded() + await settle() + XCTAssertEqual(view.contentOffset.y, view.contentSize.height - view.bounds.height, accuracy: 1) + XCTAssertEqual(driver.viewport?.followsTail, true) + } + + func testShortAndHiddenOnlyPagesStillAcknowledgeLayout() async { + let driver = Driver([Row(id: "history", height: 44), Row(id: "message", height: 55)]) + let (window, _) = host(driver) + defer { window.isHidden = true } + await settle { driver.layouts.contains(0) } + XCTAssertEqual(driver.viewport?.needsOlder, true) + driver.version = 1 // hidden-only page; no new views or resize signal + await settle { driver.layouts.contains(1) } + XCTAssertEqual(driver.viewport?.needsOlder, true) + } + + func testRepeatedRowIDsAreCoalescedBeforeEverySnapshotConsumer() async throws { + let id = "tool-group:exec-3a551efc-3d25-4093-831e-f7fd43482fae" + let driver = Driver([Row(id: id, height: 65), Row(id: id, height: 115)] + rows(0..<40)) + let (window, view) = host(driver) + defer { window.isHidden = true } + await settle { driver.layouts.contains(0) } + XCTAssertEqual(view.numberOfItems(inSection: 0), 41) + await browse(view, index: 0, inset: 17) + let anchor = try XCTUnwrap(readingAnchor(view)) + XCTAssertEqual(anchor.id, "chat-row-" + id) + let cell = try XCTUnwrap(view.visibleCells.first { $0.accessibilityIdentifier == anchor.id }) + XCTAssertEqual(cell.frame.height, 115, accuracy: 1) + driver.rows = [Row(id: "older", height: 80)] + driver.rows + [Row(id: id, height: 135)] + driver.version = 1 + await settle { driver.layouts.contains(1) } + XCTAssertEqual(view.numberOfItems(inSection: 0), 42) + XCTAssertEqual(try XCTUnwrap(anchorY(anchor.id, in: view)), anchor.y, accuracy: 1) + let updated = try XCTUnwrap(view.visibleCells.first { $0.accessibilityIdentifier == anchor.id }) + XCTAssertEqual(updated.frame.height, 135, accuracy: 1) + } + + func testRegroupedHistoryRetainsBothGroupsAndTheExistingReadingAnchor() async throws { + func tool(_ id: String) -> ChatBlock { + .toolCall(ToolCallBlock( + id: id, localId: nil, createdAt: 0, invokedAt: nil, durationMs: nil, usage: nil, model: nil, + tool: ChatToolCall(id: id, name: "Read", state: .completed, createdAt: 0), children: [], meta: nil + )) + } + func group(_ blocks: [ChatBlock], previous: [VisibleChatBlock] = [], more: Bool = false) -> [VisibleChatBlock] { + let oldGroups = previous.compactMap { if case .toolGroup(let group) = $0 { group } else { nil as ToolGroupBlock? } } + return buildVisibleChatBlocks(blocks, options: .init(hasMoreMessages: more, previousGroups: oldGroups)) + } + func displayRows(_ blocks: [VisibleChatBlock]) -> [Row] { + blocks.map { Row(id: $0.stableId, height: 100) } + } + let original = group(["c", "d", "e"].map(tool)) + let trimmed = group([tool("d"), tool("e")], previous: original) + let tail = rows(0..<40) + let driver = Driver(displayRows(trimmed) + tail) + let (window, view) = host(driver) + defer { window.isHidden = true } + await settle { driver.layouts.contains(0) } + await browse(view, index: 0, inset: 23) + let anchor = try XCTUnwrap(readingAnchor(view)) + let separator = ChatBlock.agentText(AgentTextBlock( + id: "separator", localId: nil, createdAt: 0, invokedAt: nil, + durationMs: nil, usage: nil, model: nil, text: "Between groups", meta: nil + )) + let prepended = group([tool("a"), tool("b"), tool("c"), separator, tool("d"), tool("e")], previous: trimmed, more: true) + driver.rows = displayRows(prepended) + tail + driver.version = 1 + await settle { driver.layouts.contains(1) } + // Two distinct groups plus their separator; no duplicate-key crash + // and no silently dropped group in the collection's fallback path. + XCTAssertEqual(view.numberOfItems(inSection: 0), 43) + XCTAssertEqual(try XCTUnwrap(anchorY(anchor.id, in: view)), anchor.y, accuracy: 1) + } + + func testRecycledPresentationStateIsKeyedAndPruned() { + let state = ChatPresentationState() + let key = ChatPresentationState.Key(id: "tool-a", field: "expanded") + state.values[key] = true + state.values[.init(id: "tool-b", field: "answer")] = "unsent" + state.prune(to: ["tool-b"]) + XCTAssertNil(state.values[key]) + XCTAssertEqual(state.values[.init(id: "tool-b", field: "answer")] as? String, "unsent") + } + + func testHostedCellKeepsExpansionAcrossRecycling() async throws { + let driver = Driver(rows(0..<80)) + let (window, view) = host(driver) + defer { window.isHidden = true } + await settle { driver.layouts.contains(0) } + await browse(view, index: 15, inset: 0) + let anchor = try XCTUnwrap(readingAnchor(view)) + let id = anchor.id.replacingOccurrences(of: "chat-row-", with: "") + let index = try XCTUnwrap(driver.rows.firstIndex { $0.id == id }) + driver.presentation.values[.init(id: id, field: "expanded")] = true + await settle() + XCTAssertEqual(try XCTUnwrap(anchorY(anchor.id, in: view)), anchor.y, accuracy: 1) + for destination in [55, 35, 65, 45] { + await browse(view, index: destination) + XCTAssertLessThan(view.visibleCells.count, 25, diagnostic(driver, view)) + } + // UIKit may cache offscreen hosting cells. Force their destruction so + // this test proves restoration, not merely reuse of a surviving view. + view.reloadData() + view.layoutIfNeeded() + await settle() + XCTAssertNil(anchorY(anchor.id, in: view), diagnostic(driver, view)) + await browse(view, index: index, inset: 0) + let cell = try XCTUnwrap(view.visibleCells.first { $0.accessibilityIdentifier == anchor.id }) + XCTAssertEqual(cell.frame.height, driver.rows[index].height + 90, accuracy: 1) + } +} diff --git a/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryPagingState.swift b/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryPagingState.swift new file mode 100644 index 00000000..c885f2bb --- /dev/null +++ b/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryPagingState.swift @@ -0,0 +1,83 @@ +import HapiProtocol + +/// One pagination run includes its first rendered layout, not just the HTTP +/// request. Geometry may submit demand repeatedly; only an idle run starts. +/// Owned by the chat's main-thread coordinator. No timers or UI dependencies. +public struct ChatHistoryPagingState: Equatable, Sendable { + public enum Phase: Equatable, Sendable { + case idle, loading, awaitingLayout(Int), retrying, failed, paused, exhausted + } + + public private(set) var phase: Phase = .idle + public private(set) var generation = 0 + public private(set) var failures = 0 + public private(set) var pagesWithoutProgress = 0 + + public init() {} + + public mutating func begin() -> Int? { + guard phase == .idle else { return nil } + generation += 1 + phase = .loading + return generation + } + + /// Returns the bounded retry delay, in milliseconds, if one is needed. + @discardableResult + public mutating func received(_ result: OlderLoadOutcome, generation: Int) -> Int? { + guard self.generation == generation, phase == .loading else { return nil } + switch result { + case .applied(let version, _, _): + phase = .awaitingLayout(version) + failures = 0 + case .failed: + failures += 1 + phase = failures <= 2 ? .retrying : .failed + return failures == 1 ? 500 : failures == 2 ? 1500 : nil + case .stopped(let reason): + switch reason { + case .exhausted: phase = .exhausted + case .cursorDidNotAdvance: phase = .paused + default: phase = .idle + } + } + return nil + } + + @discardableResult + public mutating func retryElapsed(generation: Int) -> Bool { + guard self.generation == generation, phase == .retrying else { return false } + phase = .idle + return true + } + + /// Also called for pages with no new cells: their version still needs an + /// acknowledgement, otherwise hidden-only pages deadlock the loader. + @discardableResult + public mutating func laidOut(historyVersion: Int, madeProgress: Bool) -> Bool { + guard case .awaitingLayout(let target) = phase, historyVersion >= target else { return false } + pagesWithoutProgress = madeProgress ? 0 : pagesWithoutProgress + 1 + phase = pagesWithoutProgress >= 3 ? .paused : .idle + return true + } + + public mutating func resume() { + guard phase == .failed || phase == .paused else { return } + failures = 0 + pagesWithoutProgress = 0 + phase = .idle + } + + /// Tail-window trimming can make older pages available again. Errors and + /// no-progress pauses still require an explicit reader action. + public mutating func refreshAvailability(hasMore: Bool) { + if hasMore, phase == .exhausted { phase = .idle } + } + + public mutating func cancel() { + generation += 1 + phase = .idle + failures = 0 + pagesWithoutProgress = 0 + } +} diff --git a/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryRequestGate.swift b/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryRequestGate.swift new file mode 100644 index 00000000..9a4d14ec --- /dev/null +++ b/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatHistoryRequestGate.swift @@ -0,0 +1,13 @@ +import Foundation + +/// Synchronously invalidated by reader input; checked inside the window +/// actor's before-apply callback. Never reaches back into UI from that actor. +public final class ChatHistoryRequestGate: @unchecked Sendable { + private let lock = NSLock() + private var valid = true + + public init() {} + + public var allowsApply: Bool { lock.withLock { valid } } + public func invalidate() { lock.withLock { valid = false } } +} diff --git a/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowController.swift b/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowController.swift index dbcbcf74..c92d587c 100644 --- a/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowController.swift +++ b/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowController.swift @@ -62,6 +62,7 @@ public actor MessageWindowController { public let sessionId: String private let provider: any MessagesProviding private let snapshots: WindowSnapshotStore? + private let historyRetentionLimit: Int private var stateValue: MessageWindowState private var observers: [UUID: AsyncStream.Continuation] = [:] @@ -72,12 +73,15 @@ public actor MessageWindowController { sessionId: String, provider: any MessagesProviding, snapshots: WindowSnapshotStore? = nil, - initialState: MessageWindowState? = nil + initialState: MessageWindowState? = nil, + historyRetentionLimit: Int = MessageWindowConstants.historyWindowSize ) { self.sessionId = sessionId self.provider = provider self.snapshots = snapshots + self.historyRetentionLimit = historyRetentionLimit self.stateValue = initialState ?? MessageWindowLogic.createState(sessionId: sessionId) + self.stateValue.historyRetentionLimit = historyRetentionLimit } /// The full window state (UI projects what it needs; see @@ -107,7 +111,8 @@ public actor MessageWindowController { _ transform: (MessageWindowState) -> MessageWindowState ) -> MessageWindowState { let previous = stateValue - let next = transform(previous) + var next = transform(previous) + next.historyRetentionLimit = historyRetentionLimit stateValue = next if next != previous { for continuation in observers.values { @@ -132,7 +137,14 @@ public actor MessageWindowController { /// `ensureAfterCurrent` the call drains: if a run is already in flight, a /// trailing run is requested and awaited, so the caller returns only /// after a sync that STARTED at or after this call. - public func syncTail(ensureAfterCurrent: Bool = false) async { + public func syncTail(ensureAfterCurrent: Bool = false, allowingHistoryReset: Bool = false) async { + if allowingHistoryReset { + let epoch = controllerEpoch + if let runningTask { await runningTask.value } + guard epoch == controllerEpoch, !Task.isCancelled else { return } + await startTailSync(allowingHistoryReset: true).value + return + } guard let running = runningTask, let runningId = runningRunId else { await startTailSync().value return @@ -154,7 +166,11 @@ public actor MessageWindowController { await drainTailSync(observedRunId: runningId, observed: running) } - private func startTailSync() -> Task { + private var defersHistoryReset: Bool { + stateValue.viewMode == .history && stateValue.requiresLatestReset && stateValue.epoch != nil + } + + private func startTailSync(allowingHistoryReset: Bool = false) -> Task { runSerial += 1 let runId = runSerial let epoch = controllerEpoch @@ -169,7 +185,7 @@ public actor MessageWindowController { let generation = stateValue.syncGeneration let task = Task { [weak self] in guard let self else { return } - await self.runTailSync(generation: generation) + await self.runTailSync(generation: generation, allowingHistoryReset: allowingHistoryReset) // Completion bookkeeping is the run's LAST actor-isolated act, so // anyone resuming from `await task.value` observes the trailing // handoff already done (web: `finish` runs via .then before @@ -214,7 +230,7 @@ public actor MessageWindowController { /// Body of one tail sync (web `runTailSync`, minus `beginTailSync`, /// which ``startTailSync()`` already executed synchronously). - private func runTailSync(generation: Int) async { + private func runTailSync(generation: Int, allowingHistoryReset: Bool) async { do { let initial = stateValue let initialCursor = initial.newestPosition @@ -231,6 +247,14 @@ public actor MessageWindowController { query: .latest(limit: MessageWindowConstants.pageSize) ) guard isCurrentTailSync(generation) else { return } + // A replay gap may have hidden messages-invalidated. Keep a + // truncated reading window only after checking the server's + // epoch, never by skipping the request (or its trailing run). + if !allowingHistoryReset, defersHistoryReset, + !response.page.reset, response.page.epoch == stateValue.epoch { + finishTailSync(generation: generation, warning: nil) + return + } update { previous in guard previous.syncGeneration == generation else { return previous } var next = MessageWindowLogic.applyLatestResponse( @@ -360,6 +384,10 @@ public actor MessageWindowController { guard stateValue.olderGeneration == generation else { return .stopped(.invalidated) } + guard !Task.isCancelled else { + update { MessageWindowLogic.cancelOlderLoad($0) } + return .stopped(.invalidated) + } if let epoch = initial.epoch, response.page.epoch != epoch { update { MessageWindowLogic.applyOlderEpochMismatch($0, generation: generation) } @@ -367,6 +395,15 @@ public actor MessageWindowController { return .stopped(.epochReset) } + if response.page.hasMore { + let at = response.page.nextBeforeAt + let seq = response.page.nextBeforeSeq + guard let at, let seq, MessagePosition(at: at, seq: seq) < before else { + update { MessageWindowLogic.failOlderLoad($0, generation: generation, + warning: "Message history cursor did not advance") } + return .stopped(.cursorDidNotAdvance) + } + } var historyVersion = 0 var addedRenderableCount = 0 var applyRejected = false @@ -403,6 +440,10 @@ public actor MessageWindowController { guard stateValue.olderGeneration == generation else { return .stopped(.invalidated) } + if Task.isCancelled || error is CancellationError { + update { MessageWindowLogic.cancelOlderLoad($0) } + return .stopped(.invalidated) + } update { MessageWindowLogic.failOlderLoad( $0, diff --git a/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowControllers.swift b/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowControllers.swift index 0f0d45b6..6e7b0d6f 100644 --- a/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowControllers.swift +++ b/ios/Packages/HapiKit/Sources/HapiClient/Stores/MessageWindowControllers.swift @@ -11,11 +11,14 @@ import HapiProtocol public actor MessageWindowControllers { private let provider: any MessagesProviding private let snapshots: WindowSnapshotStore? + private let historyRetentionLimit: Int private var controllers: [String: MessageWindowController] = [:] - public init(provider: any MessagesProviding, snapshots: WindowSnapshotStore? = nil) { + public init(provider: any MessagesProviding, snapshots: WindowSnapshotStore? = nil, + historyRetentionLimit: Int = MessageWindowConstants.historyWindowSize) { self.provider = provider self.snapshots = snapshots + self.historyRetentionLimit = historyRetentionLimit } /// The session's controller, hydrating from its snapshot on first open. @@ -30,7 +33,8 @@ public actor MessageWindowControllers { sessionId: sessionId, provider: provider, snapshots: snapshots, - initialState: hydrated + initialState: hydrated, + historyRetentionLimit: historyRetentionLimit ) controllers[sessionId] = controller return controller diff --git a/ios/Packages/HapiKit/Sources/HapiProtocol/Chat/ToolGroups.swift b/ios/Packages/HapiKit/Sources/HapiProtocol/Chat/ToolGroups.swift index aec4df45..75a337c5 100644 --- a/ios/Packages/HapiKit/Sources/HapiProtocol/Chat/ToolGroups.swift +++ b/ios/Packages/HapiKit/Sources/HapiProtocol/Chat/ToolGroups.swift @@ -299,23 +299,40 @@ private func getGroupingFamily(_ block: ToolCallBlock) -> ToolGroupBlock.Present return isCodexExplorationTool(block) ? .codexExploration : .default } -/// Port of `createToolGroupId` — reuses a previous group's id when the run -/// still shares a boundary tool with it (scroll-anchor stability). +/// Boundary-based identity reuse, with native list uniqueness guarantees: +/// splits may claim an old ID only once; fresh runs cannot steal a later +/// group's historical ID. No row-index or random IDs (scroll-anchor stability). private func createToolGroupId( tools: [ToolCallBlock], needsOlderHistory: Bool, - previousGroups: [ToolGroupBlock] + previousGroups: [ToolGroupBlock], + reservedGroupIds: Set, + usedIds: inout Set ) -> String { let firstToolId = tools.first?.id ?? "unknown" let lastToolId = tools.last?.id ?? firstToolId - if let previous = previousGroups.first(where: { $0.firstToolId == firstToolId || $0.lastToolId == lastToolId }) { + if let previous = previousGroups.first(where: { + ($0.firstToolId == firstToolId || $0.lastToolId == lastToolId) && !usedIds.contains($0.id) + }) { + usedIds.insert(previous.id) return previous.id } - return needsOlderHistory - ? "tool-group:\(lastToolId)" - : "tool-group:\(firstToolId)" + let boundaries = needsOlderHistory ? [lastToolId, firstToolId] : [firstToolId, lastToolId] + for boundary in boundaries { + let candidate = "tool-group:\(boundary)" + if !reservedGroupIds.contains(candidate), usedIds.insert(candidate).inserted { return candidate } + } + let base = "tool-group:\(boundaries[0])" + var suffix = 2 + var candidate = "\(base)#\(suffix)" + while reservedGroupIds.contains(candidate) || usedIds.contains(candidate) { + suffix += 1 + candidate = "\(base)#\(suffix)" + } + usedIds.insert(candidate) + return candidate } // MARK: - buildVisibleChatBlocks @@ -327,6 +344,9 @@ public func buildVisibleChatBlocks( ) -> [VisibleChatBlock] { var visibleBlocks: [VisibleChatBlock] = [] let previousGroups = options.previousGroups + let reservedGroupIds = Set(previousGroups.map(\.id)) + // Includes flattened tool children as well as ordinary display blocks. + var usedIds = Set(blocks.map(\.id)) var index = 0 while index < blocks.count { @@ -367,7 +387,8 @@ public func buildVisibleChatBlocks( return getInputStringAny(previousBlock.tool.input, ["title"]) }() visibleBlocks.append(.toolGroup(ToolGroupBlock( - id: createToolGroupId(tools: tools, needsOlderHistory: needsOlderHistory, previousGroups: previousGroups), + id: createToolGroupId(tools: tools, needsOlderHistory: needsOlderHistory, + previousGroups: previousGroups, reservedGroupIds: reservedGroupIds, usedIds: &usedIds), createdAt: tools[0].createdAt, invokedAt: tools[0].invokedAt, firstToolId: tools[0].id, diff --git a/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowLogic.swift b/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowLogic.swift index 4cd398f6..013cf7d4 100644 --- a/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowLogic.swift +++ b/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowLogic.swift @@ -255,7 +255,7 @@ public enum MessageWindowLogic { ?? (previous.viewMode == .history ? .prepend : .append) let effectiveLimit = regularLimit ?? (previous.viewMode == .history - ? MessageWindowConstants.historyWindowSize + ? previous.historyRetentionLimit : MessageWindowConstants.visibleWindowSize) let merged = MessageMerge.mergeMessages(previous.messages, retainedIncoming) let trim = trimPreservingQueued(merged, regularLimit: effectiveLimit, mode: effectiveMode) diff --git a/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowState.swift b/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowState.swift index ea49563b..93dd9926 100644 --- a/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowState.swift +++ b/ios/Packages/HapiKit/Sources/HapiProtocol/Window/MessageWindowState.swift @@ -58,6 +58,7 @@ public enum OlderLoadOutcome: Sendable { case invalidated case epochReset = "epoch-reset" case exhausted + case cursorDidNotAdvance = "cursor-did-not-advance" } } @@ -84,6 +85,8 @@ public struct MessageWindowState: Equatable, Sendable { public var isLoadingMore: Bool = false public var warning: String? public var viewMode: MessageViewMode = .tail + /// Local presentation policy; not persisted and not part of the wire. + public var historyRetentionLimit = MessageWindowConstants.historyWindowSize /// Bumped whenever the ``messages`` list instance changes. public var messagesVersion: Int = 0 /// Bumped per applied older page (scroll-anchoring handle). diff --git a/ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift b/ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift new file mode 100644 index 00000000..4b8bbb99 --- /dev/null +++ b/ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift @@ -0,0 +1,408 @@ +#if os(iOS) +import SwiftUI +import UIKit +import os + +public struct TranscriptViewport: Equatable { + public let followsTail: Bool + public let needsOlder: Bool + public let isAtBottom: Bool +} + +/// SwiftUI owns message content; UIKit owns scrolling, recycling and layout +/// transactions. In particular, a prepend never issues scrollToItem. +public struct AnchoredTranscriptList: UIViewControllerRepresentable where Item.ID == String { + public var items: [Item] + public var historyVersion: Int + public var jumpToken: Int + public var historyControlID: String + public var onViewport: (TranscriptViewport) -> Void + public var onLayout: (Int, Bool) -> Void + public var content: (Item) -> AnyView + + public init(items: [Item], historyVersion: Int, jumpToken: Int, historyControlID: String, + onViewport: @escaping (TranscriptViewport) -> Void, + onLayout: @escaping (Int, Bool) -> Void, + content: @escaping (Item) -> AnyView) { + self.items = items + self.historyVersion = historyVersion + self.jumpToken = jumpToken + self.historyControlID = historyControlID + self.onViewport = onViewport + self.onLayout = onLayout + self.content = content + } + + public func makeUIViewController(context: Context) -> TranscriptCollectionController { + TranscriptCollectionController() + } + + public func updateUIViewController(_ controller: TranscriptCollectionController, context: Context) { + controller.update(self) + } +} + +struct ReadingAnchor { + let id: String + let offset: CGFloat +} + +/// Stable-ID height estimates survive prepends and cell recycling. Unlike a +/// flow layout, offscreen measurements are not discarded on every snapshot. +/// Offset deltas participate in UIKit's layout, preserving an active pan/fling. +final class TranscriptLayout: UICollectionViewLayout { + var anchorForUpdate: ReadingAnchor? + var followsTail = true + private var ids: [String] = [] + private var indicesByID: [String: Int] = [:] + private var heights: [String: CGFloat] = [:] + private var frames: [CGRect] = [] + private var attributes: [UICollectionViewLayoutAttributes?] = [] + private var width: CGFloat = 0 + private var contentHeight: CGFloat = 0 + private var dirtyFromIndex: Int? = 0 + #if DEBUG + private(set) var attributeCreationCount = 0 + private(set) var visibleQueryProbeCount = 0 + #endif + + func setItems(_ ids: [String], width: CGFloat) { + let structureChanged = self.ids != ids + guard structureChanged || self.width != width else { return } + if abs(self.width - width) > 0.5 { heights.removeAll() } + self.width = width + if structureChanged { + self.ids = ids + indicesByID.removeAll(keepingCapacity: true) + for (index, id) in ids.enumerated() { indicesByID[id] = index } + heights = heights.filter { indicesByID[$0.key] != nil } + frames = Array(repeating: .zero, count: ids.count) + attributes = Array(repeating: nil, count: ids.count) + } + dirtyFromIndex = 0 + invalidateLayout() + } + + override func prepare() { + super.prepare() + guard let start = dirtyFromIndex else { return } + dirtyFromIndex = nil + var y: CGFloat = start == 0 ? 10 : frames[start - 1].maxY + 10 + for index in start.. [UICollectionViewLayoutAttributes]? { + guard !rect.isNull, !frames.isEmpty else { return [] } + // Positive, non-overlapping rows: binary-search the first possible + // intersection, then inspect only the viewport/prefetch range. + var lower = 0 + var upper = frames.count + while lower < upper { + let middle = (lower + upper) / 2 + #if DEBUG + visibleQueryProbeCount += 1 + #endif + if frames[middle].maxY < rect.minY { lower = middle + 1 } + else { upper = middle } + } + var result: [UICollectionViewLayoutAttributes] = [] + var index = lower + while index < frames.count { + #if DEBUG + visibleQueryProbeCount += 1 + #endif + let frame = frames[index] + if frame.minY > rect.maxY { break } + if frame.intersects(rect) { result.append(attributesForItem(at: index)) } + index += 1 + } + return result + } + + override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { + guard indexPath.section == 0, frames.indices.contains(indexPath.item) else { return nil } + return attributesForItem(at: indexPath.item) + } + + private func attributesForItem(at index: Int) -> UICollectionViewLayoutAttributes { + if let cached = attributes[index] { return cached } + let value = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: index, section: 0)) + value.frame = frames[index] + attributes[index] = value + #if DEBUG + attributeCreationCount += 1 + #endif + return value + } + + override func shouldInvalidateLayout(forPreferredLayoutAttributes preferredAttributes: UICollectionViewLayoutAttributes, + withOriginalAttributes originalAttributes: UICollectionViewLayoutAttributes) -> Bool { + abs(preferredAttributes.size.height - originalAttributes.size.height) > 0.5 + } + + override func invalidationContext(forPreferredLayoutAttributes preferredAttributes: UICollectionViewLayoutAttributes, + withOriginalAttributes originalAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutInvalidationContext { + let context = super.invalidationContext(forPreferredLayoutAttributes: preferredAttributes, + withOriginalAttributes: originalAttributes) + let index = originalAttributes.indexPath.item + guard ids.indices.contains(index), let view = collectionView else { return context } + let height = max(1, preferredAttributes.size.height) + let delta = height - (heights[ids[index]] ?? 100) + heights[ids[index]] = height + dirtyFromIndex = min(dirtyFromIndex ?? index, index) + context.invalidateItems(at: [preferredAttributes.indexPath]) + let viewportTop = view.contentOffset.y + view.adjustedContentInset.top + if followsTail || originalAttributes.frame.maxY <= viewportTop { + context.contentOffsetAdjustment.y += delta + } + return context + } + + override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint { + guard let view = collectionView else { return proposedContentOffset } + prepare() + if followsTail { return CGPoint(x: proposedContentOffset.x, y: bottomOffset(view)) } + if let anchor = anchorForUpdate, let index = indicesByID[anchor.id], + let frame = layoutAttributesForItem(at: IndexPath(item: index, section: 0))?.frame { + return CGPoint(x: proposedContentOffset.x, + y: frame.minY - anchor.offset - view.adjustedContentInset.top) + } + return proposedContentOffset + } + + func bottomOffset(_ view: UICollectionView) -> CGFloat { + max(-view.adjustedContentInset.top, + collectionViewContentSize.height - view.bounds.height + view.adjustedContentInset.bottom) + } + + func correctOffset(to y: CGFloat) { + guard let view = collectionView, abs(view.contentOffset.y - y) > 0.5 else { return } + let context = UICollectionViewLayoutInvalidationContext() + context.contentOffsetAdjustment.y = y - view.contentOffset.y + invalidateLayout(with: context) + } +} + +private final class TranscriptCollectionView: UICollectionView { + var accessibilityIntent: (() -> Void)? + override func accessibilityScroll(_ direction: UIAccessibilityScrollDirection) -> Bool { + accessibilityIntent?() + return super.accessibilityScroll(direction) + } +} + +public final class TranscriptCollectionController: UIViewController, UICollectionViewDelegate where Item.ID == String { + private let layout = TranscriptLayout() + private lazy var collection = TranscriptCollectionView(frame: .zero, collectionViewLayout: layout) + private var source: UICollectionViewDiffableDataSource! + private var configuration: AnchoredTranscriptList? + private var pending: AnchoredTranscriptList? + private var items: [Item] = [] + private var values: [String: Item] = [:] + private var applying = false + private var configuredWidth: CGFloat = 0 + private var renderedHistoryVersion = -1 + private var lastJumpToken = 0 + private var reportScheduled = false + private var lastViewport: TranscriptViewport? + private let logger = Logger(subsystem: "run.hapi", category: "Transcript") + + public override func loadView() { view = collection } + + public override func viewDidLoad() { + super.viewDidLoad() + collection.backgroundColor = .clear + collection.keyboardDismissMode = .interactive + collection.contentInsetAdjustmentBehavior = .never + collection.alwaysBounceVertical = true + collection.delegate = self + collection.accessibilityIdentifier = "chat-transcript" + collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "message") + collection.accessibilityIntent = { [weak self] in + self?.layout.followsTail = false + self?.reportViewport(deferred: false) + } + source = UICollectionViewDiffableDataSource(collectionView: collection) { [weak self] view, index, id in + let cell = view.dequeueReusableCell(withReuseIdentifier: "message", for: index) + guard let self, let item = self.values[id], let configuration = self.configuration else { return cell } + let width = max(1, view.bounds.width - 24) + cell.contentConfiguration = UIHostingConfiguration { + configuration.content(item) + .id(id) + .frame(width: width, alignment: .leading) + }.margins(.all, 0) + cell.backgroundColor = .clear + cell.accessibilityIdentifier = "chat-row-\(id)" + return cell + } + } + + public override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + guard collection.bounds.width > 24 else { return } + if abs(configuredWidth - collection.bounds.width) > 0.5, !applying { + if pending == nil { pending = configuration } + drain() + } else if layout.followsTail, !applying, !collection.isDragging { + layout.correctOffset(to: layout.bottomOffset(collection)) + } + reportViewport() + } + + func update(_ configuration: AnchoredTranscriptList) { + loadViewIfNeeded() + pending = configuration + // The first SwiftUI update may arrive before UIKit supplies bounds. + guard collection.bounds.width > 24 else { return } + drain() + } + + private func captureAnchor() -> ReadingAnchor? { + let top = collection.contentOffset.y + collection.adjustedContentInset.top + let bottom = collection.contentOffset.y + collection.bounds.height - collection.adjustedContentInset.bottom + for index in collection.indexPathsForVisibleItems.sorted() { + guard let id = source.itemIdentifier(for: index), id != configuration?.historyControlID, + let frame = layout.layoutAttributesForItem(at: index)?.frame, + frame.maxY > top, frame.minY < bottom else { continue } + return ReadingAnchor(id: id, offset: frame.minY - top) + } + return nil + } + + private func drain() { + guard !applying, var next = pending else { return } + pending = nil + // The producer must give distinct logical rows distinct IDs. As a + // final guard against repeated deliveries, retain the latest value + // at the first occurrence's position. Normalize ALL consumers, not + // only the dictionary: diffable snapshots also reject duplicate IDs. + var order: [String] = [] + var nextValues: [String: Item] = [:] + for item in next.items { + if nextValues[item.id] == nil { order.append(item.id) } + nextValues[item.id] = item + } + let duplicates = next.items.count - order.count + if duplicates > 0 { + logger.error("Coalesced \(duplicates) duplicate transcript row IDs") + next.items = order.compactMap { nextValues[$0] } + } + let oldIDs = Set(items.map(\.id)) + let oldHeight = layout.collectionViewContentSize.height + let widthChanged = abs(configuredWidth - collection.bounds.width) > 0.5 + // Hosting roots inherit a snapshot of SwiftUI's environment. Refresh + // only visible roots for context changes (theme, text size, basePath), + // in addition to rows whose value actually changed. + let visibleIDs = Set(collection.indexPathsForVisibleItems.compactMap { source.itemIdentifier(for: $0) }) + let changed = next.items.filter { widthChanged || values[$0.id] != $0 || visibleIDs.contains($0.id) }.map(\.id) + let structureChanged = items.map(\.id) != next.items.map(\.id) + let jump = next.jumpToken != lastJumpToken + lastJumpToken = next.jumpToken + if jump { layout.followsTail = true } + // Capture at COMMIT, after any reader motion during the request. + layout.anchorForUpdate = layout.followsTail ? nil : captureAnchor() + configuration = next + items = next.items + values = nextValues + configuredWidth = collection.bounds.width + guard structureChanged || !changed.isEmpty else { + if jump { layout.correctOffset(to: layout.bottomOffset(collection)) } + layout.anchorForUpdate = nil + acknowledgeLayout(version: next.historyVersion, madeProgress: false) + reportViewport() + return + } + applying = true + layout.setItems(items.map(\.id), width: configuredWidth) + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([0]) + snapshot.appendItems(items.map(\.id)) + snapshot.reconfigureItems(changed.filter { oldIDs.contains($0) }) + source.apply(snapshot, animatingDifferences: false) { [weak self] in + guard let self else { return } + self.collection.layoutIfNeeded() + let target = self.layout.targetContentOffset(forProposedContentOffset: self.collection.contentOffset) + self.layout.correctOffset(to: target.y) + self.layout.anchorForUpdate = nil + self.applying = false + let addedRows = self.items.contains { $0.id != next.historyControlID && !oldIDs.contains($0.id) } + let grew = self.layout.collectionViewContentSize.height > oldHeight + 1 + self.acknowledgeLayout(version: next.historyVersion, madeProgress: addedRows || grew) + self.reportViewport() + self.drain() + } + } + + private func acknowledgeLayout(version: Int, madeProgress: Bool) { + guard version != renderedHistoryVersion else { return } + renderedHistoryVersion = version + let callback = configuration?.onLayout + DispatchQueue.main.async { [weak self] in + self?.reportViewport(deferred: false) + callback?(version, madeProgress) + } + } + + private func reportViewport(deferred: Bool = true) { + if deferred { + guard !reportScheduled else { return } + reportScheduled = true + DispatchQueue.main.async { [weak self] in + self?.reportScheduled = false + self?.reportViewport(deferred: false) + } + return + } + guard !applying, collection.bounds.height > 0, configuration != nil else { return } + let height = collection.bounds.height - collection.adjustedContentInset.top - collection.adjustedContentInset.bottom + let top = max(0, collection.contentOffset.y + collection.adjustedContentInset.top) + let bottomDistance = layout.bottomOffset(collection) - collection.contentOffset.y + let short = layout.collectionViewContentSize.height <= height + 1 + let viewport = TranscriptViewport( + followsTail: layout.followsTail, + needsOlder: short || (!layout.followsTail && top <= height), + isAtBottom: bottomDistance <= 1 + ) + guard viewport != lastViewport else { return } + lastViewport = viewport + configuration?.onViewport(viewport) + } + + public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { + layout.followsTail = false + layout.anchorForUpdate = nil + reportViewport(deferred: false) + } + + public func scrollViewDidScroll(_ scrollView: UIScrollView) { + if !applying, (collection.isDragging || collection.isDecelerating) { + layout.followsTail = layout.bottomOffset(collection) - collection.contentOffset.y <= 1 + } + reportViewport(deferred: !(collection.isDragging || collection.isDecelerating)) + } + + public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { + if !decelerate { finishGesture() } + } + + public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { finishGesture() } + + private func finishGesture() { + layout.followsTail = layout.bottomOffset(collection) - collection.contentOffset.y <= 1 + reportViewport(deferred: false) + } +} +#endif diff --git a/ios/Packages/HapiKit/Sources/HapiUI/Markdown/MarkdownRenderCache.swift b/ios/Packages/HapiKit/Sources/HapiUI/Markdown/MarkdownRenderCache.swift new file mode 100644 index 00000000..548225ac --- /dev/null +++ b/ios/Packages/HapiKit/Sources/HapiUI/Markdown/MarkdownRenderCache.swift @@ -0,0 +1,100 @@ +import Foundation +import SwiftUI + +/// Per-chat bounded cache. Synchronous lookup avoids an actor-hop placeholder +/// when a cell is recycled; preparation runs off the main thread. +public final class MarkdownRenderCache: @unchecked Sendable { + private struct Entry { + let blocks: [MarkdownBlockNode] + let cost: Int + var accessed: Int + } + private let lock = NSLock() + private var entries: [String: Entry] = [:] + private var clock = 0 + private var totalCost = 0 + private let maxCost: Int + + public init(maxCost: Int = 8 * 1024 * 1024) { self.maxCost = maxCost } + + public func cached(_ text: String) -> [MarkdownBlockNode]? { + lock.withLock { + guard var entry = entries[text] else { return nil } + clock += 1 + entry.accessed = clock + entries[text] = entry + return entry.blocks + } + } + + public func prepare(_ sources: [String]) async { + let task = Task.detached(priority: .userInitiated) { [self] in + for text in Set(sources) where cached(text) == nil { + guard !Task.isCancelled else { return } + guard text.utf8.count * 4 <= maxCost else { continue } + let blocks = MarkdownBlockTree.build(from: text) + lock.withLock { + guard entries[text] == nil else { return } + clock += 1 + let cost = max(1, text.utf8.count * 4) + guard cost <= maxCost else { return } + entries[text] = Entry(blocks: blocks, cost: cost, accessed: clock) + totalCost += cost + while totalCost > maxCost || entries.count > 800 { + guard let oldest = entries.min(by: { $0.value.accessed < $1.value.accessed }) else { break } + totalCost -= oldest.value.cost + entries.removeValue(forKey: oldest.key) + } + } + } + } + await withTaskCancellationHandler { + await task.value + } onCancel: { + task.cancel() + } + } +} + +private struct MarkdownRenderCacheKey: EnvironmentKey { + static let defaultValue: MarkdownRenderCache? = nil +} + +public extension EnvironmentValues { + var hapiMarkdownCache: MarkdownRenderCache? { + get { self[MarkdownRenderCacheKey.self] } + set { self[MarkdownRenderCacheKey.self] = newValue } + } +} + +/// Normally prewarmed before publishing the chat snapshot. An on-demand +/// miss keeps previous content until the new revision has been parsed. +public struct CachedMarkdownView: View { + public let markdown: String + @Environment(\.hapiMarkdownCache) private var cache + @State private var prepared: [MarkdownBlockNode]? + + public init(markdown: String) { self.markdown = markdown } + + public var body: some View { + Group { + if let cache { + if let blocks = cache.cached(markdown) ?? prepared { MarkdownView(blocks: blocks) } + else { Text(markdown).textSelection(.enabled) } + } else { + MarkdownView(markdown: markdown) + } + } + .task(id: markdown) { + guard let cache else { return } + await cache.prepare([markdown]) + guard !Task.isCancelled else { return } + if let blocks = cache.cached(markdown) { prepared = blocks } + else { + let source = markdown + let blocks = await Task.detached { MarkdownBlockTree.build(from: source) }.value + if !Task.isCancelled { prepared = blocks } + } + } + } +} diff --git a/ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatHistoryPagingStateTests.swift b/ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatHistoryPagingStateTests.swift new file mode 100644 index 00000000..6c666db5 --- /dev/null +++ b/ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatHistoryPagingStateTests.swift @@ -0,0 +1,83 @@ +import HapiClient +import HapiProtocol +import Testing + +@Suite("Chat history paging") +struct ChatHistoryPagingStateTests { + @Test func exhaustionCanReviveButPausedNeedsExplicitRetry() { + var state = ChatHistoryPagingState() + let first = state.begin()! + state.received(.stopped(.exhausted), generation: first) + state.refreshAvailability(hasMore: true) + #expect(state.phase == .idle) + let second = state.begin()! + state.received(.stopped(.cursorDidNotAdvance), generation: second) + state.refreshAvailability(hasMore: true) + #expect(state.phase == .paused) + } + @Test func pagesWaitForTheirLayoutAndDoNotNeedANewGesture() { + var state = ChatHistoryPagingState() + let first = state.begin()! + let duplicateRequestRejected = state.begin() == nil + #expect(duplicateRequestRejected) + state.received(.applied(historyVersion: 3, hasMore: true, addedRenderableCount: 2), generation: first) + let requestDuringLayoutRejected = state.begin() == nil + #expect(requestDuringLayoutRejected) + let staleLayoutIgnored = !state.laidOut(historyVersion: 2, madeProgress: true) + #expect(staleLayoutIgnored) + let layoutAccepted = state.laidOut(historyVersion: 3, madeProgress: true) + #expect(layoutAccepted) + let nextRequestStarted = state.begin() != nil + #expect(nextRequestStarted) + } + + @Test func retriesAreBoundedAndExplicitRetryResumes() { + struct Failure: Error {} + var state = ChatHistoryPagingState() + for delay in [500, 1500] { + let request = state.begin()! + let delayMatches = state.received(.failed(Failure()), generation: request) == delay + #expect(delayMatches) + let requestDuringBackoffRejected = state.begin() == nil + #expect(requestDuringBackoffRejected) + let retryAccepted = state.retryElapsed(generation: request) + #expect(retryAccepted) + } + let request = state.begin()! + let noAutomaticRetry = state.received(.failed(Failure()), generation: request) == nil + #expect(noAutomaticRetry) + #expect(state.phase == .failed) + state.resume() + let manualRetryStarted = state.begin() != nil + #expect(manualRetryStarted) + } + + @Test func nonRenderingPagesPauseWithoutClaimingExhaustion() { + var state = ChatHistoryPagingState() + for version in 1...3 { + let request = state.begin()! + state.received(.applied(historyVersion: version, hasMore: true, addedRenderableCount: 0), generation: request) + let layoutAccepted = state.laidOut(historyVersion: version, madeProgress: false) + #expect(layoutAccepted) + } + #expect(state.phase == .paused) + state.resume() + let explicitContinueStarted = state.begin() != nil + #expect(explicitContinueStarted) + } + + @Test func cancellationRejectsOldResponsesAndTimers() { + var state = ChatHistoryPagingState() + let old = state.begin()! + state.cancel() + let current = state.begin()! + state.received(.applied(historyVersion: 99, hasMore: true, addedRenderableCount: 1), generation: old) + let staleTimerIgnored = !state.retryElapsed(generation: old) + #expect(staleTimerIgnored) + #expect(state.generation == current) + #expect(state.phase == .loading) + let gate = ChatHistoryRequestGate() + gate.invalidate() + #expect(!gate.allowsApply) + } +} diff --git a/ios/Packages/HapiKit/Tests/HapiClientTests/MessageWindowControllerTests.swift b/ios/Packages/HapiKit/Tests/HapiClientTests/MessageWindowControllerTests.swift index 198d76b4..0e4ef84f 100644 --- a/ios/Packages/HapiKit/Tests/HapiClientTests/MessageWindowControllerTests.swift +++ b/ios/Packages/HapiKit/Tests/HapiClientTests/MessageWindowControllerTests.swift @@ -81,6 +81,152 @@ struct MessageWindowControllerTests { // MARK: - Tail sync + @Test func cancellingHistoryDoesNotPublishAnOfflineWarning() async { + let provider = GatedMessagesProvider() + var initial = MessageWindowState(sessionId: "history") + initial.hasMore = true + initial.epoch = 1 + initial.oldestPosition = MessagePosition(at: 1000, seq: 1) + let controller = MessageWindowController(sessionId: "history", provider: provider, initialState: initial) + let task = Task { await controller.fetchOlder() } + await provider.waitForRequests(1) + task.cancel() + await provider.failPending(CancellationError()) + guard case .stopped(.invalidated) = await task.value else { + Issue.record("Cancellation must not appear as a failed network request") + return + } + let state = await controller.state + #expect(!state.isLoadingMore) + #expect(state.warning == nil) + #expect(state.hasMore) + } + + @Test func nativeHistoryRetainsThePrependBudgetAcrossSSEAndReset() async throws { + var initial = MessageWindowState(sessionId: "history") + initial.messages = (1...800).map { WindowMessage(wire: agentRow(id: "a-\($0)", seq: $0, at: $0 * 1000)) } + initial.epoch = 1 + initial.viewMode = .history + let provider = GatedMessagesProvider() + let controller = MessageWindowController(sessionId: "history", provider: provider, + initialState: initial, historyRetentionLimit: 800) + await controller.ingestSSEMessages([WindowMessage(wire: agentRow(id: "new", seq: 801, at: 801000))]) + let history = await controller.state + #expect(history.messages.count == 800) + #expect(history.messages.first?.id == "a-1") + #expect(history.requiresLatestReset) + await provider.release(latestPage([agentRow(id: "new", seq: 801, at: 801000)], epoch: 1)) + try await controller.reconcileQueuedState() + let afterReconnect = await controller.state + #expect(await provider.requests == [.latest(limit: 200)]) + #expect(afterReconnect.messages == history.messages) + #expect(afterReconnect.oldestPosition == history.oldestPosition) + #expect(afterReconnect.newestPosition == history.newestPosition) + #expect(afterReconnect.hasMore == history.hasMore) + #expect(afterReconnect.historyVersion == history.historyVersion) + #expect(afterReconnect.syncGeneration == history.syncGeneration + 1) + #expect(afterReconnect.requiresLatestReset) + #expect(!afterReconnect.isSyncingTail) + await controller.setViewMode(.tail) + let tail = await controller.state + #expect(tail.messages.count == 400) + #expect(tail.epoch == nil) + await controller.clear() + let cleared = await controller.state + #expect(cleared.historyRetentionLimit == 800) + } + + @Test(arguments: [false, true]) + func gapRecoveryReplacesTruncatedHistoryOnEpochChangeOrExplicitReset(reset: Bool) async throws { + let provider = GatedMessagesProvider() + var initial = MessageWindowState(sessionId: "history") + initial.messages = [WindowMessage(wire: agentRow(id: "obsolete", seq: 1, at: 1000))] + initial.epoch = 1 + initial.viewMode = .history + initial.requiresLatestReset = true + initial.hasMore = true + initial.oldestPosition = MessagePosition(at: 1000, seq: 1) + initial.newestPosition = initial.oldestPosition + let controller = MessageWindowController(sessionId: "history", provider: provider, initialState: initial) + let epoch = reset ? 1 : 2 + // Empty authoritative latest also covers deleted history. + await provider.release(latestPage([], epoch: epoch, reset: reset)) + try await controller.reconcileQueuedState() + #expect(await provider.requests.count == 1) + let state = await controller.state + #expect(state.epoch == epoch) + #expect(state.messages.isEmpty) + #expect(!state.requiresLatestReset) + #expect(!state.hasMore) + } + + @Test func gapRecoveryDrainsAFreshEpochCheckDuringTruncatedHistorySync() async throws { + let provider = GatedMessagesProvider() + var initial = MessageWindowState(sessionId: "history") + initial.messages = [WindowMessage(wire: agentRow(id: "obsolete", seq: 1, at: 1000))] + initial.epoch = 1 + initial.viewMode = .history + initial.requiresLatestReset = true + let controller = MessageWindowController(sessionId: "history", provider: provider, initialState: initial) + let first = Task { await controller.syncTail() } + await provider.waitForRequests(1) + let recovery = Task { try await controller.reconcileQueuedState() } + // Let recovery join the parked run before releasing its old snapshot. + try await Task.sleep(for: .milliseconds(20)) + await provider.release(latestPage([], epoch: 1)) + await first.value + await provider.waitForRequests(2) + #expect(await provider.requests == [.latest(limit: 200), .latest(limit: 200)]) + #expect(await controller.state.messages.map(\.id) == ["obsolete"]) + await provider.release(latestPage([agentRow(id: "replacement", seq: 2, at: 2000)], epoch: 2)) + try await recovery.value + #expect(await controller.state.epoch == 2) + #expect(await controller.state.messages.map(\.id) == ["replacement"]) + } + + @Test func stalledOlderCursorDoesNotApplyOrPretendHistoryIsExhausted() async { + let provider = GatedMessagesProvider() + var initial = MessageWindowState(sessionId: "history") + initial.hasMore = true + initial.epoch = 1 + initial.oldestPosition = MessagePosition(at: 1000, seq: 1) + await provider.release(MessagesResponse(messages: [], page: MessagesPage( + direction: .before, limit: 200, epoch: 1, reset: false, + nextBeforeSeq: 1, nextBeforeAt: 1000, nextAfterSeq: nil, nextAfterAt: nil, + snapshotHeadSeq: nil, snapshotHeadAt: nil, hasMore: true + ))) + let controller = MessageWindowController(sessionId: "history", provider: provider, initialState: initial) + let outcome = await controller.fetchOlder() + guard case .stopped(.cursorDidNotAdvance) = outcome else { Issue.record("Expected a stalled cursor"); return } + let state = await controller.state + #expect(state.hasMore) + #expect(!state.isLoadingMore) + #expect(state.historyVersion == 0) + } + + @Test func readerReversalVetoesAnInflightPrepend() async { + let provider = GatedMessagesProvider() + var initial = MessageWindowState(sessionId: "history") + initial.hasMore = true + initial.epoch = 1 + initial.oldestPosition = MessagePosition(at: 1000, seq: 1) + let controller = MessageWindowController(sessionId: "history", provider: provider, initialState: initial) + let gate = ChatHistoryRequestGate() + let request = Task { await controller.fetchOlder(onBeforeApply: { _ in gate.allowsApply }) } + await provider.waitForRequests(1) + gate.invalidate() + await provider.release(MessagesResponse(messages: [agentRow(id: "older", seq: 0, at: 0)], page: MessagesPage( + direction: .before, limit: 200, epoch: 1, reset: false, + nextBeforeSeq: 0, nextBeforeAt: 0, nextAfterSeq: nil, nextAfterAt: nil, + snapshotHeadSeq: nil, snapshotHeadAt: nil, hasMore: false + ))) + let outcome = await request.value + guard case .stopped(.invalidated) = outcome else { Issue.record("Stale page was applied"); return } + let state = await controller.state + #expect(state.messages.isEmpty) + #expect(state.historyVersion == 0) + } + @Test func concurrentSyncTailCallsCoalesceIntoASingleRun() async { let provider = GatedMessagesProvider() let controller = MessageWindowController(sessionId: "s", provider: provider) @@ -334,6 +480,10 @@ actor GatedMessagesProvider: MessagesProviding { } } + func failPending(_ error: any Error) { + pending.removeFirst().resume(throwing: error) + } + /// Suspends until at least `threshold` requests have been issued. func waitForRequests(_ threshold: Int) async { if requests.count >= threshold { return } diff --git a/ios/Packages/HapiKit/Tests/HapiProtocolTests/ToolGroupIdentityTests.swift b/ios/Packages/HapiKit/Tests/HapiProtocolTests/ToolGroupIdentityTests.swift new file mode 100644 index 00000000..b4d1f322 --- /dev/null +++ b/ios/Packages/HapiKit/Tests/HapiProtocolTests/ToolGroupIdentityTests.swift @@ -0,0 +1,73 @@ +import Testing +@testable import HapiProtocol + +@Suite("Native tool group identity") +struct ToolGroupIdentityTests { + private func tool(_ id: String) -> ChatBlock { + .toolCall(ToolCallBlock( + id: id, localId: nil, createdAt: 0, invokedAt: nil, + durationMs: nil, usage: nil, model: nil, + tool: ChatToolCall(id: id, name: "Read", state: .completed, createdAt: 0), + children: [], meta: nil + )) + } + + private var separator: ChatBlock { + .agentText(AgentTextBlock( + id: "text", localId: nil, createdAt: 0, invokedAt: nil, + durationMs: nil, usage: nil, model: nil, text: "Between tool runs", meta: nil + )) + } + + private func groups(_ blocks: [ChatBlock], more: Bool = false, + previous: [ToolGroupBlock] = []) -> [ToolGroupBlock] { + buildVisibleChatBlocks(blocks, options: .init(hasMoreMessages: more, previousGroups: previous)) + .compactMap { if case .toolGroup(let group) = $0 { group } else { nil } } + } + + @Test(arguments: [false, true]) + func splittingAGroupCannotReuseItsIdentityTwice(_ more: Bool) { + let tools = ["a", "b", "c", "d"].map(tool) + let previous = groups(tools, more: more) + let split = [tools[0], tools[1], separator, tools[2], tools[3]] + let next = groups(split, more: more, previous: previous) + #expect(next.count == 2) + #expect(Set(next.map(\.id)).count == 2) + #expect(next.filter { $0.id == previous[0].id }.count == 1) + #expect(next.flatMap(\.tools).map(\.id) == ["a", "b", "c", "d"]) + #expect(groups(split, more: more, previous: next).map(\.id) == next.map(\.id)) + } + + @Test func aFallbackCannotCollideWithAnAlreadyClaimedHistoricalId() { + let original = groups([tool("c"), tool("d")]) // tool-group:c + let grown = groups(["a", "b", "c", "d"].map(tool), previous: original) + let split = [tool("a"), tool("b"), separator, tool("c"), tool("d")] + let next = groups(split, previous: grown) + #expect(next[0].id == original[0].id) + #expect(next[1].id != next[0].id) + #expect(groups(split, previous: next).map(\.id) == next.map(\.id)) + } + + @Test func prependingANewGroupCannotStealALaterGroupsHistoricalId() { + let original = groups(["c", "d", "e"].map(tool)) // tool-group:c + let trimmed = groups([tool("d"), tool("e")], previous: original) + let prepended = [tool("a"), tool("b"), tool("c"), separator, tool("d"), tool("e")] + let next = groups(prepended, more: true, previous: trimmed) + #expect(next.count == 2) + #expect(next[0].id != next[1].id) + #expect(next[1].id == original[0].id) + #expect(groups(prepended, more: true, previous: next).map(\.id) == next.map(\.id)) + } + + @Test func bothBoundaryCollisionsUseAStableSuffixWithoutDroppingBlocks() { + guard case .agentText(var left) = separator, case .agentText(var right) = separator else { return } + left.id = "tool-group:a" + right.id = "tool-group:b" + let blocks: [ChatBlock] = [.agentText(left), tool("a"), tool("b"), .agentText(right)] + let visible = buildVisibleChatBlocks(blocks, options: .init(hasMoreMessages: false)) + let next = groups(blocks) + #expect(visible.count == 3) + #expect(next[0].id == "tool-group:a#2") + #expect(groups(blocks, previous: next).map(\.id) == next.map(\.id)) + } +} diff --git a/ios/Packages/HapiKit/Tests/HapiUITests/MarkdownRenderCacheTests.swift b/ios/Packages/HapiKit/Tests/HapiUITests/MarkdownRenderCacheTests.swift new file mode 100644 index 00000000..46d33b9f --- /dev/null +++ b/ios/Packages/HapiKit/Tests/HapiUITests/MarkdownRenderCacheTests.swift @@ -0,0 +1,24 @@ +import HapiUI +import Testing + +@Suite("Chat markdown cache") +struct MarkdownRenderCacheTests { + @Test func prewarmingMatchesTheExistingRenderer() async { + let cache = MarkdownRenderCache() + let text = "## History\n\n```swift\nlet stable = true\n```" + await cache.prepare([text]) + #expect(cache.cached(text) == MarkdownBlockTree.build(from: text)) + await cache.prepare([text]) + #expect(cache.cached(text) != nil) + } + + @Test func retentionIsBoundedAndHugeEntriesDoNotEvictNormalOnes() async { + let cache = MarkdownRenderCache(maxCost: 32) + await cache.prepare(["one"]) + await cache.prepare([String(repeating: "x", count: 100)]) + #expect(cache.cached("one") != nil) + await cache.prepare(["second"]) + #expect(cache.cached("one") == nil) + #expect(cache.cached("second") != nil) + } +} diff --git a/ios/README.md b/ios/README.md index 04f0053f..979af895 100644 --- a/ios/README.md +++ b/ios/README.md @@ -16,6 +16,9 @@ it shares only the protocol contract (`docs/api/`) and the golden fixtures ## Build +Native chat scrolling architecture and acceptance checklist: +[Native transcript scrolling](../docs/native-chat-scrolling.md). + Open `ios/Hapi.xcodeproj` in Xcode and run the shared `Hapi` scheme, or from the command line: @@ -106,9 +109,9 @@ ios/ ChatPipeline off-main, ~100 ms coalesced, last-seen stamping, header cascade; ChatView — bottom-anchored - ScrollView/LazyVStack with auto-stick, - new-messages pill, top sentinel paging - with scroll re-anchoring, degraded + UICollectionView with cached heights, + ID/offset anchoring, viewport paging, + explicit return-to-latest, degraded banners; Blocks/ — user bubble, agent markdown, reasoning, tool cards with per-tool bodies + knownTools-parity diff --git a/ios/scripts/test-transcript.sh b/ios/scripts/test-transcript.sh new file mode 100755 index 00000000..aefc73f2 --- /dev/null +++ b/ios/scripts/test-transcript.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Isolated app-hosted UIKit layout tests; never installs into a user's device. +set -euo pipefail +cd "$(dirname "$0")/../.." + +runtime=$(xcrun simctl list runtimes -j | python3 -c ' +import json, sys +items = [x for x in json.load(sys.stdin)["runtimes"] if x.get("isAvailable") and x.get("name", "").startswith("iOS")] +print(max(items, key=lambda x: tuple(map(int, x["version"].split("."))))["identifier"]) +') +device=$(xcrun simctl create "HAPI Transcript Tests" com.apple.CoreSimulator.SimDeviceType.iPhone-15 "$runtime") +trap 'xcrun simctl delete "$device" >/dev/null 2>&1 || true' EXIT + +xcodebuild test -project ios/Hapi.xcodeproj -scheme Hapi \ + -destination "platform=iOS Simulator,id=$device" \ + -derivedDataPath "${HAPI_TEST_DERIVED_DATA:-/tmp/hapi-transcript-tests}" \ + -parallel-testing-enabled NO CODE_SIGNING_ALLOWED=NO "$@"