feat(pr-review): daemon prefetches PR data so no forge credential enters the container

Adds the mechanism pr-review's watched-repos and operator triggers need:
before acquiring a pr-review task's container, the daemon fetches the pull
request's metadata, diff and head snapshot over its own credentialed forge
client (prefetchPRReview, using the already-built HTTP-based prsource.Source
from 7ddaed38) and writes the result under workDir/.archie-pr-review -- the
same host directory Docker bind-mounts into the container. The container-side
pipeline reads it back with prsource.MountSource, a local filesystem read, no
network call and no credential.

This follows the existing convention exactly (docs/development/agent.md,
"Giving the agent something new": "the daemon's own administrative token
never enters a container") and the same pattern WriteTaskJSON already uses
for the task brief -- prepare on the host before the container starts,
because the container binds the directory. No RPC service, no environment
variable, no exception to the stated rule.
This commit is contained in:
sam committed 2026-09-28 15:19:39 +10:00
1 parent 7ddaed386b
commit 2142e446ff
6 files changed
+484

No files matched your search

@@ -15,6 +15,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/infrastructure/prsource"
"github.com/samcharles93/archie-core/internal/installtype"
"github.com/samcharles93/archie-core/internal/storage"
"github.com/samcharles93/archie-core/internal/taskrun"
@@ -320,6 +321,16 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
Bus: bus,
Log: log,
}
// pr-review reads an external pull request's data from workDir, which the
// daemon already populated before this task started (daemon's
// prefetchPRReview, writing under workDir/.archie-pr-review -- the same
// host directory Docker bind-mounts at storage.WorktreeMountDir) -- never
// over the network, and never with a forge credential in this process.
// See docs/development/agent.md, "Giving the agent something new".
if req.Task.Workflow == "pr-review" {
tc.PRSource = prsource.NewFromMount(filepath.Join(workDir, prsource.PrefetchDirName))
}
if req.Repo.PersistentStorage {
memory, readErr := readProjectMemory(storage.MemoryPath)
if readErr != nil {
+15
View File
@@ -1527,6 +1527,21 @@ func (d *Daemon) acquireTaskContainer(
return nil, nil, false
}
// pr-review needs an external pull request's metadata, diff and head
// snapshot inside the container, but the container must never hold a
// forge credential (docs/development/agent.md, "Giving the agent
// something new"). The daemon fetches it here, before the container
// exists, using its own credentialed forge client, and writes the result
// into workDir -- the same host directory Docker bind-mounts at
// storage.WorktreeMountDir -- so the sandboxed pipeline can read it back
// with prsource.MountSource and no network call of its own.
if task.Workflow == "pr-review" {
if err := prefetchPRReview(ctx, d.forgeFor(task), task.Owner, task.Repo, task.PRNumber, workDir); err != nil {
park("pr-review prefetch failed", err)
return nil, nil, false
}
}
// Guard: Storage may be nil if the daemon was wired incorrectly. In
// normal operation, Storage is always set when ContainerPool is set.
if d.Storage == nil {
+105
View File
@@ -0,0 +1,105 @@
package daemon
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/infrastructure/prsource"
)
// prefetchPRReview fetches a pull request's metadata, diff and head snapshot
// over f (the daemon's own forge client, which holds the real credential)
// and writes them under workDir/prReviewPrefetchDir, before the task's
// container is acquired. This is how pr-review gets its data into the
// sandbox with no forge credential ever entering the container: the daemon
// does the real HTTP fetch here, unsandboxed, and the container-side
// prsource.MountSource only ever reads the result off the bind-mounted disk
// (docs/development/agent.md, "Giving the agent something new": "the
// daemon's own administrative token never enters a container").
func prefetchPRReview(ctx context.Context, f any, owner, repo string, number int, workDir string) error {
prReader, ok := f.(forge.PullRequestReader)
if !ok {
return errors.New("prefetch pr-review: this forge cannot read pull requests")
}
diffReader, ok := f.(forge.PullRequestDiffReader)
if !ok {
return errors.New("prefetch pr-review: this forge cannot read pull request diffs")
}
archiveReader, ok := f.(forge.RepoArchiveReader)
if !ok {
return errors.New("prefetch pr-review: this forge cannot read repository archives")
}
source := prsource.New(forgeAdapter{pr: prReader, diff: diffReader, archive: archiveReader})
base := filepath.Join(workDir, prsource.PrefetchDirName)
if err := os.MkdirAll(base, 0o755); err != nil {
return fmt.Errorf("prefetch pr-review: create prefetch directory: %w", err)
}
meta, err := source.Metadata(ctx, owner, repo, number)
if err != nil {
return fmt.Errorf("prefetch pr-review: %w", err)
}
snapshotDir := filepath.Join(base, "snapshot")
if err := os.MkdirAll(snapshotDir, 0o755); err != nil {
return fmt.Errorf("prefetch pr-review: create snapshot directory: %w", err)
}
headSHA, err := source.Snapshot(ctx, owner, repo, number, snapshotDir)
if err != nil {
return fmt.Errorf("prefetch pr-review: %w", err)
}
diff, err := source.Diff(ctx, owner, repo, number)
if err != nil {
return fmt.Errorf("prefetch pr-review: %w", err)
}
if err := os.WriteFile(filepath.Join(base, "diff.patch"), []byte(diff), 0o644); err != nil {
return fmt.Errorf("prefetch pr-review: write diff: %w", err)
}
metaJSON, err := json.Marshal(struct {
Title string `json:"title"`
Body string `json:"body"`
HeadSHA string `json:"head_sha"`
}{Title: meta.Title, Body: meta.Body, HeadSHA: headSHA})
if err != nil {
return fmt.Errorf("prefetch pr-review: encode metadata: %w", err)
}
if err := os.WriteFile(filepath.Join(base, "metadata.json"), metaJSON, 0o644); err != nil {
return fmt.Errorf("prefetch pr-review: write metadata: %w", err)
}
return nil
}
// forgeAdapter narrows a concrete forge client's optional capabilities into
// prsource.Forge, converting forge.PullRequest (the daemon-side type, richer
// than prsource needs) into prsource.PullRequest.
type forgeAdapter struct {
pr forge.PullRequestReader
diff forge.PullRequestDiffReader
archive forge.RepoArchiveReader
}
func (a forgeAdapter) GetPullRequest(ctx context.Context, owner, repo string, number int) (prsource.PullRequest, error) {
pr, err := a.pr.GetPullRequest(ctx, owner, repo, number)
if err != nil {
return prsource.PullRequest{}, err
}
return prsource.PullRequest{Title: pr.Title, Body: pr.Body, HeadSHA: pr.HeadSHA}, nil
}
func (a forgeAdapter) GetPullRequestDiff(ctx context.Context, owner, repo string, number int) (string, error) {
return a.diff.GetPullRequestDiff(ctx, owner, repo, number)
}
func (a forgeAdapter) GetRepoArchive(ctx context.Context, owner, repo, ref string) (io.ReadCloser, error) {
return a.archive.GetRepoArchive(ctx, owner, repo, ref)
}
+127
View File
@@ -0,0 +1,127 @@
package daemon
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"errors"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/infrastructure/prsource"
)
// buildTestArchive builds a gzipped tar containing files, each wrapped under
// a synthetic top-level directory the way a forge's repo-archive endpoint
// does, so prefetchPRReview exercises the same extraction path production
// traffic does.
func buildTestArchive(t *testing.T, files map[string]string) []byte {
t.Helper()
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
tw := tar.NewWriter(gz)
for name, content := range files {
hdr := &tar.Header{
Name: "repo-abc123/" + name,
Mode: 0o644,
Size: int64(len(content)),
}
if err := tw.WriteHeader(hdr); err != nil {
t.Fatal(err)
}
if _, err := tw.Write([]byte(content)); err != nil {
t.Fatal(err)
}
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
if err := gz.Close(); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
type fakePRForge struct {
pr forge.PullRequest
diff string
archive []byte
pullErr error
diffErr error
archErr error
archCalls int
}
func (f *fakePRForge) GetPullRequest(context.Context, string, string, int) (forge.PullRequest, error) {
return f.pr, f.pullErr
}
func (f *fakePRForge) GetPullRequestDiff(context.Context, string, string, int) (string, error) {
return f.diff, f.diffErr
}
func (f *fakePRForge) GetRepoArchive(context.Context, string, string, string) (io.ReadCloser, error) {
f.archCalls++
if f.archErr != nil {
return nil, f.archErr
}
return io.NopCloser(strings.NewReader(string(f.archive))), nil
}
func TestPrefetchPRReviewWritesMetadataDiffAndSnapshot(t *testing.T) {
archive := buildTestArchive(t, map[string]string{"a.go": "package a\n"})
f := &fakePRForge{
pr: forge.PullRequest{Title: "t", Body: "b", HeadSHA: "abc123"},
diff: "diff --git a/x b/x\n",
archive: archive,
}
workDir := t.TempDir()
if err := prefetchPRReview(context.Background(), f, "acme", "widget", 7, workDir); err != nil {
t.Fatalf("prefetchPRReview: %v", err)
}
base := filepath.Join(workDir, prsource.PrefetchDirName)
meta, err := os.ReadFile(filepath.Join(base, "metadata.json"))
if err != nil {
t.Fatalf("read metadata.json: %v", err)
}
if !strings.Contains(string(meta), "abc123") || !strings.Contains(string(meta), `"t"`) {
t.Fatalf("metadata.json = %s, want title and head_sha", meta)
}
diff, err := os.ReadFile(filepath.Join(base, "diff.patch"))
if err != nil {
t.Fatalf("read diff.patch: %v", err)
}
if string(diff) != "diff --git a/x b/x\n" {
t.Fatalf("diff.patch = %q", diff)
}
if _, err := os.Stat(filepath.Join(base, "snapshot", "a.go")); err != nil {
t.Fatalf("snapshot not extracted: %v", err)
}
}
func TestPrefetchPRReviewFailsWhenForgeCannotReadPullRequests(t *testing.T) {
workDir := t.TempDir()
if err := prefetchPRReview(context.Background(), noPRCapability{}, "acme", "widget", 7, workDir); err == nil {
t.Fatal("want an error when the forge cannot read pull requests")
}
}
func TestPrefetchPRReviewPropagatesForgeError(t *testing.T) {
f := &fakePRForge{pullErr: errors.New("boom")}
workDir := t.TempDir()
if err := prefetchPRReview(context.Background(), f, "acme", "widget", 7, workDir); err == nil {
t.Fatal("want the forge error surfaced")
}
}
// noPRCapability satisfies forge.Forge's method set with none of
// PullRequestReader/PullRequestDiffReader/RepoArchiveReader -- like the noop
// forge, or a forge configured without PR-reading support.
type noPRCapability struct{}
+135
View File
@@ -0,0 +1,135 @@
package prsource
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// PrefetchDirName is the subdirectory of a task's worktree directory the
// daemon's prefetch step writes pull request data into, and MountSource
// reads it back from. Named once here so the daemon-side writer and this
// package's reader cannot drift into two different paths.
const PrefetchDirName = ".archie-pr-review"
// MountSource implements workflow.PRSource by reading data the daemon
// already fetched and wrote to disk before the container started, instead of
// calling the forge itself. This is how the sandboxed pipeline gets pull
// request data with no forge credential ever entering the container: the
// daemon (which holds the credential) runs Source (this package's
// HTTP-backed implementation) against baseDir before handing the container
// off, and the pipeline's Stage.Run bodies read the result through
// MountSource instead.
//
// baseDir holds three artifacts, all written by prefetchTo (the daemon-side
// counterpart, in internal/daemon):
//
// metadata.json -- {"title", "body", "head_sha"}
// diff.patch -- the raw unified diff
// snapshot/ -- the extracted repository tree at head_sha
type MountSource struct {
baseDir string
}
var _ workflow.PRSource = (*MountSource)(nil)
// NewFromMount builds a MountSource reading from baseDir.
func NewFromMount(baseDir string) *MountSource {
return &MountSource{baseDir: baseDir}
}
type mountMetadata struct {
Title string `json:"title"`
Body string `json:"body"`
HeadSHA string `json:"head_sha"`
}
func (s *MountSource) readMetadata() (mountMetadata, error) {
data, err := os.ReadFile(filepath.Join(s.baseDir, "metadata.json"))
if err != nil {
return mountMetadata{}, fmt.Errorf("prsource: read prefetched metadata: %w", err)
}
var m mountMetadata
if err := json.Unmarshal(data, &m); err != nil {
return mountMetadata{}, fmt.Errorf("prsource: decode prefetched metadata: %w", err)
}
return m, nil
}
// Metadata reads the prefetched title/body. owner, repo and number are
// unused: baseDir already names exactly one pull request, the one the
// daemon prefetched for this task.
func (s *MountSource) Metadata(_ context.Context, _, _ string, _ int) (workflow.PRMetadata, error) {
m, err := s.readMetadata()
if err != nil {
return workflow.PRMetadata{}, err
}
return workflow.PRMetadata{Title: m.Title, Body: m.Body}, nil
}
// Diff reads the prefetched unified diff.
func (s *MountSource) Diff(_ context.Context, _, _ string, _ int) (string, error) {
data, err := os.ReadFile(filepath.Join(s.baseDir, "diff.patch"))
if err != nil {
return "", fmt.Errorf("prsource: read prefetched diff: %w", err)
}
return string(data), nil
}
// Snapshot copies the prefetched snapshot tree into destDir and returns the
// head SHA it was measured against. This is a local filesystem copy inside
// the same container -- no network call, no credential -- since the daemon
// already extracted the archive into baseDir/snapshot before the container
// started.
func (s *MountSource) Snapshot(_ context.Context, _, _ string, _ int, destDir string) (string, error) {
m, err := s.readMetadata()
if err != nil {
return "", err
}
if err := copyTree(filepath.Join(s.baseDir, "snapshot"), destDir); err != nil {
return "", fmt.Errorf("prsource: copy prefetched snapshot: %w", err)
}
return m.HeadSHA, nil
}
// copyTree copies every file under src into dst, preserving the relative
// directory structure. dst is expected to already exist.
func copyTree(src, dst string) error {
return filepath.WalkDir(src, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
target := filepath.Join(dst, rel)
if d.IsDir() {
return os.MkdirAll(target, 0o755)
}
return copyFile(path, target)
})
}
func copyFile(src, dst string) error {
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
return err
}
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
@@ -0,0 +1,91 @@
package prsource
import (
"context"
"os"
"path/filepath"
"testing"
)
func writePrefetched(t *testing.T, dir string) {
t.Helper()
if err := os.MkdirAll(filepath.Join(dir, "snapshot", "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "metadata.json"), []byte(`{"title":"t","body":"b","head_sha":"abc123"}`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "diff.patch"), []byte("diff --git a/x b/x\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "snapshot", "file.go"), []byte("package a\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "snapshot", "sub", "nested.go"), []byte("package sub\n"), 0o644); err != nil {
t.Fatal(err)
}
}
func TestMountSourceMetadataReadsSidecarFile(t *testing.T) {
dir := t.TempDir()
writePrefetched(t, dir)
src := NewFromMount(dir)
meta, err := src.Metadata(context.Background(), "acme", "widget", 1)
if err != nil {
t.Fatalf("Metadata: %v", err)
}
if meta.Title != "t" || meta.Body != "b" {
t.Fatalf("meta = %+v, want title/body from the sidecar file", meta)
}
}
func TestMountSourceDiffReadsPatchFile(t *testing.T) {
dir := t.TempDir()
writePrefetched(t, dir)
src := NewFromMount(dir)
diff, err := src.Diff(context.Background(), "acme", "widget", 1)
if err != nil {
t.Fatalf("Diff: %v", err)
}
if diff != "diff --git a/x b/x\n" {
t.Fatalf("diff = %q, want the patch file's content", diff)
}
}
func TestMountSourceSnapshotCopiesFilesAndReturnsHeadSHA(t *testing.T) {
dir := t.TempDir()
writePrefetched(t, dir)
src := NewFromMount(dir)
destDir := t.TempDir()
headSHA, err := src.Snapshot(context.Background(), "acme", "widget", 1, destDir)
if err != nil {
t.Fatalf("Snapshot: %v", err)
}
if headSHA != "abc123" {
t.Fatalf("headSHA = %q, want abc123", headSHA)
}
got, err := os.ReadFile(filepath.Join(destDir, "file.go"))
if err != nil {
t.Fatalf("read copied file: %v", err)
}
if string(got) != "package a\n" {
t.Fatalf("copied file content = %q", got)
}
nested, err := os.ReadFile(filepath.Join(destDir, "sub", "nested.go"))
if err != nil {
t.Fatalf("read copied nested file: %v", err)
}
if string(nested) != "package sub\n" {
t.Fatalf("copied nested file content = %q", nested)
}
}
func TestMountSourceMetadataFailsWithoutPrefetchedData(t *testing.T) {
src := NewFromMount(t.TempDir())
if _, err := src.Metadata(context.Background(), "acme", "widget", 1); err == nil {
t.Fatal("want an error when no metadata.json was prefetched")
}
}