feat(mobile): beta Working section hides busy threads until they need you (#15346)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-10-03 15:03:59 -07:00
1 parent a78760e5e7
commit 8d846660ce
16 files changed
+587 -103

No files matched your search

@@ -0,0 +1,59 @@
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import { createInboxReturnTracker } from "./threadInbox.ts";
const environmentId = EnvironmentId.make("environment-1");
function thread(id: string, working: boolean) {
return {
id: ThreadId.make(id),
environmentId,
createdAt: "2026-06-01T00:00:00.000Z",
unsettledAt: null,
latestRun: null,
hasActionableProposedPlan: false,
hasPendingApprovals: false,
hasPendingUserInput: false,
interactionMode: "default" as const,
runtime: working
? {
status: "running" as const,
activeRunId: null,
providerInstanceId: ProviderInstanceId.make("codex"),
providerName: "Codex",
lastError: null,
updatedAt: "2026-06-01T00:00:00.000Z",
}
: null,
};
}
describe("createInboxReturnTracker", () => {
it("stamps a thread when it stops working, but never on the first observation", () => {
const tracker = createInboxReturnTracker();
tracker.observe([thread("a", true), thread("b", false)]);
expect(tracker.returnedAt(thread("a", true))).toBeUndefined();
expect(tracker.returnedAt(thread("b", false))).toBeUndefined();
tracker.observe([thread("a", false), thread("b", false)]);
expect(tracker.returnedAt(thread("a", false))).toBeDefined();
expect(tracker.returnedAt(thread("b", false))).toBeUndefined();
});
it("forgets deleted threads and resets when the beta turns off", () => {
const tracker = createInboxReturnTracker();
tracker.observe([thread("a", true), thread("b", true)]);
tracker.observe([thread("a", false), thread("b", false)]);
tracker.observe([thread("b", false)]);
expect(tracker.returnedAt(thread("a", false))).toBeUndefined();
expect(tracker.returnedAt(thread("b", false))).toBeDefined();
tracker.observe(null);
expect(tracker.returnedAt(thread("b", false))).toBeUndefined();
// After a reset the next call is a fresh baseline again.
tracker.observe([thread("b", true)]);
tracker.observe([thread("b", false)]);
expect(tracker.returnedAt(thread("b", false))).toBeDefined();
});
});
@@ -0,0 +1,111 @@
import { threadRuntimeIsActive, type EnvironmentThreadShell } from "./models.ts";
import { toSortableTimestamp } from "./threadSort.ts";
// Working section beta, shared so web and mobile fold and order the inbox the
// same way. Off by default; each client owns its own toggle.
type WorkingThreadInput = Pick<
EnvironmentThreadShell,
| "hasActionableProposedPlan"
| "hasPendingApprovals"
| "hasPendingUserInput"
| "interactionMode"
| "latestRun"
| "runtime"
>;
/** Threads busy with work that does not need the user fold into the Working
section: a running run, or one stopped with background work that will wake
it. Approvals, questions, plan prompts, and failures stay in the inbox. */
export function isThreadWorking(thread: WorkingThreadInput): boolean {
if (thread.hasPendingApprovals || thread.hasPendingUserInput) return false;
if (!threadRuntimeIsActive(thread.runtime) && thread.runtime?.status !== "idle") return false;
// A plan prompt outranks lingering background work: the user has to act on it.
const run = thread.latestRun;
const runSettled =
run !== null &&
run.status !== "preparing" &&
run.status !== "queued" &&
run.status !== "starting" &&
run.status !== "running" &&
run.status !== "waiting" &&
thread.runtime?.activeRunId !== run.runId;
return !(thread.interactionMode === "plan" && thread.hasActionableProposedPlan && runSettled);
}
type InboxThreadInput = Pick<
EnvironmentThreadShell,
"id" | "environmentId" | "createdAt" | "unsettledAt" | "latestRun"
>;
/** The inbox lists threads newest first by when each last came back to the
user, so a thread that leaves the Working section lands on top.
`observedReturnAt` adds returns the server does not stamp, such as an
approval request mid-turn or background work ending. */
export function sortInboxThreadsByReturn<T extends InboxThreadInput>(
threads: readonly T[],
observedReturnAt?: (thread: T) => number | undefined,
): T[] {
const timestamps = new Map(
threads.map((thread) => [
thread,
Math.max(
toSortableTimestamp(thread.createdAt) ?? 0,
toSortableTimestamp(thread.unsettledAt ?? undefined) ?? 0,
toSortableTimestamp(thread.latestRun?.requestedAt ?? undefined) ?? 0,
toSortableTimestamp(thread.latestRun?.completedAt ?? undefined) ?? 0,
observedReturnAt?.(thread) ?? 0,
),
]),
);
return [...threads].sort(
(left, right) =>
timestamps.get(right)! - timestamps.get(left)! ||
left.id.localeCompare(right.id) ||
left.environmentId.localeCompare(right.environmentId),
);
}
/**
* Remembers when this client saw each thread leave the Working section. Keep
* one at module scope so the inbox order survives routes that unmount the
* list. Call `observe` with every thread shell on each list rebuild, or with
* null to reset while the beta is off. The first call only takes a baseline,
* so mounting never reshuffles the inbox.
*/
export function createInboxReturnTracker() {
const keyOf = (thread: Pick<EnvironmentThreadShell, "environmentId" | "id">) =>
`${thread.environmentId}:${thread.id}`;
let lastWorkingKeys: ReadonlySet<string> | null = null;
const returns = new Map<string, number>();
return {
observe(threads: ReadonlyArray<WorkingThreadInput & InboxThreadInput> | null): void {
if (threads === null) {
lastWorkingKeys = null;
returns.clear();
return;
}
const working = new Set<string>();
const present = new Set<string>();
for (const thread of threads) {
const key = keyOf(thread);
present.add(key);
if (isThreadWorking(thread)) working.add(key);
}
// Drop deleted threads so the map stays bounded by the live thread list.
for (const key of returns.keys()) {
if (!present.has(key)) returns.delete(key);
}
// The moment this client saw the change is the data; there is no
// server stamp to read instead.
// @effect-diagnostics-next-line globalDate:off
const at = Date.now();
for (const key of lastWorkingKeys ?? []) {
if (present.has(key) && !working.has(key)) returns.set(key, at);
}
lastWorkingKeys = working;
},
returnedAt: (thread: Pick<EnvironmentThreadShell, "environmentId" | "id">) =>
returns.get(keyOf(thread)),
};
}