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
//! Independently guarded repeat-emission source. No counter-derived or full-history claims.
use super::{Database, DatabaseValue, Error};
use bcode_workflow::{WorkflowRepeatOutcomeSummary, WorkflowSemanticHistoryCoverage as Coverage};
use switchy_database::{
    DatabaseTransaction,
    query::FilterableQuery,
    schema::{
        Column, DataType, create_table,
        write_guard::{
            GuardEvent, GuardPredicate as P, GuardRelation, GuardScalar as S, GuardValue as V,
            WriteGuard,
        },
    },
};

const ORIGINS: &str = "workflow_repeat_event_origins_v1";
const PROOFS: &str = "workflow_repeat_event_proofs_v1";
const SOURCE: &str = "workflow_repeat_outcomes_v1";
const EVENT_TYPE: &str = "repeat_emission_v1";

fn columns(table: &str) -> Vec<Column> {
    let names = if table == ORIGINS {
        vec![("run_id", DataType::Text), ("version", DataType::Int)]
    } else {
        vec![
            ("output_id", DataType::Text),
            ("run_id", DataType::Text),
            ("identity", DataType::Text),
            ("summary_json", DataType::Text),
            ("recorded_at_ms", DataType::Int),
        ]
    };
    names
        .into_iter()
        .map(|(name, data_type)| Column {
            name: name.into(),
            data_type,
            nullable: false,
            auto_increment: false,
            default: None,
        })
        .collect()
}
fn relation(table: &str, alias: &str, predicate: P) -> GuardRelation {
    GuardRelation {
        table: table.into(),
        alias: alias.into(),
        joins: vec![],
        predicate: Box::new(predicate),
    }
}
fn eq(alias: &str, column: &str) -> P {
    P::Equal(
        V::Column {
            relation: alias.into(),
            column: column.into(),
        },
        V::NewColumn(column.into()),
    )
}
const fn constant(value: i64) -> V {
    V::Constant(S::Integer(value))
}
fn guards() -> Vec<WriteGuard> {
    let mut result = Vec::new();
    for table in [ORIGINS, PROOFS] {
        for event in [GuardEvent::BeforeUpdate, GuardEvent::BeforeDelete] {
            result.push(WriteGuard {
                name: format!("immutable_{table}_{event:?}"),
                table: table.into(),
                event,
                reject_when: P::Equal(constant(1), constant(1)),
                violation: "workflow_repeat_event_immutable".into(),
            });
        }
    }
    result.push(WriteGuard {
        name: "repeat_event_origin_requires_new_run_v1".into(),
        table: ORIGINS.into(),
        event: GuardEvent::BeforeInsert,
        reject_when: P::Any(vec![
            P::Not(Box::new(P::Equal(
                V::NewColumn("version".into()),
                constant(1),
            ))),
            P::Exists(relation("workflow_runs_v2", "run", eq("run", "run_id"))),
            P::Exists(relation("workflow_runs", "legacy", eq("legacy", "run_id"))),
            P::NotExists(relation(super::ORIGINS, "public", eq("public", "run_id"))),
            P::Exists(relation(SOURCE, "source", eq("source", "run_id"))),
        ]),
        violation: "workflow_repeat_event_origin_invalid".into(),
    });
    result.push(WriteGuard {
        name: "repeat_event_proof_requires_fact_v1".into(),
        table: PROOFS.into(),
        event: GuardEvent::BeforeInsert,
        reject_when: P::Any(vec![
            P::NotExists(relation(ORIGINS, "origin", eq("origin", "run_id"))),
            P::NotExists(relation(
                super::EVENTS,
                "fact",
                P::All(vec![
                    eq("fact", "run_id"),
                    eq("fact", "identity"),
                    eq("fact", "recorded_at_ms"),
                    P::Equal(
                        V::Column {
                            relation: "fact".into(),
                            column: "payload".into(),
                        },
                        V::NewColumn("summary_json".into()),
                    ),
                    P::Equal(
                        V::Column {
                            relation: "fact".into(),
                            column: "event_type".into(),
                        },
                        V::Constant(S::Text(EVENT_TYPE.into())),
                    ),
                ]),
            )),
        ]),
        violation: "workflow_repeat_event_missing_fact".into(),
    });
    result.push(WriteGuard {
        name: "repeat_event_source_requires_proof_v1".into(),
        table: SOURCE.into(),
        event: GuardEvent::BeforeInsert,
        reject_when: P::All(vec![
            P::Exists(relation(ORIGINS, "origin", eq("origin", "run_id"))),
            P::Any(vec![
                P::Not(Box::new(P::Equal(
                    V::NewColumn("version".into()),
                    constant(1),
                ))),
                P::NotExists(relation(
                    PROOFS,
                    "proof",
                    P::All(vec![
                        eq("proof", "run_id"),
                        eq("proof", "output_id"),
                        eq("proof", "summary_json"),
                    ]),
                )),
            ]),
        ]),
        violation: "workflow_repeat_event_missing_proof".into(),
    });
    result.extend(immutable_source_guards());
    result
}

fn immutable_source_guards() -> Vec<WriteGuard> {
    let mut result = Vec::new();
    for event in [GuardEvent::BeforeUpdate, GuardEvent::BeforeDelete] {
        let mut origins = vec![P::Exists(relation(
            ORIGINS,
            "old_origin",
            P::Equal(
                V::Column {
                    relation: "old_origin".into(),
                    column: "run_id".into(),
                },
                V::OldColumn("run_id".into()),
            ),
        ))];
        if event == GuardEvent::BeforeUpdate {
            // Prevent moving an unmarked historical row into a covered run.
            origins.push(P::Exists(relation(
                ORIGINS,
                "new_origin",
                eq("new_origin", "run_id"),
            )));
        }
        result.push(WriteGuard {
            name: format!("repeat_event_source_immutable_v1_{event:?}"),
            table: SOURCE.into(),
            event,
            reject_when: P::Any(origins),
            violation: "workflow_repeat_event_source_immutable".into(),
        });
    }
    result
}

/// Install this optional source contract in the caller's coordinated installation transaction.
/// Does not opt in existing runs or change the shared execution contract.
/// # Errors
/// Rejects partial/incompatible schema, unavailable typed guards or database failures.
pub async fn install_in(tx: &dyn DatabaseTransaction) -> Result<(), Error> {
    super::installation::verify(tx).await?;
    if tx.get_table_info(ORIGINS).await?.is_some() {
        return verify(tx).await;
    }
    if tx.get_table_info(PROOFS).await?.is_some() || tx.get_table_info(SOURCE).await?.is_none() {
        return Err(Error::MaintenanceRequired);
    }
    for table in [ORIGINS, PROOFS] {
        let mut statement = create_table(table);
        for column in columns(table) {
            statement = statement.column(column);
        }
        statement
            .primary_key(if table == ORIGINS {
                "run_id"
            } else {
                "output_id"
            })
            .execute(tx)
            .await?;
    }
    for guard in guards() {
        tx.install_write_guard(&guard).await?;
    }
    verify(tx).await
}
async fn verify(db: &dyn Database) -> Result<(), Error> {
    super::installation::verify(db).await?;
    for name in [ORIGINS, PROOFS] {
        let table = db
            .get_table_info(name)
            .await?
            .ok_or(Error::MaintenanceRequired)?;
        if table.columns.len() != columns(name).len() {
            return Err(Error::MaintenanceRequired);
        }
        for expected in columns(name) {
            let actual = table
                .columns
                .get(&expected.name)
                .ok_or(Error::MaintenanceRequired)?;
            let data_type = if expected.data_type == DataType::Int {
                DataType::BigInt
            } else {
                expected.data_type
            };
            if actual.data_type != data_type
                || actual.nullable
                || actual.auto_increment
                || actual.default_value.is_some()
                || actual.is_primary_key
                    != (expected.name
                        == if name == ORIGINS {
                            "run_id"
                        } else {
                            "output_id"
                        })
            {
                return Err(Error::MaintenanceRequired);
            }
        }
    }
    if !db.verify_write_guards(&guards()).await? {
        return Err(Error::MaintenanceRequired);
    }
    Ok(())
}

/// Opt in only before run insertion, after public-event origin insertion, in admission's transaction.
/// # Errors
/// Rejects historical/duplicate origins, missing installation or incompatible guards.
pub async fn enable_in(tx: &dyn DatabaseTransaction, run: &str) -> Result<(), Error> {
    verify(tx).await?;
    if run.is_empty() || run.len() > 256 {
        return Err(Error::InvalidAdmission);
    }
    tx.insert(ORIGINS)
        .value("run_id", run)
        .value("version", 1_i64)
        .execute(tx)
        .await?;
    Ok(())
}

/// Verify coverage of repeat-emission rows only, never broader control or workflow semantics.
/// # Errors
/// Rejects incompatible schema/origins and missing source guards.
pub async fn coverage(db: &dyn Database, run: &str) -> Result<Coverage, Error> {
    if db.get_table_info(ORIGINS).await?.is_none() {
        if db.get_table_info(PROOFS).await?.is_some() {
            return Err(Error::MaintenanceRequired);
        }
        return Ok(Coverage::Unsupported);
    }
    verify(db).await?;
    let rows = db
        .select(ORIGINS)
        .where_eq("run_id", run)
        .limit(1)
        .execute(db)
        .await?;
    match rows.first() {
        None => Ok(Coverage::HistoricalGap),
        Some(row) if row.get("version") == Some(DatabaseValue::Int64(1)) => {
            Ok(Coverage::CompleteFromAdmission)
        }
        Some(_) => Err(Error::MaintenanceRequired),
    }
}

/// Record the exact validated repeat summary before its source insertion, in the same fenced transaction.
///
/// Caller owns output/outcome validation and authority; this helper never dispatches or authorizes work.
/// Uses only `summary.created_at_ms`; conflicting duplicates, including changed time, fail closed.
/// # Errors
/// Rejects malformed summaries, conflicting identities, damaged coverage or database failures.
pub async fn record_in(
    tx: &dyn DatabaseTransaction,
    summary: &WorkflowRepeatOutcomeSummary,
) -> Result<(), Error> {
    if coverage(tx, &summary.run_id).await? != Coverage::CompleteFromAdmission {
        return Ok(());
    }
    let json = encode(summary)?;
    let identity = operation_identity(summary)?;
    super::append_encoded(
        tx,
        &summary.run_id,
        &identity,
        summary.created_at_ms,
        EVENT_TYPE,
        &json,
    )
    .await?;
    let rows = tx
        .select(PROOFS)
        .where_eq("output_id", summary.output_id.as_str())
        .limit(1)
        .execute(tx)
        .await?;
    if let Some(row) = rows.first() {
        if row.get("run_id") != Some(DatabaseValue::String(summary.run_id.clone()))
            || row.get("identity") != Some(DatabaseValue::String(identity))
            || row.get("summary_json") != Some(DatabaseValue::String(json))
            || row.get("recorded_at_ms")
                != Some(DatabaseValue::Int64(
                    i64::try_from(summary.created_at_ms).map_err(|_| Error::InvalidAdmission)?,
                ))
        {
            return Err(Error::MaintenanceRequired);
        }
        return Ok(());
    }
    tx.insert(PROOFS)
        .value("output_id", summary.output_id.as_str())
        .value("run_id", summary.run_id.as_str())
        .value("identity", identity)
        .value("summary_json", json)
        .value(
            "recorded_at_ms",
            i64::try_from(summary.created_at_ms).map_err(|_| Error::InvalidAdmission)?,
        )
        .execute(tx)
        .await?;
    Ok(())
}
fn operation_identity(summary: &WorkflowRepeatOutcomeSummary) -> Result<String, Error> {
    Ok(crate::sha256_hex(
        &serde_json::to_vec(&(EVENT_TYPE, &summary.run_id, &summary.output_id))
            .map_err(|_| Error::InvalidAdmission)?,
    ))
}
pub(super) fn validate_retained(
    summary: &WorkflowRepeatOutcomeSummary,
    run: &str,
    identity: &str,
    time: u64,
) -> Result<(), Error> {
    encode(summary).map_err(|_| Error::MaintenanceRequired)?;
    if summary.run_id != run
        || summary.created_at_ms != time
        || operation_identity(summary)? != identity
    {
        return Err(Error::MaintenanceRequired);
    }
    Ok(())
}
fn encode(summary: &WorkflowRepeatOutcomeSummary) -> Result<String, Error> {
    for (value, bound) in [
        (&summary.run_id, 256),
        (&summary.node_id, 256),
        (&summary.activation_id, 512),
        (&summary.output_id, 512),
    ] {
        if value.is_empty() || value.len() > bound {
            return Err(Error::InvalidAdmission);
        }
    }
    if summary.checksum_sha256.len() != 64
        || !summary
            .checksum_sha256
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
        || summary.effective_iteration_bound != summary.max_iterations.min(summary.cycle_cap)
        || summary.iterations_completed > summary.effective_iteration_bound
    {
        return Err(Error::InvalidAdmission);
    }
    let json = serde_json::to_string(summary).map_err(|_| Error::InvalidAdmission)?;
    if json.len() > 8192 {
        return Err(Error::InvalidAdmission);
    }
    Ok(json)
}

/// Separate repeat-source coverage and a bounded pin scan of public events.
/// Existing page scope declarations remain unchanged; the extra coverage concerns repeat emissions only.
#[derive(Debug)]
pub struct Page {
    /// Coverage checked in the same read snapshot as the event pin.
    pub repeat_coverage: Coverage,
    /// Existing independently scoped public diagnostic page, including safely withheld new event kinds.
    pub events: super::Page,
}

/// Read repeat coverage and public events in one snapshot; never install or repair.
/// # Errors
/// Rejects invalid cursors, incompatible origins/guards or damaged events.
pub async fn page(
    db: &dyn Database,
    version: u32,
    run: &str,
    after: u64,
    through: Option<u64>,
    limit: u16,
) -> Result<Page, Error> {
    let tx = db.begin_transaction().await?;
    let repeat_coverage = coverage(&*tx, run).await?;
    let events = super::page_in(&*tx, version, run, after, through, limit).await?;
    tx.commit().await?;
    Ok(Page {
        repeat_coverage,
        events,
    })
}

#[cfg(test)]
mod tests;