Skip to content

Commit a9303f2

Browse files
authored
fix(workboard): preserve unassigned worker ownership (#115119)
* fix(workboard): preserve unassigned worker ownership * fix(plugins): defer session config until ownership resolution * test(workboard): keep claimed default workers unassigned
1 parent 4048b57 commit a9303f2

7 files changed

Lines changed: 136 additions & 24 deletions

File tree

‎extensions/workboard/src/dispatcher.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ describe("dispatchAndStartWorkboardCards", () => {
10721072
],
10731073
},
10741074
});
1075+
expect((await store.get(card.id))?.agentId).toBeUndefined();
10751076
expect((await store.get(card.id))?.metadata?.claim).toBeUndefined();
10761077
});
10771078
});

‎extensions/workboard/src/dispatcher.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ async function runWorkboardDispatch(
484484
if (runCwd && !workspaceAccess.unrestricted) {
485485
await assertRestrictedWorkboardTarget({
486486
root: runCwd,
487-
// Claim may populate agentId; keep the sessionKey target identity.
488487
agentId: card.agentId,
489488
sessionKey,
490489
modelProvider: params.options?.provider,

‎extensions/workboard/src/lifecycle-sync.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,9 +698,9 @@ describe("Workboard gateway lifecycle sync", () => {
698698

699699
expect(claimed.card).toMatchObject({
700700
status: "running",
701-
agentId: "workboard-dispatcher",
702701
metadata: { claim: { ownerId: "workboard-dispatcher" } },
703702
});
703+
expect(claimed.card.agentId).toBeUndefined();
704704
await runSessionSweep({
705705
store,
706706
sessions: [

‎extensions/workboard/src/store-workflow.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ export class WorkboardWorkflowStore extends WorkboardPromoteStore {
143143
guarded.status === "backlog" || guarded.status === "todo" || guarded.status === "ready"
144144
? "running"
145145
: guarded.status,
146-
agentId: guarded.agentId ?? ownerId,
147146
...(options.adoptWorkspaceAccess && !guarded.metadata?.automation?.workspaceAccess
148147
? { workspaceAccess: options.adoptWorkspaceAccess }
149148
: {}),

‎extensions/workboard/src/store.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@ describe("WorkboardStore", () => {
21612161

21622162
expect(claimed.token).toBeTruthy();
21632163
expect(claimed.card.status).toBe("running");
2164-
expect(claimed.card.agentId).toBe("main");
2164+
expect(claimed.card.agentId).toBeUndefined();
21652165
expect(claimed.card.metadata?.claim).toMatchObject({ ownerId: "main" });
21662166

21672167
await expect(store.claim(card.id, { ownerId: "other" })).rejects.toThrow(/already claimed/);

‎src/plugins/registry-runtime.ts‎

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
parseSqliteSessionFileMarker,
1010
sqliteSessionFileMarkerMatchesTarget,
1111
} from "../config/sessions/legacy-sqlite-marker.js";
12+
import { resolveSessionEntryAccessTarget } from "../config/sessions/session-accessor.js";
1213
import { resolveSessionStorePathForScope } from "../config/sessions/session-store-path.js";
1314
import type { SessionEntry } from "../config/sessions/types.js";
15+
import type { OpenClawConfig } from "../config/types.openclaw.js";
1416
import {
1517
createPluginBlobStore,
1618
type OpenBlobStoreOptions,
@@ -23,7 +25,12 @@ import {
2325
type PluginStateKeyedStore,
2426
type PluginStateSyncKeyedStore,
2527
} from "../plugin-state/plugin-state-store.js";
26-
import { normalizeAgentId, parseAgentSessionKey } from "../routing/session-key.js";
28+
import {
29+
classifySessionKeyShape,
30+
isUnscopedSessionKeySentinel,
31+
normalizeAgentId,
32+
parseAgentSessionKey,
33+
} from "../routing/session-key.js";
2734
import {
2835
isAgentHarnessSessionKey,
2936
isAgentHarnessSessionKeyOwnedBy,
@@ -79,6 +86,8 @@ const PLUGIN_GATEWAY_GLOBAL_SESSION_MUTATION_METHODS = new Set([
7986

8087
export function createPluginRuntimeResolver(state: PluginRegistryState) {
8188
const { registry, registryParams } = state;
89+
// SAFETY: Logical session resolution only reads the immutable runtime config snapshot.
90+
const currentSessionConfig = () => registryParams.runtime.config.current() as OpenClawConfig;
8291
const pluginRuntimeById = new Map<string, PluginRuntime>();
8392
const pluginRuntimeRecordById = new Map<string, PluginRecord>();
8493
const activePluginRuntimeRecords = new WeakSet<PluginRecord>();
@@ -319,50 +328,71 @@ export function createPluginRuntimeResolver(state: PluginRegistryState) {
319328
}
320329
assertReservedSessionKeyOwned(params.sessionKey, params.action);
321330
};
331+
const resolveStoredSessionOwnershipTarget = (params: {
332+
agentId?: string;
333+
env?: NodeJS.ProcessEnv;
334+
sessionKey: string;
335+
storePath?: string;
336+
}): { entry?: SessionEntry; sessionKey: string } => {
337+
if (
338+
classifySessionKeyShape(params.sessionKey) === "legacy_or_alias" &&
339+
!isUnscopedSessionKeySentinel(params.sessionKey) &&
340+
params.agentId === undefined &&
341+
params.storePath === undefined
342+
) {
343+
// Logical keys need their configured agent before SQLite ownership admission.
344+
const target = resolveSessionEntryAccessTarget({
345+
cfg: currentSessionConfig(),
346+
sessionKey: params.sessionKey,
347+
...(params.env !== undefined ? { env: params.env } : {}),
348+
});
349+
return { entry: target.entry, sessionKey: target.canonicalKey };
350+
}
351+
return {
352+
entry: registryParams.runtime.agent.session.getSessionEntry({
353+
sessionKey: params.sessionKey,
354+
readConsistency: "latest",
355+
...(params.agentId !== undefined ? { agentId: params.agentId } : {}),
356+
...(params.env !== undefined ? { env: params.env } : {}),
357+
...(params.storePath !== undefined ? { storePath: params.storePath } : {}),
358+
}),
359+
sessionKey: params.sessionKey,
360+
};
361+
};
322362
const assertStoredSessionEntryOwned = (params: {
323363
action: string;
324364
agentId?: string;
325365
env?: NodeJS.ProcessEnv;
326366
sessionKey: string;
327367
storePath?: string;
328368
}): SessionEntry | undefined => {
329-
const entry = registryParams.runtime.agent.session.getSessionEntry({
330-
sessionKey: params.sessionKey,
331-
readConsistency: "latest",
332-
...(params.agentId !== undefined ? { agentId: params.agentId } : {}),
333-
...(params.env !== undefined ? { env: params.env } : {}),
334-
...(params.storePath !== undefined ? { storePath: params.storePath } : {}),
335-
});
336-
assertSessionEntryOwned({ action: params.action, entry, sessionKey: params.sessionKey });
337-
return entry;
369+
const target = resolveStoredSessionOwnershipTarget(params);
370+
assertSessionEntryOwned({ action: params.action, ...target });
371+
return target.entry;
338372
};
339373
const resolveStoredSessionExecutionOwner = (params: {
340374
action: string;
341375
agentId?: string;
342376
sessionKey: string;
343377
storePath?: string;
344378
}): string | undefined => {
345-
const entry = registryParams.runtime.agent.session.getSessionEntry({
346-
sessionKey: params.sessionKey,
347-
readConsistency: "latest",
348-
...(params.agentId !== undefined ? { agentId: params.agentId } : {}),
349-
...(params.storePath !== undefined ? { storePath: params.storePath } : {}),
350-
});
379+
const target = resolveStoredSessionOwnershipTarget(params);
380+
const { entry, sessionKey } = target;
351381
const locked = entry
352-
? resolveLockedSessionHarnessRegistration(params.sessionKey, entry, params.action)
382+
? resolveLockedSessionHarnessRegistration(sessionKey, entry, params.action)
353383
: undefined;
354384
if (!entry || !locked || locked.ownerPluginId === pluginId) {
355-
assertSessionEntryOwned({ action: params.action, entry, sessionKey: params.sessionKey });
385+
assertSessionEntryOwned({ action: params.action, ...target });
356386
return undefined;
357387
}
358388
const registration = "registration" in locked ? locked.registration : undefined;
359389
if (!registration) {
360390
throw new Error(
361-
`Locked session "${params.sessionKey}" is owned by plugin "${locked.ownerPluginId}", not "${pluginId}".`,
391+
`Locked session "${sessionKey}" is owned by plugin "${locked.ownerPluginId}", not "${pluginId}".`,
362392
);
363393
}
364394
if (!registration.harness.delegatedExecutionPluginIds?.includes(pluginId)) {
365-
assertLockedSessionEntryOwned(params.sessionKey, entry, params.action);
395+
assertLockedSessionEntryOwned(sessionKey, entry, params.action);
366396
}
367397
return locked.ownerPluginId;
368398
};

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,89 @@ function createTestRegistry(runtime: ReturnType<typeof createPluginRuntime>) {
1818
}
1919

2020
describe("plugin registry SQLite session ownership", () => {
21+
it("does not read runtime config before a logical session requires it", () => {
22+
const runtime = createPluginRuntime();
23+
const readConfig = vi.fn(() => {
24+
throw new Error("runtime config was accessed eagerly");
25+
});
26+
Object.defineProperty(runtime, "config", { configurable: true, get: readConfig });
27+
28+
expect(() => createTestRegistry(runtime)).not.toThrow();
29+
expect(readConfig).not.toHaveBeenCalled();
30+
});
31+
32+
it("resolves unscoped worker keys through the configured default agent", async () => {
33+
await withTempHome(async () => {
34+
const config = {
35+
agents: { list: [{ id: "researcher", default: true }] },
36+
} as OpenClawConfig;
37+
const subagent = {
38+
run: vi.fn(async () => ({ runId: "workboard-run" })),
39+
waitForRun: vi.fn(async () => ({ status: "ok" as const })),
40+
getSessionMessages: vi.fn(async () => ({ messages: [] })),
41+
deleteSession: vi.fn(async () => {}),
42+
} satisfies PluginRuntime["subagent"];
43+
const runtime = createPluginRuntime({ subagent });
44+
runtime.config = { ...runtime.config, current: () => config };
45+
const pluginRegistry = createTestRegistry(runtime);
46+
const record = createPluginRecord({
47+
id: "workboard",
48+
source: "/plugins/workboard/index.js",
49+
origin: "bundled",
50+
enabled: true,
51+
configSchema: false,
52+
});
53+
const api = pluginRegistry.createApi(record, { config });
54+
const ownerRecord = createPluginRecord({
55+
id: "harness-owner",
56+
source: "/plugins/harness-owner/index.js",
57+
origin: "bundled",
58+
enabled: true,
59+
configSchema: false,
60+
});
61+
const ownerApi = pluginRegistry.createApi(ownerRecord, { config });
62+
ownerApi.registerAgentHarness({
63+
id: "test-harness",
64+
label: "Test Harness",
65+
supports: () => ({ supported: true }),
66+
runAttempt: async () => {
67+
throw new Error("unused");
68+
},
69+
});
70+
71+
try {
72+
const sessionKey = "subagent:workboard-default-unassigned";
73+
await expect(api.runtime.subagent.run({ sessionKey, message: "start" })).resolves.toEqual({
74+
runId: "workboard-run",
75+
});
76+
expect(subagent.run).toHaveBeenCalledWith({ sessionKey, message: "start" });
77+
78+
for (const invalidSessionKey of ["agent::malformed", "global", "unknown"]) {
79+
await expect(
80+
api.runtime.subagent.run({ sessionKey: invalidSessionKey, message: "reject" }),
81+
).rejects.toThrow("Cannot resolve SQLite session scope without an agent id");
82+
}
83+
84+
const lockedSessionKey = "harness:test-harness:owned";
85+
await replaceSessionEntry(
86+
{ agentId: "researcher", sessionKey: `agent:researcher:${lockedSessionKey}` },
87+
{
88+
sessionId: "owned-session",
89+
updatedAt: 1,
90+
agentHarnessId: "test-harness",
91+
modelSelectionLocked: true,
92+
},
93+
);
94+
await expect(
95+
api.runtime.subagent.run({ sessionKey: lockedSessionKey, message: "continue" }),
96+
).rejects.toThrow('owned by plugin "harness-owner"');
97+
expect(subagent.run).toHaveBeenCalledOnce();
98+
} finally {
99+
closeOpenClawAgentDatabasesForTest();
100+
}
101+
});
102+
});
103+
21104
it("keeps embedded incognito ID scans in the key's agent store", async () => {
22105
await withTempHome(async (home) => {
23106
const sessionKey = "agent:researcher:dashboard:incognito-ownership-check";

0 commit comments

Comments
 (0)