Repository navigation
fix(agents): rebuilt base image never picked up — adopt on cold start via ninth recreate predicate (#1809) - #1820
Conversation
needs_recreation checked 8 config surfaces but never the image, so after build-base-image.sh a plain stop/start silently kept every agent on the old image — the v0.8.5 upgrade instruction described a mechanism that did not exist (reproduced live: tag moved, stop/start, same container). - ninth predicate check_base_image_matches (helpers.py): the container's resolved image id vs what its OWN Config.Image reference resolves to now; fail-open on every unreadable state (WARNING-logged), INFO on drift; async via new docker_utils.image_get (event-loop safety) - wired lazily in start_agent_internal, gated to COLD starts only (start-on-running stays an idempotent no-op — MCP ensure-running, auto-switch restart, restart_system) and skipped for ephemeral ghosts - recreate: refresh trinity.base-image-version label from the adopted image (UI reported the old version while running the new one); tolerate the concurrent-start race (remove NotFound -> proceed, run 409 -> adopt the winner's container) - surface recreated/recreate_reason in the start response + audit event - docs: 0.8.5 release-note correction (fleet restart does NOT adopt a rebuilt image), architecture + feature-flow updates, learnings entry, cso diff report (0 findings) - tests: 19 new (predicate matrix + wiring source-pins); 4 sibling harnesses gain the new import names; registry entry. Full unit suite matches the merge-base failure-profile exactly (4931 passed) - follow-ups filed: trinity-enterprise#252 (drain-aware rolling fleet upgrade), #1816 (trinity-system ensure_deployed), #1817 (start lock) Fixes #1809 Co-Authored-By: Claude Fable 5 <[email protected]>
…md append conflict (keep both entries, chronological order) Co-Authored-By: Claude Fable 5 <[email protected]>
AndriiPasternak31
left a comment
There was a problem hiding this comment.
/validate-pr — APPROVE (no blockers)
CI 17/17 green (6 pytest runs across base+head × 3 seeds, regression-diff, prod-image-smoke, CodeQL, gitleaks, schema-parity). Security greps clean; no new top-level backend module, no new os.getenv, no infra changes. Fixes #1809 is same-repo, so the issue auto-promotes to status-in-dev on merge.
Verified against source rather than the diff alone: import docker is present (lifecycle.py:11), so the new except docker.errors.* clauses resolve; image is assigned at :440 and passed through validate_base_image (SEC-172) before the new label-refresh reads it; was_already_running was already computed at :285 ahead of the predicate block; and the recreate re-bakes CLAUDE_CODE_OAUTH_TOKEN from the DB at :479, so the now-more-frequent recreates do not regress the #1089 hot-rotated token when the writable layer is wiped. Mechanism is sound — fail-open throughout, lazily evaluated, correctly gated.
Merging now. Four non-blocking findings for follow-up:
1. The comment justifying the cold-start gate names two paths that are themselves cold starts. lifecycle.py:300-304 (and the PR body) cite "the SUB-003 auto-switch restart, restart_system" as start-on-running no-ops. Both stop first — subscription_auto_switch.py:310-311 and routers/systems.py:236-239 each do container_stop → start_agent_internal. So after any build-base-image.sh, a restart_system call and a SUB-003 fallback restart will image-recreate. The outcome is benign (the recreate re-bakes env from the DB, which is what auto-switch wants) and the gate still protects the genuine case — the start endpoint hit on a running agent — but the stated blast radius is understated and the comment will mislead the next reader. The release note's separate claim about the Operating Room fleet restart (routers/ops.py, raw stop+start) not adopting is accurate and distinct.
2. The ephemeral guard fails toward recreating a ghost. lifecycle.py:311-315 — if db.get_agent_ephemeral_info raises, _eph_gate = None, the is_ephemeral test is false, and the image check proceeds, potentially recreating a volume-less ghost and destroying its workspace mid-budget. That inverts the fail-open philosophy check_base_image_matches's own docstring states ("any unreadable state returns True"). Low probability and enterprise-gated, but the safe direction is one line.
3. Feature-flows index not updated. agent-lifecycle.md and async-docker-operations.md both changed, but docs/memory/feature-flows.md gets no Recent Updates row — the convention every recent entry follows.
4. Wiring coverage is source-pin-only. The 10 predicate tests are genuinely good (drift, all three fail-open branches, the ID-pinned tautology, version-pinned tag isolation). But nothing drives start_agent_internal with a drifted container to assert it recreates with recreate_reason == "image_drift" — the regexes would still pass if the block became unreachable. test_start_agent_skip_inject.py already has the harness and only got a default-True stub.
Trivial: tests/registry.json lost its trailing newline.
Summary
needs_recreationnever checked the image, so afterbuild-base-image.sha plain stop/start silently kept every agent on the old image — the v0.8.5 release-note upgrade step described a mechanism that did not exist (reproduced live before the fix; issue bug: a rebuilt agent base image is never picked up — no image-drift predicate, so the v0.8.5 "stop/start-with-recreate" upgrade step is a no-op #1809 has the full repro).check_base_image_matchescompares the container's resolved image ID against what its ownConfig.Imagereference resolves to now. Fail-open on every unreadable state (WARNING-logged;ImageNotFoundwould fail the recreate identically), INFO log on drift. Async via a newdocker_utils.image_getexecutor wrapper — the first predicate doing a daemon round-trip, evaluated lazily (only when no other predicate fired) and only for cold starts: start-on-running stays an idempotent no-op (MCP ensure-running calls, the SUB-003 auto-switch restart,restart_systemall start running agents), and ephemeral ghosts are excluded (volume-less — a recreate would destroy their workspace).container_removetoleratesNotFoundand a 409 name-conflict adopts the concurrent winner's container; thetrinity.base-image-versionlabel is refreshed from the adopted image (both AgentStatus readers prefer the container label — the UI otherwise reports the old version while running the new image).recreated+recreate_reason(config_drift|image_drift) surfaced in the start response and theagent_lifecycle:startaudit event.Changes
src/backend/services/docker_utils.py— asyncimage_getwrappersrc/backend/services/agent_service/helpers.py—check_base_image_matchessrc/backend/services/agent_service/lifecycle.py— lazy cold-start wiring, ephemeral guard, label refresh, race tolerance, response surfacingsrc/backend/routers/agents.py— audit details + response fieldstests/unit/test_1809_image_drift_recreate.py— 19 tests (predicate matrix + wiring source-pins)tests/unit/{test_start_agent_skip_inject,test_agent_readiness_probe,test_subscription_auto_switch_no_cred_import,test_monitoring_router_signatures}.py— enumerated import-stubs gain the new namesdocs/…— release-note correction, architecture, 2 feature flows, learnings, CSO diff report (0 findings)Test Plan
pytest tests/unit/test_1809_image_drift_recreate.py(19 tests)Europe/Kievtzdata case — fails identically ate9eb40da)reason: image_drift, version label refreshed, workspace volume intact; (3) stop/start with matching image → no churn; (4) tag restored → adopts back/reviewclean (2 informational, fixed) ·/cso --diff— 0 findings (report indocs/security-reports/)Follow-ups (filed)
trinity-systemnever adopts (ensure_deployed short-circuit)Fixes #1809
🤖 Generated with Claude Code