Files
UHD_Media_Tool/apps/server/test/e2e/e5-auto-investigate.test.ts
T
rootandClaude Opus 5.5 94fd48bff2 feat(identity): lazy TMDB lookup and bounded identity pool (DEC-1)
RSS/list releases that match no subscription or local work no longer hit
TMDB or open a pending item; they go to the identity pool as `unmatched`.
TMDB is queried only on a local alias hit, a published new-title rule whose
prefilter passes, manual/Agent retry, search results, or when the new
setting identity.eagerTmdbLookup (default off) is on. Mirrors MoviePilot:
torrents are recognised only when matched to a subscription or searched,
and recognition failures are logged, not alerted.

- Pending items only for related releases (ambiguous after lookup, or
  related to a local work but unresolved); same source + normalized title
  merge into one decision ("来源「X」的 N 条资源待识别"), resolve/dismiss
  applies to the whole group, members detach as they get resolved.
- Pool governance: unmatched reviews capped (daily, 3x), title_alias
  insert trigger wakes similar entries, stale unmatched entries archived
  after identity.poolArchiveDays (30); review idempotency keys include the
  due time.
- Migration 0047_identity_pool: reason/outcome `unmatched`,
  identity_candidate.normalized_title (+trgm index), wake trigger, and
  cleanup of open identity_unresolved items with no related work
  (dismissed, resolution=moved_to_pool).
- Inbox: group by type + work/source with collapse, hide info-only items
  by default (minSeverity=warning), badge counts warning+ only; pool sheet
  gets manual TMDB/local-work assign and dismiss; settings card for the
  new identity settings.
- Runbook docs/runbook/identity.md.

Requires rebuild/restart (migration runs on start).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-29 15:05:39 +08:00

149 lines
5.8 KiB
TypeScript

import { TMDB_FIXTURES, type TmdbFixture } from "@umt/testkit";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { INVESTIGATE_CAPABILITIES } from "../../src/modules/agent/runner.ts";
import { investigateScript } from "./fixtures/scripts.ts";
import { createE2E, type E2E, pgAvailable, runEvents, seedBase, waitFor } from "./harness.ts";
// E5 后台唤醒:识别歧义产生 decision.created → outbox → resolveDecision → autoInvestigate 开启时入队有界 runAgent
// (plan_only、白名单只读 + identity.resolve、小预算、trigger=event);同一 decision 只一个 run(dedupe_key)。
const hasPg = await pgAvailable();
const twin = (id: number): TmdbFixture => ({
type: "movie",
id,
original_title: "Twins",
original_language: "en",
origin_country: ["US"],
date: "1988-12-09",
titles: { "zh-CN": "龙兄鼠弟", "en-US": "Twins" },
popularity: 10 + id / 1000,
alternative_titles: [],
external_ids: {},
});
describe.skipIf(!hasPg)("E5 后台唤醒有界调查", () => {
let e: E2E;
let siteId: string;
beforeAll(async () => {
e = await createE2E("e5", {
fakeScripts: [investigateScript],
tmdbFixtures: [...TMDB_FIXTURES, twin(111), twin(112)],
});
siteId = (await seedBase(e)).siteId;
await e.settings({ agent: { autoInvestigate: true, investigateMaxToolCalls: 6 } });
}, 120_000);
afterAll(async () => {
await e?.close();
}, 60_000);
it("识别歧义 → 自动发起只读调查 → 结论回写待处理事项;重复事件只一个 run", async () => {
const rel = await e.db
.insertInto("source_release")
.values({ site_id: siteId, site_release_id: "990001", title: "Twins.1988.1080p.BluRay.x264-GRP" })
.returning("id")
.executeTakeFirstOrThrow();
await e.booted.ctx.engine.enqueue(
"resolveObservation",
// 惰性识别(DEC-1):不相关的资源不查 TMDB;按搜索结果触发才会查到两个同名候选
{ sourceReleaseId: rel.id, trigger: "search" },
{ idempotencyKey: `e5:resolve:${rel.id}`, traceId: rel.id },
);
const decision = await waitFor(
async () =>
e.db
.selectFrom("decision")
.selectAll()
.where("kind", "=", "identity_ambiguous")
.where("subject_id", "=", rel.id)
.executeTakeFirst(),
60_000,
"识别歧义 decision",
);
const run = await waitFor(
async () =>
e.db
.selectFrom("agent_run")
.selectAll()
.where("dedupe_key", "=", `investigate:${decision.id}`)
.executeTakeFirst(),
60_000,
"自动调查 run",
);
expect(run).toMatchObject({ trigger: "event", mode: "plan_only", created_by: "system:auto-investigate" });
expect(run.goal).toContain(decision.id);
expect(run.scope).toMatchObject({
capabilities: [...INVESTIGATE_CAPABILITIES],
allowedKinds: ["read", "preview", "propose"],
});
expect(run.budget).toMatchObject({ maxToolCalls: 6 });
const finished = await waitFor(
async () => {
const r = await e.db.selectFrom("agent_run").selectAll().where("id", "=", run.id).executeTakeFirstOrThrow();
return ["succeeded", "failed", "budget_exceeded"].includes(r.status) ? r : null;
},
60_000,
"调查 run 结束",
);
expect(finished.status).toBe("succeeded");
const events = await runEvents(e, run.id);
const invoked = events
.filter((x) => x.type === "tool_call_start")
.map((x) => (x.payload as { capabilityId: string }).capabilityId);
expect(invoked).toEqual(["catalog.lookup", "inventory.query"]);
// 白名单外的写能力:被拒绝,没有执行
const rejected = events.filter((x) => x.type === "tool_call_rejected");
expect(rejected.map((x) => (x.payload as { toolName: string }).toolName)).toEqual(["download__submit"]);
expect(
await e.db.selectFrom("capability_invocation").select("id").where("agent_run_id", "=", run.id).execute(),
).toHaveLength(0);
// 结论回写:待处理事项交还人工并带调查摘要
const d2 = await e.db.selectFrom("decision").selectAll().where("id", "=", decision.id).executeTakeFirstOrThrow();
expect(d2.status).toBe("awaiting_user");
expect(d2.agent_run_id).toBe(run.id);
expect((d2.evidence as { agentInvestigation?: { runId: string } }).agentInvestigation?.runId).toBe(run.id);
// 同一根因的重复事件(不同 outbox 键)与事项重新打开后的再次触发:仍只有一个 run
await e.db.updateTable("decision").set({ status: "open" }).where("id", "=", decision.id).execute();
for (const k of ["dup-1", "dup-2"]) {
await e.booted.ctx.outbox.emit(
e.db,
"decision.created",
{ decisionId: decision.id, kind: "identity_ambiguous", severity: "warning", title: decision.title },
{ idempotencyKey: `e5:${k}` },
);
}
await waitFor(
async () => {
const rows = await e.db
.selectFrom("outbox_event")
.select("status")
.where("idempotency_key", "in", ["e5:dup-1", "e5:dup-2"])
.execute();
return rows.length === 2 && rows.every((r) => r.status === "dispatched");
},
30_000,
"重复事件已投递",
);
// 等 resolveDecision 跑完
await waitFor(
async () => {
const runs = await e.booted.ctx.engine.list({ taskName: "resolveDecision" });
return runs.length >= 3 && runs.every((r) => r.status === "completed") ? runs : null;
},
30_000,
"resolveDecision 完成",
);
const runs = await e.db
.selectFrom("agent_run")
.select("id")
.where("dedupe_key", "=", `investigate:${decision.id}`)
.execute();
expect(runs).toHaveLength(1);
expect(await e.db.selectFrom("agent_run").select("id").execute()).toHaveLength(1);
}, 180_000);
});