The same verbs over HTTP #
What is new: the hosted API is not a different product with its own rules. RemoteWorkspace has the SDK's method names, the service answers with the same results and the same refusal codes, and the same bytes have the same root wherever they are stored. Tenants are isolated by API key; raw keys are never stored, only their sha256.
Why it matters: an agent can move from a laptop to the hosted service without changing what anything means. A root computed locally proves an object fetched remotely, and the other way round.
This page drives the real service app in-process (no network): FastAPI's test client is handed to RemoteWorkspace as its transport. Against a deployed service you pass only the base URL and your key.
A service with two tenants #
import hashlib, json
from pathlib import Path
from fastapi.testclient import TestClient
from tensorchain.client import RemoteWorkspace, ClientRefused
from tensorchain.service.app import create_app
from tensorchain.service.config import Settings
from tensorchain.service.tenancy import key_digest
from tensorchain.workspace import Workspace
Path("keys.json").write_text(json.dumps({key_digest("key-acme"): "acme", key_digest("key-globex"): "globex"}))
app = create_app(Settings(data_dir=Path("service-data"), keys_file=Path("keys.json"), dev_key=None,
fork_min_disk=0, fork_min_ram=0))
http = TestClient(app)
def send(method, url, data, headers, timeout): # the transport: in-process here, urllib by default
r = http.request(method, url.replace("http://testserver", ""), content=data, headers=headers)
return r.status_code, r.content
acme = RemoteWorkspace.create("http://testserver", api_key="key-acme", workspace="research", send=send)
print("acme's workspaces:", RemoteWorkspace.list("http://testserver", api_key="key-acme", send=send))
stored = json.loads(Path("keys.json").read_text())
assert "key-acme" not in json.dumps(stored) # only digests of keys are kept
assert key_digest("key-acme") in stored
acme's workspaces: ['research']
The same work, the same answers #
h = lambda text: hashlib.sha256(text.encode()).hexdigest()
finding = b"Finding: p99 latency regressed 18% after the 3.2 upgrade."
remote_root = acme.put(finding)
acme.tag("findings/latency", remote_root)
plan = acme.log_step(agent="lead", cause=h("investigate latency"), cause_kind="prompt", kind="plan")
step = acme.log_step(agent="analyst", cause=h("measure p99"), cause_kind="plan-step", kind="change",
parent=plan, output=remote_root, output_kind="object")
acme.evaluate(step)
remote_rewards = sorted((r["side"], r["outcome"]) for r in acme.rewards())
with Workspace("local.sqlite", actor="agent:laptop") as local:
local_root = local.put(finding)
print("remote root == local root:", remote_root == local_root)
print("remote bytes:", acme.get(remote_root).decode())
print("rewards over HTTP:", remote_rewards)
print("remote verify:", acme.verify()["ok"])
assert remote_root == local_root == hashlib.sha256(finding).hexdigest()
assert acme.get(remote_root) == finding
assert ("child", "success") in remote_rewards and acme.verify()["ok"] is True
remote root == local root: True
remote bytes: Finding: p99 latency regressed 18% after the 3.2 upgrade.
rewards over HTTP: [('child', 'success'), ('parent', 'success')]
remote verify: TrueIsolation and refusals #
globex = RemoteWorkspace("http://testserver", api_key="key-globex", workspace="research", send=send)
try:
globex.get(remote_root); crossed = "served (wrong)"
except ClientRefused as e:
crossed = f"{e.code} (HTTP {e.status})"
print("globex reads acme's workspace:", crossed)
intruder = RemoteWorkspace("http://testserver", api_key="not-a-key", workspace="research", send=send)
try:
intruder.tags(); denied = "served (wrong)"
except ClientRefused as e:
denied = f"{e.code} (HTTP {e.status})"
print("an unknown key:", denied)
try:
acme.get("0" * 64); missing = "served (wrong)"
except ClientRefused as e:
missing = e.code
print("an object that does not exist:", missing)
assert crossed != "served (wrong)" and denied.startswith("auth")
assert missing == "not_found"
assert acme.tags() == {"findings/latency": remote_root}
globex reads acme's workspace: not_found (HTTP 404) an unknown key: auth (HTTP 401) an object that does not exist: not_found
Globex's key reaches only Globex's tenant: the same workspace name there is a different, empty workspace, so Acme's object is simply not found. A request without a valid key is refused before anything is opened.