graph router and router added
Browse files- .claude/settings.local.json +8 -0
- graph.py +3 -0
- prompts.py +16 -7
- router.py +25 -6
- tests/test_prompts.py +23 -0
- tests/test_router.py +33 -0
.claude/settings.local.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"permissions": {
|
| 3 |
+
"allow": [
|
| 4 |
+
"Bash(.venv/Scripts/python.exe -m pytest tests/ -q)",
|
| 5 |
+
"Bash(.venv/Scripts/python.exe -c ' *)"
|
| 6 |
+
]
|
| 7 |
+
}
|
| 8 |
+
}
|
graph.py
CHANGED
|
@@ -69,10 +69,13 @@ def route_action(state, semantic_router=None) -> str:
|
|
| 69 |
"""
|
| 70 |
action = state.get("action")
|
| 71 |
if action == "ats":
|
|
|
|
| 72 |
return "ats_analysis"
|
| 73 |
if action:
|
|
|
|
| 74 |
return "answer"
|
| 75 |
if semantic_router is None:
|
|
|
|
| 76 |
return "answer"
|
| 77 |
|
| 78 |
messages = state.get("messages") or []
|
|
|
|
| 69 |
"""
|
| 70 |
action = state.get("action")
|
| 71 |
if action == "ats":
|
| 72 |
+
logger.info("Route ats_analysis | preset action %r, router bypassed", action)
|
| 73 |
return "ats_analysis"
|
| 74 |
if action:
|
| 75 |
+
logger.info("Route answer | preset action %r, router bypassed", action)
|
| 76 |
return "answer"
|
| 77 |
if semantic_router is None:
|
| 78 |
+
logger.info("Route answer | no router configured")
|
| 79 |
return "answer"
|
| 80 |
|
| 81 |
messages = state.get("messages") or []
|
prompts.py
CHANGED
|
@@ -42,10 +42,16 @@ If the documents genuinely do not cover something, say so directly and stop. "Th
|
|
| 42 |
|
| 43 |
## Output
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
## Candidate documents
|
| 51 |
|
|
@@ -90,9 +96,12 @@ If the excerpts do not address the question at all, say so plainly and stop.
|
|
| 90 |
|
| 91 |
## Style
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
## Retrieved excerpts
|
| 98 |
|
|
|
|
| 42 |
|
| 43 |
## Output
|
| 44 |
|
| 45 |
+
Default to short and scannable. The user is preparing under time pressure and reads on a screen; a long answer buries the one thing they needed.
|
| 46 |
+
|
| 47 |
+
These defaults govern free-form questions. When a request specifies its own format — named headings, a table, a word count, a paragraph count — that format wins and the length ceiling below does not apply.
|
| 48 |
+
|
| 49 |
+
- Open with the direct answer in one sentence. No preamble, no restating the question.
|
| 50 |
+
- Support it with at most four bullets. One line each, roughly fifteen words. Start each with the bolded project, skill, or requirement it is about, so the answer can be skimmed.
|
| 51 |
+
- Ceiling of about 150 words. If you cannot fit the answer, you are including things that were not asked for — cut, do not compress into denser prose.
|
| 52 |
+
- Nothing after the last bullet. No summary, no restatement, no offer of further help.
|
| 53 |
+
- FIT questions: one-line verdict, then one bullet per requirement tagged MET, PARTIAL, or MISSING, then the single probe an interviewer is most likely to press on.
|
| 54 |
+
- Drafted interview answers are the exception to the bullets — first person, natural spoken register, continuous speech, no corporate filler. Still short enough to say out loud in under a minute.
|
| 55 |
|
| 56 |
## Candidate documents
|
| 57 |
|
|
|
|
| 96 |
|
| 97 |
## Style
|
| 98 |
|
| 99 |
+
The question was narrow, so the answer should be too.
|
| 100 |
+
|
| 101 |
+
- Open with the direct answer in one sentence.
|
| 102 |
+
- Add at most three bullets of evidence, one line each, starting with the bolded project, role, or skill each one draws on. Skip the bullets entirely when the opening sentence already settles it.
|
| 103 |
+
- Ceiling of about 80 words.
|
| 104 |
+
- No preamble, no praise, no restating the question, no closing offer of further help.
|
| 105 |
|
| 106 |
## Retrieved excerpts
|
| 107 |
|
router.py
CHANGED
|
@@ -7,8 +7,12 @@ evidence behind it, while sending a greeting to the full profile merely wastes
|
|
| 7 |
tokens. So anything short of a clear, confident match falls back to FULL.
|
| 8 |
"""
|
| 9 |
|
|
|
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
|
|
|
|
|
|
|
| 12 |
TRIVIAL = "trivial"
|
| 13 |
SPECIFIC = "specific"
|
| 14 |
FULL = "full"
|
|
@@ -80,15 +84,30 @@ class SemanticRouter:
|
|
| 80 |
def route(self, query) -> str:
|
| 81 |
# Defensive: an unroutable query must never crash the turn.
|
| 82 |
if not isinstance(query, str) or not query.strip():
|
|
|
|
| 83 |
return FULL
|
| 84 |
|
| 85 |
scores = self.scores(query)
|
| 86 |
ranked = sorted(scores.items(), key=lambda item: item[1], reverse=True)
|
| 87 |
-
(winner, top), (
|
|
|
|
| 88 |
|
| 89 |
-
if winner == FULL:
|
| 90 |
-
return FULL
|
| 91 |
# Leaving the profile behind requires confidence; FULL is the safe default.
|
| 92 |
-
if
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
tokens. So anything short of a clear, confident match falls back to FULL.
|
| 8 |
"""
|
| 9 |
|
| 10 |
+
import logging
|
| 11 |
+
|
| 12 |
import numpy as np
|
| 13 |
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
TRIVIAL = "trivial"
|
| 17 |
SPECIFIC = "specific"
|
| 18 |
FULL = "full"
|
|
|
|
| 84 |
def route(self, query) -> str:
|
| 85 |
# Defensive: an unroutable query must never crash the turn.
|
| 86 |
if not isinstance(query, str) or not query.strip():
|
| 87 |
+
logger.info("Route %s | unroutable query, not scored", FULL)
|
| 88 |
return FULL
|
| 89 |
|
| 90 |
scores = self.scores(query)
|
| 91 |
ranked = sorted(scores.items(), key=lambda item: item[1], reverse=True)
|
| 92 |
+
(winner, top), (runner_up_route, runner_up) = ranked[0], ranked[1]
|
| 93 |
+
margin = top - runner_up
|
| 94 |
|
|
|
|
|
|
|
| 95 |
# Leaving the profile behind requires confidence; FULL is the safe default.
|
| 96 |
+
if winner == FULL:
|
| 97 |
+
chosen, why = FULL, "top match"
|
| 98 |
+
elif top < self.min_score:
|
| 99 |
+
chosen, why = FULL, f"{winner} below min_score {self.min_score}"
|
| 100 |
+
elif margin < self.min_margin:
|
| 101 |
+
chosen, why = FULL, f"{winner} over {runner_up_route} below min_margin {self.min_margin}"
|
| 102 |
+
else:
|
| 103 |
+
chosen, why = winner, "top match"
|
| 104 |
+
|
| 105 |
+
logger.info(
|
| 106 |
+
"Route %s | %s | margin %.3f | %s | query: %.60r",
|
| 107 |
+
chosen,
|
| 108 |
+
why,
|
| 109 |
+
margin,
|
| 110 |
+
" ".join(f"{route}={score:.3f}" for route, score in ranked),
|
| 111 |
+
query,
|
| 112 |
+
)
|
| 113 |
+
return chosen
|
tests/test_prompts.py
CHANGED
|
@@ -31,6 +31,29 @@ def test_ats_action_has_no_inclusion_list_of_technologies():
|
|
| 31 |
assert "ONLY from the text inside the <job_description>" in instruction
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def test_system_prompt_renders_both_sources():
|
| 35 |
rendered = render_system_prompt("PROFILE-MARKER", "JD-MARKER")
|
| 36 |
assert "PROFILE-MARKER" in rendered
|
|
|
|
| 31 |
assert "ONLY from the text inside the <job_description>" in instruction
|
| 32 |
|
| 33 |
|
| 34 |
+
class TestBrevityDefaults:
|
| 35 |
+
"""Chat answers are capped, but the preset actions run long by design and
|
| 36 |
+
inherit the same system prompt, so the cap has to yield to them."""
|
| 37 |
+
|
| 38 |
+
def test_the_chat_prompts_cap_their_length(self):
|
| 39 |
+
from prompts import QA_SYSTEM_PROMPT, RAG_SYSTEM_PROMPT
|
| 40 |
+
|
| 41 |
+
for prompt in (QA_SYSTEM_PROMPT, RAG_SYSTEM_PROMPT):
|
| 42 |
+
assert "Ceiling of about" in prompt
|
| 43 |
+
|
| 44 |
+
def test_a_requested_format_overrides_the_cap(self):
|
| 45 |
+
"""Without this, the 150-word ceiling would truncate a cover letter."""
|
| 46 |
+
from prompts import QA_SYSTEM_PROMPT
|
| 47 |
+
|
| 48 |
+
assert "that format wins and the length ceiling below does not apply" in QA_SYSTEM_PROMPT
|
| 49 |
+
|
| 50 |
+
def test_the_actions_that_run_long_still_state_their_own_format(self):
|
| 51 |
+
from prompts import COVER_LETTER, DETAILED_REVIEW
|
| 52 |
+
|
| 53 |
+
assert "EXACTLY FOUR paragraphs" in COVER_LETTER
|
| 54 |
+
assert "Use these headings" in DETAILED_REVIEW
|
| 55 |
+
|
| 56 |
+
|
| 57 |
def test_system_prompt_renders_both_sources():
|
| 58 |
rendered = render_system_prompt("PROFILE-MARKER", "JD-MARKER")
|
| 59 |
assert "PROFILE-MARKER" in rendered
|
tests/test_router.py
CHANGED
|
@@ -72,6 +72,39 @@ def test_shipped_thresholds_leave_room_for_a_confident_match():
|
|
| 72 |
assert 0 < MIN_MARGIN < 1
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def test_scores_cover_every_route(router):
|
| 76 |
assert set(router.scores("GREET")) == {TRIVIAL, SPECIFIC, FULL}
|
| 77 |
|
|
|
|
| 72 |
assert 0 < MIN_MARGIN < 1
|
| 73 |
|
| 74 |
|
| 75 |
+
class TestRouteLogging:
|
| 76 |
+
"""Answers do not reveal which tier produced them, so the log is the only
|
| 77 |
+
way to tell a full-profile answer from one built on five excerpts."""
|
| 78 |
+
|
| 79 |
+
def test_the_chosen_route_is_logged(self, router, caplog):
|
| 80 |
+
with caplog.at_level("INFO", logger="router"):
|
| 81 |
+
router.route("NARROW")
|
| 82 |
+
assert "Route specific" in caplog.text
|
| 83 |
+
|
| 84 |
+
def test_the_log_records_every_score(self, router, caplog):
|
| 85 |
+
with caplog.at_level("INFO", logger="router"):
|
| 86 |
+
router.route("NARROW")
|
| 87 |
+
for route in (TRIVIAL, SPECIFIC, FULL):
|
| 88 |
+
assert f"{route}=" in caplog.text
|
| 89 |
+
|
| 90 |
+
def test_a_fallback_says_which_rule_rejected_the_winner(self, router, caplog):
|
| 91 |
+
"""'Route full' alone cannot be told apart from a genuine full match."""
|
| 92 |
+
with caplog.at_level("INFO", logger="router"):
|
| 93 |
+
router.route("GREET NARROW")
|
| 94 |
+
assert "min_margin" in caplog.text
|
| 95 |
+
|
| 96 |
+
def test_a_low_score_fallback_names_the_score_rule(self, caplog):
|
| 97 |
+
strict = SemanticRouter(fake_embed, references=REFERENCES, min_score=1.1)
|
| 98 |
+
with caplog.at_level("INFO", logger="router"):
|
| 99 |
+
strict.route("GREET")
|
| 100 |
+
assert "min_score" in caplog.text
|
| 101 |
+
|
| 102 |
+
def test_an_unroutable_query_still_logs(self, router, caplog):
|
| 103 |
+
with caplog.at_level("INFO", logger="router"):
|
| 104 |
+
router.route(None)
|
| 105 |
+
assert "Route full" in caplog.text
|
| 106 |
+
|
| 107 |
+
|
| 108 |
def test_scores_cover_every_route(router):
|
| 109 |
assert set(router.scores("GREET")) == {TRIVIAL, SPECIFIC, FULL}
|
| 110 |
|