Files
ARTEX/third_party/norma/noa/tier.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

85 lines
2.7 KiB
Go

package noa
// TierDecision is the outcome of classifying one resolved range.
type TierDecision struct {
// TargetTier is the layer being compressed: the LOWEST tier present in the
// range. It decides which blocks are absorbed.
TargetTier Tier
// OutputTier is the new block's tier, one above the target and capped at
// MaxTier.
OutputTier Tier
// ConsumedBlockIDs are the blocks this compression absorbs — only those at
// TargetTier.
ConsumedBlockIDs []string
// SurvivingBlockIDs are blocks inside the range that are NOT absorbed
// because they sit above TargetTier. They stay active, and the panel reports
// them so the model understands why they are still there.
SurvivingBlockIDs []string
}
// DecideTier classifies a resolved range.
//
// ONE TIER AT A TIME. A compression absorbs only the blocks at the lowest tier
// present in the range; anything higher stays active. Re-summarising an
// already-distilled tier-2 summary alongside raw tier-1 summaries would put the
// same content through three lossy passes, and detail disappears fast.
//
// The rule is also what makes tier 3 reachable at all: if a tier-2 block could
// be absorbed by a same-tier sibling, TargetTier would be pinned at 1 forever
// and OutputTier would never exceed 2.
func DecideTier(r ResolvedRange, state CompressionState, cfg Config) TierDecision {
maxTier := cfg.Tiers.MaxTier
if maxTier < 2 {
maxTier = 3
}
d := TierDecision{TargetTier: 1, OutputTier: 1}
if r.Kind != BoundaryBlock {
// A pure message range always produces a tier-1 block.
return d
}
lowest := Tier(0)
for _, id := range r.NestedBlockIDs {
b := FindBlock(&state, id)
if b == nil || !b.Active {
continue
}
if lowest == 0 || b.Tier < lowest {
lowest = b.Tier
}
}
if lowest > 0 {
d.TargetTier = lowest
}
d.OutputTier = min(maxTier, d.TargetTier+1)
for _, id := range r.NestedBlockIDs {
b := FindBlock(&state, id)
if b == nil || !b.Active {
continue
}
if b.Tier == d.TargetTier {
d.ConsumedBlockIDs = append(d.ConsumedBlockIDs, id)
} else {
d.SurvivingBlockIDs = append(d.SurvivingBlockIDs, id)
}
}
return d
}
// IsTerminalRewrite reports a compression that would absorb terminal-tier blocks
// into another terminal-tier block without folding in any new message.
//
// That reclaims nothing — the same summaries go in and come out one pass lossier
// — and a model that tries it once will try it again. Checking TargetTier alone
// is sufficient: by DecideTier, TargetTier == MaxTier implies OutputTier ==
// MaxTier.
func IsTerminalRewrite(d TierDecision, directCount int, cfg Config) bool {
maxTier := cfg.Tiers.MaxTier
if maxTier < 2 {
maxTier = 3
}
return d.TargetTier == maxTier && directCount == 0
}