codegen: fidelity gate reads the whole body; bound artifacts to engine ctx

The fidelity gate truncated the body to 4000 chars, so a body whose real
action sat after its helpers (the common shape) was judged on a prefix with
no action and wrongly returned `reconsider` -- a false reject that wasted a
rewrite and mislabeled a working skill. Live-engine probe: the truncated
state scored reconsider 0.96; the whole body scores accept 0.98.

- Fidelity gate: feed the whole body plus a deterministic operation
  inventory and the de-identified goal; drop the raw request from the state
  (a remote-engine PII path that added no signal). Ask the positive question
  ("is there a concrete operation that realizes the goal?") and give the gate
  its own `codegen.fidelity.tau` instead of the run-assessment tau.
- Artifact size budget: a decision ingests a generated artifact whole, so
  bound it. `parse_skill_body`/`parse_skill_test` reject an over-budget
  artifact (`ArtifactTooLong`); the codegen retry ladder asks for a shorter
  one. The ceiling derives from the active engine's context
  (`DecisionEngine.context_tokens`: `engine.context_tokens` for the local
  GGUF, `engine.winnow.context_tokens` for Winnow) minus
  `codegen.decision_reserve_tokens`; `codegen.max_body_tokens` /
  `max_test_tokens` override (separate limits).
- `codegen.operation_inventory()`: static HTTP-verb/path, CLI, mail, file,
  and timer inventory, resolving the verb at the call site so a thin-helper
  body's real endpoint is visible.
- Docs: AGENTS.md, README.md, CODEGEN.md, config.example.json.
- Tests: unit coverage for the budget, inventory, retry-shortening, and the
  gate state; integration regression `test_fidelity_gate_reads_the_whole_body`
  (real engine) guards the truncation bug.
This commit is contained in:
gabby committed 2026-10-11 07:33:03 +00:00
1 parent 1833932f1c
commit 9757ae893e
12 files changed
+527 -53

No files matched your search

+84 -1
View File
@@ -17,6 +17,7 @@ import pytest
from semif_agent.codegen import (
BODY_DIRECTIVES,
ArtifactTooLong,
CodegenClient,
CodegenError,
DegenerationError,
@@ -28,6 +29,7 @@ from semif_agent.codegen import (
generate_skill_tests,
infer_integration,
integration_findings,
operation_inventory,
parse_contract,
parse_integration,
parse_skill_body,
@@ -35,6 +37,7 @@ from semif_agent.codegen import (
read_skill_contract,
read_testgen_contract,
regenerate_skill_body,
render_operation_inventory,
run_skill_test,
skill_contract_ref,
)
@@ -1929,4 +1932,84 @@ def test_testgen_prompt_requires_loopback_fixture_and_hermeticity():
joined = messages[-1]["content"]
assert "HERMETIC" in joined
assert "loopback" in joined
assert "never simulate the action" in joined.lower()
assert "never simulate the action" in joined.lower()
# ---- artifact size budget + operation inventory ----
def test_parse_skill_body_enforces_size_budget():
"""A body over the decision-ingestible ceiling is rejected as too long.
The ceiling is hard because a decision (the fidelity gate) reads the body
whole; the retry ladder then asks for a shorter one.
"""
assert parse_skill_body(GOOD_BODY, max_chars=100000) == GOOD_BODY.strip()
with pytest.raises(ArtifactTooLong):
parse_skill_body(GOOD_BODY, max_chars=len(GOOD_BODY) // 2)
# No limit means no rejection.
assert parse_skill_body(GOOD_BODY) == GOOD_BODY.strip()
def test_parse_skill_test_enforces_size_budget():
assert parse_skill_test(GOOD_TEST, max_chars=100000) == GOOD_TEST.strip()
with pytest.raises(ArtifactTooLong):
parse_skill_test(GOOD_TEST, max_chars=10)
def test_retry_prompt_asks_for_a_shorter_body_when_too_long():
draft = SkillDraft(name="probe", description="Probe the service.")
too_long = _retry_prompt("probe", draft, "CONTRACT", ArtifactTooLong("too long"))
assert "SHORTER" in json.dumps(too_long)
looping = _retry_prompt("probe", draft, "CONTRACT", ValueError("bad shape"))
assert "looping" in json.dumps(looping)
def test_operation_inventory_finds_http_verb_and_path_via_helper():
"""The verb + path live at the call site, not the transport helper: the
inventory must surface `POST /tasks/complete` for a thin-helper body."""
code = '''\
import urllib.request
from semif_agent.skills import ActionResult
def _bridge(ctx, method, path, payload=None):
return urllib.request.urlopen(path)
def act(ctx, request):
_bridge(ctx, "POST", "/tasks/complete", payload={"uid": "1"})
return ActionResult(action_log="ok", new_state="ok")
'''
ops = operation_inventory(code)
assert any("HTTP POST /tasks/complete" in op for op in ops), ops
def test_operation_inventory_flags_a_body_with_no_action():
code = '''\
from semif_agent.skills import ActionResult
def act(ctx, request):
return ActionResult(action_log="done", new_state="done")
'''
assert operation_inventory(code) == ["no side-effecting operation found"]
def test_operation_inventory_detects_subprocess_and_file_write():
code = '''\
import subprocess
def act(ctx, request):
subprocess.run(["/bin/ps", "-e"])
with open("out.txt", "w") as handle:
handle.write("x")
'''
ops = operation_inventory(code)
assert any("run CLI" in op for op in ops), ops
assert any("write file" in op for op in ops), ops
def test_render_operation_inventory_is_bulleted():
assert render_operation_inventory(["a", "b"]) == "- a\n- b"