22 ORCHESTRATION_CACHE_SCHEMA_VERSION ,
33 ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION ,
44 COMPACT_THREAD_CACHE_SCHEMA_VERSION ,
5+ type ConnectionPersistenceError ,
56 StoredOrchestrationThreadSnapshot ,
67} from "@t3tools/client-runtime/platform" ;
78import {
@@ -19,6 +20,7 @@ import {
1920import { describe , expect , it } from "@effect/vitest" ;
2021import * as DateTime from "effect/DateTime" ;
2122import * as Effect from "effect/Effect" ;
23+ import * as Fiber from "effect/Fiber" ;
2224import * as Logger from "effect/Logger" ;
2325import * as Option from "effect/Option" ;
2426import * as Schema from "effect/Schema" ;
@@ -213,6 +215,35 @@ function threadSnapshotWithItems(): OrchestrationV2ThreadDetailSnapshot {
213215 } ;
214216}
215217
218+ // More rows than one encoding chunk, so a cold save yields to the host mid-encode.
219+ function threadSnapshotWithManyItems (
220+ snapshotSequence : number ,
221+ message = "A settled local notice." ,
222+ ) : OrchestrationV2ThreadDetailSnapshot {
223+ const base = threadSnapshotWithItems ( ) ;
224+ const turnItems = Array . from ( { length : 40 } , ( _ , index ) => ( {
225+ ...base . projection . turnItems [ 0 ] ! ,
226+ id : TurnItemId . make ( `notice-${ snapshotSequence } -${ index } ` ) ,
227+ ordinal : index ,
228+ message,
229+ } ) ) ;
230+ return {
231+ ...THREAD_SNAPSHOT ,
232+ snapshotSequence,
233+ projection : {
234+ ...THREAD_SNAPSHOT . projection ,
235+ turnItems,
236+ visibleTurnItems : turnItems . map ( ( item , position ) => ( {
237+ position,
238+ visibility : "local" as const ,
239+ sourceThreadId : THREAD_ID ,
240+ sourceItemId : item . id ,
241+ item,
242+ } ) ) ,
243+ } ,
244+ } ;
245+ }
246+
216247function makeDatabase ( ) {
217248 const values = new Map < string , string > ( ) ;
218249 const schemaVersions = new Map < string , number > ( ) ;
@@ -608,6 +639,53 @@ describe("mobile SQLite environment cache store", () => {
608639 } ) . pipe ( Effect . provide ( Logger . layer ( [ logger ] , { mergeWithExisting : false } ) ) ) ;
609640 } ) ;
610641
642+ it . effect ( "does not write a save that a removal, clear, or newer save overtook" , ( ) =>
643+ Effect . gen ( function * ( ) {
644+ const id = cacheId ( ENVIRONMENT_ID , "thread" , THREAD_ID ) ;
645+ const overtake = Effect . fn ( "overtake" ) ( function * (
646+ later : (
647+ store : Effect . Success < ReturnType < typeof make > > ,
648+ ) => Effect . Effect < void , ConnectionPersistenceError > ,
649+ ) {
650+ const memory = makeDatabase ( ) ;
651+ const store = yield * make ( ) . pipe (
652+ Effect . provideService ( MobileDatabase . MobileDatabase , memory . database ) ,
653+ ) ;
654+ const older = yield * store
655+ . saveThread ( ENVIRONMENT_ID , threadSnapshotWithManyItems ( 1 ) )
656+ . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
657+ yield * later ( store ) ;
658+ yield * Fiber . join ( older ) ;
659+ return { memory, store } ;
660+ } ) ;
661+
662+ const removed = yield * overtake ( ( store ) => store . removeThread ( ENVIRONMENT_ID , THREAD_ID ) ) ;
663+ expect ( removed . memory . values . has ( id ) ) . toBe ( false ) ;
664+ const cleared = yield * overtake ( ( store ) => store . clear ( ENVIRONMENT_ID ) ) ;
665+ expect ( cleared . memory . values . has ( id ) ) . toBe ( false ) ;
666+ const replaced = yield * overtake ( ( store ) =>
667+ store . saveThread ( ENVIRONMENT_ID , { ...THREAD_SNAPSHOT , snapshotSequence : 2 } ) ,
668+ ) ;
669+ const kept = yield * replaced . store . loadThread ( ENVIRONMENT_ID , THREAD_ID ) ;
670+ expect ( Option . getOrThrow ( kept ) . snapshotSequence ) . toBe ( 2 ) ;
671+ } ) ,
672+ ) ;
673+
674+ it . effect ( "removes the older copy of a thread that grew past the cache limit" , ( ) =>
675+ Effect . gen ( function * ( ) {
676+ const memory = makeDatabase ( ) ;
677+ const store = yield * make ( ) . pipe (
678+ Effect . provideService ( MobileDatabase . MobileDatabase , memory . database ) ,
679+ ) ;
680+ const id = cacheId ( ENVIRONMENT_ID , "thread" , THREAD_ID ) ;
681+ yield * store . saveThread ( ENVIRONMENT_ID , threadSnapshotWithItems ( ) ) ;
682+ expect ( memory . values . has ( id ) ) . toBe ( true ) ;
683+ yield * store . saveThread ( ENVIRONMENT_ID , threadSnapshotWithManyItems ( 2 , "x" . repeat ( 200_000 ) ) ) ;
684+ expect ( memory . values . has ( id ) ) . toBe ( false ) ;
685+ expect ( memory . removed ) . toEqual ( [ id ] ) ;
686+ } ) ,
687+ ) ;
688+
611689 it . effect ( "deletes a corrupt cache record and treats it as a miss" , ( ) =>
612690 Effect . gen ( function * ( ) {
613691 const memory = makeDatabase ( ) ;
0 commit comments