Skip to content

refactor: ♻️ Consolidate label clean and host bypass - #1650

Merged
evaline-ju merged 3 commits into
rossoctl:mainfrom
evaline-ju:duplicate-refactor
Nov 3, 2026
Merged

evaline-ju merged 3 commits into
rossoctl:mainfrom
evaline-ju:duplicate-refactor

Conversation

@evaline-ju

@evaline-ju evaline-ju commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replaces the duplicated label-cleaning and bypass-host matching code in the cost packages, agentop and four plugins with one shared helper each. Labels now look the same on screen as in storage, and bypass_hosts in ibac, sparc and cpex now ignores case and handles IPv6 hosts, as lineage already did

Related issue(s)

Closes #1581

Summary by CodeRabbit

  • Bug Fixes
    • Labels containing control characters are now sanitized consistently across usage displays and cost records. Long labels are shortened without splitting multi-byte characters, while retaining the existing byte limit.
    • Configured host bypass patterns are now matched consistently across outbound request checks, including case variations and hosts with ports. Empty hosts do not match bypass patterns.
  • Improvements
    • These updates make displayed labels and host bypass behavior more predictable across the app.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 20 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 22d2fe0c-fbd0-4581-813b-760b2393eb3e
Commits

Reviewing files that changed from the base of the PR and between b64725a and 397c3f5.

Files selected for processing: 10
  • core/cost/ledger/query_test.go
  • core/cost/ledger/row.go
  • core/cost/ledger/store.go
  • core/cost/usage/usage.go
  • core/pipeline/client.go
  • core/plugins/internal/hostmatch/hostmatch.go
  • core/plugins/internal/hostmatch/hostmatch_test.go
  • docs/cpex-plugin.md
  • docs/ibac-plugin.md
  • docs/sparc-plugin.md
Walkthrough

The changes add shared pipeline helpers for control-rune sanitization and byte-limited label truncation, then use them in agentop and cost packages. They also add shared host-pattern matching and replace local matchers in four plugins.

Changes: Shared Label Helpers

Layer / File(s) Summary
Pipeline label helpers
core/pipeline/client.go, core/pipeline/client_test.go, core/pipeline/subagent.go, core/pipeline/subagent_test.go
Pipeline exports control-rune detection and byte-limited, rune-boundary truncation. capUA and subagent validation use the exported helpers.
Agentop and cost label integration
cmd/agentop/*, core/cost/ledger/*, core/cost/usage/*
Agentop and cost label paths use the pipeline sanitizer and truncator. Related assertions use pipeline.HasControlRunes; truncation tests now cover the shared helper.

Shared Host Matching

Layer / File(s) Summary
Shared host matcher
core/plugins/internal/hostmatch/*
Adds MatchesAny for case-insensitive host-pattern matching, with port handling and tests for host patterns and IPv6.
Plugin bypass integration
core/plugins/{cpex,ibac,lineage,sparc}/*
CPEX, IBAC, LineageTelemetry, and SPARC use hostmatch.MatchesAny for bypass checks and remove their local matchers.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Suggested reviewers: huang195, mrsabath

Merge Risk: Low · previous review b6472

Configured IPv6 bypass rules can be missed for portless bracketed hosts, causing requests to undergo plugin processing instead. The impact is bounded; correct host normalization or explicitly accept this limitation before merging.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title accurately and concisely identifies the two main changes: consolidating label cleaning and host bypass matching.
Linked Issues check Passed Directly linked issue #1581 has two coding objectives. Both are implemented at the reviewed head.

(1) Label sanitization and truncation [#1581]: core/pipeline/client.go now exports the shared rune …
Out of Scope Changes check Passed Every changed file maps to the consolidation work in #1581 or to a direct caller update.

- Label helpers: the removed duplicates in usage.go, row.go, and usage_render.go, and the unicode/utf8…
Docstring Coverage Passed Docstring coverage is 92.31% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
Full details: Linked Issues check

Explanation

Directly linked issue #1581 has two coding objectives. Both are implemented at the reviewed head.

(1) Label sanitization and truncation [#1581]: core/pipeline/client.go now exports the shared rune rules. IsControlRune is the single predicate, HasControlRunes is the scan, and SanitizeControlRunes is the rewrite. It also adds TruncateLabel(s, n), a byte-capped, rune-boundary truncation. core/cost/usage/usage.go and core/cost/ledger/row.go drop their duplicate helpers and delegate to the pipeline package. cmd/agentop/tui/usage_render.go and cmd/agentop/cmd_pipeline.go now use pipeline.SanitizeControlRunes, so the rendered and stored control-rune handling match. The issue asks for SanitizeLabel and TruncateLabel. The PR exports SanitizeControlRunes instead. This is a naming difference and does not change the requirement. Each package keeps its policy comments and thin wrappers, as the issue requests. Test coverage: TestTruncateLabel_CutsOnTheLastRuneBoundaryAtOrBelowTheCap is added in core/pipeline/client_test.go. The usage-package truncation test is removed, and its behavior is covered by the new pipeline test.

(2) Host matching [#1581]: core/plugins/internal/hostmatch/hostmatch.go adds MatchesAny. It returns false for an empty host, strips the port with net.SplitHostPort (so IPv6 literals survive), lowercases host and patterns, and applies path.Match. cpex, ibac, lineage, and sparc call it and remove their local matchesAnyHost copies. The base sparc/collect.go copy used strings.IndexByte, which truncated [::1]:4317 to [. The shared helper fixes that case. TestMatchesAny in hostmatch_test.go covers wildcards, ports, case, exact match, IPv6, and empty input.

No unmet coding requirement is found. The issue states its four host copies are identical. The sparc copy differed slightly, but the consolidation target is the single shared helper the issue requests.

Full details: Out of Scope Changes check

Explanation

Every changed file maps to the consolidation work in #1581 or to a direct caller update.

  • Label helpers: the removed duplicates in usage.go, row.go, and usage_render.go, and the unicode/utf8 imports they no longer need, are part of the requested deduplication. Caller updates in agents_pane.go, store.go, writer.go, query_test.go, usage_test.go, subagent.go, subagent_test.go, and client_test.go rename or repoint calls to the exported helpers and update comments.
  • Host matching: cpex/plugin.go, ibac/plugin.go, lineage/plugin.go, lineage/config.go, sparc/collect.go, and sparc/plugin.go switch to hostmatch.MatchesAny. The sparc host_bypass outcome is unchanged.

The PR introduces three behavior changes. Each follows from the consolidation target and is disclosed in the PR description. (a) bypass_hosts matching in ibac, sparc, and cpex becomes case-insensitive and IPv6-aware. The shared helper already had these properties, and the issue asks for one helper for all four copies. (b) agentop render-time sanitization now replaces the full C1 and bidi set, not only C0 and DEL. The issue names this as the intended stored-versus-rendered consistency. (c) Invalid UTF-8 bytes are now replaced in agentop labels. This follows from SanitizeControlRunes. No change was found that lacks a connection to #1581.

Full details: Docstring Coverage

Explanation

Docstring coverage is 92.31% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 13 functions across 20 files.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7fe16c3d-f8de-47ab-8012-39e85803c528
Commits

Reviewing files that changed from the base of the PR and between 3141461 and b64725a.

Files selected for processing: 21
  • cmd/agentop/cmd_pipeline.go
  • cmd/agentop/tui/agents_pane.go
  • cmd/agentop/tui/usage_render.go
  • core/cost/ledger/query_test.go
  • core/cost/ledger/row.go
  • core/cost/ledger/store.go
  • core/cost/ledger/writer.go
  • core/cost/usage/usage.go
  • core/cost/usage/usage_test.go
  • core/pipeline/client.go
  • core/pipeline/client_test.go
  • core/pipeline/subagent.go
  • core/pipeline/subagent_test.go
  • core/plugins/cpex/plugin.go
  • core/plugins/ibac/plugin.go
  • core/plugins/internal/hostmatch/hostmatch.go
  • core/plugins/internal/hostmatch/hostmatch_test.go
  • core/plugins/lineage/config.go
  • core/plugins/lineage/plugin.go
  • core/plugins/sparc/collect.go
  • core/plugins/sparc/plugin.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@coderabbitai coderabbitai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • Fix comments: Fix CodeRabbit comments on this PR

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

This is a good consolidation. The four matchesAnyHost copies become internal/hostmatch.MatchesAny, using the lineage variant that case-folds and handles IPv6. The ledger, usage and agentop label sanitisers and truncators now all call pipeline.SanitizeControlRunes, pipeline.HasControlRunes and pipeline.TruncateLabel. I checked the behaviour of each removed copy against what replaces it:

  • Ledger and usage sanitizeLabel: the logic was byte-for-byte the same as SanitizeControlRunes, including the fast path that skips allocation and the handling of invalid UTF-8. Nothing changes.
  • agentop tui.sanitizeLabel: it had an open-coded C0/C1/DEL/bidi switch plus IsControlRune, and now uses the same set. As a bonus, clean strings no longer allocate.
  • agentop sanitizeCell: this widens from C0/DEL only to the full set. That is the intended fix, and the C0 assertions in configure_status_test.go still hold.
  • truncateLabel and capUA: these now call TruncateLabel(s, n) with the same walk-back. The new pipeline test pins the exact cut length for 2-, 3- and 4-byte runes and for U+FFFD, and the 1-byte prefix is kept. That is tighter than the usage test it replaces.
  • Host bypass in ibac, sparc and cpex: the old code cut at the first :, so any IPv6 literal became [. Matching now uses net.SplitHostPort and lower-cases both sides. Case-folding only makes the bypass match the same DNS name the request already reaches, so it does not widen what an attacker can reach. cpex and lineage keep their outbound-only gating.

No must-fix issues. There is one inline suggestion, plus two small nits that fall outside the diff hunks:

  • core/pipeline/client.go:185: the IsControlRune doc still says "The ledger's copy will carry the same clauses in the same order." After this PR the ledger has no copy, so that sentence is stale.
  • The commit subject :recycle: Consolidate label clean and host bypass has no conventional prefix (the PR title does). This is advisory only.
  • Optional: docs/ibac-plugin.md, docs/sparc-plugin.md and docs/cpex-plugin.md could say that bypass_hosts matching is now case-insensitive and IPv6-aware, as docs/lineage-wire-contract.md already does.

Author: evaline-ju (CONTRIBUTOR association; repo permission admin, so maintainer)
Areas reviewed: Go (core/pipeline, core/cost/{ledger,usage}, core/plugins/{ibac,sparc,cpex,lineage}, new core/plugins/internal/hostmatch, cmd/agentop), tests, security
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, signed off: yes
CI status: passing. CodeRabbit and Go CI (core peerproc, macOS) were still pending when I reviewed.

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

I re-reviewed at f0b7dce. The follow-up commit deals with everything from my earlier review of b64725a:

  • Bracketed IPv6 with no port: hostmatch.MatchesAny now strips the brackets when net.SplitHostPort fails, so [::1] matches ::1. The len(host) > 1 guard keeps a lone [ safe, and the {"[::1]", true} case covers it in the test table. This does not widen the bypass. The same input with a port ([x]:p) already had its brackets dropped by SplitHostPort, so the portless form now behaves the same way.
  • Stale doc sentence: the "ledger's copy" sentence is gone from the IsControlRune doc in core/pipeline/client.go. The remaining "ledger's copy" mentions in core/cost/usage mean the stored label data, not a duplicate of the code, so they are still accurate.
  • Plugin docs: docs/ibac-plugin.md, docs/sparc-plugin.md and docs/cpex-plugin.md now say that bypass_hosts matching is case-insensitive and strips the port and IPv6 brackets. This matches the implementation and docs/lineage-wire-contract.md.

On this head, go test passes locally for core/plugins/{internal/hostmatch,ibac,sparc,lineage}, core/pipeline, core/cost/... and cmd/agentop/tui, and go vet and gofmt are clean. Two cmd/agentop tests fail here, TestServiceInstall_RefusesABrokenConfig and TestReportSessionInterruption. They fail the same way on main (3141461), because this sandbox has no systemd user bus and can't inspect loopback connections. They are not related to this PR.

No must-fix issues and no new inline comments. One advisory nit remains: the commit subjects (:recycle: ..., :memo::goal_net: ...) have no conventional prefix. The PR title does, and that is the part that gets checked.

Author: evaline-ju (CONTRIBUTOR association; repo permission admin, so maintainer)
Areas reviewed: Go (core/plugins/internal/hostmatch, core/pipeline, core/cost/{ledger,usage}, core/plugins/{ibac,sparc,cpex,lineage}, cmd/agentop), tests, docs, security
Agent/IDE config (.claude/.vscode): none
Commits: 2 commits, all signed off: yes (DCO passing)
CI status: pending. DCO, PR title, action pinning, Hadolint and Dependency Review pass. Go CI, CodeQL and the image builds were still running when I reviewed.

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

I re-reviewed at 397c3f5. The only change since f0b7dce, which I approved, is :bulb: Consolidate sanitize spelling. It is 8 comment-only line changes in five files: sanitised/sanitiser/sanitises/serialised/recolours become the -ize/-or forms in core/cost/ledger/{row,store,query_test}.go, core/cost/usage/usage.go and core/pipeline/client.go. No code, identifiers, test names or strings changed, so the behaviour I checked in the earlier reviews still holds.

One optional nit is inline. The sweep only covers the lines this PR already touched, so the same comment blocks still mix both spellings (for example Sanitising in rowLabel's doc, two lines below the corrected sanitized). A follow-up or a codespell rule would finish the job. It doesn't block this PR.

Author: evaline-ju (CONTRIBUTOR association; repo permission admin, so maintainer)
Areas reviewed: Go comments in core/cost/{ledger,usage} and core/pipeline (incremental f0b7dce..397c3f5); full-PR re-scan for agent/IDE config and security
Agent/IDE config (.claude/.vscode): none
Commits: 3 commits, all signed off: yes (DCO passing)
CI status: passing. Every required check is green, including Go CI, CodeQL, Trivy and the image builds. Go CI (core peerproc, macOS) was still pending when I reviewed, and Spellcheck was skipped.

Comment thread core/cost/ledger/row.go
// CWE-150. This is the primary guard for Endpoint, Model and Provenance, and defence in
// depth for Agent, which pipeline.ParseUserAgent already sanitized.
//
// THE ORDER MATTERS. Sanitising can triple a string's length — every replaced byte becomes

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: The comment above now says sanitized, but this line still says Sanitising. The same split appears in usage.go (ringLabel's sanitised, then capped / sanitising can triple) and in client.go (sanitised (see, the sanitiser). This is optional: either finish the sweep in these doc blocks or leave it for a repo-wide codespell pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I purposely only updated lines that were already going to be touched by the PR, not existing other comments

@evaline-ju
evaline-ju merged commit 027ec2b into rossoctl:main Nov 3, 2026
59 of 60 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 3, 2026
@evaline-ju
evaline-ju deleted the duplicate-refactor branch November 3, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Core: duplicated leaf helpers — label sanitization and host matching

2 participants