a876c3c780
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
43 lines
1.9 KiB
Go
43 lines
1.9 KiB
Go
package service
|
|
|
|
import "github.com/kandev/kandev/internal/task/models"
|
|
|
|
const (
|
|
// planTruncationMinPriorChars is the floor below which a shrinking write
|
|
// isn't worth flagging. A 300-char stub plan losing half its content is
|
|
// not a review-round-costing event; both real WO-38 incidents were on
|
|
// 40k+ char plans.
|
|
planTruncationMinPriorChars = 2000
|
|
|
|
// planTruncationMaxRetainRatio: this workflow's normal write is an
|
|
// append ("appends its own section"), so a plan's content grows
|
|
// monotonically across a build. A write that keeps less than half of the
|
|
// prior document is anomalous by construction, not a normal edit. Both
|
|
// real WO-38 incidents retained 22.8% and 23.9% of the prior plan — this
|
|
// line catches both with wide margin.
|
|
planTruncationMaxRetainRatio = 0.5
|
|
)
|
|
|
|
// planTruncationDetected reports whether newContent looks like an accidental
|
|
// destructive truncation of priorContent, rather than a deliberate edit or a
|
|
// legitimate (smaller) prune.
|
|
//
|
|
// Measured in runes, not bytes: len() on a Go string counts UTF-8 bytes, so a
|
|
// script change (e.g. an ASCII plan rewritten in CJK) can retain a small
|
|
// fraction of the document's characters while retaining most of its bytes —
|
|
// silently defeating this guard on exactly the kind of loss it exists to
|
|
// catch. Kandev ships zh-cn/zh-hk/zh-tw/pt-pt locales, so non-ASCII plan
|
|
// content is not hypothetical.
|
|
//
|
|
// This detector runs inside PlanService's per-task write lock (see
|
|
// docs/specs/tasks/system-design/plan-write-consistency.md), evaluated
|
|
// against the content the write's own read observed, so a concurrent writer
|
|
// cannot change what "prior" means out from under this decision.
|
|
func planTruncationDetected(priorContent, newContent string) bool {
|
|
priorLen := models.PlanContentLength(priorContent)
|
|
if priorLen < planTruncationMinPriorChars {
|
|
return false
|
|
}
|
|
return float64(models.PlanContentLength(newContent)) < float64(priorLen)*planTruncationMaxRetainRatio
|
|
}
|