-- name: agentsession__insert_if_absent :one
INSERT INTO agent_sessions (
    run_id,
    correlation_id,
    project_id,
    repository_full_name,
    agent_key,
    issue_number,
    branch_name,
    pr_number,
    pr_url,
    trigger_kind,
    template_kind,
    template_source,
    template_locale,
    model,
    reasoning_effort,
    status,
    session_id,
    session_json,
    codex_cli_session_path,
    codex_cli_session_json,
    snapshot_version,
    snapshot_checksum,
    snapshot_updated_at,
    started_at,
    finished_at,
    updated_at
)
VALUES (
    $1::uuid,
    $2,
    $3::uuid,
    $4,
    $5,
    $6,
    $7,
    $8,
    $9,
    $10,
    $11,
    $12,
    $13,
    $14,
    $15,
    $16,
    $17,
    COALESCE($18::jsonb, '{}'::jsonb),
    $19,
    $20::jsonb,
    1,
    NULLIF($23, ''),
    COALESCE($24, NOW()),
    COALESCE($21, NOW()),
    $22,
    NOW()
)
ON CONFLICT (run_id) DO NOTHING
RETURNING snapshot_version, snapshot_checksum, snapshot_updated_at;
