Skip to content

fix(civitai): snippet cuts a server body on a rune boundary, not a byte index - #972

Merged
ZacxDev merged 5 commits into
mainfrom
fix/snippet-rune-boundary
Nov 23, 2026
Merged

ZacxDev merged 5 commits into
mainfrom
fix/snippet-rune-boundary

Conversation

@ZacxDev

@ZacxDev ZacxDev commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #627

What

pkg/civitai's snippet bounded a server body at 500 bytes with s[:max]. A body whose 500th byte fell inside a multi-byte rune came back as invalid UTF-8 inside the SDK's error string. The cut now backs up to the nearest rune start (utf8.RuneStart), so it keeps at most 500 bytes and at least 497.

Tests

pkg/civitai/snippet_rune_boundary_test.go, committed first on its own so CI shows it red:

  • TestSnippetCutsOnARuneBoundary: the issue's CJK fixture at four alignments, plus 4-byte emoji (regression). The v2 alignment happens to land on a boundary and is valid before the fix too.
  • TestSnippetLeavesABodyAtTheBudgetAlone: invariant guard (exactly 500 bytes; renamed after round-0 D1)
  • TestSnippetBoundsTheBackUpOnInvalidBytes: added after round 1

🤖 Generated with Claude Code

ZacxDev and others added 2 commits November 23, 2026 16:52
Committed before the fix so CI shows it red.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…te index

The 500-byte bound now backs up to utf8.RuneStart, so the SDK's error
text is always valid UTF-8 and keeps 497-500 bytes. The short-body
invariant guard moves to exactly 500 bytes (round-0 audit D1).

Fixes #627

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ZacxDev

ZacxDev commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Audit round 0 (requirements & deletion) at 8c88a87cea (test-only commit): proceed to the checklist. 4 requirements examined, traced to #627; the 500-byte bound is kept over a rune bound because existing redaction tests depend on a byte budget.

D1 applied in the fix commit (the guard now sits exactly on the 500-byte boundary). D2 kept: internal/cmd/feedback.go's trimSplitRune has different semantics (it trims only an incomplete trailing rune at a read cap).

D1 — TestSnippetLeavesAShortBodyAlone uses a 498-byte body, off the 500 boundary, so it cannot catch an off-by-one in len(s) > max; move it to exactly 500 bytes
D2 — a second rune-boundary helper beside internal/cmd/feedback.go trimSplitRune instead of one shared helper; recommend keep separate (different semantics)

ZacxDev and others added 2 commits November 23, 2026 17:05
Round-1 audit of #972: a body of bare continuation bytes (invalid UTF-8,
passed through by saferune.Strip) backed the cut to nothing. The loop now
stops within utf8.UTFMax of the budget, and the comment scopes the
497-500 claim to valid UTF-8.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ZacxDev

ZacxDev commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Audit round 1 (nine axes) at 4eb77cd50d: no 🔴 and no 🟡, so the ladder ends. One 🟢 was fixed in the final commit (not re-audited). The rune back-up loop had no lower bound, so a server body made only of continuation bytes (invalid UTF-8, which saferune.Strip passes through) backed the cut to nothing, keeping 0 bytes for 600 × 0x80. It now stops within utf8.UTFMax of the budget, with a test. The code comment now limits the 497–500 byte claim to valid UTF-8. PR body corrected: the invariant guard is TestSnippetLeavesABodyAtTheBudgetAlone.

1. snippet's rune back-up is bounded to utf8.UTFMax bytes; comment scoped to valid UTF-8

@ZacxDev
ZacxDev marked this pull request as ready for review November 23, 2026 23:18
@ZacxDev
ZacxDev merged commit 31b9843 into main Nov 23, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkg/civitai: snippet truncates on a BYTE index, so a multi-byte body is cut mid-rune and the SDK returns invalid UTF-8

1 participant