Skip to content

Commit 1c86769

Browse files
committed
test(gateway): keep trajectory export live proof correlated
1 parent b1caba5 commit 1c86769

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎src/gateway/gateway-trajectory-export.live.test.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,37 @@ function formatTextPreview(texts: string[], maxChars = 800): string {
172172
return combined.length > maxChars ? `${combined.slice(0, maxChars)}...` : combined;
173173
}
174174

175+
function extractAssistantTexts(messages: unknown[]): string[] {
176+
const texts: string[] = [];
177+
for (const entry of messages) {
178+
if (!entry || typeof entry !== "object") {
179+
continue;
180+
}
181+
if ((entry as { role?: unknown }).role !== "assistant") {
182+
continue;
183+
}
184+
const text = extractVisibleMessageText(entry);
185+
if (typeof text === "string" && text.trim().length > 0) {
186+
texts.push(text);
187+
}
188+
}
189+
return texts;
190+
}
191+
175192
async function waitForTrajectoryExportInstructionText(params: {
193+
client: GatewayClient;
176194
events: EventFrame[];
195+
eventStartIndex: number;
177196
expectedText: string;
178197
runId: string;
198+
sessionKey: string;
179199
timeoutMs: number;
180200
}): Promise<string> {
181201
const deadline = Date.now() + params.timeoutMs;
182202
let finalTexts: string[] = [];
183203
while (Date.now() < deadline) {
184-
finalTexts = params.events
204+
const newEvents = params.events.slice(params.eventStartIndex);
205+
finalTexts = newEvents
185206
.map((event) => extractChatFinalText(event, params.runId))
186207
.filter((text): text is string => typeof text === "string" && text.trim().length > 0);
187208
const matchedText = finalTexts.find((text) => text.includes(params.expectedText));
@@ -192,9 +213,23 @@ async function waitForTrajectoryExportInstructionText(params: {
192213
setTimeout(resolve, 500);
193214
});
194215
}
216+
let assistantTexts: string[];
217+
try {
218+
const history = (await params.client.request(
219+
"chat.history",
220+
{
221+
sessionKey: params.sessionKey,
222+
limit: 24,
223+
},
224+
{ timeoutMs: 10_000 },
225+
)) as { messages?: unknown[] };
226+
assistantTexts = extractAssistantTexts(history.messages ?? []);
227+
} catch {
228+
assistantTexts = [];
229+
}
195230
throw new Error(
196231
`timed out waiting for trajectory export instruction text for ${params.runId}; ` +
197-
`events=${params.events.length}; finalTexts=${formatTextPreview(finalTexts)}`,
232+
`events=${params.events.length}; finalTexts=${formatTextPreview(finalTexts)}; assistantTexts=${formatTextPreview(assistantTexts)}`,
198233
);
199234
}
200235

@@ -210,6 +245,10 @@ function extractChatFinalText(event: EventFrame, runId: string): string | undefi
210245
if (record.runId !== runId || record.state !== "final") {
211246
return undefined;
212247
}
248+
return extractChatFinalRecordText(record);
249+
}
250+
251+
function extractChatFinalRecordText(record: Record<string, unknown>): string | undefined {
213252
const message = record.message;
214253
if (!message || typeof message !== "object") {
215254
return undefined;
@@ -395,6 +434,7 @@ describeLive("gateway live trajectory export", () => {
395434
const bundleDir = path.join(workspaceDir, ".openclaw", "trajectory-exports", "bundle");
396435
const beforeExport = new Set(await listDirectoryNames(tempDir));
397436
const exportRunId = `chat-export-${randomUUID()}`;
437+
const exportEventStartIndex = gatewayEvents.length;
398438
logLiveStep("export:start", { bundleDir, exportRunId });
399439
const exportResponse = (await client.request(
400440
"chat.send",
@@ -415,9 +455,12 @@ describeLive("gateway live trajectory export", () => {
415455
typeof exportResponse?.message === "object"
416456
? extractVisibleMessageText(exportResponse.message)
417457
: await waitForTrajectoryExportInstructionText({
458+
client,
418459
events: gatewayEvents,
460+
eventStartIndex: exportEventStartIndex,
419461
expectedText: "Trajectory exports can include",
420462
runId: exportRunId,
463+
sessionKey,
421464
timeoutMs: 60_000,
422465
});
423466
expect(finalText).toContain("Trajectory exports can include");

‎src/gateway/server-methods/chat.directive-tags.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,16 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
23862386
expect(text).toContain("Trajectory exports can include");
23872387
expect(text).toContain("through exec approval");
23882388
expect(text).toContain("Approve once");
2389+
const broadcast = lastBroadcastPayload(context);
2390+
expect(broadcast?.runId).toBe("idem-command-block");
2391+
expect(broadcast?.state).toBe("final");
2392+
const broadcastText = getMessageContent(broadcast)
2393+
.map((block) => (typeof block.text === "string" ? block.text : ""))
2394+
.filter(Boolean)
2395+
.join("\n");
2396+
expect(broadcastText).toContain("Trajectory exports can include");
2397+
expect(broadcastText).toContain("through exec approval");
2398+
expect(broadcastText).toContain("Approve once");
23892399
});
23902400

23912401
it("keeps slash-command block text when the final payload only adds media", async () => {

0 commit comments

Comments
 (0)