Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.6 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: workflow_handoff.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const getWorkflowSquadHandoffTask = `-- name: GetWorkflowSquadHandoffTask :one
SELECT id FROM agent_task_queue
WHERE issue_id = $1
  AND agent_id = $2
  AND squad_id = $3
  AND is_leader_task
  AND context->>'workflow_handoff_revision' = $4::bigint::text
ORDER BY created_at DESC, id DESC
LIMIT 1
`

type GetWorkflowSquadHandoffTaskParams struct {
	IssueID       pgtype.UUID `json:"issue_id"`
	AgentID       pgtype.UUID `json:"agent_id"`
	SquadID       pgtype.UUID `json:"squad_id"`
	IssueRevision int64       `json:"issue_revision"`
}

// A replay of the same status write is already covered, including after claim
// or completion. A later visit to the same stage has a different issue revision.
func (q *Queries) GetWorkflowSquadHandoffTask(ctx context.Context, arg GetWorkflowSquadHandoffTaskParams) (pgtype.UUID, error) {
	row := q.db.QueryRow(ctx, getWorkflowSquadHandoffTask,
		arg.IssueID,
		arg.AgentID,
		arg.SquadID,
		arg.IssueRevision,
	)
	var id pgtype.UUID
	err := row.Scan(&id)
	return id, err
}

const listAssigneeRunsForHandoff = `-- name: ListAssigneeRunsForHandoff :many
SELECT id, agent_id, status, started_at, created_at
FROM agent_task_queue
WHERE issue_id = $1
  AND status IN ('queued', 'dispatched', 'running', 'waiting_local_directory', 'deferred')
  AND (
    ($2::text = 'agent' AND agent_id = $3)
    OR ($2::text = 'squad' AND squad_id = $3)
  )
ORDER BY created_at DESC, id DESC
`

type ListAssigneeRunsForHandoffParams struct {
	IssueID      pgtype.UUID `json:"issue_id"`
	AssigneeType string      `json:"assignee_type"`
	AssigneeID   pgtype.UUID `json:"assignee_id"`
}

type ListAssigneeRunsForHandoffRow struct {
	ID        pgtype.UUID        `json:"id"`
	AgentID   pgtype.UUID        `json:"agent_id"`
	Status    string             `json:"status"`
	StartedAt pgtype.Timestamptz `json:"started_at"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
}

// Preview and explicit stop must cover every unfinished run of the previous
// assignee, including stage handoffs still waiting for a pending queue slot.
func (q *Queries) ListAssigneeRunsForHandoff(ctx context.Context, arg ListAssigneeRunsForHandoffParams) ([]ListAssigneeRunsForHandoffRow, error) {
	rows, err := q.db.Query(ctx, listAssigneeRunsForHandoff, arg.IssueID, arg.AssigneeType, arg.AssigneeID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListAssigneeRunsForHandoffRow{}
	for rows.Next() {
		var i ListAssigneeRunsForHandoffRow
		if err := rows.Scan(
			&i.ID,
			&i.AgentID,
			&i.Status,
			&i.StartedAt,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const lockWorkflowSquadHandoff = `-- name: LockWorkflowSquadHandoff :one
SELECT now()::timestamptz AS locked_at
FROM (SELECT pg_advisory_xact_lock(hashtextextended($1::uuid::text || ':workflow_squad_handoff', 0))) AS handoff_lock
`

// Serialize admission for one issue without taking its owner-row locks out of
// the order lock_task_owner_rows uses to fence workspace teardown.
// Returns the database clock: the handoff row's fire_at must be due by the same
// clock the promoter compares it against (fire_at <= now()).
func (q *Queries) LockWorkflowSquadHandoff(ctx context.Context, issueID pgtype.UUID) (pgtype.Timestamptz, error) {
	row := q.db.QueryRow(ctx, lockWorkflowSquadHandoff, issueID)
	var locked_at pgtype.Timestamptz
	err := row.Scan(&locked_at)
	return locked_at, err
}