Fork, try both, bring one home #

What is new: a decision becomes a branch: one verified child workspace per option, each a full workspace of its own. Agents work in each in parallel. You merge the winner home; the loser is not thrown away, it stays as the road not taken, with everything it did.

Why it matters: agents can explore alternatives without touching the main line, and a decision can be revisited later: the option you did not choose is still there, intact, with its reasoning. Merging re-judges the child's work from the parent's own evidence, so a child cannot smuggle in claims.

Branch a decision #

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

h = lambda text: hashlib.sha256(text.encode()).hexdigest()
caps = Caps(min_disk=0, min_ram=0)            # this example is tiny; real use keeps the owner's caps
registry = Registry(Path("registry.sqlite"))   # who forked what, kept outside every workspace

with Workspace("main.sqlite", actor="agent:lead") as main:
    for program in ("clone-v1", "pushback-v1"):          # forking and merging are capabilities too
        main.set_capability(program, "Advisory", approved_by="owner:cookbook")
    decision = main.log_step(agent="lead", cause=h("which cache policy?"), cause_kind="prompt", kind="plan")
    fork = main.branch(decision, ["lru", "ttl"], "lead", ["lru.sqlite", "ttl.sqlite"],
                       caps=caps, registry=registry)
    steps = {b["option"]: b["entry"] for b in fork["branches"]}
    print("branches:", [(b["option"], b["status"], "depth", b["depth"]) for b in fork["branches"]])
    print("each child starts equal to its parent:", all(b["layer0_digest"] == b["expected"] for b in fork["branches"]))

assert [b["option"] for b in fork["branches"]] == ["lru", "ttl"]
assert all(b["status"] == "completed" and b["depth"] == 1 for b in fork["branches"])
assert all(b["layer0_digest"] == b["expected"] for b in fork["branches"])
Output (captured at build)
branches: [('lru', 'completed', 'depth', 1), ('ttl', 'completed', 'depth', 1)]
each child starts equal to its parent: True

Work in both, in parallel #

for option, path in (("lru", "lru.sqlite"), ("ttl", "ttl.sqlite")):
    with Workspace(path, actor="agent:" + option) as child:
        design = child.put(f"design: {option} cache, benchmark p99 = {'41' if option == 'lru' else '67'} ms".encode())
        child.tag("design", design)
        child.log_step(agent=option, cause=h(f"try {option}"), cause_kind="plan-step", kind="change",
                       parent=steps[option], output=design, output_kind="object")

with Workspace("main.sqlite") as main:
    print("main line untouched while the children worked:", main.tags() == {} and main.merged_tags() == {})
    assert main.merged_tags() == {}
Output (captured at build)
main line untouched while the children worked: True

Bring one home, keep the other #

with Workspace("main.sqlite", actor="agent:lead") as main:
    merged = main.merge("lru.sqlite")
    home = {name.split("@")[0]: root for name, root in main.merged_tags().items()}
    print("merged:", {k: merged[k] for k in ("objects_added", "lineage_added", "heads_set")})
    print("now at home:", sorted(home))
    home_design = main.get(home["design"]).decode()
    print("home design:", home_design)
    rewarded = sorted((r["side"], r["beneficiary"]) for r in main.rewards() if r["outcome"] == "success")
    print("rewarded:", rewarded)

with Workspace("ttl.sqlite") as road_not_taken:
    kept = road_not_taken.get(road_not_taken.tags()["design"]).decode()
    print("the road not taken, intact:", kept)

assert merged["objects_added"] == 1 and merged["lineage_added"] >= 1
assert sorted(home) == ["design"] and "lru" in home_design
assert "ttl" in kept and "lru" not in kept
assert ("child", "lru") in rewarded
Output (captured at build)
merged: {'objects_added': 1, 'lineage_added': 2, 'heads_set': 1}
now at home: ['design']
home design: design: lru cache, benchmark p99 = 41 ms
rewarded: [('child', 'lru'), ('parent', 'lead/lru')]
the road not taken, intact: design: ttl cache, benchmark p99 = 67 ms

The lru branch merged home: its design object, its tag (named design@<child> at home) and its steps, which the main workspace re-judged from its own evidence. The ttl branch was never merged and never deleted: it is still a complete workspace you can open, inspect, or merge later.