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

@ -17,6 +17,8 @@ internal static partial class Program
public readonly HashSet<uint> Cached = new();
public readonly HashSet<uint> Released = new();
public bool Refuse;
public readonly HashSet<uint> Containers = new();
public readonly List<uint> Looked = new(); // every CollectOwned call
public readonly List<(uint Id, IdRequester Who, long At)> Sent = new();
public long Now;
@ -30,6 +32,7 @@ internal static partial class Program
public bool SnapshotWarm => Warm;
public int CollectOwned(uint container, Span<uint> dest)
{
Looked.Add(container);
if (!Warm) return 0;
int n = 0;
foreach (var kv in Snapshot)
@ -42,7 +45,8 @@ internal static partial class Program
public bool IsLive(uint id) => !Released.Contains(id);
public bool HasAppraisal(uint id) => Cached.Contains(id);
public bool IsFresh(uint id) => Cached.Contains(id);
public bool Enqueue(uint id, IdRequester who)
public bool IsContainer(uint id) => Containers.Contains(id);
public bool QueueIdentify(uint id, IdRequester who)
{
if (Refuse) return false;
Sent.Add((id, who, Now));
@ -53,9 +57,10 @@ internal static partial class Program
private const uint Me = 0x50000001, SidePack = 0x80000900;
private static AutoIdDrain.Context Ctx(FakeWorld w, long loginAt, AutoIdMode mode = AutoIdMode.Limited, long settle = 0) => new()
private static AutoIdDrain.Context Ctx(FakeWorld w, long loginAt, AutoIdMode mode = AutoIdMode.Limited, long settle = 0, bool combat = false) => new()
{
Now = w.Now, Mode = mode, PlayerId = Me, OpenContainer = 0, OpenedAtMs = 0, SettleUntilMs = settle, LoginCompleteAtMs = loginAt,
CombatBusy = combat,
};
/// <summary>Runs the 100 ms drain from w.Now to <paramref name="until"/>.</summary>
@ -75,6 +80,8 @@ internal static partial class Program
DrainUnknownWaitsForSnapshotThenGivesUp();
DrainRefusedEnqueueIsKept();
DrainBounds();
DrainCombatHoldsInventory();
DrainSweepRetriesAfterOverflow();
}
// The pack is created at login, before the snapshot is warm; it must still go out.
@ -86,7 +93,7 @@ internal static partial class Program
// The login create burst: 20 pack items and a side pack with 5 more, created at 10 s.
var pack = new List<uint>();
for (uint i = 1; i <= 20; i++) { pack.Add(0x80001000 + i); w.Snapshot[0x80001000 + i] = (Me, 0); }
w.Snapshot[SidePack] = (Me, 0);
w.Snapshot[SidePack] = (Me, 0); w.Containers.Add(SidePack);
pack.Add(SidePack);
for (uint i = 1; i <= 5; i++) { pack.Add(0x80002000 + i); w.Snapshot[0x80002000 + i] = (SidePack, 0); }
foreach (uint id in pack) d.Offer(id, w.Now);
@ -117,7 +124,7 @@ internal static partial class Program
var w = new FakeWorld { Now = 100_000, Warm = true };
var d = new AutoIdDrain(w);
for (uint i = 1; i <= 8; i++) w.Snapshot[0x80003000 + i] = (Me, 0); // main pack, never offered
w.Snapshot[SidePack] = (Me, 0);
w.Snapshot[SidePack] = (Me, 0); w.Containers.Add(SidePack);
for (uint i = 1; i <= 4; i++) w.Snapshot[0x80004000 + i] = (SidePack, 0); // side pack contents
w.Snapshot[0x80005000] = (0, Me); // worn
w.Cached.Add(0x80003001); // already appraised
@ -133,6 +140,9 @@ internal static partial class Program
Check(Enumerable.Range(1, 4).All(i => ids.Contains(0x80004000 + (uint)i)), "the side pack's contents are swept");
Check(ids.Contains(SidePack) && ids.Contains(0x80005000), "the side pack itself and worn gear are swept");
Check(!ids.Contains(0x80006000), "a ground item is not");
// Only containers are looked into: the player, then the side pack; never a sword.
Check(w.Looked.All(x => x == Me || x == SidePack) && w.Looked.Contains(SidePack),
$"only containers looked into ({string.Join(",", w.Looked.Distinct().Select(x => x.ToString("X8")))})");
Check(d.Swept == 13, $"13 swept (7 main pack, the side pack, its 4, 1 worn), got {d.Swept}");
int count = w.Sent.Count;
@ -229,4 +239,60 @@ internal static partial class Program
f.Tick(Ctx(h, 0, AutoIdMode.All), AutoIdDrain.MaxPerTick);
Check(f.PendingCount == AutoIdDrain.MaxIncoming, $"the newest {AutoIdDrain.MaxIncoming} kept, got {f.PendingCount}");
}
}
// The pack waits through combat (target selected, cast or attack in flight); kept, not
// dropped; an open container is not held.
private static void DrainCombatHoldsInventory()
{
_case = "drain combat";
const uint Chest = 0x80000A00, Loot = 0x80000A01;
var w = new FakeWorld { Now = 1_000_000, Warm = true };
var d = new AutoIdDrain(w);
for (uint i = 1; i <= 3; i++) { w.Snapshot[0x8000C000 + i] = (Me, 0); d.Offer(0x8000C000 + i, w.Now - 10_000); }
w.Snapshot[Loot] = (Chest, 0); d.Offer(Loot, w.Now - 10_000);
AutoIdDrain.Context Fight(bool combat) => new()
{
Now = w.Now, Mode = AutoIdMode.Limited, PlayerId = Me, OpenContainer = Chest, OpenedAtMs = w.Now - 5_000,
SettleUntilMs = 0, LoginCompleteAtMs = 1, CombatBusy = combat,
};
for (int k = 0; k < 50; k++) { d.Tick(Fight(true), 0); w.Now += 100; }
Check(w.Sent.Count == 1 && w.Sent[0].Id == Loot, $"in combat: only the open container's item is sent ({w.Sent.Count})");
Check(d.PendingCount == 3, $"the pack is kept, pending {d.PendingCount}");
Check(d.SkipCombat == 3, $"each counted once, got {d.SkipCombat}");
for (int k = 0; k < 20; k++) { d.Tick(Fight(false), 0); w.Now += 100; }
Check(w.Sent.Count == 4 && d.PendingCount == 0, $"out of combat: the pack goes out (sent {w.Sent.Count}, pending {d.PendingCount})");
Check(w.Sent.Skip(1).All(s => s.Who == IdRequester.AutoIdInventory), "as autoid-inventory");
}
// A sweep that overflowed the pending list is not marked done; it runs again once
// there is room, and every pack item is still sent.
private static void DrainSweepRetriesAfterOverflow()
{
_case = "drain sweep overflow";
long loginAt = 100_000, sweepAt = loginAt + AutoIdPolicy.DeferMs;
var pack = new List<uint>();
for (uint i = 1; i <= 5; i++) pack.Add(0x8000D000 + i);
// Full: the sweep does not walk (no overflow counted) and is not done.
var w = new FakeWorld { Now = sweepAt, Warm = true };
foreach (uint id in pack) w.Snapshot[id] = (Me, 0);
var d = new AutoIdDrain(w);
for (uint i = 0; i < AutoIdDrain.MaxPendingLimited; i++) d.Offer(0x84000000 + i, w.Now); // young, unplaced: they wait
d.Tick(Ctx(w, loginAt), AutoIdDrain.MaxPerTick);
Check(d.PendingCount == AutoIdDrain.MaxPendingLimited && d.Swept == 0 && d.Overflow == 0,
$"pending full: no sweep yet (pending {d.PendingCount}, swept {d.Swept}, overflow {d.Overflow})");
RunUntil(d, w, sweepAt + 20_000, loginAt);
Check(pack.All(id => w.Sent.Any(s => s.Id == id)), $"swept once there was room: all 5 sent ({w.Sent.Count})");
// Partly full: 2 fit, 3 overflow; the sweep runs again and the other 3 go out too.
var v = new FakeWorld { Now = sweepAt, Warm = true };
foreach (uint id in pack) v.Snapshot[id] = (Me, 0);
var e = new AutoIdDrain(v);
for (uint i = 0; i < AutoIdDrain.MaxPendingLimited - 2; i++) e.Offer(0x85000000 + i, v.Now);
e.Tick(Ctx(v, loginAt), AutoIdDrain.MaxPerTick);
Check(e.Swept == 2 && e.Overflow == 3, $"2 fit, 3 overflowed (swept {e.Swept}, overflow {e.Overflow})");
RunUntil(e, v, sweepAt + 20_000, loginAt);
Check(pack.All(id => v.Sent.Any(s => s.Id == id)), $"the overflowed 3 are swept again and sent ({v.Sent.Count})");
Check(e.Swept == 5, $"5 swept in all, got {e.Swept}");
}
}

View file

@ -30,7 +30,7 @@ internal static partial class Program
private static readonly long[] Ages = { 0, AutoIdPolicy.MinAgeMs - 1, AutoIdPolicy.MinAgeMs, 60_000, AutoIdPolicy.UnknownMaxWaitMs - 1, AutoIdPolicy.UnknownMaxWaitMs };
/// <summary>The rule as the spec states it, written out independently of AutoIdPolicy.Decide.</summary>
private static V Expected(AutoIdMode mode, C cls, long age, bool defer, bool openedAfter, bool fresh, bool cached, bool rateOk, bool warm)
private static V Expected(AutoIdMode mode, C cls, long age, bool defer, bool openedAfter, bool fresh, bool cached, bool rateOk, bool warm, bool combat)
{
if (mode == AutoIdMode.Off) return V.SkipMode;
if (mode == AutoIdMode.All) return cls == C.Target && fresh ? V.SkipFresh : V.Send;
@ -39,7 +39,7 @@ internal static partial class Program
{
C.Target => fresh ? V.SkipFresh : V.Send,
C.Container => fresh ? V.SkipFresh : young ? V.WaitAge : defer && !openedAfter ? V.WaitDefer : V.Send,
C.Inventory => cached ? V.SkipFresh : young ? V.WaitAge : defer ? V.WaitDefer : !rateOk ? V.WaitRate : V.Send,
C.Inventory => cached ? V.SkipFresh : young ? V.WaitAge : defer ? V.WaitDefer : combat ? V.WaitCombat : !rateOk ? V.WaitRate : V.Send,
C.Unknown => young ? V.WaitAge : !warm && age < AutoIdPolicy.UnknownMaxWaitMs ? V.WaitSnapshot : V.SkipMode,
_ => V.SkipMode,
};
@ -58,16 +58,17 @@ internal static partial class Program
foreach (bool fresh in new[] { false, true })
foreach (bool rateOk in new[] { false, true })
foreach (bool warm in new[] { false, true })
foreach (bool combat in new[] { false, true })
{
if (fresh && !cached) continue; // fresh implies cached
rows++;
V got = AutoIdPolicy.Decide(mode, cls, age, defer, opened, fresh, cached, rateOk, warm);
V want = Expected(mode, cls, age, defer, opened, fresh, cached, rateOk, warm);
V got = AutoIdPolicy.Decide(mode, cls, age, defer, opened, fresh, cached, rateOk, warm, combat);
V want = Expected(mode, cls, age, defer, opened, fresh, cached, rateOk, warm, combat);
if (got != want && wrong++ < 10)
Check(false, $"{mode} {cls} age={age} defer={defer} opened={opened} cached={cached} fresh={fresh} rateOk={rateOk} warm={warm}: want {want}, got {got}");
Check(false, $"{mode} {cls} age={age} defer={defer} opened={opened} cached={cached} fresh={fresh} rateOk={rateOk} warm={warm} combat={combat}: want {want}, got {got}");
}
Check(wrong == 0, $"{wrong} of {rows} rows wrong");
Check(rows == 3 * 5 * 6 * 2 * 2 * 3 * 2 * 2, $"row count {rows}");
Check(rows == 3 * 5 * 6 * 2 * 2 * 3 * 2 * 2 * 2, $"row count {rows}");
}
/// <summary>The same rules as named cases, so a mutation is easy to read off a failure.</summary>
@ -104,6 +105,11 @@ internal static partial class Program
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, Old, false, true, false, false, inventoryRateOk: false) == V.Send, "the rate cap is the inventory's only (container)");
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Target, 0, true, false, false, false, inventoryRateOk: false) == V.Send, "the rate cap never holds the selected target");
Check(AutoIdPolicy.Decide(AutoIdMode.All, C.Inventory, 0, false, false, false, false, inventoryRateOk: false) == V.Send, "All mode keeps the old behaviour (no cap)");
// Combat: only the pack waits; the target and an open container do not.
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, Old, false, false, false, false, combatBusy: true) == V.WaitCombat, "pack item waits while in combat");
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Target, 0, false, false, false, false, combatBusy: true) == V.Send, "combat never holds the selected target");
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, Old, false, true, false, false, combatBusy: true) == V.Send, "combat never holds an open container");
Check(AutoIdPolicy.Decide(AutoIdMode.All, C.Inventory, Old, false, false, false, false, combatBusy: true) == V.Send, "All mode: no combat hold (old behaviour)");
// Limited: unknown place.
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Unknown, 100, false, false, false, false) == V.WaitAge, "unknown place waits while young");
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Unknown, Old, false, false, false, false) == V.SkipMode, "still unknown when old: skipped");

View file

@ -419,6 +419,34 @@ internal static unsafe class Program
c.EngineSend(0, Corpse);
Out(c, Identify(Corpse));
Has(Last(c), "[cli]", "a send without a requester is not registered");
Eq(c.EngineSend(0, Corpse), -1, "no requester: no token");
// Registered before the call (CombatActionHooks.RequestId): a 0xC8 that AC sends
// synchronously, inside the call, is still tagged.
int sync = c.EngineSend((byte)IdRequester.AutoIdInventory, Corpse);
Out(c, Identify(Corpse));
Has(Last(c), "[eng:autoid-inventory] IdentifyObject 0x80068ACE", "a send made inside the call is tagged");
c.EngineSendFailed(sync, Corpse); // already matched: a no-op
Out(c, Identify(Corpse));
Has(Last(c), "[cli]", "a matched entry is not revived");
// The call returned false (or threw): its entry is retired, so a later 0xC8 for the
// same id is AC's or Decal's.
long foreignFail = c.IdentifyForeignTotal;
int failed = c.EngineSend((byte)IdRequester.AutoIdInventory, Corpse);
Check(failed >= 0, "a registration returns a token");
c.EngineSendFailed(failed, Corpse);
Out(c, Identify(Corpse));
Has(Last(c), "[cli]", "a failed send leaves no live entry");
Eq(c.IdentifyForeignTotal, foreignFail + 1, "and that 0xC8 counts as foreign");
// Retiring a token whose slot now holds another id leaves that entry alone.
int stale = c.EngineSend((byte)IdRequester.AutoIdTarget, Other);
c.EngineSendFailed(stale, Corpse);
c.EngineSendFailed(-1, Other);
c.EngineSendFailed(int.MaxValue, Other);
Out(c, Identify(Other));
Has(Last(c), "[eng:autoid-target]", "a retire for another id, or a bad token, is a no-op");
// 200 sends in a burst (a full RequestId queue drained at once) do not push out other
// engine calls (their own table), and every one of them is matched.

View file

@ -98,7 +98,7 @@ ENQUEUE = "AcMainThreadQueue.Enqueue"
# (file, method) pairs, each verified to do no off-thread AC mutation.
ENQUEUE_PRODUCER_ALLOW = {
("AutoIdService.cs", "DrainTick"), # appraisal: enqueues 0xC8, sent on main thread in DrainRequestIds
("AutoIdService.cs", "Enqueue"), # LiveWorld.Enqueue, called from AutoIdDrain on the drain timer: only EnqueueRequestId
("AutoIdService.cs", "QueueIdentify"), # LiveWorld.QueueIdentify, AutoIdDrain's send on the drain timer: only EnqueueRequestId
("RynthCoreChatCommands.cs", "Reply"), # /rc and /rv plates replies: only EnqueueWriteToChat
("AcMouseOver.cs", "Say"), # /rc acmouse watch lines: only EnqueueWriteToChat (and the log)
}