Skip to content

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

refactor: share content-block byte handling in asymptoteobserve

Kyle Elliott8868efd0f91f
diff --git a/cli/beacon-hooks/cmd/content_blocks.go b/cli/beacon-hooks/cmd/content_blocks.go
deleted file mode 100644
index f5b02c85..00000000
--- a/cli/beacon-hooks/cmd/content_blocks.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package cmd
-
-import (
- "crypto/sha256"
- "encoding/base64"
- "encoding/hex"
-)
-
-// summarizeEncodedContent replaces the encoded bytes in a list of content blocks with their
-// decoded size and SHA-256, keeping every other field and the block order.
-//
-// Claude Code delivers an MCP tool's image as an Anthropic image block with its base64 under
-// source.data. MCP's own image and audio blocks carry it under data, and an embedded resource
-// under blob. The writer cuts every string at 4096 bytes, so a stored copy could never be decoded,
-// and each one costs that much of the 64 KiB event budget: enough screenshots and the writer drops
-// the whole result, text blocks included. The size and digest still identify the media, which
-// Claude Code saves under the session's tool-results directory and names in the next block.
-func summarizeEncodedContent(blocks []interface{}) []interface{} {
- out := make([]interface{}, len(blocks))
- for i, item := range blocks {
- out[i] = item
- block, ok := item.(map[string]interface{})
- if !ok {
- continue
- }
- switch block["type"] {
- case "image", "audio", "document":
- if source, ok := block["source"].(map[string]interface{}); ok && source["type"] == "base64" {
- if summary, changed := summarizeEncodedField(source, "data"); changed {
- out[i] = withField(block, "source", summary)
- }
- } else if summary, changed := summarizeEncodedField(block, "data"); changed {
- out[i] = summary
- }
- case "resource":
- if resource, ok := block["resource"].(map[string]interface{}); ok {
- if summary, changed := summarizeEncodedField(resource, "blob"); changed {
- out[i] = withField(block, "resource", summary)
- }
- }
- }
- }
- return out
-}
-
-// summarizeEncodedField returns a copy of m with the base64 string under key replaced by
-// "bytes" and "sha256" of the decoded data. A value that is not a base64 string is not encoded
-// bytes, and m is returned unchanged.
-func summarizeEncodedField(m map[string]interface{}, key string) (map[string]interface{}, bool) {
- encoded, ok := m[key].(string)
- if !ok {
- return m, false
- }
- decoded, err := base64.StdEncoding.DecodeString(encoded)
- if err != nil {
- return m, false
- }
- sum := sha256.Sum256(decoded)
- out := withField(m, "bytes", len(decoded))
- delete(out, key)
- out["sha256"] = hex.EncodeToString(sum[:])
- return out, true
-}
-
-// withField returns a shallow copy of m with key set to value.
-func withField(m map[string]interface{}, key string, value interface{}) map[string]interface{} {
- out := make(map[string]interface{}, len(m)+1)
- for k, v := range m {
- out[k] = v
- }
- out[key] = value
- return out
-}
diff --git a/cli/beacon-hooks/cmd/post_tool.go b/cli/beacon-hooks/cmd/post_tool.go
index 734241bf..b0ccd3c1 100644
--- a/cli/beacon-hooks/cmd/post_tool.go
+++ b/cli/beacon-hooks/cmd/post_tool.go
@@ -10,6 +10,7 @@ import (
"github.com/asymptote-labs/agent-beacon/cli/beacon-hooks/internal/config"
"github.com/asymptote-labs/agent-beacon/cli/beacon-hooks/internal/diff"
"github.com/asymptote-labs/agent-beacon/cli/beacon-hooks/internal/logging"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
var postToolCmd = &cobra.Command{
@@ -404,7 +405,7 @@ func resolveToolResponse(input map[string]interface{}) map[string]interface{} {
if len(content) == 0 {
return nil
}
- return map[string]interface{}{"content": summarizeEncodedContent(content)}
+ return map[string]interface{}{"content": asymptoteobserve.SummarizeEncodedContent(content)}
}
// If tool_response is a plain string, wrap it for downstream compatibility
if respStr, ok := input["tool_response"].(string); ok && respStr != "" {
diff --git a/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go b/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go
index b6de6c2a..ab1ce199 100644
--- a/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go
+++ b/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go
@@ -90,38 +90,3 @@ func TestClaudeMCPPostToolWithNoContentRecordsNoResult(t *testing.T) {
}
}
-// MCP's own media shapes, which a runtime that passes blocks through unchanged would send, are
-// summarized the same way; anything that is not base64 is left as it is, and the input is not
-// modified.
-func TestSummarizeEncodedContent(t *testing.T) {
- digest := func(s string) string {
- sum := sha256.Sum256([]byte(s))
- return hex.EncodeToString(sum[:])
- }
- in := []interface{}{
- map[string]interface{}{"type": "image", "mimeType": "image/png", "data": base64.StdEncoding.EncodeToString([]byte("img"))},
- map[string]interface{}{"type": "audio", "mimeType": "audio/wav", "data": base64.StdEncoding.EncodeToString([]byte("audio"))},
- map[string]interface{}{"type": "resource", "resource": map[string]interface{}{"uri": "file:///r.bin", "blob": base64.StdEncoding.EncodeToString([]byte("bin"))}},
- map[string]interface{}{"type": "resource", "resource": map[string]interface{}{"uri": "file:///r.txt", "text": "notes"}},
- map[string]interface{}{"type": "image", "data": "not base64!"},
- map[string]interface{}{"type": "text", "text": "kept"},
- "not a block",
- }
- before, err := json.Marshal(in)
- if err != nil {
- t.Fatal(err)
- }
- got := summarizeEncodedContent(in)
- want := []interface{}{
- map[string]interface{}{"type": "image", "mimeType": "image/png", "bytes": 3, "sha256": digest("img")},
- map[string]interface{}{"type": "audio", "mimeType": "audio/wav", "bytes": 5, "sha256": digest("audio")},
- map[string]interface{}{"type": "resource", "resource": map[string]interface{}{"uri": "file:///r.bin", "bytes": 3, "sha256": digest("bin")}},
- in[3], in[4], in[5], in[6],
- }
- if !reflect.DeepEqual(got, want) {
- t.Errorf("summarized blocks:\ngot %#v\nwant %#v", got, want)
- }
- if after, _ := json.Marshal(in); string(after) != string(before) {
- t.Errorf("input was modified:\nbefore %s\nafter %s", before, after)
- }
-}
diff --git a/pkg/asymptoteobserve/toolresult.go b/pkg/asymptoteobserve/toolresult.go
index 64af4238..2a58dec2 100644
--- a/pkg/asymptoteobserve/toolresult.go
+++ b/pkg/asymptoteobserve/toolresult.go
@@ -1,8 +1,12 @@
package asymptoteobserve
import (
+ "crypto/sha256"
+ "encoding/base64"
+ "encoding/hex"
"encoding/json"
- "sort"
+ "maps"
+ "slices"
"strings"
)
@@ -66,17 +70,24 @@ var contentBlockTypes = map[string]bool{
"text": true, "image": true, "audio": true, "document": true, "resource": true, "resource_link": true,
}
-// skipContentBlockField reports whether a field of a content block, or of a resource block's
-// embedded resource, is metadata or encoded bytes rather than text the model reads.
+// contentBlock returns a list element as a content block, when it is one.
+func contentBlock(item interface{}) (map[string]interface{}, bool) {
+ block, ok := item.(map[string]interface{})
+ if !ok {
+ return nil, false
+ }
+ blockType, _ := block["type"].(string)
+ return block, contentBlockTypes[blockType]
+}
+
+// isEncodedBytes reports whether a field of a content block, or of a resource block's embedded
+// resource, holds encoded bytes rather than text the model reads: an image's or audio clip's
+// base64 "data", an Anthropic block's base64 "source", or an embedded resource's "blob".
//
-// Encoded bytes are an image's or audio clip's base64 "data", an Anthropic block's base64
-// "source", and an embedded resource's "blob". They are skipped because they are not text, and
-// because a run of them would otherwise use up the result_text cap before a text block that
-// follows them. "sha256" is the digest a capture path records in their place.
-func skipContentBlockField(key string, value interface{}) bool {
+// It is the one definition both readers of content blocks share: ToolResultPlainText leaves these
+// fields out of the text, and SummarizeEncodedContent replaces them with a size and digest.
+func isEncodedBytes(key string, value interface{}) bool {
switch key {
- case "type", "mimeType", "annotations", "_meta", "sha256":
- return true
case "data", "blob":
_, encoded := value.(string)
return encoded
@@ -87,6 +98,74 @@ func skipContentBlockField(key string, value interface{}) bool {
return false
}
+// skipContentBlockField reports whether a field of a content block, or of a resource block's
+// embedded resource, is metadata or encoded bytes rather than text the model reads.
+//
+// Encoded bytes are skipped because they are not text, and because a run of them would otherwise
+// use up the result_text cap before a text block that follows them. "sha256" is the digest
+// SummarizeEncodedContent records in their place.
+func skipContentBlockField(key string, value interface{}) bool {
+ switch key {
+ case "type", "mimeType", "annotations", "_meta", "sha256":
+ return true
+ }
+ return isEncodedBytes(key, value)
+}
+
+// SummarizeEncodedContent returns a list of content blocks with the encoded bytes in each block
+// replaced by "bytes" (the decoded size) and "sha256" (of the decoded data), keeping every other
+// field and the block order. A value that is not valid base64 is not encoded bytes and is kept.
+// The input is not modified.
+//
+// A capture path stores this rather than the bytes because the writer cuts every string at
+// DefaultStringLimit, so a stored copy could never be decoded, and each copy costs that much of
+// the event's size budget: enough images and the writer drops the whole result, text included.
+// The size and digest still identify the media.
+func SummarizeEncodedContent(blocks []interface{}) []interface{} {
+ out := make([]interface{}, len(blocks))
+ for i, item := range blocks {
+ out[i] = item
+ if block, ok := contentBlock(item); ok {
+ out[i], _ = summarizeEncodedFields(block)
+ }
+ }
+ return out
+}
+
+// summarizeEncodedFields returns m with its encoded fields summarized, reporting whether any were.
+// An embedded resource and an Anthropic base64 source hold their bytes one level down, and are
+// summarized the same way. When nothing changes, m itself is returned.
+func summarizeEncodedFields(m map[string]interface{}) (map[string]interface{}, bool) {
+ out := maps.Clone(m)
+ changed := false
+ for key, value := range m {
+ if nested, ok := value.(map[string]interface{}); ok {
+ if key != "resource" && !isEncodedBytes(key, value) {
+ continue
+ }
+ if summary, ok := summarizeEncodedFields(nested); ok {
+ out[key], changed = summary, true
+ }
+ continue
+ }
+ if !isEncodedBytes(key, value) {
+ continue
+ }
+ decoded, err := base64.StdEncoding.DecodeString(value.(string))
+ if err != nil {
+ continue
+ }
+ sum := sha256.Sum256(decoded)
+ delete(out, key)
+ out["bytes"], out["sha256"] = len(decoded), hex.EncodeToString(sum[:])
+ changed = true
+ }
+ if !changed {
+ return m, false
+ }
+ return out, true
+}
+
func collectToolResultStrings(v interface{}, out *[]string) {
switch typed := v.(type) {
case string:
@@ -97,11 +176,9 @@ func collectToolResultStrings(v interface{}, out *[]string) {
collectMapStrings(typed, nil, out)
case []interface{}:
for _, item := range typed {
- if block, ok := item.(map[string]interface{}); ok {
- if blockType, _ := block["type"].(string); contentBlockTypes[blockType] {
- collectMapStrings(block, skipContentBlockField, out)
- continue
- }
+ if block, ok := contentBlock(item); ok {
+ collectMapStrings(block, skipContentBlockField, out)
+ continue
}
collectToolResultStrings(item, out)
}
@@ -124,12 +201,7 @@ func collectToolResultStrings(v interface{}, out *[]string) {
// collectMapStrings walks m in sorted key order, leaving out the fields skip names. A content
// block's embedded resource is walked with the same skip, since its blob is encoded bytes too.
func collectMapStrings(m map[string]interface{}, skip func(string, interface{}) bool, out *[]string) {
- keys := make([]string, 0, len(m))
- for key := range m {
- keys = append(keys, key)
- }
- sort.Strings(keys)
- for _, key := range keys {
+ for _, key := range slices.Sorted(maps.Keys(m)) {
value := m[key]
if skip != nil && skip(key, value) {
continue
diff --git a/pkg/asymptoteobserve/toolresult_test.go b/pkg/asymptoteobserve/toolresult_test.go
index bcc332fe..ea72a3fa 100644
--- a/pkg/asymptoteobserve/toolresult_test.go
+++ b/pkg/asymptoteobserve/toolresult_test.go
@@ -1,6 +1,13 @@
package asymptoteobserve
-import "testing"
+import (
+ "crypto/sha256"
+ "encoding/base64"
+ "encoding/hex"
+ "encoding/json"
+ "reflect"
+ "testing"
+)
func TestIngestedContentScope(t *testing.T) {
for action, want := range map[string]bool{
@@ -62,3 +69,48 @@ func TestToolResultPlainText(t *testing.T) {
func blocks(items ...interface{}) map[string]interface{} {
return map[string]interface{}{"content": items}
}
+
+// Every shape isEncodedBytes names is summarized: MCP data, an embedded resource's blob, and the
+// Anthropic base64 source Claude Code sends. Anything that is not base64 is kept, other fields and
+// the block order are kept, and the input is not modified.
+func TestSummarizeEncodedContent(t *testing.T) {
+ encode := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) }
+ digest := func(s string) string {
+ sum := sha256.Sum256([]byte(s))
+ return hex.EncodeToString(sum[:])
+ }
+ in := []interface{}{
+ map[string]interface{}{"type": "image", "mimeType": "image/png", "data": encode("img")},
+ map[string]interface{}{"type": "audio", "mimeType": "audio/wav", "data": encode("audio")},
+ map[string]interface{}{"type": "image", "source": map[string]interface{}{"type": "base64", "media_type": "image/png", "data": encode("png")}},
+ map[string]interface{}{"type": "resource", "resource": map[string]interface{}{"uri": "file:///r.bin", "blob": encode("bin")}},
+ map[string]interface{}{"type": "resource", "resource": map[string]interface{}{"uri": "file:///r.txt", "text": "notes"}},
+ map[string]interface{}{"type": "image", "source": map[string]interface{}{"type": "url", "url": "https://example.test/a.png"}},
+ map[string]interface{}{"type": "image", "data": "not base64!"},
+ map[string]interface{}{"type": "text", "text": "kept"},
+ map[string]interface{}{"data": encode("not a block")},
+ "not a map",
+ }
+ before, err := json.Marshal(in)
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := SummarizeEncodedContent(in)
+ want := []interface{}{
+ map[string]interface{}{"type": "image", "mimeType": "image/png", "bytes": 3, "sha256": digest("img")},
+ map[string]interface{}{"type": "audio", "mimeType": "audio/wav", "bytes": 5, "sha256": digest("audio")},
+ map[string]interface{}{"type": "image", "source": map[string]interface{}{"type": "base64", "media_type": "image/png", "bytes": 3, "sha256": digest("png")}},
+ map[string]interface{}{"type": "resource", "resource": map[string]interface{}{"uri": "file:///r.bin", "bytes": 3, "sha256": digest("bin")}},
+ in[4], in[5], in[6], in[7], in[8], in[9],
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("summarized blocks:\ngot %#v\nwant %#v", got, want)
+ }
+ if after, _ := json.Marshal(in); string(after) != string(before) {
+ t.Errorf("input was modified:\nbefore %s\nafter %s", before, after)
+ }
+ // What the summary leaves behind contributes nothing to the rule text but the text block.
+ if text := ToolResultPlainText(map[string]interface{}{"content": got[:4]}); text != "file:///r.bin" {
+ t.Errorf("summarized media contributes %q to the rule text, want only the resource URI", text)
+ }
+}