Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,10 @@ or — with no identity (`~`) — the human operator. An orchestrator dispatches
sub-agents or delegates to other harnesses per this table; a solo operator
embodies whatever routes to `~`. The hub's config should declare all four
roles at project onboarding, each routed explicitly; an orchestrator finding
no routing table should prompt for one rather than assume. The CLI tolerates
an absent table (every role is then operator-held) but says so in its output.
no routing table should prompt for one rather than assume. `codecrew init`
(§6) scaffolds exactly this — the table with every role routed `~` — so
onboarding starts explicit. The CLI tolerates an absent table (every role is
then operator-held) but says so in its output.

`identity` names the **GitHub App** — or the GitHub username of a specific
human — the role acts as. A value containing a slash is reserved: it will
Expand Down Expand Up @@ -377,6 +379,7 @@ hub).
| Verb | What it does |
|------|--------------|
| `codecrew status` | Where the project is: open milestones, task states, raised gates. |
| `codecrew init [--hub owner/repo]` | Scaffolds a new repo: hub mode writes `.codecrew.yml` with the full `~`-routed roles table, the ROADMAP.md seed, the role contracts (embedded at the installed release), and an AGENTS.md entry point; spoke mode writes the two-line pointer. Idempotent — existing files are kept and reported. |
| `codecrew milestone new` | Creates a milestone tracking issue in the hub from the template; updates ROADMAP.md. |
| `codecrew task new --milestone <id> --repo <spoke>` | Creates a task issue in the spoke from the template; attaches it to the milestone as a sub-issue. |
| `codecrew task start <ref>` | Assigns the caller's identity, verifies a plan is present (refuses to start a planless nontrivial task), creates the working branch — unless the caller's role routing resolves to a role whose contract forbids commits (`qa`, `reviewer`), which get no branch. |
Expand Down
11 changes: 11 additions & 0 deletions assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package codecrew embeds the repo's own protocol assets so the shipped
// binary can scaffold new projects (`codecrew init`) with the role
// contracts at their installed release — no copies to drift.
package codecrew

import "embed"

// Roles holds the four role contracts under "roles/".
//
//go:embed roles/*.md
var Roles embed.FS
5 changes: 3 additions & 2 deletions docs/identities.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ perform the qa contract yourself: post per-requirement verdicts on the
milestone issue in the standard `**M1-R1 — satisfied.**` form — the close
gate counts the qa role holder's verdicts, and unrouted, that is you.
Declare the routing table in your hub's `.codecrew.yml` at onboarding (all
four roles, `~` for the ones you embody); an absent table works but the CLI
will nag.
four roles, `~` for the ones you embody) — `codecrew init` scaffolds exactly
this, along with the roadmap seed and role contracts; an absent table works
but the CLI will nag.

Exactly one thing degrades: the non-doer review gate. GitHub forbids
approving your own PR, and in pure solo there is no second principal to
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
const usage = `usage: codecrew <verb>

verbs:
init [--hub owner/repo] scaffold a new hub (or spoke pointer)
status where the project is
milestone new --title T [--goal G] create a milestone tracking issue
milestone close <n> close a milestone (gates: tasks closed, doc merged)
Expand All @@ -30,6 +31,8 @@ func Run(args []string) error {
}
verb, rest := args[0], args[1:]
switch verb {
case "init":
return initCmd(os.Stdout, rest)
case "status":
return status(os.Stdout)
case "milestone", "task":
Expand Down
120 changes: 120 additions & 0 deletions internal/cli/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package cli

import (
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"slices"

codecrew "github.com/radiusred/gh-codecrew"
)

const hubConfigScaffold = `codecrew: "0.1"
hub: self

# Role routing: who holds each role (SPEC §5). Declare all four at
# onboarding. ~ routes the role to you, the human operator; replace it with
# a GitHub App slug or a username to delegate. (A value with a slash is
# reserved for team routing.)
roles:
implementer: { identity: ~ }
reviewer: { identity: ~ }
qa: { identity: ~ }
doc-synthesizer: { identity: ~ }
`

const roadmapScaffold = `# Roadmap

| Milestone | Goal | Tracking issue | Status |
|-----------|------|----------------|--------|
`

const agentsScaffold = `# Agents

This repository is part of a CodeCrew project — coordination state lives in
GitHub issues and PRs, per the protocol at
https://github.com/radiusred/gh-codecrew (SPEC.md).

- ` + "`.codecrew.yml`" + ` names the hub; the hub's ` + "`roles/`" + ` holds the role
contracts. Read the contract for the role you were dispatched as before
doing anything else.
- ` + "`gh codecrew status`" + ` shows where the project is; ` + "`gh codecrew help`" + `
lists the workflow verbs. Blocked gates refuse with
` + "`refused[CODE]: detail`" + ` — act on the code, don't work around it.
- Plans before commits, decisions recorded when made, and the verifier is
never the doer.
`

// scaffold writes the greenfield files into dir. Hub mode (hub == "self")
// writes the full set; spoke mode writes only the pointer. Existing files
// are never touched — they are reported as skipped.
func scaffold(dir, hub string, contracts fs.FS) (written, skipped []string, err error) {
files := map[string]string{
".codecrew.yml": hubConfigScaffold,
}
if hub != "self" {
files[".codecrew.yml"] = fmt.Sprintf("codecrew: \"0.1\"\nhub: %s\n", hub)
} else {
files["ROADMAP.md"] = roadmapScaffold
files["AGENTS.md"] = agentsScaffold
entries, err := fs.ReadDir(contracts, "roles")
if err != nil {
return nil, nil, err
}
for _, e := range entries {
data, err := fs.ReadFile(contracts, "roles/"+e.Name())
if err != nil {
return nil, nil, err
}
files[filepath.Join("roles", e.Name())] = string(data)
}
}
for rel, content := range files {
path := filepath.Join(dir, rel)
if _, err := os.Stat(path); err == nil {
skipped = append(skipped, rel)
continue
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return written, skipped, err
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
return written, skipped, err
}
written = append(written, rel)
}
slices.Sort(written)
slices.Sort(skipped)
return written, skipped, nil
}

// initCmd scaffolds a new CodeCrew repo: hub mode by default, spoke mode
// with --hub owner/repo (pointer only — contracts and roadmap live in the
// hub).
func initCmd(w io.Writer, args []string) error {
flags := flag.NewFlagSet("init", flag.ContinueOnError)
hub := flags.String("hub", "self", "hub repo this spoke points at (default: this repo is the hub)")
if err := flags.Parse(args); err != nil {
return err
}
written, skipped, err := scaffold(".", *hub, codecrew.Roles)
if err != nil {
return err
}
for _, f := range written {
fmt.Fprintf(w, "wrote %s\n", f)
}
for _, f := range skipped {
fmt.Fprintf(w, "kept existing %s\n", f)
}
if *hub == "self" {
fmt.Fprintln(w, "\nnext: declare who holds each role in .codecrew.yml (~ = you),")
fmt.Fprintln(w, "commit, then `codecrew milestone new --title \"...\" --goal \"...\"`")
} else {
fmt.Fprintf(w, "\nnext: commit the pointer; tasks for this spoke attach to milestones in %s\n", *hub)
}
return nil
}
82 changes: 82 additions & 0 deletions internal/cli/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cli

import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"testing/fstest"
)

var fakeContracts = fstest.MapFS{
"roles/implementer.md": {Data: []byte("# Role: implementer\n")},
"roles/qa.md": {Data: []byte("# Role: qa\n")},
}

func TestScaffoldHub(t *testing.T) {
dir := t.TempDir()
written, skipped, err := scaffold(dir, "self", fakeContracts)
if err != nil {
t.Fatal(err)
}
if len(skipped) != 0 {
t.Errorf("fresh dir skipped %v", skipped)
}
for _, want := range []string{".codecrew.yml", "ROADMAP.md", "AGENTS.md", filepath.Join("roles", "qa.md")} {
if !slices.Contains(written, want) {
t.Errorf("missing %s from written %v", want, written)
}
if _, err := os.Stat(filepath.Join(dir, want)); err != nil {
t.Errorf("%s not on disk: %v", want, err)
}
}
cfg, err := os.ReadFile(filepath.Join(dir, ".codecrew.yml"))
if err != nil {
t.Fatal(err)
}
for _, role := range []string{"implementer", "reviewer", "qa", "doc-synthesizer"} {
if !strings.Contains(string(cfg), role) {
t.Errorf("hub config missing role %q", role)
}
}
}

func TestScaffoldSpoke(t *testing.T) {
dir := t.TempDir()
written, _, err := scaffold(dir, "org/hub", fakeContracts)
if err != nil {
t.Fatal(err)
}
if len(written) != 1 || written[0] != ".codecrew.yml" {
t.Fatalf("spoke mode wrote %v, want only the pointer", written)
}
cfg, _ := os.ReadFile(filepath.Join(dir, ".codecrew.yml"))
if !strings.Contains(string(cfg), "hub: org/hub") {
t.Errorf("pointer = %q", cfg)
}
}

func TestScaffoldIdempotent(t *testing.T) {
dir := t.TempDir()
if _, _, err := scaffold(dir, "self", fakeContracts); err != nil {
t.Fatal(err)
}
marker := filepath.Join(dir, "ROADMAP.md")
if err := os.WriteFile(marker, []byte("edited"), 0o644); err != nil {
t.Fatal(err)
}
written, skipped, err := scaffold(dir, "self", fakeContracts)
if err != nil {
t.Fatal(err)
}
if len(written) != 0 {
t.Errorf("re-run wrote %v", written)
}
if len(skipped) == 0 {
t.Error("re-run reported nothing skipped")
}
if data, _ := os.ReadFile(marker); string(data) != "edited" {
t.Error("re-run clobbered an existing file")
}
}
Loading