mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 15:10:52 +00:00
fix: scope session slot restart cleanup
This commit is contained in:
@@ -565,14 +565,119 @@ describe("plugin session extension SessionEntry projection", () => {
|
||||
const stored = loadSessionStore(storePath, { skipCache: true });
|
||||
const entry = stored["agent:main:main"] as unknown as Record<string, unknown>;
|
||||
expect(entry.approvalSnapshot).toBeUndefined();
|
||||
expect(entry.pluginExtensionSlotKeys).toEqual({
|
||||
expect(entry.pluginExtensionSlotKeys).toBeUndefined();
|
||||
expect(entry.pluginExtensions).toEqual({
|
||||
"restart-promoted-plugin": {
|
||||
workflow: { state: "waiting" },
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
if (previousStateDir === undefined) {
|
||||
delete process.env.OPENCLAW_STATE_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_STATE_DIR = previousStateDir;
|
||||
}
|
||||
await fs.rm(stateDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("clears only stale promoted SessionEntry slots on mixed plugin restart", async () => {
|
||||
const previousFixture = createPluginRegistryFixture();
|
||||
registerTestPlugin({
|
||||
registry: previousFixture.registry,
|
||||
config: previousFixture.config,
|
||||
record: createPluginRecord({ id: "restart-mixed-plugin", name: "Restart" }),
|
||||
register(api) {
|
||||
api.registerSessionExtension({
|
||||
namespace: "workflow",
|
||||
description: "promoted workflow",
|
||||
sessionEntrySlotKey: "approvalSnapshot",
|
||||
});
|
||||
api.registerSessionExtension({
|
||||
namespace: "legacy",
|
||||
description: "legacy promoted workflow",
|
||||
sessionEntrySlotKey: "legacyApprovalSnapshot",
|
||||
});
|
||||
},
|
||||
});
|
||||
const nextFixture = createPluginRegistryFixture();
|
||||
registerTestPlugin({
|
||||
registry: nextFixture.registry,
|
||||
config: nextFixture.config,
|
||||
record: createPluginRecord({ id: "restart-mixed-plugin", name: "Restart" }),
|
||||
register(api) {
|
||||
api.registerSessionExtension({
|
||||
namespace: "workflow",
|
||||
description: "promoted workflow",
|
||||
sessionEntrySlotKey: "approvalSnapshot",
|
||||
});
|
||||
api.registerSessionExtension({
|
||||
namespace: "legacy",
|
||||
description: "legacy workflow",
|
||||
});
|
||||
},
|
||||
});
|
||||
setActivePluginRegistry(previousFixture.registry.registry);
|
||||
|
||||
const stateDir = await fs.mkdtemp(
|
||||
path.join(resolvePreferredOpenClawTmpDir(), "openclaw-host-hooks-slot-restart-mixed-"),
|
||||
);
|
||||
const storePath = path.join(stateDir, "sessions.json");
|
||||
const tempConfig = { session: { store: storePath } };
|
||||
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
|
||||
try {
|
||||
process.env.OPENCLAW_STATE_DIR = stateDir;
|
||||
await withTempConfig({
|
||||
cfg: tempConfig,
|
||||
run: async () => {
|
||||
await updateSessionStore(storePath, (store) => {
|
||||
store["agent:main:main"] = {
|
||||
sessionId: "session-id",
|
||||
updatedAt: Date.now(),
|
||||
} as unknown as SessionEntry;
|
||||
});
|
||||
await expect(
|
||||
patchPluginSessionExtension({
|
||||
cfg: tempConfig as never,
|
||||
sessionKey: "agent:main:main",
|
||||
pluginId: "restart-mixed-plugin",
|
||||
namespace: "workflow",
|
||||
value: { state: "waiting" },
|
||||
}),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
await expect(
|
||||
patchPluginSessionExtension({
|
||||
cfg: tempConfig as never,
|
||||
sessionKey: "agent:main:main",
|
||||
pluginId: "restart-mixed-plugin",
|
||||
namespace: "legacy",
|
||||
value: { state: "legacy" },
|
||||
}),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
|
||||
await expect(
|
||||
cleanupReplacedPluginHostRegistry({
|
||||
cfg: tempConfig as never,
|
||||
previousRegistry: previousFixture.registry.registry,
|
||||
nextRegistry: nextFixture.registry.registry,
|
||||
}),
|
||||
).resolves.toMatchObject({ failures: [] });
|
||||
|
||||
const stored = loadSessionStore(storePath, { skipCache: true });
|
||||
const entry = stored["agent:main:main"] as unknown as Record<string, unknown>;
|
||||
expect(entry.approvalSnapshot).toEqual({ state: "waiting" });
|
||||
expect(entry.legacyApprovalSnapshot).toBeUndefined();
|
||||
expect(entry.pluginExtensionSlotKeys).toEqual({
|
||||
"restart-mixed-plugin": {
|
||||
workflow: "approvalSnapshot",
|
||||
},
|
||||
});
|
||||
expect(entry.pluginExtensions).toEqual({
|
||||
"restart-promoted-plugin": {
|
||||
"restart-mixed-plugin": {
|
||||
workflow: { state: "waiting" },
|
||||
legacy: { state: "legacy" },
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
@@ -70,12 +70,48 @@ function clearPromotedSessionEntrySlots(
|
||||
entry: SessionEntry,
|
||||
pluginId?: string,
|
||||
sessionEntrySlotKeys?: ReadonlySet<string>,
|
||||
options: { includeStoredSlotKeys?: boolean; pruneSlotOwnership?: boolean } = {},
|
||||
): void {
|
||||
const slotKeys = collectPromotedSessionEntrySlotKeys(entry, pluginId, sessionEntrySlotKeys);
|
||||
const slotKeys =
|
||||
options.includeStoredSlotKeys === false && sessionEntrySlotKeys
|
||||
? new Set(sessionEntrySlotKeys)
|
||||
: collectPromotedSessionEntrySlotKeys(entry, pluginId, sessionEntrySlotKeys);
|
||||
const entryRecord = entry as Record<string, unknown>;
|
||||
for (const slotKey of slotKeys) {
|
||||
delete entryRecord[slotKey];
|
||||
}
|
||||
if (!options.pruneSlotOwnership || !entry.pluginExtensionSlotKeys) {
|
||||
return;
|
||||
}
|
||||
const pruneRecord = (record: Record<string, string>): void => {
|
||||
for (const [namespace, slotKey] of Object.entries(record)) {
|
||||
const normalized = normalizeSessionEntrySlotKey(slotKey);
|
||||
if (normalized.ok && slotKeys.has(normalized.key)) {
|
||||
delete record[namespace];
|
||||
}
|
||||
}
|
||||
};
|
||||
if (pluginId) {
|
||||
const record = entry.pluginExtensionSlotKeys[pluginId];
|
||||
if (record) {
|
||||
pruneRecord(record);
|
||||
if (Object.keys(record).length === 0) {
|
||||
delete entry.pluginExtensionSlotKeys[pluginId];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (const record of Object.values(entry.pluginExtensionSlotKeys)) {
|
||||
pruneRecord(record);
|
||||
}
|
||||
for (const [ownerPluginId, record] of Object.entries(entry.pluginExtensionSlotKeys)) {
|
||||
if (Object.keys(record).length === 0) {
|
||||
delete entry.pluginExtensionSlotKeys[ownerPluginId];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Object.keys(entry.pluginExtensionSlotKeys).length === 0) {
|
||||
delete entry.pluginExtensionSlotKeys;
|
||||
}
|
||||
}
|
||||
|
||||
export function clearPluginOwnedSessionState(
|
||||
@@ -225,7 +261,10 @@ async function clearPromotedSessionEntrySlotStores(params: {
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
clearPromotedSessionEntrySlots(entry, params.pluginId, params.sessionEntrySlotKeys);
|
||||
clearPromotedSessionEntrySlots(entry, params.pluginId, params.sessionEntrySlotKeys, {
|
||||
includeStoredSlotKeys: false,
|
||||
pruneSlotOwnership: true,
|
||||
});
|
||||
entry.updatedAt = now;
|
||||
clearedInStore += 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user