Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions crates/engine/src/game/engine_resolution_choices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2988,12 +2988,25 @@ pub(super) fn handle_resolution_choice(
kept.len()
)));
}
} else if kept.len() != keep_count {
return Err(EngineError::InvalidAction(format!(
"Must select exactly {} cards, got {}",
keep_count,
kept.len()
)));
} else {
// CR 609.3 + CR 101.3: a dig whose filter (or a short library)
// leaves fewer selectable cards than `keep_count` must keep as
// many as possible, not reject every selection. Without the
// clamp no legal action exists in that state —
// `validate_dig_selection` below requires every kept id to be in
// `selectable_cards` while this gate demands more ids than it
// holds — softlocking every controller. Matches the clamp the
// candidate enumerator (`ai_support/candidates.rs:1185`) and
// `cheap_reject_candidate` (`ai_support/mod.rs:702`) already
// apply.
let required = keep_count.min(selectable_cards.len());
Comment on lines +2991 to +3002

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Correct the duplicated CR 101.3 citation.

CR 101.3 defines “card.” It does not support impossible-instruction handling. CR 609.3 supports the “do as much as possible” behavior. (media.wizards.com)

  • crates/engine/src/game/engine_resolution_choices.rs#L2991-L3002: remove or replace the CR 101.3 citation in the DigChoice validation comment.
  • crates/engine/tests/integration/dig_impossible_keep_count.rs#L5-L7: remove or replace the matching CR 101.3 citation in the test documentation.

As per path instructions, flag a CR citation whose rule body does not describe the code.

📍 Affects 2 files
  • crates/engine/src/game/engine_resolution_choices.rs#L2991-L3002 (this comment)
  • crates/engine/tests/integration/dig_impossible_keep_count.rs#L5-L7
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/engine/src/game/engine_resolution_choices.rs` around lines 2991 -
3002, Correct the CR citation in the DigChoice validation comment so it
references CR 609.3 only (or another rule that directly supports the behavior),
removing the inaccurate CR 101.3 reference. Apply the same citation correction
in crates/engine/tests/integration/dig_impossible_keep_count.rs lines 5-7; both
sites document the “do as much as possible” handling.

Source: Path instructions

if kept.len() != required {
return Err(EngineError::InvalidAction(format!(
"Must select exactly {} cards, got {}",
required,
kept.len()
)));
}
}

// CR 401.2 + CR 608.2c: the keep-selection must be unique, drawn from
Expand Down
113 changes: 113 additions & 0 deletions crates/engine/tests/integration/dig_impossible_keep_count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//! Issue #6942: a `DigChoice` whose filter (or a short library) leaves fewer
//! selectable cards than `keep_count` must accept the largest possible
//! selection, not reject every selection.
//!
//! CR 609.3 ("If an effect attempts to do something impossible, it does only as
//! much as possible") and CR 101.3 ("Any part of an instruction that's
//! impossible to perform is ignored"). Before the fix, the exact-cardinality
//! gate in `engine_resolution_choices.rs` demanded `keep_count` ids while
//! `validate_dig_selection` required every kept id to be in `selectable_cards`
//! — so when `selectable_cards.len() < keep_count` the two rules had NO common
//! solution and every controller (AI, human, multiplayer server) softlocked.
//!
//! The candidate enumerator (`ai_support/candidates.rs`) and
//! `cheap_reject_candidate` (`ai_support/mod.rs`) already clamped to
//! `keep_count.min(selectable_cards.len())`; the resolution handler was the
//! outlier.
use engine::game::scenario::GameScenario;
use engine::types::actions::GameAction;
use engine::types::game_state::WaitingFor;
use engine::types::identifiers::ObjectId;
use engine::types::phase::Phase;
use engine::types::zones::Zone;
use engine::types::PlayerId;

const P0: PlayerId = PlayerId(0);

/// A filtered dig that looked at three cards but whose filter matched only one,
/// with `keep_count: 2` and `up_to: false`.
///
/// This is the shape `effects/dig.rs` produces: `selectable_cards` is pruned by
/// the effect's filter while `keep_count` stays at the card-literal value.
fn filtered_dig_runner() -> (engine::game::scenario::GameRunner, Vec<ObjectId>) {
let mut scenario = GameScenario::new();
scenario.at_phase(Phase::PreCombatMain);
let looked_at: Vec<ObjectId> = ["Dug One", "Dug Two", "Dug Three"]
.iter()
.map(|name| scenario.add_spell_to_library_top(P0, name, false).id())
.collect();
let mut runner = scenario.build();
runner.state_mut().waiting_for = WaitingFor::DigChoice {
player: P0,
library_owner: P0,
cards: looked_at.clone(),
keep_count: 2,
up_to: false,
// The filter matched exactly one of the three looked-at cards.
selectable_cards: vec![looked_at[0]],
kept_destination: Some(Zone::Hand),
rest_destination: Some(Zone::Graveyard),
source_id: None,
enter_tapped: false,
};
(runner, looked_at)
}

/// PAIRED NEGATIVE, run first because it must leave the prompt intact: the
/// clamp relaxes the *cardinality* gate only. A selection of the clamped size
/// whose id is outside `selectable_cards` is still rejected by
/// `validate_dig_selection`, so the filter check was not disabled.
#[test]
fn dig_clamp_does_not_disable_the_filter_check() {
let (mut runner, looked_at) = filtered_dig_runner();

let err = runner
.act(GameAction::SelectCards {
cards: vec![looked_at[1]],
})
.expect_err("a non-matching id must still be refused");
assert!(
format!("{err:?}").contains("does not match the effect's filter"),
"the refusal must come from validate_dig_selection, not the cardinality \
gate — got {err:?}"
);
assert!(
matches!(runner.state().waiting_for, WaitingFor::DigChoice { .. }),
"a refused selection must leave the prompt pending"
);
}

/// MAIN TEST. FAILS BEFORE THE FIX: `kept.len() != keep_count` evaluates
/// `1 != 2` and returns `InvalidAction("Must select exactly 2 cards, got 1")`,
/// while every larger selection is refused by `validate_dig_selection` — no
/// legal action exists.
#[test]
fn dig_with_fewer_selectable_cards_than_keep_count_keeps_as_many_as_possible() {
let (mut runner, looked_at) = filtered_dig_runner();
let (kept, unkept) = (looked_at[0], [looked_at[1], looked_at[2]]);

runner
.act(GameAction::SelectCards { cards: vec![kept] })
.expect(
"CR 609.3: the only selection the filter permits must be accepted \
when keep_count exceeds the selectable set",
);

let hand = &runner.state().players[P0.0 as usize].hand;
assert!(
hand.contains(&kept),
"the single filter-matching card must reach kept_destination (hand)"
);
let graveyard = &runner.state().players[P0.0 as usize].graveyard;
for id in unkept {
assert!(
graveyard.contains(&id),
"the unkept cards must reach rest_destination (graveyard); \
graveyard = {graveyard:?}"
);
}
assert!(
!matches!(runner.state().waiting_for, WaitingFor::DigChoice { .. }),
"the dig prompt must be resolved, not re-parked"
);
}
1 change: 1 addition & 0 deletions crates/engine/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ mod destroy_redirect_to_battlefield_delivery_tail;
mod deterministic_blocker_prompt_order;
mod devour_co_entry_regression;
mod devour_intellect_treasure_rider;
mod dig_impossible_keep_count;
mod dig_rest_pile_stranding_on_etb_pause;
mod diligent_farmhand_counts_as_named;
mod diluvian_primordial_6754;
Expand Down
Loading
Loading