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
Let me start by exploring the repository structure to understand what we're working with.
Assistant
Let me explore the repository structure more to find the WebUI related code.
Assistant
Let me look at the chat module structure first.
Assistant
Let me look at the chat_send_handler and chat_new_thread_handler to understand how they currently work.
Assistant
Let me continue exploring the chat module. I need to see more code to understand the existing structure, then make a plan.
Assistant
Let me look at the types.rs file to understand the
SendMessageRequestandSendMessageResponse.Assistant
Now let me look at the session manager to understand the threads structure.
Assistant
Let me look at how the responses API and OpenAI compat handle idempotency since they might have similar concerns.
Assistant
There are no existing idempotency mechanisms for chat messages. Now let me look at the parent issue #3607 in the docs.
Assistant
There's an
IdempotencyKeytype defined somewhere. Let me look for it.Assistant
Let me look at how the existing test suite looks to understand the test patterns.
Assistant
Let me look at the test_helpers file to see how to construct a GatewayState for testing.
Assistant
Let me look at existing tests for chat_send_handler.
Assistant
Let me look at the chat_new_thread tests.
Assistant
Now let me look at the openai_compat.rs tosee if there's any existing idempotency-like behavior.
Assistant
Let me look at the IncomingMessage structure and how it's processed.
Assistant
Let me look at the IncomingMessage more, particularly the
web_incoming_messagehelper.Assistant
Let me check how the agent handles deduplication, particularly in the session/turn management.
Assistant
Let me also check what fields are in the test_user struct.
Assistant
Let me now look at the engine v2 thread_id_isolation test.
Assistant
Now let me look at the test_db infrastructure to understand how to use it.
Assistant
Let me check the build setup and ensure I understand it.
Assistant
Now let me do a quick sanity check that the project builds.
Assistant
Now I have enough understanding to design the solution. Let me lay out the plan:
Plan
Goal
Make WebUI mutations (
chat/send,chat/thread/new) idempotent so duplicate browser submits do not create duplicate turns/messages.Approach
- Add an
IdempotencyLedgertoGatewayState— a per-user map from(user_id, scope, action_id)to anAcceptedRef. Uses an in-flight slot +Notifyfor concurrent duplicate handling. - Add optional
idempotency_keyfield toSendMessageRequestand aIdempotency-Keyheader convention forchat_new_thread_handler. - Update handlers to consult the ledger before performing the side effect, and to store the accepted ref on success.
- Validation failures do not create ledger entries — allowing retries.
- Concurrent duplicates: only one tx.send / DB write happens; secondaries wait via Notify and replay the cached accepted ref.
Let me start implementing. First, I'll create the idempotency ledger.
- Add an
Assistant
Now let me start implementing. First, I'll create the idempotency ledger module.
Assistant
Now let me add the module to the platform mod.rs.
Assistant
Now let me add the ledger to the
GatewayState.Assistant
Now let me add the
idempotency_ledgertoGatewayState. Let me find the right place.Assistant
Now let me add the idempotency ledger field to
GatewayState.Assistant
Now I need to update the test_helpers.rs to include the new field. Let me find all the places that construct
GatewayState.Assistant
Let me first update the test_helpers.rs file to construct the ledger.
Assistant
Let me use a Python script to update all the constructions of
GatewayStateto add theidempotency_ledgerfield.Assistant
Total = 17. Let me update them all. For each, I need to add
idempotency_ledger: Arc::new(IdempotencyLedger::new()),to the construction.The most efficient approach is to look for
tool_dispatcher: None,followed by}in a GatewayState context, and add the field. But that's tricky with regex.Actually, let me look at the patterns more carefully. The GatewayState constructions end with:
tool_dispatcher: None,(when no tool dispatcher)tool_dispatcher: Some(...),(when there is one)
I can add the field right after
tool_dispatcher:in all cases.Let me check what kinds of tool_dispatcher values exist:
Assistant
So the structure is consistent. After
tool_dispatcher: ...(which can beNoneorSome(...)), there's},or}, and we need to addidempotency_ledger: Arc::new(IdempotencyLedger::new()),in between.Wait, I also need to look at the settings/mod.rs more carefully because there are 7 GatewayState constructions there.
Assistant
I see - lines 1426, 1445, 1464 use the functional update syntax
GatewayState { secrets_store: None, ..test_gateway_state(...) }. These will work as-is because the basetest_gateway_statereturns a GatewayState with the new field set.The full GatewayState constructions are:
- 1263-1317 (the
test_gateway_statefunction in settings/mod.rs) - 2384 (settings test)
- 2461 (settings test)
Let me check 2384 and 2461 to see if they're full or partial.
- 1263-1317 (the
Assistant
These all use the functional update syntax. So I only need to update the full GatewayState constructions. Let me check each file's specifics.
Assistant
Let me look at the various closing patterns more carefully.
Assistant
Now I'll write a Python script to add the
idempotency_ledgerfield to each construction.Assistant
Good. Let me also check the mod.rs at line 210 which is rebuild_state (should use functional update):
Assistant
I see two issues:
- Line 199 in mod.rs has indentation that's wrong - the new field has different indentation from the rest.
- Line 266 in rebuild_state needs to be updated too.
Let me fix the indentation issue and add the field to rebuild_state.
Assistant
Let me fix the indentation issue in mod.rs.
Assistant
Now let me add the
idempotency_ledgerto therebuild_statefunction.Assistant
Let me verify all the modifications look correct.
Assistant
Let me look at the settings/mod.rs to see if its test_gateway_state is gated by cfg(test).
Assistant
Indentation is wrong for settings/mod.rs (line 1316). Let me fix that.
Assistant
Now let me update
types.rsto add theidempotency_keyfield toSendMessageRequest.Assistant
Now let me also add a
NewThreadRequeststruct.Assistant
Now let me build the code to see if everything compiles.
Assistant
Let me fix the compilation errors.
Assistant
The acquire function uses
entry()which returns a&mut Entry, so we canmutate it. But.clone()gives us an ownedEntrythat's immutable. Let me fix it.Assistant
The library compiles. Now let me update the
chat_send_handlerandchat_new_thread_handlerto use the idempotency ledger.Assistant
Let me now update the handlers in chat/mod.rs. First, let me understand the structure better.
Assistant
Now let me update the chat_send_handler to use idempotency.
Assistant
Now I need to add the helper functions. Let me add them after the chat_send_handler.
Assistant
Now let me add the helper functions just before the chat_approval_handler.
Assistant
Now let me update the chat_new_thread_handler similarly.
Assistant
Now let me update the
chat_new_thread_handlerto use idempotency. Since the handler currently takes no body, I'll use the HTTP header convention.Assistant
Let me fix both errors.
Assistant
Now I need to add
DeserializetoThreadInfo. Let me check the current struct.Assistant
The library compiles. Let me look at the warnings -
last_used_atfield is unused.Assistant
Let me remove the unused fields.
Assistant
Library compiles cleanly. Now let me check the test build.
Assistant
I have many test files to update. Let me find all the constructions.
Assistant
Let me fix the
SendMessageRequestconstructions and the chat_new_thread_handler call in chat/mod.rs first.Assistant
Now let me find and fix the
chat_new_thread_handlertest calls (line 2595 in the error).Assistant
Now let me find and fix the test GatewayState constructions.
Assistant
Now I see the issue with line 534 - it's not using
tool_dispatcher.Let me check.Assistant
Let me update the multi_tenant_integration.rs line 534 manually.
Assistant
Now let me check the existing updates and verify they have correct indentation.
Assistant
Let me check the indentation of these lines.
Assistant
The indentation is wrong. Let me fix it.
Assistant
Some lines still have 4-space indentation. Let me look at the surrounding context.
Assistant
I see - the surrounding fields have 8-space indentation, but my script didn't fix the existing 4-space ones. The reason is
startswith(' ')matches an 8-space prefix, so lines with 4 spaces stay. Let me fix them.Assistant
Now let me check the compilation.
Assistant
The integration tests in
tests/directory can't findcrate::channels. This is because theidempotencymodule is insidecrate::channels::web::platform::idempotency. Let me check how other tests reference things.Assistant
I see, integration tests use
ironclaw::channels::web::platform::...notcrate::.... Let me fix the path.Assistant
Library and tests compile cleanly. Let me also verify all tests are accessible by running a quick build of test binaries.
Assistant
All 15 unit tests for the idempotency module pass. Let me also run the existing chat send tests.
Assistant
Now let me also test the new thread handler test.
Assistant
Let me write some new tests for the idempotency behavior.
Assistant
Let me fix the broken indentation in the existing tests.
Assistant
The issue is
}should be};. Let me fix that.Assistant
Now let me add new tests for the idempotency behavior.
Assistant
Let me add the new tests after the existing
test_chat_new_thread_handler_persists_to_db_and_sessiontest.Assistant
I need to add
ClonetoSendMessageRequestand find the right constant for attachment size.Assistant
Now let me fix the
MAX_INLINE_ATTACHMENT_TOTAL_BYTESreference. Let me use a different approach.Assistant
Test passes! Let me run all the new tests.
Assistant
Let me run each test separately, starting with the slow one that uses 6MB attachment.
Assistant
Let me check the inline_attachments_to_incoming function.
Assistant
I need a larger payload. Let me increase to ~8 MiB of base64 (which would decode to ~6 MiB, exceeding 5 MiB).
Assistant
The first validation failed (good) but the retry was rejected with 409. The issue is that the validation failure happens AFTER the ledger acquire, so the slot remains in-flight. Let me fix the handler.
Assistant
Let me refactor the handler to use an inner function/closure that does the work and ensures
fail()is called on any error path.Assistant
The closure syntax doesn't work for async. Let me use anexplicit async block.
Assistant
Let me restructure to use an async block.
Assistant
Let me run all the new chat_send tests now.
Assistant
The
from_staticdoesn't accept NUL bytes. Let me fix the test.Assistant
Now let me run all the chat_send tests at once.
Assistant
All 11 chat_send tests pass. Now let me run the new_thread tests.
Assistant
The test uses
test_gateway_state_with_dependencieswhich doesn't set session_manager. Let me fix.Assistant
Let me check the
TestGatewayBuilder::build()return type.Assistant
The
build()returnsArc<GatewayState>. SoArc::new(TestGatewayBuilder::new().build())would beArc<Arc<GatewayState>>. Let me fix.Assistant
Let me use a different approach - write a helper inline in the test.
Assistant
Looks like the test is hanging. Let me debug.
Assistant
Let me check if SessionManager could be causing the hang.
Assistant
I found a bug - the fingerprint in
complete()doesn't match the one inacquire(). Let me fix.Assistant
Let me make this cleaner by making the FINGERPRINT a public constant.
Assistant
The function
run_with_new_thread_idempotencyis called at line 1217 from withinchat_new_thread_handler, butchat_new_thread_handleris defined ABOVE the function. Letme check the order.Assistant
Let me use a module-level constant instead.
Assistant
Now let me run all the new thread tests.
Assistant
All 3 new thread tests pass. Now let me run all the chat tests to make sure nothing else is broken.
Assistant
All 55 chat tests pass. Let me also run the idempotency module tests.
Assistant
Now let me also test other tests that use the test_helpers and GatewayState to make sure they didn't break.
Assistant
All 57 multi_tenant tests pass. Let me also run the rebuild_state test.
Assistant
Let me also test the handlers/memory and platform/ws tests.
Assistant
All tests pass. Now let me also check if the integration tests build.
Assistant
All tests build. Let me also verify some integration tests still work.
Assistant
Let me check the openai_compat to make sure it doesn't need updating.
Assistant
All 16 openai_compat tests pass. Let me also run a quick check on the wider test suite to make sure nothing's broken.
Assistant
1023 tests passed in the channels module! Let me also run all the library tests.
Assistant
The test failure is in
cli::completion::tests::test_run_generates_outputwhich is unrelated to my changes - it's an existing stack overflow issue. Let me verify.Assistant
Confirmed - the test failure is unrelated to my changes. It's a pre-existing stack overflow in
cli::completion::tests::test_run_generates_output. Let me restore my changes.Assistant
All changes are restored. Let me verify the code compiles and tests pass.
Assistant
All 55 chat tests pass! Let me now also run a quick scan of the entire test suite to make sure everything is OK.
Assistant
All 1023 channels tests pass. Let me also verify the agent tests still work.
Assistant
All 497 agent tests pass. Let me now do a final review of my changes.
Assistant
Let me also verify clippy is clean.
Assistant
Let me fix the clippy warning.
Assistant
No clippy warnings or errors. Let me also run clippy on the tests.
Assistant
Let me add the
is_emptymethod.Assistant
The remaining warnings are pre-existing in
tracing_fmt.rsand unrelated to my changes. Let me run the tests one more time to verify.Assistant
All tests pass. Let me also verify my changes look good. Let me read the final chat/mod.rs handler implementation.
Assistant
Let me also do a final review of the idempotency module to make sure the documentation is clear.
Assistant
Now let me create a summary of all the changes I made and review them.
Assistant
Let me also make sure that the channel.rs module's get_or_create_session is OK. Actually that doesn't change. Let me look at a final test summary to verify the scope.
Assistant
All 29 tests for the idempotency feature pass. Let me now also verify the openai_compat tests, the platform tests, and the integration tests one more time.
Assistant
All 131 platform tests pass. Let me also test rebuild_state.
Assistant
The rebuild_state test still passes, confirming that the
idempotency_ledgerfield is correctly preserved through rebuild operations.Now let me also check that I haven't broken anything by reviewing the changes I made:
Assistant
The changes look good. Now let me check the final diff for the chat handler:
Assistant
Now let me run a comprehensive test to verify everything works.
Assistant
Let me run a final comprehensive test of all the related tests.
Assistant
537 tests pass in channels::web. Let me also verify the openai_compat and ws_gateway integration tests still work.
Assistant
All openai_compat integration tests pass. Let me also do a final check on the diff for the platform/idempotency.rs to make sure it looks good.
Assistant
Let me now do a final review of everything I've changed.
Assistant
Let me also look at how the test for the same-thread duplicate scenario works:
Assistant
All my required test cases exist. Let me also check the AGENTS.md / CLAUDE.md to make sure I'm following conventions.
Assistant
Only
panic!in tests, which is fine. No productionunwrap()/expect(). Let me also check the test_helpers for similar patterns.Assistant
No production
unwrap()/expect().Now let me also verify a key edge case - the rebuild_state preserves the idempotency ledger. This is already verified by the rebuild_state test. Let me also do one more quick verification by running the related tests.
Assistant
All 30 tests pass. Let me now make sure the final diff is comprehensive.
Assistant
Let me do one last sanity check - run a representative subset of tests one final time.
Assistant
All 537 web tests pass. Let me also verify that my new idempotency tests are part of that count.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.