Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(hooks): answer goose's Stop hook with an explicit allow too

Claude0a4d3ce73acf
diff --git a/cli/beacon-hooks/cmd/goose.go b/cli/beacon-hooks/cmd/goose.go
index 7fbc1994..8c5bed2a 100644
--- a/cli/beacon-hooks/cmd/goose.go
+++ b/cli/beacon-hooks/cmd/goose.go
@@ -133,6 +133,27 @@ var (
// gooseReadImageToolName is the built-in that reads an image from a path *or* a URL.
const gooseReadImageToolName = "read_image"
+// gooseBlockingEventResponse is the reply goose's two blocking events require.
+//
+// goose runs `PreToolUse` and `Stop` through emit_blocking, which classifies what the hook wrote to
+// stdout instead of ignoring it. A hook that exits 0 with stdout carrying no recognized decision is
+// classified a *failure*: goose logs one per tool call and per turn, and on a PreToolUse rule
+// configured `on_failure: block` it denies the call outright. The no-op `{}` every other runtime
+// reads as "no opinion" lands there, and so does `{"permission":"allow"}` -- goose reads `decision`
+// and accepts only "allow" and "block".
+//
+// Saying "allow" approves nothing. goose's hook chain is a plugin-policy layer inside
+// ToolExecutionOperation, and the pipeline registers ToolApprovalOperation *before* it, so the
+// operator's decision has already been made by the time a hook is consulted; HookDecision::Allow
+// means "this policy hook does not object" and reaches no permission judge. That is what separates
+// goose from Qwen Code, where "allow" would genuinely disarm the user's own prompts and Beacon
+// therefore answers with an empty object.
+//
+// Shared by both callers rather than written twice, because the two events are one contract: a
+// change to what goose accepts has to reach pre-tool and stop together, and the literal appearing
+// in two files is how one of them gets left behind.
+var gooseBlockingEventResponse = map[string]interface{}{"decision": "allow"}
+
// gooseHookEvent reads which goose lifecycle event a payload describes.
func gooseHookEvent(input map[string]interface{}) string {
return getFirstStr(input, "event")
diff --git a/cli/beacon-hooks/cmd/goose_hooks_test.go b/cli/beacon-hooks/cmd/goose_hooks_test.go
index 8e4565af..3bd021bd 100644
--- a/cli/beacon-hooks/cmd/goose_hooks_test.go
+++ b/cli/beacon-hooks/cmd/goose_hooks_test.go
@@ -171,6 +171,46 @@ func TestGoosePreToolAnswersWithAnExplicitAllowDecision(t *testing.T) {
}
}
+// Stop is goose's other blocking event, and it runs through the same classifier: a `{}` there is a
+// failed hook once per turn, for a hook whose entire job is to observe the turn ending. Pinned
+// separately from pre-tool because the two replies are written by different functions and the
+// failure mode is invisible -- goose logs it and carries on.
+//
+// stopResponse is called directly rather than through runStop, which ends with os.Exit and so
+// cannot be driven in-process. That is the same reason no existing test runs that command either;
+// the reply is the whole of what this asserts, and stopResponse is where the reply is decided.
+func TestGooseStopAnswersWithAnExplicitAllowDecision(t *testing.T) {
+ gooseTestSetup(t)
+
+ out := stopResponse()
+ if got, _ := out["decision"].(string); got != "allow" {
+ t.Fatalf("stopResponse() = %#v, want {\"decision\":\"allow\"} -- goose runs Stop through "+
+ "emit_blocking and reads anything else as a failed hook", out)
+ }
+ // Both blocking events answer with the same value, from one definition. A second literal is how
+ // one of them gets left behind when goose's accepted decisions change.
+ stop, _ := out["decision"].(string)
+ if pre, _ := preToolResponse()["decision"].(string); pre != stop {
+ t.Fatalf("pre-tool answers %q and stop answers %q; goose's two blocking events are one "+
+ "contract and must be answered from one definition", pre, stop)
+ }
+}
+
+// The stop reply must not change for any runtime that is not goose. Every other one either ignores
+// this hook's stdout or reads `{}` as no opinion, and a decision key appearing there would be
+// Beacon asserting something about a turn it only watched.
+func TestGooseStopReplyDoesNotLeakToOtherRuntimes(t *testing.T) {
+ gooseTestSetup(t)
+ for _, platform := range []string{"claude", "qwen", openHandsPlatform, kiroPlatform} {
+ t.Run(platform, func(t *testing.T) {
+ platformFlag = platform
+ if out := stopResponse(); len(out) != 0 {
+ t.Fatalf("stopResponse() for %s = %#v, want an empty object", platform, out)
+ }
+ })
+ }
+}
+
// Saying "allow" to goose does not approve anything, which is what makes the test above safe and
// what separates goose from Qwen Code. goose's hook chain is a plugin-policy layer inside
// ToolExecutionOperation, and the pipeline registers ToolApprovalOperation before it -- so the
diff --git a/cli/beacon-hooks/cmd/pre_tool.go b/cli/beacon-hooks/cmd/pre_tool.go
index 167d8da7..5d1d1bf6 100644
--- a/cli/beacon-hooks/cmd/pre_tool.go
+++ b/cli/beacon-hooks/cmd/pre_tool.go
@@ -175,26 +175,10 @@ func preToolResponse() map[string]interface{} {
// answering the same question for every runtime rather than having one whose answer is
// "nothing, and the writer knows why".
// goose is the one runtime where the empty object is the harmful answer, so it is answered
- // before the group below rather than joining it.
- //
- // Its PreToolUse chain classifies every hook run, and a hook that exits 0 with stdout carrying
- // no decision is classified a *failure*: goose logs "Plugin hook failed; continuing without it"
- // for every tool call, and on a rule configured `on_failure: block` it denies the call
- // outright. `{}` and `{"permission":"allow"}` both land there -- goose reads `decision`, and
- // only the values "allow" and "block". Silence would also be accepted, but an explicit allow is
- // the one shape that is unambiguous on both of goose's blocking events, Stop included.
- //
- // Saying "allow" here does not approve anything, which is what makes it safe and what separates
- // goose from Qwen Code above. goose's hook chain is a plugin-policy layer inside
- // ToolExecutionOperation, and the pipeline registers ToolApprovalOperation *before* it -- so by
- // the time a hook is consulted the operator's decision has already been made and the call has
- // already survived it. HookDecision::Allow means "this policy hook does not object"; there is
- // no code path by which it reaches the permission judge or skips a prompt.
- //
- // The key is `decision` rather than `permission`: allowResponse below is a different runtime's
- // spelling and goose would read it as no decision at all.
+ // before the group below rather than joining it. gooseBlockingEventResponse carries why, and
+ // carries it once because Stop needs the same answer for the same reason.
if platformFlag == goosePlatform {
- return map[string]interface{}{"decision": "allow"}
+ return gooseBlockingEventResponse
}
if platformFlag == "claude" || platformFlag == "qwen" || isDevinLikePlatform(platformFlag) || platformFlag == "hermes" || platformFlag == "vscode" || platformFlag == "muse" || platformFlag == openHandsPlatform || platformFlag == kiroPlatform {
return emptyResponse
diff --git a/cli/beacon-hooks/cmd/stop.go b/cli/beacon-hooks/cmd/stop.go
index b3b06f77..55b4c1fc 100644
--- a/cli/beacon-hooks/cmd/stop.go
+++ b/cli/beacon-hooks/cmd/stop.go
@@ -26,12 +26,26 @@ func init() {
rootCmd.AddCommand(stopCmd)
}
+// stopResponse is the no-op reply for the Stop hook, per runtime.
+//
+// It exists for goose and only for goose, and it is the same hazard preToolResponse answers: goose
+// runs Stop through emit_blocking, so the `{}` this hook used to write on every path is classified
+// as a failed hook rather than as no opinion -- once per turn, for a hook whose entire job is to
+// observe one. Every other runtime ignores this hook's stdout or reads `{}` as no opinion, and
+// keeps getting it.
+func stopResponse() map[string]interface{} {
+ if platformFlag == goosePlatform {
+ return gooseBlockingEventResponse
+ }
+ return emptyResponse
+}
+
func runStop(cmd *cobra.Command, args []string) {
start := time.Now()
input, err := readStdinJSON()
if err != nil {
- outputJSONAndExit(emptyResponse)
+ outputJSONAndExit(stopResponse())
return
}
@@ -52,11 +66,11 @@ func runStop(cmd *cobra.Command, args []string) {
logger.Info("stop completed")
emitHookEvent(logger, "tool.completed", "tool", "info", "Agent response completed", input, sessionFields("", input))
uploadCloudTelemetry(logger, true)
- outputJSON(emptyResponse)
+ outputJSON(stopResponse())
return
}
uploadCloudTelemetry(logger, true)
- outputJSONAndExit(emptyResponse)
+ outputJSONAndExit(stopResponse())
return
}
@@ -78,7 +92,7 @@ func runStop(cmd *cobra.Command, args []string) {
logger.Info("stop completed", "duration_ms", elapsed.Milliseconds())
emitHookEvent(logger, "tool.completed", "tool", "info", "Agent response completed", input, sessionFields(sessionID, input))
uploadCloudTelemetry(logger, true)
- outputJSONAndExit(emptyResponse)
+ outputJSONAndExit(stopResponse())
}
// platformToTranscriptName maps the platform flag to the transcript platform identifier.