ff-script 0.6.1

FlowFabric typed FCALL wrappers and Lua library loader
Documentation
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
//! Typed FCALL wrappers for flow coordination functions (lua/flow.lua).

use ff_core::contracts::*;
use crate::error::ScriptError;
use ff_core::keys::{ExecKeyContext, FlowIndexKeys, FlowKeyContext, IndexKeys};
use ff_core::state::PublicState;

use crate::result::{FcallResult, FromFcallResult};

/// Key context for flow-structural operations on {fp:N}.
pub struct FlowStructOpKeys<'a> {
    pub fctx: &'a FlowKeyContext,
    pub fidx: &'a FlowIndexKeys,
}

/// Key context for child-local dependency operations on {p:N}.
///
/// `flow_ctx` is required for dependency ops (every dependency edge
/// belongs to a flow); it supplies the `edgegroup` key used by
/// RFC-016 Stage A. Post-RFC-011 the flow and exec partitions share
/// the `{fp:N}` hash tag so the FCALL is still single-slot.
pub struct DepOpKeys<'a> {
    pub ctx: &'a ExecKeyContext,
    pub idx: &'a IndexKeys,
    pub lane_id: &'a ff_core::types::LaneId,
    pub flow_ctx: &'a FlowKeyContext,
    pub downstream_eid: &'a ff_core::types::ExecutionId,
}

/// Extended key context for [`ff_resolve_dependency`], which needs
/// access to the upstream execution's result key for server-side
/// `data_passing_ref` resolution (Batch C item 3). Separate from
/// [`DepOpKeys`] so the other dependency wrappers —
/// `ff_apply_dependency_to_child`, `ff_evaluate_flow_eligibility`,
/// `ff_promote_blocked_to_eligible`, `ff_replay_execution` — don't
/// have to carry an upstream context they never use.
///
/// Upstream and downstream are co-located on the same `{fp:N}` slot
/// via flow membership (RFC-011 §7.3), so `upstream_ctx` builds the
/// upstream key on the child's partition.
pub struct ResolveDependencyKeys<'a> {
    pub ctx: &'a ExecKeyContext,
    pub idx: &'a IndexKeys,
    pub lane_id: &'a ff_core::types::LaneId,
    pub upstream_ctx: &'a ExecKeyContext,
    pub flow_ctx: &'a FlowKeyContext,
    pub flow_idx: &'a FlowIndexKeys,
    pub downstream_eid: &'a ff_core::types::ExecutionId,
}

// ─── ff_create_flow ──────────────────────────────────────────────────
// KEYS (3): flow_core, members_set, flow_index
// ARGV (4): flow_id, flow_kind, namespace, now_ms

ff_function! {
    pub ff_create_flow(args: CreateFlowArgs) -> CreateFlowResult {
        keys(k: &FlowStructOpKeys<'_>) {
            k.fctx.core(),
            k.fctx.members(),
            k.fidx.flow_index(),
        }
        argv {
            args.flow_id.to_string(),
            args.flow_kind.clone(),
            args.namespace.to_string(),
            args.now.to_string(),
        }
    }
}

impl FromFcallResult for CreateFlowResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        let fid_str = r.field_str(0);
        let fid = ff_core::types::FlowId::parse(&fid_str)
            .map_err(|e| ScriptError::Parse {
                fcall: "ff_create_flow".into(),
                execution_id: None,
                message: format!("bad flow_id: {e}"),
            })?;
        match r.status.as_str() {
            "OK" => Ok(CreateFlowResult::Created { flow_id: fid }),
            "ALREADY_SATISFIED" => Ok(CreateFlowResult::AlreadySatisfied { flow_id: fid }),
            _ => Err(ScriptError::Parse {
                fcall: "ff_create_flow".into(),
                execution_id: None,
                message: format!("unexpected status: {}", r.status),
            }),
        }
    }
}

// ─── ff_add_execution_to_flow ────────────────────────────────────────
// KEYS (3): flow_core, members_set, flow_index
// ARGV (3): flow_id, execution_id, now_ms

ff_function! {
    pub ff_add_execution_to_flow(args: AddExecutionToFlowArgs) -> AddExecutionToFlowResult {
        keys(k: &FlowStructOpKeys<'_>) {
            k.fctx.core(),
            k.fctx.members(),
            k.fidx.flow_index(),
        }
        argv {
            args.flow_id.to_string(),
            args.execution_id.to_string(),
            args.now.to_string(),
        }
    }
}

impl FromFcallResult for AddExecutionToFlowResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        let eid_str = r.field_str(0);
        let nc_str = r.field_str(1);
        match r.status.as_str() {
            "ALREADY_SATISFIED" => {
                let eid = ff_core::types::ExecutionId::parse(&eid_str)
                    .map_err(|e| ScriptError::Parse {
                        fcall: "ff_add_execution_to_flow".into(),
                        execution_id: None,
                        message: format!("bad execution_id: {e}"),
                    })?;
                let nc: u32 = nc_str.parse().unwrap_or(0);
                Ok(AddExecutionToFlowResult::AlreadyMember {
                    execution_id: eid,
                    node_count: nc,
                })
            }
            "OK" => {
                let eid = ff_core::types::ExecutionId::parse(&eid_str)
                    .map_err(|e| ScriptError::Parse {
                        fcall: "ff_add_execution_to_flow".into(),
                        execution_id: None,
                        message: format!("bad execution_id: {e}"),
                    })?;
                let nc: u32 = nc_str.parse().unwrap_or(0);
                Ok(AddExecutionToFlowResult::Added {
                    execution_id: eid,
                    new_node_count: nc,
                })
            }
            _ => Err(ScriptError::Parse {
                fcall: "ff_add_execution_to_flow".into(),
                execution_id: None,
                message: format!("unexpected status: {}", r.status),
            }),
        }
    }
}

// ─── ff_cancel_flow ──────────────────────────────────────────────────
// KEYS (5): flow_core, members_set, flow_index, pending_cancels, cancel_backlog
// ARGV (5): flow_id, reason, cancellation_policy, now_ms, grace_ms
//
// pending_cancels + cancel_backlog are populated only on policy=cancel_all
// with members > 0. The cancel_reconciler scanner drains any entries
// live dispatch leaves behind. Passing grace_ms as "" accepts the Lua
// default (30s).

ff_function! {
    pub ff_cancel_flow(args: CancelFlowArgs) -> CancelFlowResult {
        keys(k: &FlowStructOpKeys<'_>) {
            k.fctx.core(),
            k.fctx.members(),
            k.fidx.flow_index(),
            k.fctx.pending_cancels(),
            k.fidx.cancel_backlog(),
        }
        argv {
            args.flow_id.to_string(),
            args.reason.clone(),
            args.cancellation_policy.clone(),
            args.now.to_string(),
            String::new(),
        }
    }
}

impl FromFcallResult for CancelFlowResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        let policy = r.field_str(0);
        let mut members = Vec::new();
        let mut i = 1;
        loop {
            let s = r.field_str(i);
            if s.is_empty() {
                break;
            }
            members.push(s);
            i += 1;
        }
        Ok(CancelFlowResult::Cancelled {
            cancellation_policy: policy,
            member_execution_ids: members,
        })
    }
}

// ─── ff_evaluate_flow_eligibility ─────────────────────────────────────
// KEYS (2): exec_core, deps_meta
// ARGV (0)

ff_function! {
    #[allow(unused_variables)]
    pub ff_evaluate_flow_eligibility(args: EvaluateFlowEligibilityArgs) -> EvaluateFlowEligibilityResult {
        keys(k: &DepOpKeys<'_>) {
            k.ctx.core(),
            k.ctx.deps_meta(),
        }
        argv {
        }
    }
}

impl FromFcallResult for EvaluateFlowEligibilityResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        Ok(EvaluateFlowEligibilityResult::Status {
            status: r.field_str(0),
        })
    }
}

// ─── ff_apply_dependency_to_child ─────────────────────────────────────
// KEYS (8): exec_core, deps_meta, unresolved_set, dep_hash,
//           eligible_zset, blocked_deps_zset, deps_all_edges, edgegroup
// ARGV (7): flow_id, edge_id, upstream_eid, graph_revision,
//           dependency_kind, data_passing_ref, now_ms

ff_function! {
    pub ff_apply_dependency_to_child(args: ApplyDependencyToChildArgs) -> ApplyDependencyToChildResult {
        keys(k: &DepOpKeys<'_>) {
            k.ctx.core(),
            k.ctx.deps_meta(),
            k.ctx.deps_unresolved(),
            k.ctx.dep_edge(&args.edge_id),
            k.idx.lane_eligible(k.lane_id),
            k.idx.lane_blocked_dependencies(k.lane_id),
            k.ctx.deps_all_edges(),
            k.flow_ctx.edgegroup(k.downstream_eid),
        }
        argv {
            args.flow_id.to_string(),
            args.edge_id.to_string(),
            args.upstream_execution_id.to_string(),
            args.graph_revision.to_string(),
            args.dependency_kind.clone(),
            args.data_passing_ref.clone().unwrap_or_default(),
            args.now.to_string(),
        }
    }
}

impl FromFcallResult for ApplyDependencyToChildResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        let sub = r.field_str(0);
        if sub == "already_applied" {
            Ok(ApplyDependencyToChildResult::AlreadyApplied)
        } else {
            let count: u32 = sub.parse().unwrap_or(0);
            Ok(ApplyDependencyToChildResult::Applied {
                unsatisfied_count: count,
            })
        }
    }
}

// ─── ff_resolve_dependency ────────────────────────────────────────────
// KEYS (14): exec_core, deps_meta, unresolved_set, dep_hash,
//            eligible_zset, terminal_zset, blocked_deps_zset,
//            attempt_hash, stream_meta, downstream_payload,
//            upstream_result, edgegroup, incoming_set,
//            pending_cancel_groups_set
// ARGV (5): edge_id, upstream_outcome, now_ms, flow_id, downstream_eid
//
// KEYS[10]/[11] added in Batch C item 3 for server-side
// data_passing_ref resolution. KEYS[12] added in RFC-016 Stage A for
// the per-downstream edge-group hash; KEYS[13]/[14] + ARGV[4]/[5]
// added in RFC-016 Stage C so the AnyOf/Quorum+CancelRemaining path
// can enumerate siblings and index the group in the per-partition
// `pending_cancel_groups` SET. Flow/exec partitions co-locate under
// `{fp:N}` post-RFC-011 so the FCALL remains single-slot.

ff_function! {
    pub ff_resolve_dependency(args: ResolveDependencyArgs) -> ResolveDependencyResult {
        keys(k: &ResolveDependencyKeys<'_>) {
            k.ctx.core(),
            k.ctx.deps_meta(),
            k.ctx.deps_unresolved(),
            k.ctx.dep_edge(&args.edge_id),
            k.idx.lane_eligible(k.lane_id),
            k.idx.lane_terminal(k.lane_id),
            k.idx.lane_blocked_dependencies(k.lane_id),
            k.ctx.attempt_hash(ff_core::types::AttemptIndex::new(0)), // placeholder
            k.ctx.stream_meta(ff_core::types::AttemptIndex::new(0)),  // placeholder
            k.ctx.payload(),
            k.upstream_ctx.result(),
            k.flow_ctx.edgegroup(k.downstream_eid),
            k.flow_ctx.incoming(k.downstream_eid),
            k.flow_idx.pending_cancel_groups(),
        }
        argv {
            args.edge_id.to_string(),
            args.upstream_outcome.clone(),
            args.now.to_string(),
            k.flow_ctx.flow_id().to_owned(),
            k.downstream_eid.to_string(),
        }
    }
}

impl FromFcallResult for ResolveDependencyResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        match r.field_str(0).as_str() {
            "satisfied" => Ok(ResolveDependencyResult::Satisfied),
            "impossible" => Ok(ResolveDependencyResult::Impossible),
            "already_resolved" => Ok(ResolveDependencyResult::AlreadyResolved),
            other => Err(ScriptError::Parse {
                fcall: "ff_resolve_dependency".into(),
                execution_id: None,
                message: format!("unknown resolve status: {other}"),
            }),
        }
    }
}

// ─── ff_promote_blocked_to_eligible ───────────────────────────────────
// KEYS (5): exec_core, blocked_deps_zset, eligible_zset, deps_meta,
//           deps_unresolved
// ARGV (2): execution_id, now_ms

ff_function! {
    pub ff_promote_blocked_to_eligible(args: PromoteBlockedToEligibleArgs) -> PromoteBlockedToEligibleResult {
        keys(k: &DepOpKeys<'_>) {
            k.ctx.core(),
            k.idx.lane_blocked_dependencies(k.lane_id),
            k.idx.lane_eligible(k.lane_id),
            k.ctx.deps_meta(),
            k.ctx.deps_unresolved(),
        }
        argv {
            args.execution_id.to_string(),
            args.now.to_string(),
        }
    }
}

impl FromFcallResult for PromoteBlockedToEligibleResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let _r = FcallResult::parse(raw)?.into_success()?;
        Ok(PromoteBlockedToEligibleResult::Promoted)
    }
}

// ─── ff_replay_execution ──────────────────────────────────────────────
// KEYS (4+N): exec_core, terminal_zset, eligible_zset, lease_history,
//             [blocked_deps_zset, deps_meta, deps_unresolved, dep_edge_0..N]
// ARGV (2+N): execution_id, now_ms, [edge_id_0..N]
//
// NOTE: Variable KEYS/ARGV. The ff_function! macro generates a fixed-size
// Vec, but this function needs dynamic N edges. For skipped flow member
// replay, use the manual FCALL path instead of this wrapper.
// This wrapper handles the common non-flow replay case (base 4 KEYS).

ff_function! {
    pub ff_replay_execution(args: ReplayExecutionArgs) -> ReplayExecutionResult {
        keys(k: &DepOpKeys<'_>) {
            k.ctx.core(),
            k.idx.lane_terminal(k.lane_id),
            k.idx.lane_eligible(k.lane_id),
            k.ctx.lease_history(),
        }
        argv {
            args.execution_id.to_string(),
            args.now.to_string(),
        }
    }
}

impl FromFcallResult for ReplayExecutionResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        // ok("0") for normal, ok(N) for skipped flow member
        let unsatisfied = r.field_str(0);
        let ps = if unsatisfied == "0" {
            PublicState::Waiting
        } else {
            PublicState::WaitingChildren
        };
        Ok(ReplayExecutionResult::Replayed { public_state: ps })
    }
}

// ─── ff_stage_dependency_edge ─────────────────────────────────────────
// KEYS (6): flow_core, members_set, edge_hash, out_adj_set, in_adj_set,
//           grant_hash
// ARGV (8): flow_id, edge_id, upstream_eid, downstream_eid,
//           dependency_kind, data_passing_ref, expected_graph_revision,
//           now_ms

ff_function! {
    pub ff_stage_dependency_edge(args: StageDependencyEdgeArgs) -> StageDependencyEdgeResult {
        keys(k: &FlowStructOpKeys<'_>) {
            k.fctx.core(),
            k.fctx.members(),
            k.fctx.edge(&args.edge_id),
            k.fctx.outgoing(&args.upstream_execution_id),
            k.fctx.incoming(&args.downstream_execution_id),
            k.fctx.grant(&args.edge_id.to_string()),
        }
        argv {
            args.flow_id.to_string(),
            args.edge_id.to_string(),
            args.upstream_execution_id.to_string(),
            args.downstream_execution_id.to_string(),
            args.dependency_kind.clone(),
            args.data_passing_ref.clone().unwrap_or_default(),
            args.expected_graph_revision.to_string(),
            args.now.to_string(),
        }
    }
}

impl FromFcallResult for StageDependencyEdgeResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        let eid = ff_core::types::EdgeId::parse(&r.field_str(0))
            .map_err(|e| ScriptError::Parse {
                fcall: "ff_stage_dependency_edge".into(),
                execution_id: None,
                message: format!("bad edge_id: {e}"),
            })?;
        let rev: u64 = r.field_str(1).parse()
            .map_err(|e| ScriptError::Parse {
                fcall: "ff_stage_dependency_edge".into(),
                execution_id: None,
                message: format!("bad graph_revision: {e}"),
            })?;
        Ok(StageDependencyEdgeResult::Staged {
            edge_id: eid,
            new_graph_revision: rev,
        })
    }
}

// ─── ff_set_edge_group_policy (RFC-016 Stage B) ─────────────────────────
// KEYS (2): flow_core, edgegroup
// ARGV (4): policy_variant, on_satisfied, k, now_ms
//
// Stage B widens the ARGV: `k` (quorum threshold, "0" for AllOf/AnyOf)
// joins the stored fields so the resolver can evaluate Quorum without a
// separate Lua round-trip.

pub struct SetEdgeGroupPolicyKeys<'a> {
    pub fctx: &'a FlowKeyContext,
    pub downstream_eid: &'a ff_core::types::ExecutionId,
}

ff_function! {
    pub ff_set_edge_group_policy(args: SetEdgeGroupPolicyArgs) -> SetEdgeGroupPolicyResult {
        keys(k: &SetEdgeGroupPolicyKeys<'_>) {
            k.fctx.core(),
            k.fctx.edgegroup(k.downstream_eid),
        }
        argv {
            args.policy.variant_str().to_string(),
            match &args.policy {
                ff_core::contracts::EdgeDependencyPolicy::AllOf => String::new(),
                ff_core::contracts::EdgeDependencyPolicy::AnyOf { on_satisfied }
                | ff_core::contracts::EdgeDependencyPolicy::Quorum { on_satisfied, .. } => {
                    on_satisfied.variant_str().to_string()
                }
                _ => String::new(),
            },
            match &args.policy {
                ff_core::contracts::EdgeDependencyPolicy::Quorum { k, .. } => k.to_string(),
                _ => "0".to_string(),
            },
            args.now.to_string(),
        }
    }
}

impl FromFcallResult for SetEdgeGroupPolicyResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        match r.field_str(0).as_str() {
            "set" => Ok(SetEdgeGroupPolicyResult::Set),
            "already_set" => Ok(SetEdgeGroupPolicyResult::AlreadySet),
            other => Err(ScriptError::Parse {
                fcall: "ff_set_edge_group_policy".into(),
                execution_id: None,
                message: format!("unexpected status: {other}"),
            }),
        }
    }
}

// ─── ff_set_flow_tags (issue #58.4) ─────────────────────────────────────
//
// Variadic-ARGV FCALL mirroring `ff_set_execution_tags`. Hand-rolled
// since the `ff_function!` macro assumes a fixed ARGV vector.
//
// KEYS (2): flow_core, tags_key
// ARGV (>=2, even): k1, v1, k2, v2, ...

/// Call `ff_set_flow_tags`: write caller-supplied tag fields to the
/// flow's separate tags key. Returns the number of pairs applied. Tag
/// keys must match `^[a-z][a-z0-9_]*\.`. The Lua function also
/// lazy-migrates any pre-58.4 reserved-namespace fields stashed
/// inline on `flow_core` into the new tags key.
pub async fn ff_set_flow_tags(
    conn: &ferriskey::Client,
    fctx: &FlowKeyContext,
    args: &SetFlowTagsArgs,
) -> Result<SetFlowTagsResult, ScriptError> {
    let keys = [fctx.core(), fctx.tags()];
    let key_refs: Vec<&str> = keys.iter().map(|s| s.as_str()).collect();

    let mut argv: Vec<String> = Vec::with_capacity(args.tags.len() * 2);
    for (k, v) in &args.tags {
        argv.push(k.clone());
        argv.push(v.clone());
    }
    let argv_refs: Vec<&str> = argv.iter().map(|s| s.as_str()).collect();

    let raw = conn
        .fcall::<ferriskey::Value>("ff_set_flow_tags", &key_refs, &argv_refs)
        .await
        .map_err(ScriptError::Valkey)?;
    SetFlowTagsResult::from_fcall_result(&raw)
}

impl FromFcallResult for SetFlowTagsResult {
    fn from_fcall_result(raw: &ferriskey::Value) -> Result<Self, ScriptError> {
        let r = FcallResult::parse(raw)?.into_success()?;
        let count: u32 = r
            .field_str(0)
            .parse()
            .map_err(|e| ScriptError::Parse {
                fcall: "ff_set_flow_tags".into(),
                execution_id: None,
                message: format!("bad tag count: {e}"),
            })?;
        Ok(SetFlowTagsResult::Ok { count })
    }
}