fix(scheduler): return a real execution_id, and 409 when nothing started (#1968) - #1976
Conversation
/review — self-review (PR #1976, #1968)Branch: Caveat: this is a self-review, so it is worth less than an independent pass. I went looking for reasons the change is wrong rather than reasons it is right, and found one worth acting on. [C1] Concurrency: the spawned task was only weakly referenced (Confidence: 8/10) — FIXED in
|
|
Resolve by running |
db46a5a to
27f4632
Compare
|
Rebased onto current Conflicts were in Local verification after the rebase is in the individual runs; no source conflicts, only the registry. Ready for review. |
|
Two separate reds here — one was mine, one is not this PR. The three new failures were my rebase (fixed)
Git never flagged it: both sides added the class at different offsets, so the textual auto-merge took both hunks with no conflict. A semantic duplicate only a reader or an importer would catch. Two failures on my side let it through, and both are worth naming:
Kept the first copy (strict superset: same fields, I also swept the other six branches I rebased today for the same class of damage — duplicate top-level defs in any changed The base-side red is not this PR
Same shard has now done this on #1952 too, so it is a specific ordering under seed 67890 on dev, not a random flake. Filed as #2019: the workflow installs |
|
Correction to my previous comment on the base-side red: I wrote that the twice-hit Since then it has hit The evidence points at something timing-dependent that usually returns fast and occasionally blocks: the #1952 log shows a 3m37s gap with zero output between two progress lines, which is a stall rather than slowness. Details and the corrected table are on #2019. Nothing changes for this PR — the base-side red still isn't yours, and the fix (a per-test |
|
Correction to my earlier comment on the duplicate I wrote that the duplicate was "this PR's at line 110 and dev's copy (#1974, as merged) at 187", and that the rebase had shadowed this PR's fixes. That attribution is backwards.
So: same defect, same fix, wrong story. What I deleted was the branch's own outdated copy, and what I kept is dev's — which is the right outcome either way, but "this PR's improvements became dead code" was wrong. They were never this PR's improvements. Nothing to change in the code. Correcting it because the comment is the record, and the next person reading it would otherwise credit the guard to the wrong PR. |
vybe
left a comment
There was a problem hiding this comment.
The stack is resolved correctly — merge-base is e6df5bf8, the branch carries #1974's own commit un-squashed while dev has the squashed version, and the three-dot diff (what GitHub shows and what merges) contains zero #1974 content. The remaining conflicts are learnings.md and tests/registry.json only, from newer dev commits, so a plain rebase clears them.
The core work is good: exactly-one-row is asserted at both ends, lock release is exactly-once across all five exits (the early returns sit before the try, so lock.release() is never reached with lock=None), 404-before-lock ordering is right, and the strong task reference is genuinely load-bearing given the reordering. The regression tests are real — reverting src/scheduler/ to dev fails 12 of 23.
One thing blocks.
The webhook consumer was missed, and it regresses.
src/backend/routers/webhooks.py is the fourth caller of the scheduler /trigger endpoint, and it wasn't updated. The scheduler now returns 409 when a webhook fires against a schedule that is already running, and this gate catches it:
if response.status_code not in (200, 202):
logger.error(f"Webhook trigger: scheduler error {response.status_code} …")
raise HTTPException(status_code=503, detail="Trigger failed — try again later")So a healthy, busy schedule now produces an ERROR log line and a 503 "try again later" to an unauthenticated public caller — advice that only hits the same lock — and the except HTTPException: arm below calls idempotency_service.fail(idem), releasing the #525 dedup claim for something that is not a failure. Before this PR that delivery returned 202.
The blast-radius table lists three consumers (UI, CLI, MCP) and asserts none regress; the omitted fourth is the one that does. And tests/test_webhook_triggers.py asserts status_code in (202, 503) throughout, so CI accepts the regression silently — which is why it slipped.
Non-blocking:
_abandon_precreated_executioncallsdb.update_execution_status, which in the standalone scheduler is an unconditionalUPDATE … WHERE id = ?with no status precondition — a new non-CAS status writer, the set architecture.md already names as the open #1082 follow-up. Safe today (the abandon fires only on gates that run before dispatch, so no competing writer exists), butAND status = 'running'is one clause and makes it safe by construction rather than by argument.- Error handling is asymmetric:
try_acquire_schedule_lockis unwrapped at the original call site and wrapped intry/exceptat the new fallback. A Redis outage at the first raises → 500. Honest (never a false 409), but one of the two is wrong. stop()never drains_inflight_triggers— the strong-ref set defeats GC, not shutdown. The exact failure the new comment describes stays reachable on SIGTERM, now with a row that didn't previously exist.- The backend's
timeout=10.0wasn't revisited, though the handler now does a RedisSETplus a synchronous SQLiteINSERTbefore responding. Under write contention a >10s handler yields 504 while the run is genuinely in flight — a fresh instance of "it's running and you have no id", which is the situation this PR exists to eliminate. - Lock-denied manual triggers now produce no audit row at all (the 409 raises before
platform_audit_service.log). Previously they produced a misleading one; sibling #1975 added the honest lock-denied audit for cron ticks, and the manual path arguably wants the same rather than nothing. schedules.ts: theconsole.log(...execution_id: ...)sits before the!result.execution_idguard, so it still printsundefined— the same string the issue is named after.- Six tests are source-regex assertions over the backend/MCP/Vue surfaces rather than behavioural — acceptable for cross-language seams, but they'd pass against a renamed-yet-broken implementation, and there is no webhook-consumer test at all, which is the gap that let the finding above through.
Timestamps are correct throughout (utc_now_iso / to_utc_iso from src/scheduler/utils.py), so Invariant #16 / #1713 parity is preserved.
|
Re-validated after the 2026-08-08 push. The rebase is clean and the non-blocking items I raised are partly addressed — but the one blocking item is still open, so I'm holding this rather than merging it with today's batch.
So after this merges, a webhook firing at a healthy, busy schedule returns 503 to an unauthenticated public caller where it previously returned 202, writes an ERROR line for a non-error, and drops the idempotency claim for something that did not fail. The advice "try again later" only re-hits the same lock. The single webhook test added ( I'm deliberately not fixing this for you, because the correct behaviour is a product decision I shouldn't make unilaterally. The public webhook contract today is 202-accepted; the options are at least:
My inclination is (2), since the caller's intent ("make sure this runs") is already satisfied and the #525 claim should stand — but it's your call and the issue doesn't settle it. Whichever you pick, it needs a test at the backend layer, and Everything else I checked is in good shape: exactly-one-row asserted at both ends, lock release exactly-once across all five exits, 404-before-lock ordering, the strong task reference, and Re-request review once the webhook consumer is settled and I'll merge it straight away. |
|
@vybe — blocking item fixed: the fourth consumer is handled, and it was regressing exactly as you described.
The response message says the delivery was folded into the run in flight rather than claiming a fresh execution started, so the 202 isn't a second lie in place of the first. Genuine scheduler errors (500/502/400) still surface as 503 — the carve-out is on 409 alone, and a parametrized test pins that it doesn't widen into "any non-2xx is fine". On the test gap you identified — Two of the non-blocking notes, both one-liners:
Left alone deliberately, and worth a ruling if you disagree: the Merged latest |
vybe
left a comment
There was a problem hiding this comment.
Re-reviewed. The blocking item is fixed and the fix is scoped correctly.
routers/webhooks.py 409 carve-out — the fourth consumer now treats a busy schedule as its own outcome: 202 {"status":"already_running"} with an INFO log and idempotency_service.complete(...) carrying a snapshot, instead of 503 + fail(idem) releasing a key for something that never failed. Genuine scheduler errors (400/500/502) still surface as 503, and the parametrized test pins that the carve-out doesn't widen into "any non-2xx is fine" — which was my actual worry.
The response wording matters and you got it right: saying the delivery was coalesced into the run in flight, rather than claiming a fresh execution started, avoids replacing one lie with another.
On the test gap — assert status_code in (202, 503) is exactly why CI accepted the regression silently, and naming the status in the new tests is the durable fix. Running them in-process on #1422's harness rather than against a live instance is the right call.
The two extras are good: expected_status=RUNNING on _abandon_precreated_execution makes it safe by construction rather than by argument (#1082's whole point), with expected_status=None keeping every other caller byte-identical; and moving the schedules.ts log below the guard fixes the literal string the issue is named after.
Deferred items accepted — each is a behaviour decision, and this PR already spans scheduler, backend, MCP and Vue.
`schedule_executions` has carried five origin columns for audit since
AUDIT-001, and the backend populates them on every path it owns. The
scheduler is a separate service with its own DB module, and its
`create_execution()` listed none of them in the INSERT — nor accepted
them in its signature, so there was nowhere to put a caller even if one
had been forwarded. Every scheduler-created row was written with all
five NULL.
`triggered_by='manual'` therefore recorded *that* a human ran something
and never *who*. The attribution lived only in backend/MCP-server logs,
bounded by log retention, so past a few weeks the durable record could
not answer "did anyone trigger this run, and who?".
The identity was dropped at three points, not one:
1. the backend's delegating POST sent no body at all, so the
authenticated caller — in scope right there — never crossed the hop;
2. `_trigger_handler` had no parameter to receive one;
3. `create_execution()` had nowhere to put it.
An `ExecutionOrigin` value object is threaded through all three. One
object rather than five parallel parameters at four call depths: five
positional siblings is how one of them silently stops being forwarded.
Two paths the DB fix alone would have left blank are covered too. A
retry inherits the original run's origin — it has no caller of its own,
but a chain of retries that drops the initiator makes the first attempt
the only attributable one; the read is fail-open, since an audit lookup
must not be able to stop a retry from running. A reminder inherits the
provenance #1296 already persisted.
Cron ticks stay NULL. Attributing an autonomous fire to, say, the
schedule's owner would make the column actively misleading — a blank
reads as "unknown", a wrong name does not.
Also hardened while here:
- the untrusted trigger body is validated at the scheduler boundary.
`source_user_id` is dropped rather than coerced when it is not an int:
`bool` IS an `int` in Python, so `True` would have persisted as user
1, a real account attributed to a run it had nothing to do with.
Strings are length-capped and blank-to-None, so "" and NULL are not
two spellings of "unknown".
- the backend prefers the validated `current_user.agent_name` over the
raw `X-Source-Agent` header — the reverse of chat.py's precedence,
which is fine for a collaboration hint but would let a caller pin its
run on a sibling agent in an audit column.
- the MCP trigger tool forwards the origin headers `chat()` already
sends (Invariant #13). Without it an MCP-triggered run attributes to
the key OWNER but not to which key or agent fired it — the part that
identifies the actor when one human owns many of both.
Not a vulnerability: nothing authorizes on these columns.
Backward compatible in both rolling-deploy directions — an old scheduler
ignores the new body fields, and a new scheduler treats a bodyless POST
as an unattributed manual trigger.
tests/unit/test_1970_execution_origin.py — 27 checks, 25 of which fail
against the pre-fix tree.
Related to #1970
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…ted (#1968) `_trigger_handler` was fire-and-forget: it spawned the run with `asyncio.create_task` and responded immediately, *before* the execution record existed. So it had no id to return. The backend relayed the same id-less fields, and the MCP tool interpolated the missing key — telling every agent `Execution started with ID 'undefined'`, on every trigger, while the execution ran fine. Callers could not correlate a trigger with its run, poll it, or fetch its result; the workaround was to guess from `list_recent_executions` by timestamp. The same ordering hid a second problem. The response was emitted before `_execute_manual_trigger` had even attempted the distributed lock, so a trigger suppressed because the schedule was already running still answered `"status": "triggered"`. A suppressed trigger and a real one were byte-identical to the caller. The handler now acquires the lock and creates the row synchronously, then hands both to the background task. That makes two facts sayable that simply did not exist yet at response time: which execution this is, and whether one was started at all. * 200 carries a real `execution_id`, valid the moment the caller receives it — a fast poller must not 404. * 409 `already_running` replaces the false "triggered", with no id and no row, because nothing ran. Exactly one row per trigger: `_execute_schedule_with_lock` takes the pre-created execution and skips its own create. Two rows would hand the caller an id naming a row that never runs while a second did the work. Because a row can now exist before a gate decides not to run, an abandoned run FAILs its pre-created row rather than leaving it `running` forever — canary E-01's exact signature, and a task the UI would show indefinitely. The handler also now holds the lock across a DB write, which is new, so every exit from that window releases it: creation raising, creation returning None, the run raising, and normal completion — exactly once each. A second release is the dangerous one, since a lock re-acquired by the next run in between would be freed out from under it. Relayed through the remaining surfaces: * the backend forwards `execution_id` (and records it on the audit row, so a trigger and its run are joinable after the fact) and maps 409 rather than flattening it into "Failed to trigger schedule" — a worse lie than the original, since it claims failure where the schedule is healthily busy; * the MCP tool returns a structured `already_running` instead of throwing, so an agent gets a decision it can act on, and GUARDS the id instead of interpolating it — an older backend still omits the field, and swapping one confident lie for another is not a fix; * `ScheduleTriggerResult.execution_id` becomes optional. Typing it as a required `string` while the wire never sent it is precisely why the compiler stayed happy through every `undefined`; * the UI reads 409 as "already running" rather than "nothing was changed — try again", and the CLI prints the id it was already fetching and discarding. tests/unit/test_1968_trigger_execution_id.py — 22 checks, 17 of which fail against the pre-fix tree. Related to #1968 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…1968) Self-review finding on this PR. The event loop holds only a WEAK reference to a task, so a bare `asyncio.create_task(...)` whose result nobody keeps can be garbage-collected mid-flight — the asyncio docs say so outright. The bare call predates this PR, but this PR changes what it costs. Before, a collected task meant the run silently did not happen. Now the lock is acquired and the execution row created BEFORE the task is spawned, so a collected task strands a `running` execution whose id the caller already holds and pins the schedule's lock until its Redis TTL. Uses the `_inflight` set + `add_done_callback(discard)` shape the #1083 result-callback path already established (`agent_server/services/result_callback.py`), so the set cannot grow without bound. Guarded by `test_the_spawned_task_is_strongly_referenced`, which checks the reference is held in the window BEFORE the task runs — the only window where collection is possible — and released after. Related to #1968 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…1968 review Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…uced My rebase of this branch onto dev left `src/scheduler/models.py` with TWO `class ExecutionOrigin` definitions: this PR's at line 110 and dev's copy (#1974, as merged) at 187. Python keeps the last one, so the second shadowed the first and both of this PR's fixes became dead code — the None-comparing `is_empty` and the SQLite-range guard on `source_user_id`. Three `test_execution_origin_properties` cases went red on head, correctly. Git did not flag it. Both sides added the class at different offsets, so the textual auto-merge took both hunks and reported no conflict — a semantic duplicate that only a reader or an importer would notice. Two things on my side let it through: - I re-read only the files git marked as conflicted, not the whole merged result of a 4-commit rebase. - My post-rebase check was `-k "1968 or ent326 or timeline or scheduler or executions"`, which does not match `test_execution_origin_properties`. The filter was narrower than the blast radius, so 129 tests passed and said nothing about the file I had just broken. Kept the first copy: it is a strict superset (identical fields, `is_empty` comparing against None so `user_id=0` is not reported empty, and the `_SQLITE_INT_MIN/MAX` range check that stops an out-of-range id reaching the INSERT and taking the dispatch down). Deleted dev's older copy. Swept the other six branches I rebased for the same class of damage — duplicate top-level defs in any changed .py — all clean. 350 passed across origin/scheduler/1968/1969/1970/execution. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The blast-radius table listed three consumers of the scheduler's `/trigger` endpoint and asserted none regress. There is a fourth — `routers/webhooks.py`, the unauthenticated public trigger — and it is the one that does. With #1968's 409, `if response.status_code not in (200, 202)` caught a HEALTHY, busy schedule and turned it into an ERROR log line plus a 503 'Trigger failed — try again later' to a public caller, advice that only hits the same lock. Worse, the `except HTTPException` arm then called `idempotency_service.fail(idem)`, releasing the #525 dedup claim for a delivery that never failed — so a retry could fire a second execution the moment the lock cleared. Before #1968 that delivery returned 202. Now a 409 is recognised as its own outcome: INFO log, the claim COMPLETED with a snapshot recording what happened, and 202 with `status: "already_running"` and a message saying the delivery was coalesced into the run in flight rather than claiming a fresh execution started. Genuine scheduler errors (500/502/400) still surface as 503 — the carve-out is on 409 alone, and a test pins that it does not widen. tests/test_webhook_triggers.py asserts `status_code in (202, 503)` throughout, which is why CI accepted the regression silently, so the new tests name the status instead of tolerating a set. They run in-process (TestClient + faked scheduler) rather than against a live instance: carve-out removed: 3 failed, 3 passed as shipped: 6 passed Two of the non-blocking notes, both one-liners: * `_abandon_precreated_execution` now passes `expected_status=RUNNING` to a new optional CAS precondition on the scheduler's `update_execution_status`. It was safe by argument (every abandon gate runs before dispatch); #1082 exists to retire exactly that kind of argument, and the clause makes it safe by construction. Default None keeps every other caller byte-identical. * `schedules.ts` logged `execution_id: undefined` above the `!result.execution_id` guard — the exact string this issue is named after. Moved below it. Related to #1968 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
a81a854 to
4bb98fa
Compare
Problem
_trigger_handlerwas fire-and-forget: it spawned the run withasyncio.create_taskand responded immediately — before the execution record existed.So the backend relayed id-less fields, and the MCP tool interpolated the missing key:
on every trigger, while the execution itself ran fine. A caller could not correlate its trigger with a run, poll it, or fetch its result. The workaround was guessing from
list_recent_executionsby timestamp.The same ordering hid a second problem. The response was emitted before
_execute_manual_triggerhad even attempted the distributed lock. A trigger suppressed because the schedule was already running still answered"status": "triggered"— a suppressed trigger and a real one were byte-identical to the caller, with only a scheduler-side WARNING as evidence.Fix
Acquire the lock and create the row synchronously in the handler, then hand both to the background task. This makes two facts sayable that did not yet exist at response time:
'undefined'execution_id, valid the moment the caller receives it"triggered"409 already_runningwhen the lock is deniedPer the pre-work decision, this is the full variant including the 409 — the issue notes the honest-suppression half is where the operator value sits.
Exactly one row per trigger
_execute_schedule_with_locknow accepts the pre-created execution and skips its own create. Two rows would be worse than none: the caller's id would name a row that never runs while a second row did the work. Asserted at both ends — the handler's, and the service's.Consequences the issue didn't list, handled here
A row can now exist before a gate decides not to run. If the schedule is deleted in the window between the response and the task starting, the row is already created and its id already in the caller's hands. Left
runningit never terminates — canary E-01's exact signature, and a task the UI shows forever. Abandoned runs now FAIL their pre-created row with a reason. The helper is best-effort and never raises: it runs on abort paths, inside a background task nobody awaits.The handler now holds a lock across a DB write. That is a new failure window, so every exit from it releases:
create_executionraising,create_executionreturningNone, the run raising, normal completion — exactly once each. Deliberately structured as acquire-then-single-finallyrather than a release in both thefinallyand an error branch: the second release is the dangerous one, since a lock re-acquired by the next run in between would be freed out from under it. (I wrote the two-release version first; it is the kind of thing that looks defensive and is not.)The 404 gate stays ahead of the lock — locking a schedule that does not exist would block nothing and leak a key until its TTL.
Relay through the remaining surfaces
execution_id, records it on the audit row (a trigger and its run are now joinable after the fact, not just correlatable by timestamp), and maps 409 rather than flattening it into"Failed to trigger schedule"— a worse lie than the original, since it claims failure where the schedule is healthily busy.already_runninginstead of throwing, so a calling agent gets a decision it can act on rather than a stack-shaped string, and can choose to poll instead of retrying into the same lock. It also guards the id instead of interpolating it — an older backend still omits the field, and swapping one confident lie for another is not a fix.ScheduleTriggerResult.execution_idbecomes optional. It was typed as a requiredstringwhile the wire never sent the field at all — which is precisely why the compiler stayed happy through everyundefined. Worth calling out: the type asserted a guarantee nothing upheld.dataand discarding.Blast radius of the 409
All three consumers improve; none regress:
'undefined'id, looked successfulalready_running+ poll hintVerification
tests/unit/test_1968_trigger_execution_id.py— 22 checks, 17 of which fail against the pre-fix tree:One test failure during development was mine, not the code's, and is worth recording: the fixture's
schedule_executionstable omitted the columnsupdate_execution_statuswrites, so the abandon path raisedOperationalError, its fail-safe swallowed it, and the row stayedrunning— the test "found" a bug that was the fixture's. The table now carries that column set with a comment tying it to the UPDATE.Also green: the full
scheduler_tests/suite plus the stacked #1970 tests — 269 passed;tsc --noEmitclean;test_1808/test_1945unaffected.Why stacked
_trigger_handleris where #1970 (PR #1974) parses the caller identity, and this PR movescreate_executioninto that same function. Built offdevinstead, whichever merged second would conflict there and the origin arguments would have to be re-attached to the moved call by hand — silently regressing #1970 to all-NULL if missed.test_precreated_row_still_carries_the_caller_identitypins that they moved together.Acceptance criteria
execution_idend to end (scheduler → backend → MCP)already_running) instead of a false successrunningwhen the run is abandonedSiblings
#1974 (#1970) is the parent. #1975 (#1969, lock-denied cron tick audit) is independent and already green — it touches
_execute_schedule, the cron entry point, not_execute_manual_trigger.Related to #1968
🤖 Generated with Claude Code
Fixes #1968