acdream/src/AcDream.Headless/Hosting/HeadlessRuntimePlacementProjectionSink.cs
Erik b1f914d508 fix(physics): restore presentation when a park is cancelled (#312)
Regression from 7f1c1f5a (C4 route 4b-2). A remote player who recalled in,
arrived, and stood still was permanently absent from the world render AND the
radar while remaining fully simulated — 71 healthy physics ticks with contact
and walkable, interpolation enqueues, equipment attached, chat visible.

Route 4b-2 is the first commit that lets an ordinary remote UpdatePosition open
a canonical SetPosition. A park publishes a synchronous Withdraw that tears down
presentation registrations; only TryPublishPlace restores them.
RestoreParkWithdrawal — added in the same slice — restores InWorld, the object
clock, and canonical residency, i.e. the Runtime half only. Eight Opus reviews
verified those three fields and the tests asserted exactly them, so the suite
stayed green while the entity was invisible.

Why it is intermittent: the presentation half IS restored incidentally by the
per-packet prologue rebucket for a MOVING remote. It only sticks when the
entity parks on its FINAL accepted Position and then goes idle, because ACE
stops broadcasting for a stationary entity, so no later packet arrives to
re-publish it and nothing else re-drives.

The fix publishes a RuntimePlacementProjectionKind.WithdrawalRestored receipt on
the one ordered placement stream, acknowledge-only in Runtime (the parked
operation is already retired by CancelCoreDeferred), which the App sink maps to
the exact inverse of its own TryPublishWithdrawal: the projection half (bucket,
IsSpatiallyProjected, IsSpatiallyVisible, spatial indexes, RefreshPresentation)
plus the publish half (_worldState, _worldEvents, _effectPoses,
_localPlayerShadow, visibility sinks). Applied with commitPose: false, because
the withdrawal never moved the sidecar; a test feeds a deliberately wrong
position to pin that.

Two alternatives were refuted on measurement, not preference. Routing the
restore's SetFullCell through CommitCanonicalCell cannot fire on the shipped
remote path at all — the prologue rebucket has already recommitted a non-zero
FullCellId before the merge cancels the park, so no cell edge remains — and it
never touches the publish half regardless. Extending RestoreParkWithdrawal
directly reduces to the same receipt, since Runtime must not reach behind the
host sink.

Gated on the entity ending the rollback canonically whole (FullCellId != 0 &&
InWorld) rather than on residencyRestored, which is false on the shipped remote
path and would have made the fix a no-op. AP-136's quiescing-prefix refusal arm
is preserved: no receipt, entity stays withdrawn.

Corrects my own framing of the defect: _worldState/_worldEvents/_effectPoses are
lost but are NOT what kills render and radar (_worldState is the plugin
IGameState; _effectPoses is the pose registry, not entity.MeshRefs). The
load-bearing casualties are the visibility sinks and the
IsSpatiallyProjected/IsSpatiallyVisible + bucket removal that gates the radar.

Register: AD-63 filed (selection deliberately not restored — user intent),
AP-136 amended (its "restored visible" claim covered only the canonical half;
the gap was a defect, not a divergence). ShadowObjectRegistry.Suspend stays
out of scope per AP-136.

Seven-revert discrimination table including one that proves the test is not
merely re-checking the bucket. Suite 11,023 passed / 4 skipped / 0 failed.

Live gate is user-run and folds into #309: two clients, ACDREAM_PROBE_PARK=1,
recall a remote in and let it stand still; acceptance is
[park-restore] ... presentation=True for that guid plus a visible model and a
radar blip.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-04 11:21:45 +02:00

122 lines
5 KiB
C#

using AcDream.Runtime;
using AcDream.Runtime.Entities;
using AcDream.Runtime.Physics;
using AcDream.Runtime.World;
namespace AcDream.Headless.Hosting;
/// <summary>
/// Validation-only no-window observer for canonical Runtime SetPosition
/// receipts. A headless host has no graphical sidecar to move or hide, so a
/// valid receipt is acknowledged without re-running placement or mutating
/// Runtime's body, controller, shadows, clocks, or worksets.
/// </summary>
internal sealed class HeadlessRuntimePlacementProjectionSink
: IRuntimePlacementProjectionSink
{
private readonly GameRuntime _runtime;
internal HeadlessRuntimePlacementProjectionSink(GameRuntime runtime)
{
_runtime = runtime ?? throw new ArgumentNullException(nameof(runtime));
}
public bool TryApply(
in RuntimePlacementProjectionSnapshot projection)
{
if (projection.Kind is RuntimePlacementProjectionKind.Discard)
{
// Discard cancels only an unacknowledged observation. It is valid
// even after its entity/session authority has been superseded.
return true;
}
if (projection.Kind is RuntimePlacementProjectionKind.ExecutorCompleted)
{
// F1: acknowledge-and-ignore, same as Discard - ExecutorCompleted
// is not a placement to project (a headless host has no
// presentation to bind off the completed initial drain; the
// executor's own drain already committed every canonical fact).
// It must NOT fall through to the record-lookup gate below: that
// gate can validly reject an unrelated entity/session mismatch,
// and this sink's caller (RuntimePlacementProjectionSubscription)
// treats a false return as "leave at the FIFO head" - a rejected
// ExecutorCompleted would permanently wedge the entire ordered
// placement stream behind it.
return true;
}
if (projection.Kind
is RuntimePlacementProjectionKind.WithdrawalRestored)
{
// Acknowledge-and-ignore for the same reason: the receipt rolls
// back the PRESENTATION half of a cancelled park's withdrawal, and
// a headless host has no graphical sidecar, plugin world state,
// effect poses, or visibility sinks to restore - Runtime already
// restored every canonical fact before publishing it. Refusing it
// would wedge the whole ordered stream.
return true;
}
RuntimePlacementProjectionToken token = projection.Token;
RuntimeEntityDirectory directory = _runtime.EntityObjects.Entities;
if (projection.Kind is RuntimePlacementProjectionKind.Place
or RuntimePlacementProjectionKind.Withdraw
&& token.IsValid
&& directory.TryGetByLocalId(
token.Entity.LocalEntityId,
out RuntimeEntityRecord residenceCandidate)
&& directory.IsCurrent(residenceCandidate)
&& residenceCandidate.Key == token.Entity
&& _runtime.EntityObjects.TryGetInitialCreateResidence(
residenceCandidate,
out _))
{
// C3c: a Place/Withdraw for an entity still holding its
// initial-create residence belongs to the first-entry conductor
// machinery, which acknowledges its own receipts at the exact
// FIFO head. Leave it there for the drive pump; validating or
// acknowledging it here would starve the conductor forever.
return false;
}
if (!token.IsValid
|| token.SessionLifetimeVersion
!= directory.SessionLifetimeVersion
|| !directory.TryGetByLocalId(
token.Entity.LocalEntityId,
out RuntimeEntityRecord record)
|| !directory.IsCurrent(record)
|| record.Key != token.Entity
|| !HasValidPortalShape(token))
{
return false;
}
if (projection.Kind is RuntimePlacementProjectionKind.Withdraw)
return true;
if (projection.Kind is not RuntimePlacementProjectionKind.Place)
return false;
return record.PositionAuthorityVersion
== token.PositionAuthorityVersion
&& record.SpatialAuthorityVersion
== token.SpatialAuthorityVersion
&& record.PlacementCommitVersion
== token.PlacementCommitVersion
&& record.FullCellId == token.ExactCellId
&& _runtime.TransitOwner.IsCurrentPlacementAuthority(
token.Portal,
token.ExactCellId);
}
private static bool HasValidPortalShape(
in RuntimePlacementProjectionToken token)
{
RuntimePortalPlacementAuthority portal = token.Portal;
if (!portal.Present)
return portal.IsEmpty;
return portal.IsValid
&& portal.Projection.DestinationCell == token.ExactCellId;
}
}