diff --git a/doc/tailscale-environments.md b/doc/tailscale-environments.md index a642cf379b1..e966cf1b5ee 100644 --- a/doc/tailscale-environments.md +++ b/doc/tailscale-environments.md @@ -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 diff --git a/packages/adapters/pi-local/src/server/execute.remote.test.ts b/packages/adapters/pi-local/src/server/execute.remote.test.ts index 6d717d7ea0d..bba89d1ef7e 100644 --- a/packages/adapters/pi-local/src/server/execute.remote.test.ts +++ b/packages/adapters/pi-local/src/server/execute.remote.test.ts @@ -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", diff --git a/packages/adapters/pi-local/src/server/execute.ts b/packages/adapters/pi-local/src/server/execute.ts index 8a14775c737..39be70e0bcf 100644 --- a/packages/adapters/pi-local/src/server/execute.ts +++ b/packages/adapters/pi-local/src/server/execute.ts @@ -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 { 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();