fix: expose runner source fetch progress

This commit is contained in:
AgentRun Codex
2026-07-20 09:33:56 +02:00
parent 0a404e8897
commit 116ee16775
6 changed files with 385 additions and 1 deletions
@@ -0,0 +1,29 @@
const managerUrl = process.env.AGENTRUN_MGR_URL;
const runnerJobId = process.env.AGENTRUN_RUNNER_JOB_ID;
const phase = process.argv[2];
const attempt = Number(process.argv[3]);
if (!managerUrl || !runnerJobId || !phase || !Number.isSafeInteger(attempt) || attempt < 1) process.exit(2);
const body = {
phase,
attempt,
runId: process.env.AGENTRUN_RUN_ID,
commandId: process.env.AGENTRUN_COMMAND_ID,
attemptId: process.env.AGENTRUN_ATTEMPT_ID,
runnerId: process.env.AGENTRUN_RUNNER_ID,
};
if (process.argv[4]) body.code = process.argv[4];
if (process.argv[5]) body.summary = process.argv[5];
const headers = { "content-type": "application/json" };
if (process.env.AGENTRUN_API_KEY) headers.authorization = `Bearer ${process.env.AGENTRUN_API_KEY}`;
const response = await fetch(new URL(`/api/v1/runner-jobs/${encodeURIComponent(runnerJobId)}/boot-observations`, managerUrl), {
method: "POST",
headers,
body: JSON.stringify(body),
});
const envelope = await response.json();
if (!response.ok || envelope?.ok !== true) {
process.stderr.write(`${JSON.stringify({ ok: false, event: "agentrun-boot-report", status: response.status, failureKind: envelope?.failureKind ?? "infra-failed", message: envelope?.message ?? "manager boot observation failed" })}\n`);
process.exit(1);
}
process.stdout.write(`${JSON.stringify(envelope.data)}\n`);
@@ -0,0 +1,65 @@
#!/bin/sh
set -eu
entrypoint="${1:-src/runner/main.ts}"
repo_url="${AGENTRUN_BOOT_REPO_URL:-http://git-mirror-http.devops-infra.svc.cluster.local/pikasTech/agentrun.git}"
commit="${AGENTRUN_BOOT_COMMIT:-${AGENTRUN_SOURCE_COMMIT:-}}"
app_root="${AGENTRUN_APP_ROOT:-/workspace/agentrun}"
reporter=/opt/agentrun/deploy/runtime/boot/agentrun-boot-report.mjs
if [ -z "$commit" ] || ! printf '%s' "$commit" | grep -Eq '^[0-9a-f]{40}$'; then
printf '{"ok":false,"event":"agentrun-boot","failureKind":"source-commit-invalid","message":"AGENTRUN_BOOT_COMMIT must be a full git SHA"}\n' >&2
exit 64
fi
mkdir -p "$(dirname "$app_root")"
rm -rf "$app_root"
mkdir -p "$app_root"
cd "$app_root"
git init -q
git remote add origin "$repo_url"
attempt=1
fetch_log=/tmp/agentrun-boot-fetch.log
while :; do
start_response=$(node "$reporter" source-fetch-started "$attempt" 2>/tmp/agentrun-boot-report.log || true)
timeout_ms=$(printf '%s' "$start_response" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{const n=JSON.parse(s).attemptTimeoutMs;process.stdout.write(Number.isFinite(n)?String(n):"")}catch{}})')
timeout_seconds=""
if [ -n "$timeout_ms" ]; then timeout_seconds=$(( (timeout_ms + 999) / 1000 )); fi
if { [ -n "$timeout_seconds" ] && timeout "${timeout_seconds}s" git fetch --depth=1 origin "$commit" >"$fetch_log" 2>&1; } || { [ -z "$timeout_seconds" ] && git fetch --depth=1 origin "$commit" >"$fetch_log" 2>&1; }; then
node "$reporter" source-fetch-succeeded "$attempt" >/tmp/agentrun-boot-report.log 2>&1 || true
break
fi
message=$(tail -n 20 "$fetch_log" | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g')
failure_kind=git-mirror-fetch-failed
if printf '%s' "$message" | grep -Eiq 'not our ref|unadvertised|Server does not allow request'; then
failure_kind=git-mirror-exact-commit-unavailable
elif printf '%s' "$message" | grep -Eiq 'Could not resolve host|Connection timed out|Failed to connect|timed out'; then
failure_kind=git-mirror-network-failed
elif printf '%s' "$message" | grep -Eiq 'Authentication failed|Permission denied|repository.*not found'; then
failure_kind=git-mirror-auth-failed
fi
failure_response=$(node "$reporter" source-fetch-failed "$attempt" "$failure_kind" 2>/tmp/agentrun-boot-report.log || true)
action=$(printf '%s' "$failure_response" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{process.stdout.write(JSON.parse(s).action||"")}catch{}})')
if [ "$action" != "retry" ]; then
printf '{"ok":false,"event":"agentrun-boot","failureKind":"%s","repoUrl":"%s","commit":"%s","message":%s}\n' "$failure_kind" "$repo_url" "$commit" "$(node -e 'console.log(JSON.stringify(process.argv[1] || ""))' "$message")" >&2
exit 65
fi
backoff_ms=$(printf '%s' "$failure_response" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{const n=JSON.parse(s).backoffMs;process.stdout.write(Number.isFinite(n)?String(n):"0")}catch{process.stdout.write("0")}})')
sleep_seconds=$(node -e 'process.stdout.write(String(Math.max(0, Number(process.argv[1]) || 0) / 1000))' "$backoff_ms")
sleep "$sleep_seconds"
attempt=$((attempt + 1))
done
git checkout -q --detach "$commit"
actual=$(git rev-parse HEAD)
if [ "$actual" != "$commit" ]; then
printf '{"ok":false,"event":"agentrun-boot","failureKind":"source-commit-mismatch","expected":"%s","actual":"%s"}\n' "$commit" "$actual" >&2
exit 66
fi
ln -sfn /opt/agentrun/node_modules "$app_root/node_modules"
printf '{"ok":true,"event":"agentrun-boot","repoUrl":"%s","commit":"%s","entrypoint":"%s","nodeModules":"/opt/agentrun/node_modules"}\n' "$repo_url" "$commit" "$entrypoint"
exec bun "$entrypoint"
+1 -1
View File
@@ -1,3 +1,3 @@
#!/bin/sh
set -eu
exec /opt/agentrun/deploy/runtime/boot/agentrun-boot.sh src/runner/main.ts
exec /opt/agentrun/deploy/runtime/boot/agentrun-runner-boot.sh src/runner/main.ts