Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(asymptoteobserve): truncate strings on a UTF-8 character boundary (#864) (#872)

claude[bot]a7be43ace760
diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdx
index 44cdcb75..08336f0a 100644
--- a/docs/changelog/cli.mdx
+++ b/docs/changelog/cli.mdx
@@ -12,6 +12,7 @@ Stay up to date with the latest changes to the Agent Beacon CLI.
## October 2026
- **Hermes Agent install, hooks and inventory follow `HERMES_HOME` and the active profile** · `beacon endpoint hooks install|uninstall|status --harness hermes`, `beacon endpoint discover`, inventory and the `beacon mcp connect` hint now use `config.yaml` in the directory Hermes uses: `HERMES_HOME` when set, otherwise `~/.hermes` plus `HERMES_DATA_DIR_SUFFIX` (`%LOCALAPPDATA%\hermes` on Windows). They previously always used `~/.hermes/config.yaml`, so with `HERMES_HOME` set Hermes never loaded Beacon's hooks. All of these, and Hermes session status, sync and handoff, also follow a sticky profile selected with `hermes profile use`, as Hermes does. A per-user install or repair also writes `HERMES_HOME` and `HERMES_DATA_DIR_SUFFIX` into the scheduled inventory job, as it does `CODEX_HOME`. An `active_profile` naming a profile Hermes cannot use is an error, as Hermes treats it. A profile chosen for one command with `hermes -p` is still not followed.
+- **Truncated event fields no longer end in `�`** · Long fields cut to their size limit, such as a tool's command, a message or a file path, were cut by byte, so text in Chinese, Japanese or Korean, emoji or other non-ASCII characters could end in a broken character that became `�` in the runtime log and every destination. The hook adapter, the collector and the CLI now cut on a character boundary, keeping the `...[truncated]` marker.
- **Concurrent hooks no longer clobber the per-session state file** · The hook adapter keeps a small `state.json` (session model, prompt-emitted flag, reprompt counts) that every hook process reads and rewrites. Two hooks running at once shared one temporary file, so one could fail with `rename ...: no such file or directory` and a surviving write could drop the other's change. Access is now serialized with a file lock and each write uses its own temporary file.
- **Claude Code settings, sessions and inventory follow `CLAUDE_CONFIG_DIR`** · `beacon endpoint install`, `beacon endpoint hooks install --harness claude`, `beacon endpoint discover`, inventory, `beacon endpoint claude status` and `sync`, the session backfill and `beacon handoff` now use `$CLAUDE_CONFIG_DIR` when `CLAUDE_CONFIG_DIR` is set, as Claude Code does: `settings.json`, `.claude.json`, `skills` and `projects` all move with it. They previously always used `~/.claude` (and `~/.claude.json`), so with the variable set Claude Code never saw Beacon's OTLP settings or hooks, live capture silently recorded nothing, and session sync found no transcripts. A per-user install or repair also writes the resolved `CLAUDE_CONFIG_DIR` into the scheduled inventory job, as it does `CODEX_HOME`. Project-level `.claude/settings.json` is unchanged, and `--projects-dir` and `--claude-projects-dir` still override.
- **Codex install, hooks and inventory follow `CODEX_HOME`** · `beacon endpoint install`, `beacon endpoint hooks install --harness codex`, `beacon endpoint discover` and inventory now read and write `$CODEX_HOME/config.toml` and `$CODEX_HOME/hooks.json` when `CODEX_HOME` is set, as Codex does. They previously always used `~/.codex`, so with `CODEX_HOME` set, Codex never saw the OTLP exporter or the SessionStart hook and live capture silently recorded nothing. A per-user install or repair also writes the resolved `CODEX_HOME` into the scheduled inventory job, which launchd and systemd start without your shell's environment. A `CODEX_HOME` that is not an existing directory is now an install error rather than a silent write to `~/.codex`. Project-level `.codex` files are unchanged.
diff --git a/pkg/asymptoteobserve/privacy.go b/pkg/asymptoteobserve/privacy.go
index aa495fbd..201d26c6 100644
--- a/pkg/asymptoteobserve/privacy.go
+++ b/pkg/asymptoteobserve/privacy.go
@@ -299,14 +299,16 @@ func TruncateUTF8(value string, limit int) string {
return value[:limit]
}
+// TruncateString cuts value to at most limit bytes on a character boundary, marking the cut
+// when limit leaves room for the marker.
func TruncateString(value string, limit int) string {
if len(value) <= limit {
return value
}
if limit < 32 {
- return value[:limit]
+ return TruncateUTF8(value, limit)
}
- return value[:limit-15] + "...[truncated]"
+ return TruncateUTF8(value, limit-15) + "...[truncated]"
}
// CleanString redacts before it truncates, so a cut can never leave part of a secret behind.
diff --git a/pkg/asymptoteobserve/privacy_test.go b/pkg/asymptoteobserve/privacy_test.go
index 2e5821d7..73808df7 100644
--- a/pkg/asymptoteobserve/privacy_test.go
+++ b/pkg/asymptoteobserve/privacy_test.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"strings"
"testing"
+ "unicode/utf8"
)
func TestRedactStringRedactsKnownSecretForms(t *testing.T) {
@@ -383,3 +384,74 @@ func TestTruncateUTF8NeverSplitsACharacter(t *testing.T) {
})
}
}
+
+func TestTruncateStringNeverSplitsACharacter(t *testing.T) {
+ marked := strings.Repeat("a", 20) + strings.Repeat("界", 10)
+ for _, tc := range []struct {
+ name, value string
+ limit int
+ want string
+ }{
+ {"fits", "héllo", 6, "héllo"},
+ {"empty", "", 0, ""},
+ {"zero limit", "héllo", 0, ""},
+ {"limit 1 inside a 2-byte character", "é", 1, ""},
+ {"limit 1 keeps an ASCII byte", "aé", 1, "a"},
+ {"ascii unmarked", "abcdefgh", 4, "abcd"},
+ {"ascii marked", strings.Repeat("a", 40), 32, strings.Repeat("a", 17) + "...[truncated]"},
+ {"cut inside a 2-byte character backs up", "aéé", 4, "aé"},
+ {"cut inside a 3-byte character backs up", strings.Repeat("界", 3), 8, strings.Repeat("界", 2)},
+ {"cut inside a 4-byte character backs up", "😀😀", 7, "😀"},
+ {"cut at a character boundary", strings.Repeat("界", 3), 6, strings.Repeat("界", 2)},
+ // limit-15 = 25 falls one byte into the second 界, so the kept prefix backs up to 23 bytes.
+ {"marked cut backs up", marked, 40, strings.Repeat("a", 20) + "界...[truncated]"},
+ {"marked cut at a character boundary", marked, 41, strings.Repeat("a", 20) + "界界...[truncated]"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := TruncateString(tc.value, tc.limit); got != tc.want {
+ t.Fatalf("TruncateString(%q, %d) = %q, want %q", tc.value, tc.limit, got, tc.want)
+ }
+ })
+ }
+}
+
+// Every cut position, including each byte inside 2-, 3- and 4-byte characters, on both sides of
+// the marker threshold, must stay within the limit and decode as valid UTF-8.
+func TestTruncateStringIsAlwaysValidUTF8WithinTheLimit(t *testing.T) {
+ values := []string{
+ strings.Repeat("abc", 40),
+ strings.Repeat("é", 60),
+ strings.Repeat("界", 40),
+ strings.Repeat("😀", 30),
+ strings.Repeat("aé界😀", 12),
+ }
+ for _, value := range values {
+ for limit := 0; limit <= len(value)+1; limit++ {
+ got := TruncateString(value, limit)
+ if len(got) > limit {
+ t.Fatalf("TruncateString(%q, %d) = %d bytes, over the limit", value, limit, len(got))
+ }
+ if !utf8.ValidString(got) {
+ t.Fatalf("TruncateString(%q, %d) = %q, not valid UTF-8", value, limit, got)
+ }
+ if len(value) <= limit {
+ if got != value {
+ t.Fatalf("TruncateString(%q, %d) = %q, want the value unchanged", value, limit, got)
+ }
+ continue
+ }
+ kept, budget := got, limit
+ if limit >= 32 {
+ var ok bool
+ if kept, ok = strings.CutSuffix(got, "...[truncated]"); !ok {
+ t.Fatalf("TruncateString(%q, %d) = %q, want the truncation marker", value, limit, got)
+ }
+ budget = limit - 15
+ }
+ // The cut gives up only the bytes of the one character it would have split.
+ if !strings.HasPrefix(value, kept) || budget-len(kept) >= utf8.UTFMax {
+ t.Fatalf("TruncateString(%q, %d) kept %q, want the longest whole-character prefix", value, limit, kept)
+ }
+ }
+ }
+}