Skip to content

Make redactAndTruncate line-boundary aware#19

Merged
inhaq merged 2 commits into
mainfrom
pi-structure-aware-truncation
Jun 15, 2026
Merged

Make redactAndTruncate line-boundary aware#19
inhaq merged 2 commits into
mainfrom
pi-structure-aware-truncation

Conversation

@inhaq

@inhaq inhaq commented Jun 15, 2026

Copy link
Copy Markdown
Owner

This pull request was created by @kiro-agent on behalf of @inhaq 👻

Comment with /kiro fix to address specific feedback or /kiro all to address everything.
Learn about Kiro Web


Closes out the final Tier 1 item from the pi-harness deep-dive: structure-aware truncation.

Problem

redactAndTruncate (used for every diff and mechanical-gate log placed in a critic artifact bundle) did a blunt slice: first 70% + last 20% of characters. On the line-structured content the critic actually reasons over — unified diffs, build/test output — this routinely cut mid-line and mid-token, degrading the signal the critic sees.

Change

Inspired by pi's harness truncate.ts, the head is now snapped back to the last complete line within budget and the tail forward to the next line start, so retained content is always whole lines. The elision marker reports both omitted chars and omitted lines. A single very long line with no boundaries falls back to the original hard char slice.

Redaction still runs over the full input first, so secrets in the retained head/tail stay masked — unchanged.

Tests (+3)

  • multi-line input truncates on line boundaries (every retained line complete, a middle line dropped, omitted-line count reported);
  • a single very long line falls back to the char slice;
  • secrets are still redacted in the retained head/tail after truncation.

Existing redaction/truncation tests unchanged and green. npm run typecheck clean; full suite 176 tests pass.

Context

With this merged, Tier 1 and Tier 2 of the deep-dive are complete:

Independent of #17/#18 (touches only redaction.ts); branched off main.

Summary by CodeRabbit

  • Bug Fixes
    • Improved redaction truncation to be structure-aware: it now truncates at natural line boundaries when possible rather than cutting mid-line.
    • The elision marker now reports omitted content more clearly, including omitted line count when applicable (in addition to character omission).
  • Tests
    • Added coverage for multi-line truncation on line boundaries, a long single-line fallback behavior, and confirmation that secrets remain redacted within the retained portions.

Replace the blunt 70%/20% head/tail char slice with structure-aware
truncation (inspired by pi's harness truncate.ts): when the content has
line structure (diffs, build/test logs), snap the head back to the last
complete line and the tail forward to the next line start, so the critic
never reasons over a line cut mid-token. The elision marker now reports
both omitted chars and omitted lines. A single very long line with no
boundaries falls back to the original hard char slice.

Redaction still runs over the full input first, so secrets in the
retained head/tail remain masked.

Tests (+3): multi-line boundary truncation (all retained lines complete,
middle dropped, line count reported), single-long-line char-slice
fallback, and secret redaction preserved through truncation.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ab8ba472-f3d8-49a8-ac92-531af4838472

📥 Commits

Reviewing files that changed from the base of the PR and between e2817b8 and 8516677.

📒 Files selected for processing (1)
  • src/engine/redaction.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/engine/redaction.ts

📝 Walkthrough

Walkthrough

redactAndTruncate is updated to trim truncated head/tail segments to line boundaries, count omitted lines in addition to characters, and emit an enriched elision marker. Three new Vitest cases validate multi-line truncation, single-long-line fallback, and secret redaction within truncated output.

Changes

Newline-aware truncation in redactAndTruncate

Layer / File(s) Summary
Newline-aware truncation implementation
src/engine/redaction.ts
JSDoc updated; implementation replaced from fixed char-budget slicing to 70%/20% head/tail budgets snapped to the nearest newline, omitted character and line count computation, enriched elision marker format ...[N chars M lines truncated]..., and a no-op path when budgets overlap.
Truncation behavior tests
test/redaction.test.ts
Three new Vitest cases added: multi-line string truncation keeps complete first/last lines and reports omitted lines in the marker; single very long line falls back to a hard character slice with bounded output length; secret values are still redacted within the retained head/tail segments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Snip, snip! The long lines grow too grand,
So I trim them neatly, head to tail on land.
The secrets hide behind a [REDACTED] veil,
The omitted lines counted without fail.
A tidy ellipsis marks what's cut away—
The rabbit hops on through the truncated day! ✂️

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Make redactAndTruncate line-boundary aware' directly and accurately describes the main change: the function now respects line boundaries during truncation instead of cutting mid-line.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch pi-structure-aware-truncation

Comment @coderabbitai help to get the list of available commands and usage tips.

@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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/redaction.test.ts (1)

44-79: ⚡ Quick win

Add regression tests for newline-at-boundary edge cases.

Please add explicit cases where truncation encounters newline at exact boundary positions (head newline at boundary start, tail newline at tail end) so whole-line retention is guaranteed in those edge inputs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/redaction.test.ts` around lines 44 - 79, Add new test cases to
test/redaction.test.ts that specifically verify newline-at-boundary edge cases
for the redactAndTruncate function. Create test cases where truncation
encounters newlines at exact boundary positions: one where the head segment ends
exactly at a newline character, and another where the tail segment starts
exactly at a newline character. These tests should verify that whole lines are
retained without mid-line cuts in these edge scenarios, ensuring the function
handles boundary alignment correctly when newlines coincide with truncation
boundaries.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/engine/redaction.ts`:
- Around line 101-107: The boundary guard conditions in the head and tail
trimming logic are excluding valid newline positions. Change the condition at
line 102 from `headNl > 0` to `headNl >= 0` to include the case where the
newline is at position 0 (the start of the head string). Similarly, change the
condition at line 107 from `tailNl < tail.length - 1` to `tailNl <= tail.length
- 1` to include the case where the newline is at the very end of the tail
string. These adjustments ensure that whole-line boundaries are properly
respected even in edge cases.

---

Nitpick comments:
In `@test/redaction.test.ts`:
- Around line 44-79: Add new test cases to test/redaction.test.ts that
specifically verify newline-at-boundary edge cases for the redactAndTruncate
function. Create test cases where truncation encounters newlines at exact
boundary positions: one where the head segment ends exactly at a newline
character, and another where the tail segment starts exactly at a newline
character. These tests should verify that whole lines are retained without
mid-line cuts in these edge scenarios, ensuring the function handles boundary
alignment correctly when newlines coincide with truncation boundaries.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: aa622b82-9417-480d-ac74-93085ab9ce19

📥 Commits

Reviewing files that changed from the base of the PR and between e9052a7 and e2817b8.

📒 Files selected for processing (2)
  • src/engine/redaction.ts
  • test/redaction.test.ts

Comment thread src/engine/redaction.ts Outdated
headNl > 0 left a partial line when the only newline was at position 0;
the tail guard kept a partial leading line when the newline sat at the
very end. Use headNl >= 0 and tailNl >= 0 so the whole-line guarantee
holds in those edge cases.

Addresses CodeRabbit review on PR #19.

Co-authored-by: Inzimam Ul Haq <27832433+inhaq@users.noreply.github.com>
@inhaq inhaq merged commit ed29814 into main Jun 15, 2026
8 checks passed
@inhaq inhaq deleted the pi-structure-aware-truncation branch June 15, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant