Skip to content

Commit fec893d

Browse files
committed
fix: bound canonical patch prefix scanning
1 parent 3ea8965 commit fec893d

5 files changed

Lines changed: 40 additions & 20 deletions

File tree

‎src/responses/apply-patch-envelope.ts‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,36 +115,46 @@ export function mayBecomePatchEnvelope(text: string): boolean {
115115
}
116116

117117
/** Largest prefix of an exactly opened patch that completion cannot rewrite. */
118-
export function stableCanonicalPatchPrefix(text: string): string | null {
118+
export function stableCanonicalPatchPrefix(
119+
text: string,
120+
boundedTail = text,
121+
openingValidated = false,
122+
): string | null {
119123
const lfOpening = `${PATCH_BEGIN}\n`;
120124
const crlfOpening = `${PATCH_BEGIN}\r\n`;
121-
if (!text.startsWith(lfOpening) && !text.startsWith(crlfOpening)) {
125+
if (!openingValidated && !text.startsWith(lfOpening) && !text.startsWith(crlfOpening)) {
122126
return lfOpening.startsWith(text) || crlfOpening.startsWith(text) ? "" : null;
123127
}
124128

125-
let core = text;
129+
let coreLength = text.length;
130+
let tail = boundedTail;
126131
let trailingBreak = "";
127-
if (core.endsWith("\r\n")) {
128-
core = core.slice(0, -2);
132+
if (tail.endsWith("\r\n")) {
133+
coreLength -= 2;
134+
tail = tail.slice(0, -2);
129135
trailingBreak = "\r\n";
130-
} else if (core.endsWith("\n")) {
131-
core = core.slice(0, -1);
136+
} else if (tail.endsWith("\n")) {
137+
coreLength -= 1;
138+
tail = tail.slice(0, -1);
132139
trailingBreak = "\n";
133-
} else if (core.endsWith("\r")) {
140+
} else if (tail.endsWith("\r")) {
134141
// This may become the first byte of a CRLF. It is stable for a canonical
135142
// closer, but must remain behind a decorated closer that may be rewritten.
136-
core = core.slice(0, -1);
143+
coreLength -= 1;
144+
tail = tail.slice(0, -1);
137145
trailingBreak = "\r";
138146
}
139-
const lineStart = Math.max(core.lastIndexOf("\n"), core.lastIndexOf("\r")) + 1;
140-
const line = core.slice(lineStart);
141-
if (!line.startsWith(PATCH_END)) return text;
142-
143-
const suffix = line.slice(PATCH_END.length);
144147
const decoration = " ***";
145-
if (suffix === "") return core + trailingBreak;
146-
if (decoration.startsWith(suffix)) {
147-
return core.slice(0, lineStart + PATCH_END.length);
148+
// Only a partial terminal decoration can be rewritten at completion. Match its
149+
// bounded tail instead of searching the complete accumulated patch on every delta.
150+
for (let suffixLength = decoration.length; suffixLength > 0; suffixLength--) {
151+
const candidate = PATCH_END + decoration.slice(0, suffixLength);
152+
if (!tail.endsWith(candidate)) continue;
153+
const lineStart = coreLength - candidate.length;
154+
const preceding = tail[tail.length - candidate.length - 1];
155+
if (lineStart === 0 || preceding === "\n" || preceding === "\r") {
156+
return text.slice(0, lineStart + PATCH_END.length);
157+
}
148158
}
149159
return text;
150160
}

‎src/server/responses-custom-tool-repair.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function replaceSseEventName(block: string, type: string): string {
4141

4242
type OpenCustomCall = {
4343
argumentsText: string;
44+
argumentsTail: string;
4445
emittedInput: string;
4546
canonicalPatchProgress: boolean;
4647
retainedBytes: number;
@@ -77,6 +78,7 @@ export function createRoutedCustomToolRestoreBlockRewrite(
7778

7879
const emptyOpenCall = (): OpenCustomCall => ({
7980
argumentsText: "",
81+
argumentsTail: "",
8082
emittedInput: "",
8183
canonicalPatchProgress: false,
8284
retainedBytes: 0,
@@ -282,6 +284,7 @@ export function createRoutedCustomToolRestoreBlockRewrite(
282284
const deltaBytes = Buffer.byteLength(delta, "utf8");
283285
if (deltaBytes > 0) budget?.chargeRetained(deltaBytes, { kind: "retained_collectors" });
284286
open.argumentsText += delta;
287+
open.argumentsTail = (open.argumentsTail + delta).slice(-64);
285288
open.retainedBytes += deltaBytes;
286289
openCalls.set(upstreamItemId, open);
287290
if (customExecItemNames.get(upstreamItemId) !== "exec"
@@ -341,6 +344,7 @@ export function createRoutedCustomToolRestoreBlockRewrite(
341344
const deltaBytes = Buffer.byteLength(delta, "utf8");
342345
if (deltaBytes > 0) budget?.chargeRetained(deltaBytes, { kind: "retained_collectors" });
343346
open.argumentsText += delta;
347+
open.argumentsTail = (open.argumentsTail + delta).slice(-64);
344348
open.retainedBytes += deltaBytes;
345349
openCalls.set(upstreamItemId, open);
346350
// A helper alias will become JavaScript at completion, never raw patch/JSON.
@@ -374,7 +378,11 @@ export function createRoutedCustomToolRestoreBlockRewrite(
374378
if (mayCompile && mayBecomePatchEnvelope(fullInput)) return [];
375379
let streamInput = fullInput;
376380
if (mayNormalize && mayBecomePatchEnvelope(fullInput)) {
377-
const stablePrefix = stableCanonicalPatchPrefix(fullInput);
381+
const stablePrefix = stableCanonicalPatchPrefix(
382+
fullInput,
383+
fullInput === open.argumentsText ? open.argumentsTail : fullInput,
384+
open.canonicalPatchProgress,
385+
);
378386
if (stablePrefix === null) return [];
379387
streamInput = stablePrefix;
380388
if (stablePrefix.length > 0) open.canonicalPatchProgress = true;

‎structure/transports/inventory.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ surface is listed here so a maintainer can find the owner without grepping:
4040
| Azure OpenAI Responses | `src/adapters/azure.ts` | Deployment-shaped URLs on top of the Responses contract. |
4141
| Tool identity digests | `src/adapters/tool-call-id.ts`, `src/adapters/google-wire-compiler.ts`, `src/adapters/kiro-wire.ts`, `src/responses/muse-tool-name-alias.ts` | Rewritten Anthropic call IDs and Google, Kiro and Muse tool names take eight lowercase hexadecimal characters from `Bun.SHA256.hash(..., "hex")` over the original or salted UTF-8 string. Prefix limits and collision policies are adapter-specific. Request-scoped allocators keep distinct identities separate even when UTF-8 encoding produces the same bytes; tool-name maps restore the original names. `tests/adapters/tool-identity-hash.test.ts` pins the wire bytes, numeric and salted collisions, and Google declaration/call/result/selector agreement. |
4242
| AWS eventstream | `src/lib/eventstream-decoder.ts` | Kiro's prelude and message checksums use `Bun.hash.crc32` over the supplied byte view, with unsigned IEEE CRC-32 results and incremental seed support. Corrupt or truncated frames fail decoding. The streaming decoder rejects advertised sizes above 16 MiB per frame or 128 KiB per header block once the corresponding prelude bytes are available. `tests/responses/eventstream-decoder.test.ts` covers fixed wire bytes, offset views, seeded checksums, corruption, and fragmented input. |
43-
| Responses custom-tool preview | `src/bridge/sse.ts`, `src/server/responses-custom-tool-repair.ts`, `src/responses/progressive-freeform-input.ts`, `src/responses/freeform-wrapper-scan.ts` | Direct adapter events and routed function restoration share one progressive wrapper decoder over one bounded JSON classification of the prefix, so property order and escaped key spellings preview as the wrapper completion unwraps them. Fence-shaped and decorated-opening `exec`/`apply_patch` prefixes stay held. Routed canonical `apply_patch` publishes only the stable prefix after its exact opening line, withholds a rewritable terminal decoration, and fails before successful completion if the authoritative repaired input no longer extends emitted progress. Each caller retains its own patch-envelope and byte-budget policy. |
43+
| Responses custom-tool preview | `src/bridge/sse.ts`, `src/server/responses-custom-tool-repair.ts`, `src/responses/progressive-freeform-input.ts`, `src/responses/freeform-wrapper-scan.ts` | Direct adapter events and routed function restoration share one progressive wrapper decoder over one bounded JSON classification of the prefix, so property order and escaped key spellings preview as the wrapper completion unwraps them. Fence-shaped and decorated-opening `exec`/`apply_patch` prefixes stay held. Routed canonical `apply_patch` publishes only the stable prefix after its exact opening line, recognizes a rewritable terminal decoration from a bounded suffix, and fails before successful completion if the authoritative repaired input no longer extends emitted progress. Each caller retains its own patch-envelope and byte-budget policy. |
4444
| Meta Muse Responses tool names | `src/responses/muse-tool-name-alias.ts`, `src/adapters/openai-responses.ts` | `api.meta.ai` only: function names over 64 characters or containing characters outside `[a-zA-Z0-9_-]` become collision-safe wire aliases and are restored before the client sees them. |
4545
| Google / Vertex / Antigravity | `src/adapters/google.ts`, `src/adapters/google-http.ts`, `src/adapters/google-wire-compiler.ts`, `src/adapters/google-tool-schema.ts`, `src/adapters/google-truncation.ts`, `src/adapters/google-errors.ts`, `src/adapters/google-antigravity-wire.ts`, `src/adapters/google-antigravity-replay.ts`, `src/adapters/google-wire-shape.ts` | Vertex and Antigravity install a Google-family `fetchResponse` and so own their retry policy, while AI Studio Gemini leaves it undefined and uses the default server fetch path. Its transient ladder uses the shared `TRANSIENT_RETRY_MAX_ATTEMPTS` constant, also used to fund each eligible generic OAuth account in the [request send budget](responses-spend.md#credential-hop-reservations). A `Retry-After` on a retryable 429/503 is the earliest resend: the two-second local maximum bounds only the wrapper's own jittered backoff, and an instruction past the shared `RETRY_AFTER_CEILING_MS` (60 seconds) ends with that upstream response, `Retry-After` intact, instead of an early resend. Antigravity 403/404 normalization reads at most 4096 bytes for at most two seconds and emits a validation marker only for a complete structured `VALIDATION_REQUIRED` reason; an unsafe, incomplete or erroring body falls back to status-only text with the original HTTP status; caller cancellation propagates. Model-404 denial provenance requires an exact model ResourceInfo under the [account refusal contract](responses-failover.md#account-refusal-and-rotation-boundaries). The Google-family wrapper reuses shared abort/deadline helpers, upstream error normalization, and policy-aware wire-body repair: strict initial schema loss sends nothing, while strict repair withholding returns the original 400 without a changed send. The final compiler produces the [content-free tool-schema loss contract](../providers/google.md#google-tool-schema-loss-reporting). `google-wire-shape.ts` remains diagnostic-only. |
4646
| Mimo Free | `src/adapters/mimo-free.ts` | Client identity and JWT handling are transport-local; the per-install client id lives in the opencodex state root. |

‎structure/transports/responses.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ restoration has held exactly these bodies since #5047 and this is the two paths
172172
new restriction invented for one of them. Bodies that are not objects, which is what an `exec`
173173
program or an `apply_patch` envelope actually looks like, are unaffected and still stream.
174174

175-
Routed restoration keeps its hold for an unrecognized JSON object and its separate code-mode patch-envelope hold. Duplicate `input` keys and wrappers that become invalid only after a valid prefix was emitted remain bounded exceptions for ordinary freeform tools. Routed `apply_patch` narrows that boundary: after an exact canonical `*** Begin Patch` line resolves, `src/responses/apply-patch-envelope.ts` exposes only bytes that delimiter normalization cannot rewrite.
175+
Routed restoration keeps its hold for an unrecognized JSON object and its separate code-mode patch-envelope hold. Duplicate `input` keys and wrappers that become invalid only after a valid prefix was emitted remain bounded exceptions for ordinary freeform tools. Routed `apply_patch` narrows that boundary: after an exact canonical `*** Begin Patch` line resolves, `src/responses/apply-patch-envelope.ts` exposes only bytes that delimiter normalization cannot rewrite, identifying a rewritable terminal decoration from a bounded suffix rather than rescanning the accumulated patch.
176176
A possible decorated terminal marker and the line break behind it remain held until authoritative. If a late-invalid or duplicate-key wrapper makes the completed repaired input disagree with an emitted canonical patch prefix, the routed rewrite fails closed before input-done, item-done, or completed output rather than publishing a rewound executable call.
177177

178178
Codex-private tool fields are removed at the same boundary from one table

‎tests/responses/apply-patch-envelope.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,7 @@ describe("raw exec patch envelope recognition", () => {
247247
.toBe("*** Begin Patch\nbody\n*** End Patch");
248248
expect(stableCanonicalPatchPrefix("*** Begin Patch\nbody\n*** End Patch ***\ninterior"))
249249
.toBe("*** Begin Patch\nbody\n*** End Patch ***\ninterior");
250+
const longLfOnlyLine = `*** Begin Patch\n${"x".repeat(200_000)}`;
251+
expect(stableCanonicalPatchPrefix(longLfOnlyLine)).toBe(longLfOnlyLine);
250252
});
251253
});

0 commit comments

Comments
 (0)