diff --git a/changelog.d/fixed-sandbox-pr-title-body.md b/changelog.d/fixed-sandbox-pr-title-body.md new file mode 100644 index 0000000000..edb0bfa9e7 --- /dev/null +++ b/changelog.d/fixed-sandbox-pr-title-body.md @@ -0,0 +1 @@ +- Sandbox PRs now take their title/body from `.hive/pr-title.txt` / `.hive/pr-body.md` in the workspace (or the HEAD commit) instead of a fixed placeholder that referenced hive's own tracker diff --git a/src/crux/agent/sandbox_executor.go b/src/crux/agent/sandbox_executor.go index d86d5f2976..b088bc76fb 100644 --- a/src/crux/agent/sandbox_executor.go +++ b/src/crux/agent/sandbox_executor.go @@ -25,6 +25,12 @@ const ( defaultSandboxNetworkMode = sandbox.NetworkModeRestricted sandboxPromptRelPath = ".hive/kick-prompt.txt" sandboxTranscriptRelPath = ".hive/sandbox-transcript.log" + // The sandboxed agent may leave the PR title and body it wants in the + // workspace; the executor (which alone can open the PR) picks them up. + sandboxPRTitleRelPath = ".hive/pr-title.txt" + sandboxPRBodyRelPath = ".hive/pr-body.md" + sandboxPRTitleMaxLen = 200 + sandboxPRBodyMaxLen = 60000 ) type sandboxCommandRunner interface { @@ -210,8 +216,7 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo return res, brokerErr } if e.PRClient != nil && bres.Pushed { - title := fmt.Sprintf("[%s] sandbox changes", spec.Agent) - body := fmt.Sprintf("Sandboxed credential-free execution result for agent `%s`.\n\nPart of #2804.", spec.Agent) + title, body := e.sandboxPRText(ctx, workspace, spec.Agent) pr, prErr := e.PRClient.CreatePR(ctx, spec.Org+"/"+spec.Repo, spec.Branch, spec.BaseRef, title, body) res.PR = &pr if prErr != nil { @@ -222,6 +227,62 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo return res, nil } +// sandboxPRText resolves the title and body for the PR the executor opens on +// the agent's behalf. Precedence: files the agent wrote into .hive/ in the +// workspace, then the HEAD commit's subject and body, then a generic +// placeholder naming the agent. The Job never holds a GitHub credential, so +// this is the only channel through which the agent's own PR description can +// reach the PR (hivecommons/hive#14691 follow-up). +func (e *SandboxExecutor) sandboxPRText(ctx context.Context, workspace, agent string) (string, string) { + title := readWorkspaceText(filepath.Join(workspace, sandboxPRTitleRelPath), sandboxPRTitleMaxLen) + body := readWorkspaceText(filepath.Join(workspace, sandboxPRBodyRelPath), sandboxPRBodyMaxLen) + if title == "" || body == "" { + if out, err := e.runner().Run(ctx, workspace, pushbroker.PushEnv(os.Environ()), "git", "log", "-1", "--format=%s%n%n%b", "HEAD"); err == nil { + subject, rest, _ := strings.Cut(strings.TrimSpace(string(out)), "\n") + if title == "" { + title = truncateRunes(strings.TrimSpace(subject), sandboxPRTitleMaxLen) + } + if body == "" { + body = truncateRunes(strings.TrimSpace(rest), sandboxPRBodyMaxLen) + } + } + } + if title == "" { + title = fmt.Sprintf("[%s] sandbox changes", agent) + } + if body == "" { + body = fmt.Sprintf("Changes produced by hive agent `%s` in a credential-free sandbox run.", agent) + } + return title, body +} + +// readWorkspaceText returns the trimmed contents of path, bounded to max +// runes, or "" when the file is absent or unreadable. The title file is +// reduced to its first line so a stray paragraph cannot become a PR title. +func readWorkspaceText(path string, max int) string { + data, err := os.ReadFile(path) + if err != nil { + return "" + } + text := strings.TrimSpace(string(data)) + if filepath.Base(path) == filepath.Base(sandboxPRTitleRelPath) { + text, _, _ = strings.Cut(text, "\n") + text = strings.TrimSpace(text) + } + return truncateRunes(text, max) +} + +func truncateRunes(s string, max int) string { + if max <= 0 { + return s + } + r := []rune(s) + if len(r) <= max { + return s + } + return string(r[:max]) +} + // prepareWorkspace clones the target repo and checks out the work branch off // the base ref. When spec.BaseRef is empty it is RESOLVED from the clone's own // refs/remotes/origin/HEAD and written back into spec, so the caller opens its diff --git a/src/crux/agent/sandbox_pr_text_test.go b/src/crux/agent/sandbox_pr_text_test.go new file mode 100644 index 0000000000..1ed0071b50 --- /dev/null +++ b/src/crux/agent/sandbox_pr_text_test.go @@ -0,0 +1,68 @@ +package agent + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" +) + +type prTextRunner struct{ gitLog string } + +func (r prTextRunner) Run(_ context.Context, _ string, _ []string, name string, args ...string) ([]byte, error) { + if name == "git" && len(args) > 0 && args[0] == "log" { + return []byte(r.gitLog), nil + } + return nil, nil +} + +func TestSandboxPRText_PrefersWorkspaceFiles(t *testing.T) { + ws := t.TempDir() + if err := os.MkdirAll(filepath.Join(ws, ".hive"), 0o755); err != nil { + t.Fatal(err) + } + os.WriteFile(filepath.Join(ws, ".hive", "pr-title.txt"), []byte("deps: bump torch-spyre to b49e05c\nsecond line ignored\n"), 0o644) + os.WriteFile(filepath.Join(ws, ".hive", "pr-body.md"), []byte("## Summary\n\nbuilt + smoke test green\n"), 0o644) + e := &SandboxExecutor{Runner: prTextRunner{gitLog: "commit subject\n\ncommit body"}} + title, body := e.sandboxPRText(context.Background(), ws, "deps") + if title != "deps: bump torch-spyre to b49e05c" { + t.Fatalf("title = %q", title) + } + if !strings.HasPrefix(body, "## Summary") { + t.Fatalf("body = %q", body) + } +} + +func TestSandboxPRText_FallsBackToHeadCommit(t *testing.T) { + e := &SandboxExecutor{Runner: prTextRunner{gitLog: "fix: tighten retry\n\nwhy it matters\n\nSigned-off-by: x\n"}} + title, body := e.sandboxPRText(context.Background(), t.TempDir(), "deps") + if title != "fix: tighten retry" { + t.Fatalf("title = %q", title) + } + if !strings.HasPrefix(body, "why it matters") { + t.Fatalf("body = %q", body) + } +} + +func TestSandboxPRText_PlaceholderLast(t *testing.T) { + e := &SandboxExecutor{Runner: prTextRunner{}} + title, body := e.sandboxPRText(context.Background(), t.TempDir(), "deps") + if title != "[deps] sandbox changes" || !strings.Contains(body, "`deps`") { + t.Fatalf("title=%q body=%q", title, body) + } + if strings.Contains(body, "#2804") { + t.Fatalf("placeholder must not reference hive's own tracker: %q", body) + } +} + +func TestSandboxPRText_TitleIsBounded(t *testing.T) { + ws := t.TempDir() + os.MkdirAll(filepath.Join(ws, ".hive"), 0o755) + os.WriteFile(filepath.Join(ws, ".hive", "pr-title.txt"), []byte(strings.Repeat("x", 500)), 0o644) + e := &SandboxExecutor{Runner: prTextRunner{}} + title, _ := e.sandboxPRText(context.Background(), ws, "deps") + if len([]rune(title)) != sandboxPRTitleMaxLen { + t.Fatalf("title len = %d", len([]rune(title))) + } +} diff --git a/src/docs/sandbox-isolation.md b/src/docs/sandbox-isolation.md index 5f6b4975bc..5ee0feea11 100644 --- a/src/docs/sandbox-isolation.md +++ b/src/docs/sandbox-isolation.md @@ -129,6 +129,7 @@ For sandboxed agents, a kick now follows this path: 3. The manager marks the agent busy while the sandbox runs and returns it to idle or failed when the timeout/completion path finishes. Dashboard status uses the existing agent status structures. 4. Hive collects the transcript at `.hive/sandbox-transcript.log` and any `agent-report*.json` artifact following `pkg/outputschema` conventions. 5. If the sandbox produced commits, `pkg/pushbroker.Broker` scans the committed diff for token-like secrets and protected-path edits, mints a short-lived scoped GitHub App token outside the sandbox, pushes the branch, and opens a PR through the existing App-authored GitHub client. Broker rejection records audit detail and nothing is pushed. + The PR title and body come from `.hive/pr-title.txt` (first line) and `.hive/pr-body.md` if the agent wrote them in the workspace, otherwise from the HEAD commit's subject and body; a generic `[] sandbox changes` placeholder is used only when neither exists. Kick templates for sandboxed agents should ask the agent to write those two files. ## Running sandboxed kicks as Kubernetes Jobs