@salesforce/sfdx-agent-harness-claude 0.11.0 → 0.12.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.
package/README.md CHANGED
@@ -68,22 +68,24 @@ mirror the gateway SDK's conservative limits so a file behaves identically here
68
68
 
69
69
  ### Tool approval
70
70
 
71
- When `StreamOptions.requireToolApproval: true` is set on `chatSession.send()`/`stream()`, the harness installs a
72
- `PreToolUse` hook that:
71
+ When `StreamOptions.requireToolApproval: true` is set on `chatSession.chat()`, the harness installs a `PreToolUse` hook
72
+ that:
73
73
 
74
- 1. Emits a `tool-approval-request` event on `ChatStreamResult.eventStream` carrying
74
+ 1. Emits a `tool-approval-request` event on the `chat()`-returned `eventStream` carrying
75
75
  `{ toolCall: { toolCallId, toolName, args } }`.
76
- 2. Suspends the stream (the `eventStream` ends after the request event) until the consumer calls
77
- `chatSession.approveToolCall(toolCallId)` or `chatSession.declineToolCall(toolCallId)`.
78
- 3. Returns a new `ChatStreamResult` whose stream carries the tool's outcome and the model's follow-up:
79
- - On **approve**: the resulting `tool-result`, then the model's follow-up `text-delta` events, then `finish`.
80
- - On **decline**: a synthetic `tool-result` with `isError: true`, then the model's acknowledgement `text-delta`
81
- events, then `finish`. The decline does **not** abort the assistant turn — the model receives the denial and can
82
- react. The decline message is hard-coded to `'User declined the tool call.'`; there is no consumer-supplied reason
83
- (matching the SDK's `declineToolCall` signature).
84
-
85
- Single-tool approve and decline, parallel batches (with per-tool sink routing and progressive disclosure), approval
86
- timeout enforcement, `abortSignal` propagation, and shutdown/`destroyThread` cleanup are supported today.
76
+ 2. Parks the SDK hook Promise until the consumer calls `chatSession.approveToolCall(toolCallId)` or
77
+ `chatSession.declineToolCall(toolCallId)`. Settle methods return `Promise<void>`.
78
+ 3. Continues delivering events on the **same** `eventStream` after the consumer settles — the resulting `tool-result`,
79
+ the model's follow-up `text-delta` events, and the terminal `finish` all arrive on that one stream. The consumer
80
+ keeps iterating until it sees `finish`.
81
+ - On **approve**: the SDK's `tool_use` runs and the `tool-result` lands on the stream.
82
+ - On **decline**: the SDK synthesizes a `tool_result` with `isError: true`, the model receives the denial and emits
83
+ an acknowledgement, then `finish`. The decline does **not** abort the assistant turn. The decline message is
84
+ hard-coded to `'User declined the tool call.'`.
85
+
86
+ This is the post-#529 single-stream-per-turn contract — see the SDK README's "Tool Approval Flow" for consumer-facing
87
+ patterns. Single-tool approve / decline, parallel batches (`requireToolApproval: 'batch'`), approval-timeout
88
+ enforcement, `abortSignal` propagation, and shutdown / `destroyThread` cleanup all share that contract.
87
89
 
88
90
  #### Abort signal
89
91
 
@@ -91,8 +93,7 @@ When `StreamOptions.abortSignal` fires during an active turn with `requireToolAp
91
93
 
92
94
  - All pending permission Promises reject with an `AbortError`.
93
95
  - The underlying Claude subprocess is killed (existing `AbortController` wiring).
94
- - Every open per-turn stream — the initial sink and any per-tool continuation sinks for already-approved tools whose
95
- `tool-result` hasn't arrived yet — ends with an `error` event (`code: 'aborted'`, `error.name === 'AbortError'`)
96
+ - The single per-turn `eventStream` ends with an `error` event (`code: 'aborted'`, `error.name === 'AbortError'`)
96
97
  followed by a `finish` event (`finishReason: 'error'`). The `'error'` finish reason is used because the SDK's
97
98
  `FinishReason` type has no `'aborted'` value; the preceding `error` event carries the abort identity.
98
99
 
@@ -101,12 +102,12 @@ If the signal is already aborted when `stream()` is called, `stream()` throws sy
101
102
 
102
103
  #### Shutdown and thread teardown
103
104
 
104
- Calling `harness.shutdown()` disposes every tracked broker (across all agents and threads). For each pending approval,
105
- the parked Promise rejects with an `AgentSDKError` whose `type` is `'DISPOSED'`; each open per-turn stream ends with a
106
- single `error` event (no `finish`) and the underlying Claude subprocess is killed. Calling
107
- `destroyThread(agentId, threadId)` performs the same cleanup but only for the broker associated with that
108
- `(agentId, threadId)` pair — brokers on other threads are untouched. Both entry points are idempotent (the harness's
109
- `shuttingDown` guard prevents double-dispose; the broker's `disposed` flag does the same).
105
+ Calling `harness.shutdown()` disposes every tracked coordinator (across all agents and threads). For each pending
106
+ approval, the parked Promise rejects with an `AgentSDKError` whose `type` is `'DISPOSED'`; the per-turn `eventStream`
107
+ ends with a single `error` event (no `finish`) and the underlying Claude subprocess is killed. Calling
108
+ `destroyThread(agentId, threadId)` performs the same cleanup but only for the coordinator associated with that
109
+ `(agentId, threadId)` pair — coordinators on other threads are untouched. Both entry points are idempotent (the
110
+ harness's `shuttingDown` guard prevents double-dispose; the coordinator's `disposed` flag does the same).
110
111
 
111
112
  #### Approval timeout
112
113
 
@@ -115,21 +116,18 @@ Each pending approval is auto-denied after `ClaudeHarnessFactoryConfig.toolAppro
115
116
 
116
117
  - The parked `PreToolUse` hook Promise resolves with `permissionDecision: 'deny'` plus `decision: 'block'` so the
117
118
  agentic loop terminates instead of letting the model retry the timed-out call.
118
- - An `error` event with `code: 'tool-approval-timeout'` is emitted on the consumer's currently-active per-turn stream,
119
+ - An `error` event with `code: 'tool-approval-timeout'` is emitted on the consumer's currently-iterating `eventStream`,
119
120
  then that stream ends.
120
- - Any subsequent `approveToolCall` / `declineToolCall` for the timed-out tool throws because the pending approval is no
121
- longer registered.
121
+ - Any subsequent `approveToolCall` / `declineToolCall` for the timed-out tool throws because the coordinator marks every
122
+ emitted approval as auto-resolved during teardown.
122
123
 
123
124
  Calling `approveToolCall` or `declineToolCall` before the timeout cancels the timer — no spurious error event surfaces
124
125
  afterwards.
125
126
 
126
- > **Consumer pattern:** to observe the timeout (or `abort` / `dispose`) error on the initial-turn `eventStream`, iterate
127
- > the stream to its natural end rather than `break`ing out after the `tool-approval-request` event. Calling
128
- > `iterator.return()` (which `break` does inside `for await`) finalizes the harness's source generator before the
129
- > terminal event can reach you. Iterate with side-effect
130
- > (`if (event.type === 'tool-approval-request') approval = event`) and let the loop exit when the stream ends. Consumers
131
- > that hand off to a continuation sink via `approveToolCall` / `declineToolCall` can use either pattern — terminal
132
- > events on the continuation flow normally.
127
+ > **Consumer pattern:** to observe the timeout (or `abort` / `dispose`) error on the `eventStream`, iterate the stream
128
+ > to its natural end rather than `break`ing out after the `tool-approval-request` event. Calling `iterator.return()`
129
+ > (which `break` does inside `for await`) finalizes the harness's source generator before the terminal event can reach
130
+ > you. Settle in-line and keep iterating; the loop exits naturally when the terminal `finish` arrives.
133
131
 
134
132
  ### Using the Salesforce Bedrock resolver
135
133
 
@@ -271,9 +269,11 @@ const agent = await manager.createAgent(projectRoot, {
271
269
  });
272
270
 
273
271
  // In your event loop, answer the consumer tool's `tool-call` via
274
- // `session.submitToolResult({ toolCallId, toolName, result })`. This pattern
275
- // composes with `requireToolApproval: true` (#516) — the approval broker
276
- // emits a `tool-approval-request` for the consumer tool first.
272
+ // `session.submitToolResult({ toolCallId, toolName, result })`. Consumer
273
+ // tools never reach the approval gate (the harness adds the consumer-tool
274
+ // wire prefix to `bypassApprovalTools` automatically — see #529 step 2b).
275
+ // They surface as a normal `tool-call` event with no preceding
276
+ // `tool-approval-request`, even when `requireToolApproval: true`.
277
277
  ```
278
278
 
279
279
  This is the recommended pattern when the consumer wants identical behavior across Mastra and Claude. For Claude-only
@@ -281,39 +281,47 @@ consumers who prefer to drive the built-in directly, see "Bypassing the approval
281
281
 
282
282
  ### Bypassing the approval gate for specific tools
283
283
 
284
- When `requireToolApproval: true` is set, every tool call reaches the broker by default — including built-ins like
285
- `AskUserQuestion` whose UX a consumer may want to drive directly off the `tool-call` event without a synthetic
286
- `tool-approval-request` round-trip. `ClaudeHarnessFactoryConfig.bypassApprovalTools` opts specific tool names out of the
287
- gate.
284
+ When `requireToolApproval: true` is set, every built-in / MCP tool call reaches the coordinator's `PreToolUse` hook by
285
+ default — including Claude built-ins like `AskUserQuestion`. (Consumer-declared tools from `AgentConfig.tools[i]`
286
+ already bypass automatically — see "Tool approval" above.) `ClaudeHarnessFactoryConfig.bypassApprovalTools` opts
287
+ additional tool names out of the gate.
288
288
 
289
289
  ```ts
290
290
  const harnessFactory = new ClaudeHarnessFactory({
291
291
  resolver: new SalesforceBedrockClaudeTransportResolver({ gatewayBaseUrl: '...' }),
292
292
  permissionMode: 'bypassPermissions',
293
293
  // Even when `requireToolApproval: true`, calls to these tools bypass the
294
- // broker. They still emit `tool-call` / `tool-result` ChatEvents, so the
295
- // consumer can drive their UX off the `tool-call` directly.
296
- bypassApprovalTools: ['AskUserQuestion'],
294
+ // PreToolUse hook. They still emit `tool-call` / `tool-result` ChatEvents,
295
+ // so the consumer can drive their UX off the `tool-call` directly.
296
+ bypassApprovalTools: [
297
+ 'AskUserQuestion', // Claude built-in
298
+ 'ask_user_tool', // consumer-declared (also bypassed automatically; listing here is harmless)
299
+ ],
297
300
  });
298
301
  ```
299
302
 
303
+ Tool names use the **consumer-facing** form — the bare `AgentConfig.tools[i].name` for consumer-declared tools, the
304
+ SDK-built-in name for Claude built-ins (no `mcp__<bridge>__` prefix needed). The harness expands each entry into the
305
+ underlying wire-prefixed form (`mcp__sfdx-agent-sdk-consumer-tools__<name>`) where applicable so consumer tools bypass
306
+ correctly without consumers needing to know the internal namespacing.
307
+
300
308
  `bypassApprovalTools` is encoded as a negative-lookahead regex on the SDK `PreToolUse` hook's `matcher` field, so
301
309
  non-matching tools fall through to the SDK's permission cache and run natively under the factory-pinned
302
310
  `permissionMode`. The flag is Claude-only by design; Mastra has no equivalent because its approval pipeline gates
303
- consumer-declared tools, not provider built-ins.
311
+ consumer-declared tools differently.
304
312
 
305
- > **Scope:** intended for Claude built-ins (e.g. `AskUserQuestion`) the consumer drives off `tool-call` directly.
306
- > Putting a consumer-tool name (e.g. `mcp__sfdx-agent-sdk-consumer-tools__<tool>`) in this list is unsupported —
307
- > consumer tools are routed through the approval broker's sink coordinator under composition mode (#516), and bypassing
308
- > them disconnects that wiring. To suppress consumer tools, omit them from `AgentConfig.tools` instead.
313
+ For bypassed consumer tools, the chat stream emits the `tool-call` ChatEvent and a synthetic `finish('tool-calls')` on
314
+ the same `eventStream` (the SDK contract for client-side tools — see `ToolDefinition` JSDoc). The consumer runs the tool
315
+ locally and calls `submitToolResult` with the result; the agent loop continues on the same `eventStream` (post-#529
316
+ single-stream-per-turn contract).
309
317
 
310
318
  > **Note:** `permissionMode: 'plan'` denies mutating built-ins (`Edit`, `Write`, `Bash`, …) at the SDK's permission
311
319
  > cache rather than running them natively. Bypassing such a tool with `bypassApprovalTools` does not "unlock" it under
312
320
  > plan mode — the SDK still denies it. Plan mode applies to the whole subprocess; this flag only chooses whether the
313
- > harness's broker gets a say.
321
+ > harness's coordinator gets a say.
314
322
 
315
- When `bypassApprovalTools` is undefined or empty (default), every tool call reaches the approval broker — no behavior
316
- change for existing consumers.
323
+ When `bypassApprovalTools` is undefined or empty (default), every built-in / MCP tool call reaches the coordinator — no
324
+ behavior change for existing consumers. (Consumer-declared tools always bypass.)
317
325
 
318
326
  ## Development
319
327
 
@@ -0,0 +1,205 @@
1
+ import type { HookCallback, SDKMessage } from '@anthropic-ai/claude-agent-sdk';
2
+ import { type ChatEvent } from '@salesforce/sfdx-agent-sdk';
3
+ import { GenSink } from '@salesforce/sfdx-agent-sdk/harness';
4
+ import type { ClaudeEventAdapter, McpToolMetadataLookup } from './claude-event-adapter.js';
5
+ export interface ClaudeApprovalCoordinatorOptions {
6
+ adapter: ClaudeEventAdapter;
7
+ toolApprovalTimeoutMs: number;
8
+ abortController: AbortController;
9
+ /**
10
+ * Optional lookup that resolves an MCP server name + annotations from a
11
+ * Claude-namespaced tool name (`mcp__<server>__<tool>`). When present,
12
+ * `tool-approval-request` events the coordinator pushes from
13
+ * {@link preToolUseHook} carry `serverName` + `annotations` for
14
+ * MCP-bridged tools — matching the enrichment the adapter applies to
15
+ * `tool-call` / `tool-result` events.
16
+ */
17
+ lookupMcpToolMetadata?: McpToolMetadataLookup;
18
+ /**
19
+ * Approval-emission mode (#447). Defaults to `'serial'` when omitted.
20
+ *
21
+ * - `'serial'`: emit `tool-approval-request` only when the SDK's
22
+ * `PreToolUse` hook fires for that tool — one at a time. Identical
23
+ * to behavior before parallel-approval UX. Safe for consumers
24
+ * whose iterator returns on the first approval-request.
25
+ * - `'batch'`: front-load `tool-approval-request` emission off the
26
+ * adapter's `tool-call` events so parallel `tool_use` batches
27
+ * surface all approvals on the same stream. Consumers MUST iterate
28
+ * to natural park collecting approvals.
29
+ */
30
+ mode?: 'serial' | 'batch';
31
+ /**
32
+ * Logical bridge server name for the consumer-tools bridge. The
33
+ * coordinator strips this prefix on `tool-call` / `tool-result`
34
+ * ChatEvents before forwarding them to the consumer so the
35
+ * `ToolCallEvent.toolName === AgentConfig.tools[i].name` cross-harness
36
+ * contract holds.
37
+ *
38
+ * When omitted, the coordinator skips consumer-tool bookkeeping
39
+ * entirely — used by tests that drive the approval path without any
40
+ * consumer tools registered.
41
+ */
42
+ consumerToolsBridgeName?: string;
43
+ }
44
+ export declare class ClaudeApprovalCoordinator {
45
+ private readonly adapter;
46
+ private readonly toolApprovalTimeoutMs;
47
+ private readonly abortController;
48
+ private readonly lookupMcpToolMetadata?;
49
+ private readonly mode;
50
+ private readonly consumerToolBridgePrefix;
51
+ /** Single per-turn sink. Issue #529 contract: one stream per turn. */
52
+ private readonly initial;
53
+ /** Parked `PreToolUse` hook promises, keyed by `toolCallId`. */
54
+ private readonly pendingApprovals;
55
+ /** Buffered consumer decisions, keyed by `toolCallId`. */
56
+ private readonly decisions;
57
+ /** ToolCallIds for which a `tool-approval-request` has been emitted. */
58
+ private readonly emittedApprovals;
59
+ /** ToolCallIds the consumer has settled. Rejects double-settle. */
60
+ private readonly consumerSettled;
61
+ /**
62
+ * ToolCallIds the coordinator auto-resolved (timeout / abort / dispose).
63
+ * A subsequent settle for these ids rejects with "already auto-resolved".
64
+ */
65
+ private readonly settledByCoordinator;
66
+ /** Args from the adapter's `tool-call` ChatEvent, keyed by `toolCallId`. */
67
+ private readonly toolCallArgs;
68
+ /**
69
+ * Consumer-tool: tool-call events pump has seen but no parker invocation
70
+ * for yet. FIFO list per tool name. Empirically the Claude subprocess
71
+ * emits the assistant's `tool_use` block (rendered as `tool-call`) BEFORE
72
+ * dispatching the MCP `tools/call` to the bridge handler.
73
+ */
74
+ private readonly recentToolCallsByName;
75
+ /** Reverse lookup `toolCallId → toolName` for entries in {@link recentToolCallsByName}. */
76
+ private readonly recentToolCallNameById;
77
+ /** Consumer-tool: parker invocations awaiting a `tool-call` arrival. FIFO list per tool name. */
78
+ private readonly waitingParkersByName;
79
+ /** Consumer-tool: bound parkers, keyed by `toolCallId`. `submitToolResult` resolves these. */
80
+ private readonly pendingByToolCallId;
81
+ /** Consumer-tool: results submitted before the bridge handler parked. Keyed by `toolCallId`. */
82
+ private readonly preSubmittedByToolCallId;
83
+ /**
84
+ * Callbacks invoked exactly once when the coordinator tears down
85
+ * (`abort` / `dispose` / pump exit, whichever fires first). Used by
86
+ * the harness to detach the consumer-AbortSignal listener.
87
+ */
88
+ private readonly teardownCallbacks;
89
+ private disposed;
90
+ private aborted;
91
+ /**
92
+ * Set on entry to {@link teardown} so re-entry from any reason
93
+ * (`abort` / `dispose` / `timeout` / `pump-error`) is a no-op. The
94
+ * timeout path in particular calls `abortController.abort()` which
95
+ * causes the pump iterator to throw — without this guard the pump's
96
+ * catch block would observe `!aborted && !disposed` and fire a second
97
+ * teardown. Side effects in `teardown` are idempotent today
98
+ * (`pendingApprovals` clear, ended-sink push no-ops, `teardownCallbacks`
99
+ * set cleared on first run, `AbortController.abort` itself is
100
+ * idempotent), but a single guard is cheaper than auditing every
101
+ * future addition.
102
+ */
103
+ private tornDown;
104
+ constructor(opts: ClaudeApprovalCoordinatorOptions);
105
+ initialSink(): GenSink<ChatEvent>;
106
+ onTeardown(callback: () => void): void;
107
+ private runTeardownCallbacks;
108
+ start(query: AsyncIterable<SDKMessage>): void;
109
+ abort(): void;
110
+ dispose(): void;
111
+ /**
112
+ * Single teardown path for abort / dispose / approval-timeout /
113
+ * pump-error. Marks every emitted approval `settledByCoordinator`
114
+ * (so a subsequent `approve` / `decline` rejects with
115
+ * "already auto-resolved"), drains every parked `PreToolUse` hook
116
+ * Promise and per-id timer, rejects every parked consumer-tool
117
+ * parker, pushes the synthetic terminal pair on the sink, aborts
118
+ * the subprocess via the injected controller, and fires teardown
119
+ * callbacks.
120
+ *
121
+ * The `reason` parameter changes two things:
122
+ * - `'dispose'` pushes an error event only, no `finish` (matches
123
+ * pre-#529 dispose contract: disposal is consumer-triggered,
124
+ * not a normal turn end).
125
+ * - `'timeout'` resolves every parked hook with `decision: 'block'`
126
+ * so the agentic loop ends politely; the other reasons reject
127
+ * parked hooks with the underlying error so the SDK surfaces
128
+ * each as a tool failure.
129
+ *
130
+ * Mirrors the Mastra coordinator's `teardown` so the pump-throw /
131
+ * abort / dispose / timeout paths can't drift out of step on either
132
+ * harness — the audit's Finding #1 (pump-throw drain hygiene) is
133
+ * the regression this single-source teardown discipline pins.
134
+ */
135
+ private teardown;
136
+ /**
137
+ * The Claude SDK's `PreToolUse` hook. Fires before the SDK's per-tool /
138
+ * per-arg permission cache (which the deprecated `canUseTool` route hit
139
+ * AFTER the cache, so ~70% of tool calls slipped past the gate — see
140
+ * #484).
141
+ *
142
+ * Consumer-executed tools never reach this hook because the options
143
+ * builder unconditionally bypasses the consumer-tool wire prefix in the
144
+ * matcher (#529 step 2b decouple). That keeps the parker path the
145
+ * single source of truth for consumer-tool lifecycle and lets this
146
+ * hook focus on built-in / external-MCP tool gating.
147
+ */
148
+ readonly preToolUseHook: HookCallback;
149
+ private emitApprovalRequest;
150
+ private enrichmentFor;
151
+ private handleApprovalTimeout;
152
+ approve(toolCallId: string): void;
153
+ decline(toolCallId: string): void;
154
+ private settlePending;
155
+ /**
156
+ * Parker callback the in-process consumer-tools MCP bridge handler
157
+ * invokes when the Claude subprocess calls `tools/call` for a
158
+ * consumer-declared tool. Returns a Promise the consumer settles via
159
+ * {@link submitToolResult}.
160
+ *
161
+ * Three-way ladder:
162
+ * 1. A `tool-call` event with `toolCallId` already routed AND a result
163
+ * has been pre-submitted under that id → resolve immediately.
164
+ * 2. A `tool-call` event already routed (no pre-submit) → bind under
165
+ * `toolCallId` so the eventual `submitToolResult` resolves us.
166
+ * 3. Neither → queue under `toolName` (FIFO); the next `tool-call`
167
+ * for that name binds to us.
168
+ */
169
+ readonly parkConsumerToolCall: (call: {
170
+ toolName: string;
171
+ args: Record<string, unknown>;
172
+ }) => Promise<{
173
+ result: unknown;
174
+ isError?: boolean;
175
+ }>;
176
+ /**
177
+ * Settle a parked consumer-tool call. Issue #529 contract: returns
178
+ * `void`; post-resume events flow through the same `initial` sink the
179
+ * consumer is iterating from `chat()`.
180
+ */
181
+ submitToolResult(toolCallId: string, result: unknown, isError?: boolean): void;
182
+ private failAllPendingConsumerCalls;
183
+ private pump;
184
+ private route;
185
+ /**
186
+ * Strip the consumer-tool bridge prefix on `tool-call` / `tool-result`
187
+ * events and FIFO-bind `toolCallId`s for parker matching. Returns
188
+ * `undefined` if no rewrite is needed.
189
+ */
190
+ private maybeRewriteForConsumerTool;
191
+ /**
192
+ * FIFO-bind a freshly-seen consumer-tool `toolCallId` to whichever queued
193
+ * state already exists for `strippedToolName`. Three-way ladder:
194
+ *
195
+ * 1. A parker is already waiting (bridge handler arrived first) →
196
+ * pair the parker against this `toolCallId`.
197
+ * 2. Neither parker nor pre-submitted → record the `toolCallId` against
198
+ * the name so the next parker invocation for that name binds to it.
199
+ *
200
+ * (Pre-submit-before-tool-call is impossible: `submitToolResult`
201
+ * requires a `toolCallId` the consumer received from a prior
202
+ * `tool-call` event.)
203
+ */
204
+ private bindToolCallId;
205
+ }