Skip to content
Kward Search API index

Class: Kward::Workers::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/workers/manager.rb

Overview

Coordinates background worker execution and role-specific tool policy.

Defined Under Namespace

Classes: WorkerTimeoutError

Constant Summary collapse

DEFAULT_TIMEOUT_SECONDS =
180

Instance Method Summary collapse

Constructor Details

#initialize(client_factory: -> { Client.new }, prompt: nil, workspace_root: Dir.pwd, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, on_status_change: nil, session_store: nil, provider: nil, model: nil, reasoning_effort: nil, write_lock: nil, worker_store: nil, git_guard: nil, write_lane_available: -> { true }) ⇒ Manager

Returns a new instance of Manager.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kward/workers/manager.rb', line 19

def initialize(client_factory: -> { Client.new }, prompt: nil, workspace_root: Dir.pwd, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, on_status_change: nil, session_store: nil, provider: nil, model: nil, reasoning_effort: nil, write_lock: nil, worker_store: nil, git_guard: nil, write_lane_available: -> { true })
  @client_factory = client_factory
  @prompt = prompt
  @workspace_root = ConfigFiles.canonical_workspace_root(workspace_root)
  @timeout_seconds = timeout_seconds
  @on_status_change = on_status_change
  @session_store = session_store
  @provider = provider
  @model = model
  @reasoning_effort = reasoning_effort
  @write_lock = write_lock
  @worker_store = worker_store
  @git_guard = git_guard || GitGuard.new(root: @workspace_root)
  @write_lane_available = write_lane_available
  @workers = {}
  @mutex = Mutex.new
end

Instance Method Details

#archive(id) ⇒ Object



69
70
71
72
73
74
# File 'lib/kward/workers/manager.rb', line 69

def archive(id)
  worker = find(id) || raise(ArgumentError, "Unknown worker: #{id}")
  worker.cancellation.cancel! if %w[queued running].include?(worker.status)
  worker.thread.raise(Cancellation::CancelledError, "cancelled") if worker.thread&.alive?
  update_status(worker, "archived")
end

#cancel(id) ⇒ Object



62
63
64
65
66
67
# File 'lib/kward/workers/manager.rb', line 62

def cancel(id)
  worker = find(id) || raise(ArgumentError, "Unknown worker: #{id}")
  worker.cancellation.cancel!
  worker.thread.raise(Cancellation::CancelledError, "cancelled") if worker.thread&.alive?
  update_status(worker, "cancelled")
end

#continue(id, role:, prompt:, title: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kward/workers/manager.rb', line 42

def continue(id, role:, prompt:, title: nil)
  archived = nil
  worker = build_worker(role: role, prompt: prompt, title: title, id: id)
  @mutex.synchronize do
    archived = @workers.delete(id.to_s)
    @workers[worker.id] = worker
  end
  archived&.update_status("archived")
  @worker_store&.archive(id) if archived || @worker_store&.find(id)
  enqueue(worker, store: false)
end

#find(id) ⇒ Object



58
59
60
# File 'lib/kward/workers/manager.rb', line 58

def find(id)
  @mutex.synchronize { @workers[id.to_s] }
end

#listObject



54
55
56
# File 'lib/kward/workers/manager.rb', line 54

def list
  @mutex.synchronize { @workers.values.reject { |worker| worker.status == "archived" }.sort_by(&:created_at) }
end

#start(role:, prompt:, title: nil, id: nil) ⇒ Object



37
38
39
40
# File 'lib/kward/workers/manager.rb', line 37

def start(role:, prompt:, title: nil, id: nil)
  worker = build_worker(role: role, prompt: prompt, title: title, id: id)
  enqueue(worker)
end