Skip to content

Guard comment writes against repeats, and audit orphan agent labels (report-only) - #3395

Open
Birfy wants to merge 20 commits into
mainfrom
dev/3393-write-guards
Open

Birfy wants to merge 20 commits into
mainfrom
dev/3393-write-guards

Conversation

@Birfy

@Birfy Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Related #3393

Two report-only guards for the write path we just watched fail: the same comment landed twice on 19 objects, and agent-working / agent-pr labels stay on objects whose work or PR is gone. Neither script writes anything; wiring them into workflows is a later slice.

What is here

.github/scripts/comment-text.mjs — the text side. <!-- flybot:<id> --> markers, bodyKey fingerprint, Jaccard over 3-grams.
.github/scripts/comment-guard.mjs — guard(comments, event, draft) returns skip / review / post; --check-only exits 0 = post, 3 = already said, 4 = needs a human.
.github/scripts/label-hygiene.mjs — agent-working / agent-pr orphans only; every other label is listed under Not touched and never judged.
Two test files beside them.

Two decisions worth reviewing:

  • Duplicate detection stays inside one object. Our close template is shared by every object, so comparing across objects marks 91 of 94 pairs as duplicates. Measured on the replay fixture, not assumed.
  • A near-duplicate goes to review, not skip. The guard does not decide that two different sentences say the same thing; it stops the poster and hands over the comment id and score.

Verified on head 16d83500001f8e38a64a889c3dea4899b95a6607

Local, no token, no network:

$ node .github/scripts/comment-guard.mjs --replay /data/workspace/.qa-triage/replay/comments-replay.json
Replayed 26 objects; 19 flagged as duplicates; 1 missed (a duplicate stays, a comment is only written twice); 0 false positive (blocks a real comment).
[exit 0]

$ node .github/scripts/comment-guard.mjs --replay-guard /data/workspace/.qa-triage/replay/comments-replay.json
Replayed 20 objects; 14 skipped as already said, 5 sent to review, 1 would post (of which 0 are text-detectable and must not).
[exit 0]

$ node .github/scripts/label-hygiene.mjs --issues /data/tmp/issues-open.json --prs /data/tmp/prs-facts.json
#3066   agent-pr      orphan PR #3067 is closed unmerged
Scanned 28 objects; 1 orphan label(s) to strip, 0 still describe live work.
Not touched: enhancement×13 area:automation×11 needs-human×9 hold×7 area:security×3 area:docs×2 area:social×2 risk:T2×2 agent-task×1 bug×1 risk:T3×1
[exit 1]

$ node --test .github/scripts/*.test.mjs
ℹ tests 17   ℹ pass 17   ℹ fail 0

The 19: this is every comment pair QA measured. 14 are the same text with and without our marker, so they are skip; 5 were reworded by hand and are review. The would post one is #2292, two different templates for one event — similarity 0.144, the documented limit of a text check, reported as a miss instead of hidden. Misses are tolerated on purpose: a miss leaves a duplicate in place, a false positive blocks a real comment, so the --replay exit is driven by false positives only.

[exit 1] from label-hygiene is the intended signal: one orphan, so a scheduled dry run goes red on drift.

Not in this slice

  • No workflow change. tests.yml does not run these two test files yet; that is the separate CI slice (Port the risk tier engine to the 0.3.0 layout (report only) #3394 lineage), so a green CI run on this PR does not mean these 17 tests ran in CI. The numbers above are local.
  • No label removal, no comment deletion. Stripping the 19 duplicates already on GitHub needs a token and is not part of this.

Birfy added 18 commits November 26, 2026 13:08
…dgeable label red

announces_pr / work_live are not fields any issue payload carries, so on the raw
/issues?state=open snapshot the old version printed "0 orphan label(s)" and exited 0
while #3066 was an orphan. Both are gone: a label is now decided from the Branch: line
our pickup writes, plus the two listings the caller passes in (--prs, whose /pulls shape
carries head.ref, and --refs from git ls-remote).

Three verdicts, and exit 1 = orphan, 2 = cannot be judged, 0 = clean. An unknown is
never 0, so a scheduled run that lacks a fact goes red instead of reporting a clean board.

Related #3393
…-gone verdict

The two fixture tests read payloads outside the repo, so on CI they print as skipped and
their assertions could be edited without anyone noticing. Both now hash the file first
(open.json b93ccd34..., comments-replay.json 5eed6b7d...) and say in their names that CI
skips them.

New cases: the Branch: line is the only PR pointer #3066 has, so a claimed branch that is
gone decides agent-pr as an orphan from --refs alone; a payload with no listings at all
still yields unknown, never a clean board. The CLI exit codes 1 / 2 / 0 are measured by
spawning the script, not by reading its source.

Related #3393
…rgins written down

0.85 was too tight for the data we have: the weakest real duplicate in the replay fixture is
0.861 (B class, the same event twice with one extra number), so it sat 0.011 from the line -
deleting three sentences from a real duplicate dropped it to 0.771 and the guard stayed quiet.
The highest pair that is genuinely different is 0.233, so 0.80 still cannot reach it.

Same 19 duplicates found, same 0 false positives, same 1 documented miss.

Related #3393
… name that CI skips it

The fixture lives outside the repo, so on CI this test printed as skipped and anyone could
edit its assertions without a red run. It now hashes comments-replay.json first
(5eed6b7d...) and the name says "QA fixture only, skipped on CI", so the skip is visible in
the log instead of hidden behind a passing line.

Related #3393
…e case cannot pass for the wrong reason

Running the suite from anywhere but the repo root made node exit 1 on every spawn (module not
found), which is exactly what the closed-object case expects: it passed without running the
script. The path now comes from import.meta.url, and the suite gives 18/18 from both the repo
root and .github/scripts.

Related #3393
…t 3, not exit 1

Two ways this could have stripped a live label. A --refs/--prs listing that came back empty
read as "the branch is gone", which is exactly what a flaked git ls-remote returns; only a
non-empty list may now decide anything, and an empty one is unjudged (exit 2). And a mistyped
path or unparsed payload used to throw (stack, exit 1), the same code as "orphan found", so
anyone wiring `exit 1 -> strip` would have acted on their own typo; input errors now exit 3.

Also: --refs takes git ls-remote output directly (`<sha>\trefs/heads/<name>`), which is the
normal input, not a hand-made JSON array.

Exit codes: 0 clean / 1 orphan found / 2 unjudged / 3 input error.

Related #3393
…d as orphan

The bug this pins: with refs=[] the run called a branch that still exists "gone" and exited 1,
so a flaked git ls-remote plus `exit 1 -> strip` would have stripped a live label. Also covers
the raw ls-remote input form, and the fixture path behind FLYBOT_QA_FIXTURES so the QA payload
is not a hardcoded absolute path.

Related #3393
Same trap as label-hygiene: `--replay` on a path that is not there threw a stack and exited 1,
which a caller reading `exit 1 -> do not post` cannot tell apart from "this comment really is a
duplicate". Input errors are now 3 with one line on stderr.

Exit codes: 0 ok / 1 a real comment would be blocked / 3 input error.

Related #3393
The replay fixture was a hardcoded absolute path, so on any other machine the two replay tests
silently skipped. Now they skip only when the directory really is absent, and the skip text says
so; the sha256 still guards the numbers in the assertions.

Related #3393
…efs)

- an empty or absent listing, against a branch that really exists, must exit 2 (unjudged) and
  never 1: a flaked git ls-remote is exactly `[]`, and 1 reads as "strip".
- `git ls-remote` output must be accepted directly and produce byte-identical output to the
  JSON array; a tags-only listing says nothing about branches, so 2.
- a missing file, unparsed JSON and a bare invocation are 3 with one line of stderr, not a stack
  and not 1. Same for `comment-guard --replay` on a missing path.

Related #3393
… a lost copy-paste)

502e0bb/2460421c were fine, but the comment-guard.mjs I pushed imported norm/similar from
comment-text.mjs, which exports none of those names: the file on the branch was a mangled merge
of two drafts. This restores the version that was measured, keeping the exit-3 wrapper.

Related #3393
Same copy-paste problem as the previous commit: this file was a draft for a design that does not
exist. It now tests the four classes against the real exports, and reads the QA fixture through
FLYBOT_QA_FIXTURES so a different machine skips instead of reading a hardcoded path.

Related #3393
The extra empty-list case I added here duplicates the one in cli-exit-codes.test.mjs, which
drives it through the CLI instead of the API. One file each, as measured: 21 tests.

Related #3393
@Birfy

Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

退出码表(head 96de428a55d6bf7d3180ffbac203f28d67fec27d)

接线方读到的唯一契约。写操作一律只认 1 的语义,因此输入错误必须与「发现孤儿」分开。

code label-hygiene.mjs comment-guard.mjs
0 clean:没有要摘的标签,每条判定都有事实支撑 ok:没有命中,可以发/没有阻断
1 orphan found:有标签对应的分支/PR 已不存在 一条真实的新评论会被判重挡住(--replay 下为 0 false positive 的对立面)
2 unjudged:--refs/--prs 没给或给了空列表,有标签无法判定 —
3 input error:文件不存在、JSON 解析失败、参数不合法(一行 stderr,无栈) input error:同上;--replay/--replay-guard 文件缺失也是 3

comment-guard --comments ... --check-only 另有细分:0 可发 / 3 已说过(skip)/ 4 需人看(review)。

两条硬约束(不是风格问题)

  1. 空列表不是事实。 git ls-remote 抖动就会返回空输出,读成「分支已删」再摘标签就是误摘活标签。只有非空列表可以下 orphan 判定。
  2. 1 只表示「找到了」,不表示「出错了」。把路径写错、JSON 坏掉归到 1,会让 exit 1 → 摘标签/不发评论 这类接线在拼错路径时动手。

--refs 接受两种输入

git ls-remote --heads origin > refs.txt      # <sha>\trefs/heads/<name>,这是常态输入
node .github/scripts/label-hygiene.mjs --issues open.json --refs refs.txt
node .github/scripts/label-hygiene.mjs --issues open.json --refs refs.json   # JSON 数组或 {"refs":[...]}

两者输出逐字节相同(本机 53 条 heads 实测)。

注意:这个 PR 的 CI 不会跑这 21 个测试

tests.yml 未接入 .github/scripts/*.test.mjs(切片 ① 卡在 #3394)。因此本 PR 上任何绿色都与这些测试无关,验证只能靠下面的命令在本地重现:

node --test .github/scripts/*.test.mjs          # 21 pass / 0 fail(未跳过:QA 夹具在本机)
git ls-remote --heads origin > /tmp/refs.txt
node .github/scripts/label-hygiene.mjs --issues <saved /issues?state=open>                    # exit 2, unjudged
node .github/scripts/label-hygiene.mjs --issues <...> --refs /tmp/refs.txt                    # exit 1, 1 orphan (#3066)
node .github/scripts/label-hygiene.mjs --issues <...> --refs /dev/stdin <<< '[]'              # exit 2, 绝不出 orphan
node .github/scripts/label-hygiene.mjs --issues /nope.json                                    # exit 3, 一行 stderr

夹具路径默认 /data/workspace/.qa-triage,可用 FLYBOT_QA_FIXTURES=/path 覆盖;夹具不在的机器上这两个回放测试打印为 skipped,而不是静默通过。

Related #3393

An agent-pr row that already got --refs was still told "pass --prs or --refs":
a caller wiring this up re-passes the flag forever and the run never settles.
Each row now names just the absent fact, and when both were given it says the
payload itself is what has to name the PR. The empty-PR-list case keeps the
existing "no PR list was given" wording (a flaked ls-remote must not read as
an orphan). No verdict changes: a live branch still cannot prove an open PR
carries the label, so that row stays unknown / exit 2 by design.
Three spawn-level cases on one fixture: --refs given (the row must not say
--refs and the summary must ask for --prs), --prs given (the mirror), and both
given (no flag at all, because then the payload is what is missing). The
agent-pr row stays unknown/exit 2 in all three: a live branch cannot prove an
open PR still carries the label.
@Birfy

Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Exit codes, one table per subcommand

Supersedes #issuecomment-7184438697 — that table put --replay and --replay-guard under one "exit 1", and the two mean opposite things. Bound to head 6641a833018c52c8834547372b32053e6eeb0e0e.

label-hygiene.mjs (report only, never writes)

exit meaning what wiring should do
0 clean: every in-scope label still describes something nothing
1 at least one agent-working / agent-pr is an orphan strip those labels, then re-run to confirm 0
2 at least one label could not be judged: the fact was not supplied, or the list given was empty strip nothing; supply the missing --prs / --refs
3 input error: missing file, bad JSON, no arguments stop and fix the input

--refs takes git ls-remote output as it comes out (<sha>\trefs/heads/<name>) or a JSON array; both give byte-identical output. An empty list is not a fact — a flaked ls-remote gives exactly that, so it stays unjudged, never orphan.

comment-guard.mjs --replay <fixture> — did the matcher call real duplicates duplicates?

exit meaning
0 no false positive: every comment it would skip really is a duplicate
1 it would skip a real comment (false positive) — the failure that matters in this mode
3 input error

comment-guard.mjs --replay-guard <fixture> — would the guard have caught the rerun?

exit meaning
0 nothing postable slipped through
1 an A/B-class duplicate (text-detectable by construction) would still be posted — a miss
3 input error

C class (#2292, similarity 0.144) may still post: two different templates for one event are past the limit of a text check, and the run reports it as a miss instead of hiding it. That is why only A/B count toward this exit.

comment-guard.mjs --comments … --check-only — on a live object

exit meaning
0 would post
3 skip: the same marker is already there, or the same text is already on the object
4 near-duplicate: a human decides

In this mode 3 has two sources — "already said" and "input error" — and they are told apart by whether stderr carries comment-guard: input error: …. Treat a 3 without that line as "already said".

comment-guard.mjs --comments … (no --check-only)

exit meaning
0 always: it prints the verdict (skip / review / post) and never fails the run
3 input error

Do not gate on this mode's exit status; gate on --check-only, which maps the verdict to 0/3/4.

One design note that wiring must not "fix"

An agent-pr issue that claims a live branch but names no PR stays unknown / exit 2. A live branch cannot prove an open PR still carries the label, so the honest answer is "I do not know" — and the wiring table reads 2 as do not strip. agent-working decides on the branch, agent-pr decides on the PR; they are different facts on purpose.

Reproduce locally, no token:

node --test '.github/scripts/*.test.mjs'
node .github/scripts/comment-guard.mjs --replay <fixture>/comments-replay.json
node .github/scripts/comment-guard.mjs --replay-guard <fixture>/comments-replay.json
node .github/scripts/label-hygiene.mjs --issues <fixture>/raw/open.json
node .github/scripts/label-hygiene.mjs --issues <fixture>/raw/open.json --refs <ls-remote --heads output>

CI on this PR does not run these tests: tests.yml still does not cover .github/scripts/*.test.mjs (that is the separate slice behind #3394), so a green run here says nothing about them.

@Birfy

Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Lead review record — head 6641a833018c52c8834547372b32053e6eeb0e0e

Binding correction: the description still says Verified on head 16d83500… with tests 17. That head is dead and those numbers are void. No one in this run has a tool to edit a pull request description (no PATCH /pulls/{n} in the available action set, no token), so this comment and #issuecomment-7184681371 are the canonical record for this head, not the body. The body needs one edit whenever a token exists: 16d83500 → 6641a833, 17 → 22, three test files, and the current output format 0 orphan(s) to strip, 0 live, 1 unjudged.

Checked by me independently on the clean tree from refs/pull/3395/head at 6641a833 (6 files, +646/−0, base 34a80ca5a1):

check result
node --test '.github/scripts/*.test.mjs' tests 22 / pass 22 / fail 0 / skipped 0
missing file, {broken, no args, --replay missing file each one line of stderr, no stack, exit 3
--refs [] / --refs ["main"] exit 2 / exit 1
agent-pr with a live branch and no named PR exit 2, reason names only --prs, the summary line no longer asks for --refs
ls-remote raw text vs JSON array diff empty, both exit 1
--replay 19 flagged / 1 missed / 0 false positive, exit 0
--replay-guard 14 skipped / 5 review / 1 would post, exit 0
7 false-positive probes failures: 0

agent-pr with a live Branch: line and no named PR staying at exit 2 is intended, not a defect: a live branch cannot show that a PR still carries the work, and exit 2 means "do not strip". Only agent-working may read a live branch as keep.

Local green, not CI green: tests.yml does not run these 22 tests, so a green run on this PR does not mean they ran in CI. Wiring them into a workflow is the next slice, and it is blocked on the risk/audit workflow slice landing (#3394 lineage) rather than on this PR.

Related #3393

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