Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(agent-core): start the job wrapper and watch supervisor as the service imported them (#2529)
Call White7e2d2afa3a4d
diff --git a/core/agent_core/tools/jobs.py b/core/agent_core/tools/jobs.pyindex 5f97e78ef..18f1da785 100644--- a/core/agent_core/tools/jobs.py+++ b/core/agent_core/tools/jobs.py@@ -4,6 +4,10 @@ Every command runs under ``job_wrapper.py`` in its own session: stdin closed,stdout and stderr through a pipe into a bounded log the wrapper keeps. Nothingin the host holds a pipe to the job, so any ``LocalJobHost`` over the samedirectory, in this process or after a restart, can read, wait on, or kill it.+The host starts the wrapper as this module imported it, from a copy under+``<jobs_dir>/wrapper/`` (``core.frozen_script``): an upgrade that replaces the+installed files before the service restarts must not put a newer wrapper, whose+argv may differ, under a running host.Job directory: ``meta.json``, ``pid``, ``decision``, ``output.log`` (the head),``tail.log`` (the rolling tail, once the head is full), ``exit``, ``stopped``@@ -64,6 +68,7 @@ from core.agent_core.tools import windows_files, windows_jobfrom core.agent_core.tools.base import CallInstance, JobStatusfrom core.agent_core.tools.paths import os_reason, run_to_endfrom core.agent_core.tools.shell import Shell, default_shell+from core.frozen_script import FrozenScriptfrom core.process_isolation import (DEFAULT_PROCESS_TERMINATE_TIMEOUT_SECONDS,KILL_SIGNAL,@@ -103,7 +108,13 @@ STOP_KILLED = "killed"STOP_WRAPPER_ERROR = "wrapper_error"_WINDOWS = os.name == "nt"-_WRAPPER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "job_wrapper.py")+#: The wrapper, with the helpers it loads beside itself on Windows, as this module was imported.+_WRAPPER = FrozenScript(+ *(+ os.path.join(os.path.dirname(os.path.abspath(__file__)), name)+ for name in ("job_wrapper.py", "windows_job.py", "windows_files.py")+ )+)#: How long past the deadline the host leaves a live wrapper to decide (its poll interval, its#: 0.5 s drain, and margin) before stopping the job itself._WRAPPER_DECIDES_S = 1.0@@ -379,6 +390,10 @@ class LocalJobHost:raise JobStartError(f"Could not start the command: its job record would be over {_META_CHARS} characters.") from None+ try:+ wrapper = _WRAPPER.path(os.path.join(self._jobs_dir, "wrapper"))+ except OSError as exc:+ raise JobStartError(f"Could not start the command: {os_reason(exc)}.") from excos.makedirs(self._jobs_dir, exist_ok=True)os.mkdir(job_dir, 0o700)self._write_meta(job_id, meta)@@ -394,8 +409,8 @@ class LocalJobHost:sys.executable,"-I","-S",- "-B", # on Windows the wrapper loads its helpers by path: no bytecode written into the install- _WRAPPER,+ "-B", # on Windows the wrapper loads its helpers by path: no bytecode written beside them+ wrapper,"avibe-job",job_dir,repr(DECISION_TIMEOUT_S),diff --git a/core/agent_core/tools/windows_job.py b/core/agent_core/tools/windows_job.pyindex 51d170ff3..abe1df46d 100644--- a/core/agent_core/tools/windows_job.py+++ b/core/agent_core/tools/windows_job.py@@ -1,8 +1,8 @@"""Windows Job Objects, and the pipe check the job wrapper needs, through ``ctypes`` alone.Standard library only: the job wrapper beside it runs as ``python -I -S`` and loads this file by its-path, and ``core/watch_worker.py`` imports it. Importing it works everywhere; calling it works only-on Windows.+path, as ``core/watch_worker.py`` does; each runs from a copy with this file beside it+(``core.frozen_script``). Importing it works everywhere; calling it works only on Windows.A job object holds a process tree as a POSIX session does, and more strictly. A process started bya member is a member, and it cannot leave, since no job here allows breakaway. Withdiff --git a/core/command_runner.py b/core/command_runner.pyindex 558858dd6..045ed43fe 100644--- a/core/command_runner.py+++ b/core/command_runner.py@@ -5,6 +5,11 @@ One place owns the mechanics of running a user command through the stableand tree teardown. Callers own policy -- registries, localization, persistenceand result interpretation -- so this module stays a leaf with no knowledge ofwatches, scheduled tasks, storage or the service layer.++The supervisor starts as this module imported it, from a copy under the runtime+directory (``core.frozen_script``): an upgrade that replaces the installed files+before the service restarts must not put a newer supervisor, whose spec may+differ, under a running runner."""from __future__ import annotations@@ -17,10 +22,12 @@ import signalimport sysfrom collections import dequefrom dataclasses import dataclass-from pathlib import Pathfrom typing import Callable, Collection, Mapping, Optional+from config import paths as config_pathsfrom core import watch_worker+from core.agent_core.tools import windows_job+from core.frozen_script import FrozenScriptfrom core.process_isolation import (DEFAULT_PROCESS_TERMINATE_TIMEOUT_SECONDS,KILL_SIGNAL,@@ -49,6 +56,9 @@ STREAM_TRUNCATION_MARKER = b"[avibe: output truncated]\n"#: it in a failure notice, and wrap the row in ``vibe task list``.COMMAND_PREVIEW_MAX_CHARS = 120+#: The supervisor, with the helper it loads beside itself on Windows, as this module was imported.+_SUPERVISOR = FrozenScript(watch_worker.__file__, windows_job.__file__)+def command_line_preview(shell_command: Optional[str],@@ -73,6 +83,12 @@ def command_line_preview(return preview[: max_chars - 1].rstrip() + "…"+def supervisor_path() -> str:+ """The supervisor to start: this module's copy of it under the runtime directory."""++ return _SUPERVISOR.path(str(config_paths.get_runtime_dir() / "watch-worker"))++@dataclass(frozen=True)class SupervisedCommandResult:"""The outcome of one supervised command run.@@ -303,7 +319,7 @@ async def run_supervised_command(spawn_env[watch_worker.PROCESS_IDENTITY_ENV] = worker_markerprocess = await asyncio.create_subprocess_exec(os.path.abspath(sys.executable),- os.fspath(Path(watch_worker.__file__).resolve()),+ supervisor_path(),cwd=cwd,env=spawn_env,stdin=asyncio.subprocess.PIPE,diff --git a/core/frozen_script.py b/core/frozen_script.pynew file mode 100644index 000000000..01b8a87e3--- /dev/null+++ b/core/frozen_script.py@@ -0,0 +1,59 @@+"""A helper script that runs as the version its launcher imported.++A helper started by its installed path runs whatever that path holds when it starts. After an upgrade+replaces the installed files and before the service restarts, that is the newer version, whose protocol+with the running launcher may differ: when ``job_wrapper.py``'s argv changed, every command failed until+the restart. A ``FrozenScript`` reads the script, and the files it loads from its own directory, when+the launcher's module is imported; the launcher then starts a copy written from those bytes.+"""++from __future__ import annotations++import contextlib+import hashlib+import os+import secrets+++class FrozenScript:+ """``script``, with ``beside`` (the files it loads from its own directory), as they are now."""++ def __init__(self, script: str, *beside: str) -> None:+ self._name = os.path.basename(script)+ self._files: dict[str, bytes] = {}+ for path in (script, *beside):+ with open(path, "rb") as handle:+ self._files[os.path.basename(path)] = handle.read()+ digest = hashlib.sha256()+ for name, data in sorted(self._files.items()):+ digest.update(b"%s\0%d\0" % (name.encode(), len(data)) + data)+ self._digest = digest.hexdigest()[:16]++ def path(self, root: str) -> str:+ """The script in its copy under ``root``, written first where any of the copy is missing.++ The copy is named by its content (``<root>/<digest>/``): launchers of the same bytes share it, and an+ older and a newer launcher never start each other's. Each file is renamed into place whole, so no+ launcher starts one half-written; on Windows a rename never replaces, so a helper that is reading a+ file is never in the way of a second launcher.+ """+ directory = os.path.join(root, self._digest)+ os.makedirs(directory, mode=0o700, exist_ok=True)+ for name, data in self._files.items():+ target = os.path.join(directory, name)+ if os.path.exists(target):+ continue+ tmp = f"{target}.{secrets.token_hex(4)}.tmp"+ try:+ with open(tmp, "wb") as handle:+ handle.write(data)+ handle.flush()+ # Durable before it is named: an empty copy left by a power loss would never be rewritten.+ os.fsync(handle.fileno())+ os.rename(tmp, target) # POSIX replaces the same bytes; Windows raises FileExistsError+ except FileExistsError:+ pass+ finally:+ with contextlib.suppress(FileNotFoundError):+ os.unlink(tmp)+ return os.path.join(directory, self._name)diff --git a/core/watch_worker.py b/core/watch_worker.pyindex b528c9bce..9e9e0c398 100644--- a/core/watch_worker.py+++ b/core/watch_worker.py@@ -173,8 +173,19 @@ def _read_watch_worker_spec() -> tuple[list[str], str | None]:def _install_windows_kill_on_close_job() -> Any:if os.name != "nt":return None- from core.agent_core.tools import windows_job-+ path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "windows_job.py")+ if os.path.exists(path):+ import importlib.util++ # Beside this file, where the runner copies it with this one (``core.frozen_script``): the installed+ # package may already hold a newer version.+ spec = importlib.util.spec_from_file_location("avibe_windows_job", path)+ windows_job = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(windows_job)+ else:+ # Started from the installed package, as a runner released before the copies starts it while an+ # upgrade waits for its restart: the package's own version then matches this file.+ from core.agent_core.tools import windows_jobreturn windows_job.create_kill_on_close_job()diff --git a/docs/plans/agent-core-contracts/tools.md b/docs/plans/agent-core-contracts/tools.mdindex 823fea2ee..7babcfe8c 100644--- a/docs/plans/agent-core-contracts/tools.md+++ b/docs/plans/agent-core-contracts/tools.md@@ -311,6 +311,10 @@ at most once and recovery can always decide whether it did; its identity is recoabsolute deadline that survives handover and restarts; output on disk is bounded; files are kept until the call isdurably settled; handover creates at most one Watch per job.+The host starts the wrapper from a copy, under `<state>/agent_core/jobs/wrapper/`, of the wrapper and the files it+loads beside itself, as the host read them when it was imported. An upgrade that installs its files before the service+restarts therefore never puts a newer wrapper, whose argv may differ, under the running host.+On Windows the wrapper's tree is a job object it creates and joins before it can run the command, named after thejob and ended when the wrapper's handle to it closes. Every process the command starts is in it and cannot leave it.`kill` terminates the job object; a kill of the wrapper alone (`TerminateProcess`, which nothing can catch) also endsdiff --git a/docs/plans/process-identity.md b/docs/plans/process-identity.mdindex c9daefb7a..a52d726c2 100644--- a/docs/plans/process-identity.md+++ b/docs/plans/process-identity.md@@ -119,7 +119,7 @@ One rule decides identity, in this order:A boolean check cannot express the third answer, so each consumer stillusing rule 4 needs one. A command line is independent proof only when itnames this particular process. Every watch supervisor runs the same- `python core/watch_worker.py` argv, with its watch id only in the+ `python …/watch_worker.py` argv, with its watch id only in theenvironment, so for those the per-watch environment is the proof, andwithout it the answer is *unknown*.diff --git a/tests/agent_core/tools/test_jobs.py b/tests/agent_core/tools/test_jobs.pyindex a7916ca02..d7895bf77 100644--- a/tests/agent_core/tools/test_jobs.py+++ b/tests/agent_core/tools/test_jobs.py@@ -10,9 +10,12 @@ from __future__ import annotationsimport asyncioimport errno+import importlib.utilimport os+import shutilimport signalimport subprocess+import sysimport timefrom datetime import datetime, timezone@@ -131,6 +134,28 @@ async def test_a_crash_at_any_launch_step_runs_the_command_at_most_once(tmp_pathassert recovery.status(job_id).state == ("exited" if may_have_started else "gone")+async def test_a_host_runs_the_wrapper_it_imported_after_an_upgrade_replaces_the_installed_one(tmp_path, monkeypatch):+ """An upgrade installs its files before the service restarts. Until then the running host speaks its own+ wrapper's argv, so it must not start the newer wrapper that the install now holds."""+ tools = os.path.dirname(os.path.abspath(jobs_module.__file__))+ install = tmp_path / "install"+ install.mkdir()+ for name in ("jobs.py", "job_wrapper.py", "windows_job.py", "windows_files.py"):+ shutil.copyfile(os.path.join(tools, name), install / name)+ spec = importlib.util.spec_from_file_location("avibe_installed_jobs", install / "jobs.py")+ installed = importlib.util.module_from_spec(spec)+ monkeypatch.setitem(sys.modules, spec.name, installed)+ spec.loader.exec_module(installed) # the service imports the job host+ for name in ("job_wrapper.py", "windows_job.py", "windows_files.py"): # then the upgrade lands+ (install / name).write_text("import sys\nsys.exit('a newer version, whose argv this host does not speak')\n")++ host = installed.LocalJobHost(str(tmp_path / "jobs"))+ job_id = await _start(host, tmp_path, "echo ran")++ assert (await host.wait(job_id, deadline_s=10)).exit_code == 0+ assert host.output(job_id) == (b"ran\n", 4)++async def test_a_wrapper_left_without_a_decision_abandons_and_a_late_host_cannot_start_it(tmp_path, monkeypatch):monkeypatch.setattr(jobs_module, "DECISION_TIMEOUT_S", 0.05)original = LocalJobHost._await_piddiff --git a/tests/test_command_runner.py b/tests/test_command_runner.pyindex ae3d4c4d3..255f5eb8e 100644--- a/tests/test_command_runner.py+++ b/tests/test_command_runner.py@@ -3,12 +3,15 @@from __future__ import annotationsimport asyncio+import importlib.utilimport os+import shutilimport sysfrom pathlib import Pathimport pytest+from config import pathsfrom core import command_runner, watch_workerfrom core.command_runner import (STREAM_TRUNCATION_MARKER,@@ -386,10 +389,11 @@ async def test_spawn_uses_the_stable_supervisor_entrypoint(tmp_path: Path, monke)assert result.exit_code == 0- assert captured["args"] == (- os.path.abspath(sys.executable),- str(Path(watch_worker.__file__).resolve()),- )+ executable, supervisor = captured["args"]+ assert executable == os.path.abspath(sys.executable)+ # The runner's own copy of the supervisor, outside the installed package.+ assert Path(supervisor).is_relative_to(paths.get_runtime_dir())+ assert Path(supervisor).read_bytes() == Path(watch_worker.__file__).read_bytes()kwargs = captured["kwargs"]assert kwargs["cwd"] == str(tmp_path)assert kwargs["stdin"] == asyncio.subprocess.PIPE@@ -397,6 +401,30 @@ async def test_spawn_uses_the_stable_supervisor_entrypoint(tmp_path: Path, monkeassert kwargs["stderr"] == asyncio.subprocess.PIPE+async def test_runner_starts_the_supervisor_it_imported_after_an_upgrade_replaces_the_installed_one(+ tmp_path: Path, monkeypatch+) -> None:+ """An upgrade installs its files before the service restarts. Until then the running runner speaks its own+ supervisor's protocol, so it must not start the newer supervisor that the install now holds."""+ installed = tmp_path / "watch_worker.py"+ shutil.copyfile(watch_worker.__file__, installed)+ monkeypatch.setattr(watch_worker, "__file__", str(installed))+ spec = importlib.util.spec_from_file_location("avibe_installed_command_runner", command_runner.__file__)+ runner = importlib.util.module_from_spec(spec)+ monkeypatch.setitem(sys.modules, spec.name, runner)+ spec.loader.exec_module(runner) # the service imports the runner+ installed.write_text("import sys\nsys.exit('a newer version, whose protocol this runner does not speak')\n")++ result = await runner.run_supervised_command(+ command=[sys.executable, "-c", "print('ran')"],+ cwd=str(tmp_path),+ timeout_seconds=10,+ label="test upgrade",+ )++ assert (result.exit_code, result.stdout.strip(), result.stderr) == (0, "ran", "")++async def test_spawn_can_discard_supervisor_stderr(tmp_path: Path, monkeypatch) -> None:captured: dict[str, object] = {}diff --git a/tests/test_watch_worker.py b/tests/test_watch_worker.pyindex cf35cf691..81fe71820 100644--- a/tests/test_watch_worker.py+++ b/tests/test_watch_worker.py@@ -13,7 +13,7 @@ from pathlib import Pathimport psutilimport pytest-from core import watch_worker+from core import command_runner, watch_workerfrom core.process_isolation import (PROCESS_IDENTITY_ENV,PersistedProcessIdentity,@@ -23,10 +23,7 @@ from core.process_isolation import (def _supervisor_command() -> list[str]:- return [- os.path.abspath(sys.executable),- str(Path(watch_worker.__file__).resolve()),- ]+ return [os.path.abspath(sys.executable), command_runner.supervisor_path()]def _supervisor_isolation_kwargs() -> dict[str, object]:@@ -377,9 +374,17 @@ def test_windows_watch_worker_job_tracks_descendant_after_command_root_exits() -@pytest.mark.skipif(os.name != "nt", reason="Windows exit code contract")-def test_windows_watch_worker_preserves_full_exit_code() -> None:[email protected]("entrypoint", ["runner copy", "installed file"])+def test_windows_watch_worker_preserves_full_exit_code(entrypoint: str) -> None:+ # A runner released before the copies starts the installed file itself while an upgrade waits for its+ # restart, without the helper the copy carries beside it.+ command = (+ _supervisor_command()+ if entrypoint == "runner copy"+ else [os.path.abspath(sys.executable), str(Path(watch_worker.__file__).resolve())]+ )result = subprocess.run(- _supervisor_command(),+ command,input=watch_worker.encode_watch_worker_spec(command=[sys.executable, "-c", "import os; os._exit(513)"],shell_command=None,diff --git a/tests/test_watches.py b/tests/test_watches.pyindex 79c467e14..53228bb70 100644--- a/tests/test_watches.py+++ b/tests/test_watches.py@@ -16,7 +16,7 @@ import pytestsys.path.insert(0, str(Path(__file__).resolve().parents[1]))from config import paths-from core import watch_worker, watches as watches_module+from core import command_runner, watch_worker, watches as watches_modulefrom core.process_isolation import (PersistedProcessIdentity,ProcessIdentity,@@ -415,10 +415,7 @@ def test_managed_watch_exec_uses_stable_supervisor(tmp_path: Path, monkeypatch)result = asyncio.run(service._run_cycle(watch, timeout_seconds=5))assert result.exit_code == 0- assert captured["args"] == (- os.path.abspath(sys.executable),- str(Path(watch_worker.__file__).resolve()),- )+ assert captured["args"] == (os.path.abspath(sys.executable), command_runner.supervisor_path())assert captured["kwargs"]["stdin"] == asyncio.subprocess.PIPEassert captured["kwargs"]["stdout"] == asyncio.subprocess.PIPEassert captured["kwargs"]["stderr"] == asyncio.subprocess.PIPE@@ -471,10 +468,7 @@ def test_managed_watch_shell_uses_stable_supervisor(tmp_path: Path, monkeypatch)result = asyncio.run(service._run_cycle(watch, timeout_seconds=5))assert result.exit_code == 0- assert captured["args"] == (- os.path.abspath(sys.executable),- str(Path(watch_worker.__file__).resolve()),- )+ assert captured["args"] == (os.path.abspath(sys.executable), command_runner.supervisor_path())assert captured["kwargs"]["stdin"] == asyncio.subprocess.PIPEassert captured["kwargs"]["stdout"] == asyncio.subprocess.PIPEassert captured["kwargs"]["stderr"] == asyncio.subprocess.PIPE