drop mock_data.json: embed fixtures inline in skill.test.py
The testgen call now replies with plain Python (no JSON envelope), so the model can no longer confuse the envelope with the artifact. skill.test.py carries its fixture data inline as Python literals; the folder layout is now skill.py, skill.test.py, contract.json, config.json. The regen ladder drops the mock option (3-option: code/contract/test) since a fixture fix is a test regen.
This commit is contained in:
1 parent
ddd2495f81
commit
06ccfb77c6
11 files changed
+192
-152
No files matched your search
+5
-5
@@ -119,10 +119,11 @@ def build_scheduler(config: dict) -> tuple[Scheduler, dict]:
|
||||
return check
|
||||
|
||||
def make_regen_decision(run_id: str):
|
||||
"""4-option SemIf decision on a failing skill test: which artifact to
|
||||
regenerate (code, contract, test, mock data). Recorded as a trace-only
|
||||
event (kind `codegen_regen`) — never in the decision log. Engine
|
||||
unavailable at call time degrades to `regen_test`."""
|
||||
"""3-option SemIf decision on a failing skill test: which artifact to
|
||||
regenerate (code, contract, or test — fixture data lives inside the
|
||||
test, so a fixture fix is a test regen). Recorded as a trace-only event
|
||||
(kind `codegen_regen`) — never in the decision log. Engine unavailable
|
||||
at call time degrades to `regen_test`."""
|
||||
def decide(reason: str) -> str:
|
||||
decision = DecisionRequest(
|
||||
state=f"[testgen {codegen.model}] skill test failed. {reason[-1200:]}",
|
||||
@@ -131,7 +132,6 @@ def build_scheduler(config: dict) -> tuple[Scheduler, dict]:
|
||||
Option("regen_code", "Regenerate the skill body code."),
|
||||
Option("regen_test", "Regenerate the test only."),
|
||||
Option("regen_contract", "Regenerate the data contract."),
|
||||
Option("regen_mock_data", "Regenerate the mock data."),
|
||||
],
|
||||
)
|
||||
try:
|
||||
|
||||
+46
-44
@@ -937,7 +937,7 @@ def generate_requirements(
|
||||
return []
|
||||
|
||||
|
||||
# ---- data contract + test artifacts ----
|
||||
# ---- data contract + test ----
|
||||
|
||||
TESTGEN_CONTRACT = Path(__file__).resolve().parent.parent / "TESTGEN.md"
|
||||
CONTRACT_VARIABLE_RE = re.compile(r"[a-z0-9_]+")
|
||||
@@ -961,7 +961,7 @@ def build_testgen_base_prompt(
|
||||
context but produce distinct artifacts.
|
||||
"""
|
||||
system = (
|
||||
"You produce the data contract and test artifacts for a skill body of a "
|
||||
"You produce the data contract and test for a skill body of a "
|
||||
"local agent. The contract below is authoritative: follow it exactly.\n\n"
|
||||
f"{contract_md}"
|
||||
)
|
||||
@@ -1054,18 +1054,19 @@ def build_testgen_request_prompt(
|
||||
target: str | None = None,
|
||||
) -> list[dict]:
|
||||
user = (
|
||||
"Generate the test artifacts now: skill.test.py (stdlib-only, runnable "
|
||||
"as `python skill.test.py`, exit 0 on pass, exercising predict and act "
|
||||
"against mock_data.json in the same folder) and mock_data.json "
|
||||
"(fixtures satisfying the contract above)."
|
||||
"Generate the test now: skill.test.py, a stdlib-only Python script, "
|
||||
"runnable as `python skill.test.py` from the skill folder (exit 0 on "
|
||||
"pass, non-zero on failure). It exercises the skill's predict and act "
|
||||
"against fixture data embedded directly in the test as inline Python "
|
||||
"literals — no external files, no network, no writes outside the folder."
|
||||
)
|
||||
if target:
|
||||
user += f"\nFocus the fix on the {target.removeprefix('regen_')}."
|
||||
if note:
|
||||
user += f"\nNote: {note}"
|
||||
user += (
|
||||
'\nReply with ONLY a JSON object: {"test": "<python code>", '
|
||||
'"mock_data": <json>}. No prose, no markdown fences.'
|
||||
"\nReply with ONLY valid Python for skill.test.py. No prose, no "
|
||||
"markdown fences, no JSON."
|
||||
)
|
||||
return base + [
|
||||
{"role": "assistant", "content": json.dumps(contract)},
|
||||
@@ -1073,40 +1074,39 @@ def build_testgen_request_prompt(
|
||||
]
|
||||
|
||||
|
||||
def parse_testgen_bundle(raw: str) -> tuple[str, dict]:
|
||||
"""Extract and validate (skill.test.py code, mock_data) from a JSON reply."""
|
||||
def parse_skill_test(raw: str) -> str:
|
||||
"""Extract and validate skill.test.py from the model's reply.
|
||||
|
||||
Accepts bare code, ```fenced``` code, or JSON {"code": "..."}. The test
|
||||
must parse as Python. Returns the cleaned source. Raises ValueError
|
||||
otherwise.
|
||||
"""
|
||||
text = raw.strip()
|
||||
start, end = text.find("{"), text.rfind("}")
|
||||
if start == -1 or end == -1:
|
||||
raise ValueError(f"testgen reply is not a JSON object: {raw!r}")
|
||||
try:
|
||||
parsed = json.loads(text[start : end + 1])
|
||||
except ValueError as exc:
|
||||
raise ValueError(f"testgen reply is not valid JSON: {exc}") from exc
|
||||
if not isinstance(parsed, dict):
|
||||
raise ValueError("testgen reply must be a JSON object")
|
||||
test = parsed.get("test")
|
||||
mock = parsed.get("mock_data")
|
||||
if not isinstance(test, str) or not test.strip():
|
||||
raise ValueError("testgen reply must carry a non-empty `test` string")
|
||||
if not isinstance(mock, (dict, list)):
|
||||
raise ValueError("testgen reply must carry `mock_data` as an object or array")
|
||||
if "code" in text[:400] and "{" in text and "}" in text:
|
||||
start, end = text.find("{"), text.rfind("}")
|
||||
try:
|
||||
parsed = json.loads(text[start : end + 1])
|
||||
candidate = parsed.get("code")
|
||||
if isinstance(candidate, str):
|
||||
text = candidate.strip()
|
||||
except (ValueError, AttributeError):
|
||||
pass
|
||||
for fence in ("```python", "```py", "```"):
|
||||
start_idx = test.find(fence)
|
||||
if start_idx != -1:
|
||||
test = test[start_idx + len(fence):]
|
||||
close = test.rfind("```")
|
||||
start = text.find(fence)
|
||||
if start != -1:
|
||||
text = text[start + len(fence):]
|
||||
close = text.rfind("```")
|
||||
if close != -1:
|
||||
test = test[:close]
|
||||
text = text[:close]
|
||||
break
|
||||
test = test.strip()
|
||||
if not test:
|
||||
raise ValueError("testgen reply's test is empty")
|
||||
text = text.strip()
|
||||
if not text:
|
||||
raise ValueError("skill test is empty")
|
||||
try:
|
||||
ast.parse(test, filename="<generated test>")
|
||||
ast.parse(text, filename="<generated test>")
|
||||
except SyntaxError as exc:
|
||||
raise ValueError(f"skill test is not valid Python: {exc}") from exc
|
||||
return test, mock
|
||||
return text
|
||||
|
||||
|
||||
def generate_skill_tests(
|
||||
@@ -1119,13 +1119,14 @@ def generate_skill_tests(
|
||||
contract_md: str | None = None,
|
||||
reason: str | None = None,
|
||||
target: str | None = None,
|
||||
) -> tuple[str, dict]:
|
||||
"""Generate skill.test.py + mock_data.json against the finished body.
|
||||
) -> str:
|
||||
"""Generate skill.test.py against the finished body.
|
||||
|
||||
Shares the base context with the contract call (TESTGEN.md + skill.py) and
|
||||
continues it with the accepted contract, so the artifacts stay compatible
|
||||
with the body and the contract. Escalates on parse failure; `reason` +
|
||||
`target` drive a corrective regeneration.
|
||||
continues it with the accepted contract, so the test stays compatible with
|
||||
the body and the contract. Fixture data is embedded inline in the test.
|
||||
Escalates on parse failure; `reason` + `target` drive a corrective
|
||||
regeneration.
|
||||
"""
|
||||
contract_text = contract_md if contract_md is not None else read_testgen_contract()
|
||||
attempts = max(client.max_attempts, 1)
|
||||
@@ -1148,7 +1149,7 @@ def generate_skill_tests(
|
||||
messages,
|
||||
**(ESCALATED_SAMPLER if attempt > 1 else {}),
|
||||
)
|
||||
return parse_testgen_bundle(raw)
|
||||
return parse_skill_test(raw)
|
||||
except ValueError as exc:
|
||||
last_error = exc
|
||||
raise ValueError(f"testgen reply rejected {attempts} times: {last_error}")
|
||||
@@ -1225,9 +1226,10 @@ def regenerate_skill_body(
|
||||
def run_skill_test(skill_dir: str | Path, timeout: float = 30.0) -> tuple[bool, str]:
|
||||
"""Run skill.test.py in its folder as a subprocess.
|
||||
|
||||
cwd is the skill folder so mock_data.json is reachable at ./mock_data.json.
|
||||
Returns (passed, captured output). A non-zero exit or a timeout is a
|
||||
failure; the output feeds the regen decision and corrective prompts.
|
||||
cwd is the skill folder so the test can import the `skill` module; its
|
||||
fixture data is embedded inline, so no external files are needed. Returns
|
||||
(passed, captured output). A non-zero exit or a timeout is a failure; the
|
||||
output feeds the regen decision and corrective prompts.
|
||||
"""
|
||||
cwd = Path(skill_dir)
|
||||
script = cwd / "skill.test.py"
|
||||
|
||||
@@ -638,8 +638,8 @@ class Scheduler:
|
||||
|
||||
def _write_skill_body(self, job: SkillWrite) -> None:
|
||||
"""Run the full authoring pipeline for one queued write (no scheduler
|
||||
lock held here): codegen body -> data contract -> test artifacts ->
|
||||
auto-run test (with a SemIf regen ladder on failure).
|
||||
lock held here): codegen body -> data contract -> test -> auto-run test
|
||||
(with a SemIf regen ladder on failure).
|
||||
|
||||
The tree is snapshotted under the lock so the prompt build reads a
|
||||
stable view even if the main thread merges another skill meanwhile.
|
||||
@@ -678,11 +678,12 @@ class Scheduler:
|
||||
self._complete_skill_write(job, code)
|
||||
|
||||
def _test_and_fix(self, job: SkillWrite, code: str, contract: dict) -> None:
|
||||
"""Generate test artifacts and auto-run them; regen the failing piece.
|
||||
"""Generate the test artifact and auto-run it; regen the failing piece.
|
||||
|
||||
On failure a SemIf decision picks which of code/contract/test/mock data
|
||||
to regenerate; whichever it is, the error and the existing files are fed
|
||||
back into the corrective call. Bounded by `test_max_attempts`.
|
||||
On failure a SemIf decision picks which of code/contract/test to
|
||||
regenerate; whichever it is, the error and the existing files are fed
|
||||
back into the corrective call. Fixture data lives inside the test, so a
|
||||
fixture fix is a test regen. Bounded by `test_max_attempts`.
|
||||
"""
|
||||
attempts = max(self.test_max_attempts, 1)
|
||||
reason: str | None = None
|
||||
@@ -698,12 +699,11 @@ class Scheduler:
|
||||
self.codegen, job.request, job.category, job.draft, code, reason=reason
|
||||
)
|
||||
self.body_store.write_contract(job.category, job.draft.name, contract)
|
||||
test, mock = generate_skill_tests(
|
||||
test = generate_skill_tests(
|
||||
self.codegen, job.request, job.category, job.draft, code, contract,
|
||||
reason=reason, target=target,
|
||||
)
|
||||
self.body_store.write_test(job.category, job.draft.name, test)
|
||||
self.body_store.write_mock(job.category, job.draft.name, mock)
|
||||
passed, output = run_skill_test(
|
||||
self.body_store.dir(job.category, job.draft.name), timeout=self.test_timeout
|
||||
)
|
||||
|
||||
@@ -176,9 +176,9 @@ class SkillStore:
|
||||
"""Persists one skill leaf as a folder of deliverables.
|
||||
|
||||
Layout: <base>/<category>/<name>/{skill.py, skill.test.py, contract.json,
|
||||
mock_data.json, config.json}. skill.py is the runnable body; the rest are
|
||||
produced by the contract/test generation steps and read back so skills stay
|
||||
runnable and configurable across restarts. The old single-file layout
|
||||
config.json}. skill.py is the runnable body; the rest are produced by the
|
||||
contract/test generation steps and read back so skills stay runnable and
|
||||
configurable across restarts. The old single-file layout
|
||||
(<base>/<category>/<name>.py) is NOT read — this is a clean switch.
|
||||
"""
|
||||
|
||||
@@ -198,9 +198,6 @@ class SkillStore:
|
||||
def write_contract(self, category: str, name: str, contract: dict) -> Path:
|
||||
return self._write_json(category, name, "contract.json", contract)
|
||||
|
||||
def write_mock(self, category: str, name: str, mock) -> Path:
|
||||
return self._write_json(category, name, "mock_data.json", mock)
|
||||
|
||||
def write_config(self, category: str, name: str, config: dict) -> Path:
|
||||
return self._write_json(category, name, "config.json", config)
|
||||
|
||||
@@ -224,9 +221,6 @@ class SkillStore:
|
||||
def read_config(self, category: str, name: str) -> dict:
|
||||
return self._read_json(category, name, "config.json")
|
||||
|
||||
def read_mock(self, category: str, name: str):
|
||||
return self._read_json(category, name, "mock_data.json")
|
||||
|
||||
def _read_json(self, category: str, name: str, filename: str):
|
||||
path = self.dir(category, name) / filename
|
||||
if not path.is_file():
|
||||
|
||||
Reference in new issue
Block a user