diff --git a/.gitignore b/.gitignore index a63f4f940f..517f332fcf 100644 --- a/.gitignore +++ b/.gitignore @@ -81,7 +81,6 @@ librechat.yaml librechat.yml # Environment -.npmrc .env* my.secrets !**/.env.example diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000000..648b657940 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +allow-remote=root diff --git a/api/package.json b/api/package.json index bd3d311b49..3edf1ab57a 100644 --- a/api/package.json +++ b/api/package.json @@ -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", diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js index 19308dbdf2..44b57ce0d4 100644 --- a/api/server/controllers/agents/client.js +++ b/api/server/controllers/agents/client.js @@ -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]: diff --git a/package-lock.json b/package-lock.json index cff4efab10..83c979271a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/packages/api/package.json b/packages/api/package.json index 1bd3ee85d0..ccfe093d2d 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -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", diff --git a/packages/api/src/agents/hitl/runtime.spec.ts b/packages/api/src/agents/hitl/runtime.spec.ts index 053e5dcbf0..4ffbcd85b8 100644 --- a/packages/api/src/agents/hitl/runtime.spec.ts +++ b/packages/api/src/agents/hitl/runtime.spec.ts @@ -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'), + ); + }); +}); diff --git a/packages/api/src/agents/hitl/runtime.ts b/packages/api/src/agents/hitl/runtime.ts index e732a3b060..60dba4785f 100644 --- a/packages/api/src/agents/hitl/runtime.ts +++ b/packages/api/src/agents/hitl/runtime.ts @@ -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. * diff --git a/packages/api/src/stream/__tests__/pendingAction.spec.ts b/packages/api/src/stream/__tests__/pendingAction.spec.ts index 11270fa97b..33f2be2638 100644 --- a/packages/api/src/stream/__tests__/pendingAction.spec.ts +++ b/packages/api/src/stream/__tests__/pendingAction.spec.ts @@ -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');