---
name: "Persistent per-session agent logs"
---

# Story 89: Persistent per-session agent logs

## User Story

As a user, I want each agent session to write its output to a persistent log file so I can inspect what an agent did after it completes, even across server restarts.

## Acceptance Criteria

- [ ] Each agent session writes output to a log file at .story_kit/logs/{story_id}/{agent_name}-{session_id}.log
- [ ] Log files persist across server restarts and agent completions
- [ ] The get_agent_output MCP tool falls back to reading the log file when the in-memory stream is empty or the agent has completed
- [ ] Log files include timestamps, tool calls, text output, and status events
- [ ] Different sessions for the same agent on the same story produce separate log files (no mixing)

## Test Plan

### Unit Tests (server/src/agent_log.rs)

1. **test_log_writer_creates_directory_and_file** — AC1: Verify `AgentLogWriter::new()` creates `.story_kit/logs/{story_id}/` and the log file `{agent_name}-{session_id}.log`.
2. **test_log_writer_writes_jsonl_with_timestamps** — AC4: Verify `write_event()` writes valid JSONL with ISO 8601 timestamps including type, data, and timestamp fields.
3. **test_read_log_parses_written_events** — AC3/AC4: Verify `read_log()` can round-trip events written by `write_event()`.
4. **test_separate_sessions_produce_separate_files** — AC5: Verify two `AgentLogWriter` instances for the same story_id+agent_name but different session_ids write to different files without mixing.
5. **test_find_latest_log_returns_most_recent** — AC3: Verify `find_latest_log()` returns the correct most-recent log for a given story_id+agent_name pair.
6. **test_log_files_persist_on_disk** — AC2: Verify that after writer is dropped, the file still exists and is readable.

### Unit Tests (server/src/agents.rs)

7. **test_emit_event_writes_to_log_writer** — AC1/AC4: Verify that `emit_event` with a log writer writes to the log file in addition to broadcast+event_log.

### Integration Tests (server/src/http/mcp.rs)

8. **test_get_agent_output_falls_back_to_log_file** — AC3: Verify that when in-memory events are empty and agent is completed, `get_agent_output` reads from the log file.

## Out of Scope

- Log rotation or cleanup of old log files
- Frontend UI for viewing log files
- Log file compression
