// // HermesConfigRail.swift // Museion // // #182: the ONE owner of the `hermes -p config set/unset` CLI // contract — argv shape, provider-first ordering (switching providers // clears stale inherited values — live-probed), and ✓-line verification. // The provisioner (seeds provider/default/max_tokens) and the settings // writer (edits provider/default/effort/base_url) hand the rail their own // key lists; the mechanism and ordering rule live only here (pinned once // in HermesConfigRailTests over the shared fake-hermes double). // // Secrets never ride this rail (`ps` reads live command lines) — it only // ever carries provider/model/effort/base_url values. // import Foundation nonisolated enum HermesConfigRail { /// One CLI write: a set (key/value, `--force` for keys some engine /// versions don't recognize), or an unset. enum Write: Sendable { case set(key: String, value: String, force: Bool) case unset(key: String) } /// A wire-verified write that failed — structured so each caller keeps /// its own error surface (the provisioner's Outcome excerpt; the /// writer's `lastWriteError` text). struct Failure: Error, Sendable, LocalizedError { let verb: String let key: String let profile: String let excerpt: String var errorDescription: String? { "couldn't \(verb) \(key) on \(profile): \(excerpt)" } } /// Executes the ops provider-first, each wire-verified: exit 0 AND the /// `✓ Set ` / `✓ Unset ` line on stdout — a CLI that exits 0 /// while writing nothing must not count as configured. An unset of an /// absent key is idempotent success (exit 1 + the not-set line). static func apply(_ writes: [Write], profile: String, binary: String, timeout: TimeInterval) throws { // Provider first, relative order otherwise preserved: `config set // model.provider` clears a stale inherited `model.base_url`, so // everything after it sees the new provider. let ordered = writes.filter(isProviderSet) + writes.filter { !isProviderSet($0) } for write in ordered { switch write { case let .set(key, value, force): // Trust boundary: free-text fields (model IDs, endpoints) // feed this rail, and a leading `-` is an option to the // child CLI, never a value — no legitimate value starts // with a dash (learning #6's class: gate at the seam). guard !value.hasPrefix("-") else { throw Failure(verb: "set", key: key, profile: profile, excerpt: "value starts with '-' — not written") } var arguments = [binary, "-p", profile, "config", "set", key, value] if force { arguments.append("--force") } let run = EngineProbe.execute(arguments, timeout: timeout) guard run.exitCode == 0, run.stdout.contains("✓ Set \(key)") else { throw Failure(verb: "set", key: key, profile: profile, excerpt: excerpt(run)) } case let .unset(key): let run = EngineProbe.execute([binary, "-p", profile, "config", "unset", key], timeout: timeout) let output = run.stdout + run.stderr guard run.stdout.contains("✓ Unset \(key)") || output.contains("Config key not set: \(key)") else { throw Failure(verb: "unset", key: key, profile: profile, excerpt: excerpt(run)) } } } } private static func isProviderSet(_ write: Write) -> Bool { if case let .set(key, _, _) = write { key == "model.provider" } else { false } } /// Errors carry CLI output only — key-bearing text never reaches error /// surfaces. #180 audit: the rail absorbed the two callers' excerpts and /// keeps the provisioner's 500-char cap (the more-lossless twin; the /// writer's old 300 was the outlier), a deliberate divergence. static func excerpt(_ run: EngineProbe.ProcessOutput) -> String { let text = run.stdout.isEmpty ? run.stderr : run.stdout return String(text.trimmingCharacters(in: .whitespacesAndNewlines).prefix(500)) } }