@rt-tools/agent-kit 0.38.0 → 0.39.0

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.
Files changed (59) hide show
  1. package/assets/checks/check-turn-map.mjs +4 -4
  2. package/assets/checks/decision-table.json +91 -24
  3. package/assets/checks/flow-data.mjs +22 -23
  4. package/assets/checks/flow-forecast.mjs +12 -8
  5. package/assets/checks/flow-guard.mjs +365 -163
  6. package/assets/checks/flow-step.mjs +188 -58
  7. package/assets/checks/flow-verdict.mjs +124 -9
  8. package/assets/checks/record-count.mjs +27 -0
  9. package/assets/checks/task-flow-script.mjs +33 -7
  10. package/assets/checks/work-result.mjs +92 -11
  11. package/assets/commands/night.md +3 -3
  12. package/assets/defaults/turn-map.md +4 -2
  13. package/assets/docs/GLOSSARY.md +1 -1
  14. package/assets/hooks/ask-judge.md +4 -2
  15. package/assets/hooks/facts.sh +66 -0
  16. package/assets/hooks/git-guard-delivery-epic.sh +2 -2
  17. package/assets/hooks/git-guard-delivery-rerun.sh +17 -5
  18. package/assets/hooks/git-guard-push-ahead.sh +24 -2
  19. package/assets/hooks/git-guard-push-marks.sh +209 -12
  20. package/assets/hooks/git-guard-push-tests.sh +29 -2
  21. package/assets/hooks/pre-push.sh +26 -2
  22. package/assets/hooks/push-ahead-call-guard.sh +447 -0
  23. package/assets/hooks/push-set.sh +31 -8
  24. package/assets/hooks/turn-exit-guard.sh +1 -1
  25. package/assets/hooks/turn-judge.md +16 -6
  26. package/assets/patterns/doc-style-human.md +36 -76
  27. package/assets/pitfalls/doc-style-human.md +112 -0
  28. package/assets/pitfalls/epic-flow.md +92 -0
  29. package/assets/pitfalls/mockups.needs-mockups.md +87 -0
  30. package/assets/pitfalls/observability.needs-app.md +186 -0
  31. package/assets/pitfalls/platform-access.md +94 -0
  32. package/assets/pitfalls/queue-audit.github.md +78 -0
  33. package/assets/pitfalls/task-flow-archive.md +2 -1
  34. package/assets/pitfalls/task-flow-close.2.md +2 -1
  35. package/assets/pitfalls/task-flow-owner.md +29 -0
  36. package/assets/pitfalls/task-flow-resume.md +2 -2
  37. package/assets/pitfalls/task-flow-start.md +1 -1
  38. package/assets/pitfalls/turn-conduct.md +1 -1
  39. package/assets/rules/autonomous-work.md +1 -1
  40. package/assets/rules/epic-flow.md +24 -63
  41. package/assets/rules/mockups.needs-mockups.md +25 -64
  42. package/assets/rules/observability.needs-app.md +43 -172
  43. package/assets/rules/platform-access.md +28 -75
  44. package/assets/rules/queue-audit.github.md +25 -52
  45. package/assets/rules/task-flow-owner.md +6 -6
  46. package/assets/rules/turn-conduct.md +22 -19
  47. package/assets/task-flow/flow.js +10 -14
  48. package/assets/task-flow/flow.md +7 -8
  49. package/assets/task-flow/measure/ci.mjs +184 -0
  50. package/assets/task-flow/measure/owner.mjs +13 -6
  51. package/assets/task-flow/measure/session.mjs +92 -15
  52. package/assets/task-flow/source/git.mjs +179 -11
  53. package/assets/task-flow/source/host.mjs +105 -0
  54. package/assets/task-flow/source/owner.mjs +33 -13
  55. package/assets/task-flow/source/session.mjs +47 -10
  56. package/lib/agent-checks.d.ts.map +1 -1
  57. package/lib/agent-checks.js +14 -2
  58. package/lib/agent-checks.js.map +1 -1
  59. package/package.json +1 -1
@@ -0,0 +1,447 @@
1
+ #!/usr/bin/env bash
2
+ # rt-hook: PreToolUse Bash
3
+ # Requires: hooks/deny-tail.sh, hooks/decide.sh
4
+ # Guard of the call of the run ahead: the notice of a background call carries the exit code of the
5
+ # set. Only a Bash call with `run_in_background` is judged: a call in the foreground returns its
6
+ # output in the result itself.
7
+ #
8
+ # A call that runs `git-guard-push-ahead.sh` in the background comes back as a notice with the code
9
+ # of the LAST command of the call, not of the set. A red set followed by `; echo "exit=$?"`,
10
+ # `; tail -5 log` or piped into `| tail` comes back as «exit code 0»: on 21.11 a session saw
11
+ # «exit=1» in the output and still read the notice «exit code 0» as green. A code printed into the
12
+ # output is no substitute for the code of the notice, so the guard refuses every call whose last
13
+ # command is not the set:
14
+ # - any command after the set by `;`, a newline or `||`, unless it is `exit`, `exit $?`,
15
+ # `rc=$?; …; exit $rc` at the end of the call, `|| exit N` with a nonzero N, or `set -e` stands
16
+ # before the set;
17
+ # - a pipe after the set, unless `set -o pipefail` stands before it and the pipe ends the call.
18
+ # `exit ${PIPESTATUS[0]}` does not count: the calls of this host run in zsh, which has no such
19
+ # array, and the exit is 0;
20
+ # - the set sent to the background by `&`, put under `!` (except `if ! set; then exit N`) or run
21
+ # inside `$(…)`;
22
+ # - a chain of `&&` after the set that ends with `;`, a newline or `||` and no `exit $?` after it.
23
+ # A set alone, a set with its own output redirected and a chain of `&&` to the end of the call pass:
24
+ # on red each ends the call with the code of the set. A text that only names the script — a search,
25
+ # a read, a heredoc, `printf` into a file, a quoted commit message, `bash -n` — passes; a run inside
26
+ # `bash -c '…'` or `eval` is judged by its inner text. Loops and groups are read through: `done`,
27
+ # `fi` and `}` after the set pass its code on.
28
+ #
29
+ # FAIL-OPEN: no `jq`, no `node`, a broken input, a call that is not a command, a command that does
30
+ # not name the script — pass. The price: in such an environment a hidden code goes through as
31
+ # before. A script named only through a variable (`bash $S`) is not recognised.
32
+
33
+ RT_GUARD_NAME=push-ahead-call-guard
34
+ RT_GUARD_RULE=git-workflow
35
+
36
+ . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/utf8.sh" 2>/dev/null || true
37
+ . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/hook-input.sh" 2>/dev/null || true
38
+
39
+ rt_hook_read
40
+ input="$RT_HOOK_INPUT"
41
+ [ -z "$input" ] && exit 0
42
+ command -v jq >/dev/null 2>&1 || exit 0
43
+ command -v node >/dev/null 2>&1 || exit 0
44
+
45
+ tool="$(rt_hook_tool)"
46
+ [ "$tool" = Bash ] || exit 0
47
+ # Only a background call comes back as a notice with one exit code. A call in the foreground returns
48
+ # its output in the result itself, and a printed `exit=…` stands right there.
49
+ [ "$(printf '%s' "$input" | jq -r '.tool_input.run_in_background // false' 2>/dev/null)" = true ] || exit 0
50
+
51
+ cmd="$(rt_hook_cmd)"
52
+ [ -z "$cmd" ] && exit 0
53
+ case "$cmd" in
54
+ *git-guard-push-ahead.sh*) ;;
55
+ *) exit 0 ;;
56
+ esac
57
+
58
+ # The judge reads the command as the shell splits it: quotes, heredoc bodies, comments, redirections
59
+ # and the separators between simple commands. It prints «pass» or «hide<TAB><what hides the code>».
60
+ verdict="$(RT_CALL="$cmd" node --input-type=module -e '
61
+ const SCRIPT = "git-guard-push-ahead.sh";
62
+ const SHELLS = new Set(["bash", "sh", "zsh"]);
63
+ // Words that only open a command: they pass the code of what follows them on.
64
+ const OPENERS = new Set(["nohup", "exec", "command", "builtin", "then", "else", "elif", "do", "if", "while", "until", "{"]);
65
+ // Words that close a loop or a group: the code of the set goes on through them.
66
+ const CLOSERS = new Set(["done", "fi", "esac", "}"]);
67
+ // `$?` inside double quotes is expanded by the shell that reads the quotes; the mark keeps that
68
+ // apart, so that an inner `bash -c "…; exit $?"` is read as the outer code it really is.
69
+ const OUTER = "\u0001";
70
+ const base = (w) => w.replace(/^.*\//, "");
71
+
72
+ const lex = (s) => {
73
+ const toks = [];
74
+ const heredocs = [];
75
+ let word = null;
76
+ let i = 0;
77
+ const push = () => {
78
+ if (word !== null) toks.push({ t: "w", v: word });
79
+ word = null;
80
+ };
81
+ // The delimiter of a heredoc: quotes and backslashes are dropped, as the shell drops them.
82
+ const delimiter = () => {
83
+ while (s[i] === " " || s[i] === "\t") i++;
84
+ let d = "";
85
+ while (i < s.length && !/[\s;|&<>()]/.test(s[i])) {
86
+ if (s[i] === "\x27" || s[i] === "\"") {
87
+ const q = s[i];
88
+ const j = s.indexOf(q, i + 1);
89
+ const end = j < 0 ? s.length : j;
90
+ d += s.slice(i + 1, end);
91
+ i = end + 1;
92
+ continue;
93
+ }
94
+ if (s[i] === "\\") { d += s[i + 1] ?? ""; i += 2; continue; }
95
+ d += s[i++];
96
+ }
97
+ return d;
98
+ };
99
+ while (i < s.length) {
100
+ const c = s[i];
101
+ if (c === "\\") {
102
+ if (s[i + 1] === "\n") { i += 2; continue; }
103
+ word = (word ?? "") + (s[i + 1] ?? "");
104
+ i += 2;
105
+ continue;
106
+ }
107
+ if (c === "\x27") {
108
+ const j = s.indexOf("\x27", i + 1);
109
+ const end = j < 0 ? s.length : j;
110
+ word = (word ?? "") + s.slice(i + 1, end);
111
+ i = end + 1;
112
+ continue;
113
+ }
114
+ if (c === "\"") {
115
+ let j = i + 1;
116
+ let v = "";
117
+ while (j < s.length && s[j] !== "\"") {
118
+ if (s[j] === "\\" && j + 1 < s.length) { v += s[j + 1]; j += 2; continue; }
119
+ if (s[j] === "$" && s[j + 1] === "?") { v += OUTER; j += 2; continue; }
120
+ v += s[j];
121
+ j++;
122
+ }
123
+ word = (word ?? "") + v;
124
+ i = j + 1;
125
+ continue;
126
+ }
127
+ if (c === "$" && s[i + 1] === "(") {
128
+ let d = 0;
129
+ let j = i + 1;
130
+ for (; j < s.length; j++) {
131
+ if (s[j] === "(") d++;
132
+ else if (s[j] === ")" && --d === 0) break;
133
+ }
134
+ word = (word ?? "") + s.slice(i, j + 1);
135
+ i = j + 1;
136
+ continue;
137
+ }
138
+ if (c === "`") {
139
+ const j = s.indexOf("`", i + 1);
140
+ const end = j < 0 ? s.length : j;
141
+ word = (word ?? "") + s.slice(i, end + 1);
142
+ i = end + 1;
143
+ continue;
144
+ }
145
+ if (c === "#" && word === null) {
146
+ const j = s.indexOf("\n", i);
147
+ i = j < 0 ? s.length : j;
148
+ continue;
149
+ }
150
+ if (c === " " || c === "\t") { push(); i++; continue; }
151
+ if (c === "\n") {
152
+ push();
153
+ toks.push({ t: "op", v: ";" });
154
+ i++;
155
+ // The bodies of the heredocs opened on this line are text, not commands.
156
+ while (heredocs.length > 0) {
157
+ const h = heredocs.shift();
158
+ for (;;) {
159
+ const j = s.indexOf("\n", i);
160
+ const line = s.slice(i, j < 0 ? s.length : j);
161
+ i = j < 0 ? s.length : j + 1;
162
+ if ((h.tabs ? line.replace(/^\t+/, "") : line) === h.word) break;
163
+ h.body += line + "\n";
164
+ if (j < 0) break;
165
+ }
166
+ }
167
+ continue;
168
+ }
169
+ // A redirection glued to a word ends that word: `set>log` is the set and its log.
170
+ if ((c === ">" || c === "<" || (c === "&" && s[i + 1] === ">")) && word !== null && !/^\d+$/.test(word)) {
171
+ push();
172
+ continue;
173
+ }
174
+ const rest = s.slice(i);
175
+ const redir = (word === null || /^\d+$/.test(word)) && rest.match(/^(&>>|&>|>>|>&|<&|<<<|<<-|<<|<>|>\||>|<)(\d+|-)?/);
176
+ if (redir) {
177
+ const fd = word ?? "";
178
+ word = null;
179
+ i += redir[0].length;
180
+ if (redir[1] === "<<" || redir[1] === "<<-") {
181
+ const h = { tabs: redir[1] === "<<-", word: delimiter(), body: "" };
182
+ heredocs.push(h);
183
+ toks.push({ t: "r", v: fd + redir[1], target: false, heredoc: h });
184
+ continue;
185
+ }
186
+ const full = /&$/.test(redir[1]) && redir[2] !== undefined;
187
+ toks.push({ t: "r", v: fd + redir[0], target: !full });
188
+ continue;
189
+ }
190
+ const op = rest.match(/^(&&|\|\||\|&|;;|\||;|&|\(|\))/);
191
+ if (op) { push(); toks.push({ t: "op", v: op[1] }); i += op[1].length; continue; }
192
+ word = (word ?? "") + c;
193
+ i++;
194
+ }
195
+ push();
196
+ return toks;
197
+ };
198
+
199
+ // Simple commands with the separator that follows each.
200
+ const commands = (toks) => {
201
+ const list = [];
202
+ let cur = { words: [], sep: null, heredocs: [] };
203
+ let skipTarget = false;
204
+ for (const tok of toks) {
205
+ if (tok.t === "r") {
206
+ skipTarget = tok.target;
207
+ if (tok.heredoc) cur.heredocs.push(tok.heredoc);
208
+ continue;
209
+ }
210
+ if (tok.t === "w") {
211
+ if (skipTarget) { skipTarget = false; continue; }
212
+ cur.words.push(tok.v);
213
+ continue;
214
+ }
215
+ if (tok.v === "(" || tok.v === ")") continue;
216
+ cur.sep = tok.v === ";;" ? ";" : tok.v;
217
+ list.push(cur);
218
+ cur = { words: [], sep: null, heredocs: [] };
219
+ }
220
+ list.push(cur);
221
+ return list.filter((c) => c.words.length > 0 || c.sep === "&" || c.sep === "|");
222
+ };
223
+
224
+ const isStatus = (w) => w === "$?" || w === "${?}" || w === OUTER;
225
+ const isCloser = (c) => c !== undefined && c.words.length === 1 && CLOSERS.has(c.words[0]);
226
+ // The pipefail state the commands before the set leave: the last `set -o pipefail` or
227
+ // `set +o pipefail` wins.
228
+ const pipefailAfter = (list, start) => {
229
+ let on = start;
230
+ for (const c of list) {
231
+ if (c.words[0] !== "set") continue;
232
+ c.words.forEach((w, k) => {
233
+ if (c.words[k + 1] !== "pipefail") return;
234
+ if (/^-[a-zA-Z]*o$/.test(w)) on = true;
235
+ if (/^\+[a-zA-Z]*o$/.test(w)) on = false;
236
+ });
237
+ }
238
+ return on;
239
+ };
240
+ // The errexit state the commands before the set leave: `set -e`, `set -o errexit` and their `+`.
241
+ const errexitAfter = (list, start) => {
242
+ let on = start;
243
+ for (const c of list) {
244
+ if (c.words[0] !== "set") continue;
245
+ c.words.forEach((w, k) => {
246
+ if (/^-[a-zA-Z]*o$/.test(w) && c.words[k + 1] === "errexit") on = true;
247
+ else if (/^\+[a-zA-Z]*o$/.test(w) && c.words[k + 1] === "errexit") on = false;
248
+ else if (/^-[a-zA-Z]*e/.test(w)) on = true;
249
+ else if (/^\+[a-zA-Z]*e/.test(w)) on = false;
250
+ });
251
+ }
252
+ return on;
253
+ };
254
+ // `exit N` with a nonzero number, after the words that only open a command: on red the call ends red.
255
+ const exitsRed = (c) => {
256
+ if (c === undefined) return false;
257
+ const w = c.words.filter((x, k) => !(k === 0 && (x === "then" || x === "{" || x === "else")));
258
+ return w[0] === "exit" && /^[0-9]+$/.test(w[1] ?? "") && Number(w[1]) !== 0;
259
+ };
260
+ // A command substitution that runs the set keeps its code inside: the call never sees it.
261
+ const substitutes = (c) => c.words.some((w) => {
262
+ for (const m of w.matchAll(/\$\(([\s\S]*)\)|`([^`]*)`/g)) {
263
+ if (judge(m[1] ?? m[2] ?? "", false, false).ran) return true;
264
+ }
265
+ return false;
266
+ });
267
+
268
+ // Words before the command itself: assignments, `!`, `env`, `time`, `timeout`, `nice`,
269
+ // `caffeinate`, `pnpm exec`, `npx` and the words that only open a command.
270
+ const strip = (words) => {
271
+ let w = [...words];
272
+ let neg = false;
273
+ const dropOptions = (withArg) => {
274
+ while (w[0] !== undefined && w[0].startsWith("-")) {
275
+ const o = w[0];
276
+ w = w.slice(1);
277
+ if (withArg.includes(o)) w = w.slice(1);
278
+ }
279
+ };
280
+ for (;;) {
281
+ const h = w[0];
282
+ if (h === undefined) break;
283
+ if (h === "!") { neg = true; w = w.slice(1); continue; }
284
+ if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(h)) { w = w.slice(1); continue; }
285
+ if (OPENERS.has(h)) { w = w.slice(1); dropOptions([]); continue; }
286
+ if (base(h) === "env") { w = w.slice(1); dropOptions(["-u", "-C", "-S"]); continue; }
287
+ if (h === "sudo") { w = w.slice(1); dropOptions(["-u", "-g", "-C"]); continue; }
288
+ if (h === "stdbuf") { w = w.slice(1); dropOptions(["-i", "-o", "-e"]); continue; }
289
+ if (h === "time") { w = w.slice(1); dropOptions([]); continue; }
290
+ if (h === "timeout") { w = w.slice(1); dropOptions(["-s", "-k"]); w = w.slice(1); continue; }
291
+ if (h === "nice") { w = w.slice(1); dropOptions(["-n"]); continue; }
292
+ if (h === "caffeinate") { w = w.slice(1); dropOptions(["-t", "-w"]); continue; }
293
+ if (base(h) === "script") { w = w.slice(1); dropOptions(["-F", "-t"]); w = w.slice(1); continue; }
294
+ if (h === "npx") { w = w.slice(1); dropOptions([]); continue; }
295
+ if ((h === "pnpm" || h === "npm") && w[1] === "exec") { w = w.slice(2); dropOptions([]); continue; }
296
+ break;
297
+ }
298
+ return { w, neg };
299
+ };
300
+
301
+ // Is the command a run of the set: the script itself, a shell running it, `source`, or a shell -c
302
+ // or `eval` whose inner text holds a run.
303
+ const runOf = (c) => {
304
+ const { w, neg } = strip(c.words);
305
+ if (w.length === 0) return null;
306
+ if (base(w[0]) === SCRIPT) return { neg };
307
+ if ((w[0] === "source" || w[0] === ".") && w[1] !== undefined && base(w[1]) === SCRIPT) return { neg };
308
+ const inner = (text, pipefail, errexit) => {
309
+ const v = judge(text.split(OUTER).join("0"), pipefail, errexit);
310
+ if (v.hide) return { neg, hide: v.hide };
311
+ return v.ran ? { neg } : null;
312
+ };
313
+ if (w[0] === "eval") return inner(w.slice(1).join(" "), false, false);
314
+ if (!SHELLS.has(base(w[0]))) return null;
315
+ let k = 1;
316
+ let check = false;
317
+ let hasC = false;
318
+ let pipefail = false;
319
+ let errexit = false;
320
+ while (k < w.length && /^[-+]/.test(w[k])) {
321
+ const o = w[k];
322
+ if (o.startsWith("--")) {
323
+ k += o === "--rcfile" || o === "--init-file" ? 2 : 1;
324
+ continue;
325
+ }
326
+ const letters = o.slice(1);
327
+ if (o[0] === "-" && letters.includes("n")) check = true;
328
+ if (letters.includes("c")) hasC = true;
329
+ if (o[0] === "-" && /^[a-zA-Z]*e/.test(letters) && !/[oO]$/.test(letters.split("e")[0])) errexit = true;
330
+ if (/[oO]$/.test(letters)) {
331
+ if (o[0] === "-" && w[k + 1] === "pipefail") pipefail = true;
332
+ if (o[0] === "-" && w[k + 1] === "errexit") errexit = true;
333
+ k += 2;
334
+ continue;
335
+ }
336
+ k++;
337
+ }
338
+ if (check) return null;
339
+ if (hasC) return inner(w[k] ?? "", pipefail, errexit);
340
+ // A shell with no script and no `-c` reads its commands from a heredoc.
341
+ if (w[k] === undefined && c.heredocs.length > 0) return inner(c.heredocs.map((h) => h.body).join("\n"), pipefail, errexit);
342
+ return w[k] !== undefined && base(w[k]) === SCRIPT ? { neg } : null;
343
+ };
344
+
345
+ const describe = {
346
+ "!": "the set stands under `!`, and the call reports the opposite of its code",
347
+ "&": "the set goes to the background by `&`, and the call ends before it",
348
+ "|": "the set is piped into the next command, and the call does not end with that pipe under `set -o pipefail`",
349
+ "$(": "the set runs inside a command substitution `$(…)`, and its code stays inside",
350
+ ";": "a command follows the set by `;` or a newline, and the call ends with the code of that command",
351
+ "||": "a command follows the set by `||`, and on red the call ends with the code of that command",
352
+ "&&;": "a chain of `&&` after the set ends with `;`, a newline or `||`, and no `exit $?` follows it",
353
+ };
354
+
355
+ function judge(text, pipefailAtStart, errexitAtStart) {
356
+ const cmds = commands(lex(text));
357
+ const last = cmds[cmds.length - 1];
358
+ const exitsWith = (c, accept) => c !== undefined && c.words[0] === "exit" && (c.words.length === 1 || accept(c.words[1]));
359
+ const endsWithExit = (c) => exitsWith(c, isStatus);
360
+ // The `then` branch from `start` runs its commands by `;` and reaches `exit N` before `fi`.
361
+ const thenExitsRed = (start) => {
362
+ for (let j = start; cmds[j] !== undefined; j++) {
363
+ const w0 = cmds[j].words[0];
364
+ if (j > start && (w0 === "fi" || w0 === "else" || w0 === "elif")) return false;
365
+ if (exitsRed(cmds[j])) return true;
366
+ if (cmds[j].sep !== ";") return false;
367
+ }
368
+ return false;
369
+ };
370
+ let ran = false;
371
+ for (let k = 0; k < cmds.length; k++) {
372
+ if (substitutes(cmds[k])) return { hide: describe["$("], ran: true };
373
+ const run = runOf(cmds[k]);
374
+ if (run === null) continue;
375
+ if (run.hide) return { hide: run.hide, ran: true };
376
+ ran = true;
377
+ // `if ! set; then exit 1; fi`: on red the call ends red.
378
+ const head = cmds[k].words[0];
379
+ if (run.neg && (head === "if" || head === "elif") && cmds[k].sep === ";" && cmds[k + 1]?.words[0] === "then" && thenExitsRed(k + 1)) continue;
380
+ if (run.neg) return { hide: describe["!"], ran };
381
+ // Loops and groups closing after the set pass its code on.
382
+ let sep = cmds[k].sep;
383
+ let i = k + 1;
384
+ while ((sep === ";" || sep === "\n") && isCloser(cmds[i])) { sep = cmds[i].sep; i++; }
385
+ if (sep === null) continue;
386
+ if (sep === "&") return { hide: describe["&"], ran };
387
+ if ((sep === ";" || sep === "||") && cmds[i] === undefined) continue;
388
+ // `set || exit 1`: on red the call ends red, whatever follows on green.
389
+ if (sep === "||" && exitsRed(cmds[i])) continue;
390
+ // Under errexit a red set ends the call with its own code, unless it is a condition.
391
+ const condition = ["if", "elif", "while", "until"].includes(head);
392
+ if (sep === ";" && !condition && errexitAfter(cmds.slice(0, k), errexitAtStart)) continue;
393
+ if (sep === "|" || sep === "|&") {
394
+ let m = i;
395
+ while (cmds[m] !== undefined && ["|", "|&"].includes(cmds[m].sep)) m++;
396
+ // `PIPESTATUS` is no help: the calls of this host run in zsh, where the array is absent
397
+ // and `exit ${PIPESTATUS[0]}` exits with 0. Only pipefail carries a red set out.
398
+ const pipefail = pipefailAfter(cmds.slice(0, k), pipefailAtStart);
399
+ const end = cmds[m]?.sep ?? null;
400
+ if (pipefail && end !== "&" && end !== "||" && end !== "&&" && (cmds[m + 1] === undefined || endsWithExit(cmds[m + 1]))) continue;
401
+ return { hide: describe["|"], ran };
402
+ }
403
+ if (sep === "&&") {
404
+ let m = i;
405
+ while (cmds[m] !== undefined && ["&&", "|", "|&"].includes(cmds[m].sep)) m++;
406
+ let end = cmds[m]?.sep ?? null;
407
+ // `set && break; done` in a retry loop: the loop ends with the code of the last set.
408
+ while ((end === ";" || end === "\n") && isCloser(cmds[m + 1])) { m++; end = cmds[m].sep; }
409
+ if (end === "&") return { hide: describe["&"], ran };
410
+ if (end === null || cmds[m + 1] === undefined) continue;
411
+ if (endsWithExit(cmds[m + 1])) continue;
412
+ return { hide: describe["&&;"], ran };
413
+ }
414
+ // `;`, a newline or `||`: only `exit`, `exit $?`, or `rc=$?` and `exit $rc` at the end.
415
+ const next = cmds[i];
416
+ if (endsWithExit(next)) continue;
417
+ const saved = next.words.length === 1 ? next.words[0].match(/^([A-Za-z_][A-Za-z0-9_]*)=(\$\?|\$\{\?\}|\u0001)$/) : null;
418
+ // `exit $rc` counts only as the last command, reached unconditionally after a `;`.
419
+ const before = cmds[cmds.length - 2];
420
+ if (saved && next !== last && before?.sep === ";" && exitsWith(last, (a) => a === "$" + saved[1] || a === "${" + saved[1] + "}")) continue;
421
+ return { hide: describe[sep === "||" ? "||" : ";"], ran };
422
+ }
423
+ return { ran };
424
+ }
425
+
426
+ const v = judge(process.env.RT_CALL ?? "", false, false);
427
+ process.stdout.write(v.hide ? `hide\t${v.hide}` : "pass");
428
+ ' 2>/dev/null)"
429
+
430
+ case "$verdict" in
431
+ hide*) ;;
432
+ *) exit 0 ;;
433
+ esac
434
+ tail_line="${verdict#hide }"
435
+
436
+ rt_hooks_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
437
+ ahead_cmd="bash ${rt_hooks_dir#"$PWD"/}/git-guard-push-ahead.sh"
438
+ # The log lies outside the working copy and is named by it: two copies running ahead at once do not
439
+ # write over each other, and a branch name with `/` makes no missing directory.
440
+ log='"${TMPDIR:-/tmp}/push-ahead-$(basename "$PWD").log"'
441
+
442
+ # shellcheck disable=SC1091
443
+ . "$rt_hooks_dir/decide.sh" 2>/dev/null
444
+ command -v rt_decide >/dev/null 2>&1 || exit 0
445
+ rt_decide push-ahead-call-guard.sh push_ahead_run '{"push_ahead_code_visible":"no"}' \
446
+ "$(jq -cn --arg t "$tail_line" --arg c "$ahead_cmd" --arg l "$log" '{tail:$t, ahead_cmd:$c, log:$l}')" PreToolUse "$input"
447
+ exit 0
@@ -14,7 +14,10 @@
14
14
  #
15
15
  # What `rt_push_set_run` found is left in the variables `rt_push_set_failed`, `_output`, `_skipped`,
16
16
  # `_held` and `_ahead` (one check per line) and in the counters `rt_push_set_ran` and
17
- # `rt_push_set_passed`.
17
+ # `rt_push_set_passed`. `rt_push_set_node` holds the line about a pinned node that is not installed:
18
+ # then no step ran, and the caller refuses by that line.
19
+ #
20
+ # Requires: hooks/facts.sh
18
21
 
19
22
  # The tree profile: first the package default, over it the project override. A function declared in
20
23
  # the override replaces the default whole and may call it back by the `_default` suffix. The root is
@@ -44,6 +47,11 @@ rt_push_set_profile() {
44
47
  command -v rt_push_subjects >/dev/null 2>&1 || rt_push_subjects() { cat >/dev/null; }
45
48
  command -v rt_push_subject_of >/dev/null 2>&1 || rt_push_subject_of() { printf unknown; }
46
49
  command -v rt_push_note_not_run >/dev/null 2>&1 || rt_push_note_not_run() { return 0; }
50
+ # The node the tree pins. No helper — the steps run on the node of PATH, as before.
51
+ # shellcheck disable=SC1091
52
+ command -v rt_node_pinned_dir >/dev/null 2>&1 || { [ -f "$hooks_dir/facts.sh" ] && . "$hooks_dir/facts.sh" 2>/dev/null; }
53
+ command -v rt_node_pinned_dir >/dev/null 2>&1 || rt_node_pinned_dir() { return 0; }
54
+ command -v rt_node_pin_mismatch >/dev/null 2>&1 || rt_node_pin_mismatch() { return 0; }
47
55
  # The line of a run for the metrics. No file — the set runs and records nothing.
48
56
  # shellcheck disable=SC1091
49
57
  [ -f "$hooks_dir/metric-marks.sh" ] && . "$hooks_dir/metric-marks.sh" 2>/dev/null
@@ -54,8 +62,14 @@ rt_push_set_profile() {
54
62
 
55
63
  rt_push_set_base() {
56
64
  local main_branch="${RT_MAIN_BRANCH:-main}"
65
+ # The tree the run ahead judged is pushed against the base that run took.
66
+ command -v rt_push_base_recorded >/dev/null 2>&1 && rt_push_base_recorded && return 0
57
67
  if command -v rt_push_base >/dev/null 2>&1; then
58
- rt_push_base "$main_branch"
68
+ if command -v rt_push_base_same >/dev/null 2>&1; then
69
+ rt_push_base_same "$(rt_push_base "$main_branch")"
70
+ else
71
+ rt_push_base "$main_branch"
72
+ fi
59
73
  return 0
60
74
  fi
61
75
  if git fetch --quiet origin "$main_branch" 2>/dev/null && git rev-parse --verify --quiet "origin/$main_branch" >/dev/null 2>&1; then
@@ -77,14 +91,9 @@ rt_push_set_base() {
77
91
  # variable empty and runs the step itself.
78
92
  rt_push_set_run() {
79
93
  local base="$1" check out status key_before skip_code="${RT_SKIP_CODE:-7}" sign started set_started set_took set_over over marked="" subjects
94
+ local node_dir hook_path="$PATH"
80
95
  case "$skip_code" in '' | 0 | 1 | *[!0-9]*) skip_code=7 ;; esac
81
96
  command -v rt_push_check_heavy >/dev/null 2>&1 || rt_push_check_heavy() { return 1; }
82
- sign="$(rt_tree_sign .)"
83
- # Whether the change touched the subject of each step, for the run lines; a step the selection
84
- # drops leaves a line of its own.
85
- subjects="$(rt_push_checks "$base" | rt_push_subjects "$base")"
86
- rt_push_note_not_run "$sign" "$base" "$subjects"
87
-
88
97
  rt_push_set_failed=""
89
98
  rt_push_set_output=""
90
99
  rt_push_set_skipped=""
@@ -92,6 +101,19 @@ rt_push_set_run() {
92
101
  rt_push_set_ahead=""
93
102
  rt_push_set_ran=0
94
103
  rt_push_set_passed=0
104
+ # The steps and their choice run on the node the tree pins: the hook may inherit another one,
105
+ # and a tree that demands its node turns every step red on it. A pinned node that is not
106
+ # installed, with another node in PATH, runs no step — the caller refuses by one line.
107
+ rt_push_set_node="$(rt_node_pin_mismatch "${CLAUDE_PROJECT_DIR:-.}")"
108
+ [ -n "$rt_push_set_node" ] && return 0
109
+ node_dir="$(rt_node_pinned_dir "${CLAUDE_PROJECT_DIR:-.}")"
110
+ [ -n "$node_dir" ] && PATH="$node_dir:$PATH"
111
+ sign="$(rt_tree_sign .)"
112
+ # Whether the change touched the subject of each step, for the run lines; a step the selection
113
+ # drops leaves a line of its own.
114
+ subjects="$(rt_push_checks "$base" | rt_push_subjects "$base")"
115
+ rt_push_note_not_run "$sign" "$base" "$subjects"
116
+
95
117
  set_started="$(date +%s 2>/dev/null)"
96
118
  while IFS= read -r check; do
97
119
  [ -z "$check" ] && continue
@@ -158,6 +180,7 @@ EOF
158
180
  }── the whole set
159
181
  ${set_over}"
160
182
  fi
183
+ PATH="$hook_path"
161
184
  return 0
162
185
  }
163
186
 
@@ -5,7 +5,7 @@
5
5
  # Turn exit guard: a turn does not end before the scenario of the task flow does. Stop.
6
6
  #
7
7
  # The engine measures the branch by the description and answers with a verdict: the turn is released
8
- # by a release case of the description — the end of the scenario, a stop word of the owner, a session
8
+ # by a release case of the description — the end of the scenario, the owner's Esc, a session
9
9
  # that wrote nothing, a role, the silent hosting, a menu that did not reach the owner — or held with
10
10
  # one move named, the next step of the scenario. This file hands the engine the input and turns its
11
11
  # verdict into the answer of the hook.
@@ -1,5 +1,5 @@
1
1
  # Turn judge
2
- <!-- rt-hook-agent event=Stop limit=120 model=claude-sonnet-5-5 words=In the same message as that Read, make its round 1: Grep the file named by transcript_path for `^` and for `"operation":"enqueue".*"content":"[^<]|"content":"The user answered: ` with output_mode count. -->
2
+ <!-- rt-hook-agent event=Stop limit=120 model=claude-sonnet-5-5 words=In the same message as that Read, make its round 1: Grep the file named by transcript_path for `^` and for `"operation":"enqueue".*"content":"[^<]|"content":"The user answered: |"type":"user","message":\{"role":"user","content":\[\{"type":"text","text":"\[Request interrupted by user` with output_mode count. -->
3
3
 
4
4
  You decide whether the agent's turn may end. You are called by the Stop event as an agent hook of
5
5
  Claude Code. The input JSON is given to you; its fields `transcript_path`, `last_assistant_message`,
@@ -24,12 +24,15 @@ and held answers the owner had asked for; do not repeat that.
24
24
  The owner's words lie in the transcript, a JSONL file with one record per line. One pattern finds
25
25
  all of them; it is called `OWNER` below:
26
26
 
27
- "operation":"enqueue".*"content":"[^<]|"content":"The user answered:
27
+ "operation":"enqueue".*"content":"[^<]|"content":"The user answered: |"type":"user","message":\{"role":"user","content":\[\{"type":"text","text":"\[Request interrupted by user
28
28
 
29
29
  - `"operation":"enqueue"` with a text that does not start with `<` — a message the owner typed, at
30
30
  the start of a turn or in the middle of one; its text is the field `content`. Queued texts that
31
31
  start with `<` are notifications of tasks and agents, and the pattern leaves them out.
32
32
  - `The user answered:` — the owner's answer to the question tool.
33
+ - `[Request interrupted by user` in a record `"type":"user"` — the owner pressed Esc. A record
34
+ `"type":"api-request-blob"` repeats an old mark next to a newer word, and the pattern leaves it
35
+ out.
33
36
 
34
37
  A record `"type":"last-prompt"` repeats an old typed message: it is never the owner's latest word.
35
38
 
@@ -55,8 +58,14 @@ the end of every turn.
55
58
  ## Let the turn end — answer `{"ok": true}` — when one of these holds
56
59
 
57
60
  - None of the cases below holds. Whether work is left is measured by the exit check, not by you.
58
- - The owner's latest word says to stop or to wait — «стоп», «хватит», «не сейчас», «потом»,
59
- «подожди», a refusal in an answer to the question tool — and the reply does not keep working.
61
+ - The owner's latest word is the Esc mark `[Request interrupted by user`: the owner stopped the
62
+ work, and a turn a notice woke after it ends without the work. An order typed before the mark
63
+ orders nothing until the owner writes again, one sent a second before the mark included.
64
+ - A background task the session started still runs, and the reply names the task and the step that
65
+ goes on by its notice. A task runs when its call ran in the background — `run_in_background` or
66
+ a background agent — and no `<task-notification>` with its id stands after it. The wait is the
67
+ move then: the notice wakes the session, and it reads the result and goes on with the step. A
68
+ word of the owner does not stop the work: the owner stops it by Esc.
60
69
  - `stop_hook_active` is true and the reason of the previous refusal is addressed, or cannot be
61
70
  addressed without the owner. Never refuse twice for the same reason: a second refusal for one
62
71
  reason is a deadlock in the making.
@@ -68,7 +77,8 @@ the end of every turn.
68
77
  answers the question or the swearing, reports, announces or promises — «следующим ходом»,
69
78
  «сейчас запущу», «дальше возьму» — and the ordered work is not in the calls of the turn. An
70
79
  announcement is not the action: a reply that announces or promises its next work is refused even
71
- when the turn did part of the ordered work. A turn whose calls did part of the ordered work and
80
+ when the turn did part of the ordered work. A reply that waits for a running background task of
81
+ the session, as named above, announces no work. A turn whose calls did part of the ordered work and
72
82
  whose reply announces nothing has the work in its calls. Swearing at an agent that stands means
73
83
  «why did you stop»: it is never a word to stop. Swearing that follows no order and names no work orders nothing. A slash
74
84
  command that writes a handover or closes the session is cleanup: the work it does not mention
@@ -100,4 +110,4 @@ The reason starts with `BLOCKED by turn-judge:` every time: refusals are counted
100
110
  transcripts. Write it in English, quote in « » the owner's words you rely on, and name one action
101
111
  the agent can take in this same turn: the work the owner's word ordered, the step it asked the owner
102
112
  to take, the command that shows the fact, the incident record, the sending of the proposal. Waiting
103
- is never the action.
113
+ is the action only for a background task of the session that still runs.