Files
museion/Museion/SettingsPaneView.swift
T
AlexKucera 1c697440d8
CI / ci (push) Canceled after 0s
feat(mentor): per-surface engine lifecycle (#137)
Profiles S4: passes run on their OWN one-shot engines —
`hermes acp -p museion-passN` spawns per pass run and tears down at
the verdict, so a model switch costs nothing (the next spawn reads
fresh config). The verdict streams into a TRANSIENT unrostered
transcript (#129's ruling: one Sessions surface over the chat
engine; a rostered pass session would be unresumable by the chat
engine). `beginPassSession` died with it — passes never mint
sessions; the #28 roster pins flipped to the transient posture.

The chat engine stays resident (adopted museion profile): a Chat
model change in Settings fires onChatSurfaceWrite after a
successful chat-surface write only, wired to requestRestart —
re-spawn in place when idle (resuming the pre-restart active
session), deferred to turn end while any turn streams (#129).

Missing-key posture (#132): refreshPassReadiness reads both pass
profiles' providers and their .env keys at pane entry; Run Pass
disables naming the provider with an Open Settings link, .unknown
never blocks, and the guard refuses before anything touches disk.

Finish-audit (ponytail/security/general fanout) applied: the
general reviewer's Medium — a session/new/load/fork completing
mid-pass stole the active surface and lost the Cancel to the wrong
engine — fixed with ONE claimActive gate across the post-await
activation writers, pinned deterministically via the fake's
loadReplayHold park + a mode-file flip (flip-verified: guard off
fails on the steal observables and the misrouted-cancel hang).
Also fixed: a spawn-window cancel loss in stopStreaming (keyed on
the transient entry, not the late-arriving wire id), and a
PRE-EXISTING rescan bug where a stale scan snapshot "vanished" a
just-opened piece and killed its session, cancelling the edit
debounce — the vanish-kill now re-checks disk and the watched
home. Ponytail cut ~59 lines of test duplication; the app target's
only force-unwrap is gone.

Suite 477 (was 472): EngineLifecycleTests new (own-engine
end-to-end, held-pass cancel, idle + deferred restart ordering,
readiness, steal pin), +3 Settings hook pins, one duplicate pin
deduplicated. swiftformat clean, swiftlint at HEAD baseline.

Closes #137
2026-10-28 17:26:40 +01:00

234 lines
6.5 KiB
Swift

//
// SettingsPaneView.swift
// Museion
//
// #17 (S1.1): Settings — the Museion Home path, repoint-only (ADR-0004).
// #135 (Profiles S2): reworked to the #130 prototype's variant C verdict —
// a Settings sidebar + detail column (System Settings shape): destinations
// Content · Pass 1 · Pass 2 · Chat · Provider Keys. Content keeps the #17
// Home control unchanged; the surface and keys pages live in
// SettingsPagesView.swift.
// #136 (Profiles S3): the model runs over HermesSettingsWriter (the real
// CLI/.env rail) and hydrates from the profiles at pane open.
//
import AppKit
import SwiftUI
/// Settings pane — sidebar + detail per prototype variant C.
struct SettingsPaneView: View {
@Binding var home: URL
/// #136: the gate's binary winner — HermesSettingsWriter's CLI rail and
/// the hydration reads both need it (nil before a gate pass: writes
/// surface their failure, reads leave the seed).
let binaryPath: String?
@State private var page: SettingsPage = .content
/// #135/#136: the model over the REAL write path; hydrated from the
/// profiles at pane open.
@State private var model: SettingsModel
init(home: Binding<URL>, binaryPath: String?, onChatSurfaceWrite: (() -> Void)? = nil) {
_home = home
self.binaryPath = binaryPath
let model = SettingsModel(writer: HermesSettingsWriter(hermesBinary: binaryPath))
// #137: a committed Chat model write reaches the resident engine
// (WorkspaceView wires requestRestart).
model.onChatSurfaceWrite = onChatSurfaceWrite
_model = State(initialValue: model)
}
var body: some View {
VStack(spacing: 0) {
PaneHeader(title: "Settings")
if let error = model.lastWriteError {
Text(error)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(.orange)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 14)
.padding(.vertical, 4)
}
HStack(spacing: 0) {
sidebar
Rectangle()
.fill(DesignTokens.border)
.frame(width: 1)
detail
}
}
.background(DesignTokens.background)
.task { await model.hydrate() }
}
// MARK: Sidebar (prototype `.vC .side`)
private var sidebar: some View {
VStack(alignment: .leading, spacing: 2) {
ForEach(SettingsPage.allCases) { candidate in
SettingsSidebarButton(
page: candidate,
selection: $page,
)
}
Spacer(minLength: 0)
}
.padding(.horizontal, 8)
.padding(.vertical, 14)
.frame(width: DesignTokens.settingsSidebarWidth)
}
// MARK: Detail (prototype `.vC .detail`)
private var detail: some View {
ScrollView {
switch page {
case .content:
ContentSettingsPage(home: $home)
case .pass1:
SurfaceSettingsPage(model: model, surface: .pass1)
case .pass2:
SurfaceSettingsPage(model: model, surface: .pass2)
case .chat:
SurfaceSettingsPage(model: model, surface: .chat)
case .keys:
ProviderKeysPage(model: model)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
/// The sidebar's destinations (#135): the #17 Content control, one page per
/// LLM surface (Sessions rides Chat — #126), and the provider keys.
enum SettingsPage: String, CaseIterable, Identifiable, Hashable {
case content
case pass1
case pass2
case chat
case keys
var id: String {
rawValue
}
var label: String {
switch self {
case .content: "Content"
case .pass1: "Pass 1"
case .pass2: "Pass 2"
case .chat: "Chat"
case .keys: "Provider Keys"
}
}
/// The prototype's trailing mono annotation: pass chips + the Sessions
/// note on Chat (the chat row notes "Sessions rides this engine").
var annotation: String? {
switch self {
case .pass1: "P1"
case .pass2: "P2"
case .chat: "· Sessions"
default: nil
}
}
}
/// One sidebar row (prototype `.vC .side-button`): quiet label, trailing
/// mono annotation, surface card + inset accent bar when current.
private struct SettingsSidebarButton: View {
let page: SettingsPage
@Binding var selection: SettingsPage
@State private var hover = false
var body: some View {
let selected = selection == page
Button {
selection = page
} label: {
HStack(spacing: 8) {
Text(page.label)
.foregroundStyle(DesignTokens.foreground)
Spacer(minLength: 0)
if let annotation = page.annotation {
Text(annotation)
.font(.system(size: 10, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
}
}
.padding(.horizontal, 10)
.padding(.vertical, 7)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.background {
if selected || hover {
RoundedRectangle(cornerRadius: 8)
.fill(selected ? DesignTokens.surface : DesignTokens.foreground.opacity(0.06))
}
}
.overlay(alignment: .leading) {
// `.side-button.current` inset accent bar.
if selected {
RoundedRectangle(cornerRadius: 1)
.fill(DesignTokens.accent)
.frame(width: 2)
.padding(.vertical, 4)
}
}
.onHover { hover = $0 }
.accessibilityLabel(page.label)
.accessibilityAddTraits(selected ? [.isSelected] : [])
}
}
/// The Content page: the existing #17 Home control, repoint-only, in the
/// variant C form (quiet label, mono path, Choose…).
private struct ContentSettingsPage: View {
@Binding var home: URL
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Content")
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
Text("Where the corpus lives.")
.font(.system(size: 12))
.foregroundStyle(DesignTokens.muted)
VStack(alignment: .leading, spacing: 14) {
VStack(alignment: .leading, spacing: 4) {
SettingsFieldLabel("Museion Home")
HStack(spacing: 8) {
Text(home.path(percentEncoded: false))
.font(.system(size: 12, design: DesignTokens.mono))
.lineLimit(1)
.truncationMode(.middle)
.textSelection(.enabled)
Spacer(minLength: 12)
Button("Choose…") { chooseHome() }
.buttonStyle(.bordered)
}
}
Text("Repoint-only: Museion never moves content.")
.font(.system(size: 11.5))
.foregroundStyle(DesignTokens.muted)
}
.padding(.top, 12)
}
.frame(maxWidth: DesignTokens.settingsFormWidth, alignment: .leading)
.padding(.horizontal, 26)
.padding(.top, 18)
.padding(.bottom, 48)
}
private func chooseHome() {
let panel = NSOpenPanel()
panel.canChooseDirectories = true
panel.canChooseFiles = false
panel.canCreateDirectories = true
panel.message = "Choose the folder holding your posts. Museion never moves content."
if panel.runModal() == .OK, let url = panel.url {
home = url
}
}
}