Implements issue #3 (RFC-EXT-0003) slices on the post-3.0.0-beta.1 tree. PR-00/01/H-06: reproduce and fix sibling heading mode inheritance. build_units now pops the current and same-or-higher-level headings from the ancestor stack before deriving the inherited mode, so an unmarked heading can never inherit the previous sibling. Covered by the units matrix, conformance fixtures 22/23, and black-box heading_inheritance through check --emit/run/call. PR-02/H-08: the deterministic release archive now ships the complete verified stdlib/ tree. package.py and smoke.py recompute the runtime tree/alias digests, record them in RELEASE_INFO.json.stdlib, and fail on mismatch; smoke removes the bundle and requires a fail-closed run. Release discovery no longer falls back to the compile-time source tree (debug builds keep that development convenience). PR-03/H-04: one marqdo_host::CORE_RESOURCE_KINDS registry is used by both the CLI provider-manifest validator and every trusted host registration path; all six core kinds are rejected as provider name or declared kind before registration. H-03: docs/architecture/resource-operations.md records every reviewed resource_request operation and architecture_boundaries pins the exact allowlist; new domain operations are forbidden in core (ADR-0062). PR-04/H-01: remove math.parse/eval/substitute/simplify/differentiate from BUILTINS, HostOp, the runtime, the host kernel and the workspace (crates/marqdo-math deleted). Formula stays inert; Int/Float arithmetic stays. Math algorithms are a pure examples/math-library package plus an optional examples/math-backend ABI plugin that requests the math.compute capability (ADR-0061). Removed calls are static symbol.unknown; the math import namespace is released again. Tests/examples/specs migrated; new math_library black-box test. PR-05/06 Step A: ADR-0060 classifies @exec/@knowledge as the closed Core Modes and specifies the Proposed metadata-only Heading Annotations v1 (target 3.1, sem.invalid_mode_marker unchanged, Analysis v2 unchanged). ADR-0063 records the foreign F-A retention and deferred F-B removal. Docs/status/compatibility/changelog and the RFC-EXT-0003 evidence file are updated. Full local gate: cargo fmt PASS, clippy -D warnings PASS, workspace tests 586 passed/0 failed/2 ignored, python tools/ci 28 passed.
172 lines
6.0 KiB
Rust
172 lines
6.0 KiB
Rust
//! Foreign-kernel adapters.
|
|
//!
|
|
//! This module is the bridge between the capability host and the native foreign
|
|
//! kernel crate. Every operation is capability-checked before the kernel is
|
|
//! touched; kernel failures are converted to structured runtime errors, never
|
|
//! panics. Domain math is not a host kernel (ADR-0061): it belongs to a pure
|
|
//! `.mq.md` package or an explicitly selected ABI backend.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::time::Duration;
|
|
|
|
use marqdo_value::{to_wire, ErrorCategory, RuntimeError, Value};
|
|
|
|
use crate::context::ExecutionContext;
|
|
use crate::host::{HostOp, StdHost};
|
|
|
|
pub(crate) fn foreign(
|
|
host: &mut StdHost,
|
|
op: HostOp,
|
|
args: Vec<Value>,
|
|
ctx: &ExecutionContext,
|
|
) -> Result<Value, RuntimeError> {
|
|
host.capabilities.check_foreign(ctx.span)?;
|
|
let span = ctx.span;
|
|
let Some(Value::Code(code)) = args.first() else {
|
|
return Err(RuntimeError::type_error(
|
|
span,
|
|
"foreign kernel expects a Code value from `ref(\"#代码块\")`",
|
|
));
|
|
};
|
|
if code.language.trim().is_empty() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::KernelUnavailable,
|
|
span,
|
|
"code fence has no language, so no kernel can be selected",
|
|
));
|
|
}
|
|
let input = match args.get(1) {
|
|
None | Some(Value::None) => None,
|
|
Some(value) => Some(to_wire(value)),
|
|
};
|
|
let request = marqdo_foreign::RunRequest {
|
|
language: code.language.clone(),
|
|
source: code.source.clone(),
|
|
input,
|
|
timeout: host.foreign_timeout,
|
|
max_output_bytes: host.foreign_output_limit,
|
|
};
|
|
match op {
|
|
HostOp::ForeignRun => {
|
|
let result = marqdo_foreign::run_with_registry(&request, &host.kernel_registry)
|
|
.map_err(|error| foreign_error(error, span))?;
|
|
Ok(foreign_result_map(result))
|
|
}
|
|
HostOp::ForeignCall => {
|
|
let result = marqdo_foreign::call_with_registry(&request, &host.kernel_registry)
|
|
.map_err(|error| foreign_error(error, span))?;
|
|
if result.status != marqdo_foreign::RunStatus::Success {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ForeignFailure,
|
|
span,
|
|
format!(
|
|
"foreign call failed (status {:?}): {}",
|
|
result.status, result.stderr
|
|
),
|
|
));
|
|
}
|
|
let value = result.value.ok_or_else(|| {
|
|
RuntimeError::new(
|
|
ErrorCategory::ForeignFailure,
|
|
span,
|
|
"foreign call returned no typed value",
|
|
)
|
|
})?;
|
|
if crate::handles::contains_plugin_handle(&value) {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::PermissionDenied,
|
|
span,
|
|
"plugin_handle.untrusted: foreign kernel output may not mint host handles",
|
|
));
|
|
}
|
|
Ok(value)
|
|
}
|
|
_ => Err(RuntimeError::new(
|
|
ErrorCategory::Internal,
|
|
span,
|
|
"unexpected foreign operation",
|
|
)),
|
|
}
|
|
}
|
|
|
|
fn foreign_result_map(result: marqdo_foreign::RunResult) -> Value {
|
|
let mut map = BTreeMap::new();
|
|
map.insert(
|
|
"status".to_string(),
|
|
Value::text(match result.status {
|
|
marqdo_foreign::RunStatus::Success => "success",
|
|
marqdo_foreign::RunStatus::Failed => "failed",
|
|
marqdo_foreign::RunStatus::TimedOut => "timed_out",
|
|
}),
|
|
);
|
|
map.insert("stdout".to_string(), Value::text(result.stdout));
|
|
map.insert("stderr".to_string(), Value::text(result.stderr));
|
|
map.insert(
|
|
"exit_code".to_string(),
|
|
match result.exit_code {
|
|
Some(code) => Value::int(code as i64),
|
|
None => Value::None,
|
|
},
|
|
);
|
|
map.insert(
|
|
"duration_ms".to_string(),
|
|
Value::int(result.duration.as_millis() as i64),
|
|
);
|
|
map.insert(
|
|
"value".to_string(),
|
|
match result.value {
|
|
Some(value) => value,
|
|
None => Value::None,
|
|
},
|
|
);
|
|
Value::Map(map)
|
|
}
|
|
|
|
fn foreign_error(
|
|
error: marqdo_foreign::ForeignError,
|
|
span: Option<marqdo_span::Span>,
|
|
) -> RuntimeError {
|
|
use marqdo_foreign::ForeignError;
|
|
let (category, message) = match error {
|
|
ForeignError::KernelUnavailable { language, hint } => (
|
|
ErrorCategory::KernelUnavailable,
|
|
format!("foreign kernel `{language}` is unavailable: {hint}"),
|
|
),
|
|
ForeignError::Spawn { message } => (ErrorCategory::HostError, message),
|
|
ForeignError::Io { message } => (ErrorCategory::HostError, message),
|
|
ForeignError::Timeout { seconds } => (
|
|
ErrorCategory::ResourceExhausted,
|
|
format!("foreign kernel exceeded its {seconds}s timeout"),
|
|
),
|
|
ForeignError::Protocol { message } => (ErrorCategory::ForeignFailure, message),
|
|
ForeignError::Integrity {
|
|
provider,
|
|
expected,
|
|
actual,
|
|
} => (
|
|
ErrorCategory::KernelUnavailable,
|
|
format!(
|
|
"foreign kernel `{provider}` failed integrity verification: expected {expected}, got {actual}"
|
|
),
|
|
),
|
|
ForeignError::EnvironmentIntegrity {
|
|
environment,
|
|
expected,
|
|
actual,
|
|
} => (
|
|
ErrorCategory::KernelUnavailable,
|
|
format!(
|
|
"foreign kernel environment `{environment}` failed integrity verification: expected {expected}, got {actual}"
|
|
),
|
|
),
|
|
ForeignError::OutputTooLarge { limit } => (
|
|
ErrorCategory::ResourceExhausted,
|
|
format!("foreign kernel exceeded the {limit} byte output limit"),
|
|
),
|
|
};
|
|
RuntimeError::new(category, span, message)
|
|
}
|
|
|
|
/// Default per-call foreign timeout used by the CLI.
|
|
pub const DEFAULT_FOREIGN_TIMEOUT: Duration = Duration::from_secs(10);
|