11import type { SessionInfo , SessionMessageAssistant , SessionMessageInfo , ShellInfo } from "@opencode/client/promise"
2+ import { scanSubagentTranscript } from "@opencode/session-ui/subagent-transcript"
3+ import { Predicate } from "effect"
24import { createMemo } from "solid-js"
35
46type Task =
@@ -14,54 +16,53 @@ export function createSessionBackground(input: {
1416} ) {
1517 const history = createMemo ( ( ) => {
1618 const completed = new Set < string > ( )
17- const subagents : { id : string ; type : "subagent" ; label : string ; agent : string | undefined } [ ] = [ ]
1819 const shells : { partID : string ; task : { id : string ; type : "shell" ; label : string } } [ ] = [ ]
1920 const id = input . sessionID ( )
21+ const messages = id ? input . messages ( id ) : [ ]
22+ const scan = scanSubagentTranscript ( messages )
2023
21- const assistant = ( id ? input . messages ( id ) : [ ] ) . reduce < SessionMessageAssistant | undefined > ( ( latest , message ) => {
22- if ( message . type === "synthetic" ) {
23- if ( message . metadata ?. source === "subagent" && typeof message . metadata . childID === "string" )
24- completed . add ( message . metadata . childID )
24+ for ( const childID of scan . completed ) completed . add ( childID )
2525
26- if ( message . metadata ?. source === "shell" ) {
27- if ( typeof message . metadata . shellID === "string ") completed . add ( message . metadata . shellID )
26+ for ( const message of messages ) {
27+ if ( message . type !== "synthetic ") continue
2828
29- if ( typeof message . metadata . jobID === "string " ) completed . add ( message . metadata . jobID )
30- }
29+ if ( message . metadata ?. source === "shell " ) {
30+ if ( Predicate . isString ( message . metadata . shellID ) ) completed . add ( message . metadata . shellID )
3131
32- return latest
32+ if ( Predicate . isString ( message . metadata . jobID ) ) completed . add ( message . metadata . jobID )
3333 }
34+ }
35+
36+ const subagents = scan . calls . flatMap ( ( call ) => {
37+ if ( ! call . backgrounded || ! call . childID || completed . has ( call . childID ) ) return [ ]
38+
39+ return [
40+ {
41+ id : call . childID ,
42+ type : "subagent" as const ,
43+ label : call . description ?? call . childID ,
44+ agent : call . agent ,
45+ } ,
46+ ]
47+ } )
48+
49+ const assistant = messages . reduce < SessionMessageAssistant | undefined > ( ( latest , message ) => {
50+ if ( message . type === "synthetic" ) return latest
3451
3552 if ( message . type !== "assistant" ) return latest
3653 message . content . forEach ( ( part ) => {
37- if ( part . type !== "tool" || ( part . name !== "subagent" && part . name !== " shell") ) return
54+ if ( part . type !== "tool" || part . name !== "shell" ) return
3855
3956 if ( part . state . status !== "completed" || part . state . metadata ?. status !== "running" ) return
4057
41- if ( part . name === "subagent" ) {
42- const sessionID = part . state . metadata . sessionID
43-
44- if ( typeof sessionID !== "string" ) return
45- const description = part . state . input . description
46- const agent = part . state . input . agent
47- subagents . push ( {
48- id : sessionID ,
49- type : "subagent" ,
50- label : typeof description === "string" ? description : sessionID ,
51- agent : typeof agent === "string" ? agent : undefined ,
52- } )
53-
54- return
55- }
56-
5758 const shellID = part . state . metadata . shellID
5859 const command = part . state . input . command
5960 shells . push ( {
6061 partID : part . id ,
6162 task : {
62- id : typeof shellID === "string" ? shellID : part . id ,
63+ id : Predicate . isString ( shellID ) ? shellID : part . id ,
6364 type : "shell" ,
64- label : typeof command === "string" ? command : part . id ,
65+ label : Predicate . isString ( command ) ? command : part . id ,
6566 } ,
6667 } )
6768 } )
@@ -70,11 +71,11 @@ export function createSessionBackground(input: {
7071 } , undefined )
7172
7273 return {
73- // Completion notices can identify the shell or its original tool call .
74- subagents : subagents . filter ( ( task ) => ! completed . has ( task . id ) ) ,
75- shells : shells
76- . filter ( ( item ) => ! completed . has ( item . partID ) && ! completed . has ( item . task . id ) )
77- . map ( ( item ) => item . task ) ,
74+ // Backgrounded calls stay listed until their synthetic completion notice lands (filtered at construction) .
75+ subagents,
76+ shells : shells . flatMap ( ( item ) =>
77+ ! completed . has ( item . partID ) && ! completed . has ( item . task . id ) ? [ item . task ] : [ ] ,
78+ ) ,
7879 blocking :
7980 assistant ?. content . flatMap ( ( part ) => {
8081 if ( part . type !== "tool" || part . state . status !== "running" ) return [ ]
@@ -85,10 +86,12 @@ export function createSessionBackground(input: {
8586
8687 return [
8788 {
89+ // SAFETY: the guard above leaves only "shell" and "subagent" names.
90+ // oxlint-disable-next-line anti-slop/require-safety-comment-for-type-assertion -- see SAFETY above
8891 type : part . name as "shell" | "subagent" ,
8992 partID : part . id ,
90- id : typeof value === "string" ? value : undefined ,
91- label : typeof label === "string" ? label : undefined ,
93+ id : Predicate . isString ( value ) ? value : undefined ,
94+ label : Predicate . isString ( label ) ? label : undefined ,
9295 } ,
9396 ]
9497 } ) ?? [ ] ,
0 commit comments