44// in-process client and reads only the result's text and structuredContent.
55// Includes the #4813 regression tests: a branchId that names an
66// Object.prototype key is an ordinary branch, and a failed call leaves the
7- // state unchanged.
7+ // state unchanged; and the #2912 tests: the history is capped at
8+ // MAX_THOUGHT_HISTORY, evicting the oldest thought from its branch too.
89
910import { describe , it , expect , beforeEach , afterEach , vi } from "vitest" ;
1011import {
@@ -14,6 +15,30 @@ import {
1415 thought ,
1516 type Connected ,
1617} from "./helpers.js" ;
18+ import { DEFAULT_MAX_THOUGHT_HISTORY } from "../lib.js" ;
19+
20+ /**
21+ * Connect to a server built under MAX_THOUGHT_HISTORY=`value` (`undefined` =
22+ * unset). The cap is read once, when the server is built, so the variable is
23+ * restored as soon as the connection is up.
24+ */
25+ async function connectCapped ( value : string | undefined ) : Promise < Connected > {
26+ const previous = process . env . MAX_THOUGHT_HISTORY ;
27+ if ( value === undefined ) {
28+ delete process . env . MAX_THOUGHT_HISTORY ;
29+ } else {
30+ process . env . MAX_THOUGHT_HISTORY = value ;
31+ }
32+ try {
33+ return await connect ( ) ;
34+ } finally {
35+ if ( previous === undefined ) {
36+ delete process . env . MAX_THOUGHT_HISTORY ;
37+ } else {
38+ process . env . MAX_THOUGHT_HISTORY = previous ;
39+ }
40+ }
41+ }
1742
1843describe ( "sequentialthinking thought processing" , ( ) => {
1944 let conn : Connected ;
@@ -392,4 +417,136 @@ describe("sequentialthinking thought processing", () => {
392417 ) . toEqual ( [ ] ) ;
393418 } ) ;
394419 } ) ;
420+
421+ // #2912: a server lives as long as its connection, so the history it keeps
422+ // is capped, oldest thought first, and the branches shrink with it.
423+ describe ( "#2912: bounded thought history" , ( ) => {
424+ let capped : Connected | undefined ;
425+
426+ afterEach ( async ( ) => {
427+ await capped ?. close ( ) ;
428+ capped = undefined ;
429+ vi . restoreAllMocks ( ) ;
430+ } ) ;
431+
432+ it ( `retains at most ${ DEFAULT_MAX_THOUGHT_HISTORY } thoughts by default` , async ( ) => {
433+ capped = await connectCapped ( undefined ) ;
434+ let length = 0 ;
435+ for ( let n = 1 ; n <= DEFAULT_MAX_THOUGHT_HISTORY + 5 ; n ++ ) {
436+ length = parseOk (
437+ await capped . think ( thought ( { thoughtNumber : n } ) ) ,
438+ ) . thoughtHistoryLength ;
439+ expect ( length ) . toBe ( Math . min ( n , DEFAULT_MAX_THOUGHT_HISTORY ) ) ;
440+ }
441+ expect ( length ) . toBe ( DEFAULT_MAX_THOUGHT_HISTORY ) ;
442+ } ) ;
443+
444+ it ( "stops thoughtHistoryLength at MAX_THOUGHT_HISTORY" , async ( ) => {
445+ capped = await connectCapped ( "3" ) ;
446+ const lengths : number [ ] = [ ] ;
447+ for ( let n = 1 ; n <= 6 ; n ++ ) {
448+ lengths . push (
449+ parseOk ( await capped . think ( thought ( { thoughtNumber : n } ) ) )
450+ . thoughtHistoryLength ,
451+ ) ;
452+ }
453+ expect ( lengths ) . toEqual ( [ 1 , 2 , 3 , 3 , 3 , 3 ] ) ;
454+ } ) ;
455+
456+ it ( "drops a branch once all of its thoughts are evicted" , async ( ) => {
457+ capped = await connectCapped ( "2" ) ;
458+ await capped . think ( thought ( { branchFromThought : 1 , branchId : "alt" } ) ) ;
459+ expect (
460+ parseOk ( await capped . think ( thought ( { thoughtNumber : 2 } ) ) ) . branches ,
461+ ) . toEqual ( [ "alt" ] ) ;
462+ expect (
463+ parseOk ( await capped . think ( thought ( { thoughtNumber : 3 } ) ) ) ,
464+ ) . toMatchObject ( { branches : [ ] , thoughtHistoryLength : 2 } ) ;
465+ } ) ;
466+
467+ it ( "keeps a branch while any of its thoughts is retained" , async ( ) => {
468+ capped = await connectCapped ( "2" ) ;
469+ await capped . think ( thought ( { branchFromThought : 1 , branchId : "alt" } ) ) ;
470+ await capped . think (
471+ thought ( { thoughtNumber : 2 , branchFromThought : 1 , branchId : "alt" } ) ,
472+ ) ;
473+ // Evicts the first "alt" thought; the second is still retained.
474+ expect (
475+ parseOk ( await capped . think ( thought ( { thoughtNumber : 3 } ) ) ) ,
476+ ) . toMatchObject ( { branches : [ "alt" ] , thoughtHistoryLength : 2 } ) ;
477+ // Evicts the second; nothing on "alt" is left.
478+ expect (
479+ parseOk ( await capped . think ( thought ( { thoughtNumber : 4 } ) ) ) . branches ,
480+ ) . toEqual ( [ ] ) ;
481+ } ) ;
482+
483+ it ( "lists a branch recreated after eviction as newly seen" , async ( ) => {
484+ capped = await connectCapped ( "3" ) ;
485+ await capped . think ( thought ( { branchFromThought : 1 , branchId : "a" } ) ) ;
486+ await capped . think (
487+ thought ( { thoughtNumber : 2 , branchFromThought : 1 , branchId : "b" } ) ,
488+ ) ;
489+ await capped . think (
490+ thought ( { thoughtNumber : 3 , branchFromThought : 1 , branchId : "b" } ) ,
491+ ) ;
492+ // Evicts the only "a" thought.
493+ expect (
494+ parseOk ( await capped . think ( thought ( { thoughtNumber : 4 } ) ) ) . branches ,
495+ ) . toEqual ( [ "b" ] ) ;
496+ // "a" is recorded again, after "b"; the first "b" thought is evicted.
497+ expect (
498+ parseOk (
499+ await capped . think (
500+ thought ( { thoughtNumber : 5 , branchFromThought : 1 , branchId : "a" } ) ,
501+ ) ,
502+ ) . branches ,
503+ ) . toEqual ( [ "b" , "a" ] ) ;
504+ } ) ;
505+
506+ it ( "evicts a branch whose id names an Object.prototype key" , async ( ) => {
507+ capped = await connectCapped ( "1" ) ;
508+ await capped . think (
509+ thought ( { branchFromThought : 1 , branchId : "__proto__" } ) ,
510+ ) ;
511+ expect (
512+ parseOk ( await capped . think ( thought ( { thoughtNumber : 2 } ) ) ) ,
513+ ) . toMatchObject ( { branches : [ ] , thoughtHistoryLength : 1 } ) ;
514+ } ) ;
515+
516+ it ( "accepts a value padded with whitespace" , async ( ) => {
517+ capped = await connectCapped ( " 1 " ) ;
518+ await capped . think ( thought ( ) ) ;
519+ expect (
520+ parseOk ( await capped . think ( thought ( { thoughtNumber : 2 } ) ) )
521+ . thoughtHistoryLength ,
522+ ) . toBe ( 1 ) ;
523+ } ) ;
524+
525+ it ( "uses the default for an empty value, without a warning" , async ( ) => {
526+ const errors = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
527+ capped = await connectCapped ( "" ) ;
528+ await capped . think ( thought ( ) ) ;
529+ expect (
530+ parseOk ( await capped . think ( thought ( { thoughtNumber : 2 } ) ) )
531+ . thoughtHistoryLength ,
532+ ) . toBe ( 2 ) ;
533+ expect ( errors ) . not . toHaveBeenCalled ( ) ;
534+ } ) ;
535+
536+ it . each ( [ "0" , "-1" , "1.5" , "abc" , "1e3" , "99999999999999999999" ] ) (
537+ "reports %j on stderr and keeps the default cap" ,
538+ async ( value ) => {
539+ const errors = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
540+ capped = await connectCapped ( value ) ;
541+ expect ( errors ) . toHaveBeenCalledWith (
542+ `MAX_THOUGHT_HISTORY must be a positive integer, got "${ value } "; using ${ DEFAULT_MAX_THOUGHT_HISTORY } ` ,
543+ ) ;
544+ await capped . think ( thought ( ) ) ;
545+ expect (
546+ parseOk ( await capped . think ( thought ( { thoughtNumber : 2 } ) ) )
547+ . thoughtHistoryLength ,
548+ ) . toBe ( 2 ) ;
549+ } ,
550+ ) ;
551+ } ) ;
395552} ) ;
0 commit comments