Files
orca/src/shared/background-task-roster.ts
T
Brennan Benson 85a68bfa4f Codex subagent rows show their task names instead of all saying "worker" (#38491)
* fix(agents): tell Codex subagent rows apart by task name instead of role

A Codex subagent's task name only reached the sidebar when one early read of its rollout succeeded; otherwise every child fell back to its role, so two workers both read "worker". The child poll now re-reads an unnamed child's own rollout, the first-line read allows the existing 256 KiB line cap, and a child with no task name or nickname carries the tail of its id, as Codex's own agent list does. Labels keep the path below the root so nested children with the same leaf stay distinct.

* fix(agents): retry an unnamed Codex child's own rollout without a parent transcript

A relay restart can leave a child hook as the only evidence, sent before the child's rollout was readable. Keep that path as pending identity work and retry it through the existing transcript poll, which now stays scheduled while a child still owes its name.

* fix(agents): carry the id tail to every child-row surface and bound identity retries

- Full-mode sidebar rows and dashboard subagent cards use the same suffixed label as compact rows.
- A role a host publishes as the name (Claude's subagent_type) no longer counts as a name.
- A final identity miss no longer re-arms polling; retries take turns, eight children per poll.
- Replace an error cast with a type guard.

* fix(agents): keep every identity read inside the per-poll budget

The parent-rollout poll now queues an unnamed child for the bounded, rotating identity retries instead of reading it directly, and full-mode rows keep the id tail for a child with no name or role.

* fix(agents): keep plain names on task-only rosters and guard the queued rollout path

A host that publishes only a task roster carries no role, so its names can't be told from roles and get no id tail. Update the sidebar lineage test for a role-only child's id tail.

* fix(agents): tell role-named tasks apart on task-only rosters

A host that publishes only a task roster sends Claude's subagent_type as the task name, so two such tasks read identically. A named agent task with no description now carries its id tail; an unnamed one keeps its kind label.

* fix(agents): a task path found meanwhile wins over a queued header read

Placeholder labels (e.g. unknown) count as unnamed when queueing, and a child the parent rollout named before its turn drops its pending read instead of being overwritten by the header's nickname.
2026-11-05 10:26:01 -08:00

256 lines
8.9 KiB
TypeScript

// Share desktop/mobile grouping and state policy over wire rosters; each surface supplies its own translation function.
import type {
AgentSessionBackgroundTask,
AgentSessionBackgroundTaskRunState
} from './agent-session/agent-session-background-task-wire'
import type { BackgroundTaskSay } from './background-task-copy'
import {
buildAgentChildRowModels,
buildLegacyTaskRowModels,
type AgentChildRowContext,
type AgentChildRowModel
} from './agent-status/presentation/agent-child-row-model'
import { usableAgentChildLabel } from './agent-status/presentation/agent-child-row-name'
import { agentChildRunStateFor } from './agent-status/child-work/agent-status-child-work-display'
import type { AgentChildWorkView } from './agent-status/child-work/agent-status-child-work-view'
import { formatNativeChatDuration } from './native-chat/transcript/native-chat-turn-status'
type TaskKind = AgentSessionBackgroundTask['kind']
type RunState = AgentSessionBackgroundTaskRunState
export type BackgroundRosterTask = {
/** What the row shows, decided by the same model the sidebar's child rows use. */
row: AgentChildRowModel
/** The row's display state in the header's vocabulary. */
state: RunState
}
export type BackgroundTaskGroup = { kind: TaskKind; tasks: BackgroundRosterTask[] }
/** Fixed presentation order; groups render only when non-empty. */
const KIND_ORDER: readonly TaskKind[] = ['agent', 'command', 'monitor', 'workflow', 'unknown']
export function backgroundTaskKindLabel(kind: TaskKind, say: BackgroundTaskSay): string {
switch (kind) {
case 'agent':
return say('agent')
case 'workflow':
return say('workflow')
case 'command':
return say('command')
case 'monitor':
return say('monitor')
case 'unknown':
return say('task')
}
}
/** Fall back through nonblank/nonplaceholder description, name and kind so rows always have a label. */
export function resolveBackgroundTaskName(
task: AgentSessionBackgroundTask,
say: BackgroundTaskSay
): string {
return (
usableAgentChildLabel(task.description) ??
usableAgentChildLabel(task.name) ??
backgroundTaskKindLabel(task.kind, say)
)
}
/** Stable-sort first-seen then id, so a live update never reshuffles surviving rows. */
function groupRosterEntries(entries: BackgroundRosterTask[]): BackgroundTaskGroup[] {
entries.sort((left, right) => {
const startDelta = left.row.firstObservedAt - right.row.firstObservedAt
return startDelta !== 0 ? startDelta : left.row.id < right.row.id ? -1 : 1
})
return KIND_ORDER.map((kind) => ({
kind,
tasks: entries.filter((entry) => entry.row.kind === kind)
})).filter((group) => group.tasks.length > 0)
}
function rosterEntries(rows: readonly AgentChildRowModel[]): BackgroundTaskGroup[] {
return groupRosterEntries(
rows.map((row) => ({ row, state: agentChildRunStateFor(row.displayState) }))
)
}
/** Kind groups from a host that publishes only the task roster, live and settled merged. */
export function buildBackgroundTaskGroups(
tasks: readonly AgentSessionBackgroundTask[],
settledTasks: readonly AgentSessionBackgroundTask[],
say: BackgroundTaskSay
): BackgroundTaskGroup[] {
// An old host's unlabeled row keeps the kind label it always showed; a view row reads its state.
return rosterEntries(
buildLegacyTaskRowModels(tasks, settledTasks).map((row) =>
row.name ? row : { ...row, name: backgroundTaskKindLabel(row.kind, say) }
)
)
}
// Until a caller passes the session's parent-row context, every live claim stands as reported.
const REPORTED_ROW_CONTEXT: AgentChildRowContext = {
parentEvidenceFresh: true,
transportObservation: 'live',
parentObservedAt: 0,
hostClockOffsetMs: 0
}
/** Use the sidebar's parent context for the same verdict. Group lead work first and nest owned work under its child instead of counting it twice. */
export function buildBackgroundTaskGroupsFromViews(
views: readonly AgentChildWorkView[],
context: AgentChildRowContext = REPORTED_ROW_CONTEXT
): BackgroundTaskGroup[] {
return rosterEntries(buildAgentChildRowModels(views, context))
}
export function backgroundTaskStateWord(state: RunState, say: BackgroundTaskSay): string {
switch (state) {
case 'working':
return say('stateWorking')
case 'monitoring':
return say('stateMonitoring')
case 'waiting':
return say('stateWaiting')
case 'blocked':
return say('stateBlocked')
case 'done':
return say('stateDone')
case 'idle':
return say('stateIdle')
case 'unverifiable':
return say('stateUnverifiable')
}
}
/** The reason line for an attention state, per the signed-off mock. */
export function backgroundTaskStateReason(state: RunState, say: BackgroundTaskSay): string | null {
switch (state) {
case 'waiting':
return say('reasonWaiting')
case 'blocked':
return say('reasonBlocked')
case 'working':
case 'monitoring':
case 'done':
case 'idle':
case 'unverifiable':
// `unverifiable`'s state word already says it.
return null
}
}
/** Translate count/state as a whole sentence so languages can agree their forms; `subject` ("2 agents") stands in for the bare count. */
export function backgroundTaskCountedState(
count: number,
state: RunState,
say: BackgroundTaskSay,
subject?: string
): string {
const value = { count, value0: subject ?? count }
switch (state) {
case 'working':
return say('stateWorkingCount', value)
case 'monitoring':
return say('stateMonitoringCount', value)
case 'waiting':
return say('stateWaitingCount', value)
case 'blocked':
return say('stateBlockedCount', value)
case 'done':
return say('stateDoneCount', value)
case 'idle':
return say('stateIdleCount', value)
case 'unverifiable':
return say('stateUnverifiableCount', value)
}
}
export function backgroundTaskGroupLabel(kind: TaskKind, say: BackgroundTaskSay): string {
switch (kind) {
case 'agent':
return say('groupAgents')
case 'command':
return say('groupShell')
case 'monitor':
return say('groupMonitors')
case 'workflow':
return say('groupWorkflows')
case 'unknown':
return say('groupTasks')
}
}
function tokenScaleText(value: number): string {
return Number.isInteger(value) ? value.toFixed(0) : value.toFixed(1)
}
/** Locale-neutral compact token literals (18.2k) match the technical mono metadata slot. */
export function formatBackgroundTaskTokens(totalTokens: number): string {
if (totalTokens < 1_000) {
return String(totalTokens)
}
// Round before picking the unit, or 999_950 renders as "1000k" instead of "1m".
const thousands = Math.round(totalTokens / 100) / 10
return thousands < 1_000
? `${tokenScaleText(thousands)}k`
: `${tokenScaleText(Math.round(totalTokens / 100_000) / 10)}m`
}
export function backgroundTaskElapsedLabel(startedAt: number, now: number): string | null {
if (startedAt <= 0) {
return null
}
return formatNativeChatDuration((now - startedAt) / 1000)
}
/** Whether a row's elapsed time moves: only live work's does. */
export function backgroundTaskRowTicks(row: AgentChildRowModel): boolean {
return !row.settled && row.firstObservedAt > 0
}
function rowsTick(rows: readonly AgentChildRowModel[]): boolean {
return rows.some((row) => backgroundTaskRowTicks(row) || rowsTick(row.owned))
}
/** Tick at 1Hz only for visible live elapsed values (open rows or one collapsed live shell); finished work never wakes the clock. */
export function backgroundTasksStripTicks(
groups: readonly BackgroundTaskGroup[],
expanded: boolean
): boolean {
const singleLiveCommand =
groups.length === 1 && groups[0].kind === 'command' && groups[0].tasks.length === 1
const ticks = groups.some((group) => rowsTick(group.tasks.map((entry) => entry.row)))
return ticks && (expanded || singleLiveCommand)
}
/** A live row's clock runs; a settled row's stops where it settled, so finished work never ticks. */
export function backgroundTaskRowElapsedLabel(row: AgentChildRowModel, now: number): string | null {
if (!row.settled) {
return backgroundTaskElapsedLabel(row.firstObservedAt, now)
}
return row.settledAt !== undefined
? backgroundTaskElapsedLabel(row.firstObservedAt, row.settledAt)
: null
}
/** The row's meta slot: tokens, then elapsed. '' when it has neither. */
export function backgroundTaskRowMeta(row: AgentChildRowModel, now: number): string {
return [
row.totalTokens !== undefined ? formatBackgroundTaskTokens(row.totalTokens) : null,
backgroundTaskRowElapsedLabel(row, now)
]
.filter((part): part is string => part !== null)
.join(' · ')
}
/** Only explicit false withholds Stop for compatibility; unsupported targets otherwise silently resolve to an empty cancellation list. */
export function backgroundTaskRowStopId(
row: AgentChildRowModel,
supportsTaskStop: boolean
): string | null {
return !row.settled && supportsTaskStop && row.canStop ? (row.providerId ?? null) : null
}