finally getting accurate answers
This commit is contained in:
@@ -183,6 +183,40 @@ Implements just enough of the aipi surface:
|
||||
phrases are disjoint substrings — the phase-71 ordering
|
||||
convention); no existing E2E question or fixture file contains the
|
||||
phrase, so every other suite is unaffected.
|
||||
- user message containing ``what are the correct llama.cpp
|
||||
arguments`` (``GREP_TEACH_TRIGGER``, the 2026-09-05 incident —
|
||||
the harness prior is that grep takes a REGEX; this app's grep is a
|
||||
case-insensitive fixed substring, owner-locked A5) **and** the
|
||||
system prompt carries the ``<tools>`` section -> the deterministic
|
||||
GREP-REGEX-TEACHING flow, discriminated statelessly from the
|
||||
messages (streaming only):
|
||||
* request 1 (``tools`` offered, no ``tool``-role result yet):
|
||||
stream ONLY ``tool_calls`` deltas — ``grep`` with
|
||||
``{"pattern": GREP_TEACH_PATTERN}`` (``qwen.*3\\.8``, id
|
||||
``call_0``) — the incident's regex-shaped first grep, which a
|
||||
fixed-substring grep can NEVER match;
|
||||
* request 2 (the last tool result is the server's TEACHING
|
||||
no-match line — it carries ``GREP_TEACH_MARKER``):
|
||||
``grep`` with the plain form ``GREP_TEACH_PLAIN``
|
||||
(``qwen3.8``, id ``call_1``) — the one-round correction;
|
||||
* request 3 (the last tool result carries
|
||||
``source/path:line: text`` match lines): ``read`` the FIRST
|
||||
match line's document by its combined ``source/path`` (id
|
||||
``call_2``);
|
||||
* request 4 (the last tool result is a read result, the
|
||||
``"Document <combined>:\n<content>"`` shape): the
|
||||
deterministic echo answer ``Read <combined>. <first 80
|
||||
chars>``, ``finish_reason: "stop"`` — the loop ended in ONE
|
||||
correction, not at the round cap.
|
||||
* A PLAIN no-match as the last result (no match line, no
|
||||
teaching marker — e.g. the plain pattern genuinely absent) is
|
||||
the deterministic terminal answer ``No matches — the knowledge
|
||||
base has no such text.`` (the flow cannot loop on a
|
||||
well-formed pattern).
|
||||
Checked BEFORE the SEARCH / TOOLS_TRIGGER flows (disjoint trigger
|
||||
phrases — the phase-72 ordering convention); no existing E2E
|
||||
question or fixture file contains the phrase, so every other suite
|
||||
is unaffected.
|
||||
- user message containing ``show me a table`` (phase 44, markdown
|
||||
tables, TODO.md L6) -> the fixed table answer (``TABLE_ANSWER``):
|
||||
a 3-column service table, an ``<img onerror>`` XSS probe line, and
|
||||
@@ -483,6 +517,34 @@ assert _CORRECTION_MARKER in CORRECTION_INSTRUCTION, (
|
||||
#: file contains the phrase, so every other suite is unaffected.
|
||||
LS_TEACH_TRIGGER = "list the files in this directory"
|
||||
|
||||
#: The deterministic GREP-REGEX-TEACH flow (the 2026-09-05 "Qwen 3.8"
|
||||
#: incident — the harness prior is that grep takes a REGEX; this app's
|
||||
#: grep is a case-insensitive fixed substring, owner-locked A5, so a
|
||||
#: regex-shaped pattern can NEVER match, and the bare no-match line
|
||||
#: made the turbo model trust the miss and end the turn with a wrong
|
||||
#: "I searched the entire knowledge base" refusal). The flow pins the
|
||||
#: self-correction on the SSE wire: the regex-shaped first grep → the
|
||||
#: server's TEACHING no-match line (``GREP_TEACH_MARKER``) → the
|
||||
#: plain-form retry grep → the match → the read → the deterministic
|
||||
#: echo answer. Checked BEFORE the SEARCH / TOOLS_TRIGGER flows
|
||||
#: (disjoint trigger phrases — the phase-71/72 ordering convention);
|
||||
#: verified: no existing E2E question or fixture file contains the
|
||||
#: phrase, so every other suite is unaffected.
|
||||
GREP_TEACH_TRIGGER = "what are the correct llama.cpp arguments"
|
||||
|
||||
#: The incident's regex-shaped first grep (it can never match a
|
||||
#: fixed-substring grep — that is the point of the flow).
|
||||
GREP_TEACH_PATTERN = "qwen.*3\\.8"
|
||||
|
||||
#: The plain-form retry — the server's teaching line hands over exactly
|
||||
#: this hint (``app.rag.agent.plain_form(GREP_TEACH_PATTERN)``).
|
||||
GREP_TEACH_PLAIN = "qwen3.8"
|
||||
|
||||
#: The marker of the agent's teaching no-match line (app.rag.agent
|
||||
#: ``NO_MATCHES_REGEX`` / ``NO_MATCHES_REGEX_SCOPED``) — the mock's
|
||||
#: plain step keys on it (a plain no-match line carries it not).
|
||||
GREP_TEACH_MARKER = "grep matches a plain substring"
|
||||
|
||||
#: The agent's ``ls`` listing header (app.rag.agent ``_execute_tool``):
|
||||
#: ``"N documents:"`` — the first line of every catalog tool result.
|
||||
_CATALOG_HEADER_RE = re.compile(r"^\d+ documents:")
|
||||
@@ -836,6 +898,61 @@ def _ls_teach_flow(body: dict[str, Any]) -> tuple[str, ...] | None:
|
||||
return ("misuse",)
|
||||
|
||||
|
||||
def _grep_teach_flow(body: dict[str, Any]) -> tuple[str, ...] | None:
|
||||
"""Classify a GREP-TEACH request (the 2026-09-05 incident — see the
|
||||
``GREP_TEACH_*`` constants). Stateless over the messages, like the
|
||||
other marker flows:
|
||||
|
||||
* ``("regex",)`` — ``tools`` are offered and no ``tool``-role result
|
||||
is in the messages yet: the incident's regex-shaped first grep —
|
||||
``grep`` with ``{"pattern": GREP_TEACH_PATTERN}`` (id ``call_0``).
|
||||
* ``("plain",)`` — the LAST tool result is the server's TEACHING
|
||||
no-match line (it carries ``GREP_TEACH_MARKER``): the one-round
|
||||
correction — ``grep`` with the plain form (id ``call_1``).
|
||||
* ``("read", combined, "call_2")`` — the last tool result carries
|
||||
``source/path:line: text`` match lines: ``read`` the FIRST match
|
||||
line's document by its combined ``source/path`` identity.
|
||||
* ``("answer", combined, content)`` — the last tool result is a
|
||||
read result (``"Document <combined>:\n<content>"``): the
|
||||
deterministic echo answer ``Read <combined>. <first 80 chars>``.
|
||||
* ``("nomatch",)`` — the last tool result is a PLAIN no-match (no
|
||||
match line, no teaching marker): the deterministic terminal
|
||||
``No matches — the knowledge base has no such text.`` answer.
|
||||
* ``None`` — not the flow: the trigger is absent, the ``<tools>``
|
||||
section is missing (deflected turns never carry it), or ``tools``
|
||||
are not offered and no tool results are in the messages yet (e.g.
|
||||
``agent_max_rounds=0``).
|
||||
"""
|
||||
user = _user(body).lower()
|
||||
if GREP_TEACH_TRIGGER not in user:
|
||||
return None
|
||||
if "<tools>" not in _system(body):
|
||||
return None
|
||||
results = [
|
||||
str(m.get("content") or "")
|
||||
for m in _messages(body)
|
||||
if m.get("role") == "tool"
|
||||
]
|
||||
if not results:
|
||||
if not body.get("tools"):
|
||||
return None
|
||||
return ("regex",)
|
||||
last = results[-1]
|
||||
if last.startswith(_READ_RESULT_PREFIX):
|
||||
head, _, content = last.partition("\n")
|
||||
# The read result is ``"Document <combined>:\n<content>"`` — the
|
||||
# head carries the server's appended ``:`` (removed here; a
|
||||
# document path never legitimately ends with one).
|
||||
return ("answer", head[len(_READ_RESULT_PREFIX):].removesuffix(":"), content)
|
||||
if GREP_TEACH_MARKER in last:
|
||||
return ("plain",)
|
||||
for line in last.splitlines():
|
||||
m = _SEARCH_LINE_RE.match(line)
|
||||
if m:
|
||||
return ("read", m.group("sp"), "call_2")
|
||||
return ("nomatch",)
|
||||
|
||||
|
||||
def long_answer() -> str:
|
||||
"""~900-word deterministic walkthrough (phase 11): numbered steps plus
|
||||
a unique final line that must survive the stream untruncated."""
|
||||
@@ -1307,6 +1424,50 @@ def chat_completions(body: dict[str, Any]) -> Any:
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
# 2026-09-05 (the "Qwen 3.8" incident — the grep regex prior):
|
||||
# the deterministic GREP-TEACH self-correction flow — checked
|
||||
# BEFORE the SEARCH / TOOLS_TRIGGER flows (disjoint trigger
|
||||
# phrases — the phase-72 ordering convention; the trigger needs
|
||||
# the ``<tools>`` section, so deflected turns never hit it).
|
||||
grep_teach = _grep_teach_flow(body)
|
||||
if grep_teach is not None:
|
||||
if grep_teach[0] == "regex":
|
||||
# The incident's misuse, deterministic: the regex-shaped
|
||||
# pattern (it can never match a fixed-substring grep).
|
||||
stream = _tool_call_stream(
|
||||
"grep", {"pattern": GREP_TEACH_PATTERN}, "call_0"
|
||||
)
|
||||
elif grep_teach[0] == "plain":
|
||||
# The one-round correction: the plain-form retry (the
|
||||
# teaching line handed over exactly this hint).
|
||||
stream = _tool_call_stream(
|
||||
"grep", {"pattern": GREP_TEACH_PLAIN}, "call_1"
|
||||
)
|
||||
elif grep_teach[0] == "read":
|
||||
stream = _tool_call_stream(
|
||||
"read", {"path": grep_teach[1]}, grep_teach[2]
|
||||
)
|
||||
elif grep_teach[0] == "nomatch":
|
||||
stream = _sse_stream(
|
||||
_apply_max_tokens(
|
||||
"No matches — the knowledge base has no such text.",
|
||||
body.get("max_tokens"),
|
||||
),
|
||||
0.0,
|
||||
)
|
||||
else: # "answer" — quote the read document (first 80 chars)
|
||||
stream = _sse_stream(
|
||||
_apply_max_tokens(
|
||||
f"Read {grep_teach[1]}. {grep_teach[2][:80]}",
|
||||
body.get("max_tokens"),
|
||||
),
|
||||
0.0,
|
||||
)
|
||||
return StreamingResponse(
|
||||
stream,
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
# Phase 68 (search tool): the deterministic search marker flow —
|
||||
# checked BEFORE the phase-37 tool flow (the more specific
|
||||
# trigger phrase wins, same convention as THINK_PARAS_TRIGGER).
|
||||
|
||||
Reference in New Issue
Block a user