feat(session): persist model tier overrides per session

Store per-session config in SQLite and route /model and /reset through command fast-paths so channel sessions keep independent model selection across reconnects and restarts.
This commit is contained in:
William Valentin
2026-02-13 01:04:26 -08:00
parent 3472a0b926
commit 9f81c01603
35 changed files with 1438 additions and 144 deletions
+320 -1
View File
@@ -1,7 +1,12 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { AgentRouter } from '../agents/router.js';
import { AgentConfigRegistry } from '../agents/registry.js';
import type { ModelTier } from '../models/router.js';
import { createMessageRouter } from './routing.js';
import { AgentOrchestrator } from '../backends/index.js';
import { CommandRegistry, registerBuiltinCommands } from '../commands/index.js';
import { ComponentRegistry } from '../intents/index.js';
import { RoutingPolicy } from '../routing/index.js';
describe('daemon agent routing integration', () => {
it('resolves agent config for channel messages', () => {
@@ -61,3 +66,317 @@ describe('daemon agent routing integration', () => {
expect(resolveTier(undefined, undefined, undefined)).toBe('default');
});
});
describe('daemon command fast-path integration', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('handles known reset command without calling agent.process', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
const session = {
id: 'telegram:user-1',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const commandRegistry = new CommandRegistry();
registerBuiltinCommands(commandRegistry);
const router = createMessageRouter({
sessionManager: {
getSession: vi.fn(() => session),
} as any,
modelRouter: {
getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
getLabel: (tier: string) => tier,
} as any,
systemPrompt: 'test prompt',
toolRegistry: {
clone() { return this; },
register: vi.fn(),
} as any,
toolExecutor: {} as any,
config: {
agents: {
primary_tier: 'default',
delegation: {
compaction: 'fast',
memory_extraction: 'fast',
classification: 'fast',
tool_summarisation: 'fast',
complex_reasoning: 'complex',
},
max_delegation_depth: 3,
max_iterations: 10,
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as any,
commandRegistry,
});
const reply = vi.fn(async () => {});
await router.handler({
id: 'm1',
channel: 'telegram',
senderId: 'user-1',
text: '/reset',
metadata: { isCommand: true, command: 'reset' },
} as any, reply);
expect(processSpy).not.toHaveBeenCalled();
expect(session.deleteConfig).toHaveBeenCalledWith('modelTier');
});
it('handles model command via fast-path and persists tier override', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process');
const setModelTierSpy = vi.spyOn(AgentOrchestrator.prototype, 'setModelTier');
const session = {
id: 'telegram:user-4',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const commandRegistry = new CommandRegistry();
registerBuiltinCommands(commandRegistry);
const router = createMessageRouter({
sessionManager: {
getSession: vi.fn(() => session),
} as any,
modelRouter: {
getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
getLabel: (tier: string) => tier,
} as any,
systemPrompt: 'test prompt',
toolRegistry: {
clone() { return this; },
register: vi.fn(),
} as any,
toolExecutor: {} as any,
config: {
agents: {
primary_tier: 'default',
delegation: {
compaction: 'fast',
memory_extraction: 'fast',
classification: 'fast',
tool_summarisation: 'fast',
complex_reasoning: 'complex',
},
max_delegation_depth: 3,
max_iterations: 10,
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as any,
commandRegistry,
});
const reply = vi.fn(async () => {});
await router.handler({
id: 'm4',
channel: 'telegram',
senderId: 'user-4',
text: '/model fast',
metadata: { isCommand: true, command: 'model', commandArgs: 'fast' },
} as any, reply);
expect(processSpy).not.toHaveBeenCalled();
expect(setModelTierSpy).toHaveBeenCalledWith('fast');
expect(session.setConfig).toHaveBeenCalledWith('modelTier', 'fast');
});
it('uses intent match to override agent target', async () => {
const session = {
id: 'telegram:user-2',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const commandRegistry = new CommandRegistry();
registerBuiltinCommands(commandRegistry);
const intentRegistry = new ComponentRegistry({ matchThreshold: 0.5 });
intentRegistry.register({
name: 'deploy-route',
patterns: ['deploy *'],
target: { type: 'agent', name: 'coder' },
priority: 10,
enabled: true,
});
const agentConfigRegistry = new AgentConfigRegistry();
agentConfigRegistry.loadFromConfig({
assistant: { model_tier: 'default', sandbox: false },
coder: { model_tier: 'complex', sandbox: false },
});
const agentRouter = new AgentRouter({
default_agent: 'assistant',
channels: {},
senders: {},
});
const router = createMessageRouter({
sessionManager: {
getSession: vi.fn(() => session),
} as any,
modelRouter: {
getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
getLabel: (tier: string) => tier,
} as any,
systemPrompt: 'test prompt',
toolRegistry: {
clone() { return this; },
register: vi.fn(),
} as any,
toolExecutor: {} as any,
config: {
intents: { enabled: true },
agents: {
primary_tier: 'default',
delegation: {
compaction: 'fast',
memory_extraction: 'fast',
classification: 'fast',
tool_summarisation: 'fast',
complex_reasoning: 'complex',
},
max_delegation_depth: 3,
max_iterations: 10,
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as any,
commandRegistry,
intentRegistry,
agentConfigRegistry,
agentRouter,
});
await router.handler({
id: 'm2',
channel: 'telegram',
senderId: 'user-2',
text: 'deploy backend now',
metadata: { isCommand: true, command: 'reset' },
} as any, vi.fn(async () => {}));
const keys = Array.from(router.agents.keys());
expect(keys.some(key => key.includes(':coder'))).toBe(true);
});
it('falls back to llm path when confidence is below fast threshold', async () => {
const session = {
id: 'telegram:user-3',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const commandRegistry = new CommandRegistry();
registerBuiltinCommands(commandRegistry);
const intentRegistry = new ComponentRegistry({ matchThreshold: 0.5 });
intentRegistry.register({
name: 'deploy-route',
patterns: ['deploy *'],
target: { type: 'agent', name: 'coder' },
priority: 10,
enabled: true,
});
const routingPolicy = new RoutingPolicy({
enabled: true,
fastPathThreshold: 0.99,
llmThreshold: 0.2,
defaultPath: 'llm',
});
const agentConfigRegistry = new AgentConfigRegistry();
agentConfigRegistry.loadFromConfig({
assistant: { model_tier: 'default', sandbox: false },
coder: { model_tier: 'complex', sandbox: false },
});
const agentRouter = new AgentRouter({
default_agent: 'assistant',
channels: {},
senders: {},
});
const router = createMessageRouter({
sessionManager: {
getSession: vi.fn(() => session),
} as any,
modelRouter: {
getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
getLabel: (tier: string) => tier,
} as any,
systemPrompt: 'test prompt',
toolRegistry: {
clone() { return this; },
register: vi.fn(),
} as any,
toolExecutor: {} as any,
config: {
intents: { enabled: true },
agents: {
primary_tier: 'default',
delegation: {
compaction: 'fast',
memory_extraction: 'fast',
classification: 'fast',
tool_summarisation: 'fast',
complex_reasoning: 'complex',
},
max_delegation_depth: 3,
max_iterations: 10,
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
} as any,
commandRegistry,
intentRegistry,
routingPolicy,
agentConfigRegistry,
agentRouter,
});
await router.handler({
id: 'm3',
channel: 'telegram',
senderId: 'user-3',
text: 'deploy backend now',
metadata: { isCommand: true, command: 'reset' },
} as any, vi.fn(async () => {}));
const keys = Array.from(router.agents.keys());
expect(keys.some(key => key.includes(':assistant'))).toBe(true);
});
});