Files
hermes-agent/ui-tui/src/domain/blockLayout.ts
T
ethernet a4bc1ca502 fix(timeline): persist typed display events (#69771)
* fix(desktop): hide persisted agent-only history scaffolding

Filter verification-stop nudges and context-compaction handoffs at the
stored-history mapper boundary. Preserve a real reply when a compaction
handoff shares its stored message.

* test(desktop): build persisted E2E sessions through the real agent

Drive tui_gateway.entry over its stdio JSON-RPC transport against the mock
provider, wait for real completion events, and persist normal session history
through AIAgent and SessionDB. Migrate resume and hidden-history coverage,
including real compression and live verify-on-stop scaffolding, then remove
the unused direct SessionDB import scripts.

* fix(desktop): use the provisioned Python for real-session E2Es

Run the stdio gateway through uv's synced project environment outside the
Nix dev shell, while retaining the fully provisioned Nix Python when the
shell advertises HERMES_PYTHON_SRC_ROOT.

* fix(nix): expose the provisioned Python environment to uv

Mark the Nix-built Python environment active in the dev shell so the shared
E2E session builder can always run through `uv run --active --no-sync`.

* fix(timeline): persist typed display events

* fix(timeline): strip display-only fields from provider payloads, preserve through rewrites, fix /resume display history

Three review findings from PR #69771:

1. Provider payload leak: display_kind and display_metadata were forwarded
   to the provider API as unknown message fields. Strict OpenAI-compatible
   backends can reject the next request after a model switch or resumed
   typed event. Strip both from the per-request api_msg copy in
   conversation_loop alongside the existing api_content pop.

2. Rewrite/import data loss: _insert_message_rows preserved display_kind
   but silently dropped display_metadata. After replace_messages,
   archive_and_compact, or session import, async-delegation completion
   events lost their task counts and fell back to generic display text.
   Add display_metadata to the INSERT columns and bind tuple.

3. CLI /resume stale recap: startup --resume A set _resume_display_history
   from A's lineage. A subsequent in-session /resume B loaded B only into
   conversation_history via get_messages_as_conversation, leaving the stale
   A display projection. _display_resumed_history preferentially read the
   stale attribute, showing A's recap for B. Switch /resume to
   get_resume_conversations and update _resume_display_history alongside
   conversation_history.

Tests: 890 Python (5 files), 35 desktop TS — all green.

* feat(tui): render typed display events as ◈ markers in the Ink TUI

The TUI was not handling display_kind at all — model switch markers and
async delegation completions rendered as opaque user messages with the
full [System: ...] text, and hidden compaction handoffs were visible.

Wire display_kind through the full TUI chain:

- _history_to_messages (tui_gateway/server.py) forwards display_kind
  and display_metadata to the gateway transcript payload.
- GatewayTranscriptMessage (gatewayTypes.ts) gains both fields.
- Msg.kind (types.ts) gains 'event' value.
- toTranscriptMessages (domain/messages.ts) maps:
  - hidden → skip entirely
  - model_switch → event "model changed"
  - async_delegation_complete → event "N background agents finished"
    (or "background agent work finished" without metadata)
- messageGroup (blockLayout.ts) routes event to its own group, with
  SELF_SPACED + PAINTS_TRAILING_GAP so it owns its margins.
- messageLine.tsx renders event-kind as a dim ◈ marker with no gutter,
  matching the CLI's ◈ event rendering.
- 4 new TUI tests for hidden/model_switch/async_delegation mapping.

TUI typecheck: clean. TUI lint: 0 errors (2 pre-existing warnings).
TUI tests: 9 passed (1 pre-existing failure on main, unrelated).
2026-07-23 14:46:24 -04:00

151 lines
5.4 KiB
TypeScript

import type { DetailsMode, Msg, SectionVisibility } from '../types.js'
import { sectionMode } from './details.js'
/**
* Visual group a transcript block belongs to. Blocks in the same group render
* flush; a single blank line opens at each group boundary. So a run of tool
* trails (or model paragraphs) reads as one section and the eye only catches a
* gap where the *kind* of content actually changes — the boundary between
* reasoning/tool trails, model prose, and notes/errors.
*
* user — the human turn (owns its separator + margins in MessageLine)
* model — assistant prose, the model's voice
* trail — reasoning + tool-call trails (the agent's working area)
* note — system notes and errors (a quieter band)
* diff — inline patch segments (an island, owns its own margins)
* slash — slash-command echoes (owns its margin)
* intro — banner / panels (rendered out-of-band, never gapped here)
*/
export type BlockGroup = 'diff' | 'event' | 'intro' | 'model' | 'note' | 'slash' | 'trail' | 'user'
export const messageGroup = (msg: Pick<Msg, 'kind' | 'role'>): BlockGroup => {
switch (msg.kind) {
case 'intro':
case 'panel':
return 'intro'
case 'slash':
return 'slash'
case 'event':
return 'event'
case 'diff':
return 'diff'
case 'trail':
return 'trail'
}
if (msg.role === 'user') {
return 'user'
}
// Assistant prose is the model's voice; system notes/errors are their own
// band. (No runtime block uses role 'tool' — tool *results* fold into
// trails — so a stray 'tool' falls through to the note band harmlessly.)
return msg.role === 'assistant' ? 'model' : 'note'
}
// Groups whose leading gap is already owned by their own chrome in
// MessageLine (the turn separator + top margin for user, the top margin for
// slash, the top+bottom margins for diff) or that are painted out-of-band
// (intro). The grouping primitive only spaces the model working area —
// model prose, reasoning/tool trails, and notes/errors.
const SELF_SPACED: ReadonlySet<BlockGroup> = new Set(['diff', 'event', 'intro', 'slash', 'user'])
// Groups that already paint a trailing blank line beneath themselves
// (marginBottom in MessageLine), so the block that follows must not add its
// own leading gap or the single boundary would become a double gap.
const PAINTS_TRAILING_GAP: ReadonlySet<BlockGroup> = new Set(['diff', 'event', 'user'])
/**
* Whether `cur` renders one blank line above it, given the block rendered
* directly above it (`prev`). True only where the visual group changes, and
* only for the model-working-area bands (model / trail / note) — user, slash,
* diff, and intro keep their existing spacing.
*
* Streaming-safe by construction: the result depends on the *predecessor's*
* group, never on `cur`'s own (live, changing) content. The actively-streaming
* assistant block therefore computes the same gap while it streams as the
* settled segment does once it flushes, so the live area never jumps.
*/
export const hasLeadGap = (prev: Pick<Msg, 'kind' | 'role'> | undefined, cur: Pick<Msg, 'kind' | 'role'>): boolean => {
const group = messageGroup(cur)
if (SELF_SPACED.has(group)) {
return false
}
if (!prev) {
return false
}
const prevGroup = messageGroup(prev)
return prevGroup !== group && !PAINTS_TRAILING_GAP.has(prevGroup)
}
export interface DetailsCtx {
commandOverride?: boolean
detailsMode: DetailsMode
sections?: SectionVisibility
}
const trailAllHidden = (ctx: DetailsCtx): boolean =>
sectionMode('thinking', ctx.detailsMode, ctx.sections, ctx.commandOverride) === 'hidden' &&
sectionMode('tools', ctx.detailsMode, ctx.sections, ctx.commandOverride) === 'hidden' &&
sectionMode('activity', ctx.detailsMode, ctx.sections, ctx.commandOverride) === 'hidden'
/**
* Whether a settled transcript block paints anything. A trail renders nothing
* when it has no reasoning/tools/todos to show (e.g. the finalDetails segment
* that carries only a token tally) or when every section it does have is hidden
* (`/details hidden`); every other block draws at least one row. A block that
* renders nothing is *transparent* to grouping: the block below it draws its
* boundary against the nearest visible block instead (see prevRenderedMsg), so
* a hidden or content-less trail never leaves a floating blank line, doubles
* the gap after a user prompt, or pads the space above the final reply. In the
* default/collapsed modes content-bearing trails always render, so this is a
* no-op there.
*/
export const blockRenders = (msg: Pick<Msg, 'kind' | 'thinking' | 'todos' | 'tools'>, ctx: DetailsCtx): boolean => {
if (msg.kind !== 'trail') {
return true
}
if (msg.todos?.length) {
return true
}
if (!(msg.tools?.length || msg.thinking?.trim())) {
return false
}
return !trailAllHidden(ctx)
}
/**
* The nearest block above `index` that actually renders, resolved through a
* lazy accessor so it works over either the virtualized history rows or the
* live block list. This is the grouping predecessor — using it (instead of the
* literal previous row) keeps hidden trails from interrupting the rhythm.
*/
export const prevRenderedMsg = (
msgAt: (i: number) => Msg | undefined,
index: number,
ctx: DetailsCtx
): Msg | undefined => {
for (let i = index - 1; i >= 0; i--) {
const candidate = msgAt(i)
if (candidate && blockRenders(candidate, ctx)) {
return candidate
}
}
return undefined
}