package db import ( "context" "github.com/dtoro/oikos/internal/checkdefaults" "github.com/google/uuid" "github.com/jackc/pgx/v5" ) // EnsureEntityChecks derives an entity's default check_defs from the // monitoring spec of its type (resolving per-entity `monitoring` overrides). // // This is the single shared hook that keeps the check graph in sync with // entity mutations. Both the HTTP create/patch handlers and the MCP // entity-mutation tools (create_entity, update_entity_attributes) call it so // that flipping an entity's `monitoring` attribute regenerates checks // regardless of which surface made the change — previously only the HTTP // path ran check derivation, so entities mutated via MCP silently produced no // checks (see plans/2026-08-03-session-review-haos-monitoring-capability-gaps.md, A2). func EnsureEntityChecks(ctx context.Context, tx pgx.Tx, id uuid.UUID, slug, entityType, name string, attrs []byte) (checkdefaults.Result, error) { tree, err := LoadTypeTree(ctx, tx) if err != nil { return checkdefaults.Result{}, err } res, err := checkdefaults.Ensure(ctx, tx, tree, checkdefaults.Target{ ID: id, Slug: slug, Type: entityType, Name: name, Attrs: attrs, }) if err != nil { return res, err } checkdefaults.LogResult(slug, entityType, res) return res, nil }