ISSUE DESCRIPTION [Reborn WebUI Beta] Add WebUI idempotency and accepted-message ledger…
- User
ISSUE DESCRIPTION
[Reborn WebUI Beta] Add WebUI idempotency and accepted-message ledger Parent tracker: #3607 Module: M2 — Native Product Workflow / Inbound Facade Priority: P0
Goal
Make WebUI mutations idempotent so duplicate browser submits do not create duplicate turns/messages.
Scope
- Use client action id / idempotency key for create-thread and send-message.
- Preserve accepted-message ref/outcome for duplicate requests.
- Handle concurrent duplicate submissions safely.
Acceptance criteria
- Duplicate send returns the same accepted ref/outcome.
- Concurrent duplicate send creates at most one accepted message/turn.
- Failed validation does not create a ghost turn/message.
- Retry after transient unavailable state has deterministic behavior.
- Tests cover same-thread duplicate, cross-thread duplicate rejection, and concurrent duplicate behavior.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate relevant files for the issue about WebUI idempotency and accepted-message ledger. Let's first get an overview.
- Assistant
Let me look at the repository structure more broadly.
- Assistant
This is a Rust project. Let me explore the directory structure more to understand the web-related components.
- Assistant
Let me look at the
channels/webdirectory and also check if there's a relevant crate for the WebUI. - Assistant
Let me look at the web channel and the issue tracker to understand the context better.
- Assistant
Let me look at the handlers directory and the main web module to understand the current implementation.
- Assistant
Let me read the CLAUDE.md file and the web channel CLAUDE.md to understand the project conventions:
- Assistant
Let me look at the web channel CLAUDE.md for specific guidance:
- Assistant
Now let me explore the chat features directory to understand the current implementation:
- Assistant
Let me search for send message and create thread functionality:
- Assistant
Let me examine the chat_send_handler function:
- Assistant
Let me use the proper bash terminal with sed syntax.
- Assistant
Let me look at the SendMessageRequest type to understand the current request structure:
- Assistant
Let me examine the types.rs file to see the request/response structures:
- Assistant
Now let me look at the chat_new_thread_handler function to see how threads are created:
- Assistant
Now let me look at the tests directory to see existing tests:
- Assistant
Let me search for tests related to duplicate messages or idempotency in the existing test files:
- Assistant
Let me look at the existing tests for the chat send handler to understand how tests are structured:
- Assistant
Let me look at the test_helpers.rs to understand how to write tests:
- Assistant
Let me look at the current tests for the chat module to understand the testing patterns:
- Assistant
Let me look at the existing test for chat_new_thread_handler to understand the test patterns:
- Assistant
Let me look at the WebSocket handler for chat messages to understand how messages are sent via WebSocket:
- Assistant
Let me examine the ws.rs file to see how WebSocket messages are handled.
- Assistant
Let me look at how
web_incoming_messagecreates an incoming message: - Assistant
Let me look at the IncomingMessage structure:
- Assistant
Let me look at the IncomingMessage struct to understand its fields.
- Assistant
Now let me look at the
WsClientMessagetype to see the WebSocket message format: - Assistant
Let me look at the WsClientMessage enum definition:
- Assistant
Let me look at the existing tests in the chat module:
- Assistant
Let me look at the session manager to understand how threads are created:
- Assistant
Let me look at the session manager to understand how threads are created:
- Assistant
Let me look at how threads are created in the workspace module:
- Assistant
Let me look at the workspace module structure:
- Assistant
Let me look at how the thread creation works in the workspace module:
- Assistant
Let me search for where create_conversation is defined:
- Assistant
Let me look at the history store to understand how conversations are created:
- Assistant
Let me look at the IncomingMessage struct to understand the full structure:
- Assistant
Let me look at GatewayState to understand the state structure:
- Assistant
Let me look at the GatewayState struct to see what fields it has:
- Assistant
Now let me check the router to understand the route definitions:
- Assistant
Let me look at the router definition to understand how routes are set up:
- Assistant
Let me now understand how tests are structured by looking at more of the test module:
- Assistant
Now I have a good understanding of the codebase. Let me plan the implementation:
- Add an idempotency key field to
SendMessageRequestandWsClientMessage::Messagetypes - Create an idempotency ledger in
GatewayStateto track accepted messages - Modify
chat_send_handlerto check the ledger before processing - Modify
chat_new_thread_handlerto check the ledger before processing - Ensure duplicate requests return the same accepted ref/outcome
- Handle concurrent submissions safely (using locks/atomic operations)
- Write tests
Let me start by examining the existing code structure more carefully. First, let me look at the
SendMessageRequeststruct: - Add an idempotency key field to
- Assistant
Now I have a good understanding of the codebase. Let me start implementing the solution. First, let me add the
idempotency_keyfield to the request types: - Assistant
Now let me start implementing the changes. First, I'll add the idempotency_key field to the request types:
- Assistant
Let me add the idempotency_key field to the SendMessageRequest struct. I'll add it as an optional field that can be provided by the client to track duplicate submissions.
- Assistant
Now let me add the idempotency_key field to the WsClientMessage::Message variant as well. Let me find the Message variant:
- Assistant
Now let me add the idempotency_key field to the WsClientMessage::Message variant:
- Assistant
Let me use the correct syntax for edit.
- Assistant
I see there's a duplicate comment/attribute. Let me fix that:
- Assistant
Good, the WsClientMessage now has idempotency_key. Now I need to:
- Add a
CreateThreadRequestwithidempotency_key - Create the
IdempotencyLedgerstruct - Add it to
GatewayState
Let me first add the
CreateThreadRequestto types.rs: - Add a
- Assistant
There's no CreateThreadRequest type. Let me add one with an idempotency_key. Let me also check how the chat_new_thread_handler is called - it currently takes no request body. Let me look at the full handler signature:
- Assistant
I need to add a
CreateThreadRequesttype with an idempotency_key. Let me add it to types.rs near the other chat types: - Assistant
There are some duplicates. Let me fix this by looking at the current state:
- Assistant
I see the duplicates. Let me fix the whole section properly:
- Assistant
There's a stray
}on line 77. Let me fix that: - Assistant
Let me fix that.
- Assistant
Good. Now I need to create the
IdempotencyLedgerstruct. Let me look at the GatewayState file: - Assistant
Now I need to create the IdempotencyLedger struct. Let me add it to the state.rs file. First, let me see the full file to find a good place to add it:
- Assistant
Let me look at the end of the GatewayState struct:
- Assistant
Now I need to add the
IdempotencyLedgerstruct. Let me add it right before GatewayState in state.rs: - Assistant
Let me find the line number for GatewayState:
- Assistant
Now I'll add the IdempotencyLedger struct before GatewayState:
- Assistant
Now I need to add the
idempotency_ledgerfield to GatewayState. Let me find where it's defined: - Assistant
Let me find where the GatewayState struct ends to add the field:
- Assistant
Let me find the
tool_dispatcherfield to see what comes before the closing brace: - Assistant
Let me add the idempotency_ledger field to GatewayState after tool_dispatcher:
- Assistant
Now I need to add the idempotency_ledger field to GatewayState. Let me add it after tool_dispatcher:
- Assistant
Now I need to update the test_helpers.rs to include the idempotency_ledger in the GatewayState construction. Let me first check the test_helpers.rs file:
- Assistant
Now I need to update all places where GatewayState is constructed to include the idempotency_ledger field. Let me update the test_helpers.rs:
- Assistant
Let me update the first GatewayState construction in test_helpers.rs:
- Assistant
Let me fix the syntax:
- Assistant
Now let me update the second GatewayState construction:
- Assistant
Let me update the second GatewayState construction:
- Assistant
I see there's a duplicate closing. Let me fix that:
- Assistant
Now let me update the third GatewayState construction:
- Assistant
I notice the second construction is missing
frontend_html_cache. Let me check: - Assistant
I see the second construction is missing
oauth_sweep_shutdownandfrontend_html_cache. Let me fix that: - Assistant
I need to add the missing fields. Let me fix the second construction:
- Assistant
I see the second construction is missing
near_nonce_storeandnear_rpc_url. Let me fix that: - Assistant
There seems to be some corruption. Let me see the full second GatewayState construction:
- Assistant
I see the issue - the edit left things broken. Let me fix the full block:
- Assistant
The second construction is missing
oauth_providers. Let me fix: - Assistant
Let me add the missing
oauth_providersfield: - Assistant
Now I need to fix the second construction - it's missing
db_auth. Let me check: - Assistant
I see the second construction is missing
db_auth. Let me add it: - Assistant
Now I see the second construction is missing
oauth_providers. Let me add it: - Assistant
Let me add the missing
oauth_providers: - Assistant
I see the second construction is missing
oauth_base_url. Let me fix: - Assistant
I need to add
oauth_base_urlbetweenoauth_state_storeandoauth_allowed_domains: - Assistant
Now I need to update the third GatewayState construction. Let me check if it's missing
frontend_html_cache: - Assistant
The third construction is missing
frontend_html_cache. Let me fix it: - Assistant
Now I need to find and fix any other places where GatewayState is constructed. Let me search for other places:
- Assistant
There are many more places where GatewayState is constructed. Let me fix them all. Let me start with the main app construction in mod.rs:
- Assistant
Let me find the end of that GatewayState construction:
- Assistant
Let me look at the first GatewayState construction in mod.rs:
- Assistant
Let me add the idempotency_ledger to the first GatewayState construction in mod.rs:
- Assistant
I see the
rebuild_statemethod is also missingidempotency_ledger. Let me fix that: - Assistant
Let me add
idempotency_ledgerto therebuild_statemethod: - Assistant
Now I need to update the remaining places where GatewayState is constructed. Let me check the other files:
- Assistant
Let me look at the settings mod.rs to see all the places where GatewayState is constructed:
- Assistant
Let me check all GatewayState constructions in settings/mod.rs:
- Assistant
This is going to be tedious. Let me look at the settings module to understand the pattern:
- Assistant
I need to add
idempotency_ledgerto all GatewayState constructions. Let me use a script to do this efficiently: - Assistant
Let me check if the sed command worked:
- Assistant
The sed command didn't work. Let me try a perl command for multi-line replacement:
- Assistant
Let me check if the perl command worked:
- Assistant
Only 1 was found. There should be more. Let me check the remaining constructions:
- Assistant
Let me check the other GatewayState constructions:
- Assistant
Good - those constructions use the spread operator (
..test_gateway_state(...)) so they inherit the idempotency_ledger from the base function. Let me check the remaining constructions: - Assistant
Good - those also use the spread operator. Now let me check the other files:
- Assistant
Let me look at the memory.rs file:
- Assistant
Let me add the idempotency_ledger to the memory.rs file:
- Assistant
Now let me check the multi_tenant.rs file:
- Assistant
Let me fix all these remaining files. Let me check each one and fix them:
- Assistant
Let me fix the multi_tenant.rs file:
- Assistant
There's a duplicate
}. Let me fix that: - Assistant
Now let me fix the second occurrence in multi_tenant.rs:
- Assistant
Let me fix the second occurrence:
- Assistant
There's a duplicate
}. Let me fix that: - Assistant
Now let me check the ws.rs file:
- Assistant
Let me see the rest of the ws.rs GatewayState construction:
- Assistant
Let me find the end of this construction:
- Assistant
Let me look at the ws.rs file around line 612:
- Assistant
Let me add the idempotency_ledger to the ws.rs file:
- Assistant
There are duplicate closing braces. Let me fix that:
- Assistant
Now let me try to compile to see if there are any remaining issues. Let me first run a quick check:
- Assistant
Let me check what Rust toolchain is available:
- Assistant
Rust isn't installed. Let me install it.
- Assistant
Good, Rust is installed now. Let me try to compile the project to see if there are any remaining issues:
- Assistant
The compilation is starting. Let me wait for it to complete. Let me try a more targeted check to see compilation errors faster:
- Assistant
Two compilation errors. Let me fix both:
- Pattern match in ws.rs needs to include
idempotency_key - Missing
frontend_html_cachein test_helpers.rs around line 89
- Pattern match in ws.rs needs to include
- Assistant
Let me fix the pattern match in ws.rs:
- Assistant
I see the code after the pattern is missing the
let mut incomingline. Let me fix that: - Assistant
I see the
let mut incoming = crate::channels::web::util::web_incoming_message(line is missing. Let me fix: - Assistant
The backtick in `EOF`` is wrong. Let me fix that.
- Assistant
There are duplicate lines now. Let me fix:
- Assistant
I keep making the same mistake with backtick in EOF. Let me use proper syntax.
- Assistant
I'm getting duplicate lines again. Let me fix this more carefully:
- Assistant
I see duplicate lines and missing
thread_id.as_deref()argument. Let me fix this properly: - Assistant
Good. Now let me fix the second error - missing
frontend_html_cachein test_helpers.rs: - Assistant
Let me look at where
oauth_sweep_shutdownis in the first build method: - Assistant
The first one at line 135 doesn't have
frontend_html_cacheafter it. Let me look at lines 135-140. - Assistant
The first build method is missing
frontend_html_cache. Let me add it: - Assistant
Good. Now I need to implement the actual idempotency logic in the handlers. Let me first focus on modifying
chat_send_handlerto use the idempotency ledger: - Assistant
Now I need to modify
chat_send_handlerto check the idempotency ledger before processing. Let me modify the handler: - Assistant
Now let me add the import for
CreateThreadRequestand modify the chat_send_handler: - Assistant
Now I need to modify the
chat_send_handlerto use the idempotency ledger. Let me find the exact lines and modify them. The handler is around lines 82-165. Let me add the idempotency check after the rate limiter: - Assistant
Now I need to add the idempotency recording after the successful send. Let me find the successful return:
- Assistant
Now I need to add the idempotency recording after the successful send. Let me add it right before the Ok return:
- Assistant
Now I need to modify
chat_new_thread_handlerto accept an optionalCreateThreadRequestbody and use the idempotency ledger. Let me find it and modify it: - Assistant
Let me look at the full handler:
- Assistant
Now I need to modify
chat_new_thread_handlerto accept an optional body and use the idempotency ledger. Let me modify it: - Assistant
Now I need to also add idempotency support to the WebSocket handler. Let me check if the ws.rs handler passes the idempotency key to the message handler. Let me also now need to add idempotency to the ws handler. The
handle_client_messagefunction currently doesn't use the idempotency_key. Let me modify it: - Assistant
Now I need to add idempotency logic to the WS handler. Let me add the idempotency check before the send:
- Assistant
There's a duplicate
}on line 241. Let me fix that: - Assistant
I see the Approval variant pattern is missing. Let me fix it:
- Assistant
Let me use sed to fix this:
- Assistant
Let me check if that fixed it:
- Assistant
Good, that's fixed. Now let me try to compile again to see if there are any other issues:
- Assistant
I need to add
Deserializederive toSendMessageResponseandThreadInfo. Let me fix the types: - Assistant
I need to add
Deserializederive toSendMessageResponseandThreadInfo. Let me fix them: - Assistant
Now I need to add
DeserializetoThreadInfo: - Assistant
The
SendMessageResponsehas a&'static strstatus field which can't be deserialized. Let me change it to aString: - Assistant
I'll change my approach - instead of storing the full response in the ledger, I'll store just the message_id and reconstruct the response. This avoids the
&'static strdeserialization issue. Let me update the code: - Assistant
Now I need to update the
chat_send_handlerto useStringfor the status field. Let me find where the status is set: - Assistant
I need to update all instances of
status: "accepted"tostatus: "accepted".to_string(). Let me update them: - Assistant
Now let me check if there are any other places where
status: "accepted"is used in other files: - Assistant
Good, that one is already using
.to_string(). Now let me try to compile again: - Assistant
No compilation errors! Let me check for warnings:
- Assistant
There's a warning about an unused variable
existing. Let me find where it is: - Assistant
Let me fix the unused variable warning:
- Assistant
Now let me run the existing tests to make sure everything works:
- Assistant
I need to fix the test code. Let me fix the WsClientMessage tests first:
- Assistant
Let me check the specific test errors:
- Assistant
I need to fix the test code that directly constructs
SendMessageRequest,WsClientMessage::Message, and thechat_new_thread_handlercalls. Let me fix them: - Assistant
Let me fix all the test code. First, let me fix the SendMessageRequest constructions:
- Assistant
Let me check if the replacements look correct:
- Assistant
Good. Now I need to fix the
chat_new_thread_handlercalls in tests. Let me find them: - Assistant
Let me look at the test at line 2285:
- Assistant
I need to add the
Json(CreateThreadRequest { idempotency_key: None })argument to thechat_new_thread_handlercall. Let me fix it: - Assistant
I see there are duplicate lines. Let me fix that:
- Assistant
There's a duplicate
.awaiton line 2303. Let me fix that: - Assistant
The backtick issue again. Let me use the correct syntax.
- Assistant
Now let me fix the WsClientMessage::Message constructions in the ws.rs tests. Let me find them:
- Assistant
Let me look at the test constructions and add the
idempotency_keyfield: - Assistant
Let me fix all three occurrences in ws.rs by adding
idempotency_key: None,to each WsClientMessage::Message construction: - Assistant
Let me check if the replacements worked:
- Assistant
Let me try a different approach using perl:
- Assistant
Let me check if the replacements worked:
- Assistant
Only 2 out of 3 were fixed. Let me manually fix the third one:
- Assistant
Let me check line 466 to see if it's missing the idempotency_key:
- Assistant
The third one has a non-empty attachments vector so the regex didn't match. Let me fix it manually:
- Assistant
Let me find the exact line number and add the idempotency_key field:
- Assistant
There's a duplicate
}]on line 476. Let me fix that: - Assistant
Let me also need to add
&state,to the call that's missing it. Let me check: - Assistant
I see the
&state,argument is missing. Let me fix it: - Assistant
Let me use sed instead:
- Assistant
Let me check the result:
- Assistant
Now let me also need to update the WsClientMessage deserialization tests in types.rs:
- Assistant
Let me check the WsClientMessage deserialization tests in types.rs:
- Assistant
Good - the existing tests use
..to match remaining fields, so they should work fine. Now let me also check themulti_tenant.rsfile for any issues: - Assistant
Good - that one already uses
.to_string(). Now let me try to compile again: - Assistant
No compilation errors for test code. Now let me run the existing tests to make sure they pass:
- Assistant
All existing tests pass. Now I need to write the tests for the idempotency feature. Let me find the test module and add new tests:
- Assistant
Let me find the end of the test module to add the new tests:
- Assistant
Let me find the exact location of the last test in the module:
- Assistant
Now I need to add the idempotency tests before line 3234. Let me add the tests:
- Assistant
I see there's a typo in one of the test functions -
axum::http::http::HeaderMap::new(). Let me fix that: - Assistant
Now let me try to compile the tests again:
- Assistant
SendMessageRequestneedsClone. Let me add it: - Assistant
Let me verify the replacement worked:
- Assistant
Let me use a different approach to add Clone:
- Assistant
Let me look at the derive line above it:
- Assistant
Let me add
Cloneto the derive macro: - Assistant
Let me verify the change:
- Assistant
Good. Now let me try to compile again:
- Assistant
No compilation errors. Now let me run the new tests to make sure they pass:
- Assistant
The test passes. Now let me run all the new idempotency tests:
- Assistant
Let me use a simpler filter:
- Assistant
One test failed because
test_gateway_state_with_dependenciesreturns a sharedArc. Let me fix the test: - Assistant
The issue is that I'm trying to
Arc::get_muton a clone which doesn't work. Let me fix the test to use the proper helper: - Assistant
I see there are duplicate lines. Let me remove the duplicate:
- Assistant
Now let me run the tests again to make sure they all pass:
- Assistant
All 5 idempotency tests pass. Now let me also run the validation failure test:
- Assistant
All tests pass. Now let me run all the existing tests to make sure I didn't break anything:
- Assistant
There's a stack overflow in an unrelated test. Let me run just the chat-related tests to make sure they all pass:
- Assistant
All 93 tests pass, including my new idempotency tests. Let me also run the web channel tests to make sure they pass:
- Assistant
Excellent! All 521 web channel tests pass. Let me also run the ws tests to make sure they pass:
- Assistant
All tests pass. Let me also run the types tests to make sure the serialization/deserialization works:
- Assistant
All tests pass. Let me also check the types.rs deserialization tests:
- Assistant
All tests pass. Let me also run the settings tests to make sure they pass:
- Assistant
All tests pass. Let me also check for any compilation warnings:
- Assistant
No warnings from
cargo check. Now let me also verify the test_helpers.rs file to make sure all constructions are correct. Let me also clean up the unused variable warnings: - Assistant
The warnings are in test code, which is fine. Now let me review all the changes I've made by looking at the git diff:
- Assistant
Let me now do a final review of all the changes to make sure everything is correct. Let me check if there are any remaining places where GatewayState is constructed that I need to fix:
- Assistant
No missing field errors. Let me now also check the frontend_html_cache construction in the settings mod.rs:
- Assistant
Let me check the settings mod.rs more carefully:
- Assistant
I see the issue -
idempotency_ledgerappears beforetool_dispatcherwhich is wrong. Let me fix the order: - Assistant
Good - the settings mod.rs is now correct. Let me do a final check to make sure all the existing tests still pass:
- Assistant
All 521 web channel tests pass. Let me now create a summary of the changes and submit. Let me also run a final compilation check:
- Assistant
The code compiles cleanly with no warnings. Now let me create a summary of all the changes I made:
- Assistant
Now let me do a final checklist verification before submitting. Let me run all the existing tests one more time to make sure everything works:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.