Skip to content

Gateway: server-side call_id → thoughtSignature cache for /v1/responses - #194

Merged
FMXExpress merged 2 commits into
mainfrom
claude/responses-thought-signature-cache
Jun 9, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/responses-thought-signature-cache

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Your 400 from Gemini:

"code": 400,
"message": "Function call is missing a thought_signature in functionCall parts.
            ... function call default_api:mcp__Kai__getProjectInfo, position 4."

Gemini 3+ tags each functionCall part it returns with a thoughtSignature and requires that exact value to be echoed back on subsequent turns. PR #154 wired this up end-to-end inside PasClaw via TToolCall.ProviderSignature, and the /v1/responses endpoint already emits the value on output items as a provider_signature extension 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 → signature map. Two helpers:

Function Called from Effect
RememberProviderSignature(CallId, Signature) FunctionCallItemJSON, whenever we emit a function_call output item with a non-empty signature Record the mapping
LookupProviderSignature(CallId) HandleResponses's function_call input parser, when provider_signature is empty Restore from server memory

Two-source resolution in the input parser:

  1. The provider_signature field on the input item (PasClaw-aware client — Codex with the PR Gemini 3: round-trip thoughtSignature on functionCall parts #154 extension echoed it back).
  2. Server-side cache by 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 TStringList is intentionally unsorted so Delete(0) removes the oldest insertion. A Sorted := True; Duplicates := dupIgnore list would order by call_id alphabetically and Delete(0) would drop whichever conversation happened to draw the smallest call_id, breaking active turns unpredictably. IndexOfName is linear on unsorted lists but the cache is small and we only touch it on tool-call boundaries — sub-millisecond and rare.

Lifecycle

  • Per-process, in-memory only (TCriticalSection-protected). Restart drops the cache, consistent with the rest of /v1/responses having no durable session.
  • The crit section guards against concurrent HandleResponses invocations on the Indy thread pool.

Test plan

  • make rebuilds cleanly
  • make test — all 15 suites green
  • Manual: drive Codex CLI through a multi-turn conversation that hits an MCP tool (the case in your bug report — mcp__Kai__getProjectInfo) on a Gemini 3 provider. Confirm:
    • first turn: tool call lands, response continues
    • second turn: no 400, the tool call from turn 1 round-trips with its signature intact via the server cache
  • Manual: long conversation that overflows 1024 cached call_ids. Confirm older signatures get evicted (no memory leak) but recent turns keep working.

Doesn't touch the existing PR #154 extension-field path — that still wins when present, server-cache is the fallback.


Generated by Claude Code

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@FMXExpress
FMXExpress force-pushed the claude/responses-thought-signature-cache branch from eeed832 to e70fb9b Compare June 9, 2026 02:53
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.
@FMXExpress
FMXExpress merged commit bd3a3b4 into main Jun 9, 2026
FMXExpress added a commit that referenced this pull request Jun 9, 2026
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
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