@@ -379,23 +379,6 @@ export function createEventHandlers(context: EventHandlerContext) {
379379 tui . requestRender ( ) ;
380380 } ;
381381
382- const collectTrackedSessionRunIds = ( ) => {
383- const runIds = new Set ( sessionRuns . keys ( ) ) ;
384- if ( state . activeChatRunId ) {
385- runIds . add ( state . activeChatRunId ) ;
386- }
387- const pendingRunId = getPendingSubmitAcceptedRunId ( state ) ;
388- if ( pendingRunId ) {
389- runIds . add ( pendingRunId ) ;
390- }
391- const finalizedRunIds = new Set ( finalizedRuns . keys ( ) ) ;
392- const displayedRunIds = new Set ( finalizedRunsWithDisplay . keys ( ) ) ;
393- for ( const runId of finalizedRunIds ) {
394- runIds . add ( runId ) ;
395- }
396- return { runIds, finalizedRunIds, displayedRunIds } ;
397- } ;
398-
399382 const handleSessionsChangedEvent = ( payload : unknown ) => {
400383 if ( ! payload || typeof payload !== "object" ) {
401384 return ;
@@ -416,7 +399,7 @@ export function createEventHandlers(context: EventHandlerContext) {
416399 }
417400 // Legacy atomic batches expose no replayable message identity. Refresh
418401 // their authoritative history without resetting the current run or stream.
419- runCoordinator . queueHistoryReload ( ) ;
402+ void runCoordinator . queueHistoryReload ( ) ;
420403 return ;
421404 }
422405
@@ -428,7 +411,11 @@ export function createEventHandlers(context: EventHandlerContext) {
428411 const displayedRunIds = finalizedRunsWithDisplay . has ( persistedRunId )
429412 ? [ persistedRunId ]
430413 : [ ] ;
431- runCoordinator . queueHistoryReload ( [ persistedRunId ] , [ persistedRunId ] , displayedRunIds ) ;
414+ void runCoordinator . queueHistoryReload (
415+ [ persistedRunId ] ,
416+ [ persistedRunId ] ,
417+ displayedRunIds ,
418+ ) ;
432419 } else {
433420 void refreshSessionInfo ?.( ) ;
434421 }
@@ -446,7 +433,7 @@ export function createEventHandlers(context: EventHandlerContext) {
446433 nextSessionId !== null &&
447434 state . currentSessionId !== nextSessionId ;
448435 if ( evt . reason === "new" && ! replacesKnownSession ) {
449- const { runIds, displayedRunIds } = collectTrackedSessionRunIds ( ) ;
436+ const { runIds, displayedRunIds } = runCoordinator . collectTrackedSessionRunIds ( ) ;
450437 if ( runIds . size > 0 ) {
451438 if ( nextSessionId ) {
452439 state . currentSessionId = nextSessionId ;
@@ -462,17 +449,14 @@ export function createEventHandlers(context: EventHandlerContext) {
462449 pendingNewSessionRunIds . add ( runId ) ;
463450 }
464451 }
465- runCoordinator . queueHistoryReload ( persistedRunIds , persistedRunIds , displayedRunIds ) ;
452+ void runCoordinator . queueHistoryReload ( persistedRunIds , persistedRunIds , displayedRunIds ) ;
466453 tui . requestRender ( ) ;
467454 return ;
468455 }
469456 }
470457
471- const {
472- runIds : reloadingRunIds ,
473- finalizedRunIds,
474- displayedRunIds,
475- } = collectTrackedSessionRunIds ( ) ;
458+ const { runIds, finalizedRunIds, displayedRunIds } =
459+ runCoordinator . collectTrackedSessionRunIds ( ) ;
476460 state . sessionGeneration = ( state . sessionGeneration ?? 0 ) + 1 ;
477461 // Reduce the old epoch before adopting its replacement ID; otherwise the
478462 // canonical reducer correctly rejects the reset as a foreign session.
@@ -491,11 +475,27 @@ export function createEventHandlers(context: EventHandlerContext) {
491475 state . sessionInfo . updatedAt = evt . updatedAt ;
492476 }
493477 getTuiSessionProjection ( state ) ;
494- if ( reloadingRunIds . size > 0 ) {
495- runCoordinator . queueHistoryReload ( reloadingRunIds , finalizedRunIds , displayedRunIds ) ;
496- } else {
497- runCoordinator . queueHistoryReload ( ) ;
498- }
478+ const selection = { sessionKey : state . currentSessionKey , agentId : state . currentAgentId } ;
479+ const generation = state . sessionGeneration ;
480+ const historyReload = runCoordinator . queueHistoryReload (
481+ runIds . size > 0 ? runIds : undefined ,
482+ finalizedRunIds ,
483+ displayedRunIds ,
484+ ) ;
485+ // The reset event can retire its own RPC acknowledgement. Its authoritative
486+ // receipt belongs after the last queued history rebuild, which clears chat.
487+ void historyReload . then ( ( current ) => {
488+ if (
489+ current &&
490+ evt . reason === "reset" &&
491+ generation === state . sessionGeneration &&
492+ matchesSelectedTuiSession ( state , selection ) &&
493+ selection . agentId === state . currentAgentId
494+ ) {
495+ chatLog . addSystem ( `session ${ state . currentSessionKey } reset` ) ;
496+ tui . requestRender ( ) ;
497+ }
498+ } ) ;
499499 tui . requestRender ( ) ;
500500 } ;
501501
@@ -739,9 +739,9 @@ export function createEventHandlers(context: EventHandlerContext) {
739739 type : "transportGap" ,
740740 scope : readTuiSessionProjectionScope ( state ) ,
741741 } ) ;
742- const { runIds, displayedRunIds } = collectTrackedSessionRunIds ( ) ;
742+ const { runIds, displayedRunIds } = runCoordinator . collectTrackedSessionRunIds ( ) ;
743743 if ( runIds . size === 0 ) {
744- runCoordinator . queueHistoryReload ( ) ;
744+ void runCoordinator . queueHistoryReload ( ) ;
745745 return ;
746746 }
747747 // A dropped final cannot distinguish a finished run from a still-streaming
0 commit comments