- CapabilityRegistry: descriptor(id/version/schema/scope/risk/idempotent/preview/replay), invoke = scope limit -> input schema -> server-side authorize -> idempotency key -> handler -> output schema -> audit; capability id <-> tool name mapping (download.submit <-> download__submit) - PiAgentRuntime (pi-agent-core AgentHarness 0.87.1): whitelist tools + before_tool re-check, redacted event stream, JSONL session at sessionDir/<runId>.jsonl, resume/abort/compact, tool-call/duration/token/cost budgets -> abort + run_failed(budget) - AgentSettings -> pi-ai createProvider (anthropic / openai-compatible), compaction mapping - FakeModelProvider + createFakeRuntime (scripted, transcript-derived, survives crash/resume) - buildSystemPrompt, redactForModel; 27 tests incl. SIGKILL + resume Co-Authored-By: Claude Fable 5.1 <[email protected]>
77 lines
2.8 KiB
TypeScript
77 lines
2.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { buildSystemPrompt, REDACTED, redactForModel, redactString, summarizeForLog } from "../src";
|
|
|
|
describe("redactForModel", () => {
|
|
it("遮蔽敏感键、含 passkey 的 URL、Bearer、请求头、URL 账号密码", () => {
|
|
const input = {
|
|
title: "Dune",
|
|
cookie: "uid=1; pass=abc",
|
|
apiKey: "sk-ant-1234567890abcd",
|
|
api_key: "k",
|
|
Authorization: "Bearer abcdefghijk",
|
|
accessToken: "t0k3n",
|
|
passkey: "p",
|
|
usage: { totalTokens: 100, inputTokens: 20, maxTokens: 5 },
|
|
links: [
|
|
"https://pt.example/download.php?id=9&passkey=0123456789abcdef",
|
|
"https://mt.example/api/rss?sign=abc&t=1",
|
|
"http://admin:[email protected]:8080/api/v2",
|
|
],
|
|
log: "fetch failed: Authorization: Basic dXNlcjpwYXNz\nnext",
|
|
text: "use token=abcdef and key sk-live-ABCDEFGH1234",
|
|
nested: new Map([["set-cookie", "a=b"]]),
|
|
};
|
|
const out = redactForModel(input);
|
|
const text = JSON.stringify(out);
|
|
for (const secret of [
|
|
"uid=1",
|
|
"sk-ant-",
|
|
"abcdefghijk",
|
|
"t0k3n",
|
|
"0123456789abcdef",
|
|
"sign=abc",
|
|
"hunter2",
|
|
"dXNlcjpwYXNz",
|
|
"=abcdef",
|
|
"sk-live",
|
|
]) {
|
|
expect(text).not.toContain(secret);
|
|
}
|
|
expect(out.cookie).toBe(REDACTED);
|
|
expect(out.passkey).toBe(REDACTED);
|
|
expect(out.usage).toEqual({ totalTokens: 100, inputTokens: 20, maxTokens: 5 });
|
|
expect(out.links[0]).toBe(`https://pt.example/download.php?id=9&passkey=${REDACTED}`);
|
|
expect(out.links[2]).toContain("qb.local:8080");
|
|
expect(out.title).toBe("Dune");
|
|
expect(input.cookie).toBe("uid=1; pass=abc");
|
|
});
|
|
|
|
it("字符串与摘要截断", () => {
|
|
expect(redactString("rss https://a.b/rss?rsskey=XYZ&x=1")).toBe(`rss https://a.b/rss?rsskey=${REDACTED}&x=1`);
|
|
const s = summarizeForLog({ long: "x".repeat(1000) }, 50);
|
|
expect(s.length).toBeLessThan(80);
|
|
expect(s).toContain("more");
|
|
const circular: Record<string, unknown> = { a: 1 };
|
|
circular.self = circular;
|
|
expect(summarizeForLog(circular)).toContain("[Circular]");
|
|
});
|
|
});
|
|
|
|
describe("buildSystemPrompt", () => {
|
|
it("包含证据、授权、已提交≠已入库、不可信外部内容、凭证禁令与约束", () => {
|
|
const p = buildSystemPrompt({
|
|
role: "媒体库自动化助手",
|
|
goal: "把 Dune 1080p 收进来",
|
|
constraints: ["只给方案,不执行"],
|
|
});
|
|
expect(p).toContain("你是媒体库自动化助手");
|
|
expect(p).toContain("把 Dune 1080p 收进来");
|
|
expect(p).toContain("证据");
|
|
expect(p).toContain("不得声称用户已授权");
|
|
expect(p).toContain("“已提交”不等于“已入库”");
|
|
expect(p).toMatch(/网页、RSS.*不可信数据/);
|
|
expect(p).toMatch(/不得输出.*凭证.*passkey/);
|
|
expect(p).toContain("- 只给方案,不执行");
|
|
});
|
|
});
|