Class: Karst::Access::PrincipalSampler

Inherits:
Object
  • Object
show all
Defined in:
lib/karst/access/principal_sampler.rb

Overview

Selects a small candidate set without ever scanning the full principal relation: one bounded recent pool, stratified in memory by coarse states derived from the schema itself -- boolean and enum columns, nullable-foreign-key presence, and low-cardinality scalars, minus anything PII- or tenancy-shaped. Nothing here is configurable: an application that needs a specific rare user reaches it through a candidate population (Karst::Access::CandidatePopulation), which is named, reportable evidence, rather than by tuning how the ordinary sample spreads.

Application-authored populations deliberately do not live here. They are a second search stage owned by Karst::Access::Search, which runs them only after an ordinary sample observes no usable outcome -- a population folded into this sample would silently become part of an ordinary-looking result, and Karst could never report the stronger, true story: "the ordinary sample failed; then system_admins reached it." This class only selects candidates. Access::Sweep remains the sole source of behavioral evidence. rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: UnsupportedPrimaryKey

Constant Summary collapse

CARDINALITY_CUTOFF =
10
MAX_DIMENSIONS =
8
TENANCY_FK_TOKENS =
%w[tenant account organization org company workspace team customer client shop].freeze
ALLOWED_SCALAR_TYPES =
%i[integer bigint string].freeze
Candidate =
Value.define(:principal, :reasons)
Result =
Value.define(:principals, :candidates, :strategy, :queries, :candidate_pool_size)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, limit: Karst.config.access_sweep_limit, pool_size: Karst.config.principal_candidate_pool_size) ⇒ PrincipalSampler

Returns a new instance of PrincipalSampler.



47
48
49
50
51
52
53
54
# File 'lib/karst/access/principal_sampler.rb', line 47

def initialize(source:, limit: Karst.config.access_sweep_limit,
               pool_size: Karst.config.principal_candidate_pool_size)
  @source = source
  @limit = limit
  @pool_size = pool_size
  @queries = 0
  @query_budget = self.class.query_budget
end

Class Method Details

.query_budget(_limit = nil) ⇒ Object

Exactly one bounded recent-pool query. Every stratification decision after that is made in memory over that pool.



43
44
45
# File 'lib/karst/access/principal_sampler.rb', line 43

def self.query_budget(_limit = nil)
  1
end

.representative_capable?(source) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/karst/access/principal_sampler.rb', line 63

def self.representative_capable?(source)
  return true if defined?(ActiveRecord::Relation) && source.is_a?(ActiveRecord::Relation)

  defined?(ActiveRecord::Base) && source.is_a?(Class) && source < ActiveRecord::Base
rescue StandardError
  false
end

Instance Method Details

#callObject



56
57
58
59
60
61
# File 'lib/karst/access/principal_sampler.rb', line 56

def call
  relation = active_record_relation
  return fallback_sample unless relation

  representative_sample(relation)
end