@sema-agent/core 1.439.0 → 1.441.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.
@@ -1,7 +1,12 @@
1
1
  import { DEFAULT_CHARS_PER_TOKEN, estimateContextTokens, estimateTokens } from "../internal/harness.js";
2
2
  import { isToolResult } from "./message-utils.js";
3
3
  const CLEARED_MARKER = "[tool result cleared to save context]";
4
- const clearedWithRef = (ref) => `[tool result cleared to save context — full content persisted; call ReadToolResult with ref "${ref}" to read it back]`;
4
+ const refNote = (ref) => `full text persisted; call ReadToolResult with ref "${ref}" to read it back`;
5
+ const mediaNote = (n) => `${n} attachment${n === 1 ? "" : "s"} no longer visible after this clear`;
6
+ const clearedMarker = (notes) => {
7
+ const present = notes.filter((n) => n !== undefined);
8
+ return present.length > 0 ? `[tool result cleared to save context — ${present.join("; ")}]` : CLEARED_MARKER;
9
+ };
5
10
  export const EDIT_FRACTION = 0.7;
6
11
  export const CONTEXT_OUTPUT_RESERVE_TOKENS = 20000;
7
12
  export const COMPACTION_TRIGGER_BUFFER_TOKENS = 13000;
@@ -69,24 +74,25 @@ export function clearStaleToolResults(messages, opts) {
69
74
  break;
70
75
  }
71
76
  const before = estimateTokens(out[idx], cpt);
72
- let marker = CLEARED_MARKER;
77
+ const msg = out[idx];
78
+ const rawContent = Array.isArray(msg.content) ? msg.content : [];
79
+ let ref;
73
80
  if (opts.offload) {
74
- const msg = out[idx];
75
- const fullText = Array.isArray(msg.content)
76
- ? msg.content
77
- .filter((c) => c?.type === "text" && typeof c.text === "string")
78
- .map((c) => c.text)
79
- .join("\n")
80
- : "";
81
+ const fullText = rawContent
82
+ .filter((c) => c?.type === "text" && typeof c.text === "string")
83
+ .map((c) => c.text)
84
+ .join("\n");
81
85
  if (msg.toolCallId && fullText.trim().length > 0) {
82
86
  try {
83
- marker = clearedWithRef(opts.offload.persist(msg.toolCallId, fullText));
87
+ ref = refNote(opts.offload.persist(msg.toolCallId, fullText));
84
88
  }
85
89
  catch {
86
- marker = CLEARED_MARKER;
90
+ ref = undefined;
87
91
  }
88
92
  }
89
93
  }
94
+ const mediaCount = rawContent.filter((c) => c?.type !== "text").length;
95
+ const marker = clearedMarker([ref, mediaCount > 0 ? mediaNote(mediaCount) : undefined]);
90
96
  const cleared = {
91
97
  ...out[idx],
92
98
  content: [{ type: "text", text: marker }],
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sema-agent/core",
3
- "version": "1.439.0",
3
+ "version": "1.441.0",
4
4
  "description": "Stateless, task-oriented AI agent core",
5
5
  "type": "module",
6
6
  "license": "BUSL-1.1",