Skip to content

fix(webhooks): keep the restored-context frame to the intake - #37786

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
fix/cse-c8-webhook-frame
Nov 16, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
fix/cse-c8-webhook-frame

Conversation

@iamwhatever

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Only the webhook intake can write the restored-context frame a resumed webhook session reads.

When a webhook call resumes a saved workflow, the intake wraps the hook's saved summary in a === Restored Context (from prior session) === ... === End Restored Context === frame and puts the posted message after it. Neither input was cleaned of that frame first, so either one could carry its own copy.

Why it matters

A copy of the frame in the posted message or the saved summary reads to the model like the genuine frame the intake wrote.

Not a goal

  • Changing the frame's wording or where it sits in the prompt.
  • Adding the frame to the shared structural-marker scrub (_STRUCTURAL_MARKER_RES): the genuine frame rides the user turn, which that scrub covers, so it would remove the real frame too.
  • Confusable-letter mapping for every marker scrub in the repo.

What changed (motivation → approach → change)

The frame was written around text that was never scrubbed of it. The fix follows the background-context frame's pattern: scrub the content before the frame is minted.

  • context_assembly/markers.py: new neutralize_restored_context_frame. It replaces the phrase restored context (any non-word run between the words) and any line that opens with an == run. Matching a banner line by its shape, not its words, covers look-alike letters and other wording. === inside a line, such as a === b, is kept.
  • hooks_webhook_intake._with_restored_context: scrubs the posted message and the saved summary, then mints the frame. It is now called on every webhook turn, so a forged frame in a message with no saved summary is scrubbed too.
  • docs/inbound-webhooks.md: says what is replaced.

Other untrusted text that reaches the same assembler (memory, history, channel text) is not changed: it sits in the session-context block ahead of the request, where the frame has no meaning, and webhook sessions start fresh with no replay.

Backwards compatibility

Compatible: the frame and guidance text are unchanged; only copies of the frame inside the payload or summary are rewritten.

Removes nothing: no field, route or capability changes; payload text matching the frame is rewritten to [marker-removed].

Tests

  • test/test_webhooks_store.py::TestFreshness::test_restored_context_frame_cannot_be_forged_by_payload_or_summary: forged open/close frames (case, zero-width, fullwidth, look-alike letter, quoted line) in both inputs leave only the genuine two frame lines; a === b survives.

Proof: PROVEN test/test_webhooks_store.py::TestFreshness::test_restored_context_frame_cannot_be_forged_by_payload_or_summary (head 6fdba83)

Manual verification

N/A — unit coverage sufficient: the change is a pure string transform at one call site.

Related Issues

Closes #37784

Sibling sweep

Swept: git grep -n "Restored Context" -- src/
Sibling: src/kiro_crew/session_summary.py:47 -- not affected -- reads the genuine frame prefix, which is unchanged
Sibling: src/kiro_crew/dashboard/chat_turn/turn_context.py -- not affected -- background-context frame already scrubbed the same way

Pattern harvest

Rule candidate: review-prompt
Pattern: a frame minted around untrusted text with no content-time scrub of that frame

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • 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

@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners November 16, 2026 03:54
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 6fdba8353db7ebf7b6dcc5af7b081f9f925cbc6f 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 6fdba8353db7ebf7b6dcc5af7b081f9f925cbc6f — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the scrub runs at the one place the frame is created, follows the existing background-context pattern, and is easy to revert.

Notes

  • The line-start == rule also rewrites ordinary content in webhook messages, such as Markdown ==== heading underlines or banner-style log lines. Keeping this broad is a reasonable trade, but the docs should say that real payload lines starting with == are changed too, not only forged frames.

[DESIGN-REVIEWED] 6fdba83

Description read

[DESCRIPTION-READ] b8af15305fc0b7fad06312e28662365097457cc313d898c21a08fdf830ae9b92

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 6fdba8353db7ebf7b6dcc5af7b081f9f925cbc6f — 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 scrubs both untrusted inputs before the frame is built, which is where the problem starts. It follows the existing neutralize_background_context_frame pattern, and issue #37784 records the defect.

Notes

  • Item 2 — oversized: the scrub matches any line that opens with == (^[^\w\n]*={2,}), not just the frame's two banner lines. So a Markdown underline such as Title\n===== or a ==== log ==== divider in a posted payload also becomes [marker-removed], and the Goal does not ask for that. Subtraction: limit the second pattern to == lines that also carry the word context, and let the RESTORED[\W_]*CONTEXT pattern handle the frame's wording.

What this change ships

Inventory (4 items) — 3 justified

Intent: Make sure a resumed webhook session's restored-context frame can only come from the intake, not from the posted message or the saved summary. This is a FIX.

  1. "Restored Context" wording in a webhook payload or saved summary is now replaced with [marker-removed] — justified
  2. Any payload or summary line that opens with == is replaced, even when it is not a frame — oversized, also catches Markdown underlines and dividers
  3. Webhook turns with no saved summary are now scrubbed too — justified
  4. The inbound-webhooks doc now says what gets replaced — justified

[FIRST-PRINCIPLES-REVIEWED] 6fdba83

Description read

[DESCRIPTION-READ] b0698aa9c5c0cbb76dae3c77567a585d735101e72dc0e62f21a579a468b944b9

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 — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 6fdba83

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

False positive or not applicable? A repository writer can comment:
/ai-review override fable 6fdba8353db7ebf7b6dcc5af7b081f9f925cbc6f 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 6fdba8353db7ebf7b6dcc5af7b081f9f925cbc6f and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 6fdba83

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

@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 Nov 16, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: the decider thinks the change is probably fine but estimates a 15% chance that this change introduces a defect, above the 12% limit for approving on its own.

The PR scrubs the restored-context frame from both the webhook payload and the saved summary before the intake writes the genuine frame. It does this on every webhook turn, using the shared span-local marker matcher (NFKC fold, zero-width removal). I verified the call site, the stale-banner interaction, the length limits downstream, the facade export list and the new test, which would fail without the change. The remaining issues are false positives on benign text (documented), an out-of-scope look-alike edge case, and a missing call-site test. None blocks.

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit 6fdba8353d · 64 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@KiroCrewCaptain
KiroCrewCaptain enabled auto-merge (squash) November 16, 2026 05:47

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The review verified head 6fdba83. The scrub runs where the frame is built, inside the existing try, and off the event loop through asyncio.to_thread. The intake's own stale-context banner matches neither pattern, and both regexes stayed linear on 200 KB pathological inputs. The new test fails on 6 of 6 mutants (each pattern, each scrub call, the early return). The overlapping open PRs #37824 and #36027 touch different hunks.

Done well: the banner is matched by shape on the NFKC-folded view. So two patterns close fullwidth, zero-width, Cyrillic and quoted forgeries, and a === b and the rest of each line stay byte-exact.

Optional:

  • Both rules also rewrite benign payload text on every turn, including turns with no saved context. For example, Title\n===== becomes Title\n[marker-removed], and restored_context keys and conflict markers change too. Narrowing the line rule would lose the look-alike coverage, so the trade is fair. The Backwards compatibility section should still say that benign text changes.
  • src/kiro_crew/docs/inbound-webhooks.md:477 says the whole line is replaced. Only the leading punctuation and = run are replaced.
  • If hooks.py:1216-1217 goes back to main's if saved_context: guard, none of the 163 webhook tests fails. One _run_hook_agent test with an empty saved context and a forged banner would pin this.

@KiroCrewCaptain
KiroCrewCaptain merged commit f386cc6 into main Nov 16, 2026
98 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the fix/cse-c8-webhook-frame branch November 16, 2026 05:47
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 16, 2026
iamwhatever pushed a commit that referenced this pull request Nov 16, 2026
The restored-context frame scrub added in #37786 rewrote ordinary webhook
text: any line opening with `==` (pytest summary banners, setext headings,
merge-conflict markers, `==> step` logs) lost its leading run, and the
phrase rule spanned sentence punctuation, so `Service restored. Context:`
lost two words.

- The phrase gap no longer crosses sentence punctuation or a line break.
- A `==`-opening line still loses its leading run unless it is a known-plain
  shape: after NFKC with invisible characters dropped it is ASCII and is a
  `==>` log arrow, or carries no words outside the pytest run-banner words
  (which covers word-free underlines and conflict markers). The check stays
  fail closed: it never guesses whether a line's words resemble the frame.
- docs/inbound-webhooks.md describes the new rule.

Every forged-frame case in the existing test is still refused.
iamwhatever pushed a commit that referenced this pull request Nov 16, 2026
The restored-context frame scrub added in #37786 rewrote ordinary webhook
text: any line opening with `==` (pytest summary banners, setext headings,
merge-conflict markers, `==> step` logs) lost its leading run, and the
phrase rule spanned sentence punctuation, so `Service restored. Context:`
lost two words.

- The phrase gap no longer crosses sentence punctuation or a line break.
- A `==`-opening line still loses its leading run unless it is a known-plain
  shape: after NFKC with invisible characters dropped it is ASCII and is a
  `==>` log arrow, or carries no words outside the pytest run-banner words
  (which covers word-free underlines and conflict markers). The check stays
  fail closed: it never guesses whether a line's words resemble the frame.
- docs/inbound-webhooks.md describes the new rule.

Every forged-frame case in the existing test is still refused.
iamwhatever pushed a commit that referenced this pull request Nov 18, 2026
The restored-context frame scrub added in #37786 rewrote ordinary webhook
text: any line opening with `==` (pytest summary banners, setext headings,
merge-conflict markers, `==> step` logs) lost its leading run, and the
phrase rule spanned sentence punctuation, so `Service restored. Context:`
lost two words.

- The phrase gap no longer crosses sentence punctuation or a line break.
- A `==`-opening line still loses its leading run unless it is a known-plain
  shape: after NFKC with invisible characters dropped it is ASCII and is a
  `==>` log arrow, or carries no words outside the pytest run-banner words
  (which covers word-free underlines and conflict markers). The check stays
  fail closed: it never guesses whether a line's words resemble the frame.
- docs/inbound-webhooks.md describes the new rule.

Every forged-frame case in the existing test is still refused.
KiroCrewCaptain pushed a commit that referenced this pull request Nov 18, 2026
…7921)

* fix(webhooks): keep ordinary == lines and prose in webhook bodies

The restored-context frame scrub added in #37786 rewrote ordinary webhook
text: any line opening with `==` (pytest summary banners, setext headings,
merge-conflict markers, `==> step` logs) lost its leading run, and the
phrase rule spanned sentence punctuation, so `Service restored. Context:`
lost two words.

- The phrase gap no longer crosses sentence punctuation or a line break.
- A `==`-opening line still loses its leading run unless it is a known-plain
  shape: after NFKC with invisible characters dropped it is ASCII and is a
  `==>` log arrow, or carries no words outside the pytest run-banner words
  (which covers word-free underlines and conflict markers). The check stays
  fail closed: it never guesses whether a line's words resemble the frame.
- docs/inbound-webhooks.md describes the new rule.

Every forged-frame case in the existing test is still refused.

* fix(webhooks): cut a frame banner split over lines again

A bare == line next to another plain banner loses its opening whatever
the words between them, so ===/End Restord Contxt/=== is scrubbed as on
main, blank lines or not. A frame word anywhere in the body (letters only,
stand-in digits or symbols read as letters, look-alike scripts) turns every
plain == shape off and widens the phrase match. One heading underline, one
conflict marker, ==> arrows, pytest banners and prose without frame words
still pass. Adds 'ran' to the pytest banner words and one test per
plain-shape guard.

---------

Co-authored-by: Joe Guo <zejiangg@amazon.com>
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.

Webhook intake: the restored-context frame is not neutralized in the posted message or the saved summary

2 participants