mirror of
https://github.com/wu736139669/hapi.git
synced 2026-10-30 23:27:57 +00:00
feat: workspace browser with --workspace-root opt-in scoping (#526)
* feat(web): add workspace browser for multi-directory navigation Add /browse route with a folder browser that lets users navigate filesystem directories on connected machines and launch sessions from any folder. Supports saved workspace paths and direct path input. The "Start Session" action pre-fills the NewSession form. - CLI: register machine-level `list-directory` RPC handler - Hub: add POST /machines/:id/list-directory route - Web: add WorkspaceBrowser component with git repo detection - Web: add /browse route with navigation from sessions sidebar - Web: support initialDirectory/initialMachineId in NewSession Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add --workspace-root opt-in scoping for /browse and session spawn Adds a single new flag, \`--workspace-root <path>\` (with \`~\` / \`~/foo\` expansion), on \`hapi runner start\` and \`hapi runner start-sync\`. When set: - The runner reports the path in machine metadata. - The list-directory and spawn-session RPC handlers reject paths outside the root, so the web UI can't escape the configured tree even if someone crafts a request manually. - The /browse page in the web UI auto-opens that root, restricts the breadcrumb / go-up to its subtree, and shows directory entries with git-repo annotations. - The /sessions/new form keeps its existing free-text directory input plus autocomplete + recent-paths chips, and gains a small "Browse" button (next to the input) that opens /browse for picking a folder. - Reconnect-time metadata sync ensures stale records get the field filled in (or cleared when the flag is dropped on a later restart), so the hub state matches the CLI's intent. When unset: - Runner behaves like the legacy hapi (no scoping, no browse feature). - /browse renders an informative state pointing at the flag instead of blocking the user. - The /sessions/new form looks identical to the pre-change behavior; the "Browse" button is hidden. Includes a startup banner so \`runner start-sync\` no longer looks like it hung, and surfaces the workspace-root sync result on stdout. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(hub): preserve workspaceRoot when rehydrating machines from store MachineCache.refreshMachine() rebuilt the metadata object from an explicit field allowlist, so any field not in the list (including the new workspaceRoot) was silently dropped on every read — even though it was correctly written to the store. Add workspaceRoot to the zod schema, the Machine interface, and the hand-rolled projection. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(web): friendlier empty state on /sessions When there are zero sessions the page used to be a vast blank rectangle with just the "0 sessions in 0 projects" caption. Render a centered empty state instead: a calendar/agenda icon, a short heading and hint, and two buttons — "Start a session" (→ /sessions/new) and "Browse workspace" (→ /browse). SessionList gains an optional onBrowse prop. Router wires it on the sessions page so the secondary button resolves; other callers can leave it unset to hide that button. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * docs: document --workspace-root flag in cli/README and root README Add a short paragraph under "Runner management" in cli/README.md explaining what \`--workspace-root\` enables (scoped /browse tree, list/spawn enforcement, tilde expansion) and that omitting it keeps the legacy behavior. Mention the workspace browser in the top-level README's Features list. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: address PR #526 review feedback Three findings from the review bot: 1. [Major] Workspace-scope check was lexical only. With workspaceRoot = /safe, a symlink such as /safe/out -> /etc would pass the relative- path test and let list-directory / spawn-happy-session reach paths outside the configured root. realpath the workspaceRoot at construction time, and resolve every incoming path through realpath (walking up to the nearest existing parent for spawn targets that haven't been created yet) before the containment check. 2. [Minor] \`hapi runner start --workspace-root\` with no value used to drop the flag silently and start the runner unscoped. Now treats a missing or flag-shaped next argument as an error. 3. [Minor] /sessions/new's "Browse" button always opened /browse using localStorage's last-used machine, ignoring the user's current selection. NewSession already passes machineId in its callback; forward it through the /browse search params and seed WorkspaceBrowser with it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(cli): gate list-directory RPC behind --workspace-root opt-in Without a configured workspaceRoot, isWithinWorkspaceRoot() returns true unconditionally, leaving the new list-directory RPC able to enumerate any path on the runner. The Web UI already hides Browse for these machines, but the backend should enforce the opt-in too. Refuse the RPC up front when no workspace root is configured. --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
3a2a02c0eb
commit
010dc41369
+2
-2
@@ -142,7 +142,7 @@ export class ApiClient {
|
||||
return new ApiSessionClient(this.token, session)
|
||||
}
|
||||
|
||||
machineSyncClient(machine: Machine): ApiMachineClient {
|
||||
return new ApiMachineClient(this.token, machine)
|
||||
machineSyncClient(machine: Machine, options?: { workspaceRoot?: string }): ApiMachineClient {
|
||||
return new ApiMachineClient(this.token, machine, options?.workspaceRoot)
|
||||
}
|
||||
}
|
||||
|
||||
+179
-2
@@ -3,7 +3,9 @@
|
||||
*/
|
||||
|
||||
import { io, type Socket } from 'socket.io-client'
|
||||
import { stat } from 'node:fs/promises'
|
||||
import { readdir, realpath, stat } from 'node:fs/promises'
|
||||
import { realpathSync } from 'node:fs'
|
||||
import { basename, dirname, isAbsolute, join, relative, resolve as resolvePath } from 'node:path'
|
||||
import { logger } from '@/ui/logger'
|
||||
import { configuration } from '@/configuration'
|
||||
import type { Update, UpdateMachineBody } from '@hapi/protocol'
|
||||
@@ -65,15 +67,50 @@ interface PathExistsResponse {
|
||||
exists: Record<string, boolean>
|
||||
}
|
||||
|
||||
interface ListMachineDirectoryRequest {
|
||||
path: string
|
||||
}
|
||||
|
||||
interface ListMachineDirectoryEntry {
|
||||
name: string
|
||||
type: 'file' | 'directory' | 'other'
|
||||
size?: number
|
||||
modified?: number
|
||||
isGitRepo?: boolean
|
||||
}
|
||||
|
||||
interface ListMachineDirectoryResponse {
|
||||
success: boolean
|
||||
entries?: ListMachineDirectoryEntry[]
|
||||
error?: string
|
||||
}
|
||||
|
||||
export class ApiMachineClient {
|
||||
private socket!: Socket<ServerToRunnerEvents, RunnerToServerEvents>
|
||||
private keepAliveInterval: NodeJS.Timeout | null = null
|
||||
private rpcHandlerManager: RpcHandlerManager
|
||||
|
||||
private readonly normalizedWorkspaceRoot: string | undefined
|
||||
|
||||
constructor(
|
||||
private readonly token: string,
|
||||
private readonly machine: Machine
|
||||
private readonly machine: Machine,
|
||||
private readonly workspaceRoot?: string
|
||||
) {
|
||||
// realpath the root once so all subsequent comparisons are against
|
||||
// the canonical, symlink-resolved path. Falls back to a lexical
|
||||
// resolve if realpath fails (e.g. unusual permission setup) so we
|
||||
// still get *some* protection rather than skipping the check.
|
||||
if (workspaceRoot) {
|
||||
try {
|
||||
this.normalizedWorkspaceRoot = realpathSync(workspaceRoot)
|
||||
} catch {
|
||||
this.normalizedWorkspaceRoot = resolvePath(workspaceRoot)
|
||||
}
|
||||
} else {
|
||||
this.normalizedWorkspaceRoot = undefined
|
||||
}
|
||||
|
||||
this.rpcHandlerManager = new RpcHandlerManager({
|
||||
scopePrefix: this.machine.id,
|
||||
logger: (msg, data) => logger.debug(msg, data)
|
||||
@@ -99,6 +136,113 @@ export class ApiMachineClient {
|
||||
|
||||
return { exists }
|
||||
})
|
||||
|
||||
this.rpcHandlerManager.registerHandler<ListMachineDirectoryRequest, ListMachineDirectoryResponse>('list-directory', async (params) => {
|
||||
if (!this.normalizedWorkspaceRoot) {
|
||||
return { success: false, error: 'Workspace browsing is not enabled for this machine' }
|
||||
}
|
||||
|
||||
const rawPath = typeof params?.path === 'string' ? params.path.trim() : ''
|
||||
if (!rawPath) {
|
||||
return { success: false, error: 'Path is required' }
|
||||
}
|
||||
|
||||
const targetPath = await this.resolveForWorkspaceCheck(rawPath)
|
||||
if (!this.isWithinWorkspaceRoot(targetPath)) {
|
||||
return { success: false, error: 'Path is outside workspace root' }
|
||||
}
|
||||
|
||||
try {
|
||||
const dirStat = await stat(targetPath)
|
||||
if (!dirStat.isDirectory()) {
|
||||
return { success: false, error: 'Path is not a directory' }
|
||||
}
|
||||
|
||||
const dirEntries = await readdir(targetPath, { withFileTypes: true })
|
||||
const entries: ListMachineDirectoryEntry[] = []
|
||||
|
||||
await Promise.all(dirEntries.map(async (entry) => {
|
||||
if (entry.name.startsWith('.')) return
|
||||
|
||||
const fullPath = join(targetPath, entry.name)
|
||||
let type: 'file' | 'directory' | 'other' = 'other'
|
||||
let size: number | undefined
|
||||
let modified: number | undefined
|
||||
let isGitRepo = false
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
type = 'directory'
|
||||
try {
|
||||
const gitStat = await stat(join(fullPath, '.git'))
|
||||
isGitRepo = gitStat.isDirectory() || gitStat.isFile()
|
||||
} catch {
|
||||
// not a git repo
|
||||
}
|
||||
} else if (entry.isFile()) {
|
||||
type = 'file'
|
||||
}
|
||||
|
||||
if (!entry.isSymbolicLink()) {
|
||||
try {
|
||||
const stats = await stat(fullPath)
|
||||
size = stats.size
|
||||
modified = stats.mtime.getTime()
|
||||
} catch {
|
||||
// ignore stat errors
|
||||
}
|
||||
}
|
||||
|
||||
entries.push({ name: entry.name, type, size, modified, isGitRepo })
|
||||
}))
|
||||
|
||||
entries.sort((a, b) => {
|
||||
if (a.type === 'directory' && b.type !== 'directory') return -1
|
||||
if (a.type !== 'directory' && b.type === 'directory') return 1
|
||||
return a.name.localeCompare(b.name)
|
||||
})
|
||||
|
||||
return { success: true, entries }
|
||||
} catch (error) {
|
||||
return { success: false, error: error instanceof Error ? error.message : 'Failed to list directory' }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private isWithinWorkspaceRoot(absolutePath: string): boolean {
|
||||
if (!this.normalizedWorkspaceRoot) return true
|
||||
const rel = relative(this.normalizedWorkspaceRoot, absolutePath)
|
||||
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))
|
||||
}
|
||||
|
||||
/**
|
||||
* Canonicalize a path for workspace-root containment checks. Resolves
|
||||
* symlinks via realpath so a symlink such as `/safe/out -> /etc` cannot
|
||||
* be used to escape the configured root with a lexical-only check.
|
||||
*
|
||||
* If the path doesn't exist (e.g. a session is being spawned in a
|
||||
* directory we'll create), walks up to the nearest existing ancestor
|
||||
* and realpaths *that*, joining the missing tail back on. This way the
|
||||
* check still runs against the real on-disk location once any
|
||||
* intermediate symlink in the parent chain has been resolved.
|
||||
*/
|
||||
private async resolveForWorkspaceCheck(path: string): Promise<string> {
|
||||
const absolute = resolvePath(path)
|
||||
try {
|
||||
return await realpath(absolute)
|
||||
} catch {
|
||||
const missing: string[] = []
|
||||
let cursor = absolute
|
||||
while (cursor !== dirname(cursor)) {
|
||||
missing.unshift(basename(cursor))
|
||||
cursor = dirname(cursor)
|
||||
try {
|
||||
return join(await realpath(cursor), ...missing)
|
||||
} catch {
|
||||
// keep walking to the nearest existing parent
|
||||
}
|
||||
}
|
||||
return absolute
|
||||
}
|
||||
}
|
||||
|
||||
setRPCHandlers({ spawnSession, stopSession, requestShutdown }: MachineRpcHandlers): void {
|
||||
@@ -109,6 +253,11 @@ export class ApiMachineClient {
|
||||
throw new Error('Directory is required')
|
||||
}
|
||||
|
||||
const resolvedDirectory = await this.resolveForWorkspaceCheck(directory)
|
||||
if (!this.isWithinWorkspaceRoot(resolvedDirectory)) {
|
||||
return { type: 'error', errorMessage: 'Directory is outside this machine\'s workspace root' }
|
||||
}
|
||||
|
||||
const result = await spawnSession({
|
||||
directory,
|
||||
sessionId,
|
||||
@@ -249,6 +398,34 @@ export class ApiMachineClient {
|
||||
})).catch((error) => {
|
||||
logger.debug('[API MACHINE] Failed to update runner state on connect', error)
|
||||
})
|
||||
|
||||
const hubWorkspaceRoot = this.machine.metadata?.workspaceRoot
|
||||
const desiredWorkspaceRoot = this.workspaceRoot
|
||||
if (desiredWorkspaceRoot !== hubWorkspaceRoot) {
|
||||
if (desiredWorkspaceRoot) {
|
||||
console.log(`[HAPI] Syncing workspace root to hub: ${desiredWorkspaceRoot} (current hub value: ${hubWorkspaceRoot ?? 'none'})`)
|
||||
} else {
|
||||
console.log(`[HAPI] Clearing workspace root on hub (was: ${hubWorkspaceRoot})`)
|
||||
}
|
||||
this.updateMachineMetadata((current) => {
|
||||
const base = current ?? this.machine.metadata
|
||||
if (!base) {
|
||||
return { workspaceRoot: desiredWorkspaceRoot } as MachineMetadata
|
||||
}
|
||||
if (desiredWorkspaceRoot) {
|
||||
return { ...base, workspaceRoot: desiredWorkspaceRoot }
|
||||
}
|
||||
const { workspaceRoot: _omit, ...rest } = base
|
||||
return rest as MachineMetadata
|
||||
}).then(() => {
|
||||
console.log(`[HAPI] Workspace root synced: ${this.machine.metadata?.workspaceRoot ?? '(none)'}`)
|
||||
}).catch((error) => {
|
||||
console.error('[HAPI] Failed to sync workspace root:', error instanceof Error ? error.message : error)
|
||||
})
|
||||
} else if (desiredWorkspaceRoot) {
|
||||
console.log(`[HAPI] Workspace root already up to date on hub: ${desiredWorkspaceRoot}`)
|
||||
}
|
||||
|
||||
this.startKeepAlive()
|
||||
})
|
||||
|
||||
|
||||
@@ -36,7 +36,8 @@ export const MachineMetadataSchema = z.object({
|
||||
displayName: z.string().optional(),
|
||||
homeDir: z.string(),
|
||||
happyHomeDir: z.string(),
|
||||
happyLibDir: z.string()
|
||||
happyLibDir: z.string(),
|
||||
workspaceRoot: z.string().optional()
|
||||
})
|
||||
|
||||
export type MachineMetadata = z.infer<typeof MachineMetadataSchema>
|
||||
|
||||
Reference in New Issue
Block a user