Improve Codex startup prompt reliability (#5686)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { hasCodexNativeDraftFlag } from './codex-startup-delivery'
|
||||
|
||||
describe('hasCodexNativeDraftFlag', () => {
|
||||
it('matches Codex --prefill option tokens', () => {
|
||||
expect(hasCodexNativeDraftFlag("codex --prefill 'linked issue context'")).toBe(true)
|
||||
expect(hasCodexNativeDraftFlag("codex --model gpt-5 --prefill 'draft'")).toBe(true)
|
||||
})
|
||||
|
||||
it('matches Codex --prefill=value option tokens', () => {
|
||||
expect(hasCodexNativeDraftFlag('codex --prefill=review')).toBe(true)
|
||||
expect(hasCodexNativeDraftFlag("codex --prefill='linked issue context'")).toBe(true)
|
||||
})
|
||||
|
||||
it('does not match quoted prompt text mentioning prefill', () => {
|
||||
expect(hasCodexNativeDraftFlag("codex 'please compare --prefill behavior'")).toBe(false)
|
||||
expect(hasCodexNativeDraftFlag("codex '--prefill=not-an-option'")).toBe(false)
|
||||
})
|
||||
|
||||
it('does not match non-Codex commands', () => {
|
||||
expect(hasCodexNativeDraftFlag("claude --prefill 'review this'")).toBe(false)
|
||||
})
|
||||
|
||||
it('leaves plain Codex and normal Codex arguments on the fast path', () => {
|
||||
expect(hasCodexNativeDraftFlag('codex')).toBe(false)
|
||||
expect(hasCodexNativeDraftFlag('codex --model gpt-5')).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,82 @@
|
||||
import { recognizeAgentProcessFromCommandLine } from './agent-process-recognition'
|
||||
|
||||
export type StartupCommandDelivery = 'fast' | 'shell-ready'
|
||||
|
||||
type CommandToken = {
|
||||
value: string
|
||||
startsQuoted: boolean
|
||||
}
|
||||
|
||||
function tokenizeCommandWithQuoteMetadata(command: string): CommandToken[] {
|
||||
const tokens: CommandToken[] = []
|
||||
let current = ''
|
||||
let inToken = false
|
||||
let startsQuoted = false
|
||||
let quote: '"' | "'" | null = null
|
||||
let escaped = false
|
||||
|
||||
for (let index = 0; index < command.length; index += 1) {
|
||||
const char = command[index]
|
||||
if (escaped) {
|
||||
current += char
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
if (char === '\\' && quote !== "'") {
|
||||
const next = command[index + 1]
|
||||
if (next && (/\s/.test(next) || next === '"' || next === "'" || next === '\\')) {
|
||||
escaped = true
|
||||
inToken = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
if ((char === '"' || char === "'") && quote === null) {
|
||||
if (!inToken) {
|
||||
startsQuoted = true
|
||||
}
|
||||
quote = char
|
||||
inToken = true
|
||||
continue
|
||||
}
|
||||
if (quote === char) {
|
||||
quote = null
|
||||
continue
|
||||
}
|
||||
if (/\s/.test(char) && quote === null) {
|
||||
if (inToken) {
|
||||
tokens.push({ value: current, startsQuoted })
|
||||
current = ''
|
||||
inToken = false
|
||||
startsQuoted = false
|
||||
}
|
||||
continue
|
||||
}
|
||||
current += char
|
||||
inToken = true
|
||||
}
|
||||
|
||||
if (inToken) {
|
||||
tokens.push({ value: current, startsQuoted })
|
||||
}
|
||||
return tokens
|
||||
}
|
||||
|
||||
export function hasCodexNativeDraftFlag(command: string | null | undefined): boolean {
|
||||
if (recognizeAgentProcessFromCommandLine(command)?.agent !== 'codex' || !command) {
|
||||
return false
|
||||
}
|
||||
const tokens = tokenizeCommandWithQuoteMetadata(command)
|
||||
return tokens.some(
|
||||
(token, index) =>
|
||||
index > 0 &&
|
||||
!token.startsQuoted &&
|
||||
(token.value === '--prefill' || token.value.startsWith('--prefill='))
|
||||
)
|
||||
}
|
||||
|
||||
export function shouldUseShellReadyStartupDelivery(args: {
|
||||
command: string | null | undefined
|
||||
startupCommandDelivery?: StartupCommandDelivery
|
||||
}): boolean {
|
||||
return args.startupCommandDelivery === 'shell-ready' || hasCodexNativeDraftFlag(args.command)
|
||||
}
|
||||
@@ -57,6 +57,24 @@ describe('tui agent startup plans', () => {
|
||||
})
|
||||
|
||||
expect(plan?.launchCommand).toBe("codex 'fix it'")
|
||||
expect(plan?.startupCommandDelivery).toBe('shell-ready')
|
||||
})
|
||||
|
||||
it('keeps plain empty Codex startup on the fast delivery path', () => {
|
||||
const plan = buildAgentStartupPlan({
|
||||
agent: 'codex',
|
||||
prompt: '',
|
||||
cmdOverrides: {},
|
||||
platform: 'linux',
|
||||
allowEmptyPromptLaunch: true
|
||||
})
|
||||
|
||||
expect(plan).toEqual({
|
||||
agent: 'codex',
|
||||
launchCommand: 'codex',
|
||||
expectedProcess: 'codex',
|
||||
followupPrompt: null
|
||||
})
|
||||
})
|
||||
|
||||
it('launches Claude without Orca settings injection', () => {
|
||||
@@ -266,6 +284,28 @@ describe('tui agent startup plans', () => {
|
||||
expect(plan?.launchCommand).toBe('omp; unset ORCA_OMP_PREFILL')
|
||||
})
|
||||
|
||||
it('returns null for oversized Windows flag drafts so callers paste after ready', () => {
|
||||
expect(
|
||||
buildAgentDraftLaunchPlan({
|
||||
agent: 'claude',
|
||||
draft: 'x'.repeat(25_000),
|
||||
cmdOverrides: {},
|
||||
platform: 'win32'
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for oversized Windows env-var drafts so callers paste after ready', () => {
|
||||
expect(
|
||||
buildAgentDraftLaunchPlan({
|
||||
agent: 'pi',
|
||||
draft: 'x'.repeat(25_000),
|
||||
cmdOverrides: {},
|
||||
platform: 'win32'
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('launches Devin with stdin-after-start prompt delivery', () => {
|
||||
const plan = buildAgentStartupPlan({
|
||||
agent: 'devin',
|
||||
|
||||
@@ -6,8 +6,11 @@ import {
|
||||
} from './agent-session-resume'
|
||||
import { tokenizeCustomCommandTemplate } from './commit-message-prompt'
|
||||
import { 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
|
||||
@@ -15,6 +18,7 @@ export type AgentStartupPlan = {
|
||||
followupPrompt: string | null
|
||||
draftPrompt?: string | null
|
||||
env?: Record<string, string>
|
||||
startupCommandDelivery?: StartupCommandDelivery
|
||||
}
|
||||
|
||||
export type AgentStartupShell = 'posix' | 'powershell' | 'cmd'
|
||||
@@ -143,6 +147,7 @@ export function buildAgentStartupPlan(args: {
|
||||
launchCommand: `${baseCommand.command} ${quotedPrompt}`,
|
||||
expectedProcess: config.expectedProcess,
|
||||
followupPrompt: null,
|
||||
...(agent === 'codex' ? { startupCommandDelivery: 'shell-ready' as const } : {}),
|
||||
...(args.agentEnv ? { env: { ...args.agentEnv } } : {})
|
||||
}
|
||||
}
|
||||
@@ -229,6 +234,23 @@ export type AgentDraftLaunchPlan = {
|
||||
launchCommand: string
|
||||
expectedProcess: string
|
||||
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: {
|
||||
@@ -256,25 +278,30 @@ export function buildAgentDraftLaunchPlan(args: {
|
||||
if (!baseCommand.ok) {
|
||||
return null
|
||||
}
|
||||
let plan: AgentDraftLaunchPlan | null = null
|
||||
if (config.draftPromptFlag) {
|
||||
const quoted = quoteStartupArg(trimmed, shell)
|
||||
return {
|
||||
plan = {
|
||||
agent,
|
||||
launchCommand: `${baseCommand.command} ${config.draftPromptFlag} ${quoted}`,
|
||||
expectedProcess: config.expectedProcess,
|
||||
// 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 } } : {})
|
||||
}
|
||||
}
|
||||
if (config.draftPromptEnvVar) {
|
||||
} else if (config.draftPromptEnvVar) {
|
||||
const clearVar = clearEnvCommand(config.draftPromptEnvVar, shell)
|
||||
return {
|
||||
plan = {
|
||||
agent,
|
||||
launchCommand: `${baseCommand.command}${commandSeparator(shell)}${clearVar}`,
|
||||
expectedProcess: config.expectedProcess,
|
||||
env: { ...args.agentEnv, [config.draftPromptEnvVar]: trimmed }
|
||||
}
|
||||
}
|
||||
return null
|
||||
if (!plan || !inlineDraftPlanFitsPlatform(plan, platform)) {
|
||||
return null
|
||||
}
|
||||
return plan
|
||||
}
|
||||
|
||||
export { isShellProcess }
|
||||
|
||||
@@ -28,6 +28,7 @@ import type {
|
||||
RepoSourceControlAiOverrides,
|
||||
SourceControlAiSettings
|
||||
} from './source-control-ai-types'
|
||||
import type { StartupCommandDelivery } from './codex-startup-delivery'
|
||||
import type { AgentKind, LaunchSource, RequestKind } from './telemetry-events'
|
||||
import type { SleepingAgentSessionRecord } from './agent-session-resume'
|
||||
import type { ClaudeAgentTeamsMode } from './claude-agent-teams-tmux-compat'
|
||||
@@ -1865,6 +1866,7 @@ export type WorktreeSetupLaunch = {
|
||||
export type WorktreeStartupLaunch = {
|
||||
command: string
|
||||
env?: Record<string, string>
|
||||
startupCommandDelivery?: StartupCommandDelivery
|
||||
telemetry?: { agent_kind: AgentKind; launch_source: LaunchSource; request_kind: RequestKind }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user