Class: RobotLab::Capabilities

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/capabilities.rb

Overview

Declared execution capabilities for a skill script: which paths it may read and write, whether it may use the network, how long it may run, and how much it is trusted.

A skill declares what it wants in SKILL.md front matter; the global config declares the ceiling of what any skill may have. The effective grant is the intersection of the two (see #intersect) — anything the skill asks for that the ceiling does not permit is dropped.

Constant Summary collapse

DEFAULT_TIMEOUT =
60
TRUST_LEVELS =
%w[core external].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fs_read: [], fs_write: [], network: false, timeout: DEFAULT_TIMEOUT, trust: "external") ⇒ Capabilities

Returns a new instance of Capabilities.



18
19
20
21
22
23
24
# File 'lib/robot_lab/capabilities.rb', line 18

def initialize(fs_read: [], fs_write: [], network: false, timeout: DEFAULT_TIMEOUT, trust: "external")
  @fs_read  = Array(fs_read).map(&:to_s)
  @fs_write = Array(fs_write).map(&:to_s)
  @network  = network ? true : false
  @timeout  = timeout.to_i.positive? ? timeout.to_i : DEFAULT_TIMEOUT
  @trust    = TRUST_LEVELS.include?(trust.to_s) ? trust.to_s : "external"
end

Instance Attribute Details

#fs_readObject (readonly)

Returns the value of attribute fs_read.



16
17
18
# File 'lib/robot_lab/capabilities.rb', line 16

def fs_read
  @fs_read
end

#fs_writeObject (readonly)

Returns the value of attribute fs_write.



16
17
18
# File 'lib/robot_lab/capabilities.rb', line 16

def fs_write
  @fs_write
end

#networkObject (readonly)

Returns the value of attribute network.



16
17
18
# File 'lib/robot_lab/capabilities.rb', line 16

def network
  @network
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



16
17
18
# File 'lib/robot_lab/capabilities.rb', line 16

def timeout
  @timeout
end

#trustObject (readonly)

Returns the value of attribute trust.



16
17
18
# File 'lib/robot_lab/capabilities.rb', line 16

def trust
  @trust
end

Class Method Details

.ceiling(config = RobotLab.config) ⇒ Object

Build the ceiling from the global config's sandbox section.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/robot_lab/capabilities.rb', line 46

def self.ceiling(config = RobotLab.config)
  section = config.respond_to?(:sandbox) ? config.sandbox : nil
  return new(fs_read: ["."]) unless section

  new(
    fs_read:  section.fs_read  || ["."],
    fs_write: section.fs_write || [],
    network:  section.network  || false,
    timeout:  section.timeout  || DEFAULT_TIMEOUT
  )
end

.fm_value(front_matter, key, default) ⇒ Object

Look up a front-matter key tolerating string or symbol keys.



39
40
41
42
43
# File 'lib/robot_lab/capabilities.rb', line 39

def self.fm_value(front_matter, key, default)
  value = front_matter[key.to_s]
  value = front_matter[key] if value.nil?
  value.nil? ? default : value
end

.from_front_matter(front_matter) ⇒ Object

Build from a SKILL.md front matter hash (string or symbol keys).



27
28
29
30
31
32
33
34
35
36
# File 'lib/robot_lab/capabilities.rb', line 27

def self.from_front_matter(front_matter)
  fm = front_matter || {}
  new(
    fs_read:  fm_value(fm, :fs_read, []),
    fs_write: fm_value(fm, :fs_write, []),
    network:  fm_value(fm, :network, false),
    timeout:  fm_value(fm, :timeout, DEFAULT_TIMEOUT),
    trust:    fm_value(fm, :trust, "external")
  )
end

Instance Method Details

#core?Boolean

Returns:

  • (Boolean)


58
# File 'lib/robot_lab/capabilities.rb', line 58

def core? = trust == "core"

#intersect(ceiling) ⇒ Object

Effective grant = this (declared) ∩ ceiling. Paths survive only when inside a ceiling root; network requires both; timeout is the smaller; trust stays as declared.



63
64
65
66
67
68
69
70
71
# File 'lib/robot_lab/capabilities.rb', line 63

def intersect(ceiling)
  self.class.new(
    fs_read:  clamp_paths(@fs_read, ceiling.fs_read),
    fs_write: clamp_paths(@fs_write, ceiling.fs_write),
    network:  @network && ceiling.network,
    timeout:  [@timeout, ceiling.timeout].min,
    trust:    @trust
  )
end