🪓 feat: Cancel Background Tool Invocations (#15773)

* ✨ feat: Cancel ordinary background tools

* 🛡️ fix: Keep cancellation receipts rollout-safe

* 🛡️ fix: Harden Background Tool Cancellation

* 🧪 test: Cover cancellation config wiring

* 🎨 style: Sort cancellation imports

* 🧪 test: Align Cancellation Claim Result

* 🛑 fix: Complete Background Cancellation Lifecycle

* 🎨 style: Align Cancellation Test Formatting
This commit is contained in:
Danny Avila 2026-09-09 09:06:26 -04:00 • committed by GitHub
parent eaed216994
commit 559fd7d607
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 904 additions and 52 deletions

View file

@ -1281,6 +1281,7 @@ describe('OpenAIChatCompletionController', () => {
const { loadAgentTools, loadToolsForExecution } = require('~/server/services/ToolService');
const { filterFilesByAgentAccess } = require('~/server/services/Files/permissions');
req.config.endpoints.agents.backgroundTasks = { ordinaryToolCancellation: true };
await OpenAIChatCompletionController(req, res);
const [initializeParams, dbMethods] = initializeAgent.mock.calls.at(-1);
@ -1308,6 +1309,7 @@ describe('OpenAIChatCompletionController', () => {
);
const toolExecuteOptions = createToolExecuteHandler.mock.calls.at(-1)[0];
expect(toolExecuteOptions.ordinaryToolCancellation).toBe(true);
await toolExecuteOptions.loadTools(['file_search'], 'agent-123');
expect(loadToolsForExecution).toHaveBeenLastCalledWith(
expect.objectContaining({

View file

@ -1872,6 +1872,7 @@ describe('createResponse controller', () => {
const { loadAgentTools, loadToolsForExecution } = require('~/server/services/ToolService');
const { filterFilesByAgentAccess } = require('~/server/services/Files/permissions');
req.config.endpoints.agents.backgroundTasks = { ordinaryToolCancellation: true };
await createResponse(req, res);
const [initializeParams, dbMethods] = initializeAgent.mock.calls.at(-1);
@ -1899,6 +1900,7 @@ describe('createResponse controller', () => {
);
const toolExecuteOptions = createToolExecuteHandler.mock.calls.at(-1)[0];
expect(toolExecuteOptions.ordinaryToolCancellation).toBe(true);
await toolExecuteOptions.loadTools(['file_search'], 'agent-123');
expect(loadToolsForExecution).toHaveBeenLastCalledWith(
expect.objectContaining({

View file

@ -466,6 +466,8 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {
const agentsEConfig = appConfig?.endpoints?.[EModelEndpoint.agents];
const allowedProviders = new Set(agentsEConfig?.allowedProviders);
const ordinaryToolCancellationEnabled =
agentsEConfig?.backgroundTasks?.ordinaryToolCancellation === true;
// Create tool loader
const loadTools = createToolLoader({ req, res, signal: execution.signal });
@ -801,6 +803,7 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {
agent never gains sandbox access even if the admin enabled the
capability globally. */
const toolExecuteOptions = {
ordinaryToolCancellation: ordinaryToolCancellationEnabled,
provisionFiles: createProvisionFilesCallback({
req,
agentToolContexts,

View file

@ -703,6 +703,8 @@ const executeResponse = async (envelope, { req, res }) => {
codeWorkspaces: request.code_workspaces ?? req.resolvedConversation?.codeWorkspaces,
});
const agentsEConfig = appConfig?.endpoints?.[EModelEndpoint.agents];
const ordinaryToolCancellationEnabled =
agentsEConfig?.backgroundTasks?.ordinaryToolCancellation === true;
const previousMessages = request.previous_response_id
? await loadPreviousMessages(request.previous_response_id, principal.userId)
: [];
@ -1144,6 +1146,7 @@ const executeResponse = async (envelope, { req, res }) => {
// Create tool execute options for event-driven tool execution
const toolExecuteOptions = {
ordinaryToolCancellation: ordinaryToolCancellationEnabled,
provisionFiles: createProvisionFilesCallback({
req,
agentToolContexts,
@ -1372,6 +1375,7 @@ const executeResponse = async (envelope, { req, res }) => {
});
const toolExecuteOptions = {
ordinaryToolCancellation: ordinaryToolCancellationEnabled,
provisionFiles: createProvisionFilesCallback({
req,
agentToolContexts,

View file

@ -183,6 +183,9 @@ const initializeClient = async ({
const completionWakeupsEnabled = backgroundCompletionWakeupsEnabled(
appConfig?.endpoints?.[EModelEndpoint.agents],
);
const ordinaryToolCancellationEnabled =
appConfig?.endpoints?.[EModelEndpoint.agents]?.backgroundTasks?.ordinaryToolCancellation ===
true;
/** The normal controller resolves this once for timestamp anchoring. Reuse
* that trusted document for child-thread execution policy; resume and direct
* callers fall back to the same owner-scoped lookup. */
@ -407,6 +410,7 @@ const initializeClient = async ({
// is composed with this authoritative job signal by the handler.
runSignal: signal,
foregroundRunId: runtimeRequestBody?.messageId,
ordinaryToolCancellation: ordinaryToolCancellationEnabled,
loadTools: async (toolNames, agentId, _configurable, callerCapabilityProjection) => {
const ctx = agentToolContexts.get(agentId) ?? {};
logger.debug(`[ON_TOOL_EXECUTE] ctx found: ${!!ctx.userMCPAuthMap}, agent: ${ctx.agent?.id}`);

View file

@ -321,6 +321,31 @@ describe('initializeClient — processAgent ACL gate', () => {
expect(capturedToolExecuteOptions.foregroundRunId).toBe('response-1');
});
it('threads the deployment ordinary-tool cancellation gate into execution', async () => {
mockInitializeAgent.mockResolvedValue(makePrimaryConfig([]));
const disabledReq = makeReq();
await initializeClient({
req: disabledReq,
res: {},
signal: new AbortController().signal,
endpointOption: makeEndpointOption(),
});
expect(capturedToolExecuteOptions.ordinaryToolCancellation).toBe(false);
const enabledReq = makeReq();
enabledReq.config.endpoints.agents = {
backgroundTasks: { ordinaryToolCancellation: true },
};
await initializeClient({
req: enabledReq,
res: {},
signal: new AbortController().signal,
endpointOption: makeEndpointOption(),
});
expect(capturedToolExecuteOptions.ordinaryToolCancellation).toBe(true);
});
it('propagates an expected-MCP-tools failure from the runtime tool loader', async () => {
const toolError = Object.assign(new Error('Expected MCP tools are unavailable'), {
code: 'AGENT_EXPECTED_MCP_TOOLS_UNAVAILABLE',