Skip to content

fix(sdk): separate latest deployment from official archives - #65

Closed
m4sh-wacker wants to merge 3 commits into
mainfrom
fix/sdk-latest-deployment-archive-policy
Closed

m4sh-wacker wants to merge 3 commits into
mainfrom
fix/sdk-latest-deployment-archive-policy

Conversation

@m4sh-wacker

@m4sh-wacker m4sh-wacker commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Separates continuous latest SDK deployments from immutable milestone SDK archives so ordinary main deployments always publish the latest website, standalone HTML, and /api / /api.js aliases without colliding with archived versions.

  • Base main commit SHA: f3f7b15aac6c7359a92a584f7c3f077207b64928

Root Cause of the Previous Deployment Failure

  1. Ordinary builds wrote directly into published archive directories: scripts/build-sdk.mjs previously wrote into dist/sdk/v1/<package.json version>/ (0.3.3), while package.json stays on the previous release version until a Release Please PR is merged (and Release Please ignores chore(deps): commits because chore is hidden: true).
  2. Bundler change + immutable diff failure: PR chore(deps): bump the monthly-maintenance group with 12 updates #62 (195c13f) bumped vite (8.3.5 -> 8.4.0, updating rolldown 1.2.14 -> 1.3.4), changing the minified bytes of decodebox.js and decodebox.mjs while package.json remained 0.3.3. Consequently, Deploy to GitHub Pages runs 46672589781 and 46692468864 failed on diff -qr "$release" "$target" in .github/workflows/deploy-pages.yml (just as earlier engine PRs fix(encodings): refuse Base45 and Base85 groups that overflow #50 and fix(encodings): refuse punctuation in Punycode digits #51 failed prior to 0.3.2 and 0.3.3).
  3. Vite 8.4.0 standalone HTML regression (discovered during Chromium verification): Vite 8.4.0 changed its internal preload placeholder to /__VITE_PRELOAD__(?:[\da-f]{32})?/g. standalonePlugin() in vite.config.ts replaced only __VITE_PRELOAD__, leaving a trailing 32-hex hash ([]e115532ea529dbadd736874a4545557e) inside dist/decodebox.html and causing a SyntaxError on load.

Why diff -qr "$release" "$target" Was Removed from Website Deployment

In the finalized two-channel architecture, the latest online SDK built from main is intentionally newer than (and allowed to differ from) older official archived SDKs. Comparing a freshly built latest SDK against an archived version directory incorrectly treated the old archive as the expected latest SDK.

  • Removed diff -qr "$release" "$target" from the ordinary main website deployment path in .github/workflows/deploy-pages.yml.
  • Kept all real quality gates active and blocking, plus added a vm.SourceTextModule parse check on dist/decodebox.html before publishing.
  • Kept strict archive immutability checks in both scripts/build-sdk.mjs (--release mode) and scripts/publish-gh-pages.mjs (--mode archive), which refuse to overwrite any existing official archive with differing bytes.

Architecture: Two Independent SDK Channels

1. Latest SDK — Continuous Deployment (main)

  • npm run build:sdk (scripts/build-sdk.mjs in default latest mode) builds decodebox.js, decodebox.mjs, decodebox.d.mts, and SHA256SUMS into staging directory build/sdk/ (gitignored, outside dist/).
  • Publishes only the latest aliases inside dist/:
    • /api, /api.js, /sdk/v1/decodebox.js, /sdk/decodebox.js
    • /sdk/v1/decodebox.esm.js, /sdk/decodebox.mjs
    • /sdk/decodebox.d.mts
  • Never creates or overwrites versioned /sdk/v1/<version>/ archive directories during ordinary deployments.
  • scripts/check-sdk-dist.mjs validates all dist/ aliases against build/sdk/SHA256SUMS, verifies retained official archives and absence of obsolete directories, and runs the Node smoke test.
  • src/sdk/index.ts keeps engineVersion tied to package.json (no artificial version bumping) and adds additive buildId metadata derived from the built Git commit SHA.

2. Official SDK Archives — PATCH=0 Milestone Policy

Only stable releases whose PATCH component is 0 generate and publish an immutable versioned archive under /sdk/v1/{version}/:

Release Archive generated?
0.3.4, 0.3.5, 0.4.1, 0.4.2 No
0.4.0, 0.5.0, 1.0.0, 1.1.0 Yes
  • scripts/build-sdk.mjs --release <tag> validates PATCH === 0 via archivePathFor(), verifies <version> matches the checked-out tag's package.json, builds into a temp dir, verifies SHA256SUMS, refuses to overwrite differing existing archives, and never touches /api or /api.js.

One-Time Archive Cleanup (0.3.1–0.3.3 Removed; 0.2.0 & 0.3.0 Preserved)

  • 0.2.0 and 0.3.0 are preserved byte-for-byte (including SHA256SUMS verification before copying forward) because both are qualifying PATCH === 0 milestone releases.
  • 0.3.1, 0.3.2, and 0.3.3 are removed via OBSOLETE_ARCHIVE_DIRS in scripts/publish-gh-pages.mjs (--mode latest) on the next main deployment because they are pre-policy patch releases (PATCH !== 0). Once removed from gh-pages, latest deployments never generate versioned directories, so they are never restored.
  • No Git tags, GitHub Releases, source files, or latest aliases are deleted.

Release Please Integration

  • Ordinary bug fixes / pending Release Please PR: Merging PRs to main updates the website, decodebox.html, and /api + /api.js; pending Release Please PRs update normally without publishing archives.
  • Patch releases (e.g. v0.3.4): Release Please creates the GitHub Release and .github/actions/standalone-release uploads/verifies the standalone HTML without creating /sdk/v1/0.3.4/ or overwriting latest aliases.
  • Milestone releases (e.g. v0.4.0): Because tags/releases created with GITHUB_TOKEN do not trigger separate push: tags or release workflows, .github/workflows/release-please.yml checks out the exact tag via standalone-release, builds /sdk/v1/<version>/ (--release "$RELEASE_TAG"), verifies SHA256SUMS, and publishes only that archive directory via scripts/publish-gh-pages.mjs --mode archive (never replacing website files or /api / /api.js). Retries are idempotent when the archive already matches and fail closed on content differences.

Concurrency & GitHub Pages Publication Safety

Separate workflow concurrency groups (github-pages in deploy-pages.yml and release-please-${{ github.ref }} in release-please.yml) serialize runs within each workflow, but are not sufficient alone when a release PR merge triggers both workflows concurrently on main. Without cross-workflow coordination, a blind git push --force from whichever finishes second would wipe out either the newly deployed website/aliases or the newly published milestone archive.

To prevent lost updates across workflows:

  • Both deploy-pages.yml (--mode latest) and release-please.yml (--mode archive) share scripts/publish-gh-pages.mjs.
  • Before committing, publish-gh-pages.mjs reads refs/heads/gh-pages (expectedOid), fetches the latest FETCH_HEAD, merges only its owned slice (latest mode: website + aliases onto retained archives; archive mode: /sdk/v1/<version>/ onto the entire live gh-pages snapshot), and pushes atomically with git push --force-with-lease=refs/heads/gh-pages:<expectedOid>.
  • On a CAS conflict (stale info / ref moved), it retries with backoff against the fresh FETCH_HEAD (up to 5 attempts), ensuring neither workflow ever force-pushes a stale snapshot or loses the other's updates.

Filesystem Integrity & CodeQL TOCTOU Resolution

Resolved GitHub Advanced Security CodeQL Alert #32 (js/file-system-race, previously flagged on scripts/sdk-integrity.mjs:26):

  • Replaced lstat() followed by readFile() in scripts/sdk-integrity.mjs (readRegularFile) with handle-based reading: opens the path once using constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0), validates handle.stat().isFile(), performs a post-open lstat (dev, ino) + isFile() check only on platforms where O_NOFOLLOW is unavailable (Windows), and reads directly via handle.readFile().
  • Eliminated pathExists (lstat -> read/readdir/copy) TOCTOU patterns and post-validation pathname reopenings across scripts/sdk-integrity.mjs, scripts/build-sdk.mjs, scripts/check-sdk-dist.mjs, and scripts/publish-gh-pages.mjs (readRegularFileIfExists, readdirIfExists, collectRegularFilesIfExists, writeVerifiedFiles writing validated Buffer payloads directly with flag: 'wx' for exclusive archive creation).
  • verifySha256Manifest validates directory contents in a single collection pass and returns verified { digests, files } buffers reused directly by archive preservation and latest distribution checks without reopening source files.

Test Results

Executed locally on Windows (Node v24.18.0) on latest commit 4b28f027e33397faff0be86891865d55ddddb47b:

  • npm run lint (eslint .): passed (0 errors, 0 warnings)
  • npm run typecheck (tsc --noEmit): passed
  • npm test -- --run: 77 test files, 1,284 tests passed, including src/sdk/sdk.test.ts (6 tests) and scripts/sdk-distribution.test.mjs (11 tests covering PATCH=0 policy, Scenarios A–G, real buildSdk latest/release modes, bidirectional CAS concurrency retries against bare git remotes, non-regular file/symlink/junction rejection, concurrent rename race during readRegularFile, post-validation tamper resistance in copyRegularDirectory/writeVerifiedFiles, and verifySha256Manifest validation)
  • npm run build & npm run check:sdk-dist: passed
  • npm audit --audit-level=high: passed (found 0 vulnerabilities)

GitHub Actions on latest commit 4b28f027e33397faff0be86891865d55ddddb47b:

  • CodeQL (Analyze JavaScript/TypeScript, run 46796753424, analysis 2293640719): passed (results_count: 0, Alert #32 state: fixed)
  • CI (run 46796753442): passed — Lint, typecheck, test, build (22), Lint, typecheck, test, build (24) (including Chrome distribution loading and Java runtime provider exercise), Dependency audit, and Dependency review
  • Documentation quality (run 46796753497): passed — Markdown lint and Link check (advisory)

Previously executed locally against f3f7b15 / initial PR snapshot (3e412d6):

  • Simulated full deployment against origin/gh-pages (67a10ed0): verified 0.2.0 & 0.3.0 preserved byte-for-byte, 0.3.1–0.3.3 removed (HTTP 404), live check-sdk-dist.mjs, check-sdk-browser.mjs (6 latest JS aliases), buildId metadata (f3f7b15aac6c), Chromium loads of index.html, decodebox.html (HTTP & file://), and simulated v0.4.0 milestone archive build/publish/--release browser check.
  • Workflow & docs checks: deploy-pages.yml, release-please.yml, and standalone-release/action.yml passed PyYAML parsing and Git Bash bash -n; verify-release-cli.cjs YAML structure assertions verified via PyYAML and sdk-paths.mjs fixture check.

Remaining Risks

  • First deployment cleanup: On the first main deployment after merge, /sdk/v1/0.3.1, /sdk/v1/0.3.2, and /sdk/v1/0.3.3 will be removed from gh-pages (by design under Phase 2); external links pinning those three pre-policy patch URLs will receive 404.
  • Post-merge workflows: Live deploy-pages.yml (main push / CDN propagation), release-please.yml, and .github/scripts/verify-release-cli.cjs / verify-release-notes.cjs only run after merge or tag/release events on main.
  • Tag-checkout behavior for pre-existing tags: standalone-release checks out the exact tagged commit, so rebuilding an old pre-PR tag runs that tag's own scripts, whereas future tags from main use the new PATCH=0 archive and Vite 8.4.0 preload handling.

Comment thread scripts/sdk-integrity.mjs Fixed
@m4sh-wacker m4sh-wacker closed this Dec 2, 2026
@m4sh-wacker
m4sh-wacker deleted the fix/sdk-latest-deployment-archive-policy branch December 2, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants