forked from longtao.wu/droboticflow
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package api
|
|
|
|
import (
|
|
"math"
|
|
"sort"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// structureChangeSummary computes the submit-time slice-structure diff
|
|
// between the rework return baseline and the submitted workspace (Q7:b).
|
|
// It reads only the two server-owned structures — never the client's
|
|
// operation event log — so a forged or lost local history cannot skew the
|
|
// review summary. Interpretation, all net-of-undo (ADR 0006):
|
|
//
|
|
// - Merged: baseline interior boundaries that no longer exist (a k-way
|
|
// merge removes k-1 of them).
|
|
// - Split: submitted interior boundaries that did not exist before.
|
|
// - BoundaryMoves: aligned boundary pairs whose position moved.
|
|
// - Discarded: submitted discarded slices that overlap at least one
|
|
// baseline slice which was still kept — a slice already discarded in
|
|
// the baseline is not counted again.
|
|
//
|
|
// Boundary alignment is order-preserving with maximum cardinality, using
|
|
// minimum total displacement to break ties (e.g. {4,7} → {6} reads as one
|
|
// moved boundary plus one merge, not a merge plus a split).
|
|
func structureChangeSummary(baseline, submitted []domain.SliceSegment) domain.StructureChangeSummary {
|
|
summary := domain.StructureChangeSummary{}
|
|
from := interiorSliceBoundaries(baseline)
|
|
to := interiorSliceBoundaries(submitted)
|
|
pairs := alignBoundaries(from, to)
|
|
summary.Merged = len(from) - len(pairs)
|
|
summary.Split = len(to) - len(pairs)
|
|
for _, pair := range pairs {
|
|
if math.Abs(from[pair[0]]-to[pair[1]]) > structureBoundaryEpsilon {
|
|
summary.BoundaryMoves++
|
|
}
|
|
}
|
|
for _, candidate := range submitted {
|
|
if !candidate.IsDiscarded() {
|
|
continue
|
|
}
|
|
for _, old := range baseline {
|
|
if old.Start < candidate.End && old.End > candidate.Start && !old.IsDiscarded() {
|
|
summary.Discarded++
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return summary
|
|
}
|
|
|
|
const structureBoundaryEpsilon = 1e-6
|
|
|
|
// interiorSliceBoundaries returns the sorted, de-duplicated cut positions
|
|
// strictly inside the covered range. The outer start/end are fixed by full
|
|
// coverage validation and carry no merge/split/move signal.
|
|
func interiorSliceBoundaries(segments []domain.SliceSegment) []float64 {
|
|
var values []float64
|
|
for _, segment := range segments {
|
|
values = append(values, segment.Start, segment.End)
|
|
}
|
|
if len(values) == 0 {
|
|
return nil
|
|
}
|
|
sort.Float64s(values)
|
|
var deduped []float64
|
|
for _, value := range values {
|
|
if len(deduped) > 0 && value <= deduped[len(deduped)-1]+structureBoundaryEpsilon {
|
|
continue
|
|
}
|
|
deduped = append(deduped, value)
|
|
}
|
|
if len(deduped) <= 2 {
|
|
return nil
|
|
}
|
|
return deduped[1 : len(deduped)-1]
|
|
}
|
|
|
|
// alignBoundaries pairs baseline and submitted boundaries in order. The
|
|
// pairing always keeps min(len(from), len(to)) pairs: skipping is only
|
|
// allowed on the longer side, so every unpaired boundary is unambiguous
|
|
// merge/split evidence rather than an alignment artifact.
|
|
func alignBoundaries(from, to []float64) [][2]int {
|
|
n, m := len(from), len(to)
|
|
if n == 0 || m == 0 {
|
|
return nil
|
|
}
|
|
type cell struct {
|
|
cost float64
|
|
move int // 0 pair, 1 skip from, 2 skip to
|
|
}
|
|
table := make([][]cell, n+1)
|
|
for i := range table {
|
|
table[i] = make([]cell, m+1)
|
|
}
|
|
for i := n - 1; i >= 0; i-- {
|
|
for j := m - 1; j >= 0; j-- {
|
|
pair := table[i+1][j+1].cost + math.Abs(from[i]-to[j])
|
|
best := cell{cost: pair, move: 0}
|
|
if n-i > m-j {
|
|
if skip := table[i+1][j].cost; skip < best.cost {
|
|
best = cell{cost: skip, move: 1}
|
|
}
|
|
}
|
|
if m-j > n-i {
|
|
if skip := table[i][j+1].cost; skip < best.cost {
|
|
best = cell{cost: skip, move: 2}
|
|
}
|
|
}
|
|
table[i][j] = best
|
|
}
|
|
}
|
|
var pairs [][2]int
|
|
for i, j := 0, 0; i < n && j < m; {
|
|
switch table[i][j].move {
|
|
case 0:
|
|
pairs = append(pairs, [2]int{i, j})
|
|
i++
|
|
j++
|
|
case 1:
|
|
i++
|
|
default:
|
|
j++
|
|
}
|
|
}
|
|
return pairs
|
|
}
|