Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,34 @@ describe("ManagerAssistantOutputTracker", () => {
tracker.activateTurn("manager-1", WEB_TARGET);

tracker.handleRuntimeEvent("manager-1", { type: "tool_execution_start", toolName: "shell", toolCallId: "t1", args: {} });
tracker.handleRuntimeEvent("manager-1", assistantMessageEnd("premature"));
tracker.handleRuntimeEvent("manager-1", assistantMessageEnd("premature", { stopReason: "toolUse" }));
tracker.handleRuntimeEvent("manager-1", { type: "turn_end", toolResults: [] });

expect(emitted).toHaveLength(1);
expect(emitted[0]).toMatchObject({ text: "premature", source: "assistant_progress" });
});

it("leaves clean finals to the final projector even while a tool remains open", () => {
const { tracker, emitted } = createTracker();
tracker.activateTurn("manager-1", WEB_TARGET);
tracker.handleRuntimeEvent("manager-1", { type: "tool_execution_start", toolName: "shell", toolCallId: "t1", args: {} });
tracker.handleRuntimeEvent("manager-1", assistantMessageEnd("Background check is still running."));

expect(emitted).toEqual([]);
});

it("waits for streamed text to be classified before publishing progress during open tool work", () => {
const { tracker, emitted } = createTracker();
tracker.activateTurn("manager-1", WEB_TARGET);
tracker.handleRuntimeEvent("manager-1", { type: "tool_execution_start", toolName: "shell", toolCallId: "t1", args: {} });
tracker.handleRuntimeEvent("manager-1", assistantMessageUpdate("Still checking", { stopReason: "toolUse" }));
tracker.handleRuntimeEvent("manager-1", assistantMessageUpdate("Still checking the task.", { stopReason: "toolUse" }));
expect(emitted).toEqual([]);
tracker.handleRuntimeEvent("manager-1", assistantMessageEnd("Still checking the task.", { stopReason: "toolUse" }));
expect(emitted).toHaveLength(1);
expect(emitted[0]).toMatchObject({ text: "Still checking the task.", source: "assistant_progress" });
});

it("does not promote clean message_end text to assistant_progress when later tool work starts", () => {
const { tracker, emitted } = createTracker();
tracker.activateTurn("manager-1", WEB_TARGET);
Expand Down
79 changes: 78 additions & 1 deletion apps/backend/src/swarm/__tests__/runtime-event-projector.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdtemp, readFile } from "node:fs/promises";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it, vi } from "vitest";
Expand All @@ -14,6 +14,7 @@ import type { WorkerActivityStateLike, WorkerStallStateLike } from "../runtime/w
import { RuntimeRecoveryState } from "../runtime/runtime-recovery-state.js";
import type { AgentDescriptor } from "../types.js";
import { getMessageRoutingReceiptsPath } from "../session/message-routing-receipts.js";
import { ConversationProjector } from "../session/conversation-projector.js";

function baseDescriptor(overrides: Partial<AgentDescriptor> & Pick<AgentDescriptor, "agentId" | "role" | "managerId">): AgentDescriptor {
const now = "2026-05-06T00:00:00.000Z";
Expand Down Expand Up @@ -1028,6 +1029,82 @@ describe("RuntimeEventProjector", () => {
expect(deps.conversationProjector.emitConversationMessage).toHaveBeenCalledTimes(2);
});

it.each([
{ priorProgress: false, streamed: false },
{ priorProgress: true, streamed: false },
{ priorProgress: false, streamed: true },
{ priorProgress: true, streamed: true },
])("publishes a clean final once with background work open ($priorProgress prior progress, $streamed streamed), live and after restart", async ({ priorProgress, streamed }) => {
const root = await mkdtemp(join(tmpdir(), "forge-background-final-"));
try {
const { projector, deps, descriptors } = createHarness();
const manager = baseDescriptor({
agentId: "manager-background", role: "manager", managerId: "manager-background",
cwd: root, sessionFile: join(root, "session.jsonl"),
});
descriptors.set(manager.agentId, manager);
const emitServerEvent = vi.fn();
const createConversation = () => new ConversationProjector({
descriptors, runtimes: new Map(), conversationEntriesByAgentId: new Map(),
now: deps.now, emitServerEvent, logDebug: vi.fn(),
});
const conversation = createConversation();
deps.conversationProjector = conversation;
projector.activateManagerAssistantOutputTurn(manager.agentId, { kind: "session_transcript", channel: "web" });
await projector.projectEvent({ agentId: manager.agentId, event: {
type: "tool_execution_start", toolName: "Bash", toolCallId: "background-1", args: {},
} });
await projector.projectEvent({ agentId: manager.agentId, event: {
type: "tool_execution_update", toolName: "Bash", toolCallId: "background-1",
executionState: "background", partialResult: { status: "running_in_background" },
} });
if (priorProgress) {
await projector.projectEvent({ agentId: manager.agentId, event: assistantEnd("Checking the background task.", { stopReason: "toolUse" }) });
}
const final = assistantEnd("The check is continuing in the background.", { stopReason: "stop" });
if (streamed) {
await projector.projectEvent({ agentId: manager.agentId, event: {
type: "message_update", message: { role: "assistant", stopReason: "toolUse", content: "The check is continuing" },
} });
await projector.projectEvent({ agentId: manager.agentId, event: {
type: "message_update", message: { role: "assistant", stopReason: "toolUse", content: "The check is continuing in the background." },
} });
}
await projector.projectEvent({ agentId: manager.agentId, event: final });
const expected = [
...(priorProgress ? [{ source: "assistant_progress", text: "Checking the background task." }] : []),
{ source: "assistant_output", text: "The check is continuing in the background." },
];
const messages = () => conversation.getConversationHistory(manager.agentId)
.filter(entry => entry.type === "conversation_message" && entry.role === "assistant")
.map(entry => ({ source: entry.source, text: entry.text }));
expect(messages()).toEqual(expected);
await projector.projectEvent({ agentId: manager.agentId, event: { type: "turn_end", toolResults: [] } });
await projector.projectEvent({ agentId: manager.agentId, event: {
type: "tool_execution_end", toolName: "Bash", toolCallId: "background-1", result: { status: "completed" }, isError: false,
} });
await projector.projectEvent({ agentId: manager.agentId, event: { type: "agent_end", settledAssistantMessage: final.message } });
expect(messages()).toEqual(expected);
const live = emitServerEvent.mock.calls
.filter(([name, event]) => name === "conversation_message" && event.role === "assistant")
.map(([, event]) => ({ source: event.source, text: event.text }));
expect(live).toEqual(expected);
// Identical content in a subsequent turn is a new reply, not a duplicate.
projector.activateManagerAssistantOutputTurn(manager.agentId, { kind: "session_transcript", channel: "web" });
await projector.projectEvent({ agentId: manager.agentId, event: final });
expected.push({ source: "assistant_output", text: "The check is continuing in the background." });
expect(messages()).toEqual(expected);
await conversation.flushPendingHistoryCacheWrites();
const restarted = createConversation();
expect(restarted.getConversationHistory(manager.agentId)
.filter(entry => entry.type === "conversation_message" && entry.role === "assistant")
.map(entry => ({ source: entry.source, text: entry.text }))).toEqual(expected);
await restarted.flushPendingHistoryCacheWrites();
} finally {
await rm(root, { recursive: true, force: true });
}
});

it("projects a post-tool clean final immediately even when no later turn_end arrives", async () => {
const { projector, deps, descriptors } = createHarness();
const manager = baseDescriptor({ agentId: "manager-rapa", role: "manager", managerId: "manager-rapa" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { MessageRouteDecision } from "../message-router.js";
import {
getToolLikeMessageBlocks,
hasNoReplySentinelLine,
isCleanManagerAssistantFinalMessage,
isToolUseStopReason,
messageHasIneligibleStopOrError,
} from "./manager-assistant-final-message.js";
Expand Down Expand Up @@ -152,6 +153,12 @@ export class ManagerAssistantOutputTracker {

activeTurn.candidate = undefined;

// Clean finals belong to the final projector, even when background tools
// keep the turn active. Do not publish the same message as progress too.
if (isCleanManagerAssistantFinalMessage(event)) {
return;
}

if (activeTurn.target.kind !== "session_transcript") {
return;
}
Expand Down Expand Up @@ -198,7 +205,11 @@ export class ManagerAssistantOutputTracker {
kind: "progress",
sourceContext: activeTurn.target.sourceContext ?? { channel: activeTurn.target.channel },
};
this.emitProgressCandidateIfEligible(agentId, activeTurn, { allowOpenToolCalls: true });
// A text-only stream may still become a clean final. Wait for its
// message boundary (or attached tool work) before publishing progress.
if (!options?.provisional) {
this.emitProgressCandidateIfEligible(agentId, activeTurn, { allowOpenToolCalls: true });
}
return;
}

Expand Down
Loading