Skip to content

The agent that grows with you

Merge origin/main (#25: 63 commits to 8f463316fde) into feat/unified-gateway-runtime

Teknium302894a2cd73
diff --cc gateway/run_shutdown.py
index cb3d2a3a478,4b457648a3f..deddf220a0a
--- a/gateway/run_shutdown.py
+++ b/gateway/run_shutdown.py
@@@ -28,7 -28,7 +28,8 @@@ from gateway.restart import
effective_stop_drain_timeout, effective_stop_watchdog_delay, resolve_cron_drain_budget
)
from gateway.run_common import _UNSET
+from gateway.run_runtime import managed_turn_count, stop_managed_turns
+ from gateway.run_shutdown_session_end import GatewaySessionEndMixin
from gateway.shutdown_watchdog import arm_shutdown_watchdog, resolve_shutdown_watchdog_delay
# Log-record parity with the origin module.
diff --cc ui-tui/src/app/useSessionLifecycle.ts
index 85eae753d1b,aeef7c89ead..cbdfa29b54a
--- a/ui-tui/src/app/useSessionLifecycle.ts
+++ b/ui-tui/src/app/useSessionLifecycle.ts
@@@ -166,12 -146,19 +166,21 @@@ export function useSessionLifecycle(opt
sys
} = opts
+ // Plugin on_session_finalize text comes back on the close result and is shown as system lines (never a
+ // model turn). `deferMessages`: the caller resets the transcript next and shows them itself afterwards.
++ // A canonical session is the gateway's: the view detaches, it never closes the owner's session.
const closeSession = useCallback(
- (targetSid?: null | string) =>
- targetSid && !gw.isCanonical
- ? rpc<SessionCloseResponse>('session.close', { session_id: targetSid })
- : Promise.resolve(null),
- [gw.isCanonical, rpc]
+ async (targetSid?: null | string, deferMessages = false) => {
- const closed = targetSid ? await rpc<SessionCloseResponse>('session.close', { session_id: targetSid }) : null
++ const closed =
++ targetSid && !gw.isCanonical ? await rpc<SessionCloseResponse>('session.close', { session_id: targetSid }) : null
+
+ if (!deferMessages) {
+ closed?.messages?.forEach(message => sys(message))
+ }
+
+ return closed
+ },
- [rpc, sys]
++ [gw.isCanonical, rpc, sys]
)
const cancelResumeScrollRef = useRef<null | (() => void)>(null)
@@@ -318,128 -206,96 +327,129 @@@
const startNewSession = useCallback(
async (msg?: string, title?: string, keepCurrent = false) => {
- const setup = await rpc<SetupStatusResponse>('setup.status', {})
+ const flight = ++attachmentFlight.current
+ const previousSid = getUiState().sid
+ const previousSubscription = previousSid ? canonicalSubscriptions.current.get(previousSid) : undefined
+ pendingAttachments.current.add(flight)
- if (setup?.provider_configured === false) {
- panel(setupRequiredTitle(), buildSetupRequiredSections())
- patchUiState({ status: t('session.status.setupRequired') })
+ try {
+ const setup = gw.isCanonical ? null : await rpc<SetupStatusResponse>('setup.status', {})
- return null
- }
+ if (flight !== attachmentFlight.current) {
+ return null
+ }
- const previousSid = getUiState().sid
- const closed = keepCurrent ? null : await closeSession(previousSid, true)
+ if (setup?.provider_configured === false) {
+ panel(setupRequiredTitle(), buildSetupRequiredSections())
+ patchUiState({ status: t('session.status.setupRequired') })
- const r = await rpc<SessionCreateResponse>('session.create', {
- cols: colsRef.current,
- ...(STARTUP_WORKSPACE_CWD ? { cwd: STARTUP_WORKSPACE_CWD } : {})
- })
+ return null
+ }
- if (!keepCurrent) {
- await closeSession(previousSid)
- if (!r) {
- patchUiState({ status: 'ready' })
++ const closed = keepCurrent ? null : await closeSession(previousSid, true)
- if (flight !== attachmentFlight.current) {
- return null
- }
- return null
- }
++ if (flight !== attachmentFlight.current) {
++ return null
+ }
- // The durable id lives on the create result; the lazy-create `info` does
- // not carry it, and session.resume / the exit epilogue need the stored id.
- const storedSid = r.stored_session_id || r.session_id
- const info = r.info ? { ...r.info, stored_session_id: storedSid } : null
- const requestedTitle = title?.trim() ?? ''
-
- resetSession()
- setSessionStartedAt(Date.now())
-
- writeActiveSessionFile(storedSid)
- patchUiState({
- info,
- sid: r.session_id,
- status: info?.version ? 'ready' : t('session.status.startingAgent'),
- storedSid,
- usage: usageFrom(info)
- })
+ // HERMES_TUI_CWD is the dashboard-picked workspace: an explicit cwd on
+ // session.create on both transports, so /new stays in that workspace.
+ const workspaceCwd = STARTUP_WORKSPACE_CWD ? { cwd: STARTUP_WORKSPACE_CWD } : {}
- if (info) {
- setHistoryItems([introMsg(info)])
- }
+ const r = await rpc<SessionCreateResponse>(
+ 'session.create',
+ gw.isCanonical
+ ? { request_id: randomUUID(), ...localCreationOptions(), ...workspaceCwd }
+ : { cols: colsRef.current, ...workspaceCwd }
+ )
- if (info?.credential_warning) {
- sys(`warning: ${describeCredentialWarning(info.credential_warning)}`)
- }
+ if (flight !== attachmentFlight.current) {
+ discardStaleAttachment(r)
- if (info?.config_warning) {
- sys(`warning: ${info.config_warning}`)
- }
+ return null
+ }
- if (msg) {
- sys(msg)
- }
+ if (!r) {
+ patchUiState({ status: 'ready' })
- // After the reset above, so the closed session's plugin messages stay visible.
- closed?.messages?.forEach(message => sys(message))
+ return null
+ }
- if (requestedTitle) {
- rpc<SessionTitleResponse>('session.title', {
- session_id: r.session_id,
- title: requestedTitle
+ // The durable id lives on the create result; the lazy-create `info` does
+ // not carry it, and session.resume / the exit epilogue need the stored id.
+ const storedSid = r.stored_session_id || r.info?.stored_session_id || r.session_id
+ const info = r.info ? { ...r.info, stored_session_id: storedSid } : null
+ const requestedTitle = title?.trim() ?? ''
+
+ resetSession()
+ setSessionStartedAt(Date.now())
+
+ writeActiveSessionFile(storedSid)
+ patchUiState({
+ info,
+ sid: r.session_id,
+ status: gw.isCanonical || info?.version ? 'ready' : t('session.status.startingAgent'),
+ storedSid,
+ usage: usageFrom(info)
})
- .then(result => {
- if (!result || getUiState().sid !== r.session_id) {
- return
- }
- const nextTitle = (result.title ?? requestedTitle).trim()
- const suffix = result.pending ? t('session.lifecycle.titleQueuedSuffix') : ''
- patchUiState({ sessionTitle: nextTitle })
- sys(`${t('session.lifecycle.sessionTitleSet', nextTitle)}${suffix}`)
- })
- .catch((err: unknown) => {
- if (getUiState().sid !== r.session_id) {
- return
- }
+ if (info) {
+ setHistoryItems([introMsg(info)])
+ }
- const message = err instanceof Error ? err.message : String(err)
- sys(`warning: ${t('session.lifecycle.failedToSetTitle', message)}`)
+ reportNewSessionNotices(info, msg, sys)
+
++ // After the reset above, so the closed session's plugin messages stay visible.
++ closed?.messages?.forEach(message => sys(message))
++
+ if (requestedTitle) {
+ rpc<SessionTitleResponse>('session.title', {
+ session_id: r.session_id,
+ title: requestedTitle
})
- }
+ .then(result => {
+ if (!result || getUiState().sid !== r.session_id) {
+ return
+ }
+
+ const nextTitle = (result.title ?? requestedTitle).trim()
+ const suffix = result.pending ? t('session.lifecycle.titleQueuedSuffix') : ''
+ patchUiState({ sessionTitle: nextTitle })
+ sys(`${t('session.lifecycle.sessionTitleSet', nextTitle)}${suffix}`)
+ })
+ .catch((err: unknown) => {
+ if (getUiState().sid !== r.session_id) {
+ return
+ }
+
+ const message = err instanceof Error ? err.message : String(err)
+ sys(`warning: ${t('session.lifecycle.failedToSetTitle', message)}`)
+ })
+ }
- signalFreshSessionBoundary(previousSid, r.session_id, onFreshSessionStarted)
+ signalFreshSessionBoundary(previousSid, r.session_id, onFreshSessionStarted)
+ adoptAttachment(r, previousSid, previousSubscription)
- return r.session_id
+ return r.session_id
+ } finally {
+ finishAttachment(flight)
+ }
},
- [closeSession, colsRef, onFreshSessionStarted, panel, resetSession, rpc, setHistoryItems, setSessionStartedAt, sys]
+ [
+ adoptAttachment,
+ closeSession,
+ colsRef,
+ discardStaleAttachment,
+ finishAttachment,
+ gw.isCanonical,
+ onFreshSessionStarted,
+ panel,
+ resetSession,
+ rpc,
+ setHistoryItems,
+ setSessionStartedAt,
+ sys
+ ]
)
const newSession = useCallback(