Make create_skill author a skill leaf stub like create_category

This commit is contained in:
Denton Social
2026-09-23 23:02:59 -05:00
parent f5419183bc
commit 225b4df959
6 changed files with 261 additions and 34 deletions
+43 -6
View File
@@ -25,6 +25,8 @@ from .skills import (
build_tree,
compose_state,
generate_category,
generate_skill,
merge_registry,
navigate,
)
from .trace import TraceLog
@@ -80,8 +82,7 @@ class Scheduler:
self.skills = build_skills(config)
self.tree = build_tree(self.skills)
self.registry = CategoryRegistry(config.get("category_registry", "data/categories.json"))
for category in self.registry.read():
self.tree.setdefault(category, [])
merge_registry(self.tree, self.registry.read())
self.ctx = ActionContext(engine=self.engine, config=config)
self.runner = SkillRunner(self.ctx, self.llm, self.log)
self.current: Process | None = None
@@ -208,10 +209,7 @@ class Scheduler:
if isinstance(navigation, CreateCategory):
return self._create_category(request)
if isinstance(navigation, CreateSkill):
return DispatchResult(
kind="create_skill",
summary="skill authoring via opencode is deferred to v2; suggestion logged.",
)
return self._create_skill(request, navigation.category)
outcome = self.runner.run(navigation, request)
if outcome.error:
self.trace.append(
@@ -270,6 +268,45 @@ class Scheduler:
skill=draft.name,
)
def _create_skill(self, request: Request, category: str) -> DispatchResult:
"""Author a new skill leaf stub with the decision model in generation mode."""
from .engine import EngineUnavailable
try:
draft = generate_skill(self.engine, request, category, self.tree)
except (EngineUnavailable, ValueError) as exc:
self.trace.append("error", request.id, phase="create_skill", message=str(exc))
return DispatchResult(kind="error", summary=f"create_skill failed: {exc}")
existing = {s.name for s in self.tree.get(category, [])}
if draft.name in existing:
self.trace.append(
"error",
request.id,
phase="create_skill",
category=category,
message=f"skill {draft.name} already exists",
)
return DispatchResult(
kind="error",
summary=f"create_skill failed: {draft.name} already exists",
)
self.registry.register_skill(category, draft.name, draft.description)
self.tree.setdefault(category, []).append(
Skill(name=draft.name, category=category, description=draft.description)
)
self.trace.append(
"skill_created",
request.id,
category=category,
skill=draft.name,
description=draft.description,
)
return DispatchResult(
kind="create_skill",
summary=f"created skill {category}.{draft.name}: {draft.description}",
skill=draft.name,
)
def status(self) -> str:
lines = []
current = f"{self.current.skill} ({self.current.request.id})" if self.current else "idle"
+105 -12
View File
@@ -2,10 +2,10 @@
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. create_category is live: the decision model is driven in
normal generation mode to propose a title + description for a broad new
category, which is persisted to a category registry and becomes a stub in the
tree. create_skill is still a stub that logs a suggestion event (deferred).
"create_skill" branch. Both are live: the decision model is driven in normal
generation mode to propose 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.
"""
@@ -64,8 +64,9 @@ class Skill:
class CreateSkill:
"""Suggestion that the current category needs a new skill.
Navigation logs the suggestion event to the trace; actual authoring is
deferred to v2. `category` names the category that needs the new skill.
Handled live, like CreateCategory: the decision model authors the new skill
stub, which is persisted and merged into the tree. `category` names the
category that needs the new skill.
"""
category: str
@@ -88,12 +89,20 @@ class CategoryDraft:
description: str
@dataclass
class SkillDraft:
"""An authored skill leaf stub: one specific action within a category."""
name: str
description: str
class CategoryRegistry:
"""Persisted category stubs, one file on disk.
Format: {name: {"description": str, "skills": []}}. The empty skills list is
the slot that create_skill will fill later; for now a stub category has no
leaves.
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"):
@@ -110,6 +119,16 @@ class CategoryRegistry:
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) -> None:
"""Add a skill leaf to a category, creating the category entry if needed."""
categories = self.read()
entry = categories.setdefault(category, {"description": "", "skills": []})
skills = entry.setdefault("skills", [])
if not any(s.get("name") == name for s in skills):
skills.append({"name": name, "description": description})
self.path.parent.mkdir(parents=True, exist_ok=True)
self.path.write_text(json.dumps(categories, indent=2) + "\n")
def compose_state(request: Request, current: str | None = None) -> str:
parts = [request.text]
@@ -209,6 +228,29 @@ def build_tree(skills: list[Skill]) -> dict[str, list[Skill]]:
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, [])
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", ""),
)
)
existing.add(name)
def navigate(
engine: SemIfEngine,
log: DecisionLog,
@@ -218,9 +260,9 @@ def navigate(
) -> Skill | CreateCategory | CreateSkill:
"""Descend the tree one SemIf choice per level. Every choice is logged.
The category level offers a "create_category" branch (handled live by
dispatch) and the leaf level a "create_skill" branch (still a stub); both
log a suggestion event to the trace.
The category level offers a "create_category" branch and the leaf level a
"create_skill" branch; both are handled live by dispatch and log a
suggestion event to the trace.
"""
categories = sorted(tree.keys())
create_category = Option("create_category", "Suggest a new category for this.")
@@ -322,3 +364,54 @@ def generate_category(
"""Author a new category stub with the decision model in generation mode."""
raw = engine.generate(build_category_prompt(request, tree))
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>", '
'"description": "<one to two sentence purpose>"}'
)
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 name.replace("_", "").isalnum():
raise ValueError(f"skill title must be snake_case alnum: {title!r}")
return SkillDraft(name=name, description=description)
def generate_skill(
engine: SemIfEngine, request: Request, category: str, tree: dict[str, list[Skill]]
) -> SkillDraft:
"""Author a new skill leaf stub with the decision model in generation mode."""
raw = engine.generate(build_skill_prompt(request, category, tree))
return parse_skill_draft(raw)