Skip to content

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

fix(browser-extension): flag truncated tool arguments and results (#944)

Rudy Mizrahi Celeklie6d4e1c67cca
diff --git a/browser-extension/src/shared/normalize.ts b/browser-extension/src/shared/normalize.ts
index 0d06e8fc..486d39d5 100644
--- a/browser-extension/src/shared/normalize.ts
+++ b/browser-extension/src/shared/normalize.ts
@@ -244,11 +244,16 @@ function toolRecord(
str('gen_ai.tool.call.name', tool.name),
];
const args = shapeText(stringify(tool.arguments), retention);
+ let truncated = args?.truncated ?? false;
if (args) attrs.push(str('gen_ai.tool.call.arguments', args.text));
if (tool.result !== undefined) {
const res = shapeText(stringify(tool.result), retention);
- if (res) attrs.push(str('gen_ai.tool.call.result', res.text));
+ if (res) {
+ attrs.push(str('gen_ai.tool.call.result', res.text));
+ truncated ||= res.truncated;
+ }
}
+ if (truncated) attrs.push(bool('beacon.field_truncated', true));
return {
timeUnixNano: when,
severityNumber: severityNumber('info'),
diff --git a/browser-extension/test/unit/tool-truncation.test.ts b/browser-extension/test/unit/tool-truncation.test.ts
new file mode 100644
index 00000000..8b97f840
--- /dev/null
+++ b/browser-extension/test/unit/tool-truncation.test.ts
@@ -0,0 +1,20 @@
+import { expect, it } from 'vitest';
+import { normalizeTurn } from '../../src/shared/normalize.js';
+import { attrValue } from '../../src/shared/otlp.js';
+import { MAX_FIELD_BYTES } from '../../src/shared/vocab.js';
+import type { ChatTurn } from '../../src/shared/types.js';
+const turn = (over: Partial<ChatTurn> = {}): ChatTurn => ({ site: 'claude_web', sessionId: 'session', turnId: 'turn', promptText: 'hi', inputMessages: [], outputMessages: [], responseText: 'ok', toolCalls: [], usage: {}, startedAt: 1700000000000, captureMode: 'sse', ...over });
+it('marks only tool records whose retained content is truncated', () => {
+ for (const field of ['arguments', 'result'] as const)
+ for (const retention of ['full', 'redacted', 'metadata'] as const) {
+ const tools = [{ id: 'big', name: 'tool', [field]: 'x'.repeat(MAX_FIELD_BYTES + 1) }, { id: 'small', name: 'tool', arguments: 'ok', result: 'ok' }];
+ const records = normalizeTurn(turn({ toolCalls: tools }), retention).logRecords.filter(r => attrValue(r.attributes, 'beacon.event.action') === 'tool.invoked');
+ expect(attrValue(records[0].attributes, 'beacon.field_truncated')).toBe(retention === 'metadata' ? undefined : true);
+ expect(attrValue(records[1].attributes, 'beacon.field_truncated')).toBeUndefined();
+ expect(records.every(r => attrValue(r.attributes, 'gen_ai.tool.call.id') !== undefined)).toBe(true);
+ }
+});
+it('does not flag exact-limit content', () => {
+ const r = normalizeTurn(turn({ toolCalls: [{ id: 'exact', name: 'tool', arguments: 'x'.repeat(MAX_FIELD_BYTES) }] }), 'full').logRecords.at(-1)!;
+ expect(attrValue(r.attributes, 'beacon.field_truncated')).toBeUndefined();
+});
diff --git a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
index 7859552f..c54332e7 100644
--- a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
+++ b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter.go
@@ -1192,6 +1192,11 @@ func (c Converter) PopulateCommon(event *Event, attrs map[string]interface{}) {
event.Repository = FirstString(attrs, "vcs.repository.url", "repository", "repo.path", "workspace.repository")
event.Branch = FirstString(attrs, "vcs.branch.name", "git.branch", "branch")
event.UserAgent = UserAgentFromAttrs(attrs)
+ // A sender that cut a field to fit (the browser extension) says so on the record; promoted, so
+ // the flag survives metadata-only forwarding, which drops raw attributes.
+ if truncated, _ := BoolAttr(attrs, "beacon.field_truncated"); truncated {
+ event.Truncated = true
+ }
if id := FirstString(attrs, "gen_ai.conversation.id", "beacon.session.id", "copilot_chat.session_id", "copilot_chat.chat_session_id", "conversation.id", "conversation_id", "session.id"); id != "" || FirstString(attrs, "cwd", "working_directory", "workspace") != "" {
event.Session = &SessionInfo{
ID: id,
diff --git a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter_test.go b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter_test.go
index 6e27c271..a2a70240 100644
--- a/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter_test.go
+++ b/collector-builder/exporter/beaconjsonexporter/internal/beaconevent/converter_test.go
@@ -1784,6 +1784,23 @@ func TestEventFromLogOmitsUserAgentWithoutIdentity(t *testing.T) {
}
}
+// The browser extension cuts an oversized prompt, response or tool field before sending and says
+// so on the record. The flag has to reach field_truncated: metadata-only forwarding drops raw
+// attributes, and field_truncated is what downstream consumers read.
+func TestEventFromLogPromotesBrowserFieldTruncated(t *testing.T) {
+ for _, truncated := range []bool{true, false} {
+ logs := browserExtensionLogs(false)
+ record := logs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0)
+ if truncated {
+ record.Attributes().PutBool("beacon.field_truncated", true)
+ }
+ events := NewConverter(Options{}).EventsFromLogs(logs)
+ if len(events) != 1 || events[0].Truncated != truncated {
+ t.Fatalf("beacon.field_truncated=%v: field_truncated = %v", truncated, events[0].Truncated)
+ }
+ }
+}
+
func TestUserAgentFromAttrs(t *testing.T) {
cases := []struct {
name string