@@ -141,7 +141,12 @@ export function foldTaskAgentEvent(
141141 const summary = "summary" in payload ? payload . summary : undefined ;
142142 const nextUsage = "usage" in payload ? taskUsage ( payload . usage ) : undefined ;
143143 const lastToolName = "lastToolName" in payload ? payload . lastToolName : undefined ;
144- const currentActivity = summary ?? lastToolName ?? previous ?. currentActivity ;
144+ const settledNow = status === "idle" || THREAD_AGENT_TERMINAL_STATUSES . has ( status ) ;
145+ // A settled agent's card shows its result/error, not a stale in-flight
146+ // activity line ("Reading files…" on a failed card).
147+ const currentActivity = settledNow
148+ ? undefined
149+ : ( summary ?? lastToolName ?? previous ?. currentActivity ) ;
145150 const activityChanged =
146151 ( summary !== undefined && summary !== previous ?. currentActivity ) ||
147152 ( lastToolName !== undefined && lastToolName !== previous ?. lastToolName ) ;
@@ -154,15 +159,23 @@ export function foldTaskAgentEvent(
154159 const wasSettled =
155160 previous !== undefined &&
156161 ( previous . status === "idle" || THREAD_AGENT_TERMINAL_STATUSES . has ( previous . status ) ) ;
157- const reactivated = wasSettled && status === "running" ;
162+ // Any non-settled status counts as a fresh activation — an idle Codex agent
163+ // resumed via a waiting approval still increments, not just idle→running.
164+ const reactivated =
165+ wasSettled && status !== "idle" && ! THREAD_AGENT_TERMINAL_STATUSES . has ( status ) ;
158166 const explicitEndTime = event . type === "task.updated" ? event . payload . endTime : undefined ;
159167 const terminal = THREAD_AGENT_TERMINAL_STATUSES . has ( status ) ;
168+ // Re-derive kind whenever this event carries a taskType/parent — a child
169+ // registered from an early notification (before its parent linkage arrived)
170+ // must not stay pinned to a first-guess "other".
171+ const eventKind = taskKind (
172+ "taskType" in payload ? payload . taskType : undefined ,
173+ payload . parentTaskId ,
174+ ) ;
160175 const next : ThreadAgentSnapshot = {
161176 agentId : payload . taskId ,
162177 provider : event . provider ,
163- kind :
164- previous ?. kind ??
165- taskKind ( "taskType" in payload ? payload . taskType : undefined , payload . parentTaskId ) ,
178+ kind : eventKind !== "other" ? eventKind : ( previous ?. kind ?? eventKind ) ,
166179 name : payload . name ?? description ?? payload . workflowName ?? previous ?. name ?? payload . taskId ,
167180 ...( ( payload . agentType ?? previous ?. agentType )
168181 ? { agentType : payload . agentType ?? previous ?. agentType }
@@ -240,8 +253,11 @@ export function foldTaskAgentEvent(
240253}
241254
242255export function pruneSettledAgents ( agents : Map < string , ThreadAgentSnapshot > ) : void {
256+ // Idle agents are resumable identities, not history — only terminal agents
257+ // compete for the retention pool, so re-activating an old idle Codex child
258+ // never loses its usage/activation record to pruning.
243259 const settled = Array . from ( agents . values ( ) )
244- . filter ( ( agent ) => agent . status === "idle" || THREAD_AGENT_TERMINAL_STATUSES . has ( agent . status ) )
260+ . filter ( ( agent ) => THREAD_AGENT_TERMINAL_STATUSES . has ( agent . status ) )
245261 . sort ( ( left , right ) => left . lastActivityAt . localeCompare ( right . lastActivityAt ) ) ;
246262 for ( const agent of settled . slice (
247263 0 ,
@@ -920,6 +936,10 @@ const make = Effect.gen(function* () {
920936 // client's latest-wins scan) always finds one while agents exist.
921937 const activitiesSinceAgentSnapshot = new Map < ThreadId , number > ( ) ;
922938 const AGENT_SNAPSHOT_REFRESH_ACTIVITY_COUNT = 400 ;
939+ // Monotonic per-thread roster revision. Snapshot activities carry it so the
940+ // client picks the newest roster deterministically even when two appends
941+ // share a createdAt millisecond. Seeded from the hydrated snapshot.
942+ const agentRosterRevision = new Map < ThreadId , number > ( ) ;
923943 let agentSnapshotDispatchCount = 0 ;
924944 const providerCommandId = ( event : ProviderRuntimeEvent , tag : string ) =>
925945 crypto . randomUUIDv4 . pipe (
@@ -1533,7 +1553,12 @@ const make = Effect.gen(function* () {
15331553 return loadedThreadDetail ;
15341554 } ) ;
15351555
1536- if ( ! hydratedAgentThreads . has ( thread . id ) ) {
1556+ // Hydrate lazily, only when an event actually touches the roster —
1557+ // never on ordinary traffic (message deltas would otherwise trigger a
1558+ // full thread-detail load per thread after every restart, serialized
1559+ // through the ingestion worker).
1560+ const eventTouchesAgents = isTaskAgentEvent ( event ) || event . type === "session.exited" ;
1561+ if ( eventTouchesAgents && ! hydratedAgentThreads . has ( thread . id ) ) {
15371562 // Hydration source is the 500-capped activities projection — the same
15381563 // list clients derive from. While agents exist, the refresh counter
15391564 // below re-publishes the roster every ~400 activities so the latest
@@ -1556,8 +1581,11 @@ const make = Effect.gen(function* () {
15561581 ) ;
15571582 const payload =
15581583 latestSnapshot ?. payload !== null && typeof latestSnapshot ?. payload === "object"
1559- ? ( latestSnapshot . payload as { agents ?: unknown } )
1584+ ? ( latestSnapshot . payload as { agents ?: unknown ; revision ?: unknown } )
15601585 : undefined ;
1586+ if ( typeof payload ?. revision === "number" && Number . isInteger ( payload . revision ) ) {
1587+ agentRosterRevision . set ( thread . id , payload . revision ) ;
1588+ }
15611589 const agents = new Map < string , ThreadAgentSnapshot > ( ) ;
15621590 if ( Array . isArray ( payload ?. agents ) ) {
15631591 for ( const candidate of payload . agents ) {
@@ -1615,14 +1643,15 @@ const make = Effect.gen(function* () {
16151643 const settled = roster . length - active ;
16161644 const count = active > 0 ? active : settled ;
16171645 const summary = `${ count } ${ count === 1 ? "agent" : "agents" } ${ active > 0 ? "active" : "settled" } ` ;
1646+ const nextRevision = ( agentRosterRevision . get ( thread . id ) ?? 0 ) + 1 ;
16181647 const snapshotUuid = yield * crypto . randomUUIDv4 ;
16191648 const activity : OrchestrationThreadActivity = {
16201649 id : EventId . make ( `${ event . eventId } :agent-snapshot:${ snapshotUuid } ` ) ,
16211650 createdAt : event . createdAt ,
16221651 tone : "info" ,
16231652 kind : THREAD_AGENTS_ACTIVITY_KIND ,
16241653 summary,
1625- payload : { agents : roster } ,
1654+ payload : { agents : roster , revision : nextRevision } ,
16261655 turnId : toTurnId ( event . turnId ) ?? null ,
16271656 } ;
16281657 // The in-memory roster is authoritative (already folded); if the
@@ -1646,6 +1675,7 @@ const make = Effect.gen(function* () {
16461675 Effect . gen ( function * ( ) {
16471676 pendingRosterRedispatch . delete ( thread . id ) ;
16481677 activitiesSinceAgentSnapshot . set ( thread . id , 0 ) ;
1678+ agentRosterRevision . set ( thread . id , nextRevision ) ;
16491679 agentSnapshotDispatchCount += 1 ;
16501680 yield * Effect . logDebug ( "provider agent snapshot appended" , {
16511681 threadId : thread . id ,
@@ -1673,6 +1703,7 @@ const make = Effect.gen(function* () {
16731703 agentsByThread . delete ( thread . id ) ;
16741704 hydratedAgentThreads . delete ( thread . id ) ;
16751705 activitiesSinceAgentSnapshot . delete ( thread . id ) ;
1706+ agentRosterRevision . delete ( thread . id ) ;
16761707 }
16771708
16781709 const now = event . createdAt ;
0 commit comments