@sema-agent/core 1.440.0 → 1.442.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);
@@ -1252,7 +1252,7 @@ export class TaskRegistry {
1252
1252
  return settled;
1253
1253
  }
1254
1254
  reapSessionBackground(sessionId, scope) {
1255
- const access = { owner: sessionId, ...(scope !== undefined ? { scope } : {}) };
1255
+ const access = { owner: sessionId, sessionId, ...(scope !== undefined ? { scope } : {}) };
1256
1256
  let reaped = 0;
1257
1257
  for (const handle of this.handles.values()) {
1258
1258
  if ((handle.type === "background_bash" || handle.type === "monitor") && handle.retained === true && handle.status === "running")
@@ -1289,6 +1289,11 @@ export class TaskRegistry {
1289
1289
  reaped++;
1290
1290
  continue;
1291
1291
  }
1292
+ if (handle.type === "workflow" && handle.status === "running" && handle.sessionScoped === true && canAccess(handle, access)) {
1293
+ void this.stopWorkflow(handle);
1294
+ reaped++;
1295
+ continue;
1296
+ }
1292
1297
  if (handle.type !== "background_agent" || handle.status !== "running" || !handle.sessionScoped)
1293
1298
  continue;
1294
1299
  if (!canAccess(handle, access))
@@ -1313,11 +1318,14 @@ export class TaskRegistry {
1313
1318
  for (const handle of this.handles.values()) {
1314
1319
  if (handle.status !== "running")
1315
1320
  continue;
1316
- if (!handle.sessionScoped || handle.owner === undefined)
1321
+ if (!handle.sessionScoped)
1322
+ continue;
1323
+ const sessionKey = handle.type === "workflow" ? handle.originatingSessionId : handle.owner;
1324
+ if (sessionKey === undefined)
1317
1325
  continue;
1318
- const key = JSON.stringify([handle.owner, handle.scope ?? null]);
1326
+ const key = JSON.stringify([sessionKey, handle.scope ?? null]);
1319
1327
  if (!pairs.has(key))
1320
- pairs.set(key, { owner: handle.owner, ...(handle.scope !== undefined ? { scope: handle.scope } : {}) });
1328
+ pairs.set(key, { owner: sessionKey, ...(handle.scope !== undefined ? { scope: handle.scope } : {}) });
1321
1329
  }
1322
1330
  let reaped = 0;
1323
1331
  for (const p of pairs.values())
@@ -1737,6 +1745,7 @@ export class TaskRegistry {
1737
1745
  if (this.handles.has(id))
1738
1746
  throw new Error(`TaskRegistry: task id already registered: ${id}`);
1739
1747
  const now = input.now ?? Date.now();
1748
+ const originatingSessionId = input.originatingSessionId;
1740
1749
  const handle = {
1741
1750
  id,
1742
1751
  type: "workflow",
@@ -1750,7 +1759,7 @@ export class TaskRegistry {
1750
1759
  runId: id,
1751
1760
  handle: input.handle,
1752
1761
  ...(input.store !== undefined ? { store: input.store } : {}),
1753
- ...(input.originatingSessionId !== undefined ? { originatingSessionId: input.originatingSessionId } : {}),
1762
+ ...(originatingSessionId !== undefined ? { originatingSessionId, sessionScoped: true } : {}),
1754
1763
  ...(input.onServedTerminal !== undefined ? { onServedTerminal: input.onServedTerminal } : {}),
1755
1764
  };
1756
1765
  this.handles.set(id, handle);
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sema-agent/core",
3
- "version": "1.440.0",
3
+ "version": "1.442.0",
4
4
  "description": "Stateless, task-oriented AI agent core",
5
5
  "type": "module",
6
6
  "license": "BUSL-1.1",