Module: Parse::Agent::FieldPolicy

Defined in:
lib/parse/agent/field_policy.rb

Overview

Per-agent field narrowing.

A class's agent_fields declaration is the CEILING: the most any agent may read. A Parse::Agent.new(fields: { "Post" => [:title, :status] }) policy NARROWS that ceiling for one agent (one MCP deployment), so a user-facing assistant and an analytics endpoint in the same process can expose different subsets of the same model. A policy can never widen past the ceiling, and a sub-agent's policy intersects its parent's.

The effective allowlist is resolved by MetadataRegistry.field_allowlist, which every enforcement point already calls (projection, where:/keys: checks, aggregation pipelines, Atlas Search fields, schema output, exports, semantic_search chunk text). This module supplies the narrowing for the agent whose tool is currently executing: Tools.invoke wraps every tool call in FieldPolicy.with, so the policy applies without threading the agent through each helper.

The scope lives in fiber storage (Fiber[]), which child fibers and threads inherit when they are created. Concurrent requests on different threads never see each other's policy, and work a custom tool hands to a thread or fiber it starts stays narrowed.

Constant Summary collapse

SCOPE_KEY =
:parse_agent_field_policy_scope

Class Method Summary collapse

Class Method Details

.current_agent ⇒ Parse::Agent?

Returns the innermost agent whose tool is executing.

Returns:

  • (Parse::Agent, nil) —

    the innermost agent whose tool is executing.



50
51
52
# File 'lib/parse/agent/field_policy.rb', line 50

def current_agent
  Fiber[SCOPE_KEY]&.last
end

.narrowing_for(class_name) ⇒ Array<String>?

Wire-format field names the current agent narrows class_name to, or nil when the agent places no narrowing on that class (or no agent is in scope).

Parameters:

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parse/agent/field_policy.rb', line 60

def narrowing_for(class_name)
  stack = Fiber[SCOPE_KEY]
  return nil if stack.nil? || stack.empty?
  result = nil
  stack.uniq.each do |agent|
    next unless agent.respond_to?(:field_narrowing_for)
    names = agent.field_narrowing_for(class_name)
    next if names.nil?
    result = result ? (result & names) : names
  end
  result
end

.with(agent) ⇒ Object

Run the block with agent's field narrowing in effect.

Scopes nest: a tool that builds another agent and invokes it (even one constructed without parent:) runs under BOTH policies, so the inner call can only narrow further, never escape the outer agent's policy.

Parameters:

Returns:

  • the block's value



41
42
43
44
45
46
47
# File 'lib/parse/agent/field_policy.rb', line 41

def with(agent)
  previous = Fiber[SCOPE_KEY]
  Fiber[SCOPE_KEY] = (previous || []) + [agent]
  yield
ensure
  Fiber[SCOPE_KEY] = previous
end