Files
worktrunk/dev/omp-hook.ts
T
Worktrunk Botandworktrunk-bot 3bf9166a6e fix(plugins): skip Windows Terminal's wt in the pi, omp, and OpenCode plugins (#4698)
The pi, oh-my-pi, and OpenCode plugins ran a bare `wt`. On Windows that
name usually resolves to Windows Terminal's app-execution alias in
`%LOCALAPPDATA%\Microsoft\WindowsApps`. The alias exits 0 and reports
the unknown program in a popup, so every activity-marker update opened
an error window. OpenCode's worktree `switch`/`remove` strategy also
never reached worktrunk. The Claude Code and Codex hooks didn't have
this problem because they run through `plugins/worktrunk/hooks/wt.sh`,
which already handles this case.

Each plugin now has a small `worktrunkBin()` that follows `wt.sh`'s
policy: use `WORKTRUNK_BIN` if set, otherwise use `wt` off Windows. On
Windows, it walks `PATH` in order and prefers `git-wt.exe`, then accepts
a `wt.exe` only if the first one found isn't under `WindowsApps`. When
nothing usable is found, marker updates are skipped, and OpenCode's
strategy throws an error naming `git-wt.exe` and `WORKTRUNK_BIN`. The
helper is copied into all three files because each one is installed as a
standalone file.

Verification: dev/*.ts has no test harness, so I checked locally with
Node 22's type stripping. The check fakes `process.platform = "win32"`,
builds a `PATH` with a `WindowsApps/wt.exe` ahead of a real `wt.exe`,
and stubs `pi.exec`. Before this change, both the pi and omp plugins
exec `wt` (the Terminal alias). After it, they skip; they pick
`git-wt.exe` when it's present and `wt` when the real binary comes first
on `PATH`. OpenCode's strategy surfaces the new error. The existing
plugin install/config integration tests pass.

**Not verified on Windows:** I couldn't run a real Windows host. `lstat`
is used instead of `exists` so the alias's reparse point still counts as
present, which keeps the search order the same as Windows' own lookup.
It's worth a check on a Windows machine with the Terminal alias enabled.

The issue's other suggestion is still open: warning when a `wt` on PATH
turns out not to be worktrunk. That needs a choice about where a plugin
should report it, so it's not in this PR.

Closes #4697 — automated triage

Co-authored-by: worktrunk-bot <254187624+worktrunk-bot@users.noreply.github.com>
2026-11-04 06:07:54 -08:00

73 lines
2.4 KiB
TypeScript

// Worktrunk activity tracking hook for oh-my-pi (`omp`).
//
// Tracks agent activity per branch, showing status markers in `wt list`:
// 🤖 — agent is working
// 💬 — agent is waiting for input
//
// Installed globally via: wt config plugins omp install
//
// oh-my-pi loads user hooks from `hooks/pre/*.ts` and exposes `HookAPI`.
// earendil-works Pi is a different agent with a different loader and API —
// see `dev/pi-extension.ts` and `wt config plugins pi install`.
import { lstatSync } from "node:fs";
import { delimiter, join } from "node:path";
import type { HookAPI } from "@oh-my-pi/pi-coding-agent/extensibility/hooks";
// The worktrunk binary, chosen as `plugins/worktrunk/hooks/wt.sh` chooses it.
// On Windows, `wt` is usually Windows Terminal's app-execution alias, which
// accepts any arguments, exits 0, and reports the unknown program in a popup
// window. So prefer `git-wt.exe` there, and never run a `wt` from WindowsApps.
// `lstat` rather than `exists`: the alias is a reparse point, and it has to be
// seen for the lookup to stop where Windows' own PATH search would.
// `undefined` means no worktrunk binary was found.
function worktrunkBin(): string | undefined {
if (process.env.WORKTRUNK_BIN) return process.env.WORKTRUNK_BIN;
if (process.platform !== "win32") return "wt";
const dirs = (process.env.PATH ?? "").split(delimiter).filter(Boolean);
const find = (name: string) =>
dirs
.map((dir) => join(dir, name))
.find((path) => {
try {
lstatSync(path);
return true;
} catch {
return false;
}
});
if (find("git-wt.exe")) return "git-wt.exe";
const wt = find("wt.exe");
return wt && !/[\\/]WindowsApps[\\/]/i.test(wt) ? "wt" : undefined;
}
export default function worktrunkActivity(pi: HookAPI): void {
const run = async (
ctx: { cwd: string },
args: ["set", string] | ["clear"],
): Promise<void> => {
const bin = worktrunkBin();
if (!bin) return;
try {
await pi.exec(bin, ["config", "state", "marker", ...args], {
cwd: ctx.cwd,
});
} catch {
// Activity tracking must never interrupt the host Pi session.
}
};
pi.on("agent_start", async (_event, ctx) => {
await run(ctx, ["set", "🤖"]);
});
pi.on("agent_end", async (_event, ctx) => {
await run(ctx, ["set", "💬"]);
});
pi.on("session_shutdown", async (_event, ctx) => {
await run(ctx, ["clear"]);
});
}