gh-aw icon indicating copy to clipboard operation
gh-aw copied to clipboard

safe-outputs prompt tells engine:pi the CLI is an optional alternative to a direct tool call that doesn't exist for it (MCP: false)

Open ekbritecore opened this issue 2 months ago • 1 comments

Analysis

safe_outputs_prompt.md and mcp_cli_tools_with_safeoutputs_prompt.md are injected into the agent prompt unconditionally whenever safe-outputs are enabled (unified_prompt_step.go:140-150, buildMCPCLIPromptSection in mcp_cli_mount.go), with no branching on the engine's capabilities. Both files frame the safe-output tools (e.g. create_pull_request) as callable two ways:

  • safe_outputs_prompt.md: "Call the tool names listed in <safe-output-tools> directly; if a separate <mcp-clis> section says safeoutputs is available on PATH, you may use that CLI form instead."
  • mcp_cli_tools_with_safeoutputs_prompt.md: "For safeoutputs, call the tool names listed in <safe-output-tools> directly; the safeoutputs CLI commands above are an optional equivalent transport."

That's accurate for every engine with native MCP tool-calling (claude_engine.go, codex_engine.go, copilot_engine.go, gemini_engine.go all set MCP: true). It is not accurate for engine: pi: pi_engine.go:46 sets MCP: false — pi has no native MCP tool-calling at all. For pi, the safeoutputs CLI is not "an optional equivalent transport", it is the only transport.

Second, sharper mechanism (found by tracing the actual failed run's raw message stream, not just the compiled prompt): even where the wording were fixed, engine: pi has a second, independent way to lose these instructions entirely mid-run. pi's own client-side auto-compaction (docs/compaction.md) is documented to preserve pi's native system-role message verbatim across compaction — only older conversation turns get summarized. But gh-aw does not deliver its compiled prompt (the <mcp-clis>/<safe-output-tools> sections this issue is about) via pi's native system role at all. Checked the raw stream of a real failed run: pi's actual "role":"system" message is just its own generic "you are an expert coding assistant" preamble — gh-aw's entire compiled prompt, including the CLI-usage instructions, is folded into the first user turn's text instead (confirmed by a literal </system> pseudo-tag appearing inside a "role":"user" message in the raw log). That means those instructions are not compaction-exempt for engine: pi, and once a run's token count grows past pi's compaction threshold, they can be summarized away into a bare tool name with no procedural detail before the model ever reaches the point of calling the tool.

Third, a plausible root cause for why that threshold is hit at all: the failed run compacted at tokensBefore: 111952. pi's default contextWindow fallback for an unrecognized/custom-provider model is 128000 (see custom-provider.md/extensions.md: contextWindow: model.context_window ?? 128000), and default reserveTokens is 16384 — 128000 - 16384 = 111616, within estimation noise of the observed compaction point. gh-aw's pi engine integration (pi_engine.go) does not appear to register a contextWindow for models routed through its own aw-gateway custom-provider bridge, so pi likely falls back to the generic 128k default regardless of what the real underlying model (here claude-sonnet-5 via a Copilot backend) actually supports. pi supports overriding this per model via modelOverrides in ~/.pi/agent/settings.json / <project>/.pi/settings.json (models.md:341), but gh-aw's pi engine currently has no hook to write or forward such a settings file into the sandbox, so there's no way for a workflow author to correct this from the gh-aw side today.

Reproduction

Hit for real running an agentic bugfix loop with engine: pi (self-hosted runners, create-pull-request safe-output enabled, bash tools with a restricted allowlist including safeoutputs:*). A run correctly committed its work locally, correctly recognized a raw git push was blocked (expected — no git credentials in the sandbox by design), then searched for a way to call create_pull_request "directly" using plain bash tool discovery (type create_pull_request, compgen -c | grep pull_request) — neither finds anything, since there is no command literally named after the tool, only the safeoutputs CLI wrapper. Concluding no such tool existed, the agent gave up and returned a text summary without ever calling create_pull_request, losing the run's work (no commit was ever pushed, so there was nothing for create_pull_request.cjs's own push-failure bundle/fallback-issue mechanism to recover — that machinery only runs downstream of the tool actually being invoked).

Traced this specific run's raw message stream: it hit pi's auto-compaction (compaction_end, reason: threshold) at ~112k tokens, well before ever reaching the "call the safe-output tool" step — a long, exploratory live-site fixture-hunt ran up the token count first. The post-compaction summary that carried forward into the rest of the run was pure narrative progress ("must call create_pull_request... or noop/ report_incomplete if blocked"); the original prompt's "via the safeoutputs CLI, here's the syntax" detail was gone, compressed into a bare tool name by the summarizer.

Confirmed this is not a systemic, deterministic "pi can't find safeoutputs" gap — other, shorter runs of the same workflow shape (same prompt sections, same MCP: false engine, ~7 turns, never approaching the compaction threshold) called safeoutputs create_pull_request correctly on the first attempt, e.g.:

jq -Rs '{title:"...", branch:"...", body:.}' pr.md | safeoutputs create_pull_request .

So the failure is token-budget/duration-dependent, not model-attention noise alone: a short run finishes before compaction ever fires and the original instructions are still in-context; a long run can blow past the threshold before it ever needs those instructions, and gets a detail-stripped summary in their place.

Implementation Plan

  1. Thread engine MCP capability (EngineCapabilities.MCP) into buildMCPCLIPromptSection / the safe-outputs prompt section builder in unified_prompt_step.go. When MCP: false, render wording that states the CLI is the only transport (e.g. "Call these via the safeoutputs CLI on PATH — this is the only way to invoke them; there is no direct tool-call interface") instead of "an optional equivalent transport" / "you may use that CLI form instead".
  2. For engine: pi specifically, investigate delivering the safe-outputs/ MCP-CLI prompt sections through whatever mechanism maps to pi's native, compaction-exempt system role, rather than folding them into the first user turn alongside everything else that's subject to summarization.
  3. See #63240 (filed separately — independent bug, independent fix, affects any engine: pi workflow regardless of safe-outputs usage): pi_models_json.cjs registers aw-gateway models with no contextWindow, so pi falls back to a 128k default well below the real model's actual capacity, making compaction (and this issue's failure mode) fire far sooner than necessary on long-running runs.
  4. Add a compiled-workflow assertion (mirroring the existing no_bash_cli_proxy_integration_test.go / prompt_validation_test.go coverage) that an engine: pi workflow's compiled prompt does NOT contain the "optional equivalent transport" framing, and does contain an unambiguous "only transport" statement.

ekbritecore avatar Sep 24 '26 18:09 ekbritecore

Follow-up: found the actual trigger mechanism in a real failed run

Traced the specific run this issue was filed from (a create-pull-request safe-output on engine: pi that never got called) down to its raw message stream instead of just the compiled prompt. Two things worth adding here, since they sharpen where the fix should land:

  1. pi's compaction preserves its own native system role, but gh-aw's tool-usage instructions aren't in it. pi's compaction docs (docs/compaction.md) state the system-role message is kept verbatim across compaction — only older conversation turns get summarized. But gh-aw doesn't deliver its compiled prompt (including the <mcp-clis>/<safe-output-tools> sections this issue is about) via pi's native system role at all. Checked the raw stream: pi's actual "role":"system" message is just its own generic "you are an expert coding assistant" preamble. gh-aw's entire compiled prompt — including the CLI-usage instructions — is folded into the first user turn's text instead (confirmed by a literal </system> pseudo-tag appearing inside a "role":"user" message in the raw log).

  2. That means those instructions are not compaction-exempt for engine: pi, and in the failing run, they were in fact compacted away. The run hit pi's auto-compaction threshold (compaction_end, reason: threshold) at ~112k tokens, well before ever reaching the "call the safe-output tool" step (a long, exploratory live-site fixture-hunt ran up the token count). The post-compaction summary that carried forward was pure narrative progress — "must call create_pull_request... or noop/report_incomplete if blocked" — with the "via the safeoutputs CLI, here's the syntax" detail from the original prompt gone entirely, compressed into a bare tool name by the summarizer.

This explains why the same workflow shape doesn't fail deterministically: a short run (Jira read → grep → write file → call tool, ~7 turns) never approaches the compaction threshold, so the original instructions are still in-context when the tool call happens. A long, exploratory run can blow past the threshold before reaching the submit step, so by the time it needs to know how to call the tool, only a compacted, detail-stripped summary remains.

Given this, item 2 in the original implementation plan (reword the CLI-vs- direct-call framing) is still worth doing, but a more robust fix for MCP: false engines specifically would be delivering the safe-outputs/ MCP-CLI prompt sections through whatever mechanism actually maps to pi's native, compaction-exempt system role (if gh-aw's pi engine integration has one), rather than folding them into the first user turn alongside everything else that's subject to summarization.

ekbritecore avatar Sep 24 '26 19:09 ekbritecore