diff --git a/app/lib/core/servers/chat/chat_identity.dart b/app/lib/core/servers/chat/chat_identity.dart index bf83a97d..3e918055 100644 --- a/app/lib/core/servers/chat/chat_identity.dart +++ b/app/lib/core/servers/chat/chat_identity.dart @@ -20,3 +20,8 @@ String federatedChatId(String server, String native) => } return null; } + +/// Whether a request field is a chat-scoped body whose `session_id` names a +/// chat: every `chat_*` body, plus the chat's Claude account requests. +bool isChatScopedField(String key) => + key.startsWith('chat_') || key == 'claude_account'; diff --git a/app/lib/core/servers/chat/chat_server_gateway.dart b/app/lib/core/servers/chat/chat_server_gateway.dart index a64cce9b..8b4e1100 100644 --- a/app/lib/core/servers/chat/chat_server_gateway.dart +++ b/app/lib/core/servers/chat/chat_server_gateway.dart @@ -62,7 +62,7 @@ class ChatServerGateway implements ApiGateway, EndpointGateway { for (final entry in fields.entries) entry.key: entry.key == 'attention_acks' && entry.value is Map ? _acks(entry.value as Map, incoming: false) - : entry.key.startsWith('chat_') && entry.value is Map + : isChatScopedField(entry.key) && entry.value is Map ? _owner(entry.value as Map, incoming: false) : entry.value, }; diff --git a/app/lib/core/servers/chat/federated_chat_gateway.dart b/app/lib/core/servers/chat/federated_chat_gateway.dart index f5454f13..51a830ae 100644 --- a/app/lib/core/servers/chat/federated_chat_gateway.dart +++ b/app/lib/core/servers/chat/federated_chat_gateway.dart @@ -136,7 +136,7 @@ class FederatedChatGateway implements ApiGateway { } final ids = {}; for (final entry in fields.entries) { - if (entry.key.startsWith('chat_') && entry.value is Map) { + if (isChatScopedField(entry.key) && entry.value is Map) { final session = (entry.value as Map)['session_id']; if (session is String) { if (session.startsWith('chat@') && diff --git a/app/test/core/servers/chat/federated_chat_gateway_test.dart b/app/test/core/servers/chat/federated_chat_gateway_test.dart index 2ac80d23..31bb57ff 100644 --- a/app/test/core/servers/chat/federated_chat_gateway_test.dart +++ b/app/test/core/servers/chat/federated_chat_gateway_test.dart @@ -336,4 +336,33 @@ void main() { await machine.events.close(); }, ); + + test('a chat\'s Claude account request reaches its own machine with the ' + 'native session id', () async { + final a = Machine(), b = Machine(); + final sources = { + 'a': ChatServerGateway(server('a'), a), + 'b': ChatServerGateway(server('b'), b), + }; + final all = FederatedChatGateway( + () => sources, + () => 'a', + const Stream.empty(), + ); + for (final type in ['claude_accounts', 'claude_account_use']) { + await all.call(type, { + 'claude_account': { + 'session_id': federatedChatId('b', 'collision'), + 'name': 'second', + }, + }); + final (sent, fields) = b.calls.last; + expect(sent, type); + expect(fields['claude_account'], { + 'session_id': 'collision', + 'name': 'second', + }); + } + expect(a.calls.where((c) => c.$1.startsWith('claude_')), isEmpty); + }); }