Skip to content

Commit 3fdbb9e

Browse files
Bring the added comments back to the prevailing style
The explanatory comments added with the reference checksum ran far longer than anything around them. ContentService.cs keeps its inline comments to a line or two and its xmldoc to a plain statement of purpose, and the new test files sat beside neighbors carrying almost no comments at all. Most of what came out was argument rather than description: six comments stated the consequence of getting the code wrong, which belongs in the pull request a reviewer reads once, not in source every later reader reads again. Three xmldoc summaries lost a second paragraph justifying the design under a heading that elsewhere only says what a thing does. The version pairing on LocalContentReference and the two comparisons behind IsPropertiesDiff keep their explanations, being the parts a reader cannot recover from the code. Also corrects "behaviour" to "behavior" and a stale reference to referenceChecksum, which has been named reference since it was bundled with its version. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent cf8a500 commit 3fdbb9e

3 files changed

Lines changed: 27 additions & 58 deletions

File tree

‎cli/cli/Services/Content/ContentService.cs‎

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,7 @@ public async Task PublishContent(AutoSnapshotType autoSnapshotType, int maxLocal
11571157
}).ToArray()
11581158
};
11591159

1160-
// The platform derives a content version from the payload it stored, and returns it per item. Index it
1161-
// so each published file can record which remote payload its local checksum now describes. The same id
1162-
// comes back once per visibility, carrying the same version.
1160+
// The platform returns the content version it assigned to each item, repeated once per visibility.
11631161
var publishedVersions = saveContentResponses
11641162
.SelectMany(response => response.content)
11651163
.GroupBy(c => c.id)
@@ -1178,8 +1176,7 @@ public async Task PublishContent(AutoSnapshotType autoSnapshotType, int maxLocal
11781176
{
11791177
ContentFile contentFile = c;
11801178

1181-
// We just uploaded these exact properties, so the local checksum is now the reference for the
1182-
// version the platform assigned to them.
1179+
// We just uploaded these properties, so our checksum describes the version the platform assigned.
11831180
if (publishedVersions.TryGetValue(contentFile.Id, out var publishedVersion))
11841181
{
11851182
contentFile.Reference = new LocalContentReference(contentFile.PropertiesChecksum, publishedVersion);
@@ -1401,10 +1398,8 @@ public async Task<ContentSyncReport> SyncLocalContent(ClientManifestJsonResponse
14011398
contentFile.Tags = JsonSerializer.SerializeToElement(c.ReferenceContent.tags);
14021399
contentFile.FetchedFromManifestUid = targetManifestUid;
14031400

1404-
// Hash the payload we just downloaded using our own canonicalization, and record that rather than
1405-
// trusting the publisher-supplied manifest checksum. The file about to be written is serialized from
1406-
// this same element, so local and reference agree by construction regardless of how the publisher
1407-
// ordered, escaped or formatted its JSON.
1401+
// Hash the downloaded payload ourselves rather than trusting the publisher's manifest checksum. The
1402+
// file we are about to write is serialized from this same element, so the two agree by construction.
14081403
contentFile.PropertiesChecksum = CalculateChecksum(in contentFile);
14091404
contentFile.Reference = new LocalContentReference(contentFile.PropertiesChecksum, c.ReferenceContent.version);
14101405
saveTasks.Add(SaveContentFile(contentFolder, contentFile, cancellationToken));
@@ -1420,8 +1415,7 @@ public async Task<ContentSyncReport> SyncLocalContent(ClientManifestJsonResponse
14201415
contentFile.Tags = JsonSerializer.SerializeToElement(c.ReferenceContent.tags);
14211416
}
14221417

1423-
// This set also contains locally modified and locally created files, whose local bytes are NOT the
1424-
// remote ones, so only record a reference for the entries that actually match the target.
1418+
// This set also holds locally modified and created files, whose bytes are not the remote ones.
14251419
if (c.ReferenceContent != null && contentFile.GetStatus() == ContentStatus.UpToDate)
14261420
{
14271421
contentFile.Reference = new LocalContentReference(contentFile.PropertiesChecksum, c.ReferenceContent.version);
@@ -1987,8 +1981,6 @@ public static IEnumerable<LocalContentManifestEntry> ContentFileToLocalContentMa
19871981

19881982
/// <summary>
19891983
/// Reads the locally derived reference off a parsed content file, if it carries one.
1990-
/// Absence is meaningful rather than exceptional: files written by an older CLI simply do not have it, and
1991-
/// resolving that here keeps every downstream reader working with a reference that is whole or not there.
19921984
/// </summary>
19931985
private static LocalContentReference? ReadReference(in JsonElement json) =>
19941986
json.TryGetProperty(ContentFile.JSON_NAME_REFERENCE, out var reference)
@@ -2053,14 +2045,8 @@ public static JsonSerializerOptions GetContentFileSerializationOptions(bool inde
20532045
{
20542046
WriteIndented = indent,
20552047
IncludeFields = true,
2056-
2057-
// Pinned deliberately. The bytes these options produce are hashed by CalculateChecksum and the
2058-
// result is published as the manifest checksum, so this is a wire format rather than a style
2059-
// preference: changing any value here re-hashes every content item and makes this CLI disagree
2060-
// with every other CLI version in the realm. JavaScriptEncoder.Default is what System.Text.Json
2061-
// used implicitly before this was written down, so naming it changes no existing checksum.
2048+
// Pinned: these bytes get hashed, so a change here re-checksums every content item in the realm.
20622049
Encoder = JavaScriptEncoder.Default,
2063-
20642050
Converters =
20652051
{
20662052
new SortedJsonElementConverter(), new SortedSnapshotConverter()
@@ -2241,10 +2227,8 @@ public struct LocalContentFiles
22412227
}
22422228

22432229
/// <summary>
2244-
/// A checksum this CLI computed over a remote content payload, paired with the platform content version that
2245-
/// says which payload it was. Neither half means anything on its own -- a checksum without its version would
2246-
/// let a stale reference mask a genuine remote change -- so the two are only ever constructed together and an
2247-
/// absent reference is represented by a null, not by empty strings.
2230+
/// A checksum this CLI computed over a remote content payload, paired with the content version identifying
2231+
/// which payload it was. The version is required: without it a stale checksum can mask a real remote change.
22482232
/// </summary>
22492233
[Serializable]
22502234
public struct LocalContentReference
@@ -2262,8 +2246,7 @@ public LocalContentReference(string checksum, string version)
22622246
}
22632247

22642248
/// <summary>
2265-
/// Reads a reference from the object a content file stores it under, yielding one only when both halves
2266-
/// are present. A file carrying half a reference is treated as carrying none.
2249+
/// Reads a reference, yielding one only when both halves are present.
22672250
/// </summary>
22682251
public static bool TryRead(in JsonElement json, out LocalContentReference reference)
22692252
{
@@ -2281,8 +2264,7 @@ public static bool TryRead(in JsonElement json, out LocalContentReference refere
22812264
}
22822265

22832266
/// <summary>
2284-
/// True when this reference was taken from the very payload <paramref name="remote"/> points at, which is
2285-
/// the only situation where comparing a local checksum against it means anything.
2267+
/// True when this reference was taken from the payload <paramref name="remote"/> points at.
22862268
/// </summary>
22872269
public bool Describes(ClientContentInfoJson remote) => Version == remote.version;
22882270
}
@@ -2312,9 +2294,9 @@ public struct ContentFile : IEquatable<ContentFile>
23122294
public string FetchedFromManifestUid;
23132295

23142296
/// <summary>
2315-
/// What the remote payload hashed to when we last synced it, as computed by this CLI rather than supplied
2316-
/// by whoever published it. Null on files written before this existed, and on files restored from a
2317-
/// snapshot, both of which fall back to comparing against the manifest checksum.
2297+
/// What the remote payload hashed to when we last synced it, computed by this CLI rather than supplied by
2298+
/// the publisher. Null on files written before this existed and on snapshot restores, which fall back to
2299+
/// the manifest checksum.
23182300
/// </summary>
23192301
[JsonPropertyName(JSON_NAME_REFERENCE)]
23202302
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
@@ -2333,10 +2315,9 @@ public ContentStatus GetStatus()
23332315

23342316
/// <summary>
23352317
/// Compares the local properties against the remote ones we last synced.
2336-
/// Prefers <see cref="Reference"/>, which both sides of the comparison canonicalized the same way, and which
2337-
/// is only usable for the remote payload it was taken from. Otherwise falls back to the publisher-supplied
2338-
/// manifest checksum, which is the historical behaviour and is only correct when the publisher happened to
2339-
/// match our serialization.
2318+
/// Prefers <see cref="Reference"/> when it describes the manifest entry in hand, since both sides of that
2319+
/// comparison were canonicalized the same way. Otherwise falls back to the publisher-supplied manifest
2320+
/// checksum, which is the historical behavior.
23402321
/// </summary>
23412322
private bool IsPropertiesDiff() => Reference.HasValue && Reference.Value.Describes(ReferenceContent)
23422323
? Reference.Value.Checksum != PropertiesChecksum
@@ -2470,8 +2451,7 @@ private static void WriteSortedJsonElement(Utf8JsonWriter writer, JsonElement el
24702451
writer.WriteStartObject();
24712452
foreach (var property in element.EnumerateObject()
24722453
.OrderBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
2473-
// OrderBy is a stable sort, so without this two keys that differ only in case would
2474-
// keep whatever order they arrived in -- exactly the instability we are removing.
2454+
// OrderBy is stable, so without this, keys differing only in case keep arrival order.
24752455
.ThenBy(p => p.Name, StringComparer.Ordinal))
24762456
{
24772457
writer.WritePropertyName(property.Name);

‎cli/tests/ContentChecksumTests/ContentChecksumTest.cs‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ namespace tests.ContentChecksumTests;
66

77
/// <summary>
88
/// Guards the canonical form that <see cref="ContentService.CalculateChecksum(string)"/> hashes.
9-
/// The resulting hash is published as the manifest checksum, so these are wire-format tests: a failure
10-
/// here means this CLI no longer agrees with content published by any other version of it.
9+
/// The hash is published as the manifest checksum, so these are wire-format tests.
1110
/// </summary>
1211
public class ContentChecksumTest
1312
{
@@ -42,8 +41,7 @@ public void Checksum_IgnoresNestedFieldOrder()
4241
[Test]
4342
public void Checksum_IsStableForKeysDifferingOnlyInCase()
4443
{
45-
// OrderBy is a stable sort, so without an Ordinal tiebreak these two inputs would sort into
46-
// different orders and hash differently despite carrying identical values.
44+
// Without an Ordinal tiebreak these sort differently despite carrying identical values.
4745
var a = ChecksumOf("""{"Damage":{"data":1},"damage":{"data":2}}""");
4846
var b = ChecksumOf("""{"damage":{"data":2},"Damage":{"data":1}}""");
4947

@@ -72,22 +70,19 @@ public void Checksum_DistinguishesDifferentValues()
7270
[Test]
7371
public void SerializationOptions_EscapeNonAsciiAsTheyAlwaysHave()
7472
{
75-
// Pinning the encoder must not have changed the bytes we hash. If this fails, every checksum in
76-
// every realm published by an older CLI has just become wrong.
73+
// Pinning the encoder must not have changed the bytes we hash.
7774
var json = JsonSerializer.Serialize(
7875
JsonSerializer.Deserialize<JsonElement>("""{"name":"Café & Co"}"""),
7976
ContentService.GetContentFileSerializationOptions(false));
8077

81-
// JavaScriptEncoder.Default escapes non-ASCII and the HTML-sensitive characters. That is not
82-
// pretty, but it is what every previously published checksum was computed over.
78+
// JavaScriptEncoder.Default escapes non-ASCII and HTML-sensitive characters, as it always has.
8379
Assert.That(json, Is.EqualTo("""{"name":"Caf\u00E9 \u0026 Co"}"""));
8480
}
8581

8682
[Test]
8783
public void Checksum_PreservesRawNumberText()
8884
{
89-
// Numbers are replayed as written rather than normalized, which keeps the float-versus-int
90-
// distinction that Unity schemas rely on. Documented here so the behaviour is deliberate.
85+
// Numbers are replayed as written, keeping the float-versus-int distinction Unity schemas rely on.
9186
var a = ChecksumOf("""{"price":{"data":1.0}}""");
9287
var b = ChecksumOf("""{"price":{"data":1}}""");
9388

‎cli/tests/ContentChecksumTests/ContentStatusTest.cs‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace tests.ContentChecksumTests;
1111

1212
/// <summary>
1313
/// Covers what <see cref="ContentFile.GetStatus"/> trusts when deciding that content was modified.
14-
/// The point of these is that a publisher supplying a checksum we cannot reproduce must not make
15-
/// value-identical content look modified forever.
1614
/// </summary>
1715
public class ContentStatusTest
1816
{
@@ -63,8 +61,7 @@ private ContentFile MakeFile(string manifestChecksum, LocalContentReference? ref
6361
[Test]
6462
public void UpToDate_WhenPublisherChecksumIsUnreproducible_ButLocalReferenceMatches()
6563
{
66-
// The reported bug: an external tool published a checksum over differently ordered JSON. The values
67-
// are identical, so this must read as clean.
64+
// The reported bug: a publisher's checksum taken over differently ordered JSON. The values match.
6865
var file = MakeFile("a-checksum-we-could-never-compute", ReferenceTo(RemoteVersion));
6966

7067
Assert.That(file.GetStatus(), Is.EqualTo(ContentStatus.UpToDate));
@@ -73,8 +70,7 @@ public void UpToDate_WhenPublisherChecksumIsUnreproducible_ButLocalReferenceMatc
7370
[Test]
7471
public void Modified_WhenTheRemotePayloadMovedOnSinceWeRecordedOurReference()
7572
{
76-
// Our locally derived reference describes the payload we downloaded. If the remote has since changed,
77-
// that reference says nothing about the new one and must not be allowed to mask a real change.
73+
// Our reference describes the payload we downloaded; it must not mask a change to a newer one.
7874
var file = MakeFile("a-checksum-we-could-never-compute", ReferenceTo("an-older-remote-version"));
7975

8076
Assert.That(file.GetStatus(), Is.EqualTo(ContentStatus.Modified));
@@ -91,7 +87,7 @@ public void Modified_WhenLocalPropertiesActuallyDiffer()
9187
[Test]
9288
public void FallsBackToPublisherChecksum_WhenFileHasNoLocalReference()
9389
{
94-
// Files written before referenceChecksum existed keep the historical comparison until their next sync.
90+
// Files written before the reference existed keep the historical comparison until their next sync.
9591
var matching = MakeFile(OurChecksum, null);
9692
var notMatching = MakeFile("a-checksum-we-could-never-compute", null);
9793

@@ -105,8 +101,7 @@ public void FallsBackToPublisherChecksum_WhenFileHasNoLocalReference()
105101
[TestCase("""{}""", TestName = "both missing")]
106102
public void ReferenceIsNotRead_WhenEitherHalfIsMissing(string referenceJson)
107103
{
108-
// Half a reference is not a reference: a checksum without its version cannot say which payload it
109-
// describes. Rejecting it here is what makes the half-populated state unrepresentable everywhere else.
104+
// Half a reference is not a reference: a checksum without its version names no payload.
110105
var json = JsonSerializer.Deserialize<JsonElement>(referenceJson);
111106

112107
Assert.That(LocalContentReference.TryRead(in json, out _), Is.False);
@@ -115,8 +110,7 @@ public void ReferenceIsNotRead_WhenEitherHalfIsMissing(string referenceJson)
115110
[Test]
116111
public void SerializedFile_NestsTheReferenceUnderOneKey()
117112
{
118-
// Pins the on-disk shape. The two halves live under one key so that a file cannot express half a
119-
// reference, which is the same invariant LocalContentReference enforces in memory.
113+
// Pins the on-disk shape: both halves under one key, so a file cannot express half a reference.
120114
var file = MakeFile(null, new LocalContentReference("the-checksum", "the-version"));
121115

122116
var json = JsonSerializer.Serialize(file, ContentService.GetContentFileSerializationOptions(false));

0 commit comments

Comments
 (0)