Display per harness spec 3.7.0: the end line, durations, locale numbers, the cards' actions
CI / check (push) Canceled after 0s

- A reply that was stopped or failed keeps the progress text at its end
  (display.md section 4): "Stopped: by you", "Failed: ...". message.stopped
  was never drawn, and a failure showed the raw error. The reason comes from
  the stream's last status frame and stays with the finished row; a stored
  row gives "Stopped" or "Failed: <first line of its error>".
- A finished tool call shows its duration (0.4s, 1m 05s, 2h 03m) from the
  call's `ms`, where the server sends it; none while running or when denied.
- Counts in the progress line are in the phone's locale format (Locale.ROOT
  is the spec's plain format, which the conformance cases use); a cut `what`
  ends in an ellipsis, counted in code points; the clock is joined with the
  shared progress_line separator.
- Approval card: Allow once / Always allow / Deny / Deny, and say why. Plan
  card: Approve, continue in Edit / in Manual, and Revise.
- fetch-contract.py pins the spec's display.json as harness_display.json
  once the server's copy of the spec has it; DisplayTest then runs its
  duration and end cases.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Fable 5.2 committed 2026-11-12 10:03:04 +00:00
1 parent f2a2acf996
commit a30075c8f3
22 files changed
+428 -41

No files matched your search

@@ -0,0 +1,88 @@
package eu.llembas.android.api
import java.text.NumberFormat
import java.util.Locale
/*
* One display in every client (harness spec 3.7.0, `display.md`): what this app says about a tool
* call, the end of a reply and a number -- the same things, from the same data, as the web UI,
* Desktop and the CLI.
*/
/** The states that end a turn without finishing it; their line stays on the reply (`display.end`). */
val END_PHASES = setOf("stopped", "failed")
/**
* A call's short title as a line may show it (`display.md` §1, step 4): trimmed, its first line
* only, and cut to [max] code points -- one fewer and `…` -- so a cut title says it was cut.
* Null when nothing is left.
*/
fun shortTitle(text: String?, max: Int = TITLE_MAX): String? {
val line = text?.trim()?.lineSequence()?.firstOrNull()?.trim().orEmpty()
if (line.isEmpty()) return null
if (line.codePointCount(0, line.length) <= max) return line
return line.substring(0, line.offsetByCodePoints(0, max - 1)) + "…"
}
/** `loop.json` `progress.what_max`. */
const val TITLE_MAX = 80
/**
* The state a drawn call is in (`display.md` §2): `running`, `done`, `failed` or `denied`, from
* what `/api/v2` says of it.
*/
fun ToolCall.state(): String = when {
status == "denied" -> "denied"
status == "error" || !error.isNullOrEmpty() -> "failed"
status == "running" || status == "pending" -> "running"
else -> "done"
}
/**
* How long a finished call took (`display.md` §2): tenths under a minute (`0.4s`), then
* `1m 05s`, then `2h 03m`. None for a call that is running or was denied, or whose time was not
* kept. Not a locale number: written with `.` everywhere.
*/
fun callDuration(state: String, ms: Long?): String? {
if (ms == null || state == "running" || state == "denied") return null
val seconds = ms.coerceAtLeast(0) / 1000.0
if (seconds < 60) return String.format(Locale.ROOT, "%.1fs", seconds)
val minutes = (seconds / 60).toLong()
return if (minutes < 60) {
String.format(Locale.ROOT, "%dm %02ds", minutes, (seconds % 60).toLong())
} else {
String.format(Locale.ROOT, "%dh %02dm", minutes / 60, minutes % 60)
}
}
/**
* How a reply ended, when it did not finish (`display.md` §4): the `stopped` or `failed` progress
* whose line stays on it. From the end the stream said (kept on the message as its `progress`,
* with the reason: by you, step limit, budget), else from what the stored row knows -- that it
* was stopped, or its error, of which the first line is the reason. Null for a reply that
* finished, a user's message, or one still queued.
*/
fun Message.end(): Progress? = when {
role != "assistant" -> null
progress?.phase in END_PHASES -> progress
!error.isNullOrBlank() -> Progress(phase = "failed", reason = error.lineSequence().firstOrNull { it.isNotBlank() }?.trim())
stopped -> Progress(phase = "stopped")
else -> null
}
/**
* A count a person reads, in [locale]'s own format -- its grouping and its decimal mark
* (`display.md` §6): `12,480`, `12 480`, `12.480`. [Locale.ROOT] is the spec's plain format, no
* grouping and `.`, which is what its conformance texts are written in.
*/
fun localeNumber(n: Long, locale: Locale): String = if (locale == Locale.ROOT) n.toString() else NumberFormat.getIntegerInstance(locale).format(n)
/** A measure with [decimals] places, in [locale]'s format; plain for [Locale.ROOT]. */
fun localeNumber(n: Double, decimals: Int, locale: Locale): String {
if (locale == Locale.ROOT) return String.format(Locale.ROOT, "%.${decimals}f", n)
val format = NumberFormat.getNumberInstance(locale)
format.minimumFractionDigits = decimals
format.maximumFractionDigits = decimals
format.roundingMode = java.math.RoundingMode.HALF_UP
return format.format(n)
}
@@ -17,6 +17,8 @@ data class LiveReply(
val status: String = "",
/** The structured progress beside [status] (spec 3.4.0); null from an older server, or idle. */
val progress: Progress? = null,
/** How the turn ended when it did not finish: the last `stopped` or `failed` progress, with its reason. */
val end: Progress? = null,
val cards: List<Card> = emptyList(),
val jobs: List<Job> = emptyList(),
/** The subagent tree (spec 3.4.0), from the `tree` frame; null until there is one. */
@@ -49,7 +51,9 @@ data class LiveReply(
"plan" -> copy(plan = decodePlan(data))
"status" -> decode(StatusFrame.serializer(), data)?.let { copy(status = it.text, progress = it.progress) } ?: this
"status" -> decode(StatusFrame.serializer(), data)?.let { frame ->
copy(status = frame.text, progress = frame.progress, end = frame.progress?.takeIf { it.phase in END_PHASES })
} ?: this
"ask" -> decode(AskFrame.serializer(), data)?.let { copy(cards = it.cards) } ?: this
@@ -59,8 +63,20 @@ data class LiveReply(
"metrics" -> decode(Metrics.serializer(), data)?.let { copy(metrics = it) } ?: this
"done" -> decode(DoneFrame.serializer(), data)?.let {
copy(finished = it.message, title = it.chat.title.ifEmpty { null }, next = it.next, cards = emptyList(), status = "", progress = null)
"done" -> decode(DoneFrame.serializer(), data)?.let { done ->
// The finished row says that it was stopped or failed; the stream said why (by you,
// the step limit, the budget). The reason goes with the row, where its line stays
// (display.md §4).
val row = done.message
val ended = row.stopped || !row.error.isNullOrBlank()
copy(
finished = if (ended && row.progress == null && end != null) row.copy(progress = end) else row,
title = done.chat.title.ifEmpty { null },
next = done.next,
cards = emptyList(),
status = "",
progress = null,
)
} ?: this
"close" -> copy(closed = true)
@@ -376,6 +376,8 @@ data class ToolCall(
val diff: String? = null,
val exit: Int? = null,
val hits: Int? = null,
/** How long the call took, in milliseconds (spec 3.7.0: `tool_end`'s `ms`); null where it was not kept. */
val ms: Long? = null,
val path: String? = null,
val truncated: Boolean = false,
val job: JsonElement? = null,
@@ -36,7 +36,7 @@ data class Progress(
) {
companion object {
/** `loop.json` `progress.no_elapsed`: drawn as their text alone. */
val NO_ELAPSED = setOf("stopped", "failed")
val NO_ELAPSED = END_PHASES
}
}
@@ -51,14 +51,16 @@ fun interface ProgressWords {
}
/**
* The phase's text, its fields filled (`progress.md` §2): numbers as integers, a rate with one
* decimal in [locale]'s format, `retrying`'s `in` counted down to [now]. Null for a phase this
* app does not know -- a newer server's -- so the caller falls back to the `status` text.
* The phase's text, its fields filled (`progress.md` §2): every count and measure in [locale]'s
* own number format (`display.md` §6: the phone's grouping and decimal mark; [Locale.ROOT] is the
* spec's plain format), a title that had to be cut ending in `…`, `retrying`'s `in` counted down
* to [now]. Null for a phase this app does not know -- a newer server's -- so the caller falls
* back to the `status` text.
*/
fun progressText(progress: Progress, words: ProgressWords, now: Long, locale: Locale = Locale.getDefault()): String? {
val p = progress
return when (p.phase) {
"queued" -> if (p.ahead != null) words.text("queued_ahead", "${p.ahead}") else words.text("queued")
"queued" -> if (p.ahead != null) words.text("queued_ahead", localeNumber(p.ahead.toLong(), locale)) else words.text("queued")
"waiting_for_model" -> {
val model = p.model ?: return null
@@ -72,7 +74,8 @@ fun progressText(progress: Progress, words: ProgressWords, now: Long, locale: Lo
val done = p.done
val total = p.total
if (done != null && total != null && total > 0) {
words.text("reading_prompt", "$done", "$total", "${done.toLong() * 100 / total}")
val percent = localeNumber(done.toLong() * 100 / total, locale)
words.text("reading_prompt", localeNumber(done.toLong(), locale), localeNumber(total.toLong(), locale), percent)
} else {
words.text("reading_prompt_bare")
}
@@ -80,11 +83,13 @@ fun progressText(progress: Progress, words: ProgressWords, now: Long, locale: Lo
"thinking" -> words.text("thinking")
"writing" -> p.rate?.let { words.text("writing", String.format(locale, "%.1f", it)) } ?: words.text("writing_bare")
"writing" -> p.rate?.let { words.text("writing", localeNumber(it, 1, locale)) } ?: words.text("writing_bare")
"running_tool" -> {
val tool = p.tool ?: return null
if (!p.what.isNullOrBlank()) words.text("running_tool", tool, p.what.take(80)) else words.text("running_tool_bare", tool)
// The call's short title (display.md §1): one line, and `…` where it had to be cut.
val what = shortTitle(p.what)
if (what != null) words.text("running_tool", tool, what) else words.text("running_tool_bare", tool)
}
"waiting_for_person" -> {
@@ -96,7 +101,7 @@ fun progressText(progress: Progress, words: ProgressWords, now: Long, locale: Lo
words.text("waiting_for_person", what)
}
"waiting_for_subagents" -> words.text("waiting_for_subagents", "${p.running ?: 0}")
"waiting_for_subagents" -> words.text("waiting_for_subagents", localeNumber((p.running ?: 0).toLong(), locale))
"compacting" -> words.text("compacting")