Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Keep gateway document requests below HTTP body limits (#2200)

Joshua France0e45eb717dc2
diff --git a/src/core/document-inputs.ts b/src/core/document-inputs.ts
index 348398d3d..1316927a4 100644
--- a/src/core/document-inputs.ts
+++ b/src/core/document-inputs.ts
@@ -1,4 +1,5 @@
import { addAbortSignal } from "node:stream";
+import { createHash } from "node:crypto";
import { Worker } from "node:worker_threads";
import type { AttachmentMeta, SessionEntry } from "../types.ts";
import type { FileArtifact, FileArtifactStore } from "../files/file-artifact-store.ts";
@@ -81,10 +82,20 @@ export async function loadDocumentInputs(
maxBytes = MAX_DOCUMENT_BYTES,
signal?: AbortSignal,
maxDocuments = 10,
+ existingDocuments: readonly DocumentInput[] = [],
): Promise<{ documents: DocumentInput[]; notices: string[] }> {
const documents: DocumentInput[] = [];
const notices: string[] = [];
const seen = new Set<string>();
+ const loadedContent = new Set(
+ existingDocuments.map((document) =>
+ JSON.stringify([
+ createHash("sha256").update(document.dataBase64, "base64").digest("hex"),
+ document.name,
+ document.mimeType,
+ ]),
+ ),
+ );
let remaining = maxBytes;
for (const meta of [...metas].reverse()) {
signal?.throwIfAborted();
@@ -93,6 +104,8 @@ export async function loadDocumentInputs(
seen.add(meta.artifactId);
const artifact = await files.get(meta.artifactId);
if (!artifact || !(await authorize(artifact)) || !artifact.sha256 || !isDocumentAttachment(artifact)) continue;
+ const contentKey = JSON.stringify([artifact.sha256, artifact.name, artifact.mimetype]);
+ if (loadedContent.has(contentKey)) continue;
if (artifact.sizeBytes > remaining || documents.length >= maxDocuments) {
notices.push(
`${artifact.name}: document omitted from model input because it exceeds the attachment count or byte budget.`,
@@ -124,6 +137,7 @@ export async function loadDocumentInputs(
continue;
}
remaining -= bytes.sizeBytes;
+ loadedContent.add(contentKey);
documents.unshift({
name: artifact.name,
mimeType: artifact.mimetype,
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index ea346fc9b..9044ab262 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -3544,6 +3544,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
emitGapWork("persist", persistStart, Date.now());
}
};
+ const initialDocuments = [...documentInputs.documents];
return deps.harness.turns.runTurn({
session,
...(turnTimezone ? { timezone: turnTimezone } : {}),
@@ -3611,6 +3612,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
remainingDocumentBytes,
turnAbort.signal,
remainingDocumentCount,
+ initialDocuments,
);
let steeredUnscreened = false;
if (enforceScreen) steeredUnscreened = await screenDocuments(steeredDocuments, text);
@@ -3687,7 +3689,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
...(extras.overheard?.length ? { overheard: extras.overheard } : {}),
...(extras.attachments?.length ? { attachments: extras.attachments } : {}),
...(extras.images?.length ? { images: extras.images } : {}),
- documents: [...documentInputs.documents],
+ documents: initialDocuments,
...(Object.keys(requestedRuntime).length ? { runtime: requestedRuntime } : {}),
...(strictReadOnly ? { readOnly: true } : {}),
surfaceName,
diff --git a/src/harness/pi-harness.ts b/src/harness/pi-harness.ts
index 7511fd2b0..1cea8ba63 100644
--- a/src/harness/pi-harness.ts
+++ b/src/harness/pi-harness.ts
@@ -1771,7 +1771,8 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {
);
}
try {
- finalPayload = trimPayloadToByteBudget(finalPayload);
+ const gateway = modelGatewayRequest(turnProviderKeys ? undefined : modelGateway, model as Model<Api>);
+ finalPayload = trimPayloadToByteBudget(finalPayload, gateway ? 9_000_000 : undefined);
} catch (e) {
swallow("pi: request byte budget", e);
}
diff --git a/test/document-inputs.test.ts b/test/document-inputs.test.ts
index 01ff22d52..b6be2e141 100644
--- a/test/document-inputs.test.ts
+++ b/test/document-inputs.test.ts
@@ -214,6 +214,54 @@ test("restores stopped/restarted turns from durable document metadata and dedupl
assert.match(await documentFallbackText(inputs.documents[0]!), /QUARTZ-731/);
});
+test("reuploaded identical documents consume the input budget once and retain the newest artifact", async () => {
+ const { store, meta, artifact, opens } = storeFor();
+ store.get = async (id) => ({ ...artifact, id });
+ const open = store.open.bind(store);
+ store.open = async (id) => {
+ const loaded = await open(id);
+ return loaded && { ...loaded, artifact: { ...loaded.artifact, id } };
+ };
+ const checked: string[] = [];
+ const inputs = await loadDocumentInputs(
+ store,
+ [meta, { ...meta, artifactId: "new-upload" }],
+ (file) => {
+ checked.push(file.id);
+ return true;
+ },
+ artifact.sizeBytes,
+ );
+ assert.equal(inputs.documents.length, 1);
+ assert.equal(inputs.documents[0]!.artifactId, "new-upload");
+ assert.deepEqual(inputs.notices, []);
+ assert.equal(opens(), 1);
+ assert.ok(checked.includes(artifact.id));
+ assert.ok(checked.includes("new-upload"));
+});
+
+test("steered reuploads already in the turn do not consume another document slot or emit budget notices", async () => {
+ const { store, meta, artifact, opens } = storeFor();
+ const initial = await loadDocumentInputs(store, [meta], () => true);
+ store.get = async (id) => ({ ...artifact, id });
+ const checked: string[] = [];
+ const steered = await loadDocumentInputs(
+ store,
+ [{ ...meta, artifactId: "steered-upload" }],
+ (file) => {
+ checked.push(file.id);
+ return true;
+ },
+ 0,
+ undefined,
+ 0,
+ initial.documents,
+ );
+ assert.deepEqual(steered, { documents: [], notices: [] });
+ assert.deepEqual(checked, ["steered-upload"]);
+ assert.equal(opens(), 1);
+});
+
test("document replay follows compaction boundaries and excludes security-tainted uploads", () => {
const { meta } = storeFor();
const recent = { ...meta, artifactId: "recent" };
diff --git a/test/pi-harness-tape-sync.test.ts b/test/pi-harness-tape-sync.test.ts
index 4e8974a11..844e2eff9 100644
--- a/test/pi-harness-tape-sync.test.ts
+++ b/test/pi-harness-tape-sync.test.ts
@@ -331,6 +331,55 @@ test("completed Pi attach results retain openable files in viewer history", asyn
}
});
+for (const route of ["gateway", "direct", "personal"] as const) {
+ test(`native document byte budget respects ${route} routing`, async () => {
+ const modelId = "claude-opus-5";
+ const harness = createPiHarness({
+ apiKey: "sk-test",
+ modelId,
+ ...(route !== "direct"
+ ? {
+ modelGateway: {
+ url: "https://gateway.test",
+ apiKey: "gateway-test",
+ apiKeyHeader: "api-key",
+ models: { [modelId]: "router/opus" },
+ },
+ }
+ : {}),
+ });
+ const sink: Sink = { entries: [], tape: [] };
+ const pdf = await readFile(new URL("./fixtures/documents/sample.pdf", import.meta.url));
+ const dataBase64 = Buffer.concat([pdf, Buffer.alloc(4_000_000 - pdf.length, 32)]).toString("base64");
+ const realFetch = globalThis.fetch;
+ let body = "";
+ globalThis.fetch = (async (_url: unknown, init?: RequestInit) => {
+ body = String(init?.body);
+ return sse(textReplyEvents("read the document"));
+ }) as typeof fetch;
+ try {
+ await harness.turns.runTurn(
+ turnInput(`document-budget-${route}`, sink, {
+ ...(route === "personal" ? { providerKeys: { anthropic: "personal-test" } } : {}),
+ documents: [
+ { name: "first.pdf", mimeType: "application/pdf", dataBase64, artifactId: "doc1" },
+ { name: "latest.pdf", mimeType: "application/pdf", dataBase64, artifactId: "doc2" },
+ ],
+ }),
+ );
+ const blocks = JSON.parse(body).messages.at(-1).content;
+ assert.equal(
+ blocks.filter((block: { type: string }) => block.type === "document").length,
+ route === "gateway" ? 1 : 2,
+ );
+ assert.equal(Buffer.byteLength(body) < 10 * 1024 * 1024, route === "gateway");
+ assert.ok(body.includes(dataBase64));
+ } finally {
+ globalThis.fetch = realFetch;
+ }
+ });
+}
+
test("native document bytes reach the provider on every step without entering the transcript tape", async () => {
const harness = createPiHarness({ apiKey: "sk-test" });
const sink: Sink = { entries: [], tape: [] };
diff --git a/test/system-prompt-order.test.ts b/test/system-prompt-order.test.ts
index 7448210a4..8946ca337 100644
--- a/test/system-prompt-order.test.ts
+++ b/test/system-prompt-order.test.ts
@@ -30,7 +30,7 @@ import type { Sandbox } from "../src/sandbox/sandbox.ts";
import type { ConnectorStatusCache } from "../src/credentials/connector-status.ts";
import type { ConnectorTokenStore } from "../src/credentials/keychain.ts";
import { createSkillStore, type SkillStore } from "../src/skills/skill-store.ts";
-import { scopeId, type Conversation, type Principal } from "../src/types.ts";
+import { scopeId, type Conversation, type Principal, type TurnRequest } from "../src/types.ts";
import type { ManagedGroupDirectory } from "../src/resolution/scope-membership.ts";
import type { IsCurrentSharedScopeMember } from "../src/resolution/scope-membership.ts";
@@ -982,11 +982,11 @@ test("steered documents use the inbound security screen before writing their con
);
});
-for (const budget of ["count", "bytes"] as const) {
- test(`steered documents share the initial turn ${budget} budget`, async () => {
+for (const budget of ["count", "bytes", "reupload", "retry"] as const) {
+ test(`steered documents respect the initial turn budget (${budget})`, async () => {
const blobTransfer = createMemoryBlobTransferStore();
const blob = await blobTransfer.put(Buffer.from(budget === "bytes" ? "x".repeat(7_999_995) : "initial"));
- const steerBlob = await blobTransfer.put(Buffer.from("steered document"));
+ const steerBlob = budget === "reupload" ? blob : await blobTransfer.put(Buffer.from("steered document"));
const sandbox: Sandbox = {
...readSandbox(),
removeDir: async () => {},
@@ -998,13 +998,23 @@ for (const budget of ["count", "bytes"] as const) {
let prepared: Awaited<ReturnType<NonNullable<HarnessTurnInput["prepareSteer"]>>> | undefined;
harness.turns.runTurn = async (turn) => {
assert.equal(turn.documents?.length, budget === "count" ? 10 : 1);
- prepared = await turn.prepareSteer!("read this", {
+ const request: TurnRequest = {
surface: "web",
actor: { externalId: actor.id },
conversation: { kind: "dm", threadRef: `steer-budget-${budget}` },
text: "read this",
- attachments: [{ name: "steered.txt", mimetype: "text/plain", ...steerBlob }],
- });
+ attachments: [
+ { name: budget === "reupload" ? "initial-0.txt" : "steered.txt", mimetype: "text/plain", ...steerBlob },
+ ],
+ };
+ prepared = await turn.prepareSteer!("read this", request);
+ if (budget === "retry") {
+ assert.equal(prepared.documents?.length, 1);
+ prepared = await turn.prepareSteer!("read this", request);
+ assert.equal(turn.documents?.length, 1);
+ }
+ if (budget === "reupload")
+ assert.notEqual(prepared.attachments?.[0]?.artifactId, turn.documents?.[0]?.artifactId);
return { reply: "done" };
};
const { orchestrator, config } = buildOrchestrator({ harness, sandbox, blobTransfer });
@@ -1018,8 +1028,10 @@ for (const budget of ["count", "bytes"] as const) {
})),
});
assert.equal(result.status, "ok", result.reason);
- assert.deepEqual(prepared?.documents, []);
- assert.match(prepared?.text ?? "", /steered.txt.*count or byte budget/);
+ if (budget === "retry") assert.equal(prepared?.documents?.length, 1);
+ else assert.deepEqual(prepared?.documents, []);
+ if (budget === "reupload" || budget === "retry") assert.doesNotMatch(prepared?.text ?? "", /count or byte budget/);
+ else assert.match(prepared?.text ?? "", /steered.txt.*count or byte budget/);
});
}