Spaces:
Sleeping
Sleeping
Download tests/unit/test_context_assembler.py from defenestrate/learning-world-goliath: direct link, hf CLI and curl.
- Browser
- Download file 16 kB
-
https://huggingface.co/spaces/defenestrate/learning-world-goliath/resolve/main/tests/unit/test_context_assembler.py
- Command line
-
hf download hf://spaces/defenestrate/learning-world-goliath/tests/unit/test_context_assembler.py
-
curl -L -o test_context_assembler.py https://huggingface.co/spaces/defenestrate/learning-world-goliath/resolve/main/tests/unit/test_context_assembler.py
16 kB
| import pytest | |
| from scenarios.examples.plant_quality_investigation.build import build_package | |
| from tests.unit.test_events import make_env | |
| from world_compiler.domain.events import EventType, ResolvedPrincipal | |
| from world_compiler.domain.schemas.facts import PrincipalTemplate | |
| from world_compiler.infrastructure.database.store import EventStore, make_engine | |
| from world_compiler.runtime.actors.role_packets import RolePacketProjection | |
| from world_compiler.runtime.orchestrator.context import ( | |
| ContextAssembler, | |
| ContextBudgetExceeded, | |
| SessionScope, | |
| ) | |
| from world_compiler.runtime.world.projection import ProjectionService | |
| def all_text(ctx): | |
| return " ".join(m["content"] for m in ctx.messages) | |
| def test_actor_context_contains_own_canary_not_others(rig): | |
| _, _, asm = rig | |
| ctx = asm.actor_context( | |
| "ACTOR-A", | |
| symbol_env={ | |
| "interview_count": 2, | |
| "turn": 2, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| text = all_text(ctx) | |
| assert "LWC-CANARY-KA" in text | |
| for other in ("KB", "KC", "KD"): | |
| assert f"LWC-CANARY-{other}" not in text | |
| def test_actor_system_prompt_renders_rules_once_and_guarded_guidance_once(rig): | |
| pkg, _, asm = rig | |
| guarded = next(f for f in pkg.private_facts if f.disclosure_class == "guarded") | |
| ctx = asm.actor_context( | |
| guarded.governance.owner_role, | |
| symbol_env={ | |
| "interview_count": 2, | |
| "turn": 2, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| system = ctx.messages[0]["content"] | |
| assert system.count("Conversation rules") == 1 | |
| assert "never begin your reply with any such label" in system | |
| # Reluctance is stated exactly once, in the guidance section — the | |
| # per-fact marker stays terse so no second authority competes with it. | |
| assert system.count("never volunteer") == 1 | |
| assert f"[GUARDED — predicate satisfied] {guarded.text}" in system | |
| def test_guarded_fact_excluded_until_threshold(rig): | |
| pkg, _, asm = rig | |
| guarded = next(f for f in pkg.private_facts if f.disclosure_class == "guarded") | |
| low = all_text( | |
| asm.actor_context( | |
| guarded.governance.owner_role, | |
| symbol_env={ | |
| "interview_count": 1, | |
| "turn": 1, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| ) | |
| high = all_text( | |
| asm.actor_context( | |
| guarded.governance.owner_role, | |
| symbol_env={ | |
| "interview_count": 2, | |
| "turn": 2, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| ) | |
| assert guarded.text not in low and guarded.text in high | |
| def test_transmission_context_is_least_privilege(rig): | |
| pkg, store, asm = rig | |
| actor_a = ResolvedPrincipal( | |
| kind="actor", | |
| tenant_id="t1", | |
| course_id="c1", | |
| session_id="s1", | |
| actor_id="ACTOR-A", | |
| ) | |
| store.append( | |
| "student_channel", | |
| make_env( | |
| event_id="actor-a-projection-canary", | |
| scenario_id=pkg.manifest.scenario_id, | |
| scenario_version=pkg.manifest.version, | |
| event_type=EventType.MESSAGE_SENT, | |
| payload={ | |
| "sender_role": "STUDENT", | |
| "recipient_role": "ACTOR-A", | |
| "text": "LWC-CANARY-PROJECTION-ONLY", | |
| "epistemic_type": "claim", | |
| }, | |
| visibility=[actor_a], | |
| parent_event_ids=[], | |
| ), | |
| idempotency_key="actor-a-projection-canary", | |
| ) | |
| src = make_env( | |
| scenario_id=pkg.manifest.scenario_id, | |
| scenario_version=pkg.manifest.version, | |
| event_type=EventType.MESSAGE_SENT, | |
| payload={ | |
| "sender_role": "STUDENT", | |
| "recipient_role": "ACTOR-A", | |
| "text": "I think the resin change caused it", | |
| "epistemic_type": "claim", | |
| }, | |
| parent_event_ids=[], | |
| ) | |
| ctx = asm.transmission_context(src, pkg.transmission_rules[0]) | |
| text = all_text(ctx) | |
| assert "resin change" in text | |
| assert "LWC-CANARY" not in text # no packet, no projection content | |
| def test_role_packet_rejects_private_fact_owned_by_another_role(): | |
| pkg = build_package() | |
| actor_a = next(profile for profile in pkg.profiles if profile.role_ref == "ACTOR-A") | |
| actor_a.private_fact_ids = ["pf_alarm_suppressed"] | |
| with pytest.raises( | |
| ValueError, | |
| match="ACTOR-A.*private fact.*pf_alarm_suppressed.*ACTOR-D", | |
| ): | |
| RolePacketProjection(pkg) | |
| def test_role_packet_rejects_private_fact_not_visible_to_its_owner(): | |
| pkg = build_package() | |
| fact = next(fact for fact in pkg.private_facts if fact.fact_id == "pf_speedup") | |
| fact.governance.visibility = [PrincipalTemplate(kind="role", role_ref="ACTOR-D")] | |
| with pytest.raises( | |
| ValueError, | |
| match="pf_speedup.*visibility.*ACTOR-A", | |
| ): | |
| RolePacketProjection(pkg) | |
| def test_role_packet_rejects_belief_for_another_role(): | |
| pkg = build_package() | |
| belief = next( | |
| belief for belief in pkg.beliefs if belief.belief_id == "belief_packaging_cause" | |
| ) | |
| belief.role_ref = "ACTOR-D" | |
| with pytest.raises( | |
| ValueError, | |
| match="ACTOR-A.*belief.*belief_packaging_cause.*ACTOR-D", | |
| ): | |
| RolePacketProjection(pkg) | |
| def test_role_packet_rejects_disclosure_policy_for_another_role(): | |
| pkg = build_package() | |
| policy = next( | |
| policy | |
| for policy in pkg.disclosure_policies | |
| if policy.policy_id == "policy-manager" | |
| ) | |
| policy.role_ref = "ACTOR-D" | |
| with pytest.raises( | |
| ValueError, | |
| match="ACTOR-A.*disclosure policy.*policy-manager.*ACTOR-D", | |
| ): | |
| RolePacketProjection(pkg) | |
| def test_role_packet_and_briefing_are_role_scoped(rig): | |
| pkg, _, _ = rig | |
| packets = RolePacketProjection(pkg) | |
| packet = packets.packet_for("ACTOR-C") | |
| briefing = packets.briefing_dto("ACTOR-C") | |
| assert [fact.fact_id for fact in packet.facts] == [ | |
| "pf_resin_change", | |
| "pf_supplier_claim", | |
| ] | |
| assert "LWC-CANARY-KC" in " ".join(briefing["facts"]) | |
| assert "LWC-NEVER" not in str(briefing) | |
| assert briefing["guarded_reminder"] == packet.guarded_reminder | |
| def test_actor_knows_never_fact_with_explicit_non_disclosure_instruction(rig): | |
| _, _, asm = rig | |
| ctx = asm.actor_context( | |
| "ACTOR-C", | |
| symbol_env={ | |
| "interview_count": 0, | |
| "turn": 1, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| assert "LWC-NEVER-K0" in all_text(ctx) | |
| assert "NEVER DISCLOSE" in all_text(ctx) | |
| assert ctx.purpose == "actor_turn" | |
| assert ctx.metadata == {"role_id": "ACTOR-C"} | |
| assert ctx.manifest.included_fact_ids == [ | |
| "pf_resin_change", | |
| "pf_supplier_claim", | |
| ] | |
| def test_compaction_uses_only_actor_projection(rig): | |
| pkg, store, asm = rig | |
| actor_a = ResolvedPrincipal( | |
| kind="actor", | |
| tenant_id="t1", | |
| course_id="c1", | |
| session_id="s1", | |
| actor_id="ACTOR-A", | |
| ) | |
| actor_b = actor_a.model_copy(update={"actor_id": "ACTOR-B"}) | |
| for event_id, text, visibility in ( | |
| ("for-a", "visible only to A", [actor_a]), | |
| ("for-b", "visible only to B", [actor_b]), | |
| ): | |
| store.append( | |
| "student_channel", | |
| make_env( | |
| event_id=event_id, | |
| scenario_id=pkg.manifest.scenario_id, | |
| scenario_version=pkg.manifest.version, | |
| event_type=EventType.MESSAGE_SENT, | |
| payload={ | |
| "sender_role": "STUDENT", | |
| "recipient_role": visibility[0].actor_id, | |
| "text": text, | |
| "epistemic_type": "claim", | |
| }, | |
| visibility=visibility, | |
| parent_event_ids=[], | |
| ), | |
| idempotency_key=event_id, | |
| ) | |
| ctx = asm.compaction_context("ACTOR-A") | |
| assert "visible only to A" in all_text(ctx) | |
| assert "visible only to B" not in all_text(ctx) | |
| assert ctx.manifest.included_event_ids == ["for-a"] | |
| assert ctx.manifest.included_fact_ids == [] | |
| assert ctx.purpose == "memory_compaction" | |
| assert ctx.metadata == {"role_id": "ACTOR-A"} | |
| audit = next( | |
| event | |
| for event in store.read_all("t1", "c1", "s1", "plant_quality", "1.0.0") | |
| if event.event_type == EventType.CONTEXT_ASSEMBLED | |
| ) | |
| assert audit.payload["packet_role_id"] is None | |
| def test_actor_context_rejects_assembled_prompt_over_input_budget(rig): | |
| pkg, store, asm = rig | |
| actor_a = ResolvedPrincipal( | |
| kind="actor", | |
| tenant_id="t1", | |
| course_id="c1", | |
| session_id="s1", | |
| actor_id="ACTOR-A", | |
| ) | |
| for index in range(3): | |
| event_id = f"long-message-{index}" | |
| store.append( | |
| "student_channel", | |
| make_env( | |
| event_id=event_id, | |
| scenario_id=pkg.manifest.scenario_id, | |
| scenario_version=pkg.manifest.version, | |
| event_type=EventType.MESSAGE_SENT, | |
| payload={ | |
| "sender_role": "STUDENT", | |
| "recipient_role": "ACTOR-A", | |
| "text": "long authorized message " * 20, | |
| "epistemic_type": "claim", | |
| }, | |
| visibility=[actor_a], | |
| parent_event_ids=[], | |
| ), | |
| idempotency_key=event_id, | |
| ) | |
| limited = ContextAssembler( | |
| ProjectionService(store, pkg.manifest.scenario_id, pkg.manifest.version), | |
| RolePacketProjection(pkg), | |
| store, | |
| asm.scope, | |
| input_budget_tokens=10, | |
| ) | |
| with pytest.raises(ContextBudgetExceeded): | |
| limited.actor_context( | |
| "ACTOR-A", | |
| symbol_env={ | |
| "interview_count": 3, | |
| "turn": 3, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| def test_transmission_context_stamps_transform_purpose(rig): | |
| pkg, _, asm = rig | |
| source = make_env( | |
| scenario_id=pkg.manifest.scenario_id, | |
| scenario_version=pkg.manifest.version, | |
| event_type=EventType.MESSAGE_SENT, | |
| payload={ | |
| "sender_role": "STUDENT", | |
| "recipient_role": "ACTOR-A", | |
| "text": "source text", | |
| "epistemic_type": "claim", | |
| }, | |
| parent_event_ids=[], | |
| ) | |
| ctx = asm.transmission_context(source, pkg.transmission_rules[0]) | |
| assert ctx.purpose == "transmission_transform" | |
| assert ctx.metadata == {} | |
| assert ctx.manifest.role_id is None | |
| assert ctx.manifest.included_event_ids == [source.event_id] | |
| assert ctx.manifest.included_fact_ids == [] | |
| def test_actor_and_compaction_exclude_interleaved_other_scenario(rig): | |
| pkg, store, asm = rig | |
| actor_a = ResolvedPrincipal( | |
| kind="actor", | |
| tenant_id="t1", | |
| course_id="c1", | |
| session_id="s1", | |
| actor_id="ACTOR-A", | |
| ) | |
| for event_id, text, scenario_id, scenario_version in ( | |
| ( | |
| "current-scenario", | |
| "authorized current scenario", | |
| pkg.manifest.scenario_id, | |
| pkg.manifest.version, | |
| ), | |
| ("foreign-scenario", "must not cross scenario boundary", "other", "99"), | |
| ): | |
| store.append( | |
| "student_channel", | |
| make_env( | |
| event_id=event_id, | |
| scenario_id=scenario_id, | |
| scenario_version=scenario_version, | |
| event_type=EventType.MESSAGE_SENT, | |
| payload={ | |
| "sender_role": "STUDENT", | |
| "recipient_role": "ACTOR-A", | |
| "text": text, | |
| "epistemic_type": "claim", | |
| }, | |
| visibility=[actor_a], | |
| parent_event_ids=[], | |
| ), | |
| idempotency_key=event_id, | |
| ) | |
| actor = asm.actor_context( | |
| "ACTOR-A", | |
| symbol_env={ | |
| "interview_count": 2, | |
| "turn": 3, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| compaction = asm.compaction_context("ACTOR-A") | |
| for context in (actor, compaction): | |
| assert "authorized current scenario" in all_text(context) | |
| assert "must not cross scenario boundary" not in all_text(context) | |
| assert context.manifest.included_event_ids == ["current-scenario"] | |
| def test_transmission_rejects_source_from_other_scenario_version(rig): | |
| pkg, _, asm = rig | |
| source = make_env( | |
| scenario_id=pkg.manifest.scenario_id, | |
| scenario_version="different-version", | |
| event_type=EventType.MESSAGE_SENT, | |
| payload={ | |
| "sender_role": "STUDENT", | |
| "recipient_role": "ACTOR-A", | |
| "text": "cross-version source", | |
| "epistemic_type": "claim", | |
| }, | |
| parent_event_ids=[], | |
| ) | |
| with pytest.raises(ValueError, match="outside the session scope"): | |
| asm.transmission_context(source, pkg.transmission_rules[0]) | |
| def test_assembler_rejects_packet_projection_from_other_scenario(rig): | |
| _, store, asm = rig | |
| foreign = build_package() | |
| foreign.manifest.scenario_id = "other-scenario" | |
| with pytest.raises(ValueError, match="packet projection does not match"): | |
| ContextAssembler( | |
| ProjectionService(store, asm.scope.scenario_id, asm.scope.scenario_version), | |
| RolePacketProjection(foreign), | |
| store, | |
| asm.scope, | |
| input_budget_tokens=6000, | |
| ) | |
| def test_every_assembly_emits_digest_only_audit_event(rig): | |
| _, store, asm = rig | |
| ctx = asm.actor_context( | |
| "ACTOR-A", | |
| symbol_env={ | |
| "interview_count": 2, | |
| "turn": 2, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| audits = [ | |
| e | |
| for e in store.read_all("t1", "c1", "s1", "plant_quality", "1.0.0") | |
| if e.event_type == EventType.CONTEXT_ASSEMBLED | |
| ] | |
| assert "LWC-CANARY-KA" in all_text(ctx) | |
| assert len(audits) == 1 | |
| assert all("LWC-CANARY" not in str(audit.payload) for audit in audits) | |
| def test_session_scope_role_map_rejects_mutation(rig): | |
| _, _, asm = rig | |
| with pytest.raises(TypeError): | |
| asm.scope.role_to_actor["ACTOR-A"] = "ACTOR-D" | |
| def test_audit_uses_package_default_retention_class(): | |
| pkg = build_package() | |
| pkg.retention.default_class = "ephemeral" | |
| store = EventStore(make_engine("sqlite://")) | |
| scope = SessionScope( | |
| tenant_id="t1", | |
| course_id="c1", | |
| session_id="s1", | |
| scenario_id=pkg.manifest.scenario_id, | |
| scenario_version=pkg.manifest.version, | |
| role_to_actor={role.role_id: role.role_id for role in pkg.roles}, | |
| student_role_id="STUDENT", | |
| retention_class=pkg.retention.default_class, | |
| ) | |
| asm = ContextAssembler( | |
| ProjectionService(store, pkg.manifest.scenario_id, pkg.manifest.version), | |
| RolePacketProjection(pkg), | |
| store, | |
| scope, | |
| input_budget_tokens=6000, | |
| ) | |
| asm.actor_context( | |
| "ACTOR-A", | |
| symbol_env={ | |
| "interview_count": 0, | |
| "turn": 1, | |
| "phase": "INVESTIGATION", | |
| }, | |
| ) | |
| audit = next( | |
| event | |
| for event in store.read_all("t1", "c1", "s1", "plant_quality", "1.0.0") | |
| if event.event_type == EventType.CONTEXT_ASSEMBLED | |
| ) | |
| assert audit.retention_class == "ephemeral" | |
| def test_scope_is_public(rig): | |
| _, _, asm = rig | |
| assert asm.scope.session_id == "s1" | |