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

+61
View File
@@ -220,6 +220,32 @@ private struct ProviderKeyBlock: View {
.font(.system(size: 11.5))
.foregroundStyle(DesignTokens.muted)
}
// #136: Test Connection (factsheet §4) — /models count + a
// minimal streaming chat; latency + model count on success,
// the honest decoded verdict otherwise.
HStack(spacing: 8) {
Button("Test Connection") {
Task { await model.testConnection(for: provider) }
}
.buttonStyle(.bordered)
.controlSize(.small)
.font(.system(size: 11, design: DesignTokens.mono))
.disabled(model.testStates[provider] == .running)
switch model.testStates[provider] {
case .running:
HStack(spacing: 4) {
ProgressView()
.controlSize(.small)
Text("testing…")
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
}
case let .done(outcome):
TestConnectionResult(outcome: outcome)
case nil:
EmptyView()
}
}
}
.padding(EdgeInsets(top: 14, leading: 16, bottom: 14, trailing: 16))
.background(
@@ -233,6 +259,41 @@ private struct ProviderKeyBlock: View {
}
}
/// The probe's one-line verdict (#136): works carries latency + model
/// count; every failure states its decoded reason (the AC's
/// wrong-key-type vs unreachable vs works distinction).
private struct TestConnectionResult: View {
let outcome: ProviderProbe.Outcome
var body: some View {
switch outcome {
case let .works(latencyMS, modelCount):
Text("✓ works · \(latencyMS) ms · \(modelCount) models")
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.accent)
case let .authFailed(detail):
text("key rejected — \(detail)")
case let .wrongKeyType(detail):
text("wrong key type — \(detail)")
case let .unreachable(detail):
text("unreachable — \(detail)")
case let .starved(detail):
text("token cap starved — \(detail)")
case let .requestFailed(status, detail):
text("HTTP \(status) — \(detail)")
}
}
private func text(_ content: String) -> some View {
Text(content)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(.orange)
.lineLimit(2)
.truncationMode(.middle)
.help(content)
}
}
/// The z.ai key-type toggle (prototype `.seg`): a two-segment capsule, the
/// selected segment filled with the foreground ink. Toggling swaps the
/// endpoint family (the model guards a custom endpoint).