Skip to content

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

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

package db

import (
	"context"

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

const createTaskToken = `-- name: CreateTaskToken :one
INSERT INTO task_token (token_hash, task_id, agent_id, workspace_id, user_id, expires_at, id)
VALUES ($1, $2, $3, $4, $5, $6, COALESCE($7::uuid, gen_random_uuid()))
RETURNING id, token_hash, task_id, agent_id, workspace_id, user_id, expires_at, created_at
`

type CreateTaskTokenParams struct {
	TokenHash   string             `json:"token_hash"`
	TaskID      pgtype.UUID        `json:"task_id"`
	AgentID     pgtype.UUID        `json:"agent_id"`
	WorkspaceID pgtype.UUID        `json:"workspace_id"`
	UserID      pgtype.UUID        `json:"user_id"`
	ExpiresAt   pgtype.Timestamptz `json:"expires_at"`
	ID          pgtype.UUID        `json:"id"`
}

func (q *Queries) CreateTaskToken(ctx context.Context, arg CreateTaskTokenParams) (TaskToken, error) {
	row := q.db.QueryRow(ctx, createTaskToken,
		arg.TokenHash,
		arg.TaskID,
		arg.AgentID,
		arg.WorkspaceID,
		arg.UserID,
		arg.ExpiresAt,
		arg.ID,
	)
	var i TaskToken
	err := row.Scan(
		&i.ID,
		&i.TokenHash,
		&i.TaskID,
		&i.AgentID,
		&i.WorkspaceID,
		&i.UserID,
		&i.ExpiresAt,
		&i.CreatedAt,
	)
	return i, err
}

const deleteExpiredTaskTokens = `-- name: DeleteExpiredTaskTokens :exec
DELETE FROM task_token WHERE expires_at <= now()
`

func (q *Queries) DeleteExpiredTaskTokens(ctx context.Context) error {
	_, err := q.db.Exec(ctx, deleteExpiredTaskTokens)
	return err
}

const deleteTaskTokensByTask = `-- name: DeleteTaskTokensByTask :exec
DELETE FROM task_token WHERE task_id = $1
`

func (q *Queries) DeleteTaskTokensByTask(ctx context.Context, taskID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteTaskTokensByTask, taskID)
	return err
}

const getTaskTokenByHash = `-- name: GetTaskTokenByHash :one
SELECT tt.id, tt.token_hash, tt.task_id, tt.agent_id, tt.workspace_id, tt.user_id, tt.expires_at, tt.created_at FROM task_token tt
JOIN agent_task_queue q ON q.id = tt.task_id
WHERE tt.token_hash = $1
  AND tt.expires_at > now()
  AND q.status NOT IN ('completed', 'failed', 'cancelled')
`

// A token authenticates only while its task is live. Terminal task rows never
// return to a live status (retries are new rows), so a token whose task is
// completed, failed or cancelled is dead even when no path deleted it: the
// bulk failure sweeps (runtime offline, stale tasks, daemon recovery) never
// delete it, and the per-task deletes are best-effort (GitHub #11378).
func (q *Queries) GetTaskTokenByHash(ctx context.Context, tokenHash string) (TaskToken, error) {
	row := q.db.QueryRow(ctx, getTaskTokenByHash, tokenHash)
	var i TaskToken
	err := row.Scan(
		&i.ID,
		&i.TokenHash,
		&i.TaskID,
		&i.AgentID,
		&i.WorkspaceID,
		&i.UserID,
		&i.ExpiresAt,
		&i.CreatedAt,
	)
	return i, err
}