fix(hints): Maia's hint is her own move, Stockfish hint has a deadline
public
fix(hints): Maia's hint is her own move, Stockfish hint has a deadline - With a Maia helper the hint is her most likely move, unchecked and immediate; Stockfish only steps in when Maia is unavailable. - Stockfish as helper gives its best move at depth 14, or after 1 s once depth 8 is reached: in won positions a single depth can take seconds, which left the hint hanging (especially with a second window). - Inactive windows still precompute the hint, losing focus no longer aborts it; "Moment …" toasts while a hint or tree is computing; hint explanation trees are cancelled once a move is made. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 69c75f0d48f731b9ed886ada9ce4f8c2323253fb |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 176fb6d09729 |
| Children | 938fb9868b30 |
| Pushlog | by timo via ssh by timo via ssh |
modified README.md +2 −1
| @@ -8,7 +8,8 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 8 | 8 | - **Hinweis** (Taste `H`): Erster Druck lässt die Figur leuchten, zweiter Druck auch |
| 9 | 9 | das Zielfeld – mit Pfeil, Feldnamen und dem Zug halb durchsichtig; `Enter` spielt ihn dann. |
| 10 | 10 | Im Eröffnungskurs zählt ein Hinweis, der nur die Figur zeigt, halb. Wer hilft, steht im Pfeil-Menü daneben: Maia 1500, 2000 (Standard) oder |
| 11 | - 2500 – jeweils von Stockfish geprüft – oder Stockfish selbst. | |
| 11 | + 2500 (ihr wahrscheinlichster Zug, so wie ein Mensch dieser Stärke spielen würde) oder Stockfish | |
| 12 | + (sein bester Zug). | |
| 12 | 13 | - **Drohung** (Taste `B`, Schwert): umgekehrt zum Hinweis. Erster Druck zeigt das |
| 13 | 14 | bedrohte Feld (orange), zweiter die drohende Figur mit Pfeil und den Drohzug halb durchsichtig. Die Drohung passt zum |
| 14 | 15 | Gegner: Maia in seiner Stärke schlägt vor, was er sehen würde, Stockfish prüft nur, ob es |
modified schach/annotate.py +13 −35
| @@ -6,6 +6,7 @@ einzeln nachgerechnet ("lazy") – oft lässt sich das sogar ganz vermeiden. | ||
| 6 | 6 | """ |
| 7 | 7 | |
| 8 | 8 | import threading |
| 9 | +import time | |
| 9 | 10 | |
| 10 | 11 | import chess |
| 11 | 12 | import chess.engine |
| @@ -25,8 +26,10 @@ NAG = {"!": 1, "?": 2, "!!": 3, "??": 4, | ||
| 25 | 26 | BLUNDER, MISTAKE, INACCURACY = 15.0, 10.0, 5.0 |
| 26 | 27 | # Ein "einziger Zug": alle Alternativen sind mindestens so viel schlechter |
| 27 | 28 | ONLY_MOVE_GAP = 12.0 |
| 28 | -# Hinweis: strenger als ?! (5), damit ein befolgter Hinweis nie markiert wird | |
| 29 | -HINT_MAX_LOSS = 3.0 | |
| 29 | +# Hinweis von Stockfish: spätestens nach so vielen Sekunden, wenn mindestens HINT_MIN_DEPTH erreicht | |
| 30 | +# ist. In klar gewonnenen Stellungen braucht Stockfish für eine einzige Tiefe manchmal Sekunden. | |
| 31 | +HINT_DEADLINE = 1.0 | |
| 32 | +HINT_MIN_DEPTH = 8 | |
| 30 | 33 | |
| 31 | 34 | TOP_LINES = 3 # so viele beste Züge rechnet Stockfish live mit |
| 32 | 35 | THINK_LIMIT = chess.engine.Limit(depth=30, time=30) # Obergrenze fürs Mitdenken |
| @@ -131,36 +134,34 @@ class Thinking: | ||
| 131 | 134 | self._analysis.stop() |
| 132 | 135 | |
| 133 | 136 | |
| 134 | -def think_job(board, candidates, thinking, on_snapshot, on_hint): | |
| 137 | +def think_job(board, thinking, on_snapshot, on_hint=None): | |
| 135 | 138 | """Job-Fabrik: Stockfish denkt mit, solange der Mensch überlegt. |
| 136 | 139 | |
| 137 | - 1. Top-Linien live bis ``MIN_DEPTH`` rechnen, | |
| 138 | - 2. Hinweis aus Maias Kandidaten bestimmen (lazy, s. ``_resolve_hint``), | |
| 140 | + 1. Top-Linien live bis ``MIN_DEPTH`` rechnen (bzw. bis HINT_DEADLINE, s. dort), | |
| 141 | + 2. ``on_hint(bester Zug)``, falls Stockfish der Hinweisgeber ist, | |
| 139 | 142 | 3. weiter in die Tiefe, bis ``thinking.stop()``. |
| 140 | 143 | ``on_snapshot``/``on_hint`` laufen im Worker-Thread; Ergebnis ist die letzte Momentaufnahme. |
| 141 | 144 | """ |
| 142 | 145 | board = board.copy() |
| 143 | - candidates = list(candidates) | |
| 144 | 146 | |
| 145 | 147 | def job(engine): |
| 146 | 148 | n = min(TOP_LINES, board.legal_moves.count()) |
| 147 | 149 | if n == 0: |
| 148 | 150 | return None |
| 149 | - extra = {} # lazy nachgerechnete Züge außerhalb der Top-Linien | |
| 150 | 151 | |
| 151 | 152 | def stream(until_depth): |
| 152 | 153 | last = None |
| 153 | 154 | limit = QUICK_LIMIT if thinking.stopped else THINK_LIMIT |
| 155 | + start = time.monotonic() | |
| 154 | 156 | with engine.analysis(board, limit, multipv=n) as analysis: |
| 155 | 157 | thinking.attach(analysis) |
| 156 | 158 | for info in analysis: |
| 157 | 159 | # Zeilen 1..n kommen am Ende jeder Tiefe; Zeile n = Tiefe fertig |
| 158 | 160 | if info.get("multipv") == n and "depth" in info: |
| 159 | 161 | last = _entry(analysis.multipv, board.turn, info["depth"]) |
| 160 | - for move, wp in extra.items(): | |
| 161 | - last["scores"].setdefault(move, wp) | |
| 162 | 162 | on_snapshot(last) |
| 163 | - if until_depth and info["depth"] >= until_depth: | |
| 163 | + if until_depth and (info["depth"] >= until_depth or info["depth"] >= HINT_MIN_DEPTH | |
| 164 | + and time.monotonic() - start >= HINT_DEADLINE): | |
| 164 | 165 | break |
| 165 | 166 | if last is None: |
| 166 | 167 | last = _entry(analysis.multipv, board.turn, 0) |
| @@ -169,36 +170,13 @@ def think_job(board, candidates, thinkin | ||
| 169 | 170 | last = stream(MIN_DEPTH) |
| 170 | 171 | if thinking.stopped: |
| 171 | 172 | return last |
| 172 | - on_hint(_resolve_hint(engine, board, last, candidates, extra)) | |
| 173 | - if extra: | |
| 174 | - last = {**last, "scores": {**extra, **last["scores"]}} | |
| 175 | - on_snapshot(last) | |
| 176 | - if thinking.stopped: | |
| 177 | - return last | |
| 173 | + if on_hint is not None: | |
| 174 | + on_hint(last["best"]) | |
| 178 | 175 | return stream(None) |
| 179 | 176 | |
| 180 | 177 | return job |
| 181 | 178 | |
| 182 | 179 | |
| 183 | -def _resolve_hint(engine, board, entry, candidates, extra): | |
| 184 | - """Erster Maia-Kandidat, mit dem Stockfish einverstanden ist; sonst Stockfishs Zug.""" | |
| 185 | - scores = dict(entry["scores"]) # Kopie: die Momentaufnahme gehört schon dem GTK-Thread | |
| 186 | - if not scores: | |
| 187 | - return None | |
| 188 | - best_wp = max(scores.values()) | |
| 189 | - third_wp = min(scores.values()) | |
| 190 | - for move in candidates: | |
| 191 | - if move in scores: | |
| 192 | - wp = scores[move] | |
| 193 | - elif entry["top"] >= TOP_LINES and best_wp - third_wp > HINT_MAX_LOSS: | |
| 194 | - continue # schlechter als der Drittbeste, also sicher nicht gut genug | |
| 195 | - else: | |
| 196 | - wp = extra[move] = scores[move] = evaluate_move(engine, board, move) | |
| 197 | - if best_wp - wp <= HINT_MAX_LOSS: | |
| 198 | - return move | |
| 199 | - return entry["best"] | |
| 200 | - | |
| 201 | - | |
| 202 | 180 | def classify_with(entry, before, move): |
| 203 | 181 | """Symbol aus einer Momentaufnahme, oder ``False`` falls nachgerechnet werden muss.""" |
| 204 | 182 | if not entry or not entry["scores"]: |
modified schach/app.py +47 −14
| @@ -32,8 +32,8 @@ from .clock import TIME_CONTROLS, GameCl | ||
| 32 | 32 | from .thinktime import think_seconds # noqa: E402 |
| 33 | 33 | |
| 34 | 34 | APP_ID = "app.schach.Schach" |
| 35 | -# Wer gibt den Hinweis? Maia schlägt menschliche Züge vor, Stockfish prüft sie (≤ 3 % schlechter); | |
| 36 | -# "stockfish" nimmt direkt den besten Zug. | |
| 35 | +# Wer gibt den Hinweis? Maia: ihr wahrscheinlichster Zug in dieser Stärke, ungeprüft (er kann auch | |
| 36 | +# einmal ein Zeichen wie ?! bekommen); "stockfish": sein bester Zug. | |
| 37 | 37 | HELPERS = { |
| 38 | 38 | "maia1500": ("Maia 1500", 1500), |
| 39 | 39 | "maia2000": ("Maia 2000", 2000), |
| @@ -865,6 +865,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 865 | 865 | |
| 866 | 866 | def _changed(self): |
| 867 | 867 | self._schedule_autosave() |
| 868 | + self._wait_done() # eine laufende Rechnung gehörte zur alten Stellung | |
| 868 | 869 | self.gen += 1 |
| 869 | 870 | self.hint_move = None |
| 870 | 871 | self.hint_level = 0 |
| @@ -1090,6 +1091,7 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1090 | 1091 | self._reveal_hint() # im Hintergrund schon fertig berechnet |
| 1091 | 1092 | else: |
| 1092 | 1093 | self.hint_pending = True # Vorausberechnung läuft noch; zeigt sich, sobald fertig |
| 1094 | + self._wait_toast("Moment, ich suche einen guten Zug …") | |
| 1093 | 1095 | self._refresh() |
| 1094 | 1096 | |
| 1095 | 1097 | def _can_explain(self): |
| @@ -1110,11 +1112,13 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1110 | 1112 | return |
| 1111 | 1113 | gen, board, move = self.gen, self.board.copy(), self.hint_move |
| 1112 | 1114 | self.explain_pending = True |
| 1115 | + self._wait_toast("Moment, ich rechne die Varianten durch …") | |
| 1113 | 1116 | self._refresh_chrome() |
| 1114 | 1117 | |
| 1115 | 1118 | def on_done(tree, error): |
| 1116 | 1119 | if gen != self.gen: |
| 1117 | 1120 | return |
| 1121 | + self._wait_done() | |
| 1118 | 1122 | self.explain_pending = False |
| 1119 | 1123 | if error is not None or tree is None or tree.first() is None: |
| 1120 | 1124 | self.toasts.add_toast(Adw.Toast(title=f"Keine Erklärung möglich: {error or 'keine Variante'}")) |
| @@ -1128,7 +1132,8 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1128 | 1132 | self._refresh_chrome() |
| 1129 | 1133 | |
| 1130 | 1134 | maia, name = self._tree_maia() |
| 1131 | - self.get_application().side_engine().submit(explain_job(board, move, maia, name), on_done) | |
| 1135 | + stale = lambda: gen != self.gen # noqa: E731 – inzwischen gezogen: Baum nicht mehr nötig | |
| 1136 | + self.get_application().side_engine().submit(explain_job(board, move, maia, name, cancel=stale), on_done) | |
| 1132 | 1137 | |
| 1133 | 1138 | def _tree_maia(self): |
| 1134 | 1139 | """(maia, name) für den Variantenbaum: Maia in der Stärke des Gegners, ihr Name fürs Schild.""" |
| @@ -1253,7 +1258,19 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1253 | 1258 | |
| 1254 | 1259 | self.maia.submit(maia_candidates(null_board(board), self._opponent_elo(), 5), on_candidates) |
| 1255 | 1260 | |
| 1261 | + def _wait_toast(self, title): | |
| 1262 | + """Rückmeldung, solange gerechnet wird; verschwindet mit dem Ergebnis (_wait_done).""" | |
| 1263 | + self._wait_done() | |
| 1264 | + self.waiting_toast = Adw.Toast(title=title, timeout=0) | |
| 1265 | + self.toasts.add_toast(self.waiting_toast) | |
| 1266 | + | |
| 1267 | + def _wait_done(self): | |
| 1268 | + toast, self.waiting_toast = self.waiting_toast, None | |
| 1269 | + if toast is not None: | |
| 1270 | + toast.dismiss() | |
| 1271 | + | |
| 1256 | 1272 | def _reveal_hint(self): |
| 1273 | + self._wait_done() | |
| 1257 | 1274 | if self.hint_move is None: |
| 1258 | 1275 | self.toasts.add_toast(Adw.Toast(title=f"Kein Hinweis möglich: {self.hint_error}")) |
| 1259 | 1276 | else: |
| @@ -1264,8 +1281,8 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1264 | 1281 | """Sobald der Mensch am Zug ist: Maias Kandidaten holen, dann denkt Stockfish mit.""" |
| 1265 | 1282 | if self.mode not in ("maia", "duo") or not self._human_to_move() or self.hint_prefetch_gen == self.gen: |
| 1266 | 1283 | return # Online-Partien: Fair Play, kein Mitdenken; Rätsel: Lösung steht fest |
| 1267 | - if not self.is_active(): | |
| 1268 | - return # nur das aktive Fenster lässt Stockfish mitdenken | |
| 1284 | + # Auch ein inaktives Fenster rechnet den Hinweis vor (danach stoppt _on_hint); tiefer | |
| 1285 | + # mitdenken lässt nur das aktive Fenster | |
| 1269 | 1286 | gen = self.hint_prefetch_gen = self.gen |
| 1270 | 1287 | board = self.board.copy() |
| 1271 | 1288 | key = annotate.key(board) |
| @@ -1284,16 +1301,22 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1284 | 1301 | elif error is not None: |
| 1285 | 1302 | self._on_hint(gen, None, error) |
| 1286 | 1303 | |
| 1287 | - def on_candidates(moves, error): | |
| 1288 | - # Auch wenn schon gezogen wurde: die Rechnung liefert dann rasch die Bewertung | |
| 1289 | - job = annotate.think_job(board, moves or [], thinking, snapshot_from_thread, hint_from_thread) | |
| 1304 | + def think(hint_from_stockfish): | |
| 1305 | + # Stockfish denkt immer mit (Bewertung, Zugzeichen); den Hinweis gibt er nur als Helfer | |
| 1306 | + job = annotate.think_job(board, thinking, snapshot_from_thread, | |
| 1307 | + hint_from_thread if hint_from_stockfish else None) | |
| 1290 | 1308 | self.stockfish.submit(job, on_done) |
| 1291 | 1309 | |
| 1310 | + def on_maia(moves, error): | |
| 1311 | + if moves: | |
| 1312 | + self._on_hint(gen, moves[0]) # der Helfer ist Maia: ihr wahrscheinlichster Zug, ohne Prüfung | |
| 1313 | + think(not moves) # Maia nicht verfügbar: dann eben Stockfish | |
| 1314 | + | |
| 1292 | 1315 | elo = HELPERS[self._helper()][1] |
| 1293 | 1316 | if elo is None: |
| 1294 | - on_candidates([], None) # Stockfish: ohne Maia-Vorschläge gilt sein bester Zug | |
| 1317 | + think(True) | |
| 1295 | 1318 | else: |
| 1296 | - self.maia.submit(maia_candidates(board, elo), on_candidates) | |
| 1319 | + self.maia.submit(maia_candidates(board, elo, 1), on_maia) | |
| 1297 | 1320 | |
| 1298 | 1321 | def _on_snapshot(self, key, entry, final): |
| 1299 | 1322 | old = self.analyses.get(key) |
| @@ -1316,6 +1339,9 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1316 | 1339 | self.hint_error = error if move is None else None |
| 1317 | 1340 | if move is None and error is None: |
| 1318 | 1341 | self.hint_error = "kein Zug gefunden" |
| 1342 | + if not self.is_active() and self.thinking is not None: | |
| 1343 | + self.thinking.stop() # Hinweis ist da; tiefer rechnet nur das aktive Fenster | |
| 1344 | + self.thinking = None | |
| 1319 | 1345 | if self.hint_pending: |
| 1320 | 1346 | self.hint_pending = False |
| 1321 | 1347 | self._reveal_hint() |
| @@ -1949,10 +1975,17 @@ class Window(OnlineMixin, PuzzleMixin, O | ||
| 1949 | 1975 | def _on_active_changed(self, *_): |
| 1950 | 1976 | if self.is_active(): |
| 1951 | 1977 | self._refresh() # startet das Mitdenken wieder |
| 1952 | - elif self.thinking is not None: | |
| 1953 | - self.thinking.stop() | |
| 1954 | - self.thinking = None | |
| 1955 | - self.hint_prefetch_gen = None | |
| 1978 | + elif self.thinking is not None and self._hint_ready(): | |
| 1979 | + self._stop_thinking() | |
| 1980 | + # sonst: erst den Hinweis fertig rechnen (_on_hint stoppt dann), sonst wartet man nach der Rückkehr | |
| 1981 | + | |
| 1982 | + def _hint_ready(self): | |
| 1983 | + return self.hint_move is not None or self.hint_error is not None | |
| 1984 | + | |
| 1985 | + def _stop_thinking(self): | |
| 1986 | + self.thinking.stop() | |
| 1987 | + self.thinking = None | |
| 1988 | + self.hint_prefetch_gen = None | |
| 1956 | 1989 | |
| 1957 | 1990 | def _puzzle_action(self, *_): |
| 1958 | 1991 | """Rätsel: eine laufende Partie bleibt erhalten – dann im neuen Fenster.""" |
modified schach/explain.py +22 −7
| @@ -271,12 +271,23 @@ def _extend(node, pv, plies): | ||
| 271 | 271 | return node |
| 272 | 272 | |
| 273 | 273 | |
| 274 | -def grow(board, first, engine, maia=None, name="Maia", limit=EXPLAIN_LIMIT, child_limit=CHILD_LIMIT, hero=None): | |
| 274 | +class Cancelled(Exception): | |
| 275 | + """Der Baum wird nicht mehr gebraucht (z. B. wurde inzwischen gezogen).""" | |
| 276 | + | |
| 277 | + | |
| 278 | +def _check(cancel): | |
| 279 | + if cancel is not None and cancel(): | |
| 280 | + raise Cancelled() | |
| 281 | + | |
| 282 | + | |
| 283 | +def grow(board, first, engine, maia=None, name="Maia", limit=EXPLAIN_LIMIT, child_limit=CHILD_LIMIT, hero=None, | |
| 284 | + cancel=None): | |
| 275 | 285 | """Variantenbaum zu ``first``: eigene Züge von Stockfish, beim Gegner Maias wahrscheinliche Züge |
| 276 | 286 | und die beste Verteidigung. ``maia(board) -> [(Zug, Wahrscheinlichkeit oder None)]``; ohne Maia |
| 277 | 287 | bleibt es eine Linie. Gabelungen gibt es nur entlang der Hauptlinie, Nebenzweige folgen Stockfish. |
| 278 | 288 | ``hero``: wessen Sicht (Standard: wer ``first`` zieht). Ist ``first`` ein Zug des Gegners (selbst |
| 279 | - gezogen auf dem Analysebrett), antwortet zuerst Stockfish für ``hero``. | |
| 289 | + gezogen auf dem Analysebrett), antwortet zuerst Stockfish für ``hero``. ``cancel() -> bool``: wird | |
| 290 | + vor jeder Rechnung gefragt; True bricht mit ``Cancelled`` ab. | |
| 280 | 291 | """ |
| 281 | 292 | hero = board.turn if hero is None else hero |
| 282 | 293 | game = chess.pgn.Game() |
| @@ -297,6 +308,7 @@ def grow(board, first, engine, maia=None | ||
| 297 | 308 | rest = rest[1:] |
| 298 | 309 | depth = 2 |
| 299 | 310 | while depth < MAX_PLIES and rest: |
| 311 | + _check(cancel) | |
| 300 | 312 | position = node.board() |
| 301 | 313 | if position.is_game_over(): |
| 302 | 314 | break |
| @@ -317,6 +329,7 @@ def grow(board, first, engine, maia=None | ||
| 317 | 329 | candidates.append((best, None)) |
| 318 | 330 | maia_moves = {m: p for m, p in offered if any(m == c for c, _q in candidates)} |
| 319 | 331 | for move, prob in candidates: |
| 332 | + _check(cancel) | |
| 320 | 333 | if len(candidates) == 1 and move == best: |
| 321 | 334 | child_info = None # keine Gabelung: Stockfishs Linie geht einfach weiter |
| 322 | 335 | else: |
| @@ -557,20 +570,22 @@ def board_null(board): | ||
| 557 | 570 | return probe |
| 558 | 571 | |
| 559 | 572 | |
| 560 | -def explain_job(board, move, maia=None, name="Maia", limit=EXPLAIN_LIMIT, hero=None, better=None): | |
| 573 | +def explain_job(board, move, maia=None, name="Maia", limit=EXPLAIN_LIMIT, hero=None, better=None, cancel=None): | |
| 561 | 574 | """Job für Stockfish: Variantenbaum zu ``move`` (auch wenn Maia ihn vorschlug), ausgedünnt und |
| 562 | 575 | kommentiert. ``maia``: siehe grow(); läuft auf dem Stockfish-Thread und darf dort blockieren. |
| 563 | - ``hero``/``better``: siehe grow() und comment().""" | |
| 576 | + ``hero``/``better``/``cancel``: siehe grow() und comment().""" | |
| 564 | 577 | board = board.copy() |
| 565 | 578 | |
| 566 | 579 | def job(engine): |
| 567 | - return _explain(engine, board, move, maia, name, limit, hero, better) | |
| 580 | + return _explain(engine, board, move, maia, name, limit, hero, better, cancel) | |
| 568 | 581 | |
| 569 | 582 | return job |
| 570 | 583 | |
| 571 | 584 | |
| 572 | -def _explain(engine, board, move, maia, name, limit, hero=None, better=None): | |
| 573 | - tree = grow(board, move, engine, maia, name, limit, hero=hero) | |
| 585 | +def _explain(engine, board, move, maia, name, limit, hero=None, better=None, cancel=None): | |
| 586 | + _check(cancel) | |
| 587 | + tree = grow(board, move, engine, maia, name, limit, hero=hero, cancel=cancel) | |
| 588 | + _check(cancel) | |
| 574 | 589 | if board.turn != tree.hero: |
| 575 | 590 | return comment(prune(tree)) # Zug des Gegners: keine Drohung, kein „einziger Zug“ |
| 576 | 591 | played = tree.meta[tree.first()]["wp"] |