Skip to content

Propagate failure reason from run_failed event to CanonicalRunStatus #148

Description

@williamthorsen

Context

After #130, the complete_run MCP tool emits run_failed events with an optional reason field when a run fails. The RunFailedEvent type and Zod schema in run-core already support this field. However, foldEvents in packages/run-core/src/event-folder.ts handles run_failed events but discards the reason field — CanonicalRunStatus has no corresponding property.

Problem

Downstream consumers (Factory visualization, status adapter) cannot access the failure reason because it is lost during event folding. If a run fails with a reason like "quality gates failed", that context is preserved in the JSONL event log but not surfaced through the canonical run status API.

Considerations

  • CanonicalRunStatus is the single source of truth for run state, defined in packages/run-core/src/types/canonical.ts. Factory re-exports it from packages/factory/src/shared/types/canonical.ts.
  • The status adapter (packages/factory/src/server/adapters/status-adapter.ts) is a re-export from @codeassembly/run-core/parsers and needs no changes.
  • The project uses exactOptionalPropertyTypes, so the field should be reason: string | undefined (always present, nullable) rather than reason?: string (possibly absent), matching the pattern of completedAt.
  • The Factory event-folder test file mirrors the run-core test file; both must be updated.

Proposed solution

  1. Add reason: string | undefined to CanonicalRunStatus in packages/run-core/src/types/canonical.ts
  2. Update foldEvents in packages/run-core/src/event-folder.ts to initialize reason: undefined and extract reason from run_failed events
  3. Display the failure reason in the Factory StatusBar (packages/factory/src/client/components/StatusBar.tsx) when present
  4. Add/update tests in both run-core and factory packages

Acceptance criteria

Must have

  • CanonicalRunStatus includes a reason: string | undefined field
  • foldEvents extracts reason from run_failed events and includes it in the returned status
  • foldEvents initializes reason to undefined for new runs and non-failure completions
  • Existing tests continue to pass
  • New tests verify reason propagation (with and without reason)

Should have

  • Factory StatusBar displays the failure reason when available
  • StatusBar tests cover both present and absent reason

Nice to have

  • Factory visualization shows failure reason on hover or in a tooltip

Metadata

Metadata

Labels

featureAdded or improved external functionalityscope:mcp

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions