Recursive AI

Limit live capture memory in OneScope

OneScope is a free, open-source viewer for simulation recordings, with a desktop window or command-line control, from Recursive AI. MIT or Apache-2.0.

OneScope stops recording when the next record would exceed the budget (256 MiB by default), but the simulation keeps running. The viewer keeps the first steps after it attaches, not the latest: this isn’t a rolling buffer.

Terminal
dotnet add package RecursiveAI.OneScope.Attach

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

The viewer attaches to a running simulation through a probe socket you can find with ProbeFinder.List, and modules are the live readers for its data.

C#
// socket: probe path; modules: live readers.
using var session = OneScope.Attach
    .LiveSession.Connect(
        socket, modules, maxRecordBytes: 1024);
 
// Each tick on the history-owning thread:
session.Pump();
if (session.Capped)
    Console.WriteLine("Capped: True");

Prints Capped: True when the next record won’t fit. Stop pumping on that branch.

Unzip the example, then dotnet run.

The full program

The download runs both ends of the connection: a OnePhysics host with a circle at (0, 4) and a OneScope viewer with a 1,024-byte budget. The first snapshot comes from the host at step zero, over a local Unix-domain socket. There is no replay file to supply. For an existing host, find its socket with ProbeFinder.List and pass the module that implements ILiveModule for that probe in modules.

The project also uses these packages to generate and send the circle’s physics records:

Terminal
dotnet add package RecursiveAI.OneScope.Probe
dotnet add package RecursiveAI.OnePhysics
dotnet add package RecursiveAI.OnePhysics.Scope
C#
using System.Buffers;
using System.Diagnostics;
using OneScope.Attach;
using OneScope.Probe;
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics.Physics2D;
using RecursiveAI.OnePhysics.Scope;
 
long budget = args.Length == 0 ? 1024 : long.Parse(args[0]);
var world = new PhysicsWorld2D();
world.AddCircle(0.5f, new Vector2D(0, 4));
var feed = new LiveFeedStream(world);
using var probe = ScopeProbe.Attach(
    new ProbeInfo("onephysics", "1.0.0", 2), new Snapshots(feed));
feed.Record = probe.Record;
using var recording = world.StartRecording(feed);
 
string socket = probe.SocketPath ?? throw new InvalidOperationException("Probe could not listen.");
var modules = new[] { new OnePhysicsModule() };
var connection = Task.Run(() => LiveSession.Connect(socket, modules, maxRecordBytes: budget));
while (!connection.IsCompleted)
{
    probe.Poll(); // Serve the initial snapshot at step zero.
    Thread.Sleep(1);
}
using var session = connection.GetAwaiter().GetResult();
 
var timer = Stopwatch.StartNew();
int hostSteps = 0;
while (!session.Capped)
{
    if (timer.Elapsed > TimeSpan.FromSeconds(5))
        throw new TimeoutException("The viewer did not reach the cap.");
    // Send one step, then wait for the viewer to keep it or hit the cap.
    world.Step(1f / 60f);
    hostSteps++;
    while (!session.Capped && session.History.EndStep < hostSteps)
    {
        if (timer.Elapsed > TimeSpan.FromSeconds(5))
            throw new TimeoutException("Missing live records.");
        session.Pump();
        Thread.Sleep(1);
    }
}
Console.WriteLine($"Budget: {session.MaxRecordBytes} bytes");
Console.WriteLine($"Kept: {session.RecordBytes} bytes");
Console.WriteLine($"Capped: {session.Capped}");
session.History.Seek(session.History.EndStep);
Console.WriteLine($"Viewer can still seek to step {session.History.CurrentStep}.");
world.Step(1f / 60f);
Console.WriteLine($"Host continued to step {hostSteps + 1}.");
 
sealed class Snapshots(LiveFeedStream feed) : ISnapshotSource
{
    public void WriteSnapshot(IBufferWriter<byte> writer) => feed.WriteSnapshot(writer);
}

dotnet run prints:

Text
Budget: 1024 bytes
Kept: 866 bytes
Capped: True
Viewer can still seek to step 1.
Host continued to step 3.

The first step fits. The next record would take the total over 1,024 bytes, so it isn’t appended. Capped is a normal stop condition, not a read error. If no loaded live module matches the probe’s module ID, Connect throws ScopeFormatException instead.

Variations

Keep a larger prefix of the recording. The example takes an optional byte budget as its first argument. With 2,048 bytes, the viewer keeps two steps and can still seek to step 2 after disconnecting.

Terminal
dotnet run -- 2048
Text
Budget: 2048 bytes
Kept: 1728 bytes
Capped: True
Viewer can still seek to step 2.
Host continued to step 4.

The short answer

Pass a positive maxRecordBytes to LiveSession.Connect, then check Capped after Pump on the thread that owns the history. The default budget is 256 MiB. A record is charged for its payload plus 48 bytes of bookkeeping, so even an empty record counts. When adding a record would exceed the budget, OneScope rejects it and closes the connection. You can still seek through what the viewer kept, and the simulation doesn’t stop.

How large should the live recording buffer be?

Choose the budget for the viewer’s retained records, not for the entire process. RecordBytes tells you what the session has charged so far; MaxRecordBytes is its ceiling. The initial snapshot, the queue of incoming records, and history keyframes are separate from this counter. Leave room for them if you need to fit a machine’s memory budget. If keyframes are the problem, tune the history keyframe settings separately.

There’s no fixed conversion from the budget to a number of steps or seconds. The count depends on what the host puts in each record and how often it sends one. For example, 1,024 bytes keeps the first step in the program above, but a simulation whose first record costs more than that would stop with only the starting snapshot. If a record brings the total to exactly the budget it still fits; the next record is what stops the session.

Does a live recording keep the last few steps or the first few?

This cap keeps the first part of the recording, starting from the snapshot taken when you attached. It isn’t a rolling buffer: it doesn’t discard older steps to make room for new ones. That makes it useful when you want to capture from a known point and inspect what happened next. If you attach again after it stops, the new session begins from a fresh snapshot; it doesn’t fill in the gap left by the earlier disconnect.

If you drive the viewer through its shell operations, the attach operation exposes the setting as capBytes (minimum 1,024). Use sources to read live.state, live.recordBytes, and live.capBytes. A budget stop is reported as capped, with endedBecause explaining where the capture stopped. That lets you distinguish a deliberate budget stop from a closed or failed stream without guessing from the last received step.

All onescope recipes