feat: add bounded npc memory retention
This commit is contained in:
@@ -50,6 +50,7 @@ func _run() -> void:
|
||||
contributor.position = Vector3.ZERO
|
||||
witness.position = Vector3(4.0, 0.0, 0.0)
|
||||
witness.hunger = 85.0
|
||||
listener.hunger = 20.0
|
||||
contributor.add_inventory(SimulationIds.RESOURCE_FOOD, 1.0)
|
||||
_check(
|
||||
is_equal_approx(
|
||||
@@ -64,7 +65,8 @@ func _run() -> void:
|
||||
_check(
|
||||
(
|
||||
"Known fact: %s deposited" % contributor.npc_name in inspector_label.text
|
||||
and "(witnessed)" in inspector_label.text
|
||||
and "witnessed · lasting" in inspector_label.text
|
||||
and "Memory: 1 lasting · 0 recent" in inspector_label.text
|
||||
and "Relationship: %s" % contributor.npc_name in inspector_label.text
|
||||
and "Because:" in inspector_label.text
|
||||
),
|
||||
@@ -86,7 +88,8 @@ func _run() -> void:
|
||||
_check(
|
||||
(
|
||||
"Known fact: %s deposited" % contributor.npc_name in inspector_label.text
|
||||
and "(heard from %s)" % witness.npc_name in inspector_label.text
|
||||
and "heard from %s · recent" % witness.npc_name in inspector_label.text
|
||||
and "Memory: 0 lasting · 1 recent" in inspector_label.text
|
||||
),
|
||||
"NPC inspector should identify who communicated a known fact"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
extends SceneTree
|
||||
|
||||
var failures: Array[String] = []
|
||||
var forgotten_pairs: Array[String] = []
|
||||
|
||||
|
||||
func _initialize() -> void:
|
||||
call_deferred("_run")
|
||||
|
||||
|
||||
func _run() -> void:
|
||||
var manager := _create_manager()
|
||||
var contributor: SimNPC = manager.npcs[0]
|
||||
var witness: SimNPC = manager.npcs[1]
|
||||
var lasting_listener: SimNPC = manager.npcs[2]
|
||||
var recent_listener: SimNPC = manager.npcs[3]
|
||||
for npc in manager.npcs:
|
||||
npc.position = Vector3(40.0 + npc.id * 2.0, 0.0, 0.0)
|
||||
contributor.position = Vector3.ZERO
|
||||
witness.position = Vector3(4.0, 0.0, 0.0)
|
||||
lasting_listener.position = Vector3(20.0, 0.0, 0.0)
|
||||
recent_listener.position = Vector3(22.0, 0.0, 0.0)
|
||||
witness.hunger = 20.0
|
||||
lasting_listener.hunger = 85.0
|
||||
recent_listener.hunger = 20.0
|
||||
contributor.add_inventory(SimulationIds.RESOURCE_FOOD, 2.0)
|
||||
_check(
|
||||
is_equal_approx(
|
||||
manager.economy.deposit_inventory(contributor, SimulationIds.RESOURCE_FOOD), 2.0
|
||||
),
|
||||
"Retention should begin from a real completed food deposit"
|
||||
)
|
||||
var deposit_event := _find_deposit_event(manager, contributor.id)
|
||||
_check(deposit_event != null, "The objective deposit fact should exist")
|
||||
if deposit_event == null:
|
||||
manager.free()
|
||||
_finish()
|
||||
return
|
||||
var event_id := int(deposit_event.data["event_id"])
|
||||
|
||||
_prepare_worker(witness, Vector3(20.0, 0.0, 0.0), true)
|
||||
_prepare_worker(lasting_listener, Vector3(21.0, 0.0, 0.0), false)
|
||||
manager.notify_npc_arrived(lasting_listener.id)
|
||||
_prepare_worker(recent_listener, Vector3(22.0, 0.0, 0.0), false)
|
||||
manager.notify_npc_arrived(recent_listener.id)
|
||||
_check(
|
||||
(
|
||||
manager.npc_knows_event(lasting_listener.id, event_id)
|
||||
and manager.npc_knows_event(recent_listener.id, event_id)
|
||||
),
|
||||
"The direct witness should communicate the same fact to both listeners"
|
||||
)
|
||||
var lasting_record: KnownEventStateRecord = manager.get_known_event_record(
|
||||
lasting_listener.id, event_id
|
||||
)
|
||||
var recent_record: KnownEventStateRecord = manager.get_known_event_record(
|
||||
recent_listener.id, event_id
|
||||
)
|
||||
_check(
|
||||
(
|
||||
lasting_record != null
|
||||
and recent_record != null
|
||||
and lasting_record.get_acquired_tick() == manager.tick_count
|
||||
and recent_record.get_acquired_tick() == manager.tick_count
|
||||
and (
|
||||
lasting_record.get_source_acquisition_method()
|
||||
== SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
|
||||
)
|
||||
),
|
||||
"Communicated memories should retain acquisition time and direct-source proof"
|
||||
)
|
||||
_check(
|
||||
manager.is_known_event_lasting(lasting_listener.id, event_id),
|
||||
"A fact that changed trust should become a lasting relationship cause"
|
||||
)
|
||||
_check(
|
||||
not manager.is_known_event_lasting(recent_listener.id, event_id),
|
||||
"A fact without a social consequence should remain recent"
|
||||
)
|
||||
_check(
|
||||
(
|
||||
manager.get_memory_summary(lasting_listener.id) == {"lasting": 1, "recent": 0}
|
||||
and manager.get_memory_summary(recent_listener.id) == {"lasting": 0, "recent": 1}
|
||||
),
|
||||
"Memory summaries should distinguish lasting and recent knowledge"
|
||||
)
|
||||
|
||||
var objective_event_count: int = manager.economic_events.size()
|
||||
var objective_next_event_id: int = manager.next_event_id
|
||||
for npc in manager.npcs:
|
||||
npc.set_task(SimulationIds.ACTION_REST, 1000.0)
|
||||
npc.target_id = &"retention_hold"
|
||||
npc.start_working()
|
||||
var review_interval: int = manager.get_knowledge_review_interval()
|
||||
for _step in range(review_interval - 1):
|
||||
manager.simulate_tick()
|
||||
_check(
|
||||
manager.npc_knows_event(recent_listener.id, event_id),
|
||||
"Recent knowledge should survive until the exact daily review boundary"
|
||||
)
|
||||
|
||||
var saved_json: String = manager.serialize_state()
|
||||
var boundary_data = JSON.parse_string(saved_json)
|
||||
boundary_data["simulation"]["tick_count"] = review_interval
|
||||
var boundary_restored := _create_manager(998)
|
||||
_check(
|
||||
boundary_restored.restore_state_from_json(JSON.stringify(boundary_data)),
|
||||
"A save loaded on a review boundary should restore successfully"
|
||||
)
|
||||
_check(
|
||||
(
|
||||
boundary_restored.npc_knows_event(lasting_listener.id, event_id)
|
||||
and not boundary_restored.npc_knows_event(recent_listener.id, event_id)
|
||||
and not boundary_restored.npc_knows_event(witness.id, event_id)
|
||||
and not boundary_restored.npc_knows_event(contributor.id, event_id)
|
||||
),
|
||||
"Loading exactly on a review boundary should immediately expire stale recent facts"
|
||||
)
|
||||
var restored := _create_manager(999)
|
||||
_check(
|
||||
restored.restore_state_from_json(saved_json),
|
||||
"Retention metadata should restore immediately before review"
|
||||
)
|
||||
manager.event_knowledge_forgotten.connect(_on_event_knowledge_forgotten)
|
||||
manager.simulate_tick()
|
||||
restored.simulate_tick()
|
||||
_check(
|
||||
restored.get_state_checksum() == manager.get_state_checksum(),
|
||||
"Original and restored simulations should forget the same facts at the same tick"
|
||||
)
|
||||
_check(
|
||||
(
|
||||
manager.npc_knows_event(lasting_listener.id, event_id)
|
||||
and not manager.npc_knows_event(recent_listener.id, event_id)
|
||||
and not manager.npc_knows_event(witness.id, event_id)
|
||||
and not manager.npc_knows_event(contributor.id, event_id)
|
||||
),
|
||||
"Daily review should keep only the lasting causal memory"
|
||||
)
|
||||
_check(
|
||||
forgotten_pairs == ["0:%d" % event_id, "1:%d" % event_id, "3:%d" % event_id],
|
||||
"Forgetting signals should be emitted once in stable knower/event order"
|
||||
)
|
||||
_check(
|
||||
(
|
||||
manager.economic_events.size() == objective_event_count
|
||||
and manager.next_event_id == objective_next_event_id
|
||||
),
|
||||
"Forgetting knowledge must not prune or replay objective event history"
|
||||
)
|
||||
var lasting_relationship: RelationshipStateRecord = (
|
||||
manager.relationship_system.get_relationship(lasting_listener.id, contributor.id)
|
||||
)
|
||||
_check(
|
||||
(
|
||||
lasting_relationship != null
|
||||
and is_equal_approx(lasting_relationship.get_trust(), 0.65)
|
||||
and lasting_relationship.get_last_trust_cause_event_id() == event_id
|
||||
),
|
||||
"Forgetting routine facts must preserve trust and its explainable current cause"
|
||||
)
|
||||
var reload_probe := _create_manager(1001)
|
||||
_check(
|
||||
reload_probe.restore_state_from_json(manager.serialize_state()),
|
||||
"A communicated memory should remain valid after its direct speaker forgets"
|
||||
)
|
||||
|
||||
_prepare_worker(witness, Vector3(20.0, 0.0, 0.0), true)
|
||||
_prepare_worker(recent_listener, Vector3(21.0, 0.0, 0.0), true)
|
||||
var before_stale_share: String = manager.get_state_checksum()
|
||||
_check(
|
||||
not manager.try_communicate_at_shared_activity(witness.id, recent_listener.id),
|
||||
"A direct witness who forgot a fact should no longer be able to share it"
|
||||
)
|
||||
_check(
|
||||
manager.get_state_checksum() == before_stale_share,
|
||||
"Rejected stale communication should not mutate state"
|
||||
)
|
||||
|
||||
_prepare_choice_divergence(manager, contributor, lasting_listener, recent_listener)
|
||||
var lasting_choice: ActionSelectionResult = manager.action_selector.select_action(
|
||||
lasting_listener, manager.village, 0.5, manager.npcs
|
||||
)
|
||||
var recent_choice: ActionSelectionResult = manager.action_selector.select_action(
|
||||
recent_listener, manager.village, 0.5, manager.npcs
|
||||
)
|
||||
_check(
|
||||
(
|
||||
lasting_choice.action_id == SimulationIds.ACTION_GATHER_FOOD
|
||||
and contributor.npc_name in lasting_choice.reason
|
||||
),
|
||||
"The lasting causal memory should preserve the informed helping choice"
|
||||
)
|
||||
_check(
|
||||
recent_choice.action_id == SimulationIds.ACTION_PATROL,
|
||||
"The villager whose routine fact faded should continue ordinary guard work"
|
||||
)
|
||||
|
||||
manager.free()
|
||||
boundary_restored.free()
|
||||
restored.free()
|
||||
reload_probe.free()
|
||||
_finish()
|
||||
|
||||
|
||||
func _prepare_worker(npc: SimNPC, position: Vector3, start_working: bool) -> void:
|
||||
npc.set_task(SimulationIds.ACTION_PATROL)
|
||||
npc.target_id = &"guard_post"
|
||||
npc.position = position
|
||||
if start_working:
|
||||
npc.start_working()
|
||||
|
||||
|
||||
func _prepare_choice_divergence(
|
||||
manager: Node, contributor: SimNPC, informed: SimNPC, control: SimNPC
|
||||
) -> void:
|
||||
var pantry: StorageStateRecord = manager.get_pantry()
|
||||
pantry.withdraw(
|
||||
SimulationIds.RESOURCE_FOOD,
|
||||
maxf(
|
||||
(
|
||||
pantry.get_amount(SimulationIds.RESOURCE_FOOD)
|
||||
- (ActionSelectionSystem.RELATIONSHIP_AID_PANTRY_THRESHOLD - 5.0)
|
||||
),
|
||||
0.0
|
||||
)
|
||||
)
|
||||
manager.economy.sync_resource(SimulationIds.RESOURCE_FOOD)
|
||||
manager.economy.deposit_resource(SimulationIds.RESOURCE_WOOD, 10.0)
|
||||
manager.village.safety = 50.0
|
||||
manager.village.knowledge = 20.0
|
||||
manager.village.update_priorities()
|
||||
contributor.hunger = 95.0
|
||||
contributor.is_starving = true
|
||||
contributor.is_dead = false
|
||||
for observer in [informed, control]:
|
||||
observer.profession = SimulationIds.PROFESSION_GUARD
|
||||
observer.hunger = 20.0
|
||||
observer.energy = 80.0
|
||||
observer.is_starving = false
|
||||
observer.starvation_ticks = 0
|
||||
observer.inventory.clear()
|
||||
observer.last_task = &""
|
||||
|
||||
|
||||
func _find_deposit_event(manager: Node, actor_id: int) -> EconomicEventRecord:
|
||||
for event in manager.get_npc_events(actor_id, 4):
|
||||
if StringName(event.data["event_type"]) == SimulationIds.EVENT_STORAGE_DEPOSITED:
|
||||
return event
|
||||
return null
|
||||
|
||||
|
||||
func _create_manager(seed_value: int = 831) -> Node:
|
||||
var manager: Node = load("res://simulation/SimulationManager.gd").new()
|
||||
manager.simulation_seed = seed_value
|
||||
manager.debug_logs = false
|
||||
var home_positions: Array[Vector3] = [
|
||||
Vector3(0.0, 0.0, 0.0),
|
||||
Vector3(1.0, 0.0, 0.0),
|
||||
Vector3(2.0, 0.0, 0.0),
|
||||
Vector3(3.0, 0.0, 0.0),
|
||||
Vector3(30.0, 0.0, 30.0),
|
||||
Vector3(40.0, 0.0, 40.0),
|
||||
]
|
||||
manager.home_positions = home_positions
|
||||
root.add_child(manager)
|
||||
manager.set_process(false)
|
||||
return manager
|
||||
|
||||
|
||||
func _on_event_knowledge_forgotten(knower_id: int, event: EconomicEventRecord) -> void:
|
||||
forgotten_pairs.append("%d:%d" % [knower_id, int(event.data["event_id"])])
|
||||
|
||||
|
||||
func _finish() -> void:
|
||||
if failures.is_empty():
|
||||
print("[TEST] Knowledge retention passed: recent fades, lasting cause remains")
|
||||
quit(0)
|
||||
return
|
||||
for failure in failures:
|
||||
push_error("[TEST] " + failure)
|
||||
quit(1)
|
||||
|
||||
|
||||
func _check(condition: bool, message: String) -> void:
|
||||
if not condition:
|
||||
failures.append(message)
|
||||
@@ -0,0 +1 @@
|
||||
uid://31vf2sh14nwe
|
||||
@@ -17,6 +17,7 @@ func _run() -> void:
|
||||
_test_previous_world_relationship_migration()
|
||||
_test_previous_world_knowledge_migration()
|
||||
_test_previous_world_provenance_migration()
|
||||
_test_previous_world_retention_migration()
|
||||
_test_relationship_schema_rejection()
|
||||
_test_schema_rejection()
|
||||
|
||||
@@ -138,6 +139,15 @@ func _test_schema_rejection() -> void:
|
||||
)
|
||||
_check(SimulationStateRecord.from_json("[]") == null, "Non-record JSON should be rejected")
|
||||
|
||||
var manager := _create_manager(41)
|
||||
var invalid_clock_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
invalid_clock_data["simulation"]["tick_interval"] = 0.0
|
||||
_check(
|
||||
SimulationStateRecord.from_dictionary(invalid_clock_data) == null,
|
||||
"Saved simulation clocks with a zero tick interval should be rejected"
|
||||
)
|
||||
manager.free()
|
||||
|
||||
|
||||
func _test_legacy_resource_migration() -> void:
|
||||
var migrated := ResourceStateRecord.from_dictionary(
|
||||
@@ -272,7 +282,7 @@ func _test_previous_world_knowledge_migration() -> void:
|
||||
var deposit_event := EconomicEventRecord.create(
|
||||
0,
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
12,
|
||||
0,
|
||||
1,
|
||||
SimulationIds.npc_inventory_id(1),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
@@ -305,11 +315,11 @@ func _test_previous_world_knowledge_migration() -> void:
|
||||
func _test_previous_world_provenance_migration() -> void:
|
||||
var manager := _create_manager(60)
|
||||
var previous_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
previous_data["schema_version"] = SimulationStateRecord.PREVIOUS_SCHEMA_VERSION
|
||||
previous_data["schema_version"] = SimulationStateRecord.PROVENANCE_LEGACY_SCHEMA_VERSION
|
||||
var deposit_event := EconomicEventRecord.create(
|
||||
0,
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
12,
|
||||
0,
|
||||
1,
|
||||
SimulationIds.npc_inventory_id(1),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
@@ -358,6 +368,77 @@ func _test_previous_world_provenance_migration() -> void:
|
||||
manager.free()
|
||||
|
||||
|
||||
func _test_previous_world_retention_migration() -> void:
|
||||
var manager := _create_manager(61)
|
||||
var previous_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
previous_data["schema_version"] = SimulationStateRecord.PREVIOUS_SCHEMA_VERSION
|
||||
previous_data["simulation"]["tick_count"] = 50
|
||||
var deposit_event := EconomicEventRecord.create(
|
||||
0,
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
0,
|
||||
0,
|
||||
SimulationIds.npc_inventory_id(0),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
SimulationIds.RESOURCE_FOOD,
|
||||
2.0
|
||||
)
|
||||
previous_data["economic_events"] = [deposit_event.to_dictionary()]
|
||||
previous_data["simulation"]["next_event_id"] = 1
|
||||
var actor_knowledge := (
|
||||
KnownEventStateRecord
|
||||
. create(0, 0, SimulationIds.KNOWLEDGE_ACQUISITION_PERFORMED)
|
||||
. to_dictionary()
|
||||
)
|
||||
var witness_knowledge := (
|
||||
KnownEventStateRecord
|
||||
. create(2, 0, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED)
|
||||
. to_dictionary()
|
||||
)
|
||||
var communicated_knowledge := (
|
||||
KnownEventStateRecord
|
||||
. create(
|
||||
1,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED,
|
||||
2,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
|
||||
)
|
||||
. to_dictionary()
|
||||
)
|
||||
for old_record in [actor_knowledge, witness_knowledge, communicated_knowledge]:
|
||||
old_record["schema_version"] = 2
|
||||
old_record.erase("acquired_tick")
|
||||
old_record.erase("source_acquisition_method")
|
||||
previous_data["event_knowledge"] = [communicated_knowledge, actor_knowledge, witness_knowledge]
|
||||
previous_data["relationships"] = []
|
||||
var migrated := SimulationStateRecord.from_dictionary(previous_data)
|
||||
_check(migrated != null, "World schema v6 should add deterministic retention metadata")
|
||||
if migrated != null:
|
||||
var records_by_knower := {}
|
||||
for record in migrated.event_knowledge:
|
||||
records_by_knower[record.get_knower_id()] = record
|
||||
var migrated_actor := records_by_knower.get(0) as KnownEventStateRecord
|
||||
var migrated_listener := records_by_knower.get(1) as KnownEventStateRecord
|
||||
_check(
|
||||
migrated_actor != null and migrated_actor.get_acquired_tick() == 0,
|
||||
"Direct v6 knowledge should retain the objective event tick"
|
||||
)
|
||||
_check(
|
||||
(
|
||||
migrated_listener != null
|
||||
and migrated_listener.get_acquired_tick() == 50
|
||||
and (
|
||||
migrated_listener.get_source_acquisition_method()
|
||||
== SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
|
||||
)
|
||||
),
|
||||
"Communicated v6 knowledge should receive a fresh lease and source snapshot"
|
||||
)
|
||||
manager.free()
|
||||
|
||||
|
||||
func _test_relationship_schema_rejection() -> void:
|
||||
var manager := _create_manager(58)
|
||||
var missing_cause_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
@@ -389,7 +470,7 @@ func _test_relationship_schema_rejection() -> void:
|
||||
var event := EconomicEventRecord.create(
|
||||
0,
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
SimulationIds.npc_inventory_id(0),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
@@ -411,7 +492,7 @@ func _test_relationship_schema_rejection() -> void:
|
||||
var unrelated_deposit := EconomicEventRecord.create(
|
||||
0,
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
1,
|
||||
0,
|
||||
2,
|
||||
SimulationIds.npc_inventory_id(2),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
@@ -435,7 +516,7 @@ func _test_relationship_schema_rejection() -> void:
|
||||
var nonpositive_deposit := EconomicEventRecord.create(
|
||||
0,
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
SimulationIds.npc_inventory_id(1),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
@@ -456,26 +537,40 @@ func _test_relationship_schema_rejection() -> void:
|
||||
var provenance_event := EconomicEventRecord.create(
|
||||
0,
|
||||
SimulationIds.EVENT_STORAGE_DEPOSITED,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
SimulationIds.npc_inventory_id(0),
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
SimulationIds.RESOURCE_FOOD,
|
||||
1.0
|
||||
)
|
||||
var missing_source_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
missing_source_data["economic_events"] = [provenance_event.to_dictionary()]
|
||||
missing_source_data["simulation"]["next_event_id"] = 1
|
||||
missing_source_data["event_knowledge"] = [
|
||||
var historical_source_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
historical_source_data["economic_events"] = [provenance_event.to_dictionary()]
|
||||
historical_source_data["simulation"]["next_event_id"] = 1
|
||||
historical_source_data["event_knowledge"] = [
|
||||
(
|
||||
KnownEventStateRecord
|
||||
. create(1, 0, SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED, 2)
|
||||
. create(
|
||||
1,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED,
|
||||
2,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
|
||||
)
|
||||
. to_dictionary()
|
||||
)
|
||||
]
|
||||
_check(
|
||||
SimulationStateRecord.from_dictionary(missing_source_data) == null,
|
||||
"Communicated knowledge must reference a source who knows the same event"
|
||||
SimulationStateRecord.from_dictionary(historical_source_data) != null,
|
||||
"A listener's provenance should remain valid after the direct source forgets"
|
||||
)
|
||||
|
||||
var unknown_source_data: Dictionary = historical_source_data.duplicate(true)
|
||||
unknown_source_data["event_knowledge"][0]["source_npc_id"] = 999
|
||||
_check(
|
||||
SimulationStateRecord.from_dictionary(unknown_source_data) == null,
|
||||
"Communicated knowledge must reference an existing historical source"
|
||||
)
|
||||
|
||||
var wrong_actor_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
@@ -499,12 +594,26 @@ func _test_relationship_schema_rejection() -> void:
|
||||
relayed_source_data["event_knowledge"] = [
|
||||
(
|
||||
KnownEventStateRecord
|
||||
. create(1, 0, SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED, 2)
|
||||
. create(
|
||||
1,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED,
|
||||
2,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED
|
||||
)
|
||||
. to_dictionary()
|
||||
),
|
||||
(
|
||||
KnownEventStateRecord
|
||||
. create(2, 0, SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED, 0)
|
||||
. create(
|
||||
2,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED,
|
||||
0,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_PERFORMED
|
||||
)
|
||||
. to_dictionary()
|
||||
),
|
||||
(
|
||||
@@ -517,6 +626,14 @@ func _test_relationship_schema_rejection() -> void:
|
||||
SimulationStateRecord.from_dictionary(relayed_source_data) == null,
|
||||
"Communicated facts must not form a second relay hop in this phase"
|
||||
)
|
||||
var reacquired_source_data: Dictionary = relayed_source_data.duplicate(true)
|
||||
reacquired_source_data["event_knowledge"][0]["source_acquisition_method"] = String(
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
|
||||
)
|
||||
_check(
|
||||
SimulationStateRecord.from_dictionary(reacquired_source_data) != null,
|
||||
"Historical direct provenance should survive a source reacquiring the fact differently"
|
||||
)
|
||||
|
||||
var order_independent_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
order_independent_data["economic_events"] = [provenance_event.to_dictionary()]
|
||||
@@ -524,7 +641,14 @@ func _test_relationship_schema_rejection() -> void:
|
||||
order_independent_data["event_knowledge"] = [
|
||||
(
|
||||
KnownEventStateRecord
|
||||
. create(1, 0, SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED, 2)
|
||||
. create(
|
||||
1,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED,
|
||||
2,
|
||||
0,
|
||||
SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
|
||||
)
|
||||
. to_dictionary()
|
||||
),
|
||||
(
|
||||
@@ -542,6 +666,22 @@ func _test_relationship_schema_rejection() -> void:
|
||||
SimulationStateRecord.from_dictionary(order_independent_data) != null,
|
||||
"Provenance validation should not depend on knowledge array ordering"
|
||||
)
|
||||
|
||||
var invalid_direct_time_data: Dictionary = manager.create_state_record().to_dictionary()
|
||||
invalid_direct_time_data["simulation"]["tick_count"] = 2
|
||||
invalid_direct_time_data["economic_events"] = [provenance_event.to_dictionary()]
|
||||
invalid_direct_time_data["simulation"]["next_event_id"] = 1
|
||||
invalid_direct_time_data["event_knowledge"] = [
|
||||
(
|
||||
KnownEventStateRecord
|
||||
. create(1, 0, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED, -1, 1)
|
||||
. to_dictionary()
|
||||
)
|
||||
]
|
||||
_check(
|
||||
SimulationStateRecord.from_dictionary(invalid_direct_time_data) == null,
|
||||
"Direct witness acquisition must use the immutable objective event tick"
|
||||
)
|
||||
manager.free()
|
||||
|
||||
|
||||
|
||||
@@ -162,16 +162,20 @@ func test_event_communication_preserves_first_provenance_and_stops_after_one_hop
|
||||
var knowledge_system := EventKnowledgeSystemScript.new()
|
||||
knowledge_system.observe_event(event, villagers)
|
||||
|
||||
assert_null(knowledge_system.communicate_event(event, listener.id, relay_target.id))
|
||||
var communicated := knowledge_system.communicate_event(event, witness.id, listener.id)
|
||||
assert_null(knowledge_system.communicate_event(event, listener.id, relay_target.id, 50))
|
||||
var communicated := knowledge_system.communicate_event(event, witness.id, listener.id, 50)
|
||||
assert_not_null(communicated)
|
||||
assert_eq(
|
||||
communicated.get_acquisition_method(), SimulationIds.KNOWLEDGE_ACQUISITION_COMMUNICATED
|
||||
)
|
||||
assert_eq(communicated.get_source_npc_id(), witness.id)
|
||||
assert_null(knowledge_system.communicate_event(event, actor.id, listener.id))
|
||||
assert_eq(communicated.get_acquired_tick(), 50)
|
||||
assert_eq(
|
||||
communicated.get_source_acquisition_method(), SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED
|
||||
)
|
||||
assert_null(knowledge_system.communicate_event(event, actor.id, listener.id, 50))
|
||||
assert_eq(knowledge_system.get_record(listener.id, event.data["event_id"]), communicated)
|
||||
assert_null(knowledge_system.communicate_event(event, listener.id, relay_target.id))
|
||||
assert_null(knowledge_system.communicate_event(event, listener.id, relay_target.id, 50))
|
||||
assert_false(knowledge_system.knows_event(relay_target.id, int(event.data["event_id"])))
|
||||
var actor_gap_system := EventKnowledgeSystemScript.new()
|
||||
var direct_witness_records: Array[KnownEventStateRecord] = [
|
||||
@@ -180,4 +184,36 @@ func test_event_communication_preserves_first_provenance_and_stops_after_one_hop
|
||||
)
|
||||
]
|
||||
actor_gap_system.restore(direct_witness_records)
|
||||
assert_null(actor_gap_system.communicate_event(event, witness.id, actor.id))
|
||||
assert_null(actor_gap_system.communicate_event(event, witness.id, actor.id, 50))
|
||||
|
||||
|
||||
func test_knowledge_retention_is_bounded_stable_and_importance_ranked() -> void:
|
||||
var knowledge_system := EventKnowledgeSystemScript.new()
|
||||
var records: Array[KnownEventStateRecord] = [
|
||||
KnownEventStateRecord.create(0, 30, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED, -1, 1),
|
||||
KnownEventStateRecord.create(0, 20, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED, -1, 2),
|
||||
KnownEventStateRecord.create(0, 10, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED, -1, 3),
|
||||
KnownEventStateRecord.create(0, 5, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED, -1, 4),
|
||||
]
|
||||
knowledge_system.restore(records)
|
||||
var no_lasting: Array[KnownEventStateRecord] = []
|
||||
var capacity_forgotten := knowledge_system.maintain_retention(4, 100, no_lasting, false)
|
||||
|
||||
assert_eq(capacity_forgotten.size(), 1)
|
||||
assert_eq(capacity_forgotten[0].get_event_id(), 30)
|
||||
assert_eq(knowledge_system.get_known_event_ids(0, 0), [20, 10, 5])
|
||||
var lasting: Array[KnownEventStateRecord] = [knowledge_system.get_record(0, 20)]
|
||||
assert_eq(knowledge_system.maintain_retention(101, 100, lasting, true).size(), 0)
|
||||
assert_true(knowledge_system.knows_event(0, 20))
|
||||
assert_eq(knowledge_system.maintain_retention(103, 100, lasting, true).size(), 1)
|
||||
assert_false(knowledge_system.knows_event(0, 10))
|
||||
assert_true(knowledge_system.knows_event(0, 20))
|
||||
|
||||
var ranked_system := EventKnowledgeSystemScript.new()
|
||||
var ranked_records: Array[KnownEventStateRecord] = [
|
||||
KnownEventStateRecord.create(1, 1, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED, -1, 10),
|
||||
KnownEventStateRecord.create(1, 2, SimulationIds.KNOWLEDGE_ACQUISITION_WITNESSED, -1, 20),
|
||||
]
|
||||
ranked_system.restore(ranked_records)
|
||||
var lasting_event_ids: Array[int] = [1]
|
||||
assert_eq(ranked_system.get_communicable_event_ids(1, lasting_event_ids), [1, 2])
|
||||
|
||||
Reference in New Issue
Block a user