diff --git a/packages/sdk/src/cloud-run.ts b/packages/sdk/src/cloud-run.ts index 94f628eb9..7dafcaabf 100644 --- a/packages/sdk/src/cloud-run.ts +++ b/packages/sdk/src/cloud-run.ts @@ -14,7 +14,7 @@ import { type CloudConnectionOptions, } from './cloud-http.js'; import { cloudRunState, isCloudRunActive, type CloudRunState } from './cloud-run-record.js'; -import { canUseCodeCache, packWorkingTree, prepareCloudSync, sha256PackedTree, uploadCloudCode } from './cloud-sync.js'; +import { canUseCodeCache, packWorkingTree, prepareCloudSync, sha256PackedTree, uploadCloudCode, type PackedTree } from './cloud-sync.js'; export type CloudFlowSource = FlowSpec | { path: string }; export const MIN_CLOUD_RUN_BUDGET_MINUTES = 30; @@ -42,10 +42,9 @@ export interface RunInCloudOptions extends CloudConnectionOptions { */ syncCode?: { root: string }; /** - * Called immediately before the one non-idempotent request, the run - * submission. Everything before it (prepare, pack, upload) is safe to - * retry, so a caller classifying an interruption can tell "nothing was - * admitted" from "admission unknown". + * Called immediately before the first non-idempotent run submission. + * A cache-expiry refusal may trigger one fresh run submission afterward; + * the callback is already set for that admission phase. */ onSubmit?: () => void; /** @@ -301,6 +300,7 @@ export async function runInCloud( // refused backend or failed upload therefore never leaves a launched run // pointing at a tree that is not there. let synced: { runId: string; codeKey: string; codeCacheHit: boolean; files: number; bytes: number; skippedLinks: string[] } | undefined; + let packedForRecovery: PackedTree | undefined; if (options.syncCode !== undefined) { options.signal?.throwIfAborted(); const packed = await packWorkingTree(options.syncCode.root); @@ -310,31 +310,22 @@ export async function runInCloud( const digest = canUseCodeCache(packed) ? await sha256PackedTree(packed) : undefined; prepared = await prepareCloudSync(options, digest); options.signal?.throwIfAborted(); - if (!prepared.codeCacheHit) await uploadCloudCode(prepared, packed, options); - } finally { + if (prepared.codeCacheHit) { + packedForRecovery = packed; + } else { + await uploadCloudCode(prepared, packed, options); + packed.dispose(); + } + } catch (error) { packed.dispose(); + throw error; } synced = { runId: prepared.runId, codeKey: prepared.codeKey, codeCacheHit: prepared.codeCacheHit === true, files: packed.files.length, bytes: packed.bytes, skippedLinks: packed.skippedLinks }; } - options.signal?.throwIfAborted(); - options.onSubmit?.(); let result: unknown; - try { - // A cache hit is copied into the run prefix after admission. The R2 copy - // may outlast the normal 30s request budget; explicit caller limits win. - const submitOptions = synced?.codeCacheHit - ? { ...options, requestTimeoutMs: options.requestTimeoutMs ?? 300_000 } - : options; - result = await cloudFetch('/api/v1/workflows/run', submitOptions, { method: 'POST', detail: true, body: JSON.stringify({ - ...cloudSubmissionBody(submission), - ...(options.workspaceId === undefined ? {} : { workspaceId: options.workspaceId }), - ...(options.runBudgetMinutes === undefined ? {} : { relayflowV2RunBudgetMinutes: options.runBudgetMinutes }), - ...(synced === undefined ? {} : { runId: synced.runId, s3CodeKey: synced.codeKey }), - ...(envSecrets === undefined ? {} : { envSecrets }), - }) }); - } catch (caught) { + const rewriteRunError = (caught: unknown): unknown => { // Withhold first: every error thrown from here, including the rewrite // below, carries only a refusal that quotes no envSecrets value. const error = withheldIfQuotingSecret(caught, envSecrets); @@ -344,13 +335,56 @@ export async function runInCloud( if (error instanceof CloudFlowError && submission.authoredAuthority !== undefined && error.refusal?.code === 'relayflow_v2_authored_authority_invalid') { const expected = error.refusal.expected?.version; - throw new CloudFlowError('unsupported_source', + return new CloudFlowError('unsupported_source', `Cloud refused this authored flow's Surface: it runs @relayflows/surface ${expected ?? '(version not reported)'} ` + `and this CLI authored against ${submission.authoredAuthority.surface.version}. ` + 'Install the matching relayflows release, or deploy the flow with `flows deploy`, which uses Cloud\'s own Surface.', error.status, error.refusal); } - throw error; + return error; + }; + try { + options.signal?.throwIfAborted(); + options.onSubmit?.(); + const submit = () => { + options.signal?.throwIfAborted(); + // A cache hit is copied into the run prefix after admission. The R2 copy + // may outlast the normal 30s request budget; explicit caller limits win. + const submitOptions = synced?.codeCacheHit + ? { ...options, requestTimeoutMs: options.requestTimeoutMs ?? 300_000 } + : options; + return cloudFetch('/api/v1/workflows/run', submitOptions, { method: 'POST', detail: true, body: JSON.stringify({ + ...cloudSubmissionBody(submission), + ...(options.workspaceId === undefined ? {} : { workspaceId: options.workspaceId }), + ...(options.runBudgetMinutes === undefined ? {} : { relayflowV2RunBudgetMinutes: options.runBudgetMinutes }), + ...(synced === undefined ? {} : { runId: synced.runId, s3CodeKey: synced.codeKey }), + ...(envSecrets === undefined ? {} : { envSecrets }), + }) }); + }; + try { + result = await submit(); + } catch (caught) { + const error = rewriteRunError(caught); + if (!(error instanceof CloudFlowError && error.status === 503 + && error.refusal?.code === 'workflow_code_unavailable' + && synced?.codeCacheHit && packedForRecovery)) throw error; + // The original run was terminalized by Cloud. A new prepare gives this + // archive a new run prefix; omitting the digest forces a real upload. + try { + const prepared = await prepareCloudSync(options); + options.signal?.throwIfAborted(); + if (prepared.runId === synced.runId) { + throw new CloudFlowError('invalid_response', 'Cloud prepare reused a terminalized run ID.'); + } + await uploadCloudCode(prepared, packedForRecovery, options); + synced = { ...synced, runId: prepared.runId, codeKey: prepared.codeKey, codeCacheHit: false }; + result = await submit(); // exactly one recovery attempt + } catch (recoveryError) { + throw rewriteRunError(recoveryError); + } + } + } finally { + packedForRecovery?.dispose(); } if (!isCloudRecord(result) || (result.status !== 'pending' && result.status !== 'running')) { throw new CloudFlowError('invalid_response', 'Cloud did not return an accepted run.'); diff --git a/packages/sdk/tests/cloud-sync.test.ts b/packages/sdk/tests/cloud-sync.test.ts index 13d4cdc50..54d4f5c7b 100644 --- a/packages/sdk/tests/cloud-sync.test.ts +++ b/packages/sdk/tests/cloud-sync.test.ts @@ -1,4 +1,5 @@ import { execFileSync } from 'node:child_process'; +import { writeFileSync } from 'node:fs'; import { chmod, mkdir, mkdtemp, readFile, readdir, rm, stat, symlink, truncate, utimes, writeFile } from 'node:fs/promises'; import { execFile } from 'node:child_process'; import { promisify } from 'node:util'; @@ -54,7 +55,9 @@ function cloud(routes: Record unknown>) { calls.push(call); const route = Object.keys(routes).find(prefix => path.startsWith(prefix)); if (!route) return new Response('{"error":"not found"}', { status: 404 }); - return new Response(JSON.stringify(routes[route]!(call)), { status: 200, headers: { 'content-type': 'application/json' } }); + const answer = routes[route]!(call); + return answer instanceof Response ? answer + : new Response(JSON.stringify(answer), { status: 200, headers: { 'content-type': 'application/json' } }); }); vi.stubEnv('FLOWS_CLOUD_URL', 'https://cloud-contract.example'); vi.stubEnv('FLOWS_CLOUD_TOKEN', 'test-scoped-cloud-token'); @@ -275,6 +278,173 @@ describe('prepareCloudSync', () => { }); describe('runInCloud with syncCode', () => { + it('prepares and uploads once when a cache hit disappears before submission', async () => { + const root = await tempDir('cloud-sync-cache-disappeared-'); + const spoolRoot = await tempDir('cloud-sync-recovery-spool-'); + await writeFile(join(root, 'flow.yaml'), JSON.stringify({ + version: '0.1.0', name: 'synced', steps: [{ id: 'ls', type: 'deterministic', command: 'ls' }], + })); + const dataPath = join(root, 'data.txt'); + await writeFile(dataPath, 'packed-before-submit'); + vi.stubEnv('TMPDIR', spoolRoot); + let prepares = 0; + let submits = 0; + const onSubmit = vi.fn(); + const { calls, options } = cloud({ + '/api/v1/workflows/prepare': ({ body }) => { + prepares++; + if (prepares === 1) { + const digest = (body as { codeSha256: string }).codeSha256; + return { ...PREPARED, runId: 'stale-run', s3CodeKey: `code-sha256-${digest}.tar.gz`, + codeCache: { sha256: digest, hit: true } }; + } + expect(body).toBeUndefined(); // force a fresh upload, even if another cache hit is reported + return { ...PREPARED, runId: 'recovered-run' }; + }, + '/api/v1/workflows/runs/recovered-run/storage/': () => ({ ok: true }), + '/api/v1/workflows/run': () => { + submits++; + if (submits === 1) { + writeFileSync(dataPath, 'changed-after-submit'); + return new Response(JSON.stringify({ error: 'workflow_code_unavailable' }), { + status: 503, headers: { 'content-type': 'application/json' }, + }); + } + return { runId: 'recovered-run', status: 'pending' }; + }, + }); + + const receipt = await runInCloud({ path: join(root, 'flow.yaml') }, + { ...options, syncCode: { root }, onSubmit }); + + expect(receipt.runId).toBe('recovered-run'); + expect(onSubmit).toHaveBeenCalledOnce(); + expect(calls.map(call => [call.method, call.path])).toEqual([ + ['POST', '/api/v1/workflows/prepare'], + ['POST', '/api/v1/workflows/run'], + ['POST', '/api/v1/workflows/prepare'], + ['PUT', '/api/v1/workflows/runs/recovered-run/storage/code.tar.gz'], + ['POST', '/api/v1/workflows/run'], + ]); + expect(calls.at(-1)!.body).toMatchObject({ runId: 'recovered-run', s3CodeKey: 'code.tar.gz' }); + const recoveredArchive = calls.find(call => call.method === 'PUT')!.body as Buffer; + expect(gunzipSync(recoveredArchive).toString('latin1')).toContain('packed-before-submit'); + expect(gunzipSync(recoveredArchive).toString('latin1')).not.toContain('changed-after-submit'); + expect(await readdir(spoolRoot)).toEqual([]); + }); + + it('makes only one recovery attempt and leaves unrelated run errors alone', async () => { + const root = await tempDir('cloud-sync-cache-recovery-limit-'); + const spoolRoot = await tempDir('cloud-sync-recovery-failure-spool-'); + vi.stubEnv('TMPDIR', spoolRoot); + await writeFile(join(root, 'flow.yaml'), JSON.stringify({ + version: '0.1.0', name: 'synced', steps: [{ id: 'ls', type: 'deterministic', command: 'ls' }], + })); + let prepares = 0; + const missing = () => new Response(JSON.stringify({ error: 'workflow_code_unavailable' }), { + status: 503, headers: { 'content-type': 'application/json' }, + }); + const { calls, options } = cloud({ + '/api/v1/workflows/prepare': ({ body }) => { + prepares++; + if (prepares === 1) { + const digest = (body as { codeSha256: string }).codeSha256; + return { ...PREPARED, runId: 'stale-run', s3CodeKey: `code-sha256-${digest}.tar.gz`, + codeCache: { sha256: digest, hit: true } }; + } + return { ...PREPARED, runId: 'recovered-run' }; + }, + '/api/v1/workflows/runs/recovered-run/storage/': () => ({ ok: true }), + '/api/v1/workflows/run': missing, + }); + await expect(runInCloud({ path: join(root, 'flow.yaml') }, { ...options, syncCode: { root } })) + .rejects.toMatchObject({ status: 503, refusal: { code: 'workflow_code_unavailable' } }); + expect(prepares).toBe(2); + expect(calls.filter(call => call.path === '/api/v1/workflows/run')).toHaveLength(2); + expect(calls.filter(call => call.method === 'PUT')).toHaveLength(1); + expect(await readdir(spoolRoot)).toEqual([]); + + calls.length = 0; + prepares = 0; + vi.restoreAllMocks(); + const unrelated = cloud({ + '/api/v1/workflows/prepare': ({ body }) => { + prepares++; + const digest = (body as { codeSha256: string }).codeSha256; + return { ...PREPARED, s3CodeKey: `code-sha256-${digest}.tar.gz`, + codeCache: { sha256: digest, hit: true } }; + }, + '/api/v1/workflows/run': () => new Response(JSON.stringify({ error: 'capacity_unavailable' }), { + status: 503, headers: { 'content-type': 'application/json' }, + }), + }); + await expect(runInCloud({ path: join(root, 'flow.yaml') }, { ...unrelated.options, syncCode: { root } })) + .rejects.toMatchObject({ status: 503, refusal: { code: 'capacity_unavailable' } }); + expect(prepares).toBe(1); + expect(unrelated.calls.filter(call => call.path === '/api/v1/workflows/run')).toHaveLength(1); + expect(unrelated.calls.filter(call => call.method === 'PUT')).toHaveLength(0); + expect(await readdir(spoolRoot)).toEqual([]); + }); + + it('does not resubmit a terminalized run ID or retry unrelated submission failures', async () => { + const root = await tempDir('cloud-sync-cache-recovery-safety-'); + await writeFile(join(root, 'flow.yaml'), JSON.stringify({ + version: '0.1.0', name: 'synced', steps: [{ id: 'ls', type: 'deterministic', command: 'ls' }], + })); + let prepares = 0; + const { calls, options } = cloud({ + '/api/v1/workflows/prepare': ({ body }) => { + prepares++; + if (prepares === 1) { + const digest = (body as { codeSha256: string }).codeSha256; + return { ...PREPARED, runId: 'stale-run', s3CodeKey: `code-sha256-${digest}.tar.gz`, + codeCache: { sha256: digest, hit: true } }; + } + return { ...PREPARED, runId: 'stale-run' }; + }, + '/api/v1/workflows/run': () => new Response(JSON.stringify({ error: 'workflow_code_unavailable' }), { + status: 503, headers: { 'content-type': 'application/json' }, + }), + }); + await expect(runInCloud({ path: join(root, 'flow.yaml') }, { ...options, syncCode: { root } })) + .rejects.toMatchObject({ code: 'invalid_response' }); + expect(calls.filter(call => call.path === '/api/v1/workflows/run')).toHaveLength(1); + expect(calls.filter(call => call.method === 'PUT')).toHaveLength(0); + + vi.restoreAllMocks(); + const ambiguous = cloud({ + '/api/v1/workflows/prepare': ({ body }) => { + const digest = (body as { codeSha256: string }).codeSha256; + return { ...PREPARED, s3CodeKey: `code-sha256-${digest}.tar.gz`, + codeCache: { sha256: digest, hit: true } }; + }, + '/api/v1/workflows/run': () => { throw new TypeError('connection lost'); }, + }); + await expect(runInCloud({ path: join(root, 'flow.yaml') }, { ...ambiguous.options, syncCode: { root } })) + .rejects.toMatchObject({ code: 'transport_error' }); + expect(ambiguous.calls.filter(call => call.path === '/api/v1/workflows/prepare')).toHaveLength(1); + expect(ambiguous.calls.filter(call => call.path === '/api/v1/workflows/run')).toHaveLength(1); + expect(ambiguous.calls.filter(call => call.method === 'PUT')).toHaveLength(0); + + vi.restoreAllMocks(); + const cacheMiss = cloud({ + '/api/v1/workflows/prepare': ({ body }) => { + const digest = (body as { codeSha256: string }).codeSha256; + return { ...PREPARED, s3CodeKey: `code-sha256-${digest}.tar.gz`, + codeCache: { sha256: digest, hit: false } }; + }, + '/api/v1/workflows/runs/prepared-run/storage/': () => ({ ok: true }), + '/api/v1/workflows/run': () => new Response(JSON.stringify({ error: 'workflow_code_unavailable' }), { + status: 503, headers: { 'content-type': 'application/json' }, + }), + }); + await expect(runInCloud({ path: join(root, 'flow.yaml') }, { ...cacheMiss.options, syncCode: { root } })) + .rejects.toMatchObject({ status: 503, refusal: { code: 'workflow_code_unavailable' } }); + expect(cacheMiss.calls.filter(call => call.path === '/api/v1/workflows/prepare')).toHaveLength(1); + expect(cacheMiss.calls.filter(call => call.path === '/api/v1/workflows/run')).toHaveLength(1); + expect(cacheMiss.calls.filter(call => call.method === 'PUT')).toHaveLength(1); + }); + it('uploads once for identical trees and uploads again after a one-byte change', async () => { const root = await tempDir('cloud-sync-cache-'); await writeFile(join(root, 'flow.yaml'), JSON.stringify({