From df83154b8c77f1094caddb72aa369b61b4d9cf91 Mon Sep 17 00:00:00 2001 From: NubsCarson Date: Fri, 4 Dec 2026 12:34:30 -0800 Subject: [PATCH] fix(eliza-harness): resolve shared alpha cache dirs via XDG_CACHE_HOME Resolve shared eliza checkout, pack, and QA cache paths via trimmed absolute XDG_CACHE_HOME with fallback to ~/.cache when unset, blank, or relative, while keeping explicit ALPHA_ELIZA_DIR, ALPHA_ELIZA_PACK_DIR, and ALPHA_QA_STATE overrides highest priority. Also add XDG_CACHE_HOME to turbo.json globalPassThroughEnv so Turbo tasks on remote Boat sandboxes see the persistent depot cache home instead of falling back to ephemeral ~/.cache, and add unit tests covering override and fallback behavior. --- packages/eliza-harness/src/checkout.ts | 11 ++++- packages/eliza-harness/src/pack.ts | 5 +- packages/eliza-harness/src/qa.ts | 4 +- packages/eliza-harness/test/harness.test.ts | 52 ++++++++++++++++++++- turbo.json | 2 +- 5 files changed, 66 insertions(+), 8 deletions(-) diff --git a/packages/eliza-harness/src/checkout.ts b/packages/eliza-harness/src/checkout.ts index f67bacd0a6..b2d61d8388 100644 --- a/packages/eliza-harness/src/checkout.ts +++ b/packages/eliza-harness/src/checkout.ts @@ -30,9 +30,18 @@ export const repository =(function rootOf(directory: string): string { const lock = JSON.parse(fs.readFileSync(path.join(repository, "eliza.lock.json"), "utf8")) as Lock; +/** Resolve a cache directory from `overrideEnv` when non-blank, else `$XDG_CACHE_HOME/alpha/` (falling back to `~/.cache/alpha/` when `XDG_CACHE_HOME` is unset, blank, or relative). @throws {never} */ +export function alphaCacheDir(overrideEnv: string, ...segments: readonly string[]): string { + const override = process.env[overrideEnv]?.trim(); + if (override !== undefined && override.length > 0) return override; + const xdg = process.env.XDG_CACHE_HOME?.trim(); + const base = xdg !== undefined && xdg.length > 0 && path.isAbsolute(xdg) ? xdg : path.join(os.homedir(), ".cache"); + return path.join(base, "alpha", ...segments); +} + /** @throws {never} */ export function checkout(): Checkout { - const root = process.env.ALPHA_ELIZA_DIR ?? path.join(os.homedir(), ".cache/alpha/eliza-vibestack", lock.commit.slice(0, 12)); + const root = alphaCacheDir("ALPHA_ELIZA_DIR", "eliza-vibestack", lock.commit.slice(0, 12)); return { commit: lock.commit, root, bundle: path.join(root, ".alpha/agent"), cloud: path.join(root, "packages/cloud-worker") }; } diff --git a/packages/eliza-harness/src/pack.ts b/packages/eliza-harness/src/pack.ts index f9318045d5..e212aaf1b9 100644 --- a/packages/eliza-harness/src/pack.ts +++ b/packages/eliza-harness/src/pack.ts @@ -1,9 +1,8 @@ import { createHash, randomBytes } from "node:crypto"; import fs from "node:fs"; -import os from "node:os"; import path from "node:path"; import { Schema } from "effect"; -import { alone, checkout, hasCommit, headCommit, repository, run } from "./checkout.ts"; +import { alone, alphaCacheDir, checkout, hasCommit, headCommit, repository, run } from "./checkout.ts"; /** * A private clone of the pinned eliza that the agent pack is built from, as Alpha's old phone build prepared its own @@ -25,7 +24,7 @@ const worktreeKey = createHash("sha256").update(repository).digest("hex").slice( /** Where the pack's clone for `commit` lives, shared across worktrees like `.eliza`, outside `node_modules` and outside the repo. @throws {never} */ export function packRoot(commit = checkout().commit): string { - return process.env.ALPHA_ELIZA_PACK_DIR ?? path.join(os.homedir(), ".cache/alpha/eliza-pack", commit.slice(0, 12)); + return alphaCacheDir("ALPHA_ELIZA_PACK_DIR", "eliza-pack", commit.slice(0, 12)); } const stampOf = (root: string) => path.join(root, ".alpha/pack.json"); diff --git a/packages/eliza-harness/src/qa.ts b/packages/eliza-harness/src/qa.ts index 3fe6a078db..2a97191847 100644 --- a/packages/eliza-harness/src/qa.ts +++ b/packages/eliza-harness/src/qa.ts @@ -3,9 +3,9 @@ import { randomBytes } from "node:crypto"; import fs from "node:fs"; import http, { type IncomingMessage, type ServerResponse } from "node:http"; import type { AddressInfo } from "node:net"; -import os from "node:os"; import path from "node:path"; import { Effect } from "effect"; +import { alphaCacheDir } from "./checkout.ts"; import { builtPack } from "./pack.ts"; import { qaGmail } from "./providers/google-gmail.ts"; import { qaMaps } from "./providers/google-maps.ts"; @@ -38,7 +38,7 @@ export interface QaServices { export interface QaAgent { readonly id: string; readonly origin: string; readonly token: string; readonly key: DeviceKey } /** The state directory of the QA services, outside the repository: `ALPHA_QA_STATE`, as the lever names it for its worktree. */ -export const qaState = () => process.env.ALPHA_QA_STATE ?? path.join(os.homedir(), ".cache/alpha/qa/default"); +export const qaState = () => alphaCacheDir("ALPHA_QA_STATE", "qa", "default"); const owner = { email: "owner@alpha.test", credits: 2_000_000_000 }; const gmailOwner = { email: "gmail@alpha.test", credits: 2_000_000_000 }; diff --git a/packages/eliza-harness/test/harness.test.ts b/packages/eliza-harness/test/harness.test.ts index 57e26309e6..3195083947 100644 --- a/packages/eliza-harness/test/harness.test.ts +++ b/packages/eliza-harness/test/harness.test.ts @@ -3,12 +3,14 @@ import { randomBytes } from "node:crypto"; import fs from "node:fs"; import http from "node:http"; import type { AddressInfo } from "node:net"; +import os from "node:os"; import path from "node:path"; import { describe, expect, it } from "@effect/vitest"; import { Effect } from "effect"; import { alone, checkout, prepared } from "../src/checkout.ts"; -import { packEntryName, stagePackEntry } from "../src/pack.ts"; +import { packEntryName, packRoot, stagePackEntry } from "../src/pack.ts"; import { pinnedCheckout } from "../src/pinned.vibe"; +import { qaState } from "../src/qa.ts"; import { type Cloud, type RealModel, said, scratch, startCloud } from "../src/services.ts"; const repository = path.join(import.meta.dirname, "../../.."); @@ -76,6 +78,54 @@ const holding = (directory: string, secret: string) => (fs.readdirSync(directory .filter((file) => fs.statSync(path.join(directory, file)).isFile() && fs.readFileSync(path.join(directory, file)).includes(secret)); describe("the eliza harness", () => { + it("resolves shared checkout, pack, and QA cache paths from XDG_CACHE_HOME with clean fallback to ~/.cache", () => { + const keys = ["XDG_CACHE_HOME", "ALPHA_ELIZA_DIR", "ALPHA_ELIZA_PACK_DIR", "ALPHA_QA_STATE"] as const; + const saved = Object.fromEntries(keys.map((key) => [key, process.env[key]])) as Record<(typeof keys)[number], string | undefined>; + const commit = "0123456789abcdef0123456789abcdef01234567"; + const lock = JSON.parse(fs.readFileSync(path.join(repository, "eliza.lock.json"), "utf8")) as { commit: string }; + const shortLock = lock.commit.slice(0, 12); + const defaultBase = path.join(os.homedir(), ".cache"); + const customBase = path.resolve(path.sep, "var", "cache", "alpha-depot"); + const resolved = () => [checkout().root, packRoot(commit), qaState()]; + const expectedUnder = (base: string) => [ + path.join(base, "alpha", "eliza-vibestack", shortLock), + path.join(base, "alpha", "eliza-pack", commit.slice(0, 12)), + path.join(base, "alpha", "qa", "default"), + ]; + try { + for (const key of keys) delete process.env[key]; + expect(resolved()).toEqual(expectedUnder(defaultBase)); + + process.env.XDG_CACHE_HOME = ` ${customBase} `; + expect(resolved()).toEqual(expectedUnder(customBase)); + + for (const invalid of ["", " ", "relative/cache"]) { + process.env.XDG_CACHE_HOME = invalid; + expect(resolved()).toEqual(expectedUnder(defaultBase)); + } + + process.env.XDG_CACHE_HOME = customBase; + process.env.ALPHA_ELIZA_DIR = " "; + process.env.ALPHA_ELIZA_PACK_DIR = " "; + process.env.ALPHA_QA_STATE = " "; + expect(resolved()).toEqual(expectedUnder(customBase)); + + const explicit = [ + path.resolve(path.sep, "srv", "eliza"), + path.resolve(path.sep, "srv", "eliza-pack"), + path.resolve(path.sep, "srv", "qa-state"), + ]; + [process.env.ALPHA_ELIZA_DIR, process.env.ALPHA_ELIZA_PACK_DIR, process.env.ALPHA_QA_STATE] = explicit; + expect(resolved()).toEqual(explicit); + } finally { + for (const key of keys) { + const value = saved[key]; + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + } + }); + it("finds the pinned checkout built and linked as .eliza", async () => { const lock = JSON.parse(fs.readFileSync(path.join(repository, "eliza.lock.json"), "utf8")) as { commit: string }; const at = prepared(); diff --git a/turbo.json b/turbo.json index 11d0bcc5ca..3c639074c3 100644 --- a/turbo.json +++ b/turbo.json @@ -1,6 +1,6 @@ { "$schema": "https://turborepo.dev/schema.json", - "globalPassThroughEnv": ["ALPHA_ELIZA_DIR", "QUINT_HOME"], + "globalPassThroughEnv": ["ALPHA_ELIZA_DIR", "QUINT_HOME", "XDG_CACHE_HOME"], "globalDependencies": [ ".nvmrc", "pnpm-workspace.yaml",