feat(sidebar): settle a running thread once the agent finishes #14495
Closedpiotrnowakowski(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/14495/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -269,16 +269,9 @@269269 ...commands,270270 snapshotAtom: optimistic.snapshotAtom,271271 settle: optimistic.wrap(commands.settle, (thread, _input, now, accepted) =>272- !accepted &&273- (!canSnooze(thread, { now }) ||274- thread.session?.status === "starting" ||275- thread.session?.status === "running")272+ !accepted && !canSnooze(thread, { now })276273 ? thread277274 : {278275 ...thread,279- hasPendingApprovals: false,280- hasPendingUserInput: false,281- settledOverride: "settled",282- settledAt: thread.settledOverride === "settled" ? (thread.settledAt ?? now) : now,283- unsettledAt: null,284- activeOrderKey: null,276+ // The server files a working thread away and settles it once idle.277+ ...(isThreadWorking(thread, { now })apps/server/src/orchestration/ThreadSettlementReactor.ts lines 124 to 128OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -90,9 +109,20 @@109+ (cause) =>110+ Effect.logWarning("settle when idle skipped", {111+ threadId,112+ cause: Cause.pretty(cause),113+ }),114+ ),115+ ),116+ );117+90118 const sweep = Effect.fn("ThreadSettlementReactor.sweep")(function* (91119 mergedPullRequest: PullRequestService.PullRequestMergeEvent | null,92120 threadId?: ThreadId,93121 ) {94122 const settings = yield* settingsService.getSettings;95- if (!autoSettlementConfigured(settings)) {123+ const configured = autoSettlementConfigured(settings);124+ // Settle when idle needs only the one-thread checks its events queue.125+ if (!configured && threadId === undefined) {96126 return;97127 }98128 const snapshot = yield* readSweepSnapshot(snapshots, threadId ?? null);apps/server/src/orchestration/ThreadSettlementReactor.ts line 389OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -339,3 +379,11 @@339379 // Merge notifications can arrive before the linked snapshot is projected.340380 // Recheck the persisted state so terminal links settle without the timer.341381 return worker.enqueue(event.payload.threadId);382+ case "thread.settle-when-idle-set":383+ // The intent can land after the turn already ended.384+ return event.payload.settleWhenIdleAt === null385+ ? Effect.void386+ : worker.enqueue(event.payload.threadId);387+ case "thread.activity-appended":388+ // Background work (a subagent, a watch loop) can end after the turn.389+ return event.payload.activity.kind === "task.completed"apps/mobile/src/features/threads/threadListV2.ts line 598Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -586,4 +587,4 @@586587 }587588 const hasQueuedMessages =588589 input.queuedThreadKeys?.has(`${thread.environmentId}:${thread.id}`) === true;589- if (supportsSettlement && thread.settledOverride === "settled" && !hasQueuedMessages) {590+ if (supportsSettlement && isFiledAsSettled(thread) && !hasQueuedMessages) {
- apps/server/src/orchestration/decider.ts lines 542 to 543OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -484,6 +536,8 @@484536 command,485537 threadId: command.threadId,486538 });539+ // The auto-settle opt-out does not cover a settle the user asked for.487540 if (488541 command.type === "thread.auto-settle" &&489- (thread.settledOverride !== null || thread.autoSettleDisabledAt != null)542+ (thread.settledOverride !== null ||543+ (thread.autoSettleDisabledAt != null && thread.settleWhenIdleAt == null))apps/server/src/orchestration/Normalizer.ts line 144OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -131,3 +134,11 @@131134 } satisfies OrchestrationCommand;132135 }133136137+ if (canonicalCommand.type === "thread.settle") {138+ // Settling stops the session. A thread still working, including139+ // background work, files away now and settles once idle instead.140+ const snapshots = yield* ProjectionSnapshotQuery;141+ const thread = yield* snapshots142+ .getThreadShellById(canonicalCommand.threadId)143+ .pipe(Effect.orElseSucceed(() => Option.none()));144+ if (Option.isSome(thread) && !isThreadIdleForSettlement(thread.value, receivedAt)) {apps/server/src/orchestration/ThreadSettlementReactor.ts line 125OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -90,6 +106,20 @@106+ effect.pipe(107+ Effect.catchCauseIf(108+ (cause) => !Cause.hasInterruptsOnly(cause),109+ (cause) =>110+ Effect.logWarning("settle when idle skipped", {111+ threadId,112+ cause: Cause.pretty(cause),113+ }),114+ ),115+ ),116+ );117+90118 const sweep = Effect.fn("ThreadSettlementReactor.sweep")(function* (91119 mergedPullRequest: PullRequestService.PullRequestMergeEvent | null,92120 threadId?: ThreadId,93121 ) {94122 const settings = yield* settingsService.getSettings;95- if (!autoSettlementConfigured(settings)) {123+ const configured = autoSettlementConfigured(settings);124+ // Settle when idle needs only the one-thread checks its events queue.125+ if (!configured && threadId === undefined) {
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
juliusmarminge closed this
Sign in to join this conversation.




macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed