@@ -2,14 +2,12 @@ import { and, eq, gt, inArray, isNotNull, ne, or } from "drizzle-orm";
22import type { StateAdapter } from "chat" ;
33import {
44 getConversationStoppedAtMs ,
5- markConversationsStopped ,
5+ stopConversationTree ,
66} from "@/chat/conversations/session-stop" ;
77import { getSqlExecutor } from "@/chat/db" ;
8- import { cancelSignInWait } from "@/chat/task-execution/turn-cancel" ;
98import {
109 dropPendingMessages ,
1110 getConversationWorkState ,
12- stopConversationWork ,
1311} from "@/chat/task-execution/store" ;
1412import { juniorAgentInvocations , juniorConversations } from "@/db/schema" ;
1513import { mapBounded } from "@/chat/fan-out" ;
@@ -28,9 +26,8 @@ import type { TerminalAgentInvocation } from "./types";
2826/**
2927 * Stop one child agent of a parent Conversation and every child agent below
3028 * it. The parent gets no result. SQL stops the work first. Then each
31- * Conversation in the tree ends its session. An agent stop is not a person
32- * stop, so their Watches end. A Conversation stop request aborts each run
33- * that is still working.
29+ * Conversation in the tree stops through `stopConversationTree`, which keeps
30+ * their Watches and holds their automated input.
3431 *
3532 * Returns undefined when the child does not belong to the parent.
3633 */
@@ -54,7 +51,7 @@ export async function stopChildAgent(args: {
5451 return undefined ;
5552 }
5653 await finishStoppedTree ( tree , tree . conversationIds , {
57- cancelWatches : true ,
54+ stoppedAtMs : Date . now ( ) ,
5855 state : args . state ,
5956 } ) ;
6057 await dropUnreadLoopReplies (
@@ -80,8 +77,7 @@ export async function stopChildAgent(args: {
8077 *
8178 * Only a person's stop of this Conversation itself leaves child agents to
8279 * stop here: an agent stop, or a stop of a Conversation above it, stops this
83- * Conversation's own agent invocations first, so it cannot spawn more. So
84- * this keeps their Watches, as a person's stop does.
80+ * Conversation's own agent invocations first, so it cannot spawn more.
8581 */
8682export async function stopChildAgentsOf (
8783 conversationId : string ,
@@ -98,13 +94,12 @@ export async function stopChildAgentsOf(
9894 includeRoot : false ,
9995 taskedAfterMs : stoppedAtMs ,
10096 } ) ;
101- await skipUnreadResults ( conversationId , nowMs , state ) ;
10297 // They take the stop of this Conversation, so input that ends it ends
10398 // theirs too.
10499 const stoppedIds = tree . conversationIds . filter ( ( id ) => id !== conversationId ) ;
105100 await finishStoppedTree ( tree , stoppedIds , {
106- cancelWatches : false ,
107- nowMs : stoppedAtMs ,
101+ nowMs ,
102+ stoppedAtMs,
108103 state,
109104 } ) ;
110105 await dropUnreadLoopReplies ( tree . conversationIds , stoppedIds , state ) ;
@@ -134,59 +129,15 @@ export async function stopChildAgentsBelow(
134129 const tree = await stopAgentInvocationTree ( conversationId , nowMs , {
135130 includeRoot : false ,
136131 } ) ;
137- await skipUnreadResults ( conversationId , nowMs , state ) ;
138132 const stoppedIds = tree . conversationIds . filter ( ( id ) => id !== conversationId ) ;
139133 await finishStoppedTree ( tree , stoppedIds , {
140- cancelWatches : false ,
141- nowMs : stoppedAtMs ,
134+ nowMs ,
135+ stoppedAtMs,
142136 state,
143137 } ) ;
144138 await dropUnreadLoopReplies ( tree . conversationIds , stoppedIds , state ) ;
145139}
146140
147- /**
148- * Skip the `appended` results and replies that still wait in a Conversation's
149- * mailbox, so they start no Turn. A result that the Conversation already read keeps its
150- * status. Call this after the tree stop: it skipped each `pending` result and
151- * stopped each child agent, so no result becomes `appended` after this read.
152- *
153- * The mailbox read is not atomic with the parent worker. A worker that takes
154- * a result before this update holds the work lease, so the stop request of
155- * the Conversation stops its Turn. A result that such a Turn took but did not
156- * ack yet is marked `skipped`.
157- */
158- async function skipUnreadResults (
159- conversationId : string ,
160- nowMs : number ,
161- state : StateAdapter | undefined ,
162- ) : Promise < void > {
163- const messages =
164- ( await getConversationWorkState ( { conversationId, state } ) ) ?. messages ?? [ ] ;
165- // A child's `reply` is its result, so a stop skips it too. A reply from a
166- // Turn on the Conversation loop names no work of its own:
167- // `dropUnreadLoopReplies` drops it instead.
168- const invocationIds = ( await readChildMessageSenders ( messages ) ) . flatMap (
169- ( sender ) => {
170- const invocation = sender && ownAgentInvocation ( sender ) ;
171- return invocation ? [ invocation ] : [ ] ;
172- } ,
173- ) ;
174- if ( invocationIds . length === 0 ) {
175- return ;
176- }
177- await getSqlExecutor ( )
178- . db ( )
179- . update ( juniorAgentInvocations )
180- . set ( { parentMailboxStatus : "skipped" , updatedAt : new Date ( nowMs ) } )
181- . where (
182- and (
183- eq ( juniorAgentInvocations . parentConversationId , conversationId ) ,
184- inArray ( juniorAgentInvocations . invocationId , invocationIds ) ,
185- eq ( juniorAgentInvocations . parentMailboxStatus , "appended" ) ,
186- ) ,
187- ) ;
188- }
189-
190141/**
191142 * Drop the unread replies that Turns on the Conversation loop of the stopped
192143 * Conversations left in each mailbox of `mailboxIds`. Such a reply names
@@ -256,17 +207,13 @@ export async function dropOwnUnreadLoopReplies(
256207async function finishStoppedTree (
257208 tree : StoppedAgentTree ,
258209 conversationIdsToStop : string [ ] ,
259- options : Parameters < typeof markConversationsStopped > [ 1 ] ,
210+ options : Omit < Parameters < typeof stopConversationTree > [ 1 ] , "onStopSaved" > ,
260211) : Promise < void > {
261212 for ( const invocation of tree . stopped ) {
262213 // oxlint-disable-next-line no-await-in-loop -- Append the end markers in stop order.
263214 await recordSubagentEnded ( invocation ) ;
264215 }
265- await markConversationsStopped ( conversationIdsToStop , options ) ;
266- await mapBounded ( conversationIdsToStop , async ( conversationId ) => {
267- await cancelSignInWait ( conversationId ) ;
268- await stopConversationWork ( { conversationId, state : options . state } ) ;
269- } ) ;
216+ await stopConversationTree ( conversationIdsToStop , options ) ;
270217}
271218
272219/**
@@ -513,7 +460,7 @@ async function stopLockedAgentInvocationTree(
513460 ) ,
514461 // With `includeRoot: false`, an `appended` result sent to the
515462 // Conversation can be one that it already read, so only `pending`
516- // results are skipped here. See `skipUnreadResults`.
463+ // results are skipped here.
517464 and (
518465 eq ( juniorAgentInvocations . parentConversationId , conversationId ) ,
519466 eq ( juniorAgentInvocations . parentMailboxStatus , "pending" ) ,
0 commit comments