refactor(core): migrate to Bun and converge acquisition policies and Agent boundaries

This commit is contained in:
root committed 2026-10-13 14:37:03 +08:00
1 parent 3f1c6d02f6
commit 27c0f27434
268 files changed
+7468 -7852

No files matched your search

+12 -35
View File
@@ -1,8 +1,8 @@
import { createRoute, z } from "@hono/zod-openapi";
import { apiResponse, CAPABILITY_IDS, capabilitySpec, FEATURES, LIFECYCLE_EVENTS } from "@umt/contracts";
import { defineTask, TASK_NAMES, TaskRegistry } from "@umt/tasks";
import { apiResponse, CAPABILITY_IDS, capabilitySpec, FEATURES, LIFECYCLE_EVENTS, TaskKind } from "@umt/contracts";
import { defineTask, TaskRegistry, UnknownTaskError } from "@umt/tasks";
import { describe, expect, it } from "vitest";
import { buildHookRegistry, buildTaskRegistry, collectOutboxRoutes, DeferredTaskEngine } from "../src/context.ts";
import { buildTaskRegistry, collectOutboxRoutes, DeferredTaskEngine } from "../src/context.ts";
import { createApp } from "../src/http/app.ts";
import { requireScope } from "../src/http/auth.ts";
import { EventBus } from "../src/http/sse.ts";
@@ -12,6 +12,8 @@ import { DEV_KEY, json, memoryLogger } from "./helpers.ts";
// register* 阶段模块只保存 ctx 引用,这里不需要真实依赖
const fakeCtx = { role: "all" } as unknown as ServerContext;
// agentRun 是业务记录类型,对应的执行任务叫 runAgent。
const taskNames = TaskKind.options.map((kind) => (kind === "agentRun" ? "runAgent" : kind));
const pingRoute = createRoute({
method: "get",
@@ -85,22 +87,25 @@ describe("ServerModule 骨架", () => {
expect((await app.request("/api/v1/nope", { headers: { authorization: `Bearer ${DEV_KEY}` } })).status).toBe(404);
});
it("任务注册表:模块任务优先,其余 §3.1-8 任务由空壳兜底;重复注册报错", async () => {
it("任务注册表只收录模块实现;未注册任务和重复注册明确报错", async () => {
const reg = buildTaskRegistry([...SERVER_MODULES, pingModule], fakeCtx);
expect(reg.names()).toEqual(expect.arrayContaining([...TASK_NAMES, "testPing"]));
expect(reg.names()).toEqual(expect.arrayContaining([...taskNames, "testPing"]));
const ingest = reg.latest("testPing");
expect(await ingest.handler({ input: {} } as never)).toBe("real");
const partial = buildTaskRegistry([pingModule], fakeCtx);
expect(partial.names()).toEqual(["testPing"]);
expect(() => partial.latest("submitDownload")).toThrow(UnknownTaskError);
expect(() => buildTaskRegistry([pingModule, { ...pingModule, name: "dup" }], fakeCtx)).toThrow(/重复注册/);
});
it("生产装配:每个核心任务都由业务模块正式注册,不落到空壳兜底", () => {
it("生产装配:契约中的每个业务任务都有正式处理器", () => {
const owned = new Set<string>();
for (const m of SERVER_MODULES) {
const own = new TaskRegistry();
m.registerTasks(own, fakeCtx);
for (const name of own.names()) owned.add(name);
}
expect(TASK_NAMES.filter((n) => !owned.has(n))).toEqual([]);
expect(taskNames.filter((n) => !owned.has(n))).toEqual([]);
});
it("outbox 路由合并;同一非广播事件被两个模块认领报错", () => {
@@ -171,34 +176,6 @@ describe("ServerModule 骨架", () => {
expect(Object.keys(subtitles.outboxRoutes?.(fakeCtx) ?? {})).toEqual(subtitles.manifest?.subscribes);
});
it("链式钩子按模块顺序注册;生产装配目前没有模块注册钩子(无注册零开销)", async () => {
const order: string[] = [];
const mod = (name: string): ServerModule => ({
name,
registerRoutes() {},
registerTasks() {},
registerHooks(hooks) {
hooks.rewrite("organize.rename", (n) => {
order.push(name);
return `${n}.${name}`;
});
},
});
const hooks = buildHookRegistry([mod("first"), mod("second")], fakeCtx);
expect(
await hooks.runRewrite("organize.rename", "a", {
targetPath: "/a",
sourcePath: "/b",
mediaEntityId: "e",
mediaType: "movie",
season: null,
episodes: null,
}),
).toBe("a.first.second");
expect(order).toEqual(["first", "second"]);
expect(buildHookRegistry(SERVER_MODULES, fakeCtx).list()).toEqual({});
});
it("引擎就绪前调用给出明确错误", async () => {
const e = new DeferredTaskEngine();
expect(e.ready).toBe(false);