@@ -30,7 +30,10 @@ import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
3030
3131import { resolveThreadWorkspaceCwd } from "../../checkpointing/Utils.ts" ;
3232import { increment , orchestrationEventsProcessedTotal } from "../../observability/Metrics.ts" ;
33- import { ProviderAdapterRequestError } from "../../provider/Errors.ts" ;
33+ import {
34+ ProviderAdapterRequestError ,
35+ ProviderAdapterValidationError ,
36+ } from "../../provider/Errors.ts" ;
3437import type { ProviderServiceError } from "../../provider/Errors.ts" ;
3538import { TextGeneration } from "../../textGeneration/TextGeneration.ts" ;
3639import { ProviderService } from "../../provider/Services/ProviderService.ts" ;
@@ -50,6 +53,7 @@ import {
5053import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts" ;
5154import { GitWorkflowService } from "../../git/GitWorkflowService.ts" ;
5255const isProviderAdapterRequestError = Schema . is ( ProviderAdapterRequestError ) ;
56+ const isProviderAdapterValidationError = Schema . is ( ProviderAdapterValidationError ) ;
5357const isProviderDriverKind = Schema . is ( ProviderDriverKind ) ;
5458
5559type ProviderIntentEvent = Extract <
@@ -72,6 +76,10 @@ function toNonEmptyProviderInput(value: string | undefined): string | undefined
7276 return normalized && normalized . length > 0 ? normalized : undefined ;
7377}
7478
79+ const isCompactCommandMessage = ( message : ThreadTitleMessage ) : boolean =>
80+ message . role === "user" &&
81+ ( message . attachments ?. length ?? 0 ) === 0 &&
82+ message . text . trim ( ) . toLowerCase ( ) === "/compact" ;
7583function mapProviderSessionStatusToOrchestrationStatus (
7684 status : "connecting" | "ready" | "running" | "error" | "closed" ,
7785) : OrchestrationSession [ "status" ] {
@@ -332,6 +340,7 @@ const make = Effect.gen(function* () {
332340 ) ;
333341
334342 const threadModelSelections = new Map < string , ModelSelection > ( ) ;
343+ const compactingThreadIds = new Set < ThreadId > ( ) ;
335344
336345 const appendProviderFailureActivity = ( input : {
337346 readonly threadId : ThreadId ;
@@ -375,11 +384,11 @@ const make = Effect.gen(function* () {
375384
376385 const formatFailureDetail = ( cause : Cause . Cause < unknown > ) : string => {
377386 const failReason = cause . reasons . find ( Cause . isFailReason ) ;
378- const providerError = isProviderAdapterRequestError ( failReason ?. error )
379- ? failReason . error
380- : undefined ;
381- if ( providerError ) {
382- return providerError . detail ;
387+ if ( isProviderAdapterRequestError ( failReason ?. error ) ) {
388+ return failReason . error . detail ;
389+ }
390+ if ( isProviderAdapterValidationError ( failReason ?. error ) ) {
391+ return failReason . error . issue ;
383392 }
384393 return Cause . pretty ( cause ) ;
385394 } ;
@@ -1137,7 +1146,6 @@ const make = Effect.gen(function* () {
11371146 if ( ! thread ) {
11381147 return ;
11391148 }
1140-
11411149 const message = thread . messages . find ( ( entry ) => entry . id === event . payload . messageId ) ;
11421150 if ( ! message || message . role !== "user" ) {
11431151 yield * appendProviderFailureActivity ( {
@@ -1147,15 +1155,28 @@ const make = Effect.gen(function* () {
11471155 detail : `User message '${ event . payload . messageId } ' was not found for turn start request.` ,
11481156 turnId : null ,
11491157 createdAt : event . payload . createdAt ,
1158+ requestId : event . payload . messageId ,
11501159 } ) ;
11511160 return ;
11521161 }
1162+ const appendTurnStartFailure = ( summary : string , detail : string ) =>
1163+ appendProviderFailureActivity ( {
1164+ threadId : event . payload . threadId ,
1165+ kind : "provider.turn.start.failed" ,
1166+ summary,
1167+ detail,
1168+ turnId : null ,
1169+ createdAt : event . payload . createdAt ,
1170+ requestId : event . payload . messageId ,
1171+ } ) ;
11531172
11541173 yield * ensureThreadWorktree ( thread ) ;
11551174
1156- const isFirstUserMessageTurn =
1157- thread . messages . filter ( ( entry ) => entry . role === "user" ) . length === 1 ;
1158- if ( isFirstUserMessageTurn ) {
1175+ const isCompactCommand = isCompactCommandMessage ( message ) ;
1176+ const nonCompactUserMessageCount = thread . messages . filter (
1177+ ( entry ) => entry . role === "user" && ! isCompactCommandMessage ( entry ) ,
1178+ ) . length ;
1179+ if ( nonCompactUserMessageCount === 1 && ! isCompactCommand ) {
11591180 const project = yield * resolveProject ( thread . projectId ) ;
11601181 const generationCwd =
11611182 resolveThreadWorkspaceCwd ( {
@@ -1194,16 +1215,7 @@ const make = Effect.gen(function* () {
11941215 detail,
11951216 createdAt : event . payload . createdAt ,
11961217 } ) . pipe (
1197- Effect . flatMap ( ( ) =>
1198- appendProviderFailureActivity ( {
1199- threadId : event . payload . threadId ,
1200- kind : "provider.turn.start.failed" ,
1201- summary : "Provider turn start failed" ,
1202- detail,
1203- turnId : null ,
1204- createdAt : event . payload . createdAt ,
1205- } ) ,
1206- ) ,
1218+ Effect . flatMap ( ( ) => appendTurnStartFailure ( "Provider turn start failed" , detail ) ) ,
12071219 Effect . asVoid ,
12081220 ) ;
12091221 } ;
@@ -1220,6 +1232,69 @@ const make = Effect.gen(function* () {
12201232 ) ,
12211233 ) ;
12221234
1235+ const handleCompactionFailure = ( cause : Cause . Cause < unknown > ) => {
1236+ if ( Cause . hasInterruptsOnly ( cause ) ) {
1237+ return Effect . void ;
1238+ }
1239+ const detail = formatFailureDetail ( cause ) ;
1240+ return appendTurnStartFailure ( "Context compaction failed" , detail ) . pipe ( Effect . asVoid ) ;
1241+ } ;
1242+ const recoverCompactionFailure = ( cause : Cause . Cause < unknown > ) =>
1243+ handleCompactionFailure ( cause ) . pipe (
1244+ Effect . catchCause ( ( recoveryCause ) =>
1245+ Effect . logWarning ( "provider command reactor failed to recover compaction failure" , {
1246+ eventType : event . type ,
1247+ threadId : event . payload . threadId ,
1248+ cause : Cause . pretty ( recoveryCause ) ,
1249+ originalCause : Cause . pretty ( cause ) ,
1250+ } ) ,
1251+ ) ,
1252+ ) ;
1253+ if ( isCompactCommand ) {
1254+ if ( nonCompactUserMessageCount === 0 ) {
1255+ return yield * appendTurnStartFailure (
1256+ "Context compaction failed" ,
1257+ "Context compaction requires an existing conversation." ,
1258+ ) ;
1259+ }
1260+ const latestThread = yield * resolveThread ( event . payload . threadId ) ;
1261+ if (
1262+ compactingThreadIds . has ( event . payload . threadId ) ||
1263+ latestThread ?. session ?. status === "starting" ||
1264+ latestThread ?. session ?. status === "running"
1265+ ) {
1266+ yield * appendTurnStartFailure (
1267+ "Context compaction failed" ,
1268+ "Context compaction is unavailable while a provider turn is running." ,
1269+ ) ;
1270+ return ;
1271+ }
1272+ compactingThreadIds . add ( event . payload . threadId ) ;
1273+ yield * Effect . gen ( function * ( ) {
1274+ yield * ensureSessionForThread (
1275+ event . payload . threadId ,
1276+ event . payload . createdAt ,
1277+ event . payload . modelSelection !== undefined
1278+ ? { modelSelection : event . payload . modelSelection }
1279+ : undefined ,
1280+ ) ;
1281+ if ( event . payload . modelSelection !== undefined ) {
1282+ threadModelSelections . set ( event . payload . threadId , event . payload . modelSelection ) ;
1283+ }
1284+ yield * providerService . compactThread ( event . payload . threadId , event . payload . modelSelection ) ;
1285+ } ) . pipe (
1286+ Effect . catchCause ( recoverCompactionFailure ) ,
1287+ Effect . ensuring ( Effect . sync ( ( ) => void compactingThreadIds . delete ( event . payload . threadId ) ) ) ,
1288+ Effect . forkScoped ,
1289+ ) ;
1290+ return ;
1291+ }
1292+ if ( compactingThreadIds . has ( event . payload . threadId ) ) {
1293+ return yield * appendTurnStartFailure (
1294+ "Provider turn start failed" ,
1295+ "Wait for context compaction to finish before sending another message." ,
1296+ ) ;
1297+ }
12231298 const sendTurnRequest = yield * buildSendTurnRequestForThread ( {
12241299 threadId : event . payload . threadId ,
12251300 messageText : message . text ,
@@ -1240,7 +1315,7 @@ const make = Effect.gen(function* () {
12401315
12411316 yield * providerService
12421317 . sendTurn ( sendTurnRequest . value )
1243- . pipe ( Effect . catchCause ( recoverTurnStartFailure ) , Effect . forkScoped ) ;
1318+ . pipe ( Effect . asVoid , Effect . catchCause ( recoverTurnStartFailure ) , Effect . forkScoped ) ;
12441319 } ) ;
12451320
12461321 const processTurnInterruptRequested = Effect . fn ( "processTurnInterruptRequested" ) ( function * (
0 commit comments