Multiplayer agent harness for work.
Keep the system prompt stable across turns so prompt caching survives
Josh Franceb813bc6d7718
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex fb3949f81..785a7d704 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -1052,11 +1052,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {const sharedFilesBlock = sharedFilesSystemSection(resolution.grantedHandles);if (sharedFilesBlock) systemPrompt += `\n\n${sharedFilesBlock}`;- const stableSystemBytes = systemPrompt.length;- if (turnTimezone) {- const timeBlock = currentTimeBlock(turnTimezone, Date.now());- if (timeBlock) systemPrompt += `\n\n${timeBlock}`;- }+ const timeBlock = turnTimezone ? currentTimeBlock(turnTimezone, Date.now()) : "";let memoryContext = "a channel";if (conversation.kind === "dm") memoryContext = "a direct message";else if (conversation.channelName) memoryContext = `#${conversation.channelName}`;@@ -1861,8 +1857,12 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {const connectionsUrl = deps.publicWebUrl ? `${deps.publicWebUrl.replace(/\/$/, "")}/keychain` : undefined;systemPrompt += `\n\n${renderConnectedAppsBlock(status, configuredProviders, connectionsUrl)}`;}+ const stableSystemBytes = systemPrompt.length;+ if (timeBlock) systemPrompt += `\n\n${timeBlock}`;systemPrompt += memoryBlock;if (onboardingBlock) systemPrompt += `\n\n${onboardingBlock}`;+ const volatileContext = systemPrompt.slice(stableSystemBytes).trim();+ systemPrompt = systemPrompt.slice(0, stableSystemBytes);if (ambientTurn &&@@ -2500,7 +2500,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {const sender = !automatedTurn && input.text.trim() ? senderNote(actor.displayName) : "";const unscreenedNote = inputUnscreened || inbound.unscreened.length ? unscreenedNotice("inbound content") : "";const turnEnv = environmentNote(- [manifest, principalDelivered, sender, unscreenedNote, input.conversationHeader?.trim()]+ [manifest, principalDelivered, sender, unscreenedNote, input.conversationHeader?.trim(), volatileContext].filter((s) => s && s.trim()).join("\n\n"),);@@ -2810,7 +2810,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {: {}),...(securityPolicy.toolApprovals === "all" ? { toolApprovalGate: authorizeToolCall } : {}),systemPrompt,- systemCacheBoundary: stableSystemBytes,history: continuation?.history ?? history,tools,...(tools.credentialExecServices ? { credentialExecServices: tools.credentialExecServices } : {}),diff --git a/src/harness/context-compaction.ts b/src/harness/context-compaction.tsindex d46947dd6..43c1d7baa 100644--- a/src/harness/context-compaction.ts+++ b/src/harness/context-compaction.ts@@ -78,8 +78,11 @@ const entryTokenCache = new Map<string, number>();const ENTRY_TOKEN_CACHE_MAX = 50_000;export function estimateEntryTokens(entry: SessionEntry): number {- const payload = entry.payload as { text?: string } | null;- const text = typeof payload?.text === "string" ? payload.text : JSON.stringify(entry.payload ?? {});+ const payload = entry.payload as { text?: string; environment?: string } | null;+ const text =+ typeof payload?.text === "string"+ ? [payload.text, payload.environment].filter((s) => typeof s === "string" && s).join("\n\n")+ : JSON.stringify(entry.payload ?? {});const key = `${entry.sessionId}:${entry.seq}:${text.length}`;const hit = entryTokenCache.get(key);if (hit !== undefined) return hit;diff --git a/src/harness/harness.ts b/src/harness/harness.tsindex 532447cf7..496149475 100644--- a/src/harness/harness.ts+++ b/src/harness/harness.ts@@ -88,7 +88,6 @@ export interface HarnessTurnInput {pollFire?: boolean;turnWallClockMs?: number;systemPrompt: string;- systemCacheBoundary?: number;history: SessionEntry[];tools: ToolContext;credentialExecServices?: readonly { service: string; binary: string }[];diff --git a/src/harness/mock-harness.ts b/src/harness/mock-harness.tsindex 36d87a0f1..8f08d7d1f 100644--- a/src/harness/mock-harness.ts+++ b/src/harness/mock-harness.ts@@ -325,7 +325,7 @@ export function createMockHarness(): Harness {} else if (command0 === "!histcount") {reply = `history:${turn.history.length}`;} else if (command0 === "!sysprompt") {- reply = turn.systemPrompt;+ reply = [turn.systemPrompt, turn.environment].filter((s) => s && s.trim()).join("\n\n");} else if (command0 === "!wallclock") {reply = `wallclock:${turn.turnWallClockMs ?? 0}`;} else if (command0 === "!surfacename") {diff --git a/src/harness/pi-harness.ts b/src/harness/pi-harness.tsindex 600f604a0..c8e5c8d27 100644--- a/src/harness/pi-harness.ts+++ b/src/harness/pi-harness.ts@@ -1115,6 +1115,7 @@ function customModelsPath(): string | null {async function buildModelRuntime(keys: ProviderKeys | string,modelGateway?: ModelGatewayTransportConfig,+ cacheRetention?: "long",): Promise<ModelRuntime> {const k: ProviderKeys = typeof keys === "string" ? { anthropic: keys } : keys;// Custom providers must exist in the runtime's own registry — a runtime@@ -1128,6 +1129,8 @@ async function buildModelRuntime(for (const [provider, apiKey] of Object.entries(k)) {if (apiKey) await runtime.setRuntimeApiKey(provider, apiKey, { allowNetwork: false });}+ const retained = <T extends object | undefined>(options: T): T =>+ cacheRetention ? ({ ...options, cacheRetention } as T) : options;const stream = runtime.stream.bind(runtime);runtime.stream = (<TApi extends Api>(model: Model<TApi>,@@ -1135,9 +1138,9 @@ async function buildModelRuntime(options?: ModelsApiStreamOptions<TApi>,) => {const request = modelGatewayRequest(modelGateway, model);- if (!request) return stream(model, context, options);+ if (!request) return stream(model, context, retained(options));const routedOptions = {- ...options,+ ...retained(options),apiKey: request.apiKey,transformHeaders: async (headers: ProviderHeaders) => ({...(options?.transformHeaders ? await options.transformHeaders(headers) : headers),@@ -1157,9 +1160,9 @@ async function buildModelRuntime(const streamSimple = runtime.streamSimple.bind(runtime);runtime.streamSimple = ((model: Model<Api>, context: Context, options?: ModelsSimpleStreamOptions) => {const request = modelGatewayRequest(modelGateway, model);- if (!request) return streamSimple(model, context, options);+ if (!request) return streamSimple(model, context, retained(options));const routedOptions = {- ...options,+ ...retained(options),apiKey: request.apiKey,transformHeaders: async (headers: ProviderHeaders) => ({...(options?.transformHeaders ? await options.transformHeaders(headers) : headers),@@ -1300,32 +1303,6 @@ export function modelHasFastMode(model: unknown): boolean {return Boolean(m?.fastMode) || Boolean(m?.headers?.["anthropic-beta"]?.includes(FAST_MODE_BETA));}-const ONE_HOUR_CACHE_CONTROL = { type: "ephemeral", ttl: "1h" } as const;--export function applySystemPromptCacheSplit(payload: unknown, boundary: number | undefined): void {- if (typeof boundary !== "number" || !Number.isFinite(boundary) || boundary <= 0) return;- if (!payload || typeof payload !== "object") return;- const p = payload as { system?: unknown; tools?: unknown };- if (!Array.isArray(p.system) || p.system.length !== 1) return;- const block = p.system[0] as { type?: unknown; text?: unknown } | undefined;- if (!block || block.type !== "text" || typeof block.text !== "string") return;- const text = block.text;- if (boundary >= text.length) return;- const stable = text.slice(0, boundary);- const rest = text.slice(boundary);- if (!stable.trim() || !rest.trim()) return;- p.system = [- { type: "text", text: stable, cache_control: { ...ONE_HOUR_CACHE_CONTROL } },- { type: "text", text: rest },- ];- if (Array.isArray(p.tools)) {- for (const t of p.tools) {- const tool = t as { cache_control?: unknown } | undefined;- if (tool && tool.cache_control) tool.cache_control = { ...ONE_HOUR_CACHE_CONTROL };- }- }-}-export const OUTPUT_BUDGET_FLOOR_TOKENS = 1_024;export const OUTPUT_GUARD_SAFETY_TOKENS = 4_096;const OUTPUT_GUARD_CHARS_PER_TOKEN = 4;@@ -1474,7 +1451,6 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {systemPrompt: string,history: SessionEntry[],priorTurns?: ConversationTurn[],- systemCacheBoundary?: number,readOnly?: boolean,surfaceTools?: boolean,surfaceName?: string,@@ -1488,12 +1464,6 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {turnProviderKeys?: ProviderKeys,): Promise<{ entry: TurnSession; compileMs: number }> {const compileStart = Date.now();- const cacheBoundary =- systemCacheSplit &&- typeof systemCacheBoundary === "number" &&- systemPrompt.slice(0, systemCacheBoundary).isWellFormed()- ? systemCacheBoundary- : undefined;let reconstructed: PiReplayMessage[] | null;try {reconstructed = reconstructMessagesFromHistory(history);@@ -1527,6 +1497,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {const modelRuntime = await buildModelRuntime(turnProviderKeys ?? (await resolveProviderKeys()),turnProviderKeys ? undefined : modelGateway,+ systemCacheSplit ? "long" : undefined,);const ref: ToolContextRef = { current: null };const { resourceLoader, cwd, agentDir } = await createIsolatedResources(tempDirPrefix, composedPrompt);@@ -1608,13 +1579,6 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {ref.pendingPrepareNextTurn = undefined;ref.pendingTransformContext = undefined;applyFastSpeed(payload, ref.fast, (model as { api?: string } | undefined)?.api);- if (cacheBoundary !== undefined) {- try {- applySystemPromptCacheSplit(payload, cacheBoundary);- } catch (e) {- swallow("pi: system prompt cache split", e);- }- }const guarded = guardOutputBudget(payload, model);if (guarded.kind === "raised") {console.error(@@ -1709,7 +1673,6 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {turn.systemPrompt,turn.history,turn.priorTurns,- turn.systemCacheBoundary,turn.readOnly,turn.surfaceTools,turn.surfaceName,@@ -1759,6 +1722,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {type: "user",payload: {text: turn.input,+ ...(turn.environment ? { environment: turn.environment } : {}),...((turn.triggerTs ?? turn.entryTs) ? { ts: turn.triggerTs ?? turn.entryTs } : {}),...(turn.attachments?.length ? { attachments: turn.attachments } : {}),},diff --git a/src/harness/replay.ts b/src/harness/replay.tsindex 8bfa10a7a..b6364eb8f 100644--- a/src/harness/replay.ts+++ b/src/harness/replay.ts@@ -162,7 +162,10 @@ export function reconstructMessagesFromHistory(history: readonly SessionEntry[])if (ov) {if (ov.text.trim() || ov.files?.length) raw.push(userMsg(renderOverheard(ov), e.createdAt));} else {- const t = entryText(e);+ const environment = (e.payload as { environment?: unknown } | null)?.environment;+ const t = [entryText(e), typeof environment === "string" ? environment.trim() : ""]+ .filter(Boolean)+ .join("\n\n");if (t) raw.push(userMsg(t, e.createdAt));}} else if (e.type === "assistant") {diff --git a/src/model/pi-models.ts b/src/model/pi-models.tsindex 76ec31d1c..23b10dc64 100644--- a/src/model/pi-models.ts+++ b/src/model/pi-models.ts@@ -429,6 +429,7 @@ export function auxiliaryModelFor(baseModelId: string): string {}const CONTEXT_BUDGET_FRACTION = 0.5;+const CONTEXT_BUDGET_CAP_TOKENS = 150_000;export function contextTokenBudgetForModel(id: string): number | undefined {const model = resolveModel(id);@@ -436,7 +437,7 @@ export function contextTokenBudgetForModel(id: string): number | undefined {const output = model?.maxTokens;if (typeof window !== "number" || window <= 0 || typeof output !== "number" || output <= 0 || output >= window)return undefined;- return Math.floor((window - output) * CONTEXT_BUDGET_FRACTION);+ return Math.min(CONTEXT_BUDGET_CAP_TOKENS, Math.floor((window - output) * CONTEXT_BUDGET_FRACTION));}export function modelSupportedByHarness(id: string | undefined, harness: string): boolean {diff --git a/test/context-compaction.test.ts b/test/context-compaction.test.tsindex 62ddac52b..8e324a232 100644--- a/test/context-compaction.test.ts+++ b/test/context-compaction.test.ts@@ -1078,3 +1078,19 @@ test("a summary that landed while the pass was summarizing makes it drop its ownassert.equal(contextSummaryPayload(summaries[0]!)?.text, "a rival fold");assert.equal(resetCalls.length, 0, "the dropped pass never reached its write");});++test("the token estimate counts the environment note persisted on a user entry", () => {+ const environment = `<environment>\n${"## What you remember\nlikes terse replies. ".repeat(40)}\n</environment>`;+ const bare = {+ sessionId: "s",+ seq: 1,+ parentSeq: null,+ type: "user",+ payload: { text: "hi" },+ scopeLabel: "org:o",+ createdAt: 1,+ } as SessionEntry;+ const withEnv = { ...bare, seq: 2, payload: { text: "hi", environment } } as SessionEntry;+ const delta = estimateHistoryTokens([withEnv]) - estimateHistoryTokens([bare]);+ assert.ok(delta >= countTokens(environment) * 0.9, `environment tokens must be counted (delta ${delta})`);+});diff --git a/test/model-overlay.test.ts b/test/model-overlay.test.tsindex f649c66e3..ec33c7457 100644--- a/test/model-overlay.test.ts+++ b/test/model-overlay.test.ts@@ -124,7 +124,7 @@ test("store reload and independent readers observe edits and soft deletion withoassert.equal(resolveModel(MODEL_ID), undefined);await reader.refresh();const inFlight = getRequiredModel(MODEL_ID);- assert.equal(contextTokenBudgetForModel(MODEL_ID), 190_000);+ assert.equal(contextTokenBudgetForModel(MODEL_ID), 150_000);assert.deepEqual(inFlight.cost, spec.cost);assert.ok(defaultWebuiModelIds().includes(MODEL_ID));assert.equal(validateWebTurnModelOptions({ model: MODEL_ID }, null), null);@@ -137,7 +137,7 @@ test("store reload and independent readers observe edits and soft deletion witho"editor",);await reader.refresh();- assert.equal(contextTokenBudgetForModel(MODEL_ID), 290_000);+ assert.equal(getRequiredModel(MODEL_ID).contextWindow, 600_000);assert.equal(getRequiredModel(MODEL_ID).cost.input, 1);assert.equal(getRequiredModel(MODEL_ID).cost.tiers, undefined);assert.equal(inFlight.contextWindow, 400_000);diff --git a/test/pi-harness-cache-split.test.ts b/test/pi-harness-cache-split.test.tsdeleted file mode 100644index a99277b7d..000000000--- a/test/pi-harness-cache-split.test.ts+++ /dev/null@@ -1,121 +0,0 @@-import { test } from "node:test";-import assert from "node:assert/strict";-import { applySystemPromptCacheSplit } from "../src/harness/pi-harness.ts";--function makePayload(systemText: string): {- system: Array<{ type: string; text: string; cache_control?: unknown }>;- tools: Array<{ name: string; cache_control?: unknown }>;- messages: Array<{ role: string; content: Array<{ type: string; text: string; cache_control?: unknown }> }>;-} {- return {- system: [{ type: "text", text: systemText, cache_control: { type: "ephemeral" } }],- tools: [{ name: "execute" }, { name: "read" }, { name: "write", cache_control: { type: "ephemeral" } }],- messages: [{ role: "user", content: [{ type: "text", text: "hi", cache_control: { type: "ephemeral" } }] }],- };-}--const SOUL = "You are Agent.\n\n## Your computer\nstuff\n\n## Scheduling\nmore";-const TAIL = "\n\n## Your logins\n- AWS ✓\n\n## What you remember\nLikes terse replies";--function countBreakpoints(p: ReturnType<typeof makePayload>): number {- let n = 0;- for (const b of p.system) if (b.cache_control) n++;- for (const t of p.tools) if (t.cache_control) n++;- for (const m of p.messages) for (const b of m.content) if (b.cache_control) n++;- return n;-}--test("splits at the boundary, relocates the breakpoint to the stable block (1h), tail uncached", () => {- const p = makePayload(SOUL + TAIL);- applySystemPromptCacheSplit(p, SOUL.length);-- assert.equal(p.system.length, 2, "system is now two blocks");- assert.equal(p.system[0]!.text, SOUL, "block 0 = the stable SOUL prefix");- assert.equal(p.system[1]!.text, TAIL, "block 1 = the volatile tail");- assert.deepEqual(- p.system[0]!.cache_control,- { type: "ephemeral", ttl: "1h" },- "stable block carries the 1h breakpoint",- );- assert.equal(- p.system[1]!.cache_control,- undefined,- "tail block carries NO breakpoint (rides the message breakpoint)",- );-});--test("byte-exact: the concatenated system text is unchanged (only split into two blocks)", () => {- const full = SOUL + TAIL;- const p = makePayload(full);- applySystemPromptCacheSplit(p, SOUL.length);- assert.equal(p.system.map((b) => b.text).join(""), full, "no bytes added or dropped");-});--test("bumps the tool breakpoint to 1h (ordering: 1h entries must precede the 5m message breakpoint)", () => {- const p = makePayload(SOUL + TAIL);- applySystemPromptCacheSplit(p, SOUL.length);- const cachedTool = p.tools.find((t) => t.cache_control);- assert.deepEqual(cachedTool!.cache_control, { type: "ephemeral", ttl: "1h" }, "the cached tool is upgraded to 1h");- assert.deepEqual(- p.messages[0]!.content[0]!.cache_control,- { type: "ephemeral" },- "the last-message breakpoint stays 5m (per-turn churn gains nothing from 1h)",- );-});--test("stays at 3 cache breakpoints — never exceeds Anthropic's max of 4", () => {- const p = makePayload(SOUL + TAIL);- assert.equal(countBreakpoints(p), 3, "baseline: system + last-tool + last-message");- applySystemPromptCacheSplit(p, SOUL.length);- assert.equal(countBreakpoints(p), 3, "after split: stable-system + last-tool + last-message");-});--test("no-op when boundary is missing / zero / negative / non-finite", () => {- for (const bad of [undefined, 0, -5, Number.NaN, Number.POSITIVE_INFINITY]) {- const p = makePayload(SOUL + TAIL);- applySystemPromptCacheSplit(p, bad as number | undefined);- assert.equal(p.system.length, 1, `boundary=${String(bad)} leaves the single block untouched`);- assert.deepEqual(p.system[0]!.cache_control, { type: "ephemeral" }, "original 5m breakpoint preserved");- }-});--test("no-op when boundary is at or past the end of the system text", () => {- const full = SOUL + TAIL;- for (const b of [full.length, full.length + 100]) {- const p = makePayload(full);- applySystemPromptCacheSplit(p, b);- assert.equal(p.system.length, 1, "nothing volatile to peel off → untouched");- }-});--test("no-op when a split would produce an empty stable or empty tail segment", () => {- const p1 = makePayload(" " + TAIL.trimStart());- applySystemPromptCacheSplit(p1, 2);- assert.equal(p1.system.length, 1, "blank stable → no split");-});--test("no-op on the OAuth 2-block system shape (leaves both blocks + their breakpoints)", () => {- const p = {- system: [- { type: "text", text: "You are Claude Code...", cache_control: { type: "ephemeral" } },- { type: "text", text: SOUL + TAIL, cache_control: { type: "ephemeral" } },- ],- tools: [{ name: "execute", cache_control: { type: "ephemeral" } }],- };- applySystemPromptCacheSplit(p, SOUL.length);- assert.equal(p.system.length, 2, "2-block shape untouched");- assert.deepEqual(p.tools[0]!.cache_control, { type: "ephemeral" }, "tool TTL not bumped when system wasn't split");-});--test("no-op when system is a string, missing, or not a text block", () => {- const asString = { system: SOUL + TAIL } as { system: unknown };- applySystemPromptCacheSplit(asString, SOUL.length);- assert.equal(typeof asString.system, "string", "string system left as-is");-- const notText = { system: [{ type: "image", text: 123 }] } as { system: unknown };- applySystemPromptCacheSplit(notText, 5);- assert.deepEqual(notText.system, [{ type: "image", text: 123 }], "non-text block left as-is");-- applySystemPromptCacheSplit({}, 5);- applySystemPromptCacheSplit(null, 5);-});diff --git a/test/pi-models.test.ts b/test/pi-models.test.tsindex 412db41fb..7b9a82dce 100644--- a/test/pi-models.test.ts+++ b/test/pi-models.test.ts@@ -179,16 +179,15 @@ test("context token budget is half of each model's real input room", () => {cacheWrite: 12.5,tiers: undefined,});- assert.equal(contextTokenBudgetForModel("claude-fable-5-1"), Math.floor((1_000_000 - 128_000) * 0.5));+ assert.equal(contextTokenBudgetForModel("claude-fable-5-1"), 150_000, "a 1M window is capped, not halved");assert.equal(getRequiredModel("claude-fable-5").contextWindow, 1_000_000);- assert.equal(contextTokenBudgetForModel("claude-fable-5"), Math.floor((1_000_000 - 128_000) * 0.5));- const sol = contextTokenBudgetForModel("gpt-5.6-sol");- assert.equal(sol, Math.floor((1_050_000 - 128_000) * 0.5));- assert.ok(sol !== undefined && sol < 1_050_000 * 0.5, "budget stays below half the window");+ assert.equal(contextTokenBudgetForModel("claude-fable-5"), 150_000);+ assert.equal(contextTokenBudgetForModel("gpt-5.6-sol"), 150_000);assert.equal(contextTokenBudgetForModel("claude-not-a-real-model"), undefined);for (const m of SELECTABLE_BASE_MODELS) {const budget = contextTokenBudgetForModel(m.id);assert.ok(budget !== undefined && budget >= 60_000, `${m.id} budget ${budget} suspiciously small`);+ assert.ok(budget <= 150_000, `${m.id} budget ${budget} exceeds the cap`);}});diff --git a/test/prompt-modes.test.ts b/test/prompt-modes.test.tsindex 951c338df..b345f80f8 100644--- a/test/prompt-modes.test.ts+++ b/test/prompt-modes.test.ts@@ -373,8 +373,9 @@ test("a display name containing template tokens cannot break prompt rendering",text: "",origin: { kind: "direct" },});- assert.match(prompt, /1:1 with Alice/);- assert.doesNotMatch(prompt, /\{\{/);+ const systemPrompt = prompt.split("\n\n<environment>")[0]!;+ assert.match(systemPrompt, /1:1 with Alice/);+ assert.doesNotMatch(systemPrompt, /\{\{/);});test("template tokens in a stored branding value are stripped, never rendered or thrown", async () => {@@ -403,17 +404,14 @@ test("Mode 2 (spine channel): static prose stays within the word-count ceiling (const orch = buildOrchestrator({ orgSoul: ORG_SOUL });const prompt = await sysprompt(orch, spineChannelTurn("", { timezone: "America/New_York" }));- const VOLATILE_BOUNDARY = "\n\n## The user's local time";- const boundaryAt = prompt.indexOf(VOLATILE_BOUNDARY);- assert.notEqual(- boundaryAt,- -1,- "expected the cached-prefix/volatile-tail boundary ('## The user's local time') to still exist — " +- "if compose renamed or moved this heading, update VOLATILE_BOUNDARY here to match",+ const systemPrompt = prompt.split("\n\n<environment>")[0]!;+ assert.match(+ prompt,+ /## The user's local time/,+ "the volatile tail rides the environment note, not the system prompt",);- const cachedPrefix = prompt.slice(0, boundaryAt);- const staticProse = cachedPrefix.split(ORG_SOUL).join("");+ const staticProse = systemPrompt.split(ORG_SOUL).join("");const wordCount = staticProse.trim().split(/\s+/).filter(Boolean).length;diff --git a/test/replay.test.ts b/test/replay.test.tsindex f01fe0079..b3ce40b63 100644--- a/test/replay.test.ts+++ b/test/replay.test.ts@@ -505,3 +505,14 @@ test("a legacy call with a numeric files count replays without it (schema wantsassert.ok(!("files" in args), "numeric files count must not replay");assert.ok(!("bytes" in args));});++test("reconstructMessagesFromHistory replays the environment note the user message was sent with", () => {+ const history = [+ ent("user", { text: "what's on today?", environment: "<environment>\nIt is Monday 9am\n</environment>" }, 1),+ ent("assistant", { text: "Nothing yet." }, 2),+ ];+ const [user] = reconstructMessagesFromHistory(history);+ assert.deepEqual(user!.content, [+ { type: "text", text: "what's on today?\n\n<environment>\nIt is Monday 9am\n</environment>" },+ ]);+});diff --git a/test/system-prompt-order.test.ts b/test/system-prompt-order.test.tsindex 4cedd9b4b..abacb3348 100644--- a/test/system-prompt-order.test.ts+++ b/test/system-prompt-order.test.ts@@ -191,16 +191,10 @@ test("system prompt is ordered cached-prefix → volatile tail, with memory LASTassert.match(prompt, /\$AGENT_API_URL/);});-test("the cached prefix is byte-identical across two turns of one conversation (only the volatile tail changes)", async () => {+test("the system prompt is byte-identical across two turns a minute apart; the clock rides the environment note", async (t) => {+ t.mock.timers.enable({ apis: ["Date"], now: Date.now() });const { orchestrator: orch } = buildOrchestrator();- const VOLATILE_BOUNDARY = "\n\n## The user's local time";- const prefixOf = (prompt: string): string => {- const at = prompt.indexOf(VOLATILE_BOUNDARY);- assert.notEqual(at, -1, "the volatile tail must open with ## The user's local time");- return prompt.slice(0, at);- };-const turn = (): OrchestratorInput =>dm("dm:U1:cache-stable", "!sysprompt", {timezone: "America/New_York",@@ -209,25 +203,38 @@ test("the cached prefix is byte-identical across two turns of one conversation ({ target: "C2", label: "#random" },],});+ const systemOf = (reply: string): string => reply.split("\n\n<environment>")[0]!;+ const environmentOf = (reply: string): string => reply.slice(systemOf(reply).length);const first = await orch.handleTurn(turn());+ t.mock.timers.tick(61_000);const second = await orch.handleTurn(turn());assert.equal(first.status, "ok");assert.equal(second.status, "ok");- const prefixA = prefixOf(first.reply ?? "");- const prefixB = prefixOf(second.reply ?? "");-- const hasHeading = (text: string, title: string): boolean => text.includes(`\n## ${title}\n`);-- for (const title of ["This machine", "Skills", "Where you are", "Where scheduled tasks post"]) {- assert.ok(hasHeading(prefixA, title), `expected "## ${title}" inside the cached prefix`);+ for (const title of [+ "This machine",+ "Skills",+ "Where you are",+ "Where scheduled tasks post",+ "Your logins",+ "Connected apps",+ ]) {+ assert.ok(systemOf(first.reply ?? "").includes(`\n## ${title}\n`), `expected "## ${title}" in the system prompt`);}- for (const title of ["Your logins", "Connected apps", "What you remember"]) {- assert.ok(!hasHeading(prefixA, title), `"## ${title}" must stay in the volatile tail, not the cached prefix`);+ for (const title of ["The user's local time", "What you remember"]) {+ assert.ok(+ !systemOf(first.reply ?? "").includes(`\n## ${title}\n`),+ `"## ${title}" must not be in the system prompt`,+ );}-- assert.equal(prefixB, prefixA, "the cached prefix must be byte-identical across two turns of one conversation");+ assert.match(environmentOf(first.reply ?? ""), /## The user's local time/);+ assert.notEqual(environmentOf(second.reply ?? ""), environmentOf(first.reply ?? ""), "the clock moved a minute");+ assert.equal(+ systemOf(second.reply ?? ""),+ systemOf(first.reply ?? ""),+ "the system prompt must be byte-identical across turns or every cached message block behind it is invalidated",+ );});test("the cached prefix survives skill-store reordering + a recordUse-style metadata update", async () => {@@ -239,11 +246,7 @@ test("the cached prefix survives skill-store reordering + a recordUse-style metaconst churningSkills = { visibleFor: async () => [...visible] } as unknown as SkillStore;const { orchestrator: orch } = buildOrchestrator({ skills: churningSkills });- const prefixOf = (prompt: string): string => {- const at = prompt.indexOf("\n\n## The user's local time");- assert.notEqual(at, -1, "the volatile tail must open with ## The user's local time");- return prompt.slice(0, at);- };+ const prefixOf = (prompt: string): string => prompt.split("\n\n<environment>")[0]!;const turn = () => dm("dm:U1:skill-order", "!sysprompt", { timezone: "America/New_York" });const first = await orch.handleTurn(turn());