Files
marqdo-cli/crates/marqdo-host/tests/worker_provider.rs
T
cflmy 9b4f0a0822
ci/woodpecker/pr/main Pipeline was successful
ci/woodpecker/push/main Pipeline was canceled
refactor: extract neutral host contract and decouple plugin ABI (issue #13)
Implements the core M0-M3 boundary work from issue #13 / ADR-0071 without
changing language semantics or any public protocol:

- add marqdo-host-contract owning Host/HostOp/ExecutionContext/ResourceRead
  and the runtime observation facts; the engine depends on it only.
- marqdo-runtime no longer links marqdo-host; the native host implements the
  contract and has no reverse edge to runtime/frontend/IR.
- reduce marqdo-plugin-abi to the public C ABI/wire/error contract; move
  manifest/artifact/loader/worker execution into marqdo-host::plugin so the
  SDK carries no loader/worker/host dependency.
- add a real independent consumer (tests/embedding, own workspace+lock) that
  runs Markdown -> frontend -> IR -> runtime with bounded budgets and explicit
  effect refusal, plus differential CLI comparison.
- add an SDK-only third-party plugin fixture and real CLI invocation gate.
- replace the frozen-crate architecture test with a reviewed responsibility
  graph enforced from Cargo metadata, with negative unit cases.
- consolidate current docs/entry: ADR-0071, CURRENT responsibility map,
  developers/architecture indexes, historical banners, STATUS and evidence.
- record keep-in-workspace for Engine/Native Host; no empty candidate repo.

Evidence: docs/evidence/2026-11-26-issue-13-engine-host-boundary.md
Refs #13
2026-11-26 18:37:02 +08:00

164 lines
4.9 KiB
Rust

use marqdo_host::{Capabilities, StdHost};
use marqdo_host_contract::Host;
use marqdo_sandbox::SandboxMode;
use marqdo_value::ErrorCategory;
fn context() -> marqdo_host_contract::ExecutionContext {
marqdo_host_contract::ExecutionContext::new("test".to_string(), None)
}
fn handle(value: marqdo_value::Value) -> marqdo_value::ResourceHandle {
match value {
marqdo_value::Value::Resource(handle) => handle,
other => panic!("expected resource, got {other:?}"),
}
}
fn helper() -> (tempfile::TempDir, std::path::PathBuf) {
let dir = tempfile::tempdir().unwrap();
let script = dir.path().join("provider.py");
std::fs::write(
&script,
r#"
import sys, json, struct
def read_frame():
header = sys.stdin.buffer.read(4)
if len(header) < 4:
return None
size = struct.unpack(">I", header)[0]
return json.loads(sys.stdin.buffer.read(size))
def write_frame(value):
payload = json.dumps(value).encode()
sys.stdout.buffer.write(struct.pack(">I", len(payload)) + payload)
sys.stdout.buffer.flush()
data = b"hello world"
position = 0
while True:
request = read_frame()
if request is None:
break
op = request["op"]
if op == "ping":
write_frame({"ok": True})
elif op == "open":
write_frame({"ok": True, "token": 7})
elif op == "read":
count = request["max_bytes"]
chunk = data[position:position + count]
position += len(chunk)
write_frame({"ok": True, "bytes": list(chunk), "eof": position >= len(data)})
elif op == "write":
write_frame({"ok": True, "count": len(request["bytes"])})
elif op == "close":
write_frame({"ok": True})
elif op == "shutdown":
break
else:
write_frame({"ok": False, "code": "resource.unsupported", "message": op})
"#,
)
.unwrap();
(dir, script)
}
#[test]
fn worker_provider_round_trips_through_framed_json() {
let (dir, script) = helper();
let project = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_resource_provider("worker"),
project.path(),
);
host.register_worker_resource_provider(
"worker",
&["worker.sensor"],
&std::path::PathBuf::from("python3"),
&[script.to_string_lossy().to_string()],
)
.unwrap();
let ctx = context();
let handle = handle(
host.resource_open("worker.sensor", "sensor-1", &["read".to_string()], &ctx)
.unwrap(),
);
let first = host.resource_read(handle, 5, &ctx).unwrap();
assert_eq!(first.bytes, b"hello");
assert!(!first.eof);
let rest = host.resource_read(handle, 64, &ctx).unwrap();
assert_eq!(rest.bytes, b" world");
assert!(rest.eof);
assert_eq!(host.resource_write(handle, b"abc", &ctx).unwrap(), 3);
host.resource_close(handle, &ctx).unwrap();
assert_eq!(
host.resource_close(handle, &ctx).unwrap_err().code(),
"resource.closed"
);
drop(dir);
}
#[test]
fn worker_provider_registration_requires_a_grant_and_project_mode() {
let (_dir, script) = helper();
let project = tempfile::tempdir().unwrap();
let ctx = context();
let mut denied = StdHost::new(
SandboxMode::Project,
Capabilities::project(),
project.path(),
);
denied
.register_worker_resource_provider(
"worker",
&["worker.sensor"],
&std::path::PathBuf::from("python3"),
&[script.to_string_lossy().to_string()],
)
.unwrap();
let error = denied
.resource_open("worker.sensor", "s", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
for mode in [SandboxMode::Pure, SandboxMode::Strict] {
let mut host = StdHost::new(
mode,
Capabilities::project().allow_resource_provider("worker"),
project.path(),
);
host.register_worker_resource_provider(
"worker",
&["worker.sensor"],
&std::path::PathBuf::from("python3"),
&[script.to_string_lossy().to_string()],
)
.unwrap();
let error = host
.resource_open("worker.sensor", "s", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
}
#[test]
fn missing_worker_program_is_a_structured_error() {
let project = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_resource_provider("worker"),
project.path(),
);
let error = host
.register_worker_resource_provider(
"worker",
&["worker.sensor"],
&std::path::PathBuf::from("/definitely/missing/marqdo-provider"),
&[],
)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::KernelUnavailable);
}