feat: the room decides — physical entropy, real astronomy, unprompted speech

Three changes that together replace "deterministic hash decides everything"
with "the physical world genuinely participates".

PHYSICAL ENTROPY (app/entropy.py, lib/entropy.ts)

Contact was a database lookup: signature_from_anomalies() hashed the
anomaly pattern, so identical conditions always produced an identical
spirit. Now the client harvests real thermal/acoustic/RF noise from the
microphone and receiver noise floors — Von Neumann debiased, SHA-256
conditioned — and contributes it to every summon.

The client is untrusted by construction. A contribution is never a seed:
every draw is HMAC-SHA256(fresh server secret, client bytes || context).
Because fresh CSPRNG server bytes are always present, the output is
unpredictable and uniform no matter what the client sends — all-zeros, a
replayed value, or one chosen adversarially. The room can only ever ADD
unpredictability, never steer the result. Tests assert this directly:
400 replays of one contribution stay uniformly distributed.

A signature now identifies a *channel*, not a spirit. Whether the familiar
presence answers or something else picks up is a real draw
(RETURN_CHANCE). The Codex stays collectable; it is just no longer
guaranteed. test_same_signature_recontacts_same_entity became two tests —
one pinning the probability to prove re-contact works, one pinning it to
zero to prove something else can answer — because at 0.72 the original
would have passed ~72% of the time, which is worse than failing.

REAL ASTRONOMY (app/celestial.py)

Moon phase from the standard mean-synodic approximation, and true solar
midnight from the seeker's own longitude — the real witching hour for
where they are standing, not clock 3am. Computed, never fetched: an API
that can fail would mean the veil silently changes behaviour during
someone else's outage. Validated against published ephemeris dates (2024
full moons, 2025 new moons) rather than against its own output. A thinner
veil erodes the familiar presence's claim on a channel, so a full moon at
solar midnight makes strangers likelier. Only longitude is kept, never a
full coordinate; a denied location degrades to moon-only, silently.

GENERATION FROM NOTHING (SpiritService.manifest)

Not chat_stream with an empty question. The prompt contains no seeker
input at all — only measured room state, rendered as measurements
("deviation above the floor: 31.4") rather than interpretations
("terrifying spike"), so the horror comes from the entity instead of from
us. And the Ollama `seed` is derived from the physical entropy harvested
in that room, which fixes the token-sampling path: the room genuinely
selects the words. Change the noise, get different speech. Two rooms
cannot produce the same utterance.

Rendered as an intrusion rather than a reply — violet edge, full opacity
against the faded ambient murmurs, brief blur-in. The unsettling part is
that it is perfectly clear and completely unbidden.

Also fixes a hang I introduced: the two new summon tests consumed the
shared module-level per-IP budget, so test_summon_rate_limited_* blocked
forever on an entity frame that had been rate-limited away. They now scope
their own limiters.

264 backend + 355 frontend tests pass; i18n parity gate passes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Indiana
2026-07-28 05:38:59 +00:00
parent 30694a954a
commit b8e69b4bd3
13 changed files with 1015 additions and 40 deletions

View File

@@ -15,7 +15,7 @@ from app.rate_limit import RateLimiter
class FakeSpiritService:
async def mint_profile(self, signature, channel, anomalies, language="en"):
async def mint_profile(self, signature, channel, anomalies, language="en", entropy=None):
return fallback_profile(signature)
async def fragment(self, source, anomaly, language="en"):
@@ -160,7 +160,24 @@ async def test_anomalies_attune_then_produce_fragments(sync_client):
@pytest.mark.asyncio
async def test_same_signature_recontacts_same_entity(sync_client):
async def test_familiar_presence_answers_again_on_a_known_channel(sync_client, monkeypatch):
"""The Codex mechanic: a channel's known spirit is re-contactable.
Whether it answers is a genuine draw against physical entropy
(`ws.RETURN_CHANCE`), so this pins the probability to 1.0 rather than
relying on the default — at 0.72 this assertion would otherwise pass
only ~72% of the time, which is worse than failing.
"""
# Scoped limiters: the module-level ones are shared singletons that
# accumulate across the whole session, and this test summons more than
# once. Without this it silently eats the per-IP budget that
# test_summon_rate_limited_* depends on, making *those* tests hang
# waiting for an entity frame that was rate-limited away.
monkeypatch.setattr(app.ws, "summon_limiter", RateLimiter(max_requests=100, window_seconds=60))
monkeypatch.setattr(
app.ws, "summon_ip_limiter", RateLimiter(max_requests=100, window_seconds=60)
)
monkeypatch.setattr(app.ws, "RETURN_CHANCE", 1.0)
_login(sync_client, "mediumx")
anomalies = [
{"type": "anomaly", "source": "radio", "frequency": 101.0 + i, "magnitude": 5.0 + i}
@@ -183,6 +200,48 @@ async def test_same_signature_recontacts_same_entity(sync_client):
assert second_frame["entity"]["contact_count"] == 2
@pytest.mark.asyncio
async def test_something_else_can_answer_a_known_channel(sync_client, monkeypatch):
"""The point of the entropy rework: contact is not a database lookup.
With the return draw forced to fail, calling into a channel that
already holds a spirit mints a *different* one rather than handing back
the same row — and the newcomer gets its own signature, since the
column is unique and the original still holds the base string.
"""
# Scoped limiters: the module-level ones are shared singletons that
# accumulate across the whole session, and this test summons more than
# once. Without this it silently eats the per-IP budget that
# test_summon_rate_limited_* depends on, making *those* tests hang
# waiting for an entity frame that was rate-limited away.
monkeypatch.setattr(app.ws, "summon_limiter", RateLimiter(max_requests=100, window_seconds=60))
monkeypatch.setattr(
app.ws, "summon_ip_limiter", RateLimiter(max_requests=100, window_seconds=60)
)
_login(sync_client, "channel-crosser")
anomalies = [
{"type": "anomaly", "source": "radio", "frequency": 88.0 + i, "magnitude": 9.0 + i}
for i in range(4)
]
monkeypatch.setattr(app.ws, "RETURN_CHANCE", 1.0)
with _ws_connect(sync_client, sync_client.cookies.get("qm_session")) as ws:
_read_until(ws, "session")
for anomaly in anomalies:
ws.send_json(anomaly)
first = _read_until(ws, "entity")["entity"]
# Same room, same anomalies — but this time nothing familiar picks up.
monkeypatch.setattr(app.ws, "RETURN_CHANCE", 0.0)
with _ws_connect(sync_client, sync_client.cookies.get("qm_session")) as ws:
_read_until(ws, "session")
for anomaly in anomalies:
ws.send_json(anomaly)
second = _read_until(ws, "entity")
assert second["is_new"] is True
assert second["entity"]["id"] != first["id"]
@pytest.mark.asyncio
async def test_summon_rate_limited_per_account(sync_client, monkeypatch):
# Swap in a tight, test-scoped limiter so this doesn't depend on (or
@@ -320,7 +379,7 @@ async def test_summon_high_rarity_item_drop_is_persisted_and_sent(
monkeypatch.setattr(app.ws, "summon_limiter", RateLimiter(max_requests=1000, window_seconds=60))
class MythicSpiritService:
async def mint_profile(self, signature, channel, anomalies, language="en"):
async def mint_profile(self, signature, channel, anomalies, language="en", entropy=None):
profile = fallback_profile(signature)
profile["rarity"] = "mythic"
return profile
@@ -384,7 +443,7 @@ async def test_summon_common_rarity_does_not_roll_a_drop(sync_client, db_session
return profile
class CommonSpiritService:
async def mint_profile(self, signature, channel, anomalies, language="en"):
async def mint_profile(self, signature, channel, anomalies, language="en", entropy=None):
return _common_only(signature)
async def fragment(self, source, anomaly, language="en"):