feat: 优化运行时判定

This commit is contained in:
richarjiang
2026-09-24 14:27:07 +08:00
parent dfd3998399
commit f0ac603f27
25 changed files with 2368 additions and 58 deletions

76
tests/test_skills.py Normal file
View File

@@ -0,0 +1,76 @@
from laya_router.skills import decide_skill, discover_skills, suggest_skill
def _skill(root, name, description, *, disabled=False, body="Do the documented steps."):
folder = root / name
folder.mkdir(parents=True)
flag = "true" if disabled else "false"
(folder / "SKILL.md").write_text(
f"---\nname: {name}\ndescription: {description}\ndisable-model-invocation: {flag}\n---\n{body}\n",
encoding="utf-8",
)
def test_discover_skips_disabled_skills(tmp_path):
home = tmp_path / "home"
_skill(home / ".claude" / "skills", "commit", "Create a git commit")
_skill(home / ".agents" / "skills", "hidden", "Should stay hidden", disabled=True)
found = discover_skills(None, home)
assert [skill.name for skill in found] == ["commit"]
def test_gate_and_fits_thresholds():
assert decide_skill(
ranked_names=["commit", "review"],
gate_scores={"acts_on_user_system": 0.1, "would_follow_documented_procedure": 0.1, "prose_suffices": 0.9},
rerank_choice="commit",
fits=[0.9, 0.1],
) is None
assert decide_skill(
ranked_names=["commit", "review"],
gate_scores={"acts_on_user_system": 0.8, "would_follow_documented_procedure": 0.8, "prose_suffices": 0.1},
rerank_choice="review",
fits=[0.2, 0.1],
) is None
assert decide_skill(
ranked_names=["commit", "review"],
gate_scores={"acts_on_user_system": 0.8, "would_follow_documented_procedure": 0.8, "prose_suffices": 0.1},
rerank_choice="review",
fits=[0.4, 0.7],
) == "review"
class _Agent:
def __init__(self, answers):
self.answers = list(answers)
self.calls = 0
def predict(self, state, questions):
payload = self.answers[self.calls % len(self.answers)]
self.calls += 1
return {"answers": payload}
def test_suggest_skips_a_skill_already_named(tmp_path):
home = tmp_path / "home"
_skill(home / ".claude" / "skills", "commit", "Create a git commit")
skills = discover_skills(None, home)
agent = _Agent(
[
{
"which": {"choice": "commit", "probabilities": {"commit": 0.9}},
"acts_on_user_system": {"noul": 0.9},
"would_follow_documented_procedure": {"noul": 0.9},
"prose_suffices": {"noul": 0.1},
},
{
"which": {"choice": "commit"},
"fits_0": {"noul": 0.8},
},
]
)
first = suggest_skill(agent, skills, "commit these changes", inject="suggest", already=set())
assert first["name"] == "commit"
assert first["mode"] == "suggest"
again = suggest_skill(agent, skills, "commit these changes", inject="suggest", already={"commit"})
assert again is None