@@ -2,16 +2,17 @@ import fs from "fs/promises"
22import path from "path"
33import { Global } from "../global"
44import { Identifier } from "../id/id"
5- import { lazy } from "../util/lazy"
65import { PermissionNext } from "../permission/next"
76import type { Agent } from "../agent/agent"
7+ import { Scheduler } from "../scheduler"
88
99export namespace Truncate {
1010 export const MAX_LINES = 2000
1111 export const MAX_BYTES = 50 * 1024
1212 export const DIR = path . join ( Global . Path . data , "tool-output" )
1313 export const GLOB = path . join ( DIR , "*" )
1414 const RETENTION_MS = 7 * 24 * 60 * 60 * 1000 // 7 days
15+ const HOUR_MS = 60 * 60 * 1000
1516
1617 export type Result = { content : string ; truncated : false } | { content : string ; truncated : true ; outputPath : string }
1718
@@ -21,6 +22,15 @@ export namespace Truncate {
2122 direction ?: "head" | "tail"
2223 }
2324
25+ export function init ( ) {
26+ Scheduler . register ( {
27+ id : "tool.truncation.cleanup" ,
28+ interval : HOUR_MS ,
29+ run : cleanup ,
30+ scope : "global" ,
31+ } )
32+ }
33+
2434 export async function cleanup ( ) {
2535 const cutoff = Identifier . timestamp ( Identifier . create ( "tool" , false , Date . now ( ) - RETENTION_MS ) )
2636 const glob = new Bun . Glob ( "tool_*" )
@@ -31,8 +41,6 @@ export namespace Truncate {
3141 }
3242 }
3343
34- const init = lazy ( cleanup )
35-
3644 function hasTaskTool ( agent ?: Agent . Info ) : boolean {
3745 if ( ! agent ?. permission ) return false
3846 const rule = PermissionNext . evaluate ( "task" , "*" , agent . permission )
@@ -81,7 +89,6 @@ export namespace Truncate {
8189 const unit = hitBytes ? "bytes" : "lines"
8290 const preview = out . join ( "\n" )
8391
84- await init ( )
8592 const id = Identifier . ascending ( "tool" )
8693 const filepath = path . join ( DIR , id )
8794 await Bun . write ( Bun . file ( filepath ) , text )
0 commit comments