Skip to content

fix(pull): the system agent claims its own queue with its system-scoped key (abilityai/trinity#3814) - #3832

Merged
obasilakis merged 2 commits into
devfrom
feature/3814-system-agent-pull-claim
Oct 20, 2026
Merged

obasilakis merged 2 commits into
devfrom
feature/3814-system-agent-pull-claim

Conversation

@obasilakis

@obasilakis obasilakis commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Seams the system key now passes

Seam System key, own name System key, other agent
GET /api/internal/next-task 200 (allowlist still required) 403
POST /api/internal/tasks/{id}/result CAS applies uniform 404
POST /api/agents/trinity-system/heartbeat 200 403
POST /api/agents/{name}/executions/{id}/result (#1083, until push is deleted) passes auth 403

No lease-renew seam exists; the pull worker calls only the first two. Every other path for system-scoped keys is unchanged. Auto-sync: the armed URL starts the auto-sync loop; any non-200 from the git routes falls back to the env value, so it cannot enable a disabled sync.

Known effect

An unpiloted system container that still carries TRINITY_BACKEND_URL from before #1816 was in a permanent 403 heartbeat loop. Its heartbeats are now accepted, so it appears in fleet health. Fresh and post-#1816 containers carry no URL unless piloted. This is the interim answer to #1816 §10 and can be revisited there.

Changes

  • services/heartbeat_service.py — authorize_heartbeat admits the system agent's own system-scoped key
  • routers/internal.py — result-seam pre-check uses the predicate on the key's own bound name
  • services/agent_service/pull_mode.py — system-agent TRINITY_BACKEND_URL set-or-clear in apply_pull_mode_env
  • services/agent_service/helpers.py — drift check requires the URL for a piloted system agent
  • services/system_agent_service.py — creation applies the pull env; _report_pull_mode at boot
  • services/platform_alerts.py — system_agent_pull_drift kind under the reserved base-image-stale- family, plus its legacy-subject parser
  • routers/system_agent.py — /restart re-reports pull state on the replacement container, so the alert ends at the remedy it names
  • Pins updated: test_1816_* (heartbeat stays off unless piloted, plus piloted counterparts), test_3590 writer set
  • Docs: architecture/backend.md, architecture/reliability.md, feature-flows/agent-heartbeat-liveness.md, feature-flows/internal-system-agent.md; learnings fragment; CSO diff report (0 findings)

Test Plan

Fixes #3814

🤖 Generated with Claude Code

obasilakis and others added 2 commits October 20, 2026 09:48
…ed key (#3814)

trinity-system's MCP key is scope="system", bound to its own name. The pull
seams and the heartbeat accepted only scope="agent", so a system agent put in
PULL_MODE_PILOT_AGENTS had every poll and heartbeat refused (403) while the
backend routed its turns to the durable queue. Push is being retired, so the
system agent needs a claim path rather than an exemption.

- authorize_heartbeat accepts a system-scoped key only when the key row's
  agent_name equals the path and the path is the system agent. It gates
  next-task, the pull result seam, the heartbeat and the #1083 callback.
  A system key never acts for another agent (403 / uniform 404).
- The result seam's pre-check runs the same predicate against the key's own
  bound name, so an unbound system key still gets 403.
- apply_pull_mode_env gives a piloted system agent TRINITY_BACKEND_URL (its
  worker cannot start without it) and removes it, clearing the heartbeat
  seen marker, on de-pilot. _create_system_agent now applies the helper.
- check_pull_mode_env_matches treats a piloted system agent without the URL
  as drift, so a pre-fix container is recreated on its next cold start.
- ensure_deployed logs a WARNING while the system agent is piloted and files
  a system_agent_pull_drift platform alert while its running container has
  no worker (it is never recreated while running).

Mutation check: reverting each production change turned a test red
(test_system_key_acts_for_the_system_agent, test_result_system_scoped_key_403,
test_piloted_system_agent_gets_pull_flag_and_backend_url,
test_creation_arms_a_piloted_system_agents_pull_worker,
test_piloted_system_agent_without_backend_url_is_drift,
test_boot_reports_the_pull_state_of_a_running_system_agent,
test_the_kind_is_registered_under_a_reserved_prefix).

Fixes #3814

Co-Authored-By: Claude Opus 5.5 <[email protected]>
)

- Boot logs the pull-mode WARNING on a fresh install too (create branch).
- POST /api/system-agent/restart re-reads the replacement container, so the
  system_agent_pull_drift alert ends at the restart it names as the remedy.
- Legacy-subject parser for the new base-image-stale-pull- prefix.
- apply_pull_mode_env no longer clears the heartbeat itself; every caller
  recreates the container and clear_agent_breakers already does it.
- Stale wording: the 403 detail, pull_mode and reports docstrings, and the
  internal-system-agent flow.
- Learnings fragment for the self-acting-gate class.

Mutation check: removing each new wiring line turns its test red
(test_boot_reports_the_pull_state_before_a_fresh_create,
test_restart_re_reports_the_pull_state_of_the_new_container,
test_a_legacy_row_with_the_prefix_parses_to_this_kind).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@obasilakis obasilakis added the ui PR touches the frontend UI — triggers Playwright e2e tests label Oct 20, 2026

@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.

Validated (/validate-pr, lane C). authorize_heartbeat admits scope=system only when the key's bound agent_name == path == trinity-system; users cannot mint system-scope keys (mcp_keys router allowlist), and the only system key is minted for trinity-system then flipped, so no cross-agent widening. Result seam pre-check is equivalent for agent keys. Admin gates untouched. Ran test_3814 + 1816 + 3590 + 293 + 186 locally: 189 passed. Mutation: reverting heartbeat_service turns 5 tests red. Test deletions are #1816 pins replaced by piloted/unpiloted counterparts. Known effect (unpiloted pre-#1816 container now heartbeats OK) is documented and acceptable. Tier 1 + journey-smoke/e2e green; dev-ci green. LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ui PR touches the frontend UI — triggers Playwright e2e tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants