Skip to content

Commit 658a4a7

Browse files
fix(clients): a thread comes back when its subagent needs approval
A delegated subagent waiting on an approval left its parent in the Working section with no alert, because subagent rows are hidden and only the subagent's own shell carried the request. Threads above a live subagent now carry what it waits on: the Working section returns the thread, the sidebar and mobile list show the approval or question, web alerts fire on the parent, a snoozed parent wakes, and the parent's banner covers approvals as well as questions.
1 parent 348f217 commit 658a4a7

11 files changed

Lines changed: 268 additions & 54 deletions

File tree

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import type { ComposerTextPaste } from "../../native/T3ComposerEditor.types";
77
import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
88
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
99
import type { HandoffEndpoints } from "@t3tools/client-runtime/handoff";
10-
import type {
11-
CodexFeedbackSubmission,
12-
EnvironmentThreadStatus,
10+
import {
11+
childThreadInputsTitle,
12+
type CodexFeedbackSubmission,
13+
type EnvironmentThreadStatus,
1314
} from "@t3tools/client-runtime/state/threads";
1415
import { useKeyboardChatComposerInset, useKeyboardScrollToEnd } from "@legendapp/list/keyboard";
1516
import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills";
@@ -523,11 +524,8 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
523524
if (childInput && contentPresentationKind === "ready") {
524525
return {
525526
kind: "child-input",
526-
label:
527-
childThreadInputs.length === 1
528-
? "Subagent needs input"
529-
: `${childThreadInputs.length} subagents need input`,
530-
accessibilityLabel: `Open question from ${childInput.title}`,
527+
label: childThreadInputsTitle(childThreadInputs),
528+
accessibilityLabel: `Open ${childInput.hasPendingApprovals ? "approval" : "question"} from ${childInput.title}`,
531529
onPress: () => {
532530
Keyboard.dismiss();
533531
navigation.navigate("Thread", {

‎apps/mobile/src/features/threads/threadListV2.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,15 @@ export function threadHasUnseenCompletion(
183183
}
184184

185185
export function resolveThreadListV2Status(
186-
thread: Pick<EnvironmentThreadShell, "hasPendingApprovals" | "hasPendingUserInput" | "runtime">,
186+
thread: Pick<
187+
EnvironmentThreadShell,
188+
"hasPendingApprovals" | "hasPendingUserInput" | "pendingSubagentRequest" | "runtime"
189+
>,
187190
): ThreadListV2Status {
188-
if (thread.hasPendingApprovals) {
191+
if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") {
189192
return "approval";
190193
}
191-
if (thread.hasPendingUserInput) {
194+
if (thread.hasPendingUserInput || thread.pendingSubagentRequest === "user_input") {
192195
return "input";
193196
}
194197
if (

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import {
9595
import { readPastedComposerContext } from "./composerInlineTokenPaste";
9696
import { isPasteAsTextShortcut } from "@t3tools/client-runtime/text-paste";
9797
import { effectiveSnoozed, threadWokeAt } from "@t3tools/client-runtime/state/thread-settled";
98+
import { childThreadInputsTitle } from "@t3tools/client-runtime/state/threads";
9899
import { useAcknowledgeThreadWoke, useThreadActions } from "../hooks/useThreadActions";
99100
import {
100101
deriveProviderSubagentStatus,
@@ -311,6 +312,7 @@ import {
311312
ChevronDownIcon,
312313
DownloadIcon,
313314
GitBranchIcon,
315+
ShieldQuestionIcon,
314316
TargetIcon,
315317
WifiOffIcon,
316318
} from "lucide-react";
@@ -7465,11 +7467,8 @@ export default function ChatView(props: ChatViewProps) {
74657467
id: `child-input:${first.id}`,
74667468
variant: "info",
74677469
priority: "activity",
7468-
icon: <MessageCircleIcon />,
7469-
title:
7470-
childThreadInputs.length === 1
7471-
? "Subagent needs input"
7472-
: `${childThreadInputs.length} subagents need input`,
7470+
icon: first.hasPendingApprovals ? <ShieldQuestionIcon /> : <MessageCircleIcon />,
7471+
title: childThreadInputsTitle(childThreadInputs),
74737472
description: childThreadInputs.map((child, index) => (
74747473
<Fragment key={child.id}>
74757474
{index > 0 ? " " : null}
@@ -7483,7 +7482,7 @@ export default function ChatView(props: ChatViewProps) {
74837482
)),
74847483
actions: (
74857484
<Button size="xs" variant="ghost" onClick={() => onOpenRelatedThread(first.id)}>
7486-
Open question
7485+
{first.hasPendingApprovals ? "Open approval" : "Open question"}
74877486
</Button>
74887487
),
74897488
};

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ type ThreadStatusInput = Pick<
652652
| "hasActionableProposedPlan"
653653
| "hasPendingApprovals"
654654
| "hasPendingUserInput"
655+
| "pendingSubagentRequest"
655656
| "interactionMode"
656657
| "latestRun"
657658
| "runtime"
@@ -977,14 +978,14 @@ export function shouldRecedeSidebarThread(input: {
977978

978979
type SidebarThreadStatusInput = Pick<
979980
SidebarThreadSummary,
980-
"hasPendingApprovals" | "hasPendingUserInput" | "runtime"
981+
"hasPendingApprovals" | "hasPendingUserInput" | "pendingSubagentRequest" | "runtime"
981982
>;
982983

983984
export function resolveSidebarThreadStatus(thread: SidebarThreadStatusInput): SidebarThreadStatus {
984-
if (thread.hasPendingApprovals) {
985+
if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") {
985986
return "approval";
986987
}
987-
if (thread.hasPendingUserInput) {
988+
if (thread.hasPendingUserInput || thread.pendingSubagentRequest === "user_input") {
988989
return "input";
989990
}
990991
if (
@@ -1181,7 +1182,7 @@ export function resolveThreadStatusPill(input: {
11811182
}): ThreadStatusPill | null {
11821183
const { thread } = input;
11831184

1184-
if (thread.hasPendingApprovals) {
1185+
if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") {
11851186
return {
11861187
label: "Pending Approval",
11871188
colorClass: "text-amber-600 dark:text-amber-300/90",
@@ -1190,7 +1191,7 @@ export function resolveThreadStatusPill(input: {
11901191
};
11911192
}
11921193

1193-
if (thread.hasPendingUserInput) {
1194+
if (thread.hasPendingUserInput || thread.pendingSubagentRequest === "user_input") {
11941195
return {
11951196
label: "Awaiting Input",
11961197
colorClass: "text-indigo-600 dark:text-indigo-300/90",

‎apps/web/src/components/ThreadNotificationCoordinator.test.tsx‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const state = vi.hoisted(() => ({
2121
limited: false,
2222
subagent: false,
2323
background: [] as Array<{ taskId: string; kind: "command" | "monitor" }>,
24+
delegatedChild: null as "running" | "approval" | null,
2425
add: vi.fn(
2526
(_toast: { title: string; description: string; actionProps: { onClick: () => void } }) =>
2627
"toast-1",
@@ -87,10 +88,33 @@ function mockThreadShell() {
8788
};
8889
}
8990

91+
// A delegated child changes while its parent's shell object stays the same.
92+
let stableParent: ReturnType<typeof mockThreadShell> | null = null;
93+
function mockThreads() {
94+
if (state.delegatedChild === null) return [mockThreadShell()];
95+
stableParent ??= mockThreadShell();
96+
const child = {
97+
...mockThreadShell(),
98+
id: "child-1",
99+
title: "Review the login form",
100+
creationSource: "mcp",
101+
lineage: {
102+
rootThreadId: "thread-1",
103+
parentThreadId: "thread-1",
104+
relationshipToParent: "subagent",
105+
},
106+
pendingRuntimeRequest:
107+
state.delegatedChild === "approval"
108+
? { id: "request-2", kind: "command", createdAt: SHELL_NOW }
109+
: null,
110+
};
111+
return [stableParent, child];
112+
}
113+
90114
vi.mock("@effect/atom-react", () => ({
91115
useAtomValue: () => ({
92116
status: state.live ? "live" : "disconnected",
93-
snapshot: Option.some({ threads: [mockThreadShell()] }),
117+
snapshot: Option.some({ threads: mockThreads() }),
94118
}),
95119
}));
96120
vi.mock("@tanstack/react-router", () => ({
@@ -154,7 +178,9 @@ beforeEach(() => {
154178
limited: false,
155179
subagent: false,
156180
background: [],
181+
delegatedChild: null,
157182
});
183+
stableParent = null;
158184
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
159185
vi.stubGlobal("window", new EventTarget());
160186
vi.stubGlobal("document", {
@@ -188,6 +214,19 @@ describe("thread notifications", () => {
188214
expect(state.notification).not.toHaveBeenCalled();
189215
});
190216

217+
it("alerts on the parent when its delegated subagent asks for approval", async () => {
218+
state.mode = "notifications-and-sound";
219+
state.delegatedChild = "running";
220+
await render();
221+
state.delegatedChild = "approval";
222+
await render();
223+
await render();
224+
expect(state.add).toHaveBeenCalledTimes(1);
225+
expect(state.add).toHaveBeenLastCalledWith(
226+
expect.objectContaining({ title: "Approval needed", description: "Fix the login form" }),
227+
);
228+
});
229+
191230
it("alerts once with system alerts off and opens the completed thread", async () => {
192231
await render();
193232
await complete();

‎apps/web/src/components/ThreadNotificationCoordinator.tsx‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { presentThreadShell } from "@t3tools/client-runtime/state/models";
1+
import {
2+
type PendingSubagentRequest,
3+
presentThreadShell,
4+
} from "@t3tools/client-runtime/state/models";
5+
import { pendingSubagentRequestsByThread } from "@t3tools/client-runtime/state/threads";
26
import { useAtomValue } from "@effect/atom-react";
37
import { useNavigate, useParams } from "@tanstack/react-router";
48
import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
@@ -90,6 +94,7 @@ export function ThreadNotificationCoordinator() {
9094

9195
interface NotificationState {
9296
readonly raw: OrchestrationV2ThreadShell;
97+
readonly subagentRequest: PendingSubagentRequest | null;
9398
readonly attention: string | null;
9499
readonly completion: number | null;
95100
}
@@ -122,15 +127,18 @@ function EnvironmentNotifications({
122127
return;
123128
}
124129
const next = new Map<ThreadId, NotificationState>();
130+
// Subagents are skipped below, so their requests notify through the thread above them.
131+
const subagentRequests = pendingSubagentRequestsByThread(threads);
125132
for (const rawThread of threads) {
126133
if (rawThread.lineage.relationshipToParent === "subagent") continue;
127134
const prior = previous.current.get(rawThread.id);
135+
const subagentRequest = subagentRequests.get(rawThread.id) ?? null;
128136
// The same object cannot produce a new notification.
129-
if (prior?.raw === rawThread) {
137+
if (prior?.raw === rawThread && prior.subagentRequest === subagentRequest) {
130138
next.set(rawThread.id, prior);
131139
continue;
132140
}
133-
const thread = presentThreadShell(environmentId, rawThread);
141+
const thread = presentThreadShell(environmentId, rawThread, subagentRequest);
134142
let status = resolveSidebarThreadStatus(thread);
135143
if (status === "ready" && thread.latestRun?.status === "failed") status = "failed";
136144
const attention =
@@ -145,7 +153,7 @@ function EnvironmentNotifications({
145153
Number.isFinite(completedAt)
146154
? completedAt
147155
: (prior?.completion ?? null);
148-
next.set(thread.id, { raw: rawThread, attention, completion });
156+
next.set(thread.id, { raw: rawThread, subagentRequest, attention, completion });
149157
if (!prior || thread.archivedAt !== null) continue;
150158
const kind =
151159
attention && attention !== prior.attention

‎packages/client-runtime/src/state/models.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export interface EnvironmentThreadShell {
109109
readonly latestUserAuthoredMessageAt?: string | null;
110110
readonly hasPendingApprovals: boolean;
111111
readonly hasPendingUserInput: boolean;
112+
/** What a live subagent below this thread waits on the user for. Subagent
113+
rows are hidden, so the threads above them carry it. */
114+
readonly pendingSubagentRequest?: PendingSubagentRequest | null;
112115
readonly hasActionableProposedPlan: boolean;
113116
readonly pendingBackgroundTasks: ReadonlyArray<
114117
NonNullable<OrchestrationV2ThreadShell["pendingBackgroundTasks"]>[number]
@@ -207,9 +210,12 @@ export function scopeProject(
207210
return { ...project, environmentId };
208211
}
209212

213+
export type PendingSubagentRequest = "approval" | "user_input";
214+
210215
export function presentThreadShell(
211216
environmentId: EnvironmentId,
212217
thread: OrchestrationV2ThreadShell,
218+
pendingSubagentRequest: PendingSubagentRequest | null = null,
213219
): EnvironmentThreadShell {
214220
const updatedAt = iso(thread.updatedAt);
215221
const latestRun =
@@ -259,6 +265,7 @@ export function presentThreadShell(
259265
thread.pendingRuntimeRequest.kind !== "user_input" &&
260266
thread.pendingRuntimeRequest.kind !== "auth_refresh",
261267
hasPendingUserInput: thread.pendingRuntimeRequest?.kind === "user_input",
268+
pendingSubagentRequest,
262269
hasActionableProposedPlan: thread.hasActionableProposedPlan,
263270
pendingBackgroundTasks: thread.pendingBackgroundTasks ?? [],
264271
providerInstanceHistory: thread.providerInstanceHistory ?? [],

‎packages/client-runtime/src/state/threadInbox.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ type WorkingThreadInput = Pick<
99
| "hasActionableProposedPlan"
1010
| "hasPendingApprovals"
1111
| "hasPendingUserInput"
12+
| "pendingSubagentRequest"
1213
| "interactionMode"
1314
| "latestRun"
1415
| "runtime"
1516
>;
1617

1718
/** Threads busy with work that does not need the user fold into the Working
1819
section: a running run, or one stopped with background work that will wake
19-
it. Approvals, questions, plan prompts, and failures stay in the inbox. */
20+
it. Approvals, questions (its own or a subagent's), plan prompts, and
21+
failures stay in the inbox. */
2022
export function isThreadWorking(thread: WorkingThreadInput): boolean {
2123
if (thread.hasPendingApprovals || thread.hasPendingUserInput) return false;
24+
if (thread.pendingSubagentRequest != null) return false;
2225
if (!threadRuntimeIsActive(thread.runtime) && thread.runtime?.status !== "idle") return false;
2326
// A plan prompt outranks lingering background work: the user has to act on it.
2427
const run = thread.latestRun;

‎packages/client-runtime/src/state/threadSettled.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @effect-diagnostics globalDate:off -- UI snooze presets use local calendar boundaries and Intl labels.
22
import * as DateTime from "effect/DateTime";
33

4+
import type { PendingSubagentRequest } from "./models.ts";
5+
46
interface SettlementRunLike {
57
readonly turnId?: unknown;
68
readonly assistantMessageId?: unknown;
@@ -90,18 +92,20 @@ export interface ThreadSnoozeShell extends QueuedThreadShell {
9092
readonly snoozedAt?: string | null;
9193
readonly hasPendingApprovals: boolean;
9294
readonly hasPendingUserInput: boolean;
95+
readonly pendingSubagentRequest?: PendingSubagentRequest | null;
9396
}
9497

9598
/**
9699
* A snoozed thread "raises its hand" when something happens that outranks
97-
* the user's snooze: the agent is blocked on them (approval / user input),
98-
* the session failed, or a run completed after the snooze was set — the
99-
* v1 taste of event-based snooze ("something happened" wakes early).
100-
* Raising a hand never clears the server-side snooze fields; it only stops
101-
* the thread from classifying as snoozed.
100+
* the user's snooze: the agent or one of its subagents is blocked on them
101+
* (approval / user input), the session failed, or a run completed after the
102+
* snooze was set — the v1 taste of event-based snooze ("something happened"
103+
* wakes early). Raising a hand never clears the server-side snooze fields; it
104+
* only stops the thread from classifying as snoozed.
102105
*/
103106
export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean {
104107
if (shell.hasPendingApprovals || shell.hasPendingUserInput) return true;
108+
if (shell.pendingSubagentRequest != null) return true;
105109
const runtime = shell.runtime ?? shell.session ?? null;
106110
const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
107111
// Only a FRESH failure raises the hand: a thread snoozed while already

0 commit comments

Comments
 (0)