/** * @file migrations.ts * * Run-once migration registry for devflow init. Migrations execute at most once * per machine (global scope) or once per machine across all discovered projects * (per-project scope). State is persisted at ~/.devflow/migrations.json. * * Registry holds 2.x entries only (first: canonicalise-agent-keys-v1). To add a * migration, append an entry to MIGRATIONS below. No 1.x → 2.0 upgrade path. */ import { promises as fs } from 'fs'; import * as path from 'path'; import { writeFileAtomicExclusive } from './fs-atomic.js'; import { pooled } from './pool.js'; import { getMemoryDir } from './project-paths.js'; import { LEGACY_AGENT_KEYS, canonicaliseAgentKeys, parseAgentMappingEnvelope } from './agent-models.js'; import { migrateLegacyTrackerConventions } from './tracker.js'; export type MigrationScope = 'global' | 'per-project'; /** * D38: Discriminated union context types eliminate ISP violation. * * GlobalMigrationContext: only devflowDir — per-project fields (memoryDir, * projectRoot) are structurally absent, so migrations that accidentally * reference them fail at compile time rather than receiving empty-string * sentinels. claudeDir is dropped entirely (was present in original but never * consumed by any migration). * * PerProjectMigrationContext: adds memoryDir and projectRoot so per-project * migrations can access them without receiving '' sentinels. * * Migration.run uses a conditional type directly over these two concrete types; * a union alias is not needed. */ export type GlobalMigrationContext = { scope: 'global'; devflowDir: string; }; export type PerProjectMigrationContext = { scope: 'per-project'; devflowDir: string; memoryDir: string; projectRoot: string; }; export interface MigrationRunResult { infos: string[]; warnings: string[]; } /** * A single migration entry. The `run` method returns a structured result * carrying infos and warnings surfaced to the user after `devflow init`. */ export interface Migration { id: string; description: string; scope: S; run( ctx: S extends 'global' ? GlobalMigrationContext : PerProjectMigrationContext, ): Promise; } /** * Discriminated union of all concrete migration variants. * * Prefer AnyMigration over the bare Migration (= Migration) in * registry and runner signatures: the union form lets TypeScript narrow the run() * overload by discriminating on `scope` at the call site, eliminating the * `as Migration<'global'>` casts that were previously required. */ export type AnyMigration = Migration<'global'> | Migration<'per-project'>; /** * The body of every canonicalise-agent-keys-vN migration: read agent-models.json raw, * apply LEGACY_AGENT_KEYS once, and write the result back atomically when a key changed. * Each migration id passes its own id so that its warnings name it. Idempotent, and a * file with no legacy key is left untouched. */ function canonicaliseAgentKeysRun(migrationId: string): (ctx: GlobalMigrationContext) => Promise { return async (ctx): Promise => { const infos: string[] = [] const warnings: string[] = [] // Fast path: no legacy keys defined — skip without touching the file. if (Object.keys(LEGACY_AGENT_KEYS).length === 0) return { infos, warnings } const filePath = path.join(ctx.devflowDir, 'agent-models.json') // parseAgentMappingEnvelope handles IO, BOM-strip, JSON parse, and shape validation. // Raw read intentionally avoids readAgentMapping (which would drop unknown fields). const envelope = await parseAgentMappingEnvelope(filePath) if (envelope.kind === 'skip') return { infos, warnings } if (envelope.kind === 'warn') { warnings.push(`${migrationId}: ${envelope.message}`) return { infos, warnings } } const onWarning = (msg: string) => warnings.push(msg) const { agents: migrated, didMutate, renamed, dropped, guardDropped } = canonicaliseAgentKeys( envelope.rawAgents, onWarning, ) if (!didMutate) return { infos, warnings } // Write back with the canonical keys, preserving the rest of the envelope. // Idempotent under concurrent execution (no lock needed; see D31 comment above). const updated = { ...envelope.envelope, agents: migrated } try { await writeFileAtomicExclusive(filePath, JSON.stringify(updated, null, 2) + '\n') } catch (err: unknown) { warnings.push(`${migrationId}: failed to write ${filePath}: ${(err as Error).message}`) return { infos, warnings } } if (renamed.length > 0) { infos.push( `Migrated agent-models.json: renamed ${renamed.map(k => `'${k}' → '${LEGACY_AGENT_KEYS[k] as string}'`).join(', ')}`, ) } if (dropped.length > 0) { warnings.push( `${migrationId}: dropped legacy key(s) ${dropped.map(k => `'${k}'`).join(', ')} — the replacement key already holds a value, existing value kept`, ) } if (guardDropped.length > 0) { warnings.push( `${migrationId}: dropped legacy key(s) ${guardDropped.map(k => `'${k}'`).join(', ')} — skipped due to prototype-pollution guard`, ) } return { infos, warnings } } } /** * D31: Registry pattern over scattered `if (!applied.includes(...))` conditionals. * * A typed array of Migration entries provides: * - Single authoritative list of all one-time migrations (no hunting across files) * - Explicit scope field that drives the runner's dispatch logic without branching * on migration IDs * - Append-only growth: adding a migration = adding an entry here, nothing else * * The `scope` field distinguishes global (one run per machine, no project context * needed) from per-project (sweeps every discovered Claude-enabled project root). * * Append new migrations here. * * KNOWN ISSUE (out of scope for this wave): the migration runner retries a * THROWING migration on every `devflow init`, forever, with no cap or backoff. * This was acceptable with an empty MIGRATIONS registry (D37: the vacuous-truth * case); this registry's first real entry is added below. Fix deferred. * * Note how that interacts with the `canonicalise-agent-keys-vN` entries: runGlobalMigration * marks a migration applied for ANY non-throwing return, and those entries never * throw — it catches every I/O and parse failure and returns it as a *warning*. * So it does NOT hit the unbounded-retry path. It hits the opposite one: a * transient EACCES or a failed write records the migration as applied and it * never runs again, leaving legacy keys on disk permanently. * * That is survivable by design rather than by luck: `readAgentMapping` applies * canonicaliseAgentKeys on EVERY read, so a user whose disk migration silently * failed still resolves their overrides correctly, and the next write persists * the canonical keys. The disk file self-heals; only the one-time rewrite is * lost. A future fix should make genuine I/O failure (as distinct from * "malformed file, skip it") throw so the runner retries it. */ export const MIGRATIONS: readonly AnyMigration[] = [ { id: 'canonicalise-agent-keys-v1', description: 'Rename legacy agent keys in ~/.devflow/agent-models.json to their canonical names', scope: 'global', run: canonicaliseAgentKeysRun('canonicalise-agent-keys-v1'), }, { // v1 is already recorded as applied on every machine that upgraded through 2.x, so the // six-agent rename needs an id of its own. Both ids run the same body. On a 1.x machine // v1 runs first and, with the single-hop LEGACY_AGENT_KEYS, already lands every key on its // final slug, so v2 finds nothing to rewrite and makes no write. id: 'canonicalise-agent-keys-v2', description: 'Rename the keys of the six renamed agents in ~/.devflow/agent-models.json to their new names', scope: 'global', run: canonicaliseAgentKeysRun('canonicalise-agent-keys-v2'), }, { id: 'tracker-conventions-per-provider-v1', description: 'Move ~/.devflow/tracker.md to ~/.devflow/tracker/{provider}.md, the provider its frontmatter names', scope: 'global', // D-TRACKER-PER-PROVIDER-CONVENTIONS: conventions became per provider, so the // single machine-wide file moves to the file of the provider it was learned // for. The move and every refusal to move are migrateLegacyTrackerConventions' // (src/core/tracker.ts); this entry maps its outcome onto the runner's // contract. A file it leaves in place is REPORTED, once, and the migration is // marked applied — the file is user content and nothing a re-run could do // differently. An I/O failure THROWS instead: a throwing migration is not // marked applied, so the runner retries it on the next `devflow init` rather // than recording a move that never happened (the retry-forever path the // KNOWN ISSUE above describes is bounded here by what can fail — a rename and // an rm inside ~/.devflow). async run(ctx): Promise { const outcome = await migrateLegacyTrackerConventions(ctx.devflowDir) switch (outcome.kind) { case 'none': return { infos: [], warnings: [] } case 'moved': return { infos: [`Moved the ${outcome.provider} tracker conventions to ${outcome.to}`], warnings: [] } case 'kept': return { infos: [], warnings: [`tracker-conventions-per-provider-v1: ${outcome.reason}`] } case 'failed': throw new Error(outcome.error) default: { const _exhaustive: never = outcome return _exhaustive } } }, }, ]; const MIGRATIONS_FILE = 'migrations.json'; interface MigrationsFile { applied: string[]; } /** * D30: State lives at `~/.devflow/migrations.json` rather than in the install * manifest because migration state is machine-wide: once a global migration runs * on a machine it should never re-run regardless of which project triggered * devflow init. * * @param devflowDir - absolute path to the resolved machine root (`~/.devflow`) */ export async function readAppliedMigrations(devflowDir: string): Promise { const filePath = path.join(devflowDir, MIGRATIONS_FILE); try { const raw = await fs.readFile(filePath, 'utf-8'); const parsed = JSON.parse(raw) as MigrationsFile; if (!Array.isArray(parsed.applied)) return []; return parsed.applied; } catch { // File missing or malformed — treat as empty return []; } } /** * Write applied migration IDs to `~/.devflow/migrations.json` atomically. * Uses exclusive-create tmp + rename so readers never observe a partial file * and a stale tmp from a previous crash does not silently overwrite good data. * * Delegates to `writeFileAtomicExclusive` in fs-atomic.ts (D34/D39: canonical * TS atomic-write helper with race-tolerant unlink before retry). * * @param devflowDir - absolute path to `~/.devflow` * @param ids - full list of applied migration IDs (cumulative, not incremental) */ export async function writeAppliedMigrations( devflowDir: string, ids: string[], ): Promise { await fs.mkdir(devflowDir, { recursive: true }); const filePath = path.join(devflowDir, MIGRATIONS_FILE); const data: MigrationsFile = { applied: ids }; const content = JSON.stringify(data, null, 2) + '\n'; await writeFileAtomicExclusive(filePath, content); } export interface MigrationFailure { id: string; scope: MigrationScope; project?: string; error: Error; } export interface RunMigrationsResult { newlyApplied: string[]; failures: MigrationFailure[]; infos: string[]; warnings: string[]; } /** * Logger interface for surfacing migration output to the user. * Injected so the reporter can be tested without a live clack prompt session. */ export interface MigrationLogger { warn(msg: string): void; info(msg: string): void; success(msg: string): void; } /** * Surface migration result infos, warnings, failures, and newly-applied IDs * to the user via the provided logger. * * Extracted from runMigrationsWithFallback (init.ts) so reporting can be * tested independently of the project-list routing logic. */ export function reportMigrationResult( result: RunMigrationsResult, logger: MigrationLogger, verbose: boolean, ): void { for (const f of result.failures) { // D33: Non-fatal — warn but continue; migration will retry on next init const where = f.project ? ` in ${path.basename(f.project)}` : ''; logger.warn(`Migration '${f.id}'${where} failed: ${f.error.message}`); } for (const info of result.infos) { logger.info(info); } for (const warn of result.warnings) { logger.warn(warn); } if (result.newlyApplied.length > 0) { logger.success(`Applied ${result.newlyApplied.length} migration(s)`); } if (verbose) { for (const id of result.newlyApplied) logger.info(` ✓ ${id}`); } } /** * Run a single global migration, returning { applied, failure, infos, warnings }. * * D33: Non-fatal semantics — if a global migration fails, we record the failure * and continue. The failing migration is NOT marked as applied so it retries on * the next `devflow init` run (transient errors such as filesystem contention * are eventually resolved without blocking the install). */ async function runGlobalMigration( migration: Migration<'global'>, ctx: GlobalMigrationContext, ): Promise<{ applied: boolean; failure: MigrationFailure | null; infos: string[]; warnings: string[]; }> { try { const runResult = await migration.run(ctx); return { applied: true, failure: null, infos: runResult.infos, warnings: runResult.warnings }; } catch (error) { return { applied: false, failure: { id: migration.id, scope: migration.scope, error: error instanceof Error ? error : new Error(String(error)), }, infos: [], warnings: [], }; } } /** * Run a single per-project migration across all discovered project roots with a * concurrency cap, returning { applied, failures, infos, warnings }. * * D35: Per-project migrations run across all discovered projects with a * concurrency cap of 16 to avoid EMFILE on machines with 50–200 projects. * This matches the pattern used by `installClaudeignore` in init.ts for * multi-project install — each project has its own `.memory/.decisions.lock` * so there is no cross-project contention. Promise.allSettled collects all * outcomes without short-circuiting on partial failures. * * Marking strategy: the migration is considered applied globally only when * ALL projects succeed. Any per-project failure causes the ID to remain * unapplied so the next `devflow init` (which may discover the same or * additional projects) can retry the failed projects. * * D37: runPerProjectMigration is unreachable in production — MIGRATIONS holds * only global migrations. The vacuous-truth * analysis is preserved for correctness: if a per-project migration is ever * added, an empty discoveredProjects list marks it applied (empty-discovery-marks-applied * intended); the applied-set write is skipped only when newlyApplied is empty, * which is guaranteed when MIGRATIONS has no per-project entries. */ async function runPerProjectMigration( migration: Migration<'per-project'>, ctx: { devflowDir: string }, discoveredProjects: string[], ): Promise<{ applied: boolean; failures: MigrationFailure[]; infos: string[]; warnings: string[]; }> { const results = await pooled( discoveredProjects, 16, (projectRoot) => { const memoryDir = getMemoryDir(projectRoot); return migration.run({ scope: 'per-project', devflowDir: ctx.devflowDir, memoryDir, projectRoot, }); }, ); const failures: MigrationFailure[] = []; const infos: string[] = []; const warnings: string[] = []; for (const [i, result] of results.entries()) { if (result.status === 'rejected') { failures.push({ id: migration.id, scope: migration.scope, project: discoveredProjects[i], error: result.reason instanceof Error ? result.reason : new Error(String(result.reason)), }); } else { infos.push(...result.value.infos); warnings.push(...result.value.warnings); } } const applied = results.every(r => r.status === 'fulfilled'); return { applied, failures, infos, warnings }; } /** * Run all unapplied migrations from MIGRATIONS. * * D32: Always-run-unapplied semantics (no fresh-vs-upgrade branch). * MIGRATIONS currently holds only global migrations; on a fresh machine each * executes once and the applied set is written to migrations.json. On subsequent * runs every ID is already in the applied set and the loop is a no-op. * * @param ctx - devflowDir, the resolved machine root (`~/.devflow`) that also holds * migrations.json; memoryDir and projectRoot are filled per-project * @param discoveredProjects - absolute paths to discovered Claude-enabled project roots * @param registryOverride - override MIGRATIONS for testing (defaults to module-level MIGRATIONS) */ export async function runMigrations( ctx: { devflowDir: string }, discoveredProjects: string[], registryOverride?: readonly AnyMigration[], ): Promise { const registry = registryOverride ?? MIGRATIONS; // D-ONE-HOME: state lives in the caller's resolved machine root, never a // second, independently derived home path (D30: machine-wide state). const appliedArray = await readAppliedMigrations(ctx.devflowDir); // Convert to Set once for O(1) lookups throughout the loop (issue #9) const applied = new Set(appliedArray); const newlyApplied: string[] = []; const failures: MigrationFailure[] = []; const infos: string[] = []; const warnings: string[] = []; for (const migration of registry) { if (applied.has(migration.id)) continue; // Already done — skip if (migration.scope === 'global') { const globalCtx: GlobalMigrationContext = { scope: 'global', devflowDir: ctx.devflowDir, }; const outcome = await runGlobalMigration(migration, globalCtx); if (outcome.applied) { newlyApplied.push(migration.id); infos.push(...outcome.infos); warnings.push(...outcome.warnings); } else if (outcome.failure) { failures.push(outcome.failure); } } else if (migration.scope === 'per-project') { const outcome = await runPerProjectMigration(migration, ctx, discoveredProjects); failures.push(...outcome.failures); infos.push(...outcome.infos); warnings.push(...outcome.warnings); if (outcome.applied) { newlyApplied.push(migration.id); } } else { // Exhaustiveness check — AnyMigration covers all MigrationScope values; // migration is narrowed to `never` here so this branch is unreachable at runtime. const _exhaustive: never = migration; void _exhaustive; throw new Error('Unknown migration scope'); } } // Write state once at end, accumulating all newly applied IDs (issue #5 — O(N²) → O(1)) if (newlyApplied.length > 0) { await writeAppliedMigrations(ctx.devflowDir, [...appliedArray, ...newlyApplied]); } return { newlyApplied, failures, infos, warnings }; }