mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 19:08:33 +01:00
fix(runner): keep OpenCode turns alive through oversized SSE events
Retain bounded OpenCode SSE events without restarting the event stream. Preserve canonical tool identity, status, output tails, original UTF-8 sizes, and truthful truncation markers. Bound id-less event deduplication with hashes. Cover fragmented and complete large frames, exact-limit split separators, bulk input retention pressure, truncation-record exhaustion, and unpaired surrogate byte counts. Full current-head CI and Apex 5/5 passed. The unrelated local post-tool answer assertion was reproduced on unchanged baseline source. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
6 files changed
+870
-41
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { spawn, type ChildProcess } from "node:child_process";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import {
|
||||
@@ -20,6 +20,7 @@ import { pipeline } from "node:stream/promises";
|
||||
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
|
||||
import { parseOpenCodeReasoningMode, type OpenCodeReasoningMode } from "./reasoning-mode.js";
|
||||
import { annotateOmittedProviderText, parseOpenCodeSseFrames } from "./sse-frames.js";
|
||||
|
||||
import {
|
||||
CODEX_SKILLLESS_BASE_INSTRUCTIONS,
|
||||
@@ -494,6 +495,7 @@ class OpenCodeHarnessSession implements HarnessSession {
|
||||
}> = [];
|
||||
readonly #messageUsageFingerprints = new Map<string, string>();
|
||||
readonly #workspaceChangesByTurn = new Map<string, Record<string, unknown>>();
|
||||
#reportedSseCompaction = false;
|
||||
readonly #emittedFileReferences = new Set<string>();
|
||||
readonly #pendingRuntimeRequests = new Map<
|
||||
string,
|
||||
@@ -1359,8 +1361,31 @@ class OpenCodeHarnessSession implements HarnessSession {
|
||||
reason: "Opened the OpenCode server-sent event stream",
|
||||
});
|
||||
}
|
||||
for await (const frame of parseSseFrames(response.body)) {
|
||||
for await (const frame of parseOpenCodeSseFrames(response.body)) {
|
||||
if (this.#closed) return;
|
||||
const compaction = frame.compaction;
|
||||
if (compaction?.omitted) {
|
||||
// Even the compacted structure exceeded its bound. Report the
|
||||
// frame instead of ending the stream or dropping it silently.
|
||||
this.#emit("harness.diagnostic", {
|
||||
code: "opencode_sse_event_omitted",
|
||||
message: `OpenCode sent a ${compaction.frameChars}-character event that could not be retained within the payload bound.`,
|
||||
providerEventType: compaction.eventType,
|
||||
frameChars: compaction.frameChars,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (compaction && !this.#reportedSseCompaction) {
|
||||
this.#reportedSseCompaction = true;
|
||||
this.#emit("harness.diagnostic", {
|
||||
code: "opencode_sse_event_compacted",
|
||||
message: `OpenCode sent a ${compaction.frameChars}-character event. Long strings were shortened to keep the event within the payload bound.`,
|
||||
providerEventType: compaction.eventType,
|
||||
frameChars: compaction.frameChars,
|
||||
truncatedStrings: compaction.truncatedStrings,
|
||||
omittedChars: compaction.omittedChars,
|
||||
});
|
||||
}
|
||||
const frameId =
|
||||
this.#runtime.trace?.frame({
|
||||
direction: "provider_to_client",
|
||||
@@ -1371,22 +1396,26 @@ class OpenCodeHarnessSession implements HarnessSession {
|
||||
let event: unknown;
|
||||
try {
|
||||
event = JSON.parse(frame.data);
|
||||
if (compaction) annotateOmittedProviderText(event, compaction);
|
||||
if (frameId) {
|
||||
this.#runtime.trace?.interpretation({
|
||||
frameId,
|
||||
stage: "typescript_opencode_sse_parse",
|
||||
ruleId: "opencode.sse.json",
|
||||
ruleId: compaction ? "opencode.sse.compacted_json" : "opencode.sse.json",
|
||||
disposition: "mapped",
|
||||
fieldMappings: [
|
||||
{
|
||||
inputPath: "$raw.data",
|
||||
outputPath: "$providerEvent",
|
||||
action: "normalized",
|
||||
reason:
|
||||
"Decoded the exact SSE data payload as an OpenCode event",
|
||||
reason: compaction
|
||||
? "Decoded the compacted SSE data payload as an OpenCode event"
|
||||
: "Decoded the exact SSE data payload as an OpenCode event",
|
||||
},
|
||||
],
|
||||
reason: "OpenCode SSE frame parsed as JSON",
|
||||
reason: compaction
|
||||
? `OpenCode SSE frame of ${compaction.frameChars} characters parsed as JSON after ${compaction.truncatedStrings} long strings were shortened (${compaction.omittedChars} characters omitted)`
|
||||
: "OpenCode SSE frame parsed as JSON",
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -1486,7 +1515,11 @@ class OpenCodeHarnessSession implements HarnessSession {
|
||||
const type = text(event.type);
|
||||
// Id-less deltas are additive: two identical chunks can be legitimate
|
||||
// adjacent tokens. Content deduplication would silently lose the second.
|
||||
const eventId = text(event.id) || (type === "message.part.delta" ? null : canonicalJson(value));
|
||||
// Key id-less events by digest: a retained canonical copy of each recent
|
||||
// event would hold up to 10,000 large tool payloads in memory.
|
||||
const eventId = text(event.id) || (type === "message.part.delta"
|
||||
? null
|
||||
: `sha256:${createHash("sha256").update(canonicalJson(value)).digest("hex")}`);
|
||||
if (eventId !== null && this.#seenProviderEvents.has(eventId)) return;
|
||||
if (eventId !== null) this.#seenProviderEvents.add(eventId);
|
||||
if (this.#seenProviderEvents.size > 10_000)
|
||||
@@ -2829,37 +2862,15 @@ async function reservePort(): Promise<number> {
|
||||
return port;
|
||||
}
|
||||
|
||||
async function* parseSseFrames(
|
||||
stream: ReadableStream<Uint8Array>,
|
||||
): AsyncIterable<{ raw: string; data: string }> {
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
for await (const chunk of stream as unknown as AsyncIterable<Uint8Array>) {
|
||||
buffer += decoder.decode(chunk, { stream: true });
|
||||
if (buffer.length > 1_048_576)
|
||||
throw new Error("OpenCode SSE event exceeded the retained payload limit");
|
||||
let boundary: RegExpExecArray | null;
|
||||
while ((boundary = /\r?\n\r?\n/.exec(buffer)) !== null) {
|
||||
const rawFrame = buffer.slice(0, boundary.index);
|
||||
const raw = buffer.slice(0, boundary.index + boundary[0].length);
|
||||
const frame = rawFrame.replaceAll("\r", "");
|
||||
buffer = buffer.slice(boundary.index + boundary[0].length);
|
||||
const data = frame
|
||||
.split("\n")
|
||||
.filter((line) => line.startsWith("data:"))
|
||||
.map((line) => line.slice(5).trimStart())
|
||||
.join("\n");
|
||||
if (!data || data === "[DONE]") continue;
|
||||
yield { raw, data };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function* parseSse(
|
||||
stream: ReadableStream<Uint8Array>,
|
||||
): AsyncIterable<unknown> {
|
||||
for await (const frame of parseSseFrames(stream))
|
||||
yield JSON.parse(frame.data);
|
||||
for await (const frame of parseOpenCodeSseFrames(stream)) {
|
||||
if (frame.compaction?.omitted) continue;
|
||||
const event = JSON.parse(frame.data) as unknown;
|
||||
if (frame.compaction) annotateOmittedProviderText(event, frame.compaction);
|
||||
yield event;
|
||||
}
|
||||
}
|
||||
|
||||
function sanitizedEnvironment(
|
||||
|
||||
Reference in new issue
Block a user