Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Invalidate a generated tool when its workflow changes, and expand scopes one way

Damian Legawiec554773299290
diff --git a/spree/core/app/tools/spree/agent_tools/describe_reporting.rb b/spree/core/app/tools/spree/agent_tools/describe_reporting.rb
index a02ccfd178..1ed86458ab 100644
--- a/spree/core/app/tools/spree/agent_tools/describe_reporting.rb
+++ b/spree/core/app/tools/spree/agent_tools/describe_reporting.rb
@@ -31,7 +31,7 @@ module Spree
if context.user_principal?
member.subject.nil? || context.can?(:read, member.subject.call)
else
- member.key_scope.blank? || context.api_key.has_scope?(member.key_scope)
+ context.holds?(member.key_scope)
end
end
end
diff --git a/spree/core/app/tools/spree/agent_tools/query_report.rb b/spree/core/app/tools/spree/agent_tools/query_report.rb
index 98b8fc208d..5049145502 100644
--- a/spree/core/app/tools/spree/agent_tools/query_report.rb
+++ b/spree/core/app/tools/spree/agent_tools/query_report.rb
@@ -81,7 +81,10 @@ module Spree
{ error: "You do not have permission to report on #{forbidden.to_s.demodulize.underscore.humanize.downcase}." }
else
- missing = query.required_key_scopes.reject { |scope| context.api_key.has_scope?(scope) }
+ # Through the context, so a key's scopes expand the one way the rest
+ # of the gate expands them — `has_scope?` treats `read_all` as
+ # granting scopes no staff key may actually hold.
+ missing = query.required_key_scopes.reject { |scope| context.holds?(scope) }
return if missing.empty?
{ error: "This API key lacks the #{missing.to_sentence} scope." }
diff --git a/spree/core/lib/spree/agent_tools/workflow_schema.rb b/spree/core/lib/spree/agent_tools/workflow_schema.rb
index 603632bccb..dc5c18a5bc 100644
--- a/spree/core/lib/spree/agent_tools/workflow_schema.rb
+++ b/spree/core/lib/spree/agent_tools/workflow_schema.rb
@@ -210,7 +210,10 @@ module Spree
# @param workflow_class [Class]
# @return [Hash{Symbol => Hash}] `{types: [String], description: String}`
def parse_docs(workflow_class)
- cache[workflow_class.name] ||= parse_source(workflow_class)
+ file, = workflow_class.instance_method(:perform).source_location
+ key = [workflow_class.name, file, (File.mtime(file) if file && File.exist?(file))]
+
+ cache[key] ||= parse_source(workflow_class)
end
# @return [void]
diff --git a/spree/core/lib/spree/agent_tools/workflow_tool.rb b/spree/core/lib/spree/agent_tools/workflow_tool.rb
index 808ac62454..b445a22606 100644
--- a/spree/core/lib/spree/agent_tools/workflow_tool.rb
+++ b/spree/core/lib/spree/agent_tools/workflow_tool.rb
@@ -28,11 +28,17 @@ module Spree
workflow_class = resolve_workflow(dependency_key)
return if workflow_class.nil?
- # Keyed by what actually shapes the class. A summary override is a
- # callable with no stable identity, so its presence is part of the
- # key but the object is not — otherwise every code reload would add
- # a permanent entry for the same tool.
- cache_key = [dependency_key, workflow_class.name, permission, except, !summary.nil?]
+ # Keyed by what actually shapes the class, including the workflow's
+ # own source location and mtime: a tool's parameters are read off
+ # `perform`, so editing a workflow in development has to produce a
+ # new tool rather than serve the schema built before the reload.
+ # That is the same reason the registry stores names and not classes.
+ #
+ # A summary override is a callable with no stable identity, so its
+ # presence is part of the key but the object is not — otherwise
+ # every reload would leave a permanent entry behind.
+ cache_key = [dependency_key, workflow_class.name, source_fingerprint(workflow_class),
+ permission, except, !summary.nil?]
cached = cache[cache_key]
return cached if cached && cached.summary_override.equal?(summary)
@@ -44,6 +50,21 @@ module Spree
@cache = {}
end
+ # Identifies the source a tool's schema was read from, so an edit in
+ # development invalidates the generated class. Nil in a packaged gem
+ # whose source is not on disk, which simply means the key never
+ # changes there — correct, since the source cannot change either.
+ #
+ # @return [Array, nil]
+ def source_fingerprint(workflow_class)
+ file, = workflow_class.instance_method(:perform).source_location
+ return if file.nil?
+
+ [file, File.mtime(file)]
+ rescue StandardError
+ nil
+ end
+
# The tool name for a dependency key: the workflow's dotted identity
# with dots as underscores, because MCP names allow only letters,
# digits, underscore and hyphen.