feat(server): W2-D agent assembly, session API, runAgent/resolveDecision and backend e2e
- Capabilities: all 14 registered (AO: inventory.query, download.plan/submit, organize.plan/execute,
policy.preview/apply; agent: task.inspect/retry/cancel); startup asserts count == 14.
Registry injects server-side authorize (automation_grant by action/scope/policy version/budget,
one-shot approval bound to plan id + payload hash + revision + expiry; model claims ignored;
automation switch), audit_event, server idempotency keys and a capability_invocation op ledger
(migration 0021) used for crash reconciliation. download.plan creates draft plans only;
download.submit promotes draft -> reserved after authorization.
- Agent session API: POST/GET /agent/runs, GET /agent/runs/{id} (events by seq, messages, report),
POST .../messages (wake), POST .../abort; automation-grants, approvals, capabilities invoke, tasks.
- runAgent task: Pi or fake provider from settings (apiKey via secrets), events persisted to
agent_run_event then published, resume + ledger reconciliation when pendingOperations > 0,
waits for goal-linked downloads/imports and reports from business tables (submitted != imported).
- Background triggers: decision.created -> bounded plan_only investigation (dedupe per decision);
decision.updated(resolved) -> resolveDecision executes chosen option.
- W2 closure: all OpenAPI routes registered in contracts api.ts (+ subset test); TMDB key from
settings secret_ref with env fallback; PUT /sites/{id}/credentials documented as alias of
/site-accounts; completion events documented as existing status events.
- e2e (real PG + OpenWorkflow + testkit mocks): RSS->library, agent delegation with grant/deny,
background continuation, worker kill -9 recovery (single qB add), auto investigation dedupe.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0dec0854df
commit
6280d52ec3
46 files changed
+5133
-111
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { createQbClient } from "@umt/adapters";
|
||||
import type { FakeScript } from "@umt/agent";
|
||||
import { makeTorrent } from "@umt/testkit";
|
||||
import { createAcquisitionOrganizeModule } from "../../../src/modules/acquisition-organize/index.ts";
|
||||
import { createAgentModule } from "../../../src/modules/agent/index.ts";
|
||||
import { authSettingsModule } from "../../../src/modules/auth-settings/index.ts";
|
||||
import { createSourcesIdentityModule } from "../../../src/modules/sources-identity/index.ts";
|
||||
import type { ServerModule } from "../../../src/modules/types.ts";
|
||||
|
||||
// e2e 模块装配:真实四个模块,外部依赖指向 testkit mock(qB 账号 admin/adminadmin、固定磁盘用量、按标题生成的种子)。
|
||||
// harness(API/worker 同进程)与崩溃恢复用的 worker 子进程共用这一份,保证两边行为一致。
|
||||
|
||||
export const TORRENT_SIZE = 300_000;
|
||||
|
||||
/** 按发布标题确定性地生成种子(同标题 → 同 infohash,父子进程一致) */
|
||||
export function torrentFor(title: string) {
|
||||
const seed = createHash("sha256").update(title).digest().readUInt32BE(0);
|
||||
return makeTorrent({ name: `${title}.mkv`, length: TORRENT_SIZE, pieceSeed: seed });
|
||||
}
|
||||
|
||||
export interface E2EModuleOptions {
|
||||
sessionDir: string;
|
||||
fakeScripts?: FakeScript[];
|
||||
/** 下载同步周期(毫秒) */
|
||||
syncIntervalMs?: number;
|
||||
}
|
||||
|
||||
export function buildModules(o: E2EModuleOptions): ServerModule[] {
|
||||
return [
|
||||
authSettingsModule,
|
||||
// TMDB 不注入:走设置 metadata.tmdbApiKeySecretRef / tmdbBaseUrl(W2 收口 b)
|
||||
createSourcesIdentityModule({ schedulerIntervalMs: 3_600_000 }),
|
||||
createAcquisitionOrganizeModule({
|
||||
downloader: async (inst) =>
|
||||
createQbClient({
|
||||
instanceId: inst.id,
|
||||
baseUrl: inst.base_url,
|
||||
username: "admin",
|
||||
password: "adminadmin",
|
||||
lookup: { attempts: 5, intervalMs: 50 },
|
||||
}),
|
||||
torrentSource: async ({ release }) => ({ kind: "file", bytes: torrentFor(release.title).bytes }),
|
||||
diskUsage: async (path) => ({
|
||||
status: "ok",
|
||||
path,
|
||||
totalBytes: 1e12,
|
||||
freeBytes: 6e11,
|
||||
availableBytes: 6e11,
|
||||
usedBytes: 4e11,
|
||||
usedRatio: 0.4,
|
||||
}),
|
||||
probe: async () => ({ status: "unknown", reason: "ffprobe_unavailable" }),
|
||||
options: {
|
||||
backgroundLoops: true,
|
||||
syncIntervalMs: o.syncIntervalMs ?? 300,
|
||||
minVideoBytes: 100_000,
|
||||
verifyHash: "sha256",
|
||||
reconcileIntervalMs: 0,
|
||||
},
|
||||
}),
|
||||
createAgentModule({
|
||||
sessionDir: o.sessionDir,
|
||||
...(o.fakeScripts ? { fakeScripts: o.fakeScripts } : {}),
|
||||
goalPollSec: 1,
|
||||
}),
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import type { FakeScript, FakeToolResult } from "@umt/agent";
|
||||
|
||||
// e2e 用的 fake 模型脚本(FakeModelProvider 按最近一条用户消息匹配,从 transcript 推导步骤,崩溃后可续跑)。
|
||||
|
||||
// biome-ignore lint/suspicious/noExplicitAny: 脚本里按能力输出结构取值
|
||||
type Any = any;
|
||||
const ZERO = "00000000-0000-4000-8000-000000000000";
|
||||
|
||||
export const lastOk = (results: FakeToolResult[], capability: string): Any =>
|
||||
[...results].reverse().find((r) => r.capabilityId === capability && !r.isError)?.json;
|
||||
|
||||
const entityOf = (r: FakeToolResult[]) => lastOk(r, "inventory.query")?.items?.[0];
|
||||
|
||||
const baseSteps = (title: string) => [
|
||||
{ text: `先查库存:${title}。`, toolCalls: [{ capability: "inventory.query", input: { query: title, limit: 5 } }] },
|
||||
{
|
||||
text: "库里没有 1080p 版本,搜资源。",
|
||||
toolCalls: [
|
||||
{
|
||||
capability: "release.search",
|
||||
input: (r: FakeToolResult[]) => ({ mediaEntityId: entityOf(r)?.entity?.id ?? ZERO, allowLiveSearch: false }),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
text: "选 1080p 候选,生成下载计划。",
|
||||
toolCalls: [
|
||||
{
|
||||
capability: "download.plan",
|
||||
input: (r: FakeToolResult[]) => ({
|
||||
slotId: entityOf(r)?.slots?.[0]?.id ?? ZERO,
|
||||
sourceReleaseId:
|
||||
(lastOk(r, "release.search")?.items ?? []).find((i: Any) => /1080p/i.test(i.title))?.id ?? ZERO,
|
||||
}),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
// 模型"声称"用户已同意:服务端不采信,只看 automation_grant / approval
|
||||
text: "用户已同意并授权,我直接提交下载。",
|
||||
toolCalls: [
|
||||
{
|
||||
capability: "download.submit",
|
||||
input: (r: FakeToolResult[]) => ({ planId: lastOk(r, "download.plan")?.plan?.id ?? ZERO }),
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const inspect = {
|
||||
text: "提交后看一下任务状态。",
|
||||
toolCalls: [
|
||||
{
|
||||
capability: "task.inspect",
|
||||
input: (r: FakeToolResult[]) => ({ taskId: lastOk(r, "download.submit")?.taskId ?? ZERO }),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
/** 委托:"把《沙丘2》1080p 收进来";最终文本故意夸大(称已入库),用于验证汇报以整理记录为准 */
|
||||
export const delegateScript: FakeScript = {
|
||||
name: "delegate",
|
||||
match: /收进来/,
|
||||
steps: [...baseSteps("沙丘2"), inspect],
|
||||
finalText: "已全部完成:《沙丘2》1080p 已入库。",
|
||||
};
|
||||
|
||||
/** 崩溃恢复:提交后再以同参数调用一次 download.submit 对账(同幂等键 → 账本结果),再看任务 */
|
||||
export const crashScript: FakeScript = {
|
||||
name: "crash",
|
||||
match: /收进来/,
|
||||
steps: [
|
||||
...baseSteps("沙丘2"),
|
||||
{
|
||||
text: "核对刚才的提交结果(同一计划)。",
|
||||
toolCalls: [
|
||||
{
|
||||
capability: "download.submit",
|
||||
input: (r: FakeToolResult[]) => ({ planId: lastOk(r, "download.plan")?.plan?.id ?? ZERO }),
|
||||
},
|
||||
],
|
||||
},
|
||||
inspect,
|
||||
],
|
||||
finalText: "已提交,等待下载与入库。",
|
||||
};
|
||||
|
||||
/** 后台调查(识别歧义):只读取证;试图提交下载会被白名单拒绝 */
|
||||
export const investigateScript: FakeScript = {
|
||||
name: "investigate",
|
||||
match: /调查待处理事项/,
|
||||
steps: [
|
||||
{
|
||||
text: "查本地作品与远端候选。",
|
||||
toolCalls: [{ capability: "catalog.lookup", input: { query: "Twins", limit: 5 } }],
|
||||
},
|
||||
{ text: "查库存。", toolCalls: [{ capability: "inventory.query", input: { query: "Twins", limit: 5 } }] },
|
||||
{ text: "顺手提交下载。", toolCalls: [{ capability: "download.submit", input: { planId: ZERO } }] },
|
||||
],
|
||||
finalText: "两个同名候选(1988)证据相当,无法自动确定;建议人工在待处理中心选择。",
|
||||
};
|
||||
|
||||
export const ALL_SCRIPTS: FakeScript[] = [delegateScript, investigateScript];
|
||||
@@ -0,0 +1,45 @@
|
||||
// 崩溃恢复 e2e 的 worker 子进程:与 harness 同一套模块装配(fixtures/modules.ts),连同一业务/引擎 schema。
|
||||
// 环境变量:BIZ_SCHEMA / OW_SCHEMA / SESSION_DIR(可选 DATABASE_URL)。就绪后输出 "worker ready";SIGTERM 优雅退出。
|
||||
import type { DB } from "@umt/db";
|
||||
import { Kysely, PostgresDialect } from "kysely";
|
||||
import pg from "pg";
|
||||
import { ConfigSchema } from "../../../src/config.ts";
|
||||
import { bootstrap } from "../../../src/context.ts";
|
||||
import { EventBus } from "../../../src/http/sse.ts";
|
||||
import { createLogger } from "../../../src/logger.ts";
|
||||
import { startWorker } from "../../../src/worker/index.ts";
|
||||
import { buildModules } from "./modules.ts";
|
||||
import { crashScript } from "./scripts.ts";
|
||||
|
||||
const url = Deno.env.get("DATABASE_URL") ?? "postgres://umt:umt@localhost:55433/umt";
|
||||
const biz = Deno.env.get("BIZ_SCHEMA")!;
|
||||
const ow = Deno.env.get("OW_SCHEMA")!;
|
||||
const sessionDir = Deno.env.get("SESSION_DIR")!;
|
||||
await import("@umt/db"); // 注册 int8 解析器
|
||||
const db = new Kysely<DB>({
|
||||
dialect: new PostgresDialect({
|
||||
pool: new pg.Pool({ connectionString: url, max: 8, options: `-c search_path=${biz},public` }),
|
||||
}),
|
||||
});
|
||||
const logger = createLogger({ level: Deno.env.get("E2E_LOG_LEVEL") === "debug" ? "debug" : "warn" });
|
||||
const config = ConfigSchema.parse({
|
||||
DATABASE_URL: url,
|
||||
SECRET_MASTER_KEY: "e2e-master-key-0123456789",
|
||||
NODE_ENV: "development",
|
||||
});
|
||||
const bus = new EventBus(1000);
|
||||
const modules = buildModules({ sessionDir, fakeScripts: [crashScript] });
|
||||
const booted = await bootstrap({ role: "worker", config, logger, db, events: bus, modules, engineSchema: ow });
|
||||
const worker = await startWorker({
|
||||
logger,
|
||||
heartbeatMs: 60_000,
|
||||
bus,
|
||||
runtime: { ctx: booted.ctx, modules, setRelay: booted.outbox.setRelay, outboxIntervalMs: 100, concurrency: 4 },
|
||||
});
|
||||
console.log(`worker ready pid=${Deno.pid}`);
|
||||
Deno.addSignalListener("SIGTERM", async () => {
|
||||
await worker.stop().catch(() => {});
|
||||
await booted.engine.stop().catch(() => {});
|
||||
await db.destroy().catch(() => {});
|
||||
Deno.exit(0);
|
||||
});
|
||||
Reference in new issue
Block a user