@kw-yeh/vibeflow 4.11.53 → 4.11.55

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.
@@ -0,0 +1,169 @@
1
+ #!/usr/bin/env node
2
+ // Wait, silently, until a PR needs the agent: prints ONE JSON line and exits.
3
+ //
4
+ // node pr-watch.mjs [--pr N] [--interval 60] [--state FILE] [--since-now] [--max-wait SEC] [--once]
5
+ //
6
+ // exit 10 {"event":"comments",...} review items nobody answered and not reported before
7
+ // exit 11 {"event":"base",...} the base branch has commits the PR head lacks
8
+ // exit 0 {"event":"merged"|"closed",...}
9
+ // exit 3 {"event":"timeout"} --max-wait passed (or --once found nothing); run it again
10
+ // exit 2 {"event":"error",...} setup failed, or GitHub kept failing
11
+ import fs from 'node:fs'
12
+ import os from 'node:os'
13
+ import path from 'node:path'
14
+ import { pathToFileURL } from 'node:url'
15
+ import { fetchBaseStatus, fetchPr, itemKey, pendingItems, repoNwo, resolvePrNumber } from './pr-lib.mjs'
16
+
17
+ export const EXIT = { done: 0, error: 2, timeout: 3, comments: 10, base: 11 }
18
+ const MIN_INTERVAL = 20
19
+ const MAX_BACKOFF = 300
20
+ const MAX_FAILURES = 10
21
+
22
+ export function parseArgs(argv) {
23
+ const opts = { interval: 60, sinceNow: false, once: false }
24
+ for (let i = 0; i < argv.length; i += 1) {
25
+ const arg = argv[i]
26
+ const value = () => {
27
+ const v = argv[++i]
28
+ if (v === undefined) throw new Error(`${arg} needs a value`)
29
+ return v
30
+ }
31
+ if (arg === '--pr') opts.pr = value()
32
+ else if (arg === '--interval') opts.interval = Math.max(MIN_INTERVAL, Number(value()) || 60)
33
+ else if (arg === '--state') opts.state = value()
34
+ else if (arg === '--max-wait') opts.maxWait = Number(value())
35
+ else if (arg === '--since-now') opts.sinceNow = true
36
+ else if (arg === '--once') opts.once = true
37
+ else throw new Error(`unknown option ${arg}`)
38
+ }
39
+ return opts
40
+ }
41
+
42
+ export function defaultStatePath(nwo, number, env = process.env) {
43
+ const dir = env.VIBEFLOW_SCRATCH_DIR || os.tmpdir()
44
+ return path.join(dir, `pr-watch-${nwo.replace('/', '-')}-${number}.json`)
45
+ }
46
+
47
+ export function readState(file) {
48
+ try {
49
+ const state = JSON.parse(fs.readFileSync(file, 'utf8'))
50
+ return { seen: Array.isArray(state.seen) ? state.seen : [], baseOid: state.baseOid ?? null }
51
+ } catch {
52
+ return null
53
+ }
54
+ }
55
+
56
+ export function writeState(file, state) {
57
+ fs.mkdirSync(path.dirname(file), { recursive: true })
58
+ const tmp = `${file}.${process.pid}.tmp`
59
+ fs.writeFileSync(tmp, `${JSON.stringify(state)}\n`)
60
+ fs.renameSync(tmp, file)
61
+ }
62
+
63
+ /**
64
+ * What one poll means, given what was already reported. A finished PR wins,
65
+ * then comments (answering them before a rebase keeps their line anchors
66
+ * meaningful), then a base branch the head has fallen behind. A base already
67
+ * reported at this commit is not reported again, so a rebase the agent could
68
+ * not finish does not wake it in a loop.
69
+ */
70
+ export function decide(pr, base, state) {
71
+ if (pr.merged) return { event: { event: 'merged', pr: pr.number }, state }
72
+ if (pr.state === 'CLOSED') return { event: { event: 'closed', pr: pr.number }, state }
73
+
74
+ const seen = new Set(state.seen)
75
+ const fresh = pendingItems(pr).filter((item) => !seen.has(itemKey(item)))
76
+ if (fresh.length) {
77
+ const keys = fresh.map(itemKey)
78
+ return {
79
+ event: { event: 'comments', pr: pr.number, count: fresh.length, items: keys },
80
+ state: { ...state, seen: [...state.seen, ...keys] },
81
+ }
82
+ }
83
+ if (base && base.behindBy > 0 && base.oid !== state.baseOid) {
84
+ return {
85
+ event: { event: 'base', pr: pr.number, base: pr.baseRefName, sha: base.oid, behindBy: base.behindBy },
86
+ state: { ...state, baseOid: base.oid },
87
+ }
88
+ }
89
+ return { event: null, state }
90
+ }
91
+
92
+ /** Mark everything already there as reported, for --since-now. */
93
+ export function baseline(pr, base) {
94
+ return { seen: pendingItems(pr).map(itemKey), baseOid: base?.behindBy > 0 ? base.oid : null }
95
+ }
96
+
97
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
98
+
99
+ export async function watch(opts, deps = {}) {
100
+ const env = deps.env ?? process.env
101
+ const fetch = deps.fetch ?? ((nwo, n) => {
102
+ const pr = fetchPr(nwo, n)
103
+ // A finished PR's head branch is often deleted already, which the comparison cannot name.
104
+ return { pr, base: pr.state === 'OPEN' ? fetchBaseStatus(nwo, pr) : null }
105
+ })
106
+ const wait = deps.sleep ?? sleep
107
+ const now = deps.now ?? (() => Date.now())
108
+
109
+ let nwo
110
+ let number
111
+ try {
112
+ nwo = deps.nwo ?? repoNwo()
113
+ number = deps.number ?? resolvePrNumber(opts.pr)
114
+ } catch (err) {
115
+ return { code: EXIT.error, event: { event: 'error', message: errorText(err) } }
116
+ }
117
+ const file = opts.state ?? defaultStatePath(nwo, number, env)
118
+ let state = readState(file)
119
+ const started = now()
120
+ let failures = 0
121
+ let first = true
122
+
123
+ for (;;) {
124
+ let polled
125
+ try {
126
+ polled = fetch(nwo, number)
127
+ failures = 0
128
+ } catch (err) {
129
+ // A first poll that fails is a setup problem (no PR, gh signed out), not a blip.
130
+ if (first || ++failures >= MAX_FAILURES) {
131
+ return { code: EXIT.error, event: { event: 'error', pr: number, message: errorText(err) } }
132
+ }
133
+ }
134
+ if (polled) {
135
+ if (!state) {
136
+ state = opts.sinceNow ? baseline(polled.pr, polled.base) : { seen: [], baseOid: null }
137
+ writeState(file, state)
138
+ }
139
+ const result = decide(polled.pr, polled.base, state)
140
+ if (result.event) {
141
+ writeState(file, result.state)
142
+ return { code: EXIT[result.event.event] ?? EXIT.done, event: result.event }
143
+ }
144
+ }
145
+ first = false
146
+ if (opts.once || (opts.maxWait && now() - started >= opts.maxWait * 1000)) {
147
+ return { code: EXIT.timeout, event: { event: 'timeout', pr: number } }
148
+ }
149
+ const backoff = failures ? Math.min(MAX_BACKOFF, opts.interval * 2 ** failures) : opts.interval
150
+ await wait(backoff * 1000)
151
+ }
152
+ }
153
+
154
+ function errorText(err) {
155
+ return String(err?.stderr || err?.message || err).trim().split('\n').slice(-3).join(' ')
156
+ }
157
+
158
+ if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
159
+ let opts
160
+ try {
161
+ opts = parseArgs(process.argv.slice(2))
162
+ } catch (err) {
163
+ console.log(JSON.stringify({ event: 'error', message: errorText(err) }))
164
+ process.exit(EXIT.error)
165
+ }
166
+ const { code, event } = await watch(opts)
167
+ console.log(JSON.stringify(event))
168
+ process.exit(code)
169
+ }
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: visual-parity
3
- description: 比對兩個已渲染網頁的樣式落差(新舊版對照 / 改版驗收),跑互動 E2E 流程並蒐集 console 與 network 錯誤,以及在 375/768/1440 三種寬度檢查 RWD 與水平溢出。當使用者提到「新舊頁面長得不一樣」「跟測試環境比對」「像素比對」「樣式對不上」「visual diff」「RWD 檢查」「開 dev server 驗收」「E2E 驗收」時使用。比對與驗收的結果寫進檔案,只回傳壓縮過的落差表。
3
+ description: 比對兩個已渲染網頁的樣式落差(新舊版對照 / 改版驗收)與 OpenCV 像素落差,跑互動 E2E 流程並蒐集 console 與 network 錯誤,以及在 375/768/1440 三種寬度檢查 RWD 與水平溢出。當使用者提到「新舊頁面長得不一樣」「跟測試環境比對」「像素比對」「樣式對不上」「visual diff」「RWD 檢查」「開 dev server 驗收」「E2E 驗收」時使用。比對與驗收的結果寫進檔案,只回傳壓縮過的落差表。
4
4
  ---
5
5
 
6
6
  # Visual Parity
@@ -17,13 +17,15 @@ description: 比對兩個已渲染網頁的樣式落差(新舊版對照 / 改
17
17
  2. **不要把報告全文讀進 context。** stdout 的摘要已含「最常出現的落差屬性」;需要細節時用 `grep` 撈特定屬性或 selector,不要 `Read` 整份。
18
18
  3. **不要逐項修。** 先看 stdout 的 top differing properties——落差通常是少數幾條系統性規則(一條 `margin-bottom`、一組 token)造成的,不是幾十個獨立問題。
19
19
  4. **改完重跑一次全量比對**,而不是只驗剛改的那一項。
20
- 5. **先選輸出目錄。** 在 VibeFlow 中,驗收證據預設寫入 `VIBEFLOW_ARTIFACTS_DIR` 的子目錄,暫存工作檔寫入 `VIBEFLOW_SCRATCH_DIR`;不要留在 agent scratchpad,也不要 commit 截圖或影片。
20
+ 5. **不要直接看截圖。** 要知道「畫面上哪裡不一樣」就跑 `pixel`,讀它的表;只有表裡標成 `pixel-only` 又看不懂的區域,才開那一張 montage。不要 Read 整頁截圖:長頁送進來會被縮小,1–2px 的落差根本看不到。
21
+ 6. **先選輸出目錄。** 在 VibeFlow 中,驗收證據預設寫入 `VIBEFLOW_ARTIFACTS_DIR` 的子目錄,暫存工作檔寫入 `VIBEFLOW_SCRATCH_DIR`;不要留在 agent scratchpad,也不要 commit 截圖或影片。
21
22
 
22
23
  ## 前置
23
24
 
24
25
  - `<skill-dir>` 是本 SKILL.md 所在的目錄;script 在 `<skill-dir>/scripts/parity.py`。
25
26
  - 下文指令寫 `python`;macOS / Linux 沒有 `python` 時改用 `python3`。
26
27
  - 需要 `pip install playwright && playwright install chromium`(macOS / Linux 用 `pip3` 或 `python3 -m pip`);錄 `.mp4` 才需要 ffmpeg(選配)。
28
+ - `pixel` 另外需要 `pip install opencv-python-headless numpy`;沒裝時只有 `pixel` 會停下並提示,其他模式照常。
27
29
  - 受測頁面要先跑起來。server 已經在跑就直接呼叫 `parity.py`;自簽憑證已預設接受(`ignore_https_errors`)。
28
30
  - 頁面需要登入或一次性 token 時,`--ref` / `--local` 直接給帶 token 的完整 URL,或先用 `flow` 的步驟登入。
29
31
  打到登入頁或拒絕頁時比對結果毫無意義——`matched` 異常地少就先檢查這個。
@@ -79,7 +81,7 @@ stdout 會回報四個數字:
79
81
 
80
82
  其他參數:`--width` / `--widths`、`--wait-selector`(等內容渲染完)、`--extra-wait`(動畫或延遲載入)。
81
83
  Exit code 1 表示有落差,0 表示完全一致。
82
- 此模式比對 DOM/computed styles,不產生像素 diff 圖;需要像素級比對時另行執行,將兩張截圖及 diff 圖同樣存入 `PARITY_OUT`。
84
+ 此模式比對 DOM/computed styles,不產生像素 diff 圖;像素級比對用下面的 `pixel`。
83
85
 
84
86
  ### --map:手動指定對照
85
87
 
@@ -95,6 +97,35 @@ DOM 結構差太多時(舊 iframe vs 新 SSR),用 JSON 補:
95
97
 
96
98
  左邊是 ref 的 selector、右邊是 local 的。支援尾綴比對,不必寫完整路徑。
97
99
 
100
+ ## pixel — 畫面上真正看得到的落差
101
+
102
+ ```bash
103
+ TMPDIR="$PARITY_SCRATCH" python "<skill-dir>/scripts/parity.py" pixel \
104
+ --ref "<ref-URL>" --local "<local-URL>" \
105
+ --mask "#clock" --mask ".ad-slot" \
106
+ --out "$PARITY_OUT/parity-pixel.md"
107
+ ```
108
+
109
+ 用 OpenCV 比對兩邊的整頁截圖,並和 computed style 交叉比對:
110
+
111
+ - **對齊靠元素配對**(與 `diff` 相同,`--map` 同樣有效)。新頁上方多一塊 banner、某一欄多一張卡片時,只有那一塊會被回報,下面和旁邊的內容不會跟著全部變成落差。各欄位各自對齊。
112
+ - **每個落差區域都有類型**:`shift`(附位移量,例如 `local moved +12,+0px`)、`color`(附前後顏色,例如 `#333333→#cc0000`)、`content`、`added`、`removed`。
113
+ - **交叉比對的結果**(這是準確度的來源):
114
+ - 區域旁列出「那裡有哪些 computed style 不同」→ 找到原因了,照那個屬性修。
115
+ - `pixel-only` → 畫面不同,但比對的 style 都相同:通常是圖片、SVG、pseudo-element、字型沒載入。只有這類才需要開 montage。
116
+ - 「style diffs with no visible pixel change」→ style 不同但畫面一樣,通常可以忽略,不要去修。
117
+ - `layout drift` 列出每個新位移量第一次出現在哪個元素;整片往下移時,原因就在那附近。
118
+ - `added locally` 是本地新增、舊頁沒有的內容。
119
+
120
+ 輸出:報告寫進 `--out`;`<out>-ref.png`、`<out>-local.png` 是兩邊的截圖,`<out>-montage.png` 是前 `--top` 個區域的「舊|新|差異」拼圖。stdout 只印摘要與前 10 個區域。
121
+ Exit code 1 表示有看得到的落差。
122
+
123
+ 參數:`--mask <selector>`(可重複,把時鐘、廣告、頭像這類每次會變的元素遮掉)、`--threshold`(色差門檻,CIE76 ΔE,預設 3,約為人眼剛好分辨得出的差異)、`--min-area`(變動像素少於這個數的區域忽略,預設 6)、`--width`、`--max-height`(截圖高度上限,預設 10000)。
124
+
125
+ - 兩邊必須在**同一台機器、同一個瀏覽器**截圖;字型渲染因作業系統而異,跨機器比對會到處是雜訊。
126
+ - 截圖前已自動關閉動畫與 transition、隱藏游標,並等字型載入完成。
127
+ - 出現 `rows map below the end of the local page` 警告時,代表內容被推出固定高度的版面(或超過 `--max-height`),那些列沒有比對到。
128
+
98
129
  ## responsive — 三種寬度 + 水平溢出
99
130
 
100
131
  ```bash
@@ -161,4 +192,4 @@ TMPDIR="$PARITY_SCRATCH" python "<skill-dir>/scripts/parity.py" flow \
161
192
 
162
193
  ## 維護
163
194
 
164
- 改過 `parity.py` 後跑 `TMPDIR="$PARITY_SCRATCH" python "<skill-dir>/scripts/parity.py" selftest`,會用內建 fixture 驗證配對與 diff 邏輯仍正確。
195
+ 改過 `parity.py` 後跑 `TMPDIR="$PARITY_SCRATCH" python "<skill-dir>/scripts/parity.py" selftest`,會用內建 fixture 驗證配對與 diff 邏輯仍正確;有安裝 OpenCV 時也驗證 `pixel` 的對齊、分類與遮罩,沒裝則略過並註明。