Skip to content

Commit cf76fb1

Browse files
author
Your Name
committed
fix(videos): complete pruned-path guard, skip namespaced aliases, mock sleep in tests
Address Wibias fifth CHANGES_REQUESTED review: 1. Finish pruned-path guard: - Filter files[] through existsSync, not just path - If all pruned: ok:false with no markdown/path - If some survive: refresh path/files/count/markdown from survivors 2. Skip namespaced tools when seeding videoPlan.toolNames: - if (t.namespace) continue — matches core.ts filter rule 3. Fix ubuntu CI timeout: - Mock setTimeout in heartbeat test so 5s poll interval resolves instantly - Suite now runs in ~156ms (was ~5s)
1 parent 938f25e commit cf76fb1

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎src/images/fulfill-video.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const MAX_POLL_INTERVAL_MS = 15_000;
1717
const POLL_BACKOFF = 1.5;
1818
const DEFAULT_VIDEO_TIMEOUT_MS = 300_000; // 5 min
1919

20-
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
20+
export function sleep(ms: number, signal?: AbortSignal): Promise<void> {
2121
if (signal?.aborted) return Promise.reject(new Error("aborted"));
2222
return new Promise((resolve, reject) => {
2323
const onAbort = (): void => { clearTimeout(timer); reject(new Error("aborted")); };

‎src/images/loop.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
import type { AdapterRequest, ProviderAdapter } from "../adapters/base";
1414
import { existsSync } from "node:fs";
15+
import { pathToFileURL } from "node:url";
1516
import { createAdapterEventQueue } from "../adapters/run-turn-queue";
1617
import type { AdapterEvent, OcxMessage, OcxParsedRequest, OcxProviderContinuationState, OcxRequestOptions, OcxThinkingContent, OcxUsage } from "../types";
1718
import { namespacedToolName } from "../types";
@@ -704,8 +705,28 @@ export async function runWithImageBridge(deps: ImageBridgeDeps): Promise<Respons
704705
pruneArtifacts(videoPlan?.artifactsKeepCount ?? plan?.artifactsKeepCount);
705706
// Drop results whose artifact files were pruned — never hand the model a dead path.
706707
for (const f of fulfilled) {
707-
if (f.result.ok && f.result.path && !existsSync(f.result.path)) {
708-
f.result = { ...f.result, ok: false, path: undefined, files: [], count: 0, error: "artifact was pruned before delivery (increase artifactsKeepCount)" };
708+
if (!f.result.ok || !f.result.files || f.result.files.length === 0) continue;
709+
const survivors = f.result.files.filter(p => existsSync(p));
710+
if (survivors.length === f.result.files.length) continue; // nothing pruned
711+
if (survivors.length === 0) {
712+
f.result = {
713+
ok: false, model: f.result.model, prompt: f.result.prompt ?? "",
714+
files: [], count: 0,
715+
error: "artifact was pruned before delivery (increase artifactsKeepCount)",
716+
} as typeof f.result;
717+
} else {
718+
// Some files survived — refresh from survivors
719+
f.result = { ...f.result, files: survivors, count: survivors.length };
720+
const primary = survivors[0]!;
721+
(f.result as { path?: string }).path = primary;
722+
if ("markdown" in f.result && f.result.markdown) {
723+
// Image markdown references the primary path; video uses pathToFileURL
724+
if (f.result.markdown.startsWith("![")) {
725+
(f.result as { markdown: string }).markdown = `![image](${pathToFileURL(primary).href})`;
726+
} else {
727+
(f.result as { markdown: string }).markdown = `[video](${pathToFileURL(primary).href})`;
728+
}
729+
}
709730
}
710731
}
711732
const now = Date.now();

‎src/images/plan.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export async function planVideoBridge(
110110
// Collect any existing function tools whose name matches a video_gen alias
111111
// so the loop can intercept and replace them (image-bridge parity).
112112
for (const t of parsed.context?.tools ?? []) {
113+
// Skip namespaced tools — a namespaced MCP video_gen must not be intercepted.
114+
if (t.namespace) continue;
113115
const fnName = typeof t.name === "string" ? t.name
114116
: (t as unknown as { function?: { name?: string } }).function?.name;
115117
if (typeof fnName === "string" && isVideoGenName(fnName)) {

‎tests/videos/fulfill-video.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ describe("pollVideoWithHeartbeats", () => {
9090
});
9191

9292
test("yields at least one heartbeat before returning", async () => {
93+
// Mock sleep to resolve immediately so the 5s poll interval doesn't block CI.
94+
const origSetTimeout = globalThis.setTimeout;
9395
let callCount = 0;
9496
mock.module("../../src/images/xai-video-client", () => ({
9597
pollVideoJob: mock(() => {
@@ -100,6 +102,8 @@ describe("pollVideoWithHeartbeats", () => {
100102
});
101103
}),
102104
}));
105+
// Override setTimeout globally — sleep() uses it internally.
106+
globalThis.setTimeout = ((fn: () => void) => { fn(); return 0 as unknown as NodeJS.Timeout; }) as typeof globalThis.setTimeout;
103107

104108
const ac = new AbortController();
105109
const gen = pollVideoWithHeartbeats("r1", { baseUrl: "https://api.x.ai/v1", token: "t" }, ac.signal, 60_000);
@@ -110,9 +114,10 @@ describe("pollVideoWithHeartbeats", () => {
110114
if (done) { result = value; break; }
111115
heartbeats.push(value.message);
112116
}
117+
globalThis.setTimeout = origSetTimeout;
113118
expect(heartbeats.length).toBeGreaterThanOrEqual(1);
114119
expect(result.ok).toBe(true);
115-
}, 15_000); // 5s initial poll interval means ~10s for 2 polls
120+
}, 5_000);
116121

117122
test("returns failed status", async () => {
118123
mock.module("../../src/images/xai-video-client", () => ({

0 commit comments

Comments
 (0)