Skip to content

finos/morphir-scala / .claude/skills/squire

squire

Verified

Use when morphir-scala work needs environment diagnostics or build/network/sandbox unblocking; reference repository management; branch lifecycle refresh after squash-merging a target branch into main; Morphir IR spec/schema sync or export; or task-tracking configuration and beads mode resolution.

FileKindSize
.gitignoreother180 B
.mill-versionother19 B
README.mdmarkdown7.6 KB
SKILL.mdskill md6.7 KB
SquireBranch.scalaother6.4 KB
SquireCellar.scalaother7.4 KB
SquireChangelog.scalaother20.6 KB
SquireDoctor.scalaother13.9 KB
SquireEnv.scalaother16.2 KB
SquireModel.scalaother4.9 KB
SquireProcess.scalaother1.5 KB
SquireRepo.scalaother30.4 KB
SquireSchemas.scalaother25.2 KB
SquireSpec.scalaother35.6 KB
SquireTests.scalaother323.1 KB
SquireTracking.scalaother16.4 KB
SquireVersionCorpus.scalaother3.3 KB
millexecutablescript6.5 KB
mill.batscript9.7 KB
references/branch.mdmarkdown4.4 KB
references/cellar.mdmarkdown4.4 KB
references/doctor.mdmarkdown5.3 KB
references/env.mdmarkdown3.9 KB
references/mill-morphir.mdmarkdown1.6 KB
references/repo.mdmarkdown7.6 KB
references/spec-sync.mdmarkdown10.6 KB
references/tracking.mdmarkdown10.9 KB
squireexecutablescript1.2 KB
squire.batscript1.8 KB
squire.scalaother36.8 KB
test-resources/META-INF/services/kyo.test.Testother275 B
test-resources/schemas/morphir-ir-v4.jsonconfig107.1 KB
test-resources/schemas/morphir-ir-v4.yamlconfig74.2 KB

SquireTests.scala

View on GitHub
//| scalaVersion: 3.9.0
//| mainClass: kyo.test.runner.Cli
//| resources: [test-resources]
//| moduleDeps: [squire.scala, SquireCellar.scala, SquireRepo.scala, SquireTracking.scala, SquireChangelog.scala, SquireVersionCorpus.scala]
//| mvnDeps:
//| - io.getkyo::kyo-test-api:1.0.0-RC7
//| - io.getkyo::kyo-test-runner:1.0.0-RC7

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import caseapp.core.Scala3Helpers.HelpWithOps
import caseapp.core.parser.Parser
import kyo.*
import kyo.test.*
import scala.jdk.CollectionConverters.*

final case class LaunchedSquire(exitCode: Int, stdout: String, stderr: String)

final case class TaskInvocation(program: String, arguments: Chunk[String])

object SquireLauncherFixtures:
  def run(
      skillDirectory: java.nio.file.Path,
      arguments: Seq[String],
      environment: Map[String, String] = Map.empty,
      launcher: String = "squire"
  ): LaunchedSquire =
    val command =
      if launcher.endsWith(".bat") then Seq("cmd", "/d", "/c", skillDirectory.resolve(launcher).toString)
      else Seq(skillDirectory.resolve(launcher).toString)
    val builder = new ProcessBuilder((command ++ arguments)*)
      .directory(skillDirectory.toFile)
    builder.environment().putAll(environment.asJava)
    val process = builder.start()
    val stdout  = new String(process.getInputStream.readAllBytes(), StandardCharsets.UTF_8)
    val stderr  = new String(process.getErrorStream.readAllBytes(), StandardCharsets.UTF_8)
    LaunchedSquire(process.waitFor(), stdout, stderr)

  /**
   * Publish an executable atomically. A plain `writeString` + `chmod` + immediate `exec` races on Linux CI with
   * `ETXTBSY` (`/usr/bin/env: 'bash': Text file busy`) when the kernel still holds the write mapping. Write a sibling
   * temp, force the bytes out, mark executable, then rename into place.
   */
  def executable(path: java.nio.file.Path, text: String): Unit =
    val parent = Option(path.getParent).getOrElse {
      throw new AssertionError(s"executable path has no parent: $path")
    }
    Files.createDirectories(parent)
    val temp = Files.createTempFile(parent, path.getFileName.toString + ".", ".tmp")
    try
      Files.writeString(temp, text, StandardCharsets.UTF_8)
      val channel = java.nio.channels.FileChannel.open(temp, java.nio.file.StandardOpenOption.WRITE)
      try channel.force(true)
      finally channel.close()
      if !temp.toFile.setExecutable(true) then
        throw new AssertionError(s"could not make $temp executable")
      try
        Files.move(
          temp,
          path,
          java.nio.file.StandardCopyOption.REPLACE_EXISTING,
          java.nio.file.StandardCopyOption.ATOMIC_MOVE
        )
      catch
        case _: java.nio.file.AtomicMoveNotSupportedException =>
          Files.move(temp, path, java.nio.file.StandardCopyOption.REPLACE_EXISTING)
    catch
      case error: Exception =>
        Files.deleteIfExists(temp)
        throw error
    if !path.toFile.canExecute && !path.toFile.setExecutable(true) then
      throw new AssertionError(s"could not make $path executable")

object SquireCiPolicy:
  val SupportedBranches = List("main", "0.4.x")

  /**
   * The two Maven Central stand-down clauses shared by every Sonatype-uploading job: the MORPHIR_RELEASE_MAVEN_CENTRAL
   * repository variable, and the maven_central input on a manual dispatch (absent on every other event, which the
   * event-name guard covers). They are policy — a GitHub-Releases-only release depends on every job honouring them.
   */
  val MavenCentralSwitchClauses =
    " && vars.MORPHIR_RELEASE_MAVEN_CENTRAL != 'false' && " +
      "(github.event_name != 'workflow_dispatch' || inputs.maven_central)"

  /**
   * ci.yml's publish job is snapshots and branch milestones only. Release tags are deliberately absent: a tag push
   * stages a draft GitHub release, and every release-tag Sonatype upload lives in release-publish.yml behind the human
   * act of publishing that draft. A tag ref reappearing here would put an irreversible Maven Central upload before that
   * promotion gate.
   */
  val PublishPredicate =
    "github.repository == 'finos/morphir-scala' && " +
      "(github.ref == 'refs/heads/main' || " +
      "github.ref == 'refs/heads/0.4.x')" +
      MavenCentralSwitchClauses

  /**
   * release-publish.yml routes by the tag its `target` job resolved, one namespace per job. The repository guard lives
   * on `target` alone — when it skips, every dependent publish job skips with it — and each publish job carries the
   * Maven Central stand-down clauses.
   */
  val ReleasePublishTargetPredicate    = "github.repository == 'finos/morphir-scala'"
  val ReleasePublishLibrariesPredicate =
    "startsWith(needs.target.outputs.tag, 'v')" + MavenCentralSwitchClauses
  val ReleasePublishPluginsPredicate =
    "startsWith(needs.target.outputs.tag, 'mill-plugins/v')" + MavenCentralSwitchClauses
  val CachePredicate =
    "github.ref == 'refs/heads/main' || " +
      "github.ref == 'refs/heads/0.4.x' || " +
      "startsWith(github.ref, 'refs/tags/')"

  /**
   * The non-classic JVM targets, in the order `testJVMPlatform` names them.
   *
   * One definition, read two ways, because they used to be two lists that could drift apart in silence — and did.
   * [[assertJvmPlatformPolicy]] holds `build.mill`'s alias to these strings, and [[assertJvmTargetInventory]] holds
   * these strings to what Mill actually resolves, so together they say the alias builds every non-classic JVM target
   * and nothing more. When the two were spelled separately, a module could be absent from the alias while the census
   * still passed: that is how the CommonMark conformance suite in `langkit.markdown.scalatags` was never once run on a
   * runner.
   *
   * The lists are not identical to each other. `benchmarks` compiles but has no test or publish target, and
   * `langkit.markdown.trees` compiles and tests but does not publish, so parsley and neotype stay out of the published
   * artifacts.
   */
  val JvmCompileSelectors = List(
    "morphir.{appkit,benchmarks,buildkit.core,connector.github,extensibility,intelligence.sdk,kit.kyo,knowledge.logic,knowledge.okf,langkit.core,langkit.elm.compiler.api,langkit.elm.compiler.ir,langkit.elm.compiler.mep,langkit.elm.core,langkit.markdown,langkit.markdown.kyo.ui,langkit.markdown.scalatags,langkit.markdown.trees,langkit.trees,lib.interop,model,model.compat.v3,naming,prelude,ui,web.server}.jvm.__.compile"
  )
  val JvmPublishSelectors = List(
    "morphir.{appkit,buildkit.core,connector.github,extensibility,knowledge.logic,knowledge.okf,langkit.core,langkit.markdown,langkit.markdown.kyo.ui,langkit.markdown.scalatags,lib.interop,model,model.compat.v3,naming,prelude,ui}.jvm.publishArtifacts"
  )
  val JvmTestSelectors = List(
    "morphir.{appkit,buildkit.core,connector.github,intelligence.sdk,kit.kyo,knowledge.logic,knowledge.okf,langkit.core,langkit.elm.compiler.api,langkit.elm.compiler.ir,langkit.elm.compiler.mep,langkit.elm.core,langkit.markdown,langkit.markdown.kyo.ui,langkit.markdown.scalatags,langkit.markdown.trees,langkit.trees,model,model.compat.v3,naming,prelude,ui,web.server}.jvm.test"
  )

  /** The Cucumber-style integration suite, which is not a `.jvm` module and so is not in the census. */
  val JvmItestSelector = "morphir.langkit.itest.testCached"

  /** The v3 reader's MCK adapter protocol tests: a plain JVM module, not a `.jvm` one, so also outside the census. */
  val MckAdapterTestSelector = "morphir.model.compat.v3.mck.test"

  /** The source and manifest guard for the Elm extension's Kyo architecture boundary. */
  val ElmExtensionArchitectureSelector = "millbuildHelpers.elmExtensionArchitectureCheck"

  /** Build-logic tests, which are outside the Morphir JVM module census. */
  val MillbuildHelpersTestSelector = "millbuildHelpers.test"

  /** Exactly what `testJVMPlatform` must name, in order. */
  val JvmPlatformAliasMembers: List[String] =
    JvmCompileSelectors ::: JvmPublishSelectors ::: JvmTestSelectors :::
      List(JvmItestSelector, MckAdapterTestSelector, ElmExtensionArchitectureSelector, MillbuildHelpersTestSelector)

  val SnapshotCommands = List(
    "echo \"MORPHIR_PUBLISH_MODE=snapshot\" >> \"$GITHUB_ENV\"",
    "echo \"MORPHIR_PUBLISH_BRANCH=${GITHUB_REF_NAME}\" >> \"$GITHUB_ENV\""
  )

  private def fail(message: String): Nothing = throw new AssertionError(message)

  private def expect(condition: Boolean, message: String): Unit =
    if !condition then fail(message)

  private def leadingSpaces(line: String): Int = line.length - line.dropWhile(_ == ' ').length

  def indentedBlock(text: String, header: String, indent: Int): String =
    val lines          = text.linesIterator.toList
    val expectedHeader = " " * indent + header
    val start          = lines.indexWhere(_ == expectedHeader)
    if start < 0 then fail(s"missing block: $header")
    lines.drop(start + 1).takeWhile(line => line.trim.isEmpty || leadingSpaces(line) > indent).mkString("\n")

  def inlineList(block: String, key: String): List[String] =
    val value = block.linesIterator.map(_.trim).find(_.startsWith(s"$key:")).map(_.drop(key.length + 1).trim)
      .getOrElse(fail(s"missing inline list: $key"))
    expect(value.startsWith("[") && value.endsWith("]"), s"invalid inline list: $key")
    value.drop(1).dropRight(1).split(',').iterator.map(_.trim.stripPrefix("\"").stripSuffix("\"")
      .stripPrefix("'").stripSuffix("'")).filter(_.nonEmpty).toList

  def scalar(block: String, key: String): String =
    block.linesIterator.map(_.trim).find(_.startsWith(s"$key:")).map(_.drop(key.length + 1).trim)
      .getOrElse(fail(s"missing scalar: $key"))

  def normalizeExpression(expression: String): String = expression.split("\\s+").filter(_.nonEmpty).mkString(" ")

  def count(text: String, needle: String): Int =
    Iterator.iterate(text.indexOf(needle)) { index =>
      if index < 0 then -1 else text.indexOf(needle, index + needle.length)
    }.takeWhile(_ >= 0).size

  private val YamlKey = """^\s*(?:\"([^\"]+)\"|'([^']+)'|([A-Za-z][A-Za-z0-9_-]*))\s*:.*$""".r

  def hasYamlKey(block: String, key: String): Boolean =
    block.linesIterator.exists {
      case YamlKey(doubleQuoted, singleQuoted, bare) => List(doubleQuoted, singleQuoted, bare).contains(key)
      case _                                         => false
    }

  def yamlKeysAtIndent(block: String, indent: Int): List[String] =
    block.linesIterator.collect {
      case line @ YamlKey(doubleQuoted, singleQuoted, bare) if leadingSpaces(line) == indent =>
        List(doubleQuoted, singleQuoted, bare).find(_ != null).get
    }.toList

  def unquoteYamlScalar(value: String): String =
    if value.length >= 2 &&
      ((value.head == '"' && value.last == '"') || (value.head == '\'' && value.last == '\''))
    then value.drop(1).dropRight(1)
    else value

  def yamlSequenceEntries(block: String, indent: Int): List[String] =
    block.linesIterator.filter { line =>
      val item = line.drop(indent)
      leadingSpaces(line) == indent && (item == "-" || item.startsWith("- "))
    }.toList

  def replaceOnce(text: String, oldValue: String, newValue: String): String =
    val index = text.indexOf(oldValue)
    if index < 0 then fail(s"mutation target not found: $oldValue")
    text.substring(0, index) + newValue + text.substring(index + oldValue.length)

  def publishBlock(workflow: String): String =
    expect(workflow.linesIterator.count(_ == "  publish:") == 1, "workflow must contain exactly one publish job")
    indentedBlock(workflow, "publish:", 2)

  def assertBranchPolicy(workflow: String): Unit =
    val events = indentedBlock(workflow, "on:", 0)
    List("pull_request:", "push:").foreach { event =>
      val branches = inlineList(indentedBlock(events, event, 2), "branches")
      expect(branches == SupportedBranches, s"$event branches were $branches")
    }

  def assertPublishPolicy(workflow: String): Unit =
    val publish = publishBlock(workflow)
    expect(scalar(publish, "needs") == "[ci]", "publish must depend only on aggregate ci")
    expect(scalar(publish, "if") == PublishPredicate, "publish predicate does not match the release allowlist")
    expect(
      publish.linesIterator.count(_ == "      - name: Release") == 1,
      "publish job must contain exactly one Release step"
    )
    val release = indentedBlock(publish, "- name: Release", 6)
    expect(
      count(release, "./mill --ticker false -i ci.publish") == 1,
      "Release step must contain the Sonatype publish invocation"
    )
    expect(
      count(workflow, "./mill --ticker false -i ci.publish") == 1,
      "workflow must contain exactly one Sonatype publish invocation"
    )
    expect(
      count(release, "writeMillEnv") == 1,
      "Release step must convert GPG_* via ci.sonatype.writeMillEnv"
    )
    expect(
      release.contains("source \"$envfile\"") || release.contains("source '$envfile'"),
      "Release step must source the writeMillEnv file into the publish mill's process env"
    )
    expect(
      !release.contains("BEGIN PGP PRIVATE KEY"),
      "Release step must not duplicate PgpSecret conversion in bash"
    )
    // Release-tag Sonatype uploads live in release-publish.yml alone. An invocation reappearing in
    // ci.yml would publish to Maven Central on the tag push, before the draft-release promotion
    // gate a human is supposed to hold.
    List("ci.sonatype.libraries", "ci.sonatype.plugins").foreach { task =>
      expect(
        count(workflow, s"./mill --ticker false -i $task") == 0,
        s"ci.yml must not invoke $task; release-tag Sonatype uploads live in release-publish.yml"
      )
    }

  /**
   * The promotion workflow: release-publish.yml. Its `target` job carries the repository guard and resolves the tag;
   * each publish job routes on one tag namespace, depends only on `target`, honours the Maven Central stand-down
   * clauses, and invokes its Sonatype destination exactly once. The workflow-level token stays read-only — promotion
   * downloads and verifies staged assets, it never uploads any.
   */
  def assertReleasePublishPolicy(releaseWorkflow: String): Unit =
    val target = indentedBlock(releaseWorkflow, "target:", 2)
    expect(
      scalar(target, "if") == ReleasePublishTargetPredicate,
      "target predicate does not match the release-publish allowlist"
    )
    val jobs = List(
      ("publish-libraries:", ReleasePublishLibrariesPredicate, "ci.sonatype.libraries"),
      ("publish-plugins:", ReleasePublishPluginsPredicate, "ci.sonatype.plugins")
    )
    jobs.foreach { case (name, predicate, task) =>
      val job = indentedBlock(releaseWorkflow, name, 2)
      expect(
        scalar(job, "if") == predicate,
        s"$name predicate does not match the release-publish allowlist"
      )
      expect(scalar(job, "needs") == "[target]", s"$name must depend only on the target job")
      expect(
        count(job, s"./mill --ticker false -i $task") == 1,
        s"$name must invoke $task exactly once"
      )
      expect(
        count(job, "writeMillEnv") == 1,
        s"$name must convert GPG_* via ci.sonatype.writeMillEnv"
      )
    }
    expect(
      count(releaseWorkflow, "./mill --ticker false -i ci.publish") == 0,
      "release-publish.yml must not run the snapshot publish"
    )
    val permissions = indentedBlock(releaseWorkflow, "permissions:", 0)
    expect(
      permissions.linesIterator.filter(_.trim.nonEmpty).toList == List("  contents: read"),
      "release-publish.yml permissions must be exactly contents: read"
    )

  def assertPublishPluginsPolicy(releaseWorkflow: String): Unit =
    val job = indentedBlock(releaseWorkflow, "publish-plugins:", 2)
    expect(
      scalar(job, "if") == ReleasePublishPluginsPredicate,
      "publish-plugins predicate does not match the plugin-family release allowlist"
    )
    expect(scalar(job, "needs") == "[target]", "publish-plugins must depend only on the target job")
    expect(
      count(job, "./mill --ticker false -i ci.sonatype.plugins") == 1,
      "publish-plugins job must invoke ci.sonatype.plugins exactly once"
    )

  def assertCiMillTicker(workflow: String): Unit =
    assertMillInvocationsDisableTicker(Seq(workflow))

  def assertMillInvocationsDisableTicker(texts: Seq[String]): Unit =
    val invocations = texts.iterator.flatMap { text =>
      text.linesIterator
        .map(_.trim)
        .filter { line =>
          line.startsWith("./mill") ||
          line.startsWith("if ./mill") ||
          line.startsWith("run: ./mill")
        }
    }.toList
    expect(invocations.nonEmpty, "CI mill invocations must exist")
    invocations.foreach { line =>
      expect(
        line.contains("--ticker false"),
        s"CI mill invocation must disable the ticker: $line"
      )
    }

  def assertSonatypePublishPolicy(script: String): Unit =
    expect(
      script.contains("resolveSegments(Seq(\"__.publishSonatypeCentral\")"),
      "sonatype publish must resolve __.publishSonatypeCentral via Evaluator.resolveSegments"
    )
    expect(
      script.contains("exclusive = true"),
      "sonatype publish commands must be exclusive Evaluator commands"
    )
    expect(
      script.contains("ModuleRef"),
      "sonatype publish must list plugins via ModuleRef"
    )
    expect(
      script.contains("uploadJobs"),
      "sonatype publish must drive upload parallelism from the uploadJobs task"
    )
    expect(
      !script.contains("millLauncher") && !script.contains("os.proc(millLauncher"),
      "sonatype publish must not nest a second mill process"
    )
    expect(
      script.contains("writeMillEnv"),
      "sonatype publish must expose writeMillEnv so GHA can feed MILL_* into a later mill"
    )
    expect(
      script.contains("MillSonatypeEnv"),
      "sonatype publish must convert CI env through MillSonatypeEnv"
    )
    expect(
      script.contains("mill-libs-scalalib"),
      "sonatype publish must record that Mill 1.x mill-libs-scalalib is on Maven Central"
    )
    expect(
      !script.contains("mill-scalalib which isn't on Maven Central"),
      "retired mill-scalalib Central exclusion must stay gone"
    )
    expect(
      !script.contains("except the mill-morphir"),
      "Mill Morphir plugins must not be excluded from Sonatype publication by comment policy"
    )

  def assertCiLintPolicy(script: String): Unit =
    expect(
      script.contains("def lint(evaluator: Evaluator, exclude: String"),
      "ci.lint must take an Evaluator and an exclude regex"
    )
    expect(
      script.contains("resolveSegments(Seq(LintSelectors.sourcesSelector)"),
      "ci.lint must resolve sources via LintSelectors.sourcesSelector"
    )
    expect(
      script.contains("LintSelectors.excludeMatching"),
      "ci.lint must drop matching modules through LintSelectors.excludeMatching"
    )
    expect(
      script.contains("mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll"),
      "ci.lint must run checkFormatAll so modules without ScalafmtModule stay covered"
    )
    expect(
      script.contains("format.checkBuildAndElm"),
      "ci.lint must check build .mill files and Elm sources via format.checkBuildAndElm"
    )
    expect(
      script.contains("exclusive = true"),
      "ci commands that use Evaluator must be exclusive"
    )
    expect(
      !script.contains("millLauncher") && !script.contains("os.proc(millLauncher"),
      "ci.lint must not nest a second mill process"
    )

  def assertLintJobPolicy(workflow: String): Unit =
    val lint = indentedBlock(workflow, "lint:", 2)
    val step = indentedBlock(lint, "- name: Lint code", 6)
    expect(
      scalar(step, "run") == "./mill --ticker false -i ci.lint",
      "lint job must run ci.lint"
    )
    expect(!lint.contains("mise run lint"), "lint job must not go through the mise lint wrapper")
    expect(!lint.contains("mise-action"), "lint job must not install mise")

  def assertSonatypePublishConfig(yaml: String): Unit =
    val lines = yaml.linesIterator.map(_.trim).filter(_.nonEmpty).toList
    expect(
      lines.contains("uploadJobs: 1"),
      "ci/package.mill.yaml must pin uploadJobs: 1 (SLF4J / #957)"
    )
    expect(
      lines.exists(_.startsWith("libraryModulePrefix:")),
      "ci/package.mill.yaml must configure libraryModulePrefix"
    )
    expect(
      !lines.exists(_.startsWith("pluginModulePrefixes:")) && !lines.contains("pluginModulePrefixes:"),
      "plugin inventory belongs in MorphirCi.mill ModuleRefs, not YAML prefixes"
    )
    expect(
      yaml.contains(".integration."),
      "ci/package.mill.yaml must exclude .integration. modules"
    )

  def assertPublishTargetInventory(targets: Set[String]): Unit =
    val requiredPluginPrefixes = List(
      "mill-plugins.morphir.toolchain.",
      "mill-plugins.morphir.javascript.",
      "mill-plugins.morphir.elm-tooling.",
      "mill-plugins.morphir.core.",
      "mill-plugins.morphir.elm."
    )
    requiredPluginPrefixes.foreach { prefix =>
      expect(
        targets.exists(target => target.startsWith(prefix) && target.endsWith(".publishSonatypeCentral")),
        s"publish inventory must include $prefix*.publishSonatypeCentral"
      )
    }
    expect(
      !targets.exists(_.contains(".integration.")),
      "publish inventory must exclude mill-plugins.morphir.integration"
    )
    expect(
      targets.contains("morphir.model.jvm.publishSonatypeCentral") &&
        targets.contains("morphir.naming.jvm.publishSonatypeCentral"),
      "publish inventory must still cover morphir.model.jvm and morphir.naming.jvm"
    )
    val logicPublishTargets = Set(
      "morphir.knowledge.logic.jvm.publishSonatypeCentral",
      "morphir.knowledge.logic.js.publishSonatypeCentral"
    )
    expect(
      logicPublishTargets.subsetOf(targets),
      "publish inventory must include both morphir.knowledge.logic artifacts"
    )
    expect(
      !targets.exists(_.startsWith("morphir.contrib.knowledge.")),
      "publish inventory must not include the retired contrib.knowledge artifacts"
    )
    expect(targets.nonEmpty, "publish inventory must not be empty")
    expect(
      targets.forall(_.endsWith(".publishSonatypeCentral")),
      "publish inventory may only contain publishSonatypeCentral targets"
    )

  def assertSnapshotPolicy(workflow: String): Unit =
    val publish  = publishBlock(workflow)
    val snapshot = indentedBlock(publish, "- name: Configure snapshot version", 6)
    expect(
      scalar(snapshot, "if") == "github.ref == 'refs/heads/main'",
      "snapshot step must run only on main"
    )
    val lines    = snapshot.linesIterator.toList
    val runIndex = lines.indexWhere(_.trim == "run: |")
    if runIndex < 0 then fail("snapshot step must have a literal run block")
    val commands = lines.drop(runIndex + 1).takeWhile(line => line.trim.isEmpty || leadingSpaces(line) > 8)
      .map(_.drop(10))
    expect(commands == SnapshotCommands, s"unexpected snapshot commands: $commands")
    expect(
      publish.indexOf("- name: Configure snapshot version") < publish.indexOf("- name: Release"),
      "snapshot configuration must precede Release"
    )
    List("MORPHIR_PUBLISH_MODE=snapshot", "MORPHIR_PUBLISH_BRANCH=${GITHUB_REF_NAME}").foreach { assignment =>
      expect(count(workflow, assignment) == 1, s"snapshot assignment must occur exactly once: $assignment")
    }

  def assertCachePolicy(workflow: String): Unit =
    List("test-js:" -> "Cache JS build output").foreach {
      case (jobName, stepName) =>
        val job       = indentedBlock(workflow, jobName, 2)
        val step      = indentedBlock(job, s"- name: $stepName", 6)
        val condition = scalar(step, "if")
        expect(
          normalizeExpression(condition) == normalizeExpression(CachePredicate),
          s"$stepName has an unapproved condition: $condition"
        )
    }
    // The JVM build output is not cached. Resolving its path glob across `out/` took 17 to 25
    // minutes per main and tag run, and the restores listed fewer paths than the save, so no run
    // ever restored it.
    expect(!workflow.contains("Cache JVM build output"), "test-jvm must not save the JVM build output")
    expect(
      raw"-mill-jvm-(\$$\{\{|\d)".r.findFirstIn(workflow).isEmpty,
      "no step may save or restore a JVM build-output cache"
    )

  def assertReadOnlyPermissions(workflow: String): Unit =
    val permissions = indentedBlock(workflow, "permissions:", 0)
    expect(
      permissions.linesIterator.filter(_.trim.nonEmpty).toList == List("  contents: read"),
      "workflow permissions must be exactly contents: read"
    )

  def assertNoForbiddenLinkerBenchmarkTokens(workflow: String): Unit =
    val forbidden = List(
      "mise",
      "ci.test",
      "ci.publish",
      "publication",
      "uses: ./.github/workflows",
      "secrets."
    )
    forbidden.foreach(token =>
      expect(!workflow.toLowerCase.contains(token), s"workflow contains forbidden token: $token")
    )

  def assertLinkerBenchmarkWorkflowPolicy(workflow: String): Unit =
    val hostedOutput = ".dev/.sdlc/mill-jvm-worker-pool/out/hosted"
    val launcher     = "./mill --ticker false scripts/ci/LinkerBenchmark.scala"
    val presets      = List(
      "quick-smoke",
      "js-strategies",
      "wasm-strategies",
      "native-long-lived",
      "native-fresh-recycled"
    )
    val overrides = List(
      ("platforms", "string", "", "PLATFORMS", "platforms"),
      ("strategies", "string", "", "STRATEGIES", "strategies"),
      ("trials", "number", "0", "TRIALS", "trials"),
      ("order_seed", "string", "", "ORDER_SEED", "order-seed"),
      ("target_filter", "string", "", "TARGET_FILTER", "target-filter"),
      ("target_limit", "number", "0", "TARGET_LIMIT", "target-limit"),
      ("memory_gib", "number", "0", "MEMORY_GIB", "memory-gib"),
      ("reserve_gib", "number", "0", "RESERVE_GIB", "reserve-gib"),
      ("mill_jobs", "number", "0", "MILL_JOBS", "mill-jobs"),
      ("max_children", "number", "0", "MAX_CHILDREN", "max-children"),
      ("batch_size", "number", "0", "BATCH_SIZE", "batch-size"),
      ("timeout_minutes", "number", "0", "TIMEOUT_MINUTES", "timeout-minutes")
    )
    val inputNames = "preset" :: overrides.map(_._1) ::: List("continue_on_failure")

    expect(
      workflow.linesIterator.nextOption().contains(
        "# Manual dispatch appears in the Actions UI only after this file exists on the default branch."
      ),
      "workflow header must explain the default-branch dispatch requirement"
    )
    val events = indentedBlock(workflow, "on:", 0)
    expect(
      yamlKeysAtIndent(events, 2) == List("workflow_dispatch"),
      "linker benchmark workflow must be workflow_dispatch-only"
    )
    List("push", "pull_request", "schedule", "release", "workflow_call").foreach { event =>
      expect(!hasYamlKey(events, event), s"linker benchmark workflow must not use $event")
    }
    assertReadOnlyPermissions(workflow)

    val dispatch = indentedBlock(events, "workflow_dispatch:", 2)
    val inputs   = indentedBlock(dispatch, "inputs:", 4)
    expect(
      yamlKeysAtIndent(inputs, 6) == inputNames,
      s"unexpected linker benchmark inputs: ${yamlKeysAtIndent(inputs, 6)}"
    )

    def input(name: String): String                                         = indentedBlock(inputs, s"$name:", 6)
    def assertInput(name: String, inputType: String, default: String): Unit =
      val block = input(name)
      expect(unquoteYamlScalar(scalar(block, "type")) == inputType, s"$name must be a $inputType input")
      expect(unquoteYamlScalar(scalar(block, "default")) == default, s"$name has an unexpected default")

    assertInput("preset", "choice", "quick-smoke")
    expect(
      yamlSequenceEntries(indentedBlock(input("preset"), "options:", 8), 10).map(line =>
        unquoteYamlScalar(line.trim.drop(2))
      ) == presets,
      "preset choices must be the exact hosted preset tokens"
    )
    overrides.foreach { case (name, inputType, default, _, _) => assertInput(name, inputType, default) }
    assertInput("continue_on_failure", "choice", "preset")
    expect(
      yamlSequenceEntries(indentedBlock(input("continue_on_failure"), "options:", 8), 10).map(line =>
        unquoteYamlScalar(line.trim.drop(2))
      ) ==
        List("preset", "true", "false"),
      "continuation choices must be preset, true, false"
    )
    expect(
      !inputNames.contains("profile") && !inputNames.exists(_.contains("heap")),
      "hosted inputs must not expose profile or heap"
    )

    val jobs = indentedBlock(workflow, "jobs:", 0)
    expect(yamlKeysAtIndent(jobs, 2) == List("benchmark"), "workflow must contain exactly one benchmark job")
    val job = indentedBlock(jobs, "benchmark:", 2)
    expect(unquoteYamlScalar(scalar(job, "runs-on")) == "ubuntu-latest", "benchmark must run on ubuntu-latest")
    expect(unquoteYamlScalar(scalar(job, "timeout-minutes")) == "360", "benchmark job timeout must be 360 minutes")
    val stepNames = job.linesIterator.collect {
      case line if line.startsWith("      - name: ") => line.stripPrefix("      - name: ")
    }.toList
    expect(
      stepNames == List(
        "Checkout current branch",
        "Setup Scala and Java",
        "Cache scala dependencies",
        "Generate artifact name",
        "Run linker benchmark",
        "Upload benchmark results",
        "Publish benchmark summary"
      ),
      s"unexpected linker benchmark steps: $stepNames"
    )
    expect(yamlSequenceEntries(job, 6).size == 7, "benchmark job must contain exactly seven named steps")
    expect(
      job.linesIterator.count(_.trim.startsWith("run:")) == 3,
      "benchmark job must contain exactly three run steps"
    )

    def runLines(step: String, name: String): List[String] =
      val lines    = step.linesIterator.toList
      val runIndex = lines.indexWhere(_.trim == "run: |")
      if runIndex < 0 then fail(s"$name must use a literal run block")
      val runIndent = leadingSpaces(lines(runIndex))
      lines.drop(runIndex + 1).takeWhile(line => line.trim.isEmpty || leadingSpaces(line) > runIndent)
        .map(_.drop(runIndent + 2)).filter(_.nonEmpty)

    val expectedActions = List(
      "actions/checkout@v7.0.1",
      "actions/setup-java@v6",
      "coursier/cache-action@v8",
      "actions/upload-artifact@v7"
    )
    val actualActions = job.linesIterator.map(_.trim).filter(_.startsWith("uses: ")).map(_.drop(6)).toList
    expect(actualActions == expectedActions, s"unexpected linker benchmark actions: $actualActions")
    val checkout = indentedBlock(job, "- name: Checkout current branch", 6)
    expect(unquoteYamlScalar(scalar(checkout, "fetch-depth")) == "0", "checkout must fetch full history")
    val java = indentedBlock(job, "- name: Setup Scala and Java", 6)
    expect(unquoteYamlScalar(scalar(java, "distribution")) == "temurin", "setup-java must use Temurin")
    expect(
      unquoteYamlScalar(scalar(java, "java-version")).takeWhile(_ != '.') == "26",
      "setup-java must use Java 26"
    )

    val artifactName = indentedBlock(job, "- name: Generate artifact name", 6)
    expect(
      unquoteYamlScalar(scalar(artifactName, "id")) == "artifact-name",
      "artifact name step must expose its output"
    )
    val artifactEnvironment = indentedBlock(artifactName, "env:", 8)
    expect(yamlKeysAtIndent(artifactEnvironment, 10) == List("PRESET"), "artifact name env must contain only PRESET")
    expect(
      scalar(artifactEnvironment, "PRESET") == "${{ inputs.preset }}",
      "artifact name PRESET must map from inputs.preset"
    )
    expect(count(artifactName, launcher) == 1, "artifact name step must invoke the linker benchmark launcher once")
    List(
      "--artifact-name-only true",
      "--artifact-ref \"$GITHUB_REF_NAME\"",
      "--artifact-run-id \"$GITHUB_RUN_ID\"",
      "--artifact-run-attempt \"$GITHUB_RUN_ATTEMPT\"",
      "printf 'name=%s\\n' \"$name\" >> \"$GITHUB_OUTPUT\""
    ).foreach(required => expect(artifactName.contains(required), s"artifact name step is missing: $required"))
    expect(
      runLines(artifactName, "artifact name step") == List(
        s"name=\"$$($launcher \\",
        "  --preset \"$PRESET\" \\",
        "  --artifact-name-only true \\",
        "  --artifact-ref \"$GITHUB_REF_NAME\" \\",
        "  --artifact-run-id \"$GITHUB_RUN_ID\" \\",
        "  --artifact-run-attempt \"$GITHUB_RUN_ATTEMPT\")\"",
        "printf 'name=%s\\n' \"$name\" >> \"$GITHUB_OUTPUT\""
      ),
      "artifact name shell must contain only command substitution and the GITHUB_OUTPUT write"
    )

    val benchmark                    = indentedBlock(job, "- name: Run linker benchmark", 6)
    val benchmarkEnvironment         = indentedBlock(benchmark, "env:", 8)
    val expectedBenchmarkEnvironment = "PRESET" :: overrides.map(_._4) ::: List("CONTINUE_ON_FAILURE")
    expect(
      yamlKeysAtIndent(benchmarkEnvironment, 10) == expectedBenchmarkEnvironment,
      s"unexpected benchmark environment: ${yamlKeysAtIndent(benchmarkEnvironment, 10)}"
    )
    expect(
      scalar(benchmarkEnvironment, "PRESET") == "${{ inputs.preset }}",
      "benchmark PRESET must map from inputs.preset"
    )
    expect(count(benchmark, launcher) == 1, "benchmark step must invoke the linker benchmark launcher once")
    expect(benchmark.contains("--preset \"$PRESET\""), "benchmark preset must cross the quoted environment boundary")
    overrides.foreach { case (inputName, _, _, environmentName, flag) =>
      expect(
        benchmark.contains(s"$environmentName: $${{ inputs.$inputName }}"),
        s"benchmark step must map $inputName to $environmentName"
      )
      expect(benchmark.contains(s"--$flag \"$$$environmentName\""), s"benchmark step must quote $environmentName")
    }
    expect(
      benchmark.contains("CONTINUE_ON_FAILURE: ${{ inputs.continue_on_failure }}") &&
        benchmark.contains("--continue-on-failure \"$CONTINUE_ON_FAILURE\""),
      "continuation must cross the quoted environment boundary"
    )
    expect(benchmark.contains(s"--output \"$hostedOutput\""), "benchmark output must use the fixed hosted directory")
    expect(
      runLines(benchmark, "benchmark step") == List(
        s"$launcher \\",
        "  --preset \"$PRESET\" \\",
        "  --platforms \"$PLATFORMS\" \\",
        "  --strategies \"$STRATEGIES\" \\",
        "  --trials \"$TRIALS\" \\",
        "  --order-seed \"$ORDER_SEED\" \\",
        "  --target-filter \"$TARGET_FILTER\" \\",
        "  --target-limit \"$TARGET_LIMIT\" \\",
        "  --memory-gib \"$MEMORY_GIB\" \\",
        "  --reserve-gib \"$RESERVE_GIB\" \\",
        "  --mill-jobs \"$MILL_JOBS\" \\",
        "  --max-children \"$MAX_CHILDREN\" \\",
        "  --batch-size \"$BATCH_SIZE\" \\",
        "  --timeout-minutes \"$TIMEOUT_MINUTES\" \\",
        "  --continue-on-failure \"$CONTINUE_ON_FAILURE\" \\",
        s"  --output \"$hostedOutput\""
      ),
      "benchmark shell must contain only the approved direct launcher command"
    )
    expect(count(workflow, launcher) == 2, "workflow must contain exactly two approved benchmark launcher invocations")
    workflow.linesIterator.filter(_.contains("${{ inputs.")).foreach { line =>
      val trimmed = line.trim
      expect(
        trimmed.matches("[A-Z][A-Z0-9_]*: \\$\\{\\{ inputs\\.[a-z0-9_]+ \\}\\}"),
        s"GitHub input expression may appear only in a step env mapping: $trimmed"
      )
    }
    expect(
      !workflow.contains("--profile") && !workflow.toLowerCase.contains("--heap"),
      "hosted workflow must not override profile or heap"
    )

    val upload = indentedBlock(job, "- name: Upload benchmark results", 6)
    expect(scalar(upload, "if") == "${{ always() }}", "artifact upload must always run")
    expect(
      unquoteYamlScalar(scalar(upload, "name")) ==
        "${{ steps.artifact-name.outputs.name || format('linker-benchmark-{0}-{1}', github.run_id, github.run_attempt) }}",
      "artifact upload must use the generated safe name with a run-based fallback"
    )
    expect(unquoteYamlScalar(scalar(upload, "path")) == s"$hostedOutput/**", "artifact upload path must be hosted/**")
    expect(
      unquoteYamlScalar(scalar(upload, "include-hidden-files")) == "true",
      "artifact upload must include hidden files within the fixed hosted path"
    )
    expect(unquoteYamlScalar(scalar(upload, "retention-days")) == "14", "artifact retention must be 14 days")

    val summary = indentedBlock(job, "- name: Publish benchmark summary", 6)
    expect(scalar(summary, "if") == "${{ always() }}", "benchmark summary must always run")
    val summaryEnvironment = indentedBlock(summary, "env:", 8)
    expect(yamlKeysAtIndent(summaryEnvironment, 10) == List("ARTIFACT_NAME"), "summary env must contain ARTIFACT_NAME")
    expect(
      scalar(summaryEnvironment, "ARTIFACT_NAME") ==
        "${{ steps.artifact-name.outputs.name || format('linker-benchmark-{0}-{1}', github.run_id, github.run_attempt) }}",
      "summary must use the generated artifact name with its safe fallback"
    )
    expect(
      runLines(summary, "summary step") == List(
        s"summary=\"$$(find \"$hostedOutput\" -type f -name summary.md -print -quit 2>/dev/null || true)\"",
        "if [[ -n \"$summary\" ]]; then",
        "  cat \"$summary\" >> \"$GITHUB_STEP_SUMMARY\"",
        "else",
        "  printf 'Linker benchmark results: artifact %s.\\n' \"$ARTIFACT_NAME\" >> \"$GITHUB_STEP_SUMMARY\"",
        "fi"
      ),
      "summary shell must contain only nonfailing find and summary-or-artifact output glue"
    )
    assertNoForbiddenLinkerBenchmarkTokens(workflow)

  def assertMorphirCapabilityPolicy(workflow: String): Unit =
    val commands = List(
      "mill-morphir-unit:"        -> "'mill-plugins.morphir.{toolchain,javascript,elm-tooling,core,elm}.__.test'",
      "mill-morphir-integration:" -> "mill-plugins.morphir.integration.test"
    )
    commands.foreach { case (job, command) =>
      val block = indentedBlock(workflow, job, 2)
      expect(block.contains(command), s"$job must run $command")
    }
    val unit = indentedBlock(workflow, "mill-morphir-unit:", 2)
    expect(!unit.contains("mill-plugins.morphir.integration"), "unit selector must exclude integration")
    List(
      "mill-morphir-integration:" -> "[mill-morphir-unit]"
    ).foreach { case (job, dependency) =>
      expect(
        scalar(indentedBlock(workflow, job, 2), "needs") == dependency,
        s"$job must depend on $dependency"
      )
    }

  def assertJvmPlatformPolicy(workflow: String, buildMill: String, task: String): Unit =
    val testJvm = indentedBlock(workflow, "test-jvm:", 2)
    val runJvm  = indentedBlock(testJvm, "- name: Run JVM tests", 6)
    expect(
      scalar(runJvm, "run") == "./mill --ticker false -i ci.testJvm",
      "generic JVM CI must use ci.testJvm"
    )
    expect(
      task.linesIterator.map(_.trim).contains("./mill --ticker false -i ci.testJvm"),
      "test:jvm-platform must invoke ci.testJvm"
    )
    val definitions = "(?m)^\\s*def testJVMPlatform\\b".r.findAllMatchIn(buildMill).size
    expect(definitions == 1, s"build must provide exactly one testJVMPlatform alias, found $definitions")
    val aliasPattern = "(?ms)^\\s*def testJVMPlatform\\s*=\\s*alias\\(\\s*\\n(.*?)^\\s*\\)\\s*$".r
    val aliasBody    = aliasPattern.findFirstMatchIn(buildMill).map(_.group(1))
      .getOrElse(fail("testJVMPlatform must have a parseable alias body"))
    val memberPattern = "^\\s*\"([^\"]+)\"(,?)\\s*$".r
    val lines         = aliasBody.linesIterator.toList
    val members       = lines.zipWithIndex.map { case (line, index) =>
      line match
        case memberPattern(member, comma) =>
          val expectedComma = if index < lines.size - 1 then "," else ""
          expect(comma == expectedComma, "testJVMPlatform members must use canonical separators")
          member
        case _ => fail("testJVMPlatform contains a non-literal member")
    }
    expect(members == JvmPlatformAliasMembers, s"unexpected testJVMPlatform members: $members")

  def assertJvmTargetInventory(inventory: Map[String, Set[String]]): Unit =
    def targets(selector: String): Set[String] =
      inventory.getOrElse(selector, fail(s"missing resolved JVM selector: $selector"))

    def selected(selectors: List[String]): Set[String] = selectors.iterator.flatMap(targets).toSet

    def assertParity(kind: String, broadSelector: String, selectors: List[String]): Unit =
      expect(selected(selectors) == targets(broadSelector), s"JVM $kind selectors differ from current targets")

    assertParity("compile", "morphir.__.jvm.__.compile", JvmCompileSelectors)
    assertParity("publish", "morphir.__.jvm.publishArtifacts", JvmPublishSelectors)
    assertParity("test", "morphir.__.jvm.__.test", JvmTestSelectors)

  def assertMorphirCachePolicy(workflow: String): Unit =
    val pluginOutputs = List(
      "out/mill-plugins/morphir/",
      "!out/mill-plugins/morphir/**/testForked.dest/**",
      "!out/mill-plugins/morphir/**/testOnly.dest/**"
    )
    val expected = List(
      "mill-morphir-unit:" -> List(
        "Cache verified Morphir tool downloads" -> List("~/.cache/morphir-scala"),
        "Cache Mill capability outputs"         -> pluginOutputs
      ),
      "mill-morphir-integration:" -> List(
        "Cache verified Morphir tool downloads" -> List("~/.cache/morphir-scala"),
        "Cache Mill capability outputs"         -> pluginOutputs
      )
    )
    expected.foreach { case (jobName, expectedSteps) =>
      val job         = indentedBlock(workflow, jobName, 2)
      val actualSteps = actionCacheSteps(job)
      expect(actualSteps == expectedSteps, s"$jobName cache paths were $actualSteps")
    }

  private def actionCacheSteps(job: String): List[(String, List[String])] =
    val lines      = job.linesIterator.toList
    val stepStarts = lines.indices.filter(index => lines(index).startsWith("      - "))
    stepStarts.zip(stepStarts.drop(1) :+ lines.size).flatMap { case (start, end) =>
      val step        = lines.slice(start, end).mkString("\n")
      val cacheAction = step.linesIterator.map(_.trim.stripPrefix("- ")).exists(line =>
        line.startsWith("uses: actions/cache@") || line.startsWith("uses: actions/cache/restore@")
      )
      if cacheAction then
        val name = step.linesIterator.collectFirst {
          case line if line.startsWith("      - name: ") => line.stripPrefix("      - name: ")
        }.getOrElse("<unnamed cache action>")
        Some(name -> cachePaths(step))
      else None
    }.toList

  private def cachePaths(step: String): List[String] =
    scalar(step, "path") match
      case "|" =>
        indentedBlock(step, "path: |", 10).linesIterator.map(_.trim).filter(_.nonEmpty).toList
      case path => List(path)

  def assertSquireCiPolicy(workflow: String): Unit =
    val jobName           = "squire-policy:"
    val stepName          = "Test Squire and release policy"
    val changelogStepName = "Check the independent-version-stream changelogs"
    expect(
      workflow.linesIterator.count(_ == s"  $jobName") == 1,
      "workflow must contain exactly one squire-policy job"
    )
    val lint = indentedBlock(workflow, "lint:", 2)
    expect(!lint.contains("mise run test:squire"), "lint must not run Squire policy")

    val policy     = indentedBlock(workflow, jobName, 2)
    val stepStarts = yamlSequenceEntries(policy, 6)
    val headers    = policy.linesIterator.collect {
      case line if line.startsWith("      - name: ") => line.stripPrefix("      - name: ")
    }.toList
    expect(stepStarts.size == 7, s"unexpected squire-policy step count: ${stepStarts.size}")
    expect(
      headers == List(
        "Checkout current branch",
        "Setup Scala and Java",
        "Cache scala dependencies",
        "Setup mise",
        "Check the shared OpenSpec workflow",
        stepName,
        changelogStepName
      ),
      s"unexpected squire-policy steps: $headers"
    )
    val extensionStep     = indentedBlock(policy, "- name: Check the shared OpenSpec workflow", 6)
    val extensionCommands = extensionStep.linesIterator.map(_.trim).filter(line =>
      line.startsWith("mise run ") || line.startsWith("mission-control-tools ")
    ).toList
    expect(
      extensionCommands == List("mission-control-tools openspec check --bundle", "mise run openspec:validate"),
      "OpenSpec must run the bundle snapshot check, then schema and artifact validation, in order"
    )
    expect(!hasYamlKey(extensionStep, "if"), "OpenSpec assurance must not be conditionally skipped")
    expect(!hasYamlKey(extensionStep, "continue-on-error"), "OpenSpec assurance failures must block CI")

    expect(!hasYamlKey(policy, "needs"), "squire-policy must run in parallel")
    expect(!hasYamlKey(policy, "fetch-depth"), "squire-policy must use the default shallow checkout")

    val step = indentedBlock(policy, s"- name: $stepName", 6)
    expect(scalar(step, "run") == "mise run test:squire", s"$stepName must run test:squire exactly")
    expect(count(workflow, "mise run test:squire") == 1, "workflow must invoke test:squire exactly once")

    val changelogStep = indentedBlock(policy, s"- name: $changelogStepName", 6)
    expect(
      scalar(changelogStep, "run") == ".claude/skills/squire/squire changelog check",
      s"$changelogStepName must run squire changelog check exactly"
    )
    expect(
      count(workflow, ".claude/skills/squire/squire changelog check") == 1,
      "workflow must invoke squire changelog check exactly once"
    )

    val ci        = indentedBlock(workflow, "ci:", 2)
    val aggregate = inlineList(ci, "needs")
    expect(aggregate.count(_ == "squire-policy") == 1, "ci must depend on squire-policy exactly once")
    expect(scalar(ci, "if") == "${{ always() }}", "ci must always run after its dependencies")
    val aggregateStep    = indentedBlock(ci, "- name: Verify required CI jobs succeeded", 6)
    val requiredResults  = aggregate.map(job => s"test \"$${{ needs.$job.result }}\" = \"success\"")
    val resultAssertions = aggregateStep.linesIterator.map(_.trim).filter(_.startsWith("test ")).toList
    expect(
      resultAssertions == requiredResults,
      "ci must fail unless every required job result is success"
    )

  def assertElmMepReleasePolicy(workflow: String, ciScript: String): Unit =
    def namedSteps(job: String): List[String] = job.linesIterator.collect {
      case line if line.startsWith("      - name: ") => line.stripPrefix("      - name: ")
    }.toList

    def requireOrderedOnce(job: String, expected: List[String], label: String): Unit =
      val actual = namedSteps(job)
      expected.foreach(step => expect(actual.count(_ == step) == 1, s"$label must contain '$step' exactly once"))
      val positions = expected.map(actual.indexOf)
      expect(positions == positions.sorted, s"$label steps are out of order: $actual")

    val nativeJob = indentedBlock(workflow, "cli-package-native:", 2)
    requireOrderedOnce(
      nativeJob,
      List(
        "Resolve Elm MEP provider version",
        "Package and smoke-test the Elm MEP native image",
        "Archive native packages for artifact transport",
        "Upload native package transport"
      ),
      "cli-package-native"
    )
    expect(
      count(nativeJob, "ci.extensions.writeMepVersionEnv --path $env:GITHUB_ENV") == 2,
      "the single version-export step must cover exactly Windows and Unix"
    )
    expect(
      count(nativeJob, "ci.extensions.packageMepNative --platform \"${{ matrix.token }}\"") == 2,
      "the MEP package step must cover exactly Windows and Unix"
    )
    expect(
      count(nativeJob, "ci.extensions.packageNativeTransport --platform \"${{ matrix.token }}\"") == 2,
      "the transport archive step must cover exactly Windows and Unix"
    )
    expect(
      nativeJob.contains("path: ${{ env.MORPHIR_EXTENSION_TRANSPORT_DIR }}/*.tar"),
      "native artifact upload must carry only the mode-preserving transport tar"
    )
    val upload = indentedBlock(nativeJob, "- name: Upload native package transport", 6)
    expect(scalar(upload, "uses") == "actions/upload-artifact@v7", "native transport must use upload-artifact@v7")
    expect(
      scalar(upload, "path") == "${{ env.MORPHIR_EXTENSION_TRANSPORT_DIR }}/*.tar",
      "native transport upload path must contain only tar archives"
    )
    expect(
      ciScript.contains("ExtensionRelease.packageMepNative("),
      "ci.extensions.packageMepNative must copy the Elm MEP executable into durable release staging"
    )
    expect(
      !ciScript.contains("morphirArea.main") && !ciScript.contains("def packageJvm(") &&
        !ciScript.contains("def packageNative("),
      "extension release tasks must be independent of the retired Scala CLI"
    )
    expect(
      !workflow.contains("cli-package-jvm") && !workflow.contains("Download JVM CLI package"),
      "extension delivery must not schedule or depend on JVM CLI packaging"
    )
    expect(
      count(ciScript, "morphirArea.langkit.elm.compiler.mep.jvm.nativeImageSmoke()") == 1,
      "the separate MEP package command must retain exactly one native-image smoke task"
    )
    expect(
      ciScript.contains("MORPHIR_ELM_MEP_VERSION=$releaseVersion"),
      "the release-version export must inject the value read by Elm MEP BuildInfo"
    )
    expect(
      ciScript.contains("requireExecutable = true"),
      "merged pre-staging verification must require Unix Elm MEP assets to remain executable"
    )
    expect(
      !ciScript.contains("\"--pattern\""),
      "published release verification must inspect every attached asset, including retired CLI files"
    )

    List("cli-verify:", "cli-release:").foreach { jobName =>
      val job = indentedBlock(workflow, jobName, 2)
      expect(
        job.contains("MORPHIR_EXTENSION_TRANSPORT_DIR: .dev/dist/extensions/transport"),
        s"$jobName must keep transport archives outside release staging"
      )
      requireOrderedOnce(
        job,
        List(
          "Download native package transports",
          "Extract native package transports",
          if jobName == "cli-verify:" then "Verify Elm MEP packages and checksums"
          else "Verify and stage Elm MEP packages on the draft release"
        ),
        jobName.stripSuffix(":")
      )
      expect(
        count(
          job,
          "ci.extensions.extractNativeTransports --platforms \"${{ needs.cli-matrix.outputs.platforms }}\""
        ) == 1,
        s"$jobName must extract native transports exactly once before verification"
      )
      val nativeDownload = indentedBlock(job, "- name: Download native package transports", 6)
      expect(
        scalar(nativeDownload, "uses") == "actions/download-artifact@v8" &&
          scalar(nativeDownload, "pattern") == "cli-native-*" &&
          scalar(nativeDownload, "path") == "${{ env.MORPHIR_EXTENSION_TRANSPORT_DIR }}",
        s"$jobName must download native transport artifacts into the transport directory"
      )

    }

  def replaceInJob(workflow: String, jobName: String, oldValue: String, newValue: String): String =
    val job = indentedBlock(workflow, jobName, 2)
    if !job.contains(oldValue) then fail(s"mutation target not found in $jobName: $oldValue")
    replaceOnce(workflow, job, replaceOnce(job, oldValue, newValue))

  def rejects(validator: String => Unit, workflow: String): Boolean =
    scala.util.Try(validator(workflow)).isFailure

class SquireCliSpec extends Test[Any]:
  "commands" - {
    "expose the complete unified command tree" in {
      val expected = Set(
        List("ai", "env", "info"),
        List("doctor"),
        List("cellar", "get"),
        List("cellar", "search"),
        List("cellar", "deps"),
        List("reference", "repo", "add"),
        List("reference", "repo", "list"),
        List("reference", "repo", "status"),
        List("reference", "repo", "remove"),
        List("branch", "refresh"),
        List("tracking", "status"),
        List("tracking", "sync"),
        List("tracking", "doctor"),
        List("spec", "sync"),
        List("spec", "export"),
        List("schemas", "build"),
        List("schemas", "compare"),
        List("schemas", "validate"),
        List("changelog", "check"),
        List("changelog", "show"),
        List("release", "prepare"),
        List("release", "status")
      )

      assert(SquireApp.commands.flatMap(_.names).toSet == expected)
    }

    "accepts every documented positional command through RemainingArgs" in {
      val cases = List(
        SquireApp.CellarGetCmd.parser.detailedParse(Seq("org.example:demo:1", "demo.Symbol")) -> List(
          "org.example:demo:1",
          "demo.Symbol"
        ),
        SquireApp.CellarSearchCmd.parser.detailedParse(Seq("org.example:demo:1", "Symbol")) -> List(
          "org.example:demo:1",
          "Symbol"
        ),
        SquireApp.CellarDepsCmd.parser.detailedParse(Seq("org.example:demo:1")) -> List("org.example:demo:1"),
        SquireApp.ReferenceRepoAddCmd.parser.detailedParse(Seq("https://example.test/repo")) -> List(
          "https://example.test/repo"
        ),
        SquireApp.ReferenceRepoStatusCmd.parser.detailedParse(Seq("mill")) -> List("mill"),
        SquireApp.ReferenceRepoRemoveCmd.parser.detailedParse(Seq("mill")) -> List("mill")
      )
      assert(cases.forall {
        case (Right((_, remaining)), expected) => remaining.remaining == expected; case _ => false
      })
    }

    "resolves documented named and positional argument contracts strictly" in {
      def failureContains[A](outcome: Result[SquireError, A], fragments: String*): Boolean = outcome match
        case Result.Failure(error) => fragments.forall(error.getMessage.contains)
        case Result.Success(_)     => false

      val mixed = Abort.run[SquireError](
        SquireCli.resolveRequiredArguments(
          "cellar get",
          List("coordinate" -> Some("org.example:demo:1"), "symbol" -> None),
          Seq("demo.Symbol"),
          Seq.empty
        )
      )
      val duplicate = Abort.run[SquireError](
        SquireCli.resolveRequiredArguments(
          "cellar deps",
          List("coordinate" -> Some("org.example:demo:1")),
          Seq("other:coordinate:1"),
          Seq.empty
        )
      )
      val missing = Abort.run[SquireError](
        SquireCli.resolveRequiredArguments("reference repo remove", List("name" -> None), Seq.empty, Seq.empty)
      )
      val excess = Abort.run[SquireError](
        SquireCli.resolveRequiredArguments(
          "cellar deps",
          List("coordinate" -> None),
          Seq("org.example:demo:1", "extra"),
          Seq.empty
        )
      )
      val afterDoubleDash = Abort.run[SquireError](
        SquireCli.resolveRequiredArguments(
          "cellar deps",
          List("coordinate" -> None),
          Seq.empty,
          Seq("org.example:demo:1")
        )
      )
      val absentOptional = Abort.run[SquireError](
        SquireCli.resolveOptionalArgument("reference repo status", "name", None, Seq.empty, Seq.empty)
      )
      val positionalOptional = Abort.run[SquireError](
        SquireCli.resolveOptionalArgument("reference repo status", "name", None, Seq("mill"), Seq.empty)
      )
      val duplicateOptional = Abort.run[SquireError](
        SquireCli.resolveOptionalArgument("reference repo status", "name", Some("mill"), Seq("other"), Seq.empty)
      )
      for
        mixedResult              <- mixed
        duplicateResult          <- duplicate
        missingResult            <- missing
        excessResult             <- excess
        afterDoubleDashResult    <- afterDoubleDash
        absentOptionalResult     <- absentOptional
        positionalOptionalResult <- positionalOptional
        duplicateOptionalResult  <- duplicateOptional
      yield assert(
        mixedResult == Result.Success(List("org.example:demo:1", "demo.Symbol")) &&
          failureContains(duplicateResult, "cellar deps", "unexpected positional") &&
          failureContains(missingResult, "reference repo remove", "missing required argument <name>") &&
          failureContains(excessResult, "cellar deps", "unexpected positional") &&
          failureContains(afterDoubleDashResult, "cellar deps", "arguments after -- are not supported") &&
          absentOptionalResult == Result.Success(None) &&
          positionalOptionalResult == Result.Success(Some("mill")) &&
          failureContains(duplicateOptionalResult, "reference repo status", "unexpected positional")
      )
    }

    "parses repeated sparse flags and rejects every extra positional before downstream work" in {
      def failureContains[A](outcome: Result[SquireError, A], fragments: String*): Boolean = outcome match
        case Result.Failure(error) => fragments.forall(error.getMessage.contains)
        case Result.Success(_)     => false

      val Right((repeatedOptions, repeatedRemaining)) = SquireApp.ReferenceRepoAddCmd.parser.detailedParse(
        Seq(
          "https://github.com/finos/morphir",
          "--sparse",
          "docs",
          "--sparse",
          "website",
          "--sparse",
          "tests/bdd",
          "--sparse",
          "wit"
        )
      ): @unchecked
      val repeated = Abort.run[SquireError](
        SquireCli.resolveRepoAddArguments(
          repeatedOptions.urlOrPath,
          repeatedOptions.sparse,
          repeatedRemaining.remaining,
          repeatedRemaining.unparsed
        )
      )
      val Right((accidentalOptions, accidentalRemaining)) = SquireApp.ReferenceRepoAddCmd.parser.detailedParse(
        Seq("https://example.test/repo", "accidental", "--sparse", "docs")
      ): @unchecked
      var accidentalInvoked = false
      val accidental        = Abort.run[SquireError](
        SquireCli
          .resolveRepoAddArguments(
            accidentalOptions.urlOrPath,
            accidentalOptions.sparse,
            accidentalRemaining.remaining,
            accidentalRemaining.unparsed
          )
          .flatMap(result => Sync.defer { accidentalInvoked = true; result })
      )
      val Right((duplicateOptions, duplicateRemaining)) = SquireApp.ReferenceRepoAddCmd.parser.detailedParse(
        Seq("--url-or-path", "https://example.test/repo", "duplicate", "--sparse", "docs")
      ): @unchecked
      var duplicateInvoked = false
      val duplicate        = Abort.run[SquireError](
        SquireCli
          .resolveRepoAddArguments(
            duplicateOptions.urlOrPath,
            duplicateOptions.sparse,
            duplicateRemaining.remaining,
            duplicateRemaining.unparsed
          )
          .flatMap(result => Sync.defer { duplicateInvoked = true; result })
      )
      val Right((postDashOptions, postDashRemaining)) = SquireApp.ReferenceRepoAddCmd.parser.detailedParse(
        Seq("https://example.test/repo", "--sparse", "docs", "--", "after")
      ): @unchecked
      val postDash = Abort.run[SquireError](
        SquireCli.resolveRepoAddArguments(
          postDashOptions.urlOrPath,
          postDashOptions.sparse,
          postDashRemaining.remaining,
          postDashRemaining.unparsed
        )
      )
      val Right((missingOptions, missingRemaining)) = SquireApp.ReferenceRepoAddCmd.parser.detailedParse(
        Seq("--sparse", "docs")
      ): @unchecked
      val missing = Abort.run[SquireError](
        SquireCli.resolveRepoAddArguments(
          missingOptions.urlOrPath,
          missingOptions.sparse,
          missingRemaining.remaining,
          missingRemaining.unparsed
        )
      )
      val Right((positionalOptions, positionalRemaining)) = SquireApp.ReferenceRepoAddCmd.parser.detailedParse(
        Seq("https://example.test/positional", "--sparse", "docs")
      ): @unchecked
      val positional = Abort.run[SquireError](
        SquireCli.resolveRepoAddArguments(
          positionalOptions.urlOrPath,
          positionalOptions.sparse,
          positionalRemaining.remaining,
          positionalRemaining.unparsed
        )
      )
      val Right((namedOptions, namedRemaining)) = SquireApp.ReferenceRepoAddCmd.parser.detailedParse(
        Seq("--url-or-path", "https://example.test/named", "--sparse", "docs")
      ): @unchecked
      val named = Abort.run[SquireError](
        SquireCli.resolveRepoAddArguments(
          namedOptions.urlOrPath,
          namedOptions.sparse,
          namedRemaining.remaining,
          namedRemaining.unparsed
        )
      )
      for
        repeatedResult   <- repeated
        accidentalResult <- accidental
        duplicateResult  <- duplicate
        postDashResult   <- postDash
        missingResult    <- missing
        positionalResult <- positional
        namedResult      <- named
      yield assert(
        repeatedOptions.sparse == List("docs", "website", "tests/bdd", "wit") &&
          repeatedResult == Result.Success(
            "https://github.com/finos/morphir" -> List("docs", "website", "tests/bdd", "wit")
          ) &&
          failureContains(accidentalResult, "reference repo add", "unexpected positional", "accidental") &&
          !accidentalInvoked &&
          failureContains(duplicateResult, "reference repo add", "unexpected positional", "duplicate") &&
          !duplicateInvoked &&
          failureContains(postDashResult, "reference repo add", "arguments after -- are not supported", "after") &&
          failureContains(missingResult, "reference repo add", "missing required argument <url-or-path>") &&
          positionalResult == Result.Success("https://example.test/positional" -> List("docs")) &&
          namedResult == Result.Success("https://example.test/named" -> List("docs"))
      )
    }

    "renders the documented positional contracts in generated help" in {
      val helpFormat = caseapp.core.help.HelpFormat.default(ansiColors = false)
      val cellarHelp =
        SquireApp.CellarGetCmd.finalHelp.withProgName("squire cellar get").help(helpFormat, showHidden = false)
      val addHelp = SquireApp.ReferenceRepoAddCmd.finalHelp
        .withProgName("squire reference repo add")
        .help(helpFormat, showHidden = false)
      val statusHelp = SquireApp.ReferenceRepoStatusCmd.finalHelp
        .withProgName("squire reference repo status")
        .help(helpFormat, showHidden = false)
      assert(
        cellarHelp.contains("<coordinate> <symbol>") && addHelp.contains("<url-or-path>") &&
          statusHelp.contains("[<name>]") && !statusHelp.contains("[[<name>]]")
      )
    }

    "rejects invalid routed arguments before downstream work" in {
      var duplicateInvoked       = false
      var missingInvoked         = false
      var excessInvoked          = false
      var afterDoubleDashInvoked = false
      val duplicate              = Abort.run[SquireError](
        SquireCli
          .resolveRequiredArguments(
            "reference repo remove",
            List("name" -> Some("mill")),
            Seq("duplicate"),
            Seq.empty
          )
          .flatMap(_ => Sync.defer { duplicateInvoked = true })
      )
      val missing = Abort.run[SquireError](
        SquireCli
          .resolveRequiredArguments("reference repo remove", List("name" -> None), Seq.empty, Seq.empty)
          .flatMap(_ => Sync.defer { missingInvoked = true })
      )
      val excess = Abort.run[SquireError](
        SquireCli
          .resolveRequiredArguments("cellar deps", List("coordinate" -> None), Seq("one", "two"), Seq.empty)
          .flatMap(_ => Sync.defer { excessInvoked = true })
      )
      val afterDoubleDash = Abort.run[SquireError](
        SquireCli
          .resolveRequiredArguments("cellar deps", List("coordinate" -> None), Seq.empty, Seq("one"))
          .flatMap(_ => Sync.defer { afterDoubleDashInvoked = true })
      )
      for
        duplicateResult       <- duplicate
        missingResult         <- missing
        excessResult          <- excess
        afterDoubleDashResult <- afterDoubleDash
      yield assert(
        duplicateResult.isFailure && !duplicateInvoked &&
          missingResult.isFailure && !missingInvoked &&
          excessResult.isFailure && !excessInvoked &&
          afterDoubleDashResult.isFailure && !afterDoubleDashInvoked
      )
    }
  }

  "environment info" - {
    "writes JSON for the full report and nothing for check mode" in {
      for
        root <- SquireFixtures.scratch("env-cli")
        platform   = SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        fullOutput = new StringBuilder
        fullExit <- SquireCli.runEnvInfo(AiEnvInfoOpts(), root, platform, value => fullOutput.append(value))
        skippedOutput = new StringBuilder
        skippedExit <- SquireCli.runEnvInfo(
          AiEnvInfoOpts(check = Some("var-folders")),
          root,
          platform,
          value => skippedOutput.append(value)
        )
        blockedPlatform = SquireFixtures.platform(root, SquireEnv.CheckResult(Present(false), "blocked", 0.0))
        blockedOutput   = new StringBuilder
        blockedExit <- SquireCli.runEnvInfo(
          AiEnvInfoOpts(check = Some("jvm-network")),
          root,
          blockedPlatform,
          value => blockedOutput.append(value)
        )
        fractionalOutput = new StringBuilder
        fractionalExit <-
          SquireCli.runEnvInfo(AiEnvInfoOpts(timeout = 0.5), root, platform, value => fractionalOutput.append(value))
        json       = fullOutput.result()
        legacyKeys =
          List("generated_at", "claude_code", "ci", "checks", "sandboxed", "claude_settings", "recommendation")
        ordered = legacyKeys.map(json.indexOf).sliding(2).forall {
          case List(left, right) => left >= 0 && left < right; case _ => true
        }
      yield assert(
        fullExit == 0 && json.contains("\"generated_at\": \"1970-01-01T00:00:00+0000\"") &&
          json.contains("\"jvm_network\"") && !json.contains("python_network") &&
          json.contains("\"entrypoint\": null") && json.contains("\"session_id\": null") && ordered &&
          fractionalExit == 0 && fractionalOutput.nonEmpty && skippedExit == 0 && skippedOutput.isEmpty &&
          blockedExit == 1 && blockedOutput.isEmpty
      )
    }

    "resolves the repository root for routed diagnostics" in {
      for
        root <- SquireFixtures.scratch("env-cli-root")
        from = root / ".claude" / "skills" / "squire"
        _ <- Sync.defer {
          Files.createDirectories(from.toJava)
          Files.createDirectory((root / ".git").toJava)
        }
        resolved <- SquireCli.projectRoot(from)
      yield assert(resolved == root)
    }
  }

  "Cellar and reference routing" - {
    "forward process exits and command output through the unified CLI" in {
      for
        root <- SquireFixtures.scratch("cli-routing")
        settings = root / ".config" / "squire" / "settings.local.yaml"
        _ <- Sync.defer {
          Files.createDirectories(settings.parent.get.toJava)
          Files.writeString(settings.toJava, "cellar:\n  binary: /opt/cellar\n")
        }
        cellarOutput = new StringBuilder
        cellarError  = new StringBuilder
        cellarRunner = RuleRunner(request => ProcessResult(request, 7, "cellar out", "cellar err"))
        cellarExit <- SquireCli.runCellar(
          CellarAction.Deps("mill-scalalib"),
          root,
          cellarRunner,
          TestSquirePlatform(),
          value => cellarOutput.append(value),
          value => cellarError.append(value)
        )
        manifest = ReferenceManifest(List(SquireRepoFixtures.repo("mill")))
        _ <- SquireRepo.saveManifest(root, manifest)
        listOut = new StringBuilder
        listExit <- SquireCli.runReferenceList(
          ReferenceRepoListOpts(json = true),
          root,
          RuleRunner(SquireRepoFixtures.ok),
          value => listOut.append(value)
        )
      yield assert(
        cellarExit == 7 && cellarOutput.result() == "cellar out" && cellarError.result() == "cellar err" &&
          cellarRunner.requests.head.argv == Chunk("/opt/cellar", "deps", "com.lihaoyi:mill-scalalib_3:0.12.0") &&
          listExit == 0 && SquireJson.decode[ReferenceManifest](listOut.result().trim) == Result.Success(manifest)
      )
    }
  }

  "branch refresh routing" - {
    "renders text and typed JSON results" in {
      val textRunner = BranchRecordingRunner(SquireBranchFixtures.successfulProofResponses)
      val jsonRunner = BranchRecordingRunner(SquireBranchFixtures.successfulProofResponses)
      val text       = new StringBuilder
      val json       = new StringBuilder
      for
        textExit <- SquireCli.runBranch(
          BranchRefreshOpts(target = "develop", dryRun = true),
          textRunner,
          value => text.append(value)
        )
        jsonExit <- SquireCli.runBranch(
          BranchRefreshOpts(target = "develop", dryRun = true, json = true),
          jsonRunner,
          value => json.append(value)
        )
      yield assert(
        textExit == 0 && text.result() ==
          s"validated: develop ${SquireBranchFixtures.targetSha} -> ${SquireBranchFixtures.sourceSha}\n" &&
          jsonExit == 0 &&
          SquireJson.decode[RefreshResult](json.result().trim) == Result.Success(
            RefreshResult(
              "validated",
              "develop",
              SquireBranchFixtures.targetSha,
              SquireBranchFixtures.sourceSha,
              Present(42)
            )
          )
      )
    }
  }

  "tracking routing" - {
    "renders quiet checks sync and doctor through the typed tracking boundary" in {
      import SquireTrackingFixtures.*
      for
        root <- SquireFixtures.scratch("tracking-cli")
        _    <- beads(root)
        _    <- Sync.defer {
          Files.writeString((root / "AGENTS.md").toJava, SquireTracking.pointer + "\n")
          Files.writeString((root / "CLAUDE.md").toJava, SquireTracking.pointer + "\n")
        }
        statusOutput = new StringBuilder
        status <- SquireCli.runTrackingStatus(
          TrackingStatusOpts(quiet = true),
          root,
          runner(gitShared, bdVersion),
          TestSquirePlatform(Present("bd")),
          value => statusOutput.append(value)
        )
        checkOutput = new StringBuilder
        check <- SquireCli.runTrackingStatus(
          TrackingStatusOpts(check = Some("off")),
          root,
          runner(gitShared, bdVersion),
          TestSquirePlatform(Present("bd")),
          value => checkOutput.append(value)
        )
        syncOutput = new StringBuilder
        sync <- SquireCli.runTrackingSync(TrackingSyncOpts(check = true), root, value => syncOutput.append(value))
        doctorOutput = new StringBuilder
        doctor <- SquireCli.runTrackingDoctor(
          root,
          runner(gitShared, bdVersion),
          TestSquirePlatform(Present("bd")),
          value => doctorOutput.append(value)
        )
      yield assert(status == 0 && statusOutput.result() == "beads\n" && check == 1 && checkOutput.isEmpty &&
        sync == 0 && syncOutput.result().contains("OK - AGENTS.md") && doctor == 0 && doctorOutput.result().contains(
          "guidance"
        ))
    }

    "rejects invalid status checks and conflicting sync modes before output or process work" in {
      for
        root <- SquireFixtures.scratch("tracking-cli-invalid")
        statusOutput = new StringBuilder
        status <- SquireCli.runTrackingStatus(
          TrackingStatusOpts(check = Some("invalid")),
          root,
          RuleRunner(SquireTrackingFixtures.unexpected),
          TestSquirePlatform(),
          value => statusOutput.append(value)
        )
        syncOutput = new StringBuilder
        sync <- SquireCli.runTrackingSync(
          TrackingSyncOpts(check = true, diff = true),
          root,
          value => syncOutput.append(value)
        )
      yield assert(status == 2 && statusOutput.isEmpty && sync == 2 && syncOutput.isEmpty)
    }

    "preserves the legacy tracking status JSON shape byte for byte" in {
      import SquireTrackingFixtures.*
      for
        root <- SquireFixtures.scratch("tracking-cli-legacy-json")
        _    <- beads(root)
        output = new StringBuilder
        exit <- SquireCli.runTrackingStatus(
          TrackingStatusOpts(),
          root,
          runner(gitShared, bdVersion),
          TestSquirePlatform(Present("bd")),
          value => output.append(value)
        )
        expected = """{
                     |  "configured_mode": "auto",
                     |  "effective_mode": "beads",
                     |  "reason": "bd is installed and .beads/ exists (mode: auto)",
                     |  "bd": {
                     |    "installed": true,
                     |    "version": "bd 0.42.0"
                     |  },
                     |  "beads_dir_present": true,
                     |  "workspace": {
                     |    "is_worktree": false,
                     |    "local_store": false,
                     |    "status": "shared"
                     |  },
                     |  "settings_file": ".config/squire/settings.local.yaml",
                     |  "settings_file_present": false,
                     |  "guidance_doc": "docs/task-tracking.md",
                     |  "guidance_drift": []
                     |}
                     |""".stripMargin
      yield assert(exit == 0 && output.result() == expected)
    }
  }

  "doctor routing" - {
    "returns one for blockers and zero for healthy refused or absent daemon findings" in {
      val healthy = SquireDoctor.DoctorReport(
        Chunk(
          SquireDoctor.Finding("mill_daemon", "NO_DAEMON", "not running", false),
          SquireDoctor.Finding("mill_daemon", "REFUSED", "not accepting connections", false),
          SquireDoctor.Finding("project", "OK", "healthy", false)
        )
      )
      val blocked = SquireDoctor.DoctorReport(
        Chunk(SquireDoctor.Finding("var_folders", "BLOCKED", "cannot write", true))
      )
      for
        healthyExit <- SquireCli.printDoctor(healthy)
        blockedExit <- SquireCli.printDoctor(blocked)
      yield
        val healthyCode: Any = healthyExit
        val blockedCode: Any = blockedExit
        assert(healthyCode == 0 && blockedCode == 1)
    }
  }

  "command boundary" - {
    "records exact exits and renders one structured domain error" in {
      val exits                             = scala.collection.mutable.ArrayBuffer.empty[Int]
      val errors                            = new StringBuilder
      val failure: Int < Abort[SquireError] = Abort.fail(
        SquireError.Failure("repo", "manifest entry is missing", Present("choose a configured repository name"))
      )
      for
        _ <- SquireCli.runCommand(0, value => errors.append(value), code => exits.append(code))
        _ <- SquireCli.runCommand(1, value => errors.append(value), code => exits.append(code))
        _ <- SquireCli.runCommand(2, value => errors.append(value), code => exits.append(code))
        _ <- SquireCli.runCommand(7, value => errors.append(value), code => exits.append(code))
        _ <- SquireCli.runCommand(failure, value => errors.append(value), code => exits.append(code))
      yield assert(
        exits.toList == List(0, 1, 2, 7, 1) &&
          errors.result() == "ERROR [repo]: manifest entry is missing\n  choose a configured repository name\n"
      )
    }
  }

  "schema routing" - {
    "uses build and compare defaults while keeping JSON stdout clean" in {
      for
        root <- SquireFixtures.scratch("schemas-cli")
        source = root / "schemas"
        _ <- Sync.defer {
          Files.createDirectories(source.toJava)
          Files.writeString((source / "morphir-ir-v4.yaml").toJava, "$id: https://example.test/v4.yaml\ntype: object\n")
        }
        buildOutput = new StringBuilder
        buildExit <- SquireCli.runSchemasBuild(
          SchemasBuildOpts(from = Some(source.toString), json = true),
          root,
          value => buildOutput.append(value),
          _ => ()
        )
        buildReport   = SquireJson.decode[SchemaReport](buildOutput.result().trim)
        generated     = root / ".dev" / "out" / "squire" / "schemas" / "morphir-ir-v4.json"
        compareOutput = new StringBuilder
        compareExit <- SquireCli.runSchemasCompare(
          SchemasCompareOpts(from = Some(source.toString), json = true),
          root,
          value => compareOutput.append(value),
          _ => ()
        )
        compareReport = SquireJson.decode[SchemaReport](compareOutput.result().trim)
      yield assert(
        buildExit == 0 && buildReport.exists(report => report.to == generated.parent.get.toString && report.ok) &&
          Files.exists(generated.toJava) && compareExit == 1 &&
          compareReport.exists(report => report.to == source.toString && !report.ok) &&
          !buildOutput.result().contains("written\nwritten")
      )
    }
  }

class SquireMetaSpec extends Test[Any]:
  private val skillDirectory = java.nio.file.Paths.get(java.lang.System.getProperty("user.dir"))

  private def read(name: String): String =
    Files.readString(skillDirectory.resolve(name), StandardCharsets.UTF_8)

  "launchers" - {
    "run the single-file application without a Mill server or ticker" in {
      assert(read("squire").contains("--no-server --ticker false squire.scala"))
      assert(read("squire.bat").contains("--no-server --ticker false squire.scala"))
    }
    "preserve the activated validator executable across Mill's PATH rewrite" in {
      assert(read("squire").contains("command -v jsonschema") && read("squire").contains("SQUIRE_JSONSCHEMA_BIN"))
      assert(read("squire.bat").contains("where jsonschema") && read("squire.bat").contains("SQUIRE_JSONSCHEMA_BIN"))
    }

    "honours the POSIX exit-file contract and propagates Mill failure without a handoff" in Sync.defer {
      val root     = Files.createTempDirectory("squire-wrapper-contract-")
      val launcher = root.resolve("squire")
      val mill     = root.resolve("mill")
      Files.copy(skillDirectory.resolve("squire"), launcher)
      SquireLauncherFixtures.executable(launcher, Files.readString(launcher, StandardCharsets.UTF_8))
      SquireLauncherFixtures.executable(
        mill,
        """#!/bin/sh
          |if [ -n "${SQUIRE_TEST_RECORDED_EXIT:-}" ]; then
          |  printf '%s\n' "$SQUIRE_TEST_RECORDED_EXIT" > "$SQUIRE_EXIT_FILE"
          |fi
          |exit "${SQUIRE_TEST_MILL_EXIT:-0}"
          |""".stripMargin
      )
      val recorded = List(0, 1, 2, 7).map(code =>
        SquireLauncherFixtures.run(
          root,
          Seq.empty,
          Map("SQUIRE_TEST_RECORDED_EXIT" -> code.toString, "SQUIRE_TEST_MILL_EXIT" -> "0")
        ).exitCode
      )
      val millFailure = SquireLauncherFixtures.run(
        root,
        Seq.empty,
        Map("SQUIRE_TEST_MILL_EXIT" -> "23")
      )
      val help                 = SquireLauncherFixtures.run(root, Seq("cellar", "get", "--help"))
      val missingHandoff       = SquireLauncherFixtures.run(root, Seq("cellar", "get"))
      val malformedHelpHandoff = SquireLauncherFixtures.run(
        root,
        Seq("cellar", "get", "--help"),
        Map("SQUIRE_TEST_RECORDED_EXIT" -> "invalid", "SQUIRE_TEST_MILL_EXIT" -> "0")
      )
      assert(
        recorded == List(0, 1, 2, 7) && millFailure.exitCode == 23 && help.exitCode == 0 &&
          missingHandoff.exitCode == 1 && malformedHelpHandoff.exitCode == 1
      )
    }

    "preserves successful Windows help handoff semantics" in {
      val launcher = read("squire.bat")
      assert(
        launcher.contains("SQUIRE_EXIT_FILE") && launcher.contains("SQUIRE_RECORDED_EXIT") &&
          launcher.contains("SQUIRE_MILL_EXIT") && launcher.contains("del /q") &&
          launcher.contains("goto no_recorded_exit") &&
          launcher.contains("--no-server --ticker false squire.scala") &&
          launcher.contains("set \"SQUIRE_HELP_REQUESTED=0\"") &&
          launcher.contains("if \"%1\"==\"\" goto run_squire") &&
          launcher.contains("if \"%~1\"==\"--\" goto run_squire") &&
          launcher.contains("if \"%SQUIRE_MILL_EXIT%\"==\"0\" if \"%SQUIRE_HELP_REQUESTED%\"==\"1\"")
      )
    }
  }

  "Scala source policy" - {
    "marks every concrete Squire case class final" in {
      val stream     = Files.list(skillDirectory)
      val violations =
        try
          stream.iterator().asScala
            .filter(path => path.getFileName.toString.endsWith(".scala"))
            .flatMap(path =>
              Files.readAllLines(path, StandardCharsets.UTF_8).asScala.iterator.zipWithIndex.map {
                case (line, index) => (path.getFileName.toString, index + 1, line.trim)
              }
            )
            .collect {
              case (file, line, source)
                  if source.startsWith("case class ") || source.startsWith("private case class ") =>
                s"$file:$line: $source"
            }
            .toList
        finally stream.close()
      assert(violations.isEmpty, violations.mkString("non-final case classes:\n", "\n", ""))
    }
  }

  "Mill version" - {
    "keeps the approved standalone Squire pin" in
      assert(read(".mill-version").trim == "1.2.0-RC1-24-042146")
  }

  "suite registry" - {
    "lists every suite declared by this test file" in {
      val registry = read("test-resources/META-INF/services/kyo.test.Test")
        .linesIterator
        .map(_.trim)
        .filter(line => line.nonEmpty && !line.startsWith("#"))
        .toSet
      assert(
        registry == Set(
          "SquireCliSpec",
          "SquireCiPolicySpec",
          "SquireMisePolicySpec",
          "SquireMigrationSpec",
          "SquireMetaSpec",
          "SquireModelSpec",
          "SquireProcessSpec",
          "SquireEnvSpec",
          "SquireDoctorSpec",
          "SquireCellarSpec",
          "SquireRepoSpec",
          "SquireBranchSpec",
          "SquireTrackingSpec",
          "SquireSchemasSpec",
          "SquireSpecSpec",
          "SquireChangelogSpec"
        )
      )
    }
  }

// Warms the Mill launcher and the Coursier cache exactly once per JVM, before any of
// SquireCiPolicySpec's leaves that shell out to a *real* `./mill resolve` (resolveJvmTarget,
// resolvePublishTargets) get to run theirs. That closes the race in bead morphir-47j: on the first
// push to develop after the desktop merge, with both caches cold, two concurrent `mill resolve`
// invocations from those leaves failed in under a second (exit 1 "coursier cache not found", exit
// 126 "found but could not execute" — consistent with one process exec'ing the native launcher while
// another was still writing/chmod'ing it), when a genuine resolve takes about thirty seconds. Eleven
// prior pull-request runs passed only because the cache happened to already be warm.
//
// This has to be a JVM-wide singleton, not a `val` on the spec class: kyo-test constructs one fresh
// SquireCiPolicySpec instance per leaf and its LeafPool runs many of those instances concurrently
// (verified by instrumenting a per-instance version of this warm-up — several instances' own
// warm-ups still overlapped with siblings' real `mill resolve` calls, because "before this
// instance's own leaf" is not "before every leaf"). A Scala `object`'s initializer, by contrast, is
// guaranteed by the JVM class-initialization contract (JLS 12.4.2) to run at most once per
// classloader: every thread that is first to touch it runs the initializer while every other
// concurrent thread blocks until that finishes, and every later touch is a no-op. Routing the
// warm-up through this object instead pins it to the process, not the instance, which is what
// actually removes the race — and it removes it for a developer running this suite (or
// `mise run test:squire`) on a machine with no ~/.cache/coursier or Mill launcher cache yet, not
// only for a CI job with an equivalent workflow-level step.
//
// Best-effort and silent: a failure here is not reported directly. The leaves that actually need
// Mill still perform their own real resolves and, since morphir-47j also makes SquireError surface
// captured stderr, report a fully diagnosed failure if Mill genuinely cannot run — duplicating that
// here would just be noise, and this warm-up existing purely to prevent a race is not itself a
// condition worth failing the suite over.
private object SquireMillLauncherWarmup:
  private val skillDirectory = java.nio.file.Paths.get(java.lang.System.getProperty("user.dir"))
  private val repositoryRoot = Path(skillDirectory.resolve("../../..").normalize.toString)

  val exitCode: Int =
    val builder =
      new java.lang.ProcessBuilder((repositoryRoot / "mill").toString, "--ticker", "false", "resolve", "morphir")
    builder.directory(repositoryRoot.toJava.toFile)
    builder.redirectOutput(java.lang.ProcessBuilder.Redirect.DISCARD)
    builder.redirectError(java.lang.ProcessBuilder.Redirect.DISCARD)
    try builder.start().waitFor()
    catch case _: java.io.IOException => -1

class SquireCiPolicySpec extends Test[Any]:
  import SquireCiPolicy.*

  // Touching `exitCode` forces (or, for every construction after the first, simply confirms) the
  // one-time JVM-wide warm-up above to have finished before this instance's own leaf runs.
  private val _millWarmedUp: Int = SquireMillLauncherWarmup.exitCode

  private val skillDirectory = java.nio.file.Paths.get(java.lang.System.getProperty("user.dir"))
  private val repositoryRoot = Path(skillDirectory.resolve("../../..").normalize.toString)
  private val workflow       = Files.readString(
    skillDirectory.resolve("../../../.github/workflows/ci.yml").normalize,
    StandardCharsets.UTF_8
  )
  private val releasePublishWorkflow = Files.readString(
    skillDirectory.resolve("../../../.github/workflows/release-publish.yml").normalize,
    StandardCharsets.UTF_8
  )
  private val linkerBenchmarkWorkflow = Files.readString(
    skillDirectory.resolve("../../../.github/workflows/linker-benchmark.yml").normalize,
    StandardCharsets.UTF_8
  )
  private val buildMill = Files.readString(
    skillDirectory.resolve("../../../build.mill").normalize,
    StandardCharsets.UTF_8
  )
  private val jvmPlatformTask = Files.readString(
    skillDirectory.resolve("../../../.config/mise/tasks/test/jvm-platform").normalize,
    StandardCharsets.UTF_8
  )
  private val lintTask = Files.readString(
    skillDirectory.resolve("../../../.config/mise/tasks/lint").normalize,
    StandardCharsets.UTF_8
  )
  private val sonatypePublishTask = Files.readString(
    skillDirectory.resolve("../../../ci/MorphirCi.mill").normalize,
    StandardCharsets.UTF_8
  )
  private val ciPackageYaml = Files.readString(
    skillDirectory.resolve("../../../ci/package.mill.yaml").normalize,
    StandardCharsets.UTF_8
  )

  /** Each broad selector followed by the curated ones it must equal; resolved against Mill, then compared. */
  private val jvmTargetSelectors =
    ("morphir.__.jvm.__.compile" :: JvmCompileSelectors) :::
      ("morphir.__.jvm.publishArtifacts" :: JvmPublishSelectors) :::
      ("morphir.__.jvm.__.test" :: JvmTestSelectors)

  /**
   * Mill's stderr, appended to a failure message rather than left only in `detail`.
   *
   * `SquireError.Failure` passes only `message` to `RuntimeException`, so when one of these escapes a kyo-test leaf as
   * a thrown exception the reporter prints the exit code and nothing else — which is exactly what happened when a
   * cold-cache launcher race produced exit 126 and the cause had to be inferred (bead morphir-47j). Widening
   * `SquireError`'s own rendering would change every consumer of that format, including the exported spec reports the
   * CLI tests assert on, so the stderr is folded in here at the one call site that needs it.
   */
  private def withStderr(message: String, stderr: String): String =
    val trimmed = stderr.trim
    if trimmed.isEmpty then message else s"$message\n$trimmed"

  private def resolveJvmTarget(selector: String): Set[String] < (Async & Abort[SquireError]) =
    LiveProcessRunner.run(
      ProcessRequest(
        Chunk((repositoryRoot / "mill").toString, "--ticker", "false", "resolve", selector),
        Present(repositoryRoot)
      )
    ).flatMap {
      case ProcessResult(_, 0, stdout, _) =>
        stdout.linesIterator.filter(_.startsWith("morphir.")).toSet
      case result =>
        Abort.fail(
          SquireError.Failure(
            "ci-policy",
            withStderr(s"could not resolve JVM selector $selector (exit ${result.exitCode})", result.stderr),
            Present(result.stderr.trim)
          )
        )
    }

  private def resolveJvmTargetInventory: Map[String, Set[String]] < (Async & Abort[SquireError]) =
    Kyo.foreach(Chunk.from(jvmTargetSelectors)) { selector =>
      resolveJvmTarget(selector).map(selector -> _)
    }.map(_.toList.toMap)

  private def resolvePublishTargets: Set[String] < (Async & Abort[SquireError]) =
    LiveProcessRunner.run(
      ProcessRequest(
        Chunk((repositoryRoot / "mill").toString, "--ticker", "false", "resolve", "__.publishSonatypeCentral"),
        Present(repositoryRoot)
      )
    ).flatMap {
      case ProcessResult(_, 0, stdout, _) =>
        stdout.linesIterator.filter(_.endsWith(".publishSonatypeCentral")).filterNot(_.contains(' ')).toSet
      case result =>
        Abort.fail(
          SquireError.Failure(
            "ci-policy",
            withStderr(s"could not resolve __.publishSonatypeCentral (exit ${result.exitCode})", result.stderr),
            Present(result.stderr.trim)
          )
        )
    }

  /**
   * The build's own area table, read back through Mill rather than by parsing `package.mill`.
   *
   * `ci.releaseAreas` reports what each area module declares. Comparing it to the corpus closes the second drift
   * surface: `SquireChangelog.Areas` restates the same three namespaces, changelog paths and floors, and a floor raised
   * in the build alone would otherwise leave `squire changelog check` validating the old one.
   */
  private def resolveReleaseAreas: List[SquireVersionCorpus.AreaCase] < (Async & Abort[SquireError]) =
    LiveProcessRunner.run(
      ProcessRequest(
        Chunk((repositoryRoot / "mill").toString, "--ticker", "false", "show", "ci.releaseAreas"),
        Present(repositoryRoot)
      )
    ).flatMap {
      case ProcessResult(_, 0, stdout, _) =>
        // `show` prefixes its own log lines, so take the JSON document rather than the whole stream.
        val json = stdout.dropWhile(_ != '[')
        SquireJson.decode[List[SquireVersionCorpus.BuildReleaseArea]](json) match
          case Result.Success(rows) =>
            rows.map(row =>
              SquireVersionCorpus.AreaCase(row.name, row.namespace, row.changelogPath, row.startingVersion, "")
            )
          case failure =>
            Abort.fail(SquireError.Failure("ci-policy", s"ci.releaseAreas did not decode: $failure"))
      case result =>
        Abort.fail(
          SquireError.Failure(
            "ci-policy",
            withStderr(s"could not read ci.releaseAreas (exit ${result.exitCode})", result.stderr),
            Present(result.stderr.trim)
          )
        )
    }

  "release areas" - {
    "the build declares the areas the corpus declares" in {
      val corpusDirectory = repositoryRoot / ".config" / "version-rules"
      for
        corpus <- SquireVersionCorpus.load(corpusDirectory)
        actual <- resolveReleaseAreas
      yield
        val expected = corpus.areas.map(_.copy(millSelector = ""))
        assert(actual == expected)
    }
  }

  "hosted CI policy" - {
    "guards the manual hosted linker benchmark workflow" in {
      assertLinkerBenchmarkWorkflowPolicy(linkerBenchmarkWorkflow)

      val events        = indentedBlock(linkerBenchmarkWorkflow, "on:", 0)
      val dispatch      = indentedBlock(events, "workflow_dispatch:", 2)
      val inputs        = indentedBlock(dispatch, "inputs:", 4)
      val overrideNames = List(
        "platforms",
        "strategies",
        "trials",
        "order_seed",
        "target_filter",
        "target_limit",
        "memory_gib",
        "reserve_gib",
        "mill_jobs",
        "max_children",
        "batch_size",
        "timeout_minutes",
        "continue_on_failure"
      )
      val missingOverrideMutations = overrideNames.map { name =>
        val declaration = s"      $name:\n" + indentedBlock(inputs, s"$name:", 6)
        name -> replaceOnce(linkerBenchmarkWorkflow, declaration, "")
      }
      missingOverrideMutations.foreach { case (name, mutation) =>
        assert(rejects(assertLinkerBenchmarkWorkflowPolicy, mutation), s"missing $name input must be rejected")
      }

      val artifactPresetUnmapped = replaceOnce(
        linkerBenchmarkWorkflow,
        "      - name: Generate artifact name\n        id: artifact-name\n        env:\n          PRESET: ${{ inputs.preset }}\n",
        "      - name: Generate artifact name\n        id: artifact-name\n"
      )
      val benchmarkPresetUnmapped = replaceOnce(
        linkerBenchmarkWorkflow,
        "      - name: Run linker benchmark\n        env:\n          PRESET: ${{ inputs.preset }}\n",
        "      - name: Run linker benchmark\n        env:\n"
      )
      val bothPresetsUnmapped = replaceOnce(
        artifactPresetUnmapped,
        "      - name: Run linker benchmark\n        env:\n          PRESET: ${{ inputs.preset }}\n",
        "      - name: Run linker benchmark\n        env:\n"
      )
      val forbiddenCommandMutation = replaceOnce(
        linkerBenchmarkWorkflow,
        "          printf 'name=%s\\n' \"$name\" >> \"$GITHUB_OUTPUT\"",
        "          printf 'name=%s\\n' \"$name\" >> \"$GITHUB_OUTPUT\"\n          echo ci.publish"
      )
      assert(
        rejects(assertNoForbiddenLinkerBenchmarkTokens, forbiddenCommandMutation),
        "forbidden command token must be rejected independently"
      )

      val mutations = List(
        "artifact preset env mapping removed"  -> artifactPresetUnmapped,
        "benchmark preset env mapping removed" -> benchmarkPresetUnmapped,
        "both preset env mappings removed"     -> bothPresetsUnmapped,
        "manual trigger replaced"              -> replaceOnce(
          linkerBenchmarkWorkflow,
          "  workflow_dispatch:\n",
          "  push:\n"
        ),
        "pull request trigger added" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "on:\n  workflow_dispatch:\n",
          "on:\n  pull_request:\n  workflow_dispatch:\n"
        ),
        "write permission added" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "permissions:\n  contents: read",
          "permissions:\n  contents: read\n  actions: write"
        ),
        "upload always removed" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "      - name: Upload benchmark results\n        if: ${{ always() }}\n",
          "      - name: Upload benchmark results\n"
        ),
        "summary always removed" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "      - name: Publish benchmark summary\n        if: ${{ always() }}\n",
          "      - name: Publish benchmark summary\n"
        ),
        "default preset changed" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "        default: quick-smoke",
          "        default: js-strategies"
        ),
        "ticker re-enabled" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "./mill --ticker false scripts/ci/LinkerBenchmark.scala",
          "./mill scripts/ci/LinkerBenchmark.scala"
        ),
        "input interpolated directly in shell" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "--platforms \"$PLATFORMS\"",
          "--platforms \"${{ inputs.platforms }}\""
        ),
        "production command added" -> forbiddenCommandMutation,
        "summary find can fail"    -> replaceOnce(
          linkerBenchmarkWorkflow,
          " -print -quit 2>/dev/null || true)",
          " -print -quit 2>/dev/null)"
        ),
        "summary fallback removed" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "          else\n            printf 'Linker benchmark results: artifact %s.\\n' \"$ARTIFACT_NAME\" >> \"$GITHUB_STEP_SUMMARY\"\n",
          ""
        ),
        "summary fallback loses artifact reference" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "\"$ARTIFACT_NAME\" >> \"$GITHUB_STEP_SUMMARY\"",
          "\"results\" >> \"$GITHUB_STEP_SUMMARY\""
        ),
        "case validator injected" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "          ./mill --ticker false scripts/ci/LinkerBenchmark.scala \\",
          "          case \"$PRESET\" in\n            quick-smoke) ;;\n            *) exit 2 ;;\n          esac\n          ./mill --ticker false scripts/ci/LinkerBenchmark.scala \\"
        ),
        "scheduling loop injected" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "          ./mill --ticker false scripts/ci/LinkerBenchmark.scala \\",
          "          for attempt in 1; do\n            true\n          done\n          ./mill --ticker false scripts/ci/LinkerBenchmark.scala \\"
        ),
        "extra run step injected" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "      - name: Upload benchmark results\n",
          "      - name: Extra shell\n        run: echo extra\n\n      - name: Upload benchmark results\n"
        ),
        "checkout pin drifted" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "actions/checkout@v7.0.1",
          "actions/checkout@v7"
        ),
        "setup-java pin drifted" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "actions/setup-java@v6",
          "actions/setup-java@v5"
        ),
        "Coursier pin drifted" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "coursier/cache-action@v8",
          "coursier/cache-action@v7"
        ),
        "upload pin drifted" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "actions/upload-artifact@v7",
          "actions/upload-artifact@v6"
        ),
        "retention drifted" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "          retention-days: 14",
          "          retention-days: 30"
        ),
        "hidden-file upload removed" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "          include-hidden-files: true\n",
          ""
        ),
        "hidden-file upload disabled" -> replaceOnce(
          linkerBenchmarkWorkflow,
          "          include-hidden-files: true",
          "          include-hidden-files: false"
        )
      )
      mutations.foreach { case (name, mutation) =>
        assert(rejects(assertLinkerBenchmarkWorkflowPolicy, mutation), s"$name must be rejected")
      }
    }

    "targets the exact supported pull-request and push branches" in {
      assertBranchPolicy(workflow)
      assert(true)
    }

    "waits for aggregate CI and owns one guarded release path" in {
      assertPublishPolicy(workflow)
      assert(true)
    }

    "routes the plugin release to its own namespaced guard" in {
      assertPublishPluginsPolicy(releasePublishWorkflow)

      val broadenedPluginGuard = replaceOnce(
        releasePublishWorkflow,
        s"    if: startsWith(needs.target.outputs.tag, 'mill-plugins/v')$MavenCentralSwitchClauses\n    needs: [target]",
        s"    if: needs.target.outputs.tag != ''$MavenCentralSwitchClauses\n    needs: [target]"
      )
      val pluginGuardLostMavenSwitches = replaceOnce(
        releasePublishWorkflow,
        s"    if: startsWith(needs.target.outputs.tag, 'mill-plugins/v')$MavenCentralSwitchClauses\n    needs: [target]",
        "    if: startsWith(needs.target.outputs.tag, 'mill-plugins/v')\n    needs: [target]"
      )
      assert(rejects(assertPublishPluginsPolicy, broadenedPluginGuard))
      assert(rejects(assertPublishPluginsPolicy, pluginGuardLostMavenSwitches))
    }

    "keeps promotion in release-publish.yml behind the namespaced and switchable guards" in {
      assertReleasePublishPolicy(releasePublishWorkflow)

      val librariesGuardBroadened = replaceOnce(
        releasePublishWorkflow,
        s"    if: startsWith(needs.target.outputs.tag, 'v')$MavenCentralSwitchClauses\n    needs: [target]",
        s"    if: needs.target.outputs.tag != ''$MavenCentralSwitchClauses\n    needs: [target]"
      )
      val librariesGuardLostMavenSwitches = replaceOnce(
        releasePublishWorkflow,
        s"    if: startsWith(needs.target.outputs.tag, 'v')$MavenCentralSwitchClauses\n    needs: [target]",
        "    if: startsWith(needs.target.outputs.tag, 'v')\n    needs: [target]"
      )
      val targetLostRepositoryGuard = replaceOnce(
        releasePublishWorkflow,
        "    if: github.repository == 'finos/morphir-scala'\n    runs-on: ubuntu-latest",
        "    if: true\n    runs-on: ubuntu-latest"
      )
      val snapshotPublishSmuggledIn = releasePublishWorkflow +
        "\n  smuggled:\n" +
        "    runs-on: ubuntu-latest\n" +
        "    steps:\n" +
        "      - name: Snapshot publish does not belong here\n" +
        "        run: ./mill --ticker false -i ci.publish\n"
      val writeToken = replaceOnce(
        releasePublishWorkflow,
        "permissions:\n  contents: read",
        "permissions:\n  contents: write"
      )
      assert(
        List(
          librariesGuardBroadened,
          librariesGuardLostMavenSwitches,
          targetLostRepositoryGuard,
          snapshotPublishSmuggledIn,
          writeToken
        ).forall(rejects(assertReleasePublishPolicy, _))
      )
    }

    "disables the mill ticker on hosted workflow and mise mill invocations" in {
      assertMillInvocationsDisableTicker(Seq(workflow, releasePublishWorkflow, lintTask, jvmPlatformTask))
      val tickerEnabled = replaceOnce(
        workflow,
        "./mill --ticker false -i ci.publish",
        "./mill -i ci.publish"
      )
      val lintJobTickerEnabled = replaceOnce(
        workflow,
        "./mill --ticker false -i ci.lint",
        "./mill -i ci.lint"
      )
      val lintTickerEnabled = replaceOnce(
        lintTask,
        "./mill --ticker false ",
        "./mill "
      )
      val jvmTickerEnabled = replaceOnce(
        jvmPlatformTask,
        "./mill --ticker false -i ",
        "./mill -i "
      )
      assert(rejects(assertCiMillTicker, tickerEnabled))
      assert(rejects(assertCiMillTicker, lintJobTickerEnabled))
      assert(scala.util.Try(assertMillInvocationsDisableTicker(Seq(
        workflow,
        lintTickerEnabled,
        jvmPlatformTask
      ))).isFailure)
      assert(scala.util.Try(assertMillInvocationsDisableTicker(Seq(workflow, lintTask, jvmTickerEnabled))).isFailure)
    }

    "runs hosted lint through ci.lint with an exclude regex" in {
      assertLintJobPolicy(workflow)
      assertCiLintPolicy(sonatypePublishTask)
      val miseLint = replaceOnce(
        workflow,
        "run: ./mill --ticker false -i ci.lint",
        "run: mise run lint"
      )
      val withoutExclude = replaceOnce(
        sonatypePublishTask,
        "def lint(evaluator: Evaluator, exclude: String = \"\")",
        "def lint(evaluator: Evaluator)"
      )
      val withoutSources = replaceOnce(
        sonatypePublishTask,
        "resolveSegments(Seq(LintSelectors.sourcesSelector)",
        "resolveSegments(Seq(\"morphir.__.checkFormat\")"
      )
      val withoutCheckFormatAll = replaceOnce(
        sonatypePublishTask,
        "mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll",
        "morphir.__.checkFormat"
      )
      val withoutBuildAndElm = replaceOnce(
        sonatypePublishTask,
        "format.checkBuildAndElm",
        "format.checkAll"
      )
      assert(rejects(assertLintJobPolicy, miseLint))
      assert(rejects(assertCiLintPolicy, withoutExclude))
      assert(rejects(assertCiLintPolicy, withoutSources))
      assert(rejects(assertCiLintPolicy, withoutCheckFormatAll))
      assert(rejects(assertCiLintPolicy, withoutBuildAndElm))
      assert(true)
    }

    "derives the Sonatype publish set from Mill resolve, including the Mill Morphir plugin family" in {
      assertSonatypePublishPolicy(sonatypePublishTask)
      assertSonatypePublishConfig(ciPackageYaml)
      val withoutResolve = replaceOnce(
        sonatypePublishTask,
        "Seq(\"__.publishSonatypeCentral\")",
        "Seq(\"morphir.__.compile\")"
      )
      val withoutSerial = replaceOnce(ciPackageYaml, "uploadJobs: 1", "uploadJobs: 8")
      assert(rejects(assertSonatypePublishPolicy, withoutResolve))
      assert(rejects(assertSonatypePublishConfig, withoutSerial))

      for targets <- resolvePublishTargets
      yield
        assertPublishTargetInventory(targets)
        val withoutPlugin = targets.filterNot(_.startsWith("mill-plugins.morphir.toolchain."))
        assert(scala.util.Try(assertPublishTargetInventory(withoutPlugin)).isFailure)
        val withoutLogic = targets.filterNot(_.startsWith("morphir.knowledge.logic."))
        assert(scala.util.Try(assertPublishTargetInventory(withoutLogic)).isFailure)
        val withRetiredLogic = targets + "morphir.contrib.knowledge.jvm.publishSonatypeCentral"
        assert(scala.util.Try(assertPublishTargetInventory(withRetiredLogic)).isFailure)
    }

    "scopes the exact snapshot configuration to main" in {
      assertSnapshotPolicy(workflow)
      assert(true)
    }

    "retains every release ref on the JS cache save and saves no JVM build output" in {
      assertCachePolicy(workflow)
      assert(true)
    }

    "restricts workflow permissions to read-only contents" in {
      assertReadOnlyPermissions(workflow)

      val permissionMutations = List(
        replaceOnce(workflow, "permissions:\n  contents: read", "permissions:\n  contents: write"),
        replaceOnce(workflow, "permissions:\n  contents: read", "permissions:\n  contents: read\n  packages: write")
      )
      assert(permissionMutations.forall(rejects(assertReadOnlyPermissions, _)))
    }

    "preserves the Morphir CI capability graph" in {
      assertMorphirCapabilityPolicy(workflow)

      val capabilityMutations = List(
        assertMorphirCapabilityPolicy -> replaceInJob(
          workflow,
          "mill-morphir-unit:",
          "'mill-plugins.morphir.{toolchain,javascript,elm-tooling,core,elm}.__.test'",
          "'mill-plugins.morphir.__.test'"
        ),
        ((workflow: String) => assertJvmPlatformPolicy(workflow, buildMill, jvmPlatformTask)) -> replaceInJob(
          workflow,
          "test-jvm:",
          "./mill --ticker false -i ci.testJvm",
          "mise run test:jvm-platform"
        ),
        assertMorphirCapabilityPolicy -> replaceInJob(
          workflow,
          "mill-morphir-integration:",
          "needs: [mill-morphir-unit]",
          "needs: []"
        )
      )
      assert(capabilityMutations.forall((validator, mutation) => rejects(validator, mutation)))
    }

    "keeps generic JVM CI on ci.testJvm and the non-classic platform alias" in {
      assertJvmPlatformPolicy(workflow, buildMill, jvmPlatformTask)

      val missingPublishAliasMutation = replaceOnce(
        buildMill,
        s"    \"${JvmPublishSelectors.head}\",\n",
        ""
      )
      val addedAliasMutation = replaceOnce(
        buildMill,
        "    \"morphir.langkit.itest.testCached\"",
        "    \"morphir.future.jvm.test\",\n    \"morphir.langkit.itest.testCached\""
      )
      // Built from the shared list rather than spelled out. A stale literal here would abort in `replaceOnce` with
      // "mutation target not found", which reads as a broken test rather than as the widened alias it actually is.
      val reorderedAliasMutation = replaceOnce(
        buildMill,
        s"    \"${JvmCompileSelectors.head}\",\n    \"${JvmPublishSelectors.head}\",",
        s"    \"${JvmPublishSelectors.head}\",\n    \"${JvmCompileSelectors.head}\","
      )
      val taskMutation = replaceOnce(
        jvmPlatformTask,
        "./mill --ticker false -i ci.testJvm",
        "./mill --ticker false -i Alias/run testJVMPlatform"
      )
      val aliasMutations = List(
        missingPublishAliasMutation,
        addedAliasMutation,
        reorderedAliasMutation
      )
      aliasMutations.zipWithIndex.foreach { case (mutation, index) =>
        assert(
          rejects(assertJvmPlatformPolicy(workflow, _, jvmPlatformTask), mutation),
          s"JVM alias mutation $index must be rejected"
        )
      }
      assert(rejects(assertJvmPlatformPolicy(workflow, buildMill, _), taskMutation))
    }

    "resolves every current non-classic JVM compile publish and test target" in {
      val selectedCompile = JvmCompileSelectors.head
      val broadCompile    = jvmTargetSelectors.head
      for
        inventory <- resolveJvmTargetInventory
        _         <- Sync.defer(assertJvmTargetInventory(inventory))
        missingSelectedTarget = inventory.updated(
          selectedCompile,
          inventory(selectedCompile) - "morphir.model.jvm.compile"
        )
        addedCurrentTarget = inventory.updated(
          broadCompile,
          inventory(broadCompile) + "morphir.future.jvm.compile"
        )
      yield assert(
        scala.util.Try(assertJvmTargetInventory(missingSelectedTarget)).isFailure &&
          scala.util.Try(assertJvmTargetInventory(addedCurrentTarget)).isFailure
      )
    }

    "keeps Morphir caches scoped to reusable capability outputs" in {
      assertMorphirCachePolicy(workflow)

      val morphirJobs = List(
        "mill-morphir-unit:",
        "mill-morphir-integration:"
      )
      val downloadCacheMutations = morphirJobs.map { job =>
        replaceInJob(workflow, job, "path: ~/.cache/morphir-scala", "path: ~/.cache/unverified-morphir")
      }
      val approvedDownloadPathOutsideNamedStep = morphirJobs.map { job =>
        replaceInJob(
          replaceInJob(workflow, job, "path: ~/.cache/morphir-scala", "path: ~/.cache/unverified-morphir"),
          job,
          "- name: Run capability",
          "- name: Document approved cache path\n" +
            "        run: echo 'path: ~/.cache/morphir-scala'\n" +
            "      - name: Run capability"
        )
      }
      val integrationExtraOutput = replaceInJob(
        workflow,
        "mill-morphir-integration:",
        "            out/mill-plugins/morphir/\n",
        "            out/mill-plugins/morphir/\n            out/\n"
      )
      val unnamedCacheAction = replaceInJob(
        workflow,
        "mill-morphir-integration:",
        "- name: Run capability",
        "- uses: actions/cache@v6\n" +
          "        with:\n" +
          "          path: out/\n" +
          "          key: injected-unnamed-cache\n" +
          "      - name: Run capability"
      )
      val unnamedRestoreAction = replaceInJob(
        workflow,
        "mill-morphir-unit:",
        "- name: Run capability",
        "- uses: actions/cache/restore@v6\n" +
          "        with:\n" +
          "          path: out/mill-plugins/morphir/\n" +
          "          key: injected-unnamed-restore\n" +
          "      - name: Run capability"
      )
      val cacheMutations = downloadCacheMutations ++ approvedDownloadPathOutsideNamedStep ++ List(
        replaceInJob(workflow, "mill-morphir-unit:", "out/mill-plugins/morphir/", "out/"),
        replaceInJob(workflow, "mill-morphir-unit:", "!out/mill-plugins/morphir/**/testForked.dest/**\n", ""),
        replaceInJob(workflow, "mill-morphir-unit:", "!out/mill-plugins/morphir/**/testOnly.dest/**\n", ""),
        integrationExtraOutput
      )
      cacheMutations.zipWithIndex.foreach { case (mutation, index) =>
        assert(rejects(assertMorphirCachePolicy, mutation), s"cache mutation $index must be rejected")
      }
      val acceptedUnnamedMutations = List(unnamedCacheAction, unnamedRestoreAction).zipWithIndex.collect {
        case (mutation, index) if !rejects(assertMorphirCachePolicy, mutation) => index
      }
      assert(acceptedUnnamedMutations.isEmpty, s"unnamed cache mutations accepted: $acceptedUnnamedMutations")
    }

    "carries Elm MEP executables through native packaging and release verification" in {
      assertElmMepReleasePolicy(workflow, sonatypePublishTask)

      val missingVersionInjection = replaceInJob(
        replaceInJob(
          workflow,
          "cli-package-native:",
          "ci.extensions.writeMepVersionEnv --path $env:GITHUB_ENV",
          "ci.extensions.version"
        ),
        "cli-package-native:",
        "ci.extensions.writeMepVersionEnv --path $env:GITHUB_ENV",
        "ci.extensions.version"
      )
      val missingPackageCopy = replaceOnce(
        sonatypePublishTask,
        "ExtensionRelease.packageMepNative(",
        "ExtensionRelease.packageNative("
      )
      val filteredDownload = replaceOnce(
        sonatypePublishTask,
        "            \"--dir\",",
        "            \"--pattern\",\n            \"morphir-scala-elm-*\",\n            \"--dir\","
      )
      val reorderedPackages = replaceInJob(
        replaceInJob(
          replaceInJob(
            workflow,
            "cli-package-native:",
            "- name: Resolve Elm MEP provider version",
            "- name: package-order-swap"
          ),
          "cli-package-native:",
          "- name: Package and smoke-test the Elm MEP native image",
          "- name: Resolve Elm MEP provider version"
        ),
        "cli-package-native:",
        "- name: package-order-swap",
        "- name: Package and smoke-test the Elm MEP native image"
      )
      val missingExtraction = replaceInJob(
        workflow,
        "cli-verify:",
        "ci.extensions.extractNativeTransports --platforms \"${{ needs.cli-matrix.outputs.platforms }}\"",
        "ci.extensions.verify --platforms \"${{ needs.cli-matrix.outputs.platforms }}\""
      )
      val rawArtifactUpload = replaceInJob(
        workflow,
        "cli-package-native:",
        "path: ${{ env.MORPHIR_EXTENSION_TRANSPORT_DIR }}/*.tar",
        "path: ${{ env.MORPHIR_EXTENSION_RELEASE_DIR }}/*"
      )
      val duplicateVersionStep = replaceInJob(
        workflow,
        "cli-package-native:",
        "- name: Resolve Elm MEP provider version",
        "- name: Resolve Elm MEP provider version\n      - name: Resolve Elm MEP provider version"
      )

      assert(rejects(assertElmMepReleasePolicy(_, sonatypePublishTask), missingVersionInjection))
      assert(scala.util.Try(assertElmMepReleasePolicy(workflow, missingPackageCopy)).isFailure)
      assert(scala.util.Try(assertElmMepReleasePolicy(workflow, filteredDownload)).isFailure)
      List(reorderedPackages, missingExtraction, rawArtifactUpload, duplicateVersionStep).zipWithIndex.foreach {
        case (mutation, index) =>
          assert(
            rejects(assertElmMepReleasePolicy(_, sonatypePublishTask), mutation),
            s"Elm MEP release workflow mutation $index must be rejected"
          )
      }
    }

    "runs Squire policy in a dedicated parallel CI job" in {
      assertSquireCiPolicy(workflow)

      val policy     = indentedBlock(workflow, "squire-policy:", 2)
      val policyJob  = s"  squire-policy:\n$policy"
      val policyStep = s"      - name: Test Squire and release policy\n" +
        indentedBlock(policy, "- name: Test Squire and release policy", 6)
      val changelogStep = s"      - name: Check the independent-version-stream changelogs\n" +
        indentedBlock(policy, "- name: Check the independent-version-stream changelogs", 6)
      val checkout = "      - name: Checkout current branch\n" +
        "        uses: actions/checkout@v7.0.1"
      val aggregate      = inlineList(indentedBlock(workflow, "ci:", 2), "needs")
      val aggregateNeeds = s"needs: [${aggregate.mkString(", ")}]"
      val requiredResult = (job: String) => s"test \"$${{ needs.$job.result }}\" = \"success\""
      val mutations      = List(
        "missing job"          -> replaceOnce(workflow, policyJob, ""),
        "duplicate job"        -> replaceOnce(workflow, policyJob, s"$policyJob\n$policyJob"),
        "step moved into lint" -> replaceOnce(
          replaceOnce(workflow, policyStep, ""),
          "      - name: Lint code\n        run: ./mill --ticker false -i ci.lint",
          "      - name: Lint code\n        run: ./mill --ticker false -i ci.lint\n" + policyStep
        ),
        "step moved into another job" -> (replaceOnce(workflow, policyStep, "") +
          "\n  bypass-policy:\n" +
          "    runs-on: ubuntu-latest\n" +
          "    steps:\n" +
          policyStep),
        "changed command" -> {
          val changedPolicy = replaceOnce(policyJob, "mise run test:squire", "mise run lint")
          replaceOnce(workflow, policyJob, changedPolicy)
        },
        "changelog step moved into lint" -> replaceOnce(
          replaceOnce(workflow, changelogStep, ""),
          "      - name: Lint code\n        run: ./mill --ticker false -i ci.lint",
          "      - name: Lint code\n        run: ./mill --ticker false -i ci.lint\n" + changelogStep
        ),
        "OpenSpec bundle check removed" ->
          replaceOnce(workflow, "          mission-control-tools openspec check --bundle\n", ""),
        "OpenSpec validation removed" -> replaceOnce(workflow, "          mise run openspec:validate\n", ""),
        "OpenSpec check skipped"      -> replaceOnce(
          workflow,
          "      - name: Check the shared OpenSpec workflow\n",
          "      - name: Check the shared OpenSpec workflow\n        if: false\n"
        ),
        "OpenSpec failure ignored" -> replaceOnce(
          workflow,
          "      - name: Check the shared OpenSpec workflow\n",
          "      - name: Check the shared OpenSpec workflow\n        continue-on-error: true\n"
        ),
        "changelog check command changed" -> {
          val changedPolicy =
            replaceOnce(
              policyJob,
              ".claude/skills/squire/squire changelog check",
              ".claude/skills/squire/squire changelog show"
            )
          replaceOnce(workflow, policyJob, changedPolicy)
        },
        "job dependency added" -> replaceOnce(
          workflow,
          "  squire-policy:\n",
          "  squire-policy:\n    needs: [lint]\n"
        ),
        "full-history checkout added" -> replaceOnce(
          workflow,
          policyJob,
          replaceOnce(policyJob, checkout, s"$checkout\n        with:\n          fetch-depth: 0")
        ),
        "unnamed run step added" -> replaceOnce(
          workflow,
          policyJob,
          s"$policyJob\n      - run: echo bypass"
        ),
        "unnamed uses step added" -> replaceOnce(
          workflow,
          policyJob,
          s"$policyJob\n      - uses: actions/checkout@v7.0.1"
        ),
        "dash-only unnamed run step added" -> replaceOnce(
          workflow,
          policyJob,
          s"$policyJob\n      -\n        run: echo bypass"
        ),
        "quoted policy dependency added" -> replaceOnce(
          workflow,
          "  squire-policy:\n",
          "  squire-policy:\n    \"needs\": [lint]\n"
        ),
        "spaced policy dependency added" -> replaceOnce(
          workflow,
          "  squire-policy:\n",
          "  squire-policy:\n    needs : [lint]\n"
        ),
        "quoted full-history checkout added" -> replaceOnce(
          workflow,
          policyJob,
          replaceOnce(policyJob, checkout, s"$checkout\n        with:\n          \"fetch-depth\": 0")
        ),
        "spaced full-history checkout added" -> replaceOnce(
          workflow,
          policyJob,
          replaceOnce(policyJob, checkout, s"$checkout\n        with:\n          fetch-depth : 0")
        ),
        "aggregate dependency removed" -> replaceOnce(
          workflow,
          aggregateNeeds,
          s"needs: [${aggregate.filterNot(_ == "squire-policy").mkString(", ")}]"
        ),
        "aggregate dependency duplicated" -> replaceOnce(
          workflow,
          aggregateNeeds,
          s"needs: [${aggregate.flatMap(name => if name == "squire-policy" then List(name, name) else List(name)).mkString(", ")}]"
        ),
        "aggregate always removed"               -> replaceOnce(workflow, "    if: ${{ always() }}\n", ""),
        "squire-policy failure guard removed"    -> replaceOnce(workflow, requiredResult("squire-policy"), ""),
        "squire-policy result handling weakened" -> replaceOnce(
          workflow,
          requiredResult("squire-policy"),
          s"test \"$${{ needs.squire-policy.result }}\" != \"failure\""
        ),
        "squire-policy failure guard weakened with || true" -> replaceOnce(
          workflow,
          requiredResult("squire-policy"),
          s"${requiredResult("squire-policy")} || true"
        ),
        "squire-policy failure guard weakened with ; true" -> replaceOnce(
          workflow,
          requiredResult("squire-policy"),
          s"${requiredResult("squire-policy")}; true"
        )
      )
      mutations.foreach { case (name, mutation) =>
        assert(rejects(assertSquireCiPolicy, mutation), s"$name must be rejected")
      }
    }

    "keeps Mill Morphir dogfood work in ordered CI jobs" in {
      val required = List(
        "mill-morphir-unit:"        -> "mill-plugins.morphir.{toolchain,javascript,elm-tooling,core,elm}.__.test",
        "mill-morphir-integration:" -> "mill-plugins.morphir.integration.test"
      )
      val dependencies = List(
        "mill-morphir-integration:" -> "needs: [mill-morphir-unit]"
      )
      assert(
        required.forall((job, command) => indentedBlock(workflow, job, 2).contains(command)) &&
          dependencies.forall((job, dependency) => indentedBlock(workflow, job, 2).contains(dependency)) &&
          workflow.contains("path: ~/.cache/morphir-scala")
      )
    }

    "rejects representative branch snapshot and publish regressions" in {
      val pushWithExtraBranch = replaceOnce(
        workflow,
        "  push:\n    branches: [\"main\", \"0.4.x\"]",
        "  push:\n    branches: [\"main\", \"0.4.x\", \"feature\"]"
      )
      val broadSnapshotCondition = replaceOnce(
        workflow,
        "        if: github.ref == 'refs/heads/main'\n        run: |",
        "        if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/feature'\n        run: |"
      )
      val extraSnapshotWrite = replaceOnce(
        workflow,
        s"          ${SnapshotCommands(1)}",
        s"          ${SnapshotCommands(1)}\n          echo EXTRA=true >> \"$$GITHUB_ENV\""
      )
      val duplicateSnapshotAssignment = workflow +
        "\nenv:\n  DUPLICATE: \"MORPHIR_PUBLISH_MODE=snapshot\"\n"
      val duplicatePublishPath = replaceOnce(
        workflow,
        "          ./mill --ticker false -i ci.publish",
        "          ./mill --ticker false -i ci.publish\n          ./mill --ticker false -i ci.publish"
      )
      val unguardedPublishPath = replaceOnce(
        workflow,
        "          ./mill --ticker false -i ci.publish",
        "          echo release command moved"
      ) +
        "\n  unguarded-publish:\n" +
        "    runs-on: ubuntu-latest\n" +
        "    steps:\n" +
        "      - name: Bypass Release\n" +
        "        run: ./mill --ticker false -i ci.publish\n"

      val mutations = List(
        (assertBranchPolicy, pushWithExtraBranch),
        (assertSnapshotPolicy, broadSnapshotCondition),
        (assertSnapshotPolicy, extraSnapshotWrite),
        (assertSnapshotPolicy, duplicateSnapshotAssignment),
        (assertPublishPolicy, duplicatePublishPath),
        (assertPublishPolicy, unguardedPublishPath)
      )
      assert(mutations.forall((validator, mutation) => rejects(validator, mutation)))
    }

    "rejects a JVM build-output cache save or restore" in {
      val save = replaceInJob(workflow, "test-js:", "- name: Cache JS build output", "- name: Cache JVM build output")
      val restore = workflow.replace("mill-js-platform-", "mill-jvm-")
      assert(rejects(assertCachePolicy, save))
      assert(rejects(assertCachePolicy, restore))
    }

    "rejects every required cache predicate removed from the JS cache job" in {
      val predicates = List(
        "github.ref == 'refs/heads/main'",
        "github.ref == 'refs/heads/0.4.x'",
        "startsWith(github.ref, 'refs/tags/')"
      )
      val mutations =
        for
          job       <- List("test-js:")
          predicate <- predicates
        yield replaceInJob(workflow, job, predicate, "false")
      assert(mutations.forall(rejects(assertCachePolicy, _)))
    }

    "rejects disabled or broadened conditions on the JS cache job" in {
      val conditions = List(s"false && ($CachePredicate)", s"($CachePredicate) || true")
      val mutations  =
        for
          job       <- List("test-js:")
          condition <- conditions
        yield replaceInJob(workflow, job, CachePredicate, condition)
      assert(mutations.forall(rejects(assertCachePolicy, _)))
    }
  }

class SquireMisePolicySpec extends Test[Any]:
  private val skillDirectory = Path(java.lang.System.getProperty("user.dir"))
  private val repositoryRoot = skillDirectory / ".." / ".." / ".."
  private val miseExecutable = Option(java.lang.System.getenv("SQUIRE_MISE_BIN")).filter(_.nonEmpty).getOrElse("mise")
  private val setupScript    = repositoryRoot / ".config" / "mise" / "tasks" / "setup"
  private val localCiScript  = repositoryRoot / ".config" / "mise" / "tasks" / "ci" / "local"
  private val expectedDependencies = List(
    "lint",
    "test:squire",
    "test:jvm-platform",
    "test:js",
    "test:native"
  )
  private val expectedSetup   = Chunk(TaskInvocation("bun", Chunk("install", "--ignore-scripts")))
  private val expectedLocalCi = Chunk(
    TaskInvocation(
      "mill",
      Chunk("-i", "-k", "mill-plugins.morphir.{toolchain,javascript,elm-tooling,core,elm}.__.test")
    ),
    TaskInvocation("mill", Chunk("-i", "mill-plugins.morphir.integration.test"))
  )

  private def runMise(arguments: String*): ProcessResult < (Async & Abort[SquireError]) =
    LiveProcessRunner.run(ProcessRequest(Chunk(miseExecutable) ++ Chunk.from(arguments), Present(repositoryRoot)))

  private def validateTaskScript(scriptText: String): Either[SquireError, Unit] =
    val scriptLines     = scriptText.linesIterator.toList
    val executableLines = scriptLines match
      case "#!/usr/bin/env bash" :: remaining => remaining
      case _                                  => scriptLines
    val executableText    = executableLines.filterNot(_.trim.startsWith("#")).mkString("\n")
    val absoluteReference = "(?<![A-Za-z0-9_.-])(/[^\\s\\\"';&|]+|\\.\\./[^\\s\\\"';&|]+)".r
      .findFirstMatchIn(executableText).map(_.group(1))
    absoluteReference match
      case Some(reference) =>
        Left(
          SquireError.Failure(
            "mise-policy",
            s"task preflight rejected absolute program reference: $reference",
            Present(reference)
          )
        )
      case None if "(?m)(?:^|[;&|]\\s*)command\\s+-p\\b".r.findFirstIn(executableText).nonEmpty =>
        Left(
          SquireError.Failure(
            "mise-policy",
            "task preflight rejected command -p PATH escape",
            Present("command -p")
          )
        )
      case None => Right(())

  /**
   * The harness replaces PATH with its own `bin` so a task script can only reach the programs the policy approves, and
   * that shadows the real `bash` with a forwarding shim. The shim therefore has to name a real bash by absolute path,
   * and a hardcoded one does not travel: bash is `/bin/bash` on macOS and `/usr/bin/bash` on most Linux distributions.
   * Naming the wrong one fails every test in this suite with exit 126 before any policy verdict is reached, which is
   * how the policy went unverified on macOS. Resolve it from the ambient PATH, which is what a shell outside the
   * sandbox would do, and fall back to the two conventional locations.
   *
   * The outer interpreter must be this absolute path — not the just-written PATH `bash` shim. Invoking the shim via
   * `/usr/bin/env … bash` races on Linux CI with `ETXTBSY` (`Text file busy`) even after an atomic publish.
   */
  private lazy val ambientBash: String =
    val fromPath = Option(java.lang.System.getenv("PATH")).getOrElse("")
      .split(java.io.File.pathSeparator)
      .iterator
      .filter(_.nonEmpty)
      .map(entry => java.nio.file.Path.of(entry, "bash"))
    val conventional = List("/bin/bash", "/usr/bin/bash").iterator.map(java.nio.file.Path.of(_))
    (fromPath ++ conventional)
      .find(candidate => Files.isRegularFile(candidate) && Files.isExecutable(candidate))
      .map(_.toString)
      .getOrElse(throw new IllegalStateException("no executable bash found on PATH or in /bin or /usr/bin"))

  private def runTaskScript(
      script: Path,
      scriptText: String,
      expectedPrograms: Set[String],
      executionStarted: () => Unit = () => ()
  ): Chunk[TaskInvocation] < (Async & Sync & Abort[SquireError]) =
    validateTaskScript(scriptText) match
      case Left(error) => Abort.fail(error)
      case Right(_)    =>
        Scope.run {
          for
            root <- SquireFixtures.scopedScratch(s"task-${script.toJava.getFileName}")
            log  <- Sync.defer {
              val bin = root / "bin"
              val log = root / "task-invocations.bin"
              Files.createDirectories(bin.toJava)
              SquireLauncherFixtures.executable((root / "task-script").toJava, scriptText)
              SquireLauncherFixtures.executable((root / "mill").toJava, fakeTaskTool("mill"))
              List("bun", "npm", "npx").foreach(program =>
                SquireLauncherFixtures.executable((bin / program).toJava, fakeTaskTool(program))
              )
              SquireLauncherFixtures.executable((bin / "bash").toJava, s"#!/bin/sh\nexec $ambientBash \"$$@\"\n")
              log
            }
            _      <- Sync.defer(executionStarted())
            result <- LiveProcessRunner.run(
              ProcessRequest(
                Chunk(
                  "/usr/bin/env",
                  s"PATH=${(root / "bin").toString}",
                  s"SQUIRE_TASK_LOG=${log.toString}",
                  s"SQUIRE_APPROVED_TASK_PROGRAMS=${expectedPrograms.toList.sorted.mkString(",")}",
                  ambientBash,
                  (root / "task-script").toString
                ),
                Present(root)
              )
            )
            _ <- result match
              case ProcessResult(_, 0, _, _) => Sync.defer(())
              case _                         =>
                Abort.fail(
                  SquireError.Failure(
                    "mise-policy",
                    s"task script ${script.toString} failed with exit ${result.exitCode}: ${result.stderr.trim}",
                    Present(result.stderr.trim)
                  )
                )
            decoded     <- Sync.defer(decodeTaskInvocations(log))
            invocations <- decoded match
              case Right(value) => Sync.defer(value)
              case Left(error)  => Abort.fail(error)
          yield invocations
        }

  private def fakeTaskTool(program: String): String =
    s"""#!/bin/sh
set -eu
printf '%s\\0%s\\0' '$program' "$$#" >> "$$SQUIRE_TASK_LOG"
for argument in "$$@"; do
  printf '%s\\0' "$$argument" >> "$$SQUIRE_TASK_LOG"
done
printf '\\n' >> "$$SQUIRE_TASK_LOG"
case ",$${SQUIRE_APPROVED_TASK_PROGRAMS}," in
  *,$program,*) exit 0 ;;
  *) echo "unapproved program: $program" >&2; exit 97 ;;
esac
"""

  private def decodeTaskInvocations(log: Path): Either[SquireError, Chunk[TaskInvocation]] =
    try
      val bytes   = Files.readAllBytes(log.toJava)
      var offset  = 0
      val decoded = scala.collection.mutable.ArrayBuffer.empty[TaskInvocation]

      def readField(): String =
        val start = offset
        while offset < bytes.length && bytes(offset) != 0 do offset += 1
        if offset == bytes.length then throw new IllegalArgumentException("unterminated task invocation field")
        val field = new String(bytes, start, offset - start, StandardCharsets.UTF_8)
        offset += 1
        field

      while offset < bytes.length do
        val program = readField()
        val count = readField().toIntOption.getOrElse(throw new IllegalArgumentException("invalid task argument count"))
        val arguments = Chunk.from(List.fill(count)(readField()))
        if offset == bytes.length || bytes(offset) != '\n' then
          throw new IllegalArgumentException("task invocation record is missing its newline terminator")
        offset += 1
        decoded += TaskInvocation(program, arguments)
      Right(Chunk.from(decoded))
    catch
      case error: Exception =>
        Left(SquireError.Failure("mise-policy", "could not decode task invocation record", Present(error.getMessage)))

  private def failureContains[A](outcome: Result[SquireError, A], fragments: String*): Boolean =
    outcome match
      case Result.Failure(error) => fragments.forall(error.getMessage.contains)
      case Result.Success(_)     => false

  private def taskScratchRoots(scriptName: String): Set[Path] =
    val temporaryRoot = java.nio.file.Path.of(java.lang.System.getProperty("java.io.tmpdir"))
    val prefix        = s"squire-task-$scriptName-"
    val stream        = Files.list(temporaryRoot)
    try
      stream.iterator.asScala.filter(path => path.getFileName.toString.startsWith(prefix))
        .map(path => Path(path.toString)).toSet
    finally stream.close()

  private def morphirElmManifests: List[Path] = List(repositoryRoot / "package.json")

  private def packageManifestIsSafe(json: String): Boolean =
    SquireJson.decode[Structure.Value](json) match
      case Result.Success(value) =>
        !recordFieldContains(value, "devDependencies", "morphir-elm") &&
        !recordFieldContains(value, "scripts", "make")
      case Result.Failure(_) => false

  private def recordFieldContains(value: Structure.Value, field: String, key: String): Boolean =
    value match
      case Structure.Value.Record(fields) =>
        fields.collect { case (`field`, Structure.Value.Record(entries)) => entries }
          .exists(_.exists(_._1 == key))
      case _ => false

  private def stringField(json: String, name: String): String =
    val pattern = ("\\\"" + java.util.regex.Pattern.quote(name) + "\\\"\\s*:\\s*\\\"([^\\\"]*)\\\"").r
    pattern.findFirstMatchIn(json).map(_.group(1)).getOrElse(throw new AssertionError(s"missing JSON field: $name"))

  private def stringArray(json: String, name: String): List[String] =
    val pattern = ("(?s)\\\"" + java.util.regex.Pattern.quote(name) + "\\\"\\s*:\\s*\\[(.*?)\\]").r
    val body    = pattern.findFirstMatchIn(json).map(_.group(1))
      .getOrElse(throw new AssertionError(s"missing JSON array: $name"))
    "\\\"([^\\\"]+)\\\"".r.findAllMatchIn(body).map(_.group(1)).toList

  "Mise task policy" - {
    "resolves Squire metadata and the exact local CI dependency list" in {
      for
        ciInfo     <- runMise("task", "info", "ci:local", "--json")
        squireInfo <- runMise("task", "info", "test:squire", "--json")
        dryRun     <- runMise("run", "--dry-run", "ci:local")
      yield assert(
        ciInfo.exitCode == 0 && squireInfo.exitCode == 0 && dryRun.exitCode == 0 &&
          stringArray(ciInfo.stdout, "depends") == expectedDependencies &&
          stringField(ciInfo.stdout, "description") == "Run the core CI workflow locally" &&
          stringField(squireInfo.stdout, "description") == "Test Squire commands and build/release policy" &&
          (dryRun.stdout + dryRun.stderr).contains("test:squire")
      )
    }

    "runs the setup script through only its approved tool" in
      runTaskScript(setupScript, Files.readString(setupScript.toJava, StandardCharsets.UTF_8), Set("bun"))
        .map(setup => assert(setup == expectedSetup))

    "runs every local-CI Morphir capability through its dedicated Mill invocation" in
      runTaskScript(
        localCiScript,
        Files.readString(localCiScript.toJava, StandardCharsets.UTF_8),
        Set("mill")
      ).map(invocations => assert(invocations == expectedLocalCi))

    "rejects executed task mutations that add package tooling or change the approved invocation sequences" in {
      val setupText   = Files.readString(setupScript.toJava, StandardCharsets.UTF_8)
      val localCiText = Files.readString(localCiScript.toJava, StandardCharsets.UTF_8)
      val addBun      = localCiText + "\nbun install\n"
      val addNpm      = localCiText + "\nnpm install\n"
      val broadUnit   = localCiText.replace(
        "mill-plugins.morphir.{toolchain,javascript,elm-tooling,core,elm}.__.test",
        "mill-plugins.morphir.__.test"
      )
      val collapseLocal = localCiText.replace("./mill -i mill-plugins.morphir.integration.test\n", "")
      val enableHooks   = setupText.replace(" --ignore-scripts", "")
      for
        bunResult            <- Abort.run[SquireError](runTaskScript(localCiScript, addBun, Set("mill")))
        npmResult            <- Abort.run[SquireError](runTaskScript(localCiScript, addNpm, Set("mill")))
        broadUnitInvocations <- runTaskScript(localCiScript, broadUnit, Set("mill"))
        collapsedInvocations <- runTaskScript(localCiScript, collapseLocal, Set("mill"))
        hooksEnabled         <- runTaskScript(setupScript, enableHooks, Set("bun"))
      yield assert(
        failureContains(bunResult, "unapproved program", "bun") &&
          failureContains(npmResult, "unapproved program", "npm") &&
          broadUnitInvocations != expectedLocalCi &&
          collapsedInvocations != expectedLocalCi &&
          hooksEnabled != expectedSetup
      )
    }

    "cleans task harness scratch after success and Abort" in {
      val successName = "cleanup-success"
      val abortName   = "cleanup-abort"
      for
        successBefore <- Sync.defer(taskScratchRoots(successName))
        success       <- runTaskScript(
          Path(successName),
          "#!/usr/bin/env bash\n./mill clean\n",
          Set("mill")
        )
        successAfter <- Sync.defer(taskScratchRoots(successName))
        abortBefore  <- Sync.defer(taskScratchRoots(abortName))
        aborted      <- Abort.run[SquireError](
          runTaskScript(
            Path(abortName),
            "#!/usr/bin/env bash\nnpm install\n",
            Set("mill")
          )
        )
        abortAfter   = taskScratchRoots(abortName)
        successLeaks = successAfter -- successBefore
        abortLeaks   = abortAfter -- abortBefore
        _ <- Sync.defer((successLeaks ++ abortLeaks).foreach(SquireFixtures.deleteRecursively))
      yield assert(
        success == Chunk(TaskInvocation("mill", Chunk("clean"))) &&
          aborted.isFailure && successLeaks.isEmpty && abortLeaks.isEmpty
      )
    }

    "rejects absolute package-tool escapes before execution or recording" in {
      for
        escapes = Chunk(
          ("npm", "/usr/bin/npm"),
          ("bun", "/home/linuxbrew/.linuxbrew/bin/bun"),
          ("npx", "/usr/bin/npx")
        )
        variants = escapes.flatMap { case (program, absoluteProgram) =>
          Chunk(
            (program, absoluteProgram, "with-shebang", s"#!/usr/bin/env bash\n$absoluteProgram --version\n"),
            (program, absoluteProgram, "without-shebang", s"$absoluteProgram --version\n")
          )
        }
        approvedShebangAccepted = validateTaskScript("#!/usr/bin/env bash\n./mill clean\n").isRight
        proofs <- Kyo.foreach(variants) { case (program, _, variant, scriptText) =>
          val scriptName       = s"absolute-$program-$variant"
          val preflight        = validateTaskScript(scriptText)
          var executionStarted = false
          preflight match
            case Left(_) =>
              for
                before <- Sync.defer(taskScratchRoots(scriptName))
                result <- Abort.run[SquireError](
                  runTaskScript(Path(scriptName), scriptText, Set(program), () => executionStarted = true)
                )
                after <- Sync.defer(taskScratchRoots(scriptName))
              yield failureContains(result, "absolute program reference", program) &&
                !executionStarted && after == before
            case Right(_) => Sync.defer(false)
        }
      yield assert(approvedShebangAccepted && proofs.forall(identity))
    }

    "semantically rejects forbidden Morphir Elm package manifest fields" in {
      val forbiddenDevDependency = """{"devDependencies":{"morphir-elm":"1.0.0"},"scripts":{}}"""
      val forbiddenScript        = """{"devDependencies":{},"scripts":{"make":"make"}}"""
      assert(
        morphirElmManifests.forall(path =>
          packageManifestIsSafe(Files.readString(path.toJava, StandardCharsets.UTF_8))
        ) &&
          !packageManifestIsSafe(forbiddenDevDependency) &&
          !packageManifestIsSafe(forbiddenScript)
      )
    }
  }

class SquireMigrationSpec extends Test[Any]:
  private val skillDirectory      = java.nio.file.Paths.get(java.lang.System.getProperty("user.dir"))
  private val repositoryRoot      = skillDirectory.resolve("../../..").normalize
  private val maintainedConsumers = List(
    ".claude/skills/squire/SKILL.md",
    ".claude/skills/squire/README.md",
    ".claude/skills/squire/references/branch.md",
    ".claude/skills/squire/references/cellar.md",
    ".claude/skills/squire/references/doctor.md",
    ".claude/skills/squire/references/env.md",
    ".claude/skills/squire/references/repo.md",
    ".claude/skills/squire/references/spec-sync.md",
    ".claude/skills/squire/references/tracking.md",
    ".config/mise/tasks/test/squire",
    ".config/mise/tasks/schemas/build",
    ".config/mise/tasks/schemas/check",
    ".config/mise/tasks/ci/local",
    ".config/mise/config.toml",
    ".config/squire/settings.local.yaml.template",
    ".github/workflows/ci.yml",
    "scripts/lib/mill-flags.sh",
    "AGENTS.md",
    "CLAUDE.md",
    "CONTRIBUTING.md",
    "docs/task-tracking.md",
    "kb/bundles/morphir/morphir-ir-v4-draft/schema-conformance.md"
  )

  "migration completeness" - {
    "removes every Python and TypeScript implementation and test from Squire" in {
      val stream      = Files.walk(skillDirectory)
      val legacyFiles =
        try stream.iterator.asScala.filter { path =>
            val rendered = path.toString
            rendered.endsWith(".py") || rendered.endsWith(".ts")
          }.map(skillDirectory.relativize).map(_.toString).toList.sorted
        finally stream.close()
      if legacyFiles.nonEmpty then
        throw new AssertionError(s"legacy Squire files remain: ${legacyFiles.mkString(", ")}")
      assert(legacyFiles.isEmpty)
    }

    "routes every maintained consumer through the unified launcher" in {
      val maintainedConsumerText = maintainedConsumers.map { relative =>
        relative -> Files.readString(repositoryRoot.resolve(relative), StandardCharsets.UTF_8)
      }
      val stale = List(
        "python3 .claude/skills/squire",
        "${CLAUDE_PLUGIN_ROOT}/scripts/",
        "bun .claude/skills/squire",
        "schemas-to-json.ts"
      ).flatMap { token =>
        maintainedConsumerText.collect { case (path, text) if text.contains(token) => s"$path: $token" }
      }
      if stale.nonEmpty then throw new AssertionError(s"stale Squire invocations remain: ${stale.mkString(", ")}")
      assert(
        !maintainedConsumerText.exists(_._2.contains("python3 .claude/skills/squire")) &&
          !maintainedConsumerText.exists(_._2.contains("${CLAUDE_PLUGIN_ROOT}/scripts/")) &&
          !maintainedConsumerText.exists(_._2.contains("bun .claude/skills/squire")) &&
          !maintainedConsumerText.exists(_._2.contains("schemas-to-json.ts"))
      )
    }

    "keeps the documented schema parity command on the compare parser" in {
      val evidence = Files.readString(
        repositoryRoot.resolve("kb/bundles/morphir/morphir-ir-v4-draft/schema-conformance.md"),
        StandardCharsets.UTF_8
      )
      val documentedSource = ".refs/finos/morphir/website/static/schemas"
      val parsed           = Parser[SchemasCompareOpts].parse(Seq("--from", documentedSource))
      assert(
        evidence.contains(
          s"`.claude/skills/squire/squire schemas compare --from $documentedSource`"
        ) &&
          !evidence.contains("schemas build --check") &&
          parsed == Right((SchemasCompareOpts(from = Some(documentedSource)), Seq.empty)) &&
          SquireApp.SchemasCompareCmd.names.contains(List("schemas", "compare"))
      )
    }
  }

class SquireBranchSpec extends Test[Any]:
  import SquireBranchFixtures.*

  "CLI options" - {
    // `--target` has no default. It defaulted to `develop` while that branch existed; with the
    // repository on trunk-based development there is no branch it would be right to assume, so the
    // caller names one.
    "require a target rather than assuming one" in {
      val parsed = Parser[BranchRefreshOpts].parse(Seq.empty)
      assert(parsed.isLeft, s"a bare refresh must not parse, got $parsed")
    }

    // A bare positional is refused at parse time now that `--target` carries no default. It used to
    // parse — the default supplied the target and the positional fell through to a later check — so
    // the refusal moved earlier rather than away, and still happens before any process runs.
    "accept a named target and reject a bare positional target before any process" in {
      val named      = Parser[BranchRefreshOpts].parse(Seq("--target", "release-line"))
      val positional = SquireApp.BranchRefreshCmd.parser.detailedParse(Seq("release-line"))
      assert(named == Right((BranchRefreshOpts(target = "release-line"), Seq.empty)))
      assert(positional.isLeft, s"a bare positional target must not parse, got $positional")
    }
  }

  "proof pipeline" - {
    "short-circuits equal remote refs before GitHub and never pushes" in {
      val sha    = "a" * 40
      val runner = BranchRecordingRunner(
        baseResponses.updated(resolveSource, ok(resolveSource, sha + "\n"))
          .updated(resolveTarget, ok(resolveTarget, sha + "\n"))
      )
      Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = false, runner)).map { outcome =>
        assert(
          outcome == Result.Success(RefreshResult("already-current", "develop", sha, sha, Absent)) &&
            !runner.requests.exists(_.argv.headOption.contains("gh")) &&
            neverPushed(runner)
        )
      }
    }

    "fetches both exact remote-tracking refspecs in a single-branch clone" in {
      for
        root <- SquireFixtures.scratch("branch-fetch")
        origin = root / "origin.git"
        seed   = root / "seed"
        clone  = root / "clone"
        _ <- Sync.defer {
          git(root, "init", "--bare", origin.toString)
          git(root, "init", "-b", "main", seed.toString)
          git(seed, "config", "user.name", "Branch Refresh Test")
          git(seed, "config", "user.email", "branch-refresh@example.invalid")
          Files.writeString((seed / "main.txt").toJava, "main one\n")
          git(seed, "add", "main.txt")
          git(seed, "commit", "-m", "main one")
          git(seed, "remote", "add", "origin", origin.toString)
          git(seed, "push", "-u", "origin", "main")
          git(seed, "switch", "-c", "develop")
          Files.writeString((seed / "develop.txt").toJava, "develop one\n")
          git(seed, "add", "develop.txt")
          git(seed, "commit", "-m", "develop one")
          git(seed, "push", "-u", "origin", "develop")
          git(root, "clone", "--single-branch", "--branch", "main", origin.toString, clone.toString)
          git(seed, "switch", "main")
          Files.writeString((seed / "main.txt").toJava, "main two\n")
          git(seed, "add", "main.txt")
          git(seed, "commit", "-m", "main two")
          git(seed, "push", "origin", "main")
          git(seed, "switch", "develop")
          Files.writeString((seed / "develop.txt").toJava, "develop two\n")
          git(seed, "add", "develop.txt")
          git(seed, "commit", "-m", "develop two")
          git(seed, "push", "origin", "develop")
        }
        mainTip     = git(origin, "rev-parse", "refs/heads/main").trim
        developTip  = git(origin, "rev-parse", "refs/heads/develop").trim
        pullRequest =
          s"""[{"number":42,"headRefOid":"$developTip","mergeCommit":{"oid":"$mainTip"}}]"""
        runner = LocalGitBranchRunner(clone, Map(repoView -> "finos/morphir-scala\n", prList -> pullRequest))
        outcome <- Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = true, runner))
        fetchedMain    = git(clone, "rev-parse", "refs/remotes/origin/main").trim
        fetchedDevelop = git(clone, "rev-parse", "refs/remotes/origin/develop").trim
      yield assert(
        outcome == Result.Success(RefreshResult("validated", "develop", developTip, mainTip, Present(42))) &&
          fetchedMain == mainTip && fetchedDevelop == developTip &&
          runner.requests.map(_.argv).contains(fetch)
      )
    }

    "rejects each pre-proof command failure with operation and PR context before push" in {
      val cases = Chunk(
        (checkTarget, "validate target branch", "invalid ref"),
        (fetch, "fetch origin branches", "network unavailable"),
        (resolveSource, "resolve remote refs", "missing origin main")
      )
      Kyo.foreach(cases) { case (command, operation, detail) =>
        val runner = BranchRecordingRunner(baseResponses.updated(command, failed(command, detail)))
        Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = false, runner)).map { outcome =>
          assert(
            failureContains(outcome, operation, "develop", "develop-to-main PR", detail) &&
              !runner.requests.exists(_.argv.headOption.contains("gh")) && neverPushed(runner)
          )
        }
      }.unit
    }

    "validates an exact target head with a reachable merge without push" in {
      val runner = BranchRecordingRunner(successfulProofResponses)
      Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = true, runner)).map { outcome =>
        assert(
          outcome == Result.Success(
            RefreshResult("validated", "develop", targetSha, sourceSha, Present(42))
          ) && runner.requests.map(_.argv).contains(ancestor) && neverPushed(runner)
        )
      }
    }

    "rejects every GitHub proof shape and command failure before push" in {
      val otherHead = matchingPullRequest.replace(targetSha, "4" * 40)
      val cases     = Chunk(
        (proofResponses(s"[$otherHead]"), Chunk("could not find", "head SHA exactly matches")),
        (
          proofResponses(s"[${matchingPullRequest.replace(s"{\"oid\":\"$mergeSha\"}", "null")}]"),
          Chunk("merge commit")
        ),
        (proofResponses(s"[${matchingPullRequest.replace("\"number\":42,", "")}]"), Chunk("number", "integer")),
        (
          proofResponses(s"[${matchingPullRequest.replace("\"number\":42", "\"number\":\"42\"")}]"),
          Chunk("number", "integer")
        ),
        (proofResponses("not json"), Chunk("JSON")),
        (proofResponses("{\"number\":42}"), Chunk("array")),
        (
          proofResponses("[]").updated(repoView, failed(repoView, "gh unavailable")),
          Chunk("identify repository", "gh unavailable")
        ),
        (
          proofResponses("[]").updated(prList, failed(prList, "PR listing unavailable")),
          Chunk("list merged PRs", "PR listing unavailable")
        )
      )
      Kyo.foreach(cases) { case (responses, fragments) =>
        val runner = BranchRecordingRunner(responses)
        Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = true, runner)).map { outcome =>
          assert(
            failureContains(outcome, (Chunk("develop", "develop-to-main PR") ++ fragments)*) &&
              neverPushed(runner)
          )
        }
      }.unit
    }

    "rejects unreachable merge ancestry before push" in {
      val runner = BranchRecordingRunner(
        proofResponses(s"[$matchingPullRequest]").updated(ancestor, failed(ancestor, "not an ancestor"))
      )
      Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = true, runner)).map { outcome =>
        assert(
          failureContains(
            outcome,
            "verify merge ancestry",
            "develop",
            "develop-to-main PR",
            "origin/main",
            "not an ancestor"
          ) && neverPushed(runner)
        )
      }
    }

    "rejects main as a target without invoking a process" in {
      val runner = BranchRecordingRunner(Map.empty)
      Abort.run[SquireError](SquireBranch.refresh("main", dryRun = false, runner)).map { outcome =>
        assert(failureContains(outcome, "target branch must not be main") && runner.requests.isEmpty)
      }
    }
  }

  "leased update boundary" - {
    "uses the exact force-with-lease request only after every proof" in {
      val runner = BranchRecordingRunner(successfulProofResponses.updated(push, ok(push)))
      Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = false, runner)).map { outcome =>
        assert(
          outcome == Result.Success(RefreshResult("updated", "develop", targetSha, sourceSha, Present(42))) &&
            runner.requests.map(_.argv) == Chunk(
              checkTarget,
              fetch,
              resolveSource,
              resolveTarget,
              repoView,
              prList,
              ancestor,
              push
            )
        )
      }
    }

    "surfaces a lease rejection without retry or unleased force" in {
      val runner = BranchRecordingRunner(successfulProofResponses.updated(push, failed(push, "lease rejected")))
      Abort.run[SquireError](SquireBranch.refresh("develop", dryRun = false, runner)).map { outcome =>
        val pushes = runner.requests.map(_.argv).filter(_.take(2) == Chunk("git", "push"))
        assert(
          failureContains(outcome, "push leased update", "develop", "develop-to-main PR", "lease rejected") &&
            pushes == Chunk(push)
        )
      }
    }
  }

class SquireModelSpec extends Test[Any]:
  "JSON" - {
    "pretty JSON preserves field order and escapes control characters" in {
      val value = Structure.Value.Record(
        Chunk(
          "z" -> Structure.Value.Str("line\n\"quoted\""),
          "a" -> Structure.Value.Sequence(
            Chunk(Structure.Value.Integer(1), Structure.Value.Bool(true))
          )
        )
      )
      assert(
        SquireJson.pretty(value) == "{\n  \"z\": \"line\\n\\\"quoted\\\"\",\n  \"a\": [\n    1,\n    true\n  ]\n}\n"
      )
    }

    "rejects Bytes as non-deterministic JSON" in
      assert(isRejectedAsNonDeterministicJson(Structure.Value.Bytes(Span.from(Array[Byte](1, 2, 3)))))

    "rejects Instant as non-deterministic JSON" in
      assert(isRejectedAsNonDeterministicJson(Structure.Value.Instant(java.time.Instant.EPOCH)))

    "rejects Duration as non-deterministic JSON" in
      assert(isRejectedAsNonDeterministicJson(Structure.Value.Duration(java.time.Duration.ZERO)))
  }

  "paths" - {
    "resolveUnder rejects a sibling prefix" in {
      val base    = Path("/tmp/squire-path-test/.refs")
      val sibling = Path("/tmp/squire-path-test/.refs-escaped/repo")
      assert(SquirePaths.resolveUnder(sibling, base).isFailure)
    }

    "resolveUnder rejects an in-base symlink that escapes" in {
      for
        root <- SquireFixtures.scratch("path")
        base    = root / ".refs"
        outside = root / "outside"
        link    = base / "link-outside"
        _ <- Sync.defer {
          Files.createDirectories(base.toJava)
          Files.createDirectories(outside.toJava)
          Files.createSymbolicLink(link.toJava, outside.toJava)
        }
        result = SquirePaths.resolveUnder(link / "repo", base)
      yield assert(result.isFailure)
    }
  }

  private def isRejectedAsNonDeterministicJson(value: Structure.Value): Boolean =
    try
      SquireJson.pretty(value)
      false
    catch
      case SquireError.Failure(area, message, _) =>
        area == "json" && message == "value cannot be represented as deterministic JSON"

class SquireSchemasSpec extends Test[Any]:
  private val skillDirectory = java.nio.file.Paths.get(java.lang.System.getProperty("user.dir"))

  "conversion" - {
    "preserves YAML scalar types, order, Unicode, escapes, mappings, and sequences while rewriting only top-level id" in {
      val yaml =
        """$id: https://example.test/schema.yaml
          |nested:
          |  $id: keep.yaml
          |nothing: null
          |yes: true
          |no: false
          |integer: 42
          |negative: -7
          |decimal: 1.25
          |exponential: 1.2e3
          |unicode: "λ café"
          |escaped: "line\n\"quoted\"\\slash"
          |mapping:
          |  z: first
          |  a: second
          |sequence:
          |  - null
          |  - false
          |  - 3
          |  - 2.5
          |""".stripMargin
      val expected =
        """{
          |  "$id": "https://example.test/schema.json",
          |  "nested": {
          |    "$id": "keep.yaml"
          |  },
          |  "nothing": null,
          |  "yes": true,
          |  "no": false,
          |  "integer": 42,
          |  "negative": -7,
          |  "decimal": 1.25,
          |  "exponential": 1200,
          |  "unicode": "λ café",
          |  "escaped": "line\n\"quoted\"\\slash",
          |  "mapping": {
          |    "z": "first",
          |    "a": "second"
          |  },
          |  "sequence": [
          |    null,
          |    false,
          |    3,
          |    2.5
          |  ]
          |}
          |""".stripMargin
      assert(SquireSchemas.convert(yaml) == Result.Success(expected))
    }

    "matches Bun JSON.stringify bytes for every legacy numeric boundary" in {
      val yaml =
        """$id: https://example.test/numeric.yaml
          |ordinaryInt: 42
          |ordinaryNegative: -7
          |ordinaryDecimal: 1.25
          |integralDecimal: 1.0
          |ordinaryExponent: 1.2e3
          |lowerFixed: 1e-6
          |lowerScientific: 1e-7
          |upperFixed: 1e20
          |upperScientific: 1E21
          |explicitPositiveExponent: 1E+7
          |upperNegativeExponent: 1E-7
          |positiveZero: 0
          |negativeZero: -0
          |negativeDecimalZero: -0.0
          |negativeExponentZero: -0e5
          |unsafeInteger: 9007199254740993
          |beyondLong: 9223372036854775808
          |notANumber: .nan
          |positiveInfinity: .inf
          |negativeInfinity: -.inf
          |""".stripMargin
      val expected =
        """{
          |  "$id": "https://example.test/numeric.json",
          |  "ordinaryInt": 42,
          |  "ordinaryNegative": -7,
          |  "ordinaryDecimal": 1.25,
          |  "integralDecimal": 1,
          |  "ordinaryExponent": 1200,
          |  "lowerFixed": 0.000001,
          |  "lowerScientific": 1e-7,
          |  "upperFixed": 100000000000000000000,
          |  "upperScientific": 1e+21,
          |  "explicitPositiveExponent": 10000000,
          |  "upperNegativeExponent": 1e-7,
          |  "positiveZero": 0,
          |  "negativeZero": 0,
          |  "negativeDecimalZero": 0,
          |  "negativeExponentZero": 0,
          |  "unsafeInteger": 9007199254740992,
          |  "beyondLong": 9223372036854776000,
          |  "notANumber": null,
          |  "positiveInfinity": null,
          |  "negativeInfinity": null
          |}
          |""".stripMargin

      assert(scala.util.Try(SquireSchemas.convert(yaml)).toOption == Some(Result.Success(expected)))
    }

    "returns malformed conversions as typed failures" in {
      val result = scala.util.Try(SquireSchemas.convert("value: !!int nope\n")).toOption
      assert(result.exists(_.isFailure))
    }

    "keeps overflow recovery compatible with YAML anchors and aliases" in {
      val yaml     = "value: &big 9223372036854775808\nalias: *big\n"
      val expected =
        """{
          |  "value": 9223372036854776000,
          |  "alias": 9223372036854776000
          |}
          |""".stripMargin
      assert(SquireSchemas.convert(yaml) == Result.Success(expected))
    }

    "matches Bun bytes for non-finite numeric anchors and every alias shape" in {
      val huge = "9" * 400
      val yaml =
        s"""positive: &positive $huge
           |positiveAlias: *positive
           |nested:
           |  positiveAlias: *positive
           |sequence:
           |  - *positive
           |  - &negative -$huge
           |  - *negative
           |negativeAlias: *negative
           |negativeNested:
           |  value: *negative
           |""".stripMargin
      val expected =
        """{
          |  "positive": null,
          |  "positiveAlias": null,
          |  "nested": {
          |    "positiveAlias": null
          |  },
          |  "sequence": [
          |    null,
          |    null,
          |    null
          |  ],
          |  "negativeAlias": null,
          |  "negativeNested": {
          |    "value": null
          |  }
          |}
          |""".stripMargin

      assert(scala.util.Try(SquireSchemas.convert(yaml)).toOption == Some(Result.Success(expected)))
    }

    "matches the committed v4 golden bytes" in {
      val yaml = Files.readString(skillDirectory.resolve("test-resources/schemas/morphir-ir-v4.yaml"))
      val json = Files.readString(skillDirectory.resolve("test-resources/schemas/morphir-ir-v4.json"))
      assert(SquireSchemas.convert(yaml) == Result.Success(json))
    }

    "matches every available reference-checkout IR golden" in {
      val root      = skillDirectory.resolve("../../..").normalize
      val mirror    = root.resolve("kb/bundles/morphir/morphir-upstream/sources/website/static/schemas")
      val reference = root.resolve(".refs/finos/morphir/website/static/schemas")
      if !Files.isDirectory(reference) then assert(true)
      else
        val stream = Files.list(mirror)
        try
          val names = stream.iterator.asScala
            .map(_.getFileName.toString)
            .filter(name => name.startsWith("morphir-ir-") && name.endsWith(".yaml"))
            .toList
            .sorted
          assert(names.forall { name =>
            val target = reference.resolve(name.stripSuffix(".yaml") + ".json")
            Files.isRegularFile(target) &&
            SquireSchemas.convert(Files.readString(mirror.resolve(name))) == Result.Success(Files.readString(target))
          })
        finally stream.close()
    }
  }

  "build and compare" - {
    "selects IR-only or all Morphir YAML in basename order and creates output directories" in {
      for
        root <- SquireFixtures.scratch("schemas-build")
        source = root / "source"
        out    = root / "nested" / "generated"
        allOut = root / "all"
        _ <- Sync.defer {
          Files.createDirectories(source.toJava)
          Files.writeString((source / "morphir-ir-z.yaml").toJava, "z: 1\n")
          Files.writeString((source / "morphir-config-v1.yaml").toJava, "config: true\n")
          Files.writeString((source / "morphir-ir-a.yaml").toJava, "a: 2\n")
          Files.writeString((source / "ignored.yaml").toJava, "ignored: true\n")
        }
        ir  <- SquireSchemas.build(source, out, all = false)
        all <- SquireSchemas.build(source, allOut, all = true)
      yield assert(
        ir.outcomes.map(_.file) == List("morphir-ir-a.json", "morphir-ir-z.json") &&
          ir.outcomes.forall(_.status == "written") && Files.isDirectory(out.toJava) &&
          all.outcomes.map(_.file) == List("morphir-config-v1.json", "morphir-ir-a.json", "morphir-ir-z.json")
      )
    }

    "reports identical, drifted, missing, and the all-missing summary with nonzero comparison status" in {
      for
        root <- SquireFixtures.scratch("schemas-compare")
        source  = root / "source"
        target  = root / "target"
        missing = root / "missing"
        _ <- Sync.defer {
          Files.createDirectories(source.toJava)
          Files.createDirectories(target.toJava)
          Files.createDirectories(missing.toJava)
          Files.writeString((source / "morphir-ir-a.yaml").toJava, "a: 1\n")
          Files.writeString((source / "morphir-ir-b.yaml").toJava, "b: 2\n")
          Files.writeString((source / "morphir-config-v1.yaml").toJava, "config: true\n")
          Files.writeString((target / "morphir-ir-a.json").toJava, "{\n  \"a\": 1\n}\n")
          Files.writeString((target / "morphir-ir-b.json").toJava, "{}\n")
        }
        compared   <- SquireSchemas.compare(source, target, all = true)
        allMissing <- SquireSchemas.compare(source, missing, all = false)
      yield assert(
        compared.outcomes.map(outcome => outcome.file -> outcome.status) == List(
          "morphir-config-v1.json" -> "missing",
          "morphir-ir-a.json"      -> "identical",
          "morphir-ir-b.json"      -> "drifted"
        ) && !compared.ok && SquireSchemas.exitCode(compared) == 1 &&
          allMissing.outcomes.forall(_.status == "missing") &&
          SquireSchemas.renderText(allMissing).contains("no generated JSON under")
      )
    }

    "fails explicitly when no input names match" in {
      for
        root <- SquireFixtures.scratch("schemas-empty")
        source = root / "source"
        _      <- Sync.defer(Files.createDirectories(source.toJava))
        result <- Abort.run[SquireError](SquireSchemas.build(source, root / "out", all = false))
      yield assert(result.isFailure)
    }
  }

  "validation" - {
    "fails when jsonschema is unavailable" in {
      for
        root <- SquireFixtures.scratch("schemas-no-jsonschema")
        runner = RuleRunner(request => ProcessResult(request, 127, "", "not found"))
        result <- Abort.run[SquireError](SquireSchemas.validate(root, root, root, runner))
      yield assert(result.isFailure && runner.requests.map(_.argv) == Chunk(Chunk("jsonschema", "--version")))
    }

    "distinguishes an incompatible validator command from an invalid schema" in {
      for
        root <- SquireFixtures.scratch("schemas-incompatible-jsonschema")
        yaml = root / "morphir-ir-v4.yaml"
        _ <- Sync.defer(Files.writeString(yaml.toJava, "type: object\n"))
        runner = RuleRunner { request =>
          if request.argv == Chunk("/tools/jsonschema", "--version") then ProcessResult(request, 0, "4.10.3\n", "")
          else ProcessResult(request, 2, "", "usage: jsonschema\njsonschema: error: unrecognized arguments")
        }
        result <- Abort.run[SquireError](
          SquireSchemas.validate(root, root, root, runner, jsonschema = "/tools/jsonschema")
        )
      yield assert(result.isFailure && runner.requests.map(_.argv) == Chunk(
        Chunk("/tools/jsonschema", "--version"),
        Chunk("/tools/jsonschema", "metaschema", yaml.toString)
      ))
    }

    "reports sorted metaschema and document outcomes, skips non-documents, and keeps validation non-gating" in {
      for
        root <- SquireFixtures.scratch("schemas-validate")
        yaml      = root / "yaml"
        generated = root / "generated"
        documents = root / "documents"
        _ <- Sync.defer {
          Files.createDirectories(yaml.toJava)
          Files.createDirectories(generated.toJava)
          Files.createDirectories(documents.toJava)
          Files.writeString((yaml / "morphir-ir-z.yaml").toJava, "type: object\n")
          Files.writeString((yaml / "morphir-ir-a.yaml").toJava, "type: object\n")
          Files.writeString((generated / "morphir-ir-v4.json").toJava, "{}\n")
          Files.writeString((documents / "a-invalid.json").toJava, "{\"formatVersion\":\"4.2\"}\n")
          Files.writeString((documents / "b-malformed.json").toJava, "{not-json")
          Files.writeString((documents / "c-array.json").toJava, "[]\n")
          Files.writeString((documents / "d-missing-schema.json").toJava, "{\"formatVersion\":\"9.0\"}\n")
          Files.writeString((documents / "z-valid.json").toJava, "{\"formatVersion\":4}\n")
        }
        runner = RuleRunner { request =>
          val name = request.argv.lastOption.getOrElse("")
          if request.argv == Chunk("jsonschema", "--version") then ProcessResult(request, 0, "v0", "")
          else if request.argv.lift(1).contains("metaschema") && name.endsWith("morphir-ir-z.yaml") then
            ProcessResult(request, 1, "", "bad schema")
          else if request.argv.lift(1).contains("validate") && name.endsWith("a-invalid.json") then
            ProcessResult(request, 2, "", s"fail: $name\nerror: Schema validation failure")
          else ProcessResult(request, 0, "", "")
        }
        report <- SquireSchemas.validate(yaml, generated, documents, runner)
      yield assert(
        report.ok && SquireSchemas.exitCode(report) == 0 &&
          report.outcomes.map(outcome => outcome.file -> outcome.status) == List(
            "morphir-ir-a.yaml"     -> "metaschema-valid",
            "morphir-ir-z.yaml"     -> "metaschema-invalid",
            "a-invalid.json"        -> "invalid",
            "b-malformed.json"      -> "skipped",
            "c-array.json"          -> "skipped",
            "d-missing-schema.json" -> "skipped",
            "z-valid.json"          -> "valid"
          ) &&
          runner.requests.exists(_.argv == Chunk(
            "jsonschema",
            "validate",
            (generated / "morphir-ir-v4.json").toString,
            (documents / "z-valid.json").toString
          )) &&
          runner.requests.forall(!_.argv.exists(_.contains("python")))
      )
    }

    "aborts when unexpected exits mimic Sourcemeta's validation failure" in {
      for
        root <- SquireFixtures.scratch("schemas-fatal-validation")
        yaml = root / "morphir-ir-v4.yaml"
        _       <- Sync.defer(Files.writeString(yaml.toJava, "type: object\n"))
        results <- Kyo.foreach(Chunk(42, 127)) { exitCode =>
          val runner = RuleRunner { request =>
            if request.argv == Chunk("jsonschema", "--version") then ProcessResult(request, 0, "v0", "")
            else
              ProcessResult(
                request,
                exitCode,
                "",
                s"fail: ${request.argv.lastOption.getOrElse("")}\nerror: Schema validation failure\n"
              )
          }
          Abort.run[SquireError](SquireSchemas.validate(root, root, root, runner))
        }
      yield assert(results.forall(_.isFailure))
    }

    "rejects every exit-2 result outside the exact target-matched Sourcemeta shape" in {
      for
        root <- SquireFixtures.scratch("schemas-exact-validation-failure")
        yaml = root / "morphir-ir-v4.yaml"
        _       <- Sync.defer(Files.writeString(yaml.toJava, "type: object\n"))
        results <- Kyo.foreach(Chunk("leading", "trailing", "mismatched", "empty", "stdout")) { shape =>
          val runner = RuleRunner { request =>
            if request.argv == Chunk("jsonschema", "--version") then ProcessResult(request, 0, "v0", "")
            else
              val target = request.argv.lastOption.getOrElse("")
              val stderr = shape match
                case "leading"    => s"fatal: transport failed\nfail: $target\nerror: Schema validation failure"
                case "trailing"   => s"fail: $target\nerror: Schema validation failure\nfatal: transport failed"
                case "mismatched" => s"fail: ${root / "unrelated.yaml"}\nerror: Schema validation failure"
                case "empty"      => "fail: \nerror: Schema validation failure"
                case _            => s"fail: $target\nerror: Schema validation failure"
              ProcessResult(request, 2, if shape == "stdout" then "unexpected stdout" else "", stderr)
          }
          Abort.run[SquireError](SquireSchemas.validate(root, root, root, runner))
        }
      yield assert(results.forall(_.isFailure))
    }

    "matches Sourcemeta's absolute normalized target for a relative request" in {
      for
        root <- SquireFixtures.scratch("schemas-relative-validation-target")
        yaml = root / "morphir-ir-v4.yaml"
        _ <- Sync.defer(Files.writeString(yaml.toJava, "type: object\n"))
        relativeRoot = Path(skillDirectory.relativize(root.toJava).toString)
        runner       = RuleRunner { request =>
          if request.argv == Chunk("jsonschema", "--version") then ProcessResult(request, 0, "v0", "")
          else
            val requested = java.nio.file.Paths.get(request.argv.lastOption.getOrElse(""))
            val target    = skillDirectory.resolve(requested).toAbsolutePath.normalize
            ProcessResult(request, 2, "", s"fail: $target\nerror: Schema validation failure")
        }
        report <- SquireSchemas.validate(relativeRoot, relativeRoot, relativeRoot, runner)
      yield assert(report.outcomes.map(_.status) == List("metaschema-invalid"))
    }

    "accepts installed Sourcemeta structured diagnostics only as complete detail triplets" in {
      for
        root <- SquireFixtures.scratch("schemas-structured-validation-failure")
        yaml = root / "morphir-ir-v4.yaml"
        _ <- Sync.defer(Files.writeString(yaml.toJava, "type: object\n"))
        runner = RuleRunner { request =>
          if request.argv == Chunk("jsonschema", "--version") then ProcessResult(request, 0, "v0", "")
          else
            val target = request.argv.lastOption.getOrElse("")
            ProcessResult(
              request,
              2,
              "",
              s"""fail: $target
                 |error: Schema validation failure
                 |  The value was expected to be of type boolean, or object but it was of type string
                 |    at instance location "/description" (line 83, column 7)
                 |    at evaluate path "/properties/description/type"
                 |  The object value was expected to validate against the defined properties subschemas
                 |    at instance location "" (line 1, column 1)
                 |    at evaluate path "/properties"
                 |""".stripMargin
            )
        }
        report <- SquireSchemas.validate(root, root, root, runner)
      yield assert(report.outcomes.map(_.status) == List("metaschema-invalid"))
    }
  }

class SquireSpecSpec extends Test[Any]:
  import SquireSpecFixtures.*

  "repository and checkout boundaries" - {
    "discovers the repository by its sibling kb launcher and reports the unified add hint" in {
      for
        root <- rootWithoutCheckout("spec-root")
        nested = root / ".claude" / "skills" / "squire" / "nested"
        _ <- Sync.defer(Files.createDirectories(nested.toJava))
        platform = TestSpecPlatform()
        found <- SquireSpec.findRepoRoot(nested, platform)
        runner = RuleRunner(unexpected)
        report <- SquireSpec.sync(SpecSyncOptions(noFetch = true), root, runner, platform)
        checkout = report.steps.find(_.step == "checkout")
      yield assert(
        found == Present(root) && !report.ok && runner.requests.isEmpty &&
          checkout.exists(step =>
            step.status == "failed" &&
              step.detail.contains("no reference checkout of finos/morphir") &&
              step.hint.exists(
                _ ==
                  "add one with:\n    squire reference repo add https://github.com/finos/morphir --sparse docs --sparse website --sparse tests/bdd --sparse wit"
              )
          ) && safe(runner)
      )
    }

    "warns for an incomplete sparse checkout but refuses prune before kb work" in {
      for
        root <- preparedRoot("spec-sparse", Chunk("docs"), schemas = false)
        checkout    = root / SquireSpec.CheckoutRel
        pruneRunner = syncRunner(root, sparse = true)
        prune <- SquireSpec.sync(SpecSyncOptions(prune = true, noFetch = true), root, pruneRunner, TestSpecPlatform())
        warningRunner = syncRunner(root, sparse = true)
        warning <- SquireSpec.sync(SpecSyncOptions(noFetch = true), root, warningRunner, TestSpecPlatform())
        warningStep = warning.steps.find(_.step == "checkout")
      yield assert(
        !prune.ok && prune.steps.last.status == "failed" && prune.steps.last.detail.contains("--prune would delete") &&
          pruneRunner.requests.map(_.argv) == Chunk(revParse(checkout), sparseConfig(checkout)) &&
          warning.ok && warningStep.exists(step =>
            step.status == "ok" && step.detail.contains("missing website, tests/bdd, wit")
          ) && safe(pruneRunner) && safe(warningRunner)
      )
    }
  }

  "sync orchestration" - {
    "refuses a dirty fetch and honours no-fetch and dry-run without refresh mutation" in {
      for
        dirtyRoot <- preparedRoot("spec-dirty")
        dirtyCheckout = dirtyRoot / SquireSpec.CheckoutRel
        dirtyRunner   = syncRunner(dirtyRoot, dirty = " M exported.yaml\n")
        dirty       <- SquireSpec.sync(SpecSyncOptions(ref = "v4"), dirtyRoot, dirtyRunner, TestSpecPlatform())
        noFetchRoot <- preparedRoot("spec-no-fetch")
        noFetchRunner = syncRunner(noFetchRoot)
        noFetch <- SquireSpec.sync(SpecSyncOptions(noFetch = true), noFetchRoot, noFetchRunner, TestSpecPlatform())
        dryRoot <- preparedRoot("spec-dry")
        dryRunner = syncRunner(dryRoot)
        dry <- SquireSpec.sync(SpecSyncOptions(ref = "release", dryRun = true), dryRoot, dryRunner, TestSpecPlatform())
        dryKb = kb(dryRoot, "sync", "pull", "--dry-run")
      yield assert(
        !dirty.ok && dirty.steps.last.step == "fetch" && dirty.steps.last.detail.contains("uncommitted changes") &&
          dirtyRunner.requests.map(_.argv) == Chunk(
            revParse(dirtyCheckout),
            sparseConfig(dirtyCheckout),
            dirtyStatus(dirtyCheckout)
          ) &&
          noFetch.ok && noFetch.steps.find(_.step == "fetch").exists(step =>
            step.status == "skipped" && step.detail == "--no-fetch"
          ) && !noFetchRunner.requests.exists(_.argv == dirtyStatus(noFetchRoot / SquireSpec.CheckoutRel)) &&
          dry.ok && dry.steps.find(_.step == "fetch").exists(step =>
            step.status == "skipped" && step.detail == "--dry-run"
          ) && dryRunner.requests.exists(_.argv == dryKb) &&
          !dryRunner.requests.exists(request => request.argv.contains("fetch") || request.argv.contains("checkout")) &&
          safe(dirtyRunner) && safe(noFetchRunner) && safe(dryRunner)
      )
    }

    "uses the exact shallow fetch and detached checkout then parses status and pull options" in {
      for
        root <- preparedRoot("spec-fetch")
        checkout = root / SquireSpec.CheckoutRel
        runner   = syncRunner(root, statusJson = "banner\n{\"summary\":{\"clean\":2,\"local-only\":1}}\n")
        report <- SquireSpec.sync(
          SpecSyncOptions(ref = "v4-draft", theirs = true, prune = true, json = true),
          root,
          runner,
          TestSpecPlatform()
        )
        fetch        = Chunk("git", "-C", checkout.toString, "fetch", "--depth", "1", "origin", "v4-draft")
        detach       = Chunk("git", "-C", checkout.toString, "checkout", "--detach", "FETCH_HEAD")
        pull         = kb(root, "sync", "pull", "--theirs", "--prune", "--json")
        statusResult = report.steps.find(_.step == "status").flatMap(_.result.toOption)
      yield assert(
        report.ok && runner.requests.map(_.argv).contains(fetch) && runner.requests.map(_.argv).contains(detach) &&
          runner.requests.map(_.argv).contains(pull) && statusResult.exists(recordHas(_, "summary")) &&
          report.steps.map(_.step) == List("checkout", "fetch", "status", "pull", "check") && safe(runner)
      )
    }

    "reports malformed status, pull failure, and final check failure without continuing past the failed step" in {
      for
        statusRoot <- preparedRoot("spec-status-fail")
        statusRunner = syncRunner(statusRoot, statusJson = "not json\n")
        status   <- SquireSpec.sync(SpecSyncOptions(noFetch = true), statusRoot, statusRunner, TestSpecPlatform())
        pullRoot <- preparedRoot("spec-pull-fail")
        pullRunner = syncRunner(pullRoot, pullExit = 9)
        pull      <- SquireSpec.sync(SpecSyncOptions(noFetch = true), pullRoot, pullRunner, TestSpecPlatform())
        checkRoot <- preparedRoot("spec-check-fail")
        checkRunner = syncRunner(checkRoot, checkExit = 1)
        check <-
          SquireSpec.sync(SpecSyncOptions(noFetch = true, json = true), checkRoot, checkRunner, TestSpecPlatform())
      yield assert(
        !status.ok && status.steps.last.step == "status" && !statusRunner.requests.exists(_.argv == kb(
          statusRoot,
          "sync",
          "pull"
        )) &&
          !pull.ok && pull.steps.last.step == "pull" && !pullRunner.requests.exists(
            _.argv.headOption.contains("check")
          ) &&
          !check.ok && check.steps.last.step == "check" && check.steps.last.status == "failed" &&
          safe(statusRunner) && safe(pullRunner) && safe(checkRunner)
      )
    }

    "preserves exact failed text diagnostics and excludes raw JSON output" in {
      for
        textRoot   <- preparedRoot("spec-check-diagnostics")
        textReport <- SquireSpec.sync(
          SpecSyncOptions(noFetch = true),
          textRoot,
          syncRunner(
            textRoot,
            checkExit = 1,
            checkOutput = "  ERROR invalid knowledge entry\nstdout tail  \n",
            checkError = Some("\tschema path: kb/example.yaml\nstderr tail\n\n")
          ),
          TestSpecPlatform()
        )
        textDetail     = textReport.steps.find(_.step == "check").map(_.detail).getOrElse("")
        textRendered   = SquireSpec.renderText(textReport)
        expectedDetail =
          "check --no-provenance\n  ERROR invalid knowledge entry\nstdout tail  \n" +
            "\tschema path: kb/example.yaml\nstderr tail\n\n"
        expectedRendered =
          s"[1/5] reference checkout\n  ok: $textRoot/${SquireSpec.CheckoutRel}\n" +
            "[2/5] refresh finos/morphir\n  skipped: --no-fetch\n" +
            "[3/5] sync status\n  ok: kb sync status\n" +
            "[4/5] sync pull\n  ok: sync pull\n" +
            "[5/5] kb check\n  ERROR: " + expectedDetail +
            "\n\nWorkflow failed.\n"
        jsonRoot   <- preparedRoot("spec-check-json-diagnostics")
        jsonReport <- SquireSpec.sync(
          SpecSyncOptions(noFetch = true, json = true),
          jsonRoot,
          syncRunner(
            jsonRoot,
            checkExit = 1,
            checkOutput = "{\"findings\":[{\"message\":\"raw-json-marker\"}]}\n",
            checkError = Some("json stderr marker\n")
          ),
          TestSpecPlatform()
        )
        jsonDetail   = jsonReport.steps.find(_.step == "check").map(_.detail).getOrElse("")
        jsonRendered = SquireSpec.renderText(jsonReport)
      yield assert(
        !textReport.ok && textDetail == expectedDetail && textRendered == expectedRendered &&
          !jsonReport.ok && jsonDetail == "check --no-provenance --json" &&
          !jsonDetail.contains("raw-json-marker") && !jsonRendered.contains("raw-json-marker") &&
          !jsonDetail.contains("json stderr marker") && !jsonRendered.contains("json stderr marker")
      )
    }

    "fails closed on repository sparse dirty and kb status probe failures" in {
      for
        headRoot <- preparedRoot("spec-head-probe")
        headCheckout = headRoot / SquireSpec.CheckoutRel
        headRunner   = syncRunner(headRoot, headExit = 128, headError = "not a repository")
        head      <- SquireSpec.sync(SpecSyncOptions(prune = true), headRoot, headRunner, TestSpecPlatform())
        emptyRoot <- preparedRoot("spec-empty-head")
        emptyRunner = syncRunner(emptyRoot, headOutput = "")
        empty      <- SquireSpec.sync(SpecSyncOptions(prune = true), emptyRoot, emptyRunner, TestSpecPlatform())
        sparseRoot <- preparedRoot("spec-sparse-probe", Chunk("docs"), schemas = false)
        sparseCheckout = sparseRoot / SquireSpec.CheckoutRel
        sparseRunner   = syncRunner(sparseRoot, sparseExit = 128, sparseError = "config failed")
        sparse <- SquireSpec.sync(
          SpecSyncOptions(prune = true, noFetch = true),
          sparseRoot,
          sparseRunner,
          TestSpecPlatform()
        )
        unsetRoot <- preparedRoot("spec-sparse-unset")
        unsetRunner = syncRunner(unsetRoot, sparseExit = 1)
        unset     <- SquireSpec.sync(SpecSyncOptions(noFetch = true), unsetRoot, unsetRunner, TestSpecPlatform())
        dirtyRoot <- preparedRoot("spec-dirty-probe")
        dirtyCheckout = dirtyRoot / SquireSpec.CheckoutRel
        dirtyRunner   = syncRunner(dirtyRoot, dirtyExit = 128, dirtyError = "status failed")
        dirty      <- SquireSpec.sync(SpecSyncOptions(prune = true), dirtyRoot, dirtyRunner, TestSpecPlatform())
        statusRoot <- preparedRoot("spec-kb-status-probe")
        statusRunner = syncRunner(statusRoot, statusExit = 7)
        status <- SquireSpec.sync(
          SpecSyncOptions(prune = true, noFetch = true),
          statusRoot,
          statusRunner,
          TestSpecPlatform()
        )
      yield assert(
        !head.ok && head.steps.last.step == "checkout" && headRunner.requests.map(_.argv) == Chunk(
          revParse(headCheckout)
        ) &&
          !empty.ok && empty.steps.last.step == "checkout" && emptyRunner.requests.size == 1 &&
          !sparse.ok && sparse.steps.last.step == "checkout" && sparseRunner.requests.map(_.argv) == Chunk(
            revParse(sparseCheckout),
            sparseConfig(sparseCheckout)
          ) && !sparseRunner.requests.exists(_.argv == kb(sparseRoot, "sync", "pull", "--prune")) &&
          unset.ok && unset.steps.find(_.step == "checkout").exists(_.status == "ok") &&
          !dirty.ok && dirty.steps.last.step == "fetch" && dirtyRunner.requests.map(_.argv) == Chunk(
            revParse(dirtyCheckout),
            sparseConfig(dirtyCheckout),
            dirtyStatus(dirtyCheckout)
          ) && !dirtyRunner.requests.exists(_.argv.contains("fetch")) &&
          !status.ok && status.steps.last.step == "status" &&
          !statusRunner.requests.exists(_.argv == kb(statusRoot, "sync", "pull", "--prune")) &&
          List(headRunner, emptyRunner, sparseRunner, unsetRunner, dirtyRunner, statusRunner).forall(safe)
      )
    }
  }

  "export orchestration" - {
    "validates the target then requests kb JSON push with every option and extracts written paths" in {
      for
        root <- rootWithoutCheckout("spec-export-target")
        missing       = root / "missing"
        missingRunner = RuleRunner(unexpected)
        missingReport <- SquireSpec.`export`(
          SpecExportOptions(to = Present(missing)),
          root,
          missingRunner,
          TestSpecPlatform()
        )
        checkout <- standaloneCheckout(root / "target")
        runner = exportRunner(
          root,
          checkout,
          written = List("docs/spec.md", "website/static/schemas/morphir-ir-v4.yaml")
        )
        report <- SquireSpec.`export`(
          SpecExportOptions(
            to = Present(checkout),
            dryRun = true,
            includeDiverged = true,
            noBranch = true,
            json = true
          ),
          root,
          runner,
          TestSpecPlatform()
        )
        expected   = kb(root, "sync", "push", "--to", checkout.toString, "--dry-run", "--include-diverged", "--json")
        pushResult = report.steps.find(_.step == "push").flatMap(_.result.toOption)
      yield assert(
        !missingReport.ok && missingRunner.requests.isEmpty && missingReport.steps.last.step == "checkout" &&
          report.ok && runner.requests.map(_.argv).take(2) == Chunk(gitCheckoutProbe(checkout), expected) &&
          pushResult.exists(recordHas(_, "actions")) &&
          report.steps.find(_.step == "push").exists(_.detail.contains("2 written path(s)")) &&
          report.steps.find(_.step == "branch").exists(_.status == "skipped") && safe(missingRunner) && safe(runner)
      )
    }

    "rejects a non-Git target before kb push" in {
      for
        root     <- rootWithoutCheckout("spec-non-git-target")
        checkout <- standaloneCheckout(root / "ordinary-directory")
        runner = exportRunner(root, checkout, gitCheckoutExit = 128)
        report <- SquireSpec.`export`(
          SpecExportOptions(to = Present(checkout)),
          root,
          runner,
          TestSpecPlatform()
        )
      yield assert(
        !report.ok && report.steps.last.step == "checkout" &&
          runner.requests.map(_.argv) == Chunk(gitCheckoutProbe(checkout)) &&
          !runner.requests.exists(_.argv.take(3) == kb(root, "sync", "push").take(3)) && safe(runner)
      )
    }

    "fails push when ownership JSON is absent malformed non-object or structurally invalid" in {
      val cases = Chunk(
        ("absent", "", false),
        ("malformed", "{", false),
        ("non-object", "[]", true),
        ("scalar", "7", true),
        ("missing-actions", "{}", true),
        ("duplicate-actions", "{\"actions\":[],\"actions\":[]}", true),
        ("actions-object", "{\"actions\":{}}", true),
        ("action-scalar", "{\"actions\":[\"wrote\"]}", true),
        ("wrote-missing-path", "{\"actions\":[{\"verb\":\"wrote\"}]}", true),
        ("other-missing-path", "{\"actions\":[{\"verb\":\"held back\"}]}", true),
        ("wrote-empty-path", "{\"actions\":[{\"verb\":\"wrote\",\"path\":\"\"}]}", true),
        ("wrote-non-string-path", "{\"actions\":[{\"verb\":\"wrote\",\"path\":7}]}", true),
        ("non-string-verb", "{\"actions\":[{\"verb\":7,\"path\":\"website/static/schemas/x.yaml\"}]}", true)
      )
      Kyo.foreach(cases) { case (label, payload, parsedEvidence) =>
        for
          root     <- rootWithoutCheckout(s"spec-push-json-$label")
          checkout <- standaloneCheckout(root / "checkout", schemas = true)
          runner = exportRunner(root, checkout, pushStdout = Some(payload))
          report <- SquireSpec.`export`(
            SpecExportOptions(to = Present(checkout), noBranch = true),
            root,
            runner,
            TestSpecPlatform()
          )
          push = report.steps.find(_.step == "push")
        yield !report.ok && push.exists(step => step.status == "failed" && step.result.isDefined == parsedEvidence) &&
          report.steps.last.step == "status" &&
          !runner.requests.exists(_.argv.headOption.contains("jsonschema")) && safe(runner)
      }.map(results => assert(results.forall(identity)))
    }

    "creates reuses or fails the review branch without ever committing or pushing" in {
      for
        root            <- rootWithoutCheckout("spec-branches")
        createdCheckout <- standaloneCheckout(root / "created")
        createdRunner = exportRunner(root, createdCheckout)
        created <-
          SquireSpec.`export`(SpecExportOptions(to = Present(createdCheckout)), root, createdRunner, TestSpecPlatform())
        reusedCheckout <- standaloneCheckout(root / "reused")
        reusedRunner = exportRunner(root, reusedCheckout, createBranchExit = 1)
        reused <-
          SquireSpec.`export`(SpecExportOptions(to = Present(reusedCheckout)), root, reusedRunner, TestSpecPlatform())
        failedCheckout <- standaloneCheckout(root / "failed")
        failedRunner = exportRunner(root, failedCheckout, createBranchExit = 1, reuseBranchExit = 1)
        failed <-
          SquireSpec.`export`(SpecExportOptions(to = Present(failedCheckout)), root, failedRunner, TestSpecPlatform())
      yield assert(
        created.ok && created.steps.find(_.step == "branch").exists(step =>
          step.status == "ok" && step.detail.contains("created")
        ) &&
          reused.ok && reused.steps.find(_.step == "branch").exists(step =>
            step.status == "ok" && step.detail.contains("already existed")
          ) &&
          !failed.ok && failed.steps.find(_.step == "branch").exists(_.status == "failed") &&
          failed.steps.last.step == "status" &&
          failedRunner.requests.exists(_.argv == Chunk("git", "-C", failedCheckout.toString, "status", "--short")) &&
          safe(createdRunner) && safe(reusedRunner) && safe(failedRunner)
      )
    }

    "preserves partial push and runner Abort failures while always recording final status" in {
      for
        pushRoot     <- rootWithoutCheckout("spec-partial-push")
        pushCheckout <- standaloneCheckout(pushRoot / "checkout")
        partialPayload = "{\"actions\":[{\"verb\":\"wrote\",\"path\":\"docs/spec.md\"}]}"
        pushRunner     = exportRunner(
          pushRoot,
          pushCheckout,
          pushExit = 9,
          pushStdout = Some(partialPayload),
          changed = " M docs/spec.md\n"
        )
        push <- SquireSpec.`export`(
          SpecExportOptions(to = Present(pushCheckout)),
          pushRoot,
          pushRunner,
          TestSpecPlatform()
        )
        abortRoot     <- rootWithoutCheckout("spec-aborted-push")
        abortCheckout <- standaloneCheckout(abortRoot / "checkout")
        abortBase   = exportResponse(abortRoot, abortCheckout, changed = " M docs/spec.md\n")
        abortRunner = AbortRuleRunner { request =>
          if request.argv.take(3) == kb(abortRoot, "sync", "push").take(3) then
            Abort.fail(SquireError.Failure("process", "push launch failed"))
          else abortBase(request)
        }
        aborted <- Abort.run[SquireError](
          SquireSpec.`export`(
            SpecExportOptions(to = Present(abortCheckout)),
            abortRoot,
            abortRunner,
            TestSpecPlatform()
          )
        )
      yield assert(
        !push.ok && push.steps.find(_.step == "push").exists(step =>
          step.status == "failed" && step.result.exists(recordHas(_, "actions"))
        ) && push.steps.last.step == "status" &&
          !pushRunner.requests.exists(_.argv.contains("switch")) &&
          aborted.exists(report =>
            !report.ok && report.steps.find(_.step == "push").exists(step =>
              step.status == "failed" && step.detail.contains("push launch failed")
            ) && report.steps.last.step == "status"
          ) && safe(pushRunner) && safe(abortRunner)
      )
    }

    "contains validator launch Abort and makes nonzero final status fail the report" in {
      for
        validatorRoot     <- rootWithoutCheckout("spec-validator-abort")
        validatorCheckout <- standaloneCheckout(validatorRoot / "checkout", schemas = true)
        validatorBase   = exportResponse(validatorRoot, validatorCheckout)
        validatorRunner = AbortRuleRunner { request =>
          if request.argv.headOption.contains("jsonschema") then
            Abort.fail(SquireError.Failure("process", "validator launch failed"))
          else validatorBase(request)
        }
        validator <- Abort.run[SquireError](
          SquireSpec.`export`(
            SpecExportOptions(to = Present(validatorCheckout), noBranch = true),
            validatorRoot,
            validatorRunner,
            TestSpecPlatform()
          )
        )
        statusRoot     <- rootWithoutCheckout("spec-status-exit")
        statusCheckout <- standaloneCheckout(statusRoot / "checkout", schemas = true)
        statusRunner = exportRunner(statusRoot, statusCheckout, statusExit = 7)
        status <- SquireSpec.`export`(
          SpecExportOptions(to = Present(statusCheckout), noBranch = true),
          statusRoot,
          statusRunner,
          TestSpecPlatform()
        )
      yield assert(
        validator.exists(report =>
          !report.ok && report.steps.find(_.step == "validator:jsonschema fmt").exists(step =>
            step.status == "failed" && step.detail.contains("validator launch failed")
          ) && report.steps.last.step == "status"
        ) &&
          !status.ok && status.steps.last.step == "status" && status.steps.last.status == "failed" &&
          status.steps.last.detail.contains("exited 7") && safe(validatorRunner) && safe(statusRunner)
      )
    }

    "expands validator globs and classifies YAML unsupported, pre-existing, and owned failures" in {
      for
        preRoot     <- rootWithoutCheckout("spec-preexisting")
        preCheckout <- standaloneCheckout(preRoot / "preexisting", schemas = true)
        preRunner   = exportRunner(preRoot, preCheckout, validatorFailure = Some("lint"))
        prePlatform = TestSpecPlatform()
        pre <- SquireSpec.`export`(
          SpecExportOptions(to = Present(preCheckout), noBranch = true),
          preRoot,
          preRunner,
          prePlatform
        )
        ownRoot     <- rootWithoutCheckout("spec-owned")
        ownCheckout <- standaloneCheckout(ownRoot / "owned", schemas = true)
        ownRunner = exportRunner(
          ownRoot,
          ownCheckout,
          written = List("website/static/schemas/morphir-ir-v4.yaml"),
          validatorFailure = Some("lint")
        )
        own <- SquireSpec.`export`(
          SpecExportOptions(to = Present(ownCheckout), noBranch = true),
          ownRoot,
          ownRunner,
          TestSpecPlatform()
        )
        yamlRoot     <- rootWithoutCheckout("spec-yaml-unsupported")
        yamlCheckout <- standaloneCheckout(yamlRoot / "yaml", schemas = true)
        yamlRunner = exportRunner(yamlRoot, yamlCheckout, yamlUnsupported = true)
        yaml <- SquireSpec.`export`(
          SpecExportOptions(to = Present(yamlCheckout), noBranch = true),
          yamlRoot,
          yamlRunner,
          TestSpecPlatform()
        )
        expanded = Chunk(
          "jsonschema",
          "lint",
          "website/static/schemas/morphir-ir-v4.yaml"
        )
      yield assert(
        pre.ok && pre.steps.find(_.step == "validator:jsonschema lint").exists(_.status == "pre-existing") &&
          !own.ok && own.steps.find(_.step == "validator:jsonschema lint").exists(_.status == "failed") &&
          yaml.ok && yaml.steps.find(_.step == "validator:jsonschema fmt").exists(step =>
            step.status == "skipped" && step.detail.contains("does not support YAML")
          ) &&
          preRunner.requests.exists(_.argv == expanded) && safe(preRunner) && safe(ownRunner) && safe(yamlRunner)
      )
    }

    "skips absent tools paths and globs while replacing Bun with in-process schema comparison" in {
      for
        toolRoot     <- rootWithoutCheckout("spec-no-tool")
        toolCheckout <- standaloneCheckout(toolRoot / "no-tool", schemas = true)
        toolRunner   = exportRunner(toolRoot, toolCheckout)
        toolPlatform = TestSpecPlatform(executables = Set.empty)
        toolReport <- SquireSpec.`export`(
          SpecExportOptions(to = Present(toolCheckout), noBranch = true),
          toolRoot,
          toolRunner,
          toolPlatform
        )
        pathRoot     <- rootWithoutCheckout("spec-no-path")
        pathCheckout <- standaloneCheckout(pathRoot / "no-path", schemas = false)
        pathRunner   = exportRunner(pathRoot, pathCheckout)
        pathPlatform = TestSpecPlatform()
        pathReport <- SquireSpec.`export`(
          SpecExportOptions(to = Present(pathCheckout), noBranch = true),
          pathRoot,
          pathRunner,
          pathPlatform
        )
        globRoot     <- rootWithoutCheckout("spec-no-glob")
        globCheckout <- standaloneCheckout(globRoot / "no-glob", schemas = true, yaml = false)
        globRunner   = exportRunner(globRoot, globCheckout)
        globPlatform = TestSpecPlatform()
        globReport <- SquireSpec.`export`(
          SpecExportOptions(to = Present(globCheckout), noBranch = true),
          globRoot,
          globRunner,
          globPlatform
        )
        schemaDir = toolCheckout / "website" / "static" / "schemas"
      yield assert(
        toolReport.ok && toolReport.steps.count(step =>
          step.step.startsWith("validator:jsonschema") && step.status == "skipped"
        ) == 3 && toolPlatform.schemaRequests == List(schemaDir) &&
          pathReport.ok && pathReport.steps.count(step =>
            step.step.startsWith("validator:") && step.status == "skipped"
          ) == 4 &&
          pathPlatform.schemaRequests.isEmpty &&
          globReport.ok && globReport.steps.find(
            _.step == "validator:jsonschema lint"
          ).exists(_.detail == "no matching files") &&
          globReport.steps.find(_.step == "validator:schemas json in step").exists(_.detail == "no matching files") &&
          globPlatform.schemaRequests.isEmpty &&
          List(toolRunner, pathRunner, globRunner).forall(runner =>
            !runner.requests.exists(_.argv.headOption.contains("bun")) && safe(runner)
          )
      )
    }

    "gates owned schema drift, still records final status, and never requests commit or push" in {
      for
        root     <- rootWithoutCheckout("spec-schema-drift")
        checkout <- standaloneCheckout(root / "checkout", schemas = true)
        runner = exportRunner(
          root,
          checkout,
          written = List("website/static/schemas/morphir-ir-v4.yaml"),
          changed = " M website/static/schemas/morphir-ir-v4.yaml\n"
        )
        platform = TestSpecPlatform(schemaOk = false)
        report <- SquireSpec.`export`(
          SpecExportOptions(to = Present(checkout), noBranch = true),
          root,
          runner,
          platform
        )
        status = report.steps.find(_.step == "status")
      yield assert(
        !report.ok && report.steps.find(_.step == "validator:schemas json in step").exists(_.status == "failed") &&
          status.exists(step => step.status == "ok" && step.detail == "1 changed path(s)") &&
          platform.schemaRequests == List(checkout / "website" / "static" / "schemas") && safe(runner)
      )
    }

    "contains typed schema comparison failures and still records final status" in {
      for
        root     <- rootWithoutCheckout("spec-schema-error")
        checkout <- standaloneCheckout(root / "checkout", schemas = true)
        runner = exportRunner(
          root,
          checkout,
          written = List("website/static/schemas/morphir-ir-v4.yaml"),
          changed = " M website/static/schemas/morphir-ir-v4.yaml\n"
        )
        platform = TestSpecPlatform(schemaFailure = Some("could not decode YAML schema"))
        outcome <- Abort.run[SquireError](
          SquireSpec.`export`(
            SpecExportOptions(to = Present(checkout), noBranch = true),
            root,
            runner,
            platform
          )
        )
      yield assert(
        outcome.exists(report =>
          !report.ok &&
            report.steps.find(_.step == "validator:schemas json in step").exists(step =>
              step.status == "failed" && step.detail.contains("could not decode YAML schema")
            ) &&
            report.steps.last.step == "status"
        ) && safe(runner)
      )
    }
  }

  "CLI reporting" - {
    "renders step text or one clean typed JSON report and forwards the aggregate exit" in {
      for
        textRoot <- preparedRoot("spec-cli-text")
        textOut = new StringBuilder
        textErr = new StringBuilder
        textExit <- SquireCli.runSpecSync(
          SpecSyncOpts(noFetch = true),
          textRoot,
          syncRunner(textRoot),
          TestSpecPlatform(),
          value => textOut.append(value),
          value => textErr.append(value)
        )
        jsonRoot <- preparedRoot("spec-cli-json")
        jsonOut = new StringBuilder
        jsonErr = new StringBuilder
        jsonExit <- SquireCli.runSpecSync(
          SpecSyncOpts(noFetch = true, json = true),
          jsonRoot,
          syncRunner(jsonRoot),
          TestSpecPlatform(),
          value => jsonOut.append(value),
          value => jsonErr.append(value)
        )
        decoded = SquireJson.decode[SpecReport](jsonOut.result().trim)
      yield assert(
        textExit == 0 && textOut.result().contains("[1/5] reference checkout") && textOut.result().contains(
          "Import complete"
        ) &&
          textErr.isEmpty && jsonExit == 0 && decoded.exists(report => report.command == "spec-sync" && report.ok) &&
          !jsonOut.result().contains("[1/5]") && jsonErr.isEmpty
      )
    }

    "reports the primary export failure after successful or failed final status" in
      Kyo.foreach(Chunk(("status-ok", 0), ("status-failed", 7))) { case (label, statusExit) =>
        for
          root     <- rootWithoutCheckout(s"spec-cli-export-$label")
          checkout <- standaloneCheckout(root / "checkout", schemas = true)
          base   = exportResponse(root, checkout, statusExit = statusExit)
          runner = AbortRuleRunner { request =>
            if request.argv.headOption.contains("jsonschema") then
              Abort.fail(SquireError.Failure("process", "validator launch failed"))
            else base(request)
          }
          out = new StringBuilder
          err = new StringBuilder
          result <- Abort.run[SquireError](
            SquireCli.runSpecExport(
              SpecExportOpts(to = Some(checkout.toString), noBranch = true, json = true),
              root,
              runner,
              TestSpecPlatform(),
              value => out.append(value),
              value => err.append(value)
            )
          )
          decoded = SquireJson.decode[SpecReport](out.result().trim)
        yield result == Result.Success(1) && decoded.exists(report =>
          !report.ok && report.command == "spec-export" &&
            report.steps.find(_.status == "failed").exists(_.detail == "validator launch failed") &&
            report.steps.lastOption.exists(step =>
              step.step == "status" && step.status == (if statusExit == 0 then "ok" else "failed")
            )
        ) && out.result().trim.linesIterator.size == 1 &&
          err.result() == "ERROR: validator launch failed\n" && safe(runner)
      }.map(results => assert(results.forall(identity)))
  }

class SquireProcessSpec extends Test[Any]:
  "process runner" - {
    "recording runner preserves argv cwd stdout stderr and exit" in {
      val request  = ProcessRequest(Chunk("git", "status"), Present(Path("/repo")))
      val expected = ProcessResult(request, 7, "out", "err")
      val runner   = RecordingRunner(Chunk(expected))
      runner.run(request).map(result => assert(result == expected && runner.requests == Chunk(request)))
    }

    "live runner captures stdout and stderr separately" in {
      val outputBytes = 128 * 1024
      for
        root <- SquireFixtures.scratch("process")
        source = root / "ProcessProbe.java"
        _ <- Path.run(source.write(
          s"class ProcessProbe { public static void main(String[] a) { String out = \"o\".repeat($outputBytes); String err = \"e\".repeat($outputBytes); System.out.print(out); System.err.print(err); System.exit(7); } }"
        ))
        outcome <- Abort.run[SquireError | Timeout](
          Async.timeout(5.seconds)(
            LiveProcessRunner.run(ProcessRequest(Chunk(SquireFixtures.javaExecutable, SquirePaths.render(source))))
          )
        )
        result = outcome match
          case Result.Success(value) =>
            value.exitCode == 7 &&
            // Java launchers may prepend inherited option diagnostics to either stream in CI.
            value.stdout.endsWith("o".repeat(outputBytes)) &&
            value.stderr.endsWith("e".repeat(outputBytes))
          case Result.Failure(_) => false
      yield assert(result)
    }
  }

object SquireFixtures:
  val javaExecutable: String =
    java.nio.file.Path.of(java.lang.System.getProperty("java.home"), "bin", "java").toString

  /**
   * Canonicalized on creation. `SquireRepo` resolves real paths on purpose: proving a destination is exactly inside its
   * configured base is what stops a symlinked path component escaping it, so the argv it builds and the metadata it
   * records name resolved paths. A scratch root that still carries a symlink therefore does not compare equal to what
   * the code under test produces. On macOS every temp directory is one, because `java.io.tmpdir` sits under
   * `/var/folders`, and `/var` is a symlink to `private/var`; on Linux the two spellings coincide and the difference
   * stays invisible.
   */
  def scratch(name: String): Path < Sync =
    Sync.defer(Path(java.nio.file.Files.createTempDirectory(s"squire-$name-").toRealPath().toString))

  def deleteRecursively(root: Path): Unit =
    if Files.exists(root.toJava) then
      val stream = Files.walk(root.toJava)
      try stream.iterator.asScala.toList.reverse.foreach(Files.deleteIfExists)
      finally stream.close()

  def scopedScratch(name: String): Path < (Scope & Sync) =
    Scope.acquireRelease(scratch(name))(root => Sync.defer(deleteRecursively(root)))

  def probeFiles(root: Path): List[String] =
    val stream = Files.list(root.toJava)
    try stream.iterator.asScala.map(_.getFileName.toString).filter(_.startsWith(".squire-env-probe")).toList
    finally stream.close()

  def platform(
      root: Path,
      jvmResult: SquireEnv.CheckResult,
      environment: Map[String, String] = Map.empty,
      osName: String = "Linux",
      home: Option[Path] = None,
      managed: Chunk[Path] = Chunk.empty,
      varFolders: Option[Path] = None,
      jvmTempDirectory: Maybe[Path] = Absent,
      daemonProbe: Int => SquireEnv.DaemonProbe = _ => SquireEnv.DaemonProbe.Open,
      probePath: Maybe[Path => Path] = Absent,
      writeProbe: Maybe[Path => Unit] = Absent,
      deleteProbe: Maybe[Path => Unit] = Absent
  ): TestEnvPlatform =
    TestEnvPlatform(
      environment,
      home.getOrElse(root / "home"),
      managed,
      varFolders.getOrElse(root),
      jvmTempDirectory.orElse(Present(varFolders.getOrElse(root))),
      _ => jvmResult,
      daemonProbe,
      probePath,
      writeProbe,
      deleteProbe,
      osName
    )

  def writeDaemonFiles(root: Path, portFile: Option[Int], logPort: Option[Int]): Unit < Sync =
    Sync.defer {
      val daemon = root / "out" / "mill-daemon"
      Files.createDirectories(daemon.toJava)
      portFile.foreach(port => Files.writeString((daemon / "socketPort").toJava, port.toString))
      logPort.foreach(port => Files.writeString((daemon / "server.log").toJava, s"listening on port $port\n"))
    }

final class RecordingRunner(responses: Chunk[ProcessResult]) extends ProcessRunner:
  private var index                   = 0
  var requests: Chunk[ProcessRequest] = Chunk.empty

  def run(request: ProcessRequest): ProcessResult < (Async & Abort[SquireError]) =
    requests = requests.append(request)
    val response = responses(index)
    index += 1
    response

trait SpecRecordedRunner:
  def requests: Chunk[ProcessRequest]

final class RuleRunner(response: ProcessRequest => ProcessResult) extends ProcessRunner with SpecRecordedRunner:
  var requests: Chunk[ProcessRequest] = Chunk.empty

  def run(request: ProcessRequest): ProcessResult < (Async & Abort[SquireError]) =
    requests = requests.append(request)
    response(request)

final class AbortRuleRunner(response: ProcessRequest => ProcessResult < Abort[SquireError])
    extends ProcessRunner
    with SpecRecordedRunner:
  var requests: Chunk[ProcessRequest] = Chunk.empty

  def run(request: ProcessRequest): ProcessResult < (Async & Abort[SquireError]) =
    requests = requests.append(request)
    response(request)

object AbortRuleRunner:
  def apply(response: ProcessRequest => ProcessResult < Abort[SquireError]): AbortRuleRunner =
    new AbortRuleRunner(response)

final case class TestSpecPlatform(
    executables: Set[String] = Set("jsonschema"),
    schemaOk: Boolean = true,
    schemaFailure: Option[String] = None
) extends SquireSpecPlatform:
  var schemaRequests: List[Path] = Nil

  def exists(path: Path): Boolean < Sync          = Sync.defer(Files.exists(path.toJava))
  def isDirectory(path: Path): Boolean < Sync     = Sync.defer(Files.isDirectory(path.toJava))
  def isSymlink(path: Path): Boolean < Sync       = Sync.defer(Files.isSymbolicLink(path.toJava))
  def resolve(path: Path): Path < Sync            = Sync.defer(Path(path.toJava.toAbsolutePath.normalize.toString))
  def findExecutable(name: String): Maybe[String] =
    if executables.contains(name) then Present(name) else Absent
  def glob(cwd: Path, pattern: String): Chunk[String] < Sync = LiveSquireSpecPlatform.glob(cwd, pattern)
  def compareSchemas(directory: Path): SchemaReport < (Sync & Abort[SquireError]) =
    schemaRequests = schemaRequests :+ directory
    schemaFailure match
      case Some(message) => Abort.fail(SquireError.Failure("schemas", message))
      case None          =>
        SchemaReport(
          "schemas-to-json",
          directory.toString,
          directory.toString,
          check = true,
          ok = schemaOk,
          List(SchemaOutcome("morphir-ir-v4.json", if schemaOk then "identical" else "drifted"))
        )

object SquireSpecFixtures:
  val head: String = "a" * 40

  def rootWithoutCheckout(name: String): Path < Sync =
    for
      root <- SquireFixtures.scratch(name)
      _    <- Sync.defer {
        val launcher = root / ".claude" / "skills" / "kb" / "kb"
        Files.createDirectories(launcher.parent.get.toJava)
        Files.writeString(launcher.toJava, "#!/bin/sh\n")
      }
    yield root

  def preparedRoot(
      name: String,
      sparse: Chunk[String] = SquireSpec.SparsePaths,
      schemas: Boolean = true
  ): Path < Sync =
    for
      root <- rootWithoutCheckout(name)
      _    <- standaloneCheckout(root / SquireSpec.CheckoutRel, sparse, schemas)
    yield root

  def standaloneCheckout(
      checkout: Path,
      sparse: Chunk[String] = SquireSpec.SparsePaths,
      schemas: Boolean = false,
      yaml: Boolean = true
  ): Path < Sync =
    Sync.defer {
      Files.createDirectories((checkout / ".git").toJava)
      sparse.foreach(path => Files.createDirectories((checkout / path).toJava))
      if schemas then
        val directory = checkout / "website" / "static" / "schemas"
        Files.createDirectories(directory.toJava)
        if yaml then
          Files.writeString((directory / "morphir-ir-v4.yaml").toJava, "type: object\n")
          Files.writeString((directory / "morphir-ir-v4.json").toJava, "{\n  \"type\": \"object\"\n}\n")
      checkout
    }

  def unexpected(request: ProcessRequest): ProcessResult =
    ProcessResult(request, 99, "", s"unexpected: ${request.argv.mkString(" ")}")

  def syncRunner(
      root: Path,
      sparse: Boolean = false,
      dirty: String = "",
      statusJson: String = "{\"summary\":{\"clean\":1}}\n",
      pullExit: Int = 0,
      checkExit: Int = 0,
      headExit: Int = 0,
      headOutput: String = head + "\n",
      headError: String = "",
      sparseExit: Int = 0,
      sparseError: String = "",
      dirtyExit: Int = 0,
      dirtyError: String = "",
      statusExit: Int = 0,
      checkOutput: String = "{\"findings\":[]}",
      checkError: Option[String] = None
  ): RuleRunner =
    val checkout = root / SquireSpec.CheckoutRel
    RuleRunner { request =>
      val argv = request.argv
      if argv == revParse(checkout) then ProcessResult(request, headExit, headOutput, headError)
      else if argv == sparseConfig(checkout) then
        val output =
          if sparseExit == 1 && !sparse && sparseError.isEmpty then "" else if sparse then "true\n" else "false\n"
        ProcessResult(request, sparseExit, output, sparseError)
      else if argv == dirtyStatus(checkout) then ProcessResult(request, dirtyExit, dirty, dirtyError)
      else if argv.take(6) == Chunk("git", "-C", checkout.toString, "fetch", "--depth", "1") then ok(request)
      else if argv == Chunk("git", "-C", checkout.toString, "checkout", "--detach", "FETCH_HEAD") then ok(request)
      else if argv == kb(root, "sync", "status", "--json") then ProcessResult(request, statusExit, statusJson, "")
      else if argv.take(3) == kb(root, "sync", "pull").take(3) then
        ProcessResult(request, pullExit, "{\"actions\":[]}", if pullExit == 0 then "" else "pull failed")
      else if argv.take(2) == kb(root, "check").take(2) then
        ProcessResult(
          request,
          checkExit,
          checkOutput,
          checkError.getOrElse(if checkExit == 0 then "" else "check failed")
        )
      else unexpected(request)
    }

  def exportRunner(
      root: Path,
      checkout: Path,
      written: List[String] = Nil,
      createBranchExit: Int = 0,
      reuseBranchExit: Int = 0,
      validatorFailure: Option[String] = None,
      yamlUnsupported: Boolean = false,
      changed: String = "",
      gitCheckoutExit: Int = 0,
      pushExit: Int = 0,
      pushStdout: Option[String] = None,
      statusExit: Int = 0
  ): RuleRunner =
    RuleRunner(
      exportResponse(
        root,
        checkout,
        written,
        createBranchExit,
        reuseBranchExit,
        validatorFailure,
        yamlUnsupported,
        changed,
        gitCheckoutExit,
        pushExit,
        pushStdout,
        statusExit
      )
    )

  def exportResponse(
      root: Path,
      checkout: Path,
      written: List[String] = Nil,
      createBranchExit: Int = 0,
      reuseBranchExit: Int = 0,
      validatorFailure: Option[String] = None,
      yamlUnsupported: Boolean = false,
      changed: String = "",
      gitCheckoutExit: Int = 0,
      pushExit: Int = 0,
      pushStdout: Option[String] = None,
      statusExit: Int = 0
  ): ProcessRequest => ProcessResult =
    request =>
      val argv = request.argv
      if argv == gitCheckoutProbe(checkout) then
        ProcessResult(request, gitCheckoutExit, if gitCheckoutExit == 0 then "true\n" else "", "not a work tree")
      else if argv.take(3) == kb(root, "sync", "push").take(3) then
        val actions = written.map(path => s"{\"verb\":\"wrote\",\"path\":\"$path\"}").mkString(",")
        ProcessResult(
          request,
          pushExit,
          pushStdout.getOrElse(s"{\"actions\":[$actions]}\n"),
          if pushExit == 0 then "" else "push failed"
        )
      else if argv == Chunk("git", "-C", checkout.toString, "switch", "-c", SquireSpec.DefaultExportBranch) then
        ProcessResult(request, createBranchExit, "", if createBranchExit == 0 then "" else "already exists")
      else if argv == Chunk("git", "-C", checkout.toString, "switch", SquireSpec.DefaultExportBranch) then
        ProcessResult(request, reuseBranchExit, "", if reuseBranchExit == 0 then "" else "cannot switch")
      else if argv == Chunk("git", "-C", checkout.toString, "status", "--short") then
        ProcessResult(request, statusExit, changed, if statusExit == 0 then "" else "status failed")
      else if argv.headOption.contains("jsonschema") then
        val operation = argv.lift(1).getOrElse("")
        if yamlUnsupported && operation == "fmt" then ProcessResult(request, 1, "", "does not support YAML")
        else if validatorFailure.contains(operation) then ProcessResult(request, 1, "", s"$operation failed")
        else ok(request)
      else unexpected(request)

  def kb(root: Path, args: String*): Chunk[String] =
    Chunk((root / ".claude" / "skills" / "kb" / "kb").toString) ++ Chunk.from(args)

  def revParse(checkout: Path): Chunk[String]     = Chunk("git", "-C", checkout.toString, "rev-parse", "HEAD")
  def sparseConfig(checkout: Path): Chunk[String] =
    Chunk("git", "-C", checkout.toString, "config", "--get", "core.sparseCheckout")
  def dirtyStatus(checkout: Path): Chunk[String]      = Chunk("git", "-C", checkout.toString, "status", "--porcelain")
  def gitCheckoutProbe(checkout: Path): Chunk[String] =
    Chunk("git", "-C", checkout.toString, "rev-parse", "--is-inside-work-tree")

  def ok(request: ProcessRequest, stdout: String = ""): ProcessResult = ProcessResult(request, 0, stdout, "")

  def recordHas(value: Structure.Value, field: String): Boolean = value match
    case Structure.Value.Record(fields) => fields.exists(_._1 == field)
    case _                              => false

  def safe(runner: SpecRecordedRunner): Boolean =
    !runner.requests.exists { request =>
      request.argv.headOption.contains("git") &&
      request.argv.exists(argument => argument == "commit" || argument == "push")
    }

final class BranchRecordingRunner(responses: Map[Chunk[String], ProcessResult]) extends ProcessRunner:
  var requests: Chunk[ProcessRequest] = Chunk.empty

  def run(request: ProcessRequest): ProcessResult < (Async & Abort[SquireError]) =
    requests = requests.append(request)
    responses(request.argv).copy(request = request)

object BranchRecordingRunner:
  def apply(responses: Map[Chunk[String], ProcessResult]): BranchRecordingRunner =
    new BranchRecordingRunner(responses)

final class LocalGitBranchRunner(cwd: Path, responses: Map[Chunk[String], String]) extends ProcessRunner:
  var requests: Chunk[ProcessRequest] = Chunk.empty

  def run(request: ProcessRequest): ProcessResult < (Async & Abort[SquireError]) =
    requests = requests.append(request)
    if request.argv.headOption.contains("git") then
      LiveProcessRunner.run(request.copy(cwd = Present(cwd))).map(_.copy(request = request))
    else
      ProcessResult(request, 0, responses(request.argv), "")

object SquireBranchFixtures:
  val sourceSha: String = "1" * 40
  val targetSha: String = "2" * 40
  val mergeSha: String  = "3" * 40

  val checkTarget: Chunk[String] = Chunk("git", "check-ref-format", "--branch", "develop")
  val fetch: Chunk[String]       = Chunk(
    "git",
    "fetch",
    "--prune",
    "origin",
    "+refs/heads/main:refs/remotes/origin/main",
    "+refs/heads/develop:refs/remotes/origin/develop"
  )
  val resolveSource: Chunk[String] = Chunk("git", "rev-parse", "refs/remotes/origin/main")
  val resolveTarget: Chunk[String] = Chunk("git", "rev-parse", "refs/remotes/origin/develop")
  val repoView: Chunk[String]      = Chunk(
    "gh",
    "repo",
    "view",
    "--json",
    "nameWithOwner",
    "--jq",
    ".nameWithOwner"
  )
  val prList: Chunk[String] = Chunk(
    "gh",
    "pr",
    "list",
    "--repo",
    "finos/morphir-scala",
    "--base",
    "main",
    "--head",
    "develop",
    "--state",
    "merged",
    "--limit",
    "100",
    "--json",
    "number,headRefOid,mergeCommit,url,mergedAt"
  )
  val ancestor: Chunk[String] =
    Chunk("git", "merge-base", "--is-ancestor", mergeSha, "refs/remotes/origin/main")
  val push: Chunk[String] = Chunk(
    "git",
    "push",
    s"--force-with-lease=refs/heads/develop:$targetSha",
    "origin",
    "refs/remotes/origin/main:refs/heads/develop"
  )

  val matchingPullRequest: String =
    s"""{"number":42,"headRefOid":"$targetSha","mergeCommit":{"oid":"$mergeSha"},"url":"https://github.com/finos/morphir-scala/pull/42","mergedAt":"2026-08-07T12:00:00Z"}"""

  def ok(argv: Chunk[String], stdout: String = ""): ProcessResult =
    ProcessResult(ProcessRequest(argv), 0, stdout, "")

  def failed(argv: Chunk[String], detail: String): ProcessResult =
    ProcessResult(ProcessRequest(argv), 1, "", detail)

  val baseResponses: Map[Chunk[String], ProcessResult] = Map(
    checkTarget   -> ok(checkTarget, "develop\n"),
    fetch         -> ok(fetch),
    resolveSource -> ok(resolveSource, sourceSha + "\n"),
    resolveTarget -> ok(resolveTarget, targetSha + "\n")
  )

  def proofResponses(pullRequests: String): Map[Chunk[String], ProcessResult] =
    baseResponses ++ Map(
      repoView -> ok(repoView, "finos/morphir-scala\n"),
      prList   -> ok(prList, pullRequests)
    )

  val successfulProofResponses: Map[Chunk[String], ProcessResult] =
    proofResponses(s"[$matchingPullRequest]").updated(ancestor, ok(ancestor))

  def neverPushed(runner: BranchRecordingRunner): Boolean =
    !runner.requests.exists(_.argv.take(2) == Chunk("git", "push"))

  def failureContains[A](outcome: Result[SquireError, A], fragments: String*): Boolean =
    outcome match
      case Result.Failure(error) => fragments.forall(error.getMessage.contains)
      case Result.Success(_)     => false

  def git(cwd: Path, args: String*): String =
    val process =
      new ProcessBuilder((Seq("git") ++ args)*).directory(cwd.toJava.toFile).redirectErrorStream(true).start()
    val output = new String(process.getInputStream.readAllBytes(), StandardCharsets.UTF_8)
    val exit   = process.waitFor()
    if exit != 0 then throw new RuntimeException(s"git ${args.mkString(" ")} failed: ${output.trim}")
    output

final case class TestSquirePlatform(
    executable: Maybe[String] = Absent,
    instant: java.time.Instant = java.time.Instant.parse("2026-08-08T12:34:56Z")
) extends SquirePlatform:
  def findExecutable(name: String): Maybe[String] = executable
  def now: java.time.Instant                      = instant

class SquireCellarSpec extends Test[Any]:
  "settings" - {
    "use defaults when absent and decode Kyo YAML when present" in {
      for
        root <- SquireFixtures.scratch("cellar-settings")
        absent   = SquireCellar.loadSettings(root)
        settings = root / ".config" / "squire" / "settings.local.yaml"
        _ <- Sync.defer {
          Files.createDirectories(settings.parent.get.toJava)
          Files.writeString(
            settings.toJava,
            "cellar:\n  repositories:\n    - https://repo.example/maven\n  binary: /opt/cellar\n"
          )
        }
        loaded = SquireCellar.loadSettings(root)
      yield assert(
        absent == Result.Success(CellarSettings()) &&
          loaded == Result.Success(CellarSettings(List("https://repo.example/maven"), Some("/opt/cellar")))
      )
    }

    "report malformed Kyo YAML instead of silently discarding it" in {
      for
        root <- SquireFixtures.scratch("cellar-settings-invalid")
        settings = root / ".config" / "squire" / "settings.local.yaml"
        _ <- Sync.defer {
          Files.createDirectories(settings.parent.get.toJava)
          Files.writeString(settings.toJava, "cellar: [unterminated")
        }
      yield assert(SquireCellar.loadSettings(root).isFailure)
    }
  }

  "coordinates and execution" - {
    "expand repository flags and every legacy coordinate alias" in {
      val aliases = Map(
        "case-app:2.1.0" -> "com.github.alexarchambault:case-app_3:2.1.0",
        "kyo-case-app"   -> "io.getkyo:kyo-case-app_3:1.0.0-RC5",
        "kyo-schema"     -> "io.getkyo:kyo-schema_3:1.0.0-RC5",
        "kyo-zio"        -> "io.getkyo:kyo-zio_3:1.0.0-RC5",
        "zio:2.1.26"     -> "dev.zio:zio_3:2.1.26",
        "zio-cli"        -> "dev.zio:zio-cli_3:0.8.1",
        "mill-scalalib"  -> "com.lihaoyi:mill-scalalib_3:0.12.0",
        "scala3-library" -> "org.scala-lang:scala3-library_3:3.8.4"
      )
      assert(
        aliases.forall((alias, coordinate) => SquireCellar.resolveCoordinate(alias) == coordinate) &&
          SquireCellar.resolveCoordinate("group:artifact:1") == "group:artifact:1" &&
          SquireCellar.repositoryFlags(CellarSettings(List("one", "two"))) ==
          Chunk("--repository", "one", "--repository", "two")
      )
    }

    "prefer the configured binary then PATH and fail when neither exists" in
      assert(
        SquireCellar.executable(
          CellarSettings(binary = Some("/opt/cellar")),
          TestSquirePlatform(Present("/usr/bin/cellar"))
        ) ==
          Result.Success("/opt/cellar") &&
          SquireCellar.executable(CellarSettings(), TestSquirePlatform(Present("/usr/bin/cellar"))) ==
          Result.Success("/usr/bin/cellar") &&
          SquireCellar.executable(CellarSettings(), TestSquirePlatform()).isFailure
      )

    "build exact argv for get search and deps options" in {
      val settings = CellarSettings(List("https://repo.example/maven"))
      val flags    = Chunk("--repository", "https://repo.example/maven")
      assert(
        SquireCellar.command(
          CellarAction.Get(
            "kyo-case-app",
            "kyo.KyoCommand",
            hideInherited = true,
            groupInherited = true,
            limit = Some(20)
          ),
          settings,
          "/usr/bin/cellar"
        ) == ProcessRequest(
          Chunk("/usr/bin/cellar", "get-external") ++ flags ++
            Chunk(
              "io.getkyo:kyo-case-app_3:1.0.0-RC5",
              "kyo.KyoCommand",
              "--hide-inherited",
              "--group-inherited",
              "--limit",
              "20"
            )
        ) &&
          SquireCellar.command(CellarAction.Search("zio:2.1.26", "provide", Some(7)), settings, "cellar") ==
          ProcessRequest(Chunk("cellar", "search-external") ++ flags ++
            Chunk("dev.zio:zio_3:2.1.26", "provide", "--limit", "7")) &&
          SquireCellar.command(CellarAction.Deps("mill-scalalib"), CellarSettings(), "cellar") ==
          ProcessRequest(Chunk("cellar", "deps", "com.lihaoyi:mill-scalalib_3:0.12.0"))
      )
    }

    "validates an absolute writable temp directory and passes it to the native process" in {
      for
        root <- SquireFixtures.scratch("cellar-temp")
        relative = Path("relative-temp")
        missing  = root / "missing"
        valid           <- SquireCellar.validateTempDirectory(Some(root.toString))
        invalidRelative <- Abort.run[SquireError](SquireCellar.validateTempDirectory(Some(relative.toString)))
        invalidMissing  <- Abort.run[SquireError](SquireCellar.validateTempDirectory(Some(missing.toString)))
      yield assert(
        valid == Present(root) && invalidRelative.isFailure && invalidMissing.isFailure &&
          SquireCellar.command(CellarAction.Deps("mill-scalalib"), CellarSettings(), "cellar", valid) ==
          ProcessRequest(
            Chunk(
              "cellar",
              s"-Djava.io.tmpdir=$root",
              "deps",
              "com.lihaoyi:mill-scalalib_3:0.12.0"
            )
          )
      )
    }

    "preserve legacy zero and negative limit argv semantics" in
      assert(
        SquireCellar.command(CellarAction.Search("coord", "query", Some(0)), CellarSettings(), "cellar") ==
          ProcessRequest(Chunk("cellar", "search-external", "coord", "query")) &&
          SquireCellar.command(CellarAction.Search("coord", "query", Some(-2)), CellarSettings(), "cellar") ==
          ProcessRequest(Chunk("cellar", "search-external", "coord", "query", "--limit", "-2"))
      )

    "resolve bare executables with POSIX names and Windows PATHEXT" in {
      for
        root <- SquireFixtures.scratch("cellar-executable")
        posixBin   = root / "posix-bin"
        windowsBin = root / "windows-bin"
        _ <- Sync.defer {
          Files.createDirectories(posixBin.toJava)
          Files.createDirectories(windowsBin.toJava)
          Files.writeString((posixBin / "cellar").toJava, "probe")
          Files.writeString((windowsBin / "cellar.CMD").toJava, "probe")
        }
        posix = SquireExecutableLookup.find(
          "cellar",
          List(posixBin),
          windows = false,
          pathExtensions = Nil,
          candidate => Files.isRegularFile(candidate.toJava)
        )
        windows = SquireExecutableLookup.find(
          "cellar",
          List(windowsBin),
          windows = true,
          pathExtensions = List(".EXE", ".CMD", ".BAT"),
          candidate => Files.isRegularFile(candidate.toJava)
        )
      yield assert(posix == Present((posixBin / "cellar").toString) &&
        windows == Present((windowsBin / "cellar.CMD").toString))
    }
  }

class SquireRepoSpec extends Test[Any]:
  "naming and clone requests" - {
    "extract names and organizations from HTTPS SSH and local sources" in {
      for
        root <- SquireFixtures.scratch("repo-names")
        local = root / "finos" / "morphir-scala"
        _ <- Sync.defer(Files.createDirectories(local.toJava))
      yield assert(
        SquireRepo.nameFrom("https://github.com/finos/morphir.git") == "morphir" &&
          SquireRepo.orgFrom("https://github.com/finos/morphir.git") == Some("finos") &&
          SquireRepo.nameFrom("git@github.com:com-lihaoyi/mill.git") == "mill" &&
          SquireRepo.orgFrom("git@github.com:com-lihaoyi/mill.git") == Some("com-lihaoyi") &&
          SquireRepo.nameFrom(local.toString) == "morphir-scala" &&
          SquireRepo.orgFrom(local.toString) == Some("finos")
      )
    }

    "reject duplicate names depth/full combinations and sparse non-clones" in {
      val manifest = ReferenceManifest(List(SquireRepoFixtures.repo("mill")))
      assert(
        SquireRepo.validate(ReferenceAdd("https://github.com/other/mill"), manifest).isFailure &&
          SquireRepo.validate(
            ReferenceAdd("https://github.com/finos/morphir", depth = Some(2), full = true),
            ReferenceManifest()
          ).isFailure &&
          SquireRepo.validate(
            ReferenceAdd("/tmp/morphir", strategy = "symlink", sparse = List("docs")),
            ReferenceManifest()
          ).isFailure &&
          SquireRepo.validate(
            ReferenceAdd("/tmp/morphir", strategy = "worktree", sparse = List("docs")),
            ReferenceManifest()
          ).isFailure
      )
    }

    "reject empty dot separator and normalization-alias path components" in {
      val invalidNames = List("", ".", "..", "nested/repo", "nested\\repo", "one/../repo")
      assert(
        invalidNames.forall(name =>
          SquireRepo.validate(
            ReferenceAdd("https://github.com/finos/morphir", name = Some(name)),
            ReferenceManifest()
          ).isFailure
        ) &&
          SquireRepo.validate(ReferenceAdd("https://github.com/../morphir"), ReferenceManifest()).isFailure
      )
    }

    "prefer gh only for authenticated GitHub clones and preserve exact Git fallback argv" in {
      val options       = ReferenceAdd("https://github.com/finos/morphir", ref = Some("main"), depth = Some(3))
      val dest          = Path("/repo/.refs/finos/morphir")
      val authenticated = ProcessResult(ProcessRequest(Chunk("gh", "auth", "status")), 1, "", "✓ Logged in")
      val rejected      = authenticated.copy(stdout = "", stderr = "not logged in")
      assert(
        SquireRepo.ghAuthenticated(authenticated) && !SquireRepo.ghAuthenticated(rejected) &&
          SquireRepo.cloneRequest(options, dest, useGh = true) == ProcessRequest(
            Chunk(
              "gh",
              "repo",
              "clone",
              options.urlOrPath,
              dest.toString,
              "--",
              "--depth",
              "3",
              "--branch",
              "main",
              "--single-branch"
            )
          ) &&
          SquireRepo.cloneRequest(options, dest, useGh = false) == ProcessRequest(
            Chunk(
              "git",
              "clone",
              "--depth",
              "3",
              "--branch",
              "main",
              "--single-branch",
              options.urlOrPath,
              dest.toString
            )
          )
      )
    }

    "compose sparse clone and checkout argv" in {
      val options =
        ReferenceAdd("https://github.com/finos/morphir", full = true, sparse = List("docs", "website", "wit"))
      val dest = Path("/repo/.refs/finos/morphir")
      assert(
        SquireRepo.cloneRequest(options, dest, useGh = false) == ProcessRequest(
          Chunk("git", "clone", "--filter=blob:none", "--sparse", options.urlOrPath, dest.toString)
        ) &&
          SquireRepo.sparseRequest(dest, options.sparse) == ProcessRequest(
            Chunk("git", "-C", dest.toString, "sparse-checkout", "set", "docs", "website", "wit")
          )
      )
    }

    "preserve legacy zero and negative clone-depth argv semantics" in {
      val dest = Path("/repo/.refs/finos/morphir")
      assert(
        SquireRepo.cloneRequest(ReferenceAdd("url", depth = Some(0)), dest, useGh = false) ==
          ProcessRequest(Chunk("git", "clone", "--depth", "1", "url", dest.toString)) &&
          SquireRepo.cloneRequest(ReferenceAdd("url", depth = Some(-2)), dest, useGh = false) ==
          ProcessRequest(Chunk("git", "clone", "--depth", "-2", "url", dest.toString))
      )
    }
  }

  "control root and manifest" - {
    "reject a symlinked control root throughout the read lifecycle" in {
      for
        root <- SquireFixtures.scratch("repo-control-root-read")
        refs     = root / ".refs"
        external = root / "external-refs"
        entry    = SquireRepoFixtures.repo("mill")
        original = SquireJson.encode(ReferenceManifest(List(entry))) + "\n"
        _ <- Sync.defer {
          Files.createDirectories(external.toJava)
          Files.writeString((external / "manifest.json").toJava, original)
          Files.createSymbolicLink(refs.toJava, external.toJava)
        }
        runner = RuleRunner(SquireRepoFixtures.ok)
        loaded   <- Abort.run[SquireError](SquireRepo.loadManifest(root))
        listed   <- Abort.run[SquireError](SquireRepo.list(root, asJson = false, runner))
        detailed <- Abort.run[SquireError](SquireRepo.status(root, None, runner))
        removed  <- Abort.run[SquireError](
          SquireRepo.remove("mill", keepFiles = false, root, runner, TestSquirePlatform())
        )
        after <- Sync.defer(Files.readString((external / "manifest.json").toJava))
      yield assert(
        loaded.isFailure && listed.isFailure && detailed.isFailure && removed.isFailure &&
          runner.requests.isEmpty && after == original && Files.isSymbolicLink(refs.toJava)
      )
    }

    "reject a symlinked control root before write lifecycle effects" in {
      for
        saveRoot <- SquireFixtures.scratch("repo-control-root-save")
        saveRefs     = saveRoot / ".refs"
        saveExternal = saveRoot / "external-refs"
        original     = "{\"repos\":[]}\n"
        _ <- Sync.defer {
          Files.createDirectories(saveExternal.toJava)
          Files.writeString((saveExternal / "manifest.json").toJava, original)
          Files.createSymbolicLink(saveRefs.toJava, saveExternal.toJava)
        }
        saved <- Abort.run[SquireError](
          SquireRepo.saveManifest(saveRoot, ReferenceManifest(List(SquireRepoFixtures.repo("mill"))))
        )
        savedAfter <- Sync.defer(Files.readString((saveExternal / "manifest.json").toJava))
        addRoot    <- SquireFixtures.scratch("repo-control-root-add")
        addRefs     = addRoot / ".refs"
        addExternal = addRoot / "external-refs"
        _ <- Sync.defer {
          Files.createDirectories(addExternal.toJava)
          Files.writeString((addExternal / "manifest.json").toJava, original)
          Files.createSymbolicLink(addRefs.toJava, addExternal.toJava)
        }
        runner = RuleRunner(SquireRepoFixtures.cloneResponse)
        added <- Abort.run[SquireError](
          SquireRepo.add(
            ReferenceAdd("https://github.com/finos/morphir"),
            addRoot,
            runner,
            TestSquirePlatform()
          )
        )
        addedAfter <- Sync.defer(Files.readString((addExternal / "manifest.json").toJava))
      yield assert(
        saved.isFailure && added.isFailure && savedAfter == original && addedAfter == original &&
          runner.requests.isEmpty && !Files.exists(
            (addExternal / "finos").toJava,
            java.nio.file.LinkOption.NOFOLLOW_LINKS
          )
      )
    }

    "reject a final manifest symlink throughout the read lifecycle" in {
      for
        root <- SquireFixtures.scratch("repo-manifest-link-read")
        refs     = root / ".refs"
        external = root / "external-manifest.json"
        entry    = SquireRepoFixtures.repo("mill")
        original = SquireJson.encode(ReferenceManifest(List(entry))) + "\n"
        _ <- Sync.defer {
          Files.createDirectories(refs.toJava)
          Files.writeString(external.toJava, original)
          Files.createSymbolicLink((refs / "manifest.json").toJava, external.toJava)
        }
        runner = RuleRunner(SquireRepoFixtures.ok)
        loaded   <- Abort.run[SquireError](SquireRepo.loadManifest(root))
        listed   <- Abort.run[SquireError](SquireRepo.list(root, asJson = false, runner))
        detailed <- Abort.run[SquireError](SquireRepo.status(root, None, runner))
        removed  <- Abort.run[SquireError](
          SquireRepo.remove("mill", keepFiles = false, root, runner, TestSquirePlatform())
        )
        after <- Sync.defer(Files.readString(external.toJava))
      yield assert(
        loaded.isFailure && listed.isFailure && detailed.isFailure && removed.isFailure &&
          runner.requests.isEmpty && after == original && Files.isSymbolicLink((refs / "manifest.json").toJava)
      )
    }

    "reject a final manifest symlink before write lifecycle effects" in {
      for
        saveRoot <- SquireFixtures.scratch("repo-manifest-link-save")
        saveRefs     = saveRoot / ".refs"
        saveExternal = saveRoot / "external-manifest.json"
        original     = "{\"repos\":[]}\n"
        _ <- Sync.defer {
          Files.createDirectories(saveRefs.toJava)
          Files.writeString(saveExternal.toJava, original)
          Files.createSymbolicLink((saveRefs / "manifest.json").toJava, saveExternal.toJava)
        }
        saved <- Abort.run[SquireError](
          SquireRepo.saveManifest(saveRoot, ReferenceManifest(List(SquireRepoFixtures.repo("mill"))))
        )
        savedAfter <- Sync.defer(Files.readString(saveExternal.toJava))
        addRoot    <- SquireFixtures.scratch("repo-manifest-link-add")
        addRefs     = addRoot / ".refs"
        addExternal = addRoot / "external-manifest.json"
        _ <- Sync.defer {
          Files.createDirectories(addRefs.toJava)
          Files.writeString(addExternal.toJava, original)
          Files.createSymbolicLink((addRefs / "manifest.json").toJava, addExternal.toJava)
        }
        runner = RuleRunner(SquireRepoFixtures.cloneResponse)
        added <- Abort.run[SquireError](
          SquireRepo.add(
            ReferenceAdd("https://github.com/finos/morphir"),
            addRoot,
            runner,
            TestSquirePlatform()
          )
        )
        addedAfter <- Sync.defer(Files.readString(addExternal.toJava))
      yield assert(
        saved.isFailure && added.isFailure && savedAfter == original && addedAfter == original &&
          runner.requests.isEmpty && !Files.exists((addRefs / "finos").toJava, java.nio.file.LinkOption.NOFOLLOW_LINKS)
      )
    }
  }

  "add and manifest" - {
    "reject an existing destination before invoking clone" in {
      for
        root <- SquireFixtures.scratch("repo-add-collision")
        dest = root / ".refs" / "finos" / "morphir"
        _ <- Sync.defer(Files.createDirectories(dest.toJava))
        runner = RuleRunner(SquireRepoFixtures.ok)
        result <- Abort.run[SquireError](
          SquireRepo.add(
            ReferenceAdd("https://github.com/finos/morphir"),
            root,
            runner,
            TestSquirePlatform()
          )
        )
      yield assert(result.isFailure && runner.requests.isEmpty && Files.isDirectory(dest.toJava))
    }

    "record clone metadata and round-trip it through Kyo Schema JSON" in {
      for
        root <- SquireFixtures.scratch("repo-clone")
        runner = RuleRunner(SquireRepoFixtures.cloneResponse)
        added <- SquireRepo.add(
          ReferenceAdd("https://github.com/finos/morphir", ref = Some("main"), sparse = List("docs", "wit")),
          root,
          runner,
          TestSquirePlatform()
        )
        loaded <- SquireRepo.loadManifest(root)
      yield assert(
        added.name == "morphir" && added.org == Some("finos") && added.path == "finos/morphir" &&
          added.strategy == "clone" && added.url == Some("https://github.com/finos/morphir") &&
          added.ref == Some("main") && added.commit == Some("abc123") && added.depth == Some(1) &&
          added.sparse == List("docs", "wit") && added.added == "2026-08-08T12:34:56Z" &&
          loaded == ReferenceManifest(List(added)) &&
          runner.requests.exists(_.argv == Chunk(
            "git",
            "-C",
            (root / ".refs" / "finos" / "morphir").toString,
            "sparse-checkout",
            "set",
            "docs",
            "wit"
          ))
      )
    }

    "create local symlinks and worktrees with their exact metadata commands" in {
      for
        root <- SquireFixtures.scratch("repo-local")
        source = root / "sources" / "finos" / "morphir"
        _ <- Sync.defer(Files.createDirectories(source.toJava))
        symlinkRunner = RuleRunner(SquireRepoFixtures.localMetadataResponse)
        linked <-
          SquireRepo.add(ReferenceAdd(source.toString, strategy = "symlink"), root, symlinkRunner, TestSquirePlatform())
        worktreeRunner = RuleRunner(SquireRepoFixtures.localMetadataResponse)
        worktree <- SquireRepo.add(
          ReferenceAdd(source.toString, name = Some("morphir-v4"), ref = Some("v4"), strategy = "worktree"),
          root,
          worktreeRunner,
          TestSquirePlatform()
        )
        linkPath     = root / ".refs" / linked.path
        worktreePath = root / ".refs" / worktree.path
      yield assert(
        Files.isSymbolicLink(linkPath.toJava) &&
          Files.readSymbolicLink(linkPath.toJava) == source.toJava.toRealPath() &&
          linked.source == Some(source.toJava.toRealPath().toString) && linked.ref == Some("main") &&
          linked.commit == Some("abc123") &&
          worktree.path == "finos/.worktrees/morphir/morphir-v4" &&
          worktreeRunner.requests.head.argv ==
          Chunk(
            "git",
            "-C",
            source.toJava.toRealPath().toString,
            "worktree",
            "add",
            worktreePath.toJava.toAbsolutePath.normalize.toString,
            "v4"
          )
      )
    }
  }

  "list and status" - {
    "emit manifest JSON and report missing dirty drift and sparse state with filtered exits" in {
      for
        root <- SquireFixtures.scratch("repo-status")
        refs = root / ".refs"
        _ <- Sync.defer {
          Files.createDirectories((refs / "finos" / "dirty").toJava)
          Files.createDirectories((refs / "finos" / "drift").toJava)
          Files.createDirectories((refs / "finos" / "sparse").toJava)
        }
        manifest = ReferenceManifest(
          List(
            SquireRepoFixtures.repo("missing", commit = Some("abc123")),
            SquireRepoFixtures.repo("dirty", commit = Some("abc123")),
            SquireRepoFixtures.repo("drift", commit = Some("old123")),
            SquireRepoFixtures.repo("sparse", commit = Some("abc123"), sparse = List("docs", "wit"))
          )
        )
        _ <- SquireRepo.saveManifest(root, manifest)
        runner = RuleRunner(SquireRepoFixtures.statusResponse)
        json    <- SquireRepo.list(root, asJson = true, runner)
        all     <- SquireRepo.status(root, None, runner)
        sparse  <- SquireRepo.status(root, Some("sparse"), runner)
        missing <- SquireRepo.status(root, Some("missing"), runner)
        unknown <- SquireRepo.status(root, Some("unknown"), runner)
      yield assert(
        SquireJson.decode[ReferenceManifest](json.trim) == Result.Success(manifest) &&
          all.exitCode == 1 && all.output.contains("MISSING") && all.output.contains("DIRTY") &&
          all.output.contains("DRIFT") &&
          all.output.contains("sparse:   docs wit") &&
          sparse.exitCode == 0 && sparse.output.contains("in sync with manifest") &&
          missing.exitCode == 1 && unknown.exitCode == 1 && unknown.output.contains("not in manifest")
      )
    }

    "share legacy disk labels and make non-Git and failed Git probes unhealthy" in {
      for
        root <- SquireFixtures.scratch("repo-status-labels")
        refs   = root / ".refs"
        target = root / "target-repo"
        _ <- Sync.defer {
          List("ok", "modified", "nongit", "git-error").foreach(name =>
            Files.createDirectories((refs / "finos" / name).toJava)
          )
          Files.createDirectories(target.toJava)
          Files.createSymbolicLink((refs / "finos" / "linked").toJava, target.toJava)
          Files.createSymbolicLink((refs / "finos" / "broken").toJava, (root / "absent-target").toJava)
        }
        manifest = ReferenceManifest(List(
          SquireRepoFixtures.repo("ok", sparse = List("docs")),
          SquireRepoFixtures.repo("modified", commit = Some("old123")),
          SquireRepoFixtures.repo("nongit"),
          SquireRepoFixtures.repo("git-error"),
          SquireRepoFixtures.repo("linked", strategy = "symlink", source = Some(target.toString)),
          SquireRepoFixtures.repo("broken", strategy = "symlink", source = Some((root / "absent-target").toString)),
          SquireRepoFixtures.repo("missing")
        ))
        _ <- SquireRepo.saveManifest(root, manifest)
        runner = RuleRunner(SquireRepoFixtures.legacyStatusResponse)
        listed   <- SquireRepo.list(root, asJson = false, runner)
        detailed <- SquireRepo.status(root, None, runner)
      yield assert(
        listed.contains("OK (abc123) [sparse]") &&
          listed.contains("MODIFIED (was old123, now new123)") &&
          listed.contains("DIR_NO_GIT") && listed.contains("GIT_ERROR") &&
          listed.contains("symlink →") && listed.contains("BROKEN_SYMLINK") && listed.contains("MISSING") &&
          detailed.exitCode == 1 && detailed.output.contains("DIR_NO_GIT") && detailed.output.contains("GIT_ERROR")
      )
    }

    "reject escaped and intermediate-link manifest paths before filesystem or Git inspection" in {
      for
        root <- SquireFixtures.scratch("repo-status-path-policy")
        refs    = root / ".refs"
        outside = root / "outside"
        _ <- Sync.defer {
          Files.createDirectories(refs.toJava)
          Files.createDirectories(outside.toJava)
          Files.createSymbolicLink((refs / "redirect").toJava, outside.toJava)
        }
        manifest = ReferenceManifest(List(
          SquireRepoFixtures.repo("lexical", path = "../outside"),
          SquireRepoFixtures.repo("redirected", path = "redirect/repo")
        ))
        _ <- SquireRepo.saveManifest(root, manifest)
        runner = RuleRunner(SquireRepoFixtures.ok)
        report <- SquireRepo.status(root, None, runner)
        listed <- SquireRepo.list(root, asJson = false, runner)
      yield assert(report.exitCode == 1 && report.output.contains("INVALID_PATH") && listed.contains("INVALID_PATH") &&
        runner.requests.isEmpty)
    }
  }

  "remove" - {
    "keep files on request and safely delete clones with empty-parent pruning" in {
      for
        root <- SquireFixtures.scratch("repo-remove-clone")
        dest = root / ".refs" / "finos" / "mill"
        _ <- Sync.defer {
          Files.createDirectories(dest.toJava)
          Files.writeString((dest / "README.md").toJava, "fixture")
        }
        entry = SquireRepoFixtures.repo("mill")
        _ <- SquireRepo.saveManifest(root, ReferenceManifest(List(entry)))
        _ <- SquireRepo.remove("mill", keepFiles = true, root, RuleRunner(SquireRepoFixtures.ok), TestSquirePlatform())
        kept <- SquirePaths.exists(dest)
        _    <- SquireRepo.saveManifest(root, ReferenceManifest(List(entry)))
        _ <- SquireRepo.remove("mill", keepFiles = false, root, RuleRunner(SquireRepoFixtures.ok), TestSquirePlatform())
        deleted <- SquirePaths.exists(dest)
        parent  <- SquirePaths.exists(root / ".refs" / "finos")
      yield assert(kept && !deleted && !parent)
    }

    "keep-files preserves empty checkout parents and reject symlink paths lexically outside refs" in {
      for
        root <- SquireFixtures.scratch("repo-remove-lexical-escape")
        emptyParent = root / ".refs" / "finos"
        outside     = root / "outside"
        outsideLink = root / "outside-link"
        _ <- Sync.defer {
          Files.createDirectories(emptyParent.toJava)
          Files.createDirectories(outside.toJava)
          Files.createSymbolicLink(outsideLink.toJava, outside.toJava)
        }
        keptEntry = SquireRepoFixtures.repo("missing")
        _ <- SquireRepo.saveManifest(root, ReferenceManifest(List(keptEntry)))
        _ <-
          SquireRepo.remove("missing", keepFiles = true, root, RuleRunner(SquireRepoFixtures.ok), TestSquirePlatform())
        emptyParentKept <- SquirePaths.exists(emptyParent)
        escapedEntry = SquireRepoFixtures.repo(
          "outside-link",
          path = "../outside-link",
          strategy = "symlink",
          source = Some(outside.toString)
        )
        _       <- SquireRepo.saveManifest(root, ReferenceManifest(List(escapedEntry)))
        escaped <- Abort.run[SquireError](
          SquireRepo.remove(
            "outside-link",
            keepFiles = false,
            root,
            RuleRunner(SquireRepoFixtures.ok),
            TestSquirePlatform()
          )
        )
      yield assert(emptyParentKept && escaped.isFailure && Files.isSymbolicLink(outsideLink.toJava))
    }

    "unlink escaped symlinks without deleting targets and reject escaped recursive deletion" in {
      for
        root <- SquireFixtures.scratch("repo-remove-escape")
        outside = root / "outside"
        link    = root / ".refs" / "finos" / "link"
        _ <- Sync.defer {
          Files.createDirectories(link.parent.get.toJava)
          Files.createDirectories(outside.toJava)
          Files.writeString((outside / "keep.txt").toJava, "keep")
          Files.createSymbolicLink(link.toJava, outside.toJava)
        }
        symlinkEntry = SquireRepoFixtures.repo("link", strategy = "symlink", source = Some(outside.toString))
        _ <- SquireRepo.saveManifest(root, ReferenceManifest(List(symlinkEntry)))
        _ <- SquireRepo.remove("link", keepFiles = false, root, RuleRunner(SquireRepoFixtures.ok), TestSquirePlatform())
        targetKept <- SquirePaths.exists(outside / "keep.txt")
        linkGone   <- Path.runReadOnly(link.exists(followLinks = false)).map(!_)
        _          <- Sync.defer {
          Files.createDirectories(link.parent.get.toJava)
          Files.createSymbolicLink(link.toJava, outside.toJava)
        }
        cloneEntry = SquireRepoFixtures.repo("link")
        _       <- SquireRepo.saveManifest(root, ReferenceManifest(List(cloneEntry)))
        escaped <- Abort.run[SquireError](SquireRepo.remove(
          "link",
          keepFiles = false,
          root,
          RuleRunner(SquireRepoFixtures.ok),
          TestSquirePlatform()
        ))
        targetStillKept <- SquirePaths.exists(outside / "keep.txt")
      yield assert(targetKept && linkGone && escaped.isFailure && targetStillKept && Files.isSymbolicLink(link.toJava))
    }

    "reject intermediate redirects and final clone aliases without touching their targets" in {
      for
        root <- SquireFixtures.scratch("repo-remove-aliases")
        refs       = root / ".refs"
        outside    = root / "outside"
        outsideDir = root / "outside-dir"
        targetRepo = refs / "target-repo"
        _ <- Sync.defer {
          Files.createDirectories(refs.toJava)
          Files.createDirectories(outside.toJava)
          Files.createDirectories(outsideDir.toJava)
          Files.createDirectories(targetRepo.toJava)
          Files.writeString((targetRepo / "keep.txt").toJava, "keep")
          Files.createSymbolicLink((refs / "redirect").toJava, outsideDir.toJava)
          Files.createSymbolicLink((outsideDir / "victim").toJava, outside.toJava)
        }
        redirected = SquireRepoFixtures.repo(
          "evil",
          path = "redirect/victim",
          strategy = "symlink",
          source = Some(outside.toString)
        )
        _                <- SquireRepo.saveManifest(root, ReferenceManifest(List(redirected)))
        redirectedResult <- Abort.run[SquireError](
          SquireRepo.remove("evil", keepFiles = false, root, RuleRunner(SquireRepoFixtures.ok), TestSquirePlatform())
        )
        victimKept = Files.isSymbolicLink((outsideDir / "victim").toJava)
        _ <- Sync.defer(Files.createSymbolicLink((refs / "alias").toJava, targetRepo.toJava))
        alias = SquireRepoFixtures.repo("alias", path = "alias")
        _           <- SquireRepo.saveManifest(root, ReferenceManifest(List(alias)))
        aliasResult <- Abort.run[SquireError](
          SquireRepo.remove("alias", keepFiles = false, root, RuleRunner(SquireRepoFixtures.ok), TestSquirePlatform())
        )
      yield assert(
        redirectedResult.isFailure && victimKept && aliasResult.isFailure &&
          Files.isSymbolicLink((refs / "alias").toJava) && Files.exists((targetRepo / "keep.txt").toJava)
      )
    }

    "handle dangling final links by strategy and reject dangling intermediate links" in {
      for
        root <- SquireFixtures.scratch("repo-remove-dangling")
        refs = root / ".refs"
        _ <- Sync.defer {
          Files.createDirectories(refs.toJava)
          Files.createSymbolicLink((refs / "dangling-link").toJava, (root / "missing-target").toJava)
        }
        symlinkEntry = SquireRepoFixtures.repo(
          "dangling-link",
          path = "dangling-link",
          strategy = "symlink",
          source = Some((root / "missing-target").toString)
        )
        _ <- SquireRepo.saveManifest(root, ReferenceManifest(List(symlinkEntry)))
        _ <- SquireRepo.remove(
          "dangling-link",
          keepFiles = false,
          root,
          RuleRunner(SquireRepoFixtures.ok),
          TestSquirePlatform()
        )
        symlinkRemoved = !Files.exists((refs / "dangling-link").toJava, java.nio.file.LinkOption.NOFOLLOW_LINKS)
        _ <- Sync.defer {
          Files.createSymbolicLink((refs / "dangling-clone").toJava, (root / "missing-target").toJava)
          Files.createSymbolicLink((refs / "dangling-parent").toJava, (root / "missing-parent").toJava)
        }
        cloneEntry = SquireRepoFixtures.repo("dangling-clone", path = "dangling-clone")
        _           <- SquireRepo.saveManifest(root, ReferenceManifest(List(cloneEntry)))
        cloneResult <- Abort.run[SquireError](
          SquireRepo.remove(
            "dangling-clone",
            keepFiles = false,
            root,
            RuleRunner(SquireRepoFixtures.ok),
            TestSquirePlatform()
          )
        )
        intermediateEntry = SquireRepoFixtures.repo(
          "nested",
          path = "dangling-parent/nested",
          strategy = "symlink",
          source = Some("unused")
        )
        _                  <- SquireRepo.saveManifest(root, ReferenceManifest(List(intermediateEntry)))
        intermediateResult <- Abort.run[SquireError](
          SquireRepo.remove("nested", keepFiles = false, root, RuleRunner(SquireRepoFixtures.ok), TestSquirePlatform())
        )
      yield assert(
        symlinkRemoved && cloneResult.isFailure && Files.isSymbolicLink((refs / "dangling-clone").toJava) &&
          intermediateResult.isFailure && Files.isSymbolicLink((refs / "dangling-parent").toJava)
      )
    }

    "delete the exact in-base worktree directory when its source is missing" in {
      for
        root <- SquireFixtures.scratch("repo-remove-worktree-fallback")
        dest   = root / ".refs" / "finos" / ".worktrees" / "morphir" / "v4"
        source = root / "missing-source"
        _ <- Sync.defer {
          Files.createDirectories(dest.toJava)
          Files.writeString((dest / "marker").toJava, "fixture")
        }
        entry = SquireRepoFixtures.repo(
          "v4",
          path = "finos/.worktrees/morphir/v4",
          strategy = "worktree",
          source = Some(source.toString),
          ref = Some("v4")
        )
        _ <- SquireRepo.saveManifest(root, ReferenceManifest(List(entry)))
        runner = RuleRunner(SquireRepoFixtures.ok)
        _ <- SquireRepo.remove("v4", keepFiles = false, root, runner, TestSquirePlatform())
      yield assert(!Files.exists(dest.toJava, java.nio.file.LinkOption.NOFOLLOW_LINKS) && runner.requests.isEmpty)
    }

    "remove worktrees through Git and prune their empty hierarchy" in {
      for
        root <- SquireFixtures.scratch("repo-remove-worktree")
        source = root / "source" / "finos" / "morphir"
        dest   = root / ".refs" / "finos" / ".worktrees" / "morphir" / "v4"
        _ <- Sync.defer {
          Files.createDirectories(source.toJava)
          Files.createDirectories(dest.toJava)
        }
        entry = SquireRepoFixtures.repo(
          "v4",
          path = "finos/.worktrees/morphir/v4",
          strategy = "worktree",
          source = Some(source.toString),
          ref = Some("v4")
        )
        _ <- SquireRepo.saveManifest(root, ReferenceManifest(List(entry)))
        runner = RuleRunner { request =>
          if request.argv.contains("worktree") then Files.delete(dest.toJava)
          ProcessResult(request, 0, "", "")
        }
        _      <- SquireRepo.remove("v4", keepFiles = false, root, runner, TestSquirePlatform())
        parent <- SquirePaths.exists(root / ".refs" / "finos")
      yield assert(
        runner.requests.head.argv ==
          Chunk("git", "-C", source.toString, "worktree", "remove", "--force", dest.toString) && !parent
      )
    }
  }

object SquireRepoFixtures:
  def repo(
      name: String,
      path: String = "",
      strategy: String = "clone",
      source: Option[String] = None,
      ref: Option[String] = Some("main"),
      commit: Option[String] = Some("abc123"),
      sparse: List[String] = Nil
  ): ReferenceRepo =
    ReferenceRepo(
      name = name,
      org = Some("finos"),
      path = if path.nonEmpty then path else s"finos/$name",
      added = "2026-08-08T12:34:56Z",
      strategy = strategy,
      url = if strategy == "clone" then Some(s"https://github.com/finos/$name") else None,
      source = source,
      ref = ref,
      commit = commit,
      depth = if strategy == "clone" then Some(1) else None,
      sparse = sparse
    )

  def ok(request: ProcessRequest): ProcessResult = ProcessResult(request, 0, "", "")

  def cloneResponse(request: ProcessRequest): ProcessResult =
    val argv = request.argv
    if argv == Chunk("gh", "auth", "status") then ProcessResult(request, 1, "", "not logged in")
    else if argv.contains("symbolic-ref") then ProcessResult(request, 0, "main\n", "")
    else if argv.contains("rev-parse") then ProcessResult(request, 0, "abc123\n", "")
    else ProcessResult(request, 0, "", "")

  def localMetadataResponse(request: ProcessRequest): ProcessResult =
    if request.argv.contains("symbolic-ref") then ProcessResult(request, 0, "main\n", "")
    else if request.argv.contains("rev-parse") then ProcessResult(request, 0, "abc123\n", "")
    else ProcessResult(request, 0, "", "")

  def statusResponse(request: ProcessRequest): ProcessResult =
    val path = request.argv.lift(2).getOrElse("")
    if request.argv.contains("rev-parse") then
      ProcessResult(request, 0, if path.endsWith("drift") then "new123\n" else "abc123\n", "")
    else if request.argv.contains("symbolic-ref") then ProcessResult(request, 0, "main\n", "")
    else if request.argv.contains("status") then
      ProcessResult(request, 0, if path.endsWith("dirty") then " M README.md\n" else "", "")
    else ProcessResult(request, 0, "", "")

  def legacyStatusResponse(request: ProcessRequest): ProcessResult =
    val path = request.argv.lift(2).getOrElse("")
    if request.argv.contains("rev-parse") then
      if path.endsWith("nongit") then ProcessResult(request, 128, "", "not a git repository")
      else ProcessResult(request, 0, if path.endsWith("modified") then "new123\n" else "abc123\n", "")
    else if request.argv.contains("symbolic-ref") then ProcessResult(request, 0, "main\n", "")
    else if request.argv.contains("status") then
      ProcessResult(request, if path.endsWith("git-error") then 2 else 0, "", "status failed")
    else ProcessResult(request, 0, "", "")

class SquireEnvSpec extends Test[Any]:
  "environment report" - {
    "detects Claude Code and CI without confusing either with a sandbox" in {
      for
        root   <- SquireFixtures.scratch("env-detection")
        report <- SquireEnv.report(
          1.seconds,
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "loopback bind+accept+connect succeeded", 0.01),
            environment = Map(
              "CLAUDECODE"                -> "1",
              "CLAUDE_CODE_ENTRYPOINT"    -> "cli",
              "CLAUDE_CODE_SESSION_ID"    -> "session-1",
              "CLAUDE_CODE_CHILD_SESSION" -> "1",
              "GITHUB_ACTIONS"            -> "true"
            )
          ),
          root
        )
        serialized = SquireJson.encode(report)
      yield assert(
        report.claudeCode.detected &&
          report.claudeCode.entrypoint == Present("cli") &&
          report.claudeCode.sessionId == Present("session-1") &&
          report.claudeCode.childSession &&
          report.ci &&
          !report.sandboxed &&
          serialized.contains("jvm_network")
      )
    }

    "merges every settings level and tolerates malformed files" in {
      for
        root <- SquireFixtures.scratch("env-settings")
        managed = root / "managed-settings.json"
        home    = root / "home"
        _ <- Sync.defer {
          Files.createDirectories((home / ".claude").toJava)
          Files.createDirectories((root / ".claude").toJava)
          Files.writeString(
            managed.toJava,
            """{"sandbox":{"enabled":true,"network":{"allowedDomains":["managed.example"]}}}"""
          )
          Files.writeString((home / ".claude" / "settings.json").toJava, "{}")
          Files.writeString(
            (root / ".claude" / "settings.json").toJava,
            """{"unknown":true,"sandbox":{"enabled":true,"network":{"allowedDomains":["project.example"]}}}"""
          )
          Files.writeString((root / ".claude" / "settings.local.json").toJava, "not json")
        }
        report <- SquireEnv.report(
          1.seconds,
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            home = Some(home),
            managed = Chunk(managed)
          ),
          root
        )
        settings = report.claudeSettings
      yield assert(
        settings.sandboxEnabled == Map(
          "managed"       -> Present(true),
          "user"          -> Absent,
          "project"       -> Present(true),
          "project_local" -> Absent
        ) &&
          settings.networkAllowedDomains == Chunk("managed.example", "project.example") &&
          settings.networkDeniedDomains == Chunk.empty &&
          settings.sources("managed").present &&
          !settings.sources("user").present &&
          settings.sources("project").present &&
          !settings.sources("project_local").present
      )
    }
  }

  "platform predicates" - {
    "normalizes Windows macOS Darwin and Linux names" in {
      for root <- SquireFixtures.scratch("env-platform-predicates")
      yield
        val ok      = SquireEnv.CheckResult(Present(true), "ok", 0.0)
        val windows = SquireFixtures.platform(root, ok, osName = "Windows 11")
        val mac     = SquireFixtures.platform(root, ok, osName = "Mac OS X")
        val darwin  = SquireFixtures.platform(root, ok, osName = "Darwin")
        val linux   = SquireFixtures.platform(root, ok, osName = "Linux")
        assert(
          windows.isWindows && !windows.isMacOS && !windows.isLinux &&
            mac.isMacOS && darwin.isMacOS && !mac.isWindows &&
            linux.isLinux && !linux.isWindows && !linux.isMacOS
        )
    }
  }

  "checks" - {
    "returns JVM loopback success failure and timeout outcomes" in {
      for
        root    <- SquireFixtures.scratch("env-network")
        success <- SquireEnv.check(
          SquireEnv.CheckKind.JvmNetwork,
          1.seconds,
          SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        )
        failure <- SquireEnv.check(
          SquireEnv.CheckKind.JvmNetwork,
          1.seconds,
          SquireFixtures.platform(root, SquireEnv.CheckResult(Present(false), "SocketException: refused", 0.0))
        )
        timeout <- SquireEnv.check(
          SquireEnv.CheckKind.JvmNetwork,
          1.seconds,
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(false), "JVM loopback probe hung past 1s timeout", 1.0)
          )
        )
      yield assert(success && !failure && !timeout)
    }

    "probes the effective JVM temp directory and cleans a successful probe" in Scope.run {
      for
        root   <- SquireFixtures.scopedScratch("env-var-folders")
        absent <- SquireEnv.check(
          SquireEnv.CheckKind.VarFolders,
          1.seconds,
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            jvmTempDirectory = Present(root / "absent")
          )
        )
        writable <- SquireEnv.check(
          SquireEnv.CheckKind.VarFolders,
          1.seconds,
          SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0), varFolders = Some(root))
        )
        // The check must report the effective JVM temp directory, not the `varFolders` fallback that names
        // `/var/folders` on the live platform. Giving the two different values is what makes that assertion mean
        // anything: with both set to the scratch root, "the detail does not say /var/folders" held on Linux only
        // because a Linux temp path never contains that string, and failed on macOS, where the scratch root is
        // itself under /var/folders. Neither outcome told us whether the check read the right member.
        effective = root / "effective"
        fallback  = root / "fallback-var-folders"
        _      <- Sync.defer(Files.createDirectories(effective.toJava))
        report <- SquireEnv.report(
          1.seconds,
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            varFolders = Some(fallback),
            jvmTempDirectory = Present(effective)
          ),
          root
        )
        leftovers          <- Sync.defer(SquireFixtures.probeFiles(root))
        effectiveLeftovers <- Sync.defer(SquireFixtures.probeFiles(effective))
      yield assert(
        !absent && writable && leftovers.isEmpty && effectiveLeftovers.isEmpty &&
          report.checks("var_folders_writable").detail.contains(effective.toString) &&
          !report.checks("var_folders_writable").detail.contains(fallback.toString)
      )
    }

    "leaves the legacy probe sentinel byte-for-byte intact" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("env-var-folders-sentinel")
        sentinel = root / ".squire-env-probe"
        expected = "  sentinel contents\nwith trailing space  \n".getBytes(java.nio.charset.StandardCharsets.UTF_8)
        _        <- Sync.defer(Files.write(sentinel.toJava, expected))
        writable <- SquireEnv.check(
          SquireEnv.CheckKind.VarFolders,
          1.seconds,
          SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        )
        actual <- Sync.defer {
          if Files.exists(sentinel.toJava) then Some(Files.readAllBytes(sentinel.toJava)) else None
        }
      yield assert(writable && actual.exists(_.sameElements(expected)))
    }

    "does not follow a legacy probe symlink or alter its target" in Scope.run {
      for
        root         <- SquireFixtures.scopedScratch("env-var-folders-symlink")
        externalRoot <- SquireFixtures.scopedScratch("env-var-folders-symlink-target")
        target   = externalRoot / "target.txt"
        link     = root / ".squire-env-probe"
        expected = "external target\n".getBytes(java.nio.charset.StandardCharsets.UTF_8)
        _ <- Sync.defer {
          Files.write(target.toJava, expected)
          Files.createSymbolicLink(link.toJava, target.toJava)
        }
        writable <- SquireEnv.check(
          SquireEnv.CheckKind.VarFolders,
          1.seconds,
          SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        )
        actual <- Sync.defer(Files.readAllBytes(target.toJava))
      yield assert(writable && Files.isSymbolicLink(link.toJava) && actual.sameElements(expected))
    }

    "uses distinct owned files for overlapping probes in one temp directory" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("env-var-folders-overlap")
        paths    = java.util.concurrent.ConcurrentHashMap.newKeySet[String]()
        barrier  = new java.util.concurrent.CyclicBarrier(2)
        platform = SquireFixtures.platform(
          root,
          SquireEnv.CheckResult(Present(true), "ok", 0.0),
          writeProbe = Present { path =>
            paths.add(path.toString)
            Files.writeString(path.toJava, "squire probe")
            barrier.await(5, java.util.concurrent.TimeUnit.SECONDS)
          }
        )
        first        <- Fiber.init(SquireEnv.check(SquireEnv.CheckKind.VarFolders, 1.seconds, platform))
        second       <- Fiber.init(SquireEnv.check(SquireEnv.CheckKind.VarFolders, 1.seconds, platform))
        firstResult  <- first.get
        secondResult <- second.get
        leftovers    <- Sync.defer(SquireFixtures.probeFiles(root))
      yield assert(firstResult && secondResult && paths.size() == 2 && leftovers.isEmpty)
    }

    "reports an unavailable check when the JVM temp property is absent" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("env-var-folders-unavailable")
        platform = TestEnvPlatform(
          Map.empty,
          root / "home",
          Chunk.empty,
          root,
          Absent,
          _ => SquireEnv.CheckResult(Present(true), "ok", 0.0),
          _ => SquireEnv.DaemonProbe.Open,
          Absent,
          Absent,
          Absent
        )
        check  <- SquireEnv.check(SquireEnv.CheckKind.VarFolders, 1.seconds, platform)
        report <- SquireEnv.report(1.seconds, platform, root)
      yield assert(
        check &&
          report.checks("var_folders_writable").ok == Absent &&
          report.checks("var_folders_writable").detail == "effective JVM temp directory is unavailable — check skipped"
      )
    }

    "reports a blocked var folders write without leaving a probe" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("env-var-folders-blocked")
        platform = SquireFixtures.platform(
          root,
          SquireEnv.CheckResult(Present(true), "ok", 0.0),
          writeProbe = Present(_ => throw java.nio.file.AccessDeniedException("/var/folders/.squire-env-probe"))
        )
        blocked   <- SquireEnv.check(SquireEnv.CheckKind.VarFolders, 1.seconds, platform)
        leftovers <- Sync.defer(SquireFixtures.probeFiles(root))
      yield assert(!blocked && leftovers.isEmpty)
    }

    "cleans a probe created before a write failure" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("env-var-folders-partial")
        platform = SquireFixtures.platform(
          root,
          SquireEnv.CheckResult(Present(true), "ok", 0.0),
          writeProbe = Present { path =>
            Files.writeString(path.toJava, "partial"); throw java.nio.file.AccessDeniedException(path.toString)
          }
        )
        partial   <- SquireEnv.check(SquireEnv.CheckKind.VarFolders, 1.seconds, platform)
        leftovers <- Sync.defer(SquireFixtures.probeFiles(root))
      yield assert(!partial && leftovers.isEmpty)
    }

    "reports a delete probe failure without throwing" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("env-var-folders-delete")
        platform = SquireFixtures.platform(
          root,
          SquireEnv.CheckResult(Present(true), "ok", 0.0),
          deleteProbe = Present(_ => throw java.nio.file.AccessDeniedException("/var/folders/.squire-env-probe"))
        )
        check  <- SquireEnv.check(SquireEnv.CheckKind.VarFolders, 1.seconds, platform)
        report <- SquireEnv.report(1.seconds, platform, root)
      yield assert(!check && report.checks("var_folders_writable").detail.startsWith("could not clean probe file:"))
    }
  }

class SquireDoctorSpec extends Test[Any]:
  "daemon diagnostics" - {
    "prefers the daemon port file over the server log" in {
      for
        root <- SquireFixtures.scratch("doctor-port-file")
        _    <- SquireFixtures.writeDaemonFiles(root, portFile = Some(41001), logPort = Some(41002))
        platform = SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        report <- SquireDoctor.run(root, RecordingRunner(Chunk.empty), platform)
      yield assert(report.finding("mill_daemon").exists(_.code == "PORT_OPEN") && platform.daemonPorts == Chunk(41001))
    }

    "uses the latest server log port when no port file exists" in {
      for
        root <- SquireFixtures.scratch("doctor-server-log")
        _    <- SquireFixtures.writeDaemonFiles(root, portFile = None, logPort = Some(41003))
        platform = SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        report <- SquireDoctor.run(root, RecordingRunner(Chunk.empty), platform)
      yield assert(report.finding("mill_daemon").exists(_.code == "PORT_OPEN") && platform.daemonPorts == Chunk(41003))
    }

    "distinguishes no daemon sandbox and refused daemon connections" in {
      for
        root     <- SquireFixtures.scratch("doctor-daemon-states")
        noDaemon <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        )
        _       <- SquireFixtures.writeDaemonFiles(root, portFile = Some(41004), logPort = None)
        sandbox <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            daemonProbe = _ => SquireEnv.DaemonProbe.Sandbox("Operation not permitted")
          )
        )
        refused <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            daemonProbe = _ => SquireEnv.DaemonProbe.Refused("Connection refused")
          )
        )
      yield assert(
        noDaemon.finding("mill_daemon").exists(_.code == "NO_DAEMON") &&
          sandbox.finding("mill_daemon").exists(_.code == "SANDBOX") &&
          refused.finding("mill_daemon").exists(_.code == "REFUSED")
      )
    }
  }

  "project diagnostics" - {
    "accepts Mill-owned setup and the MEP main class without the retired CLI" in {
      for
        root <- SquireFixtures.scratch("doctor-project")
        _    <- Sync.defer {
          Files.createDirectories((root / ".config" / "mise" / "tasks").toJava)
          val mep = root / "morphir" / "langkit" / "elm" / "compiler" / "mep"
          Files.createDirectories(mep.toJava)
          Files.writeString((root / ".config" / "mise" / "tasks" / "setup").toJava, "bun install --ignore-scripts\n")
          Files.writeString((root / "package.json").toJava, "{}\n")
          Files.writeString(
            (mep / "package.mill.yaml").toJava,
            "object jvm:\n  mainClass: morphir.langkit.elm.compiler.mep.Main\n"
          )
          val plugin  = root / "mill-plugins" / "morphir"
          val modules = List("toolchain", "javascript", "elm-tooling", "core", "elm", "integration")
          modules.foreach(name => Files.createDirectories((plugin / name).toJava))
          Files.writeString(
            (plugin / "package.mill").toJava,
            modules.map(name =>
              s"object ${if name.contains('-') then s"`$name`" else name} extends Module\n"
            ).mkString +
              "publishLocalTestRepo publishedPluginRepositories\n"
          )
          val integration =
            plugin / "integration" / "test" / "src" / "org" / "finos" / "morphir" / "mill" / "PublishedPluginIntegrationTests.scala"
          Files.createDirectories(integration.parent.get.toJava)
          Files.writeString(integration.toJava, "COURSIER_REPOSITORIES millExecutable\n")
          val consumer = plugin / "integration" / "resources" / "published-consumer" / "build.mill"
          Files.createDirectories(consumer.parent.get.toJava)
          Files.writeString(consumer.toJava, "MORPHIR_PUBLISHED_TEST_REPOSITORIES\n")
        }
        report <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(root, SquireEnv.CheckResult(Present(true), "ok", 0.0))
        )
      yield assert(
        report.finding("setup").exists(_.code == "OK") &&
          report.finding("main_class_task").exists(_.code == "OK") &&
          report.finding("mill_morphir").exists(_.code == "OK") &&
          report.finding("jvm_temp").exists(_.code == "OK")
      )
    }

    "blocks missing Mill Morphir modules and a relative acquisition cache override" in {
      for
        root <- SquireFixtures.scratch("doctor-project-invalid")
        platform = SquireFixtures.platform(
          root,
          SquireEnv.CheckResult(Present(true), "ok", 0.0),
          environment = Map("MORPHIR_NODE_CACHE" -> "relative-cache")
        )
        report <- SquireDoctor.run(root, RecordingRunner(Chunk.empty), platform)
      yield assert(
        report.finding("mill_morphir").exists(finding =>
          finding.blocked && finding.message.contains("plugin modules")
        ) &&
          report.finding("acquisition_cache").exists(finding => finding.blocked && finding.message.contains("absolute"))
      )
    }

    "detects corrupt acquisition cache content and stale metabuild output" in {
      for
        root <- SquireFixtures.scratch("doctor-cache-metabuild")
        cache    = root / "cache"
        digest   = "0" * 64
        source   = root / "build.mill"
        compiled = root / "out" / "mill-build" / "compile.dest" / "classes" / "build.class"
        _ <- Sync.defer {
          Files.createDirectories((cache / "sha256").toJava)
          Files.writeString((cache / "sha256" / digest).toJava, "not the named digest")
          Files.createDirectories(compiled.parent.get.toJava)
          Files.writeString(compiled.toJava, "compiled")
          Files.writeString(source.toJava, "package build\n")
          Files.setLastModifiedTime(compiled.toJava, java.nio.file.attribute.FileTime.fromMillis(1_700_000_000_000L))
          Files.setLastModifiedTime(source.toJava, java.nio.file.attribute.FileTime.fromMillis(1_700_000_060_000L))
        }
        report <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("MORPHIR_NODE_CACHE" -> cache.toString)
          )
        )
      yield assert(
        report.finding("acquisition_cache").exists(finding => finding.blocked && finding.code == "CORRUPT") &&
          report.finding("metabuild").exists(finding => finding.blocked && finding.code == "STALE")
      )
    }

    "inspects corrupt default acquisition caches at each platform root" in Scope.run {
      def corrupt(root: Path): Unit = {
        val digestRoot = root / "sha256"
        Files.createDirectories(digestRoot.toJava)
        Files.writeString((digestRoot / ("0" * 64)).toJava, "not the zero digest")
      }

      for
        root <- SquireFixtures.scopedScratch("doctor-platform-cache-roots")
        macHome              = root / "mac-home"
        macCache             = macHome / "Library" / "Caches" / "morphir-scala"
        windowsHome          = root / "windows-home"
        localAppData         = root / "local-app-data"
        windowsAbsoluteCache = localAppData / "morphir-scala" / "Cache"
        windowsRelativeCache = windowsHome / "AppData" / "Local" / "morphir-scala" / "Cache"
        linuxHome            = root / "linux-home"
        xdgCacheHome         = root / "xdg-cache"
        linuxAbsoluteCache   = xdgCacheHome / "morphir-scala"
        linuxRelativeCache   = linuxHome / ".cache" / "morphir-scala"
        _ <- Sync.defer {
          corrupt(macCache)
          corrupt(windowsAbsoluteCache)
          corrupt(windowsRelativeCache)
          corrupt(linuxAbsoluteCache)
          corrupt(linuxRelativeCache)
        }
        mac <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("XDG_CACHE_HOME" -> (root / "ignored").toString),
            osName = "Mac OS X",
            home = Some(macHome)
          )
        )
        windowsAbsolute <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("LOCALAPPDATA" -> localAppData.toString),
            osName = "Windows 11",
            home = Some(windowsHome)
          )
        )
        windowsRelative <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("LOCALAPPDATA" -> "relative-local-app-data"),
            osName = "Windows 11",
            home = Some(windowsHome)
          )
        )
        linuxAbsolute <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("XDG_CACHE_HOME" -> xdgCacheHome.toString),
            osName = "Linux",
            home = Some(linuxHome)
          )
        )
        linuxRelative <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("XDG_CACHE_HOME" -> "relative-xdg-cache"),
            osName = "Linux",
            home = Some(linuxHome)
          )
        )
      yield assert(
        List(mac, windowsAbsolute, windowsRelative, linuxAbsolute, linuxRelative).forall(
          _.finding("acquisition_cache").exists(finding => finding.blocked && finding.code == "CORRUPT")
        )
      )
    }

    "falls back from malformed Windows and XDG cache homes without aborting" in Scope.run {
      def corrupt(root: Path): Unit = {
        val digestRoot = root / "sha256"
        Files.createDirectories(digestRoot.toJava)
        Files.writeString((digestRoot / ("0" * 64)).toJava, "not the zero digest")
      }

      for
        root <- SquireFixtures.scopedScratch("doctor-malformed-cache-homes")
        windowsHome     = root / "windows-home"
        windowsFallback = windowsHome / "AppData" / "Local" / "morphir-scala" / "Cache"
        linuxHome       = root / "linux-home"
        linuxFallback   = linuxHome / ".cache" / "morphir-scala"
        _ <- Sync.defer {
          corrupt(windowsFallback)
          corrupt(linuxFallback)
        }
        windows <- Abort.run[SquireError](
          SquireDoctor.run(
            root,
            RecordingRunner(Chunk.empty),
            SquireFixtures.platform(
              root,
              SquireEnv.CheckResult(Present(true), "ok", 0.0),
              environment = Map("LOCALAPPDATA" -> "\u0000"),
              osName = "Windows 11",
              home = Some(windowsHome)
            )
          )
        )
        linux <- Abort.run[SquireError](
          SquireDoctor.run(
            root,
            RecordingRunner(Chunk.empty),
            SquireFixtures.platform(
              root,
              SquireEnv.CheckResult(Present(true), "ok", 0.0),
              environment = Map("XDG_CACHE_HOME" -> "\u0000"),
              osName = "Linux",
              home = Some(linuxHome)
            )
          )
        )
      yield assert(
        windows.exists(_.finding("acquisition_cache").exists(finding =>
          finding.blocked && finding.code == "CORRUPT"
        )) &&
          linux.exists(_.finding("acquisition_cache").exists(finding => finding.blocked && finding.code == "CORRUPT"))
      )
    }

    "prefers an absolute acquisition cache override to a clean platform default" in Scope.run {
      def corrupt(root: Path): Unit = {
        val digestRoot = root / "sha256"
        Files.createDirectories(digestRoot.toJava)
        Files.writeString((digestRoot / ("0" * 64)).toJava, "not the zero digest")
      }

      for
        root <- SquireFixtures.scopedScratch("doctor-cache-override-precedence")
        home          = root / "mac-home"
        overrideCache = root / "override-cache"
        _      <- Sync.defer(corrupt(overrideCache))
        report <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("MORPHIR_NODE_CACHE" -> overrideCache.toString),
            osName = "Mac OS X",
            home = Some(home)
          )
        )
      yield assert(
        report.finding("acquisition_cache").exists(finding =>
          finding.blocked && finding.code == "CORRUPT" && finding.message.contains(overrideCache.toString)
        )
      )
    }

    "bounds oversized acquisition cache diagnostics without declaring content corrupt" in {
      var allocatedRoot = Option.empty[Path]
      for
        report <- Scope.run {
          for
            root <- SquireFixtures.scopedScratch("doctor-cache-bounded")
            _    <- Sync.defer { allocatedRoot = Some(root) }
            cache  = root / "cache"
            digest = "0" * 64
            _ <- Sync.defer {
              val entry = cache / "sha256" / digest
              Files.createDirectories(entry.parent.get.toJava)
              val channel = Files.newByteChannel(
                entry.toJava,
                java.nio.file.StandardOpenOption.CREATE_NEW,
                java.nio.file.StandardOpenOption.WRITE
              )
              try
                channel.position(65L * 1024 * 1024 - 1L)
                channel.write(java.nio.ByteBuffer.wrap(Array[Byte](0)))
              finally channel.close()
            }
            report <- SquireDoctor.run(
              root,
              RecordingRunner(Chunk.empty),
              SquireFixtures.platform(
                root,
                SquireEnv.CheckResult(Present(true), "ok", 0.0),
                environment = Map("MORPHIR_NODE_CACHE" -> cache.toString)
              )
            )
          yield report
        }
        rootRemoved <- Sync.defer(allocatedRoot.exists(root => !Files.exists(root.toJava)))
        _           <- Sync.defer(allocatedRoot.foreach(SquireFixtures.deleteRecursively))
      yield assert(
        report.finding("acquisition_cache").exists(finding =>
          !finding.blocked && finding.code == "NOTICE" && finding.message.contains("oversized entry")
        ) && rootRemoved
      )
    }

    "treats a digest-named symlink to matching external content as corrupt" in Scope.run {
      for
        externalRoot <- SquireFixtures.scopedScratch("doctor-cache-symlink-external")
        root         <- SquireFixtures.scopedScratch("doctor-cache-symlink")
        cache          = root / "cache"
        digest         = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        externalTarget = externalRoot / "matching-content"
        _ <- Sync.defer {
          Files.writeString(externalTarget.toJava, "")
          Files.createDirectories((cache / "sha256").toJava)
          Files.createSymbolicLink((cache / "sha256" / digest).toJava, externalTarget.toJava)
        }
        report <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("MORPHIR_NODE_CACHE" -> cache.toString)
          )
        )
        externalTargetIntact <- Sync.defer(Files.isRegularFile(externalTarget.toJava))
      yield assert(
        report.finding("acquisition_cache").exists(finding => finding.blocked && finding.code == "CORRUPT") &&
          externalTargetIntact
      )
    }

    "requires scoped cleanup for cache fixtures after success and abort" in {
      var completedRoot = Option.empty[Path]
      var abortedRoot   = Option.empty[Path]
      for
        _ <- Scope.run {
          for
            root <- SquireFixtures.scopedScratch("doctor-cache-clean-success")
            _    <- Sync.defer { completedRoot = Some(root) }
          yield ()
        }
        completedRemoved <- Sync.defer(completedRoot.exists(root => !Files.exists(root.toJava)))
        aborted          <- Abort.run[String](
          Scope.run {
            for
              root <- SquireFixtures.scopedScratch("doctor-cache-clean-abort")
              _    <- Sync.defer { abortedRoot = Some(root) }
              _    <- Abort.fail("deliberate cleanup regression")
            yield ()
          }
        )
        abortedRemoved <- Sync.defer(abortedRoot.exists(root => !Files.exists(root.toJava)))
        _              <- Sync.defer {
          completedRoot.foreach(SquireFixtures.deleteRecursively)
          abortedRoot.foreach(SquireFixtures.deleteRecursively)
        }
      yield assert(completedRemoved && aborted.isFailure && abortedRemoved)
    }

    "validates a relative cache override before honoring disabled mode and skips corrupt cache content" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("doctor-cache-disabled")
        cache  = root / "cache"
        digest = "0" * 64
        _ <- Sync.defer {
          Files.createDirectories((cache / "sha256").toJava)
          Files.writeString((cache / "sha256" / digest).toJava, "not the named digest")
        }
        disabled <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map(
              "MORPHIR_NODE_CACHE"                 -> cache.toString,
              "MORPHIR_NODE_DISABLE_MACHINE_CACHE" -> "true"
            )
          )
        )
        relativeDisabled <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map(
              "MORPHIR_NODE_CACHE"                 -> "relative-cache",
              "MORPHIR_NODE_DISABLE_MACHINE_CACHE" -> "true"
            )
          )
        )
      yield assert(
        disabled.finding("acquisition_cache").exists(finding => !finding.blocked && finding.code == "DISABLED") &&
          relativeDisabled.finding("acquisition_cache").exists(finding => finding.blocked && finding.code == "INVALID")
      )
    }

    "bounds acquisition cache inspection at 256 directory entries" in Scope.run {
      for
        root <- SquireFixtures.scopedScratch("doctor-cache-entry-limit")
        cache = root / "cache"
        _ <- Sync.defer {
          val digestRoot = cache / "sha256"
          Files.createDirectories(digestRoot.toJava)
          (1 to 257).foreach(index => Files.writeString((digestRoot / f"entry-$index%03d").toJava, "fixture"))
        }
        report <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("MORPHIR_NODE_CACHE" -> cache.toString)
          )
        )
      yield assert(
        report.finding("acquisition_cache").exists(finding =>
          !finding.blocked && finding.code == "NOTICE" && finding.message.contains(
            "directory entry limit reached (256)"
          )
        )
      )
    }

    "blocks non-regular digest entries and bounds unreadable valid entries" in Scope.run {
      val emptyDigest = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
      for
        root <- SquireFixtures.scopedScratch("doctor-cache-unreadable")
        cache      = root / "cache"
        digestRoot = cache / "sha256"
        unreadable = digestRoot / emptyDigest
        _ <- Sync.defer {
          Files.createDirectories((digestRoot / ("1" * 64)).toJava)
          Files.writeString(unreadable.toJava, "")
        }
        structural <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("MORPHIR_NODE_CACHE" -> cache.toString)
          )
        )
        _ <- Sync.defer(Files.delete((digestRoot / ("1" * 64)).toJava))
        _ <- Scope.acquireRelease(
          Sync.defer {
            val original = Files.getPosixFilePermissions(unreadable.toJava)
            Files.setPosixFilePermissions(
              unreadable.toJava,
              java.util.EnumSet.noneOf(classOf[java.nio.file.attribute.PosixFilePermission])
            )
            original
          }
        )(original => Sync.defer(Files.setPosixFilePermissions(unreadable.toJava, original)))
        report <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("MORPHIR_NODE_CACHE" -> cache.toString)
          )
        )
      yield assert(
        structural.finding("acquisition_cache").exists(finding => finding.blocked && finding.code == "CORRUPT") &&
          report.finding("acquisition_cache").exists(finding =>
            !finding.blocked && finding.code == "NOTICE" && finding.message.contains(
              "unreadable or changed during inspection"
            )
          )
      )
    }

    "bounds acquisition cache hashing after 256 MiB" in Scope.run {
      val entryBytes = 64L * 1024 * 1024
      for
        root <- SquireFixtures.scopedScratch("doctor-cache-total-budget")
        cache = root / "cache"
        _ <- Sync.defer {
          val digestRoot = cache / "sha256"
          Files.createDirectories(digestRoot.toJava)
          (1 to 5).foreach { index =>
            val entry   = digestRoot / s"entry-$index"
            val channel = Files.newByteChannel(
              entry.toJava,
              java.nio.file.StandardOpenOption.CREATE_NEW,
              java.nio.file.StandardOpenOption.WRITE
            )
            try
              channel.write(java.nio.ByteBuffer.wrap(Array(index.toByte)))
              channel.position(entryBytes - 1L)
              channel.write(java.nio.ByteBuffer.wrap(Array[Byte](0)))
            finally channel.close()
            val digest = java.security.MessageDigest.getInstance("SHA-256")
            digest.update(index.toByte)
            val zeroes    = Array.ofDim[Byte](1024 * 1024)
            var remaining = entryBytes - 1L
            while remaining > 0L do
              val count = math.min(remaining, zeroes.length.toLong).toInt
              digest.update(zeroes, 0, count)
              remaining -= count
            val name = digest.digest().map(byte => f"${byte & 0xff}%02x").mkString
            Files.move(entry.toJava, (digestRoot / name).toJava)
          }
        }
        report <- SquireDoctor.run(
          root,
          RecordingRunner(Chunk.empty),
          SquireFixtures.platform(
            root,
            SquireEnv.CheckResult(Present(true), "ok", 0.0),
            environment = Map("MORPHIR_NODE_CACHE" -> cache.toString)
          )
        )
      yield assert(
        report.finding("acquisition_cache").exists(finding =>
          !finding.blocked && finding.code == "NOTICE" && finding.message.contains(
            "total hash budget reached (268435456 bytes)"
          )
        )
      )
    }
  }

class SquireTrackingSpec extends Test[Any]:
  import SquireTrackingFixtures.*

  "tracking resolution" - {
    "defaults absent settings to auto and reports unavailable when bd is missing" in {
      for
        root   <- SquireFixtures.scratch("tracking-absent")
        report <- SquireTracking.resolve(root, runner(gitFailure, bdFailure), TestSquirePlatform())
      yield assert(report.configuredMode == TrackingMode.Auto && report.effectiveMode == TrackingMode.Unavailable &&
        report.reason == "bd is not on PATH")
    }

    "honours auto beads off and YAML boolean mode settings" in {
      for
        root         <- SquireFixtures.scratch("tracking-modes")
        _            <- settings(root, "auto")
        _            <- beads(root)
        auto         <- SquireTracking.resolve(root, runner(gitShared, bdVersion), TestSquirePlatform(Present("bd")))
        _            <- settings(root, "beads")
        forced       <- SquireTracking.resolve(root, runner(gitShared, bdVersion), TestSquirePlatform(Present("bd")))
        _            <- settings(root, "off")
        off          <- SquireTracking.resolve(root, runner(gitFailure, bdFailure), TestSquirePlatform())
        _            <- settings(root, "false")
        booleanOff   <- SquireTracking.resolve(root, runner(gitFailure, bdFailure), TestSquirePlatform())
        _            <- settings(root, "true")
        booleanBeads <- SquireTracking.resolve(root, runner(gitShared, bdVersion), TestSquirePlatform(Present("bd")))
      yield assert(auto.effectiveMode == TrackingMode.Beads && forced.effectiveMode == TrackingMode.Beads &&
        off.effectiveMode == TrackingMode.Off && booleanOff.configuredMode == TrackingMode.Off &&
        booleanBeads.configuredMode == TrackingMode.Beads)
    }

    "warns for invalid or unavailable forced beads settings" in {
      for
        root        <- SquireFixtures.scratch("tracking-warnings")
        _           <- settings(root, "unknown")
        invalid     <- SquireTracking.resolve(root, runner(gitFailure, bdFailure), TestSquirePlatform())
        _           <- settings(root, "beads")
        unavailable <- SquireTracking.resolve(root, runner(gitFailure, bdFailure), TestSquirePlatform())
      yield assert(invalid.configuredMode == TrackingMode.Auto && invalid.warning.exists(_.contains("unrecognised")) &&
        unavailable.warning.exists(_.contains("tracking.mode is 'beads'")))
    }

    "reports guidance drift independently for both agent instruction files" in {
      for
        root <- SquireFixtures.scratch("tracking-guidance-drift")
        _    <- Sync.defer {
          Files.writeString(
            (root / "AGENTS.md").toJava,
            "<!-- BEGIN BEADS INTEGRATION -->old<!-- END BEADS INTEGRATION -->"
          )
          Files.writeString((root / "CLAUDE.md").toJava, "no pointer")
        }
        report <- SquireTracking.resolve(root, runner(gitFailure, bdFailure), TestSquirePlatform())
      yield assert(report.guidanceDrift.map(_.file).toSet == Set("AGENTS.md", "CLAUDE.md"))
    }

    "distinguishes missing beads git worktree and workspace fallback states" in {
      for
        root         <- SquireFixtures.scratch("tracking-workspaces")
        missingBeads <- SquireTracking.resolve(root, runner(gitShared, bdVersion), TestSquirePlatform(Present("bd")))
        _            <- beads(root)
        localStore = root / ".beads" / "embeddeddolt"
        _     <- Sync.defer(Files.createDirectories(localStore.toJava))
        local <- SquireTracking.resolve(
          root,
          runner(gitWorktree(root / ".git", root / ".git"), bdVersion),
          TestSquirePlatform(Present("bd"))
        )
        _ <- Sync.defer(Files.delete(localStore.toJava))
        main   = root / "main"
        common = main / ".git"
        _      <- Sync.defer(Files.createDirectories(common.toJava))
        _      <- Sync.defer(Files.createDirectories((main / ".beads").toJava))
        _      <- Sync.defer(Files.writeString((main / ".beads" / "config.yaml").toJava, "prefix: morphir\n"))
        shared <- SquireTracking.resolve(
          root,
          runner(gitWorktree(root / "worktree-git", common), bdVersion),
          TestSquirePlatform(Present("bd"))
        )
        _          <- Sync.defer(Files.delete((main / ".beads" / "config.yaml").toJava))
        unresolved <- SquireTracking.resolve(
          root,
          runner(gitWorktree(root / "worktree-git", common), bdVersion),
          TestSquirePlatform(Present("bd"))
        )
      yield assert(missingBeads.effectiveMode == TrackingMode.Unavailable && local.workspace.status == "local" &&
        shared.workspace.status == "shared" && unresolved.effectiveMode == TrackingMode.Unavailable &&
        unresolved.workspace.remedy.exists(_.contains("bd bootstrap")))
    }

    "treats bd version failure and a non repository as unavailable without mutating beads" in {
      for
        root   <- SquireFixtures.scratch("tracking-failures")
        _      <- beads(root)
        failed <- SquireTracking.resolve(root, runner(gitFailure, bdFailure), TestSquirePlatform(Present("bd")))
      yield assert(failed.effectiveMode == TrackingMode.Unavailable && failed.workspace.status == "no-repo")
    }
  }

  "guidance rewrite" - {
    "removes both beads marker families and replaces the pointer with one trailing newline" in {
      val input = "before\n<!-- BEGIN BEADS INTEGRATION -->old<!-- END BEADS INTEGRATION -->\n" +
        "<!-- BEGIN BEADS CODEX SETUP -->old<!-- END BEADS CODEX SETUP -->\n" +
        "<!-- BEGIN MORPHIR TRACKING -->stale<!-- END MORPHIR TRACKING -->\n\n"
      val rewrite = SquireTracking.rewriteGuidance(input)
      assert(rewrite.removedBeadsBlocks == 2 && !rewrite.text.contains("BEGIN BEADS") &&
        rewrite.text.contains(".claude/skills/squire/squire tracking status --quiet") && rewrite.text.endsWith("\n") &&
        !rewrite.text.endsWith("\n\n"))
    }

    "appends the pointer and is idempotent" in {
      val first  = SquireTracking.rewriteGuidance("agent instructions\n")
      val second = SquireTracking.rewriteGuidance(first.text)
      assert(first.changed && second.text == first.text && !second.changed)
    }

    "checks diffs applies only when requested and reports missing targets" in {
      for
        root   <- SquireFixtures.scratch("tracking-guidance")
        _      <- Sync.defer(Files.writeString((root / "AGENTS.md").toJava, "stale\n"))
        check  <- SquireTracking.syncGuidance(root, SquireTracking.GuidanceMode.Check)
        diff   <- SquireTracking.syncGuidance(root, SquireTracking.GuidanceMode.Diff)
        before <- Sync.defer(Files.readString((root / "AGENTS.md").toJava))
        apply  <- SquireTracking.syncGuidance(root, SquireTracking.GuidanceMode.Apply)
        after  <- Sync.defer(Files.readString((root / "AGENTS.md").toJava))
        second <- SquireTracking.syncGuidance(root, SquireTracking.GuidanceMode.Apply)
      yield assert(check.exitCode == 1 && diff.exitCode == 1 && diff.output.contains("--- a/AGENTS.md") &&
        before == "stale\n" && apply.exitCode == 1 && after.contains("BEGIN MORPHIR TRACKING") &&
        apply.missing == Chunk("CLAUDE.md") && second.changed.isEmpty)
    }

    "rejects symlinked guidance targets before apply check or doctor can follow them" in {
      for
        root <- SquireFixtures.scratch("tracking-guidance-symlink")
        outside  = root / "outside"
        external = outside / "AGENTS.md"
        _ <- Sync.defer {
          Files.createDirectories(outside.toJava)
          Files.writeString(external.toJava, "outside\n")
          Files.createSymbolicLink((root / "AGENTS.md").toJava, external.toJava)
          Files.writeString((root / "CLAUDE.md").toJava, SquireTracking.pointer + "\n")
        }
        checked <- SquireTracking.syncGuidance(root, SquireTracking.GuidanceMode.Check)
        applied <- SquireTracking.syncGuidance(root, SquireTracking.GuidanceMode.Apply)
        doctorOutput = new StringBuilder
        doctor <- SquireCli.runTrackingDoctor(
          root,
          runner(gitFailure, bdFailure),
          TestSquirePlatform(),
          value => doctorOutput.append(value)
        )
        after <- Sync.defer(Files.readString(external.toJava))
      yield assert(checked.exitCode == 1 && applied.exitCode == 1 && doctor == 1 && after == "outside\n" &&
        checked.output.contains("unsafe") && applied.output.contains("unsafe") && doctorOutput.result().contains(
          "unsafe"
        ))
    }

    "rejects a symlinked repository root before reading its guidance targets" in {
      for
        root <- SquireFixtures.scratch("tracking-guidance-intermediate")
        outside  = root / "outside"
        alias    = root / "alias"
        external = outside / "AGENTS.md"
        _ <- Sync.defer {
          Files.createDirectories(outside.toJava)
          Files.writeString(external.toJava, "outside\n")
          Files.writeString((outside / "CLAUDE.md").toJava, SquireTracking.pointer + "\n")
          Files.createSymbolicLink(alias.toJava, outside.toJava)
        }
        result <- SquireTracking.syncGuidance(alias, SquireTracking.GuidanceMode.Apply)
        after  <- Sync.defer(Files.readString(external.toJava))
      yield assert(result.exitCode == 1 && result.output.contains("unsafe") && after == "outside\n")
    }
  }

object SquireTrackingFixtures:
  val gitShared: ProcessRequest => ProcessResult  = request => ProcessResult(request, 0, ".git\n.git\n", "")
  val gitFailure: ProcessRequest => ProcessResult = request => ProcessResult(request, 1, "", "not a repository")
  val bdVersion: ProcessRequest => ProcessResult  = request => ProcessResult(request, 0, "bd 0.42.0\n", "")
  val bdFailure: ProcessRequest => ProcessResult  = request => ProcessResult(request, 1, "", "failed")
  val unexpected: ProcessRequest => ProcessResult =
    request => throw new AssertionError(s"unexpected process: ${request.argv}")

  def runner(git: ProcessRequest => ProcessResult, bd: ProcessRequest => ProcessResult): RuleRunner =
    RuleRunner(request => if request.argv.headOption.contains("git") then git(request) else bd(request))

  def gitWorktree(gitDir: Path, commonDir: Path): ProcessRequest => ProcessResult =
    request => ProcessResult(request, 0, s"$gitDir\n$commonDir\n", "")

  def settings(root: Path, mode: String): Unit < Sync =
    Sync.defer {
      val path = root / ".config" / "squire" / "settings.local.yaml"
      Files.createDirectories(path.parent.get.toJava)
      Files.writeString(path.toJava, s"tracking:\n  mode: $mode\n")
    }

  def beads(root: Path): Unit < Sync =
    Sync.defer(Files.createDirectories((root / ".beads").toJava))

final class TestEnvPlatform(
    val environment: Map[String, String],
    val home: Path,
    val managedSettingsCandidates: Chunk[Path],
    val varFolders: Path,
    override val jvmTempDirectory: Maybe[Path],
    val jvmProbe: Duration => SquireEnv.CheckResult,
    val daemonProbe: Int => SquireEnv.DaemonProbe,
    val probePathFn: Maybe[Path => Path],
    val writeProbeFn: Maybe[Path => Unit],
    val deleteProbeFn: Maybe[Path => Unit],
    val osName: String = "Linux"
) extends SquireEnv.Platform:
  var daemonPorts: Chunk[Int] = Chunk.empty

  def now: java.time.Instant          = java.time.Instant.EPOCH
  override def zone: java.time.ZoneId = java.time.ZoneOffset.UTC

  def probeJvmNetwork(timeout: Duration): SquireEnv.CheckResult = jvmProbe(timeout)

  def probeDaemon(port: Int): SquireEnv.DaemonProbe =
    daemonPorts = daemonPorts.append(port)
    daemonProbe(port)

  override def probePath(directory: Path): Path = probePathFn match
    case Present(value) => value(directory)
    case Absent         => super.probePath(directory)

  override def writeProbe(path: Path, channel: java.nio.channels.SeekableByteChannel): Unit = writeProbeFn match
    case Present(value) => value(path)
    case Absent         => super.writeProbe(path, channel)

  override def deleteProbe(path: Path): Unit = deleteProbeFn match
    case Present(value) => value(path)
    case Absent         => super.deleteProbe(path)

class SquireChangelogSpec extends Test[Any]:
  import SquireChangelogFixtures.*

  // Same derivation the mise-policy suite uses: the tests run with the skill directory as the working directory.
  private val corpusDirectory =
    Path(java.lang.System.getProperty("user.dir")) / ".." / ".." / ".." / ".config" / "version-rules"

  /**
   * The port asserted against the corpus the originals are asserted against, in
   * `mill-plugins/morphir/core/test/.../VersionCorpusTests.scala`. A rule that changes on one side alone fails here or
   * there. The cases live in the data, so adding one needs no change in either suite.
   */
  "version rules corpus" - {
    "parses and rejects exactly the versions the corpus names" in {
      for corpus <- SquireVersionCorpus.load(corpusDirectory)
      yield
        val misparsed = corpus.semverParse.filter { expected =>
          SquireVersion.SemVer.parse(expected.text) !=
            Some(SquireVersion.SemVer(expected.major, expected.minor, expected.patch, expected.prerelease.toOption))
        }
        val accepted = corpus.semverRejects.filter(rejected => SquireVersion.SemVer.parse(rejected.text).isDefined)
        assert(misparsed.isEmpty && accepted.isEmpty)
    }

    "orders every pair the way the corpus orders it" in {
      for corpus <- SquireVersionCorpus.load(corpusDirectory)
      yield
        val misordered = corpus.semverCompare.filter { expected =>
          SquireVersion.SemVer.compare(expected.left, expected.right) != Right(expected.sign)
        }
        val accepted = corpus.semverCompareRejects.filter { expected =>
          !SquireVersion.SemVer.compare(expected.left, expected.right).left.exists(_.contains(expected.messageContains))
        }
        assert(misordered.isEmpty && accepted.isEmpty)
    }

    "derives the pattern, tag and version of every stream" in {
      for corpus <- SquireVersionCorpus.load(corpusDirectory)
      yield
        val wrong = corpus.tagStreams.filter { expected =>
          val stream = SquireVersion.TagStream(expected.namespace.toOption)
          stream.pattern != expected.pattern ||
          expected.tagFor.exists(pair => stream.tagFor(pair.version) != pair.tag) ||
          expected.versionFromTag.exists(pair => stream.versionFromTag(pair.tag) != pair.version.toOption)
        }
        assert(wrong.isEmpty)
    }

    "reads every release line and fails with the exact message the corpus records" in {
      for
        corpus   <- SquireVersionCorpus.load(corpusDirectory)
        accepted <- Kyo.foreach(corpus.changelogReleaseLine) { expected =>
          SquireVersionCorpus.changelog(corpusDirectory, expected.file).map { text =>
            (expected, SquireVersion.Changelog.releaseLine(text, expected.source))
          }
        }
        rejected <- Kyo.foreach(corpus.changelogReleaseLineRejects) { expected =>
          SquireVersionCorpus.changelog(corpusDirectory, expected.file).map { text =>
            (expected, SquireVersion.Changelog.releaseLine(text, expected.source))
          }
        }
      yield
        val wrongLine    = accepted.filter((expected, actual) => actual != Right(expected.releaseLine))
        val wrongMessage = rejected.filter((expected, actual) => actual != Left(expected.message))
        assert(wrongLine.isEmpty && wrongMessage.isEmpty)
    }

    "lists every version heading in document order" in {
      for
        corpus <- SquireVersionCorpus.load(corpusDirectory)
        actual <- Kyo.foreach(corpus.changelogHeadings) { expected =>
          SquireVersionCorpus.changelog(corpusDirectory, expected.file).map { text =>
            (expected, SquireVersion.Changelog.headings(text).toList)
          }
        }
      yield
        val wrong = actual.filter { (expected, headings) =>
          headings != expected.headings.map(row => SquireVersion.ChangelogHeading(row.version, row.date.toOption))
        }
        assert(wrong.isEmpty)
    }

    "declares the same areas the corpus does" in {
      for corpus <- SquireVersionCorpus.load(corpusDirectory)
      yield
        val expected = corpus.areas.map(area =>
          ReleaseArea(area.name, area.namespace.toOption, area.changelogPath, area.startingVersion.toOption)
        )
        assert(SquireChangelog.Areas == expected)
    }
  }

  "changelog check" - {
    "passes on a well-formed changelog with one undated heading" in {
      for
        root   <- SquireFixtures.scratch("changelog-check-ok")
        _      <- writeAllAreas(root, validLibraries, validMillPlugins)
        report <- SquireChangelog.check(root)
      yield assert(
        report.ok && report.outcomes.forall(_.status == "ok") &&
          report.outcomes.find(_.area == "libraries").exists(_.releaseLine.exists(_ == "1.2.3")) &&
          report.outcomes.find(_.area == "mill-plugins").exists(_.releaseLine.exists(_ == "0.5.0-M05"))
      )
    }

    "fails naming the file when there is no undated heading" in {
      for
        root   <- SquireFixtures.scratch("changelog-check-none")
        _      <- writeAllAreas(root, noUndated, validMillPlugins)
        report <- SquireChangelog.check(root)
        libraries = report.outcomes.find(_.area == "libraries").get
      yield assert(
        !report.ok && libraries.status == "issue" &&
          libraries.detail.exists(message =>
            message.contains("CHANGELOG.md") && message.contains("no undated release heading")
          )
      )
    }

    "fails naming both when there are two undated headings" in {
      for
        root   <- SquireFixtures.scratch("changelog-check-two")
        _      <- writeAllAreas(root, validLibraries, twoUndated)
        report <- SquireChangelog.check(root)
        millPlugins = report.outcomes.find(_.area == "mill-plugins").get
      yield assert(
        !report.ok && millPlugins.status == "issue" &&
          millPlugins.detail.exists(message =>
            message.contains("0.5.0-M06") && message.contains("0.5.0-M05") &&
              message.contains("expected one undated release heading")
          )
      )
    }

    "fails when the release line is below the area's starting version" in {
      // mill-plugins is the never-published area here: unlike libraries below, it has no real
      // history to protect, but its own floor (see SquireChangelog.Areas) must still be enforced.
      for
        root   <- SquireFixtures.scratch("changelog-check-floor")
        _      <- writeAllAreas(root, validLibraries, belowFloorMillPlugins)
        report <- SquireChangelog.check(root)
        millPlugins = report.outcomes.find(_.area == "mill-plugins").get
      yield assert(
        !report.ok && millPlugins.status == "issue" && millPlugins.releaseLine.exists(_ == "0.0.5") &&
          millPlugins.detail.exists(message => message.contains("0.0.5") && message.contains("0.5.0-M04"))
      )
    }

    "fails when the libraries release line is below the shared floor" in {
      // The libraries are the one stream with real published history (~40 tags): unlike the
      // never-published mill-plugins area, a bad merge in its undated heading must not be free to
      // publish below everything already shipped. See SquireChangelog.Areas.
      for
        root   <- SquireFixtures.scratch("changelog-check-libraries-floor")
        _      <- writeAllAreas(root, belowFloorLibraries, validMillPlugins)
        report <- SquireChangelog.check(root)
        libraries = report.outcomes.find(_.area == "libraries").get
      yield assert(
        !report.ok && libraries.status == "issue" && libraries.releaseLine.exists(_ == "0.4.9") &&
          libraries.detail.exists(message => message.contains("0.4.9") && message.contains("0.5.0-M04"))
      )
    }
  }

  "changelog show" - {
    "prints the release line for each area without enforcing the floor" in {
      for
        root   <- SquireFixtures.scratch("changelog-show")
        _      <- writeAllAreas(root, validLibraries, belowFloorMillPlugins)
        report <- SquireChangelog.show(root)
        rendered    = SquireChangelog.renderChangelogReport(report)
        millPlugins = report.outcomes.find(_.area == "mill-plugins").get
      yield assert(
        report.ok && millPlugins.status == "ok" && millPlugins.releaseLine.exists(_ == "0.0.5") &&
          rendered.contains("release line 1.2.3") && rendered.contains("release line 0.0.5")
      )
    }
  }

  "release prepare" - {
    "dates the undated heading with the date it is given and inserts a real next undated heading above it" in {
      for
        root    <- SquireFixtures.scratch("release-prepare")
        _       <- writeAllAreas(root, preparable, validMillPlugins)
        result  <- SquireChangelog.prepare(root, "libraries", "2026-08-18")
        updated <- Sync.defer(Files.readString((root / "CHANGELOG.md").toJava))
        // The build must be able to read a release line straight out of the file `prepare` just
        // wrote — that is the whole point of not leaving `## [Unreleased]` behind.
        rereadLine = SquireVersion.Changelog.releaseLine(updated, "CHANGELOG.md")
      yield
        val nextIndex  = updated.indexOf("## [0.6.1]")
        val datedIndex = updated.indexOf("## [0.6.0-M01] - 2026-08-18")
        assert(
          result.version == "0.6.0-M01" && result.date == "2026-08-18" && result.tag == "v0.6.0-M01" &&
            result.gitTagCommand == "git tag v0.6.0-M01" && result.nextVersion == "0.6.1" &&
            nextIndex >= 0 && datedIndex > nextIndex &&
            !updated.contains("## [Unreleased]") &&
            !updated.contains("## [0.6.0-M01]\n\n### Added") &&
            updated.contains("## [0.5.0-M04] - 2026-04-22") &&
            rereadLine == Right("0.6.1")
        )
    }

    "refuses, without writing, when the changelog has no undated heading to date" in {
      for
        root   <- SquireFixtures.scratch("release-prepare-refuse")
        _      <- writeAllAreas(root, noUndated, validMillPlugins)
        before <- Sync.defer(Files.readString((root / "CHANGELOG.md").toJava))
        result <- Abort.run[SquireError](SquireChangelog.prepare(root, "libraries", "2026-08-18"))
        after  <- Sync.defer(Files.readString((root / "CHANGELOG.md").toJava))
      yield assert(result.isFailure && before == after)
    }

    "refuses an unknown area and a malformed date without writing" in {
      for
        root        <- SquireFixtures.scratch("release-prepare-bad-input")
        _           <- writeAllAreas(root, validLibraries, validMillPlugins)
        before      <- Sync.defer(Files.readString((root / "CHANGELOG.md").toJava))
        unknownArea <- Abort.run[SquireError](SquireChangelog.prepare(root, "nope", "2026-08-18"))
        badDate     <- Abort.run[SquireError](SquireChangelog.prepare(root, "libraries", "not-a-date"))
        after       <- Sync.defer(Files.readString((root / "CHANGELOG.md").toJava))
      yield assert(unknownArea.isFailure && badDate.isFailure && before == after)
    }
  }

  "release status" - {
    "reports which stream a tag would release and whether tag and changelog agree" in {
      val statusRunner = RuleRunner { request =>
        val pattern = request.argv.lastOption.getOrElse("")
        val stdout  = pattern match
          case "v*"              => ""
          case "mill-plugins/v*" => "mill-plugins/v0.5.0-M05\n"
          case _                 => ""
        ProcessResult(request, 0, stdout, "")
      }
      for
        root   <- SquireFixtures.scratch("release-status")
        _      <- writeAllAreas(root, validLibraries, millPluginsWithRelease)
        report <- SquireChangelog.status(root, statusRunner)
        libraries   = report.areas.find(_.area == "libraries").get
        millPlugins = report.areas.find(_.area == "mill-plugins").get
      yield assert(
        report.ok &&
          libraries.status == "pending" && libraries.tag.exists(_ == "v1.2.3") &&
          millPlugins.status == "released" && millPlugins.tag.exists(_ == "mill-plugins/v0.5.0-M05")
      )
    }

    "reports a tag/changelog mismatch as an issue rather than pending or released" in {
      val statusRunner = RuleRunner { request =>
        val pattern = request.argv.lastOption.getOrElse("")
        val stdout  = pattern match
          case "v*"              => ""
          case "mill-plugins/v*" => "mill-plugins/v9.9.9\n"
          case _                 => ""
        ProcessResult(request, 0, stdout, "")
      }
      for
        root   <- SquireFixtures.scratch("release-status-issue")
        _      <- writeAllAreas(root, validLibraries, validMillPlugins)
        report <- SquireChangelog.status(root, statusRunner)
        millPlugins = report.areas.find(_.area == "mill-plugins").get
      yield assert(
        !report.ok && millPlugins.status == "issue" &&
          millPlugins.detail.exists(message =>
            message.contains("mill-plugins/v9.9.9") && message.contains("does not record")
          )
      )
    }

    "fails rather than reporting every area pending when the git probe itself fails" in {
      val brokenRunner = RuleRunner { request =>
        ProcessResult(request, 128, "", "fatal: not a git repository\n")
      }
      for
        root   <- SquireFixtures.scratch("release-status-git-failure")
        _      <- writeAllAreas(root, validLibraries, validMillPlugins)
        result <- Abort.run[SquireError](SquireChangelog.status(root, brokenRunner))
      yield assert(result match
        case Result.Failure(error) => error.getMessage.contains("not a git repository")
        case _                     => false)
    }
  }

  "squire CLI wiring" - {
    "routes changelog check, show, and release prepare through SquireCli with correct exit codes" in {
      for
        root <- SquireFixtures.scratch("changelog-cli")
        _    <- writeAllAreas(root, preparable, validMillPlugins)
        checkOutput = new StringBuilder
        checkExit <-
          SquireCli.runChangelogCheck(ChangelogCheckOpts(json = true), root, value => checkOutput.append(value))
        showOutput = new StringBuilder
        showExit <- SquireCli.runChangelogShow(ChangelogShowOpts(), root, value => showOutput.append(value))
        prepareOutput = new StringBuilder
        prepareExit <-
          SquireCli.runReleasePrepare(
            ReleasePrepareOpts(area = Some("libraries"), date = Some("2026-08-18")),
            root,
            value => prepareOutput.append(value)
          )
        checkReport = SquireJson.decode[ChangelogReport](checkOutput.result().trim)
      yield assert(
        checkExit == 0 && checkReport.exists(_.ok) &&
          showExit == 0 && showOutput.result().contains("release line 0.6.0-M01") &&
          prepareExit == 0 && prepareOutput.result().contains("git tag v0.6.0-M01")
      )
    }

    "release prepare requires --area" in {
      for
        root   <- SquireFixtures.scratch("changelog-cli-missing-area")
        result <- Abort.run[SquireError](SquireCli.runReleasePrepare(ReleasePrepareOpts(), root, _ => ()))
      yield assert(result.isFailure)
    }
  }

object SquireChangelogFixtures:
  val validLibraries: String =
    """# Changelog
      |
      |## [1.2.3]
      |
      |### Added
      |- widget
      |""".stripMargin

  val belowFloorLibraries: String =
    """# Changelog
      |
      |## [0.4.9]
      |
      |### Added
      |- widget
      |""".stripMargin

  val validMillPlugins: String =
    """# Changelog
      |
      |## [0.5.0-M05]
      |
      |### Changed
      |- widget
      |""".stripMargin

  val belowFloorMillPlugins: String =
    """# Changelog
      |
      |## [0.0.5]
      |
      |### Added
      |- widget
      |""".stripMargin

  val noUndated: String =
    """# Changelog
      |
      |## [1.2.3] - 2026-01-01
      |
      |### Added
      |- widget
      |""".stripMargin

  val twoUndated: String =
    """# Changelog
      |
      |## [0.5.0-M06]
      |
      |## [0.5.0-M05]
      |
      |### Changed
      |- widget
      |
      |## [0.5.0-M04] - 2026-04-22
      |
      |### Added
      |- widget
      |""".stripMargin

  val preparable: String =
    """# Changelog
      |
      |All notable changes are recorded here.
      |
      |## [0.6.0-M01]
      |
      |### Added
      |- widget
      |
      |## [0.5.0-M04] - 2026-04-22
      |
      |### Added
      |- gadget
      |""".stripMargin

  val millPluginsWithRelease: String =
    """# Changelog
      |
      |## [0.5.0-M06]
      |
      |### Changed
      |- widget
      |
      |## [0.5.0-M05] - 2026-06-01
      |
      |### Changed
      |- widget
      |""".stripMargin

  def writeChangelog(root: Path, path: String, text: String): Unit < Sync =
    Sync.defer {
      val target = root / path
      Files.createDirectories(target.parent.get.toJava)
      Files.writeString(target.toJava, text)
    }

  def writeAllAreas(root: Path, libraries: String, millPlugins: String): Unit < Sync =
    for
      _ <- writeChangelog(root, "CHANGELOG.md", libraries)
      _ <- writeChangelog(root, "mill-plugins/morphir/CHANGELOG.md", millPlugins)
    yield ()
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall