@krysoworks/kryso 0.0.0-stage → 2.0.0-rc.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 (162) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +152 -2
  3. package/bin/kryso +31 -0
  4. package/bin/ship-it-status +14 -0
  5. package/bin/ship-ledger +14 -0
  6. package/bin/ship-loop +14 -0
  7. package/bin/ship-pass +14 -0
  8. package/bin/warden +14 -0
  9. package/dist/answer.d.ts +38 -0
  10. package/dist/answer.js +48 -0
  11. package/dist/bytes.d.ts +5 -0
  12. package/dist/bytes.js +35 -0
  13. package/dist/cleanup.d.ts +1 -0
  14. package/dist/cleanup.js +629 -0
  15. package/dist/commands.d.ts +48 -0
  16. package/dist/commands.js +293 -0
  17. package/dist/completions.d.ts +24 -0
  18. package/dist/completions.js +239 -0
  19. package/dist/daemon.d.ts +15 -0
  20. package/dist/daemon.js +23 -0
  21. package/dist/diagnose.d.ts +47 -0
  22. package/dist/diagnose.js +314 -0
  23. package/dist/doctor.d.ts +1 -0
  24. package/dist/doctor.js +241 -0
  25. package/dist/env.d.ts +9 -0
  26. package/dist/env.js +17 -0
  27. package/dist/evidence.d.ts +39 -0
  28. package/dist/evidence.js +327 -0
  29. package/dist/home.d.ts +3 -0
  30. package/dist/home.js +27 -0
  31. package/dist/install.d.ts +59 -0
  32. package/dist/install.js +137 -0
  33. package/dist/isolate.d.ts +28 -0
  34. package/dist/isolate.js +402 -0
  35. package/dist/leftovers.d.ts +5 -0
  36. package/dist/leftovers.js +156 -0
  37. package/dist/machine.d.ts +6 -0
  38. package/dist/machine.js +185 -0
  39. package/dist/palette.d.ts +147 -0
  40. package/dist/palette.js +167 -0
  41. package/dist/plan.d.ts +1 -0
  42. package/dist/plan.js +133 -0
  43. package/dist/proc.d.ts +23 -0
  44. package/dist/proc.js +23 -0
  45. package/dist/read.d.ts +1 -0
  46. package/dist/read.js +297 -0
  47. package/dist/readiness.d.ts +5 -0
  48. package/dist/readiness.js +64 -0
  49. package/dist/remote.d.ts +6 -0
  50. package/dist/remote.js +85 -0
  51. package/dist/self-update.d.ts +1 -0
  52. package/dist/self-update.js +224 -0
  53. package/dist/semver.d.ts +7 -0
  54. package/dist/semver.js +42 -0
  55. package/dist/style.d.ts +71 -0
  56. package/dist/style.js +187 -0
  57. package/dist/update.d.ts +47 -0
  58. package/dist/update.js +190 -0
  59. package/dist/work.d.ts +25 -0
  60. package/dist/work.js +449 -0
  61. package/dist/workflows.d.ts +4 -0
  62. package/dist/workflows.js +73 -0
  63. package/docker/entrypoint.sh +44 -0
  64. package/docker/runner.Dockerfile +39 -0
  65. package/package.json +65 -4
  66. package/scripts/install-skills.sh +607 -0
  67. package/skills/code-review/SKILL.md +231 -0
  68. package/skills/kryso/SKILL.md +107 -0
  69. package/skills/kryso/golden/help.rounded.ansi +23 -0
  70. package/skills/kryso/golden/help.square.ansi +23 -0
  71. package/skills/kryso/golden/help.txt +23 -0
  72. package/skills/kryso/golden/table-narrow.json +11 -0
  73. package/skills/kryso/golden/table-narrow.rounded.ansi +4 -0
  74. package/skills/kryso/golden/table-narrow.square.ansi +4 -0
  75. package/skills/kryso/golden/table.ansi +5 -0
  76. package/skills/kryso/golden/table.json +11 -0
  77. package/skills/kryso/ledger.sh +2146 -0
  78. package/skills/kryso/palette.json +57 -0
  79. package/skills/kryso/reference/resuming.md +30 -0
  80. package/skills/kryso/reference/routing.md +23 -0
  81. package/skills/kryso/reference/stage-1-claim.md +79 -0
  82. package/skills/kryso/reference/stage-2-build.md +133 -0
  83. package/skills/kryso/reference/stage-3-prove.md +51 -0
  84. package/skills/kryso/reference/stage-4-pr.md +76 -0
  85. package/skills/kryso/reference/stage-5-reconcile.md +81 -0
  86. package/skills/kryso/reference/stage-6-merge-or-park.md +73 -0
  87. package/skills/kryso/reference/stage-7-close-out.md +62 -0
  88. package/skills/kryso/reference/terminal.md +7 -0
  89. package/skills/kryso/ship-it-status.sh +1503 -0
  90. package/skills/kryso/ui.py +274 -0
  91. package/skills/kryso/ui.sh +609 -0
  92. package/skills/kryso-build/SKILL.md +168 -0
  93. package/skills/kryso-shift/SKILL.md +167 -0
  94. package/skills/kryso-shift/archive.py +2784 -0
  95. package/skills/kryso-shift/archive_checker.py +462 -0
  96. package/skills/kryso-shift/archive_checker_lambda.py +155 -0
  97. package/skills/kryso-shift/archive_health.py +364 -0
  98. package/skills/kryso-shift/archive_launchd.py +140 -0
  99. package/skills/kryso-shift/archive_remote.py +990 -0
  100. package/skills/kryso-shift/archiving-checker.md +140 -0
  101. package/skills/kryso-shift/archiving-keep.md +115 -0
  102. package/skills/kryso-shift/archiving-recovery.md +112 -0
  103. package/skills/kryso-shift/archiving-remote.md +166 -0
  104. package/skills/kryso-shift/archiving.md +146 -0
  105. package/skills/kryso-shift/coverage.schema.json +225 -0
  106. package/skills/kryso-shift/deadline.sh +254 -0
  107. package/skills/kryso-shift/feedback.schema.json +35 -0
  108. package/skills/kryso-shift/feedback.sh +228 -0
  109. package/skills/kryso-shift/history-manifest.schema.json +331 -0
  110. package/skills/kryso-shift/keep-after.sh +55 -0
  111. package/skills/kryso-shift/measure.sh +611 -0
  112. package/skills/kryso-shift/merge-ready.sh +207 -0
  113. package/skills/kryso-shift/outcome.schema.json +16 -0
  114. package/skills/kryso-shift/pass-rules.md +8 -0
  115. package/skills/kryso-shift/profile.schema.json +42 -0
  116. package/skills/kryso-shift/reader.sh +692 -0
  117. package/skills/kryso-shift/reads.sh +151 -0
  118. package/skills/kryso-shift/reference/allowance.md +21 -0
  119. package/skills/kryso-shift/reference/driver.md +95 -0
  120. package/skills/kryso-shift/reference/measuring.md +67 -0
  121. package/skills/kryso-shift/reference/outputs.md +21 -0
  122. package/skills/kryso-shift/reference/pass-script.md +115 -0
  123. package/skills/kryso-shift/reference/reconcile-and-merge.md +55 -0
  124. package/skills/kryso-shift/reference/second-reader.md +9 -0
  125. package/skills/kryso-shift/reference/sweeps.md +83 -0
  126. package/skills/kryso-shift/repair.schema.json +14 -0
  127. package/skills/kryso-shift/report-template.html +679 -0
  128. package/skills/kryso-shift/report.py +389 -0
  129. package/skills/kryso-shift/report_fixture.py +65 -0
  130. package/skills/kryso-shift/reporting.md +58 -0
  131. package/skills/kryso-shift/result.sh +91 -0
  132. package/skills/kryso-shift/runner.sh +1178 -0
  133. package/skills/kryso-shift/ship-loop.sh +3977 -0
  134. package/skills/kryso-shift/ship-pass.sh +4987 -0
  135. package/skills/kryso-shift/steps.jq +19 -0
  136. package/skills/kryso-shift/workflow.mjs +830 -0
  137. package/skills/kryso-shift/workflows/default.json +22 -0
  138. package/skills/kryso-shift/workflows/ralph.json +13 -0
  139. package/skills/kryso-watch/SKILL.md +100 -0
  140. package/skills/kryso-watch/answer.schema.json +23 -0
  141. package/skills/kryso-watch/base-refs.sh +44 -0
  142. package/skills/kryso-watch/board.sh +58 -0
  143. package/skills/kryso-watch/reference/config.md +23 -0
  144. package/skills/kryso-watch/reference/outputs.md +30 -0
  145. package/skills/kryso-watch/reference/report.md +29 -0
  146. package/skills/kryso-watch/reference/scheduler.md +32 -0
  147. package/skills/kryso-watch/reference/settlement.md +29 -0
  148. package/skills/kryso-watch/reference/shift.md +44 -0
  149. package/skills/kryso-watch/reference/shim-limits.md +22 -0
  150. package/skills/kryso-watch/reference/triage.md +26 -0
  151. package/skills/kryso-watch/schema.jq +40 -0
  152. package/skills/kryso-watch/settle.schema.json +36 -0
  153. package/skills/kryso-watch/stage-common.sh +187 -0
  154. package/skills/kryso-watch/stages/report.sh +525 -0
  155. package/skills/kryso-watch/stages/settle.sh +722 -0
  156. package/skills/kryso-watch/stages/shift.sh +466 -0
  157. package/skills/kryso-watch/stages/triage.sh +379 -0
  158. package/skills/kryso-watch/tracker.sh +27 -0
  159. package/skills/kryso-watch/triage.schema.json +77 -0
  160. package/skills/kryso-watch/warden.launchd.plist +50 -0
  161. package/skills/kryso-watch/warden.schema.json +98 -0
  162. package/skills/kryso-watch/warden.sh +1004 -0
@@ -0,0 +1,40 @@
1
+ # schema.jq: the subset of JSON Schema the warden's schemas use, read off the schema file so
2
+ # each schema stays the one source. Run with the document as input and the schema as
3
+ # --slurpfile schema. Prints the first failure as "<path>: <what>", or nothing when the
4
+ # document matches. The config and every triage outcome go through this one reader.
5
+ def kind: if type == "number" and . == floor then "integer" else type end;
6
+ def fits($want): ($want == kind) or ($want == "number" and type == "number");
7
+ def check($s; $p):
8
+ . as $v
9
+ | (if ($s | has("type")) then [$s.type] | flatten else [] end) as $types
10
+ | if ($types | length) > 0 and ([$types[] as $t | $v | fits($t)] | any | not) then
11
+ "\($p): must be \($types | join(" or ")), got \($v | type)"
12
+ elif ($s | has("enum")) and ([$s.enum[] | . == $v] | any | not) then
13
+ "\($p): must be one of \($s.enum | map(tojson) | join(", ")), got \($v | tojson)"
14
+ else
15
+ (if ($v | type) == "number" and ($s | has("minimum")) and $v < $s.minimum then
16
+ "\($p): must be at least \($s.minimum)" else empty end),
17
+ (if ($v | type) == "number" and ($s | has("maximum")) and $v > $s.maximum then
18
+ "\($p): must be at most \($s.maximum)" else empty end),
19
+ (if ($v | type) == "string" and ($s | has("minLength")) and ($v | length) < $s.minLength then
20
+ "\($p): must be at least \($s.minLength) character(s)" else empty end),
21
+ (if ($v | type) == "array" then
22
+ (if ($s | has("minItems")) and ($v | length) < $s.minItems then
23
+ "\($p): must have at least \($s.minItems) item(s)" else empty end),
24
+ (if ($s.uniqueItems // false) and ($v | unique | length) != ($v | length) then
25
+ "\($p): must not repeat an item" else empty end),
26
+ (if ($s | has("items")) then
27
+ range(0; $v | length) as $i | $v[$i] | check($s.items; "\($p)/\($i)") else empty end)
28
+ else empty end),
29
+ (if ($v | type) == "object" then
30
+ (($s.required // [])[] as $k | select($v | has($k) | not) | "\($p)/\($k): is required"),
31
+ ($v | to_entries[] as $e
32
+ | if (($s.properties // {}) | has($e.key)) then $e.value | check($s.properties[$e.key]; "\($p)/\($e.key)")
33
+ elif $s.additionalProperties == false then "\($p)/\($e.key): is not allowed"
34
+ elif ($s.additionalProperties | type) == "object" then $e.value | check($s.additionalProperties; "\($p)/\($e.key)")
35
+ else empty end),
36
+ (if ($s | has("propertyNames")) then
37
+ $v | keys[] as $k | $k | check($s.propertyNames; "\($p)/\($k)") else empty end)
38
+ else empty end)
39
+ end;
40
+ [check($schema[0]; "")] | first // empty
@@ -0,0 +1,36 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/krysoworks/kryso/skills/kryso-watch/settle.schema.json",
4
+ "title": "the outcome one settlement context returns for one issue read against a commit range",
5
+ "description": "The context requests, the driver authorises. A shipped verdict closes the issue only when every acceptance criterion appears in the evidence and every ref resolves to a commit or merged pull request on the base. A superseded verdict goes to the decision report. An open verdict changes nothing.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["verdict", "evidence", "unmet", "reason"],
9
+ "properties": {
10
+ "verdict": {
11
+ "description": "shipped: the base satisfies every acceptance criterion. superseded: the base took another route and the issue no longer applies. open: something the issue asks for is still missing.",
12
+ "type": "string",
13
+ "enum": ["shipped", "superseded", "open"]
14
+ },
15
+ "evidence": {
16
+ "description": "One row per acceptance criterion the base satisfies, naming the criterion word for word as the issue body lists it, or by its number, and the commit or merged pull request that satisfies it.",
17
+ "type": "array",
18
+ "items": {
19
+ "type": "object",
20
+ "additionalProperties": false,
21
+ "required": ["criterion", "kind", "ref"],
22
+ "properties": {
23
+ "criterion": { "description": "The criterion as the issue body lists it, or its 1-based number.", "type": "string", "minLength": 1 },
24
+ "kind": { "type": "string", "enum": ["commit", "pr"] },
25
+ "ref": { "description": "The commit sha, or the pull request number.", "type": "string", "minLength": 1 }
26
+ }
27
+ }
28
+ },
29
+ "unmet": {
30
+ "description": "The acceptance criteria the base does not satisfy, word for word. Empty for a shipped verdict.",
31
+ "type": "array",
32
+ "items": { "type": "string" }
33
+ },
34
+ "reason": { "description": "One paragraph: what was read and why the verdict follows.", "type": "string", "minLength": 1 }
35
+ }
36
+ }
@@ -0,0 +1,187 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck disable=SC2034 # the markers are read by the stage bodies that source this file
3
+ # stage-common.sh: what the stage bodies share. Sourced, never run, by each body under stages/
4
+ # after it has sourced tracker.sh. It defines the markers the warden leaves on issues, the
5
+ # triage role mapping, the reading of a context's outcome, the unsettled range, the
6
+ # exclusions every stage applies, the state label edit and the report items, and the one way
7
+ # an issue body's acceptance criteria are rewritten, so every stage reads and writes them alike.
8
+
9
+ # The stem every agent's comment header shares, whatever procedure it names after it, so a
10
+ # read that asks whether a person wrote a comment matches this and not the full disclaimer.
11
+ # The playbook's https://github.com/jv-k/engineering-playbook/blob/main/conventions/agent-friendly-repos.md has the rule.
12
+ AGENT_HEADER_STEM='> *This was generated by AI during '
13
+ # The triage skill's disclaimer, the stem naming triage. Every comment a stage posts on an
14
+ # issue starts with it, and the last comment that does is the last triage note a needs-info
15
+ # issue's reporter answers.
16
+ DISCLAIMER="${AGENT_HEADER_STEM}triage.*"
17
+ # The markers a later watch reads back. A parked issue's last parking or release marker says
18
+ # whether it still waits on the report. Every comment the warden posts on the report carries
19
+ # the report marker, which is how a person's answers are told from the warden's own comments.
20
+ PARK_MARKER='<!-- warden:parked'
21
+ RELEASE_MARKER='<!-- warden:released'
22
+ REPORT_MARKER='<!-- warden:report'
23
+ # The report's hand-off of a confirmed out-of-scope match: settlement closes an open issue
24
+ # whose last such comment carries it, citing the file it names. It is neither a parking nor a
25
+ # release marker, so the issue stays parked for triage and the shift until settlement closes it.
26
+ CONFIRM_MARKER='<!-- warden:wontfix-confirmed'
27
+ # Every marker is written with the warden: prefix, and a later release may write kryso-watch:
28
+ # instead. markers_read <json> is a tracker read with each <!-- kryso-watch:… marker read as the
29
+ # <!-- warden:… one, so every rule that matches a marker matches either prefix.
30
+ markers_read() { jq -c 'walk(if type == "string" then gsub("<!-- kryso-watch:"; "<!-- warden:") else . end)' <<< "$1"; }
31
+ # The option a ledger item's confirmation answers with: triage writes it, the report reads it.
32
+ CLOSE_OPTION='close it as wontfix'
33
+ ROLES=(needs-triage needs-info ready-for-agent ready-for-human wontfix)
34
+
35
+ # role_map <repo-root>: the five canonical roles mapped to the label strings the repository's
36
+ # triage-labels doc gives, as one JSON object. A doc row whose first two cells are a
37
+ # backticked role and a backticked label maps it. A role the doc leaves out, or a repository
38
+ # with no doc, uses the role's own name.
39
+ role_map() {
40
+ local map doc="$1/docs/agents/triage-labels.md" line row_re
41
+ map="$(jq -cn '$ARGS.positional | map({key: ., value: .}) | from_entries' --args "${ROLES[@]}")"
42
+ if [[ -f "$doc" ]]; then
43
+ # shellcheck disable=SC2016 # the backticks are the doc's, not a command substitution
44
+ row_re='^\|[[:space:]]*`([^`]+)`[[:space:]]*\|[[:space:]]*`([^`]+)`[[:space:]]*\|'
45
+ while IFS= read -r line; do
46
+ [[ "$line" =~ $row_re ]] || continue
47
+ map="$(jq -c --arg r "${BASH_REMATCH[1]}" --arg l "${BASH_REMATCH[2]}" 'if has($r) then .[$r] = $l else . end' <<< "$map")"
48
+ done < "$doc"
49
+ fi
50
+ printf '%s' "$map"
51
+ }
52
+
53
+ # comment_file <file> <text>: a comment body on disk, the disclaimer first unless it already is.
54
+ comment_file() {
55
+ local f="$1" text="$2"
56
+ { echo "$DISCLAIMER"; echo; printf '%s\n' "$text"; } > "$f"
57
+ [[ "$text" != "$DISCLAIMER"* ]] || printf '%s\n' "$text" > "$f"
58
+ }
59
+
60
+ # outcome_of <host-result> <required-key>: the outcome object, from the structured output or,
61
+ # where the host wrote it into its text, the first fenced JSON object that carries the key.
62
+ outcome_of() {
63
+ local block
64
+ block="$(jq -c '.structured_output | select(type == "object")' "$1" 2>/dev/null)"
65
+ if [[ -z "$block" ]]; then
66
+ block="$(jq -r '.result // ""' "$1" 2>/dev/null | tr -d '\r' \
67
+ | awk '/^```(json)?$/{f=!f; next} f' | jq -c --arg k "$2" 'select(type == "object" and has($k))' 2>/dev/null | head -1)"
68
+ fi
69
+ printf '%s' "$block"
70
+ }
71
+
72
+ # ---- the unsettled range -----------------------------------------------------------------
73
+ #
74
+ # The range settlement reads, from the settled sha in the state file to the fetched base. It
75
+ # is read here once, so every stage that reads it reads the same range the same way.
76
+
77
+ # range_start <base> <now> <settled-json>: where the unsettled range starts. Prints the settled
78
+ # sha and returns 0 when there is a range. Returns 1 with the reason when there is no usable
79
+ # start: no settled sha yet, one on another branch, or one this repository does not have.
80
+ # Returns 2 with the reason when the base has not moved past it.
81
+ range_start() {
82
+ local base="$1" now="$2" sha branch
83
+ sha="$(jq -r '.sha // empty' <<< "$3" 2>/dev/null)"
84
+ branch="$(jq -r '.branch // empty' <<< "$3" 2>/dev/null)"
85
+ [[ -n "$sha" ]] || { echo "no settled sha for $base yet"; return 1; }
86
+ [[ "$branch" == "$base" ]] || { echo "the settled sha is on '$branch', not '$base'"; return 1; }
87
+ git cat-file -e "$sha^{commit}" 2>/dev/null || { echo "the settled sha ${sha:0:12} is not in this repository"; return 1; }
88
+ [[ "$sha" != "$now" ]] || { echo "base $base unmoved since ${now:0:12}"; return 2; }
89
+ echo "$sha"
90
+ }
91
+
92
+ # range_commits <from> <to>: the commits from..to, oldest first, as one JSON array of {sha,
93
+ # subject, cites}, where cites are the issue numbers the message names as #<n>. Prints the
94
+ # reason and returns 1 when the range cannot be read.
95
+ range_commits() {
96
+ local out
97
+ out="$(git log --reverse --format='%H%x09%s%x09%B%x1e' "$1..$2" 2>&1)" \
98
+ || { echo "cannot read the commits ${1:0:12}..${2:0:12}: $(head -1 <<< "$out")"; return 1; }
99
+ jq -Rsc 'split("\u001e") | map(ltrimstr("\n") | select(length > 0) | split("\t")
100
+ | {sha: .[0], subject: .[1], cites: ([.[2:] | join("\t") | scan("#([0-9]+)") | .[0] | tonumber] | unique)})' <<< "$out"
101
+ }
102
+
103
+ # range_prs <base> <commits-json>: the pull requests merged into the base whose merge commit is
104
+ # one of the commits, as one JSON array of {number, url, title, sha, cites}, cites read off
105
+ # the title and body. The newest 200 merged into the base are read. Prints the reason and
106
+ # returns 1 when the list cannot be read.
107
+ range_prs() {
108
+ local out
109
+ out="$(gh pr list --state merged --base "$1" --limit 200 --json number,title,body,url,mergeCommit 2>&1)" \
110
+ || { echo "cannot list the merged pull requests: $(head -1 <<< "$out")"; return 1; }
111
+ jq -c --argjson commits "$2" '
112
+ if type != "array" then error("not an array") else . end
113
+ | ($commits | map(.sha)) as $shas
114
+ | [.[] | select((.mergeCommit.oid // "") as $o | $shas | index($o))
115
+ | {number, url, title, sha: .mergeCommit.oid, cites: ([(.title // ""), (.body // "")] | join("\n") | [scan("#([0-9]+)") | .[0] | tonumber] | unique)}]' <<< "$out" 2>/dev/null \
116
+ || { echo "the merged pull request list is not JSON"; return 1; }
117
+ }
118
+
119
+ # ---- the exclusions every stage shares ------------------------------------------------------
120
+ #
121
+ # EXCLUSIONS_JQ: jq definitions a stage body prepends to its candidate program, over one issue
122
+ # as `gh issue list` returns it. own_exclusion($log; $report; $deferred) is why the issue is
123
+ # the warden's own or Deferred, or null. parked_reason($marker; $released) is why it waits on
124
+ # the decision report, read off its last parking or release marker, or null.
125
+ # shellcheck disable=SC2016 # jq's own $variables, not the shell's
126
+ EXCLUSIONS_JQ='
127
+ def own_exclusion($log; $report; $deferred):
128
+ [.labels[]?.name] as $labels
129
+ | if ($labels | index($log)) then "the warden log"
130
+ elif ($labels | index($report)) then "the decision report"
131
+ elif ($labels | index($deferred)) then "deferred: carries the \($deferred) label"
132
+ elif ([.projectItems[]? | .status.name? // ""] | index("Deferred")) then "deferred: its card is in the Deferred column"
133
+ else null end;
134
+ def parked_reason($marker; $released):
135
+ ([(.comments // [])[] | select((.body // "") | contains($marker) or contains($released))] | last
136
+ | if . != null and (.body | contains($marker)) then . else null end) as $park
137
+ | if $park == null then null
138
+ else ([$park.body | capture("report=(?<r>[0-9]+) q=(?<q>[0-9,]+)")][0]) as $posted
139
+ | if $posted then "parked on report #\($posted.r) \($posted.q | split(",") | map("Q\(.)") | join(", "))"
140
+ else "parked on report item \(([$park.body | capture("items=(?<i>[^ ]+)")][0].i // "unknown") | split(",") | join(", "))" end
141
+ end;'
142
+
143
+ # ---- the writes every stage shares ----------------------------------------------------------
144
+
145
+ # state_edit <role-map-json> <held-labels-json> <target-label>: the label edit that moves an
146
+ # issue to the target state label, which replaces any other state label it holds. Leaves the
147
+ # arguments in STATE_ADD and STATE_REMOVE, both empty when it holds the target alone.
148
+ state_edit() {
149
+ local l
150
+ STATE_ADD=(); STATE_REMOVE=()
151
+ jq -e --arg t "$3" 'index($t)' <<< "$2" >/dev/null || STATE_ADD=(--add-label "$3")
152
+ while IFS= read -r l; do [[ -n "$l" ]] && STATE_REMOVE+=(--remove-label "$l"); done \
153
+ < <(jq -r --argjson map "$1" --arg t "$3" '[$map[]] as $s | .[] | select(. as $l | ($s | index($l)) and $l != $t)' <<< "$2")
154
+ }
155
+
156
+ # add_report_items <watch-dir> <items-json>: the items appended to the watch's
157
+ # report-items.json, where the decision report body finds the items a watch parked.
158
+ add_report_items() {
159
+ local f="$1/report-items.json" items='[]'
160
+ [[ ! -f "$f" ]] || items="$(jq -c 'if type == "array" then . else [] end' "$f" 2>/dev/null || echo '[]')"
161
+ jq --argjson i "$2" '. + $i' <<< "$items" > "$f"
162
+ }
163
+
164
+ # schema_reason <schema> <document>: why the document does not match the schema, or nothing.
165
+ schema_reason() {
166
+ local why
167
+ why="$(jq -r --slurpfile schema "$1" -f "$WARDEN_DIR/schema.jq" <<< "$2" 2>&1)" \
168
+ || { echo "the outcome cannot be validated: $(head -1 <<< "$why")"; return; }
169
+ [[ -z "$why" ]] || echo "the outcome does not match $(basename "$1"): $why"
170
+ }
171
+
172
+ # criteria_body <body> <criteria-json>: the body with its acceptance criteria section replaced
173
+ # by the criteria, one unticked box each, or appended where it has none. Every other section
174
+ # stays where it was.
175
+ criteria_body() {
176
+ jq -rn --arg body "$1" --argjson criteria "$2" '
177
+ ($criteria | map("- [ ] \(.)") | join("\n")) as $list
178
+ | ($body | split("\n")) as $lines
179
+ | ([$lines | to_entries[] | select(.value | test("^##[[:space:]]+Acceptance criteria[[:space:]]*$"; "i")) | .key] | first) as $at
180
+ | if $at == null then
181
+ "\($body | sub("\\s+$"; ""))\(if ($body | length) > 0 then "\n\n" else "" end)## Acceptance criteria\n\n\($list)"
182
+ else
183
+ ([$lines | to_entries[] | select(.key > $at and (.value | test("^#{1,2}[[:space:]]"))) | .key] | first // ($lines | length)) as $next
184
+ | ($lines[:$at] + ["## Acceptance criteria", ""] + ($list | split("\n"))
185
+ + (if $next < ($lines | length) then [""] + $lines[$next:] else [] end)) | join("\n")
186
+ end'
187
+ }