Skip to content
Merged
13 changes: 13 additions & 0 deletions packages/cli/src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,19 @@ describe('loadCliConfig', () => {
expect(config.getAgentsSettings().maxParallelAgents).toBe(2);
});

it('passes agents.maxParallelAgentsByModel from settings to core config', async () => {
process.argv = ['node', 'script.js'];
const argv = await parseArguments();
const config = await loadCliConfig(
{ agents: { maxParallelAgentsByModel: { 'weak-model': 1 } } },
argv,
);

expect(config.getAgentsSettings().maxParallelAgentsByModel).toEqual({
'weak-model': 1,
});
});

it('passes tools.shell.defaultTimeoutMs from settings to core config', async () => {
process.argv = ['node', 'script.js'];
const argv = await parseArguments();
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,7 @@ export async function loadCliConfig(
}
: undefined,
maxParallelAgents: settings.agents.maxParallelAgents,
maxParallelAgentsByModel: settings.agents.maxParallelAgentsByModel,
displayMode: settings.agents.displayMode,
arena: settings.agents.arena
? {
Expand Down
20 changes: 19 additions & 1 deletion packages/cli/src/config/settingsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2822,13 +2822,31 @@ const SETTINGS_SCHEMA = {
default: undefined as number | undefined,
minimum: 1,
description:
'Global maximum number of background sub-agents that can run concurrently. Additional background agents wait in a queue until a slot is available. Per-model limits are not supported yet.',
'Global maximum number of background sub-agents that can run concurrently. Additional background agents wait in a queue until a slot is available. Use maxParallelAgentsByModel to cap a specific model below this global limit.',
showInDialog: false,
jsonSchemaOverride: {
type: 'integer',
minimum: 1,
},
},
maxParallelAgentsByModel: {
type: 'object',
label: 'Max Parallel Agents Per Model',
category: 'Advanced',
requiresRestart: true,
default: undefined as Record<string, number> | undefined,
description:
'Per-model maximum number of background sub-agents that can run concurrently, keyed by model ID (e.g. { "qwen3-max": 2 }). Useful when a model has a lower concurrency capacity. Takes precedence over the global maxParallelAgents for the matched model; models not listed here fall back to the global limit.',
showInDialog: false,
mergeStrategy: MergeStrategy.SHALLOW_MERGE,
jsonSchemaOverride: {
type: 'object',
additionalProperties: {
type: 'integer',
minimum: 1,
},
},
},
displayMode: {
type: 'enum',
label: 'Display Mode',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/ui/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2970,7 +2970,7 @@ export const AppContainer = (props: AppContainerProps) => {
stickyTodos !== null &&
!dialogsVisible &&
!isFeedbackDialogOpen &&
streamingState !== StreamingState.WaitingForConfirmation;
streamingState === StreamingState.Responding;
const stickyTodoWidth = Math.min(mainAreaWidth, 64);
const stickyTodoMaxVisibleItems =
getStickyTodoMaxVisibleItems(terminalHeight);
Expand Down
18 changes: 17 additions & 1 deletion packages/cli/src/ui/layouts/DefaultAppLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const baseUIState: Partial<UIState> = {
terminalHeight: 24,
staticExtraHeight: 0,
constrainHeight: true,
streamingState: StreamingState.Idle,
streamingState: StreamingState.Responding,
historyManager: {
addItem: vi.fn(),
history: [],
Expand Down Expand Up @@ -212,6 +212,22 @@ describe('DefaultAppLayout', () => {
expect(output).toContain('Composer');
});

it('does not render sticky todo list when agent is idle', () => {
mockedUseAgentViewState.mockReturnValue({
activeView: 'main',
agents: new Map(),
});

const { lastFrame } = renderLayout({
...baseUIState,
streamingState: StreamingState.Idle,
});

const output = lastFrame() ?? '';
expect(output).not.toContain('StickyTodoList');
expect(output).toContain('Composer');
});

it('does not render sticky todo list when feedback dialog is open', () => {
mockedUseAgentViewState.mockReturnValue({
activeView: 'main',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/ui/layouts/DefaultAppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const DefaultAppLayout: React.FC = () => {
uiState.stickyTodos !== null &&
!uiState.dialogsVisible &&
!uiState.isFeedbackDialogOpen &&
uiState.streamingState !== StreamingState.WaitingForConfirmation;
uiState.streamingState === StreamingState.Responding;

// Clear terminal on view switch so previous view's <Static> output
// is removed. refreshStatic clears the terminal and bumps the
Expand Down
13 changes: 12 additions & 1 deletion packages/cli/src/ui/layouts/ScreenReaderAppLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const baseUIState: Partial<UIState> = {
terminalHeight: 24,
staticExtraHeight: 0,
constrainHeight: true,
streamingState: StreamingState.Idle,
streamingState: StreamingState.Responding,
historyManager: {
addItem: vi.fn(),
history: [],
Expand Down Expand Up @@ -164,6 +164,17 @@ describe('ScreenReaderAppLayout', () => {
expect(output).toContain('Composer');
});

it('does not render sticky todo list when agent is idle', () => {
const { lastFrame } = renderLayout({
...baseUIState,
streamingState: StreamingState.Idle,
});

const output = lastFrame() ?? '';
expect(output).not.toContain('StickyTodoList');
expect(output).toContain('Composer');
});

it('does not render sticky todo list when feedback dialog is open', () => {
const { lastFrame } = renderLayout({
...baseUIState,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/ui/layouts/ScreenReaderAppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const ScreenReaderAppLayout: React.FC = () => {
uiState.stickyTodos !== null &&
!uiState.dialogsVisible &&
!uiState.isFeedbackDialogOpen &&
uiState.streamingState !== StreamingState.WaitingForConfirmation;
uiState.streamingState === StreamingState.Responding;

return (
<Box flexDirection="column" width="90%" height="100%">
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agents/background-agent-resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export class BackgroundAgentResumeService {
// entry back to paused, so an at-capacity revive fails cleanly instead of
// stranding the entry as paused.
try {
registry.assertCanStartBackgroundAgent();
registry.assertCanStartBackgroundAgent(entry.model);
} catch (error) {
debugLogger.warn(
`[BackgroundAgentResume] Cannot revive "${agentId}": ` +
Expand Down Expand Up @@ -579,7 +579,7 @@ export class BackgroundAgentResumeService {
);
}
try {
registry.assertCanStartBackgroundAgent();
registry.assertCanStartBackgroundAgent(entry.model);
} catch (error) {
debugLogger.warn(
`[BackgroundAgentResume] Cannot revive "${agentId}": ` +
Expand Down
167 changes: 167 additions & 0 deletions packages/core/src/agents/background-tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,173 @@ describe('BackgroundTaskRegistry', () => {
});
});

describe('per-model background concurrency limit', () => {
it('caps a single model while leaving room for others', () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 10,
maxConcurrentBackgroundAgentsByModel: { 'weak-model': 1 },
});

registry.register(makeRegistration('bg-1', { model: 'weak-model' }));

// The capped model is full...
expect(() =>
registry.register(makeRegistration('bg-2', { model: 'weak-model' })),
).toThrow(
'Cannot start background agent: maximum concurrent background agents ' +
'for model "weak-model" (1) reached. Stop an existing agent on that ' +
'model first.',
);
expect(registry.get('bg-2')).toBeUndefined();

// ...but a different model is unaffected.
registry.register(makeRegistration('bg-3', { model: 'strong-model' }));
expect(registry.get('bg-3')?.status).toBe('running');
});

it('lets a model without a per-model cap use the global limit', () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 2,
maxConcurrentBackgroundAgentsByModel: { 'weak-model': 1 },
});

registry.register(makeRegistration('bg-1', { model: 'uncapped-model' }));
registry.register(makeRegistration('bg-2', { model: 'uncapped-model' }));

// The global cap still bounds uncapped models.
expect(() =>
registry.register(
makeRegistration('bg-3', { model: 'uncapped-model' }),
),
).toThrow('maximum concurrent background agents (2) reached');
});

it('enforces the global cap even when the per-model cap has room', () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 1,
maxConcurrentBackgroundAgentsByModel: { 'weak-model': 5 },
});

registry.register(makeRegistration('bg-1', { model: 'other-model' }));

expect(() =>
registry.register(makeRegistration('bg-2', { model: 'weak-model' })),
).toThrow('maximum concurrent background agents (1) reached');
});

it('counts reservations against the per-model cap', () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 10,
maxConcurrentBackgroundAgentsByModel: { 'weak-model': 1 },
});

const reservation = registry.tryReserveBackgroundSlot('weak-model');
expect(reservation).toBeDefined();
expect(reservation?.model).toBe('weak-model');

// A second reservation for the same model is refused while the first
// is outstanding.
expect(registry.tryReserveBackgroundSlot('weak-model')).toBeUndefined();
// A reservation for a different model is still granted.
expect(registry.tryReserveBackgroundSlot('strong-model')).toBeDefined();

// Releasing frees the per-model slot.
registry.releaseBackgroundSlot(reservation!);
expect(registry.tryReserveBackgroundSlot('weak-model')).toBeDefined();
});

it('frees the per-model cap when an agent on that model completes', () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 10,
maxConcurrentBackgroundAgentsByModel: { 'weak-model': 1 },
});

registry.register(makeRegistration('bg-1', { model: 'weak-model' }));
expect(registry.tryReserveBackgroundSlot('weak-model')).toBeUndefined();

registry.complete('bg-1', 'done');
registry.register(makeRegistration('bg-2', { model: 'weak-model' }));
expect(registry.get('bg-2')?.status).toBe('running');
});

it('drains a different-model waiter while a capped-model waiter stays queued', async () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 2,
maxConcurrentBackgroundAgentsByModel: { 'weak-model': 1 },
});
// Fill both the weak-model cap (1) and the global cap (2) so neither
// waiter below can reserve a slot immediately.
registry.register(makeRegistration('bg-weak', { model: 'weak-model' }));
registry.register(makeRegistration('bg-other', { model: 'other-model' }));

// Both queue because the global cap is full.
const weakWaiter = registry.waitForBackgroundSlot(
new AbortController().signal,
'weak-model',
);
const strongWaiter = registry.waitForBackgroundSlot(
new AbortController().signal,
'strong-model',
);
expect(registry.getQueuedCount()).toBe(2);

// Freeing one global slot (but NOT the weak-model slot) lets the
// strong-model waiter through while the weak-model waiter stays queued.
registry.complete('bg-other', 'done');

const strongReservation = await strongWaiter;
expect(strongReservation.model).toBe('strong-model');
expect(registry.getQueuedCount()).toBe(1);

// The weak-model waiter is released only once a weak-model slot frees.
registry.complete('bg-weak', 'done');
const weakReservation = await weakWaiter;
expect(weakReservation.model).toBe('weak-model');
expect(registry.getQueuedCount()).toBe(0);
});

it('ignores malformed per-model cap values', () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 10,
maxConcurrentBackgroundAgentsByModel: {
'bad-zero': 0,
'bad-negative': -3,
'bad-float': 1.5,
good: 1,
} as Record<string, number>,
});

// Malformed entries are dropped, so those models fall back to the
// global cap and can each start.
registry.register(makeRegistration('bg-zero', { model: 'bad-zero' }));
registry.register(
makeRegistration('bg-negative', { model: 'bad-negative' }),
);
registry.register(makeRegistration('bg-float', { model: 'bad-float' }));
expect(registry.get('bg-zero')?.status).toBe('running');
expect(registry.get('bg-negative')?.status).toBe('running');
expect(registry.get('bg-float')?.status).toBe('running');

// The one valid entry is still enforced.
registry.register(makeRegistration('bg-good', { model: 'good' }));
expect(() =>
registry.register(makeRegistration('bg-good-2', { model: 'good' })),
).toThrow('for model "good" (1) reached');
});

it('accepts a ReadonlyMap for the per-model caps', () => {
registry = new BackgroundTaskRegistry({
maxConcurrentBackgroundAgents: 10,
maxConcurrentBackgroundAgentsByModel: new Map([['weak-model', 1]]),
});

registry.register(makeRegistration('bg-1', { model: 'weak-model' }));
expect(() =>
registry.register(makeRegistration('bg-2', { model: 'weak-model' })),
).toThrow('for model "weak-model" (1) reached');
});
});

it('aborts all running agents and emits fallback notifications', () => {
const callback = vi.fn();
registry.setNotificationCallback(callback);
Expand Down
Loading
Loading