Skip to content

Commit a95d991

Browse files
committed
fix(update): pin detached worker to its owning installation
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent d41e71b commit a95d991

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎docs/development/frontend-delivery.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ uv run --extra test python scripts/chat_bundle.py verify --source
425425
uv run --extra test loopx chat --no-open
426426
```
427427

428+
`loopx update apply` and `loopx upgrade` keep their detached worker on the
429+
requesting installation, including when invoked inside another LoopX checkout.
430+
The caller's working directory and explicit registry/runtime routes remain in
431+
effect; a workspace or `PYTHONPATH` entry must not select another installation's
432+
update code. An observer disconnect still leaves one durable worker and the
433+
existing installation lock, status and result readback in control.
434+
428435
Builds start with current assets only. Use `build --previous /path/to/prior/chat`
429436
when deliberately carrying a prior local delivery; release and installation
430437
commands select their predecessor automatically.

‎loopx/cli_commands/update_operation.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,15 @@ def start_or_observe(args: argparse.Namespace, registry_path: Path, *, registry_
115115
os.chmod(folder / "worker.log", 0o600)
116116
options = {"start_new_session": True} if os.name != "nt" else {
117117
"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS}
118-
child = subprocess.Popen([sys.executable, "-m", __name__, "--worker", operation_id],
118+
# Keep relative source/state routes in the caller's cwd, but import
119+
# the installation that admitted this update. Plain -m can load a
120+
# different LoopX checkout from cwd before the installed package.
121+
entry = (
122+
"import sys; sys.path.insert(0, sys.argv.pop(1)); "
123+
"import runpy; runpy.run_module('loopx.cli_commands.update_operation', run_name='__main__')"
124+
)
125+
child = subprocess.Popen([sys.executable, "-P", "-c", entry,
126+
str(Path(__file__).resolve().parents[2]), "--worker", operation_id],
119127
stdin=subprocess.DEVNULL, stdout=log, stderr=log, **options)
120128
atomic_json(root / f"{key}.active.json", {"operation_id": operation_id, "pid": child.pid})
121129
atomic_json(folder / "worker.json", {"pid": child.pid})

‎tests/test_update_operation.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import json
55
import os
6+
from pathlib import Path
67
import subprocess
78
import sys
89
import time
@@ -124,6 +125,55 @@ def test_real_detached_worker_finishes_after_its_observer_exits(tmp_path):
124125
assert payload["result"]["plan"]["apply_supported"] is False
125126

126127

128+
@pytest.mark.parametrize("command", ["update", "upgrade"])
129+
@pytest.mark.parametrize("shadow_pythonpath", [False, True])
130+
def test_detached_worker_keeps_its_installation_in_a_different_checkout(
131+
tmp_path, monkeypatch, command, shadow_pythonpath,
132+
):
133+
"""The requesting installation owns the worker, not the caller's workspace."""
134+
foreign = tmp_path / "another checkout"
135+
shadow = foreign / "loopx"
136+
shadow.mkdir(parents=True)
137+
imported = tmp_path / "foreign-imported"
138+
(shadow / "__init__.py").write_text(
139+
f"from pathlib import Path\nPath({str(imported)!r}).touch()\n"
140+
"raise RuntimeError('foreign installation must not run the update')\n"
141+
)
142+
monkeypatch.chdir(foreign)
143+
monkeypatch.setenv("LOOPX_UPDATE_STATE_DIR", str(tmp_path / "state"))
144+
monkeypatch.setenv("LOOPX_BIN_DIR", str(tmp_path / "bin"))
145+
monkeypatch.delenv("LOOPX_RELEASE_ROOT", raising=False)
146+
# Exercise the real checkout's supported refusal, never an installed user's
147+
# archive/package-manager mutation. The worker still runs the owning CLI.
148+
own_root = Path(operations.__file__).resolve().parents[2]
149+
monkeypatch.setenv("PATH", str(own_root / "scripts") + os.pathsep + os.environ["PATH"])
150+
if shadow_pythonpath:
151+
monkeypatch.setenv("PYTHONPATH", str(foreign))
152+
else:
153+
monkeypatch.delenv("PYTHONPATH", raising=False)
154+
registry, runtime = tmp_path / "registry.json", tmp_path / "runtime"
155+
if command == "upgrade":
156+
registry, runtime = Path("registry.json"), Path("runtime")
157+
args = argparse.Namespace(command=command, runtime_root=str(runtime),
158+
timeout_seconds=30, repo=None, ref="main", archive_url=None,
159+
source=True, allow_downgrade=False)
160+
started = operations.start_or_observe(args, registry, registry_was_supplied=True)
161+
operation_id = started["operation_id"]
162+
for _ in range(100):
163+
state = operations.read_operation(operation_id)
164+
if imported.exists() or state["status"] not in {"starting", "running"}:
165+
break
166+
time.sleep(.1)
167+
assert not imported.exists(), "the detached process imported the foreign checkout"
168+
assert state["status"] == "failed", state
169+
assert state["result"]["changes_applied"] is False
170+
assert state["result"]["plan"]["apply_supported"] is False
171+
request = json.loads((operations.operation_root() / operation_id / "request.json").read_text())
172+
argv = request["argv"]
173+
assert argv[argv.index("--registry") + 1] == str(registry)
174+
assert argv[argv.index("--runtime-root") + 1] == str(runtime)
175+
176+
127177
@pytest.mark.parametrize("switched", [True, False])
128178
def test_material_failure_reports_an_observed_switch_but_never_invents_one(monkeypatch, switched):
129179
from loopx.self_update import execute_update_plan

0 commit comments

Comments
 (0)