Skip to content

Port the risk tier engine to the 0.3.0 layout (report only) - #3394

Open
Birfy wants to merge 5 commits into
mainfrom
dev/3393-risk-gate-port
Open

Birfy wants to merge 5 commits into
mainfrom
dev/3393-risk-gate-port

Conversation

@Birfy

@Birfy Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Related #3393 (stage 1, the risk tier engine; report only)

The 0.2 guard that graded a PR's risk died with the 0.3.0 layout: .github/scripts/risk-gate.mjs and .github/agent/risk-rules.json were deleted in the rewrite, and the 0.2 script itself called gh api, which does not exist on this box. This ports both to the current tree.

Report only. It prints a tier and exits 0; it does not block, label or merge anything, does not touch tests.yml, and introduces no check name.

node .github/scripts/risk-gate.mjs --pr 3382                 # anonymous: no token, no gh
node .github/scripts/risk-gate.mjs --replay 30              # tiers of the last 30 merged PRs
PR=3382 GITHUB_REPOSITORY=owner/repo node .github/scripts/risk-gate.mjs   # what a workflow will pass

The tier is the highest of three things: the path of every changed file (and the old path of a rename), red-flag hits in the added lines of code files, and a size cap for the tier reached so far. unknownPathTier stays T2, so a new directory cannot fall below T2 by default.

Files:

  • .github/agent/risk-rules.json — the path table and caps, recomputed for this layout, with the sample PRs that justify each cap in limitsNote.
  • .github/scripts/risk-tier.mjs — the engine (glob matching, judge, size caps, report). Pure over the rules file and the file list.
  • .github/scripts/risk-gate.mjs — the command: reads the PR over the REST API with the global fetch, follows the Link header so a PR with more than 100 files is read whole (Add staged Teamily-style chat tools and owner-scoped history search #3112 has 194), and uses a token only if the environment has one.
  • .github/scripts/risk-gate.{fixtures,test,judge,api}*.mjs — the 0.2 test file carried over, set-up extracted to risk-gate.fixtures.mjs and the rest split over three files so no file is oversized.

Local, on this branch (node v24.22.3, no token, no gh):

$ node --test '.github/scripts/*.test.mjs'
tests 24 / pass 24 / fail 0

Verdicts from the same command, all four read anonymously against main's layout:

PR tier size reason
#3382 T3 633 lines / 7 files runtime/flybot/plugins/social/test/client-harness.mjs: /new Function\(/ (same in poll-visibility.test.js)
#3353 T2 105 lines / 6 files core/src/host/diagnostics.js, two patches/ds-bot/*.patch — unnamed paths, so T2
#3358 T2 117 lines / 5 files core/src/host/schedules.js, one patch file
#3112 T3 47014 lines / 194 files unreadable diff on core/client.js, plus core/dev/offline-fixture/package.json and a message-fold.js red flag; the file list came from page 2 of the API, so pagination works

Two intended behaviour changes from 0.2, both visible in the tests:

  • --replay filters closed PRs on merged_at instead of calling gh pr list --state merged.
  • render() gives a reason for T1/T2 too: with no T3 hit it lists the paths that set the tier reached, so a T2 verdict is quotable instead of having an empty Why.

One thing for review rather than for this PR: #3382 lands on T3 because it adds new Function('window', readFileSync(...client.js...)) in a new test harness. That is the rule doing what it says, not a bug in the port — but whether new test scaffolding should be able to raise a PR to T3 is a rule decision (tier scope), and I have not tuned the rules to make that PR greener.

Not in this slice: no workflow, no risk:T* label, no queue, no audit gate — those are later stages of #3393.

Path table rewritten for the tree that exists now: .github/, scripts/,
deploy/, the vendor tree and the dependency/deploy manifests are T3 (the old
table named core/, plugins/ and ds-bot/, none of which exist); services/,
manager/, runtime/flybot* and flybot_workspace code are T2; client code,
tests and checked-in pages T1; docs T0. unknownPathTier stays T2 so a path no
rule names is never silently passed as low risk.

Size caps are recomputed from six real changes in this tree (#3390 5L/1F,
#3389 54L/5F, #3386 280L/11F, #3391 386L/13F, #3382 633L/7F, #3388
1128L/14F): the old caps would have called #3388 T3. limitsNote records it.

The T3 meaning no longer claims a self-PASS lets the gate merge by itself;
this file only reports a tier.
The 0.2 script mixed the rules engine, the network reads and the CLI in one file. The engine
(tier paths, glob matching, judge, size caps, report) is pure over the rules file and the list of
changed files, so it moves to risk-tier.mjs on its own; the command and the REST reads follow in
risk-gate.mjs, which re-exports this module.

No behaviour change: same ORDER, globToRe, judge, summaryLine, render, loadRules as the 0.2
version, and the two intended differences of the port (a reason for T1/T2 as well as T3, and a
`--replay` that filters on merged_at instead of `gh pr list`) live here and in the CLI.
The 0.2 script shelled out to `gh api`, which does not exist here. This reads the REST API with the
global fetch, so the command runs with no `gh` and no token: `node .github/scripts/risk-gate.mjs
--pr 3382` is the acceptance command. A token is used when the environment has one (the anonymous
core quota is 60/h and shared) and is never required; a spent quota is named in the error with its
reset time instead of a bare 403.

The changed-file list is read page by page off the Link header, so a PR with more than 100 files is
read whole (#3112 has 194), and `--replay` filters closed PRs on merged_at rather than calling
`gh pr list --state merged`, which no longer exists.

Report only: it prints a tier and exits 0; it does not block, label, merge or touch tests.yml.
The 0.2 test file is carried over as-is: glob matching, tierOf precedence, judge over a set of
files, content red flags (JS and Python), renames, size caps, the truncated-list rule, the report
and the summary line, plus the #679 topic narrowing. Added for this layout: every tier path and cap
in .github/agent/risk-rules.json is asserted against real paths of this tree, and the unknown-path
default must stay at T2 or above.

The set-up shared by the test files (rules, gate, a changed-file factory) moves to
risk-gate.fixtures.mjs so each test file stays small.

Local: node --test '.github/scripts/*.test.mjs' -> 24 tests, 24 pass, 0 fail.
… without a network

Second and third test file of the same 24-test suite, split only so no file is oversized:
- risk-gate-judge.test.mjs: what must not raise a tier (removed lines, prose, allowed hosts), what
  must (an unreadable diff, generated-file counting, size caps at the tier reached, a possibly
  truncated list), the T2-is-quotable report, the summary line, and the rules file's own shape.
- risk-gate-api.test.mjs: the REST layer with a stubbed fetch and no token: an anonymous request
  sends no authorization header, a token is sent when one is set, the Link header is followed, a
  spent quota is named in the error, --replay keeps only merged PRs, and the command does not shell
  out to `gh`.

Local: node --test '.github/scripts/*.test.mjs' -> 24 tests, 24 pass, 0 fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant