Skip to content

Commit 63df443

Browse files
authored
Merge pull request #17 from Microck/fix/normalize-acp-random
fix(release): guard ACP random compatibility
2 parents 363c4e7 + ad39e26 commit 63df443

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎.github/workflows/personal-release.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ jobs:
4646
with:
4747
node-version: "24"
4848
- run: npm install --global @github/copilot@1.0.101
49+
- name: Test release compatibility normalization
50+
run: node --test scripts/personal-release-random.node-check.ts
4951
- name: Prepare official source with pending fixes
5052
id: prepare
5153
env:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as NodeAssert from "node:assert/strict";
2+
import * as NodeTest from "node:test";
3+
import { normalizeAcpRandomCompatibility } from "./personal-release.ts";
4+
5+
const legacy = [
6+
'import * as Crypto from "effect/Crypto";',
7+
"type Requirements = Digest.Digest | Crypto.Crypto;",
8+
"const crypto = yield* Crypto.Crypto;",
9+
...Array.from({ length: 4 }, () => "Effect.provideService(Crypto.Crypto, crypto)"),
10+
].join("\n");
11+
12+
NodeTest.test("normalizes the known legacy Crypto shape without changing other text", () => {
13+
const result = normalizeAcpRandomCompatibility(legacy);
14+
NodeAssert.match(result, /import \* as Digest from "effect\/crypto\/Digest";/);
15+
NodeAssert.match(result, /import \* as SecureRandom from "effect\/crypto\/SecureRandom";/);
16+
NodeAssert.match(result, /type Requirements = Digest\.Digest \| SecureRandom\.SecureRandom;/);
17+
NodeAssert.match(result, /const random = yield\* SecureRandom\.SecureRandom;/);
18+
NodeAssert.equal(result.split("SecureRandom.SecureRandom, random)").length - 1, 4);
19+
NodeAssert.doesNotMatch(result, /effect\/Crypto|Crypto\.Crypto/);
20+
});
21+
22+
NodeTest.test("leaves already-current code unchanged", () => {
23+
const current = 'import * as SecureRandom from "effect/crypto/SecureRandom";';
24+
NodeAssert.equal(normalizeAcpRandomCompatibility(current), current);
25+
});
26+
27+
NodeTest.test("fails closed when the legacy shape is unexpected", () => {
28+
NodeAssert.throws(() =>
29+
normalizeAcpRandomCompatibility(legacy.replace("Crypto.Crypto", "Unexpected")),
30+
);
31+
});
32+
33+
NodeTest.test("rejects an unrecognized legacy import and residual namespace reference", () => {
34+
NodeAssert.throws(() =>
35+
normalizeAcpRandomCompatibility(legacy.replace('"effect/Crypto"', "'effect/Crypto'")),
36+
);
37+
NodeAssert.throws(() =>
38+
normalizeAcpRandomCompatibility(legacy + "\nconst cryptoModule = Crypto;"),
39+
);
40+
});
41+
42+
NodeTest.test("rejects extra uses of the renamed crypto binding", () => {
43+
NodeAssert.throws(() => normalizeAcpRandomCompatibility(legacy + "\nconst saved = crypto;"));
44+
});

‎scripts/personal-release.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,46 @@ function replaceRequired(file: string, before: string, after: string) {
153153
NodeFS.writeFileSync(file, source.replace(before, after));
154154
}
155155

156+
// A conflicted carried fix can retain the pre-migration Crypto service even
157+
// though the current Effect version exposes Digest and SecureRandom separately.
158+
// Normalize only the exact known shape; fail closed if that shape changes.
159+
export function normalizeAcpRandomCompatibility(source: string): string {
160+
const legacyImport = 'import * as Crypto from "effect/Crypto";';
161+
if (!source.includes(legacyImport)) {
162+
if (/\bCrypto\b|effect\/Crypto/.test(source))
163+
throw new Error("Unexpected ACP random compatibility shape: unrecognized Crypto reference");
164+
return source;
165+
}
166+
if ((source.match(/\bcrypto\b/g) ?? []).length !== 5)
167+
throw new Error("Unexpected ACP random compatibility shape: extra crypto binding references");
168+
const replacements: ReadonlyArray<readonly [string, string, number]> = [
169+
[
170+
legacyImport,
171+
'import * as Digest from "effect/crypto/Digest";\nimport * as SecureRandom from "effect/crypto/SecureRandom";',
172+
1,
173+
],
174+
["Crypto.Crypto", "SecureRandom.SecureRandom", 6],
175+
[
176+
"const crypto = yield* SecureRandom.SecureRandom;",
177+
"const random = yield* SecureRandom.SecureRandom;",
178+
1,
179+
],
180+
["SecureRandom.SecureRandom, crypto)", "SecureRandom.SecureRandom, random)", 4],
181+
];
182+
let result = source;
183+
for (const [before, after, expected] of replacements) {
184+
const count = result.split(before).length - 1;
185+
if (count !== expected)
186+
throw new Error(
187+
`Unexpected ACP random compatibility shape: ${before} (${count}, expected ${expected})`,
188+
);
189+
result = result.replaceAll(before, after);
190+
}
191+
if (/\bCrypto\b|effect\/Crypto/.test(result))
192+
throw new Error("Unexpected ACP random compatibility shape: residual Crypto reference");
193+
return result;
194+
}
195+
156196
function resolveConflict(commit: string, allowedFiles: ReadonlyArray<string>) {
157197
if (!process.env.COPILOT_GITHUB_TOKEN)
158198
throw new Error("Set the COPILOT_GITHUB_TOKEN Actions secret to resolve conflicts.");
@@ -329,6 +369,14 @@ function prepare() {
329369
else if (fix.label)
330370
applied.push(`${fix.label}: https://github.com/${fork}/commit/${fix.commit}`);
331371
}
372+
const acpDriver = "packages/provider-acp-registry/src/server/driver.ts";
373+
const acpSource = NodeFS.readFileSync(acpDriver, "utf8");
374+
const normalizedAcpSource = normalizeAcpRandomCompatibility(acpSource);
375+
if (normalizedAcpSource !== acpSource) {
376+
NodeFS.writeFileSync(acpDriver, normalizedAcpSource);
377+
git("add", acpDriver);
378+
git("commit", "-m", "fix(acp): retain current random and digest services");
379+
}
332380
// The CLI release index and downloads must point at the same distribution.
333381
replaceRequired(
334382
"packages/shared/src/cliRelease.ts",

0 commit comments

Comments
 (0)