Where the 1,058 lines worth reading are, which three places a mistake would cost most, and what has not been tried on real AWS. github.com/povio/lrbo3/pull/76
Before: each stage runs an API service and one always-on job worker (2 vCPU / 4 GB) that claims every queued job. After: a third service, the run worker (2 vCPU / 8 GB), claims only backtest runs, one at a time, and has no task while nobody needs it. The job worker starts it when a trader shows up and stops it after 15 idle minutes.
Not in it: the run job itself. On develop a run still executes inside the request; the runs/execute job arrives with the run-as-job pull request. Until then this worker would only ever answer warm-ups, which is why this is a draft.
| Layer | What is new | Files | Added |
|---|---|---|---|
apps/server | A container can be told which jobs it claims and how many of one job it runs at once. A container can hold another ECS service at 1 or 0 tasks from the state of the queue. 255 of these lines are tests. | 20 | 773 |
apps/robodev-be | A warm-up job, a one-row table recording when the worker was last asked for and last answered, and GET/POST /api/run-worker. A conversation turn asks for the worker. 104 lines are the OpenAPI snapshot, 52 are tests. | 8 | 265 |
apps/fe | Opening a strategy asks for the worker, once per strategy shown. 32 lines are an API test. | 3 | 53 |
infra | The run worker service on both stages (no load balancer, 0 tasks), three stage variables for its size, and permission for the job worker to scale that one service. | 7 | 232 |
| Deploy config and workflow | A run-worker deploy entry per stage, the scaling settings on the job worker, and a deploy job that runs after the job worker's. | 3 | 84 |
| Docs and rules | ADR-0015, the deploy guide's run worker section with cost and apply order, vocabulary, layer rule wording. | 10 | 97 |
bun.lock | @aws-sdk/client-ecs 3.1178.0 for apps/server. | 1 | 3 |
Everything is driven through the queue: asking for the worker is queuing a job only it may claim, and the job worker reads the same queue to decide whether the service should have a task. No component calls another directly, and nothing in packages/* knows any of this exists.
ADR-0015 records the first three with their alternatives.
| Decision | Why | What it costs |
|---|---|---|
| One long-lived worker that stops when idle, not one task per run | A second run in the same session reuses warm data caches; a task per run pays the cold start and the data load every time. | The first run of a session waits for a cold start that has not been measured, and each session pays a 15-minute idle tail (about $0.03). |
| The job worker scales the run worker itself, every 3 s, with ECS calls | It already runs all day and sees the queue. Application Auto Scaling on a queue metric reacts in minutes, not seconds, and needs a metric published from somewhere anyway. | Scaling logic lives in our runner and has only been tested against a fake service. If the job worker is down, the run worker neither starts nor stops. |
| Route jobs by narrowing the runtime's claim query on its way to Postgres | The Robodev runtime's queue has no setting for "only these jobs" or "at most N of this job". | It depends on the text of a query we do not own. A test pins the real query so an upgrade that changes it fails CI, and at runtime a claim that cannot be narrowed is refused, so jobs stall rather than run on the wrong container. |
| Warm-up is a no-op job, throttled to one per API process per 30 s | The scaler then needs one rule only: a run-worker job is queued, running or recent. The job also proves the worker is really claiming. | An active trader writes a queue row every 30 s per API task, and "starting" is inferred from a request left unanswered for 3 s. |
| One run at a time; memory is one stage variable (8192) | 3.0 to 3.7 GB a run was measured on generated data only. Two runs side by side could exceed 8 GB. | A second trader's run queues behind the first. |
| Terraform holds desired count at 0 and does not ignore drift | No module change was needed; the service module stays as it is. | Applying the run worker unit while the worker is up stops it. The job worker starts it again within a minute if still wanted. |
| Public IPv4 on the task, like the other two services | Same network layout as the services already there. | $0.005 an hour while it is up. |
| Claim | Checked against | Result |
|---|---|---|
| The repo's checks pass | bun check | 30 of 31 tasks. rules:check fails only on a checkout holding untracked files under .claude/rules/; with them aside it reports up to date |
| Routing narrows the runtime's real claim query and refuses one it cannot narrow | The query read from @robodev-ai/runtime 0.41.0 in the test | Part of 33 passing server tests |
| The scaler wants 1 while a job is queued, running or recent, and 0 after the idle period | A fake queue and a fake ECS service | Same 33 tests |
/api/run-worker and the run_worker store behave end to end | A throwaway Robodev | 60 and 41 API tests pass |
| The service is created with 0 tasks, 8192 MiB and no load balancer | terragrunt plan -lock=false, staging | 16 to add |
| The job worker gains only the scaling permission | terragrunt plan -lock=false, staging api/worker-ecs | 1 to change |
| Production plans the same service | terragrunt plan -lock=false, production | 16 to add; its job worker unit from #73 is also still unapplied (16 to add) |
| No layer rule is broken | arch-verifier, plus comment-auditor and shape-reviewer | No violation; findings applied |
Not evidenced: nothing here has run on AWS. The scaler has never called a real ECS service. The cold start (image pull, boot, first claim) is unmeasured. Whether 8 GB fits a real year of options is unknown: a real ES day lists about 2.7 times more contracts than the generated data the 3.0 to 3.7 GB came from. Whether the deploy tool's --watch returns cleanly on a service with 0 tasks is untried. apps/server is outside the repo's lint scope, so its new files are type-checked and tested but not linted. The plans ran before the last comment-only edits to the stage files.
runs/execute and runs/warm. If the run worker service is missing or cannot start, those jobs stay queued instead of running on the job worker.bun dev, docker compose and hosted Robodev are unchanged: no routing setting is set there, and the warm-up job runs like any other.Nothing has been applied or deployed. The order matters.
api/run-worker-ecsCreates the service at 0 tasks, its roles and the deploy role.api/worker-ecsThe job worker's task role gains the scaling permission. On production this unit is new from #73 and creates the job worker too.run-worker entry. No new secret: it reads the same database URL and JWT secret.Deployed before step 1, the new deploy job has no role or service to use, and the job worker logs worker scaling failed every 3 s while run jobs stay queued. To undo: remove the ON_DEMAND_WORKER_* settings from the job worker and redeploy; it claims run jobs again and the run worker stays at whatever count it had, so set it to 0 by hand.
| Usage, per stage | Hours up a month | Cost a month |
|---|---|---|
| 1 h session per workday | 27.5 | $3.30 |
| 4 h per workday in two sessions | 99 | $12 |
| 8 h per workday | 181.5 | $22 |
| Always on | 730 | $89 |
On demand in us-east-1: $0.1165 an hour for 2 vCPU / 8 GB plus $0.005 for the public IPv4.
Ordered by how much a mistake would cost. Tick them off as you go; the ticks stay in this browser.
run_worker_ecs_memory if 8 GB is short.