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

+62
View File
@@ -24,12 +24,14 @@ 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,
@@ -157,6 +159,66 @@ def _scheduler(tmp_path, codegen=None, choices=None, default="success", verbosit
)
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_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