Skip to content
Open
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
45 changes: 45 additions & 0 deletions cmd/agentsync/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func TestRunImport_UpdateRefusesAnUncommittedCapture(t *testing.T) {
t.Errorf("the report does not mention %q:\n%s", want, stdout)
}
}
if strings.Contains(stdout, "Git reported:") {
t.Errorf("an uncommitted capture got a Git diagnostic:\n%s", stdout)
}

// The remedy works.
commitRepo(t, repoRoot)
Expand All @@ -258,6 +261,48 @@ func TestRunImport_UpdateRefusesAnUncommittedCapture(t *testing.T) {
}
}

func TestRunImport_UpdateReportsMissingHEADObject(t *testing.T) {
t.Setenv("LC_ALL", "C")
base, repoRoot := importFixture(t)
src := projectAgent(t, base, "proj", "researcher", "current\n")
dest := backupPath(t, repoRoot, "proj", "researcher")
mustWriteT(t, dest, "old capture\n")
commitRepo(t, repoRoot)
testfs.DeleteHeadCommit(t, repoRoot)

args := importArgs(t, repoRoot, base, "--apply", "--update")
for _, jsonOutput := range []bool{false, true} {
runArgs := args
if jsonOutput {
runArgs = append(args, "--json")
}
code, stdout, stderr := runCLI(t, runArgs...)
if code != exitConflict {
t.Fatalf("exit = %d, want %d\nstdout:\n%s\nstderr:\n%s", code, exitConflict, stdout, stderr)
}
reason := stdout
if jsonOutput {
var report importReport
if err := json.Unmarshal([]byte(stdout), &report); err != nil || len(report.Items) != 1 {
t.Fatalf("expected one reported capture, got %+v (%v)\n%s", report.Items, err, stdout)
}
reason = report.Items[0].Reason
}
if !strings.Contains(reason, "Git reported: fatal: not a tree object") {
t.Errorf("the refusal does not name the Git failure:\n%s", reason)
}
if strings.Contains(reason, "commit project-agents/ first") {
t.Errorf("the refusal advises committing to a broken repo:\n%s", reason)
}
if got := readFileT(t, dest); got != "old capture\n" {
t.Errorf("capture was changed to %q", got)
}
if got := readFileT(t, src); got != "current\n" {
t.Errorf("project file was changed to %q", got)
}
}
}

// TestRunImport_SecondUpdateDoesNotDestroyTheFirst is the defect reproduced
// against the built binary, driven the same way here: two ordinary commands,
// no hand-editing.
Expand Down
47 changes: 38 additions & 9 deletions cmd/agentsync/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func TestRunLink_ApplyLinksAndIsIdempotent(t *testing.T) {
if code != exitOK {
t.Fatalf("exit = %d, want %d\nstdout:\n%s\nstderr:\n%s", code, exitOK, stdout, stderr)
}
if strings.Contains(stdout, "Git reported:") {
t.Errorf("a non-repository got a Git diagnostic:\n%s", stdout)
}
assertIsLink(t, path, target)

code, stdout, stderr = runCLI(t, linkArgs(t, repoRoot, base, "--apply")...)
Expand Down Expand Up @@ -427,21 +430,47 @@ func TestRunLink_AgentNameCannotEscapeTheProject(t *testing.T) {
// gate being silently off: git exits non-zero, a repository plainly exists, and
// the adoptable file must survive.
func TestRunLink_TrackedProjectWithAnUnaskableGitIsRefused(t *testing.T) {
requireGit(t)
t.Setenv("LC_ALL", "C")
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
base, repoRoot := linkFixture(t)
const body = "canonical shared agent\n"
globalAgent(t, repoRoot, "shared", body)
path := projectAgentPath(base, "proj", "shared")
mustWriteT(t, path, body)
mustWriteT(t, filepath.Join(base, "proj", ".git"), "gitdir: /nonexistent-agentsync-test\n")

code, stdout, stderr := runCLI(t, linkArgs(t, repoRoot, base, "--apply")...)
if code != exitAtRisk {
t.Fatalf("exit = %d, want %d\nstdout:\n%s\nstderr:\n%s", code, exitAtRisk, stdout, stderr)
}
if !strings.Contains(stdout, "blocked") {
t.Errorf("the report does not say the project was refused:\n%s", stdout)
project := filepath.Join(base, "proj")
commitRepo(t, project)
testfs.DubiousOwnership(t, project)

args := linkArgs(t, repoRoot, base, "--apply")
for _, jsonOutput := range []bool{false, true} {
runArgs := args
if jsonOutput {
runArgs = append(args, "--json")
}
code, stdout, stderr := runCLI(t, runArgs...)
if code != exitAtRisk {
t.Fatalf("exit = %d, want %d\nstdout:\n%s\nstderr:\n%s", code, exitAtRisk, stdout, stderr)
}
reason := stdout
if jsonOutput {
var report linkReport
if err := json.Unmarshal([]byte(stdout), &report); err != nil || len(report.Blocked) != 1 {
t.Fatalf("expected one blocked project, got %+v (%v)\n%s", report.Blocked, err, stdout)
}
reason = report.Blocked[0].Reason
} else if !strings.Contains(stdout, "blocked") {
t.Errorf("the report does not say the project was refused:\n%s", stdout)
}
if !strings.Contains(reason, "Git reported: fatal: detected dubious ownership") {
t.Errorf("the refusal does not name the Git failure:\n%s", reason)
}
assertIsRegular(t, path, "a project whose git could not be asked must not be linked into")
if got := readFileT(t, path); got != body {
t.Errorf("project file was changed to %q", got)
}
}
assertIsRegular(t, path, "a project whose git could not be asked must not be linked into")
}

// --- the home guard ---------------------------------------------------------
Expand Down
62 changes: 38 additions & 24 deletions internal/importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/agentic-tooling/agentsync/internal/scan"
"github.com/agentic-tooling/agentsync/internal/testfs"
)

// --- fixtures ---------------------------------------------------------------
Expand Down Expand Up @@ -704,30 +705,43 @@ func TestNewPlan_UpdateRefusesWhenGitCannotAnswer(t *testing.T) {
func TestApply_UpdateRefusesWhenGitCannotAnswerAtWriteTime(t *testing.T) {
requireGit(t)

f := newFixture(t)
f.project("solo", "bespoke", "the current project file\n")
dest := f.backup("solo", "bespoke", "an older capture\n")
f.commitRepo()

// Planned while git could answer, so this really is an authorised
// update rather than a conflict the planner already rejected.
plan := f.plan(Options{Update: UpdateAll()})
assertActions(t, plan, map[string]Action{"solo/bespoke": ActionUpdate})

noGit(t)

written, errs := plan.Apply()
if len(written) != 0 {
t.Errorf("wrote %v, want nothing", written)
}
if len(errs) != 1 {
t.Fatalf("errors = %v, want exactly one refusal", errs)
}
if !strings.Contains(errs[0].Error(), "refusing to replace") {
t.Errorf("error = %v, want a refusal to replace", errs[0])
}
if got := mustRead(t, dest); got != "an older capture\n" {
t.Errorf("destination = %q — it was replaced without the gate being satisfied", got)
for _, tc := range []struct {
name string
breakGit func(*fixture)
diagnostic string
}{
{"git unavailable", func(f *fixture) { noGit(f.t) }, ""},
{"packed refs corrupt", func(f *fixture) { testfs.CorruptPackedRefs(f.t, f.repoRoot) }, "packed-refs"},
} {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
f.project("solo", "bespoke", "the current project file\n")
dest := f.backup("solo", "bespoke", "an older capture\n")
f.commitRepo()

plan := f.plan(Options{Update: UpdateAll()})
assertActions(t, plan, map[string]Action{"solo/bespoke": ActionUpdate})
tc.breakGit(f)

written, errs := plan.Apply()
if len(written) != 0 {
t.Errorf("wrote %v, want nothing", written)
}
if len(errs) != 1 {
t.Fatalf("errors = %v, want exactly one refusal", errs)
}
reason := errs[0].Error()
if !strings.Contains(reason, "refusing to replace") {
t.Errorf("error = %q, want a refusal to replace", reason)
}
_, reported, hasDiagnostic := strings.Cut(reason, "Git reported: ")
if hasDiagnostic != (tc.diagnostic != "") || !strings.Contains(reported, tc.diagnostic) {
t.Errorf("error = %q, want Git diagnostic %q", reason, tc.diagnostic)
}
if got := mustRead(t, dest); got != "an older capture\n" {
t.Errorf("destination = %q — it was replaced without the gate being satisfied", got)
}
})
}
}

Expand Down
9 changes: 7 additions & 2 deletions internal/importer/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,18 @@ func judgeDest(item Item, opts Options) (verdict, error) {
// a corrupt object store, no repository at all — then lands on the refusing side
// without any call site remembering to handle it.
func commitGate(dest string) (Refusal, string) {
switch scan.CommittedInHEAD(dest) {
committed, diagnostic := scan.CommittedInHEADWithDiagnostic(dest)
switch committed {
case scan.CommittedYes:
return RefusalNone, ""
case scan.CommittedNo:
return RefusalUncommitted, "the captured copy's current bytes are in no commit — it is untracked, or it has been edited since it was last committed — so replacing it would leave them nowhere at all; commit project-agents/ first, then re-run"
default:
return RefusalCommitUnknown, "git could not say whether the captured copy's current bytes are committed, and an unanswered question is not permission to replace what may be the only copy"
reason := "git could not say whether the captured copy's current bytes are committed, and an unanswered question is not permission to replace what may be the only copy"
if diagnostic != "" {
reason += ". Git reported: " + diagnostic
}
return RefusalCommitUnknown, reason
}
}

Expand Down
13 changes: 10 additions & 3 deletions internal/linker/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,14 @@ func gitGate(project, dir string, excludeLocal bool) gate {

default:
note.Note = "git could not be asked about this directory"
reason := "git could not say whether this directory is tracked, and a symlink committed into one breaks every other clone; refusing rather than assuming it is not"
if g.Diagnostic != "" {
reason += ". Git reported: " + g.Diagnostic
}
return gate{note: note, blocked: &Blocked{
Project: project,
Dir: dir,
Reason: "git could not say whether this directory is tracked, and a symlink committed into one breaks every other clone; refusing rather than assuming it is not",
Reason: reason,
}}
}

Expand All @@ -633,11 +637,14 @@ func gitGate(project, dir string, excludeLocal bool) gate {

if g.GitDir == "" || g.Rel == "" {
note.Note = "git did not name this repository's git directory, so no local exclusion could be planned"
reason := fmt.Sprintf("--%s was given but git did not name this repository's git directory, so the links could not be made local; refusing rather than linking into a directory git can see", ExcludeLocalFlag)
if g.GitDirDiagnostic != "" {
reason += ". Git reported: " + g.GitDirDiagnostic
}
return gate{note: note, blocked: &Blocked{
Project: project,
Dir: dir,
Reason: fmt.Sprintf("--%s was given but git did not name this repository's git directory, so the links could not be made local; refusing rather than linking into a directory git can see",
ExcludeLocalFlag),
Reason: reason,
}}
}

Expand Down
12 changes: 11 additions & 1 deletion internal/linker/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ func gitWithoutGitDir(t *testing.T) {
t.Fatalf("git not on PATH: %v", err)
}
bin := t.TempDir()
script := "#!/bin/sh\nfor a in \"$@\"; do [ \"$a\" = --absolute-git-dir ] && exit 128; done\nexec '" + real + "' \"$@\"\n"
script := `#!/bin/sh
if [ "$2" = --absolute-git-dir ]; then
printf '%s\n' 'fatal: git directory unavailable' >&2
exit 128
fi
exec "$AGENTSYNC_TEST_REAL_GIT" "$@"
`
if err := os.WriteFile(filepath.Join(bin, "git"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("AGENTSYNC_TEST_REAL_GIT", real)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
}

Expand All @@ -68,6 +75,9 @@ func TestNewPlan_ExcludeLocalWithoutAGitDirIsRefused(t *testing.T) {
if len(plan.Blocked) != 1 || !strings.Contains(plan.Blocked[0].Reason, "git directory") {
t.Fatalf("blocked = %+v, want the project refused for want of a git directory", plan.Blocked)
}
if !strings.Contains(plan.Blocked[0].Reason, "Git reported: fatal: git directory unavailable") {
t.Errorf("blocked reason omitted the Git diagnostic: %s", plan.Blocked[0].Reason)
}
if len(plan.Excludes) != 0 || len(plan.Writes()) != 0 {
t.Errorf("planned excludes %v and writes %v with nowhere to exclude into", plan.Excludes, plan.Writes())
}
Expand Down
Loading