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:
1 parent
7df2ae3626
commit
dbb6550ab3
19 files changed
+494
-175
No files matched your search
@@ -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")),
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -47,6 +47,13 @@ type TaskBus interface {
|
||||
Request(ctx context.Context, subject string, payload []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
// WorktreeGrantIssuer gives one container dispatch the narrow authority to
|
||||
// publish its already-prepared branch. The daemon owns grant lifetime; the
|
||||
// transport implementation owns token mechanics.
|
||||
type WorktreeGrantIssuer interface {
|
||||
Issue(task *store.Task) (token string, revoke func(), err error)
|
||||
}
|
||||
|
||||
type Daemon struct {
|
||||
// Cfg publishes the running configuration. Read through Cfg() so a
|
||||
// reload swaps the published snapshot atomically. See config.Holder.
|
||||
@@ -67,7 +74,8 @@ type Daemon struct {
|
||||
Log *slog.Logger
|
||||
// Tasks is the optional message bus for task distribution. Nil means no
|
||||
// bus is configured; the existing SQLite ClaimNext flow is used.
|
||||
Tasks TaskBus
|
||||
Tasks TaskBus
|
||||
WorktreeGrants WorktreeGrantIssuer
|
||||
// TaskRunReadyTimeout bounds how long runViaAgent retries an initial
|
||||
// taskrun request that fails with nats.ErrNoResponders, giving a
|
||||
// freshly spawned archie-agent container time to connect to NATS, set
|
||||
@@ -1031,11 +1039,25 @@ func (d *Daemon) acquireTaskContainer(
|
||||
// never answered by) an archie-agent, or archie-agent failed before its
|
||||
// own workflow.Run got a chance to record an outcome.
|
||||
func (d *Daemon) runViaAgent(ctx context.Context, task *store.Task, repo config.Repo) {
|
||||
if d.WorktreeGrants == nil {
|
||||
const reason = "worktree publication grants are unavailable"
|
||||
d.Log.Error(reason, "task", task.ID)
|
||||
_ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, reason)
|
||||
return
|
||||
}
|
||||
grant, revoke, err := d.WorktreeGrants.Issue(task)
|
||||
if err != nil {
|
||||
d.Log.Error("worktree publication grant failed", "task", task.ID, "err", err)
|
||||
_ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "worktree publication grant failed: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer revoke()
|
||||
req := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: repo,
|
||||
Cfg: d.configFor(task).ForTask(),
|
||||
Providers: agentexec.ProvidersFromConfig(d.configFor(task).Providers),
|
||||
Task: task,
|
||||
Repo: repo,
|
||||
Cfg: d.configFor(task).ForTask(),
|
||||
Providers: agentexec.ProvidersFromConfig(d.configFor(task).Providers),
|
||||
WorktreeGrant: grant,
|
||||
}
|
||||
data, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -408,13 +408,20 @@ func daemonWithNATS(t *testing.T) (*Daemon, *store.Store, *arnats.Client) {
|
||||
"parked": "archie:parked", "queued": "archie:queued", "dead": "archie:dead",
|
||||
}},
|
||||
}),
|
||||
Store: s,
|
||||
Tasks: client,
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
Store: s,
|
||||
Tasks: client,
|
||||
WorktreeGrants: fixedGrantIssuer{token: "test-worktree-grant"},
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
return d, s, client
|
||||
}
|
||||
|
||||
type fixedGrantIssuer struct{ token string }
|
||||
|
||||
func (g fixedGrantIssuer) Issue(*store.Task) (string, func(), error) {
|
||||
return g.token, func() {}, nil
|
||||
}
|
||||
|
||||
func TestRequestTaskRunDoesNotSleepPastReadyDeadline(t *testing.T) {
|
||||
d, _, _ := daemonWithNATS(t)
|
||||
d.TaskRunReadyTimeout = 10 * time.Millisecond
|
||||
@@ -620,6 +627,9 @@ func TestRunViaAgentSendsExpectedRequest(t *testing.T) {
|
||||
if req.Cfg.DiffCapLines != 999 {
|
||||
t.Fatalf("request Cfg.DiffCapLines = %d, want 999", req.Cfg.DiffCapLines)
|
||||
}
|
||||
if req.WorktreeGrant != "test-worktree-grant" {
|
||||
t.Fatalf("request WorktreeGrant = %q, want dispatch grant", req.WorktreeGrant)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("archied did not publish a taskrun request")
|
||||
}
|
||||
|
||||
@@ -173,8 +173,7 @@ func (t *Transport) EventPublisher() agentexec.EventPublisher { return t.conn }
|
||||
|
||||
// RemoteTrees is the proxied half of workflow.Trees.
|
||||
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
|
||||
}
|
||||
|
||||
// The top-level forgerpc, storerpc, and worktreerpc packages remain legacy
|
||||
@@ -192,8 +191,8 @@ func (t *Transport) Store(timeout time.Duration) store.WorkflowStore {
|
||||
}
|
||||
|
||||
// Trees constructs the identity-scoped worktree RPC client.
|
||||
func (t *Transport) Trees(identity string, timeout time.Duration) RemoteTrees {
|
||||
return &worktreerpc.Client{Conn: t.conn, Timeout: timeout, Identity: identity}
|
||||
func (t *Transport) Trees(identity, grant string, timeout time.Duration) RemoteTrees {
|
||||
return &worktreerpc.Client{Conn: t.conn, Timeout: timeout, Identity: identity, Grant: grant}
|
||||
}
|
||||
|
||||
// TaskHandler executes one decoded full-task request.
|
||||
|
||||
@@ -99,7 +99,7 @@ func TestSubscribeTasksPreservesDedicatedAndSharedSubjects(t *testing.T) {
|
||||
}
|
||||
t.Cleanup(func() { _ = subscription.Close() })
|
||||
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &store.Task{ID: test.requestID}})
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &store.Task{ID: test.requestID}, WorktreeGrant: "grant"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -140,8 +140,9 @@ func TestSubscribeTasksOwnsWireErrors(t *testing.T) {
|
||||
{name: "missing task", payload: []byte(`{}`), want: "validate taskrun request: task is required"},
|
||||
{name: "null task", payload: []byte(`{"task":null}`), want: "validate taskrun request: task is required"},
|
||||
{name: "nonpositive task ID", payload: []byte(`{"task":{"id":0}}`), want: "validate taskrun request: task ID must be positive, got 0"},
|
||||
{name: "payload subject mismatch", payload: []byte(`{"task":{"id":8}}`), want: "validate taskrun request: task ID 8 does not match subject task ID 9"},
|
||||
{name: "handler", payload: []byte(`{"task":{"id":9}}`), want: "run failed", wantHandlerCalls: 1},
|
||||
{name: "missing worktree grant", payload: []byte(`{"task":{"id":9}}`), want: "validate taskrun request: worktree grant is required"},
|
||||
{name: "payload subject mismatch", payload: []byte(`{"task":{"id":8},"worktree_grant":"grant"}`), want: "validate taskrun request: task ID 8 does not match subject task ID 9"},
|
||||
{name: "handler", payload: []byte(`{"task":{"id":9},"worktree_grant":"grant"}`), want: "run failed", wantHandlerCalls: 1},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
before := handlerCalls
|
||||
@@ -204,7 +205,7 @@ func TestHandleTaskRejectsSubjectCorrelationErrors(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &store.Task{ID: test.requestID}})
|
||||
payload, err := json.Marshal(taskrun.Request{Task: &store.Task{ID: test.requestID}, WorktreeGrant: "grant"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -424,8 +425,8 @@ func TestRPCFactoriesPreserveIdentityAndTimeout(t *testing.T) {
|
||||
if !ok || storeClient.Timeout != timeout {
|
||||
t.Fatalf("store client = %#v", storeClient)
|
||||
}
|
||||
treeClient, ok := transport.Trees("identity-a", timeout).(*worktreerpc.Client)
|
||||
if !ok || treeClient.Identity != "identity-a" || treeClient.Timeout != timeout {
|
||||
treeClient, ok := transport.Trees("identity-a", "grant-a", timeout).(*worktreerpc.Client)
|
||||
if !ok || treeClient.Identity != "identity-a" || treeClient.Grant != "grant-a" || treeClient.Timeout != timeout {
|
||||
t.Fatalf("tree client = %#v", treeClient)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@ type Request struct {
|
||||
// transports, discover tools, and register them locally. Absent/empty
|
||||
// means no MCP servers (backward compatible).
|
||||
MCPServers []config.MCPServer `json:"mcp_servers,omitempty"`
|
||||
// WorktreeGrant is an opaque, per-dispatch capability authorizing the
|
||||
// daemon to publish this task's already-prepared branch. Repository
|
||||
// coordinates never cross back from the sandbox as authority.
|
||||
WorktreeGrant string `json:"worktree_grant,omitempty"`
|
||||
}
|
||||
|
||||
// Validate rejects a full-task request that cannot be correlated to a real
|
||||
@@ -40,6 +44,9 @@ func (r Request) Validate() error {
|
||||
if r.Task.ID <= 0 {
|
||||
return fmt.Errorf("task ID must be positive, got %d", r.Task.ID)
|
||||
}
|
||||
if r.WorktreeGrant == "" {
|
||||
return errors.New("worktree grant is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ func TestRequestJSONRoundTrip(t *testing.T) {
|
||||
Providers: map[string]agentexec.Provider{
|
||||
"anthropic": {Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY"},
|
||||
},
|
||||
WorktreeGrant: "grant",
|
||||
}
|
||||
|
||||
data, err := json.Marshal(req)
|
||||
@@ -48,6 +49,9 @@ func TestRequestJSONRoundTrip(t *testing.T) {
|
||||
if got.Providers["anthropic"].APIKeyEnv != "ANTHROPIC_API_KEY" {
|
||||
t.Fatalf("Providers did not round-trip: %+v", got.Providers)
|
||||
}
|
||||
if got.WorktreeGrant != "grant" {
|
||||
t.Fatalf("WorktreeGrant did not round-trip: %q", got.WorktreeGrant)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestValidateRequiresPositiveTaskID(t *testing.T) {
|
||||
@@ -59,7 +63,8 @@ func TestRequestValidateRequiresPositiveTaskID(t *testing.T) {
|
||||
{name: "missing task", wantErr: true},
|
||||
{name: "zero ID", request: Request{Task: &store.Task{}}, wantErr: true},
|
||||
{name: "negative ID", request: Request{Task: &store.Task{ID: -1}}, wantErr: true},
|
||||
{name: "positive ID", request: Request{Task: &store.Task{ID: 1}}},
|
||||
{name: "missing worktree grant", request: Request{Task: &store.Task{ID: 1}}, wantErr: true},
|
||||
{name: "valid request", request: Request{Task: &store.Task{ID: 1}, WorktreeGrant: "grant"}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := test.request.Validate()
|
||||
|
||||
@@ -19,6 +19,7 @@ package worktree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -62,7 +63,17 @@ type Manager struct {
|
||||
|
||||
// Dir is the worktree path for a task.
|
||||
func (m *Manager) Dir(owner, repo string, issue int) string {
|
||||
return filepath.Join(m.WorkDir, owner+"-"+repo, fmt.Sprintf("issue-%d", issue))
|
||||
ownerKey := base64.RawURLEncoding.EncodeToString([]byte(owner))
|
||||
repoKey := base64.RawURLEncoding.EncodeToString([]byte(repo))
|
||||
return filepath.Join(m.WorkDir, ownerKey, repoKey, fmt.Sprintf("issue-%d", issue))
|
||||
}
|
||||
|
||||
// ValidCoordinates reports whether owner, repo, and issue can safely identify
|
||||
// a task worktree without being interpreted as filesystem navigation.
|
||||
func ValidCoordinates(owner, repo string, issue int) bool {
|
||||
return owner != "" && repo != "" && issue > 0 &&
|
||||
filepath.Base(owner) == owner && filepath.Base(repo) == repo &&
|
||||
owner != "." && owner != ".." && repo != "." && repo != ".."
|
||||
}
|
||||
|
||||
// auth builds the HTTP credential for clone, fetch and push.
|
||||
@@ -100,6 +111,9 @@ func (m *Manager) Prepare(
|
||||
issue int,
|
||||
title, body, labels string,
|
||||
) (dir, branch string, err error) {
|
||||
if !ValidCoordinates(owner, repo, issue) {
|
||||
return "", "", fmt.Errorf("invalid worktree coordinates")
|
||||
}
|
||||
dir = m.Dir(owner, repo, issue)
|
||||
branch = archieBranch(issue, title, body, labels)
|
||||
|
||||
@@ -109,6 +123,14 @@ func (m *Manager) Prepare(
|
||||
}
|
||||
return dir, branch, nil
|
||||
}
|
||||
if migrated, migrateErr := m.migrateLegacy(owner, repo, issue, dir); migrateErr != nil {
|
||||
return "", "", migrateErr
|
||||
} else if migrated {
|
||||
if err := m.refresh(ctx, dir, base, branch); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return dir, branch, nil
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
return "", "", fmt.Errorf("clear stale worktree: %w", err)
|
||||
@@ -146,6 +168,56 @@ func (m *Manager) Prepare(
|
||||
return dir, branch, nil
|
||||
}
|
||||
|
||||
func (m *Manager) legacyDir(owner, repo string, issue int) string {
|
||||
return filepath.Join(m.WorkDir, owner+"-"+repo, fmt.Sprintf("issue-%d", issue))
|
||||
}
|
||||
|
||||
// migrateLegacy preserves worktrees created before repository coordinates
|
||||
// were encoded as separate path components. The old flattened key was
|
||||
// ambiguous, so migration is allowed only when the clone's origin exactly
|
||||
// matches the requested repository; an uncertain directory is left untouched.
|
||||
func (m *Manager) migrateLegacy(owner, repo string, issue int, dir string) (bool, error) {
|
||||
legacy := m.legacyDir(owner, repo, issue)
|
||||
if _, err := os.Stat(filepath.Join(legacy, preparedSentinel)); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("inspect legacy worktree: %w", err)
|
||||
}
|
||||
if !repositoryUsesURL(legacy, m.cloneURL(owner, repo)) {
|
||||
return false, nil
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil {
|
||||
return false, fmt.Errorf("create migrated worktree parent: %w", err)
|
||||
}
|
||||
if err := os.Rename(legacy, dir); err != nil {
|
||||
return false, fmt.Errorf("migrate legacy worktree: %w", err)
|
||||
}
|
||||
_ = os.Remove(filepath.Dir(legacy))
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func repositoryUsesURL(dir, want string) bool {
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
cfg, err := r.Config()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
remote, ok := cfg.Remotes[git.DefaultRemoteName]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
for _, got := range remote.URLs {
|
||||
if got == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// refresh brings an already-prepared worktree back in line with the
|
||||
// remote: fetch, move onto the task branch, and reset it to the base.
|
||||
func (m *Manager) refresh(ctx context.Context, dir, base, branch string) error {
|
||||
@@ -406,7 +478,20 @@ func (m *Manager) ChangedFiles(ctx context.Context, dir, base string) ([]string,
|
||||
// Cleanup removes a task's worktree (merged/rejected); parked worktrees
|
||||
// are kept for post-mortems.
|
||||
func (m *Manager) Cleanup(owner, repo string, issue int) error {
|
||||
return os.RemoveAll(m.Dir(owner, repo, issue))
|
||||
if !ValidCoordinates(owner, repo, issue) {
|
||||
return fmt.Errorf("invalid worktree coordinates")
|
||||
}
|
||||
if err := os.RemoveAll(m.Dir(owner, repo, issue)); err != nil {
|
||||
return err
|
||||
}
|
||||
legacy := m.legacyDir(owner, repo, issue)
|
||||
if repositoryUsesURL(legacy, m.cloneURL(owner, repo)) {
|
||||
if err := os.RemoveAll(legacy); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = os.Remove(filepath.Dir(legacy))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func remoteBase(base string) string {
|
||||
@@ -457,7 +542,6 @@ func archieBranch(issue int, title, body, labels string) string {
|
||||
// branchPrefix derives a conventional-commit prefix from the issue
|
||||
// labels, then falls back to "feat". Maps: bug→fix, feature→feat,
|
||||
// enhancement→feat, docs→docs, chore→chore, test→test.
|
||||
// FIXME - Unused parameters title and body where they should be used. - just a missing implementation by the looks of it.
|
||||
func branchPrefix(title, body, labels string) string {
|
||||
for l := range strings.SplitSeq(labels, ",") {
|
||||
switch strings.TrimSpace(strings.ToLower(l)) {
|
||||
|
||||
@@ -541,6 +541,112 @@ func TestCleanupRemovesWorktree(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirDistinguishesRepositoryTuples(t *testing.T) {
|
||||
m := &Manager{WorkDir: t.TempDir()}
|
||||
first := m.Dir("a-b", "c", 1)
|
||||
second := m.Dir("a", "b-c", 1)
|
||||
if first == second {
|
||||
t.Fatalf("distinct repositories share worktree path %q", first)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareMigratesMatchingLegacyWorktree(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
m := newManager(t, host)
|
||||
legacy := filepath.Join(m.WorkDir, "acme-todo", "issue-44")
|
||||
if _, err := git.PlainCloneContext(ctx, legacy, &git.CloneOptions{
|
||||
URL: m.cloneURL("acme", "todo"),
|
||||
ReferenceName: plumbing.NewBranchReferenceName(testBase),
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(legacy, preparedSentinel), nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
marker := filepath.Join(legacy, "legacy-marker")
|
||||
if err := os.WriteFile(marker, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 44, "feat: migrate", "", "feature")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dir != m.Dir("acme", "todo", 44) {
|
||||
t.Fatalf("Prepare dir = %q, want %q", dir, m.Dir("acme", "todo", 44))
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "legacy-marker")); err != nil {
|
||||
t.Fatalf("legacy worktree was not migrated: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(legacy); !os.IsNotExist(err) {
|
||||
t.Fatalf("legacy path still exists after migration: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparePreservesMismatchedLegacyWorktree(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
desiredHost := newLocalRemote(t, "acme", "todo")
|
||||
otherHost := newLocalRemote(t, "other", "widget")
|
||||
m := newManager(t, desiredHost)
|
||||
legacy := filepath.Join(m.WorkDir, "acme-todo", "issue-45")
|
||||
if _, err := git.PlainCloneContext(ctx, legacy, &git.CloneOptions{
|
||||
URL: filepath.Join(otherHost, "other", "widget.git"),
|
||||
ReferenceName: plumbing.NewBranchReferenceName(testBase),
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(legacy, preparedSentinel), nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
marker := filepath.Join(legacy, "keep")
|
||||
if err := os.WriteFile(marker, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, _, err := m.Prepare(ctx, "acme", "todo", testBase, 45, "feat: safe migration", "", "feature"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(marker); err != nil {
|
||||
t.Fatalf("mismatched legacy worktree was altered: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupHandlesLegacyWorktreesByOrigin(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
matching bool
|
||||
wantExists bool
|
||||
}{
|
||||
{name: "matching origin is removed", matching: true},
|
||||
{name: "mismatched origin is preserved", wantExists: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
desiredHost := newLocalRemote(t, "acme", "todo")
|
||||
m := newManager(t, desiredHost)
|
||||
cloneURL := m.cloneURL("acme", "todo")
|
||||
if !tc.matching {
|
||||
otherHost := newLocalRemote(t, "other", "widget")
|
||||
cloneURL = filepath.Join(otherHost, "other", "widget.git")
|
||||
}
|
||||
legacy := filepath.Join(m.WorkDir, "acme-todo", "issue-46")
|
||||
if _, err := git.PlainClone(legacy, &git.CloneOptions{URL: cloneURL}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := m.Cleanup("acme", "todo", 46); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := os.Stat(legacy)
|
||||
if tc.wantExists && err != nil {
|
||||
t.Fatalf("legacy worktree should remain: %v", err)
|
||||
}
|
||||
if !tc.wantExists && !os.IsNotExist(err) {
|
||||
t.Fatalf("legacy worktree still exists: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The prepared sentinel must never reach a commit. It previously lived in
|
||||
// the working tree, hidden by .git/info/exclude -- which git honours and
|
||||
// go-git does not, so it was committed and pushed onto every task branch.
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
// Package worktreerpc lets archie-agent request the two worktree
|
||||
// operations that touch the network -- clone/fetch (Prepare) and push
|
||||
// (Push) -- over core NATS request/reply, instead of holding the forge
|
||||
// credential itself. archied remains the sole holder of
|
||||
// worktree.Manager.Token and performs both against the host worktree
|
||||
// directory (the same files the agent container has bind-mounted at
|
||||
// /data/worktree).
|
||||
// Package worktreerpc lets archie-agent request publication of the task branch
|
||||
// over core NATS request/reply without holding the forge credential itself.
|
||||
// archied has already prepared the bind-mounted worktree before the container
|
||||
// starts. Publication is authorized by an opaque per-dispatch grant; sandbox
|
||||
// input never selects a host path, repository, or branch.
|
||||
//
|
||||
// CommitAll/Diff/ChangedFiles/ChangedLines are purely local git
|
||||
// operations against the already-mounted worktree and don't need the
|
||||
@@ -13,20 +11,24 @@ package worktreerpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/natsrpc"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
)
|
||||
|
||||
const (
|
||||
SubjectPush = "archie.worktree.push"
|
||||
SubjectPrepare = "archie.worktree.prepare"
|
||||
SubjectPush = "archie.worktree.push"
|
||||
)
|
||||
|
||||
// SubjectFor returns the subject for base, scoped to identity when set.
|
||||
@@ -42,48 +44,75 @@ func SubjectFor(identity, base string) string {
|
||||
return "archie.worktree." + identity + "." + strings.TrimPrefix(base, "archie.worktree.")
|
||||
}
|
||||
|
||||
// PushRequest identifies the task's worktree by owner/repo/issue rather
|
||||
// than trusting a path from the (sandboxed) agent -- the server resolves
|
||||
// the directory itself via worktree.Manager.Dir.
|
||||
type PushRequest struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Issue int `json:"issue"`
|
||||
Branch string `json:"branch"`
|
||||
Grant string `json:"grant"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
natsrpc.Envelope
|
||||
}
|
||||
|
||||
// PrepareRequest mirrors worktree.Manager.Prepare's arguments.
|
||||
type PrepareRequest struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Base string `json:"base"`
|
||||
Issue int `json:"issue"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Labels string `json:"labels"`
|
||||
type grant struct {
|
||||
identity string
|
||||
owner, repo string
|
||||
issue int
|
||||
branch string
|
||||
}
|
||||
|
||||
type PrepareResponse struct {
|
||||
Dir string `json:"dir"`
|
||||
Branch string `json:"branch"`
|
||||
natsrpc.Envelope
|
||||
// Grants owns short-lived publication capabilities issued for one dispatched
|
||||
// task. A grant is useful only on the identity-scoped server that issued it.
|
||||
type Grants struct {
|
||||
mu sync.RWMutex
|
||||
grants map[string]grant
|
||||
}
|
||||
|
||||
// defaultHandlerTimeout bounds a single prepare or push when the Server
|
||||
// has no explicit Timeout. A clone of a large repository over a slow link
|
||||
// is the long pole, so this is generous rather than tight.
|
||||
func NewGrants() *Grants { return &Grants{grants: make(map[string]grant)} }
|
||||
|
||||
// Issue creates a per-dispatch grant and a revocation function.
|
||||
func (g *Grants) Issue(task *store.Task) (string, func(), error) {
|
||||
if task == nil || task.ID <= 0 || task.Branch == "" ||
|
||||
!worktree.ValidCoordinates(task.Owner, task.Repo, task.IssueNumber) {
|
||||
return "", nil, errors.New("task is not ready for worktree publication")
|
||||
}
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", nil, fmt.Errorf("generate worktree grant: %w", err)
|
||||
}
|
||||
token := fmt.Sprintf("%x", raw)
|
||||
g.mu.Lock()
|
||||
g.grants[token] = grant{identity: task.Identity, owner: task.Owner, repo: task.Repo, issue: task.IssueNumber, branch: task.Branch}
|
||||
g.mu.Unlock()
|
||||
return token, func() {
|
||||
g.mu.Lock()
|
||||
delete(g.grants, token)
|
||||
g.mu.Unlock()
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *Grants) resolve(token, identity string) (grant, error) {
|
||||
if g == nil || token == "" {
|
||||
return grant{}, errors.New("worktree publication grant is required")
|
||||
}
|
||||
g.mu.RLock()
|
||||
item, ok := g.grants[token]
|
||||
g.mu.RUnlock()
|
||||
if !ok || item.identity != identity {
|
||||
return grant{}, errors.New("worktree publication grant is invalid")
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
// defaultHandlerTimeout bounds one publication request when the Server has no
|
||||
// explicit Timeout. A large repository push can be slow, so this is generous.
|
||||
const defaultHandlerTimeout = 15 * time.Minute
|
||||
|
||||
// Server proxies push requests to a real worktree.Manager holding the
|
||||
// forge push token.
|
||||
type Server struct {
|
||||
Trees *worktree.Manager
|
||||
Log *slog.Logger
|
||||
// Timeout bounds a single prepare or push. Zero uses
|
||||
Trees *worktree.Manager
|
||||
Grants *Grants
|
||||
Log *slog.Logger
|
||||
// Timeout bounds a single publication. Zero uses
|
||||
// defaultHandlerTimeout.
|
||||
Timeout time.Duration
|
||||
}
|
||||
@@ -102,23 +131,20 @@ func (s *Server) handlerContext() (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(context.Background(), timeout)
|
||||
}
|
||||
|
||||
// Register subscribes the push and prepare handlers on nc under the root
|
||||
// (identity-less) subjects. RegisterFor(nc, identity) registers them under
|
||||
// that identity's scoped subjects; the daemon calls it once per identity.
|
||||
// Register subscribes the publication handler under the root identity.
|
||||
// RegisterFor subscribes it under one identity-scoped subject.
|
||||
func (s *Server) Register(nc *nats.Conn) (unsubscribe func(), err error) {
|
||||
return s.RegisterFor(nc, "")
|
||||
}
|
||||
|
||||
// RegisterFor registers the push and prepare handlers under the subjects
|
||||
// scoped to identity.
|
||||
// RegisterFor registers publication under the subject scoped to identity.
|
||||
func (s *Server) RegisterFor(nc *nats.Conn, identity string) (unsubscribe func(), err error) {
|
||||
return natsrpc.RegisterAll(nc, []natsrpc.Registration{
|
||||
{Subject: SubjectFor(identity, SubjectPush), Handler: s.handlePush},
|
||||
{Subject: SubjectFor(identity, SubjectPrepare), Handler: s.handlePrepare},
|
||||
{Subject: SubjectFor(identity, SubjectPush), Handler: func(msg *nats.Msg) { s.handlePush(identity, msg) }},
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handlePush(msg *nats.Msg) {
|
||||
func (s *Server) handlePush(identity string, msg *nats.Msg) {
|
||||
var req PushRequest
|
||||
if err := json.Unmarshal(msg.Data, &req); err != nil {
|
||||
s.respond(msg, err)
|
||||
@@ -126,25 +152,13 @@ func (s *Server) handlePush(msg *nats.Msg) {
|
||||
}
|
||||
ctx, cancel := s.handlerContext()
|
||||
defer cancel()
|
||||
dir := s.Trees.Dir(req.Owner, req.Repo, req.Issue)
|
||||
s.respond(msg, s.Trees.Push(ctx, dir, req.Branch))
|
||||
}
|
||||
|
||||
func (s *Server) handlePrepare(msg *nats.Msg) {
|
||||
var req PrepareRequest
|
||||
if err := json.Unmarshal(msg.Data, &req); err != nil {
|
||||
s.respondPrepare(msg, "", "", err)
|
||||
item, err := s.Grants.resolve(req.Grant, identity)
|
||||
if err != nil {
|
||||
s.respond(msg, err)
|
||||
return
|
||||
}
|
||||
ctx, cancel := s.handlerContext()
|
||||
defer cancel()
|
||||
dir, branch, err := s.Trees.Prepare(ctx, req.Owner, req.Repo, req.Base, req.Issue, req.Title, req.Body, req.Labels)
|
||||
s.respondPrepare(msg, dir, branch, err)
|
||||
}
|
||||
|
||||
func (s *Server) respondPrepare(msg *nats.Msg, dir, branch string, err error) {
|
||||
resp := PrepareResponse{Dir: dir, Branch: branch, Envelope: natsrpc.NewEnvelope(err)}
|
||||
natsrpc.Respond(msg, s.Log, "worktreerpc", resp)
|
||||
dir := s.Trees.Dir(item.owner, item.repo, item.issue)
|
||||
s.respond(msg, s.Trees.Push(ctx, dir, item.branch))
|
||||
}
|
||||
|
||||
func (s *Server) respond(msg *nats.Msg, err error) {
|
||||
@@ -159,34 +173,20 @@ type Client struct {
|
||||
// Identity scopes this client's calls to one identity's RPC server.
|
||||
// Empty uses the root subjects (single-identity deployments).
|
||||
Identity string
|
||||
Grant string
|
||||
}
|
||||
|
||||
func (c *Client) rpc() *natsrpc.Client { return &natsrpc.Client{Conn: c.Conn, Timeout: c.Timeout} }
|
||||
|
||||
func (c *Client) subject(base string) string { return SubjectFor(c.Identity, base) }
|
||||
|
||||
// Push requests that archied push branch for owner/repo/issue's worktree
|
||||
// using its held forge credential.
|
||||
func (c *Client) Push(ctx context.Context, owner, repo string, issue int, branch string) error {
|
||||
req := PushRequest{Owner: owner, Repo: repo, Issue: issue, Branch: branch}
|
||||
// Push asks archied to publish the branch bound to this client's dispatch
|
||||
// grant using the daemon-held forge credential.
|
||||
func (c *Client) Push(ctx context.Context) error {
|
||||
req := PushRequest{Grant: c.Grant}
|
||||
resp, err := natsrpc.Call[Response](ctx, c.rpc(), c.subject(SubjectPush), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return resp.Err()
|
||||
}
|
||||
|
||||
// Prepare requests that archied clone/fetch the worktree for
|
||||
// owner/repo/issue using its held forge credential, matching
|
||||
// worktree.Manager.Prepare's signature.
|
||||
func (c *Client) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (dir, branch string, err error) {
|
||||
req := PrepareRequest{Owner: owner, Repo: repo, Base: base, Issue: issue, Title: title, Body: body, Labels: labels}
|
||||
resp, err := natsrpc.Call[PrepareResponse](ctx, c.rpc(), c.subject(SubjectPrepare), req)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if err := resp.Err(); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return resp.Dir, resp.Branch, nil
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
natssrv "github.com/nats-io/nats-server/v2/test"
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
)
|
||||
|
||||
@@ -109,10 +110,29 @@ func remoteHasBranch(t *testing.T, host, owner, repo, branch string) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func TestClientPreparePublishesViaServer(t *testing.T) {
|
||||
func TestManagerPrepareCannotDeleteOutsideWorkDir(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "widget")
|
||||
root := t.TempDir()
|
||||
workDir := filepath.Join(root, "worktrees")
|
||||
outside := filepath.Join(root, "escape-widget", "issue-7")
|
||||
if err := os.MkdirAll(outside, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
marker := filepath.Join(outside, "keep")
|
||||
if err := os.WriteFile(marker, []byte("operator data"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
m := &worktree.Manager{WorkDir: workDir, BaseURL: t.TempDir()}
|
||||
_, _, _ = m.Prepare(ctx, "../escape", "widget", "main", 7, "feat: test", "", "")
|
||||
if _, err := os.Stat(marker); err != nil {
|
||||
t.Fatalf("Prepare removed a path outside WorkDir: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientPushRejectsTaskMismatchedCoordinates(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "other", "widget")
|
||||
m := &worktree.Manager{
|
||||
WorkDir: t.TempDir(),
|
||||
Token: "unused-for-file-remotes",
|
||||
@@ -120,27 +140,32 @@ func TestClientPreparePublishesViaServer(t *testing.T) {
|
||||
BotEmail: "archie-bot@example.com",
|
||||
BaseURL: host,
|
||||
}
|
||||
dir, branch, err := m.Prepare(ctx, "other", "widget", "main", 9, "feat: other", "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "unexpected.txt"), []byte("unexpected\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if changed, err := m.CommitAll(ctx, dir, "unexpected"); err != nil || !changed {
|
||||
t.Fatalf("CommitAll = (%v, %v)", changed, err)
|
||||
}
|
||||
|
||||
srv := startEmbedded(t)
|
||||
url := srv.ClientURL()
|
||||
|
||||
rpcServer := &Server{Trees: m, Log: slog.New(slog.DiscardHandler)}
|
||||
unsub, err := rpcServer.Register(connect(t, url))
|
||||
grants := NewGrants()
|
||||
rpcServer := &Server{Trees: m, Grants: grants, Log: slog.New(slog.DiscardHandler)}
|
||||
unsub, err := rpcServer.Register(connect(t, srv.ClientURL()))
|
||||
if err != nil {
|
||||
t.Fatalf("register: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(unsub)
|
||||
|
||||
client := &Client{Conn: connect(t, url), Timeout: 5 * time.Second}
|
||||
dir, branch, err := client.Prepare(ctx, "acme", "widget", "main", 1, "feat: test", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare: %v", err)
|
||||
client := &Client{Conn: connect(t, srv.ClientURL()), Timeout: 5 * time.Second}
|
||||
if err := client.Push(ctx); err == nil {
|
||||
t.Fatal("Push accepted coordinates that were not correlated to an authoritative task")
|
||||
}
|
||||
if dir == "" || branch != "feat/1-test" {
|
||||
t.Fatalf("Prepare = (%q, %q)", dir, branch)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil {
|
||||
t.Fatalf("expected worktree cloned at %q: %v", dir, err)
|
||||
if remoteHasBranch(t, host, "other", "widget", branch) {
|
||||
t.Fatalf("task-mismatched branch %q was published", branch)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,15 +195,21 @@ func TestClientPushPublishesViaServer(t *testing.T) {
|
||||
srv := startEmbedded(t)
|
||||
url := srv.ClientURL()
|
||||
|
||||
rpcServer := &Server{Trees: m, Log: slog.New(slog.DiscardHandler)}
|
||||
grants := NewGrants()
|
||||
token, revoke, err := grants.Issue(&store.Task{ID: 1, Owner: "acme", Repo: "widget", IssueNumber: 1, Branch: branch})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(revoke)
|
||||
rpcServer := &Server{Trees: m, Grants: grants, Log: slog.New(slog.DiscardHandler)}
|
||||
unsub, err := rpcServer.Register(connect(t, url))
|
||||
if err != nil {
|
||||
t.Fatalf("register: %v", err)
|
||||
}
|
||||
t.Cleanup(unsub)
|
||||
|
||||
client := &Client{Conn: connect(t, url), Timeout: 5 * time.Second}
|
||||
if err := client.Push(ctx, "acme", "widget", 1, branch); err != nil {
|
||||
client := &Client{Conn: connect(t, url), Timeout: 5 * time.Second, Grant: token}
|
||||
if err := client.Push(ctx); err != nil {
|
||||
t.Fatalf("Push: %v", err)
|
||||
}
|
||||
|
||||
@@ -192,7 +223,7 @@ func TestClientPropagatesServerError(t *testing.T) {
|
||||
srv := startEmbedded(t)
|
||||
url := srv.ClientURL()
|
||||
|
||||
rpcServer := &Server{Trees: m, Log: slog.New(slog.DiscardHandler)}
|
||||
rpcServer := &Server{Trees: m, Grants: NewGrants(), Log: slog.New(slog.DiscardHandler)}
|
||||
unsub, err := rpcServer.Register(connect(t, url))
|
||||
if err != nil {
|
||||
t.Fatalf("register: %v", err)
|
||||
@@ -201,7 +232,7 @@ func TestClientPropagatesServerError(t *testing.T) {
|
||||
|
||||
client := &Client{Conn: connect(t, url), Timeout: 5 * time.Second}
|
||||
// No worktree ever prepared at this dir -- push must fail.
|
||||
err = client.Push(context.Background(), "acme", "nonexistent", 999, "some-branch")
|
||||
err = client.Push(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("expected Push to propagate the server-side git error")
|
||||
}
|
||||
@@ -211,7 +242,7 @@ func TestClientPushTimesOutWithNoResponder(t *testing.T) {
|
||||
srv := startEmbedded(t)
|
||||
client := &Client{Conn: connect(t, srv.ClientURL()), Timeout: 100 * time.Millisecond}
|
||||
|
||||
err := client.Push(context.Background(), "acme", "widget", 1, "branch")
|
||||
err := client.Push(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("expected Push to time out with no server registered")
|
||||
}
|
||||
@@ -226,8 +257,39 @@ func TestSubjectFor(t *testing.T) {
|
||||
if got := SubjectFor("winter", SubjectPush); got != "archie.worktree.winter.push" {
|
||||
t.Fatalf("SubjectFor(\"winter\", %q) = %q", SubjectPush, got)
|
||||
}
|
||||
if got := SubjectFor("winter", SubjectPrepare); got != "archie.worktree.winter.prepare" {
|
||||
t.Fatalf("SubjectFor(\"winter\", %q) = %q", SubjectPrepare, got)
|
||||
}
|
||||
|
||||
func TestGrantIsScopedAndRevocable(t *testing.T) {
|
||||
grants := NewGrants()
|
||||
token, revoke, err := grants.Issue(&store.Task{
|
||||
ID: 1, Identity: "winter", Owner: "acme", Repo: "widget",
|
||||
IssueNumber: 7, Branch: "feat/7-widget",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := grants.resolve(token, ""); err == nil {
|
||||
t.Fatal("root identity accepted another identity's grant")
|
||||
}
|
||||
if _, err := grants.resolve(token, "winter"); err != nil {
|
||||
t.Fatalf("owning identity rejected grant: %v", err)
|
||||
}
|
||||
revoke()
|
||||
if _, err := grants.resolve(token, "winter"); err == nil {
|
||||
t.Fatal("revoked grant remained usable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrantRejectsIncompleteTask(t *testing.T) {
|
||||
grants := NewGrants()
|
||||
for _, task := range []*store.Task{
|
||||
nil,
|
||||
{ID: 1, Owner: "../escape", Repo: "widget", IssueNumber: 7, Branch: "feat/7-widget"},
|
||||
{ID: 1, Owner: "acme", Repo: "widget", IssueNumber: 7},
|
||||
} {
|
||||
if _, _, err := grants.Issue(task); err == nil {
|
||||
t.Fatalf("Issue(%+v) error = nil", task)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user