Skip to content

Commit d6343ec

Browse files
fix(plugins): retain incognito scope for embedded session ownership
1 parent f4387b7 commit d6343ec

2 files changed

Lines changed: 105 additions & 2 deletions

File tree

‎src/plugins/registry-runtime.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
parseSqliteSessionFileMarker,
77
sqliteSessionFileMarkerMatchesTarget,
88
} from "../config/sessions/legacy-sqlite-marker.js";
9+
import { resolveSessionStorePathForScope } from "../config/sessions/session-store-path.js";
910
import type { SessionEntry } from "../config/sessions/types.js";
1011
import {
1112
createPluginBlobStore,
@@ -24,6 +25,7 @@ import {
2425
type PluginStateKeyedStore,
2526
type PluginStateSyncKeyedStore,
2627
} from "../plugin-state/plugin-state-store.js";
28+
import { normalizeAgentId, parseAgentSessionKey } from "../routing/session-key.js";
2729
import {
2830
isAgentHarnessSessionKey,
2931
isAgentHarnessSessionKeyOwnedBy,
@@ -308,9 +310,28 @@ export function createPluginRuntimeResolver(state: PluginRegistryState) {
308310
if (sessionIds.size === 0 && sessionFiles.size === 0) {
309311
return;
310312
}
313+
const soleSessionKey = sessionKeys.size === 1 ? sessionKeys.values().next().value : undefined;
314+
const sessionKeyAgentId = parseAgentSessionKey(soleSessionKey)?.agentId;
315+
const explicitScanAgentId = soleSessionKey && agentId ? normalizeAgentId(agentId) : agentId;
316+
if (sessionKeyAgentId && explicitScanAgentId && explicitScanAgentId !== sessionKeyAgentId) {
317+
throw new Error(
318+
`Plugin session ownership agent "${explicitScanAgentId}" does not match session key agent "${sessionKeyAgentId}".`,
319+
);
320+
}
321+
const scanAgentId = sessionKeyAgentId ?? explicitScanAgentId;
322+
// Keyed reads resolve agent and incognito-store ownership from the key itself,
323+
// while ID/file scans need that same scope carried forward explicitly.
324+
const scanStorePath =
325+
soleSessionKey && sessionKeyAgentId
326+
? resolveSessionStorePathForScope({
327+
agentId: scanAgentId,
328+
sessionKey: soleSessionKey,
329+
...(storePath ? { storePath } : {}),
330+
})
331+
: storePath;
311332
const entries = registryParams.runtime.agent.session.listSessionEntries({
312-
...(agentId ? { agentId } : {}),
313-
...(storePath ? { storePath } : {}),
333+
...(scanAgentId ? { agentId: scanAgentId } : {}),
334+
...(scanStorePath ? { storePath: scanStorePath } : {}),
314335
readOnly: true,
315336
});
316337
for (const { sessionKey, entry } of entries) {

‎src/plugins/registry.runtime-config.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describe, expect, it, vi } from "vitest";
55
import { formatSqliteSessionFileMarker } from "../config/sessions/legacy-sqlite-marker.js";
66
import type { SessionEntry } from "../config/sessions/types.js";
77
import type { OpenClawConfig } from "../config/types.openclaw.js";
8+
import { resolveIncognitoOpenClawAgentSqlitePath } from "../state/openclaw-agent-db.js";
89
import { resolveUserPath } from "../utils.js";
910
import { createPluginRecord } from "./loader-records.js";
1011
import { createPluginRegistry } from "./registry.js";
@@ -447,6 +448,87 @@ describe("plugin registry runtime config scope", () => {
447448
).rejects.toThrow("requires exactly one runtime owner");
448449
});
449450

451+
it("carries incognito scope into embedded agent session identity checks", async () => {
452+
const sessionKey = "agent:researcher:dashboard:incognito-ownership-check";
453+
const entry = {
454+
sessionId: "incognito-session",
455+
updatedAt: 1,
456+
agentHarnessId: "test-harness",
457+
modelSelectionLocked: true as const,
458+
};
459+
const runtime = createPluginRuntime();
460+
runtime.agent.session.getSessionEntry = vi.fn((params) => {
461+
expect(params).toEqual(
462+
params.agentId === undefined
463+
? { sessionKey, readConsistency: "latest" }
464+
: { agentId: "main", sessionKey, readConsistency: "latest" },
465+
);
466+
return params.sessionKey === sessionKey ? entry : undefined;
467+
});
468+
runtime.agent.session.listSessionEntries = vi.fn((params) => {
469+
expect(params).toEqual({
470+
agentId: "researcher",
471+
readOnly: true,
472+
storePath: resolveIncognitoOpenClawAgentSqlitePath({ agentId: "researcher" }),
473+
});
474+
return [{ sessionKey, entry }];
475+
});
476+
const runEmbeddedAgent = vi.fn(async () => ({
477+
ok: true,
478+
})) as unknown as PluginRuntime["agent"]["runEmbeddedAgent"];
479+
Object.defineProperty(runtime.agent, "runEmbeddedAgent", {
480+
configurable: true,
481+
value: runEmbeddedAgent,
482+
});
483+
const pluginRegistry = createTestRegistry(runtime);
484+
const ownerRecord = createPluginRecord({
485+
id: "harness-owner",
486+
source: "/plugins/harness-owner/index.js",
487+
origin: "bundled",
488+
enabled: true,
489+
configSchema: false,
490+
});
491+
const otherRecord = createPluginRecord({
492+
id: "other-plugin",
493+
source: "/plugins/other-plugin/index.js",
494+
origin: "bundled",
495+
enabled: true,
496+
configSchema: false,
497+
});
498+
const ownerApi = pluginRegistry.createApi(ownerRecord, { config: {} as OpenClawConfig });
499+
const otherApi = pluginRegistry.createApi(otherRecord, { config: {} as OpenClawConfig });
500+
ownerApi.registerAgentHarness({
501+
id: "test-harness",
502+
label: "Test Harness",
503+
supports: () => ({ supported: true }),
504+
runAttempt: async () => {
505+
throw new Error("unused");
506+
},
507+
});
508+
const runParams = {
509+
sessionId: entry.sessionId,
510+
sessionKey,
511+
workspaceDir: "/tmp",
512+
prompt: "continue",
513+
timeoutMs: 1,
514+
runId: "run-1",
515+
} as Parameters<PluginRuntime["agent"]["runEmbeddedAgent"]>[0];
516+
517+
await expect(ownerApi.runtime.agent.runEmbeddedAgent(runParams)).resolves.toEqual({ ok: true });
518+
expect(runtime.agent.session.listSessionEntries).toHaveBeenCalledOnce();
519+
expect(runEmbeddedAgent).toHaveBeenCalledOnce();
520+
await expect(
521+
ownerApi.runtime.agent.runEmbeddedAgent({ ...runParams, agentId: "main" }),
522+
).rejects.toThrow('does not match session key agent "researcher"');
523+
expect(runtime.agent.session.listSessionEntries).toHaveBeenCalledOnce();
524+
expect(runEmbeddedAgent).toHaveBeenCalledOnce();
525+
await expect(otherApi.runtime.agent.runEmbeddedAgent(runParams)).rejects.toThrow(
526+
'owned by plugin "harness-owner"',
527+
);
528+
expect(runtime.agent.session.listSessionEntries).toHaveBeenCalledOnce();
529+
expect(runEmbeddedAgent).toHaveBeenCalledOnce();
530+
});
531+
450532
it("limits locked harness session mutation and execution to the harness owner", async () => {
451533
const reservedKey = "agent:main:harness:codex:thread-1";
452534
const ordinaryKey = "agent:main:ordinary";

0 commit comments

Comments
 (0)