@@ -338,6 +338,18 @@ export interface ThreadManagementServiceShape {
338338 readonly threadId : ThreadId ;
339339 readonly runId : RunId ;
340340 } ) => Effect . Effect < void , ThreadManagementFailure > ;
341+ /**
342+ * Settles a thread. When the thread's own agent asks, it is mid-turn, so
343+ * the thread settles through `settleAfterRun` once that turn ends.
344+ */
345+ readonly settleThread : ( input : {
346+ readonly threadId : ThreadId ;
347+ readonly commandId : CommandId ;
348+ readonly byOwnAgent : boolean ;
349+ } ) => Effect . Effect <
350+ { readonly sequence : number } | { readonly settlesWhenTurnEnds : true } ,
351+ Orchestrator . OrchestratorV2Error
352+ > ;
341353 readonly interruptThread : (
342354 input : ThreadManagementInterruptInput ,
343355 ) => Effect . Effect < ThreadManagementInterruptResult , ThreadManagementFailure > ;
@@ -418,6 +430,7 @@ const SETTLE_AFTER_RUN_WAIT_MS = 24 * 60 * 60 * 1_000;
418430
419431const make = Effect . gen ( function * ( ) {
420432 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
433+ const layerScope = yield * Effect . scope ;
421434 const legacyImporter = yield * LegacyV1ThreadImporter . LegacyV1ThreadImporter ;
422435
423436 const ensureLegacyTranscript = Effect . fn (
@@ -751,6 +764,26 @@ const make = Effect.gen(function* () {
751764 } ) ;
752765 } ) ;
753766
767+ const settleThread : ThreadManagementServiceShape [ "settleThread" ] = Effect . fn (
768+ "orchestrationV2.threadManagement.settleThread" ,
769+ ) ( function * ( input ) {
770+ const shell = input . byOwnAgent ? yield * orchestrator . getThreadShell ( input . threadId ) : null ;
771+ if ( shell != null && shell . activeRunId !== null ) {
772+ yield * settleAfterRun ( {
773+ projectId : shell . projectId ,
774+ threadId : shell . id ,
775+ runId : shell . activeRunId ,
776+ } ) . pipe ( Effect . ignoreCause ( { log : true } ) , Effect . forkIn ( layerScope ) ) ;
777+ return { settlesWhenTurnEnds : true } as const ;
778+ }
779+ const result = yield * dispatch ( {
780+ type : "thread.settle" ,
781+ commandId : input . commandId ,
782+ threadId : input . threadId ,
783+ } ) ;
784+ return { sequence : result . sequence } ;
785+ } ) ;
786+
754787 const interruptThread : ThreadManagementServiceShape [ "interruptThread" ] = ( input ) =>
755788 Effect . gen ( function * ( ) {
756789 const target = yield * getProjectThreadRecords ( input , [ "runs" , "providerTurns" ] ) ;
@@ -857,6 +890,7 @@ const make = Effect.gen(function* () {
857890 sendToThread,
858891 waitForThread,
859892 settleAfterRun,
893+ settleThread,
860894 interruptThread,
861895 stopDelegatedTasks,
862896 getThreadEventSequence : orchestrator . getThreadEventSequence ,
0 commit comments