Exact runs #
What is new: a run is not "done" because a program exited. It completes only when its output equals an expectation you supplied up front, computed independently. Requests have identities, so retries are safe.
Why it matters: an agent or a pipeline can retry as often as it likes without double-writing, a changed request can never masquerade as an old one, and a wrong result moves nothing. "It ran" and "it is right" stop being the same claim.
A source and its expected state #
Any object with heads() and export() is a source. The expectation is computed here, by our own code, from the published digest definition: not by the engine that will run the import.
import hashlib
from tensorchain.workspace import Workspace, Refused
class Notes:
"""A tiny source: three notes, two named heads."""
def __init__(self):
self.objects = {hashlib.sha256(t).hexdigest(): t for t in (b"note: plan", b"note: data", b"note: spare")}
by = {v: k for k, v in self.objects.items()}
self.h = {"notes/plan": by[b"note: plan"], "notes/data": by[b"note: data"]}
def heads(self):
return dict(self.h)
def export(self, after, limit, max_bytes):
roots = [r for r in sorted(self.objects) if r > after][:limit]
more = bool(roots) and any(r > roots[-1] for r in self.objects)
return {"objects": [{"root": r, "raw": self.objects[r], "deps": []} for r in roots],
"next": roots[-1] if more else None}
def expected(self):
h = hashlib.sha256(b"tc-sql.objects.v1\x00")
for r in sorted(set(self.h.values())):
h.update(("%s %s\n" % (r, hashlib.sha256(self.objects[r]).hexdigest())).encode())
g = hashlib.sha256(b"tc-sql.heads.v1\x00")
for name, r in sorted(self.h.items()):
g.update(("%s %s\n" % (name, r)).encode())
return hashlib.sha256(("tc-sql.layer0.v1\x00%s %s" % (h.hexdigest(), g.hexdigest())).encode()).hexdigest()
src = Notes()
print("expected state digest:", src.expected())
assert len(src.expected()) == 64
expected state digest: a76635bcc8271fce9ad9876d8e2d7e058f02ed2ea45ac51da2664c753e82434d
Run it, retry it, change it #
ws = Workspace("runs.sqlite", actor="agent:importer")
d = ws.describe()
frozen = ws.freeze_source(src)
request = {"request_id": "import-notes", "base": d["base"]["id"], "program": d["programs"]["mirror-v1"],
"input": {"format": frozen["format"], "sha256": frozen["sha256"]},
"limits": {"page_objects": 2, "page_bytes": 4096},
"verification": {"kind": "supplied-state-digest", "expected": src.expected()},
"expected_revision": 0}
first = ws.submit(request, src)
print("status: ", first["outcome"]["status"])
print("state now equals:", ws.digest()["layer0_digest"] == src.expected())
retry = ws.submit(request, src) # the same request again
print("identical retry returns the stored outcome:", retry == first)
try:
ws.submit(dict(request, limits={"page_objects": 3, "page_bytes": 4096}), src)
changed = "accepted"
except Refused as e:
changed = e.code
print("same id, changed field:", changed)
assert first["outcome"]["status"] == "completed"
assert ws.digest()["layer0_digest"] == src.expected()
assert retry == first
assert changed == "identity_reused"
status: completed state now equals: True identical retry returns the stored outcome: True same id, changed field: identity_reused
A wrong expectation moves nothing #
before = (ws.revision(), ws.digest()["layer0_digest"])
wrong = dict(request, request_id="import-notes-wrong", expected_revision=ws.revision(),
verification={"kind": "supplied-state-digest", "expected": "0" * 64})
try:
out = ws.submit(wrong, src)
status = out["outcome"]["status"]
except Refused as e:
status = "refused " + e.code
after = (ws.revision(), ws.digest()["layer0_digest"])
print("wrong expectation:", status)
print("state unchanged: ", before == after)
try:
ws.submit(dict(request, request_id="import-stale", expected_revision=0), src)
stale = "accepted"
except Refused as e:
stale = e.code
print("stale writer: ", stale)
ws.close()
assert status != "completed"
assert before == after
assert stale == "stale_revision"
wrong expectation: failed state unchanged: True stale writer: stale_revision
The wrong expectation did not complete, and the workspace's revision and digest are exactly what they were. A writer holding an old revision is refused rather than overwriting newer work.