aves 0.5.0

Typed async application runtime with domain messaging, RPC, and durable workflow automation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::any::Any;
use std::path::PathBuf;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use specta::Type;
use tokio::sync::oneshot;
use uuid::Uuid;

use crate::auth::{Jwt, MachineIssuerId};
use crate::bus::BusRpcContext;

use super::FlowDomain;
use super::ast::{ContentAddressedPath, ContentAddressedRoot, FlowNode};
use super::cron::FlowTriggerRecord;
use super::mobility::{
    FlowActivationAcknowledgement, FlowMobilityCommitAcknowledgement, FlowMobilitySnapshot, FlowYieldAcknowledgement,
};
use super::registration::NodeKind;
use super::types::{
    FlowDefinition, FlowError, FlowExecutionDetail, FlowExecutionId, FlowExecutionSnapshot, FlowFailure, FlowId,
    FlowInvocationKind, FlowMobilityDestination, FlowMobilityReadiness, FlowNodeId, NodeExecutionId,
    ScriptSourceLocation,
};

/// Correlation carried by every node-owner lifecycle message.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Type, JsonSchema)]
pub struct LifecycleCorrelation {
    /// Owner kind responsible for this node invocation.
    pub node_kind: NodeKind,
    /// Persisted Flow definition identity.
    pub flow_id: FlowId,
    /// Persisted graph-node identity.
    pub flow_node_id: FlowNodeId,
    /// Identity of this execution of the Flow.
    pub flow_execution_id: FlowExecutionId,
    /// Identity of this execution of the node.
    pub node_execution_id: NodeExecutionId,
}

/// Typed envelope used by a host-owned start message.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LifecycleStart<T> {
    /// Durable graph and execution correlation.
    pub correlation: LifecycleCorrelation,
    /// Owner-specific start configuration.
    pub payload: T,
}

/// Typed envelope used by a host-owned tick message.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LifecycleTick<T> {
    /// Durable graph and execution correlation.
    pub correlation: LifecycleCorrelation,
    /// Owner-specific tick input.
    pub payload: T,
}

/// Typed envelope returned by a host-owned successful lifecycle message.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LifecycleSuccess<T> {
    /// Durable graph and execution correlation.
    pub correlation: LifecycleCorrelation,
    /// Owner-specific terminal result.
    pub payload: T,
}

/// Typed envelope returned by a host-owned failed lifecycle message.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LifecycleError<T> {
    /// Durable graph and execution correlation.
    pub correlation: LifecycleCorrelation,
    /// Owner-specific terminal error.
    pub payload: T,
}

/// Erased payload used only at the generic adapter boundary.
pub type ErasedLifecyclePayload = Box<dyn Any + Send + Sync>;

/// Correlated lifecycle message after its owner-specific payload is erased.
pub struct ErasedLifecycleMessage {
    /// Durable graph and execution correlation.
    pub correlation: LifecycleCorrelation,
    /// Complete persisted leaf configuration selected by the graph.
    pub node: FlowNode,
    /// Explicit execution directory below the configured Flow data root.
    pub execution_root: PathBuf,
    /// Fresh node-scoped token, present only for a start dispatch.
    pub node_token: Option<Jwt>,
    /// Owner-specific typed message, erased at the generic boundary.
    pub payload: ErasedLifecyclePayload,
}

/// Internal request wrapper routed through the Flow domain actor.
pub(crate) struct FlowRequest {
    pub payload: FlowRequestPayload,
    pub reply_to: oneshot::Sender<Result<FlowResponse, FlowError>>,
}

/// Flow control requests. Node-owner lifecycle messages are declared by their
/// owning domains and are intentionally not part of this generic domain.
#[aves::bus_requests(
    marker = FlowDomain,
    context = BusRpcContext,
    root_error = FlowError,
    domain_error = FlowError
)]
#[derive(Clone, Debug, Deserialize, Serialize, Type, JsonSchema)]
pub enum FlowRequestPayload {
    #[privileged(reason = "Flow runtime lifecycle is owned by Aves")]
    StartRuntime,

    #[privileged(reason = "Flow runtime lifecycle is owned by Aves")]
    StopRuntime,

    /// Creates a new immutable Flow definition.
    #[operation(name = "createFlow", kind = "mutation", response = FlowCreatedOutput, response_variant = "FlowCreated")]
    CreateFlow { flow: FlowDefinition },

    /// Downloads and imports an explicit native Collection-format blob ticket.
    /// Creates or replaces a persisted adapter-backed trigger.
    #[operation(name = "createFlowTrigger", kind = "mutation", response = FlowTriggerCreatedOutput, response_variant = "FlowTriggerCreated")]
    CreateFlowTrigger { trigger: FlowTriggerRecord },

    /// Adds a typed node to a Flow graph revision.
    #[operation(name = "createFlowNode", kind = "mutation", response = FlowNodeCreatedOutput, response_variant = "FlowNodeCreated")]
    CreateFlowNode { flow_id: FlowId, node: FlowNode },

    /// Lists Flow definitions.
    #[operation(name = "listFlows", kind = "query", response = FlowsListedOutput, response_variant = "FlowsListed")]
    ListFlows,

    /// Lists persisted Cron triggers for one Flow.
    #[operation(name = "listFlowTriggers", kind = "query", response = FlowTriggersListedOutput, response_variant = "FlowTriggersListed")]
    ListFlowTriggers { flow_id: FlowId },

    /// Project locally authoritative enabled triggers to their source domain.
    #[privileged(reason = "Source-domain runtime reconciliation")]
    ListSourceTriggers { adapter_kind: String },

    /// Lists active execution snapshots.
    #[operation(name = "listActiveExecutions", kind = "query", response = ActiveExecutionsListedOutput, response_variant = "ActiveExecutionsListed")]
    ListActiveExecutions,

    /// Lists terminal execution snapshots.
    #[operation(name = "listTerminalExecutions", kind = "query", response = TerminalExecutionsListedOutput, response_variant = "TerminalExecutionsListed")]
    ListTerminalExecutions,

    /// Returns one Flow definition.
    #[operation(name = "getFlow", kind = "query", response = FlowRetrievedOutput, response_variant = "FlowRetrieved")]
    GetFlow { flow_id: FlowId },

    /// Returns the exact saved graph revision and execution detail.
    #[operation(name = "getExecutionDetail", kind = "query", response = ExecutionDetailRetrievedOutput, response_variant = "ExecutionDetailRetrieved")]
    GetExecutionDetail { execution_id: FlowExecutionId },

    /// Reads one UTF-8 Script source file from a mapped or stored location.
    #[operation(name = "readScriptSource", kind = "query", response = ScriptSourceReadOutput, response_variant = "ScriptSourceRead")]
    ReadScriptSource {
        location: ScriptSourceLocation,
        path: String,
    },

    /// Starts a Flow execution through its public runtime operation.
    #[operation(name = "start", kind = "mutation", response = StartedOutput, response_variant = "Started")]
    Start {
        flow_id: FlowId,
        pulse: Value,
        automatic: bool,
        #[serde(default)]
        invocation_kind: FlowInvocationKind,
    },

    /// Fire a persisted event trigger through Flow's authoritative admission.
    #[privileged(reason = "Source-domain event admission")]
    FireSourceTrigger {
        #[specta(type = String)]
        #[schemars(with = "String")]
        trigger_id: uuid::Uuid,
        pulse: Value,
    },

    /// Aborts an active Flow execution through its public runtime operation.
    #[operation(name = "abort", kind = "mutation", response = AbortedOutput, response_variant = "Aborted")]
    Abort { execution_id: FlowExecutionId },

    /// Imports one explicit host-local file or directory into Flow's private content store.
    /// Files are rooted at their basename; directory entries are rooted relative to the directory.
    #[operation(name = "importWorkspace", kind = "mutation", response = WorkspaceImportedOutput, response_variant = "WorkspaceImported")]
    ImportWorkspace { source_path: String },

    #[operation(name = "downloadWorkspaceBlob", kind = "mutation", response = WorkspaceImportedOutput, response_variant = "WorkspaceImported")]
    DownloadWorkspaceBlob { ticket: String },

    /// Enables a validated Flow or pauses future automatic starts without changing its graph.
    #[operation(name = "setEnabled", kind = "mutation", response = FlowUpdatedOutput, response_variant = "FlowUpdated")]
    SetEnabled { flow_id: FlowId, enabled: bool },

    /// Updates a Flow definition by creating its next revision.
    #[operation(name = "updateFlow", kind = "mutation", response = FlowUpdatedOutput, response_variant = "FlowUpdated")]
    UpdateFlow { flow: FlowDefinition },

    /// Updates a persisted trigger.
    #[operation(name = "updateFlowTrigger", kind = "mutation", response = FlowTriggerUpdatedOutput, response_variant = "FlowTriggerUpdated")]
    UpdateFlowTrigger { trigger: FlowTriggerRecord },

    /// Replaces a typed node in a Flow graph revision.
    #[operation(name = "updateFlowNode", kind = "mutation", response = FlowNodeUpdatedOutput, response_variant = "FlowNodeUpdated")]
    UpdateFlowNode { flow_id: FlowId, node: FlowNode },

    /// Saves one UTF-8 Script source file to a mapped directory or content-addressed root.
    #[operation(name = "saveScriptSource", kind = "mutation", response = ScriptSourceSavedOutput, response_variant = "ScriptSourceSaved")]
    SaveScriptSource {
        location: ScriptSourceLocation,
        path: String,
        source: String,
    },

    /// Updates the plain completed-run limit through the existing Flow control
    /// service and immediately prunes eligible terminals, including while idle.
    #[privileged(reason = "Flow retention is runtime-owned")]
    UpdateRetention { retention_completed_runs: u32 },

    /// Prevents new starts for a Flow.
    #[operation(name = "deleteFlow", kind = "mutation", response = FlowDeletedOutput, response_variant = "FlowDeleted")]
    DeleteFlow { flow_id: FlowId },

    /// Removes a trigger.
    #[operation(name = "deleteFlowTrigger", kind = "mutation", response = FlowTriggerDeletedOutput, response_variant = "FlowTriggerDeleted")]
    DeleteFlowTrigger {
        #[specta(type = String)]
        #[schemars(with = "String")]
        trigger_id: Uuid,
    },

    /// Removes a typed node from a Flow graph revision.
    #[operation(name = "deleteFlowNode", kind = "mutation", response = FlowNodeDeletedOutput, response_variant = "FlowNodeDeleted")]
    DeleteFlowNode { flow_id: FlowId, node_id: FlowNodeId },

    /// Selects exactly one already-confirmed peer destination for a Flow synchronization.
    #[operation(name = "selectMobilityDestination", kind = "mutation", response = MobilityDestinationSelectedOutput, response_variant = "MobilityDestinationSelected")]
    SelectMobilityDestination {
        flow_id: FlowId,
        destinations: Vec<FlowMobilityDestination>,
    },

    /// Reads durable selected-Flow replica readiness without changing authority.
    #[operation(name = "getFlowMobilityReadiness", kind = "query", response = FlowMobilityReadinessOutput, response_variant = "FlowMobilityReadiness")]
    GetFlowMobilityReadiness { flow_id: FlowId },

    /// Stages a peer-captured selected-Flow transfer unit without activating it.
    ///
    /// The authenticated remote-operation surface owns peer authorization at
    /// this boundary; the snapshot remains a typed generated operation input.
    #[operation(name = "importMobilitySnapshot", kind = "mutation", response = FlowMobilityCommitAcknowledgementOutput, response_variant = "FlowMobilityCommitAcknowledgement")]
    ImportMobilitySnapshot {
        snapshot: FlowMobilitySnapshot,
        source_peer_key: MachineIssuerId,
    },

    /// Captures the selected Flow's durable transfer unit and begins synchronization.
    #[operation(name = "synchronizeFlow", kind = "mutation", response = FlowMobilityReadinessOutput, response_variant = "FlowMobilityReadiness")]
    SynchronizeFlow { flow_id: FlowId },

    /// Durably fences the current authority before its acknowledgement is handed to a replica.
    #[operation(name = "yieldFlowAuthority", kind = "mutation", response = FlowAuthorityYieldedOutput, response_variant = "FlowAuthorityYielded")]
    YieldFlowAuthority { flow_id: FlowId },

    /// Promotes a fully committed inactive replica only with a source yield acknowledgement.
    #[operation(name = "promoteFlowReplica", kind = "mutation", response = FlowReplicaPromotedOutput, response_variant = "FlowReplicaPromoted")]
    PromoteFlowReplica {
        flow_id: FlowId,
        acknowledgement: FlowYieldAcknowledgement,
    },

    /// Removes a yielded source's transferred Flow state after destination activation.
    #[operation(name = "finalizeFlowTransfer", kind = "mutation", response = FlowTransferFinalizedOutput, response_variant = "FlowTransferFinalized")]
    FinalizeFlowTransfer { flow_id: FlowId },
}

/// Responses produced by the Flow actor, in lifecycle and operation order.
#[aves::bus_responses]
#[derive(Clone, Debug, Deserialize, Serialize, Type, JsonSchema)]
pub enum FlowResponse {
    /// Locally authoritative enabled triggers, empty while Flow is stopped.
    SourceTriggersListed {
        triggers: Vec<FlowTriggerRecord>,
    },
    RuntimeStarted,
    RuntimeStopped,
    /// Created Flow definition.
    FlowCreated {
        flow: FlowDefinition,
    },
    /// Immutable root returned by a local workspace import.
    WorkspaceImported {
        root: ContentAddressedRoot,
    },
    ScriptSourceRead {
        source: String,
    },
    ScriptSourceSaved {
        stored: Option<ContentAddressedPath>,
    },
    /// Flow definition updated.
    FlowUpdated {
        flow: FlowDefinition,
    },
    /// Flow definition retrieved.
    FlowRetrieved {
        flow: FlowDefinition,
    },
    /// Flow definitions listed.
    FlowsListed {
        flows: Vec<FlowDefinition>,
    },
    /// Persisted Cron triggers for one Flow.
    FlowTriggersListed {
        triggers: Vec<FlowTriggerRecord>,
    },
    /// Trigger created.
    FlowTriggerCreated {
        trigger: FlowTriggerRecord,
    },
    /// Trigger updated.
    FlowTriggerUpdated {
        trigger: FlowTriggerRecord,
    },
    /// Trigger deleted.
    FlowTriggerDeleted {
        #[specta(type = String)]
        #[schemars(with = "String")]
        trigger_id: Uuid,
    },
    /// Node created.
    FlowNodeCreated {
        node: FlowNode,
    },
    /// Node updated.
    FlowNodeUpdated {
        node: FlowNode,
    },
    /// Node deleted.
    FlowNodeDeleted {
        flow_id: FlowId,
        node_id: FlowNodeId,
    },
    /// Flow deletion accepted.
    FlowDeleted {
        flow_id: FlowId,
    },
    /// Active executions listed.
    ActiveExecutionsListed {
        executions: Vec<FlowExecutionSnapshot>,
    },
    /// Terminal executions listed.
    TerminalExecutionsListed {
        executions: Vec<FlowExecutionSnapshot>,
    },
    /// A Flow execution detail was retrieved.
    ExecutionDetailRetrieved {
        detail: FlowExecutionDetail,
    },
    /// A Flow execution was accepted.
    Started {
        execution_id: FlowExecutionId,
    },
    /// An active Flow execution was aborted.
    Aborted {
        execution_id: FlowExecutionId,
    },
    /// One destination was accepted after durable pairing validation.
    MobilityDestinationSelected {
        flow_id: FlowId,
        destination: FlowMobilityDestination,
    },
    /// Durable selected-Flow mobility readiness.
    FlowMobilityReadiness {
        readiness: FlowMobilityReadiness,
    },
    /// The destination committed the imported snapshot durably.
    FlowMobilityCommitAcknowledgement {
        acknowledgement: FlowMobilityCommitAcknowledgement,
    },
    /// A source durably stopped scheduling and admitting Flow mutations.
    FlowAuthorityYielded {
        acknowledgement: FlowYieldAcknowledgement,
    },
    /// The destination durably became the sole authority for the transferred Flow.
    FlowReplicaPromoted {
        acknowledgement: FlowActivationAcknowledgement,
    },
    /// A source's exclusive transferred state was removed.
    FlowTransferFinalized {
        flow_id: FlowId,
    },
    /// Completed-run retention was applied.
    RetentionUpdated,
}

/// Observer events emitted by the Flow domain.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum FlowBroadcast {
    /// Source domains must reconcile their live listeners against Flow.
    TriggersChanged,
    /// A management-visible Flow definition changed.
    DefinitionChanged { flow: Box<FlowDefinition> },
    /// Observer-visible execution snapshot transition.
    ExecutionStatusChanged {
        flow_name: String,
        snapshot: FlowExecutionSnapshot,
    },
    /// Node-ready events carry correlation only; credentials are requested separately through Auth.
    Ready { correlation: LifecycleCorrelation },
    /// A Flow execution reached a node-failed transition.
    Failed {
        correlation: LifecycleCorrelation,
        failure: FlowFailure,
    },
    /// A Flow execution reached a node-succeeded transition.
    Succeeded { correlation: LifecycleCorrelation },
    /// A Flow execution started.
    Started {
        execution_id: FlowExecutionId,
        flow_name: String,
    },
    /// A Flow execution was aborted.
    Aborted { execution_id: FlowExecutionId },
}