14252e71cb
Fix all Windows-specific test failures caused by path handling problems
and EBUSY errors from unclosed SQLite database handles.
Root causes fixed:
1. POSIX path assumptions: replaced hard-coded file:///tmp, /repo, etc.
with pathToFileURL/path.resolve/path.join computed expectations
2. shortenPath() now normalizes backslashes to forward slashes after ~
and respects home directory boundaries
3. HistoryStorage.resetInstance() leaked its Database — added #close()
that finalizes all prepared statements and closes the DB
4. AgentStorage gained the same resetInstance()/#close() pattern
5. SqliteAuthCredentialStore.close() leaked one-off prepared statements
from inline this.#db.prepare() calls — wrapped each in try/finally
6. model-cache.ts used a process-global DB even for custom dbPath —
now opens/closes per-call via withModelCacheDb
7. createAgentSession leaked AuthStorage on construction failure —
added ownsAuthStorage cleanup in catch block
8. MnemopiBackend.removeDbFiles() now truly best-effort (catches errors)
9. TempDir retry window expanded from 4x10ms to 40x25ms
10. TempDir prefix convention: non-@ prefixes created dirs relative to
cwd instead of os.tmpdir() — all test temp dirs now use @ prefix
11. Shell-escaped interpolated paths in bash tool tests
12. git core.autocrlf false in autoresearch test repo init
All 522 previously-failing Windows tests now pass.
131 lines
2.9 KiB
TypeScript
131 lines
2.9 KiB
TypeScript
import * as fs from "node:fs";
|
|
import * as fsPromises from "node:fs/promises";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
|
|
export class TempDir {
|
|
#path: string;
|
|
private constructor(path: string) {
|
|
this.#path = path;
|
|
}
|
|
|
|
static createSync(prefix?: string): TempDir {
|
|
return new TempDir(fs.mkdtempSync(normalizePrefix(prefix)));
|
|
}
|
|
|
|
static async create(prefix?: string): Promise<TempDir> {
|
|
return new TempDir(await fsPromises.mkdtemp(normalizePrefix(prefix)));
|
|
}
|
|
|
|
#removePromise: Promise<void> | null = null;
|
|
|
|
path(): string {
|
|
return this.#path;
|
|
}
|
|
|
|
absolute(): string {
|
|
return path.resolve(this.#path);
|
|
}
|
|
|
|
remove(): Promise<void> {
|
|
if (this.#removePromise) {
|
|
return this.#removePromise;
|
|
}
|
|
const removePromise = removeWithRetries(this.#path);
|
|
this.#removePromise = removePromise;
|
|
return removePromise;
|
|
}
|
|
|
|
removeSync(): void {
|
|
removeSyncWithRetries(this.#path);
|
|
this.#removePromise = Promise.resolve();
|
|
}
|
|
|
|
toString(): string {
|
|
return this.#path;
|
|
}
|
|
|
|
join(...paths: string[]): string {
|
|
return path.join(this.#path, ...paths);
|
|
}
|
|
|
|
async [Symbol.asyncDispose](): Promise<void> {
|
|
try {
|
|
await this.remove();
|
|
} catch {
|
|
// Ignore cleanup errors
|
|
}
|
|
}
|
|
|
|
[Symbol.dispose](): void {
|
|
try {
|
|
this.removeSync();
|
|
} catch {
|
|
// Ignore cleanup errors
|
|
}
|
|
}
|
|
}
|
|
|
|
const kTempDir = os.tmpdir();
|
|
|
|
function normalizePrefix(prefix?: string): string {
|
|
if (!prefix) {
|
|
return `${kTempDir}${path.sep}pi-temp-`;
|
|
} else if (prefix.startsWith("@")) {
|
|
return path.join(kTempDir, prefix.slice(1));
|
|
}
|
|
return prefix;
|
|
}
|
|
|
|
const kRemoveOptions = { recursive: true, force: true } as const;
|
|
const kRemoveRetries = 40;
|
|
const kRemoveRetryDelayMs = 25;
|
|
const kRetryableRemoveErrorCodes = new Set(["EBUSY", "EPERM", "ENOTEMPTY"]);
|
|
const kSleepBuffer = new Int32Array(new SharedArrayBuffer(4));
|
|
|
|
async function removeWithRetries(target: string): Promise<void> {
|
|
for (let attempt = 0; ; attempt++) {
|
|
try {
|
|
await fsPromises.rm(target, kRemoveOptions);
|
|
return;
|
|
} catch (err) {
|
|
if (!shouldRetryRemove(err, attempt)) throw err;
|
|
await Bun.sleep(kRemoveRetryDelayMs);
|
|
}
|
|
}
|
|
}
|
|
|
|
function removeSyncWithRetries(target: string): void {
|
|
for (let attempt = 0; ; attempt++) {
|
|
try {
|
|
fs.rmSync(target, kRemoveOptions);
|
|
return;
|
|
} catch (err) {
|
|
if (!shouldRetryRemove(err, attempt)) throw err;
|
|
sleepSync(kRemoveRetryDelayMs);
|
|
}
|
|
}
|
|
}
|
|
|
|
function shouldRetryRemove(err: unknown, attempt: number): boolean {
|
|
return attempt < kRemoveRetries && process.platform === "win32" && isRetryableRemoveError(err);
|
|
}
|
|
|
|
function isRetryableRemoveError(err: unknown): boolean {
|
|
return (
|
|
typeof err === "object" &&
|
|
err !== null &&
|
|
"code" in err &&
|
|
typeof err.code === "string" &&
|
|
kRetryableRemoveErrorCodes.has(err.code)
|
|
);
|
|
}
|
|
|
|
function sleepSync(ms: number): void {
|
|
if ("sleepSync" in Bun && typeof Bun.sleepSync === "function") {
|
|
Bun.sleepSync(ms);
|
|
return;
|
|
}
|
|
Atomics.wait(kSleepBuffer, 0, 0, ms);
|
|
}
|