oficina / equipo Public

Roll back only when no open task holds unfinished work

Astra round 5 on the branch (one P1): an instance at protocol 31 that
accepted a timed-out step's work and was then rolled back to an older
release would take that work, and anything built on it, for finished.
Stored work cannot be made safe for older code through every later
step, so the production rollback (scripts/upgrade-thin.sh rollback)
now refuses, before it changes anything, while an open task holds an
Engineering step done with step_timeout. Finish or cancel those tasks
first, or set UPGRADE_ALLOW_UNFINISHED=1 to roll back anyway.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a5b04b94ab086d71232ca74e8dd1432b440b34ef parent 216458b
2 files changed, +47 −1
  • internal/release/upgrade_test.go +34 −1
  • scripts/upgrade-thin.sh +13 −0
diff --git a/internal/release/upgrade_test.go b/internal/release/upgrade_test.go
index 5c25ee7f..05db4bf9 100644
--- a/internal/release/upgrade_test.go
+++ b/internal/release/upgrade_test.go
@@ -24,6 +24,8 @@ type upgradeRig struct {
root, home, runner string
script, log string
serviceEnv, unit string
+ // env is added to every run's environment.
+ env []string
}
func newUpgradeRig(t *testing.T) *upgradeRig {
@@ -70,7 +72,7 @@ func newUpgradeRig(t *testing.T) *upgradeRig {
r.write(filepath.Join(bin, "carlos"), "#!/bin/sh\n"+log+`case "$1 $2" in
"db "*) [ -e `+filepath.Join(root, "replicabroken")+` ] && { echo "the copy failed its integrity check" >&2; exit 1; }
case "$2 $*" in pull*) for a; do case $prev in --out) python3 -c "import sqlite3,sys; c=sqlite3.connect(sys.argv[1]); [c.execute('create table '+t+'(x)') for t in ('teams','chat_tasks','chat_steps','chat_turns','runners')]; c.commit()" "$a" ;; esac; prev=$a; done ;;
- *integrity_check*) printf 'integrity_check\nok\n' ;; *) printf 'x\n%s\n' "$(cat `+filepath.Join(root, "inflight")+`)" ;; esac ;;
+ *integrity_check*) printf 'integrity_check\nok\n' ;; *step_timeout*) printf 'x\n%s\n' "$(cat `+filepath.Join(root, "unfinished")+` 2>/dev/null || echo 0)" ;; *) printf 'x\n%s\n' "$(cat `+filepath.Join(root, "inflight")+`)" ;; esac ;;
deploy*) for a; do case $prev in --version) echo "$a" > `+filepath.Join(root, "served")+` ;; esac; prev=$a; done ;;
promote*) shift; for a; do last2=$last; last=$a; done; echo "$last2" > `+filepath.Join(root, "served")+` ;;
esac
@@ -99,6 +101,7 @@ func (r *upgradeRig) read(path string) string {
func (r *upgradeRig) run(args ...string) (string, error) {
cmd := exec.Command("bash", append([]string{r.script}, args...)...)
cmd.Env = []string{"HOME=" + r.home, "PATH=" + filepath.Join(r.root, "bin") + ":/usr/bin:/bin", "UPGRADE_YES=1", "UPGRADE_TRIES=3", "UPGRADE_WAIT=0", "UPGRADE_PROC=" + filepath.Join(r.root, "proc"), "LANG=C.UTF-8"}
+ cmd.Env = append(cmd.Env, r.env...)
out, err := cmd.CombinedOutput()
return string(out), err
}
@@ -418,3 +421,33 @@ func TestUpgradeResumesAfterTheRestart(t *testing.T) {
t.Fatalf("rollback retry stopped the previous runner again: %v\n%s\n%s", err, out, r.read(r.log))
}
}
+
+// An older release takes the work of an Engineering step that ran out of
+// time (done, step_timeout) for finished work: a rollback refuses while
+// an open task holds such work, before it changes anything, unless the
+// operator says to go on (UPGRADE_ALLOW_UNFINISHED=1).
+func TestUpgradeRollbackRefusesWithUnfinishedWork(t *testing.T) {
+ r := newUpgradeRig(t)
+ if out, err := r.run(upgradeTo, "--execute"); err != nil {
+ t.Fatalf("upgrade: %v\n%s", err, out)
+ }
+ exe := filepath.Join(r.root, "proc", "4242", "exe")
+ r.must(os.Remove(exe))
+ r.must(os.Symlink(filepath.Join(r.runner, "releases", upgradeFrom, "equipo-runner"), exe))
+ r.write(filepath.Join(r.root, "unfinished"), "2", 0600)
+ before := r.read(r.log)
+ out, err := r.run("rollback", "--execute")
+ if err == nil || !strings.Contains(out, "refused: 2 open task(s) hold unfinished work") || strings.Contains(r.state(), "rolled_back=1") {
+ t.Fatalf("rollback with unfinished work: %v\n%s", err, out)
+ }
+ if changed := strings.TrimPrefix(r.read(r.log), before); strings.Contains(changed, "systemctl") || strings.Contains(changed, "promote") {
+ t.Fatalf("changed something before refusing:\n%s", changed)
+ }
+ if plan, err := r.run("rollback"); err != nil || !strings.Contains(plan, "unfinished work") {
+ t.Fatalf("the plan does not name the check: %v\n%s", err, plan)
+ }
+ r.env = []string{"UPGRADE_ALLOW_UNFINISHED=1"}
+ if out, err := r.run("rollback", "--execute"); err != nil || !strings.Contains(r.state(), "rolled_back=1") {
+ t.Fatalf("rollback told to go on: %v\n%s", err, out)
+ }
+}
diff --git a/scripts/upgrade-thin.sh b/scripts/upgrade-thin.sh
index 0d984158..aad56fa9 100755
--- a/scripts/upgrade-thin.sh
+++ b/scripts/upgrade-thin.sh
@@ -62,6 +62,10 @@ RELEASE_REPO=${EQUIPO_RELEASE_REPO:-$REPO}
RELEASES=$RELEASE_REPO/releases
PROC=${UPGRADE_PROC:-/proc}
carlos_app=(--app "$APP" --account "$ACCOUNT" --console "$CONSOLE")
+# Open tasks holding the work of an Engineering step that ran out of time
+# (done, step_timeout; protocol 31): a release before it takes that work
+# for finished, so a rollback refuses while any is open.
+UNFINISHED="SELECT count(DISTINCT k.id) FROM chat_tasks k JOIN chat_steps s ON s.task_id=k.id WHERE s.specialist='engineering' AND s.state='done' AND s.reason='step_timeout' AND k.stage NOT IN ('done','cancelled')"
IN_FLIGHT="SELECT (SELECT count(*) FROM chat_steps WHERE state IN ('queued','running'))||'/'||(SELECT count(*) FROM chat_steps WHERE specialist='merge' AND state IN ('queued','running'))||'/'||(SELECT count(*) FROM chat_turns WHERE dispatched=1 AND state NOT IN ('replied','failed','cancelled'))"
mode=upgrade
@@ -187,6 +191,15 @@ if [[ $mode == rollback ]]; then
prev_exec=$(sed -n 's/^ExecStart=\(.*\) run$/\1/p' "$snap/equipo-runner.service")
[[ -f $prev_exec ]] || die "the previous runner $prev_exec is missing"
prev_sha=$(sha256sum "$prev_exec" | cut -d ' ' -f 1)
+ step "rollback 0 no open task holds unfinished work"
+ echo " open tasks whose Engineering ran out of time and kept its work: 0 (a release before"
+ echo " protocol 31 takes that work for finished); finish or cancel them first, or set"
+ echo " UPGRADE_ALLOW_UNFINISHED=1 to roll back anyway"
+ if ((execute)) && [[ ${UPGRADE_ALLOW_UNFINISHED:-} != 1 ]]; then
+ n=$(carlos db query "${carlos_app[@]}" --sql "$UNFINISHED" | tail -1) || die "could not read the open tasks. $REPLICA"
+ [[ $n =~ ^[0-9]+$ ]] || die "could not read the open tasks. $REPLICA"
+ [[ $n == 0 ]] || die "$n open task(s) hold unfinished work a release before protocol 31 takes for finished: finish or cancel them, or set UPGRADE_ALLOW_UNFINISHED=1 to roll back anyway"
+ fi
step "rollback 1 stop the runner"
# A retry after the previous runner started again does not stop it a second time.
if ((execute)) && runs "$prev_sha"; then echo " $UNIT already runs $prev_exec"; else run systemctl --user stop "$UNIT"; fi