diff --git a/README.md b/README.md index e05466b..1bacc3e 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Depending on setup, Codex reaches the ledger through a local MCP bridge or a com ## Choose a setup -- **Local Windows:** one person, no VM. Download the [Windows installer](https://github.com/gonzoinc/Conductor/releases/tag/v0.4.0), choose Codex desktop, VS Code, or both, and keep task data in a separate folder. Setup downloads a private Python runtime and runs the ledger only when Codex uses it. +- **Local Windows:** one person, no VM. Download the [Windows installer](https://github.com/gonzoinc/Conductor/releases/tag/v0.4.1), choose Codex desktop, VS Code, or both, and keep task data in a separate folder. Setup downloads a private Python runtime and runs the ledger only when Codex uses it. - **Local macOS:** one person, no VM. Needs Python 3.11+, internet for package installation, and a stable local checkout. The SQLite ledger runs through an MCP bridge only while Codex uses it. - **Shared Linux VM + Windows client:** a team or multiple computers. The Windows client uses an authorized SSH login and installs one command and the skill. It needs no local Python, MCP package, copied service key, or background tunnel. @@ -51,7 +51,7 @@ The [older hosted MCP path](docs/client-setup.md#single-script-windows-hosted-se ### Local Windows -Download `ConductorSetup.exe` from the [latest release](https://github.com/gonzoinc/Conductor/releases/tag/v0.4.0) and run it as your normal Windows user. The app guides you through the install folder, task-data folder, and Codex clients. See the [Windows setup guide](docs/windows-gui-setup.md) for the full walkthrough. +Download `ConductorSetup.exe` from the [latest release](https://github.com/gonzoinc/Conductor/releases/tag/v0.4.1) and run it as your normal Windows user. The app guides you through the install folder, task-data folder, and Codex clients. See the [Windows setup guide](docs/windows-gui-setup.md) for the full walkthrough. ### Local macOS diff --git a/docs/releases/v0.4.1.md b/docs/releases/v0.4.1.md new file mode 100644 index 0000000..2b96339 --- /dev/null +++ b/docs/releases/v0.4.1.md @@ -0,0 +1,19 @@ +# Conductor 0.4.1 + +Fixes [issue #3](https://github.com/gonzoinc/Conductor/issues/3): Windows packaged Desktop and VS Code could resolve the same AppData ledger path differently, preventing VS Code from connecting to an existing local ledger. + +## Windows setup and repair + +- New local installations keep task data in `%USERPROFILE%\.conductor`, separate from the program installation. Setup rejects redirectable AppData task-data locations. +- Existing installations have an explicit offline repair command. It verifies a consistent SQLite backup and all records, preserves private permissions and ownership metadata, switches settings atomically, and retains the original ledger and rollback journal. Logical and physical aliases of one database are recognized as one source; conflicting databases are rejected. +- Bridge errors identify a missing configured ledger and the possible Windows package-redirection mismatch. Startup and installer retries refuse to initialize an empty replacement for a configured missing ledger. + +**Existing users:** downloading or rerunning setup does not migrate your ledger automatically. Close both clients and follow the [repair instructions](https://github.com/gonzoinc/Conductor/blob/v0.4.1/docs/windows-gui-setup.md#repair-an-existing-redirected-local-ledger). Keep the original ledger until both clients pass the shared-read/write and restart checks. + +Download `ConductorSetup.exe` below for new Windows installations. Run it as your normal Windows user. The accompanying SHA-256 file verifies the download. macOS and Linux state-directory defaults are unchanged. + +## Validation + +Installer, GUI, bridge, and migration tests passed locally, including interrupted two-profile migration and rollback. The corrected installer passed its packaged self-test. Actual Codex Desktop and VS Code health, writes in both directions to the same migrated ledger, and post-restart reads were verified on the repaired Windows installation. Actual macOS client testing was not performed for this patch. + +The release workflow builds the installer from this tag, runs Windows setup tests and the packaged self-test, and publishes the installer and checksum. diff --git a/docs/windows-gui-setup.md b/docs/windows-gui-setup.md index 546336e..1e9d7e3 100644 --- a/docs/windows-gui-setup.md +++ b/docs/windows-gui-setup.md @@ -3,7 +3,7 @@ `ConductorSetup.exe` installs the local, single-user ledger for Codex desktop and the Codex VS Code extension. It downloads a private Python runtime and Python packages during installation, so an internet connection is required. It does not ask for a Git-host token or a Codex API key. To install a shared service on a Linux VM, use [the VM installer](host-install.md) on that VM. 1. Run `ConductorSetup.exe` on Windows 10 or 11 as the same normal Windows user who runs Codex. Administrator rights are not needed. Running as another administrator account installs into that account's profile instead. -2. Pick an app folder and a separate local task-data folder. No VM, Docker, SSH, or service key is needed. +2. Pick an app folder and a separate local task-data folder (default: `%USERPROFILE%\.conductor`). Task data under AppData is rejected because Windows packaged apps can redirect it. No VM, Docker, SSH, or service key is needed. 3. Choose Codex desktop, VS Code, or both. **Separate test profiles** is checked by default and leaves normal Codex settings alone. Clear it only when you want Conductor added to your usual Codex configuration. 4. Select **Install Conductor**. Progress and errors appear in the app. Fix an error and select the same button again to resume. Cancel stops the current command and can be retried. A newer setup package can reuse an earlier Conductor-owned app folder; it stages updated source inside that folder and preserves the earlier source during setup. @@ -18,3 +18,27 @@ The Windows GUI has no VM option. A Linux VM uses its own one-command installer ## Building the Windows app From a Windows checkout, run `py -3 gui/build_windows.py .`. The script requires an installed Python 3.11+ and network access. It pins PyInstaller, downloads uv from its official release, verifies SHA256 for the archive and executable, and includes the maintained Conductor source tree. Build scratch and the resulting executable stay under the repository's primary `.codex/gui-build/` directory. The expected output is `.codex/gui-build/dist/ConductorSetup.exe`. Run setup and the client checks on the target machine after building. + +## Repair an existing redirected local ledger + +Changing the default does not move an existing ledger. Do not delete the old database or rerun setup into an empty folder to work around a missing-ledger error. The bridge and repeat installer refuse to recreate a configured missing database. + +Close Codex Desktop, VS Code, and any other Conductor writers before repair. Use the new source with a Python environment containing Conductor's legacy MCP dependencies, as the same normal Windows user. The repair command is included in the GUI source payload. Supply the **physical existing** database path, a destination that does not yet exist, and every affected `client.json` (repeat `--settings` for separate profiles). Do not copy a live SQLite file with Explorer; it may have uncheckpointed WAL records. + +```powershell +& '' '\scripts\repair_local.py' --source '' --destination "$HOME\.conductor" --settings "$HOME\.codex\conductor-coordination\client.json" --clients-stopped +``` + +For a redirected installation, the physical source is typically beneath `%LOCALAPPDATA%\Packages\\LocalCache\Local\Conductor`. The tool checks the configured location and matching package-cache candidates. It refuses missing or ambiguous sources and existing destinations. It verifies source ownership and private permissions, holds a SQLite writer lock during backup, checks integrity and all schema/records at the destination, then switches settings using private atomic writes. The original ledger is retained. Interrupted repairs retain the destination and `.conductor-repair.json` journal for recovery; do not retry into that existing destination. + +With clients still stopped, restore original settings using: + +```powershell +& '' '\scripts\repair_local.py' --rollback "$HOME\.conductor\.conductor-repair.json" --clients-stopped +``` + +Rollback preserves both databases and refuses to overwrite unrelated settings changes. If either ledger received writes after cutover, resolve those records before rollback or deleting either copy; rollback does not merge databases. A forced interruption between settings writes can leave profiles temporarily split: keep clients closed and use the journal to restore settings before restarting. + +Installation or repair success means **actual-client verification is pending**. In actual Desktop and VS Code sessions, confirm `coordination_health` is discovered and succeeds. Create a bounded test workload in Desktop and read it in VS Code, write a change in VS Code and read it in Desktop, then restart both clients and repeat the read/health checks. Use the same workload ID throughout. Keep the old ledger and rollback journal until these checks pass. Backend checks alone do not establish shared physical storage. + +macOS and Linux state-directory defaults are unchanged. diff --git a/gui/build_windows.py b/gui/build_windows.py index 8207cb5..1d3312e 100644 --- a/gui/build_windows.py +++ b/gui/build_windows.py @@ -33,7 +33,7 @@ def build_source_zip(source: Path, output: Path) -> None: with zipfile.ZipFile(output, "w", compression=zipfile.ZIP_DEFLATED) as archive: candidates = [source / name for name in roots] candidates.extend(source / "scripts" / name for name in - ("bootstrap.py", "install_client.py", "legacy_install.py")) + ("bootstrap.py", "install_client.py", "legacy_install.py", "repair_local.py")) for name in prefixes: folder = source / name if folder.is_dir(): @@ -97,7 +97,7 @@ def main() -> None: environment = dict(os.environ, PYINSTALLER_CONFIG_DIR=str(build / "pyinstaller-cache"), TEMP=str(build / "tmp"), TMP=str(build / "tmp"), TMPDIR=str(build / "tmp")) subprocess.run([str(python), "-m", "PyInstaller", "--noconfirm", "--clean", "--onefile", "--windowed", - "--name", "ConductorSetup", "--distpath", str(build / "dist"), "--workpath", str(build / "work"), + "--paths", str(source / "src"), "--name", "ConductorSetup", "--distpath", str(build / "dist"), "--workpath", str(build / "work"), "--specpath", str(build), "--add-data", add("uv.exe"), "--add-data", add("conductor-source.zip"), "--add-data", add("runtime-lock.json"), str(GUI / "setup_app.py")], check=True, env=environment) diff --git a/gui/setup_app.py b/gui/setup_app.py index b6b941d..306291e 100644 --- a/gui/setup_app.py +++ b/gui/setup_app.py @@ -11,7 +11,7 @@ from tkinter import filedialog, ttk from setup_controller import (SetupCancelled, SetupConfig, completed_setup, install, - installed_setup, launch_test_profile) + installed_setup, launch_test_profile, local_state_root) from workflow_window import WorkflowWindow @@ -27,7 +27,7 @@ def __init__(self) -> None: self.configure(padx=28, pady=22) local = Path(os.environ.get("LOCALAPPDATA", str(Path.home() / "AppData/Local"))) self.install_path = tk.StringVar(value=str(local / "Programs/Conductor")) - self.data_path = tk.StringVar(value=str(local / "Conductor")) + self.data_path = tk.StringVar(value=str(local_state_root())) self.desktop = tk.BooleanVar(value=True) self.vscode = tk.BooleanVar(value=False) self.isolated = tk.BooleanVar(value=True) diff --git a/gui/setup_controller.py b/gui/setup_controller.py index 7439ee7..a155a15 100644 --- a/gui/setup_controller.py +++ b/gui/setup_controller.py @@ -19,6 +19,10 @@ from client_launch import launch_isolated +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) +from conductor_coordination.state_paths import local_state_root, validate_shared_windows_path + + PYTHON_VERSION = "3.12.15" OWNERSHIP = {"product": "conductor-codex", "purpose": "windows-gui-install", "version": 1} @@ -139,6 +143,7 @@ def validate(config: SetupConfig) -> SetupConfig: if config.state_dir is None: raise ValueError("Choose where Conductor should keep its local task data.") state = _local_path(config.state_dir) + validate_shared_windows_path(state) install = _local_path(config.install_dir) if state == install or state.is_relative_to(install) or install.is_relative_to(state): raise ValueError("Choose separate folders for the app and task data.") @@ -328,5 +333,5 @@ def install(config: SetupConfig, resources: Path, progress: Callable[[str], None arguments += ["--local-state-root", str(_local_path(config.state_dir)), "--db-path", str(_local_path(config.state_dir) / "tasks.sqlite3")] _run([str(python), str(source / "scripts/bootstrap.py"), str(source), *arguments], env=env, cancel=cancel, progress=progress) - progress("Conductor setup is complete.") + progress("Conductor installation is complete. Desktop and VS Code verification is pending; restart both clients and run the cross-client acceptance checks.") return install_dir diff --git a/pyproject.toml b/pyproject.toml index 030c635..977ed6f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "conductor-codex" -version = "0.4.0" +version = "0.4.1" description = "Durable task coordination for native Codex subagents" requires-python = ">=3.11" dependencies = [] diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py index 94b3863..9f5cd7b 100644 --- a/scripts/bootstrap.py +++ b/scripts/bootstrap.py @@ -12,6 +12,10 @@ import tomllib +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) +from conductor_coordination.state_paths import local_state_root + + def run(command: list[str], *, root: Path, env: dict[str, str], stage: str, quiet: bool = False) -> None: print(stage + '...', flush=True) subprocess.run(command, cwd=root, env=env, check=True, **({"stdout": subprocess.PIPE} if quiet else {})) @@ -154,10 +158,7 @@ def setup(root: Path, arguments: list[str], *, prepared_runtime: Path | None = N else: if args.token_file or args.url or args.ssh_target: raise ValueError('Local mode does not use a service URL or token file') - if os.name == 'nt': - default_root = Path(os.environ.get('LOCALAPPDATA') or Path.home() / 'AppData/Local') / 'Conductor' - else: - default_root = Path(os.environ.get('XDG_STATE_HOME') or Path.home() / '.local/state') / 'conductor' + default_root = local_state_root() state_root = (args.local_state_root or default_root).expanduser().absolute() database = (args.db_path or state_root / 'tasks.sqlite3').expanduser().absolute() # The installed client performs the full local-disk and private-path checks. diff --git a/scripts/install_client.py b/scripts/install_client.py index 19bfe83..89e3d2a 100644 --- a/scripts/install_client.py +++ b/scripts/install_client.py @@ -18,6 +18,7 @@ import tempfile import tomllib +from conductor_coordination.state_paths import validate_shared_windows_path from conductor_coordination.client_bridge import local_state_root, read_private, validate_local_database, validate_url SERVER_NAME = "conductor_coordination" @@ -81,6 +82,7 @@ def install(*, codex_home: Path, skills_dir: Path, url: str | None = None, token if token is not None or url is not None or ssh_target is not None: raise ValueError("Local setup uses no service URL or credential") data_root = (data_root or local_state_root()).expanduser().absolute() + validate_shared_windows_path(data_root) db_path = validate_local_database(db_path or data_root / "tasks.sqlite3", data_root) else: raise ValueError("Unknown Conductor client mode") @@ -93,6 +95,10 @@ def install(*, codex_home: Path, skills_dir: Path, url: str | None = None, token settings_dir = codex_home / "conductor-coordination" token_path = settings_dir / "service-token" settings_path = settings_dir / "client.json" + if mode == "local" and settings_path.exists(): + saved = json.loads(read_private(settings_path)) + if saved.get("mode") == "local" and not Path(saved["db_path"]).is_file(): + raise ValueError(f"Configured ledger is missing: {saved['db_path']}. Use explicit repair; no empty replacement was created.") data_marker = data_root / ".conductor-local-owner.json" if mode == "local" else None marker_content = b'{"purpose":"conductor-local-state","version":1}\n' skill_source = source_root / "skills" / SKILL_NAME diff --git a/scripts/install_support.py b/scripts/install_support.py index c90ac10..26935b8 100644 --- a/scripts/install_support.py +++ b/scripts/install_support.py @@ -20,6 +20,10 @@ import urllib.request +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) +from conductor_coordination.state_paths import local_state_root + + def protect(path: Path) -> None: if os.name == 'nt': result = subprocess.run(['whoami', '/user', '/fo', 'csv', '/nh'], check=True, capture_output=True, text=True) @@ -49,10 +53,7 @@ def private_write(path: Path, data: bytes, *, replace: bool = False) -> None: def local_database_path(identifier: str) -> Path: if not re.fullmatch(r'[a-f0-9]{8}', identifier): raise ValueError('Invalid local setup identifier') - if os.name == 'nt': - base = Path(os.environ.get('LOCALAPPDATA') or Path.home() / 'AppData/Local') / 'Conductor' - else: - base = Path(os.environ.get('XDG_STATE_HOME') or Path.home() / '.local/state') / 'conductor' + base = local_state_root() return (base / 'guided' / identifier / 'tasks.sqlite3').absolute() diff --git a/scripts/repair_local.py b/scripts/repair_local.py new file mode 100644 index 0000000..5d2df50 --- /dev/null +++ b/scripts/repair_local.py @@ -0,0 +1,158 @@ +"""Explicit, offline local-ledger repair. Stop both clients before using this tool.""" +from __future__ import annotations + +import argparse +from contextlib import closing +import json +from pathlib import Path +import sqlite3 +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) + +from install_client import atomic_write, private_permissions +from conductor_coordination.client_bridge import ensure_private, read_private, validate_local_database +from conductor_coordination.state_paths import validate_shared_windows_path + +MARKER = ".conductor-local-owner.json" +JOURNAL = ".conductor-repair.json" + + +def snapshot(connection: sqlite3.Connection) -> list: + """Compare schema and every row, including history and SQLite sequence state.""" + if connection.execute("PRAGMA integrity_check").fetchall() != [("ok",)]: + raise ValueError("Ledger integrity check failed") + schema = connection.execute("SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY type,name").fetchall() + tables = [] + for kind, name, _, _ in schema: + if kind == "table": + quoted = '"' + name.replace('"', '""') + '"' + rows = connection.execute("SELECT * FROM " + quoted).fetchall() + tables.append((name, sorted(repr(row) for row in rows))) + if not {"workloads", "tasks", "operations", "events"}.issubset({name for name, _ in tables}): + raise ValueError("Source has no ledger tables; refusing an empty replacement") + return [schema, tables] + + +def source_candidates(configured: Path) -> list[Path]: + """Report legacy candidates; never select one automatically.""" + candidates = [configured] + legacy = Path.home() / "AppData/Local" + if configured.is_relative_to(legacy): + suffix = configured.relative_to(legacy) + packages = legacy / "Packages" + if packages.is_dir(): + candidates.extend(packages.glob("*/LocalCache/Local/" + suffix.as_posix())) + unique = [] + for path in candidates: + if path.is_file() and not any(path.samefile(other) for other in unique): + unique.append(path) + return unique + + +def rollback(journal: Path, *, clients_stopped: bool) -> None: + if not clients_stopped: + raise ValueError("Stop Desktop, VS Code and all ledger writers before repair or rollback") + state = json.loads(read_private(journal)) + if state.get("purpose") != "conductor-local-repair" or state.get("version") != 1 or not isinstance(state.get("settings"), list): + raise ValueError("Invalid Conductor repair journal") + # Preflight every setting before restoring any of them. + for item in state["settings"]: + current = read_private(Path(item["path"])) + if current not in (item["before"], item["after"]): + raise ValueError("Client settings changed after repair; refusing to overwrite them") + for item in state["settings"]: + atomic_write(Path(item["path"]), item["before"].encode()) + # Keep both databases and the journal: writes after cutover must never be discarded. + + +def migrate(source: Path, destination: Path, settings_paths: list[Path], *, clients_stopped: bool) -> Path: + if not clients_stopped: + raise ValueError("Stop Desktop, VS Code and all ledger writers before repair or rollback") + source = source.expanduser().absolute() + destination = destination.expanduser().absolute() + validate_shared_windows_path(destination) + database = validate_local_database(destination / "tasks.sqlite3", destination) + if destination.exists(): + raise ValueError("Repair destination must not exist; existing files were preserved") + if not settings_paths or len(set(settings_paths)) != len(settings_paths): + raise ValueError("Supply each affected client settings file exactly once") + ensure_private(source) + source_root = None + changes = [] + for path in settings_paths: + path = path.expanduser().absolute() + before = read_private(path) + setting = json.loads(before) + if setting.get("mode") != "local": + raise ValueError("Repair only accepts existing local client settings") + configured = Path(setting["db_path"]) + configured_root = Path(setting.get("state_root", configured.parent)) + validate_local_database(configured, configured_root) + candidates = source_candidates(configured) + if len(candidates) != 1 or not candidates[0].samefile(source): + raise ValueError(f"Source is missing, ambiguous, or does not match {path}; candidates: {candidates}") + relative = configured.relative_to(configured_root) + physical_root = source.parents[len(relative.parts) - 1] + if source_root is not None and source_root != physical_root: + raise ValueError("Client settings disagree on source state root") + source_root = physical_root + setting.update(db_path=str(database), state_root=str(destination)) + changes.append({"path": str(path), "before": before, "after": json.dumps(setting, indent=2) + "\n"}) + validate_local_database(source, source_root) + ensure_private(source_root, directory=True) + ensure_private(source.parent, directory=True) + marker_text = read_private(source_root / MARKER) + if json.loads(marker_text) != {"purpose": "conductor-local-state", "version": 1}: + raise ValueError("Source ownership marker is invalid") + # Explicit URI modes prevent sqlite3 from creating a missing source database. + with closing(sqlite3.connect(source.as_uri() + "?mode=rw", uri=True, timeout=5)) as lock: + lock.execute("BEGIN IMMEDIATE") + with closing(sqlite3.connect(source.as_uri() + "?mode=ro", uri=True)) as original: + expected = snapshot(original) + destination.mkdir(mode=0o700) + private_permissions(destination) + journal = destination / JOURNAL + atomic_write(journal, (json.dumps({"purpose": "conductor-local-repair", "version": 1, "source": str(source), "settings": changes}, indent=2) + "\n").encode()) + try: + with closing(sqlite3.connect(database)) as copied: + private_permissions(database) + original.backup(copied) + if snapshot(copied) != expected: + raise ValueError("Destination schema or task history differs from source") + atomic_write(destination / MARKER, marker_text.encode()) + for item in changes: + if read_private(Path(item["path"])) != item["before"]: + raise ValueError("Client settings changed during repair") + atomic_write(Path(item["path"]), item["after"].encode()) + except BaseException: + rollback(journal, clients_stopped=True) + raise + finally: + lock.rollback() + return journal + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--source", type=Path) + parser.add_argument("--destination", type=Path) + parser.add_argument("--settings", type=Path, action="append", default=[]) + parser.add_argument("--rollback", type=Path, help="Repair journal to restore original settings; preserves both ledgers") + parser.add_argument("--clients-stopped", action="store_true", help="Confirm all clients and ledger writers are stopped") + args = parser.parse_args() + try: + if args.rollback: + rollback(args.rollback, clients_stopped=args.clients_stopped) + elif args.source and args.destination: + journal = migrate(args.source, args.destination, args.settings, clients_stopped=args.clients_stopped) + print(f"Ledger copied and settings switched. Rollback journal: {journal}") + print("Keep the original ledger. Actual Desktop and VS Code acceptance is still pending.") + else: + parser.error("Supply --source, --destination and --settings, or --rollback") + except (ValueError, OSError, sqlite3.Error) as error: + parser.exit(2, f"Repair stopped: {error}\n") + + +if __name__ == "__main__": + main() diff --git a/src/conductor_coordination/client_bridge.py b/src/conductor_coordination/client_bridge.py index d2a9a59..11806ad 100644 --- a/src/conductor_coordination/client_bridge.py +++ b/src/conductor_coordination/client_bridge.py @@ -18,6 +18,7 @@ from mcp.server import MCPServer from conductor_coordination import coordination_proxy +from conductor_coordination.state_paths import local_state_root _SSH_TUNNEL = None @@ -117,14 +118,6 @@ def read_private(path: Path) -> str: return path.read_text(encoding="utf-8") -def local_state_root() -> Path: - if os.name == "nt": - base = Path(os.environ.get("LOCALAPPDATA") or Path.home() / "AppData/Local") - return base / "Conductor" - base = Path(os.environ.get("XDG_STATE_HOME") or Path.home() / ".local/state") - return base / "conductor" - - def validate_local_database(path: Path, state_root: Path | None = None) -> Path: path = path.expanduser().absolute() root = (state_root or local_state_root()).expanduser().absolute() @@ -170,6 +163,10 @@ def configure(settings_path: Path) -> dict: if mode == "local": state_root = Path(settings["state_root"]) if "state_root" in settings else local_state_root() database = validate_local_database(Path(settings["db_path"]), state_root) + if not database.is_file(): + raise ValueError(f"Configured Conductor ledger is missing or not a regular file: {database}. " + "On Windows, packaged Desktop and VS Code may see different AppData paths. " + "Use explicit ledger repair; no replacement database was created.") ensure_private(state_root, directory=True) ensure_private(database.parent, directory=True) ensure_private(database) @@ -204,7 +201,7 @@ def local_dispatch(name: str, arguments: dict) -> dict: coordination_proxy._dispatch = local_dispatch else: coordination_proxy._dispatch = coordination_proxy._remote - server = MCPServer("Conductor", version="0.4.0", instructions=( + server = MCPServer("Conductor", version="0.4.1", instructions=( "First choose workload shape. For one coherent task, work directly, including " "investigation, changes, validation and cleanup; no ledger is needed. For multiple " "distinct workstreams, manage native workers and record verified results here. " diff --git a/src/conductor_coordination/coordination_server.py b/src/conductor_coordination/coordination_server.py index 7e56030..6c9b461 100644 --- a/src/conductor_coordination/coordination_server.py +++ b/src/conductor_coordination/coordination_server.py @@ -39,7 +39,7 @@ def dispatch(name, arguments): api._dispatch = dispatch -mcp = MCPServer("Conductor Native Coordination", version="0.4.0", instructions=( +mcp = MCPServer("Conductor Native Coordination", version="0.4.1", instructions=( "Durable metadata for client-owned native Codex subagents. First choose workload shape: " "for one coherent task, the main agent performs investigation, implementation, testing " "and cleanup directly, without ledger overhead. Mechanical steps within one issue are " diff --git a/src/conductor_coordination/state_paths.py b/src/conductor_coordination/state_paths.py new file mode 100644 index 0000000..1a2ad7e --- /dev/null +++ b/src/conductor_coordination/state_paths.py @@ -0,0 +1,26 @@ +"""Shared, dependency-free local state paths for installers and the bridge.""" +from __future__ import annotations + +import os +import stat +from pathlib import Path + + +def local_state_root() -> Path: + if os.name == "nt": + return Path.home() / ".conductor" + return Path(os.environ.get("XDG_STATE_HOME") or Path.home() / ".local/state") / "conductor" + + +def validate_shared_windows_path(path: Path) -> None: + """Reject known virtualized locations; client acceptance still remains required.""" + if os.name != "nt": + return + value = path.expanduser().absolute() + appdata = Path.home() / "AppData" + if value == appdata or value.is_relative_to(appdata): + raise ValueError(f"Conductor task data at {value} may resolve differently in packaged Desktop and VS Code. " + "Choose a local folder outside AppData, such as ~/.conductor; use explicit repair for existing data.") + if any(part.is_symlink() or (part.exists() and part.stat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT) + for part in (value, *value.parents)): + raise ValueError("Shared task data cannot use symbolic links or junctions") diff --git a/tests/test_client_bridge.py b/tests/test_client_bridge.py index cf10a74..5e4d3d9 100644 --- a/tests/test_client_bridge.py +++ b/tests/test_client_bridge.py @@ -9,12 +9,26 @@ import threading from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer import unittest +from unittest import mock + +from conductor_coordination import client_bridge from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client class BridgeSessionTests(unittest.IsolatedAsyncioTestCase): + def test_missing_local_ledger_explains_redirection_without_creating_file(self): + with tempfile.TemporaryDirectory() as folder: + root = Path(folder) + database = root / "tasks.sqlite3" + settings = json.dumps({"mode": "local", "db_path": str(database), "state_root": str(root)}) + with mock.patch.object(client_bridge, "read_private", return_value=settings): + with self.assertRaisesRegex(ValueError, "packaged Desktop and VS Code") as error: + client_bridge.configure(root / "client.json") + self.assertIn(str(database), str(error.exception)) + self.assertFalse(database.exists()) + async def test_native_stdio_session_discovers_and_forwards_fourteen_tools(self): token = "stdio-session-test-" + "y" * 40 observations = [] diff --git a/tests/test_client_setup.py b/tests/test_client_setup.py index e8d44e2..2f60e2d 100644 --- a/tests/test_client_setup.py +++ b/tests/test_client_setup.py @@ -122,6 +122,18 @@ def test_local_user_install_preserves_ledger_and_explains_mcp_transport(self): if os.name != 'nt': self.assertEqual(stat.S_IMODE(database.stat().st_mode), 0o600) + def test_repeat_local_install_does_not_recreate_missing_ledger(self): + options = dict(mode="local", url=None, token=None, data_root=self.root / "state") + self.install(**options) + settings_path = self.codex / "conductor-coordination/client.json" + before = settings_path.read_bytes() + database = Path(json.loads(before)["db_path"]) + database.unlink() + with self.assertRaisesRegex(ValueError, "no empty replacement"): + self.install(**options) + self.assertFalse(database.exists()) + self.assertEqual(settings_path.read_bytes(), before) + def test_installer_command_uses_token_file_without_printing_credential(self): credential = self.root / "incoming-token" credential.write_text(self.token) diff --git a/tests/test_gui_setup.py b/tests/test_gui_setup.py index 49ab45f..6050c9d 100644 --- a/tests/test_gui_setup.py +++ b/tests/test_gui_setup.py @@ -30,6 +30,10 @@ def config(self, **changes): values.update(changes) return SetupConfig(**values) + def test_rejects_redirectable_task_data(self): + with self.assertRaisesRegex(ValueError, "resolve differently"): + validate(self.config(state_dir=Path.home() / "AppData/Local/Conductor")) + def test_builder_repairs_existing_venv_without_pip(self): venv = self.root / "builder-no-pip" subprocess.run([sys.executable, "-m", "venv", "--without-pip", str(venv)], check=True) @@ -59,6 +63,8 @@ def test_gui_package_excludes_vm_installer(self): names = set(archive.namelist()) self.assertIn("scripts/bootstrap.py", names) self.assertIn("scripts/install_client.py", names) + self.assertIn("scripts/repair_local.py", names) + self.assertIn("src/conductor_coordination/state_paths.py", names) self.assertNotIn("install-vm.sh", names) self.assertNotIn("scripts/install_host.py", names) self.assertFalse(any(name.startswith("deploy/") for name in names)) diff --git a/tests/test_local_repair.py b/tests/test_local_repair.py new file mode 100644 index 0000000..f5f4c83 --- /dev/null +++ b/tests/test_local_repair.py @@ -0,0 +1,180 @@ +"""Regression coverage for offline migration and path redirection guards.""" +from __future__ import annotations + +from contextlib import closing +import json +import os +from pathlib import Path +import sqlite3 +import sys +import tempfile +import unittest +from unittest import mock + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "scripts")) +import repair_local as repair +import install_client as installer +from conductor_coordination.client_bridge import configure +from conductor_coordination.coordination_store import Store +from conductor_coordination import state_paths + + +class RepairTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + self.source = self.root / "old" / "tasks.sqlite3" + self.source.parent.mkdir() + installer.private_permissions(self.source.parent) + Store(self.source) + installer.private_permissions(self.source) + installer.atomic_write(self.source.parent / repair.MARKER, + b'{"purpose":"conductor-local-state","version":1}\n') + self.settings = self.root / "client.json" + installer.atomic_write(self.settings, json.dumps({"mode": "local", "state_root": str(self.source.parent), + "db_path": str(self.source)}).encode()) + self.before = self.settings.read_bytes() + self.destination = self.root / "new" + + def migrate(self): + return repair.migrate(self.source, self.destination, [self.settings], clients_stopped=True) + + def test_preserves_every_record_and_supports_rollback(self): + store = Store(self.source) + store.claim("retained-work", "manager", "desktop", "claim-1", 300) + # Preserve workload, operation, event and user records. + with closing(sqlite3.connect(self.source)) as db: + db.execute("INSERT INTO users (id, name, created) VALUES (?, ?, ?)", ("repair-test", "Repair Test", 1.0)) + db.commit() + journal = self.migrate() + with closing(sqlite3.connect(self.source)) as old, closing(sqlite3.connect(self.destination / "tasks.sqlite3")) as new: + self.assertEqual(repair.snapshot(old), repair.snapshot(new)) + self.assertEqual(configure(self.settings)["db_path"], str(self.destination / "tasks.sqlite3")) + self.assertEqual(Store(self.destination / "tasks.sqlite3").snapshot("retained-work")["workload"]["manager"], "manager") + self.assertEqual((self.destination / repair.MARKER).read_bytes(), (self.source.parent / repair.MARKER).read_bytes()) + repair.rollback(journal, clients_stopped=True) + self.assertEqual(self.settings.read_bytes(), self.before) + self.assertTrue(self.source.is_file()) + self.assertTrue((self.destination / "tasks.sqlite3").is_file()) + + def test_two_profiles_switch_and_partial_switch_rolls_back(self): + second = self.root / "vscode.json" + installer.atomic_write(second, self.before) + original = repair.atomic_write + failed = False + def fail_second(path, content): + nonlocal failed + if path == second and not failed: + failed = True + raise OSError("interrupted second profile") + original(path, content) + with mock.patch.object(repair, "atomic_write", side_effect=fail_second): + with self.assertRaisesRegex(OSError, "second profile"): + repair.migrate(self.source, self.destination, [self.settings, second], clients_stopped=True) + self.assertEqual(self.settings.read_bytes(), self.before) + self.assertEqual(second.read_bytes(), self.before) + + def test_failed_settings_switch_restores_original(self): + original = repair.atomic_write + failed = False + def fail_once(path, content): + nonlocal failed + if path == self.settings and not failed: + failed = True + raise OSError("simulated interrupted settings switch") + original(path, content) + with mock.patch.object(repair, "atomic_write", side_effect=fail_once): + with self.assertRaisesRegex(OSError, "interrupted"): + self.migrate() + self.assertEqual(self.settings.read_bytes(), self.before) + self.assertTrue(self.source.is_file()) + + @unittest.skipUnless(os.name == "nt", "Windows package paths") + def test_real_redirected_candidate_is_migrated(self): + home = self.root / "profile" + logical = home / "AppData/Local/Conductor/tasks.sqlite3" + physical = home / "AppData/Local/Packages/Example.Desktop/LocalCache/Local/Conductor/tasks.sqlite3" + physical.parent.mkdir(parents=True) + installer.private_permissions(physical.parent) + self.source.replace(physical) + (self.source.parent / repair.MARKER).replace(physical.parent / repair.MARKER) + installer.atomic_write(self.settings, json.dumps({"mode": "local", "db_path": str(logical), + "state_root": str(logical.parent)}).encode()) + with mock.patch.object(Path, "home", return_value=home): + self.assertEqual(repair.source_candidates(logical), [physical]) + repair.migrate(physical, self.destination, [self.settings], clients_stopped=True) + self.assertTrue(physical.is_file()) + self.assertEqual(configure(self.settings)["db_path"], str(self.destination / "tasks.sqlite3")) + self.assertFalse(logical.exists()) + + @unittest.skipUnless(os.name == "nt", "Windows package paths") + def test_two_aliases_of_one_physical_ledger_are_not_ambiguous(self): + home = self.root / "profile" + logical = home / "AppData/Local/Conductor/tasks.sqlite3" + physical = home / "AppData/Local/Packages/Example.Desktop/LocalCache/Local/Conductor/tasks.sqlite3" + physical.parent.mkdir(parents=True) + logical.parent.mkdir(parents=True) + installer.private_permissions(physical.parent) + self.source.replace(physical) + (self.source.parent / repair.MARKER).replace(physical.parent / repair.MARKER) + logical.hardlink_to(physical) + installer.atomic_write(self.settings, json.dumps({"mode": "local", "db_path": str(logical), + "state_root": str(logical.parent)}).encode()) + with mock.patch.object(Path, "home", return_value=home): + self.assertEqual(len(repair.source_candidates(logical)), 1) + repair.migrate(physical, self.destination, [self.settings], clients_stopped=True) + self.assertTrue(logical.samefile(physical)) + self.assertTrue((self.destination / "tasks.sqlite3").is_file()) + + def test_requires_stopped_clients_and_unused_destination(self): + with self.assertRaisesRegex(ValueError, "Stop Desktop"): + repair.migrate(self.source, self.destination, [self.settings], clients_stopped=False) + self.destination.mkdir() + with self.assertRaisesRegex(ValueError, "must not exist"): + self.migrate() + self.assertEqual(self.settings.read_bytes(), self.before) + + def test_missing_source_and_ambiguous_source_never_create_destination(self): + with mock.patch.object(repair, "source_candidates", return_value=[self.source, self.root / "other.sqlite3"]): + with self.assertRaisesRegex(ValueError, "ambiguous"): + self.migrate() + self.source.unlink() + with self.assertRaises(ValueError): + self.migrate() + self.assertFalse(self.destination.exists()) + with self.assertRaisesRegex(ValueError, "no replacement database"): + configure(self.settings) + self.assertFalse(self.source.exists()) + + def test_installer_does_not_recreate_configured_missing_ledger(self): + self.source.unlink() + home = self.root / "home" + folder = home / "conductor-coordination" + folder.mkdir(parents=True) + installer.private_permissions(folder) + installer.atomic_write(folder / "client.json", self.before) + with self.assertRaisesRegex(ValueError, "no empty replacement"): + installer.install(codex_home=home, skills_dir=self.root / "skills", source_root=ROOT, + python=Path(sys.executable), mode="local", data_root=self.source.parent) + self.assertFalse(self.source.exists()) + + def test_rollback_preserves_later_settings_edits(self): + journal = self.migrate() + installer.atomic_write(self.settings, b'{"user_edit":true}') + with self.assertRaisesRegex(ValueError, "changed after repair"): + repair.rollback(journal, clients_stopped=True) + self.assertEqual(self.settings.read_bytes(), b'{"user_edit":true}') + + @unittest.skipUnless(os.name == "nt", "Windows paths") + def test_windows_default_and_appdata_rejection(self): + with mock.patch.dict(os.environ, {"LOCALAPPDATA": str(self.root / "redirected")}): + self.assertEqual(state_paths.local_state_root(), Path.home() / ".conductor") + with self.assertRaisesRegex(ValueError, "resolve differently"): + state_paths.validate_shared_windows_path(Path.home() / "AppData/Local/Conductor") + + def test_unix_default_unchanged(self): + from types import SimpleNamespace + with mock.patch.object(state_paths, "os", SimpleNamespace(name="posix", environ={"XDG_STATE_HOME": str(self.root)})): + self.assertEqual(state_paths.local_state_root(), self.root / "conductor")