diff --git a/packages/app/src/tenant.ts b/packages/app/src/tenant.ts index 3f6be572..dd46dea2 100644 --- a/packages/app/src/tenant.ts +++ b/packages/app/src/tenant.ts @@ -954,9 +954,9 @@ export class TenantV2 extends DurableObject { if (typeof run.exec_response === "string" && run.exec_response) run.exec_response = await decrypt(run.exec_response, this.env.CREDENTIAL_ENCRYPTION_KEY); if (typeof run.prompt_delivery_request === "string" && run.prompt_delivery_request) run.prompt_delivery_request = await decrypt(run.prompt_delivery_request, this.env.CREDENTIAL_ENCRYPTION_KEY); if (typeof run.prompt_delivery_response === "string" && run.prompt_delivery_response) run.prompt_delivery_response = await decrypt(run.prompt_delivery_response, this.env.CREDENTIAL_ENCRYPTION_KEY); - // Duplicate-webhook records predate encrypted results and deliberately retain - // a plain-text explanation. Only completed/failed agent output is encrypted. - if (run.state !== "ignored" && typeof run.result === "string" && run.result) run.result = await decrypt(run.result, this.env.CREDENTIAL_ENCRYPTION_KEY); + // New transcripts are scrubbed plaintext; readTranscript keeps legacy + // encrypted rows accessible during migration. + if (run.state !== "ignored" && typeof run.result === "string" && run.result) run.result = await this.readTranscript(run.result); })); return json({ flow, events, runs, pagination: { page, hasNext } }); } @@ -986,9 +986,10 @@ export class TenantV2 extends DurableObject { const run = this.one("SELECT runs.*, pipes.name AS flow_name FROM runs JOIN pipes ON pipes.id = runs.pipe_id WHERE runs.id = ?", runId); if (!run) return new Response("Not found", { status: 404 }); await this.backfillRunIssueDetails([run]); - for (const field of ["prompt", "exec_request", "exec_response", "prompt_delivery_request", "prompt_delivery_response", "result"] as const) { + for (const field of ["prompt", "exec_request", "exec_response", "prompt_delivery_request", "prompt_delivery_response"] as const) { if (run.state !== "ignored" && typeof run[field] === "string" && run[field]) run[field] = await decrypt(String(run[field]), this.env.CREDENTIAL_ENCRYPTION_KEY); } + if (run.state !== "ignored" && typeof run.result === "string" && run.result) run.result = await this.readTranscript(run.result); return json(run); } @@ -1082,7 +1083,7 @@ export class TenantV2 extends DurableObject { } for (const run of this.rows(`SELECT r.id,r.pipe_id,r.issue_id,r.issue_title,r.run_name,r.agent_name,r.workspace_name,r.agent_kind,r.state,r.provider,r.session,r.result,r.created_at FROM runs r ORDER BY r.created_at DESC LIMIT 1000`)) { const title = String(run.run_name || run.issue_title || `Run ${String(run.id).slice(0, 8)}`); - const session = run.session ? await decrypt(String(run.session), this.env.CREDENTIAL_ENCRYPTION_KEY) : (run.result ? await decrypt(String(run.result), this.env.CREDENTIAL_ENCRYPTION_KEY) : ""); + const session = run.session ? await this.readTranscript(String(run.session)) : (run.result ? await this.readTranscript(String(run.result)) : ""); add("run", run.id, title, `${run.state} ยท ${run.agent_name || run.agent_kind || run.provider || "run"}`, `/job-runs/${encodeURIComponent(String(run.id))}`, { job: run.pipe_id, issue_id: run.issue_id, issue_title: run.issue_title, run_name: run.run_name, agent: run.agent_name, agent_kind: run.agent_kind, session }); } for (const event of this.rows("SELECT id,job_id,provider,delivery_id,outcome,detail,received_at FROM job_events ORDER BY received_at DESC LIMIT 1000")) { @@ -1115,8 +1116,8 @@ export class TenantV2 extends DurableObject { for (const key of ["invocation_id", "invocation_job_id", "invocation_source", "invocation_trigger_id", "invocation_claim_key", "invocation_occurrence", "invocation_created_at"]) delete run[key]; if (typeof run.encrypted_prompt === "string" && run.encrypted_prompt) run.prompt = await decrypt(run.encrypted_prompt, this.env.CREDENTIAL_ENCRYPTION_KEY); delete run.encrypted_prompt; - if (run.state !== "ignored" && typeof run.result === "string" && run.result) run.result = await decrypt(run.result, this.env.CREDENTIAL_ENCRYPTION_KEY); - if (run.state !== "ignored" && typeof run.session === "string" && run.session) run.session = await decrypt(run.session, this.env.CREDENTIAL_ENCRYPTION_KEY); + if (run.state !== "ignored" && typeof run.result === "string" && run.result) run.result = await this.readTranscript(run.result); + if (run.state !== "ignored" && typeof run.session === "string" && run.session) run.session = await this.readTranscript(run.session); if (!run.session && run.result) run.session = run.result; run.activity = this.rows("SELECT action, detail, created_at FROM run_activity WHERE run_id = ? ORDER BY created_at, id", runId); this.presentExecution(run); @@ -1133,7 +1134,7 @@ export class TenantV2 extends DurableObject { const scrubbed = safeSession(output); if (scrubbed !== null) { run.live_output = scrubbed; - this.ctx.storage.sql.exec("UPDATE runs SET session=? WHERE id=?", await encrypt(scrubbed, this.env.CREDENTIAL_ENCRYPTION_KEY), run.id); + this.ctx.storage.sql.exec("UPDATE runs SET session=? WHERE id=?", scrubbed, run.id); } } } catch { @@ -1195,7 +1196,7 @@ export class TenantV2 extends DurableObject { } } } - this.ctx.storage.sql.exec("UPDATE runs SET state = 'stopped', result = ?, updated_at = ? WHERE id = ?", await encrypt("Stopped by user", this.env.CREDENTIAL_ENCRYPTION_KEY), now(), runId); + this.ctx.storage.sql.exec("UPDATE runs SET state = 'stopped', result = ?, updated_at = ? WHERE id = ?", "Stopped by user", now(), runId); this.ctx.storage.sql.exec("UPDATE job_runs SET state='stopped',updated_at=? WHERE id=?", now(), runId); this.ctx.storage.sql.exec("DELETE FROM active_claims WHERE run_id = ?", runId); await this.afterJobTerminal(String(run.pipe_id), runId, "stopped"); @@ -1216,7 +1217,7 @@ export class TenantV2 extends DurableObject { // Terminalize first. Backend deletion is cleanup work and must never keep a // dead run holding a concurrency slot. - const result = await encrypt("Killed by user", this.env.CREDENTIAL_ENCRYPTION_KEY); + const result = "Killed by user"; this.ctx.storage.sql.exec("UPDATE runs SET state='stopped',result=?,claim_released=1,updated_at=? WHERE id=?", result, timestamp, runId); this.ctx.storage.sql.exec("UPDATE job_runs SET state='stopped',updated_at=? WHERE id=?", timestamp, runId); this.ctx.storage.sql.exec("DELETE FROM active_claims WHERE run_id=?", runId); @@ -1617,6 +1618,13 @@ export class TenantV2 extends DurableObject { this.ctx.storage.sql.exec("INSERT INTO run_activity VALUES (?,?,?,?,?)", id(), runId, action, detail.slice(0, 1000), now()); } + /** New transcripts are scrubbed plaintext; decrypt only legacy AES-GCM rows. */ + private async readTranscript(value: string): Promise { + if (!/^[A-Za-z0-9+/]{16}=*\.[A-Za-z0-9+/]+=*$/.test(value)) return value; + try { return await decrypt(value, this.env.CREDENTIAL_ENCRYPTION_KEY); } + catch { return value; } + } + private diagnosticExcerpt(body: string): string { return body.slice(-700) .replace(/("(?:authorization|cookie|token|secret|credential|prompt|body)"\s*:\s*")[^"]*/gi, "$1[redacted]") @@ -1636,9 +1644,8 @@ export class TenantV2 extends DurableObject { const transitionedAt = now(); const scrubbed = safeSession(result); const safeResult = scrubbed ?? "The run completed without a persistable transcript."; - const encryptedResult = await encrypt(safeResult, this.env.CREDENTIAL_ENCRYPTION_KEY); - const encryptedSession = scrubbed && outputCaptured ? await encrypt(scrubbed, this.env.CREDENTIAL_ENCRYPTION_KEY) : null; - this.ctx.storage.sql.exec("UPDATE runs SET state = ?, result = ?, session = COALESCE(?, session), output_captured=?, updated_at = ? WHERE id = ?", state, encryptedResult, encryptedSession, encryptedSession ? 1 : Number(run.output_captured || 0), transitionedAt, run.id); + const session = scrubbed && outputCaptured ? scrubbed : null; + this.ctx.storage.sql.exec("UPDATE runs SET state = ?, result = ?, session = COALESCE(?, session), output_captured=?, updated_at = ? WHERE id = ?", state, safeResult, session, session ? 1 : Number(run.output_captured || 0), transitionedAt, run.id); this.ctx.storage.sql.exec("UPDATE job_runs SET state=?,updated_at=? WHERE id=?", state === "done" ? "succeeded" : "failed", transitionedAt, run.id); this.ctx.storage.sql.exec("DELETE FROM active_claims WHERE pipe_id = ? AND issue_id = ?", run.pipe_id, run.claim_key || run.issue_id); this.ctx.storage.sql.exec("UPDATE runs SET claim_released=1 WHERE id=?", run.id); @@ -1667,7 +1674,7 @@ export class TenantV2 extends DurableObject { this.ctx.storage.sql.exec("UPDATE runs SET vm_cleanup_complete=1,vm_cleanup_attempt=?,vm_cleanup_next_at=NULL,updated_at=? WHERE id=?", attempt, now(), run.id); this.activity(run.id, "vm_deleted", `Deleted the run-owned VM on attempt ${attempt}.`); if (String(run.state) === "stopping") { - this.ctx.storage.sql.exec("UPDATE runs SET state='stopped',result=?,updated_at=? WHERE id=?", await encrypt("Stopped by user", this.env.CREDENTIAL_ENCRYPTION_KEY), now(), run.id); + this.ctx.storage.sql.exec("UPDATE runs SET state='stopped',result=?,updated_at=? WHERE id=?", "Stopped by user", now(), run.id); this.ctx.storage.sql.exec("UPDATE job_runs SET state='stopped',updated_at=? WHERE id=?", now(), run.id); this.ctx.storage.sql.exec("DELETE FROM active_claims WHERE run_id=?", run.id); await this.afterJobTerminal(String(run.pipe_id), String(run.id), "stopped"); diff --git a/packages/app/test/exe-run-lifecycle.e2e.test.ts b/packages/app/test/exe-run-lifecycle.e2e.test.ts index da2e9c63..1afa19e0 100644 --- a/packages/app/test/exe-run-lifecycle.e2e.test.ts +++ b/packages/app/test/exe-run-lifecycle.e2e.test.ts @@ -10,6 +10,7 @@ const origin = "https://app.factorize.sh"; describe("exe.dev run lifecycle", () => { const vms = new Map(); let scenario: "success" | "launch-failure" = "success"; + let agentOutput = "Hello from the stubbed agent!"; beforeAll(() => { fetchMock.activate(); @@ -35,13 +36,15 @@ describe("exe.dev run lifecycle", () => { const name = command.match(/^ssh '([^']+)'/)?.[1] ?? "", state = vms.get(name)?.state ?? "running"; return { statusCode: 200, data: state, responseOptions: { headers } }; } - if (command.startsWith("ssh ") && command.includes("cat /tmp/factorize.log")) return { statusCode: 200, data: "Hello from the stubbed agent!", responseOptions: { headers } }; + if (command.startsWith("ssh ") && command.includes("cat /tmp/factorize.log")) return { statusCode: 200, data: agentOutput, responseOptions: { headers } }; return { statusCode: 422, data: `unexpected command: ${command}` }; }).persist(); }); - it("runs through the Worker, Durable Object, stubbed exe backend, diagnostics, and cleanup", async () => { + it("stores a completed large transcript as scrubbed plaintext and reaches cleanup end to end", async () => { scenario = "success"; + const secret = "sk-this-must-never-be-stored"; + agentOutput = `Hello from the stubbed agent!\nAuthorization: Bearer ${secret}\n${"large transcript line ๐Ÿ\n".repeat(12_000)}`; const tenantId = `exe-e2e-${crypto.randomUUID()}`, userId = "owner"; const tenant = env.TENANTS.get(env.TENANTS.idFromName(`tenant:${tenantId}`)); await tenant.fetch("https://tenant/members", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ userId, email: "owner@example.com" }) }); @@ -66,7 +69,21 @@ describe("exe.dev run lifecycle", () => { const inspected = await SELF.fetch(`${origin}/api/v1/runs/${run.runId}`, { headers }); expect(inspected.status, await inspected.clone().text()).toBe(200); - await expect(inspected.json()).resolves.toMatchObject({ id: run.runId, state: "succeeded", result: expect.stringContaining("Hello from the stubbed agent!") }); + const body = await inspected.json<{ id: string; state: string; result: string; session: string }>(); + expect(body).toMatchObject({ id: run.runId, state: "succeeded" }); + expect(body.result).toContain("Hello from the stubbed agent!"); + expect(body.result).toContain("[REDACTED]"); + expect(body.result).not.toContain(secret); + expect(body.result.length).toBeGreaterThan(250_000); + expect(body.session).toBe(body.result); + const stored = await runInDurableObject(tenant, (_instance, state) => { + const [row] = [...state.storage.sql.exec<{ result: string; session: string }>("SELECT result,session FROM runs WHERE id=?", run.runId)]; + return row; + }); + expect(stored.result).toBe(body.result); + expect(stored.session).toBe(body.session); + expect(stored.result).toContain("[REDACTED]"); + expect(stored.result).not.toContain(secret); const diagnostics = await SELF.fetch(`${origin}/api/v1/runs/${run.runId}/diagnostics`, { headers }); expect(diagnostics.status, await diagnostics.clone().text()).toBe(200); await expect(diagnostics.json()).resolves.toMatchObject({ runId: run.runId, state: "succeeded", checks: { launchAcknowledged: true, promptAccepted: true, outputCaptured: true, claimReleased: true, cleanupComplete: true } }); @@ -75,6 +92,7 @@ describe("exe.dev run lifecycle", () => { it("reports an HTTP-200 shell launch failure without calling an LLM", async () => { scenario = "launch-failure"; + agentOutput = "Hello from the stubbed agent!"; const tenantId = `exe-failure-${crypto.randomUUID()}`, userId = "owner"; const tenant = env.TENANTS.get(env.TENANTS.idFromName(`tenant:${tenantId}`)); await tenant.fetch("https://tenant/members", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ userId, email: "owner@example.com" }) }); @@ -94,6 +112,7 @@ describe("exe.dev run lifecycle", () => { it("repairs lost alarms, recovers stale starts, and kills queued runs without holding capacity", async () => { scenario = "success"; + agentOutput = "Hello from the stubbed agent!"; const tenantId = `durability-${crypto.randomUUID()}`, userId = "owner"; const tenant = env.TENANTS.get(env.TENANTS.idFromName(`tenant:${tenantId}`)); await tenant.fetch("https://tenant/members", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ userId, email: "owner@example.com" }) });