Skip to content

fix(channels): seal fences across a credential-graded rotation cut - #25701

Merged
bolichen97 merged 1 commit into
mainfrom
fix/rotation-recut-fence-and-uploads-14645
Oct 29, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/rotation-recut-fence-and-uploads-14645

Conversation

@CrysisDeu

@CrysisDeu CrysisDeu commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: a credential-graded rotation cut that lands inside a fenced code block still sends two pieces that each render as code, and on Discord a re-cut segment uploads no file.

When the splitter's own boundary severs a key, Telegram and Discord re-cut the segment at an offset safe_split_offset picks and send two raw source slices instead of the splitter's chunks. Two things go wrong when that offset falls inside an open fence:

  1. The head is sealed with an opener and no closer. Telegram escapes the code body as prose and Discord shows the backticks literally; the remainder's real closer then opens a block that never closes.
  2. On Discord, _segment_uploads_safe was computed for the splitter's boundary, not the re-cut one. With ![a](/p.png) inside a fenced block, a cut between the opener and the reference makes the reference read as real markup in the tail, so the final seal can upload a file the author wrote as code.

Why it matters

The first corrupts every fenced answer long enough to rotate where a key sits near the cut. The second uploads a local file from text the model quoted as code.

Not a goal

  • Changing where the credential grade cuts, or what counts as a sever.
  • The splitter's own fence handling at its own cuts, which already reopens with the original opener.

Intended? / Repro

Intended? No. The Opus 5 review of #13494 raised both as non-blocking findings and they were deliberately left out of that PR; the splitter's own cuts already repair fences the same way.

Repro (read on current main 4efafcf60f):

  • src/kiro_crew/telegram/renderer.py:799: chunks = [head, raw[offset:]] sends raw slices with no fence repair; the odd-fence fix-up above it ran on the splitter's chunks, which this line replaces.
  • src/kiro_crew/discord/renderer.py:515-531: candidate_sealed = [split_source[:offset]], tail = candidate_tail, with _segment_uploads_safe left as the earlier tier analysis set it.
  • Source "```python\n" + 60 code lines + "```\nAfter" over the budget, with the graded offset at line 20: the first Telegram bubble shows ```python and the code as plain text.

What changed (motivation → approach → change)

Symptom: broken fences and a stale upload verdict after a graded re-cut. Cause: the re-cut path skipped the splitter's repair and kept the splitter's verdict. Change: one shared repair, a re-grade, and a fresh verdict.

  • src/kiro_crew/messaging/split.py: new rebalance_fenced_cuts(pieces). For consecutive source slices, a piece that ends inside a fence gets that fence's closer on a new line, and the next piece is prefixed with the original opener line (run length and info string kept). Fence state comes from _advance over lines finished before the cut; the last piece is never closed, since it may still be streaming.
  • src/kiro_crew/messaging/split.py: new fence_line_safe_offset(text, offset). A cut that would split a fence delimiter line moves back to that line's start: when the part before the cut starts like a delimiter (up to three spaces, then a backtick or tilde), or a mid-line cut leaves the part after it starting like one. Every re-cut site applies it to the offset safe_split_offset returns, before the repair; the moved cut is then repaired and graded again like any other, and an answer of 0 withholds.
  • src/kiro_crew/telegram/renderer.py: the main re-cut measures the repaired head against the rendered cap inside its budget loop, grades the repaired pair again with severs_a_credential, and withholds (keeps the whole buffer) when it severs. _carve_graded repairs its pieces, holds each to the rendered cap, and grades the sequence as before.
  • src/kiro_crew/discord/renderer.py: the main re-cut shrinks its budget by what the repair adds, repairs head and tail (tail includes the held remainder), holds the head to the limit, and the existing re-grade reads the repaired pair. After a re-cut, _segment_uploads_safe = False for the whole segment, a held image outside every fence included. The presentation-branch carve repairs its pieces, holds every piece to the limit, and also marks the segment upload-ineligible.
  • docs/system-specs/modules/messaging.md: the rotation rule and the split.py row describe the repair and the upload rule.

Security

This sits next to the credential-split guard. Every path that sends repaired pieces grades exactly those pieces again and withholds on a sever, so the added delimiter lines can never ship ungraded. Withholding keeps all text in the buffer for the next rotation or the final seal. The upload change only removes uploads.

An independent read-only review (a fresh agent that saw only this diff and the issue, asked "can a credential now be split across two messages, or leak in a new way") found no blocking findings. Its two non-blocking notes are applied: the Discord presentation carve now holds the LAST piece to the limit too, and marks the segment upload-ineligible.

Backwards compatibility

Compatible: text with no fence at the cut is sent exactly as before. A repaired pair that would sever, or would exceed the budget, is withheld, which only delays it to the final seal.

Removes nothing: no key, field or error path is removed.

Tests

test/test_rotation_recut_fences.py (new):

  • TestFenceLineSafeOffset: a cut two characters into a closer moves to the closer line, and the repaired tail keeps the closer; a cut after a nested ```python line's backticks moves to that line; a mid-line cut right before a backtick run moves back; an ordinary mid-line cut stays; a first-line cut can become 0.
  • TestRebalanceFencedCuts: no-op outside a fence; close and reopen inside one; a mid-line cut closes on its own line; run length and info string kept ( block with an inner ``` ```` line); last piece never closed; a line split by the cut opens nothing; every cut of a three-way split repaired.
  • TestTelegramRecutInsideAFence: the re-cut is forced (first sever check true, fixed offset inside the fence); every bubble has balanced <pre>, no literal ``` on screen, the repaired pair is graded again; a pair the second grade rejects is withheld with the buffer intact.
  • TestDiscordRecutInsideAFence: every frame has an even count of fence lines, the second frame reopens with ```python; a re-cut segment's _segment_uploads_safe is False.

On main the renderer tests fail (unbalanced fences, uploads flag still True).

Proof: not provable -- other: this host runs no local tests; CI runs the new tests

Manual verification

N/A: needs a live Telegram or Discord channel. Static gates run locally: black, isort, flake8 and the Fast Gate scripts. Tests run in CI.

Related Issues

Fixes #14645

Sibling sweep

Swept: git grep -n "safe_split_offset" -- src/kiro_crew
Sibling: src/kiro_crew/telegram/renderer.py (main re-cut, _carve_graded) -- changed here
Sibling: src/kiro_crew/discord/renderer.py (main re-cut, presentation carve) -- changed here
Sibling: src/kiro_crew/messaging/display_safety.py -- not affected (defines the search)
Sibling: src/kiro_crew/wecom/renderer.py -- not affected: its safe_split_offset cut truncates the live frame of one bubble, and the remainder goes into the next frame of the same text; it does not seal two source slices as separate messages the way the Telegram and Discord re-cut does. A fence crossing a WeCom bubble rotation is outside this issue.

Pattern harvest

Not generalizable: one shared repair helper now used by every re-cut site.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

@CrysisDeu
CrysisDeu requested review from a team and KiroCrewCaptain as code owners October 29, 2026 07:26
@CrysisDeu
CrysisDeu requested a review from bolichen97 October 29, 2026 07:26
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision ef13174cafadf98be58da3b01dc66cb1465a4760 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of ef13174cafadf98be58da3b01dc66cb1465a4760 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. Every item comes from issue #14645. It reuses the splitter's existing _advance and _Fence primitives, and it covers every site that re-cuts and seals.

What this change ships

Inventory (6 items) — 6 justified

Intent: when a credential-graded rotation cut lands inside a fenced code block, the code should still render as code on Telegram and Discord, and Discord should not upload a file the model only quoted as code. This is a FIX.

  1. A re-cut inside a fence is now closed on the first piece and reopened on the next, with the fence's original opener, on Telegram and Discord — justified
  2. A re-cut that would split a fence delimiter line moves back to the start of that line, or withholds when the result is 0 — justified
  3. The repaired pieces are graded for a severed credential again and withheld when they still sever (Telegram main path, Discord head and tail) — justified
  4. The repaired pieces are held to the rendered cap or limit, and withheld when they exceed it — justified
  5. On Discord, a segment that has been re-cut uploads no file, even a held image outside every fence — justified
  6. The messaging spec now describes the repair, the boundary move and the upload rule — justified

[FIRST-PRINCIPLES-REVIEWED] ef13174

Description read

[DESCRIPTION-READ] 0d09cba3849e9c9673ad279474df25aac0a205c419fdde5dc5e2353b570cbb6e

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of ef13174cafadf98be58da3b01dc66cb1465a4760 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks. Every re-cut site now repairs fences through one shared helper and re-grades the exact pieces it sends. The upload change only takes uploads away, and it is documented.

Notes

  • After a Discord re-cut, self._segment_uploads_safe = False covers the whole segment, so an image held outside every fence now stays literal markup instead of uploading. Running the protected-span/tier check again on the repaired pair would keep that image uploadable.

[DESIGN-REVIEWED] ef13174

Description read

[DESCRIPTION-READ] beba7d37448b89e0ff5a5d077d65687d93f98382be08c993cd37b5b161689dd2

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed ef13174cafadf98be58da3b01dc66cb1465a4760 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] ef13174

Verdict parsed from the review's SHA-scoped output markers for commit ef13174cafadf98be58da3b01dc66cb1465a4760.

False positive or not applicable? A repository writer can comment:
/ai-review override fable ef13174cafadf98be58da3b01dc66cb1465a4760 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of ef13174cafadf98be58da3b01dc66cb1465a4760 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] ef13174

False positive or not applicable? A repository writer can comment:
/ai-review override gpt ef13174cafadf98be58da3b01dc66cb1465a4760 false-positive: <reason, 20 to 500 characters>

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this. The shared repair helper is the right shape, and every re-cut path now re-grades the repaired pieces before sending, which keeps the credential guard intact. One blocking problem in rebalance_fenced_cuts itself, and it is the same root cause as the GPT lane finding.

Verified at 2340291: the 11 new tests pass, and the related renderer and split credential suites (test_discord_renderer.py, test_telegram_renderer*.py, test_split_credential_boundary*.py) pass locally, 359 passed. The red mypy job reports _frame_parents errors in files this PR does not touch, which is also red on main.

Blocking:

  1. A cut that lands inside a delimiter line breaks the fence it is meant to seal. The helper reads fence state only from finished lines, so the partial line on the head side is treated as body. But safe_split_offset samples offsets at limit - 2^k with no line alignment, so a cut through a fence line is reachable. Two cases, both reproduced against the helper directly:

    • Cut through a closer, "```\ncode\n```\nafter prose\n" cut two characters into the closer. The head becomes "```\ncode\n``\n```" (fine), but the tail becomes "```\n\nafter prose\n"`: the original closer is gone, so the reopened block never closes and the prose after it renders as code. On main this tail renders as prose, so this case is a regression.
    • Cut after the backticks of a nested ```python body line (the GPT lane case). The partial ``` reads as a closer in the head, so the appended closer opens a new block, and the tail shows python without its backticks.

    Suggestion: when the partial line at a cut is a prefix of a fence delimiter, or would become one, move the cut back to the start of that line (or treat that cut as unsafe so the caller withholds), then rebalance and grade again. A test for each case above would pin it.

Not blocking:

  • After any re-cut, Discord marks the whole segment upload-ineligible, including a held image outside every fence. That is a safe loss, and the Design lane already asked for the spec line to say it covers the whole segment.
  • The WeCom renderer (wecom/renderer.py) also cuts body[: safe_split_offset(...)] as a raw slice. A line in the sibling sweep saying whether it can land inside a fence would close that question.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 29, 2026
@CrysisDeu
CrysisDeu force-pushed the fix/rotation-recut-fence-and-uploads-14645 branch from 2340291 to 991d1ee Compare October 29, 2026 09:08
@CrysisDeu

Copy link
Copy Markdown
Contributor Author

@bolichen97 thanks, both cases were real. Fixed in 991d1ee9e3.

Blocking: a cut inside a delimiter line. New fence_line_safe_offset(text, offset) in messaging/split.py. Before the repair, every re-cut site (Telegram main and _carve_graded, Discord main and the presentation carve) moves the offset safe_split_offset returns back to the start of its line when:

  • the part before the cut starts like a delimiter (up to three spaces, then a backtick or tilde): your closer case and the GPT nested ```python case; or
  • a mid-line cut leaves the part after it starting like one, which would turn body text into a delimiter on the reopened side.

The moved cut is a new boundary, so it goes through the same repair and the same re-grade as before, and an answer of 0 withholds. Tests in TestFenceLineSafeOffset pin both of your cases, including that "```\ncode\n```\nafter prose\n" cut two characters into the closer now keeps the closer on the tail.

Non-blocking:

  • Discord uploads: the spec line and the PR body now say the whole segment becomes upload-ineligible after a re-cut, a held image outside every fence included.
  • WeCom: added to the sibling sweep. Its safe_split_offset cut truncates the live frame of one bubble, and the remainder goes into the next frame of the same text, so it never seals two source slices as separate messages. A fence crossing a WeCom bubble rotation is outside this issue.

The mypy _frame_parents red is the main-wide one; #25706 fixes it, and I'll rebase once that lands.

@CrysisDeu
CrysisDeu requested a review from bolichen97 October 29, 2026 09:09
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 29, 2026
When the splitter's boundary severs a key, Telegram and Discord re-cut
the segment at a graded offset and send two raw source slices. A cut
inside an open fence sent a head with an unmatched opener, so the code
showed as prose and the next piece's closer opened a block that never
closed. On Discord the upload-safety verdict also survived a boundary
it was never computed for.

Add rebalance_fenced_cuts to messaging/split.py: a piece that ends
inside a fence gets the fence's closer and the next piece is reopened
with the original opener. Every re-cut path uses it, grades the
repaired pieces again and withholds when they sever, and keeps each
piece within the budget. A Discord re-cut makes the segment
upload-ineligible.

Fixes #14645
@CrysisDeu
CrysisDeu force-pushed the fix/rotation-recut-fence-and-uploads-14645 branch from 991d1ee to ef13174 Compare October 29, 2026 09:52
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 29, 2026

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update. This looks good to ship and resolves my earlier change request. I will approve once the remaining backend test jobs and the readiness signal finish green on this head.

What changed since 2340291: the new fence_line_safe_offset moves a cut that would split a fence delimiter line back to that line's start, and it runs before rebalance_fenced_cuts at all four re-cut sites (two in the Discord renderer, two in the Telegram renderer). A moved cut only shrinks the head, and every site still grades the repaired pieces with severs_a_credential before anything is sent, so the credential guard holds at the new boundary. The spec now says the Discord upload loss covers the whole segment, and the PR body explains why the WeCom bubble cut is a different path. Both earlier non-blocking notes are resolved.

Verified at ef13174:

  • Range-diff against 2340291: only the helper, its four call sites, the spec line and five new tests changed.
  • Both earlier failing cases, called directly: the closer case now gives a closed head and a tail whose prose renders as prose; the nested ```python case keeps the inner line intact in the tail.
  • A randomized check over about 120,000 cuts of mixed fence texts (backtick and tilde fences, nested and indented delimiters, inline backtick runs): with the new helper, the text outside fences after repair always matches the original; without it, about 17,000 cuts broke it.
  • test_rotation_recut_fences.py plus the Discord, Telegram, WeCom renderer and split suites: 941 passed locally.

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 29, 2026

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving at head ef13174.

Our earlier change request on this PR is resolved by the current head, which our review read in full and found no blocking issues in.

Gates checked at this head: readiness label is passed, every check is green (including the Opus, GPT, Design, First Principles, UX, Security Scope and Sensitive Change review lanes), no AI lane blocks, no other reviewer has requested changes, and the branch merges cleanly into main.

This approval replaces our previous request for changes. Auto merge is pinned to this exact head commit.

@bolichen97
bolichen97 merged commit 0cca9c3 into main Oct 29, 2026
94 checks passed
@bolichen97
bolichen97 deleted the fix/rotation-recut-fence-and-uploads-14645 branch October 29, 2026 11:07
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 29, 2026
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.

channels: the rotation's graded re-cut drops fence rebalancing and keeps a stale uploads-safe verdict

2 participants