rungate 0.8.1 → 0.8.2

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.
@@ -9,6 +9,7 @@
9
9
  */
10
10
  import { existsSync, lstatSync, mkdirSync, readFileSync, writeFileSync, statSync } from "fs";
11
11
  import { readdirSorted } from "../readdir-sorted";
12
+ import { fileExtensions } from "../file-extensions";
12
13
  import { join, basename, dirname, resolve, sep } from "path";
13
14
  import { auditSpecs } from "../../scripts/audit-specs";
14
15
  import { generateAgentsMd as buildAgentsMdContent, generateScopedRules } from "../generators/agents-md";
@@ -35,6 +36,7 @@ import {
35
36
  import { PRE_PUSH_MANAGED_MARKER, prePushHookScript } from "./pre-push-hook";
36
37
  import {
37
38
  AI_REVIEW_JOB,
39
+ AI_REVIEW_PULL_REQUEST_TYPES,
38
40
  aiReviewScaffoldAction,
39
41
  buildAuthDetectStep,
40
42
  buildOutcomesHeader,
@@ -1563,9 +1565,11 @@ function generateCodeMapInline(root: string, outPath: string, actions: string[])
1563
1565
  const full = join(root, entry);
1564
1566
  try {
1565
1567
  if (!statSync(full).isDirectory()) continue;
1566
- const files = readdirSorted(full, { recursive: true });
1567
- const exts = new Set(files.map(f => f.split(".").pop()).filter((e): e is string => Boolean(e)));
1568
- dirs.push({ name: entry, types: [...exts].slice(0, 5) });
1568
+ // One binding for what a "type" is, shared with `lib/scanner.ts` and
1569
+ // `scripts/generate-code-map.ts` (#1200). This is the fallback that
1570
+ // runs when the script is unavailable, so a copy here reintroduces
1571
+ // whatever the other two fix.
1572
+ dirs.push({ name: entry, types: fileExtensions(full) });
1569
1573
  } catch {}
1570
1574
  }
1571
1575
  // Unsorted. Row order is `directoryTable`'s single decision (#1163).
@@ -2676,13 +2680,23 @@ export function writeManagedFile(
2676
2680
  * and it would be wrong the first time a different sub-key joins the map.
2677
2681
  */
2678
2682
  function revocationActions(label: string, revoked: readonly RevokedSubKey[]): string[] {
2679
- return revoked.map(
2680
- r =>
2681
- `REVOKED: ${label} no longer declares \`${r.section}: ${r.key}\` — the harness ` +
2682
- `computes that sub-key and this project's \`ci\` config no longer produces it, so ` +
2683
- `the grant has nothing left to justify it. Name \`${r.section}\` in ` +
2684
- `\`ci.workflowOverrides\` to keep it, and re-scaffold.`,
2685
- );
2683
+ return revoked.map(r => {
2684
+ // The override hatch is named per TOP-LEVEL key, so an authored path
2685
+ // (`on.pull_request`) has to advise `on` and not the path it was found
2686
+ // at. Advice naming a key `ci.workflowOverrides` would reject is worse
2687
+ // than none: the consumer follows it and the filter goes anyway.
2688
+ const topLevel = r.section.split(".")[0];
2689
+ const keep =
2690
+ `Name \`${topLevel}\` in \`ci.workflowOverrides\` to keep it, and re-scaffold.`;
2691
+ return r.reason === "authored"
2692
+ ? `REVOKED: ${label} no longer declares \`${r.section}: ${r.key}\` — the harness ` +
2693
+ `authors that path and this version of the generator does not emit it, so the ` +
2694
+ `value here is one the harness itself wrote and has since changed its mind ` +
2695
+ `about. ${keep}`
2696
+ : `REVOKED: ${label} no longer declares \`${r.section}: ${r.key}\` — the harness ` +
2697
+ `computes that sub-key and this project's \`ci\` config no longer produces it, so ` +
2698
+ `the grant has nothing left to justify it. ${keep}`;
2699
+ });
2686
2700
  }
2687
2701
 
2688
2702
  /** One globally installed CI tool, and the exact version it is pinned to. */
@@ -2806,21 +2820,63 @@ export const SHARD_AGGREGATE_REFUSAL_EXIT = 1;
2806
2820
  *
2807
2821
  * The script contains **no `${{ }}` expression at all**. Every GitHub-side
2808
2822
  * value arrives through the step's `env:`, which is what makes the shipped
2809
- * script text executable by a test with a stubbed `gh` on PATH. A script that
2810
- * can only run inside Actions is a script no negative control can reach, and
2811
- * the negative control is the whole of #1181.
2823
+ * script text executable by a test that plants a marker directory on disk. A
2824
+ * script that can only run inside Actions is a script no negative control can
2825
+ * reach, and the negative control is the whole of #1181.
2812
2826
  */
2813
2827
  export const SHARD_AGGREGATE_ENV = {
2814
2828
  rollup: "SHARD_ROLLUP",
2815
2829
  expected: "SHARD_EXPECTED",
2816
2830
  jobName: "SHARD_JOB_NAME",
2817
- repo: "SHARD_REPO",
2818
- runId: "SHARD_RUN_ID",
2819
- runAttempt: "SHARD_RUN_ATTEMPT",
2820
- pollAttempts: "SHARD_POLL_ATTEMPTS",
2821
- pollSeconds: "SHARD_POLL_SECONDS",
2831
+ attempt: "SHARD_ATTEMPT",
2832
+ markerDir: "SHARD_MARKER_DIR",
2833
+ } as const;
2834
+
2835
+ /**
2836
+ * Environment variable names the per-shard marker step writes from (#1190).
2837
+ *
2838
+ * Separate from `SHARD_AGGREGATE_ENV` because they are read on the other side
2839
+ * of the fan-in, by a different job, and a single map would suggest one step
2840
+ * sets them all.
2841
+ */
2842
+ export const SHARD_MARKER_ENV = {
2843
+ index: "SHARD_INDEX",
2844
+ status: "SHARD_STATUS",
2845
+ attempt: "SHARD_ATTEMPT",
2846
+ dir: "SHARD_MARKER_DIR",
2822
2847
  } as const;
2823
2848
 
2849
+ /**
2850
+ * The artifact name one shard's marker is uploaded under (#1190).
2851
+ *
2852
+ * The full name is `<prefix><run_attempt>-<shard>`, and **both** halves are
2853
+ * load-bearing.
2854
+ *
2855
+ * `<shard>` because v4 artifacts are immutable and names must be unique
2856
+ * within a run — the shape `actions/upload-artifact`'s own README prescribes
2857
+ * for a matrix.
2858
+ *
2859
+ * `<run_attempt>` because **artifacts are scoped to the RUN, not the
2860
+ * attempt**, and that was measured rather than assumed. On run `39835866754`
2861
+ * a two-leg matrix was re-run with "Re-run failed jobs"; on attempt 2 the
2862
+ * aggregate's download reported `Found 2 artifact(s)` and read
2863
+ * `shard-1 => success attempt=1` — evidence written by a leg that **did not
2864
+ * execute in attempt 2 at all**. Without the attempt in the name, a re-run in
2865
+ * which a leg never executes reads that leg's stale `success` as proof it
2866
+ * ran, which is #1181 reintroduced with a required check gone green over
2867
+ * shards that did not run.
2868
+ *
2869
+ * The aggregate matches on the prefix rather than enumerating names, so the
2870
+ * two sides agree through this one declaration instead of through two copies
2871
+ * of a loop bound. It downloads EVERY attempt's artifacts deliberately: a
2872
+ * pattern narrowed to this attempt would make a stale leg indistinguishable
2873
+ * from a leg that never ran, and those want different sentences.
2874
+ */
2875
+ export const SHARD_MARKER_ARTIFACT_PREFIX = "rungate-shard-marker-";
2876
+
2877
+ /** The file one shard writes inside its marker directory, by index. */
2878
+ export const SHARD_MARKER_FILE_PREFIX = "shard-";
2879
+
2824
2880
  /**
2825
2881
  * The shard aggregate's verdict script — it asserts that it MEASURED (#1181).
2826
2882
  *
@@ -2843,21 +2899,39 @@ export const SHARD_AGGREGATE_ENV = {
2843
2899
  * ## What this does instead
2844
2900
  *
2845
2901
  * The rollup is still read first, so a failed or a cancelled shard produces
2846
- * exactly the diagnostic it produces today. Then the job asks the Actions API
2847
- * how many `test-shard (N)` jobs this run **actually concluded**, and refuses
2848
- * unless that count equals the matrix width. The printed count is the measured
2849
- * one, never the generated one: a sentence that says "4" because the generator
2850
- * wrote "4" is the thing that lied on `39799846607`.
2851
- *
2852
- * Every reading that cannot be taken is a refusal. No `gh`, no token, an API
2853
- * error, a malformed count — all exit non-zero, because UNMEASURED is not a
2854
- * pass. A consumer whose runner has no `gh` gets a red required check rather
2855
- * than a green one that measured nothing.
2856
- *
2857
- * The poll exists for propagation lag, not for the defect: `needs` already
2858
- * gates on the legs being terminal, so on an ordinary run the first reading
2859
- * is complete and the loop costs nothing. It bounds how long a genuinely
2860
- * stalled shard delays the refusal.
2902
+ * exactly the diagnostic it produces today. Then the job counts the markers
2903
+ * the shards wrote **about themselves** — one tiny artifact per matrix leg,
2904
+ * uploaded by the leg as its last step — and refuses unless there is one per
2905
+ * declared shard and every one of them says `success`. The printed count is
2906
+ * the measured one, never the generated one: a sentence that says "4" because
2907
+ * the generator wrote "4" is the thing that lied on `39799846607`.
2908
+ *
2909
+ * Every reading that cannot be taken is a refusal. No marker directory, a
2910
+ * missing marker, an unreadable marker, a malformed count — all exit
2911
+ * non-zero, because UNMEASURED is not a pass.
2912
+ *
2913
+ * ## Why not the Actions API (#1190)
2914
+ *
2915
+ * #1189 answered the same question with `gh api …/jobs`, which worked and
2916
+ * cost every consumer two things the generated `test` job never needed
2917
+ * before: **`gh` on the runner image**, which is a GitHub-hosted-image fact
2918
+ * and not a runner fact — a self-hosted or container `runs-on`, which
2919
+ * `ci.runner` lets a consumer choose, has no `gh` — and **`actions: read` on
2920
+ * the job's token**. A marker needs neither. The runner reaches the artifact
2921
+ * service for the run it is already in, so upload and same-run download take
2922
+ * no `permissions:` scope and no token at all (`actions/download-artifact`'s
2923
+ * README: a `github-token` and `actions: read` are "required when downloading
2924
+ * artifacts from a different repository or from a different workflow run").
2925
+ *
2926
+ * A marker is also strictly better evidence than a conclusion. The API says
2927
+ * what GitHub recorded about a job; a marker is a file the job's own shell
2928
+ * wrote, so a leg that was created and then never executed leaves nothing
2929
+ * behind — which is exactly `39799846607`'s three queued shards.
2930
+ *
2931
+ * And the poll is gone with it. It existed for API propagation lag; a marker
2932
+ * uploaded before the leg concluded is downloadable the moment `needs` lets
2933
+ * this job start, so there is no lag to wait out and no `SHARD_POLL_*` knob
2934
+ * to surface in config.
2861
2935
  */
2862
2936
  export function buildShardAggregateScript(): string {
2863
2937
  const E = SHARD_AGGREGATE_ENV;
@@ -2873,7 +2947,7 @@ if [ "$rollup" != "success" ]; then
2873
2947
  fi
2874
2948
 
2875
2949
  # 2. A success rollup is not a measurement. Everything below establishes how
2876
- # many shards this run actually concluded (#1181).
2950
+ # many shards this run actually EXECUTED (#1181).
2877
2951
  expected="\${${E.expected}:-}"
2878
2952
  case "$expected" in
2879
2953
  "" | *[!0-9]*)
@@ -2887,69 +2961,146 @@ if [ "$expected" -lt 1 ]; then
2887
2961
  fi
2888
2962
 
2889
2963
  job_name="\${${E.jobName}:-test-shard}"
2890
- attempts="\${${E.pollAttempts}:-3}"
2891
- pause="\${${E.pollSeconds}:-5}"
2892
- attempt=1
2893
-
2894
- while : ; do
2895
- # \`|| api_status=$?\` rather than a bare assignment followed by \`$?\`.
2896
- # GitHub runs a \`run:\` block as \`bash -e\`, and under errexit an
2897
- # assignment whose only word is a failing command substitution aborts the
2898
- # shell THERE — so the bare form never reaches the handler below, and the
2899
- # operator gets a red job with gh's stderr captured into a variable
2900
- # nothing prints. Measured: \`bash -e\` skipped the handler, \`bash\` ran
2901
- # it. A \`||\` compound is exempt from errexit and \`$?\` is still the
2902
- # substitution's. Same #826 shape this whole step is written against: a
2903
- # script proved correct under a shell the generated step does not use.
2904
- api_status=0
2905
- rows="$(gh api --paginate \\
2906
- "repos/\${${E.repo}}/actions/runs/\${${E.runId}}/attempts/\${${E.runAttempt}}/jobs?per_page=100" \\
2907
- --jq '.jobs[] | [.name, (.conclusion // "none")] | @tsv' 2>&1)" || api_status=$?
2908
- if [ "$api_status" -ne 0 ]; then
2909
- # No reading at all. This is the one branch most likely to be written
2910
- # fail-open, and fail-open here reproduces the defect exactly.
2911
- echo "::error::could not read this run's jobs from the Actions API (gh exit $api_status): $rows"
2912
- echo "::error::the shard count is UNMEASURED, which is not a pass. Refusing."
2964
+
2965
+ # 3. THIS attempt. Artifacts belong to the run, not the attempt — measured on
2966
+ # run 39835866754, where attempt 2's download reported \`Found 2
2967
+ # artifact(s)\` and handed back a marker a leg wrote in attempt 1 and never
2968
+ # rewrote, because that leg did not execute in attempt 2 at all. Reading it
2969
+ # as evidence about attempt 2 is #1181 with a required check green over
2970
+ # shards that did not run, so the attempt is part of what gets checked.
2971
+ attempt="\${${E.attempt}:-}"
2972
+ case "$attempt" in
2973
+ "" | *[!0-9]*)
2974
+ echo "::error::${E.attempt} is '$attempt', which is not a run attempt, so no marker can be dated. UNMEASURED, so refusing."
2913
2975
  exit ${SHARD_AGGREGATE_REFUSAL_EXIT}
2976
+ ;;
2977
+ esac
2978
+
2979
+ # 4. The evidence, written by the shards about THEMSELVES. Each leg's last
2980
+ # step writes one file naming its own status and uploads it; a leg that
2981
+ # was created and never executed uploads nothing, and nothing is exactly
2982
+ # what run 39799846607's three queued shards would have left (#1190).
2983
+ dir="\${${E.markerDir}:-}"
2984
+ if [ -z "$dir" ]; then
2985
+ echo "::error::${E.markerDir} is unset, so this job has nowhere to read the shards' own evidence."
2986
+ echo "::error::the shard count is UNMEASURED, which is not a pass. Refusing."
2987
+ exit ${SHARD_AGGREGATE_REFUSAL_EXIT}
2988
+ fi
2989
+ if [ ! -d "$dir" ]; then
2990
+ # The download produced nothing at all. This is the one branch most likely
2991
+ # to be written fail-open — the download step is \`continue-on-error\` so
2992
+ # that the refusal is THIS script's, with a sentence, rather than the
2993
+ # action's bare red — and fail-open here reproduces the defect exactly.
2994
+ echo "::error::no shard markers were downloaded: '$dir' does not exist, so not one of the $expected shards left evidence that it ran."
2995
+ echo "::error::the shard count is UNMEASURED, which is not a pass. Refusing."
2996
+ exit ${SHARD_AGGREGATE_REFUSAL_EXIT}
2997
+ fi
2998
+
2999
+ # Every artifact directory belonging to THIS attempt. Counted off the disk
3000
+ # rather than off the 1..expected walk below, so a leg that contributed more
3001
+ # than its own evidence disagrees out loud instead of being skipped by a loop
3002
+ # that only looks where it expects.
3003
+ found=0
3004
+ for art in "$dir"/${SHARD_MARKER_ARTIFACT_PREFIX}"$attempt"-*; do
3005
+ [ -d "$art" ] || continue
3006
+ found=$((found + 1))
3007
+ done
3008
+
3009
+ passed=0
3010
+ missing=0
3011
+ stale=0
3012
+ unbound=0
3013
+ detail=""
3014
+ n=1
3015
+ while [ "$n" -le "$expected" ]; do
3016
+ want="${SHARD_MARKER_FILE_PREFIX}$n"
3017
+ art="$dir/${SHARD_MARKER_ARTIFACT_PREFIX}$attempt-$n"
3018
+
3019
+ if [ ! -d "$art" ]; then
3020
+ # No evidence from this attempt. Evidence from an EARLIER one is not
3021
+ # evidence about this one, but it is a different diagnosis and gets a
3022
+ # different sentence: a partial re-run leaves exactly this state for
3023
+ # every leg GitHub chose not to re-run.
3024
+ older=""
3025
+ for cand in "$dir"/${SHARD_MARKER_ARTIFACT_PREFIX}*-"$n"; do
3026
+ [ -d "$cand" ] || continue
3027
+ older="$older \${cand##*/}"
3028
+ done
3029
+ if [ -n "$older" ]; then
3030
+ stale=$((stale + 1))
3031
+ detail="$detail \\"$job_name ($n)\\"=only-from:$older"
3032
+ else
3033
+ missing=$((missing + 1))
3034
+ detail="$detail \\"$job_name ($n)\\"=no-marker"
3035
+ fi
3036
+ n=$((n + 1))
3037
+ continue
2914
3038
  fi
2915
3039
 
2916
- seen=0
2917
- passed=0
2918
- pending=0
2919
- detail=""
2920
- while IFS="$(printf '\\t')" read -r name conclusion; do
2921
- [ -n "$name" ] || continue
2922
- case "$name" in
2923
- "$job_name ("*")") ;;
2924
- *) continue ;;
2925
- esac
2926
- seen=$((seen + 1))
2927
- case "$conclusion" in
2928
- success) passed=$((passed + 1)) ;;
2929
- none | "")
2930
- pending=$((pending + 1))
2931
- detail="$detail \\"$name\\"=no-conclusion"
2932
- ;;
2933
- *) detail="$detail \\"$name\\"=$conclusion" ;;
2934
- esac
2935
- done <<< "$rows"
2936
-
2937
- # Complete reading, or out of attempts. The loop is for API propagation
2938
- # lag on an ordinary run, not for the defect.
2939
- if [ "$pending" -eq 0 ] && [ "$seen" -ge "$expected" ]; then break; fi
2940
- if [ "$attempt" -ge "$attempts" ]; then break; fi
2941
- attempt=$((attempt + 1))
2942
- sleep "$pause"
3040
+ # One artifact carries one leg's marker and nothing else. The upload names
3041
+ # a single FILE, so this can only fail if something upstream changed —
3042
+ # which is the point: "each leg produced its own marker" is the claim, and
3043
+ # a flattened directory read by filename alone does not measure it.
3044
+ files=0
3045
+ for f in "$art"/*; do
3046
+ [ -f "$f" ] || continue
3047
+ files=$((files + 1))
3048
+ done
3049
+ if [ "$files" -ne 1 ] || [ ! -f "$art/$want" ]; then
3050
+ unbound=$((unbound + 1))
3051
+ detail="$detail \\"$job_name ($n)\\"=artifact-holds-$files-file\\(s\\)-not-just-$want"
3052
+ n=$((n + 1))
3053
+ continue
3054
+ fi
3055
+
3056
+ # \`|| true\` rather than a bare \`read\`. GitHub runs a \`run:\` block as
3057
+ # \`bash -e\`, and under errexit a \`read\` that returns non-zero aborts the
3058
+ # shell THERE — so the handlers below never run and the operator gets a red
3059
+ # job with no sentence saying why. Measured in #1191 on this same step.
3060
+ mstatus=""
3061
+ mattempt=""
3062
+ mshard=""
3063
+ read -r mstatus mattempt mshard < "$art/$want" || true
3064
+
3065
+ # The body is the SECOND binding, independent of the name. A name is a
3066
+ # convention the generator controls; a body is a reading. A generator bug
3067
+ # that templates the wrong attempt into the name is caught here and only
3068
+ # here — which is why adding the attempt to the name alone is not the fix.
3069
+ if [ "$mattempt" != "$attempt" ] || [ "$mshard" != "$n" ]; then
3070
+ unbound=$((unbound + 1))
3071
+ detail="$detail \\"$job_name ($n)\\"=marker-says-attempt-'$mattempt'-shard-'$mshard'"
3072
+ n=$((n + 1))
3073
+ continue
3074
+ fi
3075
+
3076
+ case "$mstatus" in
3077
+ success) passed=$((passed + 1)) ;;
3078
+ "") detail="$detail \\"$job_name ($n)\\"=empty-marker" ;;
3079
+ *) detail="$detail \\"$job_name ($n)\\"=$mstatus" ;;
3080
+ esac
3081
+ n=$((n + 1))
2943
3082
  done
2944
3083
 
2945
- if [ "$seen" -ne "$expected" ]; then
2946
- echo "::error::this run reports $seen \\"$job_name\\" jobs, and the matrix declares $expected. The aggregate cannot speak for a suite it did not measure."
3084
+ if [ "$missing" -ne 0 ]; then
3085
+ echo "::error::$missing of $expected shards left NO marker, so they never executed:$detail"
3086
+ echo "::error::a shard that never ran is UNMEASURED, not a pass — this is #1181, where three of four were queued and the aggregate went green."
3087
+ exit ${SHARD_AGGREGATE_REFUSAL_EXIT}
3088
+ fi
3089
+
3090
+ if [ "$stale" -ne 0 ]; then
3091
+ echo "::error::$stale of $expected shards left no marker for attempt $attempt, only for an earlier one:$detail"
3092
+ echo "::error::artifacts belong to the RUN, not the attempt, so an older marker is readable here and says nothing about attempt $attempt. Re-run ALL jobs — a partial re-run leaves the legs it did not re-run UNMEASURED, and UNMEASURED is not a pass."
3093
+ exit ${SHARD_AGGREGATE_REFUSAL_EXIT}
3094
+ fi
3095
+
3096
+ if [ "$unbound" -ne 0 ]; then
3097
+ echo "::error::$unbound of $expected shard markers do not identify the leg and attempt that wrote them:$detail"
3098
+ echo "::error::evidence that cannot be tied to one leg of this attempt is UNMEASURED, not a pass. Refusing."
2947
3099
  exit ${SHARD_AGGREGATE_REFUSAL_EXIT}
2948
3100
  fi
2949
3101
 
2950
- if [ "$pending" -ne 0 ]; then
2951
- echo "::error::$pending of $expected shards produced NO conclusion:$detail"
2952
- echo "::error::a shard that never concluded is UNMEASURED, not a pass — this is #1181, where three of four were queued and the aggregate went green."
3102
+ if [ "$found" -ne "$expected" ]; then
3103
+ echo "::error::attempt $attempt left $found shard-marker artifacts, and the matrix declares $expected. The aggregate cannot speak for a suite it did not measure."
2953
3104
  exit ${SHARD_AGGREGATE_REFUSAL_EXIT}
2954
3105
  fi
2955
3106
 
@@ -2964,6 +3115,76 @@ echo "All $passed shards concluded, and all $passed passed."
2964
3115
  `;
2965
3116
  }
2966
3117
 
3118
+ /**
3119
+ * The steps one matrix leg runs to leave evidence that it executed (#1190).
3120
+ *
3121
+ * Two steps rather than one: a shell step writes the file, and
3122
+ * `actions/upload-artifact` carries it to the one place a sibling job can
3123
+ * read. Both are `if: always()`, because a leg that FAILED still executed and
3124
+ * the aggregate distinguishes "ran and was red" from "never ran" — collapsing
3125
+ * those two is #1181.
3126
+ *
3127
+ * The artifact name carries the run attempt as well as the matrix index, and
3128
+ * so does the marker's BODY. Two independent bindings, because a name is a
3129
+ * convention the generator controls and a body is a reading: a generator that
3130
+ * templated the wrong expression into the name would still be caught. The
3131
+ * attempt is there because artifacts are scoped to the run — see
3132
+ * `SHARD_MARKER_ARTIFACT_PREFIX` for the run this was measured on.
3133
+ *
3134
+ * The attempt in the name also removes the need for `overwrite`, which the
3135
+ * first draft of #1190 carried: names are now unique per attempt, so a
3136
+ * re-running leg collides with nothing and no deletion primitive is needed.
3137
+ *
3138
+ * `path:` names a single FILE, not the directory. A directory upload plus the
3139
+ * aggregate's `merge-multiple` flatten would discard the name's binding on
3140
+ * the reading side — a leg that wrote `shard-1`, `shard-2`, `shard-3` into
3141
+ * its own temp directory would contribute evidence for legs it is not, and
3142
+ * "each leg produced its own marker" would not be what the check measures.
3143
+ * Measured on run 39836500284: the leg writes a decoy `shard-99` and the
3144
+ * artifact carries only `shard-<its own index>`.
3145
+ *
3146
+ * The directory is created FRESH each run, so nothing an earlier step left
3147
+ * behind can ship as evidence.
3148
+ *
3149
+ * `if-no-files-found: error` because a marker step that produced no file is a
3150
+ * shard whose evidence is UNMEASURED, and the honest place to say so is in
3151
+ * the leg that failed to write it.
3152
+ */
3153
+ export function buildShardMarkerSteps(indent: string): string {
3154
+ const M = SHARD_MARKER_ENV;
3155
+ const dir = `\${{ runner.temp }}/rungate-shard-marker`;
3156
+ const file = `${SHARD_MARKER_FILE_PREFIX}\${{ matrix.shard }}`;
3157
+ const lines = [
3158
+ `# #1190. The aggregate below counts these instead of asking the Actions`,
3159
+ `# API: \`gh\` is a GitHub-hosted-image fact rather than a runner fact, and`,
3160
+ `# \`actions: read\` is a scope a consumer's token has to be granted. A`,
3161
+ `# marker needs neither — same run, same artifact service.`,
3162
+ `- name: Record that this shard executed`,
3163
+ ` if: always()`,
3164
+ ` env:`,
3165
+ ` ${M.index}: \${{ matrix.shard }}`,
3166
+ ` ${M.status}: \${{ job.status }}`,
3167
+ ` ${M.attempt}: \${{ github.run_attempt }}`,
3168
+ ` ${M.dir}: ${dir}`,
3169
+ ` run: |`,
3170
+ ` # Fresh, so nothing an earlier step left behind ships as evidence.`,
3171
+ ` rm -r "$${M.dir}" 2>/dev/null || true`,
3172
+ ` mkdir -p "$${M.dir}"`,
3173
+ ` # status, attempt and leg — the body the aggregate cross-checks`,
3174
+ ` # against the artifact's own name.`,
3175
+ ` printf '%s %s %s\\n' "$${M.status}" "$${M.attempt}" "$${M.index}" > "$${M.dir}/${SHARD_MARKER_FILE_PREFIX}$${M.index}"`,
3176
+ `- uses: ${actionRef("actions/upload-artifact")}`,
3177
+ ` if: always()`,
3178
+ ` with:`,
3179
+ ` name: ${SHARD_MARKER_ARTIFACT_PREFIX}\${{ github.run_attempt }}-\${{ matrix.shard }}`,
3180
+ ` # One FILE, so this leg cannot ship evidence about another leg.`,
3181
+ ` path: ${dir}/${file}`,
3182
+ ` if-no-files-found: error`,
3183
+ ` retention-days: 1`,
3184
+ ];
3185
+ return lines.map(line => `${indent}${line}`).join("\n");
3186
+ }
3187
+
2967
3188
  /** Indent a script for a YAML block scalar, leaving blank lines blank. */
2968
3189
  function indentedScript(script: string, indent: string): string {
2969
3190
  return script
@@ -3139,6 +3360,30 @@ export const CI_PULL_REQUEST_TRIGGER = ` # NO branches: filter, deliberately (#
3139
3360
  # no new coverage.
3140
3361
  pull_request:`;
3141
3362
 
3363
+ /**
3364
+ * `ai-review.yml`'s own `on.pull_request`: `CI_PULL_REQUEST_TRIGGER`'s body
3365
+ * plus an explicit `types:` list (#1207).
3366
+ *
3367
+ * Why this workflow and no other, why all five types are spelled, and what the
3368
+ * label could not do without them: `AI_REVIEW_PULL_REQUEST_TYPES` in
3369
+ * `lib/scaffold/ai-review-step.ts`, which is the single list this renders.
3370
+ *
3371
+ * Built FROM `CI_PULL_REQUEST_TRIGGER` rather than beside it. The no-`branches:`
3372
+ * rationale is #1136's and is identical here; a second copy of that comment
3373
+ * block is a second thing to update when the reasoning moves, and
3374
+ * `test/stacked-pr-ci-trigger.test.ts` checks all four generated bodies for the
3375
+ * property it describes.
3376
+ */
3377
+ export const AI_REVIEW_PULL_REQUEST_TRIGGER = `${CI_PULL_REQUEST_TRIGGER}
3378
+ # #1207. The three GitHub defaults, PLUS \`labeled\`/\`unlabeled\` — and
3379
+ # the defaults are spelled because \`types:\` REPLACES them rather than
3380
+ # extending them. Omitting them would fix the waiver and stop this
3381
+ # required check running on ordinary pushes.
3382
+ #
3383
+ # Only this workflow subscribes to label events, because only this
3384
+ # workflow reads a label. A label is not a code change.
3385
+ types: [${AI_REVIEW_PULL_REQUEST_TYPES.join(", ")}]`;
3386
+
3142
3387
  export const CI_CONCURRENCY = `# The GROUP does the work here, not the flag. A run is only ever cancelled by
3143
3388
  # another run in the SAME group, so keying a push on its own commit makes the
3144
3389
  # question moot: there is never a second occupant to do the cancelling.
@@ -3390,6 +3635,7 @@ ${baseRefStep}
3390
3635
  ${bunStep}
3391
3636
  - run: bun install${globalTools.step}${ciSetupStep()}
3392
3637
  - run: ${test.command} --shard=\${{ matrix.shard }}/${shards}${typecheckStep}
3638
+ ${buildShardMarkerSteps(" ")}
3393
3639
 
3394
3640
  # The aggregate. Named "test" because that is the context branch protection
3395
3641
  # requires; it exists to turn N shard results into the one verdict.
@@ -3401,25 +3647,42 @@ ${bunStep}
3401
3647
  # protection configurations read a skip as satisfied — which would make
3402
3648
  # this aggregate a gate that cannot refuse.
3403
3649
  if: always()
3650
+ # #1190. \`contents: read\` and nothing else. #1189 also took
3651
+ # \`actions: read\` so the step could \`gh api\` this run's jobs, which
3652
+ # worked and made every consumer's required check depend on a token scope
3653
+ # and on \`gh\` being present — neither of which the generated test job
3654
+ # needed before, and neither of which a self-hosted or container
3655
+ # \`ci.runner\` is guaranteed to have. Same-run artifacts take no scope
3656
+ # and no token.
3404
3657
  permissions:
3405
3658
  contents: read
3406
- # #1181. The aggregate asks the Actions API how many shards actually
3407
- # concluded, because \`needs.test-shard.result\` collapses a matrix into
3408
- # one word and reported \`success\` for three legs that never ran.
3409
- actions: read
3410
3659
  steps:
3660
+ - uses: ${actionRef("actions/download-artifact")}
3661
+ # continue-on-error so the REFUSAL is the script's and carries a
3662
+ # sentence. A download that fails here is still red either way, but
3663
+ # the action's own error names none of this — and a check that is
3664
+ # correctly red and silent is the expensive half.
3665
+ continue-on-error: true
3666
+ with:
3667
+ # EVERY attempt's markers, not just this one's. The script dates
3668
+ # each marker itself, so a leg whose only evidence is older gets
3669
+ # "re-run all jobs" rather than "never ran" — a pattern narrowed to
3670
+ # this attempt would make those two indistinguishable.
3671
+ pattern: ${SHARD_MARKER_ARTIFACT_PREFIX}*
3672
+ # NO merge-multiple: each artifact keeps its own directory, which
3673
+ # is what ties a marker to the leg that uploaded it. Flattening
3674
+ # discards that and the reading falls back to filenames alone.
3675
+ path: \${{ runner.temp }}/rungate-shard-markers
3411
3676
  - name: Every shard concluded, and every shard passed
3412
3677
  env:
3413
3678
  # Every GitHub-side value arrives here, and the script below holds
3414
3679
  # no GitHub expression at all — that is what lets a test execute
3415
- # the shipped text with a stubbed gh and watch it refuse (#1181).
3680
+ # the shipped text against a planted marker directory (#1181).
3416
3681
  ${SHARD_AGGREGATE_ENV.rollup}: \${{ needs.test-shard.result }}
3417
3682
  ${SHARD_AGGREGATE_ENV.expected}: "${shards}"
3418
3683
  ${SHARD_AGGREGATE_ENV.jobName}: test-shard
3419
- ${SHARD_AGGREGATE_ENV.repo}: \${{ github.repository }}
3420
- ${SHARD_AGGREGATE_ENV.runId}: \${{ github.run_id }}
3421
- ${SHARD_AGGREGATE_ENV.runAttempt}: \${{ github.run_attempt }}
3422
- GH_TOKEN: \${{ github.token }}
3684
+ ${SHARD_AGGREGATE_ENV.attempt}: \${{ github.run_attempt }}
3685
+ ${SHARD_AGGREGATE_ENV.markerDir}: \${{ runner.temp }}/rungate-shard-markers
3423
3686
  run: |
3424
3687
  ${indentedScript(buildShardAggregateScript(), " ")}
3425
3688
  `
@@ -3638,10 +3901,27 @@ ${bunStep}
3638
3901
  // `id-token: write` is what lets the runner mint the OIDC token GCP's STS
3639
3902
  // exchanges. Granted ONLY when there is a federation to use it, because a
3640
3903
  // permission nobody consumes is a permission nobody audits.
3904
+ //
3905
+ // `actions: read` is #1168's, and it is granted unconditionally because
3906
+ // the round counter it serves runs on every review. It is READ on the
3907
+ // workflow's own run list and nothing else: the step lifts one integer,
3908
+ // `total_count`, which is how many times this workflow has run on this
3909
+ // pull request — the only thing in reach that is actually a review round.
3910
+ // A permission nobody consumes is a permission nobody audits, and this one
3911
+ // has exactly one consumer, named in `buildRoundsScript`.
3912
+ //
3913
+ // LAST in the block, after `id-token: write`, and the order is not
3914
+ // cosmetic. This file is job-preserving: a consumer who already has the
3915
+ // workflow gets the new key MERGED into their existing permissions block,
3916
+ // which appends. A generator emitting it in the middle therefore disagrees
3917
+ // with what every already-scaffolded tree ends up holding, and AC-12 in
3918
+ // test/ai-review-surface-census.test.ts compares the committed file
3919
+ // against a FRESH generation — so the two orders are a byte difference
3920
+ // that reads as a hand-edit. Measured: that is exactly how this was found.
3641
3921
  const aiReviewPermissions =
3642
3922
  vertex.config === null
3643
- ? `permissions:\n contents: read`
3644
- : `permissions:\n contents: read\n # Mint the OIDC token Workload Identity Federation exchanges (#631).\n id-token: write`;
3923
+ ? `permissions:\n contents: read\n # Count this pull request's previous AI Review runs — the round budget (#1168).\n actions: read`
3924
+ : `permissions:\n contents: read\n # Mint the OIDC token Workload Identity Federation exchanges (#631).\n id-token: write\n # Count this pull request's previous AI Review runs — the round budget (#1168).\n actions: read`;
3645
3925
 
3646
3926
  const aiReviewYml = `# Managed by rungate — do not edit. Customize the ci section of .claude/rungate/config.json.
3647
3927
  #
@@ -3668,7 +3948,7 @@ on:
3668
3948
  # See the block comment in lib/scaffold/steps.ts: no branches: filter, and
3669
3949
  # its absence hangs a merge queue silently rather than failing it (#867).
3670
3950
  merge_group:
3671
- ${CI_PULL_REQUEST_TRIGGER}
3951
+ ${AI_REVIEW_PULL_REQUEST_TRIGGER}
3672
3952
 
3673
3953
  ${aiReviewPermissions}
3674
3954
 
package/lib/scanner.ts CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
  import { existsSync, readFileSync, statSync } from "fs";
12
12
  import { readdirSorted } from "./readdir-sorted";
13
+ import { fileExtensions } from "./file-extensions";
13
14
  import { join, basename } from "path";
14
15
  import { resolveConfig } from "./config-loader";
15
16
  // A leaf, not a generator. SC-365 / AC-3 keeps generation logic out of the
@@ -476,9 +477,15 @@ function scanDirs(root: string): DirEntry[] {
476
477
  const full = join(root, entry);
477
478
  try {
478
479
  if (!statSync(full).isDirectory()) continue;
479
- const files = readdirSorted(full, { recursive: true });
480
- const exts = new Set(files.map(f => f.split(".").pop()).filter((e): e is string => Boolean(e)));
481
- dirs.push({ name: entry, types: [...exts].slice(0, 5) });
480
+ // One binding for what a "type" is, shared with
481
+ // `scripts/generate-code-map.ts` and the scaffold's inline fallback
482
+ // in `lib/scaffold/steps.ts` (#1200). The copy that used to be here
483
+ // reported directory names.
484
+ //
485
+ // Naming that fallback by its function would red
486
+ // `test/unit/scanner.test.ts`: it greps this file's raw TEXT for
487
+ // generator symbols, so a COMMENT mentioning one reads as an import.
488
+ dirs.push({ name: entry, types: fileExtensions(full) });
482
489
  } catch {}
483
490
  }
484
491
  } catch {}