Skip to content

fix(artifacts): anchor the origin session key at the true end of input - #18781

Merged
iamwhatever merged 1 commit into
mainfrom
fix/18727-origin-session-key-true-end
Oct 10, 2026
Merged

iamwhatever merged 1 commit into
mainfrom
fix/18727-origin-session-key-true-end

Conversation

@pepmach

@pepmach pepmach commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: An artifact saved with an origin session key that carries a trailing newline is attributed to no session, like any other value outside the key grammar, instead of being stored under a key no session has.

The dashboard's artifact create route (POST /api/artifacts) and the conversation-log materialize route (POST /api/artifacts/materialize) accept an origin_session_key such as "dashboard:abc\n" and store it unchanged. The validator's docstring says the value must match the permitted grammar, and the grammar has no newline in it. The artifact store compares session keys exactly, so an artifact stored this way never shows up in the ?session= filter or under the right Source entry for the real dashboard:abc session.

Reproduced on main with the real helper:

from kiro_crew.dashboard.handlers.artifacts import _clean_origin_session_key
_clean_origin_session_key("dashboard:abc\n")   # returns 'dashboard:abc\n', expected ''

Why it matters

Any client that sends a key with a stray trailing newline (a copied value, a script that reads a line from a file) gets an artifact that exists but belongs to a session nobody can select. The in-session Artifacts tab does not list it, and the Source column cannot resolve it. The same gap was closed for artifact tags in #12892 (reported as #10900); this validator was left on the old anchor.

Not a goal

  • Changing the session key grammar itself (the character set and the 1 to 128 length cap are unchanged).
  • Validating session keys inside the artifact store; the HTTP boundary remains the one place that judges a client-supplied origin key.
  • Changing how the ?session= and ?touched_by= read filters handle a validation miss (they keep the raw value by design, so a miss returns an empty list rather than another session's artifacts).
  • Moving the dashboard handlers package under the structural half of the regex anchor contract test; other modules in that package still carry the $ shape and that sweep is a separate change.
  • Rewording the publish route's 400 text, which spells the provider grammar as ^[a-z0-9-]{1,32}$. The characters and length it names are exactly what the code enforces; only the anchor spelling differs, and a Python-specific \Z would read worse to a user than it helps.

What changed (motivation → approach → change)

Symptom: a key with one trailing newline passes _clean_origin_session_key and is stored as-is.

Root cause: _SESSION_KEY_RE was ^[A-Za-z0-9:_.\-]{1,128}$ and was applied with .match. In Python, $ matches at the end of the string and also immediately before a final newline, so "dashboard:abc\n" satisfies the pattern.

Change: the pattern now ends in \Z, which matches only at the true end of the input. This is the same shape #12892 applied to the two tag validators. The one sibling in the same handler file, the publish-provider name gate _ARTIFACT_PROVIDER_RE (^[a-z0-9-]{1,32}$, also applied raw with .match), gets the same anchor. Nothing else in the handler, the grammar or the store changes.

Area Change
src/kiro_crew/dashboard/handlers/artifacts.py _SESSION_KEY_RE and _ARTIFACT_PROVIDER_RE anchored at \Z; comments state why
test/test_artifacts_handlers.py TestCleanOriginSessionKey: the trailing-newline cases, valid keys, other newline placements, non-string and over-long values
test/test_regex_anchor_contract.py both handler patterns registered in the behavioral half of the trailing-newline contract
docs/system-specs/modules/artifacts.md one sentence: the grammar is judged against the whole value, anchored at the true end of input

Behaviour after the change: a key outside the grammar, trailing newline included, collapses to "" (no originating session) on the two write routes, exactly as a non-string, an empty string, an over-long key or an illegal character does today. A provider name with a trailing newline is now rejected at the regex gate with the existing provider must match error instead of reaching the provider lookup; that gate answers 400 before any audit record is written, as it does for every other malformed provider name on main.

Backwards compatibility

Compatible: every value the grammar describes still passes; the only inputs that change outcome are ones ending in a newline, which no client sends on purpose and which the grammar never admitted. The three origin-key writers on main (the dashboard client's createArtifact and materialize calls, WidgetFrame's save, and the MCP artifact_save tool, which passes the resolved session key) all send a bare slot key with no newline.

Removes nothing: the validator tightens by one character position; no route, field, kind or function is removed or renamed.

Tests

  • test/test_artifacts_handlers.py::TestCleanOriginSessionKey
    • test_trailing_newline_collapses_to_no_origin: _clean_origin_session_key("dashboard:abc\n") == "" and the same with a 128-character key plus newline. Red on main (AssertionError: assert 'dashboard:abc\n' == ''), green with the fix.
    • test_key_inside_the_grammar_is_returned_unchanged: chat-2, dashboard:chat-2, cron:foo, a Slack ts, a 128-character key.
    • test_any_other_newline_placement_still_collapses: embedded, leading and double trailing newline.
    • test_values_outside_the_grammar_still_collapse: None, an int, a list, "", 129 characters, a space, markup.
  • test/test_regex_anchor_contract.py: artifact-origin-session-key and artifact-publish-provider entries. Both test_valid_input_plus_newline_is_rejected cases were red on main and are green with the fix; the accept cases pass on both.

Gates run locally on the branch: black gate, isort, flake8, mypy, subprocess-encoding gate, comment-history gate, docs lint, and scripts/local-gate.py (the change-related backend and frontend sets).

Manual verification

N/A, unit coverage sufficient: the change is a one-token anchor swap on two compiled patterns, and the unit tests exercise the exact helper the two HTTP routes call with the exact value from the report.

Screenshots / video

No user-visible UI change: a backend validator anchor, no frontend file touched.

Related Issues

Fixes #18727

Same defect class as #10900, fixed for tags in #12892.

Pattern harvest

Rule candidate: semgrep
Pattern: a re.compile pattern ending in $ (without re.MULTILINE) applied with .match or .search to client-supplied input; $ also matches before a trailing newline, so the validator admits valid + "\n". Anchor with \Z or use fullmatch. test/test_regex_anchor_contract.py already enforces this structurally for the packages it registers; the dashboard handlers package is not yet one of them.

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

Contributed under the project's Apache-2.0 license, per the Licensing section of CONTRIBUTING.md.

@pepmach
pepmach requested review from a team and KiroCrewCaptain as code owners October 10, 2026 08:04
@pepmach
pepmach requested a review from CrysisDeu October 10, 2026 08:04
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 10, 2026
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 9370f21931ab9c4c7dae181f105146e12fc06153 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

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 9370f21931ab9c4c7dae181f105146e12fc06153 — 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: PASS

The fix only covers artifacts.py. About 19 other $-anchored patterns in dashboard/handlers/ keep the same flaw; the description defers that sweep as a separate change.

Not justified as shipped

  • Item 2 — rides along: "The one sibling in the same handler file, the publish-provider name gate _ARTIFACT_PROVIDER_RE … gets the same anchor". No issue reports it. It is declared, it is one token, and it adds no surface.

What this change ships

Inventory (4 items) — 3 justified

Intent: an artifact saved with a stray trailing newline in its origin session key should be attributed to no session, not stored under a key nobody can select. This is a FIX (#18727, reproduced on main, with a test that fails on base).

  1. An origin session key ending in a newline now collapses to "no session" on the create and materialize routes — justified
  2. A publish provider name ending in a newline now gets the existing 400 "provider must match" error — rides along, a sibling with no reported defect
  3. Both patterns are registered in the behavioral half of the regex-anchor contract test — justified
  4. The artifacts spec gains one sentence: the key is checked against the whole value, anchored at \Z — justified

[FIRST-PRINCIPLES-REVIEWED] 9370f21

Description read

[DESCRIPTION-READ] afa7d25dd497580ffb026d4c104c0adae50a8ee1b254bc25de96c961957d902b

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 Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing to check.

[DESIGN-REVIEWED] 9370f21

Description read

[DESCRIPTION-READ] 3ace57da46ca944c1b1a4ffbfbbdaceab7ba75834c8ebbf8e885be55c5e46215

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 Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 9370f21

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

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

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 9370f21

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

@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 Oct 10, 2026
@pepmach

pepmach commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

CI note for head aca11ef: the red checks on this head are inherited from main, not caused by this change.

  • Build Wheel, Build Desktop (ubuntu-22.04), Build Desktop (ubuntu-22.04-arm) and E2E (stub ACP backend, offline) fail in their "Build frontend" step with gen-ui-index: src/kiro_crew/docs/ui-index.generated.json is stale.
  • Frontend Tests (1) fails src/uiLocations/uiIndex.test.ts > the committed index > projects every Settings control, joined by its stable id (expected 238 entries to deeply equal 239) on the same stale file; the other 3578 tests in that shard pass.
  • Coverage Gate fails closed because that shard did not succeed, and PR Readiness reflects the same set.

This PR changes 4 files (src/kiro_crew/dashboard/handlers/artifacts.py, test/test_artifacts_handlers.py, test/test_regex_anchor_contract.py, docs/system-specs/modules/artifacts.md) and touches neither the website nor the index. main is red on the same cause at this PR's base d3f462f: Build run https://github.com/kirodotdev/KiroCrew/actions/runs/38036318549 (Build Wheel and all Build Desktop jobs fail on the same gen-ui-index message) and CI run https://github.com/kirodotdev/KiroCrew/actions/runs/38036318449 (Frontend Tests (1), E2E (stub ACP backend, offline) and Coverage Gate fail there too).

No rerun: the failure is deterministic until main carries the regenerated index. The index is not regenerated in this PR. This branch will be rebased once main carries the fix (#18753 regenerates the index).

The dashboard artifact create and conversation-log materialize routes
validate a client-supplied origin_session_key with a `$`-anchored pattern
applied through `.match`. Python's `$` also matches immediately before a
final newline, so "dashboard:abc\n" passed the validator and was stored
unchanged; the store compares session keys exactly, so the artifact never
matched the real session again in the session filter or the Source column.

Anchor the pattern at `\Z`, the true end of input, the same shape PR #12892
used for the two artifact tag validators. The one sibling in the same
handler, the publish-provider name gate, is matched raw with `.match` too
and gets the same anchor. The grammar itself and the store are unchanged: a
key with a trailing newline now collapses to the no-origin value exactly as
every other value outside the grammar does.

Tests: the red case `_clean_origin_session_key("dashboard:abc\n") == ""`
plus valid keys, embedded newlines and non-string values in
test_artifacts_handlers.py; both handler patterns registered in the
trailing-newline contract test.

Fixes #18727
@pepmach
pepmach force-pushed the fix/18727-origin-session-key-true-end branch from aca11ef to 9370f21 Compare October 10, 2026 09:47
@pepmach

pepmach commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main d32c084 (same single commit, now 9370f21; the ui-index regeneration on main is included, so the Build frontend and uiIndex.test reds cited above should clear on this head).

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 10, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This fixes _clean_origin_session_key accepting "dashboard:abc\n": under .match, Python's $ also matches before a final newline. Such a key was stored as-is and never matched its real session again. The root cause is fixed: _SESSION_KEY_RE and its sibling _ARTIFACT_PROVIDER_RE in src/kiro_crew/dashboard/handlers/artifacts.py:126,222 now end in \Z, the same shape #12892 used for the tag validators. Character set and length cap are unchanged. Behaviour changes only for values ending in a newline, which now collapse to "" (no origin) or get the existing 400 "provider must match" error. The ?session= read filter deliberately keeps the raw value, so nothing widens there. The tests prove the fix. TestCleanOriginSessionKey's trailing-newline cases and the two new test_regex_anchor_contract entries fail on main and pass here, and the accept cases pin the grammar. All checks are green on 9370f21, every AI lane passes, and there are no reviews or change requests. Small, correct and safe. — crew-pr-reviewer

@iamwhatever
iamwhatever enabled auto-merge (squash) October 10, 2026 19:05
@iamwhatever
iamwhatever merged commit 2081ef3 into main Oct 10, 2026
102 checks passed
@iamwhatever
iamwhatever deleted the fix/18727-origin-session-key-true-end branch October 10, 2026 19:06
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 10, 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.

Artifact HTTP routes accept an origin_session_key with a trailing newline

2 participants