Files
marqdo-extension-conformance/kernel/mocklang
T
2026-11-21 23:08:00 +08:00

51 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Unknown-language foreign kernel for the Marqdo foreign JSON wire (v1).
The Marqdo host writes the inert code resource to a temporary file and passes
its path as argv[1]; stdin receives exactly one JSON envelope:
{"wire_version": 1, "kind": "request", "input": <typed value or null>}
For foreign.call, stdout must contain exactly one type-tagged wire value
(spec/value-wire.md). This provider is intentionally not a built-in language:
`mocklang` is unknown to the Marqdo CLI and is registered only through
kernel.json at run time.
"""
import hashlib
import json
import sys
def main() -> int:
source_path = sys.argv[1] if len(sys.argv) > 1 else ""
try:
envelope = json.load(sys.stdin)
except json.JSONDecodeError as error: # pragma: no cover - protocol failure
print(f"mocklang: invalid request envelope: {error}", file=sys.stderr)
return 1
if envelope.get("wire_version") != 1 or envelope.get("kind") != "request":
print("mocklang: expected wire_version 1 request envelope", file=sys.stderr)
return 1
try:
with open(source_path, "rb") as source_file:
source = source_file.read()
except OSError as error: # pragma: no cover - protocol failure
print(f"mocklang: cannot read code resource: {error}", file=sys.stderr)
return 1
input_value = envelope.get("input")
result = {
"type": "map",
"entries": {
"engine": {"type": "text", "value": "mocklang"},
"answer": {"type": "int", "decimal": "42"},
"source_digest": {"type": "text", "value": hashlib.sha256(source).hexdigest()},
"input_kind": {"type": "text", "value": "null" if input_value is None else "typed"},
},
}
sys.stdout.write(json.dumps(result, ensure_ascii=False))
return 0
if __name__ == "__main__":
sys.exit(main())