diff --git a/README.md b/README.md index 161cb73..a818d3c 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,13 @@ wrappers such as `sudo`, `timeout`, `xargs`, `nice`, `env`), or inside `bash -c` and `cat ~/.local/bin/gitpush` name one without running it and are left alone. It guards against mistakes, not adversaries: `'ghmerge'`, `ghm\erge` and `$x` are deliberately not read. +The lease protects the holder's CODE (branch, pull request, merge, release), so +only code writes need it. A TRACKER write -- `ghissue` with `--new`, a comment, +`--close` or `--reopen` (plus `--dry-run` and the flags that carry text) -- is +allowed with or without a claim; when another session holds one the gate prints a +one-line notice naming the holder, so they can be told. Anything it cannot classify +(`ghissue --edit`, `--delete`, `--comment`, an unknown flag, every other program) +stays refused: fail closed. It was written after go-scikit pushed and merged `go-numerics/optimize#62` on a claim that had lapsed and been taken by another session. diff --git a/cmd/agentsync/gate.go b/cmd/agentsync/gate.go index 49b58a8..9497422 100644 --- a/cmd/agentsync/gate.go +++ b/cmd/agentsync/gate.go @@ -144,12 +144,22 @@ func gateHook(stdin io.Reader, out, errOut io.Writer, env gateEnv) { fmt.Fprintln(errOut, "agentsync gate: cannot tell which session this is; allowing (a refusal is not a verdict)") return } + var notice []string for _, inv := range invs { repo, err := env.target(inv) if err != nil { fmt.Fprintf(errOut, "agentsync gate: %s: %v; allowing (a refusal is not a verdict)\n", inv.prog, err) continue } + if trackerWrite(inv) { + // A tracker write touches no file of the holder's, so it needs no lease; the + // holder is only named, so someone can tell them. + if l := env.holder(repo); l != nil { + notice = append(notice, fmt.Sprintf("agentsync gate: %s is claimed by %s until %s; %s writes to its tracker only, which needs no lease. Tell the holder if it matters: agentsync tell %s ...", + repo, short(l.Owner), l.Expires.Format("15:04:05"), inv.prog, short(l.Owner))) + } + continue + } if msg := env.decide(repo, errOut); msg != "" { _ = json.NewEncoder(out).Encode(map[string]any{ "hookSpecificOutput": map[string]any{ @@ -161,6 +171,71 @@ func gateHook(stdin io.Reader, out, errOut io.Writer, env gateEnv) { return } } + if len(notice) > 0 { + for _, n := range notice { + fmt.Fprintln(errOut, n) + } + // Context for the model, with no permission decision: the command goes on + // through the ordinary permission path. + _ = json.NewEncoder(out).Encode(map[string]any{ + "hookSpecificOutput": map[string]any{ + "hookEventName": "PreToolUse", + "additionalContext": strings.Join(notice, "\n"), + }, + }) + } +} + +// trackerBool and trackerValue are the ONLY ghissue flags that write to the +// tracker without touching the repository's code. Anything else (--edit, +// --delete, --comment, an unknown flag) is not classified, so it is a code write +// and keeps needing the lease: fail closed. +var trackerBool = map[string]bool{"--new": true, "--close": true, "--reopen": true, "--dry-run": true} +var trackerValue = map[string]bool{ + "--title": true, "--body-file": true, "--reason": true, "--token-file": true, "--base": true, + "--allow-private-path": true, "--parity-waiver": true, "--prior-art": true, "--prior-art-waiver": true, + "--repo": true, "-repo": true, "-R": true, +} + +// trackerWrite reports whether inv writes to the issue tracker only: a new +// issue, a comment, a close or a reopen. The lease protects the holder's CODE +// (branch, pull request, merge, release); an issue touches no file of theirs. +func trackerWrite(inv invocation) bool { + if inv.prog != "ghissue" { + return false + } + for i := 0; i < len(inv.args); i++ { + a := inv.args[i] + switch { + case trackerBool[a]: + case trackerValue[a]: + i++ + case strings.HasPrefix(a, "-"): + if k := strings.Index(a, "="); k > 0 && trackerValue[a[:k]] { + continue + } + return false + } + } + return true +} + +// holder is the live claim of ANOTHER session covering repo, or nil. +func (g gateEnv) holder(repo string) *lease.Lease { + if info, err := os.Stat(g.store.Dir); err != nil || !info.IsDir() { + return nil + } + ls, err := g.store.Covering(repo) + if err != nil { + return nil + } + now := g.now() + for i := range ls { + if !ls[i].Expired(now) && ls[i].Owner != g.owner { + return &ls[i] + } + } + return nil } // decide returns the refusal for publishing into repo, or "" to allow. @@ -549,7 +624,8 @@ var valueFlags = map[string]bool{ "--evidence": true, "--issues-waiver": true, "--token-file": true, "--wait": true, "--waive": true, "--title": true, "--body-file": true, "--notes-file": true, "--ref": true, "--input": true, "--inputs-file": true, "--confirm": true, - "--backfill-max": true, "-o": true, "--push-option": true, "--receive-pack": true, + "--reason": true, "--prior-art": true, "--prior-art-waiver": true, "--allow-private-path": true, + "--parity-waiver": true, "--base": true, "--backfill-max": true, "-o": true, "--push-option": true, "--receive-pack": true, } // target names the repository a publishing command writes to, as owner/repo. diff --git a/cmd/agentsync/gate_tracker_test.go b/cmd/agentsync/gate_tracker_test.go new file mode 100644 index 0000000..2236259 --- /dev/null +++ b/cmd/agentsync/gate_tracker_test.go @@ -0,0 +1,68 @@ +package main + +import ( + "strings" + "testing" + "time" +) + +// A tracker write touches no file of the holder's: it needs no lease, and the +// holder is only named. Observed 2026-11-18: ghissue --new was refused for 30+ +// minutes by another session's claim. +func TestATrackerWriteIsAllowedWhereAnotherSessionHoldsTheClaim(t *testing.T) { + env, s := gateFixture(t) + if _, err := s.Acquire("go-numerics/optimize", them, "refbench", 45*time.Minute); err != nil { + t.Fatal(err) + } + for _, cmd := range []string{ + "ghissue go-numerics/optimize --new --title T --body-file f", + "ghissue go-numerics/optimize --new --title T --body-file f --dry-run", + "ghissue go-numerics/optimize 12 --body-file f", + "ghissue go-numerics/optimize 12 --close --reason not_planned", + "ghissue go-numerics/optimize 12 --reopen --body-file=f", + "ghissue --repo go-numerics/optimize 12 --body-file f", + `ghissue go-numerics/optimize --new --title "a b" --body-file f --prior-art "read go-numerics/x"`, + } { + denied, reason, errText := ask(env, payload(t, me, "/work/opt", cmd)) + if denied { + t.Errorf("%q refused, but a tracker write needs no lease: %s", cmd, reason) + } + if !strings.Contains(errText, "22222222") || !strings.Contains(errText, "tracker") { + t.Errorf("%q: the notice must name the holder and say tracker; stderr %q", cmd, errText) + } + } +} + +// The other side of the boundary: everything that is not a tracker write is +// still refused, and so is a ghissue flag the classifier does not know. +func TestACodeWriteOrAnUnclassifiedCommandIsStillRefused(t *testing.T) { + env, s := gateFixture(t) + if _, err := s.Acquire("go-numerics/optimize", them, "refbench", 45*time.Minute); err != nil { + t.Fatal(err) + } + for _, cmd := range []string{ + "gitpush -u origin topic", + "ghmerge go-numerics/optimize 62", + "ghrelease go-numerics/optimize 62 v0.1.0", + "ghpr go-numerics/optimize 62 edit", + "ghrel go-numerics/optimize v0.1.0 edit --title T", + "ghrun go-numerics/optimize dispatch ci.yml --ref main", + "ghissue go-numerics/optimize 12 --edit --title T", + "ghissue go-numerics/optimize 12 --delete --confirm go-numerics/optimize#12", + "ghissue go-numerics/optimize --new --title T --body-file f --frobnicate", + "ghissue go-numerics/optimize 12 --body-file f --edit", + "ghissue go-numerics/optimize --new --title T --body-file f && gitpush -u origin topic", + } { + if denied, _, _ := ask(env, payload(t, me, "/work/opt", "cd /work/opt && "+cmd)); !denied { + t.Errorf("%q was allowed into a repository claimed by another session", cmd) + } + } +} + +func TestATrackerWriteWithNoClaimAtAllIsAllowedSilently(t *testing.T) { + env, _ := gateFixture(t) + denied, reason, errText := ask(env, payload(t, me, "/work/opt", "ghissue go-numerics/optimize --new --title T --body-file f")) + if denied || strings.Contains(errText, "claimed") { + t.Fatalf("denied=%v reason=%q stderr=%q", denied, reason, errText) + } +}