refactor(profiles): one layout + config-set rail
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
This commit is contained in:
AlexKucera committed 2026-11-10 11:32:47 +01:00
1 parent f49e4b6ca0
commit a8b158ca26
14 files changed
+484 -113

No files matched your search

+1
View File
@@ -32,6 +32,7 @@ Conventions every suite follows. Per-suite pin inventories are NOT kept here —
- `ACPClientTests` — turn-outcome seam + view predicates over the scripted engine (#178: the client wire contract itself lives in EngineSmokeTests; `makeFakeACP`/`acpTempDir` remain here as the shared wire fixture).
- `ScriptedEngine.swift` — #178: the scripted in-process `EngineTransport` adapter, shared payloads, and the scripted rig roots every engine suite builds on.
- `EngineSmokeTests` — #178: the ONLY child-spawning suite — one real-process end-to-end wire smoke per LLM Surface (chat on `museion`: handshake/stream/verdict/crash/restart/teardown; pass1/pass2: own engine under its profile, teardown reap, chat engine untouched).
- `ProfileLayoutTests`, `HermesConfigRailTests` — #182: the layout's one pinning suite (root default + every per-profile path builder against literals) and the config-set rail's ONE contract suite (argv+`--force`, provider-first hoisting, ✓-line verification, unset idempotence, structured Failure) — both over the shared fake-hermes double; callers' suites pin only their own key lists.
- `ProfileProvisionerTests`, `ProfileSurfaceTests`, `ProfileSkillMigrationTests`, `ProfileProvisionerFailureTests`, `ProfileBackupTests` — provisioning, symlink migration, failure paths, backup (one fake-`hermes` harness).
- `PostLibraryTests`, `PostStoreTests`, `PostStorePassTests`, `PostFrontmatterTests` — scan, write side, pass substrate, stats capture, research notes, the frontmatter codec (#180: posture round-trips, lenient decode, named eligibility predicates).
- `DraftStatsTests`, `TrendViewTests` — text metrics and the trend view's pure facts.
+139
View File
@@ -0,0 +1,139 @@
//
// HermesConfigRailTests.swift
// MuseionTests
//
// #182 — the config-set CLI contract's ONE pinning suite, over the shared
// fake-hermes double (makeFakeHermes, ProfileProvisionerTests.swift). The
// callers' suites pin only their own key lists.
//
// Scenario: The config-set wire is owned once — From user story #182
// Given the provisioner and the settings writer both write config keys
// through `hermes -p <profile> config set`
// When either hands the rail its key list
// Then every write rides the exact argv (with --force where asked), is
// verified against the `✓ Set` line (not exit code alone), executes
// provider-first (switching providers clears stale inherited values),
// and a failed write surfaces key + profile + CLI excerpt.
//
import Foundation
@testable import Museion
import Testing
@Suite("HermesConfigRailTests")
struct HermesConfigRailTests {
@Test func `set argv rides the exact wire with force`() throws {
let binDir = provTempDir("rail")
let binary = makeFakeHermes(at: binDir, mode: "ok")
try HermesConfigRail.apply(
[
.set(key: "model.provider", value: "anthropic", force: false),
.set(key: "model.extra_body.max_tokens", value: "24000", force: true),
],
profile: "museion-pass1",
binary: binary,
timeout: 5,
)
#expect(fakeHermesLog(binDir, "config-set-museion-pass1") == [
"-p museion-pass1 config set model.provider anthropic",
"-p museion-pass1 config set model.extra_body.max_tokens 24000 --force",
])
}
@Test func `provider goes first whatever the caller s list order`() throws {
// model.provider clears a stale inherited base_url — the rail owns
// the rule; a caller can't order it wrong. Relative order holds.
let binDir = provTempDir("rail-order")
let binary = makeFakeHermes(at: binDir, mode: "ok")
try HermesConfigRail.apply(
[
.set(key: "model.base_url", value: "https://x", force: false),
.unset(key: "model.reasoning_effort"),
.set(key: "model.default", value: "m", force: false),
.set(key: "model.provider", value: "zai", force: false),
],
profile: "museion",
binary: binary,
timeout: 5,
)
#expect(fakeHermesLog(binDir, "config-set-museion") == [
"-p museion config set model.provider zai",
"-p museion config set model.base_url https://x",
"-p museion config set model.default m",
])
#expect(fakeHermesLog(binDir, "config-unset-museion") == [
"-p museion config unset model.reasoning_effort",
])
}
@Test func `unset of an absent key is idempotent success`() throws {
let binDir = provTempDir("rail-unset")
let binary = makeFakeHermes(at: binDir, mode: "ok")
try HermesConfigRail.apply([.unset(key: "model.reasoning_effort")], profile: "museion", binary: binary, timeout: 5)
}
@Test func `a failed set throws naming key and profile with the cli excerpt`() {
let binDir = provTempDir("rail-fail")
try? "".write(toFile: binDir + "/config-set-fails", atomically: true, encoding: .utf8)
let binary = makeFakeHermes(at: binDir, mode: "ok")
do {
try HermesConfigRail.apply(
[.set(key: "model.default", value: "m", force: false)],
profile: "museion-pass2",
binary: binary,
timeout: 5,
)
Issue.record("expected a throw")
} catch {
let failure = error as? HermesConfigRail.Failure
#expect(failure?.key == "model.default")
#expect(failure?.profile == "museion-pass2")
#expect(failure?.excerpt == "Error: config set exploded")
#expect(failure?.errorDescription == "couldn't set model.default on museion-pass2: Error: config set exploded")
}
}
@Test func `a failed unset throws the structured failure`() {
// The fourth branch: neither ✓ Unset nor the not-set line.
let binDir = provTempDir("rail-unset-fail")
try? "".write(toFile: binDir + "/config-unset-fails", atomically: true, encoding: .utf8)
let binary = makeFakeHermes(at: binDir, mode: "ok")
do {
try HermesConfigRail.apply([.unset(key: "model.reasoning_effort")], profile: "museion", binary: binary, timeout: 5)
Issue.record("expected a throw")
} catch {
let failure = error as? HermesConfigRail.Failure
#expect(failure?.verb == "unset")
#expect(failure?.key == "model.reasoning_effort")
#expect(failure?.excerpt == "Error: config unset exploded")
}
}
@Test func `a value that looks like an option is refused before any spawn`() {
// Free-text fields (model IDs, endpoints) feed the rail; a leading
// `-` is an option to the child CLI, never a value.
let binDir = provTempDir("rail-dash")
let binary = makeFakeHermes(at: binDir, mode: "ok")
do {
try HermesConfigRail.apply(
[.set(key: "model.default", value: "--force", force: false)],
profile: "museion",
binary: binary,
timeout: 5,
)
Issue.record("expected a throw")
} catch {
let failure = error as? HermesConfigRail.Failure
#expect(failure?.key == "model.default")
#expect(failure?.excerpt == "value starts with '-' — not written")
}
#expect(fakeHermesLog(binDir, "config-set-museion").isEmpty) // nothing reached the CLI
}
@Test func `excerpt caps at 500 chars`() {
// The #180 deliberate divergence (writer's old 300 is gone): pinned,
// not just commented.
let output = EngineProbe.ProcessOutput(exitCode: 1, stdout: String(repeating: "x", count: 900))
#expect(HermesConfigRail.excerpt(output).count == 500)
}
}
@@ -173,6 +173,30 @@ struct HermesSettingsWriterTests {
}
}
@Test func `whitespace-only endpoint is treated as empty at both gates`() async throws {
// Learning #6's class: raw isEmpty passes whitespace through; the
// gates mirror the trimmed emptiness the model side validates.
let (binDir, writer) = makeRig()
// Surface gate: a blank endpoint never rides base_url.
try await writer.writeSurface(
.pass1,
config: SurfaceModelConfig(provider: .zai, model: "glm-5.3", effort: nil),
zaiEndpoint: " ",
)
#expect(fakeHermesLog(binDir, "config-set-museion-pass1") == [
"-p museion-pass1 config set model.provider zai",
"-p museion-pass1 config set model.default glm-5.3",
])
// Key gate: a blank zai endpoint skips the write-through (the key
// still fans out to .env).
try await writer.writeProviderKey(
provider: .zai,
config: ProviderKeyConfig(endpoint: " ", apiKey: "sk-z"),
profiles: [.chat],
)
#expect(fakeHermesLog(binDir, "config-set-museion").isEmpty)
}
@Test func `non-zai key write only touches env`() async throws {
let (binDir, writer) = makeRig()
let root = binDir + "/profiles"
+35
View File
@@ -0,0 +1,35 @@
//
// ProfileLayoutTests.swift
// MuseionTests
//
// #182 — the profile layout's one pinning suite.
//
// Scenario: A layout change is one module + one suite — From user story #182
// Given the Hermes profile layout (root, per-profile dir, .env, marker,
// gateway pid, skills dir) is owned by ProfileLayout alone
// When any layout fact moves (root renamed, a file renamed)
// Then ProfileLayout.swift plus exactly this suite change — the provisioner,
// the settings writer, and backup/restore derive every path through it and
// no literal profile-root string exists outside the layout module.
//
import Foundation
@testable import Museion
import Testing
@Suite("ProfileLayoutTests")
struct ProfileLayoutTests {
/// Pinned against LITERALS (not the layout's own inputs) so a fact change
/// fails here — this suite is the layout contract of record.
@Test func `root default is the hermes profiles dir`() {
#expect(ProfileLayout.defaultRoot == NSHomeDirectory() + "/.hermes/profiles")
}
@Test func `per-profile path builders pin every layout fact`() {
#expect(ProfileLayout.profileDir("museion-pass1", root: "/r") == "/r/museion-pass1")
#expect(ProfileLayout.envFile(in: "/r/museion") == "/r/museion/.env")
#expect(ProfileLayout.configuredMarker(in: "/r/museion") == "/r/museion/.museion-provisioned")
#expect(ProfileLayout.gatewayPID(in: "/r/museion") == "/r/museion/gateway.pid")
#expect(ProfileLayout.skillsDir(in: "/r/museion") == "/r/museion/skills")
}
}
+6 -2
View File
@@ -35,8 +35,8 @@ struct SeedSkill {
/// per-profile marker, prints "Saved 'museion-tools' to …" (verified wire
/// strings). `config set <key> <value> [--force]` appends the exact argv to
/// `<binaryDir>/config-set-<profile>` and prints the verified "✓ Set …"
/// line. `<binaryDir>/mcp-add-fails` / `config-set-fails` force the matching
/// failure. Every `profile create` name appends to
/// line. `<binaryDir>/mcp-add-fails` / `config-set-fails` / `config-unset-fails`
/// force the matching failure. Every `profile create` name appends to
/// `<binaryDir>/profile-create-log`.
/// #136: `config set` also records KEY=VALUE into
/// `<binaryDir>/config-store-<profile>` (dedup per key), and the fake speaks
@@ -77,6 +77,10 @@ private func fakeHermesConfigCLI() -> String {
fi
if [[ "$1" == "-p" && "$3" == "config" && "$4" == "unset" ]]; then
PROFILE="$2"; KEY="$5"
if [[ -f "${0:h}/config-unset-fails" ]]; then
print "Error: config unset exploded"
exit 1
fi
print -r -- "$*" >> "${0:h}/config-unset-$PROFILE"
STORE="${0:h}/config-store-$PROFILE"
if [[ -f "$STORE" && -n "$(grep -m1 "^${KEY}=" "$STORE")" ]]; then