Class: Kernai::Context
- Inherits:
-
Object
- Object
- Kernai::Context
- Defined in:
- lib/kernai/context.rb
Overview
Execution context passed through Kernel.run. It holds the active plan, its tasks, accumulated task results and recursion depth. Sub-agents receive a fresh child context so their state never pollutes the parent.
Instance Attribute Summary collapse
-
#current_task_id ⇒ Object
Returns the value of attribute current_task_id.
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#media_store ⇒ Object
readonly
Returns the value of attribute media_store.
-
#plan ⇒ Object
Returns the value of attribute plan.
-
#task_results ⇒ Object
Returns the value of attribute task_results.
-
#tasks ⇒ Object
Returns the value of attribute tasks.
Instance Method Summary collapse
-
#hydrate_from_plan(plan) ⇒ Object
Replace current task list with a fresh copy built from the plan so the context can be mutated independently of the source plan.
-
#initialize(plan: nil, tasks: nil, task_results: nil, depth: 0, current_task_id: nil, media_store: nil) ⇒ Context
constructor
A new instance of Context.
-
#record_result(task_id, result) ⇒ Object
Thread-safe write — used by the TaskScheduler running parallel tasks.
-
#recorder_scope ⇒ Object
Snapshot of the scope to stamp on every recorder entry.
- #root? ⇒ Boolean
-
#spawn_child ⇒ Object
Build an isolated child context for a sub-agent.
- #to_h ⇒ Object
Constructor Details
#initialize(plan: nil, tasks: nil, task_results: nil, depth: 0, current_task_id: nil, media_store: nil) ⇒ Context
Returns a new instance of Context.
11 12 13 14 15 16 17 18 19 |
# File 'lib/kernai/context.rb', line 11 def initialize(plan: nil, tasks: nil, task_results: nil, depth: 0, current_task_id: nil, media_store: nil) @plan = plan @tasks = tasks || [] @task_results = task_results || {} @depth = depth @current_task_id = current_task_id @media_store = media_store || MediaStore.new @mutex = Mutex.new end |
Instance Attribute Details
#current_task_id ⇒ Object
Returns the value of attribute current_task_id.
9 10 11 |
# File 'lib/kernai/context.rb', line 9 def current_task_id @current_task_id end |
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
8 9 10 |
# File 'lib/kernai/context.rb', line 8 def depth @depth end |
#media_store ⇒ Object (readonly)
Returns the value of attribute media_store.
8 9 10 |
# File 'lib/kernai/context.rb', line 8 def media_store @media_store end |
#plan ⇒ Object
Returns the value of attribute plan.
9 10 11 |
# File 'lib/kernai/context.rb', line 9 def plan @plan end |
#task_results ⇒ Object
Returns the value of attribute task_results.
9 10 11 |
# File 'lib/kernai/context.rb', line 9 def task_results @task_results end |
#tasks ⇒ Object
Returns the value of attribute tasks.
9 10 11 |
# File 'lib/kernai/context.rb', line 9 def tasks @tasks end |
Instance Method Details
#hydrate_from_plan(plan) ⇒ Object
Replace current task list with a fresh copy built from the plan so the context can be mutated independently of the source plan.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kernai/context.rb', line 39 def hydrate_from_plan(plan) @plan = plan @tasks = plan.tasks.map do |t| Task.new( id: t.id, input: t.input, parallel: t.parallel, depends_on: t.depends_on.dup, status: :pending ) end @task_results = {} end |
#record_result(task_id, result) ⇒ Object
Thread-safe write — used by the TaskScheduler running parallel tasks.
33 34 35 |
# File 'lib/kernai/context.rb', line 33 def record_result(task_id, result) @mutex.synchronize { @task_results[task_id.to_s] = result } end |
#recorder_scope ⇒ Object
Snapshot of the scope to stamp on every recorder entry. Sub-agents inherit depth + the task they were spawned for so the flat recording stream can be grouped into a tree by consumers.
28 29 30 |
# File 'lib/kernai/context.rb', line 28 def recorder_scope { depth: @depth, task_id: @current_task_id } end |
#root? ⇒ Boolean
21 22 23 |
# File 'lib/kernai/context.rb', line 21 def root? @depth.zero? end |
#spawn_child ⇒ Object
Build an isolated child context for a sub-agent. Depth is incremented so nested plans can be detected and ignored. The media store is shared so any Media a parent or sibling produced is visible to the child — workflows need this to pass images between tasks.
57 58 59 |
# File 'lib/kernai/context.rb', line 57 def spawn_child self.class.new(depth: @depth + 1, media_store: @media_store) end |
#to_h ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/kernai/context.rb', line 61 def to_h { plan: @plan&.to_h, tasks: @tasks.map(&:to_h), task_results: @task_results, depth: @depth, current_task_id: @current_task_id } end |