packages feed

kenshou-evidence (empty) → 0.1.0.0

raw patch · 27 files changed

+5009/−0 lines, 27 filesdep +aesondep +basedep +bytestring

Dependencies added: aeson, base, bytestring, containers, directory, filepath, hspec, kenshou-core, kenshou-evidence, kenshou-remote, okf-core, optparse-applicative, process, settei, settei-env, settei-optparse-applicative, temporary, text, time, transformers, unix

Files

+ CHANGELOG.md view
@@ -0,0 +1,13 @@+# Changelog++## 0.1.0.0 - 2026-10-28++### New Features++- First public release of immutable run records, verification bundles, attestation, and history.+- The record subject resolves from `--subject`, then the run layer's bundle, then `evidence.default-subject`; `history` takes the bundle URI from `evidence.bundle-uri` or `--bundle-uri`.++### Other Changes++- Extracted from `mori://shinzui/keiro-runtime-kenshou` at commit `a1648293443ad88ff1695ed800ff44c99993574e`, with its package and module names unchanged.+- Consumer records written before the extraction still read and compare unchanged.
+ LICENSE view
@@ -0,0 +1,28 @@+BSD 3-Clause License++Copyright (c) 2026, Nadeem Bitar++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this+   list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice,+   this list of conditions and the following disclaimer in the documentation+   and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors+   may be used to endorse or promote products derived from this software without+   specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ kenshou-evidence.cabal view
@@ -0,0 +1,107 @@+cabal-version: 3.0+name: kenshou-evidence+version: 0.1.0.0+synopsis: Historic verification records and durable object storage+description: Evidence types and storage boundaries for Kenshou run records.+license: BSD-3-Clause+license-file: LICENSE+author: Nadeem Bitar+maintainer: nadeem@gmail.com+homepage: https://github.com/shinzui/kenshou+bug-reports: https://github.com/shinzui/kenshou/issues+category: Testing+build-type: Simple+extra-doc-files: CHANGELOG.md+tested-with: ghc >=9.12 && <9.13+extra-source-files:+  test/fixtures/consumer-records/*.json+  test/fixtures/consumer-records/*.md+  test/fixtures/consumer-records/SOURCE+  test/fixtures/profile-invalid/*.json++common warnings+  ghc-options:+    -Wall+    -Wcompat+    -Widentities+    -Wincomplete-record-updates+    -Wincomplete-uni-patterns+    -Wpartial-fields+    -Wredundant-constraints++  default-language: GHC2024+  default-extensions:+    BlockArguments+    DuplicateRecordFields+    ImportQualifiedPost+    NoFieldSelectors+    OverloadedRecordDot+    OverloadedStrings++library+  import: warnings+  hs-source-dirs: src+  exposed-modules:+    Kenshou.Evidence.Attest+    Kenshou.Evidence.Bundle+    Kenshou.Evidence.Check+    Kenshou.Evidence.Cli+    Kenshou.Evidence.Config+    Kenshou.Evidence.Frontmatter+    Kenshou.Evidence.History+    Kenshou.Evidence.HistoryCli+    Kenshou.Evidence.Publish+    Kenshou.Evidence.Record+    Kenshou.Evidence.Source+    Kenshou.Evidence.Store+    Kenshou.Evidence.Types++  build-depends:+    aeson >=2.2 && <2.3,+    base >=4.21 && <4.22,+    bytestring >=0.12 && <0.13,+    containers >=0.7 && <0.8,+    directory >=1.3 && <1.4,+    filepath >=1.5 && <1.6,+    kenshou-core ^>=0.1.0.0,+    kenshou-remote ^>=0.1.0.0,+    okf-core ^>=0.9.0.0,+    optparse-applicative >=0.19 && <0.20,+    process >=1.6 && <1.7,+    settei ^>=0.2.0.0,+    settei-env ^>=0.2.0.0,+    settei-optparse-applicative ^>=0.2.0.0,+    temporary >=1.3 && <1.4,+    text >=2.1 && <2.2,+    time >=1.14 && <1.15,+    transformers >=0.6 && <0.7,+    unix >=2.8 && <2.9,++test-suite kenshou-evidence-test+  import: warnings+  type: exitcode-stdio-1.0+  hs-source-dirs: test+  main-is: Main.hs+  build-depends:+    aeson,+    base >=4.21 && <4.22,+    bytestring,+    containers,+    directory >=1.3 && <1.4,+    filepath,+    hspec >=2.11 && <2.12,+    kenshou-core ^>=0.1.0.0,+    kenshou-evidence,+    kenshou-remote ^>=0.1.0.0,+    okf-core,+    process >=1.6 && <1.7,+    settei,+    settei-env,+    settei-optparse-applicative,+    temporary >=1.3 && <1.4,+    text,+    time >=1.14 && <1.15,++source-repository head+  type: git+  location: https://github.com/shinzui/kenshou.git
+ src/Kenshou/Evidence/Attest.hs view
@@ -0,0 +1,682 @@+module Kenshou.Evidence.Attest+  ( Recomputation (..),+    Recomputer (..),+    AttestOptions (..),+    AttestResult (..),+    AttestError (..),+    coreRecomputers,+    attest,+  )+where++import Control.Exception (IOException, try)+import Control.Monad (forM)+import Data.Aeson (FromJSON (..), Result (..), Value (..), eitherDecodeStrict', fromJSON, object, withObject, (.:), (.:?), (.=))+import Data.Aeson.KeyMap qualified as KeyMap+import Data.ByteString qualified as ByteString+import Data.List (find, nub, sort)+import Data.Map.Strict qualified as Map+import Data.Maybe (mapMaybe)+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Text.IO qualified as Text.IO+import Data.Time (UTCTime, defaultTimeLocale, formatTime, getCurrentTime)+import Kenshou.Core.Cohort (CohortIdentity (..), PlanHash (..), ResolvedComponent (..), ResolvedPackage (..))+import Kenshou.Core.Cohort qualified as Cohort+import Kenshou.Core.Id (newRunId, parseRunId, renderRunId, renderScenarioId)+import Kenshou.Core.Manifest (Manifest (..), ManifestFile (..))+import Kenshou.Core.Outcome (Outcome (Passed), renderOutcome)+import Kenshou.Core.RunSpec (CohortExpectation (..), RunSpec (..))+import Kenshou.Evidence.Bundle (BundleWriteError, BundleWriteResult (..), writeAttestationRecord)+import Kenshou.Evidence.Frontmatter (AttestationCheck (..), AttestationEvidence (..), ComparisonEvidence (..), EvidenceRecord (..), comparisonFromDocument, recordFromDocument)+import Kenshou.Evidence.Record (RecordInput (..), buildRunRecord)+import Kenshou.Evidence.Source (ComparisonSource (..), ComparisonView (..), RunResultView (..), RunSource (..), loadComparisonSource, loadRunSource)+import Kenshou.Evidence.Store (ObjectStore (..))+import Kenshou.Evidence.Types (ComponentRef (..), ComponentSource (FromGit), DataKind (..), DataLink (..), Revision (..), Sha256 (..), mkRevision, sha256Bytes)+import Okf.Actor (Actor (ProcessActor))+import Okf.Document (OKFDocument (..), Verification (..), frontmatterLookup, parseDocument, readVerified, serializeDocument, setVerified)+import System.Directory (doesDirectoryExist, listDirectory)+import System.Environment (getEnvironment)+import System.Exit (ExitCode (..))+import System.FilePath (isAbsolute, normalise, splitDirectories, takeBaseName, takeExtension, (</>))+import System.IO.Temp (withSystemTempDirectory)+import System.Process (CreateProcess (..), proc, readCreateProcessWithExitCode, readProcessWithExitCode)++data Recomputation = Recomputation+  { agreesWithDocuments :: !Bool,+    outcome :: !(Maybe Outcome),+    comparisonVerdict :: !(Maybe Text),+    detail :: !Text+  }+  deriving stock (Eq, Show)++data Recomputer = Recomputer+  { algorithm :: !Text,+    algorithmVersion :: !Integer,+    recompute :: FilePath -> IO (Either Text Recomputation)+  }++data AttestOptions = AttestOptions+  { bundleRoot :: !FilePath,+    offline :: !Bool,+    linkedOnly :: !Bool,+    acceptedAnomaly :: !(Maybe (Text, Text))+  }+  deriving stock (Eq, Show)++data AttestResult = AttestResult+  { path :: !FilePath,+    verdict :: !Text+  }+  deriving stock (Eq, Show)++data AttestError+  = InvalidTarget !Text+  | AttestIo !Text+  | AttestBundle !BundleWriteError+  deriving stock (Eq, Show)++-- A generic run result does not contain enough information to replay its+-- scenario oracle. The fixed self-test is the one core scenario with an+-- independent outcome that can be derived from its identity alone.+coreRecomputers :: [Recomputer]+coreRecomputers = [selftestOutcomeRecomputer]++selftestOutcomeRecomputer :: Recomputer+selftestOutcomeRecomputer = Recomputer "run-outcome" 1 $ \root -> do+  loaded <- loadRunSource root+  pure case loaded of+    Left err -> Left (Text.pack (show err))+    Right source+      | renderScenarioId source.result.resultScenario == "selftest/kernel/correctness/always-pass" ->+          Right+            Recomputation+              { agreesWithDocuments = source.spec.scenario == source.result.resultScenario && source.result.resultOutcome == Passed,+                outcome = Just Passed,+                comparisonVerdict = Nothing,+                detail = "the fixed always-pass self-test independently requires a passed outcome"+              }+      | otherwise -> Left "no domain outcome oracle is registered for this scenario"++attest :: ObjectStore -> [Recomputer] -> AttestOptions -> Text -> IO (Either AttestError AttestResult)+attest store recomputers options rawTarget = do+  attempted <- try $ do+    resolved <- resolveTarget options.bundleRoot rawTarget+    case resolved of+      Left err -> pure (Left err)+      Right relative -> attestRun store recomputers options relative+  pure $ either (Left . AttestIo . Text.pack . show) id (attempted :: Either IOException (Either AttestError AttestResult))++resolveTarget :: FilePath -> Text -> IO (Either AttestError FilePath)+resolveTarget root rawTarget = do+  let relative = Text.unpack (Text.dropWhile (== '/') rawTarget)+      segments = splitDirectories relative+  if safeRelative relative && length segments == 5 && take 1 segments == ["runs"] && takeExtension relative == ".md"+    then pure (Right relative)+    else case parseRunId rawTarget of+      Left _ -> pure (Left (InvalidTarget "attestation target must be a run concept path or run ID"))+      Right _ -> do+        let runs = root </> "runs"+        layers <- ifMDirectory runs listDirectory+        matches <- fmap concat $ forM layers $ \layer -> do+          years <- ifMDirectory (runs </> layer) listDirectory+          fmap concat $ forM years $ \year -> do+            months <- ifMDirectory (runs </> layer </> year) listDirectory+            fmap concat $ forM months $ \month -> do+              let folder = runs </> layer </> year </> month+              names <- ifMDirectory folder listDirectory+              pure ["runs" </> layer </> year </> month </> name | name <- names, name == Text.unpack rawTarget <> ".md"]+        pure case matches of+          [path] -> Right path+          [] -> Left (InvalidTarget "run ID is not present in the evidence bundle")+          _ -> Left (InvalidTarget "run ID matches more than one run concept")++ifMDirectory :: FilePath -> (FilePath -> IO [FilePath]) -> IO [FilePath]+ifMDirectory path action = do+  exists <- doesDirectoryExist path+  if exists then action path else pure []++attestRun :: ObjectStore -> [Recomputer] -> AttestOptions -> FilePath -> IO (Either AttestError AttestResult)+attestRun store recomputers options relative = do+  content <- Text.IO.readFile (options.bundleRoot </> relative)+  case parseDocument content of+    Left err -> pure (Left (InvalidTarget (Text.pack (show err))))+    Right document -> case frontmatterLookup "recordKind" document.frontmatter of+      Just (String "run") -> case recordFromDocument document of+        Left err -> pure (Left (InvalidTarget (Text.pack (show err))))+        Right record -> attestOrdinaryRun store recomputers options relative content record+      Just (String "comparison") -> case comparisonFromDocument document of+        Left err -> pure (Left (InvalidTarget (Text.pack (show err))))+        Right record -> attestComparison store recomputers options relative content record+      _ -> pure (Left (InvalidTarget "target is neither a run nor a comparison record"))++attestOrdinaryRun :: ObjectStore -> [Recomputer] -> AttestOptions -> FilePath -> Text -> EvidenceRecord -> IO (Either AttestError AttestResult)+attestOrdinaryRun store recomputers options relative content record =+  withSystemTempDirectory "kenshou-attest" $ \scratch -> do+    now <- getCurrentTime+    (digestCheck, matchedDigests, source) <- verifyData store options.linkedOnly scratch record+    if null matchedDigests+      then pure (Left (AttestIo "no linked object could be fetched and checked"))+      else do+        revision <- currentAttesterRevision+        case revision of+          Left err -> pure (Left err)+          Right (attesterRevision, attesterDirty) -> do+            revisions <- checkRevisions options.offline scratch record source+            let cohort = checkCohort now record source+                environment = checkEnvironment now record source+                clean = checkClean record source attesterDirty+            recomputed <- checkRecomputation options.bundleRoot recomputers scratch record source+            let checks = [digestCheck, revisions, cohort, recomputed, environment, clean]+            finishAttestation options relative content record.runId now attesterRevision matchedDigests checks++data ComparisonRefs = ComparisonRefs+  { baselinePaths :: ![Text],+    candidatePaths :: ![Text],+    verdict :: !Text,+    design :: !Text,+    factor :: !Text,+    factorName :: !(Maybe Text),+    baselineValue :: !Value,+    candidateValue :: !Value+  }++instance FromJSON ComparisonRefs where+  parseJSON = withObject "comparison references" $ \value ->+    ComparisonRefs+      <$> value .: "baselineRuns"+      <*> value .: "candidateRuns"+      <*> value .: "verdict"+      <*> value .: "design"+      <*> value .: "factor"+      <*> value .:? "factorName"+      <*> value .: "baselineValue"+      <*> value .: "candidateValue"++data FetchedArm = FetchedArm+  { record :: !EvidenceRecord,+    source :: !(Maybe RunSource),+    digestCheck :: !AttestationCheck,+    digests :: ![Sha256]+  }++attestComparison :: ObjectStore -> [Recomputer] -> AttestOptions -> FilePath -> Text -> ComparisonEvidence -> IO (Either AttestError AttestResult)+attestComparison store recomputers options relative content record =+  withSystemTempDirectory "kenshou-attest-comparison" $ \scratch -> do+    now <- getCurrentTime+    revision <- currentAttesterRevision+    case revision of+      Left err -> pure (Left err)+      Right (attesterRevision, attesterDirty) -> do+        let decodedRefs = case fromJSON record.comparison of+              Success refs -> Right refs+              Error message -> Left (Text.pack message)+        case decodedRefs of+          Left message -> pure (Left (InvalidTarget message))+          Right refs -> do+            fetchedComparison <- case record.dataLinks of+              [link] | link.kind == ComparisonData -> do+                fetched <- fetchLink store scratch record.runId link+                case fetched of+                  Left err -> pure (Left err)+                  Right digest -> do+                    decoded <- loadComparisonSource (scratch </> "comparison.json")+                    pure case decoded of+                      Left err -> Left (Text.pack (show err))+                      Right source -> Right (digest, source)+              _ -> pure (Left "comparison record must link one comparison.json")+            armRecords <- traverse (loadArm options.bundleRoot) (refs.baselinePaths <> refs.candidatePaths)+            let armProblem = case sequence armRecords of+                  Left message -> Just message+                  Right _ -> Nothing+                records = [(index, arm) | (index, Right arm) <- zip [0 :: Int ..] armRecords]+            fetchedArms <- forM records $ \(index, arm) -> do+              let side = if index < length refs.baselinePaths then "baseline" else "candidate"+                  position = if side == "baseline" then index else index - length refs.baselinePaths+                  armRoot = scratch </> side </> show position+              (checked, digests, source) <- verifyData store options.linkedOnly armRoot arm+              pure FetchedArm {record = arm, source, digestCheck = checked, digests}+            let linkedDigests = either (const []) (\(digest, _) -> [digest]) fetchedComparison+                allDigests = linkedDigests <> concatMap (.digests) fetchedArms+                digestFailures =+                  maybe [] pure (either Just (const Nothing) fetchedComparison)+                    <> [Text.pack (show index) <> ": " <> maybe "" id item.digestCheck.detail | (index, item) <- zip [0 :: Int ..] fetchedArms, item.digestCheck.result == "failed"]+                    <> maybe [] pure armProblem+                digestCheck =+                  if not (null digestFailures)+                    then check "digests-match" "failed" (Text.intercalate "; " digestFailures)+                    else+                      if options.linkedOnly+                        then check "digests-match" "skipped" "only linked objects were checked"+                        else check "digests-match" "passed" "comparison document and every arm's linked and manifest objects match"+                source = either (const Nothing) (Just . snd) fetchedComparison+                cohort = checkComparisonCohort now record refs source fetchedArms armProblem+                environment = combineArmChecks "environment-captured" [checkEnvironment now arm.record arm.source | arm <- fetchedArms]+                clean = combineArmChecks "clean-worktree" (check "clean-worktree" (if record.harnessDirty || maybe False ((== Just True) . (.harnessDirty) . (.view)) source || attesterDirty then "failed" else "passed") "comparison and attester worktrees" : [checkClean arm.record arm.source False | arm <- fetchedArms])+            if null allDigests+              then pure (Left (AttestIo ("no linked object could be fetched and checked: " <> Text.intercalate "; " digestFailures)))+              else do+                revisions <- checkComparisonRevisions options.offline scratch record fetchedArms+                recomputed <-+                  if digestCheck.result == "passed" && all (maybe False (const True) . (.source)) fetchedArms+                    then checkComparisonRecomputation options.bundleRoot recomputers scratch record source+                    else pure (check "verdict-recomputed" "skipped" "comparison or arm source data could not be verified")+                finishAttestation options relative content record.runId now attesterRevision allDigests [digestCheck, revisions, cohort, recomputed, environment, clean]++loadArm :: FilePath -> Text -> IO (Either Text EvidenceRecord)+loadArm bundle reference = do+  let relative = Text.unpack (Text.dropWhile (== '/') reference)+  if not (safeRelative relative && length (splitDirectories relative) == 5 && take 1 (splitDirectories relative) == ["runs"] && takeExtension relative == ".md")+    then pure (Left ("invalid comparison arm path: " <> reference))+    else do+      loaded <- try (Text.IO.readFile (bundle </> relative)) :: IO (Either IOException Text)+      pure do+        content <- either (Left . Text.pack . show) Right loaded+        document <- either (Left . Text.pack . show) Right (parseDocument content)+        either (Left . Text.pack . show) Right (recordFromDocument document)++combineArmChecks :: Text -> [AttestationCheck] -> AttestationCheck+combineArmChecks name checks+  | any ((== "failed") . (.result)) checks = check name "failed" "one or more comparison arms failed"+  | null checks || any ((== "skipped") . (.result)) checks = check name "skipped" "one or more comparison arms could not be checked"+  | otherwise = check name "passed" "every comparison arm passed"++checkComparisonCohort :: UTCTime -> ComparisonEvidence -> ComparisonRefs -> Maybe ComparisonSource -> [FetchedArm] -> Maybe Text -> AttestationCheck+checkComparisonCohort _ _ _ Nothing _ _ = check "cohort-matches-plan" "skipped" "comparison document is unavailable"+checkComparisonCohort now record refs (Just source) arms armProblem =+  let baseline = take (length refs.baselinePaths) arms+      candidate = drop (length refs.baselinePaths) arms+      armIds = map (.record.runId) arms+      expectedIds = map renderRunId (source.view.baselineRuns <> source.view.candidateRuns)+      paths = refs.baselinePaths <> refs.candidatePaths+      pathsMatch = and (zipWith (\path arm -> takeBaseName (Text.unpack path) == Text.unpack arm.record.runId) paths arms)+      armChecks = [checkCohort now arm.record arm.source | arm <- arms]+      axis = case (refs.factor, refs.factorName) of+        ("cohort", Nothing) -> Just "cohort"+        ("dimension", Just name) -> Just ("dim:" <> name)+        ("knob", Just name) -> Just ("knob:" <> name)+        _ -> Nothing+      valueFor valueAxis arm+        | valueAxis == "cohort" = Just (String arm.record.cohort)+        | Just name <- Text.stripPrefix "dim:" valueAxis = String <$> lookup name arm.record.dimensions+        | Just name <- Text.stripPrefix "knob:" valueAxis = lookup name arm.record.knobs+        | otherwise = Nothing+      uniformValue valueAxis members = case nub (mapMaybe (valueFor valueAxis) members) of+        [value] | length members == length (mapMaybe (valueFor valueAxis) members) -> Just value+        _ -> Nothing+      factorMatches = case axis of+        Just valueAxis ->+          source.view.variedFactors == [valueAxis]+            && uniformValue valueAxis baseline == Just refs.baselineValue+            && uniformValue valueAxis candidate == Just refs.candidateValue+            && refs.baselineValue /= refs.candidateValue+        Nothing -> False+      outcomeMatches = case refs.verdict of+        "pass" -> renderOutcome record.outcome == "passed"+        "regression" -> renderOutcome record.outcome == "failed"+        "inconclusive" -> renderOutcome record.outcome == "inconclusive"+        "infrastructure-failure" -> renderOutcome record.outcome == "infrastructure-failure"+        _ -> False+      metadataMatches =+        record.runId == renderRunId source.view.comparisonId+          && refs.verdict == source.view.verdict+          && refs.design == source.view.design+          && record.startedAt == utcText source.view.startedAt+          && record.finishedAt == utcText source.view.finishedAt+          && source.view.harnessRevision == Just (let Revision value = record.harnessRevision in value)+          && source.view.harnessDirty == Just record.harnessDirty+          && outcomeMatches+      valid =+        armProblem == Nothing+          && length arms == length paths+          && armIds == expectedIds+          && pathsMatch+          && all (\arm -> arm.record.scenario == record.scenario) arms+          && all ((== "passed") . (.result)) armChecks+          && factorMatches+          && metadataMatches+   in if valid+        then check "cohort-matches-plan" "passed" "comparison identity, arm records and factor values agree with source data"+        else check "cohort-matches-plan" "failed" (maybe "comparison record or an arm contradicts the fetched documents" id armProblem)++checkComparisonRevisions :: Bool -> FilePath -> ComparisonEvidence -> [FetchedArm] -> IO AttestationCheck+checkComparisonRevisions offline scratch record arms = do+  harness <- gitCommitExists Nothing record.harnessRevision+  checked <- forM arms $ \arm -> checkRevisions offline scratch arm.record arm.source+  pure $ combineArmChecks "revisions-resolve" ((if harness then check "revisions-resolve" "passed" "comparison harness commit resolves" else check "revisions-resolve" "skipped" "comparison harness commit does not resolve") : checked)++checkComparisonRecomputation :: FilePath -> [Recomputer] -> FilePath -> ComparisonEvidence -> Maybe ComparisonSource -> IO AttestationCheck+checkComparisonRecomputation _ _ _ _ Nothing = pure (check "verdict-recomputed" "skipped" "comparison document is unavailable")+checkComparisonRecomputation bundle recomputers root record _ = do+  definitions <- computationDefinitions bundle+  let configured =+        [ (handle, lookup handle definitions >>= \(name, version) -> find (\candidate -> candidate.algorithm == name && candidate.algorithmVersion == version) recomputers)+        | handle <- record.computations+        ]+      missing = [handle | (handle, Nothing) <- configured]+  results <- forM [(handle, recomputer) | (handle, Just recomputer) <- configured] $ \(handle, recomputer) -> do+    result <- recomputer.recompute root+    pure (handle, result)+  let expectedVerdict = refsVerdict record.comparison+      contradictions = [handle | (handle, Right result) <- results, not result.agreesWithDocuments || result.comparisonVerdict /= expectedVerdict]+      unavailable = missing <> [handle | (handle, Left _) <- results]+  pure $+    if not (null contradictions)+      then check "verdict-recomputed" "failed" ("comparison recomputation disagrees with the record: " <> Text.intercalate ", " contradictions)+      else+        if null configured+          then check "verdict-recomputed" "skipped" "comparison names no computation definitions"+          else+            if not (null unavailable)+              then check "verdict-recomputed" "skipped" ("recomputer unavailable for " <> Text.intercalate ", " unavailable)+              else check "verdict-recomputed" "passed" "registered comparison recomputer agrees with the source document"+  where+    refsVerdict (Object value) = case KeyMap.lookup "verdict" value of+      Just (String verdict) -> Just verdict+      _ -> Nothing+    refsVerdict _ = Nothing++finishAttestation :: AttestOptions -> FilePath -> Text -> Text -> UTCTime -> Revision -> [Sha256] -> [AttestationCheck] -> IO (Either AttestError AttestResult)+finishAttestation options relative content targetId now attesterRevision matchedDigests checks = do+  let verdict = deriveVerdict checks+      run = "/" <> Text.pack relative+  identifier <- renderRunId <$> newRunId+  let attestation =+        AttestationEvidence+          { title = "Attestation of " <> targetId <> " — " <> verdict,+            description = "The kenshou attester checked the linked data and recorded " <> verdict <> ".",+            generatedAt = utcText now,+            attestationId = identifier,+            run,+            attesterRevision,+            attestedAt = utcText now,+            verdict,+            checks,+            dataDigests = sort (nub matchedDigests),+            exception = fmap (\(authority, reason) -> object ["authority" .= authority, "reason" .= reason]) options.acceptedAnomaly,+            body = "The attester recorded " <> verdict <> " for [the evidence record](" <> run <> ").\n"+          }+  amended <-+    if verdict == "confirmed"+      then appendVerification (options.bundleRoot </> relative) content (utcText now)+      else pure (Right Nothing)+  case amended of+    Left err -> pure (Left err)+    Right oldContent -> do+      written <- writeAttestationRecord options.bundleRoot attestation+      case written of+        Left _ | Just old <- oldContent -> Text.IO.writeFile (options.bundleRoot </> relative) old+        _ -> pure ()+      pure case written of+        Left err -> Left (AttestBundle err)+        Right (RecordCreated path) -> Right (AttestResult path verdict)+        Right (RecordPresent path) -> Right (AttestResult path verdict)++appendVerification :: FilePath -> Text -> Text -> IO (Either AttestError (Maybe Text))+appendVerification path original at = do+  current <- Text.IO.readFile path+  if current /= original+    then pure (Left (AttestIo "run record changed while attesting"))+    else case parseDocument current of+      Left err -> pure (Left (InvalidTarget (Text.pack (show err))))+      Right document -> do+        let actor = ProcessActor "kenshou-attester/0.1.0.0"+            existing = readVerified document.frontmatter+        if any (\entry -> entry.verificationBy == actor) existing+          then pure (Right Nothing)+          else do+            let updated = document {frontmatter = setVerified (existing <> [Verification actor (Just at)]) document.frontmatter}+            Text.IO.writeFile path (serializeDocument updated)+            pure (Right (Just original))++verifyData :: ObjectStore -> Bool -> FilePath -> EvidenceRecord -> IO (AttestationCheck, [Sha256], Maybe RunSource)+verifyData store linkedOnly root record = do+  linked <- forM record.dataLinks (fetchLink store root record.runId)+  let matched = [digest | Right digest <- linked]+      failures = [reason | Left reason <- linked]+  if not (null failures)+    then pure (check "digests-match" "failed" (Text.intercalate "; " failures), matched, Nothing)+    else+      if linkedOnly+        then pure (check "digests-match" "skipped" "only linked objects were checked", matched, Nothing)+        else do+          manifestBytes <- ByteString.readFile (root </> "manifest.json")+          case (eitherDecodeStrict' manifestBytes :: Either String Manifest) of+            Left message -> pure (check "digests-match" "failed" (Text.pack message), matched, Nothing)+            Right manifest -> do+              extra <- forM manifest.files (fetchManifestFile store root record.runId record.dataLinks)+              let allDigests = matched <> [digest | Right (Just digest) <- extra]+                  extraFailures = [reason | Left reason <- extra]+              if not (null extraFailures)+                then pure (check "digests-match" "failed" (Text.intercalate "; " extraFailures), allDigests, Nothing)+                else do+                  loaded <- loadRunSource root+                  case loaded of+                    Left err -> pure (check "digests-match" "failed" (Text.pack (show err)), allDigests, Nothing)+                    Right source -> pure (check "digests-match" "passed" "linked objects and manifest files match their SHA-256 and size", allDigests, Just source)++fetchLink :: ObjectStore -> FilePath -> Text -> DataLink -> IO (Either Text Sha256)+fetchLink store root runId link = case targetPath of+  Nothing -> pure (Left ("unsafe data URI: " <> link.uri))+  Just relative -> do+    let path = root </> relative+    fetched <- store.fetchObject link.uri path+    case fetched of+      Left err -> pure (Left (Text.pack (show err)))+      Right () -> do+        contents <- ByteString.readFile path+        let actual = sha256Bytes contents+        pure $+          if actual == link.digest && fromIntegral (ByteString.length contents) == link.bytes+            then Right actual+            else Left ("object differs from recorded digest or size: " <> link.uri)+  where+    targetPath = if link.kind == CellManifestData then Just "cell-manifest.json" else relativeObjectPath runId link.uri++fetchManifestFile :: ObjectStore -> FilePath -> Text -> [DataLink] -> ManifestFile -> IO (Either Text (Maybe Sha256))+fetchManifestFile store root runId links file+  | not (safeRelative file.path) = pure (Left ("unsafe manifest path: " <> Text.pack file.path))+  | any (\link -> relativeObjectPath runId link.uri == Just file.path) links = pure (Right Nothing)+  | otherwise = case [Text.dropEnd (Text.length "manifest.json") link.uri | link <- links, link.kind == ManifestData] of+      base : _ -> do+        let uri = base <> Text.pack file.path+            path = root </> file.path+        fetched <- store.fetchObject uri path+        case fetched of+          Left err -> pure (Left (Text.pack (show err)))+          Right () -> do+            contents <- ByteString.readFile path+            let digest = sha256Bytes contents+            let Sha256 digestText = digest+            pure $+              if "sha256:" <> digestText == file.sha256 && fromIntegral (ByteString.length contents) == file.bytes+                then Right (Just digest)+                else Left ("manifest object differs: " <> uri)+      [] -> pure (Left "manifest link is missing")++relativeObjectPath :: Text -> Text -> Maybe FilePath+relativeObjectPath runId uri = do+  let marker = "/" <> runId <> "/"+      (prefix, suffix) = Text.breakOnEnd marker uri+      path = Text.unpack suffix+  if Text.null prefix || not (safeRelative path) then Nothing else Just path++safeRelative :: FilePath -> Bool+safeRelative path =+  not (null path)+    && not (isAbsolute path)+    && normalise path == path+    && all (`notElem` ["", ".", ".."]) (splitDirectories path)++checkRevisions :: Bool -> FilePath -> EvidenceRecord -> Maybe RunSource -> IO AttestationCheck+checkRevisions offline scratch record source = do+  harness <- gitCommitExists Nothing record.harnessRevision+  componentChecks <- forM (zip [0 :: Int ..] [component | component <- record.components, component.source == FromGit]) $ \(index, component) -> do+    local <- moriCheckout component.project+    foundLocally <- maybe (pure False) (\path -> maybe (pure False) (gitCommitExists (Just path)) component.revision) local+    if foundLocally+      then pure Nothing+      else+        if offline+          then pure (Just component.package)+          else case (component.revision, source >>= componentLocation component) of+            (Just revision, Just location) -> do+              fetched <- fetchRevision (scratch </> "revision-" <> show index) location revision+              pure (if fetched then Nothing else Just component.package)+            _ -> pure (Just component.package)+  let unresolved = [name | Just name <- componentChecks]+      problems = ["harness revision is absent from this checkout" | not harness] <> ["component revision unresolved: " <> name | name <- unresolved]+  pure $+    if null problems+      then check "revisions-resolve" "passed" "harness and git component commits resolve"+      else check "revisions-resolve" "skipped" (Text.intercalate "; " problems)++componentLocation :: ComponentRef -> RunSource -> Maybe Text+componentLocation component source = do+  cohortComponent <- find (\candidate -> candidate.resolvedComponentMoriUri == component.project) source.result.resultCohort.identityComponents+  package <- find (\candidate -> candidate.resolvedPackageName == component.package) cohortComponent.resolvedComponentPackages+  case package.resolvedPackageSource of+    Cohort.FromGit location _ _ -> Just location+    _ -> Nothing++moriCheckout :: Text -> IO (Maybe FilePath)+moriCheckout project = do+  response <- try (readProcessWithExitCode "mori" ["path", Text.unpack project] "") :: IO (Either IOException (ExitCode, String, String))+  case response of+    Right (ExitSuccess, output, _) -> case reverse (lines output) of+      path : _ -> do+        present <- doesDirectoryExist path+        pure (if present then Just path else Nothing)+      [] -> pure Nothing+    _ -> pure Nothing++gitCommitExists :: Maybe FilePath -> Revision -> IO Bool+gitCommitExists location (Revision revision) = do+  let args = maybe [] (\path -> ["-C", path]) location <> ["cat-file", "-e", Text.unpack revision <> "^{commit}"]+  response <- try (readProcessWithExitCode "git" args "") :: IO (Either IOException (ExitCode, String, String))+  pure $ case response of+    Right (ExitSuccess, _, _) -> True+    _ -> False++fetchRevision :: FilePath -> Text -> Revision -> IO Bool+fetchRevision root location revision@(Revision sha) = do+  prepared <- noPromptGit ["init", "--bare", root]+  if not prepared+    then pure False+    else do+      fetched <- noPromptGit ["-c", "protocol.ext.allow=never", "-c", "protocol.file.allow=never", "-C", root, "fetch", "--depth=1", Text.unpack location, Text.unpack sha]+      if fetched then gitCommitExists (Just root) revision else pure False++noPromptGit :: [String] -> IO Bool+noPromptGit args = do+  environment <- getEnvironment+  let process = (proc "git" args) {env = Just (("GIT_TERMINAL_PROMPT", "0") : filter ((/= "GIT_TERMINAL_PROMPT") . fst) environment)}+  response <- try (readCreateProcessWithExitCode process "") :: IO (Either IOException (ExitCode, String, String))+  pure $ case response of+    Right (ExitSuccess, _, _) -> True+    _ -> False++checkCohort :: UTCTime -> EvidenceRecord -> Maybe RunSource -> AttestationCheck+checkCohort _ _ Nothing = check "cohort-matches-plan" "skipped" "source documents are unavailable"+checkCohort now record (Just source) = case buildRunRecord (RecordInput record.purpose now True (Just (record.subject, record.subjectKind)) record.produced record.previousRun Map.empty Nothing) source record.dataLinks of+  Left err -> check "cohort-matches-plan" "failed" (Text.pack (show err))+  Right expected ->+    let expectationMatches = case source.spec.cohortExpectation of+          Nothing -> True+          Just expectation ->+            expectation.planHash == unPlanHash source.result.resultCohort.identityPlanHash+              && maybe True (== record.cohort) expectation.name+     in if expectationMatches && (record.cohort, record.solverPlanHash, record.components) == (expected.cohort, expected.solverPlanHash, expected.components)+          then check "cohort-matches-plan" "passed" "cohort identity and run-spec expectation agree"+          else check "cohort-matches-plan" "failed" "cohort identity or run-spec expectation differs"++checkEnvironment :: UTCTime -> EvidenceRecord -> Maybe RunSource -> AttestationCheck+checkEnvironment _ _ Nothing = check "environment-captured" "skipped" "source documents are unavailable"+checkEnvironment now record (Just source) = case buildRunRecord (RecordInput record.purpose now True (Just (record.subject, record.subjectKind)) record.produced record.previousRun Map.empty Nothing) source record.dataLinks of+  Left err -> check "environment-captured" "failed" (Text.pack (show err))+  Right expected ->+    if record.environment == expected.environment && record.placement == expected.placement && record.outcome == expected.outcome+      then check "environment-captured" "passed" "recorded environment and effective outcome match the source evidence"+      else check "environment-captured" "failed" "recorded environment, placement or effective outcome differs from the source evidence"++checkClean :: EvidenceRecord -> Maybe RunSource -> Bool -> AttestationCheck+checkClean record source attesterDirty =+  let sourceDirty =+        source+          >>= ( \run -> case run.result.resultFingerprint of+                  Object fields -> case KeyMap.lookup "kenshou" fields of+                    Just (Object kenshou) -> case KeyMap.lookup "dirty" kenshou of Just (Bool value) -> Just value; _ -> Nothing+                    _ -> Nothing+                  _ -> Nothing+              )+   in if record.harnessDirty || sourceDirty == Just True || attesterDirty+        then check "clean-worktree" "failed" "the run or attester worktree is dirty"+        else+          if sourceDirty == Just False+            then check "clean-worktree" "passed" "run and attester worktrees are clean"+            else check "clean-worktree" "skipped" "run dirty state is unavailable"++checkRecomputation :: FilePath -> [Recomputer] -> FilePath -> EvidenceRecord -> Maybe RunSource -> IO AttestationCheck+checkRecomputation _ _ _ _ Nothing = pure (check "verdict-recomputed" "skipped" "source documents are unavailable")+checkRecomputation bundle recomputers root record (Just source) = do+  definitions <- computationDefinitions bundle+  let configured =+        [ (handle, lookup handle definitions >>= \(name, version) -> find (\candidate -> candidate.algorithm == name && candidate.algorithmVersion == version) recomputers)+        | handle <- record.computations+        ]+      missing = [handle | (handle, Nothing) <- configured]+  results <- forM [(handle, recomputer) | (handle, Just recomputer) <- configured] $ \(handle, recomputer) -> do+    result <- recomputer.recompute root+    pure (handle, result)+  let contradictions =+        [ handle+        | (handle, Right result) <- results,+          not result.agreesWithDocuments || maybe False (/= source.result.resultOutcome) result.outcome+        ]+      unavailable = missing <> [handle | (handle, Left _) <- results]+  pure $+    if not (null contradictions)+      then check "verdict-recomputed" "failed" ("recomputation disagrees with the record: " <> Text.intercalate ", " contradictions)+      else+        if null configured+          then check "verdict-recomputed" "skipped" "run names no computation definitions"+          else+            if not (null unavailable)+              then check "verdict-recomputed" "skipped" ("recomputer unavailable for " <> Text.intercalate ", " unavailable)+              else check "verdict-recomputed" "passed" "registered recomputers agree with the source documents"++computationDefinitions :: FilePath -> IO [(Text, (Text, Integer))]+computationDefinitions bundle = do+  let folder = bundle </> "computations"+  names <- ifMDirectory folder listDirectory+  fmap concat $ forM [name | name <- names, takeExtension name == ".md"] $ \name -> do+    content <- Text.IO.readFile (folder </> name)+    pure case parseDocument content of+      Left _ -> []+      Right document -> case (frontmatterLookup "computationId" document.frontmatter, frontmatterLookup "algorithm" document.frontmatter, frontmatterLookup "algorithmVersion" document.frontmatter) of+        (Just (String handle), Just (String algorithm), Just rawVersion) -> case fromJSON rawVersion of+          Success version -> [(handle, (algorithm, version))]+          Error _ -> []+        _ -> []++deriveVerdict :: [AttestationCheck] -> Text+deriveVerdict checks+  | any (\item -> item.result == "failed" && item.name `elem` ["digests-match", "cohort-matches-plan", "verdict-recomputed"]) checks = "refuted"+  | all (\item -> item.result == "passed") checks = "confirmed"+  | otherwise = "incomplete"++check :: Text -> Text -> Text -> AttestationCheck+check name result detail = AttestationCheck name result (Just detail)++currentAttesterRevision :: IO (Either AttestError (Revision, Bool))+currentAttesterRevision = do+  (revisionExit, rawRevision, revisionError) <- readProcessWithExitCode "git" ["rev-parse", "HEAD"] ""+  (statusExit, status, statusError) <- readProcessWithExitCode "git" ["status", "--porcelain"] ""+  pure do+    if revisionExit /= ExitSuccess then Left (AttestIo (Text.pack revisionError)) else Right ()+    if statusExit /= ExitSuccess then Left (AttestIo (Text.pack statusError)) else Right ()+    revision <- either (Left . AttestIo) Right (mkRevision (Text.strip (Text.pack rawRevision)))+    Right (revision, not (null status))++utcText :: UTCTime -> Text+utcText = Text.pack . formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ"
+ src/Kenshou/Evidence/Bundle.hs view
@@ -0,0 +1,185 @@+module Kenshou.Evidence.Bundle+  ( BundleWriteError (..),+    BundleWriteResult (..),+    runRecordPath,+    comparisonRecordPath,+    attestationRecordPath,+    writeRunRecord,+    writeRunRecordWith,+    writeComparisonRecord,+    writeAttestationRecord,+  )+where++import Control.Exception (IOException, try)+import Data.ByteString qualified as ByteString+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Text.Encoding qualified as Text+import Data.Text.IO qualified as Text.IO+import Data.Time (UTCTime, defaultTimeLocale, formatTime, parseTimeM)+import Kenshou.Core.Id (ScenarioId (..), parseRunId, renderLayer, renderScenarioId)+import Kenshou.Core.Outcome (renderOutcome)+import Kenshou.Evidence.Frontmatter (AttestationEvidence (..), ComparisonEvidence (..), EvidenceRecord (..), attestationToDocument, comparisonToDocument, recordToDocument)+import Okf.Bundle (walkBundle, walkBundleInventory, walkLogs)+import Okf.Document (OKFDocument (..), parseDocument, removeField, serializeDocument)+import Okf.Index (readBundleVersion, writeBundleIndexes)+import Okf.Log (Log (..), LogEntry (..), appendLogEntry, parseLog, serializeLog)+import Okf.Profile (compileProfile, loadProfileFile, validateProfileWith)+import Okf.Validation (ValidationProfile (StrictAuthoring), validateBundle, validateBundleLogs)+import System.Directory (createDirectoryIfMissing, doesFileExist, removeFile)+import System.FilePath (takeDirectory, (</>))+import System.IO (hFlush)+import System.IO.Error (isAlreadyExistsError)+import System.IO.Temp (withTempFile)+import System.Posix.Files (createLink)++data BundleWriteError+  = InvalidRecordIdentity !Text+  | RecordConflict !FilePath+  | BundleInvalid !Text+  | BundleIo !Text+  deriving stock (Eq, Show)++data BundleWriteResult = RecordCreated !FilePath | RecordPresent !FilePath+  deriving stock (Eq, Show)++runRecordPath :: EvidenceRecord -> Either BundleWriteError FilePath+runRecordPath record = eventRecordPath record.runId record.scenario record.startedAt++comparisonRecordPath :: ComparisonEvidence -> Either BundleWriteError FilePath+comparisonRecordPath record = eventRecordPath record.runId record.scenario record.startedAt++attestationRecordPath :: AttestationEvidence -> Either BundleWriteError FilePath+attestationRecordPath record = do+  case parseRunId record.attestationId of+    Left reason -> Left (InvalidRecordIdentity reason)+    Right _ -> Right ()+  attested <- maybe (Left (InvalidRecordIdentity "attestedAt must be an RFC 3339 UTC timestamp")) Right (parseUtc record.attestedAt)+  pure $ "attestations" </> formatTime defaultTimeLocale "%Y" attested </> formatTime defaultTimeLocale "%m" attested </> Text.unpack record.attestationId <> ".md"++eventRecordPath :: Text -> ScenarioId -> Text -> Either BundleWriteError FilePath+eventRecordPath eventId scenario startedAt = do+  case parseRunId eventId of+    Left reason -> Left (InvalidRecordIdentity reason)+    Right _ -> Right ()+  started <- maybe (Left (InvalidRecordIdentity "startedAt must be an RFC 3339 UTC timestamp")) Right (parseUtc startedAt)+  pure $ "runs" </> Text.unpack (renderLayer scenario.layer) </> formatTime defaultTimeLocale "%Y" started </> formatTime defaultTimeLocale "%m" started </> Text.unpack eventId <> ".md"++writeRunRecord :: FilePath -> EvidenceRecord -> IO (Either BundleWriteError BundleWriteResult)+writeRunRecord = writeRunRecordWith validateEvidenceBundle++writeRunRecordWith :: (FilePath -> IO (Either BundleWriteError ())) -> FilePath -> EvidenceRecord -> IO (Either BundleWriteError BundleWriteResult)+writeRunRecordWith validate root record = case runRecordPath record of+  Left err -> pure (Left err)+  Right relative -> writeDocument validate root relative (recordToDocument record) (EventIdentity ("Recorded run " <> record.runId <> " (" <> renderScenarioId record.scenario <> ", " <> renderOutcome record.outcome <> ").") record.generatedAt)++writeComparisonRecord :: FilePath -> ComparisonEvidence -> IO (Either BundleWriteError BundleWriteResult)+writeComparisonRecord root record = case comparisonRecordPath record of+  Left err -> pure (Left err)+  Right relative -> writeDocument validateEvidenceBundle root relative (comparisonToDocument record) (EventIdentity ("Recorded comparison " <> record.runId <> " (" <> renderScenarioId record.scenario <> ", " <> renderOutcome record.outcome <> ").") record.generatedAt)++writeAttestationRecord :: FilePath -> AttestationEvidence -> IO (Either BundleWriteError BundleWriteResult)+writeAttestationRecord root record = case attestationRecordPath record of+  Left err -> pure (Left err)+  Right relative -> writeDocument validateEvidenceBundle root relative (attestationToDocument record) (EventIdentity ("Attested " <> record.run <> " (" <> record.verdict <> ").") record.generatedAt)++data EventIdentity = EventIdentity+  { message :: !Text,+    generatedAt :: !Text+  }++writeDocument :: (FilePath -> IO (Either BundleWriteError ())) -> FilePath -> FilePath -> OKFDocument -> EventIdentity -> IO (Either BundleWriteError BundleWriteResult)+writeDocument validate root relative document event = do+  completed <- try (write validate root relative document event) :: IO (Either IOException (Either BundleWriteError BundleWriteResult))+  pure $ either (Left . BundleIo . Text.pack . show) id completed++write :: (FilePath -> IO (Either BundleWriteError ())) -> FilePath -> FilePath -> OKFDocument -> EventIdentity -> IO (Either BundleWriteError BundleWriteResult)+write validate root relative document event = do+  let target = root </> relative+      parent = takeDirectory target+      logPath = parent </> "log.md"+  createDirectoryIfMissing True parent+  oldLog <- do+    exists <- doesFileExist logPath+    if exists then Just <$> Text.IO.readFile logPath else pure Nothing+  let rendered = serializeDocument document+  published <- withTempFile parent ".kenshou-record-" $ \temporary handle -> do+    ByteString.hPut handle (Text.encodeUtf8 rendered)+    hFlush handle+    result <- try (createLink temporary target)+    pure case result of+      Right () -> Right True+      Left err+        | isAlreadyExistsError err -> Right False+        | otherwise -> Left (BundleIo (Text.pack (show (err :: IOException))))+  case published of+    Left err -> pure (Left err)+    Right False -> do+      existing <- Text.IO.readFile target+      pure $ case parseDocument existing of+        Right old | sameFact old document -> Right (RecordPresent relative)+        _ -> Left (RecordConflict relative)+    Right True -> do+      result <- try (finish validate root relative logPath oldLog event) :: IO (Either IOException (Either BundleWriteError ()))+      let rollback err = do+            removeFile target+            restoreLog logPath oldLog+            _ <- writeBundleIndexes root+            pure (Left err)+      case result of+        Right (Right ()) -> pure (Right (RecordCreated relative))+        Left err -> rollback (BundleIo (Text.pack (show err)))+        Right (Left err) -> rollback err++finish :: (FilePath -> IO (Either BundleWriteError ())) -> FilePath -> FilePath -> FilePath -> Maybe Text -> EventIdentity -> IO (Either BundleWriteError ())+finish validate root relative logPath oldLog event = case parseUtc event.generatedAt of+  Nothing -> pure (Left (InvalidRecordIdentity "generatedAt must be an RFC 3339 UTC timestamp"))+  Just generated -> do+    let logTitle = Text.pack (takeDirectory relative) <> " Update Log"+        current = maybe (Log logTitle []) parseLog oldLog+        updated = appendLogEntry (Text.pack (formatTime defaultTimeLocale "%Y-%m-%d" generated)) (LogEntry (Just "Addition") event.message) current+    Text.IO.writeFile logPath (serializeLog updated)+    indexed <- writeBundleIndexes root+    case indexed of+      Left err -> pure (Left (BundleInvalid (Text.pack (show err))))+      Right () -> validate root++restoreLog :: FilePath -> Maybe Text -> IO ()+restoreLog path = \case+  Just previous -> Text.IO.writeFile path previous+  Nothing -> do+    exists <- doesFileExist path+    if exists then removeFile path else pure ()++sameFact :: OKFDocument -> OKFDocument -> Bool+sameFact left right =+  strip left.frontmatter == strip right.frontmatter && left.body == right.body+  where+    strip = removeField "verified" . removeField "generated"++validateEvidenceBundle :: FilePath -> IO (Either BundleWriteError ())+validateEvidenceBundle root = do+  loaded <- loadProfileFile (root </> "profile.dhall")+  case loaded of+    Left err -> pure (Left (BundleInvalid err))+    Right profile -> case compileProfile profile of+      Left errors -> pure (Left (BundleInvalid (Text.pack (show errors))))+      Right compiled -> do+        concepts <- walkBundle root+        inventory <- walkBundleInventory root+        declaration <- readBundleVersion root+        logs <- walkLogs root+        pure $ do+          found <- either (Left . BundleInvalid . Text.pack . show) Right concepts+          files <- either (Left . BundleInvalid . Text.pack . show) Right inventory+          version <- either (Left . BundleInvalid . Text.pack . show) Right declaration+          parsedLogs <- either (Left . BundleInvalid . Text.pack . show) Right logs+          let errors =+                map show (validateBundle StrictAuthoring version files found)+                  <> map show (validateProfileWith files StrictAuthoring compiled found)+                  <> map show (validateBundleLogs parsedLogs)+          if null errors then Right () else Left (BundleInvalid (Text.intercalate "; " (Text.pack <$> errors)))++parseUtc :: Text -> Maybe UTCTime+parseUtc = parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" . Text.unpack
+ src/Kenshou/Evidence/Check.hs view
@@ -0,0 +1,442 @@+module Kenshou.Evidence.Check+  ( CheckOptions (..),+    CheckError (..),+    Finding (..),+    checkBundle,+    checkBundleWithStore,+    checkDocument,+  )+where++import Control.Exception (IOException, try)+import Control.Monad (forM)+import Data.Aeson (Result (..), Value (..), fromJSON)+import Data.Aeson.Key qualified as Key+import Data.Aeson.KeyMap qualified as KeyMap+import Data.ByteString qualified as ByteString+import Data.List (isPrefixOf, nub, sort)+import Data.Map.Strict qualified as Map+import Data.Maybe (mapMaybe)+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Text.IO qualified as Text.IO+import Data.Time (UTCTime, defaultTimeLocale, formatTime, parseTimeM)+import Kenshou.Core.Id (ScenarioId (..), parseRunId, parseScenarioId, renderLayer)+import Kenshou.Evidence.Store (ObjectStat (..), ObjectStore (..), validateObjectUri)+import Kenshou.Evidence.Types (mkRevision, mkSha256, sha256Bytes)+import Okf.Actor (renderActor)+import Okf.Document (OKFDocument (..), Verification (..), frontmatterKeys, frontmatterLookup, parseDocument, readVerified, removeField)+import System.Directory (canonicalizePath, doesDirectoryExist, listDirectory)+import System.Exit (ExitCode (..))+import System.FilePath (makeRelative, splitDirectories, takeExtension, (</>))+import System.IO.Temp (withSystemTempDirectory)+import System.Process (readProcessWithExitCode)++data CheckOptions = CheckOptions+  { bundleRoot :: !FilePath,+    baseRef :: !(Maybe Text),+    network :: !Bool,+    deep :: !Bool+  }+  deriving stock (Eq, Show)++newtype CheckError = CheckError Text deriving stock (Eq, Show)++data Finding = Finding+  { concept :: !FilePath,+    rule :: !Text,+    message :: !Text+  }+  deriving stock (Eq, Show)++checkBundle :: CheckOptions -> IO (Either CheckError [Finding])+checkBundle = checkBundleWithStore Nothing++checkBundleWithStore :: Maybe ObjectStore -> CheckOptions -> IO (Either CheckError [Finding])+checkBundleWithStore store options = do+  result <- try (check store options) :: IO (Either IOException (Either CheckError [Finding]))+  pure $ either (Left . CheckError . Text.pack . show) id result++check :: Maybe ObjectStore -> CheckOptions -> IO (Either CheckError [Finding])+check store options = do+  exists <- doesDirectoryExist options.bundleRoot+  if not exists+    then pure (Left (CheckError "evidence bundle does not exist"))+    else do+      paths <- sort <$> conceptFiles options.bundleRoot+      current <- forM paths $ \path -> do+        content <- Text.IO.readFile (options.bundleRoot </> path)+        pure (path, content)+      let local =+            concatMap+              ( \(path, content) -> case parseDocument content of+                  Left err -> [Finding path "parse" (Text.pack (show err))]+                  Right document -> checkDocument path document+              )+              current+          references = checkReferences current+      historical <- checkHistory options+      remote <-+        if options.network+          then case store of+            Nothing -> pure (Left (CheckError "--network requires an object store and GCP project"))+            Just objectStore -> checkNetwork objectStore options.deep current+          else pure (Right [])+      pure $ do+        historyFindings <- historical+        remoteFindings <- remote+        pure (local <> references <> historyFindings <> remoteFindings)++checkReferences :: [(FilePath, Text)] -> [Finding]+checkReferences current = concatMap inspect parsed <> concatMap attestationConsistency parsed+  where+    parsed = [(path, document) | (path, content) <- current, Right document <- [parseDocument content]]+    byPath = Map.fromList parsed+    field document name = frontmatterLookup name document.frontmatter+    asText (Just (String value)) = Just value+    asText _ = Nothing+    nested name = \case+      Just (Object fields) -> KeyMap.lookup (Key.fromText name) fields+      _ -> Nothing+    paths = \case+      Just (Array values) -> [value | String value <- foldr (:) [] values]+      _ -> []+    target path scenario ordinaryRunOnly ref =+      let relative = Text.unpack (Text.dropWhile (== '/') ref)+       in case Map.lookup relative byPath of+            Nothing -> [Finding path "reference-targets" ("missing run target: " <> ref)]+            Just document | field document "type" /= Just (String "Verification Run") || (ordinaryRunOnly && field document "recordKind" /= Just (String "run")) -> [Finding path "reference-targets" ("target is not a recorded run: " <> ref)]+            Just document -> [Finding path "reference-targets" ("target has a different scenario: " <> ref) | scenario /= Nothing, field document "scenario" /= scenario]+    inspect (path, document)+      | field document "type" == Just (String "Verification Run") =+          let previous = maybe [] (\ref -> target path (field document "scenario") True ref <> checkPrevious path document ref) (asText (field document "previousRun"))+              comparison = field document "comparison"+              arms = paths (nested "baselineRuns" comparison) <> paths (nested "candidateRuns" comparison)+           in previous <> concatMap (target path (field document "scenario") True) arms+      | field document "type" == Just (String "Attestation") =+          maybe [] (target path Nothing False) (asText (field document "run"))+      | otherwise = []+    attestationConsistency (path, document)+      | field document "type" == Just (String "Attestation") =+          let checks = case field document "checks" of+                Just (Array values) -> foldr (:) [] values+                _ -> []+              names = mapMaybe (\value -> asText (nested "name" (Just value))) checks+              required = ["digests-match", "revisions-resolve", "cohort-matches-plan", "verdict-recomputed", "environment-captured", "clean-worktree"]+              outcomes = [(name, asText (nested "result" (Just value))) | value <- checks, Just name <- [asText (nested "name" (Just value))]]+              contradicted = any (\name -> lookup name outcomes == Just (Just "failed")) ["digests-match", "cohort-matches-plan", "verdict-recomputed"]+              complete = all (\(_, result) -> result == Just "passed") outcomes+              expected = if contradicted then "refuted" else if complete then "confirmed" else "incomplete"+              matched = sort names == sort required+              verdictIssue = [Finding path "attestation-consistency" "attestation verdict disagrees with its checks" | matched, field document "verdict" /= Just (String expected)]+              namesIssue = [Finding path "attestation-consistency" "attestation must have all six distinct check names" | not matched]+              runReference = asText (field document "run")+              digestsIssue = case runReference >>= (\ref -> Map.lookup (Text.unpack (Text.dropWhile (== '/') ref)) byPath) of+                Just runDocument+                  | lookup "digests-match" outcomes == Just (Just "passed") ->+                      let recorded = case field runDocument "data" of+                            Just (Array values) -> sort (mapMaybe (\value -> asText (nested "digest" (Just value))) (foldr (:) [] values))+                            _ -> []+                          attested = case field document "dataDigests" of+                            Just (Array values) -> sort [value | String value <- foldr (:) [] values]+                            _ -> []+                       in [Finding path "attestation-consistency" "dataDigests must cover the run's linked data" | not (all (`elem` attested) recorded)]+                _ -> []+              verifiedIssue = case runReference >>= (\ref -> Map.lookup (Text.unpack (Text.dropWhile (== '/') ref)) byPath) of+                Just runDocument+                  | field document "verdict" == Just (String "confirmed") ->+                      let actor = asText (field document "attester")+                          backed = any (\entry -> Just (renderActor entry.verificationBy) == actor) (readVerified runDocument.frontmatter)+                       in [Finding path "attestation-consistency" "confirmed attestation has no matching verified entry on the run" | not backed]+                _ -> []+           in namesIssue <> verdictIssue <> digestsIssue <> verifiedIssue+      | field document "type" == Just (String "Verification Run") =+          let processEntries = [(renderActor entry.verificationBy, entry.verificationAt) | entry <- readVerified document.frontmatter, "process:" `Text.isPrefixOf` renderActor entry.verificationBy]+              matching actor at = any (\(_, attestation) -> field attestation "type" == Just (String "Attestation") && field attestation "run" == Just (String ("/" <> Text.pack path)) && field attestation "verdict" == Just (String "confirmed") && field attestation "attester" == Just (String actor) && field attestation "attestedAt" == (String <$> at)) parsed+           in [Finding path "attestation-consistency" "verified process entry has no matching confirmed attestation" | (actor, at) <- processEntries, not (matching actor at)]+      | otherwise = []+    checkPrevious path document ref =+      let relative = Text.unpack (Text.dropWhile (== '/') ref)+       in case Map.lookup relative byPath of+            Nothing -> []+            Just previous ->+              [ Finding path "reference-targets" "previousRun must have the same scenario and compatibility key, and start earlier"+              | field document "scenario" /= field previous "scenario"+                  || field document "compatibilityKey" /= field previous "compatibilityKey"+                  || maybe+                    True+                    not+                    ( do+                        now <- asText (field document "startedAt") >>= parseUtc+                        thenTime <- asText (field previous "startedAt") >>= parseUtc+                        pure (thenTime < now)+                    )+              ]++checkNetwork :: ObjectStore -> Bool -> [(FilePath, Text)] -> IO (Either CheckError [Finding])+checkNetwork store deep current = withSystemTempDirectory "kenshou-evidence-network" $ \scratch -> do+  results <- forM current $ \(path, content) -> case parseDocument content of+    Left _ -> pure (Right [])+    Right document -> do+      let links = case frontmatterLookup "data" document.frontmatter of+            Just (Array values) -> foldr (:) [] values+            _ -> []+      fmap (fmap concat . sequence) $ forM links $ \link -> case networkLink link of+        Nothing -> pure (Right [])+        Just (uri, digest, expectedBytes) -> do+          observed <- store.statObject uri+          case observed of+            Left err -> pure (Left (CheckError (Text.pack (show err))))+            Right Nothing -> pure (Right [Finding path "network" ("object is missing: " <> uri)])+            Right (Just stat) ->+              if stat.bytes /= expectedBytes || stat.recordedSha256 /= Just digest+                then pure (Right [Finding path "network" ("object metadata differs: " <> uri)])+                else+                  if not deep+                    then pure (Right [])+                    else do+                      let destination = scratch </> "object"+                      fetched <- store.fetchObject uri destination+                      case fetched of+                        Left err -> pure (Left (CheckError (Text.pack (show err))))+                        Right () -> do+                          actual <- sha256Bytes <$> ByteString.readFile destination+                          pure (Right [Finding path "network" ("object bytes differ: " <> uri) | actual /= digest])+  pure (concat <$> sequence results)+  where+    networkLink (Object fields) = do+      String uri <- KeyMap.lookup "uri" fields+      String rawDigest <- KeyMap.lookup "digest" fields+      digest <- either (const Nothing) Just (mkSha256 rawDigest)+      rawBytes <- KeyMap.lookup "bytes" fields+      bytes <- case fromJSON rawBytes of+        Success parsed -> Just parsed+        Error _ -> Nothing+      pure (uri, digest, bytes)+    networkLink _ = Nothing++conceptFiles :: FilePath -> IO [FilePath]+conceptFiles root = do+  let descend relative = do+        let directory = root </> relative+        names <- listDirectory directory+        fmap concat $ forM names $ \name -> do+          let child = relative </> name+          isDirectory <- doesDirectoryExist (root </> child)+          if isDirectory then descend child else pure [child | takeExtension name == ".md", name /= "index.md", name /= "log.md"]+  runs <- doesDirectoryExist (root </> "runs")+  attestations <- doesDirectoryExist (root </> "attestations")+  (if runs then descend "runs" else pure []) >>= \runFiles ->+    (if attestations then descend "attestations" else pure []) >>= \attestationFiles ->+      pure (runFiles <> attestationFiles)++checkDocument :: FilePath -> OKFDocument -> [Finding]+checkDocument path document =+  let front = document.frontmatter+      field = (`frontmatterLookup` front)+      issue name detail = Finding path name detail+      textField name = case field name of+        Just (String value) -> Just value+        _ -> Nothing+      requireText name = [issue "string-typing" (name <> " must be a JSON string") | textField name == Nothing]+      digest name = case textField name of+        Nothing -> []+        Just value -> [issue "hex-shape" (name <> " must be 64 lowercase hexadecimal characters") | either (const True) (const False) (mkSha256 value)]+      revision name = case textField name of+        Nothing -> []+        Just value -> [issue "hex-shape" (name <> " must be 40 lowercase hexadecimal characters") | either (const True) (const False) (mkRevision value)]+      entries name = case field name of+        Just (Array values) -> toList values+        _ -> []+      member name value = case value of+        Object fields -> KeyMap.lookup (Key.fromText name) fields+        _ -> Nothing+      entryText name value = case member name value of+        Just (String result) -> Just result+        _ -> Nothing+      run = field "type" == Just (String "Verification Run")+      attestation = field "type" == Just (String "Attestation")+      common =+        [issue "event-keys" (name <> " is forbidden on an event") | (run || attestation), name <- ["status", "stale_after"], field name /= Nothing]+      runChecks =+        requireText "runId"+          <> requireText "scenario"+          <> requireText "startedAt"+          <> requireText "finishedAt"+          <> [issue "id-shape" "runId must be a UUIDv7" | maybe True (either (const True) (const False) . parseRunId) (textField "runId")]+          <> pathChecks textField issue+          <> timeChecks textField issue+          <> requireText "harnessRevision"+          <> revision "harnessRevision"+          <> [issue "dirty-purpose" "a dirty harness requires purpose: investigation" | field "harnessDirty" == Just (Bool True), field "purpose" /= Just (String "investigation")]+          <> concatMap+            ( \value ->+                [issue "string-typing" "data.uri must be a JSON string" | entryText "uri" value == Nothing]+                  <> [issue "data-uri" "data.uri must use a durable gs:// object URI" | Just uri <- [entryText "uri" value], either (const True) (const False) (validateObjectUri uri)]+                  <> [issue "hex-shape" "data.digest must be 64 lowercase hexadecimal characters" | maybe True (either (const True) (const False) . mkSha256) (entryText "digest" value)]+            )+            (entries "data")+          <> (if field "recordKind" == Just (String "run") then runOnly else [])+          <> (if field "recordKind" == Just (String "comparison") then comparisonOnly else [])+      runOnly =+        requireText "compatibilityKey"+          <> requireText "solverPlanHash"+          <> digest "compatibilityKey"+          <> digest "solverPlanHash"+          <> concatMap+            ( \value ->+                [issue "string-typing" "dimension.value must be a JSON string" | entryText "value" value == Nothing]+                  <> [issue "string-typing" "dimension.name must be a JSON string" | entryText "name" value == Nothing]+            )+            (entries "dimensions")+          <> concatMap+            ( \value ->+                [issue "hex-shape" "component.revision must be 40 lowercase hexadecimal characters" | maybe False (either (const True) (const False) . mkRevision) (entryText "revision" value)]+            )+            (entries "components")+          <> [issue "data-completeness" "run must link manifest, run-spec and run-result" | field "recordKind" == Just (String "run"), not (all (`elem` mapMaybe (entryText "kind") (entries "data")) ["manifest", "run-spec", "run-result"])]+          <> [issue "data-completeness" "cell run must link exactly one outer cell manifest" | field "recordKind" == Just (String "run"), field "placement" == Just (String "cell"), length (filter (== Just "cell-manifest") (map (entryText "kind") (entries "data"))) /= 1]+          <> [ issue "cell-fields" "cell placement requires environment.cell and cellRun"+             | field "placement" == Just (String "cell"),+               not+                 ( all+                     ( \name -> case field "environment" of+                         Just environment -> maybe False isString (member name environment)+                         _ -> False+                     )+                     ["cell", "cellRun"]+                 )+             ]+      comparisonOnly =+        let comparison = field "comparison"+            verdict = comparison >>= member "verdict"+            expected = case verdict of+              Just (String "pass") -> Just (String "passed")+              Just (String "regression") -> Just (String "failed")+              Just (String "inconclusive") -> Just (String "inconclusive")+              Just (String "infrastructure-failure") -> Just (String "infrastructure-failure")+              _ -> Nothing+         in [issue "comparison-outcome" "comparison verdict and outcome disagree" | expected /= field "outcome"]+              <> [issue "data-completeness" "comparison must link exactly one comparison document" | mapMaybe (entryText "kind") (entries "data") /= ["comparison"]]+      attestationChecks =+        [issue "event-keys" (name <> " belongs only on runs") | name <- ["layer", "tier"], field name /= Nothing]+          <> requireText "attestationId"+          <> requireText "attesterRevision"+          <> requireText "attestedAt"+          <> revision "attesterRevision"+          <> [issue "id-shape" "attestationId must be a UUIDv7" | maybe True (either (const True) (const False) . parseRunId) (textField "attestationId")]+          <> ( case (textField "attestationId", textField "attestedAt") of+                 (Just identifier, Just at) -> case parseUtc at of+                   Just time ->+                     let expected = "attestations/" <> formatTime defaultTimeLocale "%Y/%m" time <> "/" <> Text.unpack identifier <> ".md"+                      in [issue "path-consistency" ("expected " <> Text.pack expected) | path /= expected]+                   Nothing -> [issue "time-order" "attestedAt must be RFC 3339 UTC"]+                 _ -> []+             )+          <> [issue "hex-shape" "dataDigests must be 64 lowercase hexadecimal characters" | value <- entries "dataDigests", case value of String digestValue -> either (const True) (const False) (mkSha256 digestValue); _ -> True]+   in common <> (if run then runChecks else []) <> (if attestation then attestationChecks else [])+  where+    isString (String _) = True+    isString _ = False+    toList = foldr (:) []+    pathChecks textField issue = case (textField "scenario", textField "runId", textField "startedAt") of+      (Just scenario, Just runId, Just startedAt) -> case (parseScenarioId scenario, parseUtc startedAt) of+        (Right parsed, Just time) ->+          let expected = "runs/" <> Text.unpack (renderLayer parsed.layer) <> "/" <> formatTime defaultTimeLocale "%Y/%m" time <> "/" <> Text.unpack runId <> ".md"+           in [issue "path-consistency" ("expected " <> Text.pack expected) | path /= expected]+        _ -> []+      _ -> []+    timeChecks textField issue = case (textField "startedAt", textField "finishedAt") of+      (Just start, Just finish) -> case (parseUtc start, parseUtc finish) of+        (Just from, Just to) -> [issue "time-order" "finishedAt precedes startedAt" | to < from]+        _ -> [issue "time-order" "run timestamps must be RFC 3339 UTC"]+      _ -> []++parseUtc :: Text -> Maybe UTCTime+parseUtc = parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" . Text.unpack++checkHistory :: CheckOptions -> IO (Either CheckError [Finding])+checkHistory options = do+  gitRoot <- readProcessWithExitCode "git" ["-C", options.bundleRoot, "rev-parse", "--show-toplevel"] ""+  case gitRoot of+    (ExitFailure _, _, _) -> pure (Right [])+    (ExitSuccess, root, _) -> do+      repo <- canonicalizePath (stripNewline root)+      bundle <- canonicalizePath options.bundleRoot+      let relative = makeRelative repo bundle+      if [".."] `isPrefixOf` splitDirectories relative+        then pure (Right [])+        else do+          shallow <- readProcessWithExitCode "git" ["-C", repo, "rev-parse", "--is-shallow-repository"] ""+          if shallow /= (ExitSuccess, "false\n", "")+            then pure (Left (CheckError "immutability requires full Git history"))+            else do+              let scope = [relative </> "runs", relative </> "attestations"]+                  range = maybe "HEAD" ((<> "..HEAD") . Text.unpack) options.baseRef+              history <- readProcessWithExitCode "git" (["-C", repo, "log", "--format=%H", "--diff-filter=DMR", "--name-status", range, "--"] <> scope) ""+              case history of+                (ExitFailure _, _, err) -> pure (Left (CheckError (Text.pack err)))+                (ExitSuccess, output, _) -> do+                  let changed = [(commit, columns) | (commit, columns) <- historyLines output, any (\item -> takeExtension item == ".md" && not (isGenerated item)) (drop 1 columns)]+                  committed <- fmap concat $ forM changed $ \(commit, columns) -> case columns of+                    status : path : _ -> do+                      let relativePath = makeRelative relative path+                      if "D" `isPrefixOf` status || "R" `isPrefixOf` status+                        then pure [Finding relativePath "immutability" ("committed deletion or rename in " <> Text.pack commit)]+                        else compareVersions repo relativePath (commit <> "^:" <> path) (commit <> ":" <> path)+                    _ -> pure []+                  worktree <- readProcessWithExitCode "git" (["-C", repo, "diff", "--name-status", "HEAD", "--"] <> scope) ""+                  staged <- case worktree of+                    (ExitSuccess, changes, _) -> fmap concat $ forM (lines changes) $ \line -> case words line of+                      status : path : _+                        | takeExtension path == ".md",+                          not (isGenerated path) ->+                            let relativePath = makeRelative relative path+                             in if "A" `isPrefixOf` status+                                  then pure []+                                  else+                                    if "D" `isPrefixOf` status || "R" `isPrefixOf` status+                                      then pure [Finding relativePath "immutability" "committed event was deleted or renamed in the working tree"]+                                      else compareVersions repo relativePath ("HEAD:" <> path) path+                      _ -> pure []+                    _ -> pure []+                  pure (Right (committed <> staged))+  where+    stripNewline = reverse . dropWhile (== '\n') . reverse+    isGenerated path = any (`elem` ["index.md", "log.md"]) (take 1 (reverse (splitDirectories path)))+    historyLines = go Nothing . lines+      where+        go _ [] = []+        go current (line : rest)+          | length line == 40 && all (`elem` ("0123456789abcdef" :: String)) line = go (Just line) rest+          | otherwise = case (current, words line) of+              (Just commit, columns@(_ : _)) -> (commit, columns) : go current rest+              _ -> go current rest+    compareVersions repo relativePath oldRef newRef = do+      old <- readVersion repo oldRef+      new <- readVersion repo newRef+      pure case (old, new) of+        (Just before, Just after) -> case (parseDocument before, parseDocument after) of+          (Right a, Right b) | allowedAppend a b -> []+          (Right a, Right b) -> [Finding relativePath "immutability" ("committed event changed: " <> Text.intercalate ", " (changedParts a b))]+          _ -> [Finding relativePath "immutability" ("committed event changed: " <> Text.pack newRef)]+        _ -> [Finding relativePath "immutability" ("committed event changed: " <> Text.pack newRef)]+    readVersion repo ref+      | ':' `elem` ref = do+          (status, output, _) <- readProcessWithExitCode "git" ["-C", repo, "show", ref] ""+          pure (if status == ExitSuccess then Just (Text.pack output) else Nothing)+      | otherwise = do+          result <- try (Text.IO.readFile (repo </> ref)) :: IO (Either IOException Text)+          pure (either (const Nothing) Just result)+    allowedAppend before after =+      before.body == after.body+        && removeVerified before == removeVerified after+        && verified before `isPrefixOf` verified after+    removeVerified document = removeField "verified" document.frontmatter+    verified document = case frontmatterLookup "verified" document.frontmatter of+      Just (Array values) -> foldr (:) [] values+      _ -> []+    changedParts before after =+      [name | name <- sort (nub (frontmatterKeys before.frontmatter <> frontmatterKeys after.frontmatter)), name /= "verified", frontmatterLookup name before.frontmatter /= frontmatterLookup name after.frontmatter]+        <> ["body" | before.body /= after.body]+        <> ["verified" | not (verified before `isPrefixOf` verified after)]
+ src/Kenshou/Evidence/Cli.hs view
@@ -0,0 +1,355 @@+module Kenshou.Evidence.Cli (recordCommand, attestCommand, attestCommandWith, evidenceCommand) where++import Control.Exception (IOException, try)+import Data.Aeson (encode, object, (.=))+import Data.ByteString.Lazy.Char8 qualified as LazyByteString+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Text.IO qualified as Text.IO+import Kenshou.Core.Bundle (registrySubjects)+import Kenshou.Core.Cli (CliCommand (..), CliEnv (..), CliGroup (Evidence))+import Kenshou.Core.Cli.Config (ConfigInputs (..), configInputsParser)+import Kenshou.Evidence.Attest (AttestError (..), AttestOptions (..), AttestResult (..), Recomputer, attest, coreRecomputers)+import Kenshou.Evidence.Bundle (BundleWriteError (..))+import Kenshou.Evidence.Check (CheckError (..), CheckOptions (..), Finding (..), checkBundleWithStore)+import Kenshou.Evidence.Config (EvidenceDefaults (..), bundleRootKey, dataBaseUriKey, evidenceConfig, projectKey, resolveEvidenceDefaults)+import Kenshou.Evidence.Publish (PublishError (..), UploadMode (..))+import Kenshou.Evidence.Record (RecordError (..), RecordOptions (..), RecordOutcome (..), recordComparison, recordRun)+import Kenshou.Evidence.Source (SourceError (..))+import Kenshou.Evidence.Store (ObjectStore, StoreError (..), directoryStore, gcloudStore)+import Kenshou.Evidence.Types (Purpose (..), SubjectKind (..))+import Options.Applicative+import Settei (ResolveResult (..), describe, renderErrorsText)+import Settei.Env (envSnapshot)+import Settei.Optparse (namedOption, resolutionDiagnostic, schemaDiagnostic)+import System.Directory (canonicalizePath, doesDirectoryExist)+import System.Environment (getEnvironment, lookupEnv)+import System.Exit (ExitCode (..))+import System.FilePath (isAbsolute, makeRelative, splitDirectories)+import System.IO (hIsTerminalDevice, stderr, stdin)+import System.Process (readProcess)++data RecordCli = RecordCli+  { runDirectory :: !(Maybe FilePath),+    comparisonFile :: !(Maybe FilePath),+    config :: !ConfigInputs,+    purpose :: !Purpose,+    storeRoot :: !(Maybe FilePath),+    verifyOnly :: !Bool,+    deepVerify :: !Bool,+    allowDirty :: !Bool,+    linkLogs :: !Bool,+    subject :: !(Maybe Text),+    subjectKind :: !SubjectKind,+    produced :: ![Text],+    json :: !Bool+  }++recordCommand :: CliCommand+recordCommand = CliCommand "record" "Publish a finished run or comparison as an OKF evidence record" Evidence False (recordHandler <$> recordParser)++attestCommand :: CliCommand+attestCommand = attestCommandWith coreRecomputers++attestCommandWith :: [Recomputer] -> CliCommand+attestCommandWith recomputers = CliCommand "attest" "Verify a recorded run or comparison and append an attestation" Evidence False (attestHandler recomputers <$> attestParser)++data AttestCli = AttestCli+  { target :: !Text,+    config :: !ConfigInputs,+    storeRoot :: !(Maybe FilePath),+    offline :: !Bool,+    linkedOnly :: !Bool,+    acceptAnomaly :: !Bool,+    authority :: !(Maybe Text),+    reason :: !(Maybe Text),+    json :: !Bool+  }++attestParser :: Parser AttestCli+attestParser =+  AttestCli+    <$> parserOptionGroup "Evidence source" (Text.pack <$> strArgument (metavar "RECORD-OR-ID" <> help "Bundle path or ID of a run or comparison record"))+    <*> configInputsParser+      ( (\bundle project -> [bundle, project])+          <$> namedOption "--bundle" bundleRootKey (long "bundle" <> metavar "DIR" <> help "OKF evidence bundle")+          <*> namedOption "--project" projectKey (long "project" <> metavar "PROJECT" <> help "GCP project for object retrieval")+      )+    <*> parserOptionGroup "Evidence source" (optional (strOption (long "store-root" <> metavar "DIR" <> internal <> help "Scratch object store")))+    <*> parserOptionGroup "Recomputation" (switch (long "offline" <> help "Skip revision resolution requiring network"))+    <*> parserOptionGroup "Recomputation" (switch (long "linked-only" <> help "Fetch linked objects without manifest expansion"))+    <*> parserOptionGroup "Anomaly acceptance" (switch (long "accept-anomaly" <> help "Record a human acceptance without changing the computed verdict"))+    <*> parserOptionGroup "Anomaly acceptance" (optional (Text.pack <$> strOption (long "authority" <> metavar "human:ID" <> help "Human accepting the anomaly")))+    <*> parserOptionGroup "Anomaly acceptance" (optional (Text.pack <$> strOption (long "reason" <> metavar "TEXT" <> help "Reason for accepting the anomaly")))+    <*> parserOptionGroup "Output" (switch (long "json" <> help "Emit one JSON attestation result"))++attestHandler :: [Recomputer] -> AttestCli -> CliEnv -> IO ExitCode+attestHandler recomputers options _+  | Just output <- schemaDiagnostic options.config.diagnostic (describe evidenceConfig) = Text.IO.putStr output >> pure ExitSuccess+  | otherwise = do+      processEnvironment <- fmap (fmap (\(name, setting) -> (Text.pack name, Text.pack setting))) getEnvironment+      resolved <- resolveEvidenceDefaults (envSnapshot processEnvironment) options.config+      case resolved of+        Left message -> attestResponse options.json 2 message Nothing+        Right result -> case result.answer of+          Left problems -> attestResponse options.json 2 (renderErrorsText problems) Nothing+          Right defaults -> case resolutionDiagnostic options.config.diagnostic result of+            Just output -> Text.IO.putStr output >> pure ExitSuccess+            Nothing -> do+              acceptance <- validateAnomaly options+              case acceptance of+                Left message -> attestResponse options.json 2 message Nothing+                Right accepted -> do+                  selected <- case options.storeRoot of+                    Just root -> do+                      allowed <- scratchBundleOutsideRepo (Text.unpack defaults.bundleRoot)+                      pure $ if allowed then Right (directoryStore root) else Left "--store-root requires --bundle outside this repository"+                    Nothing -> pure $ case defaults.project of+                      Just project | not (Text.null project) -> Right (gcloudStore project)+                      _ -> Left "--project is required for GCS storage"+                  case selected of+                    Left message -> attestResponse options.json 2 message Nothing+                    Right store -> do+                      attested <- attest store recomputers (AttestOptions (Text.unpack defaults.bundleRoot) options.offline options.linkedOnly accepted) options.target+                      case attested of+                        Left err -> attestResponse options.json (attestExitCode err) (Text.pack (show err)) Nothing+                        Right observed -> attestResponse options.json (case observed.verdict of "confirmed" -> 0; "refuted" -> 1; _ -> 3) observed.verdict (Just observed.path)++validateAnomaly :: AttestCli -> IO (Either Text (Maybe (Text, Text)))+validateAnomaly options+  | not options.acceptAnomaly =+      pure $+        if options.authority == Nothing && options.reason == Nothing+          then Right Nothing+          else Left "--authority and --reason require --accept-anomaly"+  | otherwise = do+      ci <- lookupEnv "CI"+      terminal <- hIsTerminalDevice stdin+      pure $+        if ci /= Nothing || not terminal+          then Left "--accept-anomaly requires an interactive terminal outside CI"+          else case (options.authority, options.reason) of+            (Just authority, Just reason)+              | Just identifier <- Text.stripPrefix "human:" authority,+                not (Text.null identifier),+                not (Text.null (Text.strip reason)) ->+                  Right (Just (authority, reason))+            _ -> Left "--accept-anomaly requires --authority human:ID and a nonempty --reason"++attestExitCode :: AttestError -> Int+attestExitCode = \case+  InvalidTarget _ -> 2+  AttestIo _ -> 4+  AttestBundle _ -> 1++attestResponse :: Bool -> Int -> Text -> Maybe FilePath -> IO ExitCode+attestResponse machine code message path = do+  if machine+    then LazyByteString.putStrLn (encode (object (["schema" .= ("kenshou.attest-result/v1" :: Text), "status" .= (if path == Nothing then "error" else message), "message" .= message, "exitCode" .= code] <> maybe [] (\item -> ["path" .= item]) path)))+    else case path of+      Just item -> Text.IO.putStrLn (message <> ": " <> Text.pack item)+      Nothing -> Text.IO.hPutStrLn stderr ("kenshou attest: " <> message)+  if machine && code /= 0 && path == Nothing+    then Text.IO.hPutStrLn stderr ("kenshou attest: " <> message)+    else pure ()+  pure $ if code == 0 then ExitSuccess else ExitFailure code++evidenceCommand :: CliCommand+evidenceCommand =+  CliCommand "evidence" "Check the historic OKF evidence bundle" Evidence False $+    hsubparser (command "check" (info (checkHandler <$> checkParser) (progDesc "Check record identity, local rules and immutability")))++data CheckCli = CheckCli+  { config :: !ConfigInputs,+    baseRef :: !(Maybe Text),+    network :: !Bool,+    deep :: !Bool,+    json :: !Bool+  }++checkParser :: Parser CheckCli+checkParser =+  CheckCli+    <$> configInputsParser+      ( (\bundle project -> [bundle, project])+          <$> namedOption "--bundle" bundleRootKey (long "bundle" <> metavar "DIR" <> help "OKF evidence bundle")+          <*> namedOption "--project" projectKey (long "project" <> metavar "PROJECT" <> help "GCP project for --network")+      )+    <*> parserOptionGroup "History scope" (optional (Text.pack <$> strOption (long "base" <> metavar "GIT-REF" <> help "Check committed changes from this base through HEAD")))+    <*> parserOptionGroup "Verification" (switch (long "network" <> help "Check recorded objects in GCS"))+    <*> parserOptionGroup "Verification" (switch (long "deep" <> help "Download objects and verify SHA-256"))+    <*> parserOptionGroup "Output" (switch (long "json" <> help "Emit one JSON result document"))++checkHandler :: CheckCli -> CliEnv -> IO ExitCode+checkHandler options _+  | options.deep && not options.network = checkResponse options.json 2 "--deep requires --network" []+  | Just output <- schemaDiagnostic options.config.diagnostic (describe evidenceConfig) = Text.IO.putStr output >> pure ExitSuccess+  | otherwise = do+      processEnvironment <- fmap (fmap (\(name, setting) -> (Text.pack name, Text.pack setting))) getEnvironment+      resolved <- resolveEvidenceDefaults (envSnapshot processEnvironment) options.config+      case resolved of+        Left message -> checkResponse options.json 2 message []+        Right result -> case result.answer of+          Left problems -> checkResponse options.json 2 (renderErrorsText problems) []+          Right defaults -> case resolutionDiagnostic options.config.diagnostic result of+            Just output -> Text.IO.putStr output >> pure ExitSuccess+            Nothing -> case (options.network, defaults.project) of+              (True, Nothing) -> checkResponse options.json 2 "--network requires --project or gcp.project" []+              _ -> do+                let store = if options.network then gcloudStore <$> defaults.project else Nothing+                checked <- checkBundleWithStore store (CheckOptions (Text.unpack defaults.bundleRoot) options.baseRef options.network options.deep)+                case checked of+                  Left (CheckError message) -> checkResponse options.json 4 message []+                  Right findings -> checkResponse options.json (if null findings then 0 else 1) (if null findings then "evidence clean" else "evidence findings") findings++checkResponse :: Bool -> Int -> Text -> [Finding] -> IO ExitCode+checkResponse machine code message findings = do+  if machine+    then do+      LazyByteString.putStrLn (encode (object ["schema" .= ("kenshou.evidence-check/v1" :: Text), "status" .= (if code == 0 then "ok" else "error" :: Text), "message" .= message, "exitCode" .= code, "findings" .= map findingValue findings]))+      mapM_ (Text.IO.hPutStrLn stderr . renderFinding) findings+    else+      if null findings+        then Text.IO.putStrLn message+        else mapM_ (Text.IO.hPutStrLn stderr . renderFinding) findings+  if code == 0+    then pure ExitSuccess+    else do+      if null findings then Text.IO.hPutStrLn stderr ("kenshou evidence check: " <> message) else pure ()+      pure (ExitFailure code)+  where+    findingValue finding = object ["concept" .= finding.concept, "rule" .= finding.rule, "message" .= finding.message]+    renderFinding finding = Text.pack finding.concept <> ": " <> finding.rule <> ": " <> finding.message++recordParser :: Parser RecordCli+recordParser =+  RecordCli+    <$> parserOptionGroup "Record source" (optional (strArgument (metavar "RUN-DIR" <> help "Finished kenshou run directory")))+    <*> parserOptionGroup "Record source" (optional (strOption (long "comparison" <> metavar "FILE" <> help "Finished kenshou comparison document")))+    <*> configInputsParser+      ( (\bundle project baseUri -> [bundle, project, baseUri])+          <$> namedOption "--bundle" bundleRootKey (long "bundle" <> metavar "DIR" <> help "OKF evidence bundle")+          <*> namedOption "--project" projectKey (long "project" <> metavar "PROJECT" <> help "Explicit GCP project")+          <*> namedOption "--data-base-uri" dataBaseUriKey (long "data-base-uri" <> metavar "gs://BUCKET/PREFIX" <> help "Durable object prefix")+      )+    <*> parserOptionGroup "Evidence destination" (option (eitherReader parsePurpose) (long "purpose" <> metavar "nightly|release|baseline|investigation" <> help "Why this run is recorded"))+    <*> parserOptionGroup "Evidence destination" (optional (strOption (long "store-root" <> metavar "DIR" <> internal <> help "Scratch object store")))+    <*> parserOptionGroup "Verification" (switch (long "verify-only" <> help "Require every object to exist; upload none"))+    <*> parserOptionGroup "Verification" (switch (long "deep-verify" <> help "Download published objects and compare SHA-256"))+    <*> parserOptionGroup "Verification" (switch (long "allow-dirty" <> help "Record a dirty harness as investigation evidence"))+    <*> parserOptionGroup "Verification" (switch (long "link-logs" <> help "Link logs even for passing runs"))+    <*> parserOptionGroup "Record source" (optional (Text.pack <$> strOption (long "subject" <> metavar "MORI-URI" <> help "Canonical subject override")))+    <*> parserOptionGroup "Record source" (option (eitherReader parseSubjectKind) (long "subject-kind" <> metavar "project|package" <> value SubjectProject <> showDefaultWith (const "project") <> help "Kind of --subject"))+    <*> parserOptionGroup "Record source" (many (Text.pack <$> strOption (long "produced" <> metavar "MORI-URI" <> help "Report artifact produced by this run")))+    <*> parserOptionGroup "Output" (switch (long "json" <> help "Emit one JSON result document"))++recordHandler :: RecordCli -> CliEnv -> IO ExitCode+recordHandler options environment+  | options.subject == Nothing && options.subjectKind == SubjectPackage = respond options.json 2 "--subject-kind package requires --subject" Nothing+  | (options.runDirectory == Nothing) == (options.comparisonFile == Nothing) = respond options.json 2 "provide exactly one RUN-DIR or --comparison FILE" Nothing+  | Just output <- schemaDiagnostic options.config.diagnostic (describe evidenceConfig) = Text.IO.putStr output >> pure ExitSuccess+  | otherwise = do+      processEnvironment <- fmap (fmap (\(name, value) -> (Text.pack name, Text.pack value))) getEnvironment+      resolved <- resolveEvidenceDefaults (envSnapshot processEnvironment) options.config+      case resolved of+        Left message -> respond options.json 2 message Nothing+        Right result -> case result.answer of+          Left problems -> respond options.json 2 (renderErrorsText problems) Nothing+          Right defaults -> case resolutionDiagnostic options.config.diagnostic result of+            Just output -> Text.IO.putStr output >> pure ExitSuccess+            Nothing -> case defaults.dataBaseUri of+              Nothing -> respond options.json 2 "--data-base-uri or evidence.data-base-uri is required" Nothing+              Just baseUri -> do+                selected <- selectStore options defaults+                case selected of+                  Left message -> respond options.json 2 message Nothing+                  Right store -> do+                    if options.allowDirty+                      then Text.IO.hPutStrLn stderr "kenshou record: warning: --allow-dirty permits investigation-only evidence"+                      else pure ()+                    let mode = if options.verifyOnly then VerifyOnly else UploadMissing+                        recordOptions = RecordOptions (Text.unpack defaults.bundleRoot) baseUri options.purpose mode options.deepVerify options.allowDirty options.linkLogs ((,options.subjectKind) <$> options.subject) options.produced (registrySubjects environment.registry) defaults.defaultSubject+                    recorded <- case (options.runDirectory, options.comparisonFile) of+                      (Just directory, Nothing) -> recordRun store recordOptions directory+                      (Nothing, Just file) -> recordComparison store recordOptions file+                      _ -> pure (Left (RecordError "provide exactly one RUN-DIR or --comparison FILE"))+                    case recorded of+                      Left err -> respond options.json (recordExitCode err) (recordMessage err) Nothing+                      Right (Recorded path) -> respond options.json 0 "recorded" (Just path)+                      Right (AlreadyRecorded path) -> respond options.json 0 "already recorded" (Just path)++selectStore :: RecordCli -> EvidenceDefaults -> IO (Either Text ObjectStore)+selectStore options defaults = case options.storeRoot of+  Just root -> do+    allowed <- scratchBundleOutsideRepo (Text.unpack defaults.bundleRoot)+    pure $ if allowed then Right (directoryStore root) else Left "--store-root requires --bundle outside this repository"+  Nothing -> pure $ case defaults.project of+    Just project | not (Text.null project) -> Right (gcloudStore project)+    _ -> Left "--project is required for GCS storage"++scratchBundleOutsideRepo :: FilePath -> IO Bool+scratchBundleOutsideRepo bundle = do+  exists <- doesDirectoryExist bundle+  if not exists+    then pure False+    else do+      result <-+        try+          ( do+              repo <- Text.unpack . Text.strip . Text.pack <$> readProcess "git" ["rev-parse", "--show-toplevel"] ""+              repoPath <- canonicalizePath repo+              bundlePath <- canonicalizePath bundle+              let relative = makeRelative repoPath bundlePath+              pure $+                isAbsolute relative || case splitDirectories relative of+                  ".." : _ -> True+                  _ -> False+          ) ::+          IO (Either IOException Bool)+      pure (either (const False) id result)++respond :: Bool -> Int -> Text -> Maybe FilePath -> IO ExitCode+respond machine code message path = do+  if machine+    then LazyByteString.putStrLn (encode (object (["schema" .= ("kenshou.record-result/v1" :: Text), "status" .= (if code == 0 then "ok" else "error" :: Text), "message" .= message, "exitCode" .= code] <> maybe [] (\value -> ["path" .= value]) path)))+    else case path of+      Just value -> Text.IO.putStrLn (message <> ": " <> Text.pack value)+      Nothing -> pure ()+  if code == 0 then pure ExitSuccess else Text.IO.hPutStrLn stderr ("kenshou record: " <> message) >> pure (ExitFailure code)++recordExitCode :: RecordError -> Int+recordExitCode = \case+  RecordError _ -> 2+  SourceFailure _ -> 1+  PublishFailure err -> case err of+    InvalidBaseUri _ -> 2+    PublishIo _ -> 4+    StoreFailure (StoreIo _) -> 4+    StoreFailure _ -> 1+    MissingObject _ -> 1+    ObjectMismatch _ -> 1+  BundleFailure err -> case err of+    InvalidRecordIdentity _ -> 2+    RecordConflict _ -> 1+    BundleInvalid _ -> 1+    BundleIo _ -> 4++recordMessage :: RecordError -> Text+recordMessage = \case+  RecordError message -> message+  SourceFailure (SourceError message) -> message+  PublishFailure (InvalidBaseUri uri) -> "data base URI must use gs://BUCKET/PREFIX; received " <> uri+  PublishFailure err -> Text.pack (show err)+  BundleFailure err -> Text.pack (show err)++parsePurpose :: String -> Either String Purpose+parsePurpose "nightly" = Right Nightly+parsePurpose "release" = Right Release+parsePurpose "baseline" = Right Baseline+parsePurpose "investigation" = Right Investigation+parsePurpose other = Left ("unknown purpose " <> other)++parseSubjectKind :: String -> Either String SubjectKind+parseSubjectKind "project" = Right SubjectProject+parseSubjectKind "package" = Right SubjectPackage+parseSubjectKind other = Left ("unknown subject kind " <> other)
+ src/Kenshou/Evidence/Config.hs view
@@ -0,0 +1,72 @@+module Kenshou.Evidence.Config+  ( EvidenceDefaults (..),+    evidenceConfig,+    bundleRootKey,+    projectKey,+    dataBaseUriKey,+    defaultSubjectKey,+    bundleUriKey,+    resolveEvidenceDefaults,+  )+where++import Data.Map.Strict qualified as Map+import Data.Text (Text)+import Data.Text qualified as Text+import Kenshou.Core.Cli.Config (ConfigInputs (..), loadConfigSources)+import Settei+import Settei.Env++data EvidenceDefaults = EvidenceDefaults+  { bundleRoot :: !Text,+    project :: !(Maybe Text),+    dataBaseUri :: !(Maybe Text),+    -- | The evidence subject for runs whose layer's bundle declares none.+    defaultSubject :: !(Maybe Text),+    -- | The canonical Mori URI of the evidence bundle that history documents cite.+    bundleUri :: !(Maybe Text)+  }+  deriving stock (Eq, Show)++evidenceConfig :: Config EvidenceDefaults+evidenceConfig =+  EvidenceDefaults+    <$> required (publicSetting bundleRootKey "OKF verification bundle" textDecoder)+    <*> optional (publicSetting projectKey "GCP project for durable evidence" textDecoder)+    <*> optional (publicSetting dataBaseUriKey "GCS prefix for run data" textDecoder)+    <*> optional (publicSetting defaultSubjectKey "Mori URI recorded as the subject of runs whose layer declares none" textDecoder)+    <*> optional (publicSetting bundleUriKey "Mori URI of the evidence bundle cited by history documents" textDecoder)++resolveEvidenceDefaults :: EnvSnapshot -> ConfigInputs -> IO (Either Text (ResolveResult EvidenceDefaults))+resolveEvidenceDefaults snapshot inputs = do+  loaded <- loadConfigSources inputs {namedSources = []}+  pure $ do+    files <- loaded+    pure (resolve defaultResolveOptions {unknownKeyPolicy = RejectUnknownKeys} ([builtIns] <> files <> [environmentSource environmentBindings snapshot] <> inputs.namedSources) evidenceConfig)++environmentBindings :: Bindings+environmentBindings =+  either+    (error . Text.unpack . renderEnvErrorsText)+    id+    ( bindings+        [ binding (EnvName "KENSHOU_EVIDENCE_BUNDLE") bundleRootKey,+          binding (EnvName "KENSHOU_GCP_PROJECT") projectKey,+          binding (EnvName "KENSHOU_EVIDENCE_DATA_BASE_URI") dataBaseUriKey,+          binding (EnvName "KENSHOU_EVIDENCE_SUBJECT") defaultSubjectKey,+          binding (EnvName "KENSHOU_EVIDENCE_BUNDLE_URI") bundleUriKey+        ]+    )++builtIns :: Source+builtIns = source "kenshou built-ins" BuiltInSource (RawObject (Map.singleton "evidence" (RawObject (Map.singleton "bundle-root" (RawText "docs/verification")))))++bundleRootKey, projectKey, dataBaseUriKey, defaultSubjectKey, bundleUriKey :: Key+bundleRootKey = validKey "evidence.bundle-root"+projectKey = validKey "gcp.project"+dataBaseUriKey = validKey "evidence.data-base-uri"+defaultSubjectKey = validKey "evidence.default-subject"+bundleUriKey = validKey "evidence.bundle-uri"++validKey :: Text -> Key+validKey value = either (error . show) id (parseKey value)
+ src/Kenshou/Evidence/Frontmatter.hs view
@@ -0,0 +1,402 @@+module Kenshou.Evidence.Frontmatter+  ( EvidenceRecord (..),+    ComparisonEvidence (..),+    AttestationEvidence (..),+    AttestationCheck (..),+    FieldError (..),+    recordToDocument,+    comparisonToDocument,+    attestationToDocument,+    recordFromDocument,+    comparisonFromDocument,+  )+where++import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (..), fromJSON, object, withObject, withText, (.:), (.:?), (.=))+import Data.Aeson.Types (Parser)+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Word (Word64)+import Kenshou.Core.Id (ScenarioId (..), renderKind, renderLayer, renderScenarioId, unSegment)+import Kenshou.Core.Outcome (Outcome, renderOutcome)+import Kenshou.Evidence.Types (ComponentRef (..), ComponentSource (..), DataKind (..), DataLink (..), Purpose (..), Revision (..), Sha256 (..), SubjectKind (..), mkRevision, mkSha256)+import Okf.Actor (Actor (ProcessActor, ProducerActor), parseActor)+import Okf.Document (Generated (..), OKFDocument (..), OkfCommon (..), frontmatterLookup, okfCommon, readGenerated, setField, setGenerated)++newtype FieldError = FieldError Text deriving stock (Eq, Show)++data EvidenceRecord = EvidenceRecord+  { title :: !Text,+    description :: !Text,+    generatedAt :: !Text,+    runId :: !Text,+    purpose :: !Purpose,+    scenario :: !ScenarioId,+    tier :: !Text,+    placement :: !Text,+    outcome :: !Outcome,+    startedAt :: !Text,+    finishedAt :: !Text,+    subject :: !Text,+    subjectKind :: !SubjectKind,+    harnessRevision :: !Revision,+    harnessDirty :: !Bool,+    computations :: ![Text],+    dataLinks :: ![DataLink],+    cohort :: !Text,+    solverPlanHash :: !Sha256,+    components :: ![ComponentRef],+    environment :: !Value,+    seed :: !Word64,+    compatibilityKey :: !Sha256,+    knobs :: ![(Text, Value)],+    dimensions :: ![(Text, Text)],+    knownDefects :: ![Text],+    produced :: ![Text],+    previousRun :: !(Maybe Text),+    body :: !Text+  }+  deriving stock (Eq, Show)++data ComparisonEvidence = ComparisonEvidence+  { title :: !Text,+    description :: !Text,+    generatedAt :: !Text,+    runId :: !Text,+    purpose :: !Purpose,+    scenario :: !ScenarioId,+    tier :: !Text,+    placement :: !Text,+    outcome :: !Outcome,+    startedAt :: !Text,+    finishedAt :: !Text,+    subject :: !Text,+    subjectKind :: !SubjectKind,+    harnessRevision :: !Revision,+    harnessDirty :: !Bool,+    computations :: ![Text],+    dataLinks :: ![DataLink],+    comparison :: !Value,+    body :: !Text+  }+  deriving stock (Eq, Show)++data AttestationCheck = AttestationCheck+  { name :: !Text,+    result :: !Text,+    detail :: !(Maybe Text)+  }+  deriving stock (Eq, Show)++instance ToJSON AttestationCheck where+  toJSON check = object (["name" .= check.name, "result" .= check.result] <> maybe [] (\detail -> ["detail" .= detail]) check.detail)++data AttestationEvidence = AttestationEvidence+  { title :: !Text,+    description :: !Text,+    generatedAt :: !Text,+    attestationId :: !Text,+    run :: !Text,+    attesterRevision :: !Revision,+    attestedAt :: !Text,+    verdict :: !Text,+    checks :: ![AttestationCheck],+    dataDigests :: ![Sha256],+    exception :: !(Maybe Value),+    body :: !Text+  }+  deriving stock (Eq, Show)++attestationToDocument :: AttestationEvidence -> OKFDocument+attestationToDocument record =+  OKFDocument+    { frontmatter = foldl (\front (name, value) -> setField name value front) generated fields,+      body = record.body+    }+  where+    generated =+      setGenerated+        Generated {generatedBy = ProcessActor "kenshou-attester/0.1.0.0", generatedAt = Just record.generatedAt}+        (okfCommon OkfCommon {commonType = "Attestation", commonTitle = Just record.title, commonDescription = Just record.description, commonTimestamp = Nothing})+    fields =+      [ ("attestationId", String record.attestationId),+        ("run", String record.run),+        ("attester", String "process:kenshou-attester/0.1.0.0"),+        ("attesterRevision", toJSON record.attesterRevision),+        ("attestedAt", String record.attestedAt),+        ("verdict", String record.verdict),+        ("checks", toJSON record.checks),+        ("dataDigests", toJSON record.dataDigests)+      ]+        <> maybe [] (\value -> [("exception", value)]) record.exception++comparisonToDocument :: ComparisonEvidence -> OKFDocument+comparisonToDocument record =+  OKFDocument+    { frontmatter = foldl (\front (name, value) -> setField name value front) generated fields,+      body = record.body+    }+  where+    generated =+      setGenerated+        Generated {generatedBy = ProducerActor "kenshou-record" "0.1.0.0", generatedAt = Just record.generatedAt}+        (okfCommon OkfCommon {commonType = "Verification Run", commonTitle = Just record.title, commonDescription = Just record.description, commonTimestamp = Nothing})+    scenarioId = record.scenario+    fields =+      [ ("runId", toJSON record.runId),+        ("recordKind", String "comparison"),+        ("purpose", String (purposeText record.purpose)),+        ("scenario", String (renderScenarioId scenarioId)),+        ("layer", String (renderLayer scenarioId.layer)),+        ("component", String (unSegment scenarioId.component)),+        ("kind", String (renderKind scenarioId.kind)),+        ("tier", String record.tier),+        ("placement", String record.placement),+        ("outcome", String (renderOutcome record.outcome)),+        ("startedAt", String record.startedAt),+        ("finishedAt", String record.finishedAt),+        ("subject", String record.subject),+        ("subjectKind", String (subjectKindText record.subjectKind)),+        ("harnessRevision", toJSON record.harnessRevision),+        ("harnessDirty", Bool record.harnessDirty),+        ("computations", toJSON record.computations),+        ("data", toJSON record.dataLinks),+        ("comparison", record.comparison)+      ]++recordToDocument :: EvidenceRecord -> OKFDocument+recordToDocument record =+  OKFDocument+    { frontmatter = foldl (\front (name, value) -> setField name value front) generated fields,+      body = record.body+    }+  where+    generated =+      setGenerated+        Generated {generatedBy = ProducerActor "kenshou-record" "0.1.0.0", generatedAt = Just record.generatedAt}+        (okfCommon OkfCommon {commonType = "Verification Run", commonTitle = Just record.title, commonDescription = Just record.description, commonTimestamp = Nothing})+    scenarioId = record.scenario+    fields =+      [ ("runId", toJSON record.runId),+        ("recordKind", String "run"),+        ("purpose", String (purposeText record.purpose)),+        ("scenario", String (renderScenarioId scenarioId)),+        ("layer", String (renderLayer scenarioId.layer)),+        ("component", String (unSegment scenarioId.component)),+        ("kind", String (renderKind scenarioId.kind)),+        ("tier", String record.tier),+        ("placement", String record.placement),+        ("outcome", String (renderOutcome record.outcome)),+        ("startedAt", String record.startedAt),+        ("finishedAt", String record.finishedAt),+        ("subject", String record.subject),+        ("subjectKind", String (subjectKindText record.subjectKind)),+        ("harnessRevision", toJSON record.harnessRevision),+        ("harnessDirty", Bool record.harnessDirty),+        ("computations", toJSON record.computations),+        ("data", toJSON record.dataLinks),+        ("cohort", String record.cohort),+        ("solverPlanHash", toJSON record.solverPlanHash),+        ("components", toJSON record.components),+        ("environment", record.environment),+        ("seed", toJSON record.seed),+        ("compatibilityKey", toJSON record.compatibilityKey)+      ]+        <> optionalList "knobs" [object ["name" .= name, "value" .= value] | (name, value) <- record.knobs]+        <> optionalList "dimensions" [object ["name" .= name, "value" .= value] | (name, value) <- record.dimensions]+        <> optionalList "knownDefects" record.knownDefects+        <> optionalList "produced" record.produced+        <> maybe [] (\path -> [("previousRun", String path)]) record.previousRun++    optionalList name values = [(name, toJSON values) | not (null values)]++recordFromDocument :: OKFDocument -> Either [FieldError] EvidenceRecord+recordFromDocument document = do+  let front = document.frontmatter+      required :: Text -> Either [FieldError] Value+      required name = maybe (Left [FieldError ("missing " <> name)]) Right (frontmatterLookup name front)+      typed :: (FromJSON value) => Text -> Either [FieldError] value+      typed name =+        required name >>= \value -> case fromJSON value of+          Error message -> Left [FieldError (name <> ": " <> Text.pack message)]+          Success decoded -> Right decoded+      optional :: (FromJSON value) => Text -> Either [FieldError] (Maybe value)+      optional name = case frontmatterLookup name front of+        Nothing -> Right Nothing+        Just value -> case fromJSON value of+          Error message -> Left [FieldError (name <> ": " <> Text.pack message)]+          Success decoded -> Right (Just decoded)+  recordType <- typed "type"+  if (recordType :: Text) /= "Verification Run" then Left [FieldError "type must be Verification Run"] else pure ()+  kind <- typed "recordKind"+  if (kind :: Text) /= "run" then Left [FieldError "recordKind must be run"] else pure ()+  title <- typed "title"+  description <- typed "description"+  generatedAt <- case readGenerated front of+    Just Generated {generatedBy, generatedAt = Just at}+      | generatedBy == parseActor "kenshou-record/0.1.0.0" -> Right at+    _ -> Left [FieldError "generated must name kenshou-record/0.1.0.0 and a time"]+  runId <- typed "runId"+  purpose <- typed "purpose"+  scenario <- typed "scenario"+  tier <- typed "tier"+  placement <- typed "placement"+  outcome <- typed "outcome"+  startedAt <- typed "startedAt"+  finishedAt <- typed "finishedAt"+  subject <- typed "subject"+  subjectKind <- typed "subjectKind"+  harnessRevision <- typed "harnessRevision"+  harnessDirty <- typed "harnessDirty"+  computations <- typed "computations"+  dataLinks <- typed "data"+  cohort <- typed "cohort"+  solverPlanHash <- typed "solverPlanHash"+  components <- typed "components"+  environment <- typed "environment"+  seed <- typed "seed"+  compatibilityKey <- typed "compatibilityKey"+  rawKnobs <- maybe [] id <$> optional "knobs"+  rawDimensions <- maybe [] id <$> optional "dimensions"+  let knobs = [(name, value) | NamedValue name value <- rawKnobs]+      dimensions = [(name, value) | NamedText name value <- rawDimensions]+  knownDefects <- maybe [] id <$> optional "knownDefects"+  produced <- maybe [] id <$> optional "produced"+  previousRun <- optional "previousRun"+  pure EvidenceRecord {title, description, generatedAt, runId, purpose, scenario, tier, placement, outcome, startedAt, finishedAt, subject, subjectKind, harnessRevision, harnessDirty, computations, dataLinks, cohort, solverPlanHash, components, environment, seed, compatibilityKey, knobs, dimensions, knownDefects, produced, previousRun, body = document.body}++comparisonFromDocument :: OKFDocument -> Either [FieldError] ComparisonEvidence+comparisonFromDocument document = do+  let front = document.frontmatter+      typed :: (FromJSON value) => Text -> Either [FieldError] value+      typed name = case frontmatterLookup name front of+        Nothing -> Left [FieldError ("missing " <> name)]+        Just value -> case fromJSON value of+          Error message -> Left [FieldError (name <> ": " <> Text.pack message)]+          Success decoded -> Right decoded+  recordType <- typed "type"+  if (recordType :: Text) /= "Verification Run" then Left [FieldError "type must be Verification Run"] else pure ()+  kind <- typed "recordKind"+  if (kind :: Text) /= "comparison" then Left [FieldError "recordKind must be comparison"] else pure ()+  generatedAt <- case readGenerated front of+    Just Generated {generatedBy, generatedAt = Just at}+      | generatedBy == parseActor "kenshou-record/0.1.0.0" -> Right at+    _ -> Left [FieldError "generated must name kenshou-record/0.1.0.0 and a time"]+  title <- typed "title"+  description <- typed "description"+  runId <- typed "runId"+  purpose <- typed "purpose"+  scenario <- typed "scenario"+  tier <- typed "tier"+  placement <- typed "placement"+  outcome <- typed "outcome"+  startedAt <- typed "startedAt"+  finishedAt <- typed "finishedAt"+  subject <- typed "subject"+  subjectKind <- typed "subjectKind"+  harnessRevision <- typed "harnessRevision"+  harnessDirty <- typed "harnessDirty"+  computations <- typed "computations"+  dataLinks <- typed "data"+  comparison <- typed "comparison"+  pure ComparisonEvidence {title, description, generatedAt, runId, purpose, scenario, tier, placement, outcome, startedAt, finishedAt, subject, subjectKind, harnessRevision, harnessDirty, computations, dataLinks, comparison, body = document.body}++purposeText :: Purpose -> Text+purposeText Nightly = "nightly"+purposeText Release = "release"+purposeText Baseline = "baseline"+purposeText Investigation = "investigation"++subjectKindText :: SubjectKind -> Text+subjectKindText SubjectProject = "project"+subjectKindText SubjectPackage = "package"++dataKindText :: DataKind -> Text+dataKindText RunSpecData = "run-spec"+dataKindText RunResultData = "run-result"+dataKindText ManifestData = "manifest"+dataKindText CellManifestData = "cell-manifest"+dataKindText SamplesData = "samples"+dataKindText SeriesData = "series"+dataKindText VerdictsData = "verdicts"+dataKindText DiagnosisData = "diagnosis"+dataKindText LogsData = "logs"+dataKindText ComparisonData = "comparison"++instance ToJSON Sha256 where toJSON (Sha256 digest) = String digest++instance FromJSON Sha256 where+  parseJSON = withText "SHA-256" (either (fail . Text.unpack) pure . mkSha256)++instance ToJSON Revision where toJSON (Revision revision) = String revision++instance FromJSON Revision where+  parseJSON = withText "revision" (either (fail . Text.unpack) pure . mkRevision)++instance FromJSON Purpose where+  parseJSON = withText "purpose" \case+    "nightly" -> pure Nightly+    "release" -> pure Release+    "baseline" -> pure Baseline+    "investigation" -> pure Investigation+    _ -> fail "unknown purpose"++instance FromJSON SubjectKind where+  parseJSON = withText "subject kind" \case+    "project" -> pure SubjectProject+    "package" -> pure SubjectPackage+    _ -> fail "unknown subject kind"++instance ToJSON DataLink where+  toJSON link = object ["kind" .= dataKindText link.kind, "uri" .= link.uri, "digest" .= link.digest, "mediaType" .= link.mediaType, "bytes" .= link.bytes]++instance FromJSON DataLink where+  parseJSON = withObject "data link" \value -> do+    rawKind <- value .: "kind" :: Parser Text+    kind <- parseKind rawKind+    DataLink kind <$> value .: "uri" <*> value .: "digest" <*> value .: "mediaType" <*> value .: "bytes"+    where+      parseKind = \case+        "run-spec" -> pure RunSpecData+        "run-result" -> pure RunResultData+        "manifest" -> pure ManifestData+        "cell-manifest" -> pure CellManifestData+        "samples" -> pure SamplesData+        "series" -> pure SeriesData+        "verdicts" -> pure VerdictsData+        "diagnosis" -> pure DiagnosisData+        "logs" -> pure LogsData+        "comparison" -> pure ComparisonData+        _ -> fail "unknown data kind"++instance ToJSON ComponentRef where+  toJSON component =+    object $+      ["project" .= component.project, "package" .= component.package, "version" .= component.version, "source" .= sourceText component.source]+        <> maybe [] (\revision -> ["revision" .= revision]) component.revision+    where+      sourceText FromHackage = "hackage" :: Text+      sourceText FromGit = "git"++instance FromJSON ComponentRef where+  parseJSON = withObject "component" \value -> do+    project <- value .: "project"+    package <- value .: "package"+    version <- value .: "version"+    rawSource <- value .: "source" :: Parser Text+    source <- case rawSource of+      "hackage" -> pure FromHackage+      "git" -> pure FromGit+      _ -> fail "unknown component source"+    revision <- value .:? "revision"+    pure ComponentRef {project, package, version, source, revision}++data NamedValue = NamedValue Text Value++instance FromJSON NamedValue where+  parseJSON = withObject "name/value" \value -> NamedValue <$> value .: "name" <*> value .: "value"++data NamedText = NamedText Text Text++instance FromJSON NamedText where+  parseJSON = withObject "name/value" \value -> NamedText <$> value .: "name" <*> value .: "value"
+ src/Kenshou/Evidence/History.hs view
@@ -0,0 +1,183 @@+module Kenshou.Evidence.History+  ( HistoryQuery (..),+    HistoryError (..),+    HistoryDocument (..),+    HistoryEntry (..),+    history,+    deriveBaseline,+  )+where++import Data.Aeson (ToJSON (..), Value (..), object, (.=))+import Data.Aeson.Key qualified as Key+import Data.Aeson.KeyMap qualified as KeyMap+import Data.List (sortOn)+import Data.Map.Strict (Map)+import Data.Map.Strict qualified as Map+import Data.Maybe (mapMaybe)+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Time (Day, defaultTimeLocale, formatTime)+import Kenshou.Core.Id (ScenarioId, renderScenarioId)+import Kenshou.Core.Outcome (Outcome, renderOutcome)+import Okf.Actor (renderActor)+import Okf.Bundle (Concept, conceptDocument, conceptSourcePath, conceptType, walkBundle)+import Okf.Document (OKFDocument (..), Verification (..), frontmatterKeys, frontmatterLookup, readVerified)+import System.FilePath (dropExtension)++data HistoryQuery = HistoryQuery+  { scenario :: !ScenarioId,+    cohortComponent :: !(Maybe (Text, Maybe Text)),+    outcomes :: ![Outcome],+    since :: !(Maybe Day),+    confirmedOnly :: !Bool+  }+  deriving stock (Eq, Show)++newtype HistoryError = HistoryError Text deriving stock (Eq, Show)++data HistoryDocument = HistoryDocument+  { -- | The canonical Mori URI of the evidence bundle the entries live in.+    bundleUri :: !Text,+    query :: !HistoryQuery,+    entries :: ![HistoryEntry]+  }+  deriving stock (Eq, Show)++data HistoryEntry = HistoryEntry+  { concept :: !Text,+    record :: !Value,+    trust :: !Text,+    attestations :: ![Value],+    derivedBaseline :: !(Maybe Text),+    startedAt :: !Text,+    scenario :: !Text,+    compatibilityKey :: !(Maybe Text),+    outcome :: !Text,+    recordKind :: !Text+  }+  deriving stock (Eq, Show)++instance ToJSON HistoryDocument where+  toJSON document =+    object+      [ "schema" .= ("kenshou.evidence-history/v1" :: Text),+        "bundle" .= document.bundleUri,+        "query" .= queryValue document.query,+        "entries" .= fmap (entryValue document.bundleUri) document.entries+      ]++entryValue :: Text -> HistoryEntry -> Value+entryValue bundleUri entry =+  object+    [ "concept" .= entry.concept,+      "ref" .= (bundleUri <> "/concepts/" <> entry.concept),+      "record" .= entry.record,+      "trust" .= entry.trust,+      "attestations" .= entry.attestations,+      "derivedBaseline" .= entry.derivedBaseline+    ]++queryValue :: HistoryQuery -> Value+queryValue query =+  object+    ( ["scenario" .= renderScenarioId query.scenario]+        <> maybe [] (\(name, version) -> ["cohortComponent" .= maybe name (\value -> name <> "=" <> value) version]) query.cohortComponent+        <> ["outcomes" .= map renderOutcome query.outcomes | not (null query.outcomes)]+        <> maybe [] (\day -> ["since" .= Text.pack (formatTime defaultTimeLocale "%Y-%m-%d" day)]) query.since+        <> ["confirmedOnly" .= True | query.confirmedOnly]+    )++history :: Text -> FilePath -> HistoryQuery -> IO (Either HistoryError HistoryDocument)+history bundleUri root query = do+  walked <- walkBundle root+  pure do+    concepts <- either (Left . HistoryError . Text.pack . show) Right walked+    let attested = attestationIndex concepts+        allEntries = sortOn (\entry -> (entry.startedAt, entry.concept)) (mapMaybe (runEntry attested) concepts)+        withBaselines = [entry {derivedBaseline = deriveBaseline allEntries entry} | entry <- allEntries]+        selected = filter (matches query) withBaselines+    Right (HistoryDocument bundleUri query selected)++deriveBaseline :: [HistoryEntry] -> HistoryEntry -> Maybe Text+deriveBaseline entries current+  | current.recordKind /= "run" = Nothing+  | otherwise = case reverse (sortOn (\entry -> (entry.startedAt, entry.concept)) candidates) of+      best : _ -> Just best.concept+      [] -> Nothing+  where+    candidates =+      [ entry+      | entry <- entries,+        entry.recordKind == "run",+        entry.scenario == current.scenario,+        entry.compatibilityKey /= Nothing,+        entry.compatibilityKey == current.compatibilityKey,+        entry.startedAt < current.startedAt,+        entry.outcome == "passed",+        hasConfirmation entry.attestations+      ]++matches :: HistoryQuery -> HistoryEntry -> Bool+matches query entry =+  entry.scenario == renderScenarioId query.scenario+    && (null query.outcomes || entry.outcome `elem` map renderOutcome query.outcomes)+    && maybe True (\day -> Text.take 10 entry.startedAt >= Text.pack (formatTime defaultTimeLocale "%Y-%m-%d" day)) query.since+    && (not query.confirmedOnly || entry.trust `elem` ["machine-confirmed", "human-reviewed"])+    && maybe True (hasComponent entry.record) query.cohortComponent++hasComponent :: Value -> (Text, Maybe Text) -> Bool+hasComponent record (package, version) = case member "components" record of+  Just (Array components) -> any matchesComponent components+  _ -> False+  where+    matchesComponent component =+      member "package" component == Just (String package)+        && maybe True (\wanted -> member "version" component == Just (String wanted) || member "revision" component == Just (String wanted)) version++runEntry :: Map Text [Value] -> Concept -> Maybe HistoryEntry+runEntry index concept+  | conceptType concept /= "Verification Run" = Nothing+  | otherwise = do+      let front = (conceptDocument concept).frontmatter+          path = Text.pack (conceptSourcePath concept)+          conceptId = Text.pack (dropExtension (conceptSourcePath concept))+          attested = Map.findWithDefault [] ("/" <> path) index+      String startedAt <- frontmatterLookup "startedAt" front+      String scenario <- frontmatterLookup "scenario" front+      String outcome <- frontmatterLookup "outcome" front+      String recordKind <- frontmatterLookup "recordKind" front+      let record = Object (KeyMap.fromList [(Key.fromText name, value) | name <- frontmatterKeys front, Just value <- [frontmatterLookup name front]])+          compatibilityKey = case frontmatterLookup "compatibilityKey" front of Just (String key) -> Just key; _ -> Nothing+          humanReviewed = any (Text.isPrefixOf "human:" . renderActor . (.verificationBy)) (readVerified front)+          confirmed = hasConfirmation attested+          trust = if humanReviewed then "human-reviewed" else if confirmed then "machine-confirmed" else "unverified"+      pure HistoryEntry {concept = conceptId, record, trust, attestations = attested, derivedBaseline = Nothing, startedAt, scenario, compatibilityKey, outcome, recordKind}++hasConfirmation :: [Value] -> Bool+hasConfirmation attestations =+  any+    (\confirmedAt -> all (\attestation -> textMember "verdict" attestation /= Just "refuted" || textMember "attestedAt" attestation <= Just confirmedAt) attestations)+    [at | attestation <- attestations, textMember "verdict" attestation == Just "confirmed", Just at <- [textMember "attestedAt" attestation]]++attestationIndex :: [Concept] -> Map Text [Value]+attestationIndex concepts = Map.map (sortOn (textMember "attestedAt")) (Map.fromListWith (<>) [(reference, [summary]) | (reference, summary) <- mapMaybe attestation concepts])+  where+    attestation concept+      | conceptType concept /= "Attestation" = Nothing+      | otherwise = do+          let front = (conceptDocument concept).frontmatter+          String run <- frontmatterLookup "run" front+          String verdict <- frontmatterLookup "verdict" front+          String attestedAt <- frontmatterLookup "attestedAt" front+          let conceptId = Text.pack (dropExtension (conceptSourcePath concept))+          pure (run, object ["concept" .= conceptId, "verdict" .= verdict, "attestedAt" .= attestedAt])++member :: Text -> Value -> Maybe Value+member name (Object fields) = KeyMap.lookup (Key.fromText name) fields+member _ _ = Nothing++textMember :: Text -> Value -> Maybe Text+textMember name value = case member name value of+  Just (String result) -> Just result+  _ -> Nothing
+ src/Kenshou/Evidence/HistoryCli.hs view
@@ -0,0 +1,92 @@+module Kenshou.Evidence.HistoryCli (historyCommand) where++import Data.Aeson (Value (String), encode)+import Data.Aeson qualified as Aeson+import Data.ByteString.Lazy.Char8 qualified as LazyByteString+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Text.IO qualified as Text.IO+import Data.Time (Day, defaultTimeLocale, parseTimeM)+import Kenshou.Core.Cli (CliCommand (..), CliEnv, CliGroup (Analysis))+import Kenshou.Core.Cli.Config (ConfigInputs (..), configInputsParser)+import Kenshou.Core.Id (ScenarioId, parseScenarioId)+import Kenshou.Core.Outcome (Outcome)+import Kenshou.Evidence.Config (EvidenceDefaults (..), bundleRootKey, bundleUriKey, evidenceConfig, resolveEvidenceDefaults)+import Kenshou.Evidence.History (HistoryDocument (..), HistoryEntry (..), HistoryError (..), HistoryQuery (..), history)+import Options.Applicative+import Settei (ResolveResult (..), describe, renderErrorsText)+import Settei.Env (envSnapshot)+import Settei.Optparse (namedOption, resolutionDiagnostic, schemaDiagnostic)+import System.Environment (getEnvironment)+import System.Exit (ExitCode (..))+import System.IO (stderr)++data HistoryCli = HistoryCli+  { config :: !ConfigInputs,+    scenario :: !ScenarioId,+    cohortComponent :: !(Maybe (Text, Maybe Text)),+    outcomes :: ![Outcome],+    since :: !(Maybe Day),+    confirmedOnly :: !Bool,+    json :: !Bool+  }++historyCommand :: CliCommand+historyCommand = CliCommand "history" "Read verification evidence for a scenario" Analysis False (historyHandler <$> historyParser)++historyParser :: Parser HistoryCli+historyParser =+  HistoryCli+    <$> configInputsParser+      ( (\bundle bundleUri -> [bundle, bundleUri])+          <$> namedOption "--bundle" bundleRootKey (long "bundle" <> metavar "DIR" <> help "OKF evidence bundle")+          <*> namedOption "--bundle-uri" bundleUriKey (long "bundle-uri" <> metavar "MORI_URI" <> help "Canonical Mori URI of the evidence bundle")+      )+    <*> parserOptionGroup "Scenario filters" (option (eitherReader (either (Left . Text.unpack) Right . parseScenarioId . Text.pack)) (long "scenario" <> metavar "SCENARIO" <> help "Scenario identifier"))+    <*> parserOptionGroup "Scenario filters" (optional (option (eitherReader parseComponent) (long "cohort-component" <> metavar "PACKAGE[=VERSION|REVISION]" <> help "Require a runtime cohort package")))+    <*> parserOptionGroup "Scenario filters" (many (option (eitherReader parseOutcome) (long "outcome" <> metavar "OUTCOME" <> help "Include this outcome; may repeat")))+    <*> parserOptionGroup "Scenario filters" (optional (option (eitherReader parseDay) (long "since" <> metavar "YYYY-MM-DD" <> help "Include records from this UTC day")))+    <*> parserOptionGroup "Trust filters" (switch (long "confirmed-only" <> help "Include machine-confirmed or human-reviewed records"))+    <*> parserOptionGroup "Output" (switch (long "json" <> help "Emit kenshou.evidence-history/v1 JSON"))++historyHandler :: HistoryCli -> CliEnv -> IO ExitCode+historyHandler options _+  | Just output <- schemaDiagnostic options.config.diagnostic (describe evidenceConfig) = Text.IO.putStr output >> pure ExitSuccess+  | otherwise = do+      processEnvironment <- fmap (fmap (\(name, setting) -> (Text.pack name, Text.pack setting))) getEnvironment+      resolved <- resolveEvidenceDefaults (envSnapshot processEnvironment) options.config+      case resolved of+        Left message -> failure message+        Right result -> case result.answer of+          Left problems -> failure (renderErrorsText problems)+          Right defaults -> case resolutionDiagnostic options.config.diagnostic result of+            Just output -> Text.IO.putStr output >> pure ExitSuccess+            Nothing -> case defaults.bundleUri of+              Nothing -> Text.IO.hPutStrLn stderr "kenshou history: evidence.bundle-uri or --bundle-uri is required" >> pure (ExitFailure 2)+              Just bundleUri -> do+                let query = HistoryQuery options.scenario options.cohortComponent options.outcomes options.since options.confirmedOnly+                observed <- history bundleUri (Text.unpack defaults.bundleRoot) query+                case observed of+                  Left (HistoryError message) -> failure message+                  Right document -> do+                    if options.json+                      then LazyByteString.putStrLn (encode document)+                      else mapM_ (\entry -> Text.IO.putStrLn (entry.concept <> "  " <> entry.outcome <> "  " <> entry.trust)) document.entries+                    pure ExitSuccess+  where+    failure message = Text.IO.hPutStrLn stderr ("kenshou history: " <> message) >> pure (ExitFailure 4)++parseComponent :: String -> Either String (Text, Maybe Text)+parseComponent raw = case break (== '=') raw of+  ([], _) -> Left "cohort component package cannot be empty"+  (_, "=") -> Left "cohort component version or revision cannot be empty"+  (name, '=' : revision) -> Right (Text.pack name, Just (Text.pack revision))+  (name, _) -> Right (Text.pack name, Nothing)++parseOutcome :: String -> Either String Outcome+parseOutcome raw = case Aeson.fromJSON (String (Text.pack raw)) of+  Aeson.Success outcome -> Right outcome+  Aeson.Error message -> Left message++parseDay :: String -> Either String Day+parseDay raw = maybe (Left "day must be YYYY-MM-DD") Right (parseTimeM True defaultTimeLocale "%Y-%m-%d" raw)
+ src/Kenshou/Evidence/Publish.hs view
@@ -0,0 +1,152 @@+module Kenshou.Evidence.Publish+  ( UploadMode (..),+    PublishOptions (..),+    PublishError (..),+    publishRunData,+    publishComparisonData,+  )+where++import Control.Exception (IOException, try)+import Control.Monad (forM, unless, when)+import Control.Monad.Trans.Class (lift)+import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE)+import Data.ByteString qualified as ByteString+import Data.List (sortOn)+import Data.Maybe (catMaybes)+import Data.Text (Text)+import Data.Text qualified as Text+import Kenshou.Core.Id (renderRunId)+import Kenshou.Core.Outcome (Outcome (..))+import Kenshou.Evidence.Source (CellEvidence (..), ComparisonSource (..), ComparisonView (..), RunResultView (..), RunSource (..), VerifiedFile (..))+import Kenshou.Evidence.Store (ObjectStat (..), ObjectStore (..), StoreError, validateObjectUri)+import Kenshou.Evidence.Types (DataKind (..), DataLink (..), Sha256, sha256Bytes)+import Kenshou.Remote.Cell.Docs (CellManifest (..))+import Numeric.Natural (Natural)+import System.FilePath ((</>))+import System.IO.Temp (withSystemTempDirectory)++data UploadMode = UploadMissing | VerifyOnly deriving stock (Eq, Show)++data PublishOptions = PublishOptions+  { baseUri :: !Text,+    uploadMode :: !UploadMode,+    deepVerify :: !Bool,+    linkLogs :: !Bool+  }+  deriving stock (Eq, Show)++data PublishError+  = InvalidBaseUri !Text+  | PublishIo !Text+  | StoreFailure !StoreError+  | MissingObject !Text+  | ObjectMismatch !Text+  deriving stock (Eq, Show)++-- | Publish every file covered by the verified manifest, even if only a+-- subset will be linked directly. The manifest link pins the rest transitively.+publishRunData :: ObjectStore -> PublishOptions -> FilePath -> RunSource -> IO (Either PublishError [DataLink])+publishRunData store options root source = case validateRunBase source options.baseUri of+  Left err -> pure (Left err)+  Right base -> do+    completed <-+      try+        ( runExceptT do+            let runPrefix = base <> "/" <> renderRunId source.result.resultRunId+                entries = sortOn (.relativePath) source.files+            linked <- forM entries $ \entry -> do+              let uri = runPrefix <> "/" <> Text.pack entry.relativePath+                  path = root </> entry.relativePath+              publishOne store options path uri entry.mediaType entry.digest entry.bytes+              pure $ do+                kind <- kindForPath options.linkLogs (source.result.resultOutcome /= Passed) entry.relativePath+                pure DataLink {kind, uri, digest = entry.digest, mediaType = entry.mediaType, bytes = entry.bytes}+            manifestBytes <- lift (ByteString.readFile (root </> "manifest.json"))+            let manifestUri = runPrefix <> "/manifest.json"+                manifestDigest = sha256Bytes manifestBytes+                manifestSize = fromIntegral (ByteString.length manifestBytes)+            publishOne store options (root </> "manifest.json") manifestUri "application/json" manifestDigest manifestSize+            cellLinks <- case source.cellEvidence of+              Nothing -> pure []+              Just cell -> do+                let uri = Text.dropEnd (Text.length "/output") base <> "/manifest.json"+                    file = cell.cellManifestFile+                publishOne store (options {uploadMode = VerifyOnly}) cell.cellManifestPath uri "application/json" file.digest file.bytes+                pure [DataLink {kind = CellManifestData, uri, digest = file.digest, mediaType = "application/json", bytes = file.bytes}]+            pure $+              sortOn (\link -> (link.kind, link.uri)) $+                DataLink {kind = ManifestData, uri = manifestUri, digest = manifestDigest, mediaType = "application/json", bytes = manifestSize} : cellLinks <> catMaybes linked+        ) ::+        IO (Either IOException (Either PublishError [DataLink]))+    pure $ either (Left . PublishIo . Text.pack . show) id completed++publishComparisonData :: ObjectStore -> PublishOptions -> FilePath -> ComparisonSource -> IO (Either PublishError DataLink)+publishComparisonData store options path source = case cleanBase options.baseUri of+  Left err -> pure (Left err)+  Right base -> do+    let entry = source.file+        uri = base <> "/" <> renderRunId source.view.comparisonId <> "/comparison.json"+    completed <- try (runExceptT (publishOne store options path uri entry.mediaType entry.digest entry.bytes)) :: IO (Either IOException (Either PublishError ()))+    pure $ do+      either (Left . PublishIo . Text.pack . show) id completed+      pure DataLink {kind = ComparisonData, uri, digest = entry.digest, mediaType = entry.mediaType, bytes = entry.bytes}++cleanBase :: Text -> Either PublishError Text+cleanBase raw = do+  let base = Text.dropWhileEnd (== '/') raw+  case validateObjectUri (base <> "/probe") of+    Left _ -> Left (InvalidBaseUri raw)+    Right () -> Right base++validateRunBase :: RunSource -> Text -> Either PublishError Text+validateRunBase source raw = do+  base <- cleanBase raw+  case source.cellEvidence of+    Nothing -> pure ()+    Just cell -> do+      let expectedSuffix = "/runs/" <> renderRunId cell.cellManifest.runId <> "/output"+      unless (expectedSuffix `Text.isSuffixOf` base) (Left (InvalidBaseUri raw))+  pure base++publishOne :: ObjectStore -> PublishOptions -> FilePath -> Text -> Text -> Sha256 -> Natural -> ExceptT PublishError IO ()+publishOne store options source uri mediaType expectedDigest expectedSize = do+  contents <- lift (ByteString.readFile source)+  when (fromIntegral (ByteString.length contents) /= expectedSize || sha256Bytes contents /= expectedDigest) (throwE (ObjectMismatch uri))+  result <- lift $ withSystemTempDirectory "kenshou-evidence-publish" $ \scratch -> do+    let snapshot = scratch </> "snapshot"+    ByteString.writeFile snapshot contents+    runExceptT (publishSnapshot snapshot scratch)+  either throwE pure result+  where+    publishSnapshot snapshot scratch = do+      case options.uploadMode of+        UploadMissing -> do+          uploaded <- lift (store.putObjectIfAbsent snapshot uri mediaType)+          either (throwE . StoreFailure) (const (pure ())) uploaded+        VerifyOnly -> pure ()+      observed <- lift (store.statObject uri) >>= either (throwE . StoreFailure) pure+      stat <- maybe (throwE (MissingObject uri)) pure observed+      when (stat.bytes /= expectedSize || maybe False (/= expectedDigest) stat.recordedSha256) (throwE (ObjectMismatch uri))+      -- Cell-owned objects carry their SHA-256 in the sealed manifest, not+      -- GCS custom metadata. Verify their bytes when metadata is absent.+      when (options.deepVerify || stat.recordedSha256 == Nothing) do+        let target = scratch </> "downloaded"+        fetched <- lift (store.fetchObject uri target)+        either (throwE . StoreFailure) pure fetched+        downloaded <- lift (ByteString.readFile target)+        unless (fromIntegral (ByteString.length downloaded) == expectedSize && sha256Bytes downloaded == expectedDigest) (throwE (ObjectMismatch uri))++kindForPath :: Bool -> Bool -> FilePath -> Maybe DataKind+kindForPath linkLogs failed path+  | path == "run-spec.json" = Just RunSpecData+  | path == "run-result.json" = Just RunResultData+  | path == "cell/manifest.json" = Just CellManifestData+  | "samples/" `Text.isPrefixOf` item = Just SamplesData+  | "series/" `Text.isPrefixOf` item = Just SeriesData+  | "verdicts/" `Text.isPrefixOf` item = Just VerdictsData+  | "diagnosis/" `Text.isPrefixOf` item = Just DiagnosisData+  | "logs/" `Text.isPrefixOf` item && (linkLogs || failed) = Just LogsData+  | otherwise = Nothing+  where+    item = Text.pack path
+ src/Kenshou/Evidence/Record.hs view
@@ -0,0 +1,427 @@+module Kenshou.Evidence.Record+  ( RecordInput (..),+    RecordOptions (..),+    RecordOutcome (..),+    RecordError (..),+    buildRunRecord,+    recordRun,+    recordRunWith,+    recordComparison,+  )+where++import Control.Applicative ((<|>))+import Control.Monad (forM, unless, when)+import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (..), fromJSON, object, withObject, (.:), (.:?), (.=))+import Data.Aeson.Key qualified as Key+import Data.Aeson.KeyMap qualified as KeyMap+import Data.Aeson.Types (Parser)+import Data.List (nub, sortOn)+import Data.Map.Strict (Map)+import Data.Map.Strict qualified as Map+import Data.Maybe (fromMaybe, mapMaybe)+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Time (UTCTime, defaultTimeLocale, formatTime, getCurrentTime)+import Kenshou.Core.Cohort (CohortIdentity (..), CohortName (..), PackageSource (..), PlanHash (..), ResolvedComponent (..), ResolvedPackage (..))+import Kenshou.Core.Id (Kind (..), LayerId, RunId, ScenarioId (..), renderLayer, renderRunId, renderScenarioId)+import Kenshou.Core.Outcome (Outcome (..), renderOutcome)+import Kenshou.Core.RunSpec (EnvironmentSpec (..), RunSpec (..), SpecPlacement (..))+import Kenshou.Evidence.Bundle (BundleWriteError, BundleWriteResult (..), writeComparisonRecord, writeRunRecord)+import Kenshou.Evidence.Frontmatter (ComparisonEvidence (..), EvidenceRecord (..), recordFromDocument)+import Kenshou.Evidence.Publish (PublishError, PublishOptions (..), UploadMode, publishComparisonData, publishRunData)+import Kenshou.Evidence.Source (CellEvidence (..), ComparisonSource (..), ComparisonView (..), RunResultView (..), RunSource (..), SourceError, VerifiedFile (..), loadComparisonSource, loadRunSource)+import Kenshou.Evidence.Store (ObjectStore)+import Kenshou.Evidence.Types (ComponentRef (..), DataKind (..), DataLink (..), Purpose (..), Sha256, SubjectKind (..), mkRevision, mkSha256)+import Kenshou.Evidence.Types qualified as EvidenceTypes+import Okf.Bundle (conceptDocument, conceptSourcePath, walkBundle)+import Okf.ConceptId (parseConceptId, renderConceptLink)++data RecordInput = RecordInput+  { purpose :: !Purpose,+    generatedAt :: !UTCTime,+    allowDirty :: !Bool,+    subjectOverride :: !(Maybe (Text, SubjectKind)),+    produced :: ![Text],+    previousRun :: !(Maybe Text),+    -- | The evidence subject each registered layer declares.+    subjects :: !(Map LayerId Text),+    -- | The subject for a layer that declares none.+    fallbackSubject :: !(Maybe Text)+  }+  deriving stock (Eq, Show)++data RecordError+  = RecordError !Text+  | SourceFailure !SourceError+  | PublishFailure !PublishError+  | BundleFailure !BundleWriteError+  deriving stock (Eq, Show)++data RecordOptions = RecordOptions+  { bundleRoot :: !FilePath,+    dataBaseUri :: !Text,+    purpose :: !Purpose,+    uploadMode :: !UploadMode,+    deepVerify :: !Bool,+    allowDirty :: !Bool,+    linkLogs :: !Bool,+    subjectOverride :: !(Maybe (Text, SubjectKind)),+    produced :: ![Text],+    subjects :: !(Map LayerId Text),+    fallbackSubject :: !(Maybe Text)+  }+  deriving stock (Eq, Show)++data RecordOutcome = Recorded !FilePath | AlreadyRecorded !FilePath+  deriving stock (Eq, Show)++recordRun :: ObjectStore -> RecordOptions -> FilePath -> IO (Either RecordError RecordOutcome)+recordRun store options directory = do+  now <- getCurrentTime+  recordRunWith writeRunRecord now store options directory++recordRunWith :: (FilePath -> EvidenceRecord -> IO (Either BundleWriteError BundleWriteResult)) -> UTCTime -> ObjectStore -> RecordOptions -> FilePath -> IO (Either RecordError RecordOutcome)+recordRunWith writer now store options directory = do+  loaded <- loadRunSource directory+  case loaded of+    Left err -> pure (Left (SourceFailure err))+    Right source -> case preflight source of+      Left err -> pure (Left err)+      Right () -> do+        let publishing = PublishOptions options.dataBaseUri options.uploadMode options.deepVerify options.linkLogs+        published <- publishRunData store publishing directory source+        case published of+          Left err -> pure (Left (PublishFailure err))+          Right links -> do+            let input = RecordInput options.purpose now options.allowDirty options.subjectOverride options.produced Nothing options.subjects options.fallbackSubject+            case buildRunRecord input source links of+              Left err -> pure (Left err)+              Right record -> do+                written <- writer options.bundleRoot record+                pure case written of+                  Left err -> Left (BundleFailure err)+                  Right (RecordCreated path) -> Right (Recorded path)+                  Right (RecordPresent path) -> Right (AlreadyRecorded path)+  where+    preflight source = do+      kenshou <- jsonField "kenshou" source.result.resultFingerprint+      revision <- jsonField "revision" kenshou+      _ <- either (Left . RecordError) Right (mkRevision revision)+      dirty <- jsonField "dirty" kenshou+      when (dirty && not options.allowDirty) (Left (RecordError "dirty harness requires --allow-dirty"))++recordComparison :: ObjectStore -> RecordOptions -> FilePath -> IO (Either RecordError RecordOutcome)+recordComparison store options path = do+  loaded <- loadComparisonSource path+  case loaded of+    Left err -> pure (Left (SourceFailure err))+    Right source -> do+      arms <- loadRecordedArms options.bundleRoot (source.view.baselineRuns <> source.view.candidateRuns)+      case arms of+        Left err -> pure (Left err)+        Right records -> case comparisonFields options source records of+          Left err -> pure (Left err)+          Right build -> do+            let publishing = PublishOptions options.dataBaseUri options.uploadMode options.deepVerify options.linkLogs+            published <- publishComparisonData store publishing path source+            case published of+              Left err -> pure (Left (PublishFailure err))+              Right link -> do+                now <- getCurrentTime+                let record = build now link+                written <- writeComparisonRecord options.bundleRoot record+                pure case written of+                  Left err -> Left (BundleFailure err)+                  Right (RecordCreated relative) -> Right (Recorded relative)+                  Right (RecordPresent relative) -> Right (AlreadyRecorded relative)++loadRecordedArms :: FilePath -> [RunId] -> IO (Either RecordError [(FilePath, EvidenceRecord)])+loadRecordedArms root identifiers = do+  found <- walkBundle root+  pure do+    concepts <- either (Left . RecordError . Text.pack . show) Right found+    let records = [(conceptSourcePath concept, record) | concept <- concepts, Right record <- [recordFromDocument (conceptDocument concept)]]+    let byId = Map.fromListWith (<>) [(record.runId, [(path, record)]) | (path, record) <- records]+    forM identifiers $ \identifier -> case Map.lookup (renderRunId identifier) byId of+      Just [entry] -> Right entry+      Just _ -> Left (RecordError ("recorded arm has duplicate concepts: " <> renderRunId identifier))+      Nothing -> Left (RecordError ("comparison arm is not recorded: " <> renderRunId identifier))++comparisonFields :: RecordOptions -> ComparisonSource -> [(FilePath, EvidenceRecord)] -> Either RecordError (UTCTime -> DataLink -> ComparisonEvidence)+comparisonFields options source arms = do+  unless (null options.produced) (Left (RecordError "--produced applies only to run records"))+  let view = source.view+      pairCount = view.pairCount+      (baselines, candidates) = splitAt pairCount arms+      allRecords = map snd arms+  unless (length baselines == pairCount && length candidates == pairCount) (Left (RecordError "comparison arm count differs from source"))+  scenario <- case nub (map (.scenario) allRecords) of+    [single] -> Right single+    _ -> Left (RecordError "comparison arms must have one scenario")+  axis <- case view.variedFactors of+    [single] -> Right single+    _ -> Left (RecordError "comparison evidence requires one varied factor")+  (factor, factorName) <- factorForAxis axis+  baselineValue <- armValue axis baselines+  candidateValue <- armValue axis candidates+  when (baselineValue == candidateValue) (Left (RecordError "comparison factor values must differ"))+  revisionText <- maybe (Left (RecordError "comparison harness revision is unavailable")) Right view.harnessRevision+  harnessRevision <- either (Left . RecordError) Right (mkRevision revisionText)+  harnessDirty <- maybe (Left (RecordError "comparison harness dirty state is unavailable")) Right view.harnessDirty+  when (harnessDirty && not options.allowDirty) (Left (RecordError "dirty harness requires --allow-dirty"))+  first <- case candidates of+    (_, candidate) : _ -> Right candidate+    [] -> Left (RecordError "comparison has no candidate arm")+  let subject = maybe first.subject fst options.subjectOverride+      subjectKind = maybe first.subjectKind snd options.subjectOverride+  unless ("mori://" `Text.isPrefixOf` subject) (Left (RecordError "subject must be a canonical Mori URI"))+  let outcome = case view.verdict of+        "pass" -> Passed+        "regression" -> Failed+        "inconclusive" -> Inconclusive+        _ -> InfrastructureFailure+      comparison =+        object+          ( [ "verdict" .= view.verdict,+              "factor" .= factor,+              "baselineValue" .= baselineValue,+              "candidateValue" .= candidateValue,+              "design" .= view.design,+              "baselineRuns" .= map (('/' :) . fst) baselines,+              "candidateRuns" .= map (('/' :) . fst) candidates+            ]+              <> maybe [] (\name -> ["factorName" .= name]) factorName+          )+      build now link =+        ComparisonEvidence+          { title = renderScenarioId scenario <> " comparison " <> view.verdict,+            description = "Recorded comparison of " <> Text.pack (show pairCount) <> " paired runs with digest-pinned data.",+            generatedAt = utcText now,+            runId = renderRunId view.comparisonId,+            purpose = if harnessDirty then Investigation else options.purpose,+            scenario,+            tier = first.tier,+            placement = first.placement,+            outcome,+            startedAt = utcText view.startedAt,+            finishedAt = utcText view.finishedAt,+            subject,+            subjectKind,+            harnessRevision,+            harnessDirty,+            computations = ["VC-3"],+            dataLinks = [link],+            comparison,+            body = "The paired comparison produced the recorded verdict under [VC-3](/computations/paired-comparison.md). Its raw document is linked by digest in the frontmatter.\n"+          }+  pure build++factorForAxis :: Text -> Either RecordError (Text, Maybe Text)+factorForAxis axis+  | axis == "cohort" = Right ("cohort", Nothing)+  | Just name <- Text.stripPrefix "dim:" axis, not (Text.null name) = Right ("dimension", Just name)+  | Just name <- Text.stripPrefix "knob:" axis, not (Text.null name) = Right ("knob", Just name)+  | otherwise = Left (RecordError ("unsupported comparison factor: " <> axis))++armValue :: Text -> [(FilePath, EvidenceRecord)] -> Either RecordError Value+armValue axis records = case nub (mapMaybe value (map snd records)) of+  [single] | length records == length (mapMaybe value (map snd records)) -> Right single+  _ -> Left (RecordError ("comparison arm has missing or inconsistent " <> axis <> " values"))+  where+    value record+      | axis == "cohort" = Just (String record.cohort)+      | Just name <- Text.stripPrefix "dim:" axis = String <$> lookup name record.dimensions+      | Just name <- Text.stripPrefix "knob:" axis = lookup name record.knobs+      | otherwise = Nothing++data FingerprintFields = FingerprintFields+  { os :: !Text,+    arch :: !Text,+    cpuModel :: !(Maybe Text),+    cores :: !Integer,+    memoryBytes :: !(Maybe Integer),+    ghc :: !Text,+    postgres :: !Text,+    harnessRevision :: !(Maybe Text),+    harnessDirty :: !(Maybe Bool)+  }++instance FromJSON FingerprintFields where+  parseJSON = withObject "fingerprint" \value -> do+    host <- value .: "host"+    runtime <- value .: "runtime"+    kenshou <- value .: "kenshou"+    pg <- value .:? "postgres" :: Parser (Maybe Value)+    postgres <- case pg of+      Just (Object fields) -> fields .: "serverVersion"+      _ -> pure "none"+    FingerprintFields+      <$> host .: "os"+      <*> host .: "arch"+      <*> host .:? "cpuModel"+      <*> host .: "logicalCores"+      <*> host .:? "memoryBytes"+      <*> runtime .: "ghc"+      <*> pure postgres+      <*> kenshou .:? "revision"+      <*> kenshou .:? "dirty"++buildRunRecord :: RecordInput -> RunSource -> [DataLink] -> Either RecordError EvidenceRecord+buildRunRecord input source dataLinks = do+  let linkedKinds = map (.kind) dataLinks+  unless (all (`elem` linkedKinds) [ManifestData, RunSpecData, RunResultData]) (Left (RecordError "run data must link its manifest, spec and result"))+  case source.spec.environment.placement of+    RunLocal -> when (source.cellEvidence /= Nothing || CellManifestData `elem` linkedKinds) (Left (RecordError "local run cannot link cell evidence"))+    RunOnCell -> do+      unless (source.cellEvidence /= Nothing && length (filter (== CellManifestData) linkedKinds) == 1) (Left (RecordError "cell run must link one outer cell manifest"))+      nested <- case [link.uri | link <- dataLinks, link.kind == ManifestData] of+        [uri] -> Right uri+        _ -> Left (RecordError "cell run must link one nested manifest")+      outer <- case [link | link <- dataLinks, link.kind == CellManifestData] of+        [link] -> Right link+        _ -> Left (RecordError "cell run must link one outer manifest")+      let suffix = "/output/" <> renderRunId source.result.resultRunId <> "/manifest.json"+      base <- maybe (Left (RecordError "nested manifest URI does not identify this cell run")) Right (Text.stripSuffix suffix nested)+      unless (outer.uri == base <> "/manifest.json") (Left (RecordError "outer cell manifest URI differs from nested run prefix"))+      case source.cellEvidence of+        Just cell -> unless (outer.digest == cell.cellManifestFile.digest && outer.bytes == cell.cellManifestFile.bytes) (Left (RecordError "outer cell manifest link differs from verified source"))+        Nothing -> Left (RecordError "cell run has no verified outer manifest")+  subject <- resolveSubject input.subjectOverride input.subjects input.fallbackSubject source.result.resultScenario.layer+  unless ("mori://" `Text.isPrefixOf` subject) (Left (RecordError "subject must be a canonical Mori URI"))+  unless (all (Text.isPrefixOf "mori://") input.produced) (Left (RecordError "produced artifacts must use canonical Mori URIs"))+  fingerprint <- decoded "fingerprint" source.result.resultFingerprint :: Either RecordError FingerprintFields+  revisionText <- maybe (Left (RecordError "harness revision is unavailable")) Right fingerprint.harnessRevision+  harnessRevision <- either (Left . RecordError) Right (mkRevision revisionText)+  harnessDirty <- maybe (Left (RecordError "harness dirty state is unavailable")) Right fingerprint.harnessDirty+  when (harnessDirty && not input.allowDirty) (Left (RecordError "dirty harness requires --allow-dirty"))+  cpuModel <- maybe (Left (RecordError "CPU model is unavailable")) Right fingerprint.cpuModel+  memoryBytes <- maybe (Left (RecordError "physical memory size is unavailable")) Right fingerprint.memoryBytes+  when (fingerprint.cores < 0 || memoryBytes < 0) (Left (RecordError "invalid environment size"))+  cellFields <- case source.cellEvidence of+    Nothing -> Right []+    Just _ -> do+      cellFingerprint <- jsonField "cell" source.result.resultFingerprint :: Either RecordError Value+      cell <- jsonField "cell" cellFingerprint :: Either RecordError Text+      cellRun <- jsonField "cellRun" cellFingerprint :: Either RecordError RunId+      gce <- jsonFieldOptional "gce" cellFingerprint :: Either RecordError (Maybe Value)+      gceFields <- case gce of+        Nothing -> Right []+        Just details -> do+          machineType <- jsonField "machineType" details :: Either RecordError Text+          zone <- jsonField "zone" details :: Either RecordError Text+          pure ["machineType" .= machineType, "zone" .= zone]+      pure (["cell" .= cell, "cellRun" .= renderRunId cellRun] <> gceFields)+  let effectivePurpose = if harnessDirty then Investigation else input.purpose+      scenario = source.result.resultScenario+      cohortIdentity = source.result.resultCohort+      cohort = unCohortName cohortIdentity.identityCohort+      subjectKind = maybe SubjectProject snd input.subjectOverride+      environment = object (["os" .= fingerprint.os, "arch" .= fingerprint.arch, "cpuModel" .= cpuModel, "cores" .= fingerprint.cores, "memoryBytes" .= memoryBytes, "ghc" .= fingerprint.ghc, "postgres" .= fingerprint.postgres] <> cellFields)+      effectiveOutcome = maybe source.result.resultOutcome (.cellEffectiveOutcome) source.cellEvidence+      title = renderScenarioId scenario <> " " <> renderOutcome effectiveOutcome <> " on " <> cohort+      description = "Recorded " <> renderScenarioId scenario <> " run against cohort " <> cohort <> " with digest-pinned data."+      computations = "VC-1" : ["VC-2" | scenario.kind `elem` [Benchmark, Soak], hasMeasurementSummary source.result.resultSummaries]+  solverPlanHash <- kernelDigest (unPlanHash cohortIdentity.identityPlanHash)+  compatibilityKey <- do+    comparisonKey <- jsonField "comparisonKey" source.result.resultCompatibility+    kernelDigest comparisonKey+  components <- concat <$> traverse componentRefs cohortIdentity.identityComponents+  knownDefects <- case source.result.resultKnownDefect of+    Nothing -> Right []+    Just value@(Object fields) -> case KeyMap.lookup "defects" fields of+      Nothing -> (: []) <$> jsonField "reference" value+      Just raw -> do+        defects <- decoded "defects" raw :: Either RecordError [Value]+        traverse (jsonField "reference") defects+    Just value -> (: []) <$> jsonField "reference" value+  knobs <- case toJSON source.spec of+    Object fields -> case KeyMap.lookup "knobs" fields of+      Just (Object entries) -> Right $ sortOn fst [(Key.toText key, value) | (key, value) <- KeyMap.toList entries]+      _ -> Left (RecordError "run spec has no resolved knobs")+    _ -> Left (RecordError "run spec is not an object")+  firstComputation <- either (Left . RecordError . Text.pack . show) Right (parseConceptId "computations/run-outcome")+  pure+    EvidenceRecord+      { title,+        description,+        generatedAt = utcText input.generatedAt,+        runId = renderRunId source.result.resultRunId,+        purpose = effectivePurpose,+        scenario,+        tier = source.result.resultTier,+        placement = case source.spec.environment.placement of RunLocal -> "local"; RunOnCell -> "cell",+        outcome = effectiveOutcome,+        startedAt = utcText source.result.resultStartedAt,+        finishedAt = utcText source.result.resultEndedAt,+        subject,+        subjectKind,+        harnessRevision,+        harnessDirty,+        computations,+        dataLinks,+        cohort,+        solverPlanHash,+        components,+        environment,+        seed = source.result.resultSeed,+        compatibilityKey,+        knobs,+        dimensions = source.spec.dimensions,+        knownDefects,+        produced = input.produced,+        previousRun = input.previousRun,+        body = "This run produced the recorded outcome under " <> renderConceptLink firstComputation "VC-1" <> ". Its raw data is linked by digest in the frontmatter.\n"+      }++componentRefs :: ResolvedComponent -> Either RecordError [ComponentRef]+componentRefs component = forM component.resolvedComponentPackages $ \package -> do+  (source, revision) <- case package.resolvedPackageSource of+    FromHackage _ -> Right (EvidenceTypes.FromHackage, Nothing)+    FromGit _ rawRevision _ -> do+      parsed <- either (Left . RecordError) Right (mkRevision rawRevision)+      Right (EvidenceTypes.FromGit, Just parsed)+    FromBoot -> Left (RecordError ("boot package is not a runtime component: " <> package.resolvedPackageName))+    FromLocalPath _ -> Left (RecordError ("local-path package cannot be recorded as a released runtime component: " <> package.resolvedPackageName))+  unless ("mori://" `Text.isPrefixOf` component.resolvedComponentMoriUri) (Left (RecordError "component has no canonical Mori project URI"))+  pure ComponentRef {project = component.resolvedComponentMoriUri, package = package.resolvedPackageName, version = package.resolvedPackageVersion, source, revision}++kernelDigest :: Text -> Either RecordError Sha256+kernelDigest value = do+  raw <- maybe (Left (RecordError "kernel digest is missing its sha256: prefix")) Right (Text.stripPrefix "sha256:" value)+  either (Left . RecordError) Right (mkSha256 raw)++jsonField :: (FromJSON value) => Text -> Value -> Either RecordError value+jsonField name value = case value of+  Object fields -> case KeyMap.lookup (Key.fromText name) fields of+    Nothing -> Left (RecordError ("missing JSON field " <> name))+    Just raw -> decoded name raw+  _ -> Left (RecordError "expected a JSON object")++jsonFieldOptional :: (FromJSON value) => Text -> Value -> Either RecordError (Maybe value)+jsonFieldOptional name value = case value of+  Object fields -> traverse (decoded name) (KeyMap.lookup (Key.fromText name) fields)+  _ -> Left (RecordError "expected a JSON object")++decoded :: (FromJSON value) => Text -> Value -> Either RecordError value+decoded label value = case fromJSON value of+  Error message -> Left (RecordError (label <> ": " <> Text.pack message))+  Success parsed -> Right parsed++utcText :: UTCTime -> Text+utcText = Text.pack . formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ"++-- | The subject of a run record: the override, else the subject the registered+-- bundle of the run's layer declares, else the configured fallback.+resolveSubject :: Maybe (Text, SubjectKind) -> Map LayerId Text -> Maybe Text -> LayerId -> Either RecordError Text+resolveSubject override subjects fallback layer = case (fst <$> override) <|> Map.lookup layer subjects <|> fallback of+  Just subject -> Right subject+  Nothing -> Left (RecordError ("no evidence subject for layer " <> renderLayer layer <> "; pass --subject or set evidence.default-subject"))++hasMeasurementSummary :: Maybe Value -> Bool+hasMeasurementSummary (Just (Object summaries)) = case KeyMap.lookup "measurements" summaries of+  Just (Object measurements) -> case KeyMap.lookup "measurements" measurements of+    Just (Object _) -> True+    _ -> False+  _ -> False+hasMeasurementSummary _ = False
+ src/Kenshou/Evidence/Source.hs view
@@ -0,0 +1,295 @@+module Kenshou.Evidence.Source+  ( SourceError (..),+    VerifiedFile (..),+    RunResultView (..),+    CellEvidence (..),+    RunSource (..),+    ComparisonView (..),+    ComparisonSource (..),+    loadRunSource,+    loadComparisonSource,+  )+where++import Control.Exception (IOException, try)+import Control.Monad (forM, forM_, unless, when)+import Control.Monad.Trans.Class (lift)+import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE)+import Data.Aeson (FromJSON (..), Value (..), eitherDecodeStrict', withObject, (.:), (.:?))+import Data.Aeson qualified as Aeson+import Data.Aeson.KeyMap qualified as KeyMap+import Data.ByteString qualified as ByteString+import Data.List (sort)+import Data.Set qualified as Set+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Time (UTCTime)+import Data.Word (Word64)+import Kenshou.Core.Canonical (sha256Hex)+import Kenshou.Core.Cohort (CohortIdentity)+import Kenshou.Core.Id (RunId, ScenarioId, renderRunId, unSeed)+import Kenshou.Core.Manifest (Manifest (..), ManifestFile (..))+import Kenshou.Core.Outcome (Outcome)+import Kenshou.Core.RunSpec (EnvironmentSpec (..), RunSpec (..), SpecPlacement (..))+import Kenshou.Evidence.Types (Sha256, sha256Bytes)+import Kenshou.Remote.Cell.Docs (Artifact (..), CellManifest (..))+import Kenshou.Remote.Cell.Fetch qualified as CellFetch+import Kenshou.Remote.Cell.Index (CellManifestLink (..), CellRunIndex (..), RunLink (..))+import Numeric.Natural (Natural)+import System.Directory (canonicalizePath, doesDirectoryExist, doesFileExist, listDirectory, pathIsSymbolicLink)+import System.FilePath (isAbsolute, normalise, splitDirectories, takeDirectory, takeFileName, (</>))++newtype SourceError = SourceError Text deriving stock (Eq, Show)++data VerifiedFile = VerifiedFile+  { relativePath :: !FilePath,+    digest :: !Sha256,+    bytes :: !Natural,+    mediaType :: !Text+  }+  deriving stock (Eq, Show)++-- RunResult has no FromJSON instance in the kernel. This reader keeps the+-- evidence-facing fields typed while leaving the large diagnostic payload to+-- the versioned run-result document itself.+data RunResultView = RunResultView+  { resultRunId :: !RunId,+    resultScenario :: !ScenarioId,+    resultOutcome :: !Outcome,+    resultTier :: !Text,+    resultSeed :: !Word64,+    resultSpecSha256 :: !Text,+    resultStartedAt :: !UTCTime,+    resultEndedAt :: !UTCTime,+    resultCohort :: !CohortIdentity,+    resultFingerprint :: !Value,+    resultCompatibility :: !Value,+    resultSummaries :: !(Maybe Value),+    resultKnownDefect :: !(Maybe Value)+  }+  deriving stock (Eq, Show)++instance FromJSON RunResultView where+  parseJSON = withObject "RunResult" \value -> do+    schema <- value .: "schema"+    unless (schema == ("kenshou.run-result/v1" :: Text)) (fail "unsupported run-result schema")+    spec <- value .: "spec"+    timings <- value .: "timings"+    RunResultView+      <$> value .: "runId"+      <*> value .: "scenario"+      <*> value .: "outcome"+      <*> value .: "tier"+      <*> value .: "seed"+      <*> spec .: "sha256"+      <*> timings .: "startedAt"+      <*> timings .: "endedAt"+      <*> value .: "cohort"+      <*> value .: "fingerprint"+      <*> value .: "compatibility"+      <*> value .:? "summaries"+      <*> value .:? "knownDefect"++data CellEvidence = CellEvidence+  { cellManifest :: !CellManifest,+    cellManifestPath :: !FilePath,+    cellManifestFile :: !VerifiedFile,+    cellEffectiveOutcome :: !Outcome+  }+  deriving stock (Eq, Show)++data RunSource = RunSource+  { manifest :: !Manifest,+    spec :: !RunSpec,+    result :: !RunResultView,+    files :: ![VerifiedFile],+    cellEvidence :: !(Maybe CellEvidence)+  }+  deriving stock (Eq, Show)++data ComparisonView = ComparisonView+  { comparisonId :: !RunId,+    baselineRuns :: ![RunId],+    candidateRuns :: ![RunId],+    startedAt :: !UTCTime,+    finishedAt :: !UTCTime,+    harnessRevision :: !(Maybe Text),+    harnessDirty :: !(Maybe Bool),+    design :: !Text,+    variedFactors :: ![Text],+    pairCount :: !Int,+    verdict :: !Text+  }+  deriving stock (Eq, Show)++data ComparisonSource = ComparisonSource+  { view :: !ComparisonView,+    file :: !VerifiedFile+  }+  deriving stock (Eq, Show)++instance FromJSON ComparisonView where+  parseJSON = withObject "Comparison" \value -> do+    schema <- value .: "schema"+    unless (schema == ("kenshou.comparison/v1" :: Text)) (fail "unsupported comparison schema")+    ComparisonView+      <$> value .: "comparisonId"+      <*> value .: "baselineRuns"+      <*> value .: "candidateRuns"+      <*> value .: "startedAt"+      <*> value .: "finishedAt"+      <*> value .:? "harnessRevision"+      <*> value .:? "harnessDirty"+      <*> value .: "design"+      <*> value .: "variedFactors"+      <*> value .: "pairCount"+      <*> value .: "verdict"++loadComparisonSource :: FilePath -> IO (Either SourceError ComparisonSource)+loadComparisonSource path = do+  loaded <- try (runExceptT (loadComparison path)) :: IO (Either IOException (Either SourceError ComparisonSource))+  pure $ either (Left . SourceError . Text.pack . show) id loaded++loadComparison :: FilePath -> ExceptT SourceError IO ComparisonSource+loadComparison path = do+  symlink <- lift (pathIsSymbolicLink path)+  when symlink (reject "comparison document is a symbolic link")+  contents <- lift (ByteString.readFile path)+  view <- either (reject . Text.pack) pure (eitherDecodeStrict' contents)+  when (view.pairCount <= 0 || view.pairCount /= length view.baselineRuns || view.pairCount /= length view.candidateRuns) (reject "comparison pair count and arm IDs disagree")+  when (view.startedAt > view.finishedAt) (reject "comparison ends before it starts")+  when (null view.variedFactors) (reject "comparison has no varied factor")+  when (view.design `notElem` ["abba", "baab", "sequential"]) (reject "comparison has an unknown design")+  when (view.verdict `notElem` ["pass", "regression", "inconclusive", "infrastructure-failure"]) (reject "comparison has an unknown verdict")+  when (Set.size (Set.fromList (view.baselineRuns <> view.candidateRuns)) /= view.pairCount * 2) (reject "comparison repeats a run ID")+  let file = VerifiedFile "comparison.json" (sha256Bytes contents) (fromIntegral (ByteString.length contents)) "application/json"+  pure ComparisonSource {view, file}++loadRunSource :: FilePath -> IO (Either SourceError RunSource)+loadRunSource root = do+  loaded <- try (runExceptT (load root)) :: IO (Either IOException (Either SourceError RunSource))+  pure $ either (Left . SourceError . Text.pack . show) id loaded++load :: FilePath -> ExceptT SourceError IO RunSource+load root = do+  manifest <- readJson (root </> "manifest.json")+  spec <- readJson (root </> "run-spec.json")+  files <- verifyFiles root manifest (spec.environment.placement == RunOnCell)+  result <- readJson (root </> "run-result.json")+  when (manifest.runId /= result.resultRunId) (reject "manifest and run result identify different runs")+  when (spec.runId /= Just result.resultRunId) (reject "run spec and run result identify different runs")+  when (spec.scenario /= result.resultScenario) (reject "run spec and run result identify different scenarios")+  when (fmap unSeed spec.seed /= Just result.resultSeed) (reject "run spec and run result use different seeds")+  when (result.resultStartedAt > result.resultEndedAt) (reject "run result ends before it starts")+  specBytes <- lift (ByteString.readFile (root </> "run-spec.json"))+  when (result.resultSpecSha256 /= sha256Hex specBytes) (reject "run result does not match run-spec.json")+  cellEvidence <- case spec.environment.placement of+    RunLocal -> pure Nothing+    RunOnCell -> Just <$> loadCellEvidence root result+  pure RunSource {manifest, spec, result, files, cellEvidence}++loadCellEvidence :: FilePath -> RunResultView -> ExceptT SourceError IO CellEvidence+loadCellEvidence root result = do+  realRoot <- lift (canonicalizePath root)+  let sidecar = root </> "cell-manifest.json"+      fetched = takeDirectory (takeDirectory realRoot) </> "manifest.json"+  hasSidecar <- lift (doesFileExist sidecar)+  hasFetched <- lift (doesFileExist fetched)+  path <- case (hasSidecar, hasFetched) of+    (True, _) -> pure sidecar+    (_, True) -> pure fetched+    _ -> reject "cell run has no outer cell manifest"+  symlink <- lift (pathIsSymbolicLink path)+  when symlink (reject "outer cell manifest is a symbolic link")+  bytes <- lift (ByteString.readFile path)+  cellManifest <- either (reject . Text.pack) pure (eitherDecodeStrict' bytes)+  (namedCell, namedRun) <- case result.resultFingerprint of+    Object fields -> case KeyMap.lookup "cell" fields of+      Just (Object cellFields) -> do+        cell <- parseField "fingerprint.cell.cell" =<< maybe (reject "fingerprint.cell.cell is missing") pure (KeyMap.lookup "cell" cellFields)+        cellRun <- parseField "fingerprint.cell.cellRun" =<< maybe (reject "fingerprint.cell.cellRun is missing") pure (KeyMap.lookup "cellRun" cellFields)+        pure (cell, cellRun)+      _ -> reject "cell run has no fingerprint.cell"+    _ -> reject "cell run has no fingerprint object"+  when (cellManifest.cell /= namedCell || cellManifest.runId /= namedRun) (reject "outer cell manifest differs from nested cell fingerprint")+  let nestedPrefix = "output/" <> renderRunId result.resultRunId <> "/"+  forM_ ["manifest.json", "run-result.json", "run-spec.json"] \name -> do+    let nestedPath = root </> Text.unpack name+    nestedBytes <- lift (ByteString.readFile nestedPath)+    case [artifact | artifact <- cellManifest.artifacts, artifact.path == nestedPrefix <> name] of+      [artifact]+        | Just artifact.sha256 == Text.stripPrefix "sha256:" (sha256Hex nestedBytes) && artifact.bytes == fromIntegral (ByteString.length nestedBytes) -> pure ()+      _ -> reject ("outer cell manifest does not pin nested " <> name)+  let cellManifestFile = VerifiedFile "cell-manifest.json" (sha256Bytes bytes) (fromIntegral (ByteString.length bytes)) "application/json"+      cellEffectiveOutcome = CellFetch.effectiveOutcome cellManifest.outcome True result.resultOutcome+      indexPath = takeDirectory (takeDirectory (takeDirectory realRoot)) </> "cell-run.json"+  hasIndex <- lift (doesFileExist indexPath)+  when hasIndex do+    index <- readJson indexPath :: ExceptT SourceError IO CellRunIndex+    when (index.cellRun /= cellManifest.runId || index.cell /= cellManifest.cell || index.cellManifest.sha256 /= Text.drop 7 (sha256Hex bytes)) (reject "cell-run index disagrees with the outer manifest")+    case [run | run <- index.runs, run.runId == result.resultRunId] of+      [run] | run.recordedOutcome == result.resultOutcome && run.effectiveOutcome == cellEffectiveOutcome -> pure ()+      _ -> reject "cell-run index disagrees with the nested effective outcome"+  pure CellEvidence {cellManifest, cellManifestPath = path, cellManifestFile, cellEffectiveOutcome}+  where+    parseField :: (FromJSON value) => Text -> Value -> ExceptT SourceError IO value+    parseField label value = case Aeson.fromJSON value of+      Aeson.Error problem -> reject (label <> ": " <> Text.pack problem)+      Aeson.Success parsed -> pure parsed++readJson :: (FromJSON value) => FilePath -> ExceptT SourceError IO value+readJson path = do+  contents <- lift (ByteString.readFile path)+  either (\message -> reject (Text.pack (takeFileName path) <> ": " <> Text.pack message)) pure (eitherDecodeStrict' contents)++verifyFiles :: FilePath -> Manifest -> Bool -> ExceptT SourceError IO [VerifiedFile]+verifyFiles root manifest cellRun = do+  let listed = map (.path) manifest.files+  when (length listed /= Set.size (Set.fromList listed)) (reject "manifest has duplicate file paths")+  verified <- forM manifest.files $ \file -> do+    unless (safeRelative file.path) (reject ("unsafe manifest path: " <> Text.pack file.path))+    let path = root </> file.path+    symlink <- lift (pathIsSymbolicLink path)+    when symlink (reject ("manifest path is a symbolic link: " <> Text.pack file.path))+    exists <- lift (doesFileExist path)+    unless exists (reject ("manifest file is missing: " <> Text.pack file.path))+    contents <- lift (ByteString.readFile path)+    when (file.sha256 /= sha256Hex contents) (reject ("digest mismatch: " <> Text.pack file.path))+    when (file.bytes /= fromIntegral (ByteString.length contents)) (reject ("size mismatch: " <> Text.pack file.path))+    when (Text.null file.mediaType) (reject ("missing media type: " <> Text.pack file.path))+    pure VerifiedFile {relativePath = file.path, digest = sha256Bytes contents, bytes = fromIntegral (ByteString.length contents), mediaType = file.mediaType}+  actual <- scanFiles root ""+  let sidecars = if cellRun then ["cell-manifest.json"] else []+  unless (sort listed == sort (filter (`notElem` ("manifest.json" : sidecars)) actual)) (reject "manifest does not enumerate every run file exactly once")+  pure verified++scanFiles :: FilePath -> FilePath -> ExceptT SourceError IO [FilePath]+scanFiles root relative = do+  let directory = root </> relative+  names <- lift (listDirectory directory)+  concat <$> forM names (visit directory)+  where+    visit directory name = do+      let relativePath = if null relative then name else relative </> name+          path = directory </> name+      unless (safeRelative relativePath) (reject ("unsafe run file path: " <> Text.pack relativePath))+      symlink <- lift (pathIsSymbolicLink path)+      when symlink (reject ("run directory contains a symbolic link: " <> Text.pack relativePath))+      isDirectory <- lift (doesDirectoryExist path)+      if isDirectory+        then scanFiles root relativePath+        else do+          regular <- lift (doesFileExist path)+          unless regular (reject ("run directory contains a non-file: " <> Text.pack relativePath))+          pure [relativePath]++safeRelative :: FilePath -> Bool+safeRelative path =+  not (null path)+    && not (isAbsolute path)+    && normalise path == path+    && all (\segment -> segment /= "" && segment /= "." && segment /= "..") (splitDirectories path)+    && '\\' `notElem` path++reject :: Text -> ExceptT SourceError IO value+reject = throwE . SourceError
+ src/Kenshou/Evidence/Store.hs view
@@ -0,0 +1,287 @@+module Kenshou.Evidence.Store+  ( ObjectStat (..),+    ObjectStore (..),+    StoreError (..),+    PutResult (..),+    durableSchemes,+    validateObjectUri,+    directoryStore,+    memoryStore,+    gcloudStore,+    gcloudStoreWith,+  )+where++import Control.Concurrent.MVar (modifyMVar, newMVar, readMVar)+import Control.Exception (IOException, try)+import Data.Aeson (Result (..), Value (..), eitherDecodeStrict', fromJSON)+import Data.Aeson.KeyMap qualified as KeyMap+import Data.ByteString (ByteString)+import Data.ByteString qualified as ByteString+import Data.Map.Strict qualified as Map+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Text.Encoding qualified as Text+import Kenshou.Evidence.Types (Sha256 (..), mkSha256, sha256Bytes)+import Numeric.Natural (Natural)+import System.Directory (createDirectoryIfMissing, doesFileExist)+import System.Exit (ExitCode (..))+import System.FilePath (takeDirectory, (</>))+import System.IO (hFlush)+import System.IO.Error (isAlreadyExistsError)+import System.IO.Temp (withTempFile)+import System.Posix.Files (createLink)+import System.Process (proc, readCreateProcessWithExitCode)+import Text.Read (readMaybe)++data ObjectStat = ObjectStat+  { bytes :: !Natural,+    recordedSha256 :: !(Maybe Sha256)+  }+  deriving stock (Eq, Show)++data StoreError+  = InvalidObjectUri !Text+  | StoreIo !Text+  | ObjectConflict !Text+  deriving stock (Eq, Show)++data PutResult = ObjectCreated | ObjectPresent deriving stock (Eq, Show)++data ObjectStore = ObjectStore+  { statObject :: Text -> IO (Either StoreError (Maybe ObjectStat)),+    fetchObject :: Text -> FilePath -> IO (Either StoreError ()),+    putObjectIfAbsent :: FilePath -> Text -> Text -> IO (Either StoreError PutResult)+  }++durableSchemes :: [Text]+durableSchemes = ["gs"]++validateObjectUri :: Text -> Either StoreError ()+validateObjectUri = fmap (const ()) . objectPath ""++-- | The scratch store preserves the gs:// URI in records while keeping the+-- bytes under root/bucket/key. Create-only publication uses an atomic hard link.+directoryStore :: FilePath -> ObjectStore+directoryStore root =+  ObjectStore+    { statObject = \uri -> withPath uri $ \path -> ioResult do+        exists <- doesFileExist path+        if exists then Just . objectStat <$> ByteString.readFile path else pure Nothing,+      fetchObject = \uri target -> withPath uri $ \path ->+        joinEither <$> ioResult do+          exists <- doesFileExist path+          if not exists+            then pure (Left (StoreIo ("object does not exist: " <> uri)))+            else do+              createDirectoryIfMissing True (takeDirectory target)+              ByteString.readFile path >>= ByteString.writeFile target+              pure (Right ()),+      putObjectIfAbsent = \source uri _mediaType -> withPath uri $ \path -> do+        loaded <- ioResult (ByteString.readFile source)+        case loaded of+          Left err -> pure (Left err)+          Right contents ->+            joinEither <$> ioResult do+              let parent = takeDirectory path+              createDirectoryIfMissing True parent+              withTempFile parent ".kenshou-object-" $ \temporary handle -> do+                ByteString.hPut handle contents+                hFlush handle+                linked <- try (createLink temporary path)+                case linked of+                  Right () -> pure (Right ObjectCreated)+                  Left err+                    | isAlreadyExistsError err -> do+                        present <- ByteString.readFile path+                        pure $ if present == contents then Right ObjectPresent else Left (ObjectConflict uri)+                    | otherwise -> pure (Left (StoreIo (Text.pack (show (err :: IOException)))))+    }+  where+    withPath :: Text -> (FilePath -> IO (Either StoreError value)) -> IO (Either StoreError value)+    withPath uri action = case objectPath root uri of+      Left err -> pure (Left err)+      Right path -> action path++memoryStore :: IO ObjectStore+memoryStore = do+  objects <- newMVar Map.empty+  pure+    ObjectStore+      { statObject = \uri -> case objectPath "" uri of+          Left err -> pure (Left err)+          Right _ -> do+            contents <- Map.lookup uri <$> readMVar objects+            pure (Right (objectStat <$> contents)),+        fetchObject = \uri target -> case objectPath "" uri of+          Left err -> pure (Left err)+          Right _ -> do+            contents <- Map.lookup uri <$> readMVar objects+            case contents of+              Nothing -> pure (Left (StoreIo ("object does not exist: " <> uri)))+              Just bytes -> ioResult do+                createDirectoryIfMissing True (takeDirectory target)+                ByteString.writeFile target bytes,+        putObjectIfAbsent = \source uri _mediaType -> case objectPath "" uri of+          Left err -> pure (Left err)+          Right _ -> do+            loaded <- ioResult (ByteString.readFile source)+            case loaded of+              Left err -> pure (Left err)+              Right contents -> modifyMVar objects $ \current ->+                case Map.lookup uri current of+                  Nothing -> pure (Map.insert uri contents current, Right ObjectCreated)+                  Just old | old == contents -> pure (current, Right ObjectPresent)+                  Just _ -> pure (current, Left (ObjectConflict uri))+      }++-- | The production adapter accepts only the explicitly selected GCP project.+-- The executable seam lets tests exercise the command protocol without cloud+-- credentials or remote mutations.+gcloudStore :: Text -> ObjectStore+gcloudStore = gcloudStoreWith "gcloud"++gcloudStoreWith :: FilePath -> Text -> ObjectStore+gcloudStoreWith executable project = store+  where+    store =+      ObjectStore+        { statObject = \uri -> do+            ready <- preflight+            case ready of+              Left err -> pure (Left err)+              Right () -> stat uri,+          fetchObject = \uri target -> do+            ready <- preflight+            case ready of+              Left err -> pure (Left err)+              Right () -> case validateObjectUri uri of+                Left err -> pure (Left err)+                Right () -> do+                  prepared <- ioResult (createDirectoryIfMissing True (takeDirectory target))+                  case prepared of+                    Left err -> pure (Left err)+                    Right () -> do+                      result <- command ["storage", "cp", Text.unpack uri, target, projectFlag, "--quiet"]+                      pure $ case result of+                        Left err -> Left err+                        Right _ -> Right (),+          putObjectIfAbsent = \source uri mediaType -> do+            ready <- preflight+            case ready of+              Left err -> pure (Left err)+              Right () -> case validateObjectUri uri of+                Left err -> pure (Left err)+                Right () -> do+                  local <- ioResult (ByteString.readFile source)+                  case local of+                    Left err -> pure (Left err)+                    Right contents -> do+                      let expected = objectStat contents+                      existing <- stat uri+                      case existing of+                        Left err -> pure (Left err)+                        Right (Just observed) -> pure (if observed == expected then Right ObjectPresent else Left (ObjectConflict uri))+                        Right Nothing -> do+                          let Sha256 digest = sha256Bytes contents+                          uploaded <- command ["storage", "cp", source, Text.unpack uri, "--if-generation-match=0", "--custom-metadata=kenshou-sha256=" <> Text.unpack digest, "--content-type=" <> Text.unpack mediaType, projectFlag, "--quiet"]+                          after <- stat uri+                          pure case after of+                            Left err -> Left err+                            Right (Just observed)+                              | observed == expected -> Right (if either (const False) (const True) uploaded then ObjectCreated else ObjectPresent)+                              | otherwise -> Left (ObjectConflict uri)+                            Right Nothing -> either Left (const (Left (StoreIo ("uploaded object is not visible: " <> uri)))) uploaded+        }++    projectFlag = "--project=" <> Text.unpack project++    preflight = do+      if Text.null project+        then pure (Left (StoreIo "GCP project must be explicit"))+        else do+          active <- command ["config", "get-value", "project"]+          pure case active of+            Left err -> Left err+            Right value+              | Text.strip value == project -> Right ()+              | otherwise -> Left (StoreIo ("active GCP project differs from requested project " <> project))++    stat uri = case validateObjectUri uri of+      Left err -> pure (Left err)+      Right () -> do+        result <- command ["storage", "objects", "describe", Text.unpack uri, "--format=json", projectFlag, "--quiet"]+        pure case result of+          Left (StoreIo message) | missingObject message -> Right Nothing+          Left err -> Left err+          Right output -> Just <$> parseGcloudStat output++    command arguments = do+      executed <- try (readCreateProcessWithExitCode (proc executable arguments) "") :: IO (Either IOException (ExitCode, String, String))+      pure case executed of+        Left err -> Left (StoreIo (Text.pack (show err)))+        Right (ExitSuccess, output, _) -> Right (Text.pack output)+        Right (ExitFailure code, _, stderrText) -> Left (StoreIo ("gcloud exited " <> Text.pack (show code) <> ": " <> Text.take 1200 (Text.pack stderrText)))++missingObject :: Text -> Bool+missingObject message = any (`Text.isInfixOf` Text.toCaseFold message) ["not_found", "not found", "404", "no urls matched"]++parseGcloudStat :: Text -> Either StoreError ObjectStat+parseGcloudStat output = do+  value <- either (Left . StoreIo . Text.pack) Right (eitherDecodeStrict' (Text.encodeUtf8 output))+  case value of+    Object fields -> do+      sizeValue <- maybe (Left (StoreIo "gcloud object description has no size")) Right (KeyMap.lookup "size" fields)+      size <- case sizeValue of+        String raw -> parseSize raw+        Number _ -> case (fromJSON sizeValue :: Result Integer) of+          Success number | number >= 0 -> Right (fromInteger number)+          _ -> Left (StoreIo "gcloud object size is invalid")+        _ -> Left (StoreIo "gcloud object size is not numeric")+      let customMetadata = case KeyMap.lookup "custom_fields" fields of+            Just metadataValue -> Just metadataValue+            Nothing -> KeyMap.lookup "metadata" fields+      digest <- case customMetadata of+        Nothing -> Right Nothing+        Just (Object metadata) -> case KeyMap.lookup "kenshou-sha256" metadata of+          Nothing -> Right Nothing+          Just (String raw) -> Just <$> either (Left . StoreIo) Right (mkSha256 raw)+          _ -> Left (StoreIo "gcloud object digest metadata is not text")+        _ -> Left (StoreIo "gcloud object custom metadata is not an object")+      Right ObjectStat {bytes = size, recordedSha256 = digest}+    _ -> Left (StoreIo "gcloud object description is not an object")+  where+    parseSize raw = case readMaybe (Text.unpack raw) :: Maybe Integer of+      Just value | value >= 0 -> Right (fromInteger value)+      _ -> Left (StoreIo "gcloud object size is invalid")++objectStat :: ByteString -> ObjectStat+objectStat contents =+  ObjectStat+    { bytes = fromIntegral (ByteString.length contents),+      recordedSha256 = Just (sha256Bytes contents)+    }++objectPath :: FilePath -> Text -> Either StoreError FilePath+objectPath root uri = do+  rest <- maybe (Left (InvalidObjectUri uri)) Right (Text.stripPrefix "gs://" uri)+  let segments = Text.splitOn "/" rest+  if length segments < 2 || any invalidSegment segments+    then Left (InvalidObjectUri uri)+    else Right (foldl (</>) root (Text.unpack <$> segments))+  where+    invalidSegment part =+      Text.null part+        || part == "."+        || part == ".."+        || Text.any (`elem` ['\\', '?', '#']) part++ioResult :: IO value -> IO (Either StoreError value)+ioResult action = do+  result <- try action+  pure $ case result of+    Left err -> Left (StoreIo (Text.pack (show (err :: IOException))))+    Right value -> Right value++joinEither :: Either error (Either error value) -> Either error value+joinEither = either Left id
+ src/Kenshou/Evidence/Types.hs view
@@ -0,0 +1,82 @@+module Kenshou.Evidence.Types+  ( Sha256 (..),+    Revision (..),+    mkSha256,+    sha256Bytes,+    mkRevision,+    RecordKind (..),+    Purpose (..),+    DataKind (..),+    DataLink (..),+    ComponentSource (..),+    ComponentRef (..),+    SubjectKind (..),+  )+where++import Data.ByteString (ByteString)+import Data.Text (Text)+import Data.Text qualified as Text+import Kenshou.Core.Canonical (sha256Hex)+import Numeric.Natural (Natural)++newtype Sha256 = Sha256 Text deriving stock (Eq, Ord, Show)++newtype Revision = Revision Text deriving stock (Eq, Ord, Show)++mkSha256 :: Text -> Either Text Sha256+mkSha256 value+  | Text.length value == 64 && Text.all lowerHex value = Right (Sha256 value)+  | otherwise = Left "SHA-256 must be exactly 64 lowercase hexadecimal characters"++-- The kernel uses a sha256: prefix in its JSON documents; OKF evidence stores+-- bare hexadecimal digests. Keep that conversion at the boundary.+sha256Bytes :: ByteString -> Sha256+sha256Bytes = Sha256 . Text.drop 7 . sha256Hex++mkRevision :: Text -> Either Text Revision+mkRevision value+  | Text.length value == 40 && Text.all lowerHex value = Right (Revision value)+  | otherwise = Left "revision must be exactly 40 lowercase hexadecimal characters"++lowerHex :: Char -> Bool+lowerHex char = char >= '0' && char <= '9' || char >= 'a' && char <= 'f'++data RecordKind = RunRecord | ComparisonRecord deriving stock (Eq, Ord, Show)++data Purpose = Nightly | Release | Baseline | Investigation deriving stock (Eq, Ord, Show)++data DataKind+  = RunSpecData+  | RunResultData+  | ManifestData+  | CellManifestData+  | SamplesData+  | SeriesData+  | VerdictsData+  | DiagnosisData+  | LogsData+  | ComparisonData+  deriving stock (Eq, Ord, Show)++data DataLink = DataLink+  { kind :: !DataKind,+    uri :: !Text,+    digest :: !Sha256,+    mediaType :: !Text,+    bytes :: !Natural+  }+  deriving stock (Eq, Show)++data ComponentSource = FromHackage | FromGit deriving stock (Eq, Ord, Show)++data ComponentRef = ComponentRef+  { project :: !Text,+    package :: !Text,+    version :: !Text,+    source :: !ComponentSource,+    revision :: !(Maybe Revision)+  }+  deriving stock (Eq, Show)++data SubjectKind = SubjectProject | SubjectPackage deriving stock (Eq, Ord, Show)
+ test/Main.hs view
@@ -0,0 +1,882 @@+module Main (main) where++import Control.Exception (bracket_)+import Control.Monad (forM_)+import Data.Aeson (Value (..), encode, object, toJSON, (.=))+import Data.Aeson qualified as Aeson+import Data.Aeson.Key qualified as Key+import Data.Aeson.KeyMap qualified as KeyMap+import Data.Aeson.Types qualified as Aeson+import Data.ByteString qualified as ByteString+import Data.ByteString.Char8 qualified as ByteString.Char8+import Data.ByteString.Lazy qualified as LazyByteString+import Data.List.NonEmpty qualified as NonEmpty+import Data.Map.Strict qualified as Map+import Data.Text (Text)+import Data.Text qualified as Text+import Data.Text.IO qualified as Text.IO+import Data.Time (UTCTime (..), fromGregorian)+import Kenshou.Core.Canonical (canonicalEncode, sha256Hex)+import Kenshou.Core.Cli.Config (ConfigInputs (..))+import Kenshou.Core.Compat (renderResourceInputs)+import Kenshou.Core.Env (mkMigrationSetName, renderMigrationSetName)+import Kenshou.Core.Id (LayerId, parseLayer, parseRunId, parseScenarioId, renderLayer, renderScenarioId, selftestLayer)+import Kenshou.Core.Manifest (writeManifest)+import Kenshou.Core.Outcome (Outcome (InfrastructureFailure, Passed))+import Kenshou.Core.Resource.Types (legacyKafkaName, renderResourceName)+import Kenshou.Core.Role qualified as Role+import Kenshou.Core.RunSpec qualified as RunSpec+import Kenshou.Evidence.Attest (AttestOptions (..), AttestResult (..), Recomputation (..), Recomputer (..), attest, coreRecomputers)+import Kenshou.Evidence.Bundle (BundleWriteError (..), BundleWriteResult (..), comparisonRecordPath, runRecordPath, writeAttestationRecord, writeRunRecord, writeRunRecordWith)+import Kenshou.Evidence.Check (CheckOptions (..), Finding (..), checkBundle, checkBundleWithStore, checkDocument)+import Kenshou.Evidence.Config (EvidenceDefaults (..), bundleRootKey, dataBaseUriKey, projectKey, resolveEvidenceDefaults)+import Kenshou.Evidence.Frontmatter (AttestationCheck (..), AttestationEvidence (..), EvidenceRecord (..), comparisonFromDocument, comparisonToDocument, recordFromDocument, recordToDocument)+import Kenshou.Evidence.History (HistoryDocument (..), HistoryEntry (..), HistoryQuery (..), deriveBaseline, history)+import Kenshou.Evidence.Publish (PublishError (..), PublishOptions (..), UploadMode (..), publishComparisonData, publishRunData)+import Kenshou.Evidence.Record (RecordError (..), RecordInput (..), RecordOptions (..), RecordOutcome (..), buildRunRecord, recordComparison, recordRunWith)+import Kenshou.Evidence.Source (ComparisonSource (..), ComparisonView (..), RunResultView (..), RunSource (..), VerifiedFile (..), loadComparisonSource, loadRunSource)+import Kenshou.Evidence.Store (ObjectStat (..), ObjectStore (..), PutResult (..), StoreError (..), directoryStore, gcloudStoreWith, memoryStore)+import Kenshou.Evidence.Types (DataKind (..), DataLink (..), Purpose (..), Revision (..), Sha256 (..), SubjectKind (..), mkRevision, mkSha256, sha256Bytes)+import Kenshou.Remote.Cell.Docs (Artifact (Artifact), CellManifest (CellManifest), ManifestPayload (ManifestPayload))+import Kenshou.Remote.Cell.Docs qualified as Cell+import Kenshou.Remote.Cell.Index (CellManifestLink (CellManifestLink), CellRunIndex (..), RunLink (RunLink))+import Okf.Actor (Actor (ProcessActor))+import Okf.Document (OKFDocument (..), Verification (..), frontmatterLookup, parseDocument, readVerified, removeField, serializeDocument, setField, setVerified)+import Settei (ResolveResult (..))+import Settei.Env (envSnapshot)+import Settei.Optparse (DiagnosticMode (NoDiagnostic), cliOverride, cliSources)+import System.Directory (Permissions (..), createDirectoryIfMissing, doesFileExist, getCurrentDirectory, getPermissions, removeFile, setCurrentDirectory, setPermissions)+import System.FilePath (takeDirectory, (</>))+import System.IO.Temp (withSystemTempDirectory)+import System.Process (callProcess, readProcess)+import Test.Hspec++main :: IO ()+main = hspec do+  -- Verbatim data from the first consumer: what it has already sealed must keep+  -- decoding, and its comparison keys must not change when it adopts this release.+  describe "consumer compatibility" do+    it "renders and parses the consumer's layer strings byte for byte" do+      layers <- Aeson.eitherDecodeFileStrict' "test/fixtures/consumer-records/layer-strings.json" >>= either fail pure+      fmap renderLayer <$> traverse parseLayer layers `shouldBe` Right (layers :: [Text])+      let identifiers = [layer <> "/component/correctness/name" | layer <- layers]+      fmap renderScenarioId <$> traverse parseScenarioId identifiers `shouldBe` Right identifiers+    it "reads a run specification with a null legacy resource field as no resources" do+      decoded <- Aeson.eitherDecodeFileStrict' "test/fixtures/consumer-records/run-spec.selftest-null-kafka.json" >>= either fail pure+      (decoded :: RunSpec.RunSpec).environment.resources `shouldBe` Map.empty+      environmentKeys (toJSON decoded) `shouldSatisfy` notElem (Key.fromText (renderResourceName legacyKafkaName))+    it "reads the legacy resource field of a run specification as the named resource" do+      raw <- Aeson.eitherDecodeFileStrict' "test/fixtures/consumer-records/run-spec.kafka-redpanda-container.json" >>= either fail pure+      decoded <- either fail pure (Aeson.parseEither Aeson.parseJSON raw) :: IO RunSpec.RunSpec+      let legacy = environmentField (renderResourceName legacyKafkaName) raw+      Map.lookup legacyKafkaName decoded.environment.resources `shouldBe` legacy+      environmentKeys (toJSON decoded) `shouldSatisfy` notElem (Key.fromText (renderResourceName legacyKafkaName))+      Aeson.parseEither Aeson.parseJSON (toJSON decoded) `shouldBe` Right decoded+      -- compat-key/v1 renders that resource where the legacy field sat, so recorded keys hold.+      object (renderResourceInputs decoded.environment.resources) `shouldBe` object [Key.fromText (renderResourceName legacyKafkaName) .= legacy]+    it "reads the legacy resource field of a worker initialization as the named resource" do+      raw <- Aeson.eitherDecodeFileStrict' "test/fixtures/consumer-records/worker-init.legacy-kafka.json" >>= either fail pure+      decoded <- either fail pure (Aeson.parseEither Aeson.parseJSON raw) :: IO Role.WorkerInit+      Just (Map.lookup legacyKafkaName decoded.resources) `shouldBe` fmap Just (topField (renderResourceName legacyKafkaName) raw)+      Aeson.parseEither Aeson.parseJSON (toJSON decoded) `shouldBe` Right decoded+    it "parses the consumer's sealed records, reproduces their paths, and re-renders them" do+      paths <- Aeson.eitherDecodeFileStrict' "test/fixtures/consumer-records/record-paths.json" >>= either fail pure+      forM_ (Map.toList (paths :: Map.Map FilePath FilePath)) \(file, committed) -> do+        text <- Text.IO.readFile ("test/fixtures/consumer-records/" <> file)+        document <- either (fail . show) pure (parseDocument text)+        -- The attester adds `verified` after recording; it is not what `kenshou record` writes.+        let written = serializeDocument document {frontmatter = removeField "verified" document.frontmatter}+        case (recordFromDocument document, comparisonFromDocument document) of+          (Right record, _) -> do+            serializeDocument (recordToDocument record) `shouldBe` written+            runRecordPath record `shouldBe` Right committed+          (_, Right comparison) -> do+            serializeDocument (comparisonToDocument comparison) `shouldBe` written+            comparisonRecordPath comparison `shouldBe` Right committed+          (Left problems, Left _) -> expectationFailure (show problems)+    it "keeps a recorded comparison key a function of its inputs and migration set names" do+      document <- Aeson.eitherDecodeFileStrict' "test/fixtures/consumer-records/run-result.hydration-cost.json" >>= either fail pure+      (recordedKey, inputs) <- case document of+        Object fields+          | Just (Object compatibility) <- KeyMap.lookup "compatibility" fields,+            Just (String key) <- KeyMap.lookup "comparisonKey" compatibility,+            Just (Object recorded) <- KeyMap.lookup "inputs" compatibility ->+              pure (key, recorded)+        _ -> fail "run result has no compatibility block"+      -- The comparison key covers every input except the cohort plan hash.+      sha256Hex (canonicalEncode (Object (KeyMap.delete "cohortPlanHash" inputs))) `shouldBe` recordedKey+      schemas <- case Aeson.fromJSON <$> KeyMap.lookup "schemas" inputs of+        Just (Aeson.Success names) -> pure names+        _ -> fail "compatibility inputs have no schemas"+      length schemas `shouldSatisfy` (> 0)+      -- The kernel renders a migration set's name as the schema text, so the same+      -- names reproduce the recorded inputs and therefore the recorded key.+      fmap renderMigrationSetName <$> traverse mkMigrationSetName schemas `shouldBe` Right (schemas :: [Text])+  describe "loadComparisonSource" do+    it "accepts a self-contained comparison and refuses inconsistent arms" do+      withSystemTempDirectory "kenshou-comparison-source" $ \root -> do+        let path = root </> "comparison.json"+            baseline = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55" :: Text+            candidate = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e56" :: Text+            comparison =+              object+                [ "schema" .= ("kenshou.comparison/v1" :: Text),+                  "comparisonId" .= ("01997f3a-5b7c-7e21-8a44-0d6c2f9b1e57" :: Text),+                  "baselineRuns" .= [baseline],+                  "candidateRuns" .= [candidate],+                  "startedAt" .= ("2026-09-26T00:00:00Z" :: Text),+                  "finishedAt" .= ("2026-09-26T00:00:01Z" :: Text),+                  "harnessRevision" .= Text.replicate 40 "f",+                  "harnessDirty" .= False,+                  "design" .= ("sequential" :: Text),+                  "variedFactors" .= ["cohort" :: Text],+                  "pairCount" .= (1 :: Int),+                  "verdict" .= ("pass" :: Text)+                ]+        LazyByteString.writeFile path (encode comparison)+        loaded <- loadComparisonSource path+        loaded `shouldSatisfy` \case+          Right source -> source.view.pairCount == 1 && source.file.digest == sha256Bytes (LazyByteString.toStrict (encode comparison))+          Left _ -> False+        source <- either (fail . show) pure loaded+        store <- memoryStore+        linked <- publishComparisonData store (PublishOptions "gs://bucket/runs" UploadMissing True False) path source+        linked `shouldSatisfy` \case+          Right link -> link.kind == ComparisonData && "/comparison.json" `Text.isSuffixOf` link.uri+          Left _ -> False+        LazyByteString.writeFile path (encode (object ["schema" .= ("kenshou.comparison/v1" :: Text), "comparisonId" .= ("01997f3a-5b7c-7e21-8a44-0d6c2f9b1e57" :: Text), "baselineRuns" .= [baseline], "candidateRuns" .= [baseline], "startedAt" .= ("2026-09-26T00:00:00Z" :: Text), "finishedAt" .= ("2026-09-26T00:00:01Z" :: Text), "harnessRevision" .= Text.replicate 40 "f", "harnessDirty" .= False, "design" .= ("sequential" :: Text), "variedFactors" .= ["cohort" :: Text], "pairCount" .= (1 :: Int), "verdict" .= ("pass" :: Text)]))+        loadComparisonSource path >>= (`shouldSatisfy` isLeft)++    it "records a comparison only after both arm concepts exist" do+      withSystemTempDirectory "kenshou-comparison-record" $ \root -> do+        let runDirectory = root </> "run"+            bundle = root </> "bundle"+            comparisonPath = root </> "comparison.json"+            baselineId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55" :: Text+            candidateId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e56" :: Text+            comparisonId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e57" :: Text+            options = RecordOptions bundle "gs://bucket/runs" Release UploadMissing False False False Nothing [] testSubjects Nothing+            comparison =+              object+                [ "schema" .= ("kenshou.comparison/v1" :: Text),+                  "comparisonId" .= comparisonId,+                  "baselineRuns" .= [baselineId],+                  "candidateRuns" .= [candidateId],+                  "startedAt" .= ("2026-09-26T00:00:00Z" :: Text),+                  "finishedAt" .= ("2026-09-26T00:00:01Z" :: Text),+                  "harnessRevision" .= Text.replicate 40 "f",+                  "harnessDirty" .= False,+                  "design" .= ("sequential" :: Text),+                  "variedFactors" .= ["cohort" :: Text],+                  "pairCount" .= (1 :: Int),+                  "verdict" .= ("pass" :: Text)+                ]+        createDirectoryIfMissing True runDirectory+        createDirectoryIfMissing True bundle+        callProcess "cp" ["-R", "../fixtures/consumer-baseline/docs/verification/.", bundle]+        writeRunFixture runDirectory+        source <- loadRunSource runDirectory >>= either (fail . show) pure+        store <- memoryStore+        links <- publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) runDirectory source >>= either (fail . show) pure+        baseline <- either (fail . show) pure (buildRunRecord (RecordInput Baseline (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing) source links)+        writeRunRecord bundle baseline >>= (`shouldSatisfy` isRight)+        LazyByteString.writeFile comparisonPath (encode comparison)+        recordComparison store options comparisonPath >>= (`shouldSatisfy` isLeft)+        let candidate =+              baseline+                { runId = candidateId,+                  cohort = "candidate",+                  dataLinks = [link {uri = Text.replace baselineId candidateId link.uri} | link <- baseline.dataLinks]+                }+        writeRunRecord bundle candidate >>= (`shouldSatisfy` isRight)+        recordComparison store options comparisonPath `shouldReturn` Right (Recorded "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e57.md")+        recordComparison store options comparisonPath `shouldReturn` Right (AlreadyRecorded "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e57.md")+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        comparisonAttested <- attest store coreRecomputers (AttestOptions bundle False False Nothing) comparisonId+        comparisonAttested `shouldSatisfy` \case+          Right result -> result.verdict == "refuted"+          Left _ -> False+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        let recordedPath = "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e57.md"+        recorded <- Text.IO.readFile (bundle </> recordedPath) >>= either (fail . show) pure . parseDocument+        let wrongOutcome = recorded {frontmatter = setField "outcome" (String "failed") recorded.frontmatter}+        map (.rule) (checkDocument recordedPath wrongOutcome) `shouldContain` ["comparison-outcome"]+        attested <- attest store coreRecomputers (AttestOptions bundle False False Nothing) "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md"+        attested `shouldSatisfy` \case+          Right result -> result.verdict == "incomplete"+          Left _ -> False+        case attested of+          Right result -> do+            attestation <- Text.IO.readFile (bundle </> result.path) >>= either (fail . show) pure . parseDocument+            let recomputationPassed = case frontmatterLookup "checks" attestation.frontmatter of+                  Just (Array values) -> any (\case Object fields -> KeyMap.lookup "name" fields == Just (String "verdict-recomputed") && KeyMap.lookup "result" fields == Just (String "passed"); _ -> False) values+                  _ -> False+            recomputationPassed `shouldBe` True+          Left err -> fail (show err)+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        let tamperedStore =+              store+                { fetchObject = \uri destination -> do+                    fetched <- store.fetchObject uri destination+                    case fetched of+                      Right () | "/run-result.json" `Text.isSuffixOf` uri -> ByteString.Char8.appendFile destination "tampered"+                      _ -> pure ()+                    pure fetched+                }+        tampered <- attest tamperedStore coreRecomputers (AttestOptions bundle False False Nothing) baselineId+        tampered `shouldSatisfy` \case+          Right result -> result.verdict == "refuted"+          Left _ -> False+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        let disagreeing = Recomputer "run-outcome" 1 (\_ -> pure (Right (Recomputation False (Just Passed) Nothing "replayed outcome differs")))+        recomputed <- attest store [disagreeing] (AttestOptions bundle False False Nothing) baselineId+        recomputed `shouldSatisfy` \case+          Right result -> result.verdict == "refuted"+          Left _ -> False+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        let attestationRecord =+              AttestationEvidence+                { title = "Attestation of baseline — incomplete",+                  description = "The source data was checked; outcome recomputation is pending.",+                  generatedAt = "2026-09-26T00:00:02Z",+                  attestationId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e5a",+                  run = "/runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md",+                  attesterRevision = Revision (Text.replicate 40 "f"),+                  attestedAt = "2026-09-26T00:00:02Z",+                  verdict = "incomplete",+                  checks = [AttestationCheck name "skipped" Nothing | name <- ["digests-match", "revisions-resolve", "cohort-matches-plan", "verdict-recomputed", "environment-captured", "clean-worktree"]],+                  dataDigests = map (.digest) baseline.dataLinks,+                  exception = Nothing,+                  body = "The attestation is incomplete.\n"+                }+        writeAttestationRecord bundle attestationRecord >>= (`shouldSatisfy` isRight)+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        let attestationPath = bundle </> "attestations/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e5a.md"+        originalAttestation <- Text.IO.readFile attestationPath+        attestationDocument <- either (fail . show) pure (parseDocument originalAttestation)+        Text.IO.writeFile attestationPath (serializeDocument (attestationDocument {frontmatter = setField "verdict" (String "confirmed") attestationDocument.frontmatter}))+        invalidAttestation <- checkBundle (CheckOptions bundle Nothing False False)+        invalidAttestation `shouldSatisfy` \case+          Right findings -> any (\finding -> finding.rule == "attestation-consistency") findings+          Left _ -> False+        Text.IO.writeFile attestationPath originalAttestation+        scenario <- either (fail . show) pure (parseScenarioId "selftest/kernel/correctness/always-pass")+        observed <- history testBundleUri bundle (HistoryQuery scenario Nothing [] Nothing False) >>= either (fail . show) pure+        let fixtureEntries = filter (\entry -> any (`Text.isSuffixOf` entry.concept) [baselineId, candidateId, comparisonId]) observed.entries+        length fixtureEntries `shouldBe` 3+        map (.recordKind) fixtureEntries `shouldBe` ["run", "run", "comparison"]+        let baselineEntry = (fixtureEntries !! 0) {trust = "machine-confirmed", attestations = [object ["verdict" .= ("confirmed" :: Text), "attestedAt" .= ("2026-09-26T00:00:03Z" :: Text)]]}+            laterEntry = (fixtureEntries !! 1 :: HistoryEntry) {startedAt = "2026-09-26T00:00:02Z"}+        deriveBaseline [baselineEntry, laterEntry] laterEntry `shouldBe` Just baselineEntry.concept+        let runPath = bundle </> "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md"+            actor = ProcessActor "kenshou-attester/0.1.0.0"+            firstTime = "2026-09-26T00:00:03Z"+            confirmedAttestation =+              attestationRecord+                { attestationId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e5b",+                  generatedAt = firstTime,+                  attestedAt = firstTime,+                  verdict = "confirmed",+                  checks = [AttestationCheck name "passed" Nothing | name <- ["digests-match", "revisions-resolve", "cohort-matches-plan", "verdict-recomputed", "environment-captured", "clean-worktree"]]+                }+        runDocument <- Text.IO.readFile runPath >>= either (fail . show) pure . parseDocument+        Text.IO.writeFile runPath (serializeDocument (runDocument {frontmatter = setVerified [Verification actor (Just firstTime)] runDocument.frontmatter}))+        writeAttestationRecord bundle confirmedAttestation >>= (`shouldSatisfy` isRight)+        writeAttestationRecord bundle (confirmedAttestation {attestationId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e5c", generatedAt = "2026-09-26T00:00:04Z", attestedAt = "2026-09-26T00:00:04Z"}) >>= (`shouldSatisfy` isRight)+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        let attestations = bundle </> "attestations/2026/09"+            target = "/runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md"+            attestation verdict at = Text.unlines ["---", "type: Attestation", "run: " <> target, "verdict: " <> verdict, "attestedAt: " <> at, "---"]+        createDirectoryIfMissing True attestations+        Text.IO.writeFile (attestations </> "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e58.md") (attestation "confirmed" "2099-09-27T00:00:03Z")+        confirmedHistory <- history testBundleUri bundle (HistoryQuery scenario Nothing [] Nothing False) >>= either (fail . show) pure+        (confirmedHistory.entries !! 0).trust `shouldBe` "machine-confirmed"+        Text.IO.writeFile (attestations </> "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e59.md") (attestation "refuted" "2099-09-27T00:00:04Z")+        refutedHistory <- history testBundleUri bundle (HistoryQuery scenario Nothing [] Nothing False) >>= either (fail . show) pure+        (refutedHistory.entries !! 0).trust `shouldBe` "unverified"++  describe "attest" do+    it "confirms a cell infrastructure override while recomputing the sealed nested verdict" do+      withSystemTempDirectory "kenshou-cell-attestation" $ \root -> do+        let attester = root </> "attester"+            bundle = root </> "bundle"+            cellRunId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e66" :: Text+            nestedId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55" :: Text+            runDirectory = root </> "cell-run" </> "tree" </> "output" </> Text.unpack nestedId+            outerPath = root </> "cell-run" </> "tree" </> "manifest.json"+            baseUri = "gs://bucket/runs/" <> cellRunId <> "/output"+            outerUri = "gs://bucket/runs/" <> cellRunId <> "/manifest.json"+            recordPath = "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md"+        createDirectoryIfMissing True attester+        createDirectoryIfMissing True bundle+        callProcess "git" ["-C", attester, "init", "-q"]+        callProcess "git" ["-C", attester, "config", "user.name", "Kenshou Test"]+        callProcess "git" ["-C", attester, "config", "user.email", "kenshou@example.invalid"]+        ByteString.Char8.writeFile (attester </> "README") "clean attester\n"+        callProcess "git" ["-C", attester, "add", "README"]+        callProcess "git" ["-C", attester, "commit", "-qm", "test: create clean attester"]+        revision <- Text.strip . Text.pack <$> readProcess "git" ["-C", attester, "rev-parse", "HEAD"] ""+        callProcess "cp" ["-R", "../fixtures/consumer-baseline/docs/verification/.", bundle]+        writeCellRunFixture runDirectory outerPath cellRunId revision False+        source <- loadRunSource runDirectory >>= either (fail . show) pure+        store <- memoryStore+        store.putObjectIfAbsent outerPath outerUri "application/json" >>= (`shouldSatisfy` isRight)+        publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) runDirectory source+          `shouldReturn` Left (InvalidBaseUri "gs://bucket/runs")+        store.statObject ("gs://bucket/runs/" <> nestedId <> "/run-result.json") `shouldReturn` Right Nothing+        links <- publishRunData store (PublishOptions baseUri UploadMissing True False) runDirectory source >>= either (fail . show) pure+        record <- either (fail . show) pure (buildRunRecord (RecordInput Investigation (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing) source links)+        record.outcome `shouldBe` InfrastructureFailure+        case record.environment of+          Object fields -> do+            KeyMap.lookup "cellRun" fields `shouldBe` Just (String cellRunId)+            KeyMap.lookup "machineType" fields `shouldBe` Nothing+            KeyMap.lookup "zone" fields `shouldBe` Nothing+          _ -> expectationFailure "cell environment must be an object"+        writeRunRecord bundle record >>= (`shouldSatisfy` isRight)+        originalDirectory <- getCurrentDirectory+        bracket_ (setCurrentDirectory attester) (setCurrentDirectory originalDirectory) do+          confirmed <- attest store coreRecomputers (AttestOptions bundle True False Nothing) (Text.pack recordPath)+          confirmed `shouldSatisfy` \case Right result -> result.verdict == "confirmed"; Left _ -> False+          checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []++    it "confirms a clean self-test run and appends one verified entry across two attestations" do+      withSystemTempDirectory "kenshou-confirmed-attestation" $ \root -> do+        let attester = root </> "attester"+            bundle = root </> "bundle"+            runDirectory = root </> "run"+            recordPath = "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md"+        createDirectoryIfMissing True attester+        createDirectoryIfMissing True bundle+        createDirectoryIfMissing True runDirectory+        callProcess "git" ["-C", attester, "init", "-q"]+        callProcess "git" ["-C", attester, "config", "user.name", "Kenshou Test"]+        callProcess "git" ["-C", attester, "config", "user.email", "kenshou@example.invalid"]+        ByteString.Char8.writeFile (attester </> "README") "clean attester\n"+        callProcess "git" ["-C", attester, "add", "README"]+        callProcess "git" ["-C", attester, "commit", "-qm", "test: create clean attester"]+        revision <- Text.strip . Text.pack <$> readProcess "git" ["-C", attester, "rev-parse", "HEAD"] ""+        callProcess "cp" ["-R", "../fixtures/consumer-baseline/docs/verification/.", bundle]+        writeRunFixtureWithRevision runDirectory revision+        source <- loadRunSource runDirectory >>= either (fail . show) pure+        store <- memoryStore+        links <- publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) runDirectory source >>= either (fail . show) pure+        record <- either (fail . show) pure (buildRunRecord (RecordInput Baseline (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing) source links)+        writeRunRecord bundle record >>= (`shouldSatisfy` isRight)+        originalDirectory <- getCurrentDirectory+        bracket_ (setCurrentDirectory attester) (setCurrentDirectory originalDirectory) do+          first <- attest store coreRecomputers (AttestOptions bundle True False Nothing) (Text.pack recordPath)+          first `shouldSatisfy` \case Right result -> result.verdict == "confirmed"; Left _ -> False+          checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+          second <- attest store coreRecomputers (AttestOptions bundle True False Nothing) record.runId+          second `shouldSatisfy` \case Right result -> result.verdict == "confirmed"; Left _ -> False+          checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        confirmedRun <- Text.IO.readFile (bundle </> recordPath) >>= either (fail . show) pure . parseDocument+        length (readVerified confirmedRun.frontmatter) `shouldBe` 1++  describe "evidence configuration" do+    it "uses built-ins, ordered YAML, environment, then named flags" do+      withSystemTempDirectory "kenshou-evidence-config" $ \root -> do+        let first = root </> "first.yaml"+            second = root </> "second.yaml"+            flags = cliSources "named flags" [cliOverride bundleRootKey "flag-bundle", cliOverride projectKey "flag-project", cliOverride dataBaseUriKey "gs://flag/runs"]+            inputs = ConfigInputs [first, second] flags NoDiagnostic+            snapshot = envSnapshot [("KENSHOU_EVIDENCE_BUNDLE", "env-bundle"), ("KENSHOU_GCP_PROJECT", "env-project"), ("KENSHOU_EVIDENCE_DATA_BASE_URI", "gs://env/runs")]+        writeFile first "evidence:\n  bundle-root: first-bundle\n  data-base-uri: gs://first/runs\ngcp:\n  project: first-project\n"+        writeFile second "evidence:\n  bundle-root: second-bundle\n  data-base-uri: gs://second/runs\ngcp:\n  project: second-project\n"+        resolved <- resolveEvidenceDefaults snapshot inputs+        case resolved of+          Right result -> result.answer `shouldBe` Right (EvidenceDefaults "flag-bundle" (Just "flag-project") (Just "gs://flag/runs") Nothing Nothing)+          Left err -> expectationFailure (Text.unpack err)+        builtIn <- resolveEvidenceDefaults (envSnapshot []) (ConfigInputs [] [] NoDiagnostic)+        case builtIn of+          Right result -> result.answer `shouldBe` Right (EvidenceDefaults "docs/verification" Nothing Nothing Nothing Nothing)+          Left err -> expectationFailure (Text.unpack err)++    it "rejects purpose and anomaly authority as unknown configuration" do+      withSystemTempDirectory "kenshou-evidence-config" $ \root -> do+        let file = root </> "invalid.yaml"+        writeFile file "evidence:\n  purpose: release\n  anomaly-authority: process:someone\n"+        resolved <- resolveEvidenceDefaults (envSnapshot []) (ConfigInputs [file] [] NoDiagnostic)+        case resolved of+          Right result -> case result.answer of+            Left problems -> length (NonEmpty.toList problems) `shouldSatisfy` (>= 2)+            Right _ -> expectationFailure "unexpectedly accepted unknown configuration"+          Left err -> expectationFailure (Text.unpack err)++  describe "evidence check" do+    it "detects event keys, coerced strings, non-durable links and changed committed records" do+      withSystemTempDirectory "kenshou-evidence-check" $ \root -> do+        let runDirectory = root </> "run"+            repo = root </> "repo"+            bundle = repo </> "docs/verification"+        createDirectoryIfMissing True runDirectory+        writeRunFixture runDirectory+        source <- loadRunSource runDirectory >>= either (fail . show) pure+        store <- memoryStore+        links <- publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) runDirectory source >>= either (fail . show) pure+        record <- either (fail . show) pure (buildRunRecord (RecordInput Baseline (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing) source links)+        path <- either (fail . show) pure (runRecordPath record)+        let document = recordToDocument record+            eventKey = document {frontmatter = setField "status" (String "stable") document.frontmatter}+            coerced = document {frontmatter = setField "dimensions" (toJSON [object ["name" .= ("telemetry" :: Text), "value" .= False]]) document.frontmatter}+            nonDurable = recordToDocument (record {dataLinks = [link {uri = "file:///tmp/sample"} | link <- record.dataLinks]})+        map (.rule) (checkDocument path eventKey) `shouldContain` ["event-keys"]+        map (.rule) (checkDocument path coerced) `shouldContain` ["string-typing"]+        map (.rule) (checkDocument path nonDurable) `shouldContain` ["data-uri"]+        createDirectoryIfMissing True (takeDirectory (bundle </> path))+        Text.IO.writeFile (bundle </> path) (serializeDocument document)+        callProcess "git" ["init", "-q", repo]+        callProcess "git" ["-C", repo, "-c", "user.name=Kenshou", "-c", "user.email=kenshou@example.invalid", "add", "."]+        callProcess "git" ["-C", repo, "-c", "user.name=Kenshou", "-c", "user.email=kenshou@example.invalid", "commit", "-qm", "test: record evidence fixture"]+        checkBundle (CheckOptions bundle Nothing False False) `shouldReturn` Right []+        checkBundleWithStore (Just store) (CheckOptions bundle Nothing True True) `shouldReturn` Right []+        absent <- memoryStore+        missing <- checkBundleWithStore (Just absent) (CheckOptions bundle Nothing True False)+        missing `shouldSatisfy` \case+          Right findings -> any (\finding -> finding.rule == "network" && "missing" `Text.isInfixOf` finding.message) findings+          Left _ -> False+        let nextId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e56"+            addedRecord = record {runId = nextId, dataLinks = [link {uri = Text.replace record.runId nextId link.uri} | link <- record.dataLinks]}+        addedPath <- either (fail . show) pure (runRecordPath addedRecord)+        Text.IO.writeFile (bundle </> addedPath) (serializeDocument (recordToDocument addedRecord))+        callProcess "git" ["-C", repo, "add", bundle </> addedPath]+        stagedAddition <- checkBundle (CheckOptions bundle Nothing False False)+        stagedAddition `shouldSatisfy` \case+          Right findings -> not (any (\finding -> finding.rule == "immutability" && finding.concept == addedPath) findings)+          Left _ -> False+        let selfReference = document {frontmatter = setField "previousRun" (String ("/" <> Text.pack path)) document.frontmatter}+        Text.IO.writeFile (bundle </> path) (serializeDocument selfReference)+        referenced <- checkBundle (CheckOptions bundle Nothing False False)+        referenced `shouldSatisfy` \case+          Right findings -> any (\finding -> finding.rule == "reference-targets") findings+          Left _ -> False+        let changed = document {frontmatter = setField "outcome" (String "failed") document.frontmatter}+        Text.IO.writeFile (bundle </> path) (serializeDocument changed)+        checked <- checkBundle (CheckOptions bundle Nothing False False)+        checked `shouldSatisfy` \case+          Right findings -> any (\finding -> finding.rule == "immutability" && finding.concept == path && "outcome" `Text.isInfixOf` finding.message) findings+          Left _ -> False+        callProcess "git" ["-C", repo, "add", "."]+        callProcess "git" ["-C", repo, "-c", "user.name=Kenshou", "-c", "user.email=kenshou@example.invalid", "commit", "-qm", "test: change evidence fixture"]+        committed <- checkBundle (CheckOptions bundle Nothing False False)+        committed `shouldSatisfy` \case+          Right findings -> any (\finding -> finding.rule == "immutability" && finding.concept == path && "outcome" `Text.isInfixOf` finding.message) findings+          Left _ -> False+  describe "recordRunWith" do+    it "records once and reports the same fact on replay" do+      withSystemTempDirectory "kenshou-record-command" $ \root -> do+        let runDirectory = root </> "run"+            bundle = root </> "bundle"+            options = RecordOptions bundle "gs://bucket/runs" Baseline UploadMissing False False False Nothing [] testSubjects Nothing+            timestamp = UTCTime (fromGregorian 2026 9 26) 0+            path = "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md"+            accept _ = pure (Right ())+        createDirectoryIfMissing True runDirectory+        createDirectoryIfMissing True bundle+        writeRunFixture runDirectory+        store <- memoryStore+        recordRunWith (writeRunRecordWith accept) timestamp store options runDirectory `shouldReturn` Right (Recorded path)+        recordRunWith (writeRunRecordWith accept) timestamp store options runDirectory `shouldReturn` Right (AlreadyRecorded path)++  describe "buildRunRecord" do+    it "names VC-2 only when a soak has its measurement summary" do+      withSystemTempDirectory "kenshou-soak-computations" $ \root -> do+        writeRunFixture root+        source <- loadRunSource root >>= either (fail . show) pure+        store <- memoryStore+        links <- publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) root source >>= either (fail . show) pure+        soakId <- either (fail . show) pure (parseScenarioId "synthetic/pipeline/soak/consumer-memory-and-fd-stability-reduced")+        let input = RecordInput Baseline (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects (Just "mori://shinzui/kenshou")+            orphan = RunSource source.manifest source.spec (source.result {resultScenario = soakId}) source.files source.cellEvidence+            withoutSummary = RunSource source.manifest source.spec (source.result {resultScenario = soakId, resultSummaries = Just (object ["measurements" .= object ["synthetic" .= object []]])}) source.files source.cellEvidence+            withSummary = RunSource source.manifest source.spec (source.result {resultScenario = soakId, resultSummaries = Just (object ["measurements" .= object ["measurements" .= object []]])}) source.files source.cellEvidence+        buildRunRecord input {fallbackSubject = Nothing} orphan links `shouldBe` Left (RecordError "no evidence subject for layer synthetic; pass --subject or set evidence.default-subject")+        without <- either (fail . show) pure (buildRunRecord input withoutSummary links)+        with <- either (fail . show) pure (buildRunRecord input withSummary links)+        without.computations `shouldBe` ["VC-1"]+        with.computations `shouldBe` ["VC-1", "VC-2"]+    it "preserves every owner reference from a grouped known-defect result" do+      withSystemTempDirectory "kenshou-grouped-defects" $ \root -> do+        let first = "mori://example/synthetic/okf/bug-reports/concepts/BUG-4" :: Text+            second = "mori://example/synthetic/okf/bug-reports/concepts/BUG-6" :: Text+            defect = object ["status" .= ("reproduced" :: Text), "defects" .= [object ["reference" .= first], object ["reference" .= second]]]+        writeRunFixtureWithKnownDefect root defect+        source <- loadRunSource root >>= either (fail . show) pure+        store <- memoryStore+        links <- publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) root source >>= either (fail . show) pure+        let input = RecordInput Baseline (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing+        record <- either (fail . show) pure (buildRunRecord input source links)+        record.knownDefects `shouldBe` [first, second]+    it "rejects a malformed grouped defect instead of dropping owner references" do+      withSystemTempDirectory "kenshou-malformed-defects" $ \root -> do+        let malformed = object ["reference" .= ("mori://example/bugs/1" :: Text), "defects" .= ("not an array" :: Text)]+        writeRunFixtureWithKnownDefect root malformed+        source <- loadRunSource root >>= either (fail . show) pure+        store <- memoryStore+        links <- publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) root source >>= either (fail . show) pure+        let input = RecordInput Baseline (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing+        buildRunRecord input source links `shouldSatisfy` isLeft+    it "derives a round-trippable run record from verified source and links" do+      withSystemTempDirectory "kenshou-record" $ \root -> do+        writeRunFixture root+        source <- loadRunSource root >>= either (fail . show) pure+        store <- memoryStore+        links <- publishRunData store (PublishOptions "gs://bucket/runs" UploadMissing False False) root source >>= either (fail . show) pure+        let input = RecordInput Baseline (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing+        record <- either (fail . show) pure (buildRunRecord input source links)+        recordFromDocument (recordToDocument record) `shouldBe` Right record+        let path = "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55.md"+            logPath = root </> "runs/selftest/2026/09/log.md"+            accept _ = pure (Right ())+        runRecordPath record `shouldBe` Right path+        writeRunRecordWith accept root record `shouldReturn` Right (RecordCreated path)+        originalLog <- Text.IO.readFile logPath+        writeRunRecordWith accept root record `shouldReturn` Right (RecordPresent path)+        Text.IO.readFile logPath `shouldReturn` originalLog+        writeRunRecordWith accept root (record {title = "changed"}) `shouldReturn` Left (RecordConflict path)+        let another = record {runId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e56"}+            rejectedPath = "runs/selftest/2026/09/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e56.md"+        writeRunRecordWith (\_ -> pure (Left (BundleInvalid "fixture rejection"))) root another `shouldReturn` Left (BundleInvalid "fixture rejection")+        doesFileExist (root </> rejectedPath) `shouldReturn` False+        Text.IO.readFile logPath `shouldReturn` originalLog++  describe "record frontmatter" do+    it "round-trips YAML-sensitive strings and a numeric-looking digest" do+      scenario <- either (fail . show) pure (parseScenarioId "selftest/kernel/correctness/always-pass")+      let digest = Sha256 (Text.replicate 31 "0" <> "e" <> Text.replicate 32 "0")+          base =+            EvidenceRecord+              { title = "Run: title # with punctuation",+                description = "A fixed description",+                generatedAt = "2026-09-26T20:00:00Z",+                runId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55",+                purpose = Investigation,+                scenario,+                tier = "smoke",+                placement = "local",+                outcome = Passed,+                startedAt = "2026-09-26T19:59:00Z",+                finishedAt = "2026-09-26T20:00:00Z",+                subject = "mori://shinzui/kenshou",+                subjectKind = SubjectProject,+                harnessRevision = Revision (Text.replicate 40 "f"),+                harnessDirty = True,+                computations = ["VC-1"],+                dataLinks = [DataLink ManifestData "gs://bucket/manifest.json" digest "application/json" 42],+                cohort = "head",+                solverPlanHash = digest,+                components = [],+                environment = object ["os" .= ("darwin" :: Text)],+                seed = 7,+                compatibilityKey = digest,+                knobs = [],+                dimensions = [],+                knownDefects = [],+                produced = [],+                previousRun = Nothing,+                body = "The computation is [VC-1](/computations/run-outcome.md).\n"+              }+      mapM_ (checkRoundTrip base) ["off", "on", "no", "yes", "null", "~", "true", "18", "1e10"]++  describe "gcloudStore" do+    it "requires the active project and uploads with a single generation precondition" do+      withSystemTempDirectory "kenshou-gcloud-store" $ \root -> do+        let executable = root </> "fake-gcloud"+            source = root </> "source"+            Sha256 digest = sha256Bytes "sealed"+            script =+              "#!/bin/sh\n"+                <> "if [ \"$1\" = config ]; then printf 'tan-nb-exp\\n'; exit 0; fi\n"+                <> "if [ \"$1\" = storage ] && [ \"$2\" = objects ]; then\n"+                <> "  if [ ! -f \"$(dirname \"$0\")/uploaded\" ]; then echo 'not found' >&2; exit 1; fi\n"+                <> "  printf '{\"size\":\"6\",\"custom_fields\":{\"kenshou-sha256\":\""+                <> Text.unpack digest+                <> "\"}}\\n'; exit 0\n"+                <> "fi\n"+                <> "if [ \"$1\" = storage ] && [ \"$2\" = cp ]; then\n"+                <> "  seen=0\n"+                <> "  for arg in \"$@\"; do\n"+                <> "    if [ \"$arg\" = --no-clobber ]; then echo 'conflicting no-clobber' >&2; exit 2; fi\n"+                <> "    if [ \"$arg\" = --if-generation-match=0 ]; then seen=1; fi\n"+                <> "  done\n"+                <> "  if [ \"$seen\" != 1 ]; then echo 'missing generation precondition' >&2; exit 2; fi\n"+                <> "  touch \"$(dirname \"$0\")/uploaded\"; exit 0\n"+                <> "fi\n"+                <> "exit 2\n"+        writeFile executable script+        ByteString.Char8.writeFile source "sealed"+        permissions <- getPermissions executable+        setPermissions executable permissions {executable = True}+        let matching = gcloudStoreWith executable "tan-nb-exp"+            mismatched = gcloudStoreWith executable "other-project"+        matching.statObject "gs://bucket/object" `shouldReturn` Right Nothing+        matching.putObjectIfAbsent source "gs://bucket/object" "application/octet-stream" `shouldReturn` Right ObjectCreated+        matching.statObject "gs://bucket/object" `shouldReturn` Right (Just (ObjectStat 6 (Just (Sha256 digest))))+        matching.putObjectIfAbsent source "gs://bucket/object" "application/octet-stream" `shouldReturn` Right ObjectPresent+        mismatch <- mismatched.statObject "gs://bucket/object"+        mismatch `shouldSatisfy` isLeft++  describe "publishRunData" do+    it "links a verified outer cell manifest and records the effective infrastructure outcome" do+      withSystemTempDirectory "kenshou-cell-evidence" $ \root -> do+        let cellRunId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e66" :: Text+            nestedId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55" :: Text+            runDirectory = root </> "cell-run" </> "tree" </> "output" </> Text.unpack nestedId+            outerPath = root </> "cell-run" </> "tree" </> "manifest.json"+            baseUri = "gs://bucket/runs/" <> cellRunId <> "/output"+            outerUri = "gs://bucket/runs/" <> cellRunId <> "/manifest.json"+        writeCellRunFixture runDirectory outerPath cellRunId (Text.replicate 40 "f") True+        source <- loadRunSource runDirectory >>= either (fail . show) pure+        source.cellEvidence `shouldSatisfy` (/= Nothing)+        manifestBytes <- ByteString.readFile outerPath+        nestedBytes <- ByteString.readFile (runDirectory </> "manifest.json")+        cellId <- either (fail . show) pure (parseRunId cellRunId)+        nestedRunId <- either (fail . show) pure (parseRunId nestedId)+        leaseId <- either (fail . show) pure (parseRunId "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e67")+        let runLink = RunLink nestedRunId ("output/" <> nestedId) "selftest/kernel/correctness/basic" (Text.drop 7 (sha256Hex nestedBytes)) Passed InfrastructureFailure (Just "cell health")+            index = CellRunIndex cellId "alpha" leaseId 1 ("gs://bucket/runs/" <> cellRunId) baseUri (CellManifestLink "manifest.json" (Text.drop 7 (sha256Hex manifestBytes)) (fromIntegral (ByteString.length manifestBytes))) Cell.InfrastructureFailure ["cell health"] (Just 0) [] [runLink] (UTCTime (fromGregorian 2026 9 26) 0)+            indexPath = root </> "cell-run" </> "cell-run.json"+        LazyByteString.writeFile indexPath (encode index)+        loadRunSource runDirectory >>= (`shouldSatisfy` isRight)+        let inconsistent = RunLink nestedRunId ("output/" <> nestedId) "selftest/kernel/correctness/basic" (Text.drop 7 (sha256Hex nestedBytes)) Passed Passed (Just "cell health")+        LazyByteString.writeFile indexPath (encode index {runs = [inconsistent]})+        loadRunSource runDirectory >>= (`shouldSatisfy` isLeft)+        LazyByteString.writeFile indexPath (encode index)+        store <- memoryStore+        store.putObjectIfAbsent outerPath outerUri "application/json" >>= (`shouldSatisfy` isRight)+        links <- publishRunData store (PublishOptions baseUri UploadMissing True False) runDirectory source >>= either (fail . show) pure+        [outer] <- pure [link | link <- links, link.kind == CellManifestData]+        outer.uri `shouldBe` outerUri+        let noMetadata =+              store+                { statObject = \uri -> do+                    observed <- store.statObject uri+                    pure (fmap (fmap (\stat -> stat {recordedSha256 = Nothing})) observed)+                }+        publishRunData noMetadata (PublishOptions baseUri VerifyOnly False False) runDirectory source `shouldReturn` Right links+        let tampered =+              noMetadata+                { fetchObject = \uri destination -> do+                    fetched <- store.fetchObject uri destination+                    case fetched of+                      Right () | uri == outerUri -> ByteString.Char8.appendFile destination "tampered"+                      _ -> pure ()+                    pure fetched+                }+        publishRunData tampered (PublishOptions baseUri VerifyOnly False False) runDirectory source >>= (`shouldSatisfy` isLeft)+        record <- either (fail . show) pure (buildRunRecord (RecordInput Investigation (UTCTime (fromGregorian 2026 9 26) 0) False Nothing [] Nothing testSubjects Nothing) source links)+        record.outcome `shouldBe` InfrastructureFailure+        case record.environment of+          Object fields -> do+            KeyMap.lookup "cell" fields `shouldBe` Just (String "alpha")+            KeyMap.lookup "cellRun" fields `shouldBe` Just (String cellRunId)+            KeyMap.lookup "zone" fields `shouldBe` Just (String "us-west1-a")+          _ -> expectationFailure "cell environment must be an object"+        removeFile outerPath+        loadRunSource runDirectory >>= (`shouldSatisfy` isLeft)++    it "publishes all verified run files and links the required ones" do+      withSystemTempDirectory "kenshou-publish" $ \root -> do+        writeRunFixture root+        source <- loadRunSource root >>= either (fail . show) pure+        store <- memoryStore+        let options = PublishOptions "gs://bucket/runs" UploadMissing True False+        published <- publishRunData store options root source+        case published of+          Left err -> expectationFailure (show err)+          Right links -> map (.kind) links `shouldBe` [RunSpecData, RunResultData, ManifestData]+        publishRunData store options root source `shouldReturn` published+        let verifyOnly = PublishOptions "gs://bucket/runs" VerifyOnly True False+        publishRunData store verifyOnly root source `shouldReturn` published+        original <- ByteString.readFile (root </> "run-result.json")+        ByteString.Char8.appendFile (root </> "run-result.json") "changed"+        conflicted <- publishRunData store options root source+        conflicted `shouldSatisfy` isLeft+        store.fetchObject ("gs://bucket/runs/01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55/run-result.json") (root </> "retained-result.json") `shouldReturn` Right ()+        retained <- ByteString.readFile (root </> "retained-result.json")+        retained `shouldBe` original++    it "refuses a non-durable destination before publishing" do+      withSystemTempDirectory "kenshou-publish" $ \root -> do+        writeRunFixture root+        source <- loadRunSource root >>= either (fail . show) pure+        store <- memoryStore+        publishRunData store (PublishOptions "file:///tmp" UploadMissing False False) root source+          `shouldReturn` Left (InvalidBaseUri "file:///tmp")++  describe "loadRunSource" do+    it "accepts a complete kernel run and rejects extra or changed files" do+      withSystemTempDirectory "kenshou-run-source" $ \root -> do+        writeRunFixture root+        loaded <- loadRunSource root+        loaded `shouldSatisfy` isRight+        ByteString.Char8.writeFile (root </> "unlisted.log") "unlisted"+        loadRunSource root >>= (`shouldSatisfy` isLeft)+        ByteString.Char8.writeFile (root </> "unlisted.log") ""+        -- The manifest must list even empty files.+        loadRunSource root >>= (`shouldSatisfy` isLeft)++    it "rejects modified bytes covered by the manifest" do+      withSystemTempDirectory "kenshou-run-source" $ \root -> do+        writeRunFixture root+        ByteString.Char8.appendFile (root </> "run-result.json") " "+        loadRunSource root >>= (`shouldSatisfy` isLeft)++  describe "evidence digest types" do+    it "accepts only lowercase fixed-length hexadecimal digests and revisions" do+      mkSha256 ("a" <> mconcat (replicate 63 "0")) `shouldBe` Right (Sha256 ("a" <> mconcat (replicate 63 "0")))+      mkSha256 (mconcat (replicate 64 "A")) `shouldSatisfy` isLeft+      mkSha256 (mconcat (replicate 63 "0")) `shouldSatisfy` isLeft+      mkRevision (mconcat (replicate 40 "f")) `shouldSatisfy` isRight+      mkRevision (mconcat (replicate 41 "f")) `shouldSatisfy` isLeft+      mkSha256 (case sha256Bytes "sample" of Sha256 digest -> digest) `shouldSatisfy` isRight++  describe "memoryStore" do+    it "reuses identical content and refuses conflicting content under one URI" do+      withSystemTempDirectory "kenshou-memory-store" $ \root -> do+        let first = root </> "first"+            second = root </> "second"+            uri = "gs://bucket/runs/id/result.json"+        ByteString.Char8.writeFile first "original"+        ByteString.Char8.writeFile second "different"+        store <- memoryStore+        store.putObjectIfAbsent first uri "application/json" `shouldReturn` Right ObjectCreated+        store.putObjectIfAbsent first uri "application/json" `shouldReturn` Right ObjectPresent+        store.putObjectIfAbsent second uri "application/json" `shouldReturn` Left (ObjectConflict uri)+        store.fetchObject uri (root </> "fetched") `shouldReturn` Right ()+        ByteString.readFile (root </> "fetched") `shouldReturn` "original"++  describe "directoryStore" do+    it "publishes create-only bytes and reports the observed digest" do+      withSystemTempDirectory "kenshou-directory-store" $ \root -> do+        let store = directoryStore (root </> "objects")+            source = root </> "source"+            other = root </> "other"+            uri = "gs://bucket/runs/id/manifest.json"+        ByteString.Char8.writeFile source "sealed"+        ByteString.Char8.writeFile other "changed"+        store.putObjectIfAbsent source uri "application/json" `shouldReturn` Right ObjectCreated+        store.putObjectIfAbsent source uri "application/json" `shouldReturn` Right ObjectPresent+        store.putObjectIfAbsent other uri "application/json" `shouldReturn` Left (ObjectConflict uri)+        result <- store.statObject uri+        result `shouldSatisfy` \case+          Right (Just ObjectStat {bytes = 6, recordedSha256 = Just _}) -> True+          _ -> False+        store.fetchObject uri (root </> "fetched") `shouldReturn` Right ()+        ByteString.readFile (root </> "fetched") `shouldReturn` "sealed"++    it "rejects URI paths that could escape the object root" do+      withSystemTempDirectory "kenshou-directory-store" $ \root -> do+        let store = directoryStore root+        store.statObject "file:///tmp/result.json" `shouldReturn` Left (InvalidObjectUri "file:///tmp/result.json")+        store.statObject "gs://bucket/../escape" `shouldReturn` Left (InvalidObjectUri "gs://bucket/../escape")++isLeft :: Either left right -> Bool+isLeft = either (const True) (const False)++isRight :: Either left right -> Bool+isRight = not . isLeft++checkRoundTrip :: EvidenceRecord -> Text -> IO ()+checkRoundTrip base sensitive = do+  let record = base {knobs = [("sensitive", String sensitive)], dimensions = [("sensitive", sensitive)]}+      rendered = serializeDocument (recordToDocument record)+  document <- either (fail . show) pure (parseDocument rendered)+  recordFromDocument document `shouldBe` Right record+  serializeDocument document `shouldBe` rendered++writeRunFixture :: FilePath -> IO ()+writeRunFixture root = writeRunFixtureWithRevision root (Text.replicate 40 "f")++writeRunFixtureWithRevision :: FilePath -> Text -> IO ()+writeRunFixtureWithRevision root harnessRevision = writeRunFixtureWithRevisionAndDefect root harnessRevision Nothing++writeRunFixtureWithKnownDefect :: FilePath -> Value -> IO ()+writeRunFixtureWithKnownDefect root defect = writeRunFixtureWithRevisionAndDefect root (Text.replicate 40 "f") (Just defect)++writeRunFixtureWithRevisionAndDefect :: FilePath -> Text -> Maybe Value -> IO ()+writeRunFixtureWithRevisionAndDefect root harnessRevision knownDefect = do+  let runId = "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55" :: Text+      spec = object ["schema" .= ("kenshou.run-spec/v1" :: Text), "runId" .= runId, "scenario" .= ("selftest/kernel/correctness/always-pass" :: Text), "seed" .= (7 :: Int)]+      specBytes = LazyByteString.toStrict (encode spec)+      fullDigest = "sha256:" <> Text.replicate 64 "a"+      cohort = object ["schema" .= ("kenshou.cohort-identity/v1" :: Text), "cohort" .= ("fixture" :: Text), "compiler" .= ("ghc" :: Text), "cabalVersion" .= ("3.14" :: Text), "os" .= ("darwin" :: Text), "arch" .= ("aarch64" :: Text), "planHash" .= fullDigest, "descriptorSha256" .= fullDigest, "components" .= [object ["id" .= ("selftest" :: Text), "moriUri" .= ("mori://shinzui/kenshou" :: Text), "packages" .= [object ["name" .= ("kenshou-core" :: Text), "version" .= ("0.1.0.0" :: Text), "source" .= object ["type" .= ("hackage" :: Text)]]]]]]+      result =+        object+          [ "schema" .= ("kenshou.run-result/v1" :: Text),+            "runId" .= runId,+            "scenario" .= ("selftest/kernel/correctness/always-pass" :: Text),+            "outcome" .= ("passed" :: Text),+            "knownDefect" .= knownDefect,+            "tier" .= ("smoke" :: Text),+            "seed" .= (7 :: Int),+            "spec" .= object ["sha256" .= sha256Hex specBytes],+            "timings" .= object ["startedAt" .= ("2026-09-26T00:00:00Z" :: Text), "endedAt" .= ("2026-09-26T00:00:01Z" :: Text)],+            "cohort" .= cohort,+            "fingerprint" .= object ["host" .= object ["os" .= ("darwin" :: Text), "arch" .= ("aarch64" :: Text), "cpuModel" .= ("fixture cpu" :: Text), "logicalCores" .= (4 :: Int), "memoryBytes" .= (4096 :: Int)], "runtime" .= object ["ghc" .= ("9.12.4" :: Text)], "kenshou" .= object ["revision" .= harnessRevision, "dirty" .= False], "postgres" .= object ["serverVersion" .= ("18.0" :: Text)]],+            "compatibility" .= object ["comparisonKey" .= fullDigest]+          ]+  ByteString.writeFile (root </> "run-spec.json") specBytes+  LazyByteString.writeFile (root </> "run-result.json") (encode result)+  parsed <- either (fail . show) pure (parseRunId runId)+  manifest <- writeManifest root parsed Map.empty+  LazyByteString.writeFile (root </> "manifest.json") (encode manifest)++writeCellRunFixture :: FilePath -> FilePath -> Text -> Text -> Bool -> IO ()+writeCellRunFixture runDirectory outerPath cellRunId revision includeGce = do+  createDirectoryIfMissing True runDirectory+  writeRunFixtureWithRevision runDirectory revision+  specValue <- Aeson.eitherDecodeFileStrict' (runDirectory </> "run-spec.json") >>= either fail pure+  let cellSpec = case specValue of+        Object fields -> Object (KeyMap.insert "environment" (object ["placement" .= ("cell" :: Text)]) fields)+        _ -> error "run fixture spec is not an object"+      specBytes = LazyByteString.toStrict (encode cellSpec)+  ByteString.writeFile (runDirectory </> "run-spec.json") specBytes+  resultValue <- Aeson.eitherDecodeFileStrict' (runDirectory </> "run-result.json") >>= either fail pure+  let cellFingerprint = object (["cell" .= ("alpha" :: Text), "cellRun" .= cellRunId] <> ["gce" .= object ["machineType" .= ("n2-standard-8" :: Text), "zone" .= ("us-west1-a" :: Text)] | includeGce])+      cellResult = case resultValue of+        Object fields -> case KeyMap.lookup "fingerprint" fields of+          Just (Object fingerprint) -> Object (KeyMap.insert "spec" (object ["sha256" .= sha256Hex specBytes]) (KeyMap.insert "fingerprint" (Object (KeyMap.insert "cell" cellFingerprint fingerprint)) fields))+          _ -> error "run fixture has no fingerprint"+        _ -> error "run fixture result is not an object"+  LazyByteString.writeFile (runDirectory </> "run-result.json") (encode cellResult)+  removeFile (runDirectory </> "manifest.json")+  nestedId <- either (fail . show) pure (parseRunId "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55")+  nested <- writeManifest runDirectory nestedId Map.empty+  LazyByteString.writeFile (runDirectory </> "manifest.json") (encode nested)+  cellId <- either (fail . show) pure (parseRunId cellRunId)+  leaseId <- either (fail . show) pure (parseRunId "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e67")+  let names = ["run-spec.json", "run-result.json", "manifest.json"]+      timestamp = UTCTime (fromGregorian 2026 9 26) 0+  artifacts <-+    traverse+      ( \name -> do+          bytes <- ByteString.readFile (runDirectory </> name)+          pure (Artifact ("output/" <> "01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55/" <> Text.pack name) (Text.drop 7 (sha256Hex bytes)) (fromIntegral (ByteString.length bytes)) "application/json")+      )+      names+  let outer = CellManifest cellId "alpha" leaseId 1 timestamp "0.1.0" (ManifestPayload (Text.replicate 64 "a") ("/nix/store/" <> Text.replicate 32 "a" <> "-fixture")) Cell.InfrastructureFailure 3600 artifacts+  LazyByteString.writeFile outerPath (encode outer)++environmentKeys :: Value -> [Key.Key]+environmentKeys (Object fields) = case KeyMap.lookup "environment" fields of+  Just (Object environment) -> KeyMap.keys environment+  _ -> []+environmentKeys _ = []++environmentField :: Text -> Value -> Maybe Value+environmentField name (Object fields) = case KeyMap.lookup "environment" fields of+  Just (Object environment) -> KeyMap.lookup (Key.fromText name) environment+  _ -> Nothing+environmentField _ _ = Nothing++topField :: Text -> Value -> Maybe Value+topField name (Object fields) = KeyMap.lookup (Key.fromText name) fields+topField _ _ = Nothing++testSubjects :: Map.Map LayerId Text+testSubjects = Map.singleton selftestLayer "mori://shinzui/kenshou"++testBundleUri :: Text+testBundleUri = "mori://shinzui/kenshou/okf/verification"
+ test/fixtures/consumer-records/01a0e106-c828-71ea-96f5-5a693335454f.md view
@@ -0,0 +1,225 @@+---+type: Verification Run+title: selftest/kernel/concurrency/worker-echo passed on released+description: Recorded selftest/kernel/concurrency/worker-echo run against cohort released+  with digest-pinned data.+generated:+  at: 2026-09-27T04:24:01.1618Z+  by: kenshou-record/0.1.0.0+cohort: released+compatibilityKey: 912b0f90f4a2793c5e7607615c07941556b5a8c9f3c5c0eedf23ddcace44e39a+component: kernel+components:+- package: keiki+  project: mori://shinzui/keiki+  source: hackage+  version: 0.9.1.0+- package: keiki-codec-json+  project: mori://shinzui/keiki+  source: hackage+  version: 0.9.1.0+- package: kiroku-store+  project: mori://shinzui/kiroku+  source: hackage+  version: 0.8.0.1+- package: kiroku-store-migrations+  project: mori://shinzui/kiroku+  source: hackage+  version: 0.4.0.0+- package: kiroku-otel+  project: mori://shinzui/kiroku+  source: hackage+  version: 0.2.0.8+- package: kiroku-metrics+  project: mori://shinzui/kiroku+  source: hackage+  version: 0.1.0.8+- package: kiroku-cli+  project: mori://shinzui/kiroku+  source: hackage+  version: 0.2.0.6+- package: shibuya-kiroku-adapter+  project: mori://shinzui/kiroku+  source: hackage+  version: 0.5.1.2+- package: keiro+  project: mori://shinzui/keiro+  source: hackage+  version: 0.17.0.0+- package: keiro-core+  project: mori://shinzui/keiro+  source: hackage+  version: 0.17.0.0+- package: keiro-pgmq+  project: mori://shinzui/keiro+  source: hackage+  version: 0.17.0.0+- package: keiro-migrations+  project: mori://shinzui/keiro+  source: hackage+  version: 0.17.0.0+- package: keiro-test-support+  project: mori://shinzui/keiro+  source: hackage+  version: 0.17.0.0+- package: shibuya-core+  project: mori://shinzui/shibuya+  source: hackage+  version: 0.9.0.3+- package: shibuya-metrics+  project: mori://shinzui/shibuya+  source: hackage+  version: 0.9.0.3+- package: shibuya-pgmq-adapter+  project: mori://shinzui/shibuya-pgmq-adapter+  source: hackage+  version: 0.16.0.0+- package: shibuya-kafka-adapter+  project: mori://shinzui/shibuya-kafka-adapter+  source: hackage+  version: 0.9.0.1+- package: kafka-effectful+  project: mori://shinzui/kafka-effectful+  source: hackage+  version: 0.3.1.0+- package: hw-kafka-streamly+  project: mori://shinzui/hw-kafka-streamly+  source: hackage+  version: 0.2.0.0+- package: hw-kafka-client+  project: mori://haskell-works/hw-kafka-client+  source: hackage+  version: 5.3.0+- package: pgmq-core+  project: mori://shinzui/pgmq-hs+  source: hackage+  version: 0.6.1.0+- package: pgmq-hasql+  project: mori://shinzui/pgmq-hs+  source: hackage+  version: 0.6.1.0+- package: pgmq-effectful+  project: mori://shinzui/pgmq-hs+  source: hackage+  version: 0.6.1.0+- package: pgmq-config+  project: mori://shinzui/pgmq-hs+  source: hackage+  version: 0.6.1.0+- package: pgmq-migration+  project: mori://shinzui/pgmq-hs+  source: hackage+  version: 0.6.1.0+- package: pg-migrate+  project: mori://shinzui/pg-migrate+  source: hackage+  version: 1.1.0.0+- package: pg-migrate-embed+  project: mori://shinzui/pg-migrate+  source: hackage+  version: 1.1.0.0+- package: pg-migrate-cli+  project: mori://shinzui/pg-migrate+  source: hackage+  version: 1.1.0.0+- package: pg-migrate-import-codd+  project: mori://shinzui/pg-migrate+  source: hackage+  version: 1.1.0.0+- package: pg-migrate-import-hasql-migration+  project: mori://shinzui/pg-migrate+  source: hackage+  version: 1.1.0.0+- package: ephemeral-pg+  project: mori://shinzui/ephemeral-pg+  source: hackage+  version: 0.3.1.0+- package: effectful+  project: mori://effectful/effectful+  source: hackage+  version: 2.6.1.0+- package: effectful-core+  project: mori://effectful/effectful+  source: hackage+  version: 2.6.1.0+- package: hs-opentelemetry-api+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.0.0.0+- package: hs-opentelemetry-sdk+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.0.0.0+- package: hs-opentelemetry-propagator-w3c+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.0.0.0+- package: hs-opentelemetry-exporter-in-memory+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.0.0.0+- package: hs-opentelemetry-exporter-otlp+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.0.0.0+- package: hs-opentelemetry-exporter-prometheus+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.0.0.0+- package: hs-opentelemetry-otlp+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.0.0.0+- package: hs-opentelemetry-semantic-conventions+  project: mori://iand675/hs-opentelemetry+  source: hackage+  version: 1.40.0.0+computations:+- VC-1+data:+- bytes: 488+  digest: 34347638cfa521b25923758549fa605f395a5ba42481cff68463d8a304d65f45+  kind: run-spec+  mediaType: application/json+  uri: gs://kenshou-evidence-tan-nb-exp/runs/01a0e106-c828-71ea-96f5-5a693335454f/run-spec.json+- bytes: 9952+  digest: 86c78d249fb7eebab8a0cd78ebbdcce93e09624b73af3b9250c5f31d381efda7+  kind: run-result+  mediaType: application/json+  uri: gs://kenshou-evidence-tan-nb-exp/runs/01a0e106-c828-71ea-96f5-5a693335454f/run-result.json+- bytes: 784+  digest: 797c3473a1cf1f22efafa20be5d45511a71b39beff8227d3e7c455ddbf238fc0+  kind: manifest+  mediaType: application/json+  uri: gs://kenshou-evidence-tan-nb-exp/runs/01a0e106-c828-71ea-96f5-5a693335454f/manifest.json+environment:+  arch: aarch64+  cores: 1+  cpuModel: Apple M1 Max+  ghc: Version {versionBranch = [9,12], versionTags = []}+  memoryBytes: 68719476736+  os: darwin+  postgres: none+finishedAt: 2026-09-27T04:01:53.238543Z+harnessDirty: false+harnessRevision: d0a5685dd880fa8be6bd9461ff6cbfbcfee93819+kind: concurrency+knobs:+- name: selftest.messages+  value: 1+layer: selftest+outcome: passed+placement: local+purpose: baseline+recordKind: run+runId: 01a0e106-c828-71ea-96f5-5a693335454f+scenario: selftest/kernel/concurrency/worker-echo+seed: 1844193026677625+solverPlanHash: f76053f0c0a8216bcc67ac5396f9ab9d0501a51a8f930ab08daa2791164e6b26+startedAt: 2026-09-27T04:01:53.19369Z+subject: mori://shinzui/keiro-runtime-kenshou+subjectKind: project+tier: smoke+---++This run produced the recorded outcome under [VC-1](/computations/run-outcome.md). Its raw data is linked by digest in the frontmatter.
+ test/fixtures/consumer-records/01a0f8be-8279-7667-ad13-3757c626211f.md view
@@ -0,0 +1,52 @@+---+type: Verification Run+title: keiro/outbox/benchmark/enqueue-to-publish comparison pass+description: Recorded comparison of 3 paired runs with digest-pinned data.+generated:+  at: 2026-10-01T18:50:02.428163Z+  by: kenshou-record/0.1.0.0+verified:+- at: 2026-10-01T19:08:25.043018Z+  by: process:kenshou-attester/0.1.0.0+comparison:+  baselineRuns:+  - /runs/keiro/2026/10/01a0f8a6-304f-7726-bcf1-ba6343c2bb1e.md+  - /runs/keiro/2026/10/01a0f8af-7dbe-7050-910d-9b3eca09b9a4.md+  - /runs/keiro/2026/10/01a0f8b7-233c-7471-96e8-29cb50b1f908.md+  baselineValue: 'off'+  candidateRuns:+  - /runs/keiro/2026/10/01a0f8a4-3c36-746e-a2ec-0c47a79ae357.md+  - /runs/keiro/2026/10/01a0f8b1-63ca-704b-8fea-fed0339b50f6.md+  - /runs/keiro/2026/10/01a0f8b5-3434-77e6-a3e9-ced441da2fa7.md+  candidateValue: serve-scraped+  design: baab+  factor: dimension+  factorName: telemetry.metrics+  verdict: pass+component: outbox+computations:+- VC-3+data:+- bytes: 6772+  digest: c64d80f306e0c8ca5cdc9a266978a2421de1e816810c21ee11e84904e1864f29+  kind: comparison+  mediaType: application/json+  uri: gs://kenshou-evidence-tan-nb-exp/runs/01a0f8be-8279-7667-ad13-3757c626211f/comparison.json+finishedAt: 2026-10-01T18:33:50.05348Z+harnessDirty: false+harnessRevision: 16ec2d667894996149149fd235fb7f6791818cf0+kind: benchmark+layer: keiro+outcome: passed+placement: cell+purpose: investigation+recordKind: comparison+runId: 01a0f8be-8279-7667-ad13-3757c626211f+scenario: keiro/outbox/benchmark/enqueue-to-publish+startedAt: 2026-10-01T18:33:49.944855Z+subject: mori://shinzui/keiro+subjectKind: project+tier: standard+---++The paired comparison produced the recorded verdict under [VC-3](/computations/paired-comparison.md). Its raw document is linked by digest in the frontmatter.
+ test/fixtures/consumer-records/SOURCE view
@@ -0,0 +1,15 @@+Verbatim data from mori://shinzui/keiro-runtime-kenshou, read by the consumer compatibility+group of kenshou-evidence/test/Main.hs.++layer-strings.json                     the layer identifiers its sealed records carry+run-result.hydration-cost.json         working-tree run 01a0d05d-17b6-7680-ba1c-1baa84dcd1ce+run-spec.selftest-null-kafka.json      kenshou-core/test/golden/run-spec.effective.json at commit+                                       a1648293443ad88ff1695ed800ff44c99993574e+run-spec.kafka-redpanda-container.json working-tree run 01a0d5bf-09e3-7727-91f9-5a6b3f04ce2c,+                                       run-spec.json+worker-init.legacy-kafka.json          constructed in the kenshou.worker-init/v1 shape that+                                       commit's WorkerInit codec writes; the consumer commits+                                       no worker-init document+01a0e106-c828-71ea-96f5-5a693335454f.md    docs/verification/runs/selftest/2026/09/ at commit a1648293443ad88ff1695ed800ff44c99993574e (a run record)+01a0f8be-8279-7667-ad13-3757c626211f.md    docs/verification/runs/keiro/2026/10/ at commit a1648293443ad88ff1695ed800ff44c99993574e (a comparison record)+record-paths.json                       the bundle-relative path each record above is committed at
+ test/fixtures/consumer-records/layer-strings.json view
@@ -0,0 +1,1 @@+["selftest", "pgmq", "kiroku", "shibuya", "kafka", "keiro", "runtime"]
+ test/fixtures/consumer-records/record-paths.json view
@@ -0,0 +1,4 @@+{+  "01a0e106-c828-71ea-96f5-5a693335454f.md": "runs/selftest/2026/09/01a0e106-c828-71ea-96f5-5a693335454f.md",+  "01a0f8be-8279-7667-ad13-3757c626211f.md": "runs/keiro/2026/10/01a0f8be-8279-7667-ad13-3757c626211f.md"+}
+ test/fixtures/consumer-records/run-result.hydration-cost.json view
@@ -0,0 +1,1 @@+{"blocking":false,"cohort":{"arch":"aarch64","cabalVersion":"3.16.1.0","cohort":"released","compiler":"ghc-9.12.4","components":[{"id":"keiki","moriUri":"mori://shinzui/keiki","packages":[{"name":"keiki","source":{"sha256":"cb2b80d6daaca71ab1a7aa8ce78026e76dd8a85570139bf02a6d676c5b02454b","type":"hackage"},"version":"0.9.1.0"},{"name":"keiki-codec-json","source":{"sha256":"f78f25ebcaf5eb91dfe53e2a175e4553a40b7ec2abba922f422546a0962152d8","type":"hackage"},"version":"0.9.1.0"}]},{"id":"kiroku","moriUri":"mori://shinzui/kiroku","packages":[{"name":"kiroku-store","source":{"sha256":"c56c8fa889e07fe7f075c3233976695699e406b185020b8b86f93de3969d6373","type":"hackage"},"version":"0.8.0.1"},{"name":"kiroku-store-migrations","source":{"sha256":"c5b99bb82aafc1194ef4f40adf4685958d2551979fc152c025f49fa972164370","type":"hackage"},"version":"0.4.0.0"},{"name":"kiroku-otel","source":{"sha256":"ba94e93d9e6396a1dc83fe09c2a4555e4ebf8d9639e5ffa9ff81785507393c1f","type":"hackage"},"version":"0.2.0.8"},{"name":"kiroku-metrics","source":{"sha256":"52a124ae7cf2f2c2eb3986d72f0124286e720c658b7f0d0ca1e7748476d5a13b","type":"hackage"},"version":"0.1.0.8"},{"name":"kiroku-cli","source":{"sha256":"9a496509212705aafa9640df7c3f87dec7deb801e78b58d35467290b86d0b327","type":"hackage"},"version":"0.2.0.6"},{"name":"shibuya-kiroku-adapter","source":{"sha256":"e7dcd2560bec0e196bc2687df360486358f75eeb2859b06061abb76914974949","type":"hackage"},"version":"0.5.1.2"}]},{"id":"keiro","moriUri":"mori://shinzui/keiro","packages":[{"name":"keiro","source":{"sha256":"f2b959b35e922557b401e6ba4db24b2bd42ec06824aa6d1c63b95e49ec4b4ad8","type":"hackage"},"version":"0.17.0.0"},{"name":"keiro-core","source":{"sha256":"afb7c90326e64fe6835a5746c54e48a828816389a8a7fee75b7f8150066d07d5","type":"hackage"},"version":"0.17.0.0"},{"name":"keiro-pgmq","source":{"sha256":"41595c6df2df16fd99e6cd0b6fb172267779c6d0d86519bbd49c765e00a858cd","type":"hackage"},"version":"0.17.0.0"},{"name":"keiro-migrations","source":{"sha256":"4eec6cd356c1989c817806713fd38dcd04f2497572b879eb2a423e1f1cf02d47","type":"hackage"},"version":"0.17.0.0"},{"name":"keiro-test-support","source":{"sha256":"e1772a2c252ead9641b350b81b55f4c99590af6902a628578feb561eb0dd89ed","type":"hackage"},"version":"0.17.0.0"}]},{"id":"shibuya","moriUri":"mori://shinzui/shibuya","packages":[{"name":"shibuya-core","source":{"sha256":"497e3698632c13ed674df2c81a1d9ea3090c05ab43bc4deb0b718ce7d585eca7","type":"hackage"},"version":"0.9.0.3"},{"name":"shibuya-metrics","source":{"sha256":"74df49e32d9ca6caea02ee0c86c7d96845334d473bb44e66413b7e8d86484a91","type":"hackage"},"version":"0.9.0.3"}]},{"id":"shibuya-pgmq-adapter","moriUri":"mori://shinzui/shibuya-pgmq-adapter","packages":[{"name":"shibuya-pgmq-adapter","source":{"sha256":"45b658364ec61bf178232643cf9c60ee907bbf2e87fa02ddeb43eaa990cb5758","type":"hackage"},"version":"0.16.0.0"}]},{"id":"shibuya-kafka-adapter","moriUri":"mori://shinzui/shibuya-kafka-adapter","packages":[{"name":"shibuya-kafka-adapter","source":{"sha256":"1918bae0c591ab47ba45d044a50170e7a340bddb1b562752cd16f556e86314e6","type":"hackage"},"version":"0.9.0.1"}]},{"id":"kafka-effectful","moriUri":"mori://shinzui/kafka-effectful","packages":[{"name":"kafka-effectful","source":{"sha256":"2ac29e16238ac0d52ea5c8377ab28f50fbb12a7a35c1922acc6fac129f0bf66c","type":"hackage"},"version":"0.3.1.0"}]},{"id":"hw-kafka-streamly","moriUri":"mori://shinzui/hw-kafka-streamly","packages":[{"name":"hw-kafka-streamly","source":{"sha256":"03c331922c28a7b378031327e702b0384c9974811b533d0b49a861e3d223a3b1","type":"hackage"},"version":"0.2.0.0"}]},{"id":"hw-kafka-client","moriUri":"mori://haskell-works/hw-kafka-client","packages":[{"name":"hw-kafka-client","source":{"sha256":"2425ed40efc4c9c54f7e6ea01b0e4c6f5deba73e9662d14599086e17eebe4733","type":"hackage"},"version":"5.3.0"}]},{"id":"pgmq-hs","moriUri":"mori://shinzui/pgmq-hs","packages":[{"name":"pgmq-core","source":{"sha256":"c391869f57be3d3a7c1ad75651b1a9bd419583330f016e3e73f152a87dca0f69","type":"hackage"},"version":"0.6.1.0"},{"name":"pgmq-hasql","source":{"sha256":"4aea5f0c55e3391f3d14c891d176953e65b66d3bf0e3527143309d66f1df0f07","type":"hackage"},"version":"0.6.1.0"},{"name":"pgmq-effectful","source":{"sha256":"8824a0ff739b0d0d6ef63f1e663ac270f6e4ab0dbc95c8b31b7e261744c9a291","type":"hackage"},"version":"0.6.1.0"},{"name":"pgmq-config","source":{"sha256":"08c9295ce20a72115b3095a376554eb93c425a32fbca93ab2f089661ce26ec59","type":"hackage"},"version":"0.6.1.0"},{"name":"pgmq-migration","source":{"sha256":"bfb5f4f1ae1e2a72204fcbfcf1631f137a36259168f6f1b41b50d7f3bfd3aaf7","type":"hackage"},"version":"0.6.1.0"}]},{"id":"pg-migrate","moriUri":"mori://shinzui/pg-migrate","packages":[{"name":"pg-migrate","source":{"sha256":"23df4d45e41fe554acc85869c35349c1dc19250d54d968b60043aa90e13cabd5","type":"hackage"},"version":"1.1.0.0"},{"name":"pg-migrate-embed","source":{"sha256":"514e8da3816f021ce34bb5aa75853b06c46a634481118143c8a9d4c6a9968688","type":"hackage"},"version":"1.1.0.0"},{"name":"pg-migrate-cli","source":{"sha256":"a23a615c198196de76700a81fdb30acf4019dd2564fc65fd3a82109d24f3ce34","type":"hackage"},"version":"1.1.0.0"},{"name":"pg-migrate-import-codd","source":{"sha256":"95034070deff11b0cda8161b73a74b0966b2f519ef24bfa474924712be07c8ee","type":"hackage"},"version":"1.1.0.0"},{"name":"pg-migrate-import-hasql-migration","source":{"sha256":"191626960a3a82b7bcaeafdc746ecc9b29f586f79c3100936ad151ec9ceef19f","type":"hackage"},"version":"1.1.0.0"}]},{"id":"ephemeral-pg","moriUri":"mori://shinzui/ephemeral-pg","packages":[{"name":"ephemeral-pg","source":{"sha256":"958e476698bf76808f0bebc9c9d16332091e3a2a060b1d5d5ca6836ed9d10c2a","type":"hackage"},"version":"0.3.1.0"}]},{"id":"effectful","moriUri":"mori://effectful/effectful","packages":[{"name":"effectful","source":{"sha256":"58752ce8aa49ff9ce713011272babb38c9f89cb6513558da612faf724b810ece","type":"hackage"},"version":"2.6.1.0"},{"name":"effectful-core","source":{"sha256":"9c679af666dafb02220630a4dd389c24beb6e9c4fdbf1ba09e6601933db869f2","type":"hackage"},"version":"2.6.1.0"}]},{"id":"hs-opentelemetry","moriUri":"mori://iand675/hs-opentelemetry","packages":[{"name":"hs-opentelemetry-api","source":{"sha256":"797c1a6c9320c5d7be92920d437398f756745e042351756bb8af7d094662612d","type":"hackage"},"version":"1.0.0.0"},{"name":"hs-opentelemetry-sdk","source":{"sha256":"e4e5040fd2366542feb7d0148c09e1a65a282486a542bb5ceba3b41fda5dae61","type":"hackage"},"version":"1.0.0.0"},{"name":"hs-opentelemetry-propagator-w3c","source":{"sha256":"b09feadcd2d3a41561a729373cefb0a420ba492e9e196f790b46d1798aa2e866","type":"hackage"},"version":"1.0.0.0"},{"name":"hs-opentelemetry-exporter-in-memory","source":{"sha256":"b2616d71bebb4f7317b2794da22a94a9a0fb84a400a15354440fa9a72df3d660","type":"hackage"},"version":"1.0.0.0"},{"name":"hs-opentelemetry-exporter-otlp","source":{"sha256":"692bd3e2de2be465589997570085e02e87cba5bc10d68deaac1f848d8ae27467","type":"hackage"},"version":"1.0.0.0"},{"name":"hs-opentelemetry-exporter-prometheus","source":{"sha256":"4f8d6313c8198c9ad58c0a1ed0137afc99cc25872d00ccd86043402c5d3f025f","type":"hackage"},"version":"1.0.0.0"},{"name":"hs-opentelemetry-otlp","source":{"sha256":"655be27046d80473eaae15034f2541cf670168bf4236c5c5d48d37d56ad2342f","type":"hackage"},"version":"1.0.0.0"},{"name":"hs-opentelemetry-semantic-conventions","source":{"sha256":"c9ba86a46a91a314df57506cab3154d5feae7c38eea345f70e8b5b20b7130ef1","type":"hackage"},"version":"1.40.0.0"}]}],"descriptorSha256":"44c8d2c5a775e67346f6f84abbbe0a9ce15550df747c4647f2c8857fcd82370b","indexState":"2026-09-20T13:44:47Z","os":"osx","planHash":"sha256:baf1098e5271312f0063b5c720c67c5dafedb44141fe9f10fdfb9841ed5f2ab1","schema":"kenshou.cohort-identity/v1"},"comparison":null,"compatibility":{"algorithm":"kenshou.compat-key/v1","comparisonKey":"sha256:dacb05fc095929d0a3723b0fc017cf23f2a81efc900c583ba47ee10a7a1ca843","inputs":{"cohortPlanHash":"sha256:baf1098e5271312f0063b5c720c67c5dafedb44141fe9f10fdfb9841ed5f2ab1","dimensions":{"pg.durability":"durable","pg.version":"18","telemetry.metrics":"off","telemetry.tracing":"off"},"knobs":{"command.duration-seconds":15,"command.page-size":64,"command.stream-length":100,"load.abort-lag-ms":30000,"load.executors":256,"load.max-lag-ms":1000,"load.model":"closed","load.rate-per-second":100,"load.think-time-us":10000,"load.workers":1,"measure.histogram-digits":3,"measure.interval-histogram-seconds":10,"measure.pg-statements":"snapshots","measure.raw-sample-one-in":100,"measure.raw-samples":"full","measure.sample-interval-ms":1000,"snapshot.policy":"every-100"},"machineProfile":"local/darwin-aarch64","phases":{"drainSeconds":5,"steadySeconds":120,"warmUpSeconds":5},"postgresProfile":{"collation":"C","encoding":"UTF8","serverVersion":"18.6","serverVersionNum":180006,"settings":{"autovacuum":"on","checkpoint_completion_target":"0.9","checkpoint_timeout":"5min","effective_cache_size":"4GB","fsync":"on","full_page_writes":"on","max_connections":"100","max_wal_size":"1GB","random_page_cost":"4","shared_buffers":"128MB","shared_preload_libraries":"pg_stat_statements","synchronous_commit":"on","wal_buffers":"4MB","wal_level":"replica","work_mem":"4MB"},"superuser":true},"scenario":"keiro/command/benchmark/hydration-cost","scenarioRevision":1,"schemas":["kiroku","keiro"],"suiteVersion":"0.1.0.0"},"seriesKey":"sha256:7cd4ca25d8d9e18fede3d10868b2a903ba244f9bb15034fba8b21ac5b419a260"},"component":"command","exitCode":0,"failures":[],"fingerprint":{"cell":null,"extraPostgres":{},"host":{"arch":"aarch64","cpuModel":"Apple M1 Max","hostname":"sungkyung","kernel":"25.6.0","logicalCores":1,"memoryBytes":68719476736,"os":"darwin"},"kenshou":{"dirty":true,"executableSha256":"sha256:5ff2889befcae3246c9596f47cf3bdb12c2a51dcca7ad5ff3aae181867edfe24","revision":"c2e97aaa1456222c14eaada5e6def332ffb1eaac","version":"0.1.0.0"},"machineProfile":"local/darwin-aarch64","placement":"local","postgres":{"collation":"C","encoding":"UTF8","serverVersion":"18.6","serverVersionNum":180006,"settings":{"autovacuum":"on","checkpoint_completion_target":"0.9","checkpoint_timeout":"5min","effective_cache_size":"4GB","fsync":"on","full_page_writes":"on","max_connections":"100","max_wal_size":"1GB","random_page_cost":"4","shared_buffers":"128MB","shared_preload_libraries":"pg_stat_statements","synchronous_commit":"on","wal_buffers":"4MB","wal_level":"replica","work_mem":"4MB"},"superuser":true},"runtime":{"capabilities":1,"ghc":"Version {versionBranch = [9,12], versionTags = []}","rtsStats":true,"threaded":true}},"invocation":{"argv":["run","keiro/command/benchmark/hydration-cost","--set","command.stream-length=100","--set","snapshot.policy=every-100","--set","command.page-size=64","--set","command.duration-seconds=15","--set","load.think-time-us=10000","--out","runs"],"workingDirectory":"/Users/shinzui/Keikaku/bokuno/keiro-runtime-kenshou"},"kind":"benchmark","knownDefect":null,"layer":"keiro","observations":[],"outcome":"passed","reason":null,"runId":"01a0d05d-17b6-7680-ba1c-1baa84dcd1ce","scenario":"keiro/command/benchmark/hydration-cost","scenarioRevision":1,"schema":"kenshou.run-result/v1","seed":7496389893774028,"spec":{"path":"run-spec.json","sha256":"sha256:55eadbb2680e2795a98f0d5fe871b6b3a74d1c7a9fe779ce3f72b90af309851f"},"summaries":{"diagnosis":{},"measurements":{"hydration-cost":{"completed":1517,"failed":0,"pageSize":64,"snapshotPolicy":"every-100","streamLength":100},"measurements":{"algorithm":{"name":"kenshou-summary","version":1},"grade":"benchmark","gradeReasons":[],"health":[{"detail":"PostgreSQL checkpoint activity in the steady window","evidence":{"completed":0,"overlapFraction":0,"started":0},"fromMonoNs":5021908000,"gate":"checkpoint-in-window","severity":"info","toMonoNs":20023242000}],"metrics":{"op.hydrate-command.error-rate":{"unit":"ratio","value":0},"op.hydrate-command.latency.max":{"unit":"ns","value":11817000},"op.hydrate-command.latency.mean":{"unit":"ns","value":2311396.276595745},"op.hydrate-command.latency.p50":{"unit":"ns","value":2168831},"op.hydrate-command.latency.p90":{"unit":"ns","value":3172351},"op.hydrate-command.latency.p99":{"unit":"ns","value":5656575},"op.hydrate-command.latency.p999":{"unit":"ns","value":9904127},"op.hydrate-command.service.p99":{"unit":"ns","value":5656575},"op.hydrate-command.throughput":{"unit":"ops/s","value":75.19331280804761},"op.hydrate-command.units-throughput":{"unit":"units/s","value":75.19331280804761},"pg.checkpoints-in-window":{"unit":"count","value":0},"pg.wal-bytes-per-op":{"unit":"bytes/op","value":1238.9193262411347},"proc.cpu-utilisation":{"unit":"ratio","value":0.10350006206114737},"proc.rss-max":{"unit":"bytes","value":80510976},"rts.alloc-bytes-per-op":{"unit":"bytes/op","value":1137314.0780141845},"rts.alloc-rate":{"unit":"bytes/s","value":8.551841322911683e7},"rts.gc-productivity":{"unit":"ratio","value":0.9980067567973566},"rts.live-bytes-major-mean":{"unit":"bytes","value":6487592},"rts.max-live-bytes":{"unit":"bytes","value":6487592}},"ops":{"hydrate-command":{"coordinatedOmissionRisk":false,"count":1128,"errors":{},"failures":0,"latencyBasis":"intended-start","latencyNs":{"max":11817000,"mean":2311396.276595745,"p50":2168831,"p90":3172351,"p99":5656575,"p999":9904127},"reportedSuccesses":1128,"serviceNs":{"max":11817000,"mean":2311396.276595745,"p50":2168831,"p90":3172351,"p99":5656575,"p999":9904127},"throughput":75.19331280804761,"unitsPerSecond":75.19331280804761}},"schema":"kenshou.measurements/v1","window":{"steadyEndMonoNs":20023242000,"steadySeconds":15.001334,"steadyStartMonoNs":5021908000}}},"telemetry":{},"verdicts":{"keiro/command/benchmark/hydration-cost":{"checks":3,"failures":[]}}},"tier":"standard","timings":{"clock":"GHC.Clock.getMonotonicTimeNSec","durationSeconds":21.90976,"endedAt":"2026-09-23T22:22:58.892721Z","phases":[],"startedAt":"2026-09-23T22:22:36.982961Z"}}
+ test/fixtures/consumer-records/run-spec.kafka-redpanda-container.json view
@@ -0,0 +1,1 @@+{"cohortExpectation":null,"comparison":null,"dimensions":{"telemetry.metrics":"off","telemetry.tracing":"off"},"environment":{"kafka":{"backend":"redpanda-container","brokerProps":{"auto_create_topics_enabled":"false"}},"machineProfile":"local/darwin-aarch64","placement":"local","postgres":null,"telemetry":null},"knobs":{"kafka.partitions":3},"labels":{},"phases":{"drainSeconds":0,"steadySeconds":0,"warmUpSeconds":0},"runId":"01a0d5bf-09e3-7727-91f9-5a6b3f04ce2c","scenario":"kafka/broker/correctness/fixture-roundtrip","scenarioRevision":1,"schema":"kenshou.run-spec/v1","seed":491709693164476,"timeoutSeconds":120}
+ test/fixtures/consumer-records/run-spec.selftest-null-kafka.json view
@@ -0,0 +1,1 @@+{"schema":"kenshou.run-spec/v1","runId":"01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55","scenario":"selftest/kernel/correctness/postgres-roundtrip","scenarioRevision":1,"knobs":{"selftest.events":3},"dimensions":{"pg.durability":"durable","pg.version":"18"},"seed":7,"phases":{"warmUpSeconds":0,"steadySeconds":0,"drainSeconds":0},"timeoutSeconds":120,"environment":{"placement":"local","machineProfile":"local/darwin-aarch64","postgres":{"mode":"ephemeral","settings":{}},"kafka":null,"telemetry":null},"cohortExpectation":null,"comparison":null,"labels":{}}
+ test/fixtures/consumer-records/worker-init.legacy-kafka.json view
@@ -0,0 +1,1 @@+{"schema":"kenshou.worker-init/v1","runId":"01997f3a-5b7c-7e21-8a44-0d6c2f9b1e55","scenario":"selftest/kernel/correctness/postgres-roundtrip","role":"selftest/worker","instance":"worker-0","seed":7,"knobs":{},"dimensions":{},"postgres":null,"kafka":{"backend":"external","brokers":["10.0.0.5:9092"],"lanes":0},"telemetry":null,"outDir":"/tmp/out","args":{}}
+ test/fixtures/profile-invalid/cases.json view
@@ -0,0 +1,22 @@+[+  {"name":"unknown-field","target":"run","op":"set","path":"/p99Millis","value":12,"expect":"frontmatter field not declared by profile: p99Millis"},+  {"name":"relative-uri","target":"run","op":"set","path":"/data/0/uri","value":"runs/sample","expect":"data[0].uri must match format uri"},+  {"name":"duplicate-data-uri","target":"run","op":"copy","from":"/data/0","path":"/data/-","expect":"duplicate"},+  {"name":"dangling-computation","target":"run","op":"set","path":"/computations/0","value":"VC-9","expect":"VC-9"},+  {"name":"dangling-previous-run","target":"run","op":"set","path":"/previousRun","value":"/runs/selftest/2026/09/missing","expect":"previousRun"},+  {"name":"missing-cohort","target":"run","op":"remove","path":"/cohort","expect":"missing profile-required field: cohort"},+  {"name":"git-component-without-revision","target":"run","op":"set","path":"/components/0/source","value":"git","expect":"revision"},+  {"name":"textual-seed","target":"run","op":"set","path":"/seed","value":"one","expect":"seed"},+  {"name":"textual-dirty-flag","target":"run","op":"set","path":"/harnessDirty","value":"false","expect":"harnessDirty"},+  {"name":"unknown-outcome","target":"run","op":"set","path":"/outcome","value":"succeeded","expect":"outcome"},+  {"name":"invalid-layer","target":"run","op":"set","path":"/layer","value":"other","expect":"layer"},+  {"name":"invalid-kind","target":"run","op":"set","path":"/kind","value":"performance","expect":"kind"},+  {"name":"missing-generated","target":"run","op":"remove","path":"/generated","expect":"generated"},+  {"name":"wrong-run-path","target":"run","op":"set","path":"/purpose","value":"baseline","file":"misc/wrong-path","expect":"path"},+  {"name":"missing-comparison","target":"comparison","op":"remove","path":"/comparison","expect":"comparison"},+  {"name":"dangling-baseline","target":"comparison","op":"set","path":"/comparison/baselineRuns/0","value":"/runs/selftest/2026/09/missing","expect":"baselineRuns"},+  {"name":"non-actor-attester","target":"attestation","op":"set","path":"/attester","value":"automated","expect":"attester"},+  {"name":"nonhuman-exception","target":"attestation","op":"set","path":"/exception","value":{"authority":"process:robot","reason":"fixture"},"expect":"authority"},+  {"name":"duplicate-check","target":"attestation","op":"copy","from":"/checks/0","path":"/checks/-","expect":"duplicate"},+  {"name":"unknown-check","target":"attestation","op":"set","path":"/checks/0/name","value":"made-up","expect":"checks"}+]