diff --git a/apps/server/src/orchestration-v2/ShellStream.test.ts b/apps/server/src/orchestration-v2/ShellStream.test.ts index aa01ec9947f37..75ee4dc7a28a2 100644 --- a/apps/server/src/orchestration-v2/ShellStream.test.ts +++ b/apps/server/src/orchestration-v2/ShellStream.test.ts @@ -73,6 +73,20 @@ describe("subagent shell events", () => { expect([tool("running"), tool("completed"), tool("failed")]).toEqual([false, true, true]); }); + it("publishes a subagent's secret or link card so its parent hears it waits on you", () => { + const subagents = new Set(["child"]); + const card = (type: string, status: string) => + shouldPublishShellApplicationEvent( + stored("child", "turn-item.updated", { type, status }), + subagents, + ); + expect([ + card("secret_request", "waiting"), + card("secret_request", "completed"), + card("link_request", "waiting"), + ]).toEqual([true, true, true]); + }); + it("tracks subagents created after the subscription starts", () => { const subagents = new Set(); const created = { diff --git a/apps/server/src/orchestration-v2/ShellStream.ts b/apps/server/src/orchestration-v2/ShellStream.ts index 3e9b74ebfb685..6aff302916224 100644 --- a/apps/server/src/orchestration-v2/ShellStream.ts +++ b/apps/server/src/orchestration-v2/ShellStream.ts @@ -440,9 +440,14 @@ export function shouldPublishShellApplicationEvent( const LATEST_ACTION_TYPES: ReadonlySet = new Set(LATEST_ACTION_ITEM_TYPES); -/** A finished tool moves the child's latest action; streaming tool output does not. */ +/** + * A secret or link card is the child's only record of waiting on you, so its parent must hear it. + * A finished tool moves the child's latest action; streaming tool output does not. + */ const changesChildShell = (item: { readonly type: string; readonly status: string }) => - LATEST_ACTION_TYPES.has(item.type) && item.status !== "running" && item.status !== "pending"; + item.type === "secret_request" || + item.type === "link_request" || + (LATEST_ACTION_TYPES.has(item.type) && item.status !== "running" && item.status !== "pending"); /** Shell updates refetch an aggregate; drop transcript bodies before retaining an event. */ export function toShellApplicationEvent(stored: ApplicationStoredEvent): ShellApplicationEvent { diff --git a/apps/web/src/components/chat/ChildThreadApprovals.browser.test.tsx b/apps/web/src/components/chat/ChildThreadApprovals.browser.test.tsx index ef136a1760137..fd0ae87a20ec7 100644 --- a/apps/web/src/components/chat/ChildThreadApprovals.browser.test.tsx +++ b/apps/web/src/components/chat/ChildThreadApprovals.browser.test.tsx @@ -10,8 +10,10 @@ const state = vi.hoisted(() => ({ respond: vi.fn(), approvalsAtom: null as unkno vi.mock("../../state/session", () => ({ useEnvironmentScope: () => true })); vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => state.respond })); +vi.mock("./SecretRequestCard", () => ({ SecretRequestCard: () => null })); vi.mock("../../state/threads", () => ({ childThreadApprovalsAtom: () => state.approvalsAtom, + childThreadSecretRequestsAtom: () => Atom.make([]), threadEnvironment: { respondToApproval: {} }, })); diff --git a/apps/web/src/components/chat/ChildThreadApprovals.tsx b/apps/web/src/components/chat/ChildThreadApprovals.tsx index 5a7cb3074b4a6..1c69fba07f444 100644 --- a/apps/web/src/components/chat/ChildThreadApprovals.tsx +++ b/apps/web/src/components/chat/ChildThreadApprovals.tsx @@ -8,9 +8,14 @@ import { } from "@t3tools/client-runtime/state/runtime"; import { useState } from "react"; import { useEnvironmentScope } from "../../state/session"; -import { childThreadApprovalsAtom, threadEnvironment } from "../../state/threads"; +import { + childThreadApprovalsAtom, + childThreadSecretRequestsAtom, + threadEnvironment, +} from "../../state/threads"; import { useAtomCommand } from "../../state/use-atom-command"; import { ComposerPendingApprovalActions } from "./ComposerPendingApprovalActions"; +import { SecretRequestCard } from "./SecretRequestCard"; function ChildApproval({ entry }: { entry: ChildThreadApproval }) { const canRespond = useEnvironmentScope(entry.thread.environmentId, AuthOrchestrationOperateScope); @@ -77,9 +82,18 @@ function ChildApproval({ entry }: { entry: ChildThreadApproval }) { export function ChildThreadApprovals({ threadRef }: { threadRef: ScopedThreadRef | null }) { const approvals = useAtomValue(childThreadApprovalsAtom(threadRef)); - if (approvals.length === 0) return null; + const secrets = useAtomValue(childThreadSecretRequestsAtom(threadRef)); + if (approvals.length === 0 && secrets.length === 0) return null; return (
+ {secrets.map((entry) => ( + + ))} {approvals.map((entry) => ( ({ + position: 0, + visibility: "local" as const, + sourceThreadId: childId, + sourceItemId: TurnItemId.make(id), + item: { + id: TurnItemId.make(id), + threadId: childId, + runId: null, + nodeId, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 1, + status: secretStatus === "pending" ? ("waiting" as const) : ("completed" as const), + title: "GITHUB_CLIENT_SECRET", + startedAt: v2Now, + completedAt: null, + updatedAt: v2Now, + type: "secret_request" as const, + label: "GITHUB_CLIENT_SECRET", + reason: "To test the real OAuth flow", + secretStatus, + }, + }); + const items = Atom.family((key: string) => + Atom.make( + key.startsWith(local) ? [card("saved", "saved"), card("asking", "pending")] : [], + ), + ); + const readItems = vi.fn((ref: ScopedThreadRef) => items(threadKey(ref))); + const secretsAtom = createChildThreadSecretRequestsAtom({ + childThreadInputsAtom: () => children, + visibleTurnItemsAtom: readItems, + }); + const unmountSecrets = registry.mount(secretsAtom(parent)); + expect( + registry.get(secretsAtom(parent)).map((entry) => [entry.thread.environmentId, entry.item.id]), + ).toEqual([[local, "asking"]]); + readItems.mockClear(); + registry.set( + children, + registry.get(children).map((child) => ({ ...child, hasPendingUserInput: false })), + ); + expect(registry.get(secretsAtom(parent))).toEqual([]); + expect(readItems).not.toHaveBeenCalled(); + unmountSecrets(); unmount(); registry.dispose(); }); diff --git a/packages/client-runtime/src/state/threadRequests.ts b/packages/client-runtime/src/state/threadRequests.ts index 7c3c44c4caa59..d9f73a7562523 100644 --- a/packages/client-runtime/src/state/threadRequests.ts +++ b/packages/client-runtime/src/state/threadRequests.ts @@ -3,6 +3,7 @@ import type { ScopedThreadRef, ThreadId, OrchestrationV2ThreadProjection, + OrchestrationV2TurnItem, OrchestrationV2RuntimeRequest, OrchestrationV2UserInputQuestion, ProviderApprovalFullAccessOverride, @@ -393,3 +394,53 @@ export function createChildThreadApprovalsAtom(input: { const empty = Atom.make>([]); return (ref: ScopedThreadRef | null) => (ref === null ? empty : family(threadKey(ref))); } + +export interface ChildThreadSecretRequest { + readonly thread: ScopedThreadRef; + readonly title: string; + readonly item: Extract; +} + +/** Waiting secret cards of the child threads under `ref`, so the parent can answer them. */ +export function createChildThreadSecretRequestsAtom(input: { + readonly childThreadInputsAtom: ( + ref: ScopedThreadRef, + ) => Atom.Atom>; + readonly visibleTurnItemsAtom: ( + ref: ScopedThreadRef, + ) => Atom.Atom; +}) { + const family = Atom.family((key: string) => { + let previous: ReadonlyArray = []; + return Atom.make((get) => { + const next = get(input.childThreadInputsAtom(parseThreadKey(key))).flatMap((child) => { + if (!child.hasPendingUserInput) return []; + const thread = { environmentId: child.environmentId, threadId: child.id }; + // Only children waiting on input need a detail subscription. + return get(input.visibleTurnItemsAtom(thread)).flatMap(({ item, visibility }) => + item.type === "secret_request" && + item.secretStatus === "pending" && + visibility === "local" + ? [{ thread, title: child.title, item }] + : [], + ); + }); + if ( + next.length === previous.length && + next.every((entry, index) => { + const old = previous[index]!; + return ( + old.item === entry.item && + old.title === entry.title && + threadKey(old.thread) === threadKey(entry.thread) + ); + }) + ) + return previous; + previous = next; + return next; + }); + }); + const empty = Atom.make>([]); + return (ref: ScopedThreadRef | null) => (ref === null ? empty : family(threadKey(ref))); +}