@sema-agent/core 1.441.0 → 1.443.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1185,13 +1185,27 @@ export class Runner {
1185
1185
  : { kind: "vision.placeholder", version: 1, taskId, count: t.count, ts: Date.now() });
1186
1186
  };
1187
1187
  const withBrainSinks = (fn) => runWithStatusSink(statusEmit, () => runWithBrainTelemetry(telemetryEmit, fn));
1188
- const trunc = (s) => s.slice(0, MAX_TOOL_END_OUTPUT_CHARS) + `…[+${s.length - MAX_TOOL_END_OUTPUT_CHARS} chars truncated]`;
1188
+ const safeJsonLength = (v) => {
1189
+ try {
1190
+ return JSON.stringify(v)?.length;
1191
+ }
1192
+ catch {
1193
+ return undefined;
1194
+ }
1195
+ };
1196
+ const trunc = (s) => ({
1197
+ text: s.slice(0, MAX_TOOL_END_OUTPUT_CHARS) + `…[+${s.length - MAX_TOOL_END_OUTPUT_CHARS} chars truncated]`,
1198
+ totalChars: s.length,
1199
+ });
1189
1200
  const toolOutputFrom = (result) => {
1190
1201
  const content = result !== null && typeof result === "object" ? result.content : result;
1191
1202
  if (content === undefined)
1192
1203
  return undefined;
1193
1204
  if (typeof content === "string") {
1194
- return content.length > MAX_TOOL_END_OUTPUT_CHARS ? { output: trunc(content), truncated: true } : { output: content, truncated: false };
1205
+ if (content.length <= MAX_TOOL_END_OUTPUT_CHARS)
1206
+ return { output: content, truncated: false };
1207
+ const t = trunc(content);
1208
+ return { output: t.text, truncated: true, totalChars: t.totalChars };
1195
1209
  }
1196
1210
  let serialized;
1197
1211
  try {
@@ -1200,7 +1214,33 @@ export class Runner {
1200
1214
  catch {
1201
1215
  return { output: "[unserializable tool output]", truncated: false };
1202
1216
  }
1203
- return serialized.length > MAX_TOOL_END_OUTPUT_CHARS ? { output: trunc(serialized), truncated: true } : { output: content, truncated: false };
1217
+ if (serialized.length <= MAX_TOOL_END_OUTPUT_CHARS)
1218
+ return { output: content, truncated: false };
1219
+ let anyNonText = false;
1220
+ let trueChars = 0;
1221
+ const raw = Array.isArray(content)
1222
+ ? content
1223
+ .map((b) => {
1224
+ const block = b;
1225
+ if (block?.type === "text" && typeof block.text === "string") {
1226
+ trueChars += block.text.length;
1227
+ return block.text;
1228
+ }
1229
+ anyNonText = true;
1230
+ trueChars += typeof block?.data === "string" ? block.data.length : (safeJsonLength(block) ?? 0);
1231
+ return `[${typeof block?.type === "string" ? block.type : "block"}${typeof block?.mimeType === "string" ? `: ${block.mimeType}` : ""}]`;
1232
+ })
1233
+ .join("\n")
1234
+ : serialized;
1235
+ const totalChars = Array.isArray(content) ? trueChars : serialized.length;
1236
+ if (raw.length > MAX_TOOL_END_OUTPUT_CHARS) {
1237
+ const t = trunc(raw);
1238
+ return { output: t.text, truncated: true, totalChars };
1239
+ }
1240
+ if (!anyNonText) {
1241
+ return { output: raw, truncated: false };
1242
+ }
1243
+ return { output: raw, truncated: true, totalChars };
1204
1244
  };
1205
1245
  const CC_DETAIL_TYPES = new Set([
1206
1246
  "edit", "multiedit", "create", "update", "bash", "notebook-edit", "text", "grep", "glob", "mcp",
@@ -1490,7 +1530,9 @@ export class Runner {
1490
1530
  isError: event.isError,
1491
1531
  ...(() => {
1492
1532
  const o = toolOutputFrom(event.result);
1493
- return o !== undefined ? { output: o.output, ...(o.truncated ? { truncated: true } : {}) } : {};
1533
+ return o !== undefined
1534
+ ? { output: o.output, ...(o.truncated ? { truncated: true } : {}), ...(o.totalChars !== undefined ? { totalChars: o.totalChars } : {}) }
1535
+ : {};
1494
1536
  })(),
1495
1537
  ...(() => {
1496
1538
  const st = structuredFrom(event.result);
@@ -498,6 +498,7 @@ export type TaskEvent = ({
498
498
  output?: unknown;
499
499
  structured?: unknown;
500
500
  truncated?: boolean;
501
+ totalChars?: number;
501
502
  } & TaskEventIdentity) | ({
502
503
  type: "context_usage";
503
504
  usedTokens: number;
@@ -2,7 +2,7 @@ import { type AgentCoreStreamRuntimeDeps } from "./runtime-deps.js";
2
2
  import type { AgentContext, AgentEvent, AgentLoopConfig, AgentMessage, StreamFn } from "./types.js";
3
3
  export type AgentEventSink = (event: AgentEvent) => Promise<void> | void;
4
4
  export type LoopContinueReason = "next_turn" | "steer_injected" | "follow_up_injected" | "reactive_compact_retry" | "max_output_tokens_recovery" | "malformed_tool_use_retry" | "thinking_only_retry" | "midstream_partial_recovery" | "degenerate_output_recovery" | "walltime_cutoff_recovery";
5
- export type LoopTerminalReason = "completed" | "aborted_before_stream" | "assistant_error" | "stop_requested";
5
+ export type LoopTerminalReason = "completed" | "aborted_before_stream" | "assistant_error" | "stop_requested" | "malformed_tool_use_exhausted";
6
6
  export type LoopStep = {
7
7
  kind: "continue";
8
8
  reason: LoopContinueReason;
@@ -402,14 +402,14 @@ async function runSingleTurn(state, signal, emit, streamFn, runtime, trace) {
402
402
  state.midstreamPartialContinues = 0;
403
403
  }
404
404
  const malformed = state.config.recovery?.malformedToolUse;
405
- if (malformed &&
406
- message.stopReason === "toolUse" &&
407
- toolCalls.length === 0 &&
408
- state.pendingMessages.length === 0 &&
409
- state.malformedToolUseRetries < (malformed.maxRetries ?? 1)) {
410
- state.malformedToolUseRetries++;
411
- state.pendingMessages = [createMalformedToolUseNudge()];
412
- return { kind: "ran", recovered: "malformed_tool_use_retry" };
405
+ if (malformed && message.stopReason === "toolUse" && toolCalls.length === 0 && state.pendingMessages.length === 0) {
406
+ if (state.malformedToolUseRetries < (malformed.maxRetries ?? 1)) {
407
+ state.malformedToolUseRetries++;
408
+ state.pendingMessages = [createMalformedToolUseNudge()];
409
+ return { kind: "ran", recovered: "malformed_tool_use_retry" };
410
+ }
411
+ await emit({ type: "agent_end", messages: state.newMessages });
412
+ return { kind: "terminal", reason: "malformed_tool_use_exhausted" };
413
413
  }
414
414
  const thinkingOnly = state.config.recovery?.thinkingOnly;
415
415
  if (thinkingOnly &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sema-agent/core",
3
- "version": "1.441.0",
3
+ "version": "1.443.0",
4
4
  "description": "Stateless, task-oriented AI agent core",
5
5
  "type": "module",
6
6
  "license": "BUSL-1.1",