@librechat/agents 3.4.0 → 3.4.2
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/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +165 -57
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +81 -33
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/graphs/applyGraphRuntimeConfig.cjs +14 -0
- package/dist/cjs/graphs/applyGraphRuntimeConfig.cjs.map +1 -0
- package/dist/cjs/graphs/createGraph.cjs +15 -0
- package/dist/cjs/graphs/createGraph.cjs.map +1 -0
- package/dist/cjs/graphs/index.cjs +1 -0
- package/dist/cjs/hooks/HookRegistry.cjs +83 -0
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/createWorkspacePolicyHook.cjs +1 -1
- package/dist/cjs/hooks/executeHooks.cjs +60 -14
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
- package/dist/cjs/hooks/index.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +2 -0
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/llm/init.cjs +3 -3
- package/dist/cjs/llm/invoke.cjs +2 -2
- package/dist/cjs/main.cjs +34 -24
- package/dist/cjs/messages/format.cjs +40 -23
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/run.cjs +130 -53
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +37 -2
- package/dist/cjs/session/AgentSession.cjs.map +1 -1
- package/dist/cjs/session/JsonlSessionStore.cjs +2 -2
- package/dist/cjs/stream.cjs +1 -1
- package/dist/cjs/summarization/node.cjs +6 -3
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +1 -1
- package/dist/cjs/tools/SubagentTool.cjs +4 -4
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +321 -95
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +1 -1
- package/dist/cjs/tools/eagerEventExecution.cjs +1 -0
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +3 -3
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +2 -2
- package/dist/cjs/tools/search/rerankers.cjs +154 -1
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +4 -1
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutionRegistry.cjs +601 -0
- package/dist/cjs/tools/subagent/SubagentExecutionRegistry.cjs.map +1 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +1114 -233
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentReplay.cjs +177 -0
- package/dist/cjs/tools/subagent/SubagentReplay.cjs.map +1 -0
- package/dist/cjs/tools/subagent/childGraphConfig.cjs +278 -0
- package/dist/cjs/tools/subagent/childGraphConfig.cjs.map +1 -0
- package/dist/cjs/tools/subagent/index.cjs +1 -0
- package/dist/cjs/tools/subagent/runtimeLimits.cjs +5 -0
- package/dist/cjs/tools/subagent/runtimeLimits.cjs.map +1 -0
- package/dist/cjs/tools/toolOutputReferences.cjs +20 -0
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
- package/dist/cjs/utils/index.cjs +2 -2
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +165 -57
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +81 -33
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/graphs/applyGraphRuntimeConfig.mjs +14 -0
- package/dist/esm/graphs/applyGraphRuntimeConfig.mjs.map +1 -0
- package/dist/esm/graphs/createGraph.mjs +15 -0
- package/dist/esm/graphs/createGraph.mjs.map +1 -0
- package/dist/esm/graphs/index.mjs +1 -0
- package/dist/esm/hooks/HookRegistry.mjs +83 -0
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs +1 -1
- package/dist/esm/hooks/executeHooks.mjs +60 -14
- package/dist/esm/hooks/executeHooks.mjs.map +1 -1
- package/dist/esm/hooks/index.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +2 -1
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/llm/init.mjs +1 -1
- package/dist/esm/llm/invoke.mjs +2 -2
- package/dist/esm/main.mjs +21 -19
- package/dist/esm/messages/format.mjs +40 -23
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/run.mjs +131 -54
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +37 -2
- package/dist/esm/session/AgentSession.mjs.map +1 -1
- package/dist/esm/session/JsonlSessionStore.mjs +2 -2
- package/dist/esm/stream.mjs +1 -1
- package/dist/esm/summarization/node.mjs +6 -3
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +1 -1
- package/dist/esm/tools/SubagentTool.mjs +4 -4
- package/dist/esm/tools/SubagentTool.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +322 -96
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +1 -1
- package/dist/esm/tools/eagerEventExecution.mjs +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +3 -3
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +2 -2
- package/dist/esm/tools/search/rerankers.mjs +154 -1
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +4 -1
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutionRegistry.mjs +597 -0
- package/dist/esm/tools/subagent/SubagentExecutionRegistry.mjs.map +1 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +1114 -233
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentReplay.mjs +170 -0
- package/dist/esm/tools/subagent/SubagentReplay.mjs.map +1 -0
- package/dist/esm/tools/subagent/childGraphConfig.mjs +272 -0
- package/dist/esm/tools/subagent/childGraphConfig.mjs.map +1 -0
- package/dist/esm/tools/subagent/index.mjs +1 -0
- package/dist/esm/tools/subagent/runtimeLimits.mjs +5 -0
- package/dist/esm/tools/subagent/runtimeLimits.mjs.map +1 -0
- package/dist/esm/tools/toolOutputReferences.mjs +20 -0
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
- package/dist/esm/utils/index.mjs +2 -2
- package/dist/types/agents/AgentContext.d.ts +1 -1
- package/dist/types/graphs/Graph.d.ts +22 -2
- package/dist/types/graphs/MultiAgentGraph.d.ts +7 -1
- package/dist/types/graphs/applyGraphRuntimeConfig.d.ts +3 -0
- package/dist/types/graphs/createGraph.d.ts +13 -0
- package/dist/types/graphs/graphFactory.d.ts +13 -0
- package/dist/types/graphs/index.d.ts +2 -0
- package/dist/types/hooks/HookRegistry.d.ts +10 -1
- package/dist/types/hooks/executeHooks.d.ts +5 -1
- package/dist/types/hooks/index.d.ts +2 -2
- package/dist/types/hooks/types.d.ts +12 -0
- package/dist/types/run.d.ts +6 -3
- package/dist/types/session/AgentSession.d.ts +1 -0
- package/dist/types/tools/SubagentTool.d.ts +1 -1
- package/dist/types/tools/ToolNode.d.ts +23 -15
- package/dist/types/tools/search/rerankers.d.ts +28 -0
- package/dist/types/tools/search/types.d.ts +38 -1
- package/dist/types/tools/subagent/SubagentExecutionRegistry.d.ts +171 -0
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +70 -36
- package/dist/types/tools/subagent/SubagentReplay.d.ts +85 -0
- package/dist/types/tools/subagent/childGraphConfig.d.ts +37 -0
- package/dist/types/tools/subagent/index.d.ts +1 -1
- package/dist/types/tools/subagent/runtimeLimits.d.ts +4 -0
- package/dist/types/tools/toolOutputReferences.d.ts +12 -0
- package/dist/types/types/graph.d.ts +189 -24
- package/dist/types/types/hitl.d.ts +15 -0
- package/package.json +8 -2
- package/src/agents/AgentContext.ts +1 -1
- package/src/graphs/Graph.ts +349 -104
- package/src/graphs/MultiAgentGraph.ts +149 -47
- package/src/graphs/__tests__/Graph.breakerLifecycle.test.ts +48 -9
- package/src/graphs/__tests__/Graph.contextOverflow.test.ts +1 -1
- package/src/graphs/__tests__/Graph.subagentResumeState.test.ts +80 -0
- package/src/graphs/__tests__/MultiAgentGraph.test.ts +86 -0
- package/src/graphs/__tests__/composition.smoke.test.ts +152 -0
- package/src/graphs/__tests__/createGraph.test.ts +206 -0
- package/src/graphs/applyGraphRuntimeConfig.ts +25 -0
- package/src/graphs/createGraph.ts +32 -0
- package/src/graphs/graphFactory.ts +12 -0
- package/src/graphs/index.ts +2 -0
- package/src/hooks/HookRegistry.ts +151 -1
- package/src/hooks/__tests__/HookRegistry.test.ts +96 -0
- package/src/hooks/__tests__/executeHooks.test.ts +160 -0
- package/src/hooks/executeHooks.ts +133 -15
- package/src/hooks/index.ts +3 -1
- package/src/hooks/types.ts +16 -0
- package/src/messages/format.ts +54 -26
- package/src/messages/formatAgentMessages.reducer.test.ts +162 -0
- package/src/messages/formatAgentMessages.steer.test.ts +16 -11
- package/src/messages/formatAgentMessages.test.ts +7 -2
- package/src/run.ts +214 -56
- package/src/session/AgentSession.ts +52 -2
- package/src/session/__tests__/JsonlSessionStore.test.ts +53 -0
- package/src/specs/graph-subagent.live.test.ts +242 -0
- package/src/specs/graph-subagent.test.ts +322 -0
- package/src/specs/langfuse-routing.integration.test.ts +120 -1
- package/src/specs/subagent.test.ts +130 -4
- package/src/summarization/__tests__/node.test.ts +32 -14
- package/src/summarization/node.ts +15 -11
- package/src/tools/SubagentTool.ts +4 -4
- package/src/tools/ToolNode.ts +643 -157
- package/src/tools/__tests__/SubagentExecutor.test.ts +740 -15
- package/src/tools/__tests__/SubagentReplay.test.ts +329 -0
- package/src/tools/__tests__/ToolNode.breakerSignal.test.ts +208 -2
- package/src/tools/__tests__/directToolHITLResumeScope.test.ts +512 -2
- package/src/tools/__tests__/hitl.test.ts +234 -5
- package/src/tools/__tests__/subagentHooks.test.ts +1126 -61
- package/src/tools/__tests__/toolOutputReferences.test.ts +19 -2
- package/src/tools/search/rag-api-reranker.test.ts +891 -0
- package/src/tools/search/rerankers.ts +260 -0
- package/src/tools/search/tool.ts +6 -0
- package/src/tools/search/types.ts +45 -1
- package/src/tools/subagent/SubagentExecutionRegistry.ts +1159 -0
- package/src/tools/subagent/SubagentExecutor.ts +2232 -312
- package/src/tools/subagent/SubagentReplay.ts +584 -0
- package/src/tools/subagent/__tests__/SubagentExecutionRegistry.test.ts +637 -0
- package/src/tools/subagent/__tests__/SubagentExecutor.lazy.test.ts +3054 -0
- package/src/tools/subagent/__tests__/childGraphConfig.test.ts +521 -0
- package/src/tools/subagent/childGraphConfig.ts +694 -0
- package/src/tools/subagent/index.ts +6 -0
- package/src/tools/subagent/runtimeLimits.ts +7 -0
- package/src/tools/toolOutputReferences.ts +40 -1
- package/src/types/graph.ts +222 -24
- package/src/types/hitl.ts +16 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSubagentResumeManifest,
|
|
3
|
+
attachSubagentResumeManifest,
|
|
4
|
+
requireValidSubagentResumeManifest,
|
|
5
|
+
stripSubagentResumeManifest,
|
|
6
|
+
} from '@/tools/subagent/SubagentReplay';
|
|
7
|
+
|
|
8
|
+
describe('SubagentReplay manifest', () => {
|
|
9
|
+
const execution = {
|
|
10
|
+
parentToolCallId: 'call_parent',
|
|
11
|
+
childRunId: 'child-run',
|
|
12
|
+
subagentType: 'researcher',
|
|
13
|
+
configId: 'researcher@v1',
|
|
14
|
+
approvalExecutionScope: 'child-approval-scope',
|
|
15
|
+
checkpoints: [
|
|
16
|
+
{
|
|
17
|
+
threadId: 'child-thread',
|
|
18
|
+
checkpointId: 'checkpoint-root',
|
|
19
|
+
checkpointNs: '',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
threadId: 'child-thread',
|
|
23
|
+
checkpointId: 'checkpoint-agent',
|
|
24
|
+
checkpointNs: 'agent:task',
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
graphState: {
|
|
28
|
+
toolCallSteps: [{ toolCallId: 'call_tool', stepId: 'step_tool' }],
|
|
29
|
+
toolSessions: [
|
|
30
|
+
{
|
|
31
|
+
toolName: 'execute_code',
|
|
32
|
+
context: { session_id: 'session', lastUpdated: 1 },
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
toolNodes: [
|
|
36
|
+
{
|
|
37
|
+
stateKey: 'child-agent',
|
|
38
|
+
toolUsageCounts: [{ toolName: 'calculator', count: 1 }],
|
|
39
|
+
directPathTurns: [{ toolCallId: 'call_tool', turn: 0 }],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
eagerToolUsage: [
|
|
43
|
+
{
|
|
44
|
+
agentId: 'child-agent',
|
|
45
|
+
toolUsageCounts: [{ toolName: 'calculator', count: 1 }],
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
eagerToolSuppressions: ['unstable_search'],
|
|
49
|
+
toolOutputReferences: {
|
|
50
|
+
entries: [{ key: 'tool0turn0', value: '42' }],
|
|
51
|
+
turnCounter: 1,
|
|
52
|
+
warnedNonStringTools: [],
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
approvalReplays: [
|
|
56
|
+
{
|
|
57
|
+
key: {
|
|
58
|
+
executionScope: 'child-approval-scope',
|
|
59
|
+
agentId: 'child-agent',
|
|
60
|
+
toolUseId: 'call_tool',
|
|
61
|
+
},
|
|
62
|
+
result: {
|
|
63
|
+
decision: 'ask' as const,
|
|
64
|
+
reason: 'review tool',
|
|
65
|
+
additionalContexts: [],
|
|
66
|
+
injectedMessages: [],
|
|
67
|
+
errors: [],
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
it('round-trips a private manifest without exposing it publicly', () => {
|
|
74
|
+
const manifest = { version: 1 as const, executions: [execution] };
|
|
75
|
+
const payload = attachSubagentResumeManifest(
|
|
76
|
+
{ type: 'tool_approval', visible: true },
|
|
77
|
+
manifest
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(getSubagentResumeManifest(payload)).toEqual(manifest);
|
|
81
|
+
expect(stripSubagentResumeManifest(payload)).toEqual({
|
|
82
|
+
type: 'tool_approval',
|
|
83
|
+
visible: true,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('accepts legacy executions without a bound subagent type', () => {
|
|
88
|
+
const { subagentType: _subagentType, ...legacyExecution } = execution;
|
|
89
|
+
const manifest = { version: 1 as const, executions: [legacyExecution] };
|
|
90
|
+
const payload = attachSubagentResumeManifest(
|
|
91
|
+
{ type: 'tool_approval' },
|
|
92
|
+
manifest
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(getSubagentResumeManifest(payload)).toEqual(manifest);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it.each(['', 42, { type: 'researcher' }])(
|
|
99
|
+
'rejects malformed bound subagent type %#',
|
|
100
|
+
(subagentType) => {
|
|
101
|
+
const manifest = {
|
|
102
|
+
version: 1 as const,
|
|
103
|
+
executions: [{ ...execution, subagentType }],
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
expect(
|
|
107
|
+
getSubagentResumeManifest({
|
|
108
|
+
__librechat_subagent_resume_manifest: manifest,
|
|
109
|
+
})
|
|
110
|
+
).toBeUndefined();
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
it.each([
|
|
115
|
+
['string', 'approve this child'],
|
|
116
|
+
['number', 42],
|
|
117
|
+
['null', null],
|
|
118
|
+
['array', ['approve', { call: 1 }]],
|
|
119
|
+
])(
|
|
120
|
+
'round-trips a private manifest around a %s interrupt payload',
|
|
121
|
+
(_label, visiblePayload) => {
|
|
122
|
+
const manifest = { version: 1 as const, executions: [execution] };
|
|
123
|
+
const payload = attachSubagentResumeManifest(visiblePayload, manifest);
|
|
124
|
+
|
|
125
|
+
expect(getSubagentResumeManifest(payload)).toEqual(manifest);
|
|
126
|
+
expect(stripSubagentResumeManifest(payload)).toEqual(visiblePayload);
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
it.each([
|
|
131
|
+
['Map', new Map([['decision', 'approve']])],
|
|
132
|
+
['Set', new Set(['approve'])],
|
|
133
|
+
['RegExp', /^approve$/i],
|
|
134
|
+
])(
|
|
135
|
+
'round-trips a private manifest around a %s interrupt payload',
|
|
136
|
+
(_label, visiblePayload) => {
|
|
137
|
+
const manifest = { version: 1 as const, executions: [execution] };
|
|
138
|
+
const payload = attachSubagentResumeManifest(visiblePayload, manifest);
|
|
139
|
+
|
|
140
|
+
expect(getSubagentResumeManifest(payload)).toEqual(manifest);
|
|
141
|
+
expect(stripSubagentResumeManifest(payload)).toBe(visiblePayload);
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
it.each([
|
|
146
|
+
{
|
|
147
|
+
__librechat_subagent_resume_manifest: { visible: true },
|
|
148
|
+
value: 'manifest collision',
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
__librechat_subagent_resume_wrapper: 1,
|
|
152
|
+
__librechat_subagent_resume_payload: 'visible nested value',
|
|
153
|
+
value: 'wrapper collision',
|
|
154
|
+
},
|
|
155
|
+
])(
|
|
156
|
+
'preserves a custom payload containing private transport keys',
|
|
157
|
+
(value) => {
|
|
158
|
+
const manifest = { version: 1 as const, executions: [execution] };
|
|
159
|
+
const payload = attachSubagentResumeManifest(value, manifest);
|
|
160
|
+
|
|
161
|
+
expect(stripSubagentResumeManifest(value)).toBe(value);
|
|
162
|
+
expect(getSubagentResumeManifest(payload)).toEqual(manifest);
|
|
163
|
+
expect(stripSubagentResumeManifest(payload)).toBe(value);
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
it('updates the manifest on an existing inline payload', () => {
|
|
168
|
+
const firstManifest = { version: 1 as const, executions: [execution] };
|
|
169
|
+
const nextManifest = {
|
|
170
|
+
version: 1 as const,
|
|
171
|
+
executions: [{ ...execution, childRunId: 'next-inline-child-run' }],
|
|
172
|
+
};
|
|
173
|
+
const firstPayload = attachSubagentResumeManifest(
|
|
174
|
+
{ type: 'tool_approval', visible: true },
|
|
175
|
+
firstManifest
|
|
176
|
+
);
|
|
177
|
+
const nextPayload = attachSubagentResumeManifest(
|
|
178
|
+
firstPayload,
|
|
179
|
+
nextManifest
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
expect(getSubagentResumeManifest(nextPayload)).toEqual(nextManifest);
|
|
183
|
+
expect(stripSubagentResumeManifest(nextPayload)).toEqual({
|
|
184
|
+
type: 'tool_approval',
|
|
185
|
+
visible: true,
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('updates the manifest on an existing private wrapper', () => {
|
|
190
|
+
const firstManifest = { version: 1 as const, executions: [execution] };
|
|
191
|
+
const nextManifest = {
|
|
192
|
+
version: 1 as const,
|
|
193
|
+
executions: [{ ...execution, childRunId: 'next-child-run' }],
|
|
194
|
+
};
|
|
195
|
+
const firstPayload = attachSubagentResumeManifest(
|
|
196
|
+
'visible payload',
|
|
197
|
+
firstManifest
|
|
198
|
+
);
|
|
199
|
+
const nextPayload = attachSubagentResumeManifest(
|
|
200
|
+
firstPayload,
|
|
201
|
+
nextManifest
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
expect(getSubagentResumeManifest(nextPayload)).toEqual(nextManifest);
|
|
205
|
+
expect(stripSubagentResumeManifest(nextPayload)).toBe('visible payload');
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('rejects ambiguous or cross-execution replay data', () => {
|
|
209
|
+
const mismatchedScope = {
|
|
210
|
+
...execution,
|
|
211
|
+
approvalReplays: [
|
|
212
|
+
{
|
|
213
|
+
...execution.approvalReplays[0],
|
|
214
|
+
key: {
|
|
215
|
+
...execution.approvalReplays[0].key,
|
|
216
|
+
executionScope: 'different-child',
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
};
|
|
221
|
+
const duplicateParent = {
|
|
222
|
+
version: 1 as const,
|
|
223
|
+
executions: [execution, { ...execution }],
|
|
224
|
+
};
|
|
225
|
+
const duplicateToolCall = {
|
|
226
|
+
...execution,
|
|
227
|
+
graphState: {
|
|
228
|
+
...execution.graphState,
|
|
229
|
+
toolCallSteps: [
|
|
230
|
+
...execution.graphState.toolCallSteps,
|
|
231
|
+
{ ...execution.graphState.toolCallSteps[0] },
|
|
232
|
+
],
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
expect(
|
|
237
|
+
getSubagentResumeManifest(
|
|
238
|
+
attachSubagentResumeManifest(
|
|
239
|
+
{ type: 'tool_approval' },
|
|
240
|
+
{ version: 1, executions: [mismatchedScope] }
|
|
241
|
+
)
|
|
242
|
+
)
|
|
243
|
+
).toBeUndefined();
|
|
244
|
+
expect(
|
|
245
|
+
getSubagentResumeManifest(
|
|
246
|
+
attachSubagentResumeManifest({ type: 'tool_approval' }, duplicateParent)
|
|
247
|
+
)
|
|
248
|
+
).toBeUndefined();
|
|
249
|
+
expect(
|
|
250
|
+
getSubagentResumeManifest(
|
|
251
|
+
attachSubagentResumeManifest(
|
|
252
|
+
{ type: 'tool_approval' },
|
|
253
|
+
{ version: 1, executions: [duplicateToolCall] }
|
|
254
|
+
)
|
|
255
|
+
)
|
|
256
|
+
).toBeUndefined();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('fails closed for malformed nested references', () => {
|
|
260
|
+
const malformed = {
|
|
261
|
+
__librechat_subagent_resume_manifest: {
|
|
262
|
+
version: 1,
|
|
263
|
+
executions: [{ ...execution, checkpoints: [null] }],
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
expect(getSubagentResumeManifest(malformed)).toBeUndefined();
|
|
267
|
+
expect(() => requireValidSubagentResumeManifest(malformed)).toThrow(
|
|
268
|
+
'Invalid subagent resume manifest.'
|
|
269
|
+
);
|
|
270
|
+
expect(
|
|
271
|
+
getSubagentResumeManifest({
|
|
272
|
+
__librechat_subagent_resume_manifest: {
|
|
273
|
+
version: 1,
|
|
274
|
+
executions: [
|
|
275
|
+
{
|
|
276
|
+
...execution,
|
|
277
|
+
graphState: {
|
|
278
|
+
...execution.graphState,
|
|
279
|
+
toolCallSteps: [null],
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
],
|
|
283
|
+
},
|
|
284
|
+
})
|
|
285
|
+
).toBeUndefined();
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('rejects duplicate checkpoint namespaces and approval replay keys', () => {
|
|
289
|
+
const duplicateCheckpointNamespace = {
|
|
290
|
+
...execution,
|
|
291
|
+
checkpoints: [execution.checkpoints[0], { ...execution.checkpoints[0] }],
|
|
292
|
+
};
|
|
293
|
+
const duplicateApprovalReplay = {
|
|
294
|
+
...execution,
|
|
295
|
+
approvalReplays: [
|
|
296
|
+
execution.approvalReplays[0],
|
|
297
|
+
{ ...execution.approvalReplays[0] },
|
|
298
|
+
],
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
for (const invalidExecution of [
|
|
302
|
+
duplicateCheckpointNamespace,
|
|
303
|
+
duplicateApprovalReplay,
|
|
304
|
+
]) {
|
|
305
|
+
expect(
|
|
306
|
+
getSubagentResumeManifest(
|
|
307
|
+
attachSubagentResumeManifest(
|
|
308
|
+
{ type: 'tool_approval' },
|
|
309
|
+
{ version: 1, executions: [invalidExecution] }
|
|
310
|
+
)
|
|
311
|
+
)
|
|
312
|
+
).toBeUndefined();
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('rejects cyclic descendant manifests', () => {
|
|
317
|
+
const cyclic: {
|
|
318
|
+
version: number;
|
|
319
|
+
executions: Array<typeof execution & { descendant?: object }>;
|
|
320
|
+
} = { version: 1, executions: [{ ...execution }] };
|
|
321
|
+
cyclic.executions[0].descendant = cyclic;
|
|
322
|
+
|
|
323
|
+
expect(
|
|
324
|
+
getSubagentResumeManifest({
|
|
325
|
+
__librechat_subagent_resume_manifest: cyclic,
|
|
326
|
+
})
|
|
327
|
+
).toBeUndefined();
|
|
328
|
+
});
|
|
329
|
+
});
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { tool } from '@langchain/core/tools';
|
|
3
|
+
import { GraphInterrupt } from '@langchain/langgraph';
|
|
3
4
|
import { AIMessage, ToolMessage } from '@langchain/core/messages';
|
|
4
5
|
import { describe, it, expect, jest, afterEach } from '@jest/globals';
|
|
5
6
|
import type { StructuredToolInterface } from '@langchain/core/tools';
|
|
6
7
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
8
|
+
import type {
|
|
9
|
+
ReplayableSubagentTool,
|
|
10
|
+
SettledSubagentToolOutput,
|
|
11
|
+
} from '@/tools/subagent/SubagentReplay';
|
|
12
|
+
import type { PreToolUseHookOutput } from '@/hooks';
|
|
7
13
|
import type * as t from '@/types';
|
|
8
|
-
import * as events from '@/utils/events';
|
|
9
14
|
import {
|
|
10
15
|
StreamLimitExceededError,
|
|
11
16
|
RUN_BREAKER_SCOPE_CONFIG_KEY,
|
|
12
17
|
} from '@/llm/streamLimits';
|
|
18
|
+
import { SUBAGENT_REPLAY_CONTROLLER } from '@/tools/subagent/SubagentReplay';
|
|
19
|
+
import * as events from '@/utils/events';
|
|
13
20
|
import { GraphEvents } from '@/common';
|
|
14
21
|
import { HookRegistry } from '@/hooks';
|
|
15
22
|
import { ToolNode } from '../ToolNode';
|
|
@@ -212,6 +219,202 @@ describe('ToolNode breaker signal composition', () => {
|
|
|
212
219
|
expect(sideEffectRan).toBe(false);
|
|
213
220
|
});
|
|
214
221
|
|
|
222
|
+
it('prioritizes a sibling breaker trip over an approval interrupt', async () => {
|
|
223
|
+
const breaker = new AbortController();
|
|
224
|
+
const trip = new StreamLimitExceededError({
|
|
225
|
+
kind: 'tool_call_args',
|
|
226
|
+
limit: 10,
|
|
227
|
+
observed: 11,
|
|
228
|
+
toolName: 'db_query',
|
|
229
|
+
});
|
|
230
|
+
const approval = createSignalBlindTool('ask_question', async () => {
|
|
231
|
+
throw new GraphInterrupt([]);
|
|
232
|
+
});
|
|
233
|
+
const tripper = createSignalBlindTool('tripping_child', async () => {
|
|
234
|
+
breaker.abort(trip);
|
|
235
|
+
return 'tripped';
|
|
236
|
+
});
|
|
237
|
+
const node = new ToolNode({
|
|
238
|
+
tools: [approval, tripper],
|
|
239
|
+
interruptingToolNames: new Set(['ask_question', 'tripping_child']),
|
|
240
|
+
getBreakerSignal: () => breaker.signal,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
await expect(
|
|
244
|
+
node.invoke({
|
|
245
|
+
messages: [
|
|
246
|
+
new AIMessage({
|
|
247
|
+
content: '',
|
|
248
|
+
tool_calls: [
|
|
249
|
+
{ id: 'call_1', name: 'ask_question', args: {} },
|
|
250
|
+
{ id: 'call_2', name: 'tripping_child', args: {} },
|
|
251
|
+
],
|
|
252
|
+
}),
|
|
253
|
+
],
|
|
254
|
+
})
|
|
255
|
+
).rejects.toBe(trip);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('reuses terminal interrupting sibling outputs across replay', async () => {
|
|
259
|
+
let terminalRuns = 0;
|
|
260
|
+
let terminalPreHooks = 0;
|
|
261
|
+
let approvalRuns = 0;
|
|
262
|
+
const completions: Array<{
|
|
263
|
+
result?: { tool_call?: { id?: string; args?: string } };
|
|
264
|
+
}> = [];
|
|
265
|
+
jest
|
|
266
|
+
.spyOn(events, 'safeDispatchCustomEvent')
|
|
267
|
+
.mockImplementation(async (event, data): Promise<boolean> => {
|
|
268
|
+
if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {
|
|
269
|
+
completions.push(data as (typeof completions)[number]);
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
});
|
|
273
|
+
const hookRegistry = new HookRegistry();
|
|
274
|
+
hookRegistry.register('PreToolUse', {
|
|
275
|
+
hooks: [
|
|
276
|
+
async (input): Promise<PreToolUseHookOutput> => {
|
|
277
|
+
if (input.toolName !== 'terminal_child') {
|
|
278
|
+
return {};
|
|
279
|
+
}
|
|
280
|
+
terminalPreHooks += 1;
|
|
281
|
+
return {
|
|
282
|
+
additionalContext: 'cached terminal context',
|
|
283
|
+
updatedInput: { rewritten: true },
|
|
284
|
+
};
|
|
285
|
+
},
|
|
286
|
+
],
|
|
287
|
+
});
|
|
288
|
+
const terminal = createSignalBlindTool('terminal_child', async () => {
|
|
289
|
+
terminalRuns += 1;
|
|
290
|
+
return 'completed';
|
|
291
|
+
});
|
|
292
|
+
const approval = createSignalBlindTool('approval_child', async () => {
|
|
293
|
+
approvalRuns += 1;
|
|
294
|
+
if (approvalRuns === 1) {
|
|
295
|
+
throw new GraphInterrupt([
|
|
296
|
+
{ id: 'approval-interrupt', value: { type: 'approval' } },
|
|
297
|
+
]);
|
|
298
|
+
}
|
|
299
|
+
return 'approved';
|
|
300
|
+
});
|
|
301
|
+
const node = new ToolNode({
|
|
302
|
+
tools: [terminal, approval],
|
|
303
|
+
interruptingToolNames: new Set(['terminal_child', 'approval_child']),
|
|
304
|
+
hookRegistry,
|
|
305
|
+
toolCallStepIds: new Map([
|
|
306
|
+
['call_terminal', 'step_terminal'],
|
|
307
|
+
['call_approval', 'step_approval'],
|
|
308
|
+
]),
|
|
309
|
+
});
|
|
310
|
+
const input = {
|
|
311
|
+
messages: [
|
|
312
|
+
new AIMessage({
|
|
313
|
+
content: '',
|
|
314
|
+
tool_calls: [
|
|
315
|
+
{ id: 'call_terminal', name: 'terminal_child', args: {} },
|
|
316
|
+
{ id: 'call_approval', name: 'approval_child', args: {} },
|
|
317
|
+
],
|
|
318
|
+
}),
|
|
319
|
+
],
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
await expect(node.invoke(input)).rejects.toBeInstanceOf(GraphInterrupt);
|
|
323
|
+
const replayResult = await node.invoke(input);
|
|
324
|
+
expect(terminalRuns).toBe(1);
|
|
325
|
+
expect(terminalPreHooks).toBe(1);
|
|
326
|
+
expect(JSON.stringify(replayResult)).toContain('cached terminal context');
|
|
327
|
+
const terminalCompletion = completions.find(
|
|
328
|
+
(completion) => completion.result?.tool_call?.id === 'call_terminal'
|
|
329
|
+
);
|
|
330
|
+
expect(terminalCompletion?.result?.tool_call?.args).toContain(
|
|
331
|
+
'"rewritten":true'
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
await node.invoke({
|
|
335
|
+
messages: [
|
|
336
|
+
new AIMessage({
|
|
337
|
+
content: '',
|
|
338
|
+
tool_calls: [
|
|
339
|
+
{
|
|
340
|
+
id: 'call_terminal',
|
|
341
|
+
name: 'terminal_child',
|
|
342
|
+
args: { laterTurn: true },
|
|
343
|
+
type: 'tool_call',
|
|
344
|
+
},
|
|
345
|
+
],
|
|
346
|
+
}),
|
|
347
|
+
],
|
|
348
|
+
});
|
|
349
|
+
expect(terminalRuns).toBe(2);
|
|
350
|
+
expect(terminalPreHooks).toBe(2);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('persists a hook-terminal outcome before a sibling interrupts', async () => {
|
|
354
|
+
const persistedOutputs: SettledSubagentToolOutput[] = [];
|
|
355
|
+
const terminal = createSignalBlindTool('terminal_child', async () => {
|
|
356
|
+
throw new Error('denied tool must not execute');
|
|
357
|
+
}) as StructuredToolInterface & ReplayableSubagentTool;
|
|
358
|
+
terminal[SUBAGENT_REPLAY_CONTROLLER] = {
|
|
359
|
+
getSettledOutput: async () => undefined,
|
|
360
|
+
persistSettledOutput: async (_call, _config, settled): Promise<void> => {
|
|
361
|
+
persistedOutputs.push(settled);
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
const approval = createSignalBlindTool('approval_child', async () => {
|
|
365
|
+
throw new GraphInterrupt([
|
|
366
|
+
{ id: 'approval-interrupt', value: { type: 'approval' } },
|
|
367
|
+
]);
|
|
368
|
+
});
|
|
369
|
+
const hookRegistry = new HookRegistry();
|
|
370
|
+
hookRegistry.register('PreToolUse', {
|
|
371
|
+
hooks: [
|
|
372
|
+
async (input): Promise<PreToolUseHookOutput> =>
|
|
373
|
+
input.toolName === 'terminal_child'
|
|
374
|
+
? { decision: 'deny', reason: 'blocked by policy' }
|
|
375
|
+
: {},
|
|
376
|
+
],
|
|
377
|
+
});
|
|
378
|
+
const node = new ToolNode({
|
|
379
|
+
tools: [terminal, approval],
|
|
380
|
+
hookRegistry,
|
|
381
|
+
interruptingToolNames: new Set(['terminal_child', 'approval_child']),
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
await expect(
|
|
385
|
+
node.invoke({
|
|
386
|
+
messages: [
|
|
387
|
+
new AIMessage({
|
|
388
|
+
content: '',
|
|
389
|
+
tool_calls: [
|
|
390
|
+
{
|
|
391
|
+
id: 'call_terminal',
|
|
392
|
+
name: 'terminal_child',
|
|
393
|
+
args: {},
|
|
394
|
+
type: 'tool_call',
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
id: 'call_approval',
|
|
398
|
+
name: 'approval_child',
|
|
399
|
+
args: {},
|
|
400
|
+
type: 'tool_call',
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
}),
|
|
404
|
+
],
|
|
405
|
+
})
|
|
406
|
+
).rejects.toBeInstanceOf(GraphInterrupt);
|
|
407
|
+
|
|
408
|
+
expect(persistedOutputs).toHaveLength(1);
|
|
409
|
+
expect(persistedOutputs[0]).toMatchObject({
|
|
410
|
+
output: {
|
|
411
|
+
content: 'Blocked: blocked by policy',
|
|
412
|
+
status: 'error',
|
|
413
|
+
tool_call_id: 'call_terminal',
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
215
418
|
it('stops event dispatch when a direct tool trips the breaker mid-batch', async () => {
|
|
216
419
|
const breaker = new AbortController();
|
|
217
420
|
const trip = new StreamLimitExceededError({
|
|
@@ -323,7 +526,10 @@ describe('ToolNode breaker signal composition', () => {
|
|
|
323
526
|
});
|
|
324
527
|
|
|
325
528
|
it('stamps the batch-entry run scope into tool configs and strips it from host batches', async () => {
|
|
326
|
-
const scope = Object.freeze({
|
|
529
|
+
const scope = Object.freeze({
|
|
530
|
+
epoch: 3,
|
|
531
|
+
controller: new AbortController(),
|
|
532
|
+
});
|
|
327
533
|
let seenScope: unknown;
|
|
328
534
|
const capture = {
|
|
329
535
|
name: 'capture_scope',
|