⛔ fix: Bind Tool Approvals to Generation Scope (#15724)

* fix: bind tool approvals to generation scope

* fix: gate generation scope on replay lifecycle support

* chore: add npmrc for npm v12 support, fix xlsx tarball dl

* chore: bump agents sdk to v3.8.3

* fix: require scoped approval replay support
This commit is contained in:
Danny Avila 2026-09-08 08:02:48 -04:00 • committed by GitHub
parent 1dfefc3037
commit ba12eaa418
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 74 additions and 10 deletions

1
.gitignore vendored
View file

@ -81,7 +81,6 @@ librechat.yaml
librechat.yml
# Environment
.npmrc
.env*
my.secrets
!**/.env.example

1
.npmrc Normal file
View file

@ -0,0 +1 @@
allow-remote=root

View file

@ -46,7 +46,7 @@
"@azure/storage-blob": "^12.30.0",
"@google/genai": "^2.8.0",
"@keyv/redis": "5.1.6",
"@librechat/agents": "^3.8.2",
"@librechat/agents": "^3.8.3",
"@librechat/api": "*",
"@librechat/data-schemas": "*",
"@microsoft/microsoft-graph-client": "^3.0.7",

View file

@ -53,6 +53,7 @@ const {
isHITLEnabled,
resolveToolApprovalPolicy,
buildToolApprovalHooks,
buildToolApprovalExecutionConfig,
collectAttachedCodeEnvironmentAgentIds,
collectAttachedCodeEnvironmentPolicySettings,
buildAttachedCodeEnvironmentAdmissionHooks,
@ -4452,6 +4453,7 @@ class AgentClient extends BaseClient {
runName: 'AgentRun',
configurable: {
thread_id: this.conversationId,
...buildToolApprovalExecutionConfig(this.responseMessageId, this.jobCreatedAt),
// LangGraph owns `checkpoint_ns` and resets it to '' at every root
// invocation. The saver maps this private immutable generation key
// into its physical namespace while tools keep the conversation id.
@ -5243,6 +5245,7 @@ class AgentClient extends BaseClient {
runName: 'AgentRun',
configurable: {
thread_id: this.conversationId,
...buildToolApprovalExecutionConfig(this.responseMessageId, this.jobCreatedAt),
checkpoint_ns: '',
[LIBRECHAT_CHECKPOINT_NAMESPACE_KEY]: this.checkpointNamespace,
[LIBRECHAT_CHECKPOINT_STORAGE_OWNER_KEY]:

10
package-lock.json generated
View file

@ -64,7 +64,7 @@
"@azure/storage-blob": "^12.30.0",
"@google/genai": "^2.8.0",
"@keyv/redis": "5.1.6",
"@librechat/agents": "^3.8.2",
"@librechat/agents": "^3.8.3",
"@librechat/api": "*",
"@librechat/data-schemas": "*",
"@microsoft/microsoft-graph-client": "^3.0.7",
@ -10636,9 +10636,9 @@
}
},
"node_modules/@librechat/agents": {
"version": "3.8.2",
"resolved": "https://registry.npmjs.org/@librechat/agents/-/agents-3.8.2.tgz",
"integrity": "sha512-F41fY3HZJGpGFB6WCVLgsbMOxNQpxWKgsk13kRHcMJtnhE7DyTu9P78h7/3ruN+UoS8eTtWBNcOGgh2MglC9yw==",
"version": "3.8.3",
"resolved": "https://registry.npmjs.org/@librechat/agents/-/agents-3.8.3.tgz",
"integrity": "sha512-PlLPYIJnL82sL+3SKHGwT4YmsbT39o1BF3V1T60Nh+CKhNBuO0BfnrvLOTmflAA70WjYXPHN/c9Wz+GPud6Lzg==",
"license": "MIT",
"dependencies": {
"@anthropic-ai/sdk": "^0.115.0",
@ -43645,7 +43645,7 @@
"@azure/storage-blob": "^12.30.0",
"@google/genai": "^2.8.0",
"@keyv/redis": "5.1.6",
"@librechat/agents": "^3.8.2",
"@librechat/agents": "^3.8.3",
"@librechat/data-schemas": "*",
"@modelcontextprotocol/sdk": "^1.30.0",
"@opentelemetry/api": "^1.9.0",

View file

@ -114,7 +114,7 @@
"@azure/storage-blob": "^12.30.0",
"@google/genai": "^2.8.0",
"@keyv/redis": "5.1.6",
"@librechat/agents": "^3.8.2",
"@librechat/agents": "^3.8.3",
"@librechat/data-schemas": "*",
"@modelcontextprotocol/sdk": "^1.30.0",
"@opentelemetry/api": "^1.9.0",

View file

@ -1,8 +1,8 @@
import { HookRegistry, executeHooks } from '@librechat/agents';
import { buildHITLRunWiring, buildToolApprovalExecutionConfig } from './runtime';
import { registerToolApprovalHook, clearToolApprovalHooks } from './hooks';
import { createAttachedCodeEnvironmentPolicyHook } from './byom';
import { resolveToolApprovalPolicy } from './policy';
import { buildHITLRunWiring } from './runtime';
describe('buildHITLRunWiring', () => {
test.each([
@ -214,3 +214,33 @@ describe('buildHITLRunWiring host-hook composition', () => {
expect(wiring?.hooks.getMatchers('PreToolUse')).toHaveLength(2);
});
});
describe('tool approval execution scope', () => {
test('reconstructs the same scope for repeated approval resumes', () => {
const generation = { responseMessageId: 'response-1', jobCreatedAt: 1000 };
const original = buildToolApprovalExecutionConfig(
generation.responseMessageId,
generation.jobCreatedAt,
);
const restored = JSON.parse(JSON.stringify(generation)) as typeof generation;
expect(
buildToolApprovalExecutionConfig(restored.responseMessageId, restored.jobCreatedAt),
).toEqual(original);
expect(Object.values(original)[0]).toBeTruthy();
});
test('separates new generations even when an edit reuses the response id', () => {
const original = buildToolApprovalExecutionConfig('response-1', 1000);
expect(buildToolApprovalExecutionConfig('response-1', 1001)).not.toEqual(original);
expect(buildToolApprovalExecutionConfig('response-2', 1000)).not.toEqual(original);
});
test('uses the response id for runs without a generation job', () => {
expect(buildToolApprovalExecutionConfig('response-1')).toEqual(
buildToolApprovalExecutionConfig('response-1'),
);
expect(buildToolApprovalExecutionConfig('response-2')).not.toEqual(
buildToolApprovalExecutionConfig('response-1'),
);
});
});

View file

@ -1,10 +1,27 @@
import { HookRegistry, createToolPolicyHook } from '@librechat/agents';
import {
HookRegistry,
createToolPolicyHook,
TOOL_APPROVAL_EXECUTION_SCOPE_CONFIG_KEY,
} from '@librechat/agents';
import type { TToolApprovalPolicy } from 'librechat-data-provider';
import type { ResolvedToolApprovalHook, ToolApprovalHookContext } from './hooks';
import type { MCPToolAlias } from '~/tools/classification';
import { isHITLEnabled, mapToolApprovalPolicy } from './policy';
import { buildToolApprovalHooks } from './hooks';
/** Stable across resumes; the job epoch separates edits that reuse a response id. */
export function buildToolApprovalExecutionConfig(
responseMessageId: string,
jobCreatedAt?: number,
): { [TOOL_APPROVAL_EXECUTION_SCOPE_CONFIG_KEY]: string } {
return {
[TOOL_APPROVAL_EXECUTION_SCOPE_CONFIG_KEY]: JSON.stringify([
responseMessageId,
jobCreatedAt ?? null,
]),
};
}
/**
* The HITL fragment spread onto a `RunConfig` when tool approval is enabled.
*

View file

@ -659,6 +659,20 @@ describe('ApprovalLifecycle via GenerationJobManager.approvals (in-memory)', ()
expect(await manager.approvals.pause('nonexistent', buildAction('nonexistent'))).toBe(false);
});
test('replacing a paused generation removes its approval and rejects a late decision', async () => {
const streamId = 'stream-replace-paused-generation';
const predecessor = await manager.createJob(streamId, 'user-1');
const action = buildAction(streamId);
await manager.approvals.pause(streamId, action);
const replacement = await manager.createJob(streamId, 'user-1');
expect(replacement.createdAt).not.toBe(predecessor.createdAt);
expect(await manager.approvals.peek(streamId)).toBeNull();
expect((await manager.getResumeState(streamId))?.pendingAction).toBeUndefined();
expect(await manager.approvals.resolve(streamId, action.actionId)).toBe(false);
expect(await manager.getJobStatus(streamId)).toBe('running');
});
test('a predecessor interrupt cannot pause a replacement generation', async () => {
const streamId = 'stream-pause-epoch-fence';
const predecessor = await manager.createJob(streamId, 'user-1');