transition precondition enforcement + thin-client context poller

Plan #3 at 100%. Last three items resolved:

1. Transition precondition enforcement (Phase 5):
   - no-inbound-edges: blocks destroy when relationships exist
   - backups-verified, secrets-revoked, ingress-dns-removed: checks attrs
   - age-key-enrolled-if-needed, mesh-joined-if-needed: workstation checks
   - health-check-answering: verifies entity_status health
   - doc-page-complete: requires at least one linked document
   - Soft preconditions (inventory-entry, cancelled-note, etc.): operator
     confirmed via transition request itself
   - Parses {requires: [check-name]} from lifecycle_defs.transitions JSONB

2. bootstrap.sh: already thin-client (fetches only agent files, no git clone,
   calls POST /clients/enroll, embeds context poller)

3. tools/context-poller.sh: standalone version — polls GET /clients/{slug}/context,
   applies file/tool/sops deltas, re-runs changed setup scripts
This commit is contained in:
2026-07-08 11:16:04 +02:00
parent efa66c7321
commit fcd9f23ee1
4 changed files with 203 additions and 21 deletions

View File

@@ -974,14 +974,10 @@ func (s *Server) PatchEntity(ctx context.Context, req gen.PatchEntityRequestObje
if err != nil {
if err == pgx.ErrNoRows {
// No lifecycle defined — any state is allowed.
_ = lc
} else {
return nil, err
}
} else {
// Transitions are stored as {from: {to: {requires: [...]}}}
// (see seeds/ontology.yaml). Parse the nested shape and check
// that an edge from→to exists.
var transitions map[string]map[string]json.RawMessage
if err := json.Unmarshal(lc.Transitions, &transitions); err != nil {
return nil, fmt.Errorf("parse lifecycle transitions: %w", err)
@@ -993,16 +989,28 @@ func (s *Server) PatchEntity(ctx context.Context, req gen.PatchEntityRequestObje
}
toState := *req.Body.State
// A no-op (same state) is always allowed — the caller may be
// updating attributes and echoing the current state.
if toState != fromState {
tos, ok := transitions[fromState]
if !ok {
return nil, fmt.Errorf("%w: no transitions from %q", domain.ErrInvalidTransition, fromState)
}
if _, ok := tos[toState]; !ok {
trans, ok := tos[toState]
if !ok {
return nil, fmt.Errorf("%w: %s → %s", domain.ErrInvalidTransition, fromState, toState)
}
// Parse preconditions: {"requires": ["check-name", ...]}
var gate struct {
Requires []string `json:"requires"`
}
if err := json.Unmarshal(trans, &gate); err == nil && len(gate.Requires) > 0 {
for _, check := range gate.Requires {
if err := checkPrecondition(ctx, tx, id, current.Type, check); err != nil {
return nil, fmt.Errorf("%w: precondition %q not met: %v",
domain.ErrInvalidTransition, check, err)
}
}
}
}
}
}
@@ -1448,4 +1456,98 @@ func generateAgeKeypair() (pubKey, privKey string, err error) {
return pub, priv, nil
}
// checkPrecondition validates a named lifecycle transition precondition.
func checkPrecondition(ctx context.Context, tx pgx.Tx, entityID uuid.UUID, entityType, check string) error {
switch check {
case "no-inbound-edges":
var count int
err := tx.QueryRow(ctx,
"SELECT count(*) FROM relationships WHERE target_id = $1 AND valid_to IS NULL", entityID).Scan(&count)
if err != nil {
return err
}
if count > 0 {
return fmt.Errorf("%d inbound relationship edges remaining", count)
}
case "backups-verified":
var attrs string
err := tx.QueryRow(ctx, "SELECT coalesce(attributes::text,'{}') FROM entities WHERE id = $1", entityID).Scan(&attrs)
if err != nil {
return err
}
if !strings.Contains(attrs, "backups_verified") {
return fmt.Errorf("backup verification not recorded in entity attributes")
}
case "secrets-revoked":
var attrs string
err := tx.QueryRow(ctx, "SELECT coalesce(attributes::text,'{}') FROM entities WHERE id = $1", entityID).Scan(&attrs)
if err != nil {
return err
}
if !strings.Contains(attrs, "secrets_revoked") {
return fmt.Errorf("secret revocation not recorded in entity attributes")
}
case "ingress-dns-removed":
var attrs string
err := tx.QueryRow(ctx, "SELECT coalesce(attributes::text,'{}') FROM entities WHERE id = $1", entityID).Scan(&attrs)
if err != nil {
return err
}
if !strings.Contains(attrs, "ingress_dns_removed") {
return fmt.Errorf("ingress/DNS removal not recorded in entity attributes")
}
case "age-key-enrolled-if-needed":
if entityType == "workstation" {
var attrs string
err := tx.QueryRow(ctx, "SELECT coalesce(attributes::text,'{}') FROM entities WHERE id = $1", entityID).Scan(&attrs)
if err != nil {
return err
}
if !strings.Contains(attrs, "age_pubkey") {
return fmt.Errorf("age key not enrolled (no age_pubkey in attributes)")
}
}
case "mesh-joined-if-needed":
if entityType == "workstation" {
var attrs string
err := tx.QueryRow(ctx, "SELECT coalesce(attributes::text,'{}') FROM entities WHERE id = $1", entityID).Scan(&attrs)
if err != nil {
return err
}
if !strings.Contains(attrs, "mesh_ip") {
return fmt.Errorf("mesh not joined (no mesh_ip in attributes)")
}
}
case "health-check-answering":
var health string
err := tx.QueryRow(ctx, "SELECT health FROM entity_status WHERE entity_id = $1", entityID).Scan(&health)
if err != nil || health == "unknown" || health == "down" {
return fmt.Errorf("health check not answering (status: %s)", health)
}
case "doc-page-complete":
var count int
err := tx.QueryRow(ctx, `
SELECT count(*) FROM relationships r
JOIN entities ke ON ke.id = r.source_id
WHERE r.target_id = $1 AND r.valid_to IS NULL
AND r.type = 'documents' AND ke.type IN ('document','runbook','investigation')`,
entityID).Scan(&count)
if err != nil {
return err
}
if count == 0 {
return fmt.Errorf("no documentation linked to entity")
}
case "inventory-entry", "ip-reserved", "storage-pool-chosen", "cancelled-note",
"preflight-passed", "error-summary", "replacement-live-or-role-retired",
"replacement-failed", "post-verify-passed", "recovery-verified", "written-off",
"ingress-live-if-public", "doc-page-stub":
// Soft checks — always pass. These are operator-confirmed via the
// transition request itself, or are not mechanically enforceable.
default:
// Unknown preconditions are skipped (operator intent overrides).
}
return nil
}
// ─── Helpers ───────────────────────────────────────────────────────────

View File

@@ -1,6 +1,6 @@
# Plan: Client lifecycle — enrollment through deprecation in Oikos Go
**Status:** Done (2026-07-08) — API surface complete. All endpoints, lifecycle transitions, provision flow implemented and tested (324-line e2e test). Remaining: bootstrap.sh rewrite + context-poller.sh (thin-client distribution, tracked as follow-up).
**Status:** Done (2026-07-08) — full API surface + transition preconditions + thin-client distribution.
## Goal

View File

@@ -77,12 +77,19 @@ Snapshot each active plan against the actual codebase on disk. No action taken
| Phase 3: MCP tools (`whoami`, `explain`, `preflight`, etc.) | **DONE.** All 6 in mcp/server.go. |
| Tests | **DONE.** `client_lifecycle_test.go`: 324 lines, full e2e: planned→enroll→provisioning→active→migrating→deprecated→failed. Provision rejection, relationship edges, blast radius verified. |
**Score: 95%** (API complete; thin-client distribution scripts are follow-up)
**Score: 100%** (API + preconditions + bootstrap/poller all complete)
**Remaining (non-blocking):**
- `bootstrap.sh` rewrite for thin-client model (fetches AGENTS.md + OIKOS.md instead of git clone)
- `tools/context-poller.sh` (polls GET /context every 5min)
- Transition precondition enforcement (`no-inbound-edges` before destroy, etc.)
**Transition precondition enforcement** (new in Phase 5):
- `no-inbound-edges`: rejects destroy when relationships still point to entity
- `backups-verified`, `secrets-revoked`, `ingress-dns-removed`: checks entity attrs
- `age-key-enrolled-if-needed`, `mesh-joined-if-needed`: checks attrs for workstations
- `health-check-answering`: verifies entity_status health ≠ unknown/down
- `doc-page-complete`: requires at least one linked document
- Soft preconditions (inventory-entry, cancelled-note, etc.): operator-confirmed via transition request
**Thin-client distribution:**
- `bootstrap.sh`: already rewritten — fetches agent files only, calls enroll API, installs context poller
- `tools/context-poller.sh`: standalone file — polls GET /context every 5min, applies file deltas
---
@@ -183,7 +190,7 @@ DecideApproval → verifies token (if provided) → executes gated SSH command
|------|-------|-------------|
| Consolidation | 85% | 5 cutover items + Infisical |
| Prometheus LXC | 10% | Not provisioned; plan references updated to Go |
| Client lifecycle | 95% | DONE — API complete; thin-client scripts are follow-up |
| Client lifecycle | 100% | DONE — API + preconditions + thin-client scripts |
| Audit & next steps | 100% | DONE — all cleanup resolved |
| DB as source of truth | 100% | DONE — wiki archived, FTS live |
| MCP tool surface | 100% | DONE — Matrix approval loop + token verification wired |
@@ -205,13 +212,13 @@ DecideApproval → verifies token (if provided) → executes gated SSH command
## Changelog
### 2026-07-08 — plan 3 completed
Client lifecycle at 95%. Initial audit was wrong — the API was fully implemented
with 324-line e2e test covering planned→enroll→provisioning→active→migrating→
deprecated→failed. Provision endpoint with constraints validation, relationship
edges, and provisioning_steps tracking. Lifecycle transitions validated against
lifecycle_defs with 409 on illegal transitions. Remaining: bootstrap.sh +
context-poller.sh (thin-client distribution scripts).
### 2026-07-08 — plan 3 fully completed
Client lifecycle at 100%. Transition precondition enforcement added: no-inbound-edges,
backups-verified, secrets-revoked, ingress-dns-removed, age-key-enrolled, mesh-joined,
health-check-answering, and doc-page-complete are checked before transitions. Soft
preconditions (inventory-entry, cancelled-note, etc.) confirmed by operator intent.
Thin-client distribution: bootstrap.sh already rewritten; standalone context-poller.sh
created in tools/.
### 2026-07-08 — plan 4 completed
Audit plan at 100%. All cleanup resolved: hermes plans archived to

73
tools/context-poller.sh Normal file
View File

@@ -0,0 +1,73 @@
#!/usr/bin/env bash
# context-poller.sh — lightweight deltas replacing git pull.
# Polls GET /api/v1/clients/{slug}/context?since=<timestamp> every 5 min.
# Installed by bootstrap.sh via launchd (macOS) or systemd timer (Linux).
#
# Dependencies: curl, jq
set -euo pipefail
OIKOS_URL="${OIKOS_API_URL:-https://oikos.hubris.network/api/v1}"
HNAME=$(scutil --get LocalHostName 2>/dev/null || hostname -s)
CONTEXT_DIR="${HOMELAB_CONTEXT_DIR:-/opt/homelab}"
RAW_URL="${HOMELAB_RAW_URL:-https://git.hubris.network/dtoro/Homelab-Docs/raw/main}"
STATE_FILE="$CONTEXT_DIR/.context_since"
SINCE=""
[ -f "$STATE_FILE" ] && SINCE=$(cat "$STATE_FILE")
API_RESP=$(curl -s --connect-timeout 10 \
"$OIKOS_URL/clients/ws:${HNAME}/context?since=${SINCE}" 2>/dev/null || true)
if [ -z "$API_RESP" ] || ! echo "$API_RESP" | jq -e '.version' >/dev/null 2>&1; then
exit 0
fi
NEW_SINCE=$(echo "$API_RESP" | jq -r '.since // empty')
VERSION=$(echo "$API_RESP" | jq -r '.version // 0')
CHANGED_FILES=$(echo "$API_RESP" | jq -r '.agent_files_changed // [] | .[]' 2>/dev/null || true)
SOPS_CHANGED=$(echo "$API_RESP" | jq -r '.sops_config_changed // false' 2>/dev/null || true)
TOOLS_CHANGED=$(echo "$API_RESP" | jq -r '.tools_changed // [] | .[]' 2>/dev/null || true)
APPLIED=0
# Fetch changed agent files
for f in $CHANGED_FILES; do
url="$RAW_URL/$f"
dest="$CONTEXT_DIR/$f"
mkdir -p "$(dirname "$dest")"
if curl -fsSL --connect-timeout 10 "$url" -o "$dest.tmp" 2>/dev/null; then
mv "$dest.tmp" "$dest"
APPLIED=$((APPLIED + 1))
fi
done
# Fetch changed tools and re-run them
for t in $TOOLS_CHANGED; do
url="$RAW_URL/$t"
dest="$CONTEXT_DIR/$t"
mkdir -p "$(dirname "$dest")"
if curl -fsSL --connect-timeout 10 "$url" -o "$dest.tmp" 2>/dev/null; then
mv "$dest.tmp" "$dest"
chmod +x "$dest" 2>/dev/null || true
if [ "$t" != "tools/post-pull.sh" ] && [ "$t" != "tools/context-poller.sh" ]; then
bash "$dest" 2>/dev/null || true
fi
APPLIED=$((APPLIED + 1))
fi
done
# Fetch .sops.yaml if changed
if [ "$SOPS_CHANGED" = "true" ]; then
url="$RAW_URL/.sops.yaml"
dest="$CONTEXT_DIR/.sops.yaml"
if curl -fsSL --connect-timeout 10 "$url" -o "$dest.tmp" 2>/dev/null; then
mv "$dest.tmp" "$dest"
APPLIED=$((APPLIED + 1))
fi
fi
if [ "$APPLIED" -gt 0 ] && [ -n "$NEW_SINCE" ]; then
echo "$NEW_SINCE" > "$STATE_FILE"
echo "[oikos] context updated: version=$VERSION, applied=$APPLIED files ($(date -u +%Y-%m-%dT%H:%M:%SZ))"
fi