core: write only new history and share files on Windows
The store rewrote all of history.json, 19 MB here, on every 60 s refresh. It now appends each refresh's changes to a journal and compacts into monthly files and a state checkpoint at 8 MiB, so an idle refresh writes only snapshot.json. Bucket updates are keyed, unchanged limit samples are kept at most hourly, Codex replays are bounded by session totals, and Claude dedupe keys expire after 60 days. Readers of atomically replaced files allow delete sharing, and the replace retries briefly, so the Stream Deck plugin reading snapshot.json cannot make a refresh fail. Claude's <synthetic> entries are skipped, and repeated diagnostics fold into one entry with a count. Refs #1
This commit is contained in:
@@ -45,10 +45,16 @@ public sealed class IncrementalScanner(HistoryStore history, DiagnosticLog diagn
|
||||
{
|
||||
try
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var info = new FileInfo(path);
|
||||
var cursor = history.Cursor(path);
|
||||
var length = info.Length;
|
||||
var lastWrite = info.LastWriteTimeUtc.Ticks;
|
||||
var creation = info.CreationTimeUtc.Ticks;
|
||||
if (cursor.ObservedLength == length && cursor.LastWriteTicks == lastWrite && cursor.CreationTicks == creation)
|
||||
return 0;
|
||||
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete,
|
||||
65536, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
var cursor = history.Cursor(path);
|
||||
var creation = File.GetCreationTimeUtc(path).Ticks;
|
||||
if (stream.Length < cursor.Offset || cursor.CreationTicks != creation ||
|
||||
cursor.PrefixHash != Fingerprint(stream, 0, cursor.PrefixLength) ||
|
||||
cursor.TailHash != Fingerprint(stream, Math.Max(0, cursor.Offset - 256), (int)Math.Min(256, cursor.Offset)))
|
||||
@@ -67,14 +73,17 @@ public sealed class IncrementalScanner(HistoryStore history, DiagnosticLog diagn
|
||||
int read;
|
||||
while ((read = await stream.ReadAsync(readBuffer, cancellationToken)) > 0)
|
||||
{
|
||||
for (var i = 0; i < read; i++)
|
||||
var start = 0;
|
||||
while (start < read)
|
||||
{
|
||||
var newline = readBuffer.AsSpan(start, read - start).IndexOf((byte)'\n');
|
||||
var segmentLength = newline < 0 ? read - start : newline;
|
||||
line.Write(readBuffer.AsSpan(start, segmentLength));
|
||||
consumed += segmentLength;
|
||||
start += segmentLength;
|
||||
if (newline < 0) break;
|
||||
consumed++;
|
||||
if (readBuffer[i] != (byte)'\n')
|
||||
{
|
||||
line.WriteByte(readBuffer[i]);
|
||||
continue;
|
||||
}
|
||||
start++;
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
// Source entries can contain large tool results. An output-capture bound
|
||||
// belongs to child processes, not to valid transcript records (D3).
|
||||
@@ -108,6 +117,9 @@ public sealed class IncrementalScanner(HistoryStore history, DiagnosticLog diagn
|
||||
cursor.PrefixLength = (int)Math.Min(256, cursor.Offset);
|
||||
cursor.PrefixHash = Fingerprint(stream, 0, cursor.PrefixLength);
|
||||
cursor.TailHash = Fingerprint(stream, Math.Max(0, cursor.Offset - 256), (int)Math.Min(256, cursor.Offset));
|
||||
cursor.ObservedLength = length;
|
||||
cursor.LastWriteTicks = lastWrite;
|
||||
history.CursorChanged(path);
|
||||
return added;
|
||||
}
|
||||
catch (Exception e) when (e is IOException or UnauthorizedAccessException)
|
||||
|
||||
Reference in New Issue
Block a user