mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-19 16:29:42 +01:00
Preserve Pi conversation files outside temporary SSH credentials
Keep in-place SSH Pi JSONL sessions in the durable task workspace. Continue to remove the private credential home after each turn. Preserve existing session paths when their SSH identity and workspace header still match. Make the real SSH heartbeat fixture use the --session file and JSONL history. Require the same file on the next turn and verify that both temporary homes are gone. The regression failed with a missing session file before the fix. The Pi and session codec suites pass, including old in-place session paths. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
586c6aa091
commit
3b629da77b
4 files changed
+48
-11
No files matched your search
@@ -95,6 +95,12 @@ that used the former home identity can reset once at upgrade. Task files remain
|
||||
in their durable SSH workspace. Older CLI sessions without a saved SSH identity
|
||||
remain unresumable; Core does not guess their source host.
|
||||
|
||||
Pi keeps its JSONL conversation file under `.paperclip/pi/sessions` in the
|
||||
durable SSH task workspace. Its temporary credential home is removed after
|
||||
each turn. A follow-up uses the saved session file only when the SSH identity
|
||||
and the file's workspace header match. Existing readable session paths keep
|
||||
working; files already removed by an older release cannot be recovered.
|
||||
|
||||
|
||||
Each task receives a private directory below the configured SSH workspace path,
|
||||
inside `.paperclip-workspaces`. The task runs in that remote directory. Core
|
||||
|
||||
@@ -304,11 +304,11 @@ describe("pi remote execution", () => {
|
||||
expect(call?.[2]).toContain("deepseek/deepseek-chat-v3.1");
|
||||
});
|
||||
|
||||
it("resumes saved Pi sessions for remote SSH execution only when the identity matches", async () => {
|
||||
it.each([false, true])("resumes saved Pi sessions when the SSH identity matches (inPlace=%s)", async inPlace => {
|
||||
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-pi-remote-resume-"));
|
||||
cleanupDirs.push(rootDir);
|
||||
const workspaceDir = path.join(rootDir, "workspace");
|
||||
const managedRemoteWorkspace = "/remote/workspace/.paperclip-runtime/runs/run-ssh-resume/workspace";
|
||||
const managedRemoteWorkspace = inPlace ? workspaceDir : "/remote/workspace/.paperclip-runtime/runs/run-ssh-resume/workspace";
|
||||
await mkdir(workspaceDir, { recursive: true });
|
||||
|
||||
runSshCommand.mockImplementation(async (...args: unknown[]) => {
|
||||
@@ -329,6 +329,12 @@ describe("pi remote execution", () => {
|
||||
|
||||
await execute({
|
||||
runId: "run-ssh-resume",
|
||||
executionTarget: inPlace ? {
|
||||
kind: "remote", transport: "ssh", remoteCwd: managedRemoteWorkspace,
|
||||
workspaceRealization: { mode: "in_place", authoritativeRoot: managedRemoteWorkspace, pathAliases: [], outboundRestorePaths: [] },
|
||||
spec: { host: "127.0.0.1", port: 2222, username: "fixture", remoteWorkspacePath: "/remote/workspace",
|
||||
remoteCwd: managedRemoteWorkspace, privateKey: "PRIVATE KEY", knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA", strictHostKeyChecking: true },
|
||||
} : undefined,
|
||||
agent: {
|
||||
id: "agent-1",
|
||||
companyId: "company-1",
|
||||
|
||||
@@ -159,9 +159,9 @@ function buildSessionPath(agentId: string, timestamp: string): string {
|
||||
return path.join(PAPERCLIP_SESSIONS_DIR, `${safeTimestamp}-${agentId}.jsonl`);
|
||||
}
|
||||
|
||||
function buildRemoteSessionPath(runtimeRootDir: string, agentId: string, timestamp: string): string {
|
||||
function buildRemoteSessionPath(sessionRootDir: string, agentId: string, timestamp: string): string {
|
||||
const safeTimestamp = timestamp.replace(/[:.]/g, "-");
|
||||
return path.posix.join(runtimeRootDir, "sessions", `${safeTimestamp}-${agentId}.jsonl`);
|
||||
return path.posix.join(sessionRootDir, "sessions", `${safeTimestamp}-${agentId}.jsonl`);
|
||||
}
|
||||
|
||||
function normalizeExecutionCwd(candidate: string, remote: boolean): string {
|
||||
@@ -498,6 +498,9 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
||||
}
|
||||
}
|
||||
const runtimeExecutionTarget = overrideAdapterExecutionTargetRemoteCwd(executionTarget, effectiveExecutionCwd);
|
||||
const remoteSessionRootDir = executionTarget?.kind === "remote" && executionTarget.transport === "ssh"
|
||||
&& executionTarget.workspaceRealization?.mode === "in_place"
|
||||
? path.posix.join(effectiveExecutionCwd, ".paperclip", "pi") : remoteRuntimeRootDir;
|
||||
if (executionTargetIsRemote && adapterExecutionTargetUsesPaperclipBridge(runtimeExecutionTarget)) {
|
||||
paperclipBridge = await startAdapterExecutionTargetPaperclipBridge({
|
||||
runId,
|
||||
@@ -555,8 +558,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
||||
sessionHeaderCwdMatches;
|
||||
const sessionPath = canResumeSession
|
||||
? runtimeSessionId
|
||||
: executionTargetIsRemote && remoteRuntimeRootDir
|
||||
? buildRemoteSessionPath(remoteRuntimeRootDir, agent.id, new Date().toISOString())
|
||||
: executionTargetIsRemote && remoteSessionRootDir
|
||||
? buildRemoteSessionPath(remoteSessionRootDir, agent.id, new Date().toISOString())
|
||||
: buildSessionPath(agent.id, new Date().toISOString());
|
||||
|
||||
if (runtimeSessionId && !canResumeSession) {
|
||||
@@ -891,8 +894,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
||||
"stdout",
|
||||
`[paperclip] Pi session "${runtimeSessionId}" is unavailable; retrying with a fresh session.\n`,
|
||||
);
|
||||
const newSessionPath = executionTargetIsRemote && remoteRuntimeRootDir
|
||||
? buildRemoteSessionPath(remoteRuntimeRootDir, agent.id, new Date().toISOString())
|
||||
const newSessionPath = executionTargetIsRemote && remoteSessionRootDir
|
||||
? buildRemoteSessionPath(remoteSessionRootDir, agent.id, new Date().toISOString())
|
||||
: buildSessionPath(agent.id, new Date().toISOString());
|
||||
if (executionTargetIsRemote) {
|
||||
await ensureAdapterExecutionTargetFile(runId, executionTarget, newSessionPath, {
|
||||
|
||||
@@ -229,10 +229,20 @@ if (process.argv.includes('models')) { console.log(${JSON.stringify(model)}); pr
|
||||
if (process.env[${JSON.stringify(authEnv)}] !== 'synthetic-selected-ssh-key') process.exit(91);
|
||||
const fs = require('fs'), path = require('path');
|
||||
const harness = ${JSON.stringify(adapterType)};
|
||||
const history = harness === 'claude_local' ? path.join(process.env.CLAUDE_CONFIG_DIR, 'projects', 'fixture-history') : path.join(process.cwd(), '.fixture-provider-history');
|
||||
const piSession = harness === 'pi_local' ? process.argv[process.argv.indexOf('--session') + 1] : null;
|
||||
const history = harness === 'claude_local' ? path.join(process.env.CLAUDE_CONFIG_DIR, 'projects', 'fixture-history')
|
||||
: piSession || path.join(process.cwd(), '.fixture-provider-history');
|
||||
const runTask = (resumed) => {
|
||||
if (resumed && !fs.existsSync(history)) throw Error('Missing provider conversation history');
|
||||
fs.mkdirSync(path.dirname(history), {recursive:true}); fs.writeFileSync(history, 'provider conversation state');
|
||||
fs.mkdirSync(path.dirname(history), {recursive:true});
|
||||
if (piSession) {
|
||||
const previous = resumed ? fs.readFileSync(piSession,'utf8') : '';
|
||||
if (resumed && JSON.parse(previous.split('\\n')[0]).cwd !== process.cwd()) throw Error('Invalid Pi session header');
|
||||
if (resumed && !previous.includes('first-turn-history')) throw Error('Missing Pi conversation messages');
|
||||
if (!resumed) fs.writeFileSync(piSession,JSON.stringify({type:'session',version:3,id:'fixture',cwd:process.cwd()})+'\\n');
|
||||
fs.appendFileSync(piSession,JSON.stringify({type:'message',message:{role:'assistant',content:resumed?'continued-history':'first-turn-history'}})+'\\n');
|
||||
fs.writeFileSync('pi-session-path.json',JSON.stringify({session:piSession,home:process.env.HOME}));
|
||||
} else fs.writeFileSync(history, 'provider conversation state');
|
||||
fs.writeFileSync('continuation.json', JSON.stringify({resumed}));
|
||||
require('fs').writeFileSync('hello_world.txt', 'Hello World\\n');
|
||||
};
|
||||
@@ -263,7 +273,7 @@ if (process.argv.includes('--acp-fixture')) {
|
||||
const id = '12345678-1234-4abc-9def-123456789012';
|
||||
const resumed = ['--resume', 'resume', '-r'].some(arg => process.argv.includes(arg))
|
||||
|| (harness === 'opencode_local' && process.argv.includes('--session'))
|
||||
|| (harness === 'pi_local' && fs.existsSync('hello_world.txt'));
|
||||
|| Boolean(piSession && fs.existsSync(piSession) && fs.statSync(piSession).size > 0);
|
||||
runTask(resumed);
|
||||
const emit = value => console.log(JSON.stringify(value));
|
||||
if (harness === 'codex_local') {
|
||||
@@ -334,6 +344,11 @@ if (process.argv.includes('--acp-fixture')) {
|
||||
const authority = finished!.runnerProfileJson!.remoteWorkspace as { workspaceId: string; state: string; remoteCwd: string };
|
||||
expect(authority.state).toBe("saved");
|
||||
expect(await readFile(path.join(authority.remoteCwd, "hello_world.txt"), "utf8")).toBe("Hello World\n");
|
||||
const piState = adapterType === "pi_local" ? JSON.parse(await readFile(path.join(authority.remoteCwd, "pi-session-path.json"), "utf8")) : null;
|
||||
if (piState) {
|
||||
expect(await readFile(piState.session, "utf8")).toContain("first-turn-history");
|
||||
await expect(fs.stat(piState.home)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
}
|
||||
const [workspace] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, authority.workspaceId));
|
||||
expect(readRemoteWorkspaceAuthority(workspace.metadata)).toMatchObject({ writerRunId: null, dirty: false });
|
||||
const comment = await issueService(db).addComment(issueId, "Confirm the file still exists.", { userId });
|
||||
@@ -348,6 +363,13 @@ if (process.argv.includes('--acp-fixture')) {
|
||||
expect(continued).toMatchObject({ status: "succeeded" });
|
||||
const continuation = JSON.parse(await readFile(path.join(authority.remoteCwd, "continuation.json"), "utf8"));
|
||||
expect(continuation).toEqual({ resumed: true });
|
||||
if (piState) {
|
||||
const next = JSON.parse(await readFile(path.join(authority.remoteCwd, "pi-session-path.json"), "utf8"));
|
||||
expect(next.session).toBe(piState.session);
|
||||
expect(next.home).not.toBe(piState.home);
|
||||
expect(await readFile(next.session, "utf8")).toContain("continued-history");
|
||||
await expect(fs.stat(next.home)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
}
|
||||
} finally {
|
||||
await heartbeat.drainActiveRunExecutions();
|
||||
executing.mockRestore();
|
||||
|
||||
Reference in new issue
Block a user