# Issue #136 — Profiles S3: Settings → profile write path + Test Connection > **Date:** 2026-10-27 > **Type:** issue > **Reference:** https://git.babylondreams.de/AlexKucera/Museion/issues/136 ## Goal Land the real write path behind the #135 Settings UI: model/effort changes persist into each Hermes profile's `model:` block, provider keys persist on disk, and a Test Connection button proves a key × endpoint × model triple against the live provider. Then make it survive dogfood. ## What Was Done - `HermesSettingsWriter` — the production `SettingsWriting`: surface writes ride the engine's own CLI (`hermes -p config set/unset model.provider|model.default|model.reasoning_effort|model.base_url`, wire-verified ✓ lines), provider keys upsert `VAR=value` into the profile `.env` files, `readSurface` hydrates via `config get` ×4, `readProviderKey` reads keys back so a restart refills the fields. Secrets never ride argv; `.env` written at 0600. - `ProviderProbe` — Test Connection per the factsheet §4: step 1 `GET /models` (key validity + reachability), step 2 a minimal streaming chat. Injected `Transport` closure, nil-safe URL builders, honest error decode (zai `error.code` 1113 → wrongKeyType, 401s → authFailed with Anthropic `request_id`, finish markers → starved, empty 200 → requestFailed). - `SettingsModel` grew `hydrate()` (parallel per-surface reads + key read-back), `testConnection(for:)`, per-target coalescing writes, `probeModel(for:)`; `SettingsPaneView` takes the gate's `binaryPath` and hydrates at pane open. - Six test files: decode matrix, transport/wire-shape pins (Content-Type per provider, OpenAI Responses dialect), writer round-trips against the fake-hermes CLI rail, model-side write-path pins. - Live dogfood (3 rounds) + finish-audit (3 parallel reviewers) — see Gotchas. ## Decisions & Rationale | Decision | Rationale | |----------|-----------| | Keys land in ALL THREE profiles' `.env`, not just matching surfaces | Maintainer ruling mid-session: a key entered before any surface is on its provider must persist (it died on restart otherwise); inert vars are ignored by hermes for other providers; the key pre-lands for any later provider switch, so the switch needs no key re-fire — that block was deleted. Per-provider gating survives only for the z.ai `base_url` write-through (a zai endpoint in an anthropic profile would be wrong). | | OpenAI speaks the Responses API (`/responses`, `input` + `max_output_tokens`, flagship `gpt-6.1-sol`) | Maintainer correction over the factsheet draft; chat-completions is the retired path. Wire pinned with the old forms asserted absent. | | `Content-Type: application/json` on every probe request | 2026-10-28 live incident: URLSession defaults to form-urlencoded without it; z.ai answers HTTP 200 with an error envelope, OpenAI 400s. curl set the header; the app didn't. Anthropic tolerated the omission — which is why it worked first. | | Probe token cap 512 (was 64) | 64 always starved glm-5.3 reasoning (reasoning_tokens 64/64, finish=length, zero content) — decoded as "HTTP 200 - no content". | | Writes chain past the debounce guard (`await previous?.value`) | Cancellation can't stop a past-the-guard CLI write (detached rail ignores it); without the chain a stale write could land after its replacement. Audit fix, converged by two reviewers. | | `.env` newline/NUL guard at the shared sink | A pasted `key\nOTHER_VAR=x` is an env-file injection into every profile (clipboard-hijack primitive). One guard in `upsertEnvFile`; refusal rides `lastWriteError`. | | `NSLock` around the `.env` read→upsert→write | Cross-provider key writes race on the same three files; atomic-write prevents torn files but not lost updates. | | Hydrate skips targets with a pending write task; custom zai base_urls seed the endpoint field | Edits made during hydrate's seconds of CLI reads owned the truth; only the coding family maps back to the key type — any other non-empty zai base_url must survive, or the next write-through clobbers it to a family default. | | `works` carries latency + model count only; `keyType` protocol param deleted | Finish-audit ponytail: no consumer of the reply payload or the keyType (the endpoint field already carries the family). ~30 lines of dead flexibility cut. | | No Keychain | Issue text: keys live in hermes' own `.env` mechanism (ADR-0014 posture). | ## Gotchas & Fixes | Problem | Root Cause | Fix | |---------|------------|-----| | z.ai "HTTP 200 - no content", openai HTTP 400, anthropic ✓ | `makeChatRequest` never set `Content-Type: application/json` — URLSession defaults to form-urlencoded; providers diverge in tolerance | Header pinned on every request + per-provider wire tests | | OpenAI "HTTP 200 - HTTP 200" on healthy streams | OpenAI Responses snapshots carry `"error": null` on every event; JSON null ≠ nil tripped `envelope["error"] != nil` | `isError` checks `.object` case only; regression pin with null-error events | | Keys vanished on restart | `hydrate()` never read keys back; keys also only fanned out to matching surfaces (none → nowhere) | `readProviderKey` + all-three-`.env` landing + hydrate refill | | Suite "pass" with 0 tests | `-only-testing` filters match nothing for free-function Swift Testing tests (again — #31/#134) | Always read `totalTestCount` from xcresult | | Two full-run failures | PassRunTests #30 flake class under load | Idle rerun 464/464, then 468/468 with the audit pins | | Flip-cycle restore silently no-oped | python replace anchored on text whose comment lines had moved; no match ≠ applied | Assert `count == 1` before every scripted replace; caught because a pin kept failing "restored" | | xcresult sampled mid-run | Background suite still executing when I read the latest xcresult | Wait for the DONE marker before sampling | ## Files Changed | File | Change Summary | |------|---------------| | `Museion/HermesSettingsWriter.swift` | NEW — production SettingsWriting (CLI rail, .env fan-out, reads, injection guard, lock) | | `Museion/ProviderProbe.swift` | NEW — two-step Test Connection probe + honest decode | | `Museion/SettingsModel.swift` | hydrate/testConnection/coalescing+chaining writes, ZaiKeyType, ModelProvider | | `Museion/SettingsPagesView.swift` | Test Connection button + verdict row | | `Museion/SettingsPaneView.swift` | binaryPath wiring + hydrate on open | | `Museion/ModelComboBox.swift` | ModelRegistry moved in (file-length split) | | `Museion/WorkspaceView.swift` | passes the gate's binary path | | `MuseionTests/` (6 files) | writer/probe/model suites + RecordingWriter knobs | | `MuseionTests/ProfileProvisionerTests.swift` | fake hermes: config get/set/unset rail | | `docs/research/native-llm-providers.md` | §6 correction 2026-10-28 | | `Museion/AGENTS.md`, `MuseionTests/AGENTS.md` | DOX through the audit fixes | ## Open Items & Next Steps - [ ] Non-zai endpoint persistence — anthropic/openai endpoints are session-only; needs a product call (ticket-worthy) - [ ] Plaintext `http://` non-localhost endpoints send the key in cleartext — warn at probe time or field render - [ ] Hydrate costs 12 CLI spawns per pane visit — a multi-key `config get` would cut it if it ever bothers - [ ] Restart-on-change slice (SettingsModel → AppGateState lift) — the pane-local model is deliberately scoped --- *Log written by write-log skill*