Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(agent): end truncated proxy streams and send serviceTier through the proxy (#2491)

Sebastian Müllerfb8d7411e2ca
diff --git a/packages/agent/.changes/fix-proxy-eof.md b/packages/agent/.changes/fix-proxy-eof.md
new file mode 100644
index 0000000000..eda03b248a
--- /dev/null
+++ b/packages/agent/.changes/fix-proxy-eof.md
@@ -0,0 +1 @@
+- Proxy streams now fail with a clear "stream truncated" error instead of hanging forever when the connection ends mid-response, and the proxy request body is derived from the shared stream-options type so new options cannot be silently dropped.
diff --git a/packages/agent/src/proxy.ts b/packages/agent/src/proxy.ts
index 83f3b65385..033c49d561 100644
--- a/packages/agent/src/proxy.ts
+++ b/packages/agent/src/proxy.ts
@@ -46,18 +46,35 @@ export type ProxyAssistantMessageEvent =
usage: AssistantMessage["usage"];
};
-type ProxySerializableStreamOptions = Pick<
- SimpleStreamOptions,
- | "temperature"
- | "maxTokens"
- | "reasoning"
- | "cacheRetention"
- | "sessionId"
- | "headers"
- | "metadata"
- | "transport"
- | "thinkingBudgets"
->;
+/**
+ * Total map over the shared stream options: every option is either serialized into the proxy
+ * request or explicitly excluded. A new option in `SimpleStreamOptions` fails to compile here
+ * until it is classified, so proxy transport cannot silently drop what direct transport sends.
+ */
+const PROXY_SERIALIZED_OPTIONS = {
+ temperature: true,
+ maxTokens: true,
+ reasoning: true,
+ cacheRetention: true,
+ sessionId: true,
+ headers: true,
+ metadata: true,
+ transport: true,
+ thinkingBudgets: true,
+ serviceTier: true,
+ // Client-local: abort, credentials, request lifetime and payload hooks stay on this side.
+ signal: false,
+ apiKey: false,
+ timeoutMs: false,
+ onPayload: false,
+ onResponse: false,
+} as const satisfies Record<keyof SimpleStreamOptions, boolean>;
+
+type ProxySerializedOption = {
+ [K in keyof typeof PROXY_SERIALIZED_OPTIONS]: (typeof PROXY_SERIALIZED_OPTIONS)[K] extends true ? K : never;
+}[keyof typeof PROXY_SERIALIZED_OPTIONS];
+
+type ProxySerializableStreamOptions = Pick<SimpleStreamOptions, ProxySerializedOption>;
export interface ProxyStreamOptions extends ProxySerializableStreamOptions {
signal?: AbortSignal;
@@ -84,7 +101,9 @@ export interface ProxyStreamOptions extends ProxySerializableStreamOptions {
* });
* ```
*/
-function buildProxyRequestOptions(options: ProxyStreamOptions): ProxySerializableStreamOptions {
+function buildProxyRequestOptions(options: ProxyStreamOptions): {
+ [K in ProxySerializedOption]: SimpleStreamOptions[K];
+} {
return {
temperature: options.temperature,
maxTokens: options.maxTokens,
@@ -95,6 +114,7 @@ function buildProxyRequestOptions(options: ProxyStreamOptions): ProxySerializabl
metadata: options.metadata,
transport: options.transport,
thinkingBudgets: options.thinkingBudgets,
+ serviceTier: options.serviceTier,
};
}
@@ -121,6 +141,7 @@ export function streamProxy(model: Model<any>, context: Context, options: ProxyS
};
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
+ let sawTerminalEvent = false;
const abortHandler = () => {
if (reader) {
@@ -183,6 +204,7 @@ export function streamProxy(model: Model<any>, context: Context, options: ProxyS
const proxyEvent = JSON.parse(data) as ProxyAssistantMessageEvent;
const event = processProxyEvent(proxyEvent, partial);
if (event) {
+ sawTerminalEvent ||= event.type === "done" || event.type === "error";
stream.push(event);
}
}
@@ -193,6 +215,9 @@ export function streamProxy(model: Model<any>, context: Context, options: ProxyS
if (options.signal?.aborted) {
throw new Error("Request aborted by user");
}
+ if (!sawTerminalEvent) {
+ throw new Error("Proxy stream truncated before completion");
+ }
stream.end();
} catch (error) {
diff --git a/packages/agent/test/proxy.test.ts b/packages/agent/test/proxy.test.ts
new file mode 100644
index 0000000000..3d6cd618e1
--- /dev/null
+++ b/packages/agent/test/proxy.test.ts
@@ -0,0 +1,29 @@
+import { getModels } from "@earendil-works/pi-ai";
+import { afterEach, expect, it, vi } from "vitest";
+import { streamProxy } from "../src/proxy.js";
+
+const model = getModels("openai")[0];
+const proxyOptions = { authToken: "token", proxyUrl: "http://proxy.test" };
+
+function stubProxyResponse(sse: string): { options: { serviceTier?: string } }[] {
+ const sent: { options: { serviceTier?: string } }[] = [];
+ vi.stubGlobal("fetch", async (_url: string, init: RequestInit) => {
+ sent.push(JSON.parse(String(init.body)));
+ return new Response(sse, { status: 200 });
+ });
+ return sent;
+}
+
+afterEach(() => vi.unstubAllGlobals());
+
+it("settles a truncated proxy stream with an error result", async () => {
+ stubProxyResponse('data: {"type":"start"}\n\n');
+ const result = await streamProxy(model, { messages: [] }, proxyOptions).result();
+ expect(result).toMatchObject({ stopReason: "error", errorMessage: expect.stringContaining("truncated") });
+});
+
+it("serializes serviceTier into the proxy request", async () => {
+ const sent = stubProxyResponse("");
+ await streamProxy(model, { messages: [] }, { ...proxyOptions, serviceTier: "priority" }).result();
+ expect(sent[0]?.options.serviceTier).toBe("priority");
+});
diff --git a/packages/coding-agent/.changes/proxy-truncated-stream-and-service-tier.md b/packages/coding-agent/.changes/proxy-truncated-stream-and-service-tier.md
new file mode 100644
index 0000000000..5bccdaaa56
--- /dev/null
+++ b/packages/coding-agent/.changes/proxy-truncated-stream-and-service-tier.md
@@ -0,0 +1 @@
+- Fixed proxy streaming: a truncated response now ends with an error instead of leaving the turn waiting forever, and the service tier setting is now sent through the proxy like it is for direct provider calls.