Skip to content

Commit 6dc7149

Browse files
committed
fix(compaction): settle command edge cases
1 parent 99e4f9b commit 6dc7149

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
833833
connectionError={props.connectionError}
834834
environmentLabel={props.environmentLabel}
835835
selectedThread={props.selectedThread}
836-
hasCompactableConversation={hasCompactableConversation}
836+
hasCompactableConversation={hasCompactableConversation && !props.isCompacting}
837837
serverConfig={props.serverConfig}
838838
queueCount={props.selectedThreadQueueCount}
839839
environmentId={props.environmentId}

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,19 @@ const make = Effect.gen(function* () {
442442
});
443443
});
444444

445-
const setThreadSessionReadyAfterCompaction = Effect.fnUntraced(function* (threadId: ThreadId) {
445+
const restoreCompaction = Effect.fnUntraced(function* (threadId: ThreadId, fromRunning = false) {
446446
if (stoppingThreadIds.has(threadId)) {
447447
compactingThreadIds.delete(threadId);
448448
return;
449449
}
450450
const thread = yield* resolveThread(threadId);
451+
if (!thread?.session) return;
451452
if (
452-
!thread?.session ||
453-
(thread.session.status !== "starting" && thread.session.status !== "ready")
454-
) {
453+
thread.session.status !== "starting" &&
454+
thread.session.status !== "ready" &&
455+
(!fromRunning || thread.session.status !== "running")
456+
)
455457
return;
456-
}
457458
const completedAt = DateTime.formatIso(yield* DateTime.now);
458459
if (stoppingThreadIds.has(threadId)) {
459460
compactingThreadIds.delete(threadId);
@@ -1335,7 +1336,7 @@ const make = Effect.gen(function* () {
13351336
}
13361337
return appendTurnStartFailure("Context compaction failed", detail).pipe(
13371338
Effect.ensuring(
1338-
setThreadSessionReadyAfterCompaction(event.payload.threadId).pipe(
1339+
restoreCompaction(event.payload.threadId).pipe(
13391340
Effect.catchCause((restoreCause) =>
13401341
Effect.logWarning("failed to restore provider session after compaction failure", {
13411342
threadId: event.payload.threadId,
@@ -1396,7 +1397,7 @@ const make = Effect.gen(function* () {
13961397
event.payload.messageId,
13971398
);
13981399
}).pipe(
1399-
Effect.andThen(setThreadSessionReadyAfterCompaction(event.payload.threadId)),
1400+
Effect.andThen(restoreCompaction(event.payload.threadId, true)),
14001401
Effect.catchCause(recoverCompactionFailure),
14011402
Effect.ensuring(Effect.sync(() => void compactingThreadIds.delete(event.payload.threadId))),
14021403
Effect.forkScoped,
@@ -1643,7 +1644,7 @@ const make = Effect.gen(function* () {
16431644
return wasCompacting && !compactingThreadIds.has(thread.id);
16441645
}).pipe(
16451646
Effect.flatMap((compactionSettled) =>
1646-
compactionSettled ? setThreadSessionReadyAfterCompaction(thread.id) : Effect.void,
1647+
compactionSettled ? restoreCompaction(thread.id) : Effect.void,
16471648
),
16481649
Effect.andThen(
16491650
appendProviderFailureActivity({

‎apps/server/src/provider/Layers/ProviderService.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,13 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
454454
return;
455455
}
456456
if (pendingCompaction.native) {
457+
const compacted = isCompactedEvent(canonicalEvent);
458+
const terminal = compacted ? "completed" : compactionTerminal(canonicalEvent);
457459
yield* publishRuntimeEvent(
458-
isCompactedEvent(canonicalEvent)
459-
? withCompactionRequestId(canonicalEvent, pendingCompaction)
460-
: canonicalEvent,
460+
compacted ? withCompactionRequestId(canonicalEvent, pendingCompaction) : canonicalEvent,
461461
);
462-
if (isCompactedEvent(canonicalEvent)) {
463-
yield* settleCompaction(canonicalEvent.threadId, pendingCompaction, "completed");
464-
}
462+
if (terminal !== null)
463+
yield* settleCompaction(canonicalEvent.threadId, pendingCompaction, terminal);
465464
return;
466465
}
467466
if (

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18551855
});
18561856
const compactSlashCommandAvailable =
18571857
composerTrigger?.kind === "slash-command" &&
1858+
prompt.slice(0, composerTrigger.rangeStart).trim() === "" &&
18581859
!compactThreadUnavailable &&
18591860
prompt.slice(composerTrigger.rangeEnd).trim() === "" &&
18601861
composerImages.length + composerFiles.length === 0 &&

0 commit comments

Comments
 (0)