Skip to content

Commit f76b38a

Browse files
author
Zoo (VP)
committed
fix(opencode-go): always send x-opencode-session via per-instance session id
The Go gateway requires the x-opencode-session header on every request for operator-side session affinity (requests missing it may error). It was only sent when metadata.taskId existed, and completePrompt never sent it at all. Add a uuidv7 per-instance session id on the handler (mirroring the OpenAiNativeHandler fallback pattern) and send x-opencode-session: metadata.taskId ?? sessionId at every request site: chat-completions streaming, Anthropic /v1/messages streaming, Responses streaming, and all three completePrompt paths. Fixes #1511
1 parent bac8adc commit f76b38a

2 files changed

Lines changed: 173 additions & 77 deletions

File tree

‎src/api/providers/__tests__/opencode-go.spec.ts‎

Lines changed: 135 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,16 @@ describe("OpencodeGoHandler", () => {
198198
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
199199
await collectStream(handler.createMessage("sys", messages))
200200

201-
expect(mockCreate).toHaveBeenCalledWith(
202-
expect.objectContaining({
203-
model: "glm-5.1",
204-
stream: true,
205-
stream_options: { include_usage: true },
206-
// glm-5.1 maxTokens (131_072) is clamped to 20% of its 204_800
207-
// context window => 40_960.
208-
max_completion_tokens: 40_960,
209-
temperature: expect.any(Number),
210-
}),
211-
)
201+
const callArgs = mockCreate.mock.calls[0][0] as Record<string, unknown>
202+
expect(callArgs).toMatchObject({
203+
model: "glm-5.1",
204+
stream: true,
205+
stream_options: { include_usage: true },
206+
// glm-5.1 maxTokens (131_072) is clamped to 20% of its 204_800
207+
// context window => 40_960.
208+
max_completion_tokens: 40_960,
209+
temperature: expect.any(Number),
210+
})
212211
})
213212

214213
it("sends the stable conversation ID to chat completions", async () => {
@@ -222,18 +221,31 @@ describe("OpencodeGoHandler", () => {
222221
})
223222
})
224223

224+
it("falls back to a stable per-instance session ID when no taskId is provided", async () => {
225+
const handler = new OpencodeGoHandler(mockOptions)
226+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
227+
228+
await collectStream(handler.createMessage("sys", messages))
229+
230+
const sessionId = mockCreate.mock.calls[0][1]?.headers["x-opencode-session"]
231+
expect(sessionId).toEqual(expect.any(String))
232+
233+
// The same handler instance reuses its session ID across requests.
234+
await collectStream(handler.createMessage("sys", messages))
235+
expect(mockCreate.mock.calls[1][1]?.headers["x-opencode-session"]).toBe(sessionId)
236+
})
237+
225238
it("forwards the model's default reasoning_effort for reasoning-capable models", async () => {
226239
const handler = new OpencodeGoHandler(mockOptions)
227240
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
228241
await collectStream(handler.createMessage("sys", messages))
229242

230243
// glm-5.1 advertises supportsReasoningEffort with a default of "medium".
231-
expect(mockCreate).toHaveBeenCalledWith(
232-
expect.objectContaining({
233-
model: "glm-5.1",
234-
reasoning_effort: "medium",
235-
}),
236-
)
244+
const callArgs = mockCreate.mock.calls[0][0] as Record<string, unknown>
245+
expect(callArgs).toMatchObject({
246+
model: "glm-5.1",
247+
reasoning_effort: "medium",
248+
})
237249
})
238250

239251
it("omits reasoning_effort when the user disables reasoning", async () => {
@@ -383,7 +395,8 @@ describe("OpencodeGoHandler", () => {
383395

384396
await collectStream(handler.createMessage("sys", messages))
385397

386-
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ max_completion_tokens: 999 }))
398+
const callArgs = mockCreate.mock.calls[0][0] as Record<string, unknown>
399+
expect(callArgs.max_completion_tokens).toBe(999)
387400
})
388401
})
389402

@@ -392,15 +405,14 @@ describe("OpencodeGoHandler", () => {
392405
mockCreate.mockResolvedValue({ choices: [{ message: { content: "the answer" } }] })
393406
const handler = new OpencodeGoHandler(mockOptions)
394407
expect(await handler.completePrompt("ping")).toBe("the answer")
395-
expect(mockCreate).toHaveBeenCalledWith(
396-
expect.objectContaining({
397-
model: "glm-5.1",
398-
stream: false,
399-
// glm-5.1 maxTokens (131_072) clamped to 20% of 204_800 => 40_960.
400-
max_completion_tokens: 40_960,
401-
reasoning_effort: "medium",
402-
}),
403-
)
408+
const callArgs = mockCreate.mock.calls[0][0] as Record<string, unknown>
409+
expect(callArgs).toMatchObject({
410+
model: "glm-5.1",
411+
stream: false,
412+
// glm-5.1 maxTokens (131_072) clamped to 20% of 204_800 => 40_960.
413+
max_completion_tokens: 40_960,
414+
reasoning_effort: "medium",
415+
})
404416
})
405417

406418
it("wraps errors with an Opencode Go-specific message", async () => {
@@ -425,7 +437,22 @@ describe("OpencodeGoHandler", () => {
425437
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
426438
const handler = new OpencodeGoHandler({ ...mockOptions, includeMaxTokens: true, modelMaxTokens: 4321 })
427439
await handler.completePrompt("ping")
428-
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ max_completion_tokens: 4321 }))
440+
const callArgs = mockCreate.mock.calls[0][0] as Record<string, unknown>
441+
expect(callArgs.max_completion_tokens).toBe(4321)
442+
})
443+
444+
it("sends the per-instance session ID on non-streaming completions", async () => {
445+
mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] })
446+
const handler = new OpencodeGoHandler(mockOptions)
447+
448+
await handler.completePrompt("ping")
449+
const sessionId = mockCreate.mock.calls[0][1]?.headers["x-opencode-session"]
450+
expect(sessionId).toEqual(expect.any(String))
451+
452+
// completePrompt has no taskId metadata, so the per-instance ID is
453+
// reused across calls on the same handler.
454+
await handler.completePrompt("ping")
455+
expect(mockCreate.mock.calls[1][1]?.headers["x-opencode-session"]).toBe(sessionId)
429456
})
430457
})
431458

@@ -481,13 +508,12 @@ describe("OpencodeGoHandler", () => {
481508

482509
await collectStream(handler.createMessage("sys", messages))
483510

484-
expect(mockAnthropicCreate).toHaveBeenCalledWith(
485-
expect.objectContaining({
486-
model: "qwen3.7-max",
487-
stream: true,
488-
system: expect.arrayContaining([expect.objectContaining({ type: "text", text: "sys" })]),
489-
}),
490-
)
511+
const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record<string, unknown>
512+
expect(callArgs).toMatchObject({
513+
model: "qwen3.7-max",
514+
stream: true,
515+
system: expect.arrayContaining([expect.objectContaining({ type: "text", text: "sys" })]),
516+
})
491517
// The OpenAI chat completions endpoint must NOT be used for this model.
492518
expect(mockCreate).not.toHaveBeenCalled()
493519
})
@@ -503,6 +529,20 @@ describe("OpencodeGoHandler", () => {
503529
})
504530
})
505531

532+
it("falls back to a stable per-instance session ID when no taskId is provided", async () => {
533+
const handler = new OpencodeGoHandler(anthropicOptions)
534+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
535+
536+
await collectStream(handler.createMessage("sys", messages))
537+
538+
const sessionId = mockAnthropicCreate.mock.calls[0][1]?.headers["x-opencode-session"]
539+
expect(sessionId).toEqual(expect.any(String))
540+
541+
// The same handler instance reuses its session ID across requests.
542+
await collectStream(handler.createMessage("sys", messages))
543+
expect(mockAnthropicCreate.mock.calls[1][1]?.headers["x-opencode-session"]).toBe(sessionId)
544+
})
545+
506546
it("streams text, tool-call, usage and cost chunks from the Anthropic stream", async () => {
507547
const handler = new OpencodeGoHandler(anthropicOptions)
508548
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
@@ -569,17 +609,16 @@ describe("OpencodeGoHandler", () => {
569609

570610
const handler = new OpencodeGoHandler(anthropicOptions)
571611
expect(await handler.completePrompt("ping")).toBe("the answer")
572-
expect(mockAnthropicCreate).toHaveBeenCalledWith(
573-
expect.objectContaining({
574-
model: "qwen3.7-max",
575-
stream: false,
576-
messages: [{ role: "user", content: "ping" }],
577-
// qwen3.7-max maxTokens (65_536) clamped to 20% of its 1M
578-
// context window (200_000) => 65_536. includeMaxTokens is off,
579-
// so the model default is used.
580-
max_tokens: 65_536,
581-
}),
582-
)
612+
const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record<string, unknown>
613+
expect(callArgs).toMatchObject({
614+
model: "qwen3.7-max",
615+
stream: false,
616+
messages: [{ role: "user", content: "ping" }],
617+
// qwen3.7-max maxTokens (65_536) clamped to 20% of its 1M
618+
// context window (200_000) => 65_536. includeMaxTokens is off,
619+
// so the model default is used.
620+
max_tokens: 65_536,
621+
})
583622
expect(mockCreate).not.toHaveBeenCalled()
584623
})
585624

@@ -594,7 +633,21 @@ describe("OpencodeGoHandler", () => {
594633
modelMaxTokens: 2048,
595634
})
596635
await handler.completePrompt("ping")
597-
expect(mockAnthropicCreate).toHaveBeenCalledWith(expect.objectContaining({ max_tokens: 2048 }))
636+
const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record<string, unknown>
637+
expect(callArgs.max_tokens).toBe(2048)
638+
})
639+
640+
it("completePrompt sends the per-instance session ID to the Anthropic endpoint", async () => {
641+
mockAnthropicCreate.mockResolvedValue({
642+
content: [{ type: "text", text: "ok" }],
643+
})
644+
645+
const handler = new OpencodeGoHandler(anthropicOptions)
646+
await handler.completePrompt("ping")
647+
648+
expect(mockAnthropicCreate.mock.calls[0][1]?.headers).toMatchObject({
649+
"x-opencode-session": expect.any(String),
650+
})
598651
})
599652

600653
it("completePrompt rethrows non-Error values unchanged from the Anthropic path", async () => {
@@ -754,7 +807,8 @@ describe("OpencodeGoHandler", () => {
754807

755808
await collectStream(handler.createMessage("sys", messages))
756809

757-
expect(mockAnthropicCreate).toHaveBeenCalledWith(expect.objectContaining({ max_tokens: 8192 }))
810+
const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record<string, unknown>
811+
expect(callArgs.max_tokens).toBe(8192)
758812
})
759813

760814
it("falls back to the model max_tokens when includeMaxTokens is on but modelMaxTokens is unset", async () => {
@@ -764,7 +818,8 @@ describe("OpencodeGoHandler", () => {
764818
await collectStream(handler.createMessage("sys", messages))
765819

766820
// qwen3.7-max maxTokens (65_536) clamped to 20% of 1M context => 65_536.
767-
expect(mockAnthropicCreate).toHaveBeenCalledWith(expect.objectContaining({ max_tokens: 65_536 }))
821+
const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record<string, unknown>
822+
expect(callArgs.max_tokens).toBe(65_536)
768823
})
769824

770825
it("accumulates output tokens across message_delta events into the final cost", async () => {
@@ -876,6 +931,22 @@ describe("OpencodeGoHandler", () => {
876931
})
877932
})
878933

934+
it("falls back to a stable per-instance session ID when no taskId is provided", async () => {
935+
const handler = new OpencodeGoHandler(lunaOptions)
936+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
937+
938+
await collectStream(handler.createMessage("sys", messages))
939+
940+
const requestOptions = mockResponsesCreate.mock.calls[0][1] as { headers: Record<string, string> }
941+
const sessionId = requestOptions.headers["x-opencode-session"]
942+
expect(sessionId).toEqual(expect.any(String))
943+
944+
// The same handler instance reuses its session ID across requests.
945+
await collectStream(handler.createMessage("sys", messages))
946+
const secondOptions = mockResponsesCreate.mock.calls[1][1] as { headers: Record<string, string> }
947+
expect(secondOptions.headers["x-opencode-session"]).toBe(sessionId)
948+
})
949+
879950
it("closes the Responses iterator when the consumer stops early", async () => {
880951
const iterator = {
881952
next: vitest.fn().mockResolvedValueOnce({
@@ -1343,10 +1414,24 @@ describe("OpencodeGoHandler", () => {
13431414

13441415
await handler.completePrompt("ping", { abortSignal: controller.signal })
13451416

1346-
expect(mockResponsesCreate.mock.calls[0][1]).toEqual({ signal: controller.signal })
1417+
expect(mockResponsesCreate.mock.calls[0][1]).toEqual({
1418+
signal: controller.signal,
1419+
headers: { "x-opencode-session": expect.any(String) },
1420+
})
13471421
expect(mockCreate).not.toHaveBeenCalled()
13481422
})
13491423

1424+
it("completePrompt sends the per-instance session ID to the Responses endpoint", async () => {
1425+
mockResponsesCreate.mockResolvedValue({ output_text: "Hello!" })
1426+
const handler = new OpencodeGoHandler(lunaOptions)
1427+
1428+
await handler.completePrompt("ping")
1429+
1430+
expect(mockResponsesCreate.mock.calls[0][1]?.headers).toMatchObject({
1431+
"x-opencode-session": expect.any(String),
1432+
})
1433+
})
1434+
13501435
it("completePrompt wraps errors with an Opencode Go-specific message", async () => {
13511436
mockResponsesCreate.mockRejectedValue(new Error("boom"))
13521437
const handler = new OpencodeGoHandler(lunaOptions)

0 commit comments

Comments
 (0)