Skip to content

fix(agents): rebuilt base image never picked up — adopt on cold start via ninth recreate predicate (#1809) - #1820

Merged
AndriiPasternak31 merged 2 commits into
devfrom
feature/1809-image-drift-recreate
Jul 27, 2026
Merged

AndriiPasternak31 merged 2 commits into
devfrom
feature/1809-image-drift-recreate

Conversation

@webmixgamer

Copy link
Copy Markdown
Contributor

Summary

  • needs_recreation never checked the image, so after build-base-image.sh a 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).
  • Adds a ninth, cold-start-gated predicate: check_base_image_matches compares the container's resolved image ID against what its own Config.Image reference resolves to now. Fail-open on every unreadable state (WARNING-logged; ImageNotFound would fail the recreate identically), INFO log on drift. Async via a new docker_utils.image_get executor 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_system all start running agents), and ephemeral ghosts are excluded (volume-less — a recreate would destroy their workspace).
  • Recreate hardening that the new frequency makes routine: container_remove tolerates NotFound and a 409 name-conflict adopts the concurrent winner's container; the trinity.base-image-version label 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).
  • Observability: recreated + recreate_reason (config_drift | image_drift) surfaced in the start response and the agent_lifecycle:start audit event.
  • Docs: the 0.8.5 release note is corrected as a point-in-time correction and now states explicitly that Operating-Room fleet restart does NOT adopt a rebuilt image; architecture + feature-flow docs updated; learnings-ledger entry added.

Changes

  • src/backend/services/docker_utils.py — async image_get wrapper
  • src/backend/services/agent_service/helpers.py — check_base_image_matches
  • src/backend/services/agent_service/lifecycle.py — lazy cold-start wiring, ephemeral guard, label refresh, race tolerance, response surfacing
  • src/backend/routers/agents.py — audit details + response fields
  • tests/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 names
  • docs/… — release-note correction, architecture, 2 feature flows, learnings, CSO diff report (0 findings)

Test Plan

  • New suite green: pytest tests/unit/test_1809_image_drift_recreate.py (19 tests)
  • Full unit suite matches the merge-base failure-profile exactly: 4931 passed, 1 pre-existing env failure (Europe/Kiev tzdata case — fails identically at e9eb40da)
  • Live e2e on a running stack: (1) start on RUNNING agent with drifted tag → no-op; (2) cold start with drift → recreated, reason: image_drift, version label refreshed, workspace volume intact; (3) stop/start with matching image → no churn; (4) tag restored → adopts back
  • /review clean (2 informational, fixed) · /cso --diff — 0 findings (report in docs/security-reports/)

Follow-ups (filed)

Fixes #1809

🤖 Generated with Claude Code

webmixgamer and others added 2 commits July 27, 2026 14:22
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]>
@webmixgamer
webmixgamer marked this pull request as ready for review July 27, 2026 13:51

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

@AndriiPasternak31
AndriiPasternak31 merged commit 28ce225 into dev Jul 27, 2026
22 checks passed
@webmixgamer
webmixgamer deleted the feature/1809-image-drift-recreate branch July 27, 2026 15:06
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.

2 participants