From 60ee09fba3be21cf595298c632dc155ab411b2e6 Mon Sep 17 00:00:00 2001 From: liuedcson <332840681+liuedcson@users.noreply.github.com> Date: Wed, 2 Dec 2026 15:00:32 +0800 Subject: [PATCH] fix(coding-agent): restore the first-party x-session-id attribution Requests to https://api.stepfun.com and https://api.stepfun.ai stopped carrying the session id as x-session-id, and the session id fell back to the allowlisted `x-step-session-id` name on every host. That is a regression from #370 (01adb40e): the config-schema branch replaced `trace-headers.ts`, `features/step.ts`, and the two tests with byte-identical copies of their pre-#368 state, while the branch was cut from a commit that already contained #368. Deleting the implementation and its tests in the same commit is why no check failed. Restore #368 as it was merged, on top of the client-identity work that landed later: `STEP_FIRST_PARTY_TRACE_HOSTS`, `isStepFirstPartyTraceUrl`, the unconditional first-party `x-session-id`, and the single vendor-neutral name on the allowlisted path. The tests cover the first-party hosts, the non-matching hosts, an absent session id, and the allowlisted overlap. The provider-header comment and the queue test's placeholder header name now use the same `x-session-id` name the product actually sends. --- packages/coding-agent/src/features/step.ts | 11 +-- .../coding-agent/src/step/trace-headers.ts | 43 +++++++++-- .../coding-agent/test/step-extension.test.ts | 7 +- .../test/step-model-queue.test.ts | 4 +- .../test/step-trace-headers.test.ts | 73 ++++++++++++++++++- 5 files changed, 120 insertions(+), 18 deletions(-) diff --git a/packages/coding-agent/src/features/step.ts b/packages/coding-agent/src/features/step.ts index 4ca72f6d..3dc7fc97 100644 --- a/packages/coding-agent/src/features/step.ts +++ b/packages/coding-agent/src/features/step.ts @@ -442,11 +442,12 @@ export function createStepExtension(options: StepExtensionOptions = {}): Extensi pi.on("before_provider_headers", (event, ctx) => { const model = ctx.model; if (!model) return; - // x-step-client marks every request. The session/workspace/provider - // attribution follows only Step-owned providers (native step + product - // proxies), keyed by provider id, so it never egresses to a distinct - // third-party provider. Caveat: overriding STEP_BASE_URL under the - // "step" id points that provider at another host and attribution follows. + // x-step-client marks every request. The session id additionally rides + // every request to a Step first-party host (api.stepfun.com / .ai) as + // x-session-id, keyed by the request URL host regardless of provider id. + // The remaining workspace/goal/provider attribution is policy-gated + // (allowlist + highSensitivityFields), so it never egresses to a + // third-party endpoint. applyStepTraceHeaders( event.headers, { diff --git a/packages/coding-agent/src/step/trace-headers.ts b/packages/coding-agent/src/step/trace-headers.ts index f82d053d..189d21e9 100644 --- a/packages/coding-agent/src/step/trace-headers.ts +++ b/packages/coding-agent/src/step/trace-headers.ts @@ -49,6 +49,25 @@ const TRACE_HEADER_ENV_NAMES = [ */ const STEP_TRACE_PRODUCT_PROVIDERS = new Set(["stepfunModelProxy", "neocodex"]); +/** + * Step's first-party hosts (domestic `.com` + oversea `.ai`). Requests to + * `https://api.stepfun.com/...` or `https://api.stepfun.ai/...` always carry + * the session id as `x-session-id`, regardless of the host trace policy — the + * session id is first-party attribution there, not third-party egress. + */ +export const STEP_FIRST_PARTY_TRACE_HOSTS: readonly string[] = ["api.stepfun.com", "api.stepfun.ai"]; + +/** Whether a request URL targets a Step first-party host over https. */ +export function isStepFirstPartyTraceUrl(requestUrl: string | undefined): boolean { + if (!requestUrl) return false; + try { + const url = new URL(requestUrl); + return url.protocol === "https:" && STEP_FIRST_PARTY_TRACE_HOSTS.includes(url.hostname); + } catch { + return false; + } +} + /** * Add Step attribution headers in place. * @@ -58,13 +77,17 @@ const STEP_TRACE_PRODUCT_PROVIDERS = new Set(["stepfunModelProxy", "neocodex"]); * replaces a half). The server reads the pair to decide which client releases * may use Step-specific capacity; the same identity is appended to the provider * `User-Agent` as `name/version`. - * The high-sensitivity fields - * (session/workspace/goal/...) are sent only when the request URL matches + * + * Requests to a Step first-party host (`https://api.stepfun.com/...` or + * `https://api.stepfun.ai/...`) always carry the session id as `x-session-id` + * too — first-party attribution there, not third-party egress. Every other + * high-sensitivity field (workspace/goal/...) — and the session id on any + * non-first-party host — is sent only when the request URL matches * `options.allowedBaseUrls` AND the field appears in * `options.highSensitivityFields`; an omitted or empty field list sends none of - * them (fail closed), so the local cwd and session id never egress to a - * third-party model endpoint. Values are size-bounded and header-safe via - * {@link encodeStepTraceHeaderValue}. + * them (fail closed), so the local cwd never egresses to a third-party model + * endpoint. The session id keeps that single name on every path. Values are + * size-bounded and header-safe via {@link encodeStepTraceHeaderValue}. */ export function applyStepTraceHeaders( headers: Record, @@ -75,6 +98,12 @@ export function applyStepTraceHeaders( setHeader(headers, "x-step-client", options.clientType ?? identity.name); setHeader(headers, "x-step-client-version", options.clientVersion ?? identity.version); + // Step's first-party host always receives the session id, independent of the + // host trace policy. Other fields (and the session id elsewhere) stay gated. + if (isStepFirstPartyTraceUrl(options.requestUrl)) { + setHeader(headers, "x-session-id", trace.sessionId); + } + const traceAllowed = options.requestUrl === undefined || options.allowedBaseUrls === undefined || @@ -89,7 +118,9 @@ export function applyStepTraceHeaders( const setIfAllowed = (field: string, value: string | undefined): void => { if (allowed.has(field)) setHeader(headers, `x-step-${field}`, value); }; - setIfAllowed("session-id", trace.sessionId); + // The session id keeps one name on every path; the other attributed fields + // stay under the x-step-* prefix. + if (allowed.has("session-id")) setHeader(headers, "x-session-id", trace.sessionId); setIfAllowed("goal-id", trace.goalId); setIfAllowed("attempt-id", trace.attemptId); setIfAllowed("harness-id", trace.harnessId); diff --git a/packages/coding-agent/test/step-extension.test.ts b/packages/coding-agent/test/step-extension.test.ts index a08117e8..85049e70 100644 --- a/packages/coding-agent/test/step-extension.test.ts +++ b/packages/coding-agent/test/step-extension.test.ts @@ -846,11 +846,12 @@ describe("Step extension", () => { sessionManager: { getSessionId: () => "session-1" }, } as unknown as Pick); // The native Step endpoint is a direct model API, not the cloud-trace - // collector. Keep session/workspace identifiers off custom or direct - // provider requests; only the low-sensitivity client markers are universal. + // collector: workspace identifiers stay off it, while the session id rides + // along as first-party attribution under its single vendor-neutral name. expect(headers).toEqual({ "x-step-client": "stepcode-cli", "x-step-client-version": STEPCODE_VERSION.value, + "x-session-id": "session-1", }); const originalOrigin = process.env.STEPCODE_CLOUD_TRACE_ORIGIN; @@ -869,7 +870,7 @@ describe("Step extension", () => { expect(otherHeaders).toMatchObject({ "x-step-client": "stepcode-cli", "x-step-client-version": STEPCODE_VERSION.value, - "x-step-session-id": "session-2", + "x-session-id": "session-2", "x-step-workspace-id": "/workspace", "x-step-provider-id": "stepfunModelProxy", "x-step-model": "step-proxy", diff --git a/packages/coding-agent/test/step-model-queue.test.ts b/packages/coding-agent/test/step-model-queue.test.ts index a81fa17f..b9ab476b 100644 --- a/packages/coding-agent/test/step-model-queue.test.ts +++ b/packages/coding-agent/test/step-model-queue.test.ts @@ -254,7 +254,7 @@ test("forwards the request's own headers to the control requests, except those d headers: { authorization: "Bearer sk-test", "x-api-key": "k", - "x-step-session-id": "s1", + "x-session-id": "s1", "content-type": "application/json", "content-length": String(BODY.length), }, @@ -262,7 +262,7 @@ test("forwards the request's own headers to the control requests, except those d const poll = h.calls[1]; assert.equal(poll?.headers.get("authorization"), "Bearer sk-test"); assert.equal(poll?.headers.get("x-api-key"), "k"); - assert.equal(poll?.headers.get("x-step-session-id"), "s1"); + assert.equal(poll?.headers.get("x-session-id"), "s1"); assert.equal(poll?.headers.get("accept"), "application/json"); assert.equal(poll?.headers.has("content-type"), false); assert.equal(poll?.headers.has("content-length"), false); diff --git a/packages/coding-agent/test/step-trace-headers.test.ts b/packages/coding-agent/test/step-trace-headers.test.ts index 16171ca1..8fbb950c 100644 --- a/packages/coding-agent/test/step-trace-headers.test.ts +++ b/packages/coding-agent/test/step-trace-headers.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest"; import { applyStepTraceHeaders, encodeStepTraceHeaderValue, + isStepFirstPartyTraceUrl, isStepTraceRequestAllowed, matchesStepTraceBaseUrl, resolveStepTraceAllowlist, @@ -66,7 +67,7 @@ describe("Step trace headers", () => { const headers: Record = { "X-Step-Client": "stale", "X-Step-Client-Version": "0.0.1", - "x-step-session-id": "stale-session", + "x-session-id": "stale-session", accept: "application/json", }; applyStepTraceHeaders(headers, trace, { @@ -76,14 +77,82 @@ describe("Step trace headers", () => { allowedBaseUrls: ["https://allowed.example/v1"], }); + // Not a first-party host and outside the allowlist: the caller's own + // `x-session-id` is left alone, and no attribution field is added. expect(headers).toEqual({ "x-step-client": "stepcode-desktop", "x-step-client-version": "1.4.0", - "x-step-session-id": "stale-session", + "x-session-id": "stale-session", accept: "application/json", }); }); + it("sends the session id as x-session-id on Step's first-party hosts", () => { + for (const requestUrl of [ + "https://api.stepfun.com/step_plan/v1/chat/completions", + "https://api.stepfun.com/v1/messages", + "https://api.stepfun.ai/v1/responses", + ]) { + const headers: Record = {}; + // No allowlist and no field policy: the first-party host still gets the + // session id, and nothing else beyond the client pair. + applyStepTraceHeaders(headers, trace, { requestUrl, allowedBaseUrls: [] }); + expect(headers).toEqual({ + "x-step-client": "stepcode-cli", + "x-step-client-version": STEPCODE_VERSION.value, + "x-session-id": "session-1", + }); + } + }); + + it("keeps the session id off every other host", () => { + const headers: Record = {}; + applyStepTraceHeaders(headers, trace, { + requestUrl: "https://api.stepfun.com.example/v1", + allowedBaseUrls: [], + }); + expect(headers).toEqual({ + "x-step-client": "stepcode-cli", + "x-step-client-version": STEPCODE_VERSION.value, + }); + }); + + it("omits x-session-id on a first-party host when the session id is absent", () => { + const headers: Record = {}; + applyStepTraceHeaders(headers, {}, { requestUrl: "https://api.stepfun.com/step_plan/v1", allowedBaseUrls: [] }); + expect(headers).toEqual({ + "x-step-client": "stepcode-cli", + "x-step-client-version": STEPCODE_VERSION.value, + }); + }); + + it("recognizes only https first-party hosts", () => { + expect(isStepFirstPartyTraceUrl("https://api.stepfun.com/step_plan/v1")).toBe(true); + expect(isStepFirstPartyTraceUrl("https://API.Stepfun.Com/v1")).toBe(true); + expect(isStepFirstPartyTraceUrl("https://api.stepfun.ai/v1")).toBe(true); + expect(isStepFirstPartyTraceUrl("http://api.stepfun.com/v1")).toBe(false); + expect(isStepFirstPartyTraceUrl("https://api.stepfun.com.evil/v1")).toBe(false); + expect(isStepFirstPartyTraceUrl("https://api.stepfun.com@evil.example/v1")).toBe(false); + expect(isStepFirstPartyTraceUrl("https://other.stepfun.com/v1")).toBe(false); + expect(isStepFirstPartyTraceUrl(undefined)).toBe(false); + }); + + it("does not duplicate the session id when the first-party host is also allowlisted", () => { + const headers: Record = {}; + applyStepTraceHeaders(headers, trace, { + requestUrl: "https://api.stepfun.com/step_plan/v1", + allowedBaseUrls: ["https://api.stepfun.com/step_plan/v1"], + highSensitivityFields: ["session-id", "workspace-id"], + }); + expect(headers["x-step-client"]).toBe("stepcode-cli"); + expect(headers["x-step-client-version"]).toBe(STEPCODE_VERSION.value); + // One name for the session id on every path: the first-party rule and the + // allowlist both naming it must not emit a second header. + expect(headers["x-session-id"]).toBe("session-1"); + expect(Object.keys(headers).filter((name) => name.includes("session"))).toEqual(["x-session-id"]); + expect(decode(headers["x-step-workspace-id"] as string)).toBe("/tmp/工作区"); + }); + it("treats an explicitly empty allowlist as untrusted", () => { const headers: Record = {}; applyStepTraceHeaders(headers, trace, {