Repository navigation
feat(mcpx): prompt-action advise hooks for exec (#5276) #9021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
75b8c65
fd6d1e2
e819507
db22dac
9b39a59
829d328
037cc53
38a9fd0
b022e30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,10 @@ import ( | |
| "time" | ||
|
|
||
| "github.com/developing-today/code/pkgs/mcpx/internal/artifacts" | ||
| "github.com/developing-today/code/pkgs/mcpx/internal/config" | ||
| "github.com/developing-today/code/pkgs/mcpx/internal/execsvc" | ||
| "github.com/developing-today/code/pkgs/mcpx/internal/model" | ||
| "github.com/developing-today/code/pkgs/mcpx/internal/settings" | ||
| ) | ||
|
|
||
| // execOptions is what the exec and run commands resolved from their flags, | ||
|
|
@@ -172,7 +175,7 @@ func (a *App) localStore() (*artifacts.Store, error) { | |
| // has launchers, phases and a typecheck mode that the wire Options | ||
| // deliberately do not mirror. Everything after the process starts is the | ||
| // shared implementation. | ||
| func (a *App) localService(store *artifacts.Store, socket string) *execsvc.Service { | ||
| func (a *App) localService(store *artifacts.Store, socket string, cfg *config.Config) *execsvc.Service { | ||
| set := a.Settings() | ||
| lim := execsvc.DefaultLimits() | ||
| lim.Interpreter = set.String("script.interpreter") | ||
|
|
@@ -182,7 +185,7 @@ func (a *App) localService(store *artifacts.Store, socket string) *execsvc.Servi | |
| lim.ChunkBytes = set.Bytes("artifacts.chunkBytes") | ||
| lim.InterceptImages = set.Bool("artifacts.interceptImages") | ||
| lim.StdoutLimit = set.Bytes("exec.maxOutputBytes") | ||
| return &execsvc.Service{ | ||
| svc := &execsvc.Service{ | ||
| Store: store, | ||
| // The script runs in this process's machine, and the daemon whose | ||
| // store it is has to be the same one for a path to mean anything to | ||
|
|
@@ -191,6 +194,44 @@ func (a *App) localService(store *artifacts.Store, socket string) *execsvc.Servi | |
| Local: socket != "", | ||
| Limits: lim, | ||
| } | ||
| // Prompt hooks (#5276) on a local run resolve through the same | ||
| // configured model layer the daemon uses, minus the broker samplers | ||
| // only a daemon can offer. model.provider=none (the default) | ||
| // resolves to (nil, nil): no behaviour change for a user who | ||
| // configures nothing, and a prompt hook reports advice unavailable. | ||
| if prov, merr := model.Resolve(model.Config{ | ||
| Provider: set.String("model.provider"), | ||
| BaseURL: set.String("model.baseURL"), | ||
| APIKeyEnv: set.String("model.apiKeyEnv"), | ||
| ProviderSettingsJSON: set.String("model.providers"), | ||
| ProfilesJSON: set.String("model.profiles"), | ||
| DefaultModel: set.String("model.default"), | ||
| Timeout: set.Duration("model.timeout"), | ||
| }); merr != nil { | ||
| svc.ModelProviderErr = merr | ||
| } else { | ||
| svc.ModelProvider = prov | ||
| } | ||
| // The same token cap the daemon resolves (prompt.maxTokens | ||
| // bounded by model.budget), so a local run's prompt hooks run | ||
| // under the operator's ceiling too (Codex review, PR #9021). | ||
| svc.ModelMaxTokens = execsvc.ModelTokenCap(set.Int("prompt.maxTokens"), set.Int("model.budget")) | ||
| // The configured default model, for a prompt hook that names | ||
| // none of its own (see execsvc.Service.ModelDefault). | ||
| svc.ModelDefault = set.String("model.default") | ||
| // Config-declared hooks apply to local runs exactly as they do | ||
| // to daemon runs: without this the service's Hooks stayed empty | ||
| // and a prompt (or script) entry in script.hooks was silently | ||
| // ignored on the default local path, provider or not (Codex | ||
| // review, PR #9021). Configured code is gated by hooks.autonomy | ||
| // exactly as the daemon gates it and as hookGate gates the | ||
| // legacy phases: below `run` it does not execute. | ||
| if cfg != nil && settings.AutonomyAtLeast(set.String("hooks.autonomy"), "run") { | ||
| for _, h := range cfg.ScriptHooks() { | ||
| svc.Hooks = append(svc.Hooks, execsvc.Hook{Phase: h.Phase, Script: h.Script, Prompt: h.Prompt, Effect: h.Effect, Model: h.Model, Name: h.Name}) | ||
|
Comment on lines
+230
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For local Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in db22dac: runEnvelope now carries res.Hooks as the envelope's hooks key when a run produced frames (pinned by TestRunEnvelopeCarriesHooks, including the no-hooks/no-key case), so the --json path delivers advice and warnings. The default text path now calls reportHooks alongside reportArtifacts: retained frames render to stderr as "mcpx: hook advice (phase): ..." / "mcpx: hook warning (phase): ...", keeping stdout as the script's own answer. The stream path already encodes hook frames live, and the structured path marshals res itself, hooks included.
Comment on lines
+229
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For local Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not fixed in this PR -- deferred with reason, split to issue #9349. The local path has no collection surface to forward from: execsvc.CallRegistry is a live, in-process, per-run sink registry (no retention, no query API), and a local run's tool calls are reported to the DAEMON process's registry via X-Mcpx-Run -- a different process from the CLI's localService, which never sees them. Populating @Calls locally therefore needs a new cross-process surface (daemon retention plus a run-calls query the CLI fetches before outcome hooks, or client-side reporting back into the local process): a design addition beyond this advise-hooks slice, and rushing it here would risk the exec paths this PR just stabilised. Daemon runs are unaffected (routes_exec stamps the shared registry; their @Calls is populated). Issue #9349 records the root cause, the design options, and the acceptance bar, including that an uncollected history must not be presented as an empty one. -- queue task 9174, lane any-c |
||
| } | ||
| } | ||
| return svc | ||
|
Comment on lines
+218
to
+234
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a local Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in e819507: localService (internal/cli/exec.go) now takes the run's config and populates svc.Hooks from cfg.ScriptHooks(), gated by hooks.autonomy at run exactly as the daemon gates config-declared hooks in routes_exec.go and as hookGate gates the legacy phases. The sole caller in commands.go passes its resolved cfg. Local mcpx exec / mcpx run runs now execute config-declared prompt (and script) hooks instead of silently ignoring script.hooks. |
||
| } | ||
|
|
||
| // ---- the remote path ---- | ||
|
|
@@ -376,6 +417,10 @@ func (a *App) renderExecResult(res *execsvc.Result, eo execOptions) error { | |
| _, _ = fmt.Fprint(os.Stdout, res.Stdout) | ||
| } | ||
| a.reportArtifacts(res) | ||
| // The remote text path renders hooks too: this renderer is | ||
| // shared by every remote caller, and a prompt hook's advice | ||
| // lives only in its retained frame (Codex review, PR #9021). | ||
| a.reportHooks(res) | ||
| } | ||
| if res.Error != "" && res.ExitCode == 0 { | ||
| return errors.New(res.Error) | ||
|
|
@@ -475,6 +520,23 @@ func (a *App) reportArtifacts(res *execsvc.Result) { | |
| } | ||
| } | ||
|
|
||
| // reportHooks renders retained hook frames on the text path, to | ||
| // stderr like reportArtifacts: stdout is the script's own answer, | ||
| // and a prompt hook's advice (or its provider-unavailable warning) | ||
| // would otherwise be generated and silently discarded on a local | ||
| // run (Codex review, PR #9021). The stream path already encodes | ||
| // hook frames as they happen, and the structured/JSON paths carry | ||
| // res.Hooks in the document itself. | ||
| func (a *App) reportHooks(res *execsvc.Result) { | ||
| for _, f := range res.Hooks { | ||
| if f.Advice != "" { | ||
| fmt.Fprintf(os.Stderr, "mcpx: hook advice (%s): %s\n", f.Phase, f.Advice) | ||
| } else if f.Error != "" { | ||
| fmt.Fprintf(os.Stderr, "mcpx: hook warning (%s): %s\n", f.Phase, f.Error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // stream issues a request and hands back the live response. | ||
| // | ||
| // do() reads the whole body, which is right for every other call and wrong | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/developing-today/code/pkgs/mcpx/internal/execsvc" | ||
| ) | ||
|
|
||
| // Oracle: a prompt hook's advice is its sole output, and mcpx_exec's | ||
| // tool result is the only channel an MCP caller has — renderExec | ||
| // must carry retained advice and provider warnings in the rendered | ||
| // result (and in the error text on a failed run) without displacing | ||
| // the script's own outcome (Codex review, PR #9021). | ||
| func TestRenderExecCarriesHookAdvice(t *testing.T) { | ||
| hooks := []execsvc.HookFrame{ | ||
| {Phase: "onSuccess", Action: "prompt", Advice: "cache the lookup"}, | ||
| {Phase: "onError", Action: "prompt", Error: "no model provider configured"}, | ||
| } | ||
|
|
||
| out, err := renderExec(execsvc.Result{Stdout: "answer\n", Hooks: hooks}) | ||
| if err != nil { | ||
| t.Fatalf("renderExec: %v", err) | ||
| } | ||
| for _, want := range []string{"answer", "hook advice (onSuccess): cache the lookup", "hook warning (onError): no model provider configured"} { | ||
| if !strings.Contains(out, want) { | ||
| t.Fatalf("rendered result missing %q:\n%s", want, out) | ||
| } | ||
| } | ||
|
|
||
| _, err = renderExec(execsvc.Result{Error: "boom", Hooks: hooks}) | ||
| if err == nil || !strings.Contains(err.Error(), "boom") || !strings.Contains(err.Error(), "cache the lookup") { | ||
| t.Fatalf("error path dropped hook advice: %v", err) | ||
| } | ||
|
|
||
| out, err = renderExec(execsvc.Result{Hooks: hooks}) | ||
| if err != nil || !strings.Contains(out, "cache the lookup") { | ||
| t.Fatalf("advice-only result = %q, %v", out, err) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package config | ||
|
|
||
| import "testing" | ||
|
|
||
| // A config-declared hook may carry a prompt action (#5276): the entry is | ||
| // usable with a prompt and no script, and its effect/model survive | ||
| // resolution. An entry with no action at all is still dropped. | ||
| func TestResolveHooksPromptEntry(t *testing.T) { | ||
| got := ResolveHooks([][]any{{ | ||
| map[string]any{ | ||
| "phase": "onSuccess", "prompt": "advise on @result", | ||
| "effect": "advise", "model": "openai/gpt-5", "name": "reviewer", | ||
| }, | ||
| map[string]any{"phase": "before", "script": "prep.ts"}, | ||
| map[string]any{"phase": "suffix"}, | ||
| }}) | ||
| if len(got) != 2 { | ||
| t.Fatalf("resolved %d hooks, want 2 (the actionless entry dropped): %+v", len(got), got) | ||
| } | ||
| p := got[0] | ||
| if p.Prompt != "advise on @result" || p.Effect != "advise" || p.Model != "openai/gpt-5" || p.Name != "reviewer" { | ||
| t.Fatalf("prompt hook mangled in resolution: %+v", p) | ||
| } | ||
| if got[1].Script != "prep.ts" { | ||
| t.Fatalf("script hook mangled in resolution: %+v", got[1]) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
mcpx exec --remoteormcpx run --remoteusing default text output, configured prompt hooks invoke the model but their advice and warnings still disappear. This newreportHookscall only covers local execution: the remote branch returns earlier throughexecOnDaemon, thenexecRemoteandrenderExecResult, whose text branch prints stdout, emits and artifacts without inspectingres.Hooks. Call the helper from that shared remote renderer too, so remote text callers receive the prompt hook's output rather than paying for advice they cannot see.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 9b39a59: renderExecResult's text branch now calls reportHooks next to reportArtifacts, so mcpx exec --remote / mcpx run --remote text callers receive prompt-hook advice and warnings from the retained frames. That renderer is the shared remote path (execRemote and the serve/call renderers go through it); its JSON/structured branch already marshals res with hooks included, and the stream path echoes hook frames as they arrive.