Files
orca/src/shared/tui-agent-startup.ts
T
BingZandJinwoo Hong 96d1fa1d62 fix(grok): clipboard, native chat, hooks, sessions, ConPTY KKP (#7944)
* fix(grok): restore clipboard and native-chat parity

Grok CLI already supports argv prompts, OSC 52 copy, and image paste chips.
Orca was blocking those paths: stdin-after-start keystroke injection, OSC 52
writes default-off, image-attachment denylist, and native-chat allowlist.

- Launch Grok with positional argv prompts
- Default OSC 52 TUI clipboard writes on (still user-toggleable)
- Treat Grok as image-attachment capable
- Parse ~/.grok/.../chat_history.jsonl for native chat

OSC 52 clipboard *query* remains ignored by design (host clipboard exfil risk);
xAI docs only require OSC 52 write for remote copy.

* fix(grok): sync OSC 52 docs and locale catalog with default-on

Update terminalAllowOsc52Clipboard type docs for the true default, and
refresh locale strings so settings UI mentions Grok alongside other TUIs.

* fix(grok): tool hook matcher, StopFailure, previews, AskUser waiting

Grok tool-event matchers are real regexes; bare `*` failed as match-all.
Install `.*` for Pre/Post tool hooks, add StopFailure for API-error ends,
recognize Grok-native tool input keys, and map ask_user_question PreToolUse
to waiting with interactivePrompt (Kimi-style live card path).

* fix(grok): resolve chat_history under GROK_HOME and long-cwd layouts

Centralize Grok session path helpers so hooks and native-chat honor
GROK_HOME and find chat_history.jsonl by session id when the cwd group
is slug-encoded (encoded name > 255 bytes) instead of only
encodeURIComponent(cwd).

* fix(terminal): keep Kitty keyboard for Grok on Windows ConPTY

Local Windows ConPTY withholds KKP so CSI-u-blind CLIs (e.g. Antigravity)
keep Enter/nav working (#2434). Grok needs KKP for Ctrl+Enter interject and
modified-Enter newline chords; blanking the advertisement for Orca-launched
Grok left those actions broken.

- Prefer KKP when tuiAgent is grok despite ConPTY withhold
- Wire launchAgent from tab/startup into keyboard protocol options

* fix(grok): restore OSC52 default-off, split decoders, honor GROK_HOME hooks

- Keep terminalAllowOsc52Clipboard default false (clipboard exfil risk)
- Split transcript-line-decoders under max-lines without suppressions
- Install local Grok hooks under resolveGrokHomeDir() / GROK_HOME

* refactor(grok): share CLI home resolution

* fix(grok): harden terminal and native chat integration

* test(grok): align CI coverage with native chat support

---------

Co-authored-by: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com>
2026-07-10 13:16:55 -07:00

321 lines
9.8 KiB
TypeScript

import { isShellProcess } from './agent-detection'
import {
getAgentResumeArgv,
type AgentProviderSessionMetadata,
type ResumableTuiAgent,
type SleepingAgentLaunchConfig
} from './agent-session-resume'
import {
clearEnvCommand,
commandSeparator,
planAgentCliArgsSuffix,
quoteStartupArg,
resolveStartupShell,
type AgentStartupShell
} from './tui-agent-startup-shell'
import { getTuiAgentLaunchCommand, TUI_AGENT_CONFIG } from './tui-agent-config'
import type { StartupCommandDelivery } from './codex-startup-delivery'
import type { TuiAgent } from './types'
const WIN32_INLINE_DRAFT_LIMIT_CHARS = 24_000
export type AgentStartupPlan = {
agent: TuiAgent
launchCommand: string
expectedProcess: string
followupPrompt: string | null
launchConfig: SleepingAgentLaunchConfig
launchToken?: string
draftPrompt?: string | null
env?: Record<string, string>
startupCommandDelivery?: StartupCommandDelivery
}
function resolveBaseCommand(args: {
agent: TuiAgent
cmdOverrides: Partial<Record<TuiAgent, string>>
platform: NodeJS.Platform
shell: AgentStartupShell
agentArgs?: string | null
isRemote?: boolean
}): { ok: true; command: string } | { ok: false; error: string } {
const override = args.cmdOverrides[args.agent]
const command =
override ||
getTuiAgentLaunchCommand(TUI_AGENT_CONFIG[args.agent], args.platform, {
isRemote: args.isRemote
})
const suffix = planAgentCliArgsSuffix(args.agentArgs, args.shell)
if (!suffix.ok) {
return suffix
}
// Why: Codex status hooks live in Orca's runtime CODEX_HOME; adding
// --profile-v2 makes Codex load a second hook representation and warn.
return { ok: true, command: suffix.suffix ? `${command} ${suffix.suffix}` : command }
}
function buildSleepingAgentLaunchConfig(args: {
agentCommand?: string | null
agentArgs?: string | null
agentEnv?: Record<string, string> | null
}): SleepingAgentLaunchConfig {
return {
...(args.agentCommand?.trim() ? { agentCommand: args.agentCommand } : {}),
agentArgs: args.agentArgs ?? '',
// Why: startupPlan.env may include prompt transport or pane identity env; the
// durable resume snapshot is limited to Orca-managed agent env inputs.
agentEnv: args.agentEnv ? { ...args.agentEnv } : {}
}
}
export function buildAgentStartupPlan(args: {
agent: TuiAgent
prompt: string
cmdOverrides: Partial<Record<TuiAgent, string>>
platform: NodeJS.Platform
shell?: AgentStartupShell
allowEmptyPromptLaunch?: boolean
agentArgs?: string | null
agentEnv?: Record<string, string> | null
/** Why: SSH remotes deploy the CLI shim as plain `orca`, so the Linux-only
* `orca-ide` rename must be skipped for remote launches. */
isRemote?: boolean
}): AgentStartupPlan | null {
const { agent, prompt, cmdOverrides, platform, allowEmptyPromptLaunch = false } = args
const shell = resolveStartupShell(platform, args.shell)
const trimmedPrompt = prompt.trim()
const config = TUI_AGENT_CONFIG[agent]
const baseCommand = resolveBaseCommand({
agent,
cmdOverrides,
platform,
shell,
agentArgs: args.agentArgs,
isRemote: args.isRemote
})
if (!baseCommand.ok) {
return null
}
const launchConfig = buildSleepingAgentLaunchConfig({
...args,
agentCommand: baseCommand.command
})
if (!trimmedPrompt) {
if (!allowEmptyPromptLaunch) {
return null
}
return {
agent,
launchCommand: baseCommand.command,
expectedProcess: config.expectedProcess,
followupPrompt: null,
launchConfig,
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
}
const quotedPrompt = quoteStartupArg(trimmedPrompt, shell)
if (config.promptInjectionMode === 'argv') {
const promptSeparator = config.argvPromptSeparator ? ` ${config.argvPromptSeparator}` : ''
return {
agent,
launchCommand: `${baseCommand.command}${promptSeparator} ${quotedPrompt}`,
expectedProcess: config.expectedProcess,
followupPrompt: null,
launchConfig,
...(agent === 'codex' ? { startupCommandDelivery: 'shell-ready' as const } : {}),
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
}
if (config.promptInjectionMode === 'flag-prompt') {
return {
agent,
launchCommand: `${baseCommand.command} --prompt ${quotedPrompt}`,
expectedProcess: config.expectedProcess,
followupPrompt: null,
launchConfig,
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
}
if (config.promptInjectionMode === 'flag-prompt-interactive') {
return {
agent,
launchCommand: `${baseCommand.command} --prompt-interactive ${quotedPrompt}`,
expectedProcess: config.expectedProcess,
followupPrompt: null,
launchConfig,
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
}
if (config.promptInjectionMode === 'flag-interactive') {
return {
agent,
launchCommand: `${baseCommand.command} -i ${quotedPrompt}`,
expectedProcess: config.expectedProcess,
followupPrompt: null,
launchConfig,
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
}
return {
agent,
launchCommand: baseCommand.command,
expectedProcess: config.expectedProcess,
followupPrompt: trimmedPrompt,
launchConfig,
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
}
export function buildAgentResumeStartupPlan(args: {
agent: ResumableTuiAgent
providerSession: AgentProviderSessionMetadata
cmdOverrides: Partial<Record<TuiAgent, string>>
platform: NodeJS.Platform
shell?: AgentStartupShell
agentArgs?: string | null
agentEnv?: Record<string, string> | null
agentCommand?: string | null
/** Why: see buildAgentStartupPlan — remote launches use the plain `orca` shim. */
isRemote?: boolean
}): AgentStartupPlan | null {
const argv = getAgentResumeArgv(args.agent, args.providerSession)
if (!argv) {
return null
}
const shell = resolveStartupShell(args.platform, args.shell)
const config = TUI_AGENT_CONFIG[args.agent]
const resolvedAgentCommand = args.agentCommand?.trim()
const baseCommand = resolvedAgentCommand
? ({ ok: true, command: resolvedAgentCommand } as const)
: resolveBaseCommand({
agent: args.agent,
cmdOverrides: args.cmdOverrides,
platform: args.platform,
shell,
agentArgs: args.agentArgs,
isRemote: args.isRemote
})
if (!baseCommand.ok) {
return null
}
const launchConfig = buildSleepingAgentLaunchConfig({
...args,
agentCommand: baseCommand.command
})
const resumeArgs = argv
.slice(1)
.map((arg) => quoteStartupArg(arg, shell))
.join(' ')
const launchCommand = resumeArgs ? `${baseCommand.command} ${resumeArgs}` : baseCommand.command
return {
agent: args.agent,
launchCommand,
expectedProcess: config.expectedProcess,
followupPrompt: null,
launchConfig,
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
}
export type AgentDraftLaunchPlan = {
agent: TuiAgent
launchCommand: string
expectedProcess: string
launchConfig: SleepingAgentLaunchConfig
env?: Record<string, string>
startupCommandDelivery?: StartupCommandDelivery
}
function inlineDraftPlanFitsPlatform(
plan: AgentDraftLaunchPlan,
platform: NodeJS.Platform
): boolean {
if (platform !== 'win32') {
return true
}
const envChars = Object.entries(plan.env ?? {}).reduce(
(total, [key, value]) => total + key.length + value.length,
0
)
// Why: Windows CreateProcess/env blocks have tight length ceilings. Large
// generated drafts should use the existing post-ready paste fallback.
return plan.launchCommand.length + envChars <= WIN32_INLINE_DRAFT_LIMIT_CHARS
}
export function buildAgentDraftLaunchPlan(args: {
agent: TuiAgent
draft: string
cmdOverrides: Partial<Record<TuiAgent, string>>
platform: NodeJS.Platform
shell?: AgentStartupShell
agentArgs?: string | null
agentEnv?: Record<string, string> | null
/** Why: see buildAgentStartupPlan — remote launches use the plain `orca` shim. */
isRemote?: boolean
}): AgentDraftLaunchPlan | null {
const { agent, draft, cmdOverrides, platform } = args
const shell = resolveStartupShell(platform, args.shell)
const config = TUI_AGENT_CONFIG[agent]
const trimmed = draft.trim()
if (!trimmed) {
return null
}
const baseCommand = resolveBaseCommand({
agent,
cmdOverrides,
platform,
shell,
agentArgs: args.agentArgs,
isRemote: args.isRemote
})
if (!baseCommand.ok) {
return null
}
const launchConfig = buildSleepingAgentLaunchConfig({
...args,
agentCommand: baseCommand.command
})
let plan: AgentDraftLaunchPlan | null = null
if (config.draftPromptFlag) {
const quoted = quoteStartupArg(trimmed, shell)
plan = {
agent,
launchCommand: `${baseCommand.command} ${config.draftPromptFlag} ${quoted}`,
expectedProcess: config.expectedProcess,
launchConfig,
// Why: native draft flags carry user text on argv and must survive rc-file startup.
...(agent === 'codex' ? { startupCommandDelivery: 'shell-ready' as const } : {}),
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
}
} else if (config.draftPromptEnvVar) {
const clearVar = clearEnvCommand(config.draftPromptEnvVar, shell)
plan = {
agent,
launchCommand: `${baseCommand.command}${commandSeparator(shell)}${clearVar}`,
expectedProcess: config.expectedProcess,
launchConfig,
env: { ...args.agentEnv, [config.draftPromptEnvVar]: trimmed }
}
}
if (!plan || !inlineDraftPlanFitsPlatform(plan, platform)) {
return null
}
return plan
}
export { isShellProcess }
export {
buildShellCommandFromArgv,
planAgentCliArgsSuffix,
quoteStartupArg,
resolveStartupShell
} from './tui-agent-startup-shell'
export type { AgentCliArgsPlan, AgentStartupShell } from './tui-agent-startup-shell'