Skip to content

fix: flatten markdown tables in imessage replies - #47860

Open
winnythedev wants to merge 1 commit into
mainfrom
fix/47825-imessage-tables
Open

winnythedev wants to merge 1 commit into
mainfrom
fix/47825-imessage-tables

Conversation

@winnythedev

@winnythedev winnythedev commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A Bot's reply that holds a markdown table reaches iMessage as one readable line per row, the way it already reaches WhatsApp, instead of as raw table source.

On iMessage a reply with a pipe table arrived as written: every row with its pipes, and the |---|---| separator row under the header. The same reply reaches WhatsApp as one labelled line per row (- Name: a.py | Size: 10 | Owner: ann).

Why it matters

Bots answer with tables often (file lists, comparisons, status rows). On iMessage, which renders no markup, those answers were hard to read on a phone: the separator row and pipes took up the screen and the columns wrapped into ragged text. The module that flattens iMessage replies already says it flattens pipe tables, so a reader of the code expects this to work.

Not a goal

  • Changing any other bridge. WhatsApp already flattens; Weixin keeps markdown on purpose; WeCom keeps tables raw on purpose; Teams and Webex convert tables themselves.
  • Changing the shared helpers flatten_pipe_tables (messaging/markup.py) or rewrite_outside_fences (messaging/split.py).
  • Unifying plaintext.py's own fence regex with the shared CommonMark fence machine (see the review section).

What changed (motivation → approach → change)

  • Symptom: a pipe table reaches iMessage as raw source.
  • Root cause: to_plaintext (src/kiro_crew/imessage/plaintext.py), which IMessageRenderer.delivery_text runs on every reply, flattens headings, rules, bullets, numbers, quotes, links, images and emphasis line by line but had no table step. IMESSAGE_CAPABILITIES keeps table_mode="off", and no renderer step converts tables on its own.
  • Change: before its line pass, to_plaintext now runs rewrite_outside_fences(text, flatten_pipe_tables), the same reduction WhatsApp applies. The table becomes markdown bullets with bold labels, and the existing line pass then flattens those bullets and the cells' own markup (bold, links, inline code) like the rest of the reply. Fenced content is untouched. Splitting still runs last, on the flat text.
  • The two helpers are imported inside to_plaintext: the module's other importers (chat titles, computer-use output and others) only use its grapheme helpers, and importing messaging.split costs them about 70-90 ms for nothing.
  • Spec: one sentence in docs/system-specs/modules/messaging.md (iMessage, "Rendering.").

Backwards compatibility

Compatible: only the plain text iMessage receives changes, and only for lines that form a pipe table (a header row followed by a separator row) outside a fence. Nothing that is accepted today is rejected.

Removes nothing: the change adds a rendering step; no key, path, record or function is removed.

Tests

test/test_imessage_tables.py, each case through the real IMessageRenderer (on_text_chunk, on_done) into a fake client:

  • test_a_table_is_sent_as_one_labelled_line_per_row: a 3-column table between two paragraphs.
  • test_cell_markup_is_flattened_too: cells holding bold, a link and inline code.
  • test_a_cjk_table_is_flattened: a CJK table.
  • test_choices_under_a_table_stay_choices: an [OPTIONS: ...] footer right under a table stays numbered choices (expected footer taken from the renderer itself).
  • test_a_table_inside_a_code_fence_is_left_alone, test_a_table_inside_a_tilde_fence_is_left_alone, test_prose_with_pipes_is_left_alone: guarded inputs that must not change.

On main the first four fail and the last three pass; with the fix all seven pass.

Proof: PROVEN test/test_imessage_tables.py::test_a_table_is_sent_as_one_labelled_line_per_row, test/test_imessage_tables.py::test_cell_markup_is_flattened_too, test/test_imessage_tables.py::test_a_cjk_table_is_flattened, test/test_imessage_tables.py::test_choices_under_a_table_stay_choices (head cb85afd68abb)

Local gates on Linux x86_64: scripts/local-gate.py --base upstream/main --tree-scans ran the related set (7,745 passed) and both tree-scan shards (57,738 passed); 11 tree-scan tests failed only for host reasons: their child Python cannot load libffi.so.6 on this host, or (test_browser_harness_supervisor.py, test_mcp_footprint_stub_probe.py) they probe host-installed tools and stub children; none imports the iMessage code. mypy src/kiro_crew reports 6 errors, all in files this PR does not touch (missing library stubs and an aiohttp version in the local venv); none in imessage/; black, isort, flake8, subprocess-encoding, comment-history, brand-name, agent-SDK boundary and file-line-cap checks pass; test/test_flake_pattern_ratchet.py and test/test_wait_discipline_ratchet.py pass; ./scripts/docs-lint.sh passes. semgrep scan --config semgrep/ --baseline-commit <merge-base> --error (1.78.0): 0 findings.

Inputs probed by hand through to_plaintext, not kept as cases: a bullet list followed by a table (both flattened, blank line kept), a fence followed by a table and the reverse, a fence left open before a table (table stays raw), a * inside a cell (2*3, a * b: unchanged), an escaped \| and a pipe inside inline code (cell text, not a boundary), a header with no data rows (left as written). Not tested: a table longer than one iMessage message (flattening runs before chunk_plaintext, which splits the flat text as before).

Review (multi-model interrogate)

Three independent reviewers read the full diff against CI's own lane contracts; all three passed it.

  • Found (Low): plaintext.py's fence regex accepts two openers the shared CommonMark machine rejects (a tab-indented fence, or a backtick inside a backtick fence's info string). For such a block the table is flattened and then shipped as "code", so literal ** labels show. Dismissed for this PR: both openers are rare in model output, the damage is cosmetic, and the real fix is to drive the line pass from iter_fence_spans so one fence rule exists, which is a separate change. The opposite disagreement (a two-word info string) keeps the table raw, as on main.
  • Found (Nit): a cell holding a lone ** pairs with the label's bold markers. This is the shared flatten_pipe_tables output format, unchanged here and the same on WhatsApp.
  • Found (Nit): tables inside a blockquote and tables without outer pipes stay raw, by the shared row rule; WhatsApp behaves the same.
  • Acted on: nothing needed a code change.

Manual verification

N/A: no live iMessage account and no gateway were used. The bridge needs macOS, but this is platform-independent text code, so the tests above drive the real renderer on Linux and Linux CI verifies it.

Real-machine verification

Machine: own computer, Linux x86_64.
Before (main e392fe2): python -m pytest -n0 test/test_imessage_tables.py -> 4 failed, 3 passed (the table, cell-markup, CJK and options-footer cases show raw pipes).
After (head aebf50a): python -m pytest -n0 test/test_imessage_tables.py -> 7 passed.
Cleanup: own computer, nothing to delete. No live iMessage account was used: the run goes through the real IMessageRenderer into a fake client.

Related Issues

Fixes #47825

Overlap: PR #47847 (merged) changed the single-star emphasis pattern in the same file (_EMPHASIS_RES) and added an iMessage sentence to messaging.md. This branch is rebased on it: the spec paragraph keeps both sentences, and table cells go through that emphasis rule like prose (| 2 * 3 * 4 | */5 * * * * | arrives as • Expr: 2 * 3 * 4 | Cron: */5 * * * *). PR #47803 also edits messaging.md (the empty-turn notice) and imessage/renderer.py, not plaintext.py.

Sibling sweep

Swept: git grep -n -i -E "table_mode|render_tables|flatten_pipe_tables|native_tables" -- src/kiro_crew/
Sibling: src/kiro_crew/whatsapp/renderer.py -- not affected -- already flattens each table outside a fence with flatten_pipe_tables
Sibling: src/kiro_crew/weixin/transport.py -- not affected -- declares native tables, keeps markdown on purpose
Sibling: src/kiro_crew/wecom/renderer.py -- not affected -- renders tables through render_tables_for_target by its own policy
Sibling: src/kiro_crew/teams/renderer.py -- not affected -- converts tables through render_tables_for_target
Sibling: src/kiro_crew/webex/renderer.py -- not affected -- converts tables through render_tables_for_target
Sibling: src/kiro_crew/feishu/client.py -- not affected -- sends replies as text with no markdown pass at all, a wider gap than a missing table step

Pattern harvest

Not generalizable: a channel converter that skipped one markdown construct its own docstring promised; the shared reduction already existed and is now called.

Checklist

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • 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.

@winnythedev
winnythedev requested review from a team and KiroCrewCaptain as code owners December 5, 2026 17:29
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision aebf50a9d1d02a184dfd832448f919bfcab5a469 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 Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks: the change fills a missing step in iMessage's own flattener by reusing the shared WhatsApp reduction, and to_plaintext has only one caller.

Notes

  • The fence rule now exists twice. rewrite_outside_fences decides fences for the table step, and plaintext.py's regex decides them for the line pass. They disagree on the edge-case openers the description names. Driving the line pass from iter_fence_spans in a follow-up would leave one fence rule.

[DESIGN-REVIEWED] aebf50a

Description read

[DESCRIPTION-READ] 943312e808c954f88f067006987b286efe936b423d76d9e0aaeaffd727d4e2e7

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 Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of aebf50a9d1d02a184dfd832448f919bfcab5a469 — 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. The fix reuses the shared flatten_pipe_tables and rewrite_outside_fences helpers in the one function that had no table step, and it adds no new surface.

What this change ships

Inventory (4 items) — 4 justified

Intent: iMessage users get bot replies with tables as readable rows instead of raw pipe source. This is a FIX (#47825; the module docstring already promised it).

  1. A pipe table outside a fence in an iMessage reply now arrives as one labelled line per row. — justified
  2. The table-flattening helpers are imported inside to_plaintext, so other importers of the module don't pay their import cost. — justified
  3. One sentence added to the iMessage rendering paragraph of the messaging spec. — justified
  4. Seven tests drive the real IMessageRenderer with tables, fenced tables and prose containing pipes. — justified

[FIRST-PRINCIPLES-REVIEWED] aebf50a

Description read

[DESCRIPTION-READ] 24203782490872f3643c1a5997ecc48133839ee5d2647388fe88ecc319e46515

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 Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] aebf50a

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

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

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] aebf50a

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

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Dec 5, 2026
iMessage renders no markup, and to_plaintext flattened headings,
bullets, quotes, links and emphasis but had no table step, so a reply
holding a pipe table arrived as its raw source, separator row included.
WhatsApp, which has the same limits, already reduces each table to one
labelled line per row.

to_plaintext now runs the shared flatten_pipe_tables over the text
outside code fences before its line pass, so the cells' markup is
flattened with the rest of the reply. A table inside a fence, prose
with pipes and an [OPTIONS: ...] footer reach the chat unchanged.

Fixes #47825
@winnythedev
winnythedev force-pushed the fix/47825-imessage-tables branch from 0cf12c3 to aebf50a Compare December 5, 2026 19:08
@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: checking Automated validation is still running

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iMessage: a reply's markdown table arrives as raw pipes and a |---| row, while WhatsApp sends it as one labelled line per row

1 participant