fix(native-chat): guard only the chat-name host lookup, not the shared host-id parser

Reviewers found that making the shared parser read a non-string as absent turns it
into 'local' for worktree removal and trash cleanup, which could act on this
machine's disk. Restore the shared parser and check the type at the one render path
the crash report hit.
This commit is contained in:
Brennan Benson
2026-11-16 23:21:04 -08:00
parent ec520d14e9
commit 38f168d69f
4 changed files with 19 additions and 25 deletions
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest'
import { structuredAgentSessionTargetForHost } from './structured-agent-session-owner'
describe('structuredAgentSessionTargetForHost', () => {
// A non-string host id reached render and threw `e?.trim is not a function` (crash d1e50ca3).
it('serves no runtime for a non-string host id instead of throwing', () => {
expect(structuredAgentSessionTargetForHost(42)).toBeNull()
expect(structuredAgentSessionTargetForHost({ id: 'local' })).toBeNull()
})
})
@@ -27,9 +27,10 @@ import type { RuntimeClientTarget } from '../../../../../shared/remote-runtime/h
* runtime serves: an SSH host, or the catalog's unresolved-owner sentinel.
*/
export function structuredAgentSessionTargetForHost(
executionHostId: string | null | undefined
executionHostId: unknown
): RuntimeClientTarget | null {
const host = parseExecutionHostId(executionHostId)
// Render reads unchecked tab and wire stamps: a non-string owner has no runtime, not a crash.
const host = typeof executionHostId === 'string' ? parseExecutionHostId(executionHostId) : null
return host ? runtimeRpcTargetForAuthorityKey(host.id) : null
}
@@ -18,18 +18,6 @@ describe('execution host identity', () => {
vi.unstubAllGlobals()
})
// A non-string host id reached render and threw `e?.trim is not a function` (crash d1e50ca3).
it('treats a non-string stored host id as absent', () => {
expect(parseExecutionHostId(42)).toBeNull()
expect(parseExecutionHostId({ id: 'local' })).toBeNull()
})
it('does not treat a repo with a non-string host id as local', () => {
expect(isRepoOnCatalogHost({ executionHostId: 42 })).toBe(false)
expect(isRepoOnCatalogHost({ connectionId: {} })).toBe(false)
expect(isRepoOnCatalogHost({})).toBe(true)
})
it('prefers explicit worktree ownership before repo and focused-host fallbacks', () => {
expect(
getWorktreeExecutionHostId(
+6 -11
View File
@@ -53,9 +53,9 @@ export function getLocalExecutionHostLabel(platform: NodeJS.Platform | null = nu
return 'This computer'
}
/** Trims a stored id; blank or non-string is absent, since no write or read path validates it. */
export function normalizeHostPart(value: unknown): string | null {
return typeof value === 'string' ? value.trim() || null : null
/** Trims a stored id; blank is absent, since no write path normalizes it. */
export function normalizeHostPart(value: string | null | undefined): string | null {
return value?.trim() || null
}
export function toSshExecutionHostId(targetId: string): `ssh:${string}` {
@@ -92,7 +92,7 @@ export function isOwnedSshTargetId(targetId: string | null | undefined): boolean
)
}
export function parseExecutionHostId(value: unknown): ParsedExecutionHost | null {
export function parseExecutionHostId(value: string | null | undefined): ParsedExecutionHost | null {
const normalized = normalizeHostPart(value)
if (!normalized) {
return null
@@ -227,14 +227,9 @@ export function getRepoExecutionHostId(repo: {
/** True only for catalog-local files; unknown hosts or a stale connectionId beside local cannot prove locality. */
export function isRepoOnCatalogHost(repo: {
connectionId?: unknown
executionHostId?: unknown
connectionId?: string | null
executionHostId?: string | null
}): boolean {
// A non-string stored id has unknown ownership, so it cannot prove the repo is local.
const ids = [repo.executionHostId, repo.connectionId]
if (ids.some((id) => id != null && typeof id !== 'string')) {
return false
}
const executionHostId = normalizeHostPart(repo.executionHostId)
return (
(executionHostId === null || executionHostId === LOCAL_EXECUTION_HOST_ID) &&