fix(spans): make fitSpanName rune-aware and normalize lookups

Follow-up to the knowledge span name column expansion: truncate span
names by rune count to match PostgreSQL VARCHAR(255), apply the same
normalization in LookupSpanByName, and add regression tests for mid-length
wiki slugs and CJK truncation boundaries.
This commit is contained in:
wizardchen
2026-07-13 10:37:37 +08:00
committed by lyingbug
parent 26f8afa3db
commit c8793b001d
2 changed files with 58 additions and 8 deletions
@@ -45,18 +45,25 @@ const maxSpanNameLen = 255
// fitSpanName ensures a span name fits the DB column. Wiki ingest builds
// names like postprocess.wiki.page[<slug>] which can exceed 64 chars when
// the slug is a long romanized entity name; when truncated an 8-hex hash
// suffix keeps concurrent subspans distinct.
// suffix keeps concurrent subspans distinct. Truncation is rune-aware to
// match PostgreSQL VARCHAR(255) character semantics and avoid splitting
// multi-byte UTF-8 sequences.
func fitSpanName(name string) string {
if len(name) <= maxSpanNameLen {
runes := []rune(name)
if len(runes) <= maxSpanNameLen {
return name
}
sum := sha256.Sum256([]byte(name))
suffix := fmt.Sprintf("~%x", sum[:4])
keep := maxSpanNameLen - len(suffix)
suffixRunes := []rune(suffix)
keep := maxSpanNameLen - len(suffixRunes)
if keep < 1 {
return suffix[:maxSpanNameLen]
if len(suffixRunes) > maxSpanNameLen {
return string(suffixRunes[:maxSpanNameLen])
}
return suffix
}
return name[:keep] + suffix
return string(runes[:keep]) + suffix
}
// Span is the in-memory handle the pipeline holds while a stage / subspan
@@ -581,6 +588,7 @@ func (t *spanTracker) LookupSpanByName(ctx context.Context, knowledgeID string,
if name == "" || knowledgeID == "" || attempt <= 0 {
return nil
}
name = fitSpanName(name)
rows, err := t.repo.ListByAttempt(ctx, knowledgeID, attempt)
if err != nil {
logger.Warnf(ctx, "[SpanTracker] LookupSpanByName list failed kid=%s attempt=%d: %v",