@@ -24,6 +24,7 @@ import {
2424import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js" ;
2525import { withStateDirEnv } from "../test-helpers/state-dir-env.js" ;
2626import { withEnv } from "../test-utils/env.js" ;
27+ import { buildSingleRowStoreChildSessionsByKey } from "./session-utils-projection.js" ;
2728import {
2829 listSessionsFromStore ,
2930 loadCombinedSessionStoreForGateway ,
@@ -174,6 +175,81 @@ describe("listSessionsFromStore subagent metadata", () => {
174175 expect ( row . previousSessionId ) . toBe ( "sess-previous" ) ;
175176 } ) ;
176177
178+ test ( "discovers controlled children through both navigation and runtime owners" , ( ) => {
179+ const now = Date . now ( ) ;
180+ const navigationParentKey = "agent:main:dashboard:navigation-parent" ;
181+ const controlParentKey = "agent:main:subagent:runtime-controller" ;
182+ const staleParentKey = "agent:main:subagent:stale-controller" ;
183+ const childSessionKey = "agent:main:subagent:controlled-child" ;
184+ const store : Record < string , SessionEntry > = {
185+ [ navigationParentKey ] : {
186+ sessionId : "sess-navigation-parent" ,
187+ updatedAt : now - 2_000 ,
188+ } as SessionEntry ,
189+ [ controlParentKey ] : {
190+ sessionId : "sess-runtime-controller" ,
191+ updatedAt : now - 1_000 ,
192+ } as SessionEntry ,
193+ [ childSessionKey ] : {
194+ sessionId : "sess-controlled-child" ,
195+ updatedAt : now ,
196+ spawnedBy : staleParentKey ,
197+ parentSessionKey : navigationParentKey ,
198+ } as SessionEntry ,
199+ } ;
200+
201+ addSubagentRunForTests ( {
202+ runId : "run-controlled-child-dual-owner" ,
203+ childSessionKey,
204+ controllerSessionKey : controlParentKey ,
205+ requesterSessionKey : controlParentKey ,
206+ requesterDisplayKey : "runtime-controller" ,
207+ task : "controlled child" ,
208+ cleanup : "keep" ,
209+ createdAt : now - 5_000 ,
210+ startedAt : now - 4_000 ,
211+ } ) ;
212+
213+ const listForOwner = ( ownerSessionKey : string ) =>
214+ listSessionsFromStore ( {
215+ cfg,
216+ storePath : "/tmp/sessions.json" ,
217+ store,
218+ opts : { spawnedBy : ownerSessionKey } ,
219+ } ) ;
220+
221+ const navigationChildren = listForOwner ( navigationParentKey ) . sessions ;
222+ expect ( navigationChildren . map ( ( session ) => session . key ) ) . toEqual ( [ childSessionKey ] ) ;
223+ expect ( navigationChildren [ 0 ] ?. parentSessionKey ) . toBe ( navigationParentKey ) ;
224+ expect ( navigationChildren [ 0 ] ?. controlOwnerSessionKey ) . toBe ( controlParentKey ) ;
225+ expect ( listForOwner ( controlParentKey ) . sessions . map ( ( session ) => session . key ) ) . toEqual ( [
226+ childSessionKey ,
227+ ] ) ;
228+ expect ( listForOwner ( staleParentKey ) . sessions ) . toEqual ( [ ] ) ;
229+
230+ const all = listSessionsFromStore ( {
231+ cfg,
232+ storePath : "/tmp/sessions.json" ,
233+ store,
234+ opts : { } ,
235+ } ) ;
236+ expect (
237+ all . sessions . find ( ( session ) => session . key === navigationParentKey ) ?. childSessions ,
238+ ) . toEqual ( [ childSessionKey ] ) ;
239+ expect ( all . sessions . find ( ( session ) => session . key === controlParentKey ) ?. childSessions ) . toEqual (
240+ [ childSessionKey ] ,
241+ ) ;
242+
243+ expect (
244+ buildSingleRowStoreChildSessionsByKey ( {
245+ store,
246+ storePath : "/tmp/sessions.json" ,
247+ key : navigationParentKey ,
248+ now,
249+ } ) . get ( navigationParentKey ) ,
250+ ) . toEqual ( [ childSessionKey ] ) ;
251+ } ) ;
252+
177253 test ( "includes subagent status timing and direct child session keys" , ( ) => {
178254 const now = Date . now ( ) ;
179255 const store : Record < string , SessionEntry > = {
0 commit comments