Files
poli0981andClaude Fable 5.2 e0bfbe6edf feat(recording): the ledger can hold Advanced metrics - schema 0023 (two tables, no column on sessions), adv.* series with NaN for a gap, two .partial chunks, and an unknown chunk is skipped (Advanced PR-4, D102/D106) (#390)
Produced by nothing yet: no Agent code composes the advanced reader, so no
session stores any of it and nothing a user sees or records changes.

- Schema 0023, the first CREATE TABLE since 0001: machine_profiles (the
  PC's facts, one row per distinct set, by the SHA-256 of their JSON) and
  session_advanced (one row per session recorded with the option on - its
  facts, each source's state, its series' statistics and directory). No
  column on sessions: every read of that row, the JSON export and the bug
  bundle carry all of it. A session recorded with the option off has no
  row, and that absence is how the App will say so.
- The advanced series are rows of sensor_blobs under names that start with
  adv., with NaN - not -1 - for a tick with no reading: a -12 V rail is
  negative, and the sensor library is read every fifth tick. A second
  encoder, ISeriesCodec.EncodeSparseFloat32, allows NaN and still refuses
  an infinity; EncodeFloat32 still throws on a NaN. FindSensorsAsync, which
  every summary, chart and export reads, leaves the adv. rows out;
  FindAdvancedSensorsAsync and FindAdvancedAsync read them.
- Application.Advanced.AdvancedJson: the one stored form. Writing is
  strict, reading is tolerant, and only a fact whose catalog entry is read
  may be written (D104 held at the door of the ledger).
- AdvancedSessionBuilder, both tiers: one series per advanced reading,
  aligned to t_ms; at most 256 a session, the rest counted. With no reading
  and no context it builds nothing, and a session is byte for byte what it
  was.
- TelemetrySample.Advanced, and the poller's optional IAdvancedHardware:
  held from Start to Dispose, each of its ticks attached to one sample and
  never repeated on the next.
- The .partial: chunk 9 (the readings of the sensor chunk before it, with
  their own names) and chunk 10 (the session's context), written only when
  there is something to write; a recovered session keeps both. D106: a
  chunk of a type the reader does not know, whose length and checksum are
  right, is skipped where it ended the read - so a build one release older
  no longer loses everything after the first new chunk.
- LedgerDatabaseTests' rewind helper undoes a script's CREATE TABLE and
  CREATE INDEX as well as its ADD COLUMN: 0023 would otherwise have failed
  every test that rewinds a ledger.
- Measured: one hour of 58 series shaped like the dev box's reader is 816
  KiB raw and 189 KiB stored (NFR-5 allows 3 MB an hour).
- docs: 06_DATA_MODEL (schema 0023, the NaN rule, chunks 9 and 10, D106),
  18_GPU_VENDOR_APIS, CLAUDE.md, CHANGELOG.

Hook overhead: not applicable - nothing in code loaded into a game
changes.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 09:37:53 +00:00

118 lines
3.9 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FrameLedger.Application.Recording;
using FrameLedger.Application.Telemetry;
using FrameLedger.Shared;
namespace FrameLedger.Application.Tests.Recording;
/// <summary>An in-memory <see cref="IPartialSessionStore"/>: what a writer appended is what a reader gets back.</summary>
internal sealed class FakePartialSessionStore : IPartialSessionStore
{
public Dictionary<Guid, Entry> Files { get; } = [];
/// <summary>Every entry ever created, kept after deletion so a test can read what was written.</summary>
public Dictionary<Guid, Entry> Created { get; } = [];
public List<Guid> Deleted { get; } = [];
public IPartialSessionWriter Create(PartialHeader header)
{
var entry = new Entry(header);
Files[header.SessionGuid] = entry;
Created[header.SessionGuid] = entry;
return entry;
}
public IReadOnlyList<Guid> ListPending() => ListFailure is { } failure ? throw failure : [.. Files.Keys];
public PartialSession? Read(Guid sessionGuid) => Files.TryGetValue(sessionGuid, out Entry? e) ? e.ToSession() : null;
public void Delete(Guid sessionGuid)
{
Deleted.Add(sessionGuid);
Files.Remove(sessionGuid);
}
public IReadOnlySet<long> PendingGameIds() => Files.Values.Select(static e => e.Header.GameId).ToHashSet();
/// <summary>Files set aside by recovery, out of <see cref="ListPending"/> but not deleted.</summary>
public List<Guid> Quarantined { get; } = [];
/// <summary>When set, <see cref="ListPending"/> throws it (a folder recovery cannot read).</summary>
public Exception? ListFailure { get; set; }
public void Quarantine(Guid sessionGuid)
{
Quarantined.Add(sessionGuid);
Files.Remove(sessionGuid);
}
internal sealed class Entry(PartialHeader header) : IPartialSessionWriter
{
public PartialHeader Header { get; } = header;
public List<FlFrameRecord> Records { get; } = [];
public List<int> Gaps { get; } = [];
public List<TelemetrySample> Sensors { get; } = [];
public List<long> Touches { get; } = [];
public List<PartialNote> Notes { get; } = [];
public PartialTick? LastTick { get; private set; }
public int Ticks { get; private set; }
public bool Disposed { get; private set; }
public bool Truncated { get; set; }
public void AppendRecords(long firstOrdinal, ReadOnlySpan<FlFrameRecord> records)
{
if (firstOrdinal != Records.Count)
{
throw new InvalidOperationException($"records chunk at {firstOrdinal} does not continue {Records.Count}");
}
Records.AddRange(records.ToArray());
}
public void AppendGaps(ReadOnlySpan<int> gapBefore) => Gaps.AddRange(gapBefore.ToArray());
public void AppendSensors(ReadOnlySpan<TelemetrySample> samples) => Sensors.AddRange(samples.ToArray());
public void AppendTick(PartialTick tick)
{
LastTick = tick;
Ticks++;
}
public void AppendTouches(ReadOnlySpan<long> touchQpc) => Touches.AddRange(touchQpc.ToArray());
public void AppendNote(DateTimeOffset at, string text) => Notes.Add(new PartialNote(at, text));
public List<string> AdvancedContexts { get; } = [];
public void AppendAdvancedContext(string json) => AdvancedContexts.Add(json);
public void Dispose() => Disposed = true;
public PartialSession ToSession() => new()
{
Header = Header,
Records = Records,
GapBefore = Gaps,
Sensors = Sensors,
AdvancedContextJson = AdvancedContexts.Count == 0 ? null : AdvancedContexts[^1],
TouchQpc = Touches,
Notes = Notes,
LastTick = LastTick,
Truncated = Truncated,
};
}
}