@@ -21,6 +21,7 @@ import {
2121 type OrchestrationV2UserInputQuestion ,
2222 type ProviderApprovalDecision ,
2323 type ProviderInstanceId ,
24+ type MessageId ,
2425 type OrchestrationV2ProviderTurnTokenUsage ,
2526} from "@t3tools/contracts" ;
2627import { PiSettings } from "../settings.ts" ;
@@ -374,6 +375,8 @@ interface ActivePiTurn {
374375 activeProviderRetry : PiProviderRetryState | null ;
375376 failure : ReturnType < typeof makeProviderFailure > | null ;
376377 rejectedPromptFailure : ReturnType < typeof makeProviderFailure > | null ;
378+ /** Steers Pi queued in send order; it reads them at a step boundary, after running tools return. */
379+ readonly unreadSteers : Array < MessageId > ;
377380 stopTreeRefs ?: PiTurnTreeRefs | null ;
378381}
379382
@@ -411,6 +414,7 @@ interface PiPromptSend {
411414 readonly providerTurnId : OrchestrationV2ProviderTurn [ "id" ] ;
412415 readonly kind : "turn_start" | "steer" ;
413416 readonly record : PiRpcRecord ;
417+ readonly messageId ?: MessageId ;
414418}
415419
416420interface PiThreadState {
@@ -625,6 +629,20 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
625629 const emit = ( event : ProviderAdapter . ProviderAdapterV2Event ) =>
626630 Queue . offer ( events , event ) . pipe ( Effect . asVoid ) ;
627631
632+ const emitSteerStatus = (
633+ turn : ActivePiTurn ,
634+ messageId : MessageId ,
635+ status : "pending" | "completed" | "cancelled" ,
636+ ) =>
637+ emit ( {
638+ type : "steer.updated" ,
639+ driver : PI_PROVIDER ,
640+ threadId : turn . turnInput . threadId ,
641+ runId : turn . turnInput . runId ,
642+ messageId,
643+ status,
644+ } ) ;
645+
628646 const updateProviderSession =
629647 /* Provider events include streaming deltas; avoid a span for each update. */
630648 Effect . fnUntraced ( function * (
@@ -1713,6 +1731,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
17131731 }
17141732 }
17151733 yield * cancelPendingPrompts ( completedAt ) ;
1734+ // Stop clears Pi's queue, so an unread steer was dropped; otherwise Pi read it before settling.
1735+ for ( const messageId of turn . unreadSteers . splice ( 0 ) ) {
1736+ yield * emitSteerStatus ( turn , messageId , interrupted ? "cancelled" : "completed" ) ;
1737+ }
17161738 const treeRefs =
17171739 turn . stopTreeRefs !== undefined ? turn . stopTreeRefs : yield * captureTurnTreeRefs ( ) ;
17181740 const tokenUsage = readUsage
@@ -1931,10 +1953,15 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
19311953 return ;
19321954 }
19331955 case "message_start" : {
1934- if ( turn !== null && recordString ( event [ "message" ] , "role" ) === "assistant" ) {
1956+ const role = recordString ( event [ "message" ] , "role" ) ;
1957+ if ( turn !== null && role === "assistant" ) {
19351958 turn . sawAgentActivity = true ;
19361959 turn . messageOrdinal += 1 ;
19371960 }
1961+ const readSteer = role === "user" ? turn ?. unreadSteers . shift ( ) : undefined ;
1962+ if ( turn !== null && readSteer !== undefined ) {
1963+ yield * emitSteerStatus ( turn , readSteer , "completed" ) ;
1964+ }
19381965 return ;
19391966 }
19401967 case "message_update" : {
@@ -2227,6 +2254,14 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
22272254 yield * scheduleSettleProbe ( responseTurn , true ) ;
22282255 }
22292256 if ( event [ "success" ] === true ) {
2257+ if (
2258+ pendingPrompt ?. messageId !== undefined &&
2259+ responseTurn !== null &&
2260+ recordString ( recordField ( event , "data" ) , "disposition" ) === "queued"
2261+ ) {
2262+ responseTurn . unreadSteers . push ( pendingPrompt . messageId ) ;
2263+ yield * emitSteerStatus ( responseTurn , pendingPrompt . messageId , "pending" ) ;
2264+ }
22302265 // Command-only prompts emit no agent_settled; probe idle and re-queue behind preceding native events.
22312266 if (
22322267 pendingPrompt ?. kind === "turn_start" &&
@@ -2802,6 +2837,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
28022837 activeProviderRetry : null ,
28032838 failure : null ,
28042839 rejectedPromptFailure : null ,
2840+ unreadSteers : [ ] ,
28052841 } ;
28062842 // Lock only install/send/start events; correlated setup must leave the pump free for dialogs.
28072843 yield * Effect . gen ( function * ( ) {
@@ -2928,6 +2964,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
29282964 yield * sendPrompt ( {
29292965 providerTurnId : turn . providerTurn . id ,
29302966 kind : "steer" ,
2967+ messageId : steerInput . message . messageId ,
29312968 record : {
29322969 type : "prompt" ,
29332970 message : payload . message ,
0 commit comments