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
17 changes: 4 additions & 13 deletions packages/core/src/agents/arena/ArenaManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
isTerminalStatus,
isSettledStatus,
isSuccessStatus,
lastVisibleAnswer,
} from '../runtime/agent-types.js';
import {
logArenaSessionStarted,
Expand Down Expand Up @@ -1674,19 +1675,9 @@ export class ArenaManager {
transcript: ArenaTranscriptEntry[] | undefined,
): string | undefined {
if (!transcript) return undefined;

for (let i = transcript.length - 1; i >= 0; i--) {
const message = transcript[i]!;
if (
message.role === 'assistant' &&
!message.thought &&
message.content.trim()
) {
return message.content.trim();
}
}

return undefined;
// Shared with TeamManager's pre-attach recovery: the most recent
// non-empty, non-thought assistant message wins.
return lastVisibleAnswer(transcript);
}

private async addApproachSummaries(
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/agents/backends/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import type { Content } from '@google/genai';
import type { AnsiOutput } from '../../utils/terminalSerializer.js';
import type {
AgentMessage,
AgentStatus,
PromptConfig,
ModelConfig,
Expand Down Expand Up @@ -142,6 +143,13 @@ export interface TeamAgentHandle {
* handles may not.
*/
getError?(): string | undefined;
/**
* Conversation message history. Optional: in-process handles
* (AgentInteractive, FakeAgent) provide it; PTY handles don't.
* TeamManager reads it to recover round text emitted before its
* event bridge attached.
*/
getMessages?(): readonly AgentMessage[];
}

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/agents/runtime/agent-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ export interface AgentMessage {
metadata?: Record<string, unknown>;
}

/**
* The last model-visible answer in a message history, or undefined
* when there is none. Scans most-recent-first; the first non-empty,
* non-thought assistant message wins. Shared by the team pre-attach
* recovery (TeamManager) and the arena final-text fallback
* (ArenaManager) so both apply the same selection rule.
*/
export function lastVisibleAnswer(
messages: readonly AgentMessage[],
): string | undefined {
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i]!;
if (message.role !== 'assistant' || message.thought) continue;
const text = message.content.trim();
if (text) return text;
}
return undefined;
}

/**
* Snapshot of in-progress streaming state for UI mid-switch handoff.
* Returned by AgentInteractive.getInProgressStream().
Expand Down
289 changes: 289 additions & 0 deletions packages/core/src/agents/team/TeamManager.initial-result.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

/**
* Regression tests for #10211: the initial teammate round can complete
* (final round text + IDLE) before TeamManager.setupEventBridge attaches,
* because spawnTeammate awaits backend.spawnAgent() first and the emitter
* does not buffer events for late subscribers. The leader must still
* receive the initial round's result exactly once.
*/

import { describe, it, expect, vi, afterEach } from 'vitest';
import { TeamCoordinationHarness } from './test-utils/coordination-harness.js';
import { Storage } from '../../config/storage.js';
import { AgentStatus } from '../runtime/agent-types.js';
import { AgentEventType } from '../runtime/agent-events.js';

vi.mock('../../config/storage.js', async (importOriginal) => {
const original =
await importOriginal<typeof import('../../config/storage.js')>();
let mockGlobalDir = '';
return {
...original,
Storage: {
...original.Storage,
getGlobalQwenDir: () => mockGlobalDir,
__setMockGlobalDir: (dir: string) => {
mockGlobalDir = dir;
},
},
};
});

function setMockDir(dir: string): void {
(
Storage as unknown as {
__setMockGlobalDir: (d: string) => void;
}
).__setMockGlobalDir(dir);
}

/** Let queued fire-and-forget coordination work settle. */
async function settleAsyncWork(): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, 50));
}

describe('initial teammate result before event bridge attachment (#10211)', () => {
let harness: TeamCoordinationHarness | undefined;

afterEach(async () => {
await harness?.cleanup();
harness = undefined;
});

async function createHarness(): Promise<TeamCoordinationHarness> {
const h = await TeamCoordinationHarness.create();
setMockDir(h.tmpDir);
harness = h;
return h;
}

it('reports final text to the leader when the initial round completes before spawnAgent resolves', async () => {
const h = await createHarness();

// onStart runs inside FakeBackend.spawnAgent(), i.e. before
// TeamManager.setupEventBridge() subscribes. Emits the final round
// text and settles IDLE while spawnAgent() is still resolving —
// the in-process race from the issue.
await h.spawnTeammate('worker', {
onStart: (agent) => {
agent.setStatus(AgentStatus.RUNNING);
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 1,
text: 'initial result',
thoughtText: '',
timestamp: Date.now(),
});
agent.setStatus(AgentStatus.IDLE);
},
});

await vi.waitFor(async () => {
expect(await h.teamManager.getLeaderMessages()).toEqual([
expect.objectContaining({
from: 'worker',
text: 'initial result',
}),
]);
});

// Exactly once: after coordination settles, no duplicate report
// for the same round may arrive.
await settleAsyncWork();
expect(await h.teamManager.getLeaderMessages()).toEqual([]);
});

it('does not re-report the initial result when a later round completes live', async () => {
const h = await createHarness();

await h.spawnTeammate('worker', {
onStart: (agent) => {
agent.setStatus(AgentStatus.RUNNING);
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 1,
text: 'initial result',
thoughtText: '',
timestamp: Date.now(),
});
agent.setStatus(AgentStatus.IDLE);
},
onMessage: (_message, agent) => {
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 2,
text: 'follow-up result',
thoughtText: '',
timestamp: Date.now(),
});
},
});

await vi.waitFor(async () => {
expect(await h.teamManager.getLeaderMessages()).toEqual([
expect.objectContaining({ text: 'initial result' }),
]);
});

await h.teamManager.sendMessage('worker', 'next task', 'leader');
await h.waitForStatus('worker', AgentStatus.IDLE);

// The live second round reports its own text exactly once; the
// pre-attach initial result must not be reported again.
await vi.waitFor(async () => {
expect(await h.teamManager.getLeaderMessages()).toEqual([
expect.objectContaining({ text: 'follow-up result' }),
]);
});

await settleAsyncWork();
expect(await h.teamManager.getLeaderMessages()).toEqual([]);
});

it('does not report anything at spawn when no round ran before the bridge attached', async () => {
const h = await createHarness();

// Plain spawn: the harness agent is IDLE at attach time but never
// emitted round text (no pre-attach round). The attach-time
// reconciliation must not invent a report.
await h.spawnTeammate('worker');
await settleAsyncWork();

expect(await h.teamManager.getLeaderMessages()).toEqual([]);

// The live path still works afterwards.
await h.teamManager.sendMessage('worker', 'task', 'leader');
await h.waitForStatus('worker', AgentStatus.IDLE);
await vi.waitFor(async () => {
expect(await h.teamManager.getLeaderMessages()).toEqual([
expect.objectContaining({
text: expect.stringContaining(
'completed a turn without a model-visible final answer',
),
}),
]);
});
});

it('still reports the recovered result when the teammate sent an explicit leader message pre-attach', async () => {
const h = await createHarness();

// The default initialTask prompt instructs teammates to report via
// send_message(to: "leader"). Such a send goes through
// TeamManager.sendMessage synchronously — no event bridge needed —
// and marks the sender as having reported explicitly. The seed must
// clear that flag exactly like the live onRoundText handler does,
// or the replayed IDLE settlement skips the recovered answer and
// the leader receives zero automatic reports of the initial result.
await h.spawnTeammate('worker', {
onStart: async (agent) => {
agent.setStatus(AgentStatus.RUNNING);
await h.teamManager.sendMessage('leader', 'progress note', 'worker');
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 1,
text: 'initial result',
thoughtText: '',
timestamp: Date.now(),
});
agent.setStatus(AgentStatus.IDLE);
},
});

await vi.waitFor(async () => {
expect(await h.teamManager.getLeaderMessages()).toEqual([
expect.objectContaining({ from: 'worker', text: 'progress note' }),
expect.objectContaining({ from: 'worker', text: 'initial result' }),
]);
});

// Exactly once: the explicit note plus one automatic forwarding.
await settleAsyncWork();
expect(await h.teamManager.getLeaderMessages()).toEqual([]);
});

it('reports the last pre-attach round text when the round had multiple turns', async () => {
const h = await createHarness();

// A multi-turn pre-attach round emits several ROUND_TEXT events;
// the recovery scan must walk the history backwards so the most
// recent non-empty visible answer wins, not the earliest one.
await h.spawnTeammate('worker', {
onStart: (agent) => {
agent.setStatus(AgentStatus.RUNNING);
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 1,
text: 'early turn answer',
thoughtText: '',
timestamp: Date.now(),
});
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 1,
text: 'final turn answer',
thoughtText: '',
timestamp: Date.now(),
});
agent.setStatus(AgentStatus.IDLE);
},
});

await vi.waitFor(async () => {
expect(await h.teamManager.getLeaderMessages()).toEqual([
expect.objectContaining({
from: 'worker',
text: 'final turn answer',
}),
]);
});

// The earlier turn text must never be reported on its own.
await settleAsyncWork();
expect(await h.teamManager.getLeaderMessages()).toEqual([]);
});

it('never reports thought-only round text, falling back to the earlier visible answer', async () => {
const h = await createHarness();

// AgentCore emits ROUND_TEXT whenever roundThoughtText is
// non-empty, so a trailing thought-only event is a reachable
// pre-attach shape. The recovery must skip thought messages and
// never surface internal reasoning as the round's final answer.
await h.spawnTeammate('worker', {
onStart: (agent) => {
agent.setStatus(AgentStatus.RUNNING);
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 1,
text: 'visible answer',
thoughtText: '',
timestamp: Date.now(),
});
agent.getEventEmitter().emit(AgentEventType.ROUND_TEXT, {
subagentId: agent.agentId,
round: 1,
text: '',
thoughtText: 'internal reasoning about the task',
timestamp: Date.now(),
});
agent.setStatus(AgentStatus.IDLE);
},
});

await vi.waitFor(async () => {
expect(await h.teamManager.getLeaderMessages()).toEqual([
expect.objectContaining({ from: 'worker', text: 'visible answer' }),
]);
});

// Nothing else — in particular no thought content — may arrive.
await settleAsyncWork();
expect(await h.teamManager.getLeaderMessages()).toEqual([]);
});
});
Loading
Loading