Files
crosscode-platform/scripts/lib/report.sh
T
joshuaandClaude Opus 5.5 bbcafe61b9 Events 3.4.0, Accounts 1.4.0, contracts 1.12.0, agent 1.3.0: jobs from the dashboard, styled Accounts emails, .env from the Hub
- Events and Accounts: Background jobs are switched on or off and rescheduled from the admin;
  the choice is kept in the database (job_overrides), wins over the .env and applies at once.
  Accounts gets an Admin -> Background jobs page and per-job env flags, as Events.
- Accounts: every email styled as Cross Events' (church colour, logo, button, footer), same
  plain text (src/lib/emails.ts).
- Contracts 1.12.0: env-read / env-set agent jobs, envReadResultSchema, isSecretEnvName.
- crosscode script: `crosscode env <church> <product>` reads the .env (secrets hidden) and, with
  --set/--unset/--restart, changes one file, rebuilds the frontend when its file changed, reloads,
  and puts the file back if the site doesn't answer (lib/envedit.cjs, tests/envedit.bats).
- Agent 1.3.0: runs env-read and env-set.

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

79 lines
3.4 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 (except `crosscode env`, the Hub's env-read: non-secret values only).
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=""
}