Skip to content

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

fix(browser): retain canonical answer content and rebuild history (#994)

Divyam Talwar1978b3bc697a
diff --git a/browser-extension/e2e/chatgpt.replay.spec.ts b/browser-extension/e2e/chatgpt.replay.spec.ts
index f92a7a80..6a61cd61 100644
--- a/browser-extension/e2e/chatgpt.replay.spec.ts
+++ b/browser-extension/e2e/chatgpt.replay.spec.ts
@@ -48,6 +48,9 @@ test('captures a simple ChatGPT turn end-to-end into normalized OTLP', async ({
const promptAttrs = flatAttrs(promptRec!.attributes);
expect(promptAttrs['beacon.prompt.text']).toBe('good evening');
expect(promptAttrs['beacon.session.id']).toBe(CONV);
+ expect(JSON.parse(String(promptAttrs['gen_ai.input.messages']))).toEqual([
+ { role: 'user', parts: [{ type: 'text', content: 'good evening' }] },
+ ]);
// Response lands on the agent.response.completed record.
const rec = recordByAction(mockCollector.received, 'agent.response.completed');
@@ -55,5 +58,7 @@ test('captures a simple ChatGPT turn end-to-end into normalized OTLP', async ({
const attrs = flatAttrs(rec!.attributes);
expect(attrs['gen_ai.conversation.id']).toBe(CONV);
expect(attrs['gen_ai.response.model']).toBe('gpt-5-6-thinking');
- expect(String(attrs['gen_ai.output.messages'])).toContain('Good evening');
+ expect(JSON.parse(String(attrs['gen_ai.output.messages']))).toEqual([
+ { role: 'assistant', parts: [{ type: 'text', content: 'Good evening! Hello to you as well. Hope your day is going well.' }] },
+ ]);
});
diff --git a/browser-extension/e2e/claude.replay.spec.ts b/browser-extension/e2e/claude.replay.spec.ts
index 128831db..7fc62008 100644
--- a/browser-extension/e2e/claude.replay.spec.ts
+++ b/browser-extension/e2e/claude.replay.spec.ts
@@ -50,6 +50,9 @@ test('captures a simple Claude turn end-to-end into normalized OTLP', async ({
expect(promptAttrs['beacon.event.category']).toBe('prompt');
expect(promptAttrs['beacon.prompt.text']).toBe('What is the capital of France?');
expect(promptAttrs['beacon.session.id']).toBe(CONV);
+ expect(JSON.parse(String(promptAttrs['gen_ai.input.messages']))).toEqual([
+ { role: 'user', parts: [{ type: 'text', content: 'What is the capital of France?' }] },
+ ]);
// The response lands on the agent.response.completed record.
const rec = recordByAction(mockCollector.received, 'agent.response.completed');
@@ -57,7 +60,9 @@ test('captures a simple Claude turn end-to-end into normalized OTLP', async ({
const attrs = flatAttrs(rec!.attributes);
expect(attrs['gen_ai.conversation.id']).toBe(CONV);
expect(attrs['gen_ai.response.model']).toBe('claude-opus-4-8');
- expect(String(attrs['gen_ai.output.messages'])).toContain('Paris');
+ expect(JSON.parse(String(attrs['gen_ai.output.messages']))).toEqual([
+ { role: 'assistant', parts: [{ type: 'text', content: 'Paris.' }] },
+ ]);
});
test('captures the prompt when the page uses fetch(new Request(...))', async ({
diff --git a/browser-extension/src/shared/normalize.ts b/browser-extension/src/shared/normalize.ts
index 486d39d5..f7f3a191 100644
--- a/browser-extension/src/shared/normalize.ts
+++ b/browser-extension/src/shared/normalize.ts
@@ -5,7 +5,7 @@
// endpoint/user/timestamp itself, so we MUST NOT emit those. We emit the
// beacon.* + gen_ai.* attributes that converter.go maps into the Event schema.
-import type { ChatTurn, Retention, ToolCall } from './types.js';
+import type { ChatTurn, Msg, Retention, ToolCall } from './types.js';
import {
type KeyValue,
type LogRecord,
@@ -35,22 +35,24 @@ export interface NormalizedTurn {
const encoder = new TextEncoder();
-/** Truncate a string to a UTF-8 byte budget; returns [text, wasTruncated]. */
-function capBytes(text: string, max = MAX_FIELD_BYTES): [string, boolean] {
- if (encoder.encode(text).length <= max) return [text, false];
+const utf8Bytes = (text: string) => encoder.encode(text).length;
+const jsonBytes = (value: unknown) => encoder.encode(JSON.stringify(value)).length;
+
+/** Truncate a string to a byte budget, as UTF-8 or as measured by `size`,
+ * never splitting a surrogate pair; returns [text, wasTruncated]. */
+function capBytes(text: string, max = MAX_FIELD_BYTES, size = utf8Bytes): [string, boolean] {
+ if (size(text) <= max) return [text, false];
+ const prefix = (end: number) =>
+ text.slice(0, /[\uD800-\uDBFF]/.test(text[end - 1] ?? '') ? end - 1 : end);
// Binary-search the largest prefix that fits (chars ≠ bytes for multibyte).
let lo = 0;
let hi = text.length;
while (lo < hi) {
const mid = (lo + hi + 1) >> 1;
- if (encoder.encode(text.slice(0, mid)).length <= max) lo = mid;
+ if (size(prefix(mid)) <= max) lo = mid;
else hi = mid - 1;
}
- // UTF-16 indices may land between the two halves of an astral character.
- if (lo > 0 && lo < text.length &&
- text.charCodeAt(lo - 1) >= 0xd800 && text.charCodeAt(lo - 1) <= 0xdbff &&
- text.charCodeAt(lo) >= 0xdc00 && text.charCodeAt(lo) <= 0xdfff) lo--;
- return [text.slice(0, lo), true];
+ return [prefix(lo), true];
}
/** Light client-side scrubber for retention=redacted (defense in depth; the
@@ -75,6 +77,29 @@ function shapeText(text: string, retention: Retention): Shaped | null {
return { text: capped, truncated };
}
+/** Retention and cap for gen_ai.*.messages, as semconv JSON. The budget is the
+ * serialized value, so a capped field still parses: messages are kept in order
+ * and the first that does not fit is cut to the longest text that does. */
+function shapeMessages(messages: Msg[], retention: Retention): Shaped | null {
+ if (retention === 'metadata') return null;
+ const out: { role: Msg['role']; parts: { type: 'text'; content: string }[] }[] = [];
+ let bytes = 2; // array brackets
+ for (const { role, text } of messages) {
+ const content = retention === 'redacted' ? scrub(text) : text;
+ const message = { role, parts: [{ type: 'text' as const, content }] };
+ const size = jsonBytes(message) + (out.length ? 1 : 0);
+ if (bytes + size <= MAX_FIELD_BYTES) {
+ out.push(message);
+ bytes += size;
+ continue;
+ }
+ const room = jsonBytes(content) - (bytes + size - MAX_FIELD_BYTES);
+ if (room >= 2) out.push({ role, parts: [{ type: 'text', content: capBytes(content, room, jsonBytes)[0] }] });
+ return { text: JSON.stringify(out), truncated: true };
+ }
+ return { text: JSON.stringify(out), truncated: false };
+}
+
/**
* Normalize a chat turn into 1..N OTLP log records:
* - one response record (agent.response.completed, or agent.response if partial)
@@ -120,7 +145,7 @@ export function normalizeTurn(
// only does so for prompt-category events). Without this, the prompt stays
// buried in raw.attributes and never reaches the queryable prompt.text column.
const prompt = shapeText(turn.promptText, retention);
- const inputMsgs = shapeText(JSON.stringify(turn.inputMessages), retention);
+ const inputMsgs = shapeMessages(turn.inputMessages, retention);
if (prompt || inputMsgs) {
const attrs = base('prompt.submitted');
if (turn.requestModel) attrs.push(str('gen_ai.request.model', turn.requestModel));
@@ -186,7 +211,7 @@ export function normalizeTurn(
respAttrs.push(int('gen_ai.usage.cache_read.input_tokens', turn.usage.cacheReadInputTokens));
let respTrunc = false;
- const outputMsgs = shapeText(JSON.stringify(turn.outputMessages), retention);
+ const outputMsgs = shapeMessages(turn.outputMessages, retention);
if (outputMsgs) {
respAttrs.push(str('gen_ai.output.messages', outputMsgs.text));
respTrunc ||= outputMsgs.truncated;
diff --git a/browser-extension/test/unit/message-contract.test.ts b/browser-extension/test/unit/message-contract.test.ts
new file mode 100644
index 00000000..046a69dd
--- /dev/null
+++ b/browser-extension/test/unit/message-contract.test.ts
@@ -0,0 +1,152 @@
+import { readFileSync } from 'node:fs';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { claudeAdapter } from '../../src/adapters/claude.js';
+import { chatgptAdapter } from '../../src/adapters/chatgpt.js';
+import { normalizeTurn, turnToEnvelope } from '../../src/shared/normalize.js';
+import { attrValue, type LogRecord } from '../../src/shared/otlp.js';
+import type { ChatTurn, Msg } from '../../src/shared/types.js';
+import { MAX_FIELD_BYTES } from '../../src/shared/vocab.js';
+
+const cases = [
+ {
+ site: 'claude', adapter: claudeAdapter,
+ url: '/api/organizations/org-test/chat_conversations/11111111-2222-3333-4444-555555555555/completion',
+ body: { prompt: 'What is the capital of France?', model: 'claude-opus-4-8' },
+ answer: 'Paris.',
+ },
+ {
+ site: 'chatgpt', adapter: chatgptAdapter,
+ url: 'https://chatgpt.com/backend-api/f/conversation',
+ body: { model: 'gpt-5-6-thinking', messages: [{ author: { role: 'user' }, content: { parts: ['good evening'] } }] },
+ answer: 'Good evening! Hello to you as well. Hope your day is going well.',
+ },
+];
+
+afterEach(() => vi.useRealTimers());
+
+describe('recorded browser message contract', () => {
+ for (const { site, adapter, url, body, answer } of cases) {
+ it(`${site} emits semconv messages for a recorded turn`, () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date('2026-10-01T10:00:00Z'));
+ const parser = adapter.createParser(`message-contract-${site}`);
+ parser.onRequest(url, 'POST', JSON.stringify(body));
+ const fixture = readFileSync(new URL(`../../fixtures/${site}/simple-turn.sse`, import.meta.url), 'utf8');
+ for (let i = 0; i < fixture.length; i += 7) parser.onChunk(fixture.slice(i, i + 7));
+ parser.onDone();
+ const turn = parser.getTurn()!;
+ expect(turn.responseText).toBe(answer);
+ const envelope = turnToEnvelope(turn, 'full');
+ const response = envelope.resourceLogs[0].scopeLogs[0].logRecords.find((record) =>
+ attrValue(record.attributes, 'beacon.event.action') === 'agent.response.completed')!;
+ expect(JSON.parse(attrValue(response.attributes, 'gen_ai.output.messages') as string)).toEqual([
+ { role: 'assistant', parts: [{ type: 'text', content: answer }] },
+ ]);
+ });
+ }
+});
+
+function turn(messages: Msg[]): ChatTurn {
+ return {
+ site: 'claude_web', sessionId: 'message-session', turnId: 'message-turn',
+ requestModel: 'request', responseModel: 'response',
+ promptText: 'prompt', inputMessages: messages, outputMessages: messages,
+ responseText: messages.map((message) => message.text).join(''), toolCalls: [],
+ startedAt: 1000, completedAt: 2000, captureMode: 'sse',
+ usage: { inputTokens: 7, outputTokens: 3 },
+ };
+}
+
+function field(record: LogRecord, key: string): { role: string; parts: { type: string; content: string }[] }[] {
+ const serialized = attrValue(record.attributes, key) as string;
+ expect(new TextEncoder().encode(serialized).length).toBeLessThanOrEqual(MAX_FIELD_BYTES);
+ return JSON.parse(serialized);
+}
+
+describe('bounded structured message retention', () => {
+ it('preserves ordered message roles and text, including JSON escapes and Unicode', () => {
+ const messages: Msg[] = [
+ { role: 'system', text: 'line\n"quoted" \\ path 😀' },
+ { role: 'user', text: 'café' }, { role: 'assistant', text: '' },
+ { role: 'tool', text: 'tool result' },
+ ];
+ const records = normalizeTurn(turn(messages), 'full').logRecords;
+ for (const [record, key] of [[records[0], 'gen_ai.input.messages'], [records[1], 'gen_ai.output.messages']] as const) {
+ expect(field(record, key)).toEqual(messages.map(({ role, text }) => ({ role, parts: [{ type: 'text', content: text }] })));
+ expect(attrValue(record.attributes, 'beacon.field_truncated')).toBeUndefined();
+ }
+ });
+
+ it('scrubs message content before serialization and drops all message fields in metadata mode', () => {
+ const messages: Msg[] = [{ role: 'assistant', text: '[email protected] skabcdefghijklmnop "\\\n😀' }];
+ const redacted = normalizeTurn(turn(messages), 'redacted').logRecords;
+ expect(field(redacted[1], 'gen_ai.output.messages')[0].parts[0].content).toBe('[email] [token] "\\\n😀');
+ const metadata = normalizeTurn(turn(messages), 'metadata').logRecords;
+ expect(metadata).toHaveLength(1);
+ expect(attrValue(metadata[0].attributes, 'gen_ai.output.messages')).toBeUndefined();
+ expect(attrValue(metadata[0].attributes, 'gen_ai.input.messages')).toBeUndefined();
+ expect(attrValue(metadata[0].attributes, 'gen_ai.usage.output_tokens')).toBe(3);
+ expect(attrValue(metadata[0].attributes, 'beacon.record.id')).toBe('message-turn#agent.response.completed#0');
+ });
+
+ it('keeps empty arrays valid without falsely reporting truncation', () => {
+ const records = normalizeTurn(turn([]), 'full').logRecords;
+ expect(field(records[0], 'gen_ai.input.messages')).toEqual([]);
+ expect(field(records[1], 'gen_ai.output.messages')).toEqual([]);
+ expect(attrValue(records[1].attributes, 'beacon.field_truncated')).toBeUndefined();
+ });
+
+ it('preserves exact-budget JSON and flags a one-byte overflow', () => {
+ const overhead = new TextEncoder().encode(JSON.stringify([{ role: 'assistant', parts: [{ type: 'text', content: '' }] }])).length;
+ const text = 'x'.repeat(MAX_FIELD_BYTES - overhead);
+ for (const extra of ['', 'x']) {
+ const records = normalizeTurn(turn([{ role: 'assistant', text: text + extra }]), 'full').logRecords;
+ expect(field(records[1], 'gen_ai.output.messages')[0].parts[0].content).toBe(text);
+ expect(attrValue(records[1].attributes, 'beacon.field_truncated')).toBe(extra ? true : undefined);
+ }
+ });
+
+ it('keeps the largest complete Unicode prefix at serialized byte boundaries', () => {
+ const encoder = new TextEncoder();
+ const overhead = encoder.encode(JSON.stringify([{ role: 'assistant', parts: [{ type: 'text', content: '' }] }])).length;
+ for (const slack of [1, 2, 3, 4, 5, 6]) {
+ const text = 'a'.repeat(MAX_FIELD_BYTES - overhead - slack) + '😀end';
+ let oracle = '';
+ let bytes = overhead;
+ for (const cp of text) {
+ const next = encoder.encode(JSON.stringify(cp)).length - 2;
+ if (bytes + next > MAX_FIELD_BYTES) break;
+ oracle += cp;
+ bytes += next;
+ }
+ const record = normalizeTurn(turn([{ role: 'assistant', text }]), 'full').logRecords[1];
+ expect(field(record, 'gen_ai.output.messages')[0].parts[0].content).toBe(oracle);
+ expect(attrValue(record.attributes, 'beacon.field_truncated')).toBe(true);
+ }
+ });
+
+ for (const text of ['"\\\n'.repeat(MAX_FIELD_BYTES), '😀é中'.repeat(MAX_FIELD_BYTES)]) {
+ it(`bounds oversized ${text.startsWith('😀') ? 'Unicode' : 'escaped'} text as valid JSON`, () => {
+ const messages: Msg[] = [{ role: 'system', text: 'keep first' }, { role: 'assistant', text }, { role: 'user', text: 'later' }];
+ const records = normalizeTurn(turn(messages), 'full').logRecords;
+ for (const [record, key] of [[records[0], 'gen_ai.input.messages'], [records[1], 'gen_ai.output.messages']] as const) {
+ const kept = field(record, key);
+ expect(kept).toHaveLength(2);
+ expect(kept[0].parts[0].content).toBe('keep first');
+ const prefix = kept[1].parts[0].content;
+ expect(prefix.length).toBeGreaterThan(0);
+ expect(text.startsWith(prefix)).toBe(true);
+ expect(Array.from(prefix).some((cp) => cp.length === 1 && /[\uD800-\uDFFF]/.test(cp))).toBe(false);
+ expect(attrValue(record.attributes, 'beacon.field_truncated')).toBe(true);
+ }
+ });
+ }
+
+ it('drops a message whose wrapper alone does not fit', () => {
+ const overhead = new TextEncoder().encode(JSON.stringify([{ role: 'assistant', parts: [{ type: 'text', content: '' }] }])).length;
+ const full = 'x'.repeat(MAX_FIELD_BYTES - overhead);
+ const records = normalizeTurn(turn([{ role: 'assistant', text: full }, { role: 'user', text: '' }]), 'full').logRecords;
+ expect(field(records[1], 'gen_ai.output.messages')).toEqual([{ role: 'assistant', parts: [{ type: 'text', content: full }] }]);
+ expect(attrValue(records[1].attributes, 'beacon.field_truncated')).toBe(true);
+ });
+});
diff --git a/cli/beacon/internal/endpoint/dashboard/history_browser_messages_test.go b/cli/beacon/internal/endpoint/dashboard/history_browser_messages_test.go
new file mode 100644
index 00000000..8267d6b1
--- /dev/null
+++ b/cli/beacon/internal/endpoint/dashboard/history_browser_messages_test.go
@@ -0,0 +1,118 @@
+package dashboard
+
+import (
+ "encoding/json"
+ "os"
+ "strings"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+)
+
+func TestHistoryRebuildsVersion15LegacyBrowserMessages(t *testing.T) {
+ for _, harness := range []string{"claude_web", "chatgpt_web"} {
+ for _, removeLog := range []bool{false, true} {
+ name := harness + "/raw-present"
+ if removeLog {
+ name = harness + "/retained-only"
+ }
+ t.Run(name, func(t *testing.T) {
+ const answer = "A uniquely retained legacy browser answer."
+ line := []byte(`{"timestamp":"2026-10-01T10:00:02Z","event":{"id":"legacy-browser-answer","action":"agent.response.completed","category":"agent"},"harness":{"name":"` + harness + `","collection_method":"otlp"},"session":{"id":"legacy-browser-session"},"message":"browser assistant response completed","model":"browser-model","gen_ai":{"output":{"messages":[{"role":"assistant","text":"` + answer + `"}]},"usage":{"input_tokens":10,"output_tokens":4}}}`)
+ var event schema.Event
+ if err := json.Unmarshal(line, &event); err != nil {
+ t.Fatal(err)
+ }
+ path := traceStoreIndexedLog(t, [][]byte{line})
+ traceID := asymptoteobserve.TraceProjectionID(event)
+ store, err := openHistoryStore(false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var originalLine, originalRecordID, originalEventID string
+ var originalRowID int64
+ if err := store.db.QueryRow(`SELECT id, record_id, event_id, hex(line) FROM events`).Scan(&originalRowID, &originalRecordID, &originalEventID, &originalLine); err != nil {
+ t.Fatal(err)
+ }
+ // Version 15 classified the completion as an agent message, but its
+ // reader ignored role/text, leaving no retained content or search text.
+ // Seed that actual prior derivation, not an arbitrary old version.
+ prior := asymptoteobserve.TraceEventFromEvent(event, originalRecordID, 1)
+ prior.Content = nil
+ fold := newTraceFold(traceID, event)
+ fold.fold(event, prior)
+ if _, err := store.db.Exec(`UPDATE events SET hay = ?`, strings.ToLower(traceEventHaystack(prior))); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := store.db.Exec(`UPDATE traces SET aggregate = ?, hay = ?`, mustJSON(t, fold.state()), strings.ToLower(traceSummaryHaystack(fold.summary()))); err != nil {
+ t.Fatal(err)
+ }
+ for _, stmt := range []string{
+ `INSERT INTO event_fts (event_fts) VALUES ('rebuild')`,
+ `INSERT INTO trace_fts (trace_fts) VALUES ('rebuild')`,
+ `UPDATE meta SET value = '15' WHERE key = 'derivation_version'`,
+ } {
+ if _, err := store.db.Exec(stmt); err != nil {
+ t.Fatal(err)
+ }
+ }
+ var oldMatches int
+ if err := store.db.QueryRow(`SELECT count(*) FROM event_fts WHERE event_fts MATCH '"uniquely retained legacy"'`).Scan(&oldMatches); err != nil || oldMatches != 0 {
+ t.Fatalf("prior search matches=%d err=%v", oldMatches, err)
+ }
+ store.close()
+ if removeLog {
+ if err := os.Remove(path); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ // An ordinary public read must rebuild v15 content, summaries and FTS.
+ got, ok, err := ShowTrace(path, traceID, TraceQuery{Limit: 100})
+ if err != nil || !ok || len(got.Events) != 1 {
+ t.Fatalf("upgraded ShowTrace ok=%v err=%v result=%+v", ok, err, got)
+ }
+ if got.Events[0].Content == nil || got.Events[0].Content.Text != answer || got.Trace.Title != answer || got.Trace.LocalMessageCount != 1 {
+ t.Fatalf("legacy browser content was not rebuilt: %+v", got)
+ }
+ messages := asymptoteobserve.TraceMessages(TraceBundleV1{Events: got.Events})
+ if len(messages) != 1 || messages[0].Role != "assistant" || messages[0].Text != answer {
+ t.Fatalf("legacy answer missing from chat: %+v", messages)
+ }
+ for _, level := range []string{"event", "trace"} {
+ search, err := SearchTraces(path, TraceQuery{EventQuery: EventQuery{Q: "uniquely retained legacy"}, ResultLevel: level, Limit: 10})
+ if err != nil || search.TotalMatched != 1 {
+ t.Fatalf("%s search = %+v err=%v", level, search, err)
+ }
+ }
+ store, err = openHistoryStore(false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer store.close()
+ if version := store.metaInt("derivation_version", 0); version != 16 {
+ t.Fatalf("derivation version=%d, want 16 (actual predecessor 15)", version)
+ }
+ var rowID int64
+ var count int
+ var recordID, eventID, storedLine string
+ if err := store.db.QueryRow(`SELECT count(*), id, record_id, event_id, hex(line) FROM events`).Scan(&count, &rowID, &recordID, &eventID, &storedLine); err != nil {
+ t.Fatal(err)
+ }
+ if count != 1 || rowID != originalRowID || recordID != originalRecordID || eventID != originalEventID || storedLine != originalLine {
+ t.Fatal("upgrade changed stored raw bytes or event identities")
+ }
+ if !removeLog {
+ raw, err := os.ReadFile(path)
+ if err != nil || strings.TrimSpace(string(raw)) != string(line) {
+ t.Fatalf("upgrade changed raw JSONL: err=%v raw=%q", err, raw)
+ }
+ }
+ if again, ok, err := ShowTrace(path, traceID, TraceQuery{Limit: 100}); err != nil || !ok || mustJSON(t, again) != mustJSON(t, got) {
+ t.Fatalf("second read changed projection: ok=%v err=%v", ok, err)
+ }
+ })
+ }
+ }
+}
diff --git a/cli/beacon/internal/endpoint/dashboard/history_store.go b/cli/beacon/internal/endpoint/dashboard/history_store.go
index da25a2ce..152becbd 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 = 15 // 15: completed assistant responses and plans retain their answer; 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 change
+ historyDerivationVersion = 16 // 16: retained browser role/text messages are readable; 15: completed assistant responses and plans retain their answer; 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 change
defaultHistoryRetentionDays = 90
// maxHistoryRetentionDays is 100 years: a retention this long or longer means nothing expires.
diff --git a/pkg/asymptoteobserve/content.go b/pkg/asymptoteobserve/content.go
index 5ac40147..2ecd9dae 100644
--- a/pkg/asymptoteobserve/content.go
+++ b/pkg/asymptoteobserve/content.go
@@ -59,6 +59,8 @@ func SystemInstructionParts(text string) []interface{} {
// - a message whose body is "content" rather than "parts", as a string or a
// block list, and a part whose text is under "text" rather than "content",
// the shapes several session mappers and providers write;
+// - a legacy browser message {"role", "text"}, only when both "parts" and
+// "content" are absent; this body is text, never reasoning;
// - a {"messages": [...]} wrapper;
// - a bare string, as the whole value, a message body or a list element,
// which is text and never reasoning;
@@ -112,7 +114,13 @@ func collectGenAIText(v interface{}, role, partType string, out *[]string) {
return
}
if !hasParts {
- body = typed["content"]
+ var hasContent bool
+ body, hasContent = typed["content"]
+ if !hasContent && hasRole && (kind == "" || kind == "message") {
+ // Older browser captures wrote their internal role/text message
+ // directly. Never override an explicit (even empty) typed body.
+ body, _ = typed["text"].(string)
+ }
}
collectGenAIText(body, role, partType, out)
case nil, bool, float64, float32, int, int64, int32, json.Number:
diff --git a/pkg/asymptoteobserve/content_legacy_browser_test.go b/pkg/asymptoteobserve/content_legacy_browser_test.go
new file mode 100644
index 00000000..6e913034
--- /dev/null
+++ b/pkg/asymptoteobserve/content_legacy_browser_test.go
@@ -0,0 +1,55 @@
+package asymptoteobserve
+
+import (
+ "encoding/json"
+ "reflect"
+ "testing"
+)
+
+func TestGenAITextLegacyBrowserMessages(t *testing.T) {
+ cases := []struct {
+ name, input, role, partType string
+ want []string
+ }{
+ {"assistant", `[{"role":"assistant","text":" retained answer "}]`, RoleAssistant, GenAIPartTypeText, []string{"retained answer"}},
+ {"input roles", `[{"role":"system","text":"policy"},{"role":"user","text":"question"}]`, "", GenAIPartTypeText, []string{"policy", "question"}},
+ {"role filter", `[{"role":"user","text":"question"},{"role":"tool","text":"result"},{"role":"assistant","text":"answer"}]`, RoleAssistant, GenAIPartTypeText, []string{"answer"}},
+ {"not reasoning", `[{"role":"assistant","text":"answer"}]`, RoleAssistant, GenAIPartTypeReasoning, nil},
+ {"parts precedence", `[{"role":"assistant","text":"legacy","content":"content","parts":[{"type":"text","content":"parts"},{"type":"reasoning","content":"thought"}]}]`, RoleAssistant, GenAIPartTypeText, []string{"parts"}},
+ {"content precedence", `[{"role":"assistant","text":"legacy","content":"content"}]`, RoleAssistant, GenAIPartTypeText, []string{"content"}},
+ {"empty parts", `[{"role":"assistant","text":"must not resurrect","parts":[]}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"nil parts", `[{"role":"assistant","text":"must not resurrect","parts":null}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"nil parts override content", `[{"role":"assistant","text":"legacy","parts":null,"content":"content"}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"empty content", `[{"role":"assistant","text":"must not resurrect","content":""}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"nil content", `[{"role":"assistant","text":"must not resurrect","content":null}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"typed content", `[{"role":"assistant","text":"must not resurrect","content":17}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"reasoning only parts", `[{"role":"assistant","text":"must not resurrect","parts":[{"type":"reasoning","content":"thought"}]}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"reasoning request", `[{"role":"assistant","text":"legacy","parts":[{"type":"text","content":"answer"},{"type":"reasoning","content":"thought"}]}]`, RoleAssistant, GenAIPartTypeReasoning, []string{"thought"}},
+ {"part filters", `[{"type":"reasoning","text":"thought"},{"type":"tool_call","text":"args"},{"type":"text","text":"answer"}]`, RoleAssistant, GenAIPartTypeText, []string{"answer"}},
+ {"nonmessage typed map", `[{"role":"assistant","type":"reasoning","text":"thought"}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"no role", `[{"text":"not a legacy browser message"}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"empty text", `[{"role":"assistant","text":" "},{"role":"assistant"}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"nonstring text", `[{"role":"assistant","text":{"content":"not text"}}]`, RoleAssistant, GenAIPartTypeText, nil},
+ {"wrapped legacy", `{"messages":[{"role":"assistant","text":"[email] retained prefix"}]}`, RoleAssistant, GenAIPartTypeText, []string{"[email] retained prefix"}},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ var value interface{}
+ if err := json.Unmarshal([]byte(tc.input), &value); err != nil {
+ t.Fatal(err)
+ }
+ if got := GenAIText(value, tc.role, tc.partType); !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("GenAIText = %q, want %q", got, tc.want)
+ }
+ })
+ }
+ t.Run("typed legacy", func(t *testing.T) {
+ value := []struct {
+ Role string `json:"role"`
+ Text string `json:"text"`
+ }{{RoleAssistant, "typed answer"}}
+ if got := GenAIText(value, RoleAssistant, GenAIPartTypeText); !reflect.DeepEqual(got, []string{"typed answer"}) {
+ t.Fatalf("typed legacy text = %q", got)
+ }
+ })
+}