Skip to content

refactor: make monitor transitions a pure typed state machine #76

Open

Description

@Igloczek

Problem

Monitor.start() currently decides retries, recovery, maintenance, important heartbeats and notifications while also performing networking and persistence. Push monitoring has its own transition decisions. Extracting HTTP transport alone leaves the policy difficult to test and makes future cron, incident and delivery features risky.

The needed improvement is a deterministic decision boundary, not a different default monitoring policy.

Existing work and scope boundary

This is a concrete next behavior boundary within #58, not a replacement for its decomposition plan. PR #66 already extracts HTTP transport and must be reused if merged. Typing is handled by Stage F (FND-018 closes #59); do not refile general cleanup here. Coordinate persistence calls with #56 / PR #67 and handler wiring with #57 / PRs #65 and #68.

Implementation specification

  1. Characterize the current HTTP/keyword/JSON-query and push transition behavior first, including first observation, pending, retry counts, recovery, inverted checks, maintenance and resend eligibility. Include configuration edits, cancellation and first-check behavior.
  2. Introduce directly owned typed observation, prior-state, transition-context and effect types in focused modules. Implement a pure reducer with explicit observation time and configuration generation: (prior, observation, context) -> { next, effects }. Effects describe persistence/notification/incident decisions; the reducer performs no I/O and imports no store or runtime.
  3. Integrate one bounded family at a time using the existing composition and lifecycle owners. Network execution reports facts; the commit owner validates generation and applies effects. Preserve lazy providers and current numeric wire/persisted statuses.
  4. Move push decisions onto the same policy where semantics match, while keeping push-specific liveness explicit. Do not invent universal policy for every protocol or add a compatibility registry.
  5. Keep retry, maintenance and recovery defaults unchanged. The inherited retry settings (retry count and retry interval) are the confirmation step: carry them into the policy as-is. Add one optional recovery period to the same policy: after a Down monitor passes, it stays Down until it has passed for N seconds (default 0, which is today's behaviour), so a flapping target does not alternate Down/Up alerts. A new degraded state belongs in a separate feature change.

Acceptance criteria

  • Fixture-based behavior tests cover the characterized transition matrix without a real clock, network or database.
  • HTTP and push produce the same decisions for equivalent inputs; their intentional differences are explicit and tested.
  • A stale result from an edited, paused, deleted or superseded run cannot persist a transition or send an effect.
  • Existing external payloads and saved monitor behavior survive the extraction; the new contract is compiler-checked.
  • Each implementation PR removes a specific mixed responsibility, rather than only moving methods between large files.

Verification and delivery

Run focused lifecycle, scheduler, timeout, heartbeat-data-plane and HTTP/push behavioral tests; then bun run lint, bun run build and the relevant backend/compiled checks. Capture before/after scheduling overhead, relevant import closure and compiled RSS for runtime changes. Preserve cold-import time inside the configured deadline.

Constraints

Keep Bun, Vue, one compiled executable and SQLite (kuma.db), with the supported Kuma importer. No required hosted service, extra database, worker deployment, Node/npm fallback or new distribution path. Backend code is written in Effect (north star D16, house style in docs/effect.md): dependencies are Effect services provided through layers, expected failures are tagged errors, and every input is decoded with Schema. Use direct @/ imports; no new barrel, ad-hoc registry, broad any facade or @ts-nocheck. Code passes the TS 7 strict typecheck and the @cloudflare/oxlint-config lint (D17). Make additive schema changes through the existing upgrade path; keep legacy rows meaningful.

Implement the idea independently. Staygreen is a design reference at the pinned revision, licensed FSL-1.1-MIT; do not transplant its source, CSS, illustrations or branding into this MIT project.

Source starting points

Plan placement

Reconciled on 2026-11-21 with the project plan (docs/roadmap.md) and the fixed decisions (docs/north-star.md). If this issue and those documents still disagree, raise it instead of choosing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions