feat(orchestration-v2): add subagent observability data model (1/5) #4626
t3code/codex-turn-mapping fromrefs/pull/4626/head·opened View on GitHubFirst of five stacked PRs replacing #4551, which bundled the whole subagent-observability feature into one ~3,100-line change. Splitting it out so each piece can actually be reviewed; the seams between these pieces are where the bugs were hiding.
Scope: data model, storage, and migration only. Nothing produces the new data yet. Adapters are given neutral defaults so every subagent row stays valid. Populating them for real is 2/5, and reuse semantics are 3/5.
What's here#
Contracts gain a reusable subagent identity — per-activation records (SubagentActivationId, OrchestrationV2SubagentActivation), cumulative usage, a role, recent activity, and workflow grouping. A subagent can also now rest at idle between activations.
Migration 043 backfills existing rows so old events stay readable.
The status mapping is the important part#
A subagent status is not a turn item status — idle has no timeline equivalent. The two are bridged by an explicit Record<subagent status, turn item status> rather than a copy, which makes the mapping total: adding a subagent status without giving it a timeline meaning is a compile error.
This is not hypothetical. An earlier iteration copied the raw status through, which wrote a turn-item.updated event that its own schema could not decode. Nothing failed at write time — but the projection rebuild runs at startup and died on that stored event, so the server became permanently unbootable with no in-app recovery. Adding idle in this PR immediately trips the new compile error at CodexAdapterV2.ts, which is the guard doing its job.
Notes for review#
- Envelope attribution change:
ProviderEventIngestorno longer replaces an explicit nullrunId/nodeIdwith the ambient run. Activations legitimately have no run id. The envelope is persistence metadata only (routing reads payload fields), but this does change stored metadata for existing event types that already pass explicit nulls. - Migration risk path: the one genuinely risky case is a partially-migrated row, where already-present JSON round-trips through
COALESCE. A test pins that structured values keep their JSON subtype rather than being rewritten as quoted strings. - A database bricked by the earlier broken build is not repaired by this PR — turn items still correctly reject
idle. Only relevant if that build ran anywhere beyond local dev.
Testing#
Typecheck clean and lint clean across server, contracts, client-runtime, and web.
- server 1,676 passing
- contracts 223 passing
- client-runtime 481 passing
- web 1,498 passing
Also booted a server on a fresh state directory and confirmed migration 043 applies and the app starts.
Reviewed by an independent agent pass before opening; its findings on helper scope and the missing mixed-row migration test are folded in.
Note
Medium Risk Touches event-sourced projections, a new migration with JSON backfill, and envelope attribution semantics; mistakes could corrupt stored events or startup rebuilds, though tests target the risky migration and status-mapping paths.
Overview
Introduces the orchestration v2 subagent observability contracts and persistence layer ahead of adapter/UI work. Subagents gain reusable identity fields (kind, role, usage, activationCount, workflow, recentActivity, etc.), a new resting idle status, and separate OrchestrationV2SubagentActivation records with a subagent-activation.updated domain event and SQLite projection table (schema version 3).
Migration 043 backfills legacy subagent JSON and creates activation storage; tests cover partial backfills and projection rebuild cleanup of stale activation rows.
Provider adapters and the orchestrator only wire neutral defaults and minimal lifecycle hooks (e.g. delegated-task spawn/terminal activation events); real population is deferred. Codex now maps subagent status to turn-item status via orchestrationV2SubagentStatusAsTurnItemStatus so idle cannot be written raw onto timeline rows.
ProviderEventIngestor stops substituting ambient runId/nodeId when the payload explicitly passes null, so detached activations and messages keep correct envelope metadata.
Note
Add subagent activation observability data model to orchestration-v2#
- Introduces
OrchestrationV2SubagentActivationas a new schema with a brandedSubagentActivationId, tracking status, usage, ordinals, and timestamps per activation lifecycle. - Extends
OrchestrationV2Subagentwith new fields:kind,role,usage,currentActivationId,activationCount,workflow,workflowMembership, andrecentActivity; legacy records decode with sensible defaults. - Adds
subagent-activation.updatedto the domain event union and wires routing, ingestion, projection storage, and in-memory reducer to handle it end-to-end. - Adds migration 43 (043_OrchestrationV2SubagentObservability.ts) which creates the
orchestration_v2_projection_subagent_activationstable and backfills missing subagent payload fields. - All provider adapters (ACP, Claude, Codex, Cursor, OpenCode) now emit the new subagent metadata fields;
CodexAdapterV2also maps subagent status throughorchestrationV2SubagentStatusAsTurnItemStatusto avoid emitting undecodable turn item statuses. - Behavioral Change: projection schema version bumped from 2 to 3; projection rebuild now clears the activations table before replaying events.
📊 Macroscope summarized 877ef38. 15 files reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted
🗂️ Filtered Issues#
No issues evaluated.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -360,3 +360,4 @@360360 }361361 case "subagent.updated":362362 return [ownsRun(event.subagent.runId) || ownsChildThread(event.subagent.threadId), state];363+ case "subagent_activation.updated":
🟡 Medium
orchestration-v2/RunExecutionService.ts:363routeProviderEventsilently dropssubagent_activation.updatedevents whoseactivation.runIdisnulland whoseactivation.threadIdis the root thread.ownsRun(null)returnsfalse(it compares againstinput.runId), andownsChildThread(input.threadId)also returnsfalse(it excludes the root thread), so the routing decision is[false, state]and the stream filters the event out beforeProviderEventIngestorever persists it. The activation schema permitsnullrun IDs, so these root-thread activations are lost. Consider accepting the event whenactivation.threadId === input.threadIdregardless of whetherrunIdis null.🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/orchestration-v2/RunExecutionService.ts around line 363: `routeProviderEvent` silently drops `subagent_activation.updated` events whose `activation.runId` is `null` and whose `activation.threadId` is the root thread. `ownsRun(null)` returns `false` (it compares against `input.runId`), and `ownsChildThread(input.threadId)` also returns `false` (it excludes the root thread), so the routing decision is `[false, state]` and the stream filters the event out before `ProviderEventIngestor` ever persists it. The activation schema permits `null` run IDs, so these root-thread activations are lost. Consider accepting the event when `activation.threadId === input.threadId` regardless of whether `runId` is null. Approvability#
Verdict: Needs human review
1 blocking correctness issue found. This PR introduces a new subagent observability feature with database schema changes, new domain types, and cross-cutting adapter modifications. An unresolved review comment also identifies a potential event-dropping bug. Both the feature scope and the open concern warrant human review.
You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Superseded by #4768 — same commit, same content. This PR's head branch lived on a fork, which meant it could not be the base of a stacked PR and pushes had to go to a different remote than the rest of the stack. #4768 uses the upstream
subagent-obs/01-contractsbranch so all five PRs push the same way.shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
4893569f-21c4-4b41-ad65-002d3d7be096You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share