From 2cddf8ae62593a7c9a771c422726da2ec4bec4a4 Mon Sep 17 00:00:00 2001 From: MCP Register Flag Bot Date: Thu, 23 Apr 2026 14:42:46 +0200 Subject: [PATCH] fix: use workflow db home fallback in cloud --- packages/sdk/src/__tests__/file-db.test.ts | 19 ++++++++++++++++++- packages/sdk/src/workflows/builder.ts | 7 +++++-- packages/sdk/src/workflows/cli.ts | 7 +++++-- packages/sdk/src/workflows/file-db.ts | 10 ++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/sdk/src/__tests__/file-db.test.ts b/packages/sdk/src/__tests__/file-db.test.ts index 7e3de6259..a900ce940 100644 --- a/packages/sdk/src/__tests__/file-db.test.ts +++ b/packages/sdk/src/__tests__/file-db.test.ts @@ -13,7 +13,7 @@ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, wr import os from 'node:os'; import path from 'node:path'; -import { JsonFileWorkflowDb } from '../workflows/file-db.js'; +import { JsonFileWorkflowDb, shouldUseWorkflowDbHomeFallback } from '../workflows/file-db.js'; import type { WorkflowRunRow, WorkflowStepRow } from '../workflows/types.js'; function makeRun(overrides: Partial = {}): WorkflowRunRow { @@ -49,6 +49,23 @@ describe('JsonFileWorkflowDb', () => { tmpDir = mkdtempSync(path.join(os.tmpdir(), 'filedb-test-')); }); + it('enables home fallback for cloud and relayfile workflow environments', () => { + expect(shouldUseWorkflowDbHomeFallback({ DAYTONA_SANDBOX_ID: 'sandbox-id' })).toBe(true); + expect(shouldUseWorkflowDbHomeFallback({ RELAY_CLOUD_PROVISIONING_DONE: '1' })).toBe(true); + expect( + shouldUseWorkflowDbHomeFallback({ + RELAYFILE_TOKEN: 'token', + RELAYFILE_WORKSPACE_ID: 'rw_test123', + }) + ).toBe(true); + expect( + shouldUseWorkflowDbHomeFallback({ + RELAYFILE_TOKEN: 'token', + }) + ).toBe(false); + expect(shouldUseWorkflowDbHomeFallback({})).toBe(false); + }); + afterEach(() => { try { // Restore perms in case a test made the dir read-only. diff --git a/packages/sdk/src/workflows/builder.ts b/packages/sdk/src/workflows/builder.ts index 7dc964ab6..c7bcee448 100644 --- a/packages/sdk/src/workflows/builder.ts +++ b/packages/sdk/src/workflows/builder.ts @@ -21,7 +21,7 @@ import type { WorkflowRunRow, WorkflowStep, } from './types.js'; -import { JsonFileWorkflowDb } from './file-db.js'; +import { JsonFileWorkflowDb, shouldUseWorkflowDbHomeFallback } from './file-db.js'; import { WorkflowRunner, type WorkflowEventListener, type RunnerStepExecutor } from './runner.js'; import { formatDryRunReport } from './dry-run-format.js'; import { createDefaultEventLogger, type LogLevel } from './default-logger.js'; @@ -408,7 +408,10 @@ export class WorkflowBuilder { const config = this.toConfig(); const runnerCwd = options.cwd ?? process.cwd(); const dbPath = path.join(runnerCwd, '.agent-relay', 'workflow-runs.jsonl'); - const db = new JsonFileWorkflowDb(dbPath); + const db = new JsonFileWorkflowDb({ + filePath: dbPath, + homeFallback: shouldUseWorkflowDbHomeFallback(), + }); const runner = new WorkflowRunner({ cwd: options.cwd, diff --git a/packages/sdk/src/workflows/cli.ts b/packages/sdk/src/workflows/cli.ts index 3c2b550b0..8e23244a4 100644 --- a/packages/sdk/src/workflows/cli.ts +++ b/packages/sdk/src/workflows/cli.ts @@ -14,7 +14,7 @@ import chalk from 'chalk'; import type { WorkflowEvent } from './runner.js'; import { WorkflowRunner } from './runner.js'; -import { JsonFileWorkflowDb } from './file-db.js'; +import { JsonFileWorkflowDb, shouldUseWorkflowDbHomeFallback } from './file-db.js'; function printUsage(): void { console.log( @@ -328,7 +328,10 @@ async function main(): Promise { // Use a file-backed DB so runs survive process restarts and --resume works. const dbPath = path.join(process.cwd(), '.agent-relay', 'workflow-runs.jsonl'); - const fileDb = new JsonFileWorkflowDb(dbPath); + const fileDb = new JsonFileWorkflowDb({ + filePath: dbPath, + homeFallback: shouldUseWorkflowDbHomeFallback(), + }); if (!fileDb.isWritable()) { console.warn( `[workflow] warning: cannot write to ${dbPath} — run state will not be persisted (--resume unavailable)` diff --git a/packages/sdk/src/workflows/file-db.ts b/packages/sdk/src/workflows/file-db.ts index 12b43fe11..7b4f21350 100644 --- a/packages/sdk/src/workflows/file-db.ts +++ b/packages/sdk/src/workflows/file-db.ts @@ -40,6 +40,16 @@ export interface JsonFileWorkflowDbOptions { homeFallback?: boolean; } +export function shouldUseWorkflowDbHomeFallback(env: NodeJS.ProcessEnv = process.env): boolean { + if (env.DAYTONA_SANDBOX_ID || env.RELAY_CLOUD_PROVISIONING_DONE === '1') { + return true; + } + + return Boolean( + env.RELAYFILE_TOKEN && (env.RELAYFILE_WORKSPACE_ID || env.RELAYFILE_WORKSPACE || env.RELAY_WORKSPACE_ID) + ); +} + /** * JSONL-backed WorkflowDb for the CLI. *