bijux-dag-runtime 0.4.1

Execution engine, replay semantics, and runtime policy layer for Bijux DAG graphs.
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
use crate::remote_execution_model::{
    MockRemoteWorker, RemoteNodeExecutionPayload, RemoteNodeExecutionResult, RemoteWorkerExecutor,
};
use crate::remote_executor::{
    RemoteExecutionReceipt, RemoteExecutionRequest, RemoteExecutorSubmitter,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Arc, Mutex};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DistributedExecutionRequest {
    pub run_id: String,
    pub node_id: String,
    pub worker_pool: String,
    pub backend_hint: String,
    pub command: Vec<String>,
    pub env: BTreeMap<String, String>,
    pub attempt: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DistributedExecutionResult {
    pub run_id: String,
    pub node_id: String,
    pub status: String,
    pub outputs: Vec<String>,
    pub logs: Vec<String>,
    pub diagnostics: Vec<String>,
    pub started_unix_ms: u128,
    pub finished_unix_ms: u128,
    pub provenance: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerIdentity {
    pub worker_id: String,
    pub worker_version: String,
    pub backend_kind: String,
    pub labels: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerCapabilities {
    pub cpu_capacity: u32,
    pub memory_mb: u32,
    pub supports_gpu: bool,
    pub supports_container: bool,
    pub supports_sandbox_profiles: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerRegistration {
    pub identity: WorkerIdentity,
    pub capabilities: WorkerCapabilities,
    pub registered_unix_ms: u128,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkLease {
    pub lease_id: String,
    pub run_id: String,
    pub node_id: String,
    pub worker_id: String,
    pub expires_unix_ms: u128,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TaskLeaseSemantics {
    pub lease_duration_ms: u64,
    pub renew_before_expiry_ms: u64,
    pub max_renewals: u32,
    pub recovery_grace_ms: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerHeartbeat {
    pub worker_id: String,
    pub unix_ms: u128,
    pub inflight_nodes: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LivenessPolicy {
    pub heartbeat_timeout_ms: u64,
    pub grace_retries: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HeartbeatSemantics {
    pub interval_ms: u64,
    pub timeout_ms: u64,
    pub delayed_threshold_ms: u64,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum HeartbeatClass {
    Healthy,
    Delayed,
    Lost,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ReassignmentRule {
    pub trigger: String,
    pub max_reassignments: u32,
    pub preserve_attempt_lineage: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum DeliveryGuarantee {
    ExactlyOnce,
    AtLeastOnce,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerSandboxNegotiation {
    pub worker_id: String,
    pub required_profile: String,
    pub accepted: bool,
    pub reason: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoteLogStreamContract {
    pub run_id: String,
    pub node_id: String,
    pub stream_endpoint: String,
    pub local_fallback_path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoteArtifactUploadContract {
    pub run_id: String,
    pub node_id: String,
    pub artifact_path: String,
    pub target_store: String,
    pub checksum: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoteArtifactCommitContract {
    pub run_id: String,
    pub node_id: String,
    pub attempt: u32,
    pub upload_id: String,
    pub committed: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoteCancellationContract {
    pub run_id: String,
    pub node_id: Option<String>,
    pub propagate_to_worker: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RetryLineageRecord {
    pub run_id: String,
    pub node_id: String,
    pub attempt: u32,
    pub parent_attempt: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RemoteStatusEvent {
    pub run_id: String,
    pub node_id: String,
    pub sequence: u64,
    pub status: String,
    pub unix_ms: u128,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerPoolCapabilityRequest {
    pub required_min_cpu_capacity: u32,
    pub required_min_memory_mb: u32,
    pub require_gpu: bool,
    pub require_container_support: bool,
    pub required_sandbox_profile: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PlacementHint {
    pub node_id: String,
    pub preferred_pool: String,
    pub preferred_worker_labels: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerPool {
    pub pool_id: String,
    pub class: String,
    pub workers: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum DistributedFailureClass {
    Infrastructure,
    TaskLogic,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkerVersionCompatibilityRule {
    pub planner_version: String,
    pub minimum_worker_version: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum StatusReportingClass {
    Healthy,
    Partitioned,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DistributedSecurityModel {
    pub worker_trust_model: String,
    pub artifact_trust_model: String,
    pub command_trust_model: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DistributedReadinessChecklist {
    pub typed_transport_contracts: bool,
    pub worker_liveness_contracts: bool,
    pub retry_lineage_contracts: bool,
    pub security_model_documented: bool,
    pub conformance_fixtures_present: bool,
}

pub fn validate_task_lease_semantics(semantics: &TaskLeaseSemantics) -> Result<(), String> {
    if semantics.lease_duration_ms == 0 {
        return Err("lease duration must be greater than zero".to_string());
    }
    if semantics.renew_before_expiry_ms >= semantics.lease_duration_ms {
        return Err("renew_before_expiry_ms must be less than lease_duration_ms".to_string());
    }
    if semantics.max_renewals == 0 {
        return Err("max_renewals must be greater than zero".to_string());
    }
    Ok(())
}

pub fn validate_worker_identity(identity: &WorkerIdentity) -> Result<(), String> {
    for field in [
        identity.worker_id.as_str(),
        identity.worker_version.as_str(),
        identity.backend_kind.as_str(),
    ] {
        if field.trim().is_empty() {
            return Err("worker identity fields must be non-empty".to_string());
        }
    }
    Ok(())
}

pub fn worker_alive(
    heartbeat: &WorkerHeartbeat,
    now_unix_ms: u128,
    policy: &LivenessPolicy,
) -> bool {
    now_unix_ms.saturating_sub(heartbeat.unix_ms) <= policy.heartbeat_timeout_ms as u128
}

pub fn should_reassign(lease: &WorkLease, now_unix_ms: u128) -> bool {
    now_unix_ms > lease.expires_unix_ms
}

pub fn recover_lost_lease(
    lease: &WorkLease,
    now_unix_ms: u128,
    semantics: &TaskLeaseSemantics,
) -> bool {
    now_unix_ms.saturating_sub(lease.expires_unix_ms) <= semantics.recovery_grace_ms as u128
}

pub fn classify_heartbeat(
    heartbeat: &WorkerHeartbeat,
    now_unix_ms: u128,
    semantics: &HeartbeatSemantics,
) -> HeartbeatClass {
    let age = now_unix_ms.saturating_sub(heartbeat.unix_ms);
    if age > semantics.timeout_ms as u128 {
        HeartbeatClass::Lost
    } else if age > semantics.delayed_threshold_ms as u128 {
        HeartbeatClass::Delayed
    } else {
        HeartbeatClass::Healthy
    }
}

pub fn is_duplicate_dispatch(
    dispatched_keys: &mut BTreeSet<String>,
    run_id: &str,
    node_id: &str,
) -> bool {
    let key = format!("{run_id}:{node_id}");
    !dispatched_keys.insert(key)
}

pub fn check_worker_version_compatibility(
    worker_version: &str,
    rule: &WorkerVersionCompatibilityRule,
) -> bool {
    worker_version >= rule.minimum_worker_version.as_str() && !rule.planner_version.is_empty()
}

pub fn reject_worker_version_mismatch(
    worker_version: &str,
    rule: &WorkerVersionCompatibilityRule,
) -> Result<(), String> {
    if check_worker_version_compatibility(worker_version, rule) {
        Ok(())
    } else {
        Err(format!(
            "worker version mismatch: worker={worker_version}, minimum_supported={}",
            rule.minimum_worker_version
        ))
    }
}

pub fn artifact_upload_can_commit(
    upload: &RemoteArtifactUploadContract,
    commit: &RemoteArtifactCommitContract,
) -> bool {
    upload.run_id == commit.run_id
        && upload.node_id == commit.node_id
        && !upload.checksum.trim().is_empty()
        && !commit.upload_id.trim().is_empty()
        && commit.committed
}

pub fn verify_remote_artifact_integrity(
    expected_checksum: &str,
    transported_checksum: &str,
) -> bool {
    !expected_checksum.trim().is_empty() && expected_checksum == transported_checksum
}

pub fn normalize_status_events(
    events: &[RemoteStatusEvent],
) -> (Vec<RemoteStatusEvent>, Vec<RemoteStatusEvent>) {
    let mut sorted = events.to_vec();
    sorted.sort_by(|a, b| a.sequence.cmp(&b.sequence).then(a.unix_ms.cmp(&b.unix_ms)));

    let mut deduped = Vec::new();
    let mut duplicates = Vec::new();
    let mut seen = BTreeSet::new();
    for event in sorted {
        let key =
            (event.run_id.clone(), event.node_id.clone(), event.sequence, event.status.clone());
        if seen.insert(key) {
            deduped.push(event);
        } else {
            duplicates.push(event);
        }
    }
    (deduped, duplicates)
}

pub fn classify_status_reporting(
    last_status_unix_ms: u128,
    now_unix_ms: u128,
    timeout_ms: u64,
) -> StatusReportingClass {
    if now_unix_ms.saturating_sub(last_status_unix_ms) > timeout_ms as u128 {
        StatusReportingClass::Partitioned
    } else {
        StatusReportingClass::Healthy
    }
}

pub fn cancellation_delivered_in_time(
    issued_unix_ms: u128,
    delivered_unix_ms: u128,
    deadline_ms: u64,
) -> bool {
    delivered_unix_ms.saturating_sub(issued_unix_ms) <= deadline_ms as u128
}

pub fn worker_pool_satisfies_capability_request(
    worker: &WorkerCapabilities,
    request: &WorkerPoolCapabilityRequest,
) -> bool {
    if worker.cpu_capacity < request.required_min_cpu_capacity
        || worker.memory_mb < request.required_min_memory_mb
    {
        return false;
    }
    if request.require_gpu && !worker.supports_gpu {
        return false;
    }
    if request.require_container_support && !worker.supports_container {
        return false;
    }
    if let Some(profile) = &request.required_sandbox_profile {
        return worker.supports_sandbox_profiles.iter().any(|p| p == profile);
    }
    true
}

#[derive(Default, Clone)]
pub struct MockRemoteBackend {
    submissions: Arc<Mutex<Vec<DistributedExecutionRequest>>>,
    payload_executions: Arc<Mutex<Vec<RemoteNodeExecutionPayload>>>,
}

impl MockRemoteBackend {
    pub fn submissions(&self) -> Vec<DistributedExecutionRequest> {
        self.submissions.lock().map(|g| g.clone()).unwrap_or_default()
    }

    pub fn payload_executions(&self) -> Vec<RemoteNodeExecutionPayload> {
        self.payload_executions.lock().map(|g| g.clone()).unwrap_or_default()
    }

    pub fn submit_distributed(
        &self,
        request: DistributedExecutionRequest,
    ) -> Result<DistributedExecutionResult, String> {
        self.submissions
            .lock()
            .map_err(|_| "submission lock poisoned".to_string())?
            .push(request.clone());
        Ok(DistributedExecutionResult {
            run_id: request.run_id,
            node_id: request.node_id,
            status: "accepted".to_string(),
            outputs: Vec::new(),
            logs: Vec::new(),
            diagnostics: Vec::new(),
            started_unix_ms: 0,
            finished_unix_ms: 0,
            provenance: BTreeMap::new(),
        })
    }

    pub fn execute_remote_payload(
        &self,
        payload: RemoteNodeExecutionPayload,
    ) -> Result<RemoteNodeExecutionResult, String> {
        self.payload_executions
            .lock()
            .map_err(|_| "payload execution lock poisoned".to_string())?
            .push(payload.clone());
        MockRemoteWorker.execute_payload(payload)
    }
}

impl RemoteExecutorSubmitter for MockRemoteBackend {
    fn submit(&self, request: RemoteExecutionRequest) -> Result<RemoteExecutionReceipt, String> {
        let distributed = DistributedExecutionRequest {
            run_id: request.run_id,
            node_id: request.node_id,
            worker_pool: "default".to_string(),
            backend_hint: "mock".to_string(),
            command: vec!["mock".to_string()],
            env: BTreeMap::new(),
            attempt: 1,
        };
        let _ = self.submit_distributed(distributed)?;
        Ok(RemoteExecutionReceipt {
            submission_id: format!("mock-{}", request.contract_digest),
            accepted: true,
            reason: None,
        })
    }
}