Repository navigation
fix(scheduler): the pre-check hook is told which schedule is firing (#3966) - #3967
Merged
Merged
Conversation
…3966) `~/.trinity/pre-check` gates every cron schedule on an agent, and its non-empty stdout replaces the firing schedule's message, but it was never told which schedule that was. An agent with two cron schedules could not gate one without rewriting the other's task (#955; a daily brief ran the hourly refresh's message and was recorded as success). - scheduler: `_apply_pre_check_gate` passes the firing schedule; `_run_pre_check(agent_name, schedule=None)` sends its id, name and message in the request body (no body without a schedule, as before). - backend: `POST /api/internal/agents/{name}/pre-check` takes an optional `InternalPreCheckRequest`; `run_pre_check` exports the fields present as TRINITY_SCHEDULE_ID / TRINITY_SCHEDULE_NAME / TRINITY_SCHEDULE_MESSAGE in the hook exec's environment (Exec Create body, not argv). A bodyless request runs the hook with no extra environment, exactly as before. - docs: Scheduling -> Pre-Check Hook (variables + a two-schedule example), the scheduling FAQ, SCHED-COND-001, the feature flow, agent-runtime. - tests: tests/unit/test_3966_pre_check_schedule_context.py (9 tests; 6 fail on the pre-fix code) + registry entry. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Di8diybSTz4qbYzhXsLBhZ
dolho
approved these changes
Oct 27, 2026
dolho
left a comment
Contributor
There was a problem hiding this comment.
/validate-pr — Lane C (routers/internal.py)
| Check | Result |
|---|---|
| Commits / issue | conventional; Fixes #3966 (P2 bug) — closing keyword present ✅ |
| Internal router | still a thin passthrough; auth (X-Internal-Secret) untouched; the optional body changes no route shape ✅ |
| Untrusted input | schedule id/name/message reach the hook as exec environment, never argv or a shell string, and only the fields sent. The hook already receives that same message as its prompt, so nothing new crosses into the container ✅ |
| Backward compat | bodyless request → no extra env; old backend ignores the body; stdout contract unchanged ✅ |
environment= exists |
docker_service.execute_command_in_container(..., environment=...) on dev ✅ |
| Docs | requirements SCHED-COND-001, feature flow, agent-runtime.md, user guide + FAQ ✅ |
| Secrets / host paths | none ✅ |
| Tests on the PR head | test_3966_pre_check_schedule_context.py 9 passed; scheduler_tests/test_pre_check.py 15 passed ✅ |
| Mutation, reproduced here | the four src/ files restored to dev, tests kept → 6 failed, 3 passed, exactly the six you list (the three survivors are the backward-compat guards) ✅ |
| CI | 20 pass / 5 skipped ✅ |
Execution coverage (§5.4)
The tests drive the real SchedulerService._apply_pre_check_gate / _run_pre_check, the real pre_check_service.run_pre_check and the real router via TestClient; only the HTTP client and the container exec are mocked. Live consumers: src/scheduler/service.py::_apply_pre_check_gate → routers/internal.py → pre_check_service.run_pre_check.
Non-blocking
- Running
unit/test_3966…andscheduler_tests/in one pytest session errors at setup (two conftests), though each tier passes on its own and CI runs them separately. Worth knowing for anyone who runs them together locally. - The >128 KiB message case is fail-open by inference, as you say; a one-line note in the hook docs ("very long messages may not be exported") would set expectations.
APPROVE.
🤖 Reviewed with Claude Code
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.
Description
~/.trinity/pre-checkgates every cron schedule on an agent, and its non-empty stdout replaces the firing schedule's message. It was never told which schedule was firing. So an agent with two cron schedules could not gate one of them without rewriting the other's task (#3966; the same contract was behind #955). The wrong task then ran, and the execution was recorded assuccess.The scheduler now sends the firing schedule's
schedule_id,schedule_nameandschedule_messagein the pre-check request body. The backend exports them to the hook asTRINITY_SCHEDULE_ID,TRINITY_SCHEDULE_NAMEandTRINITY_SCHEDULE_MESSAGE, in the Exec Create environment rather than argv. A hook that gates one schedule can now let the others through withprintf '%s\n' "$TRINITY_SCHEDULE_MESSAGE".The change is backward compatible:
Changes:
src/scheduler/service.py:_apply_pre_check_gatepasses the firing schedule._run_pre_check(agent_name, schedule=None)sends the body, and no body without a schedule.src/backend/models.py+src/backend/routers/internal.py: the endpoint takes an optionalInternalPreCheckRequestbody and stays a thin passthrough.src/backend/services/pre_check_service.py:run_pre_check(..., schedule_id=, schedule_name=, schedule_message=)exports only the fields present, verbatim.requirements/scheduling.md, the feature flow, and theagent-runtime.mdarea file.tests/registry.json: an entry for the new test file.Related Issue
Fixes #3966
Journey Impact
Journey Impact: extends: J05
Type of Change
Testing
tests/unit/test_3966_pre_check_schedule_context.pyhas 9 tests. They drive the realSchedulerService._apply_pre_check_gate/_run_pre_checkwith httpx mocked, the realpre_check_service.run_pre_checkwith the container exec mocked, and the real internal router throughTestClient.Mutation: I reverted the
src/changes (stash, tests kept, restored byte-identical, checked by sha256 of the diff). 6 of the 9 then go RED:test_each_cron_schedule_reaches_the_backend_with_its_own_identitytest_the_hooks_stdout_contract_is_unchangedtest_the_hook_runs_with_the_firing_schedule_in_its_environmenttest_only_the_fields_the_caller_sent_are_exportedtest_a_message_with_quotes_and_newlines_is_exported_verbatimtest_the_schedule_in_the_body_reaches_the_serviceThe other 3 are backward-compat guards that pass on both the old and the new code.
Run locally (Python 3.14 venv; CI uses 3.13):
pytest unit/test_3966_pre_check_schedule_context.pypytest scheduler_tests/(includestest_pre_check.py,test_ent689_readiness_gate.py,test_1472_next_run_repair.py)pytest unit/related files (test_2969_exec_timeout,test_3363,test_1968,test_1969,test_1808,test_1081,test_3940,test_3966)pytest unit/ -m "not slow" -n auto --dist loadfile -p randomly --randomly-seed=1(the CI invocation)python tests/lint_sys_modules.py/python tests/lint_root_test_placement.pyThe 4 failures are all in
test_ent679_payments_pin_parity.py. My local venv resolves olderpayments-py/mcp/pyjwt/websocket-clientthan the image pins, and the same 4 fail identically on a cleanorigin/dev@40b329593checkout. They're environment-only, not caused by this PR.Not run:
/verify-local. No real agent container executed a hook with the new environment; the exec is mocked atexecute_command_in_container, whoseenvironment=path already carries credentials for git (ent#615).Checklist
Screenshots (if applicable)
n/a: no UI change.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Di8diybSTz4qbYzhXsLBhZ