polyclaw v5.0.0

Services Layer

The services layer provides infrastructure management, secret handling, and external integrations.

Tunnel Service

Module: app/runtime/services/tunnel.py

Manages a Cloudflare quick-tunnel subprocess to expose local endpoints publicitly.

FeatureDescription
Auto-startLaunched during server startup
URL detectionParses tunnel URL from subprocess output
Health monitoringDetects tunnel disconnections
Restricted modeTUNNEL_RESTRICTED=true limits access to bot/voice endpoints

The tunnel URL is used as the Bot Framework messaging endpoint and ACS callback URL.

Key Vault Service

Module: app/runtime/services/keyvault.py

Integrates with Azure Key Vault for secret management.

FeatureDescription
@kv: resolutionSecrets prefixed with @kv:secret-name are resolved at startup
Write-backSettings.write_env() stores secrets in Key Vault and writes @kv: references
Firewall allowlistingAutomatically adds current IP to Key Vault firewall rules
Credential chainUses AzureCliCredential or DefaultAzureCredential

Provisioner

Module: app/runtime/services/provisioner.py

Orchestrates the infrastructure lifecycle for a configured bot:

MethodDescription
provision()Register Entra ID app and provision a scoped runtime identity
recreate_endpoint()Update the bot messaging endpoint when the tunnel URL changes
decommission()Delete the bot registration and revoke the runtime identity
status()Return current provisioning state from the deploy store

Channel configuration (Telegram) is applied as part of provision() when a token is present in the infra config. Teams is enabled automatically via the bot resource itself.

Bot Deployer

Module: app/runtime/services/deployer.py

BotDeployer manages the full Azure Bot resource lifecycle:

MethodDescription
deploy()Create resource group, register Entra ID app, generate credentials, and register the Azure Bot
register_app()Register the Entra ID app and bot resource without creating infrastructure
recreate()Update the bot messaging endpoint and re-register credentials
delete()Remove the Azure Bot resource and optionally the resource group

ACA Deployer

Module: app/runtime/services/aca_deployer.py

AcaDeployer deploys the full Polyclaw stack to Azure Container Apps:

FeatureDescription
Image build & pushBuilds polyclaw:latest and pushes to a provisioned Azure Container Registry
ACA environmentCreates the Container Apps environment with workload profile
Runtime appDeploys the runtime container app with CPU/memory limits and ingress
Managed identityCreates and assigns polyclaw-runtime-mi with Bot Contributor, Reader, and Session Executor roles
IP allowlistingAdds the deployer’s public IP to the Key Vault and ACR firewall
Deploy stateRecords each deployment in DeployStateStore for idempotent re-runs

Key operations: deploy(req), destroy(deploy_id), status(), restart().

Runtime Identity Provisioner

Module: app/runtime/services/runtime_identity.py

RuntimeIdentityProvisioner provisions and revokes the scoped identity the agent runtime uses to interact with Azure:

StrategyDescription
Service principalUsed in Docker Compose deployments; credentials written to /data/.env as RUNTIME_SP_* keys
Managed identityUsed in ACA deployments; polyclaw-runtime-mi attached to the container app

RBAC roles granted (scoped to the resource group):

RolePurpose
Azure Bot Service ContributorCreate/update/delete the Bot Service registration
ReaderEnumerate resources in the resource group
Key Vault Secrets OfficerRead/write bot credentials stored in Key Vault
Azure ContainerApps Session ExecutorInvoke ACA Dynamic Sessions for code execution

Key operations: provision(resource_group), revoke(), provision_managed_identity(), revoke_managed_identity(), status().

Azure CLI Wrapper

Module: app/runtime/services/azure.py

Wraps az CLI commands for:

  • Bot creation and deletion
  • Channel management (Teams, Telegram)
  • Resource group operations
  • Subscription queries

OpenTelemetry Service

Module: app/runtime/services/otel.py

Bootstraps Azure Monitor distributed tracing via the OpenTelemetry SDK:

FeatureDescription
configure_otel()Initialises the Azure Monitor distro with a connection string and sampling ratio
Agent spansagent_span() / invoke_agent_span() context managers wrap agent invocations
Event recordingrecord_event() emits custom span events; set_span_attribute() annotates the active span
Graceful initMonitoring is optional – a missing connection string or import error never blocks startup
Noisy logger suppressionAzure SDK HTTP and identity loggers are quieted to WARNING

Prompt Shield Service

Module: app/runtime/services/prompt_shield.py

PromptShieldService calls the Azure AI Content Safety Prompt Shields API to detect prompt injection attacks in tool arguments before execution:

FeatureDescription
AuthenticationDefaultAzureCredential with https://cognitiveservices.azure.com/.default scope; API keys are never used
Result typeShieldResult(attack_detected, mode, detail) frozen dataclass
Opt-inService is a no-op when no Content Safety endpoint is configured

Security Preflight Checker

Module: app/runtime/services/security_preflight.py

SecurityPreflightChecker runs verifiable runtime security checks and produces a structured PreflightResult:

Check categoryWhat is verified
Azure loginActive az login session exists
Identity configuredRUNTIME_SP_* or managed identity env vars are present
Identity validService principal or MI is resolvable and not expired
RBAC rolesRuntime identity holds expected roles and no elevated roles (Owner, Contributor, etc.)
RBAC scopeRole assignments are scoped to the correct resource group, not subscription-wide
Secret isolationAdmin CLI credentials are not accessible from the runtime container
Bot credentialsMicrosoftAppId / MicrosoftAppPassword are set and non-empty
Key Vault reachabilityKey Vault endpoint responds to a token-authenticated probe

Every check executes a real command or environment inspection – no static claims.

Other Services

ModulePurpose
github.pyGitHub API integration
foundry_iq.pyAzure AI Foundry IQ indexing and search
resource_tracker.pyAzure resource tracking and cost awareness
misconfig_checker.pyConfiguration auditing and validation