@crewhaus/streaming-tool-executor 0.2.0 → 0.2.1

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.
package/dist/index.d.ts CHANGED
@@ -13,12 +13,17 @@ import type Anthropic from "@anthropic-ai/sdk";
13
13
  * The dispatch logic is unchanged:
14
14
  *
15
15
  * - No tools currently running → run.
16
- * - Currently-running set is all concurrency-safe AND this entry is
17
- * concurrency-safe → run.
16
+ * - Fewer than `maxConcurrent` in flight AND the running set is all
17
+ * concurrency-safe AND this entry is concurrency-safe → run.
18
18
  * - Otherwise → wait for the running set to drain.
19
19
  *
20
- * Concurrency-safety follows the same triple-conjunction as
21
- * `tool-orchestrator`: `concurrencySafe && readOnly && !destructive`.
20
+ * Concurrency-safety follows the same rule as `tool-orchestrator`: the
21
+ * static triple-conjunction `concurrencySafe && readOnly && !destructive`,
22
+ * UNLESS the tool ships a per-call `concurrencyClassifier` (e.g. `Task`,
23
+ * whose safety depends on which sub-agent a specific call spawns), in
24
+ * which case that decides per invocation. `maxConcurrent` bounds the
25
+ * in-flight set so a wide sub-agent fan-out can't open unbounded model
26
+ * connections.
22
27
  *
23
28
  * Sibling abort: a per-call `AbortController` (`siblingAbortController`)
24
29
  * is signalled when a tool result errors AND `shouldAbortOnError` says
@@ -74,6 +79,12 @@ export type StreamingExecuteOptions = {
74
79
  * persistence).
75
80
  */
76
81
  readonly runTool?: (block: CompletedToolUseBlock) => Promise<Anthropic.ToolResultBlockParam>;
82
+ /**
83
+ * Max concurrency-safe tool calls in flight at once (chiefly parallel
84
+ * sub-agent spawns). Defaults to `DEFAULT_MAX_CONCURRENT` (4). `<= 0` is
85
+ * clamped to 1 (serial).
86
+ */
87
+ readonly maxConcurrent?: number;
77
88
  };
78
89
  export type StreamingExecuteResult = {
79
90
  readonly finalContent: ReadonlyArray<CanonicalContentBlock>;
package/dist/index.js CHANGED
@@ -1,10 +1,30 @@
1
1
  import { RuntimeError } from "@crewhaus/errors";
2
2
  import { executeTool } from "@crewhaus/tool-executor";
3
+ // Mirror of runtime-core's DEFAULT_MAX_CONCURRENT_TOOLS — bound the
4
+ // in-flight set so a wide sub-agent fan-out can't open unbounded model
5
+ // connections. Kept in sync intentionally; both default to 4.
6
+ const DEFAULT_MAX_CONCURRENT = 4;
3
7
  function defaultShouldAbortOnError(_failedToolName, tool) {
4
8
  return tool?.destructive === true;
5
9
  }
6
- function isConcurrencySafe(tool) {
7
- return tool?.concurrencySafe === true && tool.readOnly && !tool.destructive;
10
+ /**
11
+ * Per-call concurrency safety. A tool's static flags decide it, unless the
12
+ * tool ships a `concurrencyClassifier` (Task), which decides per call from
13
+ * the invocation input and the sibling catalog. Fail-closed: a missing
14
+ * tool, a throw, or a `false` return all mean "not parallel-safe".
15
+ */
16
+ function isCallSafe(block, tool, catalog) {
17
+ if (tool === undefined)
18
+ return false;
19
+ if (tool.concurrencyClassifier !== undefined) {
20
+ try {
21
+ return tool.concurrencyClassifier(block.input, catalog);
22
+ }
23
+ catch {
24
+ return false;
25
+ }
26
+ }
27
+ return tool.concurrencySafe === true && tool.readOnly && !tool.destructive;
8
28
  }
9
29
  /**
10
30
  * Drive an `AsyncIterable<StreamEvent>` and dispatch `tool_use` blocks
@@ -20,6 +40,10 @@ export async function executeStreaming(stream, opts) {
20
40
  const inFlight = new Set();
21
41
  const siblingAbort = new AbortController();
22
42
  const shouldAbortOnError = opts.shouldAbortOnError ?? defaultShouldAbortOnError;
43
+ // Sibling catalog for per-call concurrency classifiers (Task resolves the
44
+ // child tool set from it). Snapshotted once — the catalog is fixed per run.
45
+ const catalog = [...opts.toolByName.values()];
46
+ const maxConcurrent = Math.max(1, opts.maxConcurrent ?? DEFAULT_MAX_CONCURRENT);
23
47
  const blocks = new Map();
24
48
  // Order content blocks were OPENED in — tool_use ordering must
25
49
  // follow open-order for downstream pairing to work.
@@ -143,11 +167,14 @@ export async function executeStreaming(stream, opts) {
143
167
  dispatch(entry);
144
168
  continue;
145
169
  }
146
- // With work already in flight, only dispatch this entry when the
147
- // running set is fully concurrency-safe AND this entry is too;
148
- // otherwise stop scanning and wait for the running set to drain.
149
- const runningAllSafe = queue.every((e) => e.status !== "running" || isConcurrencySafe(e.tool));
150
- const canRunConcurrently = runningAllSafe && isConcurrencySafe(entry.tool);
170
+ // With work already in flight, only dispatch this entry when we're
171
+ // under the concurrency cap AND the running set is fully
172
+ // concurrency-safe AND this entry is too; otherwise stop scanning and
173
+ // wait for the running set to drain.
174
+ if (inFlight.size >= maxConcurrent)
175
+ return;
176
+ const runningAllSafe = queue.every((e) => e.status !== "running" || e.safe);
177
+ const canRunConcurrently = runningAllSafe && entry.safe;
151
178
  if (!canRunConcurrently)
152
179
  return;
153
180
  dispatch(entry);
@@ -226,9 +253,11 @@ export async function executeStreaming(stream, opts) {
226
253
  // Persist the parsed input on the block so finalContent
227
254
  // reflects what we passed to runTool.
228
255
  block.jsonBuffer = JSON.stringify(input);
256
+ const enqueuedTool = opts.toolByName.get(completed.name);
229
257
  queue.push({
230
258
  block: completed,
231
- tool: opts.toolByName.get(completed.name),
259
+ tool: enqueuedTool,
260
+ safe: isCallSafe(completed, enqueuedTool, catalog),
232
261
  status: "queued",
233
262
  });
234
263
  processQueue();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/streaming-tool-executor",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Dispatch tool execution mid-stream so tools start before message_stop",
6
6
  "main": "dist/index.js",
@@ -16,12 +16,12 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@anthropic-ai/sdk": "^0.96.0",
19
- "@crewhaus/adapter-anthropic": "0.2.0",
20
- "@crewhaus/errors": "0.2.0",
21
- "@crewhaus/tool-builder": "0.2.0",
22
- "@crewhaus/tool-catalog": "0.2.0",
23
- "@crewhaus/tool-executor": "0.2.0",
24
- "@crewhaus/turn-state-machine": "0.2.0",
19
+ "@crewhaus/adapter-anthropic": "0.2.1",
20
+ "@crewhaus/errors": "0.2.1",
21
+ "@crewhaus/tool-builder": "0.2.1",
22
+ "@crewhaus/tool-catalog": "0.2.1",
23
+ "@crewhaus/tool-executor": "0.2.1",
24
+ "@crewhaus/turn-state-machine": "0.2.1",
25
25
  "zod": "^3.23.8"
26
26
  },
27
27
  "license": "Apache-2.0",