mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-28 10:41:50 +08:00
Merge pull request #193 from RuoJi6/RuoJi6/fix/issue-186-intercept-polling-count
fix(intercept): avoid full-history polling and full-table counts (#186)
This commit is contained in:
commit
10be95ac44
14 files changed
+676
-159
No files matched your search
+59
-9
@@ -258,7 +258,16 @@ func (s *Server) interceptListPending(w http.ResponseWriter, r *http.Request) {
|
||||
if pg == nil {
|
||||
return
|
||||
}
|
||||
pending, err := pg.ListPendingIntercepts()
|
||||
taskID := strings.TrimSpace(r.URL.Query().Get("task_id"))
|
||||
var (
|
||||
pending []db.InterceptPending
|
||||
err error
|
||||
)
|
||||
if taskID != "" {
|
||||
pending, err = pg.ListPendingInterceptsByTask(taskID)
|
||||
} else {
|
||||
pending, err = pg.ListPendingIntercepts()
|
||||
}
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
@@ -269,6 +278,32 @@ func (s *Server) interceptListPending(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]any{"pending": pending})
|
||||
}
|
||||
|
||||
func (s *Server) interceptPendingSummary(w http.ResponseWriter, r *http.Request) {
|
||||
pg := s.pg(w)
|
||||
if pg == nil {
|
||||
return
|
||||
}
|
||||
taskID := strings.TrimSpace(r.URL.Query().Get("task_id"))
|
||||
if taskID == "" {
|
||||
count, err := pg.CountPendingIntercepts()
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"count": count})
|
||||
return
|
||||
}
|
||||
summary, err := pg.TaskPendingInterceptSummary(taskID)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
if summary.AgentNames == nil {
|
||||
summary.AgentNames = []string{}
|
||||
}
|
||||
writeJSON(w, 200, summary)
|
||||
}
|
||||
|
||||
func (s *Server) interceptGetOne(w http.ResponseWriter, r *http.Request) {
|
||||
pg := s.pg(w)
|
||||
if pg == nil {
|
||||
@@ -308,7 +343,7 @@ func (s *Server) interceptListTaskItems(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
if q.Get("page") == "" && q.Get("size") == "" && filter == (db.InterceptApprovalFilter{}) {
|
||||
items, err := pg.ListTaskIntercepts(taskID)
|
||||
items, err := pg.ListTaskInterceptsLimit(taskID, 200)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
@@ -320,15 +355,12 @@ func (s *Server) interceptListTaskItems(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
page, size := interceptPageParams(q)
|
||||
items, total, err := pg.ListTaskInterceptsPage(taskID, page, size, filter)
|
||||
res, err := pg.ListTaskInterceptsPage(taskID, page, size, filter)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
if items == nil {
|
||||
items = []db.InterceptApprovalRow{}
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"items": items, "total": total, "page": page, "page_size": size})
|
||||
writeInterceptPage(w, res, page, size)
|
||||
}
|
||||
|
||||
func (s *Server) interceptHistory(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -355,15 +387,33 @@ func (s *Server) interceptHistory(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
page, size := interceptPageParams(q)
|
||||
items, total, err := pg.ListAllInterceptsPage(page, size, filter)
|
||||
res, err := pg.ListAllInterceptsPage(page, size, filter)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
writeInterceptPage(w, res, page, size)
|
||||
}
|
||||
|
||||
func writeInterceptPage(w http.ResponseWriter, res db.InterceptApprovalPage, page, size int) {
|
||||
items := res.Items
|
||||
if items == nil {
|
||||
items = []db.InterceptApprovalRow{}
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"items": items, "total": total, "page": page, "page_size": size})
|
||||
payload := map[string]any{
|
||||
"items": items,
|
||||
"total": res.Total,
|
||||
"has_more": res.HasMore,
|
||||
"page": page,
|
||||
"page_size": size,
|
||||
}
|
||||
if res.TotalApproximate {
|
||||
payload["total_approximate"] = true
|
||||
}
|
||||
if res.TotalCapped {
|
||||
payload["total_capped"] = true
|
||||
}
|
||||
writeJSON(w, 200, payload)
|
||||
}
|
||||
|
||||
func interceptFilterParams(q url.Values) (db.InterceptApprovalFilter, error) {
|
||||
|
||||
@@ -63,10 +63,11 @@ func TestInterceptFilterHTTP(t *testing.T) {
|
||||
}
|
||||
scope := "approval-http-filter"
|
||||
t.Cleanup(func() { _, _ = d.Exec(`DELETE FROM intercept_pending WHERE task_id IN ($1,$2)`, scope, scope+"-other") })
|
||||
_, baseline, err := d.ListAllInterceptsPage(1, 20, db.InterceptApprovalFilter{Status: "denied", DecisionSource: "model"})
|
||||
basePage, err := d.ListAllInterceptsPage(1, 20, db.InterceptApprovalFilter{Status: "denied", DecisionSource: "model"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
baseline := basePage.Total
|
||||
for _, state := range []string{"pending", "allowed", "denied", "timeout"} {
|
||||
if _, err := d.CreateDecidedIntercept(0, 0, scope, "test", "Bash", []byte(`{}`), state, "[模型] fixture"); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -143,4 +144,50 @@ func TestInterceptFilterHTTP(t *testing.T) {
|
||||
t.Fatalf("%s: %d %s", tc.query, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Pending summary endpoints must require auth and return lightweight counts
|
||||
// without historical rows.
|
||||
if rec := do("/api/intercept/pending/summary", false); rec.Code != 401 {
|
||||
t.Fatalf("unprotected summary: %d", rec.Code)
|
||||
}
|
||||
rec := do("/api/intercept/pending/summary", true)
|
||||
var globalSummary struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &globalSummary) != nil || globalSummary.Count < 1 {
|
||||
t.Fatalf("global summary: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec = do("/api/intercept/pending/summary?task_id="+scope, true)
|
||||
var taskSummary struct {
|
||||
Count int `json:"count"`
|
||||
AgentNames []string `json:"agent_names"`
|
||||
}
|
||||
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &taskSummary) != nil || taskSummary.Count != 1 || len(taskSummary.AgentNames) != 1 || taskSummary.AgentNames[0] != "test" {
|
||||
t.Fatalf("task summary: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec = do("/api/intercept/pending?task_id="+scope, true)
|
||||
var taskPending struct {
|
||||
Pending []db.InterceptPending `json:"pending"`
|
||||
}
|
||||
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &taskPending) != nil || len(taskPending.Pending) != 1 {
|
||||
t.Fatalf("task pending: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Unpaginated task history must stay bounded so large tasks never dump full history.
|
||||
bigScope := scope + "-cap"
|
||||
t.Cleanup(func() { _, _ = d.Exec(`DELETE FROM intercept_pending WHERE task_id=$1`, bigScope) })
|
||||
if _, err := d.Exec(`
|
||||
INSERT INTO intercept_pending (task_id, agent_name, tool_name, tool_input, status, reason, decision_source)
|
||||
SELECT $1, 'worker', 'Bash', '{}'::jsonb, 'allowed', 'ok', 'rule'
|
||||
FROM generate_series(1, 205)`, bigScope); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec = do("/api/intercept/task/"+bigScope, true)
|
||||
var cappedTask struct {
|
||||
Items []db.InterceptApprovalRow `json:"items"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &cappedTask) != nil || len(cappedTask.Items) != 200 || cappedTask.Total != 200 {
|
||||
t.Fatalf("unpaginated task cap: %d len=%d total=%d", rec.Code, len(cappedTask.Items), cappedTask.Total)
|
||||
}
|
||||
}
|
||||
@@ -1152,6 +1152,7 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("POST /api/asset-intercept/rules/{id}/toggle", s.assetInterceptToggleRule)
|
||||
|
||||
mux.HandleFunc("GET /api/intercept/pending", s.interceptListPending)
|
||||
mux.HandleFunc("GET /api/intercept/pending/summary", s.interceptPendingSummary)
|
||||
mux.HandleFunc("GET /api/intercept/pending/{id}", s.interceptGetOne)
|
||||
mux.HandleFunc("POST /api/intercept/pending/{id}/decide", s.interceptDecide)
|
||||
mux.HandleFunc("GET /api/intercept/history", s.interceptHistory)
|
||||
|
||||
Reference in new issue
Block a user