Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306"""Worktrees and worktree-backed agents through the real daemon and kernel.
Catches a child whose kernel starts outside its worktree, a worktree handle thatloses its session or paths, a worktree deleted (or a work item lost) when thechild closes or the worktree goes, and `worktree=` accepting a bare string.Only a real repository, a real daemon and the kernel's `worktrees` and`agents` bindings together show these."""
import jsonimport osimport shutilimport subprocessimport textwrapimport unittest
from harness import Albedo, Provider, operation_id, prompt_text, python, text
def run_git(cwd, *args): subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True)
def system_text(request): messages = request.get("messages", []) return "\n".join( json.dumps(item.get("content", "")) for item in messages if item.get("role") in ("system", "developer") ) + json.dumps(request.get("instructions", ""))
class WorktreesTests(unittest.TestCase): def setUp(self): self.provider = Provider(lambda request: text("ok")) self.addCleanup(self.provider.close) self.app = Albedo(self.provider) self.app.__enter__() self.addCleanup(self.app.__exit__, None, None, None) self.repo = str(self.app.workspace) run_git(self.repo, "init", "-q", "-b", "main") run_git(self.repo, "config", "user.email", "t@example.com") run_git(self.repo, "config", "user.name", "t") run_git(self.repo, "config", "commit.gpgsign", "false") with open(os.path.join(self.repo, "README"), "w") as readme: readme.write("hello\n") run_git(self.repo, "add", "README") run_git(self.repo, "commit", "-q", "-m", "init") self.parent = self.app.session() self.addCleanup(self.drop_worktrees)
def drop_worktrees(self): """Delete what a failed test left under the shared daemon's worktree dir.""" base = os.path.realpath(self.app.home / "wt") listing = subprocess.run( ["git", "worktree", "list", "--porcelain"], cwd=self.repo, capture_output=True, text=True, ).stdout for line in listing.splitlines(): path = os.path.realpath(line.removeprefix("worktree ")) if line.startswith("worktree ") and path.startswith(base + os.sep): shutil.rmtree(path, ignore_errors=True)
def turn(self, session, source): """Run `source` as one python cell of `session` and return its output.""" marker = "worktrees fixture cell " + operation_id() answer = self.provider.script
def dispatch(request): messages = request.get("messages", request.get("input", [])) if messages[-1].get("role") == "user" and prompt_text(request) == marker: return python(source) return answer(request)
self.provider.script = dispatch try: self.app.prompt(session, marker) self.app.idle(session) finally: self.provider.script = answer return self.last_output(session)
def last_output(self, session): result = [ entry for entry in self.app.history(session)["items"] if entry["kind"] == "tool_result" ][-1] execution = next( json.loads(part["value"]) for part in result["content"] if part["kind"] == "json" and part["field"] == "result" ) self.assertEqual(execution["status"], "ok", execution) return execution["output"]
def cell(self, source): model = f"{self.app.profile}/fixture-model" prelude = f"MODEL = {model!r}\nawait agents.models()\n" return json.loads(self.turn(self.parent, prelude + source))
def test_create_get_list_query_and_remove(self): out = self.cell( textwrap.dedent(""" import json, os from dataclasses import asdict wt = await worktrees.create('feat-one') rec = await worktrees.query('feat-one', repo=wt.repo) ids = lambda found: [w.id for w in found] out = dict(id=wt.id, name=wt.name, path=wt.path, kind=wt.kind, count=rec.count) out["got"] = (await worktrees.get(wt.id)).id out["listed"] = ids(await worktrees.list()) out["by_query"] = ids(await worktrees.list('feat-one')) out["none"] = len(await worktrees.list('no-such-thing-zzz')) out["by_kind"] = ids(await worktrees.list(kind=wt.kind)) out["by_other_kind"] = len(await worktrees.list(kind='darcs')) out["isdir"] = os.path.isdir(wt.path) await wt.remove() out["gone"] = os.path.exists(wt.path) out["after"] = len(await worktrees.list('feat-one')) print(json.dumps(out)) """) ) self.assertEqual(out["name"], "feat-one") self.assertEqual(out["got"], out["id"]) self.assertIn(out["id"], out["listed"]) self.assertEqual(out["by_query"], [out["id"]]) self.assertEqual(out["none"], 0) self.assertIn(out["id"], out["by_kind"]) self.assertEqual(out["by_other_kind"], 0) self.assertEqual(out["count"], 1) self.assertTrue(out["isdir"]) self.assertIn("/wt/", out["path"]) self.assertFalse(out["gone"]) self.assertEqual(out["after"], 0)
def test_spawn_rejects_a_string_worktree(self): out = self.cell( "import json\n" "seen = {}\n" "try:\n" " await agents.self.spawn('t', name='bad', model=MODEL, worktree='bad')\n" "except TypeError as failure:\n" " seen['spawn'] = str(failure)\n" "try:\n" " await agents.start('t', 'bad', MODEL, worktree='bad')\n" "except TypeError as failure:\n" " seen['start'] = str(failure)\n" "print(json.dumps(seen))\n" ) self.assertIn("worktree must be a Worktree or bool", out["spawn"]) self.assertIn("worktree must be a Worktree or bool", out["start"])
def child_probe_script(self, marker, source): answer = self.provider.script self.child_systems = []
def dispatch(request): messages = request.get("messages", request.get("input", [])) if messages[-1].get("role") == "user" and marker in prompt_text(request): self.child_systems.append(system_text(request)) return python(source) return answer(request)
self.provider.script = dispatch self.addCleanup(setattr, self.provider, "script", answer)
def spawn_in_worktree(self, argument, probe): marker = "worktree child probe " + operation_id() self.child_probe_script(marker, probe) out = self.cell( "import json\nfrom dataclasses import asdict\n" "wt = await worktrees.create('kid-tree')\n" f"kid = await agents.self.spawn({marker!r}, name='kid', model=MODEL, worktree={argument})\n" "print(json.dumps(dict(kid=kid.id, wt=wt.id, wt_path=wt.path,\n" " wt_workspace=wt.workspace, repo=wt.repo,\n" " h=asdict(kid.worktree) if kid.worktree else None)))\n" ) self.app.idle(out["kid"], timeout=60) return out
def test_a_child_runs_in_its_worktree_and_shares_work_and_memory(self): probe = ( "import json, os\n" "item = await work.create('filed in the worktree')\n" "await memory.append('worktree-memory-note')\n" "origin = str(memory.read())\n" "print(json.dumps(dict(cwd=os.getcwd(), work=item.id,\n" " origin='parent-origin-note' in origin)))\n" "await mail.submit('parent', 'done')\n" ) self.cell("await memory.append('parent-origin-note')\nprint('{}')\n") out = self.spawn_in_worktree("wt", probe) h = out["h"] self.assertEqual(h["id"], out["wt"]) self.assertEqual(h["session"], out["kid"]) self.assertEqual(h["path"], out["wt_path"]) self.assertEqual(h["workspace"], out["wt_workspace"]) self.assertEqual(h["repo"], out["repo"]) self.assertTrue(h["origin"] and h["kind"] and h["name"] == "kid-tree") seen = json.loads(self.last_output(out["kid"])) self.assertEqual( os.path.realpath(seen["cwd"]), os.path.realpath(out["wt_workspace"]) ) self.assertTrue(seen["origin"]) self.assertTrue(any(out["wt"] in text_ for text_ in self.child_systems))
shared = self.cell( "import json\n" "items = await work.list()\n" "print(json.dumps(dict(titles=[i.title for i in items],\n" " memory='worktree-memory-note' in str(memory.read()))))\n" ) self.assertIn("filed in the worktree", shared["titles"]) self.assertTrue(shared["memory"])
after = self.cell( "import json\n" "await work.create('filed in the main checkout')\n" "before = {i.title: i.workspace for i in await work.list()}\n" f"await worktrees.remove({out['wt']!r}, force=True)\n" "after = {i.title: i.workspace for i in await work.list()}\n" "memory_after = 'worktree-memory-note' in str(memory.read())\n" "origin_after = 'parent-origin-note' in str(memory.read())\n" "print(json.dumps(dict(before=before, after=after,\n" " memory=memory_after, origin=origin_after)))\n" ) self.assertTrue(after["memory"]) self.assertTrue(after["origin"]) title = "filed in the worktree" self.assertEqual( os.path.realpath(after["before"][title]), os.path.realpath(out["wt_workspace"]), ) self.assertEqual( os.path.realpath(after["after"][title]), os.path.realpath(self.repo) ) self.assertIn("filed in the main checkout", after["after"])
def test_spawn_true_makes_a_worktree_and_close_keeps_it(self): marker = "worktree child probe " + operation_id() self.child_probe_script(marker, "await mail.submit('parent', 'done')") out = self.cell( "import json, os\n" f"kid = await agents.self.spawn({marker!r}, name='auto', model=MODEL, worktree=True)\n" "wt = kid.worktree\n" "owned = await worktrees.list(session=kid)\n" "print(json.dumps(dict(kid=kid.id, id=wt.id, path=wt.path,\n" " owned=[w.id for w in owned], session=wt.session)))\n" ) self.assertEqual(out["owned"], [out["id"]]) self.assertEqual(out["session"], out["kid"]) self.app.idle(out["kid"], timeout=60) closed = self.turn( self.parent, "import os\n" f"kid = await agents.get({out['kid']!r})\n" "await kid.close()\n" f"print(os.path.isdir({out['path']!r}))\n", ) self.assertIn("will not be deleted automatically", closed) self.assertIn("True", closed) peer_marker = "worktree peer probe " + operation_id() self.child_probe_script(peer_marker, "await mail.submit('parent', 'done')") failed = self.cell( "import json\n" "try:\n" " await agents.self.spawn('t', name='fail-kid', model=MODEL, cyber_access='daybreak_blue', worktree=True)\n" " raised = False\n" "except Exception:\n" " raised = True\n" "print(json.dumps(dict(raised=raised,\n" " leaked=[w.id for w in await worktrees.list() if 'fail-kid' in w.id])))\n" ) self.assertTrue(failed["raised"]) self.assertEqual(failed["leaked"], []) peer = self.cell( "import json\n" f"peer = await agents.start({peer_marker!r}, 'My Peer.1', MODEL, worktree=True)\n" "print(json.dumps(dict(id=peer.worktree.id, path=peer.worktree.path,\n" " cwd=peer.cwd, session=peer.worktree.session, peer=peer.id)))\n" ) self.app._sessions.add(peer["peer"]) self.app.idle(peer["peer"], timeout=60) self.assertEqual(peer["session"], peer["peer"]) self.assertEqual(os.path.realpath(peer["cwd"]), os.path.realpath(peer["path"])) removed = self.cell( "import json, os\n" f"wt = await worktrees.get({out['id']!r})\n" "await worktrees.remove(wt)\n" "ids = [w.id for w in await worktrees.list()]\n" f"print(json.dumps(dict(gone=not os.path.exists({out['path']!r}),\n" f" listed={out['id']!r} in ids)))\n" ) self.assertTrue(removed["gone"]) self.assertFalse(removed["listed"]) self.cell( f"await worktrees.remove({peer['id']!r}, force=True)\nprint('{{}}')\n" )
if __name__ == "__main__": unittest.main()