Files
crosscode-platform/scripts/lib/report.sh
T
joshuaandClaude Opus 5.5 e9e9b5a97f crosscode: --json reports, --config, env templates, frpc tunnels, restart/export/uninstall/purge
- Every command takes --json (crosscode.script-report/1 on stdout, the
  log on stderr) and --config (the .env values by layer, from the agent).
- .env files come from the release's env.template.json (lib/envtpl.js):
  written whole on install; on deploy only new variables are appended,
  and a new required one with no value stops the deploy at the checks.
- install --frp: Hub-given ports, a marked frpc.ini block (backup,
  frpc verify, reload; [common] admin lines added once), no nginx.
  A database on another server via DB_ADMIN_URL. Refuses an existing
  database or role. Everything the run made is removed if it fails.
- restart, export (crosscode.church-export/1 ZIP), uninstall (final
  backup, uploads and .env kept 30 days) and purge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 12:50:19 +02:00

79 lines
3.3 KiB
Bash

# 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 <command>: starts collecting (only with --json).
report_init() {
REPORT_FILE="$(mktemp "${TMPDIR:-/tmp}/crosscode-report.XXXXXX")"
REPORT_COMMAND="$1"
REPORT_STARTED="$(now_iso)"
}
# report_line <kind> <fields…>: 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 <key> <value>: a string. report_result_json <key> <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 <outcome> [detail]: the outcome of the step that's running (default ok).
report_step_outcome() { report_line outcome "$1" "${2:-}"; }
# report_emit <exit code>: 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=""
}