Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(agent): select an offered ACP permission option so Hermes writes aren't denied (MUL-4441) (#5351)
Bohan Jiang7a1a2a9de419
diff --git a/server/pkg/agent/hermes.go b/server/pkg/agent/hermes.goindex 7cf5c193a8..f28b44713c 100644--- a/server/pkg/agent/hermes.go+++ b/server/pkg/agent/hermes.go@@ -555,10 +555,12 @@ func (c *hermesClient) handleLine(line string) {}// Agent → client request: has id + method (no result / error yet).- // Kimi uses this for session/request_permission; if we don't answer,- // the agent blocks for 300s and the task hangs. Hermes doesn't send- // these when launched with HERMES_YOLO_MODE=1, but we still handle- // the case generically for any future ACP backend we bolt on.+ // Kimi and Hermes both use session/request_permission; if we don't+ // answer, the agent blocks for its internal timeout and the task+ // hangs. HERMES_YOLO_MODE=1 only suppresses Hermes' dangerous-shell-+ // command prompts (tools/approval.py); its ACP edit-approval guard+ // (acp_adapter/edit_approval.py) still asks before every file write,+ // so we must handle these requests for Hermes too.if _, hasID := raw["id"]; hasID {if _, hasResult := raw["result"]; hasResult {c.handleResponse(raw)@@ -583,10 +585,22 @@ func (c *hermesClient) handleLine(line string) {// handleAgentRequest replies to JSON-RPC requests the agent sends// us (agent → client direction). The only one we care about today is// `session/request_permission`: the daemon is headless and cannot-// actually prompt a user, so we auto-approve every action. Using-// `approve_for_session` rather than `approve` means subsequent-// identical actions (every Shell invocation, every file write) don't-// round-trip through us — the agent remembers them locally.+// actually prompt a user, so we answer it ourselves — granting when a+// safe option is offered, otherwise declining just this action or+// failing closed (see below).+//+// The reply MUST select one of the optionIds the agent actually+// offered — the ACP permission contract is "pick from these options",+// and an id the agent never offered is treated as a denial. Hermes'+// edit-approval path offers only ["allow_once","deny"] and rejects+// anything but exactly "allow_once" (acp_adapter/edit_approval.py), so+// the previous hardcoded "approve_for_session" silently blocked every+// file write on the Hermes ACP runtime (GitHub multica#5300).+// selectACPPermissionOption picks an option the agent offered — a safe+// grant when one exists, otherwise an offered single-use reject to deny+// just this action — and we fail closed with a protocol error when the+// request offers nothing safely selectable, never a permanent grant or a+// whole-turn "cancelled".func (c *hermesClient) handleAgentRequest(raw map[string]json.RawMessage) {var method string_ = json.Unmarshal(raw["method"], &method)@@ -599,17 +613,44 @@ func (c *hermesClient) handleAgentRequest(raw map[string]json.RawMessage) {var resp map[string]anyswitch method {case "session/request_permission":- resp = map[string]any{- "jsonrpc": "2.0",- "id": json.RawMessage(rawID),- "result": map[string]any{- "outcome": map[string]any{- "outcome": "selected",- "optionId": "approve_for_session",+ optionID, grant, ok := selectACPPermissionOption(raw["params"])+ if ok {+ // Select an offered option — either a safe grant (approve) or,+ // when no safe grant exists, an offered reject_once (deny THIS+ // action). Both are ACP "selected" outcomes; we deliberately do+ // NOT reply "cancelled" here, which means the whole prompt turn+ // was cancelled — other ACP backends sharing this client (kimi,+ // kiro, ...) would abort the entire task, not just this action.+ resp = map[string]any{+ "jsonrpc": "2.0",+ "id": json.RawMessage(rawID),+ "result": map[string]any{+ "outcome": map[string]any{+ "outcome": "selected",+ "optionId": optionID,+ },},- },+ }+ if grant {+ c.cfg.Logger.Debug("auto-approved agent permission request", "method", method, "optionId", optionID)+ } else {+ c.cfg.Logger.Warn("no safe grant offered; selecting offered reject option", "method", method, "optionId", optionID)+ }+ } else {+ // The request offered nothing we can safely select: no safe grant+ // and no single-use reject_once (empty, malformed, permanent-only,+ // or reject_always-only). Return a protocol error rather than+ // fabricate an un-offered id or a whole-turn "cancelled".+ resp = map[string]any{+ "jsonrpc": "2.0",+ "id": json.RawMessage(rawID),+ "error": map[string]any{+ "code": -32603,+ "message": "no auto-selectable permission option offered",+ },+ }+ c.cfg.Logger.Warn("no safely selectable permission option offered; returning error", "method", method)}- c.cfg.Logger.Debug("auto-approved agent permission request", "method", method)default:// Unknown agent→client method — reply with standard "method// not found" so the agent doesn't block waiting for us. Better@@ -636,6 +677,102 @@ func (c *hermesClient) handleAgentRequest(raw map[string]json.RawMessage) {}}+// acpPermissionOption is one entry in a session/request_permission+// request's `options` array. `kind` is the ACP-level classification+// (allow_once / allow_always / reject_once / reject_always); `optionId`+// is the agent-defined opaque string we echo back to select that option.+type acpPermissionOption struct {+ OptionID string `json:"optionId"`+ Kind string `json:"kind"`+}++// ACP v1 PermissionOptionKind values. Only the two allow kinds grant;+// any other or unknown kind is treated as non-granting so a future or+// abnormal kind can never be auto-approved. reject_once denies a single+// action (the only reject we auto-select — reject_always would persist a+// denial the way allow_always persists a grant).+// https://agentclientprotocol.com/protocol/v1/schema#permissionoptionkind+const (+ acpKindAllowOnce = "allow_once"+ acpKindAllowAlways = "allow_always"+ acpKindRejectOnce = "reject_once"+)++// acpSessionScopedOptionIDs are optionIds known to grant for the current+// session only, without persisting a decision. Both Hermes' "allow_session"+// and its permanent "allow_always" option carry ACP kind "allow_always"+// (ACP has no session-scoped kind), so kind alone cannot tell them apart —+// we recognise the session-scoped ones by id. "approve_for_session" is the+// equivalent id other ACP backends use.+var acpSessionScopedOptionIDs = []string{"allow_session", "approve_for_session"}++// selectACPPermissionOption decides how to auto-answer a+// session/request_permission. It returns the offered optionId to select,+// whether that selection grants (true) or denies (false) the action, and+// ok=false when the request offers nothing safely selectable (the caller+// then returns a protocol error rather than fabricate an outcome).+//+// It only ever returns an id the agent actually offered. Per review of+// GitHub multica#5300 it refuses to auto-select a permanent "allow_always"+// grant — on Hermes that persists to the runtime owner's on-disk allowlist+// and would outlive the task (ACP v1 allow_always "remembers the choice").+// Grant nature is decided purely by the explicit ACP kind, never by the+// opaque optionId, so unknown kinds fail closed. Order of preference:+//+// 1. a known session-scoped grant id;+// 2. any single-use (kind=allow_once) grant;+// 3. an offered single-use reject_once — deny just this action rather than+// reply "cancelled", which other ACP backends read as cancelling the+// whole prompt turn.+func selectACPPermissionOption(params json.RawMessage) (optionID string, grant bool, ok bool) {+ var p struct {+ Options []acpPermissionOption `json:"options"`+ }+ if len(params) > 0 {+ if err := json.Unmarshal(params, &p); err != nil {+ return "", false, false+ }+ }++ // 1. A known session-scoped grant id, if actually offered with a grant kind.+ for _, want := range acpSessionScopedOptionIDs {+ for _, opt := range p.Options {+ if opt.OptionID == want && isACPGrantKind(opt.Kind) {+ return opt.OptionID, true, true+ }+ }+ }+ // 2. Any single-use grant. kind=allow_once is inherently scoped to this+ // one action, so it is safe regardless of the (opaque) optionId — this+ // also covers agents that use non-standard option ids.+ for _, opt := range p.Options {+ if opt.OptionID != "" && strings.EqualFold(strings.TrimSpace(opt.Kind), acpKindAllowOnce) {+ return opt.OptionID, true, true+ }+ }+ // 3. No safe grant: deny THIS action by selecting an offered reject_once.+ for _, opt := range p.Options {+ if opt.OptionID != "" && strings.EqualFold(strings.TrimSpace(opt.Kind), acpKindRejectOnce) {+ return opt.OptionID, false, true+ }+ }+ // 4. Nothing safely selectable (empty, malformed, permanent-only, or+ // reject_always-only). Signal the caller to return a protocol error.+ return "", false, false+}++// isACPGrantKind reports whether an ACP PermissionOptionKind grants the+// action. Only the two current allow kinds qualify; every other or unknown+// value is non-granting, so grant detection fails closed.+func isACPGrantKind(kind string) bool {+ switch strings.ToLower(strings.TrimSpace(kind)) {+ case acpKindAllowOnce, acpKindAllowAlways:+ return true+ default:+ return false+ }+}+// acpRPCError is a JSON-RPC error frame returned by the agent process.// It renders exactly like the flat string handleResponse used to build// with fmt.Errorf, so logs and surfaced task errors are unchanged, butdiff --git a/server/pkg/agent/hermes_test.go b/server/pkg/agent/hermes_test.goindex 0605e3eae6..6d4041fb92 100644--- a/server/pkg/agent/hermes_test.go+++ b/server/pkg/agent/hermes_test.go@@ -578,49 +578,161 @@ func (b *bufferWriter) String() string {return b.buf.String()}-// TestHermesClientAutoApprovesPermissionRequest asserts that when an-// ACP agent sends us `session/request_permission` (kimi does this on-// every Shell / file-mutating tool call), the client replies with-// `approve_for_session` — without this the agent blocks 300s and the-// task hangs. The id in the reply must match the agent's request id-// so its in-flight future resolves.+// TestHermesClientAutoApprovesPermissionRequest asserts how the client+// answers an ACP agent's `session/request_permission`. It must select an+// optionId the agent *actually offered* — an id the agent never offered is+// treated as a denial and, on Hermes' edit path, silently blocks every file+// write (GitHub multica#5300). It prefers a session-scoped grant over a+// single-use one, refuses to auto-select a permanent "allow_always" (which+// persists to the runtime owner's on-disk allowlist), denies a single action+// by selecting an offered reject_once rather than replying "cancelled" (which+// other ACP backends read as cancelling the whole turn), and returns a+// protocol error when nothing is safely selectable. The reply always echoes+// the agent's request id.func TestHermesClientAutoApprovesPermissionRequest(t *testing.T) {t.Parallel()- w := &bufferWriter{}- c := &hermesClient{- cfg: Config{Logger: slog.Default()},- stdin: w,- pending: make(map[int]*pendingRPC),+ cases := []struct {+ name string+ options string+ wantErr bool // true → JSON-RPC error reply, no outcome+ wantID string // expected selected optionId when wantErr == false+ }{+ {+ // Hermes edit-approval offers only these two and requires+ // exactly "allow_once"; this is the multica#5300 regression+ // the old hardcoded "approve_for_session" reply broke.+ name: "hermes edit approval selects allow_once",+ options: `[{"optionId":"allow_once","name":"Allow edit","kind":"allow_once"},{"optionId":"deny","name":"Deny","kind":"reject_once"}]`,+ wantID: "allow_once",+ },+ {+ // Hermes command approval offers a session option; prefer it over+ // both the permanent "allow_always" and single-use "allow_once".+ name: "command approval prefers session over permanent",+ options: `[{"optionId":"allow_once","kind":"allow_once"},{"optionId":"allow_session","kind":"allow_always"},{"optionId":"allow_always","kind":"allow_always"},{"optionId":"deny","kind":"reject_once"},{"optionId":"deny_always","kind":"reject_always"}]`,+ wantID: "allow_session",+ },+ {+ // Other ACP agents' session-scoped id is honoured when offered.+ name: "session-scoped id honoured",+ options: `[{"optionId":"approve","kind":"allow_once"},{"optionId":"approve_for_session","kind":"allow_always"},{"optionId":"reject","kind":"reject_once"}]`,+ wantID: "approve_for_session",+ },+ {+ // Grant nature comes from the ACP kind, not the opaque optionId:+ // a single-use option with a non-standard id is still selected.+ name: "non-standard single-use id selected by kind",+ options: `[{"optionId":"yolo-42","kind":"allow_once"},{"optionId":"nope","kind":"reject_once"}]`,+ wantID: "yolo-42",+ },+ {+ // Only a permanent grant offered: never auto-persist it; deny this+ // action by selecting the offered single-use reject instead.+ name: "permanent grant refused, offered reject_once selected",+ options: `[{"optionId":"allow_always","kind":"allow_always"},{"optionId":"deny","kind":"reject_once"}]`,+ wantID: "deny",+ },+ {+ // No grant at all: deny this action via the offered reject_once.+ name: "reject-only selects reject_once",+ options: `[{"optionId":"deny","kind":"reject_once"},{"optionId":"deny_always","kind":"reject_always"}]`,+ wantID: "deny",+ },+ {+ // Unknown / future kind is not a grant; fall through to reject_once.+ name: "unknown kind selects offered reject_once",+ options: `[{"optionId":"allow_forever","kind":"allow_super"},{"optionId":"deny","kind":"reject_once"}]`,+ wantID: "deny",+ },+ {+ // Permanent grant with no single-use reject: nothing safely+ // selectable → protocol error, not a fabricated outcome.+ name: "permanent-only without reject_once errors",+ options: `[{"optionId":"allow_always","kind":"allow_always"}]`,+ wantErr: true,+ },+ {+ // Only a permanent reject offered: we don't auto-persist a denial+ // either → protocol error.+ name: "reject_always-only errors",+ options: `[{"optionId":"deny_always","kind":"reject_always"}]`,+ wantErr: true,+ },+ {+ // No options at all: protocol error rather than fabricate a reply.+ name: "empty options errors",+ options: `[]`,+ wantErr: true,+ },+ {+ // Malformed options payload (not an array): protocol error.+ name: "malformed options errors",+ options: `"not-an-array"`,+ wantErr: true,+ },}- c.handleLine(`{"jsonrpc":"2.0","id":42,"method":"session/request_permission","params":{"sessionId":"ses_1","options":[{"optionId":"approve","name":"Approve once","kind":"allow_once"},{"optionId":"approve_for_session","name":"Approve for this session","kind":"allow_always"},{"optionId":"reject","name":"Reject","kind":"reject_once"}],"toolCall":{"toolCallId":"tc_1","title":"Shell","content":[]}}}`)+ for _, tc := range cases {+ tc := tc+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()- got := w.String()- var resp struct {- JSONRPC string `json:"jsonrpc"`- ID int `json:"id"`- Result struct {- Outcome struct {- Outcome string `json:"outcome"`- OptionID string `json:"optionId"`- } `json:"outcome"`- } `json:"result"`- }- if err := json.Unmarshal([]byte(strings.TrimSpace(got)), &resp); err != nil {- t.Fatalf("reply is not valid JSON: %q err=%v", got, err)- }- if resp.JSONRPC != "2.0" {- t.Errorf("jsonrpc: got %q, want 2.0", resp.JSONRPC)- }- if resp.ID != 42 {- t.Errorf("id: got %d, want 42 (must echo agent's request id)", resp.ID)- }- if resp.Result.Outcome.Outcome != "selected" {- t.Errorf("outcome.outcome: got %q, want %q", resp.Result.Outcome.Outcome, "selected")- }- if resp.Result.Outcome.OptionID != "approve_for_session" {- t.Errorf("outcome.optionId: got %q, want %q", resp.Result.Outcome.OptionID, "approve_for_session")+ w := &bufferWriter{}+ c := &hermesClient{+ cfg: Config{Logger: slog.Default()},+ stdin: w,+ pending: make(map[int]*pendingRPC),+ }++ c.handleLine(`{"jsonrpc":"2.0","id":42,"method":"session/request_permission","params":{"sessionId":"ses_1","options":` + tc.options + `,"toolCall":{"toolCallId":"tc_1","title":"write: reply.md","content":[]}}}`)++ got := w.String()+ var resp struct {+ JSONRPC string `json:"jsonrpc"`+ ID int `json:"id"`+ Result *struct {+ Outcome struct {+ Outcome string `json:"outcome"`+ OptionID string `json:"optionId"`+ } `json:"outcome"`+ } `json:"result"`+ Error *struct {+ Code int `json:"code"`+ Message string `json:"message"`+ } `json:"error"`+ }+ if err := json.Unmarshal([]byte(strings.TrimSpace(got)), &resp); err != nil {+ t.Fatalf("reply is not valid JSON: %q err=%v", got, err)+ }+ if resp.JSONRPC != "2.0" {+ t.Errorf("jsonrpc: got %q, want 2.0", resp.JSONRPC)+ }+ if resp.ID != 42 {+ t.Errorf("id: got %d, want 42 (must echo agent's request id)", resp.ID)+ }+ if tc.wantErr {+ if resp.Error == nil {+ t.Errorf("want a JSON-RPC error reply, got result %+v", resp.Result)+ }+ if resp.Result != nil {+ t.Errorf("error reply must not carry a result, got %+v", resp.Result)+ }+ return+ }+ if resp.Error != nil {+ t.Fatalf("unexpected JSON-RPC error reply: %+v", resp.Error)+ }+ if resp.Result == nil {+ t.Fatalf("want a selected outcome, got no result: %q", got)+ }+ if resp.Result.Outcome.Outcome != "selected" {+ t.Errorf("outcome.outcome: got %q, want %q", resp.Result.Outcome.Outcome, "selected")+ }+ if resp.Result.Outcome.OptionID != tc.wantID {+ t.Errorf("outcome.optionId: got %q, want %q", resp.Result.Outcome.OptionID, tc.wantID)+ }+ })}}diff --git a/server/pkg/agent/kimi.go b/server/pkg/agent/kimi.goindex 3ccc476d35..0402e7605f 100644--- a/server/pkg/agent/kimi.go+++ b/server/pkg/agent/kimi.go@@ -53,8 +53,9 @@ func (b *kimiBackend) Execute(ctx context.Context, prompt string, opts ExecOptio// `kimi acp` ignores --yolo / --auto-approve (they're flags on the// root `kimi` command, not on the `acp` subcommand). Instead, the- // daemon auto-approves in hermesClient.handleAgentRequest by replying- // "approve_for_session" to every session/request_permission request.+ // daemon auto-approves in hermesClient.handleAgentRequest by selecting+ // a safe granting option the agent offered (see+ // selectACPApprovalOptionID) for each session/request_permission request.kimiArgs := append([]string{"acp"}, filterCustomArgs(opts.CustomArgs, kimiBlockedArgs, b.cfg.Logger)...)cmd := exec.CommandContext(runCtx, execPath, kimiArgs...)hideAgentWindow(cmd)