Sync upstream v3.2.4: slim install refs, a crash-safe installer, Change project restarts the agent (v3.2.4)

AI Assisted
This commit is contained in:
jparkerweb committed 2026-12-03 15:17:47 -08:00
1 parent 50319aa332
commit f997a82c4d
45 files changed
+7851 -306

No files matched your search

+104 -13
View File
@@ -1,10 +1,13 @@
// Plan2Code Web Console - is a newer Plan2Code release out?
//
// `git ls-remote --tags` needs no API token and no rate-limited REST call,
// rides on the person's own git and proxy setup, and the newest `vX.Y.Z`
// release tag is "latest". The server
// starts this at boot and never waits on it; the answer, failures included, is
// cached for an hour so a burst of sessions asks GitHub once.
// `git ls-remote --tags --heads` needs no API token and no rate-limited REST
// call, rides on the person's own git and proxy setup, and the newest `vX.Y.Z`
// release tag is "latest". The same one snapshot also carries the synthetic
// `install-vX.Y.Z` tags and the branch heads, which the launcher and the
// installer read to pick the ref npx fetches (`chooseReleaseRef()`,
// `installRefForRelease()`). The server starts this at boot and never waits on
// it; the answer, failures included, is cached for an hour so a burst of
// sessions asks GitHub once.
//
// Nothing here reads, stores or sends a credential: git authenticates itself,
// with every prompt turned off so a missing login fails fast instead of
@@ -21,7 +24,11 @@ export const RELEASES_URL = "https://github.com/jparkerweb/plan2code/releases/";
export const CACHE_MS = 60 * 60 * 1000;
const SEMVER = /^\d+\.\d+\.\d+$/;
const TAG_REF = /refs\/tags\/v(\d+\.\d+\.\d+)$/;
// The ref starts the line or follows the sha's whitespace: with `--heads` in the same answer, a
// branch named like `x/refs/tags/v9.9.9` must never read as release 9.9.9.
const TAG_REF = /(?:^|\s)refs\/tags\/v(\d+\.\d+\.\d+)$/;
const SNAPSHOT_LINE = /^([0-9a-f]{40})\s+refs\/(tags|heads)\/(\S+)$/i;
const RELEASE_TAG = /^(install-)?v(\d+\.\d+\.\d+)$/;
/** -1, 0 or 1 comparing two plain X.Y.Z versions numerically; null unless both are X.Y.Z. */
export function compareVersions(a, b) {
@@ -35,9 +42,13 @@ export function compareVersions(a, b) {
}
/**
* The highest `vX.Y.Z` tag in `git ls-remote --tags` output, without the `v`.
* Peeled lines (`^{}`) never match, so an annotated tag counts once, and
* pre-releases or other tag names are ignored. Null when nothing matches.
* The highest `vX.Y.Z` tag in `git ls-remote` output, without the `v`.
* Peeled lines (`^{}`), `install-vX.Y.Z` tags, branch heads, pre-releases and
* other tag names never match, so an annotated tag counts once. Null when
* nothing matches. This is the plain-release compatibility API the console
* notice uses: it keeps its own line scan rather than going through
* `parseLsRemoteSnapshot()`, whose stricter sha check and peeled-line
* evidence would change what it has always answered.
*/
export function latestTag(text) {
if (typeof text !== "string") return null;
@@ -49,6 +60,84 @@ export function latestTag(text) {
return best;
}
/**
* One `git ls-remote --tags --heads` answer, parsed: `{ tags, peeledTags,
* installTags, heads }`. `tags` and `installTags` map a version (`X.Y.Z`) to
* the sha its `vX.Y.Z` or `install-vX.Y.Z` ref line names; `peeledTags` maps a
* tag name (`v3.1.2`, `install-v3.1.2`) to the commit on its `^{}` line, which
* only annotated tags have; `heads` maps a branch name to its commit. Shas are
* lowercased. Malformed lines and tags of neither shape are skipped; anything
* but a string gives an empty snapshot.
*/
export function parseLsRemoteSnapshot(text) {
const snapshot = { tags: {}, peeledTags: {}, installTags: {}, heads: {} };
if (typeof text !== "string") return snapshot;
for (const line of text.split(/\r?\n/)) {
const m = SNAPSHOT_LINE.exec(line.trim());
if (!m) continue;
const sha = m[1].toLowerCase();
if (m[2] === "heads") {
snapshot.heads[m[3]] = sha;
continue;
}
const peeled = m[3].endsWith("^{}");
const name = peeled ? m[3].slice(0, -3) : m[3];
const tag = RELEASE_TAG.exec(name);
if (!tag) continue;
if (peeled) snapshot.peeledTags[name] = sha;
else (tag[1] ? snapshot.installTags : snapshot.tags)[tag[2]] = sha;
}
return snapshot;
}
/** Whether the snapshot advertises the tag `name` (`vX.Y.Z` or `install-vX.Y.Z`): its ref line or its peeled line. */
function hasTag(snapshot, name) {
const m = RELEASE_TAG.exec(name);
const refs = (m[1] ? snapshot.installTags : snapshot.tags) || {};
return Object.hasOwn(refs, m[2]) || Object.hasOwn(snapshot.peeledTags || {}, name);
}
/**
* The ref npx should fetch for the logical release `vX.Y.Z` (`X.Y.Z` reads the
* same): `{ releaseRef: "vX.Y.Z", fetchRef: "install-vX.Y.Z", synthetic: true }`
* when the snapshot advertises the slim install tag, `{ releaseRef, fetchRef:
* "vX.Y.Z", synthetic: false }` when it advertises only the plain tag, and
* null when it advertises neither. Chosen by ref name, never by commit; an
* annotated tag's peeled line counts as evidence of its tag.
*/
export function installRefForRelease(version, snapshot) {
if (typeof version !== "string" || !snapshot) return null;
const bare = version.startsWith("v") ? version.slice(1) : version;
if (!SEMVER.test(bare)) return null;
const releaseRef = `v${bare}`;
if (hasTag(snapshot, `install-${releaseRef}`)) return { releaseRef, fetchRef: `install-${releaseRef}`, synthetic: true };
if (hasTag(snapshot, releaseRef)) return { releaseRef, fetchRef: releaseRef, synthetic: false };
return null;
}
/**
* The release a default update installs: always the numerically newest plain
* `vX.Y.Z`, the same release latestTag() reports to the banner, the launcher
* notice and the app, fetched as its slim `install-vX.Y.Z` when that tag
* exists and as the full plain tag when it does not (a release published
* before its install ref, or whose ref publication failed). Never an older
* release because only it has an install tag: that would roll a user back.
* `{ releaseRef, fetchRef, synthetic, version }` with `version` as `X.Y.Z`, or
* null when the snapshot holds no plain release at all.
*/
export function chooseReleaseRef(snapshot) {
if (!snapshot) return null;
const plain = new Set(Object.keys(snapshot.tags || {}));
for (const name of Object.keys(snapshot.peeledTags || {})) {
const m = RELEASE_TAG.exec(name);
if (m && !m[1]) plain.add(m[2]);
}
const version = [...plain].reduce((best, v) => (best === null || compareVersions(v, best) > 0 ? v : best), null);
if (version === null) return null;
const synthetic = hasTag(snapshot, `install-v${version}`);
return { releaseRef: `v${version}`, fetchRef: synthetic ? `install-v${version}` : `v${version}`, synthetic, version };
}
function readCache(cacheFile, now) {
try {
const cached = JSON.parse(fs.readFileSync(cacheFile, "utf8"));
@@ -73,9 +162,11 @@ function writeCache(cacheFile, data) {
}
/**
* `git ls-remote --tags` against the repo: resolves its stdout, or null when
* git is missing, fails, or is still going after `timeoutMs`. No shell, no
* window, no prompt.
* `git ls-remote --tags --heads` against the repo, one call for the release
* tags, the synthetic install tags and the branch heads: resolves its stdout,
* or null when git is missing, fails, or is still going after `timeoutMs`. No
* shell, no window, no prompt. Readers that want releases only (`latestTag()`)
* skip the head lines.
*/
export function runLsRemote({ timeoutMs = 10000 } = {}) {
return new Promise((resolve) => {
@@ -90,7 +181,7 @@ export function runLsRemote({ timeoutMs = 10000 } = {}) {
if (!process.env.GIT_SSH_COMMAND) env.GIT_SSH_COMMAND = "ssh -o BatchMode=yes";
let child;
try {
child = spawn("git", ["ls-remote", "--tags", REPO_URL], {
child = spawn("git", ["ls-remote", "--tags", "--heads", REPO_URL], {
shell: false,
windowsHide: true,
stdio: ["ignore", "pipe", "ignore"],