Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/configurable-parallel-subagents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@truefoundry/trueforge-core': patch
'@truefoundry/trueforge': patch
---

Make the per-turn sub-agent concurrency limit configurable with `MAX_PARALLEL_SUB_AGENTS`, defaulting to 10.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface ResolvedAgentDefinition {
* per-request state (secrets, providers, caches) freely.
*/
export interface ITurnResourceResolver<TTurnCustom extends object = Record<string, never>> {
/** Maximum sub-agent threads executed in parallel per turn. Defaults to 10. */
readonly maxParallelSubAgents?: number;
/**
* Host web-search backend. When set and `config.web_search.enabled`, builtins
* register search/fetch system tools. Omit when the host has no provider configured.
Expand Down
2 changes: 2 additions & 0 deletions packages/trueforge-core/src/agent-session/SessionHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,10 @@ export class SessionHandle<
});

const previousMcpServers = input.previous?.snapshot.mcp_servers;
const maxParallelSubAgents = input.resolver.maxParallelSubAgents;
const orchestrator = new AgentThreadOrchestrator({
agentThreads,
...(maxParallelSubAgents !== undefined ? { maxParallelSubAgents } : {}),
createDynamicSubAgentThread,
tracing,
logger: input.resolver.logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class TurnResourceResolver<
readonly #sources = new Map<string, Promise<ToolSource>>();
readonly #models = new Map<string, Promise<ResolvedModel>>();
#sandbox?: HarnessSandbox | undefined;
readonly maxParallelSubAgents?: number;

constructor(
protected readonly deps: {
Expand All @@ -97,6 +98,7 @@ export class TurnResourceResolver<
mcpRequestTimeoutMs: number;
mcpConnectTimeoutMs: number;
mcpMaxResponseBytes?: number | undefined;
maxParallelSubAgents?: number;
/** One sandbox type per runtime. Omit = no sandbox support. */
sandboxProvider?: TurnSandboxFactory | undefined;
/**
Expand All @@ -115,7 +117,11 @@ export class TurnResourceResolver<
/** Forwarded to RemoteMCP / AgentThread (required by their constructors). */
logger: Logger;
},
) {}
) {
if (deps.maxParallelSubAgents !== undefined) {
this.maxParallelSubAgents = deps.maxParallelSubAgents;
}
}

get webSearchProvider(): IWebSearchProvider | undefined {
return this.deps.webSearchProvider;
Expand Down
2 changes: 1 addition & 1 deletion packages/trueforge-core/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type {
AgentThreadExecutionResult,
SubAgentCompletion,
} from './runtime/AgentThread.types';
export { AgentThreadOrchestrator } from './runtime/AgentThreadOrchestrator';
export { AgentThreadOrchestrator, DEFAULT_MAX_PARALLEL_SUB_AGENTS } from './runtime/AgentThreadOrchestrator';
export type { CreateDynamicSubAgentThread } from './runtime/CreateDynamicSubAgentThread';
export { isAgentUserMessage, isEmptyMessageContent, isFileContentPart } from './runtime/UserInputMessage';
export type { UserMessage } from './runtime/UserInputMessage';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
import type { CreateDynamicSubAgentThread } from './CreateDynamicSubAgentThread';
import { addAgentThreadMetrics, createEmptyAgentThreadMetrics, type AgentThreadMetrics } from './metrics';

const MAX_PARALLEL_SUB_AGENTS = 5;
export const DEFAULT_MAX_PARALLEL_SUB_AGENTS = 10;
Comment thread
thesujai marked this conversation as resolved.
Resolved
Comment thread
thesujai marked this conversation as resolved.
Resolved
const CANCELED_BECAUSE_USER_SENT_NEW_MESSAGE = 'Canceled because user sent a new message.';

function getActiveAgentThreads(agentThreads: Map<string, AgentThread>): AgentThread[] {
Expand Down Expand Up @@ -193,6 +193,7 @@ export async function* wrapWithSubAgentSpan(

export interface AgentThreadOrchestratorInput {
agentThreads: Map<string, AgentThread>;
maxParallelSubAgents?: number;
createDynamicSubAgentThread: CreateDynamicSubAgentThread;
tracing: AgentTracing;
logger: Logger;
Expand All @@ -207,6 +208,7 @@ export interface AgentThreadOrchestratorInput {

export class AgentThreadOrchestrator {
private readonly agentThreads: Map<string, AgentThread>;
private readonly maxParallelSubAgents: number;
private readonly createDynamicSubAgentThread: CreateDynamicSubAgentThread;
private readonly tracing: AgentTracing;
private readonly logger: Logger;
Expand All @@ -218,6 +220,7 @@ export class AgentThreadOrchestrator {
private readonly mcpServerInitInfoById = new Map<string, MCPServerInitInfo>();

constructor(params: AgentThreadOrchestratorInput) {
this.maxParallelSubAgents = params.maxParallelSubAgents ?? DEFAULT_MAX_PARALLEL_SUB_AGENTS;
this.agentThreads = params.agentThreads;
this.createDynamicSubAgentThread = params.createDynamicSubAgentThread;
this.tracing = params.tracing;
Expand Down Expand Up @@ -694,8 +697,8 @@ export class AgentThreadOrchestrator {
turnPaused = false;
}

for (let i = 0; i < runnable.length; i += MAX_PARALLEL_SUB_AGENTS) {
const batch = runnable.slice(i, i + MAX_PARALLEL_SUB_AGENTS);
for (let i = 0; i < runnable.length; i += this.maxParallelSubAgents) {
const batch = runnable.slice(i, i + this.maxParallelSubAgents);
const generators = batch.map(thread =>
thread.parent
? wrapWithSubAgentSpan(rootSpan.trace, thread, signal)
Expand Down
2 changes: 2 additions & 0 deletions packages/trueforge/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ REDIS_URL=redis://localhost:6379
# REDIS_REQUEST_REPLY_POLL_INTERVAL_MS=500

## Turn execution and resumable stream settings. All optional.
## Max sub-agents running in parallel per turn. Must be a positive integer. Default 10.
# MAX_PARALLEL_SUB_AGENTS=10
## Max seconds a single turn may run before cancellation. Default 600 (10 minutes).
# SERVER_EXECUTION_TIMEOUT_SECONDS=600
## Max seconds to wait for turn cancellation + connection drain on SIGTERM/SIGINT. Default 30.
Expand Down
1 change: 1 addition & 0 deletions packages/trueforge/src/apis/turns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ function createTurnResolver(deps: {
mcpRequestTimeoutMs: configuration.MCP_REQUEST_TIMEOUT_MS,
mcpConnectTimeoutMs: configuration.MCP_CONNECT_TIMEOUT_MS,
mcpMaxResponseBytes: configuration.MCP_TOOL_CALL_MAX_RESPONSE_BYTES,
maxParallelSubAgents: configuration.MAX_PARALLEL_SUB_AGENTS,
sandboxProvider: async ({ spec, existingSandboxId, tracing }) => {
const provider = await resolveSandboxProvider({
tenant_id,
Expand Down
8 changes: 8 additions & 0 deletions packages/trueforge/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { DEFAULT_MAX_PARALLEL_SUB_AGENTS } from '@truefoundry/trueforge-core/core';
import envPaths from 'env-paths';
import { z } from 'zod';

Expand Down Expand Up @@ -788,6 +789,8 @@ export interface SharedServerConfiguration {
* Default 3600 (60 minutes).
*/
SERVER_EXECUTION_TIMEOUT_SECONDS: number;
/** Max parallel sub-agents per turn. Env: `MAX_PARALLEL_SUB_AGENTS`. Default 10. */
MAX_PARALLEL_SUB_AGENTS: number;
/**
* TTL for a running turn's resumable event stream.
* Env: `TURN_STREAM_TTL_SECONDS`. Default execution timeout + 300.
Expand Down Expand Up @@ -1227,6 +1230,11 @@ const shared: SharedServerConfiguration = {
defaultValue: 30,
}),
SERVER_EXECUTION_TIMEOUT_SECONDS: serverExecutionTimeoutSeconds,
MAX_PARALLEL_SUB_AGENTS: parsePositiveInt({
envKey: 'MAX_PARALLEL_SUB_AGENTS',
raw: getEnv('MAX_PARALLEL_SUB_AGENTS'),
defaultValue: DEFAULT_MAX_PARALLEL_SUB_AGENTS,
}),
TURN_STREAM_TTL_SECONDS: parsePositiveInt({
envKey: 'TURN_STREAM_TTL_SECONDS',
raw: getEnv('TURN_STREAM_TTL_SECONDS'),
Expand Down
Loading