import type { ChildModelState, ChildTokenState, StatuslineState, } from "./state.js"; import { deriveOpenCodeSessionStatus, hasStructuredErrorEvidence, } from "./reconcile.js"; import { markChildStatus, setChildModel, upsertChildDetails, upsertRunningChild, } from "./state.js"; export type EventLike = { type?: unknown; title?: unknown; name?: unknown; sessionID?: unknown; sessionId?: unknown; status?: unknown; state?: unknown; properties?: { id?: unknown; sessionID?: unknown; sessionId?: unknown; title?: unknown; name?: unknown; info?: { id?: unknown; title?: unknown; name?: unknown; agent?: unknown; subagent_type?: unknown; sessionID?: unknown; sessionId?: unknown; parentID?: unknown; role?: unknown; time?: unknown; status?: unknown; state?: unknown; }; parentID?: unknown; part?: unknown; status?: unknown; state?: unknown; }; parentID?: unknown; [key: string]: unknown; }; type SubtaskChild = { id: string; title: string; summary?: string; agentName?: string; parentID: string; messageID: string; targetSessionID?: string; startedAt?: string; updatedAt?: string; }; type ToolChild = SubtaskChild & { toolName: "delegate" | "task"; status: "running" | "done" | "error"; endedAt?: string; }; export type TaskToolStatus = "running" | "done" | "error"; export type TaskToolEvidence = { status: TaskToolStatus; targetSessionID?: string; endedAt?: string; }; type SyntheticTargetContext = { id: string; parentID: string; messageID?: string; }; export function asString(value: unknown): string | undefined { return typeof value === "string" && value.length > 0 ? value : undefined; } function normalizeMessage(value: unknown): Record | undefined { const message = isRecord(value) ? value : undefined; if (!message) return undefined; return isRecord(message.info) ? message.info : message; } function messageActivityMs(message: Record): number { const time = isRecord(message.time) ? message.time : undefined; const value = time?.completed ?? time?.updated ?? time?.created; if (typeof value === "number" && Number.isFinite(value)) return value; if (typeof value === "string") { const parsed = Date.parse(value); return Number.isNaN(parsed) ? 0 : parsed; } return 0; } export function extractLatestAssistantModel( input: unknown | readonly unknown[], ): { sessionID: string; model?: ChildModelState; updatedAt?: string } | undefined { const values = Array.isArray(input) ? input : [input]; const assistants = values .map((value, index) => ({ message: normalizeMessage(value), index })) .filter( (entry): entry is { message: Record; index: number } => entry.message?.role === "assistant", ) .sort( (left, right) => messageActivityMs(left.message) - messageActivityMs(right.message) || left.index - right.index, ); const latest = assistants.at(-1)?.message; if (!latest) return undefined; const sessionID = asString(latest.sessionID); if (!sessionID) return undefined; const providerID = asString(latest.providerID)?.trim(); const modelID = asString(latest.modelID)?.trim(); const variant = asString(latest.variant)?.trim(); const model = providerID && modelID ? { providerID, modelID, ...(variant ? { variant } : {}) } : undefined; const activity = messageActivityMs(latest); return { sessionID, model, updatedAt: activity > 0 ? new Date(activity).toISOString() : undefined, }; } function conciseText(value: unknown): string | undefined { if (typeof value !== "string") return undefined; const text = value.replace(/\s+/g, " ").trim(); if (!text) return undefined; return text.length > 180 ? `${text.slice(0, 179)}…` : text; } function sameDisplayText( a: string | undefined, b: string | undefined, ): boolean { if (!a || !b) return false; return ( a.replace(/\s+/g, " ").trim().toLowerCase() === b.replace(/\s+/g, " ").trim().toLowerCase() ); } function firstDistinctSummary( candidates: unknown[], title: string | undefined, ): string | undefined { for (const candidate of candidates) { const summary = conciseText(candidate); if (summary && !sameDisplayText(summary, title)) return summary; } return undefined; } function isTechnicalDelegationTitle(value: string | undefined): boolean { if (!value) return false; return /^delegation:\s+/i.test(value.trim()); } function promptTitle(value: unknown): string | undefined { const text = conciseText(value); if (!text) return undefined; const sentence = text.match(/^(.+?[.!?])\s/)?.[1]?.trim(); const title = sentence && sentence.length <= 100 ? sentence : text; return title.length > 100 ? `${title.slice(0, 99)}…` : title; } function firstUsefulTitle(candidates: unknown[]): string | undefined { for (const candidate of candidates) { const title = promptTitle(candidate); if (title && !isTechnicalDelegationTitle(title)) return title; } return undefined; } export function extractCreatedChild(event: EventLike): { id: string; title: string; agentName?: string; parentID: string; startedAt?: string; updatedAt?: string; } | null { const info = event.properties?.info; const parentID = asString(info?.parentID); if (!parentID) return null; const id = asString(info?.id) ?? asString(event.properties?.id); if (!id) return null; const title = asString(info?.title) ?? "subagent"; const agentName = asString(info?.agent) ?? asString(info?.subagent_type); const startedAt = extractEventTimestamp(event, [ "started", "start", "created", "updated", ]); const updatedAt = extractEventTimestamp(event, ["updated", "created", "started", "start"]) ?? startedAt; return { id, title, agentName, parentID, startedAt, updatedAt }; } export function extractSessionID(event: EventLike): string | undefined { return ( asString(event.properties?.sessionID) ?? asString(event.properties?.sessionId) ?? asString(event.properties?.info?.sessionID) ?? asString(event.properties?.info?.sessionId) ?? asString(event.sessionID) ?? asString(event.sessionId) ?? asString(event.properties?.info?.id) ?? asString(event.properties?.id) ); } function isRecord(value: unknown): value is Record { return !!value && typeof value === "object"; } function isSessionID(value: unknown): value is string { return typeof value === "string" && value.startsWith("ses_"); } function collectSessionIDs( input: unknown, target: Set, depth = 0, ): void { if (depth > 4 || !input) return; if (isSessionID(input)) { target.add(input); return; } if (!isRecord(input) && !Array.isArray(input)) return; if (Array.isArray(input)) { for (const value of input) { collectSessionIDs(value, target, depth + 1); } return; } for (const [key, value] of Object.entries(input)) { if (!key.toLowerCase().includes("session")) continue; collectSessionIDs(value, target, depth + 1); } } function resolveSyntheticTargetSessionID( state: StatuslineState, synthetic: SyntheticTargetContext, explicitCandidates: readonly string[] = [], ): string | undefined { const candidates = new Set(explicitCandidates.filter(isSessionID)); const byMessage = Object.values(state.children).filter( (child) => child.id.startsWith("ses_") && child.parentID === synthetic.parentID && child.messageID && synthetic.messageID && child.messageID === synthetic.messageID, ); if (byMessage.length === 1) { candidates.add(byMessage[0].id); } const byParent = Object.values(state.children).filter( (child) => child.id.startsWith("ses_") && child.parentID === synthetic.parentID, ); if (byParent.length === 1) { candidates.add(byParent[0].id); } if (candidates.size !== 1) return undefined; return [...candidates][0]; } function extractPartTargetSessionCandidates(event: EventLike): string[] { const part = isRecord(event.properties?.part) ? event.properties.part : undefined; if (!part) return []; const candidates = new Set(); collectSessionIDs(part, candidates); const parentSessionID = asString(part.sessionID) ?? extractSessionID(event); if (parentSessionID) candidates.delete(parentSessionID); return [...candidates]; } function parseTaskSessionIDFromOutput( value: unknown, parentSessionID?: string, ): string | undefined { if (typeof value !== "string") return undefined; const matches = [...value.matchAll(/\b(?:task_id\s*:\s*)?(ses_[a-zA-Z0-9_-]+)\b/gi)]; const candidates = new Set(matches.map((match) => match[1])); if (parentSessionID) candidates.delete(parentSessionID); return candidates.size === 1 ? [...candidates][0] : undefined; } function backfillSyntheticTargetsForSession( state: StatuslineState, session: { id: string; parentID: string; messageID?: string; updatedAt?: string; }, ): boolean { const targetlessSynthetic = Object.values(state.children).filter( (child) => (child.source === "tool" || child.source === "subtask") && !child.targetSessionID && child.parentID === session.parentID, ); const messageMatches = session.messageID ? targetlessSynthetic.filter( (child) => child.messageID === session.messageID, ) : []; const existingSessionSiblings = Object.values(state.children).filter( (child) => child.id !== session.id && (child.source === "session" || child.id.startsWith("ses_")) && child.parentID === session.parentID, ); const candidates = messageMatches.length > 0 ? messageMatches : targetlessSynthetic; if (candidates.length !== 1) return false; if (messageMatches.length === 0 && existingSessionSiblings.length > 0) { return false; } const synthetic = candidates[0]; const targetSessionID = resolveSyntheticTargetSessionID( state, { id: synthetic.id, parentID: synthetic.parentID, messageID: synthetic.messageID, }, [session.id], ); if (targetSessionID !== session.id) return false; return upsertChildDetails(state, synthetic.id, { targetSessionID, updatedAt: session.updatedAt, }); } export function extractTaskToolEvidence( event: EventLike, ): TaskToolEvidence | null { const part = event.properties?.part; if (!isRecord(part) || part.type !== "tool") return null; if (asString(part.tool) !== "task") return null; const state = isRecord(part.state) ? part.state : undefined; if (!state) return null; const rawStatus = asString(state.status); const status: TaskToolStatus = rawStatus === "completed" ? "done" : rawStatus === "error" ? "error" : "running"; const metadata = isRecord(state.metadata) ? state.metadata : undefined; const targetFromMetadata = asString(metadata?.sessionId); const parentSessionID = asString(part.sessionID) ?? extractSessionID(event); const targetFromOutput = parseTaskSessionIDFromOutput( state.output, parentSessionID, ); const targetCandidates = extractPartTargetSessionCandidates(event); const targetSessionID = targetFromMetadata ?? targetFromOutput ?? (targetCandidates.length === 1 ? targetCandidates[0] : undefined); const endedAt = status === "done" || status === "error" ? extractEventTimestamp(event, ["completed", "end", "ended", "updated"]) : undefined; return { status, targetSessionID, endedAt, }; } function mapTaskToolToSubtaskID( state: StatuslineState, task: { parentID: string; messageID: string; parentMessageID?: string; title: string; summary?: string; agentName?: string; targetSessionID?: string; }, ): string | undefined { const runningSubtasks = Object.values(state.children).filter( (child) => child.source === "subtask" && child.status === "running" && child.parentID === task.parentID, ); const primaryCandidates = runningSubtasks.filter( (child) => child.messageID === task.messageID, ); const legacyCandidates = task.parentMessageID ? runningSubtasks.filter( (child) => child.messageID === task.parentMessageID, ) : []; const candidates = primaryCandidates.length > 0 ? primaryCandidates : legacyCandidates; if (candidates.length === 0) return undefined; if (task.targetSessionID) { const byTarget = candidates.filter( (child) => child.targetSessionID === task.targetSessionID, ); if (byTarget.length === 1) return byTarget[0].id; } const byTitle = candidates.filter((child) => sameDisplayText(child.title, task.title), ); if (byTitle.length === 1) return byTitle[0].id; const bySummary = candidates.filter((child) => sameDisplayText(child.summary, task.summary), ); if (bySummary.length === 1) return bySummary[0].id; const byAgent = task.agentName ? candidates.filter((child) => sameDisplayText(child.agentName, task.agentName), ) : []; if (byAgent.length === 1) return byAgent[0].id; if (candidates.length === 1) return candidates[0].id; return undefined; } function extractParentMessageID(event: EventLike): string | undefined { return ( asString(event.properties?.info?.parentID) ?? asString(event.properties?.parentID) ?? asString(event.parentID) ); } function toIsoTimestamp(value: unknown): string | undefined { if (typeof value === "string") { if (value.trim().length === 0) return undefined; const parsed = Date.parse(value); if (Number.isNaN(parsed)) return undefined; return new Date(parsed).toISOString(); } if (typeof value === "number" && Number.isFinite(value)) { if (value <= 0) return undefined; const millis = value < 10_000_000_000 ? value * 1000 : value; const parsed = new Date(millis); return Number.isNaN(parsed.getTime()) ? undefined : parsed.toISOString(); } return undefined; } function extractEventTimestamp( event: EventLike, keys: string[], ): string | undefined { const part = isRecord(event.properties?.part) ? event.properties?.part : undefined; const state = isRecord(part?.state) ? part?.state : undefined; const sources = [ isRecord(event.properties?.info?.time) ? event.properties?.info?.time : undefined, isRecord(part?.time) ? part?.time : undefined, isRecord(part?.timestamps) ? part?.timestamps : undefined, isRecord(state?.time) ? state?.time : undefined, isRecord(state?.timestamps) ? state?.timestamps : undefined, state, part, ]; for (const source of sources) { if (!source) continue; for (const key of keys) { const candidate = toIsoTimestamp(source[key]); if (candidate) return candidate; } } return undefined; } function extractSubtaskChild(event: EventLike): SubtaskChild | null { const part = event.properties?.part; if (!isRecord(part) || part.type !== "subtask") return null; const partID = asString(part.id); const parentID = asString(part.sessionID) ?? extractSessionID(event); const messageID = asString(part.messageID); if (!partID || !parentID || !messageID) return null; const description = asString(part.description); const command = asString(part.command); const agent = asString(part.agent); const title = description || command || agent || "subtask"; const state = isRecord(part.state) ? part.state : undefined; const input = isRecord(state?.input) ? state.input : undefined; const summary = firstDistinctSummary( [input?.prompt, input?.description, part.description, state?.description], title, ); const startedAt = extractEventTimestamp(event, [ "started", "start", "created", "updated", ]); const updatedAt = extractEventTimestamp(event, ["updated", "created", "started", "start"]) ?? startedAt; const targetCandidates = extractPartTargetSessionCandidates(event); const targetSessionID = targetCandidates.length === 1 ? targetCandidates[0] : undefined; return { id: `subtask:${partID}`, title, summary, agentName: agent, parentID, messageID, targetSessionID, startedAt, updatedAt, }; } function extractToolChild(event: EventLike): ToolChild | null { const part = event.properties?.part; if (!isRecord(part) || part.type !== "tool") return null; const tool = asString(part.tool); if (tool !== "delegate" && tool !== "task") return null; const partID = asString(part.id); const parentID = asString(part.sessionID) ?? extractSessionID(event); const messageID = asString(part.messageID); const state = isRecord(part.state) ? part.state : undefined; if (!partID || !parentID || !messageID || !state) return null; const taskEvidence = extractTaskToolEvidence(event); const rawStatus = asString(state.status); const status = taskEvidence?.status ?? (rawStatus === "completed" ? "done" : rawStatus === "error" ? "error" : "running"); const input = isRecord(state.input) ? state.input : {}; const description = asString(input.description); const subagentType = asString(input.subagent_type); const rawTitle = asString(state.title); const title = (isTechnicalDelegationTitle(rawTitle) ? undefined : rawTitle) || description || firstUsefulTitle([input.prompt, part.description, state.description]) || subagentType || tool; const summary = firstDistinctSummary( [input.prompt, input.description, part.description, state.description], title, ); const startedAt = extractEventTimestamp(event, [ "started", "start", "created", "updated", ]); const updatedAt = extractEventTimestamp(event, [ "updated", "completed", "created", "started", "start", ]) ?? startedAt; const endedAt = status === "done" || status === "error" ? extractEventTimestamp(event, ["completed", "end", "ended", "updated"]) : undefined; const targetCandidates = extractPartTargetSessionCandidates(event); const targetSessionID = taskEvidence?.targetSessionID ?? (targetCandidates.length === 1 ? targetCandidates[0] : undefined); return { id: `tool:${partID}`, title, summary, agentName: subagentType, parentID, messageID, toolName: tool, targetSessionID, status, startedAt, updatedAt, endedAt, }; } function extractCompletedAssistantMessage(event: EventLike): { sessionID: string; messageID: string; } | null { const info = event.properties?.info; if (!isRecord(info)) return null; if (info.role !== "assistant") return null; const time = info.time; if (!isRecord(time) || typeof time.completed !== "number") return null; const sessionID = asString(info.sessionID) ?? extractSessionID(event); const messageID = asString(info.id); if (!sessionID || !messageID) return null; return { sessionID, messageID }; } function extractDetailTargetIDs(event: EventLike): string[] { const ids = new Set(); const part = event.properties?.part; if (isRecord(part)) { const partID = asString(part.id); if (part.type === "subtask" && partID) { ids.add(`subtask:${partID}`); } if (part.type === "tool") { const tool = asString(part.tool); if ((tool === "delegate" || tool === "task") && partID) { ids.add(`tool:${partID}`); } } } const sessionID = extractSessionID(event); if (sessionID) ids.add(sessionID); return [...ids]; } function normalizePercent(value: number): number { if (value > 0 && value <= 1) { return value * 100; } return value; } export function extractChildDetails(event: EventLike): { title?: string; summary?: string; agentName?: string; tokens?: ChildTokenState; updatedAt?: string; } { const details: { title?: string; summary?: string; agentName?: string; tokens?: ChildTokenState; updatedAt?: string; } = {}; details.updatedAt = extractEventTimestamp(event, [ "updated", "completed", "created", "started", "start", ]); const titleCandidates = [ event.properties?.info?.title, event.properties?.title, event.properties?.info?.name, event.properties?.name, event.title, event.name, ]; for (const candidate of titleCandidates) { const title = asString(candidate); if (title) { details.title = title; break; } } const part = isRecord(event.properties?.part) ? event.properties.part : undefined; const partState = isRecord(part?.state) ? part.state : undefined; const partInput = isRecord(partState?.input) ? partState.input : undefined; details.agentName = asString(partInput?.subagent_type) ?? asString(partInput?.agent) ?? asString(part?.agent) ?? asString(event.properties?.info?.agent) ?? asString(event.properties?.info?.subagent_type); details.summary = firstDistinctSummary( [ partInput?.prompt, partInput?.description, part?.description, partState?.description, ], details.title, ); if (isTechnicalDelegationTitle(details.title)) { const replacementTitle = asString(partInput?.description) ?? firstUsefulTitle([ partInput?.prompt, part?.description, partState?.description, ]); if (replacementTitle) { details.title = replacementTitle; } } const tokenHints: ChildTokenState = {}; const visited = new Set(); const walk = (node: unknown, depth: number): void => { if (!isRecord(node) || depth > 6) return; if (visited.has(node)) return; visited.add(node); for (const [rawKey, rawValue] of Object.entries(node)) { const key = rawKey.toLowerCase(); const asNumber = typeof rawValue === "number" ? rawValue : typeof rawValue === "string" && rawValue.trim().length > 0 ? Number(rawValue) : undefined; if (typeof asNumber === "number" && Number.isFinite(asNumber)) { if (key.includes("context") && key.includes("percent")) { tokenHints.contextPercent = normalizePercent(asNumber); } else if (key.includes("context") && key.includes("usage")) { tokenHints.contextPercent = normalizePercent(asNumber); } else if ( (key.includes("input") || key.includes("prompt")) && key.includes("token") ) { tokenHints.input = asNumber; } else if ( (key.includes("output") || key.includes("completion")) && key.includes("token") ) { tokenHints.output = asNumber; } else if (key.includes("total") && key.includes("token")) { tokenHints.total = asNumber; } else if (key === "tokens" || key === "token") { tokenHints.total = asNumber; } } if (isRecord(rawValue)) { walk(rawValue, depth + 1); } } }; walk(event, 0); if ( tokenHints.input !== undefined || tokenHints.output !== undefined || tokenHints.total !== undefined || tokenHints.contextPercent !== undefined ) { details.tokens = tokenHints; } return details; } export function applySubagentEvent( state: StatuslineState, event: unknown, ): boolean { const e = (event ?? {}) as EventLike; const type = asString(e.type); if (!type) return false; if (type === "session.created" || type === "session.updated") { const child = extractCreatedChild(e); if (child) { const details = extractChildDetails(e); let changed = upsertRunningChild(state, { ...child, source: "session", targetSessionID: child.id, }); changed = upsertChildDetails(state, child.id, details) || changed; changed = backfillSyntheticTargetsForSession(state, { id: child.id, parentID: child.parentID, updatedAt: child.updatedAt, }) || changed; const sessionStatusFromUpdate = type === "session.updated" ? hasStructuredErrorEvidence(e.properties ?? e) ? "error" : deriveOpenCodeSessionStatus( e.properties?.status ?? e.properties?.state ?? e.properties?.info?.status ?? e.status ?? e.state, ) : undefined; if ( sessionStatusFromUpdate === "done" || sessionStatusFromUpdate === "error" ) { const endedAt = extractEventTimestamp(e, [ "completed", "end", "ended", "updated", ]); changed = markChildStatus(state, child.id, sessionStatusFromUpdate, endedAt) || changed; } return changed; } return false; } if (type === "session.idle") { const childID = extractSessionID(e); if (!childID) return false; const endedAt = extractEventTimestamp(e, [ "completed", "end", "ended", "updated", ]); const details = extractChildDetails(e); const status = deriveOpenCodeSessionStatus(e.properties ?? e) === "error" || hasStructuredErrorEvidence(e.properties ?? e) ? "error" : "done"; let changed = markChildStatus(state, childID, status, endedAt); changed = upsertChildDetails(state, childID, details) || changed; return changed; } if (type === "session.error") { const childID = extractSessionID(e); if (!childID) return false; const endedAt = extractEventTimestamp(e, [ "completed", "end", "ended", "updated", ]); const details = extractChildDetails(e); let changed = markChildStatus(state, childID, "error", endedAt); changed = upsertChildDetails(state, childID, details) || changed; return changed; } if (type === "session.status") { const childID = extractSessionID(e); if (!childID) return false; const status = hasStructuredErrorEvidence(e.properties ?? e) ? "error" : deriveOpenCodeSessionStatus( e.properties?.status ?? e.properties?.state ?? e.properties?.info?.status ?? e.status ?? e.state ?? e.properties, ); if (!status) return false; const endedAt = status === "done" || status === "error" ? extractEventTimestamp(e, ["completed", "end", "ended", "updated"]) : undefined; const details = extractChildDetails(e); let changed = status === "running" ? false : markChildStatus(state, childID, status, endedAt); changed = upsertChildDetails(state, childID, details) || changed; return changed; } let changed = false; if (type === "message.part.updated") { const subtask = extractSubtaskChild(e); if (subtask) { const targetSessionID = resolveSyntheticTargetSessionID( state, { id: subtask.id, parentID: subtask.parentID, messageID: subtask.messageID, }, subtask.targetSessionID ? [subtask.targetSessionID] : [], ); changed = upsertRunningChild(state, { ...subtask, source: "subtask", targetSessionID, startedAt: subtask.startedAt, updatedAt: subtask.updatedAt, }) || changed; } const tool = extractToolChild(e); if (tool) { const targetSessionID = resolveSyntheticTargetSessionID( state, { id: tool.id, parentID: tool.parentID, messageID: tool.messageID, }, tool.targetSessionID ? [tool.targetSessionID] : [], ); const childChanged = upsertRunningChild(state, { ...tool, source: "tool", targetSessionID, startedAt: tool.startedAt, updatedAt: tool.updatedAt, }); changed = childChanged || changed; if (tool.status === "done" || tool.status === "error") { changed = markChildStatus( state, tool.id, tool.status, tool.endedAt ?? tool.updatedAt, ) || changed; if ( asString( (e.properties?.part as Record | undefined)?.tool, ) === "task" ) { const subtaskID = mapTaskToolToSubtaskID(state, { parentID: tool.parentID, messageID: tool.messageID, parentMessageID: extractParentMessageID(e), title: tool.title, summary: tool.summary, agentName: tool.agentName, targetSessionID: targetSessionID, }); if (subtaskID) { if (targetSessionID) { changed = upsertChildDetails(state, subtaskID, { targetSessionID, updatedAt: tool.updatedAt, }) || changed; } changed = markChildStatus( state, subtaskID, tool.status, tool.endedAt ?? tool.updatedAt, ) || changed; } } } } } if (type === "message.updated") { const assistantModel = extractLatestAssistantModel(e.properties?.info ?? e); if (assistantModel) { changed = setChildModel( state, assistantModel.sessionID, assistantModel.model, assistantModel.updatedAt, ) || changed; } const completed = extractCompletedAssistantMessage(e); if (completed) { for (const child of Object.values(state.children)) { if ( child.source === "subtask" && child.status === "running" && child.parentID === completed.sessionID && child.messageID === completed.messageID ) { changed = markChildStatus(state, child.id, "done") || changed; } } } } if (type === "message.updated" || type === "message.part.updated") { const details = extractChildDetails(e); for (const childID of extractDetailTargetIDs(e)) { if (state.children[childID]) { changed = upsertChildDetails(state, childID, details) || changed; } } } return changed; }