Skip to content

Gracefully handle invalid log files in orchestrated-run directories #171

Description

@williamthorsen

Problem

The Factory server's ProjectScanner encounters invalid run-index.json files during directory scans. When parsing fails, the server logs console.error with full stack traces that look like server crashes. In reality, these are data quality issues: old files, interrupted runs, or incompatible formats.

Example log output:

Error parsing run data for codeassembly/CODY-115/20260302-122846Z-orchestrated: Error: Invalid run-index.json at /Users/william/.ai/projects/codeassembly/tickets/CODY-115/20260302-122846Z-orchestrated/run-index.json
    at parseRunIndexFromRaw (file:///...run-data-parser.js:104:11)
    ...

Root cause

The parseRunData function in run-core throws plain Error objects with minimal context. The scanner's catch block in scanTicket treats all non-ENOENT errors the same way: console.error + skip the run. There are three distinct failure categories that should be handled differently:

  1. Corrupt JSONrun-index.json exists but contains invalid JSON syntax
  2. Invalid schema — valid JSON but doesn't match v2 or v3 schema (Zod validation details are discarded)
  3. Missing companion — v3 header exists but run-log.jsonl is missing

Proposed solution

  1. Introduce a structured RunDataParseError class in run-core with category, filePath, and optional zodIssues fields
  2. Refactor the parser to throw RunDataParseError instead of plain Error (preserving existing error messages for backward compatibility)
  3. Update the scanner to catch RunDataParseError specifically and log console.warn with an informative message and fix suggestion instead of console.error with a stack trace

Acceptance criteria

Must have

  • Parse errors produce a console.warn (not console.error) with a clear, single-line message
  • The warning includes the run path and what went wrong
  • Invalid runs are still skipped (no crash, no partial data)
  • Truly unexpected errors (permissions, I/O failures) remain as console.error

Should have

  • The warning includes a suggestion for how to fix the problem
  • Schema validation errors include Zod issue details (available on the error object)

Nice to have

  • Auto-fixable errors are fixed automatically

Out of scope

  • User sees errors in the UI

Metadata

Metadata

Labels

featureAdded or improved external functionalityscope:factory

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions