---
phase: 01.1-hexagonal-architecture
plan: 03
type: execute
wave: 2
depends_on: ["01.1-01"]
files_modified: [src/process/registry.test.ts, src/process/manager.test.ts, src/process/manager.ts, src/process/index.ts]
autonomous: true
---

<objective>
Add test coverage and event emission to the Process module.

Purpose: ProcessManager is core to agent lifecycle. Events like ProcessSpawned/ProcessStopped will drive coordination logic in later phases.
Output: Fully tested process module that emits domain events via the event bus.
</objective>

<execution_context>
@~/.claude/get-shit-done/workflows/execute-plan.md
@~/.claude/get-shit-done/templates/summary.md
</execution_context>

<context>
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/STATE.md
@.planning/phases/01.1-hexagonal-architecture/01.1-01-SUMMARY.md

@src/process/index.ts
@src/process/manager.ts
@src/process/registry.ts
@src/process/types.ts
@src/events/index.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Write unit tests for ProcessRegistry and ProcessManager</name>
  <files>src/process/registry.test.ts, src/process/manager.test.ts</files>
  <action>
    Create src/process/registry.test.ts:
    - Test register() adds process to registry
    - Test get() retrieves registered process
    - Test getAll() returns all processes
    - Test updateStatus() changes status correctly
    - Test unregister() removes process
    - Test get() returns undefined for non-existent process

    Create src/process/manager.test.ts:
    - Test spawn() creates process and registers it
    - Test spawn() throws if process ID already running
    - Test stop() terminates process
    - Test stop() throws if process not found
    - Test stopAll() stops all running processes
    - Test restart() stops and respawns process
    - Test isRunning() returns correct state

    Testing approach for ProcessManager:
    - Mock the execa module to avoid spawning real processes
    - Use vi.mock('execa') with mock subprocess that has controllable behavior
    - Create mock registry for isolation

    Focus on behavior, not implementation details.
  </action>
  <verify>npm test passes with process module tests green</verify>
  <done>ProcessRegistry and ProcessManager have comprehensive test coverage</done>
</task>

<task type="auto">
  <name>Task 2: Refactor ProcessManager to emit events via event bus</name>
  <files>src/process/manager.ts, src/process/index.ts</files>
  <action>
    Modify ProcessManager constructor:
    - Add optional eventBus parameter: EventBus | undefined
    - Store as private readonly property

    Add event emission in spawn():
    - After successful spawn, emit ProcessSpawned event:
      { type: 'process.spawned', timestamp: new Date(), payload: { processId, pid, command } }

    Add event emission in exit handler:
    - On normal exit, emit ProcessStopped:
      { type: 'process.stopped', timestamp: new Date(), payload: { processId, pid, exitCode } }
    - On crash (non-zero exit), emit ProcessCrashed:
      { type: 'process.crashed', timestamp: new Date(), payload: { processId, pid, signal } }

    Add event emission in stop():
    - After successful stop, ProcessStopped is already emitted by exit handler
    - No additional event needed

    Update src/process/index.ts exports if needed.

    Update tests to verify events are emitted:
    - Create mock EventBus
    - Pass to ProcessManager
    - Assert emit() called with correct event shape

    Key: eventBus is optional for backwards compatibility. If not provided, no events emitted.
  </action>
  <verify>npm test passes, events emitted correctly in tests</verify>
  <done>ProcessManager emits domain events, tests verify event emission</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm run build succeeds
- [ ] npm test passes (all process module tests)
- [ ] ProcessManager accepts optional eventBus
- [ ] ProcessSpawned event emitted on spawn
- [ ] ProcessStopped/ProcessCrashed events emitted on exit
</verification>

<success_criteria>

- All tasks completed
- All verification checks pass
- Process lifecycle events flow through event bus
- Backwards compatible (eventBus is optional)
</success_criteria>

<output>
After completion, create `.planning/phases/01.1-hexagonal-architecture/01.1-03-SUMMARY.md`
</output>
