Skip to content

Commit 3ff5b3c

Browse files
fix(web): a workflow thread opens with its phases and agents instead of its script (#20857)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 91f8c35 commit 3ff5b3c

14 files changed

Lines changed: 1080 additions & 5 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment";
1919
import type { HandoffEndpoints } from "@t3tools/client-runtime/handoff";
2020
import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message";
2121
import { canForkProjectedAssistantItem } from "@t3tools/client-runtime/state/thread-workflows";
22+
import type { WorkflowOpening } from "@t3tools/client-runtime/state/subagentRuntime";
2223
import {
2324
type OrchestrationMessageContext,
2425
ThreadId,
@@ -217,6 +218,7 @@ import { composerDocumentAttachmentRecord } from "../../lib/composerContext";
217218
import * as Option from "effect/Option";
218219
import { appAtomRegistry } from "../../state/atom-registry";
219220
import { environmentThreadShells, threadEnvironment } from "../../state/threads";
221+
import { ThreadWorkflowOpening, useWorkflowOpening } from "./thread-workflow-opening";
220222
import { useAtomCommand } from "../../state/use-atom-command";
221223
import { useV2ItemSupport } from "../../state/v2-item-support";
222224
import {
@@ -1005,6 +1007,7 @@ function renderFeedEntry(
10051007
readonly markdownContentWidth: number;
10061008
readonly contentWidth: number;
10071009
readonly threadTitle: string;
1010+
readonly workflowOpening: WorkflowOpening | null;
10081011
},
10091012
) {
10101013
const entry = info.item;
@@ -1152,6 +1155,15 @@ function renderFeedEntry(
11521155
!assistantTurnStillInProgress &&
11531156
!message.streaming;
11541157

1158+
if (isUser && props.workflowOpening?.messageId === message.id) {
1159+
return (
1160+
<ThreadWorkflowOpening
1161+
environmentId={props.environmentId}
1162+
threadId={props.threadId}
1163+
opening={props.workflowOpening}
1164+
/>
1165+
);
1166+
}
11551167
if (isUser) {
11561168
const enterAnimated = isFreshTimestamp(message.createdAt);
11571169
const intentBadge = resolveUserMessageIntentBadge(message.inputIntent);
@@ -1661,6 +1673,7 @@ function ThreadFeedPlaceholder(props: {
16611673
}
16621674

16631675
export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
1676+
const workflowOpening = useWorkflowOpening(props.environmentId, props.threadId);
16641677
const deviceType = useDeviceType();
16651678
const duoColumns = useNativeWorkspaceColumnsSupported() && isDuoDevice(deviceType);
16661679
const openPullRequest = useOpenPullRequest();
@@ -2010,6 +2023,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
20102023
themeAppearance,
20112024
userBubbleColor,
20122025
viewportWidth,
2026+
workflowOpening,
20132027
}),
20142028
[
20152029
props.worktreeSetup,
@@ -2026,6 +2040,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
20262040
themeAppearance,
20272041
userBubbleColor,
20282042
viewportWidth,
2043+
workflowOpening,
20292044
],
20302045
);
20312046
const reportHeaderMaterialVisibility = useCallback(
@@ -2476,6 +2491,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
24762491
threadTitle: props.threadTitle,
24772492
skills: props.skills,
24782493
workspaceRoot: props.workspaceRoot,
2494+
workflowOpening,
24792495
})}
24802496
{props.worktreeSetup && info.index === setupAnchorIndex ? (
24812497
<WorktreeSetupCard key={props.threadId} {...props.worktreeSetup} />
@@ -2525,6 +2541,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
25252541
props.threadTitle,
25262542
props.skills,
25272543
props.workspaceRoot,
2544+
workflowOpening,
25282545
renderMarkdownImage,
25292546
renderViewedImage,
25302547
renderReasoning,
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import { StackActions, useNavigation } from "@react-navigation/native";
3+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4+
import {
5+
resolveWorkflowOpening,
6+
workflowOpeningHeadline,
7+
workflowOpeningParentThreadId,
8+
type RuntimeSubagent,
9+
} from "@t3tools/client-runtime/state/subagentRuntime";
10+
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
11+
import { useState } from "react";
12+
import { Pressable, View } from "react-native";
13+
14+
import { AppText as Text } from "../../components/ui/AppText";
15+
import { environmentThreadDetails } from "../../state/threads";
16+
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
17+
import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";
18+
import { SubagentRow } from "./SubagentRow";
19+
import { WorkLogBlock } from "./work-log-layout";
20+
21+
// A script longer than this many lines shows its head until opened.
22+
const FOLDED_SCRIPT_LINES = 10;
23+
24+
/** The workflow opening for a thread, or null; the parent's detail loads only for a workflow. */
25+
export function useWorkflowOpening(environmentId: EnvironmentId, threadId: ThreadId) {
26+
const threadAtom = environmentThreadDetails.threadAtom(scopeThreadRef(environmentId, threadId));
27+
// Selected as a primitive so an ordinary thread's updates never change the result.
28+
const parentId = useAtomValue(threadAtom, (thread) =>
29+
thread === null
30+
? null
31+
: workflowOpeningParentThreadId({
32+
thread: thread.projection.thread,
33+
messages: thread.projection.messages,
34+
}),
35+
);
36+
const projection = useAtomValue(threadAtom, (thread) =>
37+
parentId === null ? null : (thread?.projection ?? null),
38+
);
39+
const parentSubagents = useAtomValue(
40+
environmentThreadDetails.threadAtom(scopeThreadRef(environmentId, parentId ?? threadId)),
41+
(parent) => (parentId === null ? null : (parent?.projection.subagents ?? null)),
42+
);
43+
return projection === null
44+
? null
45+
: resolveWorkflowOpening({
46+
thread: projection.thread,
47+
messages: projection.messages,
48+
parentSubagents,
49+
});
50+
}
51+
52+
function memberRowSubagent(
53+
member: RuntimeSubagent,
54+
opening: WorkflowOpeningValue,
55+
coordinatorThreadId: ThreadId,
56+
) {
57+
return {
58+
// A member's parent thread is the coordinator's thread, the one showing this opening.
59+
threadId: coordinatorThreadId,
60+
childThreadId: member.childThreadId as ThreadId | null,
61+
model: member.model,
62+
driver: opening.driver!,
63+
providerInstanceId: opening.providerInstanceId!,
64+
title: member.title,
65+
prompt: member.progress ?? member.title,
66+
status: member.status,
67+
progress: undefined,
68+
result: member.result ?? member.error,
69+
};
70+
}
71+
72+
type WorkflowOpeningValue = NonNullable<ReturnType<typeof useWorkflowOpening>>;
73+
74+
/** Replaces a workflow coordinator's script message: the plan, its agents, then the script. */
75+
export function ThreadWorkflowOpening(props: {
76+
readonly environmentId: EnvironmentId;
77+
readonly threadId: ThreadId;
78+
readonly opening: WorkflowOpeningValue;
79+
}) {
80+
const navigation = useNavigation();
81+
const { opening } = props;
82+
const headline = workflowOpeningHeadline(opening);
83+
const canListMembers = opening.driver !== null && opening.providerInstanceId !== null;
84+
const phases = (opening.group?.phases ?? []).filter((phase) => phase.members.length > 0);
85+
const unphased = opening.group?.unphasedMembers ?? [];
86+
const renderMember = (member: RuntimeSubagent) => {
87+
const threadId = member.childThreadId;
88+
return (
89+
<Pressable
90+
key={member.id}
91+
accessibilityRole={threadId === null ? undefined : "link"}
92+
accessibilityHint={threadId === null ? undefined : "Opens this agent's thread"}
93+
disabled={threadId === null}
94+
onPress={() => {
95+
if (threadId !== null)
96+
navigation.dispatch(
97+
StackActions.push("Thread", {
98+
environmentId: String(props.environmentId),
99+
threadId,
100+
}),
101+
);
102+
}}
103+
className="rounded-lg px-3 py-3 active:bg-subtle"
104+
>
105+
<SubagentRow
106+
environmentId={props.environmentId}
107+
subagent={memberRowSubagent(member, opening, props.threadId)}
108+
elapsed={null}
109+
/>
110+
</Pressable>
111+
);
112+
};
113+
return (
114+
<WorkLogBlock>
115+
<View className="mb-5 gap-3">
116+
<View className="flex-row items-start gap-2">
117+
<View className="min-w-0 flex-1 gap-0.5">
118+
<Text className="font-t3-medium text-2xs text-foreground-muted">Workflow started</Text>
119+
<Text numberOfLines={1} className="font-t3-medium text-sm text-foreground">
120+
{headline.title}
121+
</Text>
122+
{headline.description ? (
123+
<Text className="text-xs text-foreground-muted">{headline.description}</Text>
124+
) : null}
125+
</View>
126+
{headline.agents ? (
127+
<Text className="shrink-0 text-2xs tabular-nums text-foreground-muted">
128+
{headline.agents}
129+
</Text>
130+
) : null}
131+
</View>
132+
{canListMembers
133+
? phases.map((phase) => (
134+
<View key={phase.index} className="gap-px">
135+
<Text className="px-1 pt-1 font-t3-medium text-2xs text-foreground-muted">
136+
{phase.title} · {phase.settledCount}/{phase.members.length}
137+
</Text>
138+
{phase.members.map(renderMember)}
139+
</View>
140+
))
141+
: null}
142+
{canListMembers ? unphased.map(renderMember) : null}
143+
<WorkflowScript script={opening.script} />
144+
</View>
145+
</WorkLogBlock>
146+
);
147+
}
148+
149+
/** The script as highlighted code; a long one shows its head until opened. */
150+
function WorkflowScript(props: { readonly script: string }) {
151+
const [expanded, setExpanded] = useState(false);
152+
const { themeAppearance } = useAppearancePreferences();
153+
const script = props.script.trimEnd();
154+
const lines = useMarkdownCodeHighlight({
155+
code: script,
156+
language: "javascript",
157+
theme: themeAppearance,
158+
enabled: true,
159+
});
160+
const lineCount = script.split("\n").length;
161+
const canFold = lineCount > FOLDED_SCRIPT_LINES;
162+
const shownLines = canFold && !expanded ? FOLDED_SCRIPT_LINES : lineCount;
163+
// Tokens are keyed by their offset into the script, like the work log's REPL code.
164+
let offset = 0;
165+
const highlighted = lines?.slice(0, shownLines).flatMap((line, lineIndex) => {
166+
const separator = lineIndex > 0 ? "\n" : "";
167+
offset += separator.length;
168+
return [
169+
separator,
170+
...line.map((token) => {
171+
const start = offset;
172+
offset += token.content.length;
173+
return (
174+
<Text key={start} style={token.color ? { color: token.color } : undefined}>
175+
{token.content}
176+
</Text>
177+
);
178+
}),
179+
];
180+
});
181+
return (
182+
<View className="gap-1">
183+
<View className="rounded-lg bg-subtle px-3 py-2.5">
184+
<Text selectable className="font-mono text-2xs leading-normal text-foreground">
185+
{highlighted ?? script.split("\n").slice(0, shownLines).join("\n")}
186+
</Text>
187+
</View>
188+
{canFold ? (
189+
<Pressable
190+
accessibilityRole="button"
191+
accessibilityState={{ expanded }}
192+
onPress={() => setExpanded((value) => !value)}
193+
className="self-start py-1 active:opacity-70"
194+
>
195+
<Text className="font-t3-medium text-xs text-foreground-muted">
196+
{expanded ? "Show less" : "Show full script"}
197+
</Text>
198+
</Pressable>
199+
) : null}
200+
</View>
201+
);
202+
}

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11663,6 +11663,95 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1166311663
),
1166411664
);
1166511665

11666+
it.effect("marks a workflow coordinator's script prompt and leaves a subagent brief plain", () =>
11667+
Effect.scoped(
11668+
Effect.gen(function* () {
11669+
const harness = yield* makeWorkflowHarness;
11670+
const script =
11671+
'export const meta = { name: "probe-wf", description: "Run the alpha workflow" };';
11672+
yield* harness.runtime.startTurn(
11673+
makeClaudeTestTurnInput({
11674+
threadId: harness.threadId,
11675+
providerThread: harness.providerThread,
11676+
now: yield* DateTime.now,
11677+
attemptId: RunAttemptId.make("attempt-workflow-script-prompt"),
11678+
text: "Run the workflow and an auditor.",
11679+
attachments: [],
11680+
}),
11681+
);
11682+
yield* harness.offerAndWait(workflowToolUse);
11683+
yield* harness.offerAndWait(claudeSdkFrame({ ...workflowTaskStarted, prompt: script }));
11684+
yield* harness.offerAndWait(workflowLaunchAck(harness.transcriptDir));
11685+
yield* harness.offerAndWait(
11686+
workflowSnapshot({ uuid: "00000000-0000-4000-8000-000000001289", state: "start" }),
11687+
);
11688+
yield* harness.offerAndWait(
11689+
claudeSdkFrame({
11690+
type: "system",
11691+
subtype: "task_started",
11692+
task_id: "task-plain-auditor",
11693+
description: "Audit recent commits",
11694+
task_type: "local_agent",
11695+
prompt: "Audit the last five commits.",
11696+
uuid: "00000000-0000-4000-8000-000000001290",
11697+
session_id: WAKE_NATIVE_SESSION,
11698+
}),
11699+
);
11700+
yield* awaitUntil(
11701+
() =>
11702+
workflowCoordinatorEvents(harness.events).length > 0 &&
11703+
harness.events.some(
11704+
(event) =>
11705+
event.type === "subagent.updated" &&
11706+
event.subagent.nativeTaskRef?.nativeId === "task-plain-auditor",
11707+
),
11708+
"workflow coordinator and auditor projected",
11709+
);
11710+
const subagentPrompt = (nativeTaskId: string) => {
11711+
const subagent = harness.events.flatMap((event) =>
11712+
event.type === "subagent.updated" &&
11713+
event.subagent.nativeTaskRef?.nativeId === nativeTaskId
11714+
? [event.subagent]
11715+
: [],
11716+
)[0];
11717+
assert.isDefined(subagent);
11718+
const message = threadMessages(harness.events, subagent?.childThreadId).find(
11719+
(event) => event.message.role === "user",
11720+
)?.message;
11721+
const turnItem = harness.events.findLast(
11722+
(event) =>
11723+
event.type === "turn_item.updated" &&
11724+
event.turnItem.type === "user_message" &&
11725+
event.turnItem.messageId === message?.id,
11726+
);
11727+
return {
11728+
text: message?.text,
11729+
messageMark: message?.workflowScript,
11730+
itemMark:
11731+
turnItem?.type === "turn_item.updated" && turnItem.turnItem.type === "user_message"
11732+
? turnItem.turnItem.workflowScript
11733+
: undefined,
11734+
};
11735+
};
11736+
assert.deepEqual(subagentPrompt(WORKFLOW_TASK_ID), {
11737+
text: script,
11738+
messageMark: true,
11739+
itemMark: true,
11740+
});
11741+
// A brief is marked as not a script, so its text is never read as one.
11742+
assert.deepEqual(subagentPrompt("task-plain-auditor"), {
11743+
text: "Audit the last five commits.",
11744+
messageMark: false,
11745+
itemMark: false,
11746+
});
11747+
}).pipe(
11748+
Effect.provide(
11749+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
11750+
),
11751+
),
11752+
),
11753+
);
11754+
1166611755
it.effect("scopes workflow member artifacts to the provider thread", () =>
1166711756
Effect.scoped(
1166811757
Effect.gen(function* () {

0 commit comments

Comments
 (0)