GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #13479 from developing-today/fix/dc-promote-idempo… · developing-today/code@fa4b1d5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pkgs/desktop-commander/worker
    • promote.d.mts
    • promote.mjs
    • test
      • promote.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pkgs/desktop-commander/worker/promote.d.mts‎

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Type surface for promote.mjs (a plain Node script; vitest tests import
2+
// its pure helper). deploymentStatus classifies the live deployment
3+
// against a provenance manifest: "already-promoted" when the manifest
4+
// version is already the sole 100% deployment, "at-baseline" when the
5+
// active version matches the manifest baseline, "drifted" otherwise.
6+
export type DeploymentStatus = "already-promoted" | "at-baseline" | "drifted";
7+
8+
export interface ManifestVersionEntry {
9+
version_id?: string;
10+
percentage?: number;
11+
}
12+
13+
export interface ManifestDeployment {
14+
versions?: ManifestVersionEntry[];
15+
}
16+
17+
export interface PromotionManifest {
18+
versionId: string;
19+
baselineVersionId: string;
20+
}
21+
22+
export function deploymentStatus(
23+
current: ManifestDeployment | undefined,
24+
manifest: PromotionManifest,
25+
): DeploymentStatus;
Collapse file

‎pkgs/desktop-commander/worker/promote.mjs‎

Copy file name to clipboardExpand all lines: pkgs/desktop-commander/worker/promote.mjs
+96-65Lines changed: 96 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// version to 100% of traffic. The version must come from a provenance
33
// manifest emitted by upload-version.mjs in a successful publish run
44
// that verify-upload-run.mjs has already vouched for, and the live
5-
// deployment must still sit at the manifest's baseline — any drift
6-
// aborts the promotion instead of guessing.
5+
// deployment must either still sit at the manifest's baseline or
6+
// already serve exactly this version at 100% (an idempotent re-run);
7+
// any other drift aborts the promotion instead of guessing.
78
// Usage: promote.mjs --manifest <provenance.json> --promote
89
import { execFileSync } from "node:child_process";
910
import { existsSync, readFileSync } from "node:fs";
@@ -13,77 +14,103 @@ import { fileURLToPath } from "node:url";
1314
const here = dirname(fileURLToPath(import.meta.url));
1415
const cfBin = join(here, "node_modules", ".bin", "cf");
1516

16-
const args = process.argv.slice(2);
17-
const manifestFlag = args.indexOf("--manifest");
18-
const manifestPath = manifestFlag >= 0 ? args[manifestFlag + 1] : undefined;
19-
const rest = args.filter((_, i) => i !== manifestFlag && i !== manifestFlag + 1);
20-
if (!manifestPath || rest.length !== 1 || rest[0] !== "--promote") {
21-
console.error("usage: promote.mjs --manifest <provenance.json> --promote");
22-
process.exit(2);
17+
// Classify the live deployment against the manifest without side
18+
// effects: "already-promoted" when the manifest version is already the
19+
// sole version at 100% of traffic, "at-baseline" when promotion may
20+
// proceed, "drifted" for anything else. Pure, for unit tests.
21+
export function deploymentStatus(current, manifest) {
22+
const versions = current?.versions ?? [];
23+
if (
24+
versions.length === 1 &&
25+
versions[0]?.version_id === manifest.versionId &&
26+
versions[0]?.percentage === 100
27+
) {
28+
return "already-promoted";
29+
}
30+
if (versions[0]?.version_id === manifest.baselineVersionId) {
31+
return "at-baseline";
32+
}
33+
return "drifted";
2334
}
2435

25-
const config = JSON.parse(readFileSync(join(here, "config.json"), "utf8"));
26-
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
27-
const verified = JSON.parse(readFileSync(requireEnv("DC_WORKER_VERIFIED_RUN_FILE"), "utf8"));
28-
const token = requireEnv("DC_WORKER_CF_TOKEN");
29-
requireEnv("CLOUDFLARE_ACCOUNT_ID");
36+
function main() {
37+
const args = process.argv.slice(2);
38+
const manifestFlag = args.indexOf("--manifest");
39+
const manifestPath = manifestFlag >= 0 ? args[manifestFlag + 1] : undefined;
40+
const rest = args.filter((_, i) => i !== manifestFlag && i !== manifestFlag + 1);
41+
if (!manifestPath || rest.length !== 1 || rest[0] !== "--promote") {
42+
console.error("usage: promote.mjs --manifest <provenance.json> --promote");
43+
process.exit(2);
44+
}
3045

31-
if (!existsSync(cfBin)) {
32-
throw new Error("pinned cf CLI missing; install dependencies before supplying credentials");
33-
}
34-
if (manifest.worker !== config.worker) {
35-
throw new Error(`manifest worker ${manifest.worker} does not match config worker ${config.worker}`);
36-
}
37-
if (manifest.sha !== verified.sha || String(manifest.runId) !== String(verified.id) || String(manifest.runAttempt) !== String(verified.attempt)) {
38-
throw new Error("manifest does not match the verified upload run; refusing to promote");
39-
}
40-
if (manifest.repository?.toLowerCase() !== process.env.GITHUB_REPOSITORY?.toLowerCase()) {
41-
throw new Error("manifest repository does not match this repository; refusing to promote");
42-
}
43-
if (typeof manifest.versionId !== "string" || !manifest.versionId) {
44-
throw new Error("manifest has no versionId; refusing to promote");
45-
}
46+
const config = JSON.parse(readFileSync(join(here, "config.json"), "utf8"));
47+
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
48+
const verified = JSON.parse(readFileSync(requireEnv("DC_WORKER_VERIFIED_RUN_FILE"), "utf8"));
49+
const token = requireEnv("DC_WORKER_CF_TOKEN");
50+
requireEnv("CLOUDFLARE_ACCOUNT_ID");
4651

47-
function cfJson(argv) {
48-
const output = execFileSync(cfBin, argv, {
49-
cwd: here,
50-
encoding: "utf8",
51-
env: { ...process.env, CLOUDFLARE_API_TOKEN: token },
52-
});
53-
let parsed;
54-
try {
55-
parsed = JSON.parse(output);
56-
} catch {
57-
throw new Error("cf returned non-JSON output; refusing to continue");
52+
if (!existsSync(cfBin)) {
53+
throw new Error("pinned cf CLI missing; install dependencies before supplying credentials");
54+
}
55+
if (manifest.worker !== config.worker) {
56+
throw new Error(`manifest worker ${manifest.worker} does not match config worker ${config.worker}`);
57+
}
58+
if (manifest.sha !== verified.sha || String(manifest.runId) !== String(verified.id) || String(manifest.runAttempt) !== String(verified.attempt)) {
59+
throw new Error("manifest does not match the verified upload run; refusing to promote");
60+
}
61+
if (manifest.repository?.toLowerCase() !== process.env.GITHUB_REPOSITORY?.toLowerCase()) {
62+
throw new Error("manifest repository does not match this repository; refusing to promote");
63+
}
64+
if (typeof manifest.versionId !== "string" || !manifest.versionId) {
65+
throw new Error("manifest has no versionId; refusing to promote");
5866
}
59-
return parsed?.result ?? parsed;
60-
}
6167

62-
const version = cfJson(["workers", "versions", "get", manifest.versionId, "--worker-id", config.worker]);
63-
if ((version.id ?? version.version_id) !== manifest.versionId) {
64-
throw new Error("cf did not return the manifest version; refusing to promote");
65-
}
66-
if (version.annotations?.["workers/tag"] && version.annotations["workers/tag"] !== manifest.tag) {
67-
throw new Error("the version's tag does not match the manifest tag; refusing to promote");
68-
}
68+
function cfJson(argv) {
69+
const output = execFileSync(cfBin, argv, {
70+
cwd: here,
71+
encoding: "utf8",
72+
env: { ...process.env, CLOUDFLARE_API_TOKEN: token },
73+
});
74+
let parsed;
75+
try {
76+
parsed = JSON.parse(output);
77+
} catch {
78+
throw new Error("cf returned non-JSON output; refusing to continue");
79+
}
80+
return parsed?.result ?? parsed;
81+
}
6982

70-
const deployments = cfJson(["workers", "deployments", "list", "--worker", config.worker, "--per-page", "1"]);
71-
const current = Array.isArray(deployments) ? deployments[0] : deployments?.deployments?.[0];
72-
const currentVersionId = current?.versions?.[0]?.version_id ?? null;
73-
if (currentVersionId !== manifest.baselineVersionId) {
74-
throw new Error(
75-
`active version ${currentVersionId} is not the manifest baseline ${manifest.baselineVersionId}; refusing to promote over drift`,
76-
);
77-
}
83+
const version = cfJson(["workers", "versions", "get", manifest.versionId, "--worker-id", config.worker]);
84+
if ((version.id ?? version.version_id) !== manifest.versionId) {
85+
throw new Error("cf did not return the manifest version; refusing to promote");
86+
}
87+
if (version.annotations?.["workers/tag"] && version.annotations["workers/tag"] !== manifest.tag) {
88+
throw new Error("the version's tag does not match the manifest tag; refusing to promote");
89+
}
7890

79-
const created = cfJson([
80-
"workers", "deployments", "create",
81-
"--worker", config.worker,
82-
"--strategy", "percentage",
83-
"--versions", JSON.stringify([{ version_id: manifest.versionId, percentage: 100 }]),
84-
]);
85-
const deploymentId = created?.id ?? created?.deployment_id;
86-
console.log(`promoted ${config.worker} version ${manifest.versionId} (tag ${manifest.tag}) to 100%${deploymentId ? `; deployment ${deploymentId}` : ""}`);
91+
const deployments = cfJson(["workers", "deployments", "list", "--worker", config.worker, "--per-page", "1"]);
92+
const current = Array.isArray(deployments) ? deployments[0] : deployments?.deployments?.[0];
93+
const status = deploymentStatus(current, manifest);
94+
if (status === "already-promoted") {
95+
console.log(`verified ${config.worker} version ${manifest.versionId} (tag ${manifest.tag}) is already the sole 100% deployment; nothing to promote`);
96+
return;
97+
}
98+
if (status === "drifted") {
99+
const active = current?.versions?.[0]?.version_id ?? null;
100+
throw new Error(
101+
`active version ${active} is neither the manifest baseline ${manifest.baselineVersionId} nor the verified upload ${manifest.versionId}; refusing to promote over drift`,
102+
);
103+
}
104+
105+
const created = cfJson([
106+
"workers", "deployments", "create",
107+
"--worker", config.worker,
108+
"--strategy", "percentage",
109+
"--versions", JSON.stringify([{ version_id: manifest.versionId, percentage: 100 }]),
110+
]);
111+
const deploymentId = created?.id ?? created?.deployment_id;
112+
console.log(`promoted ${config.worker} version ${manifest.versionId} (tag ${manifest.tag}) to 100%${deploymentId ? `; deployment ${deploymentId}` : ""}`);
113+
}
87114

88115
function requireEnv(name) {
89116
const value = process.env[name];
@@ -92,3 +119,7 @@ function requireEnv(name) {
92119
}
93120
return value;
94121
}
122+
123+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
124+
main();
125+
}
Collapse file

‎pkgs/desktop-commander/worker/test/promote.test.ts‎

Copy file name to clipboard
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { describe, expect, it } from "vitest";
2+
import { deploymentStatus } from "../promote.mjs";
3+
4+
// Oracle: the promote drift contract in pkgs/desktop-commander/worker/README.md —
5+
// promotion proceeds only at the manifest baseline, is a no-op success when the
6+
// verified upload is already the sole 100% deployment, and refuses any other
7+
// live state (#13472: the target version had already been deployed and the
8+
// baseline-only check misread that success as drift).
9+
10+
const manifest = {
11+
versionId: "37e2f6a3-f2ed-4f77-a84a-a7acf67f6ff4",
12+
baselineVersionId: "c2ac67bb-f7ab-4912-a820-aa2ead4aa908",
13+
};
14+
15+
describe("promotion drift classification", () => {
16+
it("treats the verified upload already serving 100% as already-promoted", () => {
17+
expect(
18+
deploymentStatus(
19+
{ versions: [{ version_id: manifest.versionId, percentage: 100 }] },
20+
manifest,
21+
),
22+
).toBe("already-promoted");
23+
});
24+
25+
it("allows promotion when the live deployment still matches the baseline", () => {
26+
expect(
27+
deploymentStatus(
28+
{ versions: [{ version_id: manifest.baselineVersionId, percentage: 100 }] },
29+
manifest,
30+
),
31+
).toBe("at-baseline");
32+
});
33+
34+
it("refuses drift to an unrelated active version", () => {
35+
expect(
36+
deploymentStatus(
37+
{ versions: [{ version_id: "00000000-0000-0000-0000-000000000000", percentage: 100 }] },
38+
manifest,
39+
),
40+
).toBe("drifted");
41+
});
42+
43+
it("refuses a partial rollout of the verified upload instead of guessing", () => {
44+
expect(
45+
deploymentStatus(
46+
{ versions: [{ version_id: manifest.versionId, percentage: 50 }, { version_id: manifest.baselineVersionId, percentage: 50 }] },
47+
manifest,
48+
),
49+
).toBe("drifted");
50+
});
51+
52+
it("refuses an empty or malformed deployment record", () => {
53+
expect(deploymentStatus(undefined, manifest)).toBe("drifted");
54+
expect(deploymentStatus({}, manifest)).toBe("drifted");
55+
expect(deploymentStatus({ versions: [] }, manifest)).toBe("drifted");
56+
});
57+
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.