dsh takes a model entry's maxTokens as the output of every request. Before deciding when to compact, it reserves that output plus 65536 of headroom out of contextWindow (dsh-compaction-basic resolveCompactSpec). When a vendor's reply limit is close to its window, dsh compacts at a sliver of the window, or can't compact at all. magpie now lists a vendor's figure held to a quarter of the window (dshMaxTokens). It does this on every pi-ai route and in the legacy llm-deepseek entry, the same rule VS Code and Copilot in JetBrains already use. A Max output the user set stays as they set it (outputOr). LoKi's patch capped only an output >= the window. That misses #2823's own case (a group at 500000 / 393216) and outputs above window - 65536, such as 131072 / 65536. The quarter rule covers all of them, and a cap can only lower the reservation. Run: real dsh 0.2.0-rc.2 headless, sandbox HOME/DSH_HOME, a fake OpenAI SSE server with a tool-call step so pre-step compaction runs, and an observe-only loader hook on resolveCompactSpec. origin/maine5b57794e: 524288/524288 "leaving no message budget" 500000/393216 threshold 41248 131072/65536 "leaving no pressure budget" 1000000/393216 threshold 541248 this change (maxTokens / threshold / retain): 524288 131072 / 327680 / 62914 500000 125000 / 309464 / 60000 131072 32768 / 32768 / 15728 1000000 250000 / 684464 / 120000 The request's max_tokens matches each new value. Tests: TestDshOutputLeavesCompactionRoom (every route plus legacy; vendor, user-set, unknown window and unknown output cases). TestDshModelLimits now expects 100000 for 400000/128000, and TestMaxTokensWithinContextWindow (the every-agent guard, which the first full run caught) holds dsh to a quarter of a known window. Broken by hand: - dsh.go reverted: 4 vendor cases and TestDshModelLimits red. - Only the legacy writer reverted: 4 llm-deepseek failures. - outputOr dropped: the user-output cases red. - Old dsh.go under the new TestMaxTokensWithinContextWindow: 3 dsh rows red (32000, 51200, 16000). Checks: gofmt -l clean. go vet darwin, linux -tags nogui and windows ok. go test -timeout 45m ./... ate5b57794eplus this change, before the sync_test fix: every package ok except internal/agent, where TestMaxTokensWithinContextWindow was red (fixed here), and internal/gateway, where TestDesktopSubagentUnsetDoesNotDecode was red (1 alloc, want 0). That is #2699, an allocation counted from another goroutine in a whole-package run. It passes alone -count=3, and this change touches no gateway code. After the fix, internal/agent ran whole and was ok (2084s), and vet on all 3 OSes was ok again. The Windows TempDir flake LoKi saw in TestDshSetEffortWritesALegacyEntryWithTheCatalog did not reproduce: 10 runs of ^TestDsh on the Windows box passed. A goroutine/file probe at each TestDsh cleanup (macOS and Windows) found no late writer. Filed as #2982. Co-authored-by: LoKi <317300729+lokic7123-star@users.noreply.github.com>
Subsystem design references
These pages describe current responsibilities, runtime paths, state, and contracts. Start with the subsystem relevant to your task, then follow its source links to verify the implementation you change.
| Subsystem | Reference |
|---|---|
| Built-in subscriptions moving to plugins | Provider and plugin ownership |
| Built-in plugins: registry, lifecycle, plugins.yml, presets, settings schemas, the window's kernel (slots, error boundary, kit) | Plugin runtime |
| Plugins that run on the gateway's requests and replies | Gateway middleware |
| Tools that plugins give, offered to every agent as an MCP server | Plugin tools |
| Plugins that await context recall and observe completed replies | Asynchronous gateway request hooks |
| Claude subscription turns across Claude Code runs | Claude subscription bridge |
| How the gateway picks a provider, key or account, and falls back | Gateway routing and fallback |
| Gateway Session Aggregation | Gateway Session Aggregation |
| Connecting an agent's config to the gateway, drift, disconnect, WSL twins | Agent wiring |
| Providers, keys, subscription accounts, refresh and allowance | Providers and accounts |
| Instructions, MCP servers, skills and RTK written into agents | Library |
Tray, panel, window, magpie web and the page's JSON API |
GUI app shell |
| Encrypted backups and setup synchronization over WebDAV, S3 or GitHub | Sync and backup |
A subsystem can span several packages or repositories. Its reference describes the behavior those parts provide together; it does not need to list every function.
Maintain a reference
Update the owning reference in the same PR when a change alters a documented responsibility, interface, state transition, runtime path, compatibility rule, or failure behavior. An internal refactor that preserves these contracts does not require a prose change.
Keep each fact in one reference and link to it from agent instructions or other pages. Link to source files and named symbols rather than copying implementation details that change frequently. Explain field meanings and constraints that signatures alone do not express.
For a new reference, include the responsibilities and boundaries, source of truth, main runtime paths, state transitions, important constraints, and existing verification commands. Include failure and compatibility behavior where relevant. Document current behavior separately from proposed changes; the proposal belongs in its issue or PR.
Describe a PR's semantic changes
For each affected subsystem, explain the observable change with its trigger and before/after behavior. Include changes to interfaces, state, compatibility, and failure behavior when relevant. Link the affected reference, implementation, and verification evidence. State when a refactor preserves the documented behavior.
For example, a change to plugin adoption should explain which migration state and account condition trigger it, which implementation handles requests before and after adoption, and which test exercises that transition.
This is an author-provided explanation that a reviewer verifies against the diff and surrounding code. It is not an automatically proven compatibility verdict.
Review a change
- Identify the affected subsystem and the actual runtime path, including plugin or built-in ownership.
- Compare the claimed before/after behavior with the base and head implementation. Trace relevant callers and state transitions beyond the changed lines.
- Check that changed contracts appear in the owning reference and that source links still resolve.
- Check that the cited tests or runtime evidence exercise the claimed behavior. Report checks that were not run.
A documentation update alone does not prove the implementation. Existing tests and runtime evidence remain necessary for behavior changes.