Anomaly remediation risk 2 · lowAnomaly & guardrailreflexion on

workflow_type: anomaly_remediation · owner: — · contract v2

AI scan flags an anomaly (margin compression, vendor cost spike, customer order pattern shift). Create anomaly_investigations row, propose remediation actions, surface in dashboard.

0 · Visual flow archify workflow graph (6 lanes: trigger → context → HITL → fan-out → post → verify)

Workflow flow
01 / Trigger 02 / Context + preconditions 03 / HITL gate (Mike approves) 04 / Fan-out cascade 05 / Post actions (log_run + reflexion + events) 06 / Verify (SQL / R2 / HTTP checks) How this workflow gets kicked off. Could be a chat-tool invocation, a cron tick, an inbound event (e.g. price.changed), or Mike clicking 'execute' from an admin page. TRIGGER kind: cron cron: 22,37,52 * * * * invoker: Mike (single-admin) risk_level: 2i Manual Mike invokes risk 2 Before deciding anything, pull related data from D1 (the local mirror of NetSuite). Each query loads a slice of the entity's current state so the AI and Mike can review before any writes happen. LOAD CONTEXT (D1 queries before fan-out) recent_anomalies: SELECT * FROM anomaly_investigations WHERE created_at > datetime('now','-2 hour…i Load context 1 D1 query Safety checks that must pass before any writes. 'block' severity halts the run; 'warn' surfaces a warning but continues. Without these, a bad input could cascade into NetSuite. PRECONDITIONS (checked before fan-out) (no preconditions)i Preconditions 0 checks The HITL (Human-In-The-Loop) gate. The workflow stages a proposed_action and waits for Mike to approve in /proposed-actions.html. Only fires when risk_level >= 3. This is the invariant: no NS write happens without Mike's go-ahead. HITL GATE (Mike approves before fan-out) action_type: workflow_anomaly_remediation entity_ref: workflow:anomaly_remediation:run_<run_id> approver: mike (single-admin) risk_gate: >= 3 (this workflow = 2) approval window: typical <= 60 min envelope: proposed_actions row staged by runneri No HITL auto-execute (risk ≤ 2) low risk Queue a write for Mike's HITL approval - staged in the proposed_actions table with status='pending'. Doesn't touch NetSuite directly. Real implementation. FAN-OUT #propose_remediation -> proposed_actions kind: stage_proposed_action action_type: anomaly_response status: REAL (src/lib/workflow_runner.ts)i propose_remediation stage_proposed_action REAL Write to the local D1 mirror (the read-side cache of NetSuite). Used for derived data and platform state. STUB today - per-tool d1_write logic lives in chat_tools/impls.ts. FAN-OUT #surface_dashboard -> D1: multiple kind: d1_write op: INSERT anomaly_investigations op: UPDATE weekly_anomalies status: REAL (src/lib/workflow_runner.ts)i surface_dashboard d1_write STUB Always-runs at the end of every workflow execution: writes a row to workflow_run_log with status, duration, step counts, and errors. Real implementation. POST ACTION: log_run -> D1: workflow_run_log fields: run_id, workflow_type, status, started_at, completed_at, summary_json source: runner automatic (always)i log_run workflow_run_log REAL Writes an entry to reflexion_log so the AI 'remembers' what happened. Only fires if the contract has reflexion_enabled=1. Future workflows can search this log for prior context. Real implementation. POST ACTION: reflexion -> D1: reflexion_log tags: anomaly_remediation reflexion_enabled: True fields: run_id, narrative, tags source: runner automatic (when reflexion_enabled=1)i reflexion reflexion_log REAL Fires a workflow.completed / workflow.partial / workflow.failed event into the event ledger so downstream subscriptions can react. Uses an idempotency_key so producer retries collapse. Real implementation (R564). POST ACTION: event -> Event ledger (recordEvent) types: workflow.completed | workflow.partial | workflow.failed idempotency_key per run_id source: runner automatici event workflow.completed REAL A post-execution sanity check. The runner stages this with status='pending'; the verify-scheduler cron (every :08 and :38 hourly) wakes up after the configured window (e.g. +24h) and executes the sql_check, then flips status to pass/fail/timeout. Real implementation (R564). VERIFY: anomalies_surfaced window: 10s sql_check: SELECT COUNT(*) FROM anomaly_investigations WHERE created_at > ? scheduler: verify cron @ :08/:38 (R563) result row: workflow_verify_resultsi anomalies_surfaced ≤60m Legend User UI Agent logic Policy Tool action Context / trace

1 · Trigger FIRES WHEN…

kind
cron
schedule (cron)
22,37,52 * * * *
existing_cron
"R174"

2 · Inputs required

No declared inputs.

3 · Context loaded D1 queries run before fan-out

name
recent_anomalies
tables
anomaly_investigations
SELECT
  *
  FROM anomaly_investigations
  WHERE created_at > datetime('now','-2 hours')
  AND status='open'

4 · Preconditions checked before any fan-out

No preconditions declared.

5 · HITL gate

Risk level 2 < 3 — no HITL gate. Fan-out runs immediately after preconditions pass.

6 · Fan-out targets 2 total · 1 real · 1 stub

#1propose_remediation stage_proposed_action REAL

action_type
anomaly_response

#2surface_dashboard d1_write STUB

operations
["INSERT anomaly_investigations","UPDATE weekly_anomalies"]
stub — not yet implemented in src/lib/workflow_runner.ts (kind d1_write hits the placeholder branch at line ~340 and emits step status 'stub'). Documented intent only.

7 · Post actions declared + runner-automatic

idactionsource
runner_log_runINSERT into workflow_run_log (run_id, workflow_type, status, started_at, completed_at, summary_json)runner automatic
runner_reflexionINSERT into reflexion_log (tags=anomaly_remediation, run_id, narrative)runner automatic (reflexion_enabled=1)
log_runINSERT workflow_runsdeclared in contract
reflexionINSERT reflexion_log with tags=anomaly_remediationdeclared in contract

8 · Verify checks written to workflow_verify_results (pending — verify cron not yet wired)

name
anomalies_surfaced
SELECT
  COUNT(*)
  FROM anomaly_investigations
  WHERE created_at > ?

9 · Retry policy

max_attempts
1
backoff
none
alert_on_final_failure
false

10 · What changes when this workflow runs aggregated side effects

systemtable / resourceactionstatussource
D1proposed_actionsINSERT (action_type=anomaly_response, entity_type=workflow_run)REALfan-out #1 (propose_remediation)
D1workflow_run_logINSERT (run summary)REALrunner automatic
D1reflexion_logINSERT (tags=anomaly_remediation)REALrunner automatic
Eventworkflow.completed (or workflow.failed)fireREALrunner automatic
D1workflow_verify_resultsINSERT pending × 1REALrunner verify staging
D1unknownwriteSTUBfan-out #2 (surface_dashboard)