;; ---------------------------------------------------------------------------
;; SX CI Pipeline
;; ---------------------------------------------------------------------------

(defcomp ()
  (~docs/page :title "SX CI Pipeline"

    (p (~tw :tokens "text-stone-500 text-sm italic mb-8")
      "Build, test, and deploy Rose Ash using the same language the application is written in.")

    (~docs/section :title "Context" :id "context"
      (p (~tw :tokens "text-stone-600")
        "Rose Ash currently uses shell scripts for CI: " (code "deploy.sh") " auto-detects changed services via git diff, builds Docker images, pushes to the registry, and restarts Swarm services. " (code "dev.sh") " starts the dev environment and runs tests. These work, but they are opaque imperative scripts with no reuse, no composition, and no relationship to SX.")
      (p (~tw :tokens "text-stone-600")
        "The CI pipeline is the last piece of infrastructure not expressed in s-expressions. Fixing that completes the \"one representation for everything\" claim — the same language that defines the spec, the components, the pages, the essays, and the deployment config also defines the build pipeline."))

    (~docs/section :title "Design" :id "design"
      (p (~tw :tokens "text-stone-600")
        "Pipeline definitions are " (code ".sx") " files. A minimal Python CLI runner evaluates them using " (code "sx_ref.py") ". CI-specific IO primitives (shell execution, Docker, git) are boundary-declared and only available to the pipeline runner — never to web components.")
      (~docs/code :src (highlight ";; pipeline/deploy.sx\n(let ((targets (if (= (length ARGS) 0)\n                 (~plans/sx-ci/detect-changed :base \"HEAD~1\")\n                 (filter (fn (svc) (some (fn (a) (= a (get svc \"name\"))) ARGS))\n                   services))))\n  (when (= (length targets) 0)\n    (log-step \"No changes detected\")\n    (exit 0))\n\n  (log-step (str \"Deploying: \" (join \" \" (map (fn (s) (get s \"name\")) targets))))\n\n  ;; Tests first\n  (~unit-tests)\n  (~sx-spec-tests)\n\n  ;; Build, push, restart\n  (for-each (fn (svc) (~plans/sx-ci/build-service :service svc)) targets)\n  (for-each (fn (svc) (~restart-service :service svc)) targets)\n\n  (log-step \"Deploy complete\"))" "lisp"))
      (p (~tw :tokens "text-stone-600")
        "Pipeline steps are components. " (code "~unit-tests") ", " (code "~plans/sx-ci/build-service") ", " (code "~plans/sx-ci/detect-changed") " are " (code "defcomp") " definitions that compose by nesting — the same mechanism used for page layouts, navigation, and every other piece of the system."))

    (~docs/section :title "CI Primitives" :id "primitives"
      (p (~tw :tokens "text-stone-600")
        "New IO primitives declared in " (code "boundary.sx") ", implemented only in the CI runner context:")
      (div (~tw :tokens "overflow-x-auto rounded border border-stone-200 mb-4")
        (table (~tw :tokens "w-full text-left text-sm")
          (thead (tr (~tw :tokens "border-b border-stone-200 bg-stone-100")
            (th (~tw :tokens "px-3 py-2 font-medium text-stone-600") "Primitive")
            (th (~tw :tokens "px-3 py-2 font-medium text-stone-600") "Signature")
            (th (~tw :tokens "px-3 py-2 font-medium text-stone-600") "Purpose")))
          (tbody
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "shell-run")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(command) -> dict")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Execute shell command, return " (code "{:exit N :stdout \"...\" :stderr \"...\"}") ""))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "shell-run!")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(command) -> dict")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Execute shell command, throw on non-zero exit"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "docker-build")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(&key file tag context) -> nil")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Build Docker image"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "docker-push")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(tag) -> nil")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Push image to registry"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "docker-restart")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(service) -> nil")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Restart Swarm service"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "git-diff-files")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(base head) -> list")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "List changed files between commits"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "git-branch")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "() -> string")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Current branch name"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "log-step")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(message) -> nil")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Formatted pipeline output"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "fail!")
              (td (~tw :tokens "px-3 py-2 font-mono text-xs text-stone-600") "(message) -> nil")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Abort pipeline with error")))))
      (p (~tw :tokens "text-stone-600")
        "The boundary system ensures these primitives are " (em "only") " available in the CI context. Web components cannot call " (code "shell-run!") " — the evaluator will refuse to resolve the symbol, just as it refuses to resolve any other unregistered IO primitive. The sandbox is structural, not a convention."))

    (~docs/section :title "Reusable Steps" :id "steps"
      (p (~tw :tokens "text-stone-600")
        "Pipeline steps are components — same " (code "defcomp") " as UI components, same " (code "&key") " params, same composition by nesting:")
      (~docs/code :src (highlight "(defcomp ~plans/sx-ci/detect-changed (&key base)\n  (let ((files (git-diff-files (or base \"HEAD~1\") \"HEAD\")))\n    (if (some (fn (f) (starts-with? f \"shared/\")) files)\n      services\n      (filter (fn (svc)\n        (some (fn (f) (starts-with? f (str (get svc \"dir\") \"/\"))) files))\n        services))))\n\n(defcomp ~plans/sx-ci/build-service (&key service)\n  (let ((name (get service \"name\"))\n        (tag (str registry \"/\" name \":latest\")))\n    (log-step (str \"Building \" name))\n    (docker-build :file (str (get service \"dir\") \"/Dockerfile\") :tag tag :context \".\")\n    (docker-push tag)))\n\n(defcomp ~plans/sx-ci/bootstrap-check ()\n  (log-step \"Checking bootstrapped files are up to date\")\n  (shell-run! \"python shared/sx/ref/bootstrap_js.py\")\n  (shell-run! \"python shared/sx/ref/bootstrap_py.py\")\n  (let ((diff (shell-run \"git diff --name-only shared/static/scripts/sx-ref.js shared/sx/ref/sx_ref.py\")))\n    (when (not (= (get diff \"stdout\") \"\"))\n      (fail! \"Bootstrapped files are stale — rebootstrap and commit\"))))" "lisp"))
      (p (~tw :tokens "text-stone-600")
        "Compare this to GitHub Actions YAML, where \"reuse\" means composite actions with " (code "uses:") " references, input/output mappings, shell script blocks inside YAML strings, and a " (a :href "https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions" (~tw :tokens "text-violet-600 hover:underline") "100-page syntax reference") ". SX pipeline reuse is function composition. That is all it has ever been."))

    (~docs/section :title "Pipelines" :id "pipelines"
      (p (~tw :tokens "text-stone-600")
        "Two primary pipelines, each a single " (code ".sx") " file:")
      (div (~tw :tokens "space-y-4")
        (div (~tw :tokens "rounded border border-stone-200 p-4")
          (h4 (~tw :tokens "font-semibold text-stone-700 mb-2") "pipeline/test.sx")
          (p (~tw :tokens "text-sm text-stone-600") "Unit tests, SX spec tests (Python + Node), bootstrap staleness check, Tailwind CSS check. Run locally or in CI.")
          (p (~tw :tokens "text-sm font-mono text-violet-700 mt-1") "python -m shared.sx.ci pipeline/test.sx"))
        (div (~tw :tokens "rounded border border-stone-200 p-4")
          (h4 (~tw :tokens "font-semibold text-stone-700 mb-2") "pipeline/deploy.sx")
          (p (~tw :tokens "text-sm text-stone-600") "Auto-detect changed services (or accept explicit args), run tests, build Docker images, push to registry, restart Swarm services.")
          (p (~tw :tokens "text-sm font-mono text-violet-700 mt-1") "python -m shared.sx.ci pipeline/deploy.sx blog market"))))

    (~docs/section :title "Why this matters" :id "why"
      (p (~tw :tokens "text-stone-600")
        "CI pipelines are the strongest test case for \"one representation for everything.\" GitHub Actions, GitLab CI, CircleCI — all use YAML. YAML is not a programming language. So every CI system reinvents conditionals (" (code "if:") " expressions evaluated as strings), iteration (" (code "matrix:") " strategies), composition (" (code "uses:") " references with input/output schemas), and error handling (" (code "continue-on-error:") " booleans) — all in a data format that was never designed for any of it.")
      (p (~tw :tokens "text-stone-600")
        "The result is a domain-specific language trapped inside YAML, with worse syntax than any language designed to be one. Every CI pipeline of sufficient complexity becomes a programming task performed in a notation that actively resists programming.")
      (p (~tw :tokens "text-stone-600")
        "SX pipelines use real conditionals, real functions, real composition, and real error handling — because SX is a real language. The pipeline definition and the application code are the same thing. An AI that can generate SX components can generate SX pipelines. A developer who reads SX pages can read SX deploys. The representation is universal."))

    (~docs/section :title "Files" :id "files"
      (div (~tw :tokens "overflow-x-auto rounded border border-stone-200")
        (table (~tw :tokens "w-full text-left text-sm")
          (thead (tr (~tw :tokens "border-b border-stone-200 bg-stone-100")
            (th (~tw :tokens "px-3 py-2 font-medium text-stone-600") "File")
            (th (~tw :tokens "px-3 py-2 font-medium text-stone-600") "Purpose")))
          (tbody
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "shared/sx/ci.py")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Pipeline runner CLI (~150 lines)"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "shared/sx/ci_primitives.py")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "CI IO primitive implementations"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "pipeline/services.sx")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Service registry (data)"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "pipeline/steps.sx")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Reusable step components"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "pipeline/test.sx")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Test pipeline"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "pipeline/deploy.sx")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Deploy pipeline"))
            (tr (~tw :tokens "border-b border-stone-100")
              (td (~tw :tokens "px-3 py-2 font-mono text-sm text-violet-700") "shared/sx/ref/boundary.sx")
              (td (~tw :tokens "px-3 py-2 text-stone-700") "Add CI primitive declarations"))))))))


;; ---------------------------------------------------------------------------
;; Live Streaming — SSE & WebSocket
;; ---------------------------------------------------------------------------
