You've already forked agentic-coding-workflow
968 lines
28 KiB
Go
968 lines
28 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: sessions.sql
|
|
|
|
package acpsqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const acquireMainWorktree = `-- name: AcquireMainWorktree :execrows
|
|
UPDATE workspaces SET active_main_session_id = $1
|
|
WHERE id = $2 AND active_main_session_id IS NULL
|
|
`
|
|
|
|
type AcquireMainWorktreeParams struct {
|
|
ActiveMainSessionID pgtype.UUID `json:"active_main_session_id"`
|
|
ID pgtype.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) AcquireMainWorktree(ctx context.Context, arg AcquireMainWorktreeParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, acquireMainWorktree, arg.ActiveMainSessionID, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const addSessionUsageTotals = `-- name: AddSessionUsageTotals :one
|
|
UPDATE acp_sessions
|
|
SET prompt_tokens = prompt_tokens + $2,
|
|
completion_tokens = completion_tokens + $3,
|
|
thinking_tokens = thinking_tokens + $4,
|
|
total_cost_usd = total_cost_usd + $5,
|
|
cost_usd = cost_usd + $5,
|
|
tokens_total = tokens_total + $2 + $3 + $4,
|
|
last_activity_at = now()
|
|
WHERE id = $1
|
|
RETURNING prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd
|
|
`
|
|
|
|
type AddSessionUsageTotalsParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
PromptTokens int64 `json:"prompt_tokens"`
|
|
CompletionTokens int64 `json:"completion_tokens"`
|
|
ThinkingTokens int64 `json:"thinking_tokens"`
|
|
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
|
}
|
|
|
|
type AddSessionUsageTotalsRow struct {
|
|
PromptTokens int64 `json:"prompt_tokens"`
|
|
CompletionTokens int64 `json:"completion_tokens"`
|
|
ThinkingTokens int64 `json:"thinking_tokens"`
|
|
TotalCostUsd pgtype.Numeric `json:"total_cost_usd"`
|
|
}
|
|
|
|
// 单 round-trip 累加 session 运行时 token/cost 总量并刷新 last_activity_at,
|
|
// 返回累加后的权威总量供预算判定。
|
|
// 同时把 dashboard rollup 列(cost_usd / tokens_total)与运行时累加器对齐,
|
|
// 使 run-history dashboard 在会话进行中及退出后都能读到 agent 上报的成本。
|
|
func (q *Queries) AddSessionUsageTotals(ctx context.Context, arg AddSessionUsageTotalsParams) (AddSessionUsageTotalsRow, error) {
|
|
row := q.db.QueryRow(ctx, addSessionUsageTotals,
|
|
arg.ID,
|
|
arg.PromptTokens,
|
|
arg.CompletionTokens,
|
|
arg.ThinkingTokens,
|
|
arg.TotalCostUsd,
|
|
)
|
|
var i AddSessionUsageTotalsRow
|
|
err := row.Scan(
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.TotalCostUsd,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const countActiveSessions = `-- name: CountActiveSessions :one
|
|
SELECT COUNT(*) FROM acp_sessions WHERE status IN ('starting', 'running')
|
|
`
|
|
|
|
func (q *Queries) CountActiveSessions(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countActiveSessions)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const countActiveSessionsByUser = `-- name: CountActiveSessionsByUser :one
|
|
SELECT COUNT(*) FROM acp_sessions
|
|
WHERE user_id = $1 AND status IN ('starting', 'running')
|
|
`
|
|
|
|
func (q *Queries) CountActiveSessionsByUser(ctx context.Context, userID pgtype.UUID) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countActiveSessionsByUser, userID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const getCrashedSessionForResume = `-- name: GetCrashedSessionForResume :one
|
|
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
|
issue_id, requirement_id, agent_session_id,
|
|
branch, cwd_path, is_main_worktree, status,
|
|
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
|
orchestrator_step_id,
|
|
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
|
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
|
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
|
cost_usd, tokens_total
|
|
FROM acp_sessions
|
|
WHERE orchestrator_step_id = $1
|
|
AND status IN ('crashed','exited')
|
|
ORDER BY started_at DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
// 返回某 step 最近一次处于可恢复终态(crashed/exited)的 session。
|
|
func (q *Queries) GetCrashedSessionForResume(ctx context.Context, orchestratorStepID pgtype.UUID) (AcpSession, error) {
|
|
row := q.db.QueryRow(ctx, getCrashedSessionForResume, orchestratorStepID)
|
|
var i AcpSession
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.ProjectID,
|
|
&i.AgentKindID,
|
|
&i.UserID,
|
|
&i.IssueID,
|
|
&i.RequirementID,
|
|
&i.AgentSessionID,
|
|
&i.Branch,
|
|
&i.CwdPath,
|
|
&i.IsMainWorktree,
|
|
&i.Status,
|
|
&i.Pid,
|
|
&i.ExitCode,
|
|
&i.LastError,
|
|
&i.StartedAt,
|
|
&i.EndedAt,
|
|
&i.LastStopReason,
|
|
&i.OrchestratorStepID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.TotalCostUsd,
|
|
&i.LastActivityAt,
|
|
&i.BudgetMaxCostUsd,
|
|
&i.BudgetMaxTokens,
|
|
&i.BudgetMaxWallClockSeconds,
|
|
&i.TerminatedReason,
|
|
&i.SandboxMode,
|
|
&i.CostUsd,
|
|
&i.TokensTotal,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getSessionByID = `-- name: GetSessionByID :one
|
|
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
|
issue_id, requirement_id, agent_session_id,
|
|
branch, cwd_path, is_main_worktree, status,
|
|
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
|
orchestrator_step_id,
|
|
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
|
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
|
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
|
cost_usd, tokens_total
|
|
FROM acp_sessions
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetSessionByID(ctx context.Context, id pgtype.UUID) (AcpSession, error) {
|
|
row := q.db.QueryRow(ctx, getSessionByID, id)
|
|
var i AcpSession
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.ProjectID,
|
|
&i.AgentKindID,
|
|
&i.UserID,
|
|
&i.IssueID,
|
|
&i.RequirementID,
|
|
&i.AgentSessionID,
|
|
&i.Branch,
|
|
&i.CwdPath,
|
|
&i.IsMainWorktree,
|
|
&i.Status,
|
|
&i.Pid,
|
|
&i.ExitCode,
|
|
&i.LastError,
|
|
&i.StartedAt,
|
|
&i.EndedAt,
|
|
&i.LastStopReason,
|
|
&i.OrchestratorStepID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.TotalCostUsd,
|
|
&i.LastActivityAt,
|
|
&i.BudgetMaxCostUsd,
|
|
&i.BudgetMaxTokens,
|
|
&i.BudgetMaxWallClockSeconds,
|
|
&i.TerminatedReason,
|
|
&i.SandboxMode,
|
|
&i.CostUsd,
|
|
&i.TokensTotal,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getSessionByStepID = `-- name: GetSessionByStepID :one
|
|
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
|
issue_id, requirement_id, agent_session_id,
|
|
branch, cwd_path, is_main_worktree, status,
|
|
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
|
orchestrator_step_id,
|
|
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
|
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
|
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
|
cost_usd, tokens_total
|
|
FROM acp_sessions
|
|
WHERE orchestrator_step_id = $1
|
|
ORDER BY started_at DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetSessionByStepID(ctx context.Context, orchestratorStepID pgtype.UUID) (AcpSession, error) {
|
|
row := q.db.QueryRow(ctx, getSessionByStepID, orchestratorStepID)
|
|
var i AcpSession
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.ProjectID,
|
|
&i.AgentKindID,
|
|
&i.UserID,
|
|
&i.IssueID,
|
|
&i.RequirementID,
|
|
&i.AgentSessionID,
|
|
&i.Branch,
|
|
&i.CwdPath,
|
|
&i.IsMainWorktree,
|
|
&i.Status,
|
|
&i.Pid,
|
|
&i.ExitCode,
|
|
&i.LastError,
|
|
&i.StartedAt,
|
|
&i.EndedAt,
|
|
&i.LastStopReason,
|
|
&i.OrchestratorStepID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.TotalCostUsd,
|
|
&i.LastActivityAt,
|
|
&i.BudgetMaxCostUsd,
|
|
&i.BudgetMaxTokens,
|
|
&i.BudgetMaxWallClockSeconds,
|
|
&i.TerminatedReason,
|
|
&i.SandboxMode,
|
|
&i.CostUsd,
|
|
&i.TokensTotal,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertSession = `-- name: InsertSession :one
|
|
INSERT INTO acp_sessions (
|
|
id, workspace_id, project_id, agent_kind_id, user_id,
|
|
issue_id, requirement_id, branch, cwd_path, is_main_worktree, status,
|
|
orchestrator_step_id,
|
|
budget_max_cost_usd, budget_max_tokens, budget_max_wall_clock_seconds
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
|
RETURNING id, workspace_id, project_id, agent_kind_id, user_id,
|
|
issue_id, requirement_id, agent_session_id,
|
|
branch, cwd_path, is_main_worktree, status,
|
|
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
|
orchestrator_step_id,
|
|
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
|
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
|
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
|
cost_usd, tokens_total
|
|
`
|
|
|
|
type InsertSessionParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
IssueID pgtype.UUID `json:"issue_id"`
|
|
RequirementID pgtype.UUID `json:"requirement_id"`
|
|
Branch string `json:"branch"`
|
|
CwdPath string `json:"cwd_path"`
|
|
IsMainWorktree bool `json:"is_main_worktree"`
|
|
Status string `json:"status"`
|
|
OrchestratorStepID pgtype.UUID `json:"orchestrator_step_id"`
|
|
BudgetMaxCostUsd pgtype.Numeric `json:"budget_max_cost_usd"`
|
|
BudgetMaxTokens *int64 `json:"budget_max_tokens"`
|
|
BudgetMaxWallClockSeconds *int32 `json:"budget_max_wall_clock_seconds"`
|
|
}
|
|
|
|
func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (AcpSession, error) {
|
|
row := q.db.QueryRow(ctx, insertSession,
|
|
arg.ID,
|
|
arg.WorkspaceID,
|
|
arg.ProjectID,
|
|
arg.AgentKindID,
|
|
arg.UserID,
|
|
arg.IssueID,
|
|
arg.RequirementID,
|
|
arg.Branch,
|
|
arg.CwdPath,
|
|
arg.IsMainWorktree,
|
|
arg.Status,
|
|
arg.OrchestratorStepID,
|
|
arg.BudgetMaxCostUsd,
|
|
arg.BudgetMaxTokens,
|
|
arg.BudgetMaxWallClockSeconds,
|
|
)
|
|
var i AcpSession
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.ProjectID,
|
|
&i.AgentKindID,
|
|
&i.UserID,
|
|
&i.IssueID,
|
|
&i.RequirementID,
|
|
&i.AgentSessionID,
|
|
&i.Branch,
|
|
&i.CwdPath,
|
|
&i.IsMainWorktree,
|
|
&i.Status,
|
|
&i.Pid,
|
|
&i.ExitCode,
|
|
&i.LastError,
|
|
&i.StartedAt,
|
|
&i.EndedAt,
|
|
&i.LastStopReason,
|
|
&i.OrchestratorStepID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.TotalCostUsd,
|
|
&i.LastActivityAt,
|
|
&i.BudgetMaxCostUsd,
|
|
&i.BudgetMaxTokens,
|
|
&i.BudgetMaxWallClockSeconds,
|
|
&i.TerminatedReason,
|
|
&i.SandboxMode,
|
|
&i.CostUsd,
|
|
&i.TokensTotal,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listAllSessions = `-- name: ListAllSessions :many
|
|
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
|
issue_id, requirement_id, agent_session_id,
|
|
branch, cwd_path, is_main_worktree, status,
|
|
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
|
orchestrator_step_id,
|
|
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
|
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
|
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
|
cost_usd, tokens_total
|
|
FROM acp_sessions
|
|
WHERE ($1::uuid IS NULL OR requirement_id = $1)
|
|
ORDER BY started_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListAllSessions(ctx context.Context, dollar_1 pgtype.UUID) ([]AcpSession, error) {
|
|
rows, err := q.db.Query(ctx, listAllSessions, dollar_1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []AcpSession
|
|
for rows.Next() {
|
|
var i AcpSession
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.ProjectID,
|
|
&i.AgentKindID,
|
|
&i.UserID,
|
|
&i.IssueID,
|
|
&i.RequirementID,
|
|
&i.AgentSessionID,
|
|
&i.Branch,
|
|
&i.CwdPath,
|
|
&i.IsMainWorktree,
|
|
&i.Status,
|
|
&i.Pid,
|
|
&i.ExitCode,
|
|
&i.LastError,
|
|
&i.StartedAt,
|
|
&i.EndedAt,
|
|
&i.LastStopReason,
|
|
&i.OrchestratorStepID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.TotalCostUsd,
|
|
&i.LastActivityAt,
|
|
&i.BudgetMaxCostUsd,
|
|
&i.BudgetMaxTokens,
|
|
&i.BudgetMaxWallClockSeconds,
|
|
&i.TerminatedReason,
|
|
&i.SandboxMode,
|
|
&i.CostUsd,
|
|
&i.TokensTotal,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSessionUsageBySession = `-- name: ListSessionUsageBySession :many
|
|
SELECT id, session_id, user_id, project_id, agent_kind_id, model_id,
|
|
prompt_tokens, completion_tokens, thinking_tokens, cost_usd,
|
|
source_event_id, created_at
|
|
FROM acp_session_usage
|
|
WHERE session_id = $1
|
|
ORDER BY created_at DESC
|
|
LIMIT $2
|
|
`
|
|
|
|
type ListSessionUsageBySessionParams struct {
|
|
SessionID pgtype.UUID `json:"session_id"`
|
|
Limit int32 `json:"limit"`
|
|
}
|
|
|
|
func (q *Queries) ListSessionUsageBySession(ctx context.Context, arg ListSessionUsageBySessionParams) ([]AcpSessionUsage, error) {
|
|
rows, err := q.db.Query(ctx, listSessionUsageBySession, arg.SessionID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []AcpSessionUsage
|
|
for rows.Next() {
|
|
var i AcpSessionUsage
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.SessionID,
|
|
&i.UserID,
|
|
&i.ProjectID,
|
|
&i.AgentKindID,
|
|
&i.ModelID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.CostUsd,
|
|
&i.SourceEventID,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSessionsByUser = `-- name: ListSessionsByUser :many
|
|
SELECT id, workspace_id, project_id, agent_kind_id, user_id,
|
|
issue_id, requirement_id, agent_session_id,
|
|
branch, cwd_path, is_main_worktree, status,
|
|
pid, exit_code, last_error, started_at, ended_at, last_stop_reason,
|
|
orchestrator_step_id,
|
|
prompt_tokens, completion_tokens, thinking_tokens, total_cost_usd,
|
|
last_activity_at, budget_max_cost_usd, budget_max_tokens,
|
|
budget_max_wall_clock_seconds, terminated_reason, sandbox_mode,
|
|
cost_usd, tokens_total
|
|
FROM acp_sessions
|
|
WHERE user_id = $1
|
|
AND ($2::uuid IS NULL OR requirement_id = $2)
|
|
ORDER BY started_at DESC
|
|
`
|
|
|
|
type ListSessionsByUserParams struct {
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
Column2 pgtype.UUID `json:"column_2"`
|
|
}
|
|
|
|
func (q *Queries) ListSessionsByUser(ctx context.Context, arg ListSessionsByUserParams) ([]AcpSession, error) {
|
|
rows, err := q.db.Query(ctx, listSessionsByUser, arg.UserID, arg.Column2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []AcpSession
|
|
for rows.Next() {
|
|
var i AcpSession
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.ProjectID,
|
|
&i.AgentKindID,
|
|
&i.UserID,
|
|
&i.IssueID,
|
|
&i.RequirementID,
|
|
&i.AgentSessionID,
|
|
&i.Branch,
|
|
&i.CwdPath,
|
|
&i.IsMainWorktree,
|
|
&i.Status,
|
|
&i.Pid,
|
|
&i.ExitCode,
|
|
&i.LastError,
|
|
&i.StartedAt,
|
|
&i.EndedAt,
|
|
&i.LastStopReason,
|
|
&i.OrchestratorStepID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.TotalCostUsd,
|
|
&i.LastActivityAt,
|
|
&i.BudgetMaxCostUsd,
|
|
&i.BudgetMaxTokens,
|
|
&i.BudgetMaxWallClockSeconds,
|
|
&i.TerminatedReason,
|
|
&i.SandboxMode,
|
|
&i.CostUsd,
|
|
&i.TokensTotal,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSessionsForReaper = `-- name: ListSessionsForReaper :many
|
|
SELECT id, user_id, started_at, last_activity_at, budget_max_wall_clock_seconds
|
|
FROM acp_sessions
|
|
WHERE status IN ('starting','running')
|
|
AND (
|
|
(budget_max_wall_clock_seconds IS NOT NULL
|
|
AND started_at < now() - make_interval(secs => budget_max_wall_clock_seconds))
|
|
OR ($1::timestamptz IS NOT NULL AND started_at < $1::timestamptz)
|
|
OR last_activity_at < $2::timestamptz
|
|
)
|
|
`
|
|
|
|
type ListSessionsForReaperParams struct {
|
|
Column1 pgtype.Timestamptz `json:"column_1"`
|
|
Column2 pgtype.Timestamptz `json:"column_2"`
|
|
}
|
|
|
|
type ListSessionsForReaperRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
StartedAt pgtype.Timestamptz `json:"started_at"`
|
|
LastActivityAt pgtype.Timestamptz `json:"last_activity_at"`
|
|
BudgetMaxWallClockSeconds *int32 `json:"budget_max_wall_clock_seconds"`
|
|
}
|
|
|
|
// 返回应被回收的活跃 session:
|
|
//
|
|
// 超过自身 wall-clock 上限(budget_max_wall_clock_seconds 非空且 started_at 过早),或
|
|
// 超过全局 wall-clock 上限($1::timestamptz = now - 全局上限),或
|
|
// 空闲超时(last_activity_at < $2::timestamptz)。
|
|
func (q *Queries) ListSessionsForReaper(ctx context.Context, arg ListSessionsForReaperParams) ([]ListSessionsForReaperRow, error) {
|
|
rows, err := q.db.Query(ctx, listSessionsForReaper, arg.Column1, arg.Column2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListSessionsForReaperRow
|
|
for rows.Next() {
|
|
var i ListSessionsForReaperRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.StartedAt,
|
|
&i.LastActivityAt,
|
|
&i.BudgetMaxWallClockSeconds,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const markSessionFailedIfActive = `-- name: MarkSessionFailedIfActive :execrows
|
|
UPDATE acp_sessions
|
|
SET status = 'crashed', last_error = $2, ended_at = now()
|
|
WHERE id = $1 AND status IN ('starting', 'running')
|
|
`
|
|
|
|
type MarkSessionFailedIfActiveParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
LastError *string `json:"last_error"`
|
|
}
|
|
|
|
func (q *Queries) MarkSessionFailedIfActive(ctx context.Context, arg MarkSessionFailedIfActiveParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, markSessionFailedIfActive, arg.ID, arg.LastError)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const markSessionTerminatedReason = `-- name: MarkSessionTerminatedReason :exec
|
|
UPDATE acp_sessions
|
|
SET terminated_reason = $2
|
|
WHERE id = $1 AND terminated_reason IS NULL
|
|
`
|
|
|
|
type MarkSessionTerminatedReasonParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
TerminatedReason *string `json:"terminated_reason"`
|
|
}
|
|
|
|
// 写入终止原因;已有非空 terminated_reason 时不覆盖(reaper / budget 互斥保护)。
|
|
func (q *Queries) MarkSessionTerminatedReason(ctx context.Context, arg MarkSessionTerminatedReasonParams) error {
|
|
_, err := q.db.Exec(ctx, markSessionTerminatedReason, arg.ID, arg.TerminatedReason)
|
|
return err
|
|
}
|
|
|
|
const releaseMainWorktree = `-- name: ReleaseMainWorktree :execrows
|
|
UPDATE workspaces SET active_main_session_id = NULL
|
|
WHERE id = $1 AND active_main_session_id = $2
|
|
`
|
|
|
|
type ReleaseMainWorktreeParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
ActiveMainSessionID pgtype.UUID `json:"active_main_session_id"`
|
|
}
|
|
|
|
func (q *Queries) ReleaseMainWorktree(ctx context.Context, arg ReleaseMainWorktreeParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, releaseMainWorktree, arg.ID, arg.ActiveMainSessionID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const resetSessionForResume = `-- name: ResetSessionForResume :exec
|
|
UPDATE acp_sessions
|
|
SET status = 'starting',
|
|
agent_session_id = NULL,
|
|
pid = NULL,
|
|
exit_code = NULL,
|
|
last_error = NULL,
|
|
ended_at = NULL,
|
|
terminated_reason = NULL,
|
|
last_activity_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
// 把崩溃/退出的 session 复用为新一轮:回到 starting、清空上次运行时字段。
|
|
// 编排器 resume 在重新 Spawn 前调用,使同一行 session 在同 cwd/worktree 上复用。
|
|
func (q *Queries) ResetSessionForResume(ctx context.Context, id pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, resetSessionForResume, id)
|
|
return err
|
|
}
|
|
|
|
const resetStuckSessionsOnRestart = `-- name: ResetStuckSessionsOnRestart :many
|
|
UPDATE acp_sessions
|
|
SET status = 'crashed',
|
|
last_error = 'process_aborted_on_restart',
|
|
ended_at = now()
|
|
WHERE status IN ('starting', 'running')
|
|
RETURNING id, workspace_id, user_id, branch, agent_kind_id, is_main_worktree
|
|
`
|
|
|
|
type ResetStuckSessionsOnRestartRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
Branch string `json:"branch"`
|
|
AgentKindID pgtype.UUID `json:"agent_kind_id"`
|
|
IsMainWorktree bool `json:"is_main_worktree"`
|
|
}
|
|
|
|
func (q *Queries) ResetStuckSessionsOnRestart(ctx context.Context) ([]ResetStuckSessionsOnRestartRow, error) {
|
|
rows, err := q.db.Query(ctx, resetStuckSessionsOnRestart)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ResetStuckSessionsOnRestartRow
|
|
for rows.Next() {
|
|
var i ResetStuckSessionsOnRestartRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.WorkspaceID,
|
|
&i.UserID,
|
|
&i.Branch,
|
|
&i.AgentKindID,
|
|
&i.IsMainWorktree,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const sumProjectCostUSD = `-- name: SumProjectCostUSD :one
|
|
SELECT COALESCE(SUM(cost_usd), 0)::numeric AS cost_usd
|
|
FROM acp_session_usage
|
|
WHERE project_id = $1 AND created_at >= $2
|
|
`
|
|
|
|
type SumProjectCostUSDParams struct {
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
}
|
|
|
|
// 某 project 自 since 起的 ACP 累计花费(per-project 软预算)。
|
|
func (q *Queries) SumProjectCostUSD(ctx context.Context, arg SumProjectCostUSDParams) (pgtype.Numeric, error) {
|
|
row := q.db.QueryRow(ctx, sumProjectCostUSD, arg.ProjectID, arg.CreatedAt)
|
|
var cost_usd pgtype.Numeric
|
|
err := row.Scan(&cost_usd)
|
|
return cost_usd, err
|
|
}
|
|
|
|
const summarizeAcpUsageByDay = `-- name: SummarizeAcpUsageByDay :many
|
|
SELECT date_trunc('day', created_at)::timestamptz AS day,
|
|
SUM(prompt_tokens)::bigint AS prompt_tokens,
|
|
SUM(completion_tokens)::bigint AS completion_tokens,
|
|
SUM(thinking_tokens)::bigint AS thinking_tokens,
|
|
SUM(cost_usd)::numeric AS cost_usd
|
|
FROM acp_session_usage
|
|
WHERE created_at >= $1 AND created_at < $2
|
|
GROUP BY day
|
|
ORDER BY day
|
|
`
|
|
|
|
type SummarizeAcpUsageByDayParams struct {
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
CreatedAt_2 pgtype.Timestamptz `json:"created_at_2"`
|
|
}
|
|
|
|
type SummarizeAcpUsageByDayRow struct {
|
|
Day pgtype.Timestamptz `json:"day"`
|
|
PromptTokens int64 `json:"prompt_tokens"`
|
|
CompletionTokens int64 `json:"completion_tokens"`
|
|
ThinkingTokens int64 `json:"thinking_tokens"`
|
|
CostUsd pgtype.Numeric `json:"cost_usd"`
|
|
}
|
|
|
|
func (q *Queries) SummarizeAcpUsageByDay(ctx context.Context, arg SummarizeAcpUsageByDayParams) ([]SummarizeAcpUsageByDayRow, error) {
|
|
rows, err := q.db.Query(ctx, summarizeAcpUsageByDay, arg.CreatedAt, arg.CreatedAt_2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []SummarizeAcpUsageByDayRow
|
|
for rows.Next() {
|
|
var i SummarizeAcpUsageByDayRow
|
|
if err := rows.Scan(
|
|
&i.Day,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.CostUsd,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const summarizeAcpUsageByModel = `-- name: SummarizeAcpUsageByModel :many
|
|
SELECT model_id,
|
|
SUM(prompt_tokens)::bigint AS prompt_tokens,
|
|
SUM(completion_tokens)::bigint AS completion_tokens,
|
|
SUM(thinking_tokens)::bigint AS thinking_tokens,
|
|
SUM(cost_usd)::numeric AS cost_usd
|
|
FROM acp_session_usage
|
|
WHERE created_at >= $1 AND created_at < $2
|
|
GROUP BY model_id
|
|
ORDER BY cost_usd DESC
|
|
`
|
|
|
|
type SummarizeAcpUsageByModelParams struct {
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
CreatedAt_2 pgtype.Timestamptz `json:"created_at_2"`
|
|
}
|
|
|
|
type SummarizeAcpUsageByModelRow struct {
|
|
ModelID pgtype.UUID `json:"model_id"`
|
|
PromptTokens int64 `json:"prompt_tokens"`
|
|
CompletionTokens int64 `json:"completion_tokens"`
|
|
ThinkingTokens int64 `json:"thinking_tokens"`
|
|
CostUsd pgtype.Numeric `json:"cost_usd"`
|
|
}
|
|
|
|
func (q *Queries) SummarizeAcpUsageByModel(ctx context.Context, arg SummarizeAcpUsageByModelParams) ([]SummarizeAcpUsageByModelRow, error) {
|
|
rows, err := q.db.Query(ctx, summarizeAcpUsageByModel, arg.CreatedAt, arg.CreatedAt_2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []SummarizeAcpUsageByModelRow
|
|
for rows.Next() {
|
|
var i SummarizeAcpUsageByModelRow
|
|
if err := rows.Scan(
|
|
&i.ModelID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.CostUsd,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const summarizeAcpUsageByUser = `-- name: SummarizeAcpUsageByUser :many
|
|
SELECT user_id,
|
|
SUM(prompt_tokens)::bigint AS prompt_tokens,
|
|
SUM(completion_tokens)::bigint AS completion_tokens,
|
|
SUM(thinking_tokens)::bigint AS thinking_tokens,
|
|
SUM(cost_usd)::numeric AS cost_usd
|
|
FROM acp_session_usage
|
|
WHERE created_at >= $1 AND created_at < $2
|
|
GROUP BY user_id
|
|
ORDER BY cost_usd DESC
|
|
`
|
|
|
|
type SummarizeAcpUsageByUserParams struct {
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
CreatedAt_2 pgtype.Timestamptz `json:"created_at_2"`
|
|
}
|
|
|
|
type SummarizeAcpUsageByUserRow struct {
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
PromptTokens int64 `json:"prompt_tokens"`
|
|
CompletionTokens int64 `json:"completion_tokens"`
|
|
ThinkingTokens int64 `json:"thinking_tokens"`
|
|
CostUsd pgtype.Numeric `json:"cost_usd"`
|
|
}
|
|
|
|
func (q *Queries) SummarizeAcpUsageByUser(ctx context.Context, arg SummarizeAcpUsageByUserParams) ([]SummarizeAcpUsageByUserRow, error) {
|
|
rows, err := q.db.Query(ctx, summarizeAcpUsageByUser, arg.CreatedAt, arg.CreatedAt_2)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []SummarizeAcpUsageByUserRow
|
|
for rows.Next() {
|
|
var i SummarizeAcpUsageByUserRow
|
|
if err := rows.Scan(
|
|
&i.UserID,
|
|
&i.PromptTokens,
|
|
&i.CompletionTokens,
|
|
&i.ThinkingTokens,
|
|
&i.CostUsd,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateSessionFinished = `-- name: UpdateSessionFinished :exec
|
|
UPDATE acp_sessions
|
|
SET status = $2, exit_code = $3, last_error = $4, ended_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateSessionFinishedParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
Status string `json:"status"`
|
|
ExitCode *int32 `json:"exit_code"`
|
|
LastError *string `json:"last_error"`
|
|
}
|
|
|
|
func (q *Queries) UpdateSessionFinished(ctx context.Context, arg UpdateSessionFinishedParams) error {
|
|
_, err := q.db.Exec(ctx, updateSessionFinished,
|
|
arg.ID,
|
|
arg.Status,
|
|
arg.ExitCode,
|
|
arg.LastError,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateSessionLastStopReason = `-- name: UpdateSessionLastStopReason :exec
|
|
UPDATE acp_sessions
|
|
SET last_stop_reason = $2
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateSessionLastStopReasonParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
LastStopReason *string `json:"last_stop_reason"`
|
|
}
|
|
|
|
func (q *Queries) UpdateSessionLastStopReason(ctx context.Context, arg UpdateSessionLastStopReasonParams) error {
|
|
_, err := q.db.Exec(ctx, updateSessionLastStopReason, arg.ID, arg.LastStopReason)
|
|
return err
|
|
}
|
|
|
|
const updateSessionRunning = `-- name: UpdateSessionRunning :exec
|
|
UPDATE acp_sessions
|
|
SET status = 'running', agent_session_id = $2, pid = $3
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateSessionRunningParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
AgentSessionID *string `json:"agent_session_id"`
|
|
Pid *int32 `json:"pid"`
|
|
}
|
|
|
|
func (q *Queries) UpdateSessionRunning(ctx context.Context, arg UpdateSessionRunningParams) error {
|
|
_, err := q.db.Exec(ctx, updateSessionRunning, arg.ID, arg.AgentSessionID, arg.Pid)
|
|
return err
|
|
}
|
|
|
|
const updateSessionSandboxMode = `-- name: UpdateSessionSandboxMode :exec
|
|
UPDATE acp_sessions
|
|
SET sandbox_mode = $2
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateSessionSandboxModeParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
SandboxMode *string `json:"sandbox_mode"`
|
|
}
|
|
|
|
// 记录本 session 运行所用的沙箱模式(审计/取证)。
|
|
func (q *Queries) UpdateSessionSandboxMode(ctx context.Context, arg UpdateSessionSandboxModeParams) error {
|
|
_, err := q.db.Exec(ctx, updateSessionSandboxMode, arg.ID, arg.SandboxMode)
|
|
return err
|
|
}
|