aegis-runtime-mv
Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.
Brought by drakestapleton
aegis-runtime-mv / docs / WORKFLOW-DAG.md
| 1 | # Workflow DAG (issue #39) |
| 2 | |
| 3 | A small dependency-aware task layer for multi-step agent work. It decides |
| 4 | when a caller-supplied task may start. Nothing else. |
| 5 | |
| 6 | ## Existing machinery |
| 7 | |
| 8 | Inspected before writing any code. |
| 9 | |
| 10 | | Area | What exists | |
| 11 | |---|---| |
| 12 | | `src/orchestration/run.rs` | One `Run` with `RunState {Running, WaitingForApproval, WaitingForTool, Completed, Failed, Cancelled}`, step and action counters, a `Trigger`. A run is a single linear lifecycle. | |
| 13 | | `src/orchestration/budget.rs` | `RunBudget` counters (steps, inference calls, actions, wall seconds) for one run. | |
| 14 | | `src/orchestration/termination.rs`, `trigger.rs` | Why a run ended; what started it. | |
| 15 | | `src/agent.rs` | `AgentEngine::execute_task`: a sequential model/tool loop. Since #38 it classifies tool failures (`FailureRecord`), retries only idempotent no-effect cases, and stops on `ApprovalPending`. | |
| 16 | | `src/failure.rs` (#38) | `FailureClass`, `EffectCertainty`, `FailureRecord`, `CancelToken`. Reused here. | |
| 17 | | `src/events/{event,envelope,bus}.rs` | `AegisEvent`, `EventEnvelope` (has a `sequence` field), broadcast `EventBus`. | |
| 18 | | `src/execution/{broker,action,receipt}.rs` | `ExecutionAuthority` and action receipts: the only authority for effects. | |
| 19 | | `src/sessions`, `tests/aegis_tests.rs` | Ids (`RunId`, `SessionId`), stores, and the integration test conventions (tokio tests, scripted fakes). | |
| 20 | |
| 21 | Already present: the single-run lifecycle, approval-gate states, a per-run budget, cancellation tokens, failure vocabulary, events. |
| 22 | |
| 23 | Absent before this change: prerequisite edges between tasks, readiness tracking, bounded concurrent execution of independent tasks, propagation of a prerequisite's failure to dependents, a workflow-level event order. No equivalent DAG exists, so `src/orchestration/workflow.rs` is new. It is not an inference scheduler: AIEN's `aien-scheduler` and the swarm manager are untouched and never called. |
| 24 | |
| 25 | ## Model |
| 26 | |
| 27 | - `TaskId` newtype. `WorkflowSpec::builder().add_task(id, &[deps], runner)...build()` rejects empty ids, duplicate tasks, duplicate edges, self dependency, unknown predecessors and cycles with typed `WorkflowError`. |
| 28 | - `TaskState {Pending, Ready, Running, WaitingForApproval, Completed, Failed(FailureRecord), Cancelled, Skipped{prerequisite}, Uncertain(FailureRecord)}`. `Uncertain` (issue #55) means "not known whether the effect happened"; it is not terminal. |
| 29 | - `TaskRunner` is the task body and returns `TaskOutcome {Completed(value), Failed(ToolFault), ApprovalPending, Cancelled}`. `certainty_on_interrupt()` says what is known about effects if the DAG interrupts the body (timeout, cancel); the default is `Uncertain`. |
| 30 | - `WorkflowConfig {run_id, session_id, max_parallel, per_task_timeout, total_budget}`. |
| 31 | |
| 32 | ## Rules the executor enforces |
| 33 | |
| 34 | - A dependent becomes Ready only when every prerequisite is `Completed`. |
| 35 | - A prerequisite that is `Failed`, `Cancelled` or `Skipped` makes the dependent `Skipped` with a `FailureRecord` of class `Rejected`, certainty `NoEffect`, cause `prerequisite failed: <id>`. This is transitive. |
| 36 | - `WaitingForApproval` is not success and not terminal. Dependents stay `Pending`; the report lists them in `unfinished` and the status is `WaitingForApproval`. |
| 37 | - The DAG never retries or reschedules anything. An `Uncertain` outcome runs once and is never retried here. |
| 38 | - Timeout gives `Failed` with class `Timeout` and the runner's certainty, unless that certainty is `Uncertain`: then the task becomes `Uncertain` (#55). Cancellation (token or total budget) cancels running tasks and every Pending/Ready task and starts nothing new; a running task whose certainty is `Uncertain` becomes `Uncertain` instead of `Cancelled`. |
| 39 | - Dependents of an `Uncertain` task are neither run nor skipped. They stay `Pending`, are listed in `unfinished`, and `WorkflowReport.blocked_behind_uncertain` names the uncertain task each one waits on (transitively). A task with both an `Uncertain` and a `Failed` prerequisite is still skipped by the failed one. |
| 40 | - Parallelism is bounded by a `tokio::sync::Semaphore`. The coordinator only awaits; task bodies run on spawned tasks. No `block_on`. |
| 41 | - The DAG authorizes nothing and caches no decision. Real effects go through the agent/skill gate path or `ExecutionAuthority` inside the task body. |
| 42 | |
| 43 | ## Events and replay |
| 44 | |
| 45 | Every transition takes a workflow sequence number under one lock, together with the state change, and is published in that same critical section. New `AegisEvent` variants: `WorkflowTaskReady/Started/Completed/Waiting/Failed/Cancelled/Skipped`, `WorkflowFinished`. They carry `workflow_seq` and go out with `EventEnvelope.sequence` set to it, so sorting by sequence replays the exact order. With `max_parallel = 1` the whole order is fixed for a given spec; with more parallelism the order of simultaneous completions follows real completion order, but the sequence is still gapless and dependency-consistent. Existing variants are unchanged; the canonical mapping returns `None` for the new ones. The `Uncertain` task state (#55) has no bus variant yet (#55 item 4): its ledger event `uncertain` is written in sequence, and the bus stays silent rather than publish a false Failed or Cancelled. `WorkflowStatus` still reads `Failed` or `Cancelled` when a task is uncertain; the "needs reconciliation" status is #55 item 2, not in the first cut. |
| 46 | |
| 47 | Results: `WorkflowSnapshot.results` maps task id to a sha256 digest of the result value. Failure records for Failed, Skipped and Cancelled tasks are in `WorkflowReport.failures`. |
| 48 | |
| 49 | ## Agent adapter |
| 50 | |
| 51 | `AgentTaskRunner` runs one `AgentEngine::execute_task` as a task; `agent_outcome` maps its result (failure class and certainty from the last tool failure, `ApprovalPending` stays pending, an incomplete run is `Failed/Uncertain`). It is a thin seam and is not exercised against a live model in tests. |
| 52 | |
| 53 | ## Restart boundary |
| 54 | |
| 55 | Workflow state is in memory only. After a process restart nothing is recovered and no task is resumed or re-run. `WorkflowSnapshot` is serializable (tested) so a later change can persist it, but no schema, table or recovery path exists. A resumed workflow must not re-run a task whose effect is `Uncertain`; that recovery policy is not designed here. Which record owns each fact of a delegated task, and the rebuild rule after a restart, are in [DELEGATED-DAG.md](DELEGATED-DAG.md). `src/orchestration/recovery.rs` (#56) implements that rebuild as a pure host function over an `AuthorityLookup` trait, tested against in-memory fakes; nothing stores the pointers yet and no real authority client exists. |
| 56 | |
| 57 | ## Not claimed |
| 58 | |
| 59 | AEGIS is outside release v1; nothing here is wired into a release path or the frozen E2E contract. Timings printed by the demo test compare `max_parallel = 1` against 2 with sleep-based fake tasks on CPU. They are informational, and no GPU or throughput claim is made. |
| 60 | |
| 61 | ## Progress messages for the Pi |
| 62 | |
| 63 | `src/orchestration/progress.rs` (#58) maps recorded task state to the PST1 Draft v0 message types (aien-dev/aien-protocols-mv#28): Pending and Ready are `queued`; Skipped is `failed` with reason `PREREQUISITE_FAILED`; Uncertain, and Failed with Uncertain effect certainty, are `uncertain_after_disconnect` (not known), never `failed`. Ids come from the stored records, ordinals count each task's messages from 0, and a completed task carries its result digest, never raw output. A message that would need a guess (no cause id, no result digest, no action digest) is refused. The first cut has no encoding and no network; the resume summary and the bytes are later. |
| 64 | |
| 65 | ## Grant gate for effectful tasks |
| 66 | |
| 67 | `src/orchestration/grant_gate.rs` (#59) wraps an effectful task body. It starts only under a desk grant bound to the request's objective id and cause id, and checks the grant again after preparation, immediately before the effect. No grant leaves the task waiting for approval; an unbound binding, a forged id, a spent, expired or revoked grant, a foreign objective or cause, and an unreachable desk are refused with a named reason and no effect. The gate checks and never spends: the effect ledger spends the grant when it opens the effect intent. The desk is a trait tested with a fake; the real adapter waits on sovereign-core#379, and binding to the INTERPLANE canonical request digest waits on aien-protocols-mv#33. |