@@ -2,7 +2,25 @@ import { describe, test, expect, beforeEach } from 'bun:test'
22
33import contextPruner from '../context-pruner'
44
5- import type { Message , ToolMessage } from '../types/util-types'
5+ import type { AgentState } from '../types/agent-definition'
6+ import type { JSONValue , Message , ToolMessage } from '../types/util-types'
7+
8+ // Helper to create a minimal mock AgentState for testing
9+ function createMockAgentState (
10+ messageHistory : Message [ ] ,
11+ contextTokenCount : number ,
12+ ) : AgentState {
13+ return {
14+ agentId : 'test-agent' ,
15+ runId : 'test-run' ,
16+ parentId : undefined ,
17+ messageHistory,
18+ output : undefined ,
19+ systemPrompt : '' ,
20+ toolDefinitions : { } ,
21+ contextTokenCount,
22+ }
23+ }
624
725/**
826 * Regression test: Verify handleSteps can be serialized and run in isolation.
@@ -29,8 +47,8 @@ describe('context-pruner handleSteps serialization', () => {
2947 const isolatedFunction = new Function ( `return (${ handleStepsString } )` ) ( )
3048
3149 // Create minimal mock data to run the function
32- const mockAgentState = {
33- messageHistory : [
50+ const mockAgentState = createMockAgentState (
51+ [
3452 {
3553 role : 'user' ,
3654 content : [ { type : 'text' , text : 'Hello' } ] ,
@@ -40,8 +58,8 @@ describe('context-pruner handleSteps serialization', () => {
4058 content : [ { type : 'text' , text : 'Hi there!' } ] ,
4159 } ,
4260 ] ,
43- contextTokenCount : 100 , // Under the limit, so it won't prune
44- }
61+ 100 , // Under the limit, so it won't prune
62+ )
4563
4664 const mockLogger = {
4765 debug : ( ) => { } ,
@@ -78,8 +96,8 @@ describe('context-pruner handleSteps serialization', () => {
7896 const isolatedFunction = new Function ( `return (${ handleStepsString } )` ) ( )
7997
8098 // Create mock data that will trigger pruning (context over limit)
81- const mockAgentState = {
82- messageHistory : [
99+ const mockAgentState = createMockAgentState (
100+ [
83101 {
84102 role : 'user' ,
85103 content : [ { type : 'text' , text : 'Please help me with a task' } ] ,
@@ -107,8 +125,8 @@ describe('context-pruner handleSteps serialization', () => {
107125 content : [ { type : 'text' , text : 'Thanks!' } ] ,
108126 } ,
109127 ] ,
110- contextTokenCount : 250000 , // Over the limit, will trigger pruning
111- }
128+ 250000 , // Over the limit, will trigger pruning
129+ )
112130
113131 const mockLogger = {
114132 debug : ( ) => { } ,
@@ -177,27 +195,24 @@ const createToolCallMessage = (
177195const createToolResultMessage = (
178196 toolCallId : string ,
179197 toolName : string ,
180- value : unknown ,
198+ value : JSONValue ,
181199) : ToolMessage => ( {
182200 role : 'tool' ,
183201 toolCallId,
184202 toolName,
185203 content : [
186204 {
187205 type : 'json' ,
188- value : value as Record < string , unknown > ,
206+ value,
189207 } ,
190208 ] ,
191209} )
192210
193211describe ( 'context-pruner handleSteps' , ( ) => {
194- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
212+ let mockAgentState : AgentState
195213
196214 beforeEach ( ( ) => {
197- mockAgentState = {
198- messageHistory : [ ] as Message [ ] ,
199- contextTokenCount : 0 ,
200- }
215+ mockAgentState = createMockAgentState ( [ ] , 0 )
201216 } )
202217
203218 const runHandleSteps = (
@@ -284,7 +299,7 @@ describe('context-pruner handleSteps', () => {
284299 createToolCallMessage ( 'call-1' , 'read_files' , {
285300 paths : [ 'file1.ts' , 'file2.ts' ] ,
286301 } ) ,
287- createToolResultMessage ( 'call-1' , 'read_files' , { content : 'file data' } ) ,
302+ createToolResultMessage ( 'call-1' , 'read_files' , { content : 'file data' } as JSONValue ) ,
288303 createMessage ( 'user' , 'Now edit this file' ) ,
289304 createToolCallMessage ( 'call-2' , 'str_replace' , {
290305 path : 'file1.ts' ,
@@ -675,13 +690,10 @@ describe('context-pruner handleSteps', () => {
675690} )
676691
677692describe ( 'context-pruner long message truncation' , ( ) => {
678- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
693+ let mockAgentState : AgentState
679694
680695 beforeEach ( ( ) => {
681- mockAgentState = {
682- messageHistory : [ ] as Message [ ] ,
683- contextTokenCount : 0 ,
684- }
696+ mockAgentState = createMockAgentState ( [ ] , 0 )
685697 } )
686698
687699 const runHandleSteps = (
@@ -772,13 +784,10 @@ describe('context-pruner long message truncation', () => {
772784} )
773785
774786describe ( 'context-pruner code_search with flags' , ( ) => {
775- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
787+ let mockAgentState : AgentState
776788
777789 beforeEach ( ( ) => {
778- mockAgentState = {
779- messageHistory : [ ] as Message [ ] ,
780- contextTokenCount : 0 ,
781- }
790+ mockAgentState = createMockAgentState ( [ ] , 0 )
782791 } )
783792
784793 const runHandleSteps = ( messages : Message [ ] ) => {
@@ -824,13 +833,10 @@ describe('context-pruner code_search with flags', () => {
824833} )
825834
826835describe ( 'context-pruner ask_user with questions and answers' , ( ) => {
827- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
836+ let mockAgentState : AgentState
828837
829838 beforeEach ( ( ) => {
830- mockAgentState = {
831- messageHistory : [ ] as Message [ ] ,
832- contextTokenCount : 0 ,
833- }
839+ mockAgentState = createMockAgentState ( [ ] , 0 )
834840 } )
835841
836842 const runHandleSteps = ( messages : Message [ ] ) => {
@@ -937,13 +943,10 @@ describe('context-pruner ask_user with questions and answers', () => {
937943} )
938944
939945describe ( 'context-pruner terminal command exit codes' , ( ) => {
940- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
946+ let mockAgentState : AgentState
941947
942948 beforeEach ( ( ) => {
943- mockAgentState = {
944- messageHistory : [ ] as Message [ ] ,
945- contextTokenCount : 0 ,
946- }
949+ mockAgentState = createMockAgentState ( [ ] , 0 )
947950 } )
948951
949952 const runHandleSteps = ( messages : Message [ ] ) => {
@@ -1009,13 +1012,10 @@ describe('context-pruner terminal command exit codes', () => {
10091012} )
10101013
10111014describe ( 'context-pruner spawn_agents with prompt and params' , ( ) => {
1012- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
1015+ let mockAgentState : AgentState
10131016
10141017 beforeEach ( ( ) => {
1015- mockAgentState = {
1016- messageHistory : [ ] as Message [ ] ,
1017- contextTokenCount : 0 ,
1018- }
1018+ mockAgentState = createMockAgentState ( [ ] , 0 )
10191019 } )
10201020
10211021 const runHandleSteps = ( messages : Message [ ] ) => {
@@ -1128,13 +1128,10 @@ describe('context-pruner spawn_agents with prompt and params', () => {
11281128} )
11291129
11301130describe ( 'context-pruner repeated compaction' , ( ) => {
1131- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
1131+ let mockAgentState : AgentState
11321132
11331133 beforeEach ( ( ) => {
1134- mockAgentState = {
1135- messageHistory : [ ] as Message [ ] ,
1136- contextTokenCount : 0 ,
1137- }
1134+ mockAgentState = createMockAgentState ( [ ] , 0 )
11381135 } )
11391136
11401137 const runHandleSteps = (
@@ -1306,13 +1303,10 @@ First assistant response
13061303} )
13071304
13081305describe ( 'context-pruner image token counting' , ( ) => {
1309- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
1306+ let mockAgentState : AgentState
13101307
13111308 beforeEach ( ( ) => {
1312- mockAgentState = {
1313- messageHistory : [ ] as Message [ ] ,
1314- contextTokenCount : 0 ,
1315- }
1309+ mockAgentState = createMockAgentState ( [ ] , 0 )
13161310 } )
13171311
13181312 const runHandleSteps = (
@@ -1371,13 +1365,10 @@ describe('context-pruner image token counting', () => {
13711365} )
13721366
13731367describe ( 'context-pruner threshold behavior' , ( ) => {
1374- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
1368+ let mockAgentState : AgentState
13751369
13761370 beforeEach ( ( ) => {
1377- mockAgentState = {
1378- messageHistory : [ ] as Message [ ] ,
1379- contextTokenCount : 0 ,
1380- }
1371+ mockAgentState = createMockAgentState ( [ ] , 0 )
13811372 } )
13821373
13831374 const runHandleSteps = (
@@ -1444,13 +1435,10 @@ describe('context-pruner threshold behavior', () => {
14441435} )
14451436
14461437describe ( 'context-pruner str_replace and write_file tool results' , ( ) => {
1447- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
1438+ let mockAgentState : AgentState
14481439
14491440 beforeEach ( ( ) => {
1450- mockAgentState = {
1451- messageHistory : [ ] as Message [ ] ,
1452- contextTokenCount : 0 ,
1453- }
1441+ mockAgentState = createMockAgentState ( [ ] , 0 )
14541442 } )
14551443
14561444 const runHandleSteps = ( messages : Message [ ] ) => {
@@ -1561,13 +1549,10 @@ describe('context-pruner str_replace and write_file tool results', () => {
15611549} )
15621550
15631551describe ( 'context-pruner glob and list_directory tools' , ( ) => {
1564- let mockAgentState : { messageHistory : Message [ ] ; contextTokenCount : number }
1552+ let mockAgentState : AgentState
15651553
15661554 beforeEach ( ( ) => {
1567- mockAgentState = {
1568- messageHistory : [ ] as Message [ ] ,
1569- contextTokenCount : 0 ,
1570- }
1555+ mockAgentState = createMockAgentState ( [ ] , 0 )
15711556 } )
15721557
15731558 const runHandleSteps = ( messages : Message [ ] ) => {
0 commit comments