ship BRAINSTORM item 1: folder skill store, contract+testgen, config tiering
Skill leaves become folders (skill.py, skill.test.py, contract.json, mock_data.json, config.json); clean switch, old single-file layout ignored. SKILL.md owns the runner-data doctrine (no mock data); TESTGEN.md owns mocking/testing. Async authoring pipeline: codegen body -> decoupled contract gen -> shared-context testgen -> subprocess auto-run test with a 4-option SemIf regen ladder (trace-only). Opt-in requirements elicitation (REPL asker) with anti-pattern guardrails. Config search auto-populates skill config via SemIf (global -> category); runner resolves tiers into ctx.config and pauses pre-predict for unresolved contract vars with record/ask-again choices.
This commit is contained in:
1 parent
927f66b696
commit
ddd2495f81
14 files changed
+1946
-164
No files matched your search
@@ -13,7 +13,7 @@ from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from semif_agent.cli import build_scheduler, load_config
|
||||
from semif_agent.codegen import CodegenClient, generate_skill_body
|
||||
from semif_agent.codegen import CodegenClient, generate_data_contract, generate_skill_body, generate_skill_tests
|
||||
from semif_agent.decisions import Request
|
||||
from semif_agent.dream import dream
|
||||
from semif_agent.engine import EngineUnavailable
|
||||
@@ -22,8 +22,8 @@ from semif_agent.skills import (
|
||||
CategoryDraft,
|
||||
Prediction,
|
||||
Skill,
|
||||
SkillBodyStore,
|
||||
SkillDraft,
|
||||
SkillStore,
|
||||
build_skills,
|
||||
build_tree,
|
||||
generate_category,
|
||||
@@ -155,7 +155,8 @@ def test_generate_skill(tmp_path):
|
||||
|
||||
|
||||
def test_generate_skill_body_codegen(tmp_path):
|
||||
"""A real OpenAI-compatible model writes a runnable skill body.
|
||||
"""A real OpenAI-compatible model writes a runnable skill body + data
|
||||
contract + test artifacts, and the auto-run test passes.
|
||||
|
||||
Slow: uses the big codegen model (qwen38-iq3s by default). Run this one in
|
||||
the background and poll — long-lived ssh sessions get SIGHUP'd.
|
||||
@@ -182,10 +183,35 @@ def test_generate_skill_body_codegen(tmp_path):
|
||||
tree,
|
||||
)
|
||||
print(f"generated {len(code)} bytes of skill body")
|
||||
contract = generate_data_contract(
|
||||
client, Request("is my home server reachable right now?"), "tracking", draft, code
|
||||
)
|
||||
print(f"data contract: {contract}")
|
||||
test, mock = generate_skill_tests(
|
||||
client,
|
||||
Request("is my home server reachable right now?"),
|
||||
"tracking",
|
||||
draft,
|
||||
code,
|
||||
contract,
|
||||
)
|
||||
print(f"generated {len(test)} bytes of skill test, mock={mock!r}")
|
||||
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
store.write_body("tracking", draft.name, code)
|
||||
store.write_contract("tracking", draft.name, contract)
|
||||
store.write_test("tracking", draft.name, test)
|
||||
store.write_mock("tracking", draft.name, mock)
|
||||
from semif_agent.codegen import run_skill_test
|
||||
|
||||
passed, output = run_skill_test(store.dir("tracking", draft.name), timeout=60)
|
||||
print(f"auto-run test: passed={passed}\n{output[:400]}")
|
||||
assert passed, "the auto-run test must pass against the mock data"
|
||||
|
||||
draft.code = code
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
skill = materialize_skill(draft, "tracking", store)
|
||||
assert callable(skill.predict) and callable(skill.act)
|
||||
assert skill.contract == contract
|
||||
|
||||
|
||||
def test_create_skill_empty_category_does_not_wedge(tmp_path):
|
||||
|
||||
+369
-34
@@ -20,22 +20,32 @@ from semif_agent.codegen import (
|
||||
CodegenError,
|
||||
DegenerationError,
|
||||
_retry_prompt,
|
||||
build_elicitation_prompt,
|
||||
build_skill_body_prompt,
|
||||
generate_data_contract,
|
||||
generate_requirements,
|
||||
generate_skill_body,
|
||||
generate_skill_tests,
|
||||
parse_data_contract,
|
||||
parse_elicitation,
|
||||
parse_skill_body,
|
||||
parse_testgen_bundle,
|
||||
read_skill_contract,
|
||||
read_testgen_contract,
|
||||
regenerate_skill_body,
|
||||
run_skill_test,
|
||||
skill_contract_ref,
|
||||
)
|
||||
from semif_agent.decisions import Request
|
||||
from semif_agent.skills import (
|
||||
SkillBodyStore,
|
||||
SkillDraft,
|
||||
SkillStore,
|
||||
build_skills,
|
||||
build_tree,
|
||||
load_skill_module,
|
||||
materialize_skill,
|
||||
merge_skill_bodies,
|
||||
merge_registry,
|
||||
merge_skill_store,
|
||||
)
|
||||
|
||||
GOOD_BODY = """\
|
||||
@@ -49,6 +59,21 @@ def act(ctx, request, prediction):
|
||||
return ActionResult(action_log="probe ran", new_state=request.text)
|
||||
"""
|
||||
|
||||
GOOD_CONTRACT = {
|
||||
"sender_address": "The email address the message is sent from.",
|
||||
"tracking_id": "The package tracking number.",
|
||||
}
|
||||
|
||||
GOOD_MOCK = {"sender_address": "agent@example.com", "tracking_id": "AB123"}
|
||||
|
||||
GOOD_TEST = """\
|
||||
import sys
|
||||
print("ok")
|
||||
sys.exit(0)
|
||||
"""
|
||||
|
||||
GOOD_BUNDLE = json.dumps({"test": GOOD_TEST, "mock_data": GOOD_MOCK})
|
||||
|
||||
|
||||
def test_read_skill_contract_loads_contract():
|
||||
text = read_skill_contract()
|
||||
@@ -84,13 +109,25 @@ def test_skill_contract_ref_degrades_off_repo(tmp_path):
|
||||
skill_contract_ref(str(tmp_path / "missing.md"))
|
||||
|
||||
|
||||
def test_contract_directs_reusable_self_mocked_skills():
|
||||
"""SKILL.md must tell the model that a body is a reusable module owning its
|
||||
own source of truth, not a one-shot that leans on the human for data."""
|
||||
def test_contract_directs_runner_provided_data():
|
||||
"""SKILL.md must tell the model that data comes from the runner via
|
||||
ctx.config — never embedded, fabricated, or asked of the human. Mock-data
|
||||
directives belong in TESTGEN.md, not here."""
|
||||
text = read_skill_contract()
|
||||
assert "Reusable module with its own data model" in text
|
||||
assert "internal/mock data model" in text
|
||||
assert "for clarifying questions only" in text
|
||||
assert "Data comes from the runner, never from you" in text
|
||||
assert "`ctx.config`" in text
|
||||
assert "never embed or fabricate working values" in text
|
||||
assert "internal/mock data model" not in text
|
||||
|
||||
|
||||
def test_testgen_contract_owns_mocking():
|
||||
"""Mocking/testing has its own contract: TESTGEN.md. It must define the
|
||||
flat semantic contract shape and forbid structure/type declarations."""
|
||||
text = read_testgen_contract()
|
||||
assert "contract.json" in text
|
||||
assert "single JSON object" in text
|
||||
assert "semantic description" in text
|
||||
assert "Do NOT put type declarations" in text
|
||||
|
||||
|
||||
def test_build_skill_body_prompt_includes_contract_request_and_draft():
|
||||
@@ -107,19 +144,36 @@ def test_build_skill_body_prompt_includes_contract_request_and_draft():
|
||||
assert "tracking.check" in joined
|
||||
|
||||
|
||||
def test_build_skill_body_prompt_directs_reuse_and_self_mock_data():
|
||||
def test_build_skill_body_prompt_directs_runner_provided_data():
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
messages = build_skill_body_prompt(
|
||||
Request("check if the service is up"), "tracking", draft, tree, "THE CONTRACT"
|
||||
)
|
||||
joined = messages[1]["content"]
|
||||
assert "reused across many requests" in joined
|
||||
assert "internal/mock data model" in joined
|
||||
assert "ask only to clarify intent" in joined
|
||||
assert "owns no working data" in joined
|
||||
assert "`ctx.config`" in joined
|
||||
assert "never embed or fabricate working values" in joined
|
||||
assert "internal/mock data model" not in joined
|
||||
|
||||
|
||||
def test_retry_prompt_directs_reuse_and_self_mock_data():
|
||||
def test_build_skill_body_prompt_includes_requirements():
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
messages = build_skill_body_prompt(
|
||||
Request("check if the service is up"),
|
||||
"tracking",
|
||||
draft,
|
||||
tree,
|
||||
"THE CONTRACT",
|
||||
requirements={"Should it actually send, or draft first?": "draft first"},
|
||||
)
|
||||
joined = messages[1]["content"]
|
||||
assert "Requirements gathered from the product owner" in joined
|
||||
assert "draft first" in joined
|
||||
|
||||
|
||||
def test_retry_prompt_directs_runner_provided_data():
|
||||
messages = _retry_prompt(
|
||||
Request("check if the service is up"),
|
||||
"tracking",
|
||||
@@ -128,9 +182,10 @@ def test_retry_prompt_directs_reuse_and_self_mock_data():
|
||||
ValueError("skill body is empty"),
|
||||
)
|
||||
joined = messages[1]["content"]
|
||||
assert "reused across many requests" in joined
|
||||
assert "internal/mock data model" in joined
|
||||
assert "ask only to clarify intent" in joined
|
||||
assert "owns no working data" in joined
|
||||
assert "`ctx.config`" in joined
|
||||
assert "never embed or fabricate working values" in joined
|
||||
assert "internal/mock data model" not in joined
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -169,64 +224,102 @@ def test_parse_skill_body_rejects_missing_act():
|
||||
|
||||
|
||||
def test_body_store_roundtrip(tmp_path):
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
assert store.list_bodies() == []
|
||||
store.write("tracking", "probe", GOOD_BODY)
|
||||
assert store.list_bodies() == [("tracking", "probe")]
|
||||
target = store.body_path("tracking", "probe")
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
assert store.list_skills() == []
|
||||
store.write_body("tracking", "probe", GOOD_BODY)
|
||||
assert store.list_skills() == [("tracking", "probe")]
|
||||
target = store.dir("tracking", "probe") / "skill.py"
|
||||
assert target.is_file()
|
||||
assert "def predict" in target.read_text()
|
||||
|
||||
|
||||
def test_store_roundtrip_all_deliverables(tmp_path):
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
store.write_body("tracking", "probe", GOOD_BODY)
|
||||
store.write_contract("tracking", "probe", GOOD_CONTRACT)
|
||||
store.write_mock("tracking", "probe", GOOD_MOCK)
|
||||
store.write_test("tracking", "probe", GOOD_TEST)
|
||||
store.write_config("tracking", "probe", {"sender_address": "agent@example.com"})
|
||||
directory = store.dir("tracking", "probe")
|
||||
assert sorted(p.name for p in directory.iterdir()) == [
|
||||
"config.json",
|
||||
"contract.json",
|
||||
"mock_data.json",
|
||||
"skill.py",
|
||||
"skill.test.py",
|
||||
]
|
||||
assert store.read_contract("tracking", "probe") == GOOD_CONTRACT
|
||||
assert store.read_config("tracking", "probe") == {"sender_address": "agent@example.com"}
|
||||
assert store.read_mock("tracking", "probe") == GOOD_MOCK
|
||||
assert store.read_category_config("tracking") == {}
|
||||
|
||||
|
||||
def test_store_ignores_legacy_single_file_layout(tmp_path):
|
||||
"""Clean switch: a skill written as <category>/<name>.py is NOT read."""
|
||||
legacy = tmp_path / "skills" / "tracking"
|
||||
legacy.mkdir(parents=True)
|
||||
(legacy / "probe.py").write_text(GOOD_BODY)
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
assert store.list_skills() == []
|
||||
|
||||
|
||||
def test_load_skill_module_exposes_predict_act(tmp_path):
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
store.write("tracking", "probe", GOOD_BODY)
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
store.write_body("tracking", "probe", GOOD_BODY)
|
||||
module = load_skill_module("tracking", "probe", store.path)
|
||||
assert callable(module.predict) and callable(module.act)
|
||||
|
||||
|
||||
def test_materialize_skill_builds_runnable_skill(tmp_path):
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
store.write_contract("tracking", "probe", GOOD_CONTRACT)
|
||||
store.write_config("tracking", "probe", {"sender_address": "agent@example.com"})
|
||||
draft = SkillDraft(name="probe", description="Probe the service.", code=GOOD_BODY)
|
||||
skill = materialize_skill(draft, "tracking", store)
|
||||
assert skill.name == "probe"
|
||||
assert skill.category == "tracking"
|
||||
assert callable(skill.predict) and callable(skill.act)
|
||||
assert skill.contract == GOOD_CONTRACT
|
||||
assert skill.config == {"sender_address": "agent@example.com"}
|
||||
|
||||
|
||||
def test_materialize_skill_requires_code(tmp_path):
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
with pytest.raises(ValueError):
|
||||
materialize_skill(draft, "tracking", store)
|
||||
|
||||
|
||||
def test_materialize_skill_rejects_import_failure(tmp_path):
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
bad = "def predict(ctx, request):\n return None\n"
|
||||
draft = SkillDraft(name="probe", description="Probe.", code=bad)
|
||||
with pytest.raises(ValueError):
|
||||
materialize_skill(draft, "tracking", store)
|
||||
|
||||
|
||||
def test_merge_skill_bodies_upgrades_stub(tmp_path):
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
store.write("tracking", "probe", GOOD_BODY)
|
||||
def test_merge_skill_store_upgrades_stub(tmp_path):
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
store.write_body("tracking", "probe", GOOD_BODY)
|
||||
store.write_contract("tracking", "probe", GOOD_CONTRACT)
|
||||
store.write_config("tracking", "probe", {"sender_address": "agent@example.com"})
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
registry = {"tracking": {"description": "", "skills": [{"name": "probe", "description": "Probe."}]}}
|
||||
merge_registry(tree, registry)
|
||||
upgraded = merge_skill_bodies(tree, store, registry)
|
||||
upgraded = merge_skill_store(tree, store, registry)
|
||||
assert upgraded == 1
|
||||
skill = next(s for s in tree["tracking"] if s.name == "probe")
|
||||
assert callable(skill.predict) and callable(skill.act)
|
||||
assert skill.description == "Probe."
|
||||
assert skill.contract == GOOD_CONTRACT
|
||||
assert skill.config == {"sender_address": "agent@example.com"}
|
||||
|
||||
|
||||
def test_merge_skill_bodies_creates_missing_category(tmp_path):
|
||||
store = SkillBodyStore(str(tmp_path / "skills"))
|
||||
store.write("brand_new", "ping", GOOD_BODY)
|
||||
def test_merge_skill_store_creates_missing_category(tmp_path):
|
||||
store = SkillStore(str(tmp_path / "skills"))
|
||||
store.write_body("brand_new", "ping", GOOD_BODY)
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
upgraded = merge_skill_bodies(tree, store, {})
|
||||
upgraded = merge_skill_store(tree, store, {})
|
||||
assert upgraded == 1
|
||||
assert tree["brand_new"][0].name == "ping"
|
||||
|
||||
@@ -1284,6 +1377,248 @@ def test_other_codegen_error_does_not_retry():
|
||||
client, Request("is the service up?"), "tracking", draft, tree
|
||||
)
|
||||
assert httpd.RequestHandlerClass.chat_calls == 1
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
# ---- elicitation ----
|
||||
|
||||
def test_elicitation_prompt_contains_examples_and_antipatterns():
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
messages = build_elicitation_prompt(
|
||||
Request("is the service up?"), "tracking", draft, tree, max_questions=3
|
||||
)
|
||||
system = messages[0]["content"]
|
||||
joined = messages[1]["content"]
|
||||
assert "requirements only" in system
|
||||
assert "should the sender address change?" in system
|
||||
assert "product goal" in system
|
||||
assert "tracking" in joined
|
||||
for example in (
|
||||
"just produce a draft you review and approve first",
|
||||
"should the skill ask you which one, or automatically pick",
|
||||
"report 'not found' as a normal result",
|
||||
"a short message back to you, or a file/report saved to disk",
|
||||
):
|
||||
assert example in system
|
||||
|
||||
|
||||
def test_parse_elicitation():
|
||||
assert parse_elicitation('{"questions": ["A?", "B?"]}') == ["A?", "B?"]
|
||||
assert parse_elicitation('Sure:\n{"questions": [" A? "]}') == ["A?"]
|
||||
assert parse_elicitation('{"questions": []}') == []
|
||||
with pytest.raises(ValueError):
|
||||
parse_elicitation("not json")
|
||||
with pytest.raises(ValueError):
|
||||
parse_elicitation('{"questions": "nope"}')
|
||||
|
||||
|
||||
def test_generate_requirements_end_to_end():
|
||||
httpd, base = _sequenced_server(['{"questions": ["Draft or send?", "Which one?"]}'])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10)
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
questions = generate_requirements(
|
||||
client, Request("is the service up?"), "tracking", draft, tree, max_questions=3
|
||||
)
|
||||
assert questions == ["Draft or send?", "Which one?"]
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_generate_requirements_truncates_to_max():
|
||||
httpd, base = _sequenced_server(['{"questions": ["A?", "B?", "C?", "D?"]}'])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10)
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
questions = generate_requirements(
|
||||
client, Request("is the service up?"), "tracking", draft, tree, max_questions=2
|
||||
)
|
||||
assert questions == ["A?", "B?"]
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_generate_requirements_degrades_on_parse_failure():
|
||||
"""Elicitation must never block authoring: a garbage reply degrades to []."""
|
||||
httpd, base = _sequenced_server(["this is not json at all"])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10, max_attempts=2)
|
||||
tree = build_tree(build_skills({"skills": {}}))
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
assert generate_requirements(
|
||||
client, Request("is the service up?"), "tracking", draft, tree
|
||||
) == []
|
||||
assert httpd.RequestHandlerClass.chat_calls == 2
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
# ---- data contract ----
|
||||
|
||||
def test_parse_data_contract_flat_object():
|
||||
contract = parse_data_contract(json.dumps(GOOD_CONTRACT))
|
||||
assert contract == GOOD_CONTRACT
|
||||
contract = parse_data_contract("Here:\n" + json.dumps(GOOD_CONTRACT))
|
||||
assert contract == GOOD_CONTRACT
|
||||
assert parse_data_contract("{}") == {}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw",
|
||||
[
|
||||
'"just a string"',
|
||||
'{"sender": {"type": "string"}}',
|
||||
'{"sender": ""}',
|
||||
'{"Sender Address": "the sender"}',
|
||||
"not json at all",
|
||||
],
|
||||
)
|
||||
def test_parse_data_contract_rejects_bad_shape(raw):
|
||||
with pytest.raises(ValueError):
|
||||
parse_data_contract(raw)
|
||||
|
||||
|
||||
def test_generate_data_contract_end_to_end(tmp_path):
|
||||
httpd, base = _sequenced_server([json.dumps(GOOD_CONTRACT)])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10)
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
contract = generate_data_contract(
|
||||
client, Request("is the service up?"), "tracking", draft, GOOD_BODY
|
||||
)
|
||||
assert contract == GOOD_CONTRACT
|
||||
sent = httpd.RequestHandlerClass.received[0]
|
||||
joined = " ".join(m["content"] for m in sent["messages"])
|
||||
assert read_testgen_contract() in sent["messages"][0]["content"]
|
||||
assert "data contract" in joined
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_generate_data_contract_escalates_on_bad_parse():
|
||||
httpd, base = _sequenced_server(["not json", json.dumps(GOOD_CONTRACT)])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10)
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
contract = generate_data_contract(
|
||||
client, Request("is the service up?"), "tracking", draft, GOOD_BODY
|
||||
)
|
||||
assert contract == GOOD_CONTRACT
|
||||
reqs = httpd.RequestHandlerClass.received
|
||||
assert len(reqs) == 2
|
||||
assert reqs[1]["temperature"] == 0.5 # escalated sampler
|
||||
assert reqs[1]["presence_penalty"] == 2.0
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_generate_data_contract_exhausts_attempts(tmp_path):
|
||||
httpd, base = _sequenced_server(["not json"])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10, max_attempts=2)
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
with pytest.raises(ValueError, match="rejected 2 times"):
|
||||
generate_data_contract(
|
||||
client, Request("is the service up?"), "tracking", draft, GOOD_BODY
|
||||
)
|
||||
assert httpd.RequestHandlerClass.chat_calls == 2
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
# ---- test artifacts ----
|
||||
|
||||
def test_parse_testgen_bundle_accepts_forms():
|
||||
test, mock = parse_testgen_bundle(GOOD_BUNDLE)
|
||||
assert test == GOOD_TEST.strip()
|
||||
assert mock == GOOD_MOCK
|
||||
fenced = json.dumps({"test": "```python\n" + GOOD_TEST + "```", "mock_data": GOOD_MOCK})
|
||||
test, mock = parse_testgen_bundle(fenced)
|
||||
assert test == GOOD_TEST.strip()
|
||||
assert mock == GOOD_MOCK
|
||||
with pytest.raises(ValueError):
|
||||
parse_testgen_bundle('{"test": "def x(:"}')
|
||||
with pytest.raises(ValueError):
|
||||
parse_testgen_bundle('{"mock_data": {}}')
|
||||
with pytest.raises(ValueError):
|
||||
parse_testgen_bundle('{"test": "", "mock_data": {}}')
|
||||
|
||||
|
||||
def test_generate_skill_tests_shares_contract_context(tmp_path):
|
||||
"""Decoupled call: the testgen call shares the contract call's base context
|
||||
and continues it with the accepted contract as the assistant turn."""
|
||||
httpd, base = _sequenced_server([GOOD_BUNDLE])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10)
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
test, mock = generate_skill_tests(
|
||||
client, Request("is the service up?"), "tracking", draft, GOOD_BODY, GOOD_CONTRACT
|
||||
)
|
||||
assert test == GOOD_TEST.strip()
|
||||
assert mock == GOOD_MOCK
|
||||
messages = httpd.RequestHandlerClass.received[0]["messages"]
|
||||
roles = [m["role"] for m in messages]
|
||||
assert roles == ["system", "user", "assistant", "user"]
|
||||
assert messages[2]["content"] == json.dumps(GOOD_CONTRACT)
|
||||
assert "skill.test.py" in messages[3]["content"]
|
||||
assert "mock_data.json" in messages[3]["content"]
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_run_skill_test_verdict(tmp_path):
|
||||
skill_dir = tmp_path / "skills" / "tracking" / "probe"
|
||||
skill_dir.mkdir(parents=True)
|
||||
(skill_dir / "skill.test.py").write_text(GOOD_TEST)
|
||||
passed, _ = run_skill_test(skill_dir)
|
||||
assert passed
|
||||
(skill_dir / "skill.test.py").write_text("import sys\nprint('boom')\nsys.exit(3)\n")
|
||||
passed, output = run_skill_test(skill_dir)
|
||||
assert not passed
|
||||
assert "boom" in output
|
||||
|
||||
|
||||
def test_run_skill_test_timeout(tmp_path):
|
||||
skill_dir = tmp_path / "skills" / "tracking" / "probe"
|
||||
skill_dir.mkdir(parents=True)
|
||||
(skill_dir / "skill.test.py").write_text("import time\ntime.sleep(5)\n")
|
||||
passed, output = run_skill_test(skill_dir, timeout=0.5)
|
||||
assert not passed
|
||||
assert "timed out" in output
|
||||
|
||||
|
||||
def test_run_skill_test_missing_script(tmp_path):
|
||||
passed, output = run_skill_test(tmp_path)
|
||||
assert not passed
|
||||
assert "no skill.test.py" in output
|
||||
|
||||
|
||||
def test_regenerate_skill_body_rewrites(tmp_path):
|
||||
httpd, base = _sequenced_server([GOOD_BODY])
|
||||
try:
|
||||
client = CodegenClient(base_url=base, model="test", timeout=10)
|
||||
draft = SkillDraft(name="probe", description="Probe the service.")
|
||||
code = regenerate_skill_body(
|
||||
client, Request("is the service up?"), "tracking", draft, GOOD_BODY,
|
||||
"test failed with an error",
|
||||
)
|
||||
assert "def predict" in code and "def act" in code
|
||||
sent = httpd.RequestHandlerClass.received[0]
|
||||
joined = " ".join(m["content"] for m in sent["messages"])
|
||||
assert "test failed with an error" in joined
|
||||
assert "Previous body" in joined
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
+242
-11
@@ -26,8 +26,10 @@ from semif_agent.skills import (
|
||||
CategoryDraft,
|
||||
CategoryRegistry,
|
||||
CreateCategory,
|
||||
Prediction,
|
||||
Skill,
|
||||
SkillDraft,
|
||||
SkillStore,
|
||||
build_category_prompt,
|
||||
build_skill_prompt,
|
||||
build_skills,
|
||||
@@ -38,6 +40,8 @@ from semif_agent.skills import (
|
||||
navigate,
|
||||
parse_category_draft,
|
||||
parse_skill_draft,
|
||||
resolve_skill_config,
|
||||
unresolved_variables,
|
||||
)
|
||||
from semif_agent.trace import TraceLog
|
||||
|
||||
@@ -53,15 +57,27 @@ def act(ctx, request, prediction):
|
||||
return ActionResult(action_log="probe ran", new_state=request.text)
|
||||
"""
|
||||
|
||||
GOOD_TEST = """\
|
||||
import sys
|
||||
print("ok")
|
||||
sys.exit(0)
|
||||
"""
|
||||
|
||||
def _fake_codegen_server() -> tuple[ThreadingHTTPServer, str]:
|
||||
"""Single-shot OpenAI-compatible SSE server standing in for ollama. Also
|
||||
answers `/api/show` so the client's context-window probe succeeds."""
|
||||
GOOD_BUNDLE = json.dumps({"test": GOOD_TEST, "mock_data": {}})
|
||||
|
||||
|
||||
def _pipeline_codegen_server(replies: list) -> tuple[ThreadingHTTPServer, str]:
|
||||
"""Sequenced OpenAI-compatible SSE server for the full authoring pipeline:
|
||||
codegen body -> data contract -> test artifacts. 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)
|
||||
self.rfile.read(length)
|
||||
raw = self.rfile.read(length).decode("utf-8")
|
||||
if self.path.endswith("/api/show"):
|
||||
body = json.dumps({"parameters": {"num_ctx": 4242}}).encode("utf-8")
|
||||
self.send_response(200)
|
||||
@@ -70,8 +86,12 @@ def _fake_codegen_server() -> tuple[ThreadingHTTPServer, str]:
|
||||
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": GOOD_BODY}}]}
|
||||
{"choices": [{"delta": {"content": reply}}]}
|
||||
) + "\n\n"
|
||||
body = (frame + "data: [DONE]\n\n").encode("utf-8")
|
||||
self.send_response(200)
|
||||
@@ -83,7 +103,8 @@ def _fake_codegen_server() -> tuple[ThreadingHTTPServer, str]:
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
httpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||||
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"
|
||||
|
||||
@@ -385,10 +406,11 @@ def test_run_skill_guards_stub_and_writing_leaves(tmp_path):
|
||||
|
||||
|
||||
def test_async_skill_write_materializes_merges_and_requeues(tmp_path):
|
||||
"""The full async path: _start_skill_write flags the leaf in-progress and
|
||||
the single-slot worker materializes the body, hot-merges it into the tree,
|
||||
and re-queues the original request for re-dispatch."""
|
||||
httpd, base = _fake_codegen_server()
|
||||
"""The full async pipeline: _start_skill_write flags the leaf in-progress
|
||||
and the single-slot worker runs codegen -> 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_BUNDLE])
|
||||
try:
|
||||
scheduler = _scheduler(tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10))
|
||||
scheduler.tree["tracking"] = [
|
||||
@@ -422,6 +444,215 @@ def test_async_skill_write_materializes_merges_and_requeues(tmp_path):
|
||||
# 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 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",
|
||||
"mock_data.json",
|
||||
"skill.py",
|
||||
"skill.test.py",
|
||||
]
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_async_skill_write_fails_gracefully_on_bad_contract(tmp_path):
|
||||
"""A contract reply that never parses (after the escalation ladder) leaves
|
||||
the leaf a restartable stub — no silent no-op, no wedged worker."""
|
||||
httpd, base = _pipeline_codegen_server([GOOD_BODY, "this is not a contract"])
|
||||
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."), 0.5)
|
||||
|
||||
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"
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_skill_pre_predict_contract_pause_and_resume(tmp_path):
|
||||
"""A contract variable the runner cannot satisfy pauses BEFORE predict;
|
||||
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)
|
||||
scheduler.tree["tracking"] = []
|
||||
store = scheduler.body_store
|
||||
store.write_contract("tracking", "track_live", {"token": "The tracking token."})
|
||||
|
||||
seen = []
|
||||
|
||||
def predict(ctx, request):
|
||||
return Prediction(text="", decisions=[])
|
||||
|
||||
def act(ctx, request, prediction):
|
||||
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.",
|
||||
predict=predict,
|
||||
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_predict is True
|
||||
assert scheduler.pending.prediction is None
|
||||
|
||||
status, detail = scheduler.answer("AB123")
|
||||
assert status == "needs_input"
|
||||
assert scheduler.pending is not None
|
||||
assert scheduler.pending.pre_predict is False
|
||||
assert scheduler.pending.request.meta["config_answers"]["token"] == "AB123"
|
||||
assert "Confirm?" in scheduler.pending.question
|
||||
|
||||
status, detail = scheduler.answer("yes")
|
||||
assert status == "ran"
|
||||
assert scheduler.pending is None
|
||||
|
||||
|
||||
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_elicit_requirements_asks_and_records(tmp_path):
|
||||
"""Opt-in elicitation asks the product owner refinement questions before
|
||||
the body is written and rides the answers on the draft into the body prompt."""
|
||||
httpd, base = _pipeline_codegen_server(
|
||||
['{"questions": ["Draft or send?"]}', GOOD_BODY, "{}", GOOD_BUNDLE]
|
||||
)
|
||||
try:
|
||||
scheduler = _scheduler(tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10))
|
||||
scheduler.elicitation_enabled = True
|
||||
answers = []
|
||||
scheduler.asker = lambda q: (answers.append(q), "draft first")[1]
|
||||
draft = SkillDraft(name="track_live", description="Follow a package.")
|
||||
scheduler._elicit_requirements(Request("track my package"), "tracking", draft)
|
||||
assert answers == ["Draft or send?"]
|
||||
assert draft.requirements == {"Draft or send?": "draft first"}
|
||||
assert any(e["kind"] == "requirements" for e in scheduler.trace.read())
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_elicit_requirements_disabled_skips(tmp_path):
|
||||
httpd, base = _pipeline_codegen_server([GOOD_BODY, "{}", GOOD_BUNDLE])
|
||||
try:
|
||||
scheduler = _scheduler(tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10))
|
||||
scheduler.elicitation_enabled = False
|
||||
scheduler.asker = lambda q: "answer"
|
||||
draft = SkillDraft(name="track_live", description="Follow a package.")
|
||||
scheduler._elicit_requirements(Request("track my package"), "tracking", draft)
|
||||
assert draft.requirements == {}
|
||||
assert httpd.RequestHandlerClass.chat_calls == 0
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
def test_elicit_requirements_asker_none_stops(tmp_path):
|
||||
"""An asker that stops answering (None) halts collection without error."""
|
||||
httpd, base = _pipeline_codegen_server(
|
||||
['{"questions": ["A?", "B?"]}', GOOD_BODY, "{}", GOOD_BUNDLE]
|
||||
)
|
||||
try:
|
||||
scheduler = _scheduler(tmp_path, codegen=CodegenClient(base_url=base, model="test", timeout=10))
|
||||
scheduler.elicitation_enabled = True
|
||||
asked = []
|
||||
scheduler.asker = lambda q: (asked.append(q), None)[1]
|
||||
draft = SkillDraft(name="track_live", description="Follow a package.")
|
||||
scheduler._elicit_requirements(Request("track my package"), "tracking", draft)
|
||||
assert asked == ["A?"]
|
||||
assert draft.requirements == {}
|
||||
finally:
|
||||
httpd.shutdown()
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
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_bundle = json.dumps({"test": "import sys\nsys.exit(1)", "mock_data": {}})
|
||||
httpd, base = _pipeline_codegen_server(
|
||||
[GOOD_BODY, "{}", failing_bundle, GOOD_BUNDLE]
|
||||
)
|
||||
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."), 0.5
|
||||
)
|
||||
|
||||
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()
|
||||
Reference in new issue
Block a user