2025-12-10 03:32:04 +08:00
import { BusEvent } from "@/bus/bus-event"
import { Bus } from "@/bus"
2025-09-13 17:46:14 +08:00
import { Session } from "."
import { Identifier } from "../id/id"
import { Instance } from "../project/instance"
import { Provider } from "../provider/provider"
import { MessageV2 } from "./message-v2"
2025-10-27 03:50:41 +08:00
import z from "zod"
2025-09-13 17:46:14 +08:00
import { SessionPrompt } from "./prompt"
2025-09-16 16:52:34 +08:00
import { Token } from "../util/token"
import { Log } from "../util/log"
2025-11-17 23:57:18 +08:00
import { SessionProcessor } from "./processor"
import { fn } from "@/util/fn"
2025-12-15 10:11:30 +08:00
import { Agent } from "@/agent/agent"
2025-12-18 03:57:09 +08:00
import { Plugin } from "@/plugin"
2025-12-27 08:31:42 +08:00
import { Config } from "@/config/config"
2025-09-13 17:46:14 +08:00
export namespace SessionCompaction {
2025-09-16 16:52:34 +08:00
const log = Log . create ( { service : "session.compaction" } )
2025-09-13 17:46:14 +08:00
export const Event = {
2025-12-10 03:32:04 +08:00
Compacted : BusEvent.define (
2025-09-13 17:46:14 +08:00
"session.compacted" ,
z . object ( {
sessionID : z.string ( ) ,
} ) ,
) ,
}
2025-12-27 08:31:42 +08:00
export async function isOverflow ( input : { tokens : MessageV2.Assistant [ "tokens" ] ; model : Provider.Model } ) {
const config = await Config . get ( )
if ( ( config . compaction ? . auto ? ? true ) === false ) return false
2025-09-13 17:59:18 +08:00
const context = input . model . limit . context
if ( context === 0 ) return false
2025-09-13 17:46:14 +08:00
const count = input . tokens . input + input . tokens . cache . read + input . tokens . output
2025-11-08 09:59:02 +08:00
const output = Math . min ( input . model . limit . output , SessionPrompt . OUTPUT_TOKEN_MAX ) || SessionPrompt . OUTPUT_TOKEN_MAX
2025-09-13 17:59:18 +08:00
const usable = context - output
2025-09-13 17:53:03 +08:00
return count > usable
2025-09-13 17:46:14 +08:00
}
2025-09-17 15:07:24 +08:00
export const PRUNE_MINIMUM = 20 _000
export const PRUNE_PROTECT = 40 _000
2025-12-23 07:24:06 +08:00
const PRUNE_PROTECTED_TOOLS = [ "skill" ]
2025-09-16 16:52:34 +08:00
// goes backwards through parts until there are 40_000 tokens worth of tool
// calls. then erases output of previous tool calls. idea is to throw away old
// tool calls that are no longer relevant.
export async function prune ( input : { sessionID : string } ) {
2025-12-27 08:31:42 +08:00
const config = await Config . get ( )
if ( ( config . compaction ? . prune ? ? true ) === false ) return
2025-09-16 16:52:34 +08:00
log . info ( "pruning" )
2025-11-07 02:20:13 +08:00
const msgs = await Session . messages ( { sessionID : input.sessionID } )
2025-09-16 16:52:34 +08:00
let total = 0
let pruned = 0
const toPrune = [ ]
2025-09-17 15:07:24 +08:00
let turns = 0
2025-09-16 16:52:34 +08:00
2025-09-17 15:07:24 +08:00
loop : for ( let msgIndex = msgs . length - 1 ; msgIndex >= 0 ; msgIndex -- ) {
2025-09-16 16:52:34 +08:00
const msg = msgs [ msgIndex ]
2025-09-17 15:07:24 +08:00
if ( msg . info . role === "user" ) turns ++
if ( turns < 2 ) continue
if ( msg . info . role === "assistant" && msg . info . summary ) break loop
2025-09-16 16:52:34 +08:00
for ( let partIndex = msg . parts . length - 1 ; partIndex >= 0 ; partIndex -- ) {
const part = msg . parts [ partIndex ]
if ( part . type === "tool" )
if ( part . state . status === "completed" ) {
2025-12-23 07:24:06 +08:00
if ( PRUNE_PROTECTED_TOOLS . includes ( part . tool ) ) continue
2025-09-16 16:52:34 +08:00
if ( part . state . time . compacted ) break loop
const estimate = Token . estimate ( part . state . output )
total += estimate
2025-09-17 15:07:24 +08:00
if ( total > PRUNE_PROTECT ) {
2025-09-16 16:52:34 +08:00
pruned += estimate
toPrune . push ( part )
}
}
}
}
log . info ( "found" , { pruned , total } )
2025-09-17 15:07:24 +08:00
if ( pruned > PRUNE_MINIMUM ) {
2025-09-16 16:52:34 +08:00
for ( const part of toPrune ) {
if ( part . state . status === "completed" ) {
part . state . time . compacted = Date . now ( )
await Session . updatePart ( part )
}
}
log . info ( "pruned" , { count : toPrune.length } )
}
}
2025-11-17 23:57:18 +08:00
export async function process ( input : {
parentID : string
messages : MessageV2.WithParts [ ]
sessionID : string
abort : AbortSignal
2025-11-26 02:10:56 +08:00
auto : boolean
2025-11-17 23:57:18 +08:00
} ) {
2025-12-15 10:11:30 +08:00
const userMessage = input . messages . findLast ( ( m ) = > m . info . id === input . parentID ) ! . info as MessageV2 . User
const agent = await Agent . get ( "compaction" )
const model = agent . model
? await Provider . getModel ( agent . model . providerID , agent . model . modelID )
: await Provider . getModel ( userMessage . model . providerID , userMessage . model . modelID )
2025-09-13 17:46:14 +08:00
const msg = ( await Session . updateMessage ( {
id : Identifier.ascending ( "message" ) ,
role : "assistant" ,
2025-11-17 23:57:18 +08:00
parentID : input.parentID ,
2025-09-13 17:46:14 +08:00
sessionID : input.sessionID ,
2025-12-15 10:11:30 +08:00
mode : "compaction" ,
agent : "compaction" ,
2025-11-17 23:57:18 +08:00
summary : true ,
2025-09-13 17:46:14 +08:00
path : {
cwd : Instance.directory ,
root : Instance.worktree ,
} ,
cost : 0 ,
tokens : {
output : 0 ,
input : 0 ,
reasoning : 0 ,
cache : { read : 0 , write : 0 } ,
} ,
2025-12-15 10:11:30 +08:00
modelID : model.id ,
2025-09-13 17:46:14 +08:00
providerID : model.providerID ,
time : {
created : Date.now ( ) ,
} ,
} ) ) as MessageV2 . Assistant
2025-11-17 23:57:18 +08:00
const processor = SessionProcessor . create ( {
assistantMessage : msg ,
2025-10-16 02:44:16 +08:00
sessionID : input.sessionID ,
2025-12-15 10:11:30 +08:00
model ,
2025-11-17 23:57:18 +08:00
abort : input.abort ,
} )
2025-12-23 12:19:14 +08:00
// Allow plugins to inject context or replace compaction prompt
2025-12-18 03:57:09 +08:00
const compacting = await Plugin . trigger (
"experimental.session.compacting" ,
{ sessionID : input.sessionID } ,
2025-12-23 12:19:14 +08:00
{ context : [ ] , prompt : undefined } ,
2025-12-18 03:57:09 +08:00
)
2025-12-23 12:19:14 +08:00
const defaultPrompt =
"Provide a detailed prompt for continuing our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next considering new session will not have access to our conversation."
const promptText = compacting . prompt ? ? [ defaultPrompt , . . . compacting . context ] . join ( "\n\n" )
2025-12-04 10:09:03 +08:00
const result = await processor . process ( {
2025-12-15 10:11:30 +08:00
user : userMessage ,
agent ,
abort : input.abort ,
sessionID : input.sessionID ,
tools : { } ,
system : [ ] ,
2025-12-04 10:09:03 +08:00
messages : [
2025-12-15 10:11:30 +08:00
. . . MessageV2 . toModelMessage ( input . messages ) ,
2025-12-04 10:09:03 +08:00
{
role : "user" ,
content : [
2025-11-19 02:09:50 +08:00
{
2025-12-04 10:09:03 +08:00
type : "text" ,
2025-12-23 12:19:14 +08:00
text : promptText ,
2025-11-19 02:09:50 +08:00
} ,
] ,
2025-12-04 10:09:03 +08:00
} ,
] ,
2025-12-15 10:11:30 +08:00
model ,
2025-12-04 10:09:03 +08:00
} )
2025-12-15 10:11:30 +08:00
2025-11-26 02:10:56 +08:00
if ( result === "continue" && input . auto ) {
2025-11-17 23:57:18 +08:00
const continueMsg = await Session . updateMessage ( {
id : Identifier.ascending ( "message" ) ,
role : "user" ,
sessionID : input.sessionID ,
time : {
created : Date.now ( ) ,
} ,
2025-12-15 10:11:30 +08:00
agent : userMessage.agent ,
model : userMessage.model ,
2025-10-23 07:31:36 +08:00
} )
2025-11-17 23:57:18 +08:00
await Session . updatePart ( {
id : Identifier.ascending ( "part" ) ,
messageID : continueMsg.id ,
2025-10-19 00:49:29 +08:00
sessionID : input.sessionID ,
2025-11-17 23:57:18 +08:00
type : "text" ,
synthetic : true ,
text : "Continue if you have next steps" ,
time : {
start : Date.now ( ) ,
end : Date.now ( ) ,
} ,
2025-10-19 00:49:29 +08:00
} )
}
2025-11-19 02:09:50 +08:00
if ( processor . message . error ) return "stop"
2025-11-26 03:47:10 +08:00
Bus . publish ( Event . Compacted , { sessionID : input.sessionID } )
2025-11-17 23:57:18 +08:00
return "continue"
2025-09-13 17:46:14 +08:00
}
2025-11-17 23:57:18 +08:00
export const create = fn (
z . object ( {
sessionID : Identifier.schema ( "session" ) ,
2025-11-21 16:13:10 +08:00
agent : z.string ( ) ,
2025-11-17 23:57:18 +08:00
model : z.object ( {
providerID : z.string ( ) ,
modelID : z.string ( ) ,
} ) ,
2025-11-26 02:10:56 +08:00
auto : z.boolean ( ) ,
2025-11-17 23:57:18 +08:00
} ) ,
async ( input ) = > {
const msg = await Session . updateMessage ( {
id : Identifier.ascending ( "message" ) ,
role : "user" ,
model : input.model ,
sessionID : input.sessionID ,
2025-11-21 16:13:10 +08:00
agent : input.agent ,
2025-11-17 23:57:18 +08:00
time : {
created : Date.now ( ) ,
} ,
} )
await Session . updatePart ( {
id : Identifier.ascending ( "part" ) ,
messageID : msg.id ,
sessionID : msg.sessionID ,
type : "compaction" ,
2025-11-26 02:10:56 +08:00
auto : input.auto ,
2025-11-17 23:57:18 +08:00
} )
} ,
)
2025-09-13 17:46:14 +08:00
}