Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/2791-native-worker-owner-decisions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `scripts/ccc-self-update.sh`, `scripts/lib/a2a-native-worker-stale.sh`, `scripts/a2a-termux-native-worker-deploy.sh` (#2791, owner decisions 2026-11-27): (1) the per-node opt-in for the idle-gated supervise-loop restart can also be the operator-owned file `~/.claude/self-update.native-supervisor-restart` (exists → on); `CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART`, when set, wins over the file (`0` keeps a node off despite it), the same precedence as `self-update.notify`. (3) The supervise loop also counts as stale when the `lib/a2a-supervisor-identity.sh` it sourced changed after it started (latest of script and lib, each `max(mtime, ctime)`); the line then carries `changed=<lib>` before `file=<script>`, which stays last, so existing parsers and the self-update restart match are unchanged. A missing lib is ignored. (4) The deploy dry-run now runs the `--apply` pre-restart checks — build revision, the artifact's rollout guard (`--deployed`) and `--smoke-cmd` — against a temp preview of the post-deploy worker root (live root minus `node_modules`/`dist.bak*`, deploy dirs dereferenced, artifact copied over, unchanged packages symlinked); a failure exits 20 before the idle gate with nothing changed. Busy still never forces a restart and hung children are still only reported.
32 changes: 25 additions & 7 deletions docs/a2a-claude-worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,15 @@ overrides the default cap of 1.
Serving≠installed warning (#2791): every run also compares the supervise
loop and each `$A2A_WORKER_ROOT/dist/worker.js` worker with the file it runs.
When that file is newer than the process (or gone) the health log gets
`worker-stale: kind=<supervise|worker> pids=… started=… installed=… file=…`,
`worker-stale: kind=<supervise|worker> pids=… started=… installed=… [changed=…] file=…`,
the `OK` line ends with `stale=<kinds>` (plus a `WARN worker-stale:` line
unless `--quiet`), and `--json` carries `"stale": [...]` (`[]` when current).
It is a warning only — the rc does not change and nothing is restarted; a
redeployed `worker.js` or changed supervise script still needs a manual
supervisor restart. Process age comes from `CLOCK_BOOTTIME`, because Android
supervisor restart. "Newer" is the later of mtime and ctime. The supervise
loop is also stale when the `lib/a2a-supervisor-identity.sh` it sourced changed
after it started; `changed=<lib>` then names it (`file=` stays the script, last
on the line). Process age comes from `CLOCK_BOOTTIME`, because Android
denies `/proc/uptime` and `/proc/stat` to Termux.

Idle-gated restart (#2791 stage 2b). Both commands refuse without `--if-idle`
Expand Down Expand Up @@ -234,7 +237,10 @@ Who restarts what (owner decision 2026-11-27): a redeployed `worker.js` is
restarted by the deploy side (`scripts/a2a-termux-native-worker-deploy.sh`);
self-update only warns about it. Self-update may restart a stale supervise loop
of its own checkout only on nodes that opt in with
`CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART=1` (default 0, docs/self-update.md).
`CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART=1` or the operator-owned file
`~/.claude/self-update.native-supervisor-restart` (default off; a set env var
wins over the file, docs/self-update.md). A worker that stays busy is never
force-restarted, and hung children are only reported (`hung=N`).

### Deploying `dist/worker.js` (#2791 stage 3)

Expand All @@ -243,7 +249,7 @@ the worker artifact. It replaces the per-rollout ad-hoc scripts and owns the
worker restart after a redeploy. It is **dry-run by default**:

```bash
# Plan + idle gate only; nothing on the node changes.
# Plan + pre-restart checks on a temp preview + idle gate; nothing on the node changes.
scripts/a2a-termux-native-worker-deploy.sh --env-file /path/canonical.env \
--artifact /path/artifact.tgz --target-sha <40-hex>
# Perform it.
Expand All @@ -253,7 +259,19 @@ scripts/a2a-termux-native-worker-deploy.sh --env-file /path/canonical.env \

`--artifact` is a directory or a tar(.gz) whose top level (or one wrapping
directory) holds `dist/worker.js`; tar members with absolute paths, `..`,
device nodes or escaping links are refused. With `--apply` the order is:
device nodes or escaping links are refused.

The dry-run also runs step 4's checks (build revision, the shipped rollout
guard, `--smoke-cmd`) against a preview of the post-deploy root in a private
temp dir under `$TMPDIR`, removed on exit (owner decision 2026-11-27). The
preview is the live root without `node_modules/` and `dist.bak*`, with
`dist/`, `scripts/` and `handlers/` copied dereferenced (a symlinked release
`dist/` is never written through) and the artifact copied over it;
`node_modules/` holds the artifact's packages plus symlinks to the other live
packages, so a smoke command must not write into `node_modules`. A failing
check exits 20 before the idle gate, with nothing changed.

With `--apply` the order is:

1. idle gate — `restart-worker --if-idle --dry-run` (busy → exit 10, nothing changed);
2. backups of the worker root (without `node_modules`) and of each
Expand All @@ -271,13 +289,13 @@ device nodes or escaping links are refused. With `--apply` the order is:

| rc | meaning |
|----|----------------------------------------------------------------------|
| 0 | deployed and verified (dry-run: plan + gate ok) |
| 0 | deployed and verified (dry-run: plan + preview checks + gate ok) |
| 2 | usage, invalid artifact, or revision ≠ `--target-sha` |
| 3 | no canonical supervisor / worker — nothing changed |
| 4 | another deploy holds the lock |
| 10 | busy — nothing changed (or files rolled back before any signal) |
| 11 | idle state unknown — nothing changed |
| 20 | pre-restart check failed — rolled back, no restart |
| 20 | pre-restart check failed — rolled back, no restart (dry-run: failed on the preview, nothing changed) |
| 30 | restart not verified — rolled back and restarted on the old files |
| 31 | rollback failed — restore manually from the printed backups |

Expand Down
11 changes: 9 additions & 2 deletions docs/self-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ rather than composing the steps ad hoc.
5-minute `a2a-termux-native-worker-health.sh` cron reports the same state
(docs/a2a-claude-worker.md). A stale `worker.js` is never restarted here —
that belongs to the deploy side (`scripts/a2a-termux-native-worker-deploy.sh`).
Opt-in per node: with `CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART=1`, a stale
For the supervise loop, "the file it runs" also covers the
`lib/a2a-supervisor-identity.sh` it sourced at start-up: a changed lib alone
makes the loop stale, and the line then names it as `changed=<lib>` before
`file=<script>` (owner decision 2026-11-27).
Opt-in per node: with `CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART=1`, or the
operator-owned file `~/.claude/self-update.native-supervisor-restart`
(exists → on; the env var, when set, wins — `0` keeps the node off despite
the file, the same precedence as `self-update.notify`), a stale
supervise loop running this checkout's own script is restarted through
`a2a-termux-native-worker.sh restart-supervisor --if-idle` (line
`action=restart-supervisor`, then `native-supervisor-restart result=<ok|deferred-busy|deferred-unknown|failed> rc=…`).
Expand Down Expand Up @@ -466,7 +473,7 @@ still does not include a complete previous dependency environment.
| `CCC_SELF_UPDATE_REAPPLY` | `1` | set to `0` to skip installer cron re-apply; equivalent operator file: `~/.claude/self-update.no-reapply` |
| `CCC_SELF_UPDATE_A2A_HANDLERS` | `1` | set to `0` to skip refreshing drifted installed A2A handler copies (#2357) |
| `CCC_SELF_UPDATE_NATIVE_WORKER_STALE_CHECK` | `auto` | Termux-native A2A worker serving≠installed log line (step 5c, #2791): `auto` = Termux only, `1` = always, `0` = off. Never restarts or notifies |
| `CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART` | `0` | Opt-in (step 5c, #2791 stage 2b): `1` restarts a stale supervise loop of this checkout via `restart-supervisor --if-idle`; busy defers. Never restarts for a stale `worker.js` |
| `CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART` | unset (off) | Opt-in (step 5c, #2791 stage 2b): `1` restarts a stale supervise loop of this checkout via `restart-supervisor --if-idle`; busy defers. Never restarts for a stale `worker.js`. Equivalent operator file: `~/.claude/self-update.native-supervisor-restart` (exists → on, used only while the env var is unset/empty; `0` overrides it) |
| `CCC_SELF_UPDATE_NATIVE_SUPERVISOR_RESTART_TIMEOUT` | `300` | Seconds the opt-in supervisor restart may take before it is killed (the health cron respawns a missing supervisor) |
| `CCC_A2A_HANDLER_SYSTEM_DEST` | `/usr/local/sbin` | system destination of the installed A2A handlers checked by the refresh step and by `ccc-doctor` (tests point it at a temp dir); the home destination is always `$HOME/a2a-broker-worker` |
| `CCC_SELF_UPDATE_NOTIFY` | `all` | which owner notices are queued (#2589). `all` (also any unknown value): every notice. `failures`, and `none`/`off` (same behaviour): only the success notices — update completed (`ok-*`) and pending activation reconciled (`reconciled-*`) — are muted, each logged as `notify=suppressed reason=operator-notify-<mode> dedup=… class=info`; every failure, stall, warning, recovery and incomplete activation (e.g. `prepare-fail-*`, `fail-*`, `pending-*`, `stalled-*`) is still pushed and logged as `notify=pushed mode=<mode> class=alert`. `silent`: deliberate full mute — alerts are suppressed too (`class=alert` in the log line) and every run logs `notify=warning mode=silent` and warns on stderr. `status` prints the effective mode. Equivalent operator file: `~/.claude/self-update.notify` (first non-comment line) |
Expand Down
117 changes: 91 additions & 26 deletions scripts/a2a-termux-native-worker-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@
# [--target-sha <40-hex>] [--backup-dir <dir>] [--smoke-cmd <cmd>]
# [--no-guard] [--apply]
#
# Dry-run by default: validates the artifact, prints the plan and runs the idle
# gate (`restart-worker --if-idle --dry-run`) — nothing on the node changes.
# Dry-run by default: validates the artifact, prints the plan, runs the step-4
# pre-restart checks (revision, the artifact's rollout guard, --smoke-cmd)
# against a throw-away preview of the post-deploy worker root, then runs the
# idle gate (`restart-worker --if-idle --dry-run`) — nothing on the node
# changes. The preview lives in a private mktemp dir under $TMPDIR (removed on
# exit): the live root without node_modules/ and dist.bak* (the root backup's
# scope; dist/ scripts/ handlers/ dereferenced) with the artifact copied over
# it, and node_modules/ holding the artifact's packages plus symlinks to every
# other live package. A failing check exits 20 with nothing changed (owner
# decision 2026-11-27: the dry-run includes the handler smoke).
# --apply performs, in order:
# 1. idle gate: `a2a-termux-native-worker.sh restart-worker --if-idle --dry-run`
# (busy → exit 10, nothing changed)
Expand All @@ -32,11 +40,12 @@
# the old files, exit 30
# 6. write <worker_root>/.deploy-source-sha
#
# Exit codes: 0 ok (dry-run: plan ok), 2 usage / invalid artifact, 3 no
# supervisor or worker (nothing changed), 4 another deploy holds the lock,
# 10 busy (nothing changed — or files rolled back before any signal),
# 11 idle state unknown (nothing changed), 20 pre-restart check failed (rolled
# back, no restart), 30 restart not verified (rolled back + restarted),
# Exit codes: 0 ok (dry-run: plan + preview checks + gate ok), 2 usage /
# invalid artifact, 3 no supervisor or worker (nothing changed), 4 another
# deploy holds the lock, 10 busy (nothing changed — or files rolled back before
# any signal), 11 idle state unknown (nothing changed), 20 pre-restart check
# failed (rolled back, no restart; dry-run: failed on the preview, nothing
# changed), 30 restart not verified (rolled back + restarted),
# 31 rollback failed — MANUAL RESTORE from the printed backups.
#
# Env: A2A_DEPLOY_BACKUP_DIR (default $HOME/a2a-nexus-local-deploy-backups),
Expand All @@ -50,7 +59,7 @@ DEPLOY_DIRS=(dist scripts handlers)

ENV_FILE=""; ARTIFACT=""; TARGET_SHA=""; SMOKE_CMD=""; APPLY=0; GUARD=1
BACKUP_DIR="${A2A_DEPLOY_BACKUP_DIR:-$HOME/a2a-nexus-local-deploy-backups}"
STAGE_TMP=""; STAGE=""; ROOT=""
STAGE_TMP=""; STAGE=""; ROOT=""; PREVIEW_TMP=""; PREVIEW_ROOT=""; STAGED_REV=""; CHECK_FAILED=""

say() { printf 'deploy: %s\n' "$*"; }
die() { printf 'deploy: %s\n' "$2" >&2; exit "$1"; }
Expand All @@ -60,7 +69,10 @@ usage() {
exit 2
}

cleanup() { if [[ -n "$STAGE_TMP" ]]; then rm -rf "$STAGE_TMP"; fi; }
cleanup() {
if [[ -n "$STAGE_TMP" ]]; then rm -rf "$STAGE_TMP"; fi
if [[ -n "$PREVIEW_TMP" ]]; then rm -rf "$PREVIEW_TMP"; fi
}
trap cleanup EXIT

# Same quoting rules as the Python validator.
Expand Down Expand Up @@ -226,24 +238,76 @@ rollback_or_die() { # <exit_rc> <message>
die "$1" "$2; rolled back"
}

copy_artifact() {
local d p
copy_artifact() { # [dest root, default $ROOT]
local dest="${1:-$ROOT}" d p
for d in "${DEPLOY_DIRS[@]}"; do
[[ -d "$STAGE/$d" ]] || continue
mkdir -p "$ROOT/$d" && cp -a "$STAGE/$d/." "$ROOT/$d/" || return 1
mkdir -p "$dest/$d" && cp -a "$STAGE/$d/." "$dest/$d/" || return 1
done
if (( ${#NM_REPLACED[@]} )); then
mkdir -p "$ROOT/node_modules" || return 1
mkdir -p "$dest/node_modules" || return 1
for p in "${NM_REPLACED[@]}"; do
rm -rf "${ROOT:?}/node_modules/$p" && cp -a "$STAGE/node_modules/$p" "$ROOT/node_modules/$p" || return 1
rm -rf "${dest:?}/node_modules/$p" && cp -a "$STAGE/node_modules/$p" "$dest/node_modules/$p" || return 1
done
fi
}

run_guard() { # the artifact's own rollout guard, when it ships one
local guard="$ROOT/scripts/worker-artifact-rollout-guard.mjs" out
# Dry-run preview of the post-deploy root in a private temp dir. Nothing
# under $ROOT is written: the deploy dirs are copied dereferenced (a symlinked
# dist/ must not let the overlay write through to the live target), unchanged
# node_modules packages are symlinks, and any symlink left in a deploy dir
# refuses the preview. Sets PREVIEW_ROOT (runs in the main shell so the EXIT
# trap removes PREVIEW_TMP).
build_preview() {
local base parent preview d e name
base="$(basename "$ROOT")"; parent="$(dirname "$ROOT")"
PREVIEW_TMP=$(mktemp -d "${TMPDIR:-/tmp}/a2a-worker-preview.XXXXXX") || return 1
preview="$PREVIEW_TMP/$base"
tar cf - -C "$parent" --exclude="$base/node_modules" --exclude="$base/dist.bak*" \
--exclude="$base/dist" --exclude="$base/scripts" --exclude="$base/handlers" "$base" |
tar xf - -C "$PREVIEW_TMP" || return 1
for d in "${DEPLOY_DIRS[@]}"; do
[[ -e "$ROOT/$d" ]] || continue
tar chf - -C "$ROOT" "$d" | tar xf - -C "$preview" || return 1
done
for d in "${DEPLOY_DIRS[@]}"; do
if [[ -e "$preview/$d" && -n "$(find "$preview/$d" -type l -print -quit)" ]]; then
say "preview refused: $d/ still holds a symlink after dereferencing"; return 1
fi
done
mkdir "$preview/node_modules" || return 1
if [[ -d "$ROOT/node_modules" ]]; then
for e in "$ROOT/node_modules"/* "$ROOT/node_modules"/.[!.]*; do
[[ -e "$e" || -L "$e" ]] || continue
name="${e##*/}"
printf '%s\n' "${NM_REPLACED[@]}" | grep -qxF -- "$name" && continue
ln -s "$e" "$preview/node_modules/$name" || return 1
done
fi
copy_artifact "$preview" || return 1
PREVIEW_ROOT="$preview"
}

# Step-4 checks on <root> (the live root after the copy, or the dry-run
# preview). On failure sets CHECK_FAILED and returns 1.
pre_restart_checks() { # <root>
local root="$1" new_rev
new_rev=$(build_rev "$root")
if [[ -n "$STAGED_REV" && "$new_rev" != "$STAGED_REV" ]]; then
CHECK_FAILED="deployed revision ${new_rev:-none} != artifact $STAGED_REV"; return 1
fi
run_guard "$root" || { CHECK_FAILED="rollout guard failed"; return 1; }
if [[ -n "$SMOKE_CMD" ]]; then
A2A_WORKER_ROOT="$root" bash -c "$SMOKE_CMD" || { CHECK_FAILED="smoke command failed"; return 1; }
fi
return 0
}

run_guard() { # <root>: the artifact's own rollout guard, when it ships one
local root="${1:-$ROOT}" guard out
guard="$root/scripts/worker-artifact-rollout-guard.mjs"
(( GUARD )) && [[ -f "$guard" ]] || return 0
out=$(A2A_WORKER_ROOT="$ROOT" ${TARGET_SHA:+A2A_WORKER_TARGET_REVISION="$TARGET_SHA"} \
out=$(A2A_WORKER_ROOT="$root" ${TARGET_SHA:+A2A_WORKER_TARGET_REVISION="$TARGET_SHA"} \
node "$guard" --deployed 2>&1) || { say "guard rc!=0: ${out##*$'\n'}"; return 1; }
python3 - "$out" <<'PY'
import json, sys
Expand Down Expand Up @@ -294,6 +358,7 @@ main() {
stage_artifact
local staged_rev current_rev source_sha
staged_rev=$(build_rev "$STAGE"); current_rev=$(build_rev "$ROOT")
STAGED_REV="$staged_rev"
if [[ -n "$TARGET_SHA" && -n "$staged_rev" && "$staged_rev" != "$TARGET_SHA" ]]; then
die 2 "artifact revision $staged_rev != --target-sha $TARGET_SHA"
fi
Expand All @@ -309,6 +374,14 @@ main() {
say "plan copy ${present[*]} node_modules=[${NM_REPLACED[*]}] guard=$([[ $GUARD = 1 ]] && echo auto || echo off) smoke=$([[ -n $SMOKE_CMD ]] && echo yes || echo no)"
say "plan backups -> $BACKUP_DIR; restart via $(basename "$HARNESS") restart-worker --if-idle"

# Dry-run: step-4 checks on a temp preview of the post-deploy root.
if (( ! APPLY )); then
build_preview || die 2 "could not build the dry-run preview — nothing changed"
pre_restart_checks "$PREVIEW_ROOT" ||
die 20 "dry-run pre-restart check failed on the staged preview: $CHECK_FAILED — nothing changed"
say "dry-run checks ok on staged preview (revision, guard=$([[ $GUARD = 1 ]] && echo auto || echo off), smoke=$([[ -n $SMOKE_CMD ]] && echo yes || echo no))"
fi

# 1. idle gate (nothing changes on refusal).
local out rc
out=$(harness restart-worker --if-idle --dry-run 2>&1); rc=$?
Expand Down Expand Up @@ -337,15 +410,7 @@ main() {
copy_artifact || rollback_or_die 20 "artifact copy failed (no restart)"

# 4. pre-restart checks.
local new_rev
new_rev=$(build_rev "$ROOT")
if [[ -n "$staged_rev" && "$new_rev" != "$staged_rev" ]]; then
rollback_or_die 20 "deployed revision ${new_rev:-none} != artifact $staged_rev (no restart)"
fi
run_guard || rollback_or_die 20 "rollout guard failed (no restart)"
if [[ -n "$SMOKE_CMD" ]]; then
A2A_WORKER_ROOT="$ROOT" bash -c "$SMOKE_CMD" || rollback_or_die 20 "smoke command failed (no restart)"
fi
pre_restart_checks "$ROOT" || rollback_or_die 20 "$CHECK_FAILED (no restart)"

# 5. restart worker.js only; the harness verifies registration + health.
out=$(harness restart-worker --if-idle 2>&1); rc=$?
Expand Down
Loading
Loading