AutoId review fixes: tag before send, pack waits out combat, sweep retries

- The flight tag is registered BEFORE _requestId again, so a 0xC8 that AC puts on the
  wire inside the call is still tagged. EngineSend returns a token; a call that returns
  false or throws hands it to EngineSendFailed, which retires the entry (a no-op once it
  was matched, or when the slot now holds another id). FlightRecorderTests: a synchronous
  send is tagged, a failed send leaves no live entry, a retire for another id or a bad
  token is a no-op.
- Inventory sends wait (WaitCombat, kept, counted once) while a target is selected, a
  cast is in flight (CastGate.GetCastBusyState) or an attack runs
  (ClientActionGates.TryReadAttacking). Each 0xC8 raises AC's busy count, which plugins
  gate casts on. The selected target and an open container are not held; All mode is
  unchanged. combat= in the minute line, combat in /rc autoid status.
- The login sweep looks into containers only (ItemsCapacity > 0 in the PWD snapshot, the
  engine's own pack rule), skips a tick while the pending list is full, and is marked
  done only when nothing overflowed; otherwise it runs again once there is room.
- CollectOwnedIdsFromSnapshot has its doc comment back; HasPwdSnapshot and the new
  IsContainerInSnapshot have their own. LiveWorld's send is renamed QueueIdentify and
  block-bodied, so check_offthread_mutators.py scans it and its allow-list entry no
  longer also covers the create-path Enqueue.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-12 16:29:54 -07:00
commit 8668f2a3ca
11 changed files with 224 additions and 50 deletions

View file

@ -18,7 +18,13 @@
// login-complete + DeferMs and with the snapshot warm, every uncached item the player
// owns, and every item in a pack the player owns, becomes a candidate. The 5 a second
// Inventory cap still applies. An Unknown candidate also waits for the snapshot (up to
// AutoIdPolicy.UnknownMaxWaitMs) instead of being dropped at MinAgeMs.
// AutoIdPolicy.UnknownMaxWaitMs) instead of being dropped at MinAgeMs. Only containers are
// looked into, and a sweep that overflowed the pending list counts as not done: it runs
// again once there is room.
//
// Combat: Inventory sends wait while a target is selected or a cast or attack
// is in flight (Context.CombatBusy). Each 0xC8 raises AC's busy count, and plugins gate
// their casts on it. The selected target and an open container are not held.
//
// Bounds: _incoming is capped at MaxIncoming (oldest dropped, counted) because the drain
// returns early through a world transition (a long death); the pending list is capped at
@ -49,7 +55,9 @@ internal interface IAutoIdWorld
/// <summary>Its appraisal is cached and recent.</summary>
bool IsFresh(uint id);
/// <summary>Queue the 0xC8 for AC's main thread; false = refused (transition, ring full).</summary>
bool Enqueue(uint id, IdRequester who);
bool QueueIdentify(uint id, IdRequester who);
/// <summary>It holds items (a pack, a chest): only these are looked into by the login sweep.</summary>
bool IsContainer(uint id);
}
internal sealed class AutoIdDrain
@ -70,13 +78,14 @@ internal sealed class AutoIdDrain
public long OpenedAtMs; // when OpenContainer was opened (0 = unknown)
public long SettleUntilMs; // PlayerLifecycleLog.SettleUntilMs
public long LoginCompleteAtMs; // LoginLifecycleHooks.LoginCompleteAtMs
public bool CombatBusy; // a target selected, or a cast or attack in flight (Inventory waits)
}
private struct Pending
{
public uint Id;
public long AtMs; // created (or moved, or its container opened, or swept) at
public byte Counted; // waits already counted: 1 = age, 2 = defer, 4 = rate
public byte Counted; // waits already counted: 1 = age, 2 = defer, 4 = rate, 8 = combat
}
private readonly IAutoIdWorld _w;
@ -88,7 +97,7 @@ internal sealed class AutoIdDrain
private long _lastInventorySendMs;
private long _sweptLoginAt;
private long _skipMode, _skipAge, _skipDefer, _skipFresh, _skipRate, _overflow, _incomingDropped, _refused, _swept;
private long _skipMode, _skipAge, _skipDefer, _skipFresh, _skipRate, _skipCombat, _overflow, _incomingDropped, _refused, _swept;
public AutoIdDrain(IAutoIdWorld world) { _w = world; }
@ -97,6 +106,8 @@ internal sealed class AutoIdDrain
public long SkipDefer => Interlocked.Read(ref _skipDefer);
public long SkipFresh => Interlocked.Read(ref _skipFresh);
public long SkipRate => Interlocked.Read(ref _skipRate);
/// <summary>Inventory candidates that waited for combat to end (counted once each).</summary>
public long SkipCombat => Interlocked.Read(ref _skipCombat);
/// <summary>Candidates not taken because the Limited pending list was full.</summary>
public long Overflow => Interlocked.Read(ref _overflow);
/// <summary>Oldest new candidates dropped because _incoming was full (the drain was held).</summary>
@ -171,23 +182,32 @@ internal sealed class AutoIdDrain
return;
if (c.Now < c.LoginCompleteAtMs + AutoIdPolicy.DeferMs || c.PlayerId == 0 || !_w.SnapshotWarm)
return;
_sweptLoginAt = c.LoginCompleteAtMs;
// A full pending list: try again on a later tick (no walk, no overflow counted).
if (_pending.Count >= MaxPendingLimited)
return;
Span<uint> top = stackalloc uint[MaxSweepItems];
Span<uint> inner = stackalloc uint[MaxSweepItems];
int n = _w.CollectOwned(c.PlayerId, top);
int before = _pending.Count;
long overflowBefore = Interlocked.Read(ref _overflow);
for (int i = 0; i < n; i++)
{
uint id = top[i];
if (_w.IsLive(id) && !_w.HasAppraisal(id))
AddPending(id, c.LoginCompleteAtMs, c.Mode);
// A side pack (or anything else holding items): its contents are the pack too.
// A side pack: its contents are the pack too. Only a container is looked into.
if (!_w.IsContainer(id))
continue;
int m = _w.CollectOwned(id, inner);
for (int k = 0; k < m; k++)
if (_w.IsLive(inner[k]) && !_w.HasAppraisal(inner[k]))
AddPending(inner[k], c.LoginCompleteAtMs, c.Mode);
}
Interlocked.Add(ref _swept, _pending.Count - before);
// Done only when everything fit; an overflowed sweep runs again once there is room
// (what it already added is not added twice: AddPending skips ids already pending).
if (Interlocked.Read(ref _overflow) == overflowBefore)
_sweptLoginAt = c.LoginCompleteAtMs;
}
private int DrainPending(in Context c, int sent)
@ -227,11 +247,11 @@ internal sealed class AutoIdDrain
bool fresh = cached && _w.IsFresh(p.Id);
bool rateOk = AutoIdPolicy.InventoryRateAllows(c.Now, _lastInventorySendMs);
AutoIdPolicy.Verdict v = AutoIdPolicy.Decide(c.Mode, cls, c.Now - p.AtMs, inDefer, openedAfterSettle,
fresh, cached, rateOk, snapshotWarm);
fresh, cached, rateOk, snapshotWarm, c.CombatBusy);
switch (v)
{
case AutoIdPolicy.Verdict.Send:
if (!_w.Enqueue(p.Id, RequesterFor(cls)))
if (!_w.QueueIdentify(p.Id, RequesterFor(cls)))
{
// Refused (a transition began, or the ring is full): keep it, and
// stop sending this tick.
@ -258,6 +278,10 @@ internal sealed class AutoIdDrain
if ((p.Counted & 4) == 0) { p.Counted |= 4; Interlocked.Increment(ref _skipRate); }
_pending[keep++] = p;
break;
case AutoIdPolicy.Verdict.WaitCombat:
if ((p.Counted & 8) == 0) { p.Counted |= 8; Interlocked.Increment(ref _skipCombat); }
_pending[keep++] = p;
break;
case AutoIdPolicy.Verdict.SkipFresh:
Interlocked.Increment(ref _skipFresh);
_pendingIds.Remove(p.Id);