Project · Load companions

Check ledger policy and stable routing

Verify ten policy boundaries in a shared in-memory owner; real process coordination belongs to the external SQLite lab.

All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.

Program.cs

LedgerChecks.Run();

Domain/MemoryLedger.cs

public sealed class MemoryLedger {
    private readonly Dictionary<string, int> values = new(StringComparer.Ordinal);
    public int Count => values.Count;
    public int Value(string key) => values[key];
    public string Record(string key, int amount) {
        if (string.IsNullOrWhiteSpace(key) || amount < 1) throw new ArgumentException("Invalid record");
        if (values.TryGetValue(key, out int stored)) return stored == amount ? "replay" : "conflict";
        values.Add(key, amount); return "created";
    }
}
public static class KeyPartition {
    public static int For(string key) { uint hash = 2166136261; foreach (byte b in System.Text.Encoding.UTF8.GetBytes(key)) hash = unchecked((hash ^ b) * 16777619); return (int)(hash % 2); }
}

Checks/LedgerChecks.cs

public static class LedgerChecks {
    public static void Run() {
        var ledger = new MemoryLedger(); MemoryLedger borrowed = ledger; int checks = 0;
        void Check(bool condition) { if (!condition) throw new Exception("Independent ledger expectation failed"); checks++; }
        Check(KeyPartition.For("A") == 0); Check(KeyPartition.For("B") == 1);
        Check(ledger.Record("demo", 25) == "created"); Check(borrowed.Record("demo", 25) == "replay");
        Check(borrowed.Record("demo", 30) == "conflict"); Check(ledger.Count == 1);
        Check(ledger.Record("other", 4) == "created"); Check(ledger.Count == 2);
        try { ledger.Record("", 1); throw new Exception("Invalid key accepted"); } catch (ArgumentException) { checks++; }
        Check(ledger.Value("demo") == 25);
        Console.WriteLine("ledger checks:" + checks); Console.WriteLine("coordination:one memory owner");
    }
}

LedgerPolicy.csproj

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable></PropertyGroup></Project>