-
Notifications
You must be signed in to change notification settings - Fork 49
feat: Add environment variable overrides for runtime configuration #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This PR introduces a comprehensive system for overriding runtime constants via environment variables, enabling fine-grained control over execution limits, timeouts, and constraints without code modification. ## Changes ### Core Runtime Configuration System **New File: `packages/agents-core/src/constants/runtime.ts`** - Centralized all ~50 runtime constants into a single file - Added `getEnvNumber()` helper for reading environment variables with fallback to defaults - All constants now support `AGENTS_*` prefixed environment variable overrides - Maintains backward compatibility - defaults unchanged **Categories of configurable constants:** - Agent Execution Loop (4 constants) - Sub-Agent Turn Execution (3 constants) - LLM Generation Timeouts (4 constants) - Function Tool Sandbox (7 constants) - MCP Tools (6 constants) - Delegation & A2A Communication (8 constants) - Artifact Processing (5 constants) - Session Management (2 constants) - Status Updates & Streaming (4 constants) - Stream Buffers (6 constants) - API Validation Limits (4 constants) - Data Components & Conversation History (2 constants) ### Environment Variable Validation **Updated: `agents-manage-api/src/env.ts`** - Added Zod schema for 10 validation-related constants - Validates environment overrides at startup - Exports `runtimeConfig` object for consumption **Updated: `agents-run-api/src/env.ts`** - Added Zod schema for 46 runtime execution constants - Validates environment overrides at startup - Exports `runtimeConfig` object for consumption ### Documentation **New: `agents-docs/content/docs/self-hosting/configure-runtime-limits.mdx`** - Comprehensive guide on runtime configuration - Concrete use case: "Long-Running Orchestration Agents" - Explains configuration hierarchy (API vs environment limits) - Links to related documentation (stopWhen, environment config) - Embeds `.env.example` source directly using remark-source-code **New: `agents-docs/_snippets/self-hosting/runtime-limits-note.mdx`** - Reusable callout snippet for deployment guides **Updated deployment guides:** - `docker-local.mdx` - `aws-ec2.mdx` - `gcp-compute-engine.mdx` - `hetzner.mdx` All now include runtime configuration callout with link to main docs. **Updated: `.env.example`** - Added comprehensive RUNTIME CONFIGURATION section (lines 71-173) - Documented all 56 environment variables with categories and defaults - All commented out by default (optional overrides) ### Code Updates Updated 17 files to import and use constants from centralized location: - `agents-run-api/src/handlers/executionHandler.ts` - `agents-run-api/src/agents/Agent.ts` - `agents-run-api/src/tools/NativeSandboxExecutor.ts` - `agents-run-api/src/tools/VercelSandboxExecutor.ts` - `agents-run-api/src/agents/ToolSessionManager.ts` - `agents-run-api/src/agents/relationTools.ts` - `agents-run-api/src/a2a/client.ts` - `agents-run-api/src/services/AgentSession.ts` - `agents-run-api/src/services/IncrementalStreamParser.ts` - `agents-run-api/src/utils/stream-helpers.ts` - `agents-run-api/src/data/conversations.ts` - `packages/agents-core/src/client-exports.ts` - `packages/agents-core/src/validation/schemas.ts` - `packages/agents-core/src/utils/mcp-client.ts` - `packages/agents-core/src/index.ts` Replaced hard-coded values with imports from `constants/runtime.ts`. ## Benefits 1. **Deployment Flexibility**: Operators can tune behavior for their specific use cases (long-running agents, resource-constrained environments, etc.) 2. **No Code Changes Required**: All tuning via environment variables 3. **Type-Safe**: Zod validation ensures correct types at startup 4. **Single Source of Truth**: Constants file remains authoritative for defaults 5. **Backward Compatible**: No breaking changes - all defaults preserved 6. **Well Documented**: Comprehensive docs with real-world examples 7. **Developer-Friendly**: Clear naming conventions (`AGENTS_` prefix) ## Use Case Example For long-running coding orchestration agents: \`\`\`bash AGENTS_EXECUTION_TRANSFER_COUNT_DEFAULT=50 AGENTS_SUB_AGENT_TURN_GENERATION_STEPS_DEFAULT=30 AGENTS_LLM_GENERATION_FIRST_CALL_TIMEOUT_MS_STREAMING=600000 AGENTS_FUNCTION_TOOL_EXECUTION_TIMEOUT_MS_DEFAULT=120000 \`\`\` ## Testing - ✅ All packages build successfully - ✅ Documentation builds without errors - ✅ TypeScript compilation passes - ✅ No breaking changes to existing behavior 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Update test expectations from 10 to 50 for VALIDATION_PAGINATION_DEFAULT_LIMIT to align with the new default value in runtime constants.
Update test input pages from 10 to 2 to match the calculation with the new default limit of 50 (100 total / 50 limit = 2 pages)
Add SESSION_TOOL_RESULT_CACHE_TIMEOUT_MS and SESSION_CLEANUP_INTERVAL_MS to @inkeep/agents-core mocks in test files that were failing.
🦋 Changeset detectedLatest commit: d0c34af The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
|
|
Claude finished @nick-inkeep's task —— View job Comprehensive Code Review: Runtime Configuration Overrides FeatureSummaryThis is a well-architected feature that adds environment variable overrides for 56+ runtime configuration constants. The implementation follows good practices but has one critical gap that prevents the feature from working as intended. 🚨 Critical Issue: Missing Runtime ImplementationThe most significant finding is that while environment parsing is implemented, the actual runtime code doesn't use the parsed configuration. Problem// Current: Agent.ts, ToolSessionManager.ts, etc. still do this:
import { FUNCTION_TOOL_EXECUTION_TIMEOUT_MS_DEFAULT } from '@inkeep/agents-core';
// But should do this to respect env overrides:
import { runtimeConfig } from '../env';
// then use: runtimeConfig.FUNCTION_TOOL_EXECUTION_TIMEOUT_MS_DEFAULTImpactEnvironment variables are parsed but completely ignored by runtime execution code. This means the core feature doesn't work despite all the infrastructure being in place. Files Needing Updates
Architecture & Design Review✅ Strengths
|
This fixes a critical bug where environment variable overrides were parsed but never actually used by the runtime execution code. Changes: - Refactored agents-core to bundle constants in runtimeConsts object - Added missing AGENTS_VALIDATION_PAGINATION_DEFAULT_LIMIT to manage-api - Updated 10+ runtime files to import runtimeConfig from env.ts instead of importing constants directly from agents-core - Environment variables documented in .env.example now actually work Affected packages: - @inkeep/agents-core - @inkeep/agents-manage-api - @inkeep/agents-run-api Files modified: - packages/agents-core/src/constants/runtime.ts - agents-manage-api/src/env.ts - agents-run-api/src/env.ts - agents-run-api/src/agents/Agent.ts - agents-run-api/src/agents/ToolSessionManager.ts - agents-run-api/src/agents/relationTools.ts - agents-run-api/src/a2a/client.ts - agents-run-api/src/services/AgentSession.ts - agents-run-api/src/utils/stream-helpers.ts - agents-run-api/src/services/IncrementalStreamParser.ts - agents-run-api/src/data/conversations.ts
The integration test was mocking ../../env.js but only exporting 'env', not the new 'runtimeConfig' export that was added. This caused test failures after updating runtime files to import runtimeConfig from env. Updated the mock to import runtimeConsts from @inkeep/agents-core and export it as runtimeConfig.
Fixed critical issue where pagination constants were hardcoded in route implementations instead of using runtimeConfig, causing environment variable overrides to be ignored. Changes: - Updated 10 route files to import and use runtimeConfig for pagination - Changed default limit from hardcoded 10 to runtimeConfig.VALIDATION_PAGINATION_DEFAULT_LIMIT (50) - Changed max limit from hardcoded 100 to runtimeConfig.VALIDATION_PAGINATION_MAX_LIMIT (100) - Updated test expectations from 10 to 50 for default pagination - Removed runtime constants section from root .env.example (reduces noise) Affected routes: - projects.ts - subAgents.ts - agent.ts - apiKeys.ts - artifactComponents.ts - contextConfigs.ts - credentials.ts - dataComponents.ts - subAgentRelations.ts - subAgentExternalAgentRelations.ts All tests passing (21/21 in projects.test.ts) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
## Problem Phase 2 (structured output generation) was not enforcing the LLM_GENERATION_MAX_ALLOWED_TIMEOUT_MS (10 minute) limit. Users could configure excessively long timeouts (e.g., 700 seconds = 11.6 minutes) that would bypass the maximum, potentially causing extended hangs. ## Solution - Added timeout capping logic for Phase 2, mirroring Phase 1's implementation - Cap user-configured maxDuration to MAX_ALLOWED (600,000ms = 10 minutes) - Added warning log when Phase 2 timeout is capped - Ensures consistency between Phase 1 and Phase 2 timeout enforcement ## Changed Files - agents-run-api/src/agents/Agent.ts:2158-2180 - Replaced simple timeout assignment with proper capping logic - Added double-cap for defensive programming - Added warning log with phase='structured_generation' context ## Impact - Prevents potential 10+ minute hangs in Phase 2 generation - Provides user feedback when configured timeout exceeds limit - Consistent behavior across all LLM generation phases 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…n-limits
Split the monolithic runtime.ts into two separate modules for better separation of concerns:
1. **Schema Validation Constants** (`packages/agents-core/src/constants/schema-validation/`)
- Used in Zod validation schemas across manage-api and run-api
- Includes pagination, prompt limits, status updates, etc.
- 18 constants exported individually for clean imports
2. **Execution Limits Constants** (`agents-run-api/src/constants/execution-limits/`)
- Runtime behavior limits for agent execution
- 44 constants for timeouts, retries, buffers, etc.
- Only created structure for future use
**Key Improvements:**
- DRY: Auto-generate Zod schemas from defaults object using Object.keys()
- Direct imports: `import { CONSTANT } from '@inkeep/agents-core/constants/schema-validation'`
- Environment override support: All constants can be overridden via `AGENTS_*` env vars
- Type-safe: Full TypeScript type inference with `as const`
- Removed 500 lines of code overall
**Files Changed:**
- Deleted: `packages/agents-core/src/constants/runtime.ts` (495 lines)
- Created: schema-validation module (2 files, 104 lines)
- Created: execution-limits module structure (2 files, 160 lines)
- Updated: 10 manage-api route files to use direct imports
- Updated: 2 manage-api test files
- Updated: 3 agents-core files (mcp-client.ts, validation/schemas.ts, client-exports.ts)
- Cleaned: Both env.ts files (removed runtimeConfig sections)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Updated all agents-run-api files to import from local execution-limits instead of runtimeConfig or @inkeep/agents-core. Files updated: - 12 source files migrated from runtimeConfig to direct imports - All execution-limit constants now imported from ./constants/execution-limits - Removed all remaining references to deleted runtime.ts This completes the runtime constants refactoring started in the previous commit.
Updated client-exports.ts and validation/schemas.ts to import from defaults.ts instead of index.ts to avoid Node.js-specific modules (locate-path, find-up) being bundled in browser code. This fixes the Next.js build errors in agents-manage-ui where webpack couldn't handle node:fs, node:os, node:process, and node:url imports.
After merging with main, the test needed loadEnvironmentFiles in the @inkeep/agents-core mock since execution-limits/index.ts now uses it.
This commit improves the documentation for all configurable constants by: - Using natural language descriptions that explain runtime behavior and impact - Avoiding references to specific code implementation details - Making the purpose and usage of each constant clear to developers familiar with the Inkeep concepts Key changes: 1. Renamed DATA_COMPONENT_FETCH_TIMEOUT_MS_DEFAULT → CONTEXT_FETCHER_HTTP_TIMEOUT_MS_DEFAULT - The original name incorrectly suggested this was for Data Components (UI elements) - The constant is actually used for Context Fetcher HTTP request timeouts - Updated all references throughout the codebase 2. Improved documentation for schema validation constants: - AGENT_EXECUTION_TRANSFER_COUNT: Explains transfer control limits between agents - SUB_AGENT_TURN_GENERATION_STEPS: Clarifies AI generation step limits per turn - STATUS_UPDATE_MAX_*: Describes event and time-based trigger thresholds - VALIDATION_*_PROMPT_MAX_CHARS: Explains prompt length limits during configuration - CONTEXT_FETCHER_HTTP_TIMEOUT_MS_DEFAULT: Describes HTTP timeout for external data fetching 3. Improved documentation for execution limits constants: - MCP_TOOL_*: Explains connection timeouts and exponential backoff retry strategy - CONVERSATION_HISTORY_MAX_OUTPUT_TOKENS_DEFAULT: Clarifies token limit for LLM context window All documentation now focuses on "what it affects" rather than "where it's used", making it more maintainable and comprehensible to self-hosting developers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This commit updates the self-hosting documentation to reflect the new constants architecture implemented in this PR. Key changes: 1. Added new section "Understanding the Constants Architecture": - Explains the two types of constants (schema validation vs runtime execution) - Points to TypeScript constant files as the source of truth (not .env.example) - Documents that each constant has detailed inline documentation - Provides examples of how to override constants via environment variables 2. Updated "All Available Configuration Variables" section: - Removed outdated reference to .env.example as "source of truth" - Reorganized categories to distinguish schema validation vs runtime execution constants - Updated "Data Components" → "Context Fetchers" to reflect the renamed constant - Maintains conceptual overview without enumerating individual constants The documentation now accurately reflects the codebase structure where: - Schema validation constants are in constants/schema-validation/defaults.ts - Runtime execution constants are in constants/execution-limits-shared/defaults.ts - Code is the source of truth with comprehensive inline documentation - Developers can explore constants directly in TypeScript files 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
These MCP connection and retry constants were duplicated from agents-core where they're already defined in execution-limits-shared. The run-api versions were unused dead code. Changes: - Removed 5 duplicate MCP constants from run-api execution limits - Kept only MCP_TOOL_REQUEST_TIMEOUT_MS_DEFAULT (run-api specific) - Updated documentation to clarify run-api specific constants - Added note pointing to agents-core for MCP connection/retry constants
Added explicit documentation for the third constants location (run-api specific). This completes the three-tier constants architecture explanation: 1. Schema Validation Constants (agents-core) 2. Runtime Execution Constants - Shared (agents-core) 3. Runtime Execution Constants - Run-API (agents-run-api) Changes: - Added "Runtime Execution Constants - Run-API" section to architecture explanation - Added run-api constants path to "All Available Configuration Variables" - Renamed "Additional Runtime Settings" to "Runtime Execution Constants - Run-API" - Made it clear which constants live in run-api vs shared locations
Replaced hardcoded conversation history limits in status update generation with shared configurable constants to ensure consistency across the codebase. Changes: - Added CONVERSATION_HISTORY_DEFAULT_LIMIT (50) to execution-limits-shared - Exported CONVERSATION_HISTORY_DEFAULT_LIMIT from agents-core - Updated AgentSession to use both shared constants: - CONVERSATION_HISTORY_DEFAULT_LIMIT (was hardcoded to 10) - CONVERSATION_HISTORY_MAX_OUTPUT_TOKENS_DEFAULT (was hardcoded to 2000) Benefits: - Single source of truth for conversation history limits - Now respects environment variable overrides (AGENTS_CONVERSATION_HISTORY_*) - Consistent behavior across all conversation history operations
Enhanced inline documentation for all runtime execution constants to make them more understandable for developers familiar with Inkeep concepts (Agents, Sub Agents, Tools, Delegation, Artifacts, Status Updates, etc.). Changes: - Sub Agent Turn Execution: Added detailed explanation of generation steps and error handling - LLM Timeouts: Reframed as "Sub Agent Decision-Making" with clear first/subsequent call distinction - Function Tools: Explained sandboxed environments, pooling strategy, and resource limits - MCP Tools: Clarified external service integration - Delegation: Distinguished from general A2A communication, explained exponential backoff - A2A Communication: Clarified broader protocol operations beyond delegation - Artifacts: Explained automatic name/description generation and context management - Session Management: Introduced "session" concept and cache rationale - Status Updates: Explained event-based triggers and automatic generation - Streaming: Clarified internal buffering for Data Components and real-time updates - Conversation History: Added clear description All descriptions now: - Map to Inkeep user-facing concepts (not just implementation details) - Explain technical terms (sandbox, backoff, TTL, VCPUS, etc.) - Describe behavioral impact (what happens when limits are exceeded) - Maintain consistent documentation pattern 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Summary
This PR refactors runtime configuration constants into a well-organized three-tier architecture and adds support for environment variable overrides, enabling users to customize execution behavior for long-running agents and custom deployment scenarios without code changes.
Motivation
Users deploying agents in production environments need the ability to:
Previously, configuration constants were scattered across the codebase with inconsistent organization, making it difficult to understand which constants could be overridden and how they affected runtime behavior.
Changes
1. Three-Tier Constants Architecture
Reorganized all runtime configuration constants into three logical categories based on their purpose and scope:
Schema Validation Constants (
packages/agents-core/src/constants/schema-validation/)AGENTS_*prefixFiles:
defaults.ts- Default values with inline documentationindex.ts- Zod validation and environment variable parsingRuntime Execution Constants - Shared (
packages/agents-core/src/constants/execution-limits-shared/)AGENTS_*prefixFiles:
defaults.ts- Default values with inline documentationindex.ts- Zod validation and environment variable parsingRuntime Execution Constants - Run-API (
packages/agents-run-api/src/constants/execution-limits/)AGENTS_*prefixFiles:
defaults.ts- Default values with inline documentationindex.ts- Zod validation and environment variable parsing2. Consistent Constant Usage
Fixed Inconsistent Hardcoded Values:
AgentSession.tsfrom usinglimit: 10andmaxOutputTokens: 2000to using shared constants:CONVERSATION_HISTORY_DEFAULT_LIMIT(50)CONVERSATION_HISTORY_MAX_OUTPUT_TOKENS_DEFAULT(4000)Benefits:
AGENTS_CONVERSATION_HISTORY_*)3. Environment Variable Support
All constants across all three tiers support environment variable overrides:
Pattern:
AGENTS_<CONSTANT_NAME>Examples:
Implementation:
4. Comprehensive Documentation
New Documentation Page
File:
agents-docs/content/docs/self-hosting/configure-runtime-limits.mdxComprehensive guide including:
.envconfigurationUpdated Documentation
.env.example)5. Test Improvements
Test Mock Pattern Enhancement
Updated test files to use
importOriginal()pattern instead of manually specifying constants:Files Updated:
agents-run-api/src/__tests__/agents/generateTaskHandler.test.tsagents-run-api/src/__tests__/routes/integration.test.tsPagination Default Update:
VALIDATION_PAGINATION_DEFAULT_LIMITfrom 10 to 50packages/agents-core/src/__tests__/validation/schemas.test.tsExample Usage
For Long-Running Orchestration Agents
Configuration Hierarchy
When an agent executes:
stopWhensettings if configured (via manage-api)AGENTS_*env vars)Architecture Benefits
Clear Separation of Concerns
Single Source of Truth
defaults.tswith comprehensive inline documentationEnvironment Variable Overrides
AGENTS_*prefix overridesMaintainability
importOriginal()to automatically get real constant valuesTesting
importOriginal()for better maintainabilityFiles Changed
Constants Architecture
packages/agents-core/src/constants/schema-validation/defaults.ts(NEW)packages/agents-core/src/constants/schema-validation/index.ts(NEW)packages/agents-core/src/constants/execution-limits-shared/defaults.ts(NEW)packages/agents-core/src/constants/execution-limits-shared/index.ts(NEW)agents-run-api/src/constants/execution-limits/defaults.ts(NEW)agents-run-api/src/constants/execution-limits/index.ts(NEW)Consistent Usage
agents-run-api/src/services/AgentSession.ts(use shared constants)Documentation
agents-docs/content/docs/self-hosting/configure-runtime-limits.mdx(NEW - comprehensive guide)Tests
packages/agents-core/src/__tests__/validation/schemas.test.ts(pagination default updated)agents-run-api/src/__tests__/agents/generateTaskHandler.test.ts(improved mock pattern)agents-run-api/src/__tests__/routes/integration.test.ts(improved mock pattern)Migration Guide
No migration required - this is a purely additive feature. All constants retain their existing default values, so existing deployments continue to work unchanged. Users can optionally add
AGENTS_*environment variables to customize behavior.Related
🤖 Generated with Claude Code