The response category scope claimed to cover "inputs the agent cannot act on", so an actionable request like "what is 255 * 12?" scored as in-scope and landed in the response.unable canned refusal. Add the actionability guard (confirm_non_action, phase navigate:actionability, navigation.action_tau) which arbitrates the response/create_category boundary whichever side the softmax proposed: the closed response tree is now reached only when the input is not a request, so a request no skill covers authors a skill instead. Reword the response scope, drop the response.unable reply, and update tests/docs.
1099 lines
41 KiB
Python
1099 lines
41 KiB
Python
"""The skill tree, registry, and SemIf-driven navigation.
|
||
|
||
A skill is a leaf reached by a chain of SemIf choices (category -> skill).
|
||
The category level carries a "create_category" branch and the leaf level a
|
||
"create_skill" branch. Both are live: a small OpenAI-compatible model (`llm`,
|
||
separate from `codegen`) proposes a title + description — a broad new category
|
||
or a specific new skill leaf — which is persisted to a category registry and
|
||
merged into the running tree as a stub.
|
||
|
||
Only the real skills live here; navigation uses the real decision engine.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import importlib.util
|
||
import json
|
||
import os
|
||
import re
|
||
from dataclasses import dataclass, field
|
||
from pathlib import Path
|
||
from typing import Callable
|
||
|
||
from .decisions import DecisionRequest, Option, Request
|
||
from .engine import SemIfEngine
|
||
from .llm import LLMClient
|
||
from .log import DecisionLog
|
||
from .timers import TimerService
|
||
from .trace import TraceLog
|
||
|
||
|
||
@dataclass
|
||
class ActionResult:
|
||
action_log: str
|
||
new_state: str
|
||
needs_input: str | None = None
|
||
# Any (DecisionRequest, DecisionResult) pairs the body made during `act`;
|
||
# the runner logs them with the run outcome (phase `act`).
|
||
decisions: list[tuple[DecisionRequest, object]] = field(default_factory=list)
|
||
|
||
|
||
@dataclass
|
||
class ActionContext:
|
||
engine: SemIfEngine
|
||
config: dict
|
||
timers: TimerService | None = None
|
||
|
||
|
||
def _noop_act(ctx: ActionContext, request: Request) -> ActionResult:
|
||
return ActionResult("", "")
|
||
|
||
|
||
@dataclass
|
||
class Skill:
|
||
name: str
|
||
category: str
|
||
description: str
|
||
cost_budget: float = 1.0
|
||
act: Callable[[ActionContext, Request], ActionResult] = field(default=_noop_act)
|
||
writing: bool = False
|
||
config: dict = field(default_factory=dict)
|
||
contract: dict = field(default_factory=dict)
|
||
integration: dict = field(default_factory=dict)
|
||
integration_source: str = "unknown"
|
||
category_description: str = ""
|
||
|
||
def is_noop(self) -> bool:
|
||
"""A stub leaf: authored (title + description) but no runnable body yet."""
|
||
return self.act is _noop_act
|
||
|
||
@property
|
||
def status(self) -> str:
|
||
"""Leaf readiness: `writing` (codegen in flight), `stub` (no body), `ready`."""
|
||
if self.writing:
|
||
return "writing"
|
||
if self.is_noop():
|
||
return "stub"
|
||
return "ready"
|
||
|
||
|
||
@dataclass
|
||
class CreateSkill:
|
||
"""Suggestion that the current category needs a new skill.
|
||
|
||
Handled live, like CreateCategory: the small `llm` provider authors the new
|
||
skill stub, which is persisted and merged into the tree. `category` names the
|
||
category that needs the new skill.
|
||
"""
|
||
|
||
category: str
|
||
|
||
|
||
@dataclass
|
||
class CreateCategory:
|
||
"""Suggestion that the request needs a brand-new top-level category.
|
||
|
||
Like CreateSkill this is handled live: the small `llm` provider authors the
|
||
category stub.
|
||
"""
|
||
|
||
|
||
@dataclass
|
||
class CategoryDraft:
|
||
"""An authored category stub: a broad bucket for future skills."""
|
||
|
||
name: str
|
||
description: str
|
||
|
||
|
||
@dataclass
|
||
class SkillDraft:
|
||
"""An authored skill leaf stub: one specific action within a category.
|
||
|
||
`requirements` maps elicitation questions (asked of the human during
|
||
authoring) to their answers; they are fed to the body-writer so the body
|
||
reflects the refined product goal. `integration` carries the service and
|
||
transport the answers point at; it is fed to the body writer as an advisory
|
||
hint only — the CODEGEN.md contract, request, and requirements still win.
|
||
"""
|
||
|
||
name: str
|
||
description: str
|
||
code: str = ""
|
||
requirements: dict[str, str] = field(default_factory=dict)
|
||
integration: dict = field(default_factory=dict)
|
||
|
||
|
||
class CategoryRegistry:
|
||
"""Persisted category stubs, one file on disk.
|
||
|
||
Format: {name: {"description": str, "skills": [{"name": str, "description":
|
||
str}, ...]}}. The skills list is filled by create_skill; each entry becomes
|
||
a stub leaf merged into the running tree.
|
||
"""
|
||
|
||
def __init__(self, path: str = "data/categories.json"):
|
||
self.path = Path(path)
|
||
|
||
def read(self) -> dict[str, dict]:
|
||
if not self.path.is_file():
|
||
return {}
|
||
return json.loads(self.path.read_text())
|
||
|
||
def register(self, name: str, description: str) -> None:
|
||
categories = self.read()
|
||
categories[name] = {"description": description, "skills": []}
|
||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||
self.path.write_text(json.dumps(categories, indent=2) + "\n")
|
||
|
||
def register_skill(
|
||
self,
|
||
category: str,
|
||
name: str,
|
||
description: str,
|
||
request_text: str = "",
|
||
requirements: dict[str, str] | None = None,
|
||
) -> None:
|
||
"""Add a skill leaf to a category, creating the category entry if needed.
|
||
|
||
`request_text` is the originating request, kept so a later restart of the
|
||
stub can re-drive codegen with the same context. `requirements` are the
|
||
elicitation answers, kept so a restart does not have to ask again.
|
||
"""
|
||
categories = self.read()
|
||
entry = categories.setdefault(category, {"description": "", "skills": []})
|
||
skills = entry.setdefault("skills", [])
|
||
entry_row = next((s for s in skills if s.get("name") == name), None)
|
||
if entry_row is None:
|
||
entry_row = {"name": name, "description": description}
|
||
skills.append(entry_row)
|
||
entry_row["request_text"] = request_text
|
||
if requirements:
|
||
entry_row["requirements"] = requirements
|
||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||
self.path.write_text(json.dumps(categories, indent=2) + "\n")
|
||
|
||
|
||
class SkillStore:
|
||
"""Persists one skill leaf as a folder of deliverables.
|
||
|
||
Layout: <base>/<category>/<name>/{skill.py, skill.test.py, contract.json,
|
||
config.json}. skill.py is the runnable body; the rest are produced by the
|
||
contract/test generation steps and read back so skills stay runnable and
|
||
configurable across restarts. The old single-file layout
|
||
(<base>/<category>/<name>.py) is NOT read — this is a clean switch.
|
||
"""
|
||
|
||
def __init__(self, path: str = "data/skills"):
|
||
self.path = Path(path)
|
||
|
||
def dir(self, category: str, name: str) -> Path:
|
||
return self.path / category / name
|
||
|
||
def write_body(self, category: str, name: str, code: str) -> Path:
|
||
directory = self.dir(category, name)
|
||
directory.mkdir(parents=True, exist_ok=True)
|
||
target = directory / "skill.py"
|
||
target.write_text(code.rstrip() + "\n")
|
||
return target
|
||
|
||
def write_contract(self, category: str, name: str, contract: dict) -> Path:
|
||
return self._write_json(category, name, "contract.json", contract)
|
||
|
||
def write_config(self, category: str, name: str, config: dict) -> Path:
|
||
return self._write_json(category, name, "config.json", config)
|
||
|
||
def write_test(self, category: str, name: str, code: str) -> Path:
|
||
directory = self.dir(category, name)
|
||
directory.mkdir(parents=True, exist_ok=True)
|
||
target = directory / "skill.test.py"
|
||
target.write_text(code.rstrip() + "\n")
|
||
return target
|
||
|
||
def _write_json(self, category: str, name: str, filename: str, value) -> Path:
|
||
directory = self.dir(category, name)
|
||
directory.mkdir(parents=True, exist_ok=True)
|
||
target = directory / filename
|
||
target.write_text(json.dumps(value, indent=2) + "\n")
|
||
return target
|
||
|
||
def read_contract(self, category: str, name: str) -> dict:
|
||
return self._read_json(category, name, "contract.json")
|
||
|
||
def read_config(self, category: str, name: str) -> dict:
|
||
return self._read_json(category, name, "config.json")
|
||
|
||
def read_manifest(self, category: str, name: str) -> dict:
|
||
return self._read_json(category, name, "manifest.json")
|
||
|
||
def _read_json(self, category: str, name: str, filename: str):
|
||
path = self.dir(category, name) / filename
|
||
if not path.is_file():
|
||
return {}
|
||
try:
|
||
return json.loads(path.read_text())
|
||
except ValueError:
|
||
return {}
|
||
|
||
def read_category_config(self, category: str) -> dict:
|
||
"""Category-scoped config: <base>/<category>/config.json."""
|
||
path = self.path / category / "config.json"
|
||
if not path.is_file():
|
||
return {}
|
||
try:
|
||
return json.loads(path.read_text())
|
||
except ValueError:
|
||
return {}
|
||
|
||
def write_category_config(self, category: str, config: dict) -> Path:
|
||
directory = self.path / category
|
||
directory.mkdir(parents=True, exist_ok=True)
|
||
target = directory / "config.json"
|
||
target.write_text(json.dumps(config, indent=2) + "\n")
|
||
return target
|
||
|
||
def list_skills(self) -> list[tuple[str, str]]:
|
||
if not self.path.is_dir():
|
||
return []
|
||
skills = []
|
||
for category_dir in sorted(self.path.iterdir()):
|
||
if not category_dir.is_dir():
|
||
continue
|
||
for entry in sorted(category_dir.iterdir()):
|
||
if entry.is_dir() and (entry / "skill.py").is_file():
|
||
skills.append((category_dir.name, entry.name))
|
||
return skills
|
||
|
||
|
||
def load_skill_module(category: str, name: str, base: str = "data/skills"):
|
||
"""Import a persisted skill body and return its module."""
|
||
path = Path(base) / category / name / "skill.py"
|
||
module_name = f"_skill_{category}_{name}".replace("-", "_")
|
||
spec = importlib.util.spec_from_file_location(module_name, path)
|
||
if spec is None or spec.loader is None:
|
||
raise ValueError(f"cannot load skill module: {path}")
|
||
module = importlib.util.module_from_spec(spec)
|
||
spec.loader.exec_module(module)
|
||
return module
|
||
|
||
|
||
def _extract_integration(code: str) -> tuple[dict, str]:
|
||
"""Integration declaration (or inference) for a body, lazily.
|
||
|
||
Lives in codegen.py; imported here so skills.py stays free of codegen at
|
||
module import time (codegen imports skills).
|
||
"""
|
||
from .codegen import extract_integration
|
||
|
||
return extract_integration(code)
|
||
|
||
|
||
def _extract_contract(code: str) -> dict:
|
||
"""The body's flat `CONTRACT` declaration, or {} when absent/unparseable.
|
||
|
||
The module constant is the source of truth; `contract.json` is a derived
|
||
mirror. Lives in codegen.py; imported lazily so skills.py stays free of
|
||
codegen at module import time.
|
||
"""
|
||
from .codegen import parse_contract
|
||
|
||
try:
|
||
return parse_contract(code)
|
||
except ValueError:
|
||
return {}
|
||
|
||
|
||
def materialize_skill(
|
||
draft: SkillDraft, category: str, store: SkillStore
|
||
) -> Skill:
|
||
"""Persist the draft's code body and build a runnable Skill from it.
|
||
|
||
The skill's config and contract are read back from the store so the runner
|
||
can resolve its data needs across restarts; its integration declaration is
|
||
extracted from the body so the tree can show what it talks to.
|
||
"""
|
||
if not draft.code:
|
||
raise ValueError(f"skill {draft.name} has no code body to materialize")
|
||
store.write_body(category, draft.name, draft.code)
|
||
try:
|
||
module = load_skill_module(category, draft.name, store.path)
|
||
except Exception as exc:
|
||
raise ValueError(f"skill {category}.{draft.name} body failed to import: {exc}") from exc
|
||
if not callable(getattr(module, "act", None)):
|
||
raise ValueError(f"skill {category}.{draft.name} body must define act")
|
||
integration, integration_source = _extract_integration(draft.code)
|
||
contract = _extract_contract(draft.code) or store.read_contract(category, draft.name)
|
||
store.write_contract(category, draft.name, contract)
|
||
return Skill(
|
||
name=draft.name,
|
||
category=category,
|
||
description=draft.description,
|
||
act=module.act,
|
||
config=store.read_config(category, draft.name),
|
||
contract=contract,
|
||
integration=integration,
|
||
integration_source=integration_source,
|
||
)
|
||
|
||
|
||
def merge_skill_store(
|
||
tree: dict[str, list[Skill]], store: SkillStore, registry: dict[str, dict]
|
||
) -> int:
|
||
"""Upgrade persisted skill folders in the tree to runnable skills.
|
||
|
||
A skill folder makes a stub leaf executable; where the registry entry was
|
||
lost (or never written), the category is created and the description falls
|
||
back to the skill name. Returns the number of skills made runnable.
|
||
"""
|
||
upgraded = 0
|
||
for category, name in store.list_skills():
|
||
description = ""
|
||
entry = registry.get(category, {})
|
||
for skill in entry.get("skills", []):
|
||
if skill.get("name") == name:
|
||
description = skill.get("description", "")
|
||
category_description = str(
|
||
entry.get("description") or CATEGORY_DESCRIPTIONS.get(category, "")
|
||
)
|
||
try:
|
||
module = load_skill_module(category, name, store.path)
|
||
except Exception:
|
||
continue
|
||
if not callable(getattr(module, "act", None)):
|
||
continue
|
||
code_path = store.dir(category, name) / "skill.py"
|
||
try:
|
||
code = code_path.read_text()
|
||
integration, integration_source = _extract_integration(code)
|
||
except OSError:
|
||
code, integration, integration_source = "", {}, "unknown"
|
||
contract = _extract_contract(code) or store.read_contract(category, name)
|
||
skill = Skill(
|
||
name=name,
|
||
category=category,
|
||
description=description or name,
|
||
act=module.act,
|
||
config=store.read_config(category, name),
|
||
contract=contract,
|
||
integration=integration,
|
||
integration_source=integration_source,
|
||
category_description=category_description,
|
||
)
|
||
skills = tree.setdefault(category, [])
|
||
for index, existing in enumerate(skills):
|
||
if existing.name == name:
|
||
skills[index] = skill
|
||
break
|
||
else:
|
||
skills.append(skill)
|
||
skills.sort(key=lambda s: s.name)
|
||
upgraded += 1
|
||
return upgraded
|
||
|
||
|
||
def merge_seed_store(
|
||
tree: dict[str, list[Skill]],
|
||
seed_store: SkillStore,
|
||
config_store: SkillStore | None = None,
|
||
) -> int:
|
||
"""Load committed starter skills from a read-only seed store into the tree.
|
||
|
||
A seed is a real skill in the same folder layout as a generated one
|
||
(`skill.py`, `contract.json`, `skill.test.py`, plus a `manifest.json` with
|
||
its description), so it is inspectable and testable exactly like a codegen
|
||
body. `config_store` is the runtime store (`data/skills`): recorded answers
|
||
— credentials collected at first fire — live there, never in the committed
|
||
seed folder. Returns the number of skills loaded.
|
||
"""
|
||
loaded = 0
|
||
for category, name in seed_store.list_skills():
|
||
directory = seed_store.dir(category, name)
|
||
try:
|
||
module = load_skill_module(category, name, seed_store.path)
|
||
except Exception:
|
||
continue
|
||
act = getattr(module, "act", None)
|
||
if not callable(act):
|
||
continue
|
||
try:
|
||
code = (directory / "skill.py").read_text()
|
||
except OSError:
|
||
continue
|
||
integration, integration_source = _extract_integration(code)
|
||
contract = _extract_contract(code) or seed_store.read_contract(category, name)
|
||
manifest = seed_store.read_manifest(category, name)
|
||
description = str(manifest.get("description") or name)
|
||
skill = Skill(
|
||
name=name,
|
||
category=category,
|
||
description=description,
|
||
act=act,
|
||
config=(config_store or seed_store).read_config(category, name),
|
||
contract=contract,
|
||
integration=integration,
|
||
integration_source=integration_source,
|
||
category_description=CATEGORY_DESCRIPTIONS.get(category, ""),
|
||
)
|
||
skills = tree.setdefault(category, [])
|
||
for index, existing in enumerate(skills):
|
||
if existing.name == name:
|
||
skills[index] = skill
|
||
break
|
||
else:
|
||
skills.append(skill)
|
||
skills.sort(key=lambda s: s.name)
|
||
loaded += 1
|
||
return loaded
|
||
|
||
|
||
def resolve_skill_config(
|
||
store: SkillStore, skill: Skill, global_config: dict, answered: dict | None = None
|
||
) -> dict:
|
||
"""Tiered config lookup: global config.json -> category config -> skill
|
||
config, plus any per-fire answers collected this run.
|
||
|
||
Higher tiers win. This is the merged view a skill reads from `ctx.config`.
|
||
"""
|
||
merged: dict = {}
|
||
merged.update(global_config)
|
||
merged.update(store.read_category_config(skill.category))
|
||
merged.update(skill.config or {})
|
||
for key, value in (answered or {}).items():
|
||
merged[key] = value
|
||
return merged
|
||
|
||
|
||
def unresolved_variables(
|
||
store: SkillStore, skill: Skill, global_config: dict, answered: dict | None = None
|
||
) -> list[str]:
|
||
"""Contract variables the runner has not satisfied yet (not in the merged
|
||
config and not answered this run). A missing input var is asked of the
|
||
human before act runs."""
|
||
merged = resolve_skill_config(store, skill, global_config, answered)
|
||
return [name for name in (skill.contract or {}) if name not in merged]
|
||
|
||
|
||
def compose_state(request: Request, additional_context: str | None = None) -> str:
|
||
parts = [request.text]
|
||
if additional_context:
|
||
parts.append(f"({additional_context})")
|
||
return " ".join(parts)
|
||
|
||
|
||
def _canned_response(name: str, message: str):
|
||
"""An act that returns a fixed line. No transport, no generation, no config."""
|
||
|
||
def act(ctx: ActionContext, request: Request) -> ActionResult:
|
||
return ActionResult(action_log=f"{name}: {message}", new_state=message)
|
||
|
||
return act
|
||
|
||
|
||
# The `response` category is a closed tree of canned replies: it never authors a
|
||
# skill (no create route) and its runs are not assessed (there is no side effect
|
||
# to succeed or fail). Every non-task input lands here instead of being gated.
|
||
CANNED_CATEGORIES = frozenset({"response"})
|
||
RESPONSE_FALLBACK = "response.clarify"
|
||
|
||
_CANNED_RESPONSES = [
|
||
(
|
||
"greeting",
|
||
"The user is greeting the agent (hello, hi, good morning).",
|
||
"Hello! What can I help you with?",
|
||
),
|
||
(
|
||
"thanks",
|
||
"The user is thanking the agent.",
|
||
"You're welcome! Anything else?",
|
||
),
|
||
(
|
||
"acknowledge",
|
||
"The user is acknowledging or making filler the agent should nod to.",
|
||
"Got it.",
|
||
),
|
||
(
|
||
"farewell",
|
||
"The user is signing off (bye, goodbye, see you).",
|
||
"Goodbye! Talk to you later.",
|
||
),
|
||
(
|
||
"affirm",
|
||
"The user is agreeing or confirming (yes, ok, sure, sounds good).",
|
||
"Okay!",
|
||
),
|
||
(
|
||
"clarify",
|
||
"The input is too vague to act on; ask the user to be more specific.",
|
||
"Could you try being more specific?",
|
||
),
|
||
]
|
||
|
||
_RESPONSE_MESSAGES = {f"response.{name}": message for name, _, message in _CANNED_RESPONSES}
|
||
|
||
# Built-in category descriptions. A category level that offers only bare names
|
||
# carries no signal (real runs sent "book a flight to japan" to the `simplex`
|
||
# bucket 0.49 vs create_category 0.12); giving the model the category's scope
|
||
# flips that to create_category 0.87 vs simplex 0.02. Authored categories carry
|
||
# their own description from the registry; this is the seed for the built-ins and
|
||
# the fallback for a legacy entry with no description.
|
||
CATEGORY_DESCRIPTIONS: dict[str, str] = {
|
||
"response": (
|
||
"Anything that is not a request to perform an action or produce a result: "
|
||
"greetings, thanks, acknowledgements, chit-chat, and off-topic statements."
|
||
),
|
||
"calendar": (
|
||
"Calendar and scheduling: reading or changing the user's calendar "
|
||
"events and reminders."
|
||
),
|
||
"simplex": (
|
||
"SimpleX messaging: reading incoming messages, sending messages to "
|
||
"contacts, and showing the user's contact link."
|
||
),
|
||
"time": (
|
||
"Time and date utilities: telling the current time or date, setting "
|
||
"countdown timers, and setting alarms."
|
||
),
|
||
}
|
||
|
||
|
||
def build_skills(config: dict) -> list[Skill]:
|
||
"""The hardcoded built-ins: internal behaviors only.
|
||
|
||
Real integrations do not live here — they are authored by the codegen
|
||
pipeline into `data/skills/` or shipped as seed skill packages under
|
||
`seeds/`. A fabricated built-in here shadows the authoring path for a real
|
||
one (navigation routes to it), so keep this list to behaviors that need no
|
||
external service.
|
||
|
||
The `response` category is a closed tree of canned replies (see
|
||
CANNED_CATEGORIES): it is the catchall for inputs that are not tasks, and it
|
||
never goes through codegen.
|
||
"""
|
||
return [
|
||
Skill(
|
||
name=f"response.{name}",
|
||
category="response",
|
||
description=description,
|
||
act=_canned_response(f"response.{name}", message),
|
||
category_description=CATEGORY_DESCRIPTIONS["response"],
|
||
)
|
||
for name, description, message in _CANNED_RESPONSES
|
||
]
|
||
|
||
|
||
def category_descriptions(tree: dict[str, list[Skill]]) -> dict[str, str]:
|
||
"""The description to offer for each category, best-effort.
|
||
|
||
An authored skill carries the registry description on its `Skill`
|
||
(merge_registry/merge_skill_store); a seed or a legacy entry with no
|
||
description falls back to `CATEGORY_DESCRIPTIONS`. An empty category bucket
|
||
(no skills to carry it) falls back too, so a category is never offered bare.
|
||
"""
|
||
merged: dict[str, str] = {}
|
||
for category, skills in tree.items():
|
||
for skill in skills:
|
||
if skill.category_description:
|
||
merged[category] = skill.category_description
|
||
break
|
||
for category in tree:
|
||
if not merged.get(category):
|
||
merged[category] = CATEGORY_DESCRIPTIONS.get(category, "")
|
||
return merged
|
||
|
||
|
||
|
||
def build_tree(skills: list[Skill]) -> dict[str, list[Skill]]:
|
||
tree: dict[str, list[Skill]] = {}
|
||
for skill in skills:
|
||
tree.setdefault(skill.category, []).append(skill)
|
||
for category in tree:
|
||
tree[category].sort(key=lambda s: s.name)
|
||
return tree
|
||
|
||
|
||
def merge_registry(tree: dict[str, list[Skill]], categories: dict[str, dict]) -> None:
|
||
"""Fold persisted categories and their skills into a running tree.
|
||
|
||
Category stubs become empty buckets; registered skills become stub leaves
|
||
(no-op bodies) so they are navigable and rerunnable immediately.
|
||
"""
|
||
for category, data in categories.items():
|
||
tree.setdefault(category, [])
|
||
description = str(data.get("description") or CATEGORY_DESCRIPTIONS.get(category, ""))
|
||
existing = {s.name for s in tree[category]}
|
||
for skill in data.get("skills", []):
|
||
name = skill.get("name")
|
||
if not name or name in existing:
|
||
continue
|
||
tree[category].append(
|
||
Skill(
|
||
name=name,
|
||
category=category,
|
||
description=skill.get("description", ""),
|
||
category_description=description,
|
||
)
|
||
)
|
||
existing.add(name)
|
||
|
||
|
||
def _navigate_canned(
|
||
engine: SemIfEngine,
|
||
log: DecisionLog,
|
||
trace: TraceLog,
|
||
request: Request,
|
||
category: str,
|
||
skills: list[Skill],
|
||
) -> Skill:
|
||
"""Pick one canned reply from a closed category. No create branch, ever.
|
||
|
||
The catchall (`RESPONSE_FALLBACK`) is offered last so the decision question
|
||
can point at it by name; it is the answer when nothing specific fits.
|
||
"""
|
||
ordered = [s for s in skills if not s.is_noop()] or list(skills)
|
||
fallback = next((s for s in ordered if s.name == RESPONSE_FALLBACK), None)
|
||
ordered = [s for s in ordered if s is not fallback] + ([fallback] if fallback else [])
|
||
if len(ordered) == 1:
|
||
return ordered[0]
|
||
leaf = DecisionRequest(
|
||
state=compose_state(
|
||
request,
|
||
additional_context=f"all choices are within the {category} category",
|
||
),
|
||
question=(
|
||
"Which canned response best fits this input? "
|
||
f"Choose {ordered[-1].name} if none of the others does."
|
||
),
|
||
options=[Option(s.name, s.description) for s in ordered],
|
||
)
|
||
result = engine.call(leaf)
|
||
log.append(leaf, result, extra={"phase": "navigate:response", "run_id": request.id})
|
||
chosen = next((s for s in ordered if s.name == result.selected), ordered[-1])
|
||
trace.append(
|
||
"response_selected",
|
||
request.id,
|
||
category=category,
|
||
skill=chosen.name,
|
||
selected=result.selected,
|
||
probs=result.probs,
|
||
)
|
||
return chosen
|
||
|
||
|
||
def navigate(
|
||
engine: SemIfEngine,
|
||
log: DecisionLog,
|
||
trace: TraceLog,
|
||
request: Request,
|
||
tree: dict[str, list[Skill]],
|
||
category_tau: float = 0.75,
|
||
action_tau: float = 0.5,
|
||
) -> Skill | CreateCategory | CreateSkill:
|
||
"""Descend the tree one SemIf choice per level. Every choice is logged.
|
||
|
||
The category level is **two-stage** (mirroring the leaf). The softmax offers
|
||
every existing category **with its description** plus a `create_category`
|
||
branch; the winner is then confirmed by `confirm_category_fit`, a name-anchored
|
||
scope check. A confirmed winner is descended into; a winner that does not
|
||
cover the request authors a new category. Bare category names carry no signal
|
||
— real runs sent "book a flight to japan" to the `simplex` bucket (0.49 vs
|
||
create_category 0.12) until the descriptions were added — so the descriptions
|
||
are load-bearing. A level with nothing to choose from (an empty tree)
|
||
short-circuits straight to create: SemIf decisions need at least two options.
|
||
|
||
The `response` catchall and `create_category` share a boundary arbitrated by
|
||
`confirm_non_action`, the **actionability guard** (`navigate:actionability`,
|
||
keyed by `navigation.action_tau`): the closed `response` tree is reached
|
||
**only** when the input is not a request (a greeting, thanks, acknowledgement,
|
||
small talk, or a stray statement). A request — even one no skill covers, e.g.
|
||
"what is 255 * 12?" — authors a skill instead, never a canned reply. This
|
||
runs whichever side of the boundary the softmax proposed, because a
|
||
description alone cannot draw the line: "1+1=2" (a statement) and
|
||
"what is 255 * 12?" (a request) both look like math to the category softmax.
|
||
|
||
The leaf level is two-stage too: navigation only ever picks among the
|
||
existing skills (`create_skill` is deliberately NOT in the softmax), and the
|
||
reuse-vs-create decision is the intent guard in dispatch
|
||
(`confirm_skill_fit`), keyed by `navigation.intent_tau`.
|
||
"""
|
||
categories = sorted(tree.keys())
|
||
descriptions = category_descriptions(tree)
|
||
create_category = Option(
|
||
"create_category",
|
||
"No existing category covers this request; a new top-level category is "
|
||
"needed.",
|
||
)
|
||
top = DecisionRequest(
|
||
state=compose_state(request),
|
||
question="Which top-level category handles this request?",
|
||
options=[Option(c, descriptions.get(c, c)) for c in categories]
|
||
+ [create_category],
|
||
)
|
||
if not categories:
|
||
trace.append(
|
||
"create_category",
|
||
request.id,
|
||
state=top.state,
|
||
question=top.question,
|
||
options=[o.id for o in top.options],
|
||
selected="create_category",
|
||
probs={},
|
||
)
|
||
return CreateCategory()
|
||
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 category not in CANNED_CATEGORIES:
|
||
if not confirm_category_fit(
|
||
engine, log, trace, request, category, descriptions.get(category, ""),
|
||
category_tau,
|
||
):
|
||
trace.append(
|
||
"category_scope_rejected",
|
||
request.id,
|
||
category=category,
|
||
probs=top_result.probs,
|
||
)
|
||
category = "create_category"
|
||
if category == "create_category" or category in CANNED_CATEGORIES:
|
||
# The actionability guard is the create door for the catchall: only
|
||
# non-request input gets a canned reply, so a request that merely fell
|
||
# through the softmax (or was pushed out by the reworded scope) authors.
|
||
if not confirm_non_action(engine, log, trace, request, action_tau) or not tree.get(
|
||
"response"
|
||
):
|
||
trace.append(
|
||
"create_category",
|
||
request.id,
|
||
state=top.state,
|
||
question=top.question,
|
||
options=[o.id for o in top.options],
|
||
selected=top_result.selected,
|
||
probs=top_result.probs,
|
||
)
|
||
return CreateCategory()
|
||
return _navigate_canned(engine, log, trace, request, "response", tree["response"])
|
||
skills = tree[category]
|
||
if not skills:
|
||
trace.append(
|
||
"skill_needed",
|
||
request.id,
|
||
category=category,
|
||
state=compose_state(
|
||
request,
|
||
additional_context=f"all choices are within the {category} category",
|
||
),
|
||
question="(this category has no skills yet)",
|
||
options=[],
|
||
selected="create_skill",
|
||
probs={},
|
||
)
|
||
return CreateSkill(category=category)
|
||
if len(skills) == 1:
|
||
# A one-option softmax is meaningless; hand the sole skill to the intent
|
||
# guard, which owns the reuse-vs-create decision.
|
||
return skills[0]
|
||
leaf = DecisionRequest(
|
||
state=compose_state(
|
||
request,
|
||
additional_context=f"all choices are within the {category} category",
|
||
),
|
||
question=f"Which {category} skill performs the action this request asks for?",
|
||
options=[Option(s.name, s.description) for s in skills],
|
||
)
|
||
leaf_result = engine.call(leaf)
|
||
log.append(leaf, leaf_result, extra={"phase": "navigate:leaf", "run_id": request.id})
|
||
pick = leaf_result.selected
|
||
return next((s for s in skills if s.name == pick), skills[0])
|
||
|
||
|
||
def confirm_category_fit(
|
||
engine: SemIfEngine,
|
||
log: DecisionLog,
|
||
trace: TraceLog,
|
||
request: Request,
|
||
category: str,
|
||
description: str,
|
||
tau: float = 0.75,
|
||
) -> bool:
|
||
"""Does this category's scope actually cover what the request asks for?
|
||
|
||
The category-level counterpart of `confirm_skill_fit`, and the create door
|
||
at that level: the described-category softmax proposes the best category,
|
||
and this name-anchored scope check confirms it. A rejected winner (or a
|
||
`create_category` win) authors a new category instead. The threshold is
|
||
`navigation.category_tau` (default 0.75 — measured so true non-tasks, which
|
||
score 0.87–0.999 on `response`, sit above it and a task misrouted into
|
||
`response` at 0.61 sits below).
|
||
|
||
Name-anchored on purpose: asking whether *the winner we picked* covers the
|
||
request is far cleaner than a global "does any category cover this?" — the
|
||
global wording collapsed in-scope and out-of-scope requests into one band
|
||
(0.47-0.98) and over-covered a non-task ("thank pepper ..."). This compares
|
||
one category's stated scope to the request. The check is deliberately
|
||
permissive: with real descriptions the in-scope cases score 0.9-1.0, so a
|
||
borderline over-cover routes into a plausible category and the leaf intent
|
||
guard catches it (a skill is only authored if no existing leaf matches).
|
||
"""
|
||
decision = DecisionRequest(
|
||
state=(
|
||
f"Request: {request.text}\n"
|
||
f"Category: {category}\n"
|
||
f"Scope: {description}"
|
||
),
|
||
question=f"Does the scope of the {category} category cover this request?",
|
||
options=[
|
||
Option("covers", "Yes, this category covers the request."),
|
||
Option(
|
||
"none",
|
||
"No, this category does not cover the request; a new category is needed.",
|
||
),
|
||
],
|
||
)
|
||
result = engine.call(decision)
|
||
log.append(
|
||
decision,
|
||
result,
|
||
extra={
|
||
"phase": "navigate:category_scope",
|
||
"run_id": request.id,
|
||
"category": category,
|
||
},
|
||
)
|
||
fits = result.prob("covers") >= tau
|
||
trace.append(
|
||
"category_scope",
|
||
request.id,
|
||
category=category,
|
||
selected=result.selected,
|
||
probs=result.probs,
|
||
fits=fits,
|
||
)
|
||
return fits
|
||
|
||
|
||
def confirm_non_action(
|
||
engine: SemIfEngine,
|
||
log: DecisionLog,
|
||
trace: TraceLog,
|
||
request: Request,
|
||
tau: float = 0.5,
|
||
) -> bool:
|
||
"""Is this input non-request (chit-chat/statement) rather than a request?
|
||
|
||
The actionability guard and the create door shared by the `response` catchall
|
||
and `create_category`: it arbitrates whichever of the two the category softmax
|
||
proposed. A request — even one no skill covers — must author a skill; only
|
||
input that asks for nothing gets a canned reply. This is what keeps
|
||
"what is 255 * 12?" (a request) out of the closed `response` tree while
|
||
"1+1=2" (a statement) stays in it — a line no single category description can
|
||
draw, because both read as math to the softmax. Returns True when the input is
|
||
non-action (route to `response`); the threshold is `navigation.action_tau`
|
||
(default 0.5, deliberately separate from the other taus so tuning
|
||
actionability moves nothing else).
|
||
"""
|
||
decision = DecisionRequest(
|
||
state=request.text,
|
||
question=(
|
||
"Is this input a request to the agent (it asks for an answer, result, "
|
||
"or task), or is it non-request input (a greeting, thanks, "
|
||
"acknowledgement, small talk, or a statement)?"
|
||
),
|
||
options=[
|
||
Option(
|
||
"action",
|
||
"It is a request: the user asks the agent for an answer, a result, "
|
||
"or an action (including one the agent has no skill for yet).",
|
||
),
|
||
Option(
|
||
"non_action",
|
||
"It is not a request: a greeting, thanks, acknowledgement, small "
|
||
"talk, or a statement.",
|
||
),
|
||
],
|
||
)
|
||
result = engine.call(decision)
|
||
log.append(
|
||
decision,
|
||
result,
|
||
extra={"phase": "navigate:actionability", "run_id": request.id},
|
||
)
|
||
non_action = result.prob("non_action") >= tau
|
||
trace.append(
|
||
"actionability",
|
||
request.id,
|
||
selected=result.selected,
|
||
probs=result.probs,
|
||
non_action=non_action,
|
||
)
|
||
return non_action
|
||
|
||
|
||
def confirm_skill_fit(
|
||
engine: SemIfEngine,
|
||
log: DecisionLog,
|
||
trace: TraceLog,
|
||
request: Request,
|
||
skill: Skill,
|
||
tau: float = 0.6,
|
||
) -> bool:
|
||
"""Does this skill's action actually match what the request asks for?
|
||
|
||
Navigation picks the closest existing leaf; this guard is now the **sole**
|
||
leaf-level reuse-vs-create decision (create_skill is no longer an option in
|
||
the leaf softmax). If the skill's action is not the same action the request
|
||
asks for, dispatch authors a new leaf instead. The threshold is
|
||
`navigation.intent_tau` (deliberately separate from the run-assessment
|
||
`tau`, so tuning reuse-vs-create does not move assessment/fidelity).
|
||
|
||
Wording tuned against the real model: comparing the two *actions*
|
||
("different action") is unambiguous where "can this serve the request" was
|
||
not — e.g. "message Sam" (an implicit send in instant-messaging syntax)
|
||
reads as a different action from "read the next message". Do not add an
|
||
instant-messaging context hint: it over-fires and pulls read phrasings to
|
||
the send side. The comparison is over-permissive on near-synonyms (read vs
|
||
send score "same" ~0.74-0.78), so it is paired with the softmax that
|
||
disambiguates them — the guard only ever sees the softmax winner.
|
||
"""
|
||
decision = DecisionRequest(
|
||
state=(
|
||
f"Requested action: {request.text}\n"
|
||
f"Action of the existing skill: {skill.description}"
|
||
),
|
||
question="Compare the requested action and the skill's action. Are they the same action?",
|
||
options=[
|
||
Option("same", "Same action."),
|
||
Option(
|
||
"different",
|
||
"Different action: the skill does not do what the user asks.",
|
||
),
|
||
],
|
||
)
|
||
result = engine.call(decision)
|
||
log.append(
|
||
decision,
|
||
result,
|
||
extra={
|
||
"phase": "navigate:intent",
|
||
"run_id": request.id,
|
||
"skill": f"{skill.category}.{skill.name}",
|
||
},
|
||
)
|
||
fits = result.prob("same") >= tau
|
||
trace.append(
|
||
"intent_guard",
|
||
request.id,
|
||
category=skill.category,
|
||
skill=skill.name,
|
||
selected=result.selected,
|
||
probs=result.probs,
|
||
fits=fits,
|
||
)
|
||
return fits
|
||
|
||
|
||
def tree_summary(tree: dict[str, list[Skill]]) -> str:
|
||
lines = []
|
||
for category in sorted(tree):
|
||
names = ", ".join(s.name for s in tree[category])
|
||
lines.append(f" {category}: {names}")
|
||
return "\n".join(lines)
|
||
|
||
|
||
def build_category_prompt(request: Request, tree: dict[str, list[Skill]]) -> list[dict]:
|
||
"""Chat messages for the decision model used as the category author.
|
||
|
||
The category must be a general bucket that many tools could fit under, not
|
||
a single skill. The existing tree is included so the model avoids duplicating
|
||
categories and stays broad enough to be useful.
|
||
"""
|
||
system = (
|
||
"You are the skill-tree authoring step of a local agent. A request did "
|
||
"not fit any existing category. Propose one new top-level category of "
|
||
"tools/skills that would encompass this request. It must be broad enough "
|
||
"that many tools could fit under it — a general-purpose bucket, not a "
|
||
"single skill. Reply with JSON only: "
|
||
'{"title": "<short lowercase snake_case id, no spaces>", '
|
||
'"description": "<one to two sentence purpose>"}'
|
||
)
|
||
user = (
|
||
f"Request: {request.text}\n"
|
||
f"Existing categories and their skills:\n{tree_summary(tree)}\n"
|
||
"Proposed new category (JSON only):"
|
||
)
|
||
return [
|
||
{"role": "system", "content": system},
|
||
{"role": "user", "content": user},
|
||
]
|
||
|
||
|
||
def parse_category_draft(raw: str) -> CategoryDraft:
|
||
"""Parse the model's JSON reply into a CategoryDraft."""
|
||
parsed = LLMClient._parse_json(raw)
|
||
title = str(parsed.get("title", "")).strip()
|
||
description = str(parsed.get("description", "")).strip()
|
||
if not title or not description:
|
||
raise ValueError(f"category draft missing title/description: {raw!r}")
|
||
name = re.sub(r"\s+", "_", title.lower())
|
||
if not name.replace("_", "").isalnum():
|
||
raise ValueError(f"category title must be snake_case alnum: {title!r}")
|
||
return CategoryDraft(name=name, description=description)
|
||
|
||
|
||
def generate_category(
|
||
client: LLMClient, request: Request, tree: dict[str, list[Skill]]
|
||
) -> CategoryDraft:
|
||
"""Author a new category stub with the `llm` provider."""
|
||
raw = client.chat(build_category_prompt(request, tree), max_tokens=128)
|
||
return parse_category_draft(raw)
|
||
|
||
|
||
def build_skill_prompt(
|
||
request: Request, category: str, tree: dict[str, list[Skill]]
|
||
) -> list[dict]:
|
||
"""Chat messages for the decision model used as the skill author.
|
||
|
||
The skill must be one specific, single-purpose action that fits inside the
|
||
given category — not a broad bucket. Existing skills in the category are
|
||
included so the model avoids duplicating them.
|
||
"""
|
||
system = (
|
||
"You are the skill-tree authoring step of a local agent. A request inside "
|
||
f"the '{category}' category did not fit any existing skill. Propose ONE "
|
||
"new skill for this category: a specific, single-purpose action the agent "
|
||
"can take. Reply with JSON only: "
|
||
'{"title": "<short lowercase snake_case id, no spaces, generic and reusable>", '
|
||
'"description": "<one to two sentence description of how it benefits user>"}'
|
||
)
|
||
existing = ", ".join(s.name for s in tree.get(category, [])) or "(none)"
|
||
user = (
|
||
f"Request: {request.text}\n"
|
||
f"Category: {category}\n"
|
||
f"Existing skills in this category: {existing}\n"
|
||
"Proposed new skill (JSON only):"
|
||
)
|
||
return [
|
||
{"role": "system", "content": system},
|
||
{"role": "user", "content": user},
|
||
]
|
||
|
||
|
||
def parse_skill_draft(raw: str) -> SkillDraft:
|
||
"""Parse the model's JSON reply into a SkillDraft."""
|
||
parsed = LLMClient._parse_json(raw)
|
||
title = str(parsed.get("title", "")).strip()
|
||
description = str(parsed.get("description", "")).strip()
|
||
if not title or not description:
|
||
raise ValueError(f"skill draft missing title/description: {raw!r}")
|
||
name = re.sub(r"\s+", "_", title.lower())
|
||
if not re.fullmatch(r"[a-z0-9_]+(?:\.[a-z0-9_]+)*", name):
|
||
raise ValueError(f"skill title must be snake_case alnum (dots allowed): {title!r}")
|
||
return SkillDraft(name=name, description=description)
|
||
|
||
|
||
def generate_skill(
|
||
client: LLMClient, request: Request, category: str, tree: dict[str, list[Skill]]
|
||
) -> SkillDraft:
|
||
"""Author a new skill leaf stub with the `llm` provider."""
|
||
raw = client.chat(build_skill_prompt(request, category, tree), max_tokens=128)
|
||
return parse_skill_draft(raw)
|