Files
Jyotisha/frontend/tests/skill-binding.test.ts
T
ClaudeandClaude Opus 5.5 ab400951c2
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m47s
Independent Staging Quality Gate / validate-python (push) Successful in 7m23s
Independent Staging Quality Gate / publish (push) Successful in 3m31s
fix: what users say happened stays background in chat; only 生时纠正 uses it (BUG-1977)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXp2U3BH1SxwAGyhhbe1
2026-11-18 12:59:09 +08:00

388 lines
22 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from "node:assert/strict";
import { existsSync, readFileSync, realpathSync } from "node:fs";
import { join, resolve } from "node:path";
import test from "node:test";
import { Agent } from "@mastra/core/agent";
import { consultationMethodologyForDomains } from "../src/lib/consultation-methodology.ts";
import { productContractBlock } from "../src/lib/product-runtime-contract.ts";
import { SKILL_FAMILY_READ_ALLOWLIST } from "../src/lib/skill-package-registry.ts";
import {
BOUND_METHOD_MARKER,
jyotishSkillBinding,
jyotishSkillBoundProcessor,
jyotishSkillMethodBlock,
jyotishSkillMethodCoreBlock,
jyotishSkillPackage,
jyotishSkillRuntimePath,
} from "../src/mastra/skill-binding.ts";
const model = { specificationVersion: "v2", provider: "probe", modelId: "probe" } as never;
function probeAgent(options: Record<string, unknown>) {
return new Agent({ id: "skill-binding-probe", name: "Probe", model, instructions: "x", ...options } as never);
}
function liveSkillBody() {
const live = readFileSync(resolve(jyotishSkillPackage.resolvedPath, "SKILL.md"), "utf8");
return live.slice(live.indexOf("---", 3) + 4).trim();
}
// 原值:整份 references/product_runtime_contract.md(trim 后)
// 新值:只取文件里的 runtime-contract 块(去掉维护注释行)
// 原因:BUG-1728 产品规则全部搬进这份文件,其余块(边界、本命指令、MEVG 策略、通用读法)各在自己的位置按名读取
function productRuntimeContractText() {
return productContractBlock("runtime-contract");
}
function boundMethodText() {
const opened = jyotishSkillMethodBlock.indexOf(`<jyotish-skill name="${jyotishSkillPackage.name}"`);
const closed = jyotishSkillMethodBlock.lastIndexOf("</jyotish-skill>");
assert.ok(opened >= 0 && closed > opened);
return jyotishSkillMethodBlock.slice(jyotishSkillMethodBlock.indexOf("\n", opened) + 1, closed).trim();
}
test("the method the model follows is the live skill, not a hashed snapshot", () => {
const body = liveSkillBody();
const bound = boundMethodText();
assert.match(jyotishSkillMethodBlock, new RegExp(`name="${jyotishSkillPackage.name}"`));
assert.doesNotMatch(jyotishSkillMethodBlock, /<jyotish-skill name="[^"]+" version="/);
// 原值: 绑定的每一行都能在 SKILL.md 正文里找到(摘录) / 新值: 绑定内容恰为 SKILL.md 正文全文 + 产品运行合同全文 / 原因: BUG-1696/BUG-1697 产品 2026-11-07:聊天完整运行占星师 skill(上游 SKILL.md 原文 + 路由全文 + 判断手册),不再摘录
assert.equal(bound, `${body}\n\n${productRuntimeContractText()}`);
assert.ok(jyotishSkillRuntimePath.endsWith(jyotishSkillPackage.name));
assert.equal(jyotishSkillRuntimePath.includes("/versions/"), false);
assert.equal(existsSync(join(jyotishSkillRuntimePath, "versions")), false);
assert.equal(
realpathSync(join(jyotishSkillRuntimePath, "SKILL.md")),
realpathSync(join(jyotishSkillPackage.resolvedPath, "SKILL.md")),
);
});
// 原值: 测试名「the bound method is the runtime excerpt, not the maintainer manual」,断言摘录只含「商业运行时路由」正文、
// 不含关联技法完整调取 / 核心方法论 / 强制工作流 / 五层硬约束 / 施工判断原则等章节,且 < 500 行、< 正文 70%
// 新值: 绑定的是占星师上游 SKILL.md 正文全文(这些章节全部在内),其后是 references/product_runtime_contract.md;不再有「商业运行时路由」
// 原因: BUG-1696/BUG-1697 产品 2026-11-07:聊天完整运行占星师 skill(上游 SKILL.md 原文 + 路由全文 + 判断手册),不再摘录
test("the bound method is the astrologer's full upstream SKILL.md, followed by the product runtime contract", () => {
const body = liveSkillBody();
const bound = boundMethodText();
const contract = productRuntimeContractText();
assert.ok(bound.startsWith(body));
assert.ok(bound.endsWith(contract));
for (const heading of ["## 关联技法完整调取", "核心方法论", "三层验证法", "Ashtottari", "强制工作流", "五层硬约束", "注意事项", "强制规范速查", "MEVG 强制外部验证门控", "Transit Actionable Output", "全谱系真实调用", "静态分析10步", "施工判断原则", "冲顶路线", "37大子命令", "验证与错题体系", "开源复用边界冻结", "~/.workbuddy/skills"]) {
assert.ok(body.includes(heading), heading);
assert.ok(bound.includes(heading), heading);
}
// The live SKILL.md is the upstream file, not this product's rewrite.
assert.equal(body.includes("商业运行时路由"), false);
assert.match(readFileSync(resolve(jyotishSkillPackage.resolvedPath, "SKILL.md"), "utf8"), /^---\n[\s\S]*github\.com\/732642856\/yinduzhanxing[\s\S]*?\n---\n/);
// The product's own safety rules travel in the contract, outside her text.
assert.match(contract, /^# 产品运行合同/);
for (const phrase of ["候选出生时间不得写成 confirmed", "只能来自服务端工具结果", "不给医疗、法律、投资、安全关键指令", "不把任何结论写成已验证、已校准或确定"]) {
// 原值: "照实说明并降低置信度" / 新值: "不把任何结论写成已验证、已校准或确定" / 原因: 产品 2026-11-08 决定(BUG-1794,偏离占星师第五轮裁定 6 / 8):联网、外部资料、MEVG、真实案例校准不写进回答,后台回执保留
assert.ok(contract.includes(phrase), phrase);
assert.equal(body.includes(phrase), false, phrase);
}
// Nothing of the method is cut: the whole body, every line.
assert.equal(bound.split("\n").length, body.split("\n").length + 1 + contract.split("\n").length);
});
test("binding the method costs a fraction of activating the skill", async () => {
// What activation would have sent, measured rather than assumed: Mastra answers
// it with the entrypoint plus a flat listing of every file in the package, and
// that listing is the majority of the bytes. Nothing carries over between
// requests, so it was resent on every turn of every conversation.
const activation = probeAgent({ skills: [jyotishSkillRuntimePath] });
const skillTool = (await activation.getToolsForExecution({ runId: "probe" })).skill as {
execute: (input: unknown, context: unknown) => Promise<unknown>;
};
const activated = await skillTool.execute(
{ name: jyotishSkillPackage.name },
{ runId: "probe" } as never,
);
const activationBytes = Buffer.byteLength(
typeof activated === "string" ? activated : JSON.stringify(activated),
"utf8",
);
const boundBytes = Buffer.byteLength(jyotishSkillMethodBlock, "utf8");
assert.ok(activationBytes > boundBytes * 2, `${activationBytes} vs ${boundBytes}`);
// 原值: 0.45 / 新值: 0.46(2026-11-08 实测 63,311 / 140,080 = 0.452) / 原因: BUG-1736 / BUG-1738 运行合同加她 Q1 的三种状态表与 Q6 的外部验证四行
// 原值: 0.46 / 新值: 0.48(2026-11-08 实测 65,938 / 140,122 = 0.471) / 原因: 占星师 2026-11-08 第六轮 R3 / R4:运行合同加白话层名、错配拒句重写、自参照分支三条,约 0.8k 字(BUG-1789..1792)
assert.ok(boundBytes < activationBytes * 0.48, `${boundBytes} of ${activationBytes}`);
});
test("agents that bind the method are not given a tool to load it", async () => {
const bound = probeAgent(jyotishSkillBinding());
const toolNames = Object.keys(await bound.getToolsForExecution({ runId: "probe" }));
assert.deepEqual(toolNames, ["skill_read"]);
// The skill is still declared, so the references the method names by path stay
// reachable for the one question the delivered sections do not cover.
assert.equal(
(await bound.listSkills()).some((skill) => skill.name === jyotishSkillPackage.name),
true,
);
});
test("a prompt assembled without the method stops the run instead of answering", () => {
const abortReasons: string[] = [];
const abort = ((reason?: string) => {
abortReasons.push(reason ?? "");
throw new Error("aborted");
}) as never;
const step = (system: unknown[]) => jyotishSkillBoundProcessor.processInputStep({
messageList: { getAllSystemMessages: () => system },
abort,
} as never);
assert.throws(() => step([{ role: "system", content: "you are a guide" }]), /aborted/);
assert.match(abortReasons[0], /not bound into the system prompt/);
// A prompt that does carry the method, and a prompt that cannot be read at all,
// both pass: this guard exists to catch a missing method, not to become a new
// way for a run to fail.
assert.doesNotThrow(() => step([{ role: "system", content: jyotishSkillMethodBlock }]));
assert.doesNotThrow(() => step([{ role: "system", content: jyotishSkillMethodCoreBlock }]));
assert.doesNotThrow(() => step([]));
});
function agentConstructorBodies(source: string): string[] {
const bodies: string[] = [];
const needle = "new Agent(";
let search = 0;
while (true) {
const start = source.indexOf(needle, search);
if (start < 0) break;
const openParen = start + needle.length - 1;
let depth = 0;
let inStr: string | null = null;
let escaped = false;
let i = openParen;
for (; i < source.length; i += 1) {
const ch = source[i];
if (inStr) {
if (escaped) {
escaped = false;
continue;
}
if (ch === "\\") {
escaped = true;
continue;
}
if (ch === inStr) inStr = null;
continue;
}
if (ch === "'" || ch === '"' || ch === "`") {
inStr = ch;
continue;
}
if (ch === "(") depth += 1;
else if (ch === ")") {
depth -= 1;
if (depth === 0) {
i += 1;
break;
}
}
}
bodies.push(source.slice(start, i));
search = i;
}
return bodies;
}
// 原值:只认 `const name = \`...\`;` 模板字面量
// 新值:也认 `const name = productContractBlock(...);`,取其参数(含填入的值,如 jyotishSkillMethodBlock)
// 原因:BUG-1728 本命产品指令改为从 references/product_runtime_contract.md 读取
function constTemplateBody(file: string, name: string): string {
return new RegExp(`(?:const|let) ${name} = \`([\\s\\S]*?)\`;`).exec(file)?.[1]
?? new RegExp(`(?:const|let) ${name} = productContractBlock\\(([\\s\\S]*?)\\);`).exec(file)?.[1]
?? "";
}
function instructionSource(agentBody: string, file: string): string {
const named = /instructions:\s*([A-Za-z_][A-Za-z0-9_]*)/.exec(agentBody);
let text = named
? `${agentBody}\n${constTemplateBody(file, named[1])}`
: (/instructions:\s*`([\s\S]*?)`/.exec(agentBody)?.[1] ?? agentBody);
for (const match of text.matchAll(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g)) {
text += `\n${constTemplateBody(file, match[1])}`;
}
return text;
}
function carriesBoundMethodMarker(text: string): boolean {
return text.includes("jyotishSkillMethodBlock")
|| text.includes("jyotishSkillMethodCoreBlock")
|| text.includes(BOUND_METHOD_MARKER)
|| text.includes("<jyotish-skill name=");
}
test("every agent that attaches jyotishSkillBinding carries the method marker (BUG-954)", () => {
const mastra = readFileSync(new URL("../src/mastra/index.ts", import.meta.url), "utf8");
const boundAgents = agentConstructorBodies(mastra).filter((body) => body.includes("...jyotishSkillBinding()"));
assert.ok(boundAgents.length >= 3, `expected natal, legacy, and window agents, got ${boundAgents.length}`);
for (const body of boundAgents) {
assert.equal(carriesBoundMethodMarker(instructionSource(body, mastra)), true, body.slice(0, 160));
}
assert.match(jyotishSkillMethodCoreBlock, new RegExp(BOUND_METHOD_MARKER.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")));
assert.doesNotMatch(jyotishSkillMethodCoreBlock, /Level 2 report template/);
assert.match(jyotishSkillMethodBlock, /Level 2 report template/);
});
test("window agent input processing does not abort (BUG-954)", async () => {
const { getWindowJyotishAgent } = await import("../src/mastra/index.ts");
const { createConsultationRuntimeState, createWindowConsultationAgentContext } = await import("../src/mastra/consultation-tools.ts");
const model = {
id: "window-binding-probe",
label: "Window probe",
description: "",
creditCost: 1,
isDefault: false,
mode: "openai",
model: "openai/gpt-5-mini",
} as never;
const agent = getWindowJyotishAgent(model, createWindowConsultationAgentContext({
userId: "user",
sessionId: "session",
requestId: "req",
consultationMode: "declared_birth_window",
declaredWindow: {
name: "探针",
toolInput: {
year: 1990, month: 1, day: 2, city: "台北", lat: 25.03, lon: 121.56, tz: 8,
ayanamsa: "raman", rangeStart: "04:00", rangeEnd: "06:00",
},
truth: {
birthDate: "1990-01-02",
birthTimeSource: "family_period",
birthTimePeriod: "morning",
birthTimeStatus: "window",
wrapsMidnight: false,
placeLabel: "台北",
placeCodes: { countryCode: "TW", provinceCode: null, cityCode: null, districtCode: null },
placeId: null,
placeType: "city",
placeProvider: "profile",
timezoneId: "Asia/Taipei",
timezoneSource: "profile",
latitude: 25.03,
longitude: 121.56,
timezoneOffset: 8,
},
},
state: createConsultationRuntimeState(),
}));
const instructions = await agent.getInstructions();
const text = typeof instructions === "string" ? instructions : JSON.stringify(instructions);
assert.match(text, /<jyotish-skill name="/);
assert.doesNotMatch(text, /Level 2 report template/);
const abortReasons: string[] = [];
assert.doesNotThrow(() => jyotishSkillBoundProcessor.processInputStep({
messageList: { getAllSystemMessages: () => [{ role: "system", content: text }] },
abort: ((reason?: string) => {
abortReasons.push(reason ?? "");
throw new Error("aborted");
}) as never,
} as never));
assert.equal(abortReasons.length, 0);
});
test("only chart-answering agents bind the method", () => {
const mastra = readFileSync(new URL("../src/mastra/index.ts", import.meta.url), "utf8");
const chart = mastra.slice(
mastra.indexOf("const jyotishInstructions"),
mastra.indexOf("const generalJyotishInstructions"),
);
const general = mastra.slice(
mastra.indexOf("const generalJyotishInstructions"),
mastra.indexOf("const dailyStarlanguageInstructions"),
);
const daily = mastra.slice(
mastra.indexOf("const dailyStarlanguageInstructions"),
// 原值:截到 birthTimeGuideInstructions 新值:截到 windowJyotishInstructions 原因:2026-11-06 生时引导 agent 随会话式校正删除
mastra.indexOf("const windowJyotishInstructions"),
);
assert.match(chart, /\.\.\.jyotishSkillBinding\(\)/);
// 原值:/\$\{jyotishSkillMethodBlock\}/(模板字面量插值)
// 新值:指令从合同文件读取,方法块作为 JYOTISH_SKILL_METHOD_BLOCK 的值填入
// 原因:BUG-1728 产品规则搬进 references/product_runtime_contract.md
assert.match(chart, /JYOTISH_SKILL_METHOD_BLOCK: jyotishSkillMethodBlock/);
// 原值: 另切 onboardingInstructions,断言它也不绑方法。
// 新值: onboarding agent 已删除;general 与 daily 仍不绑。
// 原因: 建议问题生成接口下线。
for (const [name, source] of [["general", general], ["daily", daily]] as const) {
assert.doesNotMatch(source, /jyotishSkillBinding|jyotishSkillMethodBlock/, name);
}
});
test("chat binds the method boundaries, not the report-shaped shared method (BUG-1256)", async () => {
// 原值: 系统块逐字绑定共享方法(Full-Spectrum Invocation Contract + event_judgment_skeleton.md)
// 新值: 不再绑定共享方法;改为 <chat-method-boundaries>,用产品自己的话保留聊天仍要守的边界
// 原因: 2026-10-07 聊天提示词精简(BUG-1256 第二步):方法书只摘诚实边界几节,工程记录、跑分、脚本、文件路径和与聊天形状冲突的输出合同不再进每轮系统提示;共享方法要求输出 JSON verdict + A/B/C/D 置信度 + 审计表 + 原始数据 + 联网验证,与聊天形状冲突
const { CHAT_METHOD_BOUNDARIES } = await import("../src/mastra/skill-binding.ts");
for (const block of [jyotishSkillMethodBlock, jyotishSkillMethodCoreBlock]) {
assert.equal(block.includes("<jyotish-shared-method>"), false);
// 原值: block 不含「Full-Spectrum Invocation Contract」字样 / 新值: 上游 SKILL.md 原文提到它(0.0.1 全谱系真实调用)而在块内;路由那一节正文不进系统块,随每轮工具结果的路由全文下发 / 原因: BUG-1696/BUG-1697 产品 2026-11-07:聊天完整运行占星师 skill(上游 SKILL.md 原文 + 路由全文 + 判断手册),不再摘录
assert.ok(block.includes("Full-Spectrum Invocation Contract"));
assert.equal(block.includes("## 0.7 Full-Spectrum Invocation Contract"), false);
assert.ok(block.includes(CHAT_METHOD_BOUNDARIES));
}
// 原值: "Vimshottari 与 Narayana 两套大运同向"
// 新值: "看 Vimshottari 与 Narayana 两套大运:都落到这件事上的那段才写成时段"(双轨要求不变,改成说怎么写)
// 原因: BUG-1603(产品 2026-11-05):「同向」规则句被换成回答里的套话(「运期同向只说明这条线活跃,不等于婚期」)
// 原值: "看 Vimshottari 与 Narayana 两套大运:都落到这件事上的那段才写成时段" / 新值: "谈应期看 Vimshottari 与 Narayana 两套大运" + 单体系候选 / 原因: BUG-1732 占星师 Q1
for (const kept of ["不从一段大运或一次行运直接跳到「会发生」", "谈应期看 Vimshottari 与 Narayana 两套大运", "Narayana 落到这件事上也只算单体系候选窗口", "只有一套或两套相抵时写成可能", "大运起止日期只引用卡上给出的", "不宣称全球第一", "没有做外部引擎比对、全网资料核验和真实案例校准,照实说明"]) {
assert.ok(CHAT_METHOD_BOUNDARIES.includes(kept), kept);
}
// 原值:assert.match(sharedConsultationMethodMarkdown(), /Full-Spectrum Invocation Contract/)
// 新值:路由全文(含 Full-Spectrum Invocation Contract)与 event_judgment_skeleton 随每轮 methodology 送达
// 原因:BUG-1726 sharedConsultationMethodMarkdown 没有调用方,已删除;骨架改为每条路由都送
const timing = consultationMethodologyForDomains(["timing"])!;
assert.ok(timing.sections.some((section) => section.text.includes("## 0.7 Full-Spectrum Invocation Contract")));
assert.ok(timing.sections.some((section) => section.source === "references/event_judgment_skeleton.md"));
});
test("skill_read opens allowlisted sub-skill files and nothing else under skills/ (BUG-1727)", async () => {
const bound = probeAgent(jyotishSkillBinding());
const read = (await bound.getToolsForExecution({ runId: "probe" })).skill_read as {
execute: (input: unknown, context: unknown) => Promise<unknown>;
};
const open = async (path: string) => String(await read.execute({ skillName: jyotishSkillPackage.name, path }, { runId: "probe" } as never));
const usagePack = readFileSync(resolve(jyotishSkillPackage.resolvedPath, "..", "jaimini-timing", "references", "generated-usage-pack.md"), "utf8");
assert.equal(await open("skills/jaimini-timing/references/generated-usage-pack.md"), usagePack);
for (const family of SKILL_FAMILY_READ_ALLOWLIST) {
const own = readFileSync(resolve(jyotishSkillPackage.resolvedPath, "..", family, "SKILL.md"), "utf8");
assert.equal(await open(`skills/${family}/SKILL.md`), own, family);
}
// Not on the allowlist: other skill packages and their versions/ trees.
for (const path of ["skills/jyotish-full-synastry-reading/SKILL.md", "skills/jyotish-birth-time-rectification/SKILL.md"]) {
assert.match(await open(path), /not found/, path);
}
await assert.rejects(open("skills/../../AGENTS.md"));
// Her router's families are all on the allowlist.
const router = JSON.parse(readFileSync(resolve(jyotishSkillPackage.resolvedPath, "references", "skill_family_router.json"), "utf8")) as { families: Record<string, unknown> };
for (const family of Object.keys(router.families)) {
assert.ok((SKILL_FAMILY_READ_ALLOWLIST as readonly string[]).includes(family), family);
}
});
// 原值: 测试名「the chat method block stays small (BUG-1256)」,上限 4_000 字符 / 新值: 「…stays bounded (BUG-1256, BUG-1696)」,上限 40_000 字符
// (2026-11-07 实测:jyotishSkillMethodBlock 37,166、jyotishSkillMethodCoreBlock 36,959;SKILL.md 正文 35,640、产品合同约 0.5k,其余是块头与 chat-method-boundaries)
// 原因: BUG-1696/BUG-1697 产品 2026-11-07:聊天完整运行占星师 skill(上游 SKILL.md 原文 + 路由全文 + 判断手册),不再摘录
test("the chat method block stays bounded (BUG-1256, BUG-1696)", () => {
// It was 25,872 characters, most of the 55k natal system prompt; 2026-10-07 to 2026-11-06 it was under 4,000.
// 原值: 40_000 / 新值: 42_000(2026-11-08 实测 40,343) / 原因: BUG-1734..1738 运行合同加 Narayana 只写卡上标出的步骤、Q1 状态表、Q6 外部验证四行,约 0.85k 字
// 原值: 42_000 / 新值: 42_500(2026-11-18 实测 42,085) / 原因: BUG-1977 chat-method-boundaries 加 B-06「用户讲的经历只用于生时纠正」(占星师 11-18 裁定),约 0.12k 字
assert.ok(jyotishSkillMethodBlock.length < 42_500, `${jyotishSkillMethodBlock.length}`);
assert.ok(jyotishSkillMethodCoreBlock.length < 42_500, `${jyotishSkillMethodCoreBlock.length}`);
// The ceiling follows the live file: the block is the body plus a bounded wrapper, not a second copy.
// 原值: + 2_500 / 新值: + 4_500(2026-11-08 实测块 39,509) / 原因: BUG-1731 / BUG-1732 运行合同加「今年」按公历年与 Narayana 单体系候选两条(她的 Q2 / Q1),约 1.7k 字
// 原值: + 4_500 / 新值: + 5_500(2026-11-08 实测块 40,343) / 原因: BUG-1734..1738 运行合同多约 0.85k 字(同上)
// 原值: + 5_500 / 新值: + 6_500(2026-11-08 实测块 41,801) / 原因: 占星师 2026-11-08 第六轮 R3 / R4:运行合同多约 0.8k 字(白话层名、错配拒句重写、自参照分支,BUG-1789..1792)
// 原值: + 6_500 / 新值: + 7_000(2026-11-18 实测块 42,085) / 原因: BUG-1977 B-06(同上)
assert.ok(jyotishSkillMethodBlock.length < liveSkillBody().length + 7_000, `${jyotishSkillMethodBlock.length}`);
});