@rt-tools/agent-kit 0.29.0 → 0.29.1

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 (48) hide show
  1. package/assets/checks/board-epic-plan.github.mjs +128 -9
  2. package/assets/checks/board-epics.github.mjs +6 -10
  3. package/assets/checks/board-folders.mjs +50 -0
  4. package/assets/checks/check-lib-layers.mjs +4 -2
  5. package/assets/checks/check-spec-coverage.mjs +65 -0
  6. package/assets/checks/check-work-steps.mjs +238 -0
  7. package/assets/checks/epic-table.github.mjs +15 -6
  8. package/assets/checks/lib-common.mjs +9 -4
  9. package/assets/checks/lib-domains.mjs +7 -4
  10. package/assets/checks/rt-kit-checks.config.mjs +1 -1
  11. package/assets/checks/task-new.github.mjs +6 -6
  12. package/assets/defaults/project.sh +15 -1
  13. package/assets/hooks/git-guard-delivery-epic.sh +8 -1
  14. package/assets/hooks/git-guard-delivery.sh +2 -1
  15. package/assets/hooks/git-guard-push-tests.sh +31 -8
  16. package/assets/hooks/grill-gate.sh +63 -2
  17. package/assets/hooks/prose-style-guard.sh +49 -3
  18. package/assets/hooks/turn-exit-epic.sh +39 -0
  19. package/assets/hooks/turn-exit-guard.sh +6 -4
  20. package/assets/hooks/turn-exit-patterns.sh +51 -0
  21. package/assets/hooks/turn-exit-verdict.sh +20 -1
  22. package/assets/hooks/waiting-turn-guard.sh +56 -1
  23. package/assets/hooks/write-targets.sh +23 -1
  24. package/assets/laws/work-conduct.md +22 -0
  25. package/assets/patterns/doc-style-human.md +6 -0
  26. package/assets/patterns/git-workflow-merge.md +1 -0
  27. package/assets/patterns/git-workflow-pr-body.github.md +10 -5
  28. package/assets/patterns/spec-driven-domain.md +4 -0
  29. package/assets/patterns/status-report-table.github.md +6 -0
  30. package/assets/patterns/task-flow-start.md +26 -22
  31. package/assets/patterns/testing-e2e.md +14 -0
  32. package/assets/pitfalls/turn-conduct.md +28 -0
  33. package/assets/rules/git-workflow.github.md +2 -1
  34. package/assets/rules/observability.needs-app.md +5 -0
  35. package/assets/rules/reuse-first.md +33 -11
  36. package/assets/rules/task-flow.md +30 -29
  37. package/assets/rules/testing.md +15 -0
  38. package/assets/rules/turn-conduct.md +44 -46
  39. package/assets/rules/typescript-conventions.md +8 -0
  40. package/assets/samples/tasks/_template/plan.md +7 -1
  41. package/assets/samples/tasks/_template/progress.md +13 -0
  42. package/lib/observations-cargo.d.ts +12 -0
  43. package/lib/observations-cargo.d.ts.map +1 -1
  44. package/lib/observations-cargo.js +18 -1
  45. package/lib/observations-cargo.js.map +1 -1
  46. package/package.json +1 -1
  47. package/rt-tools-agent-kit-0.29.1.tgz +0 -0
  48. package/rt-tools-agent-kit-0.29.0.tgz +0 -0
@@ -13,12 +13,17 @@ import { CONFIG, ROOT, parseAllowlist } from './rt-kit-checks.config.mjs';
13
13
 
14
14
  const FAMILIES = CONFIG.families;
15
15
  /**
16
- * The lib root and the backend family come from the tree settings, not from the code: in a tree
16
+ * The lib root and the backend families come from the tree settings, not from the code: in a tree
17
17
  * that keeps its libs under another name the walk went past the code and the check went green on an
18
18
  * empty directory — that is, it answered "no violations" where it had not looked at all.
19
19
  */
20
20
  const LIBS_ROOT = CONFIG.libsRoot;
21
- const API_FAMILY = CONFIG.apiFamily;
21
+ /**
22
+ * The backend families are a list, and a single name is read as a list of one: a tree holds more
23
+ * than one server application under the same root, and a family the walk skipped is
24
+ * indistinguishable from one walked without divergences.
25
+ */
26
+ const API_FAMILIES = [CONFIG.apiFamily ?? []].flat().filter(Boolean);
22
27
  /**
23
28
  * The selector prefix mandatory for frontend libs. The word belongs to the tree entirely; empty
24
29
  * means the prefix means nothing here, and the check says nothing about it.
@@ -37,7 +42,7 @@ const COMMON_DOMAIN_LAYERS = [...FLAT_LAYERS, 'feature'].sort();
37
42
  /** The backend has no `ui` and no `shell`: it renders no markup and does no routing */
38
43
  const API_DOMAIN_LAYERS = ['api', 'data-access', 'feature', 'util'];
39
44
  /** Where libs that ended up outside the domain grid are looked for */
40
- const LIB_ROOTS = [...FAMILIES, API_FAMILY].map((family) => `${LIBS_ROOT}/${family}`);
45
+ const LIB_ROOTS = [...FAMILIES, ...API_FAMILIES].map((family) => `${LIBS_ROOT}/${family}`);
41
46
  const REQUIRED_FILES = ['project.json', 'tsconfig.json', 'vitest.config.mts', 'src/index.ts'];
42
47
  const BOUNDARIES_DIR = 'eslint/boundaries/domains';
43
48
 
@@ -173,7 +178,7 @@ function sourceCount(libPath) {
173
178
  export {
174
179
  FAMILIES,
175
180
  LIBS_ROOT,
176
- API_FAMILY,
181
+ API_FAMILIES,
177
182
  LIB_PREFIX,
178
183
  BARREL_FILES,
179
184
  FLAT_LAYERS,
@@ -8,7 +8,7 @@
8
8
  */
9
9
  import {
10
10
  API_DOMAIN_LAYERS,
11
- API_FAMILY,
11
+ API_FAMILIES,
12
12
  COMMON_DOMAIN_LAYERS,
13
13
  FAMILIES,
14
14
  FEATURE_DOMAIN_LAYERS,
@@ -88,7 +88,8 @@ function collectDomainLibs(domainPath, isCommon) {
88
88
  }
89
89
 
90
90
  /**
91
- * The domains of the backend: `<libs root>/<backend family>/<domain>` with four layers. `feature`
91
+ * The domains of the backend: `<libs root>/<backend family>/<domain>` with four layers, walked for
92
+ * every family of the list. `feature`
92
93
  * is both a lib and a directory with a lib per proto service — by the same rule by which a
93
94
  * feature domain of the frontend has `feature/<screen>`.
94
95
  *
@@ -99,8 +100,10 @@ function collectDomainLibs(domainPath, isCommon) {
99
100
  function collectApiDomainLibs() {
100
101
  const libs = [];
101
102
 
102
- for (const entry of dirsIn(`${LIBS_ROOT}/${API_FAMILY}`)) {
103
- const domainPath = `${LIBS_ROOT}/${API_FAMILY}/${entry}`;
103
+ const domainPaths = API_FAMILIES.flatMap((family) =>
104
+ dirsIn(`${LIBS_ROOT}/${family}`).map((entry) => `${LIBS_ROOT}/${family}/${entry}`)
105
+ );
106
+ for (const domainPath of domainPaths) {
104
107
  if (isNotDomain(domainPath) || isLegacyDomain(domainPath)) {
105
108
  continue;
106
109
  }
@@ -186,7 +186,7 @@ const DEFAULTS = {
186
186
  libsRoot: 'libs',
187
187
  /** The families of front-end libs: `<libs root>/<family>/<domain>/<layer>`. */
188
188
  families: ['site', 'admin'],
189
- /** The backend family under the same root: its domains have a layer ladder of their own. */
189
+ /** The backend families under the same root — one name or a list of them; their domains have a layer ladder of their own. */
190
190
  apiFamily: 'api',
191
191
  /** The scope of import aliases: `@scope/<family>/<domain>/<layer>` in `tsconfig.base.json`. */
192
192
  importScope: '@app',
@@ -227,27 +227,27 @@ function epicOfCard(epicNumber) {
227
227
  // law into every task of the epic as its plan, and the branch of the epic was then read from
228
228
  // that law and came back empty. Both sides answered as usual, and the miss showed only by
229
229
  // reading a created task.
230
- const found = planPathOf(card.body, { makeupRequired: false });
230
+ const found = planPathOf(card.body, { makeupRequired: false, epicNumber });
231
231
  const plan = found.path;
232
232
  if (plan === null) {
233
233
  fail(`the card of the epic #${epicNumber}: ${found.why}. The branch of the epic is named in the plan too`);
234
234
  }
235
- return { plan, branch: epicBranchOf(plan, epicNumber) };
235
+ return { plan, branch: epicBranchOf(found.text, epicNumber) };
236
236
  }
237
237
 
238
238
  /**
239
- * The branch of the epic, read from the header of its plan.
239
+ * The branch of the epic, read from the header of its plan. The text comes from the shared reading:
240
+ * a plan of a live epic lies in the branch of that epic and not on the disk of this one.
240
241
  *
241
242
  * Three spellings are accepted: the one this command writes, the bare word about a branch and the
242
243
  * English one — the plans of a tree are translated one at a time, and a single spelling would take
243
244
  * every plan but the freshest out of the reading in silence.
244
245
  */
245
246
  function epicBranchOf(plan, epicNumber) {
246
- const path = join(ROOT, plan);
247
- if (!existsSync(path)) {
247
+ if (plan === null || plan === undefined) {
248
248
  return null;
249
249
  }
250
- const named = readFileSync(path, 'utf8').match(
250
+ const named = plan.match(
251
251
  new RegExp(`\\*\\*(?:Ветка эпика|Ветка|Branch):\\*\\*\\s*\`?(${TASK_KEY}-${epicNumber}-[a-z0-9][a-z0-9-]*)\`?`)
252
252
  );
253
253
  return named ? named[1] : null;
@@ -101,13 +101,26 @@ rt_push_checks_default() {
101
101
 
102
102
  for check in check-doc-paths check-specs check-file-size check-dupes check-styles \
103
103
  check-glossary check-lib-layers check-reuse check-schema-drift check-states check-state-next \
104
- check-turn-map check-archive-age check-profile-drift check-hook-scope check-push-gate; do
104
+ check-turn-map check-work-steps check-spec-coverage check-archive-age check-profile-drift \
105
+ check-hook-scope check-push-gate; do
105
106
  [ -f "$root/$RT_CHECKS_DIR/$check.mjs" ] && printf '%s\n' "node $RT_CHECKS_DIR/$check.mjs"
106
107
  done
107
108
 
108
109
  return 0
109
110
  }
110
111
 
112
+ # Is a check of the gate set heavy: success — it builds, raises or shoots, and costs minutes. After
113
+ # the first red check the gate still runs the light ones, so that one refusal names every red among
114
+ # them; a heavy one is not started then — its minutes buy nothing while the push is refused anyway.
115
+ # Before, the gate left on the first red, and every fix opened the next reason by a push of its own.
116
+ rt_push_check_heavy_default() {
117
+ case "$1" in
118
+ *' nx '* | *'nx affected'* | *'nx run'* | *'docker '* | *playwright* | *e2e*) return 0 ;;
119
+ esac
120
+
121
+ return 1
122
+ }
123
+
111
124
  # Which document must travel by the same commit as this file. Prints a path pattern or stays silent.
112
125
  rt_docs_pair_for_default() {
113
126
  case "$1" in
@@ -456,6 +469,7 @@ rt_qa_decorative_default() {
456
469
  # Defaults under the shared names. The project override will declare any of them anew — and call the
457
470
  # same name from here with the `_default` suffix for everything it did not name itself.
458
471
  rt_push_checks() { rt_push_checks_default "$@"; }
472
+ rt_push_check_heavy() { rt_push_check_heavy_default "$@"; }
459
473
  rt_push_docs_only() { rt_push_docs_only_default "$@"; }
460
474
 
461
475
  rt_docs_pair_for() { rt_docs_pair_for_default "$@"; }
@@ -76,8 +76,15 @@ rt_epic_base() {
76
76
 
77
77
  # The main branch is asked of the epic, not of the base: there it is fixed by one merge, and the
78
78
  # branches of all its tasks get it at once.
79
+ #
80
+ # The base answers for the epic when it already carries the tip. The merge of the main branch
81
+ # reaches the remote copy of the epic branch only through a push, and a push is sometimes
82
+ # refused by a check over code that a task of this epic is meant to fix — the task whose branch
83
+ # is being created right here. Judged by the remote copy alone, the guard closes the only road
84
+ # to that fix and names no other: the epic stands until a person lifts a check by hand.
79
85
  if git rev-parse --verify --quiet "origin/${main_branch}" >/dev/null 2>&1 \
80
- && ! git merge-base --is-ancestor "origin/${main_branch}" "origin/${_epic_branch}" 2>/dev/null; then
86
+ && ! git merge-base --is-ancestor "origin/${main_branch}" "origin/${_epic_branch}" 2>/dev/null \
87
+ && ! git merge-base --is-ancestor "origin/${main_branch}" "$_base_ref" 2>/dev/null; then
81
88
  _behind="$(git rev-list --count "origin/${_epic_branch}..origin/${main_branch}" 2>/dev/null)"
82
89
  fault "the branch of the epic «${_epic_branch}» does not carry the tip of «${main_branch}» — it has moved ahead by ${_behind:-several} commits. The main branch is merged into the branch of the epic, not bypassed by taking the base from the main one: git checkout ${_epic_branch}, git merge origin/${main_branch}, and send the branch."
83
90
  fi
@@ -368,7 +368,8 @@ command -v rt_epic_own_pull >/dev/null 2>&1 && rt_epic_own_pull "$state"
368
368
  title=''
369
369
  if command -v perl >/dev/null 2>&1; then
370
370
  title="$(printf '%s' "$cmd" | perl -0ne '
371
- if (/(?:--title|-t)(?:=|\s+)(?:"((?:[^"\\]|\\.)*)"|\x27([^\x27]*)\x27|(\S+))/s) {
371
+ my $v = qr/(?:"((?:[^"\\]|\\.)*)"|\x27([^\x27]*)\x27|(\S+))/;
372
+ if (/--title(?:=|\s+)$v/s || /(?<![-\w])-t(?:=|\s+)$v/s) {
372
373
  print defined $1 ? $1 : (defined $2 ? $2 : $3);
373
374
  }
374
375
  ' 2>/dev/null)"
@@ -176,12 +176,24 @@ fi
176
176
  # breakage.
177
177
  rt_skip_code="${RT_SKIP_CODE:-7}"
178
178
 
179
+ # Every red among the light checks is named by one refusal. The gate used to leave on the first red
180
+ # one, and each fix opened the next reason by a push of its own: three pushes in a row went so on one
181
+ # branch — a repeated function, a name shared by two kits, a paint probe. After the first red a
182
+ # heavy check is not started: the push is refused anyway, and its minutes buy nothing.
183
+ command -v rt_push_check_heavy >/dev/null 2>&1 || rt_push_check_heavy() { return 1; }
184
+
179
185
  failed=""
180
186
  output=""
181
187
  skipped=""
188
+ held=""
182
189
  ran=0
183
190
  while IFS= read -r check; do
184
191
  [ -z "$check" ] && continue
192
+ if [ -n "$failed" ] && rt_push_check_heavy "$check"; then
193
+ held="${held}${held:+
194
+ }${check}"
195
+ continue
196
+ fi
185
197
  ran=$((ran + 1))
186
198
  out="$(eval "$check" 2>&1)"
187
199
  status=$?
@@ -191,9 +203,12 @@ while IFS= read -r check; do
191
203
  }${check}"
192
204
  continue
193
205
  fi
194
- failed="$check"
195
- output="$out"
196
- break
206
+ failed="${failed}${failed:+
207
+ }${check}"
208
+ output="${output}${output:+
209
+
210
+ }── ${check}
211
+ $(printf '%s' "$out" | tail -n 25)"
197
212
  done <<EOF
198
213
  $(rt_push_checks "$base")
199
214
  EOF
@@ -245,19 +260,27 @@ fi
245
260
 
246
261
  rt_push_gate_note red
247
262
 
248
- # The tail of the output, not all of it: the runner prints a long one, and what is needed is the
249
- # reason for the refusal.
250
- tail_out="$(printf '%s' "$output" | tail -n 40 | tr -d '\000')"
263
+ # The tail of every red output, not all of it: the runner prints a long one, and what is needed is
264
+ # the reason for the refusal.
265
+ tail_out="$(printf '%s' "$output" | tr -d '\000')"
266
+ red_count="$(printf '%s\n' "$failed" | grep -c .)"
267
+ held_line=""
268
+ [ -n "$held" ] && held_line="
269
+
270
+ Not started after the red ones — heavy, and the push is refused anyway:
271
+ ${held}"
251
272
  # Red comes in two kinds, and the guard does not tell them apart: it checks only the return code.
252
273
  # When a check of the code fails, "fix it and push again" is right. When the check itself is wrong,
253
274
  # the same text orders fixing code nobody touched: a refusal once taken apart lay wholly in
254
275
  # documents untouched by any commit of the branch. The verifiability law says a broken check does
255
276
  # not stop the work, and until this line there was no such option in the refusal.
256
- reason="BLOCKED: a push without a green local run. «${failed}» failed — fix it and push again, the guard must not be bypassed. A push is the entry into the pipeline, and red from here is checked already in production.
277
+ reason="BLOCKED: a push without a green local run. Red checks: ${red_count} — fix them and push again, the guard must not be bypassed. A push is the entry into the pipeline, and red from here is checked already in production.
278
+
279
+ ${failed}${held_line}
257
280
 
258
281
  Three moves from here: fix what is named and repeat the call; fix the check itself, if it is the one that is wrong — take the refusals apart one by one, show the analysis to the owner and correct the check; or bring the owner the price of a bypass and wait for their word. What is in dispute is not added to the known list: it holds what was accepted, not the results of a broken check.
259
282
 
260
- The tail of the output:
283
+ The tail of every red output:
261
284
 
262
285
  ${tail_out}"
263
286
 
@@ -105,6 +105,7 @@ if command -v skill_for >/dev/null 2>&1 && [ -n "$rules_dir" ]; then
105
105
  def is_input:
106
106
  .type == "user"
107
107
  and ((.isCompactSummary // false) | not)
108
+ and ((.isMeta // false) | not)
108
109
  and (((.message.content // []) | if type == "array"
109
110
  then ([.[] | select(.type == "tool_result")] | length)
110
111
  else 0 end) == 0);
@@ -128,6 +129,9 @@ fi
128
129
  # A turn is everything recorded after the last real input from the owner. A tool answer comes in
129
130
  # under the same `user` role, so lines with `tool_result` do not count as input: otherwise the turn
130
131
  # would be the piece after the last tool call, and reading the rules at its start would be lost.
132
+ # The text of a loaded rule and the report of a subagent come in under that role too, marked
133
+ # `isMeta`. Taken for input, a rule loaded right before a question cut the turn after its own
134
+ # loading, and its text stood as the owner's last reply.
131
135
  #
132
136
  # A tail of 400 lines: the turn record grows all session, and only the last turn is judged.
133
137
  # On the tool call event the question is already known — it is the call; only whether the rules
@@ -137,6 +141,7 @@ verdict="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r --arg re "$read_re"
137
141
  def is_input:
138
142
  .type == "user"
139
143
  and ((.isCompactSummary // false) | not)
144
+ and ((.isMeta // false) | not)
140
145
  and (((.message.content // []) | if type == "array"
141
146
  then ([.[] | select(.type == "tool_result")] | length)
142
147
  else 0 end) == 0);
@@ -184,11 +189,16 @@ verdict="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r --arg re "$read_re"
184
189
  # FAIL-OPEN: no question in the call, no reply from the owner, no earlier question — the sign stays
185
190
  # silent.
186
191
  if [ -n "$tool" ]; then
187
- asked_json="$(printf '%s' "$input" | jq -r '(.tool_input.questions // []) | tostring' 2>/dev/null)"
188
- seen="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r --arg now "$asked_json" '
192
+ # The words are taken from what the owner reads — the question, the options and their
193
+ # descriptions — and not from the serialised menu: its keys `question`, `label` and
194
+ # `description` stand in every call and matched any English text by two words of the three.
195
+ asked_text="$(printf '%s' "$input" | jq -r '[(.tool_input.questions // [])[]
196
+ | (.question // ""), ((.options // [])[] | (.label // ""), (.description // ""))] | join(" ")' 2>/dev/null)"
197
+ seen="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r --arg now "$asked_text" '
189
198
  def is_input:
190
199
  .type == "user"
191
200
  and ((.isCompactSummary // false) | not)
201
+ and ((.isMeta // false) | not)
192
202
  and (((.message.content // []) | if type == "array"
193
203
  then ([.[] | select(.type == "tool_result")] | length)
194
204
  else 0 end) == 0);
@@ -232,6 +242,57 @@ ${deny_tail_text}"
232
242
  fi
233
243
  fi
234
244
 
245
+ # The third sign of the same guard: the owner answered the recommended option twice in a row.
246
+ #
247
+ # Every menu carries a recommended option, and an owner who takes it twice running has shown that
248
+ # the decisions are not theirs: the questions are answered by the tree, and the menu only asks them
249
+ # to confirm it. A grill went on to a fifth menu after four such answers, and the owner closed it
250
+ # with the name of a ready-made module that answered all five. From the second answer on, the
251
+ # remaining questions are closed by an assumption written into the grill, and the owner gets the
252
+ # list in one line — not a menu.
253
+ #
254
+ # What is judged is the record of the question tool: the last two of its answers, each with every
255
+ # option taken marked as recommended. Any other answer breaks the streak, and a menu after it goes
256
+ # out as usual. The mark is the word the menu itself carries, in either language of the tree.
257
+ #
258
+ # FAIL-OPEN: fewer than two answers in the record, no record, no parser — the sign stays silent.
259
+ if [ -n "$tool" ]; then
260
+ streak="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r '
261
+ def answer_texts: [.[] | select(.type == "user") | (.message.content // [])
262
+ | select(type == "array") | .[] | select(.type == "tool_result") | .content
263
+ | if type == "string" then . elif type == "array"
264
+ then (map(if type == "object" then (.text // "") else "" end) | join("\n")) else "" end
265
+ | select(test("^The user answered:"))];
266
+ def recommended: ([match("\"=\"[^\"]*\""; "g")] | length) as $all
267
+ | ([match("\\((Recommended|Рекомендую|Рекомендован[^)]*)\\)\"(,|\\.|[[:space:]]|$)"; "g")] | length) as $marked
268
+ | $all > 0 and $all == $marked;
269
+ (answer_texts) as $answers
270
+ | if ($answers | length) < 2 then "no"
271
+ elif ($answers[-1] | recommended) and ($answers[-2] | recommended) then "streak"
272
+ else "no" end
273
+ ' 2>/dev/null)"
274
+
275
+ if [ "$streak" = "streak" ]; then
276
+ reason="BLOCKED by grill-gate: the owner took the recommended option on the last two menus in a row — the remaining questions are closed by assumption, not by a menu.
277
+
278
+ Two answers «recommended» running say the decisions are not the owner's: the tree answers these questions, and the menu only asks them to confirm it. Write what is taken into the grill as decisions, name the list to the owner in one line, and go on with the work. Before that, look for a ready-made module of the same kind — in this tree and in the trees recorded as samples: one grill of five menus was closed by the owner naming such a module.
279
+
280
+ A menu goes out again only for a question no assumption closes, and it says so in its first line."
281
+
282
+ # shellcheck disable=SC1090
283
+ [ -f "$rt_hooks_dir/deny-tail.sh" ] && . "$rt_hooks_dir/deny-tail.sh" 2>/dev/null
284
+ command -v rt_deny_tail >/dev/null 2>&1 || rt_deny_tail() { :; }
285
+ deny_tail_text="$(rt_deny_tail "")"
286
+ [ -n "$deny_tail_text" ] && reason="${reason}
287
+
288
+ ${deny_tail_text}"
289
+
290
+ jq -n --arg r "$reason" '{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$r}}' 2>/dev/null \
291
+ || printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"grill-gate: two recommended answers in a row close the remaining questions by assumption."}}\n'
292
+ exit 0
293
+ fi
294
+ fi
295
+
235
296
  [ "$verdict" = "ask" ] || exit 0
236
297
 
237
298
  if [ -n "$tool" ]; then
@@ -40,6 +40,30 @@ rt_hooks_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
40
40
  [ -f "$rt_hooks_dir/write-targets.sh" ] && . "$rt_hooks_dir/write-targets.sh" 2>/dev/null
41
41
  command -v rt_write_targets >/dev/null 2>&1 || rt_write_targets() { cat >/dev/null; }
42
42
 
43
+ # The bodies of the heredocs of a command, one after another. The delimiter is taken as the shell
44
+ # takes it — quoted or bare — and everything up to the closing line is the written text.
45
+ rt_prose_heredoc_bodies() {
46
+ awk -v q="'" '
47
+ inside {
48
+ if ($0 == tag) { inside = 0 } else { print }
49
+ next
50
+ }
51
+ {
52
+ s = $0
53
+ re = "<<-?[ \t]*(\"[^\"]+\"|" q "[^" q "]+" q "|[A-Za-z_][A-Za-z0-9_]*)"
54
+ while (match(s, re)) {
55
+ t = substr(s, RSTART, RLENGTH)
56
+ s = substr(s, RSTART + RLENGTH)
57
+ sub(/^<<-?[ \t]*/, "", t)
58
+ gsub(/\"/, "", t)
59
+ gsub(q, "", t)
60
+ tag = t
61
+ inside = 1
62
+ }
63
+ }
64
+ '
65
+ }
66
+
43
67
  tool="$(rt_hook_tool)"
44
68
  case "$tool" in
45
69
  Edit | Write | MultiEdit)
@@ -52,9 +76,13 @@ case "$tool" in
52
76
  # The first document among the write targets. A command writing several is judged by one of
53
77
  # them: the text is one for the whole command, and a second refusal would repeat the first.
54
78
  path="$(printf '%s' "$cmd" | rt_write_targets | grep -m1 '\.md$')"
55
- # The new text is the body of the command: the written text stands inside it, and the
56
- # findings of the check name the very lines that go into the file.
57
- added="$cmd"
79
+ # The new text is taken from the heredoc bodies of the command, not from the whole call.
80
+ # A rewrite carrying the previous wording inside the call would otherwise be refused by
81
+ # the words of that previous wording, and the refusal repeats at the second attempt. No
82
+ # heredoc in the command — the whole text is judged, as before: a redirect of a printf
83
+ # holds the written text and nothing else.
84
+ added="$(printf '%s' "$cmd" | rt_prose_heredoc_bodies)"
85
+ [ -z "$added" ] && added="$cmd"
58
86
  ;;
59
87
  *) exit 0 ;;
60
88
  esac
@@ -64,6 +92,24 @@ case "$path" in
64
92
  *) exit 0 ;;
65
93
  esac
66
94
 
95
+ # The judgement's boundary is the project root. A scratch document written outside it belongs to no
96
+ # repository, and the wording conventions of this tree are nobody's convention there: a fixture in
97
+ # the system temporary directory was refused over a word in it.
98
+ if [ -n "${CLAUDE_PROJECT_DIR:-}" ]; then
99
+ case "$path" in
100
+ /*)
101
+ rt_root="$(cd "$CLAUDE_PROJECT_DIR" 2>/dev/null && pwd)"
102
+ # A root that cannot be entered is taken as it was declared: the directory may be
103
+ # absent on this machine, and that says nothing about where the document lies.
104
+ [ -n "$rt_root" ] || rt_root="$CLAUDE_PROJECT_DIR"
105
+ case "$path" in
106
+ "$rt_root"/*) ;;
107
+ *) exit 0 ;;
108
+ esac
109
+ ;;
110
+ esac
111
+ fi
112
+
67
113
  # The archive and the task folders are not judged: the archive is not edited at all, and the
68
114
  # progress is written in haste and lives until the merge.
69
115
  case "$path" in
@@ -108,3 +108,42 @@ Do what does not depend on the answer in this same turn, then ask by the tool.
108
108
 
109
109
  The guard judges one turn: the next session is not refused." "a question without work in a running stage."
110
110
  }
111
+
112
+ # The next step rewritten into the progress and not begun. The turn rewrote the progress, and after
113
+ # that edit did nothing but commit and push it: the stage number moved, the report is full, and the
114
+ # step the line names is untouched. Judged in a running stage alone; handed-in work has a tier of
115
+ # its own. Expects the verdict signs and `$state` to be set.
116
+ rt_te_next_step_deny() {
117
+ [ "${progress_edited:-false}" = "true" ] || return 0
118
+ [ "${after_progress:-false}" = "true" ] && return 0
119
+ [ "${state:-}" = "этап-идёт" ] || return 0
120
+ [ "${handed_over:-false}" = "true" ] && return 0
121
+ rt_te_deny "BLOCKED by turn-exit-guard: the progress was rewritten in this turn, and after that edit the turn did nothing but commit and push it — the next step it names is not begun.
122
+
123
+ The volume done before the line does not replace the step: a closed stage, a full report and a moved stage number are the shape of four stops in a row. A rewritten progress is a promise about the next action, and the turn that wrote it keeps it.
124
+
125
+ The next step is written in the progress: ${next_step}
126
+
127
+ Begin it in this same turn: an edit outside the task folder or a command changing the tree. The owner said to stop — then write so: the guard reads their word, not a retelling.
128
+
129
+ The guard judges one turn: the next session is not refused." "the next step named by the progress is not begun."
130
+ }
131
+
132
+ # A launch in the background as the last action of the turn. A role sent to review, an exploration
133
+ # sent to a subagent, a command sent behind the turn: none of them asks anything while it runs and
134
+ # none goes faster for being waited on. A turn that ended on the launch stands exactly as long as an
135
+ # empty one — the owner sees the executor standing still with the role working. The tier lives here
136
+ # next to the state tiers because the guard file is at its length limit; it is not gated by the
137
+ # epic. Expects the verdict signs to be set.
138
+ rt_te_launch_last_deny() {
139
+ [ "${launched_last:-false}" = "true" ] || return 0
140
+ rt_te_deny "BLOCKED by turn-exit-guard: the last action of the turn was a launch in the background — a role or a command — and the turn ended on it.
141
+
142
+ A launch is an announcement of intent, not work: what was sent goes on without the executor and asks nothing on the way. While it runs, what does not depend on it is done — the epic, the tasks, the branch, the next stage — and its findings are taken when they come back.
143
+
144
+ The next step is written in the progress: ${next_step}
145
+
146
+ Do it in this same turn; the launch may stand anywhere in the turn but last.
147
+
148
+ The guard judges one turn: the next session is not refused." "the turn ended on a launch in the background."
149
+ }
@@ -379,6 +379,8 @@ The guard judges one turn: the next session is not refused."
379
379
  rt_te_deny "$reason" "a closed stage is not backed by the output of a command."
380
380
  fi
381
381
  fi
382
+ # The next step rewritten into the progress and not begun: the tier lies next to the epic tiers.
383
+ command -v rt_te_next_step_deny >/dev/null 2>&1 && rt_te_next_step_deny
382
384
 
383
385
  # The work is handed over and the next one is only named. There is more work in such a turn than in
384
386
  # any other — and all of it is on the task handed in: the handover finishes the previous work, not
@@ -418,7 +420,7 @@ The guard judges one turn: the next session is not refused."
418
420
 
419
421
  rt_te_deny "$reason" "the turn ended with waiting for a step by someone else."
420
422
  fi
421
-
423
+ command -v rt_te_launch_last_deny >/dev/null 2>&1 && rt_te_launch_last_deny
422
424
  # The shared line. There was work in the turn — but the last action was not it, it was a text to the
423
425
  # owner. The particular tiers above name the kind of stop more precisely; what reaches here is what
424
426
  # they do not know by name.
@@ -446,9 +448,7 @@ if [ "$worked" = "true" ] && [ "$handed_over" != "true" ]; then
446
448
  if [ -n "$unpushed" ] && [ "$unpushed" -gt 0 ] 2>/dev/null; then
447
449
  reason="BLOCKED by turn-exit-guard: there was work in the turn, but it stayed in the working tree — commits not handed over: ${unpushed}.
448
450
 
449
- The owner sees the former state and reads it as «nothing was done». Two moves from here: bring the work to the hosting — push the branch and open a request — or remove what was done if it is not needed.
450
-
451
- What is left in the tree is named with a reason — in the words of the owner, not as a list of leftovers.
451
+ The owner sees the former state and reads it as «nothing was done». Two moves from here: bring the work to the hosting — push the branch and open a request — or remove what was done if it is not needed. What is left in the tree is named with a reason — in the words of the owner, not as a list of leftovers.
452
452
 
453
453
  The guard judges one turn: the next session is not refused."
454
454
 
@@ -456,6 +456,8 @@ The guard judges one turn: the next session is not refused."
456
456
  fi
457
457
  fi
458
458
 
459
+ rt_te_steps_deny "$progress" # the steps of the plan are not done; the tier lies in the patterns
460
+
459
461
  # There was work in the turn, and the epic is open: the turn is released outside an epic and under a
460
462
  # closed one alone. Under an open epic the work stands where it stood, and the next step is named.
461
463
  [ "$worked" = "true" ] && rt_te_epic_open && rt_te_epic_deny worked
@@ -69,3 +69,54 @@ standing_work_re='работай[^.]{0,40}(без остановок|без па
69
69
  handover_re='gh[[:space:]]+pr[[:space:]]+create'
70
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'
71
71
 
72
+ # How many steps of the plan are not done yet. The progress mirrors the steps of the plan with a
73
+ # mark each — `[x]` done, `[>]` going on right now, `[ ]` not begun — and a check keeps the two
74
+ # lists matched. The reading is a local file, not a call to the hosting: it costs nothing and is
75
+ # asked on every turn, unlike the state of the epic.
76
+ #
77
+ # Prints the number and answers non-zero when there is nothing to read: no progress, or a progress
78
+ # with no list of steps. A tier that REFUSES a turn must tell "the work goes on" from "there is
79
+ # nothing to ask with" — the same reason the count of the epic's tasks is printed rather than folded
80
+ # into an exit code.
81
+ rt_te_steps_left() {
82
+ [ -f "$1" ] || return 1
83
+ grep -qE '^-[[:space:]]+\[[x> ]\][[:space:]]+[0-9]+\.[0-9]+[[:space:]]' "$1" 2>/dev/null || return 1
84
+ printf '%s' "$(grep -cE '^-[[:space:]]+\[[> ]\][[:space:]]+[0-9]+\.[0-9]+[[:space:]]' "$1" 2>/dev/null || printf '0')"
85
+ return 0
86
+ }
87
+
88
+ # The step going on right now: its number and its name, as the progress writes them. Empty when no
89
+ # step carries the mark — the refusal then names the count alone.
90
+ rt_te_step_now() {
91
+ [ -f "$1" ] || return 0
92
+ sed -nE 's/^-[[:space:]]+\[>\][[:space:]]+([0-9]+\.[0-9]+[[:space:]]+.*)$/\1/p' "$1" 2>/dev/null | head -1
93
+ }
94
+
95
+ # The refusal about a step that is not done. The text lies here and not in the guard for the same
96
+ # reason as the one about the epic: the guard stands at its length limit.
97
+ #
98
+ # Why the tier exists. The tier about the epic judges the whole: while the epic holds tasks, a turn
99
+ # does not end. It says nothing about a turn inside one task — the epic may hold a single task, and
100
+ # the work inside it breaks off in the middle all the same. The stage tier next to it reads the
101
+ # «Verified by» command of a stage declared closed, and a stage nobody declared closed it does not
102
+ # touch either.
103
+ rt_te_steps_reason() {
104
+ printf '%s' "BLOCKED by turn-exit-guard: ${1} steps of the plan are not done, and the turn ends without the word of the owner about stopping.
105
+
106
+ The step going on right now: ${2:-none is marked as going on}
107
+
108
+ Work is not finished while the plan holds steps that are not done. A turn that did work and then reported is no exception: the report ends the account, not the work.
109
+
110
+ The steps are listed in the progress and counted by «npm run check:work-steps»; the plan holds the same list and is not edited.
111
+
112
+ Do them in this same turn. Lawful exits stay as they were: a question to the owner through the tool, a refusal of another guard, a session handover, and the owner's own word about stopping — the guard reads that word from them, not from a retelling."
113
+ }
114
+
115
+ # The tier itself: the steps of the plan are not done. It lies here and not in the guard for the
116
+ # same reason as the text above — the guard stands at its length limit. The tier judges the
117
+ # progress of the current task and says nothing about the epic: the tier next to it does that.
118
+ rt_te_steps_deny() {
119
+ _steps_left="$(rt_te_steps_left "$1" 2>/dev/null)" || return 0
120
+ [ "${_steps_left:-0}" -gt 0 ] 2>/dev/null || return 0
121
+ rt_te_deny "$(rt_te_steps_reason "$_steps_left" "$(rt_te_step_now "$1")")" "${_steps_left} steps of the plan are not done."
122
+ }
@@ -37,6 +37,10 @@ verdict="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r --arg work "$work_re
37
37
  # however much work there was before: the work stays exactly where it stood.
38
38
  | ([$uses[] | select((.name // "") == "Bash") | ((.input.command // "") + (if (.input.run_in_background // false) then " &" else "" end))] | last // "") as $last
39
39
  | ([$uses[] | (.name // "")] | last // "") as $last_name
40
+ # A launch in the background as the last action: a role sent to work by the agent tool, or a
41
+ # command sent behind the turn. The launch is an announcement of intent, and what does not
42
+ # depend on it is done while it runs — the turn does not end on the launch itself.
43
+ | (($last_name | test("^(Agent|Task)$")) or (($last_name == "Bash") and ($last | test("&[[:space:]]*$")))) as $launched_last
40
44
  | (($last_name == "Bash") and ($last | test($wait))) as $waited
41
45
  # Handing the work over: the tail of the turn after the PR was opened. Everything before it was
42
46
  # done on the task handed in and says nothing about the next one.
@@ -90,7 +94,22 @@ verdict="$(tail -n 400 "$transcript" 2>/dev/null | jq -s -r --arg work "$work_re
90
94
  | (($last_say | test("[Жж]ду (твоего|вашего|его|её) (слова|указани|решени|ответа|команды|отмашки)|[Жж]ду слова владельца|[Жж]ду, что скаж|[Оо]стаюсь ждать|[Бб]уду ждать (твоего|вашего)"))
91
95
  and (($asked or $told_stop or $handed) | not)) as $awaits_word
92
96
  | ($last_say | test($promise)) as $promised
97
+ # The next step rewritten into the progress and not begun. The last edit of the progress in the
98
+ # turn is found — by the editing tool or by a writing command naming the file — and after it the
99
+ # turn must hold work other than the commit and the push of it: an edit outside the task folder
100
+ # or a command changing the tree. Four stops of one shape ended with a full report, a moved
101
+ # stage number and an untouched next step; the volume before the line is not the step.
102
+ | ($uses | map(
103
+ (((.name // "") | test("^(Edit|Write|MultiEdit|NotebookEdit)$")) and ((.input.file_path // "") | test("(^|/)progress\\.md$")))
104
+ or (((.name // "") == "Bash") and ((.input.command // "") | test("progress\\.md")) and ((.input.command // "") | test("sed -i|tee |>|python3|cat ")))
105
+ ) | rindex(true)) as $progress_at
106
+ | ($progress_at != null) as $progress_edited
107
+ | (if $progress_at == null then [] else $uses[($progress_at + 1):] end) as $after
108
+ | ($after | map(
109
+ (((.name // "") | test("^(Edit|Write|MultiEdit|NotebookEdit)$")) and ((.input.file_path // "") | test("/tasks/") | not))
110
+ or (((.name // "") == "Bash") and ([(.input.command // "") | splits($part)] | map(test($work) and (test($read) | not) and (test("^[[:space:]]*([^[:space:]]*/)?git[[:space:]]+(add|commit|push)") | not)) | any))
111
+ ) | any) as $after_progress
93
112
  | (($last_name == "Bash") and ($last | test($started)) and ($handed_over | not)) as $only_took
94
- | { promised: $promised, only_took: $only_took, asked: $asked, handed_by_hand: ($handed and (($asked or $denied or $told_stop) | not)), standing_work: $standing_work, 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 }
113
+ | { promised: $promised, only_took: $only_took, asked: $asked, handed_by_hand: ($handed and (($asked or $denied or $told_stop) | not)), standing_work: $standing_work, 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, progress_edited: $progress_edited, after_progress: $after_progress, launched_last: $launched_last, ran: $ran }
95
114
  ' 2>/dev/null)"
96
115
  }