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

SquireEnv.scala

View on GitHub
//| scalaVersion: 3.9.0
//| moduleDeps: [SquireModel.scala]

import java.net.{InetAddress, InetSocketAddress, ServerSocket, Socket, SocketException}
import java.nio.ByteBuffer
import java.nio.channels.SeekableByteChannel
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, LinkOption, Path as JavaPath, StandardOpenOption}
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import kyo.*

object SquireEnv:
  enum CheckKind:
    case JvmNetwork, VarFolders

  enum DaemonProbe:
    case Open
    case Sandbox(detail: String)
    case Refused(detail: String)

  final case class CheckResult(ok: Maybe[Boolean], detail: String, durationS: Double) derives Schema

  final case class ClaudeCodeInfo(
      detected: Boolean,
      entrypoint: Maybe[String],
      sessionId: Maybe[String],
      childSession: Boolean
  ) derives Schema

  final case class SettingsSource(path: Maybe[String], present: Boolean) derives Schema

  final case class ClaudeSettings(
      sources: Map[String, SettingsSource],
      sandboxEnabled: Map[String, Maybe[Boolean]],
      networkAllowedDomains: Chunk[String],
      networkDeniedDomains: Chunk[String]
  ) derives Schema

  final case class EnvReport(
      generatedAt: String,
      claudeCode: ClaudeCodeInfo,
      ci: Boolean,
      checks: Map[String, CheckResult],
      sandboxed: Boolean,
      claudeSettings: ClaudeSettings,
      recommendation: Map[String, String]
  ) derives Schema

  trait Platform:
    def environment: Map[String, String]
    def osName: String
    private final def normalizedOsName: String = osName.toLowerCase(java.util.Locale.ROOT)
    final def isWindows: Boolean = normalizedOsName.startsWith("windows")
    final def isMacOS: Boolean   = normalizedOsName.startsWith("mac") || normalizedOsName == "darwin"
    final def isLinux: Boolean   = normalizedOsName.startsWith("linux")
    def home: Path
    def managedSettingsCandidates: Chunk[Path]
    def varFolders: Path
    def jvmTempDirectory: Maybe[Path] = Present(varFolders)
    def now: Instant
    def zone: ZoneId = ZoneId.systemDefault()
    def probeJvmNetwork(timeout: Duration): CheckResult
    def probeDaemon(port: Int): DaemonProbe
    def probePath(directory: Path): Path = directory / s".squire-env-probe-${java.util.UUID.randomUUID()}"
    def createProbe(path: Path): SeekableByteChannel =
      Files.newByteChannel(
        path.toJava,
        StandardOpenOption.CREATE_NEW,
        StandardOpenOption.WRITE,
        LinkOption.NOFOLLOW_LINKS
      )
    def writeProbe(path: Path, channel: SeekableByteChannel): Unit =
      val bytes = ByteBuffer.wrap("squire probe".getBytes(StandardCharsets.UTF_8))
      while bytes.hasRemaining do channel.write(bytes)
    def closeProbe(channel: SeekableByteChannel): Unit = channel.close()
    def deleteProbe(path: Path): Unit = Files.deleteIfExists(path.toJava)

  object LivePlatform extends Platform:
    def environment: Map[String, String] = java.lang.System.getenv().entrySet().toArray.foldLeft(Map.empty[String, String]) {
      case (values, entry: java.util.Map.Entry[String @unchecked, String @unchecked]) => values.updated(entry.getKey, entry.getValue)
      case (values, _)                                                                  => values
    }

    def osName: String = java.lang.System.getProperty("os.name", "")

    def home: Path = Path(java.lang.System.getProperty("user.home"))

    def managedSettingsCandidates: Chunk[Path] =
      Chunk(
        Path("/Library/Application Support/ClaudeCode/managed-settings.json"),
        Path("/etc/claude-code/managed-settings.json")
      )

    def varFolders: Path = Path("/var/folders")

    override def jvmTempDirectory: Maybe[Path] =
      Option(java.lang.System.getProperty("java.io.tmpdir")).filter(_.nonEmpty) match
        case Some(value) => Present(Path(value).toJava.toAbsolutePath.normalize).map(path => Path(path.toString))
        case None        => Absent

    def now: Instant = Instant.now()

    def probeJvmNetwork(timeout: Duration): CheckResult = liveJvmNetwork(timeout)

    def probeDaemon(port: Int): DaemonProbe =
      try
        val socket = new Socket()
        try
          socket.connect(new InetSocketAddress("127.0.0.1", port), timeoutMillis(1.seconds))
          DaemonProbe.Open
        finally socket.close()
      catch
        case error: SecurityException => DaemonProbe.Sandbox(error.getMessage)
        case error: SocketException if Option(error.getMessage).exists(_.toLowerCase.contains("operation not permitted")) =>
          DaemonProbe.Sandbox(error.getMessage)
        case error: java.io.IOException => DaemonProbe.Refused(s"${error.getClass.getSimpleName}: ${error.getMessage}")

  def report(timeout: Duration, platform: Platform = LivePlatform, root: Path = Path(".")): EnvReport < Sync =
    for
      jvmNetwork <- checkResult(CheckKind.JvmNetwork, timeout, platform)
      varFolders <- checkResult(CheckKind.VarFolders, timeout, platform)
      settings   <- readClaudeSettings(platform, root)
    yield
      val sandboxed = jvmNetwork.ok == Present(false)
      EnvReport(
        generatedAt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ").format(platform.now.atZone(platform.zone)),
        claudeCode = ClaudeCodeInfo(
          detected = claudeEnvironmentDetected(platform.environment),
          entrypoint = environmentValue(platform.environment, "CLAUDE_CODE_ENTRYPOINT"),
          sessionId = environmentValue(platform.environment, "CLAUDE_CODE_SESSION_ID"),
          childSession = platform.environment.get("CLAUDE_CODE_CHILD_SESSION").contains("1")
        ),
        ci = platform.environment.get("CI").exists(_.nonEmpty) || platform.environment.get("GITHUB_ACTIONS").exists(_.nonEmpty),
        checks = Map("jvm_network" -> jvmNetwork, "var_folders_writable" -> varFolders),
        sandboxed = sandboxed,
        claudeSettings = settings,
        recommendation = Map("mill_daemon" -> (if sandboxed then "use_no_server" else "ok"))
      )

  def check(kind: CheckKind, timeout: Duration, platform: Platform = LivePlatform): Boolean < Sync =
    checkResult(kind, timeout, platform).map {
      _.ok match
        case Present(false) => false
        case _              => true
    }

  def renderLegacyReport(report: EnvReport): String =
    def nullable(value: Maybe[String]): Structure.Value = value match
      case Present(text) => Structure.Value.Str(text)
      case Absent        => Structure.Value.Null
    def nullableBoolean(value: Maybe[Boolean]): Structure.Value = value match
      case Present(boolean) => Structure.Value.Bool(boolean)
      case Absent           => Structure.Value.Null
    def checkValue(value: CheckResult): Structure.Value = Structure.Value.Record(
      Chunk("ok" -> nullableBoolean(value.ok), "detail" -> Structure.Value.Str(value.detail), "duration_s" -> Structure.Value.Decimal(value.durationS))
    )
    def sourceValue(value: SettingsSource): Structure.Value = Structure.Value.Record(
      Chunk("path" -> nullable(value.path), "present" -> Structure.Value.Bool(value.present))
    )
    def stringArray(values: Chunk[String]): Structure.Value = Structure.Value.Sequence(values.map(Structure.Value.Str(_)))
    def settingSources: Structure.Value = Structure.Value.Record(
      Chunk("managed", "user", "project", "project_local").map(name => name -> sourceValue(report.claudeSettings.sources(name)))
    )
    def sandboxEnabled: Structure.Value = Structure.Value.Record(
      Chunk("managed", "user", "project", "project_local").map(name => name -> nullableBoolean(report.claudeSettings.sandboxEnabled(name)))
    )
    SquireJson.pretty(
      Structure.Value.Record(
        Chunk(
          "generated_at" -> Structure.Value.Str(report.generatedAt),
          "claude_code" -> Structure.Value.Record(Chunk(
            "detected" -> Structure.Value.Bool(report.claudeCode.detected),
            "entrypoint" -> nullable(report.claudeCode.entrypoint),
            "session_id" -> nullable(report.claudeCode.sessionId),
            "child_session" -> Structure.Value.Bool(report.claudeCode.childSession)
          )),
          "ci" -> Structure.Value.Bool(report.ci),
          "checks" -> Structure.Value.Record(Chunk("jvm_network" -> checkValue(report.checks("jvm_network")), "var_folders_writable" -> checkValue(report.checks("var_folders_writable")))),
          "sandboxed" -> Structure.Value.Bool(report.sandboxed),
          "claude_settings" -> Structure.Value.Record(Chunk(
            "sources" -> settingSources,
            "sandbox_enabled" -> sandboxEnabled,
            "network_allowed_domains" -> stringArray(report.claudeSettings.networkAllowedDomains),
            "network_denied_domains" -> stringArray(report.claudeSettings.networkDeniedDomains)
          )),
          "recommendation" -> Structure.Value.Record(Chunk("mill_daemon" -> Structure.Value.Str(report.recommendation("mill_daemon"))))
        )
      )
    )

  private def checkResult(kind: CheckKind, timeout: Duration, platform: Platform): CheckResult < Sync =
    kind match
      case CheckKind.JvmNetwork => Sync.defer(platform.probeJvmNetwork(timeout))
      case CheckKind.VarFolders => checkVarFolders(platform)

  private def checkVarFolders(platform: Platform): CheckResult < Sync =
    Sync.defer {
      platform.jvmTempDirectory match
        case Absent => CheckResult(Absent, "effective JVM temp directory is unavailable — check skipped", 0.0)
        case Present(directory) if !Files.isDirectory(directory.toJava) =>
          CheckResult(Present(false), s"effective JVM temp directory does not exist: $directory", 0.0)
        case Present(directory) =>
          var ownedProbe: Maybe[(Path, SeekableByteChannel)] = Absent
          var result: CheckResult = CheckResult(Present(false), "write probe did not complete", 0.0)
          var cleanupFailure: Maybe[String] = Absent
          try
            val path    = platform.probePath(directory)
            val channel = platform.createProbe(path)
            ownedProbe = Present(path -> channel)
            platform.writeProbe(path, channel)
            result = CheckResult(Present(true), s"JVM temp write probe succeeded at $directory", 0.0)
          catch
            case error: java.io.IOException => result = CheckResult(Present(false), s"${error.getClass.getSimpleName}: ${error.getMessage}", 0.0)
            case error: SecurityException    => result = CheckResult(Present(false), s"${error.getClass.getSimpleName}: ${error.getMessage}", 0.0)
          finally ownedProbe match
            case Present((path, channel)) =>
              try platform.closeProbe(channel)
              catch
                case error: java.io.IOException => cleanupFailure = Present(s"could not clean probe file: ${error.getClass.getSimpleName}: ${error.getMessage}")
                case error: SecurityException    => cleanupFailure = Present(s"could not clean probe file: ${error.getClass.getSimpleName}: ${error.getMessage}")
              try platform.deleteProbe(path)
              catch
                case error: java.io.IOException => cleanupFailure = Present(s"could not clean probe file: ${error.getClass.getSimpleName}: ${error.getMessage}")
                case error: SecurityException    => cleanupFailure = Present(s"could not clean probe file: ${error.getClass.getSimpleName}: ${error.getMessage}")
            case Absent => ()
          cleanupFailure match
            case Present(detail) => CheckResult(Present(false), detail, 0.0)
            case Absent          => result
    }

  private def claudeEnvironmentDetected(environment: Map[String, String]): Boolean =
    Chunk("CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT", "CLAUDE_CODE_SESSION_ID", "CLAUDE_CODE_CHILD_SESSION")
      .exists(key => environment.get(key).exists(_.nonEmpty))

  private def environmentValue(environment: Map[String, String], key: String): Maybe[String] =
    environment.get(key).filter(_.nonEmpty) match
      case Some(value) => Present(value)
      case None        => Absent

  private final case class SettingsFile(sandbox: Maybe[SandboxSettings] = Absent) derives Schema
  private final case class SandboxSettings(
      enabled: Maybe[Boolean] = Absent,
      network: Maybe[NetworkSettings] = Absent
  ) derives Schema
  private final case class NetworkSettings(
      allowedDomains: Chunk[String] = Chunk.empty,
      deniedDomains: Chunk[String] = Chunk.empty
  ) derives Schema

  private final case class LoadedSettings(path: Maybe[Path], settings: Maybe[SettingsFile], present: Boolean)

  private def readClaudeSettings(platform: Platform, root: Path): ClaudeSettings < Sync =
    Sync.defer {
      val managedPath = platform.managedSettingsCandidates.find(path => Files.exists(path.toJava))
      val levels = Chunk(
        "managed" -> loadSettings(managedPath match
          case Some(path) => Present(path)
          case None       => Absent
        ),
        "user" -> loadSettings(Present(platform.home / ".claude" / "settings.json")),
        "project" -> loadSettings(Present(root / ".claude" / "settings.json")),
        "project_local" -> loadSettings(Present(root / ".claude" / "settings.local.json"))
      )
      val sources = levels.map { case (name, loaded) =>
        name -> SettingsSource(loaded.path.map(_.toString), loaded.present)
      }.toMap
      val enabled = levels.map { case (name, loaded) => name -> sandbox(loaded).flatMap(_.enabled) }.toMap
      val allowed = levels.flatMap { case (_, loaded) => sandbox(loaded).flatMap(_.network).map(_.allowedDomains).getOrElse(Chunk.empty) }
      val denied  = levels.flatMap { case (_, loaded) => sandbox(loaded).flatMap(_.network).map(_.deniedDomains).getOrElse(Chunk.empty) }
      ClaudeSettings(sources, enabled, Chunk.from(allowed.toSeq.distinct.sorted), Chunk.from(denied.toSeq.distinct.sorted))
    }

  private def sandbox(loaded: LoadedSettings): Maybe[SandboxSettings] =
    loaded.settings.flatMap(_.sandbox)

  private def loadSettings(path: Maybe[Path]): LoadedSettings =
    path match
      case Absent => LoadedSettings(Absent, Absent, false)
      case Present(value) if !Files.exists(value.toJava) => LoadedSettings(Present(value), Absent, false)
      case Present(value) =>
        val (settings, present) =
          try
            val content = Files.readString(value.toJava)
            SquireJson.decode[SettingsFile](content) match
              case Result.Success(decoded) => (Present(decoded), content.filterNot(_.isWhitespace) != "{}")
              case Result.Failure(_)       => (Absent, false)
          catch case _: java.io.IOException => (Absent, false)
        LoadedSettings(Present(value), settings, present)

  private def timeoutMillis(timeout: Duration): Int =
    math.max(1L, math.min(Int.MaxValue.toLong, timeout.toJava.toMillis)).toInt

  private def liveJvmNetwork(timeout: Duration): CheckResult =
    val started = java.lang.System.nanoTime()
    try
      val server = new ServerSocket()
      try
        server.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0), 1)
        val client = new Socket()
        try
          client.connect(new InetSocketAddress("127.0.0.1", server.getLocalPort), remainingMillis(started, timeout))
          server.setSoTimeout(remainingMillis(started, timeout))
          val accepted = server.accept()
          try CheckResult(Present(true), "loopback bind+accept+connect succeeded", elapsedSeconds(started))
          finally accepted.close()
        finally client.close()
      finally server.close()
    catch
      case _: java.net.SocketTimeoutException => CheckResult(Present(false), s"JVM loopback probe hung past ${timeout.toJava.toMillis}ms timeout — JVM sockets likely blocked by sandbox", elapsedSeconds(started))
      case error: java.io.IOException => CheckResult(Present(false), s"${error.getClass.getSimpleName}: ${error.getMessage}", elapsedSeconds(started))
      case error: SecurityException    => CheckResult(Present(false), s"${error.getClass.getSimpleName}: ${error.getMessage}", elapsedSeconds(started))

  private def elapsedSeconds(started: Long): Double =
    math.round((java.lang.System.nanoTime() - started).toDouble / 1000000.0) / 1000.0

  private def remainingMillis(started: Long, timeout: Duration): Int =
    val elapsed = java.lang.System.nanoTime() - started
    val remaining = timeout.toJava.toMillis - elapsed / 1000000L
    math.max(1L, math.min(Int.MaxValue.toLong, remaining)).toInt
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