Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ The codex backend is newer and narrower than the claude one:
- File changes render as real diff cards, in both the unified and side-by-side
styles. Other tool calls render as plain cards rather than the typed ones
claude gets.
- Either agent can ask you a question, and both use the same picker. A codex
question that offers no options, or asks for a secret, is left unanswered —
the picker chooses between options and cannot take typed input.
- `/sysprompt` and `/mcp` are claude-only and are hidden on codex rather than
offered and inert. Session replay and `/compact` are claude-only too.
- `@path` inserts a path but does not inject the file. Only claude expands an
Expand Down
5 changes: 5 additions & 0 deletions approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ type approvalReq struct {
type approvalReply struct {
allow bool
message string
// answers carries a question's answer as data instead of as denial text, for
// a backend that takes it that way. claude has only the message channel and
// ignores this; codex replies with an answer per question id
// (codexquestion.go).
answers []questionAnswer
}

const (
Expand Down
73 changes: 43 additions & 30 deletions codexapproval.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
// turning a decision back into a JSON-RPC response.

// codexApprovalMethods are the server requests that gate an action, and so can
// be answered with a decision. Anything not on this list is answered with a
// JSON-RPC error instead — inventing a reply for a request whose semantics we
// have not established is worse than declining it plainly.
// be answered with a decision. A request that is neither one of these nor a
// question is answered with a JSON-RPC error instead — inventing a reply for a
// request whose semantics we have not established is worse than declining it
// plainly.
var codexApprovalMethods = map[string]bool{
"item/commandExecution/requestApproval": true,
"item/fileChange/requestApproval": true,
Expand Down Expand Up @@ -62,47 +63,59 @@ func (e *codexEngine) answerServerRequest(f codexFrame) {
if f.ID == nil {
return
}
if !codexApprovalMethods[f.Method] {
// Not an approval. Answer with the JSON-RPC "method not found" code,
// which is a well-defined way to say "this client cannot do that" and
// leaves the server to decide what happens next.
switch {
case f.Method == codexQuestionMethod:
// A question, not a permission (codexquestion.go).
e.askQuestion(*f.ID, f)
case codexApprovalMethods[f.Method]:
e.askUser(*f.ID, f)
default:
// Neither. Answer with the JSON-RPC "method not found" code, which is a
// well-defined way to say "this client cannot do that" and leaves the
// server to decide what happens next.
e.replyError(*f.ID, -32601, "cathode does not implement "+f.Method)
e.emitError("unhandled request " + f.Method)
return
}
e.askUser(*f.ID, f)
}

// askUser puts a gated action in front of the user and answers with what they
// choose.
// ask puts one request in front of the user and answers it with result(reply).
// Every path to the pane goes through here, because the two rules it keeps are
// the ones that hang a turn when one is missed.
//
// All of it runs off the reader goroutine, for two separate reasons. The reader
// must keep draining while the pane is up, or the item events that draw the
// very card being approved never arrive. And approvals are admitted one at a
// time: the UI holds a single pending approval, so a second would overwrite the
// first and leave codex waiting on a reply that can no longer be given.
func (e *codexEngine) askUser(id int64, f codexFrame) {
var p codexApprovalParams
_ = json.Unmarshal(f.Params, &p)

// It runs off the reader goroutine: the reader must keep draining while the
// pane is up, or the item events that draw the very card being decided never
// arrive. And it holds the slot for the whole wait, because the UI has room for
// one request at a time — a second would overwrite the first and leave codex
// waiting on a reply that can no longer be given.
func (e *codexEngine) ask(id int64, req approvalReq, result func(approvalReply) any) {
reply := make(chan approvalReply, 1)
req.reply = reply
go func() {
e.approvalSlot <- struct{}{}
defer func() { <-e.approvalSlot }()

reply := make(chan approvalReply, 1)
e.emitMsg(pendingApprovalMsg{req: approvalReq{
toolName: codexApprovalLabel(f.Method, p),
toolUseID: p.ItemID,
input: f.Params,
reply: reply,
}})
e.emitMsg(pendingApprovalMsg{req: req})
e.replyResult(id, result(<-reply))
}()
}

// askUser puts a gated action in front of the user and answers with the
// decision they made.
func (e *codexEngine) askUser(id int64, f codexFrame) {
var p codexApprovalParams
_ = json.Unmarshal(f.Params, &p)

e.ask(id, approvalReq{
toolName: codexApprovalLabel(f.Method, p),
toolUseID: p.ItemID,
input: f.Params,
}, func(r approvalReply) any {
decision := codexRefusal
if (<-reply).allow {
if r.allow {
decision = codexApproval
}
e.replyResult(id, map[string]any{"decision": decision})
}()
return map[string]any{"decision": decision}
})
}

// codexApprovalLabel names the action on the approval bar. The command itself
Expand Down
118 changes: 118 additions & 0 deletions codexquestion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2026 Triple Down AB
// SPDX-License-Identifier: Apache-2.0

package main

import "encoding/json"

// ---- codex asks the user a question ----
//
// item/tool/requestUserInput is codex's analogue of claude's AskUserQuestion,
// and it carries the same thing: a list of questions, each offering labelled
// options. So this translates the request into the shape question.go already
// parses, and the chosen labels back into the result codex expects. The picker,
// the one-question-at-a-time flow and the transcript note are all shared.
//
// Two differences shape the code below. codex keys each answer by a question
// id rather than by position, so the id rides out with the question and back
// with the answer. And a codex question may offer no options at all, or be
// marked secret; the picker can do neither, so those questions are left out of
// the answer map. codex reads a missing id as unanswered, which is exactly what
// happened.
//
// Two schema fields are narrowed away on purpose. isOther offers a free-form
// answer beside the options, which the picker has no way to take — the same
// narrowing it already applies to claude's questions. And isBlocking is unread:
// answering is correct either way, and it is not answering that stops a turn.

const codexQuestionMethod = "item/tool/requestUserInput"

type codexQuestionOption struct {
Label string `json:"label"`
Description string `json:"description"`
}

type codexQuestion struct {
ID string `json:"id"`
Header string `json:"header"`
Question string `json:"question"`
Options []codexQuestionOption `json:"options"`
IsSecret bool `json:"isSecret"`
}

type codexQuestionParams struct {
ItemID string `json:"itemId"`
Questions []codexQuestion `json:"questions"`
}

// codexAskable reports whether cathode can put this question to the user. It
// needs an id to file the answer under, and at least one option to choose from.
// A secret answer is refused whatever it offers, because the picker prints the
// choice into the transcript.
func codexAskable(q codexQuestion) bool {
return q.ID != "" && len(q.Options) > 0 && !q.IsSecret
}

// codexAskInput translates the askable questions into the AskUserQuestion input
// that question.go parses. It reports false when none of them are askable,
// which tells the caller to answer with nothing rather than open an empty
// picker.
func codexAskInput(p codexQuestionParams) (json.RawMessage, bool) {
var in askInput
for _, q := range p.Questions {
if !codexAskable(q) {
continue
}
opts := make([]askOption, 0, len(q.Options))
for _, o := range q.Options {
opts = append(opts, askOption{Label: o.Label, Description: o.Description})
}
in.Questions = append(in.Questions, askQuestion{
ID: q.ID, Question: q.Question, Header: q.Header, Options: opts,
})
}
if len(in.Questions) == 0 {
return nil, false
}
b, err := json.Marshal(in)
if err != nil {
return nil, false
}
return b, true
}

// codexQuestionResult builds the reply: each answered question id mapped to the
// labels chosen for it. An unanswered id is absent rather than empty.
func codexQuestionResult(answers []questionAnswer) map[string]any {
out := map[string]any{}
for _, a := range answers {
if a.id == "" || len(a.labels) == 0 {
continue
}
out[a.id] = map[string]any{"answers": a.labels}
}
return map[string]any{"answers": out}
}

// askQuestion puts codex's question to the user and answers with what they
// chose. It goes to the pane through ask, like an approval does.
func (e *codexEngine) askQuestion(id int64, f codexFrame) {
var p codexQuestionParams
_ = json.Unmarshal(f.Params, &p)

input, ok := codexAskInput(p)
if !ok {
// Nothing here cathode can ask. Answer with no answers, which is the
// dismissed-question case, and say why — a question that never appears
// reads as a bug in the pane rather than a limit of it.
e.replyResult(id, codexQuestionResult(nil))
e.emitError("unanswered question: cathode can only answer one that offers options and is not secret")
return
}

e.ask(id, approvalReq{
toolName: askUserQuestionTool,
toolUseID: p.ItemID,
input: input,
}, func(r approvalReply) any { return codexQuestionResult(r.answers) })
}
Loading
Loading