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
//! Engine-as-op composition bridge.
//!
//! Stage 1 does not mutate existing `EngineSpec` consumers. It adapts each
//! registered engine into the same composable surface used by operation specs
//! and rejects unsafe pairs before any CPU or GPU dispatch can happen.
#[cfg(test)]
pub use catalog::{
deferred_engine_specs, engine_op_specs, migration_report, DeferredEngine, EngineMigrationReport,
};
pub use harness::{run_pairwise_bridge_harness, PairOutcome, PairStatus};
pub use op::{
can_compose, compose_cpu, ComposableOp, CompositionError, EngineMigrationStage, EngineOpSpec,
WireFormat,
};
mod op {
/// Unified composable operation surface for L1/L2 ops and L3 engines.
use crate::spec::types::conform::CpuReferenceFn;
use crate::spec::types::OpSpec;
use crate::spec::types::{DataType, OpSignature};
/// Semantic wire format carried by an operation output or accepted as input.
///
/// `DataType::Bytes` is intentionally too broad for engine composition: a DFA
/// match stream and a serialized eval request are both byte buffers, but feeding
/// one into the other corrupts semantics. This tag is the minimum bridge needed
/// until engine request and response schemas become typed u32 records.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WireFormat {
/// A single fixed-width scalar or vector value.
Scalar,
/// An unstructured byte buffer from an ordinary op.
Bytes,
/// Serialized DFA engine request.
DfaRequest,
/// DFA match triples: pattern_id, start, end as u32 words.
DfaMatches,
/// Serialized bytecode eval engine request.
EvalRequest,
/// Eval fired flags widened to u32 words.
EvalFiredWords,
/// Serialized scatter engine request.
ScatterRequest,
/// Scatter rule bitmap as u32 words.
RuleBitmapWords,
}
/// Migration state for an engine adapted onto the composable op surface.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EngineMigrationStage {
/// The engine has an op-like CPU reference and type signature, but its
/// public wire boundary is not yet a valid downstream engine input.
BridgeOnly {
/// Actionable reason composition must be rejected.
reason: &'static str,
},
/// The engine can participate in direct engine-op composition.
Composable,
}
/// Unified trait for anything that can participate in an op chain.
///
/// Existing primitive specs implement this directly. Engines implement it
/// through [`EngineOpSpec`] so their old `EngineSpec` API remains stable.
pub trait ComposableOp {
/// Stable operation or engine id.
fn id(&self) -> &str;
/// Type-level signature checked before composition.
fn signature(&self) -> &OpSignature;
/// Semantic input wire format.
fn input_wire_format(&self) -> WireFormat;
/// Semantic output wire format.
fn output_wire_format(&self) -> WireFormat;
/// CPU reference that defines this op's behavior.
fn cpu_reference(&self) -> CpuReferenceFn;
/// Engine migration state.
fn migration_stage(&self) -> EngineMigrationStage {
EngineMigrationStage::Composable
}
}
impl ComposableOp for OpSpec {
fn id(&self) -> &str {
self.id
}
fn signature(&self) -> &OpSignature {
&self.signature
}
fn input_wire_format(&self) -> WireFormat {
self.signature
.inputs
.first()
.map(wire_format_for_data_type)
.unwrap_or(WireFormat::Bytes)
}
fn output_wire_format(&self) -> WireFormat {
wire_format_for_data_type(&self.signature.output)
}
fn cpu_reference(&self) -> CpuReferenceFn {
self.cpu_fn
}
}
/// Engine adapted as a first-class composable op.
#[derive(Clone)]
pub struct EngineOpSpec {
id: &'static str,
description: &'static str,
signature: OpSignature,
input_wire_format: WireFormat,
output_wire_format: WireFormat,
cpu_reference: CpuReferenceFn,
migration_stage: EngineMigrationStage,
}
impl EngineOpSpec {
/// Construct an engine-op adapter from explicit metadata.
#[inline]
pub fn new(
id: &'static str,
description: &'static str,
signature: OpSignature,
wire_formats: (WireFormat, WireFormat),
cpu_reference: CpuReferenceFn,
migration_stage: EngineMigrationStage,
) -> Self {
Self {
id,
description,
signature,
input_wire_format: wire_formats.0,
output_wire_format: wire_formats.1,
cpu_reference,
migration_stage,
}
}
/// Human-readable engine description.
#[inline]
pub fn description(&self) -> &'static str {
self.description
}
/// Stable engine id with static lifetime.
#[inline]
pub fn stable_id(&self) -> &'static str {
self.id
}
}
impl ComposableOp for EngineOpSpec {
fn id(&self) -> &str {
self.id
}
fn signature(&self) -> &OpSignature {
&self.signature
}
fn input_wire_format(&self) -> WireFormat {
self.input_wire_format
}
fn output_wire_format(&self) -> WireFormat {
self.output_wire_format
}
fn cpu_reference(&self) -> CpuReferenceFn {
self.cpu_reference
}
fn migration_stage(&self) -> EngineMigrationStage {
self.migration_stage
}
}
/// Composition validation failure.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CompositionError {
/// An engine is explicitly marked bridge-only.
StageBlocked {
/// Engine id that blocked direct composition.
engine_id: String,
/// Actionable reason.
reason: &'static str,
},
/// The outer op has no declared input slot.
MissingInput {
/// Outer operation id.
op_id: String,
},
/// Type signatures do not align.
TypeMismatch {
/// First operation id.
first_id: String,
/// First output type.
first_output: DataType,
/// Second operation id.
second_id: String,
/// Second first input type.
second_input: DataType,
},
/// Byte-level data types match, but their semantic wire formats do not.
WireMismatch {
/// First operation id.
first_id: String,
/// First output wire format.
first_output: WireFormat,
/// Second operation id.
second_id: String,
/// Second input wire format.
second_input: WireFormat,
},
/// CPU references produced different bytes for identical composed input.
CpuNondeterministic {
/// First operation id.
first_id: String,
/// Second operation id.
second_id: String,
},
}
impl std::fmt::Display for CompositionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::StageBlocked { engine_id, reason } => {
write!(f, "{engine_id} is bridge-only. Fix: {reason}")
}
Self::MissingInput { op_id } => {
write!(
f,
"{op_id} has no input slot. Fix: declare an input type before composing"
)
}
Self::TypeMismatch {
first_id,
first_output,
second_id,
second_input,
} => write!(
f,
"{first_id} outputs {first_output}, but {second_id} accepts {second_input}. \
Fix: insert a typed adapter op or change one signature"
),
Self::WireMismatch {
first_id,
first_output,
second_id,
second_input,
} => write!(
f,
"{first_id} outputs {first_output:?}, but {second_id} accepts {second_input:?}. \
Fix: compose only engines with the same typed wire schema"
),
Self::CpuNondeterministic {
first_id,
second_id,
} => write!(
f,
"{first_id} then {second_id} produced nondeterministic CPU output. \
Fix: make both CPU references pure and deterministic"
),
}
}
}
impl std::error::Error for CompositionError {}
/// Check whether two composable specs can be chained.
#[inline]
pub fn can_compose<A, B>(first: &A, second: &B) -> Result<(), CompositionError>
where
A: ComposableOp,
B: ComposableOp,
{
ensure_composable_stage(first)?;
ensure_composable_stage(second)?;
let Some(second_input) = second.signature().inputs.first() else {
return Err(CompositionError::MissingInput {
op_id: second.id().to_string(),
});
};
if &first.signature().output != second_input {
return Err(CompositionError::TypeMismatch {
first_id: first.id().to_string(),
first_output: first.signature().output.clone(),
second_id: second.id().to_string(),
second_input: second_input.clone(),
});
}
if first.output_wire_format() != second.input_wire_format() {
return Err(CompositionError::WireMismatch {
first_id: first.id().to_string(),
first_output: first.output_wire_format(),
second_id: second.id().to_string(),
second_input: second.input_wire_format(),
});
}
Ok(())
}
/// Run two CPU references as a composed chain after compatibility checking.
#[inline]
pub fn compose_cpu<A, B>(
first: &A,
second: &B,
input: &[u8],
) -> Result<Vec<u8>, CompositionError>
where
A: ComposableOp,
B: ComposableOp,
{
can_compose(first, second)?;
let first_out = (first.cpu_reference())(input);
Ok((second.cpu_reference())(&first_out))
}
fn ensure_composable_stage<T: ComposableOp>(op: &T) -> Result<(), CompositionError> {
match op.migration_stage() {
EngineMigrationStage::Composable => Ok(()),
EngineMigrationStage::BridgeOnly { reason } => Err(CompositionError::StageBlocked {
engine_id: op.id().to_string(),
reason,
}),
}
}
fn wire_format_for_data_type(data_type: &DataType) -> WireFormat {
match data_type {
DataType::Bytes | DataType::Array { .. } | DataType::Tensor => WireFormat::Bytes,
DataType::U32
| DataType::I32
| DataType::U64
| DataType::Vec2U32
| DataType::Vec4U32
| DataType::Bool
| DataType::F16
| DataType::BF16
| DataType::F32
| DataType::F64 => WireFormat::Scalar,
}
}
}
mod catalog {
/// Engine-op migration catalog.
use crate::spec::engine;
use crate::spec::types::conform::CpuReferenceFn;
use crate::spec::types::{DataType, OpSignature};
use crate::enforce::enforcers::engine_composition::{
ComposableOp, EngineMigrationStage, EngineOpSpec, WireFormat,
};
const DFA_BRIDGE_REASON: &str =
"carry DFA match triples plus pattern-to-rule metadata in one typed u32 stream";
const EVAL_BRIDGE_REASON: &str =
"replace serialized eval requests with typed u32 request and fired-output records";
const SCATTER_BRIDGE_REASON: &str =
"split scatter request metadata from match triples so DFA output can feed it directly";
/// Engine known to exist in `vyre::engine` but not yet registered as an
/// executable conform `EngineSpec`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeferredEngine {
/// Stable engine id.
pub id: &'static str,
/// Actionable migration reason.
pub reason: &'static str,
}
/// Current engine migration state.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EngineMigrationReport {
/// Engines adapted onto the op-like bridge.
pub bridged: Vec<&'static str>,
/// Engines fully composable with at least one other engine or op.
pub composable: Vec<&'static str>,
/// Engines still missing conform registration or typed wire schemas.
pub deferred: Vec<DeferredEngine>,
}
/// Return all conform-registered engines as first-class op adapters.
#[inline]
pub fn engine_op_specs() -> Result<Vec<EngineOpSpec>, String> {
let dfa = engine::dfa::spec();
let eval = engine::eval::spec();
let scatter = engine::scatter::spec();
Ok(vec![
EngineOpSpec::new(
dfa.id,
dfa.description,
OpSignature {
inputs: vec![DataType::Bytes],
output: DataType::Array { element_size: 12 },
},
(WireFormat::DfaRequest, WireFormat::DfaMatches),
required_cpu(dfa.id, dfa.cpu_fn)?,
EngineMigrationStage::BridgeOnly {
reason: DFA_BRIDGE_REASON,
},
),
EngineOpSpec::new(
eval.id,
eval.description,
OpSignature {
inputs: vec![DataType::Bytes],
output: DataType::Array { element_size: 4 },
},
(WireFormat::EvalRequest, WireFormat::EvalFiredWords),
eval_u32_cpu,
EngineMigrationStage::BridgeOnly {
reason: EVAL_BRIDGE_REASON,
},
),
EngineOpSpec::new(
scatter.id,
scatter.description,
OpSignature {
inputs: vec![DataType::Bytes],
output: DataType::Array { element_size: 4 },
},
(WireFormat::ScatterRequest, WireFormat::RuleBitmapWords),
required_cpu(scatter.id, scatter.cpu_fn)?,
EngineMigrationStage::BridgeOnly {
reason: SCATTER_BRIDGE_REASON,
},
),
])
}
/// Return engines that still need conform registration before bridge adapters
/// can be built.
#[inline]
pub fn deferred_engine_specs() -> Vec<DeferredEngine> {
vec![
DeferredEngine {
id: "engine.dataflow",
reason: "add a conform EngineSpec with a deterministic u32 CPU reference",
},
DeferredEngine {
id: "engine.decode",
reason: "add a conform EngineSpec for the recursive decode pipeline",
},
DeferredEngine {
id: "engine.prefix",
reason: "promote prefix helpers from CPU-side helpers into a typed engine spec",
},
DeferredEngine {
id: "engine.tokenize",
reason: "promote tokenize helpers from host filtering into a typed engine spec",
},
]
}
/// Build a report of migrated and deferred engines.
#[inline]
pub fn migration_report() -> Result<EngineMigrationReport, String> {
let mut bridged = Vec::new();
let mut composable = Vec::new();
for spec in engine_op_specs()? {
match spec.migration_stage() {
EngineMigrationStage::Composable => composable.push(spec.stable_id()),
EngineMigrationStage::BridgeOnly { .. } => bridged.push(spec.stable_id()),
}
}
Ok(EngineMigrationReport {
bridged,
composable,
deferred: deferred_engine_specs(),
})
}
fn required_cpu(id: &str, cpu: Option<CpuReferenceFn>) -> Result<CpuReferenceFn, String> {
match cpu {
Some(cpu) => Ok(cpu),
None => Err(format!(
"{id} has no CPU reference. Fix: add a deterministic pure-Rust u32 reference"
)),
}
}
fn eval_u32_cpu(input: &[u8]) -> Vec<u8> {
let raw = engine::eval::cpu_fn(input);
if raw.len() == 4 && raw == u32::MAX.to_le_bytes() {
return raw;
}
raw.into_iter()
.flat_map(|byte| u32::from(byte).to_le_bytes())
.collect()
}
}
mod harness {
/// Pairwise engine composition harness.
use crate::enforce::enforcers::engine_composition::{
can_compose, compose_cpu, CompositionError, EngineOpSpec,
};
/// Cross-product result for one engine pair.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PairOutcome {
/// First engine id.
pub first_id: &'static str,
/// Second engine id.
pub second_id: &'static str,
/// Pair verification status.
pub status: PairStatus,
}
/// Verification status for one engine pair.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PairStatus {
/// The pair composed and the CPU chain was deterministic.
Passed,
/// The pair was rejected before dispatch with an actionable reason.
Rejected {
/// Compatibility error.
reason: CompositionError,
},
}
/// Run the Stage-1 pairwise bridge harness over `engines`.
///
/// Compatible pairs execute their CPU chain twice and must return identical
/// bytes. Incompatible pairs are accepted only when the compatibility checker
/// produces a structured rejection.
#[inline]
pub fn run_pairwise_bridge_harness(engines: &[EngineOpSpec], input: &[u8]) -> Vec<PairOutcome> {
let mut outcomes = Vec::new();
for first in engines {
for second in engines {
let status = match can_compose(first, second) {
Ok(()) => deterministic_pair_status(first, second, input),
Err(reason) => PairStatus::Rejected { reason },
};
outcomes.push(PairOutcome {
first_id: first.stable_id(),
second_id: second.stable_id(),
status,
});
}
}
outcomes
}
fn deterministic_pair_status(
first: &EngineOpSpec,
second: &EngineOpSpec,
input: &[u8],
) -> PairStatus {
let first_run = compose_cpu(first, second, input);
let second_run = compose_cpu(first, second, input);
match (first_run, second_run) {
(Ok(a), Ok(b)) if a == b => PairStatus::Passed,
(Ok(_), Ok(_)) => PairStatus::Rejected {
reason: CompositionError::CpuNondeterministic {
first_id: first.stable_id().to_string(),
second_id: second.stable_id().to_string(),
},
},
(Err(reason), _) | (_, Err(reason)) => PairStatus::Rejected { reason },
}
}
}
/// Registry entry for `engine_composition` enforcement.
pub struct EngineCompositionEnforcer;
impl crate::enforce::EnforceGate for EngineCompositionEnforcer {
fn id(&self) -> &'static str {
"engine_composition"
}
fn name(&self) -> &'static str {
"engine_composition"
}
fn run(&self, _ctx: &crate::enforce::EnforceCtx<'_>) -> Vec<crate::enforce::Finding> {
let messages = match catalog::migration_report() {
Ok(report) => report
.deferred
.into_iter()
.map(|engine| {
format!(
"engine_composition({}): {}. Fix: complete the engine bridge registration.",
engine.id, engine.reason
)
})
.collect(),
Err(error) => vec![error],
};
crate::enforce::finding_result(self.id(), messages)
}
}
/// Auto-registered `engine_composition` enforcer.
pub const REGISTERED: EngineCompositionEnforcer = EngineCompositionEnforcer;