From 305d609d0d97e3aaeb547f76f902491e69413edf Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 16 Aug 2026 17:10:28 +0800 Subject: [PATCH] =?UTF-8?q?chore(quality):=20=E5=90=AF=E5=8A=A8=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=B4=A8=E9=87=8F=20autoresearch=20=E4=BC=9A=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - .auto/ 会话文件(prompt/measure/checks/ideas) - pnpm-workspace.yaml 认可 @parcel/watcher 与 @swc/core 构建脚本, 修复 pnpm 11 下 make code-check 无法运行的问题 --- .auto/checks.sh | 15 +++++ .auto/ideas.md | 10 ++++ .auto/measure.sh | 42 +++++++++++++ .auto/prompt.md | 110 +++++++++++++++++++++++++++++++++++ frontend/pnpm-workspace.yaml | 2 + 5 files changed, 179 insertions(+) create mode 100755 .auto/checks.sh create mode 100644 .auto/ideas.md create mode 100755 .auto/measure.sh create mode 100644 .auto/prompt.md diff --git a/.auto/checks.sh b/.auto/checks.sh new file mode 100755 index 00000000..eb59aed4 --- /dev/null +++ b/.auto/checks.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# Correctness gate: must pass after every edit. Fails fast on real breakage. +set -euo pipefail +cd "$(dirname "$0")/.." + +echo "==> go vet ./..." +go vet ./... 2>&1 | tail -20 + +echo "==> go build ./..." +go build ./... 2>&1 | tail -20 + +echo "==> golangci-lint run (repo config)" +golangci-lint run 2>&1 | tail -20 + +echo "OK: checks passed" \ No newline at end of file diff --git a/.auto/ideas.md b/.auto/ideas.md new file mode 100644 index 00000000..64a70560 --- /dev/null +++ b/.auto/ideas.md @@ -0,0 +1,10 @@ +# Ideas backlog (代码质量) + +- 后端 204 个 *_test.go 被 repo golangci 配置 `tests: false` 跳过;可以用 + testifylint/usetesting/paralleltest 等对测试代码做一次质量扫描(当前基准不含测试,属于后续深化方向)。 +- biome check 发现 76 处(基本全是格式漂移);repo 只跑 format 不跑 check。 + 可作为独立的一次性格式化提交处理,不进基准(避免纯格式噪声污染 metric)。 +- repo `make code-check` 还有 `make format`(goimports + biome)契约:迭代中保持已改文件格式化干净。 +- 观察 go vet 已启用但 `unusedresult`/`copylocks` 等子检查默认关闭;若需要可考虑仅作发现用,不进基准。 +- errorlint 修复中有 4 处 `err != context.Canceled`,若 runner 实际从不 wrap, + 用 errors.Is 是更稳妥的最佳实践,且语义不变 —— 已验证三处 cmd 入口同构。 \ No newline at end of file diff --git a/.auto/measure.sh b/.auto/measure.sh new file mode 100755 index 00000000..ccb06114 --- /dev/null +++ b/.auto/measure.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# Benchmark: total code-quality issues across backend + frontend (lower is better). +# Fixed linter set — see .auto/prompt.md. Never tune this file to game counts. +set -euo pipefail +cd "$(dirname "$0")/.." +start=$(date +%s) + +# ---------- Backend: golangci-lint, repo config + fixed best-practice extras ---------- +EXTRA_LINTERS="errorlint,errname,nilnil,forcetypeassert,copyloopvar,intrange,mirror,perfsprint,prealloc,usestdlibvars,modernize,sloglint,canonicalheader,nosprintfhostport,recvcheck,wastedassign" +golang_out=$(golangci-lint run --enable="$EXTRA_LINTERS" 2>&1 || true) + +golang_total=0 +while IFS= read -r line; do + if [[ "$line" =~ ^\*\ ([a-zA-Z0-9_]+):\ ([0-9]+)$ ]]; then + name="${BASH_REMATCH[1]}" + n="${BASH_REMATCH[2]}" + golang_total=$((golang_total + n)) + echo "METRIC golint_${name}=$n" + fi +done <<< "$golang_out" +echo "METRIC golint_total=$golang_total" + +# ---------- Frontend: eslint (repo gate) ---------- +cd frontend +eslint_out=$(pnpm exec eslint . --max-warnings 0 2>&1 || true) +eslint_problems=0; eslint_errors=0; eslint_warnings=0 +if [[ "$eslint_out" =~ ([0-9]+)\ problems? ]]; then eslint_problems="${BASH_REMATCH[1]}"; fi +if [[ "$eslint_out" =~ \(([0-9]+)\ errors?, ]]; then eslint_errors="${BASH_REMATCH[1]}"; fi +if [[ "$eslint_out" =~ ,\ ([0-9]+)\ warnings? ]]; then eslint_warnings="${BASH_REMATCH[1]}"; fi +echo "METRIC eslint_problems=$eslint_problems" +echo "METRIC eslint_errors=$eslint_errors" +echo "METRIC eslint_warnings=$eslint_warnings" + +# ---------- Frontend: tsc (repo gate) ---------- +tsc_out=$(pnpm exec tsc --noEmit --jsx preserve 2>&1 || true) +tsc_errors=$(grep -cE "error TS" <<< "$tsc_out" || true) +echo "METRIC tsc_errors=$tsc_errors" + +end=$(date +%s) +total=$((golang_total + eslint_problems + tsc_errors)) +echo "METRIC total_issues=$total" +echo "METRIC measure_s=$((end - start))" \ No newline at end of file diff --git a/.auto/prompt.md b/.auto/prompt.md new file mode 100644 index 00000000..df29a735 --- /dev/null +++ b/.auto/prompt.md @@ -0,0 +1,110 @@ +# Autoresearch: 前后端代码质量符合最佳代码实践 + +## Objective + +Improve backend (Go) and frontend (Next.js/TS) code quality so the codebase +conforms to best practices. NOT a performance task. Each experiment is a code +change that removes real, lint-diagnosed code-quality issues (dead assignments, +error-wrapping bugs, non-idiomatic loops, mixed receivers, unsafe error +comparisons, unnecessary string fmt, etc.) without changing behavior. + +Genuine quality work only: fix code, never weaken the checks. Do NOT edit +`.golangci.yml`, eslint/biome config, or add `nolint`/`eslint-disable` +comments to reduce counts. Do NOT reformat code that isn't part of a fix +(no formatted-only churn). + +## Metrics + +- **Primary**: `total_issues` (unitless, lower is better) = backend golangci + issues (extended linter set below) + frontend eslint problems + tsc errors. +- **Secondary**: per-linter counts (`golint_modernize`, `golint_perfsprint`, + `golint_errorlint`, `golint_gosec`, `golint_canonicalheader`, + `golint_recvcheck`, `golint_wastedassign`, `golint_usestdlibvars`, + `golint_intrange`, `golint_forcetypeassert`, `golint_nilnil`, + `golint_prealloc`, `golint_errname`, `golint_sloglint`, + `golint_copyloopvar`, `golint_mirror`, `golint_nosprintfhostport`), + `eslint_problems`, `eslint_errors`, `eslint_warnings`, `tsc_errors`, + `measure_s` (benchmark wall time). + +## How to Run + +`./.auto/measure.sh` — outputs `METRIC name=value` lines. Parsed by +run_experiment automatically. + +Correctness gate: `./.auto/checks.sh` runs `go vet ./...`, `go build ./...`, +and the repo's own `golangci-lint run` (repo config, tests excluded) — all +must pass. Note: `go test ./...` is NOT in checks.sh — several tests fail on +main today for environmental reasons (no local redis; flaky frpc process +tests). Don't "fix" those unless cheap and clearly unrelated to redis/flaky. + +## Benchmark Definition (fixed — never change mid-session) + +Backend: `golangci-lint run --enable=errorlint,errname,nilnil,forcetypeassert, +copyloopvar,intrange,mirror,perfsprint,prealloc,usestdlibvars,modernize, +sloglint,canonicalheader,nosprintfhostport,recvcheck,wastedassign` +(repo `.golangci.yml` linters stay active too; `tests: false` as configured). + +Frontend: `pnpm exec eslint . --max-warnings 0` (repo gate) + +`pnpm exec tsc --noEmit --jsx preserve` (repo gate). + +Do not add/remove linters or change settings to make the number go down. + +## Files in Scope + +Backend (Go): `cmd/`, `internal/`, `pkg/`. Anything lint-flagged in the +extended set above. Note: module name in go.mod is `github.com/Rain-kl/Wavelet`. + +Frontend (TS/React): `frontend/app/`, `frontend/components/`, `frontend/lib/`, +`frontend/contexts/`, `frontend/hooks/`, `frontend/types/`, frontend scripts. + +Infra: `frontend/pnpm-workspace.yaml` — approved @parcel/watcher + @swc/core +builds (fixes `make code-check` under pnpm 11; ERR_PNPM_IGNORED_BUILDS +otherwise). Already committed in setup. + +## Off Limits + +- `.golangci.yml`, `eslint.config.mjs`, `biome.json` — never touch to reduce counts. +- No `//nolint` / `eslint-disable` comments to silence checks. +- No reformat-only commits (biome/gofmt churn without a fix). +- No behavior changes: refactors must compile (checks.sh gate) and keep tests + semantics identical. Re-run checks.sh after every edit. +- `frontend/node_modules`, `frontend/bun.lock` (untracked, not ours). +- Do not run `go test` suites that need redis/network to declare success. + +## Constraints + +- Backend conventions (AGENTS.md): apps → repository → model layering; + `pkg/util/` must not import Gin/GORM/sessions; no `db.DB` in model; + response.Abort* for API errors; Chinese docs for content changes + (code-quality fixes are not content changes — no doc sync needed unless + behavior/UX changes; changelog only for user-visible changes, typically + none here). +- Frontend: run `pnpm exec biome format --write` only on files you edit + (repo `make format` uses biome); keep component placement rules. +- `golangci-lint --fix` is allowed and preferred for safe fixes + (modernize/intrange/perfsprint/usestdlibvars/canonicalheader/mirror/ + copyloopvar/sloglint/errname) — review the resulting diff before keeping. + For no-fix linters (errorlint wrapping, wastedassign, recvcheck, nilnil, + prealloc, forcetypeassert) edit by hand. + +## Workflow per iteration + +1. Read current measure output: which categories remain, where. +2. Pick ONE category (or a coherent set of similar fixes), locate files, fix + by hand or with golangci-lint --fix scoped to that category. +3. `./.auto/measure.sh` → if total dropped → `./.auto/checks.sh` → log keep. + If flat/worse → discard or adjust. + +## What's Been Tried + +- Setup commit `ee6974d` (autoresearch/code-quality-2026-08-16): branch, + .auto/ session files, frontend/pnpm-workspace.yaml build approvals. +- Baseline (before any code fix): total_issues = 108 + (golangci 107 = modernize 37, perfsprint 18, errorlint 12, canonicalheader 8, + recvcheck 7, wastedassign 7, usestdlibvars 3, intrange 3, forcetypeassert 3, + nilnil 3, prealloc 3, errname 1, gosec 2; eslint 1 warning + [react-hooks/exhaustive-deps in + app/(main)/pages/detail/components/pages-source-card.tsx:275]; tsc 0). +- Environment notes: golangci-lint 2.12.2 warm cache ~3s; eslint cold ~27s + (ignore stderr pnpm noise); go vet+go build ~15-30s after edits. Some + go test failures on main are pre-existing (redis down, flaky frpc) — not ours. \ No newline at end of file diff --git a/frontend/pnpm-workspace.yaml b/frontend/pnpm-workspace.yaml index 07399aa0..42ea1c72 100644 --- a/frontend/pnpm-workspace.yaml +++ b/frontend/pnpm-workspace.yaml @@ -1,4 +1,6 @@ allowBuilds: + '@parcel/watcher': true + '@swc/core': true core-js: true sharp: true unrs-resolver: true