@srobroek/pi-worktrunk-merge-policy 0.7.2 → 0.7.4
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
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## worktrunk [0.7.4](https://github.com/srobroek/harness-plugins/compare/worktrunk--v0.7.3...worktrunk--v0.7.4) (2026-11-29)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
* ship trunk-dev and worktrunk to OMP, bound the provisioning hook, fail the Claude agent guard closed ([0411610](https://github.com/srobroek/harness-plugins/commit/041161067223ab93ba3ce802d60ef4c64e91b0e0)), closes [#170](https://github.com/srobroek/harness-plugins/issues/170)
|
|
6
|
+
|
|
7
|
+
## worktrunk [0.7.3](https://github.com/srobroek/harness-plugins/compare/worktrunk--v0.7.2...worktrunk--v0.7.3) (2026-11-25)
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **worktrunk:** refuse a merge after a leading cd the gate cannot read ([d3b36fe](https://github.com/srobroek/harness-plugins/commit/d3b36fe5e15a7dacfd7481074c10739adfddb6f2))
|
|
12
|
+
|
|
1
13
|
## worktrunk [0.7.2](https://github.com/srobroek/harness-plugins/compare/worktrunk--v0.7.1...worktrunk--v0.7.2) (2026-11-24)
|
|
2
14
|
|
|
3
15
|
## worktrunk [0.7.1](https://github.com/srobroek/harness-plugins/compare/worktrunk--v0.7.0...worktrunk--v0.7.1) (2026-11-23)
|
package/extensions/worktrunk.js
CHANGED
|
@@ -1383,14 +1383,16 @@ var HAS_WELLFORMED = !!"".isWellFormed;
|
|
|
1383
1383
|
var compiled = new Map;
|
|
1384
1384
|
// lib/src/shell-command.ts
|
|
1385
1385
|
import { resolve } from "path";
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1386
|
+
var LITERAL_CD = /^\s*cd[ \t]+([A-Za-z0-9_@%+=:,./-]+)[ \t]*(?:&&|$)/;
|
|
1387
|
+
var STARTS_WITH_CD = /^\s*(?:cd|pushd|popd)(?=\s|$|[;&|<>()])/;
|
|
1388
|
+
function leadingCd(command, cwd) {
|
|
1389
|
+
if (!STARTS_WITH_CD.test(command))
|
|
1390
|
+
return { kind: "none" };
|
|
1391
|
+
const match = LITERAL_CD.exec(command);
|
|
1392
|
+
const dir = match?.[1];
|
|
1393
|
+
if (match === null || dir === undefined || dir.startsWith("-") || dir.split("/").includes(".."))
|
|
1394
|
+
return { kind: "rejected" };
|
|
1395
|
+
return { kind: "dir", dir: resolve(cwd, dir), length: match[0].length };
|
|
1394
1396
|
}
|
|
1395
1397
|
function scanShell(source, options = {}) {
|
|
1396
1398
|
const items = [];
|
|
@@ -2000,6 +2002,7 @@ var SOURCE_WORKTREE_RETRY = `${MERGE_RETRY} from the source worktree`;
|
|
|
2000
2002
|
var MERGE_POLICY_REFUSAL = `worker-to-epic merges must preserve history; retry with ${MERGE_RETRY}`;
|
|
2001
2003
|
var OPAQUE_DYNAMIC_MERGE_REFUSAL = "opaque dynamic shell body names a merge or land command; retry with a literal command";
|
|
2002
2004
|
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";
|
|
2005
|
+
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`";
|
|
2003
2006
|
var SHELLS = new Set(["bash", "sh", "zsh", "dash", "ksh"]);
|
|
2004
2007
|
var GLOBAL_VALUE_OPTIONS = new Set(["-C", "--config", "--config-set"]);
|
|
2005
2008
|
var MERGE_VALUE_OPTIONS = new Set(["--stage", "--format", "--config", "--config-set", "-C"]);
|
|
@@ -2169,13 +2172,16 @@ async function decideCommands(command, cwd, git, wt) {
|
|
|
2169
2172
|
return { block: true, reason: UNPARSEABLE_MERGE_REFUSAL };
|
|
2170
2173
|
if (collected.mergeVariables.size > 0 && collected.executed.some((word) => references(word, collected.mergeVariables)))
|
|
2171
2174
|
return { block: true, reason: OPAQUE_DYNAMIC_MERGE_REFUSAL };
|
|
2172
|
-
const
|
|
2175
|
+
const lead = leadingCd(command, cwd);
|
|
2176
|
+
const shellCwd = lead.kind === "dir" ? lead.dir : lead.kind === "none" ? cwd : undefined;
|
|
2173
2177
|
for (const argv of collected.commands) {
|
|
2174
2178
|
const invocation = parseMergeInvocation(argv);
|
|
2175
2179
|
if (!invocation || invocation.target === null)
|
|
2176
2180
|
continue;
|
|
2177
2181
|
const target = invocation.target;
|
|
2178
|
-
const repoCwd = invocation.workdir
|
|
2182
|
+
const repoCwd = invocation.workdir?.startsWith("/") ? invocation.workdir : shellCwd === undefined ? undefined : invocation.workdir ? `${shellCwd}/${invocation.workdir}` : shellCwd;
|
|
2183
|
+
if (repoCwd === undefined)
|
|
2184
|
+
return { block: true, reason: UNKNOWN_DIRECTORY_MERGE_REFUSAL };
|
|
2179
2185
|
const configured = await wt(["config", "state", "default-branch"], repoCwd);
|
|
2180
2186
|
const symbolic = configured || await git(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], repoCwd);
|
|
2181
2187
|
const defaultBranch = symbolic?.replace(/^origin\//, "");
|
|
@@ -2209,7 +2215,10 @@ import { dirname, isAbsolute, join, resolve as resolve2 } from "path";
|
|
|
2209
2215
|
var APPROVALS_FIX = "wt config approvals add --yes";
|
|
2210
2216
|
var MERGE_FIX = "wt merge --no-squash --no-ff";
|
|
2211
2217
|
var WORKTREE_FIX = "wt switch -y --create --no-cd --base <base-commit> --format json <branch>";
|
|
2212
|
-
var
|
|
2218
|
+
var PROVISIONING_COPY_TIMEOUT_SECONDS = 600;
|
|
2219
|
+
var PROVISIONING_COPY_WRAPPER = "use POSIX (); $t=shift; $p=fork; defined $p or do{print STDERR q{copy-ignored: fork failed: },$!,$/; exit 125}; if(!$p){setpgrp(0,0); exec @ARGV; print STDERR q{copy-ignored: cannot run }.$ARGV[0].q{: }.$!.$/; POSIX::_exit(127)} $SIG{ALRM}=sub{kill q{TERM},-$p; select undef,undef,undef,2; kill q{KILL},-$p; print STDERR q{copy-ignored: timed out after }.$t.q{s; killed its process group}.$/; exit 124}; alarm $t; waitpid $p,0; exit($?&127 ? 128+($?&127) : $?>>8)";
|
|
2220
|
+
var provisioningCopyCommand = (seconds) => `perl -e '${PROVISIONING_COPY_WRAPPER}' ${seconds} wt step copy-ignored --require-include`;
|
|
2221
|
+
var PROVISIONING_COPY_COMMAND = provisioningCopyCommand(PROVISIONING_COPY_TIMEOUT_SECONDS);
|
|
2213
2222
|
var PROVISIONING_UV_COMMAND = "uv sync";
|
|
2214
2223
|
var PROVISIONING_FIX = "rerun with --apply; project hooks require `wt config approvals add` before the next worktree";
|
|
2215
2224
|
var DEFAULT_TIMEOUT_MS = 60000;
|
package/package.json
CHANGED
|
@@ -22,7 +22,25 @@ APPROVALS_FIX = "wt config approvals add --yes"
|
|
|
22
22
|
MERGE_FIX = "wt merge --no-squash --no-ff"
|
|
23
23
|
WORKTREE_FIX = "wt switch -y --create --no-cd --base <base-commit> --format json <branch>"
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
# Seconds before the provisioning copy is killed, so a stuck walk cannot block lane setup.
|
|
26
|
+
PROVISIONING_COPY_TIMEOUT_SECONDS = 600
|
|
27
|
+
# Perl wrapper (perl is on macOS and Linux; stock macOS has no `timeout`). It runs the command in its own process
|
|
28
|
+
# group, kills the whole group on timeout (so `git` children die too), says so on stderr and exits 124. A command it
|
|
29
|
+
# cannot exec prints an error and exits 127. It holds no double quote or backslash: the hook sits in a TOML string.
|
|
30
|
+
PROVISIONING_COPY_WRAPPER = (
|
|
31
|
+
"use POSIX (); $t=shift; $p=fork; defined $p or do{print STDERR q{copy-ignored: fork failed: },$!,$/; exit 125}; "
|
|
32
|
+
"if(!$p){setpgrp(0,0); exec @ARGV; print STDERR q{copy-ignored: cannot run }.$ARGV[0].q{: }.$!.$/; POSIX::_exit(127)} "
|
|
33
|
+
"$SIG{ALRM}=sub{kill q{TERM},-$p; select undef,undef,undef,2; kill q{KILL},-$p; "
|
|
34
|
+
"print STDERR q{copy-ignored: timed out after }.$t.q{s; killed its process group}.$/; exit 124}; "
|
|
35
|
+
"alarm $t; waitpid $p,0; exit($?&127 ? 128+($?&127) : $?>>8)"
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def provisioning_copy_command(seconds: int) -> str:
|
|
40
|
+
return f"perl -e '{PROVISIONING_COPY_WRAPPER}' {seconds} wt step copy-ignored --require-include"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
PROVISIONING_COPY_COMMAND = provisioning_copy_command(PROVISIONING_COPY_TIMEOUT_SECONDS)
|
|
26
44
|
PROVISIONING_UV_COMMAND = "uv sync"
|
|
27
45
|
PROVISIONING_FIX = "rerun with --apply; project hooks require `wt config approvals add` before the next worktree"
|
|
28
46
|
|
|
@@ -7,9 +7,13 @@ import contextlib
|
|
|
7
7
|
import importlib.util
|
|
8
8
|
import io
|
|
9
9
|
import json
|
|
10
|
+
import os
|
|
10
11
|
from pathlib import Path
|
|
12
|
+
import shutil
|
|
13
|
+
import subprocess
|
|
11
14
|
import sys
|
|
12
15
|
import tempfile
|
|
16
|
+
import time
|
|
13
17
|
import unittest
|
|
14
18
|
from unittest.mock import patch
|
|
15
19
|
|
|
@@ -282,7 +286,61 @@ class PreflightRegressionTests(unittest.TestCase):
|
|
|
282
286
|
self.assertIn("[post-start]", result.detail)
|
|
283
287
|
self.assertIn("copy-ignored", result.detail)
|
|
284
288
|
|
|
285
|
-
def
|
|
289
|
+
def test_provisioning_copy_hook_is_bounded_and_still_recognised(self) -> None:
|
|
290
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
291
|
+
root = Path(directory)
|
|
292
|
+
home = root / "home"
|
|
293
|
+
with patch.object(Path, "home", return_value=home):
|
|
294
|
+
absent = preflight.check_provisioning_hook(FakeContext(root))
|
|
295
|
+
self.assertIn("setpgrp", absent.detail)
|
|
296
|
+
config = home / ".config" / "worktrunk" / "config.toml"
|
|
297
|
+
config.parent.mkdir(parents=True)
|
|
298
|
+
config.write_text(
|
|
299
|
+
f"[post-start]\ncopy-ignored = {json.dumps(preflight.PROVISIONING_COPY_COMMAND)}\n", encoding="utf-8"
|
|
300
|
+
)
|
|
301
|
+
with patch.object(Path, "home", return_value=home):
|
|
302
|
+
result = preflight.check_provisioning_hook(FakeContext(root))
|
|
303
|
+
self.assertEqual(result.status, "pass")
|
|
304
|
+
|
|
305
|
+
def _run_copy_hook(self, seconds: int, path: str) -> subprocess.CompletedProcess[str]:
|
|
306
|
+
return subprocess.run(
|
|
307
|
+
["/bin/sh", "-c", preflight.provisioning_copy_command(seconds)],
|
|
308
|
+
env={"PATH": path},
|
|
309
|
+
capture_output=True,
|
|
310
|
+
text=True,
|
|
311
|
+
check=False,
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
def _fake_wt(self, directory: str, script: str) -> str:
|
|
315
|
+
wt = Path(directory) / "wt"
|
|
316
|
+
wt.write_text(f"#!/bin/sh\n{script}\n", encoding="utf-8")
|
|
317
|
+
wt.chmod(0o755)
|
|
318
|
+
return directory
|
|
319
|
+
|
|
320
|
+
def test_copy_hook_wrapper_passes_exit_code_through(self) -> None:
|
|
321
|
+
perl_dir = str(Path(shutil.which("perl") or "/usr/bin/perl").parent)
|
|
322
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
323
|
+
done = self._run_copy_hook(5, f"{self._fake_wt(directory, 'exit 3')}:{perl_dir}")
|
|
324
|
+
self.assertEqual(done.returncode, 3)
|
|
325
|
+
|
|
326
|
+
def test_copy_hook_wrapper_kills_the_process_group_on_timeout(self) -> None:
|
|
327
|
+
perl_dir = str(Path(shutil.which("perl") or "/usr/bin/perl").parent)
|
|
328
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
329
|
+
marker = Path(directory) / "child.pid"
|
|
330
|
+
bin_dir = self._fake_wt(directory, f"sleep 30 &\necho $! > {marker}\nwait")
|
|
331
|
+
done = self._run_copy_hook(1, f"{bin_dir}:{perl_dir}:/bin")
|
|
332
|
+
self.assertEqual(done.returncode, 124)
|
|
333
|
+
self.assertIn("timed out after 1s", done.stderr)
|
|
334
|
+
pid = int(marker.read_text(encoding="utf-8").strip())
|
|
335
|
+
time.sleep(0.3)
|
|
336
|
+
with self.assertRaises(ProcessLookupError):
|
|
337
|
+
os.kill(pid, 0)
|
|
338
|
+
|
|
339
|
+
def test_copy_hook_wrapper_errors_when_wt_cannot_run(self) -> None:
|
|
340
|
+
perl_dir = str(Path(shutil.which("perl") or "/usr/bin/perl").parent)
|
|
341
|
+
done = self._run_copy_hook(5, perl_dir)
|
|
342
|
+
self.assertEqual(done.returncode, 127)
|
|
343
|
+
self.assertIn("cannot run wt", done.stderr)
|
|
286
344
|
with tempfile.TemporaryDirectory() as directory:
|
|
287
345
|
root = Path(directory)
|
|
288
346
|
(root / "pyproject.toml").write_text("[project]\nname = \"example\"\n", encoding="utf-8")
|
|
@@ -326,7 +384,7 @@ class PreflightRegressionTests(unittest.TestCase):
|
|
|
326
384
|
self.assertEqual(result["status"], "pass")
|
|
327
385
|
self.assertEqual(
|
|
328
386
|
config.read_text(encoding="utf-8"),
|
|
329
|
-
original +
|
|
387
|
+
original + f"\n[post-start]\ncopy-ignored = {json.dumps(preflight.PROVISIONING_COPY_COMMAND)}\n",
|
|
330
388
|
)
|
|
331
389
|
self.assertIn("wt config approvals add", result["detail"])
|
|
332
390
|
|