All jobs

Add a Temporal Schedule to a TypeScript Workflow

Yes. An agent adds a Temporal Schedule that starts your Workflow on a calendar spec with overlap and catch-up policies, a script to manage it, a test, and a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent clones the repository into its own cloud sandbox and reads how the Workflow is started today. In a project like temporalio/money-transfer-project-template-ts, package.json pins every @temporalio/* package at ~1.14.1, runs the Worker and the client through ts-node (npm run worker, npm run client), tests with Mocha and @temporalio/testing, and lints with ESLint, where @typescript-eslint/no-floating-promises is an error and Node built-ins are banned from src/workflows.ts. .nvmrc says Node 18 and tsconfig.json extends @tsconfig/node18. src/client.ts loads the connection with loadClientConnectConfig from @temporalio/envconfig, falls back to localhost:7233 and the default namespace, and starts moneyTransfer once with client.workflow.start, a fixed workflowId of pay-invoice-801, the money-transfer task queue from src/shared.ts, and a PaymentDetails of 400 from account 85-150 to 43-812 with referenceId 12345. src/workflows.ts proxies withdraw, deposit and refund with a retry policy of up to 500 attempts and two non-retryable error types. Nothing in the repository creates a Schedule.

The agent adds src/schedule.ts, which connects the same way client.ts does and calls client.schedule.create with a scheduleId from the prompt, a spec of calendars entries (hour and minute, with the timeZone the prompt names) rather than a cron string so the intent reads in code, an action of type startWorkflow naming moneyTransfer, the shared task queue and the PaymentDetails arguments, and policies with overlap set to ScheduleOverlapPolicy.SKIP so a slow transfer never runs twice at once and a catchupWindow for runs missed while the Worker was down. ScheduleOverlapPolicy comes from @temporalio/client. The script catches ScheduleAlreadyRunning and updates the existing Schedule instead, so running it twice is safe, and takes pause, unpause, trigger and delete subcommands that go through client.schedule.getHandle. package.json gets a schedule script beside worker and client, and the README gets a section on creating and pausing it.

One thing the agent flags rather than hides: a Schedule passes the same arguments to every run, so the fixed referenceId the client sends today would reach the bank on each transfer. The pull request says so, and depending on the prompt either leaves the arguments as they are or derives the reference from workflowInfo().workflowId inside the Workflow, which Temporal makes unique per scheduled run by appending the fire time.

For the test the agent follows src/mocha/workflows.test.ts, which already starts a TestWorkflowEnvironment.createLocal() server and a Worker with stubbed withdraw and deposit activities. The new src/mocha/schedule.test.ts creates the Schedule paused against that environment, describes it and asserts the spec, action and overlap policy, triggers it with handle.trigger() while the stubbed Worker runs until the resulting Workflow completes, and deletes it in after. The agent runs npm install, npm run lint, npm run build and npm test in the sandbox; the local test server is a binary the testing package downloads, so no Docker or external Temporal service is needed.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add a Temporal Schedule for the moneyTransfer Workflow. Read src/client.ts, src/worker.ts, src/workflows.ts, src/shared.ts and the Mocha tests first. Create src/schedule.ts that connects through the same envconfig loader as the client and creates a Schedule with id money-transfer-nightly that fires at 02:00 America/New_York every day using a calendar spec, starts moneyTransfer on the shared task queue with the same PaymentDetails the client uses, skips overlapping runs and has a one day catch-up window. Make the script safe to run twice and give it pause, unpause, trigger and delete subcommands. Call out in the pull request that the fixed referenceId repeats on every scheduled run and leave the arguments unchanged. Add a schedule script to package.json, a README section, and a Mocha test that creates the Schedule paused in the local test environment, asserts its description and triggers one run against stubbed activities. Run lint, build and the tests, and open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that adds src/schedule.ts and src/mocha/schedule.test.ts and changes package.json and README.md. The description shows the Schedule's calendar spec, overlap policy and catch-up window as the code sets them, explains the referenceId concern in two sentences, and pastes the lint, build and Mocha output from the sandbox, including the new test's description of the paused Schedule.

The run's record keeps every command, including the local test server the testing package started, so a reviewer can see the Schedule round-trip through a real Temporal server rather than a mock of the client.

Before merging, a reviewer confirms the fire time and time zone, decides how the reference id should be made unique per run, and plans where the script runs, since a Schedule lives on the server and is created once rather than on every deploy.

Run it on every Linear issue labelled schedule

In the console, an automation in the Project that has the repository, with a linear trigger on linear.issue.labeled and labels set to schedule, starts one run per issue with the issue as its context after the prompt; the saved prompt is the whole instruction and the trigger fills in no placeholder. The prompt asks the run to add the Schedule the issue describes to src/schedule.ts, with its own id, spec and policies, so every recurring Workflow in the service arrives as code with a test.

From your own tracker, a handler sends one POST /o/{org}/sessions with this body, the repository in resources and a request_id that is a UUID v5 of the issue id, so a repeated delivery returns the run already started. A webhook URL on the API key reports run.completed with the session_url when the pull request is up.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

A Schedule is a few lines, but the lines that matter are the policies: an overlap policy that lets a slow run stack on the next fire, a missing catch-up window that replays a weekend of transfers after a Worker outage, arguments that repeat a reference id the bank treats as already seen. The agent reads how the Workflow and client are written, sets each policy deliberately, writes the concern it cannot decide into the pull request, and proves the Schedule against a real local Temporal server in its sandbox.

The sandbox is its own computer with the Node version from .nvmrc, the testing package's local server runs there without Docker, and the full lint, build and test output stays in the run's record for the reviewer.

Frequently asked questions

Does the run need a Temporal server or Temporal Cloud credentials?

No. The test uses TestWorkflowEnvironment.createLocal() from @temporalio/testing, the same helper the existing Workflow tests use, which starts a local server binary in the sandbox. The schedule.ts script reads the connection from environment configuration at the time you run it, like client.ts does.

Why a calendar spec instead of a cron string?

Both are accepted by client.schedule.create, and a cron string in cronExpressions would work. A calendars entry with hour, minute and timeZone reads as what it means and avoids the question of which time zone a cron string is in. The pull request shows both forms so the reviewer can choose.

What happens if a transfer is still running when the next fire time arrives?

With ScheduleOverlapPolicy.SKIP the server skips that fire and records it on the Schedule's description. The pull request explains the other policies, BUFFER_ONE, BUFFER_ALL, CANCEL_OTHER and ALLOW_ALL, so the choice is deliberate rather than the default.

Can the script be run on every deploy?

Yes. It catches ScheduleAlreadyRunning and updates the existing Schedule's spec and policies instead of failing, so a deploy pipeline can run npm run schedule idempotently. Pausing or deleting is a separate subcommand so a deploy never does either by accident.

More Temporal jobs

The same job on other stacks

More jobs like this: Turn a Linear label into a PR

Keep reading