ego_ablation

Per-channel ego-ablation (non-dual pronoun) mode backed by Redis.

Stores a single per-channel toggle in Redis under star:ablate_ego: keys that, when active, makes the bot suppress singular first-person pronouns in its output. The module exposes the key builders (redis_key(), redis_key_for_channel_key()), the read/write pair (is_active(), set_active()), and merge_into_room_context_if_missing(), which backfills the active flag and the EGO_ABLATION_DIRECTIVE text into a prompt room-context dict. Reads merge across Discord family channel-key aliases and honour a module killswitch, a config-level global disable, and per-user absolute bypasses (via feature_toggles); the directive itself is injected into the SpiritGraph and subagent system prompts elsewhere in the codebase.

ego_ablation.redis_key(platform, channel_id)[source]

Build the Redis flag key for a single platform:channel_id pair.

Returns the canonical star:ablate_ego:{platform}:{channel_id} string key under which the per-channel ego-ablation toggle is stored. This is the exact-prefix key written by set_active(); reads go through redis_key_for_channel_key() instead so they can also match Discord family aliases. This is a pure string formatter that performs no I/O.

Called only by set_active() in this module.

Parameters:
  • platform (str) – The platform prefix (e.g. discord, matrix).

  • channel_id (str) – The channel/room identifier on that platform.

Returns:

The Redis key star:ablate_ego:{platform}:{channel_id}.

Return type:

str

ego_ablation.redis_key_for_channel_key(channel_key)[source]

Build the Redis flag key from an already-composed platform:channel_id key.

Returns star:ablate_ego: prefixed onto a pre-joined platform:channel_id composite. This variant exists so the read path in is_active() can build a key for each Discord family alias produced by discord_family_channel_key_variants() without re-splitting the composite. This is a pure string formatter that performs no I/O.

Called only by is_active() in this module (once per alias variant).

Parameters:

channel_key (str) – A composite platform:channel_id identifier.

Returns:

The Redis key star:ablate_ego:{channel_key}.

Return type:

str

async ego_ablation.is_active(redis, platform, channel_id, user_id='', config=None)[source]

Report whether ego-ablation mode is active for a channel right now.

Resolves the effective per-channel toggle, honouring every layer of suppression in order: the module-level enabled killswitch, the ego_ablation_global_disabled config flag, and the per-user/per-channel absolute bypass via feature_toggles.is_absolute_bypass(). If none of those short-circuit, it reads Redis for each Discord family alias of the channel (via discord_family_channel_key_variants() and redis_key_for_channel_key()) and treats any truthy stored value (1/true/yes/on) as active, so a flag set under one Discord channel-key prefix still applies across that channel’s known aliases. Per-key Redis GET failures are logged at debug level and skipped rather than raising. This is the read counterpart to set_active().

Called by merge_into_room_context_if_missing() in this module and by the prompt-context assembly in prompt_context.py (the ego-ablation branches around lines 2746 and 2834).

Parameters:
  • redis (Redis) – Async Redis client used for the per-alias GET lookups.

  • platform (str) – The platform prefix for the channel being checked.

  • channel_id (str) – The channel/room identifier on that platform.

  • user_id (str) – The sender’s user id, consulted only for the absolute bypass check; empty by default.

  • config (Config | None) – Optional config providing the global-disable flag and bypass rules; when None those checks are skipped.

Returns:

True if ego ablation should apply to this channel, else False.

Return type:

bool

async ego_ablation.set_active(redis, platform, channel_id, active)[source]

Persist the ego-ablation toggle for one channel under its exact prefix.

Writes (or clears) the per-channel flag in Redis using the exact platform:channel_id key from redis_key(). When active is true it stores 1; when false it deletes the key entirely (absence is read as inactive). Note the asymmetry with is_active(), which on read merges across Discord family aliases — this writer only ever touches the literal platform prefix it was given, so a toggle set on one alias is visible to reads of its siblings but is cleared only on the prefix written here.

Called by the message processor’s toggle handler in message_processor/processor.py (around line 3428).

Parameters:
  • redis (Redis) – Async Redis client used for the SET/DELETE.

  • platform (str) – The platform prefix for the channel being toggled.

  • channel_id (str) – The channel/room identifier on that platform.

  • active (bool) – True to enable ego ablation (store 1), False to disable it (delete the key).

Return type:

None

async ego_ablation.merge_into_room_context_if_missing(room_context, redis, platform, channel_id, user_id='', config=None)[source]

Backfill the ego-ablation flags into a room context dict if not already set.

Ensures room_context carries both ego_ablation_active (a bool) and ego_ablation_directive (the EGO_ABLATION_DIRECTIVE text when active, else an empty string) so the system-prompt template can render consistently even when the context was assembled by a minimal path that did not populate them. If ego_ablation_active is already present the dict is left untouched; otherwise the flag is resolved via is_active() (which reads Redis and applies all suppression layers) and both keys are written in place. The module killswitch and a missing Redis client both force the inactive defaults, and any unexpected error is swallowed (logged at debug) with the same inactive fallback so prompt assembly never fails on this.

Called by message_processor/generate_and_send.py (around line 826) and message_processor/channel_heartbeat.py (around line 300).

Parameters:
  • room_context (dict[str, Any]) – The prompt room-context dict, mutated in place to carry the two ego-ablation keys.

  • redis (Redis | None) – Async Redis client for the lookup; when None the inactive defaults are written without any I/O.

  • platform (str) – The platform prefix for the channel.

  • channel_id (str) – The channel/room identifier on that platform.

  • user_id (str) – The sender’s user id, forwarded to is_active() for the absolute-bypass check; empty by default.

  • config (Config | None) – Optional config forwarded to is_active().

Return type:

None