Skip to content

Commit c5c7e70

Browse files
fix(qa): inject credential leasing into transport adapters
1 parent d00ec30 commit c5c7e70

9 files changed

Lines changed: 94 additions & 140 deletions

File tree

‎extensions/buzz/src/qa/adapter.runtime.test.ts‎

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {
22
QaBusInboundMessageInput,
33
QaBusMessage,
44
} from "openclaw/plugin-sdk/qa-channel-protocol";
5-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { beforeEach, describe, expect, it, vi } from "vitest";
66
import { parseBuzzQaCredentialPayload } from "./credentials.js";
77

88
const credentials = parseBuzzQaCredentialPayload({
@@ -15,7 +15,7 @@ const credentials = parseBuzzQaCredentialPayload({
1515
const release = vi.hoisted(() => vi.fn(async () => {}));
1616
const heartbeatStop = vi.hoisted(() => vi.fn(async () => {}));
1717
const acquireQaCredentialLease = vi.hoisted(() =>
18-
vi.fn(async () => ({
18+
vi.fn(async (_options: unknown) => ({
1919
source: "env" as const,
2020
kind: "buzz",
2121
payload: credentials,
@@ -26,7 +26,7 @@ const acquireQaCredentialLease = vi.hoisted(() =>
2626
})),
2727
);
2828
const startQaCredentialLeaseHeartbeat = vi.hoisted(() =>
29-
vi.fn(() => ({
29+
vi.fn((_lease: unknown) => ({
3030
getFailure: () => null,
3131
stop: heartbeatStop,
3232
throwIfFailed: vi.fn(),
@@ -62,10 +62,6 @@ const createBuzzQaRelayDriver = vi.hoisted(() =>
6262
}),
6363
);
6464

65-
vi.mock("openclaw/plugin-sdk/qa-runtime", () => ({
66-
acquireQaCredentialLease,
67-
startQaCredentialLeaseHeartbeat,
68-
}));
6965
vi.mock("./credentials.js", async (importOriginal) => ({
7066
...(await importOriginal<typeof import("./credentials.js")>()),
7167
readBuzzQaCredentialFile,
@@ -74,9 +70,13 @@ vi.mock("./relay-client.js", () => ({ createBuzzQaRelayDriver }));
7470

7571
import { createBuzzQaTransportAdapter } from "./adapter.runtime.js";
7672

73+
const credentialHost: Parameters<typeof createBuzzQaTransportAdapter>[0]["credentials"] = {
74+
acquire: async (options) => (await acquireQaCredentialLease(options)) as never,
75+
startHeartbeat: startQaCredentialLeaseHeartbeat,
76+
};
77+
7778
describe("Buzz QA transport adapter", () => {
7879
beforeEach(() => {
79-
vi.stubEnv("OPENCLAW_QA_CREDENTIAL_SOURCE", "");
8080
vi.clearAllMocks();
8181
relayDriverState.onMessage = undefined;
8282
sendMessage.mockResolvedValue({
@@ -86,17 +86,14 @@ describe("Buzz QA transport adapter", () => {
8686
readBuzzQaCredentialFile.mockResolvedValue(credentials);
8787
});
8888

89-
afterEach(() => {
90-
vi.unstubAllEnvs();
91-
});
92-
9389
it("uses private file credentials by default", async () => {
9490
await createBuzzQaTransportAdapter({
9591
adapterOptions: {
9692
credentialFile: "private/buzz-qa.json",
9793
repoRoot: "/repo",
9894
},
9995
channelId: "buzz",
96+
credentials: credentialHost,
10097
driver: "live",
10198
messages: {
10299
addInboundMessage: vi.fn(),
@@ -115,26 +112,6 @@ describe("Buzz QA transport adapter", () => {
115112
);
116113
});
117114

118-
it("honors the shared credential source environment override", async () => {
119-
vi.stubEnv("OPENCLAW_QA_CREDENTIAL_SOURCE", "convex");
120-
121-
await createBuzzQaTransportAdapter({
122-
channelId: "buzz",
123-
driver: "live",
124-
messages: {
125-
addInboundMessage: vi.fn(),
126-
addOutboundMessage: vi.fn(),
127-
editMessage: vi.fn(),
128-
},
129-
outputDir: ".artifacts/qa-e2e/buzz",
130-
});
131-
132-
expect(readBuzzQaCredentialFile).not.toHaveBeenCalled();
133-
expect(acquireQaCredentialLease).toHaveBeenCalledWith(
134-
expect.objectContaining({ kind: "buzz", source: "convex" }),
135-
);
136-
});
137-
138115
it("sends a portable mentioned message through the native Buzz relay driver", async () => {
139116
const addInboundMessage = vi.fn(async (input) => ({
140117
...input,
@@ -145,6 +122,7 @@ describe("Buzz QA transport adapter", () => {
145122
const adapter = await createBuzzQaTransportAdapter({
146123
adapterOptions: { credentialSource: "convex", sutAccountId: "sut" },
147124
channelId: "buzz",
125+
credentials: credentialHost,
148126
driver: "live",
149127
messages: {
150128
addInboundMessage,
@@ -209,6 +187,7 @@ describe("Buzz QA transport adapter", () => {
209187
const adapter = await createBuzzQaTransportAdapter({
210188
adapterOptions: { credentialSource: "convex", sutAccountId: "sut" },
211189
channelId: "buzz",
190+
credentials: credentialHost,
212191
driver: "live",
213192
messages: {
214193
addInboundMessage,
@@ -292,6 +271,7 @@ describe("Buzz QA transport adapter", () => {
292271
const adapter = await createBuzzQaTransportAdapter({
293272
adapterOptions: { credentialSource: "convex", sutAccountId: "sut" },
294273
channelId: "buzz",
274+
credentials: credentialHost,
295275
driver: "live",
296276
messages: {
297277
addInboundMessage,
@@ -337,6 +317,7 @@ describe("Buzz QA transport adapter", () => {
337317
const adapter = await createBuzzQaTransportAdapter({
338318
adapterOptions: { credentialSource: "convex" },
339319
channelId: "buzz",
320+
credentials: credentialHost,
340321
driver: "live",
341322
messages: {
342323
addInboundMessage: vi.fn(),

‎extensions/buzz/src/qa/adapter.runtime.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { setTimeout as sleep } from "node:timers/promises";
22
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
33
import type { QaRunnerCliRegistration } from "openclaw/plugin-sdk/qa-runner-runtime";
4-
import {
5-
acquireQaCredentialLease,
6-
startQaCredentialLeaseHeartbeat,
7-
} from "openclaw/plugin-sdk/qa-runtime";
84
import type { BuzzInboundMessage } from "../message-event.js";
95
import { buildBuzzTarget } from "../target.js";
106
import { parseBuzzQaCredentialPayload, readBuzzQaCredentialFile } from "./credentials.js";
@@ -57,10 +53,7 @@ export async function createBuzzQaTransportAdapter(
5753
context: FactoryContext,
5854
): Promise<AdapterDefinition> {
5955
const options = context.adapterOptions ?? {};
60-
const requestedCredentialSource =
61-
options.credentialSource?.trim().toLowerCase() ||
62-
process.env.OPENCLAW_QA_CREDENTIAL_SOURCE?.trim().toLowerCase() ||
63-
"file";
56+
const requestedCredentialSource = options.credentialSource?.trim().toLowerCase() || "file";
6457
if (requestedCredentialSource !== "file" && requestedCredentialSource !== "convex") {
6558
throw new Error('Buzz QA credential source must be "file" or "convex".');
6659
}
@@ -80,7 +73,7 @@ export async function createBuzzQaTransportAdapter(
8073
requestedCredentialSource === "file" && credentialFile
8174
? await readBuzzQaCredentialFile({ filePath: credentialFile, repoRoot: options.repoRoot })
8275
: undefined;
83-
const lease = await acquireQaCredentialLease({
76+
const lease = await context.credentials.acquire({
8477
kind: "buzz",
8578
source: requestedCredentialSource === "convex" ? "convex" : "env",
8679
role: options.credentialRole,
@@ -92,7 +85,7 @@ export async function createBuzzQaTransportAdapter(
9285
},
9386
parsePayload: parseBuzzQaCredentialPayload,
9487
});
95-
const heartbeat = startQaCredentialLeaseHeartbeat(lease);
88+
const heartbeat = context.credentials.startHeartbeat(lease);
9689
const credentials = lease.payload;
9790
const accountId = options.sutAccountId?.trim() || "sut";
9891
const nativeMessageIds = new Map<string, string>();

‎extensions/qa-lab/src/live-transports/live-transport-adapters.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ describe("live transport adapter factories", () => {
9191
expect.objectContaining({
9292
adapterOptions,
9393
channelId,
94+
credentials: {
95+
acquire: expect.any(Function),
96+
startHeartbeat: expect.any(Function),
97+
},
9498
driver: "live",
9599
messages: expect.objectContaining({
96100
addInboundMessage: expect.any(Function),

‎extensions/qa-lab/src/live-transports/shared/live-transport-suite.runtime.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22

33
const runQaSuiteCommand = vi.hoisted(() => vi.fn());
44

@@ -8,9 +8,14 @@ import { runLiveTransportQaSuiteCommand } from "./live-transport-suite.runtime.j
88

99
describe("live transport suite runtime", () => {
1010
beforeEach(() => {
11+
vi.stubEnv("OPENCLAW_QA_CREDENTIAL_SOURCE", "");
1112
vi.clearAllMocks();
1213
});
1314

15+
afterEach(() => {
16+
vi.unstubAllEnvs();
17+
});
18+
1419
it("normalizes one live command into the shared suite host", async () => {
1520
await runLiveTransportQaSuiteCommand({
1621
channelId: "slack",
@@ -73,6 +78,21 @@ describe("live transport suite runtime", () => {
7378
);
7479
});
7580

81+
it("normalizes the shared credential source environment override", async () => {
82+
vi.stubEnv("OPENCLAW_QA_CREDENTIAL_SOURCE", " convex ");
83+
84+
await runLiveTransportQaSuiteCommand({
85+
channelId: "buzz",
86+
defaultProviderMode: "mock-openai",
87+
options: {},
88+
selectScenarioIds: () => ["channel-canary"],
89+
});
90+
91+
expect(runQaSuiteCommand).toHaveBeenCalledWith(
92+
expect.objectContaining({ credentialSource: "convex" }),
93+
);
94+
});
95+
7696
it("rejects shared credentials for disposable transports", async () => {
7797
await expect(
7898
runLiveTransportQaSuiteCommand({

‎extensions/qa-lab/src/live-transports/shared/live-transport-suite.runtime.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ export async function runLiveTransportQaSuiteCommand(params: {
2020
selectScenarioIds: LiveTransportScenarioSelection;
2121
}) {
2222
const options = params.options;
23+
const credentialSource =
24+
options.credentialSource?.trim() || process.env.OPENCLAW_QA_CREDENTIAL_SOURCE?.trim();
2325
if (params.credentialMode === "env-only") {
2426
const laneLabel = params.laneLabel ?? params.channelId;
25-
const credentialSource = options.credentialSource?.trim().toLowerCase();
26-
if (credentialSource && credentialSource !== "env") {
27+
if (credentialSource && credentialSource.toLowerCase() !== "env") {
2728
throw new Error(
2829
`QA Lab ${laneLabel} supports only --credential-source env${params.envCredentialReason ? ` because ${params.envCredentialReason}` : "."}`,
2930
);
@@ -62,7 +63,7 @@ export async function runLiveTransportQaSuiteCommand(params: {
6263
...(params.credentialMode === "env-only"
6364
? {}
6465
: {
65-
credentialSource: options.credentialSource?.trim(),
66+
credentialSource,
6667
credentialRole: options.credentialRole?.trim(),
6768
}),
6869
explicitScenarioSelection: Boolean(options.scenarioIds?.length),

‎extensions/qa-lab/src/qa-transport-registry.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { QaRunnerCliRegistration } from "openclaw/plugin-sdk/qa-runner-runtime";
22
// Qa Lab plugin module implements qa transport registry behavior.
33
import type { QaBusState } from "./bus-state.js";
4+
import {
5+
acquireQaCredentialLease,
6+
startQaCredentialLeaseHeartbeat,
7+
} from "./live-transports/shared/credential-lease.runtime.js";
48
import {
59
createQaChannelTransport,
610
QA_CHANNEL_DEFAULT_SUITE_CONCURRENCY,
@@ -102,6 +106,10 @@ function createQaTransportAdapterFactoryRegistry(
102106
const definition = await factory.create({
103107
adapterOptions: context.adapterOptions,
104108
channelId: context.channelId,
109+
credentials: {
110+
acquire: acquireQaCredentialLease,
111+
startHeartbeat: startQaCredentialLeaseHeartbeat,
112+
},
105113
driver: context.driver,
106114
messages: {
107115
addInboundMessage: (input) => context.state.addInboundMessage(input),

‎src/plugin-sdk/qa-runner-runtime.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,46 @@ type QaRunnerMessageRecorder = {
4141
editMessage: (input: QaBusEditMessageInput) => QaBusMessage | Promise<QaBusMessage>;
4242
};
4343

44+
type QaRunnerCredentialLease<TPayload> = {
45+
credentialId?: string;
46+
heartbeat(): Promise<void>;
47+
heartbeatIntervalMs: number;
48+
kind: string;
49+
leaseToken?: string;
50+
leaseTtlMs: number;
51+
ownerId?: string;
52+
payload: TPayload;
53+
release(): Promise<void>;
54+
role?: "ci" | "maintainer";
55+
source: "convex" | "env";
56+
};
57+
58+
type QaRunnerCredentialLeaseOptions<TPayload> = {
59+
kind: string;
60+
parsePayload: (payload: unknown) => TPayload;
61+
resolveEnvPayload: () => TPayload;
62+
role?: string;
63+
source?: string;
64+
};
65+
66+
type QaRunnerCredentialHeartbeat = {
67+
getFailure(): Error | null;
68+
stop(): Promise<void>;
69+
throwIfFailed(): void;
70+
};
71+
72+
type QaRunnerCredentialHost = {
73+
acquire<TPayload>(
74+
options: QaRunnerCredentialLeaseOptions<TPayload>,
75+
): Promise<QaRunnerCredentialLease<TPayload>>;
76+
startHeartbeat(
77+
lease: Pick<
78+
QaRunnerCredentialLease<unknown>,
79+
"heartbeat" | "heartbeatIntervalMs" | "kind" | "source"
80+
>,
81+
): QaRunnerCredentialHeartbeat;
82+
};
83+
4484
type QaRunnerTransportFlowPreparationInput = {
4585
config: Record<string, unknown>;
4686
scenarioId: string;
@@ -148,6 +188,7 @@ type QaRunnerTransportFactory = {
148188
create: (context: {
149189
adapterOptions?: QaRunnerAdapterOptions;
150190
channelId: string;
191+
credentials: QaRunnerCredentialHost;
151192
driver: string;
152193
messages: QaRunnerMessageRecorder;
153194
outputDir: string;

‎src/plugin-sdk/qa-runtime.test.ts‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -142,40 +142,6 @@ describe("plugin-sdk qa-runtime", () => {
142142
});
143143
});
144144

145-
it("leases plugin-owned transport credentials through the private QA host", async () => {
146-
const lease = {
147-
source: "env" as const,
148-
kind: "buzz",
149-
payload: { roomId: "qa-room" },
150-
heartbeatIntervalMs: 0,
151-
leaseTtlMs: 0,
152-
heartbeat: vi.fn(async () => {}),
153-
release: vi.fn(async () => {}),
154-
};
155-
const acquireQaCredentialLease = vi.fn(async () => lease);
156-
const heartbeat = {
157-
getFailure: vi.fn(() => null),
158-
stop: vi.fn(async () => {}),
159-
throwIfFailed: vi.fn(),
160-
};
161-
const startQaCredentialLeaseHeartbeat = vi.fn(() => heartbeat);
162-
loadBundledPluginPublicSurfaceModuleSync.mockReturnValue({
163-
acquireQaCredentialLease,
164-
startQaCredentialLeaseHeartbeat,
165-
});
166-
const module = await import("./qa-runtime.js");
167-
const options = {
168-
kind: "buzz",
169-
parsePayload: (payload: unknown) => payload as { roomId: string },
170-
resolveEnvPayload: () => ({ roomId: "qa-room" }),
171-
};
172-
173-
await expect(module.acquireQaCredentialLease(options)).resolves.toBe(lease);
174-
expect(module.startQaCredentialLeaseHeartbeat(lease)).toBe(heartbeat);
175-
expect(acquireQaCredentialLease).toHaveBeenCalledWith(options);
176-
expect(startQaCredentialLeaseHeartbeat).toHaveBeenCalledWith(lease, undefined);
177-
});
178-
179145
it("registers shared live transport QA CLI options", async () => {
180146
const module = await import("./qa-runtime.js");
181147
const run = vi.fn(async () => {});

0 commit comments

Comments
 (0)