fix(abilities): pause/resume ordering and continuation correctness

Issue 1 — ActionGroup AND continuation ordering:
  The old code prepended `all` (preceding AND-group actions) into
  ContinuationActionsJson, so resume ran [chosen, preceding, outer] instead
  of the correct [preceding, chosen, outer].  Fix: add PrecedingActionsJson
  to PendingActionGroupPick; AND-case stores preceding there (prepending to
  any inner-group preceding); ResumeActionGroupPick reads it and uses
  preceding.Concat(chosen).Concat(continuation) for final execution.
  Nested remaining-group pauses also accumulate preceding correctly.

Issue 2 — Effect-level pause terminal contract:
  Documented as a design constraint: pausing effects (CloneSpiritAsToken
  TargetTile, ChannelCardFromZone TargetTile/Choice) must be last in their
  action's Effects list.  Remaining effects / remaining targets are silently
  dropped on pause.  No functional change; constraint is now explicit in code.

Issue 3 — StackDeck pause loses continuation:
  PendingRevealChoicePick already had ContinuationActionsJson/RangeJson/
  ForceDurationJson fields but StackDeckEffect never populated them and
  ResumeRevealChoicePick never executed them.  Both are now fixed: pause
  stores ctx.ContinuationActions + ctx.ActivationRange + duration; resume
  runs ExecuteActions on the stored continuation after ordering the deck.

Issue 4 — ResumeTargetPick ActionIndex=0 contract:
  Added a clear CONTRACT comment: the paused action replays at index 0, so
  the client must submit targets with ActionIndex=0 in its response.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-21 07:43:22 -04:00
co-authored by Claude Happy
parent 0e85441fa1
commit ac0215fab3
2 changed files with 82 additions and 14 deletions
+74 -14
View File
@@ -920,15 +920,21 @@ public static class AbilitySystem
forceDuration, continuationActions);
if (childActions is null)
{
// Child emitted PendingActionGroupPick. Patch it to include:
// - `all` (already-flattened preceding actions) prepended to continuation
// - remaining sibling groups stored so ResumeActionGroupPick can process them
// Child emitted PendingActionGroupPick. Patch it:
// - Store preceding actions (all actions resolved so far in this AND group)
// so resume can execute them BEFORE the chosen branch.
// - Store remaining sibling groups.
// - Leave ContinuationActionsJson untouched (it already carries the outer
// continuation that was passed into this recursive call).
if (ctx.State.PendingActionGroupPick is { } pend)
{
var existingCont = JsonSerializer.Deserialize<Models.Action[]>(
pend.ContinuationActionsJson, _jsonOpts) ?? [];
pend.ContinuationActionsJson = JsonSerializer.Serialize(
all.Concat(existingCont).ToArray(), _jsonOpts);
// An inner AND group may have already set PrecedingActionsJson.
// Prepend our own `all` to whatever the inner group stored.
var innerPreceding = pend.PrecedingActionsJson is not null
? JsonSerializer.Deserialize<Models.Action[]>(pend.PrecedingActionsJson, _jsonOpts) ?? []
: [];
pend.PrecedingActionsJson = JsonSerializer.Serialize(
all.Concat(innerPreceding).ToArray(), _jsonOpts);
var remaining = ag.Groups.Skip(gi + 1).ToArray();
if (remaining.Length > 0)
@@ -1011,6 +1017,13 @@ public static class AbilitySystem
var ctx = new AbilityContext { State = state, ActorId = pending.ActorId, Source = source };
var continuation = JsonSerializer.Deserialize<Models.Action[]>(
pending.ContinuationActionsJson, _jsonOpts) ?? [];
// Actions that ran before the pause in a parent AND group.
// These must execute BEFORE the chosen branch to preserve AND-group order.
var preceding = pending.PrecedingActionsJson is not null
? JsonSerializer.Deserialize<Models.Action[]>(pending.PrecedingActionsJson, _jsonOpts) ?? []
: System.Array.Empty<Models.Action>();
AbilityRange? range = pending.RangeJson is not null
? JsonSerializer.Deserialize<AbilityRange>(pending.RangeJson, _jsonOpts) : null;
DurationModel? forceDuration = pending.ForceDurationJson is not null
@@ -1027,7 +1040,16 @@ public static class AbilitySystem
abilityIndex: 0, range, forceDuration, continuation);
if (chosen is null)
{
// Nested group triggered another pick — prepend any chosen-leaf prefix.
// Nested group triggered another pick.
// Accumulate our preceding into whatever the inner group set.
if (ctx.State.PendingActionGroupPick is { } innerPend && preceding.Length > 0)
{
var innerInner = innerPend.PrecedingActionsJson is not null
? JsonSerializer.Deserialize<Models.Action[]>(innerPend.PrecedingActionsJson, _jsonOpts) ?? []
: [];
innerPend.PrecedingActionsJson = JsonSerializer.Serialize(
preceding.Concat(innerInner).ToArray(), _jsonOpts);
}
return state;
}
}
@@ -1046,13 +1068,16 @@ public static class AbilitySystem
abilityIndex: 0, range, forceDuration, continuation);
if (rgActions is null)
{
// This remaining group paused — prepend already-resolved actions.
// This remaining group paused. Carry forward the full preceding context:
// preceding (from parent) + tempList (chosen branch + already-resolved groups)
if (ctx.State.PendingActionGroupPick is { } newPend)
{
var existingCont = JsonSerializer.Deserialize<Models.Action[]>(
newPend.ContinuationActionsJson, _jsonOpts) ?? [];
newPend.ContinuationActionsJson = JsonSerializer.Serialize(
tempList.Concat(existingCont).ToArray(), _jsonOpts);
var newInnerPreceding = newPend.PrecedingActionsJson is not null
? JsonSerializer.Deserialize<Models.Action[]>(newPend.PrecedingActionsJson, _jsonOpts) ?? []
: [];
newPend.PrecedingActionsJson = JsonSerializer.Serialize(
preceding.Concat(tempList).Concat(newInnerPreceding).ToArray(), _jsonOpts);
// ContinuationActionsJson is already set by the new pause.
var afterThis = remainingGroups.Skip(rgi + 1).ToArray();
newPend.RemainingGroupsJson = afterThis.Length > 0
? JsonSerializer.Serialize(afterThis, _jsonOpts) : null;
@@ -1064,7 +1089,8 @@ public static class AbilitySystem
chosen = tempList.ToArray();
}
var allActions = chosen.Concat(continuation).ToArray();
// Correct AND-group order: [preceding] + [chosen/remaining] + [outer continuation].
var allActions = preceding.Concat(chosen).Concat(continuation).ToArray();
ExecuteActions(allActions, ctx, clientTargets: null, requireClientTargets: false,
activationRange: range, forceDuration: forceDuration);
return state;
@@ -1123,6 +1149,24 @@ public static class AbilitySystem
ps.Deck.AddRange(top);
}
// Execute any continuation actions stored at pause time.
var rrcSource = state.Players.Values
.SelectMany(p => p.Field).OfType<FieldCard>()
.FirstOrDefault(f => f.InstanceId == pending.SourceInstanceId);
var rrcCtx = new AbilityContext { State = state, ActorId = pending.ActorId, Source = rrcSource };
var rrcContinuation = JsonSerializer.Deserialize<Models.Action[]>(
pending.ContinuationActionsJson, _jsonOpts) ?? [];
if (rrcContinuation.Length > 0)
{
AbilityRange? rrcRange = pending.RangeJson is not null
? JsonSerializer.Deserialize<AbilityRange>(pending.RangeJson, _jsonOpts) : null;
DurationModel? rrcForceDuration = pending.ForceDurationJson is not null
? JsonSerializer.Deserialize<DurationModel>(pending.ForceDurationJson, _jsonOpts) : null;
ExecuteActions(rrcContinuation, rrcCtx, clientTargets: null, activationRange: rrcRange,
forceDuration: rrcForceDuration);
}
return state;
}
@@ -1481,6 +1525,9 @@ public static class AbilitySystem
? JsonSerializer.Deserialize<DurationModel>(pending.ForceDurationJson, _jsonOpts) : null;
// Execute the paused action (now with client targets) then run the continuation.
// CONTRACT: The paused action is placed at index 0 in the replayed slice, so
// the client must submit targets using ActionIndex=0 regardless of the original
// action index in the ability definition. The prompt payload should reflect this.
var allActions = new[] { action }.Concat(continuation).ToArray();
ExecuteActions(allActions, ctx, clientTargets, requireClientTargets: true,
activationRange: range, forceDuration: forceDuration);
@@ -1639,6 +1686,12 @@ public static class AbilitySystem
}
// Expose continuation context so effect-level pauses can store it.
// DESIGN CONSTRAINT: A pausing effect (e.g. CloneSpiritAsToken with TargetTile,
// ChannelCardFromZone with TargetTile/Choice) must be the LAST effect in its
// action's Effects list. Any effects after a pausing one will be silently skipped
// because the outer loop returns on the first HasPendingInteraction() hit.
// Similarly, remaining targets in the same action are also dropped.
// This is an intentional trade-off: pausing effects are semantically terminal.
ctx.ContinuationActions = continuationActions;
ctx.ActivationRange = activationRange;
@@ -3173,6 +3226,13 @@ public static class AbilitySystem
RevealedCardsJson = JsonSerializer.Serialize(revealed, _jsonOpts),
Count = n,
TargetPlayerAccountId = stackPs.AccountId,
// Preserve the rest of the ability sequence so ResumeRevealChoicePick can
// continue execution after the chooser submits their ordering.
ContinuationActionsJson = JsonSerializer.Serialize(ctx.ContinuationActions, _jsonOpts),
RangeJson = ctx.ActivationRange is not null
? JsonSerializer.Serialize(ctx.ActivationRange, _jsonOpts) : null,
ForceDurationJson = duration is not null
? JsonSerializer.Serialize(duration, _jsonOpts) : null,
};
break;
}
@@ -38,6 +38,14 @@ public sealed class PendingActionGroupPick
/// </summary>
public string? RemainingGroupsJson { get; set; }
/// <summary>
/// JSON-serialized Action[]? — actions from the parent AND group that were already
/// flattened BEFORE the paused child group. These must execute BEFORE the chosen
/// branch, so resume computes: preceding + chosen + remaining-groups + continuation.
/// Null when there are no such preceding actions.
/// </summary>
public string? PrecedingActionsJson { get; set; }
/// <summary>JSON-serialized AbilityRange? — passed through to ExecuteActions on resume.</summary>
public string? RangeJson { get; set; }