This document defines the critical end-to-end interactions between ratelord components. It maps the static structures (Architecture, Agent Contract, Policy Engine) into dynamic processes.
All workflows adhere to the Daemon Authority and Event Sourcing principles.
This is the fundamental interaction loop that runs for every constrained action. It enforces the "Ask Before You Act" mandate.
sequenceDiagram
participant Agent as Agent/Client
participant Daemon as Daemon (ratelord-d)
participant Engine as Policy/Prediction
participant Provider as External API
Note over Agent: Wants to perform action
Agent->>Daemon: Submit Intent (AgentID, Identity, Scope)
Note over Daemon: 1. Hydrate Context
Daemon->>Daemon: Load Identity State & Forecasts
Note over Daemon: 2. Evaluate
Daemon->>Engine: Check Policy(Intent, Forecasts)
Engine-->>Daemon: Verdict (Approve/Mod/Deny)
Note over Daemon: 3. Commit
Daemon->>Daemon: Log event: intent_decision
Daemon-->>Agent: Return Decision
alt Approved
Agent->>Provider: Execute Action
Agent->>Daemon: (Optional) Report Usage
else Modified (Wait)
Agent->>Agent: Sleep(wait_seconds)
Agent->>Provider: Execute Action
else Denied
Agent->>Agent: Handle Rejection (Retry/Fail)
end
-
Submission:
- The Agent constructs an Intent identifying who (AgentID, Identity), what (Workload), and where (Scope).
- It sends this intent to the Daemon via the Unix Socket API (
POST /intent).
-
Daemon Evaluation:
- Context Loading: The Daemon retrieves the current state of the requested
IdentityandScope(current usage, remaining budget). - Forecasting: The Prediction Engine calculates the
P50andP99time-to-exhaustion if this intent proceeds. - Policy Check: The Policy Engine evaluates rules against the Intent + Forecasts.
- Example Rule: "Deny if P99 exhaustion < 10 mins and Urgency != High".
- Context Loading: The Daemon retrieves the current state of the requested
-
Decision & Commitment:
- The Daemon generates a Decision:
Approve,ApproveWithModifications, orDeny. - CRITICAL: The Decision is appended to the Event Log (
intent_decision) before returning to the Agent. This ensures the ledger is the source of truth.
- The Daemon generates a Decision:
-
Agent Execution:
- The Agent receives the Decision.
- If
wait_seconds > 0, the Agent sleeps. - If
Approve, the Agent calls the external Provider. - If
Deny, the Agent aborts the call.
This workflow registers a new set of credentials (e.g., a GitHub Personal Access Token) with the system, establishing its scope and initial state.
sequenceDiagram
participant Operator as Human/Config
participant Daemon as Daemon
participant Provider as External API
Operator->>Daemon: Register Identity (Token, Type, Owner)
Note over Daemon: Validate & Store Secret
Daemon->>Daemon: Encrypt Token -> Secure Storage
Daemon->>Daemon: Log event: identity_registered
Note over Daemon: Initial Discovery
Daemon->>Provider: Poll Limits (GET /rate_limit)
Provider-->>Daemon: Quota Status (Limit, Remaining, Reset)
Note over Daemon: Baseline State
Daemon->>Daemon: Log event: limits_polled
Daemon->>Daemon: Log event: provider_state_initialized
Note over Daemon: Ready
Daemon-->>Operator: Acknowledged (ID: pat:123)
-
Registration:
- Operator uses the CLI or TUI to add an identity:
ratelord identity add --type=github_pat --token=... --scope=org:my-org. - Security: The Daemon receives the token, encrypts it (or stores a reference to an env var), and never logs the raw token.
- Operator uses the CLI or TUI to add an identity:
-
Scope Assignment:
- The Daemon associates the Identity with specific Constraint Pools based on the provider type (e.g., GitHub PATs map to
core,search,graphql).
- The Daemon associates the Identity with specific Constraint Pools based on the provider type (e.g., GitHub PATs map to
-
Initial Polling:
- The Daemon immediately performs a Poll against the Provider to establish the baseline.
- It records the absolute values (
limit,remaining,reset_time) in the Event Log. - Note: Without this step, the Daemon cannot calculate burn rates or approve intents.
How the system detects and mitigates imminent or actual budget exhaustion. This illustrates Time-Domain Reasoning.
sequenceDiagram
participant Daemon as Daemon
participant Engine as Policy Engine
participant Agents as All Agents
loop Background Loop
Daemon->>Daemon: Update Forecasts
end
Note over Daemon: Detects Risk
Daemon->>Daemon: P99 Time-to-Empty < 5 mins
Note over Daemon: Trigger Policy
Daemon->>Engine: Evaluate System Health
Engine-->>Daemon: Transition -> "Defcon 2" (Throttle)
Daemon->>Daemon: Log event: policy_state_change
Note over Agents: Next Intents
Agents->>Daemon: Submit Intent (Urgency: Low)
Daemon-->>Agents: Deny (Reason: Shedding Load)
Agents->>Daemon: Submit Intent (Urgency: High)
Daemon-->>Agents: Approve (Critical Path Only)
-
Detection (Predictive):
- The Prediction Engine constantly updates the "Time to Exhaustion" (TTE) for active identities.
- Trigger: A forecast (e.g.,
P99 TTE) drops below a safety threshold (e.g.,reset_window_remaining).
-
Alerting & State Change:
- The Daemon emits a
risk_alertevent. - Global or Scope-level variables are updated (e.g.,
system_statuschanges fromOKtoWARNING).
- The Daemon emits a
-
Throttling (Reactive Defense):
- Policy Activation: Policies referencing
system_status == WARNINGbecome active. - Load Shedding: Low-urgency intents (
background,analytics) are summarily Denied to save remaining budget for critical tasks. - Rate Limiting: High-urgency intents might receive
ApproveWithModifications(forced waits) to flatten the curve.
- Policy Activation: Policies referencing
-
Recovery:
- As the reset window approaches or load decreases, TTE increases.
- When TTE > Safe Threshold, the system reverts to
OK.
How the operator changes the rules of engagement without restarting the system.
- Edit: Operator modifies
policy.yaml(e.g., changing a reserve buffer from 10% to 20%). - Signal: Operator sends
SIGHUPto the Daemon or runsratelord reload. - Ingest:
- Daemon parses the new policy file.
- Daemon validates the syntax (rejects if invalid, keeping old policy).
- Apply:
- Daemon logs
policy_updatedevent. - The very next Intent processed uses the new rules. No restart is required.
- Daemon logs
Since external providers are the ultimate source of truth, the Daemon must periodically sync to correct drift (drift = difference between local prediction and remote reality).
- Schedule: The Daemon maintains a polling schedule per Identity (adaptive based on load; higher load = more frequent polls).
- Execute: Daemon calls the Provider's "Rate Limit Status" endpoint.
- Reconcile:
- Daemon compares
provider.remainingvs.local.estimated_remaining. - Drift Event: If significant drift is found (> 5%), a
drift_detectedevent is logged. - Correction: The local state is hard-reset to the Provider's values.
- Daemon compares
- Learn: The Prediction Engine uses the drift magnitude to adjust the uncertainty variance for future forecasts.
- Daemon is Passive-Aggressive: It never initiates work for agents. It only judges work proposed by agents.
- Blocking is Feature: The Agent's
Waitstate is the primary mechanism for smoothing traffic. - Silence is Failure: If an agent acts without asking, the Daemon cannot govern it. (Future: Log scraping to detect unnegotiated actions).