1
0
Fork 0
polaris-task-force/tests/int/ai-idle-watchdog.int.spec.ts
Z8MB1E 51ead07da0 fix(ai): drive the structured stream and use the chat endpoint
Dialogue drafting hung on "Drafting..." forever against OpenRouter, with the
provider having received and completed the request. Two independent faults, both
found by running the app's own code against the live endpoint.

The hang: streamObject is lazy. Awaiting result.object alone does not pump the
stream in this SDK version, so the promise waited for a parse that never started
and the call sat until its ten minute ceiling. One identical request, measured
twice: object-only never returned in 60s, draining textStream first returned a
valid draft in 6.9s. generateStructured now drains the stream before reading the
object.

The endpoint: provider(modelId) resolves to OpenAI's Responses API, which only
OpenAI implements. OpenRouter answered 200 and never completed. Every
OpenAI-compatible service the app targets serves /chat/completions, which the
plain path already used, so chatModelFor reaches the model through .chat(). The
instrumented request now goes to /chat/completions and Compose's structured
branch uses the same call.

Defences, since a provider can still answer and then go quiet: an idle watchdog
aborts a body that delivers no bytes for 60s on both paths, transport retries are
off so one stall is one wait, a draft is capped at four minutes, and the dialog
has a UI watchdog plus a catch that shows the failure instead of spinning.

Against the live endpoint the real function now returns a five node draft in
6.2s. 246 tests across 24 files pass, including a regression test that fails if
the stream drain is ever removed.
2026-12-01 01:47:49 -05:00

73 lines
2.8 KiB
TypeScript

// @vitest-environment node
import { describe, expect, it } from "vitest";
import { idleWatchdogFetch, PROVIDER_IDLE_TIMEOUT_MS } from "@/lib/ai/generate";
/**
* The idle watchdog exists because a provider can answer 200, stream part of a
* response, and then go quiet without finishing or closing. These tests drive
* the wrapper with fake streams: a healthy body must pass through unchanged, a
* silent one must fail fast instead of waiting for a total timeout.
*/
describe("provider idle watchdog", () => {
it("is a full minute by default", () => {
expect(PROVIDER_IDLE_TIMEOUT_MS).toBe(60_000);
});
it("delivers a healthy stream byte for byte", async () => {
const encoder = new TextEncoder();
const body = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(encoder.encode("data: one\n\n"));
controller.enqueue(encoder.encode("data: two\n\n"));
controller.close();
},
});
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } });
try {
const response = await idleWatchdogFetch(500)("https://example.test/chat/completions");
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("text/event-stream");
await expect(response.text()).resolves.toBe("data: one\n\ndata: two\n\n");
} finally {
globalThis.fetch = originalFetch;
}
});
it("fails a stream that stops sending instead of hanging", async () => {
const encoder = new TextEncoder();
const body = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(encoder.encode("data: partial\n\n"));
// Never closes: this is the OpenRouter stall, in miniature.
},
});
const originalFetch = globalThis.fetch;
globalThis.fetch = async () => new Response(body, { status: 200 });
try {
const response = await idleWatchdogFetch(60)("https://example.test/chat/completions");
const started = Date.now();
await expect(response.text()).rejects.toThrow(/stopped sending data/i);
// Fast, not after a total timeout: the silence window, not the call budget.
expect(Date.now() - started).toBeLessThan(2_000);
} finally {
globalThis.fetch = originalFetch;
}
});
it("leaves a body-less response alone", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async () => new Response(null, { status: 204 });
try {
const response = await idleWatchdogFetch(50)("https://example.test/chat/completions");
expect(response.status).toBe(204);
expect(response.body).toBeNull();
} finally {
globalThis.fetch = originalFetch;
}
});
});