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:
@@ -0,0 +1,146 @@
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { getSidecarPath, readAppliedMigrations, writeAppliedMigrations } from "./migrations-sidecar"
|
||||
|
||||
describe("migrations sidecar", () => {
|
||||
let workdir: string
|
||||
|
||||
beforeEach(() => {
|
||||
workdir = mkdtempSync(join(tmpdir(), "omo-migrations-sidecar-"))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(workdir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
describe("getSidecarPath", () => {
|
||||
test("appends .migrations.json to the config path", () => {
|
||||
expect(getSidecarPath("/home/user/.config/opencode/oh-my-openagent.json")).toBe(
|
||||
"/home/user/.config/opencode/oh-my-openagent.json.migrations.json",
|
||||
)
|
||||
})
|
||||
|
||||
test("works for jsonc configs too", () => {
|
||||
expect(getSidecarPath("/home/user/oh-my-openagent.jsonc")).toBe(
|
||||
"/home/user/oh-my-openagent.jsonc.migrations.json",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("readAppliedMigrations", () => {
|
||||
test("returns an empty set when no sidecar exists", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.json")
|
||||
expect(readAppliedMigrations(configPath).size).toBe(0)
|
||||
})
|
||||
|
||||
test("returns the applied migrations listed in a well-formed sidecar", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.json")
|
||||
writeFileSync(
|
||||
getSidecarPath(configPath),
|
||||
JSON.stringify({
|
||||
appliedMigrations: [
|
||||
"model-version:openai/gpt-5.3-codex->openai/gpt-5.4",
|
||||
"model-version:anthropic/claude-opus-4-5->anthropic/claude-opus-4-6",
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
const applied = readAppliedMigrations(configPath)
|
||||
|
||||
expect(applied.size).toBe(2)
|
||||
expect(applied.has("model-version:openai/gpt-5.3-codex->openai/gpt-5.4")).toBe(true)
|
||||
expect(applied.has("model-version:anthropic/claude-opus-4-5->anthropic/claude-opus-4-6")).toBe(true)
|
||||
})
|
||||
|
||||
test("returns an empty set on malformed JSON instead of throwing", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.json")
|
||||
writeFileSync(getSidecarPath(configPath), "{ this is not json")
|
||||
|
||||
expect(readAppliedMigrations(configPath).size).toBe(0)
|
||||
})
|
||||
|
||||
test("returns an empty set when the sidecar payload has the wrong shape", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.json")
|
||||
writeFileSync(getSidecarPath(configPath), JSON.stringify({ appliedMigrations: "not-an-array" }))
|
||||
|
||||
expect(readAppliedMigrations(configPath).size).toBe(0)
|
||||
})
|
||||
|
||||
test("ignores non-string entries inside appliedMigrations", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.json")
|
||||
writeFileSync(
|
||||
getSidecarPath(configPath),
|
||||
JSON.stringify({
|
||||
appliedMigrations: ["model-version:a->b", 42, null, "model-version:c->d"],
|
||||
}),
|
||||
)
|
||||
|
||||
const applied = readAppliedMigrations(configPath)
|
||||
|
||||
expect(applied.size).toBe(2)
|
||||
expect(applied.has("model-version:a->b")).toBe(true)
|
||||
expect(applied.has("model-version:c->d")).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("writeAppliedMigrations", () => {
|
||||
test("creates the sidecar with the given migration keys", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.json")
|
||||
const migrations = new Set([
|
||||
"model-version:openai/gpt-5.3-codex->openai/gpt-5.4",
|
||||
])
|
||||
|
||||
const ok = writeAppliedMigrations(configPath, migrations)
|
||||
|
||||
expect(ok).toBe(true)
|
||||
expect(existsSync(getSidecarPath(configPath))).toBe(true)
|
||||
|
||||
const body = JSON.parse(readFileSync(getSidecarPath(configPath), "utf-8"))
|
||||
expect(body.appliedMigrations).toEqual(["model-version:openai/gpt-5.3-codex->openai/gpt-5.4"])
|
||||
})
|
||||
|
||||
test("writes entries in sorted order for stable diffs", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.json")
|
||||
const migrations = new Set([
|
||||
"model-version:z->y",
|
||||
"model-version:a->b",
|
||||
"model-version:m->n",
|
||||
])
|
||||
|
||||
writeAppliedMigrations(configPath, migrations)
|
||||
|
||||
const body = JSON.parse(readFileSync(getSidecarPath(configPath), "utf-8"))
|
||||
expect(body.appliedMigrations).toEqual([
|
||||
"model-version:a->b",
|
||||
"model-version:m->n",
|
||||
"model-version:z->y",
|
||||
])
|
||||
})
|
||||
|
||||
test("creates parent directories if they do not exist yet", () => {
|
||||
const nested = join(workdir, "nested", "dir", "that", "does", "not", "exist")
|
||||
const configPath = join(nested, "oh-my-openagent.json")
|
||||
// Parent chain intentionally not created.
|
||||
|
||||
const ok = writeAppliedMigrations(configPath, new Set(["model-version:a->b"]))
|
||||
|
||||
expect(ok).toBe(true)
|
||||
expect(existsSync(getSidecarPath(configPath))).toBe(true)
|
||||
})
|
||||
|
||||
test("round-trips via readAppliedMigrations", () => {
|
||||
const configPath = join(workdir, "oh-my-openagent.jsonc")
|
||||
const original = new Set([
|
||||
"model-version:openai/gpt-5.3-codex->openai/gpt-5.4",
|
||||
"model-version:anthropic/claude-opus-4-5->anthropic/claude-opus-4-6",
|
||||
])
|
||||
|
||||
writeAppliedMigrations(configPath, original)
|
||||
const roundTripped = readAppliedMigrations(configPath)
|
||||
|
||||
expect(roundTripped).toEqual(original)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user