@srobroek/pi-worktrunk-merge-policy 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +11 -23
- package/extensions/worktrunk.js +42 -319
- package/package.json +1 -1
- package/skills/worktrunk-worktrees/SKILL.md +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## worktrunk [1.0.2](https://github.com/srobroek/harness-plugins/compare/worktrunk--v1.0.1...worktrunk--v1.0.2) (2026-12-05)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
* **lib:** settle a subprocess whose pipe a descendant holds, and verify cut-off mutations ([#342](https://github.com/srobroek/harness-plugins/issues/342)) ([8fd6bd8](https://github.com/srobroek/harness-plugins/commit/8fd6bd8df1c19fab2618a0965c620093aa1444c6))
|
|
6
|
+
|
|
7
|
+
## worktrunk [1.0.1](https://github.com/srobroek/harness-plugins/compare/worktrunk--v1.0.0...worktrunk--v1.0.1) (2026-12-04)
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* retire beads, worktrunk, trunk-dev and chezmoi gates (guidance only) ([#294](https://github.com/srobroek/harness-plugins/issues/294)) ([19eab97](https://github.com/srobroek/harness-plugins/commit/19eab97fee1e3eb8719838e4ba2a9a8d87c3ea9e))
|
|
12
|
+
|
|
1
13
|
## worktrunk [1.0.0](https://github.com/srobroek/harness-plugins/compare/worktrunk--v0.8.0...worktrunk--v1.0.0) (2026-12-04)
|
|
2
14
|
|
|
3
15
|
### ⚠ BREAKING CHANGES
|
package/README.md
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
omp-plugins `worktrunk` package. The omp isolation precheck is not ported: OpenCode has no native
|
|
5
5
|
checkout-cloning isolation to refuse.
|
|
6
6
|
|
|
7
|
-
It does three things:
|
|
7
|
+
It does three things, and refuses no shell call:
|
|
8
8
|
|
|
9
9
|
- Registers `wt` as OpenCode's worktree strategy.
|
|
10
|
-
-
|
|
10
|
+
- Raises the timeout of a short `wt switch` call so foreground hooks finish.
|
|
11
11
|
- Ships two skills: `worktrunk-preflight` and `worktrunk-worktrees`.
|
|
12
12
|
|
|
13
13
|
## Install
|
|
@@ -29,7 +29,7 @@ claude plugin install worktrunk@harness-plugins
|
|
|
29
29
|
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
Claude Code get the two skills only; the worktree strategy and
|
|
32
|
+
Claude Code get the two skills only; the worktree strategy and the `wt switch` timeout are OpenCode code.
|
|
33
33
|
|
|
34
34
|
Needs `wt` and `git` on `PATH`. The preflight skill also needs `python3` (3.11 or later).
|
|
35
35
|
|
|
@@ -37,7 +37,7 @@ Needs `wt` and `git` on `PATH`. The preflight skill also needs `python3` (3.11 o
|
|
|
37
37
|
|
|
38
38
|
| Option | Default | Effect |
|
|
39
39
|
| --- | --- | --- |
|
|
40
|
-
| `strategy` | `true` | `false` keeps OpenCode's bundled Git strategy. The
|
|
40
|
+
| `strategy` | `true` | `false` keeps OpenCode's bundled Git strategy. The `wt switch` timeout and the skills stay on. |
|
|
41
41
|
|
|
42
42
|
```jsonc
|
|
43
43
|
{ "plugins": [{ "package": "...", "options": { "strategy": false } }] }
|
|
@@ -66,22 +66,11 @@ restores the previous one. Worktrees created before the plugin loaded keep their
|
|
|
66
66
|
Removing a directory that no longer exists does nothing.
|
|
67
67
|
- `list` reports the main worktree as `root` and every linked worktree as `worktree`.
|
|
68
68
|
|
|
69
|
-
## Merge
|
|
69
|
+
## Merge guidance
|
|
70
70
|
|
|
71
|
-
`
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
`--no-ff`;
|
|
75
|
-
- such a merge run from the target worktree, or from a detached `HEAD`, because `wt merge`
|
|
76
|
-
merges the current branch;
|
|
77
|
-
- a merge it cannot check: the default branch cannot be resolved, a probe times out, or the
|
|
78
|
-
command is unterminated or runs a merge stored in a variable (`cmd="wt merge x"; eval "$cmd"`).
|
|
79
|
-
|
|
80
|
-
`wt merge` with no target and merges to the default branch pass. The gate reads the default branch
|
|
81
|
-
from `wt config state default-branch`, then `origin/HEAD`, in the call's directory: the shell
|
|
82
|
-
`workdir` (resolved against the session directory), a leading `cd DIR &&`, or `wt -C DIR`. It
|
|
83
|
-
finds merges behind wrappers (`env`, `sudo`, `timeout`, ...), in `$(...)`, backticks, process
|
|
84
|
-
substitutions, `eval` and `sh -c`. `git merge` and `gh pr merge` are not gated.
|
|
71
|
+
The plugin no longer checks `wt merge` calls. The `worktrunk-worktrees` skill carries the rule as
|
|
72
|
+
guidance: a worker-to-epic merge passes `--no-squash --no-ff` to keep per-commit history, and
|
|
73
|
+
`wt merge` runs from the source worktree.
|
|
85
74
|
|
|
86
75
|
## Skills
|
|
87
76
|
|
|
@@ -99,19 +88,18 @@ resolve it as `../skills`. A skill with the same id that the host already has wi
|
|
|
99
88
|
|
|
100
89
|
## pi
|
|
101
90
|
|
|
102
|
-
One extension, `pi/extensions/worktrunk.js` (source `pi-src/worktrunk.ts`). The
|
|
103
|
-
`wt switch` timeout
|
|
91
|
+
One extension, `pi/extensions/worktrunk.js` (source `pi-src/worktrunk.ts`). The
|
|
92
|
+
`wt switch` timeout shares `shared/switch-timeout.ts` with OpenCode.
|
|
104
93
|
|
|
105
94
|
| Feature | pi hook | Behavior |
|
|
106
95
|
| --- | --- | --- |
|
|
107
|
-
| Merge policy gate | `tool_call` on `bash` | Returns `{ block: true, reason }` for every merge the OpenCode gate refuses, checked in `ctx.cwd`. A handler error blocks the call, as pi treats a throwing `tool_call` handler. |
|
|
108
96
|
| `wt switch` timeout | `tool_call` on `bash` | Raises a `timeout` shorter than 600 s to 600 s on a `wt switch` call, so foreground hooks are not killed mid-provisioning. pi's bash tool has no default timeout, so a call without one is left alone. |
|
|
109
97
|
| `worktrunk_preflight` tool | `registerTool` | The checks of `preflight.py`, ported to TypeScript in `shared/preflight.ts` with the same ids, statuses and `{ ok, summary, checks }` JSON. Parameters: `path` (default: the session directory), `only` (check ids), `apply`. `apply` writes `.config/wt.toml` under pi's file-mutation queue. |
|
|
110
98
|
|
|
111
99
|
The worktree strategy has no pi counterpart: pi has no worktree-strategy hook. pi-subagents allocates
|
|
112
100
|
its child worktrees itself, running `wt -C <root> switch --create <branch> --base <commit> --no-cd
|
|
113
101
|
--no-hooks --format json` with `spawnSync` from its extension process, not through the bash tool.
|
|
114
|
-
pi fires `tool_call` only for tool calls, so
|
|
102
|
+
pi fires `tool_call` only for tool calls, so the timeout hook does not see that allocation.
|
|
115
103
|
|
|
116
104
|
pi-subagents' foreground children do not load the parent's extensions ([required host extensions](https://github.com/nicobailon/pi-subagents/blob/main/docs/agents.md#required-host-extensions)). List this extension for every child in `~/.pi/agent/settings.json`, with the path where pi installed this repository:
|
|
117
105
|
|
package/extensions/worktrunk.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// Generated by scripts/build.ts. Do not edit.
|
|
3
3
|
// plugins/worktrunk/pi-src/worktrunk.ts
|
|
4
|
-
import { isAbsolute as isAbsolute2, resolve as
|
|
4
|
+
import { isAbsolute as isAbsolute2, resolve as resolve2 } from "path";
|
|
5
5
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
6
6
|
import {
|
|
7
7
|
isToolCallEventType,
|
|
@@ -9,6 +9,11 @@ import {
|
|
|
9
9
|
} from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import { Type } from "typebox";
|
|
11
11
|
|
|
12
|
+
// plugins/worktrunk/shared/preflight.ts
|
|
13
|
+
import { accessSync, constants as constants2, existsSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } from "fs";
|
|
14
|
+
import { homedir } from "os";
|
|
15
|
+
import { dirname, isAbsolute, join, resolve } from "path";
|
|
16
|
+
|
|
12
17
|
// lib/src/agent-tools.ts
|
|
13
18
|
var MCP_TOOL_AGENTS = new Set([
|
|
14
19
|
"adversarial-challenger",
|
|
@@ -1400,18 +1405,6 @@ var HAS_WELLFORMED = !!"".isWellFormed;
|
|
|
1400
1405
|
// lib/src/patterns.ts
|
|
1401
1406
|
var compiled = new Map;
|
|
1402
1407
|
// lib/src/shell-command.ts
|
|
1403
|
-
import { resolve } from "path";
|
|
1404
|
-
var LITERAL_CD = /^\s*cd[ \t]+([A-Za-z0-9_@%+=:,./-]+)[ \t]*(?:&&|$)/;
|
|
1405
|
-
var STARTS_WITH_CD = /^\s*(?:cd|pushd|popd)(?=\s|$|[;&|<>()])/;
|
|
1406
|
-
function leadingCd(command, cwd) {
|
|
1407
|
-
if (!STARTS_WITH_CD.test(command))
|
|
1408
|
-
return { kind: "none" };
|
|
1409
|
-
const match = LITERAL_CD.exec(command);
|
|
1410
|
-
const dir = match?.[1];
|
|
1411
|
-
if (match === null || dir === undefined || dir.startsWith("-") || dir.split("/").includes(".."))
|
|
1412
|
-
return { kind: "rejected" };
|
|
1413
|
-
return { kind: "dir", dir: resolve(cwd, dir), length: match[0].length };
|
|
1414
|
-
}
|
|
1415
1408
|
function scanShell(source, options = {}) {
|
|
1416
1409
|
const items = [];
|
|
1417
1410
|
const substitutions = [];
|
|
@@ -1798,6 +1791,8 @@ var INTERPRETERS = new Set(["bash", "sh", "zsh", "dash", "ksh"]);
|
|
|
1798
1791
|
import { constants } from "os";
|
|
1799
1792
|
var EXIT_GRACE_MS = 2000;
|
|
1800
1793
|
var EXIT_POLL_MS = 100;
|
|
1794
|
+
var PIPE_DRAIN_MS = 2000;
|
|
1795
|
+
var DRAIN_EXTRA_TURNS = 2;
|
|
1801
1796
|
function goneWatch(graceMs, pollMs) {
|
|
1802
1797
|
const polls = Math.max(1, Math.ceil(graceMs / Math.max(1, pollMs)));
|
|
1803
1798
|
let since = null;
|
|
@@ -1864,6 +1859,7 @@ function capture(stream) {
|
|
|
1864
1859
|
}
|
|
1865
1860
|
function supervise(child, options = {}) {
|
|
1866
1861
|
const grace = Math.max(0, options.graceMs ?? EXIT_GRACE_MS);
|
|
1862
|
+
const drainMs = Math.max(0, options.drainMs ?? PIPE_DRAIN_MS);
|
|
1867
1863
|
const pollMs = Math.max(1, options.pollMs ?? EXIT_POLL_MS);
|
|
1868
1864
|
const alive = options.alive ?? processAlive;
|
|
1869
1865
|
const group = options.group ?? false;
|
|
@@ -1877,6 +1873,7 @@ function supervise(child, options = {}) {
|
|
|
1877
1873
|
const gone = goneWatch(grace, pollMs);
|
|
1878
1874
|
let deadline;
|
|
1879
1875
|
let graceTimer;
|
|
1876
|
+
let drainTimer;
|
|
1880
1877
|
const settle = (run) => {
|
|
1881
1878
|
if (settled)
|
|
1882
1879
|
return;
|
|
@@ -1884,6 +1881,7 @@ function supervise(child, options = {}) {
|
|
|
1884
1881
|
clearTimeout(deadline);
|
|
1885
1882
|
clearTimeout(graceTimer);
|
|
1886
1883
|
clearInterval(poller);
|
|
1884
|
+
clearTimeout(drainTimer);
|
|
1887
1885
|
signal?.removeEventListener("abort", onAbort);
|
|
1888
1886
|
resolve(run);
|
|
1889
1887
|
};
|
|
@@ -1907,7 +1905,8 @@ function supervise(child, options = {}) {
|
|
|
1907
1905
|
end("lost");
|
|
1908
1906
|
}, pollMs);
|
|
1909
1907
|
unref(poller);
|
|
1910
|
-
|
|
1908
|
+
const bounded = timeoutMs !== undefined && timeoutMs > 0 && Number.isFinite(timeoutMs);
|
|
1909
|
+
if (bounded) {
|
|
1911
1910
|
deadline = setTimeout(() => end("timeout"), timeoutMs);
|
|
1912
1911
|
if (options.unref)
|
|
1913
1912
|
unref(deadline);
|
|
@@ -1915,8 +1914,28 @@ function supervise(child, options = {}) {
|
|
|
1915
1914
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1916
1915
|
if (signal?.aborted)
|
|
1917
1916
|
onAbort();
|
|
1918
|
-
child.exited.then(() => {
|
|
1917
|
+
child.exited.then((exitCode) => {
|
|
1919
1918
|
exitSeen = true;
|
|
1919
|
+
if (bounded || ended !== null || settled)
|
|
1920
|
+
return;
|
|
1921
|
+
let turns = 0;
|
|
1922
|
+
const drain = () => {
|
|
1923
|
+
if (ended !== null || settled)
|
|
1924
|
+
return;
|
|
1925
|
+
if (turns++ < DRAIN_EXTRA_TURNS) {
|
|
1926
|
+
drainTimer = setTimeout(drain, 0);
|
|
1927
|
+
if (options.unref)
|
|
1928
|
+
unref(drainTimer);
|
|
1929
|
+
return;
|
|
1930
|
+
}
|
|
1931
|
+
out.cancel();
|
|
1932
|
+
err.cancel();
|
|
1933
|
+
const signalCode = signalName(child.signalCode);
|
|
1934
|
+
settle({ kind: "incomplete", exitCode, signalCode, stdout: out.snapshot(), stderr: err.snapshot() });
|
|
1935
|
+
};
|
|
1936
|
+
drainTimer = setTimeout(drain, drainMs);
|
|
1937
|
+
if (options.unref)
|
|
1938
|
+
unref(drainTimer);
|
|
1920
1939
|
}, () => {
|
|
1921
1940
|
return;
|
|
1922
1941
|
});
|
|
@@ -1940,296 +1959,7 @@ var WRITE_TOOLS = new Set(["edit", "write", "patch"]);
|
|
|
1940
1959
|
var READ_TOOLS = new Set(["read"]);
|
|
1941
1960
|
var SHELL_ALIASES = new Set(["bash", "Bash"]);
|
|
1942
1961
|
var SHELL_EXECUTABLES = new Set(["bash", "sh", "zsh", "dash", "ksh"]);
|
|
1943
|
-
// lib/src/safe-git.ts
|
|
1944
|
-
import { devNull } from "os";
|
|
1945
|
-
var SAFE_GIT_CONFIG = [
|
|
1946
|
-
["core.fsmonitor", "false"],
|
|
1947
|
-
["core.hooksPath", devNull]
|
|
1948
|
-
];
|
|
1949
|
-
var SAFE_GIT_ARGS = [...SAFE_GIT_CONFIG.flatMap(([key, value]) => ["-c", `${key}=${value}`]), "--no-optional-locks"];
|
|
1950
|
-
var HARDENED_GIT_CONFIG = [
|
|
1951
|
-
...SAFE_GIT_CONFIG,
|
|
1952
|
-
["core.attributesFile", devNull],
|
|
1953
|
-
["log.showSignature", "false"],
|
|
1954
|
-
["gpg.program", devNull],
|
|
1955
|
-
["gpg.ssh.program", devNull],
|
|
1956
|
-
["gpg.x509.program", devNull],
|
|
1957
|
-
["commit.gpgSign", "false"],
|
|
1958
|
-
["tag.gpgSign", "false"],
|
|
1959
|
-
["gpg.ssh.defaultKeyCommand", ""],
|
|
1960
|
-
["core.sshCommand", ""],
|
|
1961
|
-
["credential.helper", ""],
|
|
1962
|
-
["diff.external", ""]
|
|
1963
|
-
];
|
|
1964
|
-
var EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
|
|
1965
|
-
var INHERITED_GIT_VARIABLES = [
|
|
1966
|
-
"GIT_DIR",
|
|
1967
|
-
"GIT_WORK_TREE",
|
|
1968
|
-
"GIT_COMMON_DIR",
|
|
1969
|
-
"GIT_INDEX_FILE",
|
|
1970
|
-
"GIT_OBJECT_DIRECTORY",
|
|
1971
|
-
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
|
|
1972
|
-
"GIT_PREFIX",
|
|
1973
|
-
"GIT_NAMESPACE",
|
|
1974
|
-
"GIT_REPLACE_REF_BASE",
|
|
1975
|
-
"GIT_GRAFT_FILE",
|
|
1976
|
-
"GIT_SHALLOW_FILE",
|
|
1977
|
-
"GIT_CONFIG",
|
|
1978
|
-
"GIT_EXEC_PATH",
|
|
1979
|
-
"GIT_EXTERNAL_DIFF",
|
|
1980
|
-
"GIT_PAGER",
|
|
1981
|
-
"GIT_EDITOR",
|
|
1982
|
-
"GIT_SEQUENCE_EDITOR",
|
|
1983
|
-
"GIT_SSH",
|
|
1984
|
-
"GIT_SSH_COMMAND",
|
|
1985
|
-
"GIT_ASKPASS",
|
|
1986
|
-
"GIT_PROXY_COMMAND",
|
|
1987
|
-
"GIT_LITERAL_PATHSPECS",
|
|
1988
|
-
"GIT_GLOB_PATHSPECS",
|
|
1989
|
-
"GIT_NOGLOB_PATHSPECS",
|
|
1990
|
-
"GIT_ICASE_PATHSPECS"
|
|
1991
|
-
];
|
|
1992
|
-
var PROGRAM_VARIABLES = ["PAGER", "EDITOR", "VISUAL", "SSH_ASKPASS"];
|
|
1993
|
-
function hardenedGitEnv(base) {
|
|
1994
|
-
const env = {};
|
|
1995
|
-
const dropped = new Set([...INHERITED_GIT_VARIABLES, ...PROGRAM_VARIABLES]);
|
|
1996
|
-
for (const [key, value] of Object.entries(base)) {
|
|
1997
|
-
if (value === undefined || dropped.has(key) || key.startsWith("GIT_TRACE"))
|
|
1998
|
-
continue;
|
|
1999
|
-
if (key === "GIT_CONFIG_PARAMETERS" || key === "GIT_CONFIG_COUNT" || /^GIT_CONFIG_(KEY|VALUE)_\d+$/.test(key))
|
|
2000
|
-
continue;
|
|
2001
|
-
env[key] = value;
|
|
2002
|
-
}
|
|
2003
|
-
let count = 0;
|
|
2004
|
-
for (const [key, value] of HARDENED_GIT_CONFIG) {
|
|
2005
|
-
env[`GIT_CONFIG_KEY_${count}`] = key;
|
|
2006
|
-
env[`GIT_CONFIG_VALUE_${count}`] = value;
|
|
2007
|
-
count += 1;
|
|
2008
|
-
}
|
|
2009
|
-
env.GIT_CONFIG_COUNT = String(count);
|
|
2010
|
-
env.GIT_ATTR_SOURCE = EMPTY_TREE;
|
|
2011
|
-
env.GIT_OPTIONAL_LOCKS = "0";
|
|
2012
|
-
env.GIT_NO_LAZY_FETCH = "1";
|
|
2013
|
-
env.GIT_ALLOW_PROTOCOL = "";
|
|
2014
|
-
return env;
|
|
2015
|
-
}
|
|
2016
|
-
|
|
2017
|
-
// plugins/worktrunk/shared/merge-gate.ts
|
|
2018
|
-
var MERGE_RETRY = "wt merge <target> --no-squash --no-ff";
|
|
2019
|
-
var SOURCE_WORKTREE_RETRY = `${MERGE_RETRY} from the source worktree`;
|
|
2020
|
-
var MERGE_POLICY_REFUSAL = `worker-to-epic merges must preserve history; retry with ${MERGE_RETRY}`;
|
|
2021
|
-
var OPAQUE_DYNAMIC_MERGE_REFUSAL = "opaque dynamic shell body names a merge or land command; retry with a literal command";
|
|
2022
|
-
var UNPARSEABLE_MERGE_REFUSAL = "the command has an unterminated quote, substitution or here-document and names wt merge, so the merge cannot be checked; retry with a complete literal command";
|
|
2023
|
-
var UNKNOWN_DIRECTORY_MERGE_REFUSAL = "the command starts with a cd this gate cannot read (a quoted, expanded, `..` or redirected directory), so the repository the merge runs in is unknown; retry with `cd <literal path> && wt merge \u2026` or `wt -C <absolute path> merge \u2026`";
|
|
2024
|
-
var SHELLS = new Set(["bash", "sh", "zsh", "dash", "ksh"]);
|
|
2025
|
-
var GLOBAL_VALUE_OPTIONS = new Set(["-C", "--config", "--config-set"]);
|
|
2026
|
-
var MERGE_VALUE_OPTIONS = new Set(["--stage", "--format", "--config", "--config-set", "-C"]);
|
|
2027
|
-
var ASSIGNMENT2 = /^([A-Za-z_]\w*)=([\s\S]*)$/;
|
|
2028
|
-
var MAX_DEPTH = 8;
|
|
2029
|
-
function base(word) {
|
|
2030
|
-
return word?.split("/").pop() ?? "";
|
|
2031
|
-
}
|
|
2032
|
-
function parseMergeInvocation(argv) {
|
|
2033
|
-
const wtIndex = argv.findIndex((word) => base(word) === "wt");
|
|
2034
|
-
if (wtIndex < 0)
|
|
2035
|
-
return null;
|
|
2036
|
-
let subIndex = wtIndex + 1;
|
|
2037
|
-
let workdir = null;
|
|
2038
|
-
while (subIndex < argv.length && argv[subIndex]?.startsWith("-")) {
|
|
2039
|
-
const option = argv[subIndex] ?? "";
|
|
2040
|
-
if (option === "-C")
|
|
2041
|
-
workdir = argv[subIndex + 1] ?? null;
|
|
2042
|
-
subIndex += GLOBAL_VALUE_OPTIONS.has(option) ? 2 : 1;
|
|
2043
|
-
}
|
|
2044
|
-
if (argv[subIndex] !== "merge")
|
|
2045
|
-
return null;
|
|
2046
|
-
if (argv.includes("--help") || argv.includes("-h"))
|
|
2047
|
-
return null;
|
|
2048
|
-
let target = null;
|
|
2049
|
-
let noSquash = false;
|
|
2050
|
-
let noFf = false;
|
|
2051
|
-
for (let i = subIndex + 1;i < argv.length; i++) {
|
|
2052
|
-
const arg = argv[i];
|
|
2053
|
-
if (arg === "--no-squash") {
|
|
2054
|
-
noSquash = true;
|
|
2055
|
-
continue;
|
|
2056
|
-
}
|
|
2057
|
-
if (arg === "--no-ff") {
|
|
2058
|
-
noFf = true;
|
|
2059
|
-
continue;
|
|
2060
|
-
}
|
|
2061
|
-
if (arg === "--") {
|
|
2062
|
-
target = argv[i + 1] ?? null;
|
|
2063
|
-
break;
|
|
2064
|
-
}
|
|
2065
|
-
if (!arg)
|
|
2066
|
-
continue;
|
|
2067
|
-
if (arg.startsWith("-") || MERGE_VALUE_OPTIONS.has(argv[i - 1] ?? ""))
|
|
2068
|
-
continue;
|
|
2069
|
-
if (target === null)
|
|
2070
|
-
target = arg;
|
|
2071
|
-
}
|
|
2072
|
-
return { target, noSquash, noFf, workdir };
|
|
2073
|
-
}
|
|
2074
|
-
function namesMergeCommand(source) {
|
|
2075
|
-
for (const item of scanShell(source).items) {
|
|
2076
|
-
if (item.kind !== "command")
|
|
2077
|
-
continue;
|
|
2078
|
-
const argv = commandWords(item.words.map((word) => word.value)).argv;
|
|
2079
|
-
const executable = base(argv[0]);
|
|
2080
|
-
if (executable === "wt" && parseMergeInvocation(argv) !== null)
|
|
2081
|
-
return true;
|
|
2082
|
-
if (executable === "git" && argv[1] === "merge")
|
|
2083
|
-
return true;
|
|
2084
|
-
if (executable === "gh" && argv[1] === "pr" && argv[2] === "merge")
|
|
2085
|
-
return true;
|
|
2086
|
-
}
|
|
2087
|
-
return false;
|
|
2088
|
-
}
|
|
2089
|
-
function references(text, names) {
|
|
2090
|
-
for (const match of text.matchAll(/\$(?:\{([A-Za-z_]\w*)[^}]*\}|([A-Za-z_]\w*))/g)) {
|
|
2091
|
-
if (names.has(match[1] ?? match[2] ?? ""))
|
|
2092
|
-
return true;
|
|
2093
|
-
}
|
|
2094
|
-
return false;
|
|
2095
|
-
}
|
|
2096
|
-
function collectCommands(source) {
|
|
2097
|
-
const collected = { commands: [], executed: [], mergeVariables: new Set, complete: true };
|
|
2098
|
-
const visit = (text, evaluated, depth) => {
|
|
2099
|
-
if (depth > MAX_DEPTH) {
|
|
2100
|
-
collected.complete = false;
|
|
2101
|
-
return;
|
|
2102
|
-
}
|
|
2103
|
-
const scan = scanShell(text);
|
|
2104
|
-
if (!scan.complete)
|
|
2105
|
-
collected.complete = false;
|
|
2106
|
-
for (const item of scan.items) {
|
|
2107
|
-
if (item.kind !== "command")
|
|
2108
|
-
continue;
|
|
2109
|
-
const values = item.words.map((word) => word.value);
|
|
2110
|
-
for (const value of values) {
|
|
2111
|
-
const assignment = ASSIGNMENT2.exec(value);
|
|
2112
|
-
if (!assignment)
|
|
2113
|
-
break;
|
|
2114
|
-
if (namesMergeCommand(assignment[2] ?? ""))
|
|
2115
|
-
collected.mergeVariables.add(assignment[1] ?? "");
|
|
2116
|
-
}
|
|
2117
|
-
const argv = commandWords(values).argv;
|
|
2118
|
-
if (argv.length === 0)
|
|
2119
|
-
continue;
|
|
2120
|
-
collected.commands.push(argv);
|
|
2121
|
-
if (evaluated)
|
|
2122
|
-
collected.executed.push(...argv);
|
|
2123
|
-
else if (argv[0] !== undefined)
|
|
2124
|
-
collected.executed.push(argv[0]);
|
|
2125
|
-
const executable = base(argv[0]);
|
|
2126
|
-
if (SHELLS.has(executable) && argv[1] === "-c" && argv[2])
|
|
2127
|
-
visit(argv[2], true, depth + 1);
|
|
2128
|
-
if (executable === "eval" && argv.length > 1)
|
|
2129
|
-
visit(argv.slice(1).join(" "), true, depth + 1);
|
|
2130
|
-
}
|
|
2131
|
-
for (const substitution of scan.substitutions)
|
|
2132
|
-
visit(substitution, evaluated, depth + 1);
|
|
2133
|
-
};
|
|
2134
|
-
visit(source, false, 0);
|
|
2135
|
-
return collected;
|
|
2136
|
-
}
|
|
2137
|
-
|
|
2138
|
-
class ProbeTimeout extends Error {
|
|
2139
|
-
}
|
|
2140
|
-
var PROBE_BOUNDS = { timeoutMs: 8000, budgetMs: 25000 };
|
|
2141
|
-
async function spawnProbe(binary, args, cwd, signal) {
|
|
2142
|
-
let child;
|
|
2143
|
-
try {
|
|
2144
|
-
child = Bun.spawn([binary, ...args], {
|
|
2145
|
-
cwd,
|
|
2146
|
-
stdin: "ignore",
|
|
2147
|
-
stdout: "pipe",
|
|
2148
|
-
stderr: "ignore",
|
|
2149
|
-
signal,
|
|
2150
|
-
env: hardenedGitEnv(Bun.env)
|
|
2151
|
-
});
|
|
2152
|
-
} catch {
|
|
2153
|
-
return null;
|
|
2154
|
-
}
|
|
2155
|
-
const run = await supervise(child, { signal });
|
|
2156
|
-
if (signal.aborted || run.kind !== "exited")
|
|
2157
|
-
throw new ProbeTimeout(`${binary} ${args.join(" ")}`);
|
|
2158
|
-
return run.exitCode === 0 ? run.stdout.trim() : null;
|
|
2159
|
-
}
|
|
2160
|
-
var defaultProbes = {
|
|
2161
|
-
git: (args, cwd, signal) => spawnProbe("git", args, cwd, signal),
|
|
2162
|
-
wt: (args, cwd, signal) => spawnProbe("wt", args, cwd, signal)
|
|
2163
|
-
};
|
|
2164
|
-
async function decideMergePolicy(command, cwd, probes = defaultProbes, bounds = PROBE_BOUNDS) {
|
|
2165
|
-
const budget = AbortSignal.timeout(bounds.budgetMs);
|
|
2166
|
-
const probe = async (binary, args, dir) => {
|
|
2167
|
-
const label = `${binary} ${args.join(" ")}`;
|
|
2168
|
-
if (budget.aborted)
|
|
2169
|
-
throw new ProbeTimeout(label);
|
|
2170
|
-
const signal = AbortSignal.any([budget, AbortSignal.timeout(bounds.timeoutMs)]);
|
|
2171
|
-
const aborted = new Promise((_, reject) => {
|
|
2172
|
-
signal.addEventListener("abort", () => reject(new ProbeTimeout(label)), { once: true });
|
|
2173
|
-
});
|
|
2174
|
-
return Promise.race([probes[binary](args, dir, signal), aborted]);
|
|
2175
|
-
};
|
|
2176
|
-
try {
|
|
2177
|
-
return await decideCommands(command, cwd, (args, dir) => probe("git", args, dir), (args, dir) => probe("wt", args, dir));
|
|
2178
|
-
} catch (error) {
|
|
2179
|
-
if (!(error instanceof ProbeTimeout))
|
|
2180
|
-
throw error;
|
|
2181
|
-
return {
|
|
2182
|
-
block: true,
|
|
2183
|
-
reason: `merge policy probe \`${error.message}\` timed out, so the merge cannot be checked; retry once git and wt answer promptly`
|
|
2184
|
-
};
|
|
2185
|
-
}
|
|
2186
|
-
}
|
|
2187
|
-
async function decideCommands(command, cwd, git, wt) {
|
|
2188
|
-
const collected = collectCommands(command);
|
|
2189
|
-
if (!collected.complete && /\bwt\b[\s\S]*\bmerge\b/.test(command))
|
|
2190
|
-
return { block: true, reason: UNPARSEABLE_MERGE_REFUSAL };
|
|
2191
|
-
if (collected.mergeVariables.size > 0 && collected.executed.some((word) => references(word, collected.mergeVariables)))
|
|
2192
|
-
return { block: true, reason: OPAQUE_DYNAMIC_MERGE_REFUSAL };
|
|
2193
|
-
const lead = leadingCd(command, cwd);
|
|
2194
|
-
const shellCwd = lead.kind === "dir" ? lead.dir : lead.kind === "none" ? cwd : undefined;
|
|
2195
|
-
for (const argv of collected.commands) {
|
|
2196
|
-
const invocation = parseMergeInvocation(argv);
|
|
2197
|
-
if (!invocation || invocation.target === null)
|
|
2198
|
-
continue;
|
|
2199
|
-
const target = invocation.target;
|
|
2200
|
-
const repoCwd = invocation.workdir?.startsWith("/") ? invocation.workdir : shellCwd === undefined ? undefined : invocation.workdir ? `${shellCwd}/${invocation.workdir}` : shellCwd;
|
|
2201
|
-
if (repoCwd === undefined)
|
|
2202
|
-
return { block: true, reason: UNKNOWN_DIRECTORY_MERGE_REFUSAL };
|
|
2203
|
-
const configured = await wt(["config", "state", "default-branch"], repoCwd);
|
|
2204
|
-
const symbolic = configured || await git(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], repoCwd);
|
|
2205
|
-
const defaultBranch = symbolic?.replace(/^origin\//, "");
|
|
2206
|
-
if (!defaultBranch)
|
|
2207
|
-
return { block: true, reason: `cannot determine the repository default branch; ${MERGE_RETRY}` };
|
|
2208
|
-
if (target === defaultBranch)
|
|
2209
|
-
continue;
|
|
2210
|
-
if (!invocation.noSquash || !invocation.noFf)
|
|
2211
|
-
return { block: true, reason: MERGE_POLICY_REFUSAL.replace("<target>", target) };
|
|
2212
|
-
const currentBranch = await git(["branch", "--show-current"], repoCwd);
|
|
2213
|
-
if (currentBranch === null) {
|
|
2214
|
-
return {
|
|
2215
|
-
block: true,
|
|
2216
|
-
reason: `\`git branch --show-current\` failed in ${repoCwd}, so the source branch cannot be checked; ${SOURCE_WORKTREE_RETRY.replace("<target>", target)}`
|
|
2217
|
-
};
|
|
2218
|
-
}
|
|
2219
|
-
if (!currentBranch || currentBranch === target) {
|
|
2220
|
-
return {
|
|
2221
|
-
block: true,
|
|
2222
|
-
reason: `worker-to-epic merges must run from the source worktree; retry with ${SOURCE_WORKTREE_RETRY.replace("<target>", target)}`
|
|
2223
|
-
};
|
|
2224
|
-
}
|
|
2225
|
-
}
|
|
2226
|
-
return;
|
|
2227
|
-
}
|
|
2228
|
-
|
|
2229
1962
|
// plugins/worktrunk/shared/preflight.ts
|
|
2230
|
-
import { accessSync, constants as constants2, existsSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } from "fs";
|
|
2231
|
-
import { homedir } from "os";
|
|
2232
|
-
import { dirname, isAbsolute, join, resolve as resolve2 } from "path";
|
|
2233
1963
|
var APPROVALS_FIX = "wt config approvals add --yes";
|
|
2234
1964
|
var MERGE_FIX = "wt merge --no-squash --no-ff";
|
|
2235
1965
|
var WORKTREE_FIX = "wt switch -y --create --no-cd --base <base-commit> --format json <branch>";
|
|
@@ -2303,7 +2033,7 @@ function unavailable(command) {
|
|
|
2303
2033
|
return command.returncode === null;
|
|
2304
2034
|
}
|
|
2305
2035
|
function resolvePath(path) {
|
|
2306
|
-
const absolute =
|
|
2036
|
+
const absolute = resolve(path);
|
|
2307
2037
|
try {
|
|
2308
2038
|
return realpathSync(absolute);
|
|
2309
2039
|
} catch {
|
|
@@ -2408,7 +2138,7 @@ function parseJsonObject(text) {
|
|
|
2408
2138
|
}
|
|
2409
2139
|
return;
|
|
2410
2140
|
}
|
|
2411
|
-
var
|
|
2141
|
+
var ASSIGNMENT2 = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
2412
2142
|
function isPathToken(token) {
|
|
2413
2143
|
return /^(?:\/|\.\/|\.\.\/|~\/)/.test(token) || token.includes("/") && !token.startsWith("-");
|
|
2414
2144
|
}
|
|
@@ -2429,7 +2159,7 @@ function commandWords2(command) {
|
|
|
2429
2159
|
if (tokens === undefined || tokens.length === 0)
|
|
2430
2160
|
return;
|
|
2431
2161
|
let index = 0;
|
|
2432
|
-
while (index < tokens.length &&
|
|
2162
|
+
while (index < tokens.length && ASSIGNMENT2.test(tokens[index]))
|
|
2433
2163
|
index++;
|
|
2434
2164
|
return index < tokens.length ? tokens.slice(index) : undefined;
|
|
2435
2165
|
}
|
|
@@ -3052,13 +2782,13 @@ async function runPreflight(options, ctx, checks = CHECKS) {
|
|
|
3052
2782
|
|
|
3053
2783
|
// plugins/worktrunk/shared/switch-timeout.ts
|
|
3054
2784
|
var SWITCH_TIMEOUT_MS = 600000;
|
|
3055
|
-
var
|
|
2785
|
+
var GLOBAL_VALUE_OPTIONS = new Set(["-C", "--config", "--config-set"]);
|
|
3056
2786
|
function isSwitch(argv) {
|
|
3057
2787
|
if (argv[0]?.split("/").pop() !== "wt")
|
|
3058
2788
|
return false;
|
|
3059
2789
|
let index = 1;
|
|
3060
2790
|
while (argv[index]?.startsWith("-"))
|
|
3061
|
-
index +=
|
|
2791
|
+
index += GLOBAL_VALUE_OPTIONS.has(argv[index] ?? "") ? 2 : 1;
|
|
3062
2792
|
return argv[index] === "switch" && !argv.includes("--help") && !argv.includes("-h");
|
|
3063
2793
|
}
|
|
3064
2794
|
function namesSwitchCommand(source) {
|
|
@@ -3074,17 +2804,11 @@ function switchTimeout(input) {
|
|
|
3074
2804
|
return;
|
|
3075
2805
|
return namesSwitchCommand(input.command) ? SWITCH_TIMEOUT_SECONDS : undefined;
|
|
3076
2806
|
}
|
|
3077
|
-
async function mergeGateBlock(event, cwd, probes) {
|
|
3078
|
-
if (!isToolCallEventType("bash", event) || !/merge/.test(event.input.command))
|
|
3079
|
-
return;
|
|
3080
|
-
const decision = await decideMergePolicy(event.input.command, cwd, probes);
|
|
3081
|
-
return decision === undefined ? undefined : { block: true, reason: decision.reason };
|
|
3082
|
-
}
|
|
3083
2807
|
function preflightDirectory(sessionDirectory, path) {
|
|
3084
2808
|
const raw = path?.replace(/^@/, "");
|
|
3085
2809
|
if (!raw)
|
|
3086
2810
|
return sessionDirectory;
|
|
3087
|
-
return isAbsolute2(raw) ? raw :
|
|
2811
|
+
return isAbsolute2(raw) ? raw : resolve2(sessionDirectory, raw);
|
|
3088
2812
|
}
|
|
3089
2813
|
var preflightParameters = Type.Object({
|
|
3090
2814
|
path: Type.Optional(Type.String({ description: "Repository directory; defaults to the session directory" })),
|
|
@@ -3097,13 +2821,13 @@ var preflightParameters = Type.Object({
|
|
|
3097
2821
|
});
|
|
3098
2822
|
function createExtension(overrides = {}) {
|
|
3099
2823
|
return (pi) => {
|
|
3100
|
-
pi.on("tool_call", async (event
|
|
2824
|
+
pi.on("tool_call", async (event) => {
|
|
3101
2825
|
if (!isToolCallEventType("bash", event))
|
|
3102
2826
|
return;
|
|
3103
2827
|
const longer = switchTimeout(event.input);
|
|
3104
2828
|
if (longer !== undefined)
|
|
3105
2829
|
event.input.timeout = longer;
|
|
3106
|
-
return
|
|
2830
|
+
return;
|
|
3107
2831
|
});
|
|
3108
2832
|
pi.registerTool({
|
|
3109
2833
|
name: "worktrunk_preflight",
|
|
@@ -3131,7 +2855,6 @@ export {
|
|
|
3131
2855
|
SWITCH_TIMEOUT_SECONDS,
|
|
3132
2856
|
createExtension,
|
|
3133
2857
|
worktrunk_default as default,
|
|
3134
|
-
mergeGateBlock,
|
|
3135
2858
|
preflightDirectory,
|
|
3136
2859
|
switchTimeout
|
|
3137
2860
|
};
|
package/package.json
CHANGED
|
@@ -38,8 +38,8 @@ Python). If an immediate test, typecheck, or dev server fails with missing-modul
|
|
|
38
38
|
## Merging
|
|
39
39
|
|
|
40
40
|
`wt merge <target>` merges the current branch, so run it from the source worktree. A worker-to-epic merge MUST pass
|
|
41
|
-
`--no-squash --no-ff` to keep per-commit history; an epic-to-default merge may be plain or squashing. This
|
|
42
|
-
|
|
41
|
+
`--no-squash --no-ff` to keep per-commit history; an epic-to-default merge may be plain or squashing. This is guidance: nothing
|
|
42
|
+
refuses a merge that breaks either rule, so check the target and flags before you run it.
|
|
43
43
|
|
|
44
44
|
## Ledger and the canonical checkout
|
|
45
45
|
|