Class: SwarmMemory::SkillState

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_memory/skill_state.rb

Overview

Immutable representation of a loaded skill's state

This object encapsulates all skill-related state for clean management. Immutability prevents accidental mutation bugs during activation.

Examples:

Creating skill state

state = SkillState.new(
  file_path: "skill/security/audit.md",
  tools: ["Read", "Grep", "WorkWithBackend"],
  permissions: { "Bash" => { deny_commands: ["rm"] } }
)

Checking if skill restricts tools

state.restricts_tools?  # => true (has tool list)

Checking if tool is allowed

state.allows_tool?("Read")  # => true
state.allows_tool?("Write") # => false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path:, tools: nil, permissions: {}) ⇒ SkillState

Create a new SkillState

Examples:

No tool restriction

SkillState.new(file_path: "skill/debug.md")  # All tools available

Specific tools only

SkillState.new(
  file_path: "skill/audit.md",
  tools: ["Read", "Grep"]
)

With permissions

SkillState.new(
  file_path: "skill/safe.md",
  tools: ["Bash"],
  permissions: { "Bash" => { deny_commands: ["rm", "sudo"] } }
)

Parameters:

  • file_path (String)

    Path to the skill in memory

  • tools (Array<String>, nil) (defaults to: nil)

    Required tools (nil = no tool restriction)

  • permissions (Hash) (defaults to: {})

    Tool permission overrides



46
47
48
49
50
51
# File 'lib/swarm_memory/skill_state.rb', line 46

def initialize(file_path:, tools: nil, permissions: {})
  @file_path = file_path
  @tools = tools&.map(&:to_s)&.freeze # Normalize and freeze
  @permissions = permissions.freeze
  freeze # Make entire object immutable
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



23
24
25
# File 'lib/swarm_memory/skill_state.rb', line 23

def file_path
  @file_path
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



23
24
25
# File 'lib/swarm_memory/skill_state.rb', line 23

def permissions
  @permissions
end

#toolsObject (readonly)

Returns the value of attribute tools.



23
24
25
# File 'lib/swarm_memory/skill_state.rb', line 23

def tools
  @tools
end

Instance Method Details

#allows_tool?(name) ⇒ Boolean

Check if a specific tool is allowed by this skill

If the skill has no tool restriction (tools is nil), all tools are allowed. If the skill has a tool list, only tools in that list are allowed.

Examples:

No restriction

state = SkillState.new(file_path: "skill/debug.md")
state.allows_tool?("AnyTool")  # => true (no restriction)

Restricted

state = SkillState.new(file_path: "skill/audit.md", tools: ["Read"])
state.allows_tool?("Read")   # => true
state.allows_tool?("Write")  # => false

Parameters:

  • name (String, Symbol)

    Tool name

Returns:

  • (Boolean)

    True if tool is in skill's list (or no restriction)



91
92
93
# File 'lib/swarm_memory/skill_state.rb', line 91

def allows_tool?(name)
  @tools.nil? || @tools.include?(name.to_s)
end

#permissions_for(name) ⇒ Hash?

Get permission config for a tool

Returns the permission configuration hash for a specific tool, or nil if no custom permissions are set.

Examples:

No custom permissions

state = SkillState.new(file_path: "skill/debug.md")
state.permissions_for("Bash")  # => nil

Custom permissions

state = SkillState.new(
  file_path: "skill/safe.md",
  permissions: { "Bash" => { deny_commands: ["rm"] } }
)
state.permissions_for("Bash")  # => { deny_commands: ["rm"] }

Parameters:

  • name (String, Symbol)

    Tool name

Returns:

  • (Hash, nil)

    Permission config or nil



113
114
115
# File 'lib/swarm_memory/skill_state.rb', line 113

def permissions_for(name)
  @permissions[name.to_s] || @permissions[name.to_sym]
end

#restricts_tools?Boolean

Check if skill specifies a tool restriction

Returns true if the skill has a NON-EMPTY tools array. Both nil and empty array mean "no restriction" (don't swap tools).

Examples:

No restriction (nil)

state = SkillState.new(file_path: "skill/debug.md")
state.restricts_tools?  # => false (nil = no restriction)

No restriction (empty array)

state = SkillState.new(file_path: "skill/minimal.md", tools: [])
state.restricts_tools?  # => false (empty = no restriction)

Restriction (specific tools)

state = SkillState.new(file_path: "skill/audit.md", tools: ["Read"])
state.restricts_tools?  # => true (has specific tools)

Returns:

  • (Boolean)

    True if skill restricts toolset



71
72
73
# File 'lib/swarm_memory/skill_state.rb', line 71

def restricts_tools?
  !@tools.nil? && !@tools.empty?
end