Skip to content

fix: send lowercase session keys so prompt caching survives - #46

Open
Mattheinrichs wants to merge 1 commit into
techartdev:mainfrom
Mattheinrichs:fix/lowercase-session-key
Open

Mattheinrichs wants to merge 1 commit into
techartdev:mainfrom
Mattheinrichs:fix/lowercase-session-key

Conversation

@Mattheinrichs

Copy link
Copy Markdown

Problem

Home Assistant hands out conversation_id values as uppercase ULIDs. _resolve_conversation_id() passes that value to scope_agent_session_id(), so the session key sent to OpenClaw looks like agent:voice:01M7KF8J84WTQTZWH6FCDSP4GN.

OpenClaw session keys are canonically lowercase. The gateway refuses the write:

[agents/session-maintenance] Optional session maintenance failed: refusing
non-canonical session key write agent:voice:01M7KF8J84WTQTZWH6FCDSP4GN;
stop the Gateway and run openclaw doctor --fix

and the envelope is rewritten on the following turn:

[agent/embedded] [prompt-cache] message 3 (toolResult) differs from the
previous request; history must be append-only; changed fields:
envelope.__openclaw sessionKey=agent:voice:01m7kf8j84wtqtzwh6fcdsp4gn
[prompt-cache] cache read dropped 23040 -> 6656 ... promptTokens=24090

Because the history is no longer append-only, the provider prefix cache is invalidated and the full agent prompt is re-prefilled every turn. On a ~24k-token voice agent that is ~9-14 s per turn instead of ~2 s.

Fix

Normalise the key in scope_agent_session_id(): lowercase the base id and the agent prefix, and keep the idempotency guard for already-scoped keys. One function, no call-site changes.

Tests

Two regression tests added: an uppercase HA ULID and an already-scoped uppercase key. python3 -m unittest discover -s tests -p 'test_*.py' -> 11 tests, OK.

Notes

  • The first turn after upgrading starts a fresh lowercase session, so short-term continuity resets once.
  • A user-configured assist_session_id still bypasses this helper and is sent verbatim; left unchanged here.

Home Assistant hands out conversation ids as uppercase ULIDs. OpenClaw
session keys are canonically lowercase, so an uppercase key is refused on
the session write and rewritten into the request envelope on the next turn.
That makes the history non-append-only, which invalidates the provider
prompt cache and forces a full prefill of the agent prompt every turn.

Normalise the base id and the agent prefix in scope_agent_session_id, and
cover the uppercase base id and already-scoped uppercase key with tests.
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.

1 participant