55 */
66import type { DeliveryContext } from "../utils/delivery-context.types.js" ;
77import { isDeliverySuspended } from "./subagent-delivery-state.js" ;
8- import type { SubagentRunRecord } from "./subagent-registry.types.js" ;
8+ import type { SubagentRunReadRecord , SubagentRunRecord } from "./subagent-registry.types.js" ;
99import { compareSubagentRunGeneration } from "./subagent-run-generation.js" ;
1010import { hasSubagentRunEnded , isLiveUnendedSubagentRun } from "./subagent-run-liveness.js" ;
1111
12- function resolveControllerSessionKey ( entry : SubagentRunRecord ) : string {
12+ function resolveControllerSessionKey (
13+ entry : Pick < SubagentRunRecord , "controllerSessionKey" | "requesterSessionKey" > ,
14+ ) : string {
1315 return entry . controllerSessionKey ?. trim ( ) || entry . requesterSessionKey ;
1416}
1517
@@ -74,31 +76,31 @@ export function listRunsForControllerFromRuns(
7476 return results ;
7577}
7678
77- type LatestRunPair = {
79+ type LatestRunPair < T extends SubagentRunReadRecord = SubagentRunRecord > = {
7880 runId : string ;
79- entry : SubagentRunRecord ;
81+ entry : T ;
8082} ;
8183
8284/** Cached read index for display, controller grouping, and descendant queries. */
83- export type SubagentRunReadIndex = {
84- getDisplaySubagentRun ( childSessionKey : string ) : SubagentRunRecord | null ;
85- latestRunsByChildSessionKey : ReadonlyMap < string , SubagentRunRecord > ;
85+ export type SubagentRunReadIndex < T extends SubagentRunReadRecord = SubagentRunRecord > = {
86+ getDisplaySubagentRun ( childSessionKey : string ) : T | null ;
87+ latestRunsByChildSessionKey : ReadonlyMap < string , T > ;
8688 countActiveDescendantRuns ( rootSessionKey : string ) : number ;
8789 countPendingDescendantRuns ( rootSessionKey : string ) : number ;
8890 countPendingDescendantRunsExcludingRun ( rootSessionKey : string , excludeRunId : string ) : number ;
8991 hasDescendantRunAwaitingSettle ( rootSessionKey : string , excludeRunId ?: string ) : boolean ;
90- listDescendantRunsForRequester ( rootSessionKey : string ) : SubagentRunRecord [ ] ;
91- runsByControllerSessionKey : ReadonlyMap < string , readonly SubagentRunRecord [ ] > ;
92+ listDescendantRunsForRequester ( rootSessionKey : string ) : T [ ] ;
93+ runsByControllerSessionKey : ReadonlyMap < string , readonly T [ ] > ;
9294} ;
9395
9496export type LatestSubagentRunReadIndex = {
9597 getLatestSubagentRun ( childSessionKey : string ) : SubagentRunRecord | null ;
9698} ;
9799
98- function rememberLatestRunEntry (
99- map : Map < string , SubagentRunRecord > ,
100+ function rememberLatestRunEntry < T extends SubagentRunReadRecord > (
101+ map : Map < string , T > ,
100102 key : string ,
101- entry : SubagentRunRecord ,
103+ entry : T ,
102104) : void {
103105 const existing = map . get ( key ) ;
104106 if ( ! existing || compareSubagentRunGeneration ( entry , existing ) > 0 ) {
@@ -124,11 +126,11 @@ export function buildLatestSubagentRunReadIndexFromRuns(
124126 } ;
125127}
126128
127- function rememberLatestRunPair (
128- map : Map < string , LatestRunPair > ,
129+ function rememberLatestRunPair < T extends SubagentRunReadRecord > (
130+ map : Map < string , LatestRunPair < T > > ,
129131 key : string ,
130132 runId : string ,
131- entry : SubagentRunRecord ,
133+ entry : T ,
132134) : void {
133135 const existing = map . get ( key ) ;
134136 if ( ! existing || compareSubagentRunGeneration ( entry , existing . entry ) > 0 ) {
@@ -137,19 +139,19 @@ function rememberLatestRunPair(
137139}
138140
139141/** Builds a read index from snapshot and optional in-memory runs. */
140- export function buildSubagentRunReadIndexFromRuns ( params : {
141- runs : Map < string , SubagentRunRecord > ;
142- inMemoryRuns ?: Iterable < SubagentRunRecord > ;
142+ export function buildSubagentRunReadIndexFromRuns < T extends SubagentRunReadRecord > ( params : {
143+ runs : Map < string , T > ;
144+ inMemoryRuns ?: Iterable < T > ;
143145 now ?: number ;
144- } ) : SubagentRunReadIndex {
146+ } ) : SubagentRunReadIndex < T > {
145147 const { runs } = params ;
146148 const now = params . now ?? Date . now ( ) ;
147- const inMemoryDisplayByChildSessionKey = new Map < string , SubagentRunRecord > ( ) ;
148- const latestSnapshotActiveByChildSessionKey = new Map < string , SubagentRunRecord > ( ) ;
149- const latestSnapshotEndedByChildSessionKey = new Map < string , SubagentRunRecord > ( ) ;
150- const latestRunByChildSessionKey = new Map < string , LatestRunPair > ( ) ;
151- const runsByControllerSessionKey = new Map < string , SubagentRunRecord [ ] > ( ) ;
152- const latestRunByRequesterAndChildSessionKey = new Map < string , Map < string , LatestRunPair > > ( ) ;
149+ const inMemoryDisplayByChildSessionKey = new Map < string , T > ( ) ;
150+ const latestSnapshotActiveByChildSessionKey = new Map < string , T > ( ) ;
151+ const latestSnapshotEndedByChildSessionKey = new Map < string , T > ( ) ;
152+ const latestRunByChildSessionKey = new Map < string , LatestRunPair < T > > ( ) ;
153+ const runsByControllerSessionKey = new Map < string , T [ ] > ( ) ;
154+ const latestRunByRequesterAndChildSessionKey = new Map < string , Map < string , LatestRunPair < T > > > ( ) ;
153155 const activeDescendantCountBySessionKey = new Map < string , number > ( ) ;
154156 const pendingDescendantCountBySessionKey = new Map < string , number > ( ) ;
155157
@@ -188,18 +190,18 @@ export function buildSubagentRunReadIndexFromRuns(params: {
188190 }
189191 let latestByChild = latestRunByRequesterAndChildSessionKey . get ( requesterSessionKey ) ;
190192 if ( ! latestByChild ) {
191- latestByChild = new Map < string , LatestRunPair > ( ) ;
193+ latestByChild = new Map < string , LatestRunPair < T > > ( ) ;
192194 latestRunByRequesterAndChildSessionKey . set ( requesterSessionKey , latestByChild ) ;
193195 }
194196 rememberLatestRunPair ( latestByChild , childSessionKey , runId , entry ) ;
195197 }
196198
197- const latestRunsByChildSessionKey = new Map < string , SubagentRunRecord > ( ) ;
199+ const latestRunsByChildSessionKey = new Map < string , T > ( ) ;
198200 for ( const [ childSessionKey , pair ] of latestRunByChildSessionKey ) {
199201 latestRunsByChildSessionKey . set ( childSessionKey , pair . entry ) ;
200202 }
201203
202- const getDisplaySubagentRun = ( childSessionKey : string ) : SubagentRunRecord | null => {
204+ const getDisplaySubagentRun = ( childSessionKey : string ) : T | null => {
203205 const key = childSessionKey . trim ( ) ;
204206 if ( ! key ) {
205207 return null ;
@@ -214,7 +216,7 @@ export function buildSubagentRunReadIndexFromRuns(params: {
214216
215217 const forEachDescendantRun = (
216218 rootSessionKey : string ,
217- visitor : ( runId : string , entry : SubagentRunRecord ) => void | boolean ,
219+ visitor : ( runId : string , entry : T ) => void | boolean ,
218220 ) : void => {
219221 const root = rootSessionKey . trim ( ) ;
220222 if ( ! root ) {
@@ -327,8 +329,8 @@ export function buildSubagentRunReadIndexFromRuns(params: {
327329 stopAtFirst : true ,
328330 } ) > 0 ;
329331
330- const listDescendantRunsForRequester = ( rootSessionKey : string ) : SubagentRunRecord [ ] => {
331- const descendants : SubagentRunRecord [ ] = [ ] ;
332+ const listDescendantRunsForRequester = ( rootSessionKey : string ) : T [ ] => {
333+ const descendants : T [ ] = [ ] ;
332334 forEachDescendantRun ( rootSessionKey , ( _runId , entry ) => {
333335 descendants . push ( entry ) ;
334336 } ) ;
0 commit comments