Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Respect compaction when replaying document attachments (#1499)

Regan Bell0de9b3bb5371
diff --git a/src/core/document-inputs.ts b/src/core/document-inputs.ts
index 0e2d5317d..348398d3d 100644
--- a/src/core/document-inputs.ts
+++ b/src/core/document-inputs.ts
@@ -2,6 +2,7 @@ import { addAbortSignal } from "node:stream";
import { Worker } from "node:worker_threads";
import type { AttachmentMeta, SessionEntry } from "../types.ts";
import type { FileArtifact, FileArtifactStore } from "../files/file-artifact-store.ts";
+import { forModelContext } from "../harness/context-compaction.ts";
import { collectBytes } from "../util/bytes.ts";
export interface DocumentInput {
@@ -62,7 +63,7 @@ export function isTextDocument(file: Pick<DocumentInput, "name" | "mimeType">):
}
export function historicalDocumentMetas(history: readonly SessionEntry[]): AttachmentMeta[] {
- return history.flatMap((entry) => {
+ return forModelContext([...history]).flatMap((entry) => {
if (entry.type !== "user") return [];
const attachments = (entry.payload as { attachments?: AttachmentMeta[] } | null)?.attachments;
return Array.isArray(attachments)
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index ca4161223..9a60474d7 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -2695,22 +2695,78 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
MAX_HISTORY_IMAGE_BYTES,
);
};
+ const tapeRows = await (async () => {
+ if (historyHasSecurityTaint || contextWindow.totalEntries > TAPE_IMPORT_MAX_ENTRIES) return undefined;
+ try {
+ const preAppended = new Set(preAppendedSeqs);
+ const priorMaxSeq = rawEntries.reduce((m, e) => (preAppended.has(e.seq) ? m : Math.max(m, e.seq)), -1);
+ let covered = priorMaxSeq < 0 || (await deps.sessions.tapeCoverage(session.id)) >= priorMaxSeq;
+ let rows = filterTapeForAudience(
+ await deps.sessions.getTape(session.id),
+ conversation.audience,
+ scopeId,
+ resolution.orgScopeId,
+ );
+ const sameHarness = rows.every(
+ (row) => row.kind !== "message" || row.harness === undefined || row.harness === "pi",
+ );
+ if (
+ (!covered || lastImportLacksScopes(rows)) &&
+ deps.sessionTapeMode === "serve" &&
+ sameHarness &&
+ participantHistorySeqs === undefined
+ ) {
+ const imported = await appendCoverageImport(deps.sessions, lease, rawEntries, scopeId);
+ if (imported) {
+ console.log(
+ `[tape-heal] session=${session.id} covers=${imported.coversEntrySeq} messages=${
+ (imported.payload as { messages: unknown[] }).messages.length
+ }`,
+ );
+ rows = [...rows, imported];
+ covered = true;
+ }
+ }
+ const eventsEntitled = tapeEventsEntitled(rows, conversation.audience, scopeId, resolution.orgScopeId);
+ const eligible =
+ deps.sessionTapeMode === "serve" &&
+ covered &&
+ sameHarness &&
+ eventsEntitled &&
+ participantHistorySeqs === undefined;
+ let fold = eligible ? await rehydrateTape(foldTape(rows)) : undefined;
+ if (eligible && rows.length && fold && tapeNeedsInterruptHeal(rows, fold)) {
+ const interrupt = await deps.sessions.appendTape(lease, {
+ kind: "context_event",
+ payload: { event: "interrupt" },
+ scopeLabel: scopeId,
+ });
+ rows = [...rows, interrupt];
+ fold = healFoldInterrupt(fold, interrupt.createdAt);
+ }
+ const serve = eligible && !!fold?.length && lintFold(fold).ok;
+ return { rows, serve, covered, fold };
+ } catch (e) {
+ swallow("tape: read/heal", e);
+ return undefined;
+ }
+ })();
+ const compactStart = Date.now();
+ const history = await compactContextIfNeeded({
+ session,
+ lease,
+ visibleHistory,
+ scopeId,
+ orgScopeId: resolution.orgScopeId,
+ actorId: actor.id,
+ ...(input.model ? { model: input.model } : {}),
+ });
+ compactMs = Date.now() - compactStart;
const documentInputs = strictReadOnly
? { documents: [], notices: [] }
: await loadDocumentInputs(
deps.files,
- [
- ...historicalDocumentMetas(
- filterHistory(
- forSearchView(
- contextWindow.totalEntries > rawEntries.length
- ? await deps.sessions.getEntries(session.id)
- : rawEntries,
- ),
- ),
- ),
- ...inbound.metas,
- ],
+ [...historicalDocumentMetas(history), ...inbound.metas],
mayReadArtifact,
undefined,
turnAbort.signal,
@@ -2770,62 +2826,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
MAX_DOCUMENT_BYTES -
documentInputs.documents.reduce((sum, document) => sum + Buffer.byteLength(document.dataBase64, "base64"), 0);
let remainingDocumentCount = 10 - documentInputs.documents.length;
- const tapeRows = await (async () => {
- if (historyHasSecurityTaint || contextWindow.totalEntries > TAPE_IMPORT_MAX_ENTRIES) return undefined;
- try {
- const preAppended = new Set(preAppendedSeqs);
- const priorMaxSeq = rawEntries.reduce((m, e) => (preAppended.has(e.seq) ? m : Math.max(m, e.seq)), -1);
- let covered = priorMaxSeq < 0 || (await deps.sessions.tapeCoverage(session.id)) >= priorMaxSeq;
- let rows = filterTapeForAudience(
- await deps.sessions.getTape(session.id),
- conversation.audience,
- scopeId,
- resolution.orgScopeId,
- );
- const sameHarness = rows.every(
- (row) => row.kind !== "message" || row.harness === undefined || row.harness === "pi",
- );
- if (
- (!covered || lastImportLacksScopes(rows)) &&
- deps.sessionTapeMode === "serve" &&
- sameHarness &&
- participantHistorySeqs === undefined
- ) {
- const imported = await appendCoverageImport(deps.sessions, lease, rawEntries, scopeId);
- if (imported) {
- console.log(
- `[tape-heal] session=${session.id} covers=${imported.coversEntrySeq} messages=${
- (imported.payload as { messages: unknown[] }).messages.length
- }`,
- );
- rows = [...rows, imported];
- covered = true;
- }
- }
- const eventsEntitled = tapeEventsEntitled(rows, conversation.audience, scopeId, resolution.orgScopeId);
- const eligible =
- deps.sessionTapeMode === "serve" &&
- covered &&
- sameHarness &&
- eventsEntitled &&
- participantHistorySeqs === undefined;
- let fold = eligible ? await rehydrateTape(foldTape(rows)) : undefined;
- if (eligible && rows.length && fold && tapeNeedsInterruptHeal(rows, fold)) {
- const interrupt = await deps.sessions.appendTape(lease, {
- kind: "context_event",
- payload: { event: "interrupt" },
- scopeLabel: scopeId,
- });
- rows = [...rows, interrupt];
- fold = healFoldInterrupt(fold, interrupt.createdAt);
- }
- const serve = eligible && !!fold?.length && lintFold(fold).ok;
- return { rows, serve, covered, fold };
- } catch (e) {
- swallow("tape: read/heal", e);
- return undefined;
- }
- })();
const principalDelivered = await recentPrincipalDeliveryNote(deps.deliveries, session.threadRef);
const sender = !automatedTurn && input.text.trim() ? senderNote(actor.displayName) : "";
const unscreenedNote =
@@ -2886,17 +2886,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
) {
void provision(true).catch(swallowAs("orchestrator: eager provision", undefined));
}
- const compactStart = Date.now();
- const history = await compactContextIfNeeded({
- session,
- lease,
- visibleHistory,
- scopeId,
- orgScopeId: resolution.orgScopeId,
- actorId: actor.id,
- ...(input.model ? { model: input.model } : {}),
- });
- compactMs = Date.now() - compactStart;
const turnStart = Date.now();
let firstChunkAt: number | undefined;
let lastChunkAt: number | undefined;
diff --git a/test/document-inputs.test.ts b/test/document-inputs.test.ts
index 651277b37..01ff22d52 100644
--- a/test/document-inputs.test.ts
+++ b/test/document-inputs.test.ts
@@ -214,6 +214,19 @@ test("restores stopped/restarted turns from durable document metadata and dedupl
assert.match(await documentFallbackText(inputs.documents[0]!), /QUARTZ-731/);
});
+test("document replay follows compaction boundaries and excludes security-tainted uploads", () => {
+ const { meta } = storeFor();
+ const recent = { ...meta, artifactId: "recent" };
+ const history = [
+ { seq: 0, type: "user", payload: { attachments: [meta] } },
+ { seq: 1, type: "user", payload: { attachments: [recent] } },
+ { seq: 2, type: "system", payload: { kind: "context_summary", text: "Prior upload", throughSeq: 0 } },
+ { seq: 3, type: "user", payload: { attachments: [meta], securityTainted: true } },
+ ] as SessionEntry[];
+ assert.deepEqual(historicalDocumentMetas(history), [recent]);
+ assert.deepEqual((history[0]!.payload as { attachments: AttachmentMeta[] }).attachments, [meta]);
+});
+
test("document restoration checks current access and budgets before opening bytes", async () => {
const { store, meta, opens } = storeFor();
assert.equal((await loadDocumentInputs(store, [meta], () => false)).documents.length, 0);
diff --git a/test/orchestrator-documents.test.ts b/test/orchestrator-documents.test.ts
index 007a98617..e5d5f96a1 100644
--- a/test/orchestrator-documents.test.ts
+++ b/test/orchestrator-documents.test.ts
@@ -8,8 +8,8 @@ import { join } from "node:path";
import { buildApp } from "../src/wiring.ts";
import { testConfig } from "./support/test-config.ts";
-function freshApp() {
- return buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "document-turn-")) }));
+function freshApp(maxContextTokens?: number) {
+ return buildApp(testConfig({ dataDir: mkdtempSync(join(tmpdir(), "document-turn-")), maxContextTokens }));
}
test("document uploads survive follow-up turns and stay isolated to their conversation", async () => {
@@ -41,7 +41,7 @@ test("document uploads survive follow-up turns and stay isolated to their conver
assert.doesNotMatch(JSON.stringify(otherRequests), /notes.txt/);
});
-test("documents remain available after conversation compaction", async () => {
+test("compaction stops automatic document replay while preserving the original file", async () => {
const built = freshApp();
const blob = await built.blobTransfer.put(Buffer.from("QUARTZ-731"));
const request = {
@@ -55,6 +55,11 @@ test("documents remain available after conversation compaction", async () => {
attachments: [{ name: "notes.txt", mimetype: "text/plain", sizeBytes: blob.sizeBytes, blobId: blob.blobId }],
});
const entries = await built.sessions.getEntries(first.sessionId!);
+ const attachment = (
+ entries.find((entry) => entry.type === "user")!.payload as {
+ attachments: Array<{ artifactId: string }>;
+ }
+ ).attachments[0]!;
const { lease } = await built.sessions.acquireLease(first.sessionId!, "compaction");
assert.ok(lease);
await built.sessions.append(lease, {
@@ -66,7 +71,66 @@ test("documents remain available after conversation compaction", async () => {
const result = await built.app.turn({ ...request, text: "what was in the document?" });
assert.equal(result.status, "ok");
const calls = (await built.sessions.listLlmRequests(result.sessionId!)).filter((record) => record.model === "mock");
- assert.match(JSON.stringify(calls.at(-1)?.promptEnvelope), /"documents":\[\{"name":"notes.txt"/);
+ assert.doesNotMatch(JSON.stringify(calls.at(-1)?.promptEnvelope), /"documents":/);
+ const artifact = await built.files.get(attachment.artifactId);
+ assert.equal(artifact?.enabled, true);
+ const opened = await built.files.open(attachment.artifactId);
+ assert.ok(opened);
+ const chunks = [];
+ for await (const chunk of opened.stream) chunks.push(Buffer.from(chunk));
+ assert.equal(Buffer.concat(chunks).toString(), "QUARTZ-731");
+ const original = await built.sessions.getEntry(first.sessionId!, entries[0]!.seq);
+ assert.deepEqual(original, entries[0]);
+});
+
+test("compaction during a turn stops document replay immediately", async () => {
+ const built = freshApp(2000);
+ const blob = await built.blobTransfer.put(Buffer.from("QUARTZ-731"));
+ const request = {
+ surface: "test" as const,
+ actor: { externalId: "U1" },
+ conversation: { kind: "dm" as const, threadRef: "dm:U1:compacted-docs" },
+ };
+ const first = await built.app.turn({
+ ...request,
+ text: "summarize",
+ attachments: [{ name: "notes.txt", mimetype: "text/plain", sizeBytes: blob.sizeBytes, blobId: blob.blobId }],
+ });
+ const entries = await built.sessions.getEntries(first.sessionId!);
+ const attachment = (
+ entries.find((entry) => entry.type === "user")!.payload as {
+ attachments: Array<{ artifactId: string }>;
+ }
+ ).attachments[0]!;
+ const { lease } = await built.sessions.acquireLease(first.sessionId!, "compaction");
+ assert.ok(lease);
+ for (let i = 0; i < 8; i++) {
+ await built.sessions.append(lease, {
+ type: i % 2 === 0 ? "user" : "assistant",
+ payload: { text: "Synthetic conversation context. ".repeat(100) },
+ scopeLabel: entries[0]!.scopeLabel,
+ });
+ }
+ await built.sessions.releaseLease(lease);
+ const result = await built.app.turn({ ...request, text: "what was in the document?" });
+ assert.equal(result.status, "ok");
+ const compacted = await built.sessions.getEntries(first.sessionId!);
+ assert.ok(
+ compacted.some(
+ (entry) => entry.type === "system" && (entry.payload as { kind?: string }).kind === "context_summary",
+ ),
+ );
+ const calls = (await built.sessions.listLlmRequests(result.sessionId!)).filter((record) => record.model === "mock");
+ assert.doesNotMatch(JSON.stringify(calls.at(-1)?.promptEnvelope), /"documents":/);
+ const artifact = await built.files.get(attachment.artifactId);
+ assert.equal(artifact?.enabled, true);
+ const opened = await built.files.open(attachment.artifactId);
+ assert.ok(opened);
+ const chunks = [];
+ for await (const chunk of opened.stream) chunks.push(Buffer.from(chunk));
+ assert.equal(Buffer.concat(chunks).toString(), "QUARTZ-731");
+ const original = await built.sessions.getEntry(first.sessionId!, entries[0]!.seq);
+ assert.deepEqual(original, entries[0]);
});
for (const extension of ["docx", "pdf"]) {