Class: PromptObjects::Execution::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/execution/policy.rb

Constant Summary collapse

SIDE_EFFECTS =
%i[read write exclusive].freeze
WORKLOADS =
%i[blocking cpu].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parallel_safe:, side_effect:, resource_keys: nil, workload: :blocking) ⇒ Policy

Returns a new instance of Policy.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
# File 'lib/prompt_objects/execution/policy.rb', line 11

def initialize(parallel_safe:, side_effect:, resource_keys: nil, workload: :blocking)
  raise ArgumentError, "unknown side effect: #{side_effect}" unless SIDE_EFFECTS.include?(side_effect.to_sym)
  raise ArgumentError, "unknown workload: #{workload}" unless WORKLOADS.include?(workload.to_sym)

  @parallel_safe = !!parallel_safe
  @side_effect = side_effect.to_sym
  @workload = workload.to_sym
  @resource_resolver = resource_keys
  freeze
end

Instance Attribute Details

#parallel_safeObject (readonly)

Returns the value of attribute parallel_safe.



22
23
24
# File 'lib/prompt_objects/execution/policy.rb', line 22

def parallel_safe
  @parallel_safe
end

#side_effectObject (readonly)

Returns the value of attribute side_effect.



22
23
24
# File 'lib/prompt_objects/execution/policy.rb', line 22

def side_effect
  @side_effect
end

#workloadObject (readonly)

Returns the value of attribute workload.



22
23
24
# File 'lib/prompt_objects/execution/policy.rb', line 22

def workload
  @workload
end

Instance Method Details

#resolve(arguments:, context:, capability:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prompt_objects/execution/policy.rb', line 24

def resolve(arguments:, context:, capability:)
  keys = if @resource_resolver
           Array(@resource_resolver.call(arguments, context, capability))
         elsif side_effect == :exclusive
           ["workspace:#{context.workspace_session_id}:exclusive"]
         else
           []
         end
  ResolvedPolicy.new(
    parallel_safe: parallel_safe,
    side_effect: side_effect,
    workload: workload,
    resource_keys: keys.compact.map(&:to_s).uniq.sort.freeze,
    signature: signature(capability).freeze
  )
end

#signature(capability = nil) ⇒ Object



41
42
43
44
# File 'lib/prompt_objects/execution/policy.rb', line 41

def signature(capability = nil)
  resolver_location = @resource_resolver&.source_location&.join(":")
  [capability&.class&.name, parallel_safe, side_effect, workload, resolver_location].compact.join("|")
end