Repository navigation
Closed
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phasetriage-requiredNeeds owner triage for priority decision
Milestone
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "LoopPhase 遷移が ad-hoc な UI callback に分散しており、invalid transition の防止と phase-aware な HUD / storage 制御が困難"
change_kind: codeParent Issue
none(単独改善)
Parent Goal Ref
- Goal: PhaseTransitionSystem を導入し、既存
LoopPhase型を使った遷移表を system / controller に集約することで、ad-hoc UI callback から phase 変更責務を剥がす - Desired Destination: N/A(単独改善 Issue のため)
Current Validated Scope
src/systems/PhaseTransitionSystem.tsを pure transition resolver / validator として新規作成し、現行LoopPhaseunion のみを対象にした遷移表を定義するsrc/main.tsとsrc/systems/SortieSystem.tsの既存 phase mutation を、phase 代入直前に PhaseTransitionSystem の validation を通す構成へ寄せるdebrief_pending_reward/debrief_reward_claimedは legacy transition として明示し、通常result -> preparationflow と混同しないことを contract とテストで固定するsrc/ui/HudController.tsは button enable policy の整合に必要な範囲だけ追従させ、通常 HUD に raw LoopPhase や raw transition error を露出しない境界を維持する
Remaining Parent Gaps
なし(単独改善 Issue のため)
Outcome
src/systems/PhaseTransitionSystem.ts が pure resolver / validator として追加され、src/main.ts と src/systems/SortieSystem.ts の実 phase mutation entrypoint が validation を経由し、legacy debrief を含む現行 LoopPhase 契約が一元管理された状態で、pnpm typecheck / pnpm lint / pnpm test / pnpm build が PASS した PR がマージされた状態。
In Scope
src/systems/PhaseTransitionSystem.tsの新規作成- 現行
LoopPhaseunion に対する遷移表の定義(title_menu/load_menu/preparation/running/result/debrief_pending_reward/debrief_reward_claimed) endedを導入せず、現行LoopPhase契約だけを対象にする- 不正遷移を防止する pure resolver / validator API の実装
src/systems/index.tsへの再エクスポート追加src/main.tsのonNewGame/onLoadGame/ bootstrap / E2E auto-start などの phase mutation を validation 経由へ変更するsrc/systems/SortieSystem.tsのstartSortie()/ terminal result /confirmResult()/ legacy debrief transition を validation 経由へ変更するsrc/ui/HudController.tsの action surface を新しい phase gate 契約に整合させるtests/phase-transition-system.test.ts、tests/main-loop.test.ts、tests/sortie-system.test.ts、tests/hud-controller.test.tsによる回帰固定
Out of Scope
endedを正式なLoopPhaseとして追加する変更src/state/GameState.tsのLoopPhaseunion 自体の拡張- save/load policy の再設計
- reward calculation / reward pipeline semantics の変更
GameSnapshotschema の変更- localStorage / IndexedDB など storage backend の変更
- HUD / Canvas の visual redesign
- PhaseTransitionSystem に DOM / Canvas / storage / reward claim / evidence mutation の副作用を持たせる変更
Acceptance Criteria
- AC1:
src/systems/PhaseTransitionSystem.tsが存在する - AC2:
tests/phase-transition-system.test.tsが存在する - AC3:
src/main.tsの phase mutation entrypoint がPhaseTransitionSystemの resolver / validator を経由することが source または test で固定されている - AC4:
src/systems/SortieSystem.tsの phase mutation entrypoint がPhaseTransitionSystemの resolver / validator を経由することが source または test で固定されている - AC5:
debrief_pending_reward/debrief_reward_claimedが legacy transition として明示され、通常result -> preparationflow と混同しないことが test で固定されている - AC6:
src/ui/HudController.tsの通常 HUD / action surface が raw LoopPhase や raw transition error を通常 UI に露出せず、新しい phase gate 契約と矛盾しないことが test で固定されている - AC7:
pnpm typecheckが PASS する - AC8:
pnpm lintが PASS する - AC9:
pnpm testが PASS する - AC10:
pnpm buildが PASS する
Verification Commands
# AC1
# baseline-expect: fail
$ test -f src/systems/PhaseTransitionSystem.ts
# AC2
# baseline-expect: fail
$ test -f tests/phase-transition-system.test.ts
# AC3
# baseline-expect: fail
$ rg -n "resolvePhaseTransition" src/main.ts tests/main-loop.test.ts
# AC4
# baseline-expect: fail
$ rg -n "resolvePhaseTransition" src/systems/SortieSystem.ts tests/sortie-system.test.ts
# AC5
# baseline-expect: fail
$ rg -n "legacy debrief" tests/sortie-system.test.ts
# AC6
# baseline-expect: pass
$ rg -n "phase gate" tests/hud-controller.test.ts
# AC7
$ pnpm typecheck
# AC8
$ pnpm lint
# AC9
$ pnpm test
# AC10
$ pnpm buildAllowed Paths
- src/systems/PhaseTransitionSystem.ts
- src/systems/index.ts
- src/main.ts
- src/systems/SortieSystem.ts
- src/ui/HudController.ts
- tests/phase-transition-system.test.ts
- tests/main-loop.test.ts
- tests/sortie-system.test.ts
- tests/hud-controller.test.ts
Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
- In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
- 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
- 後続 Phase / 別スコープへの波及が判明した場合
- nested SubAgent delegation が必要になった場合
- 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
endedを正式なLoopPhaseとして導入しないと成立しないと判明した場合- PhaseTransitionSystem に DOM / Canvas / storage / reward / evidence の副作用を吸い込まないと成立しないと判明した場合
- save/load policy や
GameSnapshotschema まで同時変更しないと成立しないと判明した場合
Required Skills
- TypeScript
- ECS(state / systems 分離)
- state machine / transition validation
Runtime Verification Applicability
- decision: not_applicable
- reason: 本 Issue の妥当性は
PhaseTransitionSystemの table / resolver と、main.ts/SortieSystem.ts/HudController.tsに対する unit・integration テスト、およびpnpm typecheck/pnpm lint/pnpm test/pnpm buildで決定論的に検証可能であり、別途の手動 runtime evidence を要求しないため
Scope Delta(任意)
owner comment https://github.com/squne121/loop-protocol/issues/1174#issuecomment-4853340922 を一次ソースとして反映。ended を現行 LoopPhase 契約から除外し、mutation surface を HudController.ts 中心から src/main.ts / src/systems/SortieSystem.ts 中心へ修正し、legacy debrief と pure resolver / validator 境界を明文化した。
Metadata
Metadata
Assignees
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phasetriage-requiredNeeds owner triage for priority decision
Activity
squne121 commentedon Jul 1, 2026
判定
REQUEST_CHANGES 相当です。
Issue #1174 の目的、つまり
LoopPhase遷移を一元化して ad-hoc callback から剥がす方向性は妥当です。ただし、現行の Issue 契約のまま実装すると、**「一元管理したように見えるが、実際の phase mutation は残る」**状態になるリスクが高いです。#1174 にはコメントがなく、追加の設計合意は Issue 本文以外に確認できませんでした。 直接対応する実装 PR も見当たらず、周辺履歴としては #738 / PR #773、#619 / PR #868、#771 / PR #877、#1173 が実質的な前提です。
主要な Blocker
1.
endedが契約に入っているが、現行LoopPhase型に存在しない#1174 は遷移表の対象として
title_menu / load_menu / preparation / running / result / debrief_pending_reward / debrief_reward_claimed / endedを挙げています。 しかし現行のLoopPhaseunion はtitle_menu、load_menu、preparation、running、result、debrief_pending_reward、debrief_reward_claimedまでで、endedは含まれていません。これは単なる命名漏れではなく、契約破綻です。
endedを本当に導入するならsrc/state/GameState.tsの変更が必要ですが、#1174 の Allowed Paths にはGameState.tsがありません。逆にGameState.tsを変更しないなら、#1174 からendedを削除し、legacy debrief phase までを対象にする必要があります。修正案:
#1174 の本文を次のどちらかに寄せるべきです。
endedを削除し、現行LoopPhaseunion だけを PhaseTransitionSystem の対象にする。endedを正式なLoopPhaseとして追加するため、Allowed Paths にsrc/state/GameState.tsと関連テストを追加し、endedの到達条件・保存可否・HUD 表示・simulation gate を明文化する。現時点では A案が安全です。
2. Allowed Paths が足りず、「一元管理」を実現できない
#1174 は Allowed Paths を
src/systems/PhaseTransitionSystem.ts、src/systems/index.ts、src/ui/HudController.ts、tests/phase-transition-system.test.tsに限定しています。 しかし実際の phase mutation は主にsrc/main.tsとsrc/systems/SortieSystem.tsにあります。HudController.tsは phase を直接変更していません。イベントハンドラをactionscallback に接続し、render 時にstate.loopPhaseから button disabled を導出しているだけです。実際の mutation は、例えば次の箇所です。
main.ts:onNewGame()がtitle_menu -> preparationを直接代入。main.ts:onLoadGame()がtitle_menu -> load_menu、load 成功時にpreparationを直接代入。main.ts: reset と E2E auto-start が phase を直接変更。SortieSystem.ts:confirmResult()がresult -> preparationを直接代入。SortieSystem.ts:startSortie()がpreparation -> runningを直接代入。SortieSystem.ts: terminal outcome がrunning -> resultを直接代入。したがって、現行 Allowed Paths では「PhaseTransitionSystem 経由に変更する」ことはできません。
HudController.tsだけを変えても、invalid transition の防止にはなりません。修正案:
本当に centralization するなら Allowed Paths に最低限これを追加してください。
endedを残すならさらに:3. #1174 の「HudController の phase 変更コード」という前提が現実とずれている
#1174 は
src/ui/HudController.tsの phase 変更コードを PhaseTransitionSystem 経由に変更すると書いています。 しかし、現行HudController.tsは phase mutation の主体ではありません。主体はmain.tsの callback 実装とSortieSystem.tsの domain system です。このまま実装者に渡すと、以下のような中途半端な PR が生まれます。
これは UI 表面の整合には役立ちますが、実際の phase mutation を止めません。
invalid transition 防止という #1174 の Outcome には届きません。
4.
LoopPhase契約が「現行 table」と「legacy debrief」の間で曖昧GameState.tsのコメント上の transition policy は、現在title_menu -> preparation -> running -> result -> preparationを中心に記述され、Save はpreparationのみ、Load はtitle_menu / load_menuのみと明記されています。 一方でLoopPhase型と実装にはdebrief_pending_reward/debrief_reward_claimedがまだ残っています。SortieSystem.tsも legacy debrief をまだ扱っています。claimPendingReward()はresultphase では reward を claimed にして result に留まり、legacydebrief_pending_rewardではdebrief_reward_claimedに遷移します。#1174 は result→claim→debrief orchestration を Out of Scope としていますが、遷移表には debrief phase を含めています。 このままだと、実装者が次のどちらをすべきか判断できません。
修正案:
PhaseTransitionSystem の table に
legacy: true相当の意味を持たせる必要があります。少なくともテスト名で明示してください。これを明示しないと、#619 / #771 で整理した TitleMenu / LoadMenu / Result 契約と衝突します。#619 は Save preparation-only、Load title/load-only、running→result→preparation の流れを定義しています。 #771 も HUD action surface をこの契約へ正規化するための Issue でした。
5. PhaseTransitionSystem が side effect を持つと設計を壊す
現在の phase transition は単なる
from -> toではありません。多くの遷移が副作用を伴います。startSortie()は phase をrunningにするだけでなく、combat runtime reset、targetTicks 計算、ally 初期化、playtest evidence 開始などを行います。runSortieSystem()は terminal result の生成、reward application id の採番、evidence 記録、self-explanation prompt 設定まで持っています。confirmResult()は pending reward を auto-claim してからpreparationに戻します。したがって PhaseTransitionSystem は、少なくとも当面は pure validator / resolver に留めるべきです。直接
GameStateを包括的に mutate する巨大 system にすると、SortieSystem、storage、evidence、HUD feedback、pause reset の責務を吸い込み、逆に境界が壊れます。推奨 API はこの程度です。
そのうえで、
main.ts/SortieSystem.tsの既存 side-effectful 関数が phase 代入直前にresolvePhaseTransition()を使う構成が安全です。6. 起動 phase が二重管理されている
createInitialGameState()はloopPhase: 'preparation'を返します。 しかし app shell では起動時にstate.loopPhase = 'title_menu'へ明示的に上書きしています。 テストでもこの差分は「createInitialGameState は preparation、main.ts が title_menu にする」と文書化されています。PhaseTransitionSystem を導入するなら、この二重性をどう扱うかを決める必要があります。
preparationのままにするのか[bootstrap] -> title_menuを PhaseTransitionSystem に入れるのかtitle_menu -> preparation -> runningを validation 対象にするのか現行 #1174 は Allowed Paths に
main.tsを含めていないため、この問題を解決できません。関連 PR 履歴から見たリスク
PR #868 は #619 の phase state machine 実装で、
LoopPhaseにtitle_menu / load_menu / resultを追加し、Save はpreparationのみ、Load はtitle_menu / load_menuのみ、running -> result -> confirmResult -> preparationの流れを確立した PR です。PR #877 は #771 の HUD action surface 正規化で、
onNewGame()の追加、onStartSortie()の preparation-only 化、onConfirmResult()の save feedback 修正、Load Game spy test 追加を扱っています。 ただし #877 のレビューでは、tests/main-loop.test.tsの AC3 spy tests が「実装コードを呼ばないトートロジー」と指摘されています。この履歴から、#1174 のテストは特に危険です。
tests/phase-transition-system.test.tsだけで table を複製して確認しても、実際のmain.ts/SortieSystem.tsの mutation が残れば何も防げません。TypeScript 実装品質の推奨
PhaseTransitionSystem は TypeScript の型でできるだけ閉じるべきです。公式 TypeScript docs は
neverを switch の exhaustiveness checking に使い、新しい union member が追加されたときに compile error にできると説明しています。(TypeScript)推奨は以下です。
ここで重要なのは、
LoopPhaseにないendedを table に混ぜないことです。endedを含めたいならGameState.tsの型を先に変える必要があります。DOM / HUD との境界
HudControllerの disabled button tests は価値があります。HTML のdisabledform control は click/focus 等の browsing events を受けず、focus もされず、form submission にも含まれないため、DOM affordance の安全性を固定する意味があります。([MDN Web Docs]2)ただし、disabled は business rule の唯一の防御線ではありません。
PhaseTransitionSystem の目的が invalid transition 防止なら、button disabled ではなく、
main.ts/SortieSystem.tsの side-effectful entrypoint 側で validation しなければ不十分です。また #1173 と UI information architecture は、通常 UI に
LoopPhase、Telemetry、Claim reward、raw internal state を出さないことを求めています。docs/product/features/ui-information-architecture.mdも、Canvas は world / visual cue、DOM HUD は player-facing status / command、raw state は debug panel に隔離すると定義しています。 禁止語彙としてLoopPhase、Telemetry、Claim reward、raw internal state name も明記されています。したがって PhaseTransitionSystem の error message を HUD にそのまま出すのは不可です。error は test/debug/evidence 用、通常 HUD は player-facing copy に変換してください。
Save / Load / 配布物実装に関する注意
#1174 は save/load phase gate を Out of Scope としていますが、現行システムでは Save/Load policy は phase contract と強く結びついています。
accessibility-save-policy.mdは Save をpreparationのみ、Load Game をtitle_menu / load_menuのみと明記しています。GameSnapshotもschemaVersion / resources / weaponPower / playerMaxHpだけで、runtime state、DOM/HUD/Canvas state、current HP は保存対象外です。Web Storage は同期 API で、操作中は JS 実行をブロックします。大きなデータや性能懸念がある場合は IndexedDB が適します。([MDN Web Docs]3) また Web Storage は string-only key/value で、一般に localStorage 5 MiB / sessionStorage 5 MiB 程度の quota があり、超過時は
QuotaExceededErrorを扱う必要があります。([MDN Web Docs]4)#1174 でやるべきことは、storage を再設計することではなく、storage を呼ぶ phase entrypoint が PhaseTransitionSystem の validation を迂回しないことです。
改稿すべき #1174 契約
現行 Issue はこのままだと実装者に誤誘導を与えます。最低限、次のように直すべきです。
Allowed Paths
endedを残すなら追加:In Scope の修正文
Out of Scope の補強
必須テスト
tests/phase-transition-system.test.tsだけでは不十分です。以下を必須にしてください。特に
rg -n "loopPhase\\s*=" srcは重要です。残る direct assignment があるなら、次のどれかに分類してください。最終結論
#1174 は、目的は正しいですが、現行契約は Allowed Paths、対象 phase、実 mutation surface、legacy debrief、test strategy が噛み合っていません。
このまま PR を作ると、
PhaseTransitionSystem.tsと unit test は追加されるが、main.ts/SortieSystem.tsの direct mutation が残り、invalid transition 防止という Outcome は満たせない可能性が高いです。実装前にやるべき修正は明確です。
endedを削除するか、GameState.tsを Allowed Paths に入れて正式導入する。main.tsとSortieSystem.tsを Allowed Paths に追加する。この修正を入れてからなら、#1174 は M4 Upgrade Loop の HUD 露出依存にも耐える、妥当な基盤 Issue になります。
squne121 commentedon Jul 1, 2026
Contract Review Result
squne121 commentedon Jul 1, 2026
Refinement Loop: Approved
Issue #1174 は owner comment
https://github.com/squne121/loop-protocol/issues/1174#issuecomment-4853340922を一次ソースとして再構成し、deterministic review と contract review の両方でapprove / goを確認しました。実装着手に必要な contract snapshot も最新 body に対して materialize 済みです。approvedhttps://github.com/squne121/loop-protocol/issues/1174#issuecomment-4853406797sha256:fe1caa50e6fdc9464c2fb2dc199065969ab73b2220883fa091e98e652f955ac1fix: validate bootstrap phase transition (#1174)
refactor: make loop phase transitions intent-driven (#1174)
feat: centralize loop phase transitions (#1174) (#1257)