55 errorShape ,
66 validateSessionsCompactParams ,
77} from "../../../packages/gateway-protocol/src/index.js" ;
8+ import { resolveDefaultAgentId } from "../../agents/agent-scope.js" ;
89import { clearSessionQueues } from "../../auto-reply/reply/queue/cleanup.js" ;
910import {
1011 resolveSessionWorkStartError ,
@@ -18,16 +19,18 @@ import {
1819} from "../../config/sessions/session-accessor.js" ;
1920import { formatErrorMessage } from "../../infra/errors.js" ;
2021import {
21- interruptSessionWorkAdmissions ,
22+ isCompetingSessionWorkAdmissionActive ,
2223 runExclusiveSessionLifecycleMutation ,
23- SESSION_WORK_ADMISSION_DRAIN_TIMEOUT_MS ,
2424} from "../../sessions/session-lifecycle-admission.js" ;
2525import { recordSessionCompacted } from "../../sessions/session-state-events.js" ;
2626import { resolveRequestedSessionAgentId as resolveRequestedGlobalAgentId } from "../session-create-service.js" ;
2727import { migrateAndPruneGatewaySessionStoreKey } from "../session-utils.js" ;
28+ import { hasVisibleActiveSessionRun } from "./session-active-runs.js" ;
2829import { emitSessionsChanged } from "./session-change-event.js" ;
29- import { runGatewaySessionCompaction } from "./sessions-compaction-runner.js" ;
30- import { interruptSessionRunIfActive } from "./sessions-messaging.js" ;
30+ import {
31+ preflightGatewaySessionCompaction ,
32+ runGatewaySessionCompaction ,
33+ } from "./sessions-compaction-runner.js" ;
3134import {
3235 emitSessionOperation ,
3336 loadAccessorSessionEntryForGatewayTarget ,
@@ -39,7 +42,7 @@ import type { GatewayRequestHandlers } from "./types.js";
3942import { assertValidParams } from "./validation.js" ;
4043
4144export const sessionCompactHandlers : GatewayRequestHandlers = {
42- "sessions.compact" : async ( { req , params, respond, context, client, isWebchatConnect } ) => {
45+ "sessions.compact" : async ( { params, respond, context, client, isWebchatConnect } ) => {
4346 if ( ! assertValidParams ( params , validateSessionsCompactParams , "sessions.compact" , respond ) ) {
4447 return ;
4548 }
@@ -168,7 +171,8 @@ export const sessionCompactHandlers: GatewayRequestHandlers = {
168171 lifecycleRevision ,
169172 ] ;
170173 let sessionStillCurrent = true ;
171- let admittedWorkReleased = true ;
174+ let compactionNoopReason : string | undefined ;
175+ let blockedByActiveRun = false ;
172176 try {
173177 await runExclusiveSessionLifecycleMutation ( {
174178 scope : storePath ,
@@ -180,23 +184,47 @@ export const sessionCompactHandlers: GatewayRequestHandlers = {
180184 cfg,
181185 agentId : requestedAgentId ,
182186 } ) . entry ;
183- sessionStillCurrent = Boolean (
184- latestEntry &&
185- latestEntry . sessionId === sessionId &&
186- latestEntry . lifecycleRevision === lifecycleRevision &&
187- ! resolveSessionWorkStartError ( target . canonicalKey , latestEntry ) ,
188- ) ;
189- if ( ! sessionStillCurrent ) {
187+ if (
188+ ! latestEntry ||
189+ latestEntry . sessionId !== sessionId ||
190+ latestEntry . lifecycleRevision !== lifecycleRevision ||
191+ resolveSessionWorkStartError ( target . canonicalKey , latestEntry )
192+ ) {
193+ sessionStillCurrent = false ;
194+ return ;
195+ }
196+ if ( maxLines === undefined ) {
197+ compactionNoopReason = (
198+ await preflightGatewaySessionCompaction ( {
199+ cfg,
200+ entry : latestEntry ,
201+ agentId : target . agentId ,
202+ sessionId,
203+ sessionKey : target . canonicalKey ,
204+ sessionStoreKey : compactTarget . primaryKey ,
205+ storePath,
206+ } )
207+ ) ?. reason ;
208+ if ( compactionNoopReason ) {
209+ return ;
210+ }
211+ }
212+ blockedByActiveRun =
213+ isCompetingSessionWorkAdmissionActive ( storePath , lifecycleIdentities ) ||
214+ hasVisibleActiveSessionRun ( {
215+ context,
216+ requestedKey : key ,
217+ canonicalKey : target . canonicalKey ,
218+ sessionId,
219+ agentId : requestedAgentId ,
220+ defaultAgentId : resolveDefaultAgentId ( cfg ) ,
221+ } ) ;
222+ if ( blockedByActiveRun ) {
190223 return ;
191224 }
192225 // Drop work queued against the pre-compaction transcript before its
193- // active admission drains and no longer exposes queue cleanup.
226+ // lifecycle fence commits and no longer exposes queue cleanup.
194227 clearSessionQueues ( [ key , target . canonicalKey , compactTarget . primaryKey , sessionId ] ) ;
195- admittedWorkReleased = await interruptSessionWorkAdmissions ( {
196- scope : storePath ,
197- identities : lifecycleIdentities ,
198- timeoutMs : SESSION_WORK_ADMISSION_DRAIN_TIMEOUT_MS ,
199- } ) ;
200228 } ,
201229 run : async ( ) => {
202230 if ( ! sessionStillCurrent ) {
@@ -211,11 +239,27 @@ export const sessionCompactHandlers: GatewayRequestHandlers = {
211239 ) ;
212240 return ;
213241 }
214- if ( ! admittedWorkReleased ) {
242+ if ( compactionNoopReason ) {
243+ respond (
244+ true ,
245+ {
246+ ok : false ,
247+ key : target . canonicalKey ,
248+ compacted : false ,
249+ reason : compactionNoopReason ,
250+ } ,
251+ undefined ,
252+ ) ;
253+ return ;
254+ }
255+ if ( blockedByActiveRun ) {
215256 respond (
216257 false ,
217258 undefined ,
218- errorShape ( ErrorCodes . UNAVAILABLE , `Session ${ key } is still active; try again.` ) ,
259+ errorShape (
260+ ErrorCodes . INVALID_REQUEST ,
261+ `Session ${ key } has an active run; retry after it finishes.` ,
262+ ) ,
219263 ) ;
220264 return ;
221265 }
@@ -243,21 +287,6 @@ export const sessionCompactHandlers: GatewayRequestHandlers = {
243287 return ;
244288 }
245289
246- const interruptResult = await interruptSessionRunIfActive ( {
247- req,
248- context,
249- client,
250- isWebchatConnect,
251- requestedKey : key ,
252- canonicalKey : target . canonicalKey ,
253- agentId : requestedAgentId ,
254- sessionId,
255- } ) ;
256- if ( interruptResult . error ) {
257- respond ( false , undefined , interruptResult . error ) ;
258- return ;
259- }
260-
261290 const operationId = randomUUID ( ) ;
262291 if ( maxLines !== undefined ) {
263292 const trimResult = await trimSessionTranscriptForManualCompact (
0 commit comments