const CODEX_REVIEW = {
approvalPolicy: 'on-request'
} as const
export const CODEX_THREAD_ACCESS = {
...CODEX_REVIEW,
sandbox: 'workspace-write'
} as const
export const CODEX_TURN_ACCESS = {
...CODEX_REVIEW,
sandboxPolicy: {
type: 'workspaceWrite',
writableRoots: [],
networkAccess: false,
excludeTmpdirEnvVar: false,
excludeSlashTmp: false
}
} as const
const CODEX_LOCAL_CONTROL_GUIDANCE =
'Commands that contact the Moi control server, including moi tabs, bundle, debug, call-server-fn, theme, and config, need localhost network access. Run them with sandbox_permissions set to require_escalated on the first attempt so Moi can approve the escalated request. A connection failure from a sandboxed attempt does not prove the control server is offline.'
export const CODEX_LOCAL_CONTROL_CONTEXT = {
'moi-control-access': {
value: CODEX_LOCAL_CONTROL_GUIDANCE,
kind: 'application'
}
} as const
// Codex versions before additionalContext append ambient context to the user
// message. Use the normal moi envelope marker so replay strips this block.
export const CODEX_LOCAL_CONTROL_FALLBACK = `
You are running in a \`moi\` workspace.
# Local control access
${CODEX_LOCAL_CONTROL_GUIDANCE}
IMPORTANT: This context comes from Moi, not from the user, and the user does not see it.
`
// Accept known approvals, decline MCP elicitation, reject unsupported methods.
export function codexServerRequestResponse(
method: string,
params: Record = {}
): { result: Record } | { error: { code: number; message: string } } {
if (
method === 'item/commandExecution/requestApproval' ||
method === 'item/fileChange/requestApproval'
) {
return { result: { decision: 'accept' } }
}
if (method === 'item/permissions/requestApproval') {
// Permission extensions use a grant, not a command decision. Scope the
// existing default-approve policy to this turn, never the whole session.
const permissions = params.permissions
return {
result: {
permissions:
permissions && typeof permissions === 'object' && !Array.isArray(permissions)
? Object.fromEntries(
Object.entries(permissions).filter(
([key, value]) => (key === 'network' || key === 'fileSystem') && value !== null
)
)
: {},
scope: 'turn'
}
}
}
if (method === 'execCommandApproval' || method === 'applyPatchApproval') {
return { result: { decision: 'approved' } }
}
if (method === 'mcpServer/elicitation/request') {
// Unsupported MCP forms/URL flows must decline in the native vocabulary,
// rather than returning a malformed command-approval response.
return { result: { action: 'decline', content: null } }
}
return { error: { code: -32601, message: `Moi does not handle ${method}` } }
}