Skip to content

Commit 04601ae

Browse files
authored
refactor(qa): simplify transport adapter contracts (#99632)
* feat(qa): add transport capability contracts * fix(qa): scope sequence support by channel * refactor(qa): simplify transport adapter contracts * fix(qa): clarify unsupported transport method errors
1 parent 677c8ff commit 04601ae

15 files changed

Lines changed: 401 additions & 249 deletions

‎extensions/qa-lab/src/crabline-transport.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ describe("crabline transport", () => {
7171
});
7272

7373
try {
74-
await transport.sendNativeCommand({
74+
expect(transport.sendNativeCommand).toBeTypeOf("function");
75+
await transport.sendNativeCommand?.({
7576
command: "stop",
7677
conversation: { id: "alice", kind: "direct" },
7778
senderId: "alice",
@@ -218,8 +219,9 @@ describe("crabline transport", () => {
218219
text: "final marker",
219220
});
220221

222+
expect(transport.waitForOutboundSequence).toBeTypeOf("function");
221223
await expect(
222-
transport.waitForOutboundSequence({
224+
transport.waitForOutboundSequence!({
223225
conversationId: "-1001234567890",
224226
finalSettleMs: 0,
225227
finalTextIncludes: "final marker",
@@ -248,6 +250,8 @@ describe("crabline transport", () => {
248250
try {
249251
expect(transport.id).toBe("crabline");
250252
expect(transport.requiredPluginIds).toEqual(["slack"]);
253+
expect(transport.sendNativeCommand).toBeUndefined();
254+
expect(transport.waitForOutboundSequence).toBeUndefined();
251255
expect(transport.createGatewayConfig({ baseUrl: "http://127.0.0.1:1" })).toMatchObject({
252256
channels: {
253257
slack: {

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ class QaCrablineTransport extends QaStateBackedTransportAdapter {
338338
readonly #adapter: StartedOpenClawCrablineAdapter;
339339
readonly #selection: OpenClawCrablineChannelDriverSelection;
340340
readonly #state: QaCrablineTransportState;
341+
readonly sendNativeCommand?: (input: QaTransportNativeCommandInput) => Promise<void>;
342+
readonly waitForOutboundSequence?: (input: QaTransportOutboundSequenceMatch) => Promise<{
343+
events: QaTransportOutboundEvent[];
344+
final: QaBusMessage;
345+
}>;
341346

342347
constructor(params: {
343348
adapter: StartedOpenClawCrablineAdapter;
@@ -354,6 +359,21 @@ class QaCrablineTransport extends QaStateBackedTransportAdapter {
354359
this.#adapter = params.adapter;
355360
this.#selection = params.selection;
356361
this.#state = params.state;
362+
if (params.selection.channel === "telegram") {
363+
this.sendNativeCommand = async (input) => {
364+
const { command, ...message } = input;
365+
await this.sendInbound({
366+
...message,
367+
text: `/${command}`,
368+
nativeCommand: { name: command },
369+
});
370+
};
371+
this.waitForOutboundSequence = async (input) =>
372+
await waitForQaTransportOutboundSequence({
373+
input,
374+
readEvents: () => this.#state.getOutboundEvents(),
375+
});
376+
}
357377
}
358378

359379
createGatewayConfig = (params: { baseUrl: string }): QaTransportGatewayConfig =>
@@ -378,27 +398,6 @@ class QaCrablineTransport extends QaStateBackedTransportAdapter {
378398

379399
createRuntimeEnvPatch = () => this.#adapter.createChannelDriverSmokeEnv({});
380400

381-
override async sendNativeCommand(input: QaTransportNativeCommandInput): Promise<void> {
382-
if (this.#selection.channel !== "telegram") {
383-
throw new Error(
384-
`Crabline ${this.#selection.channel} does not support native command injection.`,
385-
);
386-
}
387-
const { command, ...message } = input;
388-
await this.sendInbound({
389-
...message,
390-
text: `/${command}`,
391-
nativeCommand: { name: command },
392-
});
393-
}
394-
395-
override async waitForOutboundSequence(input: QaTransportOutboundSequenceMatch) {
396-
return await waitForQaTransportOutboundSequence({
397-
input,
398-
readEvents: () => this.#state.getOutboundEvents(),
399-
});
400-
}
401-
402401
handleAction = async (_params: {
403402
action: QaTransportActionName;
404403
args: Record<string, unknown>;

‎extensions/qa-lab/src/manual-lane.runtime.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export async function runQaManualLane(params: QaManualLaneParams) {
7777
let gateway: Awaited<ReturnType<typeof startQaGatewayChild>> | undefined;
7878
let lab: Awaited<ReturnType<typeof startQaLabServer>> | undefined;
7979
let mock: Awaited<ReturnType<typeof startQaProviderServer>> | undefined;
80+
let transportCleanup: (() => Promise<void>) | undefined;
8081
let result: ManualLaneResult | undefined;
8182
let cleanupError: Error | undefined;
8283
let runError: unknown;
@@ -86,10 +87,14 @@ export async function runQaManualLane(params: QaManualLaneParams) {
8687
repoRoot: params.repoRoot,
8788
embeddedGateway: "disabled",
8889
});
89-
const transport = createQaTransportAdapter({
90-
id: params.transportId ?? "qa-channel",
90+
const transportFactoryResult = await createQaTransportAdapter({
91+
channelId: params.transportId ?? "qa-channel",
92+
driver: params.transportId ?? "qa-channel",
93+
outputDir: params.repoRoot,
9194
state: lab.state,
9295
});
96+
const transport = transportFactoryResult.adapter;
97+
transportCleanup = transportFactoryResult.cleanup;
9398
mock = await startQaProviderServer(params.providerMode);
9499
gateway = await startQaGatewayChild({
95100
repoRoot: params.repoRoot,
@@ -164,7 +169,12 @@ export async function runQaManualLane(params: QaManualLaneParams) {
164169
} catch (error) {
165170
runError = error;
166171
} finally {
167-
cleanupError = await stopManualLaneResources({ gateway, lab, mock });
172+
let transportCleanupError: Error | undefined;
173+
await transportCleanup?.().catch((error: unknown) => {
174+
transportCleanupError = normalizeManualLaneCleanupError(error);
175+
});
176+
const resourceCleanupError = await stopManualLaneResources({ gateway, lab, mock });
177+
cleanupError = transportCleanupError ?? resourceCleanupError;
168178
}
169179
if (runError) {
170180
throw new Error(formatErrorMessage(runError), { cause: runError });

‎extensions/qa-lab/src/qa-channel-transport.test.ts‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ describe("qa channel transport", () => {
9898
).rejects.toThrow("last probe error: channels.status exploded");
9999
});
100100

101-
it("inherits the shared normalized message capabilities", async () => {
101+
it("uses the shared normalized message state", async () => {
102102
const transport = createQaChannelTransport(createQaBusState());
103103

104-
const inbound = await transport.capabilities.sendInboundMessage({
104+
const inbound = await transport.sendInbound({
105105
accountId: "default",
106106
conversation: { id: "dm:qa-operator", kind: "direct" },
107107
senderId: "qa-operator",
108108
text: "hello from the operator",
109109
});
110110

111-
expect(transport.capabilities.getNormalizedMessageState().messages).toHaveLength(1);
112-
const message = await transport.capabilities.readNormalizedMessage({
111+
expect(transport.state.getSnapshot().messages).toHaveLength(1);
112+
const message = await transport.state.readMessage({
113113
messageId: inbound.id,
114114
});
115115
if (!message) {
@@ -161,11 +161,11 @@ describe("qa channel transport", () => {
161161
let injected = false;
162162

163163
await expect(
164-
transport.capabilities.waitForCondition(
164+
transport.waitForCondition(
165165
async () => {
166166
if (!injected) {
167167
injected = true;
168-
await transport.capabilities.injectOutboundMessage({
168+
await transport.state.addOutboundMessage({
169169
accountId: "default",
170170
to: "dm:qa-operator",
171171
text: "⚠️ agent failed before reply: synthetic failure for wait helper",
@@ -182,22 +182,20 @@ describe("qa channel transport", () => {
182182
it("captures a fresh failure cursor for each wait helper call", async () => {
183183
const transport = createQaChannelTransport(createQaBusState());
184184

185-
await transport.capabilities.injectOutboundMessage({
185+
await transport.state.addOutboundMessage({
186186
accountId: "default",
187187
to: "dm:qa-operator",
188188
text: "⚠️ agent failed before reply: stale failure should not leak",
189189
});
190190

191-
await expect(transport.capabilities.waitForCondition(async () => "ok", 50, 10)).resolves.toBe(
192-
"ok",
193-
);
191+
await expect(transport.waitForCondition(async () => "ok", 50, 10)).resolves.toBe("ok");
194192
});
195193

196194
it("keeps oversized wait helper intervals within the timeout", async () => {
197195
const transport = createQaChannelTransport(createQaBusState());
198196

199197
await expect(
200-
transport.capabilities.waitForCondition(async () => undefined, 5, Number.MAX_SAFE_INTEGER),
198+
transport.waitForCondition(async () => undefined, 5, Number.MAX_SAFE_INTEGER),
201199
).rejects.toThrow("timed out after 5ms");
202200
});
203201
});

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
55
import type { QaBusState } from "./bus-state.js";
66
import { QaSuiteInfraError } from "./errors.js";
77
import { getQaProvider } from "./providers/index.js";
8-
import { QaStateBackedTransportAdapter } from "./qa-transport.js";
8+
import {
9+
QaStateBackedTransportAdapter,
10+
waitForQaTransportOutboundSequence,
11+
} from "./qa-transport.js";
912
import type {
1013
QaTransportActionName,
1114
QaTransportGatewayConfig,
1215
QaTransportGatewayClient,
1316
QaTransportNativeCommandInput,
17+
QaTransportOutboundSequenceMatch,
1418
QaTransportReportParams,
1519
} from "./qa-transport.js";
1620
import { qaChannelPlugin } from "./runtime-api.js";
@@ -148,14 +152,20 @@ class QaChannelTransport extends QaStateBackedTransportAdapter {
148152
replyChannel: QA_CHANNEL_ID,
149153
replyTo: target,
150154
});
151-
override async sendNativeCommand(input: QaTransportNativeCommandInput): Promise<void> {
155+
async sendNativeCommand(input: QaTransportNativeCommandInput): Promise<void> {
152156
const { command, ...message } = input;
153157
await this.sendInbound({
154158
...message,
155159
text: `/${command}`,
156160
nativeCommand: { name: command },
157161
});
158162
}
163+
async waitForOutboundSequence(input: QaTransportOutboundSequenceMatch) {
164+
return await waitForQaTransportOutboundSequence({
165+
input,
166+
readEvents: () => this.state.getSnapshot().events,
167+
});
168+
}
159169
handleAction = handleQaChannelAction;
160170
createReportNotes = createQaChannelReportNotes;
161171
}
Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
// Qa Lab tests cover qa transport registry plugin behavior.
2-
import { describe, expect, it } from "vitest";
3-
import { normalizeQaTransportId } from "./qa-transport-registry.js";
2+
import { describe, expect, it, vi } from "vitest";
3+
import { createQaBusState } from "./bus-state.js";
4+
import { createQaChannelTransport } from "./qa-channel-transport.js";
5+
import {
6+
createQaTransportAdapter,
7+
createQaTransportAdapterFactoryRegistry,
8+
normalizeQaTransportId,
9+
type QaTransportAdapterFactory,
10+
type QaTransportFactoryContext,
11+
} from "./qa-transport-registry.js";
12+
import type { QaTransportAdapter } from "./qa-transport.js";
13+
14+
function createFactoryContext(
15+
overrides: Partial<QaTransportFactoryContext> = {},
16+
): QaTransportFactoryContext {
17+
return {
18+
channelId: "qa-channel",
19+
driver: "qa-channel",
20+
outputDir: ".artifacts/qa-e2e/transport-contract-test",
21+
state: createQaBusState(),
22+
...overrides,
23+
};
24+
}
425

526
describe("qa transport registry", () => {
627
it("rejects inherited prototype keys as unsupported transport ids", () => {
@@ -9,4 +30,68 @@ describe("qa transport registry", () => {
930
"unsupported QA transport: __proto__",
1031
);
1132
});
33+
34+
it("creates QA Channel through the default async registry", async () => {
35+
const created = await createQaTransportAdapter(createFactoryContext());
36+
37+
expect(created.adapter.id).toBe("qa-channel");
38+
await created.cleanup();
39+
});
40+
41+
it("selects an injected matching factory", async () => {
42+
const adapter = createQaChannelTransport(createQaBusState());
43+
const skippedCreate = vi.fn(async () => adapter);
44+
const selectedCreate = vi.fn(async () => adapter);
45+
const factories: QaTransportAdapterFactory[] = [
46+
{ id: "skipped", matches: () => false, create: skippedCreate },
47+
{ id: "selected", matches: () => true, create: selectedCreate },
48+
];
49+
const registry = createQaTransportAdapterFactoryRegistry(factories);
50+
51+
const created = await registry.create(createFactoryContext());
52+
53+
expect(created.adapter).toBe(adapter);
54+
expect(skippedCreate).not.toHaveBeenCalled();
55+
expect(selectedCreate).toHaveBeenCalledOnce();
56+
});
57+
58+
it("returns cleanup owned by the selected adapter", async () => {
59+
const cleanup = vi.fn(async () => undefined);
60+
const adapter: QaTransportAdapter = createQaChannelTransport(createQaBusState());
61+
adapter.cleanup = cleanup;
62+
const factory: QaTransportAdapterFactory = {
63+
id: "cleanup",
64+
matches: () => true,
65+
async create() {
66+
return adapter;
67+
},
68+
};
69+
const registry = createQaTransportAdapterFactoryRegistry([factory]);
70+
const created = await registry.create(createFactoryContext());
71+
72+
await created.cleanup();
73+
74+
expect(cleanup).toHaveBeenCalledOnce();
75+
});
76+
77+
it("reports no-match and startup failures with transport context", async () => {
78+
const context = createFactoryContext();
79+
const emptyRegistry = createQaTransportAdapterFactoryRegistry([]);
80+
await expect(emptyRegistry.create(context)).rejects.toThrow(
81+
"no QA transport factory for qa-channel:qa-channel",
82+
);
83+
84+
const brokenRegistry = createQaTransportAdapterFactoryRegistry([
85+
{
86+
id: "broken",
87+
matches: () => true,
88+
async create() {
89+
throw new Error("provider boot failed");
90+
},
91+
},
92+
]);
93+
await expect(brokenRegistry.create(context)).rejects.toThrow(
94+
"broken failed to create QA transport qa-channel:qa-channel: provider boot failed",
95+
);
96+
});
1297
});

0 commit comments

Comments
 (0)