Skip to content

Commit 4c775be

Browse files
committed
refactor(qa): share Matrix flows across drivers
1 parent 6d84622 commit 4c775be

13 files changed

Lines changed: 373 additions & 141 deletions

‎extensions/qa-lab/src/ci-smoke-plan.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { OPENCLAW_CRABLINE_DEFAULT_CHANNEL } from "@openclaw/crabline";
33
import { defaultQaModelForMode, normalizeQaProviderMode } from "./model-selection.js";
44
import { readQaScenarioPack } from "./scenario-catalog.js";
5+
import { scenarioMatchesQaProviderLane } from "./scenario-lane.js";
56
import { readQaScorecardTaxonomyReport } from "./scorecard-taxonomy.js";
6-
import { scenarioMatchesQaProviderLane } from "./suite-planning.js";
77

88
const QA_SMOKE_PROFILE = "smoke-ci";
99
const QA_SMOKE_CI_PARTS = ["profile-1", "profile-2"] as const;
@@ -73,6 +73,7 @@ export function createQaSmokeCiPart(partId: string): QaSmokeCiPart {
7373
providerMode,
7474
primaryModel,
7575
channelDriver: profile.channelDriver,
76+
channel: scenario.execution.channel ?? OPENCLAW_CRABLINE_DEFAULT_CHANNEL,
7677
}),
7778
);
7879
if (scenarios.length === 0) {

‎extensions/qa-lab/src/cli.runtime.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
readQaScenarioPack,
7676
type QaRuntimeParityTier,
7777
} from "./scenario-catalog.js";
78+
import { scenarioMatchesQaProviderLane } from "./scenario-lane.js";
7879
import { resolveQaScenarioPackScenarioIds } from "./scenario-packs.js";
7980
import { attachQaProfileScorecardEvidenceToFile } from "./scorecard-evidence.js";
8081
import {
@@ -86,11 +87,7 @@ import {
8687
} from "./scorecard-taxonomy.js";
8788
import { isQaSelfCheckSuccessful } from "./self-check.js";
8889
import { runQaFlowSuiteFromRuntime, runQaSuite } from "./suite-launch.runtime.js";
89-
import {
90-
resolveQaSuiteScenarioChannel,
91-
resolveQaSuiteScenarioChannels,
92-
scenarioMatchesQaProviderLane,
93-
} from "./suite-planning.js";
90+
import { resolveQaSuiteScenarioChannel, resolveQaSuiteScenarioChannels } from "./suite-planning.js";
9491
import { readQaSuiteFailedOrSkippedScenarioCountFromFile } from "./suite-summary.js";
9592
import {
9693
buildTokenEfficiencyReport,
@@ -353,6 +350,7 @@ function resolveQaRuntimeParityTierScenarioIds(params: {
353350
providerMode: params.providerMode,
354351
primaryModel: params.primaryModel,
355352
channelDriver: params.channelDriver,
353+
channel: scenario.execution.channel,
356354
claudeCliAuthMode: params.claudeCliAuthMode,
357355
}),
358356
);
@@ -796,6 +794,7 @@ export async function runQaProfileCommand(opts: QaProfileCommandOptions) {
796794
providerMode: normalizedProviderMode,
797795
primaryModel,
798796
channelDriver: profileReport.channelDriver,
797+
channel: scenario.execution.channel,
799798
}),
800799
);
801800
if (scenarios.length === 0) {
@@ -863,6 +862,7 @@ function selectQaScenarioDefinitionsForChannelResolution(params: {
863862
providerMode: params.providerMode,
864863
primaryModel: params.primaryModel,
865864
channelDriver: params.channelDriver,
865+
channel: scenario.execution.channel,
866866
claudeCliAuthMode: params.claudeCliAuthMode,
867867
}),
868868
);

‎extensions/qa-lab/src/coverage-report.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,34 @@ describe("qa coverage report", () => {
340340
);
341341
});
342342

343+
it("uses the live lane for channel scenarios without a driver restriction", () => {
344+
const matches = findQaScenarioMatches(readQaScenarioPack().scenarios, "dm-per-room-session");
345+
const report = renderQaScenarioMatchesMarkdownReport({
346+
query: "dm-per-room-session",
347+
matches,
348+
});
349+
350+
expect(report).toContain(
351+
"- Suite command: `pnpm openclaw qa suite --channel-driver live --channel matrix --scenario dm-per-room-session`",
352+
);
353+
});
354+
355+
it("splits flow commands across channel lanes", () => {
356+
const scenarios = readQaScenarioPack().scenarios;
357+
const matches = [
358+
...findQaScenarioMatches(scenarios, "dm-per-room-session"),
359+
...findQaScenarioMatches(scenarios, "whatsapp-access-control-group-disabled"),
360+
];
361+
const report = renderQaScenarioMatchesMarkdownReport({ query: "channel lanes", matches });
362+
363+
expect(report).toContain(
364+
"--channel-driver live --channel matrix --scenario dm-per-room-session",
365+
);
366+
expect(report).toContain(
367+
"--channel-driver live --channel whatsapp --scenario whatsapp-access-control-group-disabled",
368+
);
369+
});
370+
343371
it("splits qa suite targets when matches mix execution kinds", () => {
344372
const playwrightExecutionPath = "ui/src/e2e/chat-flow.e2e.test.ts";
345373
const flowScenario = scenarioWithCoverage({

‎extensions/qa-lab/src/coverage-report.ts‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -481,47 +481,43 @@ function formatOptionalScenarioMetadata(match: QaScenarioSearchMatch) {
481481
return metadata.length > 0 ? metadata.join("; ") : "none";
482482
}
483483

484+
function uniqueScenarioValues(values: (string | undefined)[]) {
485+
return [...new Set(values.filter((value): value is string => Boolean(value)))];
486+
}
487+
484488
function formatSuiteCommand(matches: readonly QaScenarioSearchMatch[]) {
485489
const scenarioArgs = matches.map((match) => `--scenario ${match.id}`).join(" ");
486-
const requiredDrivers = [
487-
...new Set(
488-
matches
489-
.map((match) => match.requiredChannelDriver)
490-
.filter((driver): driver is string => Boolean(driver)),
491-
),
492-
];
490+
const requiredDrivers = uniqueScenarioValues(matches.map((match) => match.requiredChannelDriver));
491+
const channels = uniqueScenarioValues(matches.map((match) => match.channel));
492+
const selectedDriver =
493+
requiredDrivers.length === 1 ? requiredDrivers[0] : channels.length === 1 ? "live" : undefined;
493494
const driverArg =
494-
requiredDrivers.length === 1 && requiredDrivers[0] !== "qa-channel"
495-
? ` --channel-driver ${requiredDrivers[0]}`
496-
: "";
497-
const channels = [
498-
...new Set(
499-
matches
500-
.map((match) => match.channel)
501-
.filter((channel): channel is string => Boolean(channel)),
502-
),
503-
];
495+
selectedDriver && selectedDriver !== "qa-channel" ? ` --channel-driver ${selectedDriver}` : "";
504496
const channelArg = driverArg && channels.length === 1 ? ` --channel ${channels[0]}` : "";
505497
return `pnpm openclaw qa suite${driverArg}${channelArg} ${scenarioArgs}`;
506498
}
507499

508500
function scenarioMatchCommandGroups(matches: readonly QaScenarioSearchMatch[]) {
509-
const groups = new Map<QaScenarioSearchMatch["executionKind"], QaScenarioSearchMatch[]>();
501+
const groups = new Map<string, QaScenarioSearchMatch[]>();
510502
for (const match of matches) {
511-
const group = groups.get(match.executionKind) ?? [];
503+
const key = JSON.stringify([match.executionKind, match.channel, match.requiredChannelDriver]);
504+
const group = groups.get(key) ?? [];
512505
group.push(match);
513-
groups.set(match.executionKind, group);
506+
groups.set(key, group);
514507
}
515508
const executionOrder: QaScenarioSearchMatch["executionKind"][] = [
516509
"flow",
517510
"script",
518511
"vitest",
519512
"playwright",
520513
];
521-
return executionOrder.flatMap((executionKind) => {
522-
const group = groups.get(executionKind);
523-
return group && group.length > 0 ? [{ executionKind, matches: group }] : [];
524-
});
514+
return [...groups.values()]
515+
.toSorted(
516+
(left, right) =>
517+
executionOrder.indexOf(left[0]!.executionKind) -
518+
executionOrder.indexOf(right[0]!.executionKind),
519+
)
520+
.map((group) => ({ executionKind: group[0]!.executionKind, matches: group }));
525521
}
526522

527523
export function renderQaScenarioMatchesMarkdownReport(params: {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { createHash } from "node:crypto";
2+
import type {
3+
OpenClawCrablineInbound,
4+
OpenClawCrablineInboundInput,
5+
StartedOpenClawCrablineAdapter,
6+
} from "@openclaw/crabline";
7+
import type { QaBusInboundMessageInput } from "./runtime-api.js";
8+
9+
const TELEGRAM_QA_DRIVER_ID = "100001";
10+
const TELEGRAM_QA_OBSERVER_ID = "100002";
11+
const MATRIX_QA_SERVER_NAME = "matrix-qa.test";
12+
const MATRIX_QA_DRIVER_ID = `@driver:${MATRIX_QA_SERVER_NAME}`;
13+
14+
export function resolveTelegramQaSenderId(senderId: string) {
15+
return senderId === "driver"
16+
? TELEGRAM_QA_DRIVER_ID
17+
: senderId === "observer"
18+
? TELEGRAM_QA_OBSERVER_ID
19+
: senderId;
20+
}
21+
22+
function resolveMatrixQaSenderId(senderId: string) {
23+
return senderId === "driver"
24+
? MATRIX_QA_DRIVER_ID
25+
: senderId === "observer"
26+
? `@observer:${MATRIX_QA_SERVER_NAME}`
27+
: senderId;
28+
}
29+
30+
function resolveMatrixQaConversationId(conversationId: string) {
31+
const trimmed = conversationId.trim();
32+
if (trimmed.startsWith("!") && trimmed.includes(":")) {
33+
return trimmed;
34+
}
35+
const digest = createHash("sha256").update(trimmed).digest("hex").slice(0, 16);
36+
return `!${digest}:${MATRIX_QA_SERVER_NAME}`;
37+
}
38+
39+
function normalizeExplicitMatrixTarget(target: string) {
40+
let normalized = target.trim();
41+
for (const prefix of ["matrix:", "room:", "user:"]) {
42+
if (normalized.toLowerCase().startsWith(prefix)) {
43+
normalized = normalized.slice(prefix.length).trim();
44+
}
45+
}
46+
return /^[!@#]/u.test(normalized) && normalized.includes(":") ? normalized : undefined;
47+
}
48+
49+
function encodeQaThreadComponent(value: string) {
50+
return value.replaceAll("%", "%25").replaceAll("/", "%2F");
51+
}
52+
53+
function resolveMatrixQaTarget(target: string) {
54+
const explicitTarget = normalizeExplicitMatrixTarget(target);
55+
if (explicitTarget) {
56+
return explicitTarget;
57+
}
58+
if (target.startsWith("thread:")) {
59+
if (target.startsWith("thread:/v1/")) {
60+
const rest = target.slice("thread:/v1/".length);
61+
const separator = rest.indexOf("/");
62+
if (separator > 0) {
63+
try {
64+
const conversationId = decodeURIComponent(rest.slice(0, separator));
65+
const resolvedConversationId =
66+
normalizeExplicitMatrixTarget(conversationId) ??
67+
resolveMatrixQaConversationId(conversationId);
68+
return `thread:/v1/${encodeQaThreadComponent(resolvedConversationId)}${rest.slice(separator)}`;
69+
} catch {
70+
return target;
71+
}
72+
}
73+
}
74+
const threadTarget = target.slice("thread:".length);
75+
const separator = threadTarget.indexOf("/");
76+
if (separator > 0) {
77+
const conversationId = threadTarget.slice(0, separator);
78+
const resolvedConversationId =
79+
normalizeExplicitMatrixTarget(conversationId) ??
80+
resolveMatrixQaConversationId(conversationId);
81+
return `thread:${resolvedConversationId}${threadTarget.slice(separator)}`;
82+
}
83+
}
84+
for (const prefix of ["channel:", "group:", "dm:"]) {
85+
if (target.startsWith(prefix)) {
86+
const conversationId = target.slice(prefix.length);
87+
const resolvedConversationId =
88+
normalizeExplicitMatrixTarget(conversationId) ??
89+
resolveMatrixQaConversationId(conversationId);
90+
return `${prefix}${resolvedConversationId}`;
91+
}
92+
}
93+
return resolveMatrixQaConversationId(target);
94+
}
95+
96+
function resolveMatrixQaText(text: string, botUserId: string) {
97+
return text.replace(
98+
/(^|[\s([{])@openclaw(?=$|[\s.,!?;)\]}])/gu,
99+
(_match, prefix: string) => `${prefix}${botUserId}`,
100+
);
101+
}
102+
103+
export function createCrablineProviderInboundInput(
104+
adapter: StartedOpenClawCrablineAdapter,
105+
input: QaBusInboundMessageInput,
106+
): OpenClawCrablineInboundInput {
107+
const kind = input.conversation.kind === "direct" ? "direct" : "group";
108+
return {
109+
...input,
110+
conversation: {
111+
...input.conversation,
112+
id:
113+
adapter.channel === "matrix"
114+
? resolveMatrixQaConversationId(input.conversation.id)
115+
: input.conversation.id,
116+
kind,
117+
},
118+
senderId:
119+
adapter.channel === "telegram"
120+
? resolveTelegramQaSenderId(input.senderId)
121+
: adapter.channel === "matrix"
122+
? resolveMatrixQaSenderId(input.senderId)
123+
: input.senderId,
124+
text:
125+
adapter.channel === "matrix" && adapter.manifest.provider === "matrix"
126+
? resolveMatrixQaText(input.text, adapter.manifest.botUserId)
127+
: input.text,
128+
};
129+
}
130+
131+
export function resolveCrablineStateConversation(params: {
132+
adapter: StartedOpenClawCrablineAdapter;
133+
input: QaBusInboundMessageInput;
134+
providerInbound: OpenClawCrablineInbound;
135+
}) {
136+
return params.adapter.channel === "matrix"
137+
? params.input.conversation
138+
: params.providerInbound.stateConversation;
139+
}
140+
141+
export function createCrablineProviderDelivery(
142+
adapter: StartedOpenClawCrablineAdapter,
143+
target: string,
144+
) {
145+
const delivery = adapter.createAgentDelivery({
146+
target: adapter.channel === "matrix" ? resolveMatrixQaTarget(target) : target,
147+
});
148+
return {
149+
delivery,
150+
providerTargetKey:
151+
adapter.channel === "matrix" ? delivery.to.replace(/^room:/u, "") : delivery.to,
152+
};
153+
}

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -671,25 +671,41 @@ describe("crabline transport", () => {
671671
MATRIX_USER_ID: "@openclaw:matrix.test",
672672
});
673673

674-
const roomId = "!qa:matrix.test";
675-
expect(transport.buildAgentDelivery({ target: `group:${roomId}` })).toEqual({
674+
const roomId = "main";
675+
const delivery = transport.buildAgentDelivery({ target: `group:${roomId}` });
676+
expect(delivery).toEqual({
677+
channel: "matrix",
678+
replyChannel: "matrix",
679+
replyTo: expect.stringMatching(/^room:![a-f0-9]{16}:matrix-qa\.test$/u),
680+
to: expect.stringMatching(/^room:![a-f0-9]{16}:matrix-qa\.test$/u),
681+
});
682+
expect(transport.buildAgentDelivery({ target: "!qa:matrix.test" })).toEqual({
683+
channel: "matrix",
684+
replyChannel: "matrix",
685+
replyTo: "room:!qa:matrix.test",
686+
to: "room:!qa:matrix.test",
687+
});
688+
expect(transport.buildAgentDelivery({ target: "matrix:room:!qa:matrix.test" })).toEqual({
676689
channel: "matrix",
677690
replyChannel: "matrix",
678-
replyTo: `room:${roomId}`,
679-
to: `room:${roomId}`,
691+
replyTo: "room:!qa:matrix.test",
692+
to: "room:!qa:matrix.test",
680693
});
694+
expect(() => transport.buildAgentDelivery({ target: "thread:/v1/main/%24event" })).toThrow(
695+
"Matrix thread targets require OpenClaw QA thread forwarding",
696+
);
681697
await expect(
682698
transport.state.addInboundMessage({
683699
conversation: { id: roomId, kind: "group" },
684-
senderId: "@alice:matrix.test",
700+
senderId: "driver",
685701
senderName: "Alice",
686702
text: "Matrix baseline marker check.",
687703
}),
688704
).resolves.toMatchObject({
689705
conversation: { id: roomId, kind: "group" },
690706
direction: "inbound",
691707
id: expect.stringMatching(/^\$[a-f0-9]{16}:matrix\.test$/u),
692-
senderId: "@alice:matrix.test",
708+
senderId: "driver",
693709
text: "Matrix baseline marker check.",
694710
});
695711
} finally {
@@ -707,13 +723,14 @@ describe("crabline transport", () => {
707723
});
708724

709725
try {
710-
const roomId = "!qa:matrix.test";
726+
const roomId = "main";
711727
await transport.state.addInboundMessage({
712728
conversation: { id: roomId, kind: "group" },
713-
senderId: "@alice:matrix.test",
729+
senderId: "driver",
714730
senderName: "Alice",
715-
text: "Matrix baseline marker check.",
731+
text: "Provision Matrix room.",
716732
});
733+
await transport.state.reset();
717734
const delivery = transport.buildAgentDelivery({ target: `group:${roomId}` });
718735
const providerRoomId = delivery.to.replace(/^room:/u, "");
719736
const env = transport.createRuntimeEnvPatch?.() ?? {};

0 commit comments

Comments
 (0)