Skip to content

Commit 5888b6b

Browse files
committed
test(agents): prove queued turns survive credential reload
1 parent 3608623 commit 5888b6b

4 files changed

Lines changed: 80 additions & 10 deletions

File tree

‎src/agents/prepared-model-runtime-lease.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
hasConfiguredOwnerMatching,
99
ownerKey,
1010
normalizePreparedModelRuntimeInput,
11+
preparedModelRuntimeConfigsMatch,
1112
publishModelRuntimeSnapshot,
1213
rebindInputToCommittedConfiguredOwner,
1314
resolveConfiguredOwner,
@@ -96,7 +97,7 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
9697
!configuredOwner.needsRefresh &&
9798
borrowed &&
9899
borrowed.metadataSnapshot === options.pluginGeneration.pluginMetadataSnapshot &&
99-
borrowed.config === input.config &&
100+
preparedModelRuntimeConfigsMatch(borrowed.config, input.config) &&
100101
borrowed.agentId === input.agentId &&
101102
borrowed.agentDir === input.agentDir &&
102103
borrowed.inheritedAuthDir === input.inheritedAuthDir &&

‎src/agents/prepared-model-runtime.owner-selection.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ describe("prepared model runtime owner selection", () => {
375375
);
376376
expect(mocks.loadAgentRuntimePluginRegistryHandle).toHaveBeenCalledTimes(registryLoads);
377377
resumed.release();
378+
const clonedConfigLease = await acquireAgentRunPreparedModelRuntime(
379+
runInput(structuredClone(previousConfig)),
380+
{ pluginGeneration: admitted!.pluginGeneration },
381+
);
382+
expect(clonedConfigLease.snapshot).toBe(previousSnapshot);
383+
clonedConfigLease.release();
378384
await expect(
379385
acquireAgentRunPreparedModelRuntime(
380386
{ ...runInput(previousConfig), workspaceDir: "/tmp/different-workspace" },

‎test/e2e/qa-lab/runtime/fixtures/worker-inference-generation-provider/index.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function requireConfig(pluginConfig) {
1515
const barrierPath = pluginConfig?.barrierPath;
1616
const mockProviderBaseUrl = pluginConfig?.mockProviderBaseUrl;
1717
if (
18-
(generation !== "A" && generation !== "B" && generation !== "C" && generation !== "D") ||
18+
(generation !== "A" && generation !== "B" && generation !== "C") ||
1919
typeof tracePath !== "string" ||
2020
!tracePath ||
2121
typeof barrierPath !== "string" ||
@@ -68,8 +68,6 @@ export default definePluginEntry({
6868
if (typeof sourceCredential !== "string" || !sourceCredential) {
6969
throw new Error("qa worker generation provider requires a direct credential");
7070
}
71-
const runtimeCredential = `qa-worker-runtime-${generation}`;
72-
7371
trace("registered", { registrationMode: api.registrationMode });
7472
api.registerReload({
7573
hotPrefixes: [`plugins.entries.${PLUGIN_ID}.config`],
@@ -101,7 +99,10 @@ export default definePluginEntry({
10199
await waitForBarrierRelease(barrierPath);
102100
trace("auth-prepare-released", { waited: true });
103101
}
104-
trace("auth-ready", { runtimeCredentialGeneration: generation });
102+
const runtimeGeneration =
103+
typeof apiKey === "string" ? apiKey.slice(apiKey.lastIndexOf("-") + 1) : generation;
104+
const runtimeCredential = `qa-worker-runtime-${runtimeGeneration}`;
105+
trace("auth-ready", { runtimeCredentialGeneration: runtimeGeneration });
105106
return { apiKey: runtimeCredential };
106107
},
107108
createStreamFn: ({ model }) => {

‎test/e2e/qa-lab/runtime/worker-inference-generation-reload.ts‎

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,49 @@ async function hotPublishGeneration(params: {
279279
return { pidBefore: before.pid!, pidAfter: after.pid! };
280280
}
281281

282+
async function hotPublishChannelCredential(params: {
283+
gateway: WireGateway;
284+
generation: Generation;
285+
}): Promise<{ pidBefore: number; pidAfter: number }> {
286+
const before = (await params.gateway.call("system.info", {})) as { pid?: number };
287+
const previous = (await params.gateway.call("config.get", {})) as { hash?: string };
288+
const config = JSON.parse(await fs.readFile(params.gateway.configPath, "utf8")) as OpenClawConfig;
289+
const providerConfig = config.models?.providers?.[PROVIDER_ID];
290+
if (!providerConfig) {
291+
throw new Error("worker generation provider was missing before credential reload");
292+
}
293+
const next: OpenClawConfig = {
294+
...config,
295+
models: {
296+
...config.models,
297+
providers: {
298+
...config.models?.providers,
299+
[PROVIDER_ID]: {
300+
...providerConfig,
301+
apiKey: `${SOURCE_CREDENTIAL_PREFIX}-${params.generation}`,
302+
},
303+
},
304+
},
305+
};
306+
await fs.writeFile(params.gateway.configPath, `${JSON.stringify(next, null, 2)}\n`, "utf8");
307+
await waitFor(`generation ${params.generation} credential publication`, async () => {
308+
const current = (await params.gateway.call("config.get", {})) as {
309+
hash?: string;
310+
appliedConfigHash?: string;
311+
configRevisionHash?: string;
312+
};
313+
return current.hash !== previous.hash &&
314+
current.appliedConfigHash === current.configRevisionHash
315+
? current
316+
: undefined;
317+
});
318+
const after = (await params.gateway.call("system.info", {})) as { pid?: number };
319+
if (!Number.isSafeInteger(before.pid) || after.pid !== before.pid) {
320+
throw new Error(`credential hot publish replaced the Gateway: ${before.pid} -> ${after.pid}`);
321+
}
322+
return { pidBefore: before.pid!, pidAfter: after.pid! };
323+
}
324+
282325
async function startTurn(operator: GatewayClient, reply: string): Promise<string> {
283326
const runId = `${SCENARIO_ID}-${randomUUID()}`;
284327
const started = await operator.request<TurnResult>("chat.send", {
@@ -419,7 +462,12 @@ async function runProof(options: ProducerOptions) {
419462
published = await createPublishedWireWorkspace(root);
420463
gateway = await startQaGatewayChild({
421464
repoRoot: options.repoRoot,
422-
useRepoCli: true,
465+
command: {
466+
executablePath: process.execPath,
467+
argsPrefix: [path.join(options.repoRoot, "dist", "index.js")],
468+
cwd: options.repoRoot,
469+
usePackagedPlugins: true,
470+
},
423471
providerBaseUrl: `${authProxy.baseUrl}/v1`,
424472
providerMode: "mock-openai",
425473
primaryModel: MODEL_REF,
@@ -436,6 +484,15 @@ async function runProof(options: ProducerOptions) {
436484
barrierPath,
437485
mockProviderBaseUrl: `${authProxy.baseUrl}/v1`,
438486
}),
487+
channels: {
488+
...config.channels,
489+
"qa-channel": {
490+
...config.channels?.["qa-channel"],
491+
accounts: {
492+
parallel: { enabled: true, baseUrl: channelBus.baseUrl, allowFrom: ["*"] },
493+
},
494+
},
495+
},
439496
session: { ...config.session, dmScope: "per-peer" },
440497
}),
441498
});
@@ -543,8 +600,9 @@ async function runProof(options: ProducerOptions) {
543600
admitted: "CHANNEL-GENERATION-C-ADMITTED-OK",
544601
current: "CHANNEL-GENERATION-D-CURRENT-OK",
545602
};
546-
const sendChannelTurn = (conversationId: string, reply: string) =>
603+
const sendChannelTurn = (conversationId: string, reply: string, accountId = "default") =>
547604
channelState.addInboundMessage({
605+
accountId,
548606
conversation: { id: conversationId, kind: "direct" },
549607
senderId: conversationId,
550608
text: `Reply exactly: ${reply}`,
@@ -571,7 +629,9 @@ async function runProof(options: ProducerOptions) {
571629
event.event === "auth-prepare" && event.generation === "C" && event.waited === true,
572630
),
573631
);
574-
sendChannelTurn("generation-admitted", channelReplies.admitted);
632+
// Each QA account serializes its own inbound stream, so use a second account
633+
// to admit the victim while the first account's turn still holds the main lane.
634+
sendChannelTurn("generation-admitted", channelReplies.admitted, "parallel");
575635
const activeGateway = gateway;
576636
const queuedMainLane = await waitFor(
577637
"admitted generation C turn waiting for the global lane",
@@ -584,7 +644,9 @@ async function runProof(options: ProducerOptions) {
584644
);
585645
},
586646
);
587-
const hotPublishD = await hotPublishGeneration({ gateway, tracePath, generation: "D" });
647+
// A provider-only config reload replaces prepared owners without stopping
648+
// the active channel accounts, unlike a whole plugin-generation reload.
649+
const hotPublishD = await hotPublishChannelCredential({ gateway, generation: "D" });
588650
await fs.writeFile(barrierPath, "released\n", "utf8");
589651
await waitForChannelReply("generation-blocker", channelReplies.blocker);
590652
await waitForChannelReply("generation-admitted", channelReplies.admitted);
@@ -688,7 +750,7 @@ async function runProducer(options: ProducerOptions): Promise<QaEvidenceSummaryJ
688750
{ filePath: `${SCENARIO_ID}-trace.jsonl`, kind: "trace" },
689751
],
690752
details:
691-
"A baseline worker turn established the placement lifecycle; generation B then retained factory, policy, wrapper, and execution ownership while C hot-published, and the next turn used C",
753+
"Worker generations A, B, and C preserved provider ownership across plugin reload; two concurrent channel turns retained C while credential generation D committed, and the next channel turn used D",
692754
durationMs: Math.max(1, Date.now() - startedAt),
693755
status: "pass",
694756
});

0 commit comments

Comments
 (0)