From 38f168d69f4e3ca58decfc9e020cddc4492259dc Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:21:04 -0800 Subject: [PATCH] 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. --- .../structured-agent-session-owner.test.ts | 10 ++++++++++ .../structured-agent-session-owner.ts | 5 +++-- .../execution-host/execution-host.test.ts | 12 ------------ src/shared/execution-host/execution-host.ts | 17 ++++++----------- 4 files changed, 19 insertions(+), 25 deletions(-) create mode 100644 src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.test.ts diff --git a/src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.test.ts b/src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.test.ts new file mode 100644 index 00000000000..ad1c8c54b1a --- /dev/null +++ b/src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.test.ts @@ -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() + }) +}) diff --git a/src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.ts b/src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.ts index 9824b9f4e3b..8e767d376e3 100644 --- a/src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.ts +++ b/src/renderer/src/runtime/native-chat/session-ownership/structured-agent-session-owner.ts @@ -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 } diff --git a/src/shared/execution-host/execution-host.test.ts b/src/shared/execution-host/execution-host.test.ts index 0e75a3d1047..752bfba5726 100644 --- a/src/shared/execution-host/execution-host.test.ts +++ b/src/shared/execution-host/execution-host.test.ts @@ -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( diff --git a/src/shared/execution-host/execution-host.ts b/src/shared/execution-host/execution-host.ts index 9ab03dc5d2e..b382b091b95 100644 --- a/src/shared/execution-host/execution-host.ts +++ b/src/shared/execution-host/execution-host.ts @@ -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) &&