A flight capture from one 26-minute session showed the client sending 3211 IdentifyObject
(0xC8) messages, most of everything it sent, because AutoId appraised every object the
client created. Bots already appraise at the point of use, so the background appraisals
are now limited.
AutoIdMode (engine.json "AutoIdMode", default Limited; /rc autoid all|limited|off, live
and saved; /rc autoid alone reports it):
All the old behaviour.
Limited only the selected target (fresh-cache skip), items in the container the
player has open now, and items in the player's own inventory with nothing
cached. The last two wait until the object is 2 s old and until 30 s after a
world transition's settle window ends (death, portal, teleport, landblock jump;
walking into the next landblock never defers). A container opened after the
settle ended is not deferred. Creatures, ground items, other players: never.
Off nothing, the selected target included.
Plugins' own RequestId calls are not limited; they still merge in the RequestId slot.
The decision is AutoIdPolicy (pure). AutoIdService now keeps a pending list on its drain
timer (waits for age/defer stay, skips leave), classifies each candidate from the PWD
snapshot (container / wielder / side pack owner, and the ground container from
ViewObjectContents), takes InventoryPutObjInContainer (0x0022) moves from the server
message stream, and rescans an opened container 2.5 s after the open.
Requester tagging: a thread-local EngineSendMarker around every engine RequestId call
(AutoId by class, plugin host API, the login self-identify, the Inventory panel hover).
A queued request carries its requester in PendingIdQueue and the main-thread drain
re-enters the marker around the real send. Only a marked send registers with the
flight recorder (FlightCapture.EngineSend, its own 64-entry table so bursts never evict
other engine calls), so its 0xC8 shows as [eng:autoid-target|autoid-container|
autoid-inventory|autoid-world|plugin|self|ui]; an unmarked 0xC8 stays [cli] and counts
as AC's or Decal's.
Per-minute log line "autoid 60s: ..." (its own line, not part of "hb #"): sends by
requester, cli, total, AutoId skips by reason (mode, age, defer, fresh), pending.
EngineSettings keeps upstream's MaxHotReloads, IltServerNames/IltHosts, ReportEndpoint
and CloseOnServerDisconnect next to AutoIdMode (owned keys, load and save).
Tests: EngineRobustnessTests (decision table against an independent expected function,
spot cases, defer window from WorldTransitionState inputs, classification, marker scopes
and thread-locality, requester through PendingIdQueue, heartbeat field names against the
launcher regex), FlightRecorderTests (identify tagging, foreign count, match window, burst),
EngineSettingsTests (AutoIdMode round trip, default left out). check_offthread_mutators.py:
DrainPending allow-listed like DrainTick (it only enqueues).
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
254 lines
15 KiB
C#
254 lines
15 KiB
C#
using RynthCore.Engine.Compatibility;
|
|
using RynthCore.Engine.Net;
|
|
using V = RynthCore.Engine.Compatibility.AutoIdPolicy.Verdict;
|
|
using C = RynthCore.Engine.Compatibility.AutoIdPolicy.Class;
|
|
|
|
namespace EngineRobustnessTests;
|
|
|
|
/// <summary>
|
|
/// AutoId limiting: the mode decision table, the post-transition defer, where an
|
|
/// object is, and the engine-send marker that tags every engine 0xC8.
|
|
/// </summary>
|
|
internal static partial class Program
|
|
{
|
|
private static void RunAutoIdLimitTests()
|
|
{
|
|
AutoIdDecisionTable();
|
|
AutoIdDecisionSpotCases();
|
|
AutoIdDeferWindow();
|
|
AutoIdClassify();
|
|
AutoIdModeParse();
|
|
IdMarkerScopes();
|
|
IdMarkerRidesTheQueue();
|
|
IdHeartbeatNames();
|
|
}
|
|
|
|
// ---- the decision table ---------------------------------------------------------
|
|
|
|
private static readonly long[] Ages = { 0, AutoIdPolicy.MinAgeMs - 1, AutoIdPolicy.MinAgeMs, 60_000 };
|
|
|
|
/// <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)
|
|
{
|
|
if (mode == AutoIdMode.Off) return V.SkipMode;
|
|
if (mode == AutoIdMode.All) return cls == C.Target && fresh ? V.SkipFresh : V.Send;
|
|
bool young = age < AutoIdPolicy.MinAgeMs;
|
|
return cls switch
|
|
{
|
|
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 : V.Send,
|
|
C.Unknown => young ? V.WaitAge : V.SkipMode,
|
|
_ => V.SkipMode,
|
|
};
|
|
}
|
|
|
|
private static void AutoIdDecisionTable()
|
|
{
|
|
_case = "autoid decision table";
|
|
int rows = 0, wrong = 0;
|
|
foreach (AutoIdMode mode in Enum.GetValues<AutoIdMode>())
|
|
foreach (C cls in Enum.GetValues<C>())
|
|
foreach (long age in Ages)
|
|
foreach (bool defer in new[] { false, true })
|
|
foreach (bool opened in new[] { false, true })
|
|
foreach (bool cached in new[] { false, true })
|
|
foreach (bool fresh in new[] { false, true })
|
|
{
|
|
if (fresh && !cached) continue; // fresh implies cached
|
|
rows++;
|
|
V got = AutoIdPolicy.Decide(mode, cls, age, defer, opened, fresh, cached);
|
|
V want = Expected(mode, cls, age, defer, opened, fresh, cached);
|
|
if (got != want && wrong++ < 10)
|
|
Check(false, $"{mode} {cls} age={age} defer={defer} opened={opened} cached={cached} fresh={fresh}: want {want}, got {got}");
|
|
}
|
|
Check(wrong == 0, $"{wrong} of {rows} rows wrong");
|
|
Check(rows == 3 * 5 * 4 * 2 * 2 * 3, $"row count {rows}");
|
|
}
|
|
|
|
/// <summary>The same rules as named cases, so a mutation is easy to read off a failure.</summary>
|
|
private static void AutoIdDecisionSpotCases()
|
|
{
|
|
_case = "autoid spot cases";
|
|
const long Old = 10_000;
|
|
// Off: nothing at all, the selected target included.
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Off, C.Target, 0, false, false, false, false) == V.SkipMode, "off skips the target");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Off, C.Container, Old, false, true, false, false) == V.SkipMode, "off skips an open container");
|
|
// All: the old behaviour.
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.All, C.World, 0, true, false, false, false) == V.Send, "all sends a fresh-born world object, deferred or not");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.All, C.Target, 0, false, false, true, true) == V.SkipFresh, "all keeps the target fresh skip");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.All, C.Inventory, 0, false, false, false, true) == V.Send, "all sends a cached pack item (old behaviour)");
|
|
// Limited: target.
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Target, 0, true, false, false, false) == V.Send, "target exempt from age and defer");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Target, 0, false, false, true, true) == V.SkipFresh, "target fresh skip");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Target, 0, false, false, false, true) == V.Send, "target cached but stale is sent");
|
|
// Limited: world never.
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.World, Old, false, false, false, false) == V.SkipMode, "creature / ground item skipped");
|
|
// Limited: container.
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, AutoIdPolicy.MinAgeMs - 1, false, true, false, false) == V.WaitAge, "container item waits for the minimum age");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, AutoIdPolicy.MinAgeMs, false, false, false, false) == V.Send, "container item at the minimum age is sent");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, Old, true, false, false, false) == V.WaitDefer, "container opened before the settle ended waits out the defer");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, Old, true, true, false, false) == V.Send, "container opened after the settle is not deferred");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, Old, false, false, true, true) == V.SkipFresh, "fresh container item skipped");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Container, Old, false, false, false, true) == V.Send, "stale cached container item re-appraised");
|
|
// Limited: inventory.
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, Old, false, false, false, true) == V.SkipFresh, "pack item with any cached appraisal skipped");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, AutoIdPolicy.MinAgeMs - 1, false, false, false, false) == V.WaitAge, "pack item waits for the minimum age");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, Old, true, true, false, false) == V.WaitDefer, "pack item deferred (the opened-container exemption does not apply)");
|
|
Check(AutoIdPolicy.Decide(AutoIdMode.Limited, C.Inventory, Old, false, false, false, false) == V.Send, "uncached pack item sent");
|
|
// 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");
|
|
Check(AutoIdPolicy.DefaultMode == AutoIdMode.Limited, "default mode is Limited");
|
|
}
|
|
|
|
// ---- the post-transition defer -----------------------------------------------------
|
|
|
|
private static void AutoIdDeferWindow()
|
|
{
|
|
_case = "autoid defer window";
|
|
const long D = AutoIdPolicy.DeferMs;
|
|
const long S = WorldTransitionState.SettleMs;
|
|
|
|
// No transition yet: nothing is deferred.
|
|
var w = new WorldTransitionState();
|
|
w.Sample(1000, dead: false, portaling: false, landblock: 0xA9B4);
|
|
Check(!AutoIdPolicy.InDefer(1000, w.SettleUntilMs, w.InTransition(1000)), "no transition, no defer");
|
|
|
|
// Walking into the next landblock never defers.
|
|
w.Sample(2000, false, false, 0xAAB4);
|
|
w.Sample(3000, false, false, 0xAAB5);
|
|
Check(w.SettleUntilMs == 0, "an adjacent walk sets no settle window");
|
|
Check(!AutoIdPolicy.InDefer(3000, w.SettleUntilMs, w.InTransition(3000)), "adjacent walk: no defer");
|
|
|
|
// A portal: deferred through portal space, the settle window, and DeferMs after it.
|
|
w.Sample(10_000, false, true, 0xAAB5);
|
|
Check(AutoIdPolicy.InDefer(10_000, w.SettleUntilMs, w.InTransition(10_000)), "in portal space: deferred");
|
|
w.Sample(12_000, false, false, 0x0007);
|
|
long settleEnd = w.SettleUntilMs;
|
|
Check(settleEnd == 12_000 + S, $"settle ends {S} ms after the portal exit, got {settleEnd}");
|
|
Check(AutoIdPolicy.InDefer(settleEnd + D - 1, settleEnd, w.InTransition(settleEnd + D - 1)), "deferred until DeferMs after the settle");
|
|
Check(!AutoIdPolicy.InDefer(settleEnd + D, settleEnd, w.InTransition(settleEnd + D)), "not deferred at DeferMs after the settle");
|
|
|
|
// A container opened after the settle ended is exempt; one opened before is not.
|
|
Check(AutoIdPolicy.OpenedAfterSettle(settleEnd + 1, settleEnd), "opened after the settle: exempt");
|
|
Check(!AutoIdPolicy.OpenedAfterSettle(settleEnd - 1, settleEnd), "opened during the settle: not exempt");
|
|
Check(!AutoIdPolicy.OpenedAfterSettle(0, settleEnd), "never opened: not exempt");
|
|
|
|
// Death defers too.
|
|
var d = new WorldTransitionState();
|
|
d.Sample(1000, false, false, 0x1234);
|
|
d.Sample(2000, true, false, 0x1234);
|
|
d.Sample(3000, false, false, 0x1234);
|
|
Check(AutoIdPolicy.InDefer(3000 + S + D - 1, d.SettleUntilMs, false), "death: deferred");
|
|
Check(!AutoIdPolicy.InDefer(3000 + S + D, d.SettleUntilMs, false), "death: defer ends");
|
|
|
|
// A landblock jump without portal space defers.
|
|
var j = new WorldTransitionState();
|
|
j.Sample(1000, false, false, 0x00AE);
|
|
j.Sample(1100, false, false, 0xA9B4);
|
|
Check(AutoIdPolicy.InDefer(1100 + S + D - 1, j.SettleUntilMs, false), "landblock jump: deferred");
|
|
|
|
// A PlayerTeleport message (a recall into the next landblock) defers.
|
|
var t = new WorldTransitionState();
|
|
t.Sample(1000, false, false, 0x1234);
|
|
t.OnTeleportMessage(5000);
|
|
Check(AutoIdPolicy.InDefer(5000 + S + D - 1, t.SettleUntilMs, false), "teleport message: deferred");
|
|
Check(!AutoIdPolicy.InDefer(5000 + S + D, t.SettleUntilMs, false), "teleport message: defer ends");
|
|
|
|
// In a transition right now: deferred whatever the settle time says.
|
|
Check(AutoIdPolicy.InDefer(1, 0, inTransition: true), "in a transition: deferred");
|
|
}
|
|
|
|
// ---- where an object is ------------------------------------------------------------
|
|
|
|
private static void AutoIdClassify()
|
|
{
|
|
_case = "autoid classify";
|
|
const uint Me = 0x50000001, Corpse = 0x80000100, Pack = 0x80000200, Monster = 0x80000300, Chest = 0x80000400;
|
|
Check(AutoIdPolicy.Classify(false, Me, Corpse, Corpse, 0, 0) == C.Unknown, "not in the snapshot: unknown");
|
|
Check(AutoIdPolicy.Classify(true, Me, 0, Me, 0, 0) == C.Inventory, "in the main pack");
|
|
Check(AutoIdPolicy.Classify(true, Me, 0, 0, Me, 0) == C.Inventory, "worn");
|
|
Check(AutoIdPolicy.Classify(true, Me, 0, Pack, 0, Me) == C.Inventory, "in a side pack");
|
|
Check(AutoIdPolicy.Classify(true, Me, Pack, Pack, 0, Me) == C.Inventory, "in an opened own side pack: inventory");
|
|
Check(AutoIdPolicy.Classify(true, Me, Corpse, Corpse, 0, 0) == C.Container, "in the open corpse");
|
|
Check(AutoIdPolicy.Classify(true, Me, Chest, Pack, 0, Chest) == C.Container, "in a pack inside the open chest");
|
|
Check(AutoIdPolicy.Classify(true, Me, Chest, Corpse, 0, 0) == C.World, "in a corpse that is not open");
|
|
Check(AutoIdPolicy.Classify(true, Me, 0, 0, 0, 0) == C.World, "on the ground, nothing open");
|
|
Check(AutoIdPolicy.Classify(true, Me, Corpse, 0, 0, 0) == C.World, "on the ground while a corpse is open (container 0 is not the open one)");
|
|
Check(AutoIdPolicy.Classify(true, Me, Corpse, 0, Monster, 0) == C.World, "wielded by a monster");
|
|
Check(AutoIdPolicy.Classify(true, 0, 0, 0, 0, 0) == C.World, "player unknown: nothing is inventory");
|
|
}
|
|
|
|
private static void AutoIdModeParse()
|
|
{
|
|
_case = "autoid mode parse";
|
|
Check(AutoIdPolicy.TryParseMode("ALL", out AutoIdMode m) && m == AutoIdMode.All, "all");
|
|
Check(AutoIdPolicy.TryParseMode(" limited ", out m) && m == AutoIdMode.Limited, "limited");
|
|
Check(AutoIdPolicy.TryParseMode("off", out m) && m == AutoIdMode.Off, "off");
|
|
Check(!AutoIdPolicy.TryParseMode("sometimes", out m) && m == AutoIdPolicy.DefaultMode, "unknown: false, default");
|
|
foreach (AutoIdMode mode in Enum.GetValues<AutoIdMode>())
|
|
Check(AutoIdPolicy.TryParseMode(AutoIdPolicy.Name(mode), out m) && m == mode, $"{mode} round-trips by name");
|
|
foreach (AutoIdMode mode in Enum.GetValues<AutoIdMode>())
|
|
Check(AutoIdPolicy.TryParseMode(mode.ToString(), out m) && m == mode, $"{mode} round-trips as engine.json writes it");
|
|
}
|
|
|
|
// ---- the engine-send marker --------------------------------------------------------
|
|
|
|
private static void IdMarkerScopes()
|
|
{
|
|
_case = "id marker";
|
|
Check(EngineSendMarker.Current == IdRequester.None, "no marker outside a scope");
|
|
using (EngineSendMarker.Enter(IdRequester.Plugin))
|
|
{
|
|
Check(EngineSendMarker.Current == IdRequester.Plugin, "marked inside");
|
|
using (EngineSendMarker.Enter(IdRequester.AutoIdContainer))
|
|
Check(EngineSendMarker.Current == IdRequester.AutoIdContainer, "nested scope");
|
|
Check(EngineSendMarker.Current == IdRequester.Plugin, "outer marker restored");
|
|
|
|
IdRequester seenElsewhere = IdRequester.Self;
|
|
var th = new Thread(() => seenElsewhere = EngineSendMarker.Current);
|
|
th.Start();
|
|
th.Join();
|
|
Check(seenElsewhere == IdRequester.None, "another thread is not marked (thread-local)");
|
|
}
|
|
Check(EngineSendMarker.Current == IdRequester.None, "cleared after the scope");
|
|
try
|
|
{
|
|
using (EngineSendMarker.Enter(IdRequester.Ui))
|
|
throw new InvalidOperationException();
|
|
}
|
|
catch (InvalidOperationException) { }
|
|
Check(EngineSendMarker.Current == IdRequester.None, "cleared after an exception");
|
|
}
|
|
|
|
private static void IdMarkerRidesTheQueue()
|
|
{
|
|
_case = "id marker queue";
|
|
var q = new PendingIdQueue(8);
|
|
q.Enqueue(1, 1000, 1, MergeMax, 0, (byte)IdRequester.AutoIdContainer);
|
|
q.Enqueue(2, 1000, 1, MergeMax, 0, (byte)IdRequester.Plugin);
|
|
Check(q.Enqueue(1, 1100, 1, MergeMax, 0, (byte)IdRequester.Plugin) == PendingIdQueue.Result.Merged, "merged");
|
|
Check(q.TryDequeue(out var a) && a.Source == (byte)IdRequester.AutoIdContainer, "the requester rides with the entry; a merge keeps the first");
|
|
Check(q.TryDequeue(out var b) && b.Source == (byte)IdRequester.Plugin, "second entry's requester");
|
|
q.Enqueue(3, 1200, 1, MergeMax);
|
|
Check(q.TryDequeue(out var c) && c.Source == 0, "unmarked stays None");
|
|
}
|
|
|
|
private static void IdHeartbeatNames()
|
|
{
|
|
_case = "id heartbeat names";
|
|
foreach (IdRequester who in Enum.GetValues<IdRequester>())
|
|
{
|
|
string s = IdRequesters.Short(who) + "=";
|
|
foreach (string bad in new[] { "fps=", "plug=", "login=", "qd=" })
|
|
Check(!s.Contains(bad) && !bad.Contains(s), $"{who}: \"{s}\" vs {bad}");
|
|
if (who != IdRequester.None)
|
|
Check(IdRequesters.Label((uint)who).Length > 0, $"{who} has a flight label");
|
|
}
|
|
Check(IdRequesters.Label(0) == "" && IdRequesters.Label(99) == "", "no label for none or an unknown code");
|
|
var regex = new System.Text.RegularExpressions.Regex(@"hb #\d+ .*?fps=(\d+) .*?plug=(\d+)/s .*?login=(\d)(?:.*?qd=(\d+))?");
|
|
string sample = "autoid 60s: mode=limited sent tgt=1 ctr=2 inv=0 wld=0 plg=5 slf=0 ui=0 cli=3 tot=11 skip mode=900 age=4 defer=0 fresh=1 pend=2";
|
|
Check(!regex.IsMatch(sample), "the minute line does not match the launcher's heartbeat regex");
|
|
}
|
|
}
|