- MCP create_task агента: origin agent + «Предложено» (раньше — задача человека, диспетчер брал её сам) - AgentLimits.perDay (5, скользящие 24 ч), proposalRoom/proposalsToday; сверх — отказ агенту, находки приёмки — в недельную сводку; событие proposal_limited - CLI agent-limits perDay=N, правило агентам в template/RULES.md, тест proposal-daily-limit Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
7.1 KiB
TypeScript
126 lines
7.1 KiB
TypeScript
// PEP-270: a story an agent files through its MCP `create_task` is a proposal (origin agent,
|
||
// «Предложено»), and a project gets not more than `perDay` proposals of agents in 24 hours.
|
||
import assert from "node:assert/strict";
|
||
import { after, describe, test } from "node:test";
|
||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
||
import { events, tasks } from "@peppa/db";
|
||
import { and, eq } from "drizzle-orm";
|
||
import { core, db, project, runner, teardown } from "./helpers.ts";
|
||
|
||
// No model for the repeat check: a failing call means «not a repeat».
|
||
process.env.PEPPA_INTAKE_BIN = "/nonexistent/mock-intake";
|
||
const { createAgentServer } = await import("@peppa/mcp/agent");
|
||
|
||
after(teardown);
|
||
|
||
async function agent(projectKey: string) {
|
||
const [a, b] = InMemoryTransport.createLinkedPair();
|
||
await createAgentServer(db, { defaultKey: projectKey }).connect(a);
|
||
const client = new Client({ name: "mock-agent", version: "0" });
|
||
await client.connect(b);
|
||
return async (name: string, args: Record<string, unknown> = {}) => {
|
||
const r = await client.callTool({ name, arguments: args });
|
||
return { text: (r.content as Array<{ text: string }>)[0]!.text, isError: !!r.isError };
|
||
};
|
||
}
|
||
|
||
const byTitle = async (projectId: number, title: string) => (await db.select().from(tasks).where(and(eq(tasks.projectId, projectId), eq(tasks.title, title))))[0];
|
||
const limited = async (projectId: number) => db.select().from(events).where(and(eq(events.projectId, projectId), eq(events.type, "proposal_limited")));
|
||
|
||
describe("PEP-270: предложения агентов", () => {
|
||
test("Агент завёл задачу на будущее — она в «Предложено», диспетчер её не берёт; «Делать» отдаёт её в бэклог", async () => {
|
||
const tp = await project({ turns: [] });
|
||
const p = await core.getProject(db, tp.key);
|
||
await core.setAutonomy(db, p.id, { pick: "auto" });
|
||
const call = await agent(tp.key);
|
||
const r = await call("create_task", { title: "Добавить экспорт в CSV" });
|
||
assert.equal(r.isError, false, r.text);
|
||
assert.match(r.text, /Предложено/);
|
||
const t = (await byTitle(p.id, "Добавить экспорт в CSV"))!;
|
||
assert.equal(t.origin, "agent");
|
||
assert.equal(t.proposed, true);
|
||
assert.equal(t.status, "backlog");
|
||
assert.equal(core.isAgentOrigin(t.origin), true);
|
||
// The board's «Предложено» and the dispatcher's backlog: the proposal is only in the first.
|
||
assert.ok((await core.proposedTasks(db, p.id)).some((x) => x.id === t.id));
|
||
const ready = await runner.readyBacklog(db, p.id);
|
||
assert.ok(!ready.some((x) => x.id === t.id));
|
||
|
||
await core.approveProposal(db, t.id);
|
||
const after = (await byTitle(p.id, "Добавить экспорт в CSV"))!;
|
||
assert.equal(after.proposed, false);
|
||
const ready2 = await runner.readyBacklog(db, p.id);
|
||
assert.ok(ready2.some((x) => x.id === t.id));
|
||
});
|
||
|
||
test("Шестая задача агента за сутки не создаётся, агент получает объяснение; ошибка продукта предел не тратит", async () => {
|
||
const tp = await project({ turns: [] });
|
||
const p = await core.getProject(db, tp.key);
|
||
const call = await agent(tp.key);
|
||
for (let i = 1; i <= 5; i++) assert.equal((await call("create_task", { title: `Идея номер ${i}` })).isError, false);
|
||
assert.deepEqual(await core.proposalRoom(db, p.id), { room: 0, limit: 5, used: 5 });
|
||
|
||
const sixth = await call("create_task", { title: "Идея номер 6" });
|
||
assert.equal(sixth.isError, true);
|
||
assert.match(sixth.text, /last 24 hours \(limit 5\)/);
|
||
assert.equal(await byTitle(p.id, "Идея номер 6"), undefined);
|
||
assert.equal((await limited(p.id)).length, 1);
|
||
|
||
// An error of the product's users and a part of a person's story don't spend the limit.
|
||
await core.createTask(db, { projectId: p.id, title: "Ошибка: TypeError", origin: "incident", severity: "breakage" });
|
||
await core.createTask(db, { projectId: p.id, title: "Часть задачи человека", origin: "analyst" });
|
||
await core.createTask(db, { projectId: p.id, title: "Задача человека" });
|
||
assert.equal(await core.proposalsToday(db, p.id), 5);
|
||
|
||
// The window slides: in 24 hours the room is back.
|
||
assert.equal((await core.proposalRoom(db, p.id, new Date(Date.now() + 86_400_000 + 1000))).room, 5);
|
||
});
|
||
|
||
test("Отклонённое владельцем название агент снова не заводит", async () => {
|
||
const tp = await project({ turns: [] });
|
||
const p = await core.getProject(db, tp.key);
|
||
const call = await agent(tp.key);
|
||
await call("create_task", { title: "Тёмная тема" });
|
||
await core.rejectProposal(db, (await byTitle(p.id, "Тёмная тема"))!.id);
|
||
const again = await call("create_task", { title: "Тёмная тема" });
|
||
assert.equal(again.isError, true);
|
||
assert.match(again.text, /already declined/);
|
||
});
|
||
|
||
test("Свой предел у проекта: 2 в сутки — третья не создаётся", async () => {
|
||
const tp = await project({ turns: [] });
|
||
const p = await core.getProject(db, tp.key);
|
||
await core.setAgentLimits(db, p.id, { perDay: 2 });
|
||
const call = await agent(tp.key);
|
||
assert.equal((await call("create_task", { title: "Первая" })).isError, false);
|
||
assert.equal((await call("create_task", { title: "Вторая" })).isError, false);
|
||
assert.equal((await call("create_task", { title: "Третья" })).isError, true);
|
||
await assert.rejects(core.setAgentLimits(db, p.id, { perDay: 51 }));
|
||
});
|
||
|
||
test("Приёмка нашла больше, чем осталось: заводится остаток, остальное ждёт недельной сводки", async () => {
|
||
const tp = await project({ turns: [] });
|
||
const p = await core.getProject(db, tp.key);
|
||
const call = await agent(tp.key);
|
||
for (let i = 1; i <= 4; i++) await call("create_task", { title: `Заранее ${i}` });
|
||
const filed = await runner.fileStories(
|
||
db,
|
||
p,
|
||
[
|
||
{ title: "Кнопка без подписи", description: "На главной кнопка без текста.", severity: "important" },
|
||
{ title: "Мелкий отступ", description: "Отступ 3 px.", severity: "minor" },
|
||
{ title: "Форма не отправляется", description: "Отправка падает.", severity: "breakage" },
|
||
],
|
||
{ envLabel: "staging", url: "", ignoreFinish: true },
|
||
);
|
||
assert.equal(filed.created.length, 1);
|
||
assert.equal(filed.created[0]!.severity, "breakage", "the most important one goes first");
|
||
assert.equal(filed.kept, 2);
|
||
const digest = await core.getDigest(db, p.id);
|
||
assert.deepEqual(digest.items.map((i) => i.title).sort(), ["Кнопка без подписи", "Мелкий отступ"]);
|
||
assert.equal((await limited(p.id)).length, 2);
|
||
assert.equal(await core.proposalsToday(db, p.id), 5);
|
||
});
|
||
});
|