mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-16 03:31:44 +01:00
Use the agent-selected AI account for new SSH environments. Preserve
remote-machine authentication for existing environments and expose it as
an advanced option. Keep managed credentials private while preserving
supported conversation state and credential refreshes across turns.
Validated with focused route, adapter, lifecycle, and real-process tests;
final CI passed on d634944df1.
Greptile Apex reviewed that revision at 5/5 with no unresolved threads.
External neutral security reports remain documented in the pull request.
Co-Authored-By: Paperclip <noreply@paperclip.ing>
49 lines
2.1 KiB
TypeScript
49 lines
2.1 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import path from "node:path";
|
|
import { shellQuote } from "./ssh.js";
|
|
|
|
/** Retain provider-owned conversation files while scrubbing per-turn credentials. */
|
|
export async function prepareSshConversationState(input: {
|
|
adapterKey: string;
|
|
preserve: readonly string[];
|
|
remoteRoot: string;
|
|
companyId: string;
|
|
agentId: string;
|
|
accountIdentity: string;
|
|
executionIdentity: unknown;
|
|
runId: string;
|
|
savedScope: unknown;
|
|
savedConversationId: unknown;
|
|
stagedHome?: string;
|
|
run: (command: string) => Promise<void>;
|
|
}) {
|
|
if (!/^[a-z0-9_-]+$/.test(input.adapterKey) || input.preserve.length === 0
|
|
|| input.preserve.some(pattern => !/^[A-Za-z0-9_.*-]+$/.test(pattern))) {
|
|
throw new Error("Invalid SSH conversation retention policy");
|
|
}
|
|
const hash = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
const scope = hash([input.companyId, input.agentId, input.accountIdentity, input.executionIdentity]);
|
|
const savedId = input.savedConversationId;
|
|
const canResume = input.savedScope === scope && typeof savedId === "string" && /^[a-f0-9]{64}$/.test(savedId);
|
|
const conversationId = canResume ? savedId : hash(input.runId);
|
|
const home = path.posix.join(input.remoteRoot, ".paperclip-runtime", input.adapterKey, "conversations", scope, conversationId);
|
|
const prefix = `set -eu\numask 077\nhome=${shellQuote(home)}\n[ ! -L "$home" ]\n`;
|
|
const stateOnly = `for entry in "$home"/* "$home"/.[!.]* "$home"/..?*; do
|
|
[ -e "$entry" ] || [ -L "$entry" ] || continue
|
|
if [ ! -L "$entry" ]; then
|
|
case "\${entry##*/}" in
|
|
${input.preserve.join("|")} ) continue ;;
|
|
esac
|
|
fi
|
|
rm -rf -- "$entry"
|
|
done`;
|
|
const cleanup = () => input.run(`${prefix}if [ -d "$home" ]; then\n${stateOnly}\nfi`);
|
|
try {
|
|
await input.run(`${prefix}mkdir -p "$home"\nchmod 700 "$home"\n${stateOnly}${input.stagedHome ? `\ncp -RP ${shellQuote(input.stagedHome + "/.")} "$home/"` : ""}`);
|
|
} catch (error) {
|
|
await cleanup();
|
|
throw error;
|
|
}
|
|
return { home, scope, conversationId, canResume, cleanup };
|
|
}
|