Skip to content

fix: cut captured bodies and skill file text on a rune boundary - #5347

Open
shunxinyi-ljs wants to merge 3 commits into
Tencent:mainfrom
shunxinyi-ljs:fix/rune-boundary-leftovers
Open

shunxinyi-ljs wants to merge 3 commits into
Tencent:mainfrom
shunxinyi-ljs:fix/rune-boundary-leftovers

Conversation

@shunxinyi-ljs

Copy link
Copy Markdown
Contributor

Description

Three text previews were still cut at a byte offset:

  • Access log: request_body / response_body are capped at 10 KiB in internal/middleware/logger.go. The request preview sliced bodyBytes[:maxBodySize], and the response writer sliced the chunk it kept.
  • Admin skill browser: projectSkillFileContent sliced text files at skillFileTextLimit (1 MiB).

Chinese text is three bytes per character, so each cut landed inside a character. The skill file content is returned to the settings drawer inside a JSON string and the access log goes through SanitizeForLog, so the half character reached users and logs as U+FFFD.

All three cuts now go through the existing common.TruncateUTF8Bytes, which keeps the byte budget and backs off to the previous rune start. The response writer also records whether it dropped bytes, because after a rune-safe cut len(body) == maxBodySize no longer means "truncated".

Type of Change

  • 🐛 Bug fix
  • 🧪 Test

Related Issue

None.

Testing

Branches are rebased onto cbdcebab; the two production files are unchanged since 26723dca.

  • go test ./internal/middleware ./internal/application/service -count=1 — pass (service package takes about 135 s).
  • Reverting only the two production files to main and rerunning the new tests fails:
    • TestReadRequestBodyKeepsMultiByteCharactersAtTheLimit: request body preview is not valid UTF-8 (len=10270)
    • TestLoggerKeepsMultiByteCharactersInResponseBody: access log contains a replacement character
    • TestProjectSkillFileContentKeepsMultiByteCharactersAtTheTextLimit: truncated content must stay valid UTF-8
  • gofmt -l and go vet on both packages: clean.

Checklist

  • git diff --check origin/main...HEAD passes
  • Changed source files are formatted
  • Targeted tests for the changed packages pass
  • Full-repository checks were run, or any unrelated/environment-dependent failures are documented above
    (full go build ./... needs a cgo vector-store build this machine cannot complete; package-scoped checks were used instead)
  • Self-reviewed the code
  • Added/updated tests covering the change
  • Updated related documentation (not applicable: no user-facing contract change beyond removing U+FFFD)
  • Breaking changes are clearly called out in the description above (none)

The access-log capture sliced request and response bodies at a byte offset,
and the admin skill browser sliced text files at 1 MiB. Chinese content is
three bytes per character, so the last character was cut in half and reached
clients and logs as U+FFFD. Route all three cuts through common.TruncateUTF8Bytes,
which keeps the same byte budget and backs off to the previous rune start.
@shunxinyi-ljs
shunxinyi-ljs marked this pull request as ready for review November 10, 2026 05:18

@lyingbug lyingbug left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Go Lint rerun passed. I reproduced two issues with this change. Please preserve the response log prefix across multiple writes and keep the skill preview allocation within its 1 MiB budget. The inline comments include the reproductions and suggested fixes.

Comment thread internal/middleware/logger.go Outdated
for end > 0 && !utf8.RuneStart(b[end]) {
end--
}
r.body.Write(b[:end])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the captured bytes contiguous across writes. Write 10,239 x bytes followed by 界 in one Write, then write Z in a second call. The first call drops 界 at the 10 KiB limit, but the second fills the remaining byte with Z. The logged body therefore skips a character. If the first write instead ends after the first byte of 界 and the second contains the rest and Z, the capture ends in invalid UTF-8. Both layouts send exactly the same response and should log just the 10,239 x bytes followed by the truncation marker.

Please capture up to maxBodySize+1 consecutive bytes across writes and apply TruncateUTF8Bytes once when formatting the log, as the request path does. Please add a regression test for both write layouts.

}
if len(body) > skillFileTextLimit {
out.Content = string(body[:skillFileTextLimit])
out.Content = common.TruncateUTF8Bytes(string(body), skillFileTextLimit)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bound the allocation to the preview size. string(body) copies the entire file before TruncateUTF8Bytes takes a substring. For a supported 32 MiB text file, the preview still returns only 1 MiB, but I measured 33,554,624 B/op with this change versus 1,048,656 B/op on main for the same input.

Please find the UTF-8 boundary at or before skillFileTextLimit in the original byte slice, then convert only body[:end] to a string. That keeps the existing multibyte test passing and the allocation proportional to the preview.

The response writer cut each Write call to a rune boundary, so bytes dropped
in one call could not be restored by the next: the same response logged
differently depending on how the handler split it, and a rune split between
calls left invalid UTF-8. Capture maxBodySize+1 contiguous bytes and truncate
once when the log line is built, matching the request path.

Also cut the skill file preview in the original byte slice. Converting the
whole file to a string first copied all 32 MiB of a large text file for a
1 MiB preview.

Tested two write layouts that send the same bytes: the preview now keeps the
same 10,239 bytes before the marker in both. The preview allocation on a
32 MiB text file stays around the 1 MiB cap.
@shunxinyi-ljs

Copy link
Copy Markdown
Contributor Author

Both points are fixed in c2f2276.

Contiguous response capture. loggerResponseBodyWriter.Write now appends up to maxBodySize+1 contiguous bytes regardless of call boundaries, and truncation happens once in the log-line builder through common.TruncateUTF8Bytes, matching the request path. The per-write truncated flag is gone.

TestLoggerCapturesResponseBodyAcrossWritesContiguously covers both layouts you described (whole rune in the first write, rune split across the two writes) and asserts the same preview — 10,239 bytes followed by the marker. On the previous head both subtests fail; with this commit both pass.

Preview allocation. The preview now finds the UTF-8 boundary at or before skillFileTextLimit in the original byte slice and converts only body[:end] to a string.

TestProjectSkillFileContentAllocationIsBoundedByThePreview feeds a 32 MiB text file: at 686bb69 it fails with 33,554,513 B/op; with this commit it stays around the 1 MiB cap. The existing multibyte test still passes.

Verification: go test ./internal/middleware -run 'TestLogger|TestReadRequestBody' and go test ./internal/application/service -run 'TestProjectSkillFileContent' pass; go vet and gofmt -l are clean.

@shunxinyi-ljs
shunxinyi-ljs force-pushed the fix/rune-boundary-leftovers branch from e1acb40 to ea19785 Compare November 23, 2026 11:12

This branch has not been deployed

No deployments
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.

2 participants