-
Notifications
You must be signed in to change notification settings - Fork 943
feat(tui): ask for workspace trust on startup with the v2 engine #2453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
apps/kimi-code/src/tui/components/dialogs/trust-prompt.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { | ||
| Key, | ||
| matchesKey, | ||
| truncateToWidth, | ||
| wrapTextWithAnsi, | ||
| type Component, | ||
| type Focusable, | ||
| } from '@moonshot-ai/pi-tui'; | ||
|
|
||
| import { SELECT_POINTER } from '#/tui/constant/symbols'; | ||
| import { currentTheme } from '#/tui/theme'; | ||
|
|
||
| export type TrustPromptChoice = 'trust' | 'distrust'; | ||
|
|
||
| export interface TrustPromptOptions { | ||
| readonly workDir: string; | ||
| /** Project-level MCP servers that trusting would enable; may be empty. */ | ||
| readonly gatedMcpServers: readonly string[]; | ||
| /** Esc resolves to 'distrust' as well. */ | ||
| readonly onSelect: (choice: TrustPromptChoice) => void; | ||
| } | ||
|
|
||
| interface TrustPromptOption { | ||
| readonly value: TrustPromptChoice; | ||
| readonly label: string; | ||
| readonly description: string; | ||
| } | ||
|
|
||
| const OPTIONS: readonly TrustPromptOption[] = [ | ||
| { | ||
| value: 'trust', | ||
| label: 'Trust this folder', | ||
| description: 'Enable project MCP servers. Remembered for this folder.', | ||
| }, | ||
| { | ||
| value: 'distrust', | ||
| label: "Don't trust", | ||
| description: 'Exit Kimi Code. Asked again next launch.', | ||
| }, | ||
| ]; | ||
|
|
||
| export class TrustPromptComponent implements Component, Focusable { | ||
| focused = false; | ||
| private selectedIndex = 0; | ||
|
|
||
| constructor(private readonly opts: TrustPromptOptions) {} | ||
|
|
||
| invalidate(): void {} | ||
|
|
||
| handleInput(data: string): void { | ||
| if (matchesKey(data, Key.escape)) { | ||
| this.opts.onSelect('distrust'); | ||
| return; | ||
| } | ||
| if (matchesKey(data, Key.up)) { | ||
| this.selectedIndex = Math.max(0, this.selectedIndex - 1); | ||
| return; | ||
| } | ||
| if (matchesKey(data, Key.down)) { | ||
| this.selectedIndex = Math.min(OPTIONS.length - 1, this.selectedIndex + 1); | ||
| return; | ||
| } | ||
| if (matchesKey(data, Key.enter) || matchesKey(data, Key.space)) { | ||
| this.opts.onSelect(OPTIONS[this.selectedIndex]!.value); | ||
| } | ||
| } | ||
|
|
||
| render(width: number): string[] { | ||
| const rule = currentTheme.fg('primary', '─'.repeat(width)); | ||
| const lines = [ | ||
| rule, | ||
| currentTheme.boldFg('primary', ' Trust this folder?'), | ||
| currentTheme.fg('textMuted', ' ↑↓ navigate · Enter select · Esc exit'), | ||
| '', | ||
| ...wrapTextWithAnsi(this.opts.workDir, Math.max(20, width - 2)).map( | ||
| (line) => ` ${currentTheme.fg('textStrong', line)}`, | ||
| ), | ||
| '', | ||
| ]; | ||
|
|
||
| const notice = | ||
| this.opts.gatedMcpServers.length > 0 | ||
| ? `Kimi Code loads project-level MCP servers (.mcp.json, .kimi-code/mcp.json) only in trusted folders. They run as local processes on your machine. This folder defines: ${this.opts.gatedMcpServers.join(', ')}.` | ||
| : 'Kimi Code loads project-level MCP servers (.mcp.json, .kimi-code/mcp.json) only in trusted folders. They run as local processes on your machine.'; | ||
| for (const line of wrapTextWithAnsi(notice, Math.max(20, width - 2))) { | ||
| lines.push(` ${currentTheme.fg('textMuted', line)}`); | ||
| } | ||
| lines.push(''); | ||
|
|
||
| for (let i = 0; i < OPTIONS.length; i += 1) { | ||
| const option = OPTIONS[i]!; | ||
| const selected = i === this.selectedIndex; | ||
| const pointer = selected ? SELECT_POINTER : ' '; | ||
| const label = selected | ||
| ? currentTheme.boldFg('primary', option.label) | ||
| : currentTheme.fg('text', option.label); | ||
| lines.push(currentTheme.fg(selected ? 'primary' : 'textDim', ` ${pointer} `) + label); | ||
| for (const line of wrapTextWithAnsi(option.description, Math.max(20, width - 4))) { | ||
| lines.push(` ${currentTheme.fg('textMuted', line)}`); | ||
| } | ||
| lines.push(''); | ||
| } | ||
|
|
||
| lines.push(rule); | ||
| return lines.map((line) => truncateToWidth(line, width)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
apps/kimi-code/test/tui/components/dialogs/trust-prompt.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { TrustPromptComponent } from '#/tui/components/dialogs/trust-prompt'; | ||
|
|
||
| const ANSI_SGR = /\[[0-9;]*m/g; | ||
|
|
||
| function strip(text: string): string { | ||
| return text.replaceAll(ANSI_SGR, ''); | ||
| } | ||
|
|
||
| function renderLines(gatedMcpServers: readonly string[] = []): string[] { | ||
| const prompt = new TrustPromptComponent({ | ||
| workDir: '/tmp/demo-workspace', | ||
| gatedMcpServers, | ||
| onSelect: vi.fn(), | ||
| }); | ||
| return prompt.render(100).map(strip); | ||
| } | ||
|
|
||
| describe('TrustPromptComponent', () => { | ||
| it('renders the header vocabulary and the workspace path', () => { | ||
| const lines = renderLines(); | ||
| const titleIdx = lines.findIndex((l) => l.includes('Trust this folder?')); | ||
| expect(titleIdx).toBeGreaterThanOrEqual(0); | ||
| const hint = lines[titleIdx + 1]; | ||
| expect(hint).toContain('↑↓ navigate'); | ||
| expect(hint).toContain('Enter select'); | ||
| expect(hint).toContain('Esc exit'); | ||
| expect(lines.some((l) => l.includes('/tmp/demo-workspace'))).toBe(true); | ||
| }); | ||
|
|
||
| it('lists the gated project MCP servers when present', () => { | ||
| const lines = renderLines(['nested-server', 'root-server']); | ||
| expect(lines.some((l) => l.includes('This folder defines'))).toBe(true); | ||
| expect(lines.some((l) => l.includes('nested-server'))).toBe(true); | ||
| expect(lines.some((l) => l.includes('root-server'))).toBe(true); | ||
| expect(renderLines().some((l) => l.includes('This folder defines'))).toBe(false); | ||
| }); | ||
|
|
||
| it('selects trust on Enter with the default highlight', () => { | ||
| const onSelect = vi.fn(); | ||
| const prompt = new TrustPromptComponent({ | ||
| workDir: '/tmp/demo-workspace', | ||
| gatedMcpServers: [], | ||
| onSelect, | ||
| }); | ||
| prompt.handleInput('\r'); | ||
| expect(onSelect).toHaveBeenCalledWith('trust'); | ||
| }); | ||
|
|
||
| it('selects distrust after moving the cursor down', () => { | ||
| const onSelect = vi.fn(); | ||
| const prompt = new TrustPromptComponent({ | ||
| workDir: '/tmp/demo-workspace', | ||
| gatedMcpServers: [], | ||
| onSelect, | ||
| }); | ||
| prompt.handleInput('\u001B[B'); | ||
| prompt.handleInput('\r'); | ||
| expect(onSelect).toHaveBeenCalledWith('distrust'); | ||
| }); | ||
|
|
||
| it('treats Esc as distrust', () => { | ||
| const onSelect = vi.fn(); | ||
| const prompt = new TrustPromptComponent({ | ||
| workDir: '/tmp/demo-workspace', | ||
| gatedMcpServers: [], | ||
| onSelect, | ||
| }); | ||
| prompt.handleInput('\u001B'); | ||
| expect(onSelect).toHaveBeenCalledWith('distrust'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For an untrusted repo, the server names come directly from project-controlled
.mcp.jsonfiles and are interpolated into the terminal output before the user has granted trust.wrapTextWithAnsi/theme coloring preserves embedded ESC/OSC bytes, and this codebase already notes that pi-tui renders strings straight to the terminal, so a malicious server name can emit terminal control sequences from the trust dialog itself. Strip control sequences (or otherwise render names as inert text) before joining them into this notice.AGENTS.md reference: AGENTS.md:L22-L22
Useful? React with 👍 / 👎.