@@ -113,6 +113,8 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
113113 let settling : string | null = null ;
114114 /** each pane's agent as the newest snapshot or status event has it, a snapshot's set before either subagent tracker reads it (owns) */
115115 const agentNow = new Map < string , string | null > ( ) ;
116+ /** the read (opencodeAgents.beginRead) each pane's agentNow came from: a snapshot read before a newer word never undoes it */
117+ const agentRead = new Map < string , number > ( ) ;
116118 /** each pane's agent as its status events have it, and since when: the rests from then on are that agent's (newLifetime) */
117119 const eventAgent = new Map < string , { agent : string | null ; since : number } > ( ) ;
118120 const statusNow = options . statusNow ?? Date . now ;
@@ -147,9 +149,15 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
147149 const found = readOpenCodeService ( options . openCodeStateDir ?? openCodeServiceOptions . stateDir ) ;
148150 if ( found === null ) return null ;
149151 if ( service ?. pid !== found . pid ) {
150- // not known until the lookup answers: meanwhile no run is taken for one the service before took along
152+ // until the lookup answers no run is taken for one the service before took along; then the store is read again
153+ // at once. A lookup with no answer is asked again
151154 const looked : { pid : number ; started : number | null } = service = { pid : found . pid , started : null } ;
152- void processStartUpperBoundAsync ( found . pid ) . then ( ( at ) => { looked . started = at ; } , ( ) => undefined ) ;
155+ void processStartUpperBoundAsync ( found . pid ) . then ( ( at ) => {
156+ if ( service !== looked ) return ;
157+ if ( at === null ) { service = null ; return ; }
158+ looked . started = at ;
159+ opencodeAgents . poll ( ) ;
160+ } , ( ) => { if ( service === looked ) service = null ; } ) ;
153161 }
154162 return service . started ;
155163 } ,
@@ -181,16 +189,26 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
181189 /** herdr's snapshot with OmO's and GJC's own status in it: what the completion tracker and web push are given */
182190 const rawSnapshot = async ( ) : Promise < SessionSnapshot > => {
183191 const promptCheckpoint = options . openCodeState ?. checkpoint ( ) ;
192+ const read = opencodeAgents . beginRead ( ) ;
184193 const snapshot = await omoSnapshot ( ) ;
185194 await gjc . refresh ( snapshot . panes ) ;
186195 const named = gjc . apply ( omo . apply ( snapshot ) ) ;
187- agentNow . clear ( ) ;
188- for ( const pane of named . panes ) agentNow . set ( pane . pane_id , pane . agent ?? null ) ;
189- for ( const paneId of eventAgent . keys ( ) ) if ( ! agentNow . has ( paneId ) ) eventAgent . delete ( paneId ) ;
196+ const ids = new Set ( named . panes . map ( ( pane ) => pane . pane_id ) ) ;
197+ for ( const paneId of [ ...agentNow . keys ( ) ] ) {
198+ if ( ids . has ( paneId ) || ( agentRead . get ( paneId ) ?? 0 ) > read ) continue ;
199+ agentNow . delete ( paneId ) ;
200+ agentRead . delete ( paneId ) ;
201+ eventAgent . delete ( paneId ) ;
202+ }
203+ for ( const pane of named . panes ) {
204+ if ( ( agentRead . get ( pane . pane_id ) ?? 0 ) > read ) continue ;
205+ agentNow . set ( pane . pane_id , pane . agent ?? null ) ;
206+ agentRead . set ( pane . pane_id , read ) ;
207+ }
190208 // not waited for: finding a transcript costs process calls, and a snapshot must not wait on them.
191209 // The plans come after: a Claude pane's transcript is the one the subagent tracker found
192210 void claudeAgents . refresh ( named . panes ) . catch ( ( ) => undefined ) . then ( ( ) => plans . refresh ( named . panes ) ) . catch ( ( ) => undefined ) ;
193- opencodeAgents . refresh ( named . panes ) ;
211+ opencodeAgents . refresh ( named . panes , read ) ;
194212 // an agent this server started has had no turn yet: its start screen is no work (launched-agents.ts)
195213 for ( const pane of named . panes ) pane . agent_status = launchedAgents . settle ( pane . pane_id , pane . agent_status ) ;
196214 options . openCodeState ?. reconcile ( named . panes , promptCheckpoint ) ;
@@ -338,6 +356,7 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
338356 if ( machines ) machines . localAgents ( named ) ; else bridgeAgents ! . status ( named ) ;
339357 // newer than the last snapshot: a route that looks the pane's session up before the next one reads it so
340358 agentNow . set ( paneId , named [ 0 ] ! . agent ?? null ) ;
359+ agentRead . set ( paneId , opencodeAgents . beginRead ( ) ) ;
341360 if ( eventAgent . get ( paneId ) ?. agent !== ( named [ 0 ] ! . agent ?? null ) ) eventAgent . set ( paneId , { agent : named [ 0 ] ! . agent ?? null , since : statusNow ( ) } ) ;
342361 // herdr says `claude/idle` for an OmO pane whatever it does, and a provider child's work for
343362 // a GJC one while its transcript is tracked: their own status stands, and no Claude turn waits there
@@ -371,7 +390,8 @@ export function createStatusHub<Owner, Lease>({ options, devices, preferences, c
371390 // an OpenCode turn whose session is not known yet: its pane is looked up now, not at the next snapshot, so a
372391 // turn that ended on its subagents is held while the finish alert still waits (and is called off)
373392 if ( tracker === opencodeAgents && opencodeAgents . sessionOf ( paneId ) ?. live !== true ) {
374- void paneGet ( paneId ) . then ( ( pane ) => { if ( opencodeAgents . ensure ( pane ) ) opencodeAgents . poll ( paneId ) ; } ) . catch ( ( ) => undefined ) ;
393+ const begun = opencodeAgents . beginRead ( ) ;
394+ void paneGet ( paneId ) . then ( ( pane ) => { if ( opencodeAgents . ensure ( pane , begun ) ) opencodeAgents . poll ( paneId ) ; } ) . catch ( ( ) => undefined ) ;
375395 }
376396 } ,
377397 // a finish reported as done, now in front at herdr's terminal: seen, idle again
0 commit comments