11import {
22 ORCHESTRATION_CACHE_SCHEMA_VERSION ,
33 ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION ,
4- StoredOrchestrationShellSnapshot ,
54} from "@t3tools/client-runtime/platform" ;
65import {
76 CommandId ,
@@ -17,16 +16,11 @@ import {
1716import { describe , expect , it } from "@effect/vitest" ;
1817import * as DateTime from "effect/DateTime" ;
1918import * as Effect from "effect/Effect" ;
20- import * as Fiber from "effect/Fiber" ;
2119import * as Logger from "effect/Logger" ;
22- import * as Schema from "effect/Schema" ;
23- import { vi } from "vite-plus/test" ;
24- import * as Deferred from "effect/Deferred" ;
2520import * as Option from "effect/Option" ;
2621
2722import * as MobileDatabase from "../persistence/mobile-database" ;
2823import { make } from "./environment-cache-store" ;
29- import { makeStoredShellSnapshotEncoder } from "./shell-cache-encoding" ;
3024
3125const ENVIRONMENT_ID = EnvironmentId . make ( "environment-1" ) ;
3226const PROJECT_ID = ProjectId . make ( "project-1" ) ;
@@ -408,258 +402,3 @@ describe("mobile SQLite environment cache store", () => {
408402 } ) ,
409403 ) ;
410404} ) ;
411-
412- describe ( "cooperative shell cache encoding" , ( ) => {
413- // A fresh encoder per call keeps these cases cold; the cache has its own cases below.
414- const encodeStoredShellSnapshot = ( stored : typeof StoredOrchestrationShellSnapshot . Type ) =>
415- makeStoredShellSnapshotEncoder ( ) ( stored ) ;
416- const encodeOriginal = Schema . encodeEffect (
417- Schema . fromJsonString ( StoredOrchestrationShellSnapshot ) ,
418- ) ;
419- const stored = {
420- schemaVersion : ORCHESTRATION_CACHE_SCHEMA_VERSION ,
421- environmentId : ENVIRONMENT_ID ,
422- snapshot : {
423- ...SHELL_SNAPSHOT ,
424- projects : SHELL_SNAPSHOT . projects . map ( ( project ) => ( {
425- ...project ,
426- title : " Project " ,
427- unexpected : "drop this field" ,
428- } ) ) ,
429- threads : Array . from ( { length : 64 } , ( _ , index ) => ( {
430- ...SHELL_SNAPSHOT . threads [ 0 ] ! ,
431- id : ThreadId . make ( `thread-${ index } ` ) ,
432- branch : " main " ,
433- activityRunStartedAt : index % 2 === 0 ? NOW : null ,
434- unsettledAt : index % 2 === 0 ? NOW : null ,
435- modelSelection : {
436- instanceId : PROVIDER_INSTANCE_ID ,
437- model : " gpt-5.4 " ,
438- unexpected : "drop this field" ,
439- } ,
440- unexpected : "drop this field" ,
441- } ) ) ,
442- archivedThreads : [ { ...SHELL_SNAPSHOT . threads [ 0 ] ! , archivedAt : NOW } ] ,
443- unexpected : "drop this field" ,
444- } ,
445- unexpected : "drop this field" ,
446- } ;
447-
448- it . effect ( "preserves exact JSON, transforms, nulls, dates, and unknown-field stripping" , ( ) =>
449- Effect . gen ( function * ( ) {
450- const expected = yield * encodeOriginal ( stored ) ;
451- const actual = yield * encodeStoredShellSnapshot ( stored ) ;
452- expect ( actual ) . toBe ( expected ) ;
453- const parsed = JSON . parse ( actual ) ;
454- expect ( parsed ) . not . toHaveProperty ( "unexpected" ) ;
455- expect ( parsed . snapshot ) . not . toHaveProperty ( "unexpected" ) ;
456- expect ( parsed . snapshot . projects [ 0 ] ) . not . toHaveProperty ( "unexpected" ) ;
457- expect ( parsed . snapshot . projects [ 0 ] . title ) . toBe ( "Project" ) ;
458- expect ( parsed . snapshot . threads [ 0 ] ) . not . toHaveProperty ( "unexpected" ) ;
459- expect ( parsed . snapshot . threads [ 0 ] . branch ) . toBe ( "main" ) ;
460- expect ( parsed . snapshot . threads [ 0 ] . modelSelection ) . toEqual ( {
461- instanceId : "codex" ,
462- model : "gpt-5.4" ,
463- } ) ;
464- expect ( parsed . snapshot . threads [ 0 ] . activityRunStartedAt ) . toBe ( DateTime . formatIso ( NOW ) ) ;
465- expect ( parsed . snapshot . threads [ 1 ] . activityRunStartedAt ) . toBeNull ( ) ;
466- expect ( parsed . snapshot . archivedThreads [ 0 ] . archivedAt ) . toBe ( DateTime . formatIso ( NOW ) ) ;
467- } ) ,
468- ) ;
469-
470- it . effect ( "preserves optional fields and dates across chunk boundaries and archived rows" , ( ) =>
471- Effect . gen ( function * ( ) {
472- const base = SHELL_SNAPSHOT . threads [ 0 ] ! ;
473- const { titleRegeneration : _ , unsettledAt : __ , ...withoutOptional } = base ;
474- const row = ( index : number ) =>
475- index % 3 === 0
476- ? { ...withoutOptional , id : ThreadId . make ( `row-${ index } ` ) }
477- : {
478- ...base ,
479- id : ThreadId . make ( `row-${ index } ` ) ,
480- snoozedUntil : DateTime . add ( NOW , { minutes : index } ) ,
481- pinnedAt : index % 2 === 0 ? null : NOW ,
482- } ;
483- const mixed = {
484- ...stored ,
485- snapshot : {
486- ...stored . snapshot ,
487- threads : Array . from ( { length : 65 } , ( _ , index ) => row ( index ) ) ,
488- archivedThreads : Array . from ( { length : 33 } , ( _ , index ) => ( {
489- ...row ( index + 100 ) ,
490- archivedAt : DateTime . add ( NOW , { hours : index } ) ,
491- } ) ) ,
492- } ,
493- } ;
494- const actual = yield * encodeStoredShellSnapshot ( mixed ) ;
495- expect ( actual ) . toBe ( yield * encodeOriginal ( mixed ) ) ;
496- const parsed = JSON . parse ( actual ) ;
497- expect ( parsed . snapshot . threads ) . toHaveLength ( 65 ) ;
498- expect ( parsed . snapshot . threads [ 63 ] ) . not . toHaveProperty ( "titleRegeneration" ) ;
499- expect ( parsed . snapshot . threads [ 63 ] ) . not . toHaveProperty ( "unsettledAt" ) ;
500- expect ( parsed . snapshot . threads [ 64 ] . snoozedUntil ) . toBe (
501- DateTime . formatIso ( DateTime . add ( NOW , { minutes : 64 } ) ) ,
502- ) ;
503- expect ( parsed . snapshot . archivedThreads ) . toHaveLength ( 33 ) ;
504- expect ( parsed . snapshot . archivedThreads [ 32 ] . archivedAt ) . toBe (
505- DateTime . formatIso ( DateTime . add ( NOW , { hours : 32 } ) ) ,
506- ) ;
507- } ) ,
508- ) ;
509-
510- it . effect ( "yields to a host timer between every 32-row chunk" , ( ) =>
511- Effect . gen ( function * ( ) {
512- const setTimer = vi . spyOn ( globalThis , "setTimeout" ) ;
513- try {
514- yield * encodeStoredShellSnapshot ( {
515- ...stored ,
516- snapshot : {
517- ...stored . snapshot ,
518- threads : Array . from ( { length : 65 } , ( ) => ( { ...stored . snapshot . threads [ 0 ] ! } ) ) ,
519- archivedThreads : Array . from ( { length : 33 } , ( ) => ( {
520- ...stored . snapshot . archivedThreads [ 0 ] ! ,
521- } ) ) ,
522- } ,
523- } ) ;
524- // Five row chunks (3 active + 2 archived): four yields between them, one before the envelope.
525- expect ( setTimer ) . toHaveBeenCalledTimes ( 5 ) ;
526- } finally {
527- vi . restoreAllMocks ( ) ;
528- }
529- } ) ,
530- ) ;
531-
532- it . effect ( "still rejects invalid active and archived rows after an encoding chunk yields" , ( ) =>
533- Effect . gen ( function * ( ) {
534- const invalidRow = { ...stored . snapshot . threads [ 0 ] ! , itemCount : - 1 } ;
535- const invalidActive = {
536- ...stored ,
537- snapshot : { ...stored . snapshot , threads : [ ...stored . snapshot . threads , invalidRow ] } ,
538- } ;
539- const invalidArchived = {
540- ...stored ,
541- snapshot : { ...stored . snapshot , archivedThreads : [ invalidRow ] } ,
542- } ;
543- const invalidEnvelope = { ...stored , snapshot : { ...stored . snapshot , snapshotSequence : - 1 } } ;
544- expect ( yield * Effect . isFailure ( encodeStoredShellSnapshot ( invalidActive ) ) ) . toBe ( true ) ;
545- expect ( yield * Effect . isFailure ( encodeStoredShellSnapshot ( invalidArchived ) ) ) . toBe ( true ) ;
546- expect ( yield * Effect . isFailure ( encodeOriginal ( invalidEnvelope ) ) ) . toBe ( true ) ;
547- expect ( yield * Effect . isFailure ( encodeStoredShellSnapshot ( invalidEnvelope ) ) ) . toBe ( true ) ;
548- } ) ,
549- ) ;
550-
551- it . effect ( "keeps concurrent saves independent" , ( ) =>
552- Effect . gen ( function * ( ) {
553- const other = {
554- ...stored ,
555- environmentId : EnvironmentId . make ( "environment-2" ) ,
556- snapshot : {
557- ...stored . snapshot ,
558- threads : stored . snapshot . threads . slice ( 0 , 40 ) . map ( ( thread ) => ( {
559- ...thread ,
560- title : `Other ${ thread . id } ` ,
561- } ) ) ,
562- archivedThreads : [ ] ,
563- } ,
564- } ;
565- const [ first , second ] = yield * Effect . all (
566- [ encodeStoredShellSnapshot ( stored ) , encodeStoredShellSnapshot ( other ) ] ,
567- { concurrency : "unbounded" } ,
568- ) ;
569- expect ( first ) . toBe ( yield * encodeOriginal ( stored ) ) ;
570- expect ( second ) . toBe ( yield * encodeOriginal ( other ) ) ;
571- } ) ,
572- ) ;
573-
574- it . effect ( "cancels the pending host timer when encoding is interrupted" , ( ) =>
575- Effect . gen ( function * ( ) {
576- vi . useFakeTimers ( { toFake : [ "setTimeout" , "clearTimeout" ] } ) ;
577- const fakeSetTimeout = globalThis . setTimeout ;
578- const scheduled = yield * Deferred . make < ReturnType < typeof setTimeout > > ( ) ;
579- vi . spyOn ( globalThis , "setTimeout" ) . mockImplementation ( ( ...args ) => {
580- const timer = fakeSetTimeout ( ...args ) ;
581- Deferred . doneUnsafe ( scheduled , Effect . succeed ( timer ) ) ;
582- return timer ;
583- } ) ;
584- const clearTimer = vi . spyOn ( globalThis , "clearTimeout" ) ;
585- const fiber = yield * Effect . forkScoped (
586- Effect . suspend ( ( ) => encodeStoredShellSnapshot ( stored ) ) ,
587- ) ;
588- try {
589- const pendingTimer = yield * Deferred . await ( scheduled ) ;
590- expect ( vi . getTimerCount ( ) ) . toBe ( 1 ) ;
591- yield * Fiber . interrupt ( fiber ) ;
592- expect ( clearTimer ) . toHaveBeenCalledWith ( pendingTimer ) ;
593- expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
594- } finally {
595- yield * Fiber . interrupt ( fiber ) ;
596- vi . restoreAllMocks ( ) ;
597- vi . useRealTimers ( ) ;
598- }
599- } ) ,
600- ) ;
601-
602- it . effect ( "reuses encoded rows across saves without changing the payload" , ( ) =>
603- Effect . gen ( function * ( ) {
604- const encode = makeStoredShellSnapshotEncoder ( ) ;
605- expect ( yield * encode ( stored ) ) . toBe ( yield * encodeOriginal ( stored ) ) ;
606-
607- const setTimer = vi . spyOn ( globalThis , "setTimeout" ) ;
608- try {
609- // Same row references, new envelope: no row work, so no host yields.
610- const resaved = {
611- ...stored ,
612- snapshot : { ...stored . snapshot , snapshotSequence : 4 } ,
613- } ;
614- expect ( yield * encode ( resaved ) ) . toBe ( yield * encodeOriginal ( resaved ) ) ;
615- expect ( setTimer ) . not . toHaveBeenCalled ( ) ;
616- } finally {
617- vi . restoreAllMocks ( ) ;
618- }
619- } ) ,
620- ) ;
621-
622- it . effect ( "encodes replaced, added, and moved-to-archive rows on a warm encoder" , ( ) =>
623- Effect . gen ( function * ( ) {
624- const encode = makeStoredShellSnapshotEncoder ( ) ;
625- yield * encode ( stored ) ;
626- const [ first , second , ...rest ] = stored . snapshot . threads ;
627- const next = {
628- ...stored ,
629- snapshot : {
630- ...stored . snapshot ,
631- threads : [
632- { ...first ! , title : "Renamed" , itemCount : 9 } ,
633- ...rest ,
634- { ...first ! , id : ThreadId . make ( "thread-new" ) , branch : " feature " } ,
635- ] ,
636- archivedThreads : [
637- ...stored . snapshot . archivedThreads ,
638- { ...second ! , archivedAt : DateTime . add ( NOW , { hours : 1 } ) } ,
639- ] ,
640- } ,
641- } ;
642- const actual = yield * encode ( next ) ;
643- expect ( actual ) . toBe ( yield * encodeOriginal ( next ) ) ;
644- const parsed = JSON . parse ( actual ) ;
645- expect ( parsed . snapshot . threads [ 0 ] . title ) . toBe ( "Renamed" ) ;
646- expect ( parsed . snapshot . threads . at ( - 1 ) . branch ) . toBe ( "feature" ) ;
647- expect ( parsed . snapshot . archivedThreads ) . toHaveLength ( 2 ) ;
648- } ) ,
649- ) ;
650-
651- it . effect ( "does not cache rows from a failed encode" , ( ) =>
652- Effect . gen ( function * ( ) {
653- const encode = makeStoredShellSnapshotEncoder ( ) ;
654- const invalidRow = { ...stored . snapshot . threads [ 0 ] ! , itemCount : - 1 } ;
655- const invalid = {
656- ...stored ,
657- snapshot : { ...stored . snapshot , threads : [ ...stored . snapshot . threads , invalidRow ] } ,
658- } ;
659- expect ( yield * Effect . isFailure ( encode ( invalid ) ) ) . toBe ( true ) ;
660- expect ( yield * Effect . isFailure ( encode ( invalid ) ) ) . toBe ( true ) ;
661- // Valid rows from the failed save still encode correctly afterwards.
662- expect ( yield * encode ( stored ) ) . toBe ( yield * encodeOriginal ( stored ) ) ;
663- } ) ,
664- ) ;
665- } ) ;
0 commit comments