Skip to main content

Documentation Index

Fetch the complete documentation index at: https://grantmaster.dev/llms.txt

Use this file to discover all available pages before exploring further.

Engineering reference: For service contracts, EventBus events, and data-layer details see src/features/agents/agents.md.

last_updated: 2026-02-22

Agent Execution Framework

StatusUpdatedCovered Files
🟢 Stable2026-02-22features/agents/, features/agents/services/agentDefinitions.ts, features/agents/services/agentExecutionService.ts, features/agents/services/agentToolRegistry.ts, features/agents/services/agentQuotaService.ts, features/billing/services/creditService.ts

Overview

The Agent Execution Framework provides a multi-step AI agent system powered by Google Gemini. Agents perform complex, multi-turn tasks — compliance checks, report generation, grant proposals, expense audits, and journal assistance — within a governed execution environment with RBAC enforcement, credit budgeting, and tool sandboxing.

Architecture

┌─────────────────────────────────────────────────────────┐
│  Agent Definitions (compile-time)                       │
│  Static declarations: tools, budgets, permissions       │
├─────────────────────────────────────────────────────────┤
│  Agent Execution Service (runtime)                      │
│  Step loop, tool dispatch, credit tracking              │
├─────────────────────────────────────────────────────────┤
│  Agent Tool Registry                                    │
│  Tool lookup, permission enforcement, I/O schemas       │
├─────────────────────────────────────────────────────────┤
│  Agent Quota Service                                    │
│  Credit reservation, balance checks, usage tracking     │
└─────────────────────────────────────────────────────────┘

Built-in Agents

IDNameFeature GateMax StepsCreditsTools
compliance_checkerCompliance CheckerAGENT_MULTI_STEP1030analyze_compliance, query_documents, generate_report
report_generatorReport GeneratorAGENT_MULTI_STEP1550generate_report, query_documents, forecast_budget
grant_proposal_writerGrant Proposal WriterAGENT_AUTONOMOUS2080generate_report, query_documents, forecast_budget
expense_auditorExpense AuditorAGENT_MULTI_STEP1540scan_expense, analyze_compliance, generate_report
journal_assistantJournal AssistantAGENT_BASIC515generate_journal, query_documents

Feature Tier Gates

FeatureDescription
AGENT_BASICSimple single-tool agents (available on lower tiers)
AGENT_MULTI_STEPMulti-step agents with tool chaining
AGENT_AUTONOMOUSAutonomous agents with higher step/credit budgets

Agent Definition Schema

Each agent is declared via a static AgentDefinition:
interface AgentDefinition {
  id: string;                         // Unique identifier (e.g. 'compliance_checker')
  name: string;                       // Human-readable name
  description: string;                // Purpose description
  requiredFeature: Feature;           // Tier entitlement gate
  requiredPermissions: Permission[];  // RBAC permissions the user must hold
  allowedTools: string[];             // Tool allowlist (sandbox boundary)
  maxSteps: number;                   // Step budget per run
  defaultCreditBudget: number;        // Credit cost ceiling
  icon: string;                       // Lucide icon name
  category: string;                   // Domain grouping
}
Definitions are stored in AGENT_DEFINITIONS (array) and AGENT_DEFINITIONS_MAP (Map for O(1) lookup).

Execution Flow

Run Lifecycle

Step Loop

  1. Pre-flight: Validate definition exists, check RBAC permissions, verify feature gate
  2. Reserve credits: Call agentQuotaService.reserveCredits() for the defaultCreditBudget
  3. Execute steps (loop until goal, error, or budget exhaustion):
    • Send context + tool results to Gemini
    • Parse tool call from response
    • Validate tool is in allowedTools (sandbox)
    • Execute tool via AgentToolRegistry
    • Record step result in AgentStep
    • Decrement remaining step budget
  4. Finalize: Update run status, settle credit reservation (refund unused)

Run States

StatusDescription
pendingCreated, awaiting execution
runningActively executing steps
awaiting_humanPaused for human-in-the-loop escalation
completedSuccessfully finished
failedError occurred or budget exceeded
cancelledUser-initiated cancellation

Agent Tool Registry

File: src/features/agents/services/agentToolRegistry.ts The registry manages all tools available to agents. Each tool has:
PropertyDescription
nameTool identifier (e.g. analyze_compliance)
descriptionWhat the tool does (sent to LLM)
inputSchemaZod schema for input validation
outputSchemaZod schema for output validation
handlerAsync function executing the tool
requiredPermissionsRBAC permissions checked before execution
creditCostPer-invocation credit cost

Tool Registration

// Core tools registered at startup
agentToolRegistry.register(createAgentTool({
  name: 'analyze_compliance',
  description: 'Analyze entries for compliance issues',
  inputSchema: z.object({ entryIds: z.array(z.string()) }),
  handler: async (input, context) => { /* ... */ },
  requiredPermissions: [Permission.VIEW_JOURNALS],
  creditCost: 2,
}));
Extensions can contribute additional tools via the manifest contributions.agentTools array.

Credit & Quota System

Credit Flow

User triggers agent run

    ├── Check credit balance ≥ defaultCreditBudget
    │       └── Insufficient → reject with INSUFFICIENT_CREDITS

    ├── Reserve credits (hold)

    ├── Execute steps (each tool call deducts from reservation)

    └── Settle reservation
            ├── Success → deduct actual usage, refund remainder
            └── Failure → full refund of reserved credits

AgentQuotaService

MethodDescription
checkQuota(orgId, agentId)Pre-flight balance check
reserveCredits(orgId, amount)Create credit hold
settleReservation(reservationId, actualUsage)Finalize and refund excess
getUsageStats(orgId)Agent usage analytics

Data Model

Firestore Collections

CollectionDocument TypeDescription
organizations/{orgId}/agent_runsAgentRunComplete record of a single agent execution with all steps, escalations, and credit accounting
organizations/{orgId}/creditReservationsCreditReservationHold on credits created at run start, settled at completion or failure

AgentRun

Complete record of an agent execution:
interface AgentRun {
  id: string;
  organizationId: string;
  agentType: string;
  agentDefinitionId: string;
  status: AgentRunStatus;
  triggeredBy: string;              // User ID for audit trail
  triggeredByName: string;
  permissions: Permission[];        // Captured RBAC at run start
  input: Record<string, unknown>;
  output?: Record<string, unknown>;
  steps: AgentStep[];               // Embedded array of execution steps
  creditReservationId: string;
  creditsReserved: number;
  creditsConsumed: number;
  totalInputTokens: number;         // Token tracking for analytics
  totalOutputTokens: number;
  startedAt: string;
  completedAt?: string;
  durationMs?: number;
  error?: AgentRunError;
  escalation?: AgentEscalation;     // Human-in-the-loop state
}

AgentStep

Single tool invocation within a run:
interface AgentStep {
  id: string;
  runId: string;
  stepIndex: number;
  toolName: string;
  status: 'pending' | 'running' | 'completed' | 'failed';
  input: Record<string, unknown>;
  output?: Record<string, unknown>;
  creditsUsed: number;
  inputTokens: number;
  outputTokens: number;
  durationMs: number;
  startedAt: string;
  completedAt?: string;
  error?: string;
}

AgentEscalation

Human-in-the-loop pause state:
  • runId, stepId, reason — what triggered the pause
  • options: EscalationOption[] — available resolution choices (approve, reject, modify, skip)
  • requiredPermission?: Permission — RBAC gating for who can resolve
  • resolvedBy, resolvedByName, resolution, resolvedAt — resolution audit trail

AgentToolResult

Return value from tool execution:
  • success, output, error — outcome
  • tokensUsed?: { input, output } — token accounting
  • creditsUsed — cost of this invocation
  • canonicalStatus? — optional domain-specific status projection (e.g., from compliance analysis)

UI Components

ComponentDescription
AgentDashboardMain agent management view with run history
AgentRunDetailStep-by-step execution viewer
AgentAttributionBadge”Generated by AI” attribution marker
CreditUsageWidgetDashboard widget showing credit balance and usage

useAgentRuns Hook

Provides reactive agent run data with filtering and pagination.

Security Model

  1. Feature gates: Agent execution requires the appropriate Feature entitlement on the organization’s subscription tier
  2. RBAC: The triggering user must hold all requiredPermissions defined on the agent
  3. Tool sandboxing: Only tools listed in the agent’s allowedTools can be invoked
  4. Credit budgets: Hard ceiling prevents runaway costs
  5. Step budgets: Hard limit on execution iterations
  6. Tenant isolation: All data access is scoped to the user’s organization

Quota Enforcement

AgentQuotaService validates these tier-based limits before and during execution:
  • Monthly run limitmaxAgentRunsPerMonth from TierLimits
  • Concurrent agents — counts runs with status 'running' or 'awaiting_human'; maxConcurrentAgents per tier
  • Hourly rate limitagentRunsPerHour
  • Step budget per rundefinition.maxSteps (enforced per AgentDefinition)
  • Token budget per runmaxAgentTokenBudgetPerRun from tier limits
  • Credit budget per rundefinition.defaultCreditBudget as ceiling
Quota warnings are emitted at 80% and 90% consumption thresholds.

Events

Emitted

EventTriggerSeverityPersistedPayload
AGENT_TASK_STARTEDstartRun() completes successfullyINFOYesrunId, agentType, agentDefinitionId, triggeredBy, triggeredByName, input, creditsReserved, startedAt
AGENT_STEP_COMPLETEDexecuteStep() finishes (success or failure)INFOYesrunId, agentType, stepId, stepIndex, toolName, creditsUsed, tokensUsed, durationMs, completedAt
AGENT_ESCALATION_REQUIREDpauseRun() creates an escalationWARNINGYesrunId, agentType, stepId, reason, options[], requiredPermission?, escalatedAt
AGENT_ESCALATION_RESOLVEDresumeRun() resolves escalationINFOYesrunId, agentType, stepId, resolvedBy, resolvedByName, resolution, resolvedAt
AGENT_TASK_COMPLETEDcompleteRun() succeedsINFOYesrunId, agentType, totalCreditsUsed, totalSteps, totalInputTokens, totalOutputTokens, durationMs, completedAt
AGENT_TASK_FAILEDfailRun() calledERRORYesrunId, agentType, error, failedAtStep, failedAt
AGENT_TASK_CANCELLEDcancelRun() calledINFOYesrunId, agentType, cancelledBy, cancelledAt
QUOTA_WARNINGQuota >= 80% or >= 90% consumedWARNINGYesorganizationId, resourceType ('agent_runs'), percentageUsed, current, limit

Consumed

No internal consumers; events flow outward to notification, audit, and analytics systems.

Dependencies

  • Depends on:
    • billingCreditService for credit reservation/consumption
    • ai — Gemini service for LLM calls (via tool wrappers)
    • ai/services/geminiServiceFacade — High-level Gemini wrappers (compliance analysis, forecasting, receipts, reports)
    • ai/services/geminiServiceProposalImpact — Proposal generation
    • ai/services/ragPipeline — RAG document queries
    • grants/application — Proposal context retrieval and draft service
    • entitlements — Feature gates and subscription tier limits
    • core/BaseService — Service base class, logging, event emission
    • core/eventBus — SystemEvent emission
    • core/firebase — Firestore client
  • Depended on by:
    • Features that trigger agents (grants, compliance, reporting, journals, expenses)
    • Dashboard & reporting features (to display agent runs and credit usage)

Key Files Reference

FilePurpose
src/features/agents/index.tsBarrel export (types, services, hooks, components)
src/features/agents/services/agentDefinitions.ts5 built-in agent definitions
src/features/agents/services/agentExecutionService.tsCore execution loop and run management
src/features/agents/services/agentToolRegistry.tsTool registration and dispatch
src/features/agents/services/agentQuotaService.tsCredit reservation and settlement
src/features/agents/types.tsAgentRun, AgentStep, AgentDefinition types
src/features/agents/hooks/useAgentRuns.tsReact hook for run data
src/features/agents/components/AgentDashboard.tsxAgent management UI
src/features/billing/services/creditService.tsCredit balance and transaction service