Skip to content

[FEATURE] Support block/proc for dynamic context setup in Agent - #903

Open
mikemikimike wants to merge 4 commits into
crmne:mainfrom
mikemikimike:fix/issue-900-dynamic-agent-context
Open

[FEATURE] Support block/proc for dynamic context setup in Agent#903
mikemikimike wants to merge 4 commits into
crmne:mainfrom
mikemikimike:fix/issue-900-dynamic-agent-context

Conversation

@mikemikimike

Copy link
Copy Markdown

Closes #900

Summary:

  • Allow Agent#context to accept a static Context or a deferred block/Proc.
  • Resolve deferred contexts from declared inputs before building plain Agent chats and instances.
  • Reuse the resolved Context during configuration so each build evaluates the block once.
  • Preserve dynamic evaluation for existing Rails-backed chat records.

Validation:

  • bundle exec rspec spec/ruby_llm/agent_dsl_spec.rb --tag ~live (27 examples, 0 failures)
  • bundle exec rspec --tag ~live (2,543 examples, 17 existing Windows/environment failures; no changed-path failures)
  • bundle exec rubocop lib/ruby_llm/agent.rb spec/ruby_llm/agent_dsl_spec.rb
  • bundle exec archspec check
  • git diff --check

Signed-off-by: mikemikimike <13286568797@163.com>
@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.43%. Comparing base (959f42c) to head (6748c56).
⚠️ Report is 52 commits behind head on main.

Files with missing lines Patch % Lines
lib/ruby_llm/agent.rb 95.83% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #903      +/-   ##
==========================================
- Coverage   94.44%   94.43%   -0.02%     
==========================================
  Files         238      238              
  Lines       11940    12085     +145     
  Branches     2030     2069      +39     
==========================================
+ Hits        11277    11412     +135     
  Misses        285      285              
- Partials      378      388      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The feature is implemented coherently with added specs, and the remaining feedback is limited to small documentation/efficiency fixes.

Pull request overview

This PR extends RubyLLM::Agent’s DSL so context can be configured with a deferred block/Proc, enabling per-run context setup based on declared inputs while ensuring plain (non-Rails) agent chats/instances resolve the context once before chat construction.

Changes:

  • Allow Agent.context to accept either a static Context or a deferred Proc/block.
  • Resolve deferred contexts from runtime inputs before building plain agent chats and instances, and reuse the resolved context during configuration to avoid double evaluation.
  • Add specs covering deferred context resolution from inputs and “evaluate once” behavior.
File summaries
File Description
lib/ruby_llm/agent.rb Adds deferred context support, context pre-resolution for plain builds, and reuses resolved contexts during configuration.
spec/ruby_llm/agent_dsl_spec.rb Adds test coverage for input-driven context blocks and single-evaluation behavior.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/ruby_llm/agent.rb
Comment on lines 545 to 548
apply_protocol(record)
apply_context(record)
apply_context(record, runtime_context(chat: record, inputs: input_values))
runtime = runtime_context(chat: record, inputs: input_values)
apply_instructions(
Comment thread lib/ruby_llm/agent.rb
Comment on lines 368 to +372
# Sets a Context whose configuration chats this agent builds should
# use, applied via Chat#with_context. Called with no argument, returns
# the configured context.
def context(value = nil)
return @context if value.nil?
# use, applied via Chat#with_context. A block defers evaluation until a
# chat is built, with declared ::inputs available as methods. Called
# with no argument, returns the configured context.
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Support block/proc for dynamic context setup in Agent

3 participants