Repository navigation
fix(#127): deleter identity for property deletes + drop orphaned per-property metadata - #128
Merged
Merged
Conversation
…perty deletes Property deletes produced a property event with no `updatedBy` (an empty `UpdatedBy` column in the ADX `AdtPropertyEvents` table), and — when the property still carried per-property metadata — with the *previous* writer's id instead of the caller that performed the delete. Root cause: `CloudEventFactory.CreatePropertyCloudEvent` resolved `updatedBy` only from the property's own metadata (`$metadata.<property>.lastUpdatedBy`, via the JSON Patch and then from `NewValue`). A `remove` patch operation never refreshes that entry: `AgeDigitalTwinsClient` deletes the property from the twin but leaves `$metadata.<property>` behind untouched (or it never existed for properties written before `TrackLastUpdatedBy` was enabled). So the lookup found either nothing or a stale value, while the identity of the deleter was written by that same update to the twin/relationship-level `$metadata.$lastUpdatedBy`. Fix: resolve the property event's `updatedBy` in one place (`GetPropertyEventUpdatedBy`) with an explicit order of preference — the value the patch actually changed, then, for `Remove`, the twin/relationship-level `$metadata.$lastUpdatedBy`, then (non-remove only) the per-property value in `NewValue` for same-user re-updates. A property delete can therefore never report a stale writer. Tests - `CloudEventFactoryTests`: four new unit tests — delete with residual per-property metadata (must not report the stale writer), delete with no per-property metadata (the reported symptom), `trackLastUpdatedBy: false`, and a relationship property delete. - `EventsIntegrationTests`: end-to-end through the real client + logical replication (create without tracking, delete with `userId`), asserting the captured delete event carries the deleter. Verified red before the fix (`updatedBy = null`) and green after it on PG16 + AGE 1.6; the full `AgeDigitalTwins.Events.Test` suite (106 tests) passes locally against that database. Docs: CLAUDE.md and docs/advanced-topics/security.mdx now state how `updatedBy` is resolved for a property delete. Closes #127 Co-authored-by: Hermes Agent <hermes@nikoraes.local>
Member
Author
|
CI on head Local evidence behind that head, on |
`UpdateDigitalTwinAsync` removed the property from the twin but left `$metadata.<property>` behind: the metadata pass walks only the properties still present in the patched twin, so a `remove` of `/<property>` never visited the entry it invalidated. The result was a twin whose `$metadata` described a property it no longer had, still naming whoever wrote that property last — which is also why the first version of the events fix had to be told never to trust that entry for a delete. Drop `$metadata.<property>` in the same write. The rule is deliberately narrow: only top-level `remove` operations whose property is gone from the final document (a remove+add of the same property in one patch keeps its metadata, since the property still exists). Nested removals such as `/component/property` are skipped — the top-level component entry, and thus its metadata, is still valid. Reserved `$`-entries are never touched. JSON Pointer segments are decoded per RFC 6901 (`~1` then `~0`). The twin-level metadata for that same write was already correct and is unchanged: `$metadata.$lastUpdatedBy` and `$metadata.$lastUpdateTime` were written with the deleting caller before this commit — verified by running the new test against the pre-change code with only the orphan assertion removed (it passed), then with the fix in place. Tests (all through the real client against PG16 + AGE 1.6): - `UpdateDigitalTwinAsync_RemoveProperty_DropsPerPropertyMetadata_AndUpdatesTwinLevelMetadata` — creator writes two properties, a different user deletes one: the property and its metadata entry are gone, the twin-level `$lastUpdatedBy` names the deleter, `$lastUpdateTime` advances, and the surviving property's metadata still names its own writer. Proven red before the change (fails exactly on the orphan assertion) and green after. - `UpdateDigitalTwinAsync_RemoveThenReAddProperty_KeepsMetadata` — the same patch document removes and re-adds a property; its metadata survives and names the rewriting user. Full `AgeDigitalTwins.Test`: 358 passed / 8 skipped / 0 failed. Full `AgeDigitalTwins.Events.Test`: 106 passed / 0 failed — the delete event still carries the deleter, now resolved from twin-level metadata while the stored twin no longer carries a stale orphan. Docs: CLAUDE.md and docs/advanced-topics/security.mdx describe the removal behaviour and why the event resolver still refuses the per-property value for a delete (twins written before this change can carry a stale orphan). Co-authored-by: Hermes Agent <hermes@nikoraes.local>
8 tasks
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.
Problem
With ADX historization and
UpdatedByenabled (Parameters:TrackLastUpdatedBy=true), a property delete was recorded inAdtPropertyEventswith an emptyUpdatedBy— and, when the property still carried per-property metadata, with the previous writer's id rather than the caller that performed the delete.Closes #127.
Root cause
CloudEventFactory.CreatePropertyCloudEventresolvedupdatedByonly from the property's own metadata ($metadata.<property>.lastUpdatedBy) — first from the JSON Patch, then as a fallback fromNewValue.A
removepatch operation never refreshes that entry:AgeDigitalTwinsClient.UpdateDigitalTwinAsyncremoves the property from the twin but leaves$metadata.<property>untouched (the loop overpatchedTwinno longer sees the property, so it neither updates nor deletes its metadata entry);TrackLastUpdatedBywas enabled, that entry does not exist at all.So the lookup found nothing (empty column, exactly as reported) or a stale value. The identity of the deleter was written by the same update — to the twin/relationship-level
$metadata.$lastUpdatedBy— but the property-event path never read it.Fix
1. Property event
updatedByOne resolver,
GetPropertyEventUpdatedBy, with an explicit order of preference:$metadata/<property>object) — the write that produced the event;Remove: the twin/relationship-level$metadata.$lastUpdatedBywritten by that same update — the identity of the deleter. The per-property value is deliberately not used here, so a delete can never report a stale writer;NewValue, for same-user re-updates whose identical value never shows up in the patch.Behaviour when
TrackLastUpdatedBy=false, and when nouserIdwas supplied on the delete (no identity is known), is unchanged: noupdatedByin the body, empty column.2. The twin payload drops the orphaned entry
UpdateDigitalTwinAsyncnow removes$metadata.<property>for every property a patch deletes, in the same write:removeoperations whose property is absent from the final document — aremove+addof the same property in one patch keeps its metadata;/component/property) are skipped: the top-level component entry, and therefore its metadata, is still valid;$-entries are never touched; JSON Pointer segments are decoded per RFC 6901 (~1then~0).The twin-level metadata was already correct for the same write —
$metadata.$lastUpdatedByand$metadata.$lastUpdateTimewere written with the deleting caller before this commit. That is now pinned by a test rather than assumed: running the new test against the pre-change code with only the orphan assertion removed passes, so the only behaviour change is the dropped entry.Verification
Unit —
CloudEventFactoryTests(+4 tests):$metadata.<prop>.lastUpdatedBy=creator-usercreator-user(stale)deleting-usernull(empty column)deleting-usertrackLastUpdatedBy: falsecreator-user(stale)rel-deleting-userClient payload —
AgeDigitalTwins.Test/DigitalTwinsTests(+2 tests, real client against PG16 + AGE 1.6), both viaJsonObjectbecause Azure'sBasicDigitalTwinconverter rejects$lastUpdatedBy:UpdateDigitalTwinAsync_RemoveProperty_DropsPerPropertyMetadata_AndUpdatesTwinLevelMetadata— creator writesdiameter+depth; a different user deletes/diameter. Asserts the property is gone, its$metadataentry is gone, twin-level$lastUpdatedBynames the deleter,$lastUpdateTimeadvances, anddepth's metadata still names its own writer. Red before this commit (the orphaned $metadata.diameter entry must be dropped with the property) and green after.UpdateDigitalTwinAsync_RemoveThenReAddProperty_KeepsMetadata— remove + add of the same property in one patch document keeps the metadata and stamps the rewriting user.End-to-end —
EventsIntegrationTests.DeleteDigitalTwinProperty_ShouldGeneratePropertyEventWithDeletingUsergoes through the real client and logical replication: create with the non-tracking client (so the property has no per-propertylastUpdatedBy), delete withuserId: "deleting-user", capture theKonnektr.Graph.Property.Event. Proven red before the events fix (Expected: "deleting-user" / Actual: null) and green after, onghcr.io/konnektr-io/age-pgvector:16-1.6.0-standard-trixie(PG16 + AGE 1.6,CNPG_TEST=true) in-cluster.Full suites on that database:
AgeDigitalTwins.Test: 358 passed / 8 skipped / 0 failedAgeDigitalTwins.Events.Test: 106 passed / 0 failed (includes the delete e2e above against the new client behaviour)CI runs the full five-leg PG × AGE matrix (PG16 + PG18 × AGE 1.6/1.7/1.8, apache + cnpg) — green on
9007ea0. The first run hit an unrelated flake inEntityRouteAliasIntegrationTests(a model-import race insertingdtmi:com:adt:dtsample:room;1twice); rerunning the failed leg only was green.Docs
CLAUDE.mdanddocs/advanced-topics/security.mdxnow describe the removal behaviour and why the event resolver still refuses a per-property value for a delete (twins written before this change can carry a stale orphan).Notes
$metadata.<property>entries until the next property write; they are payload debris only and cannot make a delete event report a stale writer, because the resolver never reads that entry for a delete.AdtPropertyEventswith an emptyUpdatedBycannot be retroactively corrected — only new deletes carry the identity.