The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): give a pre-save sign-in the same jar its saved connection reads
Hermes Agent3836710cfa0b
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 2f79838b353..8b0a79e8b19 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -7248,10 +7248,12 @@ function getOauthSession() {// shared partition; see oauth-partition.ts for the full rules.const oauthSessionsByPartition = new Map()-function resolveOauthPartitionForUrl(url, { connectionId = '' } = {}) {+function resolveOauthPartitionForUrl(url, { connectionId = '', pendingAuthMode = '', pendingKind = '' } = {}) {try {return resolveOauthPartition(url, {connectionId,+ pendingAuthMode,+ pendingKind,registry: readDesktopConnectionsRegistry(),v1RemoteUrl: readDesktopConnectionConfig()?.remote?.url})@@ -7261,8 +7263,8 @@ function resolveOauthPartitionForUrl(url, { connectionId = '' } = {}) {}}-function getOauthSessionForUrl(url, { connectionId = '' } = {}) {- const partition = resolveOauthPartitionForUrl(url, { connectionId })+function getOauthSessionForUrl(url, { connectionId = '', pendingAuthMode = '', pendingKind = '' } = {}) {+ const partition = resolveOauthPartitionForUrl(url, { connectionId, pendingAuthMode, pendingKind })if (partition === OAUTH_SESSION_PARTITION) {return getOauthSession()@@ -7337,8 +7339,8 @@ function warmOauthCookieStore(url?) {// connection-config.ts (cookiesHaveSession / cookiesHaveLiveSession). See// that module for details.-async function hasOauthSessionCookie(baseUrl, { connectionId = '' } = {}) {- const sess = getOauthSessionForUrl(baseUrl, { connectionId })+async function hasOauthSessionCookie(baseUrl, { connectionId = '', pendingAuthMode = '', pendingKind = '' } = {}) {+ const sess = getOauthSessionForUrl(baseUrl, { connectionId, pendingAuthMode, pendingKind })if (!sess) {return false@@ -7460,7 +7462,7 @@ async function clearOauthSession(baseUrl) {// ``/auth/login`` → portal ``/oauth/authorize`` (auto-approves org members)// → ``/auth/callback``, which sets the gateway cookie with NO interactive// prompt. This is the per-agent cloud cascade (decisions.md Q5).-function openOauthLoginWindow(baseUrl, { silent = false, background = false, connectionId = '' } = {}) {+function openOauthLoginWindow(baseUrl, { silent = false, background = false, connectionId = '', pendingAuthMode = '', pendingKind = '' } = {}) {return new Promise((resolve, reject) => {if (!app.isReady()) {reject(new Error('Desktop is not ready to start an OAuth login.'))@@ -7468,7 +7470,7 @@ function openOauthLoginWindow(baseUrl, { silent = false, background = false, conreturn}- const sess = getOauthSessionForUrl(baseUrl, { connectionId })+ const sess = getOauthSessionForUrl(baseUrl, { connectionId, pendingAuthMode, pendingKind })if (!sess) {reject(new Error('OAuth session partition is unavailable.'))@@ -7521,7 +7523,7 @@ function openOauthLoginWindow(baseUrl, { silent = false, background = false, conreturn}- if (await hasOauthSessionCookie(baseUrl, { connectionId })) {+ if (await hasOauthSessionCookie(baseUrl, { connectionId, pendingAuthMode, pendingKind })) {finish(null)}}@@ -16326,6 +16328,15 @@ ipcMain.handle('hermes:connection-config:oauth-login', async (_event, rawUrl, raloginConnectionId = ''}+ // The draft's intended entry shape (kind/authMode the save will persist).+ // The identity branch in oauth-partition.ts gates the pre-save private jar+ // on it: only a cookie-auth remote draft earns its own jar up front; a+ // cloud or token draft signs in on the legacy jar — the jar the saved+ // entry reads — so login and read can never disagree. Invalid or missing+ // values fail closed to the legacy jar in the resolver.+ const pendingKind = typeof rawOpts?.kind === 'string' ? rawOpts.kind : ''+ const pendingAuthMode = typeof rawOpts?.authMode === 'string' ? rawOpts.authMode : ''+let statusBody: any = nulltry {@@ -16391,9 +16402,17 @@ ipcMain.handle('hermes:connection-config:oauth-login', async (_event, rawUrl, ra}// Legacy embedded-webview cookie flow.- await openOauthLoginWindow(baseUrl, { connectionId: loginConnectionId })+ await openOauthLoginWindow(baseUrl, {+ connectionId: loginConnectionId,+ pendingAuthMode,+ pendingKind+ })- const connected = await hasOauthSessionCookie(baseUrl, { connectionId: loginConnectionId })+ const connected = await hasOauthSessionCookie(baseUrl, {+ connectionId: loginConnectionId,+ pendingAuthMode,+ pendingKind+ })// Only a CONFIRMED sign-in releases the latch. A cancelled/closed login// window must leave it set, or the overlay's "Sign in" button startsdiff --git a/apps/desktop/electron/oauth-partition.test.ts b/apps/desktop/electron/oauth-partition.test.tsindex c964a5b6d5a..562d3d85dcd 100644--- a/apps/desktop/electron/oauth-partition.test.ts+++ b/apps/desktop/electron/oauth-partition.test.ts@@ -165,21 +165,39 @@ describe('resolveOauthPartition (#92183 per-connection cookie jars)', () => {// must then write to the jar the saved entry will read — an unsaved draft has// no on-disk entry to URL-match, so identity (connectionId) decides instead.describe('resolveOauthPartition with connectionId (pre-save sign-in identity)', () => {- it('sends a pending (unsaved) draft sign-in to its own jar, not the legacy shared one', () => {+ it('sends a pending (unsaved) cookie-auth remote draft to its own jar, not the legacy shared one', () => {const reg = registry('local', [{ id: 'local', kind: 'local' }])- const got = resolveOauthPartition('https://macmini.lan:9119', { registry: reg, connectionId: 'macmini' })+ const got = resolveOauthPartition('https://macmini.lan:9119', {+ registry: reg,+ connectionId: 'macmini',+ pendingAuthMode: 'oauth',+ pendingKind: 'remote'+ })expect(got).not.toBe(LEGACY_OAUTH_PARTITION)expect(got).toContain('conn-macmini')// Deterministic across calls — the saved entry must read the jar the login wrote.- expect(resolveOauthPartition('https://macmini.lan:9119', { registry: reg, connectionId: 'macmini' })).toBe(got)+ expect(+ resolveOauthPartition('https://macmini.lan:9119', {+ registry: reg,+ connectionId: 'macmini',+ pendingAuthMode: 'oauth',+ pendingKind: 'remote'+ })+ ).toBe(got)})it('keeps a pending same-host sign-in out of an existing gateway’s jar (no #92183 eviction)', () => {const reg = registry('local', [{ id: 'local', kind: 'local' }, remote('conn-a', 'https://10.27.27.7:9119')])- const pending = resolveOauthPartition('https://10.27.27.7:9220', { registry: reg, connectionId: 'conn-b' })+ const pending = resolveOauthPartition('https://10.27.27.7:9220', {+ registry: reg,+ connectionId: 'conn-b',+ pendingAuthMode: 'oauth',+ pendingKind: 'remote'+ })+const existing = resolveOauthPartition('https://10.27.27.7:9119', { registry: reg })expect(pending).not.toBe(LEGACY_OAUTH_PARTITION)@@ -241,10 +259,107 @@ describe('resolveOauthPartition with connectionId (pre-save sign-in identity)',}})+ // The unknown-id shortcut must grant a private jar ONLY to the draft+ // shapes that earn one after the save. Before this gate, ANY unsaved id got+ // its own jar — so a pre-save CLOUD sign-in wrote its portal session into+ // `persist:hermes-remote-oauth-conn-<id>` while the saved cloud entry kept+ // reading the shared `persist:hermes-remote-oauth` (the silent per-agent+ // cascade jar): a successful-looking sign-in that bounces straight back to+ // signed-out, plus an orphan jar. The invariant below is the contract: for+ // every draft shape, the jar the login writes equals the jar the saved+ // entry reads.+ it('pins the login-jar == saved-read-jar invariant for every draft shape', () => {+ const draftUrl = 'https://gw-draft.example.com'++ const shapes = [+ ['remote', 'oauth'],+ ['remote', 'token'],+ ['cloud', 'oauth']+ ] as const++ for (const [kind, authMode] of shapes) {+ const draftReg = registry('local', [{ id: 'local', kind: 'local' }])++ const loginJar = resolveOauthPartition(draftUrl, {+ registry: draftReg,+ connectionId: 'draft-1',+ pendingAuthMode: authMode,+ pendingKind: kind+ })++ const savedReg = registry('local', [+ { id: 'local', kind: 'local' },+ kind === 'remote'+ ? remote('draft-1', draftUrl, { authMode })+ : { id: 'draft-1', kind, url: draftUrl, authMode }+ ])++ const readJar = resolveOauthPartition(draftUrl, { registry: savedReg })++ expect(loginJar).toBe(readJar)+ }+ })++ it('keeps a pending cloud draft on the legacy shared jar (portal cascade)', () => {+ const reg = registry('local', [{ id: 'local', kind: 'local' }])++ const got = resolveOauthPartition('https://team.hermes.cloud', {+ registry: reg,+ connectionId: 'team-cloud',+ pendingAuthMode: 'oauth',+ pendingKind: 'cloud'+ })++ expect(got).toBe(LEGACY_OAUTH_PARTITION)+ })++ it('keeps a pending token-auth remote draft on the legacy jar (no cookies)', () => {+ const reg = registry('local', [{ id: 'local', kind: 'local' }])++ const got = resolveOauthPartition('https://gw-t.example.com', {+ registry: reg,+ connectionId: 'tok-draft',+ pendingAuthMode: 'token',+ pendingKind: 'remote'+ })++ expect(got).toBe(LEGACY_OAUTH_PARTITION)+ })++ it('keeps a pending draft whose URL IS the v1 remote on the legacy jar (migration)', () => {+ const reg = registry('local', [{ id: 'local', kind: 'local' }])++ const got = resolveOauthPartition('https://gw-a.example.com', {+ registry: reg,+ connectionId: 'mig-draft',+ pendingAuthMode: 'oauth',+ pendingKind: 'remote',+ v1RemoteUrl: 'https://gw-a.example.com'+ })++ expect(got).toBe(LEGACY_OAUTH_PARTITION)+ })++ it('fails closed for an unknown-id sign-in with no pending shape: legacy jar, never a private one', () => {+ // A stale renderer can still name an id without the draft shape. The+ // resolver must never hand such a sign-in a private jar the saved entry+ // would not read — the legacy jar is the safe pre-PR behavior.+ const reg = registry('local', [{ id: 'local', kind: 'local' }])++ expect(resolveOauthPartition('https://gw.example.com', { registry: reg, connectionId: 'ghost' })).toBe(+ LEGACY_OAUTH_PARTITION+ )+ })+it('sanitizes hostile pending ids into partition-safe names', () => {const reg = registry('local', [{ id: 'local', kind: 'local' }])- const got = resolveOauthPartition('https://gw.example.com', { registry: reg, connectionId: 'we ird/id:€' })+ const got = resolveOauthPartition('https://gw.example.com', {+ registry: reg,+ connectionId: 'we ird/id:€',+ pendingAuthMode: 'oauth',+ pendingKind: 'remote'+ })expect(got.startsWith('persist:')).toBe(true)expect(got).not.toMatch(/[\s/€]/)diff --git a/apps/desktop/electron/oauth-partition.ts b/apps/desktop/electron/oauth-partition.tsindex 4b865d00bd5..d80d19f3520 100644--- a/apps/desktop/electron/oauth-partition.ts+++ b/apps/desktop/electron/oauth-partition.ts@@ -75,6 +75,16 @@ export interface ResolveOauthPartitionOptions {* is skipped entirely.*/connectionId?: unknown+ /**+ * Draft entry shape for the pre-save login: the `kind`/`authMode` the save+ * will persist. The unknown-id shortcut below grants a private jar only+ * when the saved entry would earn one (a non-primary cookie-auth remote);+ * cloud and token drafts keep the legacy shared jar, which is what they+ * resolve to after the save. Absent/invalid values fail closed to the+ * legacy jar — never a private one a saved entry would not read.+ */+ pendingAuthMode?: unknown+ pendingKind?: unknown}/**@@ -156,7 +166,26 @@ export function resolveOauthPartition(requestUrl: unknown, opts: ResolveOauthParconst own = (registry.connections as unknown[]).find(c => entryField(c, 'id') === wantedId)if (!own) {- return `${CONNECTION_PARTITION_PREFIX}${sanitizePartitionComponent(wantedId)}`+ // No persisted entry yet: gate the up-front private jar on the SAME+ // eligibility the URL-match path applies after the save, so the jar the+ // login writes is always the jar the saved entry reads. Only a+ // non-primary cookie-auth remote earns its own partition (module+ // header): cloud drafts must share the portal jar (the silent per-agent+ // cloud cascade depends on it) and token drafts never ride cookies, so+ // both sign in on the legacy jar — which is also what they resolve to+ // once saved. A draft whose URL IS the v1 remote's lands on legacy for+ // the same reason a saved v1-migrated entry does. An unknown id cannot+ // be the registry primary (the primary is, by definition, persisted), so+ // no primary check is needed here. Missing/invalid pending shape (an+ // older renderer) fails closed to the legacy jar, never a private one.+ const pendingKind = typeof opts.pendingKind === 'string' ? opts.pendingKind : ''+ const pendingAuthMode = typeof opts.pendingAuthMode === 'string' ? opts.pendingAuthMode : ''++ return pendingKind === 'remote' &&+ pendingAuthMode === 'oauth' &&+ !(v1Norm && requestNorm === v1Norm)+ ? `${CONNECTION_PARTITION_PREFIX}${sanitizePartitionComponent(wantedId)}`+ : LEGACY_OAUTH_PARTITION}const ownBaseNorm = normalizeForMatch(entryField(own, 'url'))diff --git a/apps/desktop/src/app/settings/connections-registry.test.tsx b/apps/desktop/src/app/settings/connections-registry.test.tsxindex ba2f4acd645..4c80a0c3b1a 100644--- a/apps/desktop/src/app/settings/connections-registry.test.tsx+++ b/apps/desktop/src/app/settings/connections-registry.test.tsx@@ -136,7 +136,11 @@ describe('ConnectionsRegistrySection', () => {await waitFor(() =>expect(oauthLoginConnectionConfig).toHaveBeenCalledWith('https://a.example', {connectionId: null,- label: 'New gateway'+ label: 'New gateway',+ // The draft's kind/authMode ride along: they gate the pre-save cookie+ // jar in oauth-partition.ts (a cookie-auth remote earns its own jar).+ authMode: 'oauth',+ kind: 'remote'}))fireEvent.change(url, { target: { value: 'https://b.example' } })@@ -216,11 +220,15 @@ describe('ConnectionsRegistrySection', () => {fireEvent.click(await screen.findByRole('button', { name: /sign in/i }))// The draft identity rides along (#99989): a pre-save sign-in must name the// connection whose jar the login writes into — connectionId null (unset draft)- // plus the draft label here.+ // plus the draft label here. The kind/authMode matter just as much: a CLOUD+ // draft must sign in on the legacy shared portal jar, which is the jar the+ // saved cloud entry reads — never a private per-connection jar.await waitFor(() =>expect(oauthLoginConnectionConfig).toHaveBeenCalledWith('https://team.hermes.cloud', {connectionId: null,- label: 'Team cloud'+ label: 'Team cloud',+ authMode: 'oauth',+ kind: 'cloud'}))diff --git a/apps/desktop/src/app/settings/connections-registry.tsx b/apps/desktop/src/app/settings/connections-registry.tsxindex 85fa526a8f6..f48608f3dd1 100644--- a/apps/desktop/src/app/settings/connections-registry.tsx+++ b/apps/desktop/src/app/settings/connections-registry.tsx@@ -237,20 +237,35 @@ export function ConnectionsRegistrySection() {// the main process, so a crafted payload can't slip past the UI check).const [dupeError, setDupeError] = useState<null | string>(null)+ // The draft's auth mode lives inside the remote-setup state below; the+ // sign-in identity callback runs at login time (after that state exists),+ // so it reads the mode through this ref rather than the render-time closure.+ const remoteAuthModeRef = useRef<'oauth' | 'token'>('token')+const remote = useRemoteSetup({host: 'registry',enabled: editor?.kind === 'remote' || editor?.kind === 'cloud',onNotice: notify,// The registry draft can sign in BEFORE it is saved: the login carries// the draft's identity so the main process settles the id the save will- // reuse and writes the session into that connection's own cookie jar.- // Pin the settled id into the draft so Save reuses it.- oauthLoginIdentity: () => ({ connectionId: editor?.id ?? null, label: editor?.label ?? '' }),+ // reuse and writes the session into the jar the saved connection reads.+ // The kind/authMode gate in oauth-partition.ts decides WHICH jar that is:+ // a cookie-auth remote draft gets its own; cloud and token drafts share+ // the legacy jar, exactly what they resolve to after the save. Pin the+ // settled id into the draft so Save reuses it.+ oauthLoginIdentity: () => ({+ connectionId: editor?.id ?? null,+ label: editor?.label ?? '',+ kind: editor?.kind,+ authMode: editor?.kind === 'cloud' ? 'oauth' : remoteAuthModeRef.current+ }),onOAuthLoginSettled: settledId => {setEditor(prev => (prev && !prev.id ? { ...prev, id: settledId } : prev))}})+ remoteAuthModeRef.current = remote.credentials.authMode+const bridge = window.hermesDesktop?.connectionsconst hasLocal = Boolean(registry?.connections.some(c => c.kind === 'local'))diff --git a/apps/desktop/src/components/remote-setup/use-remote-oauth.ts b/apps/desktop/src/components/remote-setup/use-remote-oauth.tsindex 5721c266577..9364823f201 100644--- a/apps/desktop/src/components/remote-setup/use-remote-oauth.ts+++ b/apps/desktop/src/components/remote-setup/use-remote-oauth.ts@@ -1,6 +1,6 @@import { type RefObject, useRef, useState } from 'react'-import type { DesktopConnectionConfigInput } from '@/global'+import type { DesktopConnectionConfigInput, DesktopOauthLoginOptions } from '@/global'import { useI18n } from '@/i18n'import type { NotificationInput } from '@/store/notifications'@@ -19,11 +19,14 @@ interface RemoteOAuthOptions {/*** Registry-draft identity for a sign-in that runs BEFORE the draft is* saved. The main process derives the login window's cookie partition from- * the settled connection id; without it an unsaved draft's session lands in- * the legacy shared jar the saved connection never reads. Absent on the- * first-run/settings hosts, which have no draft identity.+ * the settled connection id, gated on the draft's kind/authMode — only a+ * cookie-auth remote draft gets its own jar; cloud and token drafts sign in+ * on the legacy shared jar the saved entry reads. Without the identity an+ * unsaved draft's session lands in the legacy shared jar the saved+ * connection never reads. Absent on the first-run/settings hosts, which+ * have no draft identity.*/- oauthLoginIdentity?: () => { connectionId: null | string; label: string } | undefined+ oauthLoginIdentity?: () => DesktopOauthLoginOptions | undefined/** Reports the settled id a pre-save sign-in wrote the session for. */onOAuthLoginSettled?: (connectionId: string) => void}@@ -89,6 +92,7 @@ export function useRemoteOAuth(options: RemoteOAuthOptions): RemoteOAuth {// Absent identity (first-run/settings hosts) keeps the legacy single-arg// call; the registry host always supplies one for its draft.const identity = oauthLoginIdentity?.()+const result = identity? await window.hermesDesktop.oauthLoginConnectionConfig(url, identity): await window.hermesDesktop.oauthLoginConnectionConfig(url)diff --git a/apps/desktop/src/components/remote-setup/use-remote-setup.ts b/apps/desktop/src/components/remote-setup/use-remote-setup.tsindex ad3e128aad7..a8262eb9f05 100644--- a/apps/desktop/src/components/remote-setup/use-remote-setup.ts+++ b/apps/desktop/src/components/remote-setup/use-remote-setup.ts@@ -1,6 +1,10 @@import { useRef, useState } from 'react'-import type { DesktopConnectionConfigInput, DesktopConnectionProbeResult } from '@/global'+import type {+ DesktopConnectionConfigInput,+ DesktopConnectionProbeResult,+ DesktopOauthLoginOptions+} from '@/global'import { useI18n } from '@/i18n'import { deriveRemoteAuthProviderShape } from '@/lib/desktop-remote-auth'import { coerceRemoteUrlScheme } from '@/lib/remote-url'@@ -28,10 +32,11 @@ export interface RemoteSetupOptions {enabled?: booleanbeforeOAuthLogin?: (payload: DesktopConnectionConfigInput) => Promise<void>/**- * Registry-draft identity for a sign-in that runs before the draft is- * saved; see useRemoteOAuth. Only the registry host supplies it.+ * Registry-draft identity (id, label, kind, authMode) for a sign-in that+ * runs before the draft is saved; see useRemoteOAuth. Only the registry+ * host supplies it.*/- oauthLoginIdentity?: () => { connectionId: null | string; label: string }+ oauthLoginIdentity?: () => DesktopOauthLoginOptions/** Reports the settled connection id a pre-save sign-in wrote the session for. */onOAuthLoginSettled?: (connectionId: string) => voidonNotice?: (notice: NotificationInput) => void@@ -145,7 +150,7 @@ export function useRemoteSetup(options: RemoteSetupOptions): RemoteSetup {targetSeq,beforeOAuthLogin: (value: DesktopConnectionConfigInput): Promise<void> | undefined =>callbacks.current.beforeOAuthLogin?.(value),- oauthLoginIdentity: (): { connectionId: null | string; label: string } | undefined =>+ oauthLoginIdentity: (): DesktopOauthLoginOptions | undefined =>callbacks.current.oauthLoginIdentity?.(),onOAuthLoginSettled: (connectionId: string): void => {callbacks.current.onOAuthLoginSettled?.(connectionId)diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.tsindex 377d48da6e8..7011da7f357 100644--- a/apps/desktop/src/global.d.ts+++ b/apps/desktop/src/global.d.ts@@ -1252,6 +1252,15 @@ export interface DesktopOauthLoginOptions {connectionId?: null | string/** Draft label — used to mint the id when `connectionId` is absent. */label?: string+ /**+ * Draft entry kind — the kind the save will persist. Together with+ * `authMode` it gates the pre-save cookie jar: only a cookie-auth remote+ * draft gets its own jar; cloud and token drafts sign in on the legacy+ * shared jar, which is what they read after the save.+ */+ kind?: DesktopConnectionKind+ /** Draft auth mode ('oauth' | 'token') the save will persist. */+ authMode?: 'oauth' | 'token'}export interface DesktopOauthLoginResult {