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
20 changes: 20 additions & 0 deletions apps/host-daemon/src/command-dispatch-support.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import { CompetingTurnError } from "@bb/agent-runtime";
import { COMPETING_TURN_ERROR_CODE } from "@bb/host-daemon-contract";
import { describe, expect, it } from "vitest";
import {
CommandDispatchError,
getErrorCode,
isExpectedOnlineRpcFailureError,
} from "./command-dispatch-support.js";

describe("command dispatch support", () => {
it("reports a runtime competing-turn refusal with the contract error code", () => {
expect(getErrorCode(new CompetingTurnError("thread-1"))).toBe(
COMPETING_TURN_ERROR_CODE,
);
expect(
getErrorCode(
new CommandDispatchError(
COMPETING_TURN_ERROR_CODE,
"Refusing to start a competing turn while thread-1 is still starting",
),
),
).toBe("competing_turn");
expect(getErrorCode(new Error("Refusing to start a competing turn"))).toBe(
"command_failed",
);
});

it("classifies oversized file reads as expected RPC failures", () => {
expect(
isExpectedOnlineRpcFailureError(
Expand Down
29 changes: 18 additions & 11 deletions apps/host-daemon/src/command-dispatch-support.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { DesktopBrowserBroker } from "./desktop-browser-broker.js";
import type { AgentRuntimeBridgeLaunch } from "@bb/agent-runtime";
import {
CompetingTurnError,
type AgentRuntimeBridgeLaunch,
} from "@bb/agent-runtime";
import type { AvailableModel } from "@bb/domain";
import type { EventSink } from "./event-sink.js";
import type {
EnvironmentHookProgressMessage,
HostDaemonCommand,
ProviderHealthResult,
ProviderUsageResult,
HostDaemonBridgeLaunch,
HostDaemonInjectedSkillSource,
HostDaemonOnlineRpcCommand,
HostDaemonConnectTunnelIdentity,
WorkspaceContext,
import {
COMPETING_TURN_ERROR_CODE,
type EnvironmentHookProgressMessage,
type HostDaemonCommand,
type ProviderHealthResult,
type ProviderUsageResult,
type HostDaemonBridgeLaunch,
type HostDaemonInjectedSkillSource,
type HostDaemonOnlineRpcCommand,
type HostDaemonConnectTunnelIdentity,
type WorkspaceContext,
} from "@bb/host-daemon-contract";
import type {
ProviderInstallationCommand,
Expand Down Expand Up @@ -186,6 +190,9 @@ export function getErrorCode(error: unknown): string {
if (error instanceof CommandDispatchError) {
return error.code;
}
if (error instanceof CompetingTurnError) {
return COMPETING_TURN_ERROR_CODE;
}
if (isStructuredSpawnMissingExecutableError(error)) {
return "missing_executable";
}
Expand Down
55 changes: 35 additions & 20 deletions apps/host-daemon/src/command-dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,7 @@ describe("dispatchCommand", () => {
.mockReturnValueOnce(newRuntime);
const manager = new RuntimeManager({
createRuntime: createRuntimeSpy,
provisionWorkspace: async (args) =>
createWorkspace(args.path),
provisionWorkspace: async (args) => createWorkspace(args.path),
});
await manager.ensureEnvironment({
environmentId: "env-old",
Expand Down Expand Up @@ -998,7 +997,7 @@ describe("dispatchCommand", () => {
});
});

it("skips a release when a turn started after the server read the thread", async () => {
it("reports a retained turn instead of releasing a turn that started after the server read the thread", async () => {
const runtime = createRuntime();
const manager = new RuntimeManager({
createRuntime: () => runtime,
Expand All @@ -1009,6 +1008,18 @@ describe("dispatchCommand", () => {
workspacePath: "/tmp/bb-release-race",
});
runtime.setActiveTurn("thread-1", "turn-new");
const options = {
dataDir: "/tmp/bb-data",
logger: silentLogger,
eventSink: { emit: vi.fn(), flush: vi.fn(async () => undefined) },
fetchProjectAttachment: async () => {
throw new Error("Unexpected project attachment fetch");
},
fetchPluginHostArtifact: fetchDispatchTestArtifact,
...unexpectedProviderMaintenance,
runtimeManager: manager,
threadStorageRootPath: "/tmp/bb-thread-storage",
};

const result = await dispatchCommand(
{
Expand All @@ -1017,23 +1028,29 @@ describe("dispatchCommand", () => {
environmentId: "env-release-race",
threadId: "thread-1",
},
{
dataDir: "/tmp/bb-data",
logger: silentLogger,
eventSink: { emit: vi.fn(), flush: vi.fn(async () => undefined) },
fetchProjectAttachment: async () => {
throw new Error("Unexpected project attachment fetch");
},
fetchPluginHostArtifact: fetchDispatchTestArtifact,
...unexpectedProviderMaintenance,
runtimeManager: manager,
threadStorageRootPath: "/tmp/bb-thread-storage",
},
options,
);

expect(runtime.stopThread).not.toHaveBeenCalled();
expect(runtime.getActiveTurnId("thread-1")).toBe("turn-new");
expect(result).toEqual({ providerCheckpointId: null });
expect(result).toEqual({
providerCheckpointId: null,
activeTurnRetained: true,
});

const interrupted = await dispatchCommand(
{
type: "thread.stop",
intent: "interrupt",
environmentId: "env-release-race",
threadId: "thread-1",
},
options,
);

expect(runtime.stopThread).toHaveBeenCalledWith({ threadId: "thread-1" });
expect(runtime.getActiveTurnId("thread-1")).toBeNull();
expect(interrupted).toEqual({ providerCheckpointId: null });
});

it("treats thread.stop as successful when no runtime holds the thread", async () => {
Expand Down Expand Up @@ -1147,8 +1164,7 @@ describe("dispatchCommand", () => {
.mockReturnValueOnce(newRuntime);
const manager = new RuntimeManager({
createRuntime: createRuntimeSpy,
provisionWorkspace: async (args) =>
createWorkspace(args.path),
provisionWorkspace: async (args) => createWorkspace(args.path),
});
await manager.ensureEnvironment({
environmentId: "env-old",
Expand Down Expand Up @@ -1198,8 +1214,7 @@ describe("dispatchCommand", () => {
.mockReturnValueOnce(newRuntime);
const manager = new RuntimeManager({
createRuntime: createRuntimeSpy,
provisionWorkspace: async (args) =>
createWorkspace(args.path),
provisionWorkspace: async (args) => createWorkspace(args.path),
});
await manager.ensureEnvironment({
environmentId: "env-old",
Expand Down
4 changes: 2 additions & 2 deletions apps/host-daemon/src/command-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type RuntimeStopCommand =
async function stopThreadRuntime(
command: RuntimeStopCommand,
options: CommandDispatchOptions,
): Promise<{ providerCheckpointId: string | null }> {
): Promise<HostDaemonCommandResult<"thread.stop">> {
const released =
await options.runtimeManager.releaseThreadFromOtherEnvironments({
activeTurn: "interrupt",
Expand All @@ -118,7 +118,7 @@ async function stopThreadRuntime(
entry.runtime.getActiveTurnId(command.threadId) !== null
) {
await options.eventSink.flush();
return { providerCheckpointId };
return { providerCheckpointId, activeTurnRetained: true };
}
if (command.type !== "thread.stop" || command.intent !== "release") {
await entry.runtime.waitForActiveTurn(command.threadId, {
Expand Down
8 changes: 6 additions & 2 deletions apps/host-daemon/src/command-handlers/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import fs from "node:fs/promises";
import path from "node:path";
import type { AgentRuntimeBridgeLaunch } from "@bb/agent-runtime";
import { flattenPromptInputGroups } from "@bb/domain";
import type { HostDaemonCommandResult } from "@bb/host-daemon-contract";
import {
COMPETING_TURN_ERROR_CODE,
type HostDaemonCommandResult,
} from "@bb/host-daemon-contract";
import type { RuntimeEntry } from "../runtime-manager.js";
import {
CommandDispatchError,
Expand Down Expand Up @@ -404,7 +407,8 @@ async function resolveLiveSubmittedTurnTarget(
return refreshedTurnId;
}
if (entry.runtime.getLiveThreadIds().includes(command.threadId)) {
throw new Error(
throw new CommandDispatchError(
COMPETING_TURN_ERROR_CODE,
`Refusing to start a competing turn while ${command.threadId} is still starting`,
);
}
Expand Down
Loading
Loading