@@ -3643,6 +3643,85 @@ it.effect("reads one sweep thread and its projects like the shell snapshot", ()
36433643 } ) . pipe ( Effect . provide ( layer ) ) ;
36443644} ) ;
36453645
3646+ it . effect ( "reads a full sweep from unsettled threads and every project" , ( ) => {
3647+ const resolved : string [ ] = [ ] ;
3648+ const layer = OrchestrationProjectionSnapshotQueryLive . pipe (
3649+ Layer . provide ( ThreadBackgroundLiveness . layer ) ,
3650+ Layer . provide ( ThreadPlanProgress . layer ) ,
3651+ Layer . provide (
3652+ Layer . succeed ( RepositoryIdentityResolver . RepositoryIdentityResolver , {
3653+ resolve : ( root ) =>
3654+ Effect . sync ( ( ) => {
3655+ resolved . push ( root ) ;
3656+ return null ;
3657+ } ) ,
3658+ } ) ,
3659+ ) ,
3660+ Layer . provideMerge ( SqlitePersistenceMemory ) ,
3661+ ) ;
3662+ return Effect . gen ( function * ( ) {
3663+ const sql = yield * SqlClient . SqlClient ;
3664+ const query = yield * ProjectionSnapshotQuery ;
3665+ yield * sql `INSERT INTO projection_projects (project_id, title, workspace_root, scripts_json, created_at, updated_at)
3666+ VALUES ('p1', 'One', '/one', '[]', '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z'),
3667+ ('p2', 'Two', '/two', '[]', '2026-09-02T00:00:00Z', '2026-09-02T00:00:00Z'),
3668+ ('p3', 'Three', '/three', '[]', '2026-09-03T00:00:00Z', '2026-09-03T00:00:00Z'),
3669+ ('p4', 'Four', '/four', '[]', '2026-09-04T00:00:00Z', '2026-09-04T00:00:00Z')` ;
3670+ yield * sql `INSERT INTO projection_threads (thread_id, project_id, title, model_selection_json, runtime_mode, interaction_mode, branch_pull_request_json, latest_turn_id, created_at, updated_at, archived_at, settled_override, settled_at)
3671+ VALUES
3672+ ('t-open', 'p1', 'Open', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, 'turn-open', '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, NULL, NULL),
3673+ ('t-resumed', 'p1', 'Resumed', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, NULL, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, 'active', NULL),
3674+ ('t-branch', 'p1', 'Branch', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default',
3675+ '{"projectId":"p2","repository":"acme/web","number":8,"url":"https://github.com/acme/web/pull/8"}',
3676+ NULL, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, NULL, NULL),
3677+ ('t-settled', 'p3', 'Settled', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, 'turn-settled', '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, 'settled', '2026-09-03T00:00:00Z'),
3678+ ('t-archived', 'p4', 'Archived', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, NULL, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', '2026-09-04T00:00:00Z', NULL, NULL)` ;
3679+ // The open and the settled thread both have a row in each joined table. The
3680+ // settled thread's turn and session are the newest rows, so updatedAt shows
3681+ // whether those two reads skip it.
3682+ yield * sql `INSERT INTO projection_thread_pull_requests (thread_id, host, repository, number, url, source, linked_at)
3683+ VALUES ('t-open', 'github.com', 'acme/web', 7, 'https://github.com/acme/web/pull/7', 'agent', '2026-09-02T00:00:00Z'),
3684+ ('t-settled', 'github.com', 'acme/web', 9, 'https://github.com/acme/web/pull/9', 'agent', '2026-09-02T00:00:00Z')` ;
3685+ yield * sql `INSERT INTO projection_turns (thread_id, turn_id, state, requested_at, checkpoint_files_json)
3686+ VALUES ('t-open', 'turn-open', 'completed', '2026-09-02T00:00:00Z', '[]'),
3687+ ('t-settled', 'turn-settled', 'completed', '2026-09-09T00:00:00Z', '[]')` ;
3688+ yield * sql `INSERT INTO projection_thread_sessions (thread_id, status, provider_name, active_turn_id, last_error, updated_at)
3689+ VALUES ('t-open', 'ready', 'codex', NULL, NULL, '2026-09-02T00:00:00Z'),
3690+ ('t-settled', 'stopped', 'codex', NULL, NULL, '2026-09-10T00:00:00Z')` ;
3691+
3692+ const full = yield * query . getShellSnapshot ( ) ;
3693+ // The settled thread's rows must reach the full read, or skipping them proves nothing.
3694+ const settled = full . threads . find ( ( thread ) => thread . id === ThreadId . make ( "t-settled" ) ) ;
3695+ assert . strictEqual ( settled ?. pullRequests [ 0 ] ?. number , 9 ) ;
3696+ assert . strictEqual ( settled ?. latestTurn ?. turnId , asTurnId ( "turn-settled" ) ) ;
3697+ assert . strictEqual ( settled ?. session ?. status , "stopped" ) ;
3698+ assert . strictEqual ( full . updatedAt , "2026-09-10T00:00:00Z" ) ;
3699+ resolved . length = 0 ;
3700+
3701+ const sweep = yield * readSweepSnapshot ( query , null ) ;
3702+ assert . strictEqual ( sweep . snapshotSequence , full . snapshotSequence ) ;
3703+ assert . deepStrictEqual (
3704+ sweep . threads ,
3705+ full . threads . filter ( ( thread ) => thread . id !== ThreadId . make ( "t-settled" ) ) ,
3706+ ) ;
3707+ assert . deepStrictEqual (
3708+ sweep . threads . map ( ( thread ) => thread . id ) ,
3709+ [ "t-branch" , "t-open" , "t-resumed" ] ,
3710+ ) ;
3711+ // Like the full read, the sweep resolves every project, so it keeps the
3712+ // repository identity cache warm for client connects.
3713+ assert . deepStrictEqual ( sweep . projects , full . projects ) ;
3714+ assert . deepStrictEqual ( resolved . toSorted ( ) , [ "/four" , "/one" , "/three" , "/two" ] ) ;
3715+ // A settled thread's link that no longer decodes breaks the full read, but
3716+ // not the sweep, which never reads it.
3717+ yield * sql `UPDATE projection_thread_pull_requests SET snapshot_json = 'invalid-json' WHERE thread_id = 't-settled'` ;
3718+ assert . strictEqual ( ( yield * Effect . exit ( query . getShellSnapshot ( ) ) ) . _tag , "Failure" ) ;
3719+ const unsettled = yield * query . getShellSnapshot ( { unsettledOnly : true } ) ;
3720+ assert . deepStrictEqual ( unsettled . threads , sweep . threads ) ;
3721+ assert . strictEqual ( unsettled . updatedAt , "2026-09-04T00:00:00Z" ) ;
3722+ } ) . pipe ( Effect . provide ( layer ) ) ;
3723+ } ) ;
3724+
36463725projectionSnapshotLayer ( "ProjectionSnapshotQuery activities by kind" , ( it ) => {
36473726 it . effect ( "lists one kind across active threads only, without hydrating the threads" , ( ) =>
36483727 Effect . gen ( function * ( ) {
0 commit comments