Sentin8:Flow - Detailed Design Document
1. Scope & Context
Sentin8:Flow is the platform's workflow/step engine: it executes declarative step sequences (generic executors + domain proxy-back steps + WASM hooks), persists every transition, parks on async decisions, resumes scheduled workflows when Sentin8:Cron calls the internal run endpoint, and publishes lifecycle events through the DBX outbox path.
- Owns (schema
flow):workflows(definition + state + step results + correlation),outbox(DBX producer table - the runtime event write goes here; the shell-spawned relay drains it to Bus),audit_journal, and the PG sequences created bySequenceStep(sentin8_flow_seq_{key}). - Does NOT own: the domain logic inside steps (proxy-back to owning
services), schedule due-ness (driven externally by Sentin8:Cron), the
WASM modules (Sentin8:Func executes them), event-triggered starts
(
event_triggersis declared empty - starts are API-triggered), and model reasoning - the agent loop lives in Sentin8:AI (§8 invariant 10). Flow executes the side effects an agent asks for, including the human-approval wait aManualDecisionStepprovides.
Trust boundary: proxy-back requests carry the workflow's env_id (the
tenant carrier - the M2M bearer identifies the service); the owning
service re-verifies the caller and enforces its own Shield gate.
2. Runtime Architecture
| Module | Role |
|---|---|
engine.rs | Sequential step loop: set processing → execute current step → honor on_error/next_step/branch → persist final state; emits lifecycle events on the way. |
types.rs | Wire types (StepDef, LogicVersion, StepResult, CallbackConfig, ProxyBackRequest/Response, WorkflowRecord) with string-tolerant deserializers (SDUI forms). |
steps_generic.rs | The 8 built-in executors and StepOutcome::{Proceed, Waiting}; state_waiting() parks with optional scheduled_at. |
proxy_back.rs | Domain-step proxy-back + in-memory HandlerRegistry (env profiles at boot + POST /v1/handlers/register). |
func_integration.rs | WASM hook execution via the Func client. |
store.rs | Persistence: insert/get/list/update with context-on-same-tx; outbox inserts; logic reload. |
handlers.rs | Lifecycle verbs (create/list/get/retry/repair/cancel/resume/register), internal schedule-run + async-callback, health. |
admin_ui.rs | SDUI overview/workflows/new/steps pages + shared policy pages; data endpoints require target_env_id. |
Workflow execution lifecycle:
3. Data Model
flow.workflows- RLS FORCE (sentin8_context_initialized() AND (sentin8_bypass_rls() OR env_id = sentin8_current_env_id())); indexes by env/domain/state/correlation; the due index coversscheduled_at WHERE state='waiting' AND scheduled_at IS NOT NULL(Cron resumes ≤50 per call).logicis stored inline so retry/resume/schedule/callback can re-load the remaining steps without a second source of truth. Noorg_idcolumn (pinned by test).flow.outbox- generated DBX table (sentin8-outbox:: outbox_table_sql("flow")) with NOTIFY trigger (flow_outbox_changed) and FORCE-RLS; the shell-spawned relay (shell boot step 8b) drains it env-by-env viasentin8_outbox_unrelayed_envs()and pushes rows to BusPOST /v1/produce(M2M, scopesentin8:bus:produce).flow.audit_journal- shared journal shape (unpushed index).
Step-result statuses: success | failed | waiting; a failed step is a
data outcome governed by on_error - engine transport errors become
failed results, not exceptions.
4. API Surface
| Endpoint | Method | Gate | Notes |
|---|---|---|---|
/v1/health | GET | none | liveness. |
/v1/workflows | POST | Shield sentin8:flow:write (+ optional ?target_env_id) | inserts + spawns the engine. |
/v1/workflows | GET | Shield sentin8:flow:read | filters domain/state/correlation, LIMIT 100. |
/v1/workflows/{id} | GET | Shield sentin8:flow:read | full record. |
/v1/workflows/{id}/retry | POST | Shield sentin8:flow:write | only failed; resumes from current_step. |
/v1/workflows/{id}/repair | POST | Shield sentin8:flow:write | truncates step_results to rewind_to (+ new input); does NOT spawn the engine. |
/v1/workflows/{id}/cancel | POST | Shield sentin8:flow:write | terminal cancel. |
/v1/workflows/{id}/resume | POST | Shield sentin8:flow:write | only waiting; decision becomes the last step's output. |
/v1/handlers/register | POST | Shield sentin8:flow:write | in-memory registry (domain-keyed). |
/v1/internal/schedules/run | POST | token (Cron's M2M) | resumes ≤50 due waiting workflows. |
/v1/internal/callbacks/async | POST | token | completes a waiting async step. |
/v1/admin/ui/{page}, /v1/admin/{resource} | GET | `has_any_scope(flow:read | flow:write |
| Shield admin API | mixed | policy-admin surface | shared bridge. |
Env addressing (uniform across the workflow surface): every handler accepts
?target_env_id and resolves the tenant as query param -> token env claim ->
system default (user tokens carry no env claim - without the param they
address the system env, the platform plane). Transitions are
optimistic-concurrency guarded: the UPDATE only lands on pending/processing/
waiting rows matching what the caller read; a raced transition (the engine
completing a run between a caller's read and its cancel) is a 409 Conflict -
terminal states (completed/failed/cancelled) reject cancel/retry/resume with
409, never a surfaced RowNotFound 500.
Event flow (producer; family flow):
Event types: sentin8:flow:workflow:created|started|completed|failed|waiting|updated|retried|repaired|cancelled|schedule:resumed
(payloads: {workflow_id, logic_id, domain} / {workflow_id, state} /
{workflow_id, current_step} / {workflow_id, rewind_to}).
4.1 Agent composition - the agent_turn step
Multi-agent work inside the platform is a workflow, not a second execution engine. A sub-agent invocation is one step kind:
{ "kind": "agent_turn",
"agent_name": "researcher",
"input": "$.task",
"output": "$.research",
"max_iterations": 6 }
The step opens (or reuses) a conversation owned by the workflow's caller,
posts one turn to POST /v1/conversations/{id}/turns on Sentin8:AI, and
records the returned assistant message as its output.
Decisions and their reasons.
-
One transcript owner per conversation. The step never holds transcript state; it passes ids. This preserves AI's invariant that conversation history lives only in AI, and it means a retried step cannot fork history.
-
The workflow's caller is the authority. The turn executes under that principal's token, so the sub-agent cannot exceed the workflow's own authority. There is no workflow-scoped escalation.
Concretely: the workflow records
caller_subandcaller_credential(asecrets://reference, never a token) at create time. At step time flow exchanges that credential at auth's RFC 8693 token-exchange grant for a short-lived delegated token naming the caller as the acting user, then callsPOST /v1/conversations/{id}/turnswith it. AI resolves the HOLDER fromact.sub, so any record the sub-agent creates belongs to the user.Flow does not broker credentials. A service-side endpoint able to mint a token naming ANY user would be impersonation-by-machine: no human approves it, no reason is recorded, and it scales to every user in the tenant. This platform already gates impersonation behind a human (actor tokens require a free-text reason, are capped at one hour, and are audited per issue and per use), and a broker would be a second, ungated door to the same authority. Exchanging the caller's OWN credential keeps "a sub-agent cannot exceed the workflow's authority" true by construction rather than by a scope check.
-
A workflow with no caller credential cannot run
agent_turn. The step fails with a reason naming the missing credential. Falling back to flow's own identity would be privilege elevation, and it would be invisible in the audit trail - the failure mode this design exists to prevent. -
awaiting_approvalmaps to workflowwaiting. This is the whole reason to compose rather than nest loops: a sub-agent that needs a human decision pauses the durable workflow, which already owns waiting/resume state and Cron-driven re-checking. A nested agent loop would have to reinvent that. -
Budget is shared. The turn charges the env and
agent:{name}budgets, so a workflow's total agent spend is bounded by the same caps as a direct call. There is no separate workflow budget tree to keep in sync. -
max_iterationsis a step-level ceiling that lowers, never raises, the agent policy'smax_turns. A step cannot grant an agent more rope than its own policy allows. -
Rejected alternative: a
sub_agenttool kind in AI. That would put workflow state inside the agent loop, giving the platform two owners of the same retry semantics. The step kind keeps durable state where it already lives.