mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-24 18:19:05 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agent adapters restore sandbox work into project workspaces after a run > - The restore lock used the target workspace parent, which can reject writes > - The teardown then hid restore errors, so a run could report success with lost work > - This pull request moves the lock into an instance-scoped root and reports safe restore failure codes > - The benefit is reliable restore coordination and visible failure evidence without changing run success semantics ## Linked Issues or Issue Description Refs: #10914 ## What Changed - Move the workspace-restore merge lock into a private, instance-scoped root. - Derive the lock key from the canonical target path with SHA-256. - Resolve the lock root from the caller environment and reject unsafe root types. - Classify restore failures with three allowlisted codes. - Add the failure code to run result JSON without exposing a host path or process identifier. - Keep restore failure fail-open for the run exit code and run status. ## Verification - Run `npx vitest run packages/adapter-utils/src/workspace-restore-merge.test.ts`. - Run `npx vitest run packages/adapter-utils/src/acpx-engine/run-fault-matrix.test.ts`. - Run the four Codex credential suites. - Confirm the branch includes the current `master` commit and no manual lockfile edit. - Confirm all pull request checks and the Greptile review reach a terminal green state. ## Risks - The lock path changes for workspace restore and removes the sibling-directory fallback. - A misconfigured or inaccessible instance home can still stop lock setup. - Restore remains fail-open, so callers must inspect the result evidence when a restore fails. ## Model Used OpenAI GPT-5. The model used tool calls and code execution to validate and route an author-provided change. The implementing engineer authored the code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes: #` / `Refs: #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
83 lines
3.6 KiB
TypeScript
83 lines
3.6 KiB
TypeScript
import { open, rename, rm } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { randomUUID } from "node:crypto";
|
|
import { withDirectoryMergeLock } from "@paperclipai/adapter-utils/workspace-restore-merge";
|
|
import { USE_SOURCE_EXIT, decideCodexAuthMerge } from "./codex-auth-merge-decision.js";
|
|
|
|
// The one atomic credential writer. It stages the source bytes into a private
|
|
// (0600) temp next to the destination, runs the shared decision predicate, and
|
|
// on a use-source decision renames the temp over the destination. The rename is
|
|
// an atomic same-directory swap that preserves mode 0600. The whole write runs
|
|
// under the directory merge lock, so a concurrent restore, copy-back, or
|
|
// promotion can never interleave. The per-identity cache slot and the
|
|
// device-login company home both use this writer, so the staged-rename and lock
|
|
// logic lives in one place. It never logs token bytes; the caller passes the
|
|
// fixed status lines.
|
|
|
|
export type WriteCredentialSeedOrNewerOutcome = "written" | "kept";
|
|
|
|
export interface WriteCredentialSeedOrNewerInput {
|
|
/** The source credential bytes to (maybe) install. */
|
|
sourceBytes: Buffer;
|
|
/** The absolute destination path to (maybe) overwrite. */
|
|
destinationPath: string;
|
|
/** Fill an absent destination from a usable subscription source. */
|
|
seedIfDestAbsent: boolean;
|
|
/** A non-leaking progress sink. It receives only the two fixed status lines. */
|
|
log: (line: string) => void | Promise<void>;
|
|
/** The fixed status line for a use-source write. It carries no secret data. */
|
|
writtenLine: string;
|
|
/** The fixed status line for a keep-destination decision. */
|
|
keptLine: string;
|
|
/** A safe, non-secret prefix for the staged temp file name. */
|
|
tempPrefix: string;
|
|
/** The caller name that prefixes a predicate error. */
|
|
errorLabel: string;
|
|
/** Environment for the merge lock root. Defaults to `process.env`. */
|
|
env?: NodeJS.ProcessEnv;
|
|
}
|
|
|
|
/**
|
|
* Writes `sourceBytes` over `destinationPath` when the shared predicate returns a
|
|
* use-source decision, else keeps the destination. Seeds an absent destination
|
|
* only when `seedIfDestAbsent` is set and the source is a usable subscription
|
|
* credential. The staged temp is always removed, so a failure never leaves a
|
|
* partial file.
|
|
*/
|
|
export async function writeCredentialSeedOrNewer(
|
|
input: WriteCredentialSeedOrNewerInput,
|
|
): Promise<WriteCredentialSeedOrNewerOutcome> {
|
|
const destinationDir = path.dirname(input.destinationPath);
|
|
return withDirectoryMergeLock(
|
|
destinationDir,
|
|
async () => {
|
|
const stagedTempPath = path.join(
|
|
destinationDir,
|
|
`.${input.tempPrefix}-${process.pid}-${randomUUID()}.tmp`,
|
|
);
|
|
// `wx` + explicit mode create the temp private (0600) and fail if it already
|
|
// exists, so the writer never writes through a pre-existing symlink.
|
|
const handle = await open(stagedTempPath, "wx", 0o600);
|
|
try {
|
|
await handle.writeFile(input.sourceBytes);
|
|
await handle.close();
|
|
const decision = await decideCodexAuthMerge(stagedTempPath, input.destinationPath, {
|
|
seedIfDestAbsent: input.seedIfDestAbsent,
|
|
errorLabel: input.errorLabel,
|
|
});
|
|
if (decision === USE_SOURCE_EXIT) {
|
|
await rename(stagedTempPath, input.destinationPath);
|
|
await input.log(input.writtenLine);
|
|
return "written";
|
|
}
|
|
await input.log(input.keptLine);
|
|
return "kept";
|
|
} finally {
|
|
await handle.close().catch(() => undefined);
|
|
await rm(stagedTempPath, { force: true }).catch(() => undefined);
|
|
}
|
|
},
|
|
input.env,
|
|
);
|
|
}
|