Skip to content

fix(gateway): allow [[as_document]] glued directly to MEDIA path - #63644

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-63632
Closed

fix(gateway): allow [[as_document]] glued directly to MEDIA path#63644
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-63632

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a regex bug in MEDIA_TAG_CLEANUP_RE that caused MEDIA tags to be silently ignored when a directive like [[as_document]] was concatenated directly to the file extension without whitespace.

When the regex failed to match, the MEDIA tag was not stripped and the file path was not extracted, resulting in the file not being delivered while the gateway reported success (no error surfaced).

Root cause

The lookahead character class [\s"',;:)}]|$)inMEDIA_TAG_CLEANUP_REdid not include[. When [[as_document]]immediately followed the file extension (e.g.,MEDIA:/home/user/report.xlsx[[as_document]]), the lookahead assertion failed because [` was not a permitted boundary character.

Fix

Add \[ to the lookahead character class so it becomes [\s"',;:)}[]|$)`. This allows directives to follow the path without whitespace.

This change is safe because [ characters (specifically [[as_document]] and [[as_image]]) are already stripped elsewhere in the same file via a literal .replace("[[as_document]]", "") call.

Related Issue

Fixes #63632

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • gateway/platforms/base.py: Added \[ to the lookahead character class in MEDIA_TAG_CLEANUP_RE
  • tests/gateway/test_media_tag_cleanup.py: New test file with 3 regression tests

How to Test

Run the new regression tests:

pytest tests/gateway/test_media_tag_cleanup.py -v

Observed result: All 3 tests pass, confirming that:

  1. MEDIA:/home/hermes/report.xlsx[[as_document]] now matches (fixes the issue)
  2. MEDIA:/home/hermes/report.xlsx [[as_document]] still matches (baseline unchanged)
  3. MEDIA:/tmp/file.docx at end of string still matches (baseline unchanged)

You can also verify the fix with a quick Python REPL session:

from gateway.platforms.base import MEDIA_TAG_CLEANUP_RE

# Issue case: [[as_document]] glued directly
text = "Готово. MEDIA:/home/hermes/report.xlsx[[as_document]]"
assert MEDIA_TAG_CLEANUP_RE.search(text) is not None  # Now passes
stripped = MEDIA_TAG_CLEANUP_RE.sub("", text)
assert "MEDIA:" not in stripped  # Tag is removed
assert "/home/hermes/report.xlsx" not in stripped  # Path is removed

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/gateway/test_media_tag_cleanup.py -v and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 15.2

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

…sResearch#63632)

MEDIA_TAG_CLEANUP_RE failed to match when a directive like [[as_document]]
was concatenated directly to the file extension without whitespace
(e.g., MEDIA:/home/user/report.xlsx[[as_document]]). This caused the
file to be silently not delivered while the gateway reported success.

Root cause: the lookahead character class [\s`",;:)\}\]|$) did not
include [, so [[as_document]] immediately after the extension broke
the lookahead assertion.

Fix: add \[ to the lookahead class so directives can follow the path
without whitespace. This is safe because [ is already stripped elsewhere
in the same file via .replace("[[as_document]]", "").

Added regression tests covering:
- Directive glued to extension (issue case)
- Directive with whitespace (baseline)
- Tag at end of string ($ anchor)

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for isolating a real media-extraction edge case. The premise still holds on current main: extract_media() scans the original response at gateway/platforms/base.py:3684-3687, while MEDIA_TAG_CLEANUP_RE at gateway/platforms/base.py:1503 does not currently accept [ after the extension.

Problems

  • The proposed [ boundary at gateway/platforms/base.py:1495 is broader than the supported directive grammar. It also accepts arbitrary bracket-bearing filenames, allowing the regex to match a valid-extension prefix rather than leaving an unrecognized path intact.
  • tests/gateway/test_media_tag_cleanup.py tests only MEDIA_TAG_CLEANUP_RE. The delivery path is BasePlatformAdapter.extract_media() (gateway/platforms/base.py:3637-3731), and current code supports [[as_document]], not [[as_image]] (gateway/platforms/base.py:1542, 3667-3671).

Suggested changes

  • Match the literal [[as_document]] boundary rather than a generic [, and add a production-path regression test in tests/gateway/test_platform_base.py asserting both extraction and cleaned output.

Automated hermes-sweeper review.

Comment thread gateway/platforms/base.py
r'''(?P<path>`[^`\n]+`|"[^"\n]+"|'[^'\n]+'|'''
r'''(?:~/|/|[A-Za-z]:[/\\])\S+(?:[^\S\n]+\S+)*?\.(?:''' + _MEDIA_EXT_ALTERNATION + r'''))'''
r'''(?=[\s`"',;:)\]}]|$)[`"']?''',
r'''(?=[\s`"',;:)\]}\[]|$)[`"']?''',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restrict this new boundary to the literal supported [[as_document]] directive rather than any [. A generic bracket boundary can make MEDIA:/safe/report.xlsx[revision] match and deliver the .xlsx prefix, whereas the current anchored parser leaves that unrecognized path intact.

assert "MEDIA:" not in stripped
assert "/home/hermes/report.xlsx" not in stripped

# Other directives ([[as_image]]) should also work

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[[as_image]] is not a supported directive on current main: cleanup only strips [[audio_as_voice]] and [[as_document]]. Please remove this case or implement that directive separately; add an extract_media() regression for the supported glued [[as_document]] form instead.

@teknium1 teknium1 added sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 16, 2026
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery and removed sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data labels Jul 16, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #72170 (rebase-merge) — your commit was cherry-picked onto current main with your authorship preserved in git history. Thanks for the fix!

PR #72170 consolidated six open MEDIA-delivery fixes (formatting-variant regex misses + the Discord upload race) into one salvage wave so the overlapping regex changes could be resolved together.

@teknium1 teknium1 closed this Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MEDIA_TAG_CLEANUP_RE fails to match when [[as_document]] is glued directly to the path — file silently not delivered

3 participants