Class: RubynCode::Teams::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/teams/manager.rb

Overview

CRUD manager for teammates backed by SQLite.

Provides lifecycle management for agent teammates: spawning, listing, status updates, removal, and parent-child discovery.

Instance Method Summary collapse

Constructor Details

#initialize(db, mailbox:) ⇒ Manager

Returns a new instance of Manager.

Parameters:

  • db (DB::Connection)

    the database connection

  • mailbox (Mailbox)

    the team mailbox for inter-agent messaging



16
17
18
19
20
# File 'lib/rubyn_code/teams/manager.rb', line 16

def initialize(db, mailbox:)
  @db = db
  @mailbox = mailbox
  ensure_table!
end

Instance Method Details

#active_teammatesArray<Teammate>

Returns all teammates with status “active”.

Returns:



160
161
162
163
164
165
166
# File 'lib/rubyn_code/teams/manager.rb', line 160

def active_teammates
  rows = @db.query(
    'SELECT * FROM teammates WHERE status = ? ORDER BY created_at ASC',
    ['active']
  ).to_a
  rows.map { |row| row_to_teammate(row) }
end

#agent_tree(root_id) ⇒ Hash?

Builds a full agent tree from a root agent ID. Returns a nested hash: { agent: Teammate, children: [{ agent:, children: }, …] }

Parameters:

  • root_id (String)

    the root agent’s ID

Returns:

  • (Hash, nil)

    nested tree structure or nil if root not found



117
118
119
120
121
122
# File 'lib/rubyn_code/teams/manager.rb', line 117

def agent_tree(root_id)
  root = find_by_id(root_id)
  return nil unless root

  build_tree_node(root)
end

#children_of(parent_id) ⇒ Array<Teammate>

Returns all direct children of the given parent agent.

Parameters:

  • parent_id (String)

    the parent agent’s ID

Returns:



94
95
96
97
98
99
100
# File 'lib/rubyn_code/teams/manager.rb', line 94

def children_of(parent_id)
  rows = @db.query(
    'SELECT * FROM teammates WHERE parent_agent_id = ? ORDER BY created_at ASC',
    [parent_id]
  ).to_a
  rows.map { |row| row_to_teammate(row) }
end

#find_by_id(id) ⇒ Teammate?

Finds a teammate by ID.

Parameters:

  • id (String)

Returns:



83
84
85
86
87
88
# File 'lib/rubyn_code/teams/manager.rb', line 83

def find_by_id(id)
  rows = @db.query('SELECT * FROM teammates WHERE id = ? LIMIT 1', [id]).to_a
  return nil if rows.empty?

  row_to_teammate(rows.first)
end

#get(name) ⇒ Teammate?

Finds a teammate by name.

Parameters:

  • name (String)

Returns:



72
73
74
75
76
77
# File 'lib/rubyn_code/teams/manager.rb', line 72

def get(name)
  rows = @db.query('SELECT * FROM teammates WHERE name = ? LIMIT 1', [name]).to_a
  return nil if rows.empty?

  row_to_teammate(rows.first)
end

#listArray<Teammate>

Returns all teammates.

Returns:



63
64
65
66
# File 'lib/rubyn_code/teams/manager.rb', line 63

def list
  rows = @db.query('SELECT * FROM teammates ORDER BY created_at ASC').to_a
  rows.map { |row| row_to_teammate(row) }
end

#remove(name) ⇒ void

This method returns an undefined value.

Removes a teammate by name.

Parameters:

  • name (String)

Raises:

  • (Error)

    if the teammate is not found



150
151
152
153
154
155
# File 'lib/rubyn_code/teams/manager.rb', line 150

def remove(name)
  teammate = get(name)
  raise Error, "Teammate '#{name}' not found" unless teammate

  @db.execute('DELETE FROM teammates WHERE name = ?', [name])
end

#rootsArray<Teammate>

Returns all root agents (those with no parent).

Returns:



105
106
107
108
109
110
# File 'lib/rubyn_code/teams/manager.rb', line 105

def roots
  rows = @db.query(
    'SELECT * FROM teammates WHERE parent_agent_id IS NULL ORDER BY created_at ASC'
  ).to_a
  rows.map { |row| row_to_teammate(row) }
end

#spawn(name:, role:, persona: nil, model: nil, parent_agent_id: nil) ⇒ Teammate

Creates a new teammate record.

Parameters:

  • name (String)

    unique teammate name

  • role (String)

    the teammate’s role description

  • persona (String, nil) (defaults to: nil)

    optional persona prompt

  • model (String, nil) (defaults to: nil)

    optional LLM model override

  • parent_agent_id (String, nil) (defaults to: nil)

    ID of the parent agent that spawned this teammate

Returns:

  • (Teammate)

    the newly created teammate

Raises:

  • (Error)

    if a teammate with the given name already exists



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubyn_code/teams/manager.rb', line 31

def spawn(name:, role:, persona: nil, model: nil, parent_agent_id: nil)
  existing = get(name)
  raise Error, "Teammate '#{name}' already exists" if existing

  id = SecureRandom.uuid
  now = Time.now.utc.iso8601
   = JSON.generate({})

  @db.execute(
    <<~SQL,
      INSERT INTO teammates (id, name, role, persona, model, status, parent_agent_id, metadata, created_at)
      VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
    SQL
    [id, name, role, persona, model, 'idle', parent_agent_id, , now]
  )

  Teammate.new(
    id: id,
    name: name,
    role: role,
    persona: persona,
    model: model,
    status: 'idle',
    parent_agent_id: parent_agent_id,
    metadata: {},
    created_at: now
  )
end

#update_status(name, status) ⇒ void

This method returns an undefined value.

Updates a teammate’s status.

Parameters:

  • name (String)
  • status (String)

    one of “idle”, “active”, “offline”

Raises:

  • (ArgumentError)

    if the status is invalid

  • (Error)

    if the teammate is not found



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rubyn_code/teams/manager.rb', line 131

def update_status(name, status)
  unless VALID_STATUSES.include?(status)
    raise ArgumentError, "Invalid status '#{status}'. Must be one of: #{VALID_STATUSES.join(', ')}"
  end

  teammate = get(name)
  raise Error, "Teammate '#{name}' not found" unless teammate

  @db.execute(
    'UPDATE teammates SET status = ? WHERE name = ?',
    [status, name]
  )
end