22import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion" ;
33import { serializeConversation } from "openclaw/plugin-sdk/agent-core" ;
44import { beforeAll , describe , expect , it , vi } from "vitest" ;
5- import { runCompactionPlanningWorker } from "./compaction-planning-worker-runtime.js" ;
5+ import * as compactionPlanningWorkerRuntime from "./compaction-planning-worker-runtime.js" ;
6+ import {
7+ CompactionPlanningWorkerError ,
8+ runCompactionPlanningWorker ,
9+ } from "./compaction-planning-worker-runtime.js" ;
610import {
711 buildOversizedFallbackPlanWithWorker ,
12+ buildStageSplitPlanWithWorker ,
813 buildSummaryChunksWithWorker ,
14+ computeAdaptiveChunkRatioWithWorker ,
915} from "./compaction-planning-worker.js" ;
1016import { estimateMessagesTokens } from "./compaction-planning.js" ;
1117import { runCompactionPlanningWorkerInput } from "./compaction-planning.worker.js" ;
@@ -26,6 +32,29 @@ function createSyntheticWorkerUrl(source: string): URL {
2632 return new URL ( `data:text/javascript,${ encodeURIComponent ( source ) } ` ) ;
2733}
2834
35+ const cancellablePlanningOperations = [
36+ {
37+ operation : "summary chunks" ,
38+ run : ( messages : AgentMessage [ ] , signal : AbortSignal ) =>
39+ buildSummaryChunksWithWorker ( { messages, maxChunkTokens : 1_200 , signal } ) ,
40+ } ,
41+ {
42+ operation : "oversized fallback" ,
43+ run : ( messages : AgentMessage [ ] , signal : AbortSignal ) =>
44+ buildOversizedFallbackPlanWithWorker ( { messages, contextWindow : 1_200 , signal } ) ,
45+ } ,
46+ {
47+ operation : "stage splitting" ,
48+ run : ( messages : AgentMessage [ ] , signal : AbortSignal ) =>
49+ buildStageSplitPlanWithWorker ( { messages, maxChunkTokens : 1_200 , signal } ) ,
50+ } ,
51+ {
52+ operation : "adaptive chunk sizing" ,
53+ run : ( messages : AgentMessage [ ] , signal : AbortSignal ) =>
54+ computeAdaptiveChunkRatioWithWorker ( { messages, contextWindow : 1_200 , signal } ) ,
55+ } ,
56+ ] ;
57+
2958describe ( "compaction planning worker" , ( ) => {
3059 let packagedSummaryChunks : Awaited < ReturnType < typeof runCompactionPlanningWorker > > ;
3160
@@ -59,6 +88,69 @@ describe("compaction planning worker", () => {
5988 }
6089 } ) ;
6190
91+ it . each (
92+ cancellablePlanningOperations . flatMap ( ( { operation, run } ) =>
93+ [ 63 , 64 ] . map ( ( messageCount ) => ( { operation, run, messageCount } ) ) ,
94+ ) ,
95+ ) (
96+ "honors cancellation for $operation with $messageCount messages" ,
97+ async ( { run, messageCount } ) => {
98+ const reason = new Error ( "operator cancelled compaction" ) ;
99+ const signal = AbortSignal . abort ( reason ) ;
100+ const messages = Array . from ( { length : messageCount } , ( _ , index ) =>
101+ makeMessage ( index + 1 , "active user request" ) ,
102+ ) ;
103+
104+ await expect ( run ( messages , signal ) ) . rejects . toBe ( reason ) ;
105+ } ,
106+ ) ;
107+
108+ it ( "does not resume cancelled compaction when its worker becomes unavailable" , async ( ) => {
109+ const controller = new AbortController ( ) ;
110+ const reason = new Error ( "operator cancelled compaction" ) ;
111+ const worker = vi
112+ . spyOn ( compactionPlanningWorkerRuntime , "runCompactionPlanningWorker" )
113+ . mockImplementationOnce ( async ( ) => {
114+ controller . abort ( reason ) ;
115+ throw new CompactionPlanningWorkerError ( "worker disappeared" , "unavailable" ) ;
116+ } ) ;
117+
118+ try {
119+ await expect (
120+ buildSummaryChunksWithWorker ( {
121+ messages : Array . from ( { length : 64 } , ( _ , index ) => makeMessage ( index + 1 , "request" ) ) ,
122+ maxChunkTokens : 1_200 ,
123+ signal : controller . signal ,
124+ } ) ,
125+ ) . rejects . toBe ( reason ) ;
126+ } finally {
127+ worker . mockRestore ( ) ;
128+ }
129+ } ) ;
130+
131+ it ( "does not restore a worker plan after its compaction has been cancelled" , async ( ) => {
132+ const controller = new AbortController ( ) ;
133+ const reason = new Error ( "operator cancelled compaction" ) ;
134+ const worker = vi
135+ . spyOn ( compactionPlanningWorkerRuntime , "runCompactionPlanningWorker" )
136+ . mockImplementationOnce ( async ( ) => {
137+ controller . abort ( reason ) ;
138+ return { kind : "summaryChunks" , chunkIndexes : [ [ 0 ] ] } ;
139+ } ) ;
140+
141+ try {
142+ await expect (
143+ buildSummaryChunksWithWorker ( {
144+ messages : Array . from ( { length : 64 } , ( _ , index ) => makeMessage ( index + 1 , "request" ) ) ,
145+ maxChunkTokens : 1_200 ,
146+ signal : controller . signal ,
147+ } ) ,
148+ ) . rejects . toBe ( reason ) ;
149+ } finally {
150+ worker . mockRestore ( ) ;
151+ }
152+ } ) ;
153+
62154 it ( "plans summary chunks in the packaged worker" , ( ) => {
63155 expect ( packagedSummaryChunks . kind ) . toBe ( "summaryChunks" ) ;
64156 if ( packagedSummaryChunks . kind !== "summaryChunks" ) {
0 commit comments