Skip to content

Durable coding-agent harnesses (Claude Code, Codex, OpenCode, Pi, fx) on a Workflow SDK World

4.0 KB
Raw Blame
// Bundle the Cloudflare Durable Objects World from vercel/workflow#4682 into
// vendor/world-cloudflare, which this app depends on as a `file:` package.
// The World is not published (the PR is a draft and the package is private),
// so the lane carries the PR's build, pinned to the commit it was built from.
//
//   node scripts/vendor-world-cloudflare.mjs [path to a vercel/workflow checkout of the PR]
//
// The checkout must have run `pnpm install` and built the World's
// dependencies (`pnpm turbo build --filter='@workflow/world-cloudflare^...'`).
// Default path: $WORKFLOW_REPO, else ../../../../vercel/workflow--pr-4682
// (a worktree next to the main checkout). See the app's README.
import { execFileSync } from 'node:child_process';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const here = dirname(fileURLToPath(import.meta.url));
const app = resolve(here, '..');
const repo = resolve(
  process.argv[2] ?? process.env.WORKFLOW_REPO ?? join(app, '../../../../vercel/workflow--pr-4682')
);
const pkg = join(repo, 'packages/world-cloudflare');
const out = join(app, 'vendor/world-cloudflare');

const git = (...args) => execFileSync('git', ['-C', repo, ...args], { encoding: 'utf8' }).trim();
const commit = git('rev-parse', 'HEAD');
const dirty = git('status', '--porcelain', '--', 'packages/world-cloudflare', 'packages/world-sim', 'packages/world') !== '';

// esbuild from this app's devDependencies (wrangler's own copy would do too).
const { build } = createRequire(import.meta.url)('esbuild');
const result = await build({
  entryPoints: [join(pkg, 'src/index.ts')],
  absWorkingDir: pkg,
  bundle: true,
  format: 'esm',
  platform: 'neutral',
  target: 'es2022',
  // workerd provides cloudflare:workers and node:*. @workflow/world, errors
  // and utils are unchanged in the PR since their 5.0.x release, so they (and
  // ulid) come from npm as dependencies of the vendored package; the PR's
  // own code and world-sim's store (private, changed by the PR) are inlined.
  external: ['cloudflare:workers', 'node:*', '@workflow/world', '@workflow/errors', '@workflow/errors/*', '@workflow/utils', 'ulid'],
  conditions: ['workerd', 'worker', 'import', 'module', 'default'],
  mainFields: ['module', 'main'],
  write: false,
  legalComments: 'none',
  metafile: true,
});
const [file] = result.outputFiles;
// lib/, not dist/: the repo's .gitignore drops dist/, and this build is committed.
await mkdir(join(out, 'lib'), { recursive: true });
const banner = `// Generated by apps/cloudflare/scripts/vendor-world-cloudflare.mjs from\n// vercel/workflow#4682 (packages/world-cloudflare) at ${commit}${dirty ? ' (with local changes)' : ''}.\n// Do not edit: refresh it with \`pnpm --filter harness-cloudflare vendor:world\`.\n`;
await writeFile(join(out, 'lib/index.js'), banner + file.text);
await writeFile(
  join(out, 'SOURCE.json'),
  `${JSON.stringify(
    {
      repository: 'vercel/workflow',
      pullRequest: 4682,
      package: 'packages/world-cloudflare',
      commit,
      dirty,
      inputs: Object.keys(result.metafile.inputs)
        .filter((p) => !p.includes('node_modules'))
        .map((p) => p.replace(/^(\.\.\/)+/, 'packages/'))
        .sort(),
    },
    null,
    2
  )}\n`
);
// The release the externals must match: fail if the PR moved past them.
const pinned = { '@workflow/world': '5.0.2', '@workflow/errors': '5.0.2', '@workflow/utils': '5.0.1' };
for (const [name, want] of Object.entries(pinned)) {
  const got = JSON.parse(await readFile(join(repo, 'packages', name.split('/')[1], 'package.json'), 'utf8')).version;
  if (got !== want) throw new Error(`${name} is ${got} in the checkout, the vendored package depends on ${want}: update vendor/world-cloudflare/package.json`);
}
const version = JSON.parse(await readFile(join(pkg, 'package.json'), 'utf8')).version;
console.log(`vendored @workflow/world-cloudflare ${version} from ${commit.slice(0, 9)}${dirty ? ' (dirty)' : ''}: ${(file.text.length / 1024).toFixed(0)} KB`);