A workspace in thirty lines #

A workspace is one file. Open it, store something, record the step that produced it, judge that step from evidence, and verify the whole workspace. This page runs as written when the site is built.

import hashlib
from tensorchain.workspace import Workspace

with Workspace("quickstart.sqlite", actor="agent:me") as ws:
    # 1. Store bytes: the name of an object is the sha256 of its content.
    answer = ws.put(b"the answer is 42")
    ws.tag("answer", answer)

    # 2. Record the work: an instruction (the plan) and the step that produced the answer.
    plan = ws.log_step(agent="me", cause=hashlib.sha256(b"find the answer").hexdigest(),
                       cause_kind="prompt", kind="plan")
    step = ws.log_step(agent="me", cause=hashlib.sha256(b"compute it").hexdigest(),
                       cause_kind="plan-step", kind="change", parent=plan,
                       output=answer, output_kind="object")

    # 3. Judge the step from exact evidence (the stored, verified object), not from what the agent says.
    ws.evaluate(step)
    outcomes = sorted((r["side"], r["outcome"]) for r in ws.rewards())

    # 4. Verify everything: every object re-hashes, the audit chain links, the lineage holds.
    report = ws.verify()

print("answer root:", answer)
print("rewards:", outcomes)
print("verified:", report["ok"])
assert answer == hashlib.sha256(b"the answer is 42").hexdigest()
assert ("child", "success") in outcomes and ("parent", "success") in outcomes
assert report["ok"] is True
Output (captured at build)
answer root: ff29438fb7a23c7eb348c56013db4df7f44bf5b081c3430c76913ebcacba6b70
rewards: [('child', 'success'), ('parent', 'success')]
verified: True

The step earned a reward for the agent that did it (child) and for the plan it came from (parent), because the evidence (an object that re-hashes) was there. Next: Workspace concepts, then the other cookbooks.