@@ -139,15 +139,20 @@ export function buildBreakdownRows(input: BreakdownInput): readonly BreakdownRow
139139 metric ,
140140 ) ;
141141 const search = query . trim ( ) ;
142- // Threads matching the search, and every thread above them.
142+ // Threads whose title, or a model they used, matches the search, and every
143+ // thread above them.
143144 const matchedThreads = new Set < string > ( ) ;
144145 if ( search !== "" ) {
145- for ( const [ key , info ] of tree . info ) {
146- if ( info . title !== null && matchesQuery ( info . title , search ) ) {
147- for ( let up : string | undefined = key ; up !== undefined ; up = tree . parentOf . get ( up ) ) {
148- matchedThreads . add ( up ) ;
149- }
146+ const mark = ( key : string ) => {
147+ for ( let up : string | undefined = key ; up !== undefined ; up = tree . parentOf . get ( up ) ) {
148+ matchedThreads . add ( up ) ;
150149 }
150+ } ;
151+ for ( const [ key , info ] of tree . info ) {
152+ if ( info . title !== null && matchesQuery ( info . title , search ) ) mark ( key ) ;
153+ }
154+ for ( const fact of facts ) {
155+ if ( fact . thread !== null && matchesQuery ( fact . model , search ) ) mark ( fact . thread ) ;
151156 }
152157 }
153158 const threadMatches = ( key : string ) => search === "" || matchedThreads . has ( key ) ;
@@ -167,19 +172,34 @@ export function buildBreakdownRows(input: BreakdownInput): readonly BreakdownRow
167172 return cached ;
168173 } ;
169174
170- const itemMatches = ( dim : BreakdownDimension , key : string , scope : readonly UsageFact [ ] ) => {
171- if ( search === "" ) return true ;
172- if ( matchesQuery ( nameOf ( dim , key ) , search ) ) return true ;
173- if ( dim === "thread" ) return threadMatches ( key ) ;
174- // A project matches when one of its threads does.
175- return (
176- dim === "project" &&
177- scope . some (
178- ( fact ) =>
179- fact . project === key && fact . thread !== null && threadMatches ( tree . rootOf ( fact . thread ) ) ,
180- )
181- ) ;
182- } ;
175+ /**
176+ * Whether something an item lists under it matches the search: a project's
177+ * threads, a provider's accounts and models, an account's models, a model's
178+ * projects. Such an item stays and opens, so the match is reachable.
179+ */
180+ const childMatches = ( dim : BreakdownDimension , key : string , scope : readonly UsageFact [ ] ) =>
181+ scope . some ( ( fact ) => {
182+ if ( keyFor ( dim , fact , tree ) !== key ) return false ;
183+ switch ( dim ) {
184+ case "project" :
185+ return fact . thread !== null && threadMatches ( tree . rootOf ( fact . thread ) ) ;
186+ case "provider" :
187+ return (
188+ matchesQuery ( fact . model , search ) ||
189+ matchesQuery ( nameOf ( "account" , fact . account ) , search )
190+ ) ;
191+ case "account" :
192+ return matchesQuery ( fact . model , search ) ;
193+ case "model" :
194+ return matchesQuery ( nameOf ( "project" , fact . project ) , search ) ;
195+ case "thread" :
196+ return false ;
197+ }
198+ } ) ;
199+ const ownMatch = ( dim : BreakdownDimension , key : string ) =>
200+ matchesQuery ( nameOf ( dim , key ) , search ) || ( dim === "thread" && threadMatches ( key ) ) ;
201+ const itemMatches = ( dim : BreakdownDimension , key : string , scope : readonly UsageFact [ ] ) =>
202+ search === "" || ownMatch ( dim , key ) || childMatches ( dim , key , scope ) ;
183203
184204 const ranked = (
185205 dim : BreakdownDimension ,
@@ -225,7 +245,11 @@ export function buildBreakdownRows(input: BreakdownInput): readonly BreakdownRow
225245 : childDim !== null && depth < 3 && ! ( dim === "project" && ! scopeThreads ?. any ) ;
226246 // A search opens the path to its matches.
227247 const isOpen =
228- expandable && ( open . has ( path ) || ( search !== "" && depth === 0 && dim === "project" ) ) ;
248+ expandable &&
249+ ( open . has ( path ) ||
250+ ( search !== "" &&
251+ ( isThread ? liveChildren . some ( threadMatches ) : ! ownMatch ( dim , entry . key ) ) &&
252+ ( isThread || childMatches ( dim , entry . key , scope ) ) ) ) ;
229253 const familyCount = isThread ? ( scopeThreads ?. families . get ( entry . key ) ?. descendants ?? 0 ) : 0 ;
230254 rows . push ( {
231255 kind : "item" ,
0 commit comments