utils/leetcode_coach/leetcode_coach/local_cards.py
Krzysztof kuhy Rudnicki 28c243d066 Coach toolbox, clusters, cold re-tests and property cards (TODO-coach 9-12)
- toolbox.json: 20 entries over a fixed 40-property vocabulary; no entry's
  property set nests in another's, so every property card has one answer.
- clusters.json: decision 10's six clusters, ids plus slugs.
- progress.py (stdlib only): solved = code + postmortem, re-tests due at
  +3/+10 days, cluster progress, all derived from the session logs.
- leetcode due / --cluster <name> / --retest <slug> (cluster_cli.py): due
  re-tests of the cluster first, then the next problem with no code event,
  skipping LeetCode-Accepted ones (live check fails open). A cold re-test
  logs `retest` only once the session reached a post-mortem.
- Post-mortem cards are property -> operation only, generated by
  toolbox_cards (shared slug `toolbox`); `add` appends an entry only after
  kuhy confirms its properties. The local client accepts {"toolbox": [...]}.
- _coach_log publishes retests_due and clusters from the coach.
- requirements-sync treats sibling repo packages as first-party.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PjvF5DZG9JduSBYKxqyysi
2026-10-23 15:39:52 +02:00

65 lines
2.3 KiB
Python

"""Post-mortem cards from a local reply: extract the ```cards block, build the deck.
The local model has no tools, so instead of the Bash call SKILL.md describes it
emits the JSON in a fenced ```cards block; this module lifts it out of the
displayed text and pipes it to `make_cards`, whose validation is unchanged.
"""
from __future__ import annotations
import json
import logging
import re
import subprocess
import sys
from typing import TYPE_CHECKING, Any
from leetcode_coach import toolbox_cards
if TYPE_CHECKING:
from collections.abc import Callable
_LOG = logging.getLogger(__name__)
CARDS_RE = re.compile(r"```cards\s*\n(.*?)```", re.DOTALL)
def split_cards(reply: str) -> tuple[str, dict[str, Any] | None]:
"""Remove a ```cards block from `reply`; return (display text, parsed JSON)."""
match = CARDS_RE.search(reply)
if match is None:
return reply, None
display = (reply[: match.start()] + reply[match.end() :]).strip()
try:
payload = json.loads(match.group(1))
except json.JSONDecodeError as exc:
_LOG.warning("the cards block is not JSON: %s", exc)
return display, {"invalid": match.group(1)}
return display, payload if isinstance(payload, dict) else {"invalid": payload}
def deliver_cards(
payload: dict[str, Any],
run: Callable[..., subprocess.CompletedProcess[str]] = subprocess.run,
) -> tuple[bool, str]:
"""Pipe the cards to the card builder; (accepted, its stdout+stderr)."""
if "invalid" in payload:
return False, f"cards block is not a JSON object: {payload['invalid']!r}"
if "toolbox" in payload:
# Property -> operation cards (decision 11) come from toolbox.json,
# so the local model names entries instead of typing fronts.
try:
cards = toolbox_cards.cards_for(list(payload["toolbox"]))
except KeyError as exc:
_LOG.warning("cards block names an unknown entry: %s", exc)
return False, str(exc.args[0])
payload = {"slug": toolbox_cards.SLUG, "cards": cards}
proc = run(
[sys.executable, "-m", "leetcode_coach.make_cards", "-"],
input=json.dumps(payload),
capture_output=True,
text=True,
check=False,
)
return proc.returncode == 0, (proc.stdout + proc.stderr).strip()