feat(android): render chips from the span record, not the mark scan

The phone was the last surface reading chips as paired U+FFFC marks
in the text: a web user's staged paste rendered as its raw payload
inline, and the mark scan stayed the only parse. Now the row's chip
record does the work. `user_message`'s `pastes` spans parse beside the
text (the session annotation's own shape, absent on every row that
predates it), the controller carries them through the fold's dedupe
guard, and the render slices each chip out of the plain text at the
record's UTF-8 offsets — one walk maps every character boundary, so
a surrogate-heavy draft still lands exactly and a span that splits a
character skips whole rather than cutting one in half. A row without
a record still scans the mark form, exactly as before.

The composer still sends plain text only — this is the read side; the
phone never staged text chips even in the mark era.

Verified as CI does (assembleDebug, lintDebug) plus a JVM harness
running the verbatim slicing code against the daemon's own e2e record
numbers, surrogates and seams included.
This commit is contained in:
vhaudiquet committed 2026-11-20 01:18:49 +01:00
1 parent c5bf168eed
commit 6abb1436b5
4 files changed
+87 -17

No files matched your search

+5 -2
View File
@@ -77,8 +77,11 @@ manages the device fleet the claw keeps.
**subscribe first, then POST**, the reply arrives on the stream. Thinking **subscribe first, then POST**, the reply arrives on the stream. Thinking
deltas render dim and collapsible, tool rows show name/summary with error deltas render dim and collapsible, tool rows show name/summary with error
coloring (bash-like tools render as shell lines instead: dim `$` prefix, coloring (bash-like tools render as shell lines instead: dim `$` prefix,
the command line, and an exit-code badge), pasted payloads embedded in a the command line, and an exit-code badge), a message's paste chips render
message as paired `U+FFFC` marks render as compact `▣N 5L` chips, and as compact `▣N 5L` labels sliced from the span record its
`user_message` carries beside the plain text (rows recorded before
spans rode the stream still scan the legacy paired-`U+FFFC` form in
the text), and
`attachment` events render as cards: file name, size, caption, an inline `attachment` events render as cards: file name, size, caption, an inline
preview for small (≤ 2 MB) images, and a download-and-open row (bytes preview for small (≤ 2 MB) images, and a download-and-open row (bytes
from `GET /v1/attachments/{id}` land in the app cache and open through a from `GET /v1/attachments/{id}` land in the app cache and open through a
@@ -11,6 +11,7 @@ import rs.rhema.phone.net.ClawApi
import rs.rhema.phone.net.ClawEvent import rs.rhema.phone.net.ClawEvent
import rs.rhema.phone.net.ClawResult import rs.rhema.phone.net.ClawResult
import rs.rhema.phone.net.OutboundImage import rs.rhema.phone.net.OutboundImage
import rs.rhema.phone.net.PasteSpan
import rs.rhema.phone.net.SseFrame import rs.rhema.phone.net.SseFrame
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -61,6 +62,10 @@ class ChatController(
override val id: Long, override val id: Long,
val text: String, val text: String,
val images: List<SentImage> = emptyList(), val images: List<SentImage> = emptyList(),
// The chip record this surface's rows never carry (its
// composer sends plain text) but a replayed row — a web user's
// chips in this conversation — does; the render slices on it.
val pastes: List<PasteSpan> = emptyList(),
) : Item ) : Item
data class Thinking(override val id: Long, val text: String) : Item data class Thinking(override val id: Long, val text: String) : Item
data class ToolCall( data class ToolCall(
@@ -355,14 +360,15 @@ class ChatController(
is ClawEvent.UserMessage -> { is ClawEvent.UserMessage -> {
// The recorded question — replayed on reload, echoed live // The recorded question — replayed on reload, echoed live
// for a message this surface just sent. An identical last // for a message this surface just sent. An identical last
// row — same text, same image count — is that echo's // row — same text, same image count, same chip record —
// stand-in; skip it, previews and all. // is that echo's stand-in; skip it, previews and all.
val last = list.lastOrNull() val last = list.lastOrNull()
if (!(last is UserMessage && last.text == event.text && last.images.size == event.images.size)) { if (!(last is UserMessage && last.text == event.text && last.images.size == event.images.size && last.pastes == event.pastes)) {
list = list + UserMessage( list = list + UserMessage(
newId(), newId(),
event.text, event.text,
event.images.map { SentImage(attachmentId = it.id, file = null, mediaType = it.mediaType) }, event.images.map { SentImage(attachmentId = it.id, file = null, mediaType = it.mediaType) },
event.pastes,
) )
} }
} }
@@ -117,6 +117,13 @@ data class RhemaNotification(
@Serializable @Serializable
data class UserImage(val id: String, @SerialName("media_type") val mediaType: String) data class UserImage(val id: String, @SerialName("media_type") val mediaType: String)
// One paste chip a user_message records: the byte span (UTF-8) of the
// payload in the message's plain text, plus the payload's line count
// — the session annotation's own shape (rhema-core `PasteSpan`), so a
// chip renders from the record instead of rebuilding the wire form.
@Serializable
data class PasteSpan(val start: Int = 0, val end: Int = 0, val lines: Int = 0)
// One event of GET /v1/sessions/{key}/history: the record's seq for it, // One event of GET /v1/sessions/{key}/history: the record's seq for it,
// beside the event itself. Seq order is the wire order. // beside the event itself. Seq order is the wire order.
@Serializable @Serializable
@@ -138,11 +145,13 @@ data class HistoryPage(val events: List<HistoryEvent> = emptyList(), val more: B
sealed interface ClawEvent { sealed interface ClawEvent {
@Serializable @Serializable
@SerialName("user_message") @SerialName("user_message")
// Rows recorded before images rode the stream carry no field at // Rows recorded before images — and then before chips — rode the
// all; absent folds to none, never a guessed shape. // stream carry no field at all; absent folds to none, never a
// guessed shape.
data class UserMessage( data class UserMessage(
val text: String = "", val text: String = "",
val images: List<UserImage> = emptyList(), val images: List<UserImage> = emptyList(),
val pastes: List<PasteSpan> = emptyList(),
) : ClawEvent ) : ClawEvent
@Serializable @Serializable
@@ -82,6 +82,7 @@ import rs.rhema.phone.chat.MAX_STAGED_IMAGES
import rs.rhema.phone.data.AgentIdentityCache import rs.rhema.phone.data.AgentIdentityCache
import rs.rhema.phone.di.AppContainer import rs.rhema.phone.di.AppContainer
import rs.rhema.phone.net.ClawResult import rs.rhema.phone.net.ClawResult
import rs.rhema.phone.net.PasteSpan
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -438,6 +439,7 @@ private sealed interface TurnGroup {
val id: Long, val id: Long,
val text: String, val text: String,
val images: List<ChatController.SentImage>, val images: List<ChatController.SentImage>,
val pastes: List<PasteSpan> = emptyList(),
) : TurnGroup { ) : TurnGroup {
override val key: String = "u$id" override val key: String = "u$id"
} }
@@ -451,7 +453,7 @@ private fun turnGroups(items: List<ChatController.Item>): List<TurnGroup> {
val groups = mutableListOf<TurnGroup>() val groups = mutableListOf<TurnGroup>()
for (item in items) { for (item in items) {
when (item) { when (item) {
is ChatController.UserMessage -> groups += TurnGroup.User(item.id, item.text, item.images) is ChatController.UserMessage -> groups += TurnGroup.User(item.id, item.text, item.images, item.pastes)
else -> { else -> {
val last = groups.lastOrNull() val last = groups.lastOrNull()
if (last is TurnGroup.Agent) last.items += item else groups += TurnGroup.Agent(mutableListOf(item)) if (last is TurnGroup.Agent) last.items += item else groups += TurnGroup.Agent(mutableListOf(item))
@@ -501,10 +503,13 @@ private fun TurnGroupRow(
} }
} }
} }
// Embedded pastes render as chips, not payloads; the // Chips render as chips, not payloads; the text
// text around them stays verbatim. // around them stays verbatim. The row's chip record
// slices them out of the plain text; a row from
// before the record rode the stream carries none
// and still scans the mark form in the text.
var chip = 0 var chip = 0
pasteParts(group.text).forEach { part -> messageParts(group.text, group.pastes).forEach { part ->
when (part) { when (part) {
is PastePart.Text -> Text(part.text, style = MaterialTheme.typography.bodyLarge) is PastePart.Text -> Text(part.text, style = MaterialTheme.typography.bodyLarge)
is PastePart.Paste -> { is PastePart.Paste -> {
@@ -740,20 +745,67 @@ private fun PasteChip(label: String) {
) )
} }
// --- embedded pastes -------------------------------------------------- // --- paste chips ------------------------------------------------------
// The wire form a long paste travels in: MARK, payload, MARK — one // A long paste rides as a chip the render stands in for its payload.
// logical unit inside otherwise plain text (rhema-core's paste module // The record is the span list a user_message carries (the session
// is the definition; these are its client-side shape). A mark with no // annotation's own shape); the marks in the text are the record's
// partner is literal text. // legacy spelling, kept for rows that predate it — a mark with no
// partner is literal text (rhema-core's paste module is the
// definition; these are its client-side shape).
private const val PASTE_MARK = '\uFFFC' private const val PASTE_MARK = '\uFFFC'
/** One scanned piece of embedded-form text. */ /** One piece of a rendered message: literal text, or a chip standing
* for its payload. */
private sealed interface PastePart { private sealed interface PastePart {
data class Text(val text: String) : PastePart data class Text(val text: String) : PastePart
data class Paste(val payload: String) : PastePart data class Paste(val payload: String) : PastePart
} }
/** A message's parts. The chip record is the modern form — each span
* names the payload's byte range in the plain text — and rows recorded
* before it rode the stream carry none; those still scan the legacy
* mark form in the text itself. */
private fun messageParts(text: String, pastes: List<PasteSpan> = emptyList()): List<PastePart> =
if (pastes.isEmpty()) pasteParts(text) else spanParts(text, pastes)
/** Splits a record-carrying message into its parts, left to right. The
* spans' offsets are UTF-8 bytes, the plain form the fold measured —
* every character boundary is mapped in one walk, and a span that
* splits a character maps to none: the render skips it rather than cut
* one in half. Malformed input never throws. */
private fun spanParts(text: String, pastes: List<PasteSpan>): List<PastePart> {
var bytes = 0
var units = 0
val at = mutableMapOf(0 to 0)
var index = 0
while (index < text.length) {
val codePoint = text.codePointAt(index)
val width = Character.charCount(codePoint)
index += width
units = index
bytes += when {
codePoint < 0x80 -> 1
codePoint < 0x800 -> 2
codePoint < 0x10000 -> 3
else -> 4
}
at[bytes] = units
}
val parts = mutableListOf<PastePart>()
var cut = 0
for (span in pastes.sortedBy { it.start }) {
val start = at[span.start]
val end = at[span.end]
if (start == null || end == null || start < cut || end < start) continue
if (start > cut) parts += PastePart.Text(text.substring(cut, start))
parts += PastePart.Paste(text.substring(start, end))
cut = end
}
if (cut < text.length) parts += PastePart.Text(text.substring(cut))
return parts
}
/** Splits embedded-form text into its parts, left to right. A mark with /** Splits embedded-form text into its parts, left to right. A mark with
* no partner is literal text; malformed input never throws. */ * no partner is literal text; malformed input never throws. */
private fun pasteParts(text: String): List<PastePart> { private fun pasteParts(text: String): List<PastePart> {