#!/usr/bin/env bash # The plugin root: the variable when the platform sets it (hooks), else this file's own place. PLUGIN_ROOT="${CLAUDE_PLUGIN_ROOT:-$(cd "$(dirname "$0")/../../.." && pwd -P)}" export CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT" # design-actions.sh: the deterministic half of the design skill (ideal/design.md). # # The skill body holds the conversation: which recipe to follow, how a work order is sized, which # criteria a proposed order should serve and own, and whether a person has confirmed a claimed # ownership. This script never decides any of that. Every fact it needs arrives already decided, # as an argument, the same split scope-actions.sh and research-actions.sh use. It writes one file # per work order under /design/, mints an id, renders that file's markdown, and runs # the design check. Deciding whether design is done belongs to whoever calls this, never to this # script. # # `close` also moves each finished critique file from /records/, which the project # ignores, into /design/, which it commits. It records the new paths, their finding # count, and the outcome line --critique-outcome passed, refused unattended. Without one, a re-close # over the same critique carries the last close's answer, else `none`. # # What reaches stdout is what reaches the orchestrator's context. Every action prints `key: value` # summary lines and the paths it wrote, and never a record body. A caller that needs a field reads # the file at the printed path. `check` writes check-design.sh's report to # /records/design-check.json and prints its status, its line count and that path. # records/ is where check-task.sh writes too, and the project's .gitignore keeps it out of history. # A report that changes on every run is a derived value and never something to commit. A clean # `check` on a light task also prints `critique: skipped, light run` and logs the skip. On any # other task it prints `closedOrders:`, the orders implementation closed, for the critic dispatch. # Every `check` removes the critique rows on closed orders and prints `critiqueDropped:` for each. # # Usage: # design-actions.sh read # design-actions.sh start # design-actions.sh create \ # --title [--criteria-served ] \ # [--criteria-owned ] [--non-goals ] \ # [--depends-on ] [--interface ] [--reasoning ] \ # [--diff-budget ] [--proof ] [--surface ]... # design-actions.sh update \ # --id [--title ] [--criteria-served ] \ # [--criteria-owned ] [--non-goals ] \ # [--depends-on ] [--interface ] [--reasoning ] \ # [--strike-reasoning ]... [--append-reasoning ]... [--absence-reviewed ]... \ # [--diff-budget ] [--proof ] [--surface ]... # design-actions.sh add-owned-file \ # --id --path [--shared] # design-actions.sh add-done-when \ # --id --text # design-actions.sh add-test \ # --id --level --description # design-actions.sh remove-test --id --description # design-actions.sh remove-done-when --id --text # design-actions.sh remove-owned-file --id --path # design-actions.sh merge --into --from --reason # design-actions.sh remove --id --reason [--merged-into ] # design-actions.sh read-guide --path [--name ] # design-actions.sh verify --id --recipe [--not-binding] [--value =]... # design-actions.sh verify --id --run [--pass
] [--kind ] [--criterion ] --cite # design-actions.sh verify --id --check [--kind ] --cite # design-actions.sh verify --id --clear # design-actions.sh render --id # design-actions.sh check # design-actions.sh --run-mode close \ # --recipe-fit --recipe-path --recipe-reason | --no-recipe \ # [--critique-outcome ]... [--approve-runs] # design-actions.sh distill # design-actions.sh --run-mode dispose --id \ # --candidate --distance \ # --cost --verdict --why \ # [--path --interface ] # design-actions.sh account --id --finding # [--set-aside | --remove] # # --run-mode is accepted on every action and `close` and `dispose` require it. A close record says who was # present, so the mode cannot default: an autonomous run that forgot the flag would otherwise # record a person nobody saw. Every other action ignores it. `dispose` needs --cost unless the # verdict is decline, which compares nothing. # # Depends on, shipped by the same part and never edited here: # ${CLAUDE_PLUGIN_ROOT}/scripts/design-render.sh called by `create`, `update` and `render` # ${CLAUDE_PLUGIN_ROOT}/scripts/check-design.sh called by `check` and `close` # ${CLAUDE_PLUGIN_ROOT}/scripts/lib/recipes.sh sourced; `verify` reads a recipe through it # ${CLAUDE_PLUGIN_ROOT}/scripts/lib/records-hash.sh sourced; its records_hash_for is called by # `close`, and by implement-actions.sh's own # `start`, so the two always agree on the # same number for the same files. # # This script never runs the schema comparison itself. Every field it writes is validated before # the write, so what it produces is shaped correctly by construction; a stale or hand-edited work # order file already on disk before this script's first call on it is a fact check-design.sh # reports, not one this script repairs. # # A work order file is plain JSON at /design/.json: no fences, no markdown # (scripts/design-schema.json). `create` mints `id`, then writes schemaVersion, title, # criteriaServed, criteriaOwned, nonGoals, dependsOn, ownedFiles, interface, tests, doneWhen, # reasoning and diffBudget in one call, and proof only when --proof was passed, so an absent field # is an order nobody chose a proof for (every reader takes it as tests); ownedFiles, tests and doneWhen start empty and grow one # entry at a time through their own add- actions, the same append-one-at-a-time shape # research-actions.sh's own `record` uses, because a test or a done-when sentence is free text # that cannot safely be packed into one comma-separated argument the way an id list can. # # An id is minted by scanning this task's own design/ folder for the highest wo already # present and taking the next number. There is no counter file recording a high-water mark the # way alignment.json's nextCriterionId does for a criterion. Two actions delete an order. `merge` # folds one order into another, and the folded order's file goes (live-run row 74). `remove` # deletes an order nothing started. Both record the id and the reason in # /design-removed.json (gap rows 234, 245), and a merge adds the survivor as # `mergedInto`. The scan reads that record too, so a removed or folded id is never minted again. # `remove --merged-into` records an order folded before merge kept a record. # # `close` records what design closed on (ideal/implementation.md, "Freezing, and what a freeze is # for"). It runs check-design.sh against the live files first, and writes # /design-closed.json only when that run exits 0. The record holds schemaVersion, the # UTC date, the run mode, who closed it, and one hash, computed by scripts/lib/records-hash.sh over alignment.json and every # design/*.json together, in work order id order (scripts/design-closed-schema.json), plus recipeFit, # design's verdict on the recipe it read, from the three --recipe-* flags (ideal/tooling.md). It sits at # the task's own root, beside task.json and alignment.json, never inside design/, because a record # inside the folder it hashes would hash itself. Implementation reads this file and refuses to # freeze anything when the hash it re-derives from the live files disagrees with the hash recorded # here; closing again after a further change, which this action always allows, is the supported # way to make the two agree again. # # `read-guide` records that design opened a guide body (live-run row 77): research names guides # without opening them, so design is the first read, and a second run in a new session could not # tell what the first read. /design-guides-read.json holds one entry per path, with # the body's sha256, the UTC date, and the name research gave it when --name was passed # (scripts/design-guides-read-schema.json). A second read of the same path replaces its entry. It # sits at the task root beside design-closed.json, never inside design/, where every file is read # as a work order and hashed into the close. `read` and `start` print `guidesRead:`, and on a # resumed run one `guide:` line per entry saying `changed`, `unchanged` or `missing` against the # body on disk, so the resumed run reads only what changed. Commits nothing; the close commits it. # # `account` records what design did with one research finding (gap row 223), the way `dispose` # records a prior-art candidate. A finding is named `#`: its search's file under # research/, and its place in that file's findings, counted from 1 as research-render.sh numbers # it. The entry lands in the order's `findings` with the finding's text, so the frozen order and # the build brief carry it. With --set-aside, the entry holds the reason and the brief leaves it # out. `check` and `close` refuse, exit 7, while a finding is in no order's `findings` with its # current text, or while an entry names a finding research no longer holds. --remove deletes an # entry. # # Exit codes, each one and only one meaning: # 0 did what was asked. For `read`, this includes an honest report that no contract exists yet # and that no work orders exist yet. For `check`, the design check found nothing that stops # the design. For `close`, the same, followed by a write of design-closed.json. A finding that # does not stop is a warning: `check` prints it, and `close` writes it to the task record. # 3 the call or the script could not do its job: not a task folder, a missing or malformed # argument, an id naming no work order, a file, a finding or a test that is not there, a file # on disk that does not parse, a write or a render that failed, or check-design.sh that could # not run or could not reach the contract or the graph. Nothing was written. # 5 `check` or `close` found what stops the design: check-design.sh's own exit 4, or a # design-removed.json out of its shape. An unattended `close` also stops on a criterion with # no serving order, no owner or two owners. A person's close records those as warnings. # 7 an unattended `close` found a research finding no work order accounts for (gap row 223). # Each one prints as `#: `. `account` is the way # through. A person's close records them as warnings, and `check` prints them. # 70 `close --approve-runs` found no person's answer for the run lines it would stamp. The # person answers through person-answer.sh, step `design close`, in every run mode. # 79 the action was run from outside the task's own worktree; every stage action but `read` runs there. # # Portability: bash 3.2+ and zsh. No mapfile, no associative arrays, no GNU-only flag, no regular # expression interval quantifier anywhere, the same rule research-actions.sh and # scope-actions.sh state for the same reason (foundations.md, Honesty). An id's own shape is # checked with a `case` glob (`wo[1-9]*`, `c[1-9]*` or `n[1-9]*`), never a regular expression, the # same way research-actions.sh checks a criterion id's own shape. set -uo pipefail # not -e: several branches test a command's exit code on purpose. trap '' PIPE # a closed pipe must not kill the writes after a print; research-actions.sh says why if [ -n "${ZSH_VERSION:-}" ]; then setopt KSH_ARRAYS 2>/dev/null fi if [ -n "${ZSH_VERSION:-}" ]; then SCRIPT_SOURCE="$0" else SCRIPT_SOURCE="${BASH_SOURCE[0]}" fi PLUGIN_ROOT="${CLAUDE_PLUGIN_ROOT:-$(cd -- "$(dirname -- "$SCRIPT_SOURCE")/../../.." >/dev/null 2>&1 && pwd -P)}" if [ -z "$PLUGIN_ROOT" ] || [ ! -d "$PLUGIN_ROOT" ]; then printf 'design-actions: could not resolve the plugin root (CLAUDE_PLUGIN_ROOT is not set and the script'"'"'s own location could not be resolved)\n' >&2 exit 3 fi DESIGN_RENDER_SCRIPT="${PLUGIN_ROOT}/scripts/design-render.sh" CHECK_DESIGN_SCRIPT="${PLUGIN_ROOT}/scripts/check-design.sh" RECORDS_HASH_LIB="${PLUGIN_ROOT}/scripts/lib/records-hash.sh" RUN_MODE="" if [ "${1:-}" = "--run-mode" ]; then [ $# -ge 2 ] || { printf 'design-actions: --run-mode needs a value\n' >&2; exit 3; } RUN_MODE="$2" shift 2 case "$RUN_MODE" in interactive|autonomous) ;; *) printf 'design-actions: run mode must be interactive or autonomous, got %s\n' "$RUN_MODE" >&2; exit 3 ;; esac fi command -v jq >/dev/null 2>&1 || { printf 'design-actions: jq is required and was not found on PATH\n' >&2; exit 3; } # An answer task_answer_find held is marked refused before the exit (task-helpers.sh). die() { ! command -v task_answer_refused >/dev/null 2>&1 || task_answer_refused "$1"; printf 'design-actions: %s\n' "$2" >&2; exit "$1"; } die3() { die 3 "$1"; } die5() { die 5 "$1"; } die7() { die 7 "$1"; } # shellcheck disable=SC2329 # called by functions in scripts/lib/task-helpers.sh die1() { die 3 "$1"; } # shellcheck disable=SC2329 # called by functions in scripts/lib/task-helpers.sh die79() { die 79 "$1"; } [ -f "$RECORDS_HASH_LIB" ] || die3 "cannot find the records-hash library at $RECORDS_HASH_LIB" # shellcheck source=/dev/null source "$RECORDS_HASH_LIB" || die3 "the records-hash library failed to load: $RECORDS_HASH_LIB" usage() { cat <<'EOF' >&2 usage: design-actions.sh read design-actions.sh start design-actions.sh create --title \ [--criteria-served ] \ [--criteria-owned ] \ [--non-goals ] [--depends-on ] \ [--interface ] [--reasoning ] \ [--diff-budget ] [--proof ] [--surface ]... design-actions.sh update --id [--title ] \ [--criteria-served ] \ [--criteria-owned ] \ [--non-goals ] [--depends-on ] \ [--interface ] [--reasoning ] \ [--strike-reasoning ]... [--append-reasoning ]... [--absence-reviewed ]... \ [--diff-budget ] [--proof ] [--surface ]... design-actions.sh add-owned-file --id --path [--shared] design-actions.sh add-done-when --id --text design-actions.sh add-test --id --level \ --description design-actions.sh remove-test --id --description design-actions.sh remove-done-when --id --text design-actions.sh remove-owned-file --id --path design-actions.sh merge --into --from --reason design-actions.sh remove --id --reason [--merged-into ] design-actions.sh read-guide --path [--name ] design-actions.sh verify --id --recipe [--not-binding] [--value =]... design-actions.sh verify --id --run [--pass ] [--kind ] [--criterion ] --cite design-actions.sh verify --id --check [--kind ] --cite design-actions.sh verify --id --clear design-actions.sh render --id design-actions.sh check design-actions.sh --run-mode close \ --recipe-fit --recipe-path --recipe-reason | --no-recipe \ [--critique-outcome ]... [--approve-runs] design-actions.sh distill design-actions.sh --run-mode dispose --id \ --candidate --distance \ --cost --verdict --why \ [--path --interface ] design-actions.sh account --id --finding # [--set-aside | --remove] EOF } # The four helpers every stage script needs before it touches a task folder live in one place # (scripts/lib/task-helpers.sh): resolve_task_folder, looks_like_flag, is_blank, write_atomic. TASK_HELPERS_LIB="${PLUGIN_ROOT}/scripts/lib/task-helpers.sh" [ -f "$TASK_HELPERS_LIB" ] || die3 "cannot find the task-helper library at $TASK_HELPERS_LIB" # shellcheck source=/dev/null source "$TASK_HELPERS_LIB" || die3 "the task-helper library failed to load: $TASK_HELPERS_LIB" # `verify` reads a recipe's `## Verifier` with the recipe readers every stage shares: # recipe_block_into, pc_trim, pc_unquote and refuse_if_unsafe. Sourced here, before the task folder # resolves, because the library resets TASK_PATH when it loads. RECIPES_LIB="${PLUGIN_ROOT}/scripts/lib/recipes.sh" # shellcheck source=/dev/null source "$RECIPES_LIB" || die3 "the recipes library failed to load: $RECIPES_LIB" # `textOnly`, the rule add-owned-file and the impliedProof line read for an order of documents. # shellcheck source=/dev/null source "${PLUGIN_ROOT}/scripts/lib/proof.sh" || die3 "the proof-kind library failed to load" # Prints the JSON array $1 with $2 added at its end. A flag a person can repeat collects each # value this way, so a second one never replaces the first (gap row 435). json_appended() { printf '%s' "$1" | jq -c --arg v "$2" '. + [$v]' } # Dies unless $3 is a whole number from 1. $1 the action and flag, $2 what the number counts. need_count() { case "$3" in ''|0*|*[!0-9]*) die3 "$1 takes a $2 number from 1, got '$3'" ;; esac return 0 } # Prints "true" when the contract at $ALIGNMENT_FILE exists, is readable, parses as JSON, and is # shaped like a contract. Never dies: `read` uses this to report an honest state rather than # fail, and `start` uses it to decide whether to refuse. contract_ok() { [ -f "$ALIGNMENT_FILE" ] || { printf 'false'; return; } [ -r "$ALIGNMENT_FILE" ] || { printf 'false'; return; } jq empty "$ALIGNMENT_FILE" 2>/dev/null || { printf 'false'; return; } local shape shape="$(jq -r ' if type != "object" then "no" elif (has("schemaVersion") | not) then "no" elif (has("goal") | not) then "no" elif (has("expectedResult") | not) then "no" elif ((.criteria | type) != "array") then "no" elif ((.nonGoals | type) != "array") then "no" else "yes" end ' "$ALIGNMENT_FILE" 2>/dev/null)" if [ "$shape" = "yes" ]; then printf 'true'; else printf 'false'; fi } contract_criteria_json() { if [ "$(contract_ok)" != "true" ]; then printf '[]' return fi jq -c '[ (.criteria // [])[]? | {id: .id, text: .text} ]' "$ALIGNMENT_FILE" 2>/dev/null || printf '[]' } contract_non_goals_json() { if [ "$(contract_ok)" != "true" ]; then printf '[]' return fi jq -c '[ (.nonGoals // [])[]? | {id: .id, text: .text} ]' "$ALIGNMENT_FILE" 2>/dev/null || printf '[]' } # True (exit 0) when $1 has the shape of an id in the space named by $2 ("wo", "c" or "n"): # that prefix, then a digit 1-9, then zero or more digits. The same case-glob idiom # research-actions.sh uses for a criterion id, extended here to the three id spaces this script # reads or writes: wo for a work order, c for a criterion, n for a non-goal. id_shape_ok() { local id="$1" space="$2" num case "$space" in wo) case "$id" in wo*) : ;; *) return 1 ;; esac; num="${id#wo}" ;; c) case "$id" in c*) : ;; *) return 1 ;; esac; num="${id#c}" ;; n) case "$id" in n*) : ;; *) return 1 ;; esac; num="${id#n}" ;; *) return 1 ;; esac case "$num" in ''|0*|*[!0-9]*) return 1 ;; esac return 0 } # Splits $1 on commas into $ID_LIST_JSON, a JSON array of strings, checking each entry is # non-blank and has the shape of an id in space $2 ("wo", "c" or "n"). Prints nothing; the caller # reads $ID_LIST_JSON. An empty $1 yields "[]". # The list is split with tr and read line by line. An unquoted `for id in $raw` under a comma IFS # splits in bash and not in zsh. zsh was handed the whole list as one id. parse_id_list() { local raw="$1" space="$2" who="$3" id ID_LIST_JSON='[]' [ -n "$raw" ] || return 0 while IFS= read -r id; do is_blank "$id" && die3 "$who: has a blank id in '$raw'" id_shape_ok "$id" "$space" \ || die3 "$who: id '$id' is not a valid ${space} id shape (no leading zero)" ID_LIST_JSON="$(printf '%s' "$ID_LIST_JSON" | jq --arg id "$id" '. + [$id]')" done < <(printf '%s\n' "$raw" | tr ',' '\n') } # The work order file for a given id, or empty when it does not exist. Never dies. wo_file_for() { printf '%s/design/%s.json' "$TASK_PATH" "$1" } wo_exists() { local f f="$(wo_file_for "$1")" [ -f "$f" ] } render_wo() { local id="$1" [ -f "$DESIGN_RENDER_SCRIPT" ] \ || die3 "cannot find design-render.sh at $DESIGN_RENDER_SCRIPT" bash "$DESIGN_RENDER_SCRIPT" "$TASK_PATH" "$id" >/dev/null local rc=$? [ "$rc" -eq 0 ] || die3 "design-render.sh could not render $id.md (exit $rc)" echo "rendered: $DESIGN_DIR/$id.md" } # The one summary printer. It names the id and the list fields, and counts the free-text ones. wo_summary() { printf '%s' "$1" | jq -r ' "id: " + .id, "criteriaServed: " + ((.criteriaServed // []) | join(",")), "criteriaOwned: " + ((.criteriaOwned // []) | join(",")), "dependsOn: " + ((.dependsOn // []) | join(",")), "ownedFiles: " + ((.ownedFiles // []) | length | tostring), "tests: " + ((.tests // []) | length | tostring), "doneWhen: " + ((.doneWhen // []) | length | tostring), "proof: " + (.proof // "tests")' } # One line naming the proof the order's owned criteria imply, printed beside the proof it declares # (live-run row 145). $1 is the work order document, $2 its id. An order whose every owned file # lies under the project folder produces a document, so it implies `record`, the rule # add-owned-file applies. On a task with no automated tests any other order implies `confirm`, the # same rule's other half. So does a text-only order elsewhere (gap row 461). Otherwise the line # reads criteriaOwned alone. Owning a machine-verified criterion implies any kind that proves one: # tests, the gate lines, a look, or run lines on a confirm order. What the order produces decides. The line said `tests` here once, and pushed a report or an update onto an # invented test. An order owning criteria of which none is machine-verified implies a proof # other than `tests`. An order owning nothing implies nothing. Printed on every create and # update, not only on a disagreement, so one line says what the contract says about this order. implied_proof_line() { local owned machine owned="$(printf '%s' "$1" | jq -r '(.criteriaOwned // []) | length')" if [ "$owned" -eq 0 ]; then echo "impliedProof: none, because $2 owns no criterion" return fi if [ "$(printf '%s' "$1" | jq -r --arg t "$PROJECT_PATH/" \ '((.ownedFiles // []) | length > 0) and ((.ownedFiles // []) | all(startswith($t)))')" = "true" ]; then echo "impliedProof: record, because every file $2 owns lies under the project folder" return fi if [ "$(automated_tests "$TASK_PATH")" = "no" ]; then echo "impliedProof: confirm, because the task has no automated tests, so a person confirms $2's done-when rows at review" return fi if [ "$(printf '%s' "$1" | jq -r "$BR_ORDER_FACTS_JQ textOnly")" = "true" ]; then echo "impliedProof: confirm, because every file $2 owns is a document or configuration, so a reading proves it and no test does" return fi if [ "$(contract_ok)" != "true" ]; then echo "impliedProof: not read, because the contract could not be read" return fi # The machine-verified criteria this order owns, read the way check-design.sh reads them. machine="$(jq -r --argjson wo "$1" ' [ (.criteria // [])[]? | select(type == "object") | select(.verifiedBy == "machine") | .id ] as $m | [ ($wo.criteriaOwned // [])[] | select(. as $c | $m | index($c) != null) ] | join(",") ' "$ALIGNMENT_FILE" 2>/dev/null)" if [ -n "$machine" ]; then local word case "$machine" in *,*) word="criteria" ;; *) word="criterion" ;; esac echo "impliedProof: any kind, because $2 owns the machine-verified $word $machine, which tests, gate lines or run lines prove. What $2 produces decides" else echo "impliedProof: not tests, because no criterion $2 owns is machine-verified" fi } # One line naming everything a check report left open, for `check` and `close` alike. # The orders whose declared proof disagrees with what their owned criteria imply, from a check # report (live-run row 145). This one finding never raises the exit code, so `check` prints it on # a line of its own. proof_disagreement_of() { printf '%s' "$1" | jq -r ' [ (.coverage.testOrdersOwningNoMachineCriterion // [])[] | .id ] | join(", ") ' 2>/dev/null } # The `findings: N` last line of the critique file at $1, or exit 1 when the file carries none. # No line means its critic did not finish. `close` reads it twice: once to decide the file is # evidence worth committing, once to count it. critique_findings_of() { local n n="$(grep -E '^findings: [0-9]+$' "$1" | tail -n 1 | sed 's/^findings: //')" [ -n "$n" ] || return 1 printf '%s' "$n" } # The order ids, one per line and sorted, of the close commit whose design-closed.json holds the # hash $1. Exit 1 when no commit of the project holds it. The project folder is two levels up, as # in commit_stage_close. answered_order_ids() { local project rel c project="$(dirname -- "$(dirname -- "$TASK_PATH")")" rel="tasks/$(jq -r '.id' "$TASK_PATH/task.json")" while IFS= read -r c; do [ "$(git -C "$project" show "$c:$rel/design-closed.json" 2>/dev/null | jq -r '.hash' 2>/dev/null)" = "$1" ] || continue git -C "$project" ls-tree --name-only "$c" "$rel/design/" | sed -n 's|.*/\(wo[0-9]*\)\.json$|\1|p' | sort return 0 done < <(git -C "$project" log --format=%H -- "$rel/design-closed.json" 2>/dev/null) return 1 } # The orders implementation closed, from the ledger's `lastStep`, comma-joined, or nothing when # no ledger exists or none closed (gap row 227). The critics judge only the others: an answer to a # finding on a closed order edits a finished order. closed_orders() { jq -r '[ (.orders // [])[] | select(.lastStep == "closed") | .id ] | join(", ")' \ "$TASK_PATH/implementation/ledger.json" 2>/dev/null } # The critique file at $1 without each table row whose order cell names only orders in $2, a # closed_orders list, and with its `findings: N` line lowered by the rows removed. A row that also # names an open order stays. Above the findings line, `dropped on closed orders: N ()` keeps # the total removed across passes, so a second pass that removes nothing changes nothing. Prints # the new file, then the count this pass removed as its own last line. critique_without_closed() { awk -F'|' -v closed="$2" ' BEGIN { n = split(closed, c, /, */); for (i = 1; i <= n; i++) shut[c[i]] = 1 } /^\|/ && $3 !~ /^ *(order|-+) *$/ { cell = $3; gsub(/^ +| +$/, "", cell); k = split(cell, ids, /, */); all = (k > 0) for (i = 1; i <= k; i++) if (!(ids[i] in shut)) all = 0 if (all) { dropped++; next } } /^dropped on closed orders: [0-9]+ / { split($0, d, " "); before = d[5]; trace = m + 1 } { line[++m] = $0 } END { for (i = 1; i <= m; i++) { if (dropped > 0 && i == trace) continue if (dropped > 0 && line[i] ~ /^findings: [0-9]+$/) { split(line[i], f, " ") print "dropped on closed orders: " (before + dropped) " (" closed ")" line[i] = "findings: " (f[2] - dropped) } print line[i] } print dropped + 0 }' "$1" } # Removes the rows on closed orders from every finished critique under records/, and prints one # `critiqueDropped:` line per file that lost a row (gap row 227). `check` runs it before a person # reads the findings, and `close` again before the files move. The rows are dropped rather than # refused: a refusal would let a critic stop the close. drop_closed_critique_rows() { local closed crit_file kept dropped closed="$(closed_orders)" [ -n "$closed" ] || return 0 while IFS= read -r crit_file; do [ -n "$crit_file" ] || continue critique_findings_of "$crit_file" >/dev/null || continue kept="$(critique_without_closed "$crit_file" "$closed")" dropped="$(printf '%s\n' "$kept" | tail -n 1)" [ "$dropped" -gt 0 ] || continue write_atomic "$crit_file" "$(printf '%s\n' "$kept" | sed '$d')" echo "critiqueDropped: $(basename -- "$crit_file") $dropped on closed orders $closed" done < <(find "$TASK_PATH/records" -mindepth 1 -maxdepth 1 -type f -name 'design-critique-*.md' 2>/dev/null | sort) } # One line per research finding no work order's `findings` names with both its reference and its # text: `#: `. A text that differs means research changed or dropped a # finding after the entry was written, so the reference may now point at a neighbour. Then one # line per entry whose reference names no finding research holds now, or a finding whose text is # not the entry's. Another order can account for the finding now at that number, so the first # pass alone lets an entry for a dropped finding through (gap row 236). Prints nothing when every # finding is accounted for, and when research holds none. unaccounted_findings() { local accounted f e wo ref search n now accounted="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -c '(.findings // [])[] | {ref, text}' "$f" 2>/dev/null; done)" find "$TASK_PATH/research" -mindepth 1 -maxdepth 1 -type f -name '*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq_files -r --arg s "$(basename -- "$f" .json)" --arg acc "$accounted" ' ($acc | split("\n") | map(select(length > 0) | fromjson)) as $acc | (.findings // []) | to_entries[] | ($s + "#" + (.key + 1 | tostring)) as $ref | .value.text as $t | select(any($acc[]; .ref == $ref and .text == $t) | not) | $ref + ": " + (($t // "") | split("\n")[0])' "$f" 2>/dev/null done find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -c '.id as $w | (.findings // [])[] | {w: $w, ref, text}' "$f" 2>/dev/null; done \ | while IFS= read -r e; do wo="$(printf '%s' "$e" | jq -r '.w')"; ref="$(printf '%s' "$e" | jq -r '.ref')" search="${ref%#*}"; n="${ref##*#}" case "$n" in ''|0*|*[!0-9]*) now="" ;; *) now="$(jq -r --argjson i "$((n - 1))" '(.findings // [])[$i].text // empty' "$TASK_PATH/research/$search.json" 2>/dev/null)" ;; esac if [ -z "$now" ]; then echo "$ref: $wo holds this entry, and research holds no such finding now. Remove it with account --remove" elif [ "$now" != "$(printf '%s' "$e" | jq -r '.text')" ]; then echo "$ref: $wo holds this entry, and research's $ref now reads differently, so the finding it named changed or was dropped. Remove it with account --remove, then account the finding it meant" fi done } # `choicePairs`, read from a work order document: each pair of backticked interface names joined # by `or`, with at most two words after it, as ` or ` (gap row 345). Such a pair is often a # choice the builder makes, such as `os.DirEntry` or an explicit `stat`. A builder that takes one # option leaves the other out of its record, and the reviewer compares the two. CHOICE_PAIRS_JQ=' def choicePairs: ((.interface // "") | if type == "string" then split("`") else [] end) as $p | ($p | length) as $n | (if $n % 2 == 1 then $n - 2 else $n - 3 end) as $last | [ range(1; $last - 1; 2) as $i | select(($p[$i] | length) > 0 and ($p[$i + 2] | length) > 0) | select($p[$i + 1] | test("^,?\\s+or\\s+([a-z]+\\s+)?([a-z]+\\s+)?$")) | $p[$i] + " or " + $p[$i + 2] ] | unique; ' # One line per order whose interface holds a choice pair: ` [, ]`. Prints nothing # when no order holds one. open_choices() { local f find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -r "$CHOICE_PAIRS_JQ"' . as $wo | choicePairs | select(length > 0) | $wo.id + " " + join(", ")' "$f" 2>/dev/null; done } # Prints $1, a work order document, with $2 added to its `reasoning` as a new paragraph. The # paragraph follows a blank line, or is the whole field when it was empty. `dispose` and # `update --append-reasoning` both write this way, so an earlier paragraph is never lost # (live-run rows 79 and 131). reasoning_appended() { printf '%s' "$1" | jq --arg v "$2" '.reasoning = (if (.reasoning // "") == "" then $v else .reasoning + "\n\n" + $v end)' } # The dispositions that stand on the work order file $1, one ` TAB TAB # ` line each. The order's `dispositions` list comes first. Then each candidate a live # `Candidate (` paragraph names and the list does not, with its last paragraph's outcome and the # run mode `unrecorded`. An order disposed before the list existed holds only paragraphs, and # those are counted too (gap row 293). The candidate goes out raw, never through @tsv, which # writes a backslash as two. The callers compare it to the text the confirmer keyed (gap row 387). standing_dispositions() { jq -r ' ((.dispositions // []) | map(.candidate)) as $listed | ((.dispositions // []) + ([ (.reasoning // "") | split("\n\n")[] | capture("^Candidate (?.*) \\((same-name|same-directory|same-layer)\\)\\. Proposed [a-z]+, citing [^\n]*?\\. Disposition: (?[a-z]+) \\(") | select(.candidate as $c | $listed | index($c) | not) ] | reduce .[] as $p ({}; .[$p.candidate] = $p.disposition) | to_entries | map({candidate: .key, disposition: .value, runMode: "unrecorded"})))[] | "\(.disposition)\t\(.runMode)\t\(.candidate)"' "$1" } # The sha256 of the file at $1, or nothing when it cannot be read. The caller resolves the hash # command first, through records_hash__resolve_sha256_cmd. file_sha256() { [ -f "$1" ] && [ -r "$1" ] || return 1 "${RECORDS_HASH_SHA256_CMD[@]}" <"$1" | cut -d' ' -f1 } # The guides-read record's summary lines, for `read` and `start`: `guidesRead: `, and when $1 # is above zero (a resumed run, work orders already on disk) one `guide: ` line per entry # ending `changed`, `unchanged` or `missing`. `changed` means the body's sha256 differs from the # recorded one; `missing` means no readable file is at the path any more, so the resumed run # resolves it again through the navigator. A record that is not valid JSON counts as zero here; # `check` is what refuses it. guides_read_lines() { local resumed="$1" n=0 path recorded live tab if [ -f "$GUIDES_FILE" ] && jq empty "$GUIDES_FILE" 2>/dev/null; then n="$(jq -r '(.guides // []) | if type == "array" then length else 0 end' "$GUIDES_FILE")" fi echo "guidesRead: $n" [ "$resumed" -gt 0 ] && [ "$n" -gt 0 ] || return 0 records_hash__resolve_sha256_cmd || die3 "neither sha256sum nor 'shasum -a 256' was found on PATH" tab="$(printf '\t')" while IFS="$tab" read -r path recorded; do [ -n "$path" ] || continue if ! live="$(file_sha256 "$path")"; then echo "guide: $path missing" elif [ "$live" = "$recorded" ]; then echo "guide: $path unchanged" else echo "guide: $path changed" fi done < <(jq -r '(.guides // [])[] | select(type == "object") | [(.path // ""), (.sha256 // "")] | @tsv' "$GUIDES_FILE") } # ------------------------------------------------------------------------------------------------ # read: the current state, never a failure just because nothing has run yet. # ------------------------------------------------------------------------------------------------ do_read() { [ "$#" -eq 0 ] || die3 "read: unrecognized argument: $1" # Summary lines only. The skill routes on `contract:` and `work-orders:`, and reads the contract # and each work order from the paths printed here when it needs their text. local contract_state design_state wo_count f ftype contract_state="absent" [ "$(contract_ok)" = "true" ] && contract_state="present" echo "action: read" echo "task: $TASK_PATH" echo "worktree: $(jq -r '.worktree.path // "none"' "$TASK_PATH/task.json" 2>/dev/null)" echo "surfaces-deferred: $(jq -r '(.surfacesDeferred // []) | if length == 0 then "none" else join(" ") end' "$TASK_PATH/task.json" 2>/dev/null)" echo "contract: $contract_state" echo "contract-file: $ALIGNMENT_FILE" echo "criteria: $(contract_criteria_json | jq -r '[.[].id] | join(" ")')" echo "non-goals: $(contract_non_goals_json | jq -r '[.[].id] | join(" ")')" design_state="not started" wo_count=0 if [ -d "$DESIGN_DIR" ]; then design_state="started" while IFS= read -r f; do [ -n "$f" ] || continue ftype="$(jq -r 'type' "$f" 2>/dev/null)" if [ "$ftype" = "object" ]; then wo_count=$((wo_count + 1)) echo "work-order: $f" else echo "unreadable: $f" fi done < <(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name '*.json' 2>/dev/null | sort) fi echo "design: $design_state" echo "design-dir: $DESIGN_DIR" echo "work-orders: $wo_count" guides_read_lines "$wo_count" exit 0 } # ------------------------------------------------------------------------------------------------ # start: makes sure the contract exists and research has closed before design begins, and makes # sure the design folder exists. Idempotent, the same as research's own `start`: no aggregate # file here could be overwritten by a second call. # # The grounding hash (ideal/design.md, "Acting on a claim about a mechanism"): research's `check` # records one sha256 per mechanismHints[].approach when it closes clean. `start` re-derives the list # and prints a NOTE: per position that differs. Never a refusal: the claim may still be right, but # the evidence no longer covers it. # ------------------------------------------------------------------------------------------------ # One sha256 per approach in $1/task.json, as a JSON array, in order. Each approach is hashed as # its own compact JSON string, one line each, so a multi-line approach never breaks the loop. # research-actions.sh computes the same list the same way at its close. mechanism_hashes_json() { local hashes='[]' line h while IFS= read -r line; do [ -n "$line" ] || continue h="$(printf '%s' "$line" | "${RECORDS_HASH_SHA256_CMD[@]}" | cut -d' ' -f1)" hashes="$(printf '%s' "$hashes" | jq --arg h "$h" '. + [$h]')" done < <(jq -c '.mechanismHints[]? | .approach' "$1/task.json" 2>/dev/null) printf '%s' "$hashes" } do_start() { [ "$#" -eq 0 ] || die3 "start: unrecognized argument: $1" [ "$(contract_ok)" = "true" ] \ || die3 "start: $ALIGNMENT_FILE not found, unreadable, or not a contract. Run the scope skill on this task first" # Research is optional, so design starts without it and says so. [ -f "$RESEARCH_CHECK_FILE" ] && [ "$(jq -r '.exitCode // 1' "$RESEARCH_CHECK_FILE" 2>/dev/null)" = "0" ] \ || task_warn "$TASK_PATH" "design start" "" "research has not closed on this task, so no criterion has a finding behind it" records_hash__resolve_sha256_cmd || die3 "start: neither sha256sum nor 'shasum -a 256' was found on PATH" mark_task_in_progress "$TASK_PATH" "design started" design mkdir -p "$DESIGN_DIR" || die3 "start: could not create $DESIGN_DIR" echo "STARTED: $DESIGN_DIR" echo "contract-file: $ALIGNMENT_FILE" echo "criteria: $(contract_criteria_json | jq -r '[.[].id] | join(" ")')" # Whether a light task's first order writes the demo-path script, read as design drafts it. ! task_is_light "$TASK_PATH" || path_script_line "$TASK_PATH" # A resumed run has work order files on disk already, and reads only the guides that changed. local resumed=0 [ -z "$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name '*.json' 2>/dev/null | head -n 1)" ] || resumed=1 guides_read_lines "$resumed" local recorded live recorded="$(jq -c '.mechanismHashes // []' "$RESEARCH_CHECK_FILE" 2>/dev/null)" live="$(mechanism_hashes_json "$TASK_PATH")" jq -nr --argjson r "$recorded" --argjson l "$live" ' range([($r | length), ($l | length)] | max) | select($r[.] != $l[.]) | "NOTE: mechanism hint " + ((. + 1) | tostring) + " in task.json was edited after research grounded it; read it as ungrounded"' exit 0 } # ------------------------------------------------------------------------------------------------ # The next work order id: scan design/*.json and the removed record for the highest wo and take # the next number, starting at wo1 when none exist. # ------------------------------------------------------------------------------------------------ next_wo_id() { local max this_id this_num max=0 while IFS= read -r this_id; do case "$this_id" in wo[1-9]*) this_num="${this_id#wo}" case "$this_num" in ''|*[!0-9]*) continue ;; esac [ "$this_num" -gt "$max" ] && max="$this_num" ;; esac done < <(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name '*.json' 2>/dev/null \ | while IFS= read -r f; do jq -r '.id? // empty' "$f" 2>/dev/null; done jq -r '(.removed // [])[]? | .id? // empty' "$REMOVED_FILE" 2>/dev/null) printf 'wo%d' "$((max + 1))" } # ------------------------------------------------------------------------------------------------ # create: mints an id and writes a new work order file with the scalar and id-list fields given. # ownedFiles, tests and doneWhen start empty; use add-owned-file, add-test and add-done-when to # grow them one entry at a time. # ------------------------------------------------------------------------------------------------ # --proof takes one of four words. A unit whose deliverable is exported configuration is proved by # the recipe's `## Configuration gate` lines and declares no test (live-run row 65). A unit whose # deliverable is a document in the task folder is proved by its done-when rows and lands no commit # in the code repository (nyc defect 17); add-owned-file below marks it `record` on its own. A unit # whose deliverable is what a page shows is proved by a model's look through a browser at each of # its surfaces, judged against its done-when rows after the build (live-run row 104): `observe`. # A code unit of a task with no automated tests is built with no test, and a person confirms its # done-when rows at review (gap row 196): `confirm`. add-owned-file below marks it on its own. proof_word_ok() { case "$2" in tests|gate|record|observe|confirm) ;; *) die3 "$1: --proof takes tests, gate, record, observe or confirm, got: $2" ;; esac } do_create() { local title="" criteria_served="" criteria_owned="" non_goals="" depends_on="" local interface="" reasoning="" diff_budget="" surfaces_json='[]' proof="" while [ "$#" -gt 0 ]; do case "$1" in --title) need_value "create" "--title" "$#" "${2:-}" title="$2"; shift 2 ;; --criteria-served) need_value "create" "--criteria-served" "$#" "${2:-}" criteria_served="$2"; shift 2 ;; --criteria-owned) need_value "create" "--criteria-owned" "$#" "${2:-}" criteria_owned="$2"; shift 2 ;; --non-goals) need_value "create" "--non-goals" "$#" "${2:-}" non_goals="$2"; shift 2 ;; --depends-on) need_value "create" "--depends-on" "$#" "${2:-}" depends_on="$2"; shift 2 ;; --interface) need_value "create" "--interface" "$#" "${2:-}" interface="$2"; shift 2 ;; --reasoning) need_value "create" "--reasoning" "$#" "${2:-}" reasoning="$2"; shift 2 ;; --diff-budget) need_value "create" "--diff-budget" "$#" "${2:-}" diff_budget="$2"; shift 2 ;; --proof) need_value "create" "--proof" "$#" "${2:-}" proof_word_ok "create" "$2" proof="$2"; shift 2 ;; --surface) need_value "create" "--surface" "$#" "${2:-}" surfaces_json="$(json_appended "$surfaces_json" "$2")"; shift 2 ;; *) die3 "create: unrecognized argument: $1" ;; esac done is_blank "$title" && die3 "create: --title is required and must not be blank" parse_id_list "$criteria_served" c "create: --criteria-served"; local served_json="$ID_LIST_JSON" parse_id_list "$criteria_owned" c "create: --criteria-owned"; local owned_json="$ID_LIST_JSON" parse_id_list "$non_goals" n "create: --non-goals"; local nongoals_json="$ID_LIST_JSON" parse_id_list "$depends_on" wo "create: --depends-on"; local dependson_json="$ID_LIST_JSON" mkdir -p "$DESIGN_DIR" || die3 "create: could not create $DESIGN_DIR" local id file id="$(next_wo_id)" file="$(wo_file_for "$id")" [ ! -e "$file" ] || die3 "create: $file already exists; the next id was minted wrong. This is a bug, not an authoring mistake" local doc doc="$(jq -n \ --arg id "$id" --arg title "$title" \ --argjson criteriaServed "$served_json" --argjson criteriaOwned "$owned_json" \ --argjson nonGoals "$nongoals_json" --argjson dependsOn "$dependson_json" \ --arg interface "$interface" --arg reasoning "$reasoning" --arg diffBudget "$diff_budget" \ --argjson surfaces "$surfaces_json" --arg proof "$proof" \ '{schemaVersion: 1, id: $id, title: $title, criteriaServed: $criteriaServed, criteriaOwned: $criteriaOwned, nonGoals: $nonGoals, dependsOn: $dependsOn, ownedFiles: [], surfaces: $surfaces, interface: $interface, tests: [], doneWhen: [], reasoning: $reasoning, diffBudget: $diffBudget} + (if $proof == "" then {} else {proof: $proof} end)')" write_atomic "$file" "$doc" echo "CREATED: $file" wo_summary "$doc" implied_proof_line "$doc" "$id" render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # update: replaces the given scalar or id-list fields on an existing work order, wholesale for # any list passed. ownedFiles, tests and doneWhen are not settable here; use their own add- # actions. --reasoning replaces the whole field; --append-reasoning adds a paragraph after a # blank line and keeps what is there, the write `dispose` makes (live-run row 131). The two # together are refused: one call cannot both replace the text and add to it. # --strike-reasoning marks paragraph n, counted from 1, with REASONING_JQ's prefix. The # paragraph stays in the record and the rendered design; no brief carries it (gap row 215). It # runs before --append-reasoning, so n counts the paragraphs already there. With --reasoning it # is refused, since the replaced text has no paragraph n. # --strike-reasoning, --append-reasoning and --absence-reviewed can repeat, and every value # applies, appends in the order given. One value that names no live paragraph or no row refuses # the call before anything is written (gap row 435). # --absence-reviewed stores the text of done-when row n, counted from 1, in `absenceReviewed`. # `check` stops printing that row under `absenceJoined:` (gap row 237). The text is the key, so a # row whose text changes flags again. Each write drops the entries no row holds any more. # ------------------------------------------------------------------------------------------------ do_update() { local id="" title="" criteria_served="" criteria_owned="" non_goals="" depends_on="" local interface="" reasoning="" appends_json='[]' strikes_json='[]' absences_json='[]' diff_budget="" surfaces_json='[]' proof="" local set_absence=false set_title=false set_served=false set_owned=false set_nongoals=false set_dependson=false local set_interface=false set_reasoning=false set_append=false set_strike=false set_diffbudget=false set_surfaces=false set_proof=false while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "update" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; --title) need_value "update" "--title" "$#" "${2:-}" title="$2"; set_title=true; shift 2 ;; --criteria-served) need_value "update" "--criteria-served" "$#" "${2:-}" criteria_served="$2"; set_served=true; shift 2 ;; --criteria-owned) need_value "update" "--criteria-owned" "$#" "${2:-}" criteria_owned="$2"; set_owned=true; shift 2 ;; --non-goals) need_value "update" "--non-goals" "$#" "${2:-}" non_goals="$2"; set_nongoals=true; shift 2 ;; --depends-on) need_value "update" "--depends-on" "$#" "${2:-}" depends_on="$2"; set_dependson=true; shift 2 ;; --interface) need_value "update" "--interface" "$#" "${2:-}" interface="$2"; set_interface=true; shift 2 ;; --reasoning) need_value "update" "--reasoning" "$#" "${2:-}" reasoning="$2"; set_reasoning=true; shift 2 ;; --append-reasoning) need_value "update" "--append-reasoning" "$#" "${2:-}" is_blank "$2" && die3 "update: --append-reasoning must not be blank" appends_json="$(json_appended "$appends_json" "$2")"; set_append=true; shift 2 ;; --strike-reasoning) need_value "update" "--strike-reasoning" "$#" "${2:-}" need_count "update: --strike-reasoning" "paragraph" "$2" strikes_json="$(json_appended "$strikes_json" "$2")"; set_strike=true; shift 2 ;; --absence-reviewed) need_value "update" "--absence-reviewed" "$#" "${2:-}" need_count "update: --absence-reviewed" "done-when row" "$2" absences_json="$(json_appended "$absences_json" "$2")"; set_absence=true; shift 2 ;; --diff-budget) need_value "update" "--diff-budget" "$#" "${2:-}" diff_budget="$2"; set_diffbudget=true; shift 2 ;; --proof) need_value "update" "--proof" "$#" "${2:-}" proof_word_ok "update" "$2" proof="$2"; set_proof=true; shift 2 ;; --surface) need_value "update" "--surface" "$#" "${2:-}" surfaces_json="$(json_appended "$surfaces_json" "$2")"; set_surfaces=true; shift 2 ;; *) die3 "update: unrecognized argument: $1" ;; esac done is_blank "$id" && die3 "update: --id is required and must not be blank" id_shape_ok "$id" wo || die3 "update: --id '$id' is not a valid wo id shape" if [ "$set_append" = "true" ]; then [ "$set_reasoning" != "true" ] \ || die3 "update: --reasoning replaces the field and --append-reasoning adds to it. Pass one" fi local struck_list="" if [ "$set_strike" = "true" ]; then struck_list="$(printf '%s' "$strikes_json" | jq -r 'map(tonumber) | unique | map(tostring) | join(", ")')" [ "$set_reasoning" != "true" ] \ || die3 "update: --reasoning replaces the field, so it has no paragraph $struck_list to strike. Pass one" fi local file file="$(wo_file_for "$id")" wo_exists "$id" || die3 "update: no work order $id in $DESIGN_DIR" jq empty "$file" 2>/dev/null || die3 "update: $file exists but is not valid JSON" local doc doc="$(cat "$file")" if [ "$set_title" = "true" ]; then is_blank "$title" && die3 "update: --title must not be blank" doc="$(printf '%s' "$doc" | jq --arg v "$title" '.title = $v')" fi if [ "$set_served" = "true" ]; then parse_id_list "$criteria_served" c "update: --criteria-served" doc="$(printf '%s' "$doc" | jq --argjson v "$ID_LIST_JSON" '.criteriaServed = $v')" fi if [ "$set_owned" = "true" ]; then parse_id_list "$criteria_owned" c "update: --criteria-owned" doc="$(printf '%s' "$doc" | jq --argjson v "$ID_LIST_JSON" '.criteriaOwned = $v')" fi if [ "$set_nongoals" = "true" ]; then parse_id_list "$non_goals" n "update: --non-goals" doc="$(printf '%s' "$doc" | jq --argjson v "$ID_LIST_JSON" '.nonGoals = $v')" fi if [ "$set_dependson" = "true" ]; then parse_id_list "$depends_on" wo "update: --depends-on" doc="$(printf '%s' "$doc" | jq --argjson v "$ID_LIST_JSON" '.dependsOn = $v')" fi if [ "$set_interface" = "true" ]; then doc="$(printf '%s' "$doc" | jq --arg v "$interface" '.interface = $v')" fi if [ "$set_reasoning" = "true" ]; then doc="$(printf '%s' "$doc" | jq --arg v "$reasoning" '.reasoning = $v')" fi if [ "$set_strike" = "true" ]; then local dead dead="$(printf '%s' "$doc" | jq -r --argjson ns "$strikes_json" "$REASONING_JQ"' ((.reasoning // "") | split("\n\n")) as $p | [ $ns[] | tonumber | select(($p[. - 1] // "") | . == "" or startswith(struckMark)) ] | unique | map(tostring) | join(", ")')" [ -z "$dead" ] \ || die3 "update: $id's reasoning has no live paragraph $dead. Read $id.md and count its paragraphs" doc="$(printf '%s' "$doc" | jq --argjson ns "$strikes_json" "$REASONING_JQ"' .reasoning = ((.reasoning | split("\n\n")) | reduce ($ns | map(tonumber) | unique)[] as $n (.; .[$n - 1] = struckMark + .[$n - 1]) | join("\n\n"))')" fi if [ "$set_append" = "true" ]; then local i for ((i = 0; i < $(printf '%s' "$appends_json" | jq length); i++)); do doc="$(reasoning_appended "$doc" "$(printf '%s' "$appends_json" | jq -r --argjson i "$i" '.[$i]')")" done fi if [ "$set_absence" = "true" ]; then local missing missing="$(printf '%s' "$doc" | jq -r --argjson ns "$absences_json" '(.doneWhen // []) as $d | [ $ns[] | tonumber | select(($d[. - 1] | type) != "string") ] | unique | map(tostring) | join(", ")')" [ -z "$missing" ] || die3 "update: $id has no done-when row $missing. Read $id.md and count its rows" doc="$(printf '%s' "$doc" | jq --argjson ns "$absences_json" '(.doneWhen // []) as $d | .absenceReviewed = ([ (.absenceReviewed // [])[] | select(. as $t | any($d[]; . == $t)) ] + [ $ns[] | tonumber | $d[. - 1] ] | unique)')" fi if [ "$set_diffbudget" = "true" ]; then doc="$(printf '%s' "$doc" | jq --arg v "$diff_budget" '.diffBudget = $v')" fi if [ "$set_surfaces" = "true" ]; then doc="$(printf '%s' "$doc" | jq --argjson v "$surfaces_json" '.surfaces = $v')" fi if [ "$set_proof" = "true" ]; then doc="$(printf '%s' "$doc" | jq --arg v "$proof" '.proof = $v')" fi write_atomic "$file" "$doc" echo "UPDATED: $file" [ -z "$struck_list" ] || echo "struck: $struck_list" wo_summary "$doc" implied_proof_line "$doc" "$id" # A proof that is no longer tests leaves the prose fields naming the test files the order # once declared. A reviewer then holds the order to them (live-run row 115). Named, never # refused. A test file is a path segment tests/ or test/, or a name ending .spec., # .test. or Test.php. local still_names if [ "$set_proof" = "true" ] && [ "$proof" != "tests" ]; then still_names="$(printf '%s' "$doc" | jq -r ' [ ("interface", "reasoning", "diffBudget") as $f | select((.[$f] // "") | test("(^|[^A-Za-z0-9_])tests?/|\\.(spec|test)\\.[A-Za-z0-9]+($|[^A-Za-z0-9_])|Test\\.php($|[^A-Za-z0-9_])")) | $f ] | join(", ")')" [ -z "$still_names" ] || echo "stillNamesATest: $still_names" fi render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # add-owned-file, add-done-when, add-test: append one entry each, the same one-call-per-item # shape research-actions.sh's own `record` uses for a finding. # ------------------------------------------------------------------------------------------------ require_wo_id_arg() { # $1 = who, remaining positional args already consumed by the caller's own loop; this just # validates $ID after the caller's own flag parsing has set it. local who="$1" id="$2" is_blank "$id" && die3 "$who: --id is required and must not be blank" id_shape_ok "$id" wo || die3 "$who: --id '$id' is not a valid wo id shape" wo_exists "$id" || die3 "$who: no work order $id in $DESIGN_DIR" } do_add_owned_file() { local id="" path_val="" shared=false while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "add-owned-file" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; --path) need_value "add-owned-file" "--path" "$#" "${2:-}" path_val="$2"; shift 2 ;; --shared) shared=true; shift ;; *) die3 "add-owned-file: unrecognized argument: $1" ;; esac done require_wo_id_arg "add-owned-file" "$id" is_blank "$path_val" && die3 "add-owned-file: --path is required and must not be blank" # A path, never a glob. Implementation derives the test author's denied reads from these entries # and compares them as paths, so a glob would deny nothing while the dispatch still reads as # enforced. Refusing here is where the model finds out; check-design.sh repeats the rule for a # file edited by hand. case "$path_val" in *'*'*|*'?'*|*'['*) die3 "add-owned-file: an owned file is a path and not a glob, and $path_val carries a wildcard. Name the directory, such as src/thing/, or add each file. A glob denies nothing when implementation derives what the test author may not read." ;; esac # A path never begins with a dash. Implementation expands these entries into the argument list of # whatever tool the framework recipe names, so a leading dash reaches that tool as an option, and # a tool with a fixing mode rewrites what it is pointed at. case "$path_val" in -*) die3 "add-owned-file: an owned file never begins with a dash, and $path_val does. Implementation hands these entries to the coding-standards, static-analysis and security tools as arguments, where a leading dash reads as an option and not as a path. Write ./$path_val, or rename the file." ;; esac local file doc file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "add-owned-file: $file exists but is not valid JSON" doc="$(jq --arg p "$path_val" '.ownedFiles = (((.ownedFiles // []) + [$p]) | unique)' "$file")" # --shared marks a file several orders only add to, so another order may own it too when it # also marks it (design-schema.json, sharedFiles). Given again on a path the order owns, it marks # that path. [ "$shared" = "false" ] \ || doc="$(printf '%s' "$doc" | jq --arg p "$path_val" '.sharedFiles = (((.sharedFiles // []) + [$p]) | unique)')" # An order whose every owned file lies under the project folder delivers a document, not code. # So its proof is `record` (nyc defect 17): no test, no commit in the code repository, its # done-when rows judged instead. The task folder's deliverables/ is the usual place; a report # may land beside earlier reports elsewhere in the project folder (live-run row 127). Marked # here, where the files arrive, and only on an order with no proof field. `create` writes one # only when --proof was passed, so a proof design set by hand stays, `tests` included. A code # file added later leaves `record` in place, and the design check names it; `update --proof # tests` is the repair. On a task whose contract says it has no automated tests, an order with # no proof field that owns a code file is a code order with no test to write. So its proof is # `confirm` (gap row 196), marked the same way and at the same moment. An order whose files are # all documents or configuration holds no logic, so it takes `confirm` on any task (gap row 461). local inferred inferred="$(printf '%s' "$doc" | jq -r --arg t "$PROJECT_PATH/" --arg tests "$(automated_tests "$TASK_PATH")" "$BR_ORDER_FACTS_JQ"' if has("proof") then "" elif (.ownedFiles // []) | all(startswith($t)) then "record" elif $tests == "no" then "confirm" elif textOnly then "confirm" else "" end')" [ -z "$inferred" ] || doc="$(printf '%s' "$doc" | jq --arg p "$inferred" '.proof = $p')" [ "$inferred" != "record" ] || echo "proof-set: record, because every owned file of $id lies under the project folder" if [ "$inferred" = "confirm" ] && [ "$(automated_tests "$TASK_PATH")" = "no" ]; then echo "proof-set: confirm, because the task has no automated tests, so a person confirms $id's done-when rows at review" elif [ "$inferred" = "confirm" ]; then echo "proof-set: confirm, because every owned file of $id is a document or configuration, so a reading proves it and no test does" fi case "$path_val" in */) [ "$(printf '%s' "$doc" | jq --arg tests "$(automated_tests "$TASK_PATH")" '(.proof // "") == "" or (.proof == "confirm" and $tests != "no")')" != "true" ] \ || echo "proof-note: a folder reads as code, so the design check treats $id as code. Name each file when it holds only text." ;; esac write_atomic "$file" "$doc" echo "UPDATED: $file" wo_summary "$doc" render_wo "$id" exit 0 } do_add_done_when() { local id="" text="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "add-done-when" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; --text) need_value "add-done-when" "--text" "$#" "${2:-}" text="$2"; shift 2 ;; *) die3 "add-done-when: unrecognized argument: $1" ;; esac done require_wo_id_arg "add-done-when" "$id" is_blank "$text" && die3 "add-done-when: --text is required and must not be blank" local file doc file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "add-done-when: $file exists but is not valid JSON" doc="$(jq --arg t "$text" '.doneWhen = ((.doneWhen // []) + [$t])' "$file")" write_atomic "$file" "$doc" echo "UPDATED: $file" wo_summary "$doc" render_wo "$id" exit 0 } do_add_test() { local id="" level="" description="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "add-test" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; --level) need_value "add-test" "--level" "$#" "${2:-}" level="$2"; shift 2 ;; --description) need_value "add-test" "--description" "$#" "${2:-}" description="$2"; shift 2 ;; *) die3 "add-test: unrecognized argument: $1" ;; esac done require_wo_id_arg "add-test" "$id" is_blank "$description" && die3 "add-test: --description is required and must not be blank" # --level is accepted and optional, and design does not pass one. Selecting a tier belongs to the # stage that writes the test, which is where the framework's own recipe puts it. Design says what # the test must observe. The flag stays so that later stage can record what it chose. local file doc file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "add-test: $file exists but is not valid JSON" if is_blank "$level"; then doc="$(jq --arg d "$description" \ '.tests = ((.tests // []) + [{description: $d}])' "$file")" else doc="$(jq --arg l "$level" --arg d "$description" \ '.tests = ((.tests // []) + [{level: $l, description: $d}])' "$file")" fi write_atomic "$file" "$doc" echo "UPDATED: $file" wo_summary "$doc" render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # remove-test: drops one declared test, named by its description, the one field design writes on # a test. The sizing rules end with a test ceasing to exist. Until this action existed, that edit # ran through jq outside the one producer (live-run row 74). The design check refuses a `tests` # order that owns a machine-verified criterion and declares no test, at the next check or close. # ------------------------------------------------------------------------------------------------ do_remove_test() { local id="" description="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "remove-test" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; --description) need_value "remove-test" "--description" "$#" "${2:-}" description="$2"; shift 2 ;; *) die3 "remove-test: unrecognized argument: $1" ;; esac done require_wo_id_arg "remove-test" "$id" is_blank "$description" && die3 "remove-test: --description is required and must not be blank" local file doc matched file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "remove-test: $file exists but is not valid JSON" matched="$(jq -r --arg d "$description" '[(.tests // [])[] | select(.description == $d)] | length' "$file")" [ "$matched" -gt 0 ] || die3 "remove-test: $id declares no test with the description '$description'" doc="$(jq --arg d "$description" '.tests = [(.tests // [])[] | select(.description != $d)]' "$file")" write_atomic "$file" "$doc" echo "UPDATED: $file" echo "removed-tests: $matched" wo_summary "$doc" render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # remove-done-when and remove-owned-file: the reverse of add-done-when and add-owned-file, the # same move remove-test makes one field over (live-run row 76). A row is named by its exact text, # the way add-done-when wrote it and remove-test names a test. No report numbers a done-when row, # so an index would be a number counted from a rendered list. Every row carrying that text goes, # and the count is printed. A path is named as it was added. The design check refuses an order left # with no owned file, on the schema's own rule: it hands the builder no boundary. On a `record` # order, the inference add-owned-file makes runs again in reverse. An order left with no owned # file under the project folder loses `proof`. It does not stay `record` on the strength of # files it no longer owns. # ------------------------------------------------------------------------------------------------ do_remove_done_when() { local id="" text="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "remove-done-when" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; --text) need_value "remove-done-when" "--text" "$#" "${2:-}" text="$2"; shift 2 ;; *) die3 "remove-done-when: unrecognized argument: $1" ;; esac done require_wo_id_arg "remove-done-when" "$id" is_blank "$text" && die3 "remove-done-when: --text is required and must not be blank" local file doc matched file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "remove-done-when: $file exists but is not valid JSON" matched="$(jq -r --arg t "$text" '[(.doneWhen // [])[] | select(. == $t)] | length' "$file")" [ "$matched" -gt 0 ] || die3 "remove-done-when: $id carries no done-when row with the text '$text'" doc="$(jq --arg t "$text" '.doneWhen = [(.doneWhen // [])[] | select(. != $t)] | if has("absenceReviewed") then .absenceReviewed -= [$t] else . end' "$file")" write_atomic "$file" "$doc" echo "UPDATED: $file" echo "removed-done-when: $matched" wo_summary "$doc" render_wo "$id" exit 0 } do_remove_owned_file() { local id="" path_val="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "remove-owned-file" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; --path) need_value "remove-owned-file" "--path" "$#" "${2:-}" path_val="$2"; shift 2 ;; *) die3 "remove-owned-file: unrecognized argument: $1" ;; esac done require_wo_id_arg "remove-owned-file" "$id" is_blank "$path_val" && die3 "remove-owned-file: --path is required and must not be blank" local file doc file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "remove-owned-file: $file exists but is not valid JSON" jq -e --arg p "$path_val" '((.ownedFiles // []) | index($p)) != null' "$file" >/dev/null 2>&1 \ || die3 "remove-owned-file: $id does not own $path_val" doc="$(jq --arg p "$path_val" '.ownedFiles = [(.ownedFiles // [])[] | select(. != $p)] | if has("sharedFiles") then .sharedFiles -= [$p] else . end' "$file")" local unset_proof unset_proof="$(printf '%s' "$doc" | jq -r --arg t "$PROJECT_PATH/" \ 'if (.proof // "") == "record" and ((.ownedFiles // []) | any(startswith($t)) | not) then "yes" else "" end')" if [ "$unset_proof" = "yes" ]; then doc="$(printf '%s' "$doc" | jq 'del(.proof)')" echo "proof-unset: record, because no owned file of $id lies under the project folder now; every reader takes the order as tests until --proof says otherwise" fi write_atomic "$file" "$doc" echo "UPDATED: $file" echo "removed-owned-file: $path_val" wo_summary "$doc" render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # verify: writes the order's own proof from the knowledge that covers it, the `verify` list, and # is that field's one writer. `--recipe` replaces the list with the covering agentic recipe's # `## Verifier`. Each entry of its `verifier:` block becomes a run entry with its `id`, `kind`, # `run` and `pass`, in any key order, the shape the dev-guides proposal asks every recipe to use. Each numbered item of the # section's prose becomes a check entry, verbatim, wrapped lines joined. A recipe may carry either # form or both. A paragraph is not a check, and no command is ever made from prose. Every entry # cites the recipe and is binding, unless --not-binding says research marked the source as one # this project did not accept. Each `--value` fills a token of the recipe's input contract. The # values are stored on the order under `values`, a repeated name as a list, and the freeze and the # build fill the recipe's lines from them. A name no run line carries is stored with a warning, # because a misspelt name leaves the token unfilled. `--run` or `--check` adds one entry from a research # finding. It cites its source and is never binding, and a second entry with the same text # replaces the first. `--clear` empties the list. A run line carrying a shell character, and a pass outside # the three forms, are refused here, before the gate would refuse them at build time. # ------------------------------------------------------------------------------------------------ # The numbered items of the `## Verifier` prose of the recipe $1, one per line, wrapped lines # joined with a space. Fenced blocks and the `verifier:` block are skipped. The block ends at the # first line that begins with neither a space, a tab nor a dash. verifier_prose_items() { awk ' function flush() { if (item != "") print item; item = "" } /^## / { flush(); inSection = ($0 == "## Verifier"); inFence = 0; inBlock = 0; next } !inSection { next } /^```/ { flush(); inFence = !inFence; next } inFence { next } /^verifier:/ { flush(); inBlock = 1; next } inBlock && /^[^ \t-]/ { inBlock = 0 } inBlock { next } /^[0-9]+\. / { flush(); item = $0; sub(/^[0-9]+\. +/, "", item); sub(/[ \t]+$/, "", item); next } /^[ \t]*$/ { flush(); next } item != "" { line = $0; gsub(/^[ \t]+|[ \t]+$/, "", line); item = item " " line; next } END { flush() } ' "$1" } # One pass form of the three the dev-guides Verifier block allows. $1 the action, $2 the value. verify_pass_ok() { case "$2" in 'exit 0'|'stdout empty'|'stdout contains '?*) ;; *) die3 "$1: a pass is exit 0, stdout empty or stdout contains , got: $2" ;; esac } # One kind of the three the dev-guides Verifier block allows, or none. $1 the action, $2 the value. verify_kind_ok() { case "$2" in ''|config-assert|live-site|self-fixture) ;; *) die3 "$1: a kind is config-assert, live-site or self-fixture, got: $2" ;; esac } # The entry held between lines of the `verifier:` block, the way PC_* holds a precondition. VF_ID=""; VF_KIND=""; VF_RUN=""; VF_PASS=""; VF_ENTRIES='[]' # Appends the held entry to VF_ENTRIES and clears it. $1 the recipe. verify_flush_entry() { [ -n "$VF_ID$VF_KIND$VF_RUN$VF_PASS" ] || return 0 [ -n "$VF_RUN" ] || die3 "verify: the verifier: entry ${VF_ID:-with no id} in $1 holds no run" [ -n "$VF_PASS" ] || die3 "verify: the verifier: entry ${VF_ID:-with no id} in $1 holds no pass. Nothing here guesses what passing means" verify_pass_ok "verify" "$VF_PASS" verify_kind_ok "verify" "$VF_KIND" # A run line with a shell character never runs. One such entry skips that entry, not the recipe. if ! refuse_if_unsafe "design-actions" "$1" "$VF_RUN" 2>/dev/null; then task_warn "$TASK_PATH" "design verify" "$id" "the verifier: entry ${VF_ID:-with no id} in $1 carries a shell character, so it was not carried: $VF_RUN" VF_ID=""; VF_KIND=""; VF_RUN=""; VF_PASS="" return 0 fi VF_ENTRIES="$(printf '%s' "$VF_ENTRIES" | jq -c --arg id "$VF_ID" --arg kind "$VF_KIND" --arg run "$VF_RUN" --arg pass "$VF_PASS" \ '. + [ (if $id == "" then {} else {id: $id} end) + (if $kind == "" then {} else {kind: $kind} end) + {run: $run, pass: $pass} ]')" VF_ID=""; VF_KIND=""; VF_RUN=""; VF_PASS="" } do_verify() { local id="" recipe="" run="" check="" cite="" pass="" kind="" criterion="" binding=true clear=false modes=0 values="" while [ "$#" -gt 0 ]; do cr_take_flag "verify" "--value=values" "$@" || { shift "$?"; continue; } case "$1" in --id) need_value "verify" "--id" "$#" "${2:-}"; id="$2"; shift 2 ;; --criterion) need_value "verify" "--criterion" "$#" "${2:-}"; criterion="$2"; shift 2 ;; --recipe) need_value "verify" "--recipe" "$#" "${2:-}"; recipe="$2"; modes=$((modes + 1)); shift 2 ;; --run) need_value "verify" "--run" "$#" "${2:-}"; run="$2"; modes=$((modes + 1)); shift 2 ;; --check) need_value "verify" "--check" "$#" "${2:-}"; check="$2"; modes=$((modes + 1)); shift 2 ;; --cite) need_value "verify" "--cite" "$#" "${2:-}"; cite="$2"; shift 2 ;; --pass) need_value "verify" "--pass" "$#" "${2:-}"; pass="$2"; shift 2 ;; --kind) need_value "verify" "--kind" "$#" "${2:-}"; kind="$2"; shift 2 ;; --not-binding) binding=false; shift ;; --clear) clear=true; modes=$((modes + 1)); shift ;; *) die3 "verify: unrecognized argument: $1" ;; esac done require_wo_id_arg "verify" "$id" [ "$modes" -eq 1 ] || die3 "verify: pass exactly one of --recipe, --run, --check or --clear" [ -z "$pass" ] || [ -n "$run" ] || die3 "verify: --pass belongs to --run" [ -z "$criterion" ] || [ -n "$run" ] || die3 "verify: --criterion belongs to --run. Only a command's output proves a criterion with no test" [ -z "$kind" ] || [ -n "$run$check" ] || die3 "verify: --kind belongs to --run and --check. A recipe's entries carry their own" verify_kind_ok "verify" "$kind" [ "$binding" = "true" ] || [ -n "$recipe" ] || die3 "verify: --not-binding belongs to --recipe. A research line is never binding" [ -z "$values" ] || [ -n "$recipe" ] || die3 "verify: --value belongs to --recipe. It fills a token of the recipe's input contract" if [ -n "$run$check" ]; then is_blank "$cite" && die3 "verify: --cite is required with --run and --check: every line names the source it came from" else [ -z "$cite" ] || die3 "verify: --cite belongs to --run and --check. A recipe's entries cite the recipe" fi local file doc entries_json source_name before file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "verify: $file exists but is not valid JSON" doc="$(cat "$file")" before="$(printf '%s' "$doc" | jq '(.verify // []) | length')" if [ "$clear" = "true" ]; then doc="$(printf '%s' "$doc" | jq 'del(.verify, .values)')" echo "cleared: $before" elif [ -n "$recipe" ]; then [ -f "$recipe" ] || die3 "verify: no file at $recipe. Give the path the navigator returned" grep -q '^## Verifier[[:space:]]*$' "$recipe" || die3 "verify: $recipe has no ## Verifier section" source_name="$recipe" local block_file state line trimmed item block_file="$(mktemp)" || die3 "verify: could not create a temporary file" state="$(recipe_block_into "$recipe" "Verifier" "verifier" "$block_file")" VF_ID=""; VF_KIND=""; VF_RUN=""; VF_PASS=""; VF_ENTRIES='[]' if [ "$state" = "ok" ]; then # An entry opens at a line beginning with a dash, whatever key that line holds, so the keys # may come in any order. while IFS= read -r line; do case "$line" in ''|[[:space:]-]*) ;; *) break ;; esac trimmed="$(pc_trim "$line")" case "$trimmed" in '- '*) verify_flush_entry "$recipe"; trimmed="$(pc_trim "${trimmed#- }")" ;; esac case "$trimmed" in 'id:'*) VF_ID="$(pc_trim "${trimmed#id:}")" ;; 'kind:'*) VF_KIND="$(pc_trim "${trimmed#kind:}")" ;; 'run:'*) VF_RUN="$(pc_unquote "$(pc_trim "${trimmed#run:}")")" ;; 'pass:'*) VF_PASS="$(pc_unquote "$(pc_trim "${trimmed#pass:}")")" ;; esac done <"$block_file" verify_flush_entry "$recipe" fi rm -f "$block_file" entries_json="$VF_ENTRIES" while IFS= read -r item; do [ -n "$item" ] || continue entries_json="$(printf '%s' "$entries_json" | jq -c --arg c "$item" '. + [{check: $c}]')" done < <(verifier_prose_items "$recipe") [ "$(printf '%s' "$entries_json" | jq 'length')" -gt 0 ] \ || die3 "verify: the ## Verifier of $recipe holds no verifier: entry and no numbered check, so there is nothing to carry" doc="$(printf '%s' "$doc" | jq --argjson e "$entries_json" --arg c "$recipe" --argjson b "$binding" --arg v "$values" \ '.verify = [ $e[] + {cites: $c, binding: $b} ] | ([ $v | split("\n")[] | select(. != "") | split("\t") | {key: .[0], value: .[1]} ] | reduce .[] as $p ({}; .[$p.key] += [$p.value])) as $vals | if $vals == {} then del(.values) else .values = $vals end')" local uncarried uncarried="$(printf '%s' "$doc" | jq -r '[ .verify[] | .run // empty | scan("[{]([^{}]+)[}]") | .[0] ] as $t | [ (.values // {}) | keys[] | select(. as $n | $t | index($n) | not) ] | join(", ")')" [ -z "$uncarried" ] \ || task_warn "$TASK_PATH" "design verify" "$id" "no verify line of $recipe carries these --value names, so nothing reads them: $uncarried" [ "$before" -eq 0 ] || echo "replaced: $before" else local entry if [ -n "$run" ]; then [ -n "$pass" ] || pass="exit 0" verify_pass_ok "verify" "$pass" refuse_if_unsafe "design-actions" "--run" "$run" || die3 "verify: the run line above is refused" entry="$(jq -nc --arg r "$run" --arg p "$pass" --arg c "$cite" '{run: $r, pass: $p, cites: $c, binding: false}')" if [ -n "$criterion" ]; then # The freeze reads the link only on a machine criterion this order owns, so the writer # refuses any other rather than leave a link nothing reads. [ "$(jq -r --argjson wo "$doc" --arg c "$criterion" ' ([ (.criteria // [])[]? | select(type == "object") | select(.verifiedBy == "machine") | .id ] | index($c) != null) and (($wo.criteriaOwned // []) | index($c) != null)' "$ALIGNMENT_FILE" 2>/dev/null)" = "true" ] \ || die3 "verify: $criterion is not a machine-verified criterion $id owns. --criterion names one, and update --criteria-owned comes first" entry="$(printf '%s' "$entry" | jq -c --arg k "$criterion" '. + {criterion: $k}')" fi else entry="$(jq -nc --arg k "$check" --arg c "$cite" '{check: $k, cites: $c, binding: false}')" fi [ -z "$kind" ] || entry="$(printf '%s' "$entry" | jq -c --arg k "$kind" '. + {kind: $k}')" doc="$(printf '%s' "$doc" | jq --argjson e "$entry" ' .verify = ([ (.verify // [])[] | select(((.run // .check) == ($e.run // $e.check)) | not) ] + [$e])')" source_name="$cite" binding=false fi write_atomic "$file" "$doc" echo "UPDATED: $file" printf '%s' "$doc" | jq -r '"verify: " + ([ (.verify // [])[] | select(has("run")) ] | length | tostring) + " run, " + ([ (.verify // [])[] | select(has("check")) ] | length | tostring) + " check"' [ "$clear" = "true" ] || echo "source: $source_name, binding $binding" render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # merge: folds one order into another (SKILL.md, "Size a work order"). Every list field is the # ordered union without duplicates, the survivor's entries first. `interface` and `reasoning` are # appended under a line naming the folded order. A disposition `dispose` wrote on it is not # lost, and a reader can tell which order stated what. That line is a paragraph of its own, so # a folded paragraph marked struck still starts with REASONING_JQ's prefix. Its `dispositions` # entries land after the survivor's, one per candidate with the survivor's kept. The survivor's # verdict file does not name them, so an unattended close asks for a confirmer again. The summary says which scalars were carried # and which were dropped. A live run that saw only list counts read the append as a drop and # rewrote the interface by hand (live-run row 78). `title`, `diffBudget` # and `proof` stay the survivor's. The design check refuses a survivor whose proof does not fit # the tests and rows it gained, such as a `gate` order carrying a test. The folded order's # json and md are removed. Every other order's `dependsOn` naming it is rewritten to the survivor, # without duplicates, and the survivor never depends on itself. The folded id, the survivor and # --reason go into design-removed.json through record_removal, as `remove` writes them (gap row # 245). --reason is required for the same reason `remove` requires it: the close record then says # why the number is missing. Commits nothing, the same as every edit before `close`. # ------------------------------------------------------------------------------------------------ do_merge() { local into="" from="" reason="" while [ "$#" -gt 0 ]; do case "$1" in --into) need_value "merge" "--into" "$#" "${2:-}" into="$2"; shift 2 ;; --from) need_value "merge" "--from" "$#" "${2:-}" from="$2"; shift 2 ;; --reason) need_value "merge" "--reason" "$#" "${2:-}"; reason="$2"; shift 2 ;; *) die3 "merge: unrecognized argument: $1" ;; esac done is_blank "$into" && die3 "merge: --into is required and must not be blank" is_blank "$from" && die3 "merge: --from is required and must not be blank" id_shape_ok "$into" wo || die3 "merge: --into '$into' is not a valid wo id shape" id_shape_ok "$from" wo || die3 "merge: --from '$from' is not a valid wo id shape" [ "$into" != "$from" ] || die3 "merge: --into and --from both name $into" require_reason "merge" "$from" "$reason" wo_exists "$into" || die3 "merge: no work order $into in $DESIGN_DIR" wo_exists "$from" || die3 "merge: no work order $from in $DESIGN_DIR" local into_file from_file into_file="$(wo_file_for "$into")" from_file="$(wo_file_for "$from")" jq empty "$into_file" 2>/dev/null || die3 "merge: $into_file exists but is not valid JSON" jq empty "$from_file" 2>/dev/null || die3 "merge: $from_file exists but is not valid JSON" # The union keeps first occurrence order, so `unique`, which sorts, is not used here. local doc doc="$(jq --slurpfile f "$from_file" --arg from "$from" ' $f[0] as $f | def dedupe: reduce .[] as $x ([]; if any(.[]; . == $x) then . else . + [$x] end); def union(k): if (has(k) or ($f | has(k))) then .[k] = (((.[k] // []) + ($f[k] // [])) | dedupe) else . end; def append(k): if (($f[k] // "") == "" or ($f[k] == .[k])) then . elif ((.[k] // "") == "") then .[k] = "From " + $from + ":\n\n" + $f[k] else .[k] = .[k] + "\n\nFrom " + $from + ":\n\n" + $f[k] end; . as $i | union("criteriaServed") | union("criteriaOwned") | union("nonGoals") | union("dependsOn") | union("ownedFiles") | union("sharedFiles") | union("surfaces") | union("tests") | union("doneWhen") | union("reuses") | union("verify") | union("findings") | union("dispositions") | if has("dispositions") then .dispositions |= reduce .[] as $d ([]; if any(.[]; .candidate == $d.candidate) then . else . + [$d] end) else . end | .dependsOn = [ (.dependsOn // [])[] | select(. != $from and . != $i.id) ] | append("interface") | append("reasoning") ' "$into_file")" local k before after for k in criteriaServed criteriaOwned nonGoals dependsOn ownedFiles sharedFiles surfaces tests doneWhen reuses verify findings dispositions; do before="$(jq -r --arg k "$k" '(.[$k] // []) | length' "$into_file")" after="$(printf '%s' "$doc" | jq -r --arg k "$k" '(.[$k] // []) | length')" echo "$k: $before -> $after" done local carried carried="$(jq -r '[ (if (.interface // "") != "" then "interface" else empty end), (if (.reasoning // "") != "" then "reasoning" else empty end) ] | join(", ")' "$from_file")" echo "carried: ${carried:-none}" echo "dropped: title, diffBudget; the survivor's stand" echo "title: $(jq -r '.title' "$into_file"), the survivor's" write_atomic "$into_file" "$doc" # Every other order that depended on the folded one now depends on the survivor. local f other_id other_doc while IFS= read -r f; do [ -n "$f" ] || continue [ "$f" != "$into_file" ] && [ "$f" != "$from_file" ] || continue jq -e --arg from "$from" '((.dependsOn // []) | index($from)) != null' "$f" >/dev/null 2>&1 || continue other_id="$(jq -r '.id' "$f")" other_doc="$(jq --arg from "$from" --arg into "$into" ' .dependsOn = ((.dependsOn // []) | map(if . == $from then $into else . end) | reduce .[] as $x ([]; if any(.[]; . == $x) then . else . + [$x] end))' "$f")" write_atomic "$f" "$other_doc" echo "REWRITTEN: $f" render_wo "$other_id" done < <(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name '*.json' 2>/dev/null | sort) rm -f "$from_file" "$DESIGN_DIR/$from.md" # Recorded last, so a failed write never leaves a record naming an order that still exists. record_removal "merge" "$from" "$reason" "$into" echo "REMOVED: $from_file" echo "removed: $DESIGN_DIR/$from.md" echo "UPDATED: $into_file" wo_summary "$doc" render_wo "$into" exit 0 } # require_reason : the one refusal of a blank --reason, for `remove` and `merge`. require_reason() { is_blank "$3" && die3 "$1: --reason is required. It says why $2 is gone, for every later reader" return 0 } # record_removal []: appends one entry to design-removed.json, with # mergedInto when a survivor is given. The one writer of that record, for `remove` and `merge`. record_removal() { local record entry if [ -f "$REMOVED_FILE" ]; then record="$(jq -c '.' "$REMOVED_FILE" 2>/dev/null)" || die3 "$1: $REMOVED_FILE exists but is not valid JSON" else record='{"schemaVersion": 1, "removed": []}' fi entry="$(jq -nc --arg id "$2" --arg reason "$3" --arg into "${4:-}" --arg d "$(date -u +%Y-%m-%d)" \ '{id: $id, reason: $reason, removedAt: $d} + (if $into == "" then {} else {mergedInto: $into} end)')" record="$(printf '%s' "$record" | jq --argjson e "$entry" '.removed = ((.removed // []) + [$e])')" write_atomic "$REMOVED_FILE" "$record" echo "RECORDED: $REMOVED_FILE" echo "removedOrders: $(printf '%s' "$record" | jq -r '[.removed[].id] | join(", ")')" } # ------------------------------------------------------------------------------------------------ # remove: deletes an order that no longer earns its place, when a changed contract left it with # nothing to do (gap row 234). The id and the reason go into /design-removed.json, # which `close` carries into design-closed.json, so a reader can tell why the number is missing. # What the removal leaves is caught later, never refused here. Implementation's `start` stops an # order it started for design drift. The design check names a dependsOn that now resolves to # nothing, a criterion left with no serving order or owner, and a finding left unaccounted. # With --merged-into, it records an order that a merge folded before merge wrote this record (gap # row 245). The order is already gone, so it refuses an id that is still an order, or one the # record already holds, and the survivor must be an order. Commits nothing, the same as every # edit before `close`. # ------------------------------------------------------------------------------------------------ do_remove() { local id="" reason="" into="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "remove" "--id" "$#" "${2:-}"; id="$2"; shift 2 ;; --reason) need_value "remove" "--reason" "$#" "${2:-}"; reason="$2"; shift 2 ;; --merged-into) need_value "remove" "--merged-into" "$#" "${2:-}"; into="$2"; shift 2 ;; *) die3 "remove: unrecognized argument: $1" ;; esac done require_reason "remove" "$id" "$reason" if [ -n "$into" ]; then is_blank "$id" && die3 "remove: --id is required and must not be blank" id_shape_ok "$id" wo || die3 "remove: --id '$id' is not a valid wo id shape" id_shape_ok "$into" wo || die3 "remove: --merged-into '$into' is not a valid wo id shape" ! wo_exists "$id" || die3 "remove: $id is still an order. Fold it into $into with merge, which records it" [ ! -f "$REMOVED_FILE" ] || jq -e --arg id "$id" 'all((.removed // [])[]; .id != $id)' "$REMOVED_FILE" >/dev/null 2>&1 \ || die3 "remove: $id is already recorded in $REMOVED_FILE" wo_exists "$into" || die3 "remove: no work order $into in $DESIGN_DIR" record_removal "remove" "$id" "$reason" "$into" exit 0 fi require_wo_id_arg "remove" "$id" local file file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "remove: $file exists but is not valid JSON" local doc_before doc_before="$(cat "$file")" record_removal "remove" "$id" "$reason" rm -f "$file" "$DESIGN_DIR/$id.md" echo "REMOVED: $file" echo "removed: $DESIGN_DIR/$id.md" # The findings the order accounted for are now in no order's list, and `check` names each until # it is accounted for again. echo "findingsLeft: $(printf '%s' "$doc_before" | jq -r '[ (.findings // [])[] | .ref ] | if length == 0 then "none" else join(", ") + ". Account for each again" end')" exit 0 } # ------------------------------------------------------------------------------------------------ # read-guide: records that design opened a guide body, by path, with the body's sha256 and the # UTC date (live-run row 77). --name carries the name research gave the guide, so the entry joins # the finding that named it. One entry per path; a second read of the same path replaces its # entry, the way `dispose` replaces a `reuses` entry, and keeps the name when --name is not passed # again. The path is stored absolute, because a resumed run compares by it. Commits nothing, the # same as every edit before `close`. # ------------------------------------------------------------------------------------------------ do_read_guide() { local path_val="" name="" while [ "$#" -gt 0 ]; do case "$1" in --path) need_value "read-guide" "--path" "$#" "${2:-}"; path_val="$2"; shift 2 ;; --name) need_value "read-guide" "--name" "$#" "${2:-}"; name="$2"; shift 2 ;; *) die3 "read-guide: unrecognized argument: $1" ;; esac done is_blank "$path_val" && die3 "read-guide: --path is required and must not be blank" [ -f "$path_val" ] || die3 "read-guide: no file at $path_val. Give the path the navigator's lookup returned, or the project's own source file" [ -r "$path_val" ] || die3 "read-guide: $path_val exists but is not readable" records_hash__resolve_sha256_cmd || die3 "read-guide: neither sha256sum nor 'shasum -a 256' was found on PATH" local abs sha abs="$(cd -- "$(dirname -- "$path_val")" 2>/dev/null && pwd -P)/$(basename -- "$path_val")" sha="$(file_sha256 "$abs")" [ -n "$sha" ] || die3 "read-guide: could not hash $abs" local doc existed entry if [ -f "$GUIDES_FILE" ]; then jq empty "$GUIDES_FILE" 2>/dev/null || die3 "read-guide: $GUIDES_FILE exists but is not valid JSON" doc="$(cat "$GUIDES_FILE")" else doc='{"schemaVersion": 1, "guides": []}' fi existed="$(printf '%s' "$doc" | jq -r --arg p "$abs" '[(.guides // [])[]? | select(type == "object" and .path == $p)] | length' 2>/dev/null)" # Without --name, the name the earlier entry recorded stays: the join to research's finding # must survive the re-read a resumed run makes. if is_blank "$name"; then name="$(printf '%s' "$doc" | jq -r --arg p "$abs" '[(.guides // [])[]? | select(type == "object" and .path == $p) | .name? // ""] | first // ""' 2>/dev/null)" fi entry="$(jq -nc --arg p "$abs" --arg s "$sha" --arg d "$(date -u +%Y-%m-%d)" --arg n "$name" \ '{path: $p, sha256: $s, readAt: $d} + (if $n == "" then {} else {name: $n} end)')" doc="$(printf '%s' "$doc" | jq --argjson e "$entry" \ '.guides = ([(.guides // [])[]? | select(type == "object" and .path != $e.path)]) + [$e]' 2>/dev/null)" [ -n "$doc" ] || die3 "read-guide: $GUIDES_FILE is valid JSON but not this record's shape; check-design.sh names what is wrong" write_atomic "$GUIDES_FILE" "$doc" echo "RECORDED: $GUIDES_FILE" echo "guide: $abs" echo "sha256: $sha" if [ "${existed:-0}" -gt 0 ]; then echo "entry: updated"; else echo "entry: new"; fi echo "guidesRead: $(printf '%s' "$doc" | jq -r '.guides | length')" exit 0 } # ------------------------------------------------------------------------------------------------ # render: calls design-render.sh directly, for a caller that only wants the markdown refreshed # without changing anything. # ------------------------------------------------------------------------------------------------ do_render() { local id="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "render" "--id" "$#" "${2:-}" id="$2"; shift 2 ;; *) die3 "render: unrecognized argument: $1" ;; esac done require_wo_id_arg "render" "$id" render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # check: calls check-design.sh and maps its exit code onto this script's own table above. # ------------------------------------------------------------------------------------------------ do_check() { [ "$#" -eq 0 ] || die3 "check: unrecognized argument: $1" [ -f "$CHECK_DESIGN_SCRIPT" ] \ || die3 "check: cannot find check-design.sh at $CHECK_DESIGN_SCRIPT" # The report goes to a file and the summary to stdout, so the conversation holds the verdict and # a path rather than the whole report. local rc verdict lines disagrees mkdir -p "$TASK_PATH/records" || die3 "check: could not create $TASK_PATH/records" bash "$CHECK_DESIGN_SCRIPT" "$TASK_PATH" >"$CHECK_FILE" rc=$? case "$rc" in 0) verdict=0 ;; 3) verdict=3 ;; 4) verdict=5 ;; *) die3 "check: check-design.sh exited with an unexpected code $rc" ;; esac local unaccounted unaccounted="$(unaccounted_findings)" local choices choices="$(open_choices)" lines="$(wc -l <"$CHECK_FILE" | tr -d '[:space:]')" echo "action: check" echo "status: $verdict" echo "lines: $lines" echo "report: $CHECK_FILE" drop_closed_critique_rows disagrees="$(proof_disagreement_of "$(cat "$CHECK_FILE")")" echo "impliedProofDisagrees: ${disagrees:-none}" [ -z "$disagrees" ] \ || echo "next: each order above owns a machine-verified criterion after all, or its proof is the gate, the record or the observation" # The orders whose proof cites a source this project did not accept. A person sees them before # the close, which is the design's approval. echo "verifyNotBinding: $(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -r 'select(any((.verify // [])[]; .binding == false)) | .id' "$f" 2>/dev/null; done \ | paste -s -d ',' - | sed 's/,/, /g; s/^$/none/')" # The done-when rows that may join an absence to a behaviour (gap row 209), less the rows a # person marked reviewed with update --absence-reviewed (gap row 237). --absence routes a # clause verbatim, so a joined row cannot go to review without a reopen. A script cannot parse a # clause, so a negation word and an `and` is the whole test, and the line never blocks the close. local joined joined="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -r "$DENIES_JQ"' . as $wo | [ (.doneWhen // []) | to_entries[] | select(.value | type == "string") | select((.value | denies) and (.value | ascii_downcase | test("\\band\\b"))) | select(.value as $v | any(($wo.absenceReviewed // [])[]; . == $v) | not) | .key + 1 | tostring ] | select(length > 0) | $wo.id + (if length == 1 then " row " else " rows " end) + join(", ")' "$f" 2>/dev/null; done \ | paste -s -d ';' - | sed 's/;/; /g')" if [ -n "$joined" ]; then echo "absenceJoined: $joined | best effort: each row holds a negation word and an \"and\". Split a row that joins an absence to a behaviour into two rows. Mark a row that does not with update --absence-reviewed " else echo "absenceJoined: none" fi # The orders whose interface names nothing in backticks (gap row 214). The build's interface # check counts only backtick-quoted names, so it reads unknown on such an order. A script cannot # tell whether prose names a code element, so the line never blocks the close. local unquoted unquoted="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -r ' select((.interface | type) == "string" and (.interface | test("\\S"))) | select(.interface | test("`[^`]+`") | not) | .id' "$f" 2>/dev/null; done \ | paste -s -d ',' - | sed 's/,/, /g')" if [ -n "$unquoted" ]; then echo "interfaceUnquoted: $unquoted | the build brief lists only backtick-quoted names for the builder's record. Quote each exposed element with update --interface" else echo "interfaceUnquoted: none" fi # The paths an order's interface names in backticks that it does not own, that no reuse of it # declares and that no order it depends on owns (gap row 253). The build brief lists # each backticked name for the builder's record, and a builder leaves out a path it never # touched, so the reviewer finds the two texts apart. `ifacePath` decides what a token is as a path. It is known when it is one of those # paths, or lies under one. A script cannot tell a path from prose, so the line never blocks the # close. local unowned unowned="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -c 'select(type == "object")' "$f" 2>/dev/null; done | jq -rs "$IFACE_PATH_JQ"' (map({(.id // ""): (.ownedFiles // [])}) | add // {}) as $owned | .[] | select((.interface | type) == "string") | . as $wo | ([ ($wo.ownedFiles // [])[], ($wo.reuses // [])[].path, (($wo.dependsOn // [])[] | $owned[.] // [] | .[]) ] | map(ltrimstr("./") | rtrimstr("/"))) as $have | [ $wo.interface | scan("`[^`]+`") | ltrimstr("`") | rtrimstr("`") | ifacePath | select(. as $t | any($have[]; . as $h | $t == $h or ($t | startswith($h + "/"))) | not) ] | unique | select(length > 0) | $wo.id + " " + join(", ")' 2>/dev/null \ | paste -s -d ';' - | sed 's/;/; /g')" if [ -n "$unowned" ]; then echo "interfaceUnowned: $unowned | best effort: each path is in backticks in the order's interface, and no owned path, reuse or dependency owner covers it. The builder's record must repeat it. Write it as plain text with update --interface, or add the reuse with dispose --path --interface" else echo "interfaceUnowned: none" fi # The backticked interface names that hold `<` or `{` (gap row 299). Such a name is often a # shape, such as `type.`, and the builder's record matches it only by repeating the shape. # A generic type such as `List` holds `<` too, so the line never blocks the close. local shaped shaped="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -r ' select((.interface | type) == "string") | . as $wo | [ .interface | scan("`[^`]+`") | ltrimstr("`") | rtrimstr("`") | select(test("[<{]")) ] | unique | select(length > 0) | $wo.id + " " + join(", ")' "$f" 2>/dev/null; done \ | paste -s -d ';' - | sed 's/;/; /g')" if [ -n "$shaped" ]; then echo "interfaceShaped: $shaped | best effort: each name in backticks holds < or {, so it may be a shape. The build brief lists it for the builder's record as written, and the reviewer compares the two. Name each real element, or write the shape as plain text with update --interface" else echo "interfaceShaped: none" fi if [ -n "$choices" ]; then echo "interfaceChoice: $(printf '%s\n' "$choices" | paste -s -d ';' - | sed 's/;/; /g') | each pair of names in backticks is joined by or, so it may be a choice the builder makes. A builder that takes one option leaves the other out of its record, and the reviewer compares the two. Write a choice as plain text with update --interface. Leave a pair whose two names must each exist" else echo "interfaceChoice: none" fi # The calls an order's done-when rows and tests make that nothing it declares names (gap row # 231). The test author may not read source, so a signature the brief lacks sent it to a runtime. # A `name()` token is looked for as `name(` in the order's interface, its reuses and the # interfaces of its dependsOn orders. A call a clause denies is named too, so the line never # blocks the close. local undeclared undeclared="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r f; do jq -c 'select(type == "object")' "$f" 2>/dev/null; done | jq -rs ' (map({(.id // ""): (.interface // "")}) | add // {}) as $ifaces | .[] | . as $wo | ([ ($wo.interface // "") ] + [ ($wo.reuses // [])[] | .interface // "" ] + [ ($wo.dependsOn // [])[] | $ifaces[.] // "" ] | join("\n")) as $known | [ ($wo.doneWhen // [])[], ($wo.tests // [])[] | if type == "object" then (.description // "") else tostring end | scan("[A-Za-z_][A-Za-z0-9_]*\\(\\)") ] | unique | map(select(rtrimstr("()") as $n | $known | test("(^|[^A-Za-z0-9_])" + $n + "\\(") | not)) | select(length > 0) | $wo.id + " " + join(", ")' 2>/dev/null \ | paste -s -d ';' - | sed 's/;/; /g')" # The ids below the highest with no order and no record (gap row 245). Only a person knows the # survivor, so the line never blocks the close. local unrecorded unrecorded="$(jq -r '(.graph.unrecordedIds // []) | join(", ")' "$CHECK_FILE" 2>/dev/null)" if [ -n "$unrecorded" ]; then echo "unrecordedIds: $unrecorded | no order file and no entry in design-removed.json. Record each with remove --id --merged-into --reason " else echo "unrecordedIds: none" fi if [ -n "$undeclared" ]; then echo "callsUndeclared: $undeclared | best effort: each call is in a done-when row or a test, and in no interface this order declares. Add the reuse with dispose --path --interface, or answer why the call needs no signature" else echo "callsUndeclared: none" fi # What does not stop here. A person's close writes it to the task record. An unattended close # stops on the unattended lines and on an unaccounted finding. jq -r '(.open.unattended // [])[] | "unattended: " + .' "$CHECK_FILE" 2>/dev/null jq -r '(.open.warn // [])[] | "warning: " + .' "$CHECK_FILE" 2>/dev/null if [ -n "$unaccounted" ]; then printf '%s\n' "$unaccounted" | sed 's/^/unaccounted: /' echo "next: account for each finding above, cited by the order it affects or set aside with a reason" fi if [ "$verdict" -ne 0 ]; then echo "open: $(jq -r '(.open.stop // []) | join("; ")' "$CHECK_FILE" 2>/dev/null)" elif task_is_light "$TASK_PATH"; then # A light task closes with no critique (gap row 197). The clean check is the last step # before the critics, so the skip is decided here. echo "critique: skipped, light run" log_compromise "$TASK_PATH" design "the three design critics" \ "dispatch three critics, on the contract, on reuse and on buildability, and answer their findings before the close" else local closed closed="$(closed_orders)" echo "closedOrders: ${closed:-none}" fi exit "$verdict" } # ------------------------------------------------------------------------------------------------ # close: records what design closed on. Runs check-design.sh against the live files, exactly as # `check` does, and only when that run exits 0 writes /design-closed.json: schemaVersion, # the UTC date, and one hash over alignment.json and every design/*.json together, computed by # records_hash_for (scripts/lib/records-hash.sh, sourced above). Never refuses a second close; # reopening, changing, and closing again is how a design is meant to change once implementation may # already have read the first close (ideal/implementation.md, "Freezing, and what a freeze is for"). # ------------------------------------------------------------------------------------------------ do_close() { [ -n "$RUN_MODE" ] \ || die3 "close: --run-mode is required. The close record says who was present, and that is never assumed" local fit="" fit_path="" fit_reason="" no_recipe=false fit_json="" outcome="" approve_runs=false while [ "$#" -gt 0 ]; do case "$1" in --recipe-fit) need_value "close" "--recipe-fit" "$#" "${2:-}"; fit="$2"; shift 2 ;; --recipe-path) need_value "close" "--recipe-path" "$#" "${2:-}"; fit_path="$2"; shift 2 ;; --recipe-reason) need_value "close" "--recipe-reason" "$#" "${2:-}"; fit_reason="$2"; shift 2 ;; --no-recipe) no_recipe=true; shift ;; --approve-runs) approve_runs=true; shift ;; --critique-outcome) need_value "close" "--critique-outcome" "$#" "${2:-}" is_blank "$2" && die3 "close: --critique-outcome must not be blank" outcome="${outcome:+$outcome }$2"; shift 2 ;; *) die3 "close: unrecognized argument: $1" ;; esac done if [ "$no_recipe" = true ]; then [ -z "$fit$fit_path$fit_reason" ] || die3 "close: --no-recipe means no recipe body was read, so it cannot come with a --recipe-fit" else case "$fit" in true|false|unsure) ;; '') die3 "close: pass --recipe-fit with --recipe-path and --recipe-reason, or --no-recipe when no recipe body was read" ;; *) die3 "close: --recipe-fit must be true, false or unsure, got '$fit'" ;; esac is_blank "$fit_path" && die3 "close: --recipe-path is required with --recipe-fit. It names the recipe body that was judged" is_blank "$fit_reason" && die3 "close: --recipe-reason is required with --recipe-fit. A verdict with no reason cannot be read later" fit_json="$(jq -nc --arg path "$fit_path" --arg fits "$fit" --arg reason "$fit_reason" '{path: $path, fits: $fits, reason: $reason}')" fi [ -f "$CHECK_DESIGN_SCRIPT" ] \ || die3 "close: cannot find check-design.sh at $CHECK_DESIGN_SCRIPT" local check_stderr_file check_report_json check_rc check_stderr_text check_stderr_file="$(mktemp)" || die3 "close: could not create a temporary file" check_report_json="$(bash "$CHECK_DESIGN_SCRIPT" "$TASK_PATH" 2>"$check_stderr_file")" check_rc=$? check_stderr_text="$(cat "$check_stderr_file" 2>/dev/null)" rm -f "$check_stderr_file" case "$check_rc" in 0) # Exit 0 means nothing the check could reach was wrong. It does not mean everything was # reached. A contract it could not read leaves the coverage joins un-run, and a task with no # design/ folder leaves the graph checks un-run, and both still exit 0 because the check # reports what it could not do rather than guessing. Closing on that would freeze a hash over # a design nothing compared against the contract, and implementation refuses to start without # exactly that hash. Unanswered is not a pass (ideal/design.md, "Design's own checks run, they # do not read"), and version 5 required a recorded not_run rather than an un-run check # (stages/07-design-coverage.md). local coverage_checked graph_checked skipped_note coverage_checked="$(printf '%s' "$check_report_json" | jq -r '.coverage.checked // false' 2>/dev/null)" graph_checked="$(printf '%s' "$check_report_json" | jq -r '.graph.checked // false' 2>/dev/null)" skipped_note="" if [ "$coverage_checked" != "true" ]; then skipped_note="$(printf '%s' "$check_report_json" | jq -r '.coverage.note // "the contract could not be read"' 2>/dev/null)" die3 "close: the coverage checks against the contract never ran, so nothing compared this design to the criteria it must serve. Reason: $skipped_note" fi if [ "$graph_checked" != "true" ]; then skipped_note="$(printf '%s' "$check_report_json" | jq -r '.graph.note // "design has not started"' 2>/dev/null)" die3 "close: the checks between work orders never ran, so nothing walked this design's own dependencies. Reason: $skipped_note" fi ;; 4) die5 "close: design has not closed cleanly. Finish design first. Open: $(printf '%s' "$check_report_json" | jq -r '.open.stop | join("; ")')" ;; 3) die3 "close: check-design.sh could not run: $check_stderr_text" ;; *) die3 "close: check-design.sh exited with an unexpected code $check_rc" ;; esac # The removals the record will carry, refused before anything moves when not in their shape. if [ -f "$REMOVED_FILE" ]; then jq -e '(.removed | type) == "array" and all(.removed[]; type == "object" and ((keys - ["mergedInto"]) == ["id", "reason", "removedAt"]) and (.id | type == "string" and test("^wo[1-9][0-9]*$")) and ((has("mergedInto") | not) or (.mergedInto | type == "string" and test("^wo[1-9][0-9]*$"))) and (.reason | type == "string" and length > 0) and (.removedAt | type == "string" and test("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")))' "$REMOVED_FILE" >/dev/null 2>&1 \ || die5 "close: $REMOVED_FILE does not match its shape: a removed list of entries, each with only id, reason, removedAt and an optional mergedInto" fi # The findings a person reads at the close, as warnings in the task record. Unattended, nobody # reads them then, so the ones a person would stop on stop the close instead. local unattended_lines warn_lines unattended_lines="$(printf '%s' "$check_report_json" | jq -r '(.open.unattended // [])[]')" warn_lines="$(printf '%s' "$check_report_json" | jq -r '(.open.warn // [])[]')" [ "$RUN_MODE" != "autonomous" ] || [ -z "$unattended_lines" ] \ || die5 "close: this close is unattended, and it stops on what a person would read first: $(printf '%s' "$unattended_lines" | paste -s -d ';' - | sed 's/;/; /g')" # Each disposition nobody watched is read against the confirmer's verdict file (gap row 293). A # missing or other value is a warning: the builder and the architecture reviewer both read the # reused interface, and both stop on a misfit. local wo_json wo_id disp mode cand verdict_file got while IFS= read -r wo_json; do [ -n "$wo_json" ] || continue wo_id="$(jq -r '.id' "$wo_json")" verdict_file="$TASK_PATH/records/disposition-$wo_id.json" while IFS="$(printf '\t')" read -r disp mode cand; do [ "$mode" != "interactive" ] || continue got="$(jq -r --arg c "$cand" '.[$c] // empty' "$verdict_file" 2>/dev/null)" if [ -z "$got" ]; then warn_lines="$warn_lines $wo_id reuses $cand as $disp, made unattended, and no confirmer verdict is in $verdict_file" elif [ "$got" != "$disp" ]; then warn_lines="$warn_lines $wo_id reuses $cand as $disp, made unattended, and the confirmer answered $got" fi done < <(standing_dispositions "$wo_json") done < <(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort) local unaccounted unaccounted="$(unaccounted_findings)" if [ -n "$unaccounted" ]; then [ "$RUN_MODE" != "autonomous" ] \ || die7 "close: a research finding is in no work order's findings, and this close is unattended. Account for each one with account, cited or set aside with a reason: $unaccounted" warn_lines="$warn_lines $(printf '%s\n' "$unaccounted" | sed 's/^/a research finding is in no work order: /')" fi # A run line that is not binding was written by a model from research, and only a person stands # between it and the worktree. The person approves such lines at this close, the design's # approval, and the stamp lands on each entry before the hash covers it. A line with no stamp # never runs: the build hands it to the reviewer as a check. # The yes is the person's answer in the task's answer record, in every run mode: step design # close, no order, and as its words each line this close would stamp, one per line (gap row 370). local wo_file approved_count=0 n_approved to_stamp if [ "$approve_runs" = true ]; then to_stamp="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort \ | while IFS= read -r wo_file; do jq -r '(.verify // [])[] | select(has("run") and .binding == false and (has("approved") | not)) | .run' "$wo_file" done)" [ -z "$to_stamp" ] \ || task_require_person 70 close "--approve-runs, a yes to the commands research wrote," "$TASK_PATH" "" "design close" "" "$to_stamp" fi if [ "$approve_runs" = true ]; then while IFS= read -r wo_file; do [ -n "$wo_file" ] || continue n_approved="$(jq '[ (.verify // [])[] | select(has("run") and .binding == false and (has("approved") | not)) ] | length' "$wo_file")" [ "$n_approved" -gt 0 ] || continue write_atomic "$wo_file" "$(jq --arg d "$(date -u +%Y-%m-%d)" \ '.verify = [ .verify[] | if has("run") and .binding == false and (has("approved") | not) then .approved = {by: "person", on: $d} else . end ]' "$wo_file")" render_wo "$(jq -r '.id' "$wo_file")" >/dev/null approved_count=$((approved_count + n_approved)) done < <(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sort) echo "approvedRuns: $approved_count" task_answer_consume # The check ran before the stamp, so its lines on an unstamped run line no longer hold. unattended_lines="$(printf '%s' "$check_report_json" | jq -r ' [ .coverage.unstampedRunProofs[]? | "order " + .id + " rests criterion " + .criterionId + " " ] as $stamped | (.open.unattended // [])[] | . as $l | select(any($stamped[]; . as $p | $l | startswith($p)) | not)')" fi local hash hash="$(records_hash_for "$TASK_PATH")" \ || die3 "close: could not compute the records hash for $TASK_PATH" # An interactive close happens in front of the person who has just read the rendered design, so # the person is the one closing. An autonomous close has nobody to be that person, and saying so # is the whole value of the field. local closed_at closed_by doc closed_at="$(date -u +%Y-%m-%d)" if [ "$RUN_MODE" = "autonomous" ]; then closed_by="nobody"; else closed_by="person"; fi doc="$(jq -n --arg closedAt "$closed_at" --arg hash "$hash" \ --arg runMode "$RUN_MODE" --arg closedBy "$closed_by" --arg pluginVersion "$(plugin_version)" \ '{schemaVersion: 1, pluginVersion: $pluginVersion, closedAt: $closedAt, runMode: $runMode, closedBy: $closedBy, hash: $hash}')" [ -z "$fit_json" ] || doc="$(printf '%s' "$doc" | jq --argjson rf "$fit_json" '.recipeFit = $rf')" # Each finished critique moves out of `records/`, which the project ignores, and into the design # folder, which this close commits. The record used to cite the paths under `records/`. A reader # a month later, on another branch or another machine, found a count and three paths the # repository never held (live-run row 176). A critique is not reproducible: the critics run at # opus, so a second dispatch answers with a different table. On an unattended close nobody read # the findings at the time either, and the count was then the whole surviving statement. The move # runs before the commit below, so one commit carries the record and the evidence it names. The # design folder is the committed place the stage already writes: `design-render.sh` puts # `design/.md` there beside every order. A work order id can never take this name. # # An unfinished critique moves too, under an `unfinished-` prefix (gap row 190). It is no more # reproducible than a finished one, and it is the only copy of what that critic wrote before it # stopped. The close does not count it: it is not evidence the close judged, and a findings total # read from it would be invented. The prefix keeps it out of the `design-critique-*.md` pattern. # So the count loop below skips it. And the design skill still routes a bare `close` to the # critique step when no finished file is there. local crit_file crit_dest moved_finished=false drop_closed_critique_rows while IFS= read -r crit_file; do [ -n "$crit_file" ] || continue if critique_findings_of "$crit_file" >/dev/null; then crit_dest="$DESIGN_DIR/$(basename -- "$crit_file")" moved_finished=true else crit_dest="$DESIGN_DIR/unfinished-$(basename -- "$crit_file")" printf 'close: %s has no findings line, so its critic did not finish; it moves to %s and is not counted\n' "$crit_file" "$crit_dest" >&2 fi # A directory at the destination takes the move inside itself, and `mv` exits 0. The count # loop below then reads one file fewer. The record names fewer files than the close carried, # with a total nothing on disk adds up to. Nothing in AIDA makes such a directory, so this # needs a hand. It is still the one shape where the record and the disk disagree in silence. [ ! -e "$crit_dest" ] || [ -f "$crit_dest" ] \ || die3 "close: $crit_dest is not a file, so the critique $crit_file cannot move there. Remove it and close again" mv -f -- "$crit_file" "$crit_dest" || die3 "close: could not move $crit_file to $crit_dest" done < <(find "$TASK_PATH/records" -mindepth 1 -maxdepth 1 -type f -name 'design-critique-*.md' 2>/dev/null | sort) # The critique files this close carries: their paths, and the total of their `findings: N` last # lines. So the close says what was read before it. Absent when no critic ran. A file without # that line was not finished by its critic, and a count read from it would be invented, so that # file is left out of the record and named on stderr. The critique blocks nothing, which is the # design: a critic that can stop a close trains a design that writes for the critic. Beside the # count sits `outcome`, the --critique-outcome line: how the findings were answered, in the # person's words, one line per flag. A count alone said nothing about what changed (live-run row # 135). `none` when no flag was passed and no earlier answer carries, below. Unattended, the # flag holds the model's answer, recorded the same way. A second close reads the files the first close # moved and records them again, so the record still names what is on disk. local critique_files critique_total crit_n critique_files=""; critique_total=0 while IFS= read -r crit_file; do [ -n "$crit_file" ] || continue crit_n="$(critique_findings_of "$crit_file")" || { printf 'close: %s has no findings line, so its critic did not finish; it is not counted\n' "$crit_file" >&2 continue } critique_total=$((critique_total + crit_n)) critique_files="$critique_files$crit_file " done < <(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'design-critique-*.md' 2>/dev/null | sort) if [ -n "$critique_files" ]; then doc="$(printf '%s' "$doc" | jq --arg files "$critique_files" --argjson n "$critique_total" --arg outcome "${outcome:-none}" \ '.critique = {files: ($files | split("\n") | map(select(length > 0))), findings: $n, outcome: $outcome}')" elif [ -n "$outcome" ]; then die3 "close: --critique-outcome names how the critique's findings were answered, and no finished critique file is under $TASK_PATH/records or $DESIGN_DIR to record it beside" fi # A re-close with no new critique and no flag keeps the answer the last close recorded (gap row # 323). The files and the count must match that record, so the answer is to these findings. # `carriedFrom` names the close that holds the answer, and its hash, which is the design the # person answered over. A later change to an order is then visible as a hash that differs. local carried="" if [ -n "$critique_files" ] && [ -z "$outcome" ] && [ "$moved_finished" = false ] && [ -f "$CLOSED_FILE" ]; then carried="$(jq -c --argjson now "$(printf '%s' "$doc" | jq -c '.critique')" ' def names: map(sub(".*/"; "")) | sort; select((.critique.outcome // "none") != "none" and (.critique.files | names) == ($now.files | names) and .critique.findings == $now.findings) | {outcome: .critique.outcome, carriedFrom: (.critique.carriedFrom // {closedAt, closedBy, hash})}' \ "$CLOSED_FILE" 2>/dev/null)" fi # An order added, removed or merged since the answer is one the critics never read. The answer # is still carried, and the difference is a warning. The order ids the answered close held come # from its own commit. if [ -n "$carried" ]; then local answered_ids now_ids answered_ids="$(answered_order_ids "$(printf '%s' "$carried" | jq -r '.carriedFrom.hash')")" || answered_ids="unknown" now_ids="$(find "$DESIGN_DIR" -mindepth 1 -maxdepth 1 -type f -name 'wo*.json' 2>/dev/null | sed 's|.*/||; s|\.json$||' | sort)" [ "$answered_ids" = "$now_ids" ] || warn_lines="$warn_lines the critique answer is carried from a close that held the orders $(printf '%s' "$answered_ids" | paste -s -d ' ' -), and the design now holds $(printf '%s' "$now_ids" | paste -s -d ' ' -)" fi if [ -n "$carried" ]; then doc="$(printf '%s' "$doc" | jq --argjson c "$carried" '.critique += $c')" outcome="$(printf '%s' "$carried" | jq -r '.outcome')" fi # The orders `remove` deleted and `merge` folded, with the reason each went, so the close says why a number is missing. if [ -f "$REMOVED_FILE" ]; then doc="$(printf '%s' "$doc" | jq --slurpfile r "$REMOVED_FILE" '.removed = $r[0].removed')" fi local line while IFS= read -r line; do [ -z "$line" ] || task_warn "$TASK_PATH" "design close" "" "$line" done < <(printf '%s\n%s\n' "$unattended_lines" "$warn_lines") write_atomic "$CLOSED_FILE" "$doc" # The stage boundary: the task folder is committed, with the order count the check just # walked and who closed as the reason. Closing again commits again, over the new hash. commit_stage_close "$TASK_PATH" design "Close design for $(jq -r '.id' "$TASK_PATH/task.json")" \ "$(printf '%s' "$check_report_json" | jq -r '.files | length | if . == 1 then "1 work order" else "\(.) work orders" end'), closed by $closed_by" echo "CLOSED: $CLOSED_FILE" echo "closedBy: $closed_by" echo "runMode: $RUN_MODE" echo "hash: $hash" [ -z "$critique_files" ] || printf '%s\n' "${outcome:-none}" | sed 's/^/critiqueOutcome: /' [ -z "$carried" ] || printf '%s' "$carried" \ | jq -r '.carriedFrom | "critiqueCarriedFrom: the close of \(.closedAt) by \(.closedBy), over hash \(.hash)"' exit 0 } # ------------------------------------------------------------------------------------------------ # dispose: records a reuse decision on a work order by table, never by the model's own reasoning # (ideal/design.md, "The reuse decision lands here"; version 5's prior-art-disposition.sh). The # caller gives the candidate, its closeness, the cost dimensions compared, the verdict and why. The # table decides what stands, and the outcome lands in `reasoning`, the write `update` makes. # # `--path` and `--interface`, given together, add one entry to the order's `reuses`: where the # reused thing lives and the shape it exposes. The tests brief carries that list, because the test # author may not open production source and a reused module belongs to no work order (live-run # row 69). A second dispose naming the same path replaces its entry, and `reasoning` gains one # paragraph per call, so every candidate's verdict survives. Neither flag, and the order's # `reuses` is left as it was. # # Each call also writes the outcome to the order's `dispositions`, one entry per candidate. # Unattended, it prints `confirmFile:`, the order's verdict file, which maps each candidate to the # confirmer's value, and `worktree:`, where the confirmer reads the cited files (gap row 356). An unattended `close` counts the dispositions against that file, because one # confirmer once judged the last of six paragraphs and the other five stood (gap row 293). # # The table. Rows are tried in order and the first that applies decides. Extend is the downgrade # because it removes nothing. A reuse or extend citing no cost has nothing to downgrade to, so it # stands and the thin reasoning is recorded for a person to see (version 5's rule). A decline # compares nothing: the candidate was weighed and set aside, and the reason says what was weighed. # It is a recorded decision, not a downgrade, so it stands in both modes (live-run row 79). # distance cost cited verdict mode outcome # any any decline any stands: nothing compared; the reason names what was weighed # any none recognised reuse|extend any stands: nothing to downgrade to; the reasoning says no cost was cited # any none recognised supersede any extend: a supersede naming no cost compared nothing # any build only supersede any extend: build is paid once, carry, agent and risk forever # same-layer any supersede any extend: sharing only a layer, it is not absorbed; a second implementation # name|dir recurring supersede attended stands: the skill asked the person once; it widens the task and owes a migration # name|dir recurring supersede unattended extend: no person to ask; re-surfaces on the next attended run # any recognised reuse|extend any stands # ------------------------------------------------------------------------------------------------ do_dispose() { [ -n "$RUN_MODE" ] || die3 "dispose: --run-mode is required. The table reads it, and it is never assumed" local id="" candidate="" distance="" cost="" verdict="" why="" reuse_path="" reuse_interface="" while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "dispose" "--id" "$#" "${2:-}"; id="$2"; shift 2 ;; --path) need_value "dispose" "--path" "$#" "${2:-}"; reuse_path="$2"; shift 2 ;; --interface) need_value "dispose" "--interface" "$#" "${2:-}"; reuse_interface="$2"; shift 2 ;; --candidate) need_value "dispose" "--candidate" "$#" "${2:-}"; candidate="$2"; shift 2 ;; --distance) need_value "dispose" "--distance" "$#" "${2:-}"; distance="$2"; shift 2 ;; --cost) need_value "dispose" "--cost" "$#" "${2:-}"; cost="$2"; shift 2 ;; --verdict) need_value "dispose" "--verdict" "$#" "${2:-}"; verdict="$2"; shift 2 ;; --why) need_value "dispose" "--why" "$#" "${2:-}"; why="$2"; shift 2 ;; *) die3 "dispose: unrecognized argument: $1" ;; esac done require_wo_id_arg "dispose" "$id" is_blank "$candidate" && die3 "dispose: --candidate is required and must not be blank" is_blank "$why" && die3 "dispose: --why is required and must not be blank" case "$verdict" in reuse|extend|supersede|decline) ;; *) die3 "dispose: --verdict must be reuse, extend, supersede or decline, got '${verdict:-}'" ;; esac [ "$verdict" = "decline" ] || { is_blank "$cost" && die3 "dispose: --cost is required and must not be blank"; } if ! is_blank "$reuse_path" || ! is_blank "$reuse_interface"; then [ "$verdict" = "decline" ] && die3 "dispose: a decline reuses nothing, so --path and --interface do not apply" is_blank "$reuse_path" && die3 "dispose: --interface was given without --path. The brief needs both: where the reused thing lives and what it exposes" is_blank "$reuse_interface" && die3 "dispose: --path was given without --interface. The brief needs both: where the reused thing lives and what it exposes" fi case "$distance" in same-name|same-directory|same-layer) ;; *) die3 "dispose: --distance must be same-name, same-directory or same-layer, got '${distance:-}'" ;; esac # Which cost classes were cited. An unknown class never counts, so "vibes" clears no bar. local known=false recurring=false dim while IFS= read -r dim; do case "$dim" in build) known=true ;; carry|agent|risk) known=true; recurring=true ;; esac done < <(printf '%s\n' "$cost" | tr ',' '\n') local outcome="$verdict" rule="stands" if [ "$verdict" = "decline" ]; then rule="stands: nothing compared; the candidate is set aside and the reason names what was weighed" elif [ "$known" != "true" ] && [ "$verdict" != "supersede" ]; then rule="stands: no recognised cost dimension cited; a $verdict has nothing to downgrade to" elif [ "$known" != "true" ]; then outcome=extend; rule="downgraded: a supersede with no cost dimension cited compared nothing" elif [ "$verdict" = "supersede" ]; then if [ "$recurring" != "true" ]; then outcome=extend; rule="downgraded: a supersede resting on build cost alone; build is paid once, carry, agent and risk forever" elif [ "$distance" = "same-layer" ]; then outcome=extend; rule="downgraded: a candidate sharing only a layer is not absorbed by a replacement" elif [ "$RUN_MODE" = "interactive" ]; then rule="stands: the supersede was recorded in an interactive run" else outcome=extend; rule="downgraded: a supersede with no person present; it re-surfaces on the next attended run" fi fi local file doc file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "dispose: $file exists but is not valid JSON" doc="$(reasoning_appended "$(cat "$file")" "Candidate $candidate ($distance). Proposed $verdict, citing ${cost:-nothing}. Disposition: $outcome ($rule). $why")" if [ -n "$reuse_path" ]; then doc="$(printf '%s' "$doc" | jq --arg p "$reuse_path" --arg i "$reuse_interface" \ '.reuses = ((.reuses // []) | map(select(.path != $p))) + [{path: $p, interface: $i}]')" fi # What stood for this candidate before this call, read before the write below. local before verdict_file held before="$(standing_dispositions "$file" | c="$candidate" awk -F'\t' '$3 == ENVIRON["c"] { print $1 }')" doc="$(printf '%s' "$doc" | jq --arg c "$candidate" --arg o "$outcome" --arg m "$RUN_MODE" ' {candidate: $c, disposition: $o, runMode: $m} as $e | .dispositions = (.dispositions // []) | if any(.dispositions[]; .candidate == $c) then .dispositions |= map(if .candidate == $c then $e else . end) else .dispositions += [$e] end')" write_atomic "$file" "$doc" # The confirmer's value for this candidate stays when it agrees with the new outcome, and when # the outcome did not change, so a disagreement is never cleared by asking again. A new outcome # it did not judge drops the value, and only this candidate's. verdict_file="$TASK_PATH/records/disposition-$id.json" held="$(jq -r --arg c "$candidate" '.[$c] // empty' "$verdict_file" 2>/dev/null)" if [ -n "$held" ] && [ "$held" != "$outcome" ] && [ "$before" != "$outcome" ]; then write_atomic "$verdict_file" "$(jq --arg c "$candidate" 'del(.[$c])' "$verdict_file")" fi [ "$outcome" = "$verdict" ] \ || task_warn "$TASK_PATH" "design dispose" "$id" "the supersede of $candidate is recorded as extend: ${rule#downgraded: }" echo "DISPOSED: $file" echo "proposed: $verdict" echo "disposition: $outcome" if [ "$RUN_MODE" = "autonomous" ]; then echo "confirmFile: $verdict_file" echo "worktree: $(jq -r '.worktree.path // "none"' "$TASK_PATH/task.json" 2>/dev/null)" fi echo "reuses: $(printf '%s' "$doc" | jq -r '(.reuses // []) | length')" wo_summary "$doc" render_wo "$id" exit 0 } # ------------------------------------------------------------------------------------------------ # account: records what the order --id does with one research finding. The entry holds the # reference and the finding's text, read from research/.json, and --set-aside's reason # when given. A second account of the same reference on the same order replaces its entry. # ------------------------------------------------------------------------------------------------ do_account() { local id="" ref="" set_aside="" aside_given=false remove=false while [ "$#" -gt 0 ]; do case "$1" in --id) need_value "account" "--id" "$#" "${2:-}"; id="$2"; shift 2 ;; --finding) need_value "account" "--finding" "$#" "${2:-}"; ref="$2"; shift 2 ;; --set-aside) need_value "account" "--set-aside" "$#" "${2:-}"; set_aside="$2"; aside_given=true; shift 2 ;; --remove) remove=true; shift ;; *) die3 "account: unrecognized argument: $1" ;; esac done require_wo_id_arg "account" "$id" [ "$aside_given" = false ] || ! is_blank "$set_aside" \ || die3 "account: --set-aside must not be blank. The reason is what a reader judges the set-aside by" [ "$remove" = false ] || [ "$aside_given" = false ] || die3 "account: --remove takes no --set-aside" local file doc file="$(wo_file_for "$id")" jq empty "$file" 2>/dev/null || die3 "account: $file exists but is not valid JSON" if [ "$remove" = true ]; then jq -e --arg r "$ref" 'any((.findings // [])[]; .ref == $r)' "$file" >/dev/null \ || die3 "account: $id holds no entry $ref" doc="$(jq --arg r "$ref" '.findings = [ .findings[] | select(.ref != $r) ]' "$file")" write_atomic "$file" "$doc" echo "REMOVED: $ref from $file" echo "findings: $(printf '%s' "$doc" | jq -r '.findings | length')" render_wo "$id" exit 0 fi parse_finding_ref account "$ref" "$TASK_PATH/research" \ || die3 "account: no research file $TASK_PATH/research/$FINDING_REF_SEARCH.json" local search="$FINDING_REF_SEARCH" n="$FINDING_REF_N" local rfile="$TASK_PATH/research/$search.json" text count count="$(jq -r '(.findings // []) | length' "$rfile" 2>/dev/null)" text="$(jq -r --argjson i "$((n - 1))" '(.findings // [])[$i].text // empty' "$rfile" 2>/dev/null)" [ -n "$text" ] || die3 "account: $rfile holds no finding $n. It holds ${count:-0}, numbered from 1 as research/$search.md shows them" doc="$(jq --arg r "$ref" --arg t "$text" --arg a "$set_aside" \ '.findings = ((.findings // []) | map(select(.ref != $r))) + [{ref: $r, text: $t} + (if $a == "" then {} else {setAside: $a} end)]' "$file")" write_atomic "$file" "$doc" echo "ACCOUNTED: $file" echo "finding: $ref" echo "text: $(printf '%s' "$text" | sed -n '1p')" if [ -n "$set_aside" ]; then echo "use: set aside"; else echo "use: cited"; fi echo "findings: $(printf '%s' "$doc" | jq -r '.findings | length')" render_wo "$id" exit 0 } # Reads the sidecar the distiller wrote after `close`; the read is distill_read in task-helpers.sh. # A light task's one distiller wrote a sidecar for each of the three stages, so each is read under # its own `stage:` line (distill_deferred). do_distill() { [ "$#" -eq 0 ] || die3 "distill: unrecognized argument: $1" if task_is_light "$TASK_PATH"; then local stage for stage in scope research design; do echo "stage: $stage" distill_read "$TASK_PATH" "$stage" done else distill_read "$TASK_PATH" design fi exit 0 } # ------------------------------------------------------------------------------------------------ # Dispatch # ------------------------------------------------------------------------------------------------ # A text argument may be @/, read from that file (task-helpers.sh, text_args). text_args "$@" set -- ${TEXT_ARGS[@]+"${TEXT_ARGS[@]}"} ACTION="${1:-}" if [ "$ACTION" = "-h" ] || [ "$ACTION" = "--help" ]; then usage exit 0 fi [ -n "$ACTION" ] || { usage; die3 "no action given"; } shift TASK_FOLDER_ARG="${1:-}" [ -n "$TASK_FOLDER_ARG" ] || { usage; die3 "$ACTION: a task folder is required"; } shift TASK_PATH="$(resolve_task_folder "$TASK_FOLDER_ARG" "$ACTION")" RESOLVE_RC=$? [ "$RESOLVE_RC" -eq 0 ] || exit "$RESOLVE_RC" # The project folder, two levels up, where commit_stage_close sends the stage commit. A record # order's files lie under it (live-run row 127). PROJECT_PATH="$(dirname -- "$(dirname -- "$TASK_PATH")")" ALIGNMENT_FILE="$TASK_PATH/alignment.json" DESIGN_DIR="$TASK_PATH/design" CLOSED_FILE="$TASK_PATH/design-closed.json" GUIDES_FILE="$TASK_PATH/design-guides-read.json" REMOVED_FILE="$TASK_PATH/design-removed.json" CHECK_FILE="$TASK_PATH/records/design-check.json" RESEARCH_CHECK_FILE="$TASK_PATH/records/research-check.json" case "$ACTION" in read) do_read "$@" ;; start) do_start "$@" ;; create) do_create "$@" ;; update) do_update "$@" ;; add-owned-file) do_add_owned_file "$@" ;; add-done-when) do_add_done_when "$@" ;; add-test) do_add_test "$@" ;; remove-test) do_remove_test "$@" ;; remove-done-when) do_remove_done_when "$@" ;; remove-owned-file) do_remove_owned_file "$@" ;; merge) do_merge "$@" ;; remove) do_remove "$@" ;; read-guide) do_read_guide "$@" ;; render) do_render "$@" ;; check) do_check "$@" ;; close) do_close "$@" ;; dispose) do_dispose "$@" ;; account) do_account "$@" ;; distill) do_distill "$@" ;; verify) do_verify "$@" ;; *) usage; die3 "unknown action: $ACTION" ;; esac