From a29371f510c6a2a2286b6518f94d21c62df91328 Mon Sep 17 00:00:00 2001 From: Denton Social Date: Tue, 29 Sep 2026 02:47:43 -0500 Subject: [PATCH] navigation: confidence gate on create_skill/create_category The create branches share a softmax with the real options, so a weak plurality win was enough to trigger authoring. Require P(create) to clear navigation.create_tau and lead the best existing option by navigation.create_margin; otherwise fall back to the best existing option (the intent guard remains a second door) and trace create_suppressed. An empty tree/category still short-circuits straight to create. --- AGENTS.md | 8 +++ config.example.json | 5 ++ semif_agent/scheduler.py | 13 +++- semif_agent/skills.py | 68 +++++++++++++++++++ tests/test_skills.py | 141 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 234 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 8258fcd..bc76aec 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -275,6 +275,14 @@ CLI, unit tests (24) + box integration tests (2). new category runs the same chain deterministically: `create_category` → `create_skill` → async body → re-dispatch. Authoring is still a single pass — validating/reusing written bodies across runs is future work. + **Create-branch confidence gate** (Sep 2026): `create_skill`/`create_category` + compete in a softmax with the real options, so a weak plurality win is not + evidence that nothing matches. Navigation fires a create branch only when + `P(create) >= navigation.create_tau` **and** it leads the best existing option + by `navigation.create_margin`; otherwise it falls back to the best existing + option (a genuinely unmatched action still reaches authoring through the + intent guard) and traces `create_suppressed` with the probs. An empty + tree/category still short-circuits straight to create. - **Real integrations, implementation questions, fidelity + repair** (Sep 2026): elicitation is on by default and asks implementation questions (which service/account, how to connect, where the credential comes from, what success diff --git a/config.example.json b/config.example.json index 4502c0c..a1fb6ee 100644 --- a/config.example.json +++ b/config.example.json @@ -2,6 +2,11 @@ "_comment": "Per-machine config. config.json is gitignored; copy this file to config.json and adjust paths. The box (guppy) keeps its own config.json with local paths.", "tau": 0.6, "max_reentries": 3, + "navigation": { + "_comment": "Confidence gate on the create branches. create_skill/create_category share a softmax with the real skills/categories, so a weak plurality win is not evidence that nothing matches. The create branch only fires when P(create) >= create_tau AND it leads the best existing option by create_margin; otherwise navigation falls back to the best existing option (a genuinely unmatched action still reaches authoring through the intent guard). Suppressed picks are traced as create_suppressed with their probs so the thresholds can be calibrated from real runs. A level with nothing to choose from still short-circuits to create.", + "create_tau": 0.4, + "create_margin": 0.15 + }, "queue": {"max_size": 100, "age_rate": 0.01}, "engine": { "backend": "llamacpp", diff --git a/semif_agent/scheduler.py b/semif_agent/scheduler.py index fac7e26..9136301 100644 --- a/semif_agent/scheduler.py +++ b/semif_agent/scheduler.py @@ -224,6 +224,9 @@ class Scheduler: self.trace = trace if trace is not None else TraceLog() self.config = config self.tau = tau + nav_cfg = config.get("navigation", {}) or {} + self.create_tau = float(nav_cfg.get("create_tau", 0.4)) + self.create_margin = float(nav_cfg.get("create_margin", 0.15)) self.max_reentries = max_reentries self.codegen = codegen self.degeneration_check_factory = degeneration_check_factory @@ -455,7 +458,15 @@ class Scheduler: # ---- dispatch ---- def _dispatch(self, request: Request, weight: float = 0.0) -> DispatchResult: - navigation = navigate(self.engine, self.log, self.trace, request, self.tree) + navigation = navigate( + self.engine, + self.log, + self.trace, + request, + self.tree, + create_tau=self.create_tau, + create_margin=self.create_margin, + ) if isinstance(navigation, CreateCategory): created = self._create_category(request) if created.kind != "create_category": diff --git a/semif_agent/skills.py b/semif_agent/skills.py index 9a53d83..9f2aa62 100644 --- a/semif_agent/skills.py +++ b/semif_agent/skills.py @@ -526,12 +526,39 @@ def merge_registry(tree: dict[str, list[Skill]], categories: dict[str, dict]) -> existing.add(name) +def _clears_create_gate( + result, + create_id: str, + existing_ids: list[str], + tau: float, + margin: float, +) -> bool: + """Should the create branch actually fire, or is it a weak plurality win? + + The create option shares a softmax with the real categories/skills, so its + probability is diluted by every existing option — it is not a measure of + "confidence that nothing matches". Require both an absolute floor (`tau`) + and a clear lead over the best existing option (`margin`); a create that + only squeaks past on a crowded tree is suppressed and navigation falls back + to the best existing branch (the intent guard remains a second door to + authoring). Returns True when the gate is effectively disabled. + """ + probs = result.probs + if create_id not in probs: + return True + create_p = probs[create_id] + best = max((probs[i] for i in existing_ids if i in probs), default=0.0) + return create_p >= tau and (create_p - best) >= margin + + def navigate( engine: SemIfEngine, log: DecisionLog, trace: TraceLog, request: Request, tree: dict[str, list[Skill]], + create_tau: float = 0.4, + create_margin: float = 0.15, ) -> Skill | CreateCategory | CreateSkill: """Descend the tree one SemIf choice per level. Every choice is logged. @@ -541,6 +568,12 @@ def navigate( (an empty tree, or a category with no skills yet) short-circuits straight to the create branch: SemIf decisions need at least two options, and asking "which of one?" is meaningless. + + A create branch that wins the softmax but fails the confidence gate + (`create_tau` floor and `create_margin` lead over the best existing option) + is suppressed: navigation falls back to the best existing option so a + genuinely unmatched action still reaches authoring through the intent guard, + while a crowded tree no longer drifts into create on a weak plurality. """ categories = sorted(tree.keys()) create_category = Option("create_category", "Suggest a new category for this.") @@ -563,6 +596,23 @@ def navigate( top_result = engine.call(top) log.append(top, top_result, extra={"phase": "navigate:category", "run_id": request.id}) category = top_result.selected + if category == "create_category" and not _clears_create_gate( + top_result, "create_category", categories, create_tau, create_margin + ): + fallback = max( + (c for c in categories if c in top_result.probs), + key=lambda c: top_result.probs[c], + ) + trace.append( + "create_suppressed", + request.id, + level="category", + fallback=fallback, + probs=top_result.probs, + tau=create_tau, + margin=create_margin, + ) + category = fallback if category == "create_category": trace.append( "create_category", @@ -600,6 +650,24 @@ def navigate( leaf_result = engine.call(leaf) log.append(leaf, leaf_result, extra={"phase": "navigate:leaf", "run_id": request.id}) pick = leaf_result.selected + if pick == "create_skill" and not _clears_create_gate( + leaf_result, "create_skill", [s.name for s in skills], create_tau, create_margin + ): + fallback = max( + (s.name for s in skills if s.name in leaf_result.probs), + key=lambda n: leaf_result.probs[n], + ) + trace.append( + "create_suppressed", + request.id, + level="leaf", + category=category, + fallback=fallback, + probs=leaf_result.probs, + tau=create_tau, + margin=create_margin, + ) + pick = fallback if pick == "create_skill": trace.append( "skill_needed", diff --git a/tests/test_skills.py b/tests/test_skills.py index 04eedf3..11880a3 100644 --- a/tests/test_skills.py +++ b/tests/test_skills.py @@ -36,6 +36,7 @@ from semif_agent.skills import ( CategoryDraft, CategoryRegistry, CreateCategory, + CreateSkill, Prediction, Skill, SkillDraft, @@ -601,6 +602,146 @@ def test_navigate_leaf_offers_create_for_unmatched_action(tmp_path): assert "action" in engine.request.question +class _ProbEngine: + """Returns caller-specified probabilities, keyed by a substring of the + decision question, so a test can force a weak create plurality.""" + + def __init__(self, by_question: dict[str, dict[str, float]]): + self.by_question = by_question + self.calls = [] + + def call(self, request): + from semif_agent.decisions import DecisionResult + + self.calls.append(request) + ids = [o.id for o in request.options] + probs_map = {} + for key, value in self.by_question.items(): + if key in request.question: + probs_map = value + break + if not probs_map: + probs_map = {ids[0]: 1.0} + return DecisionResult( + request=request, + option_ids=ids, + probabilities=[float(probs_map.get(i, 0.0)) for i in ids], + ) + + +def _leaf_tree(): + return { + "simplex": [ + Skill( + name="next_message", + category="simplex", + description="Read the next SimpleX message.", + ) + ] + } + + +def test_navigate_suppresses_weak_create_plurality(tmp_path): + """create_skill wins the softmax by a hair but fails the margin gate: the + weak plurality must fall back to the best existing skill, not author.""" + log = DecisionLog(str(tmp_path / "decisions.jsonl")) + trace = TraceLog(str(tmp_path / "runs.jsonl")) + engine = _ProbEngine( + { + "top-level category": {"simplex": 1.0}, + "skill performs": {"next_message": 0.45, "create_skill": 0.5}, + } + ) + result = navigate( + engine, log, trace, Request("send a simplex message"), _leaf_tree() + ) + assert getattr(result, "name", None) == "next_message" + suppressed = [e for e in trace.read() if e["kind"] == "create_suppressed"] + assert len(suppressed) == 1 + assert suppressed[0]["level"] == "leaf" + assert suppressed[0]["fallback"] == "next_message" + + +def test_navigate_create_clears_margin_and_tau(tmp_path): + """A create that leads the best skill by >= margin and clears tau fires.""" + log = DecisionLog(str(tmp_path / "decisions.jsonl")) + trace = TraceLog(str(tmp_path / "runs.jsonl")) + engine = _ProbEngine( + { + "top-level category": {"simplex": 1.0}, + "skill performs": {"next_message": 0.2, "create_skill": 0.7}, + } + ) + result = navigate( + engine, log, trace, Request("send a simplex message"), _leaf_tree() + ) + assert isinstance(result, CreateSkill) + assert result.category == "simplex" + assert not [e for e in trace.read() if e["kind"] == "create_suppressed"] + + +def test_navigate_create_below_tau_is_suppressed(tmp_path): + """Even with a clear lead, a create below the absolute floor is suppressed + (all options weak => nothing is a confident match).""" + log = DecisionLog(str(tmp_path / "decisions.jsonl")) + trace = TraceLog(str(tmp_path / "runs.jsonl")) + engine = _ProbEngine( + { + "top-level category": {"simplex": 1.0}, + "skill performs": {"next_message": 0.1, "create_skill": 0.3}, + } + ) + result = navigate( + engine, log, trace, Request("send a simplex message"), _leaf_tree() + ) + assert getattr(result, "name", None) == "next_message" + assert any(e["kind"] == "create_suppressed" for e in trace.read()) + + +def test_navigate_category_create_gate_suppresses_and_falls_back(tmp_path): + """The same gate applies to create_category: a weak plurality win falls back + to the best existing category and descends into it.""" + log = DecisionLog(str(tmp_path / "decisions.jsonl")) + trace = TraceLog(str(tmp_path / "runs.jsonl")) + tree = _leaf_tree() + tree["calendar"] = [ + Skill(name="next_event", category="calendar", description="Report the next event.") + ] + engine = _ProbEngine( + { + "top-level category": { + "calendar": 0.30, + "simplex": 0.32, + "create_category": 0.38, + }, + "skill performs": {"next_message": 1.0}, + } + ) + result = navigate(engine, log, trace, Request("send a simplex message"), tree) + assert getattr(result, "name", None) == "next_message" + suppressed = [e for e in trace.read() if e["kind"] == "create_suppressed"] + assert len(suppressed) == 1 and suppressed[0]["level"] == "category" + assert suppressed[0]["fallback"] == "simplex" + + +def test_navigate_category_create_clears_gate(tmp_path): + log = DecisionLog(str(tmp_path / "decisions.jsonl")) + trace = TraceLog(str(tmp_path / "runs.jsonl")) + tree = _leaf_tree() + engine = _ProbEngine( + { + "top-level category": { + "simplex": 0.1, + "create_category": 0.8, + }, + } + ) + result = navigate(engine, log, trace, Request("book a flight"), tree) + assert isinstance(result, CreateCategory) + assert not [e for e in trace.read() if e["kind"] == "create_suppressed"] + + + def test_confirm_skill_fit_returns_true_when_action_matches(tmp_path): log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl"))