mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 23:13:35 +00:00
🪀 fix: Preserve Coding Approval Mode on Background Returns (#16322)
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
56301c1b5a
commit
edddf92ead
6 changed files with 142 additions and 5 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { AGENT_TRIGGER_WORKER_CAPABILITY_BACKGROUND_COMPLETION_RECEIPT_V2 } from '@librechat/data-schemas';
|
||||
import type { AgentTriggerProducerLeaseStatus } from '@librechat/data-schemas';
|
||||
import type { CodeApprovalMode } from 'librechat-data-provider';
|
||||
import type { EnqueueBackgroundToolCompletion } from './backgroundCompletionWakeup';
|
||||
import {
|
||||
BACKGROUND_TOOL_WAKEUP_INPUT_MAX_CHARS,
|
||||
|
|
@ -45,12 +46,15 @@ function envelope(registrationOverrides = {}) {
|
|||
});
|
||||
}
|
||||
|
||||
function resolverMethods() {
|
||||
function resolverMethods(codeApprovalMode?: CodeApprovalMode) {
|
||||
const releaseBackgroundToolResultClaims = jest.fn(async () => true);
|
||||
return {
|
||||
releaseBackgroundToolResultClaims,
|
||||
methods: {
|
||||
getConvo: jest.fn(async () => ({ tenantId: 'tenant-1' })),
|
||||
getConvo: jest.fn(async () => ({
|
||||
tenantId: 'tenant-1',
|
||||
...(codeApprovalMode != null && { codeApprovalMode }),
|
||||
})),
|
||||
getMessages: jest.fn(async () => [
|
||||
{
|
||||
messageId: 'response-1',
|
||||
|
|
@ -440,6 +444,72 @@ describe('background tool completion wakeups', () => {
|
|||
expect(retire).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe.each(['projection', 'receipt', 'legacy receipt'] as const)(
|
||||
'%s approval context',
|
||||
(source) => {
|
||||
it.each([undefined, 'ask', 'acceptEdits', 'fullAccess'] as const)(
|
||||
'inherits the parent mode %s without granting broader access',
|
||||
async (mode) => {
|
||||
const { methods } = resolverMethods(mode);
|
||||
if (source !== 'projection') {
|
||||
methods.claimBackgroundToolResults.mockResolvedValue({
|
||||
status: 'not_ready',
|
||||
results: [],
|
||||
});
|
||||
}
|
||||
if (source === 'receipt') {
|
||||
methods.claimAgentBackgroundToolResults.mockResolvedValue({
|
||||
status: 'acquired',
|
||||
results: [
|
||||
{
|
||||
taskId: 'task-1',
|
||||
toolCallId: 'call-1',
|
||||
toolName: 'slow_tool',
|
||||
status: 'completed',
|
||||
output: 'done',
|
||||
},
|
||||
],
|
||||
} as never);
|
||||
}
|
||||
if (source === 'legacy receipt') {
|
||||
Reflect.deleteProperty(methods, 'claimAgentBackgroundToolResults');
|
||||
methods.getAgentBackgroundToolResult.mockResolvedValue({
|
||||
status: 'completed',
|
||||
output: 'done',
|
||||
settledAt: new Date(NOW),
|
||||
} as never);
|
||||
}
|
||||
const resolve = createBackgroundToolCompletionWakeupResolver({
|
||||
methods: methods as never,
|
||||
getGenerationJob: async () => null,
|
||||
});
|
||||
|
||||
const prepared = await resolve(await envelope(), { idempotencyKey: 'delivery-1' });
|
||||
|
||||
expect(prepared?.status).toBe('ready');
|
||||
expect(prepared?.status === 'ready' && prepared.codeApprovalMode).toBe(mode);
|
||||
expect(methods.getConvo).toHaveBeenCalledTimes(1);
|
||||
expect(methods.getConvo).toHaveBeenCalledWith('user-1', 'conversation-1');
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it('rereads the parent approval mode on a delivery retry', async () => {
|
||||
const { methods } = resolverMethods('fullAccess');
|
||||
const resolve = createBackgroundToolCompletionWakeupResolver({
|
||||
methods: methods as never,
|
||||
getGenerationJob: async () => null,
|
||||
});
|
||||
const delivery = await envelope();
|
||||
const first = await resolve(delivery, { idempotencyKey: 'delivery-1' });
|
||||
expect(first?.status === 'ready' && first.codeApprovalMode).toBe('fullAccess');
|
||||
|
||||
methods.getConvo.mockResolvedValue({ tenantId: 'tenant-1', codeApprovalMode: 'ask' });
|
||||
const retry = await resolve(delivery, { idempotencyKey: 'delivery-1' });
|
||||
expect(retry?.status === 'ready' && retry.codeApprovalMode).toBe('ask');
|
||||
});
|
||||
|
||||
it('claims a bounded sibling batch and continues from the latest branch leaf', async () => {
|
||||
const { methods } = resolverMethods();
|
||||
const resolve = createBackgroundToolCompletionWakeupResolver({
|
||||
|
|
|
|||
|
|
@ -386,6 +386,7 @@ export function createBackgroundToolCompletionWakeupResolver({
|
|||
return {
|
||||
status: 'ready',
|
||||
parentMessageId,
|
||||
...(parent.codeApprovalMode != null && { codeApprovalMode: parent.codeApprovalMode }),
|
||||
input,
|
||||
releaseOnDefiniteFailure: async () => {
|
||||
const released = await methods.releaseBackgroundToolResultClaims({
|
||||
|
|
@ -455,6 +456,7 @@ export function createBackgroundToolCompletionWakeupResolver({
|
|||
return {
|
||||
status: 'ready',
|
||||
parentMessageId,
|
||||
...(parent.codeApprovalMode != null && { codeApprovalMode: parent.codeApprovalMode }),
|
||||
input: buildWakeupInput(receiptClaim.results),
|
||||
releaseOnDefiniteFailure: async () => {
|
||||
const projectionReleased = await methods.releaseBackgroundToolResultClaims({
|
||||
|
|
@ -490,6 +492,7 @@ export function createBackgroundToolCompletionWakeupResolver({
|
|||
return {
|
||||
status: 'ready',
|
||||
parentMessageId,
|
||||
...(parent.codeApprovalMode != null && { codeApprovalMode: parent.codeApprovalMode }),
|
||||
input: buildWakeupInput([
|
||||
{ ...registration, status: receipt.status, output: receipt.output },
|
||||
]),
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import type { CodeApprovalMode } from 'librechat-data-provider';
|
||||
import type { IMessage } from '@librechat/data-schemas';
|
||||
import type { AgentContinueTriggerEnvelope } from './triggers/envelope';
|
||||
import type { SubagentTaskWakeupRegistration } from './subagentThreads';
|
||||
|
|
@ -153,7 +154,7 @@ function wakeupEnvelope(): AgentContinueTriggerEnvelope {
|
|||
return envelope;
|
||||
}
|
||||
|
||||
function resolverMethods() {
|
||||
function resolverMethods(codeApprovalMode?: CodeApprovalMode) {
|
||||
const subagentTask: IMessage['subagentTask'] = {
|
||||
attemptKey: 'attempt-1',
|
||||
parentRunId: 'response-1',
|
||||
|
|
@ -173,7 +174,11 @@ function resolverMethods() {
|
|||
const methods = {
|
||||
getConvo: jest.fn(async (_userId: string, conversationId: string) =>
|
||||
conversationId === 'conversation-1'
|
||||
? { conversationId, tenantId: 'tenant-1' }
|
||||
? {
|
||||
conversationId,
|
||||
tenantId: 'tenant-1',
|
||||
...(codeApprovalMode != null && { codeApprovalMode }),
|
||||
}
|
||||
: {
|
||||
conversationId,
|
||||
tenantId: 'tenant-1',
|
||||
|
|
@ -259,6 +264,27 @@ function orchestrationSnapshot(
|
|||
}
|
||||
|
||||
describe('createSubagentCompletionWakeupResolver', () => {
|
||||
it.each([undefined, 'ask', 'acceptEdits', 'fullAccess'] as const)(
|
||||
'inherits parent approval mode %s rather than child or event permissions',
|
||||
async (mode) => {
|
||||
const { methods } = resolverMethods(mode);
|
||||
const resolve = createSubagentCompletionWakeupResolver({
|
||||
methods: methods as never,
|
||||
getGenerationJob: async () => null,
|
||||
});
|
||||
const delivery = wakeupEnvelope();
|
||||
if (delivery.event.payload == null || typeof delivery.event.payload !== 'object') {
|
||||
throw new Error('Expected a completion payload');
|
||||
}
|
||||
delivery.event.payload = { ...delivery.event.payload, codeApprovalMode: 'fullAccess' };
|
||||
|
||||
const prepared = await resolve(delivery, { idempotencyKey: 'delivery-1' });
|
||||
|
||||
expect(prepared?.status).toBe('ready');
|
||||
expect(prepared?.status === 'ready' && prepared.codeApprovalMode).toBe(mode);
|
||||
},
|
||||
);
|
||||
|
||||
it('defers without claiming while the parent generation is active', async () => {
|
||||
const { methods } = resolverMethods();
|
||||
const resolve = createSubagentCompletionWakeupResolver({
|
||||
|
|
|
|||
|
|
@ -778,6 +778,7 @@ export function createSubagentCompletionWakeupResolver({
|
|||
return {
|
||||
status: 'ready',
|
||||
parentMessageId,
|
||||
...(parent.codeApprovalMode != null && { codeApprovalMode: parent.codeApprovalMode }),
|
||||
input: renderWakeupInput(registration, resultTaskId, claim.message, orchestrationSnapshot),
|
||||
releaseOnDefiniteFailure: async () => {
|
||||
await methods.releaseSubagentTaskResultClaim({
|
||||
|
|
|
|||
|
|
@ -588,6 +588,37 @@ describe('createAgentTriggerExecutionHost continue adapter', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it.each([undefined, 'ask', 'acceptEdits', 'fullAccess'] as const)(
|
||||
'forwards prepared coding mode %s without trusting the event payload',
|
||||
async (mode) => {
|
||||
const envelope = createContinueEnvelope();
|
||||
envelope.event.payload = { codeApprovalMode: 'fullAccess' };
|
||||
const fetcher = fetchMock(async () =>
|
||||
response({
|
||||
streamId: 'conversation-1',
|
||||
conversationId: 'conversation-1',
|
||||
status: 'started',
|
||||
}),
|
||||
);
|
||||
const host = createAgentTriggerExecutionHost(
|
||||
deps(fetcher, {
|
||||
prepareContinue: async () => ({
|
||||
status: 'ready',
|
||||
input: 'durable result',
|
||||
parentMessageId: 'response-1',
|
||||
codeApprovalMode: mode,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
await host.dispatch(envelope);
|
||||
|
||||
const body = JSON.parse(String(fetcher.mock.calls[0][1]?.body));
|
||||
expect(body.codeApprovalMode).toBe(mode);
|
||||
if (mode === undefined) expect(body).not.toHaveProperty('codeApprovalMode');
|
||||
},
|
||||
);
|
||||
|
||||
it('carries a prepared queued-turn payload and settles it after admission', async () => {
|
||||
const envelope = createContinueEnvelope();
|
||||
const admitted = {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { logger, tenantStorage } from '@librechat/data-schemas';
|
||||
import { Constants, EModelEndpoint } from 'librechat-data-provider';
|
||||
import type { TFile } from 'librechat-data-provider';
|
||||
import type { CodeApprovalMode, TFile } from 'librechat-data-provider';
|
||||
import type {
|
||||
AgentContinueTriggerEnvelope,
|
||||
AgentFireTriggerEnvelope,
|
||||
|
|
@ -58,6 +58,9 @@ export type AgentTriggerContinuePreparation =
|
|||
files?: Partial<TFile>[];
|
||||
quotes?: string[];
|
||||
manualSkills?: string[];
|
||||
/** Parent-selected coding preference for a completion turn. Admission
|
||||
* revalidates it against live policy; event payloads cannot supply it. */
|
||||
codeApprovalMode?: CodeApprovalMode;
|
||||
/** Trusted source identity committed by execution enrollment before the
|
||||
* provider-start fence opens. */
|
||||
admissionSource?: AgentContinuationAdmissionSource;
|
||||
|
|
@ -668,6 +671,9 @@ async function startRun(
|
|||
...(readyPreparation?.quotes != null && {
|
||||
quotes: readyPreparation.quotes,
|
||||
}),
|
||||
...(readyPreparation?.codeApprovalMode != null && {
|
||||
codeApprovalMode: readyPreparation.codeApprovalMode,
|
||||
}),
|
||||
...(readyPreparation?.manualSkills != null && {
|
||||
manualSkills: readyPreparation.manualSkills,
|
||||
}),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue