feat(m0): complete scaffold, logging and signature checks (T0.2-T0.6)

Add os.Logger categories, OSSignposter wrapper and shortHash helper; enforce exact Debug/Release/install signatures; lint tests; update M0 docs and progress.
This commit is contained in:
schihei committed 2026-11-26 12:33:51 +01:00
1 parent 374085ec2f
commit 7ca5c710ec
17 files changed
+331 -9

No files matched your search

@@ -0,0 +1,21 @@
/// Log categories from ARCHITECTURE §15.
public enum LogCategory: String, CaseIterable, Sendable {
/// Double-copy detection and capture.
case capture
/// Pasteboard polling and own-write suppression.
case pasteboard
/// Translation session lifecycle.
case session
/// Ollama HTTP client.
case ollama
/// Prompt and adapter construction.
case engine
/// Output validation.
case validate
/// Accessibility or paste insertion.
case insert
/// Panel presentation.
case panel
/// Settings persistence.
case settings
}
@@ -0,0 +1,15 @@
import CryptoKit
import Foundation
/// Privacy-safe helpers for log values (SP-2).
public enum LogRedaction {
/// Returns the first 12 lowercase hex characters of the SHA-256 digest of `value`.
///
/// Use this for identifiers that must be correlatable across log lines without revealing
/// the original text. Always log the result with `privacy: .private`; SECURITY_PRIVACY
/// §Logging requires the private qualifier for correlation hashes as well.
public static func shortHash(_ value: String) -> String {
let digest = SHA256.hash(data: Data(value.utf8))
return digest.prefix(6).map { String(format: "%02x", $0) }.joined()
}
}
@@ -0,0 +1,31 @@
import os
/// Factory for the app's `os.Logger` and `OSSignposter` instances.
///
/// The subsystem is supplied by the composition root (the app bundle identifier or the CLI
/// subsystem); this module never hard-codes one.
///
/// ## Leaf modules
/// `SprachmittlerOllama` and `SprachmittlerEngines` cannot import Core. When they gain logging,
/// the composition root passes `Logger` values created here into their initializers. A leaf that
/// needs signposts uses an `OSSignposter` with the literal names from ARCHITECTURE §15. Do not
/// add a Core dependency to a leaf module.
public struct Loggers: Sendable {
/// The `os.Logger` subsystem, normally the app bundle identifier.
public let subsystem: String
/// Creates a factory for the given subsystem.
public init(subsystem: String) {
self.subsystem = subsystem
}
/// Returns the logger for one ARCHITECTURE §15 category.
public func logger(_ category: LogCategory) -> Logger {
Logger(subsystem: subsystem, category: category.rawValue)
}
/// Returns the signposter for one ARCHITECTURE §15 category.
public func signposts(_ category: LogCategory) -> Signposts {
Signposts(subsystem: subsystem, category: category)
}
}
@@ -0,0 +1,13 @@
import os
/// A started signpost interval.
///
/// The value is intentionally not `Sendable`: an interval must begin and end on the same task.
/// Obtain one from ``Signposts/begin(_:)`` and pass it to ``Signposts/end(_:)``.
public struct SignpostInterval {
/// The interval name.
public let name: SignpostName
let state: OSSignpostIntervalState
let signposter: OSSignposter
}
@@ -0,0 +1,33 @@
/// Signpost interval names from ARCHITECTURE §15.
public enum SignpostName: String, CaseIterable, Sendable {
/// Double-copy detection, from the second copy to a captured text.
case capture
/// Panel presentation.
case panel
/// An Ollama request, from send to the final delta.
case request
/// The first translation token.
case firstToken
/// The full translation of one chunk.
case translate
/// Output validation.
case validate
/// Writing the translation to the clipboard.
case copy
/// Accessibility or paste insertion.
case insert
/// `OSSignposter.beginInterval` requires a `StaticString`, so each case maps to its own literal.
public var literal: StaticString {
switch self {
case .capture: "capture"
case .panel: "panel"
case .request: "request"
case .firstToken: "firstToken"
case .translate: "translate"
case .validate: "validate"
case .copy: "copy"
case .insert: "insert"
}
}
}
@@ -0,0 +1,29 @@
import os
/// Thin wrapper around `OSSignposter` that uses the names from ARCHITECTURE §15.
///
/// Leaf modules cannot import Core; the composition root can pass an `OSSignposter` directly to
/// them, using the literal names in ARCHITECTURE §15.
public struct Signposts: Sendable {
private let signposter: OSSignposter
init(subsystem: String, category: LogCategory) {
signposter = OSSignposter(subsystem: subsystem, category: category.rawValue)
}
/// Starts an interval; pair it with ``end(_:)`` on the same task.
public func begin(_ name: SignpostName) -> SignpostInterval {
let state = signposter.beginInterval(name.literal)
return SignpostInterval(name: name, state: state, signposter: signposter)
}
/// Ends an interval started by ``begin(_:)``.
public func end(_ interval: SignpostInterval) {
interval.signposter.endInterval(interval.name.literal, interval.state)
}
/// Emits a single signpost event.
public func event(_ name: SignpostName) {
signposter.emitEvent(name.literal)
}
}
@@ -0,0 +1,16 @@
import Testing
@testable import SprachmittlerCore
/// T0.4: SP-2 allows short hashes in logs, never the underlying text.
@Suite("Log redaction")
struct LogRedactionTests {
@Test func shortHashIsStableTwelveHexAndOpaque() {
let hash = LogRedaction.shortHash("hello")
#expect(hash == "2cf24dba5fb0")
#expect(hash == LogRedaction.shortHash("hello"))
#expect(hash.count == 12)
#expect(hash.allSatisfy { $0.isHexDigit && !$0.isUppercase })
#expect(hash != LogRedaction.shortHash("hello!"))
#expect(!hash.contains("hello"))
}
}
@@ -0,0 +1,54 @@
import os
import Testing
@testable import SprachmittlerCore
/// T0.4: the logging helper must stay in sync with ARCHITECTURE §15.
@Suite("Logging contract")
struct LoggingContractTests {
@Test func categoriesMatchArchitecture() {
#expect(LogCategory.allCases.map(\.rawValue) == [
"capture",
"pasteboard",
"session",
"ollama",
"engine",
"validate",
"insert",
"panel",
"settings",
])
}
@Test func signpostNamesMatchArchitecture() {
#expect(SignpostName.allCases.map(\.rawValue) == [
"capture",
"panel",
"request",
"firstToken",
"translate",
"validate",
"copy",
"insert",
])
}
/// `OSSignposter.beginInterval` needs a `StaticString`; this catches a hand-written switch drift.
@Test func signpostLiteralsMatchRawValues() {
for name in SignpostName.allCases {
#expect(String(describing: name.literal) == name.rawValue)
}
}
@Test func loggersWorkForEveryCategory() {
let loggers = Loggers(subsystem: "test.sprachmittler")
for category in LogCategory.allCases {
loggers.logger(category).debug("logging contract test")
let signposts = loggers.signposts(category)
let interval = signposts.begin(.capture)
#expect(interval.name.rawValue == "capture")
signposts.end(interval)
signposts.event(.panel)
}
}
}