Skip to main content
Version: 1.0
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.md and 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.id only).
  • 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):
  1. Cores call publishEvent({ event_name, organization_id, payload }) from @/platform/events.
  2. Event is inserted into fw_domain_events.
  3. DB trigger fw_process_domain_event() runs on INSERT:
    • Matches fw_automation_rules with trigger_type = 'event' (or pm_event) and event_config/trigger_config.
    • For each match, inserts a row into fw_workflow_executions with status = 'queued' and trigger_payload.
Path B — HTTP event-consumer:
  1. Caller (client or edge function) invokes supabase.functions.invoke('event-consumer', { body: { event_type, payload } }).
  2. event-consumer edge function:
    • Validates auth and body.
    • Runs Teams notifications (if pf_teams_notification_config has a rule for event_type).
    • Runs domain handlers (e.g. referral_accepted → CL transition + discharge checklist; other event types can be added in code).
  3. No subscription to PostgreSQL NOTIFY; invocation is push-by-caller only.
Path C — Realtime (Supabase):
  • Postgres Changes: Client subscribes to table changes (e.g. fw_workflow_executions, pf_notifications) via @/platform/realtime hooks. 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.
Path D — pg_notify (documented, server-side consumer unclear):
  • 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_rules with trigger_type, trigger_config, event_config, date_relative_config, conditions, status. Actions in fw_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_data with trigger_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).
  • Event-triggered rules: When an event is inserted into fw_domain_events, fw_process_domain_event() only queues executions (inserts into fw_workflow_executions). The automation-executor is not invoked for those queued rows in the codebase (no cron or worker that reads fw_workflow_executions and 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 / useWorkflowMachine in @/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)

  1. 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.
  2. Close the execution loop: Ensure “queued” workflow executions are processed (cron or webhook) and form submission triggers automation in a documented, consistent way.
  3. Single reference for triggers: One maintained “trigger config” and “event vs PM-internal” reference for automation and workflow.
  4. Unified automation UX: One entry point for creating automations (simple rule vs workflow) and clear guidance on workflow vs rule.
  5. 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 tree
  • src/platform/events/README.md — Platform events API
  • src/cores/fw/AGENTS.md — FW automation and workflow patterns