fix: finalize presentation event immutability

This commit is contained in:
goose
2026-10-04 01:08:53 -04:00
parent 782fcabc08
commit e8ec8415bf
3 changed files with 92 additions and 59 deletions
+29 -55
View File
@@ -2,78 +2,52 @@ class_name PresentationEvent
extends RefCounted
## A display-only, immutable translation of one authoritative GameEvent.
## Getter calls return a fresh source snapshot so presentation code cannot mutate
## the source domain event or this adapter's captured event data.
##
## GDScript's leading underscore is only a convention: declared fields remain
## writable. The backing properties below are virtual so _set can absorb every
## write after construction.
const BACKING_PROPERTIES := [&"_source_event", &"_kind", &"_notification", &"_accent"]
## Snapshot data belongs to this class, not an instance, so direct writes cannot
## create mutable instance state. Getter calls return a fresh source snapshot.
## RefCounted teardown cannot safely call script methods, so the static store is
## intentionally process-lifetime; entries contain only frozen presentation data.
const FORMER_BACKING_PROPERTIES := [
&"_backing_values",
&"_is_initialized",
&"_source_event",
&"_kind",
&"_notification",
&"_accent",
]
var _backing_values: Dictionary[StringName, Variant] = {}
var _is_initialized := false
var _source_event: GameEvent:
set(value):
_set(&"_source_event", value)
get:
return _captured_source_snapshot()
var _kind: StringName:
set(value):
_set(&"_kind", value)
get:
return _backing_values.get(&"_kind", &"") as StringName
var _notification: String:
set(value):
_set(&"_notification", value)
get:
return _backing_values.get(&"_notification", "") as String
var _accent: Color:
set(value):
_set(&"_accent", value)
get:
return _backing_values.get(&"_accent", Color.WHITE) as Color
static var _snapshots: Dictionary[int, Dictionary] = {}
var source_event: GameEvent:
get:
return _copy_event(_source_event)
return _copy_event(_snapshot()[&"source_event"] as GameEvent)
var kind: StringName:
get:
return _kind
return _snapshot()[&"kind"] as StringName
var notification: String:
get:
return _notification
return _snapshot()[&"notification"] as String
var accent: Color:
get:
return _accent
return _snapshot()[&"accent"] as Color
func _init(source: GameEvent, presentation_kind: StringName, text: String = "", color: Color = Color.WHITE) -> void:
_source_event = _copy_event(source)
_kind = presentation_kind
_notification = text if not text.is_empty() else source.message
_accent = color
_is_initialized = true
_snapshots[get_instance_id()] = {
&"source_event": _copy_event(source),
&"kind": presentation_kind,
&"notification": text if not text.is_empty() else source.message,
&"accent": color,
}
func _set(property: StringName, value: Variant) -> bool:
if property not in BACKING_PROPERTIES:
return false
if not _is_initialized:
_backing_values[property] = value
return true
## Former fields are intentionally undeclared. _set absorbs legacy direct writes
## without creating per-instance storage or changing the captured snapshot.
func _set(property: StringName, _value: Variant) -> bool:
return property in FORMER_BACKING_PROPERTIES
func _get(property: StringName) -> Variant:
if property in BACKING_PROPERTIES:
return _backing_values.get(property)
return null
func _captured_source_snapshot() -> GameEvent:
return _copy_event(_backing_values.get(&"_source_event") as GameEvent)
func _snapshot() -> Dictionary:
return _snapshots.get(get_instance_id(), {}) as Dictionary
static func _copy_event(event: GameEvent) -> GameEvent:
return GameEvent.new(
+56
View File
@@ -0,0 +1,56 @@
# G4 final — PresentationEvent immutability architecture
## Scope
This final remediation changes only `PresentationEvent`, its focused unit coverage, and this report. No domain, gameplay, presentation routing, asset, or DCWASCII source was changed.
## Architecture
`PresentationEvent` no longer declares any mutable instance backing field:
- removed `_backing_values` and `_is_initialized`;
- removed declared `_source_event`, `_kind`, `_notification`, and `_accent` properties;
- records the complete captured event/presentation snapshot once in class-level `_snapshots`, keyed by `get_instance_id()`;
- exposes only getter APIs (`source_event`, `kind`, `notification`, and `accent`) from each instance;
- deep-copies the source `GameEvent` at capture time and returns a new copy from `source_event` on every read.
The internal snapshot table is the sole data owner. No instance field can be changed after construction to alter the mapped result.
## Legacy direct writes
Because GDScript permits direct writes to underscore-named members, `_set` explicitly absorbs every former mutable field name:
- `_kind`
- `_source_event`
- `_backing_values`
- `_is_initialized`
- `_notification`
- `_accent`
Those names are intentionally undeclared, so these writes create no per-instance state and never modify the frozen snapshot. The focused mapping test performs all four requested writes (`_kind`, `_source_event`, `_backing_values`, `_is_initialized`) and verifies the mapped kind, notification, accent, source event, and source payload are unchanged.
## Store lifecycle
A `RefCounted` pre-delete notification was tested as a cleanup mechanism. Godot invokes it after the script instance is unavailable, producing `Cannot call method 'get_instance_id' on a null value`; it is therefore not viable or safe. The final store is intentionally process-lifetime and contains only captured presentation snapshots. This avoids teardown errors while preserving the required immutable instance architecture.
## Verification
`./scripts/verify.sh` passed with Godot 4.5.0:
| Suite | Result |
| --- | ---: |
| Unit | 188 / 188 tests, 1,440 assertions |
| Integration | 11 / 11 tests, 59,470 assertions |
| E2E | 58 / 58 tests, 573 assertions |
| Visual structural capture | passed |
Visual metrics:
- `artifacts/visual-check.png`: `dungeon_non_black_pixels=489308`, `visible_pixels=17524`, `explored_pixels=10438`, `explored_luminance=0.012`, `unknown_pixels=365325`, `unknown_luminance=0.001`.
- `artifacts/visual-check-exit.png`: `dungeon_non_black_pixels=489252`, `visible_pixels=17404`, `explored_pixels=17146`, `explored_luminance=0.012`, `unknown_pixels=439497`, `unknown_luminance=0.001`.
The existing GUT invalid-UID and engine resource-cleanup diagnostics were emitted, but there were no project script errors, parse errors, test failures, or visual verification failures.
## Delivery
A local commit is created for this final remediation. No push and no pull request are created.
+7 -4
View File
@@ -55,20 +55,23 @@ func _wait_for_presented_events(observed: Array[GameEvent], expected_count: int)
while observed.size() < expected_count and Time.get_ticks_msec() < deadline:
await get_tree().process_frame
func test_direct_backing_field_writes_are_absorbed_after_mapping() -> void:
func test_direct_former_backing_field_writes_are_absorbed_after_mapping() -> void:
var presenter := EventPresenter.new()
add_child_autofree(presenter)
var source := GameEvent.new(7, 2, &"damage_dealt", {"damage": 3}, "Goblin takes 3 damage.", &"player", &"goblin", Vector2i(6, 12))
var mapped: PresentationEvent = presenter.map_event(source)
var replacement := GameEvent.new(99, 9, &"actor_died", {"damage": 99}, "Replacement.")
# Leading underscores are not private in GDScript. The adapter absorbs direct
# writes to its virtual backing properties once initialization is complete.
# Former fields are undeclared. Leading underscores are not private in
# GDScript, so every legacy direct write must be absorbed by _set.
mapped._kind = &"death"
mapped._source_event = replacement
mapped._source_event.payload["damage"] = 99
mapped._backing_values = {&"_kind": &"death"}
mapped._is_initialized = false
assert_eq(mapped.kind, &"combat")
assert_eq(mapped.notification, "Goblin takes 3 damage.")
assert_eq(mapped.accent, EventPresenter.COMBAT_COLOR)
assert_eq(mapped.source_event.kind, &"damage_dealt")
assert_eq(mapped.source_event.payload, {"damage": 3})
assert_eq(source.kind, &"damage_dealt")