From bc01c44f3f290bf8fcf6e30c5dbf52ae71d83e6a Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Thu, 26 Nov 2026 17:07:51 +0530 Subject: [PATCH] fix(db): match company domain rules when classifying an agent scope addition isScopeExpansion asked company_scope for kind IN ('root_domain','subdomain') and compared against the value column. company_scope only has kind 'domain', and a domain rule keeps its host in the domain column with value NULL, so the query never matched. Every domain the agent added under a company that was already in the task's declared scope was recorded and reported as a scope expansion. Match kind='domain' rules on the domain column, exactly or as a parent domain. --- db/task_scope.go | 2 +- db/task_scope_company_expansion_test.go | 55 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 db/task_scope_company_expansion_test.go diff --git a/db/task_scope.go b/db/task_scope.go index 5e05009f..11d6f850 100644 --- a/db/task_scope.go +++ b/db/task_scope.go @@ -266,7 +266,7 @@ func (s *AssetStore) isScopeExpansion(taskID int64, candidate TaskScope) (bool, if r.Kind == "company" && r.CompanyID != nil && *r.CompanyID > 0 { var matched int err := s.db.QueryRow( - `SELECT 1 FROM company_scope WHERE company_id=$1 AND kind IN ('root_domain','subdomain') AND ($2 = value OR $2 LIKE '%.' || value) LIMIT 1`, + `SELECT 1 FROM company_scope WHERE company_id=$1 AND kind='domain' AND ($2 = domain OR right($2, length(domain)+1) = '.' || domain) LIMIT 1`, *r.CompanyID, candidate.Domain, ).Scan(&matched) if err == nil && matched == 1 { diff --git a/db/task_scope_company_expansion_test.go b/db/task_scope_company_expansion_test.go new file mode 100644 index 00000000..fc4a41bf --- /dev/null +++ b/db/task_scope_company_expansion_test.go @@ -0,0 +1,55 @@ +package db + +import ( + "fmt" + "testing" + "time" +) + +// A domain the agent adds under a company that is already in the task's declared +// scope does not widen the scope: the company's own domain rules cover it. +func TestAddAgentScopeUnderDeclaredCompanyIsNotExpansion(t *testing.T) { + d, assets, companies := testSetup(t) + t.Cleanup(func() { d.Close() }) + + stamp := time.Now().UnixNano() + root := fmt.Sprintf("acme%d.com", stamp) + companyID, _, _, _, _, err := companies.CreateCompanyWithScope(fmt.Sprintf("acme-%d", stamp), "", + []ScopeInput{{Value: root}}, "test") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cleanupErr(t, "company", companies.DeleteCompany(companyID)) }) + + task, err := d.CreateTask("company scope", "goal", nil, 0, 0) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cleanupErr(t, "task", d.DeleteTask(task.ID)) }) + + if _, err := assets.AddAgentScope(task.ID, "company", fmt.Sprint(companyID), "declared", "manual"); err != nil { + t.Fatal(err) + } + + for _, c := range []struct{ kind, value string }{ + {"subdomain", "api." + root}, + {"root_domain", root}, + } { + ts, err := assets.AddAgentScope(task.ID, c.kind, c.value, "agent", "agent") + if err != nil { + t.Fatalf("%s %s: %v", c.kind, c.value, err) + } + if ts.Expanded { + t.Errorf("%s %s: expanded=true, want false (covered by company %d scope %s)", c.kind, c.value, companyID, root) + } + } + + outside := fmt.Sprintf("other%d.com", stamp) + ts, err := assets.AddAgentScope(task.ID, "root_domain", outside, "agent", "agent") + if err != nil { + t.Fatal(err) + } + if !ts.Expanded { + t.Errorf("root_domain %s: expanded=false, want true (outside the company scope)", outside) + } +}