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.
This commit is contained in:
Chaitanya Deshpande
2026-11-26 17:07:51 +05:30
parent 3cb06383fd
commit bc01c44f3f
2 changed files with 56 additions and 1 deletions
+1 -1
View File
@@ -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 {
+55
View File
@@ -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)
}
}