Skip to content

Commit 85b3800

Browse files
StiensWoutclaudejuliusmarminge
authored
fix(acp-registry): an interrupted binary update no longer leaves its install lock behind (#48335)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 27c6c1e commit 85b3800

2 files changed

Lines changed: 76 additions & 13 deletions

File tree

‎packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1313
import { SpawnExecutableResolution } from "@t3tools/shared/shell";
1414
import * as Deferred from "effect/Deferred";
1515
import * as Effect from "effect/Effect";
16+
import * as Exit from "effect/Exit";
1617
import * as FileSystem from "effect/FileSystem";
1718
import * as Path from "effect/Path";
1819
import * as Fiber from "effect/Fiber";
@@ -1000,6 +1001,66 @@ describe("AcpRegistrySupport", () => {
10001001
);
10011002
});
10021003

1004+
it.effect(
1005+
"removes the install lock when an update is interrupted right after creating it",
1006+
() => {
1007+
let agent = makeAgent({
1008+
binary: { "linux-x86_64": { archive: archiveUrl, cmd: "./example-agent" } },
1009+
});
1010+
return Effect.gen(function* () {
1011+
const fs = yield* FileSystem.FileSystem;
1012+
const path = yield* Path.Path;
1013+
const cacheDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-acp-registry-lock-" });
1014+
const lockCreated = yield* Deferred.make<void>();
1015+
const finishWrite = yield* Deferred.make<void>();
1016+
const updateLock = path.join("1.2.4", "linux-x86_64.lock");
1017+
const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({
1018+
cacheDir,
1019+
toolsDir: `${cacheDir}/tools`,
1020+
registryUrl,
1021+
}).pipe(
1022+
Effect.provideService(FileSystem.FileSystem, {
1023+
...fs,
1024+
writeFileString: (file, data, options) => {
1025+
const write = fs.writeFileString(file, data, options);
1026+
return options?.flag === "wx" && file.endsWith(updateLock)
1027+
? write.pipe(
1028+
Effect.andThen(Deferred.succeed(lockCreated, undefined)),
1029+
Effect.andThen(Deferred.await(finishWrite)),
1030+
)
1031+
: write;
1032+
},
1033+
}),
1034+
);
1035+
yield* resolver.prepare({ agentId: agent.id });
1036+
agent = { ...agent, version: "1.2.4" };
1037+
yield* resolver.refreshIndex();
1038+
1039+
const first = yield* resolver.resolve(settings(), cacheDir).pipe(Effect.forkChild);
1040+
yield* Deferred.await(lockCreated);
1041+
const interruptor = yield* Effect.fiberId;
1042+
first.interruptUnsafe(interruptor);
1043+
yield* Deferred.succeed(finishWrite, undefined);
1044+
expect(Exit.hasInterrupts(yield* Fiber.await(first))).toBe(true);
1045+
expect(yield* fs.exists(path.join(cacheDir, "tools", "example-agent", updateLock))).toBe(
1046+
false,
1047+
);
1048+
}).pipe(
1049+
Effect.scoped,
1050+
Effect.provide(
1051+
layerResolver((request) =>
1052+
Effect.succeed(
1053+
HttpClientResponse.fromWeb(
1054+
request,
1055+
new Response(request.url === registryUrl ? makeRegistry(agent) : "#!/bin/sh\n"),
1056+
),
1057+
),
1058+
),
1059+
),
1060+
);
1061+
},
1062+
);
1063+
10031064
it.effect("gives Antigravity a T3-owned temp directory that is removed with the process", () => {
10041065
const agent = {
10051066
...makeAgent({ binary: { "linux-x86_64": { archive: archiveUrl, cmd: "./agy" } } }),

‎packages/provider-acp-registry/src/server/AcpRegistrySupport.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,10 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
16421642
lockPath: string,
16431643
) {
16441644
for (let attempt = 0; attempt < INSTALL_LOCK_RETRY_COUNT; attempt += 1) {
1645-
const acquired = yield* fileSystem.writeFileString(lockPath, "", { flag: "wx" }).pipe(
1645+
const acquired = yield* Effect.acquireRelease(
1646+
fileSystem.writeFileString(lockPath, "", { flag: "wx" }),
1647+
() => fileSystem.remove(lockPath, { force: true }).pipe(Effect.ignore),
1648+
).pipe(
16461649
Effect.as(true),
16471650
Effect.catchIf(
16481651
(error) => isAlreadyExists(error),
@@ -1774,19 +1777,19 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
17741777
),
17751778
);
17761779
const lockPath = `${installRoot}.lock`;
1777-
yield* acquireInstallLock(lockPath).pipe(
1778-
Effect.mapError((cause) =>
1779-
isAcpRegistryError(cause)
1780-
? cause
1781-
: new AcpRegistryError({
1782-
reason: "install_failed",
1783-
detail: `Could not acquire install lock for ACP Registry agent ${agent.id}.`,
1784-
cause,
1785-
}),
1786-
),
1787-
);
17881780

17891781
return yield* Effect.gen(function* () {
1782+
yield* acquireInstallLock(lockPath).pipe(
1783+
Effect.mapError((cause) =>
1784+
isAcpRegistryError(cause)
1785+
? cause
1786+
: new AcpRegistryError({
1787+
reason: "install_failed",
1788+
detail: `Could not acquire install lock for ACP Registry agent ${agent.id}.`,
1789+
cause,
1790+
}),
1791+
),
1792+
);
17901793
if (yield* fileSystem.exists(executablePath).pipe(Effect.orElseSucceed(() => false))) {
17911794
return yield* assertExecutableInRoot(installRoot, executablePath);
17921795
}
@@ -1918,7 +1921,6 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
19181921
return yield* assertExecutableInRoot(installRoot, executablePath);
19191922
}).pipe(
19201923
Effect.scoped,
1921-
Effect.ensuring(fileSystem.remove(lockPath, { force: true }).pipe(Effect.ignore)),
19221924
Effect.mapError((cause) =>
19231925
isAcpRegistryError(cause)
19241926
? cause

0 commit comments

Comments
 (0)