The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(traces): stop byte limits from splitting UTF-8 characters (#841)
Rudy Mizrahi Celeklid501900b6945
diff --git a/cli/beacon/internal/endpoint/dashboard/history_store.go b/cli/beacon/internal/endpoint/dashboard/history_store.goindex dc477b39..ce11d90c 100644--- a/cli/beacon/internal/endpoint/dashboard/history_store.go+++ b/cli/beacon/internal/endpoint/dashboard/history_store.go@@ -43,7 +43,7 @@ const (// historyDerivationVersion covers everything computed from a stored line: trace keys, event// numbers, types, summaries and search text. Bump it when that projection changes; the next// catch-up recomputes it from the stored lines, so history survives the change.- historyDerivationVersion = 13 // 2: trace events carry the event's policy block; 3: and its error; 4: usage carries the 1h cache-write split; 5: agent.response is an agent message; 6: the summary names its project; 7-11 were the sharing-staleness steps on a parallel branch; 12: sharing goes stale on activity after what a share covered (shared_through), ignoring a share's own events and events the share projection skips, and carries a refresh command; 13: redaction recognizes more credential shapes, so trace text and search text change+ historyDerivationVersion = 14 // 14: trace labels preserve UTF-8 at byte limits; 2: trace events carry the event's policy block; 3: and its error; 4: usage carries the 1h cache-write split; 5: agent.response is an agent message; 6: the summary names its project; 7-11 were the sharing-staleness steps on a parallel branch; 12: sharing goes stale on activity after what a share covered (shared_through), ignoring a share's own events and events the share projection skips, and carries a refresh command; 13: redaction recognizes more credential shapes, so trace text and search text changedefaultHistoryRetentionDays = 90// maxHistoryRetentionDays is 100 years: a retention this long or longer means nothing expires.diff --git a/cli/beacon/internal/endpoint/dashboard/traces.go b/cli/beacon/internal/endpoint/dashboard/traces.goindex d3b998fa..482706e6 100644--- a/cli/beacon/internal/endpoint/dashboard/traces.go+++ b/cli/beacon/internal/endpoint/dashboard/traces.go@@ -568,7 +568,7 @@ func traceSnippet(event TraceEventV1, query string) string {continue}if query == "" || strings.Contains(strings.ToLower(candidate), strings.ToLower(strings.Fields(query)[0])) {- return truncateString(candidate, 240)+ return asymptoteobserve.TruncateUTF8(strings.TrimSpace(candidate), 240)}}return ""@@ -649,14 +649,6 @@ func firstNonEmpty(values ...string) string {return ""}-func truncateString(value string, limit int) string {- value = strings.TrimSpace(value)- if len(value) <= limit {- return value- }- return value[:limit]-}-func valueOrEmpty[T any](value *T, render func(*T) string) string {if value == nil {return ""diff --git a/cli/beacon/internal/endpoint/inventory/content.go b/cli/beacon/internal/endpoint/inventory/content.goindex 475c296e..4d29dcb1 100644--- a/cli/beacon/internal/endpoint/inventory/content.go+++ b/cli/beacon/internal/endpoint/inventory/content.go@@ -3,7 +3,8 @@ package inventoryimport ("regexp""strings"- "unicode/utf8"++ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve")// DefaultMaxContentBytes caps how much redacted text is retained per captured@@ -62,7 +63,7 @@ func captureContent(data []byte, maxBytes int) *CapturedContent {redacted, count := redactSecrets(string(data))truncated := falseif maxBytes > 0 && len(redacted) > maxBytes {- redacted = truncateUTF8(redacted, maxBytes)+ redacted = asymptoteobserve.TruncateUTF8(redacted, maxBytes)truncated = true}return &CapturedContent{@@ -255,16 +256,3 @@ func yamlBlockScalarIndent(header string, keyIndent int) int {}return 0}--func truncateUTF8(s string, max int) string {- if max <= 0 {- return ""- }- if len(s) <= max {- return s- }- for max > 0 && !utf8.RuneStart(s[max]) {- max--- }- return s[:max]-}diff --git a/pkg/asymptoteobserve/privacy.go b/pkg/asymptoteobserve/privacy.goindex 4ab914cf..aa495fbd 100644--- a/pkg/asymptoteobserve/privacy.go+++ b/pkg/asymptoteobserve/privacy.go@@ -4,6 +4,7 @@ import ("encoding/json""regexp""strings"+ "unicode/utf8")const (@@ -283,6 +284,21 @@ var redactKeySuffixes = []string{"privatekey", "authorization", "credential", "credentials", "cookie",}+// TruncateUTF8 cuts value to at most limit bytes, backing up to the start of a character so a+// multi-byte character is never split into invalid UTF-8.+func TruncateUTF8(value string, limit int) string {+ if limit <= 0 {+ return ""+ }+ if len(value) <= limit {+ return value+ }+ for limit > 0 && !utf8.RuneStart(value[limit]) {+ limit--+ }+ return value[:limit]+}+func TruncateString(value string, limit int) string {if len(value) <= limit {return valuediff --git a/pkg/asymptoteobserve/privacy_test.go b/pkg/asymptoteobserve/privacy_test.goindex 8bd3237f..2e5821d7 100644--- a/pkg/asymptoteobserve/privacy_test.go+++ b/pkg/asymptoteobserve/privacy_test.go@@ -361,3 +361,25 @@ func TestCleanStringWidensTheWindowWhenRedactionShrinksIt(t *testing.T) {t.Fatalf("CleanString = %d bytes, want at most %d", len(got), limit)}}++func TestTruncateUTF8NeverSplitsACharacter(t *testing.T) {+ for _, tc := range []struct {+ name, value string+ limit int+ want string+ }{+ {"fits", "abc", 3, "abc"},+ {"ascii cut", "abcdef", 4, "abcd"},+ {"cut inside a 3-byte character backs up", strings.Repeat("界", 3), 7, strings.Repeat("界", 2)},+ {"cut inside a 4-byte character backs up", "a😀b", 3, "a"},+ {"cut exactly at a character boundary", strings.Repeat("界", 3), 6, strings.Repeat("界", 2)},+ {"limit shorter than the first character", "界", 2, ""},+ {"zero limit", "abc", 0, ""},+ } {+ t.Run(tc.name, func(t *testing.T) {+ if got := TruncateUTF8(tc.value, tc.limit); got != tc.want {+ t.Fatalf("TruncateUTF8(%q, %d) = %q, want %q", tc.value, tc.limit, got, tc.want)+ }+ })+ }+}diff --git a/pkg/asymptoteobserve/tracebuild.go b/pkg/asymptoteobserve/tracebuild.goindex 2e87216f..5e2cb9bb 100644--- a/pkg/asymptoteobserve/tracebuild.go+++ b/pkg/asymptoteobserve/tracebuild.go@@ -787,13 +787,9 @@ func traceFirstNonEmpty(values ...string) string {return ""}-// traceTruncate trims value and cuts it to limit bytes.+// traceTruncate trims value and cuts it to at most limit bytes without splitting UTF-8.func traceTruncate(value string, limit int) string {- value = strings.TrimSpace(value)- if len(value) <= limit {- return value- }- return value[:limit]+ return TruncateUTF8(strings.TrimSpace(value), limit)}func traceRawString(raw map[string]interface{}, key string) string {