Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
feat(computer): size tool output limits at 15% of the context window
LeoXingb56805f8cc33
diff --git a/apps/computer/local-daemon/internal/provider/native/claude_policy.go b/apps/computer/local-daemon/internal/provider/native/claude_policy.goindex 22761eed..9bb38979 100644--- a/apps/computer/local-daemon/internal/provider/native/claude_policy.go+++ b/apps/computer/local-daemon/internal/provider/native/claude_policy.go@@ -145,7 +145,8 @@ func claudeSettings(contextWindow int) string {settingsEnv["CLAUDE_CODE_DISABLE_1M_CONTEXT"] = "1"}}- if limit := toolOutputTokenLimit(contextWindow); limit < toolOutputTokens {+ if contextWindow > 0 {+ limit := toolOutputTokenLimit(contextWindow)// One tool result keeps the share of the window Codex gives it.// Claude measures Bash output in characters, and one character can be// a whole token, so the token limit bounds characters too. Longerdiff --git a/apps/computer/local-daemon/internal/provider/native/claude_runtime_test.go b/apps/computer/local-daemon/internal/provider/native/claude_runtime_test.goindex f6a02a81..6a904ae6 100644--- a/apps/computer/local-daemon/internal/provider/native/claude_runtime_test.go+++ b/apps/computer/local-daemon/internal/provider/native/claude_runtime_test.go@@ -331,17 +331,17 @@ func TestClaudeSettingsFollowContextWindow(t *testing.T) {return settings, env}small, env := settingsFor(64896)- if small["autoCompactWindow"] != float64(200000) || small["bashOutputMaxChars"] != float64(4056) ||+ if small["autoCompactWindow"] != float64(200000) || small["bashOutputMaxChars"] != float64(9734) ||env["CLAUDE_CODE_MAX_CONTEXT_TOKENS"] != "64896" || env["CLAUDE_CODE_DISABLE_1M_CONTEXT"] != "1" ||- env["CLAUDE_CODE_FILE_READ_MAX_OUTPUT_TOKENS"] != "4056" || env["MAX_MCP_OUTPUT_TOKENS"] != "4056" ||+ env["CLAUDE_CODE_FILE_READ_MAX_OUTPUT_TOKENS"] != "9734" || env["MAX_MCP_OUTPUT_TOKENS"] != "9734" ||env["CLAUDE_CODE_DISABLE_BACKGROUND_TASKS"] != "1" {t.Fatalf("64k settings = %v", small)}- // A large window keeps Claude's own tool output limits; only a 1M one- // keeps the [1m] variants.+ // A large window caps tool output at Claude's own Read and MCP limit;+ // only a 1M one keeps the [1m] variants.large, env := settingsFor(400000)- if _, ok := large["bashOutputMaxChars"]; ok || env["CLAUDE_CODE_MAX_CONTEXT_TOKENS"] != "400000" ||- env["CLAUDE_CODE_DISABLE_1M_CONTEXT"] != "1" || env["MAX_MCP_OUTPUT_TOKENS"] != nil {+ if large["bashOutputMaxChars"] != float64(25000) || env["CLAUDE_CODE_MAX_CONTEXT_TOKENS"] != "400000" ||+ env["CLAUDE_CODE_DISABLE_1M_CONTEXT"] != "1" || env["MAX_MCP_OUTPUT_TOKENS"] != "25000" {t.Fatalf("400k settings = %v", large)}if _, env := settingsFor(1000000); env["CLAUDE_CODE_DISABLE_1M_CONTEXT"] != nil {diff --git a/apps/computer/local-daemon/internal/provider/native/codex_binding.go b/apps/computer/local-daemon/internal/provider/native/codex_binding.goindex 166397ea..7c6edfc7 100644--- a/apps/computer/local-daemon/internal/provider/native/codex_binding.go+++ b/apps/computer/local-daemon/internal/provider/native/codex_binding.go@@ -216,7 +216,7 @@ func (op *codexOperation) openThread(ctx context.Context) error {if spec.Effort != "" {config["model_reasoning_effort"] = spec.Effort}- if limit := toolOutputTokenLimit(spec.ContextWindow); limit < toolOutputTokens && stableCodexAtLeast(op.proc.version, codexToolOutputLimitVersion) {+ if limit := toolOutputTokenLimit(spec.ContextWindow); limit != toolOutputTokens && stableCodexAtLeast(op.proc.version, codexToolOutputLimitVersion) {config["tool_output_token_limit"] = limit}if hook := spec.CodexPermissionHook; hook != nil {diff --git a/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.go b/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.goindex 659a2180..2f34165f 100644--- a/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.go+++ b/apps/computer/local-daemon/internal/provider/native/codex_runtime_test.go@@ -951,8 +951,8 @@ func TestCodexThreadToolOutputLimitFollowsContextWindow(t *testing.T) {window intwant any}{- {"small window", "0.165.0", 64896, float64(4056)},- {"large window", "0.165.0", 272000, nil},+ {"small window", "0.165.0", 64896, float64(9734)},+ {"large window", "0.165.0", 272000, float64(25000)},{"unknown window", "0.165.0", 0, nil},{"Codex without the setting", "0.100.0", 64896, nil},}diff --git a/apps/computer/local-daemon/internal/provider/native/codex_units_test.go b/apps/computer/local-daemon/internal/provider/native/codex_units_test.goindex 626b78c3..ff86bda8 100644--- a/apps/computer/local-daemon/internal/provider/native/codex_units_test.go+++ b/apps/computer/local-daemon/internal/provider/native/codex_units_test.go@@ -477,7 +477,7 @@ func TestCodexUnsentRequestLeavesNothingPending(t *testing.T) {}func TestCodexToolOutputTokenLimitFollowsContextWindow(t *testing.T) {- for window, want := range map[int]int{0: 12000, -1: 12000, 32768: 4000, 64896: 4056, 131072: 8192, 192000: 12000, 1048576: 12000} {+ for window, want := range map[int]int{0: 12000, -1: 12000, 16384: 4000, 32768: 4915, 64896: 9734, 131072: 19660, 166667: 25000, 1048576: 25000} {if got := toolOutputTokenLimit(window); got != want {t.Fatalf("window %d: limit %d, want %d", window, got, want)}@@ -488,8 +488,8 @@ func TestCodexToolOutputTokenLimitFollowsContextWindow(t *testing.T) {if large := codexBindingFingerprint("/home", "", spec); large == small {t.Fatal("a different tool output limit must rebind the thread")}- spec.ContextWindow = 0- if unknown := codexBindingFingerprint("/home", "", spec); unknown != codexBindingFingerprint("/home", "", RunSpec{Dir: "/work", Model: "local", ContextWindow: 400000}) {- t.Fatal("windows that keep the global limit must keep the thread")+ spec.ContextWindow = 400000+ if capped := codexBindingFingerprint("/home", "", spec); capped != codexBindingFingerprint("/home", "", RunSpec{Dir: "/work", Model: "local", ContextWindow: 1048576}) {+ t.Fatal("windows that keep the same limit must keep the thread")}}diff --git a/apps/computer/local-daemon/internal/provider/native/tool_output.go b/apps/computer/local-daemon/internal/provider/native/tool_output.goindex c4c1cd62..ef4c3b1f 100644--- a/apps/computer/local-daemon/internal/provider/native/tool_output.go+++ b/apps/computer/local-daemon/internal/provider/native/tool_output.go@@ -2,22 +2,28 @@ package nativeconst (// toolOutputTokens is the most one tool result may keep when the model's- // window is unknown or large: Codex's global limit, in its bytes/4 token- // estimate, before Codex cuts out the middle.+ // window is unknown: Codex's global limit, in its bytes/4 token estimate,+ // before Codex cuts out the middle.toolOutputTokens = 12000+ // toolOutputWindowPercent is the share of a known window one tool result+ // may take; the rest holds the instructions, the history and the other+ // tool results of the turn.+ toolOutputWindowPercent = 15// minToolOutputTokens keeps a small window's limit large enough to read a// page of a file or a command's error.minToolOutputTokens = 4000+ // maxToolOutputTokens keeps a large window's results as small as Claude+ // keeps its own Read and MCP results.+ maxToolOutputTokens = 25000)// toolOutputTokenLimit sizes the tool output limit by the model's context-// window: a sixteenth of it, so one tool result never fills a small window,-// and at most toolOutputTokens. Codex estimates four bytes per token, which-// dense or binary output can exceed threefold. A zero window is unknown and-// keeps toolOutputTokens.+// window: 15% of it, between minToolOutputTokens and maxToolOutputTokens.+// Codex estimates four bytes per token, which dense or binary output can+// exceed threefold. A zero window is unknown and keeps toolOutputTokens.func toolOutputTokenLimit(contextWindow int) int {if contextWindow <= 0 {return toolOutputTokens}- return min(max(contextWindow/16, minToolOutputTokens), toolOutputTokens)+ return min(max(contextWindow*toolOutputWindowPercent/100, minToolOutputTokens), maxToolOutputTokens)}diff --git a/apps/node-services/src/memory/README.md b/apps/node-services/src/memory/README.mdindex f07103d8..f9ff701c 100644--- a/apps/node-services/src/memory/README.md+++ b/apps/node-services/src/memory/README.md@@ -271,11 +271,11 @@ The top 6 sources carry limited text: a long Chunk gives only a window of aboutmatch, keeping the time and speaker heading of its message; Raw candidates also keep a bounded directory of thematching and adjacent Chunks for a follow-up read of missing facts or truncated text.Memory keeps every search and read result within an output budget of JSON bytes; the CLI passes results-through unchanged. The budget is 15% of the context window of the model the owner's calls run on, at four-bytes per token, and at most 100,000 bytes; the window comes from the owner's Computer through the model-call layer. An unknown window, or an offline Computer, keeps 12,000 bytes. A Runtime cuts the middle out-of a tool result above its own output limit, which the Daemon sets to a sixteenth of the window, between-4,000 and 12,000 tokens, so a budget above that limit reaches the Agent cut.+through unchanged. A Runtime cuts the middle out of a tool result above its output limit, which the Daemon+sets to 15% of the model's context window, between 4,000 and 25,000 tokens of four bytes each. The budget+is 90% of that limit for the model the owner's calls run on, leaving room for the CLI's labels, so a+bounded result reaches the Agent whole; the window comes from the owner's Computer through the model call+layer. An unknown window, or an offline Computer, keeps 12,000 bytes.Session facts take at most a quarter of the budget, the best-ranked first. If the result is still toolarge, inline chunks go from the lowest rank up until only the top candidate's best chunk is left, thencandidates go from the lowest rank up, then that chunk; the top candidate always stays, and every keptdiff --git a/apps/node-services/src/memory/knowledge-query.ts b/apps/node-services/src/memory/knowledge-query.tsindex 03a05861..63067498 100644--- a/apps/node-services/src/memory/knowledge-query.ts+++ b/apps/node-services/src/memory/knowledge-query.ts@@ -33,16 +33,16 @@ const INLINE_CONTENT_SOURCES = 6;const INLINE_CONTENT_CHARS = 800;const INLINE_CONTENT_BUDGET_CHARS = 6_000;const INLINE_CONTENT_SEPARATOR = "\n\n…\n\n";-// One search or read result may take this share of the model's context-// window, measured at four bytes per token as Codex estimates tool output.-// The rest of the window holds the instructions, the history and other tool-// results of the same turn.+// The Daemon limits one tool result to 15% of the model's context window and+// at most 25,000 tokens, which Codex counts at four bytes each; a Runtime cuts+// the middle out of a result above it. A search or read result stays within+// 90% of that limit, since the CLI's labels add about 2% to the JSON.const WINDOW_OUTPUT_SHARE = 0.15;+const MAX_TOOL_OUTPUT_TOKENS = 25_000;const BYTES_PER_TOKEN = 4;-// A large window still keeps one result this small.-const MAX_KNOWLEDGE_OUTPUT_BYTES = 100_000;-// The budget when the model's window is unknown: Codex's smallest tool output-// limit for a small window, 4,000 tokens of four bytes, less the CLI's labels.+const CLI_LABEL_ROOM = 0.9;+// The budget when the model's window is unknown, within the Daemon's smallest+// tool output limit, 4,000 tokens of four bytes, less the CLI's labels.const DEFAULT_KNOWLEDGE_OUTPUT_BYTES = 12_000;// A section that does not fit still returns a page this large, so every read// makes progress.@@ -73,10 +73,11 @@ export function knowledgeOutputBudget(contextWindow: number | undefined,): number {if (contextWindow === undefined) return DEFAULT_KNOWLEDGE_OUTPUT_BYTES;- return Math.min(- Math.floor(contextWindow * WINDOW_OUTPUT_SHARE * BYTES_PER_TOKEN),- MAX_KNOWLEDGE_OUTPUT_BYTES,+ const toolOutputTokens = Math.min(+ contextWindow * WINDOW_OUTPUT_SHARE,+ MAX_TOOL_OUTPUT_TOKENS,);+ return Math.floor(toolOutputTokens * BYTES_PER_TOKEN * CLI_LABEL_ROOM);}export async function queryPersonalKnowledge(diff --git a/apps/node-services/tests/memory/raw-knowledge-query.test.ts b/apps/node-services/tests/memory/raw-knowledge-query.test.tsindex 01daf3fb..c3a7e0e3 100644--- a/apps/node-services/tests/memory/raw-knowledge-query.test.ts+++ b/apps/node-services/tests/memory/raw-knowledge-query.test.ts@@ -1121,9 +1121,9 @@ describe("knowledge output budget", () => {it("sizes the budget by the model's context window", () => {expect(knowledgeOutputBudget(undefined)).toBe(12_000);- expect(knowledgeOutputBudget(8_192)).toBe(4_915);- expect(knowledgeOutputBudget(64_896)).toBe(38_937);- expect(knowledgeOutputBudget(1_000_000)).toBe(100_000);+ expect(knowledgeOutputBudget(8_192)).toBe(4_423);+ expect(knowledgeOutputBudget(64_896)).toBe(35_043);+ expect(knowledgeOutputBudget(1_000_000)).toBe(90_000);});it("keeps more of a result within a larger budget", async () => {diff --git a/apps/node-services/tests/memory/server.test.ts b/apps/node-services/tests/memory/server.test.tsindex cd7e7770..64700ce9 100644--- a/apps/node-services/tests/memory/server.test.ts+++ b/apps/node-services/tests/memory/server.test.ts@@ -417,8 +417,8 @@ describe("Memory server", () => {status: "empty",});expect(received).toEqual([body]);- // 15% of the owner's model window, at four bytes per token.- expect(budgets).toEqual([38_937]);+ // 90% of 15% of the owner's model window, at four bytes per token.+ expect(budgets).toEqual([35_043]);await post({ ...body, user_artifact_owner_actor_id: "human_offline" },"memory-token",