---
phase: 01-core-infrastructure
plan: 04
type: execute
wave: 2
depends_on: ["01-01"]
files_modified: [src/logging/types.ts, src/logging/writer.ts, src/logging/manager.ts, src/logging/index.ts]
autonomous: true
---

<objective>
Create file-based logging infrastructure for per-process stdout/stderr capture.

Purpose: Each agent process needs its own log files. This creates the logging infrastructure that agent processes will use.
Output: LogManager that creates and manages per-process log files in a dedicated directory.
</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
</context>

<tasks>

<task type="auto">
  <name>Task 1: Create log directory management</name>
  <files>src/logging/types.ts, src/logging/manager.ts</files>
  <action>
    Create src/logging/types.ts:
    - LogLevel: 'debug' | 'info' | 'warn' | 'error'
    - LogEntry: { timestamp: Date, level: LogLevel, processId: string, message: string }
    - LogConfig: { baseDir: string, maxFileSize?: number, retainDays?: number }

    Create src/logging/manager.ts:
    - LogManager class:
      - constructor(config: LogConfig) - baseDir defaults to ~/.cw/logs
      - ensureLogDir(): Promise<void> - creates log directory if not exists
      - getLogPath(processId: string, stream: 'stdout' | 'stderr'): string
      - cleanOldLogs(retainDays: number): Promise<number> - removes logs older than N days
      - listLogs(): Promise<string[]> - lists all log files

    Use node:fs/promises for all file operations.
    Use node:path and node:os for cross-platform paths.
    Log directory structure: ~/.cw/logs/{processId}/stdout.log, stderr.log
  </action>
  <verify>LogManager creates ~/.cw/logs directory, returns correct paths</verify>
  <done>Log directory management works, paths are cross-platform correct</done>
</task>

<task type="auto">
  <name>Task 2: Create per-process log writer</name>
  <files>src/logging/writer.ts, src/logging/index.ts</files>
  <action>
    Create src/logging/writer.ts:
    - ProcessLogWriter class:
      - constructor(processId: string, logManager: LogManager)
      - open(): Promise<void> - opens file handles for stdout/stderr
      - writeStdout(data: string | Buffer): Promise<void>
      - writeStderr(data: string | Buffer): Promise<void>
      - close(): Promise<void> - flushes and closes file handles
      - getStdoutStream(): fs.WriteStream
      - getStderrStream(): fs.WriteStream

    Use fs.createWriteStream with { flags: 'a' } for append mode.
    Add timestamps to each line of output.
    Handle backpressure properly (pause source if drain needed).

    Create src/logging/index.ts:
    - Export LogManager, ProcessLogWriter, types
    - Export createLogger(processId: string) convenience function
  </action>
  <verify>ProcessLogWriter creates files, writes stdout/stderr with timestamps</verify>
  <done>Per-process logging captures output to separate files with timestamps</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] `npm run build` succeeds
- [ ] LogManager creates ~/.cw/logs/ directory
- [ ] ProcessLogWriter creates per-process log files
- [ ] Stdout and stderr go to separate files
- [ ] Log entries include timestamps
- [ ] File handles close properly (no resource leaks)
</verification>

<success_criteria>

- All tasks completed
- All verification checks pass
- Logging infrastructure ready for agent process output capture
- Satisfies INFRA-05 (basic logging captures stdout/stderr per agent)
</success_criteria>

<output>
After completion, create `.planning/phases/01-core-infrastructure/01-04-SUMMARY.md`
</output>
