Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Recover failed compaction with bounded recent context (#1634)

Joshua Franceb5bd49bd297a
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index 49b74eb6f..e903753de 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -43,6 +43,7 @@ import { SESSION_BUSY_FIRE_TEXT, SESSION_BUSY_USER_TEXT } from "./failure-copy.t
import { CONFIG_DEFAULTS } from "../config.ts";
import {
acquireLeaseWithin,
+ entrySecurityTainted,
isOverheardEntry,
TAPE_IMPORT_MAX_ENTRIES,
tapeCheckpointPayload,
@@ -299,7 +300,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
}
const securitySteersInFlight = new Set<string>();
- const { compactContextIfNeeded, scheduleBackgroundCompaction } = createCompaction(deps);
+ const { compactContextIfNeeded, compactRecent, scheduleBackgroundCompaction } = createCompaction(deps);
async function approvalSummary(
scopeId: ScopeId,
@@ -1940,6 +1941,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
};
let tailOwnsCleanup = false;
let leaseReleased = false;
+ let contextRecovered = false;
let turnProgress = 0;
const turnAbort = new AbortController();
if (input.cancel?.aborted) turnAbort.abort();
@@ -2872,6 +2874,9 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
covered &&
sameHarness &&
eventsEntitled &&
+ !rows.some(
+ (row) => row.kind === "context_event" && isObj(row.payload) && row.payload.mode === "recent",
+ ) &&
participantHistorySeqs === undefined;
let fold = eligible ? await rehydrateTape(foldTape(rows)) : undefined;
if (eligible && rows.length && fold && tapeNeedsInterruptHeal(rows, fold)) {
@@ -2891,15 +2896,21 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
}
})();
const compactStart = Date.now();
- const history = await compactContextIfNeeded({
- session,
- lease,
- visibleHistory,
- scopeId,
- orgScopeId: resolution.orgScopeId,
- actorId: actor.id,
- ...(input.model ? { model: input.model } : {}),
- });
+ const history = await withManagedRosterVersion(() =>
+ compactContextIfNeeded({
+ cancel: turnAbort.signal,
+ session,
+ lease,
+ visibleHistory,
+ scopeId,
+ orgScopeId: resolution.orgScopeId,
+ actorId: actor.id,
+ ...(input.model ? { model: input.model } : {}),
+ }),
+ );
+ contextRecovered =
+ history !== visibleHistory &&
+ history.some((entry) => isObj(entry.payload) && entry.payload.mode === "recent");
compactMs = Date.now() - compactStart;
const documentInputs = strictReadOnly
? { documents: [], notices: [] }
@@ -2992,7 +3003,18 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
String((pausedTurnUserEntry.payload as { text?: string } | null)?.text ?? "").trim() === input.text.trim();
const partial = isRetry ? (recordedTurn ?? findTrailingPartialTurn(visibleHistory, input.text)) : null;
const resume = partial && partial.workEntries > 0 ? partial : null;
- if (partial) postKeys.seed(completedSurfaceEnqueues(visibleHistory, partial.userSeq, surfaceName));
+ if (partial)
+ postKeys.seed(
+ completedSurfaceEnqueues(
+ filterHistory(
+ (await deps.sessions.getEntries(session.id, { sinceSeq: partial.userSeq })).filter(
+ (entry) => !entrySecurityTainted(entry),
+ ),
+ ),
+ partial.userSeq,
+ surfaceName,
+ ),
+ );
if (partial) {
deps.auditLog.record({
at: Date.now(),
@@ -3009,6 +3031,8 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
);
}
let turnInput = partial ? resumeNote({ backgroundJobs: !!backgroundBroker, workRecorded: !!resume }) : baseText;
+ if (partial && !history.some((entry) => entry.seq === partial.userSeq))
+ turnInput += `\nCurrent request (continue from recorded work; do not restart):\n${baseText}`;
if (releasedToolOutput) {
turnInput = `The human released quarantined tool output recorded in the conversation. Continue the original task using that output. The tool action already ran; do not repeat it. Original task: ${baseText}`;
}
@@ -3671,13 +3695,31 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
if (++runtimeHandoffs > 8) throw new NonRetryableTurnError("Too many runtime changes in one task");
if (effectiveTurnWallClockMs && Date.now() - turnStart >= effectiveTurnWallClockMs)
throw new NonRetryableTurnError("The task reached its wall-clock limit during runtime handoff");
- await adoptRuntime(segment.runtimeHandoff.choice);
- await deps.harness.turns.resetSession?.(session.id);
- const resumedHistory = filterHistory(
+ const recovery = "context" in segment.runtimeHandoff;
+ if ("choice" in segment.runtimeHandoff) {
+ await adoptRuntime(segment.runtimeHandoff.choice);
+ await deps.harness.turns.resetSession?.(session.id);
+ }
+ let resumedHistory = filterHistory(
forModelContext((await deps.sessions.getContextWindow(session.id)).entries, {
includeSecurityTainted: false,
}),
);
+ if (recovery)
+ resumedHistory = await withManagedRosterVersion(() =>
+ compactRecent({
+ session,
+ lease,
+ visibleHistory: resumedHistory,
+ scopeId,
+ orgScopeId: resolution.orgScopeId,
+ actorId: actor.id,
+ ...(requestedRuntime.modelId ? { model: requestedRuntime.modelId } : {}),
+ cancel: turnAbort.signal,
+ }),
+ );
+ contextRecovered ||= recovery;
+ turnAbort.signal.throwIfAborted();
const resumedTape = tapeRows
? {
rows: filterTapeForAudience(
@@ -3691,7 +3733,18 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
: undefined;
segment = await runHarnessSegment(
resumeNote() +
- "\nRuntime handoff completed. Continue the user's unfinished request using the saved conversation and tool results. Do not repeat completed actions or ask the user to repeat the request.",
+ (recovery ? "\nContext reduced without a new summary." : "\nRuntime handoff completed.") +
+ " Continue the user's unfinished request using the saved conversation and tool results. Do not repeat completed actions or ask the user to repeat the request." +
+ (recovery &&
+ !resumedHistory.some(
+ (entry) =>
+ entry.type === "user" &&
+ isObj(entry.payload) &&
+ typeof entry.payload.text === "string" &&
+ entry.payload.text.startsWith(baseText),
+ )
+ ? `\nCurrent request (continue from recorded work; do not restart):\n${baseText}`
+ : ""),
inbound.images.length ? { images: inbound.images } : {},
{ history: resumedHistory, ...(resumedTape ? { tape: resumedTape } : {}) },
);
@@ -3803,7 +3856,15 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
row.entrySeq === primarySubturnEndSeq &&
(row.payload as { subturnEnd?: unknown } | null)?.subturnEnd === true,
);
- if (primaryServedTape && sameHarness && eventsEntitled && primarySubturnComplete) {
+ if (
+ primaryServedTape &&
+ sameHarness &&
+ eventsEntitled &&
+ primarySubturnComplete &&
+ !rows.some(
+ (row) => row.kind === "context_event" && isObj(row.payload) && row.payload.mode === "recent",
+ )
+ ) {
const fold = await rehydrateTape(foldTape(rows));
if (fold.length && lintFold(fold).ok) return { rows, mode: "serve" as const, fold };
}
@@ -4248,13 +4309,14 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
await catchUpMessageRevisions();
await deps.sessions.releaseLease(lease);
}
- scheduleBackgroundCompaction({
- sessionId: session.id,
- scopeId,
- orgScopeId: resolution.orgScopeId,
- actorId: actor.id,
- ...(input.model ? { model: input.model } : {}),
- });
+ if (!contextRecovered && !turnAbort.signal.aborted)
+ scheduleBackgroundCompaction({
+ sessionId: session.id,
+ scopeId,
+ orgScopeId: resolution.orgScopeId,
+ actorId: actor.id,
+ ...(input.model ? { model: input.model } : {}),
+ });
}
},
};
diff --git a/src/core/orchestrator/compaction.ts b/src/core/orchestrator/compaction.ts
index 90dced42d..bd6f81b47 100644
--- a/src/core/orchestrator/compaction.ts
+++ b/src/core/orchestrator/compaction.ts
@@ -15,6 +15,7 @@ import {
validateCompactSummary,
compactionThroughSeq,
estimateEntryTokens,
+ estimateHistoryTokens,
forModelContext,
overBudgetFraction,
planCompaction,
@@ -34,6 +35,7 @@ interface Summarized {
summaryLabel: ScopeId;
throughSeq: number;
securityTainted: boolean;
+ mode?: "recent";
}
export interface CompactionContext {
@@ -45,6 +47,17 @@ export interface CompactionContext {
orgScopeId: string;
actorId: string;
model?: string;
+ cancel?: AbortSignal;
+ }): Promise<SessionEntry[]>;
+ compactRecent(input: {
+ session: Session;
+ lease: Lease;
+ visibleHistory: SessionEntry[];
+ scopeId: string;
+ orgScopeId: string;
+ actorId: string;
+ model?: string;
+ cancel?: AbortSignal;
}): Promise<SessionEntry[]>;
scheduleBackgroundCompaction(input: {
sessionId: string;
@@ -77,14 +90,18 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
const keepRecentTokenFraction = Math.min(KEEP_RECENT_TOKEN_FRACTION, COMPACT_SOFT_FRACTION - 0.1);
- async function summarizeForCompaction(input: {
- session: Session;
- visibleHistory: SessionEntry[];
- scopeId: string;
- orgScopeId: string;
- actorId: string;
- model?: string;
- }): Promise<Summarized | null> {
+ async function summarizeForCompaction(
+ input: {
+ session: Session;
+ visibleHistory: SessionEntry[];
+ scopeId: string;
+ orgScopeId: string;
+ actorId: string;
+ model?: string;
+ cancel?: AbortSignal;
+ },
+ recoverOnFailure = false,
+ ): Promise<Summarized | null> {
if (isManagedGroupScope(input.scopeId)) return null;
if (!deps.harness.models.compactHistory) return null;
const maxContextTokens = tokenBudgetFor(input.scopeId, input.model);
@@ -94,15 +111,34 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
const summaryLabel = input.scopeId;
- const raw = await deps.harness.models.compactHistory({
- session: input.session,
- history: plan.toSummarize,
- recordModelCall: (rec) => {
- deps.modelGateway.recordCall({ at: Date.now(), scopeLabel: summaryLabel, ...rec });
- void deps.budget?.record(input.actorId, estimateCostUsd(rec.inputTokens));
- },
- });
- const text = validateCompactSummary(raw);
+ input.cancel?.throwIfAborted();
+ let text: string;
+ try {
+ const raw = await deps.harness.models.compactHistory({
+ session: input.session,
+ history: plan.toSummarize,
+ recordModelCall: (rec) => {
+ deps.modelGateway.recordCall({ at: Date.now(), scopeLabel: summaryLabel, ...rec });
+ void deps.budget?.record(input.actorId, estimateCostUsd(rec.inputTokens));
+ },
+ });
+ text = validateCompactSummary(raw);
+ } catch (error) {
+ input.cancel?.throwIfAborted();
+ if (!recoverOnFailure || (error instanceof Error && error.name === "AbortError")) throw error;
+ deps.errors?.record(
+ {
+ category: "turn",
+ code: "compaction_summary_failed",
+ message: errMessage(error),
+ scopeLabel: input.scopeId as ScopeId,
+ sessionId: input.session.id,
+ },
+ error,
+ );
+ return null;
+ }
+ input.cancel?.throwIfAborted();
return {
text,
summaryLabel,
@@ -119,7 +155,7 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
summarized: Summarized;
goalSource: SessionEntry | null;
}): Promise<{ summary: SessionEntry; goalEntry?: SessionEntry }> {
- const { text, summaryLabel, throughSeq, securityTainted } = input.summarized;
+ const { text, summaryLabel, throughSeq, securityTainted, mode } = input.summarized;
const goal = latestGoalRecord(input.goalSource ? [input.goalSource] : []);
const goalEntry =
goal && input.goalSource
@@ -137,13 +173,14 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
type: "system",
payload: {
...createContextSummaryPayload(throughSeq, text),
+ ...(mode ? { mode } : {}),
...(securityTainted ? { securityTainted: true } : {}),
},
scopeLabel: summaryLabel,
});
await deps.sessions.appendTape(input.lease, {
kind: "context_event",
- payload: { event: "compaction", text },
+ payload: { event: "compaction", text, ...(mode ? { mode } : {}) },
scopeLabel: summaryLabel as ScopeId,
entrySeq: summary.seq,
coversEntrySeq: throughSeq,
@@ -172,8 +209,9 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
orgScopeId: string;
actorId: string;
model?: string;
+ cancel?: AbortSignal;
}): Promise<SessionEntry[] | null> {
- const summarized = await summarizeForCompaction(input);
+ const summarized = await summarizeForCompaction(input, true);
if (!summarized) return null;
const { summary, goalEntry } = await writeCompaction({
...input,
@@ -197,13 +235,89 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
orgScopeId: string;
actorId: string;
model?: string;
+ cancel?: AbortSignal;
}): Promise<SessionEntry[]> {
const maxContextTokens = tokenBudgetFor(input.scopeId, input.model);
if (!overBudgetFraction(input.visibleHistory, maxContextTokens, COMPACT_HARD_FRACTION)) {
return input.visibleHistory;
}
const rebuilt = await applyCompaction(input);
- return rebuilt ?? boundRecent(input.visibleHistory, maxContextTokens);
+ return (
+ rebuilt ??
+ (!deps.harness.models.compactHistory || isManagedGroupScope(input.scopeId)
+ ? boundRecent(input.visibleHistory, maxContextTokens)
+ : compactRecent(input))
+ );
+ }
+
+ async function compactRecent(input: Parameters<CompactionContext["compactRecent"]>[0]): Promise<SessionEntry[]> {
+ input.cancel?.throwIfAborted();
+ const budget = Math.floor(tokenBudgetFor(input.scopeId, input.model) * keepRecentTokenFraction);
+ const goalSource = latestGoalEntry(input.visibleHistory);
+ const prior = input.visibleHistory.findLast((entry) => contextSummaryPayload(entry));
+ const rest = input.visibleHistory.filter((entry) => !contextSummaryPayload(entry));
+ const notice =
+ "[Context reduced without a new summary. Earlier entries are excluded, not deleted. Use history to reopen missing requests and tool calls; history does not return tool results. Check actual state before repeating actions whose outcomes are missing.]";
+ const marker: SessionEntry = {
+ sessionId: input.session.id,
+ seq: -1,
+ parentSeq: null,
+ type: "system",
+ payload: { text: `${notice}\n\nLast saved summary (not updated):\n` },
+ scopeLabel: input.scopeId as ScopeId,
+ createdAt: 0,
+ };
+ let remaining = Math.max(
+ 0,
+ budget - estimateEntryTokens(marker) - (goalSource ? estimateEntryTokens(goalSource) : 0),
+ );
+ let start = rest.length;
+ while (start > 0) {
+ const cost = estimateEntryTokens(rest[start - 1]!);
+ if (cost > remaining) break;
+ remaining -= cost;
+ start--;
+ }
+ const retainedCalls = new Set<unknown>();
+ for (let i = start; i < rest.length; i++) {
+ const entry = rest[i]!;
+ const callId = (entry.payload as { callId?: unknown } | null)?.callId;
+ if (entry.type === "tool_call" && callId) retainedCalls.add(callId);
+ if (entry.type === "tool_result" && !retainedCalls.has(callId)) {
+ start = i + 1;
+ retainedCalls.clear();
+ }
+ }
+ const recent = rest.slice(start);
+ remaining =
+ budget -
+ estimateEntryTokens(marker) -
+ (goalSource ? estimateEntryTokens(goalSource) : 0) -
+ estimateHistoryTokens(recent);
+ const includedPrior = prior && estimateEntryTokens(prior) <= remaining ? prior : undefined;
+ const priorText = includedPrior ? contextSummaryPayload(includedPrior)!.text : undefined;
+ let text = notice;
+ if (priorText)
+ text = priorText.startsWith(notice) ? priorText : `${notice}\n\nLast saved summary (not updated):\n${priorText}`;
+ const window = await deps.sessions.getContextWindow(input.session.id);
+ const boundary = window.entries.reduce(
+ (seq, entry) => Math.max(seq, contextSummaryPayload(entry)?.throughSeq ?? -1),
+ -1,
+ );
+ const throughSeq = Math.max(boundary, compactionThroughSeq([...(prior ? [prior] : []), ...rest.slice(0, start)]));
+ input.cancel?.throwIfAborted();
+ const { summary, goalEntry } = await writeCompaction({
+ ...input,
+ summarized: {
+ text,
+ summaryLabel: (includedPrior?.scopeLabel ?? input.scopeId) as ScopeId,
+ throughSeq,
+ securityTainted: !!includedPrior && entrySecurityTainted(includedPrior),
+ mode: "recent",
+ },
+ goalSource: goalSource && !recent.includes(goalSource) ? goalSource : null,
+ });
+ return [summary, ...recent, ...(goalEntry ? [goalEntry] : [])];
}
const WRITE_LEASE_WAIT_MS = 60_000;
@@ -278,5 +392,5 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
});
}
- return { compactContextIfNeeded, scheduleBackgroundCompaction };
+ return { compactContextIfNeeded, compactRecent, scheduleBackgroundCompaction };
}
diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.ts
index 4464f0589..fd6175625 100644
--- a/src/harness/agent-tools.ts
+++ b/src/harness/agent-tools.ts
@@ -2,7 +2,7 @@ import { MaskedExecutionError } from "../security/secret-masking.ts";
import type { DocumentInput } from "../core/document-inputs.ts";
import { createKeyedQueue } from "../util/async.ts";
import { createGrindMeter, grindState } from "./grind.ts";
-import type { RuntimeHandoff, RuntimeRequest } from "./runtime-types.ts";
+import type { HarnessHandoff, RuntimeRequest } from "./runtime-types.ts";
import { defineTool, type ToolDefinition } from "@earendil-works/pi-coding-agent";
import { Type, type TSchema } from "typebox";
import { Check, Clone } from "typebox/value";
@@ -49,7 +49,7 @@ function describePublishAudience(a: PublishAudienceDescriptor | undefined): stri
export interface ToolContextRef {
documents?: DocumentInput[];
- runtimeHandoff?: RuntimeHandoff;
+ runtimeHandoff?: HarnessHandoff;
runtimeRunId?: string;
runtimeActorId?: string;
runtimeMutationPending?: boolean;
@@ -4042,6 +4042,27 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
}),
);
+ const context = defineTool({
+ name: "context",
+ label: "context",
+ description:
+ "Reduce this conversation's model context without calling a summarizer. compact with mode recent keeps bounded recent entries, a saved summary if it fits, and structured goal state. Stored history is not deleted. This stops the current segment and resumes the unfinished request; do not repeat completed actions. Call this by itself after other tools finish.",
+ parameters: Type.Object({ action: Type.Literal("compact"), mode: Type.Literal("recent") }),
+ async execute(callId, params) {
+ await recordCall(callId, { tool: "context", ...params });
+ const result = await recordCoreAuthoredResult(
+ callId,
+ { tool: "context", ...params, ok: true },
+ {
+ ...text("Recent-context recovery requested. Continue from recorded results after the context is reduced."),
+ terminate: true,
+ },
+ );
+ ref.runtimeHandoff = { context: "recent" };
+ return result;
+ },
+ });
+
const runtime = defineTool({
name: "runtime",
label: "runtime",
@@ -4136,6 +4157,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
finishSilently,
resourceTool("goal", { create: createGoal, get: getGoal, update: updateGoal }),
runtime,
+ context,
...mcpTools,
...clientTools,
];
@@ -4254,7 +4276,8 @@ function withRuntimeBarrier(tool: ToolDefinition, ref: ToolContextRef): ToolDefi
details: {},
terminate: !!ref.runtimeHandoff,
};
- const mutation = tool.name === "runtime" && (!isObj(params) || params.action !== "get");
+ const mutation =
+ tool.name === "context" || (tool.name === "runtime" && (!isObj(params) || params.action !== "get"));
if (mutation) {
ref.runtimeMutationPending = true;
try {
diff --git a/src/harness/harness.ts b/src/harness/harness.ts
index d6e0aa32e..92a097a30 100644
--- a/src/harness/harness.ts
+++ b/src/harness/harness.ts
@@ -1,5 +1,5 @@
import type { DocumentInput } from "../core/document-inputs.ts";
-import type { RuntimeControl, RuntimeHandoff } from "./runtime-types.ts";
+import type { RuntimeControl, HarnessHandoff } from "./runtime-types.ts";
import type {
AttachmentMeta,
ClientToolDeclaration,
@@ -154,7 +154,7 @@ export interface HarnessTurnInput {
}
export interface HarnessTurnResult {
- runtimeHandoff?: RuntimeHandoff;
+ runtimeHandoff?: HarnessHandoff;
reply: string;
silent?: boolean;
stopped?: true;
diff --git a/src/harness/runtime-types.ts b/src/harness/runtime-types.ts
index 9d393de3c..d21d70336 100644
--- a/src/harness/runtime-types.ts
+++ b/src/harness/runtime-types.ts
@@ -10,11 +10,13 @@ export interface RuntimeRequest {
lifetime?: "task" | "scope";
}
-export interface RuntimeHandoff {
+interface RuntimeHandoff {
choice: RuntimeChoice;
lifetime: "task" | "scope";
}
+export type HarnessHandoff = RuntimeHandoff | { context: "recent" };
+
export type RuntimeResult =
| { ok: false; error: string; message?: string; candidates?: string[] }
| { ok: true; handoff?: RuntimeHandoff; [key: string]: unknown };
diff --git a/test/agent-tools.test.ts b/test/agent-tools.test.ts
index a3bf4baf1..ee418ab9c 100644
--- a/test/agent-tools.test.ts
+++ b/test/agent-tools.test.ts
@@ -1,4 +1,5 @@
import { test } from "node:test";
+import { createGoalRecord } from "../src/harness/goal.ts";
import assert from "node:assert/strict";
import { Check } from "typebox/value";
import { createAgentTools, pauseStampAfterToolCall, type ToolContextRef } from "../src/harness/agent-tools.ts";
@@ -3716,3 +3717,80 @@ test("a client tool stops waiting when the turn is cancelled", async () => {
assert.match(ret.content[0]?.text ?? "", /cancelled/);
assert.equal(emitted.find((e) => e.type === "tool_result")!.payload.cancelled, true);
});
+
+test("context recovery drains in-flight effects, preserves an active goal, and blocks subsequent calls", async () => {
+ const gate = Promise.withResolvers<void>();
+ const events: Emitted[] = [];
+ let effects = 0;
+ const ref: ToolContextRef = {
+ current: {
+ ...fakeToolContext(),
+ execute: async () => {
+ effects++;
+ await gate.promise;
+ return { stdout: "done", stderr: "", code: 0, timedOut: false };
+ },
+ },
+ scopeLabel: "personal:U1",
+ goal: createGoalRecord({ objective: "finish verification", source: "tool" }),
+ emit: async (entry) => {
+ events.push(entry as Emitted);
+ },
+ };
+ const goal = structuredClone(ref.goal);
+ const tools = createAgentTools(ref);
+ const first = call(
+ tools.find((t) => t.name === "execute"),
+ { command: "echo done", purpose: "Test effect" },
+ );
+ await new Promise((resolve) => setImmediate(resolve));
+ const recovery = call(
+ tools.find((t) => t.name === "context"),
+ { action: "compact", mode: "recent" },
+ );
+ await new Promise((resolve) => setImmediate(resolve));
+ assert.equal(
+ events.some((e) => e.payload.tool === "context"),
+ false,
+ );
+ await call(
+ tools.find((t) => t.name === "execute"),
+ { command: "echo duplicate", purpose: "Test barrier" },
+ );
+ assert.equal(effects, 1);
+ gate.resolve();
+ await first;
+ assert.equal(((await recovery) as { terminate: boolean }).terminate, true);
+ assert.deepEqual(ref.runtimeHandoff, { context: "recent" });
+ assert.deepEqual(ref.goal, goal);
+ assert.equal(events.at(-1)!.payload.tool, "context");
+ assert.equal(events.filter((e) => e.type === "tool_result" && e.payload.tool === "execute").length, 1);
+});
+
+for (const cancel of [false, true]) {
+ test(`context recovery cannot latch after ${cancel ? "cancellation" : "persistence failure"}`, async () => {
+ const gate = Promise.withResolvers<void>();
+ const abort = new AbortController();
+ const ref: ToolContextRef = {
+ current: fakeToolContext(),
+ scopeLabel: "personal:U1",
+ abortSignal: abort.signal,
+ runtimeInFlight: new Set([gate.promise]),
+ emit: async (entry) => {
+ if (!cancel && entry.type === "tool_result") throw new Error("write failed");
+ },
+ };
+ const tool = createAgentTools(ref).find((t) => t.name === "context");
+ const recovery = call(tool, { action: "compact", mode: "recent" });
+ if (cancel) abort.abort();
+ gate.resolve();
+ if (cancel) await recovery;
+ else await assert.rejects(recovery, /write failed/);
+ assert.equal(ref.runtimeHandoff, undefined);
+ assert.equal(ref.runtimeMutationPending, false);
+ });
+}
+
+test("context recovery is not available in read-only mode", () => {
+ assert.ok(!createAgentTools({ current: fakeToolContext() }, { readOnly: true }).some((t) => t.name === "context"));
+});
diff --git a/test/context-compaction.test.ts b/test/context-compaction.test.ts
index 9e31beed7..fe56e7373 100644
--- a/test/context-compaction.test.ts
+++ b/test/context-compaction.test.ts
@@ -107,6 +107,7 @@ function buildOrchestrator(
maxContextTokens?: number,
defaultTurnWallClockMs?: number,
swarm?: Awaited<ReturnType<typeof swarmFixture>>,
+ sessionTapeMode?: "serve" | "shadow",
) {
const config = createMemoryConfigStore(ORG);
const acl = createAclStore();
@@ -140,6 +141,7 @@ function buildOrchestrator(
acl,
maxContextTokens,
defaultTurnWallClockMs,
+ sessionTapeMode,
runtime: createRuntimeService(
{ config, harnessId: "pi", baseModelDefault: "claude-sonnet-5" },
{ authorizesCapabilityScope: async () => true },
@@ -1379,3 +1381,117 @@ test("verified swarm workers use category defaults instead of copied parent choi
assert.equal(run.request.model, inherited.modelId, "the verified stored dispatch is not mutated");
}
});
+
+for (const explicit of [false, true]) {
+ test(`${explicit ? "explicit" : "automatic"} recent recovery continues the request once and stays reduced next turn`, async () => {
+ const base = createMockHarness();
+ const seen: Parameters<Harness["turns"]["runTurn"]>[0][] = [];
+ let summaries = 0;
+ let effects = 0;
+ const harness: Harness = {
+ ...base,
+ models: {
+ ...base.models,
+ compactHistory: async () => {
+ summaries++;
+ throw new Error("summary refused");
+ },
+ },
+ turns: {
+ ...base.turns,
+ runTurn: async (input) => {
+ seen.push(input);
+ if (explicit && seen.length === 1) {
+ await input.emit({ type: "user", payload: { text: input.input }, scopeLabel: input.scopeLabel });
+ await input.emit({
+ type: "tool_call",
+ payload: { tool: "files", callId: "effect", action: "write" },
+ scopeLabel: input.scopeLabel,
+ });
+ effects++;
+ await input.emit({
+ type: "tool_result",
+ payload: { tool: "files", callId: "effect", result: "stored" },
+ scopeLabel: input.scopeLabel,
+ });
+ await input.emit({
+ type: "tool_call",
+ payload: { tool: "context", callId: "reduce", action: "compact", mode: "recent" },
+ scopeLabel: input.scopeLabel,
+ });
+ await input.emit({
+ type: "tool_result",
+ payload: { tool: "context", callId: "reduce", result: "requested" },
+ scopeLabel: input.scopeLabel,
+ });
+ return { reply: "not final", runtimeHandoff: { context: "recent" } };
+ }
+ if (seen.length === (explicit ? 2 : 1)) {
+ assert.equal(input.tapeMode, "shadow");
+ assert.ok(estimateHistoryTokens(input.history) <= 600);
+ assert.ok(input.history.some((e) => (e.payload as { mode?: string }).mode === "recent"));
+ assert.ok(
+ input.input.includes("finish the request") ||
+ input.history.some((e) => (e.payload as { text?: string }).text === "finish the request"),
+ );
+ assert.ok(!JSON.stringify(input.history).includes("old excluded sentinel"));
+ if (explicit) assert.ok(input.history.some((e) => (e.payload as { callId?: string }).callId === "effect"));
+ }
+ await input.emit({ type: "assistant", payload: { text: "finished" }, scopeLabel: input.scopeLabel });
+ return { reply: "finished" };
+ },
+ },
+ };
+ const { orch, sessions } = buildOrchestrator(harness, 1000, undefined, undefined, "serve");
+ const sid = await seed(
+ sessions,
+ explicit ? [] : Array.from({ length: 10 }, () => ({ payload: { text: "old excluded sentinel ".repeat(300) } })),
+ );
+ const before = await sessions.getEntries(sid);
+ const result = await orch.handleTurn({ ...turn("finish the request"), surfaceTools: false });
+ assert.equal(result.status, "ok");
+ assert.equal(result.reply, "finished");
+ assert.equal(seen.length, explicit ? 2 : 1);
+ assert.equal(summaries, explicit ? 0 : 1);
+ assert.equal(effects, explicit ? 1 : 0);
+ assert.deepEqual((await sessions.getEntries(sid)).slice(0, before.length), before);
+ assert.equal((await orch.handleTurn({ ...turn("what happened?"), surfaceTools: false })).status, "ok");
+ assert.equal(seen.at(-1)!.tapeMode, "shadow");
+ assert.ok(!JSON.stringify(seen.at(-1)!.history).includes("old excluded sentinel"));
+ });
+}
+
+test("retry recovery supplies an excluded request without rerunning its completed action", async () => {
+ const base = createMockHarness();
+ let mainCalls = 0;
+ const harness: Harness = {
+ ...base,
+ models: {
+ ...base.models,
+ compactHistory: async () => {
+ throw new Error("summary refused");
+ },
+ },
+ turns: {
+ ...base.turns,
+ runTurn: async (input) => {
+ mainCalls++;
+ assert.match(input.input, /Current request.*\nfinish the stored request/);
+ assert.match(input.input, /don't start over or repeat completed steps/);
+ assert.ok(!input.history.some((e) => e.type === "user"));
+ await input.emit({ type: "assistant", payload: { text: "resumed" }, scopeLabel: input.scopeLabel });
+ return { reply: "resumed" };
+ },
+ },
+ };
+ const { orch, sessions } = buildOrchestrator(harness, 500);
+ await seed(sessions, [
+ { payload: { text: "finish the stored request" } },
+ { type: "tool_call", payload: { tool: "files", action: "write", callId: "done" } },
+ { type: "tool_result", payload: { tool: "files", callId: "done", result: "stored ".repeat(1000) } },
+ ]);
+ const result = await orch.handleTurn({ ...turn("finish the stored request"), attempt: 2, surfaceTools: false });
+ assert.equal(result.status, "ok");
+ assert.equal(result.reply, "resumed");
+ assert.equal(mainCalls, 1);
+});
diff --git a/test/context-recovery.test.ts b/test/context-recovery.test.ts
new file mode 100644
index 000000000..ff7eb27f7
--- /dev/null
+++ b/test/context-recovery.test.ts
@@ -0,0 +1,305 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { createCompaction } from "../src/core/orchestrator/compaction.ts";
+import type { OrchestratorDeps } from "../src/core/orchestrator/types.ts";
+import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
+import { estimateHistoryTokens, forModelContext } from "../src/harness/context-compaction.ts";
+
+import {
+ contextSummaryPayload,
+ createContextSummaryPayload,
+ tapeCheckpointPayload,
+} from "../src/sessions/session-store.ts";
+import { createGoalRecord, goalSnapshotPayload, latestGoalRecord } from "../src/harness/goal.ts";
+import { filterHistoryForAudience } from "../src/resolution/context-filter.ts";
+
+async function fixture() {
+ const sessions = createMemorySessionStore();
+ const session = await sessions.getOrCreateByThread("recovery", "dm", "personal:test");
+ const { lease } = await sessions.acquireLease(session.id);
+ assert.ok(lease);
+ for (let i = 0; i < 12; i++)
+ await sessions.append(lease, {
+ type: "user",
+ payload: { text: `old message ${i} `.repeat(100) },
+ scopeLabel: "personal:test",
+ });
+ let resets = 0;
+ const harness = {
+ models: {
+ compactHistory: async (): Promise<string> => {
+ throw new Error("summary unavailable");
+ },
+ },
+ turns: {
+ resetSession: () => {
+ resets++;
+ },
+ },
+ };
+ const compaction = createCompaction({
+ sessions,
+ harness,
+ maxContextTokens: 500,
+ modelGateway: { recordCall() {} },
+ } as unknown as OrchestratorDeps);
+ const input = {
+ session,
+ lease,
+ visibleHistory: forModelContext((await sessions.getContextWindow(session.id)).entries),
+ scopeId: "personal:test",
+ orgScopeId: "org:test",
+ actorId: "test",
+ };
+ return { sessions, harness, compaction, input, resets: () => resets };
+}
+
+test("mandatory summarizer failure recovers instead of wedging the pending turn", async () => {
+ const f = await fixture();
+ try {
+ const history = await f.compaction.compactContextIfNeeded(f.input);
+ assert.ok(history.length < f.input.visibleHistory.length);
+ assert.equal(f.resets(), 1);
+ assert.equal((await f.sessions.getEntries(f.input.session.id)).length, f.input.visibleHistory.length + 1);
+ assert.deepEqual(forModelContext((await f.sessions.getContextWindow(f.input.session.id)).entries), history);
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+});
+
+async function refresh(f: Awaited<ReturnType<typeof fixture>>) {
+ f.input.visibleHistory = forModelContext((await f.sessions.getContextWindow(f.input.session.id)).entries);
+}
+
+for (const status of ["active", "paused", "complete", "blocked"] as const) {
+ test(`recent recovery preserves ${status} goal and a fitting saved summary without a model call`, async () => {
+ const f = await fixture();
+ const goal = { ...createGoalRecord({ objective: "verify result", source: "tool" }), status };
+ try {
+ await f.sessions.append(f.input.lease, {
+ type: "system",
+ payload: createContextSummaryPayload(11, "Saved work."),
+ scopeLabel: "personal:test",
+ });
+ await f.sessions.append(f.input.lease, {
+ type: "system",
+ payload: goalSnapshotPayload(goal),
+ scopeLabel: "personal:test",
+ });
+ await f.sessions.append(f.input.lease, {
+ type: "user",
+ payload: { text: "Continue verification." },
+ scopeLabel: "personal:test",
+ });
+ await refresh(f);
+ let calls = 0;
+ f.harness.models.compactHistory = async () => {
+ calls++;
+ return "unexpected";
+ };
+ const before = await f.sessions.getEntries(f.input.session.id);
+ const history = await f.compaction.compactRecent(f.input);
+ assert.equal(calls, 0);
+ assert.deepEqual(latestGoalRecord(history), goal);
+ assert.equal(history.filter((e) => latestGoalRecord([e])).length, 1);
+ assert.match(contextSummaryPayload(history[0]!)!.text, /without a new summary/);
+ assert.match(contextSummaryPayload(history[0]!)!.text, /Saved work/);
+ assert.ok(estimateHistoryTokens(history) <= 300);
+ const all = await f.sessions.getEntries(f.input.session.id);
+ assert.deepEqual(all.slice(0, before.length), before);
+ assert.deepEqual(forModelContext((await f.sessions.getContextWindow(f.input.session.id)).entries), history);
+ assert.equal((history[0]!.payload as { mode: string }).mode, "recent");
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+ });
+}
+
+for (const failure of ["throw", "empty", "oversized"] as const) {
+ test(`foreground ${failure} summary uses deterministic recovery`, async () => {
+ const f = await fixture();
+ try {
+ f.harness.models.compactHistory = async () => {
+ if (failure === "throw") throw new Error("provider refused");
+ return failure === "empty" ? "" : "x".repeat(20000);
+ };
+ const history = await f.compaction.compactContextIfNeeded(f.input);
+ assert.ok(estimateHistoryTokens(history) <= 300);
+ assert.equal((history[0]!.payload as { mode: string }).mode, "recent");
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+ });
+}
+
+for (const stage of ["before", "during", "abort-error"] as const) {
+ test(`cancellation ${stage} cannot commit a recovery boundary`, async () => {
+ const f = await fixture();
+ const abort = new AbortController();
+ try {
+ if (stage === "before") abort.abort();
+ f.harness.models.compactHistory = async () => {
+ if (stage === "during") abort.abort();
+ throw new DOMException("cancelled", "AbortError");
+ };
+ await assert.rejects(f.compaction.compactContextIfNeeded({ ...f.input, cancel: abort.signal }), {
+ name: "AbortError",
+ });
+ assert.equal(f.resets(), 0);
+ assert.deepEqual(await f.sessions.getEntries(f.input.session.id), f.input.visibleHistory);
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+ });
+}
+
+for (const stage of ["append", "appendTape", "reset"] as const) {
+ test(`${stage} failure is not swallowed or retried as summarization failure`, async () => {
+ const f = await fixture();
+ try {
+ let calls = 0;
+ f.harness.models.compactHistory = async () => {
+ calls++;
+ return "Summary.";
+ };
+ if (stage === "reset")
+ f.harness.turns.resetSession = () => {
+ throw new Error("storage unavailable");
+ };
+ else
+ f.sessions[stage] = async () => {
+ throw new Error("storage unavailable");
+ };
+ await assert.rejects(f.compaction.compactContextIfNeeded(f.input), /storage unavailable/);
+ assert.equal(calls, 1);
+ assert.ok(
+ !(await f.sessions.getEntries(f.input.session.id)).some(
+ (e) => (e.payload as { mode?: string }).mode === "recent",
+ ),
+ );
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+ });
+}
+
+test("oversized entries and orphaned parallel tool results cannot bypass the recent budget", async () => {
+ const f = await fixture();
+ try {
+ for (const [type, payload] of [
+ ["tool_call", { callId: "large", tool: "files", data: "oversized ".repeat(1000) }],
+ ["tool_call", { callId: "small", tool: "files" }],
+ ["tool_result", { callId: "large", result: "ok" }],
+ ["tool_result", { callId: "small", result: "ok" }],
+ ["user", { text: "Continue." }],
+ ] as const)
+ await f.sessions.append(f.input.lease, { type, payload, scopeLabel: "personal:test" });
+ await refresh(f);
+ const history = await f.compaction.compactRecent(f.input);
+ assert.ok(estimateHistoryTokens(history) <= 300);
+ assert.ok(!history.some((e) => e.type === "tool_result"));
+ assert.equal(history.at(-1)!.type, "user");
+ await f.sessions.append(f.input.lease, {
+ type: "user",
+ payload: { text: "oversized ".repeat(1000) },
+ scopeLabel: "personal:test",
+ });
+ await refresh(f);
+ const emptyTail = await f.compaction.compactRecent(f.input);
+ assert.equal(emptyTail.length, 1);
+ assert.deepEqual(forModelContext((await f.sessions.getContextWindow(f.input.session.id)).entries), emptyTail);
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+});
+
+test("recent recovery cannot recover an unauthorized summary or goal", async () => {
+ const f = await fixture();
+ try {
+ await f.sessions.append(f.input.lease, {
+ type: "system",
+ payload: createContextSummaryPayload(11, "private sentinel"),
+ scopeLabel: "personal:other",
+ });
+ await f.sessions.append(f.input.lease, {
+ type: "system",
+ payload: goalSnapshotPayload(createGoalRecord({ objective: "private sentinel", source: "tool" })),
+ scopeLabel: "personal:other",
+ });
+ await refresh(f);
+ f.input.visibleHistory = filterHistoryForAudience(
+ f.input.visibleHistory,
+ [{ id: "test", type: "internal" }],
+ "personal:test",
+ "org:test",
+ );
+ const history = await f.compaction.compactRecent(f.input);
+ assert.equal(latestGoalRecord(history), null);
+ assert.doesNotMatch(JSON.stringify(history), /private sentinel/);
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+});
+
+for (const covered of [false, true]) {
+ test(`recent recovery ${covered ? "advances" : "does not manufacture"} tape coverage`, async () => {
+ const f = await fixture();
+ try {
+ if (covered)
+ await f.sessions.appendTape(f.input.lease, {
+ kind: "annotation",
+ payload: tapeCheckpointPayload("turnEnd"),
+ scopeLabel: "personal:test",
+ entrySeq: 11,
+ });
+ const before = await f.sessions.tapeCoverage(f.input.session.id);
+ const history = await f.compaction.compactRecent(f.input);
+ assert.equal(await f.sessions.tapeCoverage(f.input.session.id), covered ? history[0]!.seq : before);
+ const tape = await f.sessions.getTape(f.input.session.id);
+ assert.equal((tape.find((row) => row.kind === "context_event")!.payload as { mode: string }).mode, "recent");
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+ });
+}
+
+for (const tainted of [false, true]) {
+ test(`a ${tainted ? "tainted" : "foreign"} summary still supplies the durable exclusion floor`, async () => {
+ const f = await fixture();
+ try {
+ await f.sessions.append(f.input.lease, {
+ type: "system",
+ payload: {
+ ...createContextSummaryPayload(11, "hidden summary sentinel"),
+ ...(tainted ? { securityTainted: true } : {}),
+ },
+ scopeLabel: tainted ? "personal:test" : "personal:other",
+ });
+ await f.sessions.append(f.input.lease, {
+ type: "user",
+ payload: { text: "Latest request." },
+ scopeLabel: "personal:test",
+ });
+ await refresh(f);
+ f.input.visibleHistory = filterHistoryForAudience(
+ f.input.visibleHistory,
+ [{ id: "test", type: "internal" }],
+ "personal:test",
+ "org:test",
+ );
+ const history = await f.compaction.compactRecent(f.input);
+ assert.equal(contextSummaryPayload(history[0]!)!.throughSeq, 11);
+ const reloaded = filterHistoryForAudience(
+ forModelContext((await f.sessions.getContextWindow(f.input.session.id)).entries),
+ [{ id: "test", type: "internal" }],
+ "personal:test",
+ "org:test",
+ );
+ assert.deepEqual(reloaded, history);
+ assert.ok(estimateHistoryTokens(reloaded) <= 300);
+ assert.doesNotMatch(JSON.stringify(reloaded), /old message|hidden summary sentinel/);
+ } finally {
+ await f.sessions.releaseLease(f.input.lease);
+ }
+ });
+}