Skip to content

Read history on a thread that has never run as empty - #71

Open
stephschofield wants to merge 1 commit into
CopilotKit:mainfrom
stephschofield:thread-history-fix
Open

Read history on a thread that has never run as empty#71
stephschofield wants to merge 1 commit into
CopilotKit:mainfrom
stephschofield:thread-history-fix

Conversation

@stephschofield

@stephschofield stephschofield commented Aug 21, 2026

Copy link
Copy Markdown

Fixes #72

What

Reading message history on a thread that has never been run returns 500 {"error":"Failed to fetch thread messages"}.

A thread id is minted before the thread exists — the platform creates it on the first run — so this is the normal opening move for every new conversation, not an error. The runtime reports the platform's THREAD_NOT_FOUND as a bare 500, which reads as a broken server and buries a stack trace in the log each time somebody opens a new chat.

Reproduced against a live Intelligence deployment:

thread state before
d3ff669d… has a prior run 200, history returned
55569917… just minted, never run 500 ← platform THREAD_NOT_FOUND

How

After a GET on /threads/:threadId/messages returns 500, ask the platform whether it lists that thread. Not listed → 200 {"messages": []}.

Only a thread the platform does not list is treated as empty. A 500 for a thread that does exist is a real failure — an outage, a bad key — and stays a 500, because answering it with empty history would tell the browser the conversation is gone and invite it to start over. Same for a failed listing, or a request with no agentId to list against.

The middleware wraps the handler rather than being added with handler.use: Hono matches middleware only against routes declared after it, and the handler arrives with all of its own already registered.

Test plan

server/src/copilot.test.ts covers the decision over a stub runtime:

  • a minted thread the platform does not list reads as empty
  • a 500 on a thread that exists stays a 500
  • a 500 stays a 500 when the listing itself fails
  • a 500 stays a 500 with no agentId to list against

Verified against a live deployment: minted thread → {"messages":[]}, existing thread → 200 with history intact. typecheck and biome check clean.

A thread id is minted before the thread exists — the platform creates it on
the first run — so reading history on a conversation nobody has spoken in yet
is the normal opening move. The runtime reports the platform's
THREAD_NOT_FOUND as a bare 500, which reads as a broken server and buries a
stack trace in the log every time somebody opens a new chat.

Only a thread the platform does not list is treated as empty. A 500 for a
thread that does exist is a real failure and stays a 500: answering it with
empty history would tell the browser the conversation is gone and invite it to
start over.

Wrapped rather than added with handler.use, because Hono matches middleware
only against routes declared after it and the handler arrives with its own
already registered.
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.

Reading history on a thread that has never run returns 500

1 participant