From 2c5b0882b677e732e782ff2964a9fd7a351f04a0 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Thu, 12 Nov 2026 16:16:07 +0100 Subject: [PATCH 1/2] test(agent): add startup composition acceptance --- skills/agent-startup-composition/SKILL.md | 38 ++++++ .../agent-startup-composition/acceptance.sh | 124 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 skills/agent-startup-composition/SKILL.md create mode 100755 skills/agent-startup-composition/acceptance.sh diff --git a/skills/agent-startup-composition/SKILL.md b/skills/agent-startup-composition/SKILL.md new file mode 100644 index 0000000..bd8285b --- /dev/null +++ b/skills/agent-startup-composition/SKILL.md @@ -0,0 +1,38 @@ +--- +name: agent-startup-composition +description: Validate Heimdall Agent dependency-injection composition by starting its source-built container without a Central dependency. +--- + +# Agent startup composition + +`src/Heimdall.Agent/Program.cs` constructs `SnapshotPipelineServiceOptions` in +the service-registration factory before registering the hosted service. Do not +register `SnapshotPipelineService` with the generic `AddHostedService()` +unless every constructor dependency, including that options record, is +registered in DI. A missing dependency can prevent `Host.StartAsync` and leave +the Agent container unable to start. + +## Acceptance + +Run `bash skills/agent-startup-composition/acceptance.sh` from the repository +root. The recipe builds only the CPU Agent image from the current checkout with +a unique local tag and starts it in a disposable Compose project with generated +site-token data and an internal-only network. Its Central URL points to the +Agent container's own unused loopback port; it cannot reach a deployment. + +The recipe proves that: + +1. The Agent host starts and remains running with its production service + registrations, including `SnapshotPipelineService`. +2. `/health/live` responds successfully before registration with Central; the + readiness endpoint `/healthz` is intentionally not used because it reports + Unhealthy until Central registration succeeds. +3. Startup logs do not contain the missing + `SnapshotPipelineServiceOptions` dependency or a host-start failure. +4. Cleanup removes the exact Compose project, generated state volume/network, + and this recipe's one exact Agent image tag, then verifies their absence. + +This checks DI and host startup only. It does not validate Central registration, +camera discovery, model inference, a published Agent, or a deployed installation. +The EXIT trap preserves the test result, attempts each cleanup independently, +and reports cleanup failures. The Dockerfile's shared .NET base images remain. diff --git a/skills/agent-startup-composition/acceptance.sh b/skills/agent-startup-composition/acceptance.sh new file mode 100755 index 0000000..f03eb7a --- /dev/null +++ b/skills/agent-startup-composition/acceptance.sh @@ -0,0 +1,124 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +repo_root=$(git rev-parse --show-toplevel) +project="heimdall-agent-startup-$(date -u +%Y%m%d%H%M%S)-$(openssl rand -hex 3)" +image_tag="heimdall-agent:${project}-agent" +temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/${project}.XXXXXX") +env_file="$temp_dir/acceptance.env" +compose_file="$temp_dir/compose.yaml" +local_image_tags=("$image_tag") + +cleanup() { + status=$? + trap - EXIT + cleanup_failed=0 + + docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \ + down --volumes --remove-orphans >/dev/null 2>&1 || cleanup_failed=1 + + containers=$(docker ps -aq --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 + volumes=$(docker volume ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 + networks=$(docker network ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 + [[ -z "$containers" && -z "$volumes" && -z "$networks" ]] || cleanup_failed=1 + + for tag in "${local_image_tags[@]}"; do + docker image rm "$tag" >/dev/null 2>&1 || true + if docker image inspect "$tag" >/dev/null 2>&1; then + cleanup_failed=1 + fi + done + + rm -f -- "$env_file" "$compose_file" || cleanup_failed=1 + rmdir -- "$temp_dir" || cleanup_failed=1 + + if (( cleanup_failed )); then + printf 'Disposable Agent startup cleanup failed for project %s\n' "$project" >&2 + status=1 + else + printf 'Cleanup verified: project=%s; containers/volumes/networks absent; image tag %s absent.\n' \ + "$project" "$image_tag" + fi + + exit "$status" +} +trap cleanup EXIT + +umask 077 +cat >"$env_file" <"$compose_file" <<'EOF' +services: + agent: + image: ${IMAGE_TAG} + build: + context: ${SOURCE_ROOT} + dockerfile: docker/Dockerfile.agent + network: host + restart: "no" + environment: + Agent__CentralUrl: http://127.0.0.1:1 + Agent__AllowInsecureHttp: "true" + Agent__SiteToken: ${SYNTHETIC_SITE_TOKEN} + Agent__AgentName: ${PROJECT} + Agent__RegistrationMaxRetries: "20" + Agent__HeartbeatIntervalSeconds: "5" + Agent__DiscoveryEnabled: "false" + Agent__UpdateCheckIntervalHours: "0" + Agent__StateDirectory: /var/lib/heimdall + Sentry__Dsn: "" + volumes: + - agent_state:/var/lib/heimdall + networks: + - isolated + +volumes: + agent_state: + +networks: + isolated: + internal: true +EOF + +docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \ + up --build --detach agent + +container_id=$(docker compose -p "$project" --env-file "$env_file" -f "$compose_file" ps -q agent) +[[ -n "$container_id" ]] || { printf 'Agent container was not created.\n' >&2; exit 1; } + +deadline=$((SECONDS + 90)) +live_ready=0 +while (( SECONDS < deadline )); do + running=$(docker inspect --format '{{.State.Running}}' "$container_id" 2>/dev/null || true) + if [[ "$running" != true ]]; then + printf 'Agent container exited before its live endpoint became ready.\n' >&2 + docker compose -p "$project" --env-file "$env_file" -f "$compose_file" ps >&2 || true + exit 1 + fi + + if docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \ + exec -T agent wget -q --tries=1 --spider http://127.0.0.1:8080/health/live >/dev/null 2>&1; then + live_ready=1 + break + fi + sleep 1 +done + +(( live_ready == 1 )) || { printf 'Agent /health/live did not become ready.\n' >&2; exit 1; } + +restart_count=$(docker inspect --format '{{.RestartCount}}' "$container_id") +[[ "$restart_count" == 0 ]] || { printf 'Agent container restarted unexpectedly.\n' >&2; exit 1; } + +if docker compose -p "$project" --env-file "$env_file" -f "$compose_file" logs agent 2>&1 | + rg -q "Unable to resolve service for type 'Heimdall\.Agent\.Services\.SnapshotPipelineServiceOptions'|Hosting failed to start"; then + printf 'Agent startup log contains a dependency-injection or host-start failure.\n' >&2 + exit 1 +fi + +printf 'Agent host started; /health/live returned success; container remained running without restart.\n' +printf 'Project: %s\nLocal image tag: %s\n' "$project" "$image_tag" -- 2.54.1 From e1c2a27d0a93ef96d782dc8fdb0ad345be3e4fd7 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Thu, 12 Nov 2026 16:20:29 +0100 Subject: [PATCH 2/2] fix(test): keep startup cleanup running after Docker query failures --- skills/agent-startup-composition/acceptance.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/skills/agent-startup-composition/acceptance.sh b/skills/agent-startup-composition/acceptance.sh index f03eb7a..820b702 100755 --- a/skills/agent-startup-composition/acceptance.sh +++ b/skills/agent-startup-composition/acceptance.sh @@ -17,10 +17,13 @@ cleanup() { docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \ down --volumes --remove-orphans >/dev/null 2>&1 || cleanup_failed=1 + containers= + volumes= + networks= containers=$(docker ps -aq --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 volumes=$(docker volume ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 networks=$(docker network ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 - [[ -z "$containers" && -z "$volumes" && -z "$networks" ]] || cleanup_failed=1 + [[ -z "${containers:-}" && -z "${volumes:-}" && -z "${networks:-}" ]] || cleanup_failed=1 for tag in "${local_image_tags[@]}"; do docker image rm "$tag" >/dev/null 2>&1 || true -- 2.54.1