Repository navigation
refactor(discord-bridge): single source of truth for [file:|send:|attach:] regex - #990
Conversation
…ach:] regex
The `[file:|send:|attach:]` marker regex was duplicated inline at THREE
sites:
- poll_results (channel replies)
- poll_proactive (owner DMs)
- poll_dm_fallback channel-redirect
Any future hardening to the pattern had to land in all three places
or one path would drift. The pre-existing pattern required absolute
paths (`/...` or `~/...`) because relative paths resolved against
the bridge's CWD, which differed between launchd-managed and bare-
shell runs.
## Changes
- `_FILE_MARKER_RE` — module-level compiled regex.
- `_split_file_markers(text) -> (clean_text, files)` — pure helper.
Three call sites now call it instead of re-defining the regex.
No behavior change. Refactor is byte-for-byte equivalent for valid
inputs; the only difference is that the regex compiles once at
module-load instead of per polling-loop iteration (minor perf win).
## Tests
`tests/discord-bridge-file-markers.test.py` (10 cases):
- common case (no markers), empty input, single-marker extraction
- `file` / `send` / `attach` keyword aliases
- absolute paths only — relative paths rejected
- multiple markers preserve textual order
- `[reply: <id>]` directive does NOT match the file pattern
- unknown keywords (`[path:]`, `[url:]`, etc.) don't silently extract
- ARCHITECTURAL: the regex body appears exactly once in the source
— guards against a future refactor accidentally re-introducing
an inline copy at a fourth call site
All 10 pass locally.
Co-Authored-By: Vasiliy's Sutando <noreply@anthropic.com>
|
LGTM. Refactor with a structural test, which is the right shape for "consolidate inline duplicates" — without the architectural assertion that the regex body appears exactly once in the source, this PR would just be cosmetic and the drift recurs on the next change. All three call sites converted (
|
…ach:] regex (sonichi#990) The `[file:|send:|attach:]` marker regex was duplicated inline at THREE sites: - poll_results (channel replies) - poll_proactive (owner DMs) - poll_dm_fallback channel-redirect Any future hardening to the pattern had to land in all three places or one path would drift. The pre-existing pattern required absolute paths (`/...` or `~/...`) because relative paths resolved against the bridge's CWD, which differed between launchd-managed and bare- shell runs. ## Changes - `_FILE_MARKER_RE` — module-level compiled regex. - `_split_file_markers(text) -> (clean_text, files)` — pure helper. Three call sites now call it instead of re-defining the regex. No behavior change. Refactor is byte-for-byte equivalent for valid inputs; the only difference is that the regex compiles once at module-load instead of per polling-loop iteration (minor perf win). ## Tests `tests/discord-bridge-file-markers.test.py` (10 cases): - common case (no markers), empty input, single-marker extraction - `file` / `send` / `attach` keyword aliases - absolute paths only — relative paths rejected - multiple markers preserve textual order - `[reply: <id>]` directive does NOT match the file pattern - unknown keywords (`[path:]`, `[url:]`, etc.) don't silently extract - ARCHITECTURAL: the regex body appears exactly once in the source — guards against a future refactor accidentally re-introducing an inline copy at a fourth call site All 10 pass locally. Co-authored-by: Vasiliy's Sutando <noreply@anthropic.com> Co-authored-by: Chi Wang <4250911+sonichi@users.noreply.github.com>
Why
The
[file:|send:|attach:]marker regex is duplicated inline at three sites indiscord-bridge.py:poll_resultspoll_proactivepoll_dm_fallbackThe pattern requires absolute paths (
/...or~/...) — relative paths previously resolved against CWD, which differed between launchd-managed and bare-shell runs. Any future hardening to the regex would need to land in all three places or one path would drift.Changes
_FILE_MARKER_RE— module-level compiled regex._split_file_markers(text) -> (clean_text, files)— pure helper. Three call sites now call it instead of re-defining the regex.No behavior change. Refactor is byte-for-byte equivalent for valid inputs; the only difference is that the regex compiles once at module-load instead of per polling-loop iteration (minor perf win).
Tests
tests/discord-bridge-file-markers.test.py— 10 cases pinning the contract:file/send/attachaliases~/...allowed[reply: 12345]does NOT match[path:]etc.) don't matchAll 10 pass locally.
Test plan
python3 tests/discord-bridge-file-markers.test.py— 10/10 pass[file: /tmp/sutando-x.png]marker → verify file delivered as attachment, marker removed from text🤖 Generated with Claude Code