The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
Merge remote-tracking branch 'origin/main' into fix/106742-thread-resolution
teknium1e94542998359
diff --cc agent/agent_init.pyindex 4f574fedb1c,9b18a3d9b76..f52a5120c73--- a/agent/agent_init.py+++ b/agent/agent_init.py@@@ -2360,14 -2352,8 +2361,15 @@@ def init_agentcheckpoint_max_file_size_mb: int = 10, pass_session_id: bool = False,requested_provider: str = None, capabilities: Optional[Dict[str, bool]] = None, cwd: Optional[str] = None,side_agent: bool = False, memory_manager=None,+ tool_result_metadata_callback: Optional[Callable[..., dict]] = None,):+ from agent.safe_worker_policy import safe_worker_enabled++ if safe_worker_enabled():+ skip_context_files = skip_memory = skip_background_review = True+ load_soul_identity = False+ prefill_messages = []+ fallback_model = {}_install_safe_stdio()_params = locals()diff --cc apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsxindex 00000000000,9857fbc9b77..c6cf51bb084mode 000000,100644..100644--- a/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx+++ b/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx@@@ -1,0 -1,400 +1,402 @@@++import type { HermesGateway } from '@/api/client'+ import { type GatewayEvent, JsonRpcGatewayClient } from '@hermes/shared'+ import { QueryClient } from '@tanstack/react-query'+ import { act, cleanup, renderHook } from '@testing-library/react'+ import { useRef } from 'react'+ import { afterEach, beforeEach, expect, it, vi } from 'vitest'++ import { reconcileActiveTranscript } from '@/app/contrib/hooks/use-background-sync'+ import { getLatestSessionMessages } from '@/hermes'+ import { chatMessageText, toChatMessages } from '@/lib/chat-messages'+ import { resetInFlightTurnJournalStateForTests } from '@/lib/inflight-turn-journal'+ import { setPrimaryGateway } from '@/store/gateway'+ import { $activeGatewayProfile } from '@/store/profile'+ import {+ $messages,+ _resetSessionOwnerHintsForTests,+ setActiveSessionId,+ setAwaitingResponse,+ setBusy,+ setConnection,+ setMessages,+ setSelectedStoredSessionId,+ setSessions+ } from '@/store/session'+ import { clearAllSessionStates } from '@/store/session-states'+ import type { SessionMessage, SessionResumeResult } from '@/types/hermes'++ import { useMessageStream } from './use-message-stream'+ import { useSessionActions } from './use-session-actions'+ import { useSessionStateCache } from './use-session-state-cache'++ vi.mock('@/hermes', async original => ({+ ...(await original<Record<string, unknown>>()),+ getLatestSessionMessages: vi.fn()+ }))+ vi.mock('@/store/profile', async original => ({+ ...(await original<Record<string, unknown>>()),+ ensureGatewayProfile: vi.fn().mockResolvedValue(undefined)+ }))++ const storedId = 'warm-replay-stored'+ const runtimeId = 'warm-replay-runtime'+ const noop = async () => undefined+ const user: SessionMessage = { id: 1, role: 'user', content: 'Review example', timestamp: 1 }++ class Socket extends EventTarget {+ readyState = 0+ sent: { id: string; method: string; params: Record<string, unknown> }[] = []+ send(data: string) {+ this.sent.push(JSON.parse(data))+ }+ close() {+ this.readyState = 3+ this.dispatchEvent(new CloseEvent('close'))+ }+ open() {+ this.readyState = 1+ this.dispatchEvent(new Event('open'))+ }+ frame(frame: unknown) {+ this.dispatchEvent(new MessageEvent('message', { data: JSON.stringify(frame) }))+ }+ event(event: GatewayEvent) {+ this.frame({ jsonrpc: '2.0', method: 'event', params: event })+ }+ }++ function event(seq: number, type: GatewayEvent['type'], payload: Record<string, unknown> = {}): GatewayEvent {+ return { session_id: runtimeId, seq, type, payload: { timestamp: seq, ...payload } } as GatewayEvent+ }++ const replay = [+ event(2, 'message.start'),+ event(3, 'message.delta', { text: 'Finished result.' }),+ event(4, 'message.complete', { text: 'Finished result.' }),+ event(5, 'session.info', { running: false })+ ]++ const snapshot: SessionResumeResult = {+ session_id: runtimeId,+ resumed: storedId,+ messages: [],+ message_count: 0,+ running: false+ }++ async function mountWithPendingReplay() {+ const requestGateway = vi.fn().mockResolvedValue(snapshot)++ const hook = renderHook(() => {+ const busyRef = useRef(false)+ const creatingSessionRef = useRef(false)+ const queryClient = useRef(new QueryClient()).current++ const cache = useSessionStateCache({+ activeSessionId: null,+ selectedStoredSessionId: null,+ busyRef,+ setMessages,+ setBusy,+ setAwaitingResponse+ })++ const actions = useSessionActions({+ ...cache,+ activeSessionId: null,+ selectedStoredSessionId: null,+ busyRef,+ creatingSessionRef,+ getRouteToken: () => 'A',+ getRoutedStoredSessionId: () => null,+ navigate: vi.fn(),+ requestGateway+ })++ const stream = useMessageStream({+ ...cache,+ queryClient,+ hydrateFromStoredSession: noop,+ refreshHermesConfig: noop,+ refreshSessions: noop+ })++ return { cache, actions, stream }+ })++ const sockets: Socket[] = []++ const client = new JsonRpcGatewayClient({+ heartbeatIntervalMs: 0,+ heartbeatDeadlineMs: 0,+ socketFactory: () => {+ const socket = new Socket()+ sockets.push(socket)++ return socket as unknown as WebSocket+ }+ })+- setPrimaryGateway(client)++ // The store types the primary as the desktop subclass; the barrier under test lives on the shared base.++ setPrimaryGateway(client as unknown as HermesGateway)+ client.onEvent(gatewayEvent => hook.result.current.stream.handleGatewayEvent(gatewayEvent))++ const connect = async () => {+ const promise = client.connect('ws://fixture.test')+ const socket = sockets.at(-1)!+ socket.open()+ await promise++ return socket+ }++ // Warm cache: the runtime is already known and has seen seq 1.+ act(() => {+ hook.result.current.cache.updateSessionState(+ runtimeId,+ state => ({ ...state, messages: toChatMessages([user]) }),+ storedId+ )+ })+ const first = await connect()+ act(() => first.event(event(1, 'session.info', { running: false })))+ client.invalidate()+ const second = await connect()+ const request = second.sent.find(item => item.method === 'session.events.since')!+ expect(request.params).toMatchObject({ session_id: runtimeId, last_seen: 1 })++ return { ...hook, client, connect, requestGateway, second, request }+ }++ beforeEach(() => {+ localStorage.clear()+ clearAllSessionStates()+ resetInFlightTurnJournalStateForTests()+ _resetSessionOwnerHintsForTests()+ $activeGatewayProfile.set('default')+ setConnection(null)+ setMessages([])+ setActiveSessionId(null)+ setSelectedStoredSessionId(null)+ setBusy(false)+ setAwaitingResponse(false)+ setSessions([+ {+ id: storedId,+ title: storedId,+ source: 'desktop',+ message_count: 3,+ tool_call_count: 0,+ is_active: true,+ started_at: 1,+ last_active: 1,+ ended_at: null,+ model: null,+ preview: null,+ input_tokens: 0,+ output_tokens: 0+ }+ ])+ vi.mocked(getLatestSessionMessages).mockReset()+ vi.mocked(getLatestSessionMessages).mockResolvedValue({+ session_id: storedId,+ messages: [user, { id: 2, role: 'assistant', content: 'Finished result.', timestamp: 2 }]+ })+ })++ afterEach(() => {+ cleanup()+ clearAllSessionStates()+ resetInFlightTurnJournalStateForTests()+ setPrimaryGateway(null)+ localStorage.clear()+ setSessions([])+ setMessages([])+ setActiveSessionId(null)+ setSelectedStoredSessionId(null)+ setBusy(false)+ setAwaitingResponse(false)+ vi.restoreAllMocks()+ })++ const activateCalls = (requestGateway: ReturnType<typeof vi.fn>) =>+ requestGateway.mock.calls.filter(([method]) => method === 'session.activate')++ it('waits for reconnect replay before activating and reading warm history', async () => {+ const { result, client, requestGateway, second, request } = await mountWithPendingReplay()++ try {+ let pending!: Promise<void>+ await act(async () => {+ pending = result.current.actions.resumeSession(storedId, true)+ })+ // The completed turn is already durable; neither the activation snapshot+ // nor REST history may publish it ahead of the replay that carries it.+ expect(activateCalls(requestGateway)).toHaveLength(0)+ expect(getLatestSessionMessages).not.toHaveBeenCalled()++ await act(async () => {+ second.frame({ id: request.id, jsonrpc: '2.0', result: { events: replay } })+ await pending+ })++ expect(activateCalls(requestGateway)).toHaveLength(1)+ const rows = result.current.cache.sessionStateByRuntimeIdRef.current.get(runtimeId)!.messages+ const text = rows.map(chatMessageText).join('\n')+ expect(text).toContain('Review example')+ expect(text.match(/Finished result\./g)).toHaveLength(1)+ } finally {+ client.close()+ }+ })++ it('still settles a warm resume through session.activate when its replay socket is lost', async () => {+ const { result, client, requestGateway } = await mountWithPendingReplay()++ requestGateway.mockImplementation(async (method: string) => {+ if (method === 'session.activate') {+ throw new Error('session not found')+ }++ return snapshot+ })++ try {+ let pending!: Promise<void>+ await act(async () => {+ pending = result.current.actions.resumeSession(storedId, true)+ })+ expect(activateCalls(requestGateway)).toHaveLength(0)++ await act(async () => {+ client.invalidate()+ await pending+ })++ // A lost socket is not proof the runtime is alive or gone: activation+ // decides, and a gone runtime falls back to a cold resume instead of+ // leaving the view on an unrebound warm cache.+ expect(activateCalls(requestGateway)).toHaveLength(1)+ expect(requestGateway.mock.calls.some(([method]) => method === 'session.resume')).toBe(true)+ } finally {+ client.close()+ }+ })++ it('holds a reconnect re-resume of the selected session behind its replay (cold path)', async () => {+ const { result, client, requestGateway, second, request } = await mountWithPendingReplay()+ const { activeSessionIdRef, runtimeIdByStoredSessionIdRef, selectedStoredSessionIdRef } = result.current.cache++ // The view is streaming this runtime, but the warm mapping is gone, so the+ // route's reconnect re-resume takes the cold REST + session.resume path.+ selectedStoredSessionIdRef.current = storedId+ activeSessionIdRef.current = runtimeId+ runtimeIdByStoredSessionIdRef.current.delete(storedId)++ try {+ let pending!: Promise<void>+ await act(async () => {+ pending = result.current.actions.resumeSession(storedId, true)+ })+ expect(getLatestSessionMessages).not.toHaveBeenCalled()+ expect(requestGateway).not.toHaveBeenCalled()++ await act(async () => {+ second.frame({ id: request.id, jsonrpc: '2.0', result: { events: replay } })+ await pending+ })++ expect(getLatestSessionMessages).toHaveBeenCalledTimes(1)+ expect(requestGateway.mock.calls.some(([method]) => method === 'session.resume')).toBe(true)+ expect(+ $messages+ .get()+ .map(chatMessageText)+ .join('\n')+ .match(/Finished result\./g)+ ).toHaveLength(1)+ } finally {+ client.close()+ }+ })++ it('does not paint a cold re-resume read ahead of a replay redialed while REST was in flight', async () => {+ const { result, client, connect } = await mountWithPendingReplay()+ const { activeSessionIdRef, runtimeIdByStoredSessionIdRef, selectedStoredSessionIdRef } = result.current.cache+ let releaseRest!: () => void+ const restGate = new Promise<void>(resolve => (releaseRest = resolve))+ const latest = vi.mocked(getLatestSessionMessages).getMockImplementation()!+ vi.mocked(getLatestSessionMessages).mockImplementation(async (...args) => restGate.then(() => latest(...args)))+ selectedStoredSessionIdRef.current = storedId+ activeSessionIdRef.current = runtimeId+ runtimeIdByStoredSessionIdRef.current.delete(storedId)++ try {+ let pending!: Promise<void>+ await act(async () => {+ pending = result.current.actions.resumeSession(storedId, true)+ })+ // The replay socket drops (barrier false: cold resume starts its REST read)+ // and redials before that read returns, owning a fresh replay of the turn.+ let third!: Awaited<ReturnType<typeof connect>>+ await act(async () => {+ client.invalidate()+ third = await connect()+ })+ expect(getLatestSessionMessages).toHaveBeenCalledTimes(1)+ const request = third.sent.find(item => item.method === 'session.events.since')!++ await act(async () => {+ releaseRest()+ await new Promise(resolve => setTimeout(resolve, 0))+ third.frame({ id: request.id, jsonrpc: '2.0', result: { events: replay } })+ await pending+ })++ expect(+ $messages+ .get()+ .map(chatMessageText)+ .join('\n')+ .match(/Finished result\./g)+ ).toHaveLength(1)+ } finally {+ client.close()+ }+ })++ it('holds a background active-transcript refresh behind the reconnect replay', async () => {+ const { result, client, second, request } = await mountWithPendingReplay()++ const { activeSessionIdRef, selectedStoredSessionIdRef, sessionStateByRuntimeIdRef, updateSessionState } =+ result.current.cache++ selectedStoredSessionIdRef.current = storedId+ activeSessionIdRef.current = runtimeId++ try {+ const refresh = reconcileActiveTranscript({+ activeSessionIdRef,+ busyRef: { current: false },+ requestSequenceRef: { current: 0 },+ resolveSession: () => ({ profile: 'default' }),+ selectedStoredSessionIdRef,+ signatureRef: { current: new Map() },+ updateSessionState+ })++ await new Promise(resolve => setTimeout(resolve, 0))+ expect(getLatestSessionMessages).not.toHaveBeenCalled()++ await act(async () => {+ second.frame({ id: request.id, jsonrpc: '2.0', result: { events: replay } })+ await refresh+ })++ expect(getLatestSessionMessages).toHaveBeenCalledTimes(1)+ const text = sessionStateByRuntimeIdRef.current.get(runtimeId)!.messages.map(chatMessageText).join('\n')+ expect(text.match(/Finished result\./g)).toHaveLength(1)+ } finally {+ client.close()+ }+ })diff --cc apps/desktop/src/app/settings/config-settings.tsxindex 013ac9d226c,085e6a21186..ee716b85d08--- a/apps/desktop/src/app/settings/config-settings.tsx+++ b/apps/desktop/src/app/settings/config-settings.tsx@@@ -446,6 -455,13 +454,12 @@@ function ConfigSettingsInner(label={c.disableF12Title}onChange={setDisableF12}/>+ <ToggleRow+ checked={alwaysExternalLinks}+ description={c.alwaysExternalLinksDesc}+ label={c.alwaysExternalLinksTitle}+ onChange={setAlwaysExternalLinks}+ />- <PoolLimitsSetting /><QuickEntrySettings /></>)}diff --cc apps/desktop/src/sdk/index.test.tsindex b6d9127b0bd,30c751a1938..c72c2d99eee--- a/apps/desktop/src/sdk/index.test.ts+++ b/apps/desktop/src/sdk/index.test.ts@@@ -1,17 -1,20 +1,18 @@@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+ import { ackComposerInsert } from '@/app/chat/composer/focus'import { createClientSessionState } from '@/lib/chat-runtime'import { host } from '@/sdk'- import { setActiveSessionId, setAwaitingResponse, setBusy } from '@/store/session'+ import { $selectedStoredSessionId, setActiveSessionId, setAwaitingResponse, setBusy } from '@/store/session'import { clearAllSessionStates, publishSessionState } from '@/store/session-states'-// The warm path must route through the guarded prewarm resolver, not dial the-// gateway directly: gateway.ts's openSecondaryCount and pool-limits' cap atom-// are the two signals prewarmProfileBackend consults, so mocking them lets the-// tests observe the guard's decision through the ONLY side effect that matters-// — whether openGatewayForProfile was dialed.+// The warm path must route through the guarded prewarm resolver (hover dwell,+// per-profile throttle), not dial the gateway directly: mocking the store's+// dialers lets the tests observe the ONLY side effect that matters — whether+// openGatewayForProfile was dialed.const warmMocks = vi.hoisted(() => ({openGatewayForAgent: vi.fn(async (_connectionId: null | string, _profile: string) => undefined),- openGatewayForProfile: vi.fn(async (_profile: string) => undefined),- openSecondaryCount: vi.fn(() => 0)+ openGatewayForProfile: vi.fn(async (_profile: string) => undefined)}))vi.mock('@/store/gateway', async importOriginal => ({diff --cc apps/shared/src/json-rpc-gateway.tsindex 6b6add7d4cb,c355f12c52b..bb02c5fa0db--- a/apps/shared/src/json-rpc-gateway.ts+++ b/apps/shared/src/json-rpc-gateway.ts@@@ -132,7 -138,6 +138,8 @@@ export class JsonRpcGatewayClient* silently believe nothing was missed.*/private replayEpoch: string | null = null++ /** Canonical authorities number events per session; a session's own epoch outranks the process one. */+ private replayEpochBySession = new Map<string, string>()private readonly stateHandlers = new Set<(state: ConnectionState) => void>()private readonly options: Required<Omit<GatewayClientOptions, 'onRequestHandlerError' | 'onUnhandledRequest' | 'socketFactory'>@@@ -424,6 -434,6 +436,8 @@@return}++ // Parked frames adopt their epoch when flushed, after the replayed gap.++ if (sid) { this.adoptSessionReplayEpoch(sid, event.replay_epoch) }this.recordSeq(event)this.dispatchEvent(event)}@@@ -479,21 -516,23 +520,25 @@@this.replayHold = hold+ // A hung background session must not hold a ready session's transcript.+ for (const [sid, lastSeen] of entries) {+ void this.fetchSessionReplay(sid, lastSeen, replayGeneration)+ }+ }++ private async fetchSessionReplay(sid: string, lastSeen: number, replayGeneration: number): Promise<void> {+ if (this.replayGeneration !== replayGeneration) {+ return+ }+try {- const entries = Object.entries(this.getSeqWatermarks())-- // One RPC per known session keeps params flat; sessions are few (<20).- const results = await Promise.allSettled(- entries.map(([sid, lastSeen]) => {- const epoch = this.replayEpochBySession.get(sid) ?? this.replayEpoch-- // `open_requests` on the answer are re-delivered by the channel itself.- return this.request<{ events?: Array<{ type: string; session_id?: string; seq?: number; payload?: unknown }> }>(- 'session.events.since',- { session_id: sid, last_seen: lastSeen, ...(epoch ? { replay_epoch: epoch } : {}) },- REPLAY_REQUEST_TIMEOUT_MS- )- })++ const sessionEpochBefore = this.replayEpochBySession.get(sid) ?? this.replayEpoch+++ // `open_requests` on the answer are re-delivered by the channel itself.- const result = await this.request<{ events?: GatewayEvent[]; epoch?: string }>(++ const result = await this.request<{ events?: GatewayEvent[]; epoch?: string; replay_epoch?: unknown; truncated?: unknown; snapshot_required?: unknown; latest_seq?: unknown }>(+ 'session.events.since',- { session_id: sid, last_seen: lastSeen },++ { session_id: sid, last_seen: lastSeen, ...(sessionEpochBefore ? { replay_epoch: sessionEpochBefore } : {}) },+ REPLAY_REQUEST_TIMEOUT_MS)// The socket that owned this replay was dropped while its requests were@@@ -503,49 -542,31 +548,46 @@@return}- for (const [index, result] of results.entries()) {- if (result.status !== 'fulfilled' || !Array.isArray(result.value?.events)) {- continue- }+ const epoch = result?.epoch++ const sessionEpoch = result?.replay_epoch- const response = result.value as { epoch?: unknown; replay_epoch?: unknown; truncated?: unknown; snapshot_required?: unknown; latest_seq?: unknown }- const sessionEpoch = response.replay_epoch- const epoch = response.epoch- const sid = entries[index][0]-- if (response.snapshot_required === true || response.truncated === true) {- // The server could not replay our window (epoch changed, ring- // truncated, cursor ahead). Keeping the watermark would make the- // next reconnect believe nothing was missed; drop it and tell the- // consumer to re-resume for a snapshot.- this.adoptSessionReplayEpoch(sid, sessionEpoch)- this.lastSeenSeq.delete(sid)- this.dispatchEvent({ type: 'session.replay_gap', session_id: sid, payload: { replay_epoch: sessionEpoch, latest_seq: response.latest_seq } })-- continue- }- if (typeof epoch === 'string' && epoch && this.replayEpoch && epoch !== this.replayEpoch) {- // The old cursor no longer describes this process's numbering.- this.adoptReplayEpoch(epoch)++ if (result?.snapshot_required === true || result?.truncated === true) {++ // The server could not replay our window (epoch changed, ring++ // truncated, cursor ahead). Keeping the watermark would make the++ // next reconnect believe nothing was missed; drop it and tell the++ // consumer to re-resume for a snapshot.++ this.adoptSessionReplayEpoch(sid, sessionEpoch)++ this.lastSeenSeq.delete(sid)++ this.dispatchEvent({ type: 'session.replay_gap', session_id: sid, payload: { replay_epoch: sessionEpoch, latest_seq: result.latest_seq } })- if (typeof sessionEpoch === 'string' && sessionEpoch) {- // Canonical responses also include `epoch`, but it is session-local,- // not the legacy process identity. Never reset unrelated cursors.- if (this.adoptSessionReplayEpoch(sid, sessionEpoch)) { continue }- } else if (typeof epoch === 'string' && epoch && this.replayEpoch && epoch !== this.replayEpoch) {- // Backend restarted: its seq numbering reset, so our watermarks —- // and this replay window — are meaningless. Drop them and start- // fresh under the new epoch.- this.adoptReplayEpoch(epoch)-- continue- } else if (typeof epoch === 'string' && epoch && !this.replayEpoch) {- this.replayEpoch = epoch- }+ return+ }- for (const event of result.value.events) {- if (!event?.type) {- continue- }- if (typeof epoch === 'string' && epoch && !this.replayEpoch) {++ if (typeof sessionEpoch === 'string' && sessionEpoch) {++ // Canonical responses also include `epoch`, but it is session-local,++ // not the legacy process identity. Never reset unrelated cursors.++ if (this.adoptSessionReplayEpoch(sid, sessionEpoch)) { return }++ } else if (typeof epoch === 'string' && epoch && this.replayEpoch && epoch !== this.replayEpoch) {++ // The old cursor no longer describes this process's numbering.++ this.adoptReplayEpoch(epoch)++++ return++ } else if (typeof epoch === 'string' && epoch && !this.replayEpoch) {+ this.replayEpoch = epoch+ }- this.dispatchIfNewer({ ...event, replayed: true } as GatewayEvent)+ if (!Array.isArray(result?.events)) {+ return+ }++ for (const event of result.events) {+ // Event handlers can synchronously invalidate and replace the socket.+ if (this.replayGeneration !== replayGeneration) {+ return+ }++ if (event?.type) {+ this.dispatchIfNewer({ ...event, replayed: true })}}} catch {@@@ -592,41 -609,69 +633,84 @@@return}- if (this.replayEpoch !== null) {+ const changed = this.replayEpoch !== null+ this.replayEpoch = epoch++ if (changed) {- this.lastSeenSeq.clear()++ // Sessions numbered by their own authority epoch keep their cursors.+ for (const sid of this.lastSeenSeq.keys()) {+ if (!this.replayEpochBySession.has(sid)) { this.lastSeenSeq.delete(sid) }+ }+ // Revoke requests/cursors from the old numbering, but retain live+ // frames already received on this still-open socket. The socket is+ // still ours and no replay can cover the old numbering, so waiting+ // history reads proceed: REST is the only recovery left (#94779).+ // Their continuations run after the parked frames below dispatch.+ const hold = this.cancelReplay(true)+ const generation = this.replayGeneration++ for (const replay of hold?.values() ?? []) {+ for (const event of replay.events) {+ if (this.replayGeneration !== generation) {+ return+ }++ this.dispatchIfNewer({ ...event, replayed: true })+ }+ }}+ }- this.replayEpoch = epoch+ /** Release frames parked during a replay fetch, seq-gated against dupes. */+ private flushReplayHold(sid: string, generation: number): void {+ const replay = this.replayHold?.get(sid)++ if (!replay) {+ return+ }++ // Keep the barrier visible through dispatch, including synchronous live+ // frames emitted by a handler. Remove consumed frames before callbacks+ // can revoke this epoch and flush the remainder.+ while (this.replayGeneration === generation && replay.events.length) {+ this.dispatchIfNewer({ ...replay.events.shift()!, replayed: true })+ }++ if (this.replayGeneration !== generation) {+ return+ }++ this.replayHold?.delete(sid)++ if (this.replayHold?.size === 0) {+ this.replayHold = null+ }++ replay.resolve(true)}+ /** Returns true when an established session numbering has changed. */+ private adoptSessionReplayEpoch(sid: string, epoch: unknown): boolean {+ if (typeof epoch !== 'string' || !epoch) { return false }+ const previous = this.replayEpochBySession.get(sid)+ const changed = previous !== undefined && previous !== epoch++ if (changed) { this.lastSeenSeq.delete(sid) }+ this.replayEpochBySession.set(sid, epoch)++ return changed+ }+- /** Release frames parked during a replay fetch, seq-gated against dupes. */- private flushReplayHold(): void {+ private cancelReplay(readsMayProceed: boolean): Map<string, SessionReplay> | null {const hold = this.replayHold+ this.replayGeneration += 1this.replayHold = null- if (!hold) {- return+ for (const replay of hold?.values() ?? []) {+ replay.resolve(readsMayProceed)}- for (const parked of hold.values()) {- for (const event of parked) {- this.dispatchIfNewer({ ...event, replayed: true })- }- }+ return hold}/** Forget the current socket generation, fail its calls, and go 'closed'. */diff --cc tui_gateway/agent_factory.pyindex bc187a491c7,00000000000..a54ce901c29mode 100644,000000..100644--- a/tui_gateway/agent_factory.py+++ b/tui_gateway/agent_factory.py@@@ -1,633 -1,0 +1,635 @@@+"""TUI agent_factory seam; functions bind to the server namespace at registration."""++from __future__ import annotations++from .method_ctx import bind_module++def _env_model_seed() -> str:+ """The launch-scoped model seed (``hermes --tui -m``, hosted provisioning); "" when unset."""+ return (os.environ.get("HERMES_MODEL", "") or os.environ.get("HERMES_INFERENCE_MODEL", "")).strip()+++def _resolve_model() -> str:+ if env := _env_model_seed():+ return env+ m = _load_cfg().get("model", "")+ if isinstance(m, dict):+ return str(m.get("default", "") or "").strip()+ if isinstance(m, str) and m:+ return m.strip()+ # No env seed / config preference: the cost-safe silent default (cache-only read), never an unpicked flagship.+ with contextlib.suppress(Exception):+ from hermes_cli.models import get_preferred_silent_default_model+ return get_preferred_silent_default_model()+ return "z-ai/glm-5.2"+++def _resolve_session_platform() -> str:+ """``HERMES_DESKTOP=1`` without ``HERMES_DESKTOP_TERMINAL`` → "desktop" (chat panel; the agent then+ suggests TUI-only slash commands), else "tui" (embedded terminal pane or standalone ``hermes --tui``)."""+ desktop = is_truthy_value(os.environ.get("HERMES_DESKTOP"))+ return "desktop" if desktop and not is_truthy_value(os.environ.get("HERMES_DESKTOP_TERMINAL")) else "tui"+++def _resolve_session_source(explicit: str | None) -> str:+ """Session DB ``source``: an explicit caller value (plugin session tagged ``"telegram"``) is never+ rewritten; only empty/None falls back to the env-resolved platform."""+ return explicit or _resolve_session_platform()+++def _resolve_agent_platform(source: str | None) -> str:+ return _resolve_session_source(source)+++def _config_model_target() -> tuple[str, str]:+ """(model, provider) selected by config.yaml — and ONLY config: the HERMES_MODEL launch seed fed into+ the per-turn sync would be replayed as a /model switch and persisted globally, or pin the session so+ dashboard/CLI model changes never reach an open chat. Empty model = "no preference" → no-op sync."""+ cfg_model = _load_cfg().get("model")+ if isinstance(cfg_model, dict):+ provider = str(cfg_model.get("provider") or "").strip()+ return str(cfg_model.get("default", "") or "").strip(), "" if provider.lower() == "auto" else provider+ return (cfg_model.strip() if isinstance(cfg_model, str) else ""), ""+++def _resolve_startup_runtime() -> tuple[str, str | None]:+ model = _resolve_model()+ if explicit_provider := os.environ.get("HERMES_TUI_PROVIDER", "").strip():+ return model, explicit_provider+ if not (explicit_model := _env_model_seed()):+ return model, None+ with contextlib.suppress(Exception):+ from hermes_cli.model_switch import resolve_startup_model_route+ from hermes_cli.models import detect_static_provider_for_model+ full_cfg = _load_cfg()+ cfg = full_cfg.get("model") or {}+ current_provider = ((str(cfg.get("provider") or "").strip().lower() if isinstance(cfg, dict) else "")+ or os.environ.get("HERMES_INFERENCE_PROVIDER", "").strip().lower() or "auto")+ # Same owner as HermesCLI/oneshot: ``custom:<name>:<model>`` selects that provider (#73943).+ if route := resolve_startup_model_route(+ explicit_model, current_provider=current_provider,+ user_providers=full_cfg.get("providers"), custom_providers=full_cfg.get("custom_providers")):+ return route.model, route.provider+ if detected := detect_static_provider_for_model(explicit_model, current_provider):+ provider, detected_model = detected+ return detected_model, provider+ return model, None+++# Bare billing buckets are not routable provider identities; restoring one as a session provider override+# breaks resume. ``openrouter`` is deliberately NOT in this set (fully routable; agent_init's gate is a different set).+# (agent_init's fail-fast gate is a DIFFERENT set that also skips "openrouter" — there it means "default+# route, don't fail fast", not "unroutable".) ``openrouter`` is deliberately excluded here — it is a fully+# routable provider with its own API key and base_url. Sessions that used OpenRouter store+# ``billing_provider="openrouter"``; dropping it forces resume to the current global model (e.g. a custom+# endpoint), which is the wrong provider for the stored model. See #57588.+++def _is_routable_provider(provider: str) -> bool:+ with contextlib.suppress(Exception):+ from hermes_cli.runtime_provider import is_routable_provider+ return is_routable_provider(provider)+ return False+++def _overrides_have_routable_provider(overrides: dict) -> bool:+ """Whether persisted runtime overrides still name a routable provider (renamed/removed → "Unknown+ provider" at agent init). Empty = NOT routable, so the caller falls back to the session's picked model."""+ provider = str(overrides.get("provider_override") or "").strip()+ if not provider:+ provider = str((overrides.get("model_override") or {}).get("provider") or "").strip()+ return bool(provider) and _is_routable_provider(provider)+++def _parse_model_config(raw, *, quiet: bool = False) -> dict:+ """A row's ``model_config`` (dict or JSON text) as a dict; ``{}`` when absent/invalid."""+ if isinstance(raw, dict):+ return raw+ if isinstance(raw, str) and raw.strip():+ try:+ parsed = json.loads(raw)+ return parsed if isinstance(parsed, dict) else {}+ except Exception:+ if not quiet:+ raise+ logger.debug("failed to parse stored session model_config", exc_info=True)+ return {}+++def _row_follows_profile(row: dict | None) -> bool:+ """Whether a stored row is a canonical Bot Chat whose runtime follows the member profile's config.+ Identity is the persisted ``follow_profile_config`` marker; the bare title compare stays ONLY here as+ the legacy fallback for rows written before the marker existed."""+ if not row:+ return False+ model_config = _parse_model_config(row.get("model_config"), quiet=True)+ return bool(model_config.get("follow_profile_config") or str(row.get("title") or "").strip() == "Bot Chat")+++def _stored_session_runtime_overrides(row: dict | None) -> dict:+ """Runtime fields persisted with a stored session (model column, ``billing_provider``, JSON ``model_config``):+ resume restores the model/provider/reasoning THAT chat used, not the global pick. Plugin-owned Bot-Mode+ sessions normally rebuild from the member profile's CURRENT config (a stale provider pin left room bots+ "out of Nous credits" after a profile switch). A canonical Bot Chat may instead restore an explicit+ composer pick while the profile model it diverged from remains unchanged."""+ if not row:+ return {}+ model_config = _parse_model_config(row.get("model_config"), quiet=True)+ _row_title = str(row.get("title") or "").strip()+ room_plumbing = model_config.get("room_plumbing") or (row.get("hidden") and _row_title.startswith("Group:"))+ composer_profile = model_config.get("composer_override_profile")+ composer_profile_matches = isinstance(composer_profile, dict) and (+ str(composer_profile.get("model") or "").strip(),+ str(composer_profile.get("provider") or "").strip(),+ ) == _config_model_target()+ if room_plumbing or (_row_follows_profile(row) and not composer_profile_matches):+ return {}+ overrides: dict = {}+ field = lambda k: str(model_config.get(k) or "").strip()+ model = str(row.get("model") or model_config.get("model") or "").strip()+ # ``billing_provider`` is only the billing bucket — for a custom endpoint the bare class "custom", which+ # agent_init treats as non-routable. Only restore an explicit provider; else resume uses the configured default.+ provider = field("provider")+ billing_provider = str(model_config.get("billing_provider") or row.get("billing_provider") or "").strip()+ if not provider and billing_provider.lower() not in _BARE_BILLING_PROVIDERS:+ provider = billing_provider+ base_url, api_mode, service_tier = field("base_url"), field("api_mode"), field("service_tier")+ reasoning_config = model_config.get("reasoning_config")+ from hermes_cli.runtime_provider import is_foreign_provider_endpoint+ if is_foreign_provider_endpoint(provider, base_url):+ # The endpoint and its wire belong to the provider this chat left; resolve the stored one's own.+ base_url = api_mode = ""+ # Heal a stale provider persisted by an older build (renamed/removed custom provider → "Unknown provider"):+ # recover ``custom:<name>`` from the stored base_url, then from the entry serving the model; else drop it.+ if provider and not _is_routable_provider(provider):+ healed = None+ try:+ from hermes_cli.runtime_provider import canonical_custom_identity+ healed = canonical_custom_identity(base_url=base_url or None, model=model or None)+ except Exception:+ logger.debug("custom provider identity recovery failed", exc_info=True)+ if healed:+ logger.info("healed stale session provider %r to %r", provider, healed)+ provider = healed+ base_url = "" # the healed identity owns a registered endpoint; the snapshot URL must not override it+ else:+ provider = ""+ if model:+ # Same dict-shaped override live /model switches use, so a DB-restored session keeps custom endpoint+ # metadata across resume and rebuilds (/new). Raw api_key is never persisted/restored.+ overrides["model_override"] = {+ "model": model, "provider": provider or None, "base_url": base_url or None, "api_mode": api_mode or None}+ if provider:+ overrides["provider_override"] = provider+ if isinstance(reasoning_config, dict):+ overrides["reasoning_config_override"] = reasoning_config+ if service_tier: # None = "inherit the profile" at _make_agent; "" = real override "no priority tier"+ overrides["service_tier_override"] = "" if service_tier.lower() == "normal" else service_tier+ return overrides+++def _runtime_model_config(agent, existing: dict | None = None) -> dict:+ """Merge the agent's CURRENT runtime identity onto the row's persisted ``model_config``. Falsy agent+ attributes DELETE the key rather than skip the write: resume reads provider/endpoint from this JSON+ (model column written separately), so a stale provider would route the resumed chat to the wrong endpoint."""+ config = dict(existing or {})+ attr = lambda k: str(getattr(agent, k, "") or "").strip()+ model, provider, base_url = attr("model"), attr("provider"), attr("base_url")+ if provider.lower() == "custom":+ # ``agent.provider`` resolves every named custom entry to the literal "custom", losing the entry+ # identity (api_key is never persisted): recover ``custom:<name>`` from the endpoint URL.+ try:+ from hermes_cli.runtime_provider import canonical_custom_identity+ provider = canonical_custom_identity(base_url=base_url, model=model or None) or provider+ except Exception:+ logger.debug("custom provider identity lookup failed", exc_info=True)+ reasoning_config = getattr(agent, "reasoning_config", None)+ live = {+ "model": model, "provider": provider, "base_url": base_url, "api_mode": attr("api_mode"),+ # An empty dict is still a real (present) reasoning config.+ "reasoning_config": reasoning_config if isinstance(reasoning_config, dict) else None,+ "service_tier": getattr(agent, "service_tier", None),+ }+ for key, value in live.items():+ if value or isinstance(value, dict):+ config[key] = value+ else:+ config.pop(key, None)+ return config+++def _persist_live_session_runtime(session: dict | None) -> None:+ """Persist active session runtime so future resumes restore the same footer."""+ live = _live_session_agent_db(session)+ if live is None:+ return+ agent, session_key, db = live+ try:+ row = db.get_session(session_key) or {}+ model_config = _runtime_model_config(agent, _parse_model_config(row.get("model_config")))+ if isinstance(composer_profile := session.get("composer_override_profile"), dict):+ model_config["composer_override_profile"] = composer_profile+ elif "composer_override_profile" in session:+ model_config.pop("composer_override_profile", None)+ if (tier_override := session.get("create_service_tier_override")) is not None:+ # agent.service_tier is None for explicit normal; without this the distinction is erased on every persist.+ model_config["service_tier"] = tier_override or "normal"+ model = str(getattr(agent, "model", "") or "").strip()+ if hasattr(db, "update_session_meta"):+ db.update_session_meta(session_key, json.dumps(model_config), model or None)+ elif model and hasattr(db, "update_session_model"):+ db.update_session_model(session_key, model)+ except Exception:+ logger.debug("failed to persist live session runtime", exc_info=True)++def _load_approval_mode() -> str:+ """Effective ``approvals.mode`` via the gate's own ``_get_approval_mode`` (a raw re-read missed the+ managed overlay and ``${VAR}`` expansion)."""+ from tools.approval_context import _get_approval_mode+ mode = _get_approval_mode()+ return mode if mode in _APPROVAL_MODES else "manual"+++def _coerce_statusbar(raw) -> str:+ if raw is False:+ return "off"+ return s if isinstance(raw, str) and (s := raw.strip().lower()) in _STATUSBAR_MODES else "top"+++_MOUSE_TRACKING_ALIASES = {+ "0": "off", "1": "all", "all": "all", "any": "all", "button": "buttons", "buttons": "buttons",+ "click": "buttons", "false": "off", "full": "all", "no": "off", "off": "off", "on": "all",+ "scroll": "wheel", "true": "all", "wheel": "wheel", "yes": "all",+}+++def _display_mouse_tracking(display: dict) -> str:+ """display.mouse_tracking → ``off|wheel|buttons|all`` (bools: True → all, False → off); ``wheel`` (DEC+ 1000+1006) is the tmux-friendly subset without hover events. Legacy ``tui_mouse`` only when ``mouse_tracking`` is absent."""+ if not isinstance(display, dict):+ return "all"+ raw = display.get("mouse_tracking") if "mouse_tracking" in display else display.get("tui_mouse", True)+ if isinstance(raw, str):+ return _MOUSE_TRACKING_ALIASES.get(raw.strip().lower(), "all")+ return "off" if raw is False or raw == 0 else "all"+++def _load_reasoning_config(model: str = "") -> dict | None:+ """Via the shared chokepoint :func:`hermes_constants.resolve_reasoning_config` (per-model override >+ global ``agent.reasoning_effort``; YAML False = disabled).++ Closes #21256.+ """+ from hermes_constants import resolve_reasoning_config+ return resolve_reasoning_config(_load_cfg(), model)+++_SERVICE_TIER_ALIASES = {"fast": "priority", "priority": "priority", "on": "priority", "auto": "auto", "cold": "cold"}+++def _load_service_tier() -> str | None:+ raw = str((_load_cfg().get("agent") or {}).get("service_tier", "") or "").strip().lower()+ return _SERVICE_TIER_ALIASES.get(raw)+++def _load_provider_routing() -> dict:+ """OpenRouter ``provider_routing`` prefs (gateway/CLI parity — without them OpenRouter picks an effectively random provider)."""+ with contextlib.suppress(Exception):+ return _load_cfg().get("provider_routing", {}) or {}+ return {}+++def _load_show_reasoning() -> bool:+ # Fallback True — keep in sync with DEFAULT_CONFIG display.show_reasoning (no DEFAULT_CONFIG merge here).+ return bool(_display_cfg().get("show_reasoning", True))+++def _load_memory_notifications() -> str:+ """``display.memory_notifications`` (``off`` / ``on`` default / ``verbose``; bool normalized) — gates the+ "💾 Self-improvement review" summary (gateway/CLI parity)."""+ raw = _display_cfg().get("memory_notifications")+ if isinstance(raw, bool):+ return "on" if raw else "off"+ return str(raw).lower() if raw else "on"+++_TOOL_PROGRESS_MODES = frozenset({"off", "new", "all", "verbose"})+++def _load_tool_progress_mode() -> str:+ env = os.environ.get("HERMES_TUI_TOOL_PROGRESS", "").strip().lower()+ if env in _TOOL_PROGRESS_MODES:+ return env+ raw = _display_cfg().get("tool_progress", "all")+ if isinstance(raw, bool):+ return "all" if raw else "off"+ mode = str(raw or "all").strip().lower()+ return mode if mode in _TOOL_PROGRESS_MODES else "all"+++def _gui_surface_toolsets(platform: str) -> set[str]:+ """Toolsets that exist because of the CLIENT (both off ``_HERMES_CORE_TOOLS``; this is the one gate).+ ``platform`` is the SESSION's source, never a process env var: the desktop may drive a URL/cloud+ backend where ``HERMES_DESKTOP`` is unset (AGENTS.md surface rule)."""+ from toolsets import CLIENT_SURFACE_TOOLSETS+ return set(CLIENT_SURFACE_TOOLSETS) if platform == "desktop" else {"project"}+++def _with_session_toolsets(selection, platform: str | None) -> list[str]:+ """*selection* plus what the session carries whatever its config says (the client surface's+ toolsets when *platform* is given; the ones its PROFILE's role reserves, from the backend-written+ profile.yaml under the session's home override), minus toolsets reserved for another role."""+ from toolsets import profile_role_toolsets+ granted, denied = profile_role_toolsets()+ surface = _gui_surface_toolsets(platform) if platform is not None else set()+ kept = [name for name in selection if name not in denied]+ return [*kept, *sorted((surface | granted) - set(kept))]+++def _tui_notice(text: str) -> None:+ print(text, file=sys.stderr, flush=True)+++def _resolve_explicit_toolsets(explicit: list[str], validate_toolset) -> list[str] | None | bool:+ """Resolve a HERMES_TUI_TOOLSETS pin: list, None for "all", False when nothing was valid."""+ built_in = [name for name in explicit if validate_toolset(name)]+ unresolved = [name for name in explicit if name not in built_in]+ if unresolved:+ try:+ from hermes_cli.plugins import discover_plugins+ discover_plugins()+ plugin_valid = [name for name in unresolved if validate_toolset(name)]+ except Exception:+ plugin_valid = []+ built_in.extend(plugin_valid)+ unresolved = [name for name in unresolved if name not in plugin_valid]+ if any(name in {"all", "*"} for name in built_in):+ if ignored := [name for name in explicit if name not in {"all", "*"}]:+ _tui_notice(f"[tui] HERMES_TUI_TOOLSETS=all enables every toolset; ignoring additional entries: {', '.join(ignored)}")+ return None+ if not unresolved:+ return built_in+ try: # (enabled, disabled) MCP server names from raw config; both empty on any failure+ from hermes_cli.config import read_raw_config+ from tools.mcp_tool_common import mcp_server_enabled+ raw_cfg = read_raw_config()+ mcp_servers = raw_cfg.get("mcp_servers") if isinstance(raw_cfg.get("mcp_servers"), dict) else {}+ mcp_names, mcp_disabled = set(), set()+ for name, server_cfg in mcp_servers.items():+ if isinstance(server_cfg, dict):+ on = mcp_server_enabled(server_cfg)+ (mcp_names if on else mcp_disabled).add(str(name))+ except Exception:+ mcp_names, mcp_disabled = set(), set()+ mcp_valid = [name for name in unresolved if name in mcp_names]+ disabled = [name for name in unresolved if name in mcp_disabled]+ unknown = [name for name in unresolved if name not in mcp_names and name not in mcp_disabled]+ if unknown:+ _tui_notice(f"[tui] ignoring unknown HERMES_TUI_TOOLSETS entries: {', '.join(unknown)}")+ if disabled:+ _tui_notice("[tui] ignoring disabled MCP servers in HERMES_TUI_TOOLSETS "+ f"(set enabled: true in config.yaml to use): {', '.join(disabled)}")+ return (built_in + mcp_valid) or False+++def _load_enabled_toolsets(platform: str | None = None) -> list[str] | None:+ """The agent's toolsets for this session (None = all): an explicit HERMES_TUI_TOOLSETS pin; else the+ coding posture (coding_context collapses to coding toolset + enabled MCP servers in a code workspace);+ else the configured CLI toolsets. Client-surface toolsets fold in here — only this surface can answer them."""+ session_platform = platform or _resolve_session_platform()+ explicit = [item.strip() for item in os.environ.get("HERMES_TUI_TOOLSETS", "").split(",") if item.strip()]+ fallback_notice = None+ if not explicit:+ with contextlib.suppress(Exception):+ from agent.coding_context import coding_selection+ selection = coding_selection(platform=session_platform)+ if selection is not None:+ return sorted(_with_session_toolsets(selection, session_platform))+ try:+ from toolsets import validate_toolset+ except Exception:+ validate_toolset = None+ if explicit and validate_toolset is not None:+ resolved = _resolve_explicit_toolsets(explicit, validate_toolset)+ if resolved is not False:+ # An operator pin replaces the surface fold-in but never strips the profile's own role toolsets.+ return resolved if resolved is None else _with_session_toolsets(resolved, None)+ fallback_notice = "[tui] no valid HERMES_TUI_TOOLSETS entries; using configured CLI toolsets"+ try:+ from hermes_cli.config import load_config+ from hermes_cli.tools_config import _get_platform_tools+ cfg = load_config()+ # include_default_mcp_servers=True is the runtime variant (the agent must be able to call+ # default MCP servers); the config-editing variant would silently drop MCP tools from the TUI.+ # Passing ``False`` here is the config-editing variant — used when we need to persist a toolset list+ # without baking in implicit MCP defaults. Using the wrong variant at agent creation time makes MCP+ # tools silently missing from the TUI. See PR #3252 for the original design split.+ enabled = _get_platform_tools(cfg, "cli", include_default_mcp_servers=True)+ if fallback_notice is not None:+ _tui_notice(fallback_notice)+ return sorted(_with_session_toolsets(enabled, session_platform)) if enabled else None+ except Exception:+ if fallback_notice is not None:+ _tui_notice("[tui] no valid HERMES_TUI_TOOLSETS entries and configured CLI toolsets could not be loaded; enabling all toolsets")+ return None+++def _session_tool_progress_mode(sid: str) -> str:+ return str(_sessions.get(sid, {}).get("tool_progress_mode", "all") or "all")+++def _session_verbose(sid: str) -> bool:+ return _session_tool_progress_mode(sid) == "verbose"+++def _tool_progress_enabled(sid: str) -> bool:+ return _session_tool_progress_mode(sid) != "off"+++def _tool_lifecycle_required_for_ui(name: str) -> bool:+ """Interactive UI, not optional chrome: Desktop renders clarify / connection cards from the tool-call part."""+ return name in ("clarify", "manage_connections", "setup_mcp")++def _resolve_runtime_with_fallback(resolve_kwargs: dict | None = None) -> _RuntimeFallbackResolution:+ """Resolve the primary runtime or one complete provider/model fallback. Provider-only fallback entries+ are skipped so the unavailable primary model can never leak into a different runtime."""+ from hermes_cli.auth import AuthError+ from hermes_cli.runtime_provider import resolve_runtime_provider+ try:+ return _RuntimeFallbackResolution(resolve_runtime_provider(**(resolve_kwargs or {})), None, False)+ except AuthError as primary_exc:+ for entry in _load_fallback_model() or []:+ fb_provider = str(entry.get("provider") or "").strip() if isinstance(entry, dict) else ""+ fb_model = str(entry.get("model") or "").strip() if isinstance(entry, dict) else ""+ if not fb_provider or not fb_model:+ continue+ try:+ from hermes_cli.fallback_config import effective_runtime_provider, resolve_entry_api_key+ fb_kwargs: dict = {"requested": fb_provider, "target_model": fb_model,+ **({"explicit_base_url": entry["base_url"]} if entry.get("base_url") else {})}+ if fb_api_key := resolve_entry_api_key(entry):+ fb_kwargs["explicit_api_key"] = fb_api_key+ runtime = resolve_runtime_provider(**fb_kwargs)+ # Named custom entries resolve to the bare "custom" billing class; keep the configured+ # identity so the session/UI shows the provider name, matching the manual-switch path (#98739).+ runtime["provider"] = effective_runtime_provider(entry, runtime)+ from hermes_cli.auth import primary_failure_wording+ logging.getLogger(__name__).warning(+ "Primary %s (%s), falling back to %s model %s",+ primary_failure_wording(primary_exc)[0], primary_exc, fb_provider, fb_model)+ return _RuntimeFallbackResolution(runtime, fb_model, True)+ except Exception:+ continue+ raise+++def _resolve_agent_model_runtime(model_override, provider_override) -> tuple[str, dict]:+ """(model, runtime) for a new agent; a per-session override (/model switch or a resumed row's persisted+ runtime) wins over global config/env. Older rows stored the resolved provider "custom" (no named entry+ matches) — recover the identity from the persisted base_url or the rebuild fails "No LLM provider+ configured". Persisted base_url/api_key/api_mode are honored only for the original runtime, never a fallback."""+ if isinstance(model_override, dict) and model_override.get("model"):+ model = str(model_override.get("model") or "")+ requested_provider = model_override.get("provider") or provider_override or None+ override_base_url = model_override.get("base_url")+ resolve_kwargs = {}+ if str(requested_provider or "").strip().lower() == "custom":+ from hermes_cli.runtime_provider import canonical_custom_identity+ if recovered := canonical_custom_identity(base_url=override_base_url or None, model=model or None):+ requested_provider = recovered+ if override_base_url:+ # Failing identity recovery, still hand base_url to the direct-alias branch so pool/env credentials resolve.+ resolve_kwargs["explicit_base_url"] = override_base_url+ resolve_kwargs.update(requested=requested_provider, target_model=model or None)+ overrides = {k: model_override.get(k) for k in ("base_url", "api_key", "api_mode")}+ else:+ model, requested_provider = _resolve_startup_runtime()+ if isinstance(model_override, str) and model_override:+ model = model_override+ if provider_override:+ requested_provider = provider_override+ resolve_kwargs = {"requested": requested_provider, "target_model": model or None}+ overrides = {}+ resolution = _resolve_runtime_with_fallback(resolve_kwargs)+ if resolution.used_fallback:+ if not resolution.selected_model:+ raise RuntimeError("Auth fallback resolved without a model")+ # Same pre-agent switch the messaging gateway surfaces (#74349); _make_agent pops it onto the+ # agent's one-shot notice so the TUI/Desktop user sees which provider actually answered.+ from hermes_cli.fallback_config import pre_agent_fallback_notice- # requested_provider=None means resolve_runtime_provider read the persisted config provider.- primary_provider = requested_provider or (_load_cfg().get("model") or {}).get("provider")++ # requested_provider=None means resolve_runtime_provider read the persisted config provider;++ # ``model: <id>`` (string shorthand) names no provider.++ cfg_model = _load_cfg().get("model")++ primary_provider = requested_provider or (cfg_model.get("provider") if isinstance(cfg_model, dict) else None)+ resolution.runtime["_fallback_notice"] = pre_agent_fallback_notice(+ primary_provider, model, resolution.runtime.get("provider"), resolution.selected_model)+ return resolution.selected_model, resolution.runtime+ if resolution.runtime.get("source") == "local-runtime":+ # Live supervisor beat any persisted loopback URL for this identity.+ overrides.pop("base_url", None)+ resolution.runtime.update({k: v for k, v in overrides.items() if v})+ if any(overrides.values()):+ _rederive_per_model_route(model, resolution.runtime)+ return model, resolution.runtime+++def _rederive_per_model_route(model: str, runtime: dict) -> None:+ """A row's persisted api_mode/base_url were written for whichever model the session last ran. Providers+ that pick the wire per model (OpenCode Zen/Go, Copilot, Nous) must re-derive both from the target model,+ or a resumed opencode-go session keeps a MiniMax-era anthropic_messages route (and its /v1-stripped or+ other-family relay URL) for a chat_completions model like deepseek-v4-flash-vision-exp (#96066)."""+ from hermes_cli.model_switch import model_derived_api_mode+ from hermes_cli.models import normalize_opencode_base_url+ provider = str(runtime.get("requested_provider") or runtime.get("provider") or "")+ api_mode = model_derived_api_mode(provider, model)+ if api_mode is None:+ return+ runtime["api_mode"] = api_mode+ runtime["base_url"] = normalize_opencode_base_url(provider, api_mode, runtime.get("base_url"))+++def _startup_system_prompt(cfg: dict, task_id: str) -> str:+ """Config ephemeral system prompt + HERMES_TUI_SKILLS preload block. Hard-fails only when EVERY requested+ skill is missing (cli.py parity): a typo'd name must not auto-block the Kanban task."""+ from hermes_cli.config import resolve_ephemeral_system_prompt_from_config+ system_prompt = resolve_ephemeral_system_prompt_from_config(cfg)+ startup_skills = _parse_tui_skills_env()+ if not startup_skills:+ return system_prompt+ from agent.skill_commands import build_preloaded_skills_prompt+ skills_prompt, loaded_skills, missing_skills = build_preloaded_skills_prompt(startup_skills, task_id=task_id)+ if missing_skills:+ missing_display = ", ".join(missing_skills)+ if not loaded_skills:+ raise ValueError(f"Unknown skill(s): {missing_display}")+ logger.warning("Unknown skill(s) requested, skipping: %s. Continuing with: %s. "+ "List available skills with `hermes skills list`.", missing_display, ", ".join(loaded_skills))+ if skills_prompt:+ system_prompt = "\n\n".join(part for part in (system_prompt, skills_prompt) if part).strip()+ return system_prompt+++def _make_agent(+ sid: str, key: str, session_id: str | None = None, session_db=None,+ model_override: dict | str | None = None, provider_override: str | None = None,+ reasoning_config_override: dict | None = None, service_tier_override: str | None = None,+ platform_override: str | None = None, context_cwd_is_launch_artifact: bool | None = None,+ cwd_override: str | None = None, auth_user_id: str | None = None):+ # AC-4 test seam: dead unless armed by the isolated certify harness.+ from tui_gateway.synthetic_turn import maybe_build_synthetic_agent+ synthetic = maybe_build_synthetic_agent(session_id or key, model_override)+ if synthetic is not None:+ return synthetic+ from run_agent import AIAgent+ # MCP discovery runs in a daemon thread (a dead server can't freeze the shell); the agent snapshots its tool+ # list once, so briefly wait for in-flight discovery. Dashboard /api/ws uses mcp_startup; TUI stdio uses entry.+ for _mod in ("hermes_cli.mcp_startup", "tui_gateway.entry"):+ with contextlib.suppress(Exception):+ importlib.import_module(_mod).wait_for_mcp_discovery()+ cfg = _load_cfg()+ # Load hooks alongside the same profile config used to construct this agent.+ from agent.shell_hooks import register_from_config+ register_from_config(cfg)+ system_prompt = _startup_system_prompt(cfg, session_id or key)+ model, runtime = _resolve_agent_model_runtime(model_override, provider_override)+ fallback_notice = runtime.pop("_fallback_notice", None)+ _pr = _load_provider_routing()+ platform = _resolve_agent_platform(platform_override)+ ignore_rules = is_truthy_value(os.environ.get("HERMES_IGNORE_RULES"))+ with _sessions_lock:+ session = _sessions.get(sid)+ agent = AIAgent(+ model=model, max_iterations=_cfg_max_turns(cfg, 500), provider=runtime.get("provider"),+ requested_provider=runtime.get("requested_provider"),+ base_url=runtime.get("base_url"), api_key=runtime.get("api_key"), api_mode=runtime.get("api_mode"),+ acp_command=runtime.get("command"), acp_args=runtime.get("args"),+ credential_pool=runtime.get("credential_pool"), quiet_mode=True,+ verbose_logging=False, # DEBUG agent logging; independent of tool_progress_mode+ reasoning_config=(+ reasoning_config_override if reasoning_config_override is not None else _load_reasoning_config(str(model or ""))),+ service_tier=service_tier_override if service_tier_override is not None else _load_service_tier(),+ enabled_toolsets=_load_enabled_toolsets(platform),+ # OpenRouter provider_routing prefs (gateway + CLI parity).+ providers_allowed=_pr.get("only"), providers_ignored=_pr.get("ignore"), providers_order=_pr.get("order"),+ provider_sort=_pr.get("sort"), provider_require_parameters=_pr.get("require_parameters", False),+ provider_data_collection=_pr.get("data_collection"), platform=platform, session_id=session_id or key,+ cwd=cwd_override,+ # The dashboard login identity reaches memory providers as the runtime user, like a gateway user id.+ # Builds that run before the record exists (branch, eager resume, compute host) pass it explicitly.+ user_id=auth_user_id if auth_user_id is not None else _session_auth_user_id(session),+ session_db=session_db if session_db is not None else _get_db(), ephemeral_system_prompt=system_prompt or None,+ checkpoints_enabled=is_truthy_value(os.environ.get("HERMES_TUI_CHECKPOINTS")),+ pass_session_id=is_truthy_value(os.environ.get("HERMES_TUI_PASS_SESSION_ID")),+ skip_context_files=ignore_rules, skip_memory=ignore_rules, fallback_model=_load_fallback_model(),+ **_agent_cbs(sid))+ if context_cwd_is_launch_artifact is None:+ context_cwd_is_launch_artifact = _context_cwd_is_launch_artifact(session)+ agent._context_cwd_is_launch_artifact = bool(context_cwd_is_launch_artifact)+ if fallback_notice:+ # Emitted once on the first successful reply via _emit_pending_fallback_notice -> status_callback.+ agent._pending_fallback_notice = fallback_notice+ return agent+++def register(server):+ bind_module(globals(), server)