Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useSyncingState } from '@/hooks/useSyncingState'
import { usePushNotifications } from '@/hooks/usePushNotifications'
import { useViewportHeight } from '@/hooks/useViewportHeight'
import { useVisibilityReporter } from '@/hooks/useVisibilityReporter'
import { useAutoSpawn } from '@/hooks/useAutoSpawn'
import { queryKeys } from '@/lib/query-keys'
import { AppContextProvider } from '@/lib/app-context'
import { fetchLatestMessages } from '@/lib/message-window-store'
Expand Down Expand Up @@ -143,12 +144,13 @@ function AppInner() {
if (!token || !api) return
const { pathname, search, hash, state } = router.history.location
const searchParams = new URLSearchParams(search)
if (!searchParams.has('server') && !searchParams.has('hub') && !searchParams.has('token')) {
const authKeys = ['server', 'hub', 'token', 'spawn', 'machine', 'dir', 'boot']
if (!authKeys.some(k => searchParams.has(k))) {
return
}
searchParams.delete('server')
searchParams.delete('hub')
searchParams.delete('token')
for (const key of authKeys) {
searchParams.delete(key)
}
const nextSearch = searchParams.toString()
const nextHref = `${pathname}${nextSearch ? `?${nextSearch}` : ''}${hash}`
router.history.replace(nextHref, state)
Expand Down Expand Up @@ -246,6 +248,9 @@ function AppInner() {
return { all: true }
}, [selectedSessionId])

// Auto-spawn session from URL params (?spawn=true&machine=xxx&dir=/path)
useAutoSpawn(api)

const { subscriptionId } = useSSE({
enabled: Boolean(api && token),
token: token ?? '',
Expand Down
46 changes: 46 additions & 0 deletions web/src/hooks/useAutoSpawn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useEffect, useRef } from 'react'
import { useNavigate } from '@tanstack/react-router'
import type { ApiClient } from '@/api/client'

// Capture spawn params at module load time, before any effect can clean the URL
const initialSearch = typeof window !== 'undefined' ? window.location.search : ''
const initialQuery = new URLSearchParams(initialSearch)
const SPAWN_PARAMS = {
spawn: initialQuery.get('spawn') === 'true',
machine: initialQuery.get('machine'),
dir: initialQuery.get('dir'),
boot: initialQuery.get('boot'),
} as const

export function useAutoSpawn(api: ApiClient | null) {
const navigate = useNavigate()
const attemptedRef = useRef(false)

useEffect(() => {
if (!api || attemptedRef.current) return
if (!SPAWN_PARAMS.spawn || !SPAWN_PARAMS.machine || !SPAWN_PARAMS.dir) return

attemptedRef.current = true

api.spawnSession(SPAWN_PARAMS.machine, SPAWN_PARAMS.dir).then(async (result) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Major] This direct spawnSession() call skips the same missing-directory guard used by the manual spawn flows. Those paths first call checkPathsExists() and stop until the user confirms creation, but the runner defaults approvedNewDirectoryCreation = true and will create the directory automatically. A typo or stale deep link can therefore create folders on disk just by opening the URL.

Suggested fix:

const exists = await api.checkMachinePathsExists(SPAWN_PARAMS.machine, [SPAWN_PARAMS.dir])
if (exists.exists[SPAWN_PARAMS.dir] === false) {
    console.error('Auto-spawn blocked: missing directory requires explicit confirmation')
    return
}

const result = await api.spawnSession(SPAWN_PARAMS.machine, SPAWN_PARAMS.dir)

if (result.type === 'success') {
if (SPAWN_PARAMS.boot) {
try {
await api.sendMessage(result.sessionId, SPAWN_PARAMS.boot)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Major] boot can be lost here. spawnSession() only returns the new session id; POST /api/sessions/:id/messages still rejects sends until requireActive passes, and sessions are only marked active later from alive updates. In the failure case the link still navigates, but the user’s first prompt disappears into a logged error.

Suggested fix:

const waitForActive = async (sessionId: string) => {
    for (let i = 0; i < 30; i += 1) {
        const { session } = await api.getSession(sessionId)
        if (session.active) return true
        await new Promise((resolve) => setTimeout(resolve, 500))
    }
    return false
}

if (SPAWN_PARAMS.boot && await waitForActive(result.sessionId)) {
    await api.sendMessage(result.sessionId, SPAWN_PARAMS.boot)
}

} catch (err) {
console.error('Auto-spawn boot message failed:', err)
}
}
navigate({
to: '/sessions/$sessionId',
params: { sessionId: result.sessionId },
replace: true,
})
} else {
console.error('Auto-spawn failed:', result.message)
}
}).catch((err) => {
console.error('Auto-spawn error:', err)
})
}, [api, navigate])
}
Loading