From 4bf7386b8ac43d8c776938af98f4e158f7978df4 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Sun, 4 Oct 2026 22:21:49 -0700 Subject: [PATCH] clover: agent instructions, Linear claims, memory hooks, and managed JJ workspaces Assisted-by: gpt-6.1-sol --- modules/shared/default.nix | 1 + users/clover/agents/AGENTS.md | 26 +- users/clover/codex-config.toml | 11 +- users/clover/configuration.nix | 1 + users/clover/home.nix | 27 + users/clover/jujutsu.nix | 20 +- .../packages/agent-linear/agent-linear.js | 222 +++++ users/clover/packages/workspaces/README.md | 83 ++ .../packages/workspaces/configure-hooks.py | 103 ++ users/clover/packages/workspaces/default.nix | 27 + .../workspaces/extension/extension.js | 33 + .../workspaces/extension/package.json | 12 + users/clover/packages/workspaces/native.c | 39 + users/clover/packages/workspaces/shell.zsh | 15 + .../workspaces/workspaces-hooks.test.mjs | 144 +++ .../clover/packages/workspaces/workspaces.mjs | 880 ++++++++++++++++++ .../packages/workspaces/workspaces.test.mjs | 634 +++++++++++++ users/clover/work/home.nix | 52 +- 18 files changed, 2319 insertions(+), 11 deletions(-) create mode 100644 users/clover/packages/workspaces/README.md create mode 100644 users/clover/packages/workspaces/configure-hooks.py create mode 100644 users/clover/packages/workspaces/default.nix create mode 100644 users/clover/packages/workspaces/extension/extension.js create mode 100644 users/clover/packages/workspaces/extension/package.json create mode 100644 users/clover/packages/workspaces/native.c create mode 100644 users/clover/packages/workspaces/shell.zsh create mode 100644 users/clover/packages/workspaces/workspaces-hooks.test.mjs create mode 100644 users/clover/packages/workspaces/workspaces.mjs create mode 100644 users/clover/packages/workspaces/workspaces.test.mjs diff --git a/modules/shared/default.nix b/modules/shared/default.nix index 6ec64f5daff596e2d2dea6d5793a13bddd73ea99..ffcd4a3058cb16280ecde8293075f3797e5373dd 100644 --- a/modules/shared/default.nix +++ b/modules/shared/default.nix @@ -10,6 +10,7 @@ # install neovim globally, but let home-manager install # the per-user configured one. environment.systemPackages = [ + pkgs.ncdu pkgs.neovim ]; } diff --git a/users/clover/agents/AGENTS.md b/users/clover/agents/AGENTS.md index 178a4b622a536d801e72130b7db8942e9fb9df5a..0669ede99b3c1619554ba74056c58db724d20a6d 100644 --- a/users/clover/agents/AGENTS.md +++ b/users/clover/agents/AGENTS.md @@ -22,11 +22,22 @@ should be derived, not stored. A field written only to appease a type and never read gets nuked. A cast must constrain real runtime behavior; one that survives is localized and justified by an actual API boundary. +Before adding compatibility code, a backfill, or a migration fallback, establish +whether the affected data or contract has shipped. An open or draft PR alone is +not evidence of deployment. Treat the user's statement that it is unshipped as +authoritative. For disposable local state, prefer resetting the local database +and implementing the final shape directly. Preserve deployed contracts; do not +build transition paths for hypothetical consumers or local test data. + Before calling a change done, do a cleanup pass: fewer names, branches, casts, public API promises. Ship the smaller locked-in version. Thinking, running, and verifying longer to land a correct answer is expected -- "should work" is a hypothesis, and say so. +Fewer moving parts never means fewer lines. Separate a function's steps with +blank lines, and name a deeply nested expression in a `const` above its use. +Code will be read and should be kept tidy for human and other agentic reviewers. + ### Comments Default to no comment; names, types, and structure carry the meaning. One earns @@ -150,14 +161,15 @@ regret missing, never mere emphasis. Usually zero per message, never two. 4. Solve merge conflicts if any arise following "solve all merge conflicts" rules. - When I say "describe" or "tag" a change, i mean to set it's description with - `jj desc -m '...'` -- When I say to "open the PR", i mean to make sure this change is up to date - with main (with a `jj git fetch`), ensure a description exists, and then use - `jj git push -c @` to push the change. then run `open` on the resulting PR if - the push command gave one. + `jj desc -m '...'`. Lean on doing this by default. Before a task, feel free to + tag the change with a `WIP: feat: ...` or something alike, or if unsure a + general topic line that evolves as you work `WIP: tuning search engine`. - Never add `Co-authored-by: ` trailer, always add `Assisted-by: ` - in this format `claude-opus-4.8` / `gpt-5.6-sol` / `qwen-3.8-27b` / etc. If - you think you are `gpt-5`, the variant is probably `gpt-5.6-sol`. + in this format `claude-opus-5.5` / `gpt-6.1-sol` / `glm-5.3` / etc. If you + think you are `gpt-6`, the variant is likely `gpt-6.1-sol`. When multiple + models collaborate, be sure to credit all of them. +- If the prompt mentions "ChatGPT Dot" or a task referred from a "Lilac" agent, + add `Assisted-by: chatgpt-dot:lilac` as the last credit. ## Personal diff --git a/users/clover/codex-config.toml b/users/clover/codex-config.toml index 7b3ee54d3d5449081b2ce2d521ee886825f3c101..d612b956f19b3481979ac6b7ed4877d06cdab3ad 100644 --- a/users/clover/codex-config.toml +++ b/users/clover/codex-config.toml @@ -99,7 +99,7 @@ matcher = "startup|resume|clear|compact" [[hooks.SessionStart.hooks]] type = "command" -command = "python3 ~/config/users/clover/agents/codex-memory-hook.py" +command = "codex-memory" timeout = 5 additionalContextLimit = 20000 @@ -107,10 +107,17 @@ additionalContextLimit = 20000 [[hooks.SubagentStart.hooks]] type = "command" -command = "python3 ~/config/users/clover/agents/codex-memory-hook.py" +command = "codex-memory" timeout = 5 additionalContextLimit = 20000 +[[hooks.UserPromptSubmit]] + +[[hooks.UserPromptSubmit.hooks]] +type = "command" +command = "agent-linear prompt" +timeout = 60 + [tui] show_tooltips = false status_line = ["model", "reasoning", "run-state", "weekly-limit", "used-tokens"] diff --git a/users/clover/configuration.nix b/users/clover/configuration.nix index c1dcecfaddd28d0dbcf5d7b185db10942b5f0f79..3384bff6d356cfeef22932daff815a05ac12d8ff 100644 --- a/users/clover/configuration.nix +++ b/users/clover/configuration.nix @@ -55,6 +55,7 @@ in InitialKeyRepeat = 10; }; dock = { + autohide-delay = 0.2; show-recents = false; expose-group-apps = false; }; diff --git a/users/clover/home.nix b/users/clover/home.nix index 7e74c061fb96604570955eee454264ded345eab0..549c234a79f5c8cb59712a6bdd9b52b346285f9a 100644 --- a/users/clover/home.nix +++ b/users/clover/home.nix @@ -35,6 +35,9 @@ in pnpm nodejs_26 (callPackage ./packages/agent-linear { }) + (writeShellScriptBin "codex-memory" '' + exec ${pkgs.python3}/bin/python3 ${./agents/codex-memory-hook.py} + '') # `open` shim that routes URLs into the VS Code Simple Browser; see # the home.file symlink for the companion extension. (callPackage ./packages/open-in-vscode-browser { }) @@ -110,6 +113,30 @@ in fi ''; + home.activation.claudeLinearHook = lib.hm.dag.entryAfter [ "writeBoundary" ] '' + run ${pkgs.nodejs}/bin/node <<'NODE' + const fs = require("node:fs"); + const path = require("node:path"); + const file = ${builtins.toJSON "${config.home.homeDirectory}/.claude/settings.json"}; + const settings = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, "utf8")) : {}; + const hook = { type: "command", command: "agent-linear prompt", timeout: 60 }; + const groups = (settings.hooks ??= {}).UserPromptSubmit ??= []; + const existing = groups.flatMap(group => group.hooks).find(item => item.command === hook.command); + if (existing) { + Object.assign(existing, hook); + } else { + groups.push({ hooks: [hook] }); + } + const contents = JSON.stringify(settings, null, 2) + "\n"; + if (!fs.existsSync(file) || fs.readFileSync(file, "utf8") !== contents) { + fs.mkdirSync(path.dirname(file), { recursive: true }); + const temporary = file + ".linear-hook.tmp"; + fs.writeFileSync(temporary, contents, { mode: fs.existsSync(file) ? fs.statSync(file).mode & 0o777 : 0o600 }); + fs.renameSync(temporary, file); + } + NODE + ''; + # Referenced by the `statusLine.command` entry in ~/.claude/settings.json, # which claude code rewrites itself and so stays unmanaged. home.file.".claude/statusline-command.sh".source = diff --git a/users/clover/jujutsu.nix b/users/clover/jujutsu.nix index 7b78bb73fc956cbbbf767bc6347514bd91732933..5864c7085753d6e540433ba8f04b484ac78f2b1d 100644 --- a/users/clover/jujutsu.nix +++ b/users/clover/jujutsu.nix @@ -1,7 +1,25 @@ -{ lib, ... }: +{ lib, pkgs, ... }: { programs.jujutsu = { enable = true; + package = pkgs.symlinkJoin { + name = "jujutsu-linear"; + paths = [ pkgs.jujutsu ]; + inherit (pkgs.jujutsu) meta version; + postBuild = '' + rm "$out/bin/jj" + cp ${pkgs.writeShellScript "jj" '' + for arg in "$@"; do + case "$arg" in + desc|describe) + exec ${pkgs.nodejs}/bin/node ${./packages/agent-linear/agent-linear.js} jj ${pkgs.jujutsu}/bin/jj "$@" + ;; + esac + done + exec ${pkgs.jujutsu}/bin/jj "$@" + ''} "$out/bin/jj" + ''; + }; # my workflow is based on creating changes and editing them. while they # are in progress the description starts with "WIP: " settings = rec { diff --git a/users/clover/packages/agent-linear/agent-linear.js b/users/clover/packages/agent-linear/agent-linear.js index 2462fa69767608340db11f5ca6a21c1486f5c0ec..ddbcd0a708d26ab649ed61bcc0ac75ab0ba6f37d 100644 --- a/users/clover/packages/agent-linear/agent-linear.js +++ b/users/clover/packages/agent-linear/agent-linear.js @@ -111,11 +111,232 @@ async function main() { const options = parseIssueArgs(argv); assertWorkRepo(); await showIssue(options); + } else if (command === "jj") { + await runJj(argv); + } else if (command === "prompt") { + const event = JSON.parse(fs.readFileSync(0, "utf8")); + if (event.hook_event_name !== "UserPromptSubmit" || event.agent_id) { + return; + } + const messages = await startIssues(event.prompt, true); + if (messages.length > 0) { + writeJson({ + hookSpecificOutput: { + hookEventName: "UserPromptSubmit", + additionalContext: messages.join("\n"), + }, + }); + } } else { throw new Error(`unknown command ${JSON.stringify(command)}`); } } +async function runJj([executable, ...argv]) { + const context = []; + const operation = []; + const revisions = []; + let command; + let positional = false; + + for (let i = 0; i < argv.length; i++) { + const arg = argv[i]; + const option = arg.split("=", 1)[0]; + if (positional) { + revisions.push(arg); + continue; + } + if (arg === "--") { + positional = true; + continue; + } + if ( + [ + "-R", + "--repository", + "--config", + "--config-file", + "--color", + "--at-op", + "--at-operation", + ].includes(option) + ) { + const value = arg.includes("=") ? [] : [argv[++i]]; + if ( + ["-R", "--repository", "--config", "--config-file"].includes(option) + ) { + context.push(arg, ...value); + } else if (["--at-op", "--at-operation"].includes(option)) { + operation.push(arg, ...value); + } + } else if (arg.startsWith("-R") && arg.length > 2) { + context.push(arg); + } else if (arg === "-m" || arg === "--message") { + i++; + } else if (arg.startsWith("-r") && arg.length > 2) { + revisions.push(arg.slice(2).replace(/^=/, "")); + } else if (!arg.startsWith("-")) { + if (command) { + revisions.push(arg); + } else { + command = arg; + } + } + } + + const describe = ["desc", "describe"].includes(command) && + !argv.some((arg) => + ["-h", "--help", "--no-integrate-operation"].includes(arg) + ); + + function readJj(args) { + const result = child_process.spawnSync(executable, [ + ...context, + "--ignore-working-copy", + "--no-pager", + "--color=never", + ...args, + ], { encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }); + if (result.error || result.status !== 0) { + throw result.error ?? new Error(result.stderr.trim()); + } + return result.stdout.trim(); + } + + let changes; + let hookError; + if (describe) { + try { + const revset = revisions.length > 0 + ? revisions.map((revision) => `(${revision.replace(/^all:/, "")})`) + .join(" | ") + : "@"; + changes = readJj([ + ...operation, + "log", + "--no-graph", + "-r", + revset, + "-T", + 'change_id ++ "\\n"', + ]) + .split("\n").filter(Boolean); + } catch (error) { + hookError = error; + } + } + + const result = child_process.spawnSync(executable, argv, { + stdio: "inherit", + }); + if (result.error) { + throw result.error; + } + if (result.signal) { + process.kill(process.pid, result.signal); + return; + } + if (result.status !== 0 || !describe) { + process.exitCode = result.status; + return; + } + + try { + if (hookError) { + throw hookError; + } + if (changes.length === 0) { + return; + } + + const revset = changes.map((id) => `change_id("${id}")`).join(" | "); + const descriptions = readJj([ + "log", + "--no-graph", + "-r", + revset, + "-T", + "description", + ]); + for (const message of await startIssues(descriptions)) { + console.error(message); + } + } catch (error) { + console.error(`linear: could not read the description: ${error.message}`); + } +} + +async function startIssues(text, onlyUnclaimed = false) { + const issues = new Set( + [...text.matchAll( + /(? match[0].toUpperCase()), + ); + + const messages = []; + let viewer; + + for (const id of issues) { + try { + const data = await linearRequest( + ` + query IssueProgress($id: String!) { + issue(id: $id) { + id + state { name } + assignee { id } + team { states { nodes { id name type } } } + } + } + `, + { id }, + ); + if (!data.issue) { + throw new Error(`${id} not found`); + } + if ( + !/^(todo|backlog)$/i.test(data.issue.state?.name ?? "") || + (onlyUnclaimed && data.issue.assignee) + ) { + continue; + } + + const state = data.issue.team.states.nodes.find((state) => + state.name.toLowerCase() === "in progress" && state.type === "started" + ); + if (!state) { + throw new Error(`${id}'s team has no In Progress state`); + } + + const input = { stateId: state.id }; + if (!data.issue.assignee) { + viewer ??= (await linearRequest("{ viewer { id name } }", {})).viewer; + input.assigneeId = viewer.id; + } + + const updated = await linearRequest( + ` + mutation StartIssue($id: String!, $input: IssueUpdateInput!) { + issueUpdate(id: $id, input: $input) { success } + } + `, + { id: data.issue.id, input }, + ); + if (!updated.issueUpdate.success) { + throw new Error(`${id} was not updated`); + } + const assignment = input.assigneeId ? `, assigned to ${viewer.name}` : ""; + messages.push(`linear: ${id} → In Progress${assignment}`); + } catch (error) { + messages.push( + `linear: could not start ${id}: ${error.message}. Send the issue again or run jj desc to retry.`, + ); + } + } + return messages; +} + async function search(options) { const found = new Map(); @@ -192,6 +413,7 @@ async function linearRequest(query, variables) { "Content-Type": "application/json", }, body: JSON.stringify({ query, variables }), + signal: AbortSignal.timeout(10000), }); const body = await response.text(); diff --git a/users/clover/packages/workspaces/README.md b/users/clover/packages/workspaces/README.md new file mode 100644 index 0000000000000000000000000000000000000000..0b7627cf3bce82a2991883b7f4e7ce9e05254df1 --- /dev/null +++ b/users/clover/packages/workspaces/README.md @@ -0,0 +1,83 @@ +# Workspaces + +Requires macOS/APFS and T3 Code nightly with orchestrator V2. The source repo is +configured locally in `~/.config/workspaces.json`. + +```sh +ws setup /path/to/source-workspace +ws new # next available number, freshly fetched origin/main +ws new search -r some-change # ~/workspaces/search, with a fresh T3 chat +ws open search # reopen and renew the chat's claim +ws deps search # reconcile dependencies after changing revisions +ws gc --dry-run # show blockers; update grace observations +``` + +Raycast's **New Workspace** command runs `ws new --background`. Its managed script lives in +`~/.config/raycast/scripts`; register that folder in Raycast's Script Commands +settings once. Each workspace has its own T3 project, named after the source +project followed by ` | `, with the same logo. Additional chats +in that project use the same workspace folder. + +`ws new` and `ws open` accept `--no-open` to bind the chat without navigating +the desktop app. Standard output is the workspace path; setup progress goes to +standard error. The chat opens in an empty folder before fetching or creating +the JJ workspace; `--background` returns without waiting for preparation. +Claude and Codex startup and prompt hooks wait for the workspace to be ready. +Setup failures block the prompt until `ws deps ` succeeds. +New chats can be opened while another workspace prepares; preparation and +cleanup share a separate lock. Local T3 authentication is cached for one +minute with a two-minute token, and renewed when the server restarts. +Background preparation writes `~/Library/Logs/workspace-.log`. A failed +setup stays protected: run `ws deps ` and `ws open ` to finish it. + +```text +reserve empty folder → create and open T3 thread + → fetch origin/main → add JJ workspace → checkout → clone snapshot → pnpm install + → release waiting Claude/Codex hooks + +Every 30 seconds: + unfinished setup / unsettled T3 chat / running work / VS Code / live shell cwd + → keep workspace, reset grace + no references for ≥60 seconds, with a fresh check immediately before removal + → JJ snapshot → JJ workspace forget → remove directory + unavailable API, untracked VS Code window, unreadable cwd, unexpected state + → keep workspaces, reset grace +``` + +Only registered workspaces under `~/workspaces` are collected. JJ retains +snapshotted changes; ignored files are deleted with the directory. Closing T3 +pauses cleanup. VS Code's companion extension reports live folders and documents +through a local socket; windows without a responding tracker block cleanup. +Shell hooks register workspace shells, and cleanup reads their live cwd, +including terminals opened before the hooks were installed. + +Default creation fetches only `main` from `origin` and starts at `main@origin`. +An explicit `-r` skips the fetch. Source checkout files remain untouched, even +when stale. Managed checkouts update automatically; fetching does not abandon +unrelated commits in the shared repo. + +The dependency key includes package manifests, the lockfile, pnpm workspace +configuration, patches, pnpm version, platform, architecture, and Node ABI. +`clonefile` shares file data without sharing writable inodes. pnpm then +regenerates launchers containing absolute paths. One cached snapshot is kept; +changing the dependency key replaces it. + +Build and verify against a downloaded nightly CLI: + +```sh +cc -Wall -Wextra -Werror native.c -o /tmp/workspace-native +T3_TEST_CLI=/path/to/nightly/t3 WORKSPACE_TEST_NATIVE=/tmp/workspace-native \ + node --test --test-isolation=none workspaces.test.mjs +``` + +The lifecycle test starts an isolated nightly server, uses real JJ and pnpm, +checks clone isolation and launcher relocation, and verifies preservation of JJ +file contents after cleanup. VS Code process status is a fixture; its reference +socket and the terminal cwd probe are live. The lock test verifies overlap +rejection and release after process exit. + +`workspaces-hooks.test.mjs` tests the installed Claude and Codex CLIs against +a local API stub. Set `WORKSPACE_TEST_CLAUDE`, `WORKSPACE_TEST_CODEX`, and +`WORKSPACE_TEST_HOOK_PYTHON` (Python with tomlkit). It verifies that early prompts +wait, setup failures prevent model requests, and instructions created during +preparation reach the first model request. It makes no external model calls. diff --git a/users/clover/packages/workspaces/configure-hooks.py b/users/clover/packages/workspaces/configure-hooks.py new file mode 100644 index 0000000000000000000000000000000000000000..5dea8c9c692e98dee9337e25e8a47725c9ddff65 --- /dev/null +++ b/users/clover/packages/workspaces/configure-hooks.py @@ -0,0 +1,103 @@ +import json +import os +from pathlib import Path +import select +import subprocess +import sys + +import tomlkit + +home = Path(sys.argv[1]) +command = sys.argv[2] +codex = sys.argv[3] + + +def write(file, contents): + if file.exists() and file.read_text() == contents: + return + file.parent.mkdir(parents=True, exist_ok=True) + temporary = file.with_name(file.name + ".workspace-hook.tmp") + temporary.write_text(contents) + os.chmod(temporary, file.stat().st_mode & 0o777 if file.exists() else 0o600) + os.replace(temporary, file) + + +for provider, filename in [("claude", "settings.json"), ("codex", "config.toml")]: + file = home / ("." + provider) / filename + if provider == "claude": + settings = json.loads(file.read_text()) if file.exists() else {} + else: + settings = tomlkit.parse(file.read_text()) if file.exists() else tomlkit.document() + + if "hooks" not in settings: + settings["hooks"] = {} + for event in ["SessionStart", "UserPromptSubmit"]: + hook = { + "type": "command", "command": command, "timeout": 300, + "statusMessage": "Preparing workspace", "async": False, + } + if provider == "claude": + hook["onFailure"] = "block" + groups = settings["hooks"].get(event) + if groups is None: + settings["hooks"][event] = [{"hooks": [hook]}] + else: + existing = next((item for group in groups for item in group["hooks"] + if item.get("command") == command), None) + if existing is None: + groups.append({"hooks": [hook]}) + else: + existing.update(hook) + + contents = json.dumps(settings, indent=2) + "\n" if provider == "claude" else tomlkit.dumps(settings) + write(file, contents) + + if provider != "codex": + continue + + with subprocess.Popen([codex, "app-server", "--stdio"], stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=0, + env={**os.environ, "CODEX_HOME": str(file.parent)}) as server: + try: + def request(request_id, method, params): + message = {"id": request_id, "method": method, "params": params} + server.stdin.write((json.dumps(message) + "\n").encode()) + while select.select([server.stdout], [], [], 15)[0]: + line = server.stdout.readline() + if not line: + break + response = json.loads(line) + if response.get("id") != request_id: + continue + if "error" in response: + raise RuntimeError(response["error"]) + return response["result"] + raise RuntimeError("Codex did not confirm workspace hook configuration") + + request(1, "initialize", { + "clientInfo": {"name": "workspace-hook-installer", "version": "1"}, + "capabilities": {"experimentalApi": True}, + }) + server.stdin.write(b'{"method":"initialized"}\n') + listed = request(2, "hooks/list", {"cwds": [str(home)]}) + hooks = [hook for entry in listed["data"] for hook in entry["hooks"] + if hook.get("command") == command + and hook["eventName"] in ["sessionStart", "userPromptSubmit"] + and hook["sourcePath"] == str(file)] + if len(hooks) != 2: + raise RuntimeError("Codex did not load both workspace readiness hooks") + + settings = tomlkit.parse(file.read_text()) + if "state" not in settings["hooks"]: + settings["hooks"]["state"] = {} + for hook in hooks: + settings["hooks"]["state"][hook["key"]] = { + "trusted_hash": hook["currentHash"], "enabled": True, + } + write(file, tomlkit.dumps(settings)) + finally: + server.terminate() + try: + server.wait(timeout=5) + except subprocess.TimeoutExpired: + server.kill() diff --git a/users/clover/packages/workspaces/default.nix b/users/clover/packages/workspaces/default.nix new file mode 100644 index 0000000000000000000000000000000000000000..9b146d3caf1e16f35d57316c4914a3b22c0df9e8 --- /dev/null +++ b/users/clover/packages/workspaces/default.nix @@ -0,0 +1,27 @@ +{ pkgs, ... }: +let + native = pkgs.runCommandCC "workspace-native" { } '' + mkdir -p $out/bin + $CC ${./native.c} -o $out/bin/workspace-native + ''; +in +pkgs.writeShellApplication { + name = "ws"; + runtimeInputs = with pkgs; [ + nodejs_26 + jujutsu + pnpm_10 + native + ]; + text = '' + if [[ "''${1-}" == ready-hook ]]; then + exec node ${./workspaces.mjs} "$@" + fi + mkdir -p "$HOME/.local/state/workspaces" + workspace_lock="$HOME/.local/state/workspaces/lock" + if [[ "''${1-}" == new ]]; then + workspace_lock="$HOME/.local/state/workspaces/creation-lock" + fi + exec workspace-native lock "$workspace_lock" node ${./workspaces.mjs} "$@" + ''; +} diff --git a/users/clover/packages/workspaces/extension/extension.js b/users/clover/packages/workspaces/extension/extension.js new file mode 100644 index 0000000000000000000000000000000000000000..c0612719807754d8217916dc92475b0fbd31e863 --- /dev/null +++ b/users/clover/packages/workspaces/extension/extension.js @@ -0,0 +1,33 @@ +const vscode = require("vscode"); +const fs = require("node:fs"); +const net = require("node:net"); +const os = require("node:os"); +const path = require("node:path"); + +function activate(context) { + const directory = path.join(os.homedir(), ".local/state/workspaces/vscode"); + fs.mkdirSync(directory, { recursive: true, mode: 0o700 }); + const socket = path.join(directory, `${process.pid}.sock`); + fs.rmSync(socket, { force: true }); + + const server = net.createServer((client) => { + const uris = [ + ...(vscode.workspace.workspaceFolders ?? []).map((folder) => folder.uri), + ...vscode.workspace.textDocuments.map((document) => document.uri), + vscode.workspace.workspaceFile, + ]; + const paths = uris.filter((uri) => uri?.scheme === "file").map((uri) => + uri.fsPath + ); + client.end(JSON.stringify({ pid: process.pid, paths }) + "\n"); + }); + server.listen(socket); + context.subscriptions.push({ + dispose() { + server.close(); + fs.rmSync(socket, { force: true }); + }, + }); +} + +module.exports = { activate }; diff --git a/users/clover/packages/workspaces/extension/package.json b/users/clover/packages/workspaces/extension/package.json new file mode 100644 index 0000000000000000000000000000000000000000..596d83076f397c9817c4975e9e57df775e8157ec --- /dev/null +++ b/users/clover/packages/workspaces/extension/package.json @@ -0,0 +1,12 @@ +{ + "name": "workspace-references", + "displayName": "Workspace References", + "description": "Keeps open VS Code folders safe from workspace cleanup.", + "publisher": "clover", + "version": "0.0.1", + "engines": { "vscode": "^1.74.0" }, + "main": "./extension.js", + "extensionKind": ["ui"], + "activationEvents": ["*"], + "capabilities": { "untrustedWorkspaces": { "supported": true } } +} diff --git a/users/clover/packages/workspaces/native.c b/users/clover/packages/workspaces/native.c new file mode 100644 index 0000000000000000000000000000000000000000..3722533a4de0fc78379ca84349b69bb873c57214 --- /dev/null +++ b/users/clover/packages/workspaces/native.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + if (argc >= 4 && (strcmp(argv[1], "lock") == 0 || + strcmp(argv[1], "wait-lock") == 0)) { + int fd = open(argv[2], O_CREAT | O_RDWR, 0600); + if (fd < 0) { + perror("Cannot open workspace lock"); + return 1; + } + int flags = LOCK_EX | (strcmp(argv[1], "lock") == 0 ? LOCK_NB : 0); + if (flock(fd, flags) < 0) { + if (errno != EWOULDBLOCK) { + perror("Cannot lock workspaces"); + return 1; + } + fprintf( + stderr, + "Another workspace command is running. Retry when it finishes.\n"); + return 1; + } + execvp(argv[3], argv + 3); + perror("Cannot start workspace command"); + return 1; + } + if (argc != 4 || strcmp(argv[1], "clone") != 0) + return 2; + if (clonefile(argv[2], argv[3], + CLONE_NOFOLLOW | CLONE_NOOWNERCOPY | CLONE_ACL) == 0) + return 0; + perror("Cannot clone dependencies"); + return 1; +} diff --git a/users/clover/packages/workspaces/shell.zsh b/users/clover/packages/workspaces/shell.zsh new file mode 100644 index 0000000000000000000000000000000000000000..77fba91b1986bc15a7764a8311ed5df02a51e542 --- /dev/null +++ b/users/clover/packages/workspaces/shell.zsh @@ -0,0 +1,15 @@ +function _workspace_reference() { + local directory="$HOME/.local/state/workspaces/shells" + if [[ "$PWD" == "$HOME/workspaces/"* ]]; then + mkdir -p "$directory" + print -r -- "$PWD" > "$directory/$$" + else + [[ ! -e "$directory/$$" ]] || rm -f "$directory/$$" + fi +} + +autoload -Uz add-zsh-hook +add-zsh-hook chpwd _workspace_reference +add-zsh-hook precmd _workspace_reference +add-zsh-hook preexec _workspace_reference +_workspace_reference diff --git a/users/clover/packages/workspaces/workspaces-hooks.test.mjs b/users/clover/packages/workspaces/workspaces-hooks.test.mjs new file mode 100644 index 0000000000000000000000000000000000000000..bcc4112217014035b2c6f646d8dca3e235701127 --- /dev/null +++ b/users/clover/packages/workspaces/workspaces-hooks.test.mjs @@ -0,0 +1,144 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import fs from "node:fs"; +import path from "node:path"; +import http from "node:http"; +import childProcess from "node:child_process"; +import { once } from "node:events"; +import { fileURLToPath } from "node:url"; + +const script = fileURLToPath(new URL("./workspaces.mjs", import.meta.url)); +const installer = fileURLToPath(new URL("./configure-hooks.py", import.meta.url)); +const python = process.env.WORKSPACE_TEST_HOOK_PYTHON; + +for (const provider of ["codex", "claude"]) { + const executable = process.env[`WORKSPACE_TEST_${provider.toUpperCase()}`]; + test(`${provider} holds a real prompt until preparation, including repository instructions`, { + skip: !executable || !python, + timeout: 45_000, + }, async () => { + const home = fs.realpathSync(fs.mkdtempSync("/tmp/workspaces-hooks-")); + const workspace = path.join(home, "workspaces/pending"); + const record = path.join(home, ".local/state/workspaces/workspaces/pending.json"); + fs.mkdirSync(workspace, { recursive: true }); + fs.mkdirSync(path.dirname(record), { recursive: true }); + fs.writeFileSync(record, JSON.stringify({ prepared: false })); + + const invoked = path.join(home, "hook-invoked"); + const wrapper = path.join(home, "hook.mjs"); + fs.writeFileSync(wrapper, `import fs from "node:fs"; +import childProcess from "node:child_process"; +fs.writeFileSync(${JSON.stringify(invoked)}, "started"); +const child = childProcess.spawn(process.execPath, [${JSON.stringify(script)}, "ready-hook"], { stdio: "inherit" }); +child.on("exit", (code) => { process.exitCode = code ?? 1; }); +`); + const command = [process.execPath, wrapper].map((value) => + "'" + value.replaceAll("'", "'\\''") + "'" + ).join(" "); + const hook = { type: "command", command, timeout: 30 }; + const hooks = { + SessionStart: [{ hooks: [hook] }], + UserPromptSubmit: [{ hooks: [hook] }], + }; + const requests = []; + const server = http.createServer(async (request, response) => { + let body = ""; + for await (const chunk of request) body += chunk; + if (request.method === "POST" && /\/(responses|messages)(\?|$)/.test(request.url)) { + requests.push(body); + } + response.writeHead(401, { "Content-Type": "application/json" }); + response.end(JSON.stringify({ error: { type: "authentication_error", message: "Fixture finished" } })); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const origin = `http://127.0.0.1:${server.address().port}`; + const codexHome = path.join(home, ".codex"); + const claudeHome = path.join(home, ".claude"); + fs.mkdirSync(codexHome); + fs.mkdirSync(claudeHome); + fs.writeFileSync(path.join(codexHome, "config.toml"), `model_provider = "fixture" +model = "gpt-6.1-sol" +[model_providers.fixture] +name = "Fixture" +base_url = "${origin}/v1" +wire_api = "responses" +requires_openai_auth = false +request_max_retries = 0 +stream_max_retries = 0 +[[hooks.SessionStart]] +[[hooks.SessionStart.hooks]] +type = "command" +command = ${JSON.stringify(command)} +timeout = 30 +[[hooks.UserPromptSubmit]] +[[hooks.UserPromptSubmit.hooks]] +type = "command" +command = ${JSON.stringify(command)} +timeout = 30 +`); + fs.writeFileSync(path.join(claudeHome, "settings.json"), JSON.stringify({ hooks })); + const env = { + ...process.env, HOME: home, CODEX_HOME: codexHome, CLAUDE_CONFIG_DIR: claudeHome, + ANTHROPIC_BASE_URL: origin, ANTHROPIC_API_KEY: "fixture", + ANTHROPIC_AUTH_TOKEN: "", CLAUDE_CODE_OAUTH_TOKEN: "", + CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", DISABLE_TELEMETRY: "1", + }; + const args = provider === "codex" + ? ["exec", "--json", "--sandbox", "read-only", "--skip-git-repo-check", "Say ready."] + : ["-p", "--model", "claude-sonnet-4-6", "--output-format", "json", "Say ready."]; + let child; + let output = ""; + try { + const configure = childProcess.spawn(python, [ + installer, home, command, process.env.WORKSPACE_TEST_CODEX, + ], { env }); + let configuredOutput = ""; + configure.stderr.on("data", (chunk) => { configuredOutput += chunk; }); + const [configuredStatus] = await once(configure, "exit"); + assert.equal(configuredStatus, 0, configuredOutput); + child = childProcess.spawn(executable, args, { cwd: workspace, env }); + child.stdin.end(); + child.stdout.on("data", (chunk) => { output += chunk; }); + child.stderr.on("data", (chunk) => { output += chunk; }); + const exited = once(child, "exit"); + for (let attempt = 0; !fs.existsSync(invoked); attempt++) { + assert.ok(attempt < 200 && child.exitCode === null, output); + await new Promise((resolve) => setTimeout(resolve, 50)); + } + await new Promise((resolve) => setTimeout(resolve, 400)); + assert.equal(requests.length, 0, "no model request while the workspace is unprepared"); + assert.equal(child.exitCode, null, "prompt remains waiting"); + fs.writeFileSync(path.join(workspace, "AGENTS.md"), "WORKSPACE_READY_INSTRUCTIONS_8732\n"); + fs.writeFileSync(record, JSON.stringify({ prepared: true })); + for (let attempt = 0; requests.length === 0; attempt++) { + assert.ok(attempt < 300 && child.exitCode === null, output); + await new Promise((resolve) => setTimeout(resolve, 50)); + } + assert.match(requests[0], /WORKSPACE_READY_INSTRUCTIONS_8732/); + child.kill("SIGKILL"); + await exited; + + fs.rmSync(invoked); + requests.length = 0; + fs.writeFileSync(record, JSON.stringify({ prepared: false, preparationError: "fixture setup failed" })); + output = ""; + child = childProcess.spawn(executable, args, { cwd: workspace, env }); + child.stdin.end(); + child.stdout.on("data", (chunk) => { output += chunk; }); + child.stderr.on("data", (chunk) => { output += chunk; }); + await once(child, "exit"); + assert.ok(fs.existsSync(invoked), output); + assert.equal(requests.length, 0, "a setup failure blocks the model request"); + if (provider === "codex") assert.match(output, /"input_tokens":0/); + else assert.match(output, /setup stopped|resend your prompt/); + } finally { + if (child?.exitCode === null && child.signalCode === null) { + child.kill("SIGKILL"); + await once(child, "exit"); + } + await new Promise((resolve) => server.close(resolve)); + fs.rmSync(home, { recursive: true, force: true, maxRetries: 3 }); + } + }); +} diff --git a/users/clover/packages/workspaces/workspaces.mjs b/users/clover/packages/workspaces/workspaces.mjs new file mode 100644 index 0000000000000000000000000000000000000000..3b9280477ea43140b93d7b272f9dfd6c8bda2431 --- /dev/null +++ b/users/clover/packages/workspaces/workspaces.mjs @@ -0,0 +1,880 @@ +import fs from "node:fs"; +import path from "node:path"; +import os from "node:os"; +import net from "node:net"; +import { createHash, randomUUID } from "node:crypto"; +import childProcess from "node:child_process"; +import { fileURLToPath } from "node:url"; + +const home = fs.realpathSync(os.homedir()); +const root = path.join(home, "workspaces"); +const state = path.join(home, ".local/state/workspaces"); +const cache = path.join(home, ".cache/workspaces/deps"); +const configFile = path.join(home, ".config/workspaces.json"); +const records = path.join(state, "workspaces"); +const t3Home = process.env.T3CODE_HOME ?? path.join(home, ".t3"); + +function run(command, args, cwd, inherit = false) { + if (command === "jj") { + args = ["--config", "snapshot.auto-update-stale=true", ...args]; + } + + return childProcess.execFileSync(command, args, { + cwd, + encoding: "utf8", + stdio: inherit ? ["ignore", 2, 2] : ["ignore", "pipe", "pipe"], + timeout: inherit ? undefined : 30_000, + })?.trim(); +} + +function writeJson(file, value) { + fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 }); + const temporary = `${file}.${process.pid}.tmp`; + fs.writeFileSync(temporary, JSON.stringify(value) + "\n", { mode: 0o600 }); + fs.renameSync(temporary, file); +} + +function alive(pid) { + try { + process.kill(pid, 0); + return true; + } catch (error) { + if (error.code === "ESRCH") return false; + throw error; + } +} + +function inside(candidate, directory) { + const relative = path.relative(directory, candidate); + return relative === "" || + (!relative.startsWith(`..${path.sep}`) && relative !== ".." && + !path.isAbsolute(relative)); +} + +function canonical(file) { + try { + return fs.realpathSync(file); + } catch (error) { + if (error.code === "ENOENT") return path.resolve(file); + throw error; + } +} + +function jjRepository(directory) { + const file = path.join(directory, ".jj/repo"); + return fs.statSync(file).isDirectory() ? canonical(file) : canonical( + path.resolve(path.dirname(file), fs.readFileSync(file, "utf8").trim()), + ); +} + +function workspace(id) { + if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/.test(id)) { + throw new Error( + "Choose a workspace name containing letters, numbers, underscores, or hyphens.", + ); + } + return path.join(root, id); +} + +export async function t3() { + const runtime = JSON.parse( + fs.readFileSync(path.join(t3Home, "userdata/server-runtime.json")), + ); + if (!Number.isInteger(runtime.pid) || !alive(runtime.pid)) { + throw new Error("Start T3 Code nightly before using workspaces."); + } + const origin = new URL(runtime.origin); + if ( + origin.protocol !== "http:" || + !["127.0.0.1", "localhost", "[::1]"].includes(origin.hostname) + ) throw new Error("Workspace cleanup needs a local T3 Code server."); + + const executable = run("/bin/ps", ["-p", String(runtime.pid), "-o", "comm="]); + const app = executable.includes("/Contents/MacOS/") + ? executable.split("/Contents/MacOS/")[0] + : null; + const cli = app + ? [ + executable, + path.join(app, "Contents/Resources/app.asar/apps/server/dist/bin.mjs"), + ] + : [executable]; + const authFile = path.join(state, "t3-auth.json"); + let authentication; + if (fs.existsSync(authFile) && Date.now() - fs.statSync(authFile).mtimeMs < 60_000) { + authentication = JSON.parse(fs.readFileSync(authFile)); + } + if (authentication?.pid !== runtime.pid) { + const token = childProcess.execFileSync(cli[0], [ + ...cli.slice(1), + "auth", + "session", + "issue", + "--base-dir", + t3Home, + "--ttl", + "2m", + "--token-only", + ], { + env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" }, + encoding: "utf8", + timeout: 15_000, + stdio: ["ignore", "pipe", "pipe"], + }).trim(); + authentication = { pid: runtime.pid, token }; + writeJson(authFile, authentication); + } + + async function request(route, body) { + const response = await fetch(new URL(route, origin), { + method: body === undefined ? "GET" : "POST", + // Synchronous JJ/pnpm calls can outlive HTTP keep-alive sockets. + headers: { + Authorization: `Bearer ${authentication.token}`, + "Content-Type": "application/json", + "x-t3-orchestration-protocol": "2", + Connection: "close", + }, + body: body === undefined ? undefined : JSON.stringify(body), + signal: AbortSignal.timeout(10_000), + }); + if (!response.ok) { + throw new Error( + `T3 Code returned ${response.status}. Cleanup is paused; check the nightly app.`, + ); + } + return response.json(); + } + + const environment = await request("/.well-known/t3/environment"); + if (environment.orchestrationProtocolVersion !== 2) { + throw new Error("Update T3 Code to a nightly build with orchestrator V2."); + } + + async function snapshot() { + const data = await request("/api/orchestration/shell"); + if ( + !Array.isArray(data.projects) || !Array.isArray(data.threads) || + !Array.isArray(data.archivedThreads) + ) { + throw new Error( + "T3 Code returned an unknown thread list. Cleanup is paused.", + ); + } + for (const thread of [...data.threads, ...data.archivedThreads]) { + if ( + ![ + "settledOverride", + "activeRunId", + "worktreePath", + "deletedAt", + "archivedAt", + "pendingRuntimeRequest", + "status", + ].every((field) => field in thread) + ) { + throw new Error( + "T3 Code returned an unknown thread state. Cleanup is paused.", + ); + } + } + return data; + } + + async function dispatch(command) { + const { ticket } = await request("/api/auth/websocket-ticket", {}); + const url = new URL("/ws", origin); + url.protocol = "ws:"; + url.searchParams.set("wsTicket", ticket); + url.searchParams.set("orchestrationProtocol", "2"); + return new Promise((resolve, reject) => { + const socket = new WebSocket(url); + const timeout = setTimeout( + () => + finish( + new Error( + "T3 Code did not confirm the workspace chat. Run ws open to retry.", + ), + ), + 15_000, + ); + function finish(error, value) { + clearTimeout(timeout); + socket.close(); + if (error) reject(error); + else resolve(value); + } + socket.onopen = () => + socket.send( + JSON.stringify({ + _tag: "Request", + id: "1", + tag: command.type.startsWith("project.") + ? "projects.mutate" + : "orchestration.dispatchCommand", + payload: { commandId: randomUUID(), ...command }, + headers: [], + }) + "\n", + ); + socket.onmessage = (event) => { + for (const line of String(event.data).trim().split("\n")) { + const message = JSON.parse(line); + if (message._tag !== "Exit" || String(message.requestId) !== "1") { + continue; + } + if (message.exit._tag === "Success") finish(null, message.exit.value); + else { + finish( + new Error( + `T3 Code rejected the workspace command: ${ + JSON.stringify(message.exit) + }`, + ), + ); + } + } + }; + socket.onerror = () => + finish( + new Error("Cannot reach T3 Code. Start the nightly app and retry."), + ); + socket.onclose = () => { + clearTimeout(timeout); + reject(new Error("T3 Code closed the chat connection.")); + }; + }); + } + return { snapshot, dispatch, environment, app }; +} + +function dependencyKey(directory) { + const files = run("jj", ["--ignore-working-copy", "file", "list"], directory).split("\n").filter(( + file, + ) => + path.basename(file) === "package.json" || + ["pnpm-lock.yaml", "pnpm-workspace.yaml", ".npmrc", ".pnpmfile.cjs"] + .includes(file) || + file.startsWith("patches/") + ).sort(); + const hash = createHash("sha256"); + hash.update( + `${process.platform}:${process.arch}:${process.versions.modules}:${ + run("pnpm", ["--version"], directory) + }\0`, + ); + for (const file of files) { + hash.update(file + "\0").update(fs.readFileSync(path.join(directory, file))) + .update("\0"); + } + return hash.digest("hex"); +} + +function moduleDirectories(directory, relative = "") { + const found = []; + for ( + const entry of fs.readdirSync(path.join(directory, relative), { + withFileTypes: true, + }) + ) { + if (!entry.isDirectory() || entry.name.startsWith(".")) continue; + const child = path.join(relative, entry.name); + if (entry.name === "node_modules") found.push(child); + else found.push(...moduleDirectories(directory, child)); + } + return found; +} + +function dependencies(directory, source) { + const key = dependencyKey(directory); + let seed; + if ( + fs.existsSync(path.join(cache, "key")) && + fs.readFileSync(path.join(cache, "key"), "utf8") === key + ) seed = cache; + else if ( + dependencyKey(source) === key && + fs.existsSync(path.join(source, "node_modules/.pnpm/lock.yaml")) + ) seed = source; + if (seed) { + for (const relative of moduleDirectories(seed)) { + const target = path.join(directory, relative); + if (fs.existsSync(target) || !fs.existsSync(path.dirname(target))) { + continue; + } + run( + "workspace-native", + ["clone", path.join(seed, relative), target], + undefined, + true, + ); + } + } + + // pnpm regenerates launchers that embed the previous workspace's absolute path. + run( + "pnpm", + [ + "install", + "--frozen-lockfile", + "--prefer-offline", + "--store-dir", + path.join(home, ".local/share/pnpm/store"), + ], + directory, + true, + ); + if (dependencyKey(directory) !== key) { + throw new Error( + "Dependency files changed during installation. Run ws deps again.", + ); + } + if (seed === cache) return; + + const temporary = `${cache}.tmp`; + fs.rmSync(temporary, { recursive: true, force: true }); + fs.mkdirSync(temporary, { recursive: true }); + for (const relative of moduleDirectories(directory)) { + const target = path.join(temporary, relative); + fs.mkdirSync(path.dirname(target), { recursive: true }); + run( + "workspace-native", + ["clone", path.join(directory, relative), target], + undefined, + true, + ); + } + fs.writeFileSync(path.join(temporary, "key"), key); + fs.rmSync(cache, { recursive: true, force: true }); + fs.renameSync(temporary, cache); +} + +async function editorReferences() { + const processes = run("/bin/ps", ["-axo", "pid=,comm="]); + const instances = processes.split("\n").filter((line) => + line.endsWith("/Visual Studio Code.app/Contents/MacOS/Code") + ).map((line) => Number(line.trim().split(/\s+/, 1)[0])); + if (!instances.length) return []; + const status = run("/usr/local/bin/code", ["--status"]); + const reported = [...status.matchAll(/^\s*\d+\s+\d+\s+(\d+)\s+code\s*$/gm)] + .map((match) => Number(match[1])); + if (instances.some((pid) => !reported.includes(pid))) { + throw new Error( + "Cannot inspect every VS Code instance. Cleanup is paused.", + ); + } + const pids = [...status.matchAll(/\b(\d+)\s+extension-host\b/g)].map(( + match, + ) => Number(match[1])); + const windows = [...status.matchAll(/\bwindow \[\d+\]/g)].length; + if (pids.length < windows) { + throw new Error( + "VS Code has a window without a reference tracker. Reload its windows before cleanup.", + ); + } + const paths = []; + for (const pid of pids) { + const response = await new Promise((resolve, reject) => { + const socket = net.createConnection( + path.join(state, "vscode", `${pid}.sock`), + ); + let data = ""; + socket.setTimeout( + 2000, + () => + socket.destroy( + new Error( + "VS Code reference tracker did not respond. Reload its windows before cleanup.", + ), + ), + ); + socket.on("data", (chunk) => { + data += chunk; + }); + socket.on("end", () => { + try { + resolve(JSON.parse(data)); + } catch (error) { + reject(error); + } + }); + socket.on("error", reject); + }); + if (response.pid !== pid || !Array.isArray(response.paths)) { + throw new Error( + "Cannot verify VS Code's open folders. Reload its windows before cleanup.", + ); + } + paths.push(...response.paths.map(canonical)); + } + return paths; +} + +function terminalReferences() { + const pids = new Set(); + const processes = run("/bin/ps", ["-axo", "pid=,tty=,comm="]); + for (const line of processes.split("\n")) { + const match = line.match(/^\s*(\d+)\s+(\S+)\s+(.+)$/); + if (match && match[2] !== "??" && /\/(zsh|bash|fish|sh)$/.test(match[3])) { + pids.add(Number(match[1])); + } + } + const directory = path.join(state, "shells"); + for ( + const name of fs.existsSync(directory) ? fs.readdirSync(directory) : [] + ) { + if (!/^\d+$/.test(name)) continue; + const pid = Number(name); + if (alive(pid)) pids.add(pid); + else fs.rmSync(path.join(directory, name)); + } + if (!pids.size) return []; + + let output; + try { + output = run("/usr/sbin/lsof", [ + "-a", + "-p", + [...pids].join(","), + "-d", + "cwd", + "-Fpn", + ]); + } catch (error) { + if (error.status !== 1) throw error; + output = String(error.stdout ?? ""); + } + const found = new Set(); + const paths = []; + let pid; + for (const line of output.split("\n")) { + if (line.startsWith("p")) pid = Number(line.slice(1)); + if (line.startsWith("n")) { + found.add(pid); + paths.push(canonical(line.slice(1))); + } + } + for (const pid of pids) { + if (!found.has(pid) && alive(pid)) { + throw new Error( + `Cannot read terminal ${pid}'s folder. Cleanup is paused.`, + ); + } + } + return paths; +} + +async function references(client) { + const [snapshot, editors] = await Promise.all([ + client.snapshot(), + editorReferences(), + ]); + const projects = new Map( + snapshot.projects.map((project) => [project.id, project.workspaceRoot]), + ); + const chats = []; + for (const thread of [...snapshot.threads, ...snapshot.archivedThreads]) { + const released = thread.deletedAt !== null || thread.archivedAt !== null || + thread.settledOverride === "settled"; + const busy = ![ + "idle", + "completed", + "interrupted", + "failed", + "cancelled", + "rolled_back", + ].includes(thread.status) || thread.activeRunId !== null || + thread.pendingRuntimeRequest !== null || + thread.pendingBackgroundTasks?.length > 0; + if (released && !busy) continue; + const directory = thread.worktreePath ?? projects.get(thread.projectId); + if (!directory) { + throw new Error( + "Cannot locate a T3 chat's workspace. Cleanup is paused.", + ); + } + chats.push(canonical(directory)); + } + return { chats, editors, terminals: terminalReferences() }; +} + +async function openChat(id, config, client, open = true) { + const directory = workspace(id); + const file = path.join(records, `${id}.json`); + const record = JSON.parse(fs.readFileSync(file)); + const snapshot = await client.snapshot(); + const repository = jjRepository(config.repository); + const source = snapshot.projects.find((project) => + canonical(project.workspaceRoot) === config.repository + ) ?? snapshot.projects.find((project) => + !inside(canonical(project.workspaceRoot), root) && + fs.existsSync(path.join(project.workspaceRoot, ".jj/repo")) && + jjRepository(project.workspaceRoot) === repository + ); + if (!source) { + throw new Error( + "Add the source repository to T3 Code before creating a workspace chat.", + ); + } + const previous = [...snapshot.threads, ...snapshot.archivedThreads].find( + (thread) => thread.id === record.threadId, + ); + if (!previous) { + let project = snapshot.projects.find((project) => + canonical(project.workspaceRoot) === directory + ); + const latest = [...snapshot.threads, ...snapshot.archivedThreads].filter( + (thread) => thread.projectId === (project?.id ?? source.id), + ).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))[0]; + const modelSelection = project?.defaultModelSelection ?? + source.defaultModelSelection ?? + latest?.modelSelection; + if (!modelSelection) { + throw new Error( + "Choose a model in the source repository's T3 settings, then run ws open again.", + ); + } + if (!project) { + project = await client.dispatch({ + type: "project.create", + projectId: randomUUID(), + title: `${source.title} | ${id}`, + workspaceRoot: directory, + }); + } + await client.dispatch({ + type: "project.update", + projectId: project.id, + projectIcon: source.projectIcon ?? null, + faviconPath: source.faviconPath ?? null, + defaultThreadEnvMode: "local", + defaultModelSelection: modelSelection, + }); + const threadId = record.threadId ?? randomUUID(); + record.threadId = threadId; + writeJson(file, record); + await client.dispatch({ + type: "thread.create", + threadId, + projectId: project.id, + title: project.title, + modelSelection, + runtimeMode: latest?.runtimeMode ?? "full-access", + interactionMode: latest?.interactionMode ?? "default", + branch: null, + worktreePath: null, + createdBy: "user", + creationSource: "web", + }); + const confirmed = await client.snapshot(); + if (!confirmed.threads.some((thread) => thread.id === threadId)) { + throw new Error( + "T3 Code has not confirmed the chat yet. Run ws open again.", + ); + } + } + if (previous?.archivedAt) { + await client.dispatch({ type: "thread.unarchive", threadId: previous.id }); + } + if (previous?.settledOverride === "settled") { + await client.dispatch({ + type: "thread.unsettle", + threadId: previous.id, + reason: "user", + }); + } + record.idleSince = null; + writeJson(file, record); + const environmentId = client.environment.environmentId; + if (open) { + run("/usr/bin/open", [ + ...(client.app ? ["-a", client.app] : ["-b", "com.t3tools.t3code"]), + `t3code://threads/${environmentId}/${record.threadId}`, + ]); + } + console.log(directory); +} + +export async function main(args) { + const [command, argument, ...rest] = args; + if (command === "ready-hook") { + let input = ""; + for await (const chunk of process.stdin) input += chunk; + const event = JSON.parse(input); + const directory = canonical(event.cwd); + if (!inside(directory, root)) return; + const id = path.relative(root, directory).split(path.sep)[0]; + const file = path.join(records, `${id}.json`); + if (!fs.existsSync(file)) return; + + const deadline = Date.now() + 240_000; + let waited = false; + while (true) { + const record = JSON.parse(fs.readFileSync(file)); + if (record.preparationError) { + throw new Error(`Workspace ${id} setup stopped: ${record.preparationError}. Run ws deps ${id}, then resend your prompt.`); + } + if (record.prepared) break; + if (Date.now() >= deadline) { + throw new Error(`Workspace ${id} is still preparing. Check ~/Library/Logs/workspace-${id}.log, then resend your prompt.`); + } + waited = true; + await new Promise((resolve) => setTimeout(resolve, 100)); + } + if (waited && event.hook_event_name === "SessionStart") { + const files = new Set(["AGENTS.md", "CLAUDE.md"].map((name) => + path.join(workspace(id), name) + ).filter((file) => fs.existsSync(file)).map(canonical)); + const instructions = [...files].map((file) => fs.readFileSync(file, "utf8")); + if (instructions.length) { + console.log(JSON.stringify({ hookSpecificOutput: { + hookEventName: "SessionStart", + additionalContext: instructions.join("\n\n"), + } })); + } + } + return; + } + if (!command || command === "--help") { + console.log( + "ws setup \nws new [name] [-r revision] [--no-open] [--background]\nws open [--no-open]\nws deps \nws gc [--dry-run]", + ); + return; + } + fs.mkdirSync(state, { recursive: true, mode: 0o700 }); + if (command === "setup") { + if (!argument || rest.length) throw new Error("Run ws setup ."); + const repository = fs.realpathSync( + run("jj", ["workspace", "root"], path.resolve(argument)), + ); + if (!fs.existsSync(path.join(repository, "pnpm-lock.yaml"))) { + throw new Error("Choose the repository's pnpm workspace root."); + } + if (fs.existsSync(records) && fs.readdirSync(records).length) { + const configured = JSON.parse(fs.readFileSync(configFile)); + if (jjRepository(repository) !== jjRepository(configured.repository)) { + throw new Error( + "Finish existing workspaces before changing their source repository.", + ); + } + } + writeJson(configFile, { repository }); + console.log(`Workspace source: ${repository}`); + return; + } + if ( + command === "gc" && + (!fs.existsSync(records) || !fs.readdirSync(records).length) + ) return; + const config = JSON.parse(fs.readFileSync(configFile)); + if (command === "deps") { + const file = path.join(records, `${argument}.json`); + const record = JSON.parse(fs.readFileSync(file)); + delete record.preparationError; + writeJson(file, record); + try { + if ("revision" in record) { + if (record.revision === null) { + run("jj", [ + "--ignore-working-copy", + "--config", "git.abandon-unreachable-commits=false", + "git", "fetch", "--remote", "origin", "--branch", "main", + ], config.repository, true); + } + const started = Date.now(); + // Full sparse patterns let checkout wait without touching the source. + run("jj", [ + "--ignore-working-copy", "workspace", "add", + "--name", `workspaces-${argument}`, "--sparse-patterns", "full", + "-r", record.revision ?? "main@origin", workspace(argument), + ], config.repository, true); + console.error(`JJ workspace initialized in ${Date.now() - started} ms`); + delete record.revision; + writeJson(file, record); + } + if (!record.prepared) { + run("jj", ["workspace", "update-stale"], workspace(argument), true); + } + dependencies(workspace(argument), config.repository); + record.prepared = true; + writeJson(file, record); + } catch (error) { + record.preparationError = error.message.split("\n", 1)[0]; + writeJson(file, record); + throw error; + } + return; + } + if (command === "new") { + const client = await t3(); + const open = ![argument, ...rest].includes("--no-open"); + const background = [argument, ...rest].includes("--background"); + const options = [argument, ...rest].filter((value) => + value !== undefined && value !== "--no-open" && value !== "--background" + ); + const id = options[0]?.startsWith("-") ? undefined : options.shift(); + const revision = options.length === 0 + ? null + : options.length === 2 && options[0] === "-r" + ? options[1] + : undefined; + if (revision === undefined) throw new Error("Run ws new [name] [-r revision]."); + fs.mkdirSync(root, { recursive: true }); + let number = 1; + while ( + fs.existsSync(workspace(String(number))) || + fs.existsSync(path.join(records, `${number}.json`)) + ) number++; + const name = id ?? String(number); + const directory = workspace(name); + if ( + fs.existsSync(directory) || + fs.existsSync(path.join(records, `${name}.json`)) + ) throw new Error(`Workspace ${name} already exists. Run ws open ${name}.`); + fs.mkdirSync(directory); + const file = path.join(records, `${name}.json`); + writeJson(file, { + threadId: null, + idleSince: null, + prepared: false, + revision, + }); + await openChat(name, config, client, open); + const logs = path.join(home, "Library/Logs"); + fs.mkdirSync(logs, { recursive: true }); + const output = background + ? fs.openSync(path.join(logs, `workspace-${name}.log`), "w", 0o600) + : 2; + try { + const child = childProcess.spawn("workspace-native", [ + "wait-lock", path.join(state, "lock"), process.execPath, + fileURLToPath(import.meta.url), "deps", name, + ], { + detached: background, + stdio: ["ignore", output, output], + }); + if (background) { + await new Promise((resolve, reject) => { + child.once("spawn", resolve); + child.once("error", reject); + }); + child.unref(); + } else { + const status = await new Promise((resolve, reject) => { + child.once("exit", resolve); + child.once("error", reject); + }); + if (status !== 0) { + throw new Error(`Workspace ${name} setup stopped. Run ws deps ${name} to retry.`); + } + } + } finally { + if (background) fs.closeSync(output); + } + } else if (command === "open") { + if (rest.length > 1 || (rest[0] && rest[0] !== "--no-open")) { + throw new Error("Run ws open [--no-open]."); + } + await openChat(argument, config, await t3(), rest[0] !== "--no-open"); + } else if (command === "gc") { + if (rest.length || (argument && argument !== "--dry-run")) { + throw new Error("Run ws gc [--dry-run]."); + } + const entries = fs.readdirSync(records).filter((file) => + file.endsWith(".json") + ); + try { + const client = await t3(); + const refs = await references(client); + for (const entry of entries) { + const id = entry.slice(0, -5); + const directory = workspace(id); + const file = path.join(records, entry); + const record = JSON.parse(fs.readFileSync(file)); + const idleSince = record.idleSince; + const blockers = Object.entries(refs).filter(([, paths]) => + paths.some((candidate) => inside(candidate, canonical(directory))) + ).map(([kind]) => kind); + if (!record.threadId || !record.prepared) blockers.push("unfinished setup"); + if (blockers.length) { + record.idleSince = null; + if (argument === "--dry-run") { + console.log(`${id}: held by ${blockers.join(", ")}`); + } + } else { + record.idleSince ??= Date.now(); + if (Date.now() - record.idleSince >= 60_000) { + if (argument === "--dry-run") { + console.log(`${id}: ready for cleanup`); + continue; + } + const fresh = await references(client); + if ( + Object.values(fresh).some((paths) => + paths.some((candidate) => + inside(candidate, canonical(directory)) + ) + ) + ) { + record.idleSince = null; + } else { + if ( + fs.lstatSync(directory).isSymbolicLink() || + canonical(directory) !== directory + ) { + throw new Error( + `Workspace ${id} was replaced by a link. Cleanup is paused.`, + ); + } + if (jjRepository(directory) !== jjRepository(config.repository)) { + throw new Error( + `Workspace ${id} belongs to another repository. Cleanup is paused.`, + ); + } + run("jj", ["status"], directory); + run( + "jj", + ["--ignore-working-copy", "workspace", "forget", `workspaces-${id}`], + config.repository, + ); + fs.rmSync(directory, { recursive: true }); + fs.rmSync(file); + console.log(`Removed workspace ${id}`); + continue; + } + } else if (argument === "--dry-run") { + console.log(`${id}: grace period`); + } + } + if (record.idleSince !== idleSince) writeJson(file, record); + } + } catch (error) { + for (const entry of entries) { + const file = path.join(records, entry); + if (!fs.existsSync(file)) continue; + const record = JSON.parse(fs.readFileSync(file)); + if (record.idleSince !== null) { + record.idleSince = null; + writeJson(file, record); + } + } + throw error; + } + } else throw new Error("Unknown workspace command. Run ws --help."); +} + +if ( + process.argv[1] && + path.resolve(process.argv[1]) === fileURLToPath(import.meta.url) +) { + main(process.argv.slice(2)).catch((error) => { + if (process.argv[2] === "ready-hook") { + console.log(JSON.stringify({ + continue: false, stopReason: error.message, systemMessage: error.message, + })); + return; + } + if (error.stderr?.length) console.error(error.stderr.toString().trim()); + console.error(`Error: ${error.message.split("\n", 1)[0]}`); + process.exitCode = 1; + }); +} diff --git a/users/clover/packages/workspaces/workspaces.test.mjs b/users/clover/packages/workspaces/workspaces.test.mjs new file mode 100644 index 0000000000000000000000000000000000000000..bc9df9a0cc341710c4897c88b2bb9ceb523efc23 --- /dev/null +++ b/users/clover/packages/workspaces/workspaces.test.mjs @@ -0,0 +1,634 @@ +import assert from "node:assert/strict"; +import { mock, test } from "node:test"; +import fs from "node:fs"; +import path from "node:path"; +import net from "node:net"; +import childProcess from "node:child_process"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import { fileURLToPath } from "node:url"; + +const cli = process.env.T3_TEST_CLI; +const native = process.env.WORKSPACE_TEST_NATIVE; + +test("workspace lifecycle through nightly, JJ, APFS, and reference sockets", { + skip: !cli || !native, + timeout: 120_000, +}, async () => { + const directory = fs.realpathSync(fs.mkdtempSync("/tmp/workspaces-test-")); + const originalHome = process.env.HOME; + const originalT3 = process.env.T3CODE_HOME; + const originalPath = process.env.PATH; + process.env.HOME = directory; + process.env.T3CODE_HOME = path.join(directory, ".t3"); + process.env.PATH = path.dirname(native) + path.delimiter + originalPath; + const source = path.join(directory, "source"); + const run = childProcess.execFileSync; + const reservation = net.createServer(); + reservation.listen(0, "127.0.0.1"); + await once(reservation, "listening"); + const port = reservation.address().port; + await new Promise((resolve) => reservation.close(resolve)); + const serverLog = fs.openSync(path.join(directory, "t3.log"), "w"); + const server = childProcess.spawn(cli, [ + "serve", + "--base-dir", + process.env.T3CODE_HOME, + "--port", + String(port), + "--no-browser", + ], { stdio: ["ignore", serverLog, serverLog] }); + let editor; + let shell; + try { + fs.mkdirSync(path.join(source, "shared/local"), { recursive: true }); + fs.writeFileSync(path.join(source, ".gitignore"), "node_modules/\n"); + fs.writeFileSync( + path.join(source, "logo.svg"), + '', + ); + fs.writeFileSync( + path.join(source, "package.json"), + JSON.stringify({ + name: "fixture", + private: true, + packageManager: "pnpm@10.32.1", + dependencies: { "local-dependency": "workspace:*" }, + }), + ); + fs.writeFileSync( + path.join(source, "pnpm-workspace.yaml"), + "packages:\n - shared/*\n", + ); + fs.writeFileSync( + path.join(source, "shared/local/package.json"), + JSON.stringify({ + name: "local-dependency", + version: "1.0.0", + main: "index.js", + bin: { "local-command": "bin.js" }, + }), + ); + fs.writeFileSync( + path.join(source, "shared/local/index.js"), + "module.exports = 42;\n", + ); + fs.writeFileSync( + path.join(source, "shared/local/bin.js"), + "#!/usr/bin/env node\nconsole.log(process.env.NODE_PATH);\n", + ); + run("jj", ["git", "init", "--colocate", source], { stdio: "pipe" }); + run("pnpm", ["install", "--no-frozen-lockfile"], { + cwd: source, + stdio: "pipe", + timeout: 30_000, + }); + const origin = path.join(directory, "origin"); + run("jj", ["git", "init", origin], { stdio: "pipe" }); + run("jj", ["describe", "-m", "Fixture main"], { cwd: source, stdio: "pipe" }); + run("jj", ["bookmark", "set", "main"], { cwd: source, stdio: "pipe" }); + run("jj", ["git", "remote", "add", "origin", path.join(origin, ".jj/repo/store/git")], { + cwd: source, + stdio: "pipe", + }); + run("jj", ["--config", "git.sign-on-push=false", "git", "push", "--bookmark", "main"], { + cwd: source, + stdio: "pipe", + }); + run("jj", ["git", "import"], { cwd: origin, stdio: "pipe" }); + run("jj", ["new", "main"], { cwd: origin, stdio: "pipe" }); + fs.writeFileSync(path.join(origin, "upstream.txt"), "latest main\n"); + run("jj", ["describe", "-m", "Advance remote main"], { + cwd: origin, + stdio: "pipe", + }); + run("jj", ["bookmark", "set", "main"], { cwd: origin, stdio: "pipe" }); + run("jj", ["git", "export"], { cwd: origin, stdio: "pipe" }); + const latestMain = run("jj", ["log", "-r", "main", "--no-graph", "-T", "commit_id"], { + cwd: origin, + encoding: "utf8", + }).trim(); + assert.notEqual(run("jj", [ + "log", "-r", "main@origin", "--no-graph", "-T", "commit_id", + ], { cwd: source, encoding: "utf8" }).trim(), latestMain); + const runtime = path.join( + process.env.T3CODE_HOME, + "userdata/server-runtime.json", + ); + for (let attempt = 0; !fs.existsSync(runtime); attempt++) { + assert.ok( + attempt < 150 && server.exitCode === null, + "nightly server starts", + ); + await new Promise((resolve) => setTimeout(resolve, 100)); + } + const projectWorkspace = path.join(directory, "Fixture"); + run("jj", ["workspace", "add", "-r", "@", projectWorkspace], { + cwd: source, + stdio: "pipe", + }); + run( + cli, + ["project", "add", projectWorkspace, "--base-dir", process.env.T3CODE_HOME], + { stdio: "pipe", timeout: 15_000 }, + ); + const { main, t3 } = await import("./workspaces.mjs"); + const client = await t3(); + const project = (await client.snapshot()).projects[0]; + await client.dispatch({ + type: "project.update", + projectId: project.id, + projectIcon: { kind: "monogram", text: "F", color: "red" }, + faviconPath: "logo.svg", + defaultThreadEnvMode: "worktree", + }); + await client.dispatch({ + type: "thread.create", + threadId: randomUUID(), + projectId: project.id, + title: "Model source", + modelSelection: { instanceId: "codex", model: "gpt-6.1-sol" }, + runtimeMode: "approval-required", + interactionMode: "default", + branch: null, + worktreePath: null, + createdBy: "user", + creationSource: "web", + }); + await main(["setup", source]); + const sourceChange = run("jj", ["log", "-r", "@", "--no-graph", "-T", "change_id"], { + cwd: source, + encoding: "utf8", + }).trim(); + fs.writeFileSync(path.join(source, "local-only.txt"), "uncommitted source work\n"); + fs.writeFileSync(path.join(projectWorkspace, "rewrite.txt"), "changed from another workspace\n"); + run("jj", ["restore", "--from", "@", "--into", sourceChange, "rewrite.txt"], { + cwd: projectWorkspace, + stdio: "pipe", + }); + assert.throws(() => run("jj", ["--config", "snapshot.auto-update-stale=false", "st"], { + cwd: source, + stdio: "pipe", + }), (error) => /working copy is stale/.test(error.stderr.toString())); + const firstBin = path.join(directory, "first-bin"); + fs.mkdirSync(firstBin); + const pnpm = run("/usr/bin/which", ["pnpm"], { encoding: "utf8" }).trim(); + fs.writeFileSync(path.join(firstBin, "pnpm"), `#!/usr/bin/env node +const fs = require("node:fs"); +const childProcess = require("node:child_process"); +const assert = require("node:assert/strict"); +const args = process.argv.slice(2); +if (args[0] === "install" && process.cwd() === ${JSON.stringify(path.join(directory, "workspaces/first"))}) { + const record = JSON.parse(fs.readFileSync(${JSON.stringify(path.join(directory, ".local/state/workspaces/workspaces/first.json"))})); + assert.ok(record.threadId, "chat is durable before dependency installation"); + assert.equal(record.prepared, false); + console.error("Error: fixture installation interrupted"); + process.exit(41); +} +const result = childProcess.spawnSync(${JSON.stringify(pnpm)}, args, { stdio: "inherit" }); +process.exit(result.status ?? 1); +`, { mode: 0o700 }); + const nativePath = process.env.PATH; + process.env.PATH = firstBin + path.delimiter + nativePath; + let opened = false; + const launchMock = mock.method( + childProcess, + "execFileSync", + (command, args, options) => { + if (command === "/usr/bin/open") { + opened = true; + assert.match(args.at(-1), /^t3code:\/\/threads\//); + assert.equal( + fs.existsSync(path.join(directory, "workspaces/first/.jj")), + false, + "desktop opens before JJ initializes the folder", + ); + return ""; + } + return run(command, args, options); + }, + ); + await assert.rejects( + main(["new", "first", "-r", "@"]), + /setup stopped/, + ); + assert.ok(opened, "desktop opens before dependency installation"); + process.env.PATH = nativePath; + launchMock.mock.restore(); + assert.equal(fs.readFileSync(path.join(source, "local-only.txt"), "utf8"), + "uncommitted source work\n", "creation preserves unsaved source edits"); + assert.equal(fs.existsSync(path.join(source, "rewrite.txt")), false, + "workspace creation leaves the stale source checkout untouched"); + assert.throws(() => run("jj", ["--config", "snapshot.auto-update-stale=false", "st"], { + cwd: source, + stdio: "pipe", + }), (error) => /working copy is stale/.test(error.stderr.toString())); + await main(["setup", projectWorkspace]); + await main(["setup", source]); + await main(["new", "second", "--no-open"]); + const created = (await client.snapshot()).threads.find((thread) => + thread.title === "Fixture | second" + ); + assert.equal( + created.runtimeMode, + "approval-required", + "new chats retain the source chat's permission mode", + ); + const first = path.join(directory, "workspaces/first"); + const second = path.join(directory, "workspaces/second"); + const projects = (await client.snapshot()).projects; + const firstProject = projects.find((project) => + project.workspaceRoot === first + ); + const secondProject = projects.find((project) => + project.workspaceRoot === second + ); + assert.equal(firstProject.title, "Fixture | first"); + assert.equal(secondProject.title, "Fixture | second"); + assert.notEqual(firstProject.id, secondProject.id); + assert.equal(created.projectId, secondProject.id); + assert.equal( + created.worktreePath, null, "new chats use their workspace project's root", + ); + assert.deepEqual(secondProject.projectIcon, { + kind: "monogram", text: "F", color: "red", + }); + assert.equal(secondProject.faviconPath, "logo.svg"); + assert.equal(secondProject.defaultThreadEnvMode, "local"); + assert.deepEqual(secondProject.defaultModelSelection, created.modelSelection); + assert.equal(run("jj", ["log", "-r", "@-", "--no-graph", "-T", "commit_id"], { + cwd: second, + encoding: "utf8", + }).trim(), latestMain, "default creation fetches the latest remote main"); + assert.equal(fs.readFileSync(path.join(second, "upstream.txt"), "utf8"), "latest main\n"); + assert.equal(fs.existsSync(path.join(second, "local-only.txt")), false, + "default creation excludes source-only work"); + assert.equal( + run(process.execPath, [ + "-e", + "process.stdout.write(String(require('local-dependency')))", + ], { cwd: second, encoding: "utf8" }), + "42", + ); + const launcherPaths = run( + path.join(second, "node_modules/.bin/local-command"), + [], + { cwd: second, encoding: "utf8" }, + ); + assert.ok(launcherPaths.includes(second)); + assert.equal( + launcherPaths.includes(source) || launcherPaths.includes(first), + false, + "pnpm relocates launchers after cloning", + ); + const clonedFile = path.join(second, "node_modules/.pnpm/lock.yaml"); + const cachedFile = path.join( + directory, + ".cache/workspaces/deps/node_modules/.pnpm/lock.yaml", + ); + const cachedContents = fs.readFileSync(cachedFile, "utf8"); + assert.notEqual(fs.statSync(clonedFile).ino, fs.statSync(cachedFile).ino); + fs.appendFileSync(clonedFile, "\n# isolated edit\n"); + assert.equal(fs.readFileSync(cachedFile, "utf8"), cachedContents); + assert.equal( + fs.readlinkSync(path.join(second, "node_modules/local-dependency")), + "../shared/local", + ); + + const background = path.join(directory, "workspaces/background"); + const started = path.join(directory, "preparation-started"); + const release = path.join(directory, "preparation-release"); + const fetchStarted = path.join(directory, "fetch-started"); + const fetchRelease = path.join(directory, "fetch-release"); + const bin = path.join(directory, "bin"); + fs.mkdirSync(bin); + fs.writeFileSync(path.join(bin, "jj"), `#!/bin/sh +case "$*" in + *"git fetch"*) + touch "$WORKSPACE_TEST_FETCH_STARTED" + while [ ! -f "$WORKSPACE_TEST_FETCH_RELEASE" ]; do sleep 0.05; done + ;; +esac +exec "$WORKSPACE_TEST_JJ" "$@" +`, { mode: 0o700 }); + fs.writeFileSync(path.join(bin, "pnpm"), `#!/bin/sh +if [ "$1" = install ] && [ "$PWD" = "$WORKSPACE_TEST_BACKGROUND" ]; then + touch "$WORKSPACE_TEST_STARTED" + while [ ! -f "$WORKSPACE_TEST_RELEASE" ]; do sleep 0.05; done +fi +exec "$WORKSPACE_TEST_PNPM" "$@" +`, { mode: 0o700 }); + const script = fileURLToPath(new URL("./workspaces.mjs", import.meta.url)); + const preparationEnv = { + ...process.env, + PATH: bin + path.delimiter + process.env.PATH, + WORKSPACE_TEST_BACKGROUND: background, + WORKSPACE_TEST_STARTED: started, + WORKSPACE_TEST_RELEASE: release, + WORKSPACE_TEST_FETCH_STARTED: fetchStarted, + WORKSPACE_TEST_FETCH_RELEASE: fetchRelease, + WORKSPACE_TEST_JJ: run("/usr/bin/which", ["jj"], { + encoding: "utf8", + }).trim(), + WORKSPACE_TEST_PNPM: pnpm, + }; + const creator = childProcess.spawn(native, [ + "lock", + path.join(directory, ".local/state/workspaces/creation-lock"), + process.execPath, + script, + "new", "background", "--no-open", "--background", + ], { + env: preparationEnv, + stdio: ["ignore", "pipe", "pipe"], + }); + let creatorOutput = ""; + creator.stdout.on("data", (chunk) => { creatorOutput += chunk; }); + creator.stderr.on("data", (chunk) => { creatorOutput += chunk; }); + const [creatorStatus] = await once(creator, "exit"); + assert.equal(creatorStatus, 0, creatorOutput); + const backgroundRecord = path.join( + directory, ".local/state/workspaces/workspaces/background.json", + ); + assert.equal(JSON.parse(fs.readFileSync(backgroundRecord)).prepared, false); + for (let attempt = 0; !fs.existsSync(fetchStarted); attempt++) { + assert.ok(attempt < 200, "background preparation starts fetching main"); + await new Promise((resolve) => setTimeout(resolve, 50)); + } + assert.equal(fs.existsSync(path.join(background, ".jj")), false, + "the chat is available while fetching main and before JJ initialization"); + fs.writeFileSync(fetchRelease, ""); + for (let attempt = 0; !fs.existsSync(started); attempt++) { + assert.ok(attempt < 200, "background preparation reaches installation"); + await new Promise((resolve) => setTimeout(resolve, 50)); + } + assert.throws( + () => run(native, [ + "lock", path.join(directory, ".local/state/workspaces/lock"), "/usr/bin/true", + ], { stdio: "pipe" }), + (error) => /Another workspace command/.test(error.stderr.toString()), + "background preparation retains the workspace lock after its parent exits", + ); + const queued = childProcess.spawn(native, [ + "lock", path.join(directory, ".local/state/workspaces/creation-lock"), + process.execPath, script, "new", "queued", "--no-open", "--background", + ], { env: preparationEnv, stdio: "pipe" }); + let queuedOutput = ""; + queued.stderr.on("data", (chunk) => { queuedOutput += chunk; }); + const [queuedStatus] = await once(queued, "exit"); + assert.equal(queuedStatus, 0, queuedOutput); + const queuedRecord = path.join(directory, ".local/state/workspaces/workspaces/queued.json"); + assert.ok(JSON.parse(fs.readFileSync(queuedRecord)).threadId, + "another chat can be created while a preparation worker holds the lock"); + assert.equal(fs.existsSync(path.join(directory, "workspaces/queued/.jj")), false); + fs.writeFileSync(release, ""); + for (let attempt = 0; + !JSON.parse(fs.readFileSync(backgroundRecord)).prepared || + !JSON.parse(fs.readFileSync(queuedRecord)).prepared; + attempt++) { + assert.ok(attempt < 200, "background preparation finishes"); + await new Promise((resolve) => setTimeout(resolve, 50)); + } + + const recordFile = path.join( + directory, + ".local/state/workspaces/workspaces/first.json", + ); + const readRecord = () => JSON.parse(fs.readFileSync(recordFile)); + const age = () => + fs.writeFileSync( + recordFile, + JSON.stringify({ ...readRecord(), idleSince: Date.now() - 61_000 }), + ); + let editorPaths = []; + const sockets = path.join(directory, ".local/state/workspaces/vscode"); + fs.mkdirSync(sockets, { recursive: true }); + editor = net.createServer((socket) => + socket.end(JSON.stringify({ pid: process.pid, paths: editorPaths })) + ); + editor.listen(path.join(sockets, `${process.pid}.sock`)); + await once(editor, "listening"); + let unknownEditor = false; + const childMock = mock.method( + childProcess, + "execFileSync", + (command, args, options) => { + if ( + command === "/bin/ps" && args.join(" ") === "-axo pid=,comm=" + ) { + return "123 /Applications/Visual Studio Code.app/Contents/MacOS/Code"; + } + if ( + command === "/usr/local/bin/code" + ) { + return `1 100 123 code\n1 100 234 window [1]\n1 100 ${ + unknownEditor ? process.pid + 1 : process.pid + } extension-host [1]`; + } + return run(command, args, options); + }, + ); + + age(); + await main(["gc"]); + assert.ok( + fs.existsSync(first), + "idle but unsettled chat owns the workspace", + ); + assert.equal(readRecord().idleSince, null); + const originalThread = readRecord().threadId; + await client.dispatch({ type: "thread.settle", threadId: originalThread }); + age(); + await main(["gc"]); + assert.ok( + fs.existsSync(first), + "settled chat cannot release an unfinished setup", + ); + assert.equal(readRecord().idleSince, null); + await main(["deps", "first"]); + assert.equal(readRecord().prepared, true, "dependency repair finishes setup"); + await main(["open", "first", "--no-open"]); + assert.equal( + (await client.snapshot()).threads.find((thread) => + thread.id === originalThread + ).settledOverride, + "active", + "opening a workspace renews the chat's claim", + ); + await client.dispatch({ type: "thread.settle", threadId: originalThread }); + await main(["gc"]); + assert.ok(fs.existsSync(first), "settlement begins the grace period"); + assert.ok(readRecord().idleSince > Date.now() - 5000); + + editorPaths = [path.join(first, "shared/local")]; + age(); + await main(["gc"]); + assert.ok(fs.existsSync(first), "editor subfolder keeps the workspace"); + assert.equal(readRecord().idleSince, null); + editorPaths = []; + + shell = childProcess.spawn("/bin/zsh", [ + "-c", + "while read -r line; do :; done", + ], { + cwd: path.join(first, "shared/local"), + stdio: ["pipe", "ignore", "ignore"], + }); + fs.mkdirSync(path.join(directory, ".local/state/workspaces/shells"), { + recursive: true, + }); + fs.writeFileSync( + path.join(directory, ".local/state/workspaces/shells", String(shell.pid)), + path.join(first, "shared/local"), + ); + age(); + await main(["gc"]); + assert.ok(fs.existsSync(first), "live shell subfolder keeps the workspace"); + assert.equal(readRecord().idleSince, null); + shell.stdin.end(); + await once(shell, "exit"); + + unknownEditor = true; + age(); + await assert.rejects(main(["gc"])); + assert.ok(fs.existsSync(first), "missing editor tracker prevents deletion"); + assert.equal(readRecord().idleSince, null); + unknownEditor = false; + + const runtimeContents = fs.readFileSync(runtime); + fs.rmSync(runtime); + age(); + await assert.rejects(main(["gc"])); + assert.ok(fs.existsSync(first), "unavailable T3 prevents deletion"); + assert.equal(readRecord().idleSince, null); + fs.writeFileSync(runtime, runtimeContents); + + const anotherThread = randomUUID(); + await client.dispatch({ + type: "thread.create", + threadId: anotherThread, + projectId: firstProject.id, + title: "Shared workspace", + modelSelection: { instanceId: "codex", model: "gpt-6.1-sol" }, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + createdBy: "user", + creationSource: "web", + }); + age(); + await main(["gc"]); + assert.ok( + fs.existsSync(first), + "another unsettled chat in the workspace project keeps the folder", + ); + await client.dispatch({ type: "thread.archive", threadId: anotherThread }); + age(); + await main(["gc", "--dry-run"]); + assert.ok(fs.existsSync(first), "dry run never removes a workspace"); + fs.writeFileSync( + path.join(first, "saved.txt"), + "survives workspace cleanup\n", + ); + await main(["gc"]); + assert.equal(fs.existsSync(first), false); + const savedChange = run("jj", [ + "--ignore-working-copy", + "log", + "-r", + 'files("saved.txt")', + "--no-graph", + "-T", + "change_id", + ], { cwd: source, encoding: "utf8" }).trim(); + assert.equal( + run("jj", ["--ignore-working-copy", "file", "show", "saved.txt", "-r", savedChange], { + cwd: source, + encoding: "utf8", + }), + "survives workspace cleanup\n", + "JJ retains the snapshotted files", + ); + assert.ok(fs.existsSync(second), "unsettled sibling workspace survives"); + await main(["new", "first", "-r", "@", "--no-open"]); + const freshRecord = readRecord(); + assert.equal((await client.snapshot()).projects.filter((project) => + project.workspaceRoot === first + ).length, 1, "a reused folder reuses its registered project"); + assert.notEqual( + freshRecord.threadId, + originalThread, + "reusing a collected workspace name creates a fresh chat", + ); + await client.dispatch({ + type: "thread.settle", + threadId: freshRecord.threadId, + }); + age(); + await main(["gc"]); + const secondRecord = JSON.parse( + fs.readFileSync( + path.join(directory, ".local/state/workspaces/workspaces/second.json"), + ), + ); + await client.dispatch({ + type: "thread.delete", + threadId: secondRecord.threadId, + }); + fs.writeFileSync( + path.join(directory, ".local/state/workspaces/workspaces/second.json"), + JSON.stringify({ ...secondRecord, idleSince: Date.now() - 61_000 }), + ); + await main(["gc"]); + assert.equal( + fs.existsSync(second), + false, + "deleted chat releases its workspace", + ); + childMock.mock.restore(); + } finally { + mock.restoreAll(); + if (shell?.exitCode === null) shell.kill(); + if (editor) await new Promise((resolve) => editor.close(resolve)); + if (server.exitCode === null) { + server.kill("SIGKILL"); + await once(server, "exit"); + } + fs.closeSync(serverLog); + fs.rmSync(directory, { recursive: true, force: true, maxRetries: 3 }); + process.env.HOME = originalHome; + if (originalT3 === undefined) delete process.env.T3CODE_HOME; + else process.env.T3CODE_HOME = originalT3; + process.env.PATH = originalPath; + } +}); + +test("workspace lock rejects overlap and releases after process exit", { + skip: !native, +}, async () => { + const directory = fs.mkdtempSync("/tmp/workspaces-lock-"); + const lock = path.join(directory, "lock"); + const holder = childProcess.spawn(native, [ + "lock", + lock, + process.execPath, + "-e", + "process.stdout.write('ready'); setInterval(() => {}, 1000)", + ]); + try { + await once(holder.stdout, "data"); + assert.throws( + () => childProcess.execFileSync(native, ["lock", lock, "/usr/bin/true"], { + stdio: "pipe", + }), + (error) => /Another workspace command/.test(error.stderr.toString()), + ); + holder.kill(); + await once(holder, "exit"); + childProcess.execFileSync(native, ["lock", lock, "/usr/bin/true"]); + } finally { + if (holder.exitCode === null && holder.signalCode === null) holder.kill(); + fs.rmSync(directory, { recursive: true, force: true }); + } +}); diff --git a/users/clover/work/home.nix b/users/clover/work/home.nix index b45324655a3330406b58f8d2abd5085778129dd2..fd26e9a9da1ad8e0c7bc787ce07ca61473668a4b 100644 --- a/users/clover/work/home.nix +++ b/users/clover/work/home.nix @@ -1,5 +1,53 @@ -{ pkgs, ... }: +{ config, lib, pkgs, ... }: +let + workspaces = pkgs.callPackage ../packages/workspaces { }; + hookPython = pkgs.python3.withPackages (ps: [ ps.tomlkit ]); +in { + home.file.".config/raycast/scripts/new-workspace.sh" = { + executable = true; + text = '' + #!/bin/bash + # @raycast.schemaVersion 1 + # @raycast.title New Workspace + # @raycast.mode silent + # @raycast.packageName T3 Code + # @raycast.icon 💬 + + log="$(mktemp -t workspace)" + if ${workspaces}/bin/ws new --background > "$log" 2>&1; then + rm -f "$log" + else + mkdir -p "$HOME/Library/Logs" + mv "$log" "$HOME/Library/Logs/workspace-creation.log" + error="$(sed -n -e 's/^Error: //p' -e 's/^Internal error: //p' "$HOME/Library/Logs/workspace-creation.log" | head -n 1)" + printf '%s. Run ws new for details.\n' "''${error:-Workspace creation stopped}" + exit 1 + fi + ''; + }; + home.file.".vscode/extensions/clover.workspace-references".source = + config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/config/users/clover/packages/workspaces/extension"; + programs.zsh.initContent = builtins.readFile ../packages/workspaces/shell.zsh; + home.activation.workspaceReadinessHooks = lib.hm.dag.entryAfter [ "codexConfig" ] '' + run ${hookPython}/bin/python3 ${../packages/workspaces/configure-hooks.py} \ + ${lib.escapeShellArg config.home.homeDirectory} \ + ${lib.escapeShellArg "${config.home.profileDirectory}/bin/ws ready-hook"} \ + ${lib.escapeShellArg "${config.home.homeDirectory}/.local/bin/codex"} + ''; + launchd.agents.workspace-cleanup = { + enable = true; + config = { + ProgramArguments = [ + "${workspaces}/bin/ws" + "gc" + ]; + StartInterval = 30; + RunAtLoad = true; + StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/workspaces.log"; + StandardOutPath = "${config.home.homeDirectory}/Library/Logs/workspaces.log"; + }; + }; programs = { zsh.profileExtra = '' _bun() { @@ -9,6 +57,7 @@ ''; }; home.packages = with pkgs; [ + workspaces (bun.overrideAttrs (_: rec { version = "1.3.5"; src = fetchurl { @@ -19,6 +68,7 @@ awscli2 deno doppler + gh rustup ssm-session-manager-plugin terraform -- 2.54.1