response: rebuild as a closed tree of canned replies; drop the gate
There is no up-front handle/ignore filter any more. Every input is scored and dispatched; inputs that are not tasks fall through navigation into the closed response category of hardcoded canned lines (greeting/thanks/ acknowledge/farewell/affirm/unable + the catchall response.clarify). The category is CANNED_CATEGORIES: navigation offers no create_skill there, dispatch/restart refuse to author it, and the runner skips assessment and repair for it (a canned line has no side effect to assess). Removes _contains_request/build_gate_decision and the dropped path.
This commit is contained in:
1 parent
5a6f220e79
commit
22f91e0360
9 files changed
+262
-148
No files matched your search
+30
-54
@@ -1,8 +1,10 @@
|
||||
"""The scheduler: gate, choice, score, queue, dispatch.
|
||||
"""The scheduler: choice, score, queue, dispatch.
|
||||
|
||||
Every SemIf decision (gate, choice, score, navigation, prediction) is logged.
|
||||
Every SemIf decision (choice, score, navigation, prediction) is logged.
|
||||
A high-priority input can preempt the current process, which requeues with its
|
||||
state preserved; a deferred input is scored and queued by urgency.
|
||||
state preserved; a deferred input is scored and queued by urgency. There is no
|
||||
up-front handle/ignore gate: every input is dispatched, and inputs that are not
|
||||
tasks fall through navigation into the closed `response` tree of canned replies.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -35,6 +37,8 @@ from .log import DecisionLog
|
||||
from .queue import UrgencyQueue
|
||||
from .skill import SkillRunner
|
||||
from .skills import (
|
||||
CANNED_CATEGORIES,
|
||||
RESPONSE_FALLBACK,
|
||||
ActionContext,
|
||||
CategoryRegistry,
|
||||
CreateCategory,
|
||||
@@ -54,12 +58,9 @@ from .skills import (
|
||||
merge_seed_store,
|
||||
merge_skill_store,
|
||||
navigate,
|
||||
tree_summary,
|
||||
)
|
||||
from .trace import TraceLog
|
||||
|
||||
GATE_YES = "yes"
|
||||
GATE_CAPABILITY_CHARS = 1200
|
||||
CHOICE_INTERRUPT = "interrupt"
|
||||
URGENCY_OPTIONS = [
|
||||
("critical", "Immediate danger or critical failure."),
|
||||
@@ -70,43 +71,6 @@ URGENCY_OPTIONS = [
|
||||
URGENCY_WEIGHTS = {"critical": 1.0, "high": 0.75, "medium": 0.5, "low": 0.25}
|
||||
|
||||
|
||||
def build_gate_decision(request: Request, tree: dict[str, list[Skill]]) -> DecisionRequest:
|
||||
"""The top-level handle/ignore filter, with end-user expectation and the
|
||||
agent's real capabilities in the state.
|
||||
|
||||
Without them the decision model reads an information request ("tell me the
|
||||
next event in my nextcloud calendar") as small talk and drops it: "actionable"
|
||||
implies a side effect, and the model cannot see that looking a value up in a
|
||||
configured service is exactly what this agent does. The available skills are
|
||||
listed so a request naming one of them is visibly within the agent's remit.
|
||||
"""
|
||||
capabilities = tree_summary(tree)[:GATE_CAPABILITY_CHARS] or " (none yet)"
|
||||
state = (
|
||||
f"User input: {request.text}\n"
|
||||
"[Context: the user is talking to a personal task agent. Any imperative "
|
||||
"request to perform a task — send, message, tell, check, create, remind, "
|
||||
"or look something up — must be handled, even if no skill matches yet. "
|
||||
"Only chatter, thanks, and bare greetings should be ignored.]\n"
|
||||
f"[Available skills:\n{capabilities}]"
|
||||
)
|
||||
return DecisionRequest(
|
||||
state=state,
|
||||
question="Should the agent handle this input?",
|
||||
options=[
|
||||
Option(
|
||||
GATE_YES,
|
||||
"Yes — do what it asks, including looking something up, reporting "
|
||||
"information, or performing an action using the agent's skills.",
|
||||
),
|
||||
Option(
|
||||
"no",
|
||||
"No — it is not a request for the agent to do or answer anything "
|
||||
"(background text, noise, or a bare greeting).",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Process:
|
||||
request: Request
|
||||
@@ -296,14 +260,6 @@ class Scheduler:
|
||||
|
||||
# ---- decision templates (all real SemIf, all logged) ----
|
||||
|
||||
def _contains_request(self, request: Request) -> bool:
|
||||
decision = build_gate_decision(request, self.tree)
|
||||
result = self.engine.call(decision)
|
||||
self.log.append(
|
||||
decision, result, extra={"phase": "gate", "run_id": request.id}
|
||||
)
|
||||
return result.prob(GATE_YES) >= self.tau
|
||||
|
||||
def _choice(self, request: Request, current: Process) -> bool:
|
||||
decision = DecisionRequest(
|
||||
state=compose_state(request, current=current.skill),
|
||||
@@ -362,9 +318,6 @@ class Scheduler:
|
||||
def _submit_locked(self, text: str, source: str = "typed") -> tuple[str, str, str]:
|
||||
request = Request(text, source=source)
|
||||
self.trace.append("submit", request.id, text=text, source=source)
|
||||
if not self._contains_request(request):
|
||||
self.trace.append("dropped", request.id, reason="no actionable request")
|
||||
return "dropped", "no actionable request", request.id
|
||||
|
||||
if self.current is None:
|
||||
weight, label = self._score(request)
|
||||
@@ -473,7 +426,20 @@ class Scheduler:
|
||||
return created
|
||||
return self._dispatch_skill(request, created.skill, weight)
|
||||
if isinstance(navigation, CreateSkill):
|
||||
if navigation.category in CANNED_CATEGORIES:
|
||||
fallback = self._canned_fallback(navigation.category)
|
||||
if fallback is not None:
|
||||
self.trace.append(
|
||||
"create_suppressed",
|
||||
request.id,
|
||||
level="canned_category",
|
||||
category=navigation.category,
|
||||
fallback=fallback.name,
|
||||
)
|
||||
return self._run_skill(fallback, request)
|
||||
return self._dispatch_skill(request, navigation.category, weight)
|
||||
if navigation.category in CANNED_CATEGORIES:
|
||||
return self._run_skill(navigation, request)
|
||||
if not confirm_skill_fit(
|
||||
self.engine, self.log, self.trace, request, navigation, self.tau
|
||||
):
|
||||
@@ -486,6 +452,14 @@ class Scheduler:
|
||||
return self._dispatch_skill(request, navigation.category, weight)
|
||||
return self._run_skill(navigation, request)
|
||||
|
||||
def _canned_fallback(self, category: str) -> Skill | None:
|
||||
"""The catchall canned reply for a closed category, else its first leaf."""
|
||||
skills = self.tree.get(category, [])
|
||||
for skill in skills:
|
||||
if skill.name == RESPONSE_FALLBACK:
|
||||
return skill
|
||||
return skills[0] if skills else None
|
||||
|
||||
def _dispatch_skill(
|
||||
self, request: Request, category: str, weight: float = 0.0
|
||||
) -> DispatchResult:
|
||||
@@ -1587,6 +1561,8 @@ class Scheduler:
|
||||
)
|
||||
if leaf is None:
|
||||
return "error", f"no skill {category}.{name}"
|
||||
if category in CANNED_CATEGORIES:
|
||||
return "error", f"{category} is a closed canned tree; there is no body to write"
|
||||
if leaf.writing:
|
||||
return "error", f"skill {category}.{name} is already being written"
|
||||
if self.codegen is None:
|
||||
|
||||
@@ -23,6 +23,7 @@ from dataclasses import dataclass
|
||||
from .decisions import DecisionRequest, Option, Request
|
||||
from .log import DecisionLog
|
||||
from .skills import (
|
||||
CANNED_CATEGORIES,
|
||||
ActionContext,
|
||||
Prediction,
|
||||
Skill,
|
||||
@@ -301,6 +302,19 @@ class SkillRunner:
|
||||
self, skill: Skill, request: Request, prediction: Prediction | None, action
|
||||
) -> RunResult:
|
||||
observed = action.new_state
|
||||
if skill.category in CANNED_CATEGORIES:
|
||||
summary = deterministic_summary(
|
||||
skill.category, skill.name, True, action.action_log, observed
|
||||
)
|
||||
return RunResult(
|
||||
skill=skill.name,
|
||||
success=True,
|
||||
summary=summary,
|
||||
action_log=action.action_log,
|
||||
new_state=observed,
|
||||
assessment_summary=summary,
|
||||
prediction=prediction,
|
||||
)
|
||||
success, updated_request = self._assess(skill, request, action)
|
||||
|
||||
decisions = getattr(prediction, "decisions", [])
|
||||
|
||||
+111
-7
@@ -470,9 +470,64 @@ def compose_state(request: Request, current: str | None = None) -> str:
|
||||
return " ".join(parts)
|
||||
|
||||
|
||||
def _response_reject(ctx: ActionContext, request: Request, prediction: Prediction) -> ActionResult:
|
||||
message = f"Rejected: I cannot act on this while busy ({request.text})."
|
||||
return ActionResult(action_log=f"response.reject: {message}", new_state=message)
|
||||
def _canned_predict(ctx: ActionContext, request: Request) -> Prediction:
|
||||
return Prediction(text="")
|
||||
|
||||
|
||||
def _canned_response(name: str, message: str):
|
||||
"""An act that returns a fixed line. No transport, no generation, no config."""
|
||||
|
||||
def act(ctx: ActionContext, request: Request, prediction: Prediction) -> ActionResult:
|
||||
return ActionResult(action_log=f"{name}: {message}", new_state=message)
|
||||
|
||||
return act
|
||||
|
||||
|
||||
# The `response` category is a closed tree of canned replies: it never authors a
|
||||
# skill (no create route) and its runs are not assessed (there is no side effect
|
||||
# to succeed or fail). Every non-task input lands here instead of being gated.
|
||||
CANNED_CATEGORIES = frozenset({"response"})
|
||||
RESPONSE_FALLBACK = "response.clarify"
|
||||
|
||||
_CANNED_RESPONSES = [
|
||||
(
|
||||
"greeting",
|
||||
"The user is greeting the agent (hello, hi, good morning).",
|
||||
"Hello! What can I help you with?",
|
||||
),
|
||||
(
|
||||
"thanks",
|
||||
"The user is thanking the agent.",
|
||||
"You're welcome! Anything else?",
|
||||
),
|
||||
(
|
||||
"acknowledge",
|
||||
"The user is acknowledging or making filler the agent should nod to.",
|
||||
"Got it.",
|
||||
),
|
||||
(
|
||||
"farewell",
|
||||
"The user is signing off (bye, goodbye, see you).",
|
||||
"Goodbye! Talk to you later.",
|
||||
),
|
||||
(
|
||||
"affirm",
|
||||
"The user is agreeing or confirming (yes, ok, sure, sounds good).",
|
||||
"Okay!",
|
||||
),
|
||||
(
|
||||
"unable",
|
||||
"A request the agent understands but has no way to perform.",
|
||||
"I'm not able to do that yet.",
|
||||
),
|
||||
(
|
||||
"clarify",
|
||||
"The input is too vague to act on; ask the user to be more specific.",
|
||||
"Could you try being more specific?",
|
||||
),
|
||||
]
|
||||
|
||||
_RESPONSE_MESSAGES = {f"response.{name}": message for name, _, message in _CANNED_RESPONSES}
|
||||
|
||||
|
||||
def build_skills(config: dict) -> list[Skill]:
|
||||
@@ -483,17 +538,24 @@ def build_skills(config: dict) -> list[Skill]:
|
||||
`seeds/`. A fabricated built-in here shadows the authoring path for a real
|
||||
one (navigation routes to it), so keep this list to behaviors that need no
|
||||
external service.
|
||||
|
||||
The `response` category is a closed tree of canned replies (see
|
||||
CANNED_CATEGORIES): it is the catchall for inputs that are not tasks, and it
|
||||
never goes through codegen.
|
||||
"""
|
||||
return [
|
||||
Skill(
|
||||
name="response.reject",
|
||||
name=f"response.{name}",
|
||||
category="response",
|
||||
description="Politely reject a request because the agent is busy.",
|
||||
act=_response_reject,
|
||||
),
|
||||
description=description,
|
||||
predict=_canned_predict,
|
||||
act=_canned_response(f"response.{name}", message),
|
||||
)
|
||||
for name, description, message in _CANNED_RESPONSES
|
||||
]
|
||||
|
||||
|
||||
|
||||
def build_tree(skills: list[Skill]) -> dict[str, list[Skill]]:
|
||||
tree: dict[str, list[Skill]] = {}
|
||||
for skill in skills:
|
||||
@@ -551,6 +613,46 @@ def _clears_create_gate(
|
||||
return create_p >= tau and (create_p - best) >= margin
|
||||
|
||||
|
||||
def _navigate_canned(
|
||||
engine: SemIfEngine,
|
||||
log: DecisionLog,
|
||||
trace: TraceLog,
|
||||
request: Request,
|
||||
category: str,
|
||||
skills: list[Skill],
|
||||
) -> Skill:
|
||||
"""Pick one canned reply from a closed category. No create branch, ever.
|
||||
|
||||
The catchall (`RESPONSE_FALLBACK`) is offered last so the decision question
|
||||
can point at it by name; it is the answer when nothing specific fits.
|
||||
"""
|
||||
ordered = [s for s in skills if not s.is_noop()] or list(skills)
|
||||
fallback = next((s for s in ordered if s.name == RESPONSE_FALLBACK), None)
|
||||
ordered = [s for s in ordered if s is not fallback] + ([fallback] if fallback else [])
|
||||
if len(ordered) == 1:
|
||||
return ordered[0]
|
||||
leaf = DecisionRequest(
|
||||
state=compose_state(request, current=category),
|
||||
question=(
|
||||
"Which canned response best fits this input? "
|
||||
f"Choose {ordered[-1].name} if none of the others does."
|
||||
),
|
||||
options=[Option(s.name, s.description) for s in ordered],
|
||||
)
|
||||
result = engine.call(leaf)
|
||||
log.append(leaf, result, extra={"phase": "navigate:response", "run_id": request.id})
|
||||
chosen = next((s for s in ordered if s.name == result.selected), ordered[-1])
|
||||
trace.append(
|
||||
"response_selected",
|
||||
request.id,
|
||||
category=category,
|
||||
skill=chosen.name,
|
||||
selected=result.selected,
|
||||
probs=result.probs,
|
||||
)
|
||||
return chosen
|
||||
|
||||
|
||||
def navigate(
|
||||
engine: SemIfEngine,
|
||||
log: DecisionLog,
|
||||
@@ -625,6 +727,8 @@ def navigate(
|
||||
)
|
||||
return CreateCategory()
|
||||
skills = tree[category]
|
||||
if category in CANNED_CATEGORIES:
|
||||
return _navigate_canned(engine, log, trace, request, category, skills)
|
||||
create_skill = Option(
|
||||
"create_skill",
|
||||
"No existing skill performs this action; create a new skill for it.",
|
||||
|
||||
Reference in new issue
Block a user