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
9 changes: 9 additions & 0 deletions src/server/lib/validation/aiAgentConfigSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export const aiAgentConfigSchema = {
systemPromptOverride: { type: 'string', maxLength: 50000 },
excludedTools: { type: 'array', items: { type: 'string' } },
excludedFilePatterns: { type: 'array', items: { type: 'string' } },
maxIterations: { type: 'integer', minimum: 1 },
maxToolCalls: { type: 'integer', minimum: 1 },
maxRepeatedCalls: { type: 'integer', minimum: 1 },
compressionThreshold: { type: 'integer', minimum: 1 },
observationMaskingRecencyWindow: { type: 'integer', minimum: 1 },
observationMaskingTokenThreshold: { type: 'integer', minimum: 1 },
toolExecutionTimeout: { type: 'integer', minimum: 1000 },
toolOutputMaxChars: { type: 'integer', minimum: 1000 },
retryBudget: { type: 'integer', minimum: 1 },
},
required: ['enabled', 'providers', 'maxMessagesPerSession', 'sessionTTL'],
additionalProperties: false,
Expand Down
6 changes: 5 additions & 1 deletion src/server/services/ai/conversation/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export interface ConversationState {
}

export class ConversationManager {
private readonly COMPRESSION_THRESHOLD = 80000;
private readonly COMPRESSION_THRESHOLD: number;

constructor(compressionThreshold?: number) {
this.COMPRESSION_THRESHOLD = compressionThreshold || 80000;
}

async shouldCompress(messages: ConversationMessage[]): Promise<boolean> {
const tokenCount = await this.estimateTokens(messages);
Expand Down
21 changes: 16 additions & 5 deletions src/server/services/ai/orchestration/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Tool, ToolCall } from '../types/tool';
import { StreamCallbacks } from '../types/stream';
import { ToolRegistry } from '../tools/registry';
import { ToolSafetyManager } from './safety';
import { LoopDetector } from './loopProtection';
import { LoopDetector, LoopProtection } from './loopProtection';
import { getLogger } from 'server/lib/logger';
import { RetryBudget, createClassifiedError, ErrorCategory } from '../errors';
import type { ClassifiedError } from '../errors';
Expand All @@ -43,11 +43,22 @@ export interface OrchestrationResult {
};
}

export interface OrchestratorOptions {
loopProtection?: Partial<LoopProtection>;
retryBudget?: number;
}

export class ToolOrchestrator {
private loopDetector: LoopDetector;

constructor(private toolRegistry: ToolRegistry, private safetyManager: ToolSafetyManager) {
this.loopDetector = new LoopDetector();
private retryBudgetMax: number;

constructor(
private toolRegistry: ToolRegistry,
private safetyManager: ToolSafetyManager,
options?: OrchestratorOptions
) {
this.loopDetector = new LoopDetector(options?.loopProtection);
this.retryBudgetMax = options?.retryBudget || 10;
}

async executeToolLoop(
Expand All @@ -69,7 +80,7 @@ export class ToolOrchestrator {

this.loopDetector.reset();

const retryBudget = new RetryBudget(10);
const retryBudget = new RetryBudget(this.retryBudgetMax);
const policy = createProviderPolicy(provider.name, retryBudget);

while (iteration < protection.maxIterations) {
Expand Down
16 changes: 11 additions & 5 deletions src/server/services/ai/orchestration/safety.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ import { OutputLimiter } from '../tools/outputLimiter';
export class ToolSafetyManager {
private requireConfirmation: boolean;
private validator: JsonSchema.Validator;
private toolExecutionTimeout: number;
private toolOutputMaxChars: number;

constructor(requireConfirmation: boolean = true) {
constructor(requireConfirmation: boolean = true, toolExecutionTimeout?: number, toolOutputMaxChars?: number) {
this.requireConfirmation = requireConfirmation;
this.validator = new JsonSchema.Validator();
this.toolExecutionTimeout = toolExecutionTimeout || 30000;
this.toolOutputMaxChars = toolOutputMaxChars || 30000;
}

async safeExecute(
Expand Down Expand Up @@ -85,22 +89,24 @@ export class ToolSafetyManager {
}

try {
const result = await this.withTimeout(tool.execute(args, signal), 30000);
const result = await this.withTimeout(tool.execute(args, signal), this.toolExecutionTimeout);

if (result.success && result.agentContent) {
result.agentContent = OutputLimiter.truncate(result.agentContent);
result.agentContent = OutputLimiter.truncate(result.agentContent, this.toolOutputMaxChars);
}

this.logToolExecution(tool.name, args, result, buildUuid);

return result;
} catch (error: any) {
if (error.message === 'Tool execution timeout') {
getLogger().warn(`AI: tool timeout tool=${tool.name} timeout=30s buildUuid=${buildUuid || 'none'}`);
getLogger().warn(
`AI: tool timeout tool=${tool.name} timeout=${this.toolExecutionTimeout}ms buildUuid=${buildUuid || 'none'}`
);
return {
success: false,
error: {
message: `${tool.name} timed out after 30 seconds`,
message: `${tool.name} timed out after ${this.toolExecutionTimeout / 1000} seconds`,
code: 'TIMEOUT',
recoverable: true,
suggestedAction: 'The operation took too long. Try narrowing your query.',
Expand Down
35 changes: 31 additions & 4 deletions src/server/services/ai/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ export interface AIAgentConfig {
inputCostPerMillion: number;
outputCostPerMillion: number;
};
maxIterations?: number;
maxToolCalls?: number;
maxRepeatedCalls?: number;
compressionThreshold?: number;
observationMaskingRecencyWindow?: number;
observationMaskingTokenThreshold?: number;
toolExecutionTimeout?: number;
toolOutputMaxChars?: number;
retryBudget?: number;
}

export interface ProcessQueryResult {
Expand All @@ -88,6 +97,7 @@ export class AIAgentCore {
private excludedTools?: string[];
private excludedFilePatterns?: string[];
private modelPricing?: { inputCostPerMillion: number; outputCostPerMillion: number };
private observationMaskingOptions?: { recencyWindow?: number; tokenThreshold?: number };

private mcpToolsLoaded = false;
private mcpToolInfos: McpToolInfo[] = [];
Expand All @@ -111,15 +121,32 @@ export class AIAgentCore {
this.excludedTools = config.excludedTools;
this.excludedFilePatterns = config.excludedFilePatterns;
this.modelPricing = config.modelPricing;
if (config.observationMaskingRecencyWindow || config.observationMaskingTokenThreshold) {
this.observationMaskingOptions = {
recencyWindow: config.observationMaskingRecencyWindow,
tokenThreshold: config.observationMaskingTokenThreshold,
};
}

this.toolRegistry = new ToolRegistry();
this.registerAllTools();

const safetyManager = new ToolSafetyManager(config.requireToolConfirmation ?? true);
this.orchestrator = new ToolOrchestrator(this.toolRegistry, safetyManager);
const safetyManager = new ToolSafetyManager(
config.requireToolConfirmation ?? true,
config.toolExecutionTimeout,
config.toolOutputMaxChars
);
this.orchestrator = new ToolOrchestrator(this.toolRegistry, safetyManager, {
loopProtection: {
maxIterations: config.maxIterations,
maxToolCalls: config.maxToolCalls,
maxRepeatedCalls: config.maxRepeatedCalls,
},
retryBudget: config.retryBudget,
});

this.promptBuilder = new AIAgentPromptBuilder();
this.conversationManager = new ConversationManager();
this.conversationManager = new ConversationManager(config.compressionThreshold);
}

async processQuery(
Expand Down Expand Up @@ -179,7 +206,7 @@ export class AIAgentCore {
textMessage(m.role as 'user' | 'assistant', m.content)
);

const maskResult = maskObservations(messages);
const maskResult = maskObservations(messages, this.observationMaskingOptions);
if (maskResult.masked) {
getLogger().info(
`AIAgentCore: observation masking applied maskedParts=${maskResult.stats.maskedParts} savedTokens=${maskResult.stats.savedTokens} buildUuid=${context.buildUuid}`
Expand Down
9 changes: 9 additions & 0 deletions src/server/services/aiAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ export default class AIAgentService extends BaseService {
excludedTools: aiAgentConfig.excludedTools,
excludedFilePatterns: aiAgentConfig.excludedFilePatterns,
modelPricing: this.modelPricing,
maxIterations: aiAgentConfig.maxIterations,
maxToolCalls: aiAgentConfig.maxToolCalls,
maxRepeatedCalls: aiAgentConfig.maxRepeatedCalls,
compressionThreshold: aiAgentConfig.compressionThreshold,
observationMaskingRecencyWindow: aiAgentConfig.observationMaskingRecencyWindow,
observationMaskingTokenThreshold: aiAgentConfig.observationMaskingTokenThreshold,
toolExecutionTimeout: aiAgentConfig.toolExecutionTimeout,
toolOutputMaxChars: aiAgentConfig.toolOutputMaxChars,
retryBudget: aiAgentConfig.retryBudget,
});
}

Expand Down
9 changes: 9 additions & 0 deletions src/server/services/types/aiAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,13 @@ export interface AIAgentConfig {
systemPromptOverride?: string;
excludedTools?: string[];
excludedFilePatterns?: string[];
maxIterations?: number;
maxToolCalls?: number;
maxRepeatedCalls?: number;
compressionThreshold?: number;
observationMaskingRecencyWindow?: number;
observationMaskingTokenThreshold?: number;
toolExecutionTimeout?: number;
toolOutputMaxChars?: number;
retryBudget?: number;
}
21 changes: 21 additions & 0 deletions src/shared/openApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,27 @@ export const openApiSpecificationForV2Api: OAS3Options = {
systemPromptOverride: { type: 'string', maxLength: 50000 },
excludedTools: { type: 'array', items: { type: 'string' } },
excludedFilePatterns: { type: 'array', items: { type: 'string' } },
maxIterations: { type: 'integer', description: 'Maximum orchestration loop iterations' },
maxToolCalls: { type: 'integer', description: 'Maximum total tool calls per query' },
maxRepeatedCalls: {
type: 'integer',
description: 'Maximum repeated calls with same arguments before loop detection',
},
compressionThreshold: {
type: 'integer',
description: 'Token count threshold before conversation history is compressed',
},
observationMaskingRecencyWindow: {
type: 'integer',
description: 'Number of recent tool results to preserve when masking observations',
},
observationMaskingTokenThreshold: {
type: 'integer',
description: 'Token count threshold before observation masking activates',
},
toolExecutionTimeout: { type: 'integer', description: 'Tool execution timeout in milliseconds' },
toolOutputMaxChars: { type: 'integer', description: 'Maximum characters in tool output before truncation' },
retryBudget: { type: 'integer', description: 'Maximum retry attempts per query on provider errors' },
},
required: ['enabled', 'providers', 'maxMessagesPerSession', 'sessionTTL'],
},
Expand Down