@@ -293,6 +293,7 @@ interface ThreadState {
293293 publishedBackgroundWork ?: boolean ;
294294 goalRevision : number ;
295295 goalFresh : boolean ;
296+ goalRefreshing : boolean ;
296297 readonly providerTurns : Map < string , OrchestrationV2ProviderTurn > ;
297298 active : ActiveTurn | undefined ;
298299 model : ModelRef | undefined ;
@@ -687,6 +688,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
687688 const continuationRequests = yield * ProviderContinuationRequests . ProviderContinuationRequests ;
688689 const crypto = yield * Digest . Digest ;
689690 const driver = OPENCODE_PROVIDER ;
691+ let nativeGoalSupport : boolean | undefined ;
690692 const mcpServerNameFor = ( threadId : string ) =>
691693 t3McpServerName ( threadId ) . pipe ( Effect . provideService ( Digest . Digest , crypto ) ) ;
692694
@@ -715,8 +717,10 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
715717 ) {
716718 let connection = initial . connection ;
717719 const supportsNativeGoals = ( ) => compareSemverVersions ( connection . version , "2.2.5" ) >= 0 ;
718- const hasActiveGoal = ( state : ThreadState ) =>
719- supportsNativeGoals ( ) && state . goalFresh && state . providerThread . goal ?. status === "active" ;
720+ nativeGoalSupport = supportsNativeGoals ( ) ;
721+ const goalHoldsTurn = ( state : ThreadState ) =>
722+ supportsNativeGoals ( ) &&
723+ ( state . goalRefreshing || ( state . goalFresh && state . providerThread . goal ?. status === "active" ) ) ;
720724 let client = connection . client ;
721725 const sessionScope = yield * Effect . scope ;
722726 // Project config changes model limits; key by directory because this runtime serves all instance threads.
@@ -779,6 +783,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
779783 providerThread,
780784 goalRevision : 0 ,
781785 goalFresh : false ,
786+ goalRefreshing : false ,
782787 providerTurns : new Map ( ) ,
783788 active : undefined ,
784789 model : undefined ,
@@ -877,6 +882,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
877882 ) {
878883 state . goalRevision += 1 ;
879884 state . goalFresh = true ;
885+ state . goalRefreshing = false ;
880886 state . providerThread = {
881887 ...state . providerThread ,
882888 goal :
@@ -2348,15 +2354,15 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
23482354 turn : ActiveTurn ,
23492355 end : TurnTerminal ,
23502356 ) {
2351- if ( end . status === "completed" && ( turn . steers . size > 0 || hasActiveGoal ( state ) ) ) {
2357+ if ( end . status === "completed" && ( turn . steers . size > 0 || goalHoldsTurn ( state ) ) ) {
23522358 turn . heldEnd = end ;
23532359 return ;
23542360 }
23552361 yield * finishTurn ( state , end ) ;
23562362 } ) ;
23572363
23582364 const endIfSettled = ( state : ThreadState , turn : ActiveTurn ) =>
2359- turn . heldEnd !== undefined && turn . steers . size === 0 && ! hasActiveGoal ( state )
2365+ turn . heldEnd !== undefined && turn . steers . size === 0 && ! goalHoldsTurn ( state )
23602366 ? finishTurn ( state , turn . heldEnd )
23612367 : Effect . void ;
23622368
@@ -2484,7 +2490,35 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
24842490 if ( event . type === "rpc.opencode.goal.changed" ) {
24852491 if ( ! supportsNativeGoals ( ) ) return ;
24862492 const goal = yield * decodeGoal ( event . data [ "goal" ] ) . pipe ( Effect . result ) ;
2487- if ( goal . _tag === "Failure" ) return ;
2493+ if ( goal . _tag === "Failure" ) {
2494+ state . goalFresh = false ;
2495+ state . goalRefreshing = true ;
2496+ state . goalRevision += 1 ;
2497+ const revision = state . goalRevision ;
2498+ yield * Effect . logWarning (
2499+ "Could not decode an OpenCode goal update; refreshing its state." ,
2500+ ) ;
2501+ yield * Effect . forkIn (
2502+ readGoal ( state ) . pipe (
2503+ Effect . timeout ( INVENTORY_TIMEOUT ) ,
2504+ Effect . catch ( ( ) =>
2505+ Effect . logWarning (
2506+ "Could not refresh OpenCode goal state after an unreadable update." ,
2507+ ) ,
2508+ ) ,
2509+ Effect . ensuring (
2510+ lock . withPermit (
2511+ Effect . gen ( function * ( ) {
2512+ if ( state . goalRevision === revision ) state . goalRefreshing = false ;
2513+ if ( state . active !== undefined ) yield * endIfSettled ( state , state . active ) ;
2514+ } ) ,
2515+ ) ,
2516+ ) ,
2517+ ) ,
2518+ sessionScope ,
2519+ ) ;
2520+ return ;
2521+ }
24882522 yield * updateGoal ( state , goal . success ) ;
24892523 if ( state . active !== undefined ) yield * endIfSettled ( state , state . active ) ;
24902524 return ;
@@ -2761,6 +2795,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27612795 const previous = currentScope ;
27622796 connection = next ;
27632797 client = next . client ;
2798+ nativeGoalSupport = supportsNativeGoals ( ) ;
27642799 session = {
27652800 ...session ,
27662801 capabilities : {
@@ -2774,6 +2809,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27742809 for ( const state of threads . values ( ) ) {
27752810 state . mcp = undefined ;
27762811 state . goalFresh = false ;
2812+ state . goalRefreshing = false ;
27772813 state . goalRevision += 1 ;
27782814 }
27792815 if ( supportsNativeGoals ( ) ) {
@@ -4251,7 +4287,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
42514287 instanceId,
42524288 driver,
42534289 getCapabilities : Effect . fn ( "OpencodeAdapter.getCapabilities" ) ( function ( ) {
4254- return Effect . succeed ( OpenCode2ProviderCapabilities ) ;
4290+ return Effect . succeed ( {
4291+ ...OpenCode2ProviderCapabilities ,
4292+ turns : {
4293+ ...OpenCode2ProviderCapabilities . turns ,
4294+ ...( nativeGoalSupport === undefined ? { } : { supportsNativeGoals : nativeGoalSupport } ) ,
4295+ } ,
4296+ } ) ;
42554297 } ) ,
42564298 planSelectionTransition : Effect . fn ( "OpencodeAdapter.planSelectionTransition" ) ( function ( ) {
42574299 return Effect . succeed ( turnScopedSelectionTransition ( ) ) ;
0 commit comments