Skip to content

fix(scheduler): the pre-check hook is told which schedule is firing (#3966) - #3967

Merged
vybe merged 1 commit into
devfrom
feature/3966-pre-check-schedule-context
Oct 27, 2026
Merged

vybe merged 1 commit into
devfrom
feature/3966-pre-check-schedule-context

Conversation

@AndriiPasternak31

Copy link
Copy Markdown
Contributor

Description

~/.trinity/pre-check gates 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 as success.

The scheduler now sends the firing schedule's schedule_id, schedule_name and schedule_message in the pre-check request body. The backend exports them to the hook as TRINITY_SCHEDULE_ID, TRINITY_SCHEDULE_NAME and TRINITY_SCHEDULE_MESSAGE, in the Exec Create environment rather than argv. A hook that gates one schedule can now let the others through with printf '%s\n' "$TRINITY_SCHEDULE_MESSAGE".

The change is backward compatible:

  • Hooks that ignore the variables behave exactly as before.
  • A bodyless request runs the hook with no extra environment, so an older scheduler talking to a new backend works.
  • An older backend ignores the body, so a new scheduler talking to an old backend works.
  • The stdout contract (override / empty means skip / non-zero exit fails open) is unchanged.

Changes:

  • src/scheduler/service.py: _apply_pre_check_gate passes 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 optional InternalPreCheckRequest body 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.
  • Docs:
    • Scheduling → Pre-Check Hook: a variables table and a two-schedule bash example. I ran the example in all five cases: the other schedule, gated with no data, gated with data, older Trinity with the variables unset, and a multi-line message.
    • The scheduling FAQ, SCHED-COND-001 in requirements/scheduling.md, the feature flow, and the agent-runtime.md area file.
  • tests/registry.json: an entry for the new test file.

Related Issue

Fixes #3966

Journey Impact

Journey Impact: extends: J05

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Testing

tests/unit/test_3966_pre_check_schedule_context.py has 9 tests. They drive the real SchedulerService._apply_pre_check_gate / _run_pre_check with httpx mocked, the real pre_check_service.run_pre_check with the container exec mocked, and the real internal router through TestClient.

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_identity
  • test_the_hooks_stdout_contract_is_unchanged
  • test_the_hook_runs_with_the_firing_schedule_in_its_environment
  • test_only_the_fields_the_caller_sent_are_exported
  • test_a_message_with_quotes_and_newlines_is_exported_verbatim
  • test_the_schedule_in_the_body_reaches_the_service

The 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):

Command Result
pytest unit/test_3966_pre_check_schedule_context.py 9 passed
pytest scheduler_tests/ (includes test_pre_check.py, test_ent689_readiness_gate.py, test_1472_next_run_repair.py) 235 passed
pytest unit/ related files (test_2969_exec_timeout, test_3363, test_1968, test_1969, test_1808, test_1081, test_3940, test_3966) 173 passed, 1 skipped
pytest unit/ -m "not slow" -n auto --dist loadfile -p randomly --randomly-seed=1 (the CI invocation) 29220 passed, 91 skipped, 9 xfailed, 4 failed
python tests/lint_sys_modules.py / python tests/lint_root_test_placement.py OK / OK

The 4 failures are all in test_ent679_payments_pin_parity.py. My local venv resolves older payments-py / mcp / pyjwt / websocket-client than the image pins, and the same 4 fail identically on a clean origin/dev @ 40b329593 checkout. They're environment-only, not caused by this PR.

Not run:

  • The live tier and /verify-local. No real agent container executed a hook with the new environment; the exec is mocked at execute_command_in_container, whose environment= path already carries credentials for git (ent#615).
  • Python 3.13.
  • A schedule message larger than the kernel's per-string environment limit (128 KiB on Linux). I expect the hook exec to fail, which fails open (it fires with the configured message, like any broken hook). That is inferred from the existing fail-open path, not tested.

Checklist

  • My code follows the project's style guidelines
  • I have updated the documentation (if applicable)
  • I have not committed any sensitive data (API keys, credentials, etc.)
  • I have added appropriate logging for new functionality (none needed: the existing pre-check logging is unchanged)

Screenshots (if applicable)

n/a: no UI change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Di8diybSTz4qbYzhXsLBhZ

…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 dolho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/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… and scheduler_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

@vybe
vybe merged commit 9274314 into dev Oct 27, 2026
25 checks passed
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.

3 participants