openassistant: a rendered UI card can no longer leak into a later reply
- Commit
fc4f7cdd25323f908470475a5cfe9ffaddc050fa- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 04:13 UTC (5 days ago)
- Parents
ee4300e
The A2UI render stash had turn-scoped meaning but was only consumed on the single happy path (Finished). A turn that rendered a card and then halted, failed, or ended TooLong/Unavailable left the stash in the shared tools object, so the NEXT reply โ possibly in a different conversation โ silently gained the dead card's fence. A confirmation flow leaked the same way: a draft composer's card resurfaced on the post-confirmation reply underneath the confirm card. MamlChatEngine (the stash's only consumer) now discards via consumePendingRenderUi() on every non-Finished outcome โ Unavailable, TooLong, Halted, Failed and PendingConfirmation (the confirm card replaces any rendered card). Finished still consumes and attaches exactly once, as the existing tests require. Two fake-model regression tests cover the halted turn and the pending-confirmation flow. History fitting, the tool loop, web tools and settings were audited and cleared; library/ml's 1 GiB Vulkan split is untouched. Verified: :openassistant:testDevUnitTest and lint BUILD SUCCESSFUL.
Diff
openassistant/src/main/java/com/vayunmathur/openassistant/platform/MamlChatEngine.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/platform/MamlChatEngine.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/platform/MamlChatEngine.ktindex 0f6440d05e..bc1922b5be 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/platform/MamlChatEngine.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/platform/MamlChatEngine.kt@@ -158,7 +158,15 @@ open class MamlChatEngine(private val modelDir: File) : AutoCloseable { open suspend fun chat(request: ChatRequest, tools: AssistantTools, onText: (String) -> Boolean): ChatResult = mutex.withLock {- val model = withContext(engineThread) { model() } ?: return@withLock ChatResult.Unavailable+ // A stashed render_ui card belongs to the turn that produced it: the loop+ // below either consumes it into that turn's reply (Finished) or discards it+ // on every other outcome. Discarding here too keeps a card stashed outside+ // a finishing turn (e.g. by a confirmed tool whose follow-up turn then+ // cannot run) from being attached to an unrelated later reply.+ val model = withContext(engineThread) { model() } ?: run {+ tools.consumePendingRenderUi()+ return@withLock ChatResult.Unavailable+ } val declarations = tools.declarations // History turns are built on the engine thread too: encoding a // kept photo runs the vision tower, which is not thread-safe.@@ -172,7 +180,10 @@ open class MamlChatEngine(private val modelDir: File) : AutoCloseable { fitConversation(history, current, { it.role == Gemma4Handle.Role.USER }) { model.positionsFor(it, request.system, declarations, CHAT_REPLY_LIMIT) <= ceiling }- } ?: return@withLock ChatResult.TooLong+ } ?: run {+ tools.consumePendingRenderUi()+ return@withLock ChatResult.TooLong+ } if (conversation.size < history.size + 1) { Log.status(TAG, "dropped ${history.size + 1 - conversation.size} oldest turns to fit") }@@ -214,13 +225,24 @@ open class MamlChatEngine(private val modelDir: File) : AutoCloseable { val text = if (pendingUi != null) withA2uiBlock(outcome.text, pendingUi) else outcome.text ChatResult.Reply(text, halted = false) }- is ToolLoopOutcome.Halted -> ChatResult.Reply(outcome.text, halted = true)- is ToolLoopOutcome.Failed -> ChatResult.Unavailable- is ToolLoopOutcome.PendingConfirmation -> ChatResult.Pending(- pendingId = outcome.pendingId,- summary = tools.pendingById(outcome.pendingId)?.summary ?: outcome.callName,- text = outcome.text,- )+ is ToolLoopOutcome.Halted -> {+ tools.consumePendingRenderUi()+ ChatResult.Reply(outcome.text, halted = true)+ }+ is ToolLoopOutcome.Failed -> {+ tools.consumePendingRenderUi()+ ChatResult.Unavailable+ }+ is ToolLoopOutcome.PendingConfirmation -> {+ // The confirmation card replaces any card this turn rendered;+ // keeping the stash would attach it to the follow-up reply.+ tools.consumePendingRenderUi()+ ChatResult.Pending(+ pendingId = outcome.pendingId,+ summary = tools.pendingById(outcome.pendingId)?.summary ?: outcome.callName,+ text = outcome.text,+ )+ } } }
openassistant/src/test/java/com/vayunmathur/openassistant/platform/MamlChatEngineFakeModelTest.kt
diff --git a/openassistant/src/test/java/com/vayunmathur/openassistant/platform/MamlChatEngineFakeModelTest.kt b/openassistant/src/test/java/com/vayunmathur/openassistant/platform/MamlChatEngineFakeModelTest.ktindex e639500022..d9aa5840a9 100644--- a/openassistant/src/test/java/com/vayunmathur/openassistant/platform/MamlChatEngineFakeModelTest.kt+++ b/openassistant/src/test/java/com/vayunmathur/openassistant/platform/MamlChatEngineFakeModelTest.kt@@ -137,6 +137,51 @@ class MamlChatEngineFakeModelTest { e.close() } + @Test fun haltedTurnDiscardsRenderedUiSoNextReplyHasNoStaleCard() = runBlocking {+ var calls = 0+ val uiJson = """{"components":[{"id":"root","component":"Text","text":"Pick one"}]}"""+ val quoted = "<|\"|>" + uiJson + "<|\"|>"+ val fake = FakeTextModel(script = { _, onToken ->+ calls+++ if (calls == 1) {+ "<|tool_call>call:render_ui{components_json:$quoted}<tool_call|>"+ } else {+ onToken("here you go"); "here you go"+ }+ })+ val e = engine(fake)+ val toolSet = tools()+ val halted = e.chat(ChatRequest("sys", emptyList(), "show a card"), toolSet) { false }+ assertTrue(halted is ChatResult.Reply && halted.halted, "$halted")+ assertEquals(null, toolSet.pendingRenderUi)+ fake.script = { _, onToken -> onToken("plain answer"); "plain answer" }+ val next = e.chat(ChatRequest("sys", emptyList(), "thanks"), toolSet) { true }+ assertEquals(ChatResult.Reply("plain answer", halted = false), next)+ e.close()+ }++ @Test fun pendingConfirmationDiscardsRenderedUiSoFollowUpHasNoStaleCard() = runBlocking {+ var calls = 0+ val quoted = { s: String -> "<|\"|>" + s + "<|\"|>" }+ val fake = FakeTextModel(script = { _, onToken ->+ calls+++ when (calls) {+ 1 -> "<|tool_call>call:draft_message{address:${quoted("+15551234")},line:${quoted("Sim")},body:${quoted("Running late")}}<tool_call|>"+ 2 -> "<|tool_call>call:send_message_v2{address:${quoted("+15551234")},line:${quoted("Sim")},body:${quoted("Running late")}}<tool_call|>"+ else -> { onToken("ok"); "ok" }+ }+ })+ val e = engine(fake)+ val toolSet = tools()+ val result = e.chat(ChatRequest("sys", emptyList(), "text Ann I'm late"), toolSet) { true }+ assertTrue(result is ChatResult.Pending, "$result")+ assertEquals(null, toolSet.pendingRenderUi)+ fake.script = { _, onToken -> onToken("sent"); "sent" }+ val next = e.chat(ChatRequest("sys", emptyList(), "thanks"), toolSet) { true }+ assertEquals(ChatResult.Reply("sent", halted = false), next)+ e.close()+ }+ @Test fun sendMessageScriptGatesAndCancelNeverExecutes() = runBlocking { var calls = 0 val quoted = { s: String -> "<|\"|>" + s + "<|\"|>" }