Proven bytes #

What is new: an object's name is its content. You do not trust that a stored value is what was written; every read proves it, and one command proves the whole workspace.

Why it matters: an agent's memory, a tool's output and a customer's file can all be checked at any time, by anyone, without trusting the process that wrote them. A single flipped byte is caught, not silently served.

Store, name and read back #

import hashlib, sqlite3
from tensorchain.workspace import Workspace, Refused

note = b"Release moved to Thursday. Owner: Dana."
with Workspace("team.sqlite", actor="agent:scribe") as ws:
    root = ws.put(note)
    ws.tag("notes/release", root)
    again = ws.put(note)                       # the same bytes are the same object, stored once
    print("root:      ", root)
    print("idempotent:", again == root)
    print("tags:      ", ws.tags())
    assert root == hashlib.sha256(note).hexdigest()
    assert again == root
    assert ws.get(root) == note
    assert ws.verify()["ok"] is True
Output (captured at build)
root:       943605059a833d0826002d9118e9a89b427dfa1b810c728a58f595d340fc0ec3
idempotent: True
tags:       {'notes/release': '943605059a833d0826002d9118e9a89b427dfa1b810c728a58f595d340fc0ec3'}

The root is just sha256(bytes), so anyone holding the bytes can recompute it. Storing the same bytes twice returns the same root.

A tampered copy is caught #

Take a copy of the workspace file and change one stored byte behind TensorChain's back, the way a disk fault, a bad sync or an attacker would.

db = sqlite3.connect("team.sqlite")
db.execute("VACUUM INTO 'tampered.sqlite'")
db.close()
db = sqlite3.connect("tampered.sqlite")
db.execute("UPDATE tc_object SET raw=? WHERE root=?", (b"Release moved to Friday. Owner: Dana.", root))
db.commit(); db.close()

with Workspace("tampered.sqlite") as bad:
    try:
        bad.get(root)
        print("read: returned the bytes (wrong)")
        refused = None
    except Refused as e:
        refused = e.code
        print("read refused:", e.code)
    report = bad.verify()
    print("verify:", report["ok"])

assert refused == "corrupt"
assert report["ok"] is False
Output (captured at build)
read refused: corrupt
verify: False

The read is refused instead of returning the altered note, and verify() fails for the copy while the original still verifies. Tamper and repair shows how the copy is healed from a peer.