The Open Framework for Building Agents
fix(eve): preserve large GenAI system instructions (#4094)
Chad Hietalaf442363a7564
diff --git a/.changeset/truncate-genai-system-instructions.md b/.changeset/truncate-genai-system-instructions.mdnew file mode 100644index 000000000..0c1a68b3d--- /dev/null+++ b/.changeset/truncate-genai-system-instructions.md@@ -0,0 +1,5 @@+---+"eve": patch+---++System instructions now remain available in traces when JSON encoding would otherwise push them over the span attribute size limit.diff --git a/packages/eve/src/cli/dev/tui/traces/trace-conversation.ts b/packages/eve/src/cli/dev/tui/traces/trace-conversation.tsindex 1a5eadd0c..128abe1be 100644--- a/packages/eve/src/cli/dev/tui/traces/trace-conversation.ts+++ b/packages/eve/src/cli/dev/tui/traces/trace-conversation.ts@@ -70,20 +70,23 @@ export function buildConversationItems(trace: LocalTrace): ConversationItem[] {if (turnId !== undefined && subagent !== undefined) subagents.set(turnId, subagent);}const entries: { readonly item: ConversationItem; readonly order: bigint }[] = [];- const firstSystemSpan = [...trace.spans]- .sort(compareLocalTraceSpans)- .find((span) => isModelSpan(span) && systemInstructionsText(span.attributes) !== undefined);- const systemText =- firstSystemSpan === undefined ? undefined : systemInstructionsText(firstSystemSpan.attributes);- if (firstSystemSpan !== undefined && typeof systemText === "string" && systemText.length > 0) {+ let system: { readonly span: LocalTraceSpan; readonly text: string } | undefined;+ for (const span of [...trace.spans].sort(compareLocalTraceSpans)) {+ const text = isModelSpan(span) ? systemInstructionsText(span.attributes) : undefined;+ if (text !== undefined) {+ system = { span, text };+ break;+ }+ }+ if (system !== undefined) {entries.push({item: {kind: "system",durationMs: 0,error: false,- span: firstSystemSpan,- subagent: subagentFor(firstSystemSpan, subagents, byId),- text: systemText,+ span: system.span,+ subagent: subagentFor(system.span, subagents, byId),+ text: system.text,},order: 0n,});diff --git a/packages/eve/src/tracing/agent-otel-content.test.ts b/packages/eve/src/tracing/agent-otel-content.test.tsindex 966190808..93f2ab53a 100644--- a/packages/eve/src/tracing/agent-otel-content.test.ts+++ b/packages/eve/src/tracing/agent-otel-content.test.ts@@ -151,6 +151,48 @@ describe("GenAI message attributes", () => {'[{"finish_reason":"tool_call","parts":[{"content":"Working.","type":"text"},{"arguments":{"message":"echo"},"id":"call-1","name":"delegate","type":"tool_call"}],"role":"assistant"}]',);});++ it.each([+ ["near the limit", "line of instructions\n".repeat(1500)],+ ["over the limit", "x".repeat(CONTENT_ATTRIBUTE_LIMIT * 2)],+ ["escape-heavy", "\\\n".repeat(9000)],+ ])("keeps %s system instructions as valid capped JSON", (_, instructions) => {+ const attribute = genAiSystemInstructionsAttribute(instructions);++ expect(attribute).toBeDefined();+ expect(attribute!.length).toBeLessThanOrEqual(CONTENT_ATTRIBUTE_LIMIT);+ const parsed = JSON.parse(attribute!) as Array<Record<string, unknown>>;+ expect(parsed).toHaveLength(1);+ expect(parsed[0]).toMatchObject({ type: "text" });+ expect(parsed[0]?.content).toEqual(expect.stringContaining("… [truncated]"));+ });++ it("marks a message truncated when oversized non-text parts are omitted", () => {+ const attribute = genAiInputMessagesAttribute([+ {+ content: [+ { text: "partial assistant answer", type: "text" },+ {+ input: { payload: "x".repeat(CONTENT_ATTRIBUTE_LIMIT) },+ toolCallId: "call-1",+ toolName: "delegate",+ type: "tool-call",+ },+ ],+ role: "assistant",+ },+ ]);++ expect(attribute).toBeDefined();+ expect(attribute!.length).toBeLessThanOrEqual(CONTENT_ATTRIBUTE_LIMIT);+ const parsed = JSON.parse(attribute!) as Array<Record<string, unknown>>;+ expect(parsed).toEqual([+ {+ parts: [{ content: "partial assistant answer… [truncated]", type: "text" }],+ role: "assistant",+ },+ ]);+ });});describe("toolResultsContentAttribute", () => {diff --git a/packages/eve/src/tracing/agent-otel-content.ts b/packages/eve/src/tracing/agent-otel-content.tsindex 505ee15cb..341bab5b7 100644--- a/packages/eve/src/tracing/agent-otel-content.ts+++ b/packages/eve/src/tracing/agent-otel-content.ts@@ -48,8 +48,9 @@ export function genAiInputMessagesAttribute(messages: unknown): string | undefin/** Serializes the system prompt using the OpenTelemetry GenAI instruction schema. */export function genAiSystemInstructionsAttribute(instructions: unknown): string | undefined {- const text = systemPromptAttribute(instructions);- return text === undefined ? undefined : semanticJsonAttribute([{ content: text, type: "text" }]);+ const text = systemPromptText(instructions);+ if (text === undefined) return undefined;+ return fitSemanticText(text, (content) => [{ content, type: "text" }]);}/** Serializes one model response using the OpenTelemetry GenAI message schema. */@@ -103,8 +104,8 @@ function cappedToolResult(entry: Record<string, unknown>, cap: number): Record<s}/** Normalizes the AI SDK's `instructions` prompt to plain text. */-export function systemPromptAttribute(instructions: unknown): string | undefined {- if (typeof instructions === "string") return textContentAttribute(instructions);+function systemPromptText(instructions: unknown): string | undefined {+ if (typeof instructions === "string") return instructions.length === 0 ? undefined : instructions;if (!isRecord(instructions) && !Array.isArray(instructions)) return undefined;const messages = Array.isArray(instructions) ? instructions : [instructions];const texts: string[] = [];@@ -117,7 +118,7 @@ export function systemPromptAttribute(instructions: unknown): string | undefinedtexts.push(part.text);}const joined = texts.join("\n\n").trim();- return joined.length === 0 ? undefined : textContentAttribute(joined);+ return joined.length === 0 ? undefined : joined;}/** Caps plain text, marking the cut. */@@ -151,16 +152,27 @@ function truncateSingleSemanticMessage(typeof message.kind === "string"? { kind: message.kind, role: message.role }: { role: message.role };+ return fitSemanticText(+ text,+ (content) => [{ ...base, parts: [{ content, type: "text" }] }],+ true,+ );+}++function fitSemanticText(+ text: string,+ wrap: (content: string) => unknown,+ alwaysMark = false,+): string | undefined {+ if (!alwaysMark && text.length <= CONTENT_ATTRIBUTE_LIMIT) {+ const full = semanticJsonAttribute(wrap(text));+ if (full !== undefined) return full;+ }for (let length = Math.min(text.length, CONTENT_ATTRIBUTE_LIMIT); length > 0; length -= 256) {- const json = semanticJsonAttribute([- {- ...base,- parts: [{ content: `${text.slice(0, length)}… [truncated]`, type: "text" }],- },- ]);+ const json = semanticJsonAttribute(wrap(`${text.slice(0, length)}… [truncated]`));if (json !== undefined) return json;}- return semanticJsonAttribute([{ ...base, parts: [{ content: "… [truncated]", type: "text" }] }]);+ return semanticJsonAttribute(wrap("… [truncated]"));}function semanticMessageText(parts: unknown): string {