33 EventId ,
44 ProjectId ,
55 ThreadId ,
6+ TurnId ,
67 ProviderInstanceId ,
78} from "@t3tools/contracts" ;
89import { legacyStaleRequestFailureDetails } from "@t3tools/shared/requestActivity" ;
@@ -11,20 +12,24 @@ import * as Effect from "effect/Effect";
1112import * as Layer from "effect/Layer" ;
1213import * as Option from "effect/Option" ;
1314import * as SqlClient from "effect/unstable/sql/SqlClient" ;
15+ import * as Statement from "effect/unstable/sql/Statement" ;
1416
1517import { SqlitePersistenceMemory } from "./Sqlite.ts" ;
1618import { ProjectionProjectRepositoryLive } from "./ProjectionProjects.ts" ;
1719import { ProjectionThreadRepositoryLive } from "./ProjectionThreads.ts" ;
1820import { ProjectionThreadActivityRepositoryLive } from "./ProjectionThreadActivities.ts" ;
21+ import { ProjectionThreadProposedPlanRepositoryLive } from "./ProjectionThreadProposedPlans.ts" ;
1922import { ProjectionProjectRepository } from "../Services/ProjectionProjects.ts" ;
2023import { ProjectionThreadRepository } from "../Services/ProjectionThreads.ts" ;
2124import { ProjectionThreadActivityRepository } from "../Services/ProjectionThreadActivities.ts" ;
25+ import { ProjectionThreadProposedPlanRepository } from "../Services/ProjectionThreadProposedPlans.ts" ;
2226
2327const projectionRepositoriesLayer = it . layer (
2428 Layer . mergeAll (
2529 ProjectionProjectRepositoryLive . pipe ( Layer . provideMerge ( SqlitePersistenceMemory ) ) ,
2630 ProjectionThreadRepositoryLive . pipe ( Layer . provideMerge ( SqlitePersistenceMemory ) ) ,
2731 ProjectionThreadActivityRepositoryLive . pipe ( Layer . provideMerge ( SqlitePersistenceMemory ) ) ,
32+ ProjectionThreadProposedPlanRepositoryLive . pipe ( Layer . provideMerge ( SqlitePersistenceMemory ) ) ,
2833 SqlitePersistenceMemory ,
2934 ) ,
3035) ;
@@ -203,6 +208,211 @@ projectionRepositoriesLayer("Projection repositories", (it) => {
203208 } ) ,
204209 ) ;
205210
211+ it . effect ( "selects the latest-turn plan before checking implementation status" , ( ) =>
212+ Effect . gen ( function * ( ) {
213+ const plans = yield * ProjectionThreadProposedPlanRepository ;
214+ const threadId = ThreadId . make ( "thread-plan-status" ) ;
215+ const latestTurnId = TurnId . make ( "turn-plan-status-current" ) ;
216+ const firstPlan = {
217+ planId : "plan-status-first" ,
218+ threadId,
219+ turnId : latestTurnId ,
220+ planMarkdown : "# First plan" ,
221+ implementedAt : null ,
222+ implementationThreadId : null ,
223+ createdAt : "2026-03-24T00:00:01.000Z" ,
224+ updatedAt : "2026-03-24T00:00:01.000Z" ,
225+ } ;
226+ yield * plans . upsert ( firstPlan ) ;
227+ yield * plans . upsert ( {
228+ ...firstPlan ,
229+ planId : "plan-status-implemented" ,
230+ implementedAt : "2026-03-24T00:00:02.000Z" ,
231+ createdAt : "2026-03-24T00:00:02.000Z" ,
232+ updatedAt : "2026-03-24T00:00:02.000Z" ,
233+ } ) ;
234+ yield * plans . upsert ( {
235+ ...firstPlan ,
236+ planId : "plan-status-other-turn" ,
237+ turnId : TurnId . make ( "turn-plan-status-old" ) ,
238+ updatedAt : "2026-03-24T00:00:10.000Z" ,
239+ } ) ;
240+
241+ assert . isFalse ( yield * plans . hasActionableByThreadId ( { threadId, latestTurnId } ) ) ;
242+ assert . isTrue ( yield * plans . hasActionableByThreadId ( { threadId, latestTurnId : null } ) ) ;
243+
244+ yield * plans . upsert ( { ...firstPlan , updatedAt : "2026-03-24T00:00:03.000Z" } ) ;
245+ assert . isTrue ( yield * plans . hasActionableByThreadId ( { threadId, latestTurnId } ) ) ;
246+ } ) ,
247+ ) ;
248+
249+ it . effect ( "falls back within the thread when the latest turn has no plan" , ( ) =>
250+ Effect . gen ( function * ( ) {
251+ const plans = yield * ProjectionThreadProposedPlanRepository ;
252+ const threadId = ThreadId . make ( "thread-plan-fallback" ) ;
253+ const latestTurnId = TurnId . make ( "turn-plan-fallback-missing" ) ;
254+ assert . isFalse ( yield * plans . hasActionableByThreadId ( { threadId, latestTurnId } ) ) ;
255+ assert . isFalse ( yield * plans . hasActionableByThreadId ( { threadId, latestTurnId : null } ) ) ;
256+
257+ const firstPlan = {
258+ planId : "plan-fallback-without-turn" ,
259+ threadId,
260+ turnId : null ,
261+ planMarkdown : "# Old plan" ,
262+ implementedAt : "2026-03-24T00:00:01.000Z" ,
263+ implementationThreadId : null ,
264+ createdAt : "2026-03-24T00:00:01.000Z" ,
265+ updatedAt : "2026-03-24T00:00:01.000Z" ,
266+ } ;
267+ yield * plans . upsert ( firstPlan ) ;
268+ yield * plans . upsert ( {
269+ ...firstPlan ,
270+ planId : "plan-fallback-with-turn" ,
271+ turnId : TurnId . make ( "turn-plan-fallback-old" ) ,
272+ implementedAt : null ,
273+ updatedAt : "2026-03-24T00:00:02.000Z" ,
274+ } ) ;
275+ yield * plans . upsert ( {
276+ ...firstPlan ,
277+ planId : "plan-fallback-other-thread" ,
278+ threadId : ThreadId . make ( "thread-plan-fallback-other" ) ,
279+ turnId : latestTurnId ,
280+ updatedAt : "2026-03-24T00:00:03.000Z" ,
281+ } ) ;
282+
283+ assert . isTrue ( yield * plans . hasActionableByThreadId ( { threadId, latestTurnId } ) ) ;
284+ assert . isTrue ( yield * plans . hasActionableByThreadId ( { threadId, latestTurnId : null } ) ) ;
285+ } ) ,
286+ ) ;
287+
288+ it . effect ( "preserves locale ordering and stable ties when selecting plan status" , ( ) =>
289+ Effect . gen ( function * ( ) {
290+ const plans = yield * ProjectionThreadProposedPlanRepository ;
291+ const timestamp = "2026-03-24T00:00:00.000Z" ;
292+ const cases = [
293+ {
294+ name : "mixed-case-ids" ,
295+ expected : "plan-A" . localeCompare ( "plan-a" ) > 0 ,
296+ rows : [
297+ {
298+ planId : "plan-a" ,
299+ implementedAt : timestamp ,
300+ createdAt : timestamp ,
301+ updatedAt : timestamp ,
302+ } ,
303+ { planId : "plan-A" , implementedAt : null , createdAt : timestamp , updatedAt : timestamp } ,
304+ ] ,
305+ } ,
306+ {
307+ name : "equivalent-ids" ,
308+ expected : true ,
309+ rows : [
310+ {
311+ planId : "plan-\u00e9" ,
312+ implementedAt : timestamp ,
313+ createdAt : timestamp ,
314+ updatedAt : timestamp ,
315+ } ,
316+ {
317+ planId : "plan-e\u0301" ,
318+ implementedAt : null ,
319+ createdAt : "2026-03-24T00:00:01.000Z" ,
320+ updatedAt : timestamp ,
321+ } ,
322+ ] ,
323+ } ,
324+ {
325+ name : "timestamp-formats" ,
326+ expected : "2026-03-24T00:00:00+00:00" . localeCompare ( "2026-03-24T00:00:00-01:00" ) > 0 ,
327+ rows : [
328+ {
329+ planId : "plan-minus" ,
330+ implementedAt : timestamp ,
331+ createdAt : timestamp ,
332+ updatedAt : "2026-03-24T00:00:00-01:00" ,
333+ } ,
334+ {
335+ planId : "plan-plus" ,
336+ implementedAt : null ,
337+ createdAt : timestamp ,
338+ updatedAt : "2026-03-24T00:00:00+00:00" ,
339+ } ,
340+ ] ,
341+ } ,
342+ ] ;
343+ for ( const testCase of cases ) {
344+ const threadId = ThreadId . make ( `thread-plan-order-${ testCase . name } ` ) ;
345+ const latestTurnId = TurnId . make ( `turn-plan-order-${ testCase . name } ` ) ;
346+ for ( const plan of testCase . rows ) {
347+ yield * plans . upsert ( {
348+ ...plan ,
349+ planId : `${ testCase . name } -${ plan . planId } ` ,
350+ threadId,
351+ turnId : latestTurnId ,
352+ planMarkdown : "# Plan" ,
353+ implementationThreadId : null ,
354+ } ) ;
355+ }
356+ assert . strictEqual (
357+ yield * plans . hasActionableByThreadId ( { threadId, latestTurnId } ) ,
358+ testCase . expected ,
359+ testCase . name ,
360+ ) ;
361+ assert . strictEqual (
362+ yield * plans . hasActionableByThreadId ( { threadId, latestTurnId : null } ) ,
363+ testCase . expected ,
364+ testCase . name ,
365+ ) ;
366+ }
367+ } ) ,
368+ ) ;
369+
370+ it . effect ( "returns only current-turn status metadata when old plans have large bodies" , ( ) =>
371+ Effect . gen ( function * ( ) {
372+ const plans = yield * ProjectionThreadProposedPlanRepository ;
373+ const sql = yield * SqlClient . SqlClient ;
374+ const threadId = ThreadId . make ( "thread-plan-metadata" ) ;
375+ const latestTurnId = TurnId . make ( "turn-plan-metadata-current" ) ;
376+ const updatedAt = "2026-03-24T00:00:00.000Z" ;
377+ yield * sql `
378+ WITH RECURSIVE history(n) AS (
379+ SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 256
380+ )
381+ INSERT INTO projection_thread_proposed_plans (
382+ plan_id, thread_id, turn_id, plan_markdown, implemented_at,
383+ implementation_thread_id, created_at, updated_at
384+ )
385+ SELECT 'plan-metadata-old-' || n, ${ threadId } , 'turn-plan-metadata-old',
386+ ${ "# Old plan\n" . repeat ( 1024 ) } , ${ updatedAt } , NULL, ${ updatedAt } , ${ updatedAt }
387+ FROM history
388+ ` ;
389+ yield * plans . upsert ( {
390+ planId : "plan-metadata-current" ,
391+ threadId,
392+ turnId : latestTurnId ,
393+ planMarkdown : "# Current plan" ,
394+ implementedAt : null ,
395+ implementationThreadId : null ,
396+ createdAt : updatedAt ,
397+ updatedAt,
398+ } ) ;
399+ const statements : Array < Statement . Statement < unknown > > = [ ] ;
400+ const actionable = yield * plans . hasActionableByThreadId ( { threadId, latestTurnId } ) . pipe (
401+ Effect . provideService ( Statement . CurrentTransformer , ( statement ) => {
402+ statements . push ( statement ) ;
403+ return Effect . succeed ( statement ) ;
404+ } ) ,
405+ ) ;
406+ assert . isTrue ( actionable ) ;
407+ assert . strictEqual ( statements . length , 1 ) ;
408+ const statement = statements [ 0 ] ;
409+ if ( statement === undefined ) return yield * Effect . die ( "Expected a plan status query." ) ;
410+ assert . deepEqual ( yield * statement , [
411+ { planId : "plan-metadata-current" , implementedAt : null , updatedAt } ,
412+ ] ) ;
413+ } ) ,
414+ ) ;
415+
206416 it . effect ( "stores SQL NULL for missing project model options" , ( ) =>
207417 Effect . gen ( function * ( ) {
208418 const projects = yield * ProjectionProjectRepository ;
0 commit comments