autonomous-sdlc-harness 0.6.1 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,7 +33,8 @@
33
33
  # the authority.
34
34
  #
35
35
  # WHO CAN START A RUN. `remote-run.sh`'s `trigger` paragraph owns the check —
36
- # the labeller, bots and HARNESS_TRIGGER_ALLOWED_BOTS, the permission call —
36
+ # the labeller, bots and HARNESS_TRIGGER_ALLOWED_BOTS, the permission call, the
37
+ # allow-list HARNESS_RUN_ACTORS, and the `repository_dispatch` sender check —
37
38
  # and the order its refusals are made in. Nothing here pre-filters on it.
38
39
  #
39
40
  # THE PERMISSIONS, declared because the default token may be read-only
@@ -69,6 +70,7 @@
69
70
  # HARNESS_REMOTE_STOP, HARNESS_TRIGGER_LABEL,
70
71
  # DEFAULT_TRIGGER_LABEL (`sdlc-harness`),
71
72
  # HARNESS_TRIGGER_ALLOWED_BOTS,
73
+ # HARNESS_RUN_ACTORS,
72
74
  # TRIGGER_DISPATCH_EVENT_TYPE (`harness-task`)
73
75
  # cli/src/config/model.ts DEFAULTS.scriptsDir
74
76
  #
@@ -112,6 +114,7 @@ jobs:
112
114
  HARNESS_REMOTE_SLUG: ${{ github.repository }}
113
115
  HARNESS_TRIGGER_LABEL: ${{ vars.HARNESS_TRIGGER_LABEL || 'sdlc-harness' }}
114
116
  HARNESS_TRIGGER_ALLOWED_BOTS: ${{ vars.HARNESS_TRIGGER_ALLOWED_BOTS }}
117
+ HARNESS_RUN_ACTORS: ${{ vars.HARNESS_RUN_ACTORS }}
115
118
  steps:
116
119
  - name: Check out the default branch
117
120
  uses: actions/checkout@v5
@@ -357,9 +357,21 @@
357
357
  # restored bundle's under HARNESS_INPUT_CHAIN above 0, else at this run's own
358
358
  # `createdAt` (`remote-run.sh run-created-at "$GITHUB_RUN_ID"`), else at
359
359
  # HARNESS_JOB_STARTED_EPOCH — NEVER at the job's own start alone, which
360
- # would lose a pause sent while the job was queued. Each successful poll
361
- # advances it to the epoch taken just before its query; a failed poll
362
- # advances nothing and pauses nothing.
360
+ # would lose a pause sent while the job was queued. That starting bound is
361
+ # the job's FLOOR. The verb's exit decides: `0` a pause, `5` none — each
362
+ # advances the bound to the larger of the floor and the epoch taken just
363
+ # before the query less CONTROL_POLL_OVERLAP_SECS, so a run the listing
364
+ # shows late is still read; anything else, `1` included, is a failed poll,
365
+ # which advances nothing and pauses nothing. Every poll logs one line,
366
+ # `job: control poll of '<branch>' since <since> (exit <rc>): <last line>`.
367
+ # * THE RUNNER WAIT: the job logs HARNESS_JOB_STARTED_EPOCH less this run's
368
+ # `createdAt` — read once per job, the same read the control poll's
369
+ # starting bound takes. On a re-run attempt (`GITHUB_RUN_ATTEMPT` above 1)
370
+ # that `createdAt` is the first attempt's, so the wait is not measured and
371
+ # no note is added. A wait of at least RUNNER_WAIT_NOTE_SECS is noted on
372
+ # the `resumed` comment the job posts as it starts, never on a later
373
+ # automatic resume's. The budget already starts at
374
+ # HARNESS_JOB_STARTED_EPOCH, so a wait costs no budget.
363
375
  # * THE DECISION, when the run leaves `running`: `paused` for `budget` ->
364
376
  # `continue`; for `user` -> `stop`; by the usage gate (`usage`) -> a lost
365
377
  # `usage_resume_at` is first given the gate's fallback and reported as
@@ -396,6 +408,19 @@
396
408
  # issue and moves its state label when `forge` is `github`; `autonomous-notify.sh`
397
409
  # is unchanged, and `completed` is reported by the workflow's `deliver` step
398
410
  # after the push.
411
+ # * THE PROGRESS PASS (job_progress_pass): every `running` pass reads the
412
+ # flow-progress ledger the flow pushes in this checkout through
413
+ # lib/harness-run-lib.sh's `hr_ledger_phases`, and calls `remote-run.sh
414
+ # report progress <branch>` when that reading differs from the last one
415
+ # this job reported, and once more when the job ends, so a tick landing
416
+ # after the last poll is still reported. It reads nothing from GitHub to
417
+ # decide and keeps no state across jobs: the render is deterministic and
418
+ # `report progress` edits the comment only when its body differs, so a
419
+ # job's first pass syncs once and a budget continuation posts nothing new
420
+ # unless a phase moved — consistent with `docs/github-run-control.md` ->
421
+ # §5's *The budget is silent*, because what is posted is a phase, not the
422
+ # continuation. What is posted, where, and whether at all is `report
423
+ # progress`'s to decide; the watcher decides only when to call it.
399
424
  # * THE INTERACTIVE-TEST PHASE IS SKIPPED, not run: a runner has no browser
400
425
  # wiring, application dependencies or QA credentials for it. With
401
426
  # `phases.qa` true, the task and user_review launch prompts gain one clause
@@ -686,8 +711,14 @@
686
711
  # A stub writing PAUSE_ACK unrequested, then exiting 0 ->
687
712
  # one auto-resume, `job: completed stop`; a stub ending
688
713
  # `exit 2` with REMOTE_AUTO_RESUME_DELAY_SECS=0 -> launched
689
- # 1 + REMOTE_AUTO_RESUME_MAX times, `job: failed stop`
690
- # remote drop `a drop`'s fixture, with "$d"'s own files committed and pushed
714
+ # 1 + REMOTE_AUTO_RESUME_MAX times, `job: failed stop`. A
715
+ # stub writing a task ledger at
716
+ # "$d/sdlc-harness/flow_progress/feat_x_progress.md" and
717
+ # sleeping past a few polls, the ledger never ticked ->
718
+ # the watcher log carries one `remote-run.sh: report:` line
719
+ # from the first pass and one from the job's end, none
720
+ # between
721
+ # remote drop `a drop`'s fixture, with "$d"'s own files committed and pushed
691
722
  # to origin's default branch, `"execution":{"target":
692
723
  # "github-actions"}` in its harness.config.json, and
693
724
  # HARNESS_GH_CLI pointed at a recorder (see remote-run.sh's
@@ -1111,6 +1142,11 @@ USAGE_WARNING_STREAK=0
1111
1142
  # none of them. How often the control poll asks GitHub for a `harness pause
1112
1143
  # <branch>` run — each poll is one `gh run list`.
1113
1144
  REMOTE_CONTROL_POLL_SECS="${REMOTE_CONTROL_POLL_SECS:-60}"
1145
+ # Not a tunable. How far each control poll's next lower bound reaches back:
1146
+ # GitHub's run listing is eventually consistent, so the window re-reads the last
1147
+ # five minutes. A marker seen twice is harmless — JOB_USER_PAUSE_DROPPED drops
1148
+ # PAUSE once per job.
1149
+ CONTROL_POLL_OVERLAP_SECS=300
1114
1150
  # The longest usage-reset wait a HOSTED job sits through rather than handing
1115
1151
  # the run to the resume poller, since a hosted job bills for the minutes it
1116
1152
  # waits. A self-hosted job waits for any reset before its deadline.
@@ -1121,6 +1157,9 @@ REMOTE_AUTO_RESUME_MAX="${REMOTE_AUTO_RESUME_MAX:-2}"
1121
1157
  # How long job mode waits before each of those resumes, so a transient outage
1122
1158
  # has time to clear.
1123
1159
  REMOTE_AUTO_RESUME_DELAY_SECS="${REMOTE_AUTO_RESUME_DELAY_SECS:-300}"
1160
+ # The runner wait at or above which the job's `resumed` comment says so (see
1161
+ # THE RUNNER WAIT in the header).
1162
+ RUNNER_WAIT_NOTE_SECS=300
1124
1163
 
1125
1164
  # JOB MODE (see the header). Assigned HERE, after the override channel and every
1126
1165
  # tunable default, so neither the file nor an inherited value can turn a
@@ -1129,9 +1168,18 @@ REMOTE_AUTO_RESUME_DELAY_SECS="${REMOTE_AUTO_RESUME_DELAY_SECS:-300}"
1129
1168
  JOB_MODE=0
1130
1169
  # Job mode's pass state, for the same reason: when the control poll last ran,
1131
1170
  # whether each pass has already dropped its one PAUSE, and the job's start
1132
- # (HARNESS_JOB_STARTED_EPOCH, else when run_job began).
1171
+ # (HARNESS_JOB_STARTED_EPOCH, else when run_job began), and the control poll's
1172
+ # starting bound, below which no later bound falls.
1133
1173
  JOB_START_EPOCH=0
1174
+ JOB_CONTROL_FLOOR=0
1175
+ # This run's `createdAt`, read once per job (empty when unread), and the note
1176
+ # job_report adds to a `resumed` report when the runner wait was long.
1177
+ JOB_RUN_CREATED_AT=""
1178
+ JOB_RUNNER_WAIT_NOTE=""
1134
1179
  LAST_CONTROL_POLL=0
1180
+ # The ledger reading the progress pass last reported; empty, so a job's first
1181
+ # pass always reports once.
1182
+ JOB_PROGRESS_LAST=""
1135
1183
  JOB_USER_PAUSE_DROPPED=0
1136
1184
  JOB_BUDGET_PAUSE_DROPPED=0
1137
1185
  if [ "${1:-}" = "job" ]; then
@@ -1185,16 +1233,21 @@ notify() {
1185
1233
 
1186
1234
  # job_report <event> <branch> [<log>] — the only route to GitHub. The detail is
1187
1235
  # withheld: it names local slash commands, and `report` words its comment from
1188
- # the registry record itself.
1236
+ # the registry record itself. A `resumed` report carries JOB_RUNNER_WAIT_NOTE
1237
+ # when it is set.
1189
1238
  job_report() {
1239
+ local note=()
1190
1240
  if [ ! -r "$REMOTE_RUN" ]; then
1191
1241
  log "notify: '$REMOTE_RUN' is not readable — '${1:-?}' event for '${2:-?}' not reported on GitHub"
1192
1242
  return 0
1193
1243
  fi
1244
+ if [ "$1" = "resumed" ] && [ -n "$JOB_RUNNER_WAIT_NOTE" ]; then
1245
+ note=(--note "$JOB_RUNNER_WAIT_NOTE")
1246
+ fi
1194
1247
  if [ -n "${3:-}" ]; then
1195
- bash "$REMOTE_RUN" report "$1" "$2" --repo "$MAIN_REPO" >>"$3" 2>&1 || true
1248
+ bash "$REMOTE_RUN" report "$1" "$2" ${note[@]+"${note[@]}"} --repo "$MAIN_REPO" >>"$3" 2>&1 || true
1196
1249
  else
1197
- bash "$REMOTE_RUN" report "$1" "$2" --repo "$MAIN_REPO" >/dev/null 2>&1 || true
1250
+ bash "$REMOTE_RUN" report "$1" "$2" ${note[@]+"${note[@]}"} --repo "$MAIN_REPO" >/dev/null 2>&1 || true
1198
1251
  fi
1199
1252
  }
1200
1253
 
@@ -1330,11 +1383,11 @@ job_report() {
1330
1383
  # 0, else `0` — so ANY USER ACTION (a drop, an answer, a
1331
1384
  # resume: each a chain-0 dispatch) resets it and restores
1332
1385
  # the full allowance
1333
- # control_polled_at job mode only: the epoch second up to which the job has
1334
- # checked for a `harness pause <branch>` run — the lower
1335
- # bound of the next control poll, this job's or the next
1336
- # chained one's. Set at start (see JOB MODE) and advanced
1337
- # by every successful poll
1386
+ # control_polled_at job mode only: the lower bound of the next control poll,
1387
+ # this job's or the next chained one's — set at start (see
1388
+ # JOB MODE), and after each successful poll the larger of
1389
+ # that starting bound and the epoch before the query less
1390
+ # CONTROL_POLL_OVERLAP_SECS
1338
1391
  # pause_note_stale job mode only: `1` when a `pause` job's restored
1339
1392
  # `status.json` was not `paused` or named another engine,
1340
1393
  # so spawn_engine's pause-resume prompt says there is no
@@ -4022,6 +4075,30 @@ job_int() {
4022
4075
  printf '%s\n' "$((10#$1))"
4023
4076
  }
4024
4077
 
4078
+ # job_runner_wait — the job's one `run-created-at` read, into JOB_RUN_CREATED_AT,
4079
+ # then THE RUNNER WAIT's log line and JOB_RUNNER_WAIT_NOTE (the header).
4080
+ job_runner_wait() {
4081
+ local wait mins
4082
+ if [ -n "${GITHUB_RUN_ID:-}" ]; then
4083
+ JOB_RUN_CREATED_AT="$(job_int "$(bash "$REMOTE_RUN" run-created-at "$GITHUB_RUN_ID" --repo "$MAIN_REPO" 2>>"$WATCHER_LOG")")" || JOB_RUN_CREATED_AT=""
4084
+ fi
4085
+ if [ -z "$JOB_RUN_CREATED_AT" ]; then
4086
+ log "job: this run's createdAt could not be read — the runner wait is unknown"
4087
+ return 0
4088
+ fi
4089
+ if [ "${GITHUB_RUN_ATTEMPT:-1}" != 1 ]; then
4090
+ log "job: run attempt ${GITHUB_RUN_ATTEMPT} — this run's createdAt is its first attempt's, so the runner wait is not measured"
4091
+ return 0
4092
+ fi
4093
+ wait=$((JOB_START_EPOCH - JOB_RUN_CREATED_AT))
4094
+ [ "$wait" -ge 0 ] || wait=0
4095
+ log "job: waited ${wait}s for a runner (run created $JOB_RUN_CREATED_AT, job started $JOB_START_EPOCH)"
4096
+ if [ "$wait" -ge "$RUNNER_WAIT_NOTE_SECS" ]; then
4097
+ mins=$(((wait + 59) / 60))
4098
+ JOB_RUNNER_WAIT_NOTE="GitHub took $mins minutes to start this job, so nothing moved until then."
4099
+ fi
4100
+ }
4101
+
4025
4102
  # job_start_control_bound <branch> <remote_status> — the control poll's first
4026
4103
  # lower bound, in the order the header's JOB MODE block states.
4027
4104
  job_start_control_bound() {
@@ -4029,35 +4106,40 @@ job_start_control_bound() {
4029
4106
  if [ "$HARNESS_INPUT_CHAIN" -gt 0 ] && [ -f "$remote_status" ]; then
4030
4107
  bound="$(job_int "$(hr_remote_status_get "$remote_status" control_polled_at)")" || bound=""
4031
4108
  fi
4032
- if [ -z "$bound" ] && [ -n "${GITHUB_RUN_ID:-}" ]; then
4033
- bound="$(job_int "$(bash "$REMOTE_RUN" run-created-at "$GITHUB_RUN_ID" --repo "$MAIN_REPO" 2>>"$WATCHER_LOG")")" || bound=""
4034
- fi
4109
+ [ -n "$bound" ] || bound="$JOB_RUN_CREATED_AT"
4035
4110
  if [ -z "$bound" ]; then
4036
4111
  bound="$JOB_START_EPOCH"
4037
4112
  log "job: could not read this run's createdAt — the control poll starts from the job's start ($bound)"
4038
4113
  fi
4114
+ JOB_CONTROL_FLOOR="$bound"
4039
4115
  registry_set "$branch" control_polled_at "$bound"
4040
4116
  }
4041
4117
 
4042
4118
  # job_control_poll <branch> <state_abs> <remote_status> — the `user` pass.
4119
+ # Exit map of `pause-requested`: 0 a pause, 5 none, anything else a failed poll
4120
+ # that neither moves the bound nor pauses.
4043
4121
  job_control_poll() {
4044
- local branch="$1" state_abs="$2" remote_status="$3" now since before rc
4122
+ local branch="$1" state_abs="$2" remote_status="$3" now since before rc out next
4045
4123
  [ "$JOB_USER_PAUSE_DROPPED" = "0" ] || return 0
4046
4124
  now="$(date +%s)"
4047
4125
  [ $((now - LAST_CONTROL_POLL)) -ge "$REMOTE_CONTROL_POLL_SECS" ] || return 0
4048
4126
  LAST_CONTROL_POLL="$now"
4049
4127
  since="$(job_int "$(registry_get "$branch" control_polled_at)")" || since="$JOB_START_EPOCH"
4050
4128
  before="$(date +%s)"
4051
- bash "$REMOTE_RUN" pause-requested "$branch" "$since" --repo "$MAIN_REPO" >>"$WATCHER_LOG" 2>&1
4129
+ out="$(bash "$REMOTE_RUN" pause-requested "$branch" "$since" --repo "$MAIN_REPO" 2>&1)"
4052
4130
  rc=$?
4131
+ [ -z "$out" ] || printf '%s\n' "$out" >>"$WATCHER_LOG"
4132
+ log "job: control poll of '$branch' since $since (exit $rc): $(printf '%s\n' "$out" | awk 'NF { l = $0 } END { print l }')"
4053
4133
  case "$rc" in
4054
- 0 | 1) ;;
4134
+ 0 | 5) ;;
4055
4135
  *)
4056
4136
  log "job: the control poll for '$branch' failed (exit $rc) — not pausing; control_polled_at stays $since"
4057
4137
  return 0
4058
4138
  ;;
4059
4139
  esac
4060
- registry_set "$branch" control_polled_at "$before"
4140
+ next=$((before - CONTROL_POLL_OVERLAP_SECS))
4141
+ [ "$next" -ge "$JOB_CONTROL_FLOOR" ] || next="$JOB_CONTROL_FLOOR"
4142
+ registry_set "$branch" control_polled_at "$next"
4061
4143
  if [ "$rc" = "0" ]; then
4062
4144
  JOB_USER_PAUSE_DROPPED=1
4063
4145
  registry_set "$branch" pause_reason user
@@ -4080,6 +4162,17 @@ job_budget_pass() {
4080
4162
  log "job: ${after}s of the hosted time budget have passed — dropped PAUSE (reason budget)"
4081
4163
  }
4082
4164
 
4165
+ # job_progress_pass <branch> <state_abs> [final] — THE PROGRESS PASS (the
4166
+ # header's JOB MODE block). `final` reports even an unchanged reading.
4167
+ job_progress_pass() {
4168
+ local branch="$1" state_abs="$2" line
4169
+ [ "$JOB_MODE" = "1" ] || return 0
4170
+ line="$(hr_ledger_phases "$state_abs/flow_progress/${branch}_progress.md")" || return 0
4171
+ [ "$line" != "$JOB_PROGRESS_LAST" ] || [ "${3:-}" = "final" ] || return 0
4172
+ JOB_PROGRESS_LAST="$line"
4173
+ bash "$REMOTE_RUN" report progress "$branch" --repo "$MAIN_REPO" >>"$WATCHER_LOG" 2>&1 || true
4174
+ }
4175
+
4083
4176
  # job_usage_wait_ok <branch> — 0 when a usage pause is waited out in the job;
4084
4177
  # leaves usage_resume_at_var's USAGE_RESUME_AT and USAGE_RESUME_REPAIRED set.
4085
4178
  # With no usable usage_resume_at: `paused_by=usage` still set means the value was
@@ -4145,6 +4238,7 @@ run_job() {
4145
4238
  local prev_status="" prev_engine="" aside_rc
4146
4239
 
4147
4240
  JOB_START_EPOCH="$(job_int "${HARNESS_JOB_STARTED_EPOCH:-}")" || JOB_START_EPOCH="$(date +%s)"
4241
+ job_runner_wait
4148
4242
  state_rel="$(run_state_dir "$worktree")" || fatal "job: the state directory in '$worktree' is unresolvable"
4149
4243
  state_abs="$worktree/$state_rel"
4150
4244
  clar_dir="$state_abs/clarifications/$branch"
@@ -4248,6 +4342,10 @@ run_job() {
4248
4342
  ;;
4249
4343
  esac || registry_set "$branch" status failed
4250
4344
 
4345
+ # The runner-wait note belongs to the job's own start: a later automatic
4346
+ # resume's `resumed` comment never carries it (THE RUNNER WAIT in the header).
4347
+ JOB_RUNNER_WAIT_NOTE=""
4348
+
4251
4349
  # The supervision loop: the header's JOB MODE block states each decision.
4252
4350
  local status reason ra when decision="stop" detail="" usage_waiting=0 restarts
4253
4351
  local wait_ok=1 usage_wait_start=0 usage_wait_ra=0
@@ -4263,6 +4361,7 @@ run_job() {
4263
4361
  [ "$(registry_get "$branch" status)" = "running" ] || continue
4264
4362
  job_control_poll "$branch" "$state_abs" "$remote_status"
4265
4363
  job_budget_pass "$branch" "$state_abs"
4364
+ job_progress_pass "$branch" "$state_abs"
4266
4365
  ;;
4267
4366
  paused)
4268
4367
  reason="$(registry_get "$branch" pause_reason)"
@@ -4354,6 +4453,7 @@ run_job() {
4354
4453
  [ "$final" = "paused" ] || registry_set "$branch" pause_reason ""
4355
4454
  [ -n "$detail" ] || detail="the run ended $final in this job"
4356
4455
  [ "$final" != "failed" ] || job_report failed "$branch" "$log_path"
4456
+ job_progress_pass "$branch" "$state_abs" final
4357
4457
  job_write_status "$branch" "$remote_status" "$decision" "$detail"
4358
4458
  echo "job: $final $decision"
4359
4459
  exit 0
@@ -5,7 +5,9 @@
5
5
  # routes an inbox filename to its engine and branch (`hr_inbox_route_var`),
6
6
  # derives a branch name from a title (`hr_derive_branch`), judges whether a
7
7
  # path lies strictly inside the state directory's scratch directory
8
- # (`hr_scratch_path_var`), places a dropped
8
+ # (`hr_scratch_path_var`), answers whether the progress comment is on
9
+ # (`hr_progress_comments`) and which main phases a flow-progress ledger records
10
+ # as through (`hr_ledger_phases`), places a dropped
9
11
  # artifact in a working copy and commits and pushes it, and derives the
10
12
  # anchors (main checkout, work root, worktree directory, repo slug,
11
13
  # state-dir paths) the scripts would otherwise each re-derive slightly
@@ -87,7 +89,10 @@
87
89
  # file is ever removed.
88
90
  # 4. THE ARTIFACT PLACEMENT writes one artifact into a working copy. Fence:
89
91
  # the caller-named `<worktree>/<rel>`, its parent directories and that
90
- # path's index entry, plus whatever the two caller-named wrappers do.
92
+ # path's index entry, plus whatever the two caller-named wrappers do, plus
93
+ # one write of its own: `hr_push_landed`'s fetch, which force-writes
94
+ # `refs/remotes/origin/<branch>` in `<worktree>`, made only after a failed
95
+ # landing, to name the commit the remote moved to.
91
96
  # Written only by `hr_place_artifact`, `hr_commit_placed` and
92
97
  # `hr_push_landed`.
93
98
  #
@@ -96,6 +101,11 @@
96
101
  # HR_REMOTE_WORKFLOW_RUN_FILE mirrors WORKFLOW_RUN_FILE
97
102
  # HR_REMOTE_STATE_ARTIFACT mirrors STATE_ARTIFACT_NAME
98
103
  #
104
+ # MIRROR OF `plugin/instructions/autonomous_pause_and_ledger.md` →
105
+ # `### 1.3 Templates`, which owns the flow-progress ledger's two header forms
106
+ # and its entry ids; `hr_ledger_phases` codes both, so renaming an id or a
107
+ # header form there is an edit here.
108
+ #
99
109
  # A caller that calls no `hr_lane_*`, `hr_registry_init`, `hr_registry_set`,
100
110
  # `hr_remote_record_init`, `hr_registry_lock`, `hr_registry_unlock`, `hr_remote_status_write`,
101
111
  # `hr_remote_bundle_write`, `hr_remote_bundle_restore`, `hr_remote_move_aside`, `hr_place_artifact`,
@@ -600,10 +610,11 @@ hr_config_load() {
600
610
  # which is what lets an explicitly EMPTY list read as a configured set rather
601
611
  # than as an absent one.
602
612
  #
603
- # A `phases.*` or `docs.retrieval` value that is not a boolean is emitted as
604
- # `invalid`, so the string `"true"` — which `tostring` would otherwise make
605
- # indistinguishable from `true` — reaches `hr_phase_enabled` or
606
- # `hr_docs_retrieval_applies` as a value it refuses (2).
613
+ # A `phases.*`, `docs.retrieval` or `execution.progressComments` value that is
614
+ # not a boolean is emitted as `invalid`, so the string `"true"` — which
615
+ # `tostring` would otherwise make indistinguishable from `true` — reaches
616
+ # `hr_phase_enabled`, `hr_docs_retrieval_applies` or `hr_progress_comments` as
617
+ # a value it refuses (2).
607
618
  out=$(jq -n -r '
608
619
  def s($k; $v):
609
620
  if $v == null then empty
@@ -638,6 +649,8 @@ hr_config_load() {
638
649
  s("phases.qa"; try (.phases.qa | if type == "boolean" or . == null then . else "invalid" end) catch null),
639
650
  s("phases.docs"; try (.phases.docs | if type == "boolean" or . == null then . else "invalid" end) catch null),
640
651
  s("execution.target"; try .execution.target catch null),
652
+ s("execution.progressComments";
653
+ try (.execution.progressComments | if type == "boolean" or . == null then . else "invalid" end) catch null),
641
654
  s("docs.retrievalBackend"; try .docs.retrievalBackend catch null),
642
655
  s("docs.retrieval"; try (.docs.retrieval | if type == "boolean" or . == null then . else "invalid" end) catch null),
643
656
  s("forge"; try .forge catch null),
@@ -927,6 +940,107 @@ hr_forge() {
927
940
  return 2
928
941
  }
929
942
 
943
+ # `execution.progressComments` — whether a remote run keeps its progress
944
+ # comment on the pull request. THE ONE READER OF THE KEY IN THIS FAMILY; a
945
+ # script that needs it calls this. PRINTS NOTHING: the answer is the status, as
946
+ # `hr_phase_enabled`'s is, except that an absent key is the schema default
947
+ # `true`. 0 = `true` or absent; 1 = `false`; 2 = the configuration is
948
+ # unresolvable or the value is not a boolean — refused rather than guessed about.
949
+ hr_progress_comments() {
950
+ local root="${1-}"
951
+ hr_config_load "$root" || return 2
952
+ hr_cfg_scalar_var "execution.progressComments" || return 0
953
+ case "$HR_CFG_VALUE" in
954
+ true) return 0 ;;
955
+ false) return 1 ;;
956
+ esac
957
+ return 2
958
+ }
959
+
960
+ # ---------------------------------------------------------------------------
961
+ # The flow-progress ledger — which of a run's four main phases it records as
962
+ # through. A mirror of the plugin's ledger templates; see the header.
963
+ # ---------------------------------------------------------------------------
964
+
965
+ # 0 when every id after <settled> appears in <settled>, a space-delimited list
966
+ # with a leading and trailing space; 1 otherwise.
967
+ hr_ledger_all_settled() {
968
+ local settled="${1-}" id
969
+ shift
970
+ for id in "$@"; do
971
+ case "$settled" in
972
+ *" $id "*) ;;
973
+ *) return 1 ;;
974
+ esac
975
+ done
976
+ return 0
977
+ }
978
+
979
+ # Print `<engine> <round> <phase1> <phase2> <phase3> <phase4>` for the ledger at
980
+ # <ledger_file> and return 0 — `<engine>` is `task` or `user_review`, `<round>`
981
+ # the header's round or `-` for the task engine, each phase `done` or `pending`.
982
+ # Return 1, printing nothing, when the file is missing or unreadable or its first
983
+ # line is not a task-engine or user-review-engine header: any other ledger has
984
+ # no phases to report.
985
+ #
986
+ # A phase is `done` when every id of its set is `[x]` or `[-]` (skipped before
987
+ # the run began). An id absent from the file is NOT settled, so a malformed
988
+ # ledger reads as `pending`, never as `done`. Phase 3 groups every end-of-branch
989
+ # review with its fixes, QA and the run gates.
990
+ hr_ledger_phases() {
991
+ local file="${1-}" line first=1 engine="" round="-" settled=" " rest id
992
+ local p1 p2 p3 p4 s1 s2 s3 s4
993
+ [ -n "$file" ] && [ -f "$file" ] && [ -r "$file" ] || return 1
994
+ while IFS= read -r line || [ -n "$line" ]; do
995
+ if [ "$first" -eq 1 ]; then
996
+ first=0
997
+ case "$line" in
998
+ *"(engine: task)"*)
999
+ engine="task"
1000
+ ;;
1001
+ *"(engine: user_review, round "*")"*)
1002
+ engine="user_review"
1003
+ round=${line#*"(engine: user_review, round "}
1004
+ round=${round%%")"*}
1005
+ case "$round" in
1006
+ ''|*[!0-9]*) return 1 ;;
1007
+ esac
1008
+ ;;
1009
+ *) return 1 ;;
1010
+ esac
1011
+ continue
1012
+ fi
1013
+ case "$line" in
1014
+ "- [x] "*|"- [-] "*) ;;
1015
+ *) continue ;;
1016
+ esac
1017
+ rest=${line#"- ["?"] "}
1018
+ id=${rest%% *}
1019
+ id=${id%.}
1020
+ if [ -n "$id" ]; then settled="$settled$id "; fi
1021
+ done < "$file"
1022
+ [ -n "$engine" ] || return 1
1023
+
1024
+ if [ "$engine" = "task" ]; then
1025
+ p1="P1 P2 P3"
1026
+ p2="A"
1027
+ p3="A1.5g A1.5f A2g A2f Bg Bm C C2g C2m C2f E G"
1028
+ p4="D"
1029
+ else
1030
+ p1="R1 R2"
1031
+ p2="R3"
1032
+ p3="R4 RG"
1033
+ p4="R5"
1034
+ fi
1035
+ # Unquoted on purpose: each set is a fixed list of space-free ids.
1036
+ if hr_ledger_all_settled "$settled" $p1; then s1="done"; else s1="pending"; fi
1037
+ if hr_ledger_all_settled "$settled" $p2; then s2="done"; else s2="pending"; fi
1038
+ if hr_ledger_all_settled "$settled" $p3; then s3="done"; else s3="pending"; fi
1039
+ if hr_ledger_all_settled "$settled" $p4; then s4="done"; else s4="pending"; fi
1040
+ printf '%s %s %s %s %s %s\n' "$engine" "$round" "$s1" "$s2" "$s3" "$s4"
1041
+ return 0
1042
+ }
1043
+
930
1044
  # Whether the docs phase's retrieval step runs: `phases.docs` and
931
1045
  # `docs.retrieval` both `true`. The shell mirror of `cli/src/config/model.ts` →
932
1046
  # `retrievalApplies`: change the predicate there and here together. PRINTS
@@ -1865,17 +1979,31 @@ hr_commit_placed() {
1865
1979
  return 0
1866
1980
  }
1867
1981
 
1868
- # hr_push_landed <push_wrapper> <worktree> <branch> — run <push_wrapper>, then 0
1869
- # only when `HEAD` and `refs/remotes/origin/<branch>` both resolve and are
1870
- # equal; 1 otherwise. `push-branch.sh` exits 0 on every path, so its status is
1871
- # never the answer.
1982
+ # hr_push_landed <push_wrapper> <worktree> <branch> — run <push_wrapper>, then
1983
+ # answer from refs alone, because `push-branch.sh` exits 0 on every path:
1984
+ # 0 landed: `HEAD` and `refs/remotes/origin/<branch>` resolve and are equal,
1985
+ # before or after that fetch;
1986
+ # 2 the remote moved: after the failed landing, a fetch of
1987
+ # `refs/remotes/origin/<branch>` succeeds and it names a commit that is not
1988
+ # an ancestor of `HEAD`. `HR_PUSH_REMOTE_TIP` is set to its short id;
1989
+ # 1 anything else — the push was refused, the fetch failed, or a ref did not
1990
+ # resolve.
1991
+ # It never retries and never rebases; the retry is `push-branch.sh`'s.
1872
1992
  hr_push_landed() {
1873
1993
  local wrapper="${1-}" worktree="${2-}" branch="${3-}" head upstream
1994
+ HR_PUSH_REMOTE_TIP=""
1874
1995
  [ -n "$wrapper" ] && [ -n "$worktree" ] && [ -n "$branch" ] || return 1
1875
1996
  "$wrapper" "$worktree"
1876
1997
  head=$(git -C "$worktree" rev-parse --verify --quiet HEAD) || return 1
1998
+ [ -n "$head" ] || return 1
1999
+ upstream=$(git -C "$worktree" rev-parse --verify --quiet "refs/remotes/origin/$branch") \
2000
+ && [ "$head" = "$upstream" ] && return 0
2001
+ git -C "$worktree" fetch --quiet origin "+refs/heads/$branch:refs/remotes/origin/$branch" || return 1
1877
2002
  upstream=$(git -C "$worktree" rev-parse --verify --quiet "refs/remotes/origin/$branch") || return 1
1878
- [ -n "$head" ] && [ "$head" = "$upstream" ]
2003
+ [ "$head" != "$upstream" ] || return 0
2004
+ git -C "$worktree" merge-base --is-ancestor "$upstream" "$head" && return 1
2005
+ HR_PUSH_REMOTE_TIP=$(git -C "$worktree" rev-parse --short "$upstream") || return 1
2006
+ return 2
1879
2007
  }
1880
2008
 
1881
2009
  # ---------------------------------------------------------------------------
@@ -1946,8 +2074,7 @@ hr_push_landed() {
1946
2074
  # the counters that must survive a job boundary
1947
2075
  # chain the writing job's OWN input `HARNESS_INPUT_CHAIN`, never
1948
2076
  # a value carried from an earlier bundle
1949
- # control_polled_at the epoch second up to which the job checked for a
1950
- # `harness pause <branch>` run, or empty
2077
+ # control_polled_at the lower bound of the next control poll, or empty
1951
2078
  # decision `continue` | `wait-poller` | `stop`
1952
2079
  # detail one human-readable line
1953
2080
  # run_id, run_url, written_at
@@ -28,7 +28,13 @@
28
28
  # must NEVER abort the calling run — a stale remote is recoverable and a halted
29
29
  # run is not. That is why this script uses `set -uo pipefail` deliberately
30
30
  # WITHOUT `set -e`, and why even the fail-closed arm below exits 0: it refuses
31
- # to push, which is the closed outcome here, and says so.
31
+ # to push, which is the closed outcome here, and says so. A push the remote
32
+ # refused for any reason but a `[rejected]` ref is attempted up to
33
+ # PUSH_ATTEMPTS times in all — waiting PUSH_RETRY_DELAY_SECS before the second
34
+ # attempt and three times that before the third — and is still never fatal once
35
+ # given up. PUSH_RETRY_DELAY_SECS is read from the environment as a test and
36
+ # tuning seam: default 5, and anything but a non-negative integer falls back to
37
+ # 5 with one line.
32
38
  #
33
39
  # DEFENCE IN DEPTH, NOT THE SOLE GUARD. The caller-agnostic backstop is the
34
40
  # committed pre-push hook `init` writes into the configured `githooksDir`, which
@@ -52,7 +58,11 @@
52
58
  #
53
59
  # WHAT IT NEVER DOES. It performs only a fast-forward push of already-committed
54
60
  # work: no `--force`, no `--force-with-lease`, no history-rewriting flag, no
55
- # commit of its own, and no non-zero exit.
61
+ # commit of its own, and no non-zero exit. It never retries a push git reports
62
+ # as `! [rejected]` (non-fast-forward, fetch first), and never fetches or
63
+ # rebases: a push that lost a race fails loudly, which is the run-control rule
64
+ # of record (docs/github-run-control.md -> "A push that loses a race fails
65
+ # loudly, and is never fetched, rebased or retried").
56
66
  #
57
67
  # Usage: push-branch.sh [<repo-dir>]
58
68
  # <repo-dir> worktree/repository to operate in (default: $PWD)
@@ -73,9 +83,35 @@
73
83
  # -> exit 0, refusal message, nothing pushed
74
84
  # push fails git -C "$d" remote set-url origin /nonexistent.git
75
85
  # -> exit 0 with a visible failure line
86
+ # refused once printf '%s\n' '#!/bin/sh' 'c="$GIT_DIR/refusals"' \
87
+ # '[ -e "$c" ] && exit 0' ': > "$c"; exit 1' > "$b/hooks/pre-receive"
88
+ # chmod +x "$b/hooks/pre-receive"
89
+ # git -C "$d" commit -qm x --allow-empty
90
+ # PUSH_RETRY_DELAY_SECS=0 push-branch.sh "$d"
91
+ # -> exit 0, one "retrying" line, and `git -C "$b" rev-parse
92
+ # feat_x` equals `git -C "$d" rev-parse HEAD`
93
+ # remote moved c=$(mktemp -d); git clone -q -b feat_x "$b" "$c"
94
+ # git -C "$c" commit -qm other --allow-empty; git -C "$c" push -q
95
+ # git -C "$d" commit -qm mine --allow-empty; push-branch.sh "$d"
96
+ # -> exit 0, a "(not retried)" line, one push attempt
76
97
 
77
98
  set -uo pipefail
78
99
 
100
+ # Attempts in all, and the wait before the second; the third waits three times
101
+ # that (EVERY FAILURE PATH IS NON-FATAL above).
102
+ PUSH_ATTEMPTS=3
103
+ PUSH_RETRY_DELAY_SECS="${PUSH_RETRY_DELAY_SECS:-5}"
104
+ case "$PUSH_RETRY_DELAY_SECS" in
105
+ '' | *[!0-9]*)
106
+ echo "push-branch.sh: PUSH_RETRY_DELAY_SECS='$PUSH_RETRY_DELAY_SECS' is not a non-negative integer — using 5"
107
+ PUSH_RETRY_DELAY_SECS=5
108
+ ;;
109
+ *)
110
+ # Base 10, so a leading zero is not read as octal.
111
+ PUSH_RETRY_DELAY_SECS=$((10#$PUSH_RETRY_DELAY_SECS))
112
+ ;;
113
+ esac
114
+
79
115
  # The library is reached by a path computed from this script's own location — no
80
116
  # session root, and no runtime-substituted token, is assumed. Not finding it is
81
117
  # non-fatal like everything else here.
@@ -125,16 +161,38 @@ esac
125
161
  # set it on the fly, so a branch with no upstream is still pushed. Fast-forward
126
162
  # only.
127
163
  if git -C "$top" rev-parse --abbrev-ref --symbolic-full-name '@{u}' >/dev/null 2>&1; then
128
- git -C "$top" push
164
+ push_cmd=(git -C "$top" push)
129
165
  else
130
- git -C "$top" push --set-upstream origin "$branch"
166
+ push_cmd=(git -C "$top" push --set-upstream origin "$branch")
131
167
  fi
132
- push_status=$?
133
168
 
134
- if [ "$push_status" -eq 0 ]; then
135
- echo "push-branch.sh: pushed $branch to origin"
136
- else
137
- # Visible but non-blocking: a push problem must never abort the calling run.
138
- echo "push-branch.sh: push failed for $branch (see output above)"
139
- fi
169
+ # Every failure line keeps the `push failed for <branch>` prefix: logs and
170
+ # Gate 12 records quote it.
171
+ attempt=1
172
+ delay="$PUSH_RETRY_DELAY_SECS"
173
+ while :; do
174
+ push_out="$("${push_cmd[@]}" 2>&1)"
175
+ push_status=$?
176
+ # git writes its ref-status lines to stderr; keep them there.
177
+ [ -n "$push_out" ] && printf '%s\n' "$push_out" >&2
178
+
179
+ if [ "$push_status" -eq 0 ]; then
180
+ echo "push-branch.sh: pushed $branch to origin"
181
+ break
182
+ fi
183
+ # A here-string, not a pipe: `grep -q` exiting early would fail a pipeline
184
+ # under pipefail.
185
+ if grep -q '^ ! \[rejected\]' <<<"$push_out"; then
186
+ echo "push-branch.sh: push failed for $branch: origin has commits this branch does not (not retried)"
187
+ break
188
+ fi
189
+ if [ "$attempt" -ge "$PUSH_ATTEMPTS" ]; then
190
+ echo "push-branch.sh: push failed for $branch after $PUSH_ATTEMPTS attempts (see output above)"
191
+ break
192
+ fi
193
+ echo "push-branch.sh: push failed for $branch (attempt $attempt of $PUSH_ATTEMPTS); retrying in ${delay}s"
194
+ sleep "$delay"
195
+ attempt=$((attempt + 1))
196
+ delay=$((delay * 3))
197
+ done
140
198
  exit 0