- 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]>
148 lines
5.7 KiB
TypeScript
148 lines
5.7 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",
|
|
{ sourceReleaseId: rel.id },
|
|
{ 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);
|
|
});
|