@@ -72,14 +72,16 @@ var hookTimeout = defaults.ExecHookTimeout |
72 | 72 | // |
73 | 73 | // `Prompt` is prompt text with @placeholders, sent to the configured model |
74 | 74 | // provider (prompt_hooks.go). Its implemented effects are advise (any |
75 | | -// phase) and propose (the outcome phases only; #11141). |
| 75 | +// phase), propose (the outcome phases only; #11141) and ask (the |
| 76 | +// before phase only, fail-closed; #15809). |
76 | 77 | type Hook struct { |
77 | 78 | Phase string `json:"phase"` |
78 | 79 | Script string `json:"script"` |
79 | 80 | // Prompt is the prompt action's text. Mutually exclusive with Script. |
80 | 81 | Prompt string `json:"prompt,omitempty"` |
81 | 82 | // Effect declares what the hook's output may do. For a prompt hook it |
82 | | - // must be empty (advise), advise, or propose at an outcome phase; |
| 83 | + // must be empty (advise), advise, propose at an outcome phase, or |
| 84 | + // ask at the before phase; |
83 | 85 | // for a script hook it must agree with the phase's fixed effect when |
84 | 86 | // set. Anything else is refused at validation rather than silently |
85 | 87 | // ignored. |
@@ -114,7 +116,12 @@ type HookFrame struct { |
114 | 116 | // propose), carried beside the original result or error and |
115 | 117 | // never applied by mcpx (0002). Empty for every other hook. |
116 | 118 | Propose json.RawMessage `json:"propose,omitempty"` |
117 | | - Error string `json:"error,omitempty"` |
| 119 | + // Ask is an ask prompt hook's question, raised through the |
| 120 | + // broker (effect ask, #15809). Refuse is its structured refusal |
| 121 | + // reason. Empty for every other hook. |
| 122 | + Ask string `json:"ask,omitempty"` |
| 123 | + Refuse string `json:"refuse,omitempty"` |
| 124 | + Error string `json:"error,omitempty"` |
118 | 125 | // Model and Provider name what answered a prompt hook, as the model |
119 | 126 | // layer reports them. Empty for script hooks. |
120 | 127 | Model string `json:"model,omitempty"` |
@@ -184,12 +191,16 @@ func validateHooks(hooks []Hook, origin string) error { |
184 | 191 | if hasPrompt { |
185 | 192 | switch h.Effect { |
186 | 193 | case "", hookEffectAdvise: |
| 194 | + case hookEffectAsk: |
| 195 | + if h.Phase != HookBefore { |
| 196 | + return fmt.Errorf("exec: %s hook %d (%s): prompt effect %q is only available at %s: asking is a gate on an operation that has not run, and at %s the operation is already complete (0002)", origin, i, h.Phase, h.Effect, HookBefore, h.Phase) |
| 197 | + } |
187 | 198 | case hookEffectPropose: |
188 | 199 | if h.Phase != HookOnSuccess && h.Phase != HookOnError { |
189 | 200 | return fmt.Errorf("exec: %s hook %d (%s): prompt effect %q is only available at %s and %s: a proposal needs a result or an error to propose against, and %s is clamped to at most %q (0001)", origin, i, h.Phase, h.Effect, HookOnSuccess, HookOnError, h.Phase, hookEffectAdvise) |
190 | 201 | } |
191 | 202 | default: |
192 | | - return fmt.Errorf("exec: %s hook %d (%s): prompt effect %q is not implemented (only %q and %q are implemented for prompt hooks; the other dial levels stay with #1929)", origin, i, h.Phase, h.Effect, hookEffectAdvise, hookEffectPropose) |
| 203 | + return fmt.Errorf("exec: %s hook %d (%s): prompt effect %q is not implemented (only %q, %q and %q are implemented for prompt hooks; the other dial levels stay with #1929)", origin, i, h.Phase, h.Effect, hookEffectAdvise, hookEffectPropose, hookEffectAsk) |
193 | 204 | } |
194 | 205 | } |
195 | 206 | if hasScript && h.Effect != "" && h.Effect != effectFor(h.Phase) { |
@@ -233,6 +244,10 @@ type hookRun struct { |
233 | 244 | // Failed is true when the hook itself did not run or did not produce a |
234 | 245 | // JSON object. It is a result, not an error: the run carries on. |
235 | 246 | Failed bool |
| 247 | + // Refusal is non-empty only for an ask prompt hook that refused |
| 248 | + // the run (#15809): RunWith honours it at the before phase by |
| 249 | + // never starting the script, with this text as the run's error. |
| 250 | + Refusal string |
236 | 251 | } |
237 | 252 |
|
238 | 253 | // runHooks executes every hook of one phase and returns what they produced. |
@@ -262,7 +277,9 @@ func (s *Service) runHooks(ctx context.Context, phase string, hooks []Hook, hctx |
262 | 277 | // A prompt hook never transforms the outcome: advise and |
263 | 278 | // propose both ride in its frame, and applyHookResult is |
264 | 279 | // never consulted for it, so it cannot change the outcome |
265 | | - // whatever phase it runs in. |
| 280 | + // whatever phase it runs in. An ask hook's refusal rides |
| 281 | + // on the hookRun instead: RunWith honours it at the |
| 282 | + // before phase by never starting the script. |
266 | 283 | run = s.runPromptHook(ctx, phase, h, hctx, ropts, res) |
267 | 284 | } else { |
268 | 285 | run = s.runOneHook(ctx, phase, h, hctx, ropts, res, emit) |
|