wf-agent/AGENTS.md

35 KiB

Codex Standalone Step Prompts

Project: Linux/Python Wi-Fi Telemetry & Command Agent

Use this file as a copy-paste source for Codex. Each section below is a standalone prompt for one Step.


Shared project context for every step

Project to implement: Linux/Python application that:

  • ingests data from multiple sources;
  • caches and persists data locally;
  • can send commands to some sources;
  • sends part of data immediately to an external information system;
  • sends part of data in batches on schedule;
  • correlates Wi-Fi events with coordinates.

Sources:

  1. wpa_supplicant over D-Bus
    • read events
    • read state
    • send commands
  2. systemd-journal
    • read wpa_supplicant events
    • read some other process events by config
  3. navigation system
    • read current coordinates

Target architecture:

  • modular monolith
  • Python 3.12+
  • asyncio
  • FastAPI
  • SQLite
  • SQLAlchemy 2.x + asyncpg
  • Pydantic v2
  • Linux + systemd

Global rules:

  1. one Step = one git commit
  2. implement only the current step
  3. do not pre-implement future steps
  4. all incoming data must go through internal envelope -> validation -> normalization
  5. persist first, send later
  6. outbox pattern is mandatory for deliverable entities
  7. latest state and event log must be separate
  8. journal cursor must be stored persistently
  9. commands must go through command service/state machine
  10. adapters must not write directly to external system
  11. API endpoints must not call adapters directly; use services
  12. all timestamps must be UTC-aware
  13. tests must never use production DB
  14. repositories/UoW only for persistence changes
  15. code must be typed
  16. add or update tests in every step

Required response format for Codex:

  1. Briefly list what will be done in this step.
  2. Implement only the current step.
  3. Summarize:
    • changed files
    • added/updated tests
    • any assumptions
    • exact commit message

Repository target layout:

wf-agent/
  README.md
  pyproject.toml
  .gitignore
  .env.example
  .env.test.example
  alembic.ini
  migrations/
  deployment/
  docs/
  app/
  tests/

Step 0.1 — Initialize repository skeleton and engineering baseline

You are implementing **Step 0.1 — Initialize repository skeleton and engineering baseline**.

Goal:
- create the repository skeleton and engineering baseline.

Scope:
- create base directory structure;
- create `README.md`, `pyproject.toml`, `.gitignore`, `.env.example`, `.env.test.example`;
- add empty package scaffolding under `app/`, `tests/`, and `docs/`.

Files to create/update:
- `README.md`
- `pyproject.toml`
- `.gitignore`
- `.env.example`
- `.env.test.example`
- package `__init__.py` files and empty directories according to the target layout

Tests:
- add a minimal smoke test that verifies `app` can be imported.

Definition of Done:
- repository structure exists;
- project installs;
- package imports do not fail.

Commit message:
`chore: initialize repository skeleton and engineering baseline`

Step 0.2 — Add configuration system and environment guardrails

You are implementing **Step 0.2 — Add configuration system and environment guardrails**.

Goal:
- implement typed configuration and guardrails preventing tests from using production-like DB settings.

Scope:
- add settings/config layer;
- load environment variables;
- add `.env.test`-based test protections;
- validate config early.

Files to create/update:
- `app/core/config.py`
- `app/core/constants.py`
- `app/core/exceptions.py`
- `tests/conftest.py`
- `tests/unit/test_config.py`

Tests:
- config validation tests;
- guard-check test that fails on non-test DB DSN.

Definition of Done:
- config validates correctly;
- test runtime aborts on production-like DSN.

Commit message:
`feat: add typed configuration system and environment guardrails`

Step 0.3 — Add logging, clock, IDs, and shared primitives

You are implementing **Step 0.3 — Add logging, clock, IDs, and shared primitives**.

Goal:
- add shared technical primitives used across the project.

Scope:
- structured logging;
- UTC-aware clock abstraction;
- UUID / identifier helpers;
- correlation helpers / value objects.

Files to create/update:
- `app/core/logging.py`
- `app/core/clock.py`
- `app/core/ids.py`
- `app/domain/value_objects.py`

Tests:
- add unit tests for helpers where reasonable.

Definition of Done:
- shared primitives are ready for reuse throughout the codebase.

Commit message:
`feat: add shared primitives for logging time and identifiers`

Step 0.4 — Freeze architecture decisions in ADRs

You are implementing **Step 0.4 — Freeze architecture decisions in ADRs**.

Goal:
- document and freeze key architecture decisions before deeper implementation.

Scope:
- write ADRs for:
  - modular monolith;
  - SQLite as persistent store;
  - outbox pattern;
  - asyncio runtime.

Files to create/update:
- `docs/adr/ADR-001-modular-monolith.md`
- `docs/adr/ADR-002-sqlite-as-persistent-store.md`
- `docs/adr/ADR-003-outbox-pattern.md`
- `docs/adr/ADR-004-asyncio-runtime.md`

Tests:
- none.

Definition of Done:
- ADRs are internally consistent and match the technical design.

Commit message:
`docs: add ADRs for architecture persistence outbox and runtime`

Step 1.1 — Define domain enums, contracts, and canonical models

You are implementing **Step 1.1 — Define domain enums, contracts, and canonical models**.

Goal:
- formalize the domain and canonical entities.

Scope:
- define enums, contracts, models, delivery modes, priorities, statuses.

Files to create/update:
- `app/domain/enums.py`
- `app/domain/contracts.py`
- `app/domain/models.py`
- `app/domain/policies.py`
- `tests/unit/test_domain_contracts.py`

Tests:
- unit tests for domain invariants.

Definition of Done:
- domain model covers state, event, location, command, and outbox concepts.

Commit message:
`feat: define domain enums contracts and canonical models`

Step 1.2 — Add database bootstrap and ORM base models

You are implementing **Step 1.2 — Add database bootstrap and ORM base models**.

Goal:
- prepare the storage foundation.

Scope:
- DB engine/session bootstrap;
- ORM base models;
- unit-of-work skeleton.

Files to create/update:
- `app/storage/db.py`
- `app/storage/models.py`
- `app/storage/unit_of_work.py`
- `tests/integration/test_db_bootstrap.py`

Tests:
- integration test for DB bootstrap.

Definition of Done:
- app connects to test DB through a single bootstrap path.

Commit message:
`feat: add database bootstrap and ORM base models`

Step 1.3 — Add Alembic and initial database schema migration

You are implementing **Step 1.3 — Add Alembic and initial database schema migration**.

Goal:
- create the initial DB schema via migrations.

Scope:
- set up Alembic;
- add initial migration for core tables.

Files to create/update:
- `alembic.ini`
- `migrations/env.py`
- `migrations/versions/*_initial_schema.py`

Core tables:
- `source_registry`
- `source_health`
- `wifi_state_latest`
- `wifi_events`
- `diagnostic_log_events`
- `location_snapshots`
- `commands`
- `derived_events`
- `outbox`
- `journal_cursors`

Tests:
- migration integration test.

Definition of Done:
- schema is created on a clean test DB via migration.

Commit message:
`feat: add alembic and initial database schema migration`

Step 1.4 — Implement repositories for persistent domain entities

You are implementing **Step 1.4 — Implement repositories for persistent domain entities**.

Goal:
- implement repositories over the DB schema.

Scope:
- repositories for state, events, diagnostics, locations, commands, outbox, cursors, and source health.

Files to create/update:
- `app/storage/repositories/*`
- `tests/integration/test_repositories.py`

Tests:
- repository integration tests.

Definition of Done:
- latest state uses upsert semantics;
- event stores are append-only;
- outbox and cursor repositories work.

Commit message:
`feat: implement repositories for persistent domain entities`

Step 2.1 — Add dependency container and application factory

You are implementing **Step 2.1 — Add dependency container and application factory**.

Goal:
- centralize application wiring.

Scope:
- dependency container;
- lifecycle wiring;
- application factory.

Files to create/update:
- `app/bootstrap/dependency_container.py`
- `app/bootstrap/app_factory.py`
- `app/bootstrap/lifecycle.py`

Tests:
- smoke test for container/app factory.

Definition of Done:
- core services are created centrally.

Commit message:
`feat: add dependency container and application factory`

Step 2.2 — Implement internal queues, envelopes, and ingestion dispatcher

You are implementing **Step 2.2 — Implement internal queues, envelopes, and ingestion dispatcher**.

Goal:
- build the ingestion backbone.

Scope:
- internal envelopes;
- typed queues;
- ingestion dispatcher.

Files to create/update:
- `app/ingestion/envelopes.py`
- `app/ingestion/queues.py`
- `app/ingestion/dispatcher.py`
- `tests/unit/test_ingestion_dispatcher.py`

Tests:
- dispatcher unit tests.

Definition of Done:
- dispatcher accepts multiple source types and routes messages internally.

Commit message:
`feat: implement internal queues envelopes and ingestion dispatcher`

Step 2.3 — Implement source manager lifecycle orchestration

You are implementing **Step 2.3 — Implement source manager lifecycle orchestration**.

Goal:
- manage source adapter lifecycle consistently.

Scope:
- register/start/stop;
- enable/disable hooks;
- health hooks.

Files to create/update:
- `app/adapters/base.py`
- `app/ingestion/source_manager.py`
- `tests/unit/test_source_manager.py`

Tests:
- source manager unit tests.

Definition of Done:
- multiple sources are managed uniformly.

Commit message:
`feat: implement source manager lifecycle orchestration`

Step 3.1 — Implement source envelope validator

You are implementing **Step 3.1 — Implement source envelope validator**.

Goal:
- validate raw envelopes before normalization.

Scope:
- required field checks;
- schema checks;
- controlled validation failures.

Files to create/update:
- `app/normalization/validator.py`
- `tests/unit/test_validator.py`

Tests:
- validator unit tests.

Definition of Done:
- invalid envelopes do not proceed downstream.

Commit message:
`feat: implement source envelope validator`

Step 3.2 — Implement canonical mapper for normalized entities

You are implementing **Step 3.2 — Implement canonical mapper for normalized entities**.

Goal:
- map validated raw envelopes into canonical entities.

Scope:
- source-independent mapping for state, event, location, diagnostic entities.

Files to create/update:
- `app/normalization/mapper.py`
- `tests/unit/test_canonical_mapper.py`

Tests:
- canonical mapper unit tests.

Definition of Done:
- all main source shapes map into canonical forms.

Commit message:
`feat: implement canonical mapper for normalized entities`

Step 3.3 — Implement delivery classifier and priority policies

You are implementing **Step 3.3 — Implement delivery classifier and priority policies**.

Goal:
- assign delivery mode and priority.

Scope:
- immediate/batch/both/none classification;
- priority rules.

Files to create/update:
- `app/normalization/classifier.py`
- `tests/unit/test_classifier.py`
- `docs/architecture/delivery-policies.md`

Tests:
- classifier unit tests.

Definition of Done:
- critical events and command results are classified correctly.

Commit message:
`feat: implement delivery classifier and priority policies`

Step 3.4 — Implement deduplication primitives and idempotency helpers

You are implementing **Step 3.4 — Implement deduplication primitives and idempotency helpers**.

Goal:
- provide basic dedup/idempotency foundation.

Scope:
- dedup keys;
- idempotency helpers;
- bucketed rules.

Files to create/update:
- `app/normalization/dedup.py`
- `tests/unit/test_dedup.py`

Tests:
- dedup unit tests.

Definition of Done:
- dedup primitives are ready for correlation and delivery work.

Commit message:
`feat: implement deduplication primitives and idempotency helpers`

Step 4.1 — Implement canonical ingestion service with persist-first flow

You are implementing **Step 4.1 — Implement canonical ingestion service with persist-first flow**.

Goal:
- assemble the central processing flow.

Scope:
- normalize -> classify -> persist orchestration.

Files to create/update:
- `app/services/canonical_ingestion_service.py`
- `app/services/state_service.py`
- `app/services/event_service.py`
- `tests/e2e/test_ingest_normalize_persist_flow.py`

Tests:
- end-to-end ingest-normalize-persist test.

Definition of Done:
- state, event, location, and diagnostic entities reach persistence.

Commit message:
`feat: implement canonical ingestion service with persist first flow`

Step 4.2 — Implement transactional outbox creation for deliverable entities

You are implementing **Step 4.2 — Implement transactional outbox creation for deliverable entities**.

Goal:
- create outbox records automatically for deliverable entities.

Scope:
- outbox service;
- transactional wiring with persistence writes.

Files to create/update:
- `app/services/outbox_service.py`
- `tests/integration/test_outbox_flow.py`

Tests:
- outbox integration tests.

Definition of Done:
- deliverable entities are atomically persisted and added to outbox.

Commit message:
`feat: implement transactional outbox creation for deliverable entities`

Step 4.3 — Add replay and retry service abstractions

You are implementing **Step 4.3 — Add replay and retry service abstractions**.

Goal:
- prepare replay/retry abstractions for operations.

Scope:
- replay service abstractions;
- failed/pending outbox replay interfaces.

Files to create/update:
- `app/services/replay_service.py`
- `tests/unit/test_replay_service.py`

Tests:
- replay service unit tests.

Definition of Done:
- replay/retry abstractions exist and are test-covered.

Commit message:
`feat: add replay and retry service abstractions`

Step 5.1 — Implement navigation adapter with fake client for local flow

You are implementing **Step 5.1 — Implement navigation adapter with fake client for local flow**.

Goal:
- implement the first working source adapter.

Scope:
- navigation adapter;
- fake client;
- parser/mapper;
- raw envelope publishing.

Files to create/update:
- `app/adapters/navigation/client.py`
- `app/adapters/navigation/adapter.py`
- `app/adapters/navigation/parser.py`
- `app/adapters/navigation/mapper.py`
- `tests/integration/test_navigation_adapter.py`
- `tests/contract/test_navigation_mapping_contracts.py`

Tests:
- integration and contract tests for navigation mapping.

Definition of Done:
- navigation data goes through the ingestion pipeline.

Commit message:
`feat: implement navigation adapter with fake client for local flow`

Step 5.2 — Persist navigation snapshots and validate freshness semantics

You are implementing **Step 5.2 — Persist navigation snapshots and validate freshness semantics**.

Goal:
- complete the location flow into persistent storage.

Scope:
- persist location snapshots;
- validate freshness/staleness semantics.

Files to create/update:
- `tests/e2e/test_navigation_ingestion_flow.py`

Tests:
- end-to-end navigation ingestion test.

Definition of Done:
- snapshots are stored;
- stale semantics are covered by tests.

Commit message:
`feat: persist navigation snapshots and validate freshness semantics`

Step 6.1 — Implement journal reader abstraction and filter model

You are implementing **Step 6.1 — Implement journal reader abstraction and filter model**.

Goal:
- create a replaceable journald access layer.

Scope:
- journald reader abstraction;
- filter model;
- cursor abstraction.

Files to create/update:
- `app/adapters/journal/reader.py`
- `app/adapters/journal/filters.py`
- `app/adapters/journal/cursor_store.py`
- `tests/unit/test_journal_filters.py`

Tests:
- journal filter unit tests.

Definition of Done:
- journald access is isolated behind an abstraction layer.

Commit message:
`feat: implement journal reader abstraction and filter model`

Step 6.2 — Implement journal adapter with persistent cursor handling

You are implementing **Step 6.2 — Implement journal adapter with persistent cursor handling**.

Goal:
- read journald with restart-safe cursor persistence.

Scope:
- journal adapter;
- cursor load/save;
- raw event publishing.

Files to create/update:
- `app/adapters/journal/adapter.py`
- `app/adapters/journal/parser.py`
- `tests/integration/test_journal_cursor_persistence.py`

Tests:
- cursor persistence integration tests.

Definition of Done:
- journald reading resumes correctly after restart.

Commit message:
`feat: implement journal adapter with persistent cursor handling`

Step 6.3 — Add canonical mapping contracts for journal diagnostic events

You are implementing **Step 6.3 — Add canonical mapping contracts for journal diagnostic events**.

Goal:
- formalize journal diagnostic mapping.

Scope:
- parsed subtype mapping;
- severity mapping;
- contract expectations.

Files to create/update:
- `tests/contract/test_journal_mapping_contracts.py`

Tests:
- contract tests for journal diagnostics mapping.

Definition of Done:
- journal mapping rules are enforced by tests.

Commit message:
`feat: add canonical mapping contracts for journal diagnostic events`

Step 7.1 — Implement dbus client abstraction and signal subscription model

You are implementing **Step 7.1 — Implement dbus client abstraction and signal subscription model**.

Goal:
- create an async D-Bus client layer for `wpa_supplicant`.

Scope:
- client abstraction;
- signal subscription model;
- health hooks.

Files to create/update:
- `app/adapters/dbus_wpa/client.py`
- `app/adapters/dbus_wpa/health.py`
- `tests/unit/test_dbus_client_abstraction.py`

Tests:
- unit tests for D-Bus client abstraction.

Definition of Done:
- D-Bus library details are hidden behind a client abstraction.

Commit message:
`feat: implement dbus client abstraction and signal subscription model`

Step 7.2 — Implement dbus adapter for wpa supplicant event ingestion

You are implementing **Step 7.2 — Implement dbus adapter for wpa supplicant event ingestion**.

Goal:
- read `wpa_supplicant` events and state over D-Bus.

Scope:
- signal handling;
- snapshots;
- raw envelope mapping.

Files to create/update:
- `app/adapters/dbus_wpa/adapter.py`
- `app/adapters/dbus_wpa/parser.py`
- `app/adapters/dbus_wpa/mapper.py`
- `tests/contract/test_dbus_mapping_contracts.py`
- `tests/e2e/test_dbus_ingestion_flow.py`

Tests:
- D-Bus mapping contract tests;
- D-Bus ingestion e2e test.

Definition of Done:
- D-Bus events and snapshots reach the persistence pipeline.

Commit message:
`feat: implement dbus adapter for wpa supplicant event ingestion`

Step 7.3 — Add reconnect and degraded health handling for dbus adapter

You are implementing **Step 7.3 — Add reconnect and degraded health handling for dbus adapter**.

Goal:
- make the D-Bus adapter reconnect-safe and observable.

Scope:
- reconnect policy;
- degraded/disconnected states;
- health reporting.

Files to create/update:
- `tests/integration/test_dbus_reconnect_behavior.py`

Tests:
- integration tests for reconnect behavior.

Definition of Done:
- adapter recovers after temporary D-Bus/source failures.

Commit message:
`feat: add reconnect and degraded health handling for dbus adapter`

Step 8.1 — Implement location matcher for wifi event enrichment

You are implementing **Step 8.1 — Implement location matcher for wifi event enrichment**.

Goal:
- enrich Wi-Fi events with nearest coordinates.

Scope:
- nearest snapshot selection;
- freshness window;
- location age calculation.

Files to create/update:
- `app/correlation/location_matcher.py`
- `tests/unit/test_location_matcher.py`

Tests:
- location matcher unit tests.

Definition of Done:
- Wi-Fi events can be enriched with location context.

Commit message:
`feat: implement location matcher for wifi event enrichment`

Step 8.2 — Implement journal linker for wifi and diagnostic evidence

You are implementing **Step 8.2 — Implement journal linker for wifi and diagnostic evidence**.

Goal:
- correlate D-Bus Wi-Fi events with journald diagnostics.

Scope:
- correlation by time/interface/type;
- primary vs supporting evidence semantics.

Files to create/update:
- `app/correlation/journal_linker.py`
- `tests/unit/test_journal_linker.py`

Tests:
- journal linker unit tests.

Definition of Done:
- D-Bus and journal data can share correlation context.

Commit message:
`feat: implement journal linker for wifi and diagnostic evidence`

Step 8.3 — Implement command linker and command effect correlation

You are implementing **Step 8.3 — Implement command linker and command effect correlation**.

Goal:
- correlate commands with observed effect/result.

Scope:
- command correlation rules;
- expected event windows;
- timeout semantics.

Files to create/update:
- `app/correlation/command_linker.py`
- `tests/unit/test_command_linker.py`

Tests:
- command linker unit tests.

Definition of Done:
- commands can be linked to downstream events and outcomes.

Commit message:
`feat: implement command linker and command effect correlation`

Step 8.4 — Implement derived events generation from correlated signals

You are implementing **Step 8.4 — Implement derived events generation from correlated signals**.

Goal:
- generate higher-level derived domain events.

Scope:
- derived events engine;
- generation rules for correlated signals.

Files to create/update:
- `app/correlation/engine.py`
- `app/correlation/derived_events.py`
- `tests/unit/test_derived_events.py`

Tests:
- derived events unit tests.

Definition of Done:
- at least several derived events are generated and persisted.

Commit message:
`feat: implement derived events generation from correlated signals`

Step 9.1 — Implement command domain service and state machine

You are implementing **Step 9.1 — Implement command domain service and state machine**.

Goal:
- make commands a first-class domain object.

Scope:
- command service;
- command state machine;
- invariant enforcement.

Files to create/update:
- `app/services/command_service.py`
- `app/commands/state_machine.py`
- `tests/unit/test_command_state_machine.py`

Tests:
- command state machine unit tests.

Definition of Done:
- invalid state transitions are prevented.

Commit message:
`feat: implement command domain service and state machine`

Step 9.2 — Implement dbus command executor for wpa supplicant

You are implementing **Step 9.2 — Implement dbus command executor for wpa supplicant**.

Goal:
- execute commands via D-Bus against `wpa_supplicant`.

Scope:
- D-Bus command executor;
- capability checks;
- result mapping.

Files to create/update:
- `app/adapters/dbus_wpa/command_executor.py`
- `app/commands/dispatcher.py`
- `tests/e2e/test_command_execution_flow.py`

Tests:
- end-to-end command execution test.

Definition of Done:
- commands are sent through the command path to `wpa_supplicant`.

Commit message:
`feat: implement dbus command executor for wpa supplicant`

Step 9.3 — Emit command result events into canonical delivery flow

You are implementing **Step 9.3 — Emit command result events into canonical delivery flow**.

Goal:
- feed command results into the common event/outbox flow.

Scope:
- canonical command result events;
- delivery classification for command results.

Files to create/update:
- `tests/integration/test_command_result_outbox_flow.py`

Tests:
- command result outbox integration test.

Definition of Done:
- command results are added to outbox and classified for immediate delivery when appropriate.

Commit message:
`feat: emit command result events into canonical delivery flow`

Step 10.1 — Implement northbound client abstraction and exporter

You are implementing **Step 10.1 — Implement northbound client abstraction and exporter**.

Goal:
- create the abstraction for sending data to the external information system.

Scope:
- northbound client interface;
- exporter;
- payload contract alignment.

Files to create/update:
- `app/delivery/northbound_client.py`
- `app/delivery/exporter.py`
- `tests/contract/test_northbound_payload_contracts.py`

Tests:
- contract tests for northbound payloads.

Definition of Done:
- delivery layer is transport-agnostic.

Commit message:
`feat: implement northbound client abstraction and exporter`

Step 10.2 — Implement immediate delivery worker

You are implementing **Step 10.2 — Implement immediate delivery worker**.

Goal:
- implement the immediate send path.

Scope:
- worker loop;
- pending immediate selection;
- success/failure status updates.

Files to create/update:
- `app/delivery/immediate_sender.py`
- `app/workers/immediate_delivery_worker.py`
- `tests/e2e/test_immediate_delivery_flow.py`

Tests:
- immediate delivery e2e test.

Definition of Done:
- critical records are sent without waiting for batch scheduling.

Commit message:
`feat: implement immediate delivery worker`

Step 10.3 — Implement batch scheduler and batch builder

You are implementing **Step 10.3 — Implement batch scheduler and batch builder**.

Goal:
- implement the batch send path.

Scope:
- batch scheduling;
- batch selection;
- batch payload assembly.

Files to create/update:
- `app/delivery/batch_scheduler.py`
- `app/delivery/batch_builder.py`
- `app/workers/batch_delivery_worker.py`
- `tests/unit/test_batch_builder.py`
- `tests/e2e/test_batch_delivery_flow.py`

Tests:
- batch builder unit tests;
- batch delivery e2e test.

Definition of Done:
- batch records are assembled and sent independently from immediate path.

Commit message:
`feat: implement batch scheduler and batch builder`

Step 10.4 — Implement retry backoff and dead letter handling for delivery

You are implementing **Step 10.4 — Implement retry backoff and dead letter handling for delivery**.

Goal:
- make northbound delivery resilient to external failures.

Scope:
- retry policy;
- backoff;
- dead-letter transitions.

Files to create/update:
- `app/delivery/retry.py`
- `app/delivery/dead_letter.py`
- `tests/integration/test_delivery_retry_and_dead_letter.py`

Tests:
- integration tests for retry and dead-letter behavior.

Definition of Done:
- failed deliveries get controlled retry;
- after retry limit they move to dead letter.

Commit message:
`feat: implement retry backoff and dead letter handling for delivery`

Step 11.1 — Add fastapi entrypoint and health endpoints

You are implementing **Step 11.1 — Add fastapi entrypoint and health endpoints**.

Goal:
- provide the base API layer.

Scope:
- FastAPI app entrypoint;
- live and ready endpoints.

Files to create/update:
- `app/api/main.py`
- `app/api/health.py`
- `tests/integration/test_health_api.py`

Tests:
- health API integration tests.

Definition of Done:
- health endpoints are available;
- readiness reflects bootstrap/DB status.

Commit message:
`feat: add fastapi entrypoint and health endpoints`

Step 11.2 — Add admin endpoints for source and outbox inspection

You are implementing **Step 11.2 — Add admin endpoints for source and outbox inspection**.

Goal:
- provide operator-facing inspection endpoints.

Scope:
- source status;
- outbox status;
- delivery overview.

Files to create/update:
- `app/api/admin.py`
- `app/api/schemas.py`
- `tests/integration/test_admin_api.py`

Tests:
- admin API integration tests.

Definition of Done:
- source and outbox state can be inspected through API.

Commit message:
`feat: add admin endpoints for source and outbox inspection`

Step 11.3 — Add command API endpoints

You are implementing **Step 11.3 — Add command API endpoints**.

Goal:
- expose the northbound command path.

Scope:
- create/get/retry command endpoints.

Files to create/update:
- `app/api/commands.py`
- `tests/integration/test_command_api.py`

Tests:
- command API integration tests.

Definition of Done:
- commands are accessible through API.

Commit message:
`feat: add command API endpoints`

Step 12.1 — Implement prometheus metrics for ingestion, delivery, and commands

You are implementing **Step 12.1 — Implement prometheus metrics for ingestion, delivery, and commands**.

Goal:
- add runtime metrics.

Scope:
- counters/gauges for sources, queues, delivery, commands.

Files to create/update:
- `app/monitoring/metrics.py`
- `tests/integration/test_metrics_endpoint.py`

Tests:
- metrics endpoint integration tests.

Definition of Done:
- `/metrics` exports key runtime metrics.

Commit message:
`feat: implement prometheus metrics for ingestion delivery and commands`

Step 12.2 — Implement source health worker and degraded state reporting

You are implementing **Step 12.2 — Implement source health worker and degraded state reporting**.

Goal:
- centralize health reporting for sources.

Scope:
- periodic health updates;
- degraded/disconnected semantics.

Files to create/update:
- `app/monitoring/health.py`
- `app/workers/source_health_worker.py`
- `tests/integration/test_source_health_worker.py`

Tests:
- source health worker integration tests.

Definition of Done:
- source health is stored and visible through API.

Commit message:
`feat: implement source health worker and degraded state reporting`

Step 12.3 — Add correlation-aware structured logging across runtime

You are implementing **Step 12.3 — Add correlation-aware structured logging across runtime**.

Goal:
- make logs usable for investigation and tracing.

Scope:
- propagate correlation/source/command/outbox identifiers into logs.

Files to create/update:
- update runtime modules as needed
- `tests/integration/test_structured_logging.py`

Tests:
- structured logging integration tests.

Definition of Done:
- critical runtime logs contain key identifiers.

Commit message:
`feat: add correlation aware structured logging across runtime`

Step 13.1 — Add systemd deployment assets and environment templates

You are implementing **Step 13.1 — Add systemd deployment assets and environment templates**.

Goal:
- prepare the Linux deployment profile.

Scope:
- systemd unit;
- environment template;
- deployment instructions.

Files to create/update:
- `deployment/systemd/wf-agent.service`
- `deployment/env/wf-agent.env.example`
- `README.md`

Tests:
- none.

Definition of Done:
- repository contains a standard Linux/systemd deployment baseline.

Commit message:
`ops: add systemd deployment assets and environment templates`

Step 13.2 — Add restart recovery end-to-end coverage

You are implementing **Step 13.2 — Add restart recovery end-to-end coverage**.

Goal:
- prove restart safety.

Scope:
- journal cursor recovery;
- outbox recovery;
- pending command recovery.

Files to create/update:
- `tests/e2e/test_restart_recovery_flow.py`

Tests:
- restart recovery end-to-end tests.

Definition of Done:
- critical pending items are not lost after restart.

Commit message:
`test: add restart recovery end to end coverage`

Step 14.1 — Add failure injection coverage for source, storage, and delivery faults

You are implementing **Step 14.1 — Add failure injection coverage for source, storage, and delivery faults**.

Goal:
- validate key failure modes.

Scope:
- source disconnects;
- temporary DB failures;
- northbound delivery failures;
- stale location.

Files to create/update:
- `tests/e2e/test_failure_injection_flow.py`

Tests:
- failure injection end-to-end tests.

Definition of Done:
- key failure scenarios are covered by tests.

Commit message:
`test: add failure injection coverage for source storage and delivery faults`

Step 14.2 — Finalize runbooks, support docs, and operational procedures

You are implementing **Step 14.2 — Finalize runbooks, support docs, and operational procedures**.

Goal:
- finish the solution as an operable, supportable system.

Scope:
- operator docs;
- recovery docs;
- replay/retry docs;
- API and DB schema docs;
- testing strategy docs.

Files to create/update:
- `docs/architecture/api-contracts.md`
- `docs/architecture/db-schema.md`
- `docs/architecture/source-contracts.md`
- `docs/architecture/testing-strategy.md`
- `docs/runbooks/operating.md`
- `docs/runbooks/recovery.md`
- `docs/runbooks/replay-and-retry.md`

Tests:
- none.

Definition of Done:
- operator runbooks and support documentation are complete and consistent with the implementation.

Commit message:
`docs: finalize runbooks operational procedures and support guides`

Step 15.1 — Implement content-delivery packaging for clean release bundles

You are implementing **Step 15.1 — Implement content-delivery packaging for clean release bundles**.

Goal:
- implement a reproducible packaging pipeline that forms a clean delivery package for end users.

Scope:
- allowlist-based release manifest;
- build script for staging, archive creation, and checksum generation;
- smoke validation of package contents;
- documentation for build usage and package composition.

Files to create/update:
- `release.manifest.yml`
- `scripts/build_release.py`
- `docs/content-delivery.md`
- `tests/unit/test_release_manifest.py`
- `tests/integration/test_build_release.py`

Tests:
- unit tests for manifest parsing and allowlist/exclude behavior;
- integration test for building a clean release bundle;
- verification that runtime files are included and development artifacts are excluded.

Definition of Done:
- the release bundle is built locally from an allowlist manifest;
- the package excludes tests, caches, virtualenvs, editor metadata, and other dev-only artifacts;
- the build script produces a `.tar.gz` archive and `.sha256` checksum in `dist/`.

Commit message:
`feat: add manifest-based release packaging pipeline for clean delivery bundles`

Step 15.2 - Diagnostic CLI for local runtime inspection and cache purge

You are implementing **Step 15.2 - Diagnostic CLI for local runtime inspection and cache purge**.

Goal:
- add a local diagnostic CLI for inspecting runtime state and purging the event cache.

Scope:
- inspect runtime state with `inspect sources`, `inspect outbox`, `inspect health`, and `inspect cache`;
- show TTL and ACL mark in the cache view;
- destructive cache purge command via `cache clear`;
- local SQLite access through bootstrap/repository layer.

Files to create/update:
- `app/cli/main.py`
- `app/cli/__init__.py`
- `tests/unit/test_cli.py`
- `tests/integration/test_cache_cli.py`
- `tests/integration/test_migration_bootstrap.py`
- `README.md`
- `docs/operations/runbook.md`
- `docs/operations/support.md`
- `docs/operations/procedures.md`

Tests:
- CLI parser tests;
- integration tests for cache inspection TTL, ACL mark, and purge behavior;
- migration smoke coverage for the cached event table.

Definition of Done:
- `wf-agent inspect sources|outbox|health|cache` shows local runtime state;
- `wf-agent inspect cache` shows active cached rows with TTL and ACL mark;
- `wf-agent cache clear` removes only cache rows;
- the cache table is present in the schema and covered by tests.

Commit message:
`feat: add diagnostic CLI for local runtime inspection and cache purge`