Files
loki/scripts/test/box-workspace-path.ts
T
04785343a4 fix(runner): meter the directory the agent actually runs in (#147)
Token accounting (#133) shipped, ran a full day, and wrote ZERO rows — no
usage_detail, no tokens, no cost, across 15 runs.

A dispatch carries the founder's LAPTOP path (`/home/g/dev/solon`). On the box
that path does not exist, so `launchAgentPty` resolves a box-local clone via
ensureBoxWorkspace — but it resolved it PRIVATELY: the effective directory never
escaped the function. Everything else in the runner kept using the unresolved
path, including the two places that look up `~/.claude/projects/<slug-of-dir>`:

  - the usage reporter tracked `-home-g-dev-solon`, a transcript directory that
    cannot exist on the box, so `collectClaudeUsage` returned null and every
    tick skipped it — silently, forever;
  - `detectAuthFailure` read the same missing directory, so dead box
    credentials were undetectable on the very runner whose two silent outages
    (2026-07-02/03) motivated writing that canary.

The fix is to stop having two answers to "where does this project live":
box-workspace-path.ts derives it once, box-workspace.ts uses it for cloning,
and the poller uses it for both transcript lookups. Pure (fs/os/path only) so
the poller can import it statically without pulling @/db into the desktop
bundle — the reason box-workspace.ts is dynamically imported in the first place.
On the laptop it is the identity function.

Also: the no-transcript branch now warns ONCE per run. Its silence is the whole
reason this went a day unnoticed — a metering path that fails quietly is
indistinguishable from a fleet that spent nothing.

7 tests, incl. the real production dispatch shape. Unblocks #136, whose start
gate waits on metered runs that could never arrive.

Closes #145

Co-authored-by: test <test@test>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-03 09:56:37 +02:00

117 lines
4.5 KiB
TypeScript

/**
* Where a dispatched project lives on THIS runner.
*
* Regression coverage for #145: token accounting shipped, ran for a day, and
* wrote ZERO rows. A dispatch carries the founder's laptop path
* (`/home/g/dev/solon`); the box resolves a local clone inside
* `launchAgentPty`, but that resolution never escaped the function — so the
* poller recorded the laptop path, `collectClaudeUsage` looked for
* `~/.claude/projects/-home-g-dev-solon`, found nothing, and skipped silently
* forever. The same unresolved path made `detectAuthFailure` unable to see dead
* box credentials.
*
* The invariant: the path used for transcript lookups is the path the agent
* actually runs in.
*
* Run: npx tsx scripts/test/box-workspace-path.ts
*/
import fs from "fs";
import os from "os";
import path from "path";
import {
resolveRunnerWorkspaceDir,
sanitizeWorkspaceKey,
BOX_DEV_ROOT,
} from "@/lib/agent-execution/box-workspace-path";
import { claudeProjectSlug } from "@/lib/usage/claude-transcript-usage";
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(message);
}
function runTests(): void {
let passed = 0;
const check = (label: string, fn: () => void) => {
fn();
passed += 1;
console.log(` ✓ ${label}`);
};
// A directory that certainly exists, to stand in for the laptop case.
const realDir = fs.mkdtempSync(path.join(os.tmpdir(), "fc-ws-"));
check("an existing requested dir is used as-is (laptop: identity)", () => {
assert(
resolveRunnerWorkspaceDir("solon", realDir) === realDir,
"a dir that exists must never be remapped",
);
});
// A path that cannot exist on ANY machine running this suite — the laptop
// path `/home/g/dev/<p>` is real on the founder's own laptop, so using it
// here would make the test pass or fail depending on who runs it.
const FOREIGN = "/nonexistent-runner-host/dev/solon";
check("a dir that does not exist here resolves to the box-local clone", () => {
const resolved = resolveRunnerWorkspaceDir("solon", FOREIGN);
assert(
resolved === path.join(BOX_DEV_ROOT, "solon"),
`expected the box clone path, got ${resolved}`,
);
});
check("resolution is keyed on the TAB, not the foreign path's basename", () => {
// The laptop path may be named anything; the box clone is named after the
// project, which is what box-workspace clones into.
const resolved = resolveRunnerWorkspaceDir("SBB-Lost-Found", "/home/g/code/whatever");
assert(
resolved === path.join(BOX_DEV_ROOT, "sbb-lost-found"),
`expected the sanitized tab clone path, got ${resolved}`,
);
});
check("the resolved dir yields a transcript slug that can exist locally", () => {
// The actual bug: a foreign path produced a slug like `-home-g-dev-solon`,
// which no transcript directory on THIS host ever matches, so the usage
// collector found nothing and skipped in silence.
const foreignSlug = claudeProjectSlug(FOREIGN);
const resolvedSlug = claudeProjectSlug(resolveRunnerWorkspaceDir("solon", FOREIGN));
assert(
foreignSlug === "-nonexistent-runner-host-dev-solon",
`unexpected foreign slug: ${foreignSlug}`,
);
assert(resolvedSlug !== foreignSlug, "resolution must move the slug onto this host");
assert(
resolvedSlug === claudeProjectSlug(path.join(BOX_DEV_ROOT, "solon")),
"resolved slug must match the box clone directory",
);
});
check("the real dispatch shape is handled: laptop path on a box-style host", () => {
// Verbatim from a production dispatch payload, with a host root that this
// suite can guarantee is absent.
const resolved = resolveRunnerWorkspaceDir("datacat", "/nonexistent-runner-host/dev/datacat");
assert(
claudeProjectSlug(resolved).startsWith(claudeProjectSlug(BOX_DEV_ROOT)),
`resolved transcript slug must live under this host's dev root, got ${resolved}`,
);
});
check("empty requested dir still resolves rather than throwing", () => {
assert(
resolveRunnerWorkspaceDir("solon", "") === path.join(BOX_DEV_ROOT, "solon"),
"an absent dir must fall back to the box clone path",
);
});
check("sanitizeWorkspaceKey never yields an empty segment", () => {
assert(sanitizeWorkspaceKey("///") === "workspace", "degenerate names need a safe fallback");
assert(sanitizeWorkspaceKey("My Project!") === "my-project", "unexpected sanitization");
});
fs.rmSync(realDir, { recursive: true, force: true });
console.log(`\n${passed} box-workspace-path test(s) passed`);
}
runTests();