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.ts — discoverRunDirectories(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.ts — validateRunDirectory(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
Problem
The Factory app logs warnings when run directories are missing required files (
run-index.json,run-log.jsonl, orstatus.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-coreso 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.ts—discoverRunDirectories(basePath): walks the project/ticket/run hierarchy (both Pattern 1tickets/and Pattern 2 direct-entry layouts), returns a flat list ofRunDirectoryEntryobjects. Skips hidden dirs and-interactivedirs.src/scanners/run-directory-validator.ts—validateRunDirectory(runPath): wrapsparseRunDataand 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— consolidatedisEnoenthelper, replacing duplicates inrun-data-parser.tsand Factory'stype-guards.ts.src/scanners/index.ts— barrel export for@codeassembly/run-core/scannerssubpath.CLI (
src/cli.ts)Standalone script following the
packages/agents/src/cli.tspattern. Binary name:codeassembly-runs.check(default) — scans and reports invalid run directories with concise reasonsarchive— scans, reports, then prompts to move invalid directories to{project}/tickets-archived/{ticketId}/{runId}/--path <dir>— override base projects path (default:AI_PROJECTS_PATHor~/.ai/projects)Factory changes
src/config.ts— centralfactoryConfigobject. Initial option:logInvalidRuns(env:FACTORY_LOG_INVALID_RUNS, defaultfalse).ProjectScanner— refactored to delegate to run-core'sdiscoverRunDirectories+validateRunDirectory. Invalid runs are silently skipped unlesslogInvalidRunsistrue. Unexpected I/O errors always logged.type-guards.ts— reduced to re-export from run-core.Acceptance criteria
discoverRunDirectoriesreturns all candidate run directories for both directory layout patternsvalidateRunDirectoryreturns structured results for all error categories (missing files, corrupt JSON, invalid schema) and valid runs (v1, v2, v3)isEnoentlives inrun-core/src/type-guards.ts; no duplicates remaincodeassembly-runs checklists invalid directories with concise reasons and exits 1 if any foundcodeassembly-runs archiveprompts and moves invalid directories totickets-archived/ProjectScanneruses run-core scanners; no directory-walking logic remains in FactoryFACTORY_LOG_INVALID_RUNS=truere-enables themfactoryConfiginpackages/factory/src/config.tsis extensible for future optionspnpm run cipasses