Files
orca/src/main/runtime/runtime-worktree-agent-rows.ts
T
Neil e3c9370da4 feat(agent-status): read OSC 7501 program status beside hooks (#30261)
Programs can tell Orca what they are doing through the Program Status
Protocol (OSC 7501); Claude Code, Codex, Grok, pi and oh-my-pi already do.
Orca answers the protocol's feature-detection probe (visible xterm and the
hidden-pane emulator), parses reports from PTY output on the execution host
(src/shared/program-status/), and keeps two lanes:

- Lifecycle state keeps one owner per pane. Hooks win; OSC 7501 drives the
  row when hooks are off, missing, or silent on a state the program has
  left (server/program-status/hook-precedence).
- The program's own report (progress, its one-line message, blocks such as
  sign-in) is a live-only `programStatus` facet kept per pane
  (server/program-status/facet-store), stitched onto rows at publish time
  and carried to the renderer store, mobile, paired clients and
  `worktree ps`. Facet-only changes patch just the facet.

Title-driven readers (idle edges, delivery, tui-idle, exit inference)
classify by the program's OSC 7501 activity while it reports one and fall
back to title glyphs only for agents that report nothing explicit, so they
keep working when Claude stops painting its title glyph after the probe.
2026-10-18 22:12:37 -07:00

127 lines
4.7 KiB
TypeScript

import { isFreshNonDoneAgentStatus } from '../../shared/agent-status-types'
import { isAgentStatusFresh } from '../../shared/agent-status-freshness'
import type { RuntimeWorktreeAgentRow, RuntimeWorktreePsSummary } from '../../shared/runtime-types'
import { mergeWorktreeSummaryStatus } from './runtime-worktree-status-projection'
import type { RuntimeWorktreePsSummaryLookup } from './runtime-worktree-summary-paths'
import type { RuntimeWorkingTerminalEvidence } from './runtime-worktree-ps-activity'
import type { RuntimeWorktreeAgentSource } from './runtime-worktree-agent-source'
export type { RuntimeAgentRowSnapshot } from './runtime-hook-agent-row-selection'
type OrchestrationDisplay = {
taskTitle?: string | null
displayName?: string | null
parentPaneKey?: string | null
}
export function attachRuntimeWorktreeAgentRows(args: {
summaries: Map<string, RuntimeWorktreePsSummary>
rowSources: ReadonlyMap<string, RuntimeWorktreeAgentSource>
workingTerminalEvidenceByWorktreeId: ReadonlyMap<
string,
readonly RuntimeWorkingTerminalEvidence[]
>
orchestrationByPaneKey: Record<string, OrchestrationDisplay> | null | undefined
getSummary: RuntimeWorktreePsSummaryLookup
}): void {
const { rowSources } = args
const now = Date.now()
const rowsByWorktree = new Map<string, RuntimeWorktreeAgentRow[]>()
for (const source of rowSources.values()) {
const { worktreeId } = source
if (!worktreeId) {
continue
}
const summary = args.getSummary(worktreeId)
if (!summary) {
continue
}
const orchestration = args.orchestrationByPaneKey?.[source.paneKey]
const row: RuntimeWorktreeAgentRow = {
paneKey: source.paneKey,
parentPaneKey: orchestration?.parentPaneKey ?? null,
state: source.state,
...(source.workingMode ? { workingMode: source.workingMode } : {}),
agentType: source.agentType,
prompt: source.prompt,
taskTitle: orchestration?.taskTitle ?? null,
displayName: orchestration?.displayName ?? null,
lastAssistantMessage: source.lastAssistantMessage,
toolName: source.toolName,
toolInput: source.toolInput,
interrupted: source.interrupted,
...(source.mainAgent ? { mainAgent: source.mainAgent } : {}),
stateStartedAt: source.stateStartedAt,
updatedAt: source.updatedAt,
...(source.children !== undefined ? { children: source.children } : {}),
...(source.subagents !== undefined ? { subagents: source.subagents } : {}),
...(source.structuredHost === 'owned' ? { structuredHostOwned: true as const } : {}),
...(source.programStatus ? { programStatus: source.programStatus } : {})
}
if (source.children !== undefined || source.subagents !== undefined) {
row.subagentObservation = isAgentStatusFresh(row, now) ? 'live' : 'unverifiable'
}
const rows = rowsByWorktree.get(summary.worktreeId)
if (rows) {
rows.push(row)
} else {
rowsByWorktree.set(summary.worktreeId, [row])
}
}
for (const [worktreeId, rows] of rowsByWorktree) {
rows.sort((a, b) => a.stateStartedAt - b.stateStartedAt)
const summary = args.summaries.get(worktreeId)
if (!summary) {
continue
}
summary.agents = rows
let hasForegroundWorkingAgent = false
const monitoringSources: RuntimeWorktreeAgentSource[] = []
for (const row of rows) {
if (!isFreshNonDoneAgentStatus(row, now)) {
continue
}
summary.hasHostSidebarActivity = true
if (row.state === 'working') {
if (row.workingMode === 'monitoring') {
const source = rowSources.get(row.paneKey)
if (source) {
monitoringSources.push(source)
}
} else {
hasForegroundWorkingAgent = true
}
} else {
mergeWorktreeSummaryStatus(summary, 'permission')
}
}
if (hasForegroundWorkingAgent || monitoringSources.length > 0) {
const hasIndependentWorkingTerminal = (
args.workingTerminalEvidenceByWorktreeId.get(worktreeId) ?? []
).some((evidence) =>
monitoringSources.every((source) => !workingTerminalEvidenceMatchesSource(evidence, source))
)
mergeWorktreeSummaryStatus(
summary,
'working',
hasForegroundWorkingAgent || hasIndependentWorkingTerminal ? undefined : 'monitoring'
)
}
}
}
function workingTerminalEvidenceMatchesSource(
evidence: RuntimeWorkingTerminalEvidence,
source: RuntimeWorktreeAgentSource
): boolean {
if (evidence.paneKey) {
return (
evidence.paneKey === source.paneKey ||
Boolean(evidence.ptyId && source.ptyId && evidence.ptyId === source.ptyId)
)
}
if (evidence.ptyId && source.ptyId) {
return evidence.ptyId === source.ptyId
}
return Boolean(evidence.tabId && evidence.tabId === source.tabId)
}