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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//! Exact owner-facing reads and fenced execution-session links.
pub use super::retry::{next_retry_attempt, schedule_retry};
use super::{
    Database, DatabaseValue, DispatchReceipt, Error, PreparedAttempt, WorkflowExecutionAuthority,
    attempt, authority, decode, encode, run, text,
};
use crate::{PreparedActivationDispatch, WorkflowExecutionSessionLink};
use switchy_database::{
    TransactionMode,
    query::{FilterableQuery, SortDirection},
    schema::{DataType, create_table},
};
const LINKS: &str = "workflow_execution_sessions_v2";
pub(super) async fn install(db: &dyn Database) -> Result<(), Error> {
    create_table(LINKS)
        .column(super::super::column(
            "dispatch_identity",
            DataType::Text,
            false,
        ))
        .primary_key("dispatch_identity")
        .column(super::super::column("link_json", DataType::Text, false))
        .execute(db)
        .await?;
    Ok(())
}
/// A bounded recovery candidate; ownership still requires trusted resolver evidence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecoverableRun {
    pub run_id: String,
    pub authority: WorkflowExecutionAuthority,
    pub recovery_required: bool,
}
/// Discover bounded nonterminal owned runs by stable run-ID keyset.
/// # Errors
/// Rejects unsupported representations, invalid bounds or malformed owner records.
pub async fn recoverable_runs(
    db: &dyn Database,
    after: Option<&str>,
    limit: u16,
) -> Result<(Vec<RecoverableRun>, Option<String>), Error> {
    if !(1..=100).contains(&limit) {
        return Err(Error::InvalidAdmission);
    }
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    let mut query = tx
        .select(super::RUNS)
        .columns(&["run_id", "authority_json"])
        .where_eq("status", "paused")
        .where_not_eq("authority_json", DatabaseValue::Null)
        .sort("run_id", SortDirection::Asc)
        .limit(usize::from(limit) + 1);
    if let Some(after) = after {
        crate::validate_id("run cursor", after).map_err(|_| Error::InvalidAdmission)?;
        query = query.where_gt("run_id", after);
    }
    let mut rows = query.execute(&*tx).await?;
    let more = rows.len() > usize::from(limit);
    rows.truncate(usize::from(limit));
    let cursor = if more {
        rows.last().map(|r| text(r, "run_id")).transpose()?
    } else {
        None
    };
    let mut result = Vec::with_capacity(rows.len());
    for row in rows {
        let run_id = text(&row, "run_id")?;
        let authority: WorkflowExecutionAuthority = decode(&row, "authority_json")?;
        let recovery_required = super::super::safety::recovery_required(&*tx, &run_id).await?;
        result.push(RecoverableRun {
            run_id,
            authority,
            recovery_required,
        });
    }
    tx.commit().await?;
    Ok((result, cursor))
}
/// Discover exact unresolved preparations/receipts under recovery owner's durable fence.
///
/// Receiptless items remain ambiguous; returning them never authorizes redispatch.
/// # Errors
/// Rejects stale authority, missing recovery barrier, invalid bounds or malformed attempt state.
pub async fn recovery_attempts(
    db: &dyn Database,
    run_id: &str,
    owner: &WorkflowExecutionAuthority,
    after: Option<&str>,
    limit: u16,
) -> Result<(Vec<super::PendingAttempt>, Option<String>), Error> {
    if !(1..=100).contains(&limit) {
        return Err(Error::InvalidAdmission);
    }
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    authority(&run(&*tx, run_id).await?, owner)?;
    if !super::super::safety::recovery_required(&*tx, run_id).await? {
        return Err(Error::AuthorityConflict);
    }
    let mut query = tx
        .select(super::TABLE)
        .where_eq("run_id", run_id)
        .where_eq("outcome_json", DatabaseValue::Null)
        .sort("dispatch_identity", SortDirection::Asc)
        .limit(usize::from(limit) + 1);
    if let Some(after) = after {
        crate::validate_id("dispatch cursor", after).map_err(|_| Error::InvalidAdmission)?;
        query = query.where_gt("dispatch_identity", after);
    }
    let mut rows = query.execute(&*tx).await?;
    let more = rows.len() > usize::from(limit);
    rows.truncate(usize::from(limit));
    let cursor = if more {
        rows.last()
            .map(|r| text(r, "dispatch_identity"))
            .transpose()?
    } else {
        None
    };
    let result = rows
        .iter()
        .map(|row| {
            let dispatch_identity = text(row, "dispatch_identity")?;
            let prepared = checked_prepared(row, run_id, &dispatch_identity)?;
            let receipt = if row.get("receipt_json") == Some(DatabaseValue::Null) {
                None
            } else {
                let receipt: DispatchReceipt = decode(row, "receipt_json")?;
                if receipt.run_id != prepared.run_id
                    || receipt.node_id != prepared.node_id
                    || receipt.activation_id != prepared.activation_id
                    || receipt.attempt != prepared.attempt
                    || receipt.dispatch_identity != dispatch_identity
                {
                    return Err(Error::MaintenanceRequired);
                }
                Some(receipt)
            };
            Ok(super::PendingAttempt {
                dispatch_identity,
                prepared,
                receipt,
            })
        })
        .collect::<Result<Vec<_>, Error>>()?;
    tx.commit().await?;
    Ok((result, cursor))
}

/// Inspect exact operator-retry facts under current ownership without scheduling or dispatching.
///
/// Returned next-attempt identity is not a retry grant. Caller must authorize and persist
/// the retry transition before returning a successful public retry result.
/// # Errors
/// Rejects stale authority, nonlatest/nonfailed/ambiguous attempts, exhausted policy or retired activation.
pub async fn retry_facts(
    db: &dyn Database,
    owner: &WorkflowExecutionAuthority,
    run_id: &str,
    node_id: &str,
    activation_id: &str,
    failed_attempt: u32,
) -> Result<bcode_workflow::WorkflowNodeRetryResult, Error> {
    let tx = db.begin_transaction().await?;
    let result =
        retry_facts_in(&*tx, owner, run_id, node_id, activation_id, failed_attempt).await?;
    tx.commit().await?;
    Ok(result)
}

pub(super) async fn retry_facts_in(
    tx: &dyn switchy_database::DatabaseTransaction,
    owner: &WorkflowExecutionAuthority,
    run_id: &str,
    node_id: &str,
    activation_id: &str,
    failed_attempt: u32,
) -> Result<bcode_workflow::WorkflowNodeRetryResult, Error> {
    super::super::verified(tx).await?;
    let root = run(tx, run_id).await?;
    authority(&root, owner)?;
    super::super::safety::ensure_admission_allowed(tx, run_id).await?;
    if !matches!(text(&root, "status")?.as_str(), "paused" | "failed") {
        return Err(Error::InvalidAdmission);
    }
    let rows = tx
        .select(super::TABLE)
        .where_eq("run_id", run_id)
        .where_eq("node_id", node_id)
        .where_eq("activation_id", activation_id)
        .sort("attempt_number", SortDirection::Desc)
        .limit(1)
        .execute(tx)
        .await?;
    let row = rows.first().ok_or(Error::InvalidAdmission)?;
    let identity = text(row, "dispatch_identity")?;
    let previous = checked_prepared(row, run_id, &identity)?;
    if previous.attempt != failed_attempt
        || row.get("outcome_json")
            != Some(DatabaseValue::String(encode(
                &super::TerminalOutcome::Failed,
            )?))
    {
        return Err(Error::InvalidAdmission);
    }
    let receipt: DispatchReceipt = decode(row, "receipt_json")?;
    if receipt.dispatch_identity != identity
        || receipt.run_id != run_id
        || receipt.node_id != node_id
        || receipt.activation_id != activation_id
        || receipt.attempt != failed_attempt
    {
        return Err(Error::MaintenanceRequired);
    }
    let limits = super::super::admission_projection::limits(&root)?;
    let limits = super::super::allowance::effective_limits(tx, run_id, &limits).await?;
    let lineage = tx
        .select("workflow_lineage_v2")
        .columns(&["root_run_id"])
        .where_eq("run_id", run_id)
        .limit(1)
        .execute(tx)
        .await?;
    let root_id = text(
        lineage.first().ok_or(Error::MaintenanceRequired)?,
        "root_run_id",
    )?;
    let root_row = run(tx, &root_id).await?;
    let root_baseline = super::super::admission_projection::limits(&root_row)?;
    let root_limits =
        super::super::allowance::effective_limits(tx, &root_id, &root_baseline).await?;
    let counters = tx
        .select("workflow_root_allowances_v2")
        .columns(&["consumed"])
        .where_eq("root_run_id", root_id)
        .limit(1)
        .execute(tx)
        .await?;
    if super::integer(
        counters.first().ok_or(Error::MaintenanceRequired)?,
        "consumed",
    )?
    .cast_unsigned()
        >= root_limits.node_execution_cap
    {
        return Err(Error::CapacityUnavailable);
    }
    let next = failed_attempt
        .checked_add(1)
        .ok_or(Error::CapacityUnavailable)?;
    if failed_attempt > limits.retry_cap
        || super::integer(&root, "consumed")?.cast_unsigned() >= limits.node_execution_cap
    {
        return Err(Error::CapacityUnavailable);
    }
    if !super::super::graph::is_ready_activation(tx, run_id, node_id, activation_id).await? {
        return Err(Error::InvalidAdmission);
    }
    Ok(bcode_workflow::WorkflowNodeRetryResult {
        run_id: run_id.into(),
        node_id: node_id.into(),
        activation_id: activation_id.into(),
        previous_attempt: failed_attempt,
        next_attempt: next,
    })
}

/// Read exact persisted authority; absence grants no authority.
/// # Errors
/// Rejects unsupported or malformed state and database failure.
pub async fn execution_authority(
    db: &dyn Database,
    id: &str,
) -> Result<Option<WorkflowExecutionAuthority>, Error> {
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    let row = run(&*tx, id).await?;
    let result = match row.get("authority_json") {
        Some(DatabaseValue::Null) => None,
        _ => Some(decode(&row, "authority_json")?),
    };
    tx.commit().await?;
    Ok(result)
}
/// Verify exact current authority without claiming liveness.
/// # Errors
/// Rejects stale ownership and unsupported or malformed state.
pub async fn verify(
    db: &dyn Database,
    id: &str,
    owner: &WorkflowExecutionAuthority,
) -> Result<(), Error> {
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    authority(&run(&*tx, id).await?, owner)?;
    tx.commit().await?;
    Ok(())
}
/// Read an exact persisted preparation, including settled attempts. Never authorizes replay.
/// # Errors
/// Rejects wrong run, corrupt normalized identity, oversized payload or unsupported state.
pub async fn prepared(
    db: &dyn Database,
    run_id: &str,
    dispatch_identity: &str,
) -> Result<Option<PreparedAttempt>, Error> {
    crate::validate_id("run_id", run_id).map_err(|_| Error::InvalidAdmission)?;
    crate::validate_id("dispatch_identity", dispatch_identity)
        .map_err(|_| Error::InvalidAdmission)?;
    let tx = db.begin_transaction().await?;
    let result = prepared_in(&*tx, run_id, dispatch_identity).await?;
    tx.commit().await?;
    Ok(result)
}

/// Read one exact prepared attempt in the caller's snapshot.
/// # Errors
/// Rejects invalid identities, unsupported state and inconsistent persisted identity.
pub async fn prepared_in(
    tx: &dyn switchy_database::DatabaseTransaction,
    run_id: &str,
    dispatch_identity: &str,
) -> Result<Option<PreparedAttempt>, Error> {
    crate::validate_id("run_id", run_id).map_err(|_| Error::InvalidAdmission)?;
    crate::validate_id("dispatch_identity", dispatch_identity)
        .map_err(|_| Error::InvalidAdmission)?;
    super::super::verified(tx).await?;
    attempt(tx, dispatch_identity)
        .await?
        .map(|row| checked_prepared(&row, run_id, dispatch_identity))
        .transpose()
}
fn checked_prepared(
    row: &switchy_database::Row,
    run_id: &str,
    identity: &str,
) -> Result<PreparedAttempt, Error> {
    let json = text(row, "prepared_json")?;
    if json.len() > super::super::MAX_ADMISSION_BYTES {
        return Err(Error::MaintenanceRequired);
    }
    let prepared: PreparedAttempt =
        serde_json::from_str(&json).map_err(|_| Error::MaintenanceRequired)?;
    if prepared.run_id != run_id
        || text(row, "run_id")? != run_id
        || prepared.dispatch_identity() != identity
        || text(row, "node_id")? != prepared.node_id
        || text(row, "activation_id")? != prepared.activation_id
        || super::integer(row, "attempt_number")? != i64::from(prepared.attempt)
    {
        return Err(Error::InvalidAdmission);
    }
    Ok(prepared)
}
/// Read the exact latest persisted attempt for an activation; None means no attempt exists.
///
/// This reports history, not permission to retry or redispatch ambiguous work.
/// # Errors
/// Rejects invalid identities, unsupported state or inconsistent persisted identity.
pub async fn latest_prepared(
    db: &dyn Database,
    run_id: &str,
    node_id: &str,
    activation_id: &str,
) -> Result<Option<PreparedAttempt>, Error> {
    for id in [run_id, node_id, activation_id] {
        crate::validate_id("attempt identity", id).map_err(|_| Error::InvalidAdmission)?;
    }
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    let rows = tx
        .select(super::TABLE)
        .where_eq("run_id", run_id)
        .where_eq("node_id", node_id)
        .where_eq("activation_id", activation_id)
        .sort("attempt_number", SortDirection::Desc)
        .limit(1)
        .execute(&*tx)
        .await?;
    let result = rows
        .first()
        .map(|row| checked_prepared(row, run_id, &text(row, "dispatch_identity")?))
        .transpose()?;
    tx.commit().await?;
    Ok(result)
}

/// Read an exact canonical terminal outcome without inferring success from missing state.
///
/// `None` means the existing attempt is nonterminal. A missing attempt is an error.
/// This observation grants no authority to dispatch, retry, or count progress.
/// # Errors
/// Rejects absent attempts, unsupported state, malformed outcomes or inconsistent receipt identities.
pub async fn terminal_outcome(
    db: &dyn Database,
    run_id: &str,
    dispatch_identity: &str,
) -> Result<Option<super::TerminalOutcome>, Error> {
    crate::validate_id("run_id", run_id).map_err(|_| Error::InvalidAdmission)?;
    crate::validate_id("dispatch_identity", dispatch_identity)
        .map_err(|_| Error::InvalidAdmission)?;
    let tx = db.begin_transaction().await?;
    match terminal_outcome_in(&*tx, run_id, dispatch_identity).await {
        Ok(outcome) => {
            tx.commit().await?;
            Ok(outcome)
        }
        Err(error) => {
            tx.rollback().await?;
            Err(error)
        }
    }
}

/// Read the exact terminal outcome inside the caller's snapshot, without a nested transaction.
///
/// Preserves receipt, identity, timestamp and compatibility validation; never commits,
/// rolls back, repairs or acquires execution authority.
/// # Errors
/// Rejects invalid/missing attempts, inconsistent receipts, unsupported storage and database failures.
pub async fn terminal_outcome_in(
    db: &dyn Database,
    run_id: &str,
    dispatch_identity: &str,
) -> Result<Option<super::TerminalOutcome>, Error> {
    Ok(attempt_observation_in(db, run_id, dispatch_identity)
        .await?
        .outcome)
}

/// Verified canonical attempt ledger transitions without exposing them publicly as payloads.
pub struct AttemptLedgerObservation {
    /// Verified terminal outcome.
    pub outcome: Option<super::TerminalOutcome>,
    /// Verified admission receipt.
    pub receipt: Option<DispatchReceipt>,
    /// Canonical terminal timestamp.
    pub terminal_at_ms: Option<u64>,
}

/// Read canonical stage facts inside the caller's snapshot.
/// # Errors
/// Rejects inconsistent identities, receipts, timestamps and unsupported storage.
pub async fn attempt_observation_in(
    db: &dyn Database,
    run_id: &str,
    dispatch_identity: &str,
) -> Result<AttemptLedgerObservation, Error> {
    crate::validate_id("run_id", run_id).map_err(|_| Error::InvalidAdmission)?;
    crate::validate_id("dispatch_identity", dispatch_identity)
        .map_err(|_| Error::InvalidAdmission)?;
    super::super::verified(db).await?;
    run(db, run_id).await?;
    let row = attempt(db, dispatch_identity)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    let prepared = checked_prepared(&row, run_id, dispatch_identity)?;
    let receipt = match row.get("receipt_json") {
        Some(DatabaseValue::Null) => None,
        Some(DatabaseValue::String(json)) if json.len() <= super::super::MAX_ADMISSION_BYTES => {
            let receipt: DispatchReceipt =
                serde_json::from_str(&json).map_err(|_| Error::MaintenanceRequired)?;
            if receipt.dispatch_identity != dispatch_identity
                || receipt.run_id != run_id
                || receipt.node_id != prepared.node_id
                || receipt.activation_id != prepared.activation_id
                || receipt.attempt != prepared.attempt
                || receipt.admitted_at_ms < prepared.prepared_at_ms
            {
                return Err(Error::MaintenanceRequired);
            }
            Some(receipt)
        }
        _ => return Err(Error::MaintenanceRequired),
    };
    let outcome = match row.get("outcome_json") {
        Some(DatabaseValue::Null) if row.get("terminal_at_ms") == Some(DatabaseValue::Null) => None,
        Some(DatabaseValue::String(json)) if json.len() <= super::super::MAX_ADMISSION_BYTES => {
            let receipt = receipt.as_ref().ok_or(Error::MaintenanceRequired)?;
            if super::integer(&row, "terminal_at_ms")?.cast_unsigned() < receipt.admitted_at_ms {
                return Err(Error::MaintenanceRequired);
            }
            Some(serde_json::from_str(&json).map_err(|_| Error::MaintenanceRequired)?)
        }
        _ => return Err(Error::MaintenanceRequired),
    };
    let terminal_at_ms = if outcome.is_some() {
        Some(super::integer(&row, "terminal_at_ms")?.cast_unsigned())
    } else {
        None
    };
    Ok(AttemptLedgerObservation {
        outcome,
        receipt,
        terminal_at_ms,
    })
}

/// Read an exact committed intent, including settled attempts, without authorizing replay.
/// # Errors
/// Rejects wrong run, malformed identity/payload, oversized intent or unsupported representation.
pub async fn dispatch_intent(
    db: &dyn Database,
    run_id: &str,
    dispatch_identity: &str,
) -> Result<serde_json::Value, Error> {
    crate::validate_id("run_id", run_id).map_err(|_| Error::InvalidAdmission)?;
    crate::validate_id("dispatch_identity", dispatch_identity)
        .map_err(|_| Error::InvalidAdmission)?;
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    run(&*tx, run_id).await?;
    let row = attempt(&*tx, dispatch_identity)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    let json = text(&row, "prepared_json")?;
    if json.len() > super::super::MAX_ADMISSION_BYTES {
        return Err(Error::MaintenanceRequired);
    }
    let prepared: PreparedAttempt =
        serde_json::from_str(&json).map_err(|_| Error::MaintenanceRequired)?;
    if prepared.run_id != run_id
        || text(&row, "run_id")? != run_id
        || prepared.dispatch_identity() != dispatch_identity
    {
        return Err(Error::InvalidAdmission);
    }
    tx.commit().await?;
    Ok(prepared.intent)
}

/// Read a durable receipt without authorizing replay.
/// # Errors
/// Rejects malformed receipt or database failure.
pub async fn receipt(db: &dyn Database, id: &str) -> Result<Option<DispatchReceipt>, Error> {
    let tx = db.begin_transaction().await?;
    let result = receipt_in(&*tx, id).await?;
    tx.commit().await?;
    Ok(result)
}

/// Read an immutable dispatch receipt within the caller's snapshot.
/// # Errors
/// Rejects unsupported storage, malformed receipts and query failures.
pub async fn receipt_in(
    tx: &dyn switchy_database::DatabaseTransaction,
    id: &str,
) -> Result<Option<DispatchReceipt>, Error> {
    super::super::verified(tx).await?;
    match attempt(tx, id).await? {
        Some(row) if row.get("receipt_json") != Some(DatabaseValue::Null) => {
            Ok(Some(decode(&row, "receipt_json")?))
        }
        _ => Ok(None),
    }
}
/// Recheck exact persisted intent, owner, activation and cancellation before external admission.
/// Existing receipts are returned for deduplication, never as permission to repeat an effect.
/// # Errors
/// Rejects stale ownership, changed intent/graph, cancellation, terminal work or malformed state.
pub async fn check_owner_admission(
    db: &dyn Database,
    request: &PreparedActivationDispatch,
    owner: &WorkflowExecutionAuthority,
) -> Result<Option<serde_json::Value>, Error> {
    let tx = db
        .begin_transaction_with_mode(TransactionMode::Immediate)
        .await?;
    super::super::verified(&*tx).await?;
    let root = run(&*tx, &request.activation.run_id).await?;
    authority(&root, owner)?;
    let row = attempt(&*tx, &request.dispatch_identity)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    let prepared: PreparedAttempt = decode(&row, "prepared_json")?;
    if prepared.dispatch_identity() != request.dispatch_identity
        || prepared.run_id != request.activation.run_id
        || prepared.node_id != request.activation.node_id
        || prepared.activation_id != request.activation.activation_id
        || prepared.attempt != request.attempt
        || prepared.intent != request.intent
    {
        return Err(Error::InvalidAdmission);
    }
    if row.get("receipt_json") != Some(DatabaseValue::Null) {
        let receipt: DispatchReceipt = decode(&row, "receipt_json")?;
        tx.commit().await?;
        return Ok(Some(receipt.receipt));
    }
    if text(&root, "status")? != "paused" || row.get("outcome_json") != Some(DatabaseValue::Null) {
        return Err(Error::InvalidAdmission);
    }
    super::super::safety::ensure_admission_allowed(&*tx, &prepared.run_id).await?;
    super::ensure_activation_admission(&*tx, &prepared.run_id, &prepared.activation_id).await?;
    if !super::super::graph::is_ready_activation(
        &*tx,
        &prepared.run_id,
        &prepared.node_id,
        &prepared.activation_id,
    )
    .await?
    {
        return Err(Error::InvalidAdmission);
    }
    let node =
        super::super::graph::execution_node(&*tx, &prepared.run_id, &prepared.node_id).await?;
    if node != request.activation.node {
        return Err(Error::InvalidAdmission);
    }
    tx.commit().await?;
    Ok(None)
}
/// Read the exact execution-session relationship, not the run's detachable association.
/// # Errors
/// Rejects malformed link and database failure.
pub async fn execution_session_link(
    db: &dyn Database,
    run_id: &str,
    node: &str,
    activation: &str,
    number: u32,
) -> Result<Option<WorkflowExecutionSessionLink>, Error> {
    crate::validate_execution_session_link_identity(run_id, node, activation, number)
        .map_err(|_| Error::InvalidAdmission)?;
    let tx = db.begin_transaction().await?;
    let result = execution_session_link_in(&*tx, run_id, node, activation, number).await?;
    tx.commit().await?;
    Ok(result)
}

/// Read an exact execution-session link inside the caller's read snapshot.
/// # Errors
/// Rejects invalid identity, unsupported representation, malformed storage or query failures.
pub async fn execution_session_link_in(
    tx: &dyn switchy_database::DatabaseTransaction,
    run_id: &str,
    node: &str,
    activation: &str,
    number: u32,
) -> Result<Option<WorkflowExecutionSessionLink>, Error> {
    crate::validate_execution_session_link_identity(run_id, node, activation, number)
        .map_err(|_| Error::InvalidAdmission)?;
    super::super::verified(tx).await?;
    let id = crate::dispatch_identity(run_id, node, activation, number);
    let rows = tx
        .select(LINKS)
        .where_eq("dispatch_identity", id)
        .limit(1)
        .execute(tx)
        .await?;
    let result = rows
        .first()
        .map(|row| decode(row, "link_json"))
        .transpose()?;
    Ok(result)
}
/// Persist an exact execution-session link under current authority in one transaction.
/// Detaching the parent association does not rewrite historical execution-session identity.
/// # Errors
/// Rejects conflicting duplicates, stale authority, cancellation, wrong workspace or missing attempt.
pub async fn persist_execution_session_link(
    db: &dyn Database,
    owner: &WorkflowExecutionAuthority,
    link: &WorkflowExecutionSessionLink,
) -> Result<(), Error> {
    crate::validate_execution_session_link(link).map_err(|_| Error::InvalidAdmission)?;
    let tx = db
        .begin_transaction_with_mode(TransactionMode::Immediate)
        .await?;
    super::super::verified(&*tx).await?;
    let root = run(&*tx, &link.run_id).await?;
    authority(&root, owner)?;
    let id = crate::dispatch_identity(
        &link.run_id,
        &link.node_id,
        &link.activation_id,
        link.attempt,
    );
    let encoded = encode(link)?;
    let rows = tx
        .select(LINKS)
        .where_eq("dispatch_identity", id.as_str())
        .limit(1)
        .execute(&*tx)
        .await?;
    if let Some(row) = rows.first() {
        if text(row, "link_json")? != encoded {
            return Err(Error::InvalidAdmission);
        }
        tx.commit().await?;
        return Ok(());
    }
    super::super::safety::ensure_admission_allowed(&*tx, &link.run_id).await?;
    let admission: crate::NewWorkflowRun = decode(&root, "admission_json")?;
    let row = attempt(&*tx, &id).await?.ok_or(Error::InvalidAdmission)?;
    let prepared: PreparedAttempt = decode(&row, "prepared_json")?;
    if text(&root, "status")? != "paused"
        || admission.workspace_snapshot != link.workspace_snapshot
        || row.get("outcome_json") != Some(DatabaseValue::Null)
        || prepared.dispatch_identity() != id
        || link.created_at_ms < prepared.prepared_at_ms
    {
        return Err(Error::InvalidAdmission);
    }
    tx.insert(LINKS)
        .value("dispatch_identity", id)
        .value("link_json", encoded)
        .execute(&*tx)
        .await?;
    tx.commit().await?;
    Ok(())
}
async fn activation_cancellation_intended(
    db: &dyn Database,
    run_id: &str,
    activation_id: &str,
) -> Result<bool, Error> {
    Ok(!db
        .select("workflow_parallel_cancellations_v2")
        .columns(&["activation_id"])
        .where_eq("run_id", run_id)
        .where_eq("activation_id", activation_id)
        .limit(1)
        .execute(db)
        .await?
        .is_empty())
}
/// Observe whole-run or exact parallel-member cancellation intent for one persisted dispatch.
/// # Errors
/// Rejects stale authority, missing/mismatched attempts and unsupported durable state.
pub async fn cancellation_intended(
    db: &dyn Database,
    run_id: &str,
    dispatch_identity: &str,
    owner: &WorkflowExecutionAuthority,
) -> Result<bool, Error> {
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    authority(&run(&*tx, run_id).await?, owner)?;
    let row = attempt(&*tx, dispatch_identity)
        .await?
        .ok_or(Error::InvalidAdmission)?;
    let prepared = checked_prepared(&row, run_id, dispatch_identity)?;
    let intended = super::super::safety::cancellation_requested_at(&*tx, run_id)
        .await?
        .is_some()
        || activation_cancellation_intended(&*tx, run_id, &prepared.activation_id).await?;
    tx.commit().await?;
    Ok(intended)
}
/// Discover only receipt-backed unsettled dispatches targeted by parallel-member cancellation.
///
/// Scans a bounded dispatch page; an empty result with a cursor is not exhaustion.
/// Neither widens cancellation to the run nor changes outcomes or resource reservations.
/// # Errors
/// Rejects stale authority, invalid bounds/cursors, inconsistent receipts or database failures.
pub async fn parallel_cancellation_receipts(
    db: &dyn Database,
    run_id: &str,
    owner: &WorkflowExecutionAuthority,
    after: Option<&str>,
    limit: usize,
) -> Result<(Vec<DispatchReceipt>, Option<String>), Error> {
    if !(1..=128).contains(&limit) {
        return Err(Error::InvalidAdmission);
    }
    if let Some(after) = after {
        crate::validate_id("dispatch cursor", after).map_err(|_| Error::InvalidAdmission)?;
    }
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    authority(&run(&*tx, run_id).await?, owner)?;
    let mut query = tx
        .select(super::TABLE)
        .where_eq("run_id", run_id)
        .where_eq("outcome_json", DatabaseValue::Null)
        .sort("dispatch_identity", SortDirection::Asc)
        .limit(limit + 1);
    if let Some(after) = after {
        query = query.where_gt("dispatch_identity", after);
    }
    let mut rows = query.execute(&*tx).await?;
    let more = rows.len() > limit;
    rows.truncate(limit);
    let cursor = if more {
        rows.last()
            .map(|r| text(r, "dispatch_identity"))
            .transpose()?
    } else {
        None
    };
    let mut receipts = Vec::new();
    for row in rows {
        let identity = text(&row, "dispatch_identity")?;
        let prepared = checked_prepared(&row, run_id, &identity)?;
        if row.get("receipt_json") == Some(DatabaseValue::Null)
            || !activation_cancellation_intended(&*tx, run_id, &prepared.activation_id).await?
        {
            continue;
        }
        let receipt: DispatchReceipt = decode(&row, "receipt_json")?;
        if receipt.run_id != run_id
            || receipt.node_id != prepared.node_id
            || receipt.activation_id != prepared.activation_id
            || receipt.attempt != prepared.attempt
            || receipt.dispatch_identity != identity
        {
            return Err(Error::MaintenanceRequired);
        }
        receipts.push(receipt);
    }
    tx.commit().await?;
    Ok((receipts, cursor))
}

/// Page receipt-backed outstanding targets after a durable cancellation request.
/// # Errors
/// Rejects stale authority, absent cancellation intent, invalid bounds or malformed receipts.
pub async fn cancellation_receipts(
    db: &dyn Database,
    run_id: &str,
    owner: &WorkflowExecutionAuthority,
    after: Option<&str>,
    limit: usize,
) -> Result<(Vec<DispatchReceipt>, Option<String>), Error> {
    if !(1..=128).contains(&limit) {
        return Err(Error::InvalidAdmission);
    }
    let tx = db.begin_transaction().await?;
    super::super::verified(&*tx).await?;
    authority(&run(&*tx, run_id).await?, owner)?;
    if super::super::safety::cancellation_requested_at(&*tx, run_id)
        .await?
        .is_none()
    {
        return Err(Error::InvalidAdmission);
    }
    let mut query = tx
        .select(super::TABLE)
        .where_eq("run_id", run_id)
        .where_eq("outcome_json", DatabaseValue::Null)
        .sort("dispatch_identity", SortDirection::Asc)
        .limit(limit + 1);
    if let Some(after) = after {
        query = query.where_gt("dispatch_identity", after);
    }
    let mut rows = query.execute(&*tx).await?;
    let more = rows.len() > limit;
    rows.truncate(limit);
    let cursor = if more {
        rows.last()
            .map(|row| text(row, "dispatch_identity"))
            .transpose()?
    } else {
        None
    };
    let receipts = rows
        .iter()
        .filter(|row| row.get("receipt_json") != Some(DatabaseValue::Null))
        .map(|row| decode(row, "receipt_json"))
        .collect::<Result<Vec<_>, _>>()?;
    tx.commit().await?;
    Ok((receipts, cursor))
}