Last Updated: 2026-03-11
Status: Analysis and recommendations This document provides a deep dive into cross-core integration strategy, event consumer/pub-sub, the automation engine, and workflow execution — with identified gaps and a consistent approach for improvement.
1. Cross-Core Integration Strategy
1.1 Current State
The platform uses three integration patterns (constitution §1.3, PLATFORM_INTEGRATION_LAYERS.md, CROSS_CORE_INTEGRATIONS.md):
Strengths:
- Clear decision tree in
.cursor/rules/integration-patterns.mdand constitution. - Platform layers are well-documented (Forms, Notifications, Workforce, Realtime, Events).
- Event channels and naming are defined in EVENT_CONTRACTS.md with canonical channels (
domain_events,automation_trigger,fa_events,cl_pm_events, etc.). - Cross-core FK is scoped (ADR-002: CL may reference
pm_encounters.idonly). - Dependency graph in CROSS_CORE_INTEGRATIONS.md is up to date.
1.2 Gaps & Inconsistencies
2. Event Consumer & Pub/Sub
2.1 Current Mechanisms
Path A — Domain events table (FW automation / workflow):- Cores call
publishEvent({ event_name, organization_id, payload })from@/platform/events. - Event is inserted into
fw_domain_events. - DB trigger
fw_process_domain_event()runs on INSERT:- Matches
fw_automation_ruleswithtrigger_type = 'event'(orpm_event) andevent_config/trigger_config. - For each match, inserts a row into
fw_workflow_executionswithstatus = 'queued'andtrigger_payload.
- Matches
- Caller (client or edge function) invokes
supabase.functions.invoke('event-consumer', { body: { event_type, payload } }). event-consumeredge function:- Validates auth and body.
- Runs Teams notifications (if
pf_teams_notification_confighas a rule forevent_type). - Runs domain handlers (e.g.
referral_accepted→ CL transition + discharge checklist; other event types can be added in code).
- No subscription to PostgreSQL NOTIFY; invocation is push-by-caller only.
- Postgres Changes: Client subscribes to table changes (e.g.
fw_workflow_executions,pf_notifications) via@/platform/realtimehooks. Used for live UI updates, not for “event routing” to server-side handlers. - Broadcast: Ephemeral, non-persisted; used for typing, presence, or in-app signals. Not used as the primary event bus for cross-core domain events.
- EVENT_CONTRACTS and DATA_FLOW describe channels (
domain_events,automation_trigger,fa_events, etc.) and say “Subscribers consume via edge functions that listen to pg_notify.” - In the repo there is no edge function that “listens” to pg_notify (edge functions are HTTP-triggered). So either:
- A Supabase Database Webhook (or similar) maps NOTIFY to HTTP and calls an edge function, or
- The intended “listener” was never implemented and form/DB-triggered flows rely on something else (e.g. client invoking automation-executor after form submit).
2.2 Gaps
3. Automator (FW-03)
3.1 Current State
- Rules:
fw_automation_ruleswithtrigger_type,trigger_config,event_config,date_relative_config,conditions,status. Actions infw_automation_actions. - Trigger types:
form_submitted,form_updated,field_changed,schedule,webhook,manual,pm_event,event,date_relative. - Executor: Edge function
automation-executor:- Invoked via HTTP with body
{ trigger_data, dry_run? }. - Validates JWT and org access (V2 pattern with
pf_user_role_assignments). - For form triggers, expects
trigger_datawithtrigger_type,submission_id,form_id,organization_id,submission_data, etc. - Fetches matching rules, evaluates conditions, executes actions (send_email, send_notification, update_record, create_record, call_webhook, run_function), supports visual workflow graph (FW-06 Phase 2) and variables (FW-18).
- Invoked via HTTP with body
- Event-triggered rules: When an event is inserted into
fw_domain_events,fw_process_domain_event()only queues executions (inserts intofw_workflow_executions). The automation-executor is not invoked for those queued rows in the codebase (no cron or worker that readsfw_workflow_executionsand calls the executor).
3.2 Gaps & Inconsistencies
4. Workflow
4.1 Current State
- Visual workflow (FW-06): Workflow builder (React Flow), definitions stored in DB; execution is driven by automation-executor when invoked with appropriate trigger_data (e.g. form_submitted or workflow_execution payload). Executions stored in
fw_workflow_executions; realtime subscription used for monitoring. - XState machine (FW-18):
createWorkflowMachine/useWorkflowMachinein@/cores/fw/machines— client-side step machine (form, approval, action, condition, notification, wait). Not wired to the server-side workflow definition or automation-executor; no production callers outside the machines module. FW AGENTS.md and FORMS_WIZARDS_WORKFLOWS_AUTOMATIONS_RECOMMENDATIONS.md mark it as experimental / reserved for future use. - Workflow vs automation: Workflow builder = graph of nodes (trigger, actions, conditions, approvals). Automation rules = single trigger + conditions + actions. Overlap in concept; different UX and data model.
4.2 Gaps
5. Recommendations — Improvement, Gaps, Consistency
5.1 Cross-Core Integration
5.2 Event Consumer & Pub/Sub
5.3 Automator
5.4 Workflow
5.5 Consistency Approach (Summary)
- One event narrative: Document the two main paths (table-driven for FW automation, HTTP event-consumer for cross-core handlers) and when to use each; clarify pg_notify and Realtime roles.
- Close the execution loop: Ensure “queued” workflow executions are processed (cron or webhook) and form submission triggers automation in a documented, consistent way.
- Single reference for triggers: One maintained “trigger config” and “event vs PM-internal” reference for automation and workflow.
- Unified automation UX: One entry point for creating automations (simple rule vs workflow) and clear guidance on workflow vs rule.
- No duplicate execution model: Keep XState experimental until a concrete feature uses it; execution remains server-side (automation-executor + fw_workflow_executions).
6. References
- CROSS_CORE_INTEGRATIONS.md — Integration matrix and dependency graph
- EVENT_CONTRACTS.md — Event channels and payloads
- PLATFORM_INTEGRATION_LAYERS.md — Platform layer index
- API_CONTRACTS.md — Synchronous API contracts
- DATA_FLOW.md — Request lifecycle and automation flow
- FORMS_WIZARDS_WORKFLOWS_AUTOMATIONS_RECOMMENDATIONS.md — Forms, wizards, workflows, automations (archived)
- Constitution §1.3 — Integration patterns
.cursor/rules/integration-patterns.md— Pattern decision treesrc/platform/events/README.md— Platform events APIsrc/cores/fw/AGENTS.md— FW automation and workflow patterns