@@ -46,6 +46,7 @@ import type * as Duration from "effect/Duration";
4646import * as Effect from "effect/Effect" ;
4747import * as Exit from "effect/Exit" ;
4848import * as FileSystem from "effect/FileSystem" ;
49+ import * as Hex from "effect/encoding/Hex" ;
4950import * as Fiber from "effect/Fiber" ;
5051import * as Option from "effect/Option" ;
5152import * as Queue from "effect/Queue" ;
@@ -1022,6 +1023,11 @@ interface AcpNativeBuildConfiguration {
10221023}
10231024
10241025interface ActiveAcpTurn {
1026+ readonly instructionDelivery ?: {
1027+ readonly state : T3AcpInstructionState ;
1028+ readonly digest : string ;
1029+ readonly compactionMarker : symbol | undefined ;
1030+ } ;
10251031 readonly input : ProviderAdapter . ProviderAdapterV2TurnInput ;
10261032 readonly providerTurnId : OrchestrationV2ProviderTurn [ "id" ] ;
10271033 readonly nativeThreadId : string ;
@@ -1922,6 +1928,31 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
19221928
19231929 const emitProviderEvent = ( event : ProviderAdapter . ProviderAdapterV2Event ) =>
19241930 Queue . offer ( events , event ) . pipe ( Effect . asVoid ) ;
1931+ const recordInstructions = Effect . fnUntraced ( function * (
1932+ context : ActiveAcpTurn ,
1933+ receipt : NonNullable <
1934+ OrchestrationV2ProviderThreadNativeMetadata [ "acpInstructions" ]
1935+ > | null ,
1936+ ) {
1937+ const metadata = { ...context . nativeMetadata , acpInstructions : receipt } ;
1938+ context . nativeMetadata = metadata ;
1939+ yield * Ref . update ( nativeMetadataBySessionId , ( current ) =>
1940+ new Map ( current ) . set ( context . nativeThreadId , metadata ) ,
1941+ ) ;
1942+ const known = ( yield * Ref . get ( providerThreadByNativeSessionId ) ) . get (
1943+ context . nativeThreadId ,
1944+ ) ;
1945+ if ( known === undefined ) return ;
1946+ const providerThread = {
1947+ ...known ,
1948+ nativeMetadata : metadata ,
1949+ updatedAt : yield * DateTime . now ,
1950+ } ;
1951+ yield * Ref . update ( providerThreadByNativeSessionId , ( current ) =>
1952+ new Map ( current ) . set ( context . nativeThreadId , providerThread ) ,
1953+ ) ;
1954+ yield * emitProviderEvent ( { type : "provider_thread.updated" , driver, providerThread } ) ;
1955+ } ) ;
19251956 let scheduleDeferredFinalize : ( context : ActiveAcpTurn ) => Effect . Effect < void > = ( ) =>
19261957 Effect . void ;
19271958
@@ -3650,6 +3681,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
36503681 : { beforeTokenCount : context . contextUsage . usedTokens } ) ,
36513682 } ;
36523683 if ( status === "completed" ) {
3684+ yield * recordInstructions ( context , null ) ;
36533685 context . contextUsage = null ;
36543686 yield * Ref . update ( contextUsageBySessionId , ( current ) => {
36553687 const updated = new Map ( current ) ;
@@ -6914,13 +6946,34 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
69146946 yield * emitProviderRetry ( context , settledStatus ) ;
69156947 }
69166948 const now = yield * DateTime . now ;
6949+ const delivery = context . instructionDelivery ;
6950+ if (
6951+ settledStatus === "completed" &&
6952+ delivery !== undefined &&
6953+ ( yield * Ref . get ( compactionMarkers ) ) . get ( context . nativeThreadId ) ===
6954+ delivery . compactionMarker
6955+ ) {
6956+ yield * recordInstructions ( context , {
6957+ nativeSessionId : context . nativeThreadId ,
6958+ digest : delivery . digest ,
6959+ } ) ;
6960+ yield * Ref . update ( promptInstructionStates , ( current ) =>
6961+ new Map ( current ) . set ( context . nativeThreadId , delivery . state ) ,
6962+ ) ;
6963+ yield * Ref . update ( compactionMarkers , ( current ) => {
6964+ const updated = new Map ( current ) ;
6965+ updated . delete ( context . nativeThreadId ) ;
6966+ return updated ;
6967+ } ) ;
6968+ }
69176969 if (
69186970 flavor . supportsCompaction === true &&
69196971 context . input . message . text . trim ( ) === "/compact" &&
69206972 context . input . message . attachments . length === 0 &&
69216973 settledStatus === "completed"
69226974 ) {
69236975 const nativeItemId = `${ context . nativeTurnId } :compaction` ;
6976+ yield * recordInstructions ( context , null ) ;
69246977 yield * Ref . update ( compactionMarkers , ( current ) => {
69256978 const updated = new Map ( current ) ;
69266979 updated . set ( context . nativeThreadId , Symbol ( ) ) ;
@@ -7092,7 +7145,27 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
70927145 interactionMode : turnInput . runtimePolicy . interactionMode ,
70937146 hasT3Mcp : ( yield * readMcpContext ( turnInput . threadId ) ) . servers . length > 0 ,
70947147 } satisfies T3AcpInstructionState ;
7095- const previousInstructionState = ( yield * Ref . get ( promptInstructionStates ) ) . get ( sessionId ) ;
7148+ const instructionDigest = yield * crypto
7149+ . digest (
7150+ "SHA-256" ,
7151+ new TextEncoder ( ) . encode (
7152+ t3AcpPromptWithInstructions ( { prompt : "" , state : instructionState } ) ,
7153+ ) ,
7154+ )
7155+ . pipe ( Effect . map ( Hex . encode ) , Effect . orDie ) ;
7156+ const rememberedInstructions = ( yield * Ref . get ( nativeMetadataBySessionId ) ) . get (
7157+ sessionId ,
7158+ ) ?. acpInstructions ;
7159+ const recordedInstructions =
7160+ rememberedInstructions === undefined
7161+ ? turnInput . providerThread . nativeMetadata ?. acpInstructions
7162+ : rememberedInstructions ;
7163+ const previousInstructionState =
7164+ ( yield * Ref . get ( promptInstructionStates ) ) . get ( sessionId ) ??
7165+ ( recordedInstructions ?. nativeSessionId === sessionId &&
7166+ recordedInstructions . digest === instructionDigest
7167+ ? instructionState
7168+ : undefined ) ;
70967169 const compactionMarker = ( yield * Ref . get ( compactionMarkers ) ) . get ( sessionId ) ;
70977170 const userText =
70987171 flavor . rewriteUserText === undefined
@@ -7173,6 +7246,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
71737246 return {
71747247 prompt,
71757248 instructionState : text === messageText ? undefined : instructionState ,
7249+ instructionDigest,
71767250 compactionMarker,
71777251 } ;
71787252 } ) ;
@@ -7308,6 +7382,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
73087382 ) ;
73097383 }
73107384 const context : ActiveAcpTurn = {
7385+ ...( promptParts ?. instructionState === undefined
7386+ ? { }
7387+ : {
7388+ instructionDelivery : {
7389+ state : promptParts . instructionState ,
7390+ digest : promptParts . instructionDigest ,
7391+ compactionMarker : promptParts . compactionMarker ,
7392+ } ,
7393+ } ) ,
73117394 input : turnInput ,
73127395 providerTurnId,
73137396 nativeThreadId : requestedSessionId ,
@@ -7477,28 +7560,6 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
74777560 }
74787561 const promptGeneration = yield * Ref . get ( runtimeCallbackGeneration ) ;
74797562 yield * runtime . prompt ( { prompt : promptParts ! . prompt } ) . pipe (
7480- Effect . tap ( ( ) =>
7481- Ref . update ( promptInstructionStates , ( current ) => {
7482- if ( promptParts ?. instructionState === undefined ) return current ;
7483- const updated = new Map ( current ) ;
7484- updated . set ( requestedSessionId , promptParts . instructionState ) ;
7485- return updated ;
7486- } ) ,
7487- ) ,
7488- Effect . tap ( ( ) =>
7489- Ref . update ( compactionMarkers , ( current ) => {
7490- if ( promptParts ?. instructionState === undefined ) return current ;
7491- const observedMarker = promptParts . compactionMarker ;
7492- if (
7493- observedMarker === undefined ||
7494- current . get ( requestedSessionId ) !== observedMarker
7495- )
7496- return current ;
7497- const updated = new Map ( current ) ;
7498- updated . delete ( requestedSessionId ) ;
7499- return updated ;
7500- } ) ,
7501- ) ,
75027563 // Wire settlement precedes the completion callback's permit request so
75037564 // settled-soft classification can observe the native return even when
75047565 // the completion fiber has not yet set promptSettled under the permit.
@@ -8334,6 +8395,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
83348395 nativeConversationHeadRef : null ,
83358396 nativeMetadata : {
83368397 ...rollbackInput . providerThread . nativeMetadata ,
8398+ acpInstructions : null ,
83378399 itemIdentityVersion : 2 as const ,
83388400 } ,
83398401 status : "idle" as const ,
0 commit comments