@@ -102,8 +102,15 @@ const DECISIONS_MS = 30_000
102102const LINEAGE_MS = 60_000
103103const SESSION_EVENTS = [ "session.created" , "session.updated" , "session.deleted" , "session.status" ]
104104
105- /** `project`: the selected project name, undefined for all projects, null while not yet known. */
106- export const createHostFeed = ( host : WorkHost , project : ( ) => string | undefined | null ) => {
105+ /** That host's handoff lineage, per editspace (lane names repeat across editspaces), and which editspace holds a
106+ * directory. `project`: the selected project, or "*" for All projects. */
107+ export type HostLineage = { project : string ; byEditspace : Record < string , { edges : Handoff [ ] ; owners : LaneOwner [ ] } > ; editspaceOf : ( directory : string ) => string | undefined }
108+
109+ const MAX_LINEAGE_EDITSPACES = 4
110+
111+ /** `project`: the selected project name, undefined for all projects, null while not yet known. `pinned`: directories of
112+ * this host's pinned sessions; in All projects, lineage is read only for the editspaces that hold them. */
113+ export const createHostFeed = ( host : WorkHost , project : ( ) => string | undefined | null , pinned : ( ) => string [ ] = ( ) => [ ] ) => {
107114 const base = `/hosts/${ host . id } `
108115 // Until the first read answers, show what this host last listed (if the project matches), marked by its age.
109116 const seen = ( ( ) => {
@@ -118,7 +125,7 @@ export const createHostFeed = (host: WorkHost, project: () => string | undefined
118125 ...( seen ? { cachedAt : seen . cachedAt } : { } ) ,
119126 } )
120127 const [ live , setLive ] = createSignal < Record < string , string > > ( { } )
121- const [ lineage , setLineage ] = createSignal < { project : string ; edges : Handoff [ ] ; owners : LaneOwner [ ] } > ( )
128+ const [ lineage , setLineage ] = createSignal < HostLineage > ( )
122129 let lastLineageRead = 0
123130 let lineageRead : Promise < void > | undefined
124131 let loading : Promise < void > | undefined
@@ -141,32 +148,47 @@ export const createHostFeed = (host: WorkHost, project: () => string | undefined
141148 . then ( ( list ) => Array . isArray ( list . records ) ? list . records . filter ( ( record ) => record . status === "open" ) : undefined )
142149 . catch ( ( ) => undefined )
143150 }
151+ const readLineage = async ( es : string ) => {
152+ const [ handoffs , dashboard ] = await Promise . all ( [
153+ read < { handoffs : Handoff [ ] } > ( `/es/api/handoffs?es=${ encodeURIComponent ( es ) } ` ) ,
154+ read < { editspace : string ; threads : { lanes ?: { lane : string ; owner ?: string } [ ] } [ ] } > ( `/es/api/state?es=${ encodeURIComponent ( es ) } ` ) ,
155+ ] )
156+ if ( dashboard . editspace !== es || ! Array . isArray ( handoffs . handoffs ) || ! Array . isArray ( dashboard . threads ) ) throw new Error ( "Invalid host lineage" )
157+ return {
158+ edges : handoffs . handoffs ,
159+ owners : dashboard . threads . flatMap ( ( thread ) => ( thread . lanes ?? [ ] ) . flatMap ( ( lane ) : LaneOwner [ ] =>
160+ lane . owner ? [ { lane : lane . lane , owner : lane . owner } ] : [ ] ) ) ,
161+ }
162+ }
144163 const refreshLineage = ( ) => {
145164 const current = scope
146- const es = current ?. es
147- if ( ! es || state ( ) . status !== "ready" || lineageRead || Date . now ( ) - lastLineageRead < LINEAGE_MS ) return
165+ // One project: its editspace. All projects: the editspaces holding this host's pinned sessions, found from that
166+ // host's own editspace roots.
167+ const all = ! ! current && current . project === undefined
168+ const key = all ? "*" : current ?. es
169+ if ( ! key || ( all && ! pinned ( ) . length ) || state ( ) . status !== "ready" || lineageRead || Date . now ( ) - lastLineageRead < LINEAGE_MS ) return
148170 lastLineageRead = Date . now ( )
149171 setLineage ( undefined )
172+ const same = ( ) => ( scope ?. project === undefined ? "*" : scope ?. es ) === key && state ( ) . status === "ready"
150173 lineageRead = ( async ( ) => {
151174 try {
152- const [ handoffs , dashboard ] = await Promise . all ( [
153- read < { handoffs : Handoff [ ] } > ( `/es/api/handoffs?es=${ encodeURIComponent ( es ) } ` ) ,
154- read < { editspace : string ; threads : { lanes ?: { lane : string ; owner ?: string } [ ] } [ ] } > ( `/es/api/state?es=${ encodeURIComponent ( es ) } ` ) ,
155- ] )
156- if ( dashboard . editspace !== es || ! Array . isArray ( handoffs . handoffs ) || ! Array . isArray ( dashboard . threads ) ) throw new Error ( "Invalid host lineage" )
157- if ( scope ?. es !== es || state ( ) . status !== "ready" ) return
158- setLineage ( {
159- project : es ,
160- edges : handoffs . handoffs ,
161- owners : dashboard . threads . flatMap ( ( thread ) => ( thread . lanes ?? [ ] ) . flatMap ( ( lane ) : LaneOwner [ ] =>
162- lane . owner ? [ { lane : lane . lane , owner : lane . owner } ] : [ ] ) ) ,
163- } )
175+ const roots = all
176+ ? ( await read < { editspaces : { name : string ; root : string } [ ] } > ( "/es/api/editspaces" ) ) . editspaces
177+ . map ( ( item ) => ( { name : item . name , base : item . root . replace ( / \/ + $ / , "" ) . replace ( / \/ \. e d i t s p a c e $ / , "" ) } ) )
178+ : [ { name : key , base : "" } ]
179+ const editspaceOf = ( directory : string ) => all
180+ ? roots . filter ( ( item ) => directory === item . base || directory . startsWith ( `${ item . base } /` ) ) . toSorted ( ( a , b ) => b . base . length - a . base . length ) [ 0 ] ?. name
181+ : key
182+ const names = [ ...new Set ( pinned ( ) . flatMap ( ( directory ) => editspaceOf ( directory ) ?? [ ] ) ) ] . slice ( 0 , MAX_LINEAGE_EDITSPACES )
183+ const lineages = await Promise . all ( ( all ? names : [ key ] ) . map ( async ( es ) => [ es , await readLineage ( es ) ] as const ) )
184+ if ( ! same ( ) ) return
185+ setLineage ( { project : key , byEditspace : Object . fromEntries ( lineages ) , editspaceOf } )
164186 } catch {
165- if ( scope ?. es === es ) setLineage ( undefined )
187+ if ( same ( ) ) setLineage ( undefined )
166188 }
167189 } ) ( ) . finally ( ( ) => {
168190 lineageRead = undefined
169- if ( scope ?. es !== es ) refreshLineage ( )
191+ if ( ! same ( ) ) refreshLineage ( )
170192 } )
171193 }
172194 const run = async ( ) => {
0 commit comments