Skip to content

feat(#504): typed workflow event system for decoupled progress + MCP streaming - #643

Merged
admarble merged 2 commits into
mainfrom
feature/504-arch-typed-workflow-event-system-for-decoupled-pro
May 14, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/504-arch-typed-workflow-event-system-for-decoupled-pro

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

  • Add WorkflowEventEmitter (src/lib/workflow/event-emitter.ts) — a Node EventEmitter wrapper with a compile-time-typed event map covering 8 lifecycle events (run_started, run_completed, phase_started, phase_completed, phase_failed, issue_status_changed, qa_verdict, progress). emit() is wrapped in Promise.allSettled so a slow or throwing listener cannot crash the run.
  • Wire into RunOrchestrator: instantiate per run, expose via getEmitter(), emit at lifecycle points already plumbed through applyProgressEvent plus a run/qa-verdict bracket in executeOneIssue. markDone() drains subscribers to prevent leaks across repeated run() invocations (MCP server use case).
  • Per the 2026-04-09 descope, existing consumers (LogWriter, MetricsWriter, CLI spinners) remain as direct calls; the emitter is opt-in for new subscribers.

Pre-PR AC Verification

Operating on the post-descope AC list (renumbered AC-1 through AC-5 per the 2026-05-14 spec comment).

AC Source Description Status Evidence
AC-1 Original WorkflowEventEmitter class with 8 typed events ✅ event-emitter.ts WorkflowEvents interface; 10 unit tests
AC-2 Original TypeScript-typed events, no third-party pub/sub ✅ Hand-rolled wrapper over node:events (no deps); compile-time safety enforced via keyof WorkflowEvents overloads on on/off/emit
AC-3 Original RunOrchestrator emits at lifecycle points (fire-and-forget) ✅ run-orchestrator.ts:applyProgressEvent (phase start/complete/failed/progress, status transitions) + executeOneIssue (run_started/run_completed/qa_verdict). All emits use void emitter.emit(...) — orchestrator does not await
AC-4 Original Structured payloads (issueNumber, phase, timestamp, duration?, verdict?, error?) ✅ Discriminated payload types in event-emitter.ts; JSON-roundtrip test in both unit + integration suites
AC-5 Original Async listener safety via Promise.allSettled wrapper ✅ WorkflowEventEmitter.emit() wraps every listener in Promise.resolve().then(listener) and resolves through Promise.allSettled; rejections routed to onListenerError and never propagated

Test plan

  • npm run build passes
  • npm run lint passes (no warnings)
  • npx vitest run src/lib/workflow/event-emitter.test.ts — 10/10 pass
  • npx vitest run __tests__/run-orchestrator-events.test.ts — 9/9 pass
  • npx vitest run src/lib/workflow/ __tests__/run-orchestrator.test.ts __tests__/run-orchestrator-resolve.test.ts __tests__/integration/run-orchestrator.integration.test.ts — 907/907 pass (no regressions)
  • npx vitest run __tests__/mcp-progress-notifications.test.ts __tests__/mcp — 47/47 pass

Quality Plan Alignment

Quality Dimension Items Addressed Notes
Error Handling 1/1 Listener errors swallowed via Promise.allSettled + onListenerError (which itself swallows throws)
Test Coverage 2/2 All 5 ACs covered in unit + integration suites
Code Quality 2/2 Hand-rolled types (no third-party dep), JSDoc documents the dual-API coexistence with onProgress
Best Practices 1/1 Verbose-gated logNonFatalWarning for listener errors, mirroring orchestrator convention

Notes

  • Coexistence with onProgress: per the spec's open-question 1, the existing onProgress callback (synchronous TUI render hook) remains in place. The emitter is the async multi-subscriber surface. Consolidating them is an intentional non-goal per the descope.
  • executeBatch not wired: batch-executor.ts:executeBatch is unused in src/ (the orchestrator calls runIssueWithLogging directly). The emitter is threaded through BatchExecutionContext regardless so existing call shape is preserved.
  • MCP wiring deferred: AC-7 (the MCP-server-side subscription wiring) is explicitly future work per the descope. The mechanism is in place — orchestrator.getEmitter().on(...) works today.

Closes #504

…streaming

Add `WorkflowEventEmitter` (`src/lib/workflow/event-emitter.ts`): a Node
`EventEmitter` wrapper with a compile-time-typed event map covering 8
lifecycle events — `run_started`, `run_completed`, `phase_started`,
`phase_completed`, `phase_failed`, `issue_status_changed`, `qa_verdict`,
`progress`. Payloads are JSON-serializable (`{ issueNumber, phase?,
timestamp, duration?, verdict?, error?, ... }`) so MCP / webhook
consumers can serialize them directly. `emit()` is wrapped in
`Promise.allSettled` so a slow or throwing listener cannot crash the run.

Wire into `RunOrchestrator`: instantiate per run, expose via
`getEmitter()`, emit at the lifecycle points already plumbed through
`applyProgressEvent` (phase start/complete/failed/progress, issue status
transitions) plus a run/qa-verdict bracket in `executeOneIssue`.
`markDone()` drains all subscribers so repeated `run()` invocations in
the same process (MCP server) don't leak listeners.

Per the 2026-04-09 descope, existing consumers (LogWriter, MetricsWriter,
CLI spinners) remain as direct calls; the emitter is opt-in for new
subscribers (TUI, MCP server, future webhooks).

Tests: 10 unit cases in `event-emitter.test.ts` covering all 8 events,
JSON roundtrip, and listener isolation; 9 integration cases in
`__tests__/run-orchestrator-events.test.ts` covering run/qa-verdict
bracket, phase-event propagation, status transitions, and slow / throwing
listener safety. All 907 existing workflow tests continue to pass.
… fields

Closes three non-blocking gaps surfaced by /qa on PR #643:

1. run_completed asymmetry — executeOneIssue emitted run_started before
   awaiting runIssueWithLogging but emitted run_completed only on the
   success path. If a future code path causes the awaited call to throw,
   subscribers would see an unbalanced start without a matching
   completion. The body is now wrapped in try { ... } finally { emit }
   so the bracket events stay symmetric; success is reported from the
   captured result, defaulting to false when the await rejects.

2. Dead emitter fields on BatchExecutionContext / IssueExecutionContext —
   threaded through but never read by batch-executor or phase-executor.
   Removed the optional fields (and the matching assignments inside the
   orchestrator). The orchestrator continues to emit lifecycle events
   from its own this.emitter; the WorkflowEventEmitter type-barrel
   re-exports stay so external getEmitter() consumers can still import
   from the workflow types module. When a real consumer needs to emit
   from inside phase-executor (e.g., MCP wiring picks up qa_verdict at
   the source), the field can return with a real motivating use case.

3. snake_case event-name nit — added a one-line note to the
   WorkflowEvents doc block clarifying that snake_case is the public
   MCP / webhook contract and should not be "normalized" to camelCase.

Added one integration test: when runIssueWithLogging rejects, the
orchestrator still emits run_completed with success:false, and the
synthetic failure IssueResult surfaces in the parallel-execute path.

Verified: build + lint clean, 20/20 targeted event tests, full suite
3357/3357 pass.
@admarble
admarble merged commit b4e8814 into main May 14, 2026
5 checks passed
admarble added a commit that referenced this pull request Sep 9, 2026
… fields

Closes three non-blocking gaps surfaced by /qa on PR #643:

1. run_completed asymmetry — executeOneIssue emitted run_started before
   awaiting runIssueWithLogging but emitted run_completed only on the
   success path. If a future code path causes the awaited call to throw,
   subscribers would see an unbalanced start without a matching
   completion. The body is now wrapped in try { ... } finally { emit }
   so the bracket events stay symmetric; success is reported from the
   captured result, defaulting to false when the await rejects.

2. Dead emitter fields on BatchExecutionContext / IssueExecutionContext —
   threaded through but never read by batch-executor or phase-executor.
   Removed the optional fields (and the matching assignments inside the
   orchestrator). The orchestrator continues to emit lifecycle events
   from its own this.emitter; the WorkflowEventEmitter type-barrel
   re-exports stay so external getEmitter() consumers can still import
   from the workflow types module. When a real consumer needs to emit
   from inside phase-executor (e.g., MCP wiring picks up qa_verdict at
   the source), the field can return with a real motivating use case.

3. snake_case event-name nit — added a one-line note to the
   WorkflowEvents doc block clarifying that snake_case is the public
   MCP / webhook contract and should not be "normalized" to camelCase.

Added one integration test: when runIssueWithLogging rejects, the
orchestrator still emits run_completed with success:false, and the
synthetic failure IssueResult surfaces in the parallel-execute path.

Verified: build + lint clean, 20/20 targeted event tests, full suite
3357/3357 pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arch: typed workflow event system for decoupled progress, logging, and MCP streaming

1 participant