Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apps/mobile/src/features/threads/threadListV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ describe("resolveThreadListV2SnoozeMenuSelection", () => {
});

describe("resolveThreadListV2Status", () => {
it("reports waiting for a running turn blocked on another thread", () => {
const thread = makeThread({
id: ThreadId.make("blocked"),
title: "Blocked on a thread",
runtime: {
status: "running",
activeRunId: null,
providerInstanceId: ProviderInstanceId.make("codex"),
providerName: "Codex",
lastError: null,
updatedAt: NOW,
},
});
expect(resolveThreadListV2Status(thread)).toBe("working");
expect(resolveThreadListV2Status({ ...thread, blockedOnThreadWait: true })).toBe("waiting");
});

it("distinguishes usage limits from ordinary failures and clears the label after recovery", () => {
const thread = makeThread({
id: ThreadId.make("limited"),
Expand Down
10 changes: 9 additions & 1 deletion apps/mobile/src/features/threads/threadListV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,22 @@ export function threadHasUnseenCompletion(
}

export function resolveThreadListV2Status(
thread: Pick<EnvironmentThreadShell, "hasPendingApprovals" | "hasPendingUserInput" | "runtime">,
thread: Pick<
EnvironmentThreadShell,
"hasPendingApprovals" | "hasPendingUserInput" | "runtime" | "blockedOnThreadWait"
>,
): ThreadListV2Status {
if (thread.hasPendingApprovals) {
return "approval";
}
if (thread.hasPendingUserInput) {
return "input";
}
// A turn blocked in a call that waits for another thread does nothing until
// that thread finishes, so it reads as Waiting, not Working.
if (thread.runtime?.status === "running" && thread.blockedOnThreadWait === true) {
return "waiting";
}
if (
thread.runtime !== null &&
["preparing", "queued", "starting", "running", "waiting"].includes(thread.runtime.status)
Expand Down
109 changes: 109 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5211,6 +5211,115 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
}),
);

it.effect("flags a running turn blocked in a call that waits for another thread", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const threadId = ThreadId.make("thread:blocked-on-thread-wait");
const runId = RunId.make("run:blocked-on-thread-wait");
const waitItemId = TurnItemId.make("turn-item:blocked-on-thread-wait:wait");
const at = DateTime.makeUnsafe("2026-10-12T12:00:00.000Z");
yield* store.apply({
id: EventId.make("event:blocked-on-thread-wait:thread"),
type: "thread.created",
threadId,
occurredAt: at,
payload: {
createdBy: "user",
creationSource: "web",
id: threadId,
projectId: ProjectId.make("project:blocked-on-thread-wait"),
title: "Waits on a thread",
providerInstanceId,
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
activeProviderThreadId: null,
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
forkedFrom: null,
createdAt: at,
updatedAt: at,
archivedAt: null,
settledOverride: null,
settledAt: null,
lastVisitedAt: null,
deletedAt: null,
},
});
yield* store.apply({
id: EventId.make("event:blocked-on-thread-wait:run"),
type: "run.created",
threadId,
runId,
driver,
providerInstanceId,
occurredAt: at,
payload: {
id: runId,
threadId,
ordinal: 1,
providerInstanceId,
modelSelection,
providerThreadId: null,
userMessageId: MessageId.make("message:blocked-on-thread-wait"),
rootNodeId: null,
activeAttemptId: null,
status: "running",
requestedAt: at,
startedAt: at,
completedAt: null,
checkpointId: null,
contextHandoffId: null,
},
});
const applyToolItem = (id: string, status: "running" | "completed", toolName: string) =>
store.apply({
id: EventId.make(`event:blocked-on-thread-wait:${id}`),
type: "turn-item.updated",
threadId,
runId,
driver,
occurredAt: at,
payload: {
id: waitItemId,
threadId,
runId,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 1,
status,
title: "Wait for a T3 thread",
startedAt: at,
completedAt: status === "completed" ? at : null,
updatedAt: at,
type: "dynamic_tool",
toolName,
input: {},
},
});
const observe = Effect.gen(function* () {
const shell = yield* store.getThreadShell(threadId);
const listed = (yield* store.getShellSnapshot()).threads.find(
(candidate) => candidate.id === threadId,
);
assert.equal(listed?.blockedOnThreadWait, shell?.blockedOnThreadWait);
return shell?.blockedOnThreadWait;
});

assert.equal(yield* observe, false);
yield* applyToolItem("read", "running", "mcp__t3-code__t3_thread_read");
assert.equal(yield* observe, false);
yield* applyToolItem("wait", "running", "mcp__t3-code__t3_thread_wait");
assert.equal(yield* observe, true);
yield* applyToolItem("done", "completed", "mcp__t3-code__t3_thread_wait");
assert.equal(yield* observe, false);
}),
);

it.effect("a pull request watch keeps a finished thread working until it ends", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
Expand Down
17 changes: 16 additions & 1 deletion apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ import {
createOrchestrationV2TurnItemVisibility,
isOrchestrationV2SupersededInterrupt,
} from "@t3tools/shared/orchestrationV2Timeline";
import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
import {
derivePendingBackgroundWork,
isThreadWaitToolItem,
} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -1517,6 +1520,11 @@ export function threadShellFromProjection(
(plan) => plan.kind === "proposed_plan" && plan.status === "active",
),
pendingBackgroundTasks: [...pendingBackgroundTasks],
blockedOnThreadWait:
activeRun !== null &&
projection.turnItems.some(
(item) => item.runId === activeRun.id && isThreadWaitToolItem(item),
),
providerInstanceHistory: providerInstanceHistoryForShell({
threadId: projection.thread.id,
providerThreads: projection.providerThreads,
Expand Down Expand Up @@ -1612,6 +1620,7 @@ type ShellThreadState = {
readonly latestUserAuthoredMessageAt: DateTime.Utc | null;
readonly hasActionableProposedPlan: boolean;
readonly pendingBackgroundTasks: OrchestrationV2ThreadShell["pendingBackgroundTasks"];
readonly blockedOnThreadWait: boolean;
readonly providerInstanceHistory: OrchestrationV2ThreadShell["providerInstanceHistory"];
readonly goal: OrchestrationV2ThreadShell["goal"];
readonly itemCount: number;
Expand Down Expand Up @@ -1778,6 +1787,7 @@ function shellFromState(input: {
latestUserAuthoredMessageAt: input.state.latestUserAuthoredMessageAt,
hasActionableProposedPlan: input.state.hasActionableProposedPlan,
pendingBackgroundTasks: input.state.pendingBackgroundTasks,
blockedOnThreadWait: input.state.blockedOnThreadWait,
providerInstanceHistory: input.state.providerInstanceHistory,
goal: input.state.goal,
itemCount: input.state.itemCount,
Expand Down Expand Up @@ -5736,6 +5746,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
: DateTime.makeUnsafe(row.latest_user_authored_message_at),
hasActionableProposedPlan: row.has_actionable_proposed_plan === 1,
pendingBackgroundTasks,
blockedOnThreadWait:
row.active_run_id !== null &&
(pendingTurnItemsByThreadId.get(thread.id) ?? []).some(
(item) => item.runId === row.active_run_id && isThreadWaitToolItem(item),
),
providerInstanceHistory: providerInstanceHistoryForShell({
threadId: thread.id,
providerThreads: providerThreadsByThreadId.get(thread.id) ?? [],
Expand Down
29 changes: 29 additions & 0 deletions apps/web/src/components/Sidebar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,27 @@ describe("resolveSidebarThreadStatus", () => {
).toBe("working");
});

it("reports waiting for a running turn blocked on another thread", () => {
expect(resolveSidebarThreadStatus({ ...idle, runtime, blockedOnThreadWait: true })).toBe(
"waiting",
);
expect(
resolveSidebarThreadStatus({
...idle,
runtime: { ...runtime, status: "starting" as const },
blockedOnThreadWait: true,
}),
).toBe("working");
expect(
resolveSidebarThreadStatus({
...idle,
hasPendingApprovals: true,
runtime,
blockedOnThreadWait: true,
}),
).toBe("approval");
});

it("keeps usage-limit stops Limited and visible until the thread recovers", () => {
const limited = {
...runtime,
Expand Down Expand Up @@ -1295,6 +1316,14 @@ describe("resolveThreadStatusPill", () => {
});
});

it("shows waiting for a running turn blocked on another thread", () => {
expect(
resolveThreadStatusPill({
thread: { ...baseThread, blockedOnThreadWait: true },
}),
).toMatchObject({ label: "Waiting", pulse: false });
});

it("keeps an active turn working when background tasks are also present", () => {
expect(
resolveThreadStatusPill({
Expand Down
27 changes: 20 additions & 7 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,11 @@ type ThreadStatusInput = Pick<
| "interactionMode"
| "latestRun"
| "runtime"
> & {
lastVisitedAt?: string | null | undefined;
pendingBackgroundTasks?: SidebarThreadSummary["pendingBackgroundTasks"] | undefined;
};
> &
Partial<Pick<SidebarThreadSummary, "blockedOnThreadWait">> & {
lastVisitedAt?: string | null | undefined;
pendingBackgroundTasks?: SidebarThreadSummary["pendingBackgroundTasks"] | undefined;
};

export interface ThreadJumpHintVisibilityController {
sync: (shouldShow: boolean) => void;
Expand Down Expand Up @@ -978,7 +979,8 @@ export function shouldRecedeSidebarThread(input: {
type SidebarThreadStatusInput = Pick<
SidebarThreadSummary,
"hasPendingApprovals" | "hasPendingUserInput" | "runtime"
>;
> &
Partial<Pick<SidebarThreadSummary, "blockedOnThreadWait">>;

export function resolveSidebarThreadStatus(thread: SidebarThreadStatusInput): SidebarThreadStatus {
if (thread.hasPendingApprovals) {
Expand All @@ -987,6 +989,11 @@ export function resolveSidebarThreadStatus(thread: SidebarThreadStatusInput): Si
if (thread.hasPendingUserInput) {
return "input";
}
// A turn blocked in a call that waits for another thread does nothing until
// that thread finishes, so it reads as Waiting, not Working.
if (thread.runtime?.status === "running" && thread.blockedOnThreadWait === true) {
return "waiting";
}
if (
thread.runtime !== null &&
["preparing", "queued", "starting", "running", "waiting"].includes(thread.runtime.status)
Expand Down Expand Up @@ -1199,7 +1206,10 @@ export function resolveThreadStatusPill(input: {
};
}

if (thread.runtime?.status === "running" || thread.runtime?.status === "waiting") {
if (
thread.runtime?.status === "waiting" ||
(thread.runtime?.status === "running" && thread.blockedOnThreadWait !== true)
) {
return {
label: "Working",
colorClass: "text-sky-600 dark:text-sky-300/80",
Expand All @@ -1221,7 +1231,10 @@ export function resolveThreadStatusPill(input: {
};
}

if (backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])) {
if (
(thread.runtime?.status === "running" && thread.blockedOnThreadWait === true) ||
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? [])
) {
return {
label: "Waiting",
colorClass: "text-sidebar-muted-foreground",
Expand Down
3 changes: 3 additions & 0 deletions packages/client-runtime/src/state/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export interface EnvironmentThreadShell {
readonly pendingBackgroundTasks: ReadonlyArray<
NonNullable<OrchestrationV2ThreadShell["pendingBackgroundTasks"]>[number]
>;
/** The running turn is blocked in a call that waits for another T3 thread. */
readonly blockedOnThreadWait?: boolean;
/** Provider instances that have owned the root conversation, oldest first. */
readonly providerInstanceHistory: ReadonlyArray<ProviderInstanceId>;
/** Native `/goal` on the active provider thread. */
Expand Down Expand Up @@ -261,6 +263,7 @@ export function presentThreadShell(
hasPendingUserInput: thread.pendingRuntimeRequest?.kind === "user_input",
hasActionableProposedPlan: thread.hasActionableProposedPlan,
pendingBackgroundTasks: thread.pendingBackgroundTasks ?? [],
blockedOnThreadWait: thread.blockedOnThreadWait ?? false,
providerInstanceHistory: thread.providerInstanceHistory ?? [],
goal: thread.goal ?? null,
itemCount: thread.itemCount,
Expand Down
7 changes: 7 additions & 0 deletions packages/contracts/src/orchestrationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,13 @@ export const OrchestrationV2ThreadShell = Schema.Struct({
pendingBackgroundTasks: Schema.optional(Schema.Array(OrchestrationV2PendingBackgroundTask)).pipe(
Schema.withDecodingDefault(Effect.succeed([])),
),
/**
* The running turn is blocked inside a call that waits for another T3 thread
* (t3_thread_wait, or delegate_task in wait mode). The agent is idle until
* that thread finishes, so lists show Waiting, not Working. Omitted by
* servers that predate it.
*/
blockedOnThreadWait: Schema.optional(Schema.Boolean),
// Distinct provider instances that have owned a root provider thread here,
// in first-use order, so lists can show where a handed-off thread has been.
// Omitted by servers that predate it; decodes to [].
Expand Down
30 changes: 30 additions & 0 deletions packages/shared/src/orchestrationV2PendingBackgroundWork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { OrchestrationV2PendingBackgroundTask } from "@t3tools/contracts";
import {
backgroundWorkHoldsCompletion,
derivePendingBackgroundWork,
isThreadWaitToolItem,
turnItemUpdateCanEndBackgroundWork,
} from "./orchestrationV2PendingBackgroundWork.ts";

Expand All @@ -19,6 +20,35 @@ describe("turnItemUpdateCanEndBackgroundWork", () => {
});
});

describe("isThreadWaitToolItem", () => {
it.each([
["Claude's prefixed wait", true, "mcp__t3-code__t3_thread_wait", "running", {}],
["a dotted wait", true, "t3-code.t3_thread_wait", "running", {}],
["a bare wait", true, "t3_thread_wait", "waiting", {}],
["a blocking delegation", true, "mcp__t3-code__delegate_task", "running", { mode: "wait" }],
["an async delegation", false, "mcp__t3-code__delegate_task", "running", { mode: "async" }],
["a thread read", false, "mcp__t3-code__t3_thread_read", "running", {}],
["a wait that has not started", false, "mcp__t3-code__t3_thread_wait", "pending", {}],
["a finished wait", false, "mcp__t3-code__t3_thread_wait", "completed", {}],
["an unrelated suffix", false, "my_t3_thread_wait_helper", "running", {}],
["another server's wait", false, "mcp__other__t3_thread_wait", "running", {}],
["a dotted third-party wait", false, "weather.t3_thread_wait", "running", {}],
["another server's delegation", false, "other.delegate_task", "running", { mode: "wait" }],
] as const)("%s: %s", (_case, expected, toolName, status, input) => {
expect(isThreadWaitToolItem({ type: "dynamic_tool", status, toolName, input })).toBe(expected);
});

it("ignores items that are not dynamic tools", () => {
expect(
isThreadWaitToolItem({
type: "command_execution",
status: "running",
toolName: "t3_thread_wait",
}),
).toBe(false);
});
});

describe("backgroundWorkHoldsCompletion", () => {
const task = (
taskId: string,
Expand Down
Loading
Loading