Skip to content

Commit ccaaa42

Browse files
fix(infra): archive plugin-state sidecar when canonical rows are equal or newer (#109832) (#110083)
* [AI] fix(infra): archive plugin-state sidecar when canonical rows are strictly newer (#109832) Conflict detection in migrateLegacyPluginStateSidecar compared rows for byte equality but treated every mismatch as a conflict. When canonical data was written by a live gateway after migration, the sidecar was retained as "conflicted", blocking startup readiness. Changes: - Use strict > for canonical timestamp comparison: only a strictly newer canonical row triggers archival. Equal-timestamp divergent rows remain as conflicts (Codex review P1 fix). - Update error message: "have different values; sidecar data is newer" instead of misleading "already existed in shared state" - 17 plugin-state migration tests covering all timestamp scenarios Related to #109832 * fix(state): clarify plugin sidecar conflicts --------- Co-authored-by: Josh Lehman <[email protected]>
1 parent 6caf637 commit ccaaa42

2 files changed

Lines changed: 118 additions & 12 deletions

File tree

‎src/commands/doctor-state-migrations.test.ts‎

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,7 +3132,7 @@ describe("doctor legacy state migrations", () => {
31323132
expect(fs.existsSync(targetPath)).toBe(false);
31333133
});
31343134

3135-
it("keeps the plugin-state sidecar when shared state already has a conflicting row", async () => {
3135+
it("archives the plugin-state sidecar when shared state has a newer row with different value", async () => {
31363136
const root = await makeTempRoot();
31373137
const sourcePath = writeLegacyPluginStateSidecar(root);
31383138
await withStateDir(root, async () => {
@@ -3150,11 +3150,9 @@ describe("doctor legacy state migrations", () => {
31503150
});
31513151
const result = await runLegacyStateMigrations({ detected });
31523152

3153-
expect(result.warnings).toStrictEqual([
3154-
"Left plugin-state sidecar in place because 1 row already existed in shared state: discord/components/interaction:1",
3155-
]);
3156-
expect(fs.existsSync(sourcePath)).toBe(true);
3157-
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(false);
3153+
expect(result.warnings).toStrictEqual([]);
3154+
expect(fs.existsSync(sourcePath)).toBe(false);
3155+
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(true);
31583156

31593157
await withStateDir(root, async () => {
31603158
const store = createPluginStateKeyedStore<{ ok: boolean }>("discord", {
@@ -3222,7 +3220,6 @@ describe("doctor legacy state migrations", () => {
32223220

32233221
expect(result.warnings).toStrictEqual([]);
32243222
expect(result.changes).toContain("Migrated 1 plugin-state sidecar entry → shared SQLite state");
3225-
expect(result.changes).toContain("Dropped 1 expired plugin-state sidecar entry");
32263223
expect(fs.existsSync(sourcePath)).toBe(false);
32273224
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(true);
32283225

@@ -3242,7 +3239,7 @@ describe("doctor legacy state migrations", () => {
32423239
});
32433240
});
32443241

3245-
it("does not report expired plugin-state sidecar rows as dropped when live conflicts keep the sidecar", async () => {
3242+
it("archives the plugin-state sidecar when canonical rows are newer than sidecar rows", async () => {
32463243
const root = await makeTempRoot();
32473244
const sourcePath = path.join(root, "plugin-state", "state.sqlite");
32483245
fs.mkdirSync(path.dirname(sourcePath), { recursive: true });
@@ -3298,9 +3295,114 @@ describe("doctor legacy state migrations", () => {
32983295
});
32993296
const result = await runLegacyStateMigrations({ detected });
33003297

3301-
expect(result.changes).toStrictEqual([]);
3298+
expect(result.warnings).toStrictEqual([]);
3299+
expect(fs.existsSync(sourcePath)).toBe(false);
3300+
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(true);
3301+
});
3302+
3303+
it("keeps the plugin-state sidecar when the sidecar has a newer row than canonical state", async () => {
3304+
const root = await makeTempRoot();
3305+
const sourcePath = path.join(root, "plugin-state", "state.sqlite");
3306+
fs.mkdirSync(path.dirname(sourcePath), { recursive: true });
3307+
const sqlite = requireNodeSqlite();
3308+
const db = new sqlite.DatabaseSync(sourcePath);
3309+
try {
3310+
db.exec(`
3311+
CREATE TABLE plugin_state_entries (
3312+
plugin_id TEXT NOT NULL,
3313+
namespace TEXT NOT NULL,
3314+
entry_key TEXT NOT NULL,
3315+
value_json TEXT NOT NULL,
3316+
created_at INTEGER NOT NULL,
3317+
expires_at INTEGER,
3318+
PRIMARY KEY (plugin_id, namespace, entry_key)
3319+
);
3320+
`);
3321+
const insert = db.prepare(`
3322+
INSERT INTO plugin_state_entries (
3323+
plugin_id, namespace, entry_key, value_json, created_at, expires_at
3324+
) VALUES (?, ?, ?, ?, ?, ?)
3325+
`);
3326+
insert.run("discord", "components", "interaction:1", '{"ok":true}', 3000, null);
3327+
} finally {
3328+
db.close();
3329+
}
3330+
await withStateDir(root, async () => {
3331+
seedPluginStateEntriesForTests([
3332+
{
3333+
pluginId: "discord",
3334+
namespace: "components",
3335+
key: "interaction:1",
3336+
value: { ok: false },
3337+
createdAt: 1000,
3338+
expiresAt: null,
3339+
},
3340+
]);
3341+
});
3342+
resetPluginStateStoreForTests();
3343+
3344+
const detected = await detectLegacyStateMigrations({
3345+
cfg: {},
3346+
env: { OPENCLAW_STATE_DIR: root } as NodeJS.ProcessEnv,
3347+
});
3348+
const result = await runLegacyStateMigrations({ detected });
3349+
3350+
expect(result.warnings).toStrictEqual([
3351+
"Left plugin-state sidecar in place because 1 row differs from shared state without a newer canonical timestamp. First key: discord/components/interaction:1",
3352+
]);
3353+
expect(fs.existsSync(sourcePath)).toBe(true);
3354+
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(false);
3355+
});
3356+
3357+
it("keeps the plugin-state sidecar when sidecar and canonical rows have equal timestamps but different values", async () => {
3358+
const root = await makeTempRoot();
3359+
const sourcePath = path.join(root, "plugin-state", "state.sqlite");
3360+
fs.mkdirSync(path.dirname(sourcePath), { recursive: true });
3361+
const sqlite = requireNodeSqlite();
3362+
const db = new sqlite.DatabaseSync(sourcePath);
3363+
try {
3364+
db.exec(`
3365+
CREATE TABLE plugin_state_entries (
3366+
plugin_id TEXT NOT NULL,
3367+
namespace TEXT NOT NULL,
3368+
entry_key TEXT NOT NULL,
3369+
value_json TEXT NOT NULL,
3370+
created_at INTEGER NOT NULL,
3371+
expires_at INTEGER,
3372+
PRIMARY KEY (plugin_id, namespace, entry_key)
3373+
);
3374+
`);
3375+
const insert = db.prepare(`
3376+
INSERT INTO plugin_state_entries (
3377+
plugin_id, namespace, entry_key, value_json, created_at, expires_at
3378+
) VALUES (?, ?, ?, ?, ?, ?)
3379+
`);
3380+
insert.run("discord", "components", "interaction:1", '{"ok":true}', 1000, null);
3381+
} finally {
3382+
db.close();
3383+
}
3384+
await withStateDir(root, async () => {
3385+
seedPluginStateEntriesForTests([
3386+
{
3387+
pluginId: "discord",
3388+
namespace: "components",
3389+
key: "interaction:1",
3390+
value: { ok: false },
3391+
createdAt: 1000,
3392+
expiresAt: null,
3393+
},
3394+
]);
3395+
});
3396+
resetPluginStateStoreForTests();
3397+
3398+
const detected = await detectLegacyStateMigrations({
3399+
cfg: {},
3400+
env: { OPENCLAW_STATE_DIR: root } as NodeJS.ProcessEnv,
3401+
});
3402+
const result = await runLegacyStateMigrations({ detected });
3403+
33023404
expect(result.warnings).toStrictEqual([
3303-
"Left plugin-state sidecar in place because 1 row already existed in shared state: discord/components/interaction:1",
3405+
"Left plugin-state sidecar in place because 1 row differs from shared state without a newer canonical timestamp. First key: discord/components/interaction:1",
33043406
]);
33053407
expect(fs.existsSync(sourcePath)).toBe(true);
33063408
expect(fs.existsSync(`${sourcePath}.migrated`)).toBe(false);

‎src/infra/state-migrations.plugin-state.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ export async function migrateLegacyPluginStateSidecar(params: {
9797
const legacyExpired = isLegacyPluginStateRowExpired(row, now);
9898
if (existing) {
9999
if (!legacyPluginStateRowsMatch(existing, row)) {
100-
if (legacyExpired) {
100+
const existingCreatedAt = normalizeLegacySqliteInteger(existing.created_at) ?? 0;
101+
const rowCreatedAt = normalizeLegacySqliteInteger(row.created_at) ?? 0;
102+
if (existingCreatedAt > rowCreatedAt) {
103+
// Canonical row is strictly newer — migration already satisfied
104+
} else if (legacyExpired) {
101105
skippedExpired += 1;
102106
} else {
103107
conflictedKeys.push(`${row.plugin_id}/${row.namespace}/${row.entry_key}`);
@@ -142,7 +146,7 @@ export async function migrateLegacyPluginStateSidecar(params: {
142146
return {
143147
changes,
144148
warnings: [
145-
`Left plugin-state sidecar in place because ${conflictedKeys.length} ${conflictedKeys.length === 1 ? "row" : "rows"} already existed in shared state: ${conflictedKeys[0]}`,
149+
`Left plugin-state sidecar in place because ${conflictedKeys.length} ${conflictedKeys.length === 1 ? "row differs" : "rows differ"} from shared state without a newer canonical timestamp. First key: ${conflictedKeys[0]}`,
146150
],
147151
};
148152
}

0 commit comments

Comments
 (0)