Skip to content

[M1.6b1] Collect and transport managed-container state reports - #172

Merged
samuelmbabhazi merged 3 commits into
mainfrom
feat/m1.6b1-agent-state-reports
Nov 7, 2026
Merged

samuelmbabhazi merged 3 commits into
mainfrom
feat/m1.6b1-agent-state-reports

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

[M1.6b1] Agent StateReport collection and transport, split from M1.6b to keep each PR under 500 changed lines. Daemon activation and control-plane persistence belong to M1.6b2, pending approval of a separate allocation observed-state model; the existing placed/stopped reservation invariant is untouched.

What changed / why

Add a bounded, read-only Docker inventory of managed containers, including stopped ones. Snapshots exclude arbitrary labels and credentials, validate identities/states, reject duplicates and inventories above 256 containers, and never truncate a failed or oversized observation into a misleading partial snapshot.

The gateway runner can sample changes every second, refresh every 30 seconds and send a fresh snapshot after Welcome. Collection is independent of heartbeats, carries the session epoch and observation time, and stops with the stream. The daemon does not enable this callback until the receiver exists, preserving compatibility with the current control plane. No protocol/action-catalog or data-model changes.

Validation

Passed locally: pnpm nx run-many -t lint typecheck test build; go test -race ./agent/... ./cli/... ./lib/...; golangci-lint run ./agent/... ./cli/... ./lib/...; pnpm gen with identical tracked diff before/after; pnpm exec prettier --check ..

Tests cover stopped/removed containers, malformed and oversized inventories, private-label redaction, collection failure, periodic/changed reports, actual TLS/gRPC transmission and cancellation. Docker acceptance created a disposable network-isolated, read-only Ubuntu container, verified running/exited/removed observations and removed it. Reproduce with a local pinned image containing /usr/bin/sleep:

SHIPKEEP_DOCKER_TEST_IMAGE=ubuntu@sha256:fe81560990fae86dc4743202b2cb4d3df98d0bcf949173cecef15ce1127ef852 go test -race ./agent/internal/reporting -run TestDockerReportsOperatorStoppedAndRemovedContainer

The existing real-Go-daemon/PostgreSQL enrollment, reconnect and revocation integration test also passed. Full unrelated integration failures previously reproduced on main remain outside this PR. CI remains off. No VM acceptance is claimed.

Security

No shell execution, new dependency, external service, action or persistence mutation. Only the management label leaves the Docker inventory. Collection failures log a fixed message. One writer owns the gRPC stream; session cancellation owns and joins its reporter. State-changing workload operations are unchanged.

Checklist

  • M1.6b1 implemented and tested; parent M1.6 remains incomplete
  • Local required checks and relevant Docker integration passed
  • Malicious-input and race tests added
  • SPEC and PLAN updated
  • M1.6b2 receiver, audited observations and daemon activation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

Review: changes requested. One blocking fix, one small fix.

The structure is good: one stream writer, a reporter owned and joined by the session, no partial snapshots, and only sk.managed leaves the node. Verified on this branch: nx, go test -race, golangci-lint, no drift, Semgrep, Prettier.

Blocking: Docker's removing state blinds the whole node

Snapshot maps six states, but Docker also reports removing. It is missing from the map, becomes STATE_UNSPECIFIED, and fails the entire inventory.

I confirmed this with a throwaway test: one running container plus one removing container gives report=nil, err=invalid or oversized managed container inventory.

Every removal therefore suppresses reports for all containers, and a container stuck in removing (a known Docker failure mode) suppresses them indefinitely. The control plane would keep acting on stale state, with only a generic warning on the node.

Fix: treat removing as absent (continue), since the proto has no such state and the container is going away. Add a test with a healthy container next to a removing one. No proto change is needed.

Should fix: one warning per second while Docker is down

reportStates logs state report collection failed on every failed sample, so a Docker outage writes 3,600 lines per hour. Log when collection starts failing and when it recovers.

Non-blocking

/containers/json every second on each node is a steady load. The Docker events API could replace the sampling later, keeping the 30 s refresh as a safety net.

Open decision

M1.6b2 needs the owner's approval for the observed-state model. That is tracked in the review summary, not in this PR.

review added 2 commits November 7, 2026 12:43
A container in Docker's removing state failed the whole inventory, so
one stuck removal hid every other container. It now counts as absent.
Collection failures log once when they start and once when they end
instead of every second.

Signed-off-by: review <[email protected]>
@samuelmbabhazi
samuelmbabhazi merged commit a57087f into main Nov 7, 2026
2 of 11 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m1.6b1-agent-state-reports branch November 7, 2026 10:45
samuelmbabhazi added a commit that referenced this pull request Nov 7, 2026
## What changed

M1.6b2: agents now send their state reports, and the control plane
records what they observe. The owner approved the separate
observed-state model on 2026-11-07.

- **`allocation_observations` table:** one row per allocation with
container id, observed state and time, under forced tenant RLS. It is
separate from `allocations.state`, so an observation never frees or
takes scheduler capacity. `assertTenantDatabaseRole` now covers 17
tables.
- **`observationChanges` (domain):**
  - containers matching no replica of the node are ignored;
  - a seen container now absent reads as `missing`;
- a replica never seen stays unobserved, because its container may not
exist yet;
- an unchanged state is refreshed at most once a minute, and only real
changes are audited.
- **`AllocationObservations`** handles a report in one tenant
transaction, using the node identity from its certificate. A report with
any invalid container (name, id, state, duplicate, over 256 containers)
is ignored whole, keeping the last good state. Changes are audited as
`allocation.observed`, with the node as actor.
- **`NodeStream`** refuses a report with a stale epoch. It hands valid
ones to `StateReports`, a small fleet registry, because deployments
depends on fleet and fleet cannot import it back.
- **Agent daemon:** the reporter from #172 is now switched on.

## How it was tested

- Domain and service unit tests: first sighting, changes, missing versus
never seen, the refresh boundary, foreign containers, malicious reports
(traversal name, short id, unknown state, duplicates, 257 containers),
registry delivery.
- Stream test: a state report reaches the registry.
- Full local gate passes: 287 API tests, Go race tests, lint, drift,
Semgrep, Prettier.
- **Not run:** the new `allocation-observations.integration.spec.ts`
(another node's report touches nothing, audit sequence, reservation
unchanged), because Docker is not reachable from this environment. It
needs a run on a machine with Docker.

## Security considerations

- A node can only affect its own placed allocations.
- Reports hold no labels other than `sk.managed`, no environment and no
commands.
- Audit volume: a container in a restart loop produces one event per
transition. This is bounded by the agent's 1 Hz sampling, and worth
revisiting if it proves noisy.

---------

Signed-off-by: review <[email protected]>
Co-authored-by: review <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant