Skip to content

#61 ロン合法source用のprivileged transaction観測hookを追加する - #62

Merged
lisbun merged 1 commit into
mainfrom
claude/project-thread-oqzqa1
Oct 8, 2026
Merged

lisbun merged 1 commit into
mainfrom
claude/project-thread-oqzqa1

Conversation

@lisbun

@lisbun lisbun commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Closes #61

Before: run_hanchan()の既存callbackは成功transaction後のplayer-safeな結果だけを渡すため、打牌→反応不要の解決→立直成立、反応解決→加槓成立、暗槓宣言→成立のように1つのtransaction内で進む順序と中間状態を取り出せなかった。

After: run_hanchan(on_transaction_observation=...)を指定すると、配牌を含む各round transactionについて、内部semantic step(追加event + 全席checkpoint)を順に持つTransactionObservationを受け取れる。未指定時は従来どおりで、stepも作らない。

lisbun/lisjong#262 のsource契約(docs/ron-legal-source.md @ 994f529)をArena(lisbun/lisjong-arena#457)が実対局から記録するための、engine側の観測境界である。``

How

  • 新module transaction_observation.py:TransactionStepKind(ROUND_STARTED / DRAW / TURN_CHOICE / REACTION_WINDOW_SKIPPED / REACTION_RESOLVED / KAN_CONFIRMED / RIICHI_FINALIZED / ROUND_ENDED)と、immutableなSeatCheckpoint / RoundCheckpoint / TransactionStep / TransactionObservation。
    • checkpointは全席の手牌・ツモ牌(source付き)・河・副露・立直種別・一発・見逃し理由(TEMPORARY / RIICHI)、公開済みドラ表示牌、山/嶺上の残り枚数、保留中の立直宣言・加槓・暗槓、立直確定履歴を持つ。山の並び・未公開表示牌・嶺上牌・乱数状態・live state・PlayerState・Wallは持たない。
  • RoundState
    • enable_transaction_observation()以後、既存の各遷移点でworking copyからstepを記録する。stepはcommit成功時だけlast_transaction_stepsへ公開され、失敗transactionのstepは捨てられる。
    • 反応windowを開かない打牌・暗槓はREACTION_WINDOW_SKIPPED(機会なし)として残る。明示windowはREACTION_RESOLVEDで、そのReactionsResolvedEventが候補・選択・capable/selected/awarded/passed・解決を持つ。
    • 鳴き(大明槓を含む)はREACTION_RESOLVEDの中で適用される。加槓・暗槓の成立はKAN_CONFIRMED、宣言牌への反応後の立直成立/不成立はRIICHI_FINALIZED、終局は精算前のROUND_ENDED。
    • commit時に、追加eventがすべていずれかのstepに属すること、最後のstepのcheckpointがcommit後の盤面と一致することを検査する。違反はRoundInvariantError。
    • 観測が無効なら_mark()は何もしない。遷移順序・判定logicには触れていない(_finish_roundをstatic methodからmethodへ変えただけ)。
  • driver.run_hanchan():start_round()の配牌を1 stepのROUND_STARTEDとして渡し、各round transactionでは既存callbackがreturnした後にTransactionObservation(round_ordinal、revision、commit後のphase/current seat、steps、同じtransactionのSelectorDecisionCommit)を渡す。callback例外はfail-fastで伝播する。
  • docs/architecture.mdにprivilegedな境界(selector・player-safe deliveryへ渡さないこと)を追記した。

engineはlisjong/Arenaへ依存しない。Learning wire・label・特徴量は実装しておらず、Arenaがlisjongの固定wireへprojectionする。

Tests

tests/test_transaction_observation.py(22件)

  • 配牌、通常/嶺上ツモ、同巡見逃しの自ツモ解除、反応不要の打牌
  • 明示pass・ポン・ロン→和了確定、三家和の流局
  • 立直選択(盤面不変)、宣言牌の反応不要/全員pass/鳴き/ロンでの成立・一発・不成立
  • 暗槓(window無し、および国士槍槓有効時のwindow→成立)、加槓→槍槓pass→成立、槍槓ロンで不成立
  • 失敗transactionでlast_transaction_stepsが変わらないこと、event全帰属、値のimmutability
  • 半荘を通した観測:callback有無で結果・selector呼出列が同一、各transactionのevent累積がRoundState.eventsと一致し、最後のcheckpointがcommit後盤面と一致する。player-safe delivery・selector入力にprivileged型が含まれないこと、callback失敗のfail-fast

ローカル確認:Ruff format/check、git diff --check。中核のtransition codeに触るため、engineのunittestもローカルで全件実行した(1503件成功)。

🤖 Generated with Claude Code

https://claude.ai/code/session_01CgaL38rjBCKr1NBpaUNUAL


Generated by Claude Code

- RoundState.enable_transaction_observation()で、成功transactionごとの
  semantic step(追加event + 全席checkpoint)をcommit時だけ公開する
- 反応不要の打牌・暗槓をREACTION_WINDOW_SKIPPEDとして残し、反応解決・
  槓成立・立直成立/不成立・終局を順序どおりに分ける
- 追加eventの全step帰属と、最後のstepとcommit後盤面の一致をcommit時に検査する
- run_hanchan(on_transaction_observation=...)で配牌を含む各transactionを渡す。
  未指定時はstepを作らず、従来の進行・結果は変わらない

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01CgaL38rjBCKr1NBpaUNUAL

lisbun commented Oct 8, 2026

Copy link
Copy Markdown
Owner Author

20半荘での観測付き確認(head a132377)

MatchState(seed=0..19) の20半荘を、テストの _winning_first_selector(ロン・ツモがあれば選び、なければ先頭の選択肢)で回した。各seedについて、観測無しの実行と on_transaction_observation 付きの実行を両方行い、次を検査した。

  • 観測無し/有りで CompletedMatch が一致する(20/20)
  • 観測した局数が history の局数と一致する
  • 各局の最初のobservationが ROUND_STARTED である
  • stepの events を積み上げると、毎transactionで active_round.events と一致する(eventの欠落・重複がない)
  • 毎transactionで、revision と phase がactive roundと一致し、最後のstepのcheckpointが committed_checkpoint() と一致する

結果:20半荘、241局、37,017 transaction。すべての検査が通った。

step 件数
round_started 241
draw 16,870
turn_choice 16,870
reaction_window_skipped 13,834
reaction_resolved 3,036
round_ended 241

所要時間:観測無し 312.8s、観測有り 322.4s(約3%増)。

この確認では出なかったケース:上の選択規則は立直・槓を選ばないので、kan_confirmed と riichi_finalized はこの20半荘では1件も出ていない。これらは tests/test_transaction_observation.py の固定局面テストで確認している(宣言牌の反応不要/全員pass/鳴き/ロン、暗槓のwindowあり/なし、加槓の槍槓pass/ロン)。実対局での出現は lisbun/lisjong-arena#457 のpilotで確認する想定。


Generated by Claude Code

@lisbun
lisbun marked this pull request as ready for review October 8, 2026 03:22
@lisbun
lisbun merged commit 91af75e into main Oct 8, 2026
1 check passed
@lisbun
lisbun deleted the claude/project-thread-oqzqa1 branch October 8, 2026 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ロン合法source用のprivileged transaction観測hookを追加する

2 participants