"""Pure-stdlib tests for skill-tree authoring: category and skill prompts, draft parsing, the category registry, the async codegen-body workflow, and the error path when the engine is unavailable. No mocking: the async write test uses a real CodegenClient against a throwaway stdlib HTTP server (real endpoint, per the repo rule); engine-dependent success paths are exercised only by the integration tests against the real decision model. """ import json import threading import time from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path import pytest from semif_agent.cli import REPO_ROOT, load_config from semif_agent.codegen import CodegenClient from semif_agent.decisions import Request from semif_agent.engine import EngineConfig, SemIfEngine from semif_agent.llm import LLMClient, LLMError from semif_agent.log import DecisionLog from semif_agent.skill import RunResult from semif_agent.scheduler import ( MIN_ARTIFACT_TOKENS, DraftAuthor, PendingQuestion, RepairOffer, Scheduler, SchedulerReply, SkillWrite, artifact_token_budget, ) from semif_agent.skills import ( ActionResult, CategoryDraft, CategoryRegistry, CreateCategory, CreateSkill, Skill, SkillCatalog, SkillDraft, SkillStore, build_category_prompt, build_skill_prompt, build_skills, build_tree, confirm_category_fit, confirm_non_action, confirm_skill_fit, category_descriptions, CATEGORY_DESCRIPTIONS, compose_state, generate_category, generate_skill_spec, last_result, merge_registry, merge_seed_store, merge_skill_store, navigate, parse_category_draft, parse_skill_draft, resolve_skill_config, unresolved_variables, ) from semif_agent.trace import TraceLog from tests.conftest import ScriptedEngine GOOD_BODY = """\ from semif_agent.skills import ActionResult INTEGRATION = { "service": "probe_service", "transport": "compute", "config_vars": [], } CONTRACT = {} def act(ctx, request): return ActionResult(action_log="probe ran", new_state=request.text) """ GOOD_TEST = """\ import sys print("ok") sys.exit(0) """ def _pipeline_codegen_server(replies: list) -> tuple[ThreadingHTTPServer, str]: """Sequenced OpenAI-compatible SSE server for the full authoring pipeline: codegen body -> data contract -> test. Also answers `/api/show` so the client's context-window probe succeeds (never counted).""" class Handler(BaseHTTPRequestHandler): received: list = [] chat_calls: int = 0 def do_POST(self): length = int(self.headers.get("Content-Length") or 0) raw = self.rfile.read(length).decode("utf-8") if self.path.endswith("/api/show"): body = json.dumps({"parameters": {"num_ctx": 32768}}).encode("utf-8") self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) return type(self).chat_calls += 1 type(self).received.append(json.loads(raw)) index = min(type(self).chat_calls - 1, len(replies) - 1) reply = replies[index] frame = "data: " + json.dumps( {"choices": [{"delta": {"content": reply}}]} ) + "\n\n" body = (frame + "data: [DONE]\n\n").encode("utf-8") self.send_response(200) self.send_header("Content-Type", "text/event-stream") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def log_message(self, format, *args): pass handler = type("Handler", (Handler,), {"received": [], "chat_calls": 0}) httpd = ThreadingHTTPServer(("127.0.0.1", 0), handler) threading.Thread(target=httpd.serve_forever, daemon=True).start() return httpd, f"http://127.0.0.1:{httpd.server_address[1]}/v1" def _scheduler(tmp_path, codegen=None, choices=None, default="success", verbosity=None, recall_window=None): """A scheduler driven by the deterministic scripted engine (conftest) so scheduling/authoring mechanics are testable without a GGUF. Assessment is a SemIf decision now, so a run needs the engine to answer `assess:*`.""" config = { "skills": {}, # Most mechanics tests exercise the authoring path, which is an opt-in # now that new-skill creation is denied by default. "new_skill_creation": "allow", "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), } if verbosity is not None: config["verbosity"] = verbosity if recall_window is not None: config["query_recall_window"] = recall_window return Scheduler( engine=ScriptedEngine(choices=choices, default=default), llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config=config, trace=TraceLog(str(tmp_path / "runs.jsonl")), codegen=codegen, ) def test_artifact_token_budget_derives_overrides_and_floors(): """The artifact ceiling is the engine context minus the reserve; a positive config value overrides it; a hard floor keeps it usable.""" assert artifact_token_budget(0, 32768, 1024) == 32768 - 1024 assert artifact_token_budget(None, 4096, 1024) == 4096 - 1024 assert artifact_token_budget(2000, 32768, 1024) == 2000 assert artifact_token_budget(0, 100, 1024) == MIN_ARTIFACT_TOKENS def test_scheduler_sizes_artifact_budgets_from_engine_context(tmp_path): """Body/test char ceilings are derived from the active engine's context.""" scheduler = _scheduler(tmp_path) # ScriptedEngine exposes no context_tokens -> the conservative 4096 default. assert scheduler.max_body_tokens == 4096 - 1024 assert scheduler.max_body_chars == int((4096 - 1024) * 4.0) assert scheduler.max_test_chars == scheduler.max_body_chars def test_skill_catalog_lists_categories_and_skills(): """The read-only catalog reports the tree's categories and one category's leaves, and follows the live provider.""" tree = { "time": [ Skill(name="now", category="time", description="time"), Skill(name="date", category="time", description="date"), ], "calculator": [Skill(name="calculate", category="calculator", description="math")], } catalog = SkillCatalog(lambda: tree) assert set(catalog.categories()) == {"time", "calculator"} assert catalog.skills("time") == ["date", "now"] assert catalog.skills("missing") == [] # A skill added after construction is still listed (the provider is live). tree["new"] = [Skill(name="thing", category="new", description="d")] assert "new" in catalog.categories() def test_scheduler_exposes_a_read_only_catalog(tmp_path): """The scheduler supplies the catalog on the run context, but reserves `admin` for the built-in housekeeping meta skills.""" scheduler = _scheduler(tmp_path) assert scheduler.catalog is not None categories = scheduler.catalog.categories() assert "housekeeping" in categories and "response" in categories assert "delete_skill" in scheduler.catalog.skills("housekeeping") # An ordinary body sees the catalog but not the mutating admin surface. housekeeping = next( s for s in scheduler.tree["housekeeping"] if s.name == "delete_skill" ) ordinary = Skill( name="probe", category="tracking", description="Probe.", act=lambda ctx, request: ActionResult(action_log="ok", new_state="x"), ) house_ctx = scheduler.runner._skill_context(housekeeping, Request("x")) plain_ctx = scheduler.runner._skill_context(ordinary, Request("x")) assert house_ctx.admin is scheduler assert plain_ctx.admin is None assert plain_ctx.catalog is scheduler.catalog # The scheduler's speech channel rides on every run context (a skill can # post a long-form read-aloud for a voice front end). assert plain_ctx.speech is scheduler.speech def test_a_run_that_registers_a_read_is_terminal(tmp_path): """A run that hands off to a long-form read is the answer: a `continue` assessment must not re-dispatch it (which authored a spurious skill).""" scheduler = _scheduler( tmp_path, choices={"additional work to be done": "continue"} ) def act(ctx, request): ctx.speech.read_aloud( path="/b/x.txt", key="k", bookmarks_path="/b/bookmarks.json", run_id=request.id, source=request.source, ) return ActionResult(action_log="Reading X aloud.", new_state="ok") skill = Skill(name="read_book", category="books", description="Read.", act=act) scheduler.tree.setdefault("books", []).append(skill) result = scheduler._run_skill(skill, Request("read x", source="voice:c")) assert result.intermediate is False, "a read-registering run must not continue" kinds = [entry["kind"] for entry in scheduler.trace.read()] assert "read_aloud_terminal" in kinds assert "continued" not in kinds def test_fidelity_gate_state_is_whole_body_without_the_request(tmp_path): """The gate ingests the whole body plus the inventory and goal, and never the raw user request (the truncation bug and the PII path, both fixed).""" scheduler = _scheduler( tmp_path, codegen=CodegenClient(base_url="http://127.0.0.1:1/v1", model="test", timeout=1), ) body = '''\ INTEGRATION = {"service": "nextcloud", "transport": "http", "config_vars": ["nextcloud_bridge_url"]} CONTRACT = {"nextcloud_bridge_url": "Bridge URL."} import urllib.request from semif_agent.skills import ActionResult def _bridge(ctx, method, path, payload=None): return urllib.request.urlopen(ctx.config["nextcloud_bridge_url"] + path) def act(ctx, request): _bridge(ctx, "POST", "/tasks/complete", payload={"uid": "1"}) return ActionResult(action_log="done", new_state="done") ''' job = SkillWrite( request=Request("mark task Eat hot dogs as completed"), category="nextcloud", draft=SkillDraft( name="update_task_status", description="Marks a task completed.", goal="Update a task's status to completed.", ), ) scheduler._fidelity_gate(job, body) state = scheduler.engine.calls[-1].state assert "/tasks/complete" in state, "the whole body must be present" assert "POST /tasks/complete" in state, "the inventory must name the action" assert "observed operations" in state assert "Update a task's status to completed." in state assert "Eat hot dogs" not in state, "the raw request must not reach the gate" def test_action_result_needs_input_defaults_none(): assert ActionResult("log", "state").needs_input is None def test_run_summary_is_deterministic_and_surfaces_action_log(tmp_path): """The summary is computed, not generated: same inputs, same string. It names the category and skill, the ok/failed flag from the SemIf assessment, and the skill's real action log. No LLM prose is involved. """ scheduler = _scheduler(tmp_path) def act(ctx, request): return ActionResult( action_log="Next event on 'personal': Team sync — 2026-09-27 10:00 CEST", new_state="next event reported", ) skill = Skill( name="next_event", category="nextcloud", description="Report the next event.", act=act, ) result = scheduler._run_skill(skill, Request("what is my next event?")) assert result.kind == "ran" expected = ( "nextcloud.next_event: ok — Next event on 'personal': Team sync — 2026-09-27 10:00 CEST" ) assessed = [e for e in scheduler.trace.read() if e["kind"] == "assessed"] assert assessed, "the run must be traced" assert assessed[-1]["summary"] == expected assert assessed[-1]["assessment_summary"] == expected def test_run_summary_failure_uses_new_state_when_no_action_log(tmp_path): """A failed run with an empty action log falls back to the new state.""" scheduler = _scheduler( tmp_path, choices={"additional work to be done": "failed"} ) def act(ctx, request): return ActionResult(action_log="", new_state="service unreachable") skill = Skill(name="probe", category="tracking", description="Probe.", act=act) scheduler._run_skill(skill, Request("probe it")) assessed = [e for e in scheduler.trace.read() if e["kind"] == "assessed"] assert assessed[-1]["success"] is False assert assessed[-1]["outcome"] == "failed" assert assessed[-1]["summary"] == "tracking.probe: failed — service unreachable" def test_build_category_prompt_contains_request_and_tree(): tree = build_tree(build_skills({"skills": {}})) messages = build_category_prompt(Request("tracking for my drone delivery"), tree) assert messages[0]["role"] == "system" assert "category" in messages[0]["content"] joined = messages[1]["content"] assert "tracking for my drone delivery" in joined assert "response: response.acknowledge" in joined def test_build_skills_are_canned_responses_only(): """Real integrations are generated or seeded, never fabricated here: a hardcoded fake shadows the authoring path for a real skill (navigation routes to it). The only built-ins are the closed `response` canned tree.""" skills = build_skills({"skills": {}}) names = [skill.name for skill in skills] assert names and all(name.startswith("response.") for name in names) assert "response.clarify" in names assert all(skill.category == "response" for skill in skills) assert not any(skill.is_noop() for skill in skills) def test_parse_category_draft_plain_json(): draft = parse_category_draft( '{"title": "delivery", "description": "Track and manage package deliveries."}' ) assert draft.name == "delivery" assert "package" in draft.description def test_parse_category_draft_json_in_prose(): draft = parse_category_draft( 'Sure! Here you go:\n{"title": "home_automation", "description": "Control ' 'lights, locks, and appliances around the house."}\nHope that helps.' ) assert draft.name == "home_automation" def test_parse_category_draft_normalizes_title(): draft = parse_category_draft( '{"title": "Home Automation", "description": "Control household devices."}' ) assert draft.name == "home_automation" def test_parse_category_draft_missing_fields_raises(): with pytest.raises(ValueError): parse_category_draft('{"title": "only_title"}') with pytest.raises(ValueError): parse_category_draft("not json at all") def test_parse_category_draft_rejects_unclean_title(): with pytest.raises(ValueError): parse_category_draft('{"title": "ca$h!", "description": "nope"}') def test_category_registry_roundtrip(tmp_path): registry = CategoryRegistry(str(tmp_path / "categories.json")) assert registry.read() == {} registry.register("delivery", "Track and manage package deliveries.") registry.register("delivery", "Track and manage package deliveries, re-registered.") loaded = registry.read() assert set(loaded) == {"delivery"} assert loaded["delivery"]["description"] == ( "Track and manage package deliveries, re-registered." ) assert loaded["delivery"]["skills"] == [] def test_generate_category_without_endpoint_raises(): client = LLMClient(base_url="http://127.0.0.1:1/v1", model="test", timeout=2) with pytest.raises(LLMError): generate_category(client, Request("anything"), {}) def test_build_tree_includes_registry_stubs(tmp_path): registry = CategoryRegistry(str(tmp_path / "categories.json")) registry.register("delivery", "Track and manage package deliveries.") tree = build_tree(build_skills({"skills": {}})) merge_registry(tree, registry.read()) assert tree["delivery"] == [] def test_build_skill_prompt_asks_for_a_deidentified_goal(): tree = build_tree(build_skills({"skills": {}})) tree["tracking"] = [ Skill( name="tracking.check", category="tracking", description="Check the delivery status of a package.", ) ] messages = build_skill_prompt(Request("tracking for my drone delivery"), "tracking", tree) assert messages[0]["role"] == "system" assert "tracking" in messages[0]["content"] system = messages[0]["content"] assert "DE-IDENTIFIED" in system assert '"goal"' in system joined = messages[1]["content"] assert "tracking for my drone delivery" in joined assert "tracking.check" in joined def test_parse_skill_draft_plain_json(): draft = parse_skill_draft( '{"title": "track_live", "description": "Follow a package in real time."}' ) assert draft.name == "track_live" assert "real time" in draft.description # No goal supplied: it degrades to the description (both are de-identified). assert draft.goal == draft.description def test_parse_skill_draft_reads_goal_and_integration(): draft = parse_skill_draft( '{"title": "track_live", "description": "Follow a package.", ' '"goal": "poll a tracking endpoint and report status", ' '"service": "tracking", "transport": "http"}' ) assert draft.goal == "poll a tracking endpoint and report status" assert draft.integration == {"service": "tracking", "transport": "http"} def test_parse_skill_draft_drops_bad_integration_hint(): draft = parse_skill_draft( '{"title": "track_live", "description": "Follow a package.", ' '"service": "Nope!", "transport": "smtp"}' ) assert draft.integration == {} def test_parse_skill_draft_json_in_prose(): draft = parse_skill_draft( 'Here you go:\n{"title": "resend_email", "description": "Re-send a failed ' 'email draft."}\nHope that helps.' ) assert draft.name == "resend_email" def test_parse_skill_draft_normalizes_title(): draft = parse_skill_draft( '{"title": "Live Tracking", "description": "Follow a package in real time."}' ) assert draft.name == "live_tracking" def test_parse_skill_draft_allows_dotted_name(): draft = parse_skill_draft( '{"title": "tracking.status_lookup", "description": "Look up a package status."}' ) assert draft.name == "tracking.status_lookup" def test_parse_skill_draft_missing_fields_raises(): with pytest.raises(ValueError): parse_skill_draft('{"title": "only_title"}') with pytest.raises(ValueError): parse_skill_draft("not json at all") def test_parse_skill_draft_rejects_unclean_title(): with pytest.raises(ValueError): parse_skill_draft('{"title": "ca$h!", "description": "nope"}') def test_generate_skill_without_endpoint_raises(): client = LLMClient(base_url="http://127.0.0.1:1/v1", model="test", timeout=2) with pytest.raises(LLMError): generate_skill_spec(client, Request("anything"), "tracking", {}) def test_category_registry_register_skill(tmp_path): registry = CategoryRegistry(str(tmp_path / "categories.json")) registry.register("delivery", "Track and manage package deliveries.") registry.register_skill("delivery", "track_live", "Follow a package in real time.") registry.register_skill("delivery", "track_live", "Duplicate, ignored.") registry.register_skill("brand_new", "ping", "Probe the service.") loaded = registry.read() assert loaded["delivery"]["skills"] == [ { "name": "track_live", "description": "Follow a package in real time.", "goal": "", } ] assert loaded["brand_new"] == { "description": "", "skills": [{"name": "ping", "description": "Probe the service.", "goal": ""}], } def test_category_registry_register_skill_stores_goal_not_request(tmp_path): registry = CategoryRegistry(str(tmp_path / "categories.json")) registry.register_skill( "delivery", "track_live", "Follow a package in real time.", goal="poll a parcel tracking endpoint and report status", ) loaded = registry.read() row = loaded["delivery"]["skills"][0] assert row["goal"] == "poll a parcel tracking endpoint and report status" # The originating request and any user detail are never persisted. assert "request_text" not in row assert "requirements" not in row def test_merge_registry_loads_categories_and_skills(tmp_path): registry = CategoryRegistry(str(tmp_path / "categories.json")) registry.register_skill("delivery", "track_live", "Follow a package in real time.") tree = build_tree(build_skills({"skills": {}})) merge_registry(tree, registry.read()) names = [s.name for s in tree["delivery"]] assert names == ["track_live"] assert tree["delivery"][0].category == "delivery" def test_category_descriptions_never_bare_and_seeded(tmp_path): """Every category must carry a usable description at the softmax — a bare name carries no signal. Built-ins come from CATEGORY_DESCRIPTIONS; an authored category carries its registry description; a legacy/empty entry falls back to the built-in or empty string without crashing.""" tree = build_tree(build_skills({"skills": {}})) assert tree["response"][0].category_description # built-in seed registry = CategoryRegistry(str(tmp_path / "categories.json")) registry.register("travel", "Booking flights, hotels, and trips.") registry.register_skill("travel", "book_flight", "Book a flight.") merge_registry(tree, registry.read()) assert tree["travel"][0].category_description == "Booking flights, hotels, and trips." descriptions = category_descriptions(tree) assert descriptions["response"] == CATEGORY_DESCRIPTIONS["response"] assert descriptions["travel"] == "Booking flights, hotels, and trips." # an empty bucket (no skill to carry the description) still resolves to text tree["orphan"] = [] assert category_descriptions(tree)["orphan"] == "" def _write_seed(seed_store, category, name, description=""): directory = seed_store.dir(category, name) directory.mkdir(parents=True, exist_ok=True) (directory / "skill.py").write_text(GOOD_BODY) (directory / "contract.json").write_text(json.dumps({"probe_url": "Where to probe."})) if description: (directory / "manifest.json").write_text(json.dumps({"description": description})) def test_merge_seed_store_loads_skill_with_runtime_config(tmp_path): """A seed is runnable from the committed folder; recorded config lives in the runtime store, never in the seed folder.""" seed_store = SkillStore(str(tmp_path / "seeds")) config_store = SkillStore(str(tmp_path / "skills")) _write_seed(seed_store, "nextcloud", "probe", description="Probe a service.") config_store.write_config("nextcloud", "probe", {"probe_url": "https://example.test"}) tree = build_tree(build_skills({"skills": {}})) loaded = merge_seed_store(tree, seed_store, config_store) assert loaded == 1 skill = tree["nextcloud"][0] assert skill.status == "ready" assert skill.description == "Probe a service." assert skill.contract == {"probe_url": "Where to probe."} assert skill.config == {"probe_url": "https://example.test"} assert skill.integration_source == "declared" def test_merge_seed_store_falls_back_to_name_and_body_store_wins(tmp_path): seed_store = SkillStore(str(tmp_path / "seeds")) body_store = SkillStore(str(tmp_path / "skills")) _write_seed(seed_store, "nextcloud", "probe") tree = build_tree(build_skills({"skills": {}})) merge_seed_store(tree, seed_store, body_store) assert tree["nextcloud"][0].description == "probe" body_store.write_body("nextcloud", "probe", GOOD_BODY) merge_skill_store(tree, body_store, {}) assert tree["nextcloud"][0].description == "probe", "the generated body replaces the seed" assert len(tree["nextcloud"]) == 1 def test_scheduler_loads_committed_seed_skills(tmp_path): """The real seed package ships in the tree at startup with its manifest description and declared integration.""" seed_root = Path(__file__).resolve().parent.parent / "packs" scheduler = Scheduler( engine=SemIfEngine(EngineConfig()), llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config={ "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(seed_root), }, trace=TraceLog(str(tmp_path / "runs.jsonl")), ) nextcloud = scheduler.tree["nextcloud"] assert [s.name for s in nextcloud] == ["create_event", "create_task", "next_event"] next_event = next(s for s in nextcloud if s.name == "next_event") assert next_event.description.startswith("Report the next") assert next_event.status == "ready" assert next_event.integration["transport"] == "http" create_event = next(s for s in nextcloud if s.name == "create_event") assert create_event.description.startswith("Create a calendar event") assert create_event.integration["transport"] == "http" def test_load_config_anchors_runtime_paths_to_checkout(tmp_path): """Path-valued config keys resolve against the checkout, not the cwd, so a fresh clone finds its committed packs from anywhere; absolute paths pass through untouched.""" config_file = tmp_path / "config.json" config_file.write_text( json.dumps( { "skill_packs": "packs", "skill_bodies": "data/skills", "log": "/absolute/decisions.jsonl", } ) ) config = load_config(str(config_file)) assert config["skill_packs"] == str(REPO_ROOT / "packs") assert config["skill_bodies"] == str(REPO_ROOT / "data/skills") assert config["category_registry"] == str(REPO_ROOT / "data/categories.json") assert config["log"] == "/absolute/decisions.jsonl" assert config["trace"] == str(REPO_ROOT / "data/runs.jsonl") def test_committed_seeds_load_from_a_foreign_cwd(tmp_path, monkeypatch): """A fresh checkout's packs come from the anchored `packs/` path even when the process runs from an unrelated directory (the zero-skills regression: relative paths resolved against cwd, so the tree was only response.reject and navigation fell into the codegen loop).""" config_file = tmp_path / "config.json" config_file.write_text( json.dumps( { "skill_packs": "packs", "skill_bodies": str(tmp_path / "skills"), "category_registry": str(tmp_path / "categories.json"), "log": str(tmp_path / "decisions.jsonl"), "trace": str(tmp_path / "runs.jsonl"), } ) ) monkeypatch.chdir(tmp_path) config = load_config(str(config_file)) scheduler = Scheduler( engine=SemIfEngine(EngineConfig()), llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(config["log"]), config=config, trace=TraceLog(config["trace"]), ) assert [s.name for s in scheduler.tree["simplex"]] == [ "connect_link", "next_message", "send_message", ] assert [s.name for s in scheduler.tree["nextcloud"]] == [ "create_event", "create_task", "next_event", ] def test_navigate_empty_tree_short_circuits(tmp_path): """An empty tree goes straight to CreateCategory without a SemIf call.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) result = navigate(None, log, trace, Request("anything"), {}) assert isinstance(result, CreateCategory) assert log.read() == [] assert any(e["kind"] == "create_category" for e in trace.read()) def test_navigate_canned_category_never_offers_create(tmp_path): """The `response` tree is closed: its leaf choice has no create_skill option and never authors, regardless of how weak the match is. The actionability guard is what routes a non-request input into it.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = build_tree(build_skills({"skills": {}})) engine = ScriptedEngine(choices={"non-request input": "non_action"}) result = navigate(engine, log, trace, Request("hello there"), tree) assert isinstance(result, Skill) assert result.category == "response" leaf = next(c for c in engine.calls if "canned response" in c.question) assert all(o.id != "create_skill" for o in leaf.options) assert "create_skill" not in [o.id for o in leaf.options] assert result.name in [o.id for o in leaf.options] def test_navigate_canned_category_points_at_the_catchall(tmp_path): """The catchall is offered last and named in the question when nothing specific fits.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = build_tree(build_skills({"skills": {}})) engine = _ProbEngine( { "non-request input": {"non_action": 1.0}, "canned response": {"response.clarify": 1.0}, } ) result = navigate(engine, log, trace, Request("do the thing"), tree) assert result.name == "response.clarify" leaf = engine.calls[-1] assert "response.clarify" in leaf.question assert leaf.options[-1].id == "response.clarify" assert "navigate:response" in [r["extra"]["phase"] for r in log.read()] def test_canned_response_runs_without_assessment(tmp_path): """A canned reply is a fixed line: no side effect to assess, no repair loop. The runner returns success without an assess:outcome decision.""" from semif_agent.skill import SkillRunner from semif_agent.skills import ActionContext log = DecisionLog(str(tmp_path / "decisions.jsonl")) skill = next(s for s in build_skills({"skills": {}}) if s.name == "response.greeting") runner = SkillRunner(ActionContext(engine=_ProbEngine({}), config={}), log) outcome = runner.run(skill, Request("hello")) assert outcome.success is True assert outcome.new_state == "Hello! What can I help you with?" assert outcome.error is None assert log.read() == [] def test_assessment_state_carries_skill_action(tmp_path): """The assessment decision must see what the skill was meant to do. The state carries the skill's declared action, the goal, and the resolved inputs so the decision can judge whether more work is needed toward the request. One three-way decision (done/continue/failed), no second call. """ from semif_agent.skill import SkillRunner from semif_agent.skills import ActionContext engine = ScriptedEngine( choices={"additional work to be done": "done"} ) log = DecisionLog(str(tmp_path / "decisions.jsonl")) def act(ctx, request): return ActionResult(action_log="SimpleX message from pepper: hello", new_state="read") skill = Skill( name="next_message", category="simplex", description="Read the next unread SimpleX message.", act=act, ) runner = SkillRunner(ActionContext(engine=engine, config={}), log) outcome = runner.run(skill, Request("What's my next simplex message?")) assert len(engine.calls) == 1 state = engine.calls[0].state assert "skill action: Read the next unread SimpleX message." in state assert "goal: What's my next simplex message?" in state assert outcome.outcome == "done" def test_assessment_three_way_verdict(tmp_path): """The one `assess:outcome` decision yields done / continue / failed by threshold precedence (failed > done > continue).""" from semif_agent.skill import SkillRunner from semif_agent.skills import ActionContext skill = Skill( name="next_message", category="simplex", description="Read the next unread SimpleX message.", act=lambda ctx, request: ActionResult(action_log="did it", new_state="x"), ) def verdict_for(selected): engine = ScriptedEngine(choices={"additional work to be done": selected}) log = DecisionLog(str(tmp_path / f"{selected}.jsonl")) runner = SkillRunner(ActionContext(engine=engine, config={}), log) return runner.run(skill, Request("go")).outcome assert verdict_for("done") == "done" assert verdict_for("continue") == "continue" assert verdict_for("failed") == "failed" def test_assessment_state_includes_run_ledger(tmp_path): """A continuation's assessment sees the steps already taken, so it can tell whether the request is satisfied or more work is needed.""" from semif_agent.skill import SkillRunner from semif_agent.skills import ActionContext engine = ScriptedEngine(choices={"additional work to be done": "done"}) log = DecisionLog(str(tmp_path / "decisions.jsonl")) skill = Skill( name="next_message", category="simplex", description="Read the next unread SimpleX message.", act=lambda ctx, request: ActionResult(action_log="second step", new_state="read"), ) runner = SkillRunner(ActionContext(engine=engine, config={}), log) request = Request("do the thing") request.run_ledger = [ {"query": "do the thing", "skill": "simplex.other", "outcome": "already ran"} ] runner.run(skill, request) state = engine.calls[0].state assert "steps already taken:" in state assert "simplex.other -> already ran" in state def test_navigate_leaf_picks_among_existing_skills_only(tmp_path): """Two-stage leaf: create_skill is NOT an option in the leaf softmax. The leaf decision offers only the existing skills, and navigation returns the best-matching one; the intent guard in dispatch owns reuse-vs-create.""" class Recording: def __init__(self): self.request = None def call(self, request): from semif_agent.decisions import DecisionResult self.request = request ids = [o.id for o in request.options] if "non_action" in ids: # the top-level actionability guard: this input is a request probs = [1.0 if o == "action" else 0.0 for o in ids] else: probs = [1.0] * len(ids) return DecisionResult(request=request, option_ids=ids, probabilities=probs) engine = Recording() log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = { "simplex": [ Skill(name="next_message", category="simplex", description="Read the next SimpleX message."), Skill(name="connect_link", category="simplex", description="Show the contact link."), ] } result = navigate(engine, log, trace, Request("send a simplex message"), tree) ids = [o.id for o in engine.request.options] assert ids == ["next_message", "connect_link"] assert "create_skill" not in ids assert getattr(result, "name", None) in ids class _ProbEngine: """Returns caller-specified probabilities, keyed by a substring of the decision question, so a test can force a specific navigation outcome.""" 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} probs_map = {k: v for k, v in probs_map.items() if k in ids} 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.", ), Skill( name="connect_link", category="simplex", description="Show the contact link.", ), ] } def test_navigate_leaf_returns_best_existing_skill(tmp_path): """The leaf softmax picks the highest-probability existing skill; no create branch competes with it (that was the dilution bug).""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = _ProbEngine( { "logical next step": {"simplex": 1.0}, "Does the scope": {"covers": 1.0}, "skill should run next": {"next_message": 0.45, "connect_link": 0.55}, } ) result = navigate( engine, log, trace, Request("send a simplex message"), _leaf_tree() ) assert getattr(result, "name", None) == "connect_link" assert not isinstance(result, CreateSkill) def test_navigate_single_skill_category_skips_the_softmax(tmp_path): """A one-option softmax is meaningless; the sole skill is returned directly and the guard decides reuse-vs-create.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = _ProbEngine( {"logical next step": {"simplex": 1.0}, "Does the scope": {"covers": 1.0}} ) tree = { "simplex": [ Skill(name="next_message", category="simplex", description="Read the next SimpleX message.") ] } result = navigate(engine, log, trace, Request("read the next message"), tree) assert getattr(result, "name", None) == "next_message" # only the category decision was made; the leaf softmax was skipped assert all("Which simplex skill" not in c.question for c in engine.calls) def test_navigate_empty_category_short_circuits_to_create(tmp_path): log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = _ProbEngine({"logical next step": {"simplex": 1.0}}) result = navigate(engine, log, trace, Request("anything"), {"simplex": []}) assert isinstance(result, CreateSkill) assert result.category == "simplex" assert any(e["kind"] == "skill_needed" for e in trace.read()) def test_navigate_category_options_carry_descriptions(tmp_path): """The category softmax must offer each category WITH its description — bare names carry no signal (the misroute bug).""" engine = _ProbEngine({"logical next step": {"simplex": 1.0}}) log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = { "simplex": [ Skill( name="next_message", category="simplex", description="Read the next SimpleX message.", category_description="SimpleX messaging: read/send messages.", ) ] } navigate(engine, log, trace, Request("read the next message"), tree) cat_decision = next(c for c in engine.calls if "logical next step" in c.question) by_id = {o.id: o.description for o in cat_decision.options} assert by_id["simplex"] == "SimpleX messaging: read/send messages." assert "new top-level category is needed" in by_id["create_category"] def test_navigate_category_rejected_scope_authors_category(tmp_path): """The confirm guard is the category create door: a softmax winner whose scope the guard rejects authors a new category. The winner is below `softmax_bypass_tau`, so the guard actually runs.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = _leaf_tree() engine = _ProbEngine( { "logical next step": {"simplex": 0.45}, "Does the scope": {"none": 1.0}, } ) result = navigate(engine, log, trace, Request("book a flight"), tree) assert isinstance(result, CreateCategory) assert any(e["kind"] == "category_scope_rejected" for e in trace.read()) assert any(e["kind"] == "create_category" for e in trace.read()) def test_navigate_category_confirmed_scope_descends(tmp_path): """A below-threshold softmax winner the guard confirms is descended into.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = _ProbEngine( { "logical next step": {"simplex": 0.45}, "Does the scope": {"covers": 1.0}, "skill should run next": {"next_message": 1.0, "connect_link": 0.0}, } ) result = navigate(engine, log, trace, Request("read a message"), _leaf_tree()) assert getattr(result, "name", None) == "next_message" assert not isinstance(result, CreateCategory) assert any(e["kind"] == "category_scope" and e["fits"] for e in trace.read()) def test_navigate_confident_category_winner_skips_scope_confirm(tmp_path): """A category softmax winner at/above `softmax_bypass_tau` is decisive: the scope confirm is not called (no `navigate:category_scope` decision).""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = _ProbEngine( { "logical next step": {"simplex": 0.9}, "skill should run next": {"next_message": 1.0, "connect_link": 0.0}, } ) result = navigate(engine, log, trace, Request("read a message"), _leaf_tree()) assert getattr(result, "name", None) == "next_message" assert any(e["kind"] == "category_scope_bypassed" for e in trace.read()) assert not any(e["kind"] == "category_scope" for e in trace.read()) assert all("Does the scope" not in c.question for c in engine.calls) phases = [r["extra"].get("phase") for r in log.read()] assert "navigate:category_scope" not in phases def test_confirm_category_fit_threshold(tmp_path): log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = ScriptedEngine(choices={"Does the scope": "covers"}) assert ( confirm_category_fit( engine, log, trace, Request("check my calendar"), "nextcloud", "Nextcloud.", tau=0.5 ) is True ) assert log.read()[-1]["extra"]["phase"] == "navigate:category_scope" engine = ScriptedEngine(choices={"Does the scope": "none"}) assert ( confirm_category_fit( engine, log, trace, Request("order pizza"), "nextcloud", "Nextcloud.", tau=0.5 ) is False ) def test_confirm_non_action_threshold(tmp_path): """The actionability guard returns True for non-request input and False for a request, logging phase `navigate:actionability`.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = ScriptedEngine(choices={"non-request input": "non_action"}) assert ( confirm_non_action(engine, log, trace, Request("hello there"), tau=0.5) is True ) assert log.read()[-1]["extra"]["phase"] == "navigate:actionability" engine = ScriptedEngine(choices={"non-request input": "action"}) assert ( confirm_non_action(engine, log, trace, Request("what is 255 * 12?"), tau=0.5) is False ) assert any( e["kind"] == "actionability" and e["non_action"] is False for e in trace.read() ) def test_navigate_actionability_sends_a_request_to_create(tmp_path): """A request the softmax dumps in the catchall must author, not get a canned reply: the actionability guard is the create door ("what is 255 * 12?").""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = build_tree(build_skills({"skills": {}})) engine = _ProbEngine( { "logical next step": {"response": 1.0}, "non-request input": {"action": 1.0}, } ) result = navigate(engine, log, trace, Request("what is 255 * 12?"), tree) assert isinstance(result, CreateCategory) assert any( e["kind"] == "actionability" and e["non_action"] is False for e in trace.read() ) assert any(e["kind"] == "create_category" for e in trace.read()) def test_navigate_actionability_keeps_a_statement_in_response(tmp_path): """The mirror: a statement the reworded scope pushes to create_category must still get a canned reply ("1+1=2"), not author a category.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = build_tree(build_skills({"skills": {}})) engine = _ProbEngine( { "logical next step": {"create_category": 1.0}, "non-request input": {"non_action": 1.0}, } ) result = navigate(engine, log, trace, Request("1+1=2"), tree) assert isinstance(result, Skill) assert result.category == "response" assert any( e["kind"] == "actionability" and e["non_action"] is True for e in trace.read() ) def test_navigate_actionability_gate_precedes_category_selection(tmp_path): """The actionability guard runs at the top: a non-request never reaches the category softmax, so it cannot be routed into a real category even when the softmax would have picked one.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = build_tree(build_skills({"skills": {}})) tree["simplex"] = [ Skill( name="next_message", category="simplex", description="Read the next SimpleX message.", ) ] engine = _ProbEngine( { "non-request input": {"non_action": 1.0}, # If the category softmax ran it would route to simplex, not response. "logical next step": {"simplex": 1.0}, "canned response": {"response.greeting": 1.0}, } ) result = navigate(engine, log, trace, Request("the sky is blue"), tree) assert getattr(result, "category", None) == "response" assert all( "logical next step" not in c.question for c in engine.calls ), "the category softmax must not run for non-request input" phases = [r["extra"]["phase"] for r in log.read()] assert "navigate:actionability" in phases assert "navigate:category" not in phases assert phases.index("navigate:actionability") < phases.index("navigate:response") def test_navigate_category_softmax_excludes_the_canned_response_tree(tmp_path): """Once actionability is decided at the top, `response` is never offered in the category softmax — a request cannot be routed to a canned reply — and actionability is logged before category selection.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) tree = build_tree(build_skills({"skills": {}})) tree["simplex"] = [ Skill( name="next_message", category="simplex", description="Read the next SimpleX message.", ) ] engine = _ProbEngine( { "logical next step": {"simplex": 1.0}, "Does the scope": {"covers": 1.0}, } ) navigate(engine, log, trace, Request("read the next simplex message"), tree) cat_decision = next( c for c in engine.calls if "logical next step" in c.question ) ids = [o.id for o in cat_decision.options] assert "response" not in ids assert "simplex" in ids and "create_category" in ids phases = [r["extra"]["phase"] for r in log.read()] assert phases.index("navigate:actionability") < phases.index("navigate:category") def test_dispatch_action_tau_controls_catchall_routing(tmp_path): """navigation.action_tau is the catchall create door, separate from the other taus: a borderline non-action verdict flips on it.""" def build(action_tau: float): scheduler = Scheduler( engine=_ProbEngine( { "logical next step": {"response": 1.0}, "non-request input": {"non_action": 0.6, "action": 0.4}, } ), llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config={ "skills": {}, "new_skill_creation": "allow", "navigation": {"action_tau": action_tau}, "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), }, trace=TraceLog(str(tmp_path / "runs.jsonl")), ) scheduler.tree = build_tree(build_skills({"skills": {}})) scheduler._queue_draft = lambda *a, **k: None return scheduler low = build(0.5) assert low.action_tau == 0.5 ran = low._dispatch(Request("hmm")) assert ran.kind == "ran", "non_action 0.6 clears action_tau 0.5: canned reply" high = build(0.7) assert high.action_tau == 0.7 created = high._dispatch(Request("hmm")) assert created.kind == "create_category", "non_action 0.6 fails action_tau 0.7: author" def test_dispatch_intent_tau_controls_reuse_vs_create(tmp_path): """The leaf reuse-vs-create knob is navigation.intent_tau, separate from the top-level tau: a borderline guard verdict flips on it while assessment is untouched.""" def build(intent_tau: float): engine = _ProbEngine( { "logical next step": {"simplex": 1.0}, "Does the scope": {"covers": 1.0}, # The leaf softmax offers only existing skills, so the intent # guard always runs (it is the create door). "skill should run next": {"next_message": 0.45, "connect_link": 0.3}, "what does this skill do": {"same": 0.8, "different": 0.2}, } ) scheduler = Scheduler( engine=engine, llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config={ "skills": {}, "new_skill_creation": "allow", "navigation": {"intent_tau": intent_tau}, "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), }, trace=TraceLog(str(tmp_path / "runs.jsonl")), ) def act(ctx, request): return ActionResult(action_log="read fixture", new_state="read") scheduler.tree = { "simplex": [ Skill(name="next_message", category="simplex", description="Read the next SimpleX message.", act=act), Skill(name="connect_link", category="simplex", description="Show the contact link."), ] } queued: list = [] scheduler._queue_draft = lambda request, category, kind: queued.append( (category, kind) ) return scheduler, queued low, queued_low = build(0.5) assert low.intent_tau == 0.5 ran = low._dispatch(Request("read the next simplex message")) assert ran.kind == "ran", "guard same=0.8 clears intent_tau=0.5: reuse" assert not queued_low high, queued_high = build(0.95) created = high._dispatch(Request("read the next simplex message")) assert created.kind == "create_skill", "guard same=0.8 fails intent_tau=0.95: author" assert queued_high == [("simplex", "skill")] def test_confirm_skill_fit_returns_same_when_action_matches(tmp_path): log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = ScriptedEngine(choices={"what does this skill do": "same"}) skill = Skill(name="next_message", category="simplex", description="Read the next SimpleX message.") assert confirm_skill_fit(engine, log, trace, Request("read the next simplex message"), skill) == "same" row = log.read()[-1] assert row["extra"]["phase"] == "navigate:intent" assert row["selected"] == "same" def test_confirm_skill_fit_returns_different_on_mismatch(tmp_path): log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = ScriptedEngine(choices={"what does this skill do": "different"}) skill = Skill(name="next_message", category="simplex", description="Read the next SimpleX message.") assert confirm_skill_fit(engine, log, trace, Request("send a simplex message"), skill) == "different" assert any(e["kind"] == "intent_guard" and e["fits"] is False for e in trace.read()) def test_confirm_skill_fit_returns_part_for_a_compound_request(tmp_path): """A skill that performs one required part of a compound request is accepted as `part` (not rejected into create), and the ledger is in the state.""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) engine = ScriptedEngine(choices={"what does this skill do": "part"}) skill = Skill(name="calculate", category="calculator", description="Do arithmetic.") request = Request("send the result of 22 * 10 to pepper on simplex") request.run_ledger = [ {"query": "x", "skill": "calculator.calculate", "outcome": "done"} ] assert confirm_skill_fit(engine, log, trace, request, skill) == "part" guard = next(e for e in trace.read() if e["kind"] == "intent_guard") assert guard["verdict"] == "part" and guard["fits"] is True def test_dispatch_intent_mismatch_authorizes_new_skill(tmp_path): """A routed skill whose action doesn't match the request must not run: the guard sends dispatch to create_skill in the same category instead.""" scheduler = _scheduler( tmp_path, choices={"logical next step": "simplex", "what does this skill do": "different"}, ) scheduler.tree.pop("response", None) scheduler.tree["simplex"] = [ Skill(name="next_message", category="simplex", description="Read the next SimpleX message."), ] queued: list = [] scheduler._queue_draft = lambda request, category, kind: queued.append( (category, kind) ) request = Request("send a simplex message to pepper") result = scheduler._dispatch(request) assert result.kind == "create_skill" assert queued == [("simplex", "skill")] phases = [r["extra"].get("phase") for r in scheduler.log.read()] assert "navigate:intent" in phases def test_dispatch_intent_match_runs_skill(tmp_path): scheduler = _scheduler( tmp_path, choices={"logical next step": "simplex", "what does this skill do": "same"} ) def act(ctx, request): return ActionResult(action_log="read the next message", new_state="read") scheduler.tree.pop("response", None) scheduler.tree["simplex"] = [ Skill( name="next_message", category="simplex", description="Read the next SimpleX message.", act=act, ), ] request = Request("read the next simplex message") result = scheduler._dispatch(request) assert result.kind == "ran" assert result.skill == "next_message" def test_dispatch_confident_leaf_winner_still_runs_intent_guard(tmp_path): """A leaf softmax winner at/above `softmax_bypass_tau` does NOT skip the intent confirm: the leaf softmax offers only existing skills (no create option), so a confident winner is "best of the wrong lot", never a match. The guard runs and, here, rejects the winner into create_skill. Regression: with Winnow as the engine a send request picked `connect_link` at 0.86 / `next_message` at 0.995, the old `intent_bypass` ran a read skill, and codegen was never reached.""" engine = _ProbEngine( { "logical next step": {"simplex": 1.0}, "skill should run next": {"next_message": 0.9, "connect_link": 0.1}, "what does this skill do": {"different": 1.0}, } ) scheduler = Scheduler( engine=engine, llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config={ "skills": {}, "new_skill_creation": "allow", "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), }, trace=TraceLog(str(tmp_path / "runs.jsonl")), ) def act(ctx, request): return ActionResult(action_log="read fixture", new_state="read") scheduler.tree = { "simplex": [ Skill( name="next_message", category="simplex", description="Read the next SimpleX message.", act=act, ), Skill(name="connect_link", category="simplex", description="Show the contact link."), ] } queued: list = [] scheduler._queue_draft = lambda request, category, kind: queued.append( (category, kind) ) result = scheduler._dispatch(Request("send a simplex message to pepper")) assert result.kind == "create_skill" assert queued == [("simplex", "skill")] assert not any(e["kind"] == "intent_bypass" for e in scheduler.trace.read()) phases = [r["extra"].get("phase") for r in scheduler.log.read()] assert "navigate:intent" in phases def test_dispatch_create_category_queues_draft_and_is_not_fatal(tmp_path): """An empty tree short-circuits to CreateCategory; the `llm` draft is queued asynchronously. An unreachable llm endpoint is graceful — traced and the request is not re-dispatched — never fatal (only SemIf is fatal).""" log = DecisionLog(str(tmp_path / "decisions.jsonl")) trace = TraceLog(str(tmp_path / "runs.jsonl")) scheduler = Scheduler( engine=SemIfEngine(EngineConfig()), llm=LLMClient(base_url="http://127.0.0.1:1/v1", model="test", timeout=2), log=log, config={ "skills": {}, "new_skill_creation": "allow", "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), }, trace=trace, ) scheduler.tree = {} request = Request("anything") result = scheduler._dispatch(request) assert result.kind == "create_category" assert scheduler.fatal is None deadline = time.monotonic() + 5 while time.monotonic() < deadline: if any(e["kind"] == "draft_failed" for e in trace.read()): break time.sleep(0.02) assert any(e["kind"] == "draft_failed" for e in trace.read()) assert scheduler.fatal is None # The failure is buffered for the front end that owns the request (the # voice gateway speaks it instead of leaving the user in silence). notices = scheduler.drain_outbox() assert notices, "a failed draft must be routable to the chat" assert notices[0].run_id == request.id assert "couldn't create a new category" in notices[0].text def _answer_approval_when_posted(scheduler, approved, timeout=5.0): """Approve/deny the first pending creation proposal, as a front end would.""" deadline = time.monotonic() + timeout while time.monotonic() < deadline: pending = scheduler.pending_approvals() if pending: scheduler.answer_approval(pending[0]["id"], approved) return True time.sleep(0.02) return False def test_creation_mode_defaults_to_deny_and_allow_opts_in(tmp_path): """New-skill creation is denied by default (the agent leans on the committed packs); a host opts in by setting `new_skill_creation: allow`.""" # Unset -> deny: the gate aborts without prompting. unset = Scheduler( engine=ScriptedEngine(), llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config={ "skills": {}, "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), }, trace=TraceLog(str(tmp_path / "runs.jsonl")), ) assert unset.new_skill_mode == "deny" job = DraftAuthor( request=Request("book a trip"), category=None, kind="category" ) assert unset._request_approval(job, "category", "travel", "Trips.") is False # Explicit allow: the gate returns immediately and posts nothing. scheduler = _scheduler(tmp_path) assert scheduler.new_skill_mode == "allow" assert scheduler._request_approval(job, "category", "travel", "Trips.") is True assert scheduler.pending_approvals() == [] def test_creation_mode_deny_aborts_without_prompting(tmp_path): """Global `deny` blocks authoring outright; no approval is posted.""" scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "deny" scheduler.defer_questions = True job = DraftAuthor( request=Request("book a trip"), category=None, kind="category" ) assert scheduler._request_approval(job, "category", "travel", "Trips.") is False assert scheduler.pending_approvals() == [] assert any( e["kind"] == "category_create_blocked" for e in scheduler.trace.read() ) def test_creation_mode_per_category_override(tmp_path): """A category override wins over the global mode.""" scheduler = _scheduler(tmp_path) scheduler.tree["travel"] = [] scheduler.new_skill_mode = "ask" scheduler.set_category_mode("travel", "allow") job = DraftAuthor( request=Request("book a trip"), category="travel", kind="skill" ) # allow -> proceeds with no prompt even though the global mode is ask assert scheduler._request_approval(job, "skill", "book_flight", "Book.") is True assert scheduler.pending_approvals() == [] scheduler.set_category_mode("travel", "deny") assert scheduler._request_approval(job, "skill", "book_flight", "Book.") is False assert any( e["kind"] == "skill_create_blocked" for e in scheduler.trace.read() ) def test_creation_approval_approved_proceeds(tmp_path): scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "ask" scheduler.defer_questions = True job = DraftAuthor( request=Request("book a trip"), category=None, kind="category" ) result = {} thread = threading.Thread( target=lambda: result.__setitem__( "ok", scheduler._request_approval(job, "category", "travel", "Trips.") ) ) thread.start() assert _answer_approval_when_posted(scheduler, approved=True) thread.join() assert result["ok"] is True kinds = [e["kind"] for e in scheduler.trace.read()] assert "creation_approval_requested" in kinds assert "creation_approval_approved" in kinds assert scheduler.pending_approvals() == [] def test_creation_approval_denied_aborts(tmp_path): scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "ask" scheduler.defer_questions = True job = DraftAuthor( request=Request("book a trip"), category="travel", kind="skill" ) result = {} thread = threading.Thread( target=lambda: result.__setitem__( "ok", scheduler._request_approval(job, "skill", "book_flight", "Book.") ) ) thread.start() assert _answer_approval_when_posted(scheduler, approved=False) thread.join() assert result["ok"] is False assert any( e["kind"] == "creation_approval_denied" for e in scheduler.trace.read() ) def test_creation_approval_timeout_denies(tmp_path): scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "ask" scheduler.defer_questions = True scheduler.approval_wait = 0.1 job = DraftAuthor( request=Request("book a trip"), category="travel", kind="skill" ) assert scheduler._request_approval(job, "skill", "book_flight", "Book.") is False assert any( e["kind"] == "creation_approval_timeout" for e in scheduler.trace.read() ) def test_creation_approval_without_front_end_denies(tmp_path): """With no deferring front end there is nobody to answer, so creation is denied immediately rather than stalling the draft worker.""" scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "ask" scheduler.defer_questions = False job = DraftAuthor( request=Request("book a trip"), category="travel", kind="skill" ) assert scheduler._request_approval(job, "skill", "book_flight", "Book.") is False assert any( e["kind"] == "creation_approval_skipped" for e in scheduler.trace.read() ) def test_creation_approval_chain_skips_second_prompt(tmp_path): """A skill job chained from an approved category is already covered.""" scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "ask" scheduler.defer_questions = True job = DraftAuthor( request=Request("book a trip"), category="travel", kind="skill", approved=True, ) assert scheduler._request_approval(job, "skill", "book_flight", "Book.") is True assert scheduler.pending_approvals() == [] def test_author_category_denied_does_not_register(tmp_path, monkeypatch): """The category hook: a denied proposal registers nothing and queues no chained skill draft.""" scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "ask" scheduler.defer_questions = True monkeypatch.setattr( "semif_agent.scheduler.generate_category", lambda client, request, tree: CategoryDraft(name="travel", description="Trips."), ) job = DraftAuthor( request=Request("book a trip"), category=None, kind="category" ) thread = threading.Thread(target=scheduler._author_category, args=(job,)) thread.start() assert _answer_approval_when_posted(scheduler, approved=False) thread.join() assert "travel" not in scheduler.tree assert not any(e["kind"] == "category_created" for e in scheduler.trace.read()) assert list(scheduler._drafts) == [] def test_author_skill_denied_does_not_register(tmp_path, monkeypatch): scheduler = _scheduler(tmp_path) scheduler.new_skill_mode = "ask" scheduler.defer_questions = True monkeypatch.setattr( "semif_agent.scheduler.generate_skill_spec", lambda client, request, category, tree: SkillDraft( name="book_flight", description="Book a flight." ), ) job = DraftAuthor( request=Request("book a flight"), category="travel", kind="skill" ) thread = threading.Thread(target=scheduler._author_skill, args=(job,)) thread.start() assert _answer_approval_when_posted(scheduler, approved=False) thread.join() assert scheduler.tree.get("travel", []) == [] assert not any(e["kind"] == "skill_created" for e in scheduler.trace.read()) def test_skill_status_reflects_writing_and_noop(): """A fresh Skill is a stub; marking it writing shows `writing`; a real body shows `ready`.""" stub = Skill(name="probe", category="tracking", description="Probe.") assert stub.is_noop() assert stub.status == "stub" stub.writing = True assert stub.status == "writing" stub.writing = False def act(ctx, request): return None ready = Skill(name="probe", category="tracking", description="Probe.", act=act) assert not ready.is_noop() assert ready.status == "ready" def test_restart_skill_without_codegen_returns_error(tmp_path): scheduler = _scheduler(tmp_path) scheduler.tree["tracking"] = [ Skill(name="track_live", category="tracking", description="Follow a package.") ] status, detail = scheduler.restart_skill("tracking", "track_live") assert status == "error" assert "no codegen" in detail def test_restart_skill_unknown_leaf_returns_error(tmp_path): scheduler = _scheduler(tmp_path) status, detail = scheduler.restart_skill("tracking", "missing") assert status == "error" assert "no skill" in detail def test_dispatch_skill_awaiting_body_does_not_reauthor(tmp_path): """A re-dispatched request whose skill body is still pending must report the pending write instead of authoring a second skill.""" scheduler = _scheduler(tmp_path) request = Request("track my package") request.meta["awaiting_skill_body"] = ["tracking", "track_live"] result = scheduler._dispatch_skill(request, "tracking") assert result.kind == "create_skill" assert "track_live" in result.summary assert "restart" in result.summary def test_run_skill_guards_stub_and_writing_leaves(tmp_path): """Running a writing leaf or a body-less stub must not silently no-op.""" scheduler = _scheduler(tmp_path) writing = Skill(name="w", category="tracking", description="writing") writing.writing = True result = scheduler._run_skill(writing, Request("x")) assert result.kind == "error" assert "still being written" in result.summary stub = Skill(name="s", category="tracking", description="stub") result = scheduler._run_skill(stub, Request("x")) assert result.kind == "error" assert "no body" in result.summary def test_async_skill_write_materializes_merges_and_requeues(tmp_path): """The full async pipeline: _start_skill_write flags the leaf in-progress and the single-slot worker runs codegen (body + CONTRACT) -> testgen -> test, then materializes the body, hot-merges it into the tree, and re-queues the original request for re-dispatch.""" httpd, base = _pipeline_codegen_server([GOOD_BODY, GOOD_TEST]) try: scheduler = _scheduler(tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10)) scheduler.tree["tracking"] = [ Skill(name="track_live", category="tracking", description="Follow a package.") ] leaf = scheduler.tree["tracking"][0] request = Request("track my drone delivery in real time") scheduler._start_skill_write(request, "tracking", SkillDraft(name="track_live", description="Follow a package.")) assert leaf.writing is True deadline = time.monotonic() + 10 created = None while time.monotonic() < deadline: created = next( (e for e in scheduler.trace.read() if e["kind"] == "skill_created"), None, ) if created: break time.sleep(0.05) assert created is not None, "worker must complete the body write" assert created["written"] is True upgraded = scheduler.tree["tracking"][0] assert upgraded is not leaf assert not upgraded.is_noop() assert callable(upgraded.act) # The worker drains the queue when idle (run_queue_buffered), so an empty # queue is a valid end state; the requeue trace proves the push happened. events = [e["kind"] for e in scheduler.trace.read()] assert "skill_requeued" in events assert "skill_testing" in events assert "skill_ready" in events # The off-gate re-run's reply is buffered for a front end, not swallowed: # the worker must not consume the answer to the user's own request. deadline = time.monotonic() + 5 while time.monotonic() < deadline and not scheduler._outbox: time.sleep(0.02) buffered = scheduler.drain_outbox() assert buffered, "the post-authoring re-run must be routable by a front end" assert buffered[0].run_id == request.id # The full deliverable set landed in the skill folder. directory = scheduler.body_store.dir("tracking", "track_live") assert sorted(p.name for p in directory.iterdir() if not p.name.startswith("__")) == [ "contract.json", "skill.py", "skill.test.py", ] finally: httpd.shutdown() httpd.server_close() def test_async_skill_write_fails_gracefully_on_bad_contract(tmp_path): """A body without a valid CONTRACT (after the escalation ladder) leaves the leaf a restartable stub — no silent no-op, no wedged worker.""" httpd, base = _pipeline_codegen_server(["def act(ctx, request):\n return None\n"]) try: scheduler = _scheduler(tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10, max_attempts=2)) scheduler.tree["tracking"] = [ Skill(name="track_live", category="tracking", description="Follow a package.") ] leaf = scheduler.tree["tracking"][0] request = Request("track my drone delivery in real time") scheduler._start_skill_write(request, "tracking", SkillDraft(name="track_live", description="Follow a package.")) deadline = time.monotonic() + 10 failed = None while time.monotonic() < deadline: failed = next( (e for e in scheduler.trace.read() if e["kind"] == "skill_write_failed"), None, ) if failed: break time.sleep(0.05) assert failed is not None, "worker must surface the failure" assert not leaf.writing assert leaf.is_noop(), "leaf must stay a restartable stub" # The failure is buffered for the front end that owns the request, so a # voice user hears it instead of the scheduler's stdout-only print. deadline = time.monotonic() + 5 while time.monotonic() < deadline and not scheduler._outbox: time.sleep(0.02) notices = scheduler.drain_outbox() assert notices, "a failed body write must be routable to the chat" assert notices[0].run_id == request.id assert "couldn't finish creating" in notices[0].text finally: httpd.shutdown() httpd.server_close() def test_async_skill_write_fails_gracefully_on_unimported_names(tmp_path): """A body with a valid CONTRACT but a name it never imports is rejected by the parse gate, so the retry ladder runs and the leaf stays a restartable stub — never a leaf that only fails at first run. Regression: the static gate used to accept a body that returned `ActionResult` without importing it; materialize then raised `name 'ActionResult' is not defined`. """ body = ( "INTEGRATION = {'service': 'probe_service', 'transport': 'compute', " "'config_vars': ['sender_address']}\n" "CONTRACT = {'sender_address': 'The sender.'}\n" "def act(ctx, request):\n" " sender = ctx.config['sender_address']\n" " return ActionResult(action_log=sender, new_state=request.text)\n" ) httpd, base = _pipeline_codegen_server([body]) try: scheduler = _scheduler( tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10, max_attempts=2), ) scheduler.tree["tracking"] = [ Skill(name="track_live", category="tracking", description="Follow a package.") ] leaf = scheduler.tree["tracking"][0] request = Request("track my drone delivery in real time") scheduler._start_skill_write( request, "tracking", SkillDraft(name="track_live", description="Follow a package.") ) deadline = time.monotonic() + 10 failed = None while time.monotonic() < deadline: failed = next( (e for e in scheduler.trace.read() if e["kind"] == "skill_write_failed"), None, ) if failed: break time.sleep(0.05) assert failed is not None, "worker must surface the failure" assert "never imports" in failed.get("message", ""), failed assert not leaf.writing assert leaf.is_noop(), "leaf must stay a restartable stub" finally: httpd.shutdown() httpd.server_close() def test_skill_pre_act_contract_pause_and_resume(tmp_path): """A contract variable the runner cannot satisfy pauses BEFORE act; the answer is recorded (engine unavailable -> ask-again, per-fire), then the run continues. A second pause comes from the skill's own needs_input.""" scheduler = _scheduler(tmp_path, choices={"ask again each time": "ask_again"}) scheduler.tree["tracking"] = [] store = scheduler.body_store store.write_contract("tracking", "track_live", {"token": "The tracking token."}) seen = [] def act(ctx, request): if request.user_input: return ActionResult(action_log="done", new_state="done") return ActionResult( action_log="need confirmation", new_state=request.text, needs_input="Confirm?" ) skill = Skill( name="track_live", category="tracking", description="Follow a package.", act=act, contract={"token": "The tracking token."}, ) result = scheduler._run_skill(skill, Request("track my package")) assert result.kind == "needs_input" assert "`token`" in result.summary assert scheduler.pending is not None assert scheduler.pending.pre_act is True reply = scheduler.answer("AB123") assert reply.status == "needs_input" assert scheduler.pending is not None assert scheduler.pending.pre_act is False assert scheduler.pending.request.meta["config_answers"]["token"] == "AB123" assert "Confirm?" in scheduler.pending.question reply = scheduler.answer("yes") assert reply.status == "ran" assert scheduler.pending is None def test_pre_act_skip_leaves_variable_unset_and_does_not_reask(tmp_path): """An empty answer skips a contract variable: it is left unset, the run proceeds, and the same variable is never re-asked (regression: the old no-op skip re-asked the identical question forever).""" scheduler = _scheduler(tmp_path) scheduler.tree["tracking"] = [] scheduler.body_store.write_contract( "tracking", "track_live", {"token": "The tracking token."} ) seen = {} def act(ctx, request): seen.update(ctx.config) return ActionResult(action_log="done", new_state="done") skill = Skill( name="track_live", category="tracking", description="Follow a package.", act=act, contract={"token": "The tracking token."}, ) result = scheduler._run_skill(skill, Request("track my package")) assert result.kind == "needs_input" assert scheduler.pending is not None and scheduler.pending.pre_act is True reply = scheduler.answer("") assert reply.status == "ran", reply.text assert scheduler.pending is None assert "token" not in seen, "a skipped variable must stay unset" def test_pre_act_skip_advances_to_next_missing_variable(tmp_path): """Skipping one contract variable leaves just that one empty and moves on to the next missing variable, then runs.""" scheduler = _scheduler(tmp_path) scheduler.tree["tracking"] = [] scheduler.body_store.write_contract( "tracking", "probe", {"alpha": "first", "beta": "second"} ) seen = {} def act(ctx, request): seen.update(ctx.config) return ActionResult(action_log="done", new_state="done") skill = Skill( name="probe", category="tracking", description="Probe.", act=act, contract={"alpha": "first", "beta": "second"}, ) result = scheduler._run_skill(skill, Request("probe")) assert result.kind == "needs_input" assert "`alpha`" in result.summary reply = scheduler.answer("") assert reply.status == "needs_input" assert scheduler.pending is not None assert "`beta`" in scheduler.pending.question reply = scheduler.answer("") assert reply.status == "ran", reply.text assert "alpha" not in seen and "beta" not in seen def test_unresolved_variables_respects_tiered_config(tmp_path): """Contract vars are satisfied by global config, category config, skill config, or per-fire answers — in that order of precedence.""" store = SkillStore(str(tmp_path / "skills")) store.write_contract("tracking", "probe", {"sender_address": "the sender", "receiver": "the receiver", "tag": "a tag"}) store.write_config("tracking", "probe", {"sender_address": "skill-value"}) store.write_category_config("tracking", {"receiver": "category-value"}) skill = Skill(name="probe", category="tracking", description="Probe.", contract={"sender_address": "the sender", "receiver": "the receiver", "tag": "a tag"}, config=store.read_config("tracking", "probe")) global_config = {"tag": "global-value"} assert unresolved_variables(store, skill, global_config) == [] merged = resolve_skill_config(store, skill, global_config) assert merged["sender_address"] == "skill-value" assert merged["receiver"] == "category-value" assert merged["tag"] == "global-value" skill2 = Skill(name="probe", category="tracking", description="Probe.", contract={"sender_address": "the sender", "receiver": "the receiver", "tag": "a tag"}) assert unresolved_variables(store, skill2, {}) == ["sender_address", "tag"] assert unresolved_variables(store, skill2, {}, answered={"tag": "per-fire"}) == ["sender_address"] def test_async_skill_write_regen_ladder_on_failing_test(tmp_path): """A failing auto-run test triggers the regen decision (regen_test by default when no factory), regenerating the test until it passes.""" failing_test = "import sys\nsys.exit(1)" httpd, base = _pipeline_codegen_server( [GOOD_BODY, failing_test, GOOD_TEST] ) try: scheduler = _scheduler(tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10)) scheduler.tree["tracking"] = [ Skill(name="track_live", category="tracking", description="Follow a package.") ] scheduler.regen_decision_factory = lambda run_id: (lambda reason: "regen_test") request = Request("track my drone delivery in real time") scheduler._start_skill_write( request, "tracking", SkillDraft(name="track_live", description="Follow a package.") ) deadline = time.monotonic() + 10 created = None while time.monotonic() < deadline: created = next( (e for e in scheduler.trace.read() if e["kind"] == "skill_created"), None, ) if created: break time.sleep(0.05) assert created is not None and created["written"] is True testings = [e for e in scheduler.trace.read() if e["kind"] == "skill_testing"] assert [t["passed"] for t in testings] == [False, True], ( "first attempt must fail, the regen must pass" ) assert any(e["kind"] == "skill_ready" for e in scheduler.trace.read()) finally: httpd.shutdown() httpd.server_close() # ---- deferred questions ---- def test_pending_questions_roundtrip(tmp_path): scheduler = _scheduler(tmp_path) scheduler.questions.append( PendingQuestion( id="q1", run_id="r1", category="tracking", skill="probe", question="Which service?", ) ) pending = scheduler.pending_questions() assert [q["id"] for q in pending] == ["q1"] status, _ = scheduler.answer_question("q1", "Nextcloud") assert status == "ok" assert scheduler.pending_questions() == [] assert scheduler.questions == [] assert any(e["kind"] == "question_answered" for e in scheduler.trace.read()) def test_answer_question_empty_skips(tmp_path): scheduler = _scheduler(tmp_path) scheduler.questions.append( PendingQuestion( id="q2", run_id="r1", category="tracking", skill="probe", question="Q?" ) ) status, detail = scheduler.answer_question("q2", "") assert status == "ok" assert "skipped" in detail assert scheduler.pending_questions() == [] def test_answer_question_unknown_id_errors(tmp_path): scheduler = _scheduler(tmp_path) status, detail = scheduler.answer_question("nope", "x") assert status == "error" assert "no pending question" in detail # ---- goal persistence ---- def test_registry_stores_goal_for_restart(tmp_path): registry = CategoryRegistry(str(tmp_path / "categories.json")) registry.register_skill( "tracking", "probe", "Probe.", goal="poll a tracking endpoint and report status", ) row = registry.read()["tracking"]["skills"][0] assert row["goal"] == "poll a tracking endpoint and report status" assert "request_text" not in row and "requirements" not in row scheduler = _scheduler(tmp_path) draft = scheduler._stub_draft("tracking", "probe") assert draft.goal == "poll a tracking endpoint and report status" # ---- repair loop ---- def _repair_offer(offer_id="r1", selected="repair_skill", skill="tracking.check"): return RepairOffer( id=offer_id, run_id="run1", category="tracking", skill=skill, selected=selected, reason="boom", request_text="track my package", failure="connection refused", ) def _install_tracking_stub(scheduler): """A repair rewrite needs the leaf to exist in the tree (a real skill that failed), so a runnable body can replace it.""" scheduler.tree["tracking"] = [ Skill( name="tracking.check", category="tracking", description="Check the delivery status of a package.", ) ] def test_resolve_repair_no_repair_declines(tmp_path): scheduler = _scheduler(tmp_path) scheduler.repairs.append(_repair_offer()) assert [r["id"] for r in scheduler.pending_repairs()] == ["r1"] status, _ = scheduler.resolve_repair("r1", "no_repair") assert status == "ok" assert scheduler.pending_repairs() == [] assert any(e["kind"] == "repair_declined" for e in scheduler.trace.read()) def test_resolve_repair_retry_requeues(tmp_path): scheduler = _scheduler(tmp_path) scheduler.repairs.append(_repair_offer(selected="retry")) status, detail = scheduler.resolve_repair("r1") assert status == "running" assert "retrying" in detail assert any(e["kind"] == "repair_retry" for e in scheduler.trace.read()) def test_resolve_repair_ask_user_question_then_answer_starts_repair(tmp_path): scheduler = _scheduler( tmp_path, codegen=CodegenClient(base_url="http://127.0.0.1:1/v1", model="test", timeout=2), ) _install_tracking_stub(scheduler) scheduler.repairs.append(_repair_offer(selected="ask_user")) status, _ = scheduler.resolve_repair("r1") assert status == "needs_input" pending = scheduler.pending_questions() assert len(pending) == 1 assert pending[0]["kind"] == "repair" status, _ = scheduler.answer_question(pending[0]["id"], "use the app password") assert status == "running" writing = [ e for e in scheduler.trace.read() if e["kind"] == "skill_writing" ] assert writing and writing[-1]["repair"] is True assert any(e["kind"] == "repair_executed" for e in scheduler.trace.read()) def test_repair_budget_is_bounded(tmp_path): scheduler = _scheduler( tmp_path, codegen=CodegenClient(base_url="http://127.0.0.1:1/v1", model="test", timeout=2), ) _install_tracking_stub(scheduler) scheduler.repairs.append(_repair_offer("r1")) assert scheduler.resolve_repair("r1")[0] == "running" scheduler.repairs.append(_repair_offer("r2")) status, detail = scheduler.resolve_repair("r2") assert status == "error" assert "budget" in detail def test_resolve_repair_unknown_offer_errors(tmp_path): scheduler = _scheduler(tmp_path) status, detail = scheduler.resolve_repair("missing") assert status == "error" assert "no repair offer" in detail def test_failed_run_offers_repair(tmp_path): """A `failed` outcome offers a repair, regardless of the reentry budget: it is a broken step, not a "more work" step.""" scheduler = _scheduler(tmp_path) skill = Skill( name="check", category="tracking", description="Check the delivery status of a package.", act=lambda ctx, request: ActionResult(action_log="boom", new_state="x"), ) request = Request("track my package") request.reentries = scheduler.max_reentries outcome = RunResult( skill="tracking.check", success=False, summary="failed", action_log="connection refused", new_state="unchanged", outcome="failed", ) scheduler._finish_run(skill, request, outcome) assert not any(e["kind"] == "continued" for e in scheduler.trace.read()) assert any(e["kind"] == "repair_offered" for e in scheduler.trace.read()) def test_continue_routes_next_step_carries_ledger(tmp_path): """A `continue` outcome re-dispatches the request with a run-ledger entry and a parent-run link; the next step's ledger leads back to the original query.""" scheduler = _scheduler(tmp_path) skill = Skill( name="check", category="tracking", description="Check the delivery status of a package.", act=lambda ctx, request: ActionResult(action_log="boom", new_state="x"), ) request = Request("track my package") outcome = RunResult( skill="tracking.check", success=True, summary="ok", action_log="tracking opened", new_state="opened", outcome="continue", ) scheduler._finish_run(skill, request, outcome) child = scheduler.queue.pop() assert child is not None assert child.source == "continue" assert child.text == "track my package" assert child.meta["parent_run"] == request.id assert child.run_ledger == [ {"query": "track my package", "skill": "tracking.check", "outcome": "tracking opened"} ] assert any(e["kind"] == "continued" for e in scheduler.trace.read()) def _completed(skill_name="calculator.calculate", log="255 * 12 = 3060"): category, name = skill_name.split(".", 1) skill = Skill( name=name, category=category, description="Do arithmetic.", act=lambda ctx, request: ActionResult(action_log=log, new_state=log), ) outcome = RunResult( skill=skill_name, success=True, summary=f"{skill_name}: ok — {log}", action_log=log, new_state=log, outcome="done", ) return skill, outcome def test_recall_records_and_attaches_recent_run(tmp_path): """A completed real run is remembered per source; a follow-up within the window gets its ledger as `prior_ledger`, rendered as a 'previous request' block and exposed to bodies via last_result.""" scheduler = _scheduler(tmp_path) assert scheduler.query_recall_window == 30 skill, outcome = _completed() first = Request("what is 255*12?", source="typed") first.origin_source = "typed" scheduler._record_recall(skill, first, outcome) follow = Request("send that to Sam", source="typed") follow.origin_source = "typed" scheduler._attach_recall(follow) assert follow.prior_ledger == [ { "query": "what is 255*12?", "skill": "calculator.calculate", "outcome": "255 * 12 = 3060", } ] assert follow.prior_at > 0 assert last_result(follow) == "255 * 12 = 3060" state = compose_state(follow) assert "previous request" in state assert "calculator.calculate -> 255 * 12 = 3060" in state assert any(e["kind"] == "recall_used" for e in scheduler.trace.read()) def test_recall_window_zero_disables(tmp_path): """`query_recall_window: 0` remembers nothing and recalls nothing.""" scheduler = _scheduler(tmp_path, recall_window=0) skill, outcome = _completed() first = Request("what is 255*12?", source="typed") first.origin_source = "typed" scheduler._record_recall(skill, first, outcome) assert scheduler._recall == {} follow = Request("send that to Sam", source="typed") follow.origin_source = "typed" scheduler._attach_recall(follow) assert follow.prior_ledger == [] assert not any(e["kind"] == "recall_used" for e in scheduler.trace.read()) def test_recall_expired_is_not_attached_and_pruned(tmp_path): """A run older than the window is not recalled, and its stale memory is dropped.""" scheduler = _scheduler(tmp_path, recall_window=30) skill, outcome = _completed() first = Request("what is 255*12?", source="typed") first.origin_source = "typed" scheduler._record_recall(skill, first, outcome) scheduler._recall["typed"]["completed_at"] = time.time() - 100 follow = Request("send that to Sam", source="typed") follow.origin_source = "typed" scheduler._attach_recall(follow) assert follow.prior_ledger == [] assert "typed" not in scheduler._recall def test_recall_is_per_source(tmp_path): """Two chats do not recall each other's runs.""" scheduler = _scheduler(tmp_path) skill, outcome = _completed() first = Request("what is 255*12?", source="gateway:a") first.origin_source = "gateway:a" scheduler._record_recall(skill, first, outcome) other = Request("send that to Sam", source="gateway:b") other.origin_source = "gateway:b" scheduler._attach_recall(other) assert other.prior_ledger == [] same = Request("send that to Sam", source="gateway:a") same.origin_source = "gateway:a" scheduler._attach_recall(same) assert same.prior_ledger and same.prior_ledger[-1]["outcome"] == "255 * 12 = 3060" def test_recall_excludes_canned_and_housekeeping(tmp_path): """The canned `response` tree and the housekeeping meta skills are never remembered (a greeting must not occupy the recall slot).""" scheduler = _scheduler(tmp_path) for category in ("response", "housekeeping"): skill = Skill( name="x", category=category, description="d", act=lambda ctx, request: ActionResult("line", "line"), ) outcome = RunResult( skill=f"{category}.x", success=True, summary="ok", action_log="line", new_state="line", outcome="done", ) req = Request("hi", source="typed") req.origin_source = "typed" scheduler._record_recall(skill, req, outcome) assert scheduler._recall == {} def test_finish_run_records_terminal_but_not_intermediate(tmp_path): """`_finish_run` records a terminal run, records a failure, but does not record an intermediate `continue` (the child records the full ledger).""" scheduler = _scheduler(tmp_path) skill, outcome = _completed(skill_name="tracking.check", log="opened") done = Request("track my package", source="typed") done.origin_source = "typed" scheduler._finish_run(skill, done, outcome) assert scheduler._recall["typed"]["ledger"][-1]["skill"] == "tracking.check" # An intermediate continue is re-dispatched, not recorded as terminal. scheduler._recall.clear() cont = Request("track my package", source="typed") cont.origin_source = "typed" cont_outcome = RunResult( skill="tracking.check", success=True, summary="ok", action_log="opened", new_state="opened", outcome="continue", ) result = scheduler._finish_run(skill, cont, cont_outcome) assert result.intermediate is True assert scheduler._recall == {} # A failure is remembered too (a follow-up can refer to what failed). scheduler._recall.clear() failed = Request( "check my inbox", source="typed" ) failed.origin_source = "typed" err = RunResult( skill="mail.check", success=False, summary="", action_log="", new_state="", error="bridge refused", outcome="failed", ) scheduler._finish_run( Skill(name="check", category="mail", description="d", act=lambda ctx, r: ActionResult("", "")), failed, err, ) assert scheduler._recall["typed"]["ledger"][-1]["outcome"] == "bridge refused" def test_continue_carries_recall_context(tmp_path): """A chained continuation keeps the recalled previous-request context and the origin source.""" scheduler = _scheduler(tmp_path) request = Request("send that to Sam", source="typed") request.origin_source = "gateway:a" request.prior_ledger = [ {"query": "what is 255*12?", "skill": "calculator.calculate", "outcome": "3060"} ] request.prior_at = 123.0 scheduler._finish_run( Skill(name="send_message", category="simplex", description="d", act=lambda ctx, r: ActionResult("sent", "sent")), request, RunResult( skill="simplex.send_message", success=True, summary="ok", action_log="sent", new_state="sent", outcome="continue", ), ) child = scheduler.queue.pop() assert child is not None assert child.origin_source == "gateway:a" assert child.prior_ledger == request.prior_ledger assert child.prior_at == 123.0 def test_capped_continue_halts_chain_not_repair(tmp_path): """A `continue` with the step budget spent stops the chain (it worked, so no repair offer) rather than looping.""" scheduler = _scheduler(tmp_path) skill = Skill( name="check", category="tracking", description="Check the delivery status of a package.", act=lambda ctx, request: ActionResult(action_log="boom", new_state="x"), ) request = Request("track my package") request.reentries = scheduler.max_reentries outcome = RunResult( skill="tracking.check", success=True, summary="ok", action_log="not done yet", new_state="partial", outcome="continue", ) scheduler._finish_run(skill, request, outcome) assert scheduler.queue.pop() is None assert not any(e["kind"] == "repair_offered" for e in scheduler.trace.read()) halted = [e for e in scheduler.trace.read() if e["kind"] == "chain_halted"] assert halted and halted[-1]["reason"] == "budget" def test_repeat_continue_halts_chain(tmp_path): """The monotonic guard: a `continue` whose (skill, outcome) already appears in the ledger makes no progress, so the chain stops.""" scheduler = _scheduler(tmp_path) skill = Skill( name="check", category="tracking", description="Check the delivery status of a package.", act=lambda ctx, request: ActionResult(action_log="boom", new_state="x"), ) request = Request("track my package") request.run_ledger = [ {"query": "track my package", "skill": "tracking.check", "outcome": "unchanged"} ] outcome = RunResult( skill="tracking.check", success=True, summary="ok", action_log="unchanged", new_state="unchanged", outcome="continue", ) scheduler._finish_run(skill, request, outcome) assert scheduler.queue.pop() is None halted = [e for e in scheduler.trace.read() if e["kind"] == "chain_halted"] assert halted and halted[-1]["reason"] == "repeat" def test_chain_continues_then_completes(tmp_path): """End-to-end: a `continue` step is re-dispatched into next-step routing with the ledger attached, and the chain ends when the second step reports `done`. The first (intermediate) reply is marked so a `result` front end hides it.""" from semif_agent.decisions import DecisionResult class ChainEngine: """Deterministic two-step chain: step 1 -> continue, step 2 -> done.""" def __init__(self): self.assessments = iter(["continue", "done"]) def call(self, request): ids = [o.id for o in request.options] q = request.question if "additional work to be done" in q: selected = next(self.assessments, "done") elif "logical next step" in q: selected = "demo" elif "what does this skill do" in q: selected = "same" elif "Is this input a request" in q: selected = "action" else: selected = ids[0] if selected not in ids: selected = ids[0] return DecisionResult( request=request, option_ids=ids, probabilities=[1.0 if i == selected else 0.0 for i in ids], ) counter = {"n": 0} def act(ctx, request): counter["n"] += 1 return ActionResult(action_log=f"step {counter['n']} done", new_state="x") from semif_agent.llm import LLMClient scheduler = Scheduler( engine=ChainEngine(), llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config={ "skills": {}, "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), }, trace=TraceLog(str(tmp_path / "runs.jsonl")), ) scheduler.tree["demo"] = [ Skill(name="step", category="demo", description="Do a demo step.", act=act) ] first = scheduler.submit_request("do the thing") assert first.intermediate is True replies = scheduler.run_queue() assert len(replies) == 1 assert replies[0].intermediate is False assert "step 2 done" in replies[0].text events = scheduler.trace.read() assert any(e["kind"] == "continued" for e in events) # The re-dispatched step carried the ledger and the parent link. child = next( e for e in events if e["kind"] == "continued" ) assert child["step"] == 1 def test_compound_request_runs_part_step_then_chains(tmp_path): """End-to-end compound request: the leaf guard accepts the first skill as `part` (not a create), the step runs, assessment says more work remains so the chain continues, and the next step completes the request with the prior result in its ledger. The guard does NOT force continuation — `assess:outcome` is the authority, so a `part` step whose work is actually done stops cleanly.""" from semif_agent.decisions import DecisionResult from semif_agent.llm import LLMClient class CompoundEngine: def __init__(self): # Assessment is the continuation authority: step 1 has more work, # step 2 (calculation already done) finishes the request. self.assessments = iter(["continue", "done"]) def call(self, request): ids = [o.id for o in request.options] q = request.question # The run ledger is rendered as a "steps already taken:" block only # once a step has run, so it is the discriminator between step 1 and # step 2 for the routing decisions. after = "steps already taken" in request.state if "Is this input a request" in q: selected = "action" elif "logical next step" in q: selected = "simplex" if after else "calculator" elif "what does this skill do" in q: selected = "same" if after else "part" elif "additional work to be done" in q: selected = next(self.assessments, "done") else: selected = ids[0] if selected not in ids: selected = ids[0] return DecisionResult( request=request, option_ids=ids, probabilities=[1.0 if i == selected else 0.0 for i in ids], ) ran = [] def calc_act(ctx, request): ran.append("calculate") return ActionResult( action_log="Twenty-two times ten is two hundred twenty.", new_state="220", ) def send_act(ctx, request): ran.append(("send", request.run_ledger[-1]["outcome"])) return ActionResult(action_log="sent to pepper", new_state="sent") scheduler = Scheduler( engine=CompoundEngine(), llm=LLMClient(base_url="http://localhost:1/v1", model="test"), log=DecisionLog(str(tmp_path / "decisions.jsonl")), config={ "skills": {}, "category_registry": str(tmp_path / "categories.json"), "skill_bodies": str(tmp_path / "skills"), "skill_packs": str(tmp_path / "seeds"), }, trace=TraceLog(str(tmp_path / "runs.jsonl")), ) scheduler.tree = { "calculator": [ Skill( name="calculate", category="calculator", description="Do arithmetic.", act=calc_act, ) ], "simplex": [ Skill( name="send_message", category="simplex", description="Send a SimpleX message.", act=send_act, ) ], } first = scheduler.submit_request("send the result of 22 * 10 to pepper on simplex") assert first.intermediate is True assert ran == ["calculate"], ran replies = scheduler.run_queue() assert len(replies) == 1 assert replies[0].intermediate is False # The second step saw the first step's result in its ledger. assert ran[-1] == ("send", "Twenty-two times ten is two hundred twenty."), ran events = scheduler.trace.read() assert any(e["kind"] == "continued" for e in events) guards = [e for e in events if e["kind"] == "intent_guard"] assert [g["verdict"] for g in guards] == ["part", "same"], guards def test_verbosity_defaults_and_decision_lines(tmp_path): """Verbosity defaults to `result`; `decision` attaches the SemIf decision phases (and only then).""" from semif_agent.scheduler import normalize_verbosity assert normalize_verbosity(None) == "result" assert normalize_verbosity("query") == "query" assert normalize_verbosity("bogus") == "result" sched = _scheduler(tmp_path, verbosity="decision") assert sched.verbosity == "decision" skill = Skill( name="probe", category="tracking", description="Probe.", act=lambda ctx, request: ActionResult(action_log="ok", new_state="x"), ) request = Request("probe it") sched._run_skill(skill, request) lines = sched._decision_lines(request.id) assert any(line.startswith("assess:outcome") for line in lines) quiet = _scheduler(tmp_path) assert quiet.verbosity == "result" assert quiet._decision_lines(request.id) == [] def test_result_verbosity_hides_intermediate_reply(capsys): from semif_agent.cli import _print_reply reply = SchedulerReply("ran", "demo.step: ok — step 1", intermediate=True) _print_reply(reply, verbosity="result") assert capsys.readouterr().out == "" _print_reply(reply, verbosity="query") assert "step 1" in capsys.readouterr().out