Repository navigation
Closed
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phase
Milestone
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
parent_issue: "#733"
goal_ref: "M3: Loop MVP Gate — terminal SortieResult から reward_formula_version: 1 の報酬を算出し resources へ exactly-once で反映する RewardSystem を追加する"
change_kind: codeParent Issue
Parent Goal Ref
- Goal: M3: Loop MVP Gate (v0.3.x) として sortie 結果から報酬を獲得し localStorage へ進行を永続化して reload 後に復元する
- Desired Destination:
src/systems/RewardSystem.tsが terminalSortieResultから reward を pure に算出し、progress.resourcesへの state transition を exactly-once で提供する
Current Validated Scope
src/state/GameState.tsにはProgressState { stageLabel, resources, weaponPower }、SortieResultdiscriminated union(outcome: 'victory' | 'defeat' | 'timeout'/endReason/durationMs/kills/shotsFired/playerHpRemaining)が実装済みsrc/state/GameState.tsのGameSnapshotはschemaVersion: 1を持つが、reward claim 用の runtime token / latch / consumed ledger は未実装src/systems/SortieSystem.tsは terminalSortieResultを生成してstate.sortieに格納するが、sortie lifecycle terminal transition event を識別するapplicationId/ consumed token /rewardApplied相当は未実装src/systems/CombatSystem.tsは damage / defeat を担当し、progress.resourcesを直接 mutate しない責務境界を維持しているdocs/product/features/resource.mdはreward_formula_version: 1、RESOURCE_CAP = 9_999_999、PER_SORTIE_REWARD_CAP = 500、victory / defeat / timeout の reward 差分、invalid 値処理、exactly-once 単位が value identity ではなく 1 sortie lifecycle の terminal transition event であることを normative に定義しているdocs/product/features/persistence.mdはSortieResultを永続化しないこと、reload 後に復元されるのは progression snapshot のみであること、reload 後の再 claim 不可は save / reload integration の責務であることを定義しているsrc/systems/RewardSystem.tsとtests/reward-system.test.tsは未作成
Remaining Parent Gaps
- debrief / preparation フェーズで terminal transition token をどこで生成・消費するかの wiring(別 child issue: 実装: sortie 後の debrief / preparation フェーズを導入する #738)
- reward claim 後の progression snapshot 保存と reload 後の復元(別 child issue: 実装: 報酬確定時に progression snapshot を localStorage へ保存する #739)
- browser runtime での sortie→reward→reload E2E / playtest 証跡(別 child issue: 実装: M3 loop の sortie→reward→reload 復元を E2E / playtest で検証する #740)
Outcome
src/systems/RewardSystem.ts が RewardSystem.calculate(result) と RewardSystem.claim(state, applicationId, result) を export し、calculate() は terminal SortieResult から reward_formula_version: 1 の RewardQuote を pure に算出し、claim() は同一 applicationId に対して progress.resources への saturated addition を最大 1 回だけ行う。tests/reward-system.test.ts には victory / defeat / timeout の exact formula、per-sortie cap、RESOURCE_CAP への saturated addition、invalid 値 fallback、same-token double-claim 防止、same-payload different-token claim を含む regression test が追加され、pnpm typecheck / pnpm lint / pnpm test / pnpm build が通る状態にする。
In Scope
src/systems/RewardSystem.ts新規作成RewardSystem.calculate(result: SortieResult): RewardQuoteの実装RewardSystem.claim(state: GameState, applicationId: RewardApplicationId, result: SortieResult): RewardClaimResultの実装REWARD_FORMULA_VERSION = 1、RESOURCE_CAP = 9_999_999、PER_SORTIE_REWARD_CAP = 500の単一定義- victory / defeat / timeout の exact reward formula 実装
- reward delta と
resourcesAfterの non-negative safe integer / clamp / saturated addition 実装 src/state/GameState.tsへの runtime-only reward claim ledger / token 消費状態の追加(applicationId重複適用防止に必要な最小範囲)- same-token double-claim 防止
- same-payload different-token claim を正当加算として扱う決定論テスト
tests/reward-system.test.ts新規作成src/systems/index.tsの export 追加(必要な場合のみ)
Out of Scope
SortieSystem.ts側で terminal transition token を生成する wiring- debrief / preparation UI や button 連打ハンドリングそのもの
- localStorage への保存 wiring
- reload 後の再 claim 不可の end-to-end 証明
- storage failure handling
CombatSystemによるresourcesの直接 mutate- weapon upgrade / resource 消費ロジック(M4 スコープ)
- reward coefficient の最終 tuning
Acceptance Criteria
- AC1:
src/systems/RewardSystem.tsがRewardSystem.calculate()とRewardSystem.claim()を export する - AC2:
RewardSystem.calculate()は terminalSortieResultだけからRewardQuoteを pure に算出し、同一入力から同一出力を返す - AC3: victory / defeat / timeout の reward が
docs/product/features/resource.mdの exact formula に一致する - AC4: reward delta は
PER_SORTIE_REWARD_CAP = 500で clamp され、resourcesAfterはRESOURCE_CAP = 9_999_999への saturated addition になる - AC5: invalid 値(
NaN/Infinity/ 負値 / 小数 / unsafe integer)を含むresultまたはresourcesBeforeでも reward / resources を壊さず、spec に沿った fallback を行う - AC6:
RewardSystem.claim()は同一applicationIdを 2 回以上渡してもprogress.resourcesを最大 1 回しか加算しない - AC7: 同一 payload の別 sortie を表す異なる
applicationIdでは、同じSortieResult内容でも別 reward application として加算される - AC8:
CombatSystemはprogress.resourcesを直接 mutate しない既存責務境界を破壊しない - AC9:
tests/reward-system.test.tsが victory / defeat / timeout exact value、cap、invalid fallback、same-token double-claim、same-payload different-token claim を検証する - AC10:
pnpm typecheck/pnpm lint/pnpm test/pnpm buildが通る
Verification Commands
# AC1
$ test -f src/systems/RewardSystem.ts
# AC2
# preflight-scope: pr_review_only
$ rg -n "pure|RewardQuote|formulaVersion" src/systems/RewardSystem.ts tests/reward-system.test.ts
# AC3
# preflight-scope: pr_review_only
$ rg -n "victory|defeat|timeout" tests/reward-system.test.ts
# preflight-scope: pr_review_only
$ rg -n "100|10|30|kills \\* 5|playerHpRemaining" tests/reward-system.test.ts
# AC4
# preflight-scope: pr_review_only
$ rg -n "PER_SORTIE_REWARD_CAP|RESOURCE_CAP|saturated" src/systems/RewardSystem.ts tests/reward-system.test.ts
# AC5
# preflight-scope: pr_review_only
$ rg -n "NaN|Infinity|fraction|negative|unsafe" tests/reward-system.test.ts
# AC6
# preflight-scope: pr_review_only
$ rg -n "applicationId|already-claimed|double-claim|same token" src/systems/RewardSystem.ts tests/reward-system.test.ts
# AC7
# preflight-scope: pr_review_only
$ rg -n "different token|different applicationId|same payload" tests/reward-system.test.ts
# AC8
# preflight-scope: pr_review_only
$ rg -n "progress\\.resources" src/systems/CombatSystem.ts
# AC9
$ test -f tests/reward-system.test.ts
# AC10
# preflight-scope: pr_review_only
$ pnpm typecheck
# preflight-scope: pr_review_only
$ pnpm lint
# preflight-scope: pr_review_only
$ pnpm test
# preflight-scope: pr_review_only
$ pnpm buildStop Conditions
- Allowed Paths 外の変更が必要と判明した場合
- In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
- 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
- 後続 Phase / 別スコープへの波及が判明した場合
- nested SubAgent delegation が必要になった場合
- 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
- terminal transition token を
SortieSystem.ts側で新規生成しないとapplicationId契約を成立できないと判明した場合(実装: sortie 後の debrief / preparation フェーズを導入する #738 へ defer するか Allowed Paths 改訂の人間判断が必要) - reward claim state を snapshot 永続化したくなった場合(
docs/product/features/persistence.mdと衝突するため新規 contract review が必要) - reload 後の再 claim 不可をこの issue 単体で証明しないと AC が満たせないと判明した場合(実装: 報酬確定時に progression snapshot を localStorage へ保存する #739 / 実装: M3 loop の sortie→reward→reload 復元を E2E / playtest で検証する #740 に defer する)
Runtime Verification Applicability
- decision: deferred
- reason: same-token double-claim の unit test は本 issue で検証できるが、reload 後に同じ reward が再 claim 不能であることは debrief wiring・snapshot 保存・reload 復元がそろって初めて成立するため
- deferred_destination:
- destination_type: issue
- destination_ref: "実装: M3 loop の sortie→reward→reload 復元を E2E / playtest で検証する #740"
- deferred_verification_condition: 実装: sortie 後の debrief / preparation フェーズを導入する #738 で debrief / preparation へ terminal transition token が正しく流れ、実装: 報酬確定時に progression snapshot を localStorage へ保存する #739 で reward claim 後 snapshot 保存と reload 復元が接続された上で、実装: M3 loop の sortie→reward→reload 復元を E2E / playtest で検証する #740 の browser runtime E2E / playtest で sortie→reward→reload 復元を通して同一 reward の再 claim 不可を確認できること
Allowed Paths
src/systems/RewardSystem.tssrc/state/GameState.tssrc/systems/index.tstests/reward-system.test.ts
Required Skills
- TypeScript / TSC strict
- ECS(state / systems 分離)
- deterministic state transition 設計
- Vitest BDD
Metadata
Metadata
Assignees
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phase
Activity
docs(#735): レビュー指摘の妥当分を反映(consumers/validation/preview/exactly-once)
docs(#735): M3 reward / resource / persistence feature spec を作成する (#748)
squne121 commentedon Jun 8, 2026
判定
CHANGES REQUESTED。#737 はこのまま実装に入ると危険です。
理由は、#737 本文の「同一
SortieResult→ 一度だけ反映」という表現が、すでに #735 / PR #748 /resource.mdで修正された lifecycle terminal transition event 単位の exactly-once とズレているためです。値同一性で二重加算を防ぐ実装に誘導すると、同一 payload の別 sortie を誤って握り潰すか、逆に同一 sortie の clone/re-render/retry を二重加算します。#737 は現在 open で、公開ページ上では related PR / branch はまだありません。(GitHub)確認した主対象は #737、parent #733、#735 と merged PR #748、#736/#738/#739、現行
resource.md/persistence.md、GameState.ts/SortieSystem.ts、および localStorage / Vite 配布 / Playwright 検証まわりの公式ドキュメントです。1. 最大の欠陥: 「同一 result」の定義が実装者を壊す
#737 は
RewardSystem.tsを作り、SortieResultから決定論的に報酬を計算し、progress.resourcesに一度だけ反映することを要求しています。In Scope でも「同一 result の反映は一度のみ」、AC4 でも「報酬反映はprogress.resourcesに対して一度のみ」と書かれています。(GitHub)しかし、これはそのままでは不十分です。PR #748 のレビューで、
exactly-onceの単位は object identity でもJSON.stringify(result)でも shallow equality でもなく、1 sortie lifecycle の terminal transition event とする必要がある、と明確に指摘されています。同一内容のSortieResultが別 sortie で発生し得るため、値ベース重複排除は正当な報酬まで落とします。(GitHub)その後の PR 対応でも、この点は反映済みです。
resource.mdは「value identity ではなく terminal transition event」「同一 payload の別 sortie は別 application」「JSON.stringify/ shallow equality 禁止」「rewardAppliedlatch または consumed token」を要求しています。(GitHub)要求修正: #737 の本文から「同一 result」という曖昧語を削り、次に置き換えるべきです。
calculate()は完全 pure。claim()はapplicationId/ consumed token /rewardAppliedlatch による state transition です。parent #733 も、#737 はRewardSystem.calculate()とRewardSystem.claim()を分け、apply-once の実装根拠を持つことを child contract としています。(GitHub)2. #737 の Allowed Paths では、正しい exactly-once を完結できない可能性が高い
現行
SortieSystem.tsは terminal 到達時にresultを作ってstate.sortie = terminalStateへ入れていますが、そこに sortie instance id、terminal transition id、pending reward、claimed flag のような識別子はありません。SortieStateも terminal state にresultを持つだけです。(GitHub)一方で #737 の Allowed Paths は
RewardSystem.ts、GameState.ts、systems/index.ts、tests/reward-system.test.tsのみです。SortieSystem.tsは含まれていません。(GitHub)parent #733 は、再 claim・ダブルクリック・reload 跨ぎの二重加算防止に、
pendingReward/ claimed flag /lastClaimedSortieId相当の識別子を #737 / #738 契約に含めると明記しています。(GitHub)敵対的に見ると、ここは stop condition です。
#737 が lifecycle token を生成・消費する責務まで持つなら、Allowed Paths に
src/systems/SortieSystem.tsを追加しないと不自然です。逆に #737 を純粋なcalculate()と token-awareclaim()の API 定義までに絞るなら、terminal token の生成と debrief wiring は #738 に明示的に defer すべきです。3. reload 後の二重 claim は #737 だけでは証明できない
#737 は localStorage wiring を Out of Scope にしています。(GitHub)
しかし parent #733 の Additional AC は、claim 済み reward が reload 後に再 claim 可能にならないことまで要求しています。(GitHub)
この reload 境界は #739 の責務です。#739 は報酬確定後に progression snapshot を localStorage へ保存し、page reload 後に
resourcesを復元する保存ポイントを wiring する issue です。さらに #739 は #736、#738、#621 の完了を前提にしています。(GitHub)persistence.mdも、SortieResultは localStorage に書かず、reload 後に復元されるのは適用済みの progression snapshot のみで、SortieResultを再構築・再計算しない、と定めています。(GitHub)要求修正: #737 の AC4 は「同一 runtime terminal transition に対する in-memory claim は一度のみ」に狭める。reload 後の再 claim 不可は #739 / #740 の integration / E2E に移す。#737 単体で reload exactly-once を満たしたと主張してはいけません。
4. 報酬式はすでに
resource.mdが正本。実装で再解釈してはいけないresource.mdはreward_formula_version: 1の固定値を定義済みです。base reward は victory 100、defeat 10、timeout 30、kill bonus はkills * 5、victory のみplayerHpRemaining * 1、per-sortie cap は 500 です。(GitHub)resources側は non-negative safe integer、初期値 0、RESOURCE_CAP = 9_999_999、overflow は cap clamp、負値 /NaN/Infinity/ 小数は invalid fallback 0 と定義されています。(GitHub)parent #733 も、
resourcesは有限非負整数、RESOURCE_CAP上限、saturated addition、reward がNaN/Infinity/ 負値 / 小数を返さないことを AC にしています。(GitHub)要求修正: #737 の
RewardSystem.tsには、少なくとも以下を明示的に export するべきです。さらに、テストは「victory / defeat / timeout の差がある」だけでは弱いです。以下の exact value を検証すべきです。
Math.floor(NaN)はNaNを返すため、killsやplayerHpRemainingに poisoned value が入った時点で計算結果が壊れます。TypeScript の型だけに依存せず、unknown as SortieResultでNaN/Infinity/ negative / fraction を流すテストを追加してください。5. timeout は independent outcome。#738 側がまだ古い
現行
GameState.tsのSortieResultはoutcome: 'victory' | 'defeat' | 'timeout'の discriminated union です。timeoutはendReason: 'timeout'を持つ独立 variant です。(GitHub)SortieSystem.tsも defeat > victory > timeout の優先順位で terminal 判定し、timeout branch ではoutcome: 'timeout'/endReason: 'timeout'を作っています。(GitHub)resource.mdも、timeout を defeat と混同してはならない neutral terminal と定めています。(GitHub)ところが #738 の Current Validated Scope は
SortieStatus = 'idle' | 'running' | 'victory' | 'defeat' | 'ended'と記載しており、timeout が抜けています。Outcome / AC1 も victory / defeat 到達後の debrief だけを明記しています。(GitHub)要求修正: #738 の issue contract を修正し、
victory | defeat | timeout全 terminal が debrief に入ることを AC に入れるべきです。#737 が timeout reward を正しく計算しても、#738 が timeout を debrief / save path に流さなければ M3 loop は壊れます。6.
calculate()とclaim()の責務分離が必須README は
systemsが DOM / Canvas API に依存しないこと、更新は fixed timestep 60Hz で進めることをアーキテクチャ方針にしています。(GitHub)parent #733 も、storage アクセスは adapter に隔離し、systems 層・60Hz update loop から呼ばないと定めています。(GitHub)
したがって、#737 の
RewardSystem.tsはlocalStorage、document、Canvas、wall-clock、乱数に触ってはいけません。calculate()はSortieResult→RewardQuoteの pure function。claim()はGameStateのprogress.resourcesを変更する唯一の state transition。保存は #739、storage failure は #621、runtime E2E は #740 です。推奨戻り値は例として次です。
例外 throw より union result の方が、UI / debrief / save failure の後続設計と接続しやすいです。
7.
GameSnapshot/ProgressStateの混同に注意現行
ProgressStateはstageLabel,resources,weaponPowerだけです。GameSnapshotはschemaVersion,resources,weaponPower,playerMaxHpを持ちます。createGameSnapshot()もこの 4 フィールドを返します。(GitHub)#736 は、schemaVersion missing は legacy v0 migration、unknown は null fallback、
resourcesは non-negative safe integer、overflow はRESOURCE_CAPclamp、SortieResultは永続化しない、と契約修正しています。(GitHub)要求修正: #737 で
pendingReward/rewardApplied/lastClaimedSortieId相当を足す場合、それが runtime-only なのか、snapshot に含むのかを明示してください。現行persistence.mdはSortieResultや sortie runtime を永続化しない設計です。pendingRewardを snapshot に入れると、その設計と衝突します。(GitHub)現実的には、#737 は runtime-only latch / consumed token を使い、保存されるのは claim 後の
resourcesだけにするのが安全です。8. localStorage / 配布物まわりの調査結果を反映すべき点
#737 自体は storage を触らないべきですが、exactly-once の設計は storage と配布環境の制約を前提にしておく必要があります。
MDN によると
localStorageは origin 単位で分割され、同じ origin の document は同じ localStorage 領域を共有します。またfile:URL の localStorage 要件は未定義で、SecurityError が発生し得ます。(MDNウェブドキュメント)Web Storage は同期 API なので、set/get/remove は他の JavaScript 実行をブロックし得ます。毎 tick 保存しない #739 の方針は妥当です。(MDNウェブドキュメント)
容量面では Web Storage は全ブラウザで最大 10 MiB、localStorage は origin あたり 5 MiB とされ、上限到達時は
QuotaExceededErrorを try/catch で扱うべきです。(MDNウェブドキュメント)Vite の本番ビルドは
vite buildにより静的ホスティング向け bundle を生成します。Vite の static deploy docs は既定出力をdistとし、vite previewはdistをhttp://localhost:4173で配信するローカル確認用サーバーで、本番サーバーではないと明記しています。(vitejs)したがって、M3 の runtime smoke test は
file://ではなく HTTP origin 上で実施する必要があります。これは parent #733 のdist/を HTTP origin 経由で smoke test する AC とも一致します。(GitHub)また、PR #748 のレビューでも指摘されている通り、GitHub Pages の
/loop-protocol/本番と/loop-protocol/pr-748/preview は同一 origin になり得るため、同じloop-protocol.mvp.savekey を共有します。persistence.mdはこのリスクを反映し、E2E / preview では test 前に key clear、必要なら preview key suffix を検討するとしています。(GitHub)9. Verification Commands が弱すぎる
#737 の verification は
rg -n "victory|defeat|timeout"やrg -n "二重|once|already|applied"のような grep が多いです。(GitHub)これは仕様語をコメントに書けば通ります。敵対的には、次の破損実装でも grep は通ります。
これは同一 payload の別 sortie を潰すため、
resource.mdの禁止事項に反します。(GitHub)最低限、
tests/reward-system.test.tsは次を検証してください。Playwright は browser context ごとに isolation され、storage state には localStorage snapshot も含まれます。#740 の E2E では、この性質を使って HTTP origin 上の localStorage を実際に inspect するのが妥当です。(Playwright)
10. #737 本文への具体的な修正案
#737 は以下のように contract を修正してから実装すべきです。
pendingReward / claimed state を snapshot に永続化したくなった場合は、persistence.md と衝突するため新規 contract review が必要
最終レビューコメント案
## 判定CHANGES REQUESTED。#737 はこのまま実装に入ると危険です。
理由は、#737 本文の「同一
SortieResult→ 一度だけ反映」という表現が、すでに #735 / PR #748 /resource.mdで修正された lifecycle terminal transition event 単位の exactly-once とズレているためです。値同一性で二重加算を防ぐ実装に誘導すると、同一 payload の別 sortie を誤って握り潰すか、逆に同一 sortie の clone/re-render/retry を二重加算します。#737 は現在 open で、公開ページ上では related PR / branch はまだありません。([GitHub]1)確認した主対象は #737、parent #733、#735 と merged PR #748、#736/#738/#739、現行
resource.md/persistence.md、GameState.ts/SortieSystem.ts、および localStorage / Vite 配布 / Playwright 検証まわりの公式ドキュメントです。1. 最大の欠陥: 「同一 result」の定義が実装者を壊す
#737 は
RewardSystem.tsを作り、SortieResultから決定論的に報酬を計算し、progress.resourcesに一度だけ反映することを要求しています。In Scope でも「同一 result の反映は一度のみ」、AC4 でも「報酬反映はprogress.resourcesに対して一度のみ」と書かれています。([GitHub]1)しかし、これはそのままでは不十分です。PR #748 のレビューで、
exactly-onceの単位は object identity でもJSON.stringify(result)でも shallow equality でもなく、1 sortie lifecycle の terminal transition event とする必要がある、と明確に指摘されています。同一内容のSortieResultが別 sortie で発生し得るため、値ベース重複排除は正当な報酬まで落とします。([GitHub]2)その後の PR 対応でも、この点は反映済みです。
resource.mdは「value identity ではなく terminal transition event」「同一 payload の別 sortie は別 application」「JSON.stringify/ shallow equality 禁止」「rewardAppliedlatch または consumed token」を要求しています。([GitHub]2)要求修正: #737 の本文から「同一 result」という曖昧語を削り、次に置き換えるべきです。
calculate()は完全 pure。claim()はapplicationId/ consumed token /rewardAppliedlatch による state transition です。parent #733 も、#737 はRewardSystem.calculate()とRewardSystem.claim()を分け、apply-once の実装根拠を持つことを child contract としています。([GitHub]3)2. #737 の Allowed Paths では、正しい exactly-once を完結できない可能性が高い
現行
SortieSystem.tsは terminal 到達時にresultを作ってstate.sortie = terminalStateへ入れていますが、そこに sortie instance id、terminal transition id、pending reward、claimed flag のような識別子はありません。SortieStateも terminal state にresultを持つだけです。([GitHub]4)一方で #737 の Allowed Paths は
RewardSystem.ts、GameState.ts、systems/index.ts、tests/reward-system.test.tsのみです。SortieSystem.tsは含まれていません。([GitHub]1)parent #733 は、再 claim・ダブルクリック・reload 跨ぎの二重加算防止に、
pendingReward/ claimed flag /lastClaimedSortieId相当の識別子を #737 / #738 契約に含めると明記しています。([GitHub]3)敵対的に見ると、ここは stop condition です。
#737 が lifecycle token を生成・消費する責務まで持つなら、Allowed Paths に
src/systems/SortieSystem.tsを追加しないと不自然です。逆に #737 を純粋なcalculate()と token-awareclaim()の API 定義までに絞るなら、terminal token の生成と debrief wiring は #738 に明示的に defer すべきです。3. reload 後の二重 claim は #737 だけでは証明できない
#737 は localStorage wiring を Out of Scope にしています。([GitHub]1)
しかし parent #733 の Additional AC は、claim 済み reward が reload 後に再 claim 可能にならないことまで要求しています。([GitHub]3)
この reload 境界は #739 の責務です。#739 は報酬確定後に progression snapshot を localStorage へ保存し、page reload 後に
resourcesを復元する保存ポイントを wiring する issue です。さらに #739 は #736、#738、#621 の完了を前提にしています。([GitHub]5)persistence.mdも、SortieResultは localStorage に書かず、reload 後に復元されるのは適用済みの progression snapshot のみで、SortieResultを再構築・再計算しない、と定めています。([GitHub]6)要求修正: #737 の AC4 は「同一 runtime terminal transition に対する in-memory claim は一度のみ」に狭める。reload 後の再 claim 不可は #739 / #740 の integration / E2E に移す。#737 単体で reload exactly-once を満たしたと主張してはいけません。
4. 報酬式はすでに
resource.mdが正本。実装で再解釈してはいけないresource.mdはreward_formula_version: 1の固定値を定義済みです。base reward は victory 100、defeat 10、timeout 30、kill bonus はkills * 5、victory のみplayerHpRemaining * 1、per-sortie cap は 500 です。([GitHub]7)resources側は non-negative safe integer、初期値 0、RESOURCE_CAP = 9_999_999、overflow は cap clamp、負値 /NaN/Infinity/ 小数は invalid fallback 0 と定義されています。([GitHub]7)parent #733 も、
resourcesは有限非負整数、RESOURCE_CAP上限、saturated addition、reward がNaN/Infinity/ 負値 / 小数を返さないことを AC にしています。([GitHub]3)要求修正: #737 の
RewardSystem.tsには、少なくとも以下を明示的に export するべきです。さらに、テストは「victory / defeat / timeout の差がある」だけでは弱いです。以下の exact value を検証すべきです。
Math.floor(NaN)はNaNを返すため、killsやplayerHpRemainingに poisoned value が入った時点で計算結果が壊れます。TypeScript の型だけに依存せず、unknown as SortieResultでNaN/Infinity/ negative / fraction を流すテストを追加してください。5. timeout は independent outcome。#738 側がまだ古い
現行
GameState.tsのSortieResultはoutcome: 'victory' | 'defeat' | 'timeout'の discriminated union です。timeoutはendReason: 'timeout'を持つ独立 variant です。([GitHub]8)SortieSystem.tsも defeat > victory > timeout の優先順位で terminal 判定し、timeout branch ではoutcome: 'timeout'/endReason: 'timeout'を作っています。([GitHub]4)resource.mdも、timeout を defeat と混同してはならない neutral terminal と定めています。([GitHub]7)ところが #738 の Current Validated Scope は
SortieStatus = 'idle' | 'running' | 'victory' | 'defeat' | 'ended'と記載しており、timeout が抜けています。Outcome / AC1 も victory / defeat 到達後の debrief だけを明記しています。([GitHub]9)要求修正: #738 の issue contract を修正し、
victory | defeat | timeout全 terminal が debrief に入ることを AC に入れるべきです。#737 が timeout reward を正しく計算しても、#738 が timeout を debrief / save path に流さなければ M3 loop は壊れます。6.
calculate()とclaim()の責務分離が必須README は
systemsが DOM / Canvas API に依存しないこと、更新は fixed timestep 60Hz で進めることをアーキテクチャ方針にしています。([GitHub]10)parent #733 も、storage アクセスは adapter に隔離し、systems 層・60Hz update loop から呼ばないと定めています。([GitHub]3)
したがって、#737 の
RewardSystem.tsはlocalStorage、document、Canvas、wall-clock、乱数に触ってはいけません。calculate()はSortieResult→RewardQuoteの pure function。claim()はGameStateのprogress.resourcesを変更する唯一の state transition。保存は #739、storage failure は #621、runtime E2E は #740 です。推奨戻り値は例として次です。
例外 throw より union result の方が、UI / debrief / save failure の後続設計と接続しやすいです。
7.
GameSnapshot/ProgressStateの混同に注意現行
ProgressStateはstageLabel,resources,weaponPowerだけです。GameSnapshotはschemaVersion,resources,weaponPower,playerMaxHpを持ちます。createGameSnapshot()もこの 4 フィールドを返します。([GitHub]8)#736 は、schemaVersion missing は legacy v0 migration、unknown は null fallback、
resourcesは non-negative safe integer、overflow はRESOURCE_CAPclamp、SortieResultは永続化しない、と契約修正しています。([GitHub]11)要求修正: #737 で
pendingReward/rewardApplied/lastClaimedSortieId相当を足す場合、それが runtime-only なのか、snapshot に含むのかを明示してください。現行persistence.mdはSortieResultや sortie runtime を永続化しない設計です。pendingRewardを snapshot に入れると、その設計と衝突します。([GitHub]6)現実的には、#737 は runtime-only latch / consumed token を使い、保存されるのは claim 後の
resourcesだけにするのが安全です。8. localStorage / 配布物まわりの調査結果を反映すべき点
#737 自体は storage を触らないべきですが、exactly-once の設計は storage と配布環境の制約を前提にしておく必要があります。
MDN によると
localStorageは origin 単位で分割され、同じ origin の document は同じ localStorage 領域を共有します。またfile:URL の localStorage 要件は未定義で、SecurityError が発生し得ます。([MDNウェブドキュメント]12)Web Storage は同期 API なので、set/get/remove は他の JavaScript 実行をブロックし得ます。毎 tick 保存しない #739 の方針は妥当です。([MDNウェブドキュメント]13)
容量面では Web Storage は全ブラウザで最大 10 MiB、localStorage は origin あたり 5 MiB とされ、上限到達時は
QuotaExceededErrorを try/catch で扱うべきです。([MDNウェブドキュメント]14)Vite の本番ビルドは
vite buildにより静的ホスティング向け bundle を生成します。Vite の static deploy docs は既定出力をdistとし、vite previewはdistをhttp://localhost:4173で配信するローカル確認用サーバーで、本番サーバーではないと明記しています。([vitejs]15)したがって、M3 の runtime smoke test は
file://ではなく HTTP origin 上で実施する必要があります。これは parent #733 のdist/を HTTP origin 経由で smoke test する AC とも一致します。([GitHub]3)また、PR #748 のレビューでも指摘されている通り、GitHub Pages の
/loop-protocol/本番と/loop-protocol/pr-748/preview は同一 origin になり得るため、同じloop-protocol.mvp.savekey を共有します。persistence.mdはこのリスクを反映し、E2E / preview では test 前に key clear、必要なら preview key suffix を検討するとしています。([GitHub]2)9. Verification Commands が弱すぎる
#737 の verification は
rg -n "victory|defeat|timeout"やrg -n "二重|once|already|applied"のような grep が多いです。([GitHub]1)これは仕様語をコメントに書けば通ります。敵対的には、次の破損実装でも grep は通ります。
これは同一 payload の別 sortie を潰すため、
resource.mdの禁止事項に反します。([GitHub]7)最低限、
tests/reward-system.test.tsは次を検証してください。RESOURCE_CAP = 9_999_999への saturated additionNaN/Infinity/ negative / fraction / huge number が reward/resource を壊さないapplicationIdを 2 回 claim しても 1 回だけ加算applicationIdなら 2 回加算calculate()は state を mutate しないRewardSystem.tsは DOM / Canvas / localStorage を import しないPlaywright は browser context ごとに isolation され、storage state には localStorage snapshot も含まれます。#740 の E2E では、この性質を使って HTTP origin 上の localStorage を実際に inspect するのが妥当です。(Playwright)
10. #737 本文への具体的な修正案
#737 は以下のように contract を修正してから実装すべきです。
最終レビューコメント案
squne121 commentedon Jun 8, 2026
Refinement Loop: Approved
The issue has been approved and is ready for implementation.
approvedsqune121 commentedon Jun 8, 2026
Contract Snapshot (2026-06-08T21:56:30Z)
SortieResultから reward を pure に算出し、同一applicationIdに対するprogress.resources反映を exactly-once で提供する。pr_review_only12 件。src/systems/RewardSystem.ts,src/state/GameState.ts,src/systems/index.ts,tests/reward-system.test.ts.claude/worktrees/issue-737-reward-systemworktree-issue-737-reward-system