Repository navigation
Record each annual job's queue time in its run telemetry - #1144
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1143
Same-Repository Draft PR Check
PolicyEngine/policyengine-sim-api, not a personal fork.Changelog
changelog.d/job-queue-seconds.added.md, whose type is added.Summary
Each annual job's
run_simulationoperation record now carriesjob_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.submit_simulationadds the submission time to the spawned job's parameters as_job_submitted_at(JOB_SUBMITTED_AT_PARAMin the contract'sjob_progress.py), the same ISO 8601 value it stores as the job metadata'ssubmitted_at._run_simulationremoves the key from the parameters when the call starts, so the calculation and segment children never see it. When the operation opens, it setsjob_queue_seconds: the stage record'sstarted_atminus the submission time.started_atis 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.job_queue_secondsin 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.JobProgressReportergainsstarted_at().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:
SimulationWorkerclass, which apps deployed before 2026-11-25 (23aea894) don't define; on those apps the spawn already fails withNotFoundError. Production routing on 2026-11-28 sendslatestto .py 6.20.0._-prefixed parameter through:split_internal_payloadremoves only_telemetryand_metadata, nothing validates the annual parameters withextra="forbid", the baseline identity reads onlyscopeandspm, andredact_params_for_loggingdrops_-prefixed keys from the logged attributes.Budget-window years
The years a budget-window batch spawns carry no
job_queue_seconds.run_budget_window_batchvalidates its parameters withBudgetWindowBatchRequest(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 (
a6dbfc78before15203e10,37c7aba8before31d6a2ca).65701912adds 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