Skip to content

fix(runtime): populate request headers in runtime error context - #6287

Merged
BenTaylorDev merged 3 commits into
CopilotKit:mainfrom
rodboev:fix/2716-error-request-headers
Aug 12, 2026
Merged

BenTaylorDev merged 3 commits into
CopilotKit:mainfrom
rodboev:fix/2716-error-request-headers

Conversation

@rodboev

@rodboev rodboev commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CopilotRuntime exposes an onError callback, but the v1-to-v2 delegation drops it before server request processing. Runtime failures therefore can't provide the incoming request headers that applications use to correlate errors with a user or tenant.

Root cause

The legacy constructor retains the callback type, while delegated runtime options omit it. Common, SSE, and Intelligence handlers consume failures inside their own boundaries, before a generic endpoint hook can reconstruct the legacy event.

Changes

  • Add one internal runtime reporter that snapshots incoming Fetch request headers.
  • Attach the configured legacy callback to the delegated runtime instance.
  • Route common, SSE, and Intelligence agent-run failures, including standard RUN_ERROR events, through that reporter exactly once.
  • Redact sensitive request headers (authorization, proxy-authorization, cookie, set-cookie, x-api-key, api-key, and the CopilotKit public-key header) before they reach the onError event.
  • Preserve responses, stream close behavior, telemetry, cleanup, logging, endpoint hooks, and callback isolation.
  • Add production-path regressions.

Compatibility

The existing CopilotErrorEvent type and optional context.request.headers field remain unchanged. Header names and values come from the failing request's Fetch Headers object, with sensitive credentials stripped before the event is emitted. The callback receives a fresh record, so mutation cannot alter the request or a later event.

Out of scope

React provider propagation, Chat, CopilotMessages, the deprecated runtime-client hook, redaction policy, HTTP response exposure, v2 endpoint-hook semantics, and non-agent runtime routes remain outside this slice.

Related PRs and Issues

Addresses #2716.

Scope follows #2716 (comment). Runtime error-routing precedent: #2143.

Test plan

  • Runtime error regression and reporter tests, 12 + 4 tests passed. Covers public routing, sanitized header propagation with credential redaction, callback rejection containment, snapshots, mutation isolation, and malformed requests.
  • SSE and Intelligence telemetry tests, 7 + 14 tests passed. Covers agent-run failures, RUN_ERROR, setup boundaries, exact-once reporting, telemetry, cleanup, and response preservation.
  • Runtime preservation suites, 57 + 129 + 56 tests passed. Existing request, endpoint-hook, and runtime-library behavior remains intact.
  • Typecheck, formatting, and lint passed on changed runtime files; lint reported four pre-existing warnings.
  • CI green (static / quality, test / unit on Node 20/22/24).

@BenTaylorDev BenTaylorDev 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.

The reporter design is good — the Symbol-keyed attachment threads the handler without widening the public options type, report() is defensively wrapped so a throwing or rejecting handler can't take down a run, and the phase enum gives real triage value. Runtime-side error reporting is worth having. One change needed before it lands.

Request headers are captured unredacted

runtime-error-reporter.ts:

headers: Object.fromEntries(request.headers.entries()),

That puts every inbound header into the CopilotErrorEvent — Authorization: Bearer …, Cookie, x-api-key, and the CopilotKit public-key header included — and report() hands the event straight to the caller's onError with no sanitization in between.

To be precise about the exposure: this doesn't leave the process on its own. It goes to the application's handler, and where it ends up depends on what the developer does with it. But onError is commonly wired to Sentry, Datadog, or PostHog, and a framework can't assume the sink scrubs — so handing raw credentials across that boundary is an unsafe default even when it's often harmless in practice.

What convinced me this isn't just my preference: context.request.headers has existed in CopilotRequestContext for a while, and nothing in the repo populates it. Two places build this exact structure and both deliberately stop short of headers:

  • packages/react-core/src/hooks/use-copilot-runtime-client.ts:44-48 — sets operation, url, startTime
  • packages/runtime/src/lib/runtime/__tests__/copilot-runtime-error.test.ts:36-41 — the canonical type-structure test, sets operation, method, url, startTime

There's also no redaction helper anywhere to reuse (lib/logger.ts redacts only pid/hostname), so this would be establishing the pattern rather than following one.

The ask: filter before assigning — either an allow-list of useful diagnostic headers (content-type, user-agent, x-request-id, and similar) or a deny-list covering at minimum authorization, cookie, set-cookie, x-api-key. Either is fine by me; the allow-list fails safe as new header names appear.

One knock-on: runtime-error-reporter.test.ts:55 asserts context.request.headers toEqual the full supplied map, so it currently pins the unredacted behavior. That assertion should flip to proving redaction — otherwise a later attempt to add filtering will look like a regression.

Also

Please drop .changeset/populate-runtime-error-request-headers.md — releases go through nx release and there are no files under .changeset/ on main. (Same note I left on #6291; your #6294 gets this right and says so in its description.)

Nothing else blocking. Happy to re-review as soon as the header filtering is in.

@rodboev

rodboev commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the close read, and for the note that context.request.headers had never been populated anywhere. Filtering it before it crosses the onError boundary is the right call.

Handled both:

  1. Header redaction. runtime-error-reporter.ts now routes the request headers through a sanitizeRequestHeaders helper backed by a deny-list (authorization, cookie, set-cookie, x-api-key). Authorization and cookie values stay in the request for the handler's own use, but never reach the CopilotErrorEvent. I went with a deny-list over an allow-list so x-request-id, user-agent, and content-type still surface for error correlation. The reporter test now carries Authorization and Cookie on the request and asserts they are absent from the emitted event while the safe headers come through, and the mutation-isolation assertion still holds.
  2. Changeset removed. Dropped .changeset/populate-runtime-error-request-headers.md. Also removed files under .changeset/ from all other open PRs.

Re-review whenever you're ready.

BenTaylorDev added a commit that referenced this pull request Aug 6, 2026
## What does this PR do?

Community PRs keep arriving with `.changeset/*.md` files even though the
repo migrated off Changesets to conventional-commit-driven releases.
`.changeset/` has now been deleted from `main` twice (`5afa55f067` on
2026-06-16, `1e5ba689e0` on 2026-07-29) and **five open PRs carry
changeset files today** (#6287, #6289, #6290, #6292, #6346).

Three mechanisms keep feeding it:

1. **Stale forks.** `rodboev/CopilotKit`'s default branch still contains
10 of the pre-cleanup `.changeset/*.md` debris files. Three of the five
open PRs come from that fork — the contributor's agent opens the repo,
sees a directory full of changesets, and adds one more. (No
`config.json`, and `@changesets/cli` isn't installed anywhere, so these
are hand-written by agents, not CLI output.)
2. **Merging stale PRs re-seeds `main`.** The two files Tyler removed in
`1e5ba689e0` arrived via 2026-06-10-authored branches (#2910, #5360)
merged on 2026-07-25 — they sat on `main` for four days, and anyone who
forked in that window inherited the directory. His hunch in that commit
message was right.
3. **Convention inference, uncontradicted.** #6346 is from a branch in
this repo, where `.changeset/` does *not* exist, and it still has one.
The repo reads as a Changesets repo: pnpm workspace monorepo,
per-package `CHANGELOG.md` in Changesets' exact `### Patch Changes`
output format, `chore: release monorepo vX.Y.Z` release PRs. Nothing in
`CONTRIBUTING.md`, the PR template, `AGENTS.md`, `CLAUDE.md`, or
`.claude/docs/` said otherwise, so the guess was well-supported.

This PR closes all three off:

- **`CONTRIBUTING.md`** — new "Changelogs and releases — do not add a
changeset" section: we did use Changesets, `scripts/release/` now builds
changelogs from commit subjects, `.changeset/*.md` is inert, write a
good conventional commit subject instead, and leave versions/changelogs
to maintainers. Includes a note to rebase old forks.
- **`AGENTS.md` / `CLAUDE.md`** — the same rule as an Essentials bullet.
This is the highest-leverage change: the contributors doing this are
coding agents, and agents load these files automatically while mostly
not reading `CONTRIBUTING.md`.
- **`static / check binaries`** — fail the PR on added `.changeset/*`
files, so this stops depending on review catching it (which is what
failed in July and restarted the loop). Added to the existing
forbidden-files gate rather than a new workflow: it already runs on
every PR to `main`, is fork-safe (`contents: read`, no secrets), and has
exactly this `git diff --name-only origin/BASE...HEAD` + `VIOLATIONS`
shape. Filters on `--diff-filter=AM` so a PR that *deletes* stale
changesets still passes.
- **`.oxfmtrc.json`** — drop the ignore entry for
`.github/actions/changesets-action/src/run.ts`, a path that hasn't
existed for a long time. It was the last grep-visible "we use
changesets" signal in a root config file.

## Related PRs and Issues

- Follows up `1e5ba689e0` ("fix: remove all changesets"), whose commit
message asked for exactly this: a durable record of the decision that
future agents can find.
- Open PRs that would be caught by the new gate: #6287, #6289, #6290,
#6292, #6346.

## Testing

Docs + CI-config change, so verification focused on the guard.
`actionlint` was run on the workflow, then the step body was extracted
with `yq` and executed against real branches.

**Lint / parse:**
```
$ actionlint .github/workflows/static_check-binaries.yml
actionlint: clean
$ python3 -c "import json; json.load(open('.oxfmtrc.json'))"   # oxfmtrc still valid JSON
oxfmtrc JSON OK
```

**True positive** — real head of #6292, via `yq
'.jobs.check-binaries.steps[1].run'` piped to bash with `BASE_REF=main`:
```
::error::Changeset files detected in PR:
.changeset/enable-mcp-apps-tool-filters.md
This repo no longer uses Changesets — releases are driven by conventional commit subjects (see scripts/release/).
Nothing reads .changeset/*.md. Delete these files and describe the change in your commit subject instead.
See the 'Changelogs and releases' section of CONTRIBUTING.md.

This PR contains files that should not be committed (see the errors above).
Please remove them and update your .gitignore if needed.
exit=1
```

**True negative** — same script on this branch, which has five changed
files and no changesets:
```
$ git diff --name-only origin/main...HEAD
.github/workflows/static_check-binaries.yml
.oxfmtrc.json
AGENTS.md
CLAUDE.md
CONTRIBUTING.md
$ BASE_REF=main bash step.sh
No binary artifacts or oversized files detected.
exit=0
```

**Delete-safety** — a commit that *removes* changesets must not be
punished. Using the real cleanup commit (`8806f668d1...1e5ba68`):
```
unfiltered:                      with --diff-filter=AM (what the gate uses):
.changeset/coalesce-...md        (empty)
.changeset/fix-parallel-...md
```

Not verified locally: the gate firing in real GitHub Actions — that
needs this PR's own CI run (the `static / check binaries` check on this
PR exercises the true-negative path).

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@pkg-pr-new

pkg-pr-new Bot commented Aug 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

@copilotkit/a2ui-renderer

pnpm add https://pkg.pr.new/@copilotkit/a2ui-renderer@6287

@copilotkit/agentcore-runner

pnpm add https://pkg.pr.new/@copilotkit/agentcore-runner@6287

@copilotkit/angular

pnpm add https://pkg.pr.new/@copilotkit/angular@6287

@copilotkit/channels

pnpm add https://pkg.pr.new/@copilotkit/channels@6287

@copilotkit/channels-core

pnpm add https://pkg.pr.new/@copilotkit/channels-core@6287

@copilotkit/channels-discord

pnpm add https://pkg.pr.new/@copilotkit/channels-discord@6287

@copilotkit/channels-intelligence

pnpm add https://pkg.pr.new/@copilotkit/channels-intelligence@6287

@copilotkit/channels-slack

pnpm add https://pkg.pr.new/@copilotkit/channels-slack@6287

@copilotkit/channels-teams

pnpm add https://pkg.pr.new/@copilotkit/channels-teams@6287

@copilotkit/channels-telegram

pnpm add https://pkg.pr.new/@copilotkit/channels-telegram@6287

@copilotkit/channels-ui

pnpm add https://pkg.pr.new/@copilotkit/channels-ui@6287

@copilotkit/channels-whatsapp

pnpm add https://pkg.pr.new/@copilotkit/channels-whatsapp@6287

@copilotkit/core

pnpm add https://pkg.pr.new/@copilotkit/core@6287

@copilotkit/react-core

pnpm add https://pkg.pr.new/@copilotkit/react-core@6287

@copilotkit/react-native

pnpm add https://pkg.pr.new/@copilotkit/react-native@6287

@copilotkit/react-textarea

pnpm add https://pkg.pr.new/@copilotkit/react-textarea@6287

@copilotkit/react-ui

pnpm add https://pkg.pr.new/@copilotkit/react-ui@6287

@copilotkit/runtime

pnpm add https://pkg.pr.new/@copilotkit/runtime@6287

@copilotkit/runtime-client-gql

pnpm add https://pkg.pr.new/@copilotkit/runtime-client-gql@6287

@copilotkit/sdk-js

pnpm add https://pkg.pr.new/@copilotkit/sdk-js@6287

@copilotkit/shared

pnpm add https://pkg.pr.new/@copilotkit/shared@6287

@copilotkit/sqlite-runner

pnpm add https://pkg.pr.new/@copilotkit/sqlite-runner@6287

@copilotkit/voice

pnpm add https://pkg.pr.new/@copilotkit/voice@6287

@copilotkit/vue

pnpm add https://pkg.pr.new/@copilotkit/vue@6287

@copilotkit/web-components

pnpm add https://pkg.pr.new/@copilotkit/web-components@6287

@copilotkit/web-inspector

pnpm add https://pkg.pr.new/@copilotkit/web-inspector@6287

commit: 3a9eda8

@BenTaylorDev BenTaylorDev 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.

Verified the redaction and the changeset removal. Both land as asked.

runtime-error-reporter.ts:37-54 strips authorization, proxy-authorization, cookie, set-cookie, x-api-key, api-key, and the CopilotKit public-key header before the event is built, and the test now proves it rather than pinning the old behavior — the toEqual fixes the surviving set to exactly the two safe headers, the per-name not.toHaveProperty assertions cover each credential, and asserting request.headers.get("authorization") still resolves shows the redaction is a copy, not a mutation of the request. Deny-list over allow-list is the right call given you want x-request-id and user-agent for correlation.

CI is green across the board now that it's been kicked off — full unit matrix, check-types, oxlint, format, package-quality, and every integration suite. I also merged the branch onto current main locally (clean, no conflicts) and ran the packages/runtime suite there: 2017 passed, and the only failures were 10 that fail identically on unmodified main in my environment. The end-to-end tests in copilot-runtime-error.test.ts are what sold me: they drive a real Request through createCopilotRuntimeHandler, so the header population is demonstrated on the production path rather than against a stubbed reporter.

Two follow-ups, neither blocking:

  1. A correction to my earlier review — I said there was no redaction helper to reuse. There is one: handlers/header-utils.ts carries DEFAULT_DENY_HEADER_NAMES / DEFAULT_DENY_HEADER_PREFIXES for inbound-header forwarding. It isn't reusable wholesale here, and shouldn't be: it forwards authorization by design and denies x-request-id, which is the opposite of what an error context wants. But its x-copilotcloud- prefix rule is worth mirroring in your set, so a future platform header can't leak into onError just because the exact name isn't listed. Fine as a later change.
  2. sse/connect.ts and handle-suggest.ts also build responses through createSseEventResponse without a reporter, so agent/connect and agent/suggest failures still bypass onError. That matches the scope you declared, just noting where the next slice would go.

I bless the code with a glad heart.

@BenTaylorDev
BenTaylorDev merged commit 4383a86 into CopilotKit:main Aug 12, 2026
39 checks passed
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.

2 participants