Bid award notification risk 4 · highBid & specreflexion on

workflow_type: bid_award_notification · owner: — · contract v2

Bid award arrives. Cascade: contract creation, customer_programs update, bid_lines → pricing_master commit, menu start dates, fulfillment heads-up.

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, portal_webhook, manual invoker: Mike (single-admin) risk_level: 4i Manual Mike invokes risk 4 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) bid: SELECT * FROM bids WHERE id=? bid_lines: SELECT * FROM bid_lines WHERE bid_id=? AND deleted_at IS NULL customer_existing_programs: SELECT * FROM customer_programs WHERE customer_id=(SELECT customer_id FROM bids…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) [block] bid_open: bid.status IN ('open','pending_award')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_bid_award_notification entity_ref: workflow:bid_award_notification:run_<run_id> approver: mike (single-admin) risk_gate: >= 3 (this workflow = 4) approval window: typical <= 60 min envelope: proposed_actions row staged by runneri Mike approves stage_proposed_action risk ≥ 3 gate 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 #commit_pricing -> D1: multiple kind: d1_write op: INSERT pricing_master FROM bid_lines status: REAL (src/lib/workflow_runner.ts)i commit_pricing d1_write 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: update_customer_progr… kind: stage_proposed_action (not in contract — diagram-only annotation)i update_customer_progr… stage_proposed_action REAL 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 #fulfillment_alert -> email draft kind: hitl_email_draft tool: propose_email_to_customer status: STUB (src/lib/workflow_runner.ts)i fulfillment_alert hitl_email_draft 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: customer_acknowledgem… kind: hitl_email_draft (not in contract — diagram-only annotation)i customer_acknowledgem… 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: bid_award_notification 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: pricing_committed window: 10s sql_check: SELECT COUNT(*) FROM pricing_master WHERE source LIKE '%bid_award_' || ? || '%' scheduler: verify cron @ :08/:38 (R563) result row: workflow_verify_resultsi pricing_committed ≤60m 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: bid_status_updated window: 2s sql_check: SELECT status FROM bids WHERE id=? scheduler: verify cron @ :08/:38 (R563) result row: workflow_verify_resultsi bid_status_updated ≤60m Legend User UI Agent logic Policy Tool action Context / trace

1 · Trigger FIRES WHEN…

kind
event
sources
["inbound_email","portal_webhook","manual"]

2 · Inputs required

namerequiredtype / hint
bid_idrequired
award_daterequired
contract_start_daterequired
contract_end_dateoptional
award_doc_r2_keyoptional

3 · Context loaded D1 queries run before fan-out

name
bid
tables
bids
SELECT
  *
  FROM bids
  WHERE id=?
name
bid_lines
tables
bid_lines
SELECT
  *
  FROM bid_lines
  WHERE bid_id=?
  AND deleted_at IS NULL
name
customer_existing_programs
tables
customer_programs · bids
SELECT
  *
  FROM customer_programs
  WHERE customer_id=(SELECT customer_id
  FROM bids
  WHERE id=?)
  AND status='active'

4 · Preconditions checked before any fan-out

checkruleseverity
bid_openbid.status IN ('open','pending_award')block

5 · HITL gate

Risk level 4 ≥ 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_bid_award_notification (proposal envelope)
entity_ref
workflow:bid_award_notification:run_<run_id>
approver
mike (single-admin)

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

#1commit_pricing d1_write STUB

operations
["INSERT pricing_master FROM bid_lines"]
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.

#2update_customer_programs stage_proposed_action REAL

action_type
create_customer_program

#3fulfillment_alert hitl_email_draft STUB

tool
propose_email_to_customer
target
fulfillment_team
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.

#4customer_acknowledgement hitl_email_draft STUB

tool
propose_email_to_customer
target
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=bid_award_notification, run_id, narrative)runner automatic (reflexion_enabled=1)
log_runINSERT workflow_runsdeclared in contract
reflexionINSERT reflexion_log with tags=bid_award,bid_id:?,customer:?declared in contract
bid_status_updateUPDATE bids SET status='awarded', awarded_at=now WHERE id=?declared in contract

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

name
pricing_committed
SELECT
  COUNT(*)
  FROM pricing_master
  WHERE source LIKE '%bid_award_' || ? || '%'
name
bid_status_updated
SELECT
  status
  FROM bids
  WHERE id=?

9 · Retry policy

max_attempts
3
backoff
exponential
base_ms
2000
max_ms
30000
alert_on_final_failure
true

10 · What changes when this workflow runs aggregated side effects

systemtable / resourceactionstatussource
D1proposed_actionsINSERT (action_type=create_customer_program, entity_type=workflow_run)REALfan-out #2 (update_customer_programs)
D1workflow_run_logINSERT (run summary)REALrunner automatic
D1reflexion_logINSERT (tags=bid_award_notification)REALrunner automatic
Eventworkflow.completed (or workflow.failed)fireREALrunner automatic
D1workflow_verify_resultsINSERT pending × 2REALrunner verify staging
D1proposed_actionsINSERT (HITL gate envelope)REALrunner HITL gate
D1unknownwriteSTUBfan-out #1 (commit_pricing)
D1proposed_actionsINSERT (email draft via propose_email_to_customer)STUBfan-out #3 (fulfillment_alert)
D1proposed_actionsINSERT (email draft via propose_email_to_customer)STUBfan-out #4 (customer_acknowledgement)