@@ -60,8 +60,18 @@ export class OrchestrationEventStore extends Context.Service<
6060 readonly eventType ?: OrchestrationV2DomainEvent [ "type" ] ;
6161 readonly limit ?: number ;
6262 readonly skipUnknownEventTypes ?: boolean ;
63+ /** Bound fetched JSON per page, always allowing one indivisible event. */
64+ readonly maxPageBytes ?: number ;
6365 } ) => Stream . Stream < OrchestrationV2StoredEvent , OrchestrationEventStoreError > ;
6466
67+ /** Check a scoped replay range without reading or restoring event payloads. */
68+ readonly hasAgentEvent : ( input : {
69+ readonly threadId : ThreadId ;
70+ readonly afterSequence : number ;
71+ readonly throughSequence : number ;
72+ readonly eventType : OrchestrationV2DomainEvent [ "type" ] ;
73+ } ) => Effect . Effect < boolean , OrchestrationEventStoreError > ;
74+
6575 readonly getAgentReplayStats : ( input : {
6676 readonly threadId : ThreadId ;
6777 readonly afterSequence : number ;
@@ -392,9 +402,9 @@ const makeEventStore = Effect.gen(function* () {
392402 readonly eventType ?: OrchestrationV2DomainEvent [ "type" ] ;
393403 readonly onlyAgentEvents ?: boolean ;
394404 readonly limit : number ;
395- } ) =>
396- sql < ApplicationEventRow > `
397- SELECT
405+ readonly maxPageBytes ?: number ;
406+ } ) => {
407+ const columns = sql `
398408 sequence,
399409 event_id,
400410 command_id,
@@ -407,6 +417,8 @@ const makeEventStore = Effect.gen(function* () {
407417 application_event_version,
408418 causation_event_id,
409419 correlation_id
420+ ` ;
421+ const from = sql `
410422 FROM orchestration_events
411423 ${
412424 // Otherwise the sequence index wins and command lookups scan every V2 event.
@@ -431,9 +443,41 @@ const makeEventStore = Effect.gen(function* () {
431443 ...( input . commandId === undefined ? [ ] : [ sql `command_id = ${ input . commandId } ` ] ) ,
432444 ...( input . eventType === undefined ? [ ] : [ sql `event_type = ${ input . eventType } ` ] ) ,
433445 ] ) }
434- ORDER BY sequence ASC
435- LIMIT ${ input . limit }
446+ ` ;
447+ if ( input . maxPageBytes === undefined ) {
448+ return sql < ApplicationEventRow > `
449+ SELECT ${ columns } ${ from } ORDER BY sequence ASC LIMIT ${ input . limit }
450+ ` . pipe ( NodeSqliteClient . readOnly ) ;
451+ }
452+ // Materialize only candidate ids. Walk their restored sizes until the page
453+ // fills (at most one overflow row), rather than restoring all 500 bodies.
454+ // Prompt references must be sized after restoration, including JSON escaping.
455+ return sql < ApplicationEventRow > `
456+ WITH RECURSIVE candidates AS MATERIALIZED (
457+ SELECT sequence, ROW_NUMBER() OVER (ORDER BY sequence) AS ordinal
458+ ${ from } ORDER BY sequence ASC LIMIT ${ input . limit }
459+ ), replay_page(cursor, replay_bytes, row_count) AS (
460+ SELECT orchestration_events.sequence,
461+ octet_length(${ restoredPayloadJson } ) + octet_length(metadata_json), 1
462+ FROM orchestration_events
463+ JOIN candidates ON candidates.sequence = orchestration_events.sequence
464+ WHERE candidates.ordinal = 1
465+ UNION ALL
466+ SELECT orchestration_events.sequence,
467+ page.replay_bytes + octet_length(${ restoredPayloadJson } ) + octet_length(metadata_json),
468+ page.row_count + 1
469+ FROM replay_page AS page
470+ JOIN candidates ON candidates.ordinal = page.row_count + 1
471+ JOIN orchestration_events ON orchestration_events.sequence = candidates.sequence
472+ WHERE page.replay_bytes < ${ input . maxPageBytes }
473+ )
474+ SELECT ${ columns }
475+ FROM orchestration_events
476+ JOIN replay_page ON replay_page.cursor = orchestration_events.sequence
477+ WHERE replay_page.row_count = 1 OR replay_page.replay_bytes <= ${ input . maxPageBytes }
478+ ORDER BY sequence
436479 ` . pipe ( NodeSqliteClient . readOnly ) ;
480+ } ;
437481
438482 const appendAgentEvents : OrchestrationEventStore [ "Service" ] [ "appendAgentEvents" ] = ( input ) =>
439483 Effect . forEach (
@@ -556,6 +600,7 @@ const makeEventStore = Effect.gen(function* () {
556600 ...( input ?. eventType === undefined ? { } : { eventType : input . eventType } ) ,
557601 onlyAgentEvents : true ,
558602 limit : pageLimit ,
603+ ...( input ?. maxPageBytes === undefined ? { } : { maxPageBytes : input . maxPageBytes } ) ,
559604 } ) . pipe (
560605 Effect . mapError ( toPersistenceSqlError ( "OrchestrationEventStore.readAgentEvents:query" ) ) ,
561606 Effect . map ( ( rows ) => {
@@ -564,7 +609,7 @@ const makeEventStore = Effect.gen(function* () {
564609 return [
565610 rows ,
566611 last === undefined ||
567- rows . length < pageLimit ||
612+ ( input ?. maxPageBytes === undefined && rows . length < pageLimit ) ||
568613 nextRemaining <= 0 ||
569614 ( input ?. throughSequence !== undefined && last . sequence >= input . throughSequence )
570615 ? Option . none ( )
@@ -585,6 +630,23 @@ const makeEventStore = Effect.gen(function* () {
585630 ) ;
586631 } ;
587632
633+ const hasAgentEvent : OrchestrationEventStore [ "Service" ] [ "hasAgentEvent" ] = ( input ) =>
634+ sql < { readonly found : number } > `
635+ SELECT EXISTS (
636+ SELECT 1 FROM orchestration_events
637+ WHERE aggregate_kind = 'thread'
638+ AND stream_id = ${ input . threadId }
639+ AND application_event_version = 2
640+ AND sequence > ${ input . afterSequence }
641+ AND sequence <= ${ input . throughSequence }
642+ AND event_type = ${ input . eventType }
643+ ) AS found
644+ ` . pipe (
645+ NodeSqliteClient . readOnly ,
646+ Effect . mapError ( toPersistenceSqlError ( "OrchestrationEventStore.hasAgentEvent:query" ) ) ,
647+ Effect . map ( ( rows ) => rows [ 0 ] ?. found === 1 ) ,
648+ ) ;
649+
588650 const getAgentReplayStats : OrchestrationEventStore [ "Service" ] [ "getAgentReplayStats" ] = ( input ) =>
589651 sql < {
590652 readonly eventCount : number ;
@@ -743,6 +805,7 @@ const makeEventStore = Effect.gen(function* () {
743805 appendProjectEvent,
744806 appendAgentEvents,
745807 readAgentEvents,
808+ hasAgentEvent,
746809 getAgentReplayStats,
747810 getReplayStats,
748811 latestAgentSequence,
0 commit comments