Skip to content

Create standalone utility to archive invalid run directories #213

Description

@williamthorsen

Problem

The Factory app logs warnings when run directories are missing required files (run-index.json, run-log.jsonl, or status.json), have corrupt JSON, or fail schema validation. These warnings create noise in server logs. There is no standalone way to identify or clean up these invalid directories.

Solution

Extract directory discovery and validation logic into @codeassembly/run-core so both a new standalone CLI and the Factory app share the same code. Add a Factory configuration system to suppress invalid-run warnings by default.

New run-core modules

  • src/scanners/run-directory-scanner.tsdiscoverRunDirectories(basePath): walks the project/ticket/run hierarchy (both Pattern 1 tickets/ and Pattern 2 direct-entry layouts), returns a flat list of RunDirectoryEntry objects. Skips hidden dirs and -interactive dirs.
  • src/scanners/run-directory-validator.tsvalidateRunDirectory(runPath): wraps parseRunData and returns a discriminated union ({ valid: true, status } or { valid: false, reason }) instead of throwing. Reason values: missing run-index.json, missing run-log.jsonl, corrupt JSON, invalid schema.
  • src/type-guards.ts — consolidated isEnoent helper, replacing duplicates in run-data-parser.ts and Factory's type-guards.ts.
  • src/scanners/index.ts — barrel export for @codeassembly/run-core/scanners subpath.

CLI (src/cli.ts)

Standalone script following the packages/agents/src/cli.ts pattern. Binary name: codeassembly-runs.

  • check (default) — scans and reports invalid run directories with concise reasons
  • archive — scans, reports, then prompts to move invalid directories to {project}/tickets-archived/{ticketId}/{runId}/
  • --path <dir> — override base projects path (default: AI_PROJECTS_PATH or ~/.ai/projects)

Factory changes

  • src/config.ts — central factoryConfig object. Initial option: logInvalidRuns (env: FACTORY_LOG_INVALID_RUNS, default false).
  • ProjectScanner — refactored to delegate to run-core's discoverRunDirectories + validateRunDirectory. Invalid runs are silently skipped unless logInvalidRuns is true. Unexpected I/O errors always logged.
  • type-guards.ts — reduced to re-export from run-core.

Acceptance criteria

  • discoverRunDirectories returns all candidate run directories for both directory layout patterns
  • validateRunDirectory returns structured results for all error categories (missing files, corrupt JSON, invalid schema) and valid runs (v1, v2, v3)
  • isEnoent lives in run-core/src/type-guards.ts; no duplicates remain
  • codeassembly-runs check lists invalid directories with concise reasons and exits 1 if any found
  • codeassembly-runs archive prompts and moves invalid directories to tickets-archived/
  • Factory's ProjectScanner uses run-core scanners; no directory-walking logic remains in Factory
  • Factory suppresses invalid-run warnings by default; FACTORY_LOG_INVALID_RUNS=true re-enables them
  • factoryConfig in packages/factory/src/config.ts is extensible for future options
  • All new code has unit tests
  • pnpm run ci passes

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions