Tamper and repair #

What is new: damage is detected and healed. verify() re-hashes every object and checks the hash-linked audit chain; repair() quarantines what is bad, refetches it from a peer (a parent or a sibling), and reports the audit chain instead of rewriting it.

Why it matters: silent corruption (a disk fault, a bad sync, a malicious edit) becomes a detected, repaired and recorded event. Nothing is deleted: the bad bytes go to quarantine, so you can still see exactly what was wrong.

A workspace and a fork of it #

import hashlib, sqlite3
from pathlib import Path
from tensorchain.workspace import Workspace, Registry, Caps, Refused

caps, registry = Caps(min_disk=0, min_ram=0), Registry(Path("registry.sqlite"))
with Workspace("origin.sqlite", actor="agent:keeper") as origin:
    for program in ("clone-v1", "heal-v1"):
        origin.set_capability(program, "Advisory", approved_by="owner:cookbook")
    contract = origin.put(b"Invoice 2041: 3 seats, 12 months, net 30.")
    origin.tag("contracts/2041", contract)
    origin.fork("replica.sqlite", "replica-1", caps=caps, registry=registry)

with Workspace("replica.sqlite") as replica:
    print("replica verifies:", replica.verify()["ok"])
    assert replica.verify()["ok"] is True
Output (captured at build)
replica verifies: True

Corrupt the replica #

db = sqlite3.connect("replica.sqlite")
db.execute("UPDATE tc_object SET raw=? WHERE root=?", (b"Invoice 2041: 30 seats, 12 months, net 30.", contract))
db.commit(); db.close()

with Workspace("replica.sqlite") as replica:
    report = replica.verify()
    try:
        replica.get(contract); read = "served (wrong)"
    except Refused as e:
        read = "refused " + e.code
print("replica verifies:", report["ok"])
print("reading the contract:", read)
assert report["ok"] is False
assert read == "refused corrupt"
Output (captured at build)
replica verifies: False
reading the contract: refused corrupt

Repair it from its origin #

with Workspace("replica.sqlite", actor="agent:keeper") as replica, Workspace("origin.sqlite") as origin:
    fix = replica.repair(origin)
    print("quarantined:", [(root == contract, why) for root, why in fix["objects_quarantined"]])
    print("refetched:  ", fix["objects_refetched"])
    print("audit chain reported, not rewritten:", fix["audit"]["ok"])
    healed = replica.verify()["ok"]
    restored = replica.get(contract)
print("replica verifies again:", healed)
print("contract:", restored.decode())

assert fix["objects_quarantined"] == [[contract, "root_mismatch"]]
assert fix["objects_refetched"] == 1 and fix["audit"]["ok"] is True
assert healed is True and restored == b"Invoice 2041: 3 seats, 12 months, net 30."
Output (captured at build)
quarantined: [(True, 'root_mismatch')]
refetched:   1
audit chain reported, not rewritten: True
replica verifies again: True
contract: Invoice 2041: 3 seats, 12 months, net 30.

The altered object was caught by its own name (root_mismatch), moved to quarantine with its bytes, and refetched from the origin. The replica verifies again and serves the original contract.