@@ -29,8 +29,12 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";
2929import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness" ;
3030
3131import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts" ;
32+ import * as CheckpointDiffQuery from "../checkpointing/CheckpointDiffQuery.ts" ;
33+ import * as CheckpointStore from "../checkpointing/CheckpointStore.ts" ;
3234import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts" ;
35+ import { checkpointRefForScopeOrdinal } from "./CheckpointService.ts" ;
3336import * as ProjectionStore from "./ProjectionStore.ts" ;
37+ import * as ThreadManagement from "./ThreadManagementService.ts" ;
3438import {
3539 buildBoundedThreadProjection ,
3640 decodeThreadHistoryCursor ,
@@ -2552,7 +2556,7 @@ it.layer(TestLayer)("ProjectionStoreV2", (it) => {
25522556 const nodeId = NodeId . make ( "node:checkpoint-context" ) ;
25532557 const scopeId = CheckpointScopeId . make ( "scope:checkpoint-context" ) ;
25542558 const checkpointId = CheckpointId . make ( "checkpoint:checkpoint-context" ) ;
2555- const ref = CheckpointRef . make ( "refs/t3/checkpoint-context/1" ) ;
2559+ const ref = checkpointRefForScopeOrdinal ( { scopeId , ordinalWithinScope : 1 } ) ;
25562560 yield * projectionStore . apply ( {
25572561 id : EventId . make ( "event:checkpoint-context:thread" ) ,
25582562 type : "thread.created" ,
@@ -2644,6 +2648,99 @@ it.layer(TestLayer)("ProjectionStoreV2", (it) => {
26442648 capturedAt : now ,
26452649 } ,
26462650 } ) ;
2651+ const records = yield * projectionStore . getThreadRecords ( threadId , [
2652+ "runs" ,
2653+ "checkpoints" ,
2654+ "checkpointScopes" ,
2655+ ] ) ;
2656+ const nextRef = checkpointRefForScopeOrdinal ( { scopeId, ordinalWithinScope : 2 } ) ;
2657+ const nextRunId = RunId . make ( "run:checkpoint-context:next" ) ;
2658+ const nextCheckpoint = {
2659+ ...records . checkpoints [ 0 ] ! ,
2660+ id : CheckpointId . make ( "checkpoint:checkpoint-context:next" ) ,
2661+ runId : nextRunId ,
2662+ appRunOrdinal : 2 ,
2663+ ordinalWithinScope : 2 ,
2664+ ref : nextRef ,
2665+ files : [
2666+ {
2667+ path : "git.txt" ,
2668+ kind : "modified" ,
2669+ additions : 1 ,
2670+ deletions : 0 ,
2671+ origin : "git" as const ,
2672+ } ,
2673+ ] ,
2674+ } ;
2675+ yield * projectionStore . apply ( {
2676+ id : EventId . make ( "event:checkpoint-context:next-run" ) ,
2677+ type : "run.created" ,
2678+ threadId,
2679+ occurredAt : now ,
2680+ payload : { ...records . runs [ 0 ] ! , id : nextRunId , ordinal : 2 } ,
2681+ } ) ;
2682+ const otherScopeId = CheckpointScopeId . make ( "scope:checkpoint-context:other" ) ;
2683+ yield * projectionStore . apply ( {
2684+ id : EventId . make ( "event:checkpoint-context:other-scope" ) ,
2685+ type : "checkpoint-scope.created" ,
2686+ threadId,
2687+ occurredAt : now ,
2688+ payload : { ...records . checkpointScopes [ 0 ] ! , id : otherScopeId , ordinalWithinParent : 1 } ,
2689+ } ) ;
2690+ const getGitChangedPaths = vi . fn ( ( ) => Effect . succeed ( [ "git.txt" ] ) ) ;
2691+ const query = yield * CheckpointDiffQuery . make . pipe (
2692+ Effect . provide (
2693+ Layer . mergeAll (
2694+ Layer . mock ( ThreadManagement . ThreadManagementService ) ( {
2695+ getCheckpointContext : ( id ) =>
2696+ projectionStore . getCheckpointContext ( id ) . pipe ( Effect . orDie ) ,
2697+ getThreadRecords : ( id , fields , filter ) =>
2698+ projectionStore . getThreadRecords ( id , fields , filter ) . pipe ( Effect . orDie ) ,
2699+ } ) ,
2700+ Layer . mock ( CheckpointStore . CheckpointStore ) ( {
2701+ getGitChangedPaths,
2702+ diffCheckpoints : ( input ) =>
2703+ Effect . succeed (
2704+ input . format === "numstat"
2705+ ? "1\t0\tgit.txt\u00001\t1\tworkspace.txt\0"
2706+ : ( input . filePaths ?? [ "workspace.txt" , "git.txt" ] ) . join ( "\n" ) ,
2707+ ) ,
2708+ } ) ,
2709+ ) ,
2710+ ) ,
2711+ ) ;
2712+ for ( const [ name , fromTurnCount , checkpoint , reuse ] of [
2713+ [
2714+ "baseline" ,
2715+ 0 ,
2716+ { ...records . checkpoints [ 0 ] ! , appRunOrdinal : 1 , files : nextCheckpoint . files } ,
2717+ true ,
2718+ ] ,
2719+ [ "adjacent" , 1 , nextCheckpoint , true ] ,
2720+ [ "wider range" , 0 , nextCheckpoint , false ] ,
2721+ [ "other scope" , 1 , { ...nextCheckpoint , scopeId : otherScopeId } , false ] ,
2722+ [ "mismatched refs" , 1 , { ...nextCheckpoint , ordinalWithinScope : 3 } , false ] ,
2723+ [ "no origins" , 1 , { ...nextCheckpoint , files : [ ] } , false ] ,
2724+ ] as const ) {
2725+ yield * projectionStore . apply ( {
2726+ id : EventId . make ( `event:checkpoint-context:${ name } ` ) ,
2727+ type : "checkpoint.captured" ,
2728+ threadId,
2729+ occurredAt : now ,
2730+ payload : checkpoint ,
2731+ } ) ;
2732+ getGitChangedPaths . mockClear ( ) ;
2733+ const input = { threadId, fromTurnCount, toTurnCount : checkpoint . appRunOrdinal } ;
2734+ const filtered = yield * query . getTurnDiff ( { ...input , includeGitChanges : false } ) ;
2735+ assert . strictEqual ( filtered . diff , "workspace.txt" , name ) ;
2736+ assert . strictEqual ( filtered . gitFileCount , 1 , name ) ;
2737+ const full = yield * query . getTurnDiff ( { ...input , includeGitChanges : true } ) ;
2738+ assert . strictEqual ( full . diff , "workspace.txt\ngit.txt" , name ) ;
2739+ assert . strictEqual ( full . gitFileCount , 1 , name ) ;
2740+ assert . strictEqual ( ( yield * query . getTurnDiff ( input ) ) . diff , full . diff , name ) ;
2741+ assert . strictEqual ( getGitChangedPaths . mock . calls . length , reuse ? 0 : 2 , name ) ;
2742+ }
2743+ const checkpointContext = yield * projectionStore . getCheckpointContext ( threadId ) ;
26472744 // Old transcript shapes must not make a metadata-only diff unreadable.
26482745 yield * sql `
26492746 INSERT INTO orchestration_v2_projection_turn_items (
@@ -2671,11 +2768,7 @@ it.layer(TestLayer)("ProjectionStoreV2", (it) => {
26712768 SET payload_json = json_set(payload_json, '$.files', 'obsolete file summary')
26722769 WHERE checkpoint_id = ${ checkpointId }
26732770 ` ;
2674- assert . deepEqual ( yield * projectionStore . getCheckpointContext ( threadId ) , {
2675- runs : [ { id : runId , ordinal : 1 , status : "completed" } ] ,
2676- checkpointScopes : [ { id : scopeId , runId, kind : "root_run" , cwd : "/repo/worktree" } ] ,
2677- checkpoints : [ { scopeId, runId, appRunOrdinal : 1 , status : "ready" , ref } ] ,
2678- } ) ;
2771+ assert . deepEqual ( yield * projectionStore . getCheckpointContext ( threadId ) , checkpointContext ) ;
26792772 const excluded = yield * projectionStore . getThreadRecords ( threadId , [ "checkpoints" ] , {
26802773 checkpointRefs : [ CheckpointRef . make ( "refs/t3/checkpoint-context/other" ) ] ,
26812774 } ) ;
0 commit comments