Skip to content

Tag accumulation bug: stripTagPrefix only removes single tag causing excessive §N§ repetition #11

Description

@tomolom

Bug Description

The stripTagPrefix function in packages/plugin/src/hooks/magic-context/tag-content-primitives.ts only strips a single tag prefix, but the transform pipeline can process the same content multiple times (e.g., during compartment compaction, message replay, or transform re-runs). This causes tags to accumulate, leading to unreadable messages with excessive §N§ symbols like:

§4687§ §4687§ §4687§ §4687§ ... §4687§ actual content

Root Cause

const TAG_PREFIX_REGEX = /^§\d+§\s*/;  // No global flag, only matches ONE tag

export function stripTagPrefix(value: string): string {
  return value.replace(TAG_PREFIX_REGEX, "");  // Only strips first tag!
}

export function prependTag(tagId: number, value: string): string {
  const stripped = stripTagPrefix(value);  // Still has old tags
  return ${tagId}§ ${stripped}`;  // Prepends new tag to remaining tags
}

Each transform pass:

  1. Calls prependTag() which strips only the outermost tag
  2. Leaves previously accumulated tags intact
  3. Prepends a new tag on top
  4. Result: tag stack grows unbounded

Proposed Fix

Change the regex to match all consecutive tags at the start:

const TAG_PREFIX_REGEX = /^(?:§\d+§\s*)+/;  // Match one or more tags

export function stripTagPrefix(value: string): string {
  return value.replace(TAG_PREFIX_REGEX, "");
}

This ensures stripTagPrefix removes all accumulated tags, so prependTag always results in exactly one tag as intended.

Impact

  • Messages become increasingly difficult to read as tags accumulate
  • Affects long-running sessions or those with frequent compartment operations
  • No data loss, but severe UX degradation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions