authoring UX: sequential elicitation questions + optional creation approval

Elicitation now asks one question at a time: only one PendingQuestion is
live, and the next is posted only after the current is answered (an empty
answer is accepted) or skipped. Each question gets a fresh
codegen.elicitation.answer_timeout (default 30s) that resets on every answer
submit; a timeout stops the sequence. questions_timeout is now traced only on
a real timeout. Front ends need no change — they read pending_questions().

Add opt-in creation_approval: the single-slot llm draft worker blocks after
authoring a proposal and before anything is registered; approve/deny via the
REPL, dashboard (/api/approvals + UI), or a gateway chat reply (yes/no). One
prompt per creation request (a category approval covers its chained skill);
denial or timeout aborts creation. Off by default.

Tests updated for both; full stdlib suite passes (337).
This commit is contained in:
gabby committed 2026-10-01 23:57:03 +00:00
1 parent 25ade160df
commit 215c22afb8
10 files changed
+684 -67

No files matched your search

+33 -1
View File
@@ -16,7 +16,7 @@ from semif_agent.decisions import DecisionRequest, DecisionResult, Option, Reque
from semif_agent.engine import EngineConfig, SemIfEngine
from semif_agent.llm import LLMClient
from semif_agent.log import DecisionLog
from semif_agent.scheduler import PendingQuestion, RepairOffer, Scheduler
from semif_agent.scheduler import PendingApproval, PendingQuestion, RepairOffer, Scheduler
from semif_agent.skills import ActionResult, Skill
from semif_agent.trace import TraceLog
@@ -356,11 +356,43 @@ def test_status_carries_questions_and_repairs(tmp_path):
selected="retry", reason="boom", request_text="track", failure="boom",
)
)
scheduler.approvals.append(
PendingApproval(
id="a1", run_id="run-1", kind="skill", category="tracking",
skill="track_live", description="Follow a package.",
)
)
server = Server(scheduler)
try:
status, payload = server.get("/api/status")
assert payload["questions"][0]["id"] == "q1"
assert payload["repairs"][0]["id"] == "r1"
assert payload["approvals"][0]["id"] == "a1"
finally:
server.close()
def test_approvals_endpoint_roundtrip(tmp_path):
scheduler = build_scheduler(tmp_path)
scheduler.approvals.append(
PendingApproval(
id="a1", run_id="run-1", kind="category", category="travel",
skill="", description="Trips.",
)
)
server = Server(scheduler)
try:
status, payload = server.get("/api/approvals")
assert status == 200
assert payload["approvals"][0]["id"] == "a1"
assert payload["approvals"][0]["kind"] == "category"
status, payload = server.post("/api/approvals", {"id": "a1", "approve": True})
assert payload["status"] == "ok"
assert scheduler.pending_approvals() == []
status, payload = server.post("/api/approvals", {"id": "missing", "approve": False})
assert payload["status"] == "error"
finally:
server.close()
+26 -1
View File
@@ -19,7 +19,7 @@ from semif_agent.gateway.service import GatewayService
from semif_agent.gateway.simplex import SimplexAdapter
from semif_agent.llm import LLMClient
from semif_agent.log import DecisionLog
from semif_agent.scheduler import Scheduler
from semif_agent.scheduler import PendingApproval, Scheduler
from semif_agent.skills import ActionResult, Skill
from semif_agent.trace import TraceLog
@@ -251,3 +251,28 @@ def test_service_answered_question_routes_back(tmp_path):
service.handle_inbound(InboundMessage(text="work", chat_id="4", contact_id="4"))
assert not scheduler.pending_questions()
def test_service_surfaces_and_resolves_approval(tmp_path):
scheduler = build_scheduler(tmp_path)
service = build_service(scheduler)
scheduler.approvals.append(
PendingApproval(
id="a1",
run_id="r1",
kind="skill",
category="tracking",
skill="track_live",
description="Follow a package.",
)
)
service._owners["r1"] = "4"
service.surface()
assert any(
"Approve creating new skill tracking.track_live" in text
for text in drain_outbound(service)
)
service.handle_inbound(InboundMessage(text="yes", chat_id="4", contact_id="4"))
assert scheduler.pending_approvals() == []
assert any("approved" in text for text in drain_outbound(service))
+205 -12
View File
@@ -24,6 +24,7 @@ from semif_agent.llm import LLMClient, LLMError
from semif_agent.log import DecisionLog
from semif_agent.skill import RunResult
from semif_agent.scheduler import (
DraftAuthor,
PendingQuestion,
RepairOffer,
Scheduler,
@@ -963,6 +964,162 @@ def test_dispatch_create_category_queues_draft_and_is_not_fatal(tmp_path):
assert scheduler.fatal is None
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_approval_off_proceeds(tmp_path):
"""The default (no `creation_approval`) leaves creation unchanged: the gate
returns immediately and posts nothing."""
scheduler = _scheduler(tmp_path)
job = DraftAuthor(
request=Request("book a trip"), category=None, weight=0.5, kind="category"
)
assert scheduler._request_approval(job, "category", "travel", "Trips.") is True
assert scheduler.pending_approvals() == []
def test_creation_approval_approved_proceeds(tmp_path):
scheduler = _scheduler(tmp_path)
scheduler.creation_approval = True
scheduler.defer_questions = True
job = DraftAuthor(
request=Request("book a trip"), category=None, weight=0.5, 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.creation_approval = True
scheduler.defer_questions = True
job = DraftAuthor(
request=Request("book a trip"), category="travel", weight=0.5, 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.creation_approval = True
scheduler.defer_questions = True
scheduler.elicitation_wait = 0.1
job = DraftAuthor(
request=Request("book a trip"), category="travel", weight=0.5, 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.creation_approval = True
scheduler.defer_questions = False
job = DraftAuthor(
request=Request("book a trip"), category="travel", weight=0.5, 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.creation_approval = True
scheduler.defer_questions = True
job = DraftAuthor(
request=Request("book a trip"),
category="travel",
weight=0.5,
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.creation_approval = True
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, weight=0.5, 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.creation_approval = True
scheduler.defer_questions = True
monkeypatch.setattr(
"semif_agent.scheduler.generate_skill",
lambda client, request, category, tree: SkillDraft(
name="book_flight", description="Book a flight."
),
)
job = DraftAuthor(
request=Request("book a flight"), category="travel", weight=0.5, 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`."""
@@ -1291,9 +1448,9 @@ def test_answer_question_unknown_id_errors(tmp_path):
assert "no pending question" in detail
def test_post_questions_waits_for_answers(tmp_path):
def test_post_questions_asks_one_at_a_time(tmp_path):
scheduler = _scheduler(tmp_path)
scheduler.elicitation_wait = 5.0
scheduler.elicitation_answer_timeout = 5.0
job = SkillWrite(
request=Request("track it"),
category="tracking",
@@ -1305,26 +1462,62 @@ def test_post_questions_waits_for_answers(tmp_path):
target=lambda: result.update(scheduler._post_questions(job, ["Q1?", "Q2?"]))
)
thread.start()
def next_pending(deadline: float) -> list:
while time.monotonic() < deadline:
pending = scheduler.pending_questions()
if pending:
return pending
time.sleep(0.02)
return []
deadline = time.monotonic() + 5
pending = []
while time.monotonic() < deadline:
pending = scheduler.pending_questions()
if len(pending) == 2:
break
time.sleep(0.02)
assert len(pending) == 2
scheduler.answer_question(pending[0]["id"], "A1")
scheduler.answer_question(pending[1]["id"], "")
first = next_pending(deadline)
assert [q["question"] for q in first] == ["Q1?"]
scheduler.answer_question(first[0]["id"], "A1")
second = next_pending(deadline)
assert [q["question"] for q in second] == ["Q2?"]
scheduler.answer_question(second[0]["id"], "")
thread.join(timeout=5)
assert result == {"Q1?": "A1"}
assert scheduler.pending_questions() == []
kinds = [e["kind"] for e in scheduler.trace.read()]
assert "questions_asked" in kinds
# An empty answer is accepted (the question is dismissed), not a timeout.
assert "questions_timeout" not in kinds
def test_post_questions_timeout_drops_remaining(tmp_path):
scheduler = _scheduler(tmp_path)
scheduler.elicitation_answer_timeout = 0.2
job = SkillWrite(
request=Request("track it"),
category="tracking",
draft=SkillDraft(name="probe", description="Probe."),
weight=0.5,
)
result: dict = {}
thread = threading.Thread(
target=lambda: result.update(scheduler._post_questions(job, ["Q1?", "Q2?"]))
)
thread.start()
deadline = time.monotonic() + 3
while time.monotonic() < deadline:
pending = scheduler.pending_questions()
if pending:
scheduler.answer_question(pending[0]["id"], "A1")
break
time.sleep(0.02)
thread.join(timeout=3)
assert result == {"Q1?": "A1"}
assert scheduler.pending_questions() == []
events = [e for e in scheduler.trace.read() if e["kind"] == "questions_timeout"]
assert events and events[-1]["answered"] == 1 and events[-1]["asked"] == 2
def test_post_questions_times_out(tmp_path):
scheduler = _scheduler(tmp_path)
scheduler.elicitation_wait = 0.1
scheduler.elicitation_answer_timeout = 0.1
job = SkillWrite(
request=Request("track it"),
category="tracking",