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: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
78 changes: 77 additions & 1 deletion cmd/agentsync/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions cmd/agentsync/gate_tracker_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading