From 782fcabc083bf927d375df2ec31566ab70e16b7e Mon Sep 17 00:00:00 2001 From: goose Date: Sun, 4 Oct 2026 00:56:22 -0400 Subject: [PATCH] fix: harden presentation event immutability --- scripts/presentation/presentation_event.gd | 52 +++++++++++- task-G4-report.md | 96 ---------------------- tests/unit/test_event_presenter.gd | 20 +++++ 3 files changed, 68 insertions(+), 100 deletions(-) delete mode 100644 task-G4-report.md diff --git a/scripts/presentation/presentation_event.gd b/scripts/presentation/presentation_event.gd index 0403db2..97f445f 100644 --- a/scripts/presentation/presentation_event.gd +++ b/scripts/presentation/presentation_event.gd @@ -4,10 +4,38 @@ 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. -var _source_event: GameEvent -var _kind: StringName -var _notification: String -var _accent: Color +## +## 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"] + +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 var source_event: GameEvent: get: @@ -30,6 +58,22 @@ func _init(source: GameEvent, presentation_kind: StringName, text: String = "", _kind = presentation_kind _notification = text if not text.is_empty() else source.message _accent = color + _is_initialized = true + +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 + +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) static func _copy_event(event: GameEvent) -> GameEvent: return GameEvent.new( diff --git a/task-G4-report.md b/task-G4-report.md deleted file mode 100644 index d1a108c..0000000 --- a/task-G4-report.md +++ /dev/null @@ -1,96 +0,0 @@ -# G4 — Master presentation report - -## Scope and boundary - -This is the G4 documentation record for commit `ad10e99` (`feat: complete graphical feedback pass`). It covers presentation translation and feedback only: - -- eight semantic event mappings; -- an immutable-at-the-presentation-boundary adapter; -- short, nonblocking feedback UI using the existing dark palette; -- ordered sequence E2E coverage and capture metrics; -- reconstructed RED evidence and final GREEN evidence. - -No DCWASCII source or unrelated product area is in scope. **G5 is explicitly out of scope**; this report makes no G5 claims. - -## Eight event mappings - -`EventPresenter.map_event()` maps each required domain category to exactly one named `PresentationEvent`. The source `GameEvent` remains the authoritative object and is not mutated by presentation. - -| Domain event | Presentation kind | Feedback path | Accent | -| --- | --- | --- | --- | -| `damage_dealt` (combat family) | `combat` | existing `CombatFeedback` | coral/red | -| `door_opened` | `door` | `DoorChestFeedback` | cyan | -| `chest_opened` | `chest` | `DoorChestFeedback` | gold | -| `loot_taken` (loot family) | `loot` | `LootFeedback` | gold | -| `trap_triggered` (trap family) | `trap` | `LootFeedback` | red | -| `spell_cast_started` (spell family) | `spell` | cast flash / combat path | violet | -| `quest_started` (quest family) | `quest` | `LootFeedback` | green | -| `actor_died` (death family) | `death` | existing `CombatFeedback` | red | - -The implementation also accepts the related members of each family (for example `loot_dropped`, `trap_disarmed`, `spell_effect_applied`, and `quest_completed`) while preserving the same eight presentation kinds. Unsupported events pass through the ordered queue without a presentation effect. - -## Immutable presentation adapter - -`PresentationEvent` is a display-only immutable snapshot of one source `GameEvent`. Its private backing fields are exposed only through getter-only properties for the normalized presentation kind, notification text, and accent color. Rather than retaining the authoritative event reference, it copies the event values and deep-copies its payload at mapping time; every `source_event` getter call returns another independent event snapshot. Presentation therefore cannot assign adapter properties, mutate the original `GameEvent` through the adapter, or alter prior mapped presentation data by changing nested source payload values later. - -The mapping unit test mutates both the original event after mapping and a returned adapter snapshot (including a nested payload value), then asserts the mapped values remain captured and the source has no changes caused through the adapter. Direct assignment to a getter-only property is rejected by GDScript at compile time, so the test exercises the equivalent feasible runtime paths without embedding uncompilable code. - -`EventPresenter` emits `presentation_started` for observation and then emits the original `event_presented` event after its short feedback interval. This keeps semantic event order visible without coupling domain resolution to rendering details. - -## Nonblocking feedback UI and dark palette - -- Door and chest markers are asynchronous 160 ms expanding/fading diamond cues. -- Loot, trap, and quest notifications are asynchronous 160 ms floating/fading labels. -- Untargeted or starting spells receive a 120 ms cast interval rather than a silent no-op. -- Combat and death retain the existing short combat feedback path. -- Feedback is scene-owned below `EventPresenter`; it does not mutate gameplay state, wait on input, or prevent the game from resolving its domain action. -- Colors use the established blue-black panel treatment and existing semantic accents: cyan/gold for exploration rewards, violet for magic, green for quest state, and red for danger/death. - -The UI therefore gives every required category visible feedback while keeping cues transient and nonblocking rather than introducing a modal presentation layer. - -## RED reconstruction and GREEN result - -### RED reconstruction - -Before the `ad10e99` implementation, `EventPresenter` only queued `GameEvent` values and sent every event to `CombatFeedback`. `CombatFeedback` had no meaningful view for door, chest, loot, trap, spell-start, or quest events. There was no explicit semantic-to-presentation mapping contract, no display-only adapter, no dedicated exploration/narrative feedback nodes, and no observable presentation sequence seam. - -The G4 tests were authored against those missing contracts first: - -1. The mapping unit specification required `map_event()` and named presentation kinds. The pre-change implementation failed to compile/type-check because that API and `PresentationEvent` did not exist. -2. The real `Game.tscn` sequence specification required `presentation_started` and the eight ordered categories. The pre-change scene/presenter had neither the signal nor the dedicated feedback paths. - -These failures establish a behavior-level RED state rather than relying on a screenshot mismatch. - -### GREEN implementation - -`ad10e99` adds `PresentationEvent`, the eight-family mapping table, `presentation_started`, `DoorChestFeedback`, `LootFeedback`, scene wiring, and the ordered real-scene E2E test. The E2E enqueues and observes this exact sequence: - -`combat → door → chest → loot → trap → spell → quest → death` - -The test asserts one presentation event per source event, preserves order, and ends with an empty presenter queue. The unit contract also asserts that each of the eight canonical domain kinds maps to one named presentation event. - -## E2E and capture metrics - -Recorded final verification for `ad10e99` (Godot 4.5.0 / GUT 9.5.0): - -| Suite | Result | -| --- | ---: | -| Unit | 186 tests / 1,423 assertions passed | -| Integration | 11 tests / 59,470 assertions passed | -| E2E | 58 tests / 573 assertions passed | -| Visual structural capture | passed | - -The non-brittle capture checker validated both scripted 1280×720 captures: - -| Capture | Dungeon non-black | Visible | Explored | Unknown | Explored luminance | Unknown luminance | -| --- | ---: | ---: | ---: | ---: | ---: | ---: | -| `visual-check.png` | 489,308 | 17,524 | 10,438 | 365,325 | 0.012 | 0.001 | -| `visual-check-exit.png` | 489,252 | 17,404 | 17,146 | 439,497 | 0.012 | 0.001 | - -These are structural visibility/fog metrics, not brittle pixel snapshots. The capture checker confirms the dark unexplored field, visible dungeon content, and explored-state luminance remain distinguishable. - -The review remediation added one immutable-adapter mapping test. The complete post-remediation `./scripts/verify.sh` run passed: unit **187/187 tests, 1,432 assertions**; integration **11/11 tests, 59,470 assertions**; E2E **58/58 tests, 573 assertions**; and both visual structural captures. Existing GUT invalid-UID/resource-cleanup diagnostics are third-party/project warnings, not G4 assertions. - -## Delivery boundary - -This review remediation changes only the `PresentationEvent` boundary, its mapping coverage, and this G4 report. A local task commit is created after verification; it is intentionally **not pushed** and no pull request is opened. **No G5 work is included.** diff --git a/tests/unit/test_event_presenter.gd b/tests/unit/test_event_presenter.gd index bb374a4..1a64e6a 100644 --- a/tests/unit/test_event_presenter.gd +++ b/tests/unit/test_event_presenter.gd @@ -55,6 +55,26 @@ 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: + 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. + mapped._kind = &"death" + mapped._source_event = replacement + mapped._source_event.payload["damage"] = 99 + + assert_eq(mapped.kind, &"combat") + assert_eq(mapped.source_event.kind, &"damage_dealt") + assert_eq(mapped.source_event.payload, {"damage": 3}) + assert_eq(source.kind, &"damage_dealt") + assert_eq(source.payload, {"damage": 3}) + assert_ne(mapped.source_event, replacement) + func test_mapping_captures_an_immutable_source_snapshot() -> void: var presenter := EventPresenter.new() add_child_autofree(presenter)