Class: RailsAiContext::Tools::GetJobPattern

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_context/tools/get_job_pattern.rb

Constant Summary

Constants inherited from BaseTool

BaseTool::DEFAULT_SESSION, BaseTool::MAX_SESSIONS, BaseTool::MAX_SESSION_ID_LENGTH, BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, api_only_app?, api_only_note, cache_key, cached_context, config, current_session, #dedupe_put_patch_routes, detail_param?, error_response, evict_oldest_sessions, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, guide_row, inherited, introspection_warnings_note, invalid_detail_note, normalize_detail, not_found_response, paginate, rails_app, rails_env_name, registered_tools, reset_all_caches!, reset_cache!, session_from, session_params, session_queries, session_record, session_reset!, static_tier_banner, static_tier_refusal, text_response, touch_session, unavailable_note, with_session, with_session_for

Methods included from SectionFetch

#fetch_section, usable?

Class Method Details

.call(job: nil, detail: "standard", server_context: nil) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rails_ai_context/tools/get_job_pattern.rb', line 33

def self.call(job: nil, detail: "standard", server_context: nil)
  root = rails_app.root.to_s
  jobs_dir = File.join(root, "app", "jobs")
  real_root = File.realpath(root).to_s
  jobs_dir_exists = Dir.exist?(jobs_dir)

  if jobs_dir_exists
    real_jobs_dir = File.realpath(jobs_dir).to_s
    files = Dir.glob(File.join(jobs_dir, "**", "*.rb"))
               .filter_map { |f| safe_glob_realpath(f, real_jobs_dir, real_root) }
               .sort
    job_files = files.reject { |f| File.basename(f) == "application_job.rb" }
  else
    real_jobs_dir = jobs_dir
    job_files = []
  end

  # Pull enriched channel data from the cached introspector context - this
  # gives us the v5.8.0 fields (identified_by, streams, periodic, actions)
  # that JobIntrospector#extract_channels populates.
  jobs_data = cached_context[:jobs]
  channels = (jobs_data.is_a?(Hash) ? jobs_data[:channels] : nil) || []
  channels = channels.reject { |c| c.is_a?(Hash) && c[:error] }
  channels_note = unavailable_note(jobs_data)

  sidekiq_line = sidekiq_queues_line(jobs_data)

  # Single-job query: requires jobs to be present.
  if job
    return text_response(no_job_files_message(jobs_dir_exists, sidekiq_line)) if job_files.empty?
    return format_single_job(job, job_files, real_jobs_dir, real_root)
  end

  # No jobs and no channels - bail out, but say honestly whether the
  # directory is missing or just has no job classes beyond ApplicationJob.
  # Channel absence is only a real negative when the :jobs section
  # actually ran - if it's unavailable (static tier), say so instead of
  # claiming "no channels detected".
  if job_files.empty? && channels.empty?
    return text_response(no_jobs_or_channels_message(jobs_dir_exists, channels_note, sidekiq_line))
  end

  # Compose: jobs section (if any) + channels section (if any).
  lines = []
  lines.concat(format_job_listing_lines(job_files, real_jobs_dir, real_root, detail)) if job_files.any?
  if channels.any?
    lines << "" if lines.any?
    lines.concat(format_channels_section(channels))
  end
  # A count of what app/jobs/ holds is still a claim about the app's async
  # work, and on an app that runs most of it through Sidekiq workers that
  # count is the small half. The queues are already in hand either way.
  if sidekiq_line
    lines << "" if lines.any?
    lines << "_#{sidekiq_line} Workers outside app/jobs/ are not covered by this tool._"
  end
  text_response(lines.join("\n"))
end