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 9a36bad7..140ec366 100644 --- a/packages/adapters/pi-local/src/server/execute.remote.test.ts +++ b/packages/adapters/pi-local/src/server/execute.remote.test.ts @@ -221,6 +221,22 @@ describe("pi remote execution", () => { const workspaceDir = path.join(rootDir, "workspace"); await mkdir(workspaceDir, { recursive: true }); + runSshCommand.mockImplementation(async (...args: unknown[]) => { + const command = String(args[1] ?? ""); + if (command.includes("head -n 1") && command.includes("session-123.jsonl")) { + return { + stdout: `${JSON.stringify({ type: "session", cwd: "/remote/workspace" })}\n`, + stderr: "", + exitCode: 0, + }; + } + return { + stdout: "", + stderr: "", + exitCode: 0, + }; + }); + await execute({ runId: "run-ssh-resume", agent: { @@ -275,4 +291,218 @@ describe("pi remote execution", () => { expect(call?.[2]).toContain("--session"); expect(call?.[2]).toContain("/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl"); }); + + it("starts a fresh remote Pi session when the saved session header cwd points at a different workspace", async () => { + const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-pi-remote-stale-session-")); + cleanupDirs.push(rootDir); + const workspaceDir = path.join(rootDir, "workspace"); + await mkdir(workspaceDir, { recursive: true }); + + runSshCommand.mockImplementation(async (...args: unknown[]) => { + const command = String(args[1] ?? ""); + if (command.includes("head -n 1") && command.includes("session-123.jsonl")) { + return { + stdout: `${JSON.stringify({ type: "session", cwd: "/remote/old-workspace" })}\n`, + stderr: "", + exitCode: 0, + }; + } + return { + stdout: "", + stderr: "", + exitCode: 0, + }; + }); + + await execute({ + runId: "run-ssh-stale-session", + agent: { + id: "agent-1", + companyId: "company-1", + name: "Pi Builder", + adapterType: "pi_local", + adapterConfig: {}, + }, + runtime: { + sessionId: "/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl", + sessionParams: { + sessionId: "/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl", + cwd: "/remote/workspace", + remoteExecution: { + transport: "ssh", + host: "127.0.0.1", + port: 2222, + username: "fixture", + remoteCwd: "/remote/workspace", + }, + }, + sessionDisplayId: "session-123", + taskKey: null, + }, + config: { + command: "pi", + model: "openai/gpt-5.4-mini", + }, + context: { + paperclipWorkspace: { + cwd: workspaceDir, + source: "project_primary", + }, + }, + executionTransport: { + remoteExecution: { + host: "127.0.0.1", + port: 2222, + username: "fixture", + remoteWorkspacePath: "/remote/workspace", + remoteCwd: "/remote/workspace", + privateKey: "PRIVATE KEY", + knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA", + strictHostKeyChecking: true, + }, + }, + onLog: async () => {}, + }); + + const call = runChildProcess.mock.calls[0] as unknown as [string, string, string[]] | undefined; + const sessionIndex = call?.[2].indexOf("--session") ?? -1; + expect(sessionIndex).toBeGreaterThanOrEqual(0); + const usedSession = sessionIndex >= 0 ? call?.[2][sessionIndex + 1] : null; + expect(usedSession).toContain("/remote/workspace/.paperclip-runtime/pi/sessions/"); + expect(usedSession).not.toBe("/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl"); + }); + + it("starts a fresh remote Pi session when the saved session header is empty or unreadable", async () => { + const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-pi-remote-empty-header-")); + cleanupDirs.push(rootDir); + const workspaceDir = path.join(rootDir, "workspace"); + await mkdir(workspaceDir, { recursive: true }); + + runSshCommand.mockImplementation(async (...args: unknown[]) => { + const command = String(args[1] ?? ""); + if (command.includes("head -n 1") && command.includes("session-123.jsonl")) { + return { stdout: "", stderr: "", exitCode: 0 }; + } + return { stdout: "", stderr: "", exitCode: 0 }; + }); + + await execute({ + runId: "run-ssh-empty-header", + agent: { + id: "agent-1", + companyId: "company-1", + name: "Pi Builder", + adapterType: "pi_local", + adapterConfig: {}, + }, + runtime: { + sessionId: "/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl", + sessionParams: { + sessionId: "/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl", + cwd: "/remote/workspace", + remoteExecution: { + transport: "ssh", + host: "127.0.0.1", + port: 2222, + username: "fixture", + remoteCwd: "/remote/workspace", + }, + }, + sessionDisplayId: "session-123", + taskKey: null, + }, + config: { command: "pi", model: "openai/gpt-5.4-mini" }, + context: { + paperclipWorkspace: { cwd: workspaceDir, source: "project_primary" }, + }, + executionTransport: { + remoteExecution: { + host: "127.0.0.1", + port: 2222, + username: "fixture", + remoteWorkspacePath: "/remote/workspace", + remoteCwd: "/remote/workspace", + privateKey: "PRIVATE KEY", + knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA", + strictHostKeyChecking: true, + }, + }, + onLog: async () => {}, + }); + + const call = runChildProcess.mock.calls[0] as unknown as [string, string, string[]] | undefined; + const sessionIndex = call?.[2].indexOf("--session") ?? -1; + expect(sessionIndex).toBeGreaterThanOrEqual(0); + const usedSession = sessionIndex >= 0 ? call?.[2][sessionIndex + 1] : null; + expect(usedSession).not.toBe("/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl"); + }); + + it("starts a fresh remote Pi session when the remote head command fails", async () => { + const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-pi-remote-head-failure-")); + cleanupDirs.push(rootDir); + const workspaceDir = path.join(rootDir, "workspace"); + await mkdir(workspaceDir, { recursive: true }); + + runSshCommand.mockImplementation(async (...args: unknown[]) => { + const command = String(args[1] ?? ""); + if (command.includes("head -n 1") && command.includes("session-123.jsonl")) { + throw Object.assign(new Error("ssh: connect failed"), { + stdout: "", + stderr: "ssh: connect failed", + code: "ENOENT", + }); + } + return { stdout: "", stderr: "", exitCode: 0 }; + }); + + await execute({ + runId: "run-ssh-head-failure", + agent: { + id: "agent-1", + companyId: "company-1", + name: "Pi Builder", + adapterType: "pi_local", + adapterConfig: {}, + }, + runtime: { + sessionId: "/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl", + sessionParams: { + sessionId: "/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl", + cwd: "/remote/workspace", + remoteExecution: { + transport: "ssh", + host: "127.0.0.1", + port: 2222, + username: "fixture", + remoteCwd: "/remote/workspace", + }, + }, + sessionDisplayId: "session-123", + taskKey: null, + }, + config: { command: "pi", model: "openai/gpt-5.4-mini" }, + context: { + paperclipWorkspace: { cwd: workspaceDir, source: "project_primary" }, + }, + executionTransport: { + remoteExecution: { + host: "127.0.0.1", + port: 2222, + username: "fixture", + remoteWorkspacePath: "/remote/workspace", + remoteCwd: "/remote/workspace", + privateKey: "PRIVATE KEY", + knownHosts: "[127.0.0.1]:2222 ssh-ed25519 AAAA", + strictHostKeyChecking: true, + }, + }, + onLog: async () => {}, + }); + + const call = runChildProcess.mock.calls[0] as unknown as [string, string, string[]] | undefined; + const sessionIndex = call?.[2].indexOf("--session") ?? -1; + expect(sessionIndex).toBeGreaterThanOrEqual(0); + const usedSession = sessionIndex >= 0 ? call?.[2][sessionIndex + 1] : null; + expect(usedSession).not.toBe("/remote/workspace/.paperclip-runtime/pi/sessions/session-123.jsonl"); + }); }); diff --git a/packages/adapters/pi-local/src/server/execute.ts b/packages/adapters/pi-local/src/server/execute.ts index 1d1172b7..f0b659b8 100644 --- a/packages/adapters/pi-local/src/server/execute.ts +++ b/packages/adapters/pi-local/src/server/execute.ts @@ -17,6 +17,7 @@ import { readAdapterExecutionTarget, resolveAdapterExecutionTargetCommandForLogs, runAdapterExecutionTargetProcess, + runAdapterExecutionTargetShellCommand, startAdapterExecutionTargetPaperclipBridge, } from "@paperclipai/adapter-utils/execution-target"; import { @@ -41,6 +42,7 @@ import { DEFAULT_PAPERCLIP_AGENT_PROMPT_TEMPLATE, runChildProcess, } from "@paperclipai/adapter-utils/server-utils"; +import { shellQuote } from "@paperclipai/adapter-utils/ssh"; import { isPiUnknownSessionError, parsePiJsonl } from "./parse.js"; import { ensurePiModelConfiguredAndAvailable } from "./models.js"; @@ -143,6 +145,68 @@ function buildRemoteSessionPath(runtimeRootDir: string, agentId: string, timesta return path.posix.join(runtimeRootDir, "sessions", `${safeTimestamp}-${agentId}.jsonl`); } +function normalizeExecutionCwd(candidate: string, remote: boolean): string { + return remote ? path.posix.normalize(candidate) : path.resolve(candidate); +} + +function executionCwdsMatch(saved: string, current: string, remote: boolean): boolean { + return normalizeExecutionCwd(saved, remote) === normalizeExecutionCwd(current, remote); +} + +function readSessionHeaderCwd(raw: string): string | null { + const headerLine = raw + .split(/\r?\n/) + .map((line) => line.trim()) + .find(Boolean); + if (!headerLine) return null; + try { + const parsed = JSON.parse(headerLine) as Record; + if (parsed.type !== "session") return null; + const cwd = typeof parsed.cwd === "string" ? parsed.cwd.trim() : ""; + return cwd.length > 0 ? cwd : null; + } catch { + return null; + } +} + +async function readSavedSessionCwd(input: { + runId: string; + sessionPath: string; + executionTarget: ReturnType; + cwd: string; + env: Record; + timeoutSec: number; + graceSec: number; +}): Promise { + if (!input.sessionPath.trim()) return null; + + if (!adapterExecutionTargetIsRemote(input.executionTarget)) { + try { + return readSessionHeaderCwd(await fs.readFile(input.sessionPath, "utf8")); + } catch { + return null; + } + } + + try { + const sessionHeader = await runAdapterExecutionTargetShellCommand( + input.runId, + input.executionTarget, + `if [ -f ${shellQuote(input.sessionPath)} ]; then head -n 1 ${shellQuote(input.sessionPath)}; fi`, + { + cwd: input.cwd, + env: input.env, + timeoutSec: input.timeoutSec > 0 ? Math.min(input.timeoutSec, 15) : 15, + graceSec: input.graceSec, + }, + ); + if (sessionHeader.timedOut || (sessionHeader.exitCode ?? 0) !== 0) return null; + return readSessionHeaderCwd(sessionHeader.stdout); + } catch { + return null; + } +} + export async function execute(ctx: AdapterExecutionContext): Promise { const { runId, agent, runtime, config, context, onLog, onMeta, onSpawn, authToken } = ctx; const executionTarget = readAdapterExecutionTarget({ @@ -373,10 +437,31 @@ export async function execute(ctx: AdapterExecutionContext): Promise 0 + ? await readSavedSessionCwd({ + runId, + sessionPath: runtimeSessionId, + executionTarget, + cwd, + env, + timeoutSec, + graceSec, + }) + : null; + const sessionHeaderCwdMatches = + runtimeSessionId.length === 0 || + (savedSessionCwd !== null && + executionCwdsMatch(savedSessionCwd, effectiveExecutionCwd, executionTargetIsRemote)); const canResumeSession = runtimeSessionId.length > 0 && - (runtimeSessionCwd.length === 0 || path.resolve(runtimeSessionCwd) === path.resolve(effectiveExecutionCwd)) && - adapterExecutionTargetSessionMatches(runtimeRemoteExecution, executionTarget); + sessionTargetMatches && + sessionParamsCwdMatches && + sessionHeaderCwdMatches; const sessionPath = canResumeSession ? runtimeSessionId : executionTargetIsRemote && remoteRuntimeRootDir @@ -384,11 +469,15 @@ export async function execute(ctx: AdapterExecutionContext): Promise