Skip to content

Commit 46e4520

Browse files
fix(sessions): avoid quadratic migration over absent agent stores (#126685)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 7dd6d94 commit 46e4520

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎src/infra/state-migrations.orphan-keys.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,29 @@ describe("migrateOrphanedSessionKeys", () => {
727727
});
728728
});
729729

730+
it("checks missing agent stores without comparing every earlier path", async () => {
731+
await withStateFixture(async ({ tmpDir, stateDir }) => {
732+
const agentCount = 100;
733+
const cfg = {
734+
session: { store: path.join(tmpDir, "stores", "{agentId}", "sessions.json") },
735+
agents: {
736+
list: Array.from({ length: agentCount }, (_, index) => ({ id: `agent-${index}` })),
737+
},
738+
} as OpenClawConfig;
739+
const statSpy = vi.spyOn(fs, "statSync");
740+
741+
try {
742+
expect(await migrateFixtureState(stateDir, cfg)).toEqual({ changes: [], warnings: [] });
743+
const storeProbes = statSpy.mock.calls.filter(([candidate]) =>
744+
candidate.toString().endsWith("sessions.json"),
745+
);
746+
expect(storeProbes).toHaveLength(agentCount);
747+
} finally {
748+
statSpy.mockRestore();
749+
}
750+
});
751+
});
752+
730753
it("does not prepare channel migration surfaces without a legacy store", async () => {
731754
await withStateFixture(async ({ stateDir }) => {
732755
const prepareLegacySessionSurfaces = vi.fn(() => EMPTY_LEGACY_SESSION_SURFACES);

‎src/infra/state-migrations.session-store.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,17 @@ export async function migrateOrphanedSessionKeys(params: {
630630
});
631631
const pluginAgentIdSet = new Set(pluginAgentIds.map((id) => normalizeAgentId(id)));
632632

633-
// Collect all known agent store paths with their owning agentIds.
634-
// A single path may be shared by multiple agents when session.store
635-
// does not contain {agentId}.
633+
// Fixed session.store paths can be shared by several agent owners.
636634
const storeMap = new Map<string, Set<string>>();
637635
const storeAliasCandidates = new Map<string, Set<string>>();
638636
const addToStoreMap = (p: string, id: string) => {
637+
try {
638+
if (!fs.statSync(p, { throwIfNoEntry: false })?.isFile()) {
639+
return;
640+
}
641+
} catch {
642+
// Inaccessible paths must stay in their fail-closed alias group.
643+
}
639644
// A fixed-store owner is durable migration provenance. Keep other configured
640645
// agents from making that store's unscoped rows look ambiguous.
641646
const ownerId =
@@ -649,12 +654,7 @@ export async function migrateOrphanedSessionKeys(params: {
649654
const aliasCandidates = storeAliasCandidates.get(storePath) ?? new Set([storePath]);
650655
aliasCandidates.add(p);
651656
storeAliasCandidates.set(storePath, aliasCandidates);
652-
const existing = storeMap.get(storePath);
653-
if (existing) {
654-
existing.add(ownerId);
655-
} else {
656-
storeMap.set(storePath, new Set([ownerId]));
657-
}
657+
storeMap.set(storePath, (storeMap.get(storePath) ?? new Set<string>()).add(ownerId));
658658
};
659659
// Configured ownership includes normal agents plus ACP runtime/default hints.
660660
for (const configuredAgentId of listConfiguredSessionStoreAgentIds(params.cfg)) {

0 commit comments

Comments
 (0)