# shellcheck shell=bash # --json reports (docs/AGENT-1.0-PLAN.md §4): with --json, every command prints one # crosscode.script-report/1 object on stdout when it ends, and its usual output goes to stderr. # The agent reads the object, adds the log lines and sends it to the Hub. # # What goes in: each step() as a step (the next step, or the end, closes it), report_result # key/values, report_status (rolled-back), and the last die() message. Only names and outcomes: # never values from .env files. REPORT_FILE="" REPORT_COMMAND="" REPORT_STARTED="" now_iso() { date -u +%Y-%m-%dT%H:%M:%S.000Z; } # report_init : starts collecting (only with --json). report_init() { REPORT_FILE="$(mktemp "${TMPDIR:-/tmp}/crosscode-report.XXXXXX")" REPORT_COMMAND="$1" REPORT_STARTED="$(now_iso)" } # report_line : one tab-separated line (tabs and newlines in fields become spaces). report_line() { [ -n "$REPORT_FILE" ] || return 0 local out="$1" f shift for f in "$@"; do f="${f//$'\t'/ }" out+=$'\t'"${f//$'\n'/ }" done printf '%s\n' "$out" >> "$REPORT_FILE" } # report_result : a string. report_result_json : any JSON value. report_result() { report_line result "$1" "$2"; } report_result_json() { report_line resultjson "$1" "$2"; } # report_status rolled-back: how the command ended, when it isn't just succeeded / failed. report_status() { report_line status "$1"; } # report_step_outcome [detail]: the outcome of the step that's running (default ok). report_step_outcome() { report_line outcome "$1" "${2:-}"; } # report_emit : prints the report on fd 3 (the real stdout) and removes the file. report_emit() { [ -n "$REPORT_FILE" ] || return 0 node -e ' const fs = require("fs"); const [file, command, startedAt, code] = process.argv.slice(1); const finishedAt = new Date().toISOString().replace(/\.\d{3}Z$/, ".000Z"); const steps = [], result = {}; let status = null, message; for (const line of fs.readFileSync(file, "utf8").split("\n")) { if (!line) continue; const [kind, a = "", b = ""] = line.split("\t"); if (kind === "step") { const last = steps[steps.length - 1]; if (last && !last.finishedAt) last.finishedAt = a; steps.push({ name: b.slice(0, 200), startedAt: a, finishedAt: null, outcome: "ok" }); } else if (kind === "outcome" && steps.length) { const last = steps[steps.length - 1]; last.outcome = a; if (b) last.detail = b.slice(0, 2000); } else if (kind === "result") result[a] = b; else if (kind === "resultjson") { try { result[a] = JSON.parse(b); } catch { result[a] = b; } } else if (kind === "status") status = a; else if (kind === "message") message = a.slice(0, 2000); } const last = steps[steps.length - 1]; if (last && !last.finishedAt) last.finishedAt = finishedAt; const failed = code !== "0"; if (failed && last && last.outcome === "ok") last.outcome = "failed"; const out = { schema: "crosscode.script-report/1", command, status: status || (failed ? "failed" : "succeeded"), startedAt, finishedAt, steps, result }; if (message) out.message = message; process.stdout.write(JSON.stringify(out) + "\n"); ' "$REPORT_FILE" "$REPORT_COMMAND" "$REPORT_STARTED" "$1" >&3 || true rm -f "$REPORT_FILE" REPORT_FILE="" }