11import { OrchestrationV2TurnItem , RunId , ThreadId , TurnItemId } from "@t3tools/contracts" ;
22import * as Context from "effect/Context" ;
33import * as Effect from "effect/Effect" ;
4- import * as DateTime from "effect/DateTime" ;
54import * as Layer from "effect/Layer" ;
65import * as Schema from "effect/Schema" ;
76import * as SqlClient from "effect/sql/SqlClient" ;
@@ -16,21 +15,17 @@ export class TurnItemPositionStoreError extends Schema.TaggedError<TurnItemPosit
1615) { }
1716
1817const isTurnItemPositionStoreError = Schema . is ( TurnItemPositionStoreError ) ;
19- const CHRONOLOGICAL_POSITION_MIN = 100_000_000_000_000 ;
2018
2119export interface TurnItemPositionStoreV2Shape {
2220 readonly allocate : ( input : {
2321 readonly threadId : ThreadId ;
2422 readonly turnItemId : TurnItemId ;
2523 readonly runId : RunId | null ;
2624 readonly runOrdinal ?: number ;
27- readonly sourceTimestamp ?: DateTime . Utc ;
28- readonly chronological ?: boolean ;
2925 } ) => Effect . Effect < number , TurnItemPositionStoreError > ;
3026 readonly normalize : (
3127 item : OrchestrationV2TurnItem ,
3228 runOrdinal ?: number ,
33- chronological ?: boolean ,
3429 ) => Effect . Effect < OrchestrationV2TurnItem , TurnItemPositionStoreError > ;
3530}
3631
@@ -47,55 +42,14 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
4742 const allocate : TurnItemPositionStoreV2Shape [ "allocate" ] =
4843 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
4944 Effect . fnUntraced (
50- function * ( {
51- threadId,
52- turnItemId,
53- runId,
54- runOrdinal : suppliedRunOrdinal ,
55- sourceTimestamp,
56- chronological = false ,
57- } ) {
58- const existing = yield * sql < { ordinal : number | null ; chronological : number } > `
59- SELECT (SELECT ordinal FROM orchestration_v2_turn_item_positions
60- WHERE thread_id = ${ threadId } AND turn_item_id = ${ turnItemId } LIMIT 1) AS ordinal,
61- EXISTS (SELECT 1 FROM orchestration_v2_turn_item_positions
62- WHERE thread_id = ${ threadId } AND ordinal >= ${ CHRONOLOGICAL_POSITION_MIN } ) AS chronological
63- ` ;
64- if (
65- sourceTimestamp !== undefined &&
66- ( chronological || existing [ 0 ] ?. chronological === 1 )
67- ) {
68- // Reserve a millisecond bucket for source-ordered imports. Existing native
69- // positions remain stable; legacy arrival-order imports are upgraded once.
70- const base = 1_000_000 + DateTime . toEpochMillis ( sourceTimestamp ) * 1000 ;
71- if ( ! Number . isSafeInteger ( base ) || base < 1_000_000 )
72- return yield * new TurnItemPositionStoreError ( {
73- threadId,
74- turnItemId,
75- cause : "Invalid source timestamp" ,
76- } ) ;
77- if ( existing [ 0 ] ?. ordinal != null && existing [ 0 ] . ordinal >= CHRONOLOGICAL_POSITION_MIN )
78- return existing [ 0 ] . ordinal ;
79- const rows = yield * sql < { ordinal : number } > `
80- SELECT COALESCE(MAX(ordinal), ${ base - 1 } ) + 1 AS ordinal
81- FROM orchestration_v2_turn_item_positions
82- WHERE thread_id = ${ threadId } AND ordinal >= ${ base } AND ordinal < ${ base + 1000 }
83- ` ;
84- const ordinal = rows [ 0 ] ! . ordinal ;
85- if ( ordinal >= base + 1000 )
86- return yield * new TurnItemPositionStoreError ( {
87- threadId,
88- turnItemId,
89- cause : "Source timestamp position bucket exhausted" ,
90- } ) ;
91- yield * sql `
92- INSERT INTO orchestration_v2_turn_item_positions (thread_id, turn_item_id, ordinal)
93- VALUES (${ threadId } , ${ turnItemId } , ${ ordinal } )
94- ON CONFLICT(thread_id, turn_item_id) DO UPDATE SET ordinal = excluded.ordinal
95- ` ;
96- return ordinal ;
97- }
98- if ( existing [ 0 ] ?. ordinal != null ) return existing [ 0 ] . ordinal ;
45+ function * ( { threadId, turnItemId, runId, runOrdinal : suppliedRunOrdinal } ) {
46+ const existing = yield * sql < { readonly ordinal : number } > `
47+ SELECT ordinal
48+ FROM orchestration_v2_turn_item_positions
49+ WHERE thread_id = ${ threadId } AND turn_item_id = ${ turnItemId }
50+ LIMIT 1
51+ ` ;
52+ if ( existing [ 0 ] !== undefined ) return existing [ 0 ] . ordinal ;
9953
10054 const runRows =
10155 runId === null
@@ -150,14 +104,12 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
150104 return TurnItemPositionStoreV2 . of ( {
151105 allocate,
152106 // Per-event normalization shares the enclosing orchestration span.
153- normalize : Effect . fnUntraced ( function * ( item , runOrdinal , chronological ) {
107+ normalize : Effect . fnUntraced ( function * ( item , runOrdinal ) {
154108 return yield * allocate ( {
155109 threadId : item . threadId ,
156110 turnItemId : item . id ,
157111 runId : item . runId ,
158112 ...( runOrdinal === undefined ? { } : { runOrdinal } ) ,
159- sourceTimestamp : item . startedAt ?? item . updatedAt ,
160- chronological : chronological ?? false ,
161113 } ) . pipe ( Effect . map ( ( ordinal ) => ( item . ordinal === ordinal ? item : { ...item , ordinal } ) ) ) ;
162114 } ) ,
163115 } ) ;
0 commit comments