@librechat/agents 3.9.5 → 3.9.7
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/cjs/graphs/Graph.cjs +1 -1
- package/dist/cjs/langfuse.cjs +46 -2
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +1 -1
- package/dist/cjs/run.cjs +35 -12
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +3 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +31 -9
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +5 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +52 -6
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/toolErrorContent.cjs +17 -0
- package/dist/cjs/tools/toolErrorContent.cjs.map +1 -0
- package/dist/cjs/types/hitl.cjs +4 -0
- package/dist/cjs/types/hitl.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +1 -1
- package/dist/esm/langfuse.mjs +47 -4
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +1 -1
- package/dist/esm/run.mjs +36 -13
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +3 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +31 -9
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +5 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +53 -7
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/toolErrorContent.mjs +17 -0
- package/dist/esm/tools/toolErrorContent.mjs.map +1 -0
- package/dist/esm/types/hitl.mjs +4 -1
- package/dist/esm/types/hitl.mjs.map +1 -1
- package/dist/types/langfuse.d.ts +19 -0
- package/dist/types/tools/ToolNode.d.ts +1 -0
- package/dist/types/tools/toolErrorContent.d.ts +2 -0
- package/dist/types/types/hitl.d.ts +11 -0
- package/dist/types/types/tools.d.ts +2 -0
- package/package.json +1 -1
- package/src/langfuse.ts +100 -3
- package/src/run.ts +32 -16
- package/src/stream.ts +5 -3
- package/src/tools/ToolNode.ts +89 -19
- package/src/tools/local/LocalProgrammaticToolCalling.ts +16 -4
- package/src/tools/subagent/SubagentExecutor.ts +105 -10
- package/src/tools/toolErrorContent.ts +48 -0
- package/src/types/hitl.ts +20 -0
- package/src/types/tools.ts +2 -0
package/src/tools/ToolNode.ts
CHANGED
|
@@ -130,8 +130,10 @@ import {
|
|
|
130
130
|
} from '@/tools/local';
|
|
131
131
|
import { stripCodeSessionFileSummary } from '@/tools/CodeSessionFileSummary';
|
|
132
132
|
import { Constants, GraphEvents, CODE_EXECUTION_TOOLS } from '@/common';
|
|
133
|
+
import { formatToolErrorContent } from '@/tools/toolErrorContent';
|
|
133
134
|
import { PreparedSubagentError } from '@/tools/preparedSubagents';
|
|
134
135
|
import { attachRunStepResumeState } from '@/tools/runStepResume';
|
|
136
|
+
import { isBackgroundDenyMode } from '@/types/hitl';
|
|
135
137
|
|
|
136
138
|
function stripToolApprovalReviewConfig(
|
|
137
139
|
configurable: Record<string, unknown> | undefined
|
|
@@ -1948,6 +1950,8 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
1948
1950
|
// they dispatch — including HITL gates on `write_file` / `edit_file`.
|
|
1949
1951
|
hookContext: {
|
|
1950
1952
|
registry: this.hookRegistry,
|
|
1953
|
+
failClosedOnHookError:
|
|
1954
|
+
isBackgroundDenyMode(this.humanInTheLoop),
|
|
1951
1955
|
runId: (config.configurable?.run_id as string | undefined) ?? '',
|
|
1952
1956
|
threadId: config.configurable?.thread_id as string | undefined,
|
|
1953
1957
|
agentId: this.agentId,
|
|
@@ -2201,9 +2205,11 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
2201
2205
|
});
|
|
2202
2206
|
}
|
|
2203
2207
|
}
|
|
2204
|
-
const errorContent =
|
|
2205
|
-
|
|
2206
|
-
this.maxToolResultChars
|
|
2208
|
+
const errorContent = formatToolErrorContent(
|
|
2209
|
+
e.message,
|
|
2210
|
+
this.maxToolResultChars,
|
|
2211
|
+
config.signal,
|
|
2212
|
+
e
|
|
2207
2213
|
);
|
|
2208
2214
|
const refMeta =
|
|
2209
2215
|
unresolvedRefs.length > 0
|
|
@@ -2538,11 +2544,18 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
2538
2544
|
onceReplayKey: approvalReplayKey,
|
|
2539
2545
|
onceReplaySessionId: approvalReplaySessionId,
|
|
2540
2546
|
}).catch((): AggregatedHookResult | undefined =>
|
|
2541
|
-
approvalReviewEvidence == null
|
|
2547
|
+
approvalReviewEvidence == null &&
|
|
2548
|
+
!isBackgroundDenyMode(this.humanInTheLoop)
|
|
2542
2549
|
? undefined
|
|
2543
2550
|
: {
|
|
2544
2551
|
decision: 'deny',
|
|
2545
|
-
reason:
|
|
2552
|
+
reason:
|
|
2553
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
2554
|
+
? 'Approval policy could not be evaluated'
|
|
2555
|
+
: 'Approval policy could not be evaluated on resume',
|
|
2556
|
+
...(isBackgroundDenyMode(this.humanInTheLoop)
|
|
2557
|
+
? { hasHookFailures: true }
|
|
2558
|
+
: {}),
|
|
2546
2559
|
additionalContexts: [],
|
|
2547
2560
|
injectedMessages: [],
|
|
2548
2561
|
errors: [],
|
|
@@ -2579,6 +2592,26 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
2579
2592
|
};
|
|
2580
2593
|
}
|
|
2581
2594
|
|
|
2595
|
+
if (
|
|
2596
|
+
preResult.hasHookFailures === true &&
|
|
2597
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
2598
|
+
) {
|
|
2599
|
+
return persistOutput(
|
|
2600
|
+
this.blockDirectCall({
|
|
2601
|
+
call,
|
|
2602
|
+
resolvedArgs,
|
|
2603
|
+
reason: this.backgroundApprovalReason(
|
|
2604
|
+
call.name,
|
|
2605
|
+
'Approval policy could not be evaluated'
|
|
2606
|
+
),
|
|
2607
|
+
hookRegistry,
|
|
2608
|
+
runId,
|
|
2609
|
+
threadId,
|
|
2610
|
+
}),
|
|
2611
|
+
effectiveCall.args as Record<string, unknown>
|
|
2612
|
+
);
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2582
2615
|
if (preResult.decision === 'deny') {
|
|
2583
2616
|
return persistOutput(
|
|
2584
2617
|
this.blockDirectCall({
|
|
@@ -2618,12 +2651,14 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
2618
2651
|
|
|
2619
2652
|
if (preResult.decision === 'ask' || reviewedApproval != null) {
|
|
2620
2653
|
if (this.humanInTheLoop?.enabled !== true) {
|
|
2621
|
-
// Fail
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2654
|
+
// Fail closed when there is no foreground approval channel.
|
|
2655
|
+
const reason =
|
|
2656
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
2657
|
+
? this.backgroundApprovalReason(call.name, preResult.reason)
|
|
2658
|
+
: this.resolveAskDecisionForDirectTool(
|
|
2659
|
+
preResult.reason,
|
|
2660
|
+
call.name
|
|
2661
|
+
);
|
|
2627
2662
|
return persistOutput(
|
|
2628
2663
|
this.blockDirectCall({
|
|
2629
2664
|
call,
|
|
@@ -3005,6 +3040,13 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
3005
3040
|
* LangGraph `interrupt()` instead — see `runDirectToolWithLifecycleHooks`.
|
|
3006
3041
|
*/
|
|
3007
3042
|
private askDirectWarningEmitted = false;
|
|
3043
|
+
private backgroundApprovalReason(toolName: string, reason?: string): string {
|
|
3044
|
+
return (
|
|
3045
|
+
`Approval required for "${toolName}"; unavailable in a background subagent. ` +
|
|
3046
|
+
`Ask the parent to run it in the foreground.${reason == null ? '' : ` Reason: ${reason}`}`
|
|
3047
|
+
);
|
|
3048
|
+
}
|
|
3049
|
+
|
|
3008
3050
|
private resolveAskDecisionForDirectTool(
|
|
3009
3051
|
reason: string | undefined,
|
|
3010
3052
|
toolName: string
|
|
@@ -3623,7 +3665,11 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
3623
3665
|
matchQuery: entry.call.name,
|
|
3624
3666
|
onceReplayKey: approvalReplayKey,
|
|
3625
3667
|
onceReplaySessionId: approvalReplaySessionId,
|
|
3626
|
-
}).catch((): AggregatedHookResult =>
|
|
3668
|
+
}).catch((): AggregatedHookResult =>
|
|
3669
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
3670
|
+
? { ...HOOK_FALLBACK, hasHookFailures: true }
|
|
3671
|
+
: HOOK_FALLBACK
|
|
3672
|
+
);
|
|
3627
3673
|
})
|
|
3628
3674
|
);
|
|
3629
3675
|
|
|
@@ -3790,6 +3836,20 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
3790
3836
|
batchAdditionalContexts.push(ctx);
|
|
3791
3837
|
}
|
|
3792
3838
|
|
|
3839
|
+
if (
|
|
3840
|
+
hookResult.hasHookFailures === true &&
|
|
3841
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
3842
|
+
) {
|
|
3843
|
+
blockEntry(
|
|
3844
|
+
entry,
|
|
3845
|
+
this.backgroundApprovalReason(
|
|
3846
|
+
entry.call.name,
|
|
3847
|
+
'Approval policy could not be evaluated'
|
|
3848
|
+
)
|
|
3849
|
+
);
|
|
3850
|
+
continue;
|
|
3851
|
+
}
|
|
3852
|
+
|
|
3793
3853
|
if (hookResult.decision === 'deny') {
|
|
3794
3854
|
blockEntry(entry, hookResult.reason ?? 'Blocked by hook');
|
|
3795
3855
|
continue;
|
|
@@ -3807,7 +3867,15 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
3807
3867
|
* JSDoc for the full rationale and the migration plan.
|
|
3808
3868
|
*/
|
|
3809
3869
|
if (this.humanInTheLoop?.enabled !== true) {
|
|
3810
|
-
blockEntry(
|
|
3870
|
+
blockEntry(
|
|
3871
|
+
entry,
|
|
3872
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
3873
|
+
? this.backgroundApprovalReason(
|
|
3874
|
+
entry.call.name,
|
|
3875
|
+
hookResult.reason
|
|
3876
|
+
)
|
|
3877
|
+
: (hookResult.reason ?? 'Blocked by hook')
|
|
3878
|
+
);
|
|
3811
3879
|
continue;
|
|
3812
3880
|
}
|
|
3813
3881
|
/**
|
|
@@ -4461,9 +4529,10 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
4461
4529
|
let finalToolOutput: unknown = result.content;
|
|
4462
4530
|
|
|
4463
4531
|
if (result.status === 'error') {
|
|
4464
|
-
contentString =
|
|
4465
|
-
|
|
4466
|
-
this.maxToolResultChars
|
|
4532
|
+
contentString = formatToolErrorContent(
|
|
4533
|
+
result.errorMessage,
|
|
4534
|
+
this.maxToolResultChars,
|
|
4535
|
+
config.signal
|
|
4467
4536
|
);
|
|
4468
4537
|
/**
|
|
4469
4538
|
* Error results bypass registration but stamp the
|
|
@@ -4977,9 +5046,10 @@ export class ToolNode<T = any> extends RunnableCallable<T, T> {
|
|
|
4977
5046
|
): Promise<boolean> {
|
|
4978
5047
|
const output =
|
|
4979
5048
|
result.status === 'error'
|
|
4980
|
-
?
|
|
4981
|
-
|
|
4982
|
-
this.maxToolResultChars
|
|
5049
|
+
? formatToolErrorContent(
|
|
5050
|
+
result.errorMessage,
|
|
5051
|
+
this.maxToolResultChars,
|
|
5052
|
+
config.signal
|
|
4983
5053
|
)
|
|
4984
5054
|
: serializeToolOutputWithinLimits(
|
|
4985
5055
|
result.content,
|
|
@@ -225,6 +225,15 @@ export async function applyPreToolUseHooksForBridge(
|
|
|
225
225
|
sessionId: hookContext.runId,
|
|
226
226
|
matchQuery: toolName,
|
|
227
227
|
}).catch(() => undefined);
|
|
228
|
+
if (
|
|
229
|
+
hookContext.failClosedOnHookError === true &&
|
|
230
|
+
(result == null || result.hasHookFailures === true)
|
|
231
|
+
) {
|
|
232
|
+
return {
|
|
233
|
+
input: toolInput,
|
|
234
|
+
denyReason: `Approval policy could not be evaluated for "${toolName}" in a background subagent. Ask the parent to run it in the foreground.`,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
228
237
|
if (result == null) {
|
|
229
238
|
return { input: toolInput };
|
|
230
239
|
}
|
|
@@ -236,10 +245,13 @@ export async function applyPreToolUseHooksForBridge(
|
|
|
236
245
|
return {
|
|
237
246
|
input: nextInput,
|
|
238
247
|
denyReason:
|
|
239
|
-
result.
|
|
240
|
-
|
|
241
|
-
? `
|
|
242
|
-
:
|
|
248
|
+
result.decision === 'ask' &&
|
|
249
|
+
hookContext.failClosedOnHookError === true
|
|
250
|
+
? `Approval required for "${toolName}"; unavailable in a background subagent. Ask the parent to run it in the foreground.${result.reason == null ? '' : ` Reason: ${result.reason}`}`
|
|
251
|
+
: (result.reason ??
|
|
252
|
+
(result.decision === 'ask'
|
|
253
|
+
? `Tool "${toolName}" requires human approval; bridge cannot raise an interrupt — denying.`
|
|
254
|
+
: `Tool "${toolName}" denied by PreToolUse hook.`)),
|
|
243
255
|
};
|
|
244
256
|
}
|
|
245
257
|
return { input: nextInput };
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
END,
|
|
14
14
|
GraphInterrupt,
|
|
15
15
|
INTERRUPT,
|
|
16
|
+
MemorySaver,
|
|
16
17
|
MessagesAnnotation,
|
|
17
18
|
START,
|
|
18
19
|
StateGraph,
|
|
@@ -141,9 +142,10 @@ import { seedAgentInitialSessions } from '@/utils/toolSessions';
|
|
|
141
142
|
import { stableStringify } from '@/tools/eagerEventExecution';
|
|
142
143
|
import { convertInjectedMessages } from '@/messages/injected';
|
|
143
144
|
import { resolveClientOptionsModel } from '@/llm/request';
|
|
145
|
+
import { isBackgroundDenyMode } from '@/types/hitl';
|
|
144
146
|
import { composeAbortSignals } from '@/utils/misc';
|
|
145
|
-
import { sleep } from '@/utils/run';
|
|
146
147
|
import { HandlerRegistry } from '@/events';
|
|
148
|
+
import { sleep } from '@/utils/run';
|
|
147
149
|
|
|
148
150
|
export {
|
|
149
151
|
buildChildInputs,
|
|
@@ -875,6 +877,27 @@ function createSubagentFailure(
|
|
|
875
877
|
return { content, messages: [], error };
|
|
876
878
|
}
|
|
877
879
|
|
|
880
|
+
function excludeBackgroundQuestionTool(agentInputs: AgentInputs): void {
|
|
881
|
+
const questionTool = 'ask_user_question';
|
|
882
|
+
agentInputs.tools = agentInputs.tools?.filter(
|
|
883
|
+
(tool) => !('name' in tool) || tool.name !== questionTool
|
|
884
|
+
);
|
|
885
|
+
agentInputs.graphTools = agentInputs.graphTools?.filter(
|
|
886
|
+
(tool) => tool.name !== questionTool
|
|
887
|
+
);
|
|
888
|
+
agentInputs.toolDefinitions = agentInputs.toolDefinitions?.filter(
|
|
889
|
+
(tool) => tool.name !== questionTool
|
|
890
|
+
);
|
|
891
|
+
if (agentInputs.toolMap?.has(questionTool) === true) {
|
|
892
|
+
agentInputs.toolMap = new Map(agentInputs.toolMap);
|
|
893
|
+
agentInputs.toolMap.delete(questionTool);
|
|
894
|
+
}
|
|
895
|
+
if (agentInputs.toolRegistry?.has(questionTool) === true) {
|
|
896
|
+
agentInputs.toolRegistry = new Map(agentInputs.toolRegistry);
|
|
897
|
+
agentInputs.toolRegistry.delete(questionTool);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
878
901
|
/**
|
|
879
902
|
* Factory that constructs a child graph for subagent execution. Injected
|
|
880
903
|
* rather than imported so that `SubagentExecutor` does not have a runtime
|
|
@@ -1106,7 +1129,10 @@ export class SubagentExecutor {
|
|
|
1106
1129
|
message: 'Maximum subagent nesting depth exceeded.',
|
|
1107
1130
|
});
|
|
1108
1131
|
}
|
|
1109
|
-
if (
|
|
1132
|
+
if (
|
|
1133
|
+
this.humanInTheLoop?.enabled === true &&
|
|
1134
|
+
this.humanInTheLoop.backgroundPausePolicy === 'reject'
|
|
1135
|
+
) {
|
|
1110
1136
|
return JSON.stringify({
|
|
1111
1137
|
status: 'rejected',
|
|
1112
1138
|
message:
|
|
@@ -1292,6 +1318,15 @@ export class SubagentExecutor {
|
|
|
1292
1318
|
usageSink: this.usageSink,
|
|
1293
1319
|
subagentContext: this.subagentContext,
|
|
1294
1320
|
streamLimits: this.streamLimits,
|
|
1321
|
+
humanInTheLoop:
|
|
1322
|
+
this.humanInTheLoop?.enabled === true ||
|
|
1323
|
+
this.humanInTheLoop?.backgroundPausePolicy === 'deny'
|
|
1324
|
+
? {
|
|
1325
|
+
enabled: false,
|
|
1326
|
+
backgroundPausePolicy: 'deny',
|
|
1327
|
+
backgroundDeny: true,
|
|
1328
|
+
}
|
|
1329
|
+
: this.humanInTheLoop,
|
|
1295
1330
|
maxDepth: this.maxDepth,
|
|
1296
1331
|
createChildGraph:
|
|
1297
1332
|
detachedGraphFactory == null
|
|
@@ -1338,6 +1373,27 @@ export class SubagentExecutor {
|
|
|
1338
1373
|
if (result.error != null) {
|
|
1339
1374
|
throw new Error(result.error);
|
|
1340
1375
|
}
|
|
1376
|
+
if (this.humanInTheLoop?.enabled === true) {
|
|
1377
|
+
const deniedTools = new Set<string>();
|
|
1378
|
+
for (const message of result.messages) {
|
|
1379
|
+
if (
|
|
1380
|
+
message instanceof ToolMessage &&
|
|
1381
|
+
message.status === 'error' &&
|
|
1382
|
+
typeof message.content === 'string' &&
|
|
1383
|
+
message.content.startsWith('Blocked: Approval required for "') &&
|
|
1384
|
+
message.name != null
|
|
1385
|
+
) {
|
|
1386
|
+
deniedTools.add(message.name);
|
|
1387
|
+
if (deniedTools.size >= 10) break;
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
if (deniedTools.size > 0) {
|
|
1391
|
+
return {
|
|
1392
|
+
...result,
|
|
1393
|
+
content: `${result.content}\nBackground approval denied for: ${[...deniedTools].join(', ')}. Ask the parent to run those tools in the foreground.`,
|
|
1394
|
+
};
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1341
1397
|
return result;
|
|
1342
1398
|
} finally {
|
|
1343
1399
|
detached.clearHeavyState();
|
|
@@ -2489,6 +2545,11 @@ export class SubagentExecutor {
|
|
|
2489
2545
|
parentMaxDepth: this.maxDepth,
|
|
2490
2546
|
keepToolDefinitions: hasToolExecuteHandler,
|
|
2491
2547
|
});
|
|
2548
|
+
if (isBackgroundDenyMode(this.humanInTheLoop)) {
|
|
2549
|
+
for (const agentInputs of childPlan.agents) {
|
|
2550
|
+
excludeBackgroundQuestionTool(agentInputs);
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2492
2553
|
const childAgentId = childPlan.subjectAgentId;
|
|
2493
2554
|
const currentHookSessionId =
|
|
2494
2555
|
asNonEmptyString(params.hookSessionId) ??
|
|
@@ -2638,6 +2699,18 @@ export class SubagentExecutor {
|
|
|
2638
2699
|
});
|
|
2639
2700
|
}
|
|
2640
2701
|
childGraph ??= this.createChildGraph(childGraphInput);
|
|
2702
|
+
if (isBackgroundDenyMode(this.humanInTheLoop)) {
|
|
2703
|
+
childGraph.humanInTheLoop = this.humanInTheLoop;
|
|
2704
|
+
childGraph.eagerEventToolExecution = undefined;
|
|
2705
|
+
// Never reuse the HITL parent's saver: its checkpoint namespace may
|
|
2706
|
+
// contain parent messages. A private saver also lets an unexpected
|
|
2707
|
+
// interrupt surface as a task error instead of a missing-saver tool
|
|
2708
|
+
// error that the child could mistake for a completed task.
|
|
2709
|
+
childGraph.compileOptions = {
|
|
2710
|
+
...childGraph.compileOptions,
|
|
2711
|
+
checkpointer: new MemorySaver(),
|
|
2712
|
+
};
|
|
2713
|
+
}
|
|
2641
2714
|
if (params.taskRuntime != null) {
|
|
2642
2715
|
childGraph.hookRegistry = this.hookRegistry;
|
|
2643
2716
|
}
|
|
@@ -2756,7 +2829,8 @@ export class SubagentExecutor {
|
|
|
2756
2829
|
childConfigurable[SUBAGENT_RESUME_ATTEMPT_CONFIG_KEY] = resumeAttemptId;
|
|
2757
2830
|
}
|
|
2758
2831
|
childConfigurable.thread_id =
|
|
2759
|
-
this.humanInTheLoop?.enabled === true
|
|
2832
|
+
this.humanInTheLoop?.enabled === true ||
|
|
2833
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
2760
2834
|
? childThreadId
|
|
2761
2835
|
: (inheritedConfigurable.thread_id ?? childRunId);
|
|
2762
2836
|
const childInvokeConfig = {
|
|
@@ -2884,16 +2958,31 @@ export class SubagentExecutor {
|
|
|
2884
2958
|
},
|
|
2885
2959
|
sessionId: currentHookSessionId,
|
|
2886
2960
|
matchQuery: subagentType,
|
|
2887
|
-
}).catch((): AggregatedHookResult =>
|
|
2961
|
+
}).catch((): AggregatedHookResult =>
|
|
2962
|
+
isBackgroundDenyMode(this.humanInTheLoop)
|
|
2963
|
+
? { ...HOOK_FALLBACK, hasHookFailures: true }
|
|
2964
|
+
: HOOK_FALLBACK
|
|
2965
|
+
);
|
|
2888
2966
|
|
|
2889
|
-
|
|
2967
|
+
const policyFailed =
|
|
2968
|
+
hookResult.hasHookFailures === true &&
|
|
2969
|
+
isBackgroundDenyMode(this.humanInTheLoop);
|
|
2970
|
+
if (
|
|
2971
|
+
policyFailed ||
|
|
2972
|
+
hookResult.decision === 'deny' ||
|
|
2973
|
+
hookResult.decision === 'ask'
|
|
2974
|
+
) {
|
|
2890
2975
|
this.clearChildGraph(childGraph);
|
|
2891
2976
|
execution.releaseActiveRun();
|
|
2892
2977
|
this.executions.remove(execution);
|
|
2893
|
-
return
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2978
|
+
return policyFailed
|
|
2979
|
+
? createSubagentFailure(
|
|
2980
|
+
'Subagent start policy could not be evaluated; background execution denied.'
|
|
2981
|
+
)
|
|
2982
|
+
: {
|
|
2983
|
+
content: `Blocked: ${hookResult.reason ?? 'Blocked by hook'}`,
|
|
2984
|
+
messages: [],
|
|
2985
|
+
};
|
|
2897
2986
|
}
|
|
2898
2987
|
}
|
|
2899
2988
|
execution.markStarted();
|
|
@@ -2956,7 +3045,8 @@ export class SubagentExecutor {
|
|
|
2956
3045
|
};
|
|
2957
3046
|
}
|
|
2958
3047
|
}
|
|
2959
|
-
} catch (
|
|
3048
|
+
} catch (caught) {
|
|
3049
|
+
let error = caught;
|
|
2960
3050
|
/** Stamped at failure, not after the error-envelope work below. */
|
|
2961
3051
|
const childTerminalAt = Date.now();
|
|
2962
3052
|
/**
|
|
@@ -2971,6 +3061,11 @@ export class SubagentExecutor {
|
|
|
2971
3061
|
* `cancelled` here.
|
|
2972
3062
|
*/
|
|
2973
3063
|
const abortedBeforeError = childSignal.aborted;
|
|
3064
|
+
if (isGraphInterrupt(error) && params.taskRuntime != null) {
|
|
3065
|
+
error = new Error(
|
|
3066
|
+
'Background subagent cannot pause for human input. Run the tool in the foreground.'
|
|
3067
|
+
);
|
|
3068
|
+
}
|
|
2974
3069
|
if (isGraphInterrupt(error)) {
|
|
2975
3070
|
const activeChildRun = execution.activeRun;
|
|
2976
3071
|
if (activeChildRun != null) {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { PreparedSubagentError } from '@/tools/preparedSubagents';
|
|
2
|
+
import { truncateToolResultContent } from '@/utils/truncation';
|
|
3
|
+
import { StreamLimitExceededError } from '@/llm/streamLimits';
|
|
4
|
+
|
|
5
|
+
export const STOPPED_RUN_TOOL_ERROR =
|
|
6
|
+
'STOP. The user doesn\'t want to proceed with this tool use. ' +
|
|
7
|
+
'The run was stopped; the tool may not have completed, and its effects should not be assumed. ' +
|
|
8
|
+
'STOP what you are doing and wait for the user to tell you how to proceed.';
|
|
9
|
+
|
|
10
|
+
/** A host result only carries error text, so require the run signal as well. */
|
|
11
|
+
function isStoppedRunToolError(
|
|
12
|
+
message: string | undefined,
|
|
13
|
+
signal?: AbortSignal,
|
|
14
|
+
error?: Error
|
|
15
|
+
): boolean {
|
|
16
|
+
if (
|
|
17
|
+
signal?.aborted !== true ||
|
|
18
|
+
signal.reason instanceof StreamLimitExceededError ||
|
|
19
|
+
signal.reason instanceof PreparedSubagentError
|
|
20
|
+
) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if (
|
|
24
|
+
(error != null && error === signal.reason) ||
|
|
25
|
+
(error instanceof Error &&
|
|
26
|
+
(error.name === 'AbortError' ||
|
|
27
|
+
('code' in error &&
|
|
28
|
+
(error.code === 'ABORT_ERR' || error.code === 'ERR_CANCELED'))))
|
|
29
|
+
) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return (
|
|
33
|
+
message != null &&
|
|
34
|
+
/AbortError|(?:operation|request|stream) was aborted/i.test(message)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function formatToolErrorContent(
|
|
39
|
+
message: string | undefined,
|
|
40
|
+
maxChars: number,
|
|
41
|
+
signal?: AbortSignal,
|
|
42
|
+
error?: Error
|
|
43
|
+
): string {
|
|
44
|
+
const content = isStoppedRunToolError(message, signal, error)
|
|
45
|
+
? STOPPED_RUN_TOOL_ERROR
|
|
46
|
+
: `Error: ${message ?? 'Unknown error'}\n Please fix your mistakes.`;
|
|
47
|
+
return truncateToolResultContent(content, maxChars);
|
|
48
|
+
}
|
package/src/types/hitl.ts
CHANGED
|
@@ -407,6 +407,26 @@ export interface HumanInTheLoopConfig {
|
|
|
407
407
|
* UI is ready to render and resolve `tool_approval` interrupts.
|
|
408
408
|
*/
|
|
409
409
|
enabled?: boolean;
|
|
410
|
+
/**
|
|
411
|
+
* Background children cannot pause for human input. `deny` (the default)
|
|
412
|
+
* lets them run but blocks tools whose hooks request approval, and excludes
|
|
413
|
+
* `ask_user_question`. `reject` preserves the legacy admission check.
|
|
414
|
+
* Durable deferred approvals require a separate resume implementation.
|
|
415
|
+
*/
|
|
416
|
+
backgroundPausePolicy?: 'deny' | 'reject';
|
|
417
|
+
/** @internal Set only on detached children and their descendants, never on the parent run. */
|
|
418
|
+
backgroundDeny?: true;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** True only on an SDK-created background child, not a foreground HITL run. */
|
|
422
|
+
export function isBackgroundDenyMode(
|
|
423
|
+
config: HumanInTheLoopConfig | undefined
|
|
424
|
+
): boolean {
|
|
425
|
+
return (
|
|
426
|
+
config?.backgroundDeny === true &&
|
|
427
|
+
config.enabled === false &&
|
|
428
|
+
config.backgroundPausePolicy === 'deny'
|
|
429
|
+
);
|
|
410
430
|
}
|
|
411
431
|
|
|
412
432
|
/**
|
package/src/types/tools.ts
CHANGED
|
@@ -1256,6 +1256,8 @@ export type ProgrammaticCache = {
|
|
|
1256
1256
|
|
|
1257
1257
|
export type ProgrammaticHookContext = {
|
|
1258
1258
|
registry: import('@/hooks').HookRegistry | undefined;
|
|
1259
|
+
/** A detached HITL child must never execute an inner tool if its policy hook fails. */
|
|
1260
|
+
failClosedOnHookError?: boolean;
|
|
1259
1261
|
runId: string;
|
|
1260
1262
|
threadId?: string;
|
|
1261
1263
|
agentId?: string;
|