#!/usr/bin/env bash # dev-tasks.sh — the DEV side of the agent task exchange. Local only, no network. # # DEV is the executor of tasks that Production puts in each site's # `/ЗАДАЧИ/dev-inbox/`. DEV reads them here, closes those it executes # (`ack … done`) into that site's `dev-ledger.jsonl`, and can raise its own # follow-up tasks for Production (`new …`) in the site's `prod-inbox/`. # The site registry is the SEO project's sites.json (read-only). set -euo pipefail ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" REG="${DEV_TASKS_REGISTRY:-/var/www/seo/sites.json}" [ -f "$REG" ] || { echo "dev-tasks: registry not found: $REG" >&2; exit 2; } die() { echo "dev-tasks: $*" >&2; exit 2; } now_iso() { date -u +%Y-%m-%dT%H:%M:%SZ; } today() { date -u +%Y%m%d; } now_date() { date -u +%Y-%m-%d; } UID_RE='^(dev|prod|seo|user):[0-9]{8}-[a-z0-9-]{2,80}$' KEY_RE='^[a-z0-9][a-z0-9:/._-]{2,120}$' site_ids() { jq -r '.sites | keys[]' "$REG"; } site_field() { jq -er --arg id "$1" ".sites[\$id] | $2" "$REG" 2>/dev/null; } site_list_field() { jq -r --arg id "$1" ".sites[\$id] | $2 // empty" "$REG" 2>/dev/null; } jlog_append() { # $1 file, $2 json line (exclusive lock) local f="$1" line="$2" mkdir -p "$(dirname -- "$f")" if command -v flock >/dev/null 2>&1; then ( flock -w 10 9 || { echo "dev-tasks: journal lock timeout: $f" >&2; exit 4; } printf '%s\n' "$line" >> "$f" ) 9>>"$f" else printf '%s\n' "$line" >> "$f" fi chmod 644 -- "$f" 2>/dev/null || true } task_field() { sed -n "s/^$2: //p" "$1" | head -n1; } # dev_find -> "site|path" dev_find() { local uid="$1" site dir f printf '%s' "$uid" | grep -Eq "$UID_RE" || return 1 local id="${uid#*:}" while IFS= read -r site; do dir="$(site_list_field "$site" '.dev_inbox')"; [ -n "$dir" ] || continue f="$(ls "$dir/$id.md" 2>/dev/null | head -n1 || true)" [ -n "$f" ] || f="$(ls "$dir/"*"$id"*.md 2>/dev/null | head -n1 || true)" if [ -n "$f" ] && [ ! -L "$f" ]; then printf '%s|%s' "$site" "$(realpath -e -- "$f")"; return 0; fi done < <(site_ids) return 1 } ledger_last() { # $1 ledger, $2 uid [ -f "$1" ] || return 0 jq -rc --arg u "$2" 'select(.uid==$u)' "$1" 2>/dev/null | tail -n1 } cmd_list() { local only="" all=0 a while [ "$#" -gt 0 ]; do case "$1" in --all) all=1; shift;; *) only="$1"; shift;; esac; done local sites=(); if [ -n "$only" ]; then sites=("$only"); else while IFS= read -r s; do sites+=("$s"); done < <(site_ids); fi local site f for site in "${sites[@]}"; do local dir led; dir="$(site_list_field "$site" '.dev_inbox')"; led="$(site_list_field "$site" '.dev_ledger')" [ -n "$dir" ] && [ -d "$dir" ] || continue for f in "$dir"/*.md; do [ -e "$f" ] || continue [ -L "$f" ] && continue local uid origin parent prio st last uid="$(task_field "$f" uid)"; [ -n "$uid" ] || uid="prod:$(basename -- "${f%.md}")" origin="$(task_field "$f" origin)"; parent="$(task_field "$f" parent)"; prio="$(task_field "$f" prio)" st="open"; last="$(ledger_last "$led" "$uid")"; [ -n "$last" ] && st="$(printf '%s' "$last" | jq -r '.status')" if [ "$all" = 0 ]; then case "$st" in done|rejected) continue;; esac; fi printf '%s\t%s\t%s\t%s\t%s\n' "$site" "$uid" "${prio:-normal}" "$st" "${origin:-prod}" done done | head -c 20480 } cmd_show() { local uid="${1:-}"; [ -n "$uid" ] || die "show: a uid is required" local hit; hit="$(dev_find "$uid")" || die "unknown task uid: $uid" local f="${hit#*|}" [ "$(stat -c%s -- "$f")" -le 20480 ] || die "task file exceeds 20 KB" cat -- "$f" } cmd_ack() { local uid="${1:-}"; shift || true local status="${1:-}"; shift || true case "$status" in taken|done|rejected) ;; *) die "ack: status must be taken|done|rejected";; esac local note="" changed="" while [ "$#" -gt 0 ]; do case "$1" in --note) note="${2:-}"; shift 2;; --changed) changed="${2:-}"; shift 2;; *) die "ack: unknown option '$1'";; esac done case "$note" in *$'\n'*|*$'\r'*) die "ack: --note must be a single line";; esac if printf '%s' "$note" | LC_ALL=C grep -q '[[:cntrl:]]'; then die "ack: --note has control characters"; fi [ "${#note}" -le 300 ] || die "ack: --note exceeds 300 characters" [ "${#changed}" -le 300 ] || die "ack: --changed exceeds 300 characters" local hit; hit="$(dev_find "$uid")" || die "unknown task uid: ${uid:-}" local site="${hit%%|*}" f="${hit#*|}" local led; led="$(site_field "$site" '.dev_ledger')" local last lastst="" lastts="" last="$(ledger_last "$led" "$uid")" if [ -n "$last" ]; then lastst="$(printf '%s' "$last" | jq -r '.status')"; lastts="$(printf '%s' "$last" | jq -r '.ts' | cut -c1-10)"; fi if [ "$lastst" = "done" ]; then case "$status" in taken|done) die "уже выполнена $lastts";; esac; fi if [ "$status" = "done" ] && [ -z "$changed" ]; then die "ack: done requires --changed"; fi local key; key="$(task_field "$f" key)" local line line="$(jq -cn --arg ts "$(now_iso)" --arg uid "$uid" --arg key "$key" --arg status "$status" \ --arg note "$note" --arg changed "$changed" \ '{ts:$ts,uid:$uid,key:$key,status:$status,note:$note,changed:$changed}')" jlog_append "$led" "$line" echo "ok ack $uid $status (site $site)" } cmd_new() { local site="${1:-}"; shift || true [ -n "$site" ] || die "new: a prod site id is required" site_field "$site" '.prod_inbox' >/dev/null 2>&1 || die "unknown site: $site" local key="" prio=normal type=code url="" needs=no vmetric="" vafter="" parent="" obs="" doer="" doner="" while [ "$#" -gt 0 ]; do case "$1" in --key) key="${2:-}"; shift 2;; --prio) prio="${2:-}"; shift 2;; --type) type="${2:-}"; shift 2;; --url) url="${2:-}"; shift 2;; --needs-code) needs="${2:-}"; shift 2;; --verify-metric) vmetric="${2:-}"; shift 2;; --verify-after) vafter="${2:-}"; shift 2;; --parent) parent="${2:-}"; shift 2;; --observed-file) obs="${2:-}"; shift 2;; --do-file) doer="${2:-}"; shift 2;; --done-file) doner="${2:-}"; shift 2;; *) die "new: unknown option '$1'";; esac done printf '%s' "$key" | grep -Eq "$KEY_RE" || die "new: --key must look like ::" case "$prio" in high|normal) ;; *) die "new: --prio must be high or normal";; esac case "$type" in code|content|data) ;; *) die "new: --type must be code, content or data";; esac [ -n "$vafter" ] || die "new: --verify-after is required" printf '%s' "$vafter" | grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' || die "new: --verify-after must be YYYY-MM-DD" [ -z "$parent" ] || printf '%s' "$parent" | grep -Eq "$UID_RE" || die "new: --parent must be a uid" local read_field; read_field() { local f="${1:-}"; [ -n "$f" ] || return 0; [ -f "$f" ] || die "file not found: $f" [ -L "$f" ] && die "refusing a symlink: $f"; local a; a="$(realpath -e -- "$f")" case "$a" in "$ROOT"/*) ;; *) die "text files must live inside the DEV project";; esac [ "$(stat -c%s -- "$a")" -le 4000 ] || die "field file exceeds 4000 bytes: $f" printf '%s' "$(<"$a")" } local OBS DOER DONER; OBS="$(read_field "$obs")"; DOER="$(read_field "$doer")"; DONER="$(read_field "$doner")" local inbox; inbox="$(site_field "$site" '.prod_inbox')" mkdir -p "$inbox" local d n=1 d="$(today)" while ls "$inbox/$d-$(printf '%02d' "$n")"-*.md >/dev/null 2>&1; do n=$((n+1)); [ "$n" -le 99 ] || die "daily id overflow"; done local cnt; cnt="$(ls "$inbox"/$d-*.md 2>/dev/null | wc -l)" || true # No daily cap by default; set DEV_TASKS_MAX to a positive number to re-enable. if [ "${DEV_TASKS_MAX:-0}" -gt 0 ] && [ "$cnt" -ge "${DEV_TASKS_MAX:-0}" ]; then die "limit: already $cnt tasks today for $site (max $DEV_TASKS_MAX)" fi local slug; slug="$(printf '%s' "$key" | tr 'A-Z' 'a-z' | tr -c 'a-z0-9' '-' | sed 's/-\{2,\}/-/g; s/^-//; s/-$//' | cut -c1-60)" local tid="$d-$(printf '%02d' "$n")"; local uid="dev:$tid"; local chain="$uid"; [ -n "$parent" ] && chain="$parent" local f="$inbox/$tid-$slug.md" { printf -- '---\n' printf 'uid: %s\n' "$uid" printf 'origin: dev\n' printf 'author: dev\n' printf 'target: prod\n' printf 'parent: %s\n' "$parent" printf 'chain: %s\n' "$chain" printf 'key: %s\n' "$key" printf 'site: %s\n' "$site" printf 'date: %s\n' "$(now_date)" printf 'prio: %s\n' "$prio" printf 'type: %s\n' "$type" printf 'status: open\n' printf 'url: %s\n' "$url" printf 'needs_code: %s\n' "$needs" printf 'verify_metric: %s\n' "$vmetric" printf 'verify_after: %s\n' "$vafter" printf -- '---\n\n' printf '## Что наблюдается (факты)\n\n%s\n\n' "$OBS" printf '## Влияние / зачем\n\n%s\n\n' "поставлено DEV: origin=dev, parent=${parent:-нет}" printf '## Что сделать (для Production)\n\n%s\n\n' "$DOER" printf '## Критерий готовности и проверка\n\n%s\n\n' "$DONER" printf '## Журнал задачи\n\n%s создана DEV (key=%s)\n' "$(now_iso)" "$key" } >"$f" chmod 644 -- "$f" echo "ok new $uid $key -> $f" } cmd_sent() { # [prod-site] [--all] — tasks DEV raised for Production, status from prod-ledger local only="" all=0 a while [ "$#" -gt 0 ]; do case "$1" in --all) all=1; shift;; *) only="$1"; shift;; esac; done local sites=(); if [ -n "$only" ]; then sites=("$only"); else while IFS= read -r s; do sites+=("$s"); done < <(site_ids); fi local site f for site in "${sites[@]}"; do local inbox led; inbox="$(site_list_field "$site" '.prod_inbox')"; led="$(site_list_field "$site" '.prod_ledger')" [ -n "$inbox" ] && [ -d "$inbox" ] || continue for f in "$inbox"/*.md; do [ -e "$f" ] || continue [ -L "$f" ] && continue local uid origin prio st last uid="$(task_field "$f" uid)"; [ -n "$uid" ] || uid="dev:$(basename -- "${f%.md}")" origin="$(task_field "$f" origin)"; prio="$(task_field "$f" prio)" st="open"; last="$(ledger_last "$led" "$uid")"; [ -n "$last" ] && st="$(printf '%s' "$last" | jq -r '.status')" if [ "$all" = 0 ]; then case "$st" in done|rejected) continue;; esac; fi printf '%s\t%s\t%s\t%s\t%s\n' "$site" "$uid" "${prio:-normal}" "$st" "${origin:-dev}" done done | head -c 20480 } cmd="${1:-}"; shift || true case "$cmd" in list) cmd_list "$@" ;; show) cmd_show "$@" ;; ack) cmd_ack "$@" ;; new) cmd_new "$@" ;; sent) cmd_sent "$@" ;; *) echo "usage: dev-tasks.sh {list [prod-site] [--all]|show |ack taken|done|rejected [--note \"...\"] [--changed \"...\"]|new --key K --prio high|normal --type code|content|data --verify-after YYYY-MM-DD [--url P] [--parent ] [--observed-file F] [--do-file F] [--done-file F]|sent [prod-site] [--all]}" >&2; exit 2;; esac