Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 7 additions & 5 deletions packages/cli/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,13 @@ export async function loadCliConfig(
: settings.tools?.discoveryCommand,
toolCallCommand: bareMode ? undefined : settings.tools?.callCommand,
mcpServerCommand: bareMode ? undefined : settings.mcp?.serverCommand,
mcpServers: bareMode ? {} : (() => {
const base = settings.mcpServers || {};
const cliMcpServers = parseMcpConfig(argv.mcpConfig);
return cliMcpServers ? { ...base, ...cliMcpServers } : base;
})(),
mcpServers: bareMode
? {}
: (() => {
const base = settings.mcpServers || {};
const cliMcpServers = parseMcpConfig(argv.mcpConfig);
return cliMcpServers ? { ...base, ...cliMcpServers } : base;
})(),
allowedMcpServers: allowedMcpServers
? Array.from(allowedMcpServers)
: undefined,
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/src/ui/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ export const AppContainer = (props: AppContainerProps) => {
);
historyManager.loadHistory(historyItems);

const recovered = await config.loadPausedBackgroundAgents(
config.getSessionId(),
);
if (recovered.length > 0) {
historyManager.addItem(
{
type: MessageType.INFO,
text: config
.getBackgroundAgentResumeService()
.buildRecoveredBackgroundAgentsNotice(recovered.length),
},
Date.now(),
);
}

// Restore session name tag from custom title
const title = config
.getSessionService()
Expand Down
82 changes: 74 additions & 8 deletions packages/cli/src/ui/commands/tasksCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { vi, describe, it, expect, beforeEach } from 'vitest';
import { tasksCommand } from './tasksCommand.js';
import { type CommandContext } from './types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import type { BackgroundShellEntry } from '@qwen-code/qwen-code-core';
import type {
BackgroundShellEntry,
BackgroundTaskEntry,
} from '@qwen-code/qwen-code-core';

type AgentTaskTestEntry = BackgroundTaskEntry & {
resumeBlockedReason?: string;
};

function entry(
overrides: Partial<BackgroundShellEntry> = {},
Expand All @@ -25,16 +32,33 @@ function entry(
};
}

function agentEntry(
overrides: Partial<AgentTaskTestEntry> = {},
): AgentTaskTestEntry {
return {
agentId: 'agent_aaaaaaaa',
description: 'Investigate flaky test failure',
subagentType: 'researcher',
status: 'running',
startTime: Date.now() - 7_000,
abortController: new AbortController(),
...overrides,
};
}

describe('tasksCommand', () => {
let context: CommandContext;
let getAll: ReturnType<typeof vi.fn>;
let getShells: ReturnType<typeof vi.fn>;
let getAgents: ReturnType<typeof vi.fn>;

beforeEach(() => {
getAll = vi.fn().mockReturnValue([]);
getShells = vi.fn().mockReturnValue([]);
getAgents = vi.fn().mockReturnValue([]);
context = createMockCommandContext({
services: {
config: {
getBackgroundShellRegistry: () => ({ getAll }),
getBackgroundShellRegistry: () => ({ getAll: getShells }),
getBackgroundTaskRegistry: () => ({ getAll: getAgents }),
},
},
} as unknown as Parameters<typeof createMockCommandContext>[0]);
Expand All @@ -45,12 +69,12 @@ describe('tasksCommand', () => {
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'No background shells.',
content: 'No background tasks.',
});
});

it('lists running and terminal entries with status / runtime / output path', async () => {
getAll.mockReturnValue([
it('lists running and terminal shell entries with status / runtime / output path', async () => {
getShells.mockReturnValue([
entry({
shellId: 'bg_run',
command: 'npm run dev',
Expand Down Expand Up @@ -81,7 +105,7 @@ describe('tasksCommand', () => {
if (!result || result.type !== 'message') {
throw new Error('expected message result');
}
expect(result.content).toContain('Background shells (3 total)');
expect(result.content).toContain('Background tasks (3 total)');
expect(result.content).toContain('[bg_run] running');
expect(result.content).toContain('pid=1111');
expect(result.content).toContain('npm run dev');
Expand All @@ -91,4 +115,46 @@ describe('tasksCommand', () => {
'output: /tmp/tasks/sess/shell-bg_done.output',
);
});

it('includes background agent entries alongside shells', async () => {
getAgents.mockReturnValue([
agentEntry({
agentId: 'agent_run',
description: 'Fix flaky test and send patch',
subagentType: 'researcher',
status: 'running',
outputFile: '/tmp/tasks/sess/agent_run.jsonl',
}),
agentEntry({
agentId: 'agent_pause',
description: 'Resume-safe task',
subagentType: 'researcher',
status: 'paused',
resumeBlockedReason: 'Subagent "researcher" is no longer available.',
}),
]);
getShells.mockReturnValue([
entry({
shellId: 'bg_shell',
command: 'npm run dev',
status: 'running',
}),
]);

const result = await tasksCommand.action!(context, '');
if (!result || result.type !== 'message') {
throw new Error('expected message result');
}

expect(result.content).toContain('Background tasks (3 total)');
expect(result.content).toContain('[agent_run] running');
expect(result.content).toContain(
'researcher: Fix flaky test and send patch',
);
expect(result.content).toContain('output: /tmp/tasks/sess/agent_run.jsonl');
expect(result.content).toContain(
'[agent_pause] paused (resume blocked): Subagent "researcher" is no longer available.',
);
expect(result.content).toContain('[bg_shell] running');
});
});
83 changes: 71 additions & 12 deletions packages/cli/src/ui/commands/tasksCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,44 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type { BackgroundShellEntry } from '@qwen-code/qwen-code-core';
import {
buildBackgroundEntryLabel,
type BackgroundShellEntry,
type BackgroundTaskEntry,
} from '@qwen-code/qwen-code-core';
import type { SlashCommand } from './types.js';
import { CommandKind } from './types.js';
import { t } from '../../i18n/index.js';
import { formatDuration } from '../utils/formatters.js';

function statusLabel(entry: BackgroundShellEntry): string {
type AgentTaskEntry = BackgroundTaskEntry & {
kind: 'agent';
resumeBlockedReason?: string;
};

type ShellTaskEntry = BackgroundShellEntry & { kind: 'shell' };

type TaskEntry = AgentTaskEntry | ShellTaskEntry;

function statusLabel(entry: TaskEntry): string {
if (entry.kind === 'agent') {
switch (entry.status) {
case 'completed':
return 'completed';
case 'failed':
return `failed: ${entry.error ?? 'unknown error'}`;
case 'cancelled':
return 'cancelled';
case 'paused':
return entry.resumeBlockedReason
? `paused (resume blocked): ${entry.resumeBlockedReason}`
: 'paused';
case 'running':
default:
return 'running';
}
}

switch (entry.status) {
case 'completed':
return `completed (exit ${entry.exitCode ?? '?'})`;
Expand All @@ -21,10 +52,25 @@ function statusLabel(entry: BackgroundShellEntry): string {
case 'running':
return 'running';
default:
return entry.status;
return 'running';
}
}

function taskLabel(entry: TaskEntry): string {
if (entry.kind === 'agent') {
return buildBackgroundEntryLabel(entry);
}
return entry.command;
}

function taskId(entry: TaskEntry): string {
return entry.kind === 'agent' ? entry.agentId : entry.shellId;
}

function taskOutputPath(entry: TaskEntry): string | undefined {
return entry.kind === 'agent' ? entry.outputFile : entry.outputPath;
}

export const tasksCommand: SlashCommand = {
name: 'tasks',
get description() {
Expand All @@ -42,31 +88,44 @@ export const tasksCommand: SlashCommand = {
};
}

const entries = config.getBackgroundShellRegistry().getAll();
const agentEntries: AgentTaskEntry[] = config
.getBackgroundTaskRegistry()
.getAll()
.map((entry) => ({ ...entry, kind: 'agent' as const }));
const shellEntries: ShellTaskEntry[] = config
.getBackgroundShellRegistry()
.getAll()
.map((entry) => ({ ...entry, kind: 'shell' as const }));
const entries = [...agentEntries, ...shellEntries].sort(
(a, b) => a.startTime - b.startTime,
);

if (entries.length === 0) {
return {
type: 'message' as const,
messageType: 'info' as const,
content: 'No background shells.',
content: 'No background tasks.',
};
}

const now = Date.now();
const lines: string[] = [
`Background shells (${entries.length} total)`,
'',
];
const lines: string[] = [`Background tasks (${entries.length} total)`, ''];
for (const entry of entries) {
const endTime = entry.endTime ?? now;
const runtime = formatDuration(endTime - entry.startTime, {
hideTrailingZeros: true,
});
const pidPart = entry.pid !== undefined ? ` pid=${entry.pid}` : '';
const pidPart =
entry.kind === 'shell' && entry.pid !== undefined
? ` pid=${entry.pid}`
: '';
lines.push(
`[${entry.shellId}] ${statusLabel(entry)} ${runtime}${pidPart} ${entry.command}`,
`[${taskId(entry)}] ${statusLabel(entry)} ${runtime}${pidPart} ${taskLabel(entry)}`,
);
lines.push(` output: ${entry.outputPath}`);
const outputPath = taskOutputPath(entry);
if (outputPath) {
lines.push(` output: ${outputPath}`);
}
}

return {
Expand Down
Loading
Loading