diff --git a/docs/retained-session-contract.md b/docs/retained-session-contract.md
index 30ff353..99bbbc9 100644
--- a/docs/retained-session-contract.md
+++ b/docs/retained-session-contract.md
@@ -110,10 +110,21 @@ requires exact coverage from zero through the descriptor count, and then uses
the same full replay and verifier path as a complete checkpoint. Both binary
forms are independently checksummed.
-The cursor is currently an index into a complete in-memory journal. Ymir does
-not yet prune or renumber that journal; storage writes are incremental, memory
-retention is not. Compaction requires a generation/base-sequence contract and
-must not be inferred from a host acknowledging a write.
+The cursor is an index within one session generation. Bounded persistence uses
+an explicit generation rollover, never an inferred acknowledgement side effect.
+`TryCreateCompactedPersistenceBaseline` may create a replacement session only
+when the current journal is empty or ends in an accepted Step and Box3D reports
+no active contact episodes. The replacement starts from the current body
+projection and time with a fresh session generation, revision zero, an empty
+receipt ledger, and an empty journal. The host atomically swaps ownership and
+then disposes the prior session.
+
+Active contacts, pending forces, or other unstepped mutations refuse rollover.
+Those sessions retain replay reconstruction until a later quiescent boundary.
+Ymir does not synthesize contact closure, serialize private Box3D handles, or
+pretend a body snapshot can preserve solver contact state. Old generation
+journals become audit history after the host commits the replacement boundary;
+they are not inputs to the new generation's restore.
## Forbidden Writers
@@ -162,7 +173,8 @@ is derived process state: it never appears in a public fact or checkpoint.
and zone: done.
5. Incremental journal chunks and bounded resume descriptors: done. Aetheria
stores them in a daemon-private CultCache; complete history does not ride in
- public product frames. Journal compaction remains separate work.
+ public product frames. Quiescent generation rollover provides the compaction
+ primitive; host adoption and old-generation retention policy are separate.
6. For optional standalone hosting, add a session registry, generation-bearing
routed commands, idempotent Create, durable receipts, and CultMesh lowering.
diff --git a/src/Ymir.Core/YmirSession.cs b/src/Ymir.Core/YmirSession.cs
index 05b1a98..f1d89e0 100644
--- a/src/Ymir.Core/YmirSession.cs
+++ b/src/Ymir.Core/YmirSession.cs
@@ -148,6 +148,48 @@ public sealed class YmirSession : IDisposable
}
}
+ ///
+ /// Gets the number of commands retained in the current persistence generation.
+ ///
+ public long PersistenceJournalEntryCount
+ {
+ get
+ {
+ lock (_gate)
+ {
+ ObjectDisposedException.ThrowIf(_disposed, this);
+ return _journal.Count;
+ }
+ }
+ }
+
+ ///
+ /// Creates a fresh persistence generation from the current Box3D projection when
+ /// no contact or unstepped mutation state would be lost. The caller owns the
+ /// returned session and remains responsible for disposing this session after swap.
+ ///
+ public bool TryCreateCompactedPersistenceBaseline(out YmirSession? compacted)
+ {
+ lock (_gate)
+ {
+ ObjectDisposedException.ThrowIf(_disposed, this);
+ if (_faulted)
+ throw new InvalidOperationException("A faulted Ymir session cannot compact persistence.");
+ if (_episodesByNativeContact.Count > 0 ||
+ (_journal.Count > 0 && _journal[^1] is not YmirStepSessionJournalEntry))
+ {
+ compacted = null;
+ return false;
+ }
+
+ compacted = new YmirSession(new YmirSessionCreateRequest(
+ _sessionId,
+ _bodies.Values.OrderBy(body => body.Id, StringComparer.Ordinal).ToArray(),
+ _time));
+ return true;
+ }
+ }
+
public YmirSessionCheckpoint Checkpoint()
{
lock (_gate)
diff --git a/tests/Ymir.Tests/YmirSessionTests.cs b/tests/Ymir.Tests/YmirSessionTests.cs
index 3eda6a1..2277551 100644
--- a/tests/Ymir.Tests/YmirSessionTests.cs
+++ b/tests/Ymir.Tests/YmirSessionTests.cs
@@ -198,6 +198,49 @@ public sealed class YmirSessionTests
Assert.Throws(() => YmirSessionCheckpointCodec.DecodeResumeDescriptor(corruptDescriptor));
}
+ [Fact]
+ public void QuiescentSessionCanRollToAnEmptyPersistenceGeneration()
+ {
+ using var original = YmirSession.Create(new YmirSessionCreateRequest(
+ "compact-quiescent", [Body("ship", velocityX: 2.0f)]));
+ original.Step(Step("step-1", 0, 0.1f));
+ original.Step(Step("step-2", 1, 0.1f));
+
+ Assert.True(original.TryCreateCompactedPersistenceBaseline(out var compacted));
+ using (compacted!)
+ {
+ var baseline = compacted!;
+ Assert.NotEqual(original.Info.SessionGeneration, baseline.Info.SessionGeneration);
+ Assert.Equal(0, baseline.Info.Revision);
+ Assert.Equal(0, baseline.PersistenceJournalEntryCount);
+ AssertWorldEqual(original.Snapshot(), baseline.Snapshot());
+
+ var capture = baseline.CapturePersistence(0);
+ Assert.Null(capture.JournalChunk);
+ Assert.Equal(0, capture.ResumeDescriptor.JournalEntryCount);
+ using var restored = YmirSession.Restore(capture.ResumeDescriptor, []);
+ AssertWorldEqual(baseline.Snapshot(), restored.Snapshot());
+ }
+ }
+
+ [Fact]
+ public void PersistenceRolloverRefusesActiveContactsAndUnsteppedMutations()
+ {
+ using var touching = YmirSession.Create(new YmirSessionCreateRequest(
+ "compact-contact",
+ [Body("ship", positionX: 0.0f, isStatic: true), Body("pickup", positionX: 1.5f)]));
+ touching.Step(Step("begin", 0, 0.01f));
+ Assert.False(touching.TryCreateCompactedPersistenceBaseline(out var contactBaseline));
+ Assert.Null(contactBaseline);
+
+ using var pendingForce = YmirSession.Create(new YmirSessionCreateRequest(
+ "compact-force", [Body("ship")]));
+ pendingForce.ApplyForce(new YmirApplyForceCommand(
+ Header("force", 0), "ship", new Vec2(10.0f, 0.0f)));
+ Assert.False(pendingForce.TryCreateCompactedPersistenceBaseline(out var forceBaseline));
+ Assert.Null(forceBaseline);
+ }
+
[Fact]
public void RemoveAndRecreateChangesBodyGeneration()
{
@@ -430,4 +473,11 @@ public sealed class YmirSessionTests
isStatic,
0.0f,
new Vec2(0.0f, 1.0f));
+
+ private static void AssertWorldEqual(YmirWorld expected, YmirWorld actual)
+ {
+ Assert.Equal(expected.Time, actual.Time);
+ Assert.Equal(expected.Bodies, actual.Bodies);
+ Assert.Equal(expected.Fields, actual.Fields);
+ }
}