fix(postgres): tasks, events, steps and run stats stay in their org
This commit is contained in:
14 files changed
+230
-97
No files matched your search
@@ -561,7 +561,7 @@ func (s *EDA) SetDispatchTask(ctx context.Context, bindingID, captureID string,
|
||||
|
||||
// ListDispatches returns the newest ledger rows matching filter.
|
||||
func (s *EDA) ListDispatches(ctx context.Context, filter storecontract.DispatchFilter) ([]storecontract.Dispatch, error) {
|
||||
params := postgresdb.ListBindingDispatchesParams{EntryLimit: int32(min(max(filter.Limit, 1), 500))} // clamped
|
||||
params := postgresdb.ListBindingDispatchesParams{EntryLimit: int32(min(max(filter.Limit, 1), 500)), ScopeOrg: scopeOrg(ctx)} // clamped
|
||||
if filter.BindingID != "" {
|
||||
params.Binding = pgtype.Text{String: filter.BindingID, Valid: true}
|
||||
}
|
||||
@@ -652,7 +652,7 @@ func (s *EDA) InsertToolCall(ctx context.Context, tc ToolCall) error {
|
||||
|
||||
// TaskToolCalls returns one task's tool calls in call order.
|
||||
func (s *EDA) TaskToolCalls(ctx context.Context, taskID int64) ([]ToolCall, error) {
|
||||
rows, err := s.q.TaskToolCalls(ctx, taskID)
|
||||
rows, err := s.q.TaskToolCalls(ctx, postgresdb.TaskToolCallsParams{TaskID: taskID, ScopeOrg: scopeOrg(ctx)})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edastore: list tool calls: %w", err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
@@ -28,6 +29,7 @@ func insertEventQ(ctx context.Context, q *postgresdb.Queries, e events.Event) (i
|
||||
Issue: int64(e.Issue), Workflow: e.Workflow, Stage: e.Stage,
|
||||
Attempt: int64(e.Attempt), ActorID: e.ActorID, ActorKind: e.ActorKind,
|
||||
PrincipalID: e.PrincipalID, Detail: clip(e.Detail, 4000), Data: string(data),
|
||||
OrgID: string(org.OrgFromContext(ctx)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -63,7 +65,7 @@ func (s *Store) EventsSince(ctx context.Context, cursor string, limit int) ([]ev
|
||||
return s.listEventsFromBeginning(ctx, limit)
|
||||
}
|
||||
rows, err := s.queries().ListEventsAfter(ctx, postgresdb.ListEventsAfterParams{
|
||||
At: atTime, ID: id, Limit: int32(limit),
|
||||
At: atTime, ID: id, PageLimit: int32(limit), ScopeOrg: scopeOrg(ctx),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -72,7 +74,7 @@ func (s *Store) EventsSince(ctx context.Context, cursor string, limit int) ([]ev
|
||||
}
|
||||
|
||||
func (s *Store) listEventsFromBeginning(ctx context.Context, limit int) ([]events.Event, error) {
|
||||
rows, err := s.queries().ListEventsFromBeginning(ctx, int32(limit))
|
||||
rows, err := s.queries().ListEventsFromBeginning(ctx, postgresdb.ListEventsFromBeginningParams{PageLimit: int32(limit), ScopeOrg: scopeOrg(ctx)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -89,7 +91,7 @@ func eventsFromRows(rows []postgresdb.Event) []events.Event {
|
||||
|
||||
// TaskEvents returns a task's full timeline, oldest first.
|
||||
func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) {
|
||||
rows, err := s.queries().TaskEventsByID(ctx, taskID)
|
||||
rows, err := s.queries().TaskEventsByID(ctx, postgresdb.TaskEventsByIDParams{TaskID: taskID, ScopeOrg: scopeOrg(ctx)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -98,7 +100,7 @@ func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, e
|
||||
|
||||
// WorkflowStats aggregates outcomes and spend per workflow.
|
||||
func (s *Store) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat, error) {
|
||||
rows, err := s.queries().WorkflowStats(ctx)
|
||||
rows, err := s.queries().WorkflowStats(ctx, scopeOrg(ctx))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -122,7 +124,7 @@ func (s *Store) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat
|
||||
// StageStats aggregates stage_finish events -- where time goes, and which
|
||||
// stages fail.
|
||||
func (s *Store) StageStats(ctx context.Context) ([]storecontract.StageStat, error) {
|
||||
rows, err := s.queries().StageStats(ctx)
|
||||
rows, err := s.queries().StageStats(ctx, scopeOrg(ctx))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -139,7 +141,7 @@ func (s *Store) StageStats(ctx context.Context) ([]storecontract.StageStat, erro
|
||||
// TokensByDay sums task token spend per UTC day, using agent_finish events as
|
||||
// the per-run accounting source and tasks.tokens_used as the durable fallback.
|
||||
func (s *Store) TokensByDay(ctx context.Context, days int) ([]storecontract.DayTokens, error) {
|
||||
rows, err := s.queries().TokensByDay(ctx, int32(days))
|
||||
rows, err := s.queries().TokensByDay(ctx, postgresdb.TokensByDayParams{Days: int32(days), ScopeOrg: scopeOrg(ctx)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -680,29 +680,30 @@ func (q *Queries) InsertSource(ctx context.Context, arg InsertSourceParams) erro
|
||||
}
|
||||
|
||||
const insertToolCall = `-- name: InsertToolCall :exec
|
||||
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at, org_id, workspace_id)
|
||||
SELECT $1, t.id, $2, $3, $4, $5, $6, t.org_id, t.workspace_id
|
||||
FROM tasks t WHERE t.id = $7
|
||||
`
|
||||
|
||||
type InsertToolCallParams struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
Attempt int64
|
||||
Tool string
|
||||
Result string
|
||||
Error string
|
||||
CalledAt time.Time
|
||||
TaskID int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertToolCall(ctx context.Context, arg InsertToolCallParams) error {
|
||||
_, err := q.db.Exec(ctx, insertToolCall,
|
||||
arg.ID,
|
||||
arg.TaskID,
|
||||
arg.Attempt,
|
||||
arg.Tool,
|
||||
arg.Result,
|
||||
arg.Error,
|
||||
arg.CalledAt,
|
||||
arg.TaskID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -1177,9 +1178,14 @@ func (q *Queries) SourceExists(ctx context.Context, path string) (bool, error) {
|
||||
|
||||
const taskToolCalls = `-- name: TaskToolCalls :many
|
||||
SELECT id, task_id, attempt, tool, result, error, called_at
|
||||
FROM tool_calls WHERE task_id = $1 ORDER BY called_at ASC
|
||||
FROM tool_calls WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2) ORDER BY called_at ASC
|
||||
`
|
||||
|
||||
type TaskToolCallsParams struct {
|
||||
TaskID int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
type TaskToolCallsRow struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
@@ -1190,8 +1196,8 @@ type TaskToolCallsRow struct {
|
||||
CalledAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) TaskToolCalls(ctx context.Context, taskID int64) ([]TaskToolCallsRow, error) {
|
||||
rows, err := q.db.Query(ctx, taskToolCalls, taskID)
|
||||
func (q *Queries) TaskToolCalls(ctx context.Context, arg TaskToolCallsParams) ([]TaskToolCallsRow, error) {
|
||||
rows, err := q.db.Query(ctx, taskToolCalls, arg.TaskID, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ package postgresdb
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const insertEvent = `-- name: InsertEvent :one
|
||||
@@ -15,9 +17,11 @@ const insertEvent = `-- name: InsertEvent :one
|
||||
WITH append_lock AS (
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0))
|
||||
)
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
|
||||
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
|
||||
FROM append_lock
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id)
|
||||
SELECT $1, $2, $3, $4, $5, $6, $7,
|
||||
$8, $9, $10, $11, $12, $13,
|
||||
COALESCE(t.org_id, $14::text), COALESCE(t.workspace_id, 'default')
|
||||
FROM append_lock LEFT JOIN tasks t ON t.id = $3
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
@@ -35,6 +39,7 @@ type InsertEventParams struct {
|
||||
PrincipalID string
|
||||
Detail string
|
||||
Data string
|
||||
OrgID string
|
||||
}
|
||||
|
||||
// Event-log queries. The (at, id) cursor is a wire contract: at is the
|
||||
@@ -60,6 +65,7 @@ func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64
|
||||
arg.PrincipalID,
|
||||
arg.Detail,
|
||||
arg.Data,
|
||||
arg.OrgID,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
@@ -69,19 +75,25 @@ func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64
|
||||
const listEventsAfter = `-- name: ListEventsAfter :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE at > $1 OR (at = $1 AND id > $2)
|
||||
WHERE (at > $1 OR (at = $1 AND id > $2)) AND ($3::text IS NULL OR org_id = $3)
|
||||
ORDER BY at, id
|
||||
LIMIT $3
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListEventsAfterParams struct {
|
||||
At time.Time
|
||||
ID int64
|
||||
Limit int32
|
||||
At time.Time
|
||||
ID int64
|
||||
ScopeOrg pgtype.Text
|
||||
PageLimit int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsAfter, arg.At, arg.ID, arg.Limit)
|
||||
rows, err := q.db.Query(ctx, listEventsAfter,
|
||||
arg.At,
|
||||
arg.ID,
|
||||
arg.ScopeOrg,
|
||||
arg.PageLimit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -120,12 +132,18 @@ func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams
|
||||
const listEventsFromBeginning = `-- name: ListEventsFromBeginning :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE ($1::text IS NULL OR org_id = $1)
|
||||
ORDER BY at, id
|
||||
LIMIT $1
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
func (q *Queries) ListEventsFromBeginning(ctx context.Context, limit int32) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsFromBeginning, limit)
|
||||
type ListEventsFromBeginningParams struct {
|
||||
ScopeOrg pgtype.Text
|
||||
PageLimit int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListEventsFromBeginning(ctx context.Context, arg ListEventsFromBeginningParams) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsFromBeginning, arg.ScopeOrg, arg.PageLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -164,10 +182,10 @@ func (q *Queries) ListEventsFromBeginning(ctx context.Context, limit int32) ([]E
|
||||
const stageStats = `-- name: StageStats :many
|
||||
SELECT workflow, stage,
|
||||
COUNT(*)::int AS runs,
|
||||
CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint) AS avg_ms,
|
||||
COALESCE(CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint), 0)::bigint AS avg_ms,
|
||||
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
|
||||
FROM events
|
||||
WHERE kind = 'stage_finish'
|
||||
WHERE kind = 'stage_finish' AND ($1::text IS NULL OR org_id = $1)
|
||||
GROUP BY workflow, stage
|
||||
ORDER BY workflow, stage
|
||||
`
|
||||
@@ -180,8 +198,8 @@ type StageStatsRow struct {
|
||||
Errors int32
|
||||
}
|
||||
|
||||
func (q *Queries) StageStats(ctx context.Context) ([]StageStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, stageStats)
|
||||
func (q *Queries) StageStats(ctx context.Context, scopeOrg pgtype.Text) ([]StageStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, stageStats, scopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -209,12 +227,17 @@ func (q *Queries) StageStats(ctx context.Context) ([]StageStatsRow, error) {
|
||||
const taskEventsByID = `-- name: TaskEventsByID :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE task_id = $1
|
||||
WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2)
|
||||
ORDER BY id
|
||||
`
|
||||
|
||||
func (q *Queries) TaskEventsByID(ctx context.Context, taskID int64) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, taskEventsByID, taskID)
|
||||
type TaskEventsByIDParams struct {
|
||||
TaskID int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) TaskEventsByID(ctx context.Context, arg TaskEventsByIDParams) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, taskEventsByID, arg.TaskID, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -256,14 +279,14 @@ WITH event_totals AS (
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number'
|
||||
THEN (data::jsonb ->> 'tokens')::bigint ELSE 0 END) AS tokens,
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number' THEN 1 ELSE 0 END) AS valid_tokens
|
||||
FROM events WHERE kind = 'agent_finish'
|
||||
FROM events WHERE kind = 'agent_finish' AND ($2::text IS NULL OR org_id = $2)
|
||||
GROUP BY task_id, day
|
||||
), usage AS (
|
||||
SELECT day, tokens FROM event_totals WHERE valid_tokens > 0
|
||||
UNION ALL
|
||||
SELECT to_char(t.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'), t.tokens_used
|
||||
FROM tasks t
|
||||
WHERE t.tokens_used != 0 AND NOT EXISTS (
|
||||
WHERE t.tokens_used != 0 AND ($2::text IS NULL OR t.org_id = $2) AND NOT EXISTS (
|
||||
SELECT 1 FROM event_totals e
|
||||
WHERE e.task_id = t.id AND e.valid_tokens > 0
|
||||
)
|
||||
@@ -272,13 +295,18 @@ SELECT day, SUM(tokens)::bigint AS tokens
|
||||
FROM usage GROUP BY day ORDER BY day DESC LIMIT $1
|
||||
`
|
||||
|
||||
type TokensByDayParams struct {
|
||||
Days int32
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
type TokensByDayRow struct {
|
||||
Day string
|
||||
Tokens int64
|
||||
}
|
||||
|
||||
func (q *Queries) TokensByDay(ctx context.Context, limit int32) ([]TokensByDayRow, error) {
|
||||
rows, err := q.db.Query(ctx, tokensByDay, limit)
|
||||
func (q *Queries) TokensByDay(ctx context.Context, arg TokensByDayParams) ([]TokensByDayRow, error) {
|
||||
rows, err := q.db.Query(ctx, tokensByDay, arg.Days, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -308,7 +336,7 @@ SELECT workflow,
|
||||
AVG(iterations)::float8 AS avg_steps,
|
||||
SUM(tokens_used)::bigint AS total_tokens
|
||||
FROM tasks
|
||||
WHERE workflow <> ''
|
||||
WHERE workflow <> '' AND ($1::text IS NULL OR org_id = $1)
|
||||
GROUP BY workflow
|
||||
ORDER BY COUNT(*) DESC
|
||||
`
|
||||
@@ -325,8 +353,8 @@ type WorkflowStatsRow struct {
|
||||
TotalTokens int64
|
||||
}
|
||||
|
||||
func (q *Queries) WorkflowStats(ctx context.Context) ([]WorkflowStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, workflowStats)
|
||||
func (q *Queries) WorkflowStats(ctx context.Context, scopeOrg pgtype.Text) ([]WorkflowStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, workflowStats, scopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -192,12 +192,14 @@ const listStepExecutions = `-- name: ListStepExecutions :many
|
||||
SELECT id, execution_id, attempt, parent_id, depth, kind, name, status, detail, tokens_used, started_at, finished_at, called_execution_id
|
||||
FROM step_executions
|
||||
WHERE execution_id = $1::bigint AND ($2::bigint = 0 OR attempt = $2::bigint)
|
||||
AND ($3::text IS NULL OR org_id = $3)
|
||||
ORDER BY attempt, id
|
||||
`
|
||||
|
||||
type ListStepExecutionsParams struct {
|
||||
ExecutionID int64
|
||||
Attempt int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
type ListStepExecutionsRow struct {
|
||||
@@ -220,7 +222,7 @@ type ListStepExecutionsRow struct {
|
||||
// (execution_id, attempt, id) makes both this and the single-attempt form a
|
||||
// straight index scan.
|
||||
func (q *Queries) ListStepExecutions(ctx context.Context, arg ListStepExecutionsParams) ([]ListStepExecutionsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listStepExecutions, arg.ExecutionID, arg.Attempt)
|
||||
rows, err := q.db.Query(ctx, listStepExecutions, arg.ExecutionID, arg.Attempt, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -14,13 +14,18 @@ import (
|
||||
|
||||
const activeTasksByOrigin = `-- name: ActiveTasksByOrigin :many
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks
|
||||
WHERE origin = $1 AND origin <> '' AND status IN ('queued', 'running')
|
||||
WHERE origin = $1 AND origin <> '' AND status IN ('queued', 'running') AND ($2::text IS NULL OR org_id = $2)
|
||||
ORDER BY id
|
||||
`
|
||||
|
||||
type ActiveTasksByOriginParams struct {
|
||||
Origin string
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
// The queued and running tasks one conversation created, for its /stop.
|
||||
func (q *Queries) ActiveTasksByOrigin(ctx context.Context, origin string) ([]Task, error) {
|
||||
rows, err := q.db.Query(ctx, activeTasksByOrigin, origin)
|
||||
func (q *Queries) ActiveTasksByOrigin(ctx context.Context, arg ActiveTasksByOriginParams) ([]Task, error) {
|
||||
rows, err := q.db.Query(ctx, activeTasksByOrigin, arg.Origin, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -306,7 +311,7 @@ func (q *Queries) ClearTerminalTasks(ctx context.Context) (int64, error) {
|
||||
}
|
||||
|
||||
const countTasksByStatus = `-- name: CountTasksByStatus :many
|
||||
SELECT status, count(*)::int AS count FROM tasks GROUP BY status
|
||||
SELECT status, count(*)::int AS count FROM tasks WHERE ($1::text IS NULL OR org_id = $1) GROUP BY status
|
||||
`
|
||||
|
||||
type CountTasksByStatusRow struct {
|
||||
@@ -314,8 +319,8 @@ type CountTasksByStatusRow struct {
|
||||
Count int32
|
||||
}
|
||||
|
||||
func (q *Queries) CountTasksByStatus(ctx context.Context) ([]CountTasksByStatusRow, error) {
|
||||
rows, err := q.db.Query(ctx, countTasksByStatus)
|
||||
func (q *Queries) CountTasksByStatus(ctx context.Context, scopeOrg pgtype.Text) ([]CountTasksByStatusRow, error) {
|
||||
rows, err := q.db.Query(ctx, countTasksByStatus, scopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -486,23 +491,24 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
||||
}
|
||||
|
||||
const insertTransition = `-- name: InsertTransition :exec
|
||||
INSERT INTO transitions (task_id, from_status, to_status, detail)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO transitions (task_id, from_status, to_status, detail, org_id, workspace_id)
|
||||
SELECT t.id, $1, $2, $3, t.org_id, t.workspace_id
|
||||
FROM tasks t WHERE t.id = $4
|
||||
`
|
||||
|
||||
type InsertTransitionParams struct {
|
||||
TaskID int64
|
||||
FromStatus string
|
||||
ToStatus string
|
||||
Detail string
|
||||
TaskID int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertTransition(ctx context.Context, arg InsertTransitionParams) error {
|
||||
_, err := q.db.Exec(ctx, insertTransition,
|
||||
arg.TaskID,
|
||||
arg.FromStatus,
|
||||
arg.ToStatus,
|
||||
arg.Detail,
|
||||
arg.TaskID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -566,14 +572,16 @@ SELECT id, owner, repo, issue_number, title, status, workflow,
|
||||
created_at, updated_at, plan, source, identity, binding_id, binding_version,
|
||||
outputs, review_gate, rereview_rounds
|
||||
FROM tasks
|
||||
WHERE (cardinality($1::text[]) = 0 OR status = ANY($1::text[]))
|
||||
AND ($2::timestamptz IS NULL
|
||||
OR (updated_at, id) < ($2::timestamptz, $3::bigint))
|
||||
WHERE ($1::text IS NULL OR org_id = $1)
|
||||
AND (cardinality($2::text[]) = 0 OR status = ANY($2::text[]))
|
||||
AND ($3::timestamptz IS NULL
|
||||
OR (updated_at, id) < ($3::timestamptz, $4::bigint))
|
||||
ORDER BY updated_at DESC, id DESC
|
||||
LIMIT $4
|
||||
LIMIT $5
|
||||
`
|
||||
|
||||
type ListTaskSummariesParams struct {
|
||||
ScopeOrg pgtype.Text
|
||||
Statuses []string
|
||||
BeforeUpdated pgtype.Timestamptz
|
||||
BeforeID int64
|
||||
@@ -617,6 +625,7 @@ type ListTaskSummariesRow struct {
|
||||
// answers"), and the round count is the cap's visible half.
|
||||
func (q *Queries) ListTaskSummaries(ctx context.Context, arg ListTaskSummariesParams) ([]ListTaskSummariesRow, error) {
|
||||
rows, err := q.db.Query(ctx, listTaskSummaries,
|
||||
arg.ScopeOrg,
|
||||
arg.Statuses,
|
||||
arg.BeforeUpdated,
|
||||
arg.BeforeID,
|
||||
@@ -919,11 +928,16 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
|
||||
}
|
||||
|
||||
const taskByID = `-- name: TaskByID :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks WHERE id = $1
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
||||
`
|
||||
|
||||
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, taskByID, id)
|
||||
type TaskByIDParams struct {
|
||||
ID int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) TaskByID(ctx context.Context, arg TaskByIDParams) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, taskByID, arg.ID, arg.ScopeOrg)
|
||||
var i Task
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
|
||||
@@ -128,12 +128,13 @@ VALUES ($1, $2, $3, $4);
|
||||
DELETE FROM playbook_dispatches WHERE playbook_id = $1;
|
||||
|
||||
-- name: InsertToolCall :exec
|
||||
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7);
|
||||
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at, org_id, workspace_id)
|
||||
SELECT sqlc.arg(id), t.id, sqlc.arg(attempt), sqlc.arg(tool), sqlc.arg(result), sqlc.arg(error), sqlc.arg(called_at), t.org_id, t.workspace_id
|
||||
FROM tasks t WHERE t.id = sqlc.arg(task_id);
|
||||
|
||||
-- name: TaskToolCalls :many
|
||||
SELECT id, task_id, attempt, tool, result, error, called_at
|
||||
FROM tool_calls WHERE task_id = $1 ORDER BY called_at ASC;
|
||||
FROM tool_calls WHERE task_id = sqlc.arg(task_id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY called_at ASC;
|
||||
|
||||
-- name: InsertEventType :exec
|
||||
INSERT INTO event_types (id, source, name, rule, schema, org_id)
|
||||
|
||||
@@ -11,28 +11,31 @@
|
||||
WITH append_lock AS (
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0))
|
||||
)
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
|
||||
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
|
||||
FROM append_lock
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id)
|
||||
SELECT sqlc.arg(at), sqlc.arg(kind), sqlc.arg(task_id), sqlc.arg(repo), sqlc.arg(issue), sqlc.arg(workflow), sqlc.arg(stage),
|
||||
sqlc.arg(attempt), sqlc.arg(actor_id), sqlc.arg(actor_kind), sqlc.arg(principal_id), sqlc.arg(detail), sqlc.arg(data),
|
||||
COALESCE(t.org_id, sqlc.arg(org_id)::text), COALESCE(t.workspace_id, 'default')
|
||||
FROM append_lock LEFT JOIN tasks t ON t.id = sqlc.arg(task_id)
|
||||
RETURNING id;
|
||||
|
||||
-- name: ListEventsFromBeginning :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
ORDER BY at, id
|
||||
LIMIT $1;
|
||||
LIMIT sqlc.arg(page_limit);
|
||||
|
||||
-- name: ListEventsAfter :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE at > $1 OR (at = $1 AND id > $2)
|
||||
WHERE (at > sqlc.arg(at) OR (at = sqlc.arg(at) AND id > sqlc.arg(id))) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
ORDER BY at, id
|
||||
LIMIT $3;
|
||||
LIMIT sqlc.arg(page_limit);
|
||||
|
||||
-- name: TaskEventsByID :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE task_id = $1
|
||||
WHERE task_id = sqlc.arg(task_id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
ORDER BY id;
|
||||
|
||||
-- name: WorkflowStats :many
|
||||
@@ -46,17 +49,17 @@ SELECT workflow,
|
||||
AVG(iterations)::float8 AS avg_steps,
|
||||
SUM(tokens_used)::bigint AS total_tokens
|
||||
FROM tasks
|
||||
WHERE workflow <> ''
|
||||
WHERE workflow <> '' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
GROUP BY workflow
|
||||
ORDER BY COUNT(*) DESC;
|
||||
|
||||
-- name: StageStats :many
|
||||
SELECT workflow, stage,
|
||||
COUNT(*)::int AS runs,
|
||||
CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint) AS avg_ms,
|
||||
COALESCE(CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint), 0)::bigint AS avg_ms,
|
||||
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
|
||||
FROM events
|
||||
WHERE kind = 'stage_finish'
|
||||
WHERE kind = 'stage_finish' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
GROUP BY workflow, stage
|
||||
ORDER BY workflow, stage;
|
||||
|
||||
@@ -66,17 +69,17 @@ WITH event_totals AS (
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number'
|
||||
THEN (data::jsonb ->> 'tokens')::bigint ELSE 0 END) AS tokens,
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number' THEN 1 ELSE 0 END) AS valid_tokens
|
||||
FROM events WHERE kind = 'agent_finish'
|
||||
FROM events WHERE kind = 'agent_finish' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
GROUP BY task_id, day
|
||||
), usage AS (
|
||||
SELECT day, tokens FROM event_totals WHERE valid_tokens > 0
|
||||
UNION ALL
|
||||
SELECT to_char(t.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'), t.tokens_used
|
||||
FROM tasks t
|
||||
WHERE t.tokens_used != 0 AND NOT EXISTS (
|
||||
WHERE t.tokens_used != 0 AND (sqlc.narg(scope_org)::text IS NULL OR t.org_id = sqlc.narg(scope_org)) AND NOT EXISTS (
|
||||
SELECT 1 FROM event_totals e
|
||||
WHERE e.task_id = t.id AND e.valid_tokens > 0
|
||||
)
|
||||
)
|
||||
SELECT day, SUM(tokens)::bigint AS tokens
|
||||
FROM usage GROUP BY day ORDER BY day DESC LIMIT $1;
|
||||
FROM usage GROUP BY day ORDER BY day DESC LIMIT sqlc.arg(days);
|
||||
@@ -77,6 +77,7 @@ RETURNING id, name;
|
||||
SELECT id, execution_id, attempt, parent_id, depth, kind, name, status, detail, tokens_used, started_at, finished_at, called_execution_id
|
||||
FROM step_executions
|
||||
WHERE execution_id = @execution_id::bigint AND (@attempt::bigint = 0 OR attempt = @attempt::bigint)
|
||||
AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
ORDER BY attempt, id;
|
||||
-- name: WaitingCallSteps :many
|
||||
-- The call steps of the execution's current attempt that are still open and
|
||||
|
||||
@@ -17,7 +17,7 @@ WHERE id = (
|
||||
RETURNING *;
|
||||
|
||||
-- name: TaskByID :one
|
||||
SELECT * FROM tasks WHERE id = $1;
|
||||
SELECT * FROM tasks WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
||||
|
||||
-- name: TaskByIssue :one
|
||||
SELECT * FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3;
|
||||
@@ -43,14 +43,15 @@ SELECT id, owner, repo, issue_number, title, status, workflow,
|
||||
created_at, updated_at, plan, source, identity, binding_id, binding_version,
|
||||
outputs, review_gate, rereview_rounds
|
||||
FROM tasks
|
||||
WHERE (cardinality(@statuses::text[]) = 0 OR status = ANY(@statuses::text[]))
|
||||
WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
AND (cardinality(@statuses::text[]) = 0 OR status = ANY(@statuses::text[]))
|
||||
AND (sqlc.narg(before_updated)::timestamptz IS NULL
|
||||
OR (updated_at, id) < (sqlc.narg(before_updated)::timestamptz, @before_id::bigint))
|
||||
ORDER BY updated_at DESC, id DESC
|
||||
LIMIT @page_limit;
|
||||
|
||||
-- name: CountTasksByStatus :many
|
||||
SELECT status, count(*)::int AS count FROM tasks GROUP BY status;
|
||||
SELECT status, count(*)::int AS count FROM tasks WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) GROUP BY status;
|
||||
|
||||
-- name: InsertChatTask :one
|
||||
-- The synthetic issue number keeps chat-sourced tasks off the forge's real
|
||||
@@ -84,7 +85,7 @@ RETURNING *;
|
||||
-- name: ActiveTasksByOrigin :many
|
||||
-- The queued and running tasks one conversation created, for its /stop.
|
||||
SELECT * FROM tasks
|
||||
WHERE origin = sqlc.arg(origin) AND origin <> '' AND status IN ('queued', 'running')
|
||||
WHERE origin = sqlc.arg(origin) AND origin <> '' AND status IN ('queued', 'running') AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
|
||||
ORDER BY id;
|
||||
|
||||
-- name: StampTaskBinding :exec
|
||||
@@ -123,8 +124,9 @@ SET status = $2,
|
||||
WHERE id = $1 AND status = $5;
|
||||
|
||||
-- name: InsertTransition :exec
|
||||
INSERT INTO transitions (task_id, from_status, to_status, detail)
|
||||
VALUES ($1, $2, $3, $4);
|
||||
INSERT INTO transitions (task_id, from_status, to_status, detail, org_id, workspace_id)
|
||||
SELECT t.id, sqlc.arg(from_status), sqlc.arg(to_status), sqlc.arg(detail), t.org_id, t.workspace_id
|
||||
FROM tasks t WHERE t.id = sqlc.arg(task_id);
|
||||
|
||||
-- name: ParkTask :execrows
|
||||
UPDATE tasks
|
||||
|
||||
@@ -175,7 +175,7 @@ func (s *Store) FinishStep(ctx context.Context, finish task.StepFinish) (events.
|
||||
return events.Event{}, fmt.Errorf("%w: %s -> %s is not a step transition",
|
||||
storecontract.ErrIllegalTransition, finish.From, finish.To)
|
||||
}
|
||||
execution, err := q.TaskByID(ctx, step.ExecutionID)
|
||||
execution, err := q.TaskByID(ctx, postgresdb.TaskByIDParams{ID: step.ExecutionID, ScopeOrg: scopeOrg(ctx)})
|
||||
if err != nil {
|
||||
return events.Event{}, err
|
||||
}
|
||||
@@ -245,6 +245,7 @@ func stepEventData(to taskstate.StepStatus, durationMS int64, detail string) map
|
||||
// 0.
|
||||
func (s *Store) ListSteps(ctx context.Context, executionID int64, attempt int) ([]task.StepExecution, error) {
|
||||
rows, err := s.queries().ListStepExecutions(ctx, postgresdb.ListStepExecutionsParams{
|
||||
ScopeOrg: scopeOrg(ctx),
|
||||
ExecutionID: executionID, Attempt: int64(attempt),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -700,6 +700,7 @@ func (s *Store) Tasks(ctx context.Context, limit int) ([]workflow.Task, error) {
|
||||
// TasksPage returns one page of tasks, most recently updated first.
|
||||
func (s *Store) TasksPage(ctx context.Context, page storecontract.TaskPage) ([]workflow.Task, error) {
|
||||
params := postgresdb.ListTaskSummariesParams{
|
||||
ScopeOrg: scopeOrg(ctx),
|
||||
Statuses: append([]string{}, page.Statuses...),
|
||||
PageLimit: int32(min(max(page.Limit, 1), 500)), // clamped
|
||||
}
|
||||
@@ -730,7 +731,7 @@ func (s *Store) TasksPage(ctx context.Context, page storecontract.TaskPage) ([]w
|
||||
|
||||
// StatusCounts returns task counts by status.
|
||||
func (s *Store) StatusCounts(ctx context.Context) (map[string]int, error) {
|
||||
rows, err := s.queries().CountTasksByStatus(ctx)
|
||||
rows, err := s.queries().CountTasksByStatus(ctx, scopeOrg(ctx))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -771,7 +772,7 @@ func (s *Store) OpenTaskByPR(ctx context.Context, owner, repo string, number int
|
||||
|
||||
// TaskByID returns the task with the given database ID, or nil.
|
||||
func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, error) {
|
||||
t, err := s.queries().TaskByID(ctx, taskID)
|
||||
t, err := s.queries().TaskByID(ctx, postgresdb.TaskByIDParams{ID: taskID, ScopeOrg: scopeOrg(ctx)})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -815,7 +816,7 @@ func (s *Store) StartCall(ctx context.Context, callerTaskID int64, callKey, wf s
|
||||
|
||||
// callRefusal explains why EnqueueCallTask inserted nothing.
|
||||
func (s *Store) callRefusal(ctx context.Context, callerTaskID int64) error {
|
||||
caller, err := s.queries().TaskByID(ctx, callerTaskID)
|
||||
caller, err := s.queries().TaskByID(ctx, postgresdb.TaskByIDParams{ID: callerTaskID, ScopeOrg: scopeOrg(ctx)})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("store: workflow.call caller task %d does not exist: %w", callerTaskID, storecontract.ErrCallNotYours)
|
||||
}
|
||||
@@ -833,7 +834,7 @@ func (s *Store) callRefusal(ctx context.Context, callerTaskID int64) error {
|
||||
// check is the row check the wire grant cannot do: a caller reads only the
|
||||
// tasks it started.
|
||||
func (s *Store) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, map[string]any, error) {
|
||||
callee, err := s.queries().TaskByID(ctx, callTaskID)
|
||||
callee, err := s.queries().TaskByID(ctx, postgresdb.TaskByIDParams{ID: callTaskID, ScopeOrg: scopeOrg(ctx)})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", "", nil, storecontract.ErrCallNotYours
|
||||
}
|
||||
@@ -863,7 +864,7 @@ func (s *Store) CallStatus(ctx context.Context, callerTaskID, callTaskID int64)
|
||||
// ActiveTasksByOrigin returns the queued and running tasks the conversation
|
||||
// origin created.
|
||||
func (s *Store) ActiveTasksByOrigin(ctx context.Context, origin string) ([]*workflow.Task, error) {
|
||||
rows, err := s.queries().ActiveTasksByOrigin(ctx, origin)
|
||||
rows, err := s.queries().ActiveTasksByOrigin(ctx, postgresdb.ActiveTasksByOriginParams{Origin: origin, ScopeOrg: scopeOrg(ctx)})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: active tasks by origin: %w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
)
|
||||
|
||||
// An org's tasks, their events and the stats built from them are invisible to
|
||||
// a request scoped to another org. An event a task emits belongs to the
|
||||
// task's org whatever org the writer acts in.
|
||||
func TestTasksStayInTheirOrg(t *testing.T) {
|
||||
scoped := func(id org.OrgID) context.Context {
|
||||
return org.WithScope(org.WithOrg(t.Context(), id), id)
|
||||
}
|
||||
db := pgstore.Open(t)
|
||||
var taskID int64
|
||||
if err := db.Pool.QueryRow(t.Context(),
|
||||
`INSERT INTO tasks (owner, repo, issue_number, identity, workflow, status, tokens_used, org_id, workspace_id)
|
||||
VALUES ('o', 'r', 1, 'sam', 'deploy', 'running', 10, 'acme', 'default') RETURNING id`,
|
||||
).Scan(&taskID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.InsertEvent(t.Context(), events.Event{Kind: "stage_finish", TaskID: taskID, Workflow: "deploy", Stage: "plan"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
ctx func() context.Context //nolint:containedctx // table input, not stored state
|
||||
sees bool
|
||||
}{
|
||||
{"the owning org", func() context.Context { return scoped("acme") }, true},
|
||||
{"another org", func() context.Context { return scoped("other") }, false},
|
||||
{"an internal service", t.Context, true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := tt.ctx()
|
||||
listed, err := db.Tasks(ctx, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := db.TaskByID(ctx, taskID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
counts, err := db.StatusCounts(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
timeline, err := db.TaskEvents(ctx, taskID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
feed, err := db.EventsSince(ctx, "", 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
workflows, err := db.WorkflowStats(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stages, err := db.StageStats(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := []bool{len(listed) == 1, got != nil, counts["running"] == 1, len(timeline) == 1, len(feed) == 1, len(workflows) == 1, len(stages) == 1}
|
||||
for i, s := range seen {
|
||||
if s != tt.sees {
|
||||
t.Fatalf("read %d visible = %v, want %v", i, s, tt.sees)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+4
-10
@@ -245,19 +245,13 @@ func (s *sseStream) drain(ctx context.Context, conn <-chan liveUpdate, stale <-c
|
||||
}
|
||||
}
|
||||
|
||||
// relayTask sends a broadcast event by reading the backlog up to it, never
|
||||
// the broadcast itself: the pump broadcasts every org's events, and the
|
||||
// backlog read is confined to the viewer's org.
|
||||
func (s *sseStream) relayTask(ctx context.Context, e events.Event) bool {
|
||||
cursor := storecontract.EventCursor(e.At, e.ID)
|
||||
if cursor <= s.since {
|
||||
return true
|
||||
}
|
||||
if !s.catchUp(ctx, cursor) {
|
||||
return false
|
||||
}
|
||||
if cursor > s.since {
|
||||
if sseVisible(e) && !s.send(e) {
|
||||
return false
|
||||
}
|
||||
s.since = cursor
|
||||
}
|
||||
return true
|
||||
return s.catchUp(ctx, cursor)
|
||||
}
|
||||
Reference in new issue
Block a user