Help us shape the future of Go! Take the 2026 Go Developer Survey

reportutil

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

采样聚簇:best-of-N 确定性聚合原语。

同一任务对同一输入跑 N 次(best-of-N),「多份采样相互复现」是确定性的 可信度信号——按调用方提供的签名通道与等价判定把 N 份输出聚簇计数, 不引入任何模型判断(与「裁决不走 LLM」的确定性纪律兼容)。

典型用法:审查/生成类 agent 对高危输入 opt-in N 采样,聚簇后 Count≥2 的 簇升级呈现权重、孤立单现的低权重条目标注降权提示;全部簇保留(漏报防线)。 Fingerprint/NormalizeComment 提供「file+规范化文本」精确指纹通道的 canonical 实现(v0.10.9 自 severity 迁入——指纹是聚簇签名的自然组成部分)。

模式级学习环的确定性数学与键派生(收编自 argus internal/learning,v0.10.32): BetaConfidence 与 WilsonCI/McNemarExact 同族——评审反馈回路的小样本统计原语; PatternKey/NormalizePatternText 是「同一类问题聚成同一模式」的键语义单源。 纯函数、零依赖、可离线复算;禁止 LLM 摘要参与键派生(不可对账)。

Package reportutil 评审/评测报告后处理原语(沉淀自 heimdallr 报告层与 bianque 评审流水线),一个包覆盖同一条消费链的三段:

  • 严重度归一(severity.go):high/medium/low 词表 + 外部审查专家常用 别名(P0/fatal/nit…)折叠——「严重级别是什么、多严重」;
  • 采样聚簇(cluster.go):best-of-N 输出按签名通道聚簇计数(含 file+规范化文本精确指纹),「哪些发现被独立复现」;
  • 对比统计(stats.go):小样本通过率 Wilson 区间 + 配对 McNemar 精确 检验——「两个版本的差异是否显著」。

三段原为 severity/sampling/stats 三包,消费者画像相同且边界已漂移过一轮 (v0.10.9 severity→sampling 指纹迁移),v0.10.11 合并为单包。零外部依赖。

对比统计:小样本通过率的诚实区间估计与配对二分类差异检验。 显著水平对应的 z 由调用方传入(如 1.96)。实现已下沉 oekit/pkg/mathx (ekit v0.33.0 首下沉、agentkit v0.10.41 起单源在彼;ekit 单体冻结后随 oekit 拆分仓继续单源);本文件保留公开面薄委托。

Index

Constants

View Source
const (
	High   = "high"
	Medium = "medium"
	Low    = "low"
)

严重级别词表。

View Source
const PatternTextMax = 160

PatternTextMax 模式键/人读样本的文本截断上限(rune)。

Variables

This section is empty.

Functions

func BetaConfidence

func BetaConfidence(posted, regret, suppress, restore int) float64

BetaConfidence 由 posted/regret/suppress/restore 计数推 Beta 后验置信度。 先验 (α=2, β=2) → 0.5;Posted α+1;Regret β+2;Suppress β+3; Restore 只回抬 β(max(β-2,1)),不加 α——此前 Restore α+2 会让 suppress+restore 成对循环净抬置信度(α+2, β+1),API 持有者可循环洗白; 现在 restore 仅撤销部分负证据,成对循环净 β+1(置信度微降),无法洗白。 samples=Posted+Regret+Suppress(PatternSamples),读时计算不落列。

func Fingerprint

func Fingerprint(file, comment string) string

Fingerprint 精确指纹:规范化评论文本哈希 + file(SHA256 前 16 字节 hex)。 行号不入指纹;同一指纹跨轮次即「同一问题」。常用作 Aggregate 的签名通道。

func Higher

func Higher(a, b string) string

Higher 取更严重的一方。

func McNemarExact

func McNemarExact(n01, n10 int) float64

McNemarExact McNemar 精确检验(双侧):n01=甲败乙过数,n10=甲过乙败数。

func Normalize

func Normalize(s string) (string, bool)

Normalize 把任意写法归一到词表。 第二个返回值=false 表示原值越界(已归为 low,调用方应记录告警)。

func NormalizeComment

func NormalizeComment(s string) string

NormalizeComment 规范化:全小写、去所有空白与标点/符号差异。

func NormalizePatternText

func NormalizePatternText(comment string) string

NormalizePatternText 规范化评论文本用于模式键: 小写 → 剥路径/十六进制/反引号标识符/纯数字 → 折叠空白 → 截断。 反引号/路径/十六进制/数字先剥,避免 "错误处理不佳 in `handler.go:12`" 与 "错误处理不佳 in `service.go:99`" 落成不同模式。

func PatternKey

func PatternKey(pluginName, comment string) string

PatternKey 派生仓库级模式键:sha256(plugin \x00 normalize(comment)) 前 16 hex。 plugin 为空时用 "unknown"——缺插件归属的键仍可计算,但不应静默混入其他插件历史。 键不含 file/line——同一类问题在不同文件聚成同一模式;含 plugin 分量—— 不同审查来源的信任历史不混用。

func PatternSample

func PatternSample(comment string) string

PatternSample 返回入库的人读样本(规范化后截断),便于审计与提示词注入展示。

func PatternSamples

func PatternSamples(posted, regret, suppress int) int

PatternSamples 参与消费判定的样本量(小样本不消费,防抖动)。

func Rank

func Rank(s string) int

Rank 越大越严重。

func Valid

func Valid(s string) bool

Valid 是否属于钉死词表。

func WilsonCI

func WilsonCI(passed, total int, z float64) (lo, hi float64)

WilsonCI 通过率的 Wilson 得分置信区间(小样本下比正态近似诚实)。

Types

type Group

type Group[T any] struct {
	// Representative 首见条目(稳定性依赖输入有序——按采样序传入)。
	Representative T
	// Items 命中本簇的全部条目(含 Representative,按首见序)。
	Items []T
	// Signature 首见条目的签名通道集(可观测/对账用;归属判定以 eq 为准)。
	Signature []string
	// Count = len(Items)(冗余字段,便于不取 Items 的调用方)。
	Count int
}

Group 一簇相互等价的采样结果。

func Aggregate

func Aggregate[T any](items []T, signature func(T) []string, eq func(a, b T) bool) []Group[T]

Aggregate 把采样输出聚簇。

  • signature:条目的签名通道集(每通道作 O(1) 快速匹配的 map 键,如 「精确指纹」「file+规范化文本」双通道)。通道只是快速定位,最终归属 以 eq 为准:任一通道命中既有簇且 eq 判定等价 → 并入;
  • eq(a, b):a(既有簇代表)与 b 是否为同一问题。实现应只对比代表: 本包语义是「与首见者等价才并入」,链式传递匹配不成立(保守,防止 漂移链把不同问题串成一簇)。签名命中但 eq 不等价时继续尝试其余 通道,全部未命中则自成新簇。

输入按采样序传入;输出簇按首见序排列。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL