Spaces:
Sleeping
Sleeping
File size: 15,976 Bytes
3d5c3e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | 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"
|