CI / ci (push) Canceled after 0s
#182: ProfileLayout owns the profile root default and every per-profile path (dir, .env, configured marker, gateway pid, skills dir) — the five scattered profile-root literals are gone; the provisioner, the settings writer, and backup/restore derive paths only through it, pinned once in ProfileLayoutTests. RESTORE.md prose and the ps-scan grep keep their literal text deliberately (doc text and argv matching, not paths). HermesConfigRail owns the config set/unset contract once: argv shape (+--force), provider-first hoisting (switching providers clears a stale inherited base_url — a caller can no longer order it wrong), ✓-line verification, unset-idempotence. The provisioner seeds provider/default/ max_tokens; the writer edits provider/default/effort/base_url — each keeps its own key list, only the mechanism and ordering rule unify. The rail's Failure carries key/profile/excerpt for both error surfaces. Audit hardenings folded in: a leading-dash value refuses before any spawn (free-text model/endpoint fields cannot reach the child CLI as options), whitespace-only endpoints read as empty at both writer gates, the deliberate 500-char excerpt cap is pinned not just commented, and the failed-unset branch is pinned (the fake hermes grew a config-unset-fails knob). Full suite 684 passed / 0 failed; dash + whitespace pins flip-verified. Closes #182
95 lines
3.8 KiB
Swift
95 lines
3.8 KiB
Swift
//
|
|
// HermesConfigRail.swift
|
|
// Museion
|
|
//
|
|
// #182: the ONE owner of the `hermes -p <profile> 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 <key>` / `✓ Unset <key>` 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))
|
|
}
|
|
}
|