fix(config): clamp max_chars to a minimum of 1#9
Open
YashManek1 wants to merge 1 commit into
Open
Conversation
TRACEROOT_PLUGIN_MAX_CHARS=0 or a negative value passed the int() parse, so load_config() never fell back to the default. With max_chars=0, middle_truncate's half=0 made s[-half:] evaluate to s[-0:], which returns the whole string -- truncation silently disabled while the output still claimed text was elided. Negative values produced overlapping, corrupted slices instead. Clamp the config value and guard the half<=0 case in middle_truncate as a second line of defense. Closes traceroot-ai#8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
TRACEROOT_PLUGIN_MAX_CHARSset to0or a negative value silently disables span-text truncation instead of erroring or falling back — oversized span attributes can then hit backend/OTLP size limits and get dropped, and negative values produce corrupted, overlapping text.Root cause
load_config()only guards against non-integer input (except ValueError), not out-of-range integers —int("0")andint("-5")both succeed. Then inmiddle_truncate,max_chars=0makeshalf=0, ands[-half:]evaluates tos[-0:], which in Python returns the entire string. So the "truncated" output is actually the full text with a misleading "middle elided" marker glued on. Negativemax_charsproduces overlapping, corrupted slices.Change
Defense in depth, two layers:
config.py: clampmax_chars < 1to the 20000 default, same as the non-integer fallback.spans.py: guardmiddle_truncateagainsthalf <= 0directly, so any bad value that reaches it still degrades safely (elision marker only, no full-string leak).Validation
tests/test_spans.py: zero budget, negative budget, small positive budget (normal path), and twoload_configclamp tests.max_chars=0->half=0->s[-0:]== full string (confirms the bug);max_chars=-5->half=-3->s[3:](confirms overlap).uv run pytest tests/test_spans.py -q-> 26 passed, 0 failed.Risk / rollback
Additive-only change to a config-bounds check and a defensive guard; no behavior changes on the default/valid-range path. Revert is a straight
git revertof this commit.Closes #8
Summary by cubic
Clamp invalid
TRACEROOT_PLUGIN_MAX_CHARSvalues and hardenmiddle_truncateso truncation can’t be disabled or corrupt span text. This keeps span attributes within size limits and prevents OTLP drops.max_chars < 1to20000inload_config.middle_truncate, return only the elision marker when the budget is too small.Written for commit e829633. Summary will update on new commits.