{"record":{"id":"b333235366684efa","repo":"clockworklabs/SpacetimeDB","slug":"uuid-counter-must-be-non-negative","errorCode":null,"errorMessage":"uuid counter must be non-negative","messagePattern":"uuid counter must be non-negative","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs","lineNumber":132,"sourceCode":"    /// Thrown if <paramref name=\"randomBytes\"/> is not exactly 4 bytes long, or <paramref name=\"now\"/> is  before unix epoch.\n    /// </exception>\n    /// <returns>\n    /// A <see cref=\"Uuid\"/> `v7`.\n    /// </returns>\n    public static Uuid FromCounterV7(\n        ref int counter,\n        Timestamp now,\n        ReadOnlySpan<byte> randomBytes // must be length 4\n    )\n    {\n        if (randomBytes.Length != 4)\n        {\n            throw new ArgumentException(\"randomBytes must be exactly 4 bytes\", nameof(randomBytes));\n        }\n\n        if (counter < 0)\n        {\n            throw new InvalidOperationException(\"uuid counter must be non-negative\");\n        }\n\n        if (now.MicrosecondsSinceUnixEpoch < 0)\n        {\n            throw new ArgumentException(\"timestamp before unix epoch\", nameof(now));\n        }\n        var unixTsMs = now.MicrosecondsSinceUnixEpoch / 1_000;\n\n        // monotonic 31-bit\n        var counterVal = counter;\n        counter = (counter + 1) & 0x7FFF_FFFF;\n\n        Span<byte> bytes = stackalloc byte[16];\n\n        // unix_ts_ms (48 bits, big-endian)\n        var ts = unixTsMs & 0x0000_FFFF_FFFF_FFFFL;\n        bytes[0] = (byte)(ts >> 40);\n        bytes[1] = (byte)(ts >> 32);","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs#L114-L150","documentation":"FromCounterV7 takes a caller-owned ref counter that it masks into an unsigned 31-bit field each call, incrementing it as (counter + 1) & 0x7FFF_FFFF. The counter must therefore be non-negative on entry; a negative value throws InvalidOperationException before any UUID is produced.","triggerScenarios":"Calling FromCounterV7 with a counter initialized to -1 (a common 'not started' sentinel) or one that application logic decremented below zero.","commonSituations":"Using -1 as an uninitialized marker; sharing the counter variable with code that decrements it; porting sequence generators that start at -1.","solutions":["Initialize the counter to 0 and let FromCounterV7 be the only code that advances it","Remove any logic that assigns or decrements the counter below zero","Track 'not yet used' state with a separate bool flag instead of -1"],"exampleFix":"// before\nint counter = -1; // 'uninitialized' sentinel\nvar id = Uuid.FromCounterV7(ref counter, now, rand); // InvalidOperationException: uuid counter must be non-negative\n\n// after\nint counter = 0;\nbool used = false; // separate flag for 'not started'\nvar id = Uuid.FromCounterV7(ref counter, now, rand);","handlingStrategy":"validation","validationCode":"// Reset or reject before generating\nif (counter < 0) counter = 0; // or throw your own domain error","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize the counter to 0 and never write to it outside FromCounterV7","Use a separate bool flag for 'not started' instead of -1","Assert counter >= 0 in debug builds"],"tags":["csharp","spacetimedb","uuid","uuidv7","state"],"backgroundTag":"uuid-counter-invalid-state","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}