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
| Status | Updated | Covered Files |
|---|
| 🟢 Stable | 2026-02-22 | features/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
| ID | Name | Feature Gate | Max Steps | Credits | Tools |
|---|
compliance_checker | Compliance Checker | AGENT_MULTI_STEP | 10 | 30 | analyze_compliance, query_documents, generate_report |
report_generator | Report Generator | AGENT_MULTI_STEP | 15 | 50 | generate_report, query_documents, forecast_budget |
grant_proposal_writer | Grant Proposal Writer | AGENT_AUTONOMOUS | 20 | 80 | generate_report, query_documents, forecast_budget |
expense_auditor | Expense Auditor | AGENT_MULTI_STEP | 15 | 40 | scan_expense, analyze_compliance, generate_report |
journal_assistant | Journal Assistant | AGENT_BASIC | 5 | 15 | generate_journal, query_documents |
Feature Tier Gates
| Feature | Description |
|---|
AGENT_BASIC | Simple single-tool agents (available on lower tiers) |
AGENT_MULTI_STEP | Multi-step agents with tool chaining |
AGENT_AUTONOMOUS | Autonomous 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
- Pre-flight: Validate definition exists, check RBAC permissions, verify feature gate
- Reserve credits: Call
agentQuotaService.reserveCredits() for the defaultCreditBudget
- 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
- Finalize: Update run status, settle credit reservation (refund unused)
Run States
| Status | Description |
|---|
pending | Created, awaiting execution |
running | Actively executing steps |
awaiting_human | Paused for human-in-the-loop escalation |
completed | Successfully finished |
failed | Error occurred or budget exceeded |
cancelled | User-initiated cancellation |
File: src/features/agents/services/agentToolRegistry.ts
The registry manages all tools available to agents. Each tool has:
| Property | Description |
|---|
name | Tool identifier (e.g. analyze_compliance) |
description | What the tool does (sent to LLM) |
inputSchema | Zod schema for input validation |
outputSchema | Zod schema for output validation |
handler | Async function executing the tool |
requiredPermissions | RBAC permissions checked before execution |
creditCost | Per-invocation credit cost |
// 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
| Method | Description |
|---|
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
| Collection | Document Type | Description |
|---|
organizations/{orgId}/agent_runs | AgentRun | Complete record of a single agent execution with all steps, escalations, and credit accounting |
organizations/{orgId}/creditReservations | CreditReservation | Hold 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
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
| Component | Description |
|---|
AgentDashboard | Main agent management view with run history |
AgentRunDetail | Step-by-step execution viewer |
AgentAttributionBadge | ”Generated by AI” attribution marker |
CreditUsageWidget | Dashboard widget showing credit balance and usage |
useAgentRuns Hook
Provides reactive agent run data with filtering and pagination.
Security Model
- Feature gates: Agent execution requires the appropriate
Feature entitlement on the organization’s subscription tier
- RBAC: The triggering user must hold all
requiredPermissions defined on the agent
- Tool sandboxing: Only tools listed in the agent’s
allowedTools can be invoked
- Credit budgets: Hard ceiling prevents runaway costs
- Step budgets: Hard limit on execution iterations
- 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 limit —
maxAgentRunsPerMonth from TierLimits
- Concurrent agents — counts runs with status
'running' or 'awaiting_human'; maxConcurrentAgents per tier
- Hourly rate limit —
agentRunsPerHour
- Step budget per run —
definition.maxSteps (enforced per AgentDefinition)
- Token budget per run —
maxAgentTokenBudgetPerRun from tier limits
- Credit budget per run —
definition.defaultCreditBudget as ceiling
Quota warnings are emitted at 80% and 90% consumption thresholds.
Events
Emitted
| Event | Trigger | Severity | Persisted | Payload |
|---|
AGENT_TASK_STARTED | startRun() completes successfully | INFO | Yes | runId, agentType, agentDefinitionId, triggeredBy, triggeredByName, input, creditsReserved, startedAt |
AGENT_STEP_COMPLETED | executeStep() finishes (success or failure) | INFO | Yes | runId, agentType, stepId, stepIndex, toolName, creditsUsed, tokensUsed, durationMs, completedAt |
AGENT_ESCALATION_REQUIRED | pauseRun() creates an escalation | WARNING | Yes | runId, agentType, stepId, reason, options[], requiredPermission?, escalatedAt |
AGENT_ESCALATION_RESOLVED | resumeRun() resolves escalation | INFO | Yes | runId, agentType, stepId, resolvedBy, resolvedByName, resolution, resolvedAt |
AGENT_TASK_COMPLETED | completeRun() succeeds | INFO | Yes | runId, agentType, totalCreditsUsed, totalSteps, totalInputTokens, totalOutputTokens, durationMs, completedAt |
AGENT_TASK_FAILED | failRun() called | ERROR | Yes | runId, agentType, error, failedAtStep, failedAt |
AGENT_TASK_CANCELLED | cancelRun() called | INFO | Yes | runId, agentType, cancelledBy, cancelledAt |
QUOTA_WARNING | Quota >= 80% or >= 90% consumed | WARNING | Yes | organizationId, resourceType ('agent_runs'), percentageUsed, current, limit |
Consumed
No internal consumers; events flow outward to notification, audit, and analytics systems.
Dependencies
-
Depends on:
billing — CreditService 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
| File | Purpose |
|---|
src/features/agents/index.ts | Barrel export (types, services, hooks, components) |
src/features/agents/services/agentDefinitions.ts | 5 built-in agent definitions |
src/features/agents/services/agentExecutionService.ts | Core execution loop and run management |
src/features/agents/services/agentToolRegistry.ts | Tool registration and dispatch |
src/features/agents/services/agentQuotaService.ts | Credit reservation and settlement |
src/features/agents/types.ts | AgentRun, AgentStep, AgentDefinition types |
src/features/agents/hooks/useAgentRuns.ts | React hook for run data |
src/features/agents/components/AgentDashboard.tsx | Agent management UI |
src/features/billing/services/creditService.ts | Credit balance and transaction service |