@prismatic-io/lux 0.0.2-preview.5 → 0.0.2-preview.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/lib/answerers/persona/index.d.ts.map +1 -1
  2. package/lib/answerers/persona/index.js +3 -2
  3. package/lib/answerers/persona/index.js.map +1 -1
  4. package/lib/core/agent-activity.d.ts +49 -0
  5. package/lib/core/agent-activity.d.ts.map +1 -0
  6. package/lib/core/agent-activity.js +206 -0
  7. package/lib/core/agent-activity.js.map +1 -0
  8. package/lib/core/agent-events.d.ts +26 -0
  9. package/lib/core/agent-events.d.ts.map +1 -0
  10. package/lib/core/agent-events.js +13 -0
  11. package/lib/core/agent-events.js.map +1 -0
  12. package/lib/core/events.d.ts +34 -0
  13. package/lib/core/events.d.ts.map +1 -1
  14. package/lib/core/events.js +9 -0
  15. package/lib/core/events.js.map +1 -1
  16. package/lib/core/index.d.ts +2 -0
  17. package/lib/core/index.d.ts.map +1 -1
  18. package/lib/core/index.js +2 -0
  19. package/lib/core/index.js.map +1 -1
  20. package/lib/core/run.d.ts +99 -0
  21. package/lib/core/run.d.ts.map +1 -1
  22. package/lib/core/run.js +3 -0
  23. package/lib/core/run.js.map +1 -1
  24. package/lib/core/wire.d.ts +11 -0
  25. package/lib/core/wire.d.ts.map +1 -1
  26. package/lib/drivers/claude-code/parse-events.d.ts.map +1 -1
  27. package/lib/drivers/claude-code/parse-events.js +73 -8
  28. package/lib/drivers/claude-code/parse-events.js.map +1 -1
  29. package/lib/drivers/codex/app-events.d.ts +9 -2
  30. package/lib/drivers/codex/app-events.d.ts.map +1 -1
  31. package/lib/drivers/codex/app-events.js +93 -12
  32. package/lib/drivers/codex/app-events.js.map +1 -1
  33. package/lib/drivers/codex/index.d.ts.map +1 -1
  34. package/lib/drivers/codex/index.js +10 -14
  35. package/lib/drivers/codex/index.js.map +1 -1
  36. package/lib/index.d.ts +3 -1
  37. package/lib/index.d.ts.map +1 -1
  38. package/lib/index.js +3 -1
  39. package/lib/index.js.map +1 -1
  40. package/lib/orchestrator/compare.d.ts +5 -1
  41. package/lib/orchestrator/compare.d.ts.map +1 -1
  42. package/lib/orchestrator/compare.js +4 -0
  43. package/lib/orchestrator/compare.js.map +1 -1
  44. package/lib/orchestrator/list-runs.d.ts +1 -0
  45. package/lib/orchestrator/list-runs.d.ts.map +1 -1
  46. package/lib/orchestrator/list-runs.js +37 -7
  47. package/lib/orchestrator/list-runs.js.map +1 -1
  48. package/lib/orchestrator/run-execution.d.ts.map +1 -1
  49. package/lib/orchestrator/run-execution.js +7 -3
  50. package/lib/orchestrator/run-execution.js.map +1 -1
  51. package/lib/orchestrator/suite-summary.d.ts.map +1 -1
  52. package/lib/orchestrator/suite-summary.js +23 -0
  53. package/lib/orchestrator/suite-summary.js.map +1 -1
  54. package/lib/previewer/server.d.ts +1 -0
  55. package/lib/previewer/server.d.ts.map +1 -1
  56. package/lib/previewer/server.js +1 -0
  57. package/lib/previewer/server.js.map +1 -1
  58. package/package.json +1 -1
  59. package/skills/lux-answerer/SKILL.md +1 -1
  60. package/src/answerers/persona/index.ts +3 -2
  61. package/src/core/agent-activity.ts +236 -0
  62. package/src/core/agent-events.ts +17 -0
  63. package/src/core/events.ts +11 -0
  64. package/src/core/index.ts +2 -0
  65. package/src/core/run.ts +3 -0
  66. package/src/drivers/claude-code/parse-events.ts +98 -12
  67. package/src/drivers/codex/app-events.ts +116 -16
  68. package/src/drivers/codex/index.ts +16 -14
  69. package/src/index.ts +15 -0
  70. package/src/orchestrator/compare.ts +6 -0
  71. package/src/orchestrator/list-runs.ts +42 -6
  72. package/src/orchestrator/run-execution.ts +7 -2
  73. package/src/orchestrator/suite-summary.ts +65 -0
  74. package/src/previewer/server.ts +2 -0
  75. package/viewer/app.css +53 -0
  76. package/viewer/app.js +6 -1
  77. package/viewer/detail.js +108 -2
@@ -0,0 +1,236 @@
1
+ import { z } from "zod";
2
+ import { ActionProgressPayloadSchema } from "./action-events.js";
3
+ import { AgentCompletedPayloadSchema, AgentSpawnPayloadSchema } from "./agent-events.js";
4
+ import type { AgentAttribution, ReadyState, RunEvent } from "./events.js";
5
+ import { ToolCallPayloadSchema, ToolResultPayloadSchema } from "./tool-events.js";
6
+ import { TokenUsageSchema } from "./usage.js";
7
+
8
+ export const AgentActivitySchema = z.object({
9
+ agent: z.object({
10
+ id: z.string(),
11
+ role: z.enum(["main", "subagent"]),
12
+ parentId: z.exactOptional(z.string()),
13
+ spawnToolUseId: z.exactOptional(z.string()),
14
+ name: z.exactOptional(z.string()),
15
+ model: z.exactOptional(z.string()),
16
+ }),
17
+ startedAt: z.number(),
18
+ endedAt: z.number(),
19
+ durationMs: z.number().nonnegative(),
20
+ status: z.enum(["completed", "failed", "cancelled", "unknown"]),
21
+ eventCount: z.number().int().nonnegative(),
22
+ usage: TokenUsageSchema.extend({ total: z.number().int().nonnegative() }),
23
+ toolCalls: z.object({
24
+ total: z.number().int().nonnegative(),
25
+ succeeded: z.number().int().nonnegative(),
26
+ failed: z.number().int().nonnegative(),
27
+ unresolved: z.number().int().nonnegative(),
28
+ }),
29
+ actions: z.object({
30
+ total: z.number().int().nonnegative(),
31
+ mutations: z.number().int().nonnegative(),
32
+ durableMutations: z.number().int().nonnegative(),
33
+ timeToFirstActionMs: z.exactOptional(z.number().nonnegative()),
34
+ timeToFirstMutationMs: z.exactOptional(z.number().nonnegative()),
35
+ timeToFirstDurableMutationMs: z.exactOptional(z.number().nonnegative()),
36
+ }),
37
+ });
38
+ export type AgentActivity = z.infer<typeof AgentActivitySchema>;
39
+
40
+ const legacyAttribution = (
41
+ event: Extract<RunEvent, { type: "progress" }>,
42
+ ready: ReadyState,
43
+ ): AgentAttribution => {
44
+ const payload = event.payload;
45
+ const parentToolUseId =
46
+ typeof payload === "object" && payload !== null && "parentToolUseId" in payload
47
+ ? payload.parentToolUseId
48
+ : undefined;
49
+ const spawnToolUseId =
50
+ typeof parentToolUseId === "string" && parentToolUseId.length > 0 ? parentToolUseId : undefined;
51
+ return spawnToolUseId
52
+ ? {
53
+ id: `subagent:${spawnToolUseId}`,
54
+ role: "subagent" as const,
55
+ parentId: ready.agentId,
56
+ spawnToolUseId,
57
+ }
58
+ : { id: ready.agentId, role: "main" as const, ...(ready.model ? { model: ready.model } : {}) };
59
+ };
60
+
61
+ type MutableActivity = AgentActivity & { calls: Map<string, "pending" | "succeeded" | "failed"> };
62
+ type ProgressEvent = Extract<RunEvent, { type: "progress" }>;
63
+ type SpawnInfo = { ts: number; name?: string; model?: string };
64
+
65
+ const initialActivity = (agent: AgentAttribution, startedAt: number): MutableActivity => ({
66
+ agent,
67
+ startedAt,
68
+ endedAt: startedAt,
69
+ durationMs: 0,
70
+ status: "unknown",
71
+ eventCount: 0,
72
+ usage: { input: 0, output: 0, cacheRead: 0, cacheCreation: 0, total: 0 },
73
+ toolCalls: { total: 0, succeeded: 0, failed: 0, unresolved: 0 },
74
+ actions: { total: 0, mutations: 0, durableMutations: 0 },
75
+ calls: new Map(),
76
+ });
77
+
78
+ const collectSpawnInfo = (events: RunEvent[]): Map<string, SpawnInfo> => {
79
+ const spawns = new Map<string, SpawnInfo>();
80
+ for (const event of events) {
81
+ if (event.type !== "progress" || event.kind !== "tool-call") continue;
82
+ const call = ToolCallPayloadSchema.safeParse(event.payload).data;
83
+ if (!call) continue;
84
+ const input = z
85
+ .object({
86
+ name: z.string().min(1).optional(),
87
+ subagent_type: z.string().min(1).optional(),
88
+ agent: z.string().min(1).optional(),
89
+ role: z.string().min(1).optional(),
90
+ model: z.string().min(1).optional(),
91
+ })
92
+ .loose()
93
+ .safeParse(call.input).data;
94
+ const name = input?.name ?? input?.subagent_type ?? input?.agent ?? input?.role;
95
+ spawns.set(call.toolUseId, {
96
+ ts: event.ts,
97
+ ...(name ? { name } : {}),
98
+ ...(input?.model ? { model: input.model } : {}),
99
+ });
100
+ }
101
+ return spawns;
102
+ };
103
+
104
+ const applyLifecycle = (
105
+ event: ProgressEvent,
106
+ activities: Map<string, MutableActivity>,
107
+ ): boolean => {
108
+ if (event.kind === "agent-spawn") {
109
+ const spawn = AgentSpawnPayloadSchema.safeParse(event.payload).data;
110
+ if (spawn && !activities.has(spawn.agent.id)) {
111
+ activities.set(spawn.agent.id, initialActivity(spawn.agent, event.ts));
112
+ }
113
+ return true;
114
+ }
115
+ if (event.kind !== "agent-completed") return false;
116
+ const completed = AgentCompletedPayloadSchema.safeParse(event.payload).data;
117
+ const activity = completed ? activities.get(completed.agentId) : undefined;
118
+ if (activity && completed) {
119
+ activity.endedAt = Math.max(activity.endedAt, event.ts);
120
+ activity.durationMs = Math.max(0, activity.endedAt - activity.startedAt);
121
+ activity.status = completed.status;
122
+ }
123
+ return true;
124
+ };
125
+
126
+ const activityFor = (
127
+ event: ProgressEvent,
128
+ ready: ReadyState,
129
+ driverStartedAt: number,
130
+ spawns: Map<string, SpawnInfo>,
131
+ activities: Map<string, MutableActivity>,
132
+ ): MutableActivity => {
133
+ const agent = event.agent ?? legacyAttribution(event, ready);
134
+ const existing = activities.get(agent.id);
135
+ if (existing) return existing;
136
+ const spawn = agent.spawnToolUseId ? spawns.get(agent.spawnToolUseId) : undefined;
137
+ const name = agent.name ?? spawn?.name;
138
+ const model = agent.model ?? spawn?.model;
139
+ const attributedAgent = {
140
+ ...agent,
141
+ ...(name ? { name } : {}),
142
+ ...(model ? { model } : {}),
143
+ };
144
+ const startedAt =
145
+ agent.role === "subagent" && agent.spawnToolUseId
146
+ ? (spawn?.ts ?? event.ts)
147
+ : driverStartedAt || event.ts;
148
+ return activities.get(agent.id) ?? initialActivity(attributedAgent, startedAt);
149
+ };
150
+
151
+ const addAction = (event: ProgressEvent, activity: MutableActivity): void => {
152
+ const action = ActionProgressPayloadSchema.safeParse(event.payload).data;
153
+ if (!action) return;
154
+ const elapsed = Math.max(0, event.ts - activity.startedAt);
155
+ activity.actions.total++;
156
+ activity.actions.timeToFirstActionMs ??= elapsed;
157
+ if (action.category === "mutation" || action.category === "durable-mutation") {
158
+ activity.actions.mutations++;
159
+ activity.actions.timeToFirstMutationMs ??= elapsed;
160
+ }
161
+ if (action.category === "durable-mutation") {
162
+ activity.actions.durableMutations++;
163
+ activity.actions.timeToFirstDurableMutationMs ??= elapsed;
164
+ }
165
+ };
166
+
167
+ const addUsage = (event: ProgressEvent, activity: MutableActivity): void => {
168
+ const usage = TokenUsageSchema.safeParse(event.payload).data;
169
+ if (!usage) return;
170
+ activity.usage.input += usage.input;
171
+ activity.usage.output += usage.output;
172
+ activity.usage.cacheRead += usage.cacheRead;
173
+ activity.usage.cacheCreation += usage.cacheCreation;
174
+ activity.usage.total += usage.input + usage.output + usage.cacheRead + usage.cacheCreation;
175
+ };
176
+
177
+ const addWork = (event: ProgressEvent, activity: MutableActivity): void => {
178
+ activity.eventCount++;
179
+ activity.endedAt = Math.max(activity.endedAt, event.ts);
180
+ activity.durationMs = Math.max(0, activity.endedAt - activity.startedAt);
181
+ if (event.kind === "tool-call") {
182
+ const call = ToolCallPayloadSchema.safeParse(event.payload).data;
183
+ if (call && !activity.calls.has(call.toolUseId)) {
184
+ activity.calls.set(call.toolUseId, "pending");
185
+ activity.toolCalls.total++;
186
+ }
187
+ return;
188
+ }
189
+ if (event.kind === "tool-result") {
190
+ const result = ToolResultPayloadSchema.safeParse(event.payload).data;
191
+ if (result && activity.calls.has(result.toolUseId)) {
192
+ activity.calls.set(result.toolUseId, result.isError ? "failed" : "succeeded");
193
+ }
194
+ return;
195
+ }
196
+ if (event.kind === "action") addAction(event, activity);
197
+ if (event.kind === "usage") addUsage(event, activity);
198
+ };
199
+
200
+ export const summarizeAgentActivity = (events: RunEvent[], ready: ReadyState): AgentActivity[] => {
201
+ const driverStart = events.find(
202
+ (event): event is Extract<RunEvent, { type: "lifecycle" }> =>
203
+ event.type === "lifecycle" && event.stage === "start-driver",
204
+ );
205
+ const driverStartedAt = driverStart?.ts ?? 0;
206
+ const spawns = collectSpawnInfo(events);
207
+ const activities = new Map<string, MutableActivity>();
208
+ for (const event of events) {
209
+ if (event.type !== "progress") continue;
210
+ if (applyLifecycle(event, activities)) continue;
211
+ const activity = activityFor(event, ready, driverStartedAt, spawns, activities);
212
+ addWork(event, activity);
213
+ activities.set(activity.agent.id, activity);
214
+ }
215
+
216
+ if (!activities.has(ready.agentId)) {
217
+ activities.set(
218
+ ready.agentId,
219
+ initialActivity(
220
+ {
221
+ id: ready.agentId,
222
+ role: "main",
223
+ ...(ready.model ? { model: ready.model } : {}),
224
+ },
225
+ driverStartedAt,
226
+ ),
227
+ );
228
+ }
229
+ return [...activities.values()]
230
+ .map(({ calls, ...activity }) => {
231
+ for (const outcome of calls.values())
232
+ activity.toolCalls[outcome === "pending" ? "unresolved" : outcome]++;
233
+ return AgentActivitySchema.parse(activity);
234
+ })
235
+ .sort((a, b) => a.startedAt - b.startedAt || a.agent.id.localeCompare(b.agent.id));
236
+ };
@@ -0,0 +1,17 @@
1
+ import { z } from "zod";
2
+ import { AgentAttributionSchema } from "./events.js";
3
+
4
+ export const AGENT_SPAWN_KIND = "agent-spawn" as const;
5
+ export const AGENT_COMPLETED_KIND = "agent-completed" as const;
6
+
7
+ export const AgentSpawnPayloadSchema = z.object({
8
+ agent: AgentAttributionSchema.extend({ role: z.literal("subagent") }),
9
+ prompt: z.exactOptional(z.string()),
10
+ });
11
+ export type AgentSpawnPayload = z.infer<typeof AgentSpawnPayloadSchema>;
12
+
13
+ export const AgentCompletedPayloadSchema = z.object({
14
+ agentId: z.string().min(1),
15
+ status: z.enum(["completed", "failed", "cancelled", "unknown"]),
16
+ });
17
+ export type AgentCompletedPayload = z.infer<typeof AgentCompletedPayloadSchema>;
@@ -57,11 +57,22 @@ export const AnswerSchema = z.discriminatedUnion("kind", [
57
57
  ]);
58
58
  export type Answer = z.infer<typeof AnswerSchema>;
59
59
 
60
+ export const AgentAttributionSchema = z.object({
61
+ id: z.string().min(1),
62
+ role: z.enum(["main", "subagent"]),
63
+ parentId: z.exactOptional(z.string().min(1)),
64
+ spawnToolUseId: z.exactOptional(z.string().min(1)),
65
+ name: z.exactOptional(z.string().min(1)),
66
+ model: z.exactOptional(z.string().min(1)),
67
+ });
68
+ export type AgentAttribution = z.infer<typeof AgentAttributionSchema>;
69
+
60
70
  export const ProgressSchema = z.object({
61
71
  id: z.string(),
62
72
  kind: z.string(),
63
73
  payload: z.unknown(),
64
74
  ts: z.number(),
75
+ agent: z.exactOptional(AgentAttributionSchema),
65
76
  });
66
77
  export type Progress = z.infer<typeof ProgressSchema>;
67
78
 
package/src/core/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./action-events.js";
2
+ export * from "./agent-activity.js";
3
+ export * from "./agent-events.js";
2
4
  export * from "./annotation.js";
3
5
  export * from "./answerer.js";
4
6
  export * from "./artifact-evidence.js";
package/src/core/run.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { ActionTimingSchema } from "./action-events.js";
3
+ import { AgentActivitySchema } from "./agent-activity.js";
3
4
  import { EvalCaseSchema } from "./case.js";
4
5
  import { ArtifactSchema } from "./driver.js";
5
6
  import { DriverMetricsSchema } from "./driver-metrics.js";
@@ -130,6 +131,8 @@ export const RunMetadataSchema = z.object({
130
131
  phaseDurationMs: z.exactOptional(PhaseDurationsSchema),
131
132
  /** Driver-reported semantic action latency, measured from `start-driver`. */
132
133
  actionTiming: z.exactOptional(ActionTimingSchema),
134
+ /** Per-agent lifecycle and activity derived from the attributed event stream. */
135
+ agentActivity: z.exactOptional(z.array(AgentActivitySchema)),
133
136
  /**
134
137
  * Harness/infrastructure failure that invalidates this run as candidate
135
138
  * evidence. A driver-reported subject failure intentionally leaves this
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import {
3
+ type AgentAttribution,
3
4
  type Interrupt,
4
5
  type ParsedDriverEvent,
5
6
  parseAgentUsage,
@@ -8,6 +9,39 @@ import {
8
9
  toolResultProgress,
9
10
  } from "../../core/index.js";
10
11
 
12
+ const claudeAgent = (parentToolUseId?: string): AgentAttribution =>
13
+ parentToolUseId
14
+ ? {
15
+ id: `claude-code:${parentToolUseId}`,
16
+ role: "subagent",
17
+ parentId: "claude-code",
18
+ spawnToolUseId: parentToolUseId,
19
+ }
20
+ : { id: "claude-code", role: "main" };
21
+
22
+ const isSubagentTool = (name: string): boolean => name === "Agent" || name === "Task";
23
+
24
+ const spawnedClaudeAgent = (
25
+ toolUseId: string,
26
+ input: unknown,
27
+ parentToolUseId?: string,
28
+ ): AgentAttribution => {
29
+ const details =
30
+ typeof input === "object" && input !== null ? (input as Record<string, unknown>) : {};
31
+ const name = [details.name, details.subagent_type, details.agent, details.role].find(
32
+ (value): value is string => typeof value === "string" && value.length > 0,
33
+ );
34
+ const model = typeof details.model === "string" && details.model ? details.model : undefined;
35
+ return {
36
+ id: `claude-code:${toolUseId}`,
37
+ role: "subagent",
38
+ parentId: claudeAgent(parentToolUseId).id,
39
+ spawnToolUseId: toolUseId,
40
+ ...(name ? { name } : {}),
41
+ ...(model ? { model } : {}),
42
+ };
43
+ };
44
+
11
45
  const ContentBlockSchema = z.discriminatedUnion("type", [
12
46
  z.object({ type: z.literal("text"), text: z.string() }),
13
47
  z.object({ type: z.literal("tool_use"), id: z.string(), name: z.string(), input: z.unknown() }),
@@ -88,6 +122,7 @@ type PendingTurn = {
88
122
  asked: boolean;
89
123
  thought: boolean;
90
124
  text?: string;
125
+ agent: AgentAttribution;
91
126
  };
92
127
 
93
128
  type AssistantContent = {
@@ -96,6 +131,7 @@ type AssistantContent = {
96
131
  asked: boolean;
97
132
  thought: boolean;
98
133
  firstText?: string;
134
+ agent: AgentAttribution;
99
135
  };
100
136
 
101
137
  const questionTextOf = (raw: unknown): string =>
@@ -225,6 +261,30 @@ class ClaudeEventParser {
225
261
  if (!block) continue;
226
262
  events.push(block);
227
263
  if (block.tag !== "progress") continue;
264
+ const resultPayload = block.progress.payload as {
265
+ name?: unknown;
266
+ toolUseId?: unknown;
267
+ isError?: unknown;
268
+ };
269
+ if (
270
+ typeof resultPayload.name === "string" &&
271
+ isSubagentTool(resultPayload.name) &&
272
+ typeof resultPayload.toolUseId === "string"
273
+ ) {
274
+ events.push({
275
+ tag: "progress",
276
+ progress: {
277
+ id: `agent-completed-${resultPayload.toolUseId}`,
278
+ kind: "agent-completed",
279
+ payload: {
280
+ agentId: `claude-code:${resultPayload.toolUseId}`,
281
+ status: resultPayload.isError === true ? "failed" : "completed",
282
+ },
283
+ agent: claudeAgent(),
284
+ ts: block.progress.ts,
285
+ },
286
+ });
287
+ }
228
288
  const text = (block.progress.payload as { text?: unknown }).text;
229
289
  const failure = typeof text === "string" ? toolInfrastructureFailure(text) : null;
230
290
  if (failure) {
@@ -244,20 +304,21 @@ class ClaudeEventParser {
244
304
 
245
305
  const toolUseId = block.tool_use_id;
246
306
  const content = block.content ?? [];
307
+ const progress = toolResultProgress(
308
+ this.eventSequence++,
309
+ {
310
+ name: this.toolNames.get(toolUseId) ?? "unknown",
311
+ toolUseId,
312
+ content,
313
+ isError: block.is_error === true,
314
+ text: toolResultText(content),
315
+ ...(parentToolUseId ? { parentToolUseId } : {}),
316
+ },
317
+ Date.now(),
318
+ );
247
319
  return {
248
320
  tag: "progress",
249
- progress: toolResultProgress(
250
- this.eventSequence++,
251
- {
252
- name: this.toolNames.get(toolUseId) ?? "unknown",
253
- toolUseId,
254
- content,
255
- isError: block.is_error === true,
256
- text: toolResultText(content),
257
- ...(parentToolUseId ? { parentToolUseId } : {}),
258
- },
259
- Date.now(),
260
- ),
321
+ progress: { ...progress, agent: claudeAgent(parentToolUseId) },
261
322
  };
262
323
  }
263
324
 
@@ -284,6 +345,7 @@ class ClaudeEventParser {
284
345
  tools: [],
285
346
  asked: false,
286
347
  thought: false,
348
+ agent: claudeAgent(event.parent_tool_use_id),
287
349
  };
288
350
  for (const rawBlock of event.message.content) {
289
351
  const block = ContentBlockSchema.safeParse(rawBlock).data;
@@ -323,6 +385,7 @@ class ClaudeEventParser {
323
385
  kind: "agent-message",
324
386
  payload: { text, ...(parentToolUseId ? { parentToolUseId } : {}) },
325
387
  ts: Date.now(),
388
+ agent: claudeAgent(parentToolUseId),
326
389
  },
327
390
  });
328
391
  }
@@ -354,8 +417,29 @@ class ClaudeEventParser {
354
417
  ...(parentToolUseId ? { parentToolUseId } : {}),
355
418
  },
356
419
  ts: Date.now(),
420
+ agent: claudeAgent(parentToolUseId),
357
421
  },
358
422
  });
423
+ if (isSubagentTool(block.name)) {
424
+ const agent = spawnedClaudeAgent(block.id, block.input, parentToolUseId);
425
+ const details =
426
+ typeof block.input === "object" && block.input !== null
427
+ ? (block.input as Record<string, unknown>)
428
+ : {};
429
+ content.events.push({
430
+ tag: "progress",
431
+ progress: {
432
+ id: `agent-spawn-${block.id}`,
433
+ kind: "agent-spawn",
434
+ payload: {
435
+ agent,
436
+ ...(typeof details.prompt === "string" ? { prompt: details.prompt } : {}),
437
+ },
438
+ agent: claudeAgent(parentToolUseId),
439
+ ts: Date.now(),
440
+ },
441
+ });
442
+ }
359
443
  }
360
444
 
361
445
  private recordTurn(
@@ -376,6 +460,7 @@ class ClaudeEventParser {
376
460
  tools: [],
377
461
  asked: false,
378
462
  thought: false,
463
+ agent: content.agent,
379
464
  };
380
465
  this.pendingTurn.usage = usage;
381
466
  this.pendingTurn.asked ||= content.asked;
@@ -451,6 +536,7 @@ class ClaudeEventParser {
451
536
  kind: "usage",
452
537
  payload: { ...this.pendingTurn.usage, ...(label ? { label } : {}) },
453
538
  ts: Date.now(),
539
+ agent: this.pendingTurn.agent,
454
540
  },
455
541
  };
456
542
  this.pendingTurn = null;
@@ -1,5 +1,7 @@
1
1
  import {
2
+ type AgentAttribution,
2
3
  type ParsedDriverEvent,
4
+ type Progress,
3
5
  type TokenUsage,
4
6
  TURN_GLYPHS,
5
7
  toolResultProgress,
@@ -20,8 +22,98 @@ const resultText = (content: unknown): string => {
20
22
  .join("\n");
21
23
  };
22
24
 
25
+ const agentCompletionStatus = (status: unknown): "completed" | "failed" | "cancelled" => {
26
+ if (status === "failed") return "failed";
27
+ if (status === "cancelled" || status === "interrupted") return "cancelled";
28
+ return "completed";
29
+ };
30
+
31
+ export const appAgentLifecycleProgress = (
32
+ item: RpcRecord,
33
+ phase: "started" | "completed",
34
+ actor?: AgentAttribution,
35
+ ts = Date.now(),
36
+ ): Progress[] => {
37
+ if (item.type !== "collabAgentToolCall" || typeof item.id !== "string") return [];
38
+ const receiverThreadIds = Array.isArray(item.receiverThreadIds)
39
+ ? item.receiverThreadIds.filter((value): value is string => typeof value === "string")
40
+ : [];
41
+ const completionStatus = agentCompletionStatus(item.status);
42
+ return receiverThreadIds.map((receiverThreadId) =>
43
+ phase === "started"
44
+ ? {
45
+ id: `agent-spawn-${item.id}-${receiverThreadId}`,
46
+ kind: "agent-spawn",
47
+ payload: {
48
+ agent: {
49
+ id: receiverThreadId,
50
+ role: "subagent",
51
+ parentId: actor?.id ?? "codex",
52
+ spawnToolUseId: item.id as string,
53
+ ...(typeof item.model === "string" ? { model: item.model } : {}),
54
+ },
55
+ ...(typeof item.prompt === "string" ? { prompt: item.prompt } : {}),
56
+ },
57
+ ...(actor ? { agent: actor } : {}),
58
+ ts,
59
+ }
60
+ : {
61
+ id: `agent-completed-${item.id}-${receiverThreadId}`,
62
+ kind: "agent-completed",
63
+ payload: {
64
+ agentId: receiverThreadId,
65
+ status: completionStatus,
66
+ },
67
+ ...(actor ? { agent: actor } : {}),
68
+ ts,
69
+ },
70
+ );
71
+ };
72
+
23
73
  export type RpcId = string | number;
24
74
  export type RpcRecord = Record<string, unknown>;
75
+
76
+ export class CodexSubagentTracker {
77
+ private readonly spawnToolByThread = new Map<string, string>();
78
+ private readonly parentAgentByThread = new Map<string, string>();
79
+
80
+ record(
81
+ item: RpcRecord | null,
82
+ spawningThreadId: string | undefined,
83
+ mainThreadId: string | null,
84
+ ): void {
85
+ if (item?.type !== "collabAgentToolCall" || typeof item.id !== "string") return;
86
+ const receiverThreadIds = Array.isArray(item.receiverThreadIds)
87
+ ? item.receiverThreadIds.filter((value): value is string => typeof value === "string")
88
+ : [];
89
+ for (const receiverThreadId of receiverThreadIds) {
90
+ this.spawnToolByThread.set(receiverThreadId, item.id);
91
+ if (spawningThreadId) {
92
+ this.parentAgentByThread.set(
93
+ receiverThreadId,
94
+ spawningThreadId === mainThreadId ? "codex" : spawningThreadId,
95
+ );
96
+ }
97
+ }
98
+ }
99
+
100
+ attribution(
101
+ threadId: string | undefined,
102
+ mainThreadId: string | null,
103
+ ): AgentAttribution | undefined {
104
+ if (!threadId) return undefined;
105
+ const spawnToolUseId = this.spawnToolByThread.get(threadId);
106
+ if (!spawnToolUseId) {
107
+ return { id: threadId === mainThreadId ? "codex" : threadId, role: "main" };
108
+ }
109
+ return {
110
+ id: threadId,
111
+ role: "subagent",
112
+ parentId: this.parentAgentByThread.get(threadId) ?? mainThreadId ?? "codex",
113
+ spawnToolUseId,
114
+ };
115
+ }
116
+ }
25
117
  export type AppTurnFacets = {
26
118
  tools: string[];
27
119
  asked: boolean;
@@ -131,8 +223,10 @@ const appItemResultText = (item: RpcRecord, error: RpcRecord | null, content: un
131
223
  export const appItemProgress = (
132
224
  item: RpcRecord,
133
225
  phase: "started" | "completed",
134
- parentToolUseId?: string,
226
+ attribution?: AgentAttribution | string,
135
227
  ): ParsedDriverEvent | null => {
228
+ const agent = typeof attribution === "string" ? undefined : attribution;
229
+ const parentToolUseId = typeof attribution === "string" ? attribution : agent?.spawnToolUseId;
136
230
  const id = typeof item.id === "string" ? item.id : `${String(item.type)}-${Date.now()}`;
137
231
  if (item.type === "agentMessage" && phase === "completed" && typeof item.text === "string") {
138
232
  return {
@@ -142,6 +236,7 @@ export const appItemProgress = (
142
236
  kind: "agent-message",
143
237
  payload: { text: item.text, ...(parentToolUseId ? { parentToolUseId } : {}) },
144
238
  ts: Date.now(),
239
+ ...(agent ? { agent } : {}),
145
240
  },
146
241
  };
147
242
  }
@@ -162,6 +257,7 @@ export const appItemProgress = (
162
257
  ...(parentToolUseId ? { parentToolUseId } : {}),
163
258
  },
164
259
  ts: Date.now(),
260
+ ...(agent ? { agent } : {}),
165
261
  },
166
262
  };
167
263
  }
@@ -184,6 +280,7 @@ export const appItemProgress = (
184
280
  ...(parentToolUseId ? { parentToolUseId } : {}),
185
281
  },
186
282
  ts: Date.now(),
283
+ ...(agent ? { agent } : {}),
187
284
  },
188
285
  };
189
286
  }
@@ -199,20 +296,23 @@ export const appItemProgress = (
199
296
  error !== null;
200
297
  return {
201
298
  tag: "progress",
202
- progress: toolResultProgress(
203
- 0,
204
- {
205
- name: tool,
206
- toolUseId: id,
207
- content,
208
- ...(asRecord(structuredContent)
209
- ? { structuredContent: asRecord(structuredContent) ?? {} }
210
- : {}),
211
- isError,
212
- text: appItemResultText(item, error, content),
213
- ...(parentToolUseId ? { parentToolUseId } : {}),
214
- },
215
- Date.now(),
216
- ),
299
+ progress: {
300
+ ...toolResultProgress(
301
+ 0,
302
+ {
303
+ name: tool,
304
+ toolUseId: id,
305
+ content,
306
+ ...(asRecord(structuredContent)
307
+ ? { structuredContent: asRecord(structuredContent) ?? {} }
308
+ : {}),
309
+ isError,
310
+ text: appItemResultText(item, error, content),
311
+ ...(parentToolUseId ? { parentToolUseId } : {}),
312
+ },
313
+ Date.now(),
314
+ ),
315
+ ...(agent ? { agent } : {}),
316
+ },
217
317
  };
218
318
  };