fix(stage): supply runtime context when read is not declared
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
# Runtime context for stages without read
|
||||
|
||||
Research rerun #17 failed before research because its published tools were only
|
||||
`websearch` and `webfetch`, but the runtime wrapper required reading `input.md`
|
||||
and `memory.md`. The deny-by-default tool policy correctly refused those calls.
|
||||
|
||||
The shared model-prompt builder now embeds the server-owned turn context whenever
|
||||
`read` is not declared, not only when the tool list is empty. This applies to
|
||||
both OpenCode and Pi stage execution. The pipeline's authored prompt, version,
|
||||
tool list and permission policy are unchanged; arbitrary filesystem access is
|
||||
not granted. Stages declaring `read` retain the existing file-based handoff.
|
||||
|
||||
Regression tests cover web-only, grep-only, glob-only and shell-only stages:
|
||||
the actual input/memory contents and exact declared tool list are present, the
|
||||
runtime does not require an undeclared read, and no read allow-rule is added.
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -57,10 +58,10 @@ func modelPrompt(def Definition, spec ModelSpec, input Values) (string, error) {
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
// A text-only stage has no file-reading tool, so asking it to read input
|
||||
// and memory would make the run prompt unreachable. Embed both files in the
|
||||
// prompt instead; the paths remain visible for evidence.
|
||||
if len(tools) == 0 {
|
||||
// Receiving runtime context must not require adding filesystem tools to a
|
||||
// web-only (or otherwise restricted) stage. Inline it whenever read is not
|
||||
// declared, without widening the harness's permissions or changing a workflow.
|
||||
if !slices.Contains(tools, "read") {
|
||||
memory, readErr := os.ReadFile(def.Turn.Memory)
|
||||
if readErr != nil {
|
||||
return "", fmt.Errorf("model stage %s: read turn memory: %w", def.Key, readErr)
|
||||
@@ -69,7 +70,11 @@ func modelPrompt(def Definition, spec ModelSpec, input Values) (string, error) {
|
||||
if readErr != nil {
|
||||
return "", fmt.Errorf("model stage %s: read turn input: %w", def.Key, readErr)
|
||||
}
|
||||
b.WriteString("This call has no tools, so the server included the turn files below.\n\n")
|
||||
if len(tools) == 0 {
|
||||
b.WriteString("This call has no tools, so the server included the turn files below.\n\n")
|
||||
} else {
|
||||
b.WriteString("The read tool is not enabled, so the server included the turn files below. Use this supplied context; no extra filesystem access is needed.\n\n")
|
||||
}
|
||||
fmt.Fprintf(&b, "# memory.md (%s)\n\n%s", def.Turn.Memory, string(memory))
|
||||
if len(memory) > 0 && !strings.HasSuffix(string(memory), "\n") {
|
||||
b.WriteString("\n")
|
||||
|
||||
@@ -115,6 +115,44 @@ func TestModelPromptStatesPermittedTools(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestModelPromptSuppliesContextWithoutGrantingUndeclaredRead(t *testing.T) {
|
||||
for _, toolset := range []string{"websearch\nwebfetch", "grep", "glob", "shell"} {
|
||||
t.Run(strings.ReplaceAll(toolset, "\n", "-"), func(t *testing.T) {
|
||||
def := turnDefinition(t)
|
||||
def.Blocks["tools"] = toolset
|
||||
if err := os.WriteFile(def.Turn.Input, []byte("Research the October 2026 release window."), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(def.Turn.Memory, []byte("Earlier findings with primary-source citations."), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spec, err := (ModelStage{}).Spec(def)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prompt, err := modelPrompt(def, spec, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"Research the October 2026 release window.", "Earlier findings with primary-source citations.", "This call has these stage tools: " + strings.ReplaceAll(toolset, "\n", ", ") + "."} {
|
||||
if !strings.Contains(prompt, want) {
|
||||
t.Fatalf("runtime context missing %q", want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(prompt, "Read these two files before working:") {
|
||||
t.Fatal("stage still requires an unavailable tool")
|
||||
}
|
||||
tools, _, _ := ToolNames(def)
|
||||
client := &AgentClient{Tools: tools}
|
||||
for _, rule := range client.permissionRules() {
|
||||
if rule["action"] == "read" && rule["effect"] == "allow" {
|
||||
t.Fatal("context delivery widened filesystem permissions")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestModelPromptCarriesServerChangedFiles keeps the server-captured path list
|
||||
// reachable for model stages after per-field memory bindings were retired.
|
||||
func TestModelPromptCarriesServerChangedFiles(t *testing.T) {
|
||||
|
||||
@@ -50,6 +50,13 @@ func turnDefinition(t *testing.T) Definition {
|
||||
def := modelDefinition()
|
||||
def.ArtifactsDir = dir
|
||||
def.Turn = TurnPaths{Dir: dir, Input: filepath.Join(dir, "input.md"), Memory: filepath.Join(dir, "memory.md"), Output: filepath.Join(dir, "output.md"), Private: filepath.Join(dir, "private"), Public: filepath.Join(root, "public")}
|
||||
// Match the runner's prepared turn contract, including stages that need
|
||||
// inline context because they do not declare the read tool.
|
||||
for _, path := range []string{def.Turn.Input, def.Turn.Memory} {
|
||||
if err := os.WriteFile(path, nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user