Repository navigation
Gateway: server-side call_id → thoughtSignature cache for /v1/responses - #194
Conversation
PR #154 added a `provider_signature` extension field on /v1/responses function_call output items so PasClaw could round-trip Gemini 3+'s thoughtSignature across turns. That works for PasClaw-aware clients (Codex configured with the extension). Stock OpenAI-Responses clients (Codex CLI, etc.) JSON-validate input items and drop unknown fields, so the signature is gone by the next turn and Gemini 3 rejects with: 400 Function call is missing a thought_signature in functionCall parts. ... function call `default_api:mcp__Kai__getProjectInfo`, position 4. Belt-and-suspenders: also keep a process-wide call_id -> signature map. Two new helpers near the top of the impl section: RememberProviderSignature(CallId, Signature) Called from FunctionCallItemJSON whenever we emit a function_call output item with a non-empty signature. LookupProviderSignature(CallId) -> Signature Called from the function_call input parser in HandleResponses when provider_signature is missing on the input item but we recognise the call_id. Either path now yields a non-empty signature whenever Gemini's original tool call carried one, so the "missing thought_signature" 400 stops triggering even when the client strips our extension. Bounded to 1024 entries (PROVIDER_SIGNATURE_CACHE_MAX). Eviction is real FIFO -- TStringList is intentionally unsorted so Delete(0) removes the oldest insertion, not the lowest-call_id-alphabetically. A sorted+dupIgnore list would drop whichever conversation happened to draw the smallest call_id and break active turns unpredictably. IndexOfName is linear on unsorted lists but PROVIDER_SIGNATURE_CACHE_MAX is small and we only touch the cache on tool-call boundaries; sub-millisecond and rare. Per-process, in-memory only -- restart drops the cache, consistent with the rest of /v1/responses (no durable session). Crit-section protected; HandleResponses can be reentered by concurrent requests on the gateway's Indy thread pool.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eeed8320cb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (CallId = '') or (Signature = '') then Exit; | ||
| GProviderSignatureCacheLock.Enter; | ||
| try | ||
| if GProviderSignatureCache.IndexOfName(CallId) >= 0 then Exit; |
There was a problem hiding this comment.
Update cached signatures when call IDs repeat
When using Gemini, calls without provider IDs are synthesized as gemini_call_<name>_<index> in PasClaw.Providers.Gemini.pas, so two conversations that call the same tool first will share the same call_id. This early exit keeps the first signature forever and prevents the newer response's signature from replacing it; if a stock Responses client strips provider_signature, the next turn for the later conversation restores the stale thought signature and Gemini will reject the replayed functionCall. The cache needs to overwrite/refresh duplicate IDs or key them with response/conversation context rather than globally ignoring repeats.
Useful? React with 👍 / 👎.
eeed832 to
e70fb9b
Compare
Gemini synthesises ids like `gemini_call_<name>_<index>` when the
original assistant turn didn't carry one (PasClaw.Providers.Gemini's
id-fallback path). So two independent conversations that both call
`mcp__Kai__getProjectInfo` first land on the SAME call_id --
`gemini_call_mcp__Kai__getProjectInfo_0`.
Original RememberProviderSignature early-exited when the call_id
was already cached:
if GProviderSignatureCache.IndexOfName(CallId) >= 0 then Exit;
That meant the first conversation's signature stuck forever. The
second conversation's signature came in, RememberProviderSignature
saw the same key, bailed -- and on the next turn for the second
conversation, LookupProviderSignature returned the FIRST
conversation's stale signature. Gemini rejects the replay.
Fix: on collision, Delete the stale entry first, then Add. Two
benefits over a Values[] in-place update:
- The cached value gets the FRESH signature -- the next replay
for that call_id round-trips correctly.
- The FIFO eviction position resets to the tail, so a tool
call that just got reused isn't the next to be evicted when
the cap is reached.
… discovery, /stats, learn, etc.)
Summary
Your 400 from Gemini:
Gemini 3+ tags each
functionCallpart it returns with athoughtSignatureand requires that exact value to be echoed back on subsequent turns. PR #154 wired this up end-to-end inside PasClaw viaTToolCall.ProviderSignature, and the/v1/responsesendpoint already emits the value on output items as aprovider_signatureextension field — but stock OpenAI-Responses clients (Codex CLI, anything that JSON-validates input items) drop unknown fields, so the signature is gone by the next turn.Fix — server-side cache
Belt-and-suspenders: keep a process-wide
call_id → signaturemap. Two helpers:RememberProviderSignature(CallId, Signature)FunctionCallItemJSON, whenever we emit afunction_calloutput item with a non-empty signatureLookupProviderSignature(CallId)HandleResponses'sfunction_callinput parser, whenprovider_signatureis emptyTwo-source resolution in the input parser:
provider_signaturefield on the input item (PasClaw-aware client — Codex with the PR Gemini 3: round-trip thoughtSignature on functionCall parts #154 extension echoed it back).call_id(stock client — Codex CLI etc. strip unknown fields, so we restore from memory).Either path yields a non-empty signature when Gemini's original tool call carried one, so the "missing thought_signature" 400 stops triggering regardless of what the client preserves.
Eviction (the tricky part)
Bounded to
PROVIDER_SIGNATURE_CACHE_MAX = 1024— a long-lived conversation has maybe dozens of tool calls, so ~50 active conversations comfortable.Eviction is real FIFO: the
TStringListis intentionally unsorted soDelete(0)removes the oldest insertion. ASorted := True; Duplicates := dupIgnorelist would order bycall_idalphabetically andDelete(0)would drop whichever conversation happened to draw the smallestcall_id, breaking active turns unpredictably.IndexOfNameis linear on unsorted lists but the cache is small and we only touch it on tool-call boundaries — sub-millisecond and rare.Lifecycle
TCriticalSection-protected). Restart drops the cache, consistent with the rest of/v1/responseshaving no durable session.HandleResponsesinvocations on the Indy thread pool.Test plan
makerebuilds cleanlymake test— all 15 suites greenmcp__Kai__getProjectInfo) on a Gemini 3 provider. Confirm:Doesn't touch the existing PR #154 extension-field path — that still wins when present, server-cache is the fallback.
Generated by Claude Code