mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-17 07:13:50 +08:00
feat: make task global asset sharing opt-in
This commit is contained in:
1 parent
d36e1c3295
commit
080a46eb2d
25 files changed
+838
-328
No files matched your search
+8
-11
@@ -490,6 +490,7 @@ func (s *AssetStore) CountDSL(dsl, typ string, taskID int64) (int, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
where = "(" + where + ")"
|
||||
if typ != "" {
|
||||
args = append(args, typ)
|
||||
where += fmt.Sprintf(" AND type = $%d", len(args))
|
||||
@@ -522,6 +523,7 @@ func (s *AssetStore) QueryDSL(dsl, typ string, taskID int64, limit, offset int)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
where = "(" + where + ")"
|
||||
if typ != "" {
|
||||
args = append(args, typ)
|
||||
where += fmt.Sprintf(" AND type = $%d", len(args))
|
||||
@@ -550,13 +552,9 @@ func (s *AssetStore) QueryDSL(dsl, typ string, taskID int64, limit, offset int)
|
||||
return assets, nil
|
||||
}
|
||||
|
||||
// QueryDSLInScope is QueryDSL restricted to assets that BELONG to taskID's (and its
|
||||
// direct source tasks') declared scope — membership, not literal value: a
|
||||
// root_domain scope returns every subdomain / service / endpoint under it. This is
|
||||
// the agent-facing list_assets path, so an agent queries the task's relevant assets
|
||||
// instead of the whole shared库. taskID<=0 (non-task contexts: Auto / pentest / chat)
|
||||
// has no scope to honor and falls back to the plain global QueryDSL. Rows carry the
|
||||
// same per-task source metadata as QueryByTask.
|
||||
// QueryDSLInScope applies the task's live asset-sharing policy. By default only
|
||||
// owned and directly related assets are visible; sharing enables global reads.
|
||||
// Non-task contexts (taskID<=0) retain global reads.
|
||||
func (s *AssetStore) QueryDSLInScope(dsl, typ string, taskID int64, limit, offset int) ([]*Asset, error) {
|
||||
if taskID <= 0 {
|
||||
return s.QueryDSL(dsl, typ, 0, limit, offset)
|
||||
@@ -578,6 +576,7 @@ func (s *AssetStore) QueryDSLInScope(dsl, typ string, taskID int64, limit, offse
|
||||
}
|
||||
args := []any{taskID}
|
||||
args = append(args, dslArgs...)
|
||||
where = "(" + where + ")"
|
||||
if typ != "" {
|
||||
args = append(args, typ)
|
||||
where += fmt.Sprintf(" AND type = $%d", len(args))
|
||||
@@ -601,10 +600,8 @@ func (s *AssetStore) QueryDSLInScope(dsl, typ string, taskID int64, limit, offse
|
||||
return assets, nil
|
||||
}
|
||||
|
||||
// GetByIDsInScope is GetByIDs restricted to ids that BELONG to taskID's (and its
|
||||
// direct source tasks') declared scope, so an agent cannot reach out-of-scope
|
||||
// assets by id. taskID<=0 (non-task contexts) falls back to the global GetByIDs.
|
||||
// Out-of-scope ids are silently dropped from the result (not an error).
|
||||
// GetByIDsInScope applies the same visibility policy to explicit ID reads.
|
||||
// Invisible IDs are omitted. Non-task contexts retain global reads.
|
||||
func (s *AssetStore) GetByIDsInScope(taskID int64, ids []int64) ([]*Asset, error) {
|
||||
if taskID <= 0 {
|
||||
return s.GetByIDs(ids)
|
||||
|
||||
+126
-95
@@ -1000,127 +1000,158 @@ func TestAssetIPRejectsHostname(t *testing.T) {
|
||||
deleteAsset(d, id)
|
||||
}
|
||||
|
||||
// TestQueryDSLInScopeMembership pins the agent-facing list_assets behavior: it
|
||||
// returns assets that BELONG to the task's declared scope (membership, not literal
|
||||
// value) — a root_domain scope surfaces its subdomains/services/endpoints even when
|
||||
// those rows were produced by a different task — and honors ip/cidr scope for
|
||||
// IP-literal hosts (the try_inet(domain) arm). Out-of-scope assets, including by id,
|
||||
// are excluded. Direct source tasks' scope is included.
|
||||
// TestQueryDSLInScopeMembership verifies that scope matching alone cannot import
|
||||
// unrelated assets, and that the live opt-in switch applies to DSL and ID reads.
|
||||
func TestQueryDSLInScopeMembership(t *testing.T) {
|
||||
d, assets, _ := testSetup(t)
|
||||
defer d.Close()
|
||||
|
||||
task, err := d.CreateTask("scope membership", "goal", nil, 0, 0)
|
||||
d, assets, companies := testSetup(t)
|
||||
t.Cleanup(func() { _ = d.Close() })
|
||||
// Older asset fixtures tag synthetic task IDs 1/2; use a task with no existing membership.
|
||||
task, err := d.CreateTask("isolated task", "goal", nil, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src, err := d.CreateTask("scope source", "goal", nil, 0, 0)
|
||||
source, err := d.CreateTaskWithOptions("source", "goal", TaskCreateOptions{ShareGlobalAssets: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = d.DeleteTask(task.ID); _ = d.DeleteTask(src.ID) })
|
||||
// task ← src is a direct source relation.
|
||||
if _, err := d.Exec(`INSERT INTO task_relations(task_id, source_task_id) VALUES ($1,$2)`, task.ID, src.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stamp := time.Now().UnixNano()
|
||||
root := fmt.Sprintf("sc%d.invalid", stamp) // in-scope root domain (task)
|
||||
srcRoot := fmt.Sprintf("src%d.invalid", stamp) // in-scope via source task
|
||||
out := fmt.Sprintf("out%d.invalid", stamp) // out of every scope
|
||||
marker := fmt.Sprintf("mk%d", stamp) // bare-text token present in all rows
|
||||
foreignTask := stamp + 777 // asset produced by an unrelated task
|
||||
|
||||
// Scope: task owns root; source task owns srcRoot; task owns an IP /24.
|
||||
for _, sc := range []struct {
|
||||
tid int64
|
||||
kind string
|
||||
dom string
|
||||
net string
|
||||
}{
|
||||
{task.ID, "root_domain", root, ""},
|
||||
{src.ID, "root_domain", srcRoot, ""},
|
||||
{task.ID, "ip", "", "198.51.100.0/24"},
|
||||
} {
|
||||
if err := assets.upsertTaskScope(TaskScope{TaskID: sc.tid, Kind: sc.kind, Domain: sc.dom, Net: sc.net}); err != nil {
|
||||
t.Fatalf("seed scope: %v", err)
|
||||
for _, candidate := range []*Task{task, source} {
|
||||
for {
|
||||
var count int
|
||||
if err := d.QueryRow(`SELECT count(*) FROM assets WHERE $1=ANY(task_ids)`, candidate.ID).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
shared := candidate.ShareGlobalAssets
|
||||
_ = d.DeleteTask(candidate.ID)
|
||||
replacement, err := d.CreateTaskWithOptions("clean membership", "goal", TaskCreateOptions{ShareGlobalAssets: shared})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
*candidate = *replacement
|
||||
}
|
||||
}
|
||||
|
||||
// Insert rows directly so root_domain/task_ids are controlled exactly. Every
|
||||
// in-scope row carries foreignTask in task_ids (never the current task) to prove
|
||||
// membership is by scope, not by producer.
|
||||
type row struct {
|
||||
typ, domain, rootDom, url, method, ip, title string
|
||||
if !source.ShareGlobalAssets || task.ShareGlobalAssets {
|
||||
t.Fatal("incorrect creation defaults")
|
||||
}
|
||||
rows := []row{
|
||||
{"subdomain", "api." + root, root, "", "", "", marker}, // under task root
|
||||
{"service", "www." + root, root, "https://www." + root + "/", "", "", marker}, // service under task root
|
||||
{"endpoint", "www." + root, root, "https://www." + root + "/a?" + marker + "=1", "GET", "", ""},
|
||||
{"subdomain", "dev." + srcRoot, srcRoot, "", "", "", marker}, // under source-task root
|
||||
{"endpoint", "198.51.100.9", "198.51.100.9", "http://198.51.100.9:8080/" + marker, "GET", "", ""}, // IP-literal host, ip col empty
|
||||
{"subdomain", "x." + out, out, "", "", "", marker}, // out of scope
|
||||
if _, err = d.Exec(`INSERT INTO task_relations(task_id,source_task_id) VALUES ($1,$2)`, task.ID, source.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var inScopeIDs, outIDs []int64
|
||||
for _, r := range rows {
|
||||
root := fmt.Sprintf("sharing-%d.invalid", time.Now().UnixNano())
|
||||
companyID, _, err := companies.UpsertCompany(root, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ids []int64
|
||||
insert := func(typ, domain, url string, owner int64) int64 {
|
||||
t.Helper()
|
||||
var id int64
|
||||
if err := d.QueryRow(`INSERT INTO assets(type,domain,root_domain,url,method,ip,page_title,task_ids,last_seen)
|
||||
VALUES ($1,NULLIF($2,''),NULLIF($3,''),NULLIF($4,''),NULLIF($5,''),NULLIF($6,''),NULLIF($7,''),ARRAY[$8]::bigint[],now())
|
||||
RETURNING id`,
|
||||
r.typ, r.domain, r.rootDom, r.url, r.method, r.ip, r.title, foreignTask).Scan(&id); err != nil {
|
||||
t.Fatalf("insert %s: %v", r.domain, err)
|
||||
}
|
||||
if r.rootDom == out {
|
||||
outIDs = append(outIDs, id)
|
||||
} else {
|
||||
inScopeIDs = append(inScopeIDs, id)
|
||||
err := d.QueryRow(`INSERT INTO assets(type,domain,root_domain,url,ip,company_id,task_ids,last_seen)
|
||||
VALUES($1,$2,$3,NULLIF($4,''),'198.51.100.9',$5,ARRAY[$6]::bigint[],now()) RETURNING id`, typ, domain, root, url, companyID, owner).Scan(&id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids = append(ids, id)
|
||||
return id
|
||||
}
|
||||
own := insert("subdomain", "www."+root, "", task.ID)
|
||||
inherited := insert("service", "www."+root, "http://www."+root+":8080/", source.ID)
|
||||
foreign := insert("service", "www."+root, "http://www."+root+":9090/", 0)
|
||||
connector := insert("root_domain", root, "", 0)
|
||||
t.Cleanup(func() {
|
||||
_, _ = d.Exec(`DELETE FROM assets WHERE id=ANY($1::bigint[]) OR id=ANY($2::bigint[])`, inScopeIDs, outIDs)
|
||||
_ = d.DeleteTask(task.ID)
|
||||
_ = d.DeleteTask(source.ID)
|
||||
_, _ = assets.DeleteByIDs(ids)
|
||||
_, _ = d.Exec(`DELETE FROM companies WHERE id=$1`, companyID)
|
||||
})
|
||||
|
||||
got, err := assets.QueryDSLInScope(marker, "", task.ID, 50, 0)
|
||||
if err := assets.upsertTaskScope(TaskScope{TaskID: task.ID, Kind: "root_domain", Domain: root}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertIDs := func(got []*Asset, err error, want ...int64) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := map[int64]bool{}
|
||||
for _, a := range got {
|
||||
seen[a.ID] = true
|
||||
}
|
||||
if len(seen) != len(want) {
|
||||
t.Fatalf("got ids %v want %v", seen, want)
|
||||
}
|
||||
for _, id := range want {
|
||||
if !seen[id] {
|
||||
t.Fatalf("missing %d from %v", id, seen)
|
||||
}
|
||||
}
|
||||
}
|
||||
query := fmt.Sprintf("id==%d OR id==%d OR id==%d OR id==%d", foreign, own, inherited, connector)
|
||||
got, err := assets.QueryDSLInScope(query, "", task.ID, 50, 0)
|
||||
assertIDs(got, err, own, inherited)
|
||||
got, err = assets.GetByIDsInScope(task.ID, ids)
|
||||
assertIDs(got, err, own, inherited)
|
||||
got, err = assets.QueryDSL(query, "subdomain", task.ID, 50, 0)
|
||||
assertIDs(got, err, own)
|
||||
count, err := assets.CountDSL(query, "subdomain", task.ID)
|
||||
if err != nil || count != 1 {
|
||||
t.Fatalf("OR bypassed count filters: %d %v", count, err)
|
||||
}
|
||||
// A source's opt-in never grants this task global reads.
|
||||
cov, err := assets.TaskCoverageWithSources(task.ID)
|
||||
if err != nil || cov.Denominator != 2 {
|
||||
t.Fatalf("coverage leak: %+v %v", cov, err)
|
||||
}
|
||||
legacy, err := assets.TaskCoverage(task.ID, task.ExplorationID)
|
||||
if err != nil || legacy.Denominator != 2 {
|
||||
t.Fatalf("legacy coverage leak: %+v %v", legacy, err)
|
||||
}
|
||||
graph, err := assets.BuildCoverageGraph(task.ID, task.ExplorationID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gotIDs := map[int64]bool{}
|
||||
for _, a := range got {
|
||||
gotIDs[a.ID] = true
|
||||
}
|
||||
for _, id := range inScopeIDs {
|
||||
if !gotIDs[id] {
|
||||
t.Fatalf("in-scope asset %d missing from QueryDSLInScope result %v", id, gotIDs)
|
||||
for _, n := range graph.Nodes {
|
||||
if n.AssetID == foreign || n.AssetID == connector {
|
||||
t.Fatalf("foreign graph asset: %+v", n)
|
||||
}
|
||||
}
|
||||
for _, id := range outIDs {
|
||||
if gotIDs[id] {
|
||||
t.Fatalf("out-of-scope asset %d leaked into scoped query", id)
|
||||
}
|
||||
cos, err := companies.ListCompaniesForTask(task.ID)
|
||||
if err != nil || len(cos) != 1 || cos[0].AssetCount != 2 {
|
||||
t.Fatalf("company isolation: %+v %v", cos, err)
|
||||
}
|
||||
|
||||
// GetByIDsInScope: in-scope id returns, out-of-scope id is dropped.
|
||||
mixed := append(append([]int64{}, inScopeIDs[0]), outIDs[0])
|
||||
byID, err := assets.GetByIDsInScope(task.ID, mixed)
|
||||
for _, enabled := range []bool{true, false} {
|
||||
updated, err := d.UpdateTask(task.ID, TaskPatch{ShareGlobalAssets: &enabled})
|
||||
if err != nil || updated.ShareGlobalAssets != enabled {
|
||||
t.Fatalf("update: %+v %v", updated, err)
|
||||
}
|
||||
saved, err := d.GetTask(task.ID)
|
||||
if err != nil || saved.ShareGlobalAssets != enabled {
|
||||
t.Fatalf("reload: %+v %v", saved, err)
|
||||
}
|
||||
want := []int64{own, inherited}
|
||||
if enabled {
|
||||
want = ids
|
||||
}
|
||||
got, err = assets.QueryDSLInScope(query, "", task.ID, 50, 0)
|
||||
assertIDs(got, err, want...)
|
||||
got, err = assets.GetByIDsInScope(task.ID, ids)
|
||||
assertIDs(got, err, want...)
|
||||
}
|
||||
got, err = assets.QueryDSLInScope(query, "", 0, 50, 0)
|
||||
assertIDs(got, err, ids...)
|
||||
empty, err := d.CreateTask("empty task", "goal", nil, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(byID) != 1 || byID[0].ID != inScopeIDs[0] {
|
||||
t.Fatalf("GetByIDsInScope mixed ids → %+v, want only %d", byID, inScopeIDs[0])
|
||||
}
|
||||
|
||||
// taskID<=0 (non-task context) falls back to global: the out-of-scope row is reachable.
|
||||
global, err := assets.QueryDSLInScope(marker, "", 0, 50, 0)
|
||||
defer d.DeleteTask(empty.ID)
|
||||
got, err = assets.QueryDSLInScope(query, "", empty.ID, 50, 0)
|
||||
assertIDs(got, err)
|
||||
// Explicit company selection imports a snapshot even with global sharing off.
|
||||
associated, err := d.CreateTaskWithOptions("company task", "goal", TaskCreateOptions{CompanyIDs: []int64{companyID}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sawOut bool
|
||||
for _, a := range global {
|
||||
if a.ID == outIDs[0] {
|
||||
sawOut = true
|
||||
}
|
||||
}
|
||||
if !sawOut {
|
||||
t.Fatalf("taskID<=0 should fall back to global and include out-of-scope asset %d", outIDs[0])
|
||||
}
|
||||
defer d.DeleteTask(associated.ID)
|
||||
got, err = assets.GetByIDsInScope(associated.ID, ids)
|
||||
assertIDs(got, err, ids...)
|
||||
}
|
||||
@@ -293,6 +293,55 @@ ORDER BY c.name`)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListCompaniesForTask exposes only task-visible ownership and explicitly
|
||||
// associated companies. Isolated tasks never receive unrelated company scopes.
|
||||
func (s *CompanyStore) ListCompaniesForTask(taskID int64) ([]*CompanyWithScope, error) {
|
||||
if taskID <= 0 {
|
||||
return s.ListCompanies()
|
||||
}
|
||||
var shared bool
|
||||
if err := s.db.QueryRow(`SELECT share_global_assets FROM tasks WHERE id=$1 AND deleted_at IS NULL`, taskID).Scan(&shared); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if shared {
|
||||
return s.ListCompanies()
|
||||
}
|
||||
rows, err := s.db.Query(`WITH `+directTaskContextCTE+taskAssetVisibilityCTE+`
|
||||
SELECT c.id,c.name,c.nkey,c.logo,c.created_at::text,c.updated_at::text,COUNT(a.id),
|
||||
EXISTS(SELECT 1 FROM task_scope ts JOIN context_tasks ctx ON ctx.task_id=ts.task_id WHERE ts.company_id=c.id AND ts.kind='company')
|
||||
FROM companies c LEFT JOIN assets a ON a.company_id=c.id AND a.id IN (SELECT id FROM visible_assets)
|
||||
WHERE a.id IS NOT NULL OR EXISTS(SELECT 1 FROM task_scope ts JOIN context_tasks ctx ON ctx.task_id=ts.task_id WHERE ts.company_id=c.id AND ts.kind='company')
|
||||
GROUP BY c.id ORDER BY c.name`, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []*CompanyWithScope{}
|
||||
associated := map[int64]bool{}
|
||||
for rows.Next() {
|
||||
c := &CompanyWithScope{Scope: []ScopeRule{}}
|
||||
var explicit bool
|
||||
if err := rows.Scan(&c.ID, &c.Name, &c.NKey, &c.Logo, &c.CreatedAt, &c.UpdatedAt, &c.AssetCount, &explicit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
associated[c.ID] = explicit
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Only explicit company associations grant access to the company's scope.
|
||||
for _, c := range out {
|
||||
if associated[c.ID] {
|
||||
c.Scope, err = s.GetScope(c.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GetScope returns all scope rules for a company.
|
||||
func (s *CompanyStore) GetScope(companyID int64) ([]ScopeRule, error) {
|
||||
rows, err := s.db.Query(`
|
||||
|
||||
@@ -550,6 +550,8 @@ ALTER TABLE tasks ADD COLUMN IF NOT EXISTS queued BOOLEAN NOT NULL DEFAULT false
|
||||
-- 给 agent 开放 add_task_scope/list_untested_assets;false=全部关闭(见 task_scope.go)。
|
||||
-- 存量任务默认 true 保持原行为;company 关联(task_scope kind=company)不受此开关影响。
|
||||
ALTER TABLE tasks ADD COLUMN IF NOT EXISTS coverage_enabled BOOLEAN NOT NULL DEFAULT true;
|
||||
-- Global asset reuse is opt-in, including for pre-existing tasks.
|
||||
ALTER TABLE tasks ADD COLUMN IF NOT EXISTS share_global_assets BOOLEAN NOT NULL DEFAULT false;
|
||||
-- 任务级 Worker 运行数量;0=用全局默认(settings.workers)。补旧库。
|
||||
ALTER TABLE tasks ADD COLUMN IF NOT EXISTS worker_count INTEGER NOT NULL DEFAULT 0;
|
||||
-- queued_at makes admission FIFO reflect the actual enqueue order rather than the
|
||||
|
||||
@@ -122,7 +122,7 @@ AND NOT EXISTS (
|
||||
if _, err := tx.Exec(`UPDATE tasks SET
|
||||
name='',remark='',category_id=NULL,description='',goal='',paused=true,queued=false,queued_at=NULL,queue_mode='',
|
||||
llm_profile_id=NULL,active_llm_profile_id=NULL,llm_chain_revision=llm_chain_revision+1,
|
||||
company_id=NULL,parent_ref=NULL,timeout_seconds=0,coverage_enabled=true,worker_count=0,pinned_at=NULL,
|
||||
company_id=NULL,parent_ref=NULL,timeout_seconds=0,coverage_enabled=true,share_global_assets=false,worker_count=0,pinned_at=NULL,
|
||||
first_run_at=NULL,deadline_at=NULL,archived_at=now(),deleted_at=now()
|
||||
WHERE id=$1`, taskID); err != nil {
|
||||
return err
|
||||
@@ -322,7 +322,7 @@ func restoreTaskStub(tx *sql.Tx, row map[string]any, taskID, remaining int64) er
|
||||
llm_profile_id=archived.llm_profile_id,active_llm_profile_id=archived.active_llm_profile_id,
|
||||
llm_chain_revision=archived.llm_chain_revision,company_id=archived.company_id,parent_ref=archived.parent_ref,
|
||||
timeout_seconds=archived.timeout_seconds,plan_heartbeat_seconds=archived.plan_heartbeat_seconds,
|
||||
coverage_enabled=archived.coverage_enabled,worker_count=COALESCE(archived.worker_count,0),pinned_at=archived.pinned_at,first_run_at=archived.first_run_at,
|
||||
coverage_enabled=archived.coverage_enabled,share_global_assets=COALESCE(archived.share_global_assets,false),worker_count=COALESCE(archived.worker_count,0),pinned_at=archived.pinned_at,first_run_at=archived.first_run_at,
|
||||
deadline_at=CASE WHEN archived.paused AND $3>0 THEN now()+make_interval(secs=>$3::double precision)
|
||||
ELSE archived.deadline_at END,
|
||||
deleted_at=NULL,archived_at=NULL,completed_at=archived.completed_at,
|
||||
|
||||
@@ -133,7 +133,7 @@ func TestTaskArchiveDatabaseRoundTrip(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := d.CreateTaskWithOptions("archive database roundtrip", "restore exact graph", TaskCreateOptions{
|
||||
Name: "cold task", WorkerCount: 7,
|
||||
Name: "cold task", WorkerCount: 7, ShareGlobalAssets: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -299,7 +299,7 @@ VALUES($1,$2,0,'quota_exhausted','balance exhausted',$3,$4,$3)`, task.ID, llmPro
|
||||
if err != nil || live == nil {
|
||||
t.Fatalf("restored task = %+v, %v", live, err)
|
||||
}
|
||||
if live.Name != "cold task" || live.Remark != remark || live.WorkerCount != 7 || !live.Paused {
|
||||
if live.Name != "cold task" || live.Remark != remark || live.WorkerCount != 7 || !live.ShareGlobalAssets || !live.Paused {
|
||||
t.Fatalf("restored task metadata mismatch: %+v", live)
|
||||
}
|
||||
if err := d.QueryRow(`SELECT count(*) FROM exploration_nodes WHERE exploration_id=$1`, task.ExplorationID).Scan(&nodes); err != nil {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTaskAssetSharingOffAllowsManualAssociations(t *testing.T) {
|
||||
d, assets, _ := testSetup(t)
|
||||
defer d.Close()
|
||||
task, err := d.CreateTask("manual assets while sharing off", "goal", nil, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.DeleteTask(task.ID)
|
||||
if task.ShareGlobalAssets {
|
||||
t.Fatal("task must default to sharing off")
|
||||
}
|
||||
var ids []int64
|
||||
defer func() { _, _ = assets.DeleteByIDs(ids) }()
|
||||
for _, name := range []string{"selected", "unrelated"} {
|
||||
id, err := assets.UpsertRootDomain(UpsertRootDomainReq{Domain: fmt.Sprintf("manual-sharing-%d-%s.invalid", task.ID, name)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if got, err := assets.GetByIDsInScope(task.ID, ids); err != nil || len(got) != 0 {
|
||||
t.Fatalf("unassociated global assets must be invisible: got=%v err=%v", got, err)
|
||||
}
|
||||
if _, err := assets.AttachAssetsToTask(task.ID, ids[:1], "用户明确选择的测试资产"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := assets.GetByIDsInScope(task.ID, ids)
|
||||
if err != nil || len(got) != 1 || got[0].ID != ids[0] {
|
||||
t.Fatalf("only the manually associated asset should be visible: got=%v err=%v", got, err)
|
||||
}
|
||||
query := fmt.Sprintf("id==%d OR id==%d", ids[0], ids[1])
|
||||
got, err = assets.QueryDSLInScope(query, "", task.ID, 10, 0)
|
||||
if err != nil || len(got) != 1 || got[0].ID != ids[0] {
|
||||
t.Fatalf("DSL must allow the association without exposing unrelated assets: got=%v err=%v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskAssetSharingLegacyArchiveDefault(t *testing.T) {
|
||||
d, _, _ := testSetup(t)
|
||||
defer d.Close()
|
||||
task, err := d.CreateTaskWithOptions("legacy sharing archive", "goal", TaskCreateOptions{ShareGlobalAssets: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.DeleteTask(task.ID)
|
||||
var raw []byte
|
||||
if err := d.QueryRow(`SELECT row_to_json(t) FROM tasks t WHERE id=$1`, task.ID).Scan(&raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var row map[string]any
|
||||
if err := json.Unmarshal(raw, &row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
delete(row, "share_global_assets")
|
||||
tx, err := d.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := restoreTaskStub(tx, row, task.ID, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var enabled bool
|
||||
if err := tx.QueryRow(`SELECT share_global_assets FROM tasks WHERE id=$1`, task.ID).Scan(&enabled); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if enabled {
|
||||
t.Fatal("archives created before the sharing field must restore with sharing disabled")
|
||||
}
|
||||
}
|
||||
+30
-37
@@ -21,7 +21,25 @@ context_tasks AS (
|
||||
WHERE relation.task_id=$1
|
||||
)`
|
||||
|
||||
const contextCoverageCTE = directTaskContextCTE + `,
|
||||
// taskAssetVisibilityCTE reads the policy on every query. Explicit source tasks
|
||||
// contribute their own assets, never their global-sharing setting.
|
||||
const taskAssetVisibilityCTE = `,
|
||||
visible_assets AS (
|
||||
SELECT a.id FROM assets a
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM tasks current
|
||||
WHERE current.id=$1 AND current.deleted_at IS NULL AND current.share_global_assets
|
||||
)
|
||||
OR EXISTS (SELECT 1 FROM context_tasks ctx WHERE ctx.task_id=ANY(a.task_ids))
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM exploration_anchors anchor
|
||||
JOIN exploration_nodes node ON node.id=anchor.node_id
|
||||
JOIN context_tasks ctx ON ctx.exploration_id=node.exploration_id
|
||||
WHERE anchor.asset_id=a.id
|
||||
)
|
||||
)`
|
||||
|
||||
const contextCoverageCTE = directTaskContextCTE + taskAssetVisibilityCTE + `,
|
||||
target AS (
|
||||
SELECT DISTINCT a.id, a.type,
|
||||
COALESCE(a.url, a.domain, a.ip, a.app_name, a.root_domain, '') AS label
|
||||
@@ -37,6 +55,10 @@ target AS (
|
||||
))
|
||||
)
|
||||
JOIN context_tasks ctx ON ctx.task_id=ts.task_id
|
||||
WHERE a.id IN (SELECT id FROM visible_assets)
|
||||
UNION
|
||||
SELECT a.id,a.type,COALESCE(a.url,a.domain,a.ip,a.app_name,a.root_domain,'') AS label
|
||||
FROM assets a WHERE EXISTS (SELECT 1 FROM context_tasks ctx WHERE ctx.task_id=ANY(a.task_ids))
|
||||
UNION
|
||||
SELECT a.id, a.type,
|
||||
COALESCE(a.url, a.domain, a.ip, a.app_name, a.root_domain, '') AS label
|
||||
@@ -52,41 +74,11 @@ tested AS (
|
||||
JOIN context_tasks ctx ON ctx.exploration_id=en.exploration_id
|
||||
)`
|
||||
|
||||
// scopeTargetCTE selects every asset that BELONGS to the current task's (and its
|
||||
// direct source tasks') declared scope — membership, not literal value: a
|
||||
// root_domain scope pulls in every subdomain / service / endpoint whose own
|
||||
// root_domain column equals it; an ip/cidr scope pulls in assets whose ip OR
|
||||
// IP-literal host falls inside the net. $1 is the task id. Unlike contextCoverageCTE
|
||||
// it carries neither the fact-anchor union nor the tested set — it is pure "in
|
||||
// declared scope", independent of what has already been touched. Used by the
|
||||
// agent's list_assets so a query returns the task's relevant assets, not the
|
||||
// whole shared库.
|
||||
const scopeTargetCTE = `
|
||||
context_tasks AS (
|
||||
SELECT t.id AS task_id
|
||||
FROM tasks t
|
||||
WHERE t.id=$1 AND t.deleted_at IS NULL
|
||||
UNION ALL
|
||||
SELECT source.id
|
||||
FROM task_relations relation
|
||||
JOIN tasks source ON source.id=relation.source_task_id AND source.deleted_at IS NULL
|
||||
WHERE relation.task_id=$1
|
||||
),
|
||||
target AS (
|
||||
SELECT DISTINCT a.id
|
||||
FROM assets a
|
||||
JOIN task_scope ts ON (
|
||||
(ts.kind='company' AND a.company_id = ts.company_id)
|
||||
OR (ts.kind='root_domain' AND a.root_domain = ts.domain)
|
||||
OR (ts.kind='subdomain' AND a.domain = ts.domain)
|
||||
OR (ts.kind IN ('ip','cidr') AND (ts.net >>= try_inet(a.ip) OR ts.net >>= try_inet(a.domain)))
|
||||
OR (ts.kind='icp' AND (
|
||||
lower(regexp_replace(COALESCE(a.icp,''), '[[:space:]]+', '', 'g')) = ts.value
|
||||
OR lower(regexp_replace(COALESCE(a.app_icp,''), '[[:space:]]+', '', 'g')) = ts.value
|
||||
))
|
||||
)
|
||||
JOIN context_tasks ctx ON ctx.task_id=ts.task_id
|
||||
)`
|
||||
// scopeTargetCTE applies the live visibility policy to DSL and ID reads.
|
||||
// An isolated task sees only its own and explicitly related tasks' assets.
|
||||
const scopeTargetCTE = directTaskContextCTE + taskAssetVisibilityCTE + `,
|
||||
target AS (SELECT id FROM visible_assets)
|
||||
`
|
||||
|
||||
// ListTaskScopeWithSources returns the current task's scope followed by the
|
||||
// scopes of its direct source tasks. TaskScope.TaskID preserves provenance.
|
||||
@@ -199,7 +191,7 @@ ORDER BY target.id LIMIT $`+limitPosition+` OFFSET $`+offsetPosition, pageArgs..
|
||||
// used for destructive task cleanup; HostsByTask intentionally retains that
|
||||
// narrower, task-owned behavior.
|
||||
func (s *AssetStore) HostsByTaskWithSources(taskID int64) ([]string, error) {
|
||||
rows, err := s.db.Query(`WITH `+directTaskContextCTE+`,
|
||||
rows, err := s.db.Query(`WITH `+directTaskContextCTE+taskAssetVisibilityCTE+`,
|
||||
context_assets AS (
|
||||
SELECT DISTINCT a.id
|
||||
FROM assets a
|
||||
@@ -223,6 +215,7 @@ context_assets AS (
|
||||
))
|
||||
)
|
||||
JOIN context_tasks ctx ON ctx.task_id=ts.task_id
|
||||
WHERE a.id IN (SELECT id FROM visible_assets)
|
||||
)
|
||||
SELECT COALESCE(a.domain,''), COALESCE(a.ip,''), COALESCE(a.url,'')
|
||||
FROM assets a JOIN context_assets ctx ON ctx.id=a.id`, taskID)
|
||||
|
||||
+4
-3
@@ -300,7 +300,7 @@ func (s *AssetStore) CoverageEnabled(taskID int64) bool {
|
||||
}
|
||||
|
||||
// task_scope→assets match predicate, reused by the count / by-type / untested queries. $1=taskID.
|
||||
const covTargetCTE = `
|
||||
const covTargetCTE = directTaskContextCTE + taskAssetVisibilityCTE + `,
|
||||
target AS (
|
||||
SELECT DISTINCT a.id, a.type,
|
||||
COALESCE(a.url, a.domain, a.ip, a.app_name, a.root_domain, '') AS label
|
||||
@@ -315,6 +315,7 @@ target AS (
|
||||
OR lower(regexp_replace(COALESCE(a.app_icp,''), '[[:space:]]+', '', 'g')) = ts.value
|
||||
))
|
||||
)
|
||||
WHERE a.id IN (SELECT id FROM visible_assets)
|
||||
),
|
||||
tested AS (
|
||||
SELECT DISTINCT ea.asset_id
|
||||
@@ -554,8 +555,8 @@ ORDER BY a.id`, taskID)
|
||||
}
|
||||
for root := range missingRoots {
|
||||
var id, companyID int64
|
||||
err := s.db.QueryRow(`SELECT id, COALESCE(company_id,0) FROM assets
|
||||
WHERE type='root_domain' AND domain=$1 LIMIT 1`, root).Scan(&id, &companyID)
|
||||
err := s.db.QueryRow(`WITH `+scopeTargetCTE+` SELECT id, COALESCE(company_id,0) FROM assets
|
||||
WHERE type='root_domain' AND domain=$2 AND id IN (SELECT id FROM target) LIMIT 1`, taskID, root).Scan(&id, &companyID)
|
||||
var node CoverageGraphNode
|
||||
if err == nil && id > 0 {
|
||||
node = CoverageGraphNode{Key: assetKey(id), Kind: "root_domain", AssetID: id,
|
||||
|
||||
+19
-14
@@ -55,7 +55,8 @@ type Task struct {
|
||||
// 覆盖度、不自动累积 task_scope(source=auto)、不给 agent 开放 add_task_scope/
|
||||
// list_untested_assets、态势里不注入 coverage 块(scope 字段仍保留)。company 关联
|
||||
// (task_scope kind=company)与此开关无关,永不受影响。见 db/task_scope.go。
|
||||
CoverageEnabled bool `json:"coverage_enabled"`
|
||||
CoverageEnabled bool `json:"coverage_enabled"`
|
||||
ShareGlobalAssets bool `json:"share_global_assets"`
|
||||
// WorkerCount is the per-task concurrent work-agent count. 0 means use the
|
||||
// global settings.workers default when the engine starts this task.
|
||||
WorkerCount int `json:"worker_count"`
|
||||
@@ -187,6 +188,8 @@ type TaskCreateOptions struct {
|
||||
// CoverageEnabled 是「资产覆盖度功能」开关;nil=默认开(true),让不关心该开关的创建
|
||||
// 路径(编排 spawn、老 API)沿用原行为。仅 web 创建任务时可显式传 false 关闭。
|
||||
CoverageEnabled *bool
|
||||
// ShareGlobalAssets allows reading unrelated global assets. The default is false.
|
||||
ShareGlobalAssets bool
|
||||
// WorkerCount 是本任务并发运行的工作 Agent 数;0/省略=用全局默认。上限 50。
|
||||
WorkerCount int
|
||||
// Remark 是创建时可带的任务备注(最多 2000 字);空=无备注。
|
||||
@@ -268,12 +271,12 @@ VALUES ($1, 'fact', $2, 0, 'origin', 'system')`, expID, string(originPayload));
|
||||
SourceTaskIDs: append([]int64(nil), opts.SourceTaskIDs...),
|
||||
CompanyIDs: append([]int64(nil), opts.CompanyIDs...),
|
||||
TimeoutSeconds: opts.TimeoutSeconds, PlanHeartbeatSeconds: opts.PlanHeartbeatSeconds,
|
||||
CoverageEnabled: coverageEnabled, WorkerCount: opts.WorkerCount, Remark: opts.Remark,
|
||||
CoverageEnabled: coverageEnabled, ShareGlobalAssets: opts.ShareGlobalAssets, WorkerCount: opts.WorkerCount, Remark: opts.Remark,
|
||||
}
|
||||
if err := tx.QueryRow(`
|
||||
INSERT INTO tasks(name, remark, category_id, description, goal, exploration_id, llm_profile_id, active_llm_profile_id, timeout_seconds, plan_heartbeat_seconds, coverage_enabled, worker_count)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$7,$8,$9,$10,$11)
|
||||
RETURNING id, status, paused, created_at`, opts.Name, opts.Remark, opts.CategoryID, description, goal, expID, active, opts.TimeoutSeconds, opts.PlanHeartbeatSeconds, coverageEnabled, opts.WorkerCount).Scan(&t.ID, &t.Status, &t.Paused, &t.CreatedAt); err != nil {
|
||||
INSERT INTO tasks(name, remark, category_id, description, goal, exploration_id, llm_profile_id, active_llm_profile_id, timeout_seconds, plan_heartbeat_seconds, coverage_enabled, worker_count, share_global_assets)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$7,$8,$9,$10,$11,$12)
|
||||
RETURNING id, status, paused, created_at`, opts.Name, opts.Remark, opts.CategoryID, description, goal, expID, active, opts.TimeoutSeconds, opts.PlanHeartbeatSeconds, coverageEnabled, opts.WorkerCount, opts.ShareGlobalAssets).Scan(&t.ID, &t.Status, &t.Paused, &t.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := insertTaskRelations(tx, t.ID, opts.SourceTaskIDs); err != nil {
|
||||
@@ -410,11 +413,11 @@ func insertTaskLLMProfiles(tx *sql.Tx, taskID int64, profileIDs []int64) error {
|
||||
|
||||
const taskCols = `id, COALESCE(name,''), remark, category_id,
|
||||
COALESCE((SELECT category.name FROM task_categories category WHERE category.id=tasks.category_id),''),
|
||||
description, goal, exploration_id, status, paused, COALESCE(pause_reason,''), queued, queued_at, COALESCE(queue_mode,''), llm_profile_id, active_llm_profile_id, COALESCE(parent_ref,''), pinned_at, created_at, completed_at, COALESCE(timeout_seconds,0), COALESCE(plan_heartbeat_seconds,300), COALESCE(coverage_enabled,true), COALESCE(worker_count,0), first_run_at, deadline_at, COALESCE(active_run_seconds,0), run_segment_started_at`
|
||||
description, goal, exploration_id, status, paused, COALESCE(pause_reason,''), queued, queued_at, COALESCE(queue_mode,''), llm_profile_id, active_llm_profile_id, COALESCE(parent_ref,''), pinned_at, created_at, completed_at, COALESCE(timeout_seconds,0), COALESCE(plan_heartbeat_seconds,300), COALESCE(coverage_enabled,true), share_global_assets, COALESCE(worker_count,0), first_run_at, deadline_at, COALESCE(active_run_seconds,0), run_segment_started_at`
|
||||
|
||||
func scanTask(sc interface{ Scan(...any) error }) (*Task, error) {
|
||||
var t Task
|
||||
if err := sc.Scan(&t.ID, &t.Name, &t.Remark, &t.CategoryID, &t.CategoryName, &t.Description, &t.Goal, &t.ExplorationID, &t.Status, &t.Paused, &t.PauseReason, &t.Queued, &t.QueuedAt, &t.QueueMode, &t.LLMProfileID, &t.ActiveLLMProfileID, &t.ParentRef, &t.PinnedAt, &t.CreatedAt, &t.CompletedAt, &t.TimeoutSeconds, &t.PlanHeartbeatSeconds, &t.CoverageEnabled, &t.WorkerCount, &t.FirstRunAt, &t.DeadlineAt, &t.ActiveRunSeconds, &t.RunSegmentStartedAt); err != nil {
|
||||
if err := sc.Scan(&t.ID, &t.Name, &t.Remark, &t.CategoryID, &t.CategoryName, &t.Description, &t.Goal, &t.ExplorationID, &t.Status, &t.Paused, &t.PauseReason, &t.Queued, &t.QueuedAt, &t.QueueMode, &t.LLMProfileID, &t.ActiveLLMProfileID, &t.ParentRef, &t.PinnedAt, &t.CreatedAt, &t.CompletedAt, &t.TimeoutSeconds, &t.PlanHeartbeatSeconds, &t.CoverageEnabled, &t.ShareGlobalAssets, &t.WorkerCount, &t.FirstRunAt, &t.DeadlineAt, &t.ActiveRunSeconds, &t.RunSegmentStartedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.Pinned = t.PinnedAt != nil
|
||||
@@ -454,14 +457,15 @@ ORDER BY (pinned_at IS NOT NULL) DESC, pinned_at DESC NULLS LAST, id DESC`)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// TaskPatch updates task list metadata without changing execution state.
|
||||
// TaskPatch updates editable metadata and global asset visibility.
|
||||
type TaskPatch struct {
|
||||
Name *string
|
||||
Remark *string
|
||||
Pinned *bool
|
||||
Name *string
|
||||
Remark *string
|
||||
Pinned *bool
|
||||
ShareGlobalAssets *bool
|
||||
}
|
||||
|
||||
// UpdateTask applies a partial task name/remark/pin mutation and returns the updated row.
|
||||
// UpdateTask applies a partial metadata/visibility mutation and returns the updated row.
|
||||
// Re-pinning an already pinned task preserves its original position.
|
||||
func (d *DB) UpdateTask(id int64, patch TaskPatch) (*Task, error) {
|
||||
task, err := scanTask(d.QueryRow(`UPDATE tasks SET
|
||||
@@ -471,9 +475,10 @@ func (d *DB) UpdateTask(id int64, patch TaskPatch) (*Task, error) {
|
||||
WHEN $4::boolean IS NULL THEN pinned_at
|
||||
WHEN $4::boolean THEN COALESCE(pinned_at, now())
|
||||
ELSE NULL
|
||||
END
|
||||
END,
|
||||
share_global_assets = COALESCE($7::boolean, share_global_assets)
|
||||
WHERE id=$1 AND deleted_at IS NULL
|
||||
RETURNING `+taskCols, id, patch.Name != nil, patch.Name, patch.Pinned, patch.Remark != nil, patch.Remark))
|
||||
RETURNING `+taskCols, id, patch.Name != nil, patch.Name, patch.Pinned, patch.Remark != nil, patch.Remark, patch.ShareGlobalAssets))
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user