refactor: centralize delivery/path/media/version lifecycle

This commit is contained in:
Peter Steinberger
2026-03-02 04:04:02 +00:00
parent f4f094fc3b
commit c0bf42f2a8
19 changed files with 616 additions and 152 deletions

View File

@@ -7,7 +7,8 @@ import { openBoundaryFile, type BoundaryFileOpenResult } from "../infra/boundary
import { writeFileWithinRoot } from "../infra/fs-safe.js";
import { PATH_ALIAS_POLICIES, type PathAliasPolicy } from "../infra/path-alias-guards.js";
import { applyUpdateHunk } from "./apply-patch-update.js";
import { assertSandboxPath, resolveSandboxInputPath } from "./sandbox-paths.js";
import { toRelativeSandboxPath, resolvePathFromInput } from "./path-policy.js";
import { assertSandboxPath } from "./sandbox-paths.js";
import type { SandboxFsBridge } from "./sandbox/fs-bridge.js";
const BEGIN_PATCH_MARKER = "*** Begin Patch";
@@ -261,7 +262,7 @@ function resolvePatchFileOps(options: ApplyPatchOptions): PatchFileOps {
await fs.writeFile(filePath, content, "utf8");
return;
}
const relative = toRelativeWorkspacePath(options.cwd, filePath);
const relative = toRelativeSandboxPath(options.cwd, filePath);
await writeFileWithinRoot({
rootDir: options.cwd,
relativePath: relative,
@@ -318,27 +319,13 @@ async function resolvePatchPath(
allowFinalHardlinkForUnlink: aliasPolicy.allowFinalHardlinkForUnlink,
})
).resolved
: resolvePathFromCwd(filePath, options.cwd);
: resolvePathFromInput(filePath, options.cwd);
return {
resolved,
display: toDisplayPath(resolved, options.cwd),
};
}
function resolvePathFromCwd(filePath: string, cwd: string): string {
return path.normalize(resolveSandboxInputPath(filePath, cwd));
}
function toRelativeWorkspacePath(workspaceRoot: string, absolutePath: string): string {
const rootResolved = path.resolve(workspaceRoot);
const resolved = path.resolve(absolutePath);
const relative = path.relative(rootResolved, resolved);
if (!relative || relative === "." || relative.startsWith("..") || path.isAbsolute(relative)) {
throw new Error(`Path escapes sandbox root (${workspaceRoot}): ${absolutePath}`);
}
return relative;
}
function assertBoundaryRead(
opened: BoundaryFileOpenResult,
targetPath: string,