feat: centralize runtime directories behind GHBOT_RUNTIME_DIR (D-13)

This commit is contained in:
wdawdwa committed 2026-08-23 11:26:48 +08:00
1 parent 941e1d2681
commit d9666c94a7
7 files changed
+64 -6

No files matched your search

+2 -1
View File
@@ -7,6 +7,7 @@ import process from "node:process";
import { config } from "../config.js"; import { config } from "../config.js";
import { logger } from "../logger.js"; import { logger } from "../logger.js";
import { startOneRunApiProxy } from "./apiProxy.js"; import { startOneRunApiProxy } from "./apiProxy.js";
import { tempRootDirectory } from "../runtimePaths.js";
const GOOSE_RUN_TIMEOUT_MS = 10 * 60 * 1000; const GOOSE_RUN_TIMEOUT_MS = 10 * 60 * 1000;
const GOOSE_DOCKER_IMAGE = "node:24-bookworm"; const GOOSE_DOCKER_IMAGE = "node:24-bookworm";
@@ -56,7 +57,7 @@ export async function runGoosePrompt(
throw new Error("GOOSE_API_KEY is required when running a goose prompt."); throw new Error("GOOSE_API_KEY is required when running a goose prompt.");
} }
const tempRoot = path.join(process.cwd(), ".ghbot-tmp"); const tempRoot = tempRootDirectory();
await fs.mkdir(tempRoot, { recursive: true }); await fs.mkdir(tempRoot, { recursive: true });
const tempDir = await fs.mkdtemp(path.join(tempRoot, "goose-")); const tempDir = await fs.mkdtemp(path.join(tempRoot, "goose-"));
const workingDirectory = options.workingDirectory const workingDirectory = options.workingDirectory
+2 -1
View File
@@ -18,6 +18,7 @@ import {
} from "../repository/knowledge.js"; } from "../repository/knowledge.js";
import { hasProtectedSegment, isProtectedBasename } from "../security/sanitization.js"; import { hasProtectedSegment, isProtectedBasename } from "../security/sanitization.js";
import { listPullRequestFiles } from "../github/pulls.js"; import { listPullRequestFiles } from "../github/pulls.js";
import { tempRootDirectory } from "../runtimePaths.js";
const CHAT_MARKER_PREFIX = "<!-- ghbot-chat:v1"; const CHAT_MARKER_PREFIX = "<!-- ghbot-chat:v1";
const MAX_REPLY_CHARS = 60_000; const MAX_REPLY_CHARS = 60_000;
@@ -310,7 +311,7 @@ function isNotFoundError(error: unknown): boolean {
export async function createRepositorySnapshot(sourceWorktree: string): Promise<string> { export async function createRepositorySnapshot(sourceWorktree: string): Promise<string> {
const sourceRoot = await fs.realpath(sourceWorktree); const sourceRoot = await fs.realpath(sourceWorktree);
const tempRoot = path.join(process.cwd(), ".ghbot-tmp"); const tempRoot = tempRootDirectory();
await fs.mkdir(tempRoot, { recursive: true }); await fs.mkdir(tempRoot, { recursive: true });
const snapshot = await fs.mkdtemp(path.join(tempRoot, "pr-chat-")); const snapshot = await fs.mkdtemp(path.join(tempRoot, "pr-chat-"));
+2 -1
View File
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { z } from "zod"; import { z } from "zod";
import { logger } from "../logger.js"; import { logger } from "../logger.js";
import { cacheRootDirectory } from "../runtimePaths.js";
import type { ReviewDecision } from "../types.js"; import type { ReviewDecision } from "../types.js";
const findingSchema = z.object({ const findingSchema = z.object({
@@ -114,7 +115,7 @@ export async function deleteLocalReviewCache(pullNumber: number): Promise<void>
} }
function cachePath(pullNumber: number): string { function cachePath(pullNumber: number): string {
return path.join(process.cwd(), ".ghbot-cache", `pr-${pullNumber}.json`); return path.join(cacheRootDirectory(), `pr-${pullNumber}.json`);
} }
function isFileNotFound(error: unknown): boolean { function isFileNotFound(error: unknown): boolean {
+2 -1
View File
@@ -9,6 +9,7 @@ import { createRepositorySnapshot } from "../chat/processor.js";
import { config } from "../config.js"; import { config } from "../config.js";
import { logger } from "../logger.js"; import { logger } from "../logger.js";
import { hasProtectedSegment, isProtectedBasename } from "../security/sanitization.js"; import { hasProtectedSegment, isProtectedBasename } from "../security/sanitization.js";
import { tempRootDirectory } from "../runtimePaths.js";
import { import {
loadRepositoryKnowledge, loadRepositoryKnowledge,
REPOSITORY_KNOWLEDGE_SCRATCH_PATH, REPOSITORY_KNOWLEDGE_SCRATCH_PATH,
@@ -99,7 +100,7 @@ export async function resolvePullRequestConflicts(
} }
const worktree = await fs.realpath(params.worktree); const worktree = await fs.realpath(params.worktree);
const tempRoot = path.join(process.cwd(), ".ghbot-tmp"); const tempRoot = tempRootDirectory();
await fs.mkdir(tempRoot, { recursive: true }); await fs.mkdir(tempRoot, { recursive: true });
const askPassDirectory = await fs.mkdtemp(path.join(tempRoot, "git-auth-")); const askPassDirectory = await fs.mkdtemp(path.join(tempRoot, "git-auth-"));
const askPassPath = path.join(askPassDirectory, "askpass.sh"); const askPassPath = path.join(askPassDirectory, "askpass.sh");
+20
View File
@@ -0,0 +1,20 @@
import path from "node:path";
import process from "node:process";
/**
* Runtime scratch/cache directories live under one configurable root so
* deployments with read-only or restricted working directories (D-13) can move
* them without code changes. Defaults to the process working directory.
*/
export function runtimeDirectory(): string {
const configured = process.env.GHBOT_RUNTIME_DIR?.trim();
return configured ? configured : process.cwd();
}
export function tempRootDirectory(): string {
return path.join(runtimeDirectory(), ".ghbot-tmp");
}
export function cacheRootDirectory(): string {
return path.join(runtimeDirectory(), ".ghbot-cache");
}
+3 -2
View File
@@ -6,6 +6,7 @@ import {
validateRepositoryKnowledge validateRepositoryKnowledge
} from "../repository/knowledge.js"; } from "../repository/knowledge.js";
import { parseReviewCacheContent } from "../review/cache.js"; import { parseReviewCacheContent } from "../review/cache.js";
import { runtimeDirectory as resolveRuntimeDirectory } from "../runtimePaths.js";
import { downloadR2Object, isR2Configured, uploadR2Object } from "./r2.js"; import { downloadR2Object, isR2Configured, uploadR2Object } from "./r2.js";
const MAX_REVIEW_CACHE_BYTES = 256 * 1024; const MAX_REVIEW_CACHE_BYTES = 256 * 1024;
@@ -62,7 +63,7 @@ export async function restorePersistentCache(params: {
if (!params.storage && !isR2Configured()) { if (!params.storage && !isR2Configured()) {
return; return;
} }
const runtimeDirectory = params.runtimeDirectory ?? process.cwd(); const runtimeDirectory = params.runtimeDirectory ?? resolveRuntimeDirectory();
const storage = params.storage ?? r2Store; const storage = params.storage ?? r2Store;
await restoreObject({ await restoreObject({
@@ -95,7 +96,7 @@ export async function savePersistentCache(params: {
if (!params.storage && !isR2Configured()) { if (!params.storage && !isR2Configured()) {
return; return;
} }
const runtimeDirectory = params.runtimeDirectory ?? process.cwd(); const runtimeDirectory = params.runtimeDirectory ?? resolveRuntimeDirectory();
const storage = params.storage ?? r2Store; const storage = params.storage ?? r2Store;
if (params.saveRepositoryKnowledge) { if (params.saveRepositoryKnowledge) {
+33
View File
@@ -0,0 +1,33 @@
import assert from "node:assert/strict";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { cacheRootDirectory, runtimeDirectory, tempRootDirectory } from "../src/runtimePaths.js";
test("runtime paths default to the process working directory", () => {
delete process.env.GHBOT_RUNTIME_DIR;
assert.equal(runtimeDirectory(), process.cwd());
assert.match(tempRootDirectory(), /[\\/.]\.ghbot-tmp$/);
assert.match(cacheRootDirectory(), /[\\/.]\.ghbot-cache$/);
});
test("GHBOT_RUNTIME_DIR relocates scratch and cache roots", () => {
const root = path.join(os.tmpdir(), `ghbot-runtime-${Date.now()}`);
process.env.GHBOT_RUNTIME_DIR = root;
try {
assert.equal(runtimeDirectory(), root);
assert.equal(tempRootDirectory(), path.join(root, ".ghbot-tmp"));
assert.equal(cacheRootDirectory(), path.join(root, ".ghbot-cache"));
} finally {
delete process.env.GHBOT_RUNTIME_DIR;
}
});
test("whitespace-only GHBOT_RUNTIME_DIR falls back to cwd", () => {
process.env.GHBOT_RUNTIME_DIR = " ";
try {
assert.equal(runtimeDirectory(), process.cwd());
} finally {
delete process.env.GHBOT_RUNTIME_DIR;
}
});