Repository navigation
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "SortieResult に endReason フィールドを追加し、敗北・勝利の理由を M3 persistence / reward 設計前に区別可能にする"
change_kind: codeParent Issue
なし(M3 着手前の単独改善)
Parent Goal Ref
- Goal: なし(単独改善)
- Desired Destination: なし
Current Validated Scope
docs/product/features/sortie.mdのSortieResultshape / invariant 定義をendReasonを含む discriminated union に更新するsrc/state/GameState.tsにSortieEndReason型とSortieResultdiscriminated union を追加するsrc/systems/SortieSystem.tsでendReasonを正しく設定するtests/sortie-system.test.tsのテストをendReasonと priority の検証に対応させる
Remaining Parent Gaps
なし
Outcome
SortieResult が outcome と endReason の整合を型・実装・unit test で保証する。M2 runtime で発生する endReason は以下の 3 種類のみ:
all_enemies_defeated: 勝利(敵全滅)player_hp_zero: 敗北(HP ゼロ)timeout: 敗北(時間切れ)
survival_timer は M2 では追加しない。将来 survival victory を再導入する Issue で追加する。pnpm typecheck / pnpm test が PASS している。
Background
現行の SortieResult は outcome: 'victory' | 'defeat' のみを持ち、敗北理由・勝利理由が区別できない。M3 の persistence / reward 設計で「HP 敗北」と「timeout 敗北」を区別したい場合、後から型を変更するとデータ移行が発生する。M3 着手前に追加しておく必要がある。
また、flat interface では { outcome: 'victory', endReason: 'timeout' } のような不正な組み合わせを型で防げない。discriminated union を使うことで、outcome と endReason の整合を型レベルで保証する。
survival_timer は #542 で 120 秒生存勝利が MVP から除外されたため、M2 では到達不能な値となっている。到達不能な値を public contract に混入すると、M3 consumer が誤認する可能性がある。TypeScript の discriminated union は後から値を追加すると exhaustive switch で検出しやすいため、将来値を今予約するより後続 PR で明示的に破壊的変更として扱う方が安全である。
追加する型定義:
export type SortieEndReason =
| 'all_enemies_defeated'
| 'player_hp_zero'
| 'timeout'
type SortieResultBase = Readonly<{
durationMs: number
kills: number
shotsFired: number
playerHpRemaining: number
}>
export type SortieResult =
| (SortieResultBase & {
readonly outcome: 'victory'
readonly endReason: 'all_enemies_defeated'
})
| (SortieResultBase & {
readonly outcome: 'defeat'
readonly endReason: 'player_hp_zero' | 'timeout'
})In Scope
docs/product/features/sortie.mdのSortieResultshape / invariant 更新(discriminated union、endReason 3 値)src/state/GameState.tsにSortieEndReason型とSortieResultdiscriminated union を追加するsrc/systems/SortieSystem.tsの result 生成部分にendReasonを設定する(HP 敗北・timeout 敗北・敵全滅勝利の 3 種類)tests/sortie-system.test.tsにendReasonと priority の検証を追加する
Out of Scope
SortieResultの永続化endReasonを使用した reward / debrief ロジック- UI 表示への
endReason連携 survival_timerの追加・実装- E2E テストの変更
Acceptance Criteria
- AC1:
docs/product/features/sortie.mdのSortieResultshape 定義にendReasonが存在し、discriminated union として記述されている - AC2:
SortieResultは discriminated union として定義され、outcome: 'victory'はendReason: 'all_enemies_defeated'のみ、outcome: 'defeat'はendReason: 'player_hp_zero' | 'timeout'のみを型で許可する - AC3:
SortieEndReasonは'all_enemies_defeated' | 'player_hp_zero' | 'timeout'の 3 値のみを持ち、survival_timerを含まない - AC4: HP ゼロ敗北時の result は
outcome: 'defeat'かつendReason: 'player_hp_zero'である(テストで検証) - AC5: timeout 敗北時の result は
outcome: 'defeat'かつendReason: 'timeout'である(テストで検証) - AC6: 敵全滅勝利時の result は
outcome: 'victory'かつendReason: 'all_enemies_defeated'である(テストで検証) - AC7: 同一 tick で HP ゼロ + 敵全滅 + timeout が成立した場合、
endReason: 'player_hp_zero'(優先順位: HP0 > 敵全滅 > timeout、テストで検証) - AC8: 同一 tick で 敵全滅 + timeout が成立し HP > 0 の場合、
endReason: 'all_enemies_defeated'(テストで検証) - AC9:
state.enemies.length > 0guard が維持されており、敵未スポーン状態で victory にならない(テストで検証) - AC10: double-result テストで result object が再生成されず、
endReasonも保持される - AC11:
pnpm typecheck/pnpm lint/pnpm test/pnpm buildが PASS する
Verification Commands
# AC1
$ rg -n "endReason" docs/product/features/sortie.md
# AC2
$ rg -n "endReason: 'all_enemies_defeated'" src/state/GameState.ts
# AC3
$ rg -n "SortieEndReason" src/state/GameState.ts
# vc-baseline: must fail (SortieEndReason not yet defined)
# vc-baseline-check: exit_nonzero_ok
# AC4
$ grep -n "endReason.*player_hp_zero\|player_hp_zero.*endReason" tests/sortie-system.test.ts
# AC5
$ grep -n "endReason.*timeout\|timeout.*endReason" tests/sortie-system.test.ts
# AC6
$ grep -n "endReason.*all_enemies_defeated\|all_enemies_defeated.*endReason" tests/sortie-system.test.ts
# AC7
$ grep -n "endReason.*player_hp_zero" tests/sortie-system.test.ts
# AC8
$ grep -n "endReason.*all_enemies_defeated" tests/sortie-system.test.ts
# AC9
# preflight-scope: pr_review_only
$ grep -n "enemies\.length\|length > 0" src/systems/SortieSystem.ts
# AC10
$ grep -n "result[!]*\.endReason\|endReason.*result" tests/sortie-system.test.ts
# AC11
$ pnpm typecheck
$ pnpm lint
$ pnpm test
$ pnpm buildRuntime Verification Applicability
decision: not_applicable
runtime_verification_applicability: not_applicable
reason: "unit test / typecheck / build のみ。ブラウザ実行不要。"Allowed Paths
docs/product/features/sortie.mdsrc/state/GameState.tssrc/systems/SortieSystem.tstests/sortie-system.test.ts
Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
- In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
- 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
- 後続 Phase / 別スコープへの波及が判明した場合
- nested SubAgent delegation が必要になった場合
- 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
SortieResultの consumer(src/main.ts・tests/e2e/・他)での型エラーが多発し Allowed Paths 拡張が必要になった場合
Required Skills
- TypeScript(型定義・discriminated union)
- Vitest(ユニットテスト)
Scope Delta(任意)
docs/product/features/sortie.mdを Allowed Paths に追加(理由: SSOT のSortieResultshape 定義をendReasondiscriminated union 反映で更新する必要があるため)
Product Spec Context
- affected_sections: SortieResult 型形状、SortieEndReason 型定義、SortieResult 不変条件
- diff_rationale: endReason discriminated union 追加により SortieResult の型形状と invariant が変わるため、SSOT である sortie.md を実装と同一 PR で更新する
Activity
squne121 commentedon Jun 6, 2026
結論
#680 は方向性は妥当ですが、このまま実装Issueとして流すのは危険です。特に
survival_timerを予約値として今入れる点、SSOTであるdocs/product/features/sortie.mdを更新しない点、outcomeとendReasonの不整合を型で防げない点が弱いです。現状、#680 は Open で関連PRなしです。Issue上も Development は “No branches or pull requests” です。([GitHub]1)
現在の
SortieResultはoutcome,durationMs,kills,shotsFired,playerHpRemainingのみで、endReasonは未実装です。([GitHub]2)関連Issue / PRの読み取り
関連の本線は以下です。
docs/product/features/sortie.mdに作成。SortieResult shape と不変条件を定義。([GitHub]3)SortieSystemとSortieResultを実装。当初は 120秒 victory 系の設計。([GitHub]4)SORTIE_DURATION_MS = 30_000、優先順位はdefeat > victory > timeout。([GitHub]5)sortie.mdも、敵全滅 victory、HP0 defeat、30秒 timeout defeat、優先順位player.hp <= 0→allEnemiesDefeated→elapsedTicks >= targetTicksを定義しています。([GitHub]6)したがって #680 は、#542/#552 後の設計を受けて「
outcome: defeatの中に HP0 と timeout が混在する問題」を解くIssueとしては妥当です。ただし「生存勝利 / survival_timer」を今のM2型に混ぜるのは、現行仕様との整合が悪いです。Blocker
1.
survival_timerを今入れると、到達不能な値が public contract になる#680 は
SortieEndReasonにsurvival_timerを予約値として入れる案ですが、#542 で 120秒生存勝利は MVP から明確に外され、30秒到達は defeat 扱いに変更されています。([GitHub]5)現行の
sortie.mdでも timeout は defeat 条件であり、生存勝利は実行時条件として存在しません。([GitHub]6)これは小さく見えて、M3 persistence / reward 設計では致命的です。永続化前に型を固める目的なら、到達不能な値を先に永続スキーマ候補へ混ぜるべきではないです。後続 consumer が
survival_timerを処理済みと誤認します。推奨修正:
survival_timerは、実際に survival victory を復活させるIssueで追加する。TypeScript の union は後から値を追加すると exhaustive switch で検出しやすいため、将来値を今予約するより、後続PRで明示的に破壊的変更として扱う方が安全です。TypeScript公式も discriminated union とneverによる exhaustiveness checking を状態・メッセージ表現に有効と説明しています。([TypeScript]7)2.
outcomeとendReasonの不正な組み合わせを型で防げない#680 の案は次のような shape です。
これだと型上は以下が通ります。
これは reward / persistence 前提のフィールドとしては弱いです。
endReasonはoutcomeの補足ではなく、終端遷移の根拠なので、型で不整合を潰すべきです。推奨:
将来 survival victory を戻すなら、その時点で:
を追加すればよいです。
3.
docs/product/features/sortie.mdが Allowed Paths に入っていない#680 は
SortieResultの shape を変えるIssueですが、現行SSOTのsortie.mdはまだendReasonなしの shape を正として定義しています。([GitHub]6)このまま
src/state/GameState.tsだけを変えると、コードがSSOTを先行して破る状態になります。#680 の Allowed Paths には
src/state/GameState.ts,src/systems/SortieSystem.ts,tests/sortie-system.test.tsしかありません。([GitHub]1)これは不足です。
修正案:
必要なら
docs/playtest/m2-combat-mvp.mdは Out of Scope のままでよいです。UI/E2EがendReasonを表示しないなら、そこまで広げる必要はありません。Non-blocker / 改善点
4. ACが grep 寄りで、誤実装を通す
現ACは
rg "player_hp_zero"のような存在確認が中心です。これでは、文字列が存在するだけで mapping が間違っていても通ります。追加すべきテスト:
Vitest の
toBeはオブジェクト同一参照の検証に使えるため、既存の double-result テストと相性が良いです。(Vitest)5.
Object.freezeとreadonlyの役割を混同しない現実装は
Object.freeze({...} satisfies SortieResult)を使っています。SortieResultは全フィールドが primitive なので、この用途では十分です。ただし MDN の説明通りObject.freezeは shallow freeze です。将来rewardBreakdownのようなネストを入れるなら freeze の効き方が変わります。([MDNウェブドキュメント]9)また TypeScript の
readonlyは実行時挙動を変えず、type-checking 上の書き込み禁止です。([TypeScript]10)6.
every()の空配列問題は維持必須現在の実装と spec は
state.enemies.length > 0 && state.enemies.every(...)を使っており、これは正しいです。Array.prototype.every()は空配列で true を返すため、length > 0ガードを外すと「敵未スポーンで victory」が発生します。([MDNウェブドキュメント]11)#680 のテスト更新時にこのガードを壊さないことをACに残すべきです。
#680 の修正版Issue案
実装上の最小差分イメージ
runSortieSystem側は現在の優先順位をそのまま使い、次のように決めればよいです。推奨判定
REQUEST CHANGES。
#680 は必要な改善ですが、現状のIssue契約は「M2で到達不能な
survival_timerを混入」「SSOT更新漏れ」「型で invariant を守れない」という3点で弱いです。実装前に Issue 本文を上記方針へ直した方が、M3 persistence / reward の土台として安全です。squne121 commentedon Jun 6, 2026
Refinement Loop: Human Escalation Required
The loop has terminated and requires human review. Cause: none
human_escalationsqune121 commentedon Jun 6, 2026
Refinement Loop: Approved
The issue has been approved and is ready for implementation.
approvedsqune121 commentedon Jun 6, 2026
コントラクトレビュー: BLOCKED
issue-contract-reviewの Product Spec Preflight でブロックされました。失敗したルール
PS001:
docs/product/features/sortie.mdが Allowed Paths に含まれているが、spec delta Issue のリンクが存在しない(本 Issue はphase/implementationラベル)。PS004:
docs/product/**更新があるが、Issue 本文にdiff_rationale・changed_requirement_id・affected_sectionsのいずれも存在しない。解消方法(最小修正)
Issue 本文に
## Product Spec Contextセクションを追加し、affected_sectionsとdiff_rationaleを記述してください。例:修正後に
issue-contract-reviewを再実行してください。squne121 commentedon Jun 6, 2026
コントラクトレビュー: BLOCKED(VC preflight)
baseline_vc_preflight.pyの実行結果で AC9 がunexpected_passとして検出されました。失敗した VC
grep -n "enemies\.length|length > 0" src/systems/SortieSystem.ts原因
AC9 のコメントには
# trivially_pass:があり「実装前後ともに pass が expected」と記述されているが、preflight スクリプトはこのアノテーションを認識しない。grepが baseline で exit_code=0 を返したためunexpected_pass/blockedと判定される。修正方針
simple_contract_hygiene_fix(VC 意味論の変更なし)として以下の修正を適用する:# trivially_pass: ...を# preflight-scope: pr_review_onlyに置換するこの変更は AC の受入意味を変えず、scope class を機械可読に明示するだけである。
squne121 commentedon Jun 6, 2026
Contract Snapshot(2026-06-06T11:56:28Z)
実装サマリー
SortieResultがoutcomeとendReasonの整合を型・実装・unit test で保証するdocs/product/features/sortie.md,src/state/GameState.ts,src/systems/SortieSystem.ts,tests/sortie-system.test.ts.claude/worktrees/issue-680-sortieresult-endreasonworktree-issue-680-sortieresult-endreasonsqune121 commentedon Jun 6, 2026
implement-issue: 実装完了 (2026-06-06T12:05:33Z)
.claude/worktrees/issue-680-sortieresult-endreasonworktree-issue-680-sortieresult-endreasontest(#680): discriminated union 型退行テスト fixture を追加する
fix(#680): ESLint no-unused-vars を修正 — fixture を void 式に変換
feat: SortieResult に endReason discriminated union を追加する (#680) (#709)