fix(agent): preserve runtime request context

OpenCode tools run in a child service, so they cannot read the Agent process-local session map. Hydrate a sanitized context through the authenticated internal bridge and centralize backend project headers to prevent context loss across both boundaries.
This commit is contained in:
2026-08-05 18:39:09 +08:00
parent a53839e157
commit 2dc37e3fd8
7 changed files with 291 additions and 19 deletions
+29
View File
@@ -0,0 +1,29 @@
import { describe, expect, it } from "bun:test";
import { buildBackendContextHeaders } from "../../src/auth/backendContextHeaders.js";
describe("buildBackendContextHeaders", () => {
it("forwards authenticated project and trace context to the backend", () => {
expect(
buildBackendContextHeaders({
accessToken: "access-token-1",
projectId: "project-id-1",
traceId: "trace-id-1",
}),
).toEqual({
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer access-token-1",
"X-Project-Id": "project-id-1",
"X-Trace-Id": "trace-id-1",
});
});
it("omits optional authentication and project headers when unavailable", () => {
expect(buildBackendContextHeaders({ traceId: "trace-id-2" })).toEqual({
Accept: "application/json",
"Content-Type": "application/json",
"X-Trace-Id": "trace-id-2",
});
});
});