fix: replace stale canonical duplicate rows

This commit is contained in:
Tak Hoffman
2026-03-26 08:03:24 -05:00
parent fde3871ee7
commit cc7f18d6c2
2 changed files with 35 additions and 6 deletions

View File

@@ -419,6 +419,34 @@ describe("gateway session utils", () => {
expect(store["agent:main:Main"]).toBeUndefined();
});
test("migrateAndPruneGatewaySessionStoreKey replaces a stale canonical row with a fresher duplicate", () => {
const cfg = {
session: { mainKey: "main" },
agents: { list: [{ id: "main", default: true }] },
} as OpenClawConfig;
const store: Record<string, SessionEntry> = {
"agent:main:main": {
sessionId: "sess-stale",
updatedAt: 1,
} as SessionEntry,
"agent:main:MAIN": {
sessionId: "sess-fresh",
updatedAt: 2,
} as SessionEntry,
};
const result = migrateAndPruneGatewaySessionStoreKey({
cfg,
key: "agent:main:main",
store,
});
expect(result.primaryKey).toBe("agent:main:main");
expect(result.entry?.sessionId).toBe("sess-fresh");
expect(store["agent:main:main"]?.sessionId).toBe("sess-fresh");
expect(store["agent:main:MAIN"]).toBeUndefined();
});
test("listAgentsForGateway rejects avatar symlink escapes outside workspace", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "session-utils-avatar-outside-"));
const workspace = path.join(root, "workspace");

View File

@@ -482,12 +482,13 @@ export function migrateAndPruneGatewaySessionStoreKey(params: {
store: params.store,
});
const primaryKey = target.canonicalKey;
if (!params.store[primaryKey]) {
const freshestMatch = resolveFreshestSessionStoreMatchFromStoreKeys(
params.store,
target.storeKeys,
);
if (freshestMatch) {
const freshestMatch = resolveFreshestSessionStoreMatchFromStoreKeys(
params.store,
target.storeKeys,
);
if (freshestMatch) {
const currentPrimary = params.store[primaryKey];
if (!currentPrimary || (freshestMatch.entry.updatedAt ?? 0) > (currentPrimary.updatedAt ?? 0)) {
params.store[primaryKey] = freshestMatch.entry;
}
}