@@ -73,6 +73,7 @@ import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2Pend
7373import * as Context from "effect/Context" ;
7474import * as DateTime from "effect/DateTime" ;
7575import * as Effect from "effect/Effect" ;
76+ import * as Equal from "effect/Equal" ;
7677import * as Layer from "effect/Layer" ;
7778import * as Ref from "effect/Ref" ;
7879import * as Schema from "effect/Schema" ;
@@ -460,6 +461,10 @@ export interface ProjectionStoreV2Shape {
460461 requestId : TurnItemId ,
461462 resultId : TurnItemId ,
462463 ) => Effect . Effect < boolean , ProjectionStoreV2Error > ;
464+ /** True when the projection already stores this exact node, so a write would change nothing. */
465+ readonly isNodeStored : (
466+ node : OrchestrationV2ExecutionNode ,
467+ ) => Effect . Effect < boolean , ProjectionStoreV2Error > ;
463468 readonly getRuntimeRequest : (
464469 threadId : ThreadId ,
465470 requestId : RuntimeRequestId ,
@@ -4404,6 +4409,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
44044409 return rows [ 0 ] ?. pending === 1 ;
44054410 } ) . pipe ( Effect . mapError ( controlReadError ( threadId ) ) ) ;
44064411
4412+ const isNodeStored : ProjectionStoreV2Shape [ "isNodeStored" ] = ( node ) =>
4413+ Effect . gen ( function * ( ) {
4414+ const payloadJson = yield * encodeNodePayload ( node ) ;
4415+ const rows = yield * sql < PayloadRow > `
4416+ SELECT payload_json FROM orchestration_v2_projection_nodes WHERE node_id = ${ node . id }
4417+ ` ;
4418+ return rows [ 0 ] ?. payload_json === payloadJson ;
4419+ } ) . pipe ( Effect . mapError ( controlReadError ( node . threadId ) ) ) ;
4420+
44074421 const getRuntimeRequest : ProjectionStoreV2Shape [ "getRuntimeRequest" ] = ( threadId , requestId ) =>
44084422 sql
44094423 . withTransaction (
@@ -5992,6 +6006,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
59926006 canStartQueuedRun,
59936007 getPendingNativeUserInputs,
59946008 hasUnpairedRunInterruptRequest,
6009+ isNodeStored,
59956010 getMessageCount,
59966011 getNextTurnItemOrdinal,
59976012 getTurnItem,
@@ -6255,6 +6270,15 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
62556270 ! projection . turnItems . some ( ( item ) => item . id === resultId )
62566271 ) ;
62576272 } ) ,
6273+ isNodeStored : ( node ) =>
6274+ Ref . get ( replayState ) . pipe (
6275+ Effect . map ( ( state ) => {
6276+ const stored = state . projections
6277+ . get ( node . threadId )
6278+ ?. nodes . find ( ( candidate ) => candidate . id === node . id ) ;
6279+ return stored !== undefined && Equal . equals ( stored , node ) ;
6280+ } ) ,
6281+ ) ,
62586282 getMessageCount : ( threadId ) =>
62596283 Ref . get ( replayState ) . pipe (
62606284 Effect . map ( ( state ) => state . projections . get ( threadId ) ?. messages . length ?? 0 ) ,
0 commit comments