Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/sdk/src/__tests__/file-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {}): WorkflowRunRow {
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions packages/sdk/src/workflows/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions packages/sdk/src/workflows/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -328,7 +328,10 @@ async function main(): Promise<void> {

// 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(),
Comment on lines +332 to +333
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replay primary run DB before enabling home fallback

Turning on homeFallback at construction time causes relayfile/cloud sessions with a readable-but-read-only workspace DB to switch to $HOME immediately, and JsonFileWorkflowDb only replays the resolved path (loadSnapshot(this.filePath)). In that case, existing runs already stored in .agent-relay/workflow-runs.jsonl are no longer visible, so --resume <runId> can fail with "not found" even though the run state is still present in the original file.

Useful? React with 👍 / 👎.

});
if (!fileDb.isWritable()) {
console.warn(
`[workflow] warning: cannot write to ${dbPath} — run state will not be persisted (--resume unavailable)`
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk/src/workflows/file-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading