feat(settings): profiles write path + Test Connection (#136)

Profiles S3 over the S2 shell: every Settings change now persists.

Model/effort writes ride the engine's own CLI — `hermes -p <profile>
config set/unset` for model.provider / model.default /
model.reasoning_effort / model.base_url (zai endpoint family written
LAST: setting model.provider clears a stale base_url, live-probed;
unset-when-absent is idempotent success). Provider keys upsert
VAR=value into ALL THREE profiles' .env — a key entered before any
surface is on its provider still persists and pre-lands for any later
switch (maintainer ruling 2026-10-28; no switch re-fire). 0600,
foreign lines preserved byte-for-byte, line-broken values refused at
the shared sink (env-file injection guard), NSLock serializes the
read-upsert-write across concurrent provider writes.

Hydration reads model blocks and keys back at pane open — a restart
neither blanks the key fields nor refuses Test Connection; an edit
made mid-read owns the truth (no stomp), and a custom z.ai base_url
seeds the endpoint field instead of being clobbered to a family
default. Per-target writes coalesce AND chain past the debounce
guard: cancellation can't stop a past-the-guard CLI write, so the
newer write queues — the last edit is always the last write to land
(finish-audit race fix, pinned).

Test Connection = two-step probe (GET /models, then a minimal
streaming chat) with honest decode per the factsheet: zai 1113 →
wrong key type, 401s → auth failed (Anthropic request_id),
finish=length → starved, empty 200 → request failed, errors riding a
2xx decoded as the provider error.

Live-probed wire corrections (dogfood, 2026-10-28): Content-Type
application/json on every request — URLSession defaults to
form-urlencoded and providers diverge (zai 200+envelope, OpenAI 400,
Anthropic tolerates); OpenAI speaks the Responses API (/responses,
input + max_output_tokens, flagship gpt-6.1-sol); probe token cap 512
(64 starved reasoning models into "no content"); OpenAI's
"error": null event snapshots don't decode as failures.

Finish-audit (ponytail + security + general reviewers): write-ordering
race + .env lost update + hydrate stomp + custom-endpoint clobber
fixed and pinned (one-shot hold / latched read knobs in the test
double); ~30 lines of dead flexibility cut (keyType protocol param,
works(reply:) payload, unused timeout knob, double JSON round trips).

Suite 468/468 xcresult-verified; swiftlint clean; factsheet §6
correction logged. Closes #136
This commit is contained in:
AlexKucera committed 2026-10-27 18:32:30 +01:00
1 parent 42a27cc8a3
commit 5ad605eb3c
20 files changed
+1983 -161

No files matched your search

+43
View File
@@ -10,8 +10,51 @@
//
import AppKit
import Foundation
import SwiftUI
/// Hermes' own picker cache (`~/.hermes/provider_models_cache.json`) — zero
/// HTTP, exactly what Hermes accepts (ADR-0014). Lenient by design: a
/// missing, unreadable, or foreign-shaped file leaves every model free-text
/// (all custom), which is the picker's first-class mode anyway.
nonisolated struct ModelRegistry: Sendable {
var models: [ModelProvider: [String]] = [:]
func list(for provider: ModelProvider) -> [String] {
models[provider] ?? []
}
/// Cache shape: `{provider: {fp, at, models: [ids]}}`. Lenient per the
/// DECODE, not per entry: unknown provider slugs are skipped, but any
/// foreign shape inside a known provider's entry (a non-string models
/// element) fails the whole decode and the registry degrades to
/// free-text-only — the picker's first-class mode anyway.
static func load(path: String = ("~/.hermes/provider_models_cache.json" as NSString).expandingTildeInPath) -> ModelRegistry {
guard let data = try? Data(contentsOf: URL(fileURLWithPath: path)) else {
return ModelRegistry()
}
struct Entry: Decodable {
let models: [String]?
}
guard let parsed = try? JSONDecoder().decode([String: Entry].self, from: data) else {
return ModelRegistry()
}
var registry = ModelRegistry()
for (slug, entry) in parsed {
if let provider = ModelProvider(rawValue: slug) {
registry.models[provider] = entry.models ?? []
}
}
return registry
}
/// The default load, memoized — `SettingsModel()` runs its default
/// arguments on every SwiftUI construction of the pane, so the disk read
/// + decode must happen once. ponytail: refreshed on next launch, not
/// live.
static let loadedDefault = ModelRegistry.load()
}
struct ModelComboBox: NSViewRepresentable {
/// The committed model id (the model's truth; the box echoes it).
var value: String