Skip to content

実装: SortieResult に endReason フィールドを追加し敗北・勝利理由を区別可能にする #680

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "SortieResult に endReason フィールドを追加し、敗北・勝利の理由を M3 persistence / reward 設計前に区別可能にする"
change_kind: code

Parent Issue

なし(M3 着手前の単独改善)

Parent Goal Ref

  • Goal: なし(単独改善)
  • Desired Destination: なし

Current Validated Scope

  • docs/product/features/sortie.md の SortieResult shape / invariant 定義を endReason を含む discriminated union に更新する
  • src/state/GameState.ts に SortieEndReason 型と SortieResult discriminated 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 の SortieResult shape / invariant 更新(discriminated union、endReason 3 値)
  • src/state/GameState.ts に SortieEndReason 型と SortieResult discriminated 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 の SortieResult shape 定義に 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 > 0 guard が維持されており、敵未スポーン状態で 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 build

Runtime Verification Applicability

decision: not_applicable
runtime_verification_applicability: not_applicable
reason: "unit test / typecheck / build のみ。ブラウザ実行不要。"

Allowed Paths

  • docs/product/features/sortie.md
  • src/state/GameState.ts
  • src/systems/SortieSystem.ts
  • tests/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 の SortieResult shape 定義を endReason discriminated union 反映で更新する必要があるため)

Product Spec Context

  • affected_sections: SortieResult 型形状、SortieEndReason 型定義、SortieResult 不変条件
  • diff_rationale: endReason discriminated union 追加により SortieResult の型形状と invariant が変わるため、SSOT である sortie.md を実装と同一 PR で更新する

Activity

  1. added
    enhancement
    phase/implementation
    on Jun 5, 2026
  2. squne121 commented on Jun 6, 2026

    @squne121
    OwnerAuthor

    結論

    #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の読み取り

    関連の本線は以下です。

    したがって #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 を処理済みと誤認します。

    推奨修正:

    export type SortieEndReason =
      | 'all_enemies_defeated'
      | 'timeout'
      | 'player_hp_zero'

    survival_timer は、実際に survival victory を復活させるIssueで追加する。TypeScript の union は後から値を追加すると exhaustive switch で検出しやすいため、将来値を今予約するより、後続PRで明示的に破壊的変更として扱う方が安全です。TypeScript公式も discriminated union と never による exhaustiveness checking を状態・メッセージ表現に有効と説明しています。([TypeScript]7)

    2. outcome と endReason の不正な組み合わせを型で防げない

    #680 の案は次のような shape です。

    interface SortieResult {
      readonly outcome: 'victory' | 'defeat'
      readonly endReason: SortieEndReason
    }

    これだと型上は以下が通ります。

    { outcome: 'victory', endReason: 'timeout' }
    { outcome: 'defeat', endReason: 'all_enemies_defeated' }

    これは reward / persistence 前提のフィールドとしては弱いです。endReason は outcome の補足ではなく、終端遷移の根拠なので、型で不整合を潰すべきです。

    推奨:

    export type SortieResult =
      | Readonly<{
          outcome: 'victory'
          endReason: 'all_enemies_defeated'
          durationMs: number
          kills: number
          shotsFired: number
          playerHpRemaining: number
        }>
      | Readonly<{
          outcome: 'defeat'
          endReason: 'player_hp_zero' | 'timeout'
          durationMs: number
          kills: number
          shotsFired: number
          playerHpRemaining: number
        }>

    将来 survival victory を戻すなら、その時点で:

    | Readonly<{
        outcome: 'victory'
        endReason: 'survival_timer'
        ...
      }>

    を追加すればよいです。

    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)
    これは不足です。

    修正案:

    Allowed Paths:
    - docs/product/features/sortie.md
    - src/state/GameState.ts
    - src/systems/SortieSystem.ts
    - tests/sortie-system.test.ts

    必要なら docs/playtest/m2-combat-mvp.md は Out of Scope のままでよいです。UI/E2Eが endReason を表示しないなら、そこまで広げる必要はありません。

    Non-blocker / 改善点

    4. ACが grep 寄りで、誤実装を通す

    現ACは rg "player_hp_zero" のような存在確認が中心です。これでは、文字列が存在するだけで mapping が間違っていても通ります。

    追加すべきテスト:

    it('HP defeat sets endReason=player_hp_zero', ...)
    it('timeout defeat sets endReason=timeout', ...)
    it('all enemies defeated sets endReason=all_enemies_defeated', ...)
    it('same tick HP zero + all enemies defeated + timeout chooses player_hp_zero', ...)
    it('same tick all enemies defeated + timeout chooses all_enemies_defeated', ...)
    it('double-result preserves same result object including endReason', ...)

    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案

    ## Title
    実装: SortieResult に endReason を追加し HP敗北・timeout敗北・敵全滅勝利を区別可能にする
    
    ## Outcome
    SortieResult が outcome と endReason の整合を型・実装・unit test で保証する。
    M2 runtime で発生する endReason は以下の3種類のみ:
    - all_enemies_defeated: victory
    - player_hp_zero: defeat
    - timeout: defeat
    
    survival_timer は M2 では追加しない。将来 survival victory を再導入するIssueで追加する。
    
    ## In Scope
    - docs/product/features/sortie.md の SortieResult shape / invariant 更新
    - src/state/GameState.ts に SortieEndReason / SortieResult union を追加
    - src/systems/SortieSystem.ts の result 生成で endReason を設定
    - tests/sortie-system.test.ts に endReason と priority の検証を追加
    
    ## Out of Scope
    - SortieResult 永続化
    - reward / debrief logic
    - UI 表示
    - E2E 更新
    - survival_timer の追加・実装
    
    ## Acceptance Criteria
    - AC1: docs/product/features/sortie.md の SortieResult shape に endReason がある
    - AC2: SortieResult は outcome と endReason の不正な組み合わせを型で表現しない
    - AC3: HP0 敗北時 result は outcome=defeat, endReason=player_hp_zero
    - AC4: timeout 敗北時 result は outcome=defeat, endReason=timeout
    - AC5: 敵全滅勝利時 result は outcome=victory, endReason=all_enemies_defeated
    - AC6: 同一tickで HP0 + 敵全滅 + timeout が成立した場合、endReason=player_hp_zero
    - AC7: 同一tickで 敵全滅 + timeout が成立し HP>0 の場合、endReason=all_enemies_defeated
    - AC8: empty enemies の vacuous truth を防ぐ length > 0 guard が維持されている
    - AC9: double-result テストで result object が再生成されず、endReason も保持される
    - AC10: pnpm typecheck / pnpm lint / pnpm test / pnpm build が PASS

    実装上の最小差分イメージ

    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 & {
          outcome: 'victory'
          endReason: 'all_enemies_defeated'
        })
      | (SortieResultBase & {
          outcome: 'defeat'
          endReason: 'player_hp_zero' | 'timeout'
        })

    runSortieSystem 側は現在の優先順位をそのまま使い、次のように決めればよいです。

    const outcome: 'victory' | 'defeat' = isDefeat
      ? 'defeat'
      : isVictory
        ? 'victory'
        : 'defeat'
    
    const endReason: SortieEndReason = isDefeat
      ? 'player_hp_zero'
      : isVictory
        ? 'all_enemies_defeated'
        : 'timeout'

    推奨判定

    REQUEST CHANGES。
    #680 は必要な改善ですが、現状のIssue契約は「M2で到達不能な survival_timer を混入」「SSOT更新漏れ」「型で invariant を守れない」という3点で弱いです。実装前に Issue 本文を上記方針へ直した方が、M3 persistence / reward の土台として安全です。

  3. squne121 commented on Jun 6, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Human Escalation Required

    The loop has terminated and requires human review. Cause: none

  4. squne121 commented on Jun 6, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Approved

    The issue has been approved and is ready for implementation.

  5. squne121 commented on Jun 6, 2026

    @squne121
    OwnerAuthor

    コントラクトレビュー: 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 を記述してください。例:

    affected_sections: SortieResult shape, SortieEndReason invariants
    diff_rationale: endReason discriminated union 追加により SortieResult の型形状と invariant が変わるため sortie.md を同時更新する
    

    修正後に issue-contract-review を再実行してください。

  6. squne121 commented on Jun 6, 2026

    @squne121
    OwnerAuthor

    コントラクトレビュー: BLOCKED(VC preflight)

    baseline_vc_preflight.py の実行結果で AC9 が unexpected_pass として検出されました。

    失敗した VC

    AC コマンド 分類 判定
    AC9 grep -n "enemies\.length|length > 0" src/systems/SortieSystem.ts unexpected_pass blocked

    原因

    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 を機械可読に明示するだけである。

  7. squne121 commented on Jun 6, 2026

    @squne121
    OwnerAuthor

    Contract Snapshot(2026-06-06T11:56:28Z)

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-06T11:56:28Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/680
      body_sha256: "sha256:84dbf78f716ab3b51e6d1ec8bf311549e7edbf75da794730ecc63135a4d89cda"
      checks:
        template_compliance: pass
        state_label: ok
        allowed_paths_present: pass
        vc_present: pass
        stop_conditions_complete: pass
        vc_preflight:
          passed: true
          vc_failed_as_expected: 9
          vc_passed_unexpectedly: 0
          vc_unrunnable: 0
          vc_expected_pass: 4
          vc_skipped: 1
          classifications:
            - ac: AC1
              command: "rg -n \"endReason\" docs/product/features/sortie.md"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC2
              command: "rg -n \"endReason: 'all_enemies_defeated'\" src/state/GameState.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC3
              command: "rg -n \"SortieEndReason\" src/state/GameState.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC4
              command: "grep -n \"endReason.*player_hp_zero|player_hp_zero.*endReason\" tests/sortie-system.test.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC5
              command: "grep -n \"endReason.*timeout|timeout.*endReason\" tests/sortie-system.test.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC6
              command: "grep -n \"endReason.*all_enemies_defeated|all_enemies_defeated.*endReason\" tests/sortie-system.test.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC7
              command: "grep -n \"endReason.*player_hp_zero\" tests/sortie-system.test.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC8
              command: "grep -n \"endReason.*all_enemies_defeated\" tests/sortie-system.test.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC9
              command: "grep -n \"enemies\\.length|length > 0\" src/systems/SortieSystem.ts"
              exit_code: 0
              category: regression_gate
              scope_class: pr_review_only
              confidence: high
              decision: go
              classification: skipped
              verification_owner: pr-review-judge
              deferred_reason: "# preflight-scope: pr_review_only が指定されている"
            - ac: AC10
              command: "grep -n \"result\\.endReason|endReason.*result\" tests/sortie-system.test.ts"
              exit_code: 1
              category: expected_baseline_fail
              scope_class: baseline_fail_expected
              confidence: high
              decision: go
            - ac: AC11
              command: "pnpm typecheck"
              exit_code: 0
              category: regression_gate
              scope_class: regression_gate
              confidence: low
              decision: go
            - ac: AC11
              command: "pnpm lint"
              exit_code: 0
              category: regression_gate
              scope_class: regression_gate
              confidence: low
              decision: go
            - ac: AC11
              command: "pnpm test"
              exit_code: 0
              category: regression_gate
              scope_class: regression_gate
              confidence: low
              decision: go
            - ac: AC11
              command: "pnpm build"
              exit_code: 0
              category: regression_gate
              scope_class: regression_gate
              confidence: low
              decision: go
        ac_verifiability: pass
        product_spec_check:
          applicability: applicable
          decision: pass
        worktree_branch_collision: clear
      next_action: implement_issue
      blocked_reasons: []
      warnings:
        - "prose_boundary_policy.py に Product Spec Context 見出しを追加(tooling fix として本 PR と同時にコミット対象)"

    実装サマリー

    • Outcome: SortieResult が outcome と endReason の整合を型・実装・unit test で保証する
    • Allowed Paths: docs/product/features/sortie.md, src/state/GameState.ts, src/systems/SortieSystem.ts, tests/sortie-system.test.ts
    • Worktree: .claude/worktrees/issue-680-sortieresult-endreason
    • Branch: worktree-issue-680-sortieresult-endreason
    • VC preflight: baseline fail = 9, 想定外 pass = 0, 実行不可 = 0, skipped = 1 (AC9: pr_review_only)
  8. squne121 commented on Jun 6, 2026

    @squne121
    OwnerAuthor

    implement-issue: 実装完了 (2026-06-06T12:05:33Z)

  9. added 2 commits that reference this issue on Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementphase/implementation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions