Skip to content

The agent that grows with you

fix(desktop): submit a fresh chat to the owner its create published

unsupportedpastels4546bb62ae57
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.test.tsx
index f065eb53c8f..c3ca2749e50 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.test.tsx
@@ -367,6 +367,44 @@ describe('submission intent destinations', () => {
}
)
+ it.each([true, false])('submits a fresh draft to the owner its create published after activation (owner known: %s)', async ownerKnown => {
+ const { deps, requestGateway } = setup()
+ deps.activeSessionIdRef.current = null
+ deps.selectedStoredSessionIdRef.current = null
+ deps.getRuntimeIdForStoredSession = () => 'runtime-new'
+ $connection.set({ connectionId: 'local', profile: 'default', mode: 'local' } as never)
+ deps.createBackendSessionForSend.mockImplementation(async () => {
+ // A newly created profile finishes activating while session.create is in flight.
+ $activeGatewayProfile.set('fresh-profile')
+ $connection.set({ connectionId: 'local', profile: 'fresh-profile', mode: 'local' } as never)
+ deps.activeSessionIdRef.current = 'runtime-new'
+ deps.selectedStoredSessionIdRef.current = 'stored-new'
+
+ if (ownerKnown) {
+ $sessions.set([{ id: 'stored-new', connection_id: 'local', profile: 'fresh-profile' }] as never)
+ }
+
+ return 'runtime-new' as never
+ })
+ const { result } = renderHook(() => useSubmitPrompt(deps))
+
+ await act(async () => {
+ expect(await result.current('first message in the new profile')).toBe(ownerKnown)
+ })
+
+ expect(requestGateway.mock.calls.filter(call => call[0] === 'prompt.submit')).toHaveLength(0)
+ const routed = wire.request.mock.calls.filter(call => call[2] === 'prompt.submit')
+
+ if (ownerKnown) {
+ expect(routed).toHaveLength(1)
+ expect(routed[0].slice(0, 2)).toEqual(['local', 'fresh-profile'])
+ expect(routed[0][3]).toMatchObject({ session_id: 'runtime-new', text: 'first message in the new profile' })
+ } else {
+ // Without a published owner the write keeps the entry authority, which has moved.
+ expect(routed).toHaveLength(0)
+ }
+ })
+
it('assigns one direct ID before preprocessing and keeps it through busy retries', async () => {
const { deps, requestGateway } = setup()
let attempts = 0
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.ts
index 998bf300bdf..3409b841b93 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submission-destination.ts
@@ -1,6 +1,10 @@
import { $activeGatewayProfile } from '@/store/profile'
import { $connection } from '@/store/session'
-import { requestForSessionProfile, type SessionOwnerScope } from '@/store/session-request-router'
+import {
+ requestForSessionProfile,
+ type SessionOwnerScope,
+ sessionRpcNeedsProfileRoute
+} from '@/store/session-request-router'
import { knownOwnerForSession } from '@/store/session-states'
import type { GatewayRequest } from './utils'
@@ -81,3 +85,29 @@ export function captureSubmissionDestination(
return destination
}
+
+/** A fresh draft had no owner at entry, so it held the ambient socket. The
+ * create it performed published the chat's exact owner; a profile activation
+ * settling during that create moves the ambient authority, not this session.
+ * Without a published owner the entry guard still decides. */
+export function destinationAfterCreate(
+ destination: SubmissionDestination,
+ sessionId: string,
+ request: GatewayRequest
+): SubmissionDestination {
+ const owner = destination.owner ? undefined : knownOwnerForSession(sessionId)
+
+ return sessionRpcNeedsProfileRoute(owner) ? captureSubmissionDestination(sessionId, request, { owner }) : destination
+}
+
+/** A draft created outside this submit (slash kickoff) had no owner at entry.
+ * Journal its published route only if it is the SAME captured authority. */
+export function sameAuthorityPublishedOwner(
+ destination: SubmissionDestination,
+ sessionId: null | string | undefined,
+ request: GatewayRequest
+): SessionOwnerScope {
+ const published = captureSubmissionDestination(sessionId, request, { owner: knownOwnerForSession(sessionId) })
+
+ return published.scopeKey === destination.scopeKey ? published.owner : undefined
+}
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
index 5eb0ce4ac34..9bb084f5bb4 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
@@ -38,7 +38,7 @@ import {
touchSessionActivity
} from '@/store/session'
import { profileScopeForSessionOwner } from '@/store/session-request-router'
-import { $sessionStates, $sessionTiles, knownOwnerForSession } from '@/store/session-states'
+import { $sessionStates, $sessionTiles } from '@/store/session-states'
import type { SessionInfo } from '@/types/hermes'
import { isCronRunSessionId, refreshCronRunWriteGate } from '../../../cron/open-cron-run'
@@ -56,7 +56,7 @@ import {
writePreparedSubmission
} from './prepared-submissions'
import { registerRecoveredRuntime, singleFlightSessionResume, takeRecoveredRuntime } from './single-flight-resume'
-import { captureSubmissionDestination } from './submission-destination'
+import { captureSubmissionDestination, destinationAfterCreate, sameAuthorityPublishedOwner } from './submission-destination'
import {
acquireSubmitInFlight,
type GatewayRequest,
@@ -469,11 +469,11 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
return false
}
- const destination = retained
+ let destination = retained
? captureSubmissionDestination(targetStoredSessionId ?? sessionId, ambientRequestGateway, retained)
: captured
- const requestGateway = destination.requestGateway
+ let requestGateway = destination.requestGateway
const submissionId = retained?.id ?? options?.submission_id ?? crypto.randomUUID()
let startingStoredSessionId = routedSessionNeedsResume
@@ -989,6 +989,8 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
}
sessionId = createdRuntimeId
+ destination = destinationAfterCreate(destination, targetStoredSessionId ?? sessionId, ambientRequestGateway)
+ requestGateway = destination.requestGateway
}
try {
@@ -1066,17 +1068,9 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
...(titlePreview && { title_preview: titlePreview })
})
- // A fresh draft had no session owner at entry. Adopt its published
- // route only if it is the SAME captured connection/profile authority.
- const publishedDestination = captureSubmissionDestination(targetStoredSessionId, ambientRequestGateway, {
- owner: knownOwnerForSession(targetStoredSessionId)
- })
-
const prepared: NonNullable<typeof retained> = retained ?? {
id: submissionId,
- owner:
- destination.owner ??
- (publishedDestination.scopeKey === destination.scopeKey ? publishedDestination.owner : undefined),
+ owner: destination.owner ?? sameAuthorityPublishedOwner(destination, targetStoredSessionId, ambientRequestGateway),
displayText: options?.displayText,
attachments: syncedAttachments,
text,