Customer invoice dispute risk 3 · mediumCustomer opsreflexion on

workflow_type: customer_invoice_dispute · owner: — · contract v2

Customer emails disputing an invoice. Extract invoice ref + reason. Attach prior invoices. Propose response. Queue for sales rep. Flag for AR hold-or-continue decision.

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: event sources: inbound_email_triage_classification 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) invoice: SELECT id, tranid, foreigntotal, trandate FROM transactions WHERE type='CustInv… recent_payments: SELECT * FROM customer_payments WHERE customer_id=? AND payment_date > datetime… prior_disputes: SELECT COUNT(*) FROM ar_disputes WHERE customer_id=? AND status IN ('open','esc…i Load context 3 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] invoice_exists: invoice IS NOT NULL OR invoice_ref IS NULLi 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_customer_invoice_dispute entity_ref: workflow:customer_invoice_dispute: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 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 #draft_response -> email draft kind: hitl_email_draft tool: propose_email_to_customer status: STUB (src/lib/workflow_runner.ts)i draft_response hitl_email_draft STUB 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 #escalate -> proposed_actions kind: stage_proposed_action action_type: customer_escalation status: REAL (src/lib/workflow_runner.ts)i escalate stage_proposed_action REAL 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 #ar_hold_decision -> proposed_actions kind: stage_proposed_action if: prior_disputes >= 2 action_type: ar_hold_review status: REAL (src/lib/workflow_runner.ts)i ar_hold_decision stage_proposed_action REAL 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: customer_invoice_dispute 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: response_drafted window: 30s sql_check: SELECT id FROM proposed_actions WHERE action_type='customer_email' AND proposed… scheduler: verify cron @ :08/:38 (R563) result row: workflow_verify_resultsi response_drafted ≤60m Legend User UI Agent logic Policy Tool action Context / trace

1 · Trigger FIRES WHEN…

kind
event
sources
["inbound_email_triage_classification"]

2 · Inputs required

namerequiredtype / hint
email_uuidrequired
customer_idoptional
invoice_refoptional
reason_textrequired

3 · Context loaded D1 queries run before fan-out

name
invoice
tables
transactions
SELECT
  id, tranid, foreigntotal, trandate
  FROM transactions
  WHERE type='CustInvc'
  AND tranid=?
name
recent_payments
tables
customer_payments
SELECT
  *
  FROM customer_payments
  WHERE customer_id=?
  AND payment_date > datetime('now','-90 days')
name
prior_disputes
tables
ar_disputes
SELECT
  COUNT(*)
  FROM ar_disputes
  WHERE customer_id=?
  AND status IN ('open','escalated')

4 · Preconditions checked before any fan-out

checkruleseverity
invoice_existsinvoice IS NOT NULL OR invoice_ref IS NULLwarn

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_customer_invoice_dispute (proposal envelope)
entity_ref
workflow:customer_invoice_dispute:run_<run_id>
approver
mike (single-admin)

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

#1draft_response 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.

#2escalate stage_proposed_action REAL

action_type
customer_escalation
entity_type
customer

#3ar_hold_decision stage_proposed_action REAL

action_type
ar_hold_review
condition (if)
prior_disputes >= 2

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=customer_invoice_dispute, run_id, narrative)runner automatic (reflexion_enabled=1)
log_runINSERT workflow_runsdeclared in contract
reflexionINSERT reflexion_log with tags=invoice_dispute,customer:?declared in contract

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

name
response_drafted
SELECT
  id
  FROM proposed_actions
  WHERE action_type='customer_email'
  AND proposed_at > ?

9 · Retry policy

max_attempts
2
backoff
linear
base_ms
3000
alert_on_final_failure
true

10 · What changes when this workflow runs aggregated side effects

systemtable / resourceactionstatussource
D1proposed_actionsINSERT (action_type=customer_escalation, entity_type=customer)REALfan-out #2 (escalate)
D1proposed_actionsINSERT (action_type=ar_hold_review, entity_type=workflow_run)REALfan-out #3 (ar_hold_decision)
D1workflow_run_logINSERT (run summary)REALrunner automatic
D1reflexion_logINSERT (tags=customer_invoice_dispute)REALrunner automatic
Eventworkflow.completed (or workflow.failed)fireREALrunner automatic
D1workflow_verify_resultsINSERT pending × 1REALrunner verify staging
D1proposed_actionsINSERT (HITL gate envelope)REALrunner HITL gate
D1proposed_actionsINSERT (email draft via propose_email_to_customer)STUBfan-out #1 (draft_response)