Refs #157 Reconcile camera config changes immediately and strengthen the disposable toggle acceptance evidence. Co-authored-by: Arnaud Dartois <admin@tordais.cc>
11 KiB
name, description
| name | description |
|---|---|
| camera-toggle-acceptance | Validate that Central camera enable/disable changes stop and resume the real Agent FFmpeg capture process and persist across Agent restarts. |
Camera enable/disable lifecycle
Read AGENTS.md, the Toggle caméra #59/#199 Gitea issue, skills/heimdall-dotnet/SKILL.md, and skills/compose-disposable-recipe/SKILL.md before changing this flow.
Implementation path
POST /api/cameras/{id}/toggle-statusrequires the normal camera-write permission and persistsCamera.Statusin Central. Enabling a Disabled camera sets it to Offline (enabled, connectivity not yet confirmed); only an Agent status report can set it Online. Toggling any enabled status sets it to Disabled.- Agent status reports require the API key to resolve to the same Agent ID in the route and the camera to be assigned to that Agent. Authenticate and authorize both resources with
ResourceAccess; never trust the route ID alone. Keep the existing same-site camera check inReportCameraStatusCommandHandler. AppDbContext.SaveChangesAsyncpublishes a payload-free config invalidation after camera/assignment/profile changes. Agent refetches its authorized full camera snapshot over its ConfigHub/API path and acknowledges the digest.GetEffectiveAgentConfigQueryemitscamera.<id>.enabledfrom the persisted status.CameraConfigStoreatomically replaces the snapshot and raisesChanged.SnapshotPipelineServicereconciles enabled assignments withCameraStreamService; disabling/removing an assignment kills FFmpeg, awaits its read loop cleanup and releases its stream permit. Enabling starts the stream. Keep central status authoritative; an Agent status report must not re-enable a disabled camera.SnapshotPipelineServicereconciles configuration onCameraConfigStore.Changed, while retaining a one-second config-change poll and the existing 30-second full resync as recovery. Coalesce notifications and ensure there is only one waiter on the config signal.- Camera counts and lists consider only Online cameras connected. An enabled Offline/Error camera remains enabled in the Agent configuration but is not counted as Online. UI toggle messages treat every status except Disabled as enabled.
- Agent identity and API key are protected in
AgentStateunder the configured state directory. Mount a project-scoped volume there so a restart reuses the same Agent identity and the Central configuration is refetched from persistent camera state.
Local verification
The Central status contract is covered by ToggleCameraStatusCommandTests and the CamerasControllerTests.ToggleStatus_WithAuth_TogglesCamera relational API test: activation persists Offline, the authorized same-site Agent status endpoint can confirm Online, and a subsequent administrative toggle persists Disabled. Run these focused tests alongside the Agent lifecycle suites when changing this contract.
Run the focused Agent lifecycle suites:
dotnet test tests/Heimdall.Agent.Tests/Heimdall.Agent.Tests.csproj --filter 'FullyQualifiedName~CameraStreamServiceTests|FullyQualifiedName~SnapshotPipelineServiceTests' --no-restore
These tests cover session idempotence, process termination, and synchronization without claiming a full Central/Agent workflow.
Central + Agent disposable acceptance
Run skills/camera-toggle-acceptance/acceptance.sh from the repository root. It builds both application images from source, bootstraps synthetic Central/site/camera data, registers the real Agent container, repeats three ON/OFF cycles, checks the OFF PID bound, restarts Agent and Central while OFF, correlates the final applied config digest, and verifies exact project/tag cleanup in its EXIT trap.
- Build Central and Agent from this checkout with unique tags recorded before the first build. Retain upstream Postgres, MediaMTX, FFmpeg, SDK and ASP.NET base images.
- Use a fresh project-scoped database, JWT/site credentials, loopback-only Central/Postgres ports, network and volumes. Add a project network Agent and a synthetic MediaMTX RTSP publisher. Do not use host networking or real camera credentials.
- Bootstrap a synthetic Admin/site, create a camera pointed at the synthetic RTSP path, register the actual Agent container with the generated site token, and assign the camera to that Agent.
- Toggle ON and confirm an Agent
ffmpegpull process exists against the synthetic path. Toggle OFF and verify the API says Disabled, Agent reports no assigned stream via its active process count and the FFmpeg process disappears in under 2 seconds. Toggle ON and verify the process returns within 10 seconds. - Restart the Agent while ON; verify it reuses its single existing identity and restarts capture within 10 seconds. Then toggle OFF, restart again, and verify the camera remains Disabled and no capture process returns.
- Use
docker toponly to count FFmpeg processes; never print its command lines because they contain the RTSP URL. Record elapsed durations and sanitized HTTP statuses only. - Install an EXIT cleanup trap before the first build/start. Teardown only the named Compose project, verify zero containers/volumes/networks, remove each exact project-unique Central/Agent image tag and verify absent, then delete the temporary environment and scripts. Keep the original test status and fail if cleanup verification fails.
This acceptance proves the source-built Central/Agent software lifecycle with a synthetic RTSP stream. It does not validate hardware codecs, physical cameras, LAN multicast, or deployed-host timing.
Observed simulator pitfalls
- Do not count
docker topoutput when the runtime'stopimplementation differs. Prefer an in-container process identity check (/proc/*/commequalsffmpeg) and ensure the inspection process cannot match itself. A shell command line containing the textffmpegcreates a false positive if the check scans all command lines. - Before asserting lifecycle timing, first confirm Agent
Online, persisted camera assignment, effective config enabled value and version, andLastAppliedCameraConfigVersionacknowledgement. A failed precondition is not an OFF/ON timing result. - On failed acceptance, collect sanitized Central, Agent, RTSP server and publisher diagnostics before the cleanup trap runs. Then verify project resource counts and exact image tags as described above.
- Earlier disposable attempts proved bootstrap, registration, assignment and Disabled config acknowledgement, but did not prove toggle timing or restart persistence. The completed source-built run below supersedes those incomplete attempts; physical camera and deployed-host validation remain external.
Latest source-built run
The fifth disposable Compose attempt used /proc/*/comm == ffmpeg, avoiding self-counting by the measurement shell. It confirmed the Disabled baseline at zero processes and progressed through ON, OFF, ON, then Agent restart while ON with capture restored. The next OFF returned Central state Disabled and effective Agent config enabled=false, with zero FFmpeg processes, but the latest config digest was not acknowledged inside the two-second OFF bound. Therefore the full OFF/ON/restart acceptance is still incomplete; do not mark #59/#199 complete. Investigate whether Agent ACK version is stale or the API config digest changes after toggle, while keeping the OFF process release measurement distinct from ACK convergence.
Sixth source-built run
The sixth historical run kept the FFmpeg process deadline separate from the config ACK deadline and verified exact initial digest equality between the Central config version and Agent LastAppliedCameraConfigVersion. It failed the OFF process bound and did not run restart-OFF acceptance. The subsequent 2026-11-12 acceptance below supersedes that incomplete result for current source.
Immediate config reconciliation regression test
SnapshotPipelineServiceTests.CameraConfigChanged_ReconcilesWithoutWaitingForPeriodicSync starts the pipeline, lets its initial synchronization complete, then changes the Central snapshot and requires the assigned camera's Error report (FFmpeg deliberately unavailable) within 500ms. This proves event-driven reconciliation ahead of the one-second config-change poll and 30-second full resync; it does not prove process termination or the full camera lifecycle. Run it with the focused camera stream suite before Compose acceptance.
Source-built acceptance — 2026-11-12
acceptance.sh passed with project heimdall-157-1794488199-31533, Central tag heimdall:heimdall-157-1794488199-31533-central, and Agent tag heimdall-agent:heimdall-157-1794488199-31533-agent. Central and PostgreSQL published only on 127.0.0.1. After confirming the ON config digest ACK, it exercised three ON/OFF cycles with FFmpeg release at 1,386ms, 555ms, and 410ms, then a final OFF at 810ms before restart. Each transition verified the exact Central config digest ACK and expected camera.<id>.enabled value. The process monitor rejected counts above one. After Agent restart while OFF, the same Agent reported Online with a heartbeat and config ACK timestamp newer than the Compose restart command's return; after Central restart it again reported Online with a fresh ACK for the persisted Disabled digest newer than the restart command's return. Agent restart while ON preserved its identity and resumed capture. Cleanup verified zero project containers, volumes, and networks and absence of both exact local image tags. This is synthetic source-built software evidence, not camera hardware or deployed-host timing.
Camera status contract acceptance
For status semantics (issue #291), run CamerasControllerTests.ToggleStatus_WithAuth_TogglesCamera against PostgreSQL and the ToggleCameraStatusCommandTests suite. The relational test must assert Disabled → Offline and the online counter remains zero before connectivity, register and heartbeat a synthetic same-site Agent, reject its report before camera assignment, assign the synthetic camera, accept that Agent's Online report, reject a request that uses its API key with a different route Agent ID, and then confirm admin toggle → Disabled.
A source-built disposable Central + PostgreSQL Compose run on 2026-11-11 used project heimdall-291-acceptance-1794427561-8428, synthetic bootstrap/site/Agent/camera data, loopback ports 18543/15439, and image heimdall:heimdall-291-acceptance-1794427561-8428-central. It observed health HTTP 200, activation Offline, online count 0, then Agent-confirmed Online and online count 1. Cleanup verified zero project containers, volumes and networks, the exact image tag absent, and temporary files removed. This is software acceptance only; it does not satisfy validation:field hardware/deployment evidence.