mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(tag-protector): do not treat a slash in an unquoted attribute as self-closing (#4681)
## Description
`tag_protector` protects custom workflow tags from lossy compression.
Its attribute lexer
(`crates/headroom-core/src/transforms/tag_protector.rs`, the `b'/'` arm
of the open-tag loop, about line 374 on `12864fc8`) set `self_closing =
true` on every `/` and only cleared it on whitespace or a quote. An
unquoted attribute value holding a path, for example `<file
path=/src/main.rs>`, therefore reached `>` with `self_closing` still
set. The open tag was protected as a self-closing tag on its own, and
the body plus `</file>` went to the compressor unprotected, the outcome
the protector exists to prevent (the comment at
`headroom/transforms/content_router.py` around `protect_tags` explains
why tagged blocks must stay verbatim).
With this PR, a `/` makes the tag self-closing only when it is directly
followed by `>`, the `/>` pair. `<marker path=/a/b/>` still self-closes,
and the whitespace and quote handling is unchanged.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `crates/headroom-core/src/transforms/tag_protector.rs`: the `b'/'` arm
sets `self_closing = bytes.get(i + 1) == Some(&b'>')` instead of `true`,
with a comment.
- Same file, tests:
`unquoted_attribute_value_with_slash_is_not_self_closing` checks that
`<file path=/src/main.rs>body</file>` becomes one protected block with
no `body` or `</file>` left in the cleaned text, and that `<marker
path=/a/b/>` still self-closes.
## Testing
- [x] Unit tests pass (`pytest`) (Rust only: `cargo test -p
headroom-core --lib`; no Python changed)
- [x] Linting passes (`ruff check .`) (no Python changed; `cargo fmt
--check` and `cargo clippy -D warnings` on headroom-core instead)
- [ ] Type checking passes (`mypy headroom`) (no Python changed)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
# fail before (tag_protector.rs at 12864fc8 with only the new test added)
cargo test -p headroom-core --lib tag_protector: 31 passed; 1 failed
unquoted_attribute_value_with_slash_is_not_self_closing ... FAILED
# pass after
cargo test -p headroom-core --lib tag_protector: 32 passed; 0 failed
cargo test -p headroom-core --lib: 1034 passed; 0 failed; 1 ignored
cargo fmt -p headroom-core -- --check: clean
cargo clippy -p headroom-core --lib -- -D warnings: clean
```
## Real Behavior Proof
- Environment: before: Ubuntu (WSL2) on Windows 11, Python 3.12.15, the
`headroom._core` extension from a 2026-10-29 build (`tag_protector.rs`
has not changed since 2026-05-02, so it matches `12864fc8`); after:
Windows 11, Rust 1.95.0 (`rust-toolchain.toml`), headroom-core at
`12864fc8` plus this change.
- Exact command / steps: before: called the shipped binding
`_core.protect_tags(text, False)` on `<file
path=/src/main.rs>body</file>` and, as a control, on `<file
path="/src/main.rs">body</file>`, offline, on a copy of the extension;
after: `cargo test -p headroom-core --lib` with the new test, which
feeds the same input to the same function.
- Observed result: before, the unquoted input came back as cleaned
`{{HEADROOM_TAG_0}}body</file>` with the single block `<file
path=/src/main.rs>`, while the quoted control came back as one
placeholder covering the whole block; after, the unquoted input is
protected as one block (`<file path=/src/main.rs>body</file>`), the new
test passes and the rest of the headroom-core library tests pass.
- Not tested: the Python extension rebuilt from this branch (I could not
build the PyO3 wheel on this machine), and a full proxy request through
`ContentRouter` with a compressor that drops text.
## Runtime Rollout Safety
- Rollout-managed feature(s): None.
- Minimum rollout channel: N/A
- Stable/default behavior changed: Yes, narrowly: an open tag whose
unquoted attribute value contains `/` is no longer treated as
self-closing, so its whole block is protected.
- Kill switch / disable path: none added.
- Unsafe override required: No
- Qualification impact: None expected
- Rollback path: revert this commit.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
## Screenshots (if applicable)
N/A
## Additional Notes
A separate PR fixes a different defect in the same file (marker mode
drops the enclosing tag's frame on a nested close); the two changes do
not overlap.
Produced with AI assistance (Claude Code) operated by me (HetCreep). The
tests and the binding call were run on my machine against a clone of the
repository.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4681"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0ccd559db0
commit
d5644cbaca
@@ -372,7 +372,10 @@ fn parse_tag_at(bytes: &[u8], start: usize) -> TagParse {
|
||||
};
|
||||
}
|
||||
b'/' => {
|
||||
self_closing = true;
|
||||
// Only the `/>` pair closes the tag. A `/` inside an
|
||||
// unquoted attribute value (`path=/src/main.rs`) is an
|
||||
// ordinary character and must not make the tag self-closing.
|
||||
self_closing = bytes.get(i + 1) == Some(&b'>');
|
||||
i += 1;
|
||||
}
|
||||
b'"' | b'\'' => {
|
||||
@@ -879,6 +882,20 @@ mod tests {
|
||||
assert_eq!(restore_tags(&cleaned, &blocks), text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unquoted_attribute_value_with_slash_is_not_self_closing() {
|
||||
let text = "Before <file path=/src/main.rs>body</file> After";
|
||||
let (cleaned, blocks) = protect(text);
|
||||
assert_eq!(blocks.len(), 1, "{cleaned}");
|
||||
assert_eq!(blocks[0].1, "<file path=/src/main.rs>body</file>");
|
||||
assert!(!cleaned.contains("body"), "{cleaned}");
|
||||
assert!(!cleaned.contains("</file>"), "{cleaned}");
|
||||
// `/>` still self-closes, with or without an unquoted value before it.
|
||||
let (_cleaned, blocks) = protect("x <marker path=/a/b/> y");
|
||||
assert_eq!(blocks.len(), 1);
|
||||
assert_eq!(blocks[0].1, "<marker path=/a/b/>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restore_basic() {
|
||||
let original = "Before <system-reminder>Rule</system-reminder> After";
|
||||
|
||||
Reference in New Issue
Block a user