feat: add omx orchestration backend selection

This commit is contained in:
kris
2026-04-03 03:17:12 +08:00
parent 60f5e2d7d6
commit ec45bed59f
18 changed files with 1993 additions and 20 deletions

View File

@@ -151,9 +151,18 @@ async function createDispatchPlanForTest() {
);
assert.equal(response.status, 200);
const payload = (await response.json()) as {
dispatchPlan: { planId: string; targets: Array<{ projectId: string }> } | null;
dispatchPlan:
| {
planId: string;
targets: Array<{ projectId: string }>;
orchestrationBackendId?: string;
orchestrationBackendLabel?: string;
}
| null;
};
assert.ok(payload.dispatchPlan, "expected seeded dispatch plan");
assert.equal(payload.dispatchPlan?.orchestrationBackendId, "boss-native-orchestrator");
assert.equal(payload.dispatchPlan?.orchestrationBackendLabel, "Boss Native Orchestrator");
return { groupProject, dispatchPlan: payload.dispatchPlan };
}
@@ -195,8 +204,20 @@ test("POST /api/v1/projects/[projectId]/dispatch-plans/[planId]/confirm confirms
const payload = (await response.json()) as {
ok: boolean;
plan: { planId: string; status: string; confirmedTargetProjectIds: string[] };
executions: Array<{ planId: string; targetProjectId: string; status: string }>;
plan: {
planId: string;
status: string;
confirmedTargetProjectIds: string[];
orchestrationBackendId?: string;
orchestrationBackendLabel?: string;
};
executions: Array<{
planId: string;
targetProjectId: string;
status: string;
orchestrationBackendId?: string;
orchestrationBackendLabel?: string;
}>;
notice: { kind: string; body: string } | null;
collaborationGate: {
isGroup: boolean;
@@ -231,6 +252,23 @@ test("POST /api/v1/projects/[projectId]/dispatch-plans/[planId]/confirm confirms
message.body.includes("已确认下发到 1 个线程"),
);
assert.ok(notice, "expected a master-agent notice in the group chat after confirmation");
const confirmedPlan = nextState.dispatchPlans.find((plan) => plan.planId === dispatchPlan.planId);
assert.ok(confirmedPlan, "expected confirmed dispatch plan in state");
assert.equal(confirmedPlan?.orchestrationBackendId, "boss-native-orchestrator");
assert.equal(confirmedPlan?.orchestrationBackendLabel, "Boss Native Orchestrator");
const createdExecution = nextState.dispatchExecutions.find((item) => item.planId === dispatchPlan.planId);
assert.ok(createdExecution, "expected dispatch execution in state");
assert.equal(createdExecution?.orchestrationBackendId, "boss-native-orchestrator");
assert.equal(createdExecution?.orchestrationBackendLabel, "Boss Native Orchestrator");
const executionTask = nextState.masterAgentTasks.find(
(task) =>
task.taskType === "dispatch_execution" &&
task.projectId === groupProject.id &&
task.targetProjectId === approvedTargetProjectId,
);
assert.ok(executionTask, "expected queued dispatch execution task");
assert.equal(executionTask?.orchestrationBackendId, "boss-native-orchestrator");
assert.equal(executionTask?.orchestrationBackendLabel, "Boss Native Orchestrator");
});
test("confirming a dispatch plan marks approval_required groups as approved", async () => {

View File

@@ -0,0 +1,270 @@
import test from "node:test";
import assert from "node:assert/strict";
import os from "node:os";
import path from "node:path";
import { mkdtemp, rm } from "node:fs/promises";
import { NextRequest } from "next/server";
let runtimeRoot = "";
let getRoute: (typeof import("../src/app/api/v1/projects/[projectId]/orchestration-backend/route"))["GET"];
let patchRoute: (typeof import("../src/app/api/v1/projects/[projectId]/orchestration-backend/route"))["PATCH"];
let postMessageRoute: (typeof import("../src/app/api/v1/projects/[projectId]/messages/route"))["POST"];
let confirmDispatchPlanRoute: (typeof import("../src/app/api/v1/projects/[projectId]/dispatch-plans/[planId]/confirm/route"))["POST"];
let createAuthSession: (typeof import("../src/lib/boss-data"))["createAuthSession"];
let createProjectGroupChat: (typeof import("../src/lib/boss-data"))["createProjectGroupChat"];
let isDispatchableThreadProject: (typeof import("../src/lib/boss-data"))["isDispatchableThreadProject"];
let readState: (typeof import("../src/lib/boss-data"))["readState"];
let writeState: (typeof import("../src/lib/boss-data"))["writeState"];
let AUTH_SESSION_COOKIE = "";
let baseState: Awaited<ReturnType<typeof import("../src/lib/boss-data")["readState"]>>;
const originalEnv = {
BOSS_OMX_ENABLED: process.env.BOSS_OMX_ENABLED,
BOSS_OMX_COMMAND: process.env.BOSS_OMX_COMMAND,
BOSS_OMX_ARGS: process.env.BOSS_OMX_ARGS,
BOSS_OMX_WORKDIR: process.env.BOSS_OMX_WORKDIR,
BOSS_OMX_TIMEOUT_MS: process.env.BOSS_OMX_TIMEOUT_MS,
};
async function setup() {
if (runtimeRoot) return;
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-omx-route-"));
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
const [orchestrationRoute, messageRoute, confirmRoute, data, auth] = await Promise.all([
import("../src/app/api/v1/projects/[projectId]/orchestration-backend/route.ts"),
import("../src/app/api/v1/projects/[projectId]/messages/route.ts"),
import("../src/app/api/v1/projects/[projectId]/dispatch-plans/[planId]/confirm/route.ts"),
import("../src/lib/boss-data.ts"),
import("../src/lib/boss-auth.ts"),
]);
getRoute = orchestrationRoute.GET;
patchRoute = orchestrationRoute.PATCH;
postMessageRoute = messageRoute.POST;
confirmDispatchPlanRoute = confirmRoute.POST;
createAuthSession = data.createAuthSession;
createProjectGroupChat = data.createProjectGroupChat;
isDispatchableThreadProject = data.isDispatchableThreadProject;
readState = data.readState;
writeState = data.writeState;
baseState = structuredClone(await readState());
AUTH_SESSION_COOKIE = auth.AUTH_SESSION_COOKIE;
}
async function authedRequest(url: string, method: "GET" | "PATCH" | "POST", body?: unknown) {
const session = await createAuthSession({
account: "17600003315",
role: "highest_admin",
displayName: "Boss 超级管理员",
loginMethod: "password",
});
return new NextRequest(url, {
method,
headers: {
"content-type": "application/json",
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
},
body: body ? JSON.stringify(body) : undefined,
});
}
async function ensureTwoSingleThreadProjects() {
const state = await readState();
const singles = state.projects.filter((project) => isDispatchableThreadProject(project));
if (singles.length >= 2) {
return singles;
}
assert.ok(singles[0], "expected at least one dispatchable project");
const seed = singles[0];
const clone = {
...seed,
id: "omx-thread-b",
name: "Boss OMX 副线程",
threadMeta: {
...seed.threadMeta,
projectId: "omx-thread-b",
threadId: "thread-omx-b",
threadDisplayName: "OMX 副线程",
codexThreadRef: "thread-omx-b",
codexFolderRef: "/Users/kris/code/boss",
},
messages: [
{
id: "msg-omx-seed",
sender: "device" as const,
senderLabel: "Mac Studio / Codex",
body: "等待群聊下发。",
sentAt: "2026-04-03T10:00:00+08:00",
kind: "text" as const,
},
],
};
await writeState({
...state,
projects: [...state.projects, clone],
});
const nextState = await readState();
return nextState.projects.filter((project) => isDispatchableThreadProject(project));
}
function configureOmxAvailable() {
process.env.BOSS_OMX_ENABLED = "true";
process.env.BOSS_OMX_COMMAND = process.execPath;
process.env.BOSS_OMX_ARGS = "/Users/kris/code/boss/scripts/omx-team-smoke.mjs";
process.env.BOSS_OMX_WORKDIR = "/Users/kris/code/boss";
process.env.BOSS_OMX_TIMEOUT_MS = "45000";
}
function restoreOmxEnv() {
process.env.BOSS_OMX_ENABLED = originalEnv.BOSS_OMX_ENABLED;
process.env.BOSS_OMX_COMMAND = originalEnv.BOSS_OMX_COMMAND;
process.env.BOSS_OMX_ARGS = originalEnv.BOSS_OMX_ARGS;
process.env.BOSS_OMX_WORKDIR = originalEnv.BOSS_OMX_WORKDIR;
process.env.BOSS_OMX_TIMEOUT_MS = originalEnv.BOSS_OMX_TIMEOUT_MS;
}
test.beforeEach(async () => {
await setup();
restoreOmxEnv();
await writeState(structuredClone(baseState));
});
test.after(async () => {
restoreOmxEnv();
if (runtimeRoot) {
await rm(runtimeRoot, { recursive: true, force: true });
}
});
test("GET orchestration backend returns null requested backend for default group chats", async () => {
const singles = await ensureTwoSingleThreadProjects();
const group = await createProjectGroupChat({
sourceProjectId: singles[0].id,
memberProjectIds: [singles[1].id],
createdBy: "17600003315",
});
const response = await getRoute(
await authedRequest(`http://127.0.0.1:3000/api/v1/projects/${group.id}/orchestration-backend`, "GET"),
{ params: Promise.resolve({ projectId: group.id }) },
);
assert.equal(response.status, 200);
const payload = (await response.json()) as {
ok: boolean;
requestedBackendId: string | null;
currentBackendId: string;
availableChoices: Array<{ backendId: string; current: boolean }>;
};
assert.equal(payload.ok, true);
assert.equal(payload.requestedBackendId, null);
assert.equal(payload.currentBackendId, "boss-native-orchestrator");
assert.equal(payload.availableChoices[0]?.backendId, "boss-native-orchestrator");
assert.equal(payload.availableChoices[0]?.current, true);
});
test("PATCH orchestration backend rejects omx when runtime is unavailable", async () => {
const singles = await ensureTwoSingleThreadProjects();
const group = await createProjectGroupChat({
sourceProjectId: singles[0].id,
memberProjectIds: [singles[1].id],
createdBy: "17600003315",
});
const response = await patchRoute(
await authedRequest(
`http://127.0.0.1:3000/api/v1/projects/${group.id}/orchestration-backend`,
"PATCH",
{ requestedBackendId: "omx-team" },
),
{ params: Promise.resolve({ projectId: group.id }) },
);
assert.equal(response.status, 200);
const payload = (await response.json()) as {
ok: boolean;
requestedBackendId: string;
currentBackendId: string;
omxAvailability: { selectable: boolean; reasonLabel: string };
};
assert.equal(payload.ok, true);
assert.equal(payload.requestedBackendId, "omx-team");
assert.equal(payload.currentBackendId, "boss-native-orchestrator");
assert.equal(payload.omxAvailability.selectable, false);
assert.equal(payload.omxAvailability.reasonLabel, "OMX Team Runtime 当前未启用。");
});
test("group dispatch plans and executions carry omx backend when selected and available", async () => {
configureOmxAvailable();
const singles = await ensureTwoSingleThreadProjects();
const group = await createProjectGroupChat({
sourceProjectId: singles[0].id,
memberProjectIds: [singles[1].id],
createdBy: "17600003315",
});
const saveResponse = await patchRoute(
await authedRequest(
`http://127.0.0.1:3000/api/v1/projects/${group.id}/orchestration-backend`,
"PATCH",
{ requestedBackendId: "omx-team" },
),
{ params: Promise.resolve({ projectId: group.id }) },
);
assert.equal(saveResponse.status, 200);
const postResponse = await postMessageRoute(
await authedRequest(
`http://127.0.0.1:3000/api/v1/projects/${group.id}/messages`,
"POST",
{ body: "请大家汇总一下今天的 OMX 联调阻塞点" },
),
{ params: Promise.resolve({ projectId: group.id }) },
);
assert.equal(postResponse.status, 200);
const postPayload = (await postResponse.json()) as {
ok: boolean;
dispatchPlan: null | {
planId: string;
orchestrationBackendId?: string;
orchestrationBackendLabel?: string;
};
};
assert.equal(postPayload.ok, true);
assert.ok(postPayload.dispatchPlan, "expected dispatch plan");
assert.equal(postPayload.dispatchPlan?.orchestrationBackendId, "omx-team");
assert.equal(postPayload.dispatchPlan?.orchestrationBackendLabel, "OMX Team Runtime");
const confirmResponse = await confirmDispatchPlanRoute(
await authedRequest(
`http://127.0.0.1:3000/api/v1/projects/${group.id}/dispatch-plans/${postPayload.dispatchPlan?.planId}/confirm`,
"POST",
{ approvedTargetProjectIds: [singles[0].id, singles[1].id] },
),
{ params: Promise.resolve({ projectId: group.id, planId: postPayload.dispatchPlan?.planId ?? "" }) },
);
assert.equal(confirmResponse.status, 200);
const confirmPayload = (await confirmResponse.json()) as {
ok: boolean;
plan: { orchestrationBackendId?: string };
executions: Array<{ orchestrationBackendId?: string; orchestrationBackendLabel?: string }>;
};
assert.equal(confirmPayload.ok, true);
assert.equal(confirmPayload.plan.orchestrationBackendId, "omx-team");
assert.ok(confirmPayload.executions.length > 0);
assert.ok(confirmPayload.executions.every((item) => item.orchestrationBackendId === "omx-team"));
assert.ok(confirmPayload.executions.every((item) => item.orchestrationBackendLabel === "OMX Team Runtime"));
const nextState = await readState();
const queuedTasks = nextState.masterAgentTasks.filter(
(task) => task.taskType === "dispatch_execution" && task.projectId === group.id,
);
assert.ok(
queuedTasks.some((task) => task.orchestrationBackendId === "omx-team"),
"expected dispatch execution tasks to carry omx metadata",
);
});

View File

@@ -0,0 +1,200 @@
import test from "node:test";
import assert from "node:assert/strict";
import os from "node:os";
import path from "node:path";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { NextRequest } from "next/server";
let runtimeRoot = "";
let AUTH_SESSION_COOKIE = "";
let createAuthSession: (typeof import("../src/lib/boss-data"))["createAuthSession"];
let readState: (typeof import("../src/lib/boss-data"))["readState"];
let orchestrationBackendRoute: typeof import("../src/app/api/v1/projects/[projectId]/orchestration-backend/route");
async function setup() {
if (runtimeRoot) return;
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-orchestration-backend-route-"));
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
const [data, auth, routeModule] = await Promise.all([
import("../src/lib/boss-data.ts"),
import("../src/lib/boss-auth.ts"),
import("../src/app/api/v1/projects/[projectId]/orchestration-backend/route.ts"),
]);
createAuthSession = data.createAuthSession;
readState = data.readState;
AUTH_SESSION_COOKIE = auth.AUTH_SESSION_COOKIE;
orchestrationBackendRoute = routeModule;
}
async function createAuthedHeaders() {
await setup();
const session = await createAuthSession({
account: "17600003315",
role: "highest_admin",
displayName: "Boss 超级管理员",
loginMethod: "password",
});
return {
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
"content-type": "application/json",
};
}
function snapshotOmxEnv() {
return {
BOSS_OMX_ENABLED: process.env.BOSS_OMX_ENABLED,
BOSS_OMX_COMMAND: process.env.BOSS_OMX_COMMAND,
BOSS_OMX_ARGS: process.env.BOSS_OMX_ARGS,
BOSS_OMX_WORKDIR: process.env.BOSS_OMX_WORKDIR,
};
}
function restoreOmxEnv(snapshot: ReturnType<typeof snapshotOmxEnv>) {
for (const [key, value] of Object.entries(snapshot)) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
}
test.after(async () => {
if (runtimeRoot) {
await rm(runtimeRoot, { recursive: true, force: true });
}
});
test("GET /api/v1/projects/[projectId]/orchestration-backend returns current choice and OMX availability", async () => {
await setup();
const tempDir = await mkdtemp(path.join(os.tmpdir(), "boss-orchestration-backend-omx-"));
const scriptPath = path.join(tempDir, "omx-team-smoke.mjs");
await writeFile(scriptPath, "console.log('ok');\n", "utf8");
const previousEnv = snapshotOmxEnv();
process.env.BOSS_OMX_ENABLED = "true";
process.env.BOSS_OMX_COMMAND = process.execPath;
process.env.BOSS_OMX_ARGS = scriptPath;
process.env.BOSS_OMX_WORKDIR = tempDir;
try {
const response = await orchestrationBackendRoute.GET(
new NextRequest("http://127.0.0.1:3000/api/v1/projects/audit-collab/orchestration-backend", {
method: "GET",
headers: await createAuthedHeaders(),
}),
{ params: Promise.resolve({ projectId: "audit-collab" }) },
);
assert.equal(response.status, 200);
const payload = (await response.json()) as {
ok: boolean;
projectId: string;
currentBackendId: string;
requestedBackendId: string | null;
availableChoices: Array<{ backendId: string; selectable: boolean; current: boolean }>;
omxAvailability: { selectable: boolean; reason: string };
};
assert.equal(payload.ok, true);
assert.equal(payload.projectId, "audit-collab");
assert.equal(payload.currentBackendId, "boss-native-orchestrator");
assert.equal(payload.requestedBackendId, null);
assert.deepEqual(
payload.availableChoices.map((choice) => choice.backendId),
["boss-native-orchestrator", "omx-team"],
);
assert.equal(payload.availableChoices[0]?.current, true);
assert.equal(payload.availableChoices[1]?.selectable, true);
assert.equal(payload.omxAvailability.selectable, true);
assert.equal(payload.omxAvailability.reason, "ready");
} finally {
restoreOmxEnv(previousEnv);
await rm(tempDir, { recursive: true, force: true });
}
});
test("PATCH /api/v1/projects/[projectId]/orchestration-backend persists OMX selection when selectable", async () => {
await setup();
const tempDir = await mkdtemp(path.join(os.tmpdir(), "boss-orchestration-backend-omx-"));
const scriptPath = path.join(tempDir, "omx-team-smoke.mjs");
await writeFile(scriptPath, "console.log('ok');\n", "utf8");
const previousEnv = snapshotOmxEnv();
process.env.BOSS_OMX_ENABLED = "true";
process.env.BOSS_OMX_COMMAND = process.execPath;
process.env.BOSS_OMX_ARGS = scriptPath;
process.env.BOSS_OMX_WORKDIR = tempDir;
try {
const response = await orchestrationBackendRoute.PATCH(
new NextRequest("http://127.0.0.1:3000/api/v1/projects/audit-collab/orchestration-backend", {
method: "PATCH",
headers: await createAuthedHeaders(),
body: JSON.stringify({ orchestrationBackendOverride: "omx-team" }),
}),
{ params: Promise.resolve({ projectId: "audit-collab" }) },
);
assert.equal(response.status, 200);
const payload = (await response.json()) as {
ok: boolean;
currentBackendId: string;
requestedBackendId: string;
omxAvailability: { selectable: boolean };
};
assert.equal(payload.ok, true);
assert.equal(payload.currentBackendId, "omx-team");
assert.equal(payload.requestedBackendId, "omx-team");
assert.equal(payload.omxAvailability.selectable, true);
const state = await readState();
const project = state.projects.find((item) => item.id === "audit-collab");
assert.equal(project?.orchestrationBackendOverride, "omx-team");
} finally {
restoreOmxEnv(previousEnv);
await rm(tempDir, { recursive: true, force: true });
}
});
test("PATCH /api/v1/projects/[projectId]/orchestration-backend falls back to native when OMX is unavailable", async () => {
await setup();
const previousEnv = snapshotOmxEnv();
delete process.env.BOSS_OMX_ENABLED;
delete process.env.BOSS_OMX_COMMAND;
delete process.env.BOSS_OMX_ARGS;
delete process.env.BOSS_OMX_WORKDIR;
try {
const response = await orchestrationBackendRoute.PATCH(
new NextRequest("http://127.0.0.1:3000/api/v1/projects/audit-collab/orchestration-backend", {
method: "PATCH",
headers: await createAuthedHeaders(),
body: JSON.stringify({ orchestrationBackendOverride: "omx-team" }),
}),
{ params: Promise.resolve({ projectId: "audit-collab" }) },
);
assert.equal(response.status, 200);
const payload = (await response.json()) as {
ok: boolean;
currentBackendId: string;
requestedBackendId: string;
omxAvailability: { selectable: boolean; reasonLabel: string };
};
assert.equal(payload.ok, true);
assert.equal(payload.requestedBackendId, "omx-team");
assert.equal(payload.currentBackendId, "boss-native-orchestrator");
assert.equal(payload.omxAvailability.selectable, false);
assert.equal(payload.omxAvailability.reasonLabel, "OMX Team Runtime 当前未启用。");
const state = await readState();
const project = state.projects.find((item) => item.id === "audit-collab");
assert.equal(project?.orchestrationBackendOverride, "omx-team");
} finally {
restoreOmxEnv(previousEnv);
}
});