Skip to content

Latest commit

 

History

History
138 lines (89 loc) · 5.6 KB

File metadata and controls

138 lines (89 loc) · 5.6 KB

ResourceManager – Future / Optional Features

Deferred and optional features that can be implemented for the resource control subsystem. The current system is production-ready without these; they are enhancements for specific use cases.


1. TTL on Redis Keys

Status: Deferred

Description:
Automatic expiry of Redis keys (afw:res:*, afw:circuit:*) after a configurable time-to-live. Keys would be removed by Redis when they are idle for longer than the TTL, reducing manual cleanup for long-lived deployments.

Current workaround:
Use cleanup_orphan_keys() after policy removal or renames. Call remove_resource_policy_and_sync() when removing policies.

Implementation outline:

  • Add ttl_seconds: float | None = None to ResourcePolicy (None = no TTL)
  • In _LUA_INIT_KEY, after HSETNX: if ttl_seconds > 0, call redis.call('EXPIRE', k, ttl_seconds)
  • On each acquire / release / _lua_on_success etc., refresh TTL: redis.call('EXPIRE', k, ttl_seconds) so active keys do not expire
  • Circuit keys: same pattern in circuit Lua scripts

Files: resource_policy.py, resource_manager.py (Lua scripts, acquire/release paths)


2. Redis Authentication

Status: Not implemented

Description:
Support for Redis instances that require a password (and optionally a username). Useful for managed Redis (ElastiCache, Redis Cloud, etc.).

Implementation outline:

  • Add redis_password: str | None = None, redis_username: str | None = None to ResourceManager.__init__ and init_resource_manager
  • Pass to aioredis.ConnectionPool via password and username kwargs
  • Add REDIS_PASSWORD, REDIS_USERNAME env var support in AgentFramework and worker
  • Include in redis_config broadcast to workers

Files: resource_manager.py, aframework/aframework.py, worker/worker.py


3. Redis URL Support

Status: Not implemented

Description:
Accept a single redis_url (e.g. redis://user:pass@host:6379/0) instead of separate host/port/db/password parameters. Simplifies configuration for cloud deployments.

Implementation outline:

  • Add redis_url: str | None = None to ResourceManager.__init__
  • If redis_url is set, use aioredis.from_url(redis_url) instead of ConnectionPool(host=..., port=..., ...)
  • Precedence: redis_url over host/port/db when both provided
  • Add REDIS_URL env var support

Files: resource_manager.py, aframework/aframework.py, worker/worker.py


4. Prometheus Label Escaping

Status: Not implemented

Description:
Resource keys containing double quotes (") or backslashes (\) can produce invalid Prometheus exposition format. The current PrometheusMetricsBackend does not escape these characters in label values.

Impact: Low. Resource keys are typically alphanumeric with colons (e.g. openai:gpt-4). Only affects deployments with unusual key names.

Implementation outline:

  • In PrometheusMetricsBackend.format_snapshot, escape label values per Prometheus text format: " → \", \ → \\, newline → \n
  • Apply to resource label and any other user-provided labels

Files: engine/metrics_backend.py


5. Queue Stats in MetricsBackend

Status: Reserved for future use

Description:
The MetricsBackend.format_snapshot signature includes queue_stats: dict[str, Any] | None = None, but it is not used. Intended for per-worker queue depth, backlog, etc.

Implementation outline:

  • Define a standard shape for queue_stats (e.g. {"worker_0": {"queue_depth": 5, "backlog": 10}, ...})
  • Wire queue metrics from the dispatch/routing layer into get_metrics_for_pull()
  • Extend PrometheusMetricsBackend to emit queue-related metrics

Files: engine/metrics_backend.py, aframework/aframework.py, routing/dispatch code


6. Per-Resource Metrics Backend Override

Status: Not implemented

Description:
Allow different metrics backends per resource (e.g. Prometheus for openai, OpenTelemetry for mcp:browser). Useful when different teams own different resources and have different observability stacks.

Implementation outline:

  • Add optional metrics_backend: MetricsBackend | None to ResourcePolicy
  • In run_with_retry, when recording events, resolve backend: policy-specific or global
  • Requires record_event to be called per-key with the appropriate backend

Files: resource_policy.py, resource_manager.py


7. Sliding Window Rate Limiter

Status: Not implemented

Description:
Alternative to token bucket: a sliding-window rate limiter that enforces “at most N requests per window W”. Some APIs (e.g. OpenAI) use this model.

Implementation outline:

  • Add algorithm: "sliding_window" to ResourcePolicy
  • New Lua script: maintain a sorted set or list of timestamps, trim old entries, check count before allowing
  • More Redis operations per request than token bucket; consider trade-offs

Files: resource_policy.py, resource_manager.py (new Lua script, algo constant)


Summary

# Feature Priority Effort
1 TTL on Redis keys Medium Medium
2 Redis authentication High* Low
3 Redis URL support Medium Low
4 Prometheus label escaping Low Low
5 Queue stats in metrics Low Medium
6 Per-resource metrics Low Medium
7 Sliding window rate limit Low High

* High if using managed Redis with auth; N/A if using local Redis without auth.