mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-21 22:52:28 +01:00
Phase A of TODO-coach.md (decisions 1-3). The skill code is now a package here with the same behaviour as before. Nothing new is added. ~/.claude/skills/leetcode and ~/.local/bin/leetcode become symlinks into the package, created by scripts/link_leetcode_coach.sh, which install.sh runs. - Package imports replace the bare sibling imports. To stay under the 250-line cap, the Stop-hook detector is split into gate_detector.py and stop_gate.py, the card rules move into card_rules.py, and the prompt text moves into local_prompt.py. - local_coach uses gate_detector directly. It no longer loads the hook file with importlib. - make_cards runs on the system python: a genanki pin (pyproject and requirements) replaces the `uv run --script` header. The note guid is passed to genanki.Note explicitly, so CardNote is no longer subclassed; the guid values are unchanged. - stop_gate also accepts the package path as a skill-load marker, because the harness may print the resolved symlink path. - Every swallowing except now logs a warning. A NullHandler on the package logger keeps stdout/stderr the same as before. - Modules whose prints are their interface (REPL, local session, card builder, fetcher, eval, Stop hook) get T201 per-file ignores, and their tests assert on stdout. - hook_shims/ holds the 3-line ~/.claude/hooks shim. It is excluded from ruff, mypy, pylint and coverage, and a test runs it end to end. - Pre-commit: a requirements-sync entry for each package, leetcode_coach and genanki added to the pylint hook, and the name-tests-test exclude widened. coverage and ruff are bumped to the latest stable releases because the dependency-freshness gate requires it. Verification: 111 coach tests pass with 100% branch coverage. ruff, mypy strict and pylint are clean (10.00). On the 370 assistant replies in 39 real /leetcode transcripts, the old hook and stop_gate give the same verdict (0 mismatches, 20 blocked). fetch_problem output (live two-sum) and make_cards --dry-run output are byte-identical to the old skill, and so are the note guids. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PjvF5DZG9JduSBYKxqyysi
177 lines
5.9 KiB
Python
177 lines
5.9 KiB
Python
"""Transcript parsing and pasteable-snippet detection for the no-solution gate.
|
|
|
|
Shared by the Stop hook (`stop_gate.py`) and the offline coach
|
|
(`local_coach.py`), so both paths judge a reply with one detector. A snippet
|
|
is pasteable when a code span or fenced block uses an identifier from the
|
|
problem's starter signature together with a call/spread/assignment -- unless
|
|
that exact snippet already appears in kuhy's own messages or the fetched
|
|
problem (quoting their code back is fine).
|
|
|
|
Moved verbatim from ``~/.claude/hooks/leetcode_no_solution_stop.py``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import re
|
|
from typing import Any
|
|
|
|
_LOG = logging.getLogger(__name__)
|
|
|
|
KEYWORDS = {
|
|
"function",
|
|
"return",
|
|
"const",
|
|
"let",
|
|
"var",
|
|
"self",
|
|
"def",
|
|
"class",
|
|
"this",
|
|
"new",
|
|
"true",
|
|
"false",
|
|
"null",
|
|
"None",
|
|
"int",
|
|
"str",
|
|
"list",
|
|
"number",
|
|
"string",
|
|
"void",
|
|
}
|
|
CODE_RE = re.compile(r"```.*?```|`[^`\n]+`", re.DOTALL)
|
|
SIG_RE = re.compile(
|
|
r"(?:function\s*(\w*)|def\s+(\w+)|(\w+)\s*=\s*(?:function)?)\s*\(([^)]*)\)"
|
|
)
|
|
# User-role messages that are not kuhy: the injected skill body and the hook's
|
|
# own block reason (which quotes the offending snippet and must not whitelist it).
|
|
NOT_KUHY = ("Base directory for this skill", "Stop hook feedback:")
|
|
MIN_IDENT_LEN = 2 # single letters (`t`) are too common in prose and toy code
|
|
LANG_PREFIXES = ("javascript", "js", "python", "typescript")
|
|
|
|
Entry = dict[str, Any]
|
|
|
|
|
|
def _parse(line: str) -> object:
|
|
"""One JSONL line, or None when it is not JSON (e.g. a half-written tail)."""
|
|
try:
|
|
return json.loads(line)
|
|
except json.JSONDecodeError as exc:
|
|
_LOG.warning("skipping a transcript line that is not JSON: %s", exc)
|
|
return None
|
|
|
|
|
|
def transcript_lines(raw: str) -> list[Entry]:
|
|
"""Every JSON object in a JSONL transcript, in order."""
|
|
return [d for d in map(_parse, raw.splitlines()) if isinstance(d, dict)]
|
|
|
|
|
|
def _content(msg: Entry) -> str | list[Entry]:
|
|
"""The `message.content` field: a string or a list of content blocks."""
|
|
content: str | list[Entry] = msg.get("message", {}).get("content", "")
|
|
return content
|
|
|
|
|
|
def text_of(msg: Entry) -> str:
|
|
"""All text in an entry, tool results included."""
|
|
content = _content(msg)
|
|
if isinstance(content, str):
|
|
return content
|
|
parts = []
|
|
for c in content:
|
|
if c.get("type") == "text":
|
|
parts.append(c.get("text", ""))
|
|
elif c.get("type") == "tool_result":
|
|
inner = c.get("content", "")
|
|
parts.append(
|
|
inner
|
|
if isinstance(inner, str)
|
|
else " ".join(x.get("text", "") for x in inner)
|
|
)
|
|
return "\n".join(parts)
|
|
|
|
|
|
def kuhy_text(msg: Entry) -> str:
|
|
"""Only what kuhy typed: no tool results, no skill body, no echoed hook reason."""
|
|
content = _content(msg)
|
|
texts = (
|
|
[content]
|
|
if isinstance(content, str)
|
|
else [c.get("text", "") for c in content if c.get("type") == "text"]
|
|
)
|
|
return "\n".join(t for t in texts if not t.startswith(NOT_KUHY))
|
|
|
|
|
|
def _signature_names(code: str) -> set[str]:
|
|
"""Function and parameter names of every signature in `code`."""
|
|
names: set[str] = set()
|
|
for sig in SIG_RE.finditer(code):
|
|
names.update(n for n in sig.groups()[:3] if n)
|
|
for raw in sig.group(4).split(","):
|
|
param = raw.strip().split(":")[0].split("=")[0].strip().lstrip("*")
|
|
if param:
|
|
names.add(param)
|
|
return names
|
|
|
|
|
|
def starter_identifiers(entries: list[Entry]) -> set[str]:
|
|
"""Function name + parameter names from the fetched '## Starter code' block."""
|
|
for e in entries:
|
|
if e.get("type") != "user":
|
|
continue
|
|
txt = text_of(e)
|
|
if "## Starter code" not in txt:
|
|
continue
|
|
block = txt.split("## Starter code", 1)[1]
|
|
m = re.search(r"```\n?(.*?)```", block, re.DOTALL)
|
|
code = m.group(1) if m else block
|
|
# LeetCode starters carry the test harness in a block comment; drop
|
|
# comments so its helpers (`log`, `...argsArr`) are not taken as symbols.
|
|
code = re.sub(r"/\*.*?\*/|//[^\n]*|#[^\n]*", "", code, flags=re.DOTALL)
|
|
names = _signature_names(code)
|
|
return {n for n in names if len(n) >= MIN_IDENT_LEN and n not in KEYWORDS}
|
|
return set()
|
|
|
|
|
|
def _squash(text: str) -> str:
|
|
"""Collapse every whitespace run to one space."""
|
|
return re.sub(r"\s+", " ", text).strip()
|
|
|
|
|
|
def allowed_text(entries: list[Entry]) -> str:
|
|
"""Everything the reply may quote: kuhy's messages and the fetched problem."""
|
|
parts = []
|
|
for e in entries:
|
|
if e.get("type") != "user":
|
|
continue
|
|
parts.append(kuhy_text(e))
|
|
content = _content(e)
|
|
if not isinstance(content, str):
|
|
parts.extend(
|
|
text_of({"message": {"content": [c]}})
|
|
for c in content
|
|
if c.get("type") == "tool_result"
|
|
)
|
|
return _squash("\n".join(parts))
|
|
|
|
|
|
def offending_snippets(reply: str, idents: set[str], allowed: str) -> list[str]:
|
|
"""Code in `reply` that uses a starter identifier and is not a quote."""
|
|
bad = []
|
|
for snippet in CODE_RE.findall(reply):
|
|
body = snippet.strip("`").strip()
|
|
if body.startswith(LANG_PREFIXES):
|
|
body = body.split("\n", 1)[1] if "\n" in body else ""
|
|
if not re.search(r"[(=]|\.\.\.", body):
|
|
continue
|
|
if not any(re.search(rf"\b{re.escape(i)}\b", body) for i in idents):
|
|
continue
|
|
probe = _squash(body).removesuffix("...").strip()
|
|
# Whole-token match: the harness has `...argsArr`; it must not admit `...args`.
|
|
if probe and re.search(rf"(?<!\w){re.escape(probe)}(?!\w)", allowed):
|
|
continue # quoting kuhy's code or the starter back at them
|
|
bad.append(_squash(body)[:80])
|
|
return bad
|