Repository navigation
fix(publish): assert registry content before publish (#71) #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srikolagani
wants to merge
2
commits into
salesforce:main
Choose a base branch
from
srikolagani:fix/align-language-publish-pins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| #!/usr/bin/env node | ||
| /* | ||
| * Copyright (c) 2026, Salesforce, Inc. | ||
| * All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * For full license text, see the LICENSE file in the repo root or https://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Pre-publish guard for agentscript issue #71: refuse to publish dependents | ||
| * that would stamp workspace:* onto a reused version whose registry dist | ||
| * differs from local dist. Complementary to verify-published-packages.mjs (PR #72). | ||
| * Run after the scope rewrite in scripts/publish.mjs. | ||
| */ | ||
|
|
||
| import { execFileSync } from "node:child_process"; | ||
| import { createHash } from "node:crypto"; | ||
| import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join, relative } from "node:path"; | ||
|
|
||
| const ROOT = new URL("..", import.meta.url).pathname; | ||
| const INTERNAL_SCOPE = "@agentscript/"; | ||
| const PUBLISH_SCOPE = "@sf-agentscript/"; | ||
|
|
||
| function walk(dir) { | ||
| const out = []; | ||
| for (const entry of readdirSync(dir, { withFileTypes: true })) { | ||
| const full = join(dir, entry.name); | ||
| if (entry.isDirectory()) out.push(...walk(full)); | ||
| else if (entry.isFile()) out.push(full); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function hashDistTree(pkgPath) { | ||
| const dist = join(pkgPath, "dist"); | ||
| if (!existsSync(dist) || !statSync(dist).isDirectory()) return null; | ||
| const files = walk(dist).filter((f) => !f.endsWith(".map")).sort(); | ||
| if (files.length === 0) return null; | ||
| const h = createHash("sha256"); | ||
| for (const file of files) { | ||
| h.update(relative(dist, file).replaceAll(String.fromCharCode(92), "/")); | ||
| h.update(Buffer.from([0])); | ||
| h.update(readFileSync(file)); | ||
| h.update(Buffer.from([0])); | ||
| } | ||
| return h.digest("hex"); | ||
| } | ||
|
|
||
| function publishName(name) { | ||
| if (name.startsWith(PUBLISH_SCOPE)) return name; | ||
| if (name.startsWith(INTERNAL_SCOPE)) return name.replace(INTERNAL_SCOPE, PUBLISH_SCOPE); | ||
| return name; | ||
| } | ||
|
|
||
| function npmMeta(name, version) { | ||
| const url = "https://registry.npmjs.org/" + name.replace("/", "%2F") + "/" + version; | ||
| try { | ||
| const raw = execFileSync("curl", ["-sS", "-f", "-L", url], { encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }); | ||
| return JSON.parse(raw); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function hashRemoteDist(tarballUrl) { | ||
| const dir = mkdtempSync(join(tmpdir(), "as-reg-")); | ||
| const tgz = join(dir, "pkg.tgz"); | ||
| try { | ||
| execFileSync("curl", ["-sS", "-f", "-L", "-o", tgz, tarballUrl], { maxBuffer: 64 * 1024 * 1024 }); | ||
| execFileSync("tar", ["-xzf", tgz, "-C", dir], { stdio: "pipe" }); | ||
| return hashDistTree(join(dir, "package")); | ||
| } finally { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| function readPkgJson(pkgPath) { | ||
| return JSON.parse(readFileSync(join(pkgPath, "package.json"), "utf8")); | ||
| } | ||
|
|
||
| function workspaceScopedDepNames(pkgJson) { | ||
| const deps = { ...(pkgJson.dependencies || {}), ...(pkgJson.optionalDependencies || {}) }; | ||
| return Object.keys(deps).filter((n) => n.startsWith(INTERNAL_SCOPE) || n.startsWith(PUBLISH_SCOPE)); | ||
| } | ||
| console.log("assert registry content"); | ||
| const listOut = execFileSync("pnpm", ["-r", "list", "--json", "--depth", "-1"], { cwd: ROOT, encoding: "utf8" }); | ||
| const workspacePkgs = JSON.parse(listOut); | ||
| const byPublishName = new Map(); | ||
| for (const pkg of workspacePkgs) { | ||
| const json = readPkgJson(pkg.path); | ||
| if (json.private) continue; | ||
| byPublishName.set(publishName(json.name), { path: pkg.path, version: json.version, json }); | ||
| } | ||
|
|
||
| const stale = []; | ||
| const willPublish = []; | ||
|
|
||
| for (const [name, info] of byPublishName) { | ||
| const localHash = hashDistTree(info.path); | ||
| if (!localHash) { | ||
| console.log(" skip " + name + "@" + info.version + " (no dist)"); | ||
| continue; | ||
| } | ||
| const meta = npmMeta(name, info.version); | ||
| if (!meta) { | ||
| willPublish.push(name); | ||
| console.log(" new " + name + "@" + info.version); | ||
| continue; | ||
| } | ||
| const tarball = meta.dist && meta.dist.tarball; | ||
| if (!tarball) { | ||
| console.log(" skip " + name + "@" + info.version + " (no tarball)"); | ||
| continue; | ||
| } | ||
| const remoteHash = hashRemoteDist(tarball); | ||
| if (!remoteHash) { | ||
| console.log(" skip " + name + "@" + info.version + " (remote no dist)"); | ||
| continue; | ||
| } | ||
| if (localHash !== remoteHash) { | ||
| stale.push({ name, version: info.version, localHash, remoteHash }); | ||
| console.log(" STALE " + name + "@" + info.version); | ||
| } else { | ||
| console.log(" ok " + name + "@" + info.version); | ||
| } | ||
| } | ||
|
|
||
| if (stale.length === 0) { | ||
| console.log("No stale version reuse detected."); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const staleNames = new Set(stale.map((s) => s.name)); | ||
| const blockers = []; | ||
| for (const name of willPublish) { | ||
| const info = byPublishName.get(name); | ||
| if (!info) continue; | ||
| for (const depNameRaw of workspaceScopedDepNames(info.json)) { | ||
| const depName = publishName(depNameRaw); | ||
| if (!staleNames.has(depName)) continue; | ||
| const depInfo = byPublishName.get(depName); | ||
| blockers.push({ | ||
| pkg: name + "@" + info.version, | ||
| dep: depName + "@" + (depInfo ? depInfo.version : "?"), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (blockers.length === 0) { | ||
| console.log("warn: reused version with different dist; no publishing dependent"); | ||
| for (const s of stale) console.log(" - " + s.name + "@" + s.version); | ||
| process.exit(0); | ||
| } | ||
| console.error("guard failed: dependency pin targets registry content that differs from local dist"); | ||
| console.error("see salesforce agentscript issue 71"); | ||
| console.error("bad pins:"); | ||
| for (const b of blockers) console.error(" - " + b.pkg + " -> STALE " + b.dep); | ||
| console.error("bump stale package versions so workspace protocol stamps a fresh version"); | ||
| for (const s of stale) console.error(" stale: " + s.name + "@" + s.version); | ||
| process.exit(1); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Treat only confirmed missing versions as unpublished
This catch returns
nullfor HTTP 500, transport failures, and malformed JSON as well as 404. Lines 106–110 then classify an existing stale package as new, leavingstaleempty and exiting successfully.With unchanged local/registry language dist and a new compiler depending on it through
workspace:*, valid mismatched metadata blocks the caller. Changing only the language metadata response to HTTP 500 makes the guard exit 0 and the unmodifiedpublish.mjsreachpnpm -r publish. Curl timeout and malformed JSON also bypass; genuine 404 and matching-content controls pass as expected. If the registry recovers before publishing, the new dependent can be released pinned to the existing stale language version, recreating #71.Please distinguish confirmed HTTP 404 from other failures and abort on HTTP/transport/parse errors or invalid metadata (including absent
dist.tarball). Verified with loopback HTTP and an intercepted publish command; no package was published. The timeout bound was supplied by the test transport, since this curl invocation has none.