fix(gateway): repair routing health and MCP command wiring
ci/woodpecker/pr/ci Pipeline was successful

This commit is contained in:
shaggy (mosaic-dev box)
2026-08-11 15:28:57 -05:00
parent 20718b5a27
commit bda308efd9
10 changed files with 230 additions and 42 deletions
@@ -8,6 +8,7 @@
* to avoid real I/O — they verify the complete classify → match → decide path.
*/
import { describe, it, expect, vi } from 'vitest';
import type { ProviderHealthStatus } from '@mosaicstack/types';
import { RoutingEngineService } from './routing-engine.service.js';
import { DEFAULT_ROUTING_RULES } from '../routing/default-rules.js';
import type { RoutingRule } from './routing.types.js';
@@ -17,7 +18,7 @@ import type { RoutingRule } from './routing.types.js';
/** Build a RoutingEngineService backed by the given rule set and health map. */
function makeService(
rules: RoutingRule[],
healthMap: Record<string, { status: string }>,
healthMap: Record<string, { status: ProviderHealthStatus }>,
): RoutingEngineService {
const mockDb = {
select: vi.fn().mockReturnValue({
@@ -67,11 +68,11 @@ function defaultRules(): RoutingRule[] {
}
/** A health map where anthropic, openai, and zai are all healthy. */
const allHealthy: Record<string, { status: string }> = {
anthropic: { status: 'up' },
openai: { status: 'up' },
zai: { status: 'up' },
ollama: { status: 'up' },
const allHealthy: Record<string, { status: ProviderHealthStatus }> = {
anthropic: { status: 'healthy' },
openai: { status: 'healthy' },
zai: { status: 'healthy' },
ollama: { status: 'healthy' },
};
// ─── M4-013 E2E tests ─────────────────────────────────────────────────────────
@@ -212,10 +213,10 @@ describe('M4-013: routing end-to-end pipeline', () => {
// Let's use a simple coding message to target Simple coding → Codex (openai)
const message = 'implement a sort function';
const unhealthyHealth = {
const unhealthyHealth: Record<string, { status: ProviderHealthStatus }> = {
anthropic: { status: 'down' },
openai: { status: 'up' },
zai: { status: 'up' },
openai: { status: 'healthy' },
zai: { status: 'healthy' },
ollama: { status: 'down' },
};
@@ -1,5 +1,6 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import { routingRules, type Db, and, asc, eq, or } from '@mosaicstack/db';
import type { ProviderHealthStatus } from '@mosaicstack/types';
import { DB } from '../../database/database.module.js';
import { ProviderService } from '../provider.service.js';
import { classifyTask } from './task-classifier.js';
@@ -49,7 +50,7 @@ export class RoutingEngineService {
async resolve(
message: string,
userId?: string,
availableProviders?: Record<string, { status: string }>,
availableProviders?: Record<string, { status: ProviderHealthStatus }>,
): Promise<RoutingDecision> {
const classification = classifyTask(message);
this.logger.debug(
@@ -69,9 +70,8 @@ export class RoutingEngineService {
if (!this.matchConditions(rule, classification)) continue;
const providerStatus = health[rule.action.provider]?.status;
const isHealthy = providerStatus === 'up' || providerStatus === 'ok';
if (!isHealthy) {
if (!this.isRoutable(providerStatus)) {
this.logger.debug(
`Rule "${rule.name}" matched but provider "${rule.action.provider}" is unhealthy (status: ${providerStatus ?? 'unknown'})`,
);
@@ -111,6 +111,10 @@ export class RoutingEngineService {
// ─── Private helpers ───────────────────────────────────────────────────────
private isRoutable(status: ProviderHealthStatus | undefined): boolean {
return status === 'healthy' || status === 'degraded';
}
private evaluateCondition(
condition: RoutingCondition,
classification: TaskClassification,
@@ -186,11 +190,12 @@ export class RoutingEngineService {
* Walk the fallback chain and return the first healthy provider/model pair.
* If none are healthy, return the first entry unconditionally (last resort).
*/
private applyFallbackChain(health: Record<string, { status: string }>): RoutingDecision {
private applyFallbackChain(
health: Record<string, { status: ProviderHealthStatus }>,
): RoutingDecision {
for (const candidate of FALLBACK_CHAIN) {
const providerStatus = health[candidate.provider]?.status;
const isHealthy = providerStatus === 'up' || providerStatus === 'ok';
if (isHealthy) {
if (this.isRoutable(providerStatus)) {
this.logger.debug(`Fallback resolved: ${candidate.provider}/${candidate.model}`);
return {
provider: candidate.provider,
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import type { ProviderHealthStatus } from '@mosaicstack/types';
import { RoutingEngineService } from './routing-engine.service.js';
import type { RoutingRule, TaskClassification } from './routing.types.js';
@@ -29,7 +30,7 @@ function makeClassification(overrides: Partial<TaskClassification> = {}): TaskCl
/** Build a minimal RoutingEngineService with mocked DB and ProviderService. */
function makeService(
rules: RoutingRule[] = [],
healthMap: Record<string, { status: string }> = {},
healthMap: Record<string, { status: ProviderHealthStatus }> = {},
): RoutingEngineService {
const mockDb = {
select: vi.fn().mockReturnValue({
@@ -217,7 +218,10 @@ describe('RoutingEngineService.resolve — priority ordering', () => {
}),
];
const service = makeService(rules, { anthropic: { status: 'up' }, openai: { status: 'up' } });
const service = makeService(rules, {
anthropic: { status: 'healthy' },
openai: { status: 'healthy' },
});
const decision = await service.resolve('implement a function');
expect(decision.ruleName).toBe('high priority');
@@ -241,7 +245,10 @@ describe('RoutingEngineService.resolve — priority ordering', () => {
}),
];
const service = makeService(rules, { anthropic: { status: 'up' }, openai: { status: 'up' } });
const service = makeService(rules, {
anthropic: { status: 'healthy' },
openai: { status: 'healthy' },
});
const decision = await service.resolve('implement a function');
expect(decision.ruleName).toBe('coding rule');
@@ -270,7 +277,7 @@ describe('RoutingEngineService.resolve — unhealthy provider handling', () => {
const service = makeService(rules, {
anthropic: { status: 'down' }, // primary is unhealthy
openai: { status: 'up' },
openai: { status: 'healthy' },
});
const decision = await service.resolve('implement a function');
@@ -290,7 +297,7 @@ describe('RoutingEngineService.resolve — unhealthy provider handling', () => {
];
const service2 = makeService(unhealthyRules, {
anthropic: { status: 'up' },
anthropic: { status: 'healthy' },
openai: { status: 'down' },
});
@@ -306,7 +313,7 @@ describe('RoutingEngineService.resolve — unhealthy provider handling', () => {
const service = makeService(rules, {
anthropic: { status: 'down' }, // Sonnet is on anthropic — down
ollama: { status: 'up' }, // Haiku is also on anthropic — use Ollama as next
ollama: { status: 'healthy' }, // Haiku is also on anthropic — use Ollama as next
});
const decision = await service.resolve('hello there');
@@ -345,7 +352,7 @@ describe('RoutingEngineService.resolve — empty conditions (fallback rule)', ()
}),
];
const service = makeService(rules, { anthropic: { status: 'up' } });
const service = makeService(rules, { anthropic: { status: 'healthy' } });
const decision = await service.resolve('completely unrelated message xyz');
expect(decision.ruleName).toBe('catch-all');
@@ -369,7 +376,7 @@ describe('RoutingEngineService.resolve — empty conditions (fallback rule)', ()
}),
];
const service = makeService(rules, { anthropic: { status: 'up' } });
const service = makeService(rules, { anthropic: { status: 'healthy' } });
const codingDecision = await service.resolve('implement a function');
expect(codingDecision.ruleName).toBe('specific coding rule');
@@ -401,7 +408,7 @@ describe('RoutingEngineService.resolve — disabled rules', () => {
}),
];
const service = makeService(rules, { anthropic: { status: 'up' } });
const service = makeService(rules, { anthropic: { status: 'healthy' } });
const decision = await service.resolve('implement a function');
expect(decision.ruleName).toBe('enabled fallback');
@@ -452,9 +459,45 @@ describe('RoutingEngineService.resolve — availableProviders override', () => {
ps: unknown,
) => RoutingEngineService)(mockDb, mockProviderService);
const preSupplied = { anthropic: { status: 'up' } };
const preSupplied: Record<string, { status: ProviderHealthStatus }> = {
anthropic: { status: 'healthy' },
};
await service.resolve('implement a function', undefined, preSupplied);
expect(mockHealthCheckAll).not.toHaveBeenCalled();
});
});
// ─── resolve — canonical ProviderHealthStatus values ──────────────────────────
describe('RoutingEngineService.resolve — canonical health status routing', () => {
it('routes healthy and degraded providers by rule, and falls through to fallback when down', async () => {
const codingRule = makeRule({
name: 'coding rule',
priority: 1,
conditions: [{ field: 'taskType', operator: 'eq', value: 'coding' }],
action: { provider: 'openai', model: 'gpt-4o' },
});
// healthy → selected by its own rule, not the fallback chain
const healthyService = makeService([codingRule], { openai: { status: 'healthy' } });
const healthyDecision = await healthyService.resolve('implement a function');
expect(healthyDecision.ruleName).toBe('coding rule');
expect(healthyDecision.provider).toBe('openai');
// down → rule is skipped as unroutable, falls through to the fallback chain
const downService = makeService([codingRule], {
openai: { status: 'down' },
anthropic: { status: 'healthy' },
});
const downDecision = await downService.resolve('implement a function');
expect(downDecision.ruleName).toBe('fallback');
expect(downDecision.provider).toBe('anthropic');
// degraded → still routable, selected by its own rule, not the fallback chain
const degradedService = makeService([codingRule], { openai: { status: 'degraded' } });
const degradedDecision = await degradedService.resolve('implement a function');
expect(degradedDecision.ruleName).toBe('coding rule');
expect(degradedDecision.provider).toBe('openai');
});
});
@@ -74,13 +74,19 @@ const mockChatGateway = {
broadcastSessionInfo: vi.fn(),
};
const mockMcpClient = {
reconnectServer: vi.fn().mockResolvedValue(undefined),
getServerStatuses: vi.fn(() => []),
getToolDefinitions: vi.fn(() => []),
};
function buildService(
redis: typeof mockRedis | null = mockRedis,
mcpClient: {
reconnectServer: ReturnType<typeof vi.fn>;
getServerStatuses: ReturnType<typeof vi.fn>;
getToolDefinitions: ReturnType<typeof vi.fn>;
} | null = null,
} = mockMcpClient,
): CommandExecutorService {
return new CommandExecutorService(
mockRegistry as never,
@@ -36,6 +36,12 @@ const authorization = {
),
};
const mockMcpClient = {
getServerStatuses: vi.fn(() => []),
getToolDefinitions: vi.fn(() => []),
reconnectServer: vi.fn().mockResolvedValue(undefined),
};
function buildExecutor(authorizationService: unknown = authorization): CommandExecutorService {
return new CommandExecutorService(
registry as never,
@@ -46,7 +52,7 @@ function buildExecutor(authorizationService: unknown = authorization): CommandEx
{ agents: {} } as never,
null,
null,
null,
mockMcpClient as never,
authorizationService as never,
);
}
@@ -34,9 +34,7 @@ export class CommandExecutorService {
@Optional()
@Inject(forwardRef(() => ChatGateway))
private readonly chatGateway: ChatGateway | null,
@Optional()
@Inject(McpClientService)
private readonly mcpClient: McpClientService | null,
@Inject(McpClientService) private readonly mcpClient: McpClientService,
@Optional()
@Inject(CommandAuthorizationService)
private readonly authorization: CommandAuthorizationService | null = null,
@@ -548,15 +546,6 @@ export class CommandExecutorService {
args: string | null,
conversationId: string,
): Promise<SlashCommandResultPayload> {
if (!this.mcpClient) {
return {
command: 'mcp',
conversationId,
success: false,
message: 'MCP client service is not available.',
};
}
const action = args?.trim().split(/\s+/)[0] ?? 'status';
switch (action) {
@@ -11,6 +11,8 @@
* - Unknown command returns descriptive error
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { CommandsModule } from './commands.module.js';
import { McpClientModule } from '../mcp-client/mcp-client.module.js';
import { CommandRegistryService } from './command-registry.service.js';
import { CommandExecutorService } from './command-executor.service.js';
import type { SlashCommandPayload } from '@mosaicstack/types';
@@ -47,6 +49,12 @@ const mockBrain = {
},
};
const mockMcpClient = {
getServerStatuses: vi.fn(() => []),
getToolDefinitions: vi.fn(() => []),
reconnectServer: vi.fn().mockResolvedValue(undefined),
};
// ─── Helpers ─────────────────────────────────────────────────────────────────
function buildRegistry(): CommandRegistryService {
@@ -65,7 +73,7 @@ function buildExecutor(registry: CommandRegistryService): CommandExecutorService
mockBrain as never,
null, // reloadService (optional)
null, // chatGateway (optional)
null, // mcpClient (optional)
mockMcpClient as never,
);
}
@@ -153,6 +161,15 @@ describe('CommandRegistryService — integration', () => {
}
});
// ─── Module Wiring Tests ──────────────────────────────────────────────────────
describe('CommandsModule — Nest wiring', () => {
it('CommandsModule imports McpClientModule in its Nest metadata', () => {
const imports = Reflect.getMetadata('imports', CommandsModule) ?? [];
expect(imports).toContain(McpClientModule);
});
});
// ─── Executor Tests ───────────────────────────────────────────────────────────
describe('CommandExecutorService — integration', () => {
@@ -259,4 +276,14 @@ describe('CommandExecutorService — integration', () => {
expect(result.command).toBe(cmd);
});
}
// /mcp status reaches the required McpClientService and never reports it unavailable
it('/mcp status calls the wired McpClientService and reports the no-servers message', async () => {
const payload: SlashCommandPayload = { command: 'mcp', conversationId };
const result = await executor.execute(payload, userScope);
expect(mockMcpClient.getServerStatuses).toHaveBeenCalledOnce();
expect(result.success).toBe(true);
expect(result.message).toContain('No MCP servers configured.');
expect(result.message).not.toBe('MCP client service is not available.');
});
});
+7 -1
View File
@@ -4,6 +4,7 @@ import type { MosaicConfig } from '@mosaicstack/config';
import { MOSAIC_CONFIG } from '../config/config.module.js';
import { ChatModule } from '../chat/chat.module.js';
import { GCModule } from '../gc/gc.module.js';
import { McpClientModule } from '../mcp-client/mcp-client.module.js';
import { ReloadModule } from '../reload/reload.module.js';
import { CommandAuthorizationService } from './command-authorization.service.js';
import { CommandExecutorService } from './command-executor.service.js';
@@ -14,7 +15,12 @@ import { COMMANDS_REDIS } from './commands.tokens.js';
const COMMANDS_QUEUE_HANDLE = 'COMMANDS_QUEUE_HANDLE';
@Module({
imports: [GCModule, forwardRef(() => ReloadModule), forwardRef(() => ChatModule)],
imports: [
GCModule,
McpClientModule,
forwardRef(() => ReloadModule),
forwardRef(() => ChatModule),
],
providers: [
{
provide: COMMANDS_QUEUE_HANDLE,
@@ -143,6 +143,12 @@ describe('ReloadService — /reload command sanitizes plugin errors', () => {
const mockSessionGC = { sweepOrphans: vi.fn() };
const mockBrain = { agents: { findByName: vi.fn(), findById: vi.fn(), create: vi.fn() } };
const mockMcpClient = {
getServerStatuses: vi.fn(() => []),
getToolDefinitions: vi.fn(() => []),
reconnectServer: vi.fn().mockResolvedValue(undefined),
};
const executor = new CommandExecutorService(
registry as never,
mockAgentService as never,
@@ -152,7 +158,7 @@ describe('ReloadService — /reload command sanitizes plugin errors', () => {
mockBrain as never,
reloadService,
mockChatGateway as never,
null,
mockMcpClient as never,
);
const payload: SlashCommandPayload = { command: 'reload', conversationId: 'conv-1' };