diff --git a/apps/host/server/agent/context-budget.mjs b/apps/host/server/agent/context-budget.mjs new file mode 100644 index 00000000..e8b3a7b2 --- /dev/null +++ b/apps/host/server/agent/context-budget.mjs @@ -0,0 +1,162 @@ +/** + * Token arithmetic for the agent's context, with no I/O (the Harness plugin `plugins/strata-context.mjs` and the host both use it). + * + * Why it exists. The Harness prices a conversation with a fixed 4 characters per token. Code, logs and JSON are about twice as dense + * (2.09 characters per token in the 2026-10-22 hands-on test), so its pressure read 31,794 tokens while the engine had been sent 59,048, + * and compaction waited until the window was already past full. The engine reports the real prompt size with every answer; this module + * turns that report into the number compaction decides on, and splits a summarising request that would not fit the window. + */ + +/** The Harness's own price of a text: characters over this, plus a fixed cost per block and per message. */ +export const CHARS_PER_TOKEN = 4; +const BLOCK_OVERHEAD = 4; +const MESSAGE_OVERHEAD = 4; +/** The most a real token can be worth in the Harness's units (one character per token is the worst a tokenizer does). */ +export const MAX_DENSITY = 6; +/** Room kept free in a window for the engine's own framing and the chat template (the engine keeps 64 tokens of slack itself). */ +export const WINDOW_SLACK = 1024; + +/** The Harness's price (4 characters a token) of one message, the same arithmetic as its token meter. */ +export function estimateMessage(message) { + const content = Array.isArray(message?.content) ? message.content : []; + if (message?.role === 'system') { + if (!content.length) return 0; + let chars = 0; + for (const block of content) chars += block?.type === 'text' ? String(block.text ?? '').length : JSON.stringify(block ?? {}).length; + return Math.ceil(chars / CHARS_PER_TOKEN) + MESSAGE_OVERHEAD; + } + let tokens = MESSAGE_OVERHEAD; + for (const block of content) { + if (block?.type === 'text' || block?.type === 'reasoning') tokens += Math.ceil(String(block.text ?? '').length / CHARS_PER_TOKEN) + BLOCK_OVERHEAD; + else if (block?.type === 'tool-call') tokens += Math.ceil(String(block.name ?? '').length / CHARS_PER_TOKEN) + Math.ceil(String(block.arguments ?? '').length / CHARS_PER_TOKEN) + BLOCK_OVERHEAD; + else tokens += BLOCK_OVERHEAD + Math.ceil(JSON.stringify(block ?? {}).length / CHARS_PER_TOKEN); + } + return tokens; +} + +/** The Harness's price of the tool list of a request header. */ +export function estimateTools(header) { + const tools = header?.tools; + if (!Array.isArray(tools) || !tools.length) return 0; + return Math.ceil(JSON.stringify(tools).length / CHARS_PER_TOKEN) + BLOCK_OVERHEAD; +} + +/** + * How many real tokens one of the Harness's tokens is worth, from one measured request: the engine said the prompt was `real` tokens, + * the Harness priced the same prompt at `estimated`. Never below 1 (a conversation of plain prose is priced about right, and a lower + * number would only delay compaction) and never above MAX_DENSITY (a wild sample must not make every step compact). + */ +export function densityOf(real, estimated) { + if (!(real > 0) || !(estimated > 0)) return 1; + return Math.min(MAX_DENSITY, Math.max(1, real / estimated)); +} + +/** + * The prompt size compaction decides on. `sample` is the last measured request: `real` tokens for a prompt the Harness priced at + * `estimated`. `now` is the Harness's price of the conversation now. What was added or removed since the sample is scaled by the sample's + * density; the measured part is exact. Without a sample, or when the measurement is lower than the Harness's own price, the Harness's + * price stands (it is never an underestimate that makes this smaller). + */ +export function pressureNow({ sample = null, now }) { + if (!sample || !(sample.real > 0) || !(sample.estimated > 0)) return Math.max(0, now); + const density = densityOf(sample.real, sample.estimated); + return Math.max(Math.round(now), Math.round(sample.real + density * (now - sample.estimated))); +} + +/** The usage a finished model request reported, as the prompt side only: input plus the cache's reads and writes (output is not in the prompt). */ +export function promptTokensOf(usage) { + if (!usage || typeof usage !== 'object') return 0; + const n = value => (Number.isFinite(Number(value)) ? Math.max(0, Number(value)) : 0); + return n(usage.inputTokens) + n(usage.cacheReadTokens) + n(usage.cacheWriteTokens); +} + +/** The usage of a settled assistant event (a `usage` on the message, or the last usage chunk of its stream). */ +export function usageOfEvent(event) { + const data = event?.data; + if (!data) return null; + if (data.usage && typeof data.usage === 'object') return data.usage; + const stream = Array.isArray(data.stream) ? data.stream : []; + for (let i = stream.length - 1; i >= 0; i--) if (stream[i]?.type === 'chunk' && stream[i].chunk?.type === 'usage') return stream[i].chunk.usage ?? null; + return null; +} + +/** + * A summarising request is: the conversation's system prompt and tools, the messages to condense, then the instruction. This is how many + * real tokens the messages may take in a window so that all of it, the summary and a safety margin fit. + */ +export function summaryBudget({ window, summaryTokens, toolsTokens = 0, systemTokens = 0, instructionTokens = 900, slack = WINDOW_SLACK }) { + return Math.floor(window - summaryTokens - toolsTokens - systemTokens - instructionTokens - slack); +} + +/** Text blocks of a message, longest first: the ones worth shortening when a single message is bigger than the budget. */ +function textBlocks(message) { + return (Array.isArray(message?.content) ? message.content : []).filter(b => (b?.type === 'text' || b?.type === 'reasoning') && typeof b.text === 'string'); +} + +/** + * One message cut down to `maxTokens` (in the Harness's price times `density`): the start and the end of its longest text are kept and + * the middle is replaced by a line saying how much was left out. Only the copy sent to the summariser is cut; the conversation keeps + * the whole message until the summary replaces it. + */ +export function clipMessage(message, maxTokens, density = 1) { + if (estimateMessage(message) * density <= maxTokens) return message; + const blocks = textBlocks(message); + if (!blocks.length) return message; + const others = estimateMessage({ ...message, content: (message.content ?? []).filter(b => !blocks.includes(b)) }) * density; + const room = Math.max(256, maxTokens - others); + const share = room / blocks.reduce((n, b) => n + b.text.length, 0) / density * CHARS_PER_TOKEN; // characters kept per character + const content = (message.content ?? []).map((block) => { + if (!blocks.includes(block)) return block; + const keep = Math.max(200, Math.floor(block.text.length * Math.min(1, share) - 120)); + if (block.text.length <= keep) return block; + const head = Math.ceil(keep * 0.6), tail = keep - head; + const left = block.text.length - head - tail; + return { ...block, text: `${block.text.slice(0, head)}\n\n[… ${left.toLocaleString('en-US')} characters from the middle are left out of this summary request …]\n\n${block.text.slice(block.text.length - tail)}` }; + }); + return { ...message, content }; +} + +/** Whether cutting the list in front of index `i` would separate a tool call from its result. */ +function balancedBefore(messages, i) { + let open = 0; + for (let k = 0; k < i; k++) { + const m = messages[k]; + if (m?.role === 'assistant') open += (Array.isArray(m.content) ? m.content : []).filter(b => b?.type === 'tool-call').length; + else if (m?.role === 'tool') open -= 1; + } + return open <= 0; +} + +/** + * Split messages into runs that each fit `budget` real tokens (price `density`), cutting between a tool call's result and the next + * message when it can. A message bigger than the budget by itself is clipped first. Order is kept. + */ +export function splitMessages(messages, budget, density = 1) { + const room = Math.max(512, budget); + const sized = messages.map(m => clipMessage(m, Math.floor(room * 0.8), density)); + const cost = sized.map(m => estimateMessage(m) * density); + const runs = []; + let start = 0, used = 0; + for (let i = 0; i < sized.length; i++) { + if (i > start && used + cost[i] > room) { + // Cut as late as possible, but never between a tool call and its result when a clean place exists inside the run. + let cut = i; + while (cut > start + 1 && !balancedBefore(sized, cut)) cut--; + if (!balancedBefore(sized, cut)) cut = i; + runs.push(sized.slice(start, cut)); + start = cut; + used = cost.slice(start, i).reduce((n, x) => n + x, 0); + } + used += cost[i]; + } + if (start < sized.length) runs.push(sized.slice(start)); + return runs; +} + +/** Whether a failure is the model server saying the prompt does not fit its window. */ +export function isContextOverflow(error) { + if (!error) return false; + if (error.code === 'CONTEXT_WINDOW_EXCEEDED') return true; + const text = [error.message, error.cause?.message, error.failure?.message, error.failure?.code].filter(Boolean).join(' '); + return /request exceeds the context window|leaves no room to answer|context[_ ]length|context window|too many tokens|maximum context/i.test(text); +} diff --git a/apps/host/server/agent/plugins/strata-context.mjs b/apps/host/server/agent/plugins/strata-context.mjs new file mode 100644 index 00000000..473cd192 --- /dev/null +++ b/apps/host/server/agent/plugins/strata-context.mjs @@ -0,0 +1,146 @@ +/** + * Strata's context plugin for the Harness (plain ESM, loaded by absolute path from the profile, like strata-gate and strata-models). + * + * What it fixes (2026-10-22 hands-on test, Gyro-S at 64K): + * 1. Compaction decided from the Harness's own price of the conversation, 4 characters a token. Code, logs and JSON are about twice as + * dense, so a chat the engine had read as 59,048 tokens was priced at 31,794 and compaction started far too late. Here the token meter + * is calibrated by what the model server reports for each answer (its real prompt size): the measured size is exact, and what was + * added since is priced with the density that measurement showed. Automatic compaction and the manual /compact both measure through + * the token meter, so both read the same number as the context meter in the app. + * 2. The summarising request itself could be bigger than the window ("/compact" sent a 66,507-token prompt to a 65,536 window). The + * summary is now asked for in pieces that fit: a message bigger than a piece is cut for the request (the conversation keeps it until + * the summary replaces it), the pieces are summarised one after the other, each building on the summary before it, and a piece the + * server still refuses is halved and tried again. + * + * How. It wraps `tokenMeter.measure` and `llm.stream` in place, once each (the same way strata-models wraps the subagent tools). Only a + * request the Harness marks `purpose: 'compaction'` is touched in `llm.stream`. Nothing in the Harness is rewritten: a Harness update + * that renames either leaves compaction as it was, and the test that runs the real Harness fails. + */ +import { appendFileSync } from 'node:fs'; +import { densityOf, estimateMessage, estimateTools, isContextOverflow, MAX_DENSITY, pressureNow, promptTokensOf, splitMessages, summaryBudget, usageOfEvent, WINDOW_SLACK } from '../context-budget.mjs'; + +export const name = 'strata-context'; +export const inject = ['tokenMeter', 'llm']; + +/** Real prompt measurements per session: { real, estimated } for the last answer the model server reported. */ +const samples = new WeakMap(); +const bySessionId = new Map(); +const wrapped = new WeakSet(); +/** Densities that made a summary request fail on this server, per session id (the next request starts from a more careful one). */ +const careful = new Map(); + +const debugFile = process.env.STRATA_CONTEXT_DEBUG ?? ''; +const dbg = text => { if (debugFile) { try { appendFileSync(debugFile, `${text}\n`); } catch { /* debug only */ } } }; + +export function apply(ctx, config = {}) { + const meter = ctx.tokenMeter; + const llm = ctx.llm; + const log = (level, text) => { try { ctx.logger?.[level]?.(`strata-context: ${text}`); } catch { /* logging never blocks */ } dbg(`${level} ${text}`); }; + dbg(`apply meter=${meter?.constructor?.name} llm=${llm?.constructor?.name}`); + + // 1. The measured size of the prompt, from the model server's own report for each answer. + if (meter && typeof meter.measure === 'function' && !wrapped.has(meter)) { + wrapped.add(meter); + const original = meter.measure; + meter.measure = function measure(session, requestHeader) { + const m = original.call(this, session, requestHeader); + const sample = samples.get(session); + dbg(`measure total=${m.totalTokens} surface=${m.surfaceTokens} baseline=${m.baseline?.kind}:${m.baseline?.tokens} sample=${JSON.stringify(sample ?? null)}`); + if (!sample) return m; + const now = m.surfaceTokens + estimateTools(requestHeader ?? session.requestHeader?.()); + const total = Math.max(m.totalTokens, pressureNow({ sample, now })); + if (total <= m.totalTokens) return m; + const density = densityOf(sample.real, sample.estimated); + return { ...m, totalTokens: total, nodes: m.nodes.map(n => ({ ...n, tokens: Math.ceil(n.tokens * density) })) }; + }; + ctx.on('session/event', (session, event) => { + if (event.type !== 'assistant/message') return; + const real = promptTokensOf(usageOfEvent(event)); + if (!(real > 0)) return; + try { + // The request that was just answered saw the conversation without this answer: its price is the surface before the answer's node. + const m = original.call(meter, session); + const heuristic = m.nodes.reduce((n, x) => n + (x.seq === event.seq ? 0 : x.heuristicTokens), 0); + const estimated = heuristic + estimateTools(session.requestHeader?.()); + if (!(estimated > 0)) return; + const sample = { real, estimated }; + samples.set(session, sample); + if (session.id) bySessionId.set(String(session.id), sample); + dbg(`sample real=${real} estimated=${estimated}`); + } catch (e) { log('warn', `could not record a measurement: ${e.message}`); } + }); + } + + // 2. A summary request that fits the window. + if (llm && typeof llm.stream === 'function' && !wrapped.has(llm)) { + wrapped.add(llm); + const original = llm.stream; + llm.stream = function stream(options) { + if (options?.purpose !== 'compaction') return original.call(this, options); + return summaryStream.call(this, options, original); + }; + } + + /** A finished stream: its chunks, the text it wrote and why it ended. */ + async function drain(iterable) { + let text = '', finish = null; + const chunks = []; + for await (const chunk of iterable) { + chunks.push(chunk); + if (chunk.type === 'text-delta') text += chunk.text ?? ''; + else if (chunk.type === 'finish') finish = chunk.reason; + } + return { text, finish, chunks }; + } + const refusedForSize = finish => finish?.kind === 'error' && isContextOverflow(finish.failure ?? finish); + + async function* summaryStream(options, original) { + let window = 0; + try { window = (await llm.resolveModelInfo(options.provider, options.model, options.signal))?.context?.contextWindow ?? 0; } catch { window = 0; } + const messages = options.messages ?? []; + const key = String(options.sessionId ?? ''); + const summaryTokens = options.maxTokens ?? 4096; + if (!(window > 0) || messages.length < 2) { yield* original.call(this, options); return; } + const own = bySessionId.get(key); + let density = Math.max(careful.get(key) ?? 1, own ? densityOf(own.real, own.estimated) : 1); + const price = list => list.reduce((n, m) => n + estimateMessage(m), 0) * density; + const limit = window - summaryTokens - WINDOW_SLACK; + // The whole request fits by this price: ask as the Harness would (the model server's cache of the conversation is reused). + if (price(messages) + estimateTools({ tools: options.tools }) * density <= limit) { + const first = await drain(original.call(this, options)); + if (!refusedForSize(first.finish)) { for (const chunk of first.chunks) yield chunk; return; } + density = Math.min(MAX_DENSITY, density * 1.6); + careful.set(key, density); + log('warn', 'the summary request did not fit the window; asking in pieces'); + } + // In pieces. The instruction is the last message; the system prompt, when there is one, the first. + const instruction = messages[messages.length - 1]; + const system = messages[0]?.role === 'system' ? messages[0] : null; + const body = messages.slice(system ? 1 : 0, -1); + let tools = options.tools; + if (tools && estimateTools({ tools }) * density > window / 3) tools = undefined; // a long tool list only describes tools: the summary does not need it + const fixed = (system ? estimateMessage(system) : 0) * density + estimateTools({ tools }) * density + estimateMessage(instruction) * density; + let budget = summaryBudget({ window, summaryTokens: summaryTokens * 2, systemTokens: fixed, instructionTokens: 0 }); + for (let attempt = 0; ; attempt++) { + const runs = splitMessages(body, Math.max(1024, budget), density); + log('info', `summarising ${body.length} messages in ${runs.length} piece(s) for a ${window}-token window (density ${density.toFixed(2)}, about ${Math.round(budget)} tokens a piece)`); + let failed = null, summary = ''; + for (let i = 0; i < runs.length && !failed; i++) { + options.signal?.throwIfAborted?.(); + const carried = summary ? [{ role: 'user', content: [{ type: 'text', text: `\n${summary}\n` }] }] : []; + const { tools: _drop, ...rest } = options; + const piece = { ...rest, ...(tools ? { tools } : {}), messages: [...(system ? [system] : []), ...carried, ...runs[i], instruction] }; + const { text, finish, chunks } = await drain(original.call(this, piece)); + if (refusedForSize(finish)) { failed = finish; break; } + // The last piece's answer is the summary the Harness reads; a piece that ended in another error ends the request the same way. + if (i === runs.length - 1 || finish?.kind === 'error' || finish?.kind === 'aborted') { for (const chunk of chunks) yield chunk; return; } + summary = text; + } + if (!failed || attempt >= 3) throw Object.assign(new Error(failed?.failure?.message ?? 'the summary request does not fit the window'), { code: 'CONTEXT_WINDOW_EXCEEDED' }); + density = Math.min(MAX_DENSITY, density * 1.6); + careful.set(key, density); + budget = Math.max(1024, budget / 2); + log('warn', `a piece did not fit; asking again with pieces of about ${Math.round(budget)} tokens`); + } + } +} diff --git a/apps/host/server/agent/profile.mjs b/apps/host/server/agent/profile.mjs index 2637b87f..9b3dfabe 100644 --- a/apps/host/server/agent/profile.mjs +++ b/apps/host/server/agent/profile.mjs @@ -55,6 +55,8 @@ export const modeOf = value => (Object.hasOwn(MODE_PRESETS, value) ? value : DEF /** The approval gate plugin (plain ESM, loaded by absolute path). */ export const GATE_PLUGIN = fileURLToPath(new URL('./plugins/strata-gate.mjs', import.meta.url)); /** The app's web search (DuckDuckGo, or the person's own Exa / Perplexity key), registered with the Harness's web seam. */ +/** Compaction that measures the real prompt and summarises in pieces that fit the window (plugins/strata-context.mjs). */ +export const CONTEXT_PLUGIN = fileURLToPath(new URL('./plugins/strata-context.mjs', import.meta.url)); export const MODELS_PLUGIN = fileURLToPath(new URL('./plugins/strata-models.mjs', import.meta.url)); export const SEARCH_PLUGIN = fileURLToPath(new URL('./plugins/strata-search.mjs', import.meta.url)); /** The app's own tool server for the tools a person writes (instructions, commands). */ @@ -286,6 +288,8 @@ export function buildProfile({ local = [], connections = [], gate = true, tools // Manual asks before a change runs, so the person sees one Approve/Deny card and nothing has happened yet. // Its web question names the search service the person chose (a keyed one only when its key is saved), and the person's switch skips the question. ...(gate ? [{ insert: [{ id: 'strata-gate', name: GATE_PLUGIN, config: { web: { provider: SEARCH_NAMES[keyed ?? 'builtin'], skipAsk: tools.webWithoutAsking === true } } }] }] : []), + // Compaction decides from what the model server measured, and its summary requests always fit the window (plugins/strata-context.mjs). + { insert: [{ id: 'strata-context', name: CONTEXT_PLUGIN, config: {} }] }, // Helper agents on a model the person names: every model the app can reach is allowed, and the plugin makes the choice forgiving (plugins/strata-models.mjs). ...(subagentRoutes.length ? [{ id: 'subagent-model-selection-settings', config: { enabled: true, allowedModels: subagentRoutes.map(r => ({ provider: r.provider, model: r.model })) } }] : []), ...(subagentRoutes.length ? [{ insert: [{ id: 'strata-models', name: MODELS_PLUGIN, config: { routes: subagentRoutes } }] }] : []), diff --git a/apps/host/tests/agent-context.test.mjs b/apps/host/tests/agent-context.test.mjs new file mode 100644 index 00000000..07afa6f0 --- /dev/null +++ b/apps/host/tests/agent-context.test.mjs @@ -0,0 +1,143 @@ +// Context pressure and compaction (2026-10-22 hands-on test): compaction reads the size the model server measured, a summary request +// always fits the window, and a refused prompt is recovered from. The Harness is real; the model server is the scripted worker, set to +// count a prompt the way the engine does (characters over a density) and to refuse one that leaves no room. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, mkdirSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { startScriptedWorker } from './helpers/scripted-worker.mjs'; +import { createAgentBridge } from '../server/agent/agent-bridge.mjs'; +import { resolveHarness } from '../server/agent/harness-runtime.mjs'; +import { buildProfile } from '../server/agent/profile.mjs'; +import { clipMessage, densityOf, estimateMessage, isContextOverflow, pressureNow, promptTokensOf, splitMessages, summaryBudget, usageOfEvent } from '../server/agent/context-budget.mjs'; + +const HARNESS = process.env.STRATA_HARNESS_DIR; +const skip = !HARNESS || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false; + +test('the profile loads the context plugin, so compaction and /compact read the measured prompt', () => { + const profile = buildProfile({ local: [{ id: 'a', modelId: 'm', baseUrl: 'http://x', context: 65536 }], connections: [] }); + assert.match(profile.patch, /"id": "strata-context"/); + assert.match(profile.patch, /strata-context\.mjs/); +}); + +test('density: the engine said 59,048 tokens for a prompt the Harness priced at 31,794', () => { + const d = densityOf(59048, 31794); + assert.ok(d > 1.85 && d < 1.87, `density ${d}`); + assert.equal(densityOf(0, 100), 1); + assert.equal(densityOf(100, 0), 1); + assert.equal(densityOf(10, 100), 1, 'plain prose is never priced lower than the Harness already does'); + assert.equal(densityOf(10000, 100), 6, 'a wild sample is capped'); +}); + +test('pressure: the measured part is exact, what came after is priced at the measured density, and it never reads lower than the Harness', () => { + const sample = { real: 59048, estimated: 31794 }; + assert.equal(pressureNow({ sample, now: 31794 }), 59048, 'nothing added since the sample: the measured number'); + const grown = pressureNow({ sample, now: 31794 + 1000 }); + assert.ok(grown > 59048 + 1800 && grown < 59048 + 1900, `1000 added tokens are worth about 1,860 real ones (${grown})`); + assert.ok(pressureNow({ sample, now: 31794 - 5000 }) < 59048, 'a compaction lowers it'); + assert.equal(pressureNow({ sample: null, now: 1234 }), 1234, 'no measurement yet: the Harness price'); + assert.equal(pressureNow({ sample: { real: 100, estimated: 5000 }, now: 5000 }), 5000, 'never below the Harness price'); +}); + +test('the compaction threshold on a 64K window leaves room for the reply even when the prompt is measured in real tokens', () => { + // Gyro-S at 64K: the reply keeps 19,660 tokens and compaction keeps 5,734 of headroom (profile.mjs), so it starts at 40,142 REAL tokens. + const window = 65536, reply = 19660, headroom = 5734; + const threshold = Math.floor(Math.min(window * 0.8, window - reply - headroom)); + assert.equal(threshold, 40142); + assert.ok(threshold + reply < window, 'the prompt at the threshold plus a full reply still fits'); + // The 2026-10-22 chat: 31,794 priced = 59,048 real. It is over the threshold now (before: under it, and the engine refused the next request). + assert.ok(pressureNow({ sample: { real: 59048, estimated: 31794 }, now: 31794 }) > threshold); +}); + +test('usage helpers: prompt side only, from a message or from its stream', () => { + assert.equal(promptTokensOf({ inputTokens: 100, outputTokens: 50, cacheReadTokens: 30, cacheWriteTokens: 5 }), 135); + assert.equal(promptTokensOf(null), 0); + assert.deepEqual(usageOfEvent({ data: { usage: { inputTokens: 7 } } }), { inputTokens: 7 }); + assert.deepEqual(usageOfEvent({ data: { stream: [{ type: 'chunk', chunk: { type: 'text-delta' } }, { type: 'chunk', chunk: { type: 'usage', usage: { inputTokens: 9 } } }] } }), { inputTokens: 9 }); + assert.equal(usageOfEvent({ data: {} }), null); +}); + +test('a refusal for the context window is recognised by its code and by the engine\'s own words', () => { + assert.ok(isContextOverflow({ code: 'CONTEXT_WINDOW_EXCEEDED', message: 'x' })); + assert.ok(isContextOverflow(new Error('400: {"type":"invalid_request_error","message":"request exceeds the context window: prompt (70141 tokens) leaves no room to answer in the context (65536); requests are never truncated"}'))); + assert.ok(!isContextOverflow(new Error('fetch failed'))); + assert.ok(!isContextOverflow(null)); +}); + +test('a summary request is split into pieces that fit, each message whole when it can be, a huge one cut in the middle', () => { + const text = n => ({ role: 'user', content: [{ type: 'text', text: 'x'.repeat(n) }] }); + const many = Array.from({ length: 12 }, () => text(4000)); // 1,000 tokens each by the Harness price + const runs = splitMessages(many, 3500, 1); + assert.deepEqual(runs.map(r => r.length), [3, 3, 3, 3]); + assert.deepEqual(runs.flat().length, 12, 'nothing is dropped'); + const dense = splitMessages(many, 3500, 2); + assert.ok(dense.length > runs.length, 'a denser text means more pieces'); + const huge = clipMessage(text(400000), 8000, 1); + assert.ok(estimateMessage(huge) <= 8000, `cut to its share (${estimateMessage(huge)})`); + assert.match(huge.content[0].text, /left out of this summary request/); + assert.equal(clipMessage(text(100), 8000, 1).content[0].text.length, 100, 'a small message is untouched'); + const oneBig = splitMessages([text(10), text(600000), text(10)], 6000, 1); + assert.ok(oneBig.every(r => r.reduce((n, m) => n + estimateMessage(m), 0) <= 6000), 'every piece fits after the cut'); + // A tool call and its result stay together when a clean cut exists. + const call = { role: 'assistant', content: [{ type: 'tool-call', id: 'c', name: 'read', arguments: '{}' }] }; + const result = { role: 'tool', toolCallId: 'c', content: [{ type: 'text', text: 'y'.repeat(2000) }] }; + const paired = splitMessages([text(2000), call, result, text(2000)], 1200, 1); + assert.ok(paired.every(r => !r.includes(call) || r.includes(result)), 'the call and its result are in one piece'); + assert.ok(summaryBudget({ window: 65536, summaryTokens: 5734, toolsTokens: 10133, systemTokens: 400 }) > 40000); +}); + +/** The scripted model server counting like the engine (3 characters a token here: logs and code) in a 32K window. */ +async function rig(t, { window = 32768, charsPerToken = 3 } = {}) { + const worker = await startScriptedWorker({ window, charsPerToken }); + const dir = mkdtempSync(join(tmpdir(), 'strata-ctx-')); + const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true }); + const bridge = createAgentBridge({ dataDir: dir, localInstances: () => [{ id: 'i', label: 'S', baseUrl: worker.url, modelId: 'scripted-model', context: window, slots: 2 }], connections: () => [], env: process.env, startTimeoutMs: 90_000 }); + t.after(async () => { await bridge.close(); await worker.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } }); + const send = async (message) => { const frames = []; await bridge.runTurn({ chatId: 'c', message, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) }); return frames; }; + const harnessLog = () => { try { return readFileSync(join(dir, 'agent', 'harness.log'), 'utf8'); } catch { return ''; } }; + return { worker, bridge, send, ws, harnessLog }; +} +const answerOf = frames => frames.filter(f => f.type === 'delta').map(f => f.text).join(''); +const dense = 'q=0x1f ok;'; // 10 characters: 2.5 tokens by the Harness price, 3.3 by the model server's count here + +test('the plugin loads in the real Harness (a plugin that fails to import would leave compaction as it was, silently)', { skip }, async (t) => { + const { send, harnessLog } = await rig(t); + const frames = await send('SCRIPT:echo hello'); + assert.equal(frames.at(-1).type, 'done'); + assert.doesNotMatch(harnessLog(), /did not activate|failed to import|strata-context/, harnessLog()); +}); + +test('dense text compacts in time: no request is refused, and every message is answered', { skip }, async (t) => { + const { worker, send } = await rig(t); + const replies = []; + for (let i = 0; i < 8; i++) { + const frames = await send(`SCRIPT:echo round${i} ${dense.repeat(900)}`); + assert.equal(frames.at(-1).type, 'done', `round ${i} ended normally: ${JSON.stringify(frames.at(-1)).slice(0, 300)}`); + replies.push(answerOf(frames).slice(0, 12)); + } + assert.ok(replies.every(r => /^round[0-9]/.test(r)), replies.join(' | ')); + const refused = worker.requests.filter(r => r.refused); + assert.equal(refused.length, 0, `the model server refused ${refused.length} request(s): ${refused.map(r => r.refused).join(', ')} tokens`); + assert.ok(worker.requests.some(r => r.summary), 'the older history was summarised'); + // Compaction starts before the next message would push the prompt past the threshold, so no answer was asked for from a nearly full window. + const peak = Math.max(...worker.requests.filter(r => !r.summary).map(r => Math.round(r.promptChars / 3))); + assert.ok(peak < 32768 - 4000, `no prompt came within 4,000 tokens of the window (peak ${peak})`); +}); + +test('a chat the model server refused once is not stuck: the next message compacts (the summary itself fits the window) and goes through', { skip }, async (t) => { + // 2026-10-22: a 103 KB message made a 70K-token prompt, the 400 left it in the history, and then neither the next message nor /compact + // could go through, because the summary request carried the same history and was refused for its size too. + const { worker, send } = await rig(t); + assert.equal((await send(`SCRIPT:echo one ${dense.repeat(300)}`)).at(-1).type, 'done'); + const huge = await send(`SCRIPT:echo two ${dense.repeat(7000)}`); + assert.equal(huge.at(-1).type, 'done', 'the Harness compacted after the refusal and asked again: ' + JSON.stringify(huge.at(-1)).slice(0, 200)); + const small = await send('SCRIPT:echo three please'); + assert.equal(small.at(-1).type, 'done', JSON.stringify(small.at(-1)).slice(0, 300)); + assert.match(answerOf(small), /three/); + const again = await send('SCRIPT:echo four please'); + assert.equal(again.at(-1).type, 'done'); + const refused = worker.requests.filter(r => r.refused); + assert.equal(refused.length, 1, `only the first, too large, prompt was refused: ${refused.map(r => r.refused).join(', ')}`); + assert.ok(worker.requests.filter(r => r.summary).every(r => Math.round(r.promptChars / 3) < 32768 - 64), 'every summary request fitted the window'); +}); diff --git a/apps/host/tests/helpers/scripted-worker.mjs b/apps/host/tests/helpers/scripted-worker.mjs index 7676ffc5..34c07490 100644 --- a/apps/host/tests/helpers/scripted-worker.mjs +++ b/apps/host/tests/helpers/scripted-worker.mjs @@ -37,7 +37,9 @@ import { createHash } from 'node:crypto'; const delay = ms => new Promise(r => setTimeout(r, ms)); -export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model = 'scripted-model', serial = false } = {}) { +export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model = 'scripted-model', serial = false, window = 0, charsPerToken = 0 } = {}) { + // `window` + `charsPerToken` make it behave like the engine: every request's prompt is counted (its size over charsPerToken), the usage it + // reports is that count, and a prompt that leaves no room to answer is refused with the engine's own 400 ("requests are never truncated"). // `serial` stands in for an engine with one request slot: the next request waits until the one before it has finished. let tail = Promise.resolve(); const turn = () => { let release; const mine = new Promise(r => { release = r; }); const wait = tail; tail = tail.then(() => mine); return wait.then(() => release); }; @@ -72,6 +74,11 @@ export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model = authSha: req.headers.authorization ? createHash('sha256').update(req.headers.authorization).digest('hex') : null, promptChars: raw.length, raw, }); + const counted = charsPerToken > 0 ? Math.ceil(raw.length / charsPerToken) : 0; + if (window > 0 && counted > 0 && counted > window - 64) { + requests.at(-1).refused = counted; + return json(400, { error: { type: 'invalid_request_error', message: `request exceeds the context window: prompt (${counted} tokens) leaves no room to answer in the context (${window}); requests are never truncated` } }); + } const shellName = () => (body.tools ?? []).find(t => ['bash', 'pwsh'].includes(t.function?.name))?.function?.name ?? 'bash'; const shellCommand = (file, value) => (shellName() === 'pwsh' ? `Add-Content -Path '${file}' -Value '${value}'` : `echo ${value} >> ${file}`); let closed = false; @@ -80,7 +87,7 @@ export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model = res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' }); const id = `chatcmpl-${requests.length}`; const send = (delta, finish = null, extra = {}) => res.write(`data: ${JSON.stringify({ id, object: 'chat.completion.chunk', created: 0, model: body.model, choices: [{ index: 0, delta, finish_reason: finish }], ...extra })}\n\n`); - let promptTokens = 100 + messages.length; + let promptTokens = counted || 100 + messages.length; const finish = (reason, tokens = 10) => { send({}, reason); res.write(`data: ${JSON.stringify({ id, object: 'chat.completion.chunk', created: 0, model: body.model, choices: [], usage: { prompt_tokens: promptTokens, completion_tokens: tokens, total_tokens: promptTokens + tokens } })}\n\n`);