@rt-tools/agent-kit 0.33.1 → 0.35.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 (159) hide show
  1. package/README.md +14 -13
  2. package/assets/agents/goal-critic.md +76 -0
  3. package/assets/agents/product-strategist.md +95 -0
  4. package/assets/agents/qa-engineer.md +3 -0
  5. package/assets/checks/check-specs.mjs +11 -2
  6. package/assets/checks/rt-kit-checks.config.mjs +6 -0
  7. package/assets/checks/spec-common.mjs +6 -3
  8. package/assets/checks/spec-scenarios.mjs +9 -1
  9. package/assets/checks/task-new.github.mjs +6 -0
  10. package/assets/defaults/project.sh +49 -81
  11. package/assets/defaults/push.sh +100 -0
  12. package/assets/docs/GLOSSARY.md +4 -0
  13. package/assets/hooks/browser-guard-device-id.sh +1 -0
  14. package/assets/hooks/browser-guard-no-asking.sh +1 -0
  15. package/assets/hooks/browser-guard-no-listing.sh +1 -0
  16. package/assets/hooks/browser-guard-no-other-drivers.sh +7 -3
  17. package/assets/hooks/browser-guard-require-select.sh +1 -0
  18. package/assets/hooks/claim-guard.sh +7 -6
  19. package/assets/hooks/dev-server-guard.sh +1 -0
  20. package/assets/hooks/dispatch.sh +56 -3
  21. package/assets/hooks/docs-guard.sh +1 -0
  22. package/assets/hooks/end-conversation-guard.sh +1 -0
  23. package/assets/hooks/epic-stop-guard.sh +1 -0
  24. package/assets/hooks/exam-guard.sh +6 -1
  25. package/assets/hooks/git-guard-delivery-body.sh +25 -3
  26. package/assets/hooks/git-guard-delivery-draft.sh +23 -0
  27. package/assets/hooks/git-guard-delivery-folder.sh +38 -1
  28. package/assets/hooks/git-guard-delivery-pipeline.sh +99 -0
  29. package/assets/hooks/git-guard-delivery-signature.sh +19 -0
  30. package/assets/hooks/git-guard-delivery.sh +15 -19
  31. package/assets/hooks/git-guard-discard.sh +1 -0
  32. package/assets/hooks/git-guard-main.sh +18 -4
  33. package/assets/hooks/git-guard-no-verify.sh +1 -0
  34. package/assets/hooks/git-guard-push-ahead.sh +98 -0
  35. package/assets/hooks/git-guard-push-marks.sh +99 -0
  36. package/assets/hooks/git-guard-push-tests.sh +102 -83
  37. package/assets/hooks/grill-gate.sh +1 -0
  38. package/assets/hooks/guard-note.sh +11 -3
  39. package/assets/hooks/handover-mark.sh +37 -0
  40. package/assets/hooks/metric-marks.sh +233 -0
  41. package/assets/hooks/metric-send-start.sh +38 -0
  42. package/assets/hooks/observe.sh +103 -0
  43. package/assets/hooks/owner-corrections.sh +36 -0
  44. package/assets/hooks/pre-push.sh +113 -0
  45. package/assets/hooks/prose-style-guard.sh +1 -0
  46. package/assets/hooks/pull-outcomes.sh +59 -0
  47. package/assets/hooks/push-set.sh +160 -0
  48. package/assets/hooks/push-sign.sh +433 -0
  49. package/assets/hooks/qa-dataid-guard.sh +1 -0
  50. package/assets/hooks/rerun-guard.sh +15 -6
  51. package/assets/hooks/reuse-first-guard.sh +1 -0
  52. package/assets/hooks/role-calls.sh +120 -0
  53. package/assets/hooks/rule-source-guard.sh +16 -0
  54. package/assets/hooks/skill-gate.sh +11 -0
  55. package/assets/hooks/task-flow-draft-guard.sh +1 -0
  56. package/assets/hooks/task-flow-guard.sh +1 -0
  57. package/assets/hooks/tree-assignment-guard.sh +1 -0
  58. package/assets/hooks/turn-exit-epic.sh +24 -0
  59. package/assets/hooks/turn-exit-guard.sh +3 -23
  60. package/assets/hooks/turn-exit-patterns.sh +4 -3
  61. package/assets/hooks/turn-exit-verdict.sh +18 -1
  62. package/assets/hooks/waiting-turn-guard.sh +3 -1
  63. package/assets/hooks/window-fill-guard.sh +1 -0
  64. package/assets/hooks/work-start-guard.sh +1 -0
  65. package/assets/hooks/write-targets.sh +21 -0
  66. package/assets/patterns/git-workflow-commit.azure.md +5 -3
  67. package/assets/patterns/git-workflow-commit.github.md +16 -3
  68. package/assets/patterns/git-workflow-commit.gitlab.md +5 -3
  69. package/assets/patterns/git-workflow-pr-body.github.md +20 -3
  70. package/assets/patterns/git-workflow-pr.azure.md +30 -0
  71. package/assets/patterns/git-workflow-pr.github.md +57 -1
  72. package/assets/patterns/git-workflow-pr.gitlab.md +33 -0
  73. package/assets/patterns/mockups-measure.needs-mockups.md +19 -3
  74. package/assets/patterns/task-flow-close.md +32 -22
  75. package/assets/patterns/task-flow-cycle.md +72 -0
  76. package/assets/patterns/task-flow-start.md +3 -3
  77. package/assets/rules/browser-verification.md +4 -0
  78. package/assets/rules/deploy-flow.azure.md +5 -0
  79. package/assets/rules/deploy-flow.github.md +5 -0
  80. package/assets/rules/deploy-flow.gitlab.md +5 -0
  81. package/assets/rules/git-workflow.azure.md +15 -0
  82. package/assets/rules/git-workflow.github.md +15 -0
  83. package/assets/rules/git-workflow.gitlab.md +15 -0
  84. package/assets/rules/mockups.needs-mockups.md +13 -5
  85. package/assets/rules/task-flow.md +31 -25
  86. package/assets/samples/tasks/_template/plan.md +8 -0
  87. package/assets/skills/interface-review-annotate.needs-mockups.md +142 -0
  88. package/assets/skills/interface-review-icons.needs-app.md +136 -0
  89. package/assets/skills/interface-review-motion-states.needs-app.md +230 -0
  90. package/assets/skills/interface-review-motion.needs-app.md +209 -0
  91. package/assets/skills/interface-review-performance.needs-app.md +110 -0
  92. package/assets/skills/interface-review-principles.needs-app.md +263 -0
  93. package/assets/skills/interface-review-surfaces.needs-app.md +249 -0
  94. package/assets/skills/interface-review.needs-app.md +199 -0
  95. package/assets/skills/write-a-skill.md +16 -3
  96. package/assets/templates/postmortem.md +2 -0
  97. package/assets/workflows/feature.js +33 -5
  98. package/assets/workflows/plan.js +71 -4
  99. package/bin/agent-kit.d.ts.map +1 -1
  100. package/bin/agent-kit.js +28 -0
  101. package/bin/agent-kit.js.map +1 -1
  102. package/lib/cargo.d.ts +35 -0
  103. package/lib/cargo.d.ts.map +1 -1
  104. package/lib/cargo.js +76 -0
  105. package/lib/cargo.js.map +1 -1
  106. package/lib/commands.d.ts +9 -0
  107. package/lib/commands.d.ts.map +1 -1
  108. package/lib/commands.js +32 -19
  109. package/lib/commands.js.map +1 -1
  110. package/lib/config.d.ts +5 -0
  111. package/lib/config.d.ts.map +1 -1
  112. package/lib/config.js +1 -0
  113. package/lib/config.js.map +1 -1
  114. package/lib/file-lock.d.ts +4 -0
  115. package/lib/file-lock.d.ts.map +1 -0
  116. package/lib/file-lock.js +23 -0
  117. package/lib/file-lock.js.map +1 -0
  118. package/lib/metric-lines.d.ts +20 -0
  119. package/lib/metric-lines.d.ts.map +1 -0
  120. package/lib/metric-lines.js +117 -0
  121. package/lib/metric-lines.js.map +1 -0
  122. package/lib/metric-send-mark.d.ts +52 -0
  123. package/lib/metric-send-mark.d.ts.map +1 -0
  124. package/lib/metric-send-mark.js +69 -0
  125. package/lib/metric-send-mark.js.map +1 -0
  126. package/lib/metric-send.d.ts +18 -0
  127. package/lib/metric-send.d.ts.map +1 -0
  128. package/lib/metric-send.js +226 -0
  129. package/lib/metric-send.js.map +1 -0
  130. package/lib/metric-usage.d.ts +34 -0
  131. package/lib/metric-usage.d.ts.map +1 -0
  132. package/lib/metric-usage.js +111 -0
  133. package/lib/metric-usage.js.map +1 -0
  134. package/lib/metrics.d.ts +92 -0
  135. package/lib/metrics.d.ts.map +1 -0
  136. package/lib/metrics.js +263 -0
  137. package/lib/metrics.js.map +1 -0
  138. package/lib/observations-cargo.d.ts +2 -0
  139. package/lib/observations-cargo.d.ts.map +1 -1
  140. package/lib/observations-cargo.js +1 -1
  141. package/lib/observations-cargo.js.map +1 -1
  142. package/lib/observations.d.ts +22 -2
  143. package/lib/observations.d.ts.map +1 -1
  144. package/lib/observations.js +40 -13
  145. package/lib/observations.js.map +1 -1
  146. package/lib/outcomes.d.ts +89 -0
  147. package/lib/outcomes.d.ts.map +1 -0
  148. package/lib/outcomes.js +274 -0
  149. package/lib/outcomes.js.map +1 -0
  150. package/lib/ship.js +1 -1
  151. package/lib/ship.js.map +1 -1
  152. package/lib/shipment.d.ts.map +1 -1
  153. package/lib/shipment.js +7 -1
  154. package/lib/shipment.js.map +1 -1
  155. package/lib/snapshot.d.ts +2 -0
  156. package/lib/snapshot.d.ts.map +1 -1
  157. package/lib/snapshot.js +6 -0
  158. package/lib/snapshot.js.map +1 -1
  159. package/package.json +1 -1
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env bash
2
+ # The gate set before a push, one way for both who run it. NOT a guard: it has no `rt-hook:`
3
+ # declaration and hooks into no agent event. The session guard of the push and the git hook
4
+ # `pre-push` source it and call its functions.
5
+ #
6
+ # Two copies of the same loop drifted apart in their first week: the hook started the heavy checks
7
+ # after a red one, said nothing about a skipped check and wrote the mark of green when nothing had
8
+ # been checked at all. Here lives the single one.
9
+ #
10
+ # rt_push_set_profile <hooks dir> <tree root> — the package default and the tree override
11
+ # rt_push_set_base — the main branch of the remote, or empty
12
+ # rt_push_set_run <base> — every line of `rt_push_checks <base>`
13
+ # rt_push_mark_path / rt_push_mark_covers <commit> / rt_push_mark_write — the mark of green
14
+ #
15
+ # What `rt_push_set_run` found is left in the variables `rt_push_set_failed`, `_output`, `_skipped`,
16
+ # `_held` and `_ahead` (one check per line) and in the counters `rt_push_set_ran` and
17
+ # `rt_push_set_passed`.
18
+
19
+ # The tree profile: first the package default, over it the project override. A function declared in
20
+ # the override replaces the default whole and may call it back by the `_default` suffix. The root is
21
+ # exported, because the profile functions find the tree files by it: a root inherited from another
22
+ # session reads that session's tree, and the set comes out narrower without a word.
23
+ rt_push_set_profile() {
24
+ local hooks_dir="$1" root="$2" profile
25
+ export CLAUDE_PROJECT_DIR="$root"
26
+ for profile in "$hooks_dir/../rt-kit/defaults/project.sh" "$hooks_dir/../defaults/project.sh" \
27
+ "$root/.claude/rt-kit/defaults/project.sh" "$root/.claude/rt-kit/project.sh"; do
28
+ # shellcheck disable=SC1090
29
+ [ -f "$profile" ] && . "$profile" 2>/dev/null
30
+ done
31
+ # The base of a stacked branch and the marks of a green run. No helper — the base is the main
32
+ # branch, no mark is read and none is written: the set runs whole.
33
+ # shellcheck disable=SC1091
34
+ [ -f "$hooks_dir/git-guard-push-marks.sh" ] && . "$hooks_dir/git-guard-push-marks.sh" 2>/dev/null
35
+ command -v rt_push_mark_has >/dev/null 2>&1 || rt_push_mark_has() { return 1; }
36
+ command -v rt_push_mark_put >/dev/null 2>&1 || rt_push_mark_put() { return 0; }
37
+ command -v rt_push_mark_clean >/dev/null 2>&1 || rt_push_mark_clean() { return 1; }
38
+ command -v rt_push_mark_key >/dev/null 2>&1 || rt_push_mark_key() { return 1; }
39
+ command -v rt_push_heavy_ahead >/dev/null 2>&1 || rt_push_heavy_ahead() { return 1; }
40
+ # The line of a run for the metrics. No file — the set runs and records nothing.
41
+ # shellcheck disable=SC1091
42
+ [ -f "$hooks_dir/metric-marks.sh" ] && . "$hooks_dir/metric-marks.sh" 2>/dev/null
43
+ command -v rt_run_note >/dev/null 2>&1 || rt_run_note() { return 0; }
44
+ command -v rt_tree_sign >/dev/null 2>&1 || rt_tree_sign() { return 0; }
45
+ return 0
46
+ }
47
+
48
+ rt_push_set_base() {
49
+ local main_branch="${RT_MAIN_BRANCH:-main}"
50
+ if command -v rt_push_base >/dev/null 2>&1; then
51
+ rt_push_base "$main_branch"
52
+ return 0
53
+ fi
54
+ if git fetch --quiet origin "$main_branch" 2>/dev/null && git rev-parse --verify --quiet "origin/$main_branch" >/dev/null 2>&1; then
55
+ printf 'origin/%s' "$main_branch"
56
+ fi
57
+ return 0
58
+ }
59
+
60
+ # Every red among the light checks is named by one refusal. After the first red a heavy check is not
61
+ # started: the push is refused anyway, and its minutes buy nothing.
62
+ #
63
+ # The code by which a check declares that there was nothing to look at does not refuse the push, and
64
+ # it is counted apart from a passed one. A skip code of 0 or 1 is no skip code: 1 is the exit of any
65
+ # failure, and every red check would read as skipped.
66
+ #
67
+ # A check green on this very tree against this very base is not run again. A caller under the hook
68
+ # limit sets `rt_push_set_ask_ahead=1`: in a tree that runs its heavy steps ahead such a step is then
69
+ # named in `rt_push_set_ahead` instead of being started. The git hook has no such limit, leaves the
70
+ # variable empty and runs the step itself.
71
+ rt_push_set_run() {
72
+ local base="$1" check out status key_before skip_code="${RT_SKIP_CODE:-7}" sign started
73
+ case "$skip_code" in '' | 0 | 1 | *[!0-9]*) skip_code=7 ;; esac
74
+ command -v rt_push_check_heavy >/dev/null 2>&1 || rt_push_check_heavy() { return 1; }
75
+ sign="$(rt_tree_sign .)"
76
+
77
+ rt_push_set_failed=""
78
+ rt_push_set_output=""
79
+ rt_push_set_skipped=""
80
+ rt_push_set_held=""
81
+ rt_push_set_ahead=""
82
+ rt_push_set_ran=0
83
+ rt_push_set_passed=0
84
+ while IFS= read -r check; do
85
+ [ -z "$check" ] && continue
86
+ if rt_push_mark_has "$check" "$base"; then
87
+ rt_push_set_ran=$((rt_push_set_ran + 1))
88
+ rt_push_set_passed=$((rt_push_set_passed + 1))
89
+ continue
90
+ fi
91
+ if [ "${rt_push_set_ask_ahead:-}" = 1 ] && rt_push_heavy_ahead && rt_push_check_heavy "$check"; then
92
+ rt_push_set_ahead="${rt_push_set_ahead}${rt_push_set_ahead:+
93
+ }${check}"
94
+ continue
95
+ fi
96
+ if [ -n "$rt_push_set_failed" ] && rt_push_check_heavy "$check"; then
97
+ rt_push_set_held="${rt_push_set_held}${rt_push_set_held:+
98
+ }${check}"
99
+ continue
100
+ fi
101
+ rt_push_set_ran=$((rt_push_set_ran + 1))
102
+ key_before=""
103
+ rt_push_mark_clean && key_before="$(rt_push_mark_key "$check" "$base" 2>/dev/null)"
104
+ started="$(date +%s 2>/dev/null)"
105
+ out="$(eval "$check" 2>&1 </dev/null)"
106
+ status=$?
107
+ if [ "$status" -eq 0 ]; then
108
+ rt_push_set_passed=$((rt_push_set_passed + 1))
109
+ rt_push_mark_put "$check" "$base" "$key_before"
110
+ rt_run_note "$sign" "$started" green "$check"
111
+ continue
112
+ fi
113
+ if [ "$status" -eq "$skip_code" ]; then
114
+ rt_push_set_skipped="${rt_push_set_skipped}${rt_push_set_skipped:+
115
+ }${check}"
116
+ rt_run_note "$sign" "$started" skipped "$check"
117
+ continue
118
+ fi
119
+ rt_run_note "$sign" "$started" red "$check"
120
+ rt_push_set_failed="${rt_push_set_failed}${rt_push_set_failed:+
121
+ }${check}"
122
+ rt_push_set_output="${rt_push_set_output}${rt_push_set_output:+
123
+
124
+ }── ${check}
125
+ $(printf '%s' "$out" | tail -n 25)"
126
+ done <<EOF
127
+ $(rt_push_checks "$base")
128
+ EOF
129
+ return 0
130
+ }
131
+
132
+ # The mark of green: the commit a green set judged, one line. It lies in the shared git directory, not
133
+ # in that of a working copy: a push from a session through `git -C` into a second copy would
134
+ # otherwise not see the mark the guard just wrote and run the set a second time.
135
+ rt_push_mark_path() {
136
+ local common
137
+ common="$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" || return 0
138
+ [ -n "$common" ] && printf '%s/rt-kit-push-green' "$common"
139
+ return 0
140
+ }
141
+
142
+ rt_push_mark_covers() {
143
+ local commit="$1" mark
144
+ mark="$(rt_push_mark_path)"
145
+ [ -n "$commit" ] && [ -n "$mark" ] && [ -f "$mark" ] && [ "$(head -n 1 "$mark" 2>/dev/null)" = "$commit" ]
146
+ }
147
+
148
+ # The mark is written only for a set that judged the commit itself. A set where every check was
149
+ # skipped judged nothing. A working copy with uncommitted edits to tracked files was judged
150
+ # together with them, so the commit alone was never judged. Files outside the history do not count:
151
+ # a working copy keeps folders of other tasks, and the mark would never be written.
152
+ rt_push_mark_write() {
153
+ local mark tip
154
+ [ "${rt_push_set_passed:-0}" -gt 0 ] || return 0
155
+ [ -z "$(git status --porcelain --untracked-files=no 2>/dev/null)" ] || return 0
156
+ mark="$(rt_push_mark_path)"
157
+ tip="$(git rev-parse --verify --quiet HEAD 2>/dev/null)"
158
+ [ -n "$mark" ] && [ -n "$tip" ] && printf '%s\n' "$tip" >"$mark" 2>/dev/null
159
+ return 0
160
+ }
@@ -0,0 +1,433 @@
1
+ #!/usr/bin/env bash
2
+ # The parse of a command for the push gate. NOT a guard: it has no `rt-hook:` declaration and hooks
3
+ # into no agent event. The push gate sources it and calls `rt_push_calls` with the command text on
4
+ # the input; it stands apart because the guard with it inside crossed the length limit for code.
5
+ #
6
+ # The push calls are found by a parse of the command as the shell reads it, not by patterns over
7
+ # the line. A push is the subcommand `push` of a `git` call, after the keys of `git` itself: `-c`
8
+ # and `-C` carry the credentials helper and the request header, and that is the form pushed with
9
+ # here. Patterns over the line missed in both directions. They cut `-c 'h: a; b'` at the `;`
10
+ # inside the quotes and let that push through; they did not see `git` after `{`, `if`, `while`,
11
+ # backticks, `bash -c` and `eval`; and they read the word in a filter of another command as a push,
12
+ # running the whole set for more than ten minutes on a command that sends nothing.
13
+ #
14
+ # Quoted text is data, except where the shell runs it: `$( )`, backticks, the string of `bash -c`
15
+ # and the words of `eval` are parsed as commands. The body of a heredoc with a quoted delimiter is
16
+ # data; with a bare delimiter its `$( )` and backticks run and are parsed. A heredoc given to a
17
+ # shell is run whole and parsed whole. A function body runs only when the function is called, so it
18
+ # is parsed at the call, with the call words put in place of `$@` and `$1`; a function named `git`
19
+ # does not hide the git call. A wrapper is passed with its keys and their values — `time -p`,
20
+ # `timeout -s KILL 5`, `sudo -iu x`; the string of `env -S` is parsed as a command. `xargs`,
21
+ # `parallel`, `find -exec`, `watch` and `repeat` run their command more than once, so a push under
22
+ # them counts as a push in a loop. A stashed edit is no push — `git stash push` has the subcommand
23
+ # `stash` and sends nothing outside.
24
+ #
25
+ # One line per push call: whether it stands inside a loop (`for`, `while`, `until`, from the
26
+ # keyword to its `done`), whether it sends several branches, whether it is a dry run. Several
27
+ # branches are `--all`, `--mirror`, `--branches`, a ref with `*` or with a brace expansion, or two
28
+ # refs after the remote; a ref that starts with `:` deletes and is not counted. `$( )` and a quoted
29
+ # word stay one word. With `--repo` every word after the keys counts as a ref: git takes the first
30
+ # of them for the remote, and the sign errs towards a refusal it does not need rather than a push it
31
+ # does not see.
32
+ #
33
+ # Two more lines. `switch` — a move to another tree: a switch to an existing branch, a new branch
34
+ # from another start point, `gh pr checkout`; restoring files — after `--`, after a tree word, by
35
+ # `--ours`, `-p` — and a new branch from the current tip are no switch. `opaque` — commands the parse cannot read: a shell reading them from
36
+ # its input, `source` of a substitution, or a text whose function calls grow without bound.
37
+ #
38
+ # FAIL-OPEN: no perl — no output, and the guard passes, like any other breakage of it.
39
+
40
+ rt_push_calls() {
41
+ command -v perl >/dev/null 2>&1 || return 0
42
+ perl -e '
43
+ use strict; use warnings;
44
+ local $/; my $text = <STDIN>; $text = "" unless defined $text;
45
+ my @out; my $calls = 0;
46
+ sub heredoc_word {
47
+ my ($s, $i) = @_; my $n = length $s; my $strip = 0;
48
+ if (substr($s, $i, 1) eq "-") { $strip = 1; $i++; }
49
+ $i++ while $i < $n && substr($s, $i, 1) =~ /[ \t]/;
50
+ my ($delim, $quoted) = ("", 0);
51
+ while ($i < $n && substr($s, $i, 1) !~ /[\s;&|<>()]/) { my $x = substr($s, $i, 1); if ($x =~ /["\x27\\]/) { $quoted = 1; } else { $delim .= $x; } $i++; }
52
+ return ($delim, $strip, $quoted, $i);
53
+ }
54
+ sub heredoc_body {
55
+ my ($s, $i, $delim, $strip) = @_; my $n = length $s; my $body = "";
56
+ while ($i < $n) {
57
+ my $e = index($s, "\n", $i); my $line = $e < 0 ? substr($s, $i) : substr($s, $i, $e - $i);
58
+ $i = $e < 0 ? $n : $e + 1; $line =~ s/^\t+// if $strip; last if $line eq $delim;
59
+ $body .= "$line\n";
60
+ }
61
+ return ($body, $i);
62
+ }
63
+ sub ansi_end {
64
+ my ($s, $i) = @_; my $n = length $s;
65
+ while ($i < $n) { my $c = substr($s, $i, 1); if ($c eq "\\") { $i += 2; next; } return $i if $c eq "\x27"; $i++; }
66
+ return $n;
67
+ }
68
+ sub balanced {
69
+ my ($s, $i) = @_; my $n = length $s; my $d = 1; my $st = $i; my @hd;
70
+ while ($i < $n) {
71
+ my $c = substr($s, $i, 1);
72
+ if ($c eq "\n" && @hd) { $i++; while (my $h = shift @hd) { (undef, $i) = heredoc_body($s, $i, @$h); } next; }
73
+ if ($c eq "\$" && substr($s, $i + 1, 1) eq "\x27") { $i = ansi_end($s, $i + 2) + 1; next; }
74
+ if ($c eq "\x27") { my $e = index($s, "\x27", $i + 1); $i = $e < 0 ? $n : $e + 1; next; }
75
+ if ($c eq "\"") { $i++; while ($i < $n && substr($s, $i, 1) ne "\"") { $i++ if substr($s, $i, 1) eq "\\"; $i++; } $i++; next; }
76
+ if ($c eq "\\") { $i += 2; next; }
77
+ if (substr($s, $i, 3) eq "<<<") { $i += 3; next; }
78
+ if (substr($s, $i, 2) eq "<<") { my ($dl, $sp, undef, $j) = heredoc_word($s, $i + 2); push @hd, [$dl, $sp]; $i = $j; next; }
79
+ $d++ if $c eq "(";
80
+ if ($c eq ")") { $d--; return (substr($s, $st, $i - $st), $i + 1) if $d == 0; }
81
+ $i++;
82
+ }
83
+ return (substr($s, $st), $n);
84
+ }
85
+ sub expansions {
86
+ my ($s) = @_; my $n = length $s; my $i = 0; my @subs;
87
+ while ($i < $n) {
88
+ my $c = substr($s, $i, 1);
89
+ if ($c eq "\\") { $i += 2; next; }
90
+ if ($c eq "\$" && substr($s, $i + 1, 1) eq "(") { my ($in, $j) = balanced($s, $i + 2); push @subs, $in; $i = $j; next; }
91
+ if ($c eq "`") { my $e = index($s, "`", $i + 1); $e = $n if $e < 0; push @subs, substr($s, $i + 1, $e - $i - 1); $i = $e + 1; next; }
92
+ $i++;
93
+ }
94
+ return @subs;
95
+ }
96
+ sub tokenize {
97
+ my ($s) = @_; my $n = length $s; my $i = 0; my @t; my @heredocs;
98
+ my ($w, $have, $q, @subs) = ("", 0, 0);
99
+ my $flush = sub { push @t, { t => "w", v => $w, q => $q, subs => [@subs] } if $have; $w = ""; $have = 0; $q = 0; @subs = (); };
100
+ while ($i < $n) {
101
+ my $c = substr($s, $i, 1);
102
+ if ($c eq "\n") {
103
+ $flush->(); push @t, { t => "s", v => "\n" }; $i++;
104
+ while (my $h = shift @heredocs) {
105
+ my ($delim, $strip, $doc) = @$h;
106
+ ($doc->{body}, $i) = heredoc_body($s, $i, $delim, $strip);
107
+ $doc->{subs} = [expansions($doc->{body})] unless $doc->{quoted};
108
+ }
109
+ next;
110
+ }
111
+ if ($c eq " " || $c eq "\t") { $flush->(); $i++; next; }
112
+ if ($c eq "#" && !$have) { my $e = index($s, "\n", $i); $i = $e < 0 ? $n : $e; next; }
113
+ if ($c eq "(" && substr($s, $i + 1, 1) eq "(" && !$have) {
114
+ my ($d, $j) = (0, $i);
115
+ while ($j < $n) { my $x = substr($s, $j, 1); $d++ if $x eq "("; if ($x eq ")") { $d--; if ($d == 0) { $j++; last; } } $j++; }
116
+ push @t, { t => "s", v => ";" }; $i = $j; next;
117
+ }
118
+ if ($c eq "\\") {
119
+ if (substr($s, $i + 1, 1) eq "\n" || $i + 1 >= $n) { $i += 2; next; }
120
+ $w .= substr($s, $i + 1, 1) if $i + 1 < $n; $have = 1; $q = 1; $i += 2; next;
121
+ }
122
+ if ($c eq "\$" && substr($s, $i + 1, 1) eq "\x27") {
123
+ my $e = ansi_end($s, $i + 2); my $raw = substr($s, $i + 2, $e - $i - 2); $raw =~ s/\\(.)/$1/gs;
124
+ $w .= $raw; $have = 1; $q = 1; $i = $e + 1; next;
125
+ }
126
+ if ($c eq "\x27") { my $e = index($s, "\x27", $i + 1); $e = $n if $e < 0; $w .= substr($s, $i + 1, $e - $i - 1); $have = 1; $q = 1; $i = $e + 1; next; }
127
+ if ($c eq "\"") {
128
+ $i++; $have = 1; $q = 1;
129
+ while ($i < $n) {
130
+ my $d = substr($s, $i, 1);
131
+ if ($d eq "\"") { $i++; last; }
132
+ if ($d eq "\\" && $i + 1 < $n) { $w .= substr($s, $i + 1, 1) unless substr($s, $i + 1, 1) eq "\n"; $i += 2; next; }
133
+ if ($d eq "\$" && substr($s, $i + 1, 1) eq "(") { my ($in, $j) = balanced($s, $i + 2); push @subs, $in; $w .= "\$(...)"; $i = $j; next; }
134
+ if ($d eq "`") { my $e = index($s, "`", $i + 1); $e = $n if $e < 0; push @subs, substr($s, $i + 1, $e - $i - 1); $w .= "`...`"; $i = $e + 1; next; }
135
+ $w .= $d; $i++;
136
+ }
137
+ next;
138
+ }
139
+ if ($c eq "\$" && substr($s, $i + 1, 1) eq "(") { my ($in, $j) = balanced($s, $i + 2); push @subs, $in; $w .= "\$(...)"; $have = 1; $i = $j; next; }
140
+ if ($c eq "`") { my $e = index($s, "`", $i + 1); $e = $n if $e < 0; push @subs, substr($s, $i + 1, $e - $i - 1); $w .= "`...`"; $have = 1; $i = $e + 1; next; }
141
+ if (substr($s, $i, 2) eq "<<" && substr($s, $i, 3) ne "<<<") {
142
+ if ($have && $w =~ /^\d+$/) { $w = ""; $have = 0; $q = 0; @subs = (); } else { $flush->(); }
143
+ my ($delim, $strip, $quoted, $j) = heredoc_word($s, $i + 2);
144
+ my $doc = { t => "d", body => "", quoted => $quoted, subs => [] };
145
+ push @t, $doc; push @heredocs, [$delim, $strip, $doc]; $i = $j; next;
146
+ }
147
+ if ($c =~ /[<>]/ || ($c eq "&" && substr($s, $i + 1, 1) eq ">")) {
148
+ if ($have && $w =~ /^\d+$/) { $w = ""; $have = 0; $q = 0; @subs = (); } else { $flush->(); }
149
+ my $j = $i; $i++ while $i < $n && substr($s, $i, 1) =~ /[<>&|]/;
150
+ push @t, { t => "r", v => substr($s, $j, $i - $j) }; next;
151
+ }
152
+ if ($c =~ /[;&|()]/) {
153
+ $flush->(); push @t, { t => "s", v => $c };
154
+ my $two = substr($s, $i, 2); $i += ($two =~ /^(&&|\|\||;;|\|&)$/) ? 2 : 1; next;
155
+ }
156
+ $w .= $c; $have = 1; $i++;
157
+ }
158
+ $flush->(); return @t;
159
+ }
160
+ my (%fn, %busy);
161
+ my $none = qr/^$/;
162
+ my %wrap = (
163
+ command => $none, builtin => $none, nohup => $none, setsid => $none, coproc => $none, arch => qr/^-arch$/,
164
+ exec => qr/^-a$/, caffeinate => qr/^(-t|-w)$/, nice => qr/^(-n|--adjustment)$/,
165
+ sudo => qr/^(-u|-g|-C|-D|-h|-p|-U|-r|-t|--user|--group|--chdir|--host|--prompt|--other-user|--role|--type)$/,
166
+ doas => qr/^(-u|-C)$/, ionice => qr/^(-c|-n|-p|-P|-u|--class|--classdata|--pid|--pgid|--uid)$/,
167
+ stdbuf => qr/^(-i|-o|-e|--input|--output|--error)$/, timeout => qr/^(-s|-k|--signal|--kill-after)$/,
168
+ time => qr/^(-f|-o|--format|--output)$/, watch => qr/^(-n|--interval)$/, repeat => $none,
169
+ xargs => qr/^(-I|-n|-L|-P|-d|-E|-s|-a|--max-args|--max-lines|--max-procs|--delimiter|--eof|--max-chars|--arg-file|--process-slot-var)$/,
170
+ parallel => qr/^(-j|-S|-I|-n|-L|-P|-d|-E|--jobs|--sshlogin|--max-args|--delimiter)$/,
171
+ );
172
+ my %repeats = map { $_ => 1 } qw(xargs parallel watch repeat);
173
+ sub parse { my ($s, $loop) = @_; return parse_tokens([tokenize($s)], $loop); }
174
+ sub opens {
175
+ my ($t, $j) = @_;
176
+ return 1 if $j == 0 || $t->[$j - 1]{t} eq "s";
177
+ my $p = $t->[$j - 1];
178
+ return $p->{t} eq "w" && !$p->{q} && $p->{v} =~ /^(fi|done|esac|\{|\}|then|do|else|elif|if|while|until|!)$/;
179
+ }
180
+ sub closes {
181
+ my ($t, $j) = @_;
182
+ return $j + 1 >= @$t || $t->[$j + 1]{t} eq "s";
183
+ }
184
+ sub body_end {
185
+ my ($t, $i) = @_;
186
+ $i++ while $i < @$t && $t->[$i]{t} eq "s" && $t->[$i]{v} eq "\n";
187
+ return -1 if $i >= @$t;
188
+ my ($f, $d) = ($t->[$i], 0);
189
+ if ($f->{t} eq "w" && $f->{v} eq "{" && !$f->{q}) {
190
+ for (my $j = $i; $j < @$t; $j++) {
191
+ my $x = $t->[$j];
192
+ next unless $x->{t} eq "w" && !$x->{q};
193
+ $d++ if $x->{v} eq "{" && ($j == $i || opens($t, $j));
194
+ if ($x->{v} eq "}" && (opens($t, $j) || closes($t, $j))) { $d--; return $j if $d == 0; }
195
+ }
196
+ return $#$t;
197
+ }
198
+ if ($f->{t} eq "s" && $f->{v} eq "(") {
199
+ for (my $j = $i; $j < @$t; $j++) {
200
+ my $x = $t->[$j]; next unless $x->{t} eq "s";
201
+ $d++ if $x->{v} eq "(";
202
+ if ($x->{v} eq ")") { $d--; return $j if $d == 0; }
203
+ }
204
+ return $#$t;
205
+ }
206
+ return -1;
207
+ }
208
+ sub parse_tokens {
209
+ my ($t, $loop) = @_; my @cmd;
210
+ for (my $i = 0; $i <= @$t; $i++) {
211
+ my $tok = $i < @$t ? $t->[$i] : { t => "s", v => "" };
212
+ if ($tok->{t} eq "s") { $loop = command(\@cmd, $loop) if @cmd; @cmd = (); next; }
213
+ if (!@cmd && $tok->{t} eq "w" && !$tok->{q}) {
214
+ my ($name, $j);
215
+ next if $tok->{v} eq "function" && $i + 1 < @$t && $t->[$i + 1]{t} eq "w" && $t->[$i + 1]{v} eq "{";
216
+ if ($tok->{v} eq "function" && $i + 1 < @$t && $t->[$i + 1]{t} eq "w") {
217
+ ($name, $j) = ($t->[$i + 1]{v}, $i + 2);
218
+ $j += 2 if $j + 1 < @$t && $t->[$j]{t} eq "s" && $t->[$j]{v} eq "(" && $t->[$j + 1]{t} eq "s" && $t->[$j + 1]{v} eq ")";
219
+ } elsif ($i + 2 < @$t && $t->[$i + 1]{t} eq "s" && $t->[$i + 1]{v} eq "(" && $t->[$i + 2]{t} eq "s" && $t->[$i + 2]{v} eq ")") {
220
+ ($name, $j) = ($tok->{v}, $i + 3);
221
+ }
222
+ if (defined $name) {
223
+ my $end = body_end($t, $j);
224
+ if ($end >= 0) { $fn{$name} = [@$t[$j .. $end]]; $i = $end; next; }
225
+ }
226
+ }
227
+ push @cmd, $tok;
228
+ }
229
+ return $loop;
230
+ }
231
+ sub bind_text {
232
+ my ($s, @args) = @_;
233
+ $s =~ s/"?\$\{?[@*](?::(\d+))?\}?"?/join(" ", @args[(defined $1 && $1 > 0 ? $1 - 1 : 0) .. $#args])/ge;
234
+ $s =~ s/\$\{?([1-9])\}?/defined $args[$1 - 1] ? $args[$1 - 1] : ""/ge;
235
+ return $s;
236
+ }
237
+ sub bind_args {
238
+ my ($body, @args) = @_; my @t;
239
+ for my $x (@$body) {
240
+ my $y = $x->{subs} && @{ $x->{subs} } ? { %$x, subs => [map { bind_text($_, @args) } @{ $x->{subs} }] } : $x;
241
+ if ($y->{t} eq "w" && $y->{v} =~ /^\$\{?[@*](?::(\d+))?\}?$/) {
242
+ my $from = defined $1 && $1 > 0 ? $1 - 1 : 0;
243
+ push @t, { t => "w", v => $_, q => 1, subs => [] } for @args[$from .. $#args];
244
+ next;
245
+ }
246
+ if ($y->{t} eq "w" && $y->{v} =~ /^\$\{?([1-9])\}?$/) { push @t, { %$y, v => defined $args[$1 - 1] ? $args[$1 - 1] : "" }; next; }
247
+ push @t, $y;
248
+ }
249
+ return \@t;
250
+ }
251
+ sub command {
252
+ my ($c, $loop) = @_; my (@w, @docs); my ($target, $input) = ("", 0);
253
+ my @c = @$c; pop @c if @c > 1 && $c[-1]{t} eq "w" && !$c[-1]{q} && $c[-1]{v} eq "}";
254
+ for my $x (@c) {
255
+ if ($x->{t} eq "r") { $target = $x->{v}; next; }
256
+ if ($x->{t} eq "d") { push @docs, $x; next; }
257
+ parse($_, $loop) for @{ $x->{subs} || [] };
258
+ if ($target ne "") {
259
+ if ($target eq "<<<") { push @docs, { body => $x->{v}, subs => [] }; } elsif ($target =~ /^</) { $input = 1; }
260
+ $target = ""; next;
261
+ }
262
+ push @w, $x->{v};
263
+ }
264
+ return words(\@w, $loop, 0, \@docs, $input);
265
+ }
266
+ sub words {
267
+ my ($wr, $loop, $rep, $docs, $input) = @_; my @w = @$wr; my $nofn = 0;
268
+ my $run = sub { my ($dl) = @_; parse($_, $dl) for map { @{ $_->{subs} || [] } } @$docs; $docs = []; };
269
+ while (@w) {
270
+ my $k = $w[0]; my $in = $loop + ($rep ? 1 : 0);
271
+ if ($k =~ /^[A-Za-z_][A-Za-z0-9_]*=/) { shift @w; next; }
272
+ if ($k eq "while" || $k eq "until") { $loop++; shift @w; next; }
273
+ if ($k eq "for" || $k eq "select" || $k eq "foreach") { $run->($loop); return $loop + 1; }
274
+ if ($k eq "repeat" && (@w < 3 || $w[2] eq "do")) { $loop++; splice(@w, 0, @w < 3 ? scalar(@w) : 3); next; }
275
+ if ($k eq "done" || $k eq "end") { $loop-- if $loop > 0; shift @w; next; }
276
+ if ($k eq "-" || $k eq "noglob" || $k eq "nocorrect") { shift @w; next; }
277
+ $w[0] =~ s/^=(?=[A-Za-z])// and next;
278
+ if ($k =~ /^(if|then|else|elif|fi|do|\{|\}|!)$/) { shift @w; next; }
279
+ if ($k eq "env") {
280
+ shift @w;
281
+ while (@w && ($w[0] =~ /^-/ || $w[0] =~ /^[A-Za-z_][A-Za-z0-9_]*=/)) {
282
+ my $o = shift @w; my $str;
283
+ if ($o =~ /^--split-string=?(.*)$/s) { $str = $1; }
284
+ elsif ($o =~ /^-([A-Za-z]*?)S(.*)$/s) { my ($pre, $rest) = ($1, $2); $str = $rest unless $pre =~ /[uC]/; }
285
+ if (defined $str) {
286
+ $str = shift @w unless length $str;
287
+ $run->($loop); parse(join(" ", (defined $str ? $str : ""), @w), $in);
288
+ return $loop;
289
+ }
290
+ shift @w if @w && $o =~ /^(-u|--unset|-C|--chdir)$/;
291
+ }
292
+ next;
293
+ }
294
+ if ($k eq "flock") {
295
+ shift @w; my $file = 0;
296
+ while (@w) {
297
+ my $o = $w[0];
298
+ if ($o eq "-c" || $o eq "--command") { $run->($loop); parse(defined $w[1] ? $w[1] : "", $in); return $loop; }
299
+ if ($o =~ /^-/) { shift @w; shift @w if @w && $o =~ /^(-w|-E|--timeout|--conflict-exit-code)$/; next; }
300
+ last if $file; shift @w; $file = 1;
301
+ }
302
+ next;
303
+ }
304
+ if ($k eq "script") {
305
+ shift @w;
306
+ while (@w && $w[0] =~ /^-/) {
307
+ my $o = shift @w;
308
+ if ($o eq "-c" || $o eq "--command") { $run->($loop); parse(defined $w[0] ? $w[0] : "", $in); return $loop; }
309
+ shift @w if @w && $o =~ /^(-t|-F|-T|-I|-O|-B|-E|--timing|--log-in|--log-out|--log-io|--echo)$/;
310
+ }
311
+ shift @w; next;
312
+ }
313
+ if (exists $wrap{$k}) {
314
+ my $v = $wrap{$k}; shift @w;
315
+ if ($k eq "command" && @w && $w[0] =~ /^-[vV]$/) { $run->($loop); return $loop; }
316
+ while (@w && ($w[0] =~ /^-/ || $w[0] =~ /^[A-Za-z_][A-Za-z0-9_]*=/ || $w[0] =~ /^[0-9.]+[smhd]?$/)) {
317
+ my $o = shift @w; my $last = $o =~ /^-([A-Za-z]{2,})$/ ? "-" . substr($1, -1) : $o;
318
+ shift @w if @w && ($o =~ $v || $last =~ $v);
319
+ }
320
+ $rep = 1 if $repeats{$k};
321
+ $nofn = 1 if $k eq "command" || $k eq "builtin";
322
+ next;
323
+ }
324
+ last;
325
+ }
326
+ unless (@w) { $run->($loop); return $loop; }
327
+ my $in = $loop + ($rep ? 1 : 0);
328
+ (my $name = $w[0]) =~ s{.*/}{};
329
+ my $shell = $name =~ /^(bash|sh|zsh|dash|ksh)$/ || $name eq "source" || $name eq ".";
330
+ $run->($loop) unless $shell;
331
+ if (exists $fn{$w[0]} && !$nofn) {
332
+ if (++$calls > 200) { push @out, "opaque"; return $loop; }
333
+ unless ($busy{$w[0]}) { $busy{$w[0]} = 1; parse_tokens(bind_args($fn{$w[0]}, @w[1 .. $#w]), $in); delete $busy{$w[0]}; }
334
+ return $loop unless $w[0] eq "git";
335
+ }
336
+ if ($shell) {
337
+ my ($c, $s, $str, $file) = (0, 0);
338
+ if ($name eq "source" || $name eq ".") { $file = $w[1]; }
339
+ else {
340
+ for (my $j = 1; $j < @w; $j++) {
341
+ my $x = $w[$j];
342
+ if ($x eq "--") { if ($c) { $str = $w[$j + 1]; } elsif (!$s) { $file = $w[$j + 1]; } last; }
343
+ if ($x =~ /^[-+][oO]$/ || $x =~ /^--(rcfile|init-file)$/) { $j++; next; }
344
+ if ($x =~ /^[-+][A-Za-z]*$/ || $x =~ /^--/) { $c = 1 if $x =~ /^-[A-Za-z]*c/; $s = 1 if $x =~ /^-[A-Za-z]*s/; next; }
345
+ if ($c) { $str = $x; } elsif (!$s) { $file = $x; }
346
+ last;
347
+ }
348
+ }
349
+ $file = undef if defined $file && $file =~ m{^(/dev/stdin|/dev/fd/0|-)$};
350
+ if (defined $str) { parse($str, $in); }
351
+ elsif (!defined $file) { if (@$docs) { parse($_->{body}, $in) for @$docs; } elsif (!$input) { push @out, "opaque"; } }
352
+ return $loop;
353
+ }
354
+ if ($name eq "eval") { parse(join(" ", @w[1 .. $#w]), $in) if @w > 1; return $loop; }
355
+ if ($name eq "find") {
356
+ for (my $j = 1; $j < @w; $j++) {
357
+ next unless $w[$j] =~ /^-(exec|execdir|ok|okdir)$/;
358
+ my @e; $j++;
359
+ push @e, $w[$j++] while $j < @w && $w[$j] ne ";" && $w[$j] ne "+";
360
+ words(\@e, $loop, 1, []);
361
+ }
362
+ return $loop;
363
+ }
364
+ if ($name eq "gh") {
365
+ my $g = 1; $g += ($w[$g] =~ /^(-R|--repo)$/ ? 2 : 1) while $g < @w && $w[$g] =~ /^-/;
366
+ push @out, "switch" if $g + 1 < @w && $w[$g] eq "pr" && $w[$g + 1] eq "checkout";
367
+ return $loop;
368
+ }
369
+ return $loop unless $name eq "git";
370
+ my $j = 1;
371
+ while ($j < @w && $w[$j] =~ /^-/) { my $o = $w[$j++]; $j++ if $o =~ /^(-c|-C|--git-dir|--work-tree|--namespace|--exec-path|--config-env|--attr-source)$/; }
372
+ return $loop unless $j < @w;
373
+ my $sub = $w[$j];
374
+ if ($sub eq "checkout" || $sub eq "switch") {
375
+ my $letters = $sub eq "checkout" ? "bB" : "cC";
376
+ my @a = @w[$j + 1 .. $#w];
377
+ my ($new, $orphan, $paths, @pos) = (0, 0, 0);
378
+ for (my $k = 0; $k < @a; $k++) {
379
+ my $x = $a[$k];
380
+ if ($x eq "--") { $paths = 1 if $k + 1 < @a; last; }
381
+ if ($x eq "-") { push @pos, $x; next; }
382
+ if ($sub eq "checkout" && $x =~ /^--(patch|ours|theirs|pathspec-from-file)(=.*)?$/) {
383
+ $paths = 1; $k++ if $1 eq "pathspec-from-file" && !defined $2; next;
384
+ }
385
+ if ($x =~ /^--(orphan|create|force-create)(=.*)?$/) {
386
+ next if $sub eq "checkout" && $1 ne "orphan";
387
+ $new = 1; $orphan = 1 if $1 eq "orphan"; $k++ unless defined $2; next;
388
+ }
389
+ if ($x eq "--conflict") { $k++; next; }
390
+ if ($x =~ /^-([A-Za-z].*)$/s && $x !~ /^--/) {
391
+ my $c = $1;
392
+ $paths = 1 if $sub eq "checkout" && $c =~ /^[^$letters]*p/;
393
+ if ($c =~ /^[A-Za-z]*?[$letters](.*)$/s) { $new = 1; $k++ unless length $1; }
394
+ next;
395
+ }
396
+ next if $x =~ /^-/;
397
+ push @pos, $x;
398
+ }
399
+ # A start point is the first word after the new name; `.` and a second word are paths, and
400
+ # git refuses the whole call then. Without a new name, checkout reads the words after the
401
+ # first one as paths: the files are restored from it, and the branch stays.
402
+ my $tip = qr/^(?:HEAD|head|\@|\@\{HEAD\}|HEAD\@\{0\}|\@\{0\}|\.)$/;
403
+ if ($new) {
404
+ push @out, "switch" if (@pos == 1 && $pos[0] !~ $tip) || ($orphan && $sub eq "switch");
405
+ }
406
+ elsif (!$paths && @pos && $pos[0] ne "." && !($sub eq "checkout" && @pos > 1)) { push @out, "switch"; }
407
+ return $loop;
408
+ }
409
+ return $loop unless $sub eq "push";
410
+ my ($dry, $several, $delete, $repo, @pos) = (0, 0, 0, 0);
411
+ my @a = @w[$j + 1 .. $#w];
412
+ for (my $k = 0; $k < @a; $k++) {
413
+ my $x = $a[$k];
414
+ if ($x eq "--dry-run") { $dry = 1; next; }
415
+ if ($x =~ /^--(all|mirror|branches)$/) { $several = 1; next; }
416
+ if ($x eq "--delete") { $delete = 1; next; }
417
+ if ($x eq "--repo") { $repo = 1; $k++; next; }
418
+ if ($x =~ /^--repo=/) { $repo = 1; next; }
419
+ if ($x =~ /^(--push-option|--receive-pack|--exec)$/) { $k++; next; }
420
+ if ($x =~ /^-[A-Za-z]+$/) { $dry = 1 if $x =~ /n/; $delete = 1 if $x =~ /d/; $k++ if $x =~ /o$/; next; }
421
+ next if $x =~ /^-/;
422
+ push @pos, $x;
423
+ }
424
+ shift @pos unless $repo;
425
+ my @refs = $delete ? () : grep { !/^\+?:/ } @pos;
426
+ $several = 1 if @refs > 1 || grep { /\*/ || /\{[^}]*(,|\.\.)[^}]*\}/ } @refs;
427
+ push @out, join(" ", ($in > 0 ? "loop" : "flat"), ($several ? "several" : "one"), ($dry ? "dry" : "real"));
428
+ return $loop;
429
+ }
430
+ parse($text, 0);
431
+ print "$_\n" for @out;
432
+ ' 2>/dev/null
433
+ }
@@ -28,6 +28,7 @@
28
28
  # Its own name in the observations: the refusal is written by the shared deny tail, not by the
29
29
  # guard itself.
30
30
  RT_GUARD_NAME=qa-dataid-guard
31
+ RT_GUARD_RULE=component-structure
31
32
 
32
33
  . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/utf8.sh" 2>/dev/null || true
33
34
  . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/hook-input.sh" 2>/dev/null || true
@@ -23,6 +23,7 @@
23
23
  # Its own name in the observations: the refusal is written by the shared deny tail, not by the
24
24
  # guard itself.
25
25
  RT_GUARD_NAME=rerun-guard
26
+ RT_GUARD_RULE=testing
26
27
 
27
28
  . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/utf8.sh" 2>/dev/null || true
28
29
  . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/hook-input.sh" 2>/dev/null || true
@@ -51,11 +52,19 @@ for profile in "$rt_hooks_dir/../rt-kit/defaults/project.sh" "$rt_hooks_dir/../d
51
52
  [ -f "$profile" ] && . "$profile" 2>/dev/null
52
53
  done
53
54
 
54
- # The rerun call: the rerun word as a separate word next to the word for a run.
55
+ # The rerun call: the rerun word as a separate word next to the word for a run. The GitLab client
56
+ # retries a job instead — `glab ci retry <id>` — and reads its log by `glab ci trace <id>`; a guard
57
+ # waiting only for `run rerun` let such a retry through unread.
55
58
  host_cli="${RT_HOST_CLI:-gh}"
56
- printf '%s' "$cmd" | grep -qE "${RT_CMD_BOUND}${host_cli}([[:space:]]|\$)" || exit 0
57
- printf '%s' "$cmd" | grep -qE '(^|[[:space:]])(run|workflow)([[:space:]]|$)' || exit 0
58
- printf '%s' "$cmd" | grep -qE '(^|[[:space:]])rerun([[:space:]]|$)' || exit 0
59
+ retry_re='(^|[[:space:]])ci[[:space:]]+retry([[:space:]]|$)'
60
+ printf '%s' "$cmd" | grep -qE "${RT_CMD_BOUND}(${host_cli}|glab)([[:space:]]|\$)" || exit 0
61
+ if printf '%s' "$cmd" | grep -qE "$retry_re"; then
62
+ log_cmd="glab ci trace"
63
+ else
64
+ printf '%s' "$cmd" | grep -qE '(^|[[:space:]])(run|workflow)([[:space:]]|$)' || exit 0
65
+ printf '%s' "$cmd" | grep -qE '(^|[[:space:]])rerun([[:space:]]|$)' || exit 0
66
+ log_cmd="${host_cli} run view"
67
+ fi
59
68
 
60
69
  # The run number. Not named — the guard stays silent: there is nothing to judge.
61
70
  run_id="$(printf '%s' "$cmd" | grep -oE '(^|[[:space:]])[0-9]{6,}([[:space:]]|$)' | tr -d ' ' | head -1)"
@@ -70,13 +79,13 @@ transcript="$(printf '%s' "$input" | jq -r '.transcript_path // empty' 2>/dev/nu
70
79
  seen="$(jq -s -r --arg id "$run_id" '
71
80
  [.[] | select(.type == "assistant") | (.message.content // [])[] | select(.type == "tool_use")
72
81
  | ((.input.command // "") | tostring)] as $used
73
- | (($used | join("\n")) | test("run[[:space:]]+view[^\\n]*" + $id + "|" + $id + "[^\\n]*--log")) as $read
82
+ | (($used | join("\n")) | test("run[[:space:]]+view[^\\n]*" + $id + "|" + $id + "[^\\n]*--log|ci[[:space:]]+(trace|view)[^\\n]*" + $id)) as $read
74
83
  | if $read then "read" else "not-read" end
75
84
  ' "$transcript" 2>/dev/null)"
76
85
 
77
86
  [ "$seen" = "read" ] && exit 0
78
87
 
79
- reason="BLOCKED by rerun-guard: a rerun of the run ${run_id} without its output read. Red on a run comes in two kinds, and in the list they look the same: a hosting refusal on the preparation step is cured by a rerun, a defect of the branch is not cured by it at all, and the circle repeats until the output is opened. Read the output of this run — ${host_cli} run view ${run_id} --log-failed — and repeat the call. The guard judges the order, not the cause of the fall: what is written in the output you judge yourself."
88
+ reason="BLOCKED by rerun-guard: a rerun of the run ${run_id} without its output read. Red on a run comes in two kinds, and in the list they look the same: a hosting refusal on the preparation step is cured by a rerun, a defect of the branch is not cured by it at all, and the circle repeats until the output is opened. Read the output of this run — ${log_cmd} ${run_id}$([ "$log_cmd" = "glab ci trace" ] || printf ' --log-failed') — and repeat the call. The guard judges the order, not the cause of the fall: what is written in the output you judge yourself."
80
89
 
81
90
  # The shared deny tail: the two lawful moves and the lawful form of bypass, if the refusal has one.
82
91
  # The file may not be laid out — then there is no tail, and the refusal reason stays as it is.
@@ -39,6 +39,7 @@
39
39
  # Its own name in the observations: the refusal is written by the shared deny tail, not by the
40
40
  # guard itself.
41
41
  RT_GUARD_NAME=reuse-first-guard
42
+ RT_GUARD_RULE=reuse-first
42
43
 
43
44
  . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/utf8.sh" 2>/dev/null || true
44
45
  . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/hook-input.sh" 2>/dev/null || true