diff --git a/herdr/skillshare-mod/RECIPE.md b/herdr/skillshare-mod/RECIPE.md index aca9498a3..aadbd1b0a 100644 --- a/herdr/skillshare-mod/RECIPE.md +++ b/herdr/skillshare-mod/RECIPE.md @@ -57,7 +57,7 @@ This is a standalone mod share folder: `mods/skillshare/`, this recipe and `prev - `main.js` imports `gpui-kit`, `gpui-base` and `herdr`. Its default role draws the panel; `toggle` draws the button. Dialog roles include `install`, `uninstall`, `audit`, `source`, `syncboth`, `conflict`, `upgrade`, `panelupdate`, `text`, `hook` and `able`. - `model.js` parses CLI output and holds shell scripts; `jobs.js` runs detached work; `channel.js` carries requests, locks and notices; `operations.js` runs mutations; `loader.js` reads resources, checks and previews. `version.js` records the release. `mark.svg` and `icons/` supply the art. - Herdr APIs: `exec`, `readTextFile`, `spaces`, `focused`, `machines`, `agents`, `knownAgents`, `layout`, `shared`, `setShared`, `settings`, `theme`, `props`, `openOverlay`, `closeOverlay`, `nextCommand`, `run` and `hc`. Commands: `layout.toggle`, `layout.hide` and `agent.focus`; `hc send` hands off audits. -- Programs: `skillshare`, `git`, `curl`, `sh` and standard filesystem/process tools. skillshare must be installed on every connected machine in `~/.local/bin`, `~/go/bin`, `/opt/homebrew/bin` or `/usr/local/bin`. +- Programs: `skillshare`, `git`, `curl`, `sh` (on Windows, Git for Windows' `bin/sh.exe`) and standard filesystem/process tools. skillshare must be installed on every connected machine in `~/.local/bin`, `~/go/bin`, `/opt/homebrew/bin` or `/usr/local/bin`. - Long work uses detached jobs and polled temp files. Preview reads up to 40 file names and 60 kB per file. MCP env/header/token values are redacted. - Panel updates fetch `herdr/skillshare/mods/skillshare` from the latest published release. Both installation options use that same updater and version. The standalone files are generated from that source with `python3 scripts/herdr/package-mod.py`. diff --git a/herdr/skillshare-mod/mods/skillshare/jobs.js b/herdr/skillshare-mod/mods/skillshare/jobs.js index 163366f99..14ac5375f 100644 --- a/herdr/skillshare-mod/mods/skillshare/jobs.js +++ b/herdr/skillshare-mod/mods/skillshare/jobs.js @@ -3,6 +3,31 @@ // reads in, and the tests pass real sh and fs. import { START, START_SCRIPT, ALIVE, SWEEP, JOB_MINUTES, lastLine } from "./model.js"; +// withShell(program) turns program(command, args, on), which rejects when the command can't +// start, into shell(args, on), which runs `sh args` on machine `on`. Windows has no sh on PATH, +// but Git for Windows' default install puts git there: `git --exec-path` prints +// //libexec/git-core, and /bin/sh.exe sets up the MSYS PATH itself +// (usr/bin/sh.exe would find Windows' own find.exe). The shell that started is kept per machine; +// a failure is not, because the machine may only be unreachable for now. +export function withShell(program) { + const shells = new Map(); + const gitShell = async (on) => { + const git = await program("git", ["--exec-path"], on).catch(() => null); + const m = /^([A-Za-z]:\/.*)\/[^/]+\/libexec\/git-core\/?$/.exec(git?.code === 0 ? git.stdout.trim().replace(/\\/g, "/") : ""); + return m ? `${m[1]}/bin/sh.exe` : null; + }; + return async (args, on = null) => { + const known = shells.get(on); + if (known) return program(known, args, on); + const run = async (sh) => { const r = await program(sh, args, on); shells.set(on, sh); return r; }; + return run("sh").catch(async (e) => { + const sh = await gitShell(on); + if (!sh) throw e; + return run(sh); + }); + }; +} + // createRunner({ exec, readText }): // exec(script, args, on) runs `sh -c script` on machine `on` (null: this computer) and resolves // to { code, stdout, stderr }; it must not reject. Herdr stops an exec after 30 s. diff --git a/herdr/skillshare-mod/mods/skillshare/main.js b/herdr/skillshare-mod/mods/skillshare/main.js index 666b89f11..f7c861ef7 100644 --- a/herdr/skillshare-mod/mods/skillshare/main.js +++ b/herdr/skillshare-mod/mods/skillshare/main.js @@ -14,7 +14,7 @@ import { parseJSON, lastLine, failure, flag, sourceLine, buildItems, shownRows, endedRequests, afterRead, afterChecks, hookArgs, toggleArgs, rowState, previewTargets, eligibleTargets, targetList, waitsForSync, syncScopes, overviewAnswer, resourceProgress, remoteLine, waitingNames, mapLanes, needsBothWays, filterItems, groupItems, sourceGroups, counts, cloneUrl, installResult, newerVersion, panelUpdate, updateState, ago, auditMessage, parseDiscover, parseAudit, } from "./model.js"; -import { createRunner } from "./jobs.js"; +import { createRunner, withShell } from "./jobs.js"; import { createChannel, EVERY } from "./channel.js"; import { operate } from "./operations.js"; import { findProject, loadResources, loadChecks, loadMachine, loadLatestRelease, loadPreview, loadFile } from "./loader.js"; @@ -31,9 +31,11 @@ const PATHS = 'PATH="$HOME/.local/bin:$HOME/go/bin:/opt/homebrew/bin:/usr/local/ // ---- Running skillshare on the space's machine ---- -// Herdr's adapter for the job runner (jobs.js): every script gets the usual PATH. +// Herdr's adapter for the job runner (jobs.js): every script gets the usual PATH, in sh or, on +// Windows, Git for Windows' sh. +const shell = withShell((command, args, on) => exec(command, args, { on })); const runner = createRunner({ - exec: (script, args, on) => exec("sh", ["-c", `${PATHS}\n${script}`, "skillshare-panel", ...args], { on }) + exec: (script, args, on) => shell(["-c", `${PATHS}\n${script}`, "skillshare-panel", ...args], on) .catch((e) => ({ code: 127, stdout: "", stderr: String(e.message ?? e) })), readText: (path, on) => readTextFile(path, { on }), }); diff --git a/herdr/skillshare-mod/mods/skillshare/model.js b/herdr/skillshare-mod/mods/skillshare/model.js index 4972726cc..af544e640 100644 --- a/herdr/skillshare-mod/mods/skillshare/model.js +++ b/herdr/skillshare-mod/mods/skillshare/model.js @@ -15,7 +15,9 @@ export const SCOPES = [["all", "All"], ["project", "Project"], ["global", "Globa const DETACH = 'unset HC_SCOPE; if command -v setsid >/dev/null 2>&1; then bg() { exec setsid "$@"; }; else set -m; bg() { exec "$@"; }; fi'; // Named so SWEEP can tell the panel's job folders from anything else in the temp folder. const TEMP = 'd=$(mktemp -d "${TMPDIR:-/tmp}/skillshare-panel.XXXXXX")'; -const READY = 'i=0; while [ ! -s "$d/pid" ] && [ $i -lt 50 ]; do sleep 0.1; i=$((i+1)); done; printf %s "$d"'; +// The panel reads the folder's files with Herdr, not sh, so under Git for Windows' sh it gets +// the Windows path (cygpath -m: C:/...), which that sh accepts too. +const READY = 'i=0; while [ ! -s "$d/pid" ] && [ $i -lt 50 ]; do sleep 0.1; i=$((i+1)); done; command -v cygpath >/dev/null 2>&1 && d=$(cygpath -m "$d"); printf %s "$d"'; // sh -c START : runs skillshare in ; prints the temp folder. export const START = `${DETACH}; ${TEMP}; dir=$1; shift bg sh -c 'echo $$ >"$2/pid"; cd "$1" 2>/dev/null || cd; d=$2; shift 2; skillshare "$@" >"$d/out" 2>"$d/err" "$d/code"' job "$dir" "$d" "$@" >/dev/null 2>&1 .tmp` in its own folder); + - `sh` (on Windows, which has none on PATH, Git for Windows' `bin/sh.exe`, found through `git --exec-path`; `cygpath` there turns a job's temp folder into the `C:/...` path `readTextFile` can open), `mktemp`, `find` and `rm` for temp folders (`rm -rf` only on folders it made with `mktemp -d`, named `skillshare-panel.*`, and on an unfinished `.bak-update-.tmp` in its own folder); - `cp`, `mkdir`, `mv` and `grep` for a panel update; - `setsid` where it exists, `sleep`, `cat`, `kill -0`, `ps` and `grep` to run long commands detached and check they still run; - `hc send` for the audit hand-off. diff --git a/herdr/skillshare/mods/skillshare/jobs.js b/herdr/skillshare/mods/skillshare/jobs.js index 163366f99..14ac5375f 100644 --- a/herdr/skillshare/mods/skillshare/jobs.js +++ b/herdr/skillshare/mods/skillshare/jobs.js @@ -3,6 +3,31 @@ // reads in, and the tests pass real sh and fs. import { START, START_SCRIPT, ALIVE, SWEEP, JOB_MINUTES, lastLine } from "./model.js"; +// withShell(program) turns program(command, args, on), which rejects when the command can't +// start, into shell(args, on), which runs `sh args` on machine `on`. Windows has no sh on PATH, +// but Git for Windows' default install puts git there: `git --exec-path` prints +// //libexec/git-core, and /bin/sh.exe sets up the MSYS PATH itself +// (usr/bin/sh.exe would find Windows' own find.exe). The shell that started is kept per machine; +// a failure is not, because the machine may only be unreachable for now. +export function withShell(program) { + const shells = new Map(); + const gitShell = async (on) => { + const git = await program("git", ["--exec-path"], on).catch(() => null); + const m = /^([A-Za-z]:\/.*)\/[^/]+\/libexec\/git-core\/?$/.exec(git?.code === 0 ? git.stdout.trim().replace(/\\/g, "/") : ""); + return m ? `${m[1]}/bin/sh.exe` : null; + }; + return async (args, on = null) => { + const known = shells.get(on); + if (known) return program(known, args, on); + const run = async (sh) => { const r = await program(sh, args, on); shells.set(on, sh); return r; }; + return run("sh").catch(async (e) => { + const sh = await gitShell(on); + if (!sh) throw e; + return run(sh); + }); + }; +} + // createRunner({ exec, readText }): // exec(script, args, on) runs `sh -c script` on machine `on` (null: this computer) and resolves // to { code, stdout, stderr }; it must not reject. Herdr stops an exec after 30 s. diff --git a/herdr/skillshare/mods/skillshare/main.js b/herdr/skillshare/mods/skillshare/main.js index 666b89f11..f7c861ef7 100644 --- a/herdr/skillshare/mods/skillshare/main.js +++ b/herdr/skillshare/mods/skillshare/main.js @@ -14,7 +14,7 @@ import { parseJSON, lastLine, failure, flag, sourceLine, buildItems, shownRows, endedRequests, afterRead, afterChecks, hookArgs, toggleArgs, rowState, previewTargets, eligibleTargets, targetList, waitsForSync, syncScopes, overviewAnswer, resourceProgress, remoteLine, waitingNames, mapLanes, needsBothWays, filterItems, groupItems, sourceGroups, counts, cloneUrl, installResult, newerVersion, panelUpdate, updateState, ago, auditMessage, parseDiscover, parseAudit, } from "./model.js"; -import { createRunner } from "./jobs.js"; +import { createRunner, withShell } from "./jobs.js"; import { createChannel, EVERY } from "./channel.js"; import { operate } from "./operations.js"; import { findProject, loadResources, loadChecks, loadMachine, loadLatestRelease, loadPreview, loadFile } from "./loader.js"; @@ -31,9 +31,11 @@ const PATHS = 'PATH="$HOME/.local/bin:$HOME/go/bin:/opt/homebrew/bin:/usr/local/ // ---- Running skillshare on the space's machine ---- -// Herdr's adapter for the job runner (jobs.js): every script gets the usual PATH. +// Herdr's adapter for the job runner (jobs.js): every script gets the usual PATH, in sh or, on +// Windows, Git for Windows' sh. +const shell = withShell((command, args, on) => exec(command, args, { on })); const runner = createRunner({ - exec: (script, args, on) => exec("sh", ["-c", `${PATHS}\n${script}`, "skillshare-panel", ...args], { on }) + exec: (script, args, on) => shell(["-c", `${PATHS}\n${script}`, "skillshare-panel", ...args], on) .catch((e) => ({ code: 127, stdout: "", stderr: String(e.message ?? e) })), readText: (path, on) => readTextFile(path, { on }), }); diff --git a/herdr/skillshare/mods/skillshare/model.js b/herdr/skillshare/mods/skillshare/model.js index 4972726cc..af544e640 100644 --- a/herdr/skillshare/mods/skillshare/model.js +++ b/herdr/skillshare/mods/skillshare/model.js @@ -15,7 +15,9 @@ export const SCOPES = [["all", "All"], ["project", "Project"], ["global", "Globa const DETACH = 'unset HC_SCOPE; if command -v setsid >/dev/null 2>&1; then bg() { exec setsid "$@"; }; else set -m; bg() { exec "$@"; }; fi'; // Named so SWEEP can tell the panel's job folders from anything else in the temp folder. const TEMP = 'd=$(mktemp -d "${TMPDIR:-/tmp}/skillshare-panel.XXXXXX")'; -const READY = 'i=0; while [ ! -s "$d/pid" ] && [ $i -lt 50 ]; do sleep 0.1; i=$((i+1)); done; printf %s "$d"'; +// The panel reads the folder's files with Herdr, not sh, so under Git for Windows' sh it gets +// the Windows path (cygpath -m: C:/...), which that sh accepts too. +const READY = 'i=0; while [ ! -s "$d/pid" ] && [ $i -lt 50 ]; do sleep 0.1; i=$((i+1)); done; command -v cygpath >/dev/null 2>&1 && d=$(cygpath -m "$d"); printf %s "$d"'; // sh -c START : runs skillshare in ; prints the temp folder. export const START = `${DETACH}; ${TEMP}; dir=$1; shift bg sh -c 'echo $$ >"$2/pid"; cd "$1" 2>/dev/null || cd; d=$2; shift 2; skillshare "$@" >"$d/out" 2>"$d/err" "$d/code"' job "$dir" "$d" "$@" >/dev/null 2>&1 rmSync(root, { recursive: true, force: true })); writeFileSync(join(root, "skillshare"), `#!/bin/sh\n${body}\n`, { mode: 0o755 }); + if (opts.cygpath) writeFileSync(join(root, "cygpath"), `#!/bin/sh\n${opts.cygpath(root)}\n`, { mode: 0o755 }); const env = { ...process.env, PATH: `${root}:${process.env.PATH}`, TMPDIR: root }; - const scripts = []; + const scripts = [], reads = []; const exec = (script, args, on) => new Promise((resolve) => { scripts.push({ script, on }); const sh = spawn("sh", ["-c", script, "test", ...args], { env, detached: true }); @@ -27,8 +28,8 @@ function fakeRunner(t, body, opts = {}) { resolve({ code: code ?? 1, stdout, stderr }); }); }); - const readText = (path) => readFile(path, "utf8").catch(() => null); - return { runner: createRunner({ exec, readText, pollMs: 20, aliveEvery: 2, ...opts }), scripts }; + const readText = (path) => { reads.push(path); return readFile(path, "utf8").catch(() => null); }; + return { runner: createRunner({ exec, readText, pollMs: 20, aliveEvery: 2, ...opts }), scripts, reads, root }; } const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); @@ -56,3 +57,48 @@ test("the first job on each machine sweeps old job folders once", async (t) => { await runner.job(["c"], { on: "other", sleep }); assert.deepEqual(scripts.filter((s) => s.script === SWEEP).map((s) => s.on), [null, "other"]); }); + +test("where cygpath exists, the job's files are read at the path it prints", async (t) => { + // Git for Windows' sh: Herdr reads files natively, so it needs C:/... rather than /tmp/... + const alias = mkdtempSync(join(tmpdir(), "ss-alias-")); + t.after(() => rmSync(alias, { recursive: true, force: true })); + const { runner, reads, root } = fakeRunner(t, "echo ok", { cygpath: (r) => `printf %s "$2" | sed "s|^${r}|${alias}/root|"` }); + symlinkSync(root, join(alias, "root")); + const r = await runner.job(["list"], { sleep }); + assert.deepEqual([r.stdout, reads.every((p) => p.startsWith(`${alias}/root/`))], ["ok\n", true]); +}); + +// A Herdr exec on a Windows machine: sh can't start, git is on PATH. +function windowsProgram(calls, gitPath = "C:/Program Files/Git/ucrt64/libexec/git-core\r\n") { + return async (command, args, on) => { + calls.push([command, on]); + if (command === "sh") throw new Error("cannot start sh: program not found"); + if (command === "git") return { code: 0, stdout: gitPath, stderr: "" }; + return { code: 0, stdout: args.join(" "), stderr: "" }; + }; +} + +test("without sh, a script runs in Git for Windows' bin/sh.exe", async () => { + const calls = []; + const r = await withShell(windowsProgram(calls))(["-c", "echo hi"], "win"); + assert.deepEqual([calls.at(-1), r.stdout], [["C:/Program Files/Git/bin/sh.exe", "win"], "-c echo hi"]); +}); + +test("where sh starts, git is never asked", async () => { + const calls = []; + const shell = withShell(async (command, args, on) => { calls.push(command); return { code: 0, stdout: "", stderr: "" }; }); + for (const on of [null, null, "linux"]) await shell(["-c", ":"], on); + assert.deepEqual(calls, ["sh", "sh", "sh"]); +}); + +test("each machine looks for its shell once", async () => { + const calls = []; + const shell = withShell(windowsProgram(calls)); + for (const on of ["win", "win", null]) await shell(["-c", ":"], on); + assert.deepEqual(calls.filter(([c]) => c === "git").map(([, on]) => on), ["win", null]); +}); + +test("without sh or Git for Windows, the reason sh could not start stands", async () => { + const shell = withShell(windowsProgram([], "/usr/lib/git-core\n")); + await assert.rejects(shell(["-c", ":"]), /cannot start sh/); +});