Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Agent: the staged Harness keeps output room in small contexts (8K): a guarded, hash-checked correction of pi-ai 1.1.0's safety margin below 64K (PR #3684 by medking82)
medking82df6928400f6e
diff --git a/apps/desktop/scripts/harness-budget.mjs b/apps/desktop/scripts/harness-budget.mjsnew file mode 100644index 000000000..4f588d756--- /dev/null+++ b/apps/desktop/scripts/harness-budget.mjs@@ -0,0 +1,52 @@+/** Strata's output-budget correction for the staged pi-ai copy. */+import { createHash } from 'node:crypto';+import { lstatSync, readFileSync, realpathSync, writeFileSync } from 'node:fs';+import { join, resolve } from 'node:path';++export const HARNESS_BUDGET_PATCH = Object.freeze({+ id: 'strata-pi-ai-context-safety-v2',+ package: '@earendil-works/pi-ai',+ version: '1.1.0',+ file: 'node_modules/@earendil-works/pi-ai/dist/api/simple-options.js',+ beforeSha256: '9b3cc0ae6daa86fdc050bc0cb0f3f986401e64ccddbde6757183c9f8ad411be2',+});++const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');+const oldClamp = 'const available = model.contextWindow - estimateContextTokens(context).tokens - CONTEXT_SAFETY_TOKENS;';+// Small windows also need room after tool results; a fixed margin must not+// consume all remaining estimated space. Keep the old 64K+ calculation.+const newClamp = `const remaining = model.contextWindow - estimateContextTokens(context).tokens;+ const safety = model.contextWindow >= 65536 ? CONTEXT_SAFETY_TOKENS+ : Math.min(CONTEXT_SAFETY_TOKENS, Math.floor(model.contextWindow / 16), Math.max(0, Math.floor(remaining / 2)));+ const available = remaining - safety;`;++/**+ * Patch only a flattened, disposable staged Harness. Returns the BUNDLE.json+ * provenance entry; the caller records it when writing the bundle manifest.+ * A repeat application or any unknown package/source fails before writing.+ */+export function patchStagedHarnessBudget(stagedRoot) {+ const fail = message => { throw new Error(`Harness output-budget patch: ${message}`); };+ const root = realpathSync(resolve(stagedRoot));+ const packageDir = join(root, 'node_modules', '@earendil-works', 'pi-ai');+ const target = join(root, HARNESS_BUDGET_PATCH.file);+ let manifest, before;+ try {+ manifest = JSON.parse(readFileSync(join(packageDir, 'package.json'), 'utf8'));+ // Flattening must have removed links; never write through a shared source file.+ if (realpathSync(target) !== target || !lstatSync(target).isFile() || lstatSync(target).nlink !== 1)+ fail('expected an unlinked staged module');+ before = readFileSync(target);+ } catch (error) {+ fail(`cannot read a standalone staged pi-ai dependency (${error.message})`);+ }+ if (manifest?.name !== HARNESS_BUDGET_PATCH.package || manifest?.version !== HARNESS_BUDGET_PATCH.version)+ fail('expected @earendil-works/pi-ai 1.1.0; review the patch before staging a different dependency');+ if (sha256(before) !== HARNESS_BUDGET_PATCH.beforeSha256)+ fail('unexpected simple-options.js SHA-256; review the dependency source before staging');+ const source = before.toString('utf8');+ if (source.split(oldClamp).length !== 2) fail('expected exactly one known context clamp');+ const after = Buffer.from(source.replace(oldClamp, newClamp));+ writeFileSync(target, after);+ return { ...HARNESS_BUDGET_PATCH, afterSha256: sha256(after) };+}diff --git a/apps/desktop/scripts/stage-harness.mjs b/apps/desktop/scripts/stage-harness.mjsindex 7327d88ee..0e6738ea4 100644--- a/apps/desktop/scripts/stage-harness.mjs+++ b/apps/desktop/scripts/stage-harness.mjs@@ -19,6 +19,7 @@ import { join, relative, resolve, sep } from 'node:path';import { createHash } from 'node:crypto';import { flattenPnpm } from './flatten-pnpm.mjs';import { isOtherOsBinaryFolder, missingHarnessImports } from './harness-prune.mjs';+import { patchStagedHarnessBudget } from './harness-budget.mjs';const args = process.argv.slice(2);const opt = (name, fallback) => { const i = args.indexOf(`--${name}`); return i >= 0 ? args[i + 1] : fallback; };@@ -144,6 +145,9 @@ function prune(dir) {}prune(out);+// Strata's provenance-recorded output-budget correction applies only to this copy.+const patches = [patchStagedHarnessBudget(out)];+// --- licence: the MIT text and the third-party notices travel with the files ---------------------------------------------for (const name of ['LICENSE', 'THIRD_PARTY_NOTICES.md']) {if (existsSync(join(src, name))) cpSync(join(src, name), join(out, name));@@ -156,6 +160,7 @@ const total = size(out);writeFileSync(join(out, 'BUNDLE.json'), JSON.stringify({component: 'deepseek-harness', version: cli.version, commit, license: 'MIT', platform, node: process.version,bytes: total.bytes, files: total.files, entry: 'apps/cli/lib/bin.js', builtAt: new Date().toISOString(),+ patches,note: 'Pruned production install (pnpm deploy --prod). No package manager runs on the user\'s PC.',}, null, 2));diff --git a/apps/host/server/agent/README.md b/apps/host/server/agent/README.mdindex b05bebcf2..dbc520c5b 100644--- a/apps/host/server/agent/README.md+++ b/apps/host/server/agent/README.md@@ -93,6 +93,13 @@ What the person chose is saved by `tools-settings.mjs` (inside the host's settin## Thinking and the output budget+The Harness staging script (`apps/desktop/scripts/stage-harness.mjs`) applies Strata's guarded pi-ai 1.1.0 patch to its staged copy+(`harness-budget.mjs`, #3684): below 64K the context safety room is at most `min(4096, floor(contextWindow / 16))` and never more+than half the estimated remaining room, so an 8K window keeps output room after a tool result instead of ending with a one-token+cap. The caller's output cap and the one-token floor remain; windows of 64K or more keep the previous calculation. `BUNDLE.json.patches`+records the patch identity, the dependency version and the before/after SHA-256. Unknown dependency bytes stop staging for review.+Source and already-installed Harness copies are not changed.+- A local route is reasoning-capable (`reasoning: true`) with `compat.thinkingFormat: 'chat-template'`: the request carries `chat_template_kwargs {enable_thinking, reasoning_effort}`, which the Strata server reads (a top-level `enable_thinking` is ignored by it, which is why pi-ai's `qwen` format left the model thinking). The chat's Reasoning menu travels as `thinking` in the stream body; the bridge sends it with `session/selectModel` (`reasoningEffort`) whenever the model or the level changed; a level a route does not have is dropped (never an error). API routes keep the provider default (the menu is disabled for them in the UI).- `localOutputBudget(context)` is 30 percent of the window, at least 4096 and at most 32768 (9,830 tokens at 32K, 19,660 at 64K). The Harness's automatic compaction reads it as the room a reply reserves. pi-ai sends the window minus the prompt. App-loaded models get `fit_max_tokens: true` so the engine clamps instead of answering 400. Sampling is not set here: the Harness sends none, so the server's own defaults apply.diff --git a/apps/host/tests/fixtures/pi-ai-1.1.0-simple-options.js.fixture b/apps/host/tests/fixtures/pi-ai-1.1.0-simple-options.js.fixturenew file mode 100644index 000000000..4e36f350e--- /dev/null+++ b/apps/host/tests/fixtures/pi-ai-1.1.0-simple-options.js.fixture@@ -0,0 +1,73 @@+import { clampThinkingLevel } from "../models.js";+import { estimateContextTokens } from "../utils/estimate.js";+const CONTEXT_SAFETY_TOKENS = 4096;+const MIN_MAX_TOKENS = 1;+export function clampMaxTokensToContext(model, context, maxTokens) {+ if (model.contextWindow <= 0)+ return Math.max(MIN_MAX_TOKENS, maxTokens);+ const available = model.contextWindow - estimateContextTokens(context).tokens - CONTEXT_SAFETY_TOKENS;+ return Math.min(maxTokens, Math.max(MIN_MAX_TOKENS, available));+}+export function resolveSamplingParams(model, thinkingLevel, requestParams) {+ const effectiveThinkingLevel = clampThinkingLevel(model, thinkingLevel);+ const thinkingLevelParams = model.samplingParamsByThinkingLevel?.[effectiveThinkingLevel];+ return model.samplingParams || thinkingLevelParams || requestParams+ ? { ...model.samplingParams, ...thinkingLevelParams, ...requestParams }+ : undefined;+}+export function buildBaseOptions(model, context, options, apiKey) {+ const samplingParams = resolveSamplingParams(model, options?.reasoning ?? "off", options?.samplingParams);+ return {+ temperature: options?.temperature,+ samplingParams,+ maxTokens: clampMaxTokensToContext(model, context, options?.maxTokens ?? model.maxTokens),+ signal: options?.signal,+ telemetryContext: options?.telemetryContext,+ apiKey: apiKey || options?.apiKey,+ fetch: options?.fetch,+ transport: options?.transport,+ cacheRetention: options?.cacheRetention,+ sessionId: options?.sessionId,+ headers: options?.headers,+ onPayload: options?.onPayload,+ onResponse: options?.onResponse,+ onProviderStreamEvent: options?.onProviderStreamEvent,+ timeoutMs: options?.timeoutMs,+ websocketConnectTimeoutMs: options?.websocketConnectTimeoutMs,+ maxRetries: options?.maxRetries,+ maxRetryDelayMs: options?.maxRetryDelayMs,+ metadata: options?.metadata,+ env: options?.env,+ };+}+/** Tokens always left for the answer when a thinking budget shares the response ceiling. */+export const MIN_ANSWER_TOKENS = 1024;+export const DEFAULT_THINKING_BUDGETS = {+ minimal: 1024,+ low: 2048,+ medium: 8192,+ high: 16384,+};+export function clampReasoning(effort) {+ return effort === "xhigh" || effort === "max" ? "high" : effort;+}+export function thinkingBudgetForLevel(reasoningLevel, customBudgets) {+ const budgets = { ...DEFAULT_THINKING_BUDGETS, ...customBudgets };+ const level = clampReasoning(reasoningLevel);+ return budgets[level];+}+/** Cap a thinking budget so at least MIN_ANSWER_TOKENS remain under a shared response ceiling. */+export function clampThinkingBudgetToAnswerRoom(thinkingBudget, ceiling) {+ return Math.min(thinkingBudget, Math.max(0, ceiling - MIN_ANSWER_TOKENS));+}+export function adjustMaxTokensForThinking(+// Undefined means no explicit caller cap. Use the model cap and fit thinking inside it.+baseMaxTokens, modelMaxTokens, reasoningLevel, customBudgets) {+ let thinkingBudget = thinkingBudgetForLevel(reasoningLevel, customBudgets);+ const maxTokens = baseMaxTokens === undefined ? modelMaxTokens : Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);+ if (maxTokens <= thinkingBudget) {+ thinkingBudget = clampThinkingBudgetToAnswerRoom(thinkingBudget, maxTokens);+ }+ return { maxTokens, thinkingBudget };+}+//# sourceMappingURL=simple-options.js.map\ No newline at end of filediff --git a/apps/host/tests/fixtures/pi-ai-LICENSE b/apps/host/tests/fixtures/pi-ai-LICENSEnew file mode 100644index 000000000..4c382c586--- /dev/null+++ b/apps/host/tests/fixtures/pi-ai-LICENSE@@ -0,0 +1,25 @@+pi-ai-1.1.0-simple-options.js.fixture is copied from @earendil-works/pi-ai 1.1.0,+dist/api/simple-options.js. Its exact bytes are a staging-patch regression fixture.+Upstream: https://github.com/earendil-works/pi/tree/main/packages/ai++MIT License++Copyright (c) 2025 Mario Zechner++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.diff --git a/apps/host/tests/harness-budget.test.mjs b/apps/host/tests/harness-budget.test.mjsnew file mode 100644index 000000000..5d8a86b57--- /dev/null+++ b/apps/host/tests/harness-budget.test.mjs@@ -0,0 +1,126 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { linkSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { createHash } from 'node:crypto';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { runInNewContext } from 'node:vm';+import { HARNESS_BUDGET_PATCH, patchStagedHarnessBudget } from '../../desktop/scripts/harness-budget.mjs';++// Exact MIT-licensed pi-ai 1.1.0 module; arithmetic fixture, not native tokenization.+const original = readFileSync(new URL('./fixtures/pi-ai-1.1.0-simple-options.js.fixture', import.meta.url), 'utf8').replaceAll('\r\n', '\n');+const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');+function staged(t, { source = original, manifest = { name: '@earendil-works/pi-ai', version: '1.1.0' } } = {}) {+ const root = mkdtempSync(join(tmpdir(), 'strata-harness-budget-'));+ t.after(() => rmSync(root, { recursive: true, force: true }));+ const file = join(root, HARNESS_BUDGET_PATCH.file), packageFile = join(root, 'node_modules', '@earendil-works', 'pi-ai', 'package.json');+ mkdirSync(join(file, '..'), { recursive: true });+ writeFileSync(file, source); writeFileSync(packageFile, JSON.stringify(manifest));+ return { root, file, packageFile };+}+function patched(t) {+ const fixture = staged(t), provenance = patchStagedHarnessBudget(fixture.root);+ return { ...fixture, provenance, clamp: clampOf(readFileSync(fixture.file, 'utf8')) };+}+function clampOf(source) {+ const code = source.replace(/^import .*;\n/gm, '').replace(/^export /gm, '');+ return runInNewContext(`${code}\nclampMaxTokensToContext;`, {+ estimateContextTokens: context => ({ tokens: context.tokens }),+ clampThinkingLevel: (_model, level) => level,+ });+}++test('staged pi-ai leaves output room at 8K Context', t => {+ assert.equal(clampOf(original)({ contextWindow: 8192 }, { tokens: 6545 }, 4096), 1, 'The Original Regression');+ const { clamp } = patched(t);+ assert.equal(clamp({ contextWindow: 8192 }, { tokens: 6545 }, 4096), 1135);+});++test('small-window safety leaves answer room after a tool result', t => {+ const { clamp } = patched(t);+ assert.equal(clamp({ contextWindow: 8192 }, { tokens: 7750 }, 4096), 221);+ assert.equal(clamp({ contextWindow: 8192 }, { tokens: 8000 }, 4096), 96);+ assert.equal(clamp({ contextWindow: 8192 }, { tokens: 8000 }, 32), 32);+});++test('the staged correction retains caller caps and the no-room floor', t => {+ const { clamp } = patched(t);+ for (const cap of [1, 128]) assert.equal(clamp({ contextWindow: 8192 }, { tokens: 6545 }, cap), cap);+ for (const tokens of [8191, 8192, 9000]) assert.equal(clamp({ contextWindow: 8192 }, { tokens }, 4096), 1);+});++test('32K scales safety to 2048; 64K and larger keep the original calculation', t => {+ const { clamp } = patched(t), old = clampOf(original);+ assert.equal(clamp({ contextWindow: 32768 }, { tokens: 6545 }, 32768), 24175);+ assert.equal(clamp({ contextWindow: 65536 }, { tokens: 6545 }, 65536), 54895);+ assert.equal(clamp({ contextWindow: 131072 }, { tokens: 6545 }, 131072), 120431);+ for (const window of [65536, 131072, 262144]) {+ for (const tokens of [0, 6545, window - 4096, window]) {+ for (const cap of [1, 128, 32768, window])+ assert.equal(clamp({ contextWindow: window }, { tokens }, cap), old({ contextWindow: window }, { tokens }, cap));+ }+ }+});++test('unknown and nonpositive context paths retain their previous behavior', t => {+ const { clamp } = patched(t), old = clampOf(original);+ for (const window of [0, -1, null, undefined, NaN]) {+ for (const cap of [0, 1, 128, 4096])+ assert.equal(clamp({ contextWindow: window }, { tokens: 6545 }, cap), old({ contextWindow: window }, { tokens: 6545 }, cap));+ }+});++test('patch provenance records its identity, exact dependency and before/after hashes', t => {+ const { file, packageFile, provenance } = patched(t);+ assert.equal(sha256(original), HARNESS_BUDGET_PATCH.beforeSha256);+ assert.deepEqual(provenance, { id: 'strata-pi-ai-context-safety-v2', package: '@earendil-works/pi-ai', version: '1.1.0',+ file: HARNESS_BUDGET_PATCH.file, beforeSha256: sha256(original), afterSha256: sha256(readFileSync(file)) });+ assert.notEqual(provenance.beforeSha256, provenance.afterSha256);+ assert.deepEqual(JSON.parse(readFileSync(packageFile, 'utf8')), { name: '@earendil-works/pi-ai', version: '1.1.0' });+ const changed = original.replace('const available = model.contextWindow - estimateContextTokens(context).tokens - CONTEXT_SAFETY_TOKENS;',+ `const remaining = model.contextWindow - estimateContextTokens(context).tokens;+ const safety = model.contextWindow >= 65536 ? CONTEXT_SAFETY_TOKENS+ : Math.min(CONTEXT_SAFETY_TOKENS, Math.floor(model.contextWindow / 16), Math.max(0, Math.floor(remaining / 2)));+ const available = remaining - safety;`);+ assert.equal(readFileSync(file, 'utf8'), changed, 'Only The Known Clamp Changes');+});++test('malformed or unexpected dependencies fail before writing', t => {+ const cases = [+ { manifest: { name: '@earendil-works/pi-ai', version: '1.2.0' } },+ { manifest: { name: 'another-package', version: '1.1.0' } },+ { manifest: null },+ { source: original + '\n' },+ { source: original.replace('CONTEXT_SAFETY_TOKENS = 4096', 'CONTEXT_SAFETY_TOKENS = 1024') },+ ];+ for (const value of cases) {+ const f = staged(t, value), before = readFileSync(f.file), manifest = readFileSync(f.packageFile);+ assert.throws(() => patchStagedHarnessBudget(f.root), /Harness output-budget patch:/);+ assert.deepEqual(readFileSync(f.file), before); assert.deepEqual(readFileSync(f.packageFile), manifest);+ }+ const f = staged(t); writeFileSync(f.packageFile, '{bad json');+ assert.throws(() => patchStagedHarnessBudget(f.root), /cannot read a standalone staged pi-ai dependency/);+ assert.equal(readFileSync(f.file, 'utf8'), original); assert.equal(readFileSync(f.packageFile, 'utf8'), '{bad json');+});++test('a repeated patch rejects already-changed bytes without writing again', t => {+ const f = patched(t), before = readFileSync(f.file);+ assert.throws(() => patchStagedHarnessBudget(f.root), /unexpected simple-options.js SHA-256/);+ assert.deepEqual(readFileSync(f.file), before);+});++test('staging refuses a module hardlinked to another copy', t => {+ const f = staged(t), other = join(f.root, 'source-module.js'); linkSync(f.file, other);+ assert.throws(() => patchStagedHarnessBudget(f.root), /expected an unlinked staged module/);+ assert.equal(readFileSync(other, 'utf8'), original); assert.equal(readFileSync(f.file, 'utf8'), original);+});++test('patching a disposable copy leaves the installed original hash unchanged', {+ skip: !process.env.STRATA_TEST_PI_AI_DIR,+}, t => {+ const installed = join(process.env.STRATA_TEST_PI_AI_DIR, 'dist', 'api', 'simple-options.js');+ const before = sha256(readFileSync(installed));+ assert.equal(before, HARNESS_BUDGET_PATCH.beforeSha256);+ const f = staged(t, { source: readFileSync(installed) }); patchStagedHarnessBudget(f.root);+ assert.equal(sha256(readFileSync(installed)), before);+});