fix(migration): track applied migrations in sidecar so user reverts stick

Users who auto-migrated from `openai/gpt-5.3-codex` to `openai/gpt-5.4`
and then reverted their config back to `gpt-5.3-codex` by hand had the
migration re-apply on every startup in an infinite loop. Discord bug
report pointed at the exact symptom: "i deleted the migrations and they
kept coming back".

The old migration tracking lived on the config body itself as a
`_migrations` string array. The skip-already-applied check relied on
the user not touching that field. But users hit by the unwanted
migration naturally reached for the JSON file to roll their model back,
and the natural human reaction to an incomprehensible internal field
next to their config is to delete it. That wiped the migration memory
and let the same migration re-apply at the next startup.

This PR introduces a sidecar state file that lives next to the config
as `<configPath>.migrations.json` and tracks applied migrations
outside the user's hand-editable config body. The migration pipeline:

  1. Reads applied migrations from BOTH the sidecar AND the legacy
     in-config `_migrations` field, unioning them. This keeps old
     configs that still carry `_migrations` working without forcing
     a reset.

  2. Writes the updated migration set to the sidecar, never to the
     config body.

  3. Strips the legacy `_migrations` field out of the config body on
     the first write after the sidecar takes over. Users stop seeing
     the mystery internal field in their own config from that point
     forward.

If the user also deletes the sidecar (explicit fresh-start gesture)
the migrations run again - that is intentional.

Tests (TDD, all new tests written before implementation):

  - src/shared/migration/migrations-sidecar.test.ts - 11 unit tests
    covering read/write/round-trip, malformed-payload resilience,
    parent-directory creation, sorted output for stable diffs, and
    non-string entry filtering.

  - src/shared/migration.test.ts - 6 new integration tests under the
    "migrateConfigFile with migration tracking via sidecar" block
    covering: no-op path, sidecar-only write, sidecar skip after user
    revert, legacy _migrations mirroring + strip, sidecar + legacy
    union with dedupe, and partial-history append. Existing
    "preserves existing _migrations and appends new ones" test was
    rewritten to assert the new sidecar-based contract.

  - Also fixes a latent test-hygiene bug: the shared
    /tmp/nonexistent-path-for-test.json config path used by many
    migrateConfigFile tests did not clean up its companion sidecar
    between tests, letting state from one test bleed into the next.
    Added afterEach that unlinks the sidecar.

Verified:

  - bun test src/shared/migration/ -> 11 new sidecar tests pass
  - bun test src/shared/migration.test.ts -> 82 pass, 0 fail
  - bun run typecheck -> clean
  - bun run script/run-ci-tests.ts -> 4458 pass, 0 fail (full suite)
This commit is contained in:
YeonGyu-Kim
2026-04-09 10:54:08 +09:00
parent d2bb5d57d1
commit 00a4f318ef
4 changed files with 444 additions and 51 deletions
+37 -7
View File
@@ -4,6 +4,7 @@ import { writeFileAtomically } from "../write-file-atomically"
import { AGENT_NAME_MAP, migrateAgentNames } from "./agent-names"
import { migrateHookNames } from "./hook-names"
import { migrateModelVersions } from "./model-versions"
import { readAppliedMigrations, writeAppliedMigrations } from "./migrations-sidecar"
export function migrateConfigFile(
configPath: string,
@@ -12,10 +13,22 @@ export function migrateConfigFile(
const copy = structuredClone(rawConfig)
let needsWrite = false
// Load previously applied migrations
const existingMigrations = Array.isArray(copy._migrations)
// Load previously applied migrations from BOTH the legacy in-config
// `_migrations` field AND the external sidecar file. The sidecar is the
// new source of truth because users were editing the config file to
// revert auto-migrated values and accidentally dropping the `_migrations`
// field in the process, which produced an infinite migration loop on
// every startup (#3263). Reading from both sources keeps old configs
// that still carry `_migrations` working without a forced reset.
const sidecarMigrations = readAppliedMigrations(configPath)
const inConfigMigrations = Array.isArray(copy._migrations)
? new Set(copy._migrations as string[])
: new Set<string>()
const existingMigrations = new Set<string>([
...sidecarMigrations,
...inConfigMigrations,
])
const hadLegacyInConfigMigrations = inConfigMigrations.size > 0
const allNewMigrations: string[] = []
if (copy.agents && typeof copy.agents === "object") {
@@ -54,13 +67,30 @@ export function migrateConfigFile(
allNewMigrations.push(...newMigrations)
}
// Record newly applied migrations
if (allNewMigrations.length > 0) {
const updatedMigrations = Array.from(existingMigrations)
updatedMigrations.push(...allNewMigrations)
copy._migrations = updatedMigrations
// Record newly applied migrations. We persist the full set (existing +
// new) to the external sidecar file and strip the legacy `_migrations`
// field from the config body on its way out, so users stop having to
// think about a field that never should have been in their config in
// the first place. The in-memory `rawConfig` never re-exposes
// `_migrations` to downstream schema validation.
const newMigrationsToRecord = allNewMigrations.filter(mKey => !existingMigrations.has(mKey))
if (newMigrationsToRecord.length > 0 || hadLegacyInConfigMigrations) {
const fullMigrationSet = new Set<string>([
...existingMigrations,
...newMigrationsToRecord,
])
writeAppliedMigrations(configPath, fullMigrationSet)
}
if (newMigrationsToRecord.length > 0) {
needsWrite = true
}
if (hadLegacyInConfigMigrations) {
// Migrating state out of the config body is itself a config write.
needsWrite = true
}
if ("_migrations" in copy) {
delete copy._migrations
}
if (copy.omo_agent) {
copy.sisyphus_agent = copy.omo_agent