@@ -123,6 +123,61 @@ function readTranscriptGeneration(projection: ResetWindowProjection): string | u
123123 ) ?. generation ;
124124}
125125
126+ function sqliteBoundarySerializedBytes ( ) {
127+ return /* kysely-allow-raw: boundary size is checked before scalar projection. */ sql < number > `LENGTH(CAST(event.event_json AS BLOB))` ;
128+ }
129+
130+ function sqliteBoundaryJsonValid ( ) {
131+ return /* kysely-allow-raw: boundary JSON validity is part of the fail-closed contract. */ sql < number > `json_valid(event.event_json)` ;
132+ }
133+
134+ function sqliteBoundaryFirstKeptEntryId ( ) {
135+ return /* kysely-allow-raw: project one bounded canonical boundary scalar. */ sql <
136+ string | null
137+ > `CASE
138+ WHEN LENGTH(CAST(event.event_json AS BLOB)) <= ${ MAX_CONTEXT_BOUNDARY_BYTES }
139+ AND json_valid(event.event_json)
140+ AND json_type(event.event_json, '$.firstKeptEntryId') = 'text'
141+ THEN json_extract(event.event_json, '$.firstKeptEntryId')
142+ ELSE NULL
143+ END` ;
144+ }
145+
146+ function sqliteBoundarySummary ( ) {
147+ return /* kysely-allow-raw: project one bounded canonical boundary scalar. */ sql <
148+ string | null
149+ > `CASE
150+ WHEN LENGTH(CAST(event.event_json AS BLOB)) <= ${ MAX_CONTEXT_BOUNDARY_BYTES }
151+ AND json_valid(event.event_json)
152+ AND json_type(event.event_json, '$.summary') = 'text'
153+ THEN json_extract(event.event_json, '$.summary')
154+ ELSE NULL
155+ END` ;
156+ }
157+
158+ function sqliteBoundaryTimestamp ( ) {
159+ return /* kysely-allow-raw: project one bounded canonical boundary scalar. */ sql <
160+ string | number | null
161+ > `CASE
162+ WHEN LENGTH(CAST(event.event_json AS BLOB)) <= ${ MAX_CONTEXT_BOUNDARY_BYTES }
163+ AND json_valid(event.event_json)
164+ AND json_type(event.event_json, '$.timestamp') IN ('integer', 'real', 'text')
165+ THEN json_extract(event.event_json, '$.timestamp')
166+ ELSE NULL
167+ END` ;
168+ }
169+
170+ function sqliteContextMessageRole ( ) {
171+ return /* kysely-allow-raw: inspect the canonical role without loading payload JSON. */ sql <
172+ string | null
173+ > `CASE WHEN json_valid(event.event_json)
174+ THEN json_extract(event.event_json, '$.message.role') ELSE NULL END` ;
175+ }
176+
177+ function sqliteContextMessageSerializedBytes ( ) {
178+ return /* kysely-allow-raw: enforce the payload budget before materialization. */ sql < number > `LENGTH(CAST(event.event_json AS BLOB)) + 1` ;
179+ }
180+
126181function readLatestActiveBoundaryByType (
127182 projection : ResetWindowProjection ,
128183 eventType : "compaction" | "reset" ,
@@ -146,30 +201,11 @@ function readLatestActiveBoundaryByType(
146201 "active.active_position" ,
147202 "identity.event_type" ,
148203 "identity.seq" ,
149- /* kysely-allow-raw: reject oversized/malformed boundaries before projecting scalars. */
150- sql < number > `LENGTH(CAST(event.event_json AS BLOB))` . as ( "serialized_bytes" ) ,
151- sql < number > `json_valid(event.event_json)` . as ( "json_valid" ) ,
152- sql < string | null > `CASE
153- WHEN LENGTH(CAST(event.event_json AS BLOB)) <= ${ MAX_CONTEXT_BOUNDARY_BYTES }
154- AND json_valid(event.event_json)
155- AND json_type(event.event_json, '$.firstKeptEntryId') = 'text'
156- THEN json_extract(event.event_json, '$.firstKeptEntryId')
157- ELSE NULL
158- END` . as ( "first_kept_entry_id" ) ,
159- sql < string | null > `CASE
160- WHEN LENGTH(CAST(event.event_json AS BLOB)) <= ${ MAX_CONTEXT_BOUNDARY_BYTES }
161- AND json_valid(event.event_json)
162- AND json_type(event.event_json, '$.summary') = 'text'
163- THEN json_extract(event.event_json, '$.summary')
164- ELSE NULL
165- END` . as ( "summary" ) ,
166- sql < string | number | null > `CASE
167- WHEN LENGTH(CAST(event.event_json AS BLOB)) <= ${ MAX_CONTEXT_BOUNDARY_BYTES }
168- AND json_valid(event.event_json)
169- AND json_type(event.event_json, '$.timestamp') IN ('integer', 'real', 'text')
170- THEN json_extract(event.event_json, '$.timestamp')
171- ELSE NULL
172- END` . as ( "timestamp" ) ,
204+ sqliteBoundarySerializedBytes ( ) . as ( "serialized_bytes" ) ,
205+ sqliteBoundaryJsonValid ( ) . as ( "json_valid" ) ,
206+ sqliteBoundaryFirstKeptEntryId ( ) . as ( "first_kept_entry_id" ) ,
207+ sqliteBoundarySummary ( ) . as ( "summary" ) ,
208+ sqliteBoundaryTimestamp ( ) . as ( "timestamp" ) ,
173209 ] )
174210 . where ( "active.session_id" , "=" , projection . resolved . sessionId )
175211 . where ( "identity.event_type" , "=" , eventType )
@@ -431,10 +467,8 @@ export function readBoundedContextMessageTail(
431467 )
432468 . select ( [
433469 "active.message_position" ,
434- /* kysely-allow-raw: inspect role and bytes without materializing message payloads. */
435- sql < string | null > `CASE WHEN json_valid(event.event_json)
436- THEN json_extract(event.event_json, '$.message.role') ELSE NULL END` . as ( "message_role" ) ,
437- sql < number > `LENGTH(CAST(event.event_json AS BLOB)) + 1` . as ( "serialized_bytes" ) ,
470+ sqliteContextMessageRole ( ) . as ( "message_role" ) ,
471+ sqliteContextMessageSerializedBytes ( ) . as ( "serialized_bytes" ) ,
438472 ] )
439473 . where ( "active.session_id" , "=" , projection . resolved . sessionId )
440474 . where ( "active.message_position" , "is not" , null )
0 commit comments