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
/// VM configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VMConfig {
/// Migration-safe config schema version.
#[serde(default = "default_config_schema_version")]
pub config_schema_version: u32,
/// Scheduling policy.
pub sched_policy: SchedPolicy,
/// Default buffer configuration for new sessions.
pub buffer_config: BufferConfig,
/// Maximum number of concurrent sessions.
pub max_sessions: usize,
/// Maximum number of concurrent coroutines.
pub max_coroutines: usize,
/// Number of registers per coroutine.
pub num_registers: u16,
/// Simulated time per scheduler round.
pub tick_duration: Duration,
/// Guard layers configured for the VM.
pub guard_layers: Vec<GuardLayerConfig>,
/// Whether speculative execution is enabled.
pub speculation_enabled: bool,
/// Determinism profile for replay/equivalence behavior.
pub determinism_mode: DeterminismMode,
/// Effect determinism tier used by admission and envelope artifacts.
#[serde(default)]
pub effect_determinism_tier: EffectDeterminismTier,
/// Output-condition policy for commit eligibility of observable outputs.
pub output_condition_policy: OutputConditionPolicy,
/// Monitor mode for pre-dispatch type checks.
#[serde(default)]
pub monitor_mode: MonitorMode,
/// Flow policy for epistemic knowledge checks.
#[serde(default)]
pub flow_policy: FlowPolicy,
/// Deterministic cost charged for each instruction dispatch.
#[serde(default = "default_instruction_cost")]
pub instruction_cost: usize,
/// Initial cost budget assigned to each coroutine.
#[serde(default = "default_initial_cost_budget")]
pub initial_cost_budget: usize,
/// Whether threaded scheduler may admit same-session picks when footprint-disjoint.
#[serde(default)]
pub footprint_guided_wave_widening: bool,
/// Runtime tuning profile used by instrumentation/benchmark harnesses.
#[serde(default)]
pub runtime_tuning_profile: RuntimeTuningProfile,
/// Round semantics mode used by threaded scheduler.
#[serde(default)]
pub threaded_round_semantics: ThreadedRoundSemantics,
/// Effect-trace capture mode for integration/perf tuning.
#[serde(default)]
pub effect_trace_capture_mode: EffectTraceCaptureMode,
/// Runtime payload hardening mode for inbound/outbound messages.
#[serde(default)]
pub payload_validation_mode: PayloadValidationMode,
/// Communication replay-consumption mode.
#[serde(default)]
pub communication_replay_mode: CommunicationReplayMode,
/// Upper bound for VM payload values in estimated wire bytes.
#[serde(default = "default_max_payload_bytes")]
pub max_payload_bytes: usize,
/// Enable runtime host-contract assertions with deterministic diagnostics.
#[serde(default)]
pub host_contract_assertions: bool,
}
impl Default for VMConfig {
fn default() -> Self {
Self {
config_schema_version: default_config_schema_version(),
sched_policy: SchedPolicy::Cooperative,
buffer_config: BufferConfig::default(),
max_sessions: 256,
max_coroutines: 1024,
num_registers: 16,
tick_duration: Duration::from_millis(1),
guard_layers: Vec::new(),
speculation_enabled: false,
determinism_mode: DeterminismMode::Full,
effect_determinism_tier: EffectDeterminismTier::StrictDeterministic,
output_condition_policy: OutputConditionPolicy::AllowAll,
monitor_mode: MonitorMode::SessionTypePrecheck,
flow_policy: FlowPolicy::AllowAll,
instruction_cost: 1,
initial_cost_budget: usize::MAX,
footprint_guided_wave_widening: false,
runtime_tuning_profile: RuntimeTuningProfile::Standard,
threaded_round_semantics: ThreadedRoundSemantics::CanonicalOneStep,
effect_trace_capture_mode: EffectTraceCaptureMode::Full,
payload_validation_mode: PayloadValidationMode::Structural,
communication_replay_mode: CommunicationReplayMode::Off,
max_payload_bytes: default_max_payload_bytes(),
host_contract_assertions: false,
}
}
}
impl VMConfig {
/// Validate VM configuration invariants required for safe state initialization.
///
/// # Errors
///
/// Returns a reason string if a required invariant is violated.
pub fn validate_invariants(&self) -> Result<(), String> {
if self.config_schema_version < 1 {
return Err("config_schema_version must be >= 1".to_string());
}
if self.max_sessions == 0 {
return Err("max_sessions must be > 0".to_string());
}
if self.max_coroutines == 0 {
return Err("max_coroutines must be > 0".to_string());
}
if self.num_registers == 0 {
return Err("num_registers must be > 0".to_string());
}
if self.instruction_cost == 0 {
return Err("instruction_cost must be > 0".to_string());
}
if self.max_payload_bytes == 0 {
return Err("max_payload_bytes must be > 0".to_string());
}
Ok(())
}
/// Assert VM configuration invariants required for safe state initialization.
///
/// # Panics
///
/// Panics when a required invariant is violated.
pub fn assert_invariants(&self) {
if let Err(reason) = self.validate_invariants() {
panic!("{reason}");
}
}
}
/// Observable event emitted by the VM.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TickedObsEvent {
/// Scheduler tick when the wrapped event occurred.
pub tick: u64,
/// Underlying observable event payload.
pub event: ObsEvent,
}
/// Observable event emitted by the VM.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ObsEvent {
/// Value sent on an edge.
Sent {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session-scoped edge for this send.
edge: Edge,
/// Session ID.
session: SessionId,
/// Sender role.
from: String,
/// Receiver role.
to: String,
/// Message label.
label: String,
},
/// Value received on an edge.
Received {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session-scoped edge for this receive.
edge: Edge,
/// Session ID.
session: SessionId,
/// Sender role.
from: String,
/// Receiver role.
to: String,
/// Message label.
label: String,
},
/// Label offered on an edge.
Offered {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session-scoped edge for this offer.
edge: Edge,
/// Label offered.
label: String,
},
/// Label chosen on an edge.
Chose {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session-scoped edge for this choice.
edge: Edge,
/// Label chosen.
label: String,
},
/// Session opened.
Opened {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
/// Participating roles.
roles: Vec<String>,
},
/// Session closed.
Closed {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
},
/// Session epoch advanced.
EpochAdvanced {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
sid: SessionId,
/// New epoch number.
epoch: usize,
},
/// Coroutine halted.
Halted {
/// Scheduler tick when the event occurred.
tick: u64,
/// Coroutine ID.
coro_id: usize,
},
/// Effect handler invoked.
Invoked {
/// Scheduler tick when the event occurred.
tick: u64,
/// Coroutine ID.
coro_id: usize,
/// Role name.
role: String,
},
/// Guard layer acquired.
Acquired {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
/// Role name.
role: String,
/// Guard layer identifier.
layer: String,
},
/// Guard layer released.
Released {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
/// Role name.
role: String,
/// Guard layer identifier.
layer: String,
},
/// Endpoint transferred between coroutines.
Transferred {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
/// Role name.
role: String,
/// Source coroutine.
from: usize,
/// Target coroutine.
to: usize,
},
/// Speculation forked for a ghost session.
Forked {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
/// Ghost session id.
ghost: usize,
},
/// Speculation joined.
Joined {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
},
/// Speculation aborted.
Aborted {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
},
/// Knowledge fact tagged.
Tagged {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
/// Role name.
role: String,
/// Fact payload.
fact: String,
},
/// Knowledge fact checked.
Checked {
/// Scheduler tick when the event occurred.
tick: u64,
/// Session ID.
session: SessionId,
/// Role name.
role: String,
/// Target role.
target: String,
/// Whether the flow policy permitted the fact.
permitted: bool,
},
/// Coroutine faulted.
Faulted {
/// Scheduler tick when the event occurred.
tick: u64,
/// Coroutine ID.
coro_id: usize,
/// The fault.
fault: Fault,
},
/// Output-condition verification was evaluated at commit time.
OutputConditionChecked {
/// Scheduler tick when the event occurred.
tick: u64,
/// Predicate reference that was checked.
predicate_ref: String,
/// Optional witness reference used by the check.
witness_ref: Option<String>,
/// Opaque output digest checked by the verifier.
output_digest: String,
/// Verification outcome.
passed: bool,
},
}
/// The VM execution result for a single step.
#[derive(Debug)]
pub enum StepResult {
/// A coroutine executed an instruction and may continue.
Continue,
/// No coroutines are ready (all blocked or done).
Stuck,
/// All coroutines have completed.
AllDone,
}
/// Terminal status returned by bounded VM run APIs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RunStatus {
/// All coroutines reached terminal states.
AllDone,
/// No runnable coroutines remain (blocked/stuck).
Stuck,
/// `max_rounds`/`max_steps` budget was exhausted before termination.
MaxRoundsExceeded,
}
/// Debug metadata for the most recent scheduler-dispatched step.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SchedExecStatus {
/// Instruction continued execution.
Continue,
/// Instruction yielded cooperative control.
Yielded,
/// Instruction blocked.
Blocked,
/// Coroutine halted normally.
Halted,
/// Coroutine faulted.
Faulted,
}
/// Debug metadata for the most recent scheduler-dispatched step.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedStepDebug {
/// Selected coroutine id.
pub selected_coro: usize,
/// Instruction-step execution status.
pub exec_status: SchedExecStatus,
}