@@ -1609,6 +1609,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
16091609 const now = yield * DateTime . now ;
16101610 const nowIso = DateTime . formatIso ( now ) ;
16111611 const threadId = ThreadId . make ( "thread:notification-turn-pages" ) ;
1612+ const providerInstanceId = ProviderInstanceId . make ( "claudeAgent" ) ;
1613+ const modelSelection = { instanceId : providerInstanceId , model : "claude-sonnet-4-6" } ;
16121614 yield * projectionStore . apply ( {
16131615 id : EventId . make ( "event:notification-turn-pages:thread" ) ,
16141616 type : "thread.created" ,
@@ -1639,6 +1641,53 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
16391641 } ,
16401642 } ) ;
16411643
1644+ const providerThreadId = ProviderThreadId . make ( "provider-thread:notification-turn-pages" ) ;
1645+ yield * projectionStore . apply ( {
1646+ id : EventId . make ( "event:notification-turn-pages:provider-thread" ) ,
1647+ type : "provider-thread.updated" ,
1648+ threadId,
1649+ occurredAt : now ,
1650+ payload : {
1651+ id : providerThreadId ,
1652+ driver : ProviderDriverKind . make ( "claudeAgent" ) ,
1653+ providerInstanceId,
1654+ providerSessionId : null ,
1655+ appThreadId : threadId ,
1656+ ownerNodeId : null ,
1657+ nativeThreadRef : null ,
1658+ nativeConversationHeadRef : null ,
1659+ status : "idle" ,
1660+ firstRunOrdinal : 1 ,
1661+ lastRunOrdinal : 412 ,
1662+ handoffIds : [ ] ,
1663+ forkedFrom : null ,
1664+ createdAt : now ,
1665+ updatedAt : now ,
1666+ } ,
1667+ } ) ;
1668+ const planId = PlanId . make ( "plan:notification-turn-pages" ) ;
1669+ const olderPlanId = PlanId . make ( "plan:notification-turn-pages:older" ) ;
1670+ for ( const [ id , run ] of [
1671+ [ olderPlanId , 1 ] ,
1672+ [ planId , 2 ] ,
1673+ ] as const ) {
1674+ yield * projectionStore . apply ( {
1675+ id : EventId . make ( `event:notification-turn-pages:plan:${ run } ` ) ,
1676+ type : "plan.updated" ,
1677+ threadId,
1678+ occurredAt : now ,
1679+ payload : {
1680+ id,
1681+ threadId,
1682+ runId : RunId . make ( `run:notification-turn-pages:${ run } ` ) ,
1683+ nodeId : NodeId . make ( `node:notification-turn-pages:${ run } ` ) ,
1684+ kind : "todo_list" ,
1685+ status : "active" ,
1686+ steps : [ { id : "unfinished" , text : "Finish the task" , status : "pending" } ] ,
1687+ } ,
1688+ } ) ;
1689+ }
1690+
16421691 const startedRuns = 412 ;
16431692 const allIds : string [ ] = [ ] ;
16441693 const startIds : string [ ] = [ ] ;
@@ -1693,14 +1742,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
16931742 run_id, thread_id, ordinal, provider, provider_thread_id, status,
16941743 requested_at, completed_at, payload_json
16951744 ) VALUES (
1696- ${ runId } , ${ threadId } , ${ run } , 'codex ', NULL , ${ status } , ${ nowIso } , ${ nowIso } ,
1745+ ${ runId } , ${ threadId } , ${ run } , 'claudeAgent ', ${ providerThreadId } , ${ status } , ${ nowIso } , ${ nowIso } ,
16971746 ${ encodeUnknownJsonString ( {
16981747 id : runId ,
16991748 threadId,
17001749 ordinal : run ,
17011750 providerInstanceId,
17021751 modelSelection,
1703- providerThreadId : null ,
1752+ providerThreadId,
17041753 userMessageId : `message:notification-turn-pages:${ run } ` ,
17051754 rootNodeId : `node:notification-turn-pages:${ run } ` ,
17061755 activeAttemptId : null ,
@@ -1763,6 +1812,22 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
17631812 snapshotSequence : 0 ,
17641813 } ) ;
17651814 assert . strictEqual ( bounded . projection . visibleTurnItems [ 0 ] ?. sourceItemId , startIds [ 262 ] ) ;
1815+ assert . deepEqual (
1816+ bounded . projection . plans . map ( ( plan ) => plan . id ) ,
1817+ [ planId , olderPlanId ] ,
1818+ ) ;
1819+ assert . deepEqual (
1820+ {
1821+ oldRunIds : bounded . projection . runs
1822+ . filter ( ( run ) => run . ordinal < 261 )
1823+ . map ( ( run ) => run . id ) ,
1824+ providerThreadIds : bounded . projection . providerThreads . map ( ( thread ) => thread . id ) ,
1825+ } ,
1826+ {
1827+ oldRunIds : [ RunId . make ( "run:notification-turn-pages:2" ) ] ,
1828+ providerThreadIds : [ providerThreadId ] ,
1829+ } ,
1830+ ) ;
17661831 const loaded = bounded . projection . visibleTurnItems . map ( ( row ) => String ( row . sourceItemId ) ) ;
17671832 let cursor = bounded . historyCursor ;
17681833 for ( let pages = 0 ; cursor !== null ; pages += 1 ) {
@@ -1784,6 +1849,119 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
17841849 cursor = page . nextCursor ;
17851850 }
17861851 assert . deepEqual ( loaded , allIds ) ;
1852+
1853+ const completedPlan = {
1854+ id : planId ,
1855+ threadId,
1856+ runId : RunId . make ( "run:notification-turn-pages:2" ) ,
1857+ nodeId : NodeId . make ( "node:notification-turn-pages:2" ) ,
1858+ kind : "todo_list" as const ,
1859+ status : "completed" as const ,
1860+ explanation : "All tasks complete" ,
1861+ steps : [ { id : "unfinished" , text : "Finish the task" , status : "completed" as const } ] ,
1862+ } ;
1863+ yield * projectionStore . apply ( {
1864+ id : EventId . make ( "event:notification-turn-pages:plan:completed" ) ,
1865+ type : "plan.updated" ,
1866+ threadId,
1867+ occurredAt : now ,
1868+ payload : completedPlan ,
1869+ } ) ;
1870+ const afterCompletion = yield * projectionStore . getThreadSnapshotWindow ( threadId , {
1871+ rowLimit : 77 ,
1872+ userTurnLimit : 10 ,
1873+ } ) ;
1874+ const completedBounded = buildBoundedThreadProjection ( {
1875+ projection : afterCompletion . projection ,
1876+ snapshotSequence : 0 ,
1877+ } ) ;
1878+ assert . deepEqual (
1879+ {
1880+ plans : completedBounded . projection . plans ,
1881+ oldRunIds : completedBounded . projection . runs
1882+ . filter ( ( run ) => run . ordinal < 261 )
1883+ . map ( ( run ) => run . id ) ,
1884+ providerThreadIds : completedBounded . projection . providerThreads . map ( ( thread ) => thread . id ) ,
1885+ } ,
1886+ {
1887+ plans : [ completedPlan , bounded . projection . plans . find ( ( plan ) => plan . id === olderPlanId ) ] ,
1888+ oldRunIds : [ completedPlan . runId ] ,
1889+ providerThreadIds : [ providerThreadId ] ,
1890+ } ,
1891+ ) ;
1892+
1893+ // Older versions left completed predecessors unsuperseded; IDs do not order replacements.
1894+ const newerPlan = {
1895+ ...completedPlan ,
1896+ id : PlanId . make ( "plan:legacy-newer" ) ,
1897+ status : "active" as const ,
1898+ explanation : "More work remains" ,
1899+ steps : [ { id : "new" , text : "Finish the new task" , status : "pending" as const } ] ,
1900+ } ;
1901+ yield * projectionStore . apply ( {
1902+ id : EventId . make ( "event:notification-turn-pages:plan:legacy-newer" ) ,
1903+ type : "plan.updated" ,
1904+ threadId,
1905+ occurredAt : now ,
1906+ payload : newerPlan ,
1907+ } ) ;
1908+ for ( const [ plan , itemOrdinal ] of [
1909+ [ completedPlan , 7 ] ,
1910+ [ newerPlan , 8 ] ,
1911+ ] as const ) {
1912+ yield * projectionStore . apply ( {
1913+ id : EventId . make ( `event:notification-turn-pages:legacy-item:${ itemOrdinal } ` ) ,
1914+ type : "turn-item.updated" ,
1915+ threadId,
1916+ runId : plan . runId ,
1917+ nodeId : plan . nodeId ,
1918+ occurredAt : now ,
1919+ payload : {
1920+ id : TurnItemId . make ( `turn-item:notification-turn-pages:${ itemOrdinal } ` ) ,
1921+ threadId,
1922+ runId : plan . runId ,
1923+ nodeId : plan . nodeId ,
1924+ providerThreadId,
1925+ providerTurnId : null ,
1926+ nativeItemRef : null ,
1927+ parentItemId : null ,
1928+ ordinal : itemOrdinal ,
1929+ status : "completed" ,
1930+ title : null ,
1931+ startedAt : now ,
1932+ completedAt : now ,
1933+ updatedAt : now ,
1934+ type : "todo_list" ,
1935+ planId : plan . id ,
1936+ steps : plan . steps ,
1937+ explanation : plan . explanation ,
1938+ } ,
1939+ } ) ;
1940+ }
1941+ const legacySnapshot = yield * projectionStore . getThreadSnapshotWindow ( threadId , {
1942+ rowLimit : 77 ,
1943+ userTurnLimit : 10 ,
1944+ } ) ;
1945+ assert . isTrue ( legacySnapshot . projection . turnItems . every ( ( item ) => item . ordinal > 8 ) ) ;
1946+ assert . deepEqual (
1947+ legacySnapshot . projection . plans . findLast ( ( plan ) => plan . runId === newerPlan . runId ) ,
1948+ newerPlan ,
1949+ ) ;
1950+ const legacyBounded = buildBoundedThreadProjection ( {
1951+ projection : legacySnapshot . projection ,
1952+ snapshotSequence : 0 ,
1953+ } ) ;
1954+ assert . deepEqual (
1955+ {
1956+ ownerPlans : legacyBounded . projection . plans . filter (
1957+ ( plan ) => plan . runId === newerPlan . runId ,
1958+ ) ,
1959+ oldRunIds : legacyBounded . projection . runs
1960+ . filter ( ( run ) => run . ordinal < 261 )
1961+ . map ( ( run ) => run . id ) ,
1962+ } ,
1963+ { ownerPlans : [ newerPlan ] , oldRunIds : [ newerPlan . runId ] } ,
1964+ ) ;
17871965 } ) ,
17881966 ) ;
17891967
0 commit comments