AR collections action plan risk 3 · mediumAR & financereflexion on

workflow_type: ar_aging_action_plan · owner: — · contract v2

Surface aging customers, propose dunning steps

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: 0 6 * * * tz: America/New_York invoker: Mike (single-admin) risk_level: 3i Manual Mike invokes risk 3 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) aging: SELECT customer_id, customer_name, SUM(amount) AS total_open, MAX(days_overdue)… prior_actions: SELECT customer_name, MAX(proposed_at) FROM proposed_actions WHERE action_type=…i Load context 2 D1 queries 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) [warn] aging_data_fresh: MAX(sync_log.completed_at WHERE sync_type='scheduled_hot_customer_payments') > …i Preconditions 1 check 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_ar_aging_action_plan entity_ref: workflow:ar_aging_action_plan:run_<run_id> approver: mike (single-admin) risk_gate: >= 3 (this workflow = 3) approval window: typical <= 60 min envelope: proposed_actions row staged by runneri Mike approves stage_proposed_action risk ≥ 3 gate Repeat the same fan-out for each item in a collection (recipients, aging buckets, sub-workflows). STUB today. FAN-OUT #draft_per_tier -> multiple (loop) kind: loop_over_aging action: stage proposed_action with tier-specific draft body status: STUB (src/lib/workflow_runner.ts)i draft_per_tier loop_over_aging STUB Set a follow-up flag on a row (e.g., 'needs_review') so a downstream cron or human picks it up later. STUB today. FAN-OUT #pause_on_hold_check -> flag: stage propose_customer_program with type=service_hold kind: flag if: customer.days_overdue > 90 AND customer.total_open > 25000 action: stage propose_customer_program with type=service_hold status: STUB (src/lib/workflow_runner.ts)i pause_on_hold_check flag STUB Draft an email and stage it as a proposed_action for Mike's review. The email is NOT sent - only drafted + queued. STUB today. FAN-OUT #sales_rep_notify -> email draft kind: hitl_email_draft tool: propose_email_to_customer status: STUB (src/lib/workflow_runner.ts)i sales_rep_notify hitl_email_draft 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: ar_aging_action_plan 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: actions_staged window: 10s sql_check: SELECT COUNT(*) FROM proposed_actions WHERE action_type='collection_action' AND… scheduler: verify cron @ :08/:38 (R563) result row: workflow_verify_resultsi actions_staged ≤60m Legend User UI Agent logic Policy Tool action Context / trace

1 · Trigger FIRES WHEN…

kind
cron
schedule (cron)
0 6 * * *
timezone
America/New_York

2 · Inputs required

namerequiredtype / hint
threshold_daysoptional
tier_definitionsoptional

3 · Context loaded D1 queries run before fan-out

name
aging
tables
v_ar_aging
SELECT
  customer_id, customer_name, SUM(amount) AS total_open, MAX(days_overdue) AS max_overdue
  FROM v_ar_aging
  WHERE days_overdue >= ?threshold_days
  GROUP BY customer_id
name
prior_actions
tables
proposed_actions
SELECT
  customer_name, MAX(proposed_at)
  FROM proposed_actions
  WHERE action_type='collection_action'
  AND status='applied'
  GROUP BY customer_name

4 · Preconditions checked before any fan-out

checkruleseverity
aging_data_freshMAX(sync_log.completed_at WHERE sync_type='scheduled_hot_customer_payments') > now - 2 hourswarn

5 · HITL gate

Risk level 3 ≥ 3 — runner stages a proposed_actions row before fan-out runs. Mike must approve in proposed-actions.html before any side-effect step executes (real or stub).

action_type
workflow_ar_aging_action_plan (proposal envelope)
entity_ref
workflow:ar_aging_action_plan:run_<run_id>
approver
mike (single-admin)

6 · Fan-out targets 3 total · 0 real · 3 stub

#1draft_per_tier loop_over_aging STUB

loop kind
loop_over_aging
action
stage proposed_action with tier-specific draft body
stub — not yet implemented in src/lib/workflow_runner.ts (kind loop_over_aging hits the placeholder branch at line ~340 and emits step status 'stub'). Documented intent only.

#2pause_on_hold_check flag STUB

action
stage propose_customer_program with type=service_hold
condition (if)
customer.days_overdue > 90 AND customer.total_open > 25000
stub — not yet implemented in src/lib/workflow_runner.ts (kind flag hits the placeholder branch at line ~340 and emits step status 'stub'). Documented intent only.

#3sales_rep_notify hitl_email_draft STUB

tool
propose_email_to_customer
stub — not yet implemented in src/lib/workflow_runner.ts (kind hitl_email_draft 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=ar_aging_action_plan, run_id, narrative)runner automatic (reflexion_enabled=1)
log_runINSERT workflow_runsdeclared in contract
reflexionINSERT reflexion_log with tags=ar_aging_tickdeclared in contract
daily_digestINSERT ar_aging_summary row for trendingdeclared in contract

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

name
actions_staged
SELECT
  COUNT(*)
  FROM proposed_actions
  WHERE action_type='collection_action'
  AND proposed_at > ?

9 · Retry policy

max_attempts
2
backoff
linear
base_ms
5000
alert_on_final_failure
true

10 · What changes when this workflow runs aggregated side effects

systemtable / resourceactionstatussource
D1workflow_run_logINSERT (run summary)REALrunner automatic
D1reflexion_logINSERT (tags=ar_aging_action_plan)REALrunner automatic
Eventworkflow.completed (or workflow.failed)fireREALrunner automatic
D1workflow_verify_resultsINSERT pending × 1REALrunner verify staging
D1proposed_actionsINSERT (HITL gate envelope)REALrunner HITL gate
Loopagingiterate inner stepsSTUBfan-out #1 (draft_per_tier)
D1flags / status fieldset =STUBfan-out #2 (pause_on_hold_check)
D1proposed_actionsINSERT (email draft via propose_email_to_customer)STUBfan-out #3 (sales_rep_notify)