Skip to content

fix(channels): send a group channel's recent messages once, then only new ones - #39506

Closed
javenciu wants to merge 1 commit into
mainfrom
fix/channel-history-new-entries
Closed

javenciu wants to merge 1 commit into
mainfrom
fix/channel-history-new-entries

Conversation

@javenciu

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A group-channel conversation is sent its recent-messages window once, and after that only the entries that arrived since, on the harnesses whose compaction Kiro Crew sees.

ContextBuilder.build_message reads channel_history.context_for on every top-level channel turn (context_assembly/turn_message.py, the leg commented "inject on every message"). The window holds up to 50 entries of at most 300 characters, and 200 entries in observe mode. The engine replays its own conversation, so the previous turn's copy is already in its history and each message adds the whole window again. The block cannot be taken out at the wire the way the reply-format rules are, because each line carries a relative age ("5m ago") that changes every turn.

Why it matters

A full window is 16,015 characters (50 entries of 300), about 4,000 tokens, and 4 times that in observe mode, added to the history on every message and re-read on every later turn. Measured with the real builder, one new message in a channel with 10 buffered entries of 150 characters makes the second turn 2,465 characters on main and 780 here. With 50 entries of 300 characters it is 16,556 and 930.

Not a goal

  • A harness that does not report compaction. It keeps today's full window on every message, because a compaction it never reports would leave it with none.
  • Thread turns. A top-level cursor does not apply to a thread window, which stays whole, and thread_replies already sends unseen replies only.
  • The scrub and the fence on the block, the window sizes, the TTLs, and what is written to disk in observe mode.
  • Dropping the relative age from the lines.
  • A new setting (see Backwards compatibility).

What changed (motivation → approach → change)

Each entry now carries an arrival number. ChannelHistory.context_since(channel_id, thread_ts, after) returns the text and the number of the newest top-level entry it covers, from one snapshot, so an entry pushed meanwhile is never skipped. With after it formats only newer entries under [New channel messages since the last turn:], and returns nothing when there are none. context_for keeps its text and gains an optional after.

The position lives in context_assembly/delivery_records.py, beside the skill-body record and on its settle: commit_skill_bodies keeps it, rollback_skill_bodies restores the value before the build. build_message uses it only when the provider says reports_compaction is True, for a top-level turn with a session key. Otherwise it makes the same context_for call as before.

Turn Window sent
New session, or the first turn after a compaction (needs_reinjection) whole
Next turn of a landed conversation entries newer than the last one sent, or nothing
After a turn that did not land (rolled back) from where that turn began
A build nothing settled, or a rebuild of the same turn from where the unsettled build began
Thread turn, or a harness that does not report compaction whole, as today

An unsettled build can repeat entries and never drops one. A record for another channel or agent does not apply, and a position ahead of the buffer reads as no position. docs/system-specs/modules/channel-history.md says so; no file under src/kiro_crew/docs/ changes.

Backwards compatibility

No setting. The reply-format change that sent a block once for compaction-reporting harnesses (#35756) shipped with no new key, and the harness check here is the same reports_compaction is True. Every other harness, a thread turn, and a call with no session key take the unchanged context_for path. test_thread_context.py still pins the exact context_for(channel_id, thread_ts=...) call.

Tests

  • Before: on main 599c87ffb5, 4 of the 10 cases in TestNewEntriesOnly fail (second turn holds the whole window, nothing-new still sends it, and the two cursor cases). The other 6 pass on main and pin its outcome: full window after a compaction, after a rolled-back turn, on a harness that does not report compaction, in a thread, and the scrub.
  • After: all pass, with the 5 TestContextSince cases and the channel, skill-body, turn-bracket, binding, thread and neutralization suites (291 passed).
  • Whole backend suite on this branch, compared with plain main: the one new failure it showed, the frozen member inventory in test_context_composition_contract_facade.py, now lists the two builder methods and passes with the binding and channel tests (136 passed). The other 210 failed or errored ids are in the failure lists of earlier plain-main runs on this machine (for example a missing h2 module).
  • Gates: flake8, isort, mypy, the black, comment-history and sync-io gates, the flake and wait ratchets (129 passed). test_process_cache_floor_ratchet.py fails only for subagent_persistence.py::_NOT_DUE_MEMO, as on main. The cursor is builder state, not module state.
  • Reproduce: python -m pytest test/test_channel_history.py -n0 -k "TestNewEntriesOnly or TestContextSince"
  • Proof: PROVEN test/test_channel_history.py::TestNewEntriesOnly::test_second_turn_holds_only_entries_added_after_the_first (head 535da6d)
  • Proof: PROVEN test/test_channel_history.py::TestNewEntriesOnly::test_the_full_window_returns_after_a_turn_that_did_not_land (head 535da6d)

Manual verification

Built two turns on one conversation with the real builder and a compaction-reporting stand-in provider: the second turn carried only the new entry (385 characters for one 300-character entry) and the first carried the whole window.

Sibling sweep

Sibling: build_message thread leg -- not affected -- keeps its whole thread window
Sibling: slack/handler_runtime/turn_context.py thread replies -- not affected -- already sends unseen replies only, the pattern copied here
Sibling: reply-format rules (#35756) -- not affected -- taken out at the wire; this block cannot be, because its ages change each turn
Sibling: dashboard turn with a thread parent -- not affected -- passes include_channel_history=False

Pattern harvest

Rule candidate: context re-sent on every turn on a harness that reports compaction keeps a per-session position that rides the existing turn settle, restores the prior value on a rollback, and reads from the last settled position when a build was never settled.

Related Issues

None filed.

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

… new ones

A group-channel turn on a harness that reports compaction is sent the recent-messages window once, then only the entries that arrived since the previous turn. The whole window goes again on a new session, after a compaction, after a turn that did not land, and when no turn settled the last build.
@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners November 19, 2026 16:19
@javenciu
javenciu enabled auto-merge (squash) November 19, 2026 16:19
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

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

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks: nothing is removed, harnesses that don't report compaction keep the old path, and any build that doesn't settle falls back to repeating entries, never dropping them.

Notes

  • The cursor saves tokens only if the channel turn path calls commit_skill_bodies. If a channel turn never settles, every build reads from the unsettled base and sends the whole window again. That is safe but saves nothing, so a test that drives the real Slack handler through commit would show the saving in production.

[DESIGN-REVIEWED] 535da6d

Description read

[DESCRIPTION-READ] 31a4935a310fefe67ea51838c7aff100ad49b399299ffee567e9a8a094a83b61

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

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 535da6de8f01efe8fbd45046b8258bf020ea9bb3 — 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

The fix stops a 4k–16k-token channel window from being re-sent every turn on harnesses that report compaction. One surface has no consumer.

Notes

  • Item 4 — zero consumers: the new context_for(..., *, after=...) keyword is called only by test_channel_history.py (h.context_for("C1", after=newer)). build_message calls context_since directly. Grep context_for( → 1 production caller, which passes no after. Subtraction: delete after from context_for and keep it as return self.context_since(channel_id, thread_ts)[0].
  • Item 3 — oversized: the 6-line restore-from-undo block (if key in self._channel_windows_undo: prior = ... pop ...) appears twice in the diff, in _channel_window_base and in rollback_skill_bodies. Subtraction: have _channel_window_base call rollback_skill_bodies's channel half instead of keeping a second copy.

What this change ships

Inventory (5 items) — 3 justified

Intent: in a group channel, the agent gets recent messages once, then only the ones that arrived since. FIX.

  1. On compaction-reporting harnesses, a top-level channel turn after the first gets only new entries under a new header, or no block at all — justified
  2. Each channel-history entry gets an arrival number; entries loaded from disk get 0 — justified
  3. A per-session position that rolls back on a failed turn and resets on a compaction or a new session — oversized (restore logic copied)
  4. context_for gains an after keyword — zero consumers
  5. Spec bullet describing the new-entries-only rule — justified

[FIRST-PRINCIPLES-REVIEWED] 535da6d

Description read

[DESCRIPTION-READ] 991fe5601e88929e6f980a3e9b410bbe0046471ca58aeb72eacf1ca0dca35e68

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

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — 🔴 changes requested (blocking)

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

The new "only new entries" branch in build_message can never run in production. Every Slack turn passes a non-empty thread_ts, and Slack is the only code that writes ChannelHistory, so the feature this PR adds and documents never runs.

BLOCKING — src/kiro_crew/context_assembly/turn_message.py:713
session_key and not thread_ts and getattr(context_provider, "reports_compaction", False) is True
Trigger: a top-level Slack message in a group channel, on a follow-up turn, served by a harness that reports compaction. Both Slack build sites call build_message(..., thread_ts=thread_ts or msg_ts, context_provider=client) (slack/transport_dispatch.py:1017, slack/handler_runtime/turn.py:876). A Slack event always has a ts, so thread_ts is always set and not thread_ts is False. Slack's events.py:1035 and :1189 are the only places that write to ChannelHistory. The callers that do pass channel_id with no thread_ts (messaging/dispatch.py:1160, telegram/dispatch/turn_engine.py:450) never have history entries for those ids.
Outcome: no production turn ever takes the context_since(after=...) cursor path, and channel context is built exactly as it was before this PR. The new spec line in channel-history.md ("a top-level turn after the first is sent only the entries that arrived since the previous turn") describes behaviour that never happens. The new tests pass only because they call build_message directly with thread_ts=None.
Anchor: defect
Fix: tell a top-level turn apart at the Slack call sites, for example by passing the event's original thread_ts, or a flag set when it is None, alongside the folded thread_ts or msg_ts, and key the cursor branch on that. Add a test that drives a Slack build site, not build_message directly.

[BLOCK-MERGE] 535da6d
[OPUS-REVIEWED] 535da6d

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

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

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 535da6d

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

@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 Nov 19, 2026
@javenciu

Copy link
Copy Markdown
Contributor Author

Closing this one. The Opus review is right: every Slack build site passes thread_ts or msg_ts, so context_for already returns only the current thread's entries, and the full-window path this change targets is not reached by any production caller. The saving it was built for is not there, so there is nothing worth keeping behind the new cursor. Thanks for the review.

@javenciu javenciu closed this Nov 19, 2026
auto-merge was automatically disabled November 19, 2026 17:50

Pull request was closed

@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Nov 19, 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.

1 participant