Class: RubynCode::Hooks::GoalHook

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/hooks/goal_hook.rb

Overview

A Stop hook that keeps the agent working until a session goal is met.

When active, firing the :stop event asks an evaluator whether the goal condition is satisfied. If it is, the hook deactivates itself (the goal “auto-clears”) and allows the agent to stop. If not, it returns a block decision whose reason is re-injected into the conversation, nudging the agent to keep working.

A max-attempts safety valve prevents an unsatisfiable goal from looping forever — after MAX_ATTEMPTS consecutive blocks the hook gives up and lets the agent stop.

Constant Summary collapse

DEFAULT_MAX_ATTEMPTS =
12

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(condition:, evaluator: nil, max_attempts: DEFAULT_MAX_ATTEMPTS) ⇒ GoalHook

Returns a new instance of GoalHook.

Parameters:

  • condition (String)

    plain-language goal condition

  • evaluator (#call, nil) (defaults to: nil)

    judges completion; nil disables auto-clear

  • max_attempts (Integer) (defaults to: DEFAULT_MAX_ATTEMPTS)

    consecutive blocks before giving up



25
26
27
28
29
30
31
# File 'lib/rubyn_code/hooks/goal_hook.rb', line 25

def initialize(condition:, evaluator: nil, max_attempts: DEFAULT_MAX_ATTEMPTS)
  @condition = condition
  @evaluator = evaluator
  @max_attempts = max_attempts
  @attempts = 0
  @active = true
end

Instance Attribute Details

#conditionString (readonly)

Returns the goal condition.

Returns:

  • (String)

    the goal condition



20
21
22
# File 'lib/rubyn_code/hooks/goal_hook.rb', line 20

def condition
  @condition
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


34
# File 'lib/rubyn_code/hooks/goal_hook.rb', line 34

def active? = @active

#call(conversation: nil, **_kwargs) ⇒ Hash?

Fired on the :stop event by Hooks::Runner.

Parameters:

Returns:

  • (Hash, nil)

    { block: true, reason: } to keep working, else nil



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubyn_code/hooks/goal_hook.rb', line 46

def call(conversation: nil, **_kwargs)
  return nil unless @active

  if goal_met?(conversation)
    @active = false
    return nil
  end

  @attempts += 1
  if @attempts >= @max_attempts
    @active = false
    RubynCode::Debug.warn("Goal abandoned after #{@max_attempts} attempts: #{@condition}")
    return nil
  end

  { block: true, reason: reminder }
end

#clear!void

This method returns an undefined value.

Cancel the goal early (e.g. ‘/goal clear`).



38
39
40
# File 'lib/rubyn_code/hooks/goal_hook.rb', line 38

def clear!
  @active = false
end