Pull request #76, draft against develop, merges after run-as-job (LATB-100)

A run worker for backtests that starts on demand and stops when idle

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

52 files
changed, in 3 commits
1,507
lines added, 65 removed
449
of those are tests (339), the OpenAPI snapshot (104), the lockfile and generated rule copies
1,058
lines of code, config and docs to read

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.

Merge order. After run-as-job, two lines are wired on the rebase: queuing a run also asks for the worker, and the run status panel says "starting the run worker" while it boots. Neither is in this diff.

What changed, by layer

LayerWhat is newFilesAdded
apps/serverA 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.20773
apps/robodev-beA 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.8265
apps/feOpening a strategy asks for the worker, once per strategy shown. 32 lines are an API test.353
infraThe 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.7232
Deploy config and workflowA run-worker deploy entry per stage, the scaling settings on the job worker, and a deploy job that runs after the job worker's.384
Docs and rulesADR-0015, the deploy guide's run worker section with cost and apply order, vocabulary, layer rule wording.1097
bun.lock@aws-sdk/client-ecs 3.1178.0 for apps/server.13

How it fits, now and later

new in this PR changed in this PR untouched not built
Web app
Workspace pageasks for the worker when a strategy opens
Run polling, listsask for nothing
Run status panel"starting the run worker" while a queued run waits
↓ generated client, conversation socket
API service
/api/run-workerask for the worker, read whether it is starting
Conversation socketa turn asks for the worker
POST /api/runsrun-as-job: queues runs/execute and asks for the worker
↓ rows in the job queue
Postgres job queue: runs/warm (new) · runs/execute (run-as-job) · every other job
↓ each container claims the jobs it is routed
ECS services
Job workeralways on, 2 vCPU / 4 GB. No longer claims run jobs; holds the run worker at 1 or 0 tasks
Run worker2 vCPU / 8 GB, 0 tasks at rest. Claims only runs/warm and runs/execute, one run at a time
↓ the engine and the record, unchanged
Packages
spec, engine, market-data, record, agentno file changed

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.

Decisions made in this PR

ADR-0015 records the first three with their alternatives.

DecisionWhyWhat it costs
One long-lived worker that stops when idle, not one task per runA 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 callsIt 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 PostgresThe 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 sThe 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 driftNo 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 servicesSame network layout as the services already there.$0.005 an hour while it is up.

The evidence

ClaimChecked againstResult
The repo's checks passbun check30 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 narrowThe query read from @robodev-ai/runtime 0.41.0 in the testPart of 33 passing server tests
The scaler wants 1 while a job is queued, running or recent, and 0 after the idle periodA fake queue and a fake ECS serviceSame 33 tests
/api/run-worker and the run_worker store behave end to endA throwaway Robodev60 and 41 API tests pass
The service is created with 0 tasks, 8192 MiB and no load balancerterragrunt plan -lock=false, staging16 to add
The job worker gains only the scaling permissionterragrunt plan -lock=false, staging api/worker-ecs1 to change
Production plans the same serviceterragrunt plan -lock=false, production16 to add; its job worker unit from #73 is also still unapplied (16 to add)
No layer rule is brokenarch-verifier, plus comment-auditor and shape-reviewerNo 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.

Changes outside the feature

Rollout, per stage

Nothing has been applied or deployed. The order matters.

  1. Apply api/run-worker-ecsCreates the service at 0 tasks, its roles and the deploy role.
  2. Apply 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.
  3. DeployThe first deploy that carries the 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 stageHours up a monthCost a month
1 h session per workday27.5$3.30
4 h per workday in two sessions99$12
8 h per workday181.5$22
Always on730$89

On demand in us-east-1: $0.1165 an hour for 2 vCPU / 8 GB plus $0.005 for the public IPv4.

Worth reviewing by hand

Ordered by how much a mistake would cost. Tick them off as you go; the ticks stay in this browser.

What comes after

  1. Rebase onto run-as-job and wire the two lines: queuing a run asks for the worker, and the run status panel shows the starting state.
  2. Apply on staging in the order above, deploy, and measure the cold start from an empty service to the first claimed job.
  3. Run a real year of options on staging and read the task's peak memory; raise run_worker_ecs_memory if 8 GB is short.
  4. With those two numbers, revisit the 15-minute idle period and whether two runs at once fit.