feat(contracts): thread agent snapshot schemas for subagent observability

ThreadAgentSnapshot + agent.snapshot activity payload, typed task usage,
task.updated runtime event, and agent-linkage fields on task payloads.
Includes the reviewed HTML plans.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-07-21 19:59:36 -07:00
1 parent 6f34ad3e87
commit f49c1a97ca
5 files changed
+2874 -2

No files matched your search

File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+1
View File
@@ -18,6 +18,7 @@ export * from "./git.ts";
export * from "./vcs.ts";
export * from "./sourceControl.ts";
export * from "./orchestration.ts";
export * from "./threadAgents.ts";
export * from "./editor.ts";
export * from "./project.ts";
export * from "./filesystem.ts";
+76 -2
View File
@@ -176,6 +176,7 @@ const ProviderRuntimeEventType = Schema.Literals([
"user-input.resolved",
"task.started",
"task.progress",
"task.updated",
"task.completed",
"hook.started",
"hook.progress",
@@ -226,6 +227,7 @@ const UserInputRequestedType = Schema.Literal("user-input.requested");
const UserInputResolvedType = Schema.Literal("user-input.resolved");
const TaskStartedType = Schema.Literal("task.started");
const TaskProgressType = Schema.Literal("task.progress");
const TaskUpdatedType = Schema.Literal("task.updated");
const TaskCompletedType = Schema.Literal("task.completed");
const HookStartedType = Schema.Literal("hook.started");
const HookProgressType = Schema.Literal("hook.progress");
@@ -459,10 +461,56 @@ const UserInputResolvedPayload = Schema.Struct({
});
export type UserInputResolvedPayload = typeof UserInputResolvedPayload.Type;
/**
* Typed task usage rollup. `usage` on task events was historically
* `Schema.Unknown` (raw Claude SDK shape); emitters now normalize to this.
*/
export const RuntimeTaskUsage = Schema.Struct({
totalTokens: NonNegativeInt,
inputTokens: Schema.optional(NonNegativeInt),
cachedInputTokens: Schema.optional(NonNegativeInt),
outputTokens: Schema.optional(NonNegativeInt),
reasoningOutputTokens: Schema.optional(NonNegativeInt),
toolUses: Schema.optional(NonNegativeInt),
durationMs: Schema.optional(NonNegativeInt),
});
export type RuntimeTaskUsage = typeof RuntimeTaskUsage.Type;
/**
* Agent-linkage fields shared by the task lifecycle payloads. Optional and
* additive: pre-existing emitters that only set taskId/description remain
* valid.
*
* `timelineBypass: true` marks events synthesized purely to feed the agent
* reducer (e.g. Codex child-thread activity): ingestion must skip the
* activity-timeline projection for them, otherwise child fleets would spray
* a row per tool call into the transcript.
*/
const TaskAgentLinkage = {
name: Schema.optional(TrimmedNonEmptyStringSchema),
agentType: Schema.optional(TrimmedNonEmptyStringSchema),
model: Schema.optional(TrimmedNonEmptyStringSchema),
toolUseId: Schema.optional(TrimmedNonEmptyStringSchema),
parentTaskId: Schema.optional(RuntimeTaskId),
workflowName: Schema.optional(TrimmedNonEmptyStringSchema),
phaseIndex: Schema.optional(NonNegativeInt),
phaseTitle: Schema.optional(TrimmedNonEmptyStringSchema),
phases: Schema.optional(
Schema.Array(Schema.Struct({ index: NonNegativeInt, title: TrimmedNonEmptyStringSchema })),
),
attempt: Schema.optional(NonNegativeInt),
scriptPath: Schema.optional(TrimmedNonEmptyStringSchema),
runId: Schema.optional(TrimmedNonEmptyStringSchema),
outputFile: Schema.optional(TrimmedNonEmptyStringSchema),
timelineBypass: Schema.optional(Schema.Boolean),
} as const;
const TaskStartedPayload = Schema.Struct({
taskId: RuntimeTaskId,
description: Schema.optional(TrimmedNonEmptyStringSchema),
taskType: Schema.optional(TrimmedNonEmptyStringSchema),
prompt: Schema.optional(TrimmedNonEmptyStringSchema),
...TaskAgentLinkage,
});
export type TaskStartedPayload = typeof TaskStartedPayload.Type;
@@ -470,16 +518,34 @@ const TaskProgressPayload = Schema.Struct({
taskId: RuntimeTaskId,
description: TrimmedNonEmptyStringSchema,
summary: Schema.optional(TrimmedNonEmptyStringSchema),
usage: Schema.optional(Schema.Unknown),
usage: Schema.optional(Schema.Union([RuntimeTaskUsage, Schema.Unknown])),
lastToolName: Schema.optional(TrimmedNonEmptyStringSchema),
...TaskAgentLinkage,
});
export type TaskProgressPayload = typeof TaskProgressPayload.Type;
/**
* Non-terminal state patch (Claude `task_updated`): running/paused flips,
* backgrounding, error text, end time.
*/
const TaskUpdatedPayload = Schema.Struct({
taskId: RuntimeTaskId,
status: Schema.optional(
Schema.Literals(["pending", "running", "completed", "failed", "killed", "paused"]),
),
endTime: Schema.optional(IsoDateTime),
isBackgrounded: Schema.optional(Schema.Boolean),
errorMessage: Schema.optional(TrimmedNonEmptyStringSchema),
...TaskAgentLinkage,
});
export type TaskUpdatedPayload = typeof TaskUpdatedPayload.Type;
const TaskCompletedPayload = Schema.Struct({
taskId: RuntimeTaskId,
status: Schema.Literals(["completed", "failed", "stopped"]),
summary: Schema.optional(TrimmedNonEmptyStringSchema),
usage: Schema.optional(Schema.Unknown),
usage: Schema.optional(Schema.Union([RuntimeTaskUsage, Schema.Unknown])),
...TaskAgentLinkage,
});
export type TaskCompletedPayload = typeof TaskCompletedPayload.Type;
@@ -835,6 +901,13 @@ const ProviderRuntimeTaskProgressEvent = Schema.Struct({
});
export type ProviderRuntimeTaskProgressEvent = typeof ProviderRuntimeTaskProgressEvent.Type;
const ProviderRuntimeTaskUpdatedEvent = Schema.Struct({
...ProviderRuntimeEventBase.fields,
type: TaskUpdatedType,
payload: TaskUpdatedPayload,
});
export type ProviderRuntimeTaskUpdatedEvent = typeof ProviderRuntimeTaskUpdatedEvent.Type;
const ProviderRuntimeTaskCompletedEvent = Schema.Struct({
...ProviderRuntimeEventBase.fields,
type: TaskCompletedType,
@@ -995,6 +1068,7 @@ export const ProviderRuntimeEventV2 = Schema.Union([
ProviderRuntimeUserInputResolvedEvent,
ProviderRuntimeTaskStartedEvent,
ProviderRuntimeTaskProgressEvent,
ProviderRuntimeTaskUpdatedEvent,
ProviderRuntimeTaskCompletedEvent,
ProviderRuntimeHookStartedEvent,
ProviderRuntimeHookProgressEvent,
+138
View File
@@ -0,0 +1,138 @@
import * as Schema from "effect/Schema";
import {
ApprovalRequestId,
IsoDateTime,
NonNegativeInt,
TrimmedNonEmptyString,
TurnId,
} from "./baseSchemas.ts";
import { ProviderDriverKind } from "./providerInstance.ts";
/**
* `ThreadAgentSnapshot` — one delegated worker (subagent, workflow, workflow
* agent, background shell, monitor) observed on a thread.
*
* The snapshot models a stable *identity* with rollups across its activations:
* a Codex collab agent is a durable, re-activatable child thread (follow-ups
* re-run it), while a Claude task is a one-shot execution. `status: "idle"`
* captures the Codex "finished a run but resumable" state, which is distinct
* from the terminal `completed`.
*
* Transport: the full per-thread roster is carried latest-wins in the payload
* of an `agent.snapshot` thread activity (see `ThreadAgentsActivityPayload`).
* There is no dedicated wire event — the activity channel's open `kind` string
* and unknown payload are the compatibility surface, mirroring how
* `context-window.updated` ships token snapshots today.
*/
export const ThreadAgentStatus = Schema.Literals([
// Not yet running (workflow agent queued behind a phase, Codex pendingInit).
"pending",
"running",
// Blocked on an approval or user-input request.
"waiting",
// Finished a run but resumable (Codex agents between activations; Claude paused).
"idle",
"completed",
"failed",
"stopped",
]);
export type ThreadAgentStatus = typeof ThreadAgentStatus.Type;
export const THREAD_AGENT_TERMINAL_STATUSES: ReadonlySet<ThreadAgentStatus> = new Set([
"completed",
"failed",
"stopped",
]);
export const ThreadAgentKind = Schema.Literals([
"subagent",
"workflow",
"workflow_agent",
"shell",
"monitor",
"other",
]);
export type ThreadAgentKind = typeof ThreadAgentKind.Type;
/** Cumulative usage across all of an agent's activations. */
export const ThreadAgentUsage = Schema.Struct({
totalTokens: NonNegativeInt,
inputTokens: Schema.optional(NonNegativeInt),
cachedInputTokens: Schema.optional(NonNegativeInt),
outputTokens: Schema.optional(NonNegativeInt),
reasoningOutputTokens: Schema.optional(NonNegativeInt),
toolUses: Schema.optional(NonNegativeInt),
});
export type ThreadAgentUsage = typeof ThreadAgentUsage.Type;
export const ThreadAgentActivityEntry = Schema.Struct({
at: IsoDateTime,
summary: TrimmedNonEmptyString,
});
export type ThreadAgentActivityEntry = typeof ThreadAgentActivityEntry.Type;
/** Ordered workflow phase, present only on `kind: "workflow"` snapshots. */
export const ThreadAgentWorkflowPhase = Schema.Struct({
index: NonNegativeInt,
title: TrimmedNonEmptyString,
});
export type ThreadAgentWorkflowPhase = typeof ThreadAgentWorkflowPhase.Type;
export const THREAD_AGENT_RECENT_ACTIVITY_LIMIT = 6;
export const ThreadAgentSnapshot = Schema.Struct({
// Stable identity: Claude task_id, Codex child thread id. Provider
// discriminates the id space so cross-provider collisions are impossible.
agentId: TrimmedNonEmptyString,
provider: ProviderDriverKind,
kind: ThreadAgentKind,
name: TrimmedNonEmptyString,
// Provider agent type: Claude subagent_type ("Explore"), Codex agent_role
// ("explorer").
agentType: Schema.optional(TrimmedNonEmptyString),
model: Schema.optional(TrimmedNonEmptyString),
status: ThreadAgentStatus,
currentActivity: Schema.optional(TrimmedNonEmptyString),
lastToolName: Schema.optional(TrimmedNonEmptyString),
usage: Schema.optional(ThreadAgentUsage),
firstStartedAt: IsoDateTime,
lastActivityAt: IsoDateTime,
// Cleared when a re-activation (Codex follow-up) starts a new run.
endedAt: Schema.optional(IsoDateTime),
// Codex follow-ups and Claude workflow retry attempts.
activationCount: NonNegativeInt,
spawnTurnId: Schema.optional(TurnId),
lastTurnId: Schema.optional(TurnId),
parentAgentId: Schema.optional(TrimmedNonEmptyString),
// Workflow phase membership. Index is authoritative; title is display-only
// (titles can repeat across phases).
phaseIndex: Schema.optional(NonNegativeInt),
phaseTitle: Schema.optional(TrimmedNonEmptyString),
// kind === "workflow" only.
phases: Schema.optional(Schema.Array(ThreadAgentWorkflowPhase)),
scriptPath: Schema.optional(TrimmedNonEmptyString),
runId: Schema.optional(TrimmedNonEmptyString),
// Set while waiting on an approval so the UI can deep-link to the request.
approvalRequestId: Schema.optional(ApprovalRequestId),
// Claude transcript/output path; the drill-in RPC reads it on demand.
outputFile: Schema.optional(TrimmedNonEmptyString),
resultSummary: Schema.optional(TrimmedNonEmptyString),
errorMessage: Schema.optional(TrimmedNonEmptyString),
recentActivity: Schema.Array(ThreadAgentActivityEntry),
// Staleness watermark: how fresh this record is, independent of transport
// state. Clients compare against their sync watermark.
updatedAt: IsoDateTime,
});
export type ThreadAgentSnapshot = typeof ThreadAgentSnapshot.Type;
export const THREAD_AGENTS_ACTIVITY_KIND = "agent.snapshot";
/**
* Payload of an `agent.snapshot` thread activity: the complete latest-wins
* roster for the thread. Decoded tolerantly on the client — a row that fails
* to decode is ignored, never fatal.
*/
export const ThreadAgentsActivityPayload = Schema.Struct({
agents: Schema.Array(ThreadAgentSnapshot),
});
export type ThreadAgentsActivityPayload = typeof ThreadAgentsActivityPayload.Type;