Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(context-engine): guard placeholder log preview against undefined content (#15097)

Arvin Xu97111fc99da1
diff --git a/packages/context-engine/src/processors/PlaceholderVariables.ts b/packages/context-engine/src/processors/PlaceholderVariables.ts
index e3d09300af8..ca3eb28add8 100644
--- a/packages/context-engine/src/processors/PlaceholderVariables.ts
+++ b/packages/context-engine/src/processors/PlaceholderVariables.ts
@@ -11,6 +11,26 @@ declare module '../types' {
const log = debug('context-engine:processor:PlaceholderVariablesProcessor');
+/**
+ * Build a short, log-safe preview of a message's content.
+ *
+ * `JSON.stringify` returns `undefined` (not a string) for `undefined` /
+ * functions / symbols, so naively calling `.slice` on its result crashes β€”
+ * this is exactly how a tool error result with `content: undefined`
+ * (e.g. budget-exceeded errors) used to take down the whole processor.
+ * Always coerce to a string before slicing.
+ */
+const buildContentPreview = (content: unknown): string => {
+ if (typeof content === 'string') return content.slice(0, 200);
+
+ try {
+ const serialized = JSON.stringify(content);
+ return (serialized ?? String(content)).slice(0, 200);
+ } catch {
+ return String(content).slice(0, 200);
+ }
+};
+
const PLACEHOLDER_START = '{{';
const PLACEHOLDER_END = '}}';
@@ -320,10 +340,7 @@ export class PlaceholderVariablesProcessor extends BaseProcessor {
for (let i = 0; i < clonedContext.messages.length; i++) {
const message = clonedContext.messages[i];
- const contentPreview =
- typeof message.content === 'string'
- ? message.content.slice(0, 200)
- : JSON.stringify(message.content).slice(0, 200);
+ const contentPreview = buildContentPreview(message.content);
log(
'Processing message %d: role=%s, contentType=%s, contentPreview=%s',
diff --git a/packages/context-engine/src/processors/__tests__/PlaceholderVariables.toolMessage.test.ts b/packages/context-engine/src/processors/__tests__/PlaceholderVariables.toolMessage.test.ts
index d10b5dcf7c6..06a0ffd1057 100644
--- a/packages/context-engine/src/processors/__tests__/PlaceholderVariables.toolMessage.test.ts
+++ b/packages/context-engine/src/processors/__tests__/PlaceholderVariables.toolMessage.test.ts
@@ -79,6 +79,37 @@ describe('PlaceholderVariablesProcessor β€” tool message substitution', () => {
expect(result.messages[0].content).toBe('agent={{agent_id}}');
});
+ // Regression for LOBE-9408: a tool error result (e.g. budget-exceeded) can
+ // arrive with `content: undefined`. The content-preview logging step used to
+ // call `JSON.stringify(undefined).slice(...)` β€” which throws because
+ // `JSON.stringify(undefined)` returns `undefined`, not a string β€” crashing
+ // the whole processor before any message was processed.
+ it('does not crash on a tool message with undefined content', async () => {
+ const processor = new PlaceholderVariablesProcessor({
+ variableGenerators: {
+ agent_id: () => 'agt_xyz',
+ },
+ });
+
+ const ctx = buildContext([
+ { role: 'user', content: 'hi {{agent_id}}' },
+ {
+ role: 'tool',
+ tool_call_id: 'toolu_err',
+ name: 'lobe-agent',
+ content: undefined,
+ error: { errorType: 'InsufficientBudgetForModel' },
+ },
+ ]);
+
+ const result = await processor.process(ctx);
+
+ // The user message after the crashing tool message must still be processed.
+ expect(result.messages[0].content).toBe('hi agt_xyz');
+ // The tool message is preserved untouched.
+ expect(result.messages[1].content).toBeUndefined();
+ });
+
it('substitutes generator returning empty string to empty (NOT raw)', async () => {
const processor = new PlaceholderVariablesProcessor({
variableGenerators: {