Repository navigation
Add the StateWriter seam (T12a) - #13
Merged
Merged
Conversation
Guest auth's whole point is that a player keeps the same account across restarts, which needs player_identifier persisted. There is no portable way to write a file on every platform this SDK targets, so the SDK defines the interface and ships two implementations. Typed accessors rather than strings-only: a store that only speaks strings forces every adopter to write the same conversions and get the same rounding wrong. GetUInt64 is separate from GetInt64 because player_id is a uint64 on the wire and a valid id above INT64_MAX must round-trip exactly (D4). A getter returns false for a type mismatch rather than converting, because guessing is how a corrupt store becomes a wrong player id. FileStateWriter writes a flat ype:key=value file. Deliberately not JSON: a corrupt save would become a parse error the adopter has to handle, where a flat format degrades to skipping the bad line. Not atomic across a crash - documented rather than hidden, because write-temp-then-rename is not portable to every target. Found while testing: GetStateWriter returned the expired handle when an injected writer died, leaving the client with nothing to persist to. The default is now always constructed and resolved at read time, not at set time. Verified by sabotage: parsing uint64 with strtoll fails four assertions on the INT64_MAX case. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Approval is blocked by an unresolved critical filesystem-I/O violation, along with moderate persistence round-trip and documentation-example issues.
3 open issues
What changed in this PR
Adds the T12a StateWriter persistence seam to the SDK, including typed state access, file-backed and null implementations, client integration, tests, and documentation.
Changes:
- Introduces public StateWriter, FileStateWriter, and NullStateWriter APIs.
- Integrates state-writer configuration, injection, and fallback behavior into Client.
- Adds persistence tests, CMake registration, core-library integration, and README guidance.
| File | Description |
|---|---|
| tests/test_state_writer.cpp | Adds tests for typed round trips, persistence, corruption handling, null behavior, and client integration. |
| tests/CMakeLists.txt | Registers the StateWriter test executable and its CTest target. |
| src/core/state_writer.cpp | Implements null storage, typed accessors, flat-file serialization, loading, validation, and persistence. |
| src/core/client.cpp | Wires default and injected state writers into client construction and runtime lookup. |
| README.md | Documents StateWriter configuration, persistence behavior, typed accessors, and usage. |
| include/lootlocker/state_writer.h | Defines the public typed StateWriter interface and built-in file-backed and null implementations. |
| include/lootlocker/client.h | Exposes client state-writer replacement and retrieval APIs. |
| CMakeLists.txt | Adds the StateWriter implementation to the core library target. |
Review effort: Lite
kirre-bylund
commented
Nov 25, 2026
Owner findings: - StateWriter is now IStateWriter, following the interface pattern IHttpTransport already uses. The two implementations keep their names: FileStateWriter and NullStateWriter are concrete classes, not interfaces. - The SetStateWriter comment claimed swapping mid-game was a legitimate thing to do. It is not - a game that does it finds the new writer knows nothing the old one stored. The behaviour is fine; the comment was wrong. Copilot findings: - The default FileStateWriter was constructed eagerly in the Client constructor, which reads lootlocker-state.txt on every client and contradicts the no-implicit-config non-negotiable (D9). It is now constructed lazily, on first use, so a client that never persists performs no file I/O. - The percent-g conversion is governed by the process-wide LC_NUMERIC locale, so a comma-decimal locale wrote 0,1 and GetDouble's strtod could not read it back under a different locale. A persisted format has to be locale-independent, so the separator is normalised to a dot on write. Same bug as QueryParameters, and it recurred because the two formatters were written separately. - The README example assigned a temporary shared_ptr to a weak_ptr, so it expired before the Client was constructed and the SDK silently fell back to the file default. The example now names an owning variable. The laziness test needed two attempts: the first version could not distinguish eager from lazy construction, because FileStateWriter's constructor reads the file rather than creating it. It now deletes the probe between construction and first use, which does distinguish them. Verified by sabotage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Implements T12a from
specs-library/specs/discipline/sdk/cpp-sdk/tasks.md.What this adds
include/lootlocker/state_writer.h— theStateWriterinterface,NullStateWriterandFileStateWriter.src/core/state_writer.cpp— the file format and the null implementation.Client::SetStateWriter/GetStateWriter, and the default wiring inConfig.tests/test_state_writer.cpp— 17 test functions.Verification
The task's three checks, all covered:
TestClientWorksWithNullWriterasserts the client is valid and nothing was written.uint64aboveINT64_MAXround-trips exactly — theplayer_idcase from D4. Verified by sabotage: parsing withstrtollinstead ofstrtoullfails four assertions.15/15 CTest targets pass with curl, 14/14 in the
-DLOOTLOCKER_MODULE_ONLY=coreconfiguration. Include boundary clean.Design notes
Typed accessors, not strings-only. A store that only speaks strings forces every adopter to write the same conversions and get the same rounding wrong — and
player_idis exactly where that bites.A getter returns
falsefor a type mismatch rather than converting. A key written as a string is not readable as an integer. Guessing is how a corrupt store becomes a wrong player id.FileStateWriteris deliberately not JSON. The SDK has a parser, but using it here would turn a corrupt save into a parse error the adopter has to handle. A flattype:key=valueformat degrades to skipping the bad line, which is tested.Not atomic across a crash. A write truncates and rewrites, so a power loss mid-write loses the file. The cost is one re-authentication, and write-temp-then-rename is not portable to every platform this SDK targets. Documented on the class rather than hidden.
A bug found while testing
GetStateWriterreturned the expired handle when an injected writer died, leaving the client with nothing to persist to — the opposite of what a default is for. The default is now always constructed and resolved at read time rather than at set time.TestSetStateWriterFallsBackWhenEmptycovers it.Note on scope
This is the seam only. T12b — the object that knows which keys to write — lands with T14, because the fields it persists are the ones T14 first produces.