From 589936cd8dafc9987371fad02a53577d75d37480 Mon Sep 17 00:00:00 2001 From: Alexander Miroshnichenko Date: Mon, 9 Nov 2026 10:03:47 +0300 Subject: [PATCH] feat(marmel): add arbitrary HTTP headers patch Apply upstream 56d0b1f so top-level, specialist, validator and MCP request headers can be configured. nix build .#marmel passes with the patch. --- .beads/issues.jsonl | 3 +- packages/marmel/arbitrary-http-headers.patch | 1244 ++++++++++++++++++ packages/marmel/package.nix | 9 +- 3 files changed, 1249 insertions(+), 7 deletions(-) create mode 100644 packages/marmel/arbitrary-http-headers.patch diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index a7b1689..9163df6 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -20,13 +20,14 @@ {"_type":"issue","id":"nix-overlay-du9","title":"Add kyojin package (Yamz-Labs/kyojin, ExLlamaV3 fork for Strix Halo)","description":"Package https://github.com/Yamz-Labs/kyojin: Yamz inference engine, a fork of ExLlamaV3 targeting AMD Strix Halo (gfx1151) on ROCm 7. Build the python package with the ROCm build of torch from nixpkgs and compile the HIP extension for gfx1151.","notes":"re-pinned to release tag v1.3 (latest GitHub release; v1.1 was stale), version = \"1.3\", updateScript uses tags; rebuilt from v1.3 sources, wrappers --help verified","status":"closed","priority":2,"issue_type":"feature","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-10-07T08:44:23Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-10-07T15:14:30Z","started_at":"2026-10-07T08:49:12Z","closed_at":"2026-10-07T15:14:30Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"nix-overlay-dv3","title":"Fix Config screen crash on F2","description":"F2 opens ConfigScreen then Textual raises InvalidSelectValueError because _refresh_aggregator_dropdown assigns Select.BLANK, resolved as False, instead of clearing selection.\\n\\nAcceptance: Config screen opens with saved consensus configuration; regression covers no selected aggregator; targeted tests pass.","status":"closed","priority":1,"issue_type":"bug","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-08-20T11:34:03Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-08-20T11:37:47Z","started_at":"2026-08-20T11:34:07Z","closed_at":"2026-08-20T11:37:47Z","close_reason":"Replaced invalid Select.BLANK handling; built haivemind and verified ConfigScreen headlessly.","dependency_count":0,"dependent_count":0,"comment_count":0} {"id":"nix-overlay-e2b","title":"awgen: bump 0.2 -> 0.3","description":"Upstream tag v0.3 exists at git.millerson.name/alex/awgen (commit 3d10c5f). Bump version + src/cargo hashes in packages/awgen/package.nix and verify nix build .#awgen.","status":"closed","priority":2,"issue_type":"task","assignee":"pi","created_at":"2026-10-25T13:58:22.504514222Z","created_by":"minder","updated_at":"2026-10-25T14:06:10.968003613Z","closed_at":"2026-10-25T14:06:10.966719541Z","close_reason":"Completed: awgen pinned to v0.3 (src sha256-86YT...), cargoHash unchanged (Cargo.lock deps identical). nix build .#awgen green, cargo checkPhase 29 passed/0 failed; result/bin/awgen runs.","source_repo":"nix-overlay","compaction_level":0,"original_size":0} +{"id":"nix-overlay-ehd","title":"marmel: apply arbitrary HTTP headers patch","description":"Add packages/marmel/arbitrary-http-headers.patch (upstream commit 56d0b1f: feat(config): add arbitrary HTTP headers for LLM and MCP) to package.nix patches and verify the build.","status":"closed","priority":2,"issue_type":"task","assignee":"minder","created_at":"2026-11-09T06:40:13.094596297Z","created_by":"minder","updated_at":"2026-11-09T07:03:30.016766126Z","closed_at":"2026-11-09T07:03:30.015930244Z","close_reason":"Added packages/marmel/arbitrary-http-headers.patch and wired it into package.nix; nix build .#marmel passes.","source_repo":"nix-overlay","compaction_level":0,"original_size":0} {"_type":"issue","id":"nix-overlay-ez7","title":"marmel: Landlock sandbox-exec tests fail in the Nix build sandbox","description":"Both harness::sandbox::tests::test_internal_sandbox_* tests re-exec sh through marmel --internal-sandbox-exec. Upstream apply_landlock_linux() allow-lists FHS paths only (/usr /bin /lib /opt /etc /var ...), never /nix/store, and PathFd() silently skips paths missing in the sandbox. Inside the Nix sandbox PATH points at /nix/store/-bash/bin and /nix/store/-coreutils/bin, so exec is denied: 'Failed to exec shell in sandbox: Permission denied (os error 13)'. Not a code defect: on a real NixOS host sh resolves through /bin -\u003e system path which the /bin rule covers.","status":"closed","priority":2,"issue_type":"task","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-10-06T18:58:44Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-10-06T19:31:56Z","started_at":"2026-10-06T18:59:02Z","closed_at":"2026-10-06T19:31:56Z","close_reason":"Skipped both sandbox-exec tests in packages/marmel; nix build .#marmel green (342/344 run). Runtime defect split out to nix-overlay-5ml.","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"nix-overlay-f8c","title":"Add nftablesbuilder package","description":"Package NftablesBuilder (github.com/AiseBouma/NftablesBuilder) web interface to manage nftables. Upstream source is incomplete (missing settings crate, no GUI build tooling, no Cargo.lock) so: patch in authored settings crate (schema from release artifacts), build Rust workspace from pinned commit, reuse built GUI assets from hash-pinned release tarball.","status":"closed","priority":2,"issue_type":"task","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-08-07T04:58:22Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-08-07T05:38:40Z","started_at":"2026-08-07T04:58:25Z","closed_at":"2026-08-07T05:38:40Z","close_reason":"Package added, builds green, committed","dependency_count":0,"dependent_count":0,"comment_count":0} {"id":"nix-overlay-g4w","title":"pdfcraft: drop nixpkgs-rust195 pin once nixos-unstable rustc assembles AVX-VNNI again","description":"nixos-unstable's rustc 1.98.1/1.99.0 (LLVM 21.1.8) rejects every AVX-VNNI intrinsic with 'intrinsic signature mismatch for llvm.x86.avx512.vpdpbusd/vpwssd', which breaks rten-gemm, pic-scale and zlib-rs. pdfcraft therefore builds against inputs.nixpkgs-rust195 (nixos-26.05, rustc 1.95.0) in packages/pdfcraft/default.nix. Re-test with: rustc -O on a snippet calling _mm512_dpbusd_epi32; when nixos-unstable's rustc compiles it, delete the input and revert default.nix to pkgs.callPackage.","status":"open","priority":3,"issue_type":"task","created_at":"2026-11-03T10:48:09.706576343Z","created_by":"minder","updated_at":"2026-11-03T10:48:09.706576343Z","source_repo":"nix-overlay","compaction_level":0,"original_size":0} {"_type":"issue","id":"nix-overlay-hcf","title":"Update all overlay packages to latest upstream versions","description":"Bump every package in packages/ to its latest upstream release/commit using nix-update, then verify each builds. Working tree already carries uncommitted bumps for 15 packages (awg-tool, ds4, freebuff, freetoken, g3, graphify, hipengine, kubernetes-mcp-server, llama-cpp, marmel, omniroute, open-code-review, radar, skillsmcp, traycer); remaining packages (aionui, container-use, desloppify, haivemind, loop, mcp-gateway, nftables-analyzer, nftablesbuilder, relay-free-llm, shardr, stakpak) still need checking.","status":"closed","priority":2,"issue_type":"task","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-10-06T12:54:12Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-10-06T13:46:41Z","started_at":"2026-10-06T12:54:20Z","closed_at":"2026-10-06T13:46:41Z","close_reason":"All 26 packages checked: 16 committed in 6013b17aadbc; aionui/container-use/desloppify/loop/mcp-gateway/nftables-analyzer/nftablesbuilder/relay-free-llm/stakpak already current; 3 evaluation/version-string errors from nix-update repaired by hand. omniroute deferred to its own issue (stale npmDepsHash).","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"nix-overlay-ikz","title":"Add g3 package (dhanji/g3)","description":"Package g3 AI coding agent from https://github.com/dhanji/g3 as rustPlatform.buildRustPackage. Pin commit (no tags), ship g3 + g3-studio binaries, category AI Coding Agents.","status":"closed","priority":2,"issue_type":"feature","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-09-23T10:59:57Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-09-23T11:54:07Z","started_at":"2026-09-23T11:00:30Z","closed_at":"2026-09-23T11:54:07Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"nix-overlay-m5y","title":"Add CodeAF package (Agent-Field/CodeAF)","description":"Add https://github.com/Agent-Field/CodeAF as a new package under packages/codeaf with default.nix + package.nix, staged for blueprint discovery, README table updated, nix build verified.","status":"closed","priority":2,"issue_type":"feature","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-10-06T18:07:38Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-10-06T18:46:34Z","started_at":"2026-10-06T18:08:19Z","closed_at":"2026-10-06T18:46:34Z","close_reason":"Closed","dependency_count":0,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"nix-overlay-nn2","title":"marmel: drop session-boot-trust-store.patch once upstream stops panicking on a missing CA store","description":"packages/marmel/session-boot-trust-store.patch forwards SSL_CERT_FILE into\nthe env_clear()d child in tests/test_session_boot.rs. It is a workaround,\nnot the fix: src/llm/client.rs default_http_client() calls unwrap_or_default()\non Result\u003cClient, reqwest::Error\u003e, and reqwest's Client::default() is\nClient::new(), which repeats the same build and .expect()s it, so the fallback\nalways panics. Reproduced independently of Nix on a stock busybox image with no\n/etc/ssl.\n\nUpstream issue filed 2026-10-16, still open:\nhttps://github.com/Na1w/marmel/issues/13\n(\"marmel panics at startup instead of reporting a missing CA trust store\")\n\nBlocked on upstream. Once upstream propagates the error instead of discarding\nit, drop the patch, bump marmel to the fixing commit, and rebuild.\n","status":"open","priority":3,"issue_type":"task","owner":"alex@millerson.name","created_at":"2026-10-16T11:32:29Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-10-16T13:27:05Z","external_ref":"https://github.com/Na1w/marmel/issues/13","dependency_count":0,"dependent_count":0,"comment_count":0} +{"id":"nix-overlay-nn2","title":"marmel: drop session-boot-trust-store.patch once upstream stops panicking on a missing CA store","description":"packages/marmel/session-boot-trust-store.patch forwards SSL_CERT_FILE into\nthe env_clear()d child in tests/test_session_boot.rs. It is a workaround,\nnot the fix: src/llm/client.rs default_http_client() calls unwrap_or_default()\non Result, and reqwest's Client::default() is\nClient::new(), which repeats the same build and .expect()s it, so the fallback\nalways panics. Reproduced independently of Nix on a stock busybox image with no\n/etc/ssl.\n\nUpstream issue filed 2026-10-16, still open:\nhttps://github.com/Na1w/marmel/issues/13\n(\"marmel panics at startup instead of reporting a missing CA trust store\")\n\nBlocked on upstream. Once upstream propagates the error instead of discarding\nit, drop the patch, bump marmel to the fixing commit, and rebuild.\n","status":"closed","priority":3,"issue_type":"task","owner":"alex@millerson.name","created_at":"2026-10-16T11:32:29Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-11-09T07:03:29.680285424Z","closed_at":"2026-11-09T07:03:29.673967963Z","close_reason":"Upstream fixed in 62cb756 (#16, 2026-10-18); session-boot-trust-store.patch was already dropped in adbed5f3c074. Current pin afcc1dc is 31 commits ahead of the fix and the package carries no such patch.","external_ref":"https://github.com/Na1w/marmel/issues/13","source_repo":".","compaction_level":0,"original_size":0} {"id":"nix-overlay-ocf","title":"agnix: ship agnix-lsp and agnix-mcp in the same package","description":"Extend packages/agnix to build -p agnix-cli -p agnix-lsp -p agnix-mcp, as upstream's release job does.","status":"closed","priority":3,"issue_type":"task","assignee":"minder","created_at":"2026-11-03T12:17:08.080999670Z","created_by":"minder","updated_at":"2026-11-03T12:33:21.279454904Z","closed_at":"2026-11-03T12:33:21.278951385Z","close_reason":"packages/agnix now builds -p agnix-cli -p agnix-lsp -p agnix-mcp; nix build .#agnix produces all three binaries; agnix-lsp answers LSP initialize with capabilities, agnix-mcp answers MCP initialize with serverInfo agnix 0.60.3","source_repo":"nix-overlay","compaction_level":0,"original_size":0} {"_type":"issue","id":"nix-overlay-olq","title":"graphify: v1.0.0 tag is a stale fork branch - verify PyPI before bumping","description":"Bumping graphify 0.9.61 -\u003e 1.0.0 (commit 6013b17) was a false upgrade. nix-update picks the highest semver tag, and Graphify-Labs/graphify has a v1.0.0 tag that is NOT newer than v0.9.77:\n\n- v1.0.0 = 0a31c086, authored 2026-04-05, its pyproject still says graphifyy 0.1.10\n- v0.9.77 = 5c7b8479, authored 2026-10-05, commit message 'release: 0.9.77'\n- GitHub compare v0.9.77...v1.0.0: diverged, ahead_by 28, behind_by 2126\n- PyPI graphifyy latest is 0.9.77; no 1.0.0 was ever published\n\nv1.0.0 is a six-month-old branch that was never released. Pin is back to v0.9.77 (built + CLI smoke-tested). Two dependency corrections were needed: upstream vendored MinHash into graphify/_minhash.py (datasketch is gone, and with it the pybloomfilter3 metadata workaround in default.nix) and added a direct numpy import, so numpy is now an explicit dep.\n\nWhat made 1.0.0 look unbuildable: its pyproject hard-requires graspologic, which pulls python-future, disabled in nixpkgs for Python \u003e= 3.13 (this flake is on 3.14). Do not chase it - v1.0.0 has no Leiden fallback (cluster.py imports graspologic.partition.leiden unconditionally), so it is worse than 0.9.x even if it built.\n\nGuard: check https://pypi.org/pypi/graphifyy/json before accepting any nix-update bump here.","status":"closed","priority":2,"issue_type":"bug","owner":"alex@millerson.name","created_at":"2026-10-06T16:36:37Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-10-06T16:50:10Z","closed_at":"2026-10-06T16:50:10Z","close_reason":"Pinned back to v0.9.77 in dcbde330e4c3; v1.0.0 confirmed to be a stale branch (2126 behind, never on PyPI). Guard noted in package.nix and bd remember.","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"nix-overlay-pdo","title":"codeaf: carried furrow is unpatched ELF, cannot run on NixOS","description":"The Agent-Field/furrow v0.1.0 release asset that codeaf embeds is a CGO/dynamic binary (INTERP /lib64/ld-linux-x86-64.so.2, NEEDED libc/libm/libgcc_s). codeaf writes it out to CODEAF_HOME/bin/furrow-0.1.0 and execs it; NixOS refuses: 'Could not start dynamically linked executable'. cmd/fetch re-hashes against internal/furrowbin/pin.json, so the artifact cannot be patchelf'd before it is staged. Fix: fetch the asset, autoPatchelfHook it in its own derivation, and point CODEAF_FURROW at it in a wrapper (internal/furrow lookBinary order is CODEAF_FURROW, embedded, PATH).","status":"in_progress","priority":1,"issue_type":"task","assignee":"Alexander Miroshnichenko","owner":"alex@millerson.name","created_at":"2026-10-07T07:11:38Z","created_by":"Alexander Miroshnichenko","updated_at":"2026-10-07T07:11:43Z","started_at":"2026-10-07T07:11:43Z","lease_expires_at":"2026-10-07T07:16:43Z","heartbeat_at":"2026-10-07T07:11:43Z","dependency_count":0,"dependent_count":0,"comment_count":0} diff --git a/packages/marmel/arbitrary-http-headers.patch b/packages/marmel/arbitrary-http-headers.patch new file mode 100644 index 0000000..100a8d2 --- /dev/null +++ b/packages/marmel/arbitrary-http-headers.patch @@ -0,0 +1,1244 @@ +From 56d0b1f73e9e80596e3494ae14c8af6debdc5789 Mon Sep 17 00:00:00 2001 +From: Alexander Miroshnichenko +Date: Mon, 9 Nov 2026 09:37:42 +0300 +Subject: [PATCH] feat(config): add arbitrary HTTP headers for LLM and MCP +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 8bit + +Add `headers` at the top level, per specialist (`headers`, +`validator_headers`) and per MCP server. Scopes merge per header +name; `false` removes an inherited header. + +`x-opencode-session = true` generates one stable `ses_` per +process. An explicit `Authorization` header suppresses `auth_token`. +Transport-owned headers are rejected at config load; direct config +construction warns and skips them. + +Signed-off-by: Alexander Miroshnichenko +--- + README.md | 26 +++- + marmel.toml.example | 21 ++- + src/agents/runner/fix_loop.rs | 67 ++++++++++ + src/agents/runner/mod.rs | 55 +++++++- + src/agents/validation.rs | 8 +- + src/config.rs | 186 ++++++++++++++++++++++++++ + src/llm/client.rs | 19 ++- + src/llm/client_tests.rs | 59 +++++++++ + src/mcp/client.rs | 9 ++ + src/mcp/http.rs | 32 ++++- + src/mcp/http_tests.rs | 48 +++++++ + src/net/headers.rs | 242 ++++++++++++++++++++++++++++++++++ + src/net/mod.rs | 1 + + src/orchestrator/delegate.rs | 2 +- + src/ui/session_tests.rs | 2 + + src/ui/tui/tests_new_ui.rs | 2 + + 16 files changed, 766 insertions(+), 13 deletions(-) + create mode 100644 src/net/headers.rs + +diff --git a/README.md b/README.md +index a243f8d2cd08..fcb58af326f3 100644 +--- a/README.md ++++ b/README.md +@@ -215,6 +215,7 @@ Marmel loads the first configuration file found from this list (files are not me + |---|---|---| + | `backend_url` | `http://localhost:8000/v1` | Base URL for OpenAI-compatible chat completions (no trailing slash). | + | `auth_token` | `""` | Optional Bearer token for authentication. | ++| `headers` | `{}` | Extra HTTP headers for LLM requests. `x-opencode-session = true` generates one stable session id per process. | + | `model` | `qwen-3.8-27b` | Model identifier requested from the backend. | + | `temperature` | `0.7` | Sampling temperature. | + | `top_p` | `0.9` | Nucleus sampling probability. | +@@ -239,8 +240,8 @@ Marmel loads the first configuration file found from this list (files are not me + | `[monitoring]` | — | Settings for repetition breakers and token ceilings (`repetition_threshold = 5`, `min_pattern_len = 5`). | + | `[orchestration]` | — | Top-level concurrency and delegation settings (`max_recursion_depth = 3`). | + | `[orchestration.concurrency]` | _unset_ | Concurrency caps per backend URL and model pair. | +-| `[orchestration.specialists.]` | — | Per-role configuration overrides (`model`, `backend_url`, `tools`, `mcp_servers`, `reasoning_effort`, `max_validator_iterations`). | +-| `[mcp_servers.]` | — | External MCP server registration (`command`, `args`, `env`, `url`, `prompt`). | ++| `[orchestration.specialists.]` | — | Per-role configuration overrides (`model`, `backend_url`, `auth_token`, `headers`, `validator_headers`, `tools`, `mcp_servers`, `reasoning_effort`, `max_validator_iterations`). | ++| `[mcp_servers.]` | — | External MCP server registration (`command`, `args`, `env`, `url`, `headers`, `prompt`). | + + ### Example `marmel.toml` + +@@ -294,6 +295,27 @@ reasoning_effort = "low" + validator_reasoning_effort = "minimal" + ``` + ++### Request Headers (`headers`) ++ ++`headers` adds arbitrary HTTP headers to LLM requests. The top level applies to the Manager and every specialist. A specialist entry overrides the top level per header name, and `validator_headers` overrides both for that role's validator. A `false` value removes an inherited header. ++ ++```toml ++headers = { x-opencode-session = true, x-api-key = "top-key" } ++ ++[orchestration.specialists.coder] ++headers = { x-api-key = "coder-key" } ++validator_headers = { x-audit = "1" } ++ ++[mcp_servers.remote] ++url = "https://example.com/mcp" ++headers = { Authorization = "Bearer mcp-token" } ++``` ++ ++- `x-opencode-session = true` generates one `ses_` at process start and sends it on every request. Use a string to pin a fixed value. ++- `auth_token` is not sent when an explicit `Authorization` header is set; Marmel logs a warning about the conflict. ++- Transport-owned headers (`Content-Type`, `Accept`, `Content-Length`, `Host`, `Connection`, `Transfer-Encoding`, `Mcp-Session-Id`) are rejected at startup. ++- MCP `headers` apply to every HTTP/SSE request. A stdio server (`command`) ignores them; use `env` instead. ++ + ### Session Concurrency (`[orchestration.concurrency]`) + + When running against a local LLM server with limited GPU memory, running multiple subagents, validator loops, and JIT prompt architects in parallel can cause severe queuing and out-of-memory errors. The concurrency table caps simultaneous streams per backend URL and model: +diff --git a/marmel.toml.example b/marmel.toml.example +index 16d3d613c90b..aec261cfb609 100644 +--- a/marmel.toml.example ++++ b/marmel.toml.example +@@ -6,6 +6,14 @@ backend_url = "http://localhost:8000/v1" + # Optional bearer token sent as `Authorization: Bearer `. + auth_token = "" + ++# Optional extra HTTP headers sent with every LLM request (Manager, specialists ++# and validators). A per-specialist `headers` entry overrides this map per ++# header name; `false` removes an inherited header. `x-opencode-session = true` ++# generates one stable `ses_` at process start; use a string to pin a ++# fixed value. Transport-owned headers (Content-Type, Accept, Content-Length, ++# Host, Connection, Transfer-Encoding, Mcp-Session-Id) are rejected at startup. ++# headers = { x-opencode-session = true, x-api-key = "..." } ++ + # Model identifier to send in the chat request. + model = "qwen-3.8-27b" + +@@ -206,6 +214,10 @@ model = "deepseek-v4-flash:cloud" + # Optional unique backend and token for coder: + # backend_url = "http://localhost:11434/v1" + # auth_token = "" ++# Optional extra request headers for this specialist and its validator. Both ++# merge over the top-level `headers` per header name. ++# headers = { x-api-key = "coder-key" } ++# validator_headers = { x-audit = "1" } + # Optional: set false to disable automated quality-assurance validation + # enable_validator = false + # Optional unique validator settings for coder's quality assurance: +@@ -282,6 +294,10 @@ model = "deepseek-v4-flash:cloud" + # `env` is optional and only applies to stdio servers: it sets environment + # variables for the spawned process (e.g. API keys the server needs). + # ++# `headers` is optional and only applies to remote HTTP/SSE servers: it adds ++# request headers to every request (`initialize`, `tools/list`, `tools/call`). ++# A stdio server ignores `headers`; use `env` there instead. ++# + # Discovered MCP tools are exposed to agents with a `__` + # prefix (e.g. a tool `read_file` from server `filesystem` appears as + # `filesystem__read_file`). This prefix prevents name collisions when multiple +@@ -289,8 +305,8 @@ model = "deepseek-v4-flash:cloud" + # before the call is sent to the MCP server, so the server always receives the + # original unprefixed tool name. + # +-# The legal keys of a server table are exactly `command`, `args`, `env`, `url` +-# and `prompt`: there is NO `enabled`, `timeout` or `headers` key (a typo'd key ++# The legal keys of a server table are exactly `command`, `args`, `env`, `url`, ++# `headers` and `prompt`: there is NO `enabled` or `timeout` key (a typo'd key + # is dropped in silence, not an error). Exactly one transport per server — + # `command` wins when both `command` and `url` are set. `prompt` is optional + # free-form text injected into the system prompt of every agent allowed to use +@@ -306,6 +322,7 @@ args = ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"] + # Example 2 — remote/external HTTP/SSE server (reached over the network). + [mcp_servers.remote] + url = "https://example.com/mcp" ++# headers = { Authorization = "Bearer ...", x-opencode-session = true } + + # Example 3 — the offline reference library built in this workspace, registered + # under the server name `kiwix` (a legal name: `[A-Za-z0-9_-]+`, no `__`, and it +diff --git a/src/agents/runner/fix_loop.rs b/src/agents/runner/fix_loop.rs +index 1c569d293e75..7ca678289864 100644 +--- a/src/agents/runner/fix_loop.rs ++++ b/src/agents/runner/fix_loop.rs +@@ -689,6 +689,27 @@ pub fn resolve_validator_backend( + (backend, token, model, reasoning_effort) + } + ++/// Resolve a validator pass's request headers: top-level ++/// [`crate::config::Config::headers`], then `[specialists.validator].headers`, ++/// then the audited specialist's `validator_headers`. ++pub fn resolve_validator_headers( ++ cfg: &crate::config::Config, ++ specialist: crate::agents::Agent, ++) -> reqwest::header::HeaderMap { ++ let mut layers = vec![&cfg.headers]; ++ if let Some(validator_cfg) = cfg ++ .orchestration ++ .specialists ++ .get(crate::agents::Agent::Validator.as_str()) ++ { ++ layers.push(&validator_cfg.headers); ++ } ++ if let Some(specialist_cfg) = cfg.orchestration.specialists.get(specialist.as_str()) { ++ layers.push(&specialist_cfg.validator_headers); ++ } ++ crate::net::headers::resolve(&layers) ++} ++ + /// Shared tool-dispatch core: monitor intervention check → cancellation check + /// → `dispatch_for_async_with_engine`. Returns `(content, execution_succeeded)` + /// where `content` starts with `"ERROR: "` on failure. +@@ -1410,6 +1431,7 @@ pub fn register_loop_worker( + mod tests { + use super::{ + append_tool_result, inject_worker_notices_at_turn_start, inject_worker_notices_mid_turn, ++ resolve_validator_headers, + }; + use crate::manager::{ContextEngine, ContextEngineFactory}; + use crate::orchestrator::notice::{TEST_NOTICE_MUTEX, has_worker_inbox, worker_inbox_len}; +@@ -2760,4 +2782,49 @@ mod tests { + ); + } + } ++ ++ /// Validator headers merge top level → `[specialists.validator]` → the ++ /// audited specialist's `validator_headers`, per header name. ++ #[test] ++ fn validator_headers_merge_in_scope_order() { ++ use crate::config::{Config, SpecialistConfig}; ++ use crate::net::headers::HeaderSpec; ++ ++ let mut cfg = Config::default(); ++ cfg.headers ++ .insert("x-scope".into(), HeaderSpec::Text("top".into())); ++ cfg.headers ++ .insert("x-top".into(), HeaderSpec::Text("kept".into())); ++ ++ let mut validator = SpecialistConfig::default(); ++ validator ++ .headers ++ .insert("x-scope".into(), HeaderSpec::Text("validator".into())); ++ cfg.orchestration ++ .specialists ++ .insert("validator".into(), validator); ++ ++ let mut coder = SpecialistConfig::default(); ++ coder ++ .validator_headers ++ .insert("x-scope".into(), HeaderSpec::Text("coder-validator".into())); ++ coder ++ .validator_headers ++ .insert("x-top".into(), HeaderSpec::Flag(false)); ++ cfg.orchestration.specialists.insert("coder".into(), coder); ++ ++ let headers = resolve_validator_headers(&cfg, crate::agents::Agent::Coder); ++ assert_eq!( ++ headers ++ .get("x-scope") ++ .expect("header present") ++ .to_str() ++ .expect("valid header value"), ++ "coder-validator" ++ ); ++ assert!( ++ headers.get("x-top").is_none(), ++ "the innermost scope removed the inherited header" ++ ); ++ } + } +diff --git a/src/agents/runner/mod.rs b/src/agents/runner/mod.rs +index b290990aabae..83152b883014 100644 +--- a/src/agents/runner/mod.rs ++++ b/src/agents/runner/mod.rs +@@ -91,6 +91,19 @@ pub fn resolve_specialist_backend( + (backend, auth_token, model) + } + ++/// Resolve a specialist's request headers: top-level [`crate::config::Config::headers`] ++/// overridden per header name by `[specialists.].headers`. ++pub fn resolve_specialist_headers( ++ cfg: &crate::config::Config, ++ agent: Agent, ++) -> reqwest::header::HeaderMap { ++ let mut layers = vec![&cfg.headers]; ++ if let Some(specialist_cfg) = cfg.orchestration.specialists.get(agent.as_str()) { ++ layers.push(&specialist_cfg.headers); ++ } ++ crate::net::headers::resolve(&layers) ++} ++ + pub(crate) async fn try_run_specialist_live( + agent: Agent, + ctx: &IsolatedContext, +@@ -115,7 +128,8 @@ pub(crate) async fn try_run_specialist_live( + if backend_url.is_empty() { + return None; + } +- let client = crate::llm::ChatClient::new_with_token(&backend_url, &model, &auth_token); ++ let client = crate::llm::ChatClient::new_with_token(&backend_url, &model, &auth_token) ++ .with_headers(resolve_specialist_headers(&cfg, agent)); + // Call class for this delegation, installed before the turn starts and + // discharged when it returns. This is the point where the delegation's + // `(backend_url, model)` pair is resolved, so the pool's gate in +@@ -139,3 +153,42 @@ pub(crate) async fn try_run_specialist_live( + }; + Some(res) + } ++ ++#[cfg(test)] ++mod tests { ++ use super::resolve_specialist_headers; ++ use crate::agents::Agent; ++ use crate::config::{Config, SpecialistConfig}; ++ use crate::net::headers::HeaderSpec; ++ ++ /// Specialist headers override the top-level map per header name, and ++ /// `false` removes an inherited header. ++ #[test] ++ fn specialist_headers_override_the_top_level_map() { ++ let mut cfg = Config::default(); ++ cfg.headers ++ .insert("x-scope".into(), HeaderSpec::Text("top".into())); ++ cfg.headers ++ .insert("x-remove".into(), HeaderSpec::Text("top".into())); ++ ++ let mut coder = SpecialistConfig::default(); ++ coder ++ .headers ++ .insert("x-scope".into(), HeaderSpec::Text("coder".into())); ++ coder ++ .headers ++ .insert("x-remove".into(), HeaderSpec::Flag(false)); ++ cfg.orchestration.specialists.insert("coder".into(), coder); ++ ++ let headers = resolve_specialist_headers(&cfg, Agent::Coder); ++ assert_eq!( ++ headers ++ .get("x-scope") ++ .expect("header present") ++ .to_str() ++ .expect("valid header value"), ++ "coder" ++ ); ++ assert!(headers.get("x-remove").is_none()); ++ } ++} +diff --git a/src/agents/validation.rs b/src/agents/validation.rs +index 8522344ff99a..2348bbaa7aea 100644 +--- a/src/agents/validation.rs ++++ b/src/agents/validation.rs +@@ -12,7 +12,7 @@ + use crate::agents::Agent; + use crate::agents::runner::fix_loop::{ + FixLoopBounds, FixLoopResult, LoopParams, assemble_tools, register_loop_worker, +- resolve_validator_backend, run_fix_loop, ++ resolve_validator_backend, resolve_validator_headers, run_fix_loop, + }; + use crate::markers::{has_failure_verdict_cue, is_failure_verdict_word}; + use crate::tool_names::is_leave_verdict_tool_name; +@@ -408,7 +408,8 @@ async fn run_automated_validation_inner( + // correct: a different model server has its own slots, so it gets its own pool. + let (backend, auth, validator_model, validator_reasoning_effort) = + resolve_validator_backend(cfg, agent); +- let val_client = crate::llm::ChatClient::new_with_token(&backend, &validator_model, &auth); ++ let val_client = crate::llm::ChatClient::new_with_token(&backend, &validator_model, &auth) ++ .with_headers(resolve_validator_headers(cfg, agent)); + + let brief = format!( + "Task Brief:\n{}\n\nSpecialist Deliverable:\n{}\n\n\ +@@ -625,7 +626,8 @@ async fn run_plan_validation_inner( + // `[specialists.validator]` override points it at another endpoint/model. + let (backend, auth, validator_model, validator_reasoning_effort) = + resolve_validator_backend(cfg, Agent::Planner); +- let val_client = crate::llm::ChatClient::new_with_token(&backend, &validator_model, &auth); ++ let val_client = crate::llm::ChatClient::new_with_token(&backend, &validator_model, &auth) ++ .with_headers(resolve_validator_headers(cfg, Agent::Planner)); + + let brief = format!( + "Proposed Execution Plan:\n```markdown\n{}\n```\n\n\ +diff --git a/src/config.rs b/src/config.rs +index 4cb22c9f7bc6..5579512f3b64 100644 +--- a/src/config.rs ++++ b/src/config.rs +@@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize}; + use std::collections::{BTreeMap, HashMap}; + use std::path::PathBuf; + ++use crate::net::headers::{HeaderSpecs, sets_header, validate as validate_headers}; ++ + /// Default delegation depth bound. + pub const DEFAULT_MAX_RECURSION_DEPTH: usize = 3; + +@@ -217,12 +219,19 @@ pub struct SpecialistConfig { + pub backend_url: Option, + /// Optional auth token override for this specialist. + pub auth_token: Option, ++ /// Optional request-header overrides for this specialist's backend. Merged ++ /// over the top-level [`Config::headers`] per header name; `false` removes an ++ /// inherited header. ++ pub headers: HeaderSpecs, + /// Optional model override for this specialist's validator. + pub validator_model: Option, + /// Optional backend URL override for this specialist's validator. + pub validator_backend_url: Option, + /// Optional auth token override for this specialist's validator. + pub validator_auth_token: Option, ++ /// Optional request-header overrides for this specialist's validator. Merged ++ /// over the top-level headers and `[specialists.validator].headers`. ++ pub validator_headers: HeaderSpecs, + /// Optional reasoning-effort override for this specialist's validator + /// (free-form pass-through; backends use different vocabularies). + pub validator_reasoning_effort: Option, +@@ -326,6 +335,9 @@ impl std::str::FromStr for GitWorkflowPolicy { + pub struct Config { + pub backend_url: String, + pub auth_token: String, ++ /// Arbitrary request headers for the LLM backend. `x-opencode-session = true` ++ /// generates one stable session id for the whole process. ++ pub headers: HeaderSpecs, + pub model: String, + pub temperature: f32, + pub top_p: f32, +@@ -431,6 +443,7 @@ impl Default for Config { + Config { + backend_url: "http://localhost:8000/v1".to_string(), + auth_token: String::new(), ++ headers: HeaderSpecs::new(), + model: "qwen-3.8-27b".to_string(), + temperature: 0.7, + top_p: 0.9, +@@ -823,10 +836,61 @@ pub fn load(explicit_path: Option<&str>) -> Result { + std::env::var(SESSION_COMMIT_MIN_MS_ENV).ok(), + ); + ++ validate_headers_config(&cfg)?; + cfg.expand_paths(); + Ok(cfg) + } + ++/// Reject transport-owned headers and flag misuse at startup, before any ++/// request is built. Direct `Config` construction (tests) skips this; the ++/// resolvers degrade invalid entries to a warning instead of panicking. ++fn validate_headers_config(cfg: &Config) -> Result<()> { ++ validate_headers(&cfg.headers, "headers").map_err(anyhow::Error::msg)?; ++ warn_auth_conflict(&cfg.headers, &cfg.auth_token, "headers"); ++ ++ let validator_cfg = cfg.orchestration.specialists.get("validator"); ++ for (role, specialist) in &cfg.orchestration.specialists { ++ validate_headers(&specialist.headers, &format!("specialist '{role}' headers")) ++ .map_err(anyhow::Error::msg)?; ++ let effective_token = specialist.auth_token.as_deref().unwrap_or(&cfg.auth_token); ++ warn_auth_conflict( ++ &specialist.headers, ++ effective_token, ++ &format!("specialist '{role}' headers"), ++ ); ++ ++ validate_headers( ++ &specialist.validator_headers, ++ &format!("specialist '{role}' validator_headers"), ++ ) ++ .map_err(anyhow::Error::msg)?; ++ let effective_validator_token = specialist ++ .validator_auth_token ++ .as_deref() ++ .or_else(|| validator_cfg.and_then(|vc| vc.auth_token.as_deref())) ++ .unwrap_or(&cfg.auth_token); ++ warn_auth_conflict( ++ &specialist.validator_headers, ++ effective_validator_token, ++ &format!("specialist '{role}' validator_headers"), ++ ); ++ } ++ ++ for (name, server) in &cfg.mcp_servers { ++ validate_headers(&server.headers, &format!("mcp server '{name}' headers")) ++ .map_err(anyhow::Error::msg)?; ++ } ++ Ok(()) ++} ++ ++fn warn_auth_conflict(headers: &HeaderSpecs, token: &str, scope: &str) { ++ if !token.is_empty() && sets_header(headers, "authorization") { ++ tracing::warn!( ++ "{scope}: both `auth_token` and an explicit `Authorization` header are set; the header wins" ++ ); ++ } ++} ++ + /// Tri-state reading of a `MARMEL_*` boolean: `1`/`true`/`yes`/`on` ⇒ `Some(true)`, + /// `0`/`false`/`no`/`off` ⇒ `Some(false)`, anything else (including unset and + /// unparseable junk) ⇒ `None`, which means "leave the file/default value alone". +@@ -925,6 +989,7 @@ fn resolve_config_path(explicit_path: Option<&str>) -> Option { + struct PartialConfig { + pub backend_url: Option, + pub auth_token: Option, ++ pub headers: Option, + pub model: Option, + pub temperature: Option, + pub top_p: Option, +@@ -1012,6 +1077,9 @@ fn merge(mut base: Config, partial: PartialConfig) -> Config { + { + base.auth_token = v; + } ++ if let Some(v) = partial.headers { ++ base.headers = v; ++ } + if let Some(v) = partial.model + && !v.is_empty() + { +@@ -1273,6 +1341,7 @@ pub(crate) fn sandbox_env_test_lock() -> std::sync::MutexGuard<'static, ()> { + #[cfg(test)] + mod tests { + use super::*; ++ use crate::net::headers::HeaderSpec; + + #[test] + fn test_orchestr_config_default_depth() { +@@ -1323,6 +1392,123 @@ mod tests { + assert!(validator.tools.iter().any(|t| t == "gedcom__*")); + } + ++ // ------------------------------------------------------------- headers ++ // ++ // `headers` is a map of header name to either a literal string or the bool ++ // flag `true` (generate the process session id) / `false` (remove an ++ // inherited header). Scopes are top level, per specialist, and per ++ // specialist validator; `validate_headers_config` is the startup gate that ++ // refuses transport-owned names and flag misuse. ++ ++ #[test] ++ fn headers_parse_at_every_scope() { ++ let cfg = merged( ++ r#" ++ headers = { x-api-key = "top", x-opencode-session = true } ++ [mcp_servers.auth] ++ url = "https://example.test/mcp" ++ headers = { Authorization = "Bearer mcp" } ++ [orchestration.specialists.coder] ++ headers = { x-api-key = "coder", x-remove = false } ++ validator_headers = { x-validator = "yes" } ++ "#, ++ ); ++ ++ assert_eq!( ++ cfg.headers.get("x-api-key"), ++ Some(&HeaderSpec::Text("top".to_string())) ++ ); ++ assert_eq!( ++ cfg.headers.get("x-opencode-session"), ++ Some(&HeaderSpec::Flag(true)) ++ ); ++ ++ let server = cfg.mcp_servers.get("auth").expect("mcp server"); ++ assert_eq!( ++ server.headers.get("Authorization"), ++ Some(&HeaderSpec::Text("Bearer mcp".to_string())) ++ ); ++ ++ let coder = cfg ++ .orchestration ++ .specialists ++ .get("coder") ++ .expect("coder present"); ++ assert_eq!( ++ coder.headers.get("x-api-key"), ++ Some(&HeaderSpec::Text("coder".to_string())) ++ ); ++ assert_eq!( ++ coder.headers.get("x-remove"), ++ Some(&HeaderSpec::Flag(false)) ++ ); ++ assert_eq!( ++ coder.validator_headers.get("x-validator"), ++ Some(&HeaderSpec::Text("yes".to_string())) ++ ); ++ } ++ ++ #[test] ++ fn headers_merge_replaces_the_whole_top_level_map() { ++ let mut base = Config::default(); ++ base.headers ++ .insert("x-keep".to_string(), HeaderSpec::Text("base".to_string())); ++ let partial: PartialConfig = ++ toml::from_str("headers = { x-new = \"project\" }\n").expect("parses"); ++ ++ let cfg = merge(base, partial); ++ assert_eq!( ++ cfg.headers.get("x-new"), ++ Some(&HeaderSpec::Text("project".to_string())) ++ ); ++ assert!( ++ cfg.headers.get("x-keep").is_none(), ++ "a re-declared top-level map replaces the whole map, like every other top-level key" ++ ); ++ } ++ ++ #[test] ++ fn headers_validation_accepts_valid_configuration() { ++ let cfg = merged( ++ r#" ++ headers = { x-api-key = "top", x-opencode-session = true } ++ [mcp_servers.auth] ++ url = "https://example.test/mcp" ++ headers = { Authorization = "Bearer mcp" } ++ [orchestration.specialists.coder] ++ headers = { x-remove = false } ++ validator_headers = { x-validator = "yes" } ++ "#, ++ ); ++ assert!(validate_headers_config(&cfg).is_ok()); ++ } ++ ++ #[test] ++ fn headers_validation_rejects_reserved_names_and_flag_misuse() { ++ let reserved = merged("headers = { Content-Type = \"text/plain\" }\n"); ++ assert!(validate_headers_config(&reserved).is_err()); ++ ++ let mcp_reserved = merged( ++ r#" ++ [mcp_servers.auth] ++ url = "https://example.test/mcp" ++ headers = { Mcp-Session-Id = "fixed" } ++ "#, ++ ); ++ assert!(validate_headers_config(&mcp_reserved).is_err()); ++ ++ let wrong_flag = merged("headers = { x-api-key = true }\n"); ++ assert!(validate_headers_config(&wrong_flag).is_err()); ++ ++ let specialist_reserved = merged( ++ r#" ++ [orchestration.specialists.coder] ++ validator_headers = { Host = "example.test" } ++ "#, ++ ); ++ assert!(validate_headers_config(&specialist_reserved).is_err()); ++ } ++ + #[test] + fn orchestration_absent_keeps_default() { + let toml_str = "backend_url = \"http://localhost:9000/v1\"\n"; +diff --git a/src/llm/client.rs b/src/llm/client.rs +index a7b026525355..385c54b272e3 100644 +--- a/src/llm/client.rs ++++ b/src/llm/client.rs +@@ -4,6 +4,7 @@ use crate::orchestrator::pool; + use crate::types::{ChatChunk, ChatRequest}; + use anyhow::Result; + use eventsource_stream::Eventsource; ++use reqwest::header::HeaderMap; + // Only for the trait methods (`source()`) needed to walk a reqwest build + // failure's cause chain; kept anonymous so it cannot shadow the `Error` derive + // macro imported from `thiserror` below. +@@ -247,6 +248,9 @@ impl StreamedReply { + pub struct ChatClient { + backend_url: String, + auth_token: String, ++ /// Configured request headers. An explicit `Authorization` here suppresses ++ /// `auth_token`, so the header wins instead of producing two credentials. ++ headers: HeaderMap, + model: String, + initial_timeout_secs: u64, + stall_timeout_secs: u64, +@@ -689,6 +693,7 @@ impl ChatClient { + Self { + backend_url: cfg.backend_url.clone(), + auth_token: cfg.auth_token.clone(), ++ headers: crate::net::headers::resolve(&[&cfg.headers]), + model: cfg.model.clone(), + initial_timeout_secs: INITIAL_RESPONSE_WATCHDOG_SECS, + stall_timeout_secs: INTER_CHUNK_WATCHDOG_SECS, +@@ -702,6 +707,7 @@ impl ChatClient { + Self { + backend_url: backend_url.into(), + auth_token: String::new(), ++ headers: HeaderMap::new(), + model: model.into(), + initial_timeout_secs: INITIAL_RESPONSE_WATCHDOG_SECS, + stall_timeout_secs: INTER_CHUNK_WATCHDOG_SECS, +@@ -719,6 +725,7 @@ impl ChatClient { + Self { + backend_url: backend_url.into(), + auth_token: auth_token.into(), ++ headers: HeaderMap::new(), + model: model.into(), + initial_timeout_secs: INITIAL_RESPONSE_WATCHDOG_SECS, + stall_timeout_secs: INTER_CHUNK_WATCHDOG_SECS, +@@ -734,6 +741,12 @@ impl ChatClient { + self + } + ++ /// Attach request headers resolved from configuration. ++ pub fn with_headers(mut self, headers: HeaderMap) -> Self { ++ self.headers = headers; ++ self ++ } ++ + pub fn with_initial_timeout_secs(mut self, secs: u64) -> Self { + self.initial_timeout_secs = secs; + self +@@ -1167,7 +1180,11 @@ impl ChatClient { + let req_start = std::time::Instant::now(); + + let mut builder = client.post(&url).json(&req_body); +- if !self.auth_token.is_empty() { ++ for (name, value) in &self.headers { ++ builder = builder.header(name, value); ++ } ++ if !self.auth_token.is_empty() && !self.headers.contains_key(reqwest::header::AUTHORIZATION) ++ { + builder = builder.bearer_auth(&self.auth_token); + } + +diff --git a/src/llm/client_tests.rs b/src/llm/client_tests.rs +index 7b92e156e882..bed827d91f35 100644 +--- a/src/llm/client_tests.rs ++++ b/src/llm/client_tests.rs +@@ -1788,3 +1788,62 @@ fn test_construction_verdict_matches_the_startup_probe() { + } + } + } ++ ++/// Configured headers reach the wire through [`ChatClient::from_config`], and an ++/// explicit `Authorization` header suppresses `auth_token`. ++#[tokio::test] ++async fn test_configured_headers_reach_the_wire_and_explicit_authorization_wins() { ++ use crate::net::headers::{HeaderSpec, HeaderSpecs, generated_session_id}; ++ use wiremock::matchers::{method, path}; ++ use wiremock::{Mock, MockServer, Request, ResponseTemplate}; ++ ++ let server = MockServer::start().await; ++ let seen: Arc, Option)>>> = Arc::new(Mutex::new(None)); ++ let recorder = seen.clone(); ++ Mock::given(method("POST")) ++ .and(path("/chat/completions")) ++ .respond_with(move |req: &Request| { ++ let header = |name: &str| { ++ req.headers ++ .get(name) ++ .and_then(|value| value.to_str().ok()) ++ .map(str::to_string) ++ }; ++ *recorder.lock().expect("recorder lock") = ++ Some((header("x-opencode-session"), header("authorization"))); ++ ResponseTemplate::new(200).set_body_string(sse_body("ok")) ++ }) ++ .mount(&server) ++ .await; ++ ++ let headers: HeaderSpecs = [ ++ ("x-opencode-session".to_string(), HeaderSpec::Flag(true)), ++ ( ++ "Authorization".to_string(), ++ HeaderSpec::Text("Bearer explicit".to_string()), ++ ), ++ ] ++ .into_iter() ++ .collect(); ++ let cfg = crate::config::Config { ++ backend_url: server.uri(), ++ auth_token: "configured-token".to_string(), ++ headers, ++ model: "test-model".to_string(), ++ ..Default::default() ++ }; ++ ++ let reply = ChatClient::from_config(&cfg) ++ .chat(&request()) ++ .await ++ .unwrap(); ++ assert_eq!(reply.content, "ok"); ++ ++ let (session, authorization) = seen ++ .lock() ++ .expect("recorder lock") ++ .clone() ++ .expect("the request must have reached the mock"); ++ assert_eq!(session.as_deref(), Some(generated_session_id())); ++ assert_eq!(authorization.as_deref(), Some("Bearer explicit")); ++} +diff --git a/src/mcp/client.rs b/src/mcp/client.rs +index 222ab5872d61..81deb01e381c 100644 +--- a/src/mcp/client.rs ++++ b/src/mcp/client.rs +@@ -20,6 +20,7 @@ use super::http::{HttpSseConnection, MCP_REQUEST_TIMEOUT}; + use super::protocol::{ + self, JsonRpcNotification, JsonRpcRequest, JsonRpcTransport, RequestIds, decode_reply, + }; ++use crate::net::headers::HeaderSpecs; + + /// Server configuration entry for an MCP server in marmel.toml. + #[derive(Debug, Clone, Default, Serialize, Deserialize)] +@@ -33,6 +34,9 @@ pub struct McpServerConfig { + pub env: HashMap, + /// Remote URL endpoint (for HTTP/SSE transport). + pub url: Option, ++ /// Request headers for the HTTP/SSE transport. Ignored for stdio servers, ++ /// which use `env` instead. ++ pub headers: HeaderSpecs, + /// Optional prompt or usage instructions injected into the system prompt of + /// every agent allowed to use this MCP server. + #[serde(alias = "instructions", alias = "system_prompt")] +@@ -627,6 +631,11 @@ impl McpManager { + /// warned (unchanged); `Err(reason)` = the connection failed, with the reason in the + /// payload form the `Failed` status wants. + async fn boot_one(&self, name: &str, cfg: &McpServerConfig) -> Result { ++ if cfg.command.is_some() && !cfg.headers.is_empty() { ++ tracing::warn!( ++ "MCP server '{name}' uses stdio (`command`); `headers` are ignored — use `env`" ++ ); ++ } + let client = if cfg.command.is_some() { + match StdioMcpConnection::spawn(name, cfg).await { + Ok(conn) => Arc::new(McpClient::Stdio(Box::new(Mutex::new(conn)))), +diff --git a/src/mcp/http.rs b/src/mcp/http.rs +index 82f0288a8137..8681cc9ca58b 100644 +--- a/src/mcp/http.rs ++++ b/src/mcp/http.rs +@@ -23,9 +23,12 @@ + + use anyhow::{Context, Result, anyhow}; + use eventsource_stream::Eventsource; ++use reqwest::header::HeaderMap; + use serde_json::Value; + use std::time::{Duration, Instant}; + ++use crate::net::headers::HeaderSpecs; ++ + use super::client::{McpServerConfig, McpTool}; + use super::protocol::{ + self, JsonRpcNotification, JsonRpcRequest, JsonRpcTransport, RequestIds, decode_reply, +@@ -234,6 +237,8 @@ pub struct HttpSseConnection { + server_name: String, + endpoint: String, + client: reqwest::Client, ++ /// Configured request headers, resolved once at connect time. ++ headers: HeaderMap, + session_id: Option, + /// The protocol layer's monotonic JSON-RPC id allocator + /// ([`super::protocol::RequestIds`]); the transport only carries it. +@@ -355,7 +360,7 @@ impl HttpSseConnection { + .as_ref() + .ok_or_else(|| anyhow!("missing `url` for HTTP MCP server '{server_name}'"))?; + +- Self::establish(server_name, url, MCP_REQUEST_TIMEOUT).await ++ Self::establish(server_name, url, &cfg.headers, MCP_REQUEST_TIMEOUT).await + } + + /// Test seam: same handshake with an explicit request deadline, so the +@@ -367,12 +372,29 @@ impl HttpSseConnection { + url: &str, + request_timeout: Duration, + ) -> Result { +- Self::establish(server_name, url, request_timeout).await ++ let headers = HeaderSpecs::new(); ++ Self::establish(server_name, url, &headers, request_timeout).await ++ } ++ ++ /// Test seam with configured headers. ++ #[cfg(test)] ++ pub(crate) async fn connect_with_timeout_and_headers( ++ server_name: &str, ++ url: &str, ++ headers: &HeaderSpecs, ++ request_timeout: Duration, ++ ) -> Result { ++ Self::establish(server_name, url, headers, request_timeout).await + } + + /// Build the reqwest client through the shared builder and run the + /// `initialize` handshake through the shared protocol layer. +- async fn establish(server_name: &str, url: &str, request_timeout: Duration) -> Result { ++ async fn establish( ++ server_name: &str, ++ url: &str, ++ headers: &HeaderSpecs, ++ request_timeout: Duration, ++ ) -> Result { + let client = build_http_client_with(http_client_options(request_timeout)) + .with_context(|| format!("failed to build HTTP client for '{server_name}'"))?; + +@@ -380,6 +402,7 @@ impl HttpSseConnection { + server_name: server_name.to_string(), + endpoint: url.to_string(), + client, ++ headers: crate::net::headers::resolve(&[headers]), + session_id: None, + request_ids: RequestIds::new(), + request_timeout, +@@ -452,6 +475,9 @@ impl HttpSseConnection { + if let Some(sid) = &self.session_id { + builder = builder.header("Mcp-Session-Id", sid); + } ++ for (name, value) in &self.headers { ++ builder = builder.header(name, value); ++ } + builder + } + +diff --git a/src/mcp/http_tests.rs b/src/mcp/http_tests.rs +index 1ded90785992..16d1fdb52240 100644 +--- a/src/mcp/http_tests.rs ++++ b/src/mcp/http_tests.rs +@@ -372,6 +372,54 @@ mod tests { + assert_eq!(sent[2], JsonRpcRequest::tools_list(1).body().expect("body")); + } + ++ /// Configured headers travel on every HTTP request of the transport — the ++ /// `initialize` handshake, the notification and `tools/list` alike. ++ #[tokio::test] ++ async fn configured_headers_travel_on_every_http_request() { ++ use crate::net::headers::{HeaderSpec, HeaderSpecs}; ++ ++ let server = MockServer::start().await; ++ let (router, _all, _calls) = endpoint_router(EndpointBehaviour::JsonToolsList); ++ let seen: Arc>>> = Arc::new(Mutex::new(Vec::new())); ++ let recorder = seen.clone(); ++ Mock::given(method("POST")) ++ .and(path("/mcp")) ++ .respond_with(move |req: &Request| { ++ recorder.lock().expect("recorder lock").push( ++ req.headers ++ .get("x-api-key") ++ .and_then(|value| value.to_str().ok()) ++ .map(str::to_string), ++ ); ++ router(req) ++ }) ++ .mount(&server) ++ .await; ++ ++ let headers: HeaderSpecs = [( ++ "x-api-key".to_string(), ++ HeaderSpec::Text("secret".to_string()), ++ )] ++ .into_iter() ++ .collect(); ++ let mut conn = HttpSseConnection::connect_with_timeout_and_headers( ++ "mock-server", ++ &format!("{}/mcp", server.uri()), ++ &headers, ++ Duration::from_secs(5), ++ ) ++ .await ++ .expect("initialize handshake should succeed"); ++ conn.list_tools().await.expect("tools/list"); ++ ++ let seen = seen.lock().expect("recorder lock").clone(); ++ assert_eq!(seen.len(), 3, "initialize + notification + tools/list"); ++ assert!( ++ seen.iter().all(|value| value.as_deref() == Some("secret")), ++ "every request must carry the configured header, got {seen:?}" ++ ); ++ } ++ + /// A `tools/call` envelope carries the protocol layer's param shape. + #[tokio::test] + async fn http_call_tool_envelope_carries_the_protocol_params() { +diff --git a/src/net/headers.rs b/src/net/headers.rs +new file mode 100644 +index 000000000000..1f73b723ae74 +--- /dev/null ++++ b/src/net/headers.rs +@@ -0,0 +1,242 @@ ++//! Arbitrary request headers from configuration, shared by the LLM chat client ++//! and the MCP HTTP/SSE transport. ++//! ++//! A configured value is either a literal string or a flag: ++//! * `true` on [`SESSION_HEADER`] asks the process to mint one stable ++//! `ses_` for the whole run and send it on every request; ++//! * `false` removes a header inherited from a broader scope. ++//! ++//! Reserved transport headers are refused at config load, never silently ++//! overridden. ++ ++use std::collections::BTreeMap; ++use std::sync::OnceLock; ++ ++use reqwest::header::{HeaderMap, HeaderName, HeaderValue as HttpHeaderValue}; ++use serde::{Deserialize, Serialize}; ++ ++/// Stable per-run session header understood by the OpenCode Console gateway. ++pub const SESSION_HEADER: &str = "x-opencode-session"; ++ ++/// Header values accepted from TOML. ++pub type HeaderSpecs = BTreeMap; ++ ++/// One configured header value. ++#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] ++#[serde(untagged)] ++pub enum HeaderSpec { ++ /// A literal header value. ++ Text(String), ++ /// `true` generates the process session id; `false` removes the header. ++ Flag(bool), ++} ++ ++/// Headers the transports own and a user must not override. ++const RESERVED_HEADERS: &[&str] = &[ ++ "accept", ++ "connection", ++ "content-length", ++ "content-type", ++ "host", ++ "mcp-session-id", ++ "transfer-encoding", ++]; ++ ++/// The process-wide generated session id. ++/// ++/// Minted on first use, so every configured `true` value and every request ++/// shares one stable id for the life of the process. ++pub fn generated_session_id() -> &'static str { ++ static ID: OnceLock = OnceLock::new(); ++ ID.get_or_init(|| format!("ses_{}", uuid::Uuid::new_v4().simple())) ++} ++ ++/// `true` when `name` is a transport-owned header. ++pub fn is_reserved(name: &str) -> bool { ++ RESERVED_HEADERS ++ .iter() ++ .any(|reserved| reserved.eq_ignore_ascii_case(name)) ++} ++ ++/// `true` when `specs` sets `name` to anything other than an explicit removal. ++pub fn sets_header(specs: &HeaderSpecs, name: &str) -> bool { ++ specs.iter().any(|(key, spec)| { ++ key.eq_ignore_ascii_case(name) && !matches!(spec, HeaderSpec::Flag(false)) ++ }) ++} ++ ++/// Validate one configuration scope, naming it in every error. ++pub fn validate(specs: &HeaderSpecs, context: &str) -> Result<(), String> { ++ for (name, spec) in specs { ++ let header_name = HeaderName::from_bytes(name.as_bytes()) ++ .map_err(|err| format!("{context}: invalid header name {name:?}: {err}"))?; ++ if is_reserved(header_name.as_str()) { ++ return Err(format!( ++ "{context}: header {name:?} is managed by the transport and cannot be overridden" ++ )); ++ } ++ match spec { ++ HeaderSpec::Text(value) => { ++ value.parse::().map_err(|err| { ++ format!("{context}: invalid value for header {name:?}: {err}") ++ })?; ++ } ++ HeaderSpec::Flag(true) if header_name.as_str() != SESSION_HEADER => { ++ return Err(format!( ++ "{context}: `true` is only supported for `{SESSION_HEADER}`; use a string for {name:?}" ++ )); ++ } ++ HeaderSpec::Flag(_) => {} ++ } ++ } ++ Ok(()) ++} ++ ++/// Merge scopes left-to-right and resolve each value into a concrete header. ++/// ++/// Later scopes win per header name; `false` removes a header inherited from an ++/// earlier scope. Invalid entries are skipped with a warning: normal startup ++/// rejects them through [`validate`], and direct `Config` construction (tests) ++/// must not panic. ++pub fn resolve(layers: &[&HeaderSpecs]) -> HeaderMap { ++ let mut map = HeaderMap::new(); ++ for layer in layers { ++ for (name, spec) in *layer { ++ let Ok(header_name) = HeaderName::from_bytes(name.as_bytes()) else { ++ tracing::warn!("ignoring invalid configured header name {name:?}"); ++ continue; ++ }; ++ if is_reserved(header_name.as_str()) { ++ tracing::warn!( ++ "ignoring configured header {name:?}: it is managed by the transport" ++ ); ++ continue; ++ } ++ match spec { ++ HeaderSpec::Text(value) => match value.parse::() { ++ Ok(value) => { ++ map.insert(header_name, value); ++ } ++ Err(err) => { ++ tracing::warn!( ++ "ignoring invalid value for configured header {name:?}: {err}" ++ ); ++ } ++ }, ++ HeaderSpec::Flag(true) if header_name.as_str() == SESSION_HEADER => { ++ if let Ok(value) = generated_session_id().parse::() { ++ map.insert(header_name, value); ++ } ++ } ++ HeaderSpec::Flag(true) => { ++ tracing::warn!( ++ "ignoring `true` for configured header {name:?}: only `{SESSION_HEADER}` supports it" ++ ); ++ } ++ HeaderSpec::Flag(false) => { ++ map.remove(&header_name); ++ } ++ } ++ } ++ } ++ map ++} ++ ++#[cfg(test)] ++mod tests { ++ use super::*; ++ ++ fn specs(pairs: &[(&str, HeaderSpec)]) -> HeaderSpecs { ++ pairs ++ .iter() ++ .map(|(name, spec)| ((*name).to_string(), spec.clone())) ++ .collect() ++ } ++ ++ fn value<'a>(headers: &'a HeaderMap, name: &str) -> &'a str { ++ headers.get(name).expect("header present").to_str().unwrap() ++ } ++ ++ #[test] ++ fn later_scopes_override_and_false_removes() { ++ let base = specs(&[ ++ ("x-keep", HeaderSpec::Text("one".into())), ++ ("x-replace", HeaderSpec::Text("old".into())), ++ ("x-remove", HeaderSpec::Text("gone".into())), ++ ]); ++ let over = specs(&[ ++ ("x-replace", HeaderSpec::Text("new".into())), ++ ("x-remove", HeaderSpec::Flag(false)), ++ ]); ++ ++ let headers = resolve(&[&base, &over]); ++ assert_eq!(value(&headers, "x-keep"), "one"); ++ assert_eq!(value(&headers, "x-replace"), "new"); ++ assert!(headers.get("x-remove").is_none()); ++ } ++ ++ #[test] ++ fn session_flag_resolves_to_the_process_id() { ++ let headers = resolve(&[&specs(&[(SESSION_HEADER, HeaderSpec::Flag(true))])]); ++ assert_eq!(value(&headers, SESSION_HEADER), generated_session_id()); ++ assert!(generated_session_id().starts_with("ses_")); ++ assert_eq!(generated_session_id(), generated_session_id()); ++ } ++ ++ #[test] ++ fn validate_accepts_text_flags_removals_and_empty_values() { ++ let valid = specs(&[ ++ (SESSION_HEADER, HeaderSpec::Flag(true)), ++ ("x-api-key", HeaderSpec::Text("secret".into())), ++ ("x-empty", HeaderSpec::Text(String::new())), ++ ("x-remove", HeaderSpec::Flag(false)), ++ ]); ++ assert!(validate(&valid, "test headers").is_ok()); ++ } ++ ++ #[test] ++ fn validate_rejects_reserved_names_and_bool_misuse() { ++ let reserved = specs(&[("Content-Type", HeaderSpec::Text("text/plain".into()))]); ++ assert!(validate(&reserved, "test headers").is_err()); ++ ++ let wrong_flag = specs(&[("x-api-key", HeaderSpec::Flag(true))]); ++ assert!(validate(&wrong_flag, "test headers").is_err()); ++ ++ let bad_name = specs(&[("bad header", HeaderSpec::Text("x".into()))]); ++ assert!(validate(&bad_name, "test headers").is_err()); ++ } ++ ++ #[test] ++ fn empty_literal_is_a_value_not_a_removal() { ++ let base = specs(&[("x-empty", HeaderSpec::Text("inherited".into()))]); ++ let over = specs(&[("x-empty", HeaderSpec::Text(String::new()))]); ++ ++ let headers = resolve(&[&base, &over]); ++ assert_eq!(value(&headers, "x-empty"), ""); ++ } ++ ++ /// The startup gate rejects reserved names; a directly constructed `Config` ++ /// skips that gate, so resolution must warn and skip them instead of ++ /// emitting a transport-owned header. ++ #[test] ++ fn reserved_headers_are_skipped_during_resolution() { ++ let headers = resolve(&[&specs(&[ ++ ("Content-Type", HeaderSpec::Text("text/plain".into())), ++ ("Mcp-Session-Id", HeaderSpec::Text("fixed".into())), ++ ("x-ok", HeaderSpec::Text("yes".into())), ++ ])]); ++ ++ assert!(headers.get("content-type").is_none()); ++ assert!(headers.get("mcp-session-id").is_none()); ++ assert_eq!(value(&headers, "x-ok"), "yes"); ++ } ++ ++ #[test] ++ fn sets_header_ignores_explicit_removal() { ++ let removal = specs(&[("Authorization", HeaderSpec::Flag(false))]); ++ assert!(!sets_header(&removal, "authorization")); ++ ++ let set = specs(&[("Authorization", HeaderSpec::Text("Bearer x".into()))]); ++ assert!(sets_header(&set, "authorization")); ++ } ++} +diff --git a/src/net/mod.rs b/src/net/mod.rs +index 2b9d66001235..74e895dda87b 100644 +--- a/src/net/mod.rs ++++ b/src/net/mod.rs +@@ -4,6 +4,7 @@ + //! hook), the generic retry-with-backoff policy, and the reqwest client + //! builder. + ++pub mod headers; + pub mod http; + pub mod retry; + pub mod sse; +diff --git a/src/orchestrator/delegate.rs b/src/orchestrator/delegate.rs +index 97746036c4eb..e5950813a8d8 100644 +--- a/src/orchestrator/delegate.rs ++++ b/src/orchestrator/delegate.rs +@@ -106,7 +106,7 @@ pub fn handle_delegate_task(args: &serde_json::Value) -> Result std::sync::Arc std::sync::Arc { + args: Vec::new(), + env: std::collections::HashMap::new(), + url: None, ++ headers: Default::default(), + prompt: None, + }, + ); +@@ -5306,6 +5307,7 @@ fn the_configured_server_count_is_seeded_from_the_config_once() { + args: Vec::new(), + env: std::collections::HashMap::new(), + url: None, ++ headers: Default::default(), + prompt: None, + }, + ); +-- +2.54.0 + diff --git a/packages/marmel/package.nix b/packages/marmel/package.nix index a1971f2..4f3666b 100644 --- a/packages/marmel/package.nix +++ b/packages/marmel/package.nix @@ -22,13 +22,10 @@ rustPlatform.buildRustPackage rec { cargoHash = "sha256-Xw9OT8HXVlnZ0XqIMnnFwQ/y9xKxOtmcwomJsRxUx5E="; - nativeCheckInputs = [ cacert ]; + # arbitrary HTTP headers for LLM and MCP requests. + patches = [ ./arbitrary-http-headers.patch ]; - # Both downstream patches are gone as of b08923eaef60 ("Landlock improvements", - # Na1w/marmel#17) and 62cb75634c6e ("report missing TLS trust store instead of - # panicking", #16): upstream now lists /nix/store and /gnu/store in - # BUILTIN_READONLY_PATHS, and the HTTP client reports a missing trust store - # instead of panicking through reqwest::Client::default(). + nativeCheckInputs = [ cacert ]; # The test suite exercises real HTTP round-trips against loopback servers and # builds its `reqwest::Client`s through rustls-native-certs, which probes only