@rt-tools/agent-kit 0.27.0 → 0.29.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 (112) hide show
  1. package/assets/checks/board-epic-link.github.mjs +13 -1
  2. package/assets/checks/board-epic-plan.github.mjs +86 -0
  3. package/assets/checks/board-epics.github.mjs +12 -67
  4. package/assets/checks/board-pull-state.github.mjs +6 -11
  5. package/assets/checks/board-runs.github.mjs +102 -2
  6. package/assets/checks/check-board.github.mjs +63 -3
  7. package/assets/checks/check-glossary.mjs +10 -2
  8. package/assets/checks/check-lib-layers.mjs +24 -1
  9. package/assets/checks/check-reuse.mjs +26 -2
  10. package/assets/checks/check-specs.mjs +8 -1
  11. package/assets/checks/lib-boundaries.mjs +2 -2
  12. package/assets/checks/lib-common.mjs +46 -0
  13. package/assets/checks/lib-manifests.mjs +18 -13
  14. package/assets/checks/main-run.github.mjs +69 -0
  15. package/assets/checks/rt-kit-checks.config.mjs +21 -0
  16. package/assets/checks/signals.mjs +39 -5
  17. package/assets/checks/spec-anchors.mjs +48 -4
  18. package/assets/checks/spec-common.mjs +40 -6
  19. package/assets/checks/spec-contract.mjs +33 -7
  20. package/assets/checks/spec-scenarios.mjs +24 -1
  21. package/assets/checks/task-new.github.mjs +9 -3
  22. package/assets/checks/tree-assignment.mjs +169 -0
  23. package/assets/defaults/gate-map.sh +20 -5
  24. package/assets/defaults/project.sh +6 -0
  25. package/assets/hooks/dev-server-guard.sh +61 -32
  26. package/assets/hooks/end-conversation-guard.sh +59 -0
  27. package/assets/hooks/epic-over.sh +11 -2
  28. package/assets/hooks/exam-guard.sh +10 -10
  29. package/assets/hooks/git-guard-delivery-conflict.sh +53 -1
  30. package/assets/hooks/git-guard-delivery-draft.sh +7 -6
  31. package/assets/hooks/git-guard-delivery-epic.sh +7 -0
  32. package/assets/hooks/git-guard-delivery-tree.sh +50 -0
  33. package/assets/hooks/git-guard-delivery.sh +45 -24
  34. package/assets/hooks/git-guard-discard.sh +144 -0
  35. package/assets/hooks/git-guard-push-tests.sh +36 -16
  36. package/assets/hooks/git-guard-tree-assignment.sh +77 -0
  37. package/assets/hooks/hook-input.sh +8 -1
  38. package/assets/hooks/main-run-context.sh +45 -0
  39. package/assets/hooks/reuse-first-guard.sh +29 -4
  40. package/assets/hooks/rule-source-guard.sh +17 -2
  41. package/assets/hooks/skill-gate.sh +7 -0
  42. package/assets/hooks/sql-guard-write.sh +18 -2
  43. package/assets/hooks/tree-assignment-guard.sh +101 -0
  44. package/assets/hooks/turn-exit-epic.sh +110 -0
  45. package/assets/hooks/turn-exit-guard.sh +80 -82
  46. package/assets/hooks/turn-exit-patterns.sh +18 -10
  47. package/assets/hooks/turn-exit-verdict.sh +96 -0
  48. package/assets/laws/delivery.md +20 -6
  49. package/assets/laws/verifiability.md +22 -4
  50. package/assets/laws/work-conduct.md +4 -0
  51. package/assets/patterns/git-workflow-merge.md +13 -0
  52. package/assets/patterns/git-workflow-pr-body.github.md +156 -0
  53. package/assets/patterns/git-workflow-pr.github.md +42 -119
  54. package/assets/patterns/git-workflow-stack.md +38 -2
  55. package/assets/patterns/task-flow-close.md +66 -79
  56. package/assets/patterns/task-flow-resume.md +8 -0
  57. package/assets/patterns/ts-procedure.md +6 -0
  58. package/assets/pitfalls/git-workflow.github.md +44 -0
  59. package/assets/pitfalls/task-flow.md +25 -23
  60. package/assets/pitfalls/turn-conduct.md +18 -0
  61. package/assets/rules/browser-verification.md +8 -0
  62. package/assets/rules/deploy-flow.github.md +6 -0
  63. package/assets/rules/doc-style.md +9 -2
  64. package/assets/rules/git-workflow.azure.md +10 -0
  65. package/assets/rules/git-workflow.github.md +128 -130
  66. package/assets/rules/git-workflow.gitlab.md +10 -0
  67. package/assets/rules/lib-layers.md +11 -0
  68. package/assets/rules/spec-driven.md +10 -1
  69. package/assets/rules/task-flow.md +82 -79
  70. package/assets/rules/testing.md +7 -0
  71. package/assets/rules/turn-conduct.md +47 -28
  72. package/assets/samples/tasks/_template/plan.md +4 -1
  73. package/assets/samples/tasks/_template/progress.md +1 -1
  74. package/assets/skills/agent-kit.md +17 -4
  75. package/bin/agent-kit.d.ts.map +1 -1
  76. package/bin/agent-kit.js +3 -2
  77. package/bin/agent-kit.js.map +1 -1
  78. package/lib/cargo.d.ts +49 -1
  79. package/lib/cargo.d.ts.map +1 -1
  80. package/lib/cargo.js +9 -2
  81. package/lib/cargo.js.map +1 -1
  82. package/lib/commands.d.ts +1 -1
  83. package/lib/commands.d.ts.map +1 -1
  84. package/lib/commands.js +59 -2
  85. package/lib/commands.js.map +1 -1
  86. package/lib/hooks-map.d.ts +5 -0
  87. package/lib/hooks-map.d.ts.map +1 -1
  88. package/lib/hooks-map.js +9 -3
  89. package/lib/hooks-map.js.map +1 -1
  90. package/lib/observations-cargo.d.ts +20 -0
  91. package/lib/observations-cargo.d.ts.map +1 -0
  92. package/lib/observations-cargo.js +88 -0
  93. package/lib/observations-cargo.js.map +1 -0
  94. package/lib/plan.d.ts +2 -0
  95. package/lib/plan.d.ts.map +1 -1
  96. package/lib/plan.js +8 -0
  97. package/lib/plan.js.map +1 -1
  98. package/lib/proposals.d.ts +25 -0
  99. package/lib/proposals.d.ts.map +1 -1
  100. package/lib/proposals.js +43 -1
  101. package/lib/proposals.js.map +1 -1
  102. package/lib/shipment.d.ts +8 -20
  103. package/lib/shipment.d.ts.map +1 -1
  104. package/lib/shipment.js +30 -50
  105. package/lib/shipment.js.map +1 -1
  106. package/lib/tree-mark.d.ts +17 -0
  107. package/lib/tree-mark.d.ts.map +1 -0
  108. package/lib/tree-mark.js +41 -0
  109. package/lib/tree-mark.js.map +1 -0
  110. package/package.json +1 -1
  111. package/rt-tools-agent-kit-0.29.0.tgz +0 -0
  112. package/rt-tools-agent-kit-0.27.0.tgz +0 -0
@@ -49,6 +49,16 @@ PIPE_EOF
49
49
  # The exception acts PER SEGMENT. While it was checked over the whole command, one mention of
50
50
  # `pg_dump` anywhere in the chain was enough for `-f` to stop counting as a write in all the
51
51
  # other calls: `pg_dump … > /dev/null && psql -d app -f /tmp/x.sql` passed silently.
52
+ sql_delivers_file && is_write="yes"
53
+ }
54
+
55
+ # A file poured into the client names not a single SQL verb, and its content the guard does not see
56
+ # — so this is a write by definition. It stands in a function of its own because the answer is
57
+ # needed by two places: the common detection of a write above, and the early exit on a local
58
+ # migration below. While the loop stood in the first alone, the chain `prisma migrate deploy &&
59
+ # psql -d app -f fix.sql` passed silently: the exit counted a second write only by SQL verbs and by
60
+ # the commands that feed the pipe.
61
+ sql_delivers_file() {
52
62
  while IFS= read -r seg; do
53
63
  [ -z "$seg" ] && continue
54
64
  # The word boundary is mandatory: `-f /tmp/pg_dump-restore.sql` is loading a dump, not
@@ -62,12 +72,13 @@ PIPE_EOF
62
72
  seg_tail="$(printf '%s' "$seg" | perl -0pe 's{^.*?(?<![[:alnum:]_./-])(psql|pg_restore|prisma)(?=\s|$)}{$1}s' 2>/dev/null)"
63
73
  [ -z "$seg_tail" ] && seg_tail="$seg"
64
74
  if printf '%s' "$seg_tail" | grep -qE '(^|[[:space:]])(-f|--file)([[:space:]]|=)|<[[:space:]]*[^[:space:]|<]+\.(sql|dump)'; then
65
- is_write="yes"
66
- break
75
+ return 0
67
76
  fi
68
77
  done <<EOF
69
78
  $segments
70
79
  EOF
80
+
81
+ return 1
71
82
  }
72
83
 
73
84
  # --- destructive for certain ------------------------------------------------------------
@@ -178,6 +189,11 @@ sql_check_migrations() {
178
189
  done <<MIGRATE_PIPE_EOF
179
190
  $(client_segments)
180
191
  MIGRATE_PIPE_EOF
192
+ # A file poured into the client is judged by the same sign as everywhere else. Its
193
+ # own set of signs here was narrower than the common one by exactly this, and the
194
+ # second link of `prisma migrate deploy && psql -d app -f fix.sql` fell out of the
195
+ # check whole: the guard sees neither the file nor its content.
196
+ sql_delivers_file && other_write="yes"
181
197
  [ -z "$other_write" ] && exit 0
182
198
  ;;
183
199
  '')
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env bash
2
+ # rt-hook: PreToolUse Bash|mcp__webstorm__execute_terminal_command|mcp__webstorm__execute_tool
3
+ # Requires: hooks/deny-tail.sh, checks/tree-assignment.mjs
4
+ # Guard of the assignment of an epic to a working copy. PreToolUse on a call that takes new work
5
+ # past the branch: creating a task under an epic and moving a card into the working column.
6
+ #
7
+ # The branch is judged by the delivery guard — there the epic of the task comes from the queue, and
8
+ # the base is judged along with it. But work is taken by three calls, not one, and the other two go
9
+ # past that guard entirely: a task created under someone else's epic and a card moved into work are
10
+ # both work taken, and both were silent.
11
+ #
12
+ # What this cost. A session found the freshest epic of the open list, decided it was its own and
13
+ # worked the whole day on it: a branch, a task folder, a plan, a written agreement — the owner
14
+ # stopped that, not a check, and said plainly they had never given that epic into work.
15
+ #
16
+ # FAIL-OPEN: no `jq`, no `node`, no reading command, an unreadable table — the call is ALLOWED. A
17
+ # guard that jams the work when a file is missing is switched off on the first day, and the price of
18
+ # a miss here is one turn of the owner's attention.
19
+
20
+ # Its own name in the observations: the refusal is written by the shared deny tail, not by the
21
+ # guard itself.
22
+ RT_GUARD_NAME=tree-assignment-guard
23
+
24
+ . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/utf8.sh" 2>/dev/null || true
25
+ . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/hook-input.sh" 2>/dev/null || true
26
+
27
+ rt_hook_read
28
+ input="$RT_HOOK_INPUT"
29
+ [ -z "$input" ] && exit 0
30
+
31
+ command -v jq >/dev/null 2>&1 || exit 0
32
+ command -v node >/dev/null 2>&1 || exit 0
33
+
34
+ tool="$(rt_hook_tool)"
35
+ case "$tool" in
36
+ Bash | mcp__webstorm__execute_terminal_command | mcp__webstorm__execute_tool) ;;
37
+ *) exit 0 ;;
38
+ esac
39
+
40
+ cmd="$(rt_hook_cmd)"
41
+ [ -z "$cmd" ] && exit 0
42
+
43
+ # The universal executor of the environment passes the real command as a nested string: it is the
44
+ # one to judge, not the wrapper.
45
+ if [ "$tool" = "mcp__webstorm__execute_tool" ] && command -v perl >/dev/null 2>&1; then
46
+ inner="$(printf '%s' "$cmd" | perl -0ne '
47
+ if (/--command(?:=|\s+)(?:"((?:[^"\\]|\\.)*)"|\x27([^\x27]*)\x27|(.+))/s) {
48
+ print defined $1 ? $1 : (defined $2 ? $2 : $3);
49
+ }
50
+ ' 2>/dev/null)"
51
+ [ -n "$inner" ] && cmd="$inner"
52
+ fi
53
+
54
+ root="$(git rev-parse --show-toplevel 2>/dev/null)"
55
+ [ -n "$root" ] || exit 0
56
+ cd "$root" 2>/dev/null || exit 0
57
+
58
+ checks="$(jq -r '.layout.checks // "tools"' ".claude/rt-kit.json" 2>/dev/null)"
59
+ [ -n "$checks" ] && [ "$checks" != null ] || checks=tools
60
+ reader="${checks}/tree-assignment.mjs"
61
+ [ -f "$reader" ] || exit 0
62
+
63
+ # What is judged, and what the epic of the work is taken from. Creating a task names its epic in the
64
+ # call itself; a card moved into work does not name it at all, and for that the table alone answers:
65
+ # a copy with no assignment takes no work whatever epic it belongs to.
66
+ takes_work() {
67
+ printf '%s' "$cmd" | grep -qE '(task:new|task-new\.mjs)' 2>/dev/null && return 0
68
+ printf '%s' "$cmd" \
69
+ | grep -qE '(task:move|board\.mjs[[:space:]]+move)([^|;&]*)in-progress' 2>/dev/null
70
+ }
71
+
72
+ # Creating an epic is not taking work: it is the owner's order written down, and the assignment for
73
+ # it is given after, by the owner, in the table.
74
+ creates_epic() {
75
+ printf '%s' "$cmd" \
76
+ | grep -qE '(task:new|task-new\.mjs)[^|;&]*--epic([[:space:]]|$)' 2>/dev/null
77
+ }
78
+
79
+ takes_work || exit 0
80
+ creates_epic && exit 0
81
+
82
+ epic="$(printf '%s' "$cmd" | sed -nE 's/.*--epic-of[[:space:]]+([0-9]+).*/\1/p' | head -1)"
83
+
84
+ said="$(node "$reader" --fault "$epic" 2>/dev/null)"
85
+ [ -n "$said" ] || exit 0
86
+
87
+ reason="Refused: this call takes work, and ${said}."
88
+
89
+ # shellcheck disable=SC1090
90
+ [ -f "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/deny-tail.sh" ] \
91
+ && . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/deny-tail.sh" 2>/dev/null
92
+ command -v rt_deny_tail >/dev/null 2>&1 || rt_deny_tail() { :; }
93
+ deny_tail_text="$(rt_deny_tail "")"
94
+ [ -n "$deny_tail_text" ] && reason="${reason}
95
+
96
+ ${deny_tail_text}"
97
+
98
+ jq -n --arg r "$reason" '{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$r}}' 2>/dev/null \
99
+ || printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"This copy has no assignment: work is not taken by guesswork."}}\n'
100
+
101
+ exit 0
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env bash
2
+ # The tiers of the open epic for the turn-exit guard. NOT a guard: it has no `rt-hook:` declaration
3
+ # and hooks into no agent event. The guard sources it right after the root of the tree is known.
4
+ #
5
+ # Why they live apart. Under an open epic a turn does not end by itself: the owner said it outright
6
+ # — until the epic is closed the executor does not stop on its own. Two ends the guard used to
7
+ # release are stops all the same: a turn that ended with work, and a second pass over the same turn.
8
+ # The work stands where it stood, and the owner sees the executor standing still with the epic open.
9
+ # The reading of the epic and the refusals about it lie here because the guard reached the file
10
+ # length limit.
11
+ #
12
+ # The state of the epic is asked by the same command that prints the table for the owner, and only
13
+ # on the way to a refusal: the call goes to the hosting and costs seconds. It is asked once per turn.
14
+ #
15
+ # FAIL-OPEN: no epic behind the branch, no table, no way to ask the hosting — the epic reads as not
16
+ # open, and the guard works as it did before these tiers existed.
17
+
18
+ # The unfinished tasks of the epic in one line; empty when the epic is over or cannot be read.
19
+ rt_te_epic_left() {
20
+ if [ -z "${rt_te_epic_asked:-}" ]; then
21
+ rt_te_epic_asked=1
22
+ rt_te_epic_left_cache=""
23
+ rt_te_epic_read=""
24
+ # shellcheck disable=SC1090
25
+ if . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/epic-over.sh" 2>/dev/null \
26
+ && command -v rt_epic_unfinished >/dev/null 2>&1; then
27
+ rt_te_epic_raw="$(rt_epic_unfinished 2>/dev/null)" && rt_te_epic_read=1
28
+ rt_te_epic_left_cache="$(printf '%s' "$rt_te_epic_raw" | tr -s '[:space:]' ' ' | sed 's/^ //; s/ $//')"
29
+ fi
30
+ fi
31
+ printf '%s' "$rt_te_epic_left_cache"
32
+ }
33
+
34
+ # The standing word of the owner about a stop, quoted in « » in the line «Waiting for the owner» of
35
+ # the progress. The guard reads their word, not a retelling: a line without a quote releases
36
+ # nothing, and the quote holds until the owner rewrites the line. Expects `$progress` to be set.
37
+ rt_te_owner_word_quoted() {
38
+ [ -n "${progress:-}" ] || return 1
39
+ local waiting
40
+ # The first waiting line of the progress is read, in either language of the heading.
41
+ waiting="$(sed -nE 's/^[[:space:]]*[-*][[:space:]]*\*\*(Waiting for the owner|Ждём владельца|Ждём от владельца):\*\*[[:space:]]*(.*)/\2/p' "$progress" 2>/dev/null | head -1)"
42
+ # Three characters and more inside « » count as a word.
43
+ printf '%s' "$waiting" | grep -q '«[^»]\{3,\}»' 2>/dev/null
44
+ }
45
+
46
+ # The epic is open: at least one of its tasks is left unfinished.
47
+ rt_te_epic_open() {
48
+ [ -n "$(rt_te_epic_left)" ]
49
+ }
50
+
51
+ # The epic is over: the table was read and printed not a single task. At that point the stop is
52
+ # lawful — waiting for the word of the owner is the work itself — and the refusal releases the turn
53
+ # whatever tier reached it. An epic that cannot be read — no table, the main branch, no way to ask
54
+ # the hosting — is not over, and the turn is judged as before. Reads the same cache: one call per turn.
55
+ rt_te_epic_over() {
56
+ rt_te_epic_left >/dev/null
57
+ [ -n "${rt_te_epic_read:-}" ] && [ -z "${rt_te_epic_left_cache:-}" ]
58
+ }
59
+
60
+ # The epic branch: named by the header line of an epic plan in the plans directory of the tree. It
61
+ # has the shape of a task branch and by the rule carries no task folder, so the tier of the taken
62
+ # task would read every turn on it as work taken and not begun. The plan is written by the command
63
+ # that creates the epic and outlives the merge; the directory is named by the settings, `docs/plans`
64
+ # by default. Expects `$root` and `$branch` to be set. FAIL-OPEN: no directory, no plan — not an
65
+ # epic branch.
66
+ rt_te_epic_branch() {
67
+ [ -n "${root:-}" ] && [ -n "${branch:-}" ] || return 1
68
+ local plans
69
+ plans="$(jq -r ".plansDir // empty" "$root/.claude/rt-kit/checks.json" 2>/dev/null)"
70
+ [ -n "$plans" ] || plans="docs/plans"
71
+ [ -d "$root/$plans" ] || return 1
72
+ grep -lE "^\\*\\*[^|]*\`$branch\`" "$root/$plans"/*.md 2>/dev/null | grep -q .
73
+ }
74
+
75
+ # The refusal of a stop under an open epic. The kind names what the turn ended with.
76
+ rt_te_epic_deny() {
77
+ local kind="$1" ended short
78
+ case "$kind" in
79
+ handed-by-hand)
80
+ ended="the turn ended with a handover written by the hand of the executor, and no window guard refused in it"
81
+ short="a handover by hand under an open epic."
82
+ ;;
83
+ *)
84
+ ended="the turn ended with work, and the work stands where it stood"
85
+ short="the turn ended with work under an open epic."
86
+ ;;
87
+ esac
88
+ rt_te_deny "BLOCKED by turn-exit-guard: the epic is open — its unfinished tasks: $(rt_te_epic_left) — and ${ended}.
89
+
90
+ Until the epic is closed the executor does not stop on its own: the owner said so outright. A turn ends in four ways and no others: a question to the owner through the question tool, when the work does not go without the answer; a refusal of a guard as the last action of the turn; the session handover written by the hook on window fill, or the refusal of the window guard; the word of the owner about stopping, said in this turn. The end of the epic is the fifth: not one task of it is left.
91
+
92
+ The next step is written in the progress: ${next_step}
93
+
94
+ Do it in this same turn. The work of this task is done — take the next task of the epic. The owner said to stop — then write so: the guard reads their word, not a retelling.
95
+
96
+ While the epic is open the guard judges every pass over the turn, not the first one alone." "$short"
97
+ }
98
+
99
+ # A question to the owner at the head of an empty turn in a running stage.
100
+ rt_te_question_without_work_deny() {
101
+ rt_te_deny "BLOCKED by turn-exit-guard: the work stands in the state 'этап-идёт', and the turn asked the owner without doing a single piece of the stage — neither an edit nor a command changing the tree.
102
+
103
+ Waiting for one part of a stage is never a stop of the stage: the parts that do not depend on the answer are done in the same turn, and the question goes after them. A question at the head of an empty turn is a stop with a question attached.
104
+
105
+ The next step is written in the progress: ${next_step}
106
+
107
+ Do what does not depend on the answer in this same turn, then ask by the tool.
108
+
109
+ The guard judges one turn: the next session is not refused." "a question without work in a running stage."
110
+ }
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env bash
2
2
  # rt-hook: Stop
3
- # Requires: hooks/deny-tail.sh, hooks/epic-over.sh, hooks/turn-exit-patterns.sh
3
+ # Requires: hooks/deny-tail.sh, hooks/epic-over.sh, hooks/turn-exit-patterns.sh, hooks/turn-exit-epic.sh
4
4
  # Turn exit guard: a turn in which nothing was done on the work does not end until the work is
5
5
  # handed over. Stop.
6
6
  #
@@ -25,6 +25,9 @@
25
25
  # 5. The session handover is written — the window has run out.
26
26
  # 6. The owner said to stop.
27
27
  #
28
+ # Under an open epic the second and the fifth do not release, and a second pass is judged again:
29
+ # until the epic is closed the executor does not stop on its own. The tiers lie in `turn-exit-epic.sh`.
30
+ #
28
31
  # Work without a branch and without a task folder is judged by the second sign. It has no state,
29
32
  # and there is nowhere to take the first sign from — but a turn without a single edit of the tree
30
33
  # does not end here either: the owner's request "lay it out", "update it", "take a look" lives
@@ -48,6 +51,8 @@ rt_hook_read
48
51
  # fallback line in case there is no parser. These twelve lines used to stand as eight copies — the
49
52
  # file grew faster on every new tier than on the requirement itself.
50
53
  rt_te_deny() {
54
+ # The end of an epic releases the turn whatever tier came here: the epic is read only on this road.
55
+ command -v rt_te_epic_over >/dev/null 2>&1 && rt_te_epic_over && exit 0
51
56
  rt_te_reason="$1"
52
57
  rt_te_short="$2"
53
58
  # shellcheck disable=SC1090
@@ -68,9 +73,7 @@ input="$RT_HOOK_INPUT"
68
73
  [ -z "$input" ] && exit 0
69
74
  command -v jq >/dev/null 2>&1 || exit 0
70
75
 
71
- # A second pass over the same turn is not judged: the guard has said its piece once and lets go.
72
76
  active="$(printf '%s' "$input" | jq -r '.stop_hook_active // false' 2>/dev/null)"
73
- [ "$active" = "true" ] && exit 0
74
77
 
75
78
  transcript="$(printf '%s' "$input" | jq -r '.transcript_path // empty' 2>/dev/null)"
76
79
  [ -z "$transcript" ] && exit 0
@@ -84,6 +87,12 @@ git rev-parse --is-inside-work-tree >/dev/null 2>&1 || exit 0
84
87
  root="$(git rev-parse --show-toplevel 2>/dev/null)"
85
88
  [ -z "$root" ] && exit 0
86
89
 
90
+ # The tiers of the open epic lie in a neighbouring file. A second pass over the same turn is judged
91
+ # only under an open epic: outside it the guard has said its piece once and lets go.
92
+ . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/turn-exit-epic.sh" 2>/dev/null || exit 0
93
+ command -v rt_te_epic_open >/dev/null 2>&1 || exit 0
94
+ [ "$active" = "true" ] && ! rt_te_epic_open && exit 0
95
+
87
96
  # The branch, the task folder and the state line are taken while they are there. Empty — the turn
88
97
  # is judged by the second sign, not released: this is where it used to leave with zero, and work by
89
98
  # the owner's word ended with an announcement of intent, silently.
@@ -101,6 +110,7 @@ fi
101
110
  case "$state" in
102
111
  работа-отдана | влито) exit 0 ;;
103
112
  esac
113
+ rt_te_owner_word_quoted && exit 0 # the owner's standing word quoted in the waiting line
104
114
 
105
115
  # A written plan is never the end of a turn at all. The mandatory action of this state is to do the
106
116
  # first stage, and whoever starts it moves the state by the same edit: a turn left in the previous
@@ -162,89 +172,24 @@ next_step=""
162
172
  . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/turn-exit-patterns.sh" 2>/dev/null || exit 0
163
173
  [ -n "${work_re:-}" ] || exit 0
164
174
 
165
- verdict="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r --arg work "$work_re" --arg read "$read_re" --arg part "$part_re" --arg wait "$wait_re" --arg handover "$handover_re" --arg started "$started_re" '
166
- def is_input:
167
- .type == "user"
168
- and ((.isCompactSummary // false) | not)
169
- and (((.message.content // []) | if type == "array"
170
- then ([.[] | select(.type == "tool_result")] | length)
171
- else 0 end) == 0);
172
-
173
- (map(is_input) | rindex(true)) as $i
174
- | (if $i == null then [] else .[$i:] end) as $turn
175
- | [$turn[] | select(.type == "assistant") | (.message.content // [])[] | select(.type == "tool_use")] as $uses
176
- # An edit of a file is work by definition, whatever tool it goes through.
177
- | ($uses | map(.name // "") | any(test("^(Edit|Write|MultiEdit|NotebookEdit)$"))) as $edited
178
- | ($uses | map(.name // "") | any(test("AskUserQuestion"))) as $asked
179
- | ($uses | map((.input.command // "")) | join("\n")) as $ran
180
- # Work is a part of the command that matched the work pattern and did not match the exploration
181
- # pattern: switching a branch and reading history in the same turn do not become work.
182
- | ([$ran | splits($part)] | map(test($work) and (test($read) | not)) | any) as $ran_work
183
- # The last action of the turn. Waiting for a step by anyone else is never the end of a turn,
184
- # however much work there was before: the work stays exactly where it stood.
185
- | ([$uses[] | select((.name // "") == "Bash") | (.input.command // "")] | last // "") as $last
186
- | ($last | test($wait)) as $waited
187
- # Handing the work over: the tail of the turn after the PR was opened. Everything before it was
188
- # done on the task handed in and says nothing about the next one.
189
- | ([$uses[] | select((.name // "") == "Bash") | (.input.command // "")]) as $cmds
190
- | (($cmds | map(test($handover)) | index(true))) as $handover_at
191
- | ($handover_at != null) as $handed_over
192
- | (if $handover_at == null then [] else $cmds[$handover_at:] end) as $tail
193
- | (($tail | map(test($started)) | any)
194
- or ($uses | map(.name // "") | any(test("^(Edit|Write|MultiEdit|NotebookEdit)$")))) as $started_next
195
- # THE LAST ACTION OF THE TURN is the shared sign, and the particular tiers below only derive an
196
- # understandable refusal from it. Nine incident analyses in a day describe nine different stops,
197
- # and in all nine the last action of the turn was a text to the owner: a report, a summary, an
198
- # announcement of intent. A tier for every kind of stop is an endless race: there are as many
199
- # kinds as there are reasons to start talking. Hence one sign — the LAST action must be work.
200
- | ([$uses[] | (.name // "")] | last // "") as $last_tool
201
- | (($last_tool | test("^(Edit|Write|MultiEdit|NotebookEdit)$"))
202
- or ([$last | splits($part)] | map(test($work) and (test($read) | not)) | any)) as $ended_working
203
- # A guard refusal and a session handover — both end the turn by the rule.
204
- | ([$turn[] | select(.type == "user") | .message.content // [] | select(type == "array") | .[]
205
- | select(.type == "tool_result") | .content
206
- | if type == "string" then .
207
- elif type == "array" then (map(if type == "object" then (.text // "") else tostring end) | join("\n"))
208
- else tostring end] | join("\n")) as $out
209
- | (($out | test("BLOCKED by|Refused by the rules gate|Отбито гейтом")) or ($ran | test("BLOCKED by"))) as $denied
210
- | ($ran | test("handoff")) as $handed
211
- # The word of the owner about stopping: the reply itself is judged, not its retelling.
212
- | ([$turn[] | select(.type == "user") | .message.content
213
- | if type == "string" then . elif type == "array"
214
- then (map(if type == "object" then (.text // "") else "" end) | join("\n")) else "" end] | join("\n")) as $said
215
- | ($said | test("останов|стоп|хватит|подожди|не надо|прерв|отложи")) as $told_stop
216
- # A question refused by the conversation guard, and a question appended as prose at the end of
217
- # the reply. The conversation guard judges the call of the question tool and does not see prose
218
- # at all: a refused question came back in the same wording one turn later and passed freely.
219
- | ($out | test("BLOCKED by grill-gate")) as $ask_denied
220
- | ([$turn[] | select(.type == "assistant") | (.message.content // [])[]
221
- | select(.type == "text") | (.text // "")] | last // "") as $last_say
222
- | (($last_say | test("\\?[[:space:]]*$")) and $ask_denied) as $asked_in_prose
223
- # Waiting for the word of the owner, announced by the executor. That word is read from
224
- # the owner: without his word in the turn and without a question to him through the tool, the
225
- # phrase "waiting for your word" is a stop announced by the one it suits. The set of patterns is
226
- # named and closed.
227
- | (($last_say | test("[Жж]ду (твоего|вашего|его|её) (слова|указани|решени|ответа|команды|отмашки)|[Жж]ду слова владельца|[Жж]ду, что скаж|[Оо]стаюсь ждать|[Бб]уду ждать (твоего|вашего)"))
228
- and (($asked or $told_stop or $handed) | not)) as $awaits_word
229
- | { worked: ($edited or $ran_work), released: ($asked or $denied or $handed or $told_stop), waited: $waited, handed_over: $handed_over, started_next: $started_next, ended_working: $ended_working, asked_in_prose: $asked_in_prose, awaits_word: $awaits_word, ran: $ran }
230
- ' 2>/dev/null)"
175
+ # The parsing of the turn record lies in a neighbouring file: the guard crossed the file length
176
+ # limit, and the parsing reads apart from the tiers that apply its answer. Without the file the
177
+ # guard stays silent — fail-open, as on any breakage of its own.
178
+ . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/turn-exit-verdict.sh" 2>/dev/null || exit 0
179
+ command -v rt_te_verdict >/dev/null 2>&1 || exit 0
180
+ rt_te_verdict
231
181
 
232
182
  [ -z "$verdict" ] && exit 0
233
183
 
234
- worked="$(printf '%s' "$verdict" | jq -r '.worked // false' 2>/dev/null)"
235
- waited="$(printf '%s' "$verdict" | jq -r '.waited // false' 2>/dev/null)"
236
- handed_over="$(printf '%s' "$verdict" | jq -r '.handed_over // false' 2>/dev/null)"
237
- started_next="$(printf '%s' "$verdict" | jq -r '.started_next // false' 2>/dev/null)"
238
- ended_working="$(printf '%s' "$verdict" | jq -r '.ended_working // false' 2>/dev/null)"
239
- released="$(printf '%s' "$verdict" | jq -r '.released // false' 2>/dev/null)"
184
+ # Every sign of the verdict becomes a variable of its own name; the commands go apart, they are text.
185
+ eval "$(printf '%s' "$verdict" | jq -r 'del(.ran) | to_entries[] | "\(.key)=\(.value | tostring | @sh)"' 2>/dev/null)"
240
186
  commands="$(printf '%s' "$verdict" | jq -r '.ran // ""' 2>/dev/null)"
241
187
 
242
188
  # A question refused by the conversation guard and asked as prose in the same turn. A guard refusal
243
189
  # releases the turn — it is the lawful end itself — but there is nothing to release here: the same
244
190
  # question came back a line later, and the work stalled on what the tree had already answered. The
245
191
  # tier stands before the lawful exits on purpose: a guard refusal covers exactly this case.
246
- asked_in_prose="$(printf '%s' "$verdict" | jq -r '.asked_in_prose // false' 2>/dev/null)"
247
- if [ "$asked_in_prose" = "true" ]; then
192
+ if [ "${asked_in_prose:-false}" = "true" ]; then
248
193
  rt_te_deny "BLOCKED by turn-exit-guard: in this turn the conversation guard refused a question to the owner, and the reply ends with a question in prose — the same question, asked in another form.
249
194
 
250
195
  The refusal of the guard named the reason: the answer lies in the tree or the owner has already given it. Read the place it named and work on; what is asked is what the tree does not hold.
@@ -260,10 +205,46 @@ fi
260
205
  # named a refusal to the owner, not a turn that said "waiting".
261
206
  # Incident analysis — the record
262
207
  # "2026-09-04-hod-konchalsya-ozhidaniem-pri-deystvuyushchem-ukazanii" in the intake.
263
- awaits_word="$(printf '%s' "$verdict" | jq -r '.awaits_word // false' 2>/dev/null)"
264
- # At the end of an epic waiting for the word of the owner is the work itself: the reading of that
265
- # lies with the patterns and is shared with the guard of the stop.
266
- [ "$awaits_word" = "true" ] && rt_te_epic_over && awaits_word=false
208
+ # A promise to do the work in the next turn. The same announcement of intent as a command named and
209
+ # not run: the tier stands before the lawful exits, because the promise most often stands next to a
210
+ # report about what was done, and by the fullness of the turn it is indistinguishable from a finish.
211
+ if [ "$promised" = "true" ] && [ "$released" != "true" ]; then
212
+ rt_te_deny "BLOCKED by turn-exit-guard: the reply ends with a promise to do the work in the next turn.
213
+
214
+ A promise is the same announcement of intent as a command named and not run, and it only sounds politer. An offer to the owner to object to the announced intent is part of the promise, not a question: a turn ends with a question when the work does not go without the answer.
215
+
216
+ Do the promised in this same turn, and the account of it can come after.
217
+
218
+ The next step is written in the progress: ${next_step}
219
+
220
+ The guard judges one turn: the next session is not refused." "the turn ended with a promise to do the work in the next turn."
221
+ fi
222
+
223
+ # Taking a task as the last action of the turn. Creating the task, the branch and the move of the
224
+ # column is preparation, and the work of the taken task did not begin. Taking it after handed-over
225
+ # work is a lawful end: there the taking answers for the handed-over one.
226
+ if [ "$only_took" = "true" ] && [ "$released" != "true" ]; then
227
+ rt_te_deny "BLOCKED by turn-exit-guard: the last action of the turn is taking a task, and the work of the taken task was not begun.
228
+
229
+ Creating the task, the branch and moving the column is preparation for work, not work. To the owner a report about a taken task is indistinguishable from a stop: they see the executor standing still.
230
+
231
+ Write the plan and begin its first stage in this same turn.
232
+
233
+ The guard judges one turn: the next session is not refused." "the turn ended with taking a task."
234
+ fi
235
+
236
+ # The owner said to work without stops and did not cancel it. Their word holds until they cancel
237
+ # it, and a turn ending with waiting for that same word invents the cancellation. Judged whether or
238
+ # not there was work in the turn: the work stood three turns in a row, and each of them was full.
239
+ if [ "$standing_work" = "true" ] && [ "$awaits_word" = "true" ]; then
240
+ rt_te_deny "BLOCKED by turn-exit-guard: the turn ended with waiting for the word of the owner, and the owner said in this session to work without stops and did not cancel it.
241
+
242
+ Their word holds until they cancel it themselves, and a new fact against it is a line about the price in the reply, not waiting. A decision the tree does not hold is needed — then ask the question by the tool.
243
+
244
+ The next step is written in the progress: ${next_step}
245
+
246
+ The guard judges one turn: the next session is not refused." "the turn ended with waiting under the standing word of the owner to work."
247
+ fi
267
248
 
268
249
  if [ "$awaits_word" = "true" ]; then
269
250
  rt_te_deny "BLOCKED by turn-exit-guard: the turn ended with words about waiting for the word of the owner, and the word about stopping the guard reads from the owner: in this turn they announced no stop, and no question was put to them by the tool.
@@ -275,6 +256,19 @@ The next step is written in the progress: ${next_step}
275
256
  The guard judges one turn: the next session is not refused." "the turn ended with waiting for the word of the owner."
276
257
  fi
277
258
 
259
+ # A question without a single piece of work in a running stage: the parts that do not depend on
260
+ # the answer go first, and the question after them.
261
+ if [ "${asked:-false}" = "true" ] && [ "$worked" != "true" ] && [ "$state" = "этап-идёт" ]; then
262
+ rt_te_question_without_work_deny
263
+ fi
264
+
265
+ # A handover written by the hand of the executor before the window filled is a stop announced by
266
+ # the one it suits: under an open epic it releases nothing. The window guard's refusal releases as
267
+ # any refusal, and the handover on compaction is written by the hook, not by a command in the turn.
268
+ if [ "${handed_by_hand:-false}" = "true" ] && rt_te_epic_open; then
269
+ rt_te_epic_deny handed-by-hand
270
+ fi
271
+
278
272
  [ "$released" = "true" ] && exit 0
279
273
 
280
274
  # Work that has reached the handover and has not been handed over. The two states in the `case`
@@ -324,7 +318,8 @@ esac
324
318
  # A folder taken apart by a branch commit does not get here: `archived` means handed-over work, and
325
319
  # the previous tier judges it. A branch without a task number is not judged at all — such ones are
326
320
  # created for a trial too.
327
- if [ "$archived" != "true" ] && [ -z "$progress" ] && [ -n "$branch" ] && [ ! -d "$root/$tasks_dir/$branch" ]; then
321
+ # An epic branch is not judged either: it carries no folder by the rule, and the epic plan names it.
322
+ if [ "$archived" != "true" ] && [ -z "$progress" ] && [ -n "$branch" ] && [ ! -d "$root/$tasks_dir/$branch" ] && ! rt_te_epic_branch; then
328
323
  task_key="${RT_TASK_KEY:-}"
329
324
  if [ -z "$task_key" ] && [ -f "$root/.claude/rt-kit/checks.json" ]; then
330
325
  task_key="$(jq -r '.board.taskKey // empty' "$root/.claude/rt-kit/checks.json" 2>/dev/null)"
@@ -461,6 +456,9 @@ The guard judges one turn: the next session is not refused."
461
456
  fi
462
457
  fi
463
458
 
459
+ # There was work in the turn, and the epic is open: the turn is released outside an epic and under a
460
+ # closed one alone. Under an open epic the work stands where it stood, and the next step is named.
461
+ [ "$worked" = "true" ] && rt_te_epic_open && rt_te_epic_deny worked
464
462
  [ "$worked" = "true" ] && exit 0
465
463
 
466
464
  if [ "$archived" = "true" ]; then
@@ -45,19 +45,27 @@ part_re='&&|\|\||;|\n'
45
45
  # resolved, a commit and a push were made, and the last action became a loop until the run was ready
46
46
  # passed it whole — there was work, and plenty. It is exactly that fullness that deceives: the
47
47
  # emptiness behind such a turn is not visible.
48
- wait_re='gh[[:space:]]+(run[[:space:]]+watch|pr[[:space:]]+checks[^|]*--watch)|until[[:space:]].*sleep|while[[:space:]].*sleep|^[[:space:]]*sleep[[:space:]]'
48
+ # Two more kinds of last action were added after an analysis. The first is a run of the checks
49
+ # started in the background: the guard is given the background sign appended to the command, and on
50
+ # the foreground the same command stays work — it holds the turn to its own end. The second is a
51
+ # read of a background task's log: the run was started, the log was read, a report was written, and
52
+ # the tree did not change by a single sign; such a turn stands exactly as long as an empty one.
53
+ wait_re='gh[[:space:]]+(run[[:space:]]+watch|pr[[:space:]]+checks[^|]*--watch)|until[[:space:]].*sleep|while[[:space:]].*sleep|^[[:space:]]*sleep[[:space:]]|(nx[[:space:]]+(affected|run-many|test|build|lint|e2e)|npm[[:space:]]+run[[:space:]]+(lint|test|check:|e2e)|pnpm[[:space:]]+run|playwright[[:space:]]+test|vitest|jest|bash[[:space:]].*tests?/)[^&]*&[[:space:]]*$|(cat|tail|head|less|grep)[^|]*\.(log|output)([[:space:]]|$)|tasks/[A-Za-z0-9]+\.output'
54
+
55
+ # A promise to do the work in the next turn. The same announcement of intent as a command named and
56
+ # not run, only it sounds politer and is therefore recognised as a stop less often. An offer to the
57
+ # owner to object to the announced intent is part of the promise, not a question: a turn ends with a
58
+ # question when the work does not go without the answer, and here it did go.
59
+ promise_re='следующим ходом|в следующий раз|дальше возьму|дальше допишу|допишу остальн|доделаю остальн|доделаю в следующ|продолжу в следующ|беру[^.]{0,40}следующим|возьму[^.]{0,40}следующим'
60
+
61
+ # The owner's standing word to work without stops. It holds until they cancel it, and a turn that
62
+ # ended with waiting for their word invents that cancellation. Without this the word about a stop
63
+ # was read out of it by the piece «останов» in «без остановок» — the instruction to work was taken
64
+ # for its opposite.
65
+ standing_work_re='работай[^.]{0,40}(без остановок|без пауз|сам)|не останавливайся|продолжай[^.]{0,40}(без остановок|без пауз)|работай дальше'
49
66
 
50
67
  # Handing work over and starting the next. The rule calls handed-over work a lawful end of a turn —
51
68
  # but on a condition: the next one is started, and an ACTION has been done on it, not spoken.
52
69
  handover_re='gh[[:space:]]+pr[[:space:]]+create'
53
70
  started_re='task:new|task:move|board\.mjs[[:space:]]+move|git[[:space:]]+checkout([[:space:]]+-[A-Za-z-]+)*[[:space:]]+-b|git[[:space:]]+switch([[:space:]]+-[A-Za-z-]+)*[[:space:]]+-c'
54
71
 
55
- # The end of an epic: there waiting for the word of the owner is the work itself, and the guard of
56
- # the stop refuses taking the next task. The reading lies apart and is shared by the three guards;
57
- # it goes to the hosting, so the guard asks it right before a refusal, not on every turn.
58
- rt_te_epic_over() {
59
- # shellcheck disable=SC1090
60
- . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/epic-over.sh" 2>/dev/null || return 1
61
- command -v rt_epic_over >/dev/null 2>&1 || return 1
62
- rt_epic_over
63
- }