@bluenyang/reins 0.3.4 → 0.4.0-rc.2

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 (66) hide show
  1. package/README.md +164 -39
  2. package/cli/dist/commands/config-gates.js +64 -6
  3. package/cli/dist/commands/config-models.js +19 -0
  4. package/cli/dist/commands/config.js +40 -16
  5. package/cli/dist/commands/configure.js +207 -47
  6. package/cli/dist/commands/install.js +91 -31
  7. package/cli/dist/commands/migrate.js +17 -20
  8. package/cli/dist/commands/update.js +164 -12
  9. package/cli/dist/lib/agents-md.js +55 -13
  10. package/cli/dist/lib/bindings.js +7 -10
  11. package/cli/dist/lib/blocks.js +82 -18
  12. package/cli/dist/lib/detect.js +88 -3
  13. package/cli/dist/lib/gate-refresh.js +35 -5
  14. package/cli/dist/lib/gate-script.js +303 -25
  15. package/cli/dist/lib/generate.js +48 -6
  16. package/cli/dist/lib/git.js +33 -0
  17. package/cli/dist/lib/migrate-plan.js +1 -1
  18. package/cli/dist/lib/profile.js +101 -0
  19. package/cli/dist/lib/prompter.js +2 -1
  20. package/cli/dist/lib/release.js +1 -1
  21. package/cli/dist/lib/schema.js +6 -3
  22. package/cli/dist/lib/upstream.js +1 -1
  23. package/cli/dist/reins.js +40 -14
  24. package/docs/README.ko.md +113 -23
  25. package/harness/CHANGELOG.md +12 -0
  26. package/harness/VERSION +1 -1
  27. package/harness/policy/common-red-lines.yaml +7 -5
  28. package/harness/roles/advisor.md +1 -1
  29. package/harness/roles/auditor.md +12 -11
  30. package/harness/roles/executor.md +4 -3
  31. package/harness/roles/implementer.md +5 -5
  32. package/harness/roles/pl.md +4 -4
  33. package/harness/roles/planner.md +7 -8
  34. package/harness/roles/pm.md +7 -8
  35. package/harness/schemas/bindings.schema.json +2 -0
  36. package/harness/schemas/install.schema.json +1 -0
  37. package/harness/schemas/intent.schema.json +9 -1
  38. package/harness/schemas/plan.schema.json +18 -0
  39. package/harness/schemas/spec.schema.json +9 -6
  40. package/harness/templates/agents-md-blocks.md +19 -54
  41. package/harness/templates/agents-md-guide.md +11 -10
  42. package/harness/templates/audit.md +14 -41
  43. package/harness/templates/history.md +1 -5
  44. package/harness/templates/intent.md +8 -19
  45. package/harness/templates/plan.md +23 -0
  46. package/harness/templates/spec.md +21 -16
  47. package/package.json +1 -1
  48. package/skills/audit/SKILL.md +27 -33
  49. package/skills/gates/SKILL.md +41 -0
  50. package/skills/implement/SKILL.md +33 -22
  51. package/skills/implement/references/procedure.md +51 -0
  52. package/skills/intent/SKILL.md +29 -20
  53. package/skills/intent/references/procedure.md +60 -0
  54. package/skills/onboard/SKILL.md +39 -28
  55. package/skills/plan/SKILL.md +35 -0
  56. package/skills/plan/references/procedure.md +52 -0
  57. package/skills/reins-setup/SKILL.md +4 -4
  58. package/skills/release/SKILL.md +18 -20
  59. package/skills/retro/SKILL.md +21 -26
  60. package/skills/review/SKILL.md +33 -17
  61. package/skills/review/references/procedure.md +50 -0
  62. package/skills/spec/SKILL.md +25 -19
  63. package/skills/spec/references/procedure.md +67 -0
  64. package/skills/merge/SKILL.md +0 -32
  65. package/skills/roadmap/SKILL.md +0 -29
  66. package/skills/test/SKILL.md +0 -26
@@ -9,7 +9,94 @@ export const GATE_SCRIPT = '.agents/harness/run-gates.sh';
9
9
  export const GATE_LOGS = '.loctx/reports/gates';
10
10
  /** The one line a hook tool runs for a stage (`pre-commit`, `pre-push`). */
11
11
  export const hookLine = (stage) => `sh ${GATE_SCRIPT} ${stage}`;
12
- /** The required (not optional) gate commands of a `gates.yaml` text, in its order. */
12
+ /** The stages whose changed files the script works out; any other stage runs every gate (spec 025 D-1). */
13
+ export const HOOK_STAGES = ['pre-commit', 'pre-push'];
14
+ /**
15
+ * An area glob as a regular expression over a path from the root (spec 025 D-1), as the script reads it:
16
+ * `*` is any run of characters but `/`, `**` any run including `/` (`**\/` also none), `?` one character
17
+ * but `/`; everything else is itself, and the whole path must match.
18
+ */
19
+ export function areaRegExp(glob) {
20
+ let re = '';
21
+ for (let i = 0; i < glob.length; i++) {
22
+ const c = glob[i];
23
+ if (c === '*') {
24
+ if (glob[i + 1] === '*') {
25
+ if (glob[i + 2] === '/') {
26
+ re += '(?:.*/)?';
27
+ i += 2;
28
+ }
29
+ else {
30
+ re += '.*';
31
+ i++;
32
+ }
33
+ }
34
+ else
35
+ re += '[^/]*';
36
+ }
37
+ else if (c === '?')
38
+ re += '[^/]';
39
+ else
40
+ re += c.replace(/[.+^${}()|[\]\\]/g, '\\$&');
41
+ }
42
+ return new RegExp(`^${re}$`);
43
+ }
44
+ const list = (v) => v === null || v === undefined ? [] : Array.isArray(v) ? v.map(String) : [String(v)];
45
+ /**
46
+ * What the gate script does with a `gates.yaml` text (spec 025 D-1, D-2), in TypeScript: the gates in
47
+ * order (`order` when it is there, else the order of `gates`), each run or skipped with the script's
48
+ * reason. `files` are the changed files of a hook stage, or null when they are not known: every area
49
+ * counts then. Only `pre-commit` and `pre-push` look at `on` and `areas`; any other stage runs every
50
+ * required gate. Throws on a gate that names an area `areas` does not define, where the script stops.
51
+ */
52
+ /** Files whose change makes every gate run. */
53
+ export const GATE_FILES = ['.agents/harness/gates.yaml', '.agents/harness/run-gates.sh'];
54
+ export function gatePlan(text, stage = 'manual', files = null) {
55
+ if (text === undefined)
56
+ return [];
57
+ const g = parse(text);
58
+ const gates = g?.gates ?? {};
59
+ const defined = g?.areas ?? {};
60
+ const hook = HOOK_STAGES.includes(stage);
61
+ // a change to the gates themselves is checked by every gate (the script does the same)
62
+ const selfChange = files?.some((f) => GATE_FILES.includes(f)) ?? false;
63
+ if (selfChange)
64
+ files = null;
65
+ const touched = new Set(Object.entries(defined)
66
+ .filter(([, globs]) => files !== null && list(globs).some((x) => files.some((f) => areaRegExp(x).test(f))))
67
+ .map(([a]) => a));
68
+ const out = [];
69
+ for (const name of g?.order ?? Object.keys(gates)) {
70
+ const x = gates[name];
71
+ if (!x?.cmd)
72
+ continue;
73
+ const areas = list(x.areas);
74
+ for (const a of areas)
75
+ if (!(a in defined))
76
+ throw new Error(`the gate ${name} names the area ${a}, which areas: does not define`);
77
+ const on = list(x.on);
78
+ const d = (run, reason) => out.push({ name, cmd: x.cmd, run, reason });
79
+ if (x.optional)
80
+ d(false, 'optional');
81
+ else if (hook && on.length && !on.includes(stage))
82
+ d(false, `not on ${stage}`);
83
+ else if (!areas.length)
84
+ d(true, 'always');
85
+ else if (!hook)
86
+ d(true, `stage ${stage} runs every gate`);
87
+ else if (files === null)
88
+ d(true, selfChange ? 'the gates themselves changed' : 'the changed files are not known');
89
+ else {
90
+ const hit = areas.filter((a) => touched.has(a));
91
+ if (hit.length)
92
+ d(true, `changed in ${hit.join(', ')}`);
93
+ else
94
+ d(false, `no change in ${areas.join(', ')}`);
95
+ }
96
+ }
97
+ return out;
98
+ }
99
+ /** The required (not optional) gate commands of a `gates.yaml` text, in its order: what a full run runs. */
13
100
  export function requiredGates(text) {
14
101
  if (text === undefined)
15
102
  return [];
@@ -21,12 +108,15 @@ export function requiredGates(text) {
21
108
  .map((x) => x.cmd);
22
109
  }
23
110
  /**
24
- * Reads the shape of `gates.yaml` that reins writes (024 S-16) with POSIX awk and prints the required
25
- * commands, one per line, in order: `order` when it is there (names not in `gates` are skipped, gates
26
- * not in `order` do not run), else the order of `gates`; `optional: true` is skipped. On any other shape
27
- * inside `gates:` or `order:` it prints `line <n>: <reason>` and exits 3.
111
+ * Reads the shape of `gates.yaml` that reins writes (024 S-16, spec 025 D-2) with POSIX awk and prints the
112
+ * run plan: for each gate in order (`order` when it is there, names not in `gates` skipped; else the order
113
+ * of `gates`) a line `= run <name>: <reason>` and `> <cmd>`, or `= skip <name>: <reason>`. A gate skips
114
+ * when it is optional, when a hook stage is not in its `on`, or when none of its `areas` holds a changed
115
+ * file. `stage`, `known` (`yes` when the changed files are known) and `why` come in with `-v`, the changed
116
+ * files one per line in the file `cfile`. On any other shape inside `gates:`, `areas:` or `order:` it
117
+ * prints `line <n>: <reason>` and exits 3.
28
118
  */
29
- const AWK = String.raw `
119
+ export const GATE_AWK = String.raw `
30
120
  function fail(why) { print "line " NR ": " why; bad = 1; exit 3 }
31
121
  function rest(r) {
32
122
  sub(/^ +/, "", r)
@@ -36,6 +126,8 @@ function value(v, out, c, i, n) {
36
126
  sub(/^ +/, "", v)
37
127
  if (v == "") return ""
38
128
  c = substr(v, 1, 1)
129
+ # a comment where the value would start: no value (YAML reads null)
130
+ if (c == "#") return ""
39
131
  if (c == "|" || c == ">") fail("a multi-line value")
40
132
  if (c == "&" || c == "*") fail("an anchor or alias")
41
133
  if (c == "{" || c == "[") fail("flow style")
@@ -70,60 +162,169 @@ function value(v, out, c, i, n) {
70
162
  return v
71
163
  }
72
164
  function word(v) { sub(/^ +/, "", v); sub(/ +#.*$/, "", v); sub(/ +$/, "", v); return v }
165
+ # one list item: "- <value>"
166
+ function item(l, v) {
167
+ # the items of one list stand at one indentation; a list at two is no YAML
168
+ match(l, /^ */)
169
+ if (lind == "") lind = RLENGTH
170
+ else if (RLENGTH != lind) fail("a list item at another indentation")
171
+ v = l; sub(/^ *-/, "", v); v = value(v)
172
+ if (v == "") fail("an empty list item")
173
+ return v
174
+ }
175
+ # a glob of an area as an ERE over the whole path: * is any run but /, ** any run, **/ also none, ? one but /
176
+ function globre(g, out, i, n, c) {
177
+ out = ""; n = length(g)
178
+ for (i = 1; i <= n; i++) {
179
+ c = substr(g, i, 1)
180
+ if (c == "*") {
181
+ if (substr(g, i + 1, 1) == "*") {
182
+ if (substr(g, i + 2, 1) == "/") { out = out "(.*/)?"; i += 2 }
183
+ else { out = out ".*"; i++ }
184
+ } else out = out "[^/]*"
185
+ } else if (c == "?") out = out "[^/]"
186
+ else if (index(".[](){}$+|", c)) out = out "[" c "]"
187
+ else if (c == "^" || c == "\\") out = out "\\" c
188
+ else out = out c
189
+ }
190
+ return "^" out "$"
191
+ }
192
+ function addto(kind, v) {
193
+ if (kind == "areas") garea[name, ++gac[name]] = v
194
+ else gon[name, ++goc[name]] = v
195
+ }
73
196
  { sub(/\r$/, "") }
74
197
  /^[ \t]*$/ || /^[ \t]*#/ { next }
75
- /^\t/ || /^ +\t/ { if (sec == "gates" || sec == "order") fail("a tab in the indentation"); next }
198
+ /^\t/ || /^ +\t/ { if (sec == "gates" || sec == "order" || sec == "areas") fail("a tab in the indentation"); next }
76
199
  /^[^ ]/ {
77
- sec = $0; sub(/:.*/, "", sec)
200
+ sec = $0; sub(/:.*/, "", sec); lst = ""; lind = ""
78
201
  if (sec == "gates") hasGates = 1
79
202
  if (sec == "order") hasOrder = 1
80
- if (sec == "gates" || sec == "order") {
203
+ if (sec == "gates" || sec == "order" || sec == "areas") {
81
204
  t = $0; sub(/^[^:]*:/, "", t); t = word(t)
82
- # the empty collections written for no gate (gates: {}, order: []) are read; other flow style is not
83
- if ((t == "{}" && sec == "gates") || (t == "[]" && sec == "order")) { sec = ""; next }
205
+ # the empty collections written for none (gates: {}, areas: {}, order: []) are read; other flow style is not
206
+ if ((t == "{}" && sec != "order") || (t == "[]" && sec == "order")) { sec = ""; next }
84
207
  if (t != "") fail("flow style")
85
208
  }
86
209
  next
87
210
  }
211
+ sec == "areas" && (/^ - / || /^ -$/ || /^ - / || /^ -$/) {
212
+ if (area == "") fail("a glob outside an area")
213
+ aglob[area, ++agc[area]] = item($0)
214
+ next
215
+ }
216
+ sec == "areas" && /^ [^ ]/ {
217
+ if (!match($0, /^ [^ :]+:/)) fail("an area that is not a name")
218
+ area = substr($0, 3, RLENGTH - 3); lind = ""
219
+ if (area in adef) fail("the area " area " twice")
220
+ adef[area] = 1; agc[area] = 0; anames[++nareas] = area
221
+ v = value(substr($0, RLENGTH + 1))
222
+ if (v != "") aglob[area, ++agc[area]] = v
223
+ next
224
+ }
225
+ sec == "areas" { fail("an area entry that is not a list item") }
88
226
  sec == "gates" && /^ [^ ]/ {
89
227
  if (!match($0, /^ [^ :]+:/)) fail("a gate that is not a name")
90
- name = substr($0, 3, RLENGTH - 3)
228
+ name = substr($0, 3, RLENGTH - 3); lst = ""; lind = ""
91
229
  if (word(substr($0, RLENGTH + 1)) != "") fail("flow style")
92
230
  if (name in seen) fail("the gate " name " twice")
93
231
  seen[name] = 1; names[++count] = name
94
232
  next
95
233
  }
234
+ sec == "gates" && lst != "" && (/^ - / || /^ -$/ || /^ - / || /^ -$/) { addto(lst, item($0)); next }
235
+ sec == "gates" && (/^ +- / || /^ +-$/) { fail("a list item outside areas or on") }
96
236
  sec == "gates" && /^ [^ ]/ {
97
237
  if (name == "") fail("a key outside a gate")
98
238
  key = $0; sub(/^ +/, "", key); sub(/:.*/, "", key)
99
- t = $0; sub(/^ *[^:]*:/, "", t)
239
+ t = $0; sub(/^ *[^:]*:/, "", t); lst = ""; lind = ""
100
240
  if (key == "cmd") { cmd[name] = value(t); if (cmd[name] == "") fail("an empty cmd") }
101
241
  else if (key == "optional") { if (word(t) == "true") opt[name] = 1 }
242
+ else if (key == "areas" || key == "on") {
243
+ if ((name, key) in had) fail("the key " key " twice in the gate " name)
244
+ had[name, key] = 1
245
+ v = value(t)
246
+ if (v == "") lst = key
247
+ else addto(key, v)
248
+ }
102
249
  else value(t)
103
250
  next
104
251
  }
105
252
  sec == "gates" { fail("a multi-line value") }
106
253
  sec == "order" && /^ - / { o = $0; sub(/^ - +/, "", o); order[++ocount] = word(o); next }
107
254
  sec == "order" { fail("an order entry that is not a list item") }
255
+ function say(s) { print "= " s }
108
256
  END {
109
257
  if (bad) exit 3
110
258
  # an empty or unrelated file is no gates.yaml: it must not pass without checks
111
259
  if (!hasGates) { print "line " NR ": no gates section"; exit 3 }
112
260
  for (k = 1; k <= count; k++) if (!(names[k] in cmd)) { print "the gate " names[k] " has no cmd"; exit 3 }
113
- # order present, even empty, decides: order: [] runs nothing
114
- if (hasOrder) { for (k = 1; k <= ocount; k++) if ((order[k] in seen) && !(order[k] in opt)) print cmd[order[k]] }
115
- else for (k = 1; k <= count; k++) if (!(names[k] in opt)) print cmd[names[k]]
261
+ for (k = 1; k <= count; k++) for (i = 1; i <= gac[names[k]]; i++)
262
+ if (!(garea[names[k], i] in adef)) { print "the gate " names[k] " names the area " garea[names[k], i] ", which areas: does not define"; exit 3 }
263
+ hook = (stage == "pre-commit" || stage == "pre-push")
264
+ filter = hook && known == "yes"
265
+ if (filter) {
266
+ while ((getline f < cfile) > 0) if (f != "") files[++nf] = f
267
+ close(cfile)
268
+ hit = ""
269
+ for (q = 1; q <= nareas; q++) {
270
+ a = anames[q]
271
+ for (i = 1; i <= agc[a] && !(a in touched); i++) {
272
+ re = globre(aglob[a, i])
273
+ for (j = 1; j <= nf; j++) if (files[j] ~ re) { touched[a] = 1; break }
274
+ }
275
+ if (a in touched) hit = hit (hit == "" ? "" : ", ") a
276
+ }
277
+ say("changed: " (nf + 0) " file" (nf == 1 ? "" : "s") ", areas touched: " (hit == "" ? "none" : hit))
278
+ } else if (hook) say("changed: not known (" why "), every area counts")
279
+ else say("changed: not looked at, stage " stage " runs every gate")
280
+ # the gates to run, in order: order when present (even empty: order: [] runs nothing), else gates
281
+ m = 0
282
+ if (hasOrder) {
283
+ for (k = 1; k <= ocount; k++) if (order[k] in seen) { todo[++m] = order[k]; listed[order[k]] = 1 }
284
+ for (k = 1; k <= count; k++) if (!(names[k] in listed)) say("skip " names[k] ": not in order")
285
+ } else for (k = 1; k <= count; k++) todo[++m] = names[k]
286
+ req = 0; ran = 0
287
+ for (k = 1; k <= m; k++) {
288
+ n = todo[k]
289
+ if (n in opt) { say("skip " n ": optional"); continue }
290
+ req++
291
+ if (hook && goc[n] > 0) {
292
+ ok = 0
293
+ for (i = 1; i <= goc[n]; i++) if (gon[n, i] == stage) ok = 1
294
+ if (!ok) { say("skip " n ": not on " stage); continue }
295
+ }
296
+ if (gac[n] == 0) why2 = "always"
297
+ else if (!hook) why2 = "stage " stage " runs every gate"
298
+ else if (!filter) why2 = "the changed files are not known"
299
+ else {
300
+ hits = ""; every = ""
301
+ for (i = 1; i <= gac[n]; i++) {
302
+ every = every (every == "" ? "" : ", ") garea[n, i]
303
+ if (garea[n, i] in touched) hits = hits (hits == "" ? "" : ", ") garea[n, i]
304
+ }
305
+ if (hits == "") { say("skip " n ": no change in " every); continue }
306
+ why2 = "changed in " hits
307
+ }
308
+ say("run " n ": " why2)
309
+ print "> " cmd[n]
310
+ ran++
311
+ }
312
+ if (req == 0) say("no required gate")
313
+ else if (ran == 0) say("no gate runs for this change")
116
314
  }
117
315
  `;
118
316
  const quote = (s) => `'${s.replaceAll("'", `'\\''`)}'`;
119
- /** The script. It holds no command: it reads gates.yaml every time it runs (024 S-15–S-17). */
317
+ /** The script. It holds no command: it reads gates.yaml every time it runs (024 S-15–S-17, spec 025 D-1). */
120
318
  export function renderGateScript() {
121
319
  return `#!/bin/sh
122
320
  # generated by reins — do not edit; \`reins update\` rewrites it. The gates are in .agents/harness/gates.yaml.
123
321
  #
124
- # Reads .agents/harness/gates.yaml, runs its required checks in order and keeps their output in
322
+ # Reads .agents/harness/gates.yaml, runs the checks this stage needs in order and keeps their output in
125
323
  # ${GATE_LOGS}/<tree>.log, where <tree> is the tree hash of what is checked: \`git rev-parse <commit>^{tree}\`
126
324
  # names a commit's log. gates.yaml is read in the shape reins writes; another shape stops the run (exit 2).
325
+ # pre-commit and pre-push run a gate only when a changed file is in one of its areas (a gate without areas
326
+ # always runs) and the stage is in its \`on\`; when the changed files cannot be told, every area counts. Any
327
+ # other stage runs every required gate. The log says which gates ran and which were skipped, and why.
127
328
  # Usage: sh ${GATE_SCRIPT} <stage> (pre-commit, pre-push, audit, or any name of your own)
128
329
  stage="\${1:-manual}"
129
330
  root=$(git rev-parse --show-toplevel 2>/dev/null) || {
@@ -152,6 +353,12 @@ dir=${GATE_LOGS}
152
353
  mkdir -p "$dir" || exit 2
153
354
  log="$dir/$tree.log"
154
355
  status="$dir/.status.$$"
356
+ names="$dir/.names.$$"
357
+ changed="$dir/.changed.$$"
358
+ input="$dir/.stdin.$$"
359
+ trap 'rm -f "$status" "$names" "$changed" "$input"' 0
360
+ trap 'exit 130' INT
361
+ trap 'exit 143' TERM
155
362
  printf '== %s stage: %s worktree: %s\\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$stage" "$worktree" >>"$log"
156
363
  stop() {
157
364
  echo "run-gates: $1" >&2
@@ -160,13 +367,74 @@ stop() {
160
367
  }
161
368
  gates=.agents/harness/gates.yaml
162
369
  [ -f "$gates" ] || stop "no $gates: nothing is checked without it"
163
- cmds=$(awk ${quote(AWK)} "$gates") || stop "$gates $cmds"
370
+ # The changed files (spec 025 D-1): pre-commit, the index against HEAD; pre-push, each pushed ref against
371
+ # the remote's (a new branch from its merge base with the default branch, a deletion none). known=no when
372
+ # they cannot be told: every area counts then.
373
+ known=no
374
+ why=
375
+ diffnames() { git diff --name-only --no-renames -z "$@" >>"$names" 2>/dev/null; }
376
+ # the merge base of a new branch: with the remote's default branch, else main or master (not the commit itself)
377
+ mergebase() {
378
+ for b in "refs/remotes/\${2:-origin}/HEAD" "refs/remotes/\${2:-origin}/main" "refs/remotes/\${2:-origin}/master" refs/heads/main refs/heads/master; do
379
+ t=$(git rev-parse --verify --quiet "$b^{commit}") || continue
380
+ [ "$t" != "$1" ] || continue
381
+ git merge-base "$t" "$1" 2>/dev/null && return 0
382
+ done
383
+ return 1
384
+ }
385
+ : >"$names"
386
+ case "$stage" in
387
+ pre-commit)
388
+ if diffnames --cached; then known=yes; else why="git diff --cached failed"; fi
389
+ ;;
390
+ pre-push)
391
+ # the hook's stdin is read once here and given to every gate, as it was before
392
+ if [ -t 0 ]; then
393
+ why="no refs: stdin is a terminal"
394
+ else
395
+ cat >"$input"
396
+ known=yes
397
+ refs=0
398
+ while read -r lref lsha rref rsha extra; do
399
+ [ -n "$lref" ] || continue
400
+ refs=$((refs + 1))
401
+ if [ -z "$rsha" ] || [ -n "$extra" ]; then known=no; why="a ref line git does not write"; break; fi
402
+ case "$lsha" in *[!0]*) ;; *) continue ;; esac
403
+ case "$rsha" in
404
+ *[!0]*) base=$rsha ;;
405
+ *) base=$(mergebase "$lsha" "$2") || { known=no; why="no merge base for the new branch $lref"; break; } ;;
406
+ esac
407
+ git cat-file -e "$base^{commit}" 2>/dev/null && diffnames "$base" "$lsha" || {
408
+ known=no
409
+ why="cannot compare $rref with $lref"
410
+ break
411
+ }
412
+ done <"$input"
413
+ if [ "$known" = yes ] && [ "$refs" -eq 0 ]; then known=no; why="no refs on stdin"; fi
414
+ fi
415
+ ;;
416
+ esac
417
+ if [ "$known" = yes ]; then
418
+ # a name with a newline cannot be one line: then the files are not known
419
+ if [ "$(tr -cd '\\n' <"$names" | wc -c | tr -cd 0-9)" != 0 ]; then
420
+ known=no
421
+ why="a file name with a newline"
422
+ else
423
+ tr '\\000' '\\n' <"$names" >"$changed"
424
+ # a change to the gates themselves is checked by every gate, so narrowing an area cannot skip one
425
+ if grep -qx -e '.agents/harness/gates.yaml' -e '.agents/harness/run-gates.sh' "$changed"; then
426
+ known=no
427
+ why="the gates themselves changed"
428
+ fi
429
+ fi
430
+ fi
431
+ plan=$(awk -v stage="$stage" -v known="$known" -v why="$why" -v cfile="$changed" ${quote(GATE_AWK)} "$gates") || stop "$gates $plan"
164
432
  code=0
433
+ say() { printf '%s\\n' "$1" | tee -a "$log"; }
165
434
  run() {
166
- [ "$code" -eq 0 ] || return 0
167
435
  printf '$ %s\\n' "$1" | tee -a "$log"
168
436
  {
169
- sh -c "$1" 2>&1
437
+ if [ -f "$input" ]; then sh -c "$1" <"$input" 2>&1; else sh -c "$1" 2>&1; fi
170
438
  echo $? >"$status"
171
439
  } | tee -a "$log"
172
440
  code=$(cat "$status" 2>/dev/null || echo 1)
@@ -174,12 +442,22 @@ run() {
174
442
  # output that did not end its last line must not run into the next record
175
443
  if [ -n "$(tail -c 1 "$log")" ]; then echo >>"$log"; fi
176
444
  }
177
- if [ -z "$cmds" ]; then echo "no required gate" >>"$log"; fi
178
- # the commands come on fd 3, so a gate still reads the hook's own stdin
445
+ # the plan comes on fd 3, so a gate still reads the hook's own stdin
446
+ gate=
179
447
  while IFS= read -r c <&3; do
180
- if [ -n "$c" ]; then run "$c"; fi
448
+ case "$c" in
449
+ '> '*)
450
+ if [ "$code" -eq 0 ]; then run "\${c#> }"; else say "skip $gate: an earlier gate failed"; fi
451
+ ;;
452
+ '= run '*)
453
+ gate=\${c#= run }
454
+ gate=\${gate%%:*}
455
+ if [ "$code" -eq 0 ]; then say "\${c#= }"; fi
456
+ ;;
457
+ '= '*) say "\${c#= }" ;;
458
+ esac
181
459
  done 3<<GATES
182
- $cmds
460
+ $plan
183
461
  GATES
184
462
  printf 'exit: %s\\n' "$code" >>"$log"
185
463
  exit "$code"
@@ -4,10 +4,11 @@ import { workTreeReader } from './reader.js';
4
4
  import { resolveRoles } from './bindings.js';
5
5
  import { agentName, claudeAgents } from './agents-md.js';
6
6
  import { splitFrontmatter } from './skills.js';
7
+ import { profileOf } from './profile.js';
7
8
  import { GATE_SCRIPT, renderGateScript } from './gate-script.js';
8
9
  /**
9
10
  * Files generated from `.agents/harness/` (design §6): `.claude/agents/rs-<group>-<role>.md` for roles
10
- * switched on in multi mode on a Claude model, the `.claude/skills/` copy, `.claude/settings.json` deny
11
+ * switched on in multi mode on a Claude model (profile full only), the `.claude/skills/` copy, `.claude/settings.json` deny
11
12
  * rules, and the gate script `.agents/harness/run-gates.sh` from `gates.yaml`. Generated from the committed team file only, so no personal override makes drift.
12
13
  */
13
14
  export const MANIFEST = '.agents/harness/generated.yaml';
@@ -91,8 +92,41 @@ export function forcePushRules() {
91
92
  'Bash(git commit * -n *)',
92
93
  ];
93
94
  }
94
- /** C4 patterns → Claude Code deny rules. A pattern that would also hide an allowed template (`.env.*` vs `.env.example`) is narrowed. */
95
- export function denyRules(lines) {
95
+ /**
96
+ * Merging and releasing, which a person does (spec 025 R-3, D-4): each C6 command alone or with arguments,
97
+ * and `git push` to the default branch — `<remote> <branch>`, any `<src>:<branch>` (`HEAD:<branch>`
98
+ * included) and `<src>:refs/heads/<branch>`, with any flags before. A work branch push is not matched: the
99
+ * branch name must be the whole last word, or the part after a colon. Globs over the command string, so
100
+ * `git -C`, variables, aliases, scripts and a bare `git push` on the default branch get past (design §6).
101
+ */
102
+ export function mergeReleaseRules(commands, defaults) {
103
+ const rules = [];
104
+ for (const c of commands)
105
+ rules.push(`Bash(${c})`, `Bash(${c} *)`);
106
+ for (const b of defaults)
107
+ rules.push(`Bash(git push * ${b})`, `Bash(git push * ${b} *)`, `Bash(git push *:${b})`, `Bash(git push *:${b} *)`, `Bash(git push *:refs/heads/${b})`, `Bash(git push *:refs/heads/${b} *)`);
108
+ return rules;
109
+ }
110
+ /**
111
+ * Pushing tags, which releases (spec 025 R-3): `--tags` and `--follow-tags` anywhere after `git push`, and a
112
+ * `refs/tags/` refspec as its own word or after a colon. A tag pushed by its bare name (`git push origin
113
+ * v1.0`) looks like a branch and gets past, as do `git -C`, variables and aliases (design §6).
114
+ */
115
+ export function tagPushRules() {
116
+ const rules = [];
117
+ for (const flag of ['--tags', '--follow-tags'])
118
+ rules.push(`Bash(git push ${flag})`, `Bash(git push ${flag} *)`, `Bash(git push * ${flag})`, `Bash(git push * ${flag} *)`);
119
+ rules.push('Bash(git push * refs/tags/*)', 'Bash(git push *:refs/tags/*)');
120
+ return rules;
121
+ }
122
+ /** When install could not tell the default branch, both usual names are covered. */
123
+ export const UNKNOWN_DEFAULT = ['main', 'master'];
124
+ /**
125
+ * Red lines → Claude Code deny rules: C4 patterns (a pattern that would also hide an allowed template,
126
+ * `.env.*` vs `.env.example`, is narrowed), force push and hook bypass, and C6 merging and releasing
127
+ * (its commands, pushes to the default branch, tag pushes).
128
+ */
129
+ export function denyRules(lines, defaults = UNKNOWN_DEFAULT) {
96
130
  const c4 = lines.find((r) => r.id === 'C4');
97
131
  const globs = new Set();
98
132
  for (const p of c4?.patterns ?? []) {
@@ -115,6 +149,9 @@ export function denyRules(lines) {
115
149
  rules.push(`${t}(${g.startsWith('**/') ? g : `**/${g}`})`);
116
150
  // force push and hook bypass: the user runs these, never an agent (force-with-lease: per person)
117
151
  rules.push(...forcePushRules());
152
+ // merging and releasing: a person does these (spec 025 R-3)
153
+ rules.push(...mergeReleaseRules(lines.find((r) => r.id === 'C6')?.commands ?? [], defaults));
154
+ rules.push(...tagPushRules());
118
155
  return rules;
119
156
  }
120
157
  /** Everything the generator owns, computed from the installed harness. Pure. */
@@ -126,7 +163,8 @@ export function generateVendorFiles(root, rd = workTreeReader(root)) {
126
163
  return out;
127
164
  const text = rd.read(`${harness}/bindings.yaml`);
128
165
  const doc = text ? parse(text) : null;
129
- if (doc?.schema === 'reins/bindings@2') {
166
+ // the rs-* agents come with the full profile only (spec 025 D-5); light calls a plain subagent of the model
167
+ if (doc?.schema === 'reins/bindings@2' && profileOf(doc) === 'full') {
130
168
  const groups = new Map();
131
169
  for (const r of resolveRoles(doc).filter((x) => x.on)) {
132
170
  const agents = claudeAgents(r);
@@ -174,11 +212,15 @@ export function generateVendorFiles(root, rd = workTreeReader(root)) {
174
212
  });
175
213
  const redText = rd.read(`${harness}/policy/common-red-lines.yaml`);
176
214
  if (redText !== undefined) {
177
- const deny = denyRules(parse(redText).rules);
215
+ // the default branch install read (install.yaml), else both usual names
216
+ const pin = rd.read(`${harness}/install.yaml`);
217
+ const branch = pin ? parse(pin)?.default_branch : null;
218
+ const defaults = typeof branch === 'string' && branch ? [branch] : UNKNOWN_DEFAULT;
219
+ const deny = denyRules(parse(redText).rules, defaults);
178
220
  out.writes.push({
179
221
  path: '.claude/settings.json',
180
222
  content: `${JSON.stringify({ $schema: SETTINGS_SCHEMA, permissions: { deny } }, null, 2)}\n`,
181
- why: 'secrets, force push, hook bypass',
223
+ why: 'secrets, force push, hook bypass, merging and releasing',
182
224
  deny,
183
225
  });
184
226
  }
@@ -24,6 +24,39 @@ export function currentBranch(cwd) {
24
24
  }
25
25
  }
26
26
  }
27
+ /**
28
+ * The default branch, read at install (design §6): `origin/HEAD`, else the one of `main` and `master` that
29
+ * exists as a local branch, else — in a repository without commits — the branch HEAD names, when it is one of
30
+ * them. Null when it cannot tell; the deny rules then cover both names.
31
+ */
32
+ export function defaultBranch(cwd) {
33
+ try {
34
+ const ref = git(['symbolic-ref', '--quiet', 'refs/remotes/origin/HEAD'], cwd);
35
+ const name = ref.replace(/^refs\/remotes\/origin\//, '');
36
+ if (name && name !== ref)
37
+ return name;
38
+ }
39
+ catch {
40
+ // no remote HEAD: look at the local branches
41
+ }
42
+ const local = ['main', 'master'].filter((b) => {
43
+ try {
44
+ git(['rev-parse', '--verify', '--quiet', `refs/heads/${b}`], cwd);
45
+ return true;
46
+ }
47
+ catch {
48
+ return false;
49
+ }
50
+ });
51
+ if (local.length === 1)
52
+ return local[0];
53
+ if (local.length === 0) {
54
+ const head = currentBranch(cwd);
55
+ if (head === 'main' || head === 'master')
56
+ return head;
57
+ }
58
+ return null;
59
+ }
27
60
  export function isAncestor(ancestor, descendant, cwd) {
28
61
  try {
29
62
  execFileSync('git', ['merge-base', '--is-ancestor', ancestor, descendant], {
@@ -108,7 +108,7 @@ function planTickets(root, ai, files, moves) {
108
108
  from: it.path,
109
109
  to: '',
110
110
  action: 'ticket',
111
- why: 'open work item → intent 000 plan',
111
+ why: 'open work item → the migrated intent note',
112
112
  group: it.group,
113
113
  };
114
114
  if (was)