mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-23 16:34:27 +00:00
🪔 feat: Expose Bounded Attached Bash Timeouts (#15776)
* 🕰️ feat: Add BYOM Bash Execution Timeouts * 🔧 fix: Type Bash Timeout Bounds * 🔒 fix: Bound BYOM Bash timeouts by policy * 🧪 test: Propagate BYOM timeout policy fixture
This commit is contained in:
parent
66faf4c27a
commit
9f311b810d
14 changed files with 289 additions and 21 deletions
|
|
@ -4,12 +4,16 @@ import {
|
|||
BashToolOutputReferencesGuide,
|
||||
createBashProgrammaticToolCallingTool,
|
||||
} from '@librechat/agents';
|
||||
import type { AgentGitIdentity, CodeEnvironmentUserConfigSchema } from 'librechat-data-provider';
|
||||
import type { DynamicStructuredTool } from '@librechat/agents/langchain/tools';
|
||||
import type { AgentGitIdentity } from 'librechat-data-provider';
|
||||
import type { LCTool } from '@librechat/agents';
|
||||
import type { WorkspaceExecuteCommandResult } from './workspace';
|
||||
import type { CodeBridgeFetch } from './bridge';
|
||||
import { executeWorkspaceTool } from './workspace';
|
||||
import {
|
||||
executeWorkspaceTool,
|
||||
WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS,
|
||||
WORKSPACE_COMMAND_MAX_TIMEOUT_MS,
|
||||
} from './workspace';
|
||||
|
||||
const DEFAULT_OUTPUT_BYTES = 256 * 1024;
|
||||
|
||||
|
|
@ -48,20 +52,64 @@ const attachedWorkingDirectorySchema: BoundedWorkingDirectorySchema = {
|
|||
'Optional working directory relative to the selected workspace root, such as "packages/api". Absolute paths and parent traversal are rejected.',
|
||||
};
|
||||
|
||||
/** Numeric bounds are valid JSON Schema, but the SDK's schema type omits them. */
|
||||
interface BoundedTimeoutSchema {
|
||||
type: 'integer';
|
||||
minimum: number;
|
||||
maximum: number;
|
||||
description: string;
|
||||
}
|
||||
|
||||
function buildAttachedTimeoutSchema(maxTimeoutMs: number): BoundedTimeoutSchema {
|
||||
const defaultTimeoutMs = Math.min(WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS, maxTimeoutMs);
|
||||
return {
|
||||
type: 'integer',
|
||||
minimum: 1,
|
||||
maximum: maxTimeoutMs,
|
||||
description: `Optional execution timeout in milliseconds, from 1 through ${maxTimeoutMs}. Defaults to ${defaultTimeoutMs}. Waiting for an available worker does not consume this execution budget.`,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeAttachedWorkspaceCommandTimeoutMax(maxTimeoutMs: number): number {
|
||||
if (!Number.isSafeInteger(maxTimeoutMs) || maxTimeoutMs < 1) {
|
||||
return WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS;
|
||||
}
|
||||
return Math.min(WORKSPACE_COMMAND_MAX_TIMEOUT_MS, maxTimeoutMs);
|
||||
}
|
||||
|
||||
export function resolveAttachedWorkspaceCommandTimeoutMax(
|
||||
configSchema?: CodeEnvironmentUserConfigSchema,
|
||||
): number {
|
||||
const configured = configSchema?.limits?.maxCommandTimeoutMs;
|
||||
return configured == null
|
||||
? WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS
|
||||
: normalizeAttachedWorkspaceCommandTimeoutMax(configured);
|
||||
}
|
||||
|
||||
export function buildAttachedWorkspaceBashSchema(
|
||||
maxTimeoutMs: number = WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS,
|
||||
): NonNullable<LCTool['parameters']> {
|
||||
const effectiveMaxTimeoutMs = normalizeAttachedWorkspaceCommandTimeoutMax(maxTimeoutMs);
|
||||
return {
|
||||
type: 'object',
|
||||
properties: {
|
||||
...bashSchema.properties,
|
||||
command: attachedCommandSchema,
|
||||
cwd: attachedWorkingDirectorySchema,
|
||||
timeoutMs: buildAttachedTimeoutSchema(effectiveMaxTimeoutMs),
|
||||
},
|
||||
required: ['command'],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This definition is shared with agent metadata. LangChain's JSON Schema
|
||||
* dereferencer annotates schemas during validation, so each tool receives an
|
||||
* isolated mutable clone instead of mutating this shared definition.
|
||||
*/
|
||||
export const ATTACHED_WORKSPACE_BASH_SCHEMA: NonNullable<LCTool['parameters']> = Object.freeze({
|
||||
type: 'object',
|
||||
properties: {
|
||||
...bashSchema.properties,
|
||||
command: attachedCommandSchema,
|
||||
cwd: attachedWorkingDirectorySchema,
|
||||
},
|
||||
required: ['command'],
|
||||
});
|
||||
export const ATTACHED_WORKSPACE_BASH_SCHEMA: NonNullable<LCTool['parameters']> = Object.freeze(
|
||||
buildAttachedWorkspaceBashSchema(),
|
||||
);
|
||||
|
||||
export function buildAttachedWorkspaceBashDescription(enableToolOutputReferences: boolean): string {
|
||||
return enableToolOutputReferences
|
||||
|
|
@ -130,23 +178,40 @@ export function createAttachedWorkspaceBashTool({
|
|||
authHeaders,
|
||||
workspaceId,
|
||||
gitIdentity,
|
||||
maxTimeoutMs = WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS,
|
||||
fetchImpl,
|
||||
}: {
|
||||
baseUrl: string;
|
||||
authHeaders: () => Promise<Record<string, string>> | Record<string, string>;
|
||||
workspaceId: string;
|
||||
gitIdentity?: AgentGitIdentity | null;
|
||||
/** Deployment ceiling already intersected with the protocol hard cap. */
|
||||
maxTimeoutMs?: number;
|
||||
fetchImpl?: CodeBridgeFetch;
|
||||
}): DynamicStructuredTool {
|
||||
const effectiveMaxTimeoutMs = normalizeAttachedWorkspaceCommandTimeoutMax(maxTimeoutMs);
|
||||
return tool(
|
||||
async (
|
||||
rawInput: { command: string; args?: string[]; cwd?: string; intent?: string },
|
||||
rawInput: {
|
||||
command: string;
|
||||
args?: string[];
|
||||
cwd?: string;
|
||||
timeoutMs?: number;
|
||||
intent?: string;
|
||||
},
|
||||
config,
|
||||
): Promise<[string, Record<string, never>]> => {
|
||||
if (rawInput.timeoutMs != null && rawInput.timeoutMs > effectiveMaxTimeoutMs) {
|
||||
throw new Error(
|
||||
`Command timeout exceeds the deployment limit of ${effectiveMaxTimeoutMs} milliseconds.`,
|
||||
);
|
||||
}
|
||||
const command = commandWithGitIdentity(
|
||||
commandWithArguments(rawInput.command, rawInput.args),
|
||||
gitIdentity,
|
||||
);
|
||||
const timeoutMs =
|
||||
rawInput.timeoutMs ?? Math.min(WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS, effectiveMaxTimeoutMs);
|
||||
const result = await executeWorkspaceTool({
|
||||
baseURL: baseUrl,
|
||||
authHeaders: await authHeaders(),
|
||||
|
|
@ -156,6 +221,7 @@ export function createAttachedWorkspaceBashTool({
|
|||
workspaceId,
|
||||
command,
|
||||
...(rawInput.cwd ? { cwd: rawInput.cwd } : {}),
|
||||
timeoutMs,
|
||||
maxOutputBytes: DEFAULT_OUTPUT_BYTES,
|
||||
},
|
||||
signal: config?.signal,
|
||||
|
|
@ -169,7 +235,7 @@ export function createAttachedWorkspaceBashTool({
|
|||
{
|
||||
name: BashExecutionToolDefinition.name,
|
||||
description: ATTACHED_WORKSPACE_BASH_DESCRIPTION,
|
||||
schema: structuredClone(ATTACHED_WORKSPACE_BASH_SCHEMA),
|
||||
schema: structuredClone(buildAttachedWorkspaceBashSchema(effectiveMaxTimeoutMs)),
|
||||
responseFormat: 'content_and_artifact',
|
||||
},
|
||||
) as unknown as DynamicStructuredTool;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue