Skip to content

fix(dashboard): refuse malformed /context keys and scope the dedup to the live session (replaces #11666) - #18759

Closed
iamwhatever wants to merge 1 commit into
mainfrom
replace/11666-context-key-and-session
Closed

iamwhatever wants to merge 1 commit into
mainfrom
replace/11666-context-key-and-session

Conversation

@iamwhatever

Copy link
Copy Markdown
Contributor

Problem

POST /api/chat/slots/{slot}/context accepts an optional contextKey, which the
live-queue reload-dedup compares to decide whether an arriving post repeats one
already queued. That makes the key an identity, and the boundary silently rewrote
it before the comparison, or matched the wrong entry:

  • an overlong key was clipped to the source-label cap, so two distinct keys
    sharing a 64-char prefix collapsed onto one;
  • a key with a leading/trailing control char or whitespace was compared after
    the strip, so "\nv7" matched the earlier "v7";
  • the dedup could match an expired entry (discarded by the drain, never
    delivered) or one posted under a different session (before a cron/rebind).

In each case the second post matched the first, was answered 200, and appended
nothing — content acknowledged and silently dropped, with no surface reporting it.
A false 200 is worse than a 400: the caller has no reason to retry.

What changed

  • chat_handlers.py — new _validate_context_key refuses a non-string, a raw
    control char (checked before the strip), leading/trailing whitespace, and an
    over-cap key, with invalid_context_key / context_key_too_long codes, mirroring
    _validate_source. The /context dedup now suppresses only an unexpired,
    same-content
    entry owned by the current session (positive ctxSession
    stamp), and the suppressed return is SEL-audited like every other 200.
    context_key/ctxSession are threaded through _build_pending_context_entry /
    _enqueue_pending_context, live-queue only (neither survives a reload).
  • chat_turn/turn_context.py — drain_pending_context drops a keyed entry whose
    ctxSession names a session this slot has since left (re-ported here: the function
    moved out of chat_runner.py on main).

Tests

test/test_ctx_boundary_key_and_label.py (new, 11 cases): refusals keep the key
verbatim, the ownership/content/expiry rules for a genuine repeat, and the drain's
refusal to deliver a keyed entry into a session it was not posted under.


Replaces #11666 by @rnoack1, re-ported onto main after drain_pending_context moved
to chat_turn/turn_context.py. Original authorship preserved (commit author + a
Co-authored-by trailer).

Fixes #18757

… live session

`POST /api/chat/slots/{slot}/context` treats `contextKey` as a dedup identity, but
the boundary rewrote it before comparing (clipped an overlong key, stripped control
chars/whitespace) and matched expired or cross-session entries — so a repost was
answered 200 and silently dropped nothing into the queue.

- `_validate_context_key` refuses a non-string, a control char (before strip),
  leading/trailing whitespace, and an over-cap key with 400s, mirroring `_validate_source`.
- The /context dedup now suppresses only an unexpired, same-content entry owned by the
  current session (positive `ctxSession` stamp), and audits the suppressed return.
- `drain_pending_context` (now in `chat_turn/turn_context.py`) drops a keyed entry whose
  `ctxSession` names a session this slot has since left.
- `context_key`/`ctxSession` are live-queue only and do not survive a reload.

Fixes #18757
Replaces #11666

Co-authored-by: Robert Noack <69695077+rnoack1@users.noreply.github.com>
@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners October 10, 2026 07:06
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 71c89da4c09d97a6fc7374ba47117308acc0f965 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🔴 BLOCK

Premise-level review of 71c89da4c09d97a6fc7374ba47117308acc0f965 — 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: BLOCK

The described "fix" targets a contextKey dedup that doesn't exist on base. This PR adds that dedup as new public API surface, and nothing in the repo calls it.

Not justified as shipped

  • Item 1 — zero consumers: the description says the endpoint "accepts an optional contextKey, which the live-queue reload-dedup compares". On base 616690598, git grep "contextKey\|ctxSession\|context_key" over src website/src docs finds 0 hits outside the unrelated crew_log context_key_unit. The PR is adding the key and the dedup, not fixing them.
  • Item 2 — zero consumers: the 200 response that suppresses a repost is a new behaviour. No caller in the repo sends contextKey (counted 0 in website/ and src/ beyond the handler).
  • Item 3 — rides along: the drain now drops entries whose ctxSession doesn't match. Only entries created by Item 1 carry that stamp, so on base it removes no harm.
  • Item 4 — rides along: max_age and ephemeral swap places in _enqueue_pending_context and _build_pending_context_entry, and the /note call site is rewritten. Nothing needs the reorder.

What this change ships

Inventory (5 items) — 1 justified

Intent: stop /context posts from being acknowledged with 200 and then silently dropped. Framed as a FIX, but it is an ADDITION.

  1. /context accepts a new contextKey and refuses malformed ones with a 400 — zero consumers
  2. A keyed repost of the same content in the same session gets a 200 and queues nothing — zero consumers
  3. The drain skips keyed entries from a session the slot has left — rides along
  4. Helper parameter order swapped, /note call site rewritten — rides along
  5. 11 new boundary tests — justified

Blockers

  • Framing contradicted by the diff, and new one-way surface with no consumers. Issue /context contextKey: malformed key rewritten, and dedup matches expired/cross-session entries → content lost under a 200 #18757 and the description describe rewriting and matching behaviour of a key that base never reads. Every contextKey line is added by this diff, and 0 callers send the key.
    Subtraction: delete _validate_context_key, the _ctx_key dedup block, the contextKey/ctxSession stamping, the ctxSession arm in drain_pending_context, and the parameter reorder.
    Clears when: a caller in the repo sends contextKey, or a base commit shows the dedup already exists.

[FIRST-PRINCIPLES-REVIEWED] 71c89da

Description read

[DESCRIPTION-READ] e0c45d5246ea1657fb2edf0c8f552e6c573b9b2ccecd049ba88a50c3c6418f0f

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

Design Review (Opus 5.5) — 🟡 CONCERNS

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

Design-Verdict: CONCERNS

There is no contextKey dedup on main for this PR to fix. The PR adds the dedup as new public API, no caller sends the key, and the spec is unchanged.

Watch

  • The premise doesn't hold on this base. The description says "the boundary silently rewrote [the key] before the comparison… answered 200, and appended nothing." On the base (616690598) nothing in chat_handlers.py or anywhere else reads contextKey. The "silently dropped" harm only existed on the fix(chat): refuse a contextKey rather than rewriting it at the boundary #11666 branch. So this PR adds a dedup that can answer 200 and enqueue nothing, plus the guards for it, and presents that as a bug fix.

    website/src/api/client/chat.ts:123 chatSlotContext sends no key, and neither does any other caller. That leaves a new request field, two new 400 codes, and a new drain-time drop rule with nothing using them. The real problem for /context contextKey: malformed key rewritten, and dedup matches expired/cross-session entries → content lost under a 200 #18757 has not been shown.

    Clears when: the description says this is a new feature and names the caller that needs reload-dedup (and the PR wires that caller up), or the dedup is dropped until a caller exists.

  • Public surface without a spec. contextKey, invalid_context_key / context_key_too_long, and "drain discards a keyed entry from a departed session" are new /context contract. No spec under docs/system-specs/ is updated, which AGENTS.md requires in the same commit.

    Clears when: the owning dashboard spec documents the field, its refusals, its live-queue-only lifetime, and the drain drop.

[DESIGN-REVIEWED] 71c89da

Description read

[DESCRIPTION-READ] 1d1bb1e8aa833a6757f8eee669b731cd17a6b92ef2172c8508ae6fc33bbb1c17

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

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 71c89da

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

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

FINDING — src/kiro_crew/dashboard/chat_handlers.py:10490 — a repost with a matching key is answered 200 but appends nothing, and it does not refresh the existing entry's injectedAt/maxAge. Take a POST at t=0 with maxAge:60, then the same key and content at t=59: the match on not context_entry_expired(e, _now) suppresses the second post. The original then expires at t=60, and drain_pending_context discards it at the next turn (t=61), so content the API acknowledged never reaches the model → Fix: on _match, set injectedAt/maxAge (and ephemeral) from the new post before returning, or compare maxAge as part of the match.

[OPUS-REVIEWED] 71c89da

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

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

@iamwhatever

Copy link
Copy Markdown
Contributor Author

Closing this without merging.

On re-check against current main, the premise this change rests on does not hold: contextKey and ctxSession appear nowhere in src/, website/src/, or docs/ on main, and no caller sends a contextKey. There is no live-queue dedup keyed on contextKey to fix — so this PR would add that key, its dedup, and the cross-session stamping as new, dormant public-API surface with zero consumers, rather than fixing an existing behaviour. The First Principles review flagged exactly this.

The code port itself was clean (the drain hunk re-homed to chat_turn/turn_context.py after it moved off chat_runner.py, tests green), but a correct port of a change whose premise is false on main is not a change worth landing.

Original work: #11666 by @rnoack1 — left open for a maintainer to decide whether the /context boundary should grow a keyed dedup at all.

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.

/context contextKey: malformed key rewritten, and dedup matches expired/cross-session entries → content lost under a 200

2 participants