bcode_workflow_store 0.0.1-alpha.0

Durable workflow persistence for bcode
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Incremental homogeneous fan-out materialization and member capacity reservation.
use super::{
    Database, DatabaseValue, Error, FilterableQuery, SortDirection, TransactionMode, Value,
    WorkflowExecutionAuthority, decode, encode, text,
};
use switchy_database::{
    DatabaseTransaction,
    schema::{DataType, create_index, create_table},
};
const CONTROLLERS: &str = "workflow_fanout_v2";
#[cfg(test)]
mod tests;
const MEMBERS: &str = "workflow_fanout_members_v2";
const CLAIMS: &str = "workflow_fanout_claims_v2";
pub(super) async fn install(db: &dyn Database) -> Result<(), Error> {
    super::fanout_progress::install(db).await?;
    create_table(CONTROLLERS)
        .column(super::super::column("activation_id", DataType::Text, false))
        .primary_key("activation_id")
        .column(super::super::column("run_id", DataType::Text, false))
        .column(super::super::column("node_id", DataType::Text, false))
        .column(super::super::column("next_index", DataType::Int, false))
        .column(super::super::column("active", DataType::Int, false))
        .column(super::super::column("cap", DataType::Int, false))
        .execute(db)
        .await?;
    create_table(MEMBERS)
        .column(super::super::column("node_id", DataType::Text, false))
        .primary_key("node_id")
        .column(super::super::column("run_id", DataType::Text, false))
        .column(super::super::column("controller", DataType::Text, false))
        .column(super::super::column("member_index", DataType::Int, false))
        .column(super::super::column("node_json", DataType::Text, false))
        .execute(db)
        .await?;
    create_index("idx_workflow_fanout_members_controller")
        .table(MEMBERS)
        .columns(vec!["run_id", "controller", "member_index"])
        .execute(db)
        .await?;
    create_table(CLAIMS)
        .column(super::super::column("activation_id", DataType::Text, false))
        .primary_key("activation_id")
        .column(super::super::column("controller", DataType::Text, false))
        .execute(db)
        .await?;
    Ok(())
}
/// Materialize at most `limit` exact member activations, retaining original input order.
/// # Errors
/// Rejects stale controller/owner/revision, invalid config/input, cancellation or storage failures.
pub async fn materialize(
    db: &dyn Database,
    owner: &WorkflowExecutionAuthority,
    run: &str,
    node: &str,
    id: &str,
    revision: u64,
    limit: u16,
) -> Result<usize, Error> {
    if limit == 0 || limit > 100 {
        return Err(Error::InvalidAdmission);
    }
    let tx = db
        .begin_transaction_with_mode(TransactionMode::Immediate)
        .await?;
    super::super::verified(&*tx).await?;
    let roots = tx
        .select(super::RUNS)
        .where_eq("run_id", run)
        .limit(1)
        .execute(&*tx)
        .await?;
    let root = roots.first().ok_or(Error::InvalidAdmission)?;
    if text(root, "authority_json")? != encode(owner)? {
        return Err(Error::AuthorityConflict);
    }
    let current = super::super::graph_pages::revision(&*tx, run).await?;
    if current != revision {
        return Err(Error::InvalidAdmission);
    }
    let target = super::super::graph_pages::node(&*tx, run, current, node)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    let config = configuration(&target)?;
    let activation = super::activation(&*tx, run, node)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    if text(&activation, "identity")? != id {
        return Err(Error::InvalidAdmission);
    }
    if activation.get("output_json") != Some(DatabaseValue::Null) {
        return Ok(0);
    }
    if text(root, "status")? != "paused" {
        return Err(Error::InvalidAdmission);
    }
    super::super::safety::ensure_admission_allowed(&*tx, run).await?;
    let input: Value = decode(&activation, "input_json")?;
    let items = input.as_array().ok_or(Error::InvalidAdmission)?;
    if items.len() > usize::try_from(config.max_members).map_err(|_| Error::InvalidAdmission)? {
        return Err(Error::InvalidAdmission);
    }
    let rows = tx
        .select(CONTROLLERS)
        .where_eq("activation_id", id)
        .limit(1)
        .execute(&*tx)
        .await?;
    let start = if let Some(row) = rows.first() {
        number(row, "next_index")?
    } else {
        tx.insert(CONTROLLERS)
            .value("activation_id", id)
            .value("run_id", run)
            .value("node_id", node)
            .value("next_index", 0_i64)
            .value("active", 0_i64)
            .value("cap", i64::from(config.max_concurrency))
            .execute(&*tx)
            .await?;
        0
    };
    let start = usize::try_from(start).map_err(|_| Error::MaintenanceRequired)?;
    let end = start.saturating_add(usize::from(limit)).min(items.len());
    for (index, item) in items.iter().enumerate().take(end).skip(start) {
        let name = format!(
            "fanout:{}",
            crate::sha256_hex(encode(&(run, id, index))?.as_bytes())
        );
        let mut member = *config.member_node.clone();
        member.id = name.clone();
        super::schema(&member.input.schema, item)?;
        tx.insert(MEMBERS)
            .value("node_id", name.as_str())
            .value("run_id", run)
            .value("controller", id)
            .value(
                "member_index",
                i64::try_from(index).map_err(|_| Error::InvalidAdmission)?,
            )
            .value("node_json", encode(&member)?)
            .execute(&*tx)
            .await?;
        super::insert_activation(&*tx, run, &name, item).await?;
    }
    tx.update(CONTROLLERS)
        .value(
            "next_index",
            i64::try_from(end).map_err(|_| Error::InvalidAdmission)?,
        )
        .where_eq("activation_id", id)
        .execute(&*tx)
        .await?;
    tx.commit().await?;
    Ok(end - start)
}
fn configuration(
    node: &bcode_workflow::NodeDefinition,
) -> Result<bcode_workflow::WorkflowFanOutConfiguration, Error> {
    if node.kind != bcode_workflow::NodeKind::FanOut {
        return Err(Error::InvalidAdmission);
    }
    let config: bcode_workflow::WorkflowFanOutConfiguration =
        serde_json::from_value(node.configuration.clone()).map_err(|_| Error::InvalidAdmission)?;
    config.validate().map_err(|_| Error::InvalidAdmission)?;
    Ok(config)
}

fn number(row: &switchy_database::Row, key: &str) -> Result<i64, Error> {
    match row.get(key) {
        Some(DatabaseValue::Int64(n)) if n >= 0 => Ok(n),
        _ => Err(Error::MaintenanceRequired),
    }
}
pub(super) async fn member_node(
    db: &dyn Database,
    run: &str,
    node: &str,
) -> Result<Option<bcode_workflow::NodeDefinition>, Error> {
    let rows = db
        .select(MEMBERS)
        .where_eq("run_id", run)
        .where_eq("node_id", node)
        .limit(1)
        .execute(db)
        .await?;
    rows.first().map(|row| decode(row, "node_json")).transpose()
}
/// Reserve virtual-member capacity in the same attempt transaction; ordinary nodes are unchanged.
/// # Errors
/// Rejects exhausted controller capacity, malformed lineage or conflicting storage state.
pub async fn reserve(
    tx: &dyn DatabaseTransaction,
    run: &str,
    node: &str,
    activation: &str,
) -> Result<(), Error> {
    let rows = tx
        .select(MEMBERS)
        .where_eq("run_id", run)
        .where_eq("node_id", node)
        .limit(1)
        .execute(tx)
        .await?;
    let Some(member) = rows.first() else {
        return Ok(());
    };
    let controller = text(member, "controller")?;
    if !tx
        .select(CLAIMS)
        .where_eq("activation_id", activation)
        .limit(1)
        .execute(tx)
        .await?
        .is_empty()
    {
        return Err(Error::InvalidAdmission);
    }
    let rows = tx
        .select(CONTROLLERS)
        .where_eq("activation_id", controller.as_str())
        .limit(1)
        .execute(tx)
        .await?;
    let row = rows.first().ok_or(Error::MaintenanceRequired)?;
    let active = number(row, "active")?;
    if active >= number(row, "cap")? {
        return Err(Error::CapacityUnavailable);
    }
    tx.insert(CLAIMS)
        .value("activation_id", activation)
        .value("controller", controller.as_str())
        .execute(tx)
        .await?;
    tx.update(CONTROLLERS)
        .value(
            "active",
            active.checked_add(1).ok_or(Error::CapacityUnavailable)?,
        )
        .where_eq("activation_id", controller)
        .execute(tx)
        .await?;
    Ok(())
}
/// Release member capacity only inside fenced authoritative attempt terminalization.
/// # Errors
/// Rejects inconsistent counters or storage failure.
pub async fn release(
    tx: &dyn DatabaseTransaction,
    _run: &str,
    _node: &str,
    activation: &str,
) -> Result<(), Error> {
    let claims = tx
        .select(CLAIMS)
        .where_eq("activation_id", activation)
        .limit(1)
        .execute(tx)
        .await?;
    let Some(claim) = claims.first() else {
        return Ok(());
    };
    let controller = text(claim, "controller")?;
    let rows = tx
        .select(CONTROLLERS)
        .where_eq("activation_id", controller.as_str())
        .limit(1)
        .execute(tx)
        .await?;
    let count = number(rows.first().ok_or(Error::MaintenanceRequired)?, "active")?
        .checked_sub(1)
        .filter(|n| *n >= 0)
        .ok_or(Error::MaintenanceRequired)?;
    tx.update(CONTROLLERS)
        .value("active", count)
        .where_eq("activation_id", controller)
        .execute(tx)
        .await?;
    tx.delete(CLAIMS)
        .where_eq("activation_id", activation)
        .execute(tx)
        .await?;
    Ok(())
}
/// Page canonical virtual member identities without loading all inputs.
/// # Errors
/// Rejects invalid bounds or database errors.
pub async fn members(
    db: &dyn Database,
    run: &str,
    controller: &str,
    after: Option<u32>,
    limit: u16,
) -> Result<Vec<String>, Error> {
    if limit == 0 || limit > 100 {
        return Err(Error::InvalidAdmission);
    }
    let mut query = db
        .select(MEMBERS)
        .columns(&["node_id"])
        .where_eq("run_id", run)
        .where_eq("controller", controller)
        .sort("member_index", SortDirection::Asc)
        .limit(usize::from(limit));
    if let Some(after) = after {
        query = query.where_gt("member_index", i64::from(after));
    }
    query
        .execute(db)
        .await?
        .iter()
        .map(|row| text(row, "node_id"))
        .collect()
}

/// Settle the controller only after all materialized members have canonical outputs.
///
/// Member discovery remains paged; result bytes are bounded by the normal output envelope.
/// # Errors
/// Rejects stale authority/revision, malformed ordering, invalid result schema or storage failure.
pub async fn settle(
    db: &dyn Database,
    owner: &WorkflowExecutionAuthority,
    run: &str,
    node: &str,
    id: &str,
    revision: u64,
) -> Result<bool, Error> {
    settle_with_policy(
        db,
        owner,
        run,
        node,
        id,
        revision,
        super::parallel_progress::OutputPolicy::default(),
    )
    .await
}
/// Settle with an explicit inline output byte budget, never a graph lifetime limit.
/// # Errors
/// Returns ordinary settlement errors or an exceeded output policy.
pub async fn settle_with_policy(
    db: &dyn Database,
    owner: &WorkflowExecutionAuthority,
    run: &str,
    node: &str,
    id: &str,
    revision: u64,
    output_policy: super::parallel_progress::OutputPolicy,
) -> Result<bool, Error> {
    let tx = db
        .begin_transaction_with_mode(TransactionMode::Immediate)
        .await?;
    super::super::verified(&*tx).await?;
    let root = owned_root(&*tx, run, owner).await?;
    let current = super::super::graph_pages::revision(&*tx, run).await?;
    let boundary = super::super::graph_edges::boundary(&*tx, run, current).await?;
    if current != revision {
        return Err(Error::InvalidAdmission);
    }
    let target = super::super::graph_pages::node(&*tx, run, current, node)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    let activation = super::activation(&*tx, run, node)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    if text(&activation, "identity")? != id {
        return Err(Error::InvalidAdmission);
    }
    if activation.get("output_json") != Some(DatabaseValue::Null) {
        return Ok(false);
    }
    if text(&root, "status")? != "paused" {
        return Err(Error::InvalidAdmission);
    }
    super::super::safety::ensure_admission_allowed(&*tx, run).await?;
    let input: Value = decode(&activation, "input_json")?;
    let count = input.as_array().ok_or(Error::InvalidAdmission)?.len();
    let controllers = tx
        .select(CONTROLLERS)
        .where_eq("activation_id", id)
        .limit(1)
        .execute(&*tx)
        .await?;
    let controller = controllers.first().ok_or(Error::InvalidAdmission)?;
    if usize::try_from(number(controller, "next_index")?).ok() != Some(count) {
        return Ok(false);
    }
    let config: bcode_workflow::WorkflowFanOutConfiguration =
        serde_json::from_value(target.configuration.clone())
            .map_err(|_| Error::InvalidAdmission)?;
    let values = if config.failure_policy == bcode_workflow::ParallelFailurePolicy::WaitAll {
        let Some(state) =
            super::fanout_progress::reduce(&*tx, run, id, count, output_policy).await?
        else {
            tx.commit().await?;
            return Ok(false);
        };
        if !state.pending.is_empty() {
            tx.commit().await?;
            return Ok(false);
        }
        if state.failed {
            super::control_state::fail(&*tx, run, &format!("fanout:{id}")).await?;
            tx.commit().await?;
            return Ok(false);
        }
        collected_values(state)?
    } else {
        if reconcile_failure(&*tx, run, id, config.failure_policy).await? {
            tx.commit().await?;
            return Ok(false);
        }
        let Some(values) = collect_outputs(&*tx, run, id, count).await? else {
            return Ok(false);
        };
        values
    };
    let value = serde_json::to_value(
        bcode_workflow::WorkflowFanOutResult::new(values)
            .map_err(|_| Error::MaintenanceRequired)?,
    )
    .map_err(|_| Error::InvalidAdmission)?;
    if encode(&value)?.len() > output_policy.max_inline_bytes {
        return Err(Error::InvalidAdmission);
    }
    super::schema(&target.output.schema, &value)?;
    super::terminal_scan::invalidate(&*tx, run).await?;
    tx.update(super::ACTIVATIONS)
        .value("output_json", encode(&value)?)
        .where_eq("identity", id)
        .execute(&*tx)
        .await?;
    super::completion::retain_control(&*tx, run, &target, id, &value).await?;
    let request: crate::NewWorkflowRun = decode(&root, "admission_json")?;
    super::propagate_paged(&*tx, &request, current, node, &value).await?;
    if boundary.exits.iter().any(|exit| exit == node) {
        super::schema(&boundary.output.schema, &value)?;
        tx.update(super::GRAPHS)
            .value("terminal_json", encode(&value)?)
            .where_eq("run_id", run)
            .execute(&*tx)
            .await?;
    }
    tx.commit().await?;
    Ok(true)
}

fn collected_values(
    state: super::parallel::MemberState,
) -> Result<Vec<bcode_workflow::WorkflowFanOutMember<Value>>, Error> {
    state
        .outcomes
        .into_iter()
        .enumerate()
        .map(|(index, (key, outcome))| {
            let parsed: usize = key.parse().map_err(|_| Error::MaintenanceRequired)?;
            if parsed != index {
                return Err(Error::MaintenanceRequired);
            }
            let value = outcome
                .get("value")
                .cloned()
                .ok_or(Error::MaintenanceRequired)?;
            Ok(bcode_workflow::WorkflowFanOutMember {
                index: u32::try_from(index).map_err(|_| Error::InvalidAdmission)?,
                value,
            })
        })
        .collect()
}

async fn reconcile_failure(
    db: &dyn DatabaseTransaction,
    run: &str,
    id: &str,
    policy: bcode_workflow::ParallelFailurePolicy,
) -> Result<bool, Error> {
    let mut after = None;
    let mut failed = false;
    let mut pending = false;
    loop {
        let page = members(db, run, id, after, 100).await?;
        if page.is_empty() {
            break;
        }
        for member in page {
            after = Some(after.map_or(0, |n| n + 1));
            if super::output(db, run, &member).await?.is_some() {
                continue;
            }
            if super::activation(db, run, &member)
                .await?
                .is_some_and(|row| row.get("skipped") == Some(DatabaseValue::Int64(1)))
            {
                continue;
            }
            let activation = super::activation_id(db, run, &member)
                .await?
                .ok_or(Error::MaintenanceRequired)?;
            let rows = db
                .select("workflow_attempts_v2")
                .where_eq("run_id", run)
                .where_eq("activation_id", activation)
                .sort("attempt_number", SortDirection::Desc)
                .limit(1)
                .execute(db)
                .await?;
            match rows.first().and_then(|row| row.get("outcome_json")) {
                Some(DatabaseValue::String(value))
                    if value == "\"Failed\"" || value == "\"Cancelled\"" =>
                {
                    failed = true;
                }
                _ => pending = true,
            }
        }
    }
    if !failed {
        return Ok(false);
    }
    if policy == bcode_workflow::ParallelFailurePolicy::FailFast {
        let mut after = None;
        loop {
            let page = members(db, run, id, after, 100).await?;
            if page.is_empty() {
                break;
            }
            for member in page {
                after = Some(after.map_or(0, |n| n + 1));
                if super::output(db, run, &member).await?.is_some() {
                    continue;
                }
                let activation = super::activation_id(db, run, &member)
                    .await?
                    .ok_or(Error::MaintenanceRequired)?;
                super::cancellation::cancel_in(db, run, &activation).await?;
            }
        }
    }
    if !pending {
        super::control_state::fail(db, run, &format!("fanout:{id}")).await?;
    }
    Ok(true)
}

async fn collect_outputs(
    db: &dyn Database,
    run: &str,
    id: &str,
    count: usize,
) -> Result<Option<Vec<bcode_workflow::WorkflowFanOutMember<Value>>>, Error> {
    let mut values = Vec::new();
    let mut after = None;
    let mut bytes = 0_usize;
    loop {
        let page = members(db, run, id, after, 100).await?;
        if page.is_empty() {
            break;
        }
        for member in page {
            let Some(value) = super::output(db, run, &member).await? else {
                return Ok(None);
            };
            bytes = bytes
                .checked_add(encode(&value)?.len())
                .ok_or(Error::InvalidAdmission)?;
            if bytes > super::super::MAX_ADMISSION_BYTES {
                return Err(Error::InvalidAdmission);
            }
            let index = u32::try_from(values.len()).map_err(|_| Error::InvalidAdmission)?;
            values.push(bcode_workflow::WorkflowFanOutMember { index, value });
            after = Some(index);
        }
    }
    if values.len() != count {
        return Err(Error::MaintenanceRequired);
    }
    Ok(Some(values))
}

async fn owned_root(
    db: &dyn Database,
    run: &str,
    owner: &WorkflowExecutionAuthority,
) -> Result<switchy_database::Row, Error> {
    let roots = db
        .select(super::RUNS)
        .where_eq("run_id", run)
        .limit(1)
        .execute(db)
        .await?;
    let root = roots.into_iter().next().ok_or(Error::InvalidAdmission)?;
    if text(&root, "authority_json")? != encode(owner)? {
        return Err(Error::AuthorityConflict);
    }
    Ok(root)
}