feat(ext): extension selection + lock v4, inspect --extensions (RFC-EXT-0001 PR-04)
- marqdo.yaml.extensions accepts only ./path or @alias/path, resolves to a regular file inside its owning package, never grants - lock_version 4 records plugin/kernel/provider manifest+artifact identity; v3 requires explicit 'marqdo lock' migration; sync/check/run compare identity - check stays static; run re-verifies artifact bytes; pure/strict refuse - inspect --extensions reports declared/locked identity, plugin descriptors and stdlib aliases with authorization 'not_granted' - black-box extension_selection suite (13) + package-layer tests; specs/ADR-0056 - workspace gate: fmt/clippy clean, 599 passed across 67 targets
This commit is contained in:
1 parent
1f6fd644a0
commit
89555382e0
19 files changed
+2240
-63
No files matched your search
@@ -17,24 +17,31 @@ pub struct InspectFlags {
|
||||
pub refs: bool,
|
||||
/// Show platform capability report.
|
||||
pub platform: bool,
|
||||
/// Show declared/loaded extension identities (never grants).
|
||||
pub extensions: bool,
|
||||
}
|
||||
|
||||
pub fn inspect(file: Option<&Path>, flags: &InspectFlags, json: bool) -> i32 {
|
||||
let prepared = match prepare_project(file, json, &[]) {
|
||||
pub fn inspect(file: Option<&Path>, flags: &InspectFlags, json: bool, plugins: &[PathBuf]) -> i32 {
|
||||
let prepared = match prepare_project(file, json, plugins) {
|
||||
Ok(prepared) => prepared,
|
||||
Err(code) => return code,
|
||||
};
|
||||
let project = prepared.loaded;
|
||||
let project = &prepared.loaded;
|
||||
let entry = project.entry_file();
|
||||
let module = &entry.analysis.module;
|
||||
let selected_any =
|
||||
flags.ast || flags.units || flags.signature || flags.ir || flags.refs || flags.platform;
|
||||
let selected_any = flags.ast
|
||||
|| flags.units
|
||||
|| flags.signature
|
||||
|| flags.ir
|
||||
|| flags.refs
|
||||
|| flags.platform
|
||||
|| flags.extensions;
|
||||
if json {
|
||||
let mut object = serde_json::Map::new();
|
||||
object.insert("command".into(), json!("inspect"));
|
||||
object.insert("module".into(), json!(entry.module_id.to_string()));
|
||||
if !selected_any {
|
||||
object.insert("summary".into(), summary_json(&project));
|
||||
object.insert("summary".into(), summary_json(project));
|
||||
}
|
||||
if flags.ast {
|
||||
object.insert(
|
||||
@@ -58,7 +65,7 @@ pub fn inspect(file: Option<&Path>, flags: &InspectFlags, json: bool) -> i32 {
|
||||
);
|
||||
}
|
||||
if flags.refs {
|
||||
object.insert("refs".into(), json!(refs_json(&project)));
|
||||
object.insert("refs".into(), json!(refs_json(project)));
|
||||
}
|
||||
if flags.platform {
|
||||
object.insert(
|
||||
@@ -66,6 +73,9 @@ pub fn inspect(file: Option<&Path>, flags: &InspectFlags, json: bool) -> i32 {
|
||||
serde_json::to_value(PlatformCapabilities::detect()).unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
if flags.extensions {
|
||||
object.insert("extensions".into(), extensions_json(&prepared, project));
|
||||
}
|
||||
output::print_json(&serde_json::Value::Object(object));
|
||||
return 0;
|
||||
}
|
||||
@@ -120,7 +130,7 @@ pub fn inspect(file: Option<&Path>, flags: &InspectFlags, json: bool) -> i32 {
|
||||
if flags.refs {
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&refs_json(&project)).unwrap_or_default()
|
||||
serde_json::to_string_pretty(&refs_json(project)).unwrap_or_default()
|
||||
);
|
||||
}
|
||||
if flags.platform {
|
||||
@@ -129,5 +139,102 @@ pub fn inspect(file: Option<&Path>, flags: &InspectFlags, json: bool) -> i32 {
|
||||
serde_json::to_string_pretty(&PlatformCapabilities::detect()).unwrap_or_default()
|
||||
);
|
||||
}
|
||||
if flags.extensions {
|
||||
let value = extensions_json(&prepared, project);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&value).unwrap_or_default()
|
||||
);
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
/// Static extension identity projection for `inspect --extensions`.
|
||||
///
|
||||
/// Reports declared sources, lock identity, static plugin descriptors and
|
||||
/// official stdlib aliases. It deliberately never shows environment variables,
|
||||
/// secrets, binary contents or an authorization result beyond
|
||||
/// `not_granted` (inspection does not grant anything).
|
||||
fn extensions_json(
|
||||
prepared: &crate::project_prepare::PreparedProgram,
|
||||
project: &crate::loader::LoadedProject,
|
||||
) -> serde_json::Value {
|
||||
let root = project.root.as_path();
|
||||
let relative = |path: &Path| {
|
||||
path.strip_prefix(root)
|
||||
.map(|value| value.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|_| path.to_string_lossy().to_string())
|
||||
};
|
||||
let declared: Vec<serde_json::Value> = project
|
||||
.extension_sources
|
||||
.iter()
|
||||
.map(|extension| {
|
||||
let identity = prepared.extension_identities.iter().find(|identity| {
|
||||
identity.kind == extension.kind
|
||||
&& identity.source == extension.from
|
||||
&& identity.logical_owner == extension.logical_owner
|
||||
});
|
||||
let locked = project.lock.as_ref().is_some_and(|lock| {
|
||||
identity.is_some_and(|identity| {
|
||||
lock.extension(identity.kind, &identity.name, &identity.logical_owner)
|
||||
.is_some()
|
||||
})
|
||||
});
|
||||
json!({
|
||||
"kind": extension.kind.as_str(),
|
||||
"source": extension.from,
|
||||
"path": relative(&extension.path),
|
||||
"logical_owner": extension.logical_owner,
|
||||
"name": identity.map(|identity| identity.name.clone()),
|
||||
"version": identity.map(|identity| identity.version.clone()),
|
||||
"manifest_digest": identity.map(|identity| identity.manifest_digest.clone()),
|
||||
"artifact_digest": identity.map(|identity| identity.artifact_digest.clone()),
|
||||
"selected_target": identity.map(|identity| identity.selected_target.clone()),
|
||||
"status": if locked { "locked" } else { "unlocked" },
|
||||
"authorization": "not_granted",
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let plugins: Vec<serde_json::Value> = prepared
|
||||
.plugins
|
||||
.plugins
|
||||
.iter()
|
||||
.map(|plugin| {
|
||||
json!({
|
||||
"package": plugin.package,
|
||||
"version": plugin.version,
|
||||
"kind": plugin.kind.as_str(),
|
||||
"manifest_path": relative(&plugin.manifest_path),
|
||||
"manifest_digest": plugin.manifest_sha256,
|
||||
"selected_target": plugin.artifact_target,
|
||||
"artifact_digest": plugin.artifact_sha256,
|
||||
"artifact_path": relative(&plugin.artifact_path()),
|
||||
"exports": plugin.exports.iter().map(|export| json!({
|
||||
"name": export.name,
|
||||
"signature": export.signature,
|
||||
})).collect::<Vec<_>>(),
|
||||
"validation": "static_manifest_ok",
|
||||
"authorization": "not_granted",
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let aliases: Vec<serde_json::Value> = project
|
||||
.official_aliases
|
||||
.iter()
|
||||
.map(|alias| {
|
||||
json!({
|
||||
"public_names": alias.public_names,
|
||||
"target": alias.function.to_string(),
|
||||
"owner_package": alias.owner_package,
|
||||
"source": "verified stdlib bundle",
|
||||
"authorization": "official_bundle",
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
json!({
|
||||
"declared": declared,
|
||||
"plugins": plugins,
|
||||
"stdlib_aliases": aliases,
|
||||
"note": "inspect never loads or executes an extension and never grants a permission",
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Package lock/sync/add/remove commands.
|
||||
|
||||
use super::*;
|
||||
use crate::extension_selection;
|
||||
|
||||
/// `marqdo lock`.
|
||||
pub fn lock(json: bool) -> i32 {
|
||||
@@ -18,19 +19,40 @@ pub fn lock(json: bool) -> i32 {
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
let graph =
|
||||
match marqdo_package::PackageResolver::new(root.clone(), Some(manifest.clone()), None)
|
||||
.with_fetch(true)
|
||||
.with_unlocked(true)
|
||||
.resolve_lock_graph()
|
||||
{
|
||||
Ok(graph) => graph,
|
||||
Err(error) => {
|
||||
eprintln!("error: {error}");
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let lockfile = graph.lock;
|
||||
let resolver = marqdo_package::PackageResolver::new(root.clone(), Some(manifest.clone()), None)
|
||||
.with_fetch(true)
|
||||
.with_unlocked(true);
|
||||
let graph = match resolver.resolve_lock_graph() {
|
||||
Ok(graph) => graph,
|
||||
Err(error) => {
|
||||
eprintln!("error: {error}");
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let mut lockfile = graph.lock;
|
||||
// Lock v4: record the exact extension identity declared by the project.
|
||||
// Reading manifests/declared digests is static; no binary is loaded and no
|
||||
// grant is implied.
|
||||
let extension_sources = match resolver.resolve_extension_sources() {
|
||||
Ok(sources) => sources,
|
||||
Err(error) => {
|
||||
eprintln!("error: extension selection: {error}");
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let identities = match extension_selection::extension_identities(&extension_sources) {
|
||||
Ok(identities) => identities,
|
||||
Err(error) => {
|
||||
eprintln!("error[{}]: {}", error.code, error.message);
|
||||
return error.exit_code;
|
||||
}
|
||||
};
|
||||
for identity in identities {
|
||||
if let Err(error) = lockfile.merge_extension(identity) {
|
||||
eprintln!("error[ext.extension_lock_mismatch]: {error}");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
let path = root.join(marqdo_package::LOCK_FILE);
|
||||
if let Err(error) = write_atomic(&path, lockfile.to_yaml().as_bytes()) {
|
||||
eprintln!("error: cannot write {}: {error}", path.display());
|
||||
@@ -41,6 +63,7 @@ pub fn lock(json: bool) -> i32 {
|
||||
"command": "lock",
|
||||
"success": true,
|
||||
"packages": lockfile.packages,
|
||||
"extensions": lockfile.extensions,
|
||||
}));
|
||||
} else {
|
||||
println!(
|
||||
@@ -90,6 +113,30 @@ pub fn sync(locked: bool, json: bool) -> i32 {
|
||||
let resolver =
|
||||
marqdo_package::PackageResolver::new(root, Some(manifest.clone()), Some(lockfile.clone()))
|
||||
.with_fetch(!locked);
|
||||
// Verify the declared extension selection against the lock before using
|
||||
// any materialized content. A missing/changed identity fails closed.
|
||||
let extension_sources = match resolver.resolve_extension_sources() {
|
||||
Ok(sources) => sources,
|
||||
Err(error) => {
|
||||
eprintln!("error: extension selection: {error}");
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
let identities = match extension_selection::extension_identities(&extension_sources) {
|
||||
Ok(identities) => identities,
|
||||
Err(error) => {
|
||||
eprintln!("error[{}]: {}", error.code, error.message);
|
||||
return error.exit_code;
|
||||
}
|
||||
};
|
||||
if let Err(error) = lockfile.validate_extension_declarations(&manifest) {
|
||||
eprintln!("error[ext.extension_lock_mismatch]: {error}");
|
||||
return 1;
|
||||
}
|
||||
if let Err(error) = extension_selection::validate_against_lock(&lockfile, &identities) {
|
||||
eprintln!("error[{}]: {}", error.code, error.message);
|
||||
return error.exit_code;
|
||||
}
|
||||
if let Err(error) = resolver.materialize_env(&lockfile) {
|
||||
eprintln!("error: cannot materialize project environment: {error}");
|
||||
return 1;
|
||||
|
||||
@@ -20,6 +20,7 @@ pub fn execute(
|
||||
Ok(prepared) => prepared,
|
||||
Err(code) => return code,
|
||||
};
|
||||
let extension_sources = prepared.loaded.extension_sources.clone();
|
||||
let project = prepared.loaded;
|
||||
let static_plugins = prepared.plugins;
|
||||
if project.has_errors() {
|
||||
@@ -51,6 +52,43 @@ pub fn execute(
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
// A project-declared extension is activation, not authorization, but it is
|
||||
// still third-party executable code. `pure`/`strict` never activate it.
|
||||
if !extension_sources.is_empty() && mode != SandboxMode::Project {
|
||||
let code = if mode == SandboxMode::Strict {
|
||||
"strict.unsupported_grant"
|
||||
} else {
|
||||
"cli.invalid_grants"
|
||||
};
|
||||
let message = format!(
|
||||
"this project declares {} extension activation source(s) in {}; \
|
||||
--mode {} cannot activate third-party extensions. Use --mode project \
|
||||
with explicit grants, or remove the extensions block",
|
||||
extension_sources.len(),
|
||||
marqdo_package::MANIFEST_FILE,
|
||||
settings.mode
|
||||
);
|
||||
emit_error(json, command_name, code, &message);
|
||||
return 2;
|
||||
}
|
||||
let mut kernel_paths = settings.kernels.clone();
|
||||
kernel_paths.extend(
|
||||
extension_sources
|
||||
.iter()
|
||||
.filter(|extension| extension.kind == marqdo_package::ExtensionKind::Kernel)
|
||||
.map(|extension| extension.path.clone()),
|
||||
);
|
||||
kernel_paths.sort();
|
||||
kernel_paths.dedup();
|
||||
let mut provider_paths = settings.providers.clone();
|
||||
provider_paths.extend(
|
||||
extension_sources
|
||||
.iter()
|
||||
.filter(|extension| extension.kind == marqdo_package::ExtensionKind::Provider)
|
||||
.map(|extension| extension.path.clone()),
|
||||
);
|
||||
provider_paths.sort();
|
||||
provider_paths.dedup();
|
||||
if mode == SandboxMode::Strict {
|
||||
if let Err(error) = settings.strict_preflight() {
|
||||
emit_error(json, command_name, "strict.unsupported_grant", &error);
|
||||
@@ -121,7 +159,7 @@ pub fn execute(
|
||||
if let Some(timeout) = settings.timeout {
|
||||
host.budget.deadline = Some(Instant::now() + Duration::from_secs(timeout));
|
||||
}
|
||||
for kernel_path in &settings.kernels {
|
||||
for kernel_path in &kernel_paths {
|
||||
if let Err(error) = load_kernel_descriptor(&mut host, kernel_path) {
|
||||
if json {
|
||||
output::print_json(&json!({"success": false, "error": error}));
|
||||
@@ -139,7 +177,7 @@ pub fn execute(
|
||||
// current target artifact, check the exact grant and reject duplicate
|
||||
// names/kinds *before* any provider or plugin worker process is spawned
|
||||
// (RFC-EXT-0001 §5.4.6/§18.2 T6–T7).
|
||||
let providers = match provider_manifest::read_provider_manifests(&settings.providers) {
|
||||
let providers = match provider_manifest::read_provider_manifests(&provider_paths) {
|
||||
Ok(providers) => providers,
|
||||
Err(error) => {
|
||||
emit_prepare_error(json, command_name, &error);
|
||||
|
||||
@@ -0,0 +1,318 @@
|
||||
//! Extension selection identities (RFC-EXT-0001 §6, ADR-0056).
|
||||
//!
|
||||
//! `marqdo.yaml.extensions` declares activation sources; the lock file records
|
||||
//! the exact selected identity (manifest digest, artifact digest, target).
|
||||
//! This module computes those identities from static manifests only:
|
||||
//!
|
||||
//! * it never `dlopen`s a library, spawns a worker or runs a script;
|
||||
//! * it reads the declared artifact SHA-256 (plus the manifest bytes) so
|
||||
//! `check`/`sync` can compare against the lock without requiring a built
|
||||
//! binary; `run` separately verifies the actual artifact bytes before use.
|
||||
//!
|
||||
//! A declaration is visibility/activation, never a grant. Grants stay in
|
||||
//! `--allow-plugin`/`--allow-provider`/`--allow-foreign` and the capability
|
||||
//! broker.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use marqdo_package::{ExtensionKind, LockedExtension, ResolvedExtension};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::extension_descriptors::PrepareError;
|
||||
|
||||
/// Compute the declared identity of every resolved extension source.
|
||||
///
|
||||
/// Duplicate `(kind, name, logical_owner)` identities with different digests
|
||||
/// are rejected deterministically; the same identity declared twice is
|
||||
/// idempotent.
|
||||
pub fn extension_identities(
|
||||
resolved: &[ResolvedExtension],
|
||||
) -> Result<Vec<LockedExtension>, PrepareError> {
|
||||
let mut identities = Vec::new();
|
||||
let mut seen: BTreeSet<(ExtensionKind, String, String)> = BTreeSet::new();
|
||||
for extension in resolved {
|
||||
let identity = match extension.kind {
|
||||
ExtensionKind::Plugin => plugin_identity(extension)?,
|
||||
ExtensionKind::Provider => provider_identity(extension)?,
|
||||
ExtensionKind::Kernel => kernel_identity(extension)?,
|
||||
};
|
||||
let key = (
|
||||
identity.kind,
|
||||
identity.name.clone(),
|
||||
identity.logical_owner.clone(),
|
||||
);
|
||||
if !seen.insert(key.clone()) {
|
||||
let existing = identities
|
||||
.iter()
|
||||
.find(|item: &&LockedExtension| {
|
||||
item.kind == key.0 && item.name == key.1 && item.logical_owner == key.2
|
||||
})
|
||||
.expect("identity was inserted with its key");
|
||||
if existing == &identity {
|
||||
continue;
|
||||
}
|
||||
return Err(PrepareError::option(
|
||||
"ext.duplicate_callable",
|
||||
format!(
|
||||
"extension {} `{}` for owner `{}` is declared with two different manifests",
|
||||
key.0, key.1, key.2
|
||||
),
|
||||
));
|
||||
}
|
||||
identities.push(identity);
|
||||
}
|
||||
identities.sort_by(|left, right| {
|
||||
(&left.kind, &left.name, &left.logical_owner).cmp(&(
|
||||
&right.kind,
|
||||
&right.name,
|
||||
&right.logical_owner,
|
||||
))
|
||||
});
|
||||
Ok(identities)
|
||||
}
|
||||
|
||||
fn plugin_identity(extension: &ResolvedExtension) -> Result<LockedExtension, PrepareError> {
|
||||
let manifest_bytes = std::fs::read(&extension.path).map_err(|error| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_unreadable",
|
||||
format!(
|
||||
"cannot read plugin manifest `{}`: {error}",
|
||||
extension.path.display()
|
||||
),
|
||||
)
|
||||
})?;
|
||||
let manifest = marqdo_plugin_abi::PluginManifest::load(&extension.path)
|
||||
.map_err(|error| PrepareError::option(error.code.clone(), error.message.clone()))?;
|
||||
manifest
|
||||
.validate()
|
||||
.map_err(|error| PrepareError::option(error.code.clone(), error.message.clone()))?;
|
||||
let host = marqdo_plugin_abi::HostTarget::current();
|
||||
let artifact = manifest
|
||||
.resolve_artifact(&host)
|
||||
.map_err(|error| PrepareError::option("ext.no_matching_artifact", error.message))?;
|
||||
Ok(LockedExtension {
|
||||
kind: ExtensionKind::Plugin,
|
||||
logical_owner: extension.logical_owner.clone(),
|
||||
name: manifest.plugin.name.clone(),
|
||||
version: manifest.plugin.version.clone(),
|
||||
manifest_digest: hex(&Sha256::digest(&manifest_bytes)),
|
||||
artifact_digest: artifact.sha256.clone(),
|
||||
selected_target: host.triple().to_string(),
|
||||
source: extension.from.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn provider_identity(extension: &ResolvedExtension) -> Result<LockedExtension, PrepareError> {
|
||||
let manifest_bytes = std::fs::read(&extension.path).map_err(|error| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_unreadable",
|
||||
format!(
|
||||
"cannot read provider manifest `{}`: {error}",
|
||||
extension.path.display()
|
||||
),
|
||||
)
|
||||
})?;
|
||||
let manifest: serde_json::Value = serde_json::from_slice(&manifest_bytes).map_err(|error| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_unreadable",
|
||||
format!(
|
||||
"invalid provider manifest `{}`: {error}",
|
||||
extension.path.display()
|
||||
),
|
||||
)
|
||||
})?;
|
||||
if manifest.get("schema").and_then(serde_json::Value::as_str) != Some("marqdo.provider") {
|
||||
return Err(PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!(
|
||||
"`{}` must declare schema `marqdo.provider`",
|
||||
extension.path.display()
|
||||
),
|
||||
));
|
||||
}
|
||||
if manifest
|
||||
.get("schema_version")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
!= Some(2)
|
||||
{
|
||||
return Err(PrepareError::option(
|
||||
"ext.unsupported_provider_schema",
|
||||
format!(
|
||||
"`{}` is not a provider schema version 2 manifest; run `marqdo lock` only with v2",
|
||||
extension.path.display()
|
||||
),
|
||||
));
|
||||
}
|
||||
let name = manifest
|
||||
.get("name")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("`{}` has no name", extension.path.display()),
|
||||
)
|
||||
})?;
|
||||
let version = manifest
|
||||
.get("version")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("`{}` has no version", extension.path.display()),
|
||||
)
|
||||
})?;
|
||||
let target = marqdo_plugin_abi::HostTarget::current()
|
||||
.triple()
|
||||
.to_string();
|
||||
let artifacts = manifest
|
||||
.get("artifacts")
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("`{}` has no artifacts array", extension.path.display()),
|
||||
)
|
||||
})?;
|
||||
let artifact = artifacts
|
||||
.iter()
|
||||
.find(|artifact| {
|
||||
artifact.get("target").and_then(serde_json::Value::as_str) == Some(&target)
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.no_matching_artifact",
|
||||
format!("provider `{name}` has no artifact for target `{target}`"),
|
||||
)
|
||||
})?;
|
||||
let sha256 = artifact
|
||||
.get("sha256")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("provider `{name}` artifact has no sha256"),
|
||||
)
|
||||
})?;
|
||||
if sha256.len() != 64 || !sha256.bytes().all(|byte| byte.is_ascii_hexdigit()) {
|
||||
return Err(PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("provider `{name}` artifact sha256 is not 64 hex characters"),
|
||||
));
|
||||
}
|
||||
Ok(LockedExtension {
|
||||
kind: ExtensionKind::Provider,
|
||||
logical_owner: extension.logical_owner.clone(),
|
||||
name: name.to_string(),
|
||||
version: version.to_string(),
|
||||
manifest_digest: hex(&Sha256::digest(&manifest_bytes)),
|
||||
artifact_digest: sha256.to_ascii_lowercase(),
|
||||
selected_target: target,
|
||||
source: extension.from.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
fn kernel_identity(extension: &ResolvedExtension) -> Result<LockedExtension, PrepareError> {
|
||||
let manifest_bytes = std::fs::read(&extension.path).map_err(|error| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_unreadable",
|
||||
format!(
|
||||
"cannot read kernel descriptor `{}`: {error}",
|
||||
extension.path.display()
|
||||
),
|
||||
)
|
||||
})?;
|
||||
let manifest: serde_json::Value = serde_json::from_slice(&manifest_bytes).map_err(|error| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_unreadable",
|
||||
format!(
|
||||
"invalid kernel descriptor `{}`: {error}",
|
||||
extension.path.display()
|
||||
),
|
||||
)
|
||||
})?;
|
||||
let name = manifest
|
||||
.get("provider_id")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("`{}` has no provider_id", extension.path.display()),
|
||||
)
|
||||
})?;
|
||||
let wire_version = manifest
|
||||
.get("wire_version")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("`{}` has no wire_version", extension.path.display()),
|
||||
)
|
||||
})?;
|
||||
if wire_version != 1 {
|
||||
return Err(PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("kernel descriptor `{name}` uses unsupported wire_version {wire_version}"),
|
||||
));
|
||||
}
|
||||
let digest = manifest
|
||||
.get("digest")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("kernel descriptor `{name}` has no digest"),
|
||||
)
|
||||
})?;
|
||||
if digest.len() != 64 || !digest.bytes().all(|byte| byte.is_ascii_hexdigit()) {
|
||||
return Err(PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("kernel descriptor `{name}` digest is not 64 hex characters"),
|
||||
));
|
||||
}
|
||||
let entrypoint = manifest
|
||||
.get("entrypoint")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or_else(|| {
|
||||
PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("kernel descriptor `{name}` has no entrypoint"),
|
||||
)
|
||||
})?;
|
||||
// The descriptor's entrypoint is package/descriptor-relative or absolute
|
||||
// (existing foreign.md contract). Reject explicit path escaping here.
|
||||
if entrypoint.is_empty() {
|
||||
return Err(PrepareError::option(
|
||||
"ext.manifest_invalid",
|
||||
format!("kernel descriptor `{name}` has an empty entrypoint"),
|
||||
));
|
||||
}
|
||||
Ok(LockedExtension {
|
||||
kind: ExtensionKind::Kernel,
|
||||
logical_owner: extension.logical_owner.clone(),
|
||||
name: name.to_string(),
|
||||
version: format!("wire:{wire_version}"),
|
||||
manifest_digest: hex(&Sha256::digest(&manifest_bytes)),
|
||||
artifact_digest: digest.to_ascii_lowercase(),
|
||||
selected_target: marqdo_plugin_abi::HostTarget::current()
|
||||
.triple()
|
||||
.to_string(),
|
||||
source: extension.from.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Validate that the lock still describes the current declared selection.
|
||||
pub fn validate_against_lock(
|
||||
lock: &marqdo_package::LockFile,
|
||||
identities: &[LockedExtension],
|
||||
) -> Result<(), PrepareError> {
|
||||
lock.validate_extension_selection(identities)
|
||||
.map_err(|message| PrepareError::load("ext.extension_lock_mismatch", message))
|
||||
}
|
||||
|
||||
fn hex(bytes: &[u8]) -> String {
|
||||
let mut out = String::with_capacity(bytes.len() * 2);
|
||||
for byte in bytes {
|
||||
out.push_str(&format!("{byte:02x}"));
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -163,6 +163,12 @@ pub struct LoadedProject {
|
||||
/// These are ordinary `Marqdo` bindings; the runtime has no print special
|
||||
/// case and no `Program.print_override`.
|
||||
pub official_aliases: Vec<marqdo_ir::callable::OfficialAlias>,
|
||||
/// Project manifest, when the entry belongs to a project.
|
||||
pub manifest: Option<Manifest>,
|
||||
/// Verified lock file, when present (lock v4).
|
||||
pub lock: Option<LockFile>,
|
||||
/// Resolved `marqdo.yaml.extensions` activation sources (static paths only).
|
||||
pub extension_sources: Vec<marqdo_package::ResolvedExtension>,
|
||||
}
|
||||
|
||||
impl LoadedProject {
|
||||
@@ -271,7 +277,7 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
||||
}
|
||||
None
|
||||
};
|
||||
let resolver = PackageResolver::new(root.clone(), manifest.clone(), lock);
|
||||
let resolver = PackageResolver::new(root.clone(), manifest.clone(), lock.clone());
|
||||
let entry_module = resolver.resolve_entry(&entry)?;
|
||||
let stdlib = StdlibBundle::discover().map_err(|error| format!("stdlib: {error}"))?;
|
||||
let mut loader = Loader {
|
||||
@@ -290,6 +296,10 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
||||
// the target module/function; only aliases the program actually references
|
||||
// load their module, and the runtime binds them as ordinary Marqdo calls.
|
||||
let official_aliases = loader.load_official_aliases()?;
|
||||
let extension_sources = loader
|
||||
.resolver
|
||||
.resolve_extension_sources()
|
||||
.map_err(|error| format!("extension selection: {error}"))?;
|
||||
|
||||
let stdlib_info = loader.stdlib.as_ref().map(|bundle| StdlibInfo {
|
||||
version: bundle.version().to_string(),
|
||||
@@ -314,6 +324,9 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
|
||||
diagnostics: loader.diagnostics,
|
||||
stdlib: stdlib_info,
|
||||
official_aliases,
|
||||
manifest,
|
||||
lock,
|
||||
extension_sources,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ mod analysis;
|
||||
mod args;
|
||||
mod commands;
|
||||
mod extension_descriptors;
|
||||
mod extension_selection;
|
||||
mod loader;
|
||||
mod output;
|
||||
mod project_prepare;
|
||||
@@ -261,6 +262,12 @@ struct InspectArgs {
|
||||
/// Print sandbox/platform capability report.
|
||||
#[arg(long)]
|
||||
platform: bool,
|
||||
/// Print declared/loaded extension identities (never grants).
|
||||
#[arg(long)]
|
||||
extensions: bool,
|
||||
/// Explicitly selected plugin manifest for extension inspection (repeatable).
|
||||
#[arg(long = "plugin", value_name = "PATH")]
|
||||
plugin: Vec<PathBuf>,
|
||||
/// Machine readable JSON output.
|
||||
#[arg(long)]
|
||||
json: bool,
|
||||
@@ -421,8 +428,9 @@ fn dispatch() -> i32 {
|
||||
ir: args.ir,
|
||||
refs: args.refs,
|
||||
platform: args.platform,
|
||||
extensions: args.extensions,
|
||||
};
|
||||
commands::inspect(args.file.as_deref(), &flags, args.json)
|
||||
commands::inspect(args.file.as_deref(), &flags, args.json, &args.plugin)
|
||||
}
|
||||
Command::Run(args) => commands::execute(
|
||||
args.file.as_deref(),
|
||||
|
||||
@@ -14,8 +14,10 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use marqdo_ir::callable::CallableRegistry;
|
||||
use marqdo_package::{ExtensionKind, LockedExtension};
|
||||
|
||||
use crate::extension_descriptors::{read_plugin_descriptors, PrepareError, StaticPluginSet};
|
||||
use crate::extension_selection;
|
||||
use crate::loader::{self, FileDiagnostic, LoadedProject};
|
||||
|
||||
/// Options that influence static preparation.
|
||||
@@ -37,6 +39,11 @@ pub struct PreparedProgram {
|
||||
/// evidence) can report the same static table without rebuilding it.
|
||||
#[allow(dead_code)]
|
||||
pub registry: CallableRegistry,
|
||||
/// Static identities of the declared extensions, already compared with the
|
||||
/// lock when one is present (never grants). Consumed by
|
||||
/// `inspect --extensions` and evidence.
|
||||
#[allow(dead_code)]
|
||||
pub extension_identities: Vec<LockedExtension>,
|
||||
}
|
||||
|
||||
/// Prepare one document without executing any project or extension code.
|
||||
@@ -49,8 +56,48 @@ pub fn prepare(
|
||||
let mut loaded = loader::load(file.unwrap_or_else(|| std::path::Path::new("index.mq.md")))
|
||||
.map_err(|message| default_load_error(&message))?;
|
||||
|
||||
// T2: extension descriptors are read from manifests only.
|
||||
let plugins = read_plugin_descriptors(&options.plugins)?;
|
||||
// T2: extension descriptors are read from manifests only. Explicit CLI
|
||||
// `--plugin` paths are merged with the project's declared and locked
|
||||
// activation sources; CLI paths remain a development form and grant
|
||||
// nothing by themselves.
|
||||
let mut plugin_paths = options.plugins.clone();
|
||||
for extension in &loaded.extension_sources {
|
||||
if extension.kind == ExtensionKind::Plugin {
|
||||
plugin_paths.push(extension.path.clone());
|
||||
}
|
||||
}
|
||||
plugin_paths.sort();
|
||||
plugin_paths.dedup();
|
||||
let plugins = read_plugin_descriptors(&plugin_paths)?;
|
||||
|
||||
// T3a: declared extensions must have a locked identity (lock v4). The
|
||||
// declaration is activation, not authorization; a mismatched/missing lock
|
||||
// is fail-closed rather than silently "latest".
|
||||
let extension_identities =
|
||||
extension_selection::extension_identities(&loaded.extension_sources)?;
|
||||
if !loaded.extension_sources.is_empty() {
|
||||
match &loaded.lock {
|
||||
Some(lock) => {
|
||||
lock.validate_extension_declarations(
|
||||
loaded
|
||||
.manifest
|
||||
.as_ref()
|
||||
.expect("extension sources require a project manifest"),
|
||||
)
|
||||
.map_err(|message| PrepareError::load("ext.extension_lock_mismatch", message))?;
|
||||
extension_selection::validate_against_lock(lock, &extension_identities)?;
|
||||
}
|
||||
None => {
|
||||
return Err(PrepareError::load(
|
||||
"ext.extension_lock_mismatch",
|
||||
format!(
|
||||
"the project declares {} extension(s) but marqdo.lock is missing; run `marqdo lock` first",
|
||||
loaded.extension_sources.len()
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// T3: unique static table. Duplicate exports/builtin shadowing are rejected
|
||||
// before any call site can be bound.
|
||||
@@ -126,6 +173,7 @@ pub fn prepare(
|
||||
loaded,
|
||||
plugins,
|
||||
registry,
|
||||
extension_identities,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user