Skip to content

Add the StateWriter seam (T12a) - #13

Merged
kirre-bylund merged 2 commits into
devfrom
t12a-state-writer
Nov 25, 2026
Merged

kirre-bylund merged 2 commits into
devfrom
t12a-state-writer

Conversation

@kirre-bylund

Copy link
Copy Markdown
Contributor

Implements T12a from specs-library/specs/discipline/sdk/cpp-sdk/tasks.md.

What this adds

  • include/lootlocker/state_writer.h — the StateWriter interface, NullStateWriter and FileStateWriter.
  • src/core/state_writer.cpp — the file format and the null implementation.
  • Client::SetStateWriter / GetStateWriter, and the default wiring in Config.
  • tests/test_state_writer.cpp — 17 test functions.

Verification

The task's three checks, all covered:

  • Each typed accessor round-trips its type — string, int64, uint64, double and bool, including a reopen from disk.
  • The null writer persists nothing and the client still works — TestClientWorksWithNullWriter asserts the client is valid and nothing was written.
  • A uint64 above INT64_MAX round-trips exactly — the player_id case from D4. Verified by sabotage: parsing with strtoll instead of strtoull fails four assertions.

15/15 CTest targets pass with curl, 14/14 in the -DLOOTLOCKER_MODULE_ONLY=core configuration. 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_id is exactly where that bites.

A getter returns false for 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.

FileStateWriter is 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 flat type:key=value format 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

GetStateWriter returned 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. TestSetStateWriterFallsBackWhenEmpty covers 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.

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>
Copilot AI lite review requested due to automatic review settings November 25, 2026 12:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/core/client.cpp Resolved
Comment thread src/core/state_writer.cpp Resolved
Comment thread README.md Resolved
Comment thread include/lootlocker/client.h Resolved
Comment thread include/lootlocker/state_writer.h Resolved
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>
@kirre-bylund
kirre-bylund merged commit 3609459 into dev Nov 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants