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

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

Purpose: Log events will be forwarded to UI/FSUI for real-time visibility into agent output. Critical for the file system UI in Phase 7.
Output: Fully tested logging module that emits LogEntry 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/logging/index.ts
@src/logging/manager.ts
@src/logging/writer.ts
@src/logging/types.ts
@src/events/index.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Write unit tests for LogManager and ProcessLogWriter</name>
  <files>src/logging/manager.test.ts, src/logging/writer.test.ts</files>
  <action>
    Create src/logging/manager.test.ts:
    - Test ensureLogDir() creates directory
    - Test ensureProcessDir() creates process-specific directory
    - Test getProcessDir() returns correct path
    - Test getLogPath() returns correct path for stdout/stderr
    - Test listLogs() returns process IDs
    - Test listLogs() returns empty array if dir doesn't exist
    - Test cleanOldLogs() removes old directories
    - Test getBaseDir() returns configured base

    Use temporary directories for file system tests (use os.tmpdir()).
    Clean up after each test.

    Create src/logging/writer.test.ts:
    - Read src/logging/writer.ts first to understand the API
    - Test open() creates log files
    - Test writeStdout() writes to stdout.log
    - Test writeStderr() writes to stderr.log
    - Test close() flushes and closes handles
    - Test writing after close throws

    Use temporary directories, clean up after tests.
  </action>
  <verify>npm test passes with logging module tests green</verify>
  <done>LogManager and ProcessLogWriter have comprehensive test coverage</done>
</task>

<task type="auto">
  <name>Task 2: Add event emission to ProcessLogWriter</name>
  <files>src/logging/writer.ts, src/logging/index.ts</files>
  <action>
    Modify ProcessLogWriter constructor:
    - Add optional eventBus parameter: EventBus | undefined
    - Store as private readonly property

    Add event emission in writeStdout():
    - After writing to file, emit LogEntry event:
      { type: 'log.entry', timestamp: new Date(), payload: { processId, stream: 'stdout', data } }

    Add event emission in writeStderr():
    - After writing to file, emit LogEntry event:
      { type: 'log.entry', timestamp: new Date(), payload: { processId, stream: 'stderr', data } }

    Update createLogger() convenience function:
    - Add optional eventBus parameter
    - Pass through to ProcessLogWriter

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

    Update tests to verify events are emitted:
    - Create mock EventBus
    - Pass to ProcessLogWriter
    - Assert emit() called with correct LogEntry shape
    - Test that events are NOT emitted if eventBus not provided

    Key: This enables real-time log streaming to UI/FSUI in later phases.
  </action>
  <verify>npm test passes, log events emitted correctly</verify>
  <done>ProcessLogWriter emits LogEntry events, tests verify emission</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm run build succeeds
- [ ] npm test passes (all logging module tests)
- [ ] ProcessLogWriter accepts optional eventBus
- [ ] LogEntry events emitted on stdout/stderr writes
- [ ] Backwards compatible (eventBus is optional)
</verification>

<success_criteria>

- All tasks completed
- All verification checks pass
- Log events flow through event bus
- Ready for real-time log streaming
</success_criteria>

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