Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(download): cancel hashing of resumable partials

Afloat13cb493d9458
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.md
index 8f13d40c..56e62919 100644
--- a/packages/local-installation/AGENTS.md
+++ b/packages/local-installation/AGENTS.md
@@ -186,6 +186,9 @@ Verified downloads stop reading on explicit cancellation even when a response
body ignores the fetch signal. They retain the caller's abort reason, cancel
the reader and keep only resumable partial bytes; cancellation never waits
for the idle timeout or moves on to another mirror.
+Rebuilding the checksum of a saved resumable partial also uses the caller's
+signal, so cancellation stops local reads before another request starts and
+leaves the partial file intact for the next attempt.
Move a freshly downloaded or unpacked file or folder into place with
`renameRetrying` from `fs-retry.ts`: on Windows antivirus and indexing hold
diff --git a/packages/local-installation/src/download-resume-cancel.test.ts b/packages/local-installation/src/download-resume-cancel.test.ts
new file mode 100644
index 00000000..0b588bd7
--- /dev/null
+++ b/packages/local-installation/src/download-resume-cancel.test.ts
@@ -0,0 +1,88 @@
+import { createHash } from "node:crypto";
+import { createReadStream } from "node:fs";
+import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import { expect, it, vi } from "vitest";
+import { downloadVerified } from "./download.js";
+
+vi.mock("node:fs", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("node:fs")>();
+ return { ...actual, createReadStream: vi.fn(actual.createReadStream) };
+});
+
+it("stops reading a resumable partial when cancelled and keeps its bytes for retry", async () => {
+ const actual = await vi.importActual<typeof import("node:fs")>("node:fs");
+ const root = await mkdtemp(path.join(tmpdir(), "download-resume-cancel-"));
+ const output = path.join(root, "runtime.tgz");
+ const partial = `${output}.part`;
+ const bytes = Buffer.alloc(1024 * 1024 + 13, 7);
+ const controller = new AbortController();
+ const reason = new Error(
+ "Installation cancelled during resume verification.",
+ );
+ let readBytes = 0;
+ let firstChunkBytes = 0;
+ let closed: Promise<void> | undefined;
+ const fetcher = vi.fn<typeof fetch>(async (_url, init) => {
+ init?.signal?.throwIfAborted();
+ return new Response(null, { status: 416 });
+ });
+ try {
+ await writeFile(partial, bytes);
+ vi.mocked(createReadStream).mockImplementation((file, options) => {
+ const stream = actual.createReadStream(file, options);
+ if (file === partial) {
+ closed = new Promise<void>((resolve) => stream.once("close", resolve));
+ stream.on("data", (chunk: string | Buffer) => {
+ readBytes += Buffer.byteLength(chunk);
+ if (!controller.signal.aborted) {
+ firstChunkBytes = Buffer.byteLength(chunk);
+ controller.abort(reason);
+ }
+ });
+ }
+ return stream;
+ });
+ await expect(
+ downloadVerified(
+ {
+ url: "https://example.com/runtime.tgz",
+ output,
+ algorithm: "sha256",
+ digest: createHash("sha256").update(bytes).digest("hex"),
+ maxBytes: bytes.length,
+ signal: controller.signal,
+ resumable: true,
+ },
+ fetcher,
+ ),
+ ).rejects.toBe(reason);
+ await closed;
+ expect(readBytes).toBeGreaterThan(0);
+ expect(readBytes).toBe(firstChunkBytes);
+ expect(readBytes).toBeLessThan(bytes.length);
+ expect(fetcher).not.toHaveBeenCalled();
+ expect(await readdir(root)).toEqual(["runtime.tgz.part"]);
+ expect((await readFile(partial)).equals(bytes)).toBe(true);
+ vi.mocked(createReadStream).mockImplementation(actual.createReadStream);
+ await downloadVerified(
+ {
+ url: "https://example.com/runtime.tgz",
+ output,
+ algorithm: "sha256",
+ digest: createHash("sha256").update(bytes).digest("hex"),
+ maxBytes: bytes.length,
+ signal: new AbortController().signal,
+ resumable: true,
+ },
+ fetcher,
+ );
+ expect(fetcher).toHaveBeenCalledTimes(1);
+ expect(await readdir(root)).toEqual(["runtime.tgz"]);
+ expect((await readFile(output)).equals(bytes)).toBe(true);
+ } finally {
+ vi.mocked(createReadStream).mockImplementation(actual.createReadStream);
+ await rm(root, { recursive: true, force: true });
+ }
+});
diff --git a/packages/local-installation/src/download.ts b/packages/local-installation/src/download.ts
index 9b12a283..7eb574bb 100644
--- a/packages/local-installation/src/download.ts
+++ b/packages/local-installation/src/download.ts
@@ -102,11 +102,19 @@ export async function downloadVerified(
if (options.resumable) {
const existing = await lstat(partial).catch(() => undefined);
if (existing?.isFile() && existing.size <= options.maxBytes) {
- for await (const chunk of createReadStream(partial))
- hash.update(chunk as Buffer);
+ try {
+ for await (const chunk of createReadStream(partial, {
+ signal: options.signal,
+ }))
+ hash.update(chunk as Buffer);
+ } catch (error) {
+ options.signal.throwIfAborted();
+ throw error;
+ }
completed = existing.size;
} else await rm(partial, { recursive: true, force: true });
}
+ options.signal.throwIfAborted();
const handle = await open(partial, options.resumable ? "a" : "ax", 0o600);
let size: number | undefined;
let lastProgressAt = 0;