Show a compaction happening, and what it recovered

The Compacting status had been declared, rendered in four places, and
never once emitted: no driver produced it, and the app had no control to
ask for a compaction in the first place. Pressing nothing for two
minutes and then quietly having less context was the whole experience.

The CLI turns out to announce all of it, which was worth measuring
rather than guessing at. Driven through /compact against 2.1.237 it
emits a `status: "compacting"` line at the start, a `status: null`
carrying `compact_result` at the end -- `"failed"` with a sentence
saying why, when it does -- and then a `compact_boundary` with the token
counts. The same records appear in the CLI's own transcript file with
camelCase keys, which is the obvious place to read the shape off and
gets every field name wrong.

So none of it is inferred here. The driver writes the line and says
nothing; the translator reports what the CLI reports. A failed
compaction surfaces the CLI's own sentence, which is specific enough to
act on.

The counts are the part worth keeping afterwards, so they land in the
transcript rather than only in a status that vanishes: a session that
went from 128,402 tokens to 9,617 has just been given its context back.
They are optional throughout, because a compaction whose size nobody
reported has to be able to say so -- a zero would read as "recovered
nothing".

Also here, all found on the way:

- `rename_all` renames variants; fields need `rename_all_fields`. Every
  field in Event was a single word until `pre_tokens`, which went out as
  snake_case, was not found by the app, and rendered as the "no counts
  reported" case -- a state it is allowed to be in, so nothing looked
  wrong. There is now a test on the wire names.
- The unparseable-line warning sliced bytes, not chars, on output that
  is full of em dashes. A panic there kills the task reading the
  session's stdout, and the session goes deaf with nothing on screen.
  The other three truncations in the tree already did this correctly.
- Echo compacts too, with invented numbers and a real shape, so this
  screen can be looked at without spending two minutes of somebody's
  account to reach the state.
This commit is contained in:
iris committed 2026-08-29 14:42:08 -04:00
1 parent 42131c75d6
commit 5396da76c7
8 files changed
+408 -38

No files matched your search

@@ -0,0 +1,53 @@
package com.example.aiapp
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
/**
* The mark a compaction leaves in the transcript.
*
* Centred and dim, because it is a divider rather than something anybody said: everything above it
* is out of the session's context now, and that is a fact about the conversation, not a turn in it.
* It has no collapsed form -- it is already one line, and there is nothing behind it to open.
*/
@Composable
fun CompactedRow(item: TranscriptItem.CompactedNote, modifier: Modifier = Modifier) {
Text(
compactionSummary(item),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center,
modifier = modifier.fillMaxWidth().padding(vertical = 8.dp),
)
}
/**
* What to say about a compaction, given what was measured about it.
*
* The counts are the whole point when they are there -- "a million tokens became ten thousand" is
* the reader's answer to why the wait was worth it. When they are not, this says nothing about size
* rather than filling in a plausible number, and when only the size before is known it says exactly
* that much.
*
* `auto` is named because it is the case the reader did not ask for, and so the one that explains a
* session going quiet on its own. Anything else -- including a trigger this build does not
* recognise -- makes no claim about who asked, which is the honest reading of not knowing.
*/
fun compactionSummary(item: TranscriptItem.CompactedNote): String {
val what = if (item.trigger == "auto") "Compacted automatically" else "Compacted"
val pre = item.preTokens
val post = item.postTokens
return when {
pre != null && post != null -> "$what -- ${tokens(pre)} to ${tokens(post)} tokens"
pre != null -> "$what -- was ${tokens(pre)} tokens, new size not reported"
else -> what
}
}
private fun tokens(count: Long): String = "%,d".format(count)