Skip to content

Record each annual job's queue time in its run telemetry - #1144

Merged
anth-volk merged 6 commits into
mainfrom
job-queue-seconds
Nov 27, 2026
Merged

anth-volk merged 6 commits into
mainfrom
job-queue-seconds

Conversation

@anth-volk

@anth-volk anth-volk commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1143

Same-Repository Draft PR Check

  • This PR is a draft.
  • This PR is from a branch in PolicyEngine/policyengine-sim-api, not a personal fork.

Changelog

  • This PR adds a changelog fragment, changelog.d/job-queue-seconds.added.md, whose type is added.

Summary

Each annual job's run_simulation operation record now carries job_queue_seconds: the seconds from the gateway's submission of the job to its start in the worker. No telemetry records queue time today, and phase 2 of #780 computes typical durations per kind of run from these records, queue time included. Cloud Logging keeps records for 30 days, so this records it early.

  • Gateway. submit_simulation adds the submission time to the spawned job's parameters as _job_submitted_at (JOB_SUBMITTED_AT_PARAM in the contract's job_progress.py), the same ISO 8601 value it stores as the job metadata's submitted_at.
  • Worker. _run_simulation removes the key from the parameters when the call starts, so the calculation and segment children never see it. When the operation opens, it sets job_queue_seconds: the stage record's started_at minus the submission time.
    • started_at is the first attempt's start and carries over to later attempts, so every attempt's record carries the same value, and the successful attempt of a preempted job carries it too. The design said "at the first attempt"; recording it only on the first attempt's record would leave a retried job's successful record without it.
    • It is set when the operation opens, so failed and preempted calls carry it.
    • job_queue_seconds in the contract reads only an ISO 8601 string with a time zone. A start before the submission, which only a difference between the gateway's and the worker's clocks can cause, counts as 0. Values are rounded to hundredths, like stage durations.
    • Telemetry failures are suppressed; tests show a failing start-time read or attribute write leaves the result unchanged.
  • Stage reporter. JobProgressReporter gains started_at().
  • Docs. "Annual job queue time" in docs/engineering/skills/observability.md, after "Annual job stage durations", and a paragraph in the gateway README.

Older worker apps

The gateway sends the key to every route, with no capability check:

  • It spawns annual jobs only through the SimulationWorker class, which apps deployed before 2026-11-25 (23aea894) don't define; on those apps the spawn already fails with NotFoundError. Production routing on 2026-11-28 sends latest to .py 6.20.0.
  • Every app that has the class passes an unknown _-prefixed parameter through: split_internal_payload removes only _telemetry and _metadata, nothing validates the annual parameters with extra="forbid", the baseline identity reads only scope and spm, and redact_params_for_logging drops _-prefixed keys from the logged attributes.
  • The deploy workflow deploys the gateway before the worker app. In between, the deployed .py 6.20.0 app passes the key through and records nothing.

Budget-window years

The years a budget-window batch spawns carry no job_queue_seconds. run_budget_window_batch validates its parameters with BudgetWindowBatchRequest (extra="forbid") in apps of every age, so the gateway keeps the key out of the batch payload; a test checks that. Decided on 2026-11-28: only annual jobs the gateway submits record a queue time, so batch years carry none.

Environment variables

The change reads and declares no environment variables, and changes no workflow, Dockerfile, Modal secret or Terraform file. Merging it redeploys the gateway and the current worker app through the existing workflow with their existing variables.

Testing

Red-green: each change's failing tests are committed before it (a6dbfc78 before 15203e10, 37c7aba8 before 31d6a2ca). 65701912 adds tests for code that already handled the case.

Only the tests this PR adds were run, each selected by node ID: 11 in the contract library, 2 in the gateway and 14 in the executor, all passing. Ruff format and lint, Pyright for the three changed packages and the quality guards pass. Full test suites were not run locally; CI runs them.

Two rounds of adversarial review by separate reviewers. Neither found a bug that could fail a worker app, change a result or reach logged attributes. Round 1 found number parsing, non-dict parameters, two documentation errors and a missing preemption test; round 2 found documentation errors and a missing failure test. All are fixed.

🤖 Generated with Claude Code

anth-volk and others added 6 commits November 28, 2026 00:09
The gateway is to send the job's submission time with the spawned
annual job, under an underscore-prefixed key, and the worker to set
job_queue_seconds on its run_simulation operation: the job record's
started_at, which is the first attempt's start, minus that time.

The tests cover the contract's calculation, the gateway's spawned
payload, the stage reporter's start time, the worker's attribute on
first and later attempts and on failures, the key's removal before the
calculation, and the written operation record. A guard test keeps the
key out of budget-window batch payloads, which run_budget_window_batch
validates with extra="forbid" on apps of every age.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gateway now sends the job's submission time with every annual job
it spawns, as the _job_submitted_at parameter, the same value it stores
as the job metadata's submitted_at. The worker removes it before the
calculation and sets job_queue_seconds on its run_simulation
operation when the operation opens: the stage record's started_at,
which is the first attempt's start and carries over to later attempts,
minus the submission time. Every attempt's record carries the value,
so a preempted job's successful attempt carries it too. A start before
the submission, which only a difference between the two machines'
clocks can cause, counts as 0.

Only apps that define the SimulationWorker class receive annual jobs,
and every such app passes unknown underscore-prefixed parameters
through and leaves them out of its logged attributes, so the gateway
sends the key to every route. Budget-window batch payloads don't get
it, since run_budget_window_batch validates them with extra="forbid",
so the years a batch spawns record no queue time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… parameters still run

job_queue_seconds reads its value as the gateway's job status does,
which takes a number or a numeric string as a Unix time, so 0 gives a
queue time of decades. And the worker now reads the submission time
with params.get before its operation opens, so parameters that aren't
a dict fail there, with no operation record and the stage record left
running, where before the calculation reported them.

Also cover a preempted attempt, whose record carries the queue time
like a failed one's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ough

job_queue_seconds now takes only an ISO 8601 string with a time zone,
as the gateway sends it, so a number from another sender records
nothing instead of a queue time of decades. The worker reads and
removes the submission time only from a dict, so parameters of another
type reach the calculation, inside the operation, as before.

The observability guide now says that a later attempt starts a new
stage record, and so counts the earlier attempts' time, whenever it
finds no earlier record (a failed read, failed writes, or a preemption
before the first write), and that the gateway keeps the key out of
the batch payload's top-level keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A failing read of the stage record's start time, or a failing
attribute write, must not change a calculation result (rule
sim.observability.failures-never-break-requests). The worker already
suppresses both; these tests show it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…jobs

The observability guide said the queue time never counts the wait
before a later attempt, without the exception its own "No earlier
record" bullet describes, and the worker's docstring had the same gap.
Both now say the value is the same on every attempt only while each
later attempt finds the earlier attempt's record.

The guide also says to take one record per function_call_id when
computing statistics over jobs, since a preempted job writes one record
per attempt, and names the two telemetry values the Modal payload
carries besides calculation inputs and functional metadata.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 21:44
@anth-volk
anth-volk merged commit ddb0114 into main Nov 27, 2026
30 checks passed
@anth-volk
anth-volk deleted the job-queue-seconds branch November 27, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Record each annual job's queue time in its run telemetry

1 participant