@@ -38,6 +38,7 @@ import type { OrchestrationDispatchError } from "../Errors.ts";
3838import { isGitRepository } from "../../git/Utils.ts" ;
3939import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts" ;
4040import * as WorkspaceEntries from "../../workspace/WorkspaceEntries.ts" ;
41+ import * as PullRequestService from "../../pullRequest/PullRequestService.ts" ;
4142
4243const nowIso = Effect . map ( DateTime . now , DateTime . formatIso ) ;
4344
@@ -88,6 +89,9 @@ const make = Effect.gen(function* () {
8889 const receiptBus = yield * RuntimeReceiptBus ;
8990 const workspaceEntries = yield * WorkspaceEntries . WorkspaceEntries ;
9091 const vcsStatusBroadcaster = yield * VcsStatusBroadcaster ;
92+ const pullRequests = yield * PullRequestService . PullRequestService ;
93+ const startedTurns = new Map < ThreadId , TurnId > ( ) ;
94+ const pending = new Set < ThreadId > ( ) ;
9195
9296 const appendRevertFailureActivity = ( input : {
9397 readonly threadId : ThreadId ;
@@ -854,6 +858,7 @@ const make = Effect.gen(function* () {
854858
855859 const processDomainEvent = Effect . fn ( "processDomainEvent" ) ( function * ( event : OrchestrationEvent ) {
856860 if ( event . type === "thread.turn-start-requested" || event . type === "thread.message-sent" ) {
861+ if ( event . type === "thread.turn-start-requested" ) pending . add ( event . payload . threadId ) ;
857862 yield * ensurePreTurnBaselineFromDomainTurnStart ( event ) ;
858863 return ;
859864 }
@@ -897,14 +902,46 @@ const make = Effect.gen(function* () {
897902 const processRuntimeEvent = Effect . fn ( "processRuntimeEvent" ) ( function * (
898903 event : ProviderRuntimeEvent ,
899904 ) {
905+ if ( event . type === "session.exited" ) {
906+ startedTurns . delete ( event . threadId ) ;
907+ pending . delete ( event . threadId ) ;
908+ return ;
909+ }
910+
900911 if ( event . type === "turn.started" ) {
912+ const turnId = toTurnId ( event . turnId ) ;
913+ const activeTurnId = ( yield * providerService . listSessions ( ) ) . find ( ( session ) =>
914+ sameId ( session . threadId , event . threadId ) ,
915+ ) ?. activeTurnId ;
916+ const mayReplace = pending . has ( event . threadId ) && sameId ( activeTurnId , turnId ) ;
917+ if ( turnId !== null && ( ! startedTurns . has ( event . threadId ) || mayReplace ) ) {
918+ startedTurns . set ( event . threadId , turnId ) ;
919+ pending . delete ( event . threadId ) ;
920+ }
901921 yield * ensurePreTurnBaselineFromTurnStart ( event ) ;
902922 return ;
903923 }
904924
905- if ( event . type === "turn.completed" ) {
925+ if ( event . type === "turn.completed" || event . type === "turn.aborted" ) {
906926 const turnId = toTurnId ( event . turnId ) ;
907- yield * refreshLocalGitStatusFromTurnCompletion ( event ) ;
927+ const thread = yield * resolveThreadDetail ( event . threadId ) ;
928+ const startedTurnId = startedTurns . get ( event . threadId ) ;
929+ const isTrackedTurn = sameId ( startedTurnId , turnId ) ;
930+ if ( isTrackedTurn ) startedTurns . delete ( event . threadId ) ;
931+ if ( event . type === "turn.completed" ) {
932+ yield * refreshLocalGitStatusFromTurnCompletion ( event ) ;
933+ }
934+ if (
935+ turnId !== null &&
936+ thread !== undefined &&
937+ ( isTrackedTurn ||
938+ sameId ( thread . session ?. activeTurnId , turnId ) ||
939+ ( startedTurnId === undefined && ! thread . session ?. activeTurnId ) )
940+ ) {
941+ pending . delete ( event . threadId ) ;
942+ yield * pullRequests . refreshAfterTurn ;
943+ }
944+ if ( event . type === "turn.aborted" ) return ;
908945 yield * captureCheckpointFromTurnCompletion ( event ) . pipe (
909946 Effect . catch ( ( error ) =>
910947 Effect . flatMap ( nowIso , ( createdAt ) =>
@@ -963,7 +1000,12 @@ const make = Effect.gen(function* () {
9631000
9641001 yield * forkParked (
9651002 Stream . runForEach ( providerService . streamEvents , ( event ) => {
966- if ( event . type !== "turn.started" && event . type !== "turn.completed" ) {
1003+ if (
1004+ event . type !== "turn.started" &&
1005+ event . type !== "turn.completed" &&
1006+ event . type !== "turn.aborted" &&
1007+ event . type !== "session.exited"
1008+ ) {
9671009 return Effect . void ;
9681010 }
9691011 return worker . enqueue ( { source : "runtime" , event } ) ;
0 commit comments