fix(cron): find unified-runtime cron runs by their scheduler admission #139273
Reviewed at 2c5e7ac7. The 10 new tests fail on current main and pass on this head. test_unified_run_is_one_owned_session_row_and_detail_agrees passes at this head. The 8 test_hermes_state.py vacuum and maintenance failures I see locally fail on clean main too.
Verified correct:
:, _, % and [], and the policy-based split for ambiguous ids.EXPLAIN QUERY PLAN shows a range seek on idx_session_admissions_cron_request; the OR is applied as a filter after the seek. list_cron_job_runs(limit=20) took about 1ms on a 200-run job with 20k interactive sessions.cron_<job>_YYYYmmdd_HHMMSS._open_read_only does no DDL.: guard: it can't refuse a real scheduler id, which is always uuid4().hex.cronRunWriteGateApplies covers everything the old submit.ts condition did.Warning: the same-second case is only partly fixed. If another attempt B is claimed before or in the same second as run A's doc (a manual hermes cron run overlapping a tick), B becomes candidates[0]. If B never created a session:
cron_output row with B's status;represented.On main the same case just drops B, so this isn't strictly a regression, but represented is still decided before reconciliation. No test covers it.
Nits
hermes_state_common.py: the partial index hard-codes WHERE principal_id = 'cron-owner', while the queries inline CRON_PRINCIPAL_ID. If the two ever drift apart, the planner silently stops using the index. Build it from the constant or assert they're equal._reconcile_cron_runs relies on _execution_candidates and _execution_id passed inside row dicts, and treats a missing key differently from an empty tuple. Returning docs and orphan ledger rows separately from _list_cron_output_runs would remove both and most of the docstring._CRON_ADMISSION_SQL hard-codes substr(request_id, 6) and length(:request_lo) - 5. Deriving them from len(_CRON_REQUEST_PREFIX) would be easier to verify.prefix[:-1] + chr(ord(prefix[-1]) + 1) now exists three times. A prefix_range() helper in hermes_state_keys would cover all three.owner fixtures and fire helpers. test_a_same_second_output_doc... also re-imports hermes_time and timedelta, which are already imported at module top.local-* cron tab still isn't write-gated before any list loads its row.Reviewed at 2c5e7ac72e, on top of @kshitijk4poor's review above; what it verifies is not repeated here. One addition on the Desktop side: with the old open-cron-run.ts under the new tests, the three #138812 gate cases fail, and all pass on this head.
@whyyagswhy's report that test_unified_run_is_one_owned_session_row_and_detail_agrees fails at 70af70a52b has no reply yet. I couldn't reproduce it at that commit: it passed in repeated runs and under UTC, America/Los_Angeles and Asia/Kolkata, and so did the tip of #138968. Asking which environment showed the second row would settle whether there is a real path to it.
Not from this PR: agent/transports/codex.py still derives a job's prompt-cache scope from the legacy cron_<job>_<timestamp> id, so unified-runtime fires of one job no longer share a scope. That predates this change and fits a follow-up.
@kshitijk4poor thanks, fixed in db1b816a.
_list_cron_output_runs now returns docs and ledger attempts separately, and _reconcile_cron_runs decides each doc's writer only after sessions are known. An attempt that finished before the doc's second loses; then an attempt whose own session spans the doc beats one with no session; then the newest claim wins. Each attempt gets one row: its session, else its doc, else its ledger row. The _execution_candidates and _execution_id keys are gone. test_an_overlapping_attempt_claimed_before_a_runs_doc_keeps_its_own_row (with B finishing before and after A's doc) failed before: A's doc showed as an extra FAILED row, and B's own row was hidden.WHERE is built from CRON_PRINCIPAL_ID.substr offsets: derived from len(CRON_REQUEST_PREFIX), which is now public.prefix_range() helper in hermes_state_keys, used in all three places.cron_owner() and admit_fire() are shared in tests/fakes/cron_owner.py, and the duplicate imports are gone.local- tab before any list loads:** not addressed here, as you noted.@whyyagswhy, the failure you reported in test_unified_run_is_one_owned_session_row_and_detail_agrees only happened outside TZ=UTC. 2c5e7ac7 pins the zone in the fixture, and both cron test files pass under TZ=America/New_York.
Tests: 166 related files, 1124 passed, 0 failed, 6 skipped. Desktop cron vitest: 69 passed. scripts/check ok.
unsupportedpastels
<img src="https://v3b.fal.media/files/b/0aae8017/tU0ZVQ8chitWDT6hXuwkr_qqsazS8L.png" alt="Cron run history" width="480">
What does this PR do?
Lane 3 of package 4 (#139162) from the unified-gateway tracker #134889.
CRON-1: unified-runtime cron runs appear in run history and open (#138812)
Agent cron runs on the unified runtime get gateway-minted
local-session ids. Run history only looked for the legacycron_<job>_<YYYYmmdd_HHMMSS>ids, so Desktop couldn't list or open those runs.local-ids stay, because exact-once retry rebuilds them from the admission id. Run history now finds a job's runs through the admission the scheduler already writes for every fire,cron:<job>:<execution>. A new partial covering index,idx_session_admissions_cron_request, is inSCHEMA_SQL. Legacy runs are still found by their id.SessionDBread-only. The gateway stays the only writer, and the queue contract is unchanged (#106742).:are refused for new admissions only. An admission made before this change keeps its exact submit/recover retry. Run history files it under its job using the stored policy record, not by splitting on the last:.YYYYmmdd_HHMMSS, so jobdailyno longer lists jobdaily_digest's runs (refs #92133).Credit
Co-authored-bytrailers.Related Issue
Fixes #138812
Refs #134889 (CRON-1) · Refs #139162 · Refs #92133
Type of Change
Changes Made
gateway/session_cron.py: the:guard applies to new admissions only; exact retry of existing identities is kepthermes_state_keys.py,hermes_state_common.py,hermes_state_portability.py: admission-keyed run lookup, the partial index, policy-based job association for ambiguous ids, and the strict legacy suffixhermes_cli/web_routers/cron.py,hermes_cli/web_routers/sessions.py: execution-aware reconciliation, candidate-only document matching, and execution identity in session detailapps/desktop/src/app/cron/open-cron-run.ts(+ test),apps/desktop/src/app/cron/index.tsx,apps/desktop/src/app/chat/sidebar/cron-jobs-section.tsx,apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts: cron sessions recognised by sourcetests/hermes_state/test_cron_run_sessions.pyandtests/hermes_cli/test_cron_run_history_sessions.pyare new;tests/hermes_state/test_hermes_state.pyis extendedHow to Test
1.
scripts/run_tests.sh tests/hermes_state/test_cron_run_sessions.py tests/hermes_cli/test_cron_run_history_sessions.py tests/hermes_state/test_hermes_state.py2. Without the fix, the run-history tests fail: the
local-run is missing and only the legacy row is returned. Without the review fixes, 4 tests fail: a pre-upgradefire:1admission is refused on retry, and a same-second output document is pinned on the earlier execution (2 variants).3. Results:
- 166 Python test files that touch cron run history,
session_cronor admissions: 1121 passed, 0 failed, 6 skipped.-
scripts/check: ok.- Desktop vitest for the cron, sidebar and prompt-action files: 779 passed, and
tsc --noEmitis clean. This ran before the review fixes, which changed no Desktop files.An independent review pass (gpt-6-astra) returned needs changes with two findings: colon-bearing ids admitted before the upgrade lost their exact retry, and a document's guessed execution was treated as certain. Both are fixed above, each with a test that failed first.
Known gaps
main.state.dbgets the new index on its next writable open, which the gateway does. Until then lookups are correct but unindexed.Checklist
Code
pytest tests/ -qand all tests pass. Not ticked: the full suite wasn't run. The 166 related files pass, as listed above.Documentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AUpdate after review (head db1b816a)
WHEREis built fromCRON_PRINCIPAL_ID, thesubstroffsets come from the prefix length, and aprefix_range()helper replaces three copies of the half-open bound.tests/fakes/cron_owner.py.local-cron tab isn't write-gated before any list loads its row.TZ=America/New_York. Desktop cron vitest: 69 passed.scripts/checkok.