mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-01 19:25:10 +02:00
e3c961d00f
* fix(ship): detect + repair VERSION/package.json drift in Step 12
/ship Step 12's idempotency check read only VERSION and its bump
action wrote only VERSION. package.json's version field was never
updated, so the first bump silently drifted and re-runs couldn't
see it (they keyed on VERSION alone). Any consumer reading
package.json (bun pm, npm publish, registry UIs) saw a stale semver.
Rewrites Step 12 as a four-state dispatch:
FRESH → normal bump, writes VERSION + package.json in sync
ALREADY_BUMPED → skip, reuse current VERSION
DRIFT_STALE_PKG → sync-only repair path, no re-bump (prevents
double-bump on re-run)
DRIFT_UNEXPECTED → halt and ask user (pkg edited manually,
ambiguous which value is authoritative)
Hardening: NEW_VERSION validated against MAJOR.MINOR.PATCH.MICRO
pattern before any write; node-or-bun required for JSON parsing
(no sed fallback — unsafe on nested "version" fields); invalid
JSON fails hard instead of silently corrupting.
Adds test/ship-version-sync.test.ts with 12 cases covering every
state transition, including the critical drift-repair regression
that verifies sync does not double-bump (the bug Codex caught in
the plan review of my own original fix).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore(ship): regenerate SKILL.md + refresh golden fixtures
Mechanical follow-on from the Step 12 template edit. `bun run
gen:skill-docs --host all` regenerates ship/SKILL.md; host-config
golden-file regression tests then need fresh baselines copied
from the regenerated claude/codex/factory host variants.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(ship): harden Step 12 against whitespace + invalid REPAIR_VERSION
Claude adversarial subagent surfaced three correctness risks in the
Step 12 state machine:
- CURRENT_VERSION and BASE_VERSION were not stripped of CR/whitespace
on read. A CRLF VERSION file would mismatch the clean package.json
version, falsely classify as DRIFT_STALE_PKG, then propagate the
carriage return into package.json via the repair path.
- REPAIR_VERSION was unvalidated. The bump path validates NEW_VERSION
against the 4-digit semver pattern, but the drift-repair path wrote
whatever cat VERSION returned directly into package.json. A
manually-corrupted VERSION file would silently poison the repair.
- Empty-string CURRENT_VERSION (0-byte VERSION, directory-at-VERSION)
fell through to "not equal to base" and misclassified as
ALREADY_BUMPED.
Template fix strips \r/newlines/whitespace on every VERSION read,
guards against empty-string results, and applies the same semver
regex gate in the repair path that already protects the bump path.
Adds two regression tests (trailing-CR idempotency + invalid-semver
repair rejection). Total Step 12 coverage: 14 tests, 14/14 pass.
Opens two follow-up TODOs flagged but not fixed in this branch:
test/template drift risk (the tests still reimplement template bash)
and BASE_VERSION silent fallback on git-show failure.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore(ship): regenerate SKILL.md + refresh goldens after hardening
Mechanical follow-on from the whitespace + REPAIR_VERSION validation
edits to ship/SKILL.md.tmpl. bun run gen:skill-docs --host all
regenerates ship/SKILL.md; host-config golden-file regression tests
need fresh baselines copied from the regenerated claude/codex/factory
host variants.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore: bump version and changelog (v1.0.1.0)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
225 lines
8.6 KiB
TypeScript
225 lines
8.6 KiB
TypeScript
// /ship Step 12: VERSION ↔ package.json drift detection + repair.
|
|
// Mirrors the bash blocks in ship/SKILL.md.tmpl Step 12. When the template
|
|
// changes, update both sides together.
|
|
//
|
|
// Coverage gap: node-absent + bun-present path. Simulating "no node" in-process
|
|
// is flaky across dev machines; covered by manual spot-check + CI running on
|
|
// bun-only images if/when we add them.
|
|
|
|
import { test, expect, beforeEach, afterEach } from "bun:test";
|
|
import { execSync } from "node:child_process";
|
|
import { mkdtempSync, writeFileSync, readFileSync, rmSync, existsSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
let dir: string;
|
|
beforeEach(() => {
|
|
dir = mkdtempSync(join(tmpdir(), "ship-drift-"));
|
|
});
|
|
afterEach(() => {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
const writeFiles = (files: Record<string, string>) => {
|
|
for (const [name, content] of Object.entries(files)) {
|
|
writeFileSync(join(dir, name), content);
|
|
}
|
|
};
|
|
|
|
const pkgJson = (version: string | null, extra: Record<string, unknown> = {}) =>
|
|
JSON.stringify(
|
|
version === null ? { name: "x", ...extra } : { name: "x", version, ...extra },
|
|
null,
|
|
2,
|
|
) + "\n";
|
|
|
|
const idempotency = (base: string): { stdout: string; code: number } => {
|
|
const script = `
|
|
cd "${dir}" || exit 2
|
|
BASE_VERSION="${base}"
|
|
CURRENT_VERSION=$(cat VERSION 2>/dev/null | tr -d '\\r\\n[:space:]' || echo "0.0.0.0")
|
|
[ -z "$CURRENT_VERSION" ] && CURRENT_VERSION="0.0.0.0"
|
|
PKG_VERSION=""
|
|
PKG_EXISTS=0
|
|
if [ -f package.json ]; then
|
|
PKG_EXISTS=1
|
|
if command -v node >/dev/null 2>&1; then
|
|
PKG_VERSION=$(node -e 'const p=require("./package.json");process.stdout.write(p.version||"")' 2>/dev/null)
|
|
PARSE_EXIT=$?
|
|
elif command -v bun >/dev/null 2>&1; then
|
|
PKG_VERSION=$(bun -e 'const p=require("./package.json");process.stdout.write(p.version||"")' 2>/dev/null)
|
|
PARSE_EXIT=$?
|
|
else
|
|
echo "ERROR: no parser"; exit 1
|
|
fi
|
|
if [ "$PARSE_EXIT" != "0" ]; then
|
|
echo "ERROR: invalid JSON"; exit 1
|
|
fi
|
|
fi
|
|
if [ "$CURRENT_VERSION" = "$BASE_VERSION" ]; then
|
|
if [ "$PKG_EXISTS" = "1" ] && [ -n "$PKG_VERSION" ] && [ "$PKG_VERSION" != "$CURRENT_VERSION" ]; then
|
|
echo "STATE: DRIFT_UNEXPECTED"; exit 1
|
|
fi
|
|
echo "STATE: FRESH"
|
|
else
|
|
if [ "$PKG_EXISTS" = "1" ] && [ -n "$PKG_VERSION" ] && [ "$PKG_VERSION" != "$CURRENT_VERSION" ]; then
|
|
echo "STATE: DRIFT_STALE_PKG"
|
|
else
|
|
echo "STATE: ALREADY_BUMPED"
|
|
fi
|
|
fi`;
|
|
try {
|
|
const stdout = execSync(script, { shell: "/bin/bash", encoding: "utf8" });
|
|
return { stdout: stdout.trim(), code: 0 };
|
|
} catch (e: any) {
|
|
return { stdout: (e.stdout || "").toString().trim(), code: e.status ?? 1 };
|
|
}
|
|
};
|
|
|
|
const bump = (newVer: string): { code: number } => {
|
|
const script = `
|
|
cd "${dir}" || exit 2
|
|
NEW_VERSION="${newVer}"
|
|
if ! printf '%s' "$NEW_VERSION" | grep -qE '^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$'; then
|
|
echo "invalid semver" >&2; exit 1
|
|
fi
|
|
echo "$NEW_VERSION" > VERSION
|
|
if [ -f package.json ]; then
|
|
node -e 'const fs=require("fs"),p=require("./package.json");p.version=process.argv[1];fs.writeFileSync("package.json",JSON.stringify(p,null,2)+"\\n")' "$NEW_VERSION"
|
|
fi`;
|
|
try {
|
|
execSync(script, { shell: "/bin/bash", stdio: "pipe" });
|
|
return { code: 0 };
|
|
} catch (e: any) {
|
|
return { code: e.status ?? 1 };
|
|
}
|
|
};
|
|
|
|
const syncRepair = (): { code: number } => {
|
|
const script = `
|
|
cd "${dir}" || exit 2
|
|
REPAIR_VERSION=$(cat VERSION | tr -d '\\r\\n[:space:]')
|
|
if ! printf '%s' "$REPAIR_VERSION" | grep -qE '^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$'; then
|
|
echo "invalid repair semver" >&2; exit 1
|
|
fi
|
|
node -e 'const fs=require("fs"),p=require("./package.json");p.version=process.argv[1];fs.writeFileSync("package.json",JSON.stringify(p,null,2)+"\\n")' "$REPAIR_VERSION"`;
|
|
try {
|
|
execSync(script, { shell: "/bin/bash", stdio: "pipe" });
|
|
return { code: 0 };
|
|
} catch (e: any) {
|
|
return { code: e.status ?? 1 };
|
|
}
|
|
};
|
|
|
|
const pkgVersion = () =>
|
|
JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version;
|
|
|
|
// --- Idempotency classification: 6 cases ---
|
|
|
|
test("FRESH: VERSION == base, pkg synced", () => {
|
|
writeFiles({ VERSION: "0.0.0.0\n", "package.json": pkgJson("0.0.0.0") });
|
|
expect(idempotency("0.0.0.0")).toEqual({ stdout: "STATE: FRESH", code: 0 });
|
|
});
|
|
|
|
test("FRESH: VERSION == base, no package.json", () => {
|
|
writeFiles({ VERSION: "0.0.0.0\n" });
|
|
expect(idempotency("0.0.0.0")).toEqual({ stdout: "STATE: FRESH", code: 0 });
|
|
});
|
|
|
|
test("ALREADY_BUMPED: VERSION ahead, pkg synced", () => {
|
|
writeFiles({ VERSION: "0.1.0.0\n", "package.json": pkgJson("0.1.0.0") });
|
|
expect(idempotency("0.0.0.0")).toEqual({ stdout: "STATE: ALREADY_BUMPED", code: 0 });
|
|
});
|
|
|
|
test("ALREADY_BUMPED: VERSION ahead, no package.json", () => {
|
|
writeFiles({ VERSION: "0.1.0.0\n" });
|
|
expect(idempotency("0.0.0.0")).toEqual({ stdout: "STATE: ALREADY_BUMPED", code: 0 });
|
|
});
|
|
|
|
test("DRIFT_STALE_PKG: VERSION ahead, pkg stale", () => {
|
|
writeFiles({ VERSION: "0.1.0.0\n", "package.json": pkgJson("0.0.0.0") });
|
|
expect(idempotency("0.0.0.0")).toEqual({ stdout: "STATE: DRIFT_STALE_PKG", code: 0 });
|
|
});
|
|
|
|
test("DRIFT_UNEXPECTED: VERSION == base, pkg edited (exits non-zero)", () => {
|
|
writeFiles({ VERSION: "0.0.0.0\n", "package.json": pkgJson("0.5.0.0") });
|
|
const r = idempotency("0.0.0.0");
|
|
expect(r.stdout.startsWith("STATE: DRIFT_UNEXPECTED")).toBe(true);
|
|
expect(r.code).toBe(1);
|
|
});
|
|
|
|
// --- Parse failures: 2 cases ---
|
|
|
|
test("idempotency: invalid JSON exits non-zero with clear error", () => {
|
|
writeFiles({ VERSION: "0.1.0.0\n", "package.json": "{ not valid" });
|
|
const r = idempotency("0.0.0.0");
|
|
expect(r.code).toBe(1);
|
|
expect(r.stdout).toContain("invalid JSON");
|
|
});
|
|
|
|
test("idempotency: package.json with no version field treated as <none>", () => {
|
|
writeFiles({ VERSION: "0.1.0.0\n", "package.json": pkgJson(null) });
|
|
// PKG_VERSION is empty → drift check skipped → ALREADY_BUMPED
|
|
expect(idempotency("0.0.0.0")).toEqual({ stdout: "STATE: ALREADY_BUMPED", code: 0 });
|
|
});
|
|
|
|
// --- Bump: 3 cases ---
|
|
|
|
test("bump: writes VERSION and package.json in sync", () => {
|
|
writeFiles({ VERSION: "0.0.0.0\n", "package.json": pkgJson("0.0.0.0") });
|
|
expect(bump("0.1.0.0").code).toBe(0);
|
|
expect(readFileSync(join(dir, "VERSION"), "utf8").trim()).toBe("0.1.0.0");
|
|
expect(pkgVersion()).toBe("0.1.0.0");
|
|
});
|
|
|
|
test("bump: rejects invalid NEW_VERSION", () => {
|
|
writeFiles({ VERSION: "0.0.0.0\n", "package.json": pkgJson("0.0.0.0") });
|
|
const r = bump("not-a-version");
|
|
expect(r.code).toBe(1);
|
|
// VERSION is unchanged — validation runs before any write.
|
|
expect(readFileSync(join(dir, "VERSION"), "utf8").trim()).toBe("0.0.0.0");
|
|
});
|
|
|
|
test("bump: no package.json is silent", () => {
|
|
writeFiles({ VERSION: "0.0.0.0\n" });
|
|
expect(bump("0.1.0.0").code).toBe(0);
|
|
expect(readFileSync(join(dir, "VERSION"), "utf8").trim()).toBe("0.1.0.0");
|
|
expect(existsSync(join(dir, "package.json"))).toBe(false);
|
|
});
|
|
|
|
// --- Adversarial review regressions: trailing whitespace + invalid REPAIR_VERSION ---
|
|
|
|
test("trailing CR in VERSION does not cause false DRIFT_STALE_PKG", () => {
|
|
// Before the tr-strip fix, VERSION="0.1.0.0\r" read via cat would mismatch
|
|
// pkg.version="0.1.0.0" and classify as DRIFT_STALE_PKG, then repair would
|
|
// write garbage \r into package.json. Now CURRENT_VERSION is stripped.
|
|
writeFileSync(join(dir, "VERSION"), "0.1.0.0\r\n");
|
|
writeFileSync(join(dir, "package.json"), pkgJson("0.1.0.0"));
|
|
expect(idempotency("0.0.0.0")).toEqual({ stdout: "STATE: ALREADY_BUMPED", code: 0 });
|
|
});
|
|
|
|
test("DRIFT REPAIR rejects invalid VERSION semver instead of propagating", () => {
|
|
// If VERSION is corrupted/manually-edited to something non-semver, the
|
|
// repair path must refuse rather than writing junk into package.json.
|
|
writeFileSync(join(dir, "VERSION"), "not-a-semver\n");
|
|
writeFileSync(join(dir, "package.json"), pkgJson("0.0.0.0"));
|
|
const r = syncRepair();
|
|
expect(r.code).toBe(1);
|
|
// package.json must NOT have been overwritten with the garbage.
|
|
expect(pkgVersion()).toBe("0.0.0.0");
|
|
});
|
|
|
|
// --- THE critical regression test: drift-repair does NOT double-bump ---
|
|
|
|
test("DRIFT REPAIR: sync path syncs pkg to VERSION without re-bumping", () => {
|
|
// Simulate a prior /ship that bumped VERSION but failed to touch package.json.
|
|
writeFiles({ VERSION: "0.1.0.0\n", "package.json": pkgJson("0.0.0.0") });
|
|
// Idempotency classifies as DRIFT_STALE_PKG.
|
|
expect(idempotency("0.0.0.0").stdout).toBe("STATE: DRIFT_STALE_PKG");
|
|
// Sync-only repair runs — no re-bump.
|
|
expect(syncRepair().code).toBe(0);
|
|
// VERSION is unchanged. package.json now matches VERSION. No 0.2.0.0.
|
|
expect(readFileSync(join(dir, "VERSION"), "utf8").trim()).toBe("0.1.0.0");
|
|
expect(pkgVersion()).toBe("0.1.0.0");
|
|
});
|