Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

1.9 KB
Raw Blame
import { execFileSync } from 'node:child_process';
import { rmSync } from 'node:fs';
import { setTimeout as delay } from 'node:timers/promises';

/**
 * About 1.8 s in total. A SQLite connection that has just closed keeps the
 * directory busy on Windows for a moment, and the handle is only released
 * while the event loop runs, so the waits here are real yields, never a
 * blocking sleep.
 */
const REMOVAL_RETRY_DELAYS_MS = [0, 25, 75, 200, 500, 1000];
const RETRYABLE_CODES = new Set(['EBUSY', 'EPERM', 'EACCES', 'ENOTEMPTY']);

/**
 * Remove a test temp directory, retrying while the filesystem still reports it
 * busy. On Windows a SQLite connection that has just closed (or an antivirus or
 * indexer pass over the -wal and -shm files) keeps the directory undeletable
 * for a few hundred milliseconds, which otherwise turns a passing test into a
 * red afterEach and hides the assertion that actually ran.
 */
export async function removeTempDir(target: string): Promise<void> {
  let lastError: unknown;
  for (const wait of REMOVAL_RETRY_DELAYS_MS) {
    if (wait) await delay(wait);
    try {
      rmSync(target, { recursive: true, force: true });
      return;
    } catch (error) {
      lastError = error;
      const code = (error as NodeJS.ErrnoException).code;
      if (process.platform === 'win32' && process.versions.bun && code === 'EFAULT') {
        // Bun 1.4.4 can fail to traverse a Windows launcher fixture even after every child has exited.
        // Node uses its own filesystem implementation. Propagate its error if the tree cannot be removed.
        execFileSync('node', ['-e',
          "require('node:fs').rmSync(process.argv[1], { recursive: true, force: true, maxRetries: 3 })",
          '--', target], { timeout: 10000 });
        return;
      }
      if (code && !RETRYABLE_CODES.has(code)) break;
    }
  }
  throw lastError instanceof Error ? lastError : new Error(String(lastError));
}