fix(worktree): bind publication to dispatch grants

Worktree RPC used to accept repository coordinates from the sandbox. It now accepts only an opaque per-dispatch grant, and the daemon derives the repository path and branch from grant-owned state.

Repository worktree paths also move from an ambiguous flattened key to encoded owner/repo components. Origin-matched legacy clones migrate safely; mismatched legacy paths are preserved.
This commit is contained in:
sam committed 2026-08-22 22:06:54 +10:00
1 parent 7df2ae3626
commit dbb6550ab3
19 files changed
+494 -175

No files matched your search

+10 -19
View File
@@ -25,20 +25,17 @@ import (
// worktreerpc; CommitAll/Diff/ChangedFiles/ChangedLines are local git
// operations run directly against the container's bind-mounted worktree.
//
// Prepare deliberately discards the RPC response's directory (the
// daemon's host-side path) and returns localDir instead -- archied and
// archie-agent see the same files at different paths.
// Prepare returns the bind-mounted worktree that archied prepared before the
// container started. The sandbox has no remote prepare capability.
type remoteTrees interface {
Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (dir, branch string, err error)
Push(ctx context.Context, owner, repo string, issue int, branch string) error
Push(ctx context.Context) error
}
type hybridTrees struct {
push remoteTrees
local *worktree.Manager
owner, repo string
issue int
localDir string
push remoteTrees
local *worktree.Manager
localDir string
branch string
// worktreeUID and worktreeGID are the daemon's own host UID/GID
// (WORKTREE_UID/WORKTREE_GID, set by containerEnv), or -1 when unset.
// The agent runs as root inside the container, so a commit it writes
@@ -50,11 +47,7 @@ type hybridTrees struct {
}
func (h *hybridTrees) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (dir, branch string, err error) {
_, branch, err = h.push.Prepare(ctx, owner, repo, base, issue, title, body, labels)
if err != nil {
return "", "", err
}
return h.localDir, branch, nil
return h.localDir, h.branch, nil
}
func (h *hybridTrees) CommitAll(ctx context.Context, dir, message string) (bool, error) {
@@ -67,7 +60,7 @@ func (h *hybridTrees) Push(ctx context.Context, dir, branch string) error {
return fmt.Errorf("reconcile worktree ownership before push: %w", err)
}
}
return h.push.Push(ctx, h.owner, h.repo, h.issue, branch)
return h.push.Push(ctx)
}
func (h *hybridTrees) Diff(ctx context.Context, dir, base string) (string, error) {
@@ -138,10 +131,8 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
trees := &hybridTrees{
push: dependencies.trees,
local: &worktree.Manager{WorkDir: workDir},
owner: req.Task.Owner,
repo: req.Task.Repo,
issue: req.Task.IssueNumber,
localDir: workDir,
branch: req.Task.Branch,
worktreeUID: worktreeOwnerID(os.Getenv("WORKTREE_UID")),
worktreeGID: worktreeOwnerID(os.Getenv("WORKTREE_GID")),
}
+31 -17
View File
@@ -38,11 +38,7 @@ type fakePushTrees struct {
pushErr error
}
func (f *fakePushTrees) Prepare(context.Context, string, string, string, int, string, string, string) (string, string, error) {
panic("unexpected call")
}
func (f *fakePushTrees) Push(context.Context, string, string, int, string) error {
func (f *fakePushTrees) Push(context.Context) error {
f.pushed = true
return f.pushErr
}
@@ -231,18 +227,20 @@ func (f *prCapturingForge) LinkBranch(context.Context, string, string, int, stri
type remoteManager struct {
manager *worktree.Manager
dir string
branch string
}
func (r *remoteManager) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (string, string, error) {
dir, branch, err := r.manager.Prepare(ctx, owner, repo, base, issue, title, body, labels)
if err == nil {
r.dir = dir
r.branch = branch
}
return dir, branch, err
}
func (r *remoteManager) Push(ctx context.Context, _, _ string, _ int, branch string) error {
return r.manager.Push(ctx, r.dir, branch)
func (r *remoteManager) Push(ctx context.Context) error {
return r.manager.Push(ctx, r.dir, r.branch)
}
func newLocalRemote(t *testing.T, owner, repo string) string {
@@ -363,10 +361,17 @@ func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
BotEmail: "archie-bot@example.com",
BaseURL: "file://" + host,
}
hostDir, _, err := daemonTrees.Prepare(ctx, "acme", "rpc-widget", "main", 2, task.Title, "", "bootstrap")
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "rpc-widget", "main", 2, task.Title, "", "bootstrap")
if err != nil {
t.Fatal(err)
}
task.Branch = branch
grants := worktreerpc.NewGrants()
grant, revokeGrant, err := grants.Issue(task)
if err != nil {
t.Fatal(err)
}
t.Cleanup(revokeGrant)
srv := startEmbeddedTaskRPCServer(t)
serverConn := connectTaskRPC(t, srv.ClientURL())
@@ -380,7 +385,7 @@ func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(unsubForge)
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Grants: grants, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
if err != nil {
t.Fatal(err)
}
@@ -396,9 +401,10 @@ func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
t.Cleanup(transport.Close)
request := taskrun.Request{
Task: task,
Repo: config.Repo{Owner: "acme", Name: "rpc-widget", Base: "main"},
Cfg: config.Config{}.ForTask(),
Task: task,
Repo: config.Repo{Owner: "acme", Name: "rpc-widget", Base: "main"},
Cfg: config.Config{}.ForTask(),
WorktreeGrant: grant,
}
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler))
if err != nil {
@@ -446,10 +452,17 @@ func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
BotEmail: "archie-bot@example.com",
BaseURL: "file://" + host,
}
hostDir, _, err := daemonTrees.Prepare(ctx, "acme", "events-widget", "main", 3, task.Title, "", "bootstrap")
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "events-widget", "main", 3, task.Title, "", "bootstrap")
if err != nil {
t.Fatal(err)
}
task.Branch = branch
grants := worktreerpc.NewGrants()
grant, revokeGrant, err := grants.Issue(task)
if err != nil {
t.Fatal(err)
}
t.Cleanup(revokeGrant)
srv := startEmbeddedTaskRPCServer(t)
serverConn := connectTaskRPC(t, srv.ClientURL())
@@ -463,7 +476,7 @@ func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(unsubForge)
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Grants: grants, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
if err != nil {
t.Fatal(err)
}
@@ -505,9 +518,10 @@ func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
t.Cleanup(transport.Close)
request := taskrun.Request{
Task: task,
Repo: config.Repo{Owner: "acme", Name: "events-widget", Base: "main"},
Cfg: config.Config{}.ForTask(),
Task: task,
Repo: config.Repo{Owner: "acme", Name: "events-widget", Base: "main"},
Cfg: config.Config{}.ForTask(),
WorktreeGrant: grant,
}
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler))
if err != nil {
+3 -3
View File
@@ -45,7 +45,7 @@ type workerTransport interface {
SubscribeTasks(context.Context, int64, bool, agentnats.TaskHandler, *slog.Logger) (agentnats.Subscription, error)
Forger(string, time.Duration) workflow.Forger
Store(time.Duration) store.WorkflowStore
Trees(string, time.Duration) agentnats.RemoteTrees
Trees(string, string, time.Duration) agentnats.RemoteTrees
}
type workerDependencies struct {
@@ -129,7 +129,7 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies
type taskServiceTransport interface {
Forger(string, time.Duration) workflow.Forger
Store(time.Duration) store.WorkflowStore
Trees(string, time.Duration) agentnats.RemoteTrees
Trees(string, string, time.Duration) agentnats.RemoteTrees
EventPublisher() agentexec.EventPublisher
}
@@ -138,7 +138,7 @@ func executeTaskRequest(ctx context.Context, request taskrun.Request, transport
dependencies := taskDependencies{
forge: transport.Forger(request.Task.Identity, rpcTimeout),
store: transport.Store(rpcTimeout),
trees: transport.Trees(request.Task.Identity, rpcTimeout),
trees: transport.Trees(request.Task.Identity, request.WorktreeGrant, rpcTimeout),
events: transport.EventPublisher(),
}
response, err := runTask(ctx, request, dependencies, agentexec.DefaultRunnerFactory, workDir, log)
+3 -3
View File
@@ -75,9 +75,9 @@ func (t *workerTransportStub) SubscribeTasks(_ context.Context, _ int64, dedicat
closeErr: t.subCloseErr,
}, nil
}
func (*workerTransportStub) Forger(string, time.Duration) workflow.Forger { return nil }
func (*workerTransportStub) Store(time.Duration) store.WorkflowStore { return nil }
func (*workerTransportStub) Trees(string, time.Duration) agentnats.RemoteTrees { return nil }
func (*workerTransportStub) Forger(string, time.Duration) workflow.Forger { return nil }
func (*workerTransportStub) Store(time.Duration) store.WorkflowStore { return nil }
func (*workerTransportStub) Trees(string, string, time.Duration) agentnats.RemoteTrees { return nil }
func TestRunPassesCompleteTransportConfiguration(t *testing.T) {
var got agentnats.Config