# Add Timeouts to a Temporal TypeScript Workflow

Yes. An agent adds execution, run and activity schedule-to-close timeouts to your Temporal TypeScript workflow, proves them with time-skipping tests, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-workflow-timeouts/temporal/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the workflow and how it is started before it bounds anything. In a project like temporalio/money-transfer-project-template-ts, `src/workflows.ts` holds one workflow, `moneyTransfer`, which proxies `withdraw`, `deposit` and `refund` with a retry policy of `initialInterval: '1 second'`, `maximumInterval: '1 minute'`, `backoffCoefficient: 2`, `maximumAttempts: 500`, two `nonRetryableErrorTypes`, and a single timeout, `startToCloseTimeout: '1 minute'`. There is no `scheduleToCloseTimeout`, so an activity that keeps failing retryably is retried until the attempt cap, with each attempt allowed its own minute. `src/client.ts` starts the workflow with `client.workflow.start(moneyTransfer, { args: [details], taskQueue, workflowId: 'pay-invoice-801' })` and no `workflowExecutionTimeout` or `workflowRunTimeout`, so the workflow has no upper bound at all. `src/worker.ts` registers the workflows and activities on the `money-transfer` task queue from `src/shared.ts`. `src/activities.ts` calls a `BankingService` in `src/banking-client.ts` whose mock bank holds accounts `85-150` and `43-812` and throws `InvalidAccountError` or `InsufficientFundsError`; it makes no network calls, so nothing in it is slow today, which is why the missing timeouts have never been noticed.

It also reads the tests and tooling it will extend. `src/mocha/workflows.test.ts` creates a `TestWorkflowEnvironment.createLocal()`, runs a `Worker` with `runUntil`, and has four cases: a successful transfer with stubbed activities and three failures for a bad target account, a bad source account and an amount over the balance, each asserting a `WorkflowFailedError`. `src/mocha/activities.test.ts` uses `MockActivityEnvironment`. `package.json` pins the `@temporalio/*` packages at `~1.14.1`, runs tests with `mocha --exit --require ts-node/register`, and lints `src` with `@typescript-eslint/no-floating-promises` as an error; `.nvmrc` says 18 and `tsconfig.json` extends `@tsconfig/node18`.

The change sets each bound where it belongs. In `workflows.ts`, the proxy options gain a `scheduleToCloseTimeout` that caps the whole retry sequence for `withdraw` and `deposit`, so a bank that stays down fails the transfer in a known time instead of after the attempt cap; `refund` gets a longer one, because giving up on a refund is worse than waiting. The values are named constants in `shared.ts` so the client and the tests read the same numbers. In `client.ts`, `start` gains `workflowExecutionTimeout` for the whole transfer including retries of the workflow itself and `workflowRunTimeout` for a single run; the `workflowTaskTimeout` default is left alone and the description says why. A `heartbeatTimeout` is not added, because none of these activities loops or streams; the pull request says what would change if one did.

The proof is a test that waits without waiting. The agent adds cases to `workflows.test.ts` on a `TestWorkflowEnvironment.createTimeSkipping()` environment: a `deposit` stub that never resolves, which must fail the workflow with an `ActivityFailure` whose cause is a `TimeoutFailure` of type `SCHEDULE_TO_CLOSE` once the clock skips past the bound, and then run `refund`; and a workflow started with a short `workflowExecutionTimeout` that must end with a `WorkflowFailedError` carrying a timeout cause. The four existing tests keep passing on the local environment. It runs `npm test`, `npm run lint` and `npm run build` and includes the output, noting that the time-skipping server is downloaded by the SDK on first use.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add timeouts to the Temporal workflow in this repository. Read the workflow, the activities, the client that starts it, the worker and the existing tests first. Add a scheduleToCloseTimeout to the activity proxy options that caps each activity's full retry sequence, with a longer bound for compensating activities, and add workflowExecutionTimeout and workflowRunTimeout where the workflow is started. Keep the values as named constants shared by the client and the tests. Do not add heartbeat timeouts to activities that do not heartbeat, and explain that choice. Add tests on a time-skipping test environment that prove an activity timing out fails the workflow with a TimeoutFailure cause and that the execution timeout ends the workflow, keep the existing tests passing, run the tests, lint and build, and include the output. 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 changes `src/workflows.ts` (the `scheduleToCloseTimeout` on the proxied activities, with a separate value for `refund`), `src/client.ts` (`workflowExecutionTimeout` and `workflowRunTimeout` on `start`), `src/shared.ts` (the named timeout constants) and `src/mocha/workflows.test.ts` (the new time-skipping cases). The retry policy, the activities and the worker are unchanged.

The description quotes the `mocha` summary with the new cases named, the lint and build results, and explains each value: what it bounds, why `refund` is longer, why `workflowTaskTimeout` was left at its default and why no heartbeat was added. The commands and full output are in the transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer checks the numbers against what the real banking service's own timeouts allow and against the retry policy's `maximumInterval`, so the activity bound is longer than one backoff step, and decides whether the execution timeout should be enforced here or by the caller that starts the workflow. A follow-up message to the same session can add a heartbeat to an activity that later gains a loop.

## Run it on every Temporal worker repository you run

A team with several workers starts one run per repository with one `POST /o/{org}/sessions` each, the repository in `resources` and this prompt as `input`. Each run reads its own workflows and finds the proxies and `start` calls without bounds, so a repository with ten workflows gets ten reviewed sets of values in one pull request.

Send a `request_id`, a UUID v5 of the repository's full name, so a retried call returns the run it already started. The key's `webhook_url` reports when each run the key starts finishes, with the run's final answer in `result`.

## Why run it on Rush?

Timeouts are the kind of change that is easy to write and hard to trust: the wrong value fails healthy transfers, and the right value is only visible after reading the retry policy, the activities and whoever starts the workflow. The agent reads all of it and proves the bounds with a time-skipping test environment, in a sandbox of its own, so a test that would take the real bound to run finishes at once with the output kept.

The pull request carries the test names, the passing summary and the reasoning for each number, so the review is a check of values against the downstream service. Several worker repositories can be done at once, each in its own sandbox, each with its own pull request.

## Frequently asked questions

### What is the difference between startToCloseTimeout and scheduleToCloseTimeout?

`startToCloseTimeout` bounds one attempt of an activity from the moment a worker picks it up. `scheduleToCloseTimeout` bounds the whole thing from scheduling to completion, retries included, so it is the one that turns a retry storm into a failure the workflow can handle. The template had only the first.

### Does a workflow execution timeout cancel the running activities?

When the execution timeout passes, the workflow is closed as timed out and activities still in flight are not retried; one already running on a worker finishes or fails on its own timeout. The test the agent adds shows the workflow ending with a timeout cause, which is what the caller of `handle.result()` sees.

### Why no heartbeat timeout?

A heartbeat timeout only helps an activity that calls `heartbeat()` during long work, so the server can notice a dead worker mid-activity. The template's activities make one call and return, so a heartbeat would add a timeout that nothing reports to. The description says what to add if an activity later loops over many records.

### Will the existing tests still pass?

Yes. The four existing cases run on the local test environment as before, and the new cases run on a time-skipping environment so they finish without waiting out the bounds. The agent includes the full `mocha` output with every case named.

## More Temporal jobs

- [Version a Running Temporal Workflow Safely](https://getrush.ai/run/add-workflow-versioning/temporal/)
- [Dockerize a Temporal TypeScript Worker](https://getrush.ai/run/dockerize-app/temporal/)
- [Prevent Duplicate Workflow Starts in a Temporal TypeScript App](https://getrush.ai/run/prevent-duplicate-workflows/temporal/)

## More jobs like this: Hand off a long task

- [Set Up Workload Identity on GKE with Terraform](https://getrush.ai/run/add-workload-identity/gke/)
- [Add zizmor to CI for Your GitHub Actions Workflows](https://getrush.ai/run/add-zizmor-to-ci/github-actions/)
- [Turn an Asana Task into a Pull Request for a Laravel App](https://getrush.ai/run/asana-task-to-pr/laravel/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
