Skip to content

Commit eefe8bd

Browse files
authored
fix(codex): preserve explicit migration ownership (#123331)
Discover legacy shared session roots without requiring an implicit default agent. Preserve ambiguous bindings while keeping agent-scoped owners exact. Closes #123326
1 parent 3712509 commit eefe8bd

2 files changed

Lines changed: 112 additions & 6 deletions

File tree

‎extensions/codex/doctor-contract-api.test.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ function openBindingStore(env: NodeJS.ProcessEnv) {
6161

6262
async function createBindingMigrationFixture(options: {
6363
binding?: Record<string, unknown>;
64+
legacySharedRoot?: boolean;
6465
name: string;
6566
sessionIndex?: Record<string, unknown>;
6667
threadId: string;
6768
}) {
6869
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-codex-doctor-"));
6970
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
70-
const sessionsDir = path.join(stateDir, "agents", "main", "sessions");
71+
const sessionsDir = options.legacySharedRoot
72+
? path.join(stateDir, "sessions")
73+
: path.join(stateDir, "agents", "main", "sessions");
7174
const storePath = path.join(sessionsDir, "sessions.json");
7275
const transcriptPath = path.join(sessionsDir, `${options.name}.jsonl`);
7376
const sidecarPath = `${transcriptPath}.codex-app-server.json`;
@@ -293,6 +296,82 @@ describe("codex doctor contract", () => {
293296
await fs.rm(fixture.stateDir, { recursive: true, force: true });
294297
});
295298

299+
it("preserves ambiguous shared-root bindings for explicit multi-agent configs", async () => {
300+
const fixture = await createBindingMigrationFixture({
301+
legacySharedRoot: true,
302+
name: "explicit-owner",
303+
sessionIndex: {
304+
legacy: {
305+
sessionId: "explicit-owner",
306+
sessionFile: "explicit-owner.jsonl",
307+
updatedAt: 1,
308+
},
309+
},
310+
threadId: "thread-explicit-owner",
311+
});
312+
const params = {
313+
...fixture.params,
314+
config: {
315+
agents: { ownership: "explicit" as const, entries: { main: {}, ops: {} } },
316+
},
317+
};
318+
319+
await expect(fixture.migration.detectLegacyState(params)).resolves.toMatchObject({
320+
preview: [expect.stringContaining("legacy sidecar")],
321+
});
322+
const result = await fixture.migration.migrateLegacyState(params);
323+
324+
expect(result.changes).toEqual([]);
325+
expect(result.warnings).toEqual([
326+
expect.stringContaining("session ownership is indeterminate"),
327+
]);
328+
await expect(fs.access(fixture.sidecarPath)).resolves.toBeUndefined();
329+
await expect(fs.access(`${fixture.sidecarPath}.migrated`)).rejects.toThrow();
330+
await expect(openBindingStore(fixture.env).entries()).resolves.toEqual([]);
331+
332+
await fs.rm(fixture.stateDir, { recursive: true, force: true });
333+
});
334+
335+
it("keeps an agent-scoped shared-root binding with its explicit owner", async () => {
336+
const sessionKey = "agent:ops:legacy";
337+
const fixture = await createBindingMigrationFixture({
338+
legacySharedRoot: true,
339+
name: "explicit-ops-owner",
340+
sessionIndex: {
341+
[sessionKey]: {
342+
sessionId: "explicit-ops-owner",
343+
sessionFile: "explicit-ops-owner.jsonl",
344+
updatedAt: 1,
345+
},
346+
},
347+
threadId: "thread-explicit-ops-owner",
348+
});
349+
const params = {
350+
...fixture.params,
351+
config: {
352+
agents: { ownership: "explicit" as const, entries: { main: {}, ops: {} } },
353+
},
354+
};
355+
356+
await expect(fixture.migration.migrateLegacyState(params)).resolves.toMatchObject({
357+
changes: [expect.stringContaining("Migrated 1")],
358+
warnings: [],
359+
});
360+
await expect(
361+
openBindingStore(fixture.env).lookup(
362+
bindingStoreKey({
363+
kind: "session",
364+
agentId: "ops",
365+
sessionId: "explicit-ops-owner",
366+
sessionKey,
367+
}),
368+
),
369+
).resolves.toMatchObject({ sessionId: "explicit-ops-owner" });
370+
await expect(fs.access(`${fixture.sidecarPath}.migrated`)).resolves.toBeUndefined();
371+
372+
await fs.rm(fixture.stateDir, { recursive: true, force: true });
373+
});
374+
296375
it("bounds oversized legacy fingerprints before plugin-state import", async () => {
297376
const rawDynamicToolsFingerprint = JSON.stringify([
298377
{ name: "legacy_large_tool", inputSchema: { description: "dynamic-marker".repeat(8_000) } },

‎extensions/codex/src/migration/session-binding-sidecars.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ async function collectSessionSurfaces(params: MigrationEnvironment): Promise<Ses
101101
const { resolveStorePath } = await import("openclaw/plugin-sdk/session-store-runtime");
102102
const surfaces = new Map<string, SessionSurface>();
103103
const stateRoot = await canonicalPathFromExistingAncestor(params.stateDir);
104-
const add = async (root: string, storePath: string, agentId: string, scan: boolean) => {
104+
const add = async (
105+
root: string,
106+
storePath: string,
107+
agentId: string | undefined,
108+
scan: boolean,
109+
) => {
105110
const canonicalRoot = await canonicalPathFromExistingAncestor(root);
106111
const surface = surfaces.get(canonicalRoot) ?? {
107112
root: canonicalRoot,
@@ -113,7 +118,9 @@ async function collectSessionSurfaces(params: MigrationEnvironment): Promise<Ses
113118
// A store's configured path defines how relative sessionFile locators are
114119
// resolved. Keep it intact; canonicalize only when deduplicating aliases.
115120
surface.storePaths.add(path.resolve(storePath));
116-
surface.agentIds.add(agentId);
121+
if (agentId) {
122+
surface.agentIds.add(agentId);
123+
}
117124
surfaces.set(canonicalRoot, surface);
118125
};
119126

@@ -147,8 +154,11 @@ async function collectSessionSurfaces(params: MigrationEnvironment): Promise<Ses
147154
}
148155

149156
const legacyRoot = path.join(params.stateDir, "sessions");
150-
const defaultAgentId = resolveSessionAgentIds({ config: params.config }).defaultAgentId;
151-
await add(legacyRoot, path.join(legacyRoot, "sessions.json"), defaultAgentId, true);
157+
const legacyOwner = tryResolveLegacyBindingOwnerAgentId({
158+
sessionKey: "",
159+
config: params.config,
160+
});
161+
await add(legacyRoot, path.join(legacyRoot, "sessions.json"), legacyOwner, true);
152162
return [...surfaces.values()].toSorted((a, b) => a.root.localeCompare(b.root));
153163
}
154164

@@ -336,11 +346,15 @@ async function collectBindingOwners(
336346
const sessionsDir = path.dirname(storePath);
337347
for (const { sessionKey, entry } of index.entries) {
338348
const sessionId = entry.sessionId;
339-
const agentId = resolveLegacyBindingOwnerAgentId({
349+
const agentId = tryResolveLegacyBindingOwnerAgentId({
340350
sessionKey,
341351
config: params.config,
342352
storeAgentIds: storeAgentIds.get(storePath),
343353
});
354+
if (!agentId) {
355+
failures.push(`session index ${storePath} has an ambiguous owner for ${sessionKey}`);
356+
continue;
357+
}
344358
let legacyTranscriptPath: string;
345359
let canonicalLegacyTranscriptPath: string;
346360
try {
@@ -428,6 +442,19 @@ function resolveLegacyBindingOwnerAgentId(params: {
428442
}).sessionAgentId;
429443
}
430444

445+
function tryResolveLegacyBindingOwnerAgentId(
446+
params: Parameters<typeof resolveLegacyBindingOwnerAgentId>[0],
447+
): string | undefined {
448+
try {
449+
return resolveLegacyBindingOwnerAgentId(params);
450+
} catch (error) {
451+
if ((error as { code?: unknown }).code === "AGENT_SELECTION_REQUIRED") {
452+
return undefined;
453+
}
454+
throw error;
455+
}
456+
}
457+
431458
function copyBindingForSession(stored: MigratedBindingRow, sessionId: string): MigratedBindingRow {
432459
return stored.state === "active"
433460
? { version: 1, state: "active", binding: stored.binding, sessionId }

0 commit comments

Comments
 (0)