feat(01-04): preserve observer identity through control and scene changes

This commit is contained in:
UnixNight committed 2026-11-12 23:20:14 -05:00
1 parent 6bccd47b9b
commit ea4dc9b5df
4 files changed
+177 -25

No files matched your search

@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Numerics;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
@@ -13,6 +14,7 @@ internal sealed class FidelityObservation : IDisposable
private static readonly JsonSerializerOptions Json = new() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower };
private readonly Stopwatch clock = Stopwatch.StartNew();
private readonly string directory, session, caseId, kind;
private readonly string launchId = Guid.NewGuid().ToString("N");
private readonly DateTimeOffset wallStart = DateTimeOffset.UtcNow;
private readonly int maxRecords;
private const long MaxBytes = 512L * 1024 * 1024;
@@ -21,6 +23,8 @@ internal sealed class FidelityObservation : IDisposable
private string? failure;
private bool completed, disposed, ownsDirectory;
private double overhead;
private long? pendingDeparture;
private (string Scene, bool Controlled, string? Owner, bool Playing, string? Dialogue)? control;
private FidelityObservation(string directory, string session, string caseId, string kind, int maxRecords)
{ this.directory = directory; this.session = session; this.caseId = caseId; this.kind = kind; this.maxRecords = maxRecords; }
@@ -41,6 +45,7 @@ internal sealed class FidelityObservation : IDisposable
var session = Option("--fidelity-session") ?? throw new ArgumentException("--fidelity-session required");
var caseId = Option("--fidelity-case") ?? throw new ArgumentException("--fidelity-case required");
var fixture = args.Any(a => a.StartsWith("--smoke-", StringComparison.Ordinal)
|| a.StartsWith("--", StringComparison.Ordinal) && a.EndsWith("-smoke", StringComparison.Ordinal)
|| a is "--headless" or "--story-time" or "--time" or "--inspect" or "--skip-intro"
or "--noclip" or "--debug-menu" or "--debug-page" or "--gadget-menu");
return Create(output, session, caseId, fixture ? "port_fixture" : "native_port", project, originals);
@@ -57,9 +62,24 @@ internal sealed class FidelityObservation : IDisposable
var path = Path.TrimEndingDirectorySeparator(Path.GetFullPath(output));
static bool Within(string child, string parent) => child.Equals(parent, StringComparison.OrdinalIgnoreCase)
|| child.StartsWith(Path.TrimEndingDirectorySeparator(parent) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase);
var original = Path.TrimEndingDirectorySeparator(Path.GetFullPath(originals));
var git = Path.Combine(Path.GetFullPath(project), ".git");
if (Within(path, original) || Within(original, path) || Within(path, git) || Within(git, path))
static string ResolvedDirectory(string value)
{
var full = Path.GetFullPath(value);
var current = Path.GetPathRoot(full)!;
foreach (var part in full[current.Length..].Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries))
{
current = Path.Combine(current, part);
var info = new DirectoryInfo(current);
if (info.LinkTarget != null)
current = info.ResolveLinkTarget(true)?.FullName ?? throw new ArgumentException("Unresolved protected asset link");
}
return Path.TrimEndingDirectorySeparator(current);
}
var protectedRoots = new List<string> { ResolvedDirectory(originals), Path.Combine(ResolvedDirectory(project), ".git") };
// A generated --assets copy must not make the checkout's authoritative sibling writable.
foreach (var candidate in new[] { Path.Combine(project, "..", "Original"), Path.Combine(project, "Original"), Path.Combine(project, "assets") })
if (Directory.Exists(candidate)) protectedRoots.Add(ResolvedDirectory(candidate));
if (protectedRoots.Any(protectedRoot => Within(path, protectedRoot) || Within(protectedRoot, path)))
throw new ArgumentException("Observation output must be separate from original assets and Git metadata");
for (var parent = new DirectoryInfo(path); parent != null; parent = parent.Parent)
if (parent.LinkTarget != null) throw new ArgumentException("Observation output cannot traverse symbolic links");
@@ -80,6 +100,7 @@ internal sealed class FidelityObservation : IDisposable
observer.WriteSummary(false);
observer.writer = sink ?? new StreamWriter(new FileStream(Path.Combine(path, "events.jsonl"),
FileMode.CreateNew, FileAccess.Write, FileShare.Read), new UTF8Encoding(false), 65536);
observer.writer.NewLine = "\n";
}
catch (Exception error) when (OutputError(error)) { observer.Fail(error.Message); }
return observer;
@@ -89,8 +110,51 @@ internal sealed class FidelityObservation : IDisposable
=> Record("input_step", scene, simulationTick, "post_owner_step", new {
requested_input = requested, accepts_input = acceptsInput,
accepted_input = acceptsInput ? (PlayerInput?)requested : null,
control_owner = controlOwner,
acceptance_scope = "player_binding_input_gate; independent body/fire work is not classified by this boolean" });
control_owner = controlOwner, control_owner_phase = "post_step",
acceptance_scope = "reported binding gate only; individual command dispatch and independent body/fire work are not classified by this boolean" });
public void RecordSceneEntry(string scene, int simulationTick, bool returning, bool retainedPlayer, string entryPath)
{
control = null;
Record("scene_entry", scene, simulationTick, "application_scene_composition", new {
returning, retained_player = retainedPlayer, entry_path = entryPath, departure_sequence = pendingDeparture });
pendingDeparture = null;
}
public void RecordTransition(string scene, string destination, int simulationTick, bool returning)
{
Record("scene_departure", scene, simulationTick, "continuation_published", new { destination, returning });
pendingDeparture = sequence;
}
public void RecordRawEvent(string scene, int simulationTick, uint eventType, uint? scancode, byte? button, byte? repeat)
=> Record("raw_sdl_event", scene, simulationTick, "sdl_poll_before_routing", new {
event_type = eventType, scancode, button, repeat, acceptance = "raw_only; routing may consume or reject this event" });
public void RecordControl(string scene, int simulationTick, bool controlled, string? owner, bool playing, string? dialogue, string boundary)
{
var current = (scene, controlled, owner, playing, dialogue);
var previous = control;
if (previous == current && !boundary.StartsWith("skip:", StringComparison.Ordinal)) return;
var data = new { controlled, control_owner = owner, playing, dialogue };
Record("control_observation", scene, simulationTick, boundary, data);
if (boundary.StartsWith("skip:", StringComparison.Ordinal) && previous is { } before && (before.Playing || !before.Controlled))
Record("dialogue_skip", scene, simulationTick, boundary, data);
if ((previous == null || previous is { Playing: false }) && playing)
Record("dialogue_start", scene, simulationTick, boundary, data);
if (previous is { Playing: true } && !playing)
Record("dialogue_end", scene, simulationTick,
boundary == "director_step" ? "natural_director_step" : boundary, data);
control = current;
}
public void RecordState(string scene, int simulationTick, Vector3 root, Vector3 rotation, Vector3 velocity,
bool grounded, int movementMode, bool inputAfterPlayerUpdate, int controllers, int physics, int effects)
=> Record("player_state", scene, simulationTick, "post_owner_value_snapshot", new {
actor_root = new[] { root.X, root.Y, root.Z }, euler_degrees = new[] { rotation.X, rotation.Y, rotation.Z },
velocity = new[] { velocity.X, velocity.Y, velocity.Z }, grounded, movement_mode = movementMode,
input_after_player_update = inputAfterPlayerUpdate, controller_steps = controllers, physics_steps = physics,
effect_steps = effects, interpretation = "copied state only; bank/landing/idle require explicit review" });
private void Record(string name, string scene, int simulationTick, string method, object data)
{
@@ -100,7 +164,7 @@ internal sealed class FidelityObservation : IDisposable
{
if (sequence >= maxRecords) throw new IOException("Observation record limit reached");
var line = JsonSerializer.Serialize(new { schema_version = 1, session_id = session, case_id = caseId,
evidence_kind = kind, clock_id = session + ":stopwatch", sequence = sequence + 1,
evidence_kind = kind, launch_id = launchId, clock_id = launchId + ":stopwatch", sequence = sequence + 1,
elapsed_seconds = clock.Elapsed.TotalSeconds, simulation_tick = simulationTick,
name, scene, method, data }, Json);
var length = Encoding.UTF8.GetByteCount(line) + 1;
@@ -148,10 +212,11 @@ internal sealed class FidelityObservation : IDisposable
var target = Path.Combine(directory, "observation-summary.json");
var temporary = target + "." + Guid.NewGuid().ToString("N") + ".tmp";
var summary = new { schema_version = 1, session_id = session, case_id = caseId, evidence_kind = kind,
clock_id = session + ":stopwatch", wall_start = wallStart, wall_end = DateTimeOffset.UtcNow,
launch_id = launchId, clock_id = launchId + ":stopwatch", wall_start = wallStart, wall_end = DateTimeOffset.UtcNow,
elapsed_seconds = clock.Elapsed.TotalSeconds, records = sequence, bytes, incomplete = !success,
status = success ? "recorded" : "incomplete", failure,
diagnostic = true, observer_seconds = overhead, max_records = maxRecords, max_bytes = MaxBytes,
overhead_scope = "Record serialization/write attempts only; excludes caller snapshots, control comparisons, final flush and summaries",
limitations = new[] { "Diagnostic observer adds serialization, buffered disk writes and state-copy overhead; not a clean audiovisual capture.",
"Observation is not a receipt or an acceptance decision; fixture and native full-route evidence remain distinct.",
"No media clock mapping is inferred. Simulation ticks are scene-local and may reset across continuations.",
+44 -13
View File
@@ -341,6 +341,24 @@ internal static class Program
var debugCheckpointFile = toolsSmoke ? Path.GetFullPath("artifacts/debug-expansion/checkpoint.tsk")
: Path.Combine(AppContext.BaseDirectory, "debug-checkpoint.tsk");
Vector3 returnRoot = loadedSave?.StoredVector ?? Vector3.Zero;
void ObserveControl(string boundary)
{
if (observation != null && director != null)
observation.RecordControl(level!.Code, simulation?.Steps ?? 0, director.Controlled,
director.ControlledTag, director.Playing, director.CurrentTag, boundary);
}
void SkipObservedDialogue(string source)
{
ObserveControl("before_skip");
director!.Skip();
ObserveControl("skip:" + source);
}
void StepObservedDirector(float delta)
{
ObserveControl("before_director_step");
director?.Step(delta);
ObserveControl("director_step");
}
void RequestTransition(LevelTransition request)
{
if (transitionRequest != null) return;
@@ -593,6 +611,8 @@ internal static class Program
var renderer = presentation.LoadScene(mesh, texture, headless,
walking ? WalkTitle : animations?.WindowTitle ?? "Jimmy Neutron — scene preview | C camera",
width, height, !args.Contains("--no-validation"));
observation?.RecordSceneEntry(level?.Code ?? "preview", simulation?.Steps ?? 0, returning,
carriedPlayer != null, carriedSave != null ? "retained" : loadedSave != null ? "tsk-story" : "fresh-direct-scene");
var window = presentation.Window;
ShrinkSoldier? shrinkVictim = null;
var shrinkPhaseMask = 0;
@@ -668,6 +688,7 @@ internal static class Program
sceneAudio.Music(originalStart!.Text("MusicDatabase") ?? "none", originalStart.Integer("MusicIndex", -1));
}
director.Begin(originalStart!, activateStartTrigger: !returning);
ObserveControl("scene_begin");
gadgets?.Reset();
baseballs?.Reset();
interactions!.Reset();
@@ -685,7 +706,7 @@ internal static class Program
Console.WriteLine($"Practice RETURN: exact saved root={restored}, root={simulation.ActorRoot}, STRT yaw={simulation.Yaw}, intro suppressed={!director.Playing}, companion restored={companionRestored}, retained player input order={simulation.InputAfterPlayerUpdate}, gadget availability={interactions.Inventory.State(0, 3)}.");
if (!restored) throw new InvalidOperationException("RETURN did not restore the live departure root without replaying STRT actions");
}
if (args.Contains("--skip-intro") || jetpackSmoke || shrinkSmoke || invisibleSmoke || bubbleSmoke || baseballSmoke || rocketSmoke || grapplerSmoke || scooterSmoke || gadgetWindowSmoke || debugSmoke || diagnosticsSmoke || toolsSmoke || controlPhaseSmoke) { sceneAudio?.StopSpeech(); director.Skip(); }
if (args.Contains("--skip-intro") || jetpackSmoke || shrinkSmoke || invisibleSmoke || bubbleSmoke || baseballSmoke || rocketSmoke || grapplerSmoke || scooterSmoke || gadgetWindowSmoke || debugSmoke || diagnosticsSmoke || toolsSmoke || controlPhaseSmoke) { sceneAudio?.StopSpeech(); SkipObservedDialogue("startup-option-or-fixture"); }
if (gadgetWindowSmoke) { interactions!.Inventory.SetState(0,3,1); interactions.Inventory.SelectGadget(3); }
if (controlPhaseSmoke)
{
@@ -710,13 +731,13 @@ internal static class Program
{
if (interactions!.Inventory.State(0, 3) != 1)
throw new InvalidOperationException("VR04 PostLoad tail did not re-enable its owned practice jetpack");
director.Skip();
SkipObservedDialogue("fixture-setup");
interactions!.Update(level.Entities.First(e => e.Kind == "LOAD").Position);
if (transitionRequest is not { Returning: true }) throw new InvalidOperationException("VR04 exit did not preserve RETURN identity");
}
if (args.Contains("--smoke-transition"))
{
director.Skip();
SkipObservedDialogue("fixture-setup");
interactions!.Update(level!.Entities.First(e => e.Kind == "LOAD").Position);
if (transitionRequest == null) throw new InvalidOperationException("Original VR01 RETURN contact did not request a transition");
}
@@ -729,9 +750,9 @@ internal static class Program
if (carlSmoke) LabRouteSmoke.StartCarl(level!, director, interactions!, () => practice.Key(17, Vector2.Zero));
if (objectiveSmoke)
{
director.Skip();
SkipObservedDialogue("fixture-setup");
director.Touch(level!.Player!.Tag!, level.Entities.Single(e => e.Tag == "teleportexplanation").Position);
director.Skip();
SkipObservedDialogue("fixture-setup");
if (interactions!.CurrentObjective != 1) throw new InvalidOperationException("Original explanation did not assign objective1");
if (objectiveWindowSmoke) objectivePanel!.Step(9);
}
@@ -739,7 +760,7 @@ internal static class Program
|| practiceReturnSmoke && level.Code == "LV1B" && !returning)
{
if ((!headless && !practiceWindowSmoke) || level?.Code != "LV1B") throw new ArgumentException("Practice smoke requires the original lab");
director.Skip();
SkipObservedDialogue("fixture-setup");
if (practiceReturnSmoke)
{
// Move the real player away from STRT so an accidental
@@ -748,7 +769,7 @@ internal static class Program
director.SyncActor(level.Player!.Tag!, simulation.ActorRoot, simulation.Rotation);
}
director.Touch(level!.Player!.Tag!, level.Entities.Single(e => e.Tag == "teleportexplanation").Position);
director.Skip();
SkipObservedDialogue("fixture-setup");
interactions!.Update(director.FindActor("shrinkray")!.Position);
if (!practice.Active) throw new InvalidOperationException("Original lab pickup did not open practice modal");
if (args.Contains("--smoke-practice-transition") || practiceReturnSmoke) practice.Key(28, Vector2.Zero);
@@ -761,7 +782,7 @@ internal static class Program
interactions!.UpdateEnergy(simulation!.Energy);
foreach (var patrol in patrols.Values) patrol.Step(FixedStepClock.Step);
mechanisms!.Step(FixedStepClock.Step);
director.Step(FixedStepClock.Step);
StepObservedDirector(FixedStepClock.Step);
objectivePanel!.Step(FixedStepClock.Step, director.ControlledTag != null, practice.Active);
if (animations!.SelectAction(director.FindActor(level!.Player!.Tag!)!.Animation)) initialPoseTime = 0;
initialPoseTime += FixedStepClock.Step;
@@ -879,6 +900,7 @@ internal static class Program
if (simulation == null) { debugMenu.Message = "RESET REQUIRES PLAY MODE"; return; }
if (practice.Active) practice.Key(17, Vector2.Zero);
playerBinding?.ResetVehicle();simulation.Reset(); poseTime = 0; sceneAudio?.StopSpeech(); director?.Begin(originalStart!);
ObserveControl("reset_begin");
rocketship?.Reset();
if(scooter!=null)
{
@@ -914,7 +936,7 @@ internal static class Program
case DebugAction.Camera: ToggleCamera(); debugMenu.Close(); break;
case DebugAction.SkipCutscene:
if (director is { Playing: true } or { Blocked: true }||director is{Controlled:false}&&rocketship?.Mounted!=true)
{ sceneAudio?.StopSpeech(); director.Skip(); debugMenu.Message = "CUTSCENE SKIPPED"; }
{ sceneAudio?.StopSpeech(); SkipObservedDialogue("debug-menu"); debugMenu.Message = "CUTSCENE SKIPPED"; }
else debugMenu.Message = "NO ACTIVE CUTSCENE";
break;
case DebugAction.RefillEnergy when simulation != null:
@@ -1137,6 +1159,10 @@ internal static class Program
}
while (!headless && Sdl.SDL_PollEvent(out var e))
{
observation?.RecordRawEvent(level?.Code ?? "preview", simulation?.Steps ?? 0,
e.Type, e.Type is Sdl.KeyDown or Sdl.KeyUp ? e.Scancode : null,
e.Type is Sdl.MouseButtonDown or Sdl.MouseButtonUp ? e.MouseButton : null,
e.Type is Sdl.KeyDown or Sdl.KeyUp ? e.Repeat : null);
if(scooterSmoke&&e.Type==Sdl.KeyDown)
sawScooterKeys|=e.Scancode switch{81=>1,82=>2,80=>4,225=>8,43=>16,60=>32,21=>64,_=>0};
if(grapplerSmoke&&e.Type==Sdl.KeyDown)
@@ -1230,7 +1256,7 @@ internal static class Program
if (smoke && e.Scancode == 79) sawKey = true;
if (e.Scancode is 41 or 44 && e.Repeat == 0 && director is { Controlled: false }&&rocketship?.Mounted!=true
&& (e.Scancode != 44 || simulation?.Noclip != true))
{ sceneAudio?.StopSpeech(); director.Skip(); keys.Clear(); mouseJump = false; }
{ sceneAudio?.StopSpeech(); SkipObservedDialogue("sdl-key-" + e.Scancode); keys.Clear(); mouseJump = false; }
else if (e.Scancode == 41) running = false;
if (inspecting && e.Repeat == 0 && animations?.Select(e.Scancode) == true)
{
@@ -1301,6 +1327,10 @@ internal static class Program
else { simulation.Step(input); acceptsInput = true; }
observation?.RecordInput(level?.Code ?? "preview", simulation.Steps, input, acceptsInput,
director?.ControlledTag ?? (playerBinding == null ? "direct-simulation" : "script"));
observation?.RecordState(level?.Code ?? "preview", simulation.Steps, simulation.ActorRoot,
simulation.Rotation, simulation.Velocity, simulation.Grounded, simulation.MovementMode,
simulation.InputAfterPlayerUpdate, simulation.ControllerSteps, simulation.PhysicsSteps, simulation.EffectSteps);
ObserveControl("player_step");
scooterFixture?.Received(input);
grapplerFixture?.Received(input);
rocketFixture?.Received(input);
@@ -1324,7 +1354,7 @@ internal static class Program
}
mechanisms?.Step(FixedStepClock.Step);
foreach (var patrol in patrols.Values) patrol.Step(FixedStepClock.Step);
director?.Step(FixedStepClock.Step);
StepObservedDirector(FixedStepClock.Step);
objectivePanel?.Key(keys.Contains(18) && director is { Controlled: true, InventoryEnabled: true });
objectivePanel?.Step(FixedStepClock.Step, director?.ControlledTag != null, practice.Active);
var switched = director is { Controlled: false } && !simulation.Noclip
@@ -1525,8 +1555,8 @@ internal static class Program
&& simulation.Energy < 20 && Vector2.Distance(new(simulation.ActorRoot.X, simulation.ActorRoot.Z), new(controlEntry.X, controlEntry.Z)) < .001f;
CutsceneIdleSmoke.LandWhileLocked(playerBinding!, director!, simulation!);
sceneAudio?.StopSpeech();
director!.Skip();
CutsceneIdleSmoke.VerifyReturn(playerBinding!, director, simulation!);
SkipObservedDialogue("control-phase-fixture");
CutsceneIdleSmoke.VerifyReturn(playerBinding!, director!, simulation!);
controlControllers = simulation!.ControllerSteps;
}
if (controlPhaseSmoke && count == 30)
@@ -1873,6 +1903,7 @@ internal static class Program
continuation = new(nextArgs.ToArray(), (debugCheckpointToLoad ?? interactions!.Snapshot()) with {
StartPoint = transitionRequest.StartPoint, StoredVector = debugCheckpointToLoad?.StoredVector ?? returnRoot }, transitionRequest.Returning,
debugCheckpointToLoad == null ? simulation?.CaptureLevelState(interactions!.Inventory.SelectedGadget) : null);
observation?.RecordTransition(level!.Code, transitionRequest.LevelFile, simulation?.Steps ?? 0, transitionRequest.Returning);
Console.WriteLine($"Loading original destination: {transitionRequest.LevelFile}, start={transitionRequest.StartPoint}; fade behavior remains unreconstructed.");
}
if (walkSmoke && !simulation!.SmokePassed) return 2;