@@ -6,6 +6,7 @@ import type {
66 OrchestrationV2ThreadShell ,
77} from "@t3tools/contracts" ;
88import { ProjectId , ProviderInstanceId , ThreadId } from "@t3tools/contracts" ;
9+ import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient" ;
910import type * as Cause from "effect/Cause" ;
1011import * as DateTime from "effect/DateTime" ;
1112import * as Deferred from "effect/Deferred" ;
@@ -15,12 +16,14 @@ import * as PubSub from "effect/PubSub";
1516import * as Queue from "effect/Queue" ;
1617import * as Stream from "effect/Stream" ;
1718import * as TestClock from "effect/testing/TestClock" ;
19+ import * as SqlClient from "effect/sql/SqlClient" ;
1820
1921import {
2022 activeShellRowsForSubscriber ,
2123 bufferShellLiveStream ,
2224 composeShellStreamWithEnrichment ,
2325 dedupeShellEnrichment ,
26+ loadSubscriberShellSnapshot ,
2427 omitIdleSubagentShells ,
2528 omitOlderSettledShells ,
2629 shellStreamItemFromEnrichmentRefresh ,
@@ -235,7 +238,7 @@ describe("omitOlderSettledShells", () => {
235238 omitOlderSettledShells ( { threads, snapshotSequence : 7 , now } ) ;
236239
237240 it ( "keeps the 25 newest settled threads and counts the rest per project" , ( ) => {
238- const sideChat = row ( "side-chat" , 0 , {
241+ const sideChat = row ( "side-chat" , - 500 , {
239242 lineage : {
240243 parentThreadId : ThreadId . make ( "new-0" ) ,
241244 relationshipToParent : "fork" ,
@@ -246,13 +249,25 @@ describe("omitOlderSettledShells", () => {
246249 const result = omit ( [
247250 ...newest ,
248251 sideChat ,
252+ row ( "subagent" , - 500 , {
253+ lineage : {
254+ parentThreadId : ThreadId . make ( "new-0" ) ,
255+ relationshipToParent : "subagent" ,
256+ rootThreadId : ThreadId . make ( "new-0" ) ,
257+ } ,
258+ } ) ,
259+ row ( "null-time" , 0 , { settledAt : null } ) ,
260+ row ( "not-settled" , 0 , { settledOverride : null } ) ,
249261 row ( "old" , 30 ) ,
250262 row ( "b-old" , 40 ) ,
251263 row ( "unsettled" , null ) ,
252264 ] ) ;
253265 expect ( result . threads . map ( ( thread ) => thread . id ) ) . toEqual ( [
254266 ...newest . map ( ( thread ) => thread . id ) ,
255267 "side-chat" ,
268+ "subagent" ,
269+ "null-time" ,
270+ "not-settled" ,
256271 "unsettled" ,
257272 ] ) ;
258273 expect ( result . olderSettled ) . toEqual ( {
@@ -266,56 +281,184 @@ describe("omitOlderSettledShells", () => {
266281 } ) ;
267282
268283 it ( "keeps threads tied with the 25th newest" , ( ) => {
269- const result = omit ( [ ...newest , row ( "tie" , 2.4 ) , row ( "older" , 2.5 ) ] ) ;
270- expect ( result . threads . map ( ( thread ) => thread . id ) . slice ( 25 ) ) . toEqual ( [ "tie" ] ) ;
284+ const ties = Array . from ( { length : 40 } , ( _ , index ) => row ( `tie-${ index } ` , 2.4 ) ) ;
285+ const threads = [ ...ties . slice ( 0 , 20 ) , ...newest , row ( "older" , 2.5 ) , ...ties . slice ( 20 ) ] ;
286+ const result = omit ( threads ) ;
287+ expect ( result . threads . map ( ( thread ) => thread . id ) ) . toEqual (
288+ threads . filter ( ( thread ) => thread . id !== "older" ) . map ( ( thread ) => thread . id ) ,
289+ ) ;
290+ expect ( result . olderSettled ) . toEqual ( {
291+ before : "1970-04-08T14:24:00.000Z" ,
292+ asOfSequence : 7 ,
293+ counts : [ { projectId : "project-a" , count : 1 } ] ,
294+ } ) ;
295+ const allTied = omit ( ties ) ;
296+ expect ( allTied . threads ) . toEqual ( ties ) ;
297+ expect ( allTied . olderSettled . counts ) . toEqual ( [ ] ) ;
271298 } ) ;
272299
273- it ( "omits nothing with fewer than 25 settled threads, however old" , ( ) => {
274- const old = Array . from ( { length : 5 } , ( _ , index ) => row ( `old-${ index } ` , 300 + index ) ) ;
275- const result = omit ( old ) ;
276- expect ( result . threads ) . toEqual ( old ) ;
277- expect ( result . olderSettled . counts ) . toEqual ( [ ] ) ;
300+ it ( "uses the oldest settled timestamp below 25 and the 25th at or above it" , ( ) => {
301+ for ( const [ size , before , omitted ] of [
302+ [ 0 , "1970-04-11T00:00:00.000Z" , 0 ] ,
303+ [ 1 , "1969-06-15T00:00:00.000Z" , 0 ] ,
304+ [ 24 , "1969-05-23T00:00:00.000Z" , 0 ] ,
305+ [ 25 , "1969-05-22T00:00:00.000Z" , 0 ] ,
306+ [ 26 , "1969-05-22T00:00:00.000Z" , 1 ] ,
307+ ] as const ) {
308+ const old = Array . from ( { length : size } , ( _ , index ) => row ( `old-${ index } ` , 300 + index ) ) ;
309+ const result = omit ( old ) ;
310+ expect ( result . threads ) . toEqual ( old . slice ( 0 , 25 ) ) ;
311+ expect ( result . olderSettled ) . toEqual ( {
312+ before,
313+ asOfSequence : 7 ,
314+ counts : omitted === 0 ? [ ] : [ { projectId : "project-a" , count : 1 } ] ,
315+ } ) ;
316+ }
317+ const epoch = omit ( [ row ( "zero" , 100 ) , row ( "negative" , 101 ) ] ) ;
318+ expect ( epoch . threads . map ( ( thread ) => thread . id ) ) . toEqual ( [ "zero" , "negative" ] ) ;
319+ expect ( epoch . olderSettled . before ) . toBe ( "1969-12-31T00:00:00.000Z" ) ;
278320 } ) ;
279321
280- it ( "keeps older rows that are snoozed, live, or above a kept subagent or side chat, and still counts them " , ( ) => {
281- const threads = [
282- ... newest ,
283- row ( "snoozed" , 30 , { snoozedUntil : DateTime . makeUnsafe ( 101 * DAY ) } ) ,
284- row ( "woke" , 30 , { snoozedUntil : DateTime . makeUnsafe ( 99 * DAY ) } ) ,
285- row ( "running" , 30 , { activityRunStatus : "running" } ) ,
286- row ( "parent" , 30 ) ,
287- shellFixture ( {
288- id : ThreadId . make ( "child" ) ,
289- projectId : ProjectId . make ( "project-a" ) ,
290- lineage : {
291- parentThreadId : ThreadId . make ( "parent" ) ,
292- relationshipToParent : "subagent ",
293- rootThreadId : ThreadId . make ( "parent" ) ,
294- } ,
295- settledOverride : null ,
296- settledAt : null ,
297- } ) ,
298- row ( "side-chat-parent" , 30 ) ,
299- row ( "side-chat" , null , {
300- lineage : {
301- parentThreadId : ThreadId . make ( "side-chat-parent" ) ,
302- relationshipToParent : "fork ",
303- rootThreadId : ThreadId . make ( "side-chat-parent" ) ,
304- sideChat : true ,
305- } ,
306- } ) ,
322+ it ( "retains the known newest IDs and project counts across large unsorted inputs " , ( ) => {
323+ const kept = [
324+ "a-0" ,
325+ "b-1" ,
326+ "a-2" ,
327+ "b-3" ,
328+ "a-4" ,
329+ "b-5" ,
330+ "a-6" ,
331+ "b-7" ,
332+ "a-8" ,
333+ "b-9" ,
334+ "a-10 ",
335+ "b-11" ,
336+ "a-12" ,
337+ "b-13" ,
338+ "a-14" ,
339+ "b-15" ,
340+ "a-16" ,
341+ "b-17" ,
342+ "a-18" ,
343+ "b-19" ,
344+ "a-20 ",
345+ "b-21" ,
346+ "a-22" ,
347+ "b-23" ,
348+ "a-24" ,
307349 ] ;
308- const result = omit ( threads ) ;
309- expect ( result . threads . map ( ( thread ) => thread . id ) . slice ( 25 ) ) . toEqual ( [
310- "snoozed" ,
311- "running" ,
312- "parent" ,
313- "child" ,
314- "side-chat-parent" ,
315- "side-chat" ,
316- ] ) ;
317- expect ( result . olderSettled . counts ) . toEqual ( [ { projectId : "project-a" , count : 5 } ] ) ;
350+ const keptIds = new Set ( kept ) ;
351+ for ( const [ size , aCount , bCount ] of [
352+ [ 351 , 163 , 163 ] ,
353+ [ 10000 , 4987 , 4988 ] ,
354+ ] as const ) {
355+ for ( let seed = 1 ; seed <= 8 ; seed ++ ) {
356+ let state = seed ;
357+ const threads = Array . from ( { length : size } , ( _ , index ) =>
358+ Object . freeze ( row ( `${ index % 2 === 0 ? "a" : "b" } -${ index } ` , index ) ) ,
359+ ) ;
360+ for ( let index = threads . length - 1 ; index > 0 ; index -- ) {
361+ state = ( Math . imul ( state , 1664525 ) + 1013904223 ) >>> 0 ;
362+ const other = state % ( index + 1 ) ;
363+ [ threads [ index ] , threads [ other ] ] = [ threads [ other ] ! , threads [ index ] ! ] ;
364+ }
365+ const result = omit ( Object . freeze ( threads ) ) ;
366+ const ids = result . threads . map ( ( thread ) => thread . id ) ;
367+ expect ( [ ...ids ] . sort ( ) ) . toEqual ( [ ...kept ] . sort ( ) ) ;
368+ expect ( ids ) . toEqual (
369+ threads . filter ( ( thread ) => keptIds . has ( thread . id ) ) . map ( ( thread ) => thread . id ) ,
370+ ) ;
371+ expect ( result . olderSettled . before ) . toBe ( "1970-03-18T00:00:00.000Z" ) ;
372+ expect ( result . olderSettled . asOfSequence ) . toBe ( 7 ) ;
373+ expect (
374+ [ ...result . olderSettled . counts ] . sort ( ( a , b ) => a . projectId . localeCompare ( b . projectId ) ) ,
375+ ) . toEqual ( [
376+ { projectId : "project-a" , count : aCount } ,
377+ { projectId : "project-b" , count : bCount } ,
378+ ] ) ;
379+ }
380+ }
318381 } ) ;
382+
383+ it . effect ( "keeps old exceptions and identical trimmed snapshots on reconnect" , ( ) =>
384+ Effect . gen ( function * ( ) {
385+ const threads = [
386+ ...newest ,
387+ row ( "pinned" , 30 , { pinnedAt : now } ) ,
388+ row ( "snoozed" , 30 , { snoozedUntil : DateTime . makeUnsafe ( 101 * DAY ) } ) ,
389+ row ( "woke" , 30 , { snoozedUntil : DateTime . makeUnsafe ( 99 * DAY ) } ) ,
390+ row ( "running" , 30 , { activityRunStatus : "running" } ) ,
391+ row ( "parent" , 30 ) ,
392+ shellFixture ( {
393+ id : ThreadId . make ( "child" ) ,
394+ activityRunStatus : "running" ,
395+ projectId : ProjectId . make ( "project-a" ) ,
396+ lineage : {
397+ parentThreadId : ThreadId . make ( "parent" ) ,
398+ relationshipToParent : "subagent" ,
399+ rootThreadId : ThreadId . make ( "parent" ) ,
400+ } ,
401+ settledOverride : null ,
402+ settledAt : null ,
403+ } ) ,
404+ row ( "side-chat-parent" , 30 ) ,
405+ row ( "side-chat" , null , {
406+ lineage : {
407+ parentThreadId : ThreadId . make ( "side-chat-parent" ) ,
408+ relationshipToParent : "fork" ,
409+ rootThreadId : ThreadId . make ( "side-chat-parent" ) ,
410+ sideChat : true ,
411+ } ,
412+ } ) ,
413+ ] ;
414+ const result = omit ( threads ) ;
415+ expect ( result . threads . map ( ( thread ) => thread . id ) . slice ( 25 ) ) . toEqual ( [
416+ "pinned" ,
417+ "snoozed" ,
418+ "running" ,
419+ "parent" ,
420+ "child" ,
421+ "side-chat-parent" ,
422+ "side-chat" ,
423+ ] ) ;
424+ expect ( result . olderSettled . counts ) . toEqual ( [ { projectId : "project-a" , count : 6 } ] ) ;
425+ yield * TestClock . setTime ( 100 * DAY ) ;
426+ const sql = yield * SqlClient . SqlClient ;
427+ const load = loadSubscriberShellSnapshot ( {
428+ sql,
429+ listShellIndex : Effect . succeed (
430+ threads . map ( ( thread ) => ( {
431+ ...thread ,
432+ pinnedAt : thread . pinnedAt ?? null ,
433+ snoozedUntil : thread . snoozedUntil ?? null ,
434+ mayBeLive : thread . activityRunStatus === "running" || thread . id === "child" ,
435+ } ) ) ,
436+ ) ,
437+ readThreads : ( ids ) =>
438+ Effect . succeed (
439+ Effect . succeed ( {
440+ schemaVersion : 1 ,
441+ snapshotSequence : 7 ,
442+ threads : threads . filter ( ( thread ) => ids ?. includes ( thread . id ) ?? true ) ,
443+ archivedThreads : [ ] ,
444+ } ) ,
445+ ) ,
446+ listProjects : Effect . succeed ( [ ] ) ,
447+ latestSequence : Effect . succeed ( 7 ) ,
448+ options : { omitIdleSubagents : true , omitOlderSettled : true } ,
449+ } ) ;
450+ const connected = yield * load ;
451+ const reconnected = yield * load ;
452+ expect ( connected . snapshot ) . toEqual ( {
453+ ...emptyShellSnapshot ,
454+ snapshotSequence : 7 ,
455+ threads : result . threads ,
456+ olderSettled : result . olderSettled ,
457+ } ) ;
458+ expect ( connected . subagentThreadIds ) . toEqual ( new Set ( [ "child" ] ) ) ;
459+ expect ( reconnected ) . toEqual ( connected ) ;
460+ } ) . pipe ( Effect . provide ( NodeSqliteClient . layer ( { filename : ":memory:" } ) ) ) ,
461+ ) ;
319462} ) ;
320463
321464describe ( "shellStreamItemsFromInitialSnapshot" , ( ) => {
0 commit comments