Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

fix(client-runtime): keep a warm thread un-settled despite a merged/closed PR (#4309)

Theo Browne193e3c62e6d5
diff --git a/packages/client-runtime/src/state/threadSettled.test.ts b/packages/client-runtime/src/state/threadSettled.test.ts
index 59f9f1d6f..421d95c6b 100644
--- a/packages/client-runtime/src/state/threadSettled.test.ts
+++ b/packages/client-runtime/src/state/threadSettled.test.ts
@@ -163,6 +163,50 @@ describe("effectiveSettled", () => {
).toBe(true);
});
+ it("does not re-settle a warm thread on the merge signal: a message sent in a settled thread keeps it active until idle", () => {
+ // The merge signal never clears, so without the idle guard a follow-up
+ // message would un-settle the row only until its turn completed, then
+ // snap straight back into the settled tail.
+ const justActive = makeShell({ activityAt: "2026-04-09T23:30:00.000Z" });
+ // The idle gate is strict: activity exactly one hour old is still warm.
+ const boundary = makeShell({ activityAt: "2026-04-09T23:00:00.000Z" });
+ const idle = makeShell({ activityAt: "2026-04-09T22:59:59.999Z" });
+
+ for (const changeRequestState of ["merged", "closed"] as const) {
+ expect(
+ effectiveSettled(justActive, {
+ now: NOW,
+ autoSettleAfterDays: null,
+ changeRequestState,
+ }),
+ ).toBe(false);
+ expect(
+ effectiveSettled(boundary, {
+ now: NOW,
+ autoSettleAfterDays: null,
+ changeRequestState,
+ }),
+ ).toBe(false);
+ expect(
+ effectiveSettled(idle, {
+ now: NOW,
+ autoSettleAfterDays: null,
+ changeRequestState,
+ }),
+ ).toBe(true);
+ }
+ });
+
+ it("re-settles a merged-PR thread once the follow-up burst goes idle", () => {
+ // Same shell, advancing clock: active while warm, settled again after
+ // the idle window passes — the burst cools and the merge signal wins.
+ const shell = makeShell({ activityAt: "2026-04-09T23:30:00.000Z" });
+ const options = { autoSettleAfterDays: null, changeRequestState: "merged" as const };
+
+ expect(effectiveSettled(shell, { ...options, now: NOW })).toBe(false);
+ expect(effectiveSettled(shell, { ...options, now: "2026-04-10T00:30:00.001Z" })).toBe(true);
+ });
+
it("never settles a starting session, even with a settled override", () => {
const shell = makeShell({
settledOverride: "settled",
diff --git a/packages/client-runtime/src/state/threadSettled.ts b/packages/client-runtime/src/state/threadSettled.ts
index 3cb4f7e65..85d1649c2 100644
--- a/packages/client-runtime/src/state/threadSettled.ts
+++ b/packages/client-runtime/src/state/threadSettled.ts
@@ -91,12 +91,29 @@ export function canSettle(
}
/**
- * Settled resolution over the server-backed settled lifecycle. The explicit
- * user override (thread.settle / thread.unsettle commands, projected into
- * settledOverride + settledAt) wins in both directions; without one, a
- * thread auto-settles on a merged/closed PR or inactivity past the window.
- * The server un-settles on real activity (user message, session start,
- * approval/user-input request), so an override never goes stale silently.
+ * A merged/closed change request settles its thread only once the thread has
+ * been idle this long. Without the idle guard the merge signal is permanent:
+ * sending a message to a merged-PR thread would un-settle the row only until
+ * its turn completed, then the still-merged PR would snap it straight back
+ * into the settled tail. An hour keeps the follow-up conversation visible
+ * while it is warm; once the burst goes stale the merge signal settles it
+ * again. Activity timestamps can originate on another device while `now` is
+ * this caller's clock: skew shortens or stretches the window by its size,
+ * the same exposure the inactivity auto-settle already accepts — worst case
+ * is a row changing lists early or late, never lost work.
+ */
+export const CHANGE_REQUEST_SETTLE_IDLE_MS = 60 * 60 * 1_000;
+
+/**
+ * Settled resolution over the server-backed settled lifecycle. Activity
+ * blockers (pending approval/user-input, a live session, an unadjudicated
+ * queued turn) are checked first and hold a thread active regardless of any
+ * override. Past the blockers, the explicit user override (thread.settle /
+ * thread.unsettle commands, projected into settledOverride + settledAt)
+ * wins in both directions; without one, a thread auto-settles on a
+ * merged/closed PR (once idle) or inactivity past the window. The server
+ * un-settles on real activity (user message, session start, approval/
+ * user-input request), so an override never goes stale silently.
*/
export function effectiveSettled(
shell: OrchestrationThreadShell,
@@ -130,7 +147,16 @@ export function effectiveSettled(
// until real activity clears it server-side.
if (shell.settledOverride === "active") return false;
if (options.changeRequestState === "merged" || options.changeRequestState === "closed") {
- return true;
+ // Only an idle thread settles on the merge signal: the signal itself
+ // never clears, so without this guard fresh activity (a message sent in
+ // a settled thread) would re-settle the moment its turn completed.
+ const lastActivityAt = threadLastActivityAt(shell);
+ if (
+ lastActivityAt === null ||
+ Date.parse(lastActivityAt) < Date.parse(options.now) - CHANGE_REQUEST_SETTLE_IDLE_MS
+ ) {
+ return true;
+ }
}
if (options.autoSettleAfterDays === null) return false;