mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-18 22:20:56 +08:00
Merge pull request #303 from Protonmatter/protonmatter/coverage-flag-error
Keep coverage tools available when the flag read fails
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
)
|
||||
|
||||
// coverageOn reports whether asset-coverage tools stay available for this task.
|
||||
// A nil store is the historical default (enabled). A database error is logged and
|
||||
// treated as enabled so a transient read failure does not drop the tools mid-run.
|
||||
func coverageOn(as *db.AssetStore, taskID int64) bool {
|
||||
if as == nil {
|
||||
return true
|
||||
}
|
||||
enabled, err := as.CoverageEnabled(taskID)
|
||||
if err != nil {
|
||||
log.Printf("[coverage] task %d: reading the coverage flag failed; leaving coverage on: %v", taskID, err)
|
||||
return true
|
||||
}
|
||||
return enabled
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
)
|
||||
|
||||
// A failed coverage-flag read stays enabled: the log says so, the worker keeps
|
||||
// record_coverage and list_untested_assets, and AddAutoScope still writes. That
|
||||
// write happens even when the stored flag is off, which is the outage behaviour.
|
||||
func TestCoverageReadFailureKeepsToolsAndWritesAutoScope(t *testing.T) {
|
||||
d := testDB(t)
|
||||
defer d.Close()
|
||||
off := false
|
||||
task, err := d.CreateTaskWithOptions("coverage outage", "goal", db.TaskCreateOptions{CoverageEnabled: &off})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = d.DeleteTask(task.ID) })
|
||||
|
||||
live := d.Assets()
|
||||
stored, err := live.CoverageEnabled(task.ID)
|
||||
if err != nil || stored {
|
||||
t.Fatalf("stored flag enabled=%v err=%v, want off", stored, err)
|
||||
}
|
||||
if coverageOn(live, task.ID) {
|
||||
t.Fatal("a readable off flag was treated as on")
|
||||
}
|
||||
|
||||
dsn, _, err := db.DSN()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
broken, err := db.Open(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := broken.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var logged bytes.Buffer
|
||||
prev := log.Default().Writer()
|
||||
log.SetOutput(&logged)
|
||||
t.Cleanup(func() { log.SetOutput(prev) })
|
||||
if !coverageOn(broken.Assets(), task.ID) {
|
||||
t.Fatal("a failed coverage read turned the feature off")
|
||||
}
|
||||
if !strings.Contains(logged.String(), "leaving coverage on") {
|
||||
t.Fatalf("coverage read failure was not logged: %q", logged.String())
|
||||
}
|
||||
|
||||
// record_coverage is the worker's coverage tool; list_untested_assets is the
|
||||
// planner's, so each is checked on the tool set that actually carries it.
|
||||
hasPlannerTool := func(ts *ToolSet, name string) bool {
|
||||
for _, tool := range ts.DropCoverageTools(ts.PlannerTools()) {
|
||||
if tool.Name() == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
offWorker := NewToolSet(nil, "worker-off")
|
||||
offWorker.SetCoverageEnabled(false)
|
||||
if hasTool(offWorker, "record_coverage") {
|
||||
t.Fatal("record_coverage stayed available to a worker with coverage off")
|
||||
}
|
||||
offPlanner := NewToolSet(nil, "planner-off")
|
||||
offPlanner.SetCoverageEnabled(false)
|
||||
if hasPlannerTool(offPlanner, "list_untested_assets") {
|
||||
t.Fatal("list_untested_assets stayed available to the planner with coverage off")
|
||||
}
|
||||
outage := coverageOn(broken.Assets(), task.ID)
|
||||
outWorker := NewToolSet(nil, "worker-outage")
|
||||
outWorker.SetCoverageEnabled(outage)
|
||||
if !hasTool(outWorker, "record_coverage") {
|
||||
t.Fatal("record_coverage dropped from the worker when the coverage read failed")
|
||||
}
|
||||
outPlanner := NewToolSet(nil, "planner-outage")
|
||||
outPlanner.SetCoverageEnabled(outage)
|
||||
if !hasPlannerTool(outPlanner, "list_untested_assets") {
|
||||
t.Fatal("list_untested_assets dropped from the planner when the coverage read failed")
|
||||
}
|
||||
|
||||
keptOff := fmt.Sprintf("off-%d.invalid", time.Now().UnixNano())
|
||||
writeIntentAutoScope(live, task.ID, []*db.Asset{{Type: "root_domain", Domain: keptOff}}, false)
|
||||
wrote := fmt.Sprintf("outage-%d.invalid", time.Now().UnixNano())
|
||||
writeIntentAutoScope(live, task.ID, []*db.Asset{{Type: "root_domain", Domain: wrote}}, coverageOn(broken.Assets(), task.ID))
|
||||
scope, err := live.ListTaskScope(task.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sawKept, sawWrote bool
|
||||
for _, row := range scope {
|
||||
if row.Domain == keptOff {
|
||||
sawKept = true
|
||||
}
|
||||
if row.Domain == wrote {
|
||||
sawWrote = true
|
||||
}
|
||||
}
|
||||
if sawKept {
|
||||
t.Fatal("auto scope was written while coverage was off")
|
||||
}
|
||||
if !sawWrote {
|
||||
t.Fatal("auto scope was not written when the coverage read failed")
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -118,7 +118,7 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
|
||||
tsx.SetAssetStore(as, as.Companies())
|
||||
}
|
||||
tsx.SetTaskID(taskID)
|
||||
tsx.SetCoverageEnabled(as == nil || as.CoverageEnabled(taskID))
|
||||
tsx.SetCoverageEnabled(coverageOn(as, taskID))
|
||||
tsx.SetNotify(notify) // generic wake-up (write ops without a dedicated callback go through it, debounced)
|
||||
tsx.SetResumeTask(resume) // set_goals adds a goal → pull a completed/paused task back to running
|
||||
tsx.SetNotifyGoal(notifyGoal) // set_goals adds a goal → record a "human added a goal: …" trigger for the planner
|
||||
|
||||
+1
-1
@@ -473,7 +473,7 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
|
||||
tsx.SetAssetStore(as, as.Companies())
|
||||
}
|
||||
tsx.SetTaskID(taskID)
|
||||
tsx.SetCoverageEnabled(as == nil || as.CoverageEnabled(taskID))
|
||||
tsx.SetCoverageEnabled(coverageOn(as, taskID))
|
||||
tsx.killWork = p.killWork // enable kill_work tool (nil = unavailable)
|
||||
tsx.steerWork = p.steerWork // enable steer_work tool (nil = unavailable)
|
||||
if origin, _ := ts.OriginFactID(); origin > 0 {
|
||||
|
||||
+19
-7
@@ -387,6 +387,21 @@ func renderWorkerGraphOverview(data map[string]any, env dataEnvelope) string {
|
||||
sections
|
||||
}
|
||||
|
||||
// writeIntentAutoScope records the intent's target assets as auto scope when
|
||||
// coverage is on. coverageEnabled is fail-open: a failed flag read is true, so
|
||||
// an outage still writes, including when the stored flag is off.
|
||||
func writeIntentAutoScope(as *db.AssetStore, taskID int64, assets []*db.Asset, coverageEnabled bool) {
|
||||
if as == nil || !coverageEnabled {
|
||||
return
|
||||
}
|
||||
for _, a := range assets {
|
||||
if a == nil {
|
||||
continue
|
||||
}
|
||||
_ = as.AddAutoScope(taskID, a.Type, a.Domain, a.URL, a.IP)
|
||||
}
|
||||
}
|
||||
|
||||
// Execute runs one intent. hooks (the per-task Guard) gates every tool call; may
|
||||
// be nil. emit, if non-nil, receives one ActivityRecord per execution step.
|
||||
// notifyFinding, if non-nil, is called (intentID, summary) when this worker writes
|
||||
@@ -412,7 +427,7 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
|
||||
tsx := NewToolSet(ts, name)
|
||||
tsx.SetFindingRecorder(w.findingRecorder)
|
||||
tsx.SetTaskID(taskID)
|
||||
coverageEnabled := as == nil || as.CoverageEnabled(taskID)
|
||||
coverageEnabled := coverageOn(as, taskID)
|
||||
tsx.SetCoverageEnabled(coverageEnabled)
|
||||
if as != nil {
|
||||
tsx.SetAssetStore(as, as.Companies())
|
||||
@@ -473,12 +488,9 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
|
||||
// toward coverage only under a declared domain or IP root (db.declaredScopeFilter);
|
||||
// coverage scope, not enforced.
|
||||
// When the asset-coverage feature is off, the test scope (denominator) is no
|
||||
// longer accumulated.
|
||||
if coverageEnabled {
|
||||
for _, a := range assets {
|
||||
_ = as.AddAutoScope(taskID, a.Type, a.Domain, a.URL, a.IP)
|
||||
}
|
||||
}
|
||||
// longer accumulated. A failed flag read is treated as on, so an outage
|
||||
// still writes, including for a task whose stored flag is off.
|
||||
writeIntentAutoScope(as, taskID, assets, coverageEnabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-7
@@ -297,17 +297,22 @@ type Coverage struct {
|
||||
}
|
||||
|
||||
// CoverageEnabled reports whether a task has the asset-coverage feature turned on
|
||||
// (tasks.coverage_enabled). Missing row / error → true (fail open to the default),
|
||||
// so unknown/legacy tasks keep the historical behavior. taskID<=0 → true.
|
||||
func (s *AssetStore) CoverageEnabled(taskID int64) bool {
|
||||
// (tasks.coverage_enabled). A missing row stays enabled, which is the historical
|
||||
// default for unknown and legacy tasks. taskID<=0 stays enabled. A query error is
|
||||
// returned with enabled=true so a database blip does not turn the feature off.
|
||||
func (s *AssetStore) CoverageEnabled(taskID int64) (bool, error) {
|
||||
if taskID <= 0 {
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
var enabled bool
|
||||
if err := s.db.QueryRow(`SELECT COALESCE(coverage_enabled,true) FROM tasks WHERE id=$1`, taskID).Scan(&enabled); err != nil {
|
||||
return true
|
||||
err := s.db.QueryRow(`SELECT COALESCE(coverage_enabled,true) FROM tasks WHERE id=$1`, taskID).Scan(&enabled)
|
||||
if err == sql.ErrNoRows {
|
||||
return true, nil
|
||||
}
|
||||
return enabled
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
return enabled, nil
|
||||
}
|
||||
|
||||
// declaredScopeFilter keeps a task_scope row ts in the coverage match only when it is
|
||||
|
||||
@@ -2,6 +2,39 @@ package db
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCoverageEnabledWithoutTask(t *testing.T) {
|
||||
on, err := (&AssetStore{}).CoverageEnabled(0)
|
||||
if err != nil || !on {
|
||||
t.Fatalf("enabled=%v err=%v", on, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoverageEnabledMissingRowStaysOn(t *testing.T) {
|
||||
d, err := Open(testDSN(t))
|
||||
if err != nil {
|
||||
t.Skipf("postgres unavailable (%v)", err)
|
||||
}
|
||||
defer d.Close()
|
||||
on, err := d.Assets().CoverageEnabled(9_000_000_000_001)
|
||||
if err != nil || !on {
|
||||
t.Fatalf("missing task: enabled=%v err=%v, want enabled with no error", on, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoverageEnabledQueryErrorStaysOn(t *testing.T) {
|
||||
d, err := Open(testDSN(t))
|
||||
if err != nil {
|
||||
t.Skipf("postgres unavailable (%v)", err)
|
||||
}
|
||||
if err := d.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
on, err := d.Assets().CoverageEnabled(1)
|
||||
if err == nil || !on {
|
||||
t.Fatalf("failed query: enabled=%v err=%v, want enabled and the error", on, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStripHostPort covers the ip:port / [ipv6]:port stripping used by the
|
||||
// ip/cidr scope path so a target like "10.0.188.136:3000" no longer 404s.
|
||||
func TestStripHostPort(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user