Skip to content

fix(channels): cut model text where no key can be rejoined - #13758

Closed
chenmingwei23 wants to merge 2 commits into
mainfrom
fix/cross-delivery-redaction-13743
Closed

chenmingwei23 wants to merge 2 commits into
mainfrom
fix/cross-delivery-redaction-13743

Conversation

@chenmingwei23

@chenmingwei23 chenmingwei23 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

What is the problem?

Six channel delivery paths cut model text on a length budget and scan each piece on its
own. A credential the cut severs matches nothing in either piece, so both are reported
clean and the reader rejoins the halves reading one message and then the next.

Five of the six already redact the whole buffer before splitting, and that closes a cut
that lands mid-line: the credential is one marker before any boundary can reach it. It
does not close a cut at a line boundary. Sealing a chunk trims the whitespace that
ended it, and the next chunk starts at the first character after that whitespace, so the
two halves end up flush against each other on screen while the whole text still holds the
break between them. That form is invisible to a scan of the text as written and to a scan
of either piece.

Why this issue matters to the user

The reader sees the key and nothing reports it. Because each piece scans clean, no
redaction notice is emitted and the delivery is recorded as a clean send: the operator has
no signal at all. This is the audit half of the problem, not only the disclosure half.

How our fix solves it

The boundary is where the defect lives, so the fix goes into the two shared splitters
rather than into six renderers. split_markdown_safe and chunk_utf8_bytes take an
optional redactor and, when given one, reduce the text before any boundary is chosen:

  1. the text is redacted against the rendered form first, via the existing
    redact_for_display, which closes a cut landing mid-line and the markup-hidden variant;
  2. the boundaries it then chooses are graded one at a time, on the RENDERED pair. A
    platform drops the whitespace at a message's edges, so the last visible character of one
    message sits against the first visible character of the next and a trailing newline
    separates nothing. Stripping those edges before the grade is what makes the predicate
    see the boundary a reader sees, and it is what carries the guarantee to the byte
    splitter, which stores the break rather than trimming it.

Only a boundary that really does rejoin a key is repaired, and the repair collapses the
breaks and redacts, so that message loses its line breaks and its markup. That is the same
one-directional trade redact_for_display already makes when it gives up markup alone:
formatting is worth less than a key. Grading the chosen boundaries rather than every
candidate is what keeps ordinary wrapped content intact -- two lines that read as one key
when glued are never joined by a cut, so they are left exactly as written, and a fixture
pins that.

The parameter is optional because a caller whose chunks land inside one message severs
nothing a reader can rejoin across messages. It is not optional for the six paths that do
turn one chunk into one message: those are enumerated in the tests, and any splitter call
there that omits the redactor fails. The check walks the AST and counts both shapes,
including the four sites that hand the splitter to asyncio.to_thread as a reference --
a check reading direct calls only would see three of the seven sites and silently pass the
rest.

Per path, all seven call sites:

path call before now
slack/renderer.py:875 split_markdown_safe, offloaded no whole-buffer redaction at the cut passes _redact_all
webex/renderer.py:396 chunk_utf8_bytes display-safe via webex_display_safe line-boundary seam also closed
whatsapp/renderer.py:302 split_markdown_safe display-safe before the split line-boundary seam also closed
teams/renderer.py:383 split_markdown_safe, offloaded display-safe via _display_text line-boundary seam also closed
teams/renderer.py:743 split_markdown_safe, offloaded display-safe before the split line-boundary seam also closed
wecom/renderer.py:306 split_markdown_safe, offloaded display-safe before the split line-boundary seam also closed
dashboard/chat_mirror.py:588 split_markdown_safe display-safe before the split line-boundary seam also closed

WeCom's streaming push already picks a credential-safe offset for the frame it is about to
send, and that path is untouched.

What tests we did

test/test_split_credential_boundary.py, 25 cases, run scoped with -n0:

  • the seam, measured rather than assumed: the fixture's cut is shown to fall between the
    halves of a key, the two pieces are shown to scan clean individually, and the joined
    pair is shown to hold the key;
  • the same fixture with a redactor: no boundary joins to a credential, the key is absent
    from the joined chunks and from each chunk read alone;
  • one channel end to end, through the function WhatsApp's client sends from, asserting on
    the frames it would deliver;
  • the byte-budget splitter, both directions;
  • backward compatibility: prose and a table row are returned byte for byte with a redactor
    passed, so existing callers are unaffected.

Mutation proof, three ways. Dropping the redactor from one of the seven call sites fails
the enumeration test (1 failed). Disabling the redactor branch in both splitters fails five
behaviour tests while the cases that assert the unguarded behaviour keep passing, so the
suite discriminates on the fix and not on the fixture. Removing the edge strip from the
boundary grade -- the part that models what the client renders -- fails the byte-splitter
case alone (1 failed), which is the case that exists for it.

Regression, each file scoped with -n0: test_messaging_split.py 138 passed,
test_channel_table_rendering.py 82 passed (this is the reassembly invariant the byte
splitter documents), test_chat_mirror.py 82 passed, test_display_split_safety.py 50
passed, test_whatsapp.py 26 passed. Gates: black, isort, flake8, mypy on the changed
files, and the comment-history gate, all clean.

The fixture uses the AWS-documented example key the repo's own display-safety tests
already use, so no new credential shape is introduced.

Any other suggestions on the work

This is the first of two stages and deliberately does not close the issue. It makes the
cut safe at every boundary these paths choose. It does not add predecessor bookkeeping at
each point a message stops being editable, which is the remaining scope: a streaming leg
that has already delivered a message cannot re-cut it, so that piece has to be graded
against what is already on screen. The channel-neutral primitives for that are in #13494
and are not on main yet, so this stage reuses only what main already has and touches none
of the files that PR owns.

Two things found while measuring, worth recording. The splitter has callers beyond the six
paths named in the issue: discord/renderer.py (five calls), telegram/renderer.py,
feishu/client.py, messaging/renderer.py and teams/session_resume.py. Discord and
Telegram seal splitter chunks as separate messages and carry the same open seam, and both
files belong to #13494, which is rewriting them with the predecessor primitives -- so they
are stage-two scope here by construction, not an oversight, and this PR touches neither.
Feishu, the shared renderer and the Teams resume path are unchanged, and none of them
belongs to #13494. Their own code answers whether their chunks become separate messages
rather than leaving it open: feishu/client.py sends every chunk as its own reply to the
same inbound anchor, and chunk_for_transport hands its parts to callers that send them
one message at a time. Neither passes a redactor, so the seam is open on those legs too.
They sit outside this stage by scope, not by another PR's ownership. And the issue's per-file grade was measured by grepping for the new primitives'
names, which cannot see that five of the six paths already close the mid-line cut by
redacting the whole buffer first; the line-boundary seam is what was actually open at all
of them.

Pattern harvest

Rule candidate: enumeration test, added in this PR as
test_split_credential_boundary.py::TestEveryDeliveryPathIsEnumerated.

Pattern: a text is cut on a length budget and then scanned one piece at a time, so the scan
runs after the boundary is chosen and cannot see what the cut separated.

The shape generalises past credentials: any check that reads one piece of a text the caller
will deliver in several is blind to whatever the boundary split. It resists a purely
syntactic rule, because "these chunks each become their own message" is not visible at the
call site -- which is why the enforceable half is an enumeration: the delivery paths are
listed, and every splitter call in them must pass a redactor. The list is checked in both
directions, so a path that stops reaching a splitter fails rather than passing by having
nothing left to check, and the AST pass counts the offloaded asyncio.to_thread shape as
well as the direct call.

Refs #13743

Six delivery paths cut model text on a length budget and scan each piece
on its own. A key the cut severs matches nothing in either piece, so both
are reported clean, and the reader rejoins the halves reading one message
and then the next. The per-piece scan emits no notice, so the operator
sees a clean send.

Five of those paths already redact the whole buffer before splitting,
which closes a mid-line hard cut. It cannot close a cut at a line
boundary: sealing a chunk trims the whitespace that ended it, so the two
halves sit flush against each other on screen while the whole text still
holds the break between them, invisible to a scan of the text as written
and to a scan of either piece.

The shared splitters now take the redactor themselves. split_markdown_safe
and chunk_utf8_bytes reduce the text before any boundary is chosen: the
form the platform renders, and the form the seal's trim produces once
every break is collapsed. A credential visible in either is a marker
before a boundary can land inside it. The parameter stays optional, so a
caller whose chunks stay inside one message is unaffected, and an
enumeration test over the six delivery paths fails when any splitter call
there omits it.

Refs #13743
@chenmingwei23
chenmingwei23 requested review from a team and smeyffret September 25, 2026 08:43
@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: checking Automated validation is still running labels Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — 🔴 changes requested (blocking)

GPT 5.6 found at least one blocking issue that must be resolved before merging abba1b6d8c5e59c2fbc0c80b93319a2ce7d32180. 2 of 2 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

This comment is updated in place on each push.

BLOCKING -- src/kiro_crew/dashboard/chat_mirror.py:588 -- large backfill splitting can terminate the gateway
return split_markdown_safe(f"{speaker}: {text}", max_chars, redactor=redact_via_context)
750 KB imported history row -> mirror-link backfill -> quadratic boundary scans on event loop exceed the 25-second watchdog -> gateway exits.
Anchor: residual/crash-data-loss-corruption
Fix: run _units_for via asyncio.to_thread.

BLOCKING -- src/kiro_crew/slack/renderer.py:875 -- hard-cap fallback reopens the credential seam (origin: validation)
split_markdown_safe, text, limit, reserve=reserve, redactor=_redact_all
Long fence scaffolding -> splitter emits an oversized safe chunk -> chunk_text creates an ungraded boundary -> Slack renders credential halves joined.
Anchor: residual/security
Fix: credential-grade and redact the final post-chunk_text sequence.

[BLOCK-MERGE] abba1b6
[GPT-REVIEWED] abba1b6

Adjudication (Opus 5) — is blocking on each finding proportionate?

F1 — UNBOUNDED (crash / in-flight work loss, and the fence is authoritative here).
Conditions confirmed: _units_for runs inline on the loop thread at src/kiro_crew/dashboard/chat_mirror.py:602–607 (only select_backfill_messages is offloaded, :567); the redactor path routes through _cut_where_no_key_rejoins → _under_a_safe_budget (src/kiro_crew/messaging/split.py:377, :293, :309) where _rejoins_a_key joins and re-scans the whole side per boundary (:222–:226) and joins_to_a_credential is ~6 full-text passes per boundary (src/kiro_crew/messaging/display_safety.py:163–:169), retried on exponentially stepped budgets; row size is uncapped (src/kiro_crew/dashboard/chat_backfill.py:53); the armed watchdog _exits after ~20–25s of loop silence (src/kiro_crew/dashboard/server.py:5799, :5860, loop_watchdog.py:174). Recovery: none in-band — the process exits.
Real fix cost: one asyncio.to_thread wrapping the existing sync helper, the same offload the Slack twin already uses at src/kiro_crew/slack/renderer.py:875 and this handler already uses twice; no new state, invariant, or ordering contract.

F2 — UNBOUNDED (security-class: credential halves rejoined on screen).
Conditions confirmed: split_markdown_safe may return a chunk exceeding limit by its verbatim fence scaffolding (src/kiro_crew/messaging/split.py:348–:355, :465–:496, opener is the whole literal line :165, :454), so with _SPLIT_HEADROOM = 100 (src/kiro_crew/slack/renderer.py:107, :858) a long fence info line — or a nonzero reserve such as :1130 — pushes a chunk past SLACK_MSG_LIMIT - reserve; chunk_text then blind-slices it (src/kiro_crew/messaging/renderer.py:137) at src/kiro_crew/slack/renderer.py:880, after all grading has happened, and each slice is posted as its own message (:913–:917). The markup-collapse reading that survives whole-text redact_for_display is exactly the one joins_to_a_credential exists to catch (display_safety.py:149–:157), and nothing re-grades the post-chunk_text sequence. Recovery: none — the messages are delivered.
Real fix cost: grade/redact the final bounded sequence in the one method that produces it; no cross-module contract and no mechanism outside this path.

[ADJUDICATION] abba1b6 total=0 uphold=0 downgrade=0
[GPT-ADJUDICATED] abba1b6

[ADJUDICATION-FENCED] abba1b6 fenced=2 flagged=0
UPHOLD-FENCED F1 src/kiro_crew/dashboard/chat_mirror.py:588 -- A single large uncapped history row is ordinary output of the system's own writer, and the quadratic credential grade runs inline on the loop thread where the armed watchdog turns it into a process exit, so nothing about the condition combination is extreme.
UPHOLD-FENCED F2 src/kiro_crew/slack/renderer.py:875 -- Every condition (an over-headroom fence opener, a line admitting no clean cut, a markup-collapsing credential at the blind slice point) is producible by the model whose text this path exists to police, so the ungraded post-chunk_text boundary is a live credential seam rather than a residual one.
[GPT-ADJUDICATED-FENCED] abba1b6

False positive or not applicable? A repository writer can comment:
/ai-review override gpt abba1b6d8c5e59c2fbc0c80b93319a2ce7d32180: <one-sentence reason>

@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 Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — 🟡 CONCERNS

Premise-level review of abba1b6d8c5e59c2fbc0c80b93319a2ce7d32180 — 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/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The seam this PR calls a silent disclosure stays open on Feishu and both chunk_for_transport legs — paths #13494 does not own, deferred on a question the code already answers.

Not justified as shipped

  • Item 1 — symptom-level (point patch, siblings counted): the deferral rests on "whether their chunks become separate messages is the question that sizes the rest of stage two" — but feishu/client.py:217-219 loop-sends each chunk as its own reply, and chunk_for_transport (messaging/renderer.py:174) is loop-delivered one unit per send_message at slack/gateway.py:3931-3938 and dashboard/handlers/messaging.py:2182-2191 (grep chunk_for_transport: 4 call sites; grep splitters: Feishu 1 delivering site). Discord/Telegram are legitimately fix(channels): grade the rotation boundary the splitter cannot see #13494's files; these three are nobody's.
    Clears when: feishu/client.py:213 and the delivering chunk_for_transport consumers pass a redactor, here or in a linked follow-up that names them.

What this change ships

Inventory (6 items) — 4 justified

Intent: FIX — stop a length-budget cut from handing a reader a credential neither delivered message contains, and stop the scan reporting that send clean.

  1. A key severed at a line/space boundary can no longer be rejoined across adjacent messages on six delivery paths — symptom-level (unfixed siblings: feishu, chunk_for_transport legs)
  2. Slack's long-answer split now redacts the whole buffer before cutting — justified
  3. A message no sampled budget cuts safely arrives with all whitespace and markup stripped — justified
  4. Both shared splitters accept an optional redactor (7 counted consumers) — justified
  5. messaging/split.py registered in the security-posture non-egress redaction registry — rides along (mechanical registry entry, harm-free)
  6. New AST test permanently fails any redactor-less splitter call in the six files — justified

[FIRST-PRINCIPLES-REVIEWED] abba1b6

@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — 🟡 CONCERNS

Design-level review of abba1b6d8c5e59c2fbc0c80b93319a2ce7d32180 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

Fail-open by default: the credential-aware cut is opt-in, enforced only by a hand-maintained six-file allowlist that a new delivery path never enters.

Watch

  • redactor defaults to None and the enforcement test enumerates exactly six files (DELIVERY_PATHS = ("slack/renderer.py", …)). A future channel renderer that calls split_markdown_safe/chunk_utf8_bytes bare reopens this exact seam — key severed across two messages, both scan clean, no signal — and no test goes red, because the guard discovers nothing outside its list. The PR's own survey proves this recurrence path is real: Discord and Telegram carry the seam today.
    Clears when: the splitters require an explicit redactor argument (with None as a deliberate opt-out) or the test inverts to an exempt-list so an unlisted splitter call fails closed; alternatively a maintainer-acknowledged follow-up tracking it alongside #13494.

Suggestions

  • _rejoins_a_key rebuilds the full joined prefix/suffix per boundary — O(chunks × length) per budget candidate — and Webex (chunk_utf8_bytes(content, WEBEX_MAX_TEXT, redactor=_redact_all)) and chat_mirror run it on the event loop; offload those two calls like Slack/Teams/WeCom already do.

[DESIGN-REVIEWED] abba1b6

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Sep 25, 2026
@chenmingwei23
chenmingwei23 force-pushed the fix/cross-delivery-redaction-13743 branch from e824c69 to c20a0e6 Compare September 25, 2026 09:59
@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 Sep 25, 2026
@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5 Review — ✅ no blocking findings

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

Review details

Advisory only — the credential-aware cut is not wired to every one-chunk-one-message path, and the Webex site runs its new grading on the event loop.

FINDING — src/kiro_crew/webex/renderer.py:396 — chunk_utf8_bytes(content, WEBEX_MAX_TEXT, redactor=_redact_all) is the only one of the six converted call sites left synchronous inside an async def on_done, so the new grade (_rejoins_a_key → joins_to_a_credential per boundary, each redacting a full-length prefix and suffix, repeated for every sampled budget) runs on the gateway's single loop and freezes every other chat turn and the heartbeat for the duration of a large answer → Fix: chunks = await asyncio.to_thread(chunk_utf8_bytes, content, WEBEX_MAX_TEXT, redactor=_redact_all) or ["…"], as slack/renderer.py:875, teams/renderer.py:382/745 and wecom/renderer.py:305 already do.

FINDING — test/test_split_credential_boundary.py:39 — DELIVERY_PATHS is documented as "Every path that delivers one splitter chunk as one message" but omits discord/renderer.py:941/962/966 (each chunk sealed and _open_new_message()), telegram/renderer.py:400, feishu/client.py:213 (one reply per chunk) and messaging/renderer.py:174 as reached by dashboard/chat_runner.py:4117 (for part in parts: await transport.send_message(...)), all still splitting with no redactor, so a key severed at a boundary still reaches two adjacent messages on those channels — sharpest at chat_runner.py:4105, which already holds redact_via_context and is the very function chat_mirror.py:588's comment says it mirrors → Fix: add those modules to DELIVERY_PATHS and pass their redactors; the chunk_for_transport byte leg additionally needs redactor threaded through split_markdown_bytes, which this PR left without the parameter, so that half cannot be fixed at the call site.

FINDING — src/kiro_crew/messaging/split.py:227 — joins_to_a_credential("".join(rendered[: i + 1]), "".join(rendered[i + 1 :]), redactor) rebuilds and re-redacts the whole text on both sides of every boundary, so one grade is O(len(text)²/limit) and _under_a_safe_budget repeats it for up to ~13 budgets; slack/renderer.py:1130 then re-runs the entire search on the whole accumulated text once per _EDIT_INTERVAL and keeps only frame[0], so a long answer's delivery latency grows superlinearly with its length × tick count → Fix: grade a bounded window on either side of each cut (wide enough for the longest credential pattern) instead of accumulating the full prefix and suffix — a change in _rejoins_a_key that needs no new call-site machinery.

[OPUS-REVIEWED] abba1b6

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

False positive or not applicable? A repository writer can comment:
/ai-review override fable abba1b6d8c5e59c2fbc0c80b93319a2ce7d32180: <one-sentence reason>

Grade the boundaries the ladder chooses, one at a time, rather than every
break at once. A scan of all of them together answers a wider question than
any cut asks: two lines that read as one key when glued would cost the message
every break and its markup, though no cut ever joins them. Wrapped content in a
fence is that shape.

The grade reads the RENDERED pair. A platform drops the whitespace at a
message's edges, so the last visible character of one message sits against the
first visible character of the next: neither a trailing newline nor a trailing
space separates anything. That is also what carries the guarantee to the byte
splitter, which stores the whitespace rather than trimming it -- lossless where
it is kept, invisible where it is shown.

The repair moves the CUT before it gives up any text. Budgets step back
exponentially and every candidate is verified, so a key straddling a boundary
ends up whole inside one chunk with every character and every break intact,
where the space between its halves is on screen and the scanner's own reading
of such text stands. Only when no budget at or below the caller's cuts safely
is the whitespace flushed and the text redacted -- every run, because the
whitespace a cut consumes can be a single space -- and that result is graded as
well, since a repair returned without a grade is a repair nobody checked.
Flushing every run is deliberately wider than one cut produces: that width is
what lets a caller bound the chunks again under its own platform limit without
a grade of its own.

The splitter applies a caller's redactor, so it is classified in
security_posture's NON_EGRESS_REDACTION_MODULES. It owns no output; the modules
that call it are the registered sinks. Same reason display_safety.py sits
there, and the posture drift guard fails without the entry.

Refs #13743
@chenmingwei23
chenmingwei23 force-pushed the fix/cross-delivery-redaction-13743 branch from c20a0e6 to abba1b6 Compare September 25, 2026 12:00
@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 Sep 25, 2026
@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: checking Automated validation is still running labels Sep 25, 2026
@chenmingwei23

Copy link
Copy Markdown
Contributor Author

Superseded by #13828, which carries the same two commits plus the follow-up fixes review asked for on this head. This branch needed a history rewrite to take them while staying inside the two-commit limit, so the replacement was cut as a fresh branch and pushed without one. No review had been left here.

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