From 4c0a06df6da70ba12f060d009726a82fc9198a83 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 31 Jul 2026 01:13:08 +1000 Subject: [PATCH] fix(container): write the task boot brief under .git WriteTaskJSON wrote task.json to the worktree root -- the same worktree the agent commits from. CommitAll stages with go-git's All option, and go-git's Add does not honour .gitignore or .git/info/exclude, so the brief was swept into the agent's commit and pushed onto every task branch. That is how a task.json for issue #57 ended up committed in the repo root. This is the identical failure the prepared sentinel hit; worktree.go already documents the fix. Nothing under .git can ever be tracked, so put the brief there rather than papering over it with an exclude. --- .../skills/archie-debugging-playbook/SKILL.md | 2 +- .../skills/archie-run-and-operate/SKILL.md | 4 +- cmd/archie-agent/main.go | 12 ++--- cmd/archie-agent/main_test.go | 23 +++++++--- internal/container/pool.go | 22 ++++++--- internal/container/pool_test.go | 25 +++++++++-- internal/storage/storage.go | 5 ++- internal/worktree/worktree_test.go | 45 +++++++++++++++++++ 8 files changed, 112 insertions(+), 26 deletions(-) diff --git a/.agents/skills/archie-debugging-playbook/SKILL.md b/.agents/skills/archie-debugging-playbook/SKILL.md index e13ec5213..a904ade8d 100644 --- a/.agents/skills/archie-debugging-playbook/SKILL.md +++ b/.agents/skills/archie-debugging-playbook/SKILL.md @@ -218,7 +218,7 @@ DB sink buffer 256; SSE client buffers 64. Drop counter not read in production. Dashboard gap can coexist with correct task state. ### Containers, worktrees, and subprocess mode -Container-mode: clones on host, mounts `/data/worktree`, writes `task.json`, +Container-mode: clones on host, mounts `/data/worktree`, writes `.git/task.json`, dedicated `archie.taskrun.` subscription. **Open:** container RPC is root-identity-bound; `SubprocessRunner` expects stdin JSON protocol. diff --git a/.agents/skills/archie-run-and-operate/SKILL.md b/.agents/skills/archie-run-and-operate/SKILL.md index afb9bccf3..34c2a5db9 100644 --- a/.agents/skills/archie-run-and-operate/SKILL.md +++ b/.agents/skills/archie-run-and-operate/SKILL.md @@ -156,7 +156,7 @@ Actual task workers are spawned by `archied` through `/var/run/docker.sock`. | Tasks, events, chat sessions/messages | NellDB append log at `db_path`; default `~/.local/share/archie/archie.db`. | | Default task worktree | `/-/issue-`; default root `~/.local/share/archie/work`. | | Identity worktree | `/identity-/-/issue-`. | -| Container worktree | Host worktree bind-mounted read/write at `/data/worktree`; `task.json` written at root. | +| Container worktree | Host worktree bind-mounted read/write at `/data/worktree`; `task.json` written under `.git/` so the agent's commit cannot sweep it onto the task branch. | | Built-in memory | `/memory/MEMORY.md` and `/memory/USER.md`. | | Telegram update state | `/telegram-update-deferrals.json` plus hashed `release-announcements-*.json`. | | Codesearch candidates | Defaults derive as `/indexes` and `/workspace-indexes.db`; no `indexing.Manager` construction found in daemon composition root. | @@ -177,7 +177,7 @@ for `archie.task.>` and `archie.agent.>`. |---|---| | Discovery | `archie.task.bug`, `.feature`, `.bootstrap`, or `.default`; daemon durable consumer `archie-daemon`, 5-min ack wait, max 3 deliveries. | | Legacy stages | `archie.agent..request`; worker durable consumer defaults to `archie-agent`, 30-min ack wait, max 3 deliveries; response address in `X-Archie-Reply`. | -| Container task | Core NATS request/reply on `archie.taskrun.`. Task container reads `/data/worktree/task.json` and creates dedicated subscription; shared worker uses queue group `archie-taskrun-workers`. | +| Container task | Core NATS request/reply on `archie.taskrun.`. Task container reads `/data/worktree/.git/task.json` and creates dedicated subscription; shared worker uses queue group `archie-taskrun-workers`. | | Privileged RPC | Store, forge, and worktree operations return to `archied` over core NATS subjects in `internal/storerpc`, `internal/forgerpc`, and `internal/worktreerpc`. | Set `containers.network` explicitly. `config.docker.toml` uses diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index d0a2cf864..ec1b12534 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -118,7 +118,7 @@ func run() int { // subscribeTaskRuns prefers a dedicated per-task subscription over the // shared queue group whenever this container was spawned for a specific -// task (task.json present at the worktree mount). +// task (.git/task.json present at the worktree mount). func subscribeTaskRuns(ctx context.Context, nc *nats.Conn, log *slog.Logger) (*nats.Subscription, error) { if taskID, ok := bootTaskID(storage.WorktreeMountDir, log); ok { log.Info("taskrun: dedicated per-task subscription", "task", taskID) @@ -126,7 +126,7 @@ func subscribeTaskRuns(ctx context.Context, nc *nats.Conn, log *slog.Logger) (*n handleTaskRun(ctx, msg, nc, log) }) } - log.Info("taskrun: shared queue-group subscription (no task.json found)") + log.Info("taskrun: shared queue-group subscription (no .git/task.json found)") return nc.QueueSubscribe(taskRunSubjectWildcard, taskRunQueueGroup, func(msg *nats.Msg) { handleTaskRun(ctx, msg, nc, log) }) @@ -134,10 +134,12 @@ func subscribeTaskRuns(ctx context.Context, nc *nats.Conn, log *slog.Logger) (*n // bootTaskID reads the boot-time task.json brief the daemon writes into // the worktree before container acquire (container.WriteTaskJSON) and -// returns its ID. Returns (0, false) when the file is absent, unparseable, -// or has no ID -- any of which mean "shared pool mode, no dedicated task." +// returns its ID. It lives under .git so the agent's own commit can't sweep +// it onto the task branch. Returns (0, false) when the file is absent, +// unparseable, or has no ID -- any of which mean "shared pool mode, no +// dedicated task." func bootTaskID(mountDir string, log *slog.Logger) (int64, bool) { - data, err := os.ReadFile(filepath.Join(mountDir, "task.json")) + data, err := os.ReadFile(filepath.Join(mountDir, ".git", "task.json")) if err != nil { return 0, false } diff --git a/cmd/archie-agent/main_test.go b/cmd/archie-agent/main_test.go index d3c7f8cd6..398824d50 100644 --- a/cmd/archie-agent/main_test.go +++ b/cmd/archie-agent/main_test.go @@ -10,6 +10,17 @@ import ( "github.com/samcharles93/archie-core/internal/container" ) +// gitDir returns a temp mount dir with the .git directory the daemon writes +// the boot brief into. +func gitDir(t *testing.T) string { + t.Helper() + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, ".git"), 0o700); err != nil { + t.Fatal(err) + } + return dir +} + func TestBootTaskID(t *testing.T) { log := slog.New(slog.DiscardHandler) @@ -21,12 +32,12 @@ func TestBootTaskID(t *testing.T) { }) t.Run("valid task.json -- dedicated per-task mode", func(t *testing.T) { - dir := t.TempDir() + dir := gitDir(t) data, err := json.Marshal(container.TaskPayload{ID: 42, Owner: "acme", Repo: "widget"}) if err != nil { t.Fatal(err) } - if err := os.WriteFile(filepath.Join(dir, "task.json"), data, 0o644); err != nil { + if err := os.WriteFile(filepath.Join(dir, ".git", "task.json"), data, 0o644); err != nil { t.Fatal(err) } id, ok := bootTaskID(dir, log) @@ -36,8 +47,8 @@ func TestBootTaskID(t *testing.T) { }) t.Run("malformed task.json falls back to shared pool mode", func(t *testing.T) { - dir := t.TempDir() - if err := os.WriteFile(filepath.Join(dir, "task.json"), []byte("not json"), 0o644); err != nil { + dir := gitDir(t) + if err := os.WriteFile(filepath.Join(dir, ".git", "task.json"), []byte("not json"), 0o644); err != nil { t.Fatal(err) } id, ok := bootTaskID(dir, log) @@ -47,9 +58,9 @@ func TestBootTaskID(t *testing.T) { }) t.Run("zero ID falls back to shared pool mode", func(t *testing.T) { - dir := t.TempDir() + dir := gitDir(t) data, _ := json.Marshal(container.TaskPayload{Owner: "acme", Repo: "widget"}) - if err := os.WriteFile(filepath.Join(dir, "task.json"), data, 0o644); err != nil { + if err := os.WriteFile(filepath.Join(dir, ".git", "task.json"), data, 0o644); err != nil { t.Fatal(err) } id, ok := bootTaskID(dir, log) diff --git a/internal/container/pool.go b/internal/container/pool.go index 71f486d5e..29980de1b 100644 --- a/internal/container/pool.go +++ b/internal/container/pool.go @@ -26,8 +26,8 @@ type Container struct { ID string } -// TaskPayload is the boot-time brief written to /data/task.json before -// the container starts, per PRD section 3. +// TaskPayload is the boot-time brief written to /data/worktree/.git/task.json +// before the container starts, per PRD section 3. type TaskPayload struct { ID int64 `json:"id"` Owner string `json:"owner"` @@ -41,9 +41,15 @@ type TaskPayload struct { Plan string `json:"plan,omitempty"` } -// WriteTaskJSON writes the task payload to /task.json. -// The workspace directory must already exist; WriteTaskJSON will not -// create directories. +// WriteTaskJSON writes the task payload to /.git/task.json. +// The workspace directory must already exist. +// +// It goes under .git deliberately. The workspace is the task worktree the +// agent commits from, and worktree.CommitAll stages with go-git's All +// option, which does not honour .gitignore or .git/info/exclude -- a brief +// at the worktree root is swept into the agent's commit and pushed onto the +// task branch. Nothing under .git can ever be tracked, which is the same +// reasoning that moved the prepared sentinel there. func WriteTaskJSON(workspace string, payload TaskPayload) error { if workspace == "" { return fmt.Errorf("task.json: empty workspace path") @@ -57,7 +63,11 @@ func WriteTaskJSON(workspace string, payload TaskPayload) error { if err != nil { return err } - return os.WriteFile(filepath.Join(workspace, "task.json"), data, 0o644) + gitDir := filepath.Join(workspace, ".git") + if err := os.MkdirAll(gitDir, 0o700); err != nil { + return fmt.Errorf("task.json: %w", err) + } + return os.WriteFile(filepath.Join(gitDir, "task.json"), data, 0o644) } // Pool manages a set of Docker containers running archie-agent. diff --git a/internal/container/pool_test.go b/internal/container/pool_test.go index b3192c2e7..e86b1434b 100644 --- a/internal/container/pool_test.go +++ b/internal/container/pool_test.go @@ -59,7 +59,7 @@ func TestWriteTaskJSONProducesValidFile(t *testing.T) { if err := WriteTaskJSON(dir, payload); err != nil { t.Fatal(err) } - data, err := os.ReadFile(filepath.Join(dir, "task.json")) + data, err := os.ReadFile(filepath.Join(dir, ".git", "task.json")) if err != nil { t.Fatal("task.json was not written:", err) } @@ -72,6 +72,23 @@ func TestWriteTaskJSONProducesValidFile(t *testing.T) { } } +// The brief goes under .git because the worktree it is written into is the +// one the agent commits from, and go-git's Add ignores .gitignore and +// .git/info/exclude. Anything left in the working tree gets pushed onto the +// task branch; nothing under .git can ever be staged. +func TestWriteTaskJSONLeavesWorkingTreeClean(t *testing.T) { + dir := t.TempDir() + if err := WriteTaskJSON(dir, TaskPayload{ID: 7, Owner: "acme", Repo: "todo"}); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(dir, "task.json")); !os.IsNotExist(err) { + t.Errorf("task.json written to the working tree root; err = %v, want IsNotExist", err) + } + if _, err := os.Stat(filepath.Join(dir, ".git", "task.json")); err != nil { + t.Errorf("task.json not written under .git: %v", err) + } +} + // ── regression: Gap 6 -- /data/task.json boot brief ─────────────────── func TestTaskPayloadWrittenAsVolumeFile(t *testing.T) { @@ -157,7 +174,7 @@ func TestTaskPayloadRoundTrip(t *testing.T) { t.Fatal(err) } - data, err := os.ReadFile(filepath.Join(dir, "task.json")) + data, err := os.ReadFile(filepath.Join(dir, ".git", "task.json")) if err != nil { t.Fatal(err) } @@ -192,7 +209,7 @@ func TestWriteTaskJSONOverwrite(t *testing.T) { _ = WriteTaskJSON(dir, p1) _ = WriteTaskJSON(dir, p2) // overwrite - data, _ := os.ReadFile(filepath.Join(dir, "task.json")) + data, _ := os.ReadFile(filepath.Join(dir, ".git", "task.json")) var decoded TaskPayload _ = json.Unmarshal(data, &decoded) @@ -209,7 +226,7 @@ func TestWriteTaskJSONMinimalPayload(t *testing.T) { t.Fatal(err) } - data, _ := os.ReadFile(filepath.Join(dir, "task.json")) + data, _ := os.ReadFile(filepath.Join(dir, ".git", "task.json")) var decoded TaskPayload _ = json.Unmarshal(data, &decoded) diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 51b9e7a11..b3dfab827 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -5,9 +5,10 @@ // /data/ volume layout (implemented): // // /data/ -// task.json -- at /data/worktree/task.json (WriteTaskJSON writes -// to the worktree root, which is bind-mounted) // worktree/ -- bind mount of host worktree +// .git/task.json -- boot-time brief (WriteTaskJSON). Under .git so +// the agent's own commit cannot sweep it onto the +// task branch: go-git's Add ignores .gitignore. // repo/ -- per-repo persistent volume (optional, on request) // cache/ // go/ -- GOMODCACHE (shared across all tasks) diff --git a/internal/worktree/worktree_test.go b/internal/worktree/worktree_test.go index 9617274d3..3b7f824e7 100644 --- a/internal/worktree/worktree_test.go +++ b/internal/worktree/worktree_test.go @@ -12,6 +12,8 @@ import ( gitconfig "github.com/go-git/go-git/v6/config" "github.com/go-git/go-git/v6/plumbing" "github.com/go-git/go-git/v6/plumbing/object" + + "github.com/samcharles93/archie-core/internal/container" ) const testBase = "main" @@ -477,3 +479,46 @@ func TestSentinelIsNeverCommitted(t *testing.T) { t.Errorf("sentinel missing after commit: %v", err) } } + +// The daemon writes the container's boot brief into the task worktree before +// the container starts. CommitAll stages with go-git's All option, which does +// not honour .gitignore or .git/info/exclude, so a brief written to the +// worktree root is swept into the agent's commit and pushed onto the task +// branch -- the same failure the prepared sentinel hit. It has to live +// somewhere that can never be tracked. +func TestTaskBriefIsNeverCommitted(t *testing.T) { + ctx := context.Background() + host := newLocalRemote(t, "acme", "todo") + m := newManager(t, host) + + dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 22, "feat: brief", "", "feature") + if err != nil { + t.Fatalf("Prepare() error = %v", err) + } + if err := container.WriteTaskJSON(dir, container.TaskPayload{ + ID: 22, Owner: "acme", Repo: "todo", Number: 22, Title: "feat: brief", + }); err != nil { + t.Fatalf("WriteTaskJSON() error = %v", err) + } + if err := os.WriteFile(filepath.Join(dir, "real.txt"), []byte("x\n"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := m.CommitAll(ctx, dir, "feat: brief"); err != nil { + t.Fatalf("CommitAll() error = %v", err) + } + + files, err := m.ChangedFiles(ctx, dir, testBase) + if err != nil { + t.Fatalf("ChangedFiles() error = %v", err) + } + for _, f := range files { + if strings.Contains(f, "task.json") { + t.Errorf("the task brief was committed: ChangedFiles() = %v", files) + } + } + + // The agent still has to be able to read it off its mount. + if _, err := os.Stat(filepath.Join(dir, ".git", "task.json")); err != nil { + t.Errorf("brief missing after commit: %v", err) + } +}