Files
t3code/apps/server/src/storageCleanup.ts
T

555 lines
22 KiB
TypeScript

import {
OrchestrationV2AppThreadJson,
OrchestrationV2ProviderSessionJson,
} from "@t3tools/contracts";
import * as Schema from "effect/Schema";
import * as SqlClient from "effect/sql/SqlClient";
import type {
OrchestrationV2ThreadShell,
ProjectId,
ServerSettings,
ServerSettingsError,
TerminalSummary,
WorktreeCleanupRules,
} from "@t3tools/contracts";
import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
import * as Cause from "effect/Cause";
import * as Clock from "effect/Clock";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Equal from "effect/Equal";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import type { PlatformError } from "effect/PlatformError";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import * as ServerConfig from "./config.ts";
import * as GitManager from "./git/GitManager.ts";
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
import * as Orchestrator from "./orchestration-v2/Orchestrator.ts";
import * as ProjectionStore from "./orchestration-v2/ProjectionStore.ts";
import { threadHasQueuedTurnStart } from "./orchestration-v2/ThreadSettlementService.ts";
import { forkParked } from "./serverActivation.ts";
import * as Settings from "./serverSettings.ts";
import * as TerminalManager from "./terminal/Manager.ts";
import { isFilesystemRoot, managedWorktreesDirectories } from "./worktreesDirectory.ts";
import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
import { withWorkspaceLease } from "./workspace/workspaceLease.ts";
const decodeCleanupThread = Schema.decodeUnknownEffect(
Schema.fromJsonString(OrchestrationV2AppThreadJson),
);
const decodeCleanupSession = Schema.decodeUnknownEffect(
Schema.fromJsonString(OrchestrationV2ProviderSessionJson),
);
const DAY_MS = 86_400_000;
const worktreeCleanupEnabled = (rules: WorktreeCleanupRules) =>
rules.worktreeAfterDays !== null ||
rules.worktreeOnMerge ||
rules.worktreeOnDelete ||
rules.worktreeUnchanged;
function anyWorktreePolicy(
settings: ServerSettings,
predicate: (rules: WorktreeCleanupRules) => boolean,
): boolean {
return (
predicate(resolveWorktreeCleanup(settings, null)) ||
Object.keys(settings.projectSettingsOverrides).some((projectId) =>
predicate(resolveWorktreeCleanup(settings, projectId as ProjectId)),
)
);
}
function sameProjectWorktreePolicies(left: ServerSettings, right: ServerSettings): boolean {
return [
...new Set([
...Object.keys(left.projectSettingsOverrides),
...Object.keys(right.projectSettingsOverrides),
]),
].every((projectId) =>
Equal.equals(
left.projectSettingsOverrides[projectId as ProjectId]?.worktreeCleanup,
right.projectSettingsOverrides[projectId as ProjectId]?.worktreeCleanup,
),
);
}
/** Live sessions keep their cwd even when no turn is currently running. */
export function storageCleanupThreadIdle(thread: OrchestrationV2ThreadShell, now: number): boolean {
return (
thread.branch !== null &&
thread.worktreePath !== null &&
thread.activeRunId === null &&
(thread.status === "idle" ||
thread.status === "completed" ||
thread.status === "interrupted" ||
thread.status === "failed" ||
thread.status === "cancelled" ||
thread.status === "rolled_back") &&
(thread.pendingBackgroundTasks?.length ?? 0) === 0 &&
thread.pendingRuntimeRequest === null &&
!threadHasQueuedTurnStart(thread, now)
);
}
/** PR metadata refreshes must not reset the inactivity clock. */
export function storageCleanupActivityAt(thread: OrchestrationV2ThreadShell): number {
return Math.max(
...[
thread.createdAt,
thread.latestUserMessageAt,
thread.latestRunRequestedAt,
thread.latestRunStartedAt,
thread.latestRunCompletedAt,
].flatMap((value) => (value == null ? [] : [DateTime.toEpochMillis(value)])),
);
}
/**
* Whether the host's pull request proves this worktree's head was merged. A
* squash or rebase merge leaves the head outside the default branch, so the
* merged pull request then has to name this exact commit.
*/
export function storageCleanupPullRequestMerged(
pullRequest: Pick<
GitManager.GitBranchPullRequest,
"state" | "headRef" | "baseRef" | "headSha"
> | null,
worktree: {
readonly branch: string;
readonly defaultBranch: string;
readonly headSha: string;
readonly integrated: boolean;
},
): boolean {
return (
pullRequest?.state === "merged" &&
(worktree.integrated ||
(pullRequest.headRef === worktree.branch &&
pullRequest.baseRef === worktree.defaultBranch &&
pullRequest.headSha === worktree.headSha))
);
}
export const make = Effect.gen(function* () {
const config = yield* ServerConfig.ServerConfig;
const settingsService = yield* Settings.ServerSettingsService;
const projectStore = yield* ProjectStore.ProjectStoreV2;
const engine = yield* Orchestrator.OrchestratorV2;
const projections = yield* ProjectionStore.ProjectionStoreV2;
const sql = yield* SqlClient.SqlClient;
const git = yield* GitVcsDriver.GitVcsDriver;
const gitManager = yield* GitManager.GitManager;
const terminals = yield* TerminalManager.TerminalManager;
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const liveTerminals = new Map<string, Map<string, TerminalSummary>>();
const noteTerminal = (terminal: TerminalSummary) => {
const threadTerminals =
liveTerminals.get(terminal.threadId) ?? new Map<string, TerminalSummary>();
threadTerminals.set(terminal.terminalId, terminal);
liveTerminals.set(terminal.threadId, threadTerminals);
};
const inside = (root: string, target: string) => {
const relative = path.relative(root, target);
return (
relative !== "" &&
relative !== ".." &&
!relative.startsWith(`..${path.sep}`) &&
!path.isAbsolute(relative)
);
};
const hasTerminal = (worktreePath: string) =>
[...liveTerminals.values()]
.flatMap((entries) => [...entries.values()])
.some((terminal) => {
if (terminal.status !== "starting" && terminal.status !== "running") return false;
const cwd = path.resolve(terminal.cwd);
return (
(terminal.worktreePath !== null &&
path.resolve(terminal.worktreePath) === worktreePath) ||
cwd === worktreePath ||
inside(worktreePath, cwd)
);
});
const readThreads = Effect.fn("StorageCleanup.readThreads")(function* () {
const active = yield* projections.getShellSnapshot();
const archived = yield* projections.getShellSnapshot({ location: "archive" });
const projects = yield* projectStore.listShells();
return { projects, threads: [...active.threads, ...archived.threads] };
});
// Local threads under another project need not have a worktreePath of their own.
const containsProjectRoot = Effect.fn("StorageCleanup.containsProjectRoot")(function* (
worktreePath: string,
projects: ReadonlyArray<{ readonly workspaceRoot: string }>,
) {
for (const project of projects) {
const projectPath = path.resolve(project.workspaceRoot);
if (projectPath === worktreePath || inside(worktreePath, projectPath)) return true;
const realPath = yield* fs
.realPath(projectPath)
.pipe(Effect.orElseSucceed(() => projectPath));
if (realPath === worktreePath || inside(worktreePath, realPath)) return true;
}
return false;
});
const cleanWorktrees = Effect.fn("StorageCleanup.cleanWorktrees")(function* (
serverSettings: ServerSettings,
now: number,
) {
if (!anyWorktreePolicy(serverSettings, worktreeCleanupEnabled)) return;
const roots: Array<string> = [];
for (const directory of managedWorktreesDirectories(
serverSettings,
config.worktreesDir,
path,
)) {
// An unmounted drive only skips its own worktrees.
const root = yield* fs.exists(directory).pipe(
Effect.flatMap((exists) => (exists ? fs.realPath(directory) : Effect.succeed(null))),
Effect.orElseSucceed(() => null),
);
if (root !== null && !isFilesystemRoot(root, path)) roots.push(root);
}
if (roots.length === 0) return;
const hasDeleteRule = anyWorktreePolicy(serverSettings, (rules) => rules.worktreeOnDelete);
const deletedRows = hasDeleteRule
? yield* sql<{ payload_json: string; workspaceRoot: string }>`
SELECT t.payload_json, p.workspace_root AS "workspaceRoot"
FROM orchestration_v2_projection_threads t
JOIN projection_projects p ON p.project_id = t.project_id
WHERE t.deleted_at IS NOT NULL
`
: [];
const deletedThreads = (yield* Effect.forEach(deletedRows, (row) =>
decodeCleanupThread(row.payload_json).pipe(
Effect.map((thread) => ({ ...thread, workspaceRoot: row.workspaceRoot })),
),
)).filter(
(thread) =>
thread.worktreePath !== null &&
thread.branch !== null &&
resolveWorktreeCleanup(serverSettings, thread.projectId).worktreeOnDelete,
);
const snapshot = yield* readThreads();
const refreshedDefaultRefs = new Map<string, Set<string>>();
const groups = Map.groupBy(
snapshot.threads.filter((thread) => thread.worktreePath !== null),
(thread) => path.resolve(thread.worktreePath!),
);
const candidates = [
...[...groups.values()].flatMap((group) => (group.length === 1 ? [group[0]!] : [])),
...deletedThreads.filter((thread) => !groups.has(path.resolve(thread.worktreePath!))),
];
for (const thread of candidates) {
const settings = resolveWorktreeCleanup(serverSettings, thread.projectId);
if (!worktreeCleanupEnabled(settings)) continue;
const worktreePath = path.resolve(thread.worktreePath!);
const deleted = "workspaceRoot" in thread;
const project = deleted
? { workspaceRoot: thread.workspaceRoot }
: snapshot.projects.find((entry) => entry.id === thread.projectId);
if (
project === undefined ||
(!deleted && !storageCleanupThreadIdle(thread, now)) ||
hasTerminal(worktreePath)
)
continue;
yield* Effect.gen(function* () {
if (!(yield* fs.exists(worktreePath))) return;
// Roots are canonical, so compare canonical paths. A symlinked parent
// (a linked drive) is fine; a symlinked worktree directory is not.
const realPath = yield* fs.realPath(worktreePath);
const realParent = yield* fs.realPath(path.dirname(worktreePath));
if (realPath !== path.join(realParent, path.basename(worktreePath))) return;
if (!roots.some((root) => inside(root, realPath))) return;
if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects])) return;
// A linked worktree has a .git file. Never remove a main checkout.
if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File") return;
const status = yield* git.statusDetailsLocal(worktreePath);
if (!status.isRepo || status.branch !== thread.branch || status.hasWorkingTreeChanges)
return;
const head = yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" });
const ignored = yield* git.execute({
operation: "StorageCleanup.ignoredFiles",
cwd: worktreePath,
args: ["ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"],
maxOutputBytes: 64 * 1024,
});
// Ignored files can contain secrets or local datasets. Dependency installs
// are reproducible; every other ignored path prevents automatic removal.
if (
ignored.stdoutTruncated ||
ignored.stdout
.split("\0")
.some((entry) => entry !== "" && !/(^|\/)node_modules\/$/.test(entry))
)
return;
const old =
!deleted &&
settings.worktreeAfterDays !== null &&
storageCleanupActivityAt(thread) < now - settings.worktreeAfterDays * DAY_MS;
let eligible = deleted || old;
if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
const repositoryCwd = path.resolve(project.workspaceRoot);
const remote = yield* git.resolvePrimaryRemoteName(repositoryCwd);
const branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
if (branch === null) return;
const defaultRef = `refs/remotes/${remote}/${branch}`;
const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();
if (!refreshed.has(defaultRef)) {
yield* git.fetchRemoteTrackingBranch({
cwd: repositoryCwd,
remoteName: remote,
remoteBranch: branch,
});
refreshed.add(defaultRef);
refreshedDefaultRefs.set(repositoryCwd, refreshed);
}
const base = yield* git.resolveCommit({
cwd: worktreePath,
revision: defaultRef,
});
const ancestor = yield* git.execute({
operation: "StorageCleanup.integratedBranch",
cwd: worktreePath,
args: ["merge-base", "--is-ancestor", head.commitSha, base.commitSha],
allowNonZeroExit: true,
});
const integrated = ancestor.exitCode === 0;
eligible = integrated && settings.worktreeUnchanged;
if (!eligible && settings.worktreeOnMerge && thread.branch !== null) {
const pullRequest = yield* gitManager.branchPullRequest(
{ cwd: worktreePath, branch: thread.branch },
{ refresh: true },
);
eligible = storageCleanupPullRequestMerged(pullRequest, {
branch: thread.branch,
defaultBranch: branch,
headSha: head.commitSha,
integrated,
});
}
}
if (!eligible) return;
// Re-read after Git/host calls so a queued turn, resumed session or new
// thread sharing this path cancels the removal.
const latestSnapshot = yield* readThreads();
if (yield* containsProjectRoot(worktreePath, [project, ...latestSnapshot.projects])) return;
const latest = latestSnapshot.threads.filter(
(entry) =>
entry.worktreePath !== null && path.resolve(entry.worktreePath) === worktreePath,
);
if (hasTerminal(worktreePath)) return;
if (deleted) {
if (
latest.length > 0 ||
!resolveWorktreeCleanup(yield* settingsService.getSettings, thread.projectId)
.worktreeOnDelete
)
return;
// V2 deletion queues durable cleanup. Do not remove its checkout until
// every effect has finished successfully or was explicitly cancelled.
const pendingCleanup = yield* sql`
SELECT 1 FROM orchestration_v2_effect_outbox
WHERE thread_id = ${thread.id} AND status NOT IN ('succeeded', 'cancelled') LIMIT 1
`;
if (pendingCleanup.length > 0) return;
} else if (
latest.length !== 1 ||
latest[0]!.id !== thread.id ||
!storageCleanupThreadIdle(latest[0]!, now) ||
storageCleanupActivityAt(latest[0]!) !== storageCleanupActivityAt(thread)
)
return;
// Sessions can outlive their run and can be shared across app threads.
const sessionRows = yield* sql<{ payload_json: string }>`
SELECT payload_json FROM orchestration_v2_projection_provider_sessions
WHERE status != 'stopped'
`;
const sessions = yield* Effect.forEach(sessionRows, (row) =>
decodeCleanupSession(row.payload_json),
);
if (
sessions.some((session) => {
const cwd = path.resolve(session.cwd);
return cwd === worktreePath || inside(worktreePath, cwd);
})
)
return;
const finalStatus = yield* git.statusDetailsLocal(worktreePath);
if (
!finalStatus.isRepo ||
finalStatus.branch !== thread.branch ||
finalStatus.hasWorkingTreeChanges
)
return;
if (
(yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" })).commitSha !==
head.commitSha
)
return;
const finalIgnored = yield* git.execute({
operation: "StorageCleanup.ignoredFiles",
cwd: worktreePath,
args: ["ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"],
maxOutputBytes: 64 * 1024,
});
if (
finalIgnored.stdoutTruncated ||
finalIgnored.stdout
.split("\0")
.some((entry) => entry !== "" && !/(^|\/)node_modules\/$/.test(entry))
)
return;
const current = resolveWorktreeCleanup(
yield* settingsService.getSettings,
thread.projectId,
);
if (
Object.keys(settings).some(
(key) =>
current[key as keyof typeof settings] !== settings[key as keyof typeof settings],
)
)
return;
yield* git.removeWorktree({ cwd: project.workspaceRoot, path: worktreePath, force: false });
yield* gitManager.invalidateStatus(project.workspaceRoot);
// Preserve branch and path: ProviderTurnStartService recreates the checkout
// from that branch when the thread is resumed.
yield* Effect.logInfo("storage cleanup removed worktree", { threadId: thread.id });
}).pipe(
(effect) => withWorkspaceLease(worktreePath, effect),
Effect.catch((error) =>
Effect.logDebug("storage cleanup skipped worktree", { threadId: thread.id, error }),
),
);
}
});
const cleanFiles = Effect.fn("StorageCleanup.cleanFiles")(function* (
root: string,
days: number | null,
now: number,
rotatedLogs: boolean,
) {
if (days === null || !(yield* fs.exists(root))) return;
const realRoot = yield* fs.realPath(root);
if (realRoot !== path.resolve(root)) return;
const visit = Effect.fn("StorageCleanup.visitFiles")(function* (
directory: string,
): Effect.fn.Return<void, PlatformError | ServerSettingsError> {
for (const name of yield* fs.readDirectory(directory)) {
const target = path.join(directory, name);
if ((yield* fs.realPath(target)) !== target || !inside(realRoot, target)) continue;
const stat = yield* fs.stat(target);
if (stat.type === "Directory" && rotatedLogs) {
yield* visit(target);
} else if (stat.type === "File" && (!rotatedLogs || /\.(?:log|ndjson)\.\d+$/.test(name))) {
const modified = Option.getOrNull(stat.mtime);
if (modified !== null && modified.getTime() < now - days * DAY_MS) {
const current = (yield* settingsService.getSettings).storageCleanup;
if ((rotatedLogs ? current.logsAfterDays : current.browserArtifactsAfterDays) !== days)
return;
yield* fs.remove(target);
}
}
}
});
yield* visit(realRoot);
});
const sweep = Effect.fn("StorageCleanup.sweep")(function* () {
const serverSettings = yield* settingsService.getSettings;
const settings = serverSettings.storageCleanup;
const now = yield* Clock.currentTimeMillis;
yield* cleanWorktrees(serverSettings, now).pipe(
Effect.catch((error) => Effect.logWarning("worktree cleanup failed", { error })),
);
yield* cleanFiles(
config.browserArtifactsDir,
settings.browserArtifactsAfterDays,
now,
false,
).pipe(
Effect.catch((error) => Effect.logWarning("browser artifact cleanup failed", { error })),
);
yield* cleanFiles(config.logsDir, settings.logsAfterDays, now, true).pipe(
Effect.catch((error) => Effect.logWarning("rotated log cleanup failed", { error })),
);
});
const worker = yield* makeDrainableWorker(() =>
sweep().pipe(
Effect.catchCauseIf(
(cause) => !Cause.hasInterruptsOnly(cause),
(cause) => Effect.logWarning("storage cleanup failed", { cause }),
),
),
);
const start = Effect.fn("StorageCleanup.start")(function* () {
const unsubscribe = yield* terminals.subscribeMetadata((event) =>
Effect.sync(() => {
if (event.type === "snapshot") {
liveTerminals.clear();
for (const terminal of event.terminals) noteTerminal(terminal);
} else if (event.type === "upsert") {
noteTerminal(event.terminal);
} else {
const threadTerminals = liveTerminals.get(event.threadId);
threadTerminals?.delete(event.terminalId);
if (threadTerminals?.size === 0) liveTerminals.delete(event.threadId);
}
}),
);
yield* Effect.addFinalizer(() => Effect.sync(unsubscribe));
const changes = yield* settingsService.subscribeChanges;
const events = engine.streamDomainEvents;
let lastSettings = yield* settingsService.getSettings.pipe(Effect.orDie);
yield* forkParked(
worker
.enqueue(undefined)
.pipe(
Effect.andThen(worker.drain),
Effect.repeat(Schedule.spaced("1 hour")),
Effect.asVoid,
),
);
yield* forkParked(
Stream.runForEach(changes, (settings) => {
if (
Equal.equals(settings.storageCleanup, lastSettings.storageCleanup) &&
Equal.equals(settings.worktreeCleanup, lastSettings.worktreeCleanup) &&
sameProjectWorktreePolicies(settings, lastSettings)
)
return Effect.void;
lastSettings = settings;
return worker.enqueue(undefined);
}),
);
yield* forkParked(
Stream.runForEach(events, (event) =>
(event.type === "thread.deleted" || event.type === "provider-session.updated") &&
anyWorktreePolicy(lastSettings, (rules) => rules.worktreeOnDelete)
? worker.enqueue(undefined)
: Effect.void,
).pipe(
Effect.catchCause((cause) =>
Effect.logWarning("Storage cleanup event stream failed", { cause }),
),
),
);
});
return { start, drain: worker.drain };
});