Compare commits

..

66 Commits

Author SHA1 Message Date
Kwanghoon-Choi 901d041d56 . 2026-02-03 10:07:29 +00:00
Kwanghoon-Choi 2acb0b9864 clickhouse compl 2026-02-03 08:19:09 +00:00
Kwanghoon-Choi 23e3336016 . 2026-02-03 07:57:36 +00:00
Kwanghoon-Choi 88555bafdb . 2026-02-03 07:45:07 +00:00
Kwanghoon-Choi 9dbf3d362c . 2026-02-03 07:42:57 +00:00
Kwanghoon-Choi b6b16c1827 . 2026-02-03 07:32:46 +00:00
Kwanghoon-Choi 66ea11b9bb . 2026-02-03 07:15:53 +00:00
Kwanghoon-Choi d0a19cf5c9 . 2026-02-03 06:58:40 +00:00
Kwanghoon-Choi 23f7edb6d7 clickhouse debuggg 2026-02-03 06:33:04 +00:00
Kwanghoon-Choi 69e9b1c90e clickhouse debug 2026-02-03 06:29:36 +00:00
Kwanghoon-Choi 8ebc28702d comment out query_spans 2026-02-01 13:12:34 +00:00
Kwanghoon-Choi ccf5e3337d kafka evall 2026-01-27 11:03:31 +00:00
Kwanghoon-Choi 8c336201a4 kafka eval 2026-01-27 10:59:21 +00:00
Kwanghoon-Choi cb7edf48d3 kafka debugg 2026-01-27 09:06:04 +00:00
Kwanghoon-Choi 8c236b89ad kafka debug 2026-01-27 08:54:00 +00:00
Kwanghoon-Choi 9c7a6316f1 kafka max bytes test 2026-01-27 08:46:00 +00:00
Kwanghoon-Choi 01de392b29 kafka 2026-01-26 14:42:26 +00:00
Kwanghoon-Choi 35d5763c1f gh test 2026-01-19 05:32:18 +00:00
Kwanghoon-Choi 779a971a95 Merge remote-tracking branch 'upstream/main' 2026-01-16 07:43:27 +00:00
荔枝 bfb94a8750 Make APO templates configurable via constructor arguments (#443) 2026-01-12 16:20:40 +08:00
Yuge Zhang 25eda47a29 Fix broken links in changelog (#433) 2025-12-24 19:13:34 +08:00
Yuge Zhang a214474402 Bump to 0.3.1 (#431) 2025-12-24 11:45:50 +08:00
Yuge Zhang 3b5d733861 [Release] v0.3.0 (#427)
Deploy Documentation / deploy (push) Has been cancelled
PyPI Release / check-version (push) Has been cancelled
PyPI Release / publish-pypi (push) Has been cancelled
2025-12-24 09:46:58 +08:00
Yuge Zhang 158f5df28e Fix documentation and dashboard building issues (#429) 2025-12-23 23:55:59 +08:00
Yuge Zhang 40dc59205b Scale out benchmark parameters (#428) 2025-12-23 23:32:19 +08:00
Yuge Zhang c1a43b6c3a Update parallelization guides (#426) 2025-12-23 14:57:24 +08:00
Jiahang Xu 4235731a0d Feat: Add trace_aggregator to support both transition and trajectory aggregation (#134) 2025-12-22 11:12:21 +08:00
Yuge Zhang 22b80b38bf v0.3 Documentation Update (#422) 2025-12-18 01:12:50 +08:00
Yuge Zhang 9f178accaf Minor optimizations to store benchmark (#421) 2025-12-18 00:10:22 +08:00
Yuge Zhang 68a47d5087 Make weave import optional (#423) 2025-12-17 20:31:48 +08:00
Yuge Zhang 4b36b25aad Fix Weave get username (#420) 2025-12-17 13:00:24 +08:00
Wang Zilong a13e09fc6c add youtu agent blog link in community projects (#416) 2025-12-17 09:00:04 +08:00
Yuge Zhang e63c340ebd Benchmark minor improvements (#418) 2025-12-17 02:14:32 +08:00
Yuge Zhang e62b7ca252 Support Weave tracer in TracerTraceToTriplet (#415) 2025-12-17 00:15:31 +08:00
Jiahang Xu f66d87745f Adapt the Search R1 Example to AGL v0.2 (#412)
Co-authored-by: SiyunZhao <siyunzhao@microsoft.com>
2025-12-16 23:32:20 +08:00
Jiahang Xu 52090e9dd5 Update benchmark results to Search-R1 v0.1 (#417) 2025-12-16 23:28:10 +08:00
Yuge Zhang fdaf3f1777 Fix unsloth config issue (#414) 2025-12-16 10:41:42 +08:00
Yuge Zhang 087c7d350a Reimplement Weave tracer and unify emitter interface (#411) 2025-12-15 15:20:59 +08:00
Yuge Zhang 2203070ef0 Misc CI fixes and Move Search-R1 to contrib (#410) 2025-12-13 15:12:23 +08:00
jinghuan-Chen a6078caa6c fix TraceTree/match_rewards assign_to elements. (#403) 2025-12-13 00:25:54 +08:00
Yuge Zhang f1a8072546 Add ChartQA to catalog (#409) 2025-12-12 23:39:41 +08:00
Totoluo 60f9955606 Multi-modal example: ChartQA (#379)
Co-authored-by: Totoluo <52833580+Yingluo-momo@users.noreply.github.com>
Co-authored-by: Yuge Zhang <scottyugochang@gmail.com>
2025-12-12 17:22:35 +08:00
Yuge Zhang 1d199b21c7 Support customizing trainer and daemon in VERL (#407) 2025-12-12 13:31:30 +08:00
Yuge Zhang 5f62ecb6f4 Split vllm 0.10.2 from 0.11.0 (#394) 2025-12-12 12:10:53 +08:00
Yuge Zhang 1948c2ba6d Sunset HTTP tracer and Refactor tests (#402) 2025-12-11 23:57:23 +08:00
Yuge Zhang 1e36e660b1 Support with_llm_proxy and with_store in algorithms (#398) 2025-12-11 16:50:42 +08:00
Yuge Zhang 267b9936bb Support image urls export in TracerTraceToTriplets (#400) 2025-12-11 16:47:28 +08:00
Wang Zilong 14714ded2b add youtu-agent in community projects (#399) 2025-12-11 15:40:49 +08:00
Yuge Zhang c3f5cc7a39 Initialize contribution area (#396) 2025-12-10 23:46:06 +08:00
Yuge Zhang ee0fffd3a2 Upgrade tinker dependency (#393) 2025-12-10 23:23:43 +08:00
Yuge Zhang 94d1cd780e Store Benchmark - Part 6 (#388) 2025-12-10 21:11:29 +08:00
Vasu bbd5c2a30a fix: handle ref_in_actor flag for LoRA compatibility with verl 0.6.0 (#386) 2025-12-10 18:30:38 +08:00
Yuge Zhang c6f4e6c283 Add playground workflow (#391) 2025-12-10 18:27:21 +08:00
Ni Hao 8c504518bb add weave tracer (#277) 2025-12-09 18:59:11 +08:00
etsplz 337cce7fdc Fix redundant cancel tracebacks on ctrl+c (issue #343) (#370) 2025-12-08 17:18:07 +08:00
Yuge Zhang 42c63d7a01 Store Benchmark - Part 5 (#380) 2025-12-08 12:43:18 +08:00
Yuge Zhang 5ecd23792d Fix trainer dev warning (#378) 2025-12-08 00:43:56 +08:00
root 28b0895fe2 change logger.info for algorithm 2025-12-07 19:29:44 +09:00
Yuge Zhang ad89e173e1 Fix TracesTable story (#375) 2025-12-06 14:25:09 +08:00
Yuge Zhang feebaec24c Add AGENTS.md (#374) 2025-12-06 13:16:26 +08:00
root c39a84d12c add SIGINT/CancelledError handling tests 2025-12-05 21:29:05 +09:00
root 2bf90cda51 exception handling using asyncio.run() 2025-12-05 21:27:49 +09:00
root 26cc501233 add _run_with_sigint tests 2025-12-05 09:10:50 +09:00
root 5706da6908 add asyncio.CancelledError to runner/algorithm 2025-12-05 09:10:38 +09:00
root 2477c4bca1 route execute() through _run_with_sigint 2025-12-05 02:17:53 +09:00
root 03f08b422c add _run_with_sigint and SIGINT handling 2025-12-05 02:11:05 +09:00
175 changed files with 16483 additions and 7226 deletions
+29
View File
@@ -0,0 +1,29 @@
name: Badge - ChartQA
on:
workflow_run:
workflows:
- Examples - ChartQA
types: [completed]
workflow_dispatch:
permissions:
actions: read
contents: read
jobs:
badge:
if: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'workflow_run' && github.event.workflow_run.head_branch == 'main') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v8
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const badgeAggregation = require('./scripts/badge_aggregation.js');
const dependencies = [
{ workflow: 'examples-chartqa.yml', label: 'chartqa', variants: ['stable'] },
];
await badgeAggregation({ github, context, core, dependencies });
+4
View File
@@ -10,6 +10,8 @@ on:
- Examples - Tinker
- Examples - Azure
- Examples - Claude Code
- Examples - RAG
- Examples - ChartQA
types: [completed]
workflow_dispatch:
@@ -37,5 +39,7 @@ jobs:
{ workflow: 'examples-tinker.yml', label: 'examples-tinker.stable', variants: ['stable'] },
{ workflow: 'examples-azure.yml', label: 'examples-azure.stable', variants: ['stable'] },
{ workflow: 'examples-claude-code.yml', label: 'examples-claude-code.stable', variants: ['stable'] },
{ workflow: 'examples-rag.yml', label: 'examples-rag.stable', variants: ['stable'] },
{ workflow: 'examples-chartqa.yml', label: 'examples-chartqa.stable', variants: ['stable'] },
];
await badgeAggregation({ github, context, core, dependencies });
+4
View File
@@ -7,6 +7,8 @@ on:
- Examples - Spider
- Examples - APO
- Examples - Unsloth
- Examples - RAG
- Examples - Claude Code
- GPU Test
types: [completed]
@@ -32,6 +34,8 @@ jobs:
{ workflow: 'examples-spider.yml', label: 'spider.latest', variants: ['latest'] },
{ workflow: 'examples-apo.yml', label: 'apo.latest', variants: ['latest'] },
{ workflow: 'examples-unsloth.yml', label: 'unsloth.latest', variants: ['latest'] },
{ workflow: 'examples-claude-code.yml', label: 'claude-code.latest', variants: ['latest'] },
{ workflow: 'examples-rag.yml', label: 'rag.latest', variants: ['latest'] },
{ workflow: 'tests-full.yml', label: 'tests-full.latest', variants: ['latest'] },
];
await badgeAggregation({ github, context, core, dependencies });
+29
View File
@@ -0,0 +1,29 @@
name: Badge - RAG
on:
workflow_run:
workflows:
- Examples - RAG
types: [completed]
workflow_dispatch:
permissions:
actions: read
contents: read
jobs:
badge:
if: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'workflow_run' && github.event.workflow_run.head_branch == 'main') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v8
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const badgeAggregation = require('./scripts/badge_aggregation.js');
const dependencies = [
{ workflow: 'examples-rag.yml', label: 'rag', variants: ['legacy', 'stable'] },
];
await badgeAggregation({ github, context, core, dependencies });
+585 -198
View File
@@ -3,12 +3,15 @@ permissions:
contents: read
on:
workflow_dispatch:
schedule:
# Every Monday and Thursday at 3 AM UTC+8
- cron: '0 19 * * 0,3'
jobs:
benchmark:
name: Benchmark (${{ matrix.backend.id }}, ${{ matrix.scenario.display }})
runs-on: [self-hosted, 1ES.Pool=agl-runner-cpu]
timeout-minutes: 60
name: ${{ matrix.workload.kind }} (${{ matrix.backend.id }}, ${{ matrix.workload.display }}, ${{ matrix.trace_sink }})
runs-on: ${{ matrix.workload.runner }}
timeout-minutes: ${{ matrix.workload.timeout }}
strategy:
fail-fast: false
matrix:
@@ -17,61 +20,108 @@ jobs:
compose_file: compose.prometheus-memory-store.yml
- id: mongo
compose_file: compose.prometheus-mongo-store.yml
scenario:
- id: minimal-production
display: Minimal production scale
store_workers: 4
args: >-
--mode batch
--total-tasks 4096
--batch-size 256
--n-runners 32
--max-rounds 6
--sleep-seconds 0.5
- id: medium-production
display: Medium production scale
store_workers: 16
args: >-
--mode batch
--total-tasks 10000
--batch-size 1000
--n-runners 100
--max-rounds 10
--sleep-seconds 0.1
- id: large-batch
display: Large batch waves
store_workers: 32
args: >-
--mode batch
--total-tasks 100000
--batch-size 8192
--n-runners 256
--max-rounds 6
--sleep-seconds 0.1
- id: long-queues
trace_sink: [store, kafka, clickhouse]
# trace_sink: [clickhouse]
workload:
# - id: scenario-minimal-scale
# display: Minimal production scale
# kind: scenario
# store_workers: 4
# runner:
# - self-hosted
# - 1ES.Pool=agl-runner-cpu
# timeout: 45
# args: >-
# --mode batch
# --total-tasks 4096
# --batch-size 256
# --n-runners 32
# --max-rounds 6
# --sleep-seconds 0.5
# - id: scenario-medium-scale
# display: Medium production scale
# kind: scenario
# store_workers: 16
# runner:
# - self-hosted
# - 1ES.Pool=agl-runner-cpu
# timeout: 45
# args: >-
# --mode batch
# --total-tasks 10000
# --batch-size 1000
# --n-runners 100
# --max-rounds 10
# --sleep-seconds 0.1
# - id: scenario-midhigh-scale
# display: Mid-high production scale
# kind: scenario
# store_workers: 24
# runner:
# - self-hosted
# - 1ES.Pool=agl-runner-cpu
# timeout: 60
# args: >-
# --mode batch
# --total-tasks 20000
# --batch-size 2048
# --n-runners 300
# --max-rounds 6
# --sleep-seconds 0.1
# - id: scenario-large-batch
# display: Large batch waves
# kind: scenario
# store_workers: 96
# runner:
# - self-hosted
# - 1ES.Pool=agl-runner-cpu-high
# timeout: 120
# args: >-
# --mode batch
# --total-tasks 50000
# --batch-size 8192
# --n-runners 1000
# --max-rounds 3
# --sleep-seconds 0.1
- id: scenario-long-queues
display: Long rollout queues
store_workers: 32
kind: scenario
store_workers: 48
runner:
- self-hosted
- 1ES.Pool=agl-runner-cpu
timeout: 120
args: >-
--mode batch_partial
--total-tasks 100000
--total-tasks 50000
--batch-size 1024
--n-runners 256
--remaining-tasks 4096
--max-rounds 4
--sleep-seconds 0.1
- id: high-concurrency
display: High-throughput concurrent requests
store_workers: 32
args: >-
--mode single
--total-tasks 100000
--concurrency 2048
--n-runners 256
--max-rounds 2
--sleep-seconds 0.1
- id: heavy-traces
# - id: scenario-high-concurrency
# display: High-throughput concurrent requests
# kind: scenario
# store_workers: 96
# runner:
# - self-hosted
# - 1ES.Pool=agl-runner-cpu-high
# timeout: 120
# args: >-
# --mode single
# --total-tasks 50000
# --concurrency 2048
# --n-runners 256
# --max-rounds 2
# --sleep-seconds 0.1
- id: scenario-heavy-traces
display: Heavy rollouts with deep traces
store_workers: 64
kind: scenario
store_workers: 96
runner:
- self-hosted
- 1ES.Pool=agl-runner-cpu-high
timeout: 60
args: >-
--mode batch_partial
--total-tasks 10000
@@ -80,15 +130,66 @@ jobs:
--n-runners 512
--max-rounds 20
--sleep-seconds 1.0
# - id: micro-worker
# display: Update worker
# kind: micro
# store_workers: 8
# runner: ubuntu-latest
# timeout: 30
# cli: worker
# - id: micro-dequeue-empty
# display: Dequeue empty
# kind: micro
# store_workers: 8
# runner: ubuntu-latest
# timeout: 30
# cli: dequeue-empty
# - id: micro-rollout
# display: Rollout + span
# kind: micro
# store_workers: 8
# runner: ubuntu-latest
# timeout: 30
# cli: rollout
# - id: micro-dequeue-update-attempt
# display: Dequeue + update attempt
# kind: micro
# store_workers: 8
# runner: ubuntu-latest
# timeout: 30
# cli: dequeue-update-attempt
# - id: micro-dequeue-only
# display: Dequeue only
# kind: micro
# store_workers: 8
# runner: ubuntu-latest
# timeout: 30
# cli: dequeue-only
# - id: micro-metrics
# display: Multi-metric fan-out
# kind: micro
# store_workers: 8
# runner: ubuntu-latest
# timeout: 15
# cli: metrics
env:
PYTHONUNBUFFERED: "1"
STORE_URL: http://localhost:4747
STORE_API_URL: http://localhost:4747/v1/agl
PROM_URL: http://localhost:9090
SCENARIO_ID: ${{ matrix.scenario.id }}
GITHUB_ACTIONS_TIMEOUT_MINUTES: ${{ matrix.workload.timeout }}
WORKLOAD_KIND: ${{ matrix.workload.kind }}
WORKLOAD_ID: ${{ matrix.workload.id }}
BACKEND_ID: ${{ matrix.backend.id }}
ARTIFACT_DIR: artifacts/${{ matrix.scenario.id }}-${{ matrix.backend.id }}
TRACE_SINK_ID: ${{ matrix.trace_sink }}
ARTIFACT_DIR: ${{ format('artifacts/{0}-{1}-{2}', matrix.workload.id, matrix.backend.id, matrix.trace_sink) }}
COMPOSE_FILE: ${{ matrix.backend.compose_file }}
AGL_STORE_N_WORKERS: ${{ matrix.scenario.store_workers }}
AGL_STORE_N_WORKERS: ${{ matrix.workload.store_workers }}
ANALYSIS_FILE: ${{ format('analysis-{0}-{1}.log', matrix.workload.id, matrix.trace_sink) }}
SUMMARY_FILE: ${{ format('summary-{0}-{1}.log', matrix.workload.id, matrix.trace_sink) }}
PROM_ARCHIVE_BASENAME: ${{ format('prometheus-{0}-{1}', matrix.workload.id, matrix.backend.id) }}
ARTIFACT_NAME: ${{ format('{0}-{1}-{2}', matrix.workload.id, matrix.backend.id, matrix.trace_sink) }}
steps:
- uses: actions/checkout@v4
@@ -122,169 +223,297 @@ jobs:
set -euo pipefail
for attempt in {1..60}; do
if curl -fsS "$STORE_API_URL/health" >/dev/null 2>&1; then
sleep 1
curl -fsS "$STORE_API_URL/rollouts" # Warm up the scraper
sleep 15 # Allow some time for the baseline metrics to be established
exit 0
fi
sleep 1
done
echo "Store did not become ready in time" >&2
docker compose -f "$COMPOSE_FILE" logs app
# show logs for debugging
cd docker && docker compose -f "$COMPOSE_FILE" logs app
exit 1
- name: Configure trace sink (store vs otlp)
run: |
set -euo pipefail
if [ "${{ matrix.trace_sink }}" = "kafka" ] || [ "${{ matrix.trace_sink }}" = "clickhouse" ]; then
echo "AGL_OTLP_ENDPOINT=http://localhost:4318/v1/traces" >> "$GITHUB_ENV"
fi
- name: Launch Kafka + OTel Collector (OTLP -> Kafka)
if: ${{ matrix.trace_sink == 'kafka' }}
run: |
set -euo pipefail
cd docker
# Generate OTel Collector config (OTLP/HTTP receiver -> Kafka exporter)
cat > otelcol-kafka.yml <<'YAML'
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
processors:
batch: {}
exporters:
kafka:
brokers: ["kafka:9092"]
topic: "agl-otlp-spans"
encoding: otlp_proto
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [kafka]
YAML
# Launch Kafka + Zookeeper + OTel Collector
cat > compose.kafka-otel.yml <<'YAML'
services:
zookeeper:
image: confluentinc/cp-zookeeper:7.6.1
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
kafka:
image: confluentinc/cp-kafka:7.6.1
depends_on: [zookeeper]
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
# Enlarge max message size to accommodate large spans
KAFKA_MESSAGE_MAX_BYTES: "10000000"
KAFKA_REPLICA_FETCH_MAX_BYTES: "10000000"
KAFKA_SOCKET_REQUEST_MAX_BYTES: "10000000"
otelcol:
image: otel/opentelemetry-collector-contrib:latest
depends_on: [kafka]
command: ["--config=/etc/otelcol/config.yml"]
# command:
# - "--config=/etc/otelcol/config.yml"
# - "--set=service.telemetry.logs.level=debug"
volumes:
- ./otelcol-kafka.yml:/etc/otelcol/config.yml:ro
ports:
- "4318:4318"
YAML
docker compose -p agl-kafka -f compose.kafka-otel.yml down -v || true
docker compose -p agl-kafka -f compose.kafka-otel.yml up -d --quiet-pull
# Create topic (idempotent)
docker compose -p agl-kafka -f compose.kafka-otel.yml exec -T kafka \
kafka-topics --bootstrap-server kafka:9092 \
--create --if-not-exists \
--topic agl-otlp-spans --partitions 3 --replication-factor 1
# Wait for OTLP/HTTP port to be reachable on the host
for attempt in {1..30}; do
if (echo > /dev/tcp/127.0.0.1/4318) >/dev/null 2>&1; then
exit 0
fi
sleep 1
done
echo "OTel Collector port 4318 not reachable in time" >&2
docker compose -p agl-kafka -f compose.kafka-otel.yml logs otelcol || true
exit 1
- name: Launch ClickHouse + OTel Collector (OTLP -> ClickHouse)
if: ${{ matrix.trace_sink == 'clickhouse' }}
run: |
set -euo pipefail
cd docker
# Generate OTel Collector config (OTLP/HTTP receiver -> ClickHouse exporter)
cat > otelcol-clickhouse.yml <<'YAML'
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
processors:
batch: {}
exporters:
clickhouse:
endpoint: tcp://clickhouse:9000?dial_timeout=10s&compress=lz4
database: otel
traces_table_name: otel_traces
username: otel
password: ${env:CLICKHOUSE_PASSWORD}
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [clickhouse]
YAML
# Launch ClickHouse + OTel Collector
cat > compose.clickhouse-otel.yml <<'YAML'
services:
clickhouse:
image: clickhouse/clickhouse-server:latest
environment:
CLICKHOUSE_USER: "otel"
CLICKHOUSE_PASSWORD: "changeme"
CLICKHOUSE_DB: "otel"
ulimits:
nofile:
soft: 262144
hard: 262144
ports:
- "8123:8123"
- "9000:9000"
volumes:
- ch_data:/var/lib/clickhouse
healthcheck:
test: ["CMD-SHELL", "wget -qO- http://localhost:8123/ping | grep -q Ok"]
interval: 2s
timeout: 2s
retries: 30
start_period: 10s
otelcol:
image: otel/opentelemetry-collector-contrib:latest
depends_on:
clickhouse:
condition: service_healthy
restart: unless-stopped
command:
- "--config=/etc/otelcol/config.yml"
- "--set=service.telemetry.logs.level=debug"
environment:
CLICKHOUSE_PASSWORD: "changeme"
volumes:
- ./otelcol-clickhouse.yml:/etc/otelcol/config.yml:ro
ports:
- "4318:4318"
volumes:
ch_data:
YAML
docker compose -p agl-clickhouse -f compose.clickhouse-otel.yml down -v || true
docker compose -p agl-clickhouse -f compose.clickhouse-otel.yml up -d --quiet-pull
# Wait for OTLP/HTTP port to be reachable on the host
for attempt in {1..30}; do
if (echo > /dev/tcp/127.0.0.1/4318) >/dev/null 2>&1; then
exit 0
fi
sleep 1
done
echo "OTel Collector port 4318 not reachable in time" >&2
docker compose -p agl-clickhouse -f compose.clickhouse-otel.yml logs otelcol || true
exit 1
- name: Prepare artifact directory
run: mkdir -p "$ARTIFACT_DIR"
- name: Record benchmark start
- name: Kafka topic offsets (before workload)
if: ${{ matrix.trace_sink == 'kafka' }}
run: |
set -euo pipefail
cd docker
# Print offsets for common topic spellings; at least one should exist.
{
echo "== Kafka offsets BEFORE workload =="
# docker compose -p agl-kafka -f compose.kafka-otel.yml exec -T kafka \
# kafka-run-class kafka.tools.GetOffsetShell --broker-list kafka:9092 --topic agl_otlp_spans 2>/dev/null || true
docker compose -p agl-kafka -f compose.kafka-otel.yml exec -T kafka \
kafka-run-class kafka.tools.GetOffsetShell --broker-list kafka:9092 --topic agl-otlp-spans 2>/dev/null || true
} | tee "$GITHUB_WORKSPACE/$ARTIFACT_DIR/kafka-offsets-before.txt"
- name: ClickHouse row count (before workload)
if: ${{ matrix.trace_sink == 'clickhouse' }}
run: |
set -euo pipefail
cd docker
{
echo "== ClickHouse rows BEFORE workload =="
docker compose -p agl-clickhouse -f compose.clickhouse-otel.yml exec -T clickhouse clickhouse-client -q "SELECT count() FROM otel.otel_traces" 2>/dev/null || true
} | tee "$GITHUB_WORKSPACE/$ARTIFACT_DIR/clickhouse-rows-before.txt"
- name: Record workload start
run: echo "BENCHMARK_START=$(date -u +%FT%TZ)" >> "$GITHUB_ENV"
- name: Run ${{ matrix.scenario.display }} workload
- name: (Scenario) Run ${{ matrix.workload.display }} workload
if: ${{ matrix.workload.kind == 'scenario' }}
run: |
set -euo pipefail
uv run --locked --no-sync python -m tests.benchmark.benchmark_store \
--store-url "$STORE_URL" \
${{ matrix.scenario.args }}
${{ matrix.workload.args }}
- name: Record benchmark end
if: ${{ always() }}
run: echo "BENCHMARK_END=$(date -u +%FT%TZ)" >> "$GITHUB_ENV"
- name: Run benchmark analysis
if: ${{ always() }}
run: |
set -euo pipefail
mkdir -p "$ARTIFACT_DIR"
if [ -z "${BENCHMARK_START:-}" ] || [ -z "${BENCHMARK_END:-}" ]; then
echo "Analysis skipped: benchmark window not recorded." > "$ARTIFACT_DIR/analysis.txt"
exit 1
fi
uv run --locked --no-sync python -m tests.benchmark.analysis \
--prom-url "$PROM_URL" \
--store-url "$STORE_API_URL" \
--start "$BENCHMARK_START" \
--end "$BENCHMARK_END" \
| tee "$ARTIFACT_DIR/analysis.txt"
- name: Stop ${{ matrix.backend.id }} Prometheus stack
if: ${{ always() }}
run: |
set -euo pipefail
cd docker
docker compose -f "$COMPOSE_FILE" down -v || true
- name: Archive Prometheus metrics
if: ${{ always() }}
run: |
set -euo pipefail
mkdir -p "$ARTIFACT_DIR"
if [ -d docker/data/prometheus ]; then
tar -C docker/data -czf "$ARTIFACT_DIR/prometheus-${SCENARIO_ID}-${BACKEND_ID}.tar.gz" prometheus
fi
if docker compose -f "$COMPOSE_FILE" ps --format '{{.Name}}' >/dev/null 2>&1; then
docker compose -f "$COMPOSE_FILE" logs app > "$ARTIFACT_DIR/docker-${SCENARIO_ID}-${BACKEND_ID}.log" || true
fi
- name: Upload benchmark artifacts
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: benchmark-${{ matrix.scenario.id }}-${{ matrix.backend.id }}
path: ${{ env.ARTIFACT_DIR }}
if-no-files-found: error
micro-benchmark:
name: Micro-benchmark (${{ matrix.backend.id }}, ${{ matrix.mode.display }})
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
backend:
- id: memory
compose_file: compose.prometheus-memory-store.yml
- id: mongo
compose_file: compose.prometheus-mongo-store.yml
mode:
- id: worker
display: Update worker throughput
cli: worker
- id: dequeue-empty
display: Dequeue empty throughput
cli: dequeue-empty
- id: rollout
display: Rollout + span throughput
cli: rollout
env:
STORE_URL: http://localhost:4747
STORE_API_URL: http://localhost:4747/v1/agl
PROM_URL: http://localhost:9090
BACKEND_ID: ${{ matrix.backend.id }}
MODE_ID: ${{ matrix.mode.id }}
ARTIFACT_DIR: artifacts/micro-${{ matrix.mode.id }}-${{ matrix.backend.id }}
COMPOSE_FILE: ${{ matrix.backend.compose_file }}
AGL_STORE_N_WORKERS: 8
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
python-version: '3.12'
- name: Sync dependencies
run: uv sync --frozen --extra mongo --group core-stable --group dev
- name: Reset benchmark data directories
run: |
set -euo pipefail
cd docker
rm -rf data
bash setup.sh
- name: Launch ${{ matrix.backend.id }} Prometheus stack
run: |
set -euo pipefail
cd docker
docker compose -f "$COMPOSE_FILE" down -v || true
docker compose -f "$COMPOSE_FILE" up -d --quiet-pull
- name: Wait for store readiness
run: |
set -euo pipefail
for attempt in {1..60}; do
if curl -fsS "$STORE_API_URL/health" >/dev/null 2>&1; then
exit 0
fi
sleep 1
done
echo "Store did not become ready in time" >&2
cd docker && docker compose -f "$COMPOSE_FILE" logs app
exit 1
- name: Prepare artifact directory
run: mkdir -p "$ARTIFACT_DIR"
- name: Record micro benchmark start
run: echo "BENCHMARK_START=$(date -u +%FT%TZ)" >> "$GITHUB_ENV"
- name: Run ${{ matrix.mode.display }}
- name: (Micro) Run ${{ matrix.workload.display }}
if: ${{ matrix.workload.kind == 'micro' }}
run: |
set -euo pipefail
mkdir -p "$ARTIFACT_DIR"
uv run --locked --no-sync python -m tests.benchmark.micro_benchmark \
--store-url "$STORE_URL" \
--summary-file "$ARTIFACT_DIR/summary-${MODE_ID}.txt" \
"${{ matrix.mode.cli }}" | tee "$ARTIFACT_DIR/micro-${MODE_ID}.txt"
--summary-file "$ARTIFACT_DIR/$SUMMARY_FILE" \
"${{ matrix.workload.cli }}" | tee "$ARTIFACT_DIR/${{ matrix.workload.id }}.txt"
- name: Record micro benchmark end
- name: Record workload end
if: ${{ always() }}
run: echo "BENCHMARK_END=$(date -u +%FT%TZ)" >> "$GITHUB_ENV"
- name: Run micro benchmark analysis
- name: Kafka topic offsets (after workload)
if: ${{ always() && matrix.trace_sink == 'kafka' }}
run: |
set -euo pipefail
cd docker
{
echo "== Kafka offsets AFTER workload =="
# docker compose -p agl-kafka -f compose.kafka-otel.yml exec -T kafka \
# kafka-run-class kafka.tools.GetOffsetShell --broker-list kafka:9092 --topic agl_otlp_spans 2>/dev/null || true
docker compose -p agl-kafka -f compose.kafka-otel.yml exec -T kafka \
kafka-run-class kafka.tools.GetOffsetShell --broker-list kafka:9092 --topic agl-otlp-spans 2>/dev/null || true
} | tee "$GITHUB_WORKSPACE/$ARTIFACT_DIR/kafka-offsets-after.txt"
- name: ClickHouse row count (after workload)
if: ${{ always() && matrix.trace_sink == 'clickhouse' }}
run: |
set -euo pipefail
cd docker
{
echo "== ClickHouse rows AFTER workload =="
docker compose -p agl-clickhouse -f compose.clickhouse-otel.yml exec -T clickhouse clickhouse-client -q "SELECT count() FROM otel.otel_traces" 2>/dev/null || true
} | tee "$GITHUB_WORKSPACE/$ARTIFACT_DIR/clickhouse-rows-after.txt"
- name: Show micro benchmark summary
if: ${{ always() && matrix.workload.kind == 'micro' }}
run: |
set -euo pipefail
summary_file="$ARTIFACT_DIR/$SUMMARY_FILE"
if [ -f "$summary_file" ]; then
echo "Micro benchmark summary ($WORKLOAD_ID/$BACKEND_ID):"
cat "$summary_file"
else
echo "Summary file not found: $summary_file"
fi
- name: Run workload analysis
if: ${{ always() }}
run: |
set -euo pipefail
mkdir -p "$ARTIFACT_DIR"
if [ -z "${BENCHMARK_START:-}" ] || [ -z "${BENCHMARK_END:-}" ]; then
echo "Analysis skipped: benchmark window not recorded." > "$ARTIFACT_DIR/analysis-${MODE_ID}.txt"
echo "Analysis skipped: benchmark window not recorded." > "$ARTIFACT_DIR/$ANALYSIS_FILE"
exit 1
fi
uv run --locked --no-sync python -m tests.benchmark.analysis \
@@ -292,18 +521,66 @@ jobs:
--store-url "$STORE_API_URL" \
--start "$BENCHMARK_START" \
--end "$BENCHMARK_END" \
| tee "$ARTIFACT_DIR/analysis-${MODE_ID}.txt"
| tee "$ARTIFACT_DIR/$ANALYSIS_FILE"
- name: Show micro benchmark summary
- name: Collect docker logs
if: ${{ always() }}
run: |
set -euo pipefail
summary_file="$ARTIFACT_DIR/summary-${MODE_ID}.txt"
if [ -f "$summary_file" ]; then
echo "Micro benchmark summary ($MODE_ID/$BACKEND_ID):"
cat "$summary_file"
else
echo "Summary file not found: $summary_file"
mkdir -p "$ARTIFACT_DIR"
cd docker
readarray -t services < <(docker compose -f "$COMPOSE_FILE" config --services)
if [ "${#services[@]}" -eq 0 ]; then
echo "No services defined in compose file."
exit 0
fi
for service in "${services[@]}"; do
docker compose -f "$COMPOSE_FILE" logs "$service" > "../$ARTIFACT_DIR/docker-${service}-${WORKLOAD_ID}-${BACKEND_ID}.log" || true
done
- name: Collect Kafka + OTel Collector logs
if: ${{ always() && matrix.trace_sink == 'kafka' }}
run: |
set -euo pipefail
mkdir -p "$ARTIFACT_DIR"
cd docker
if [ -f compose.kafka-otel.yml ]; then
for service in zookeeper kafka otelcol; do
docker compose -p agl-kafka -f compose.kafka-otel.yml logs "$service" \
> "../$ARTIFACT_DIR/docker-kafka-${service}-${WORKLOAD_ID}-${BACKEND_ID}.log" || true
done
fi
- name: Collect ClickHouse + OTel Collector logs
if: ${{ always() && matrix.trace_sink == 'clickhouse' }}
run: |
set -euo pipefail
mkdir -p "$ARTIFACT_DIR"
cd docker
if [ -f compose.clickhouse-otel.yml ]; then
for service in clickhouse otelcol; do
docker compose -p agl-clickhouse -f compose.clickhouse-otel.yml logs "$service" > "../$ARTIFACT_DIR/docker-clickhouse-${service}-${WORKLOAD_ID}-${BACKEND_ID}.log" || true
done
fi
- name: Stop Kafka + OTel Collector
if: ${{ always() && matrix.trace_sink == 'kafka' }}
run: |
set -euo pipefail
cd docker
if [ -f compose.kafka-otel.yml ]; then
docker compose -p agl-kafka -f compose.kafka-otel.yml down -v || true
fi
- name: Stop ClickHouse + OTel Collector
if: ${{ always() && matrix.trace_sink == 'clickhouse' }}
run: |
set -euo pipefail
cd docker
if [ -f compose.clickhouse-otel.yml ]; then
docker compose -p agl-clickhouse -f compose.clickhouse-otel.yml down -v || true
fi
- name: Stop ${{ matrix.backend.id }} Prometheus stack
@@ -319,16 +596,126 @@ jobs:
set -euo pipefail
mkdir -p "$ARTIFACT_DIR"
if [ -d docker/data/prometheus ]; then
tar -C docker/data -czf "$ARTIFACT_DIR/prometheus-micro-${MODE_ID}-${BACKEND_ID}.tar.gz" prometheus
fi
if docker compose -f "$COMPOSE_FILE" ps --format '{{.Name}}' >/dev/null 2>&1; then
docker compose -f "$COMPOSE_FILE" logs app > "$ARTIFACT_DIR/docker-micro-${MODE_ID}-${BACKEND_ID}.log" || true
tar -C docker/data -czf "$ARTIFACT_DIR/${PROM_ARCHIVE_BASENAME}.tar.gz" prometheus
fi
- name: Upload micro benchmark artifacts
- name: Upload workload artifacts
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: micro-benchmark-${{ matrix.mode.id }}-${{ matrix.backend.id }}
name: ${{ env.ARTIFACT_NAME }}
path: ${{ env.ARTIFACT_DIR }}
if-no-files-found: error
# collection-benchmarks:
# name: collection (${{ matrix.backend.id }}, ${{ matrix.workload.id }})
# runs-on: ${{ matrix.backend.runner }}
# timeout-minutes: 15
# strategy:
# fail-fast: false
# matrix:
# backend:
# - id: memory
# needs_mongo: false
# runner: ubuntu-latest
# - id: mongo
# needs_mongo: true
# runner: ubuntu-latest
# workload:
# - id: high-insert
# total_tasks: 50000
# concurrency: 2048
# type: insert
# - id: medium-insert
# total_tasks: 50000
# concurrency: 128
# type: insert
# - id: low-insert
# total_tasks: 50000
# concurrency: 4
# type: insert
# - id: high-dequeue
# total_tasks: 50000
# concurrency: 2048
# type: dequeue
# - id: medium-dequeue
# total_tasks: 50000
# concurrency: 128
# type: dequeue
# - id: low-dequeue
# total_tasks: 50000
# concurrency: 4
# type: dequeue
# env:
# ARTIFACT_DIR: ${{ format('artifacts/{0}-{1}', matrix.backend.id, matrix.workload.id) }}
# SUMMARY_FILE: ${{ format('artifacts/{0}-{1}/summary-{0}-{1}.jsonl', matrix.backend.id, matrix.workload.id) }}
# ARTIFACT_NAME: ${{ format('collections-{0}-{1}', matrix.backend.id, matrix.workload.id) }}
# MONGO_URI: mongodb://localhost:27017/?replicaSet=rs0
# steps:
# - uses: actions/checkout@v4
# - uses: astral-sh/setup-uv@v7
# with:
# enable-cache: true
# python-version: '3.12'
# - name: Sync dependencies
# run: uv sync --frozen --extra mongo --group core-stable --group dev
# - name: Launch MongoDB
# if: ${{ matrix.backend.needs_mongo }}
# run: |
# set -euo pipefail
# cd docker
# docker compose -f compose.mongo.yml down -v || true
# docker compose -f compose.mongo.yml up -d --quiet-pull
# for attempt in {1..60}; do
# if docker compose -f compose.mongo.yml exec -T mongo mongosh --quiet --eval 'db.runCommand({ping:1})' >/dev/null 2>&1; then
# exit 0
# fi
# sleep 2
# done
# echo "MongoDB did not become ready in time" >&2
# docker compose -f compose.mongo.yml logs mongo
# exit 1
# - name: Run collection benchmark
# run: |
# set -euo pipefail
# mkdir -p "$ARTIFACT_DIR"
# echo "Running collection benchmark (backend=${{ matrix.backend.id }}, workload=${{ matrix.workload.id }})"
# uv run --locked --no-sync python -m tests.benchmark.collection_benchmark \
# "${{ matrix.workload.type }}" \
# --backend "${{ matrix.backend.id }}" \
# --total-tasks "${{ matrix.workload.total_tasks }}" \
# --concurrency "${{ matrix.workload.concurrency }}" \
# --task-prefix "${{ matrix.backend.id }}-${{ matrix.workload.id }}" \
# --summary-file "$SUMMARY_FILE" \
# --mongo-uri "$MONGO_URI" \
# --mongo-database agentlightning_collection_bench
# - name: Show collection benchmark summary
# if: ${{ always() }}
# run: |
# set -euo pipefail
# if [ -f "$SUMMARY_FILE" ]; then
# echo "Collection benchmark summary (${{ matrix.backend.id }}):"
# cat "$SUMMARY_FILE"
# else
# echo "Summary file not found: $SUMMARY_FILE"
# fi
# - name: Stop MongoDB
# if: ${{ always() && matrix.backend.needs_mongo }}
# run: |
# set -euo pipefail
# cd docker
# docker compose -f compose.mongo.yml down -v || true
# - name: Upload collection artifacts
# if: ${{ always() }}
# uses: actions/upload-artifact@v4
# with:
# name: ${{ env.ARTIFACT_NAME }}
# path: ${{ env.ARTIFACT_DIR }}
# if-no-files-found: error
+100 -2
View File
@@ -171,12 +171,12 @@ jobs:
- name: Sync dependencies (latest)
run: |
uv sync --frozen --no-default-groups --extra verl \
--group dev --group experiment --group agents --group torch-gpu-stable
--group dev --group experiment --group agents --extra weave --extra mongo --group torch-gpu-stable
if: matrix.setup-script == 'latest'
- name: Sync dependencies (stable & legacy)
run: |
uv sync --frozen --no-default-groups --extra verl \
--group dev --group experiment --group agents --group torch-gpu-${{ matrix.setup-script }}
--group dev --group experiment --group agents --extra weave --extra mongo --group torch-gpu-${{ matrix.setup-script }}
if: matrix.setup-script != 'latest'
- name: Freeze dependencies
run: |
@@ -270,6 +270,104 @@ jobs:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
- name: Setup Docker environments
run: ./scripts/mongodb_docker_run.sh
shell: bash
- name: Training with MongoDB
run: |
set -ex
source .venv/bin/activate
cd examples/calc_x
../../scripts/restart_ray.sh
sleep 5
PYTHONUNBUFFERED=1 python train_calc_agent.py --val-file data/test_mini.parquet --ci-fast --mongo-uri mongodb://localhost:27017/?replicaSet=rs0
sleep 10
shell: bash
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
id: calc_x_train_mongo
- name: Validate training with MongoDB
run: |
set -ex
uv run scripts/validate_example_wandb.py ${{ steps.calc_x_train_mongo.outputs.project_name }} ${{ steps.calc_x_train_mongo.outputs.run_name }}
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
- name: Training with LoRA
run: |
set -ex
source .venv/bin/activate
cd examples/calc_x
../../scripts/restart_ray.sh
sleep 5
PYTHONUNBUFFERED=1 python train_calc_agent.py --val-file data/test_mini.parquet --ci-fast --lora
sleep 10
shell: bash
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
id: calc_x_train_lora
if: matrix.setup-script != 'legacy'
- name: Validate training with LoRA
run: |
set -ex
uv run scripts/validate_example_wandb.py ${{ steps.calc_x_train_lora.outputs.project_name }} ${{ steps.calc_x_train_lora.outputs.run_name }}
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
if: matrix.setup-script != 'legacy'
- name: Training with trajectory level aggregation
run: |
set -ex
source .venv/bin/activate
cd examples/calc_x
../../scripts/restart_ray.sh
sleep 5
PYTHONUNBUFFERED=1 python train_calc_agent.py --val-file data/test_mini.parquet --ci-fast --trajectory-level
sleep 10
shell: bash
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
id: calc_x_train_trajectory_level
- name: Validate training with trajectory level aggregation
run: |
set -ex
uv run scripts/validate_example_wandb.py ${{ steps.calc_x_train_trajectory_level.outputs.project_name }} ${{ steps.calc_x_train_trajectory_level.outputs.run_name }}
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
- name: Training with Weave
run: |
set -ex
source .venv/bin/activate
cd examples/calc_x
../../scripts/restart_ray.sh
sleep 5
PYTHONUNBUFFERED=1 python train_calc_agent.py --val-file data/test_mini.parquet --ci-fast --weave
sleep 10
shell: bash
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
id: calc_x_train_weave
- name: Validate training with Weave
run: |
set -ex
uv run scripts/validate_example_wandb.py ${{ steps.calc_x_train_weave.outputs.project_name }} ${{ steps.calc_x_train_weave.outputs.run_name }}
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
- name: Training with external store
run: |
set -euo pipefail
+168
View File
@@ -0,0 +1,168 @@
name: Examples - ChartQA
permissions:
contents: read
on:
schedule:
# Every day at 6 AM UTC+8
- cron: "0 22 * * *"
workflow_dispatch:
repository_dispatch:
types: [ci-chartqa, ci-all]
run-name: >-
${{ github.event_name == 'repository_dispatch'
&& format(
'ChartQA - PR #{0} - {1} - {2}',
github.event.client_payload.pull_number,
github.event.client_payload.ci_label,
github.event.client_payload.correlation_id
)
|| format('ChartQA - {0}', github.event_name) }}
jobs:
chartqa:
if: >
github.event_name != 'repository_dispatch' ||
github.event.action == 'ci-chartqa' ||
github.event.action == 'ci-all'
name: ChartQA (Python ${{ matrix.python-version }}, ${{ matrix.setup-script }})
runs-on: [self-hosted, 1ES.Pool=agl-runner-gpu]
timeout-minutes: 60
strategy:
matrix:
include:
- python-version: '3.12'
setup-script: 'stable'
fail-fast: false
steps:
- name: Check GPU status
run: nvidia-smi
- name: Check disk space
run: df -h
- uses: actions/checkout@v4
with:
ref: ${{ github.event_name == 'repository_dispatch' && github.event.client_payload.pr_ref || (github.event.pull_request.number && format('refs/pull/{0}/merge', github.event.pull_request.number)) || github.ref }}
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
python-version: ${{ matrix.python-version }}
- name: Upgrade dependencies (latest)
run: uv lock --upgrade
if: matrix.setup-script == 'latest'
- name: Sync dependencies
run: |
uv sync --frozen --no-default-groups --extra verl \
--group dev --group experiment --group image --group langchain --group vllm-0-10-2 --group torch-gpu-stable
- name: Freeze dependencies
run: |
set -ex
uv pip freeze | tee requirements-freeze.txt
echo "UV_LOCKED=1" >> $GITHUB_ENV
echo "UV_NO_SYNC=1" >> $GITHUB_ENV
- name: Upload dependencies artifact
uses: actions/upload-artifact@v4
with:
name: dependencies-chartqa-${{ matrix.python-version }}-${{ matrix.setup-script }}
path: requirements-freeze.txt
compression-level: 0
- name: Launch LiteLLM Proxy
run: |
./scripts/litellm_run.sh
env:
AZURE_API_BASE: ${{ secrets.AZURE_GROUP_SUBSCRIPTION_API_BASE }}
AZURE_API_KEY: ${{ secrets.AZURE_GROUP_SUBSCRIPTION_API_KEY }}
- name: Prepare ChartQA dataset
run: |
set -euo pipefail
cd examples/chartqa
uv run gdown --fuzzy "https://drive.google.com/file/d/1fWRt9hehg8_uV7BDWSCwKTycM60JcmGN/view?usp=sharing" -O chartqa-data.zip
unzip chartqa-data.zip
rm chartqa-data.zip
shell: bash
- name: ChartQA sanity check with GPT
run: |
set -euo pipefail
cd examples/chartqa
uv run python debug_chartqa_agent.py
shell: bash
env:
OPENAI_API_BASE: http://localhost:12306/
OPENAI_API_KEY: dummy
- name: Run vLLM Server
run: |
set -euo pipefail
source .venv/bin/activate
cd examples/chartqa
uv run --no-sync vllm serve Qwen/Qwen2-VL-2B-Instruct \
--gpu-memory-utilization 0.9 \
--max-model-len 4096 \
--allowed-local-media-path "$(pwd)/data" \
--enable-prefix-caching \
--port 8088 &
VLLM_READY=0
for i in {1..100}; do
if curl -sSf http://localhost:8088/v1/models > /dev/null 2>&1; then
echo "vLLM server is ready!"
VLLM_READY=1
break
fi
echo "Waiting for vLLM server to be ready... (${i})"
sleep 5
done
if [[ "$VLLM_READY" != "1" ]]; then
echo "vLLM server failed to start!"
exit 1
fi
- name: ChartQA sanity check with vLLM
run: |
set -euo pipefail
source .venv/bin/activate
cd examples/chartqa
uv run python debug_chartqa_agent.py
shell: bash
env:
USE_LLM_PROXY: "1"
OPENAI_API_BASE: http://localhost:8088/v1
OPENAI_MODEL: Qwen/Qwen2-VL-2B-Instruct
- name: Stop vLLM Server
run: |
set -euo pipefail
pkill -f vllm
for i in {1..60}; do
if ! pgrep -f vllm; then
break
fi
sleep 5
done
- name: ChartQA training
run: |
set -euo pipefail
source .venv/bin/activate
cd examples/chartqa
../../scripts/restart_ray.sh
sleep 5
PYTHONUNBUFFERED=1 python train_chartqa_agent.py ci
sleep 10
shell: bash
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
id: chartqa_train
- name: Validate ChartQA training
run: |
set -euo pipefail
uv run scripts/validate_example_wandb.py ${{ steps.chartqa_train.outputs.project_name }} ${{ steps.chartqa_train.outputs.run_name }}
env:
WANDB_BASE_URL: ${{ secrets.MSR_WANDB_BASE_URL }}
WANDB_API_KEY: ${{ secrets.MSR_WANDB_API_KEY }}
+1 -1
View File
@@ -74,7 +74,7 @@ jobs:
- name: Upload dependencies artifact
uses: actions/upload-artifact@v4
with:
name: dependencies-spider-${{ matrix.python-version }}-${{ matrix.setup-script }}
name: dependencies-rag-${{ matrix.python-version }}-${{ matrix.setup-script }}
path: requirements-freeze.txt
compression-level: 0
+13 -3
View File
@@ -68,13 +68,23 @@ jobs:
path: requirements-freeze.txt
compression-level: 0
- name: Tinker LLM sanity check
# TODO: Currently only test the client tracer implementation.
- name: Tinker LLM sanity check (tracer text)
run: |
set -euo pipefail
source .venv/bin/activate
cd examples/tinker
# TODO: Currently only test the client tracer implementation.
python -m tests.test_tinker_llm
python -m tests.test_tinker_llm tracer-text
shell: bash
env:
TINKER_API_KEY: ${{ secrets.TINKER_API_KEY }}
- name: Tinker LLM sanity check (tracer tool)
run: |
set -euo pipefail
source .venv/bin/activate
cd examples/tinker
python -m tests.test_tinker_llm tracer-tool
shell: bash
env:
TINKER_API_KEY: ${{ secrets.TINKER_API_KEY }}
+18
View File
@@ -0,0 +1,18 @@
# Pre-defined workflow with workflow_dispatch trigger,
# convenient for testing and debugging.
name: Playground
permissions:
contents: read
on:
workflow_dispatch:
jobs:
playground:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run script
run: |
echo "Hello, world!"
+71 -59
View File
@@ -27,13 +27,43 @@ jobs:
github.event_name != 'repository_dispatch' ||
github.event.action == 'ci-gpu' ||
github.event.action == 'ci-all'
name: GPU Test with Python ${{ matrix.python-version }} (${{ matrix.setup-script }})
name: Full Test (${{ matrix.mark.display-name }}, ${{ matrix.env.setup-script }}, Python ${{ matrix.env.python-version }})
runs-on: [self-hosted, 1ES.Pool=agl-runner-gpu]
runs-on: ${{ matrix.mark.runs-on }}
timeout-minutes: 30
strategy:
matrix:
include:
mark:
- id: store
display-name: Store
pytest-mark: 'store' # store tests should not require gpu
runs-on: ubuntu-latest
has-gpu: false
# AgentOps needs to be separated because it injects tricky global state.
- id: agentops
display-name: AgentOps
pytest-mark: 'agentops' # including agentops+litellm tests here
runs-on: [self-hosted, 1ES.Pool=agl-runner-gpu]
has-gpu: true
# Similar for Weave.
- id: weave
display-name: Weave
pytest-mark: 'weave'
runs-on: ubuntu-latest # No GPU tests for Weave.
has-gpu: false
# Other tests that require GPU
- id: gpu
display-name: GPU required
pytest-mark: '(gpu or llmproxy) and not agentops'
runs-on: [self-hosted, 1ES.Pool=agl-runner-gpu]
has-gpu: true
# Other uncovered tests
- id: others
display-name: Others
pytest-mark: 'not store and not agentops and not weave and not gpu and not llmproxy'
runs-on: ubuntu-latest
has-gpu: false
env:
- python-version: '3.10'
setup-script: 'legacy'
- python-version: '3.12'
@@ -43,6 +73,7 @@ jobs:
fail-fast: false
steps:
- name: Check GPU status
if: matrix.mark.has-gpu
run: nvidia-smi
- uses: actions/checkout@v4
with:
@@ -51,20 +82,32 @@ jobs:
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
python-version: ${{ matrix.python-version }}
python-version: ${{ matrix.env.python-version }}
- name: Upgrade dependencies (latest)
run: uv lock --upgrade
if: matrix.setup-script == 'latest'
- name: Sync dependencies (latest)
run: uv sync --frozen --no-default-groups --extra apo --extra mongo --group dev --group agents --group langchain --group torch-gpu-stable
if: matrix.setup-script == 'latest'
- name: Sync dependencies (stable)
run: uv sync --frozen --no-default-groups --extra apo --extra mongo --group dev --group agents --group langchain --group torch-gpu-${{ matrix.setup-script }}
if: matrix.setup-script == 'stable'
if: matrix.env.setup-script == 'latest'
- name: Sync dependencies (latest, gpu)
if: matrix.env.setup-script == 'latest' && matrix.mark.has-gpu
run: uv sync --frozen --no-default-groups --extra apo --extra weave --extra mongo --group dev --group agents --group langchain --group torch-gpu-stable
# Don't install vllm/pytorch on CPU counterparts
- name: Sync dependencies (latest, cpu)
if: matrix.env.setup-script == 'latest' && !matrix.mark.has-gpu
run: uv sync --frozen --no-default-groups --extra apo --extra weave --extra mongo --group dev --group agents --group langchain --group core-stable
- name: Sync dependencies (stable, gpu)
if: matrix.env.setup-script == 'stable' && matrix.mark.has-gpu
run: uv sync --frozen --no-default-groups --extra apo --extra weave --extra mongo --group dev --group agents --group langchain --group torch-gpu-${{ matrix.env.setup-script }}
- name: Sync dependencies (stable, cpu)
if: matrix.env.setup-script == 'stable' && !matrix.mark.has-gpu
run: uv sync --frozen --no-default-groups --extra apo --extra weave --extra mongo --group dev --group agents --group langchain --group core-stable
# Don't install langchain for legacy dependency because it has conflicts with torch.
- name: Sync dependencies (legacy)
run: uv sync --frozen --no-default-groups --extra apo --extra mongo --group dev --group agents --group torch-gpu-legacy
if: matrix.setup-script == 'legacy'
- name: Sync dependencies (legacy, gpu)
if: matrix.env.setup-script == 'legacy' && matrix.mark.has-gpu
run: uv sync --frozen --no-default-groups --extra apo --extra weave --extra mongo --group dev --group agents --group torch-gpu-legacy
- name: Sync dependencies (legacy, cpu)
if: matrix.env.setup-script == 'legacy' && !matrix.mark.has-gpu
run: uv sync --frozen --no-default-groups --extra apo --extra weave --extra mongo --group dev --group agents --group core-legacy
- name: Freeze dependencies
run: |
set -ex
@@ -74,62 +117,22 @@ jobs:
- name: Upload dependencies artifact
uses: actions/upload-artifact@v4
with:
name: dependencies-tests-full-${{ matrix.python-version }}-${{ matrix.setup-script }}
name: dependencies-tests-full-${{ matrix.mark.id }}-${{ matrix.env.python-version }}-${{ matrix.env.setup-script }}
path: requirements-freeze.txt
compression-level: 0
- uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: dashboard/package-lock.json
- name: Install JavaScript dependencies
run: cd dashboard && npm ci
- name: Build dashboard
run: cd dashboard && npm run build
- name: Setup Docker environments
run: |
set -euo pipefail
cd docker
# Setup data directories
./setup.sh
# Start Dockers
docker compose -f compose.mongo.yml up -d
SERVICE_NAME=mongo
TIMEOUT=60 # seconds
SLEEP=2
cid="$(docker compose -f compose.mongo.yml ps -q "$SERVICE_NAME")"
if [ -z "$cid" ]; then
echo "Service $SERVICE_NAME is not running"
exit 1
fi
echo "Waiting for $SERVICE_NAME to become healthy..."
end=$((SECONDS + TIMEOUT))
while [ "$SECONDS" -lt "$end" ]; do
status="$(docker inspect -f '{{.State.Health.Status}}' "$cid")"
echo "Current status: $status"
if [ "$status" = "healthy" ]; then
echo "$SERVICE_NAME is healthy ✅"
exit 0
elif [ "$status" = "unhealthy" ]; then
echo "$SERVICE_NAME is unhealthy ❌"
docker logs "$cid" || true
exit 1
fi
sleep "$SLEEP"
done
echo "Timed out waiting for $SERVICE_NAME to become healthy after ${TIMEOUT}s"
docker logs "$cid" || true
exit 1
run: ./scripts/mongodb_docker_run.sh
shell: bash
- name: Launch LiteLLM Proxy
@@ -139,9 +142,10 @@ jobs:
AZURE_API_BASE: ${{ secrets.AZURE_GROUP_SUBSCRIPTION_API_BASE }}
AZURE_API_KEY: ${{ secrets.AZURE_GROUP_SUBSCRIPTION_API_KEY }}
# mongo, openai, gpu, all enabled by default
- name: Run tests
run: |
uv run pytest -v --durations=0 tests
uv run pytest -v --durations=0 tests -m "${{ matrix.mark.pytest-mark }}${{ matrix.env.setup-script == 'legacy' && ' and not langchain' || '' }}"
env:
PYTEST_ADDOPTS: "--color=yes"
OPENAI_BASE_URL: http://localhost:12306/
@@ -230,6 +234,14 @@ jobs:
python write_traces.py agentops
sleep 5
- name: Write Traces with Operations
run: |
set -euo pipefail
source .venv/bin/activate
cd examples/minimal
python write_traces.py operation
sleep 5
- name: Write Traces via Otel Tracer with Client
run: |
set -euo pipefail
+54 -14
View File
@@ -19,7 +19,8 @@ jobs:
lint:
strategy:
matrix:
setup: [fast, slow]
setup: [fast, slow, next]
fail-fast: false
name: Lint - ${{ matrix.setup }}
runs-on: ubuntu-latest
timeout-minutes: 10
@@ -32,10 +33,14 @@ jobs:
- name: Sync dependencies (fast)
run: uv sync --frozen --group dev --no-default-groups
if: matrix.setup == 'fast'
- name: Upgrade dependencies (next)
run: uv lock --upgrade
if: matrix.setup == 'next'
- name: Sync dependencies (slow)
run: |
uv sync --frozen \
--extra apo \
--extra weave \
--extra verl \
--extra mongo \
--group dev \
@@ -46,7 +51,7 @@ jobs:
--group agents \
--group langchain \
--no-default-groups
if: matrix.setup == 'slow'
if: matrix.setup != 'fast'
# This pre-commit skips JavaScript on purpose.
- name: Run pre-commit
uses: pre-commit/action@v3.0.1
@@ -61,7 +66,7 @@ jobs:
if: matrix.setup == 'fast'
- name: Run pyright (slow)
run: uv run --locked --no-sync pyright -p pyrightconfig.json
if: matrix.setup == 'slow'
if: matrix.setup != 'fast'
lint-js:
name: Lint - JavaScript
@@ -72,6 +77,8 @@ jobs:
- uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: dashboard/package-lock.json
- name: Install dependencies
run: cd dashboard && npm ci
- name: Run ESLint
@@ -104,6 +111,10 @@ jobs:
- name: Set source commit for docs
run: |
echo "SOURCE_COMMIT=${{ github.sha }}" >> $GITHUB_ENV
- name: Verify OpenAPI specification is up-to-date
run: |
uv run --locked --no-sync python scripts/export_openapi.py
git diff --exit-code docs/assets/store-openapi.json
- name: Build documentation
run: uv run --locked --no-sync mkdocs build --strict
- name: Upload docs artifact
@@ -116,7 +127,32 @@ jobs:
test:
strategy:
matrix:
include:
mark:
# store has many tests and is a good isolated group.
- id: store
display-name: Store
pytest-mark: 'store'
# AgentOps needs to be separated because it injects tricky global state.
- id: agentops
display-name: AgentOps
pytest-mark: 'agentops'
# Similar for Weave.
- id: weave
display-name: Weave
pytest-mark: 'weave'
# litellm proxy tests are slow
- id: llmproxy
display-name: LLM proxy
pytest-mark: 'llmproxy'
# Robustness of utilities is important. There are many tests.
- id: utils
display-name: Utilities
pytest-mark: 'utils'
# unmarked tests: adapter, execution engine, etc.
- id: others
display-name: Others
pytest-mark: 'not store and not agentops and not weave and not llmproxy and not utils'
env:
- python-version: '3.10'
setup-script: 'legacy'
- python-version: '3.11'
@@ -127,7 +163,7 @@ jobs:
setup-script: 'latest'
fail-fast: false
name: Test with Python ${{ matrix.python-version }} (${{ matrix.setup-script }})
name: Test (${{ matrix.mark.display-name }}, ${{ matrix.env.setup-script }}, Python ${{ matrix.env.python-version }})
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
@@ -135,16 +171,16 @@ jobs:
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
python-version: ${{ matrix.python-version }}
python-version: ${{ matrix.env.python-version }}
- name: Upgrade dependencies (latest)
run: uv lock --upgrade
if: matrix.setup-script == 'latest'
if: matrix.env.setup-script == 'latest'
- name: Sync dependencies (latest)
run: uv sync --frozen --no-default-groups --extra apo --group dev --group agents --group langchain --group core-stable
if: matrix.setup-script == 'latest'
run: uv sync --frozen --no-default-groups --extra apo --extra weave --group dev --group agents --group langchain --group core-stable
if: matrix.env.setup-script == 'latest'
- name: Sync dependencies (stable & legacy)
run: uv sync --frozen --no-default-groups --extra apo --group dev --group agents --group langchain --group core-${{ matrix.setup-script }}
if: matrix.setup-script != 'latest'
run: uv sync --frozen --no-default-groups --extra apo --extra weave --group dev --group agents --group langchain --group core-${{ matrix.env.setup-script }}
if: matrix.env.setup-script != 'latest'
- name: Freeze dependencies
run: |
set -ex
@@ -154,13 +190,15 @@ jobs:
- name: Upload dependencies artifact
uses: actions/upload-artifact@v4
with:
name: dependencies-${{ matrix.python-version }}-${{ matrix.setup-script }}
name: dependencies-${{ matrix.mark.id }}-${{ matrix.env.python-version }}-${{ matrix.env.setup-script }}
path: requirements-freeze.txt
compression-level: 0
- uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: dashboard/package-lock.json
- name: Install JavaScript dependencies
run: cd dashboard && npm ci
- name: Build dashboard
@@ -168,12 +206,12 @@ jobs:
- name: Run tests
run: |
uv run pytest -v --durations=0 tests -m "not mongo"
uv run pytest -v --durations=0 tests -m "not mongo and not openai and not gpu and (${{ matrix.mark.pytest-mark }})"
env:
PYTEST_ADDOPTS: "--color=yes"
test-js:
name: Test - JavaScript
name: Test (JavaScript)
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
@@ -183,6 +221,8 @@ jobs:
- uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: dashboard/package-lock.json
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
+3 -1
View File
@@ -1,8 +1,10 @@
# Agentlightning specific files
verl_old
meta-llama/**
debug/*.png
**/debug/**/*.png
**/debug/**/*.json
requirements-freeze*.txt
/playground
# Byte-compiled / optimized / DLL files
__pycache__/
+2 -1
View File
@@ -3,13 +3,14 @@ repos:
rev: v6.0.0
hooks:
- id: end-of-file-fixer
exclude: (.*store-openapi\.json$)
- id: trailing-whitespace
- id: check-yaml
exclude: ^mkdocs\.yml$
- id: check-toml
- id: check-added-large-files
args: ["--maxkb=1024"]
exclude: (^uv\.lock$)|(^docs/assets/.*\.svg$)
exclude: (^uv\.lock$)|(^docs/assets/.*\.svg$)|(.*store-openapi\.json$)
- id: check-shebang-scripts-are-executable
- id: detect-private-key
- repo: https://github.com/pycqa/isort
+41
View File
@@ -0,0 +1,41 @@
# Repository Guidelines
## Architecture Overview
Agent Lightning runs through a continuous loop: runners and tracers emit spans, `LightningStore` (`agentlightning/store/`) keeps them synchronized, and algorithms in `agentlightning/algorithm/` consume those traces to improve behavior.
## Project Structure & Module Organization
- `agentlightning/`: adapters, execution stack, training loop, tracer, reward logic, and the `agl` CLI.
- `docs/` & `examples/`: narrative and procedural docs (assets in `docs/assets/`, navigation in `mkdocs.yml`) plus runnable workflows whose READMEs point to their companion how-to guides. `docs/how-to` covers task-focused instructions, while `docs/tutorials` explains concepts and subsystems.
- `dashboard/`, `scripts/`, `tests/`: UI bundles, release/dataset/CI automation, and mirrored coverage of the runtime tree. Record download steps rather than committing binaries.
## Build, Test, and Development Commands
- `uv sync --group dev` — provision tooling once per environment.
- `uv run --no-sync pytest -v` — execute the full suite; add a path or `-k expr` to narrow the run.
- `uv run --no-sync pyright` — enforce static typing parity with CI.
- `uv run --no-sync pre-commit run --all-files --show-diff-on-failure` and `uv run --no-sync mkdocs build --strict` — keep formatting tidy and documentation valid.
Always commit the refreshed `uv.lock` when dependencies shift, and mention optional groups (VERL, APO, GPU) in PR notes.
## Common Issues & Fixes
- When `uv run` errors with `Permission denied` under `~/.cache`, override both cache locations inline: ``UV_CACHE="$(pwd)/.cache_uv" XDG_CACHE_HOME="$(pwd)/.cache_xdg" uv run --no-sync <command>``.
## Coding Style & Naming Conventions
- Target `requires-python >= 3.10`, four-space indentation, 120-character lines (though docstrings may run longer), and formatter-owned diffs (Black + isort, `black` profile). Use `snake_case` for modules, functions, and variables; `PascalCase` for classes and React components; lowercase hyphenation for CLI flags, branch names, and TypeScript filenames.
- Maintain exhaustive type hints (pyright enforces them) and prefer shared dataclasses or Pydantic models from `agentlightning.types`.
- Author Google-style docstrings for new modules or public methods—succinct descriptions, no redundant type info, no redundant `Key features/components` bullet points. Use mkdocs styles: `[][]` syntax for cross-references and single backticks for inline code blocks.
- Writing logs is encouraged, especially for long functions with multiple steps and try-except blocks that catch all exceptions. Use `logging.getLogger(__name__)` to get loggers. Distinguish between DEBUG, INFO, WARNING, and ERROR logs.
## Testing Guidelines
- Mirror runtime directories under `tests/` and match filenames for quick traceability.
- Parametrize pytest cases and apply markers (`openai`, `gpu`, `agentops`, `mongo`, `llmproxy`) so optional suites can be skipped via selectors like `-m "not mongo"` yet still exercised in CI.
- Lean on fixtures, favor real stores/spans/agents over mocks, and drive coverage across the majority of branches.
- If an imported module is missing from the environment, check whether `uv sync` has been run with the right groups. Do not make stubs for external dependencies unless necessary.
## Example Contributions
- Ship each example with a README that includes smoke-test instructions so maintainers can validate quickly. The README must contain an "Included Files" section summarizing every file and its role.
- Keep runnable example modules self-contained with a module-level docstring describing CLI usage. Document important or educational classes/functions with targeted docstrings and inline comments where clarity matters.
- Add a CI workflow per example named `examples-<name>.yml` in `.github/workflows/`. Register it in `badge-<name>.yml`, `badge-examples.yml`, and `badge-latest.yml` when applicable so badges stay accurate.
## Commit & Pull Request Guidelines
- Branch from a fresh `main` using `feature/<slug>`, `fix/<slug>`, `docs/<slug>`, or `chore/<slug>`.
- Write imperative, scoped commits, reference issues with `Fixes #123`, and rerun pre-commit plus the relevant pytest/doc builds before pushing.
- Use PR descriptions to summarize intent, list verification commands, call out dependency or docs-navigation updates, and link new docs/examples via `mkdocs.yml` or `examples/README.md`. Include logs for dashboard changes.
Symlink
+1
View File
@@ -0,0 +1 @@
AGENTS.md
+3 -1
View File
@@ -37,7 +37,7 @@ pip install agentlightning
For the latest nightly build (cutting-edge features), you can install from Test PyPI:
```bash
pip install --upgrade --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ agentlightning
pip install --upgrade --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ --pre agentlightning
```
Please refer to our [installation guide](https://microsoft.github.io/agent-lightning/stable/tutorials/installation/) for more details.
@@ -46,6 +46,7 @@ To start using Agent-lightning, check out our [documentation](https://microsoft.
## ⚡ Articles
- 12/17/2025 [Adopting the Trajectory Level Aggregation for Faster Training](https://agent-lightning.github.io/posts/trajectory_level_aggregation/) Agent-lightning blog.
- 11/4/2025 [Tuning ANY AI agent with Tinker ✕ Agent-lightning](https://medium.com/@yugez/tuning-any-ai-agent-with-tinker-agent-lightning-part-1-1d8c9a397f0e) Medium. See also [Part 2](https://medium.com/@yugez/tuning-any-ai-agent-with-tinker-agent-lightning-part-2-332c5437f0dc).
- 10/22/2025 [No More Retokenization Drift: Returning Token IDs via the OpenAI Compatible API Matters in Agent RL](https://blog.vllm.ai/2025/10/22/agent-lightning.html) vLLM blog. See also [Zhihu writeup](https://zhuanlan.zhihu.com/p/1965067274642785725).
- 8/11/2025 [Training AI Agents to Write and Self-correct SQL with Reinforcement Learning](https://medium.com/@yugez/training-ai-agents-to-write-and-self-correct-sql-with-reinforcement-learning-571ed31281ad) Medium.
@@ -57,6 +58,7 @@ To start using Agent-lightning, check out our [documentation](https://microsoft.
- [DeepWerewolf](https://github.com/af-74413592/DeepWerewolf) — A case study of agent RL training for the Chinese Werewolf game built with AgentScope and Agent Lightning.
- [AgentFlow](https://agentflow.stanford.edu/) — A modular multi-agent framework that combines planner, executor, verifier, and generator agents with the Flow-GRPO algorithm to tackle long-horizon, sparse-reward tasks.
- [Youtu-Agent](https://github.com/TencentCloudADP/Youtu-agent) — Youtu-Agent lets you build and train your agent with ease. Built with [a modified branch](https://github.com/microsoft/agent-lightning/tree/contrib/youtu-agent-lightning) of Agent Lightning, Youtu-Agent has verified up to 128 GPUs RL training on maths/code and search capabilities with steady convergence. Also check [the recipe](https://github.com/TencentCloudADP/youtu-agent/tree/rl/agl) and their blog [*Stop Wrestling with Your Agent RL: How Youtu-Agent Achieved Stable, 128-GPU Scaling Without Breaking a Sweat*](https://spotted-coconut-df8.notion.site/Stop-Wrestling-with-Your-Agent-RL-How-Youtu-Agent-Achieved-Stable-128-GPU-Scaling-Without-Breaking-2ca5e8f089ba80539a98c582b65e0233).
## ⚡ Architecture
+1 -1
View File
@@ -1,6 +1,6 @@
# Copyright (c) Microsoft. All rights reserved.
__version__ = "0.3.0"
__version__ = "0.3.1"
from .adapter import *
from .algorithm import *
+186 -16
View File
@@ -12,13 +12,56 @@ from opentelemetry.sdk.trace import ReadableSpan
from pydantic import BaseModel
from agentlightning.emitter.reward import get_reward_value
from agentlightning.semconv import AGL_OPERATION, AGL_REWARD, LightningSpanAttributes
from agentlightning.types import Span, Triplet
from agentlightning.utils.otel import filter_and_unflatten_attributes
from .base import TraceAdapter
logger = logging.getLogger(__name__)
def _attributes_get_multiple(attributes: Dict[str, Any], keys: List[str]) -> Optional[str]:
"""Get a string from the attributes, if present.
If there are multiple matches, the first one is returned.
"""
for key in keys:
if key in attributes:
if isinstance(attributes[key], str):
return attributes[key]
else:
logger.warning(f"Attribute {key} is found but is not a string: {attributes[key]}")
return None
def _attributes_get_ids_multiple(attributes: Dict[str, Any], keys: List[str]) -> Optional[List[int]]:
"""Get a list of integers from the attributes, if present.
If there are multiple matches, the first one is returned.
"""
for key in keys:
if key in attributes:
if (isinstance(attributes[key], list) or isinstance(attributes[key], tuple)) and all(
isinstance(x, int) for x in attributes[key]
):
return list(attributes[key])
else:
logger.warning(f"Attribute {key} is found but is not a list of integers: {attributes[key]}")
return None
def _attributes_unflatten_multiple(
attributes: Dict[str, Any], keys: List[str]
) -> Union[Dict[str, Any], List[Any], None]:
"""Unflatten the attributes, if present.
If there are multiple matches, the first one is returned.
"""
for key in keys:
result = filter_and_unflatten_attributes(attributes, key)
if result:
return result
return None
class Transition(BaseModel):
"""A single transition within a reinforcement learning trajectory.
@@ -131,7 +174,7 @@ class TraceTree:
if not should_visit(node):
return False
agent_name = node.agent_name()
vis_name = node.id[:8] + " (" + node.span.name + ")"
vis_name = node.id[-8:] + " (" + node.span.name + ")"
if agent_name is not None:
vis_name += " [" + agent_name + "]"
dot.node(node.id, vis_name) # type: ignore
@@ -308,6 +351,19 @@ class TraceTree:
if agent_name is not None:
return agent_name
# Case 6: Weave
is_agent_type = attributes.get("type") == "agent"
if is_agent_type:
agent_name = cast(Optional[str], attributes.get("agentlightning.operation.input.name"))
if agent_name is not None:
return agent_name
# Case 7: Weave + LangChain
if self.span.name.startswith("langchain.Chain."):
attributes_lc_name = cast(Optional[str], attributes.get("lc_name"))
if attributes_lc_name is not None:
return attributes_lc_name
def maybe_reward_dict(self) -> dict[str, Any]:
"""Return a reward payload if the span encodes one.
@@ -327,7 +383,17 @@ class TraceTree:
`True` when the span payload describes a reward, otherwise `False`.
"""
maybe_reward = self.maybe_reward_dict()
return maybe_reward and maybe_reward.get("type") == "reward" # type: ignore
if maybe_reward and maybe_reward.get("type") == "reward": # type: ignore
return True
# Agent-lightning 0.3+
if (
self.span.name == AGL_OPERATION
and self.span.attributes.get(LightningSpanAttributes.OPERATION_NAME.value) == AGL_REWARD
):
return True
return False
def find_llm_calls(
self,
@@ -364,7 +430,9 @@ class TraceTree:
is_llm_call = False
if is_llm_call:
# Check the response id
response_id: Optional[str] = self.span.attributes.get("gen_ai.response.id") # type: ignore
response_id = _attributes_get_multiple(
self.span.attributes, ["gen_ai.response.id", "agentlightning.operation.output.id"]
)
if response_id is None and within_llm_call is True:
is_llm_call = False
if (
@@ -376,7 +444,8 @@ class TraceTree:
if is_llm_call:
llm_calls.append((self, within_matching_subtree)) # type: ignore
existing_llm_call_response_ids = existing_llm_call_response_ids or set()
if existing_llm_call_response_ids is None:
existing_llm_call_response_ids = set()
if response_id is not None:
existing_llm_call_response_ids.add(response_id)
if within_llm_call is not None:
@@ -490,12 +559,12 @@ class TraceTree:
assign_to: List[Tuple[str, int]] = []
for child in item.children:
if child.id in llm_call_ids:
assign_to.append(child.id) # type: ignore
assign_to.append((child.id, child.end_time)) # type: ignore
agentops_output = item.maybe_reward_dict()
agentops_output = child.maybe_reward_dict()
if agentops_output and agentops_output.get("type") == "reward":
for assign_to_id, assign_to_end_time in reversed(assign_to):
if assign_to_end_time > item.start_time: # type: ignore
if assign_to_end_time > child.start_time: # type: ignore
# This reward happens before the end of the LLM call.
continue
if assign_to_id in rewards:
@@ -505,28 +574,129 @@ class TraceTree:
return rewards
def extract_prompt_image_urls(self, prompt_raw_content: Any) -> List[str]:
"""Extract image URLs from the span attributes, in order of appearance.
Args:
prompt_raw_content: The raw content of the prompt, which can be in one of several formats:
- List[dict]: A list of message entries, each being a dict with at least a "content" key.
- Dict[str, Any]: A dictionary, often with numeric string keys (e.g., `{"0": {...}, "1": {...}}`), where each value is a message entry.
If the dict does not have numeric keys, it is treated as a single message entry.
"""
message_entries: List[Any] = []
if isinstance(prompt_raw_content, list):
message_entries = cast(List[Any], prompt_raw_content)
elif isinstance(prompt_raw_content, dict):
# Common when the attributes expand to {"0": {...}, "prompt_filter_results": ...}
numeric_keys = [
key
for key in cast(Dict[str, Any], prompt_raw_content).keys()
if isinstance(key, str) and key.isdigit() # pyright: ignore[reportUnnecessaryIsInstance]
]
if numeric_keys:
for key in sorted(numeric_keys, key=int):
message_entries.append(prompt_raw_content[key])
else:
message_entries = [prompt_raw_content]
else:
return []
image_urls: List[str] = []
for message in cast(List[Dict[str, Any]], message_entries):
if (
not isinstance(message, dict) # pyright: ignore[reportUnnecessaryIsInstance]
or "content" not in message
):
continue
content = message["content"]
if isinstance(content, str):
try:
content = json.loads(content) # This content should now be a list
except json.JSONDecodeError:
logger.debug(f"Failed to parse message content as JSON: {content}")
continue
if isinstance(content, list):
for content_part in cast(List[Dict[str, Any]], content):
if not isinstance(content_part, dict): # pyright: ignore[reportUnnecessaryIsInstance]
continue
if content_part.get("type") == "image_url":
image_url_dict = cast(Dict[str, Any], content_part.get("image_url"))
if not isinstance(image_url_dict, dict): # pyright: ignore[reportUnnecessaryIsInstance]
continue
if "url" in image_url_dict:
image_urls.append(image_url_dict["url"])
return image_urls
def span_to_triplet(self, span: Span, agent_name: str) -> Triplet:
"""Convert a span to a triplet.
Subclass can override this method to add more fields to the triplet,
such as chat messages and tool calls.
"""
prompt_token_ids = span.attributes.get("prompt_token_ids", []) # type: ignore
response_token_ids = span.attributes.get("response_token_ids", []) # type: ignore
response_id = span.attributes.get("gen_ai.response.id", None) # type: ignore
prompt_token_ids = (
_attributes_get_ids_multiple(
span.attributes,
[
"prompt_token_ids",
"agentlightning.operation.output.prompt_token_ids", # Weave tracer
],
)
or []
)
response_token_ids = (
_attributes_get_ids_multiple(
span.attributes,
[
"response_token_ids",
"agentlightning.operation.output.response_token_ids.0", # Weave tracer
"agentlightning.operation.output.choices.0.token_ids", # Weave tracer with newer vLLM
"agentlightning.operation.output.choices.0.provider_specific_fields.token_ids", # new vLLM + new OpenAI client SDK
],
)
or []
)
response_id = _attributes_get_multiple(
span.attributes, ["gen_ai.response.id", "agentlightning.operation.output.id"]
)
request_metadata = _attributes_unflatten_multiple(
span.attributes, ["gen_ai.request", "agentlightning.operation.input"]
)
response_metadata = _attributes_unflatten_multiple(
span.attributes, ["gen_ai.response", "agentlightning.operation.output"]
)
# Special handling for Weave tracer: messages are handled separately
if isinstance(request_metadata, dict):
request_metadata.pop("messages", None)
if isinstance(response_metadata, dict):
response_metadata.pop("choices", None)
response_metadata.pop("prompt_token_ids", None)
response_metadata.pop("response_token_ids", None)
prompt_raw_content = _attributes_unflatten_multiple(
span.attributes, ["gen_ai.prompt", "agentlightning.operation.input.messages"]
)
completion_raw_content = _attributes_unflatten_multiple(
span.attributes, ["gen_ai.completion", "agentlightning.operation.output.choices"]
)
image_urls = self.extract_prompt_image_urls(prompt_raw_content)
prompt_payload = {"token_ids": prompt_token_ids, "raw_content": prompt_raw_content, "image_urls": image_urls}
response_payload = {"token_ids": response_token_ids, "raw_content": completion_raw_content}
# FIXME: logprob doesn't support Weave tracer yet.
logprobs_content = span.attributes.get("logprobs.content", None) # type: ignore
if isinstance(logprobs_content, str):
logprobs_content = json.loads(logprobs_content)
response: Dict[str, Any] = {"token_ids": response_token_ids, "logprobs": logprobs_content}
else:
response = {"token_ids": response_token_ids}
response_payload["logprobs"] = logprobs_content
return Triplet(
prompt={"token_ids": prompt_token_ids},
response=response,
prompt=prompt_payload,
response=response_payload,
reward=None,
metadata=dict(response_id=response_id, agent_name=agent_name),
metadata=dict(
request=request_metadata, response=response_metadata, response_id=response_id, agent_name=agent_name
),
)
def to_trajectory(
+37 -5
View File
@@ -7,23 +7,44 @@ APO with textual gradients that read rollout spans and outputs to modify the pro
- rollout: same pattern as your example, but task is a dict (T_task)
"""
from __future__ import annotations
import asyncio
import logging
import random
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Counter, Dict, Generic, Iterator, List, Optional, Sequence, Set, Tuple, TypedDict, TypeVar, cast
from typing import (
TYPE_CHECKING,
Any,
Counter,
Dict,
Generic,
Iterator,
List,
Optional,
Sequence,
Set,
Tuple,
TypedDict,
TypeVar,
cast,
)
import poml
from openai import AsyncOpenAI
from agentlightning.adapter.messages import TraceToMessages
from agentlightning.algorithm.base import Algorithm
from agentlightning.algorithm.utils import batch_iter_over_dataset
from agentlightning.algorithm.utils import batch_iter_over_dataset, with_llm_proxy, with_store
from agentlightning.reward import find_final_reward
from agentlightning.types import Dataset, NamedResources, PromptTemplate, Rollout, RolloutMode, RolloutStatus
if TYPE_CHECKING:
from agentlightning.llm_proxy import LLMProxy
from agentlightning.store.base import LightningStore
logger = logging.getLogger(__name__)
T_task = TypeVar("T_task")
@@ -91,6 +112,8 @@ class APO(Algorithm, Generic[T_task]):
beam_rounds: int = 3,
rollout_batch_timeout: float = 3600.0,
run_initial_validation: bool = True,
gradient_prompt_files: Optional[List[Path]] = None,
apply_edit_prompt_files: Optional[List[Path]] = None,
# Internal flags for debugging
_poml_trace: bool = False,
):
@@ -111,6 +134,8 @@ class APO(Algorithm, Generic[T_task]):
rollout_batch_timeout: Maximum time in seconds to wait for rollout batch completion.
run_initial_validation: If True, runs validation on the seed prompt before starting
optimization to establish a baseline score. Defaults to True.
gradient_prompt_files: Prompt templates used to compute textual gradients (critiques).
apply_edit_prompt_files: Prompt templates used to apply edits based on critiques.
"""
self.async_openai_client = async_openai_client
self.gradient_model = gradient_model
@@ -123,6 +148,8 @@ class APO(Algorithm, Generic[T_task]):
self.beam_rounds = beam_rounds
self.rollout_batch_timeout = rollout_batch_timeout
self.run_initial_validation = run_initial_validation
self.gradient_prompt_files = gradient_prompt_files or GRADIENT_PROMPT_FILES
self.apply_edit_prompt_files = apply_edit_prompt_files or APPLY_EDIT_PROMPT_FILES
self._history_best_prompt: Optional[PromptTemplate] = None
self._history_best_score: float = float("-inf")
@@ -249,7 +276,7 @@ class APO(Algorithm, Generic[T_task]):
Returns:
A textual critique generated by the LLM, or None if generation fails.
"""
tg_template = random.choice(GRADIENT_PROMPT_FILES)
tg_template = random.choice(self.gradient_prompt_files)
if len(rollout_results) < self.gradient_batch_size:
self._log(
@@ -331,7 +358,7 @@ class APO(Algorithm, Generic[T_task]):
return current_prompt.prompt_template.template
# 2) Apply edit
ae_template = random.choice(APPLY_EDIT_PROMPT_FILES)
ae_template = random.choice(self.apply_edit_prompt_files)
self._log(
logging.INFO,
f"Edit will be generated by {self.apply_edit_model} with template: {ae_template.name}",
@@ -360,8 +387,10 @@ class APO(Algorithm, Generic[T_task]):
)
return new_prompt
@with_store
async def get_rollout_results(
self,
store: LightningStore,
rollout: List[Rollout],
*,
prefix: Optional[str] = None,
@@ -379,7 +408,6 @@ class APO(Algorithm, Generic[T_task]):
List of rollout results formatted for APO processing.
"""
rollout_results: List[RolloutResultForAPO] = []
store = self.get_store()
adapter = self.get_adapter()
for r in rollout:
spans = await store.query_spans(r.rollout_id)
@@ -776,8 +804,12 @@ class APO(Algorithm, Generic[T_task]):
prefix=prefix,
)
@with_llm_proxy()
@with_store
async def run(
self,
store: LightningStore, # Injected by decorator - callers should not provide this parameter
llm_proxy: Optional[LLMProxy], # Injected by decorator - callers should not provide this parameter
train_dataset: Optional[Dataset[T_task]] = None,
val_dataset: Optional[Dataset[T_task]] = None,
) -> None:
+12 -3
View File
@@ -5,11 +5,16 @@ from __future__ import annotations
import asyncio
import logging
from datetime import datetime
from typing import Any, List, Literal, Optional
from typing import TYPE_CHECKING, Any, List, Literal, Optional
from agentlightning.types import Attempt, Dataset, Rollout, RolloutStatus, Span
from .base import Algorithm
from .utils import with_llm_proxy, with_store
if TYPE_CHECKING:
from agentlightning.llm_proxy import LLMProxy
from agentlightning.store.base import LightningStore
logger = logging.getLogger(__name__)
@@ -36,6 +41,8 @@ class Baseline(FastAlgorithm):
finish, and logs every collected span and reward. It is primarily useful as
a smoke test for the platform plumbing rather than a performant trainer.
The baseline algorithm will auto-start a LLM proxy if one is provided and not yet started.
Args:
n_epochs: Number of dataset passes to execute for both the train and val
splits during developer experiments.
@@ -180,8 +187,12 @@ class Baseline(FastAlgorithm):
await asyncio.sleep(self.polling_interval)
@with_llm_proxy()
@with_store
async def run(
self,
store: LightningStore, # Injected by decorator - callers should not provide this parameter
llm_proxy: Optional[LLMProxy], # Injected by decorator - callers should not provide this parameter
train_dataset: Optional[Dataset[Any]] = None,
val_dataset: Optional[Dataset[Any]] = None,
) -> None:
@@ -202,8 +213,6 @@ class Baseline(FastAlgorithm):
logger.debug(f"Train indices: {train_indices}")
logger.debug(f"Val indices: {val_indices}")
store = self.get_store()
# Currently we only supports a single resource update at the start.
initial_resources = self.get_initial_resources()
if initial_resources is not None:
+135 -1
View File
@@ -1,11 +1,42 @@
# Copyright (c) Microsoft. All rights reserved.
from __future__ import annotations
import functools
import logging
import random
from typing import Iterator, List, Sequence, TypeVar
from collections.abc import Coroutine
from typing import (
TYPE_CHECKING,
Any,
Callable,
Concatenate,
Iterator,
List,
Literal,
Optional,
ParamSpec,
Sequence,
TypeVar,
overload,
)
from agentlightning.types import Dataset
if TYPE_CHECKING:
from agentlightning.llm_proxy import LLMProxy
from agentlightning.store.base import LightningStore
from .base import Algorithm
T_task = TypeVar("T_task")
T_algo = TypeVar("T_algo", bound="Algorithm")
P = ParamSpec("P")
R = TypeVar("R")
logger = logging.getLogger(__name__)
def batch_iter_over_dataset(dataset: Dataset[T_task], batch_size: int) -> Iterator[Sequence[T_task]]:
@@ -41,3 +72,106 @@ def batch_iter_over_dataset(dataset: Dataset[T_task], batch_size: int) -> Iterat
if len(current_batch) == batch_size:
yield [dataset[index] for index in current_batch]
current_batch = []
def with_store(
func: Callable[Concatenate[T_algo, LightningStore, P], Coroutine[Any, Any, R]],
) -> Callable[Concatenate[T_algo, P], Coroutine[Any, Any, R]]:
"""Inject the algorithm's `LightningStore` into coroutine methods.
The decorator calls `Algorithm.get_store()` once per invocation and passes the
resulting store as an explicit argument to the wrapped coroutine. Decorated
methods therefore receive the resolved store even when invoked by helper
utilities rather than directly by the algorithm.
Args:
func: The coroutine that expects `(self, store, *args, **kwargs)`.
Returns:
A coroutine wrapper that automatically retrieves the store and forwards it
to `func`.
"""
@functools.wraps(func)
async def wrapper(self: T_algo, *args: P.args, **kwargs: P.kwargs) -> R:
store = self.get_store()
return await func(self, store, *args, **kwargs)
return wrapper
@overload
def with_llm_proxy(
required: Literal[False] = False,
auto_start: bool = True,
) -> Callable[
[Callable[Concatenate[T_algo, Optional[LLMProxy], P], Coroutine[Any, Any, R]]],
Callable[Concatenate[T_algo, P], Coroutine[Any, Any, R]],
]: ...
@overload
def with_llm_proxy(
required: Literal[True],
auto_start: bool = True,
) -> Callable[
[Callable[Concatenate[T_algo, LLMProxy, P], Coroutine[Any, Any, R]]],
Callable[Concatenate[T_algo, P], Coroutine[Any, Any, R]],
]: ...
def with_llm_proxy(
required: bool = False,
auto_start: bool = True,
) -> Callable[
[Callable[..., Coroutine[Any, Any, Any]]],
Callable[..., Coroutine[Any, Any, Any]],
]:
"""Resolve and optionally lifecycle-manage the configured LLM proxy.
Args:
required: When True, raises `ValueError` if the algorithm does not have an
[`LLMProxy`][agentlightning.LLMProxy] set. When False, the wrapped coroutine receives
`None` if no proxy is available.
auto_start: When True, [`LLMProxy.start()`][agentlightning.LLMProxy.start] is invoked if the proxy is not
already running before calling `func` and [`LLMProxy.stop()`][agentlightning.LLMProxy.stop] is
called afterwards.
Returns:
A decorator that injects the [`LLMProxy`][agentlightning.LLMProxy] (or `None`) as the first
argument after `self` and manages automatic startup/shutdown when requested.
"""
def decorator(
func: Callable[..., Coroutine[Any, Any, Any]],
) -> Callable[..., Coroutine[Any, Any, Any]]:
@functools.wraps(func)
async def wrapper(self: Algorithm, *args: Any, **kwargs: Any) -> Any:
llm_proxy = self.get_llm_proxy()
if required and llm_proxy is None:
raise ValueError(
"LLM proxy is required but not configured. Call set_llm_proxy() before using this method."
)
auto_started = False
if auto_start and llm_proxy is not None:
if llm_proxy.is_running():
logger.info("Proxy is already running, skipping start")
else:
logger.info("Starting proxy, managed by the algorithm")
await llm_proxy.start()
auto_started = True
try:
# At type level, overloads guarantee that if `required=True`
# then `func` expects a non-optional LLMProxy.
return await func(self, llm_proxy, *args, **kwargs)
finally:
if auto_started and llm_proxy is not None:
logger.info("Stopping proxy, managed by the algorithm")
await llm_proxy.stop()
return wrapper
return decorator
+50 -2
View File
@@ -1,6 +1,8 @@
# Copyright (c) Microsoft. All rights reserved.
from typing import Any, Optional
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Optional, Type
from hydra import compose, initialize
from omegaconf import OmegaConf
@@ -10,6 +12,10 @@ from agentlightning.client import AgentLightningClient
from agentlightning.types import Dataset
from agentlightning.verl.entrypoint import run_ppo # type: ignore
if TYPE_CHECKING:
from agentlightning.verl.daemon import AgentModeDaemon
from agentlightning.verl.trainer import AgentLightningTrainer
class VERL(Algorithm):
"""VERL-powered algorithm that delegates training to the VERL PPO runner.
@@ -23,6 +29,32 @@ class VERL(Algorithm):
config: Dictionary mirroring the overrides passed to the VERL CLI. The
overrides are merged with VERL's packaged defaults via Hydra before
launching training.
trainer_cls: Optional override for the trainer class. Experimental.
daemon_cls: Optional override for the daemon class. Experimental.
!!! note "Trajectory aggregation (experimental)"
Trajectory-level aggregation merges an entire multi-turn rollout into a single,
masked training sample so GPU time is spent once per trajectory rather than N times
per turn. Enable it via:
```python
config["agentlightning"]["trace_aggregator"] = {
"level": "trajectory",
"trajectory_max_prompt_length": 4096,
"trajectory_max_response_length": 34384,
}
```
Keep conversations structured (message lists rather than manual string
concatenation) so prefix matching can stitch traces. `trajectory_max_prompt_length`
should be set to the maximum length of the prompt for the first turn, and
`trajectory_max_response_length` should be set to the maximum cumulative
length of agent responses in the full trajectory.
Toggle `debug=True` plus `mismatch_log_dir` when you need to inspect
retokenization or chat-template mismatches. See
[this blog post](https://agent-lightning.github.io/posts/trajectory_level_aggregation/)
for more details.
Examples:
```python
@@ -90,7 +122,12 @@ class VERL(Algorithm):
```
"""
def __init__(self, config: dict[str, Any]):
def __init__(
self,
config: dict[str, Any],
trainer_cls: Optional[Type[AgentLightningTrainer]] = None,
daemon_cls: Optional[Type[AgentModeDaemon]] = None,
):
super().__init__()
# Compose the base config exactly like your decorator:
@@ -102,6 +139,8 @@ class VERL(Algorithm):
# Allow adding new fields
OmegaConf.set_struct(base_cfg, False)
self.config = OmegaConf.merge(base_cfg, override_conf)
self.trainer_cls = trainer_cls
self.daemon_cls = daemon_cls
def run(
self,
@@ -119,6 +158,11 @@ class VERL(Algorithm):
adapter have been garbage-collected when using the V1 execution
mode.
"""
from agentlightning.verl.daemon import AgentModeDaemon
from agentlightning.verl.trainer import AgentLightningTrainer
trainer_cls = self.trainer_cls or AgentLightningTrainer
daemon_cls = self.daemon_cls or AgentModeDaemon
try:
store = self.get_store()
except Exception:
@@ -130,6 +174,8 @@ class VERL(Algorithm):
store=None,
llm_proxy=None,
adapter=None,
trainer_cls=trainer_cls,
daemon_cls=daemon_cls,
)
else:
print("Store is set. Assuming v1 execution mode.")
@@ -142,6 +188,8 @@ class VERL(Algorithm):
store=store,
llm_proxy=llm_proxy,
adapter=adapter,
trainer_cls=trainer_cls,
daemon_cls=daemon_cls,
)
def get_client(self) -> AgentLightningClient:
+1
View File
@@ -12,6 +12,7 @@ from typing import Dict, Iterable, Tuple
_SUBCOMMANDS: Dict[str, Tuple[str, str]] = {
"vllm": ("agentlightning.cli.vllm", "Run the vLLM CLI with Agent Lightning instrumentation."),
"store": ("agentlightning.cli.store", "Run a LightningStore server."),
"prometheus": ("agentlightning.cli.prometheus", "Serve Prometheus metrics from the multiprocess registry."),
"agentops": ("agentlightning.cli.agentops_server", "Start the AgentOps server manager."),
}
+115
View File
@@ -0,0 +1,115 @@
# Copyright (c) Microsoft. All rights reserved.
"""Serve Prometheus metrics from the Agent Lightning multiprocess registry."""
from __future__ import annotations
import argparse
import asyncio
import logging
import os
from pathlib import Path
from typing import Iterable
from fastapi import FastAPI
from prometheus_client import make_asgi_app # pyright: ignore[reportUnknownVariableType]
from agentlightning.logging import setup as setup_logging
from agentlightning.utils.metrics import get_prometheus_registry
from agentlightning.utils.server_launcher import PythonServerLauncher, PythonServerLauncherArgs
logger = logging.getLogger(__name__)
def ensure_prometheus_dir() -> str:
"""Ensure PROMETHEUS_MULTIPROC_DIR is set and the directory exists."""
directory = os.getenv("PROMETHEUS_MULTIPROC_DIR")
if directory is None:
raise ValueError("PROMETHEUS_MULTIPROC_DIR is not set.")
Path(directory).mkdir(parents=True, exist_ok=True)
logger.info("Serving Prometheus multiprocess metrics from %s", directory)
return directory
def create_prometheus_app(metrics_path: str = "/v1/prometheus") -> FastAPI:
"""Create a FastAPI app that exposes Prometheus metrics and a health endpoint.
Args:
metrics_path: URL path to expose the Prometheus metrics endpoint on.
Returns:
A FastAPI application ready to serve metrics.
"""
if not metrics_path.startswith("/"):
raise ValueError("metrics_path must start with '/'.")
normalized_path = metrics_path.rstrip("/")
if normalized_path in ("", "/"):
raise ValueError("metrics_path must not be '/'. Choose a sub-path such as /v1/prometheus.")
app = FastAPI(title="Agent Lightning Prometheus exporter", docs_url=None, redoc_url=None)
metrics_app = make_asgi_app(registry=get_prometheus_registry()) # pyright: ignore[reportUnknownVariableType]
app.mount(normalized_path, metrics_app) # pyright: ignore[reportUnknownArgumentType]
@app.get("/health")
async def healthcheck() -> dict[str, str]: # pyright: ignore[reportUnusedFunction]
return {"status": "ok"}
return app
def main(argv: Iterable[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Serve Prometheus metrics outside the LightningStore server.")
parser.add_argument("--host", default="0.0.0.0", help="Host to bind the metrics server to.")
parser.add_argument("--port", type=int, default=4748, help="Port to expose the Prometheus metrics on.")
parser.add_argument(
"--metrics-path",
default="/v1/prometheus",
help="HTTP path used to expose metrics. Must start with '/' and not be the root path.",
)
parser.add_argument(
"--log-level",
default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
help="Configure the logging level for the metrics server.",
)
parser.add_argument(
"--access-log",
action="store_true",
help="Enable uvicorn access logs. Disabled by default to reduce noise.",
)
args = parser.parse_args(list(argv) if argv is not None else None)
setup_logging(args.log_level)
ensure_prometheus_dir()
try:
app = create_prometheus_app(args.metrics_path)
except ValueError as exc:
logger.error("Failed to configure prometheus app: %s", exc)
return 1
launcher_args = PythonServerLauncherArgs(
host=args.host,
port=args.port,
log_level=getattr(logging, args.log_level),
access_log=args.access_log,
healthcheck_url="/health",
)
launcher = PythonServerLauncher(app, launcher_args)
try:
asyncio.run(launcher.run_forever())
except KeyboardInterrupt:
logger.info("Received shutdown signal. Stopping Prometheus server.")
except RuntimeError as exc:
logger.error("Prometheus server failed to start: %s", exc, exc_info=True)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())
+38 -8
View File
@@ -7,11 +7,18 @@ from __future__ import annotations
import argparse
import asyncio
import logging
from typing import Iterable
from typing import Iterable, List
from agentlightning import setup_logging
from agentlightning.store.client_server import LightningStoreServer
from agentlightning.store.memory import InMemoryLightningStore
from agentlightning.utils.metrics import (
ConsoleMetricsBackend,
MetricsBackend,
MultiMetricsBackend,
PrometheusMetricsBackend,
setup_multiprocess_prometheus,
)
logger = logging.getLogger(__name__)
@@ -33,9 +40,10 @@ def main(argv: Iterable[str] | None = None) -> int:
help="Configure the logging level for the store.",
)
parser.add_argument(
"--prometheus",
action="store_true",
help="Enable Prometheus metrics.",
"--tracker",
nargs="+",
choices=["prometheus", "console"],
help="Enable metrics tracking. Repeat for multiple trackers.",
)
parser.add_argument(
"--n-workers",
@@ -63,14 +71,36 @@ def main(argv: Iterable[str] | None = None) -> int:
setup_logging(args.log_level)
trackers: List[MetricsBackend] = []
if args.tracker:
if "prometheus" in args.tracker:
logger.info("Enabling Prometheus metrics tracking.")
if args.n_workers > 1:
# This has to be done before prometheus_client is imported
setup_multiprocess_prometheus()
logger.info("Setting up Prometheus multiprocess directory for metrics tracking.")
trackers.append(PrometheusMetricsBackend())
if "console" in args.tracker:
logger.info("Enabling console metrics tracking.")
trackers.append(ConsoleMetricsBackend())
if len(trackers) == 0:
tracker: MetricsBackend | None = None
elif len(trackers) == 1:
tracker = trackers[0]
else:
tracker = MultiMetricsBackend(trackers)
if args.backend == "memory":
store = InMemoryLightningStore(
prometheus=args.prometheus, thread_safe=True
) # Using thread_safe store for server
thread_safe=True, # Using thread_safe store for server
tracker=tracker,
)
elif args.backend == "mongo":
from agentlightning.store.mongo import MongoLightningStore
store = MongoLightningStore(client=args.mongo_uri, prometheus=args.prometheus)
store = MongoLightningStore(mongo_uri=args.mongo_uri, tracker=tracker)
else:
raise ValueError(f"Invalid backend: {args.backend}")
@@ -86,7 +116,7 @@ def main(argv: Iterable[str] | None = None) -> int:
port=args.port,
cors_allow_origins=args.cors_origins,
launch_mode=launch_mode,
prometheus=args.prometheus,
tracker=tracker,
n_workers=args.n_workers,
)
try:
+11
View File
@@ -1,5 +1,16 @@
# Copyright (c) Microsoft. All rights reserved.
"""Convenient helpers for creating spans / traces.
All emitters operate in two modes, switchable via the `propagate` parameter.
The emitters first [`SpanCreationRequest`][agentlightning.SpanCreationRequest] object, then:
1. When `propagate` is True, this creation request will be propagated to the active tracer
and a [`Span`][agentlightning.Span] instance will be created (possibly deferred).
2. When `propagate` is False, the creation request will be returned directly. Useful for cases
when you don't have a tracer but you want to create a creation request for later use.
"""
from .annotation import emit_annotation, operation
from .exception import emit_exception
from .message import emit_message, get_message_value
+159 -153
View File
@@ -5,7 +5,6 @@
import asyncio
import functools
import inspect
import json
import logging
from types import TracebackType
from typing import (
@@ -22,19 +21,18 @@ from typing import (
overload,
)
from opentelemetry import trace
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.trace import Status, StatusCode
from agentlightning.semconv import AGL_ANNOTATION, AGL_OPERATION, LightningSpanAttributes
from agentlightning.utils.otel import flatten_attributes, get_tracer
from agentlightning.tracer.base import get_active_tracer
from agentlightning.tracer.dummy import DummyTracer
from agentlightning.types import SpanCoreFields, SpanRecordingContext, TraceStatus
from agentlightning.utils.otel import check_attributes_sanity, flatten_attributes, sanitize_attributes
_FnType = TypeVar("_FnType", bound=Callable[..., Any])
logger = logging.getLogger(__name__)
def emit_annotation(annotation: Dict[str, Any], propagate: bool = True) -> ReadableSpan:
def emit_annotation(annotation: Dict[str, Any], propagate: bool = True) -> SpanCoreFields:
"""Emit a new annotation span.
This is the underlying implementation of [`emit_reward`][agentlightning.emit_reward].
@@ -48,62 +46,46 @@ def emit_annotation(annotation: Dict[str, Any], propagate: bool = True) -> Reada
Args:
annotation: Dictionary containing annotation key-value pairs.
Representatives are rewards, tags, and metadata.
propagate: Whether to propagate the span to exporters automatically.
propagate: Whether to propagate the span to tracers automatically.
"""
annotation_attributes = flatten_attributes(annotation)
if any(not isinstance(v, (str, int, float, bool, bytes)) for v in annotation_attributes.values()):
raise TypeError("All annotation attributes must be primitive types (str, int, float, bool, bytes)")
annotation_attributes = flatten_attributes(annotation, expand_leaf_lists=False)
check_attributes_sanity(annotation_attributes)
sanitized_attributes = sanitize_attributes(annotation_attributes)
logger.debug("Emitting annotation span with keys %s", sanitized_attributes.keys())
# TODO: this should use a tracer from current context rather than the singleton
tracer = get_tracer(use_active_span_processor=propagate)
span = tracer.start_span(
AGL_ANNOTATION,
attributes=annotation_attributes,
if propagate:
tracer = get_active_tracer()
if tracer is None:
raise RuntimeError("No active tracer found. Cannot emit annotation span.")
else:
tracer = DummyTracer()
return tracer.create_span(
name=AGL_ANNOTATION,
attributes=sanitized_attributes,
status=TraceStatus(status_code="OK"),
)
logger.debug("Emitting annotation span with keys %s", annotation_attributes)
with span:
pass
if not isinstance(span, ReadableSpan):
raise ValueError(f"Span is not a ReadableSpan: {span}")
return span
def _safe_json_dump(obj: Any) -> str:
"""Serialize an object to JSON, falling back to ``str(obj)`` if needed.
Args:
obj: Object to be serialized.
Returns:
The JSON-encoded string representation of the object, or its string
representation if JSON encoding fails.
"""
try:
return json.dumps(obj, default=str, ensure_ascii=False)
except Exception:
return str(obj)
class OperationContext:
"""Context manager and decorator for tracing operations.
This class manages an OpenTelemetry span for a logical unit of work. It can
be used either:
This class manages a tracer-backed span for a logical unit of work. It can be
used either:
* As a decorator, in which case inputs and outputs are inferred
automatically from the wrapped function's signature.
* As a context manager, in which case inputs and outputs can be recorded
explicitly via :meth:`set_input` and :meth:`set_output`.
explicitly via [`set_input`][agentlightning.emitter.annotation.OperationContext.set_input]
and [`set_output`][agentlightning.emitter.annotation.OperationContext.set_output].
Attributes:
name: Human-readable span name.
initial_attributes: Attributes applied when the span is created.
tracer: OpenTelemetry tracer used to create spans.
span: The currently active span, if any.
tracer: Tracer implementation used to create spans.
"""
def __init__(self, name: str, attributes: Dict[str, Any], *, propagate: bool = True) -> None:
def __init__(self, name: str, attributes: Dict[str, Any], propagate: bool = True) -> None:
"""Initialize a new operation context.
Args:
@@ -112,12 +94,19 @@ class OperationContext:
JSON-serialized where necessary.
propagate: Whether the span should be sent to active exporters.
"""
self.name: str = name
self.initial_attributes: Dict[str, Any] = attributes
self.propagate: bool = propagate
self.tracer: trace.Tracer = get_tracer(use_active_span_processor=propagate)
self.span: Optional[trace.Span] = None
self._ctx_token: Optional[ContextManager[Any]] = None
self.name = name
self.initial_attributes = flatten_attributes(attributes, expand_leaf_lists=False)
self.propagate = propagate
if propagate:
tracer = get_active_tracer()
if tracer is None:
raise RuntimeError("No active tracer found. Cannot trace operation spans.")
self.tracer = tracer
else:
self.tracer = DummyTracer()
self._ctx_manager: Optional[ContextManager[SpanRecordingContext]] = None
self._recording_context: Optional[SpanRecordingContext] = None
self._span: Optional[SpanCoreFields] = None
def __enter__(self) -> "OperationContext":
"""Enter the context manager and start a new span.
@@ -125,15 +114,10 @@ class OperationContext:
Returns:
The current :class:`OperationContext` instance with an active span.
"""
# 1. Start the span with initial attributes (JSON serialized)
sanitized_attrs = {
k: _safe_json_dump(v) if not isinstance(v, (str, int, float, bool)) else v
for k, v in self.initial_attributes.items()
}
self.span = self.tracer.start_span(self.name, attributes=sanitized_attrs)
self._ctx_token = trace.use_span(self.span, end_on_exit=True)
self._ctx_token.__enter__()
sanitized_attrs = sanitize_attributes(self.initial_attributes)
self._ctx_manager = self.tracer.operation_context(self.name, attributes=sanitized_attrs)
recording_context = self._ctx_manager.__enter__()
self._recording_context = recording_context
return self
def __exit__(
@@ -142,57 +126,63 @@ class OperationContext:
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
"""Exit the context manager and finish the span.
"""Exit the context manager and finish the span."""
if self._ctx_manager:
self._ctx_manager.__exit__(exc_type, exc_val, exc_tb)
if self._recording_context:
self._span = self._recording_context.get_recorded_span()
self._ctx_manager = None
self._recording_context = None
Any exception raised inside the context is recorded on the span and the
span status is set to error.
Args:
exc_type: Exception type, if an exception occurred.
exc_val: Exception instance, if an exception occurred.
exc_tb: Traceback object, if an exception occurred.
"""
# 1. Record Exception if present
if exc_val and self.span:
self.span.record_exception(exc_val)
self.span.set_status(Status(StatusCode.ERROR, str(exc_val)))
# 2. Close span
if self._ctx_token:
self._ctx_token.__exit__(exc_type, exc_val, exc_tb)
def span(self) -> SpanCoreFields:
"""Get the span that was created by this context manager."""
if self._span is None:
raise RuntimeError("Span is not ready yet.")
return self._span
def set_input(self, *args: Any, **kwargs: Any) -> None:
"""Record input arguments on the current span.
Positional arguments are stored under the ``input.args`` attribute,
and keyword arguments are stored under ``input.<name>`` attributes.
Positional arguments are stored under the `input.args.<index>` attributes,
and keyword arguments are stored under `input.<name>` attributes.
This is intended for use inside a ``with operation(...) as op`` block.
This is intended for use inside a `with operation(...) as op` block.
Args:
*args: Positional arguments to record.
**kwargs: Keyword arguments to record.
"""
if not self.span:
return
if not self._recording_context:
raise RuntimeError("No recording context found. Cannot set input.")
prefix = LightningSpanAttributes.OPERATION_INPUT.value
attributes: Dict[str, Any] = {}
if args:
self.span.set_attribute("input.args", _safe_json_dump(args))
for idx, value in enumerate(args):
flattened = flatten_attributes({str(idx): value})
for nested_key, nested_value in flattened.items():
attributes[f"{prefix}.args.{nested_key}"] = nested_value
if kwargs:
for k, v in kwargs.items():
self.span.set_attribute(f"input.{k}", _safe_json_dump(v))
for key, value in kwargs.items():
flattened = flatten_attributes({key: value})
for nested_key, nested_value in flattened.items():
attributes[f"{prefix}.{nested_key}"] = nested_value
if attributes:
self._recording_context.record_attributes(sanitize_attributes(attributes))
def set_output(self, output: Any) -> None:
"""Record the output value on the current span.
This is intended for use inside a ``with operation(...) as op`` block.
This is intended for use inside a `with operation(...) as op` block.
Args:
output: The output value to record.
"""
if not self.span:
return
self.span.set_attribute("output", _safe_json_dump(output))
if not self._recording_context:
raise RuntimeError("No recording context found. Cannot set output.")
flattened = flatten_attributes({LightningSpanAttributes.OPERATION_OUTPUT.value: output})
self._recording_context.record_attributes(sanitize_attributes(flattened))
def __call__(self, fn: _FnType) -> _FnType:
"""Wrap a callable so its execution is traced in a span.
@@ -212,60 +202,60 @@ class OperationContext:
sig = inspect.signature(fn)
def _record_auto_inputs(span: trace.Span, args: Tuple[Any, ...], kwargs: Dict[str, Any]) -> None:
"""Bind arguments to signature and log them on the span.
sanitized_init_attrs = sanitize_attributes(
{LightningSpanAttributes.OPERATION_NAME.value: function_name, **self.initial_attributes}
)
Args:
span: Span on which to record attributes.
args: Positional arguments passed to the wrapped callable.
kwargs: Keyword arguments passed to the wrapped callable.
"""
def _record_auto_inputs(
recording_ctx: SpanRecordingContext, args: Tuple[Any, ...], kwargs: Dict[str, Any]
) -> None:
"""Bind arguments to signature and log them on the span."""
attributes: Dict[str, Any] = {}
try:
bound = sig.bind(*args, **kwargs)
bound.apply_defaults()
for k, v in bound.arguments.items():
span.set_attribute(
f"{LightningSpanAttributes.OPERATION_INPUT.value}.{k}",
_safe_json_dump(v),
)
for name, value in bound.arguments.items():
parameter = sig.parameters.get(name)
if parameter and parameter.kind is inspect.Parameter.VAR_POSITIONAL:
attr_prefix = f"{LightningSpanAttributes.OPERATION_INPUT.value}.{name}"
for idx, item in enumerate(value):
flattened = flatten_attributes({str(idx): item})
for nested_key, nested_value in flattened.items():
attributes[f"{attr_prefix}.{nested_key}"] = nested_value
else:
flattened = flatten_attributes({name: value})
for nested_key, nested_value in flattened.items():
attributes[f"{LightningSpanAttributes.OPERATION_INPUT.value}.{nested_key}"] = nested_value
except Exception:
span.set_attribute(
f"{LightningSpanAttributes.OPERATION_INPUT.value}.args",
_safe_json_dump(args),
)
span.set_attribute(
f"{LightningSpanAttributes.OPERATION_INPUT.value}.kwargs",
_safe_json_dump(kwargs),
)
if args:
for idx, value in enumerate(args):
flattened = flatten_attributes({str(idx): value})
for nested_key, nested_value in flattened.items():
attributes[f"{LightningSpanAttributes.OPERATION_INPUT.value}.args.{nested_key}"] = (
nested_value
)
if kwargs:
flattened = flatten_attributes({"kwargs": kwargs})
for nested_key, nested_value in flattened.items():
attributes[f"{LightningSpanAttributes.OPERATION_INPUT.value}.{nested_key}"] = nested_value
if attributes:
recording_ctx.record_attributes(sanitize_attributes(attributes))
def _record_auto_outputs(recording_ctx: SpanRecordingContext, result: Any) -> None:
"""Record the output value on the span."""
flattened = flatten_attributes({LightningSpanAttributes.OPERATION_OUTPUT.value: result})
recording_ctx.record_attributes(sanitize_attributes(flattened))
if asyncio.iscoroutinefunction(fn) or inspect.iscoroutinefunction(fn):
@functools.wraps(fn)
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
"""Async wrapper that traces the wrapped coroutine."""
# Reuse __enter__ logic via 'with self' would share state incorrectly
# across concurrent calls. We must create a new span per call.
# So we manually reimplement the span logic for the wrapper here.
sanitized_attrs = {
k: _safe_json_dump(v) if not isinstance(v, (str, int, float, bool)) else v
for k, v in self.initial_attributes.items()
}
with self.tracer.start_as_current_span(self.name, attributes=sanitized_attrs) as span:
span.set_attribute(LightningSpanAttributes.OPERATION_NAME.value, function_name)
_record_auto_inputs(span, args, kwargs)
try:
result = await fn(*args, **kwargs)
span.set_attribute(
LightningSpanAttributes.OPERATION_OUTPUT.value,
_safe_json_dump(result),
)
return result
except Exception as e:
span.record_exception(e)
span.set_status(Status(StatusCode.ERROR, str(e)))
raise
with self.tracer.operation_context(self.name, attributes=sanitized_init_attrs) as recording_ctx:
_record_auto_inputs(recording_ctx, args, kwargs)
result = await fn(*args, **kwargs)
_record_auto_outputs(recording_ctx, result)
return result
return cast(_FnType, async_wrapper)
@@ -274,41 +264,48 @@ class OperationContext:
@functools.wraps(fn)
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
"""Sync wrapper that traces the wrapped callable."""
sanitized_attrs = {
k: _safe_json_dump(v) if not isinstance(v, (str, int, float, bool)) else v
for k, v in self.initial_attributes.items()
}
with self.tracer.start_as_current_span(self.name, attributes=sanitized_attrs) as span:
span.set_attribute(LightningSpanAttributes.OPERATION_NAME.value, function_name)
_record_auto_inputs(span, args, kwargs)
try:
result = fn(*args, **kwargs)
span.set_attribute(
LightningSpanAttributes.OPERATION_OUTPUT.value,
_safe_json_dump(result),
)
return result
except Exception as e:
span.record_exception(e)
span.set_status(Status(StatusCode.ERROR, str(e)))
raise
with self.tracer.operation_context(self.name, attributes=sanitized_init_attrs) as recording_ctx:
_record_auto_inputs(recording_ctx, args, kwargs)
result = fn(*args, **kwargs)
_record_auto_outputs(recording_ctx, result)
return result
return cast(_FnType, sync_wrapper)
@overload
def operation(fn: _FnType, *, propagate: bool = True, **additional_attributes: Any) -> _FnType: ...
def operation(
fn: _FnType, *, propagate: bool = True, name: Optional[str] = None, **additional_attributes: Any
) -> _FnType: ...
@overload
def operation(*, propagate: bool = True, **additional_attributes: Any) -> OperationContext: ...
def operation(
*, propagate: bool = True, name: Optional[str] = None, **additional_attributes: Any
) -> OperationContext: ...
@overload
def operation(fn: _FnType, *, name: Optional[str] = None, **additional_attributes: Any) -> _FnType: ...
@overload
def operation(*, name: Optional[str] = None, **additional_attributes: Any) -> OperationContext: ...
@overload
def operation(fn: _FnType, **additional_attributes: Any) -> _FnType: ...
@overload
def operation(**additional_attributes: Any) -> OperationContext: ...
def operation(
fn: Optional[_FnType] = None,
*,
propagate: bool = True,
name: Optional[str] = None,
**additional_attributes: Any,
) -> Union[_FnType, OperationContext]:
"""Entry point for tracking operations.
@@ -344,6 +341,9 @@ def operation(
left as `None`) and only keyword attributes are provided.
propagate: Whether spans should use the active span processor. When False,
spans will stay local and not be exported.
name: Optional alias that populates
[`LightningSpanAttributes.OPERATION_NAME`][agentlightning.semconv.LightningSpanAttributes.OPERATION_NAME]
when `additional_attributes` does not already define it.
**additional_attributes: Additional span attributes to attach at
creation time.
@@ -352,6 +352,12 @@ def operation(
[`OperationContext`][agentlightning.emitter.annotation.OperationContext]
(when used as a context manager factory).
"""
if name is not None:
if LightningSpanAttributes.OPERATION_NAME.value in additional_attributes:
raise ValueError("Cannot specify both `name` and `additional_attributes.operation_name`.")
additional_attributes[LightningSpanAttributes.OPERATION_NAME.value] = name
# Case 1: Used as @operation (bare decorator or with attributes)
if callable(fn):
# Create context with fixed name, then immediately wrap the function
+18 -20
View File
@@ -1,13 +1,13 @@
# Copyright (c) Microsoft. All rights reserved.
import logging
import traceback
from typing import Any, Dict, Optional
from opentelemetry.semconv.attributes import exception_attributes
from agentlightning.semconv import AGL_EXCEPTION
from agentlightning.utils.otel import get_tracer
from agentlightning.tracer.base import get_active_tracer
from agentlightning.tracer.dummy import DummyTracer
from agentlightning.types import TraceStatus
from agentlightning.utils.otel import flatten_attributes, format_exception_attributes, sanitize_attributes
logger = logging.getLogger(__name__)
@@ -32,25 +32,23 @@ def emit_exception(
"""
if not isinstance(exception, BaseException): # type: ignore
raise TypeError(f"Expected a BaseException instance, got: {type(exception)}.")
tracer = get_tracer(use_active_span_processor=propagate)
stacktrace = "".join(traceback.format_exception(type(exception), exception, exception.__traceback__))
span_attributes = {
exception_attributes.EXCEPTION_TYPE: type(exception).__name__,
exception_attributes.EXCEPTION_MESSAGE: str(exception),
exception_attributes.EXCEPTION_ESCAPED: True,
}
if stacktrace.strip():
span_attributes[exception_attributes.EXCEPTION_STACKTRACE] = stacktrace
span_attributes = format_exception_attributes(exception)
if attributes:
span_attributes.update(attributes)
flattened = flatten_attributes(attributes, expand_leaf_lists=False)
span_attributes.update(sanitize_attributes(flattened))
span = tracer.start_span(
logger.debug("Emitting exception span for %s", type(exception).__name__)
if propagate:
tracer = get_active_tracer()
if tracer is None:
raise RuntimeError("No active tracer found. Cannot emit exception span.")
else:
tracer = DummyTracer()
tracer.create_span(
AGL_EXCEPTION,
attributes=span_attributes,
# The exception span is successful by itself.
status=TraceStatus(status_code="OK"),
)
logger.debug("Emitting exception span for %s", type(exception).__name__)
with span:
span.record_exception(exception)
# We don't set the status of the span here. They have other semantics.
+15 -9
View File
@@ -4,8 +4,10 @@ import logging
from typing import Any, Dict, Optional
from agentlightning.semconv import AGL_MESSAGE, LightningSpanAttributes
from agentlightning.types import SpanLike
from agentlightning.utils.otel import get_tracer
from agentlightning.tracer.base import get_active_tracer
from agentlightning.tracer.dummy import DummyTracer
from agentlightning.types import Attributes, SpanLike
from agentlightning.utils.otel import flatten_attributes, sanitize_attributes
logger = logging.getLogger(__name__)
@@ -27,17 +29,21 @@ def emit_message(message: str, attributes: Optional[Dict[str, Any]] = None, prop
if not isinstance(message, str): # type: ignore
raise TypeError(f"Message must be a string or list of strings, got: {type(message)}.")
tracer = get_tracer(use_active_span_processor=propagate)
span_attributes = {LightningSpanAttributes.MESSAGE_BODY.value: message}
if propagate:
tracer = get_active_tracer()
if tracer is None:
raise RuntimeError("No active tracer found. Cannot emit message span.")
else:
tracer = DummyTracer()
span_attributes: Attributes = {LightningSpanAttributes.MESSAGE_BODY.value: message}
if attributes:
span_attributes.update(attributes)
span = tracer.start_span(
flattened = flatten_attributes(attributes, expand_leaf_lists=False)
span_attributes.update(sanitize_attributes(flattened))
logger.debug("Emitting message span with message: %s", message)
tracer.create_span(
AGL_MESSAGE,
attributes=span_attributes,
)
logger.debug("Emitting message span with message: %s", message)
with span:
pass
def get_message_value(span: SpanLike) -> Optional[str]:
+22 -11
View File
@@ -6,13 +6,15 @@ import logging
from typing import Any, Dict, Optional
from agentlightning.semconv import AGL_OBJECT, LightningSpanAttributes
from agentlightning.types import SpanLike
from agentlightning.utils.otel import full_qualified_name, get_tracer
from agentlightning.tracer.base import get_active_tracer
from agentlightning.tracer.dummy import DummyTracer
from agentlightning.types import SpanCoreFields, SpanLike, TraceStatus
from agentlightning.utils.otel import flatten_attributes, full_qualified_name, sanitize_attributes
logger = logging.getLogger(__name__)
def emit_object(object: Any, attributes: Optional[Dict[str, Any]] = None, propagate: bool = True) -> None:
def emit_object(object: Any, attributes: Optional[Dict[str, Any]] = None, propagate: bool = True) -> SpanCoreFields:
"""Emit an object's serialized representation as an OpenTelemetry span.
Args:
@@ -25,20 +27,29 @@ def emit_object(object: Any, attributes: Optional[Dict[str, Any]] = None, propag
"""
span_attributes = encode_object(object)
if attributes:
span_attributes.update(attributes)
tracer = get_tracer(use_active_span_processor=propagate)
span = tracer.start_span(
AGL_OBJECT,
attributes=span_attributes,
)
flattened = flatten_attributes(attributes, expand_leaf_lists=False)
span_attributes.update(sanitize_attributes(flattened))
attr_length = 0
if LightningSpanAttributes.OBJECT_JSON.value in span_attributes:
attr_length = len(span_attributes[LightningSpanAttributes.OBJECT_JSON.value])
elif LightningSpanAttributes.OBJECT_LITERAL.value in span_attributes:
attr_length = len(span_attributes[LightningSpanAttributes.OBJECT_LITERAL.value])
logger.debug("Emitting object span with payload size %d characters", attr_length)
with span:
pass
if propagate:
tracer = get_active_tracer()
if tracer is None:
raise RuntimeError("No active tracer found. Cannot emit object span.")
else:
# Do not actually propagate to any store or tracer backend.
tracer = DummyTracer()
return tracer.create_span(
name=AGL_OBJECT,
attributes=span_attributes,
status=TraceStatus(status_code="OK"),
)
def encode_object(object: Any) -> Dict[str, Any]:
+7 -10
View File
@@ -20,13 +20,10 @@ from typing import (
cast,
)
import agentops
from agentops.sdk.decorators import operation
from opentelemetry.sdk.trace import ReadableSpan
from pydantic import TypeAdapter
from agentlightning.semconv import AGL_ANNOTATION, LightningSpanAttributes, RewardPydanticModel
from agentlightning.types import SpanLike
from agentlightning.types import SpanCoreFields, SpanLike
from agentlightning.utils.otel import filter_and_unflatten_attributes
from .annotation import emit_annotation
@@ -61,6 +58,8 @@ _FnType = TypeVar("_FnType", bound=Callable[..., Any])
def _agentops_initialized() -> bool:
"""Return `True` when the AgentOps client has been configured."""
import agentops
return agentops.get_client().initialized
@@ -81,6 +80,8 @@ def reward(fn: _FnType) -> _FnType:
Wrapped callable that preserves the original signature.
"""
from agentops.sdk.decorators import operation
def wrap_result(result: Optional[float]) -> _RewardSpanData:
"""Normalize the reward value into the span payload format."""
if result is None:
@@ -146,7 +147,7 @@ def emit_reward(
primary_key: str | None = None,
attributes: Dict[str, Any] | None = None,
propagate: bool = True,
) -> ReadableSpan:
) -> SpanCoreFields:
"""Emit a reward value as an OpenTelemetry span.
Examples:
@@ -172,11 +173,7 @@ def emit_reward(
propagate: Whether to propagate the span to exporters automatically.
Returns:
Readable span capturing the recorded reward.
Raises:
ValueError: If the provided reward cannot be interpreted as a float or the
resulting span is not a [`ReadableSpan`](https://opentelemetry.io/docs/concepts/signals/traces/) instance.
Span core fields capturing the recorded reward.
"""
logger.debug(f"Emitting reward: {reward}")
reward_dimensions: List[RewardDimension] = []
+22 -2
View File
@@ -143,6 +143,10 @@ class ClientServerExecutionStrategy(ExecutionStrategy):
logger.debug("Algorithm bundle starting against endpoint %s", wrapper_store.endpoint)
await algorithm(wrapper_store, stop_evt)
logger.debug("Algorithm bundle completed successfully")
except asyncio.CancelledError:
logger.info("Algorithm received CancelledError; signaling stop event")
stop_evt.set()
raise
except KeyboardInterrupt:
logger.warning("Algorithm received KeyboardInterrupt; signaling stop event")
stop_evt.set()
@@ -179,6 +183,10 @@ class ClientServerExecutionStrategy(ExecutionStrategy):
logger.debug("Runner %s executing with provided store", worker_id)
await runner(client_store, worker_id, stop_evt)
logger.debug("Runner %s completed successfully", worker_id)
except asyncio.CancelledError:
logger.debug("Runner %s received CancelledError; signaling stop event", worker_id)
stop_evt.set()
raise
except KeyboardInterrupt:
logger.warning("Runner %s received KeyboardInterrupt; signaling stop event", worker_id)
stop_evt.set()
@@ -210,7 +218,13 @@ class ClientServerExecutionStrategy(ExecutionStrategy):
def _runner_sync(runner: RunnerBundle, worker_id: int, store: LightningStore, stop_evt: ExecutionEvent) -> None:
# Runners are executed in child processes; each process owns its own
# event loop to keep the asyncio scheduler isolated.
asyncio.run(self._execute_runner(runner, worker_id, store, stop_evt))
try:
asyncio.run(self._execute_runner(runner, worker_id, store, stop_evt))
except KeyboardInterrupt:
logger.warning("Runner (asyncio) %s received KeyboardInterrupt; exiting gracefully", worker_id)
except BaseException as exc:
logger.exception("Runner (asyncio) %s crashed by %s; signaling stop event", worker_id, exc)
raise
for i in range(self.n_runners):
process = cast(
@@ -234,7 +248,13 @@ class ClientServerExecutionStrategy(ExecutionStrategy):
"""Used when `main_process == "runner"`."""
def _algorithm_sync(algorithm: AlgorithmBundle, store: LightningStore, stop_evt: ExecutionEvent) -> None:
asyncio.run(self._execute_algorithm(algorithm, store, stop_evt))
try:
asyncio.run(self._execute_algorithm(algorithm, store, stop_evt))
except KeyboardInterrupt:
logger.warning("Algorithm (asyncio.run) received KeyboardInterrupt; exiting gracefully")
except BaseException as exc:
logger.exception("Algorithm (asyncio.run) crashed by %s; signaling stop event", exc)
raise
process = cast(
multiprocessing.Process,
@@ -6,6 +6,7 @@ AGENTOPS_INSTALLED: bool = False
AGENTOPS_LANGCHAIN_INSTALLED: bool = False
LITELLM_INSTALLED: bool = False
VLLM_INSTALLED: bool = False
WEAVE_INSTALLED: bool = False
try:
from . import agentops # type: ignore
+500
View File
@@ -0,0 +1,500 @@
# Copyright (c) Microsoft. All rights reserved.
from __future__ import annotations
import logging
import threading
import warnings
from datetime import datetime, timezone
from typing import Any, Callable, Dict, Iterator, List
import weave.trace.weave_init
from pydantic import validate_call
from weave.trace_server import trace_server_interface as tsi
from weave.trace_server.ids import generate_id
from weave.trace_server_bindings.client_interface import TraceServerClientInterface
from weave.trace_server_bindings.models import ServerInfoRes
logger = logging.getLogger(__name__)
__all__ = [
"instrument_weave",
"uninstrument_weave",
"InMemoryWeaveTraceServer",
]
class InMemoryWeaveTraceServer(TraceServerClientInterface):
"""A minimal in-memory implementation of the TraceServerInterface.
It stores calls and objects in local dictionaries and returns valid Pydantic
responses to satisfy the Weave client and FullTraceServerInterface protocol.
"""
def __init__(self):
# Minimal storage to allow basic querying in tests
self.calls: Dict[str, tsi.CallSchema] = {}
self.partial_calls: Dict[str, Dict[str, Any]] = {}
self.objs: Dict[str, Any] = {}
self.files: Dict[str, bytes] = {}
self.feedback: List[tsi.FeedbackCreateReq] = []
self._call_threading_lock = threading.Lock()
@classmethod
def from_env(cls, *args: Any, **kwargs: Any) -> InMemoryWeaveTraceServer:
return cls()
def server_info(self) -> ServerInfoRes:
return ServerInfoRes(min_required_weave_python_version="0.52.22")
def ensure_project_exists(self, entity: str, project: str) -> tsi.EnsureProjectExistsRes:
return tsi.EnsureProjectExistsRes(project_name=project)
# --- Call API ---
@validate_call
def call_start(self, req: tsi.CallStartReq) -> tsi.CallStartRes:
# NOTE: It's not necessary that call_end must be called after call_start.
request_content = req.start.model_dump(exclude_none=True)
# If id needs to be generated here, it's very likely we won't be able to find the call later.
# This is just to make the type checker happy.
call_id = request_content.get("id") or generate_id()
trace_id = request_content.get("trace_id") or generate_id()
request_content["id"] = call_id
request_content["trace_id"] = trace_id
with self._call_threading_lock:
if call_id in self.partial_calls:
# call_end has already been called for this call.
kwargs = {**request_content, **self.partial_calls[call_id]}
self.calls[call_id] = tsi.CallSchema(**kwargs)
del self.partial_calls[call_id]
else:
self.partial_calls[call_id] = request_content
return tsi.CallStartRes(id=call_id, trace_id=trace_id)
@validate_call
def call_end(self, req: tsi.CallEndReq) -> tsi.CallEndRes:
request_content = req.end.model_dump(exclude_none=True)
call_id = req.end.id
with self._call_threading_lock:
if call_id in self.partial_calls:
# End request always override the start request content.
kwargs = {**self.partial_calls[call_id], **request_content}
self.calls[call_id] = tsi.CallSchema(**kwargs)
del self.partial_calls[call_id]
else:
self.partial_calls[call_id] = request_content
return tsi.CallEndRes()
@validate_call
def call_start_batch(self, req: tsi.CallCreateBatchReq) -> tsi.CallCreateBatchRes:
for item in req.batch:
if isinstance(item, tsi.CallStartReq):
self.call_start(item)
elif isinstance(item, tsi.CallEndReq):
self.call_end(item)
return tsi.CallCreateBatchRes(res=[])
@validate_call
def call_read(self, req: tsi.CallReadReq) -> tsi.CallReadRes:
call_data = self.calls.get(req.id)
return tsi.CallReadRes(call=call_data)
@validate_call
def calls_query(self, req: tsi.CallsQueryReq) -> tsi.CallsQueryRes:
return tsi.CallsQueryRes(calls=list(self.calls_query_stream(req)))
@validate_call
def calls_query_stream(self, req: tsi.CallsQueryReq) -> Iterator[tsi.CallSchema]:
yield from self.calls.values()
@validate_call
def calls_delete(self, req: tsi.CallsDeleteReq) -> tsi.CallsDeleteRes:
num_deleted = 0
for call_id in req.call_ids:
if call_id in self.calls:
del self.calls[call_id]
num_deleted += 1
return tsi.CallsDeleteRes(num_deleted=num_deleted)
@validate_call
def call_update(self, req: tsi.CallUpdateReq) -> tsi.CallUpdateRes:
return tsi.CallUpdateRes()
@validate_call
def calls_query_stats(self, req: tsi.CallsQueryStatsReq) -> tsi.CallsQueryStatsRes:
return tsi.CallsQueryStatsRes(count=len(self.calls))
# --- Cost API ---
@validate_call
def cost_create(self, req: tsi.CostCreateReq) -> tsi.CostCreateRes:
return tsi.CostCreateRes(ids=[(generate_id(), generate_id()) for _ in req.costs])
@validate_call
def cost_query(self, req: tsi.CostQueryReq) -> tsi.CostQueryRes:
return tsi.CostQueryRes(results=[])
@validate_call
def cost_purge(self, req: tsi.CostPurgeReq) -> tsi.CostPurgeRes:
return tsi.CostPurgeRes()
# --- Object API (Legacy V1) ---
@validate_call
def obj_create(self, req: tsi.ObjCreateReq) -> tsi.ObjCreateRes:
digest = generate_id()
self.objs[digest] = req.obj
return tsi.ObjCreateRes(digest=digest)
@validate_call
def obj_read(self, req: tsi.ObjReadReq) -> tsi.ObjReadRes:
return tsi.ObjReadRes(obj=self.objs.get(req.digest, {}))
@validate_call
def objs_query(self, req: tsi.ObjQueryReq) -> tsi.ObjQueryRes:
return tsi.ObjQueryRes(objs=[])
@validate_call
def obj_delete(self, req: tsi.ObjDeleteReq) -> tsi.ObjDeleteRes:
return tsi.ObjDeleteRes(num_deleted=0)
# --- Table API ---
@validate_call
def table_create(self, req: tsi.TableCreateReq) -> tsi.TableCreateRes:
return tsi.TableCreateRes(digest=generate_id(), row_digests=[])
@validate_call
def table_create_from_digests(self, req: tsi.TableCreateFromDigestsReq) -> tsi.TableCreateFromDigestsRes:
return tsi.TableCreateFromDigestsRes(digest=generate_id())
@validate_call
def table_update(self, req: tsi.TableUpdateReq) -> tsi.TableUpdateRes:
return tsi.TableUpdateRes(digest=generate_id(), updated_row_digests=[])
@validate_call
def table_query(self, req: tsi.TableQueryReq) -> tsi.TableQueryRes:
return tsi.TableQueryRes(rows=[])
@validate_call
def table_query_stream(self, req: tsi.TableQueryReq) -> Iterator[tsi.TableRowSchema]:
yield from []
@validate_call
def table_query_stats(self, req: tsi.TableQueryStatsReq) -> tsi.TableQueryStatsRes:
return tsi.TableQueryStatsRes(count=0)
@validate_call
def table_query_stats_batch(self, req: tsi.TableQueryStatsBatchReq) -> tsi.TableQueryStatsBatchRes:
return tsi.TableQueryStatsBatchRes(tables=[])
# --- Ref API ---
@validate_call
def refs_read_batch(self, req: tsi.RefsReadBatchReq) -> tsi.RefsReadBatchRes:
return tsi.RefsReadBatchRes(vals=[])
# --- File API ---
def file_create(self, req: tsi.FileCreateReq) -> tsi.FileCreateRes:
self.files[req.name] = req.content
return tsi.FileCreateRes(digest=generate_id())
def file_content_read(self, req: tsi.FileContentReadReq) -> tsi.FileContentReadRes:
return tsi.FileContentReadRes(content=self.files.get(req.digest, b"dummy_content"))
def files_stats(self, req: tsi.FilesStatsReq) -> tsi.FilesStatsRes:
total_size = sum(len(c) for c in self.files.values())
return tsi.FilesStatsRes(total_size_bytes=total_size)
# --- Feedback API ---
@validate_call
def feedback_create(self, req: tsi.FeedbackCreateReq) -> tsi.FeedbackCreateRes:
req.id = req.id or generate_id()
self.feedback.append(req)
return tsi.FeedbackCreateRes(
id=req.id,
created_at=datetime.now(timezone.utc),
wb_user_id="dummy_user",
payload=req.payload,
)
def feedback_create_batch(self, req: tsi.FeedbackCreateBatchReq) -> tsi.FeedbackCreateBatchRes:
results: List[tsi.FeedbackCreateRes] = []
for item in req.batch:
res = self.feedback_create(item)
results.append(res)
return tsi.FeedbackCreateBatchRes(res=results)
@validate_call
def feedback_query(self, req: tsi.FeedbackQueryReq) -> tsi.FeedbackQueryRes:
return tsi.FeedbackQueryRes(result=[])
@validate_call
def feedback_purge(self, req: tsi.FeedbackPurgeReq) -> tsi.FeedbackPurgeRes:
self.feedback.clear()
return tsi.FeedbackPurgeRes()
@validate_call
def feedback_replace(self, req: tsi.FeedbackReplaceReq) -> tsi.FeedbackReplaceRes:
return tsi.FeedbackReplaceRes(
id=req.id or generate_id(),
created_at=datetime.now(timezone.utc),
wb_user_id="dummy",
payload={},
)
# --- Action API ---
@validate_call
def actions_execute_batch(self, req: tsi.ActionsExecuteBatchReq) -> tsi.ActionsExecuteBatchRes:
return tsi.ActionsExecuteBatchRes()
# --- Execute LLM API ---
@validate_call
def completions_create(self, req: tsi.CompletionsCreateReq) -> tsi.CompletionsCreateRes:
return tsi.CompletionsCreateRes(response={"choices": [{"text": "dummy completion"}]})
@validate_call
def completions_create_stream(self, req: tsi.CompletionsCreateReq) -> Iterator[dict[str, Any]]:
yield {"choices": [{"text": "dummy "}]}
yield {"choices": [{"text": "stream"}]}
# --- Execute Image Generation API ---
@validate_call
def image_create(self, req: tsi.ImageGenerationCreateReq) -> tsi.ImageGenerationCreateRes:
return tsi.ImageGenerationCreateRes(response={})
# --- Project Statistics API ---
@validate_call
def project_stats(self, req: tsi.ProjectStatsReq) -> tsi.ProjectStatsRes:
return tsi.ProjectStatsRes(
trace_storage_size_bytes=0,
objects_storage_size_bytes=0,
tables_storage_size_bytes=0,
files_storage_size_bytes=0,
)
# --- Thread API ---
@validate_call
def threads_query_stream(self, req: tsi.ThreadsQueryReq) -> Iterator[tsi.ThreadSchema]:
yield from []
# --- Evaluation API (V1) ---
@validate_call
def evaluate_model(self, req: tsi.EvaluateModelReq) -> tsi.EvaluateModelRes:
return tsi.EvaluateModelRes(call_id=generate_id())
@validate_call
def evaluation_status(self, req: tsi.EvaluationStatusReq) -> tsi.EvaluationStatusRes:
return tsi.EvaluationStatusRes(status=tsi.EvaluationStatusNotFound())
# --- OTEL API ---
def otel_export(self, req: tsi.OtelExportReq) -> tsi.OtelExportRes:
return tsi.OtelExportRes()
# ==========================================
# Object Interface (V2 APIs)
# ==========================================
# --- Ops ---
def op_create(self, req: tsi.OpCreateReq) -> tsi.OpCreateRes:
return tsi.OpCreateRes(digest=generate_id(), object_id=generate_id(), version_index=0)
def op_read(self, req: tsi.OpReadReq) -> tsi.OpReadRes:
return tsi.OpReadRes(op=None) # type: ignore
def op_list(self, req: tsi.OpListReq) -> Iterator[tsi.OpReadRes]:
yield from []
def op_delete(self, req: tsi.OpDeleteReq) -> tsi.OpDeleteRes:
return tsi.OpDeleteRes(num_deleted=0)
# --- Datasets ---
def dataset_create(self, req: tsi.DatasetCreateReq) -> tsi.DatasetCreateRes:
return tsi.DatasetCreateRes(digest=generate_id(), object_id=generate_id(), version_index=0)
def dataset_read(self, req: tsi.DatasetReadReq) -> tsi.DatasetReadRes:
return tsi.DatasetReadRes(dataset=None) # type: ignore
def dataset_list(self, req: tsi.DatasetListReq) -> Iterator[tsi.DatasetReadRes]:
yield from []
def dataset_delete(self, req: tsi.DatasetDeleteReq) -> tsi.DatasetDeleteRes:
return tsi.DatasetDeleteRes(num_deleted=0)
# --- Scorers ---
def scorer_create(self, req: tsi.ScorerCreateReq) -> tsi.ScorerCreateRes:
return tsi.ScorerCreateRes(digest=generate_id(), object_id=generate_id(), version_index=0, scorer=generate_id())
def scorer_read(self, req: tsi.ScorerReadReq) -> tsi.ScorerReadRes:
return tsi.ScorerReadRes(scorer=None) # type: ignore
def scorer_list(self, req: tsi.ScorerListReq) -> Iterator[tsi.ScorerReadRes]:
yield from []
def scorer_delete(self, req: tsi.ScorerDeleteReq) -> tsi.ScorerDeleteRes:
return tsi.ScorerDeleteRes(num_deleted=0)
# --- Evaluations (V2) ---
def evaluation_create(self, req: tsi.EvaluationCreateReq) -> tsi.EvaluationCreateRes:
return tsi.EvaluationCreateRes(
digest=generate_id(), object_id=generate_id(), version_index=0, evaluation_ref=generate_id()
)
def evaluation_read(self, req: tsi.EvaluationReadReq) -> tsi.EvaluationReadRes:
return tsi.EvaluationReadRes(evaluation=None) # type: ignore
def evaluation_list(self, req: tsi.EvaluationListReq) -> Iterator[tsi.EvaluationReadRes]:
yield from []
def evaluation_delete(self, req: tsi.EvaluationDeleteReq) -> tsi.EvaluationDeleteRes:
return tsi.EvaluationDeleteRes(num_deleted=0)
# --- Models ---
def model_create(self, req: tsi.ModelCreateReq) -> tsi.ModelCreateRes:
return tsi.ModelCreateRes(
digest=generate_id(), object_id=generate_id(), version_index=0, model_ref=generate_id()
)
def model_read(self, req: tsi.ModelReadReq) -> tsi.ModelReadRes:
return tsi.ModelReadRes(model=None) # type: ignore
def model_list(self, req: tsi.ModelListReq) -> Iterator[tsi.ModelReadRes]:
yield from []
def model_delete(self, req: tsi.ModelDeleteReq) -> tsi.ModelDeleteRes:
return tsi.ModelDeleteRes(num_deleted=0)
# --- Evaluation Runs ---
def evaluation_run_create(self, req: tsi.EvaluationRunCreateReq) -> tsi.EvaluationRunCreateRes:
return tsi.EvaluationRunCreateRes(evaluation_run_id=generate_id())
def evaluation_run_read(self, req: tsi.EvaluationRunReadReq) -> tsi.EvaluationRunReadRes:
return tsi.EvaluationRunReadRes(evaluation_run=None) # type: ignore
def evaluation_run_list(self, req: tsi.EvaluationRunListReq) -> Iterator[tsi.EvaluationRunReadRes]:
yield from []
def evaluation_run_delete(self, req: tsi.EvaluationRunDeleteReq) -> tsi.EvaluationRunDeleteRes:
return tsi.EvaluationRunDeleteRes(num_deleted=0)
def evaluation_run_finish(self, req: tsi.EvaluationRunFinishReq) -> tsi.EvaluationRunFinishRes:
return tsi.EvaluationRunFinishRes(success=True)
# --- Predictions ---
def prediction_create(self, req: tsi.PredictionCreateReq) -> tsi.PredictionCreateRes:
return tsi.PredictionCreateRes(prediction_id=generate_id())
def prediction_read(self, req: tsi.PredictionReadReq) -> tsi.PredictionReadRes:
return tsi.PredictionReadRes(prediction=None) # type: ignore
def prediction_list(self, req: tsi.PredictionListReq) -> Iterator[tsi.PredictionReadRes]:
yield from []
def prediction_delete(self, req: tsi.PredictionDeleteReq) -> tsi.PredictionDeleteRes:
return tsi.PredictionDeleteRes(num_deleted=0)
def prediction_finish(self, req: tsi.PredictionFinishReq) -> tsi.PredictionFinishRes:
return tsi.PredictionFinishRes(success=True)
# --- Scores ---
def score_create(self, req: tsi.ScoreCreateReq) -> tsi.ScoreCreateRes:
return tsi.ScoreCreateRes(score_id=generate_id())
def score_read(self, req: tsi.ScoreReadReq) -> tsi.ScoreReadRes:
return tsi.ScoreReadRes(score=None) # type: ignore
def score_list(self, req: tsi.ScoreListReq) -> Iterator[tsi.ScoreReadRes]:
yield from []
def score_delete(self, req: tsi.ScoreDeleteReq) -> tsi.ScoreDeleteRes:
return tsi.ScoreDeleteRes(num_deleted=0)
# Module-level storage for originals
_original_init_weave_get_server: Callable[..., Any] | None = None
_original_get_entity_project_from_project_name: Callable[..., Any] | None = None
_original_get_username: Callable[..., Any] | None = None
def init_weave_get_server_factory(server: InMemoryWeaveTraceServer) -> Callable[..., Any]:
# Bypass the usage of Weave remote server
def init_weave_get_server(*args: Any, **kwargs: Any) -> InMemoryWeaveTraceServer:
return server
return init_weave_get_server
def get_entity_project_from_project_name_factory(entity_name: str) -> tuple[str, str]:
# Bypass the usage of API
try:
assert _original_get_entity_project_from_project_name is not None
if _original_get_entity_project_from_project_name is not get_entity_project_from_project_name_factory:
return _original_get_entity_project_from_project_name(entity_name)
else:
warnings.warn("W&B integration might have been repeatedly/recursively instrumented.")
return "agl", "weave"
except weave.trace.weave_init.WeaveWandbAuthenticationException:
# In case API is not available.
return "agl", "weave"
def get_username() -> str:
# Bypass the usage of API
try:
assert _original_get_username is not None
return _original_get_username()
except RuntimeError:
return "agl"
except Exception as exc:
warnings.warn(f"Unexpected error in get_username. Using default username. Error: {exc}")
return "agl"
def instrument_weave(server: InMemoryWeaveTraceServer):
"""Patch the Weave/W&B integration to bypass actual network calls for testing."""
global _original_init_weave_get_server, _original_get_entity_project_from_project_name, _original_get_username
_original_init_weave_get_server = weave.trace.weave_init.init_weave_get_server
_original_get_entity_project_from_project_name = weave.trace.weave_init.get_entity_project_from_project_name
_original_get_username = weave.trace.weave_init.get_username
weave.trace.weave_init.init_weave_get_server = init_weave_get_server_factory(server)
weave.trace.weave_init.get_entity_project_from_project_name = get_entity_project_from_project_name_factory
weave.trace.weave_init.get_username = get_username
def uninstrument_weave():
"""Restore the original Weave/W&B integration methods and HTTP requests."""
global _original_init_weave_get_server, _original_get_entity_project_from_project_name, _original_get_username
if _original_init_weave_get_server is not None:
weave.trace.weave_init.init_weave_get_server = _original_init_weave_get_server
_original_init_weave_get_server = None
else:
raise RuntimeError("Weave/W&B integration was not instrumented.")
if _original_get_entity_project_from_project_name is not None:
weave.trace.weave_init.get_entity_project_from_project_name = _original_get_entity_project_from_project_name
_original_get_entity_project_from_project_name = None
else:
raise RuntimeError("Weave/W&B integration was not instrumented.")
if _original_get_username is not None:
weave.trace.weave_init.get_username = _original_get_username
_original_get_username = None
else:
raise RuntimeError("Weave/W&B integration was not instrumented.")
+1
View File
@@ -198,6 +198,7 @@ class LitAgent(Generic[T]):
* `float` representing the final reward.
* `List[ReadableSpan]` with OpenTelemetry spans.
* `List[Span]` with Agent Lightning spans.
* `List[SpanCoreFields]` with Agent Lightning spans.
"""
raise NotImplementedError("Agents must implement the `rollout` method.")
+241 -57
View File
@@ -43,6 +43,7 @@ from agentlightning.types import (
RolloutMode,
RolloutRawResult,
Span,
SpanCoreFields,
)
from agentlightning.utils.system_snapshot import system_snapshot
@@ -74,7 +75,8 @@ class LitAgentRunner(Runner[T_task]):
poll_interval: float = 5.0,
heartbeat_interval: float = 10.0,
interval_jitter: float = 0.5,
heartbeat_launch_mode: Literal["asyncio", "thread"] = "asyncio",
heartbeat_launch_mode: Literal["asyncio", "thread"] = "thread",
heartbeat_include_gpu: bool = False,
) -> None:
"""Initialize the agent runner.
@@ -88,7 +90,10 @@ class LitAgentRunner(Runner[T_task]):
poll_interval - interval_jitter and poll_interval + interval_jitter.
This is to avoid the overload caused by the synchronization of the runners.
heartbeat_launch_mode: Launch mode for the heartbeat loop. Can be "asyncio" or "thread".
"asyncio" is the default and recommended mode. Use "thread" if you are experiencing blocking coroutines.
"thread" is the default and recommended mode as it prevents blocking the event loop
under load. Use "asyncio" for simpler deployments with low worker counts.
heartbeat_include_gpu: Whether to include GPU stats in heartbeat snapshots.
Querying GPU stats can be slow under load, so this is disabled by default.
"""
super().__init__()
self._tracer = tracer
@@ -97,6 +102,7 @@ class LitAgentRunner(Runner[T_task]):
self._heartbeat_interval = heartbeat_interval
self._interval_jitter = interval_jitter
self._heartbeat_launch_mode = heartbeat_launch_mode
self._heartbeat_include_gpu = heartbeat_include_gpu
self._random_state = random.Random()
# Set later
@@ -276,7 +282,7 @@ class LitAgentRunner(Runner[T_task]):
"""
store = self.get_store()
trace_spans: list[ReadableSpan] | list[Span] = []
trace_spans: list[Span] = []
result_recognized: bool = False
# Case 0: result is None
@@ -295,31 +301,38 @@ class LitAgentRunner(Runner[T_task]):
# Preserve the existing spans before another span is emitted
trace_spans = list(self._tracer.get_last_trace())
# This will NOT emit another span to the tracer
reward_span = emit_reward(raw_result, propagate=False)
reward_span_core_fields = emit_reward(raw_result, propagate=False)
# We add it to the store manually
await store.add_otel_span(rollout.rollout_id, rollout.attempt.attempt_id, reward_span)
trace_spans.append(reward_span)
sequence_id = await store.get_next_span_sequence_id(rollout.rollout_id, rollout.attempt.attempt_id)
reward_span = Span.from_core_fields(
reward_span_core_fields,
rollout_id=rollout.rollout_id,
attempt_id=rollout.attempt.attempt_id,
sequence_id=sequence_id,
)
await store.add_span(reward_span)
result_recognized = True
# Case 2-3: result is a list
# Case 2-4: result is a list
if isinstance(raw_result, list):
# For rollout methods that return a list, we assume that the returned spans
# are the complete span set from the whole rollout
trace_spans = raw_result
# Case 2: result is a list of ReadableSpan (OpenTelemetry spans)
if len(raw_result) > 0 and all(isinstance(t, ReadableSpan) for t in raw_result):
if not isinstance(self._tracer, OtelTracer):
for span in raw_result:
await store.add_otel_span(
rollout.rollout_id, rollout.attempt.attempt_id, cast(ReadableSpan, span)
)
else:
if isinstance(self._tracer, OtelTracer):
logger.warning(
f"{self._log_prefix(rollout.rollout_id)} Tracer is already an OpenTelemetry tracer. "
"The traces should have already been added to the store. "
"No need to return anything from rollout."
"Returning the traces from the rollout will result in duplicate spans."
)
for span in raw_result:
added_span = await store.add_otel_span(
rollout.rollout_id, rollout.attempt.attempt_id, cast(ReadableSpan, span)
)
if added_span is not None:
trace_spans.append(added_span)
else:
logger.error(
f"{self._log_prefix(rollout.rollout_id)} Failed to add OpenTelemetry span to the store: {span}"
)
result_recognized = True
# Case 3: result is a list of Span (agentlightning spans)
@@ -327,7 +340,25 @@ class LitAgentRunner(Runner[T_task]):
# Add the spans directly to the store
for span in raw_result:
await store.add_span(cast(Span, span))
trace_spans = raw_result
trace_spans = [cast(Span, span) for span in raw_result]
result_recognized = True
# Case 4: result is a list of SpanCoreFields (agentlightning spans)
elif len(raw_result) > 0 and all(isinstance(t, SpanCoreFields) for t in raw_result):
# Add the spans directly to the store too, but needs to get sequence id first
sequence_ids = await store.get_many_span_sequence_ids(
[(rollout.rollout_id, rollout.attempt.attempt_id) for _ in range(len(raw_result))]
)
trace_spans = [
Span.from_core_fields(
cast(SpanCoreFields, span_core_fields),
rollout_id=rollout.rollout_id,
attempt_id=rollout.attempt.attempt_id,
sequence_id=sequence_id,
)
for span_core_fields, sequence_id in zip(raw_result, sequence_ids, strict=True)
]
await store.add_many_spans(trace_spans)
result_recognized = True
# Left over cases for list
@@ -336,7 +367,7 @@ class LitAgentRunner(Runner[T_task]):
f"{self._log_prefix(rollout.rollout_id)} The rollout returns an empty list. "
"Please check your rollout implementation."
)
trace_spans = raw_result
trace_spans = []
result_recognized = True
else:
@@ -355,14 +386,46 @@ class LitAgentRunner(Runner[T_task]):
return trace_spans
async def _emit_heartbeat(self, store: LightningStore) -> None:
"""Send a heartbeat tick to the store."""
"""Send a heartbeat tick to the store.
Args:
store: The lightning store to update.
"""
logger.debug(f"{self._log_prefix()} Preparing to emit heartbeat.")
worker_id = self.get_worker_id()
try:
await store.update_worker(worker_id, system_snapshot())
snapshot = await asyncio.wait_for(
asyncio.to_thread(system_snapshot, self._heartbeat_include_gpu),
timeout=self._heartbeat_interval,
)
logger.debug(f"{self._log_prefix()} Heartbeat snapshot acquired.")
except asyncio.TimeoutError:
logger.warning(
"%s Heartbeat snapshot acquisition timed out after %.1fs, skipping.",
self._log_prefix(),
self._heartbeat_interval,
)
return
except asyncio.CancelledError:
# bypass the exception
raise
except Exception:
logger.exception("%s Unable to acquire heartbeat snapshot.", self._log_prefix())
return
try:
await asyncio.wait_for(store.update_worker(worker_id, snapshot), timeout=self._heartbeat_interval)
logger.debug(f"{self._log_prefix()} Heartbeat updated successfully.")
except asyncio.CancelledError:
# bypass the exception
raise
except asyncio.TimeoutError:
logger.warning(
"%s update worker heartbeat timed out after %.1fs, skipping.",
self._log_prefix(),
self._heartbeat_interval,
)
except Exception:
logger.exception("%s Unable to update worker heartbeat.", self._log_prefix())
@@ -377,51 +440,161 @@ class LitAgentRunner(Runner[T_task]):
return None
if self._heartbeat_launch_mode == "asyncio":
stop_event = asyncio.Event()
async def heartbeat_loop() -> None:
while not stop_event.is_set():
await self._emit_heartbeat(store)
with suppress(asyncio.TimeoutError):
interval = self._heartbeat_interval + self._random_state.uniform(
-self._interval_jitter, self._interval_jitter
)
interval = max(interval, 0.01)
await asyncio.wait_for(stop_event.wait(), timeout=interval)
task = asyncio.create_task(heartbeat_loop(), name=f"{self.get_worker_id()}-heartbeat")
async def stop() -> None:
stop_event.set()
with suppress(asyncio.CancelledError):
await task
return stop
return self._start_heartbeat_asyncio_loop(store)
if self._heartbeat_launch_mode == "thread":
stop_evt = threading.Event()
return self._start_heartbeat_thread_loop(store)
raise ValueError(f"Unsupported heartbeat launch mode: {self._heartbeat_launch_mode}")
def thread_worker() -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
while not stop_evt.is_set():
loop.run_until_complete(self._emit_heartbeat(store))
def _start_heartbeat_asyncio_loop(self, store: LightningStore) -> Optional[Callable[[], Awaitable[None]]]:
"""Start a background heartbeat loop using asyncio.
Args:
store: The lightning store to update.
Returns:
An async stopper function that can be used to stop the heartbeat loop.
"""
stop_event = asyncio.Event()
async def heartbeat_loop() -> None:
while not stop_event.is_set():
try:
# Run _emit_heartbeat in thread pool to avoid blocking the event loop.
# Timeout at the interval - if it takes longer, the data is stale anyway.
await self._emit_heartbeat(store)
except Exception:
logger.exception("%s Heartbeat failed.", self._log_prefix())
with suppress(asyncio.TimeoutError):
interval = self._heartbeat_interval + self._random_state.uniform(
-self._interval_jitter, self._interval_jitter
)
interval = max(interval, 0.01)
stop_evt.wait(interval)
await asyncio.wait_for(stop_event.wait(), timeout=interval)
thread = threading.Thread(target=thread_worker, name=f"{self.get_worker_id()}-heartbeat", daemon=True)
thread.start()
task = asyncio.create_task(heartbeat_loop(), name=f"{self.get_worker_id()}-heartbeat")
async def stop() -> None:
stop_evt.set()
await asyncio.to_thread(thread.join)
async def stop() -> None:
stop_event.set()
with suppress(asyncio.CancelledError):
await task
return stop
return stop
raise ValueError(f"Unsupported heartbeat launch mode: {self._heartbeat_launch_mode}")
def _start_heartbeat_thread_loop(self, store: LightningStore) -> Optional[Callable[[], Awaitable[None]]]:
"""Start a background heartbeat loop using threading.
It uses two threads: one to produce the snapshot and one to consume it,
to avoid either of them blocking the event loop.
Args:
store: The lightning store to update.
Returns:
An async stopper function that can be used to stop the heartbeat loop.
"""
stop_evt = threading.Event()
lock = threading.Lock()
latest_snapshot = None
latest_ts = 0.0 # time.monotonic() when snapshot was captured
# Consider snapshot stale after ~1 interval plus jitter slack.
stale_after = self._heartbeat_interval + self._interval_jitter + 1.0
worker_id = self.get_worker_id()
def producer() -> None:
nonlocal latest_snapshot, latest_ts
while not stop_evt.is_set():
try:
logger.debug(f"{self._log_prefix()} Heartbeat producer: acquiring snapshot.")
snap = system_snapshot(self._heartbeat_include_gpu) # sync
logger.debug(f"{self._log_prefix()} Heartbeat producer: snapshot acquired.")
ts = time.monotonic()
with lock:
latest_snapshot = snap
latest_ts = ts
except Exception:
logger.warning("%s Heartbeat producer: system_snapshot failed.", self._log_prefix(), exc_info=True)
interval = self._heartbeat_interval + self._random_state.uniform(
-self._interval_jitter, self._interval_jitter
)
stop_evt.wait(max(interval, 0.01))
def consumer() -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
last_warned_ts = None # Track which snapshot we've already warned about
try:
while not stop_evt.is_set():
with lock:
snap = latest_snapshot
ts = latest_ts
wait_interval = max(
self._heartbeat_interval
+ self._random_state.uniform(-self._interval_jitter, self._interval_jitter),
0.01,
)
if snap is None:
# probably just started
logger.debug("%s Heartbeat consumer: no snapshot yet; skipping update.", self._log_prefix())
stop_evt.wait(wait_interval)
continue
age = time.monotonic() - ts
if age > stale_after:
# Only warn once per stale snapshot (check if we haven't warned about this timestamp yet)
if last_warned_ts != ts:
logger.warning(
"%s Heartbeat consumer: snapshot stale (age=%.2fs > %.2fs); skipping update.",
self._log_prefix(),
age,
stale_after,
)
last_warned_ts = ts
stop_evt.wait(wait_interval)
continue
try:
logger.debug(f"{self._log_prefix()} Heartbeat consumer: updating worker.")
loop.run_until_complete(
asyncio.wait_for(
store.update_worker(worker_id, snap),
timeout=self._heartbeat_interval,
)
)
logger.debug(f"{self._log_prefix()} Heartbeat consumer: worker updated.")
except asyncio.TimeoutError:
logger.warning(
"%s Heartbeat consumer: update timed out after %.1fs.",
self._log_prefix(),
self._heartbeat_interval,
)
except Exception:
logger.warning("%s Heartbeat consumer: update failed.", self._log_prefix(), exc_info=True)
stop_evt.wait(wait_interval)
finally:
with suppress(Exception):
loop.stop()
with suppress(Exception):
loop.close()
t_prod = threading.Thread(target=producer, name=f"{worker_id}-heartbeat-producer", daemon=True)
t_cons = threading.Thread(target=consumer, name=f"{worker_id}-heartbeat-consumer", daemon=True)
t_prod.start()
t_cons.start()
async def stop() -> None:
stop_evt.set()
await asyncio.to_thread(t_prod.join)
await asyncio.to_thread(t_cons.join)
return stop
async def _sleep_until_next_poll(self, event: Optional[ExecutionEvent] = None) -> None:
"""Sleep until the next poll interval, with optional event-based interruption.
@@ -477,6 +650,8 @@ class LitAgentRunner(Runner[T_task]):
logger.error(f"{self._log_prefix(rollout_id)} Failed to fetch resources. Skipping.")
return rollout_id
logger.debug(f"{self._log_prefix(rollout_id)} Resources fetched (id={resources_update.resources_id}).")
trace_spans: List[ReadableSpan] | List[Span] = []
has_exception: bool = False
@@ -484,9 +659,11 @@ class LitAgentRunner(Runner[T_task]):
await self._trigger_hooks(hook_type="on_rollout_start", agent=agent, runner=self, rollout=next_rollout)
start_time = time.time()
logger.debug(f"{self._log_prefix(rollout_id)} Prepared for trace context.")
async with self._tracer.trace_context(
name=rollout_id, rollout_id=rollout_id, attempt_id=next_rollout.attempt.attempt_id
):
logger.debug(f"{self._log_prefix(rollout_id)} Entered trace context.")
await self._trigger_hooks(
hook_type="on_trace_start", agent=agent, runner=self, tracer=self._tracer, rollout=next_rollout
)
@@ -498,21 +675,27 @@ class LitAgentRunner(Runner[T_task]):
rollout_method = (
agent.training_rollout_async if next_rollout.mode == "train" else agent.validation_rollout_async
)
logger.debug(f"{self._log_prefix(rollout_id)} Starting async rollout method.")
result = await rollout_method(
next_rollout.input, resources=resources_update.resources, rollout=next_rollout
)
logger.debug(f"{self._log_prefix(rollout_id)} Async rollout method completed.")
else:
rollout_method = (
agent.training_rollout if next_rollout.mode == "train" else agent.validation_rollout
)
logger.debug(f"{self._log_prefix(rollout_id)} Starting sync rollout method.")
result = rollout_method(
next_rollout.input, resources=resources_update.resources, rollout=next_rollout
)
logger.debug(f"{self._log_prefix(rollout_id)} Sync rollout method completed.")
await self._trigger_hooks(
hook_type="on_trace_end", agent=agent, runner=self, tracer=self._tracer, rollout=next_rollout
)
logger.debug(f"{self._log_prefix(rollout_id)} Trace context exited.")
# Possible exceptions in post_process will be caught in the overall exception handler
trace_spans = await self._post_process_rollout_result(next_rollout, result)
last_reward = find_final_reward(trace_spans)
@@ -582,6 +765,7 @@ class LitAgentRunner(Runner[T_task]):
while not (event is not None and event.is_set()):
logger.debug(f"{self._log_prefix()} Try to poll for next rollout.")
next_rollout = await store.dequeue_rollout(worker_id=self.get_worker_id())
logger.debug(f"{self._log_prefix()} Next rollout retrieved: {next_rollout}")
if next_rollout is None:
logger.debug(
f"{self._log_prefix()} No rollout to poll. Waiting for {self._poll_interval} seconds."
+11 -8
View File
@@ -12,7 +12,7 @@ from agentlightning.client import AgentLightningClient
from agentlightning.litagent import LitAgent
from agentlightning.litagent.litagent import is_v0_1_rollout_api
from agentlightning.tracer.base import Tracer
from agentlightning.types import RolloutLegacy, RolloutRawResultLegacy, Triplet
from agentlightning.types import RolloutLegacy, RolloutRawResultLegacy, Span, SpanLike, Triplet
from .base import Runner
@@ -99,7 +99,7 @@ class LegacyAgentRunner(Runner[Any]):
trace: Any = None
final_reward: Optional[float] = None
triplets: Optional[List[Triplet]] = None
trace_spans: Optional[List[ReadableSpan]] = None
trace_spans: Optional[List[SpanLike]] = None
# Handle different types of results from the agent
# Case 1: result is a float (final reward)
@@ -108,10 +108,14 @@ class LegacyAgentRunner(Runner[Any]):
# Case 2: result is a list of Triplets
if isinstance(result, list) and all(isinstance(t, Triplet) for t in result):
triplets = result # type: ignore
# Case 3: result is a list of ReadableSpan (OpenTelemetry spans)
if isinstance(result, list) and all(isinstance(t, ReadableSpan) for t in result):
# Case 3.1: result is a list of ReadableSpan (OpenTelemetry spans)
if isinstance(result, list) and all(isinstance(t, (ReadableSpan)) for t in result):
trace_spans = result # type: ignore
trace = [json.loads(readable_span.to_json()) for readable_span in trace_spans] # type: ignore
# Case 3.2: result is a list of Span (Agent-lightning spans)
if isinstance(result, list) and all(isinstance(t, Span) for t in result):
trace_spans = result # type: ignore
trace = [span.model_dump() for span in trace_spans] # type: ignore
# Case 4: result is a list of dict (trace JSON)
if isinstance(result, list) and all(isinstance(t, dict) for t in result):
trace = result
@@ -123,10 +127,9 @@ class LegacyAgentRunner(Runner[Any]):
# If the agent has tracing enabled, use the tracer's last trace if not already set
if self.tracer and (trace is None or trace_spans is None):
spans = self.tracer.get_last_trace()
if spans:
trace = [json.loads(readable_span.to_json()) for readable_span in spans]
trace_spans = spans
trace_spans = self.tracer.get_last_trace() # type: ignore
if trace_spans:
trace = [cast(Span, span).model_dump() for span in trace_spans]
# Always extract triplets from the trace using TracerTraceToTriplet
if trace_spans:
+6
View File
@@ -34,6 +34,9 @@ AGL_OPERATION = "agentlightning.operation"
Wrap function or code-blocks as operations.
"""
AGL_REWARD = "agentlightning.reward"
"""Agent-lightning's standard span name for reward operations."""
AGL_VIRTUAL = "agentlightning.virtual"
"""Agent-lightning's standard span name for virtual operations.
@@ -53,6 +56,9 @@ class LightningResourceAttributes(Enum):
SPAN_SEQUENCE_ID = "agentlightning.span_sequence_id"
"""Resource name for span sequence ID in Agent-lightning spans."""
TRACER_NAME = "agentlightning.tracer.name"
"""Which tracer is used to create this span."""
class LightningSpanAttributes(Enum):
"""Attribute names that commonly appear in Agent-lightning spans.
+49 -50
View File
@@ -59,10 +59,12 @@ from agentlightning.types import (
Worker,
WorkerStatus,
)
from agentlightning.utils.metrics import MetricsBackend, get_prometheus_registry
from agentlightning.utils.otlp import handle_otlp_export, spans_from_proto
from agentlightning.utils.server_launcher import LaunchMode, PythonServerLauncher, PythonServerLauncherArgs
from .base import UNSET, LightningStore, LightningStoreCapabilities, LightningStoreStatistics, Unset
from .collection.base import resolve_error_type
from .utils import LATENCY_BUCKETS
server_logger = logging.getLogger("agentlightning.store.server")
@@ -238,7 +240,7 @@ class LightningStoreServer(LightningStore):
launcher_args: The arguments to use for the server launcher.
It's not allowed to set `host`, `port`, `launch_mode` together with `launcher_args`.
n_workers: The number of workers to run in the server. Only applicable for `mp` launch mode.
prometheus: Whether to enable Prometheus metrics.
tracker: The metrics tracker to use for the server.
"""
def __init__(
@@ -250,7 +252,7 @@ class LightningStoreServer(LightningStore):
launch_mode: LaunchMode = "thread",
launcher_args: PythonServerLauncherArgs | None = None,
n_workers: int = 1,
prometheus: bool = False,
tracker: MetricsBackend | None = None,
):
super().__init__()
self.store = store
@@ -268,6 +270,7 @@ class LightningStoreServer(LightningStore):
port=port,
launch_mode=launch_mode,
healthcheck_url=API_V1_AGL_PREFIX + "/health",
n_workers=n_workers,
)
store_capabilities = self.store.capabilities
@@ -287,7 +290,7 @@ class LightningStoreServer(LightningStore):
app=self.app,
args=self.launcher_args,
)
self._prometheus = prometheus
self._tracker = tracker
self._lock: threading.Lock = threading.Lock()
self._cors_allow_origins = self._normalize_cors_origins(cors_allow_origins)
@@ -332,7 +335,6 @@ class LightningStoreServer(LightningStore):
return {
"launcher_args": self.launcher_args,
"server_launcher": self.server_launcher,
"_prometheus": self._prometheus,
"_owner_pid": self._owner_pid,
}
@@ -350,11 +352,12 @@ class LightningStoreServer(LightningStore):
self.store = None
self.launcher_args = state["launcher_args"]
self.server_launcher = state["server_launcher"]
self._prometheus = state["_prometheus"]
self._tracker = None
self._owner_pid = state["_owner_pid"]
self._cors_allow_origins = state.get("_cors_allow_origins")
self._client = None
self._lock = threading.Lock()
self._prometheus_registry = None
# Do NOT reconstruct app, _uvicorn_config, _uvicorn_server
# to avoid transferring server state to subprocess
@@ -435,8 +438,8 @@ class LightningStoreServer(LightningStore):
api = APIRouter(prefix=API_V1_PREFIX)
# The outermost-layer of monitoring
if self._prometheus:
self._setup_prometheus(api=api, app=self.app)
if self._tracker is not None:
self._setup_metrics(api=api, app=self.app)
# TODO: This should only be enabled in development mode.
@self.app.middleware("http")
@@ -844,41 +847,21 @@ class LightningStoreServer(LightningStore):
# Finally, mount the dashboard assets
self._setup_dashboard()
def _setup_prometheus(self, api: APIRouter, app: FastAPI):
def _setup_metrics(self, api: APIRouter, app: FastAPI):
"""Setup Prometheus metrics endpoints."""
try:
from prometheus_client import make_asgi_app # type: ignore
from prometheus_client import (
REGISTRY,
CollectorRegistry,
Counter,
Histogram,
multiprocess,
)
except ImportError:
raise ImportError(
"Prometheus client is not installed. Please either install it or set prometheus to False."
)
if self._tracker is None:
return
# Multi-process mode: https://prometheus.github.io/client_python/multiprocess/
is_multiprocess = self.launcher_args.launch_mode == "mp" and self.launcher_args.n_workers > 1
if is_multiprocess:
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
else:
registry = REGISTRY
HTTP_REQUESTS = Counter(
"http_requests_total",
"Total HTTP requests",
["method", "path", "status_code"],
self._tracker.register_counter(
"agl.http.total",
["path", "method", "status"],
group_level=2,
)
HTTP_LATENCY = Histogram(
"http_request_duration_seconds",
"Latency of HTTP requests",
["method", "path", "status_code"],
self._tracker.register_histogram(
"agl.http.latency",
["path", "method", "status"],
buckets=LATENCY_BUCKETS,
group_level=2,
)
def get_template_path(path: str) -> str:
@@ -904,9 +887,12 @@ class LightningStoreServer(LightningStore):
return path
@app.middleware("http")
async def prometheus_http_middleware( # pyright: ignore[reportUnusedFunction]
async def tracking_middleware( # pyright: ignore[reportUnusedFunction]
request: Request, call_next: Callable[[Request], Awaitable[Response]]
) -> Response:
if self._tracker is None:
return await call_next(request)
start = time.perf_counter()
status = 520 # Default to 520 if things crash hard
@@ -917,10 +903,11 @@ class LightningStoreServer(LightningStore):
except asyncio.CancelledError:
# Client disconnected (Timeout)
status = 499 # Standard Nginx code for "Client Closed Request"
server_logger.debug(f"Client disconnected (Timeout): {request.url.path}", exc_info=True)
raise # Re-raise to let Uvicorn handle the cleanup
except Exception:
# TODO: Record the error type
status = 500
except Exception as exc:
status = resolve_error_type(exc)
server_logger.debug(f"Server error: {request.url.path}", exc_info=True)
raise
finally:
# This block executes NO MATTER WHAT happens above
@@ -930,13 +917,25 @@ class LightningStoreServer(LightningStore):
path = get_template_path(request.url.path)
method = request.method
HTTP_REQUESTS.labels(method, path, status).inc()
HTTP_LATENCY.labels(method, path, status).observe(elapsed)
await self._tracker.inc_counter(
"agl.http.total",
labels={"method": method, "path": path, "status": str(status)},
)
await self._tracker.observe_histogram(
"agl.http.latency",
value=elapsed,
labels={"method": method, "path": path, "status": str(status)},
)
metrics_app = make_asgi_app(registry=registry) # type: ignore
if self._tracker.has_prometheus():
from prometheus_client import make_asgi_app # pyright: ignore[reportUnknownVariableType]
# This App would need to be accessed via /v1/prometheus/ (note the trailing slash)
app.mount(api.prefix + "/prometheus", metrics_app) # pyright: ignore[reportUnknownArgumentType]
metrics_app = make_asgi_app( # pyright: ignore[reportUnknownVariableType]
registry=get_prometheus_registry()
)
# This App would need to be accessed via /v1/prometheus/ (note the trailing slash)
app.mount(api.prefix + "/prometheus", metrics_app) # pyright: ignore[reportUnknownArgumentType]
def _setup_otlp(self, api: APIRouter):
"""Setup OTLP endpoints."""
@@ -1521,7 +1520,7 @@ class LightningStoreClient(LightningStore):
except aiohttp.ClientResponseError as cre:
# Respect app-level 4xx as final
# 4xx => application issue; do not retry (except 408 which is transient)
client_logger.debug(f"ClientResponseError: {cre.status} {cre.message}", exc_info=True)
client_logger.debug(f"ClientResponseError ({method} {path}): {cre.status} {cre.message}", exc_info=True)
if 400 <= cre.status < 500 and cre.status != 408:
raise
# 5xx and others will be retried below if they raise
@@ -1537,9 +1536,9 @@ class LightningStoreClient(LightningStore):
asyncio.TimeoutError,
) as net_exc:
# Network/session issue: probe health before retrying
client_logger.debug(f"Network/session issue: {net_exc}", exc_info=True)
client_logger.debug(f"Network/session issue ({method} {path}): {net_exc}", exc_info=True)
last_exc = net_exc
client_logger.info(f"Network/session issue will be retried. Retrying the request {method}: {path}")
client_logger.info(f"Network/session issue: {net_exc} - will retry the request {method}: {path}")
if not await self._wait_until_healthy(session):
break # server is not healthy, do not retry
+179 -6
View File
@@ -2,6 +2,10 @@
from __future__ import annotations
import functools
import time
from contextlib import asynccontextmanager
from numbers import Real
from typing import (
TYPE_CHECKING,
Any,
@@ -18,10 +22,14 @@ from typing import (
Sequence,
Tuple,
Type,
TypeGuard,
TypeVar,
cast,
)
from agentlightning.store.utils import LATENCY_BUCKETS
from agentlightning.utils.metrics import MetricsBackend
if TYPE_CHECKING:
from typing import Self
@@ -40,19 +48,146 @@ from agentlightning.types import (
T = TypeVar("T") # Recommended to be a BaseModel
K = TypeVar("K")
V = TypeVar("V")
T_callable = TypeVar("T_callable", bound=Callable[..., Any])
AtomicMode = Literal["r", "w", "rw"]
"""What is expected within the atomic context. Can be "read", "write", or "read-write"."""
AtomicLabels = Literal["rollouts", "attempts", "spans", "resources", "workers", "rollout_queue", "span_sequence_ids"]
AtomicLabels = Literal[
"rollouts", "attempts", "spans", "resources", "workers", "rollout_queue", "span_sequence_ids", "generic"
]
"""Labels for atomic operations.
These labels are used to identify the collections that are affected by the atomic operation.
The `generic` label is used to identify atomic operations that are not associated with any specific collection.
"""
class Collection(Generic[T]):
"""Behaves like a list of items. Supporting addition, updating, and deletion of items."""
def resolve_error_type(exc: BaseException | None) -> str:
if exc is None:
return "N/A"
try:
from .mongo import resolve_mongo_error_type
error_type = resolve_mongo_error_type(exc)
if error_type is not None:
return error_type
except ImportError:
# If the mongo backend is not available, fall back to using the exception's class name.
pass
return exc.__class__.__name__
def tracked(operation: str):
"""Decorator to track the execution of the decorated method."""
def decorator(func: T_callable) -> T_callable:
@functools.wraps(func)
async def wrapper(self: TrackedCollection, *args: Any, **kwargs: Any) -> Any:
async with self.tracking_context(operation, self.collection_name):
return await func(self, *args, **kwargs)
return cast(T_callable, wrapper)
return decorator
def ensure_numeric(value: Any, *, description: str) -> TypeGuard[Real]:
"""Validate that *value* behaves like a real number.
Returns true or crashes.
"""
if isinstance(value, bool):
raise TypeError(f"{description} must be numeric; got bool")
if not isinstance(value, Real):
raise TypeError(f"{description} must be numeric; got {type(value).__name__}")
return True
class DuplicatedPrimaryKeyError(ValueError):
"""Error raised when a duplicate key is encountered."""
pass
class TrackedCollection:
"""An object that can be tracked by the metrics backend."""
def __init__(self, tracker: MetricsBackend | None = None):
self._tracker = tracker
@property
def tracker(self) -> MetricsBackend | None:
return self._tracker
@property
def collection_name(self) -> str:
"""The identifier of the collection."""
raise NotImplementedError()
@property
def extra_tracking_labels(self) -> Mapping[str, Any]:
"""Extra labels to add to the tracking context."""
return {}
@asynccontextmanager
async def tracking_context(self, operation: str, collection: str):
"""Context manager to track the execution of the decorated method.
Args:
operation: The operation to track.
collection: The collection to track.
"""
if self._tracker is None:
# no-op context manager
yield
else:
from agentlightning.store.collection_based import get_current_store_methods
# Enable tracking
start_time = time.perf_counter()
status: str = "OK"
public_store_method, private_store_method = get_current_store_methods()
try:
yield
except BaseException as exc:
status = resolve_error_type(exc)
raise
finally:
elapsed = time.perf_counter() - start_time
await self._tracker.inc_counter( # pyright: ignore[reportPrivateUsage]
"agl.collections.total",
labels={
"store_pubmeth": public_store_method,
"store_privmeth": private_store_method,
"operation": operation,
"collection": collection,
"status": status,
**self.extra_tracking_labels,
},
)
await self._tracker.observe_histogram( # pyright: ignore[reportPrivateUsage]
"agl.collections.latency",
value=elapsed,
labels={
"store_pubmeth": public_store_method,
"store_privmeth": private_store_method,
"operation": operation,
"collection": collection,
"status": status,
**self.extra_tracking_labels,
},
)
class Collection(TrackedCollection, Generic[T]):
"""Standard collection interface. Behaves like a list of items. Supporting addition, updating, and deletion of items."""
def primary_keys(self) -> Sequence[str]:
"""Get the primary keys of the collection."""
@@ -174,7 +309,7 @@ class Collection(Generic[T]):
raise NotImplementedError()
class Queue(Generic[T]):
class Queue(TrackedCollection, Generic[T]):
"""Behaves like a deque. Supporting appending items to the end and popping items from the front."""
def __repr__(self) -> str:
@@ -228,7 +363,7 @@ class Queue(Generic[T]):
raise NotImplementedError()
class KeyValue(Generic[K, V]):
class KeyValue(TrackedCollection, Generic[K, V]):
"""Behaves like a dictionary. Supporting addition, updating, and deletion of items."""
def __repr__(self) -> str:
@@ -246,6 +381,22 @@ class KeyValue(Generic[K, V]):
"""Set the value for the given key."""
raise NotImplementedError()
async def inc(self, key: K, amount: V) -> V:
"""Increase the numeric value for the given key by `amount` and return the new value.
Raises:
TypeError: If the existing value or `amount` is not numeric.
"""
raise NotImplementedError()
async def chmax(self, key: K, value: V) -> V:
"""Set the value for the given key to the maximum of the current and new value.
Raises:
TypeError: If the existing value or `value` is not numeric.
"""
raise NotImplementedError()
async def pop(self, key: K, default: V | None = None) -> V | None:
"""Pop the value for the given key, or the default value if the key is not found."""
raise NotImplementedError()
@@ -255,13 +406,35 @@ class KeyValue(Generic[K, V]):
raise NotImplementedError()
class LightningCollections:
class LightningCollections(TrackedCollection):
"""Collections of rollouts, attempts, spans, resources, and workers.
[LightningStore][agentlightning.LightningStore] implementations can use this as a storage base
to implement the store API.
"""
def __init__(self, tracker: MetricsBackend | None = None, extra_labels: Optional[Sequence[str]] = None):
super().__init__(tracker=tracker)
self.register_collection_metrics(extra_labels)
def register_collection_metrics(self, extra_labels: Optional[Sequence[str]] = None) -> None:
if self._tracker is None:
return
labels = ["store_pubmeth", "operation", "collection", "store_privmeth", "status"]
if extra_labels is not None:
labels.extend(extra_labels)
self._tracker.register_histogram(
"agl.collections.latency",
labels,
buckets=LATENCY_BUCKETS,
group_level=2,
)
self._tracker.register_counter("agl.collections.total", labels, group_level=2)
@property
def tracker(self) -> MetricsBackend | None:
return self._tracker
@property
def rollouts(self) -> Collection[Rollout]:
"""Collections of rollouts."""
+131 -45
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
import asyncio
import logging
import time
import uuid
import weakref
from collections import deque
from contextlib import AsyncExitStack, asynccontextmanager
@@ -23,12 +23,12 @@ from typing import (
Type,
TypeVar,
Union,
cast,
)
import aiologic
from pydantic import BaseModel
from agentlightning.store.utils import LATENCY_BUCKETS
from agentlightning.types import (
Attempt,
FilterField,
@@ -40,16 +40,21 @@ from agentlightning.types import (
Span,
Worker,
)
from agentlightning.utils.metrics import MetricsBackend
from .base import (
AtomicLabels,
AtomicMode,
Collection,
DuplicatedPrimaryKeyError,
FilterMap,
KeyValue,
LightningCollections,
Queue,
ensure_numeric,
normalize_filter_options,
resolve_sort_options,
tracked,
)
T = TypeVar("T") # Recommended to be a BaseModel, not a dict
@@ -192,10 +197,19 @@ class ListBasedCollection(Collection[T]):
if the field is str-like, 0 if the field is int-like, 0.0 if the field is float-like.
"""
def __init__(self, items: List[T], item_type: Type[T], primary_keys: Sequence[str]):
def __init__(
self,
items: List[T],
item_type: Type[T],
primary_keys: Sequence[str],
id: Optional[str] = None,
tracker: Optional[MetricsBackend] = None,
):
super().__init__(tracker=tracker)
if not primary_keys:
raise ValueError("primary_keys must be non-empty")
self._id = id if id is not None else str(uuid.uuid4())
self._items: Dict[Any, Any] = {}
self._size: int = 0
if issubclass(item_type, dict):
@@ -207,6 +221,10 @@ class ListBasedCollection(Collection[T]):
for item in items or []:
self._mutate_single(item, mode="insert")
@property
def collection_name(self) -> str:
return self._id
def primary_keys(self) -> Sequence[str]:
"""Return the primary key field names for this collection."""
return self._primary_keys
@@ -299,7 +317,9 @@ class ListBasedCollection(Collection[T]):
if mode == "insert":
if exists:
raise ValueError(f"Item already exists with primary key(s): {self._render_key_values(key_values)}")
raise DuplicatedPrimaryKeyError(
f"Item already exists with primary key(s): {self._render_key_values(key_values)}"
)
parent[final_key] = item
self._size += 1
else: # upsert
@@ -483,6 +503,7 @@ class ListBasedCollection(Collection[T]):
# No items exist for this primary-key prefix.
return ()
@tracked("query")
async def query(
self,
filter: Optional[FilterOptions] = None,
@@ -546,6 +567,7 @@ class ListBasedCollection(Collection[T]):
total=total_matched,
)
@tracked("get")
async def get(
self,
filter: Optional[FilterOptions] = None,
@@ -582,11 +604,12 @@ class ListBasedCollection(Collection[T]):
return best_item
@tracked("insert")
async def insert(self, items: Sequence[T]) -> None:
"""Insert the given items.
Raises:
ValueError: If any item with the same primary keys already exists.
DuplicatedPrimaryKeyError: If any item with the same primary keys already exists.
"""
seen_keys: set[Tuple[Any, ...]] = set()
prepared: List[T] = []
@@ -594,8 +617,8 @@ class ListBasedCollection(Collection[T]):
self._ensure_item_type(item)
key_values = self._extract_primary_key_values(item)
if key_values in seen_keys:
raise ValueError(
f"Insert payload contains duplicate primary key(s): {self._render_key_values(key_values)}"
raise DuplicatedPrimaryKeyError(
f"Insert payload contains duplicated primary key(s): {self._render_key_values(key_values)}"
)
seen_keys.add(key_values)
prepared.append(item)
@@ -603,6 +626,7 @@ class ListBasedCollection(Collection[T]):
for item in prepared:
self._mutate_single(item, mode="insert")
@tracked("update")
async def update(self, items: Sequence[T], update_fields: Sequence[str] | None = None) -> Sequence[T]:
"""Update the given items.
@@ -617,6 +641,7 @@ class ListBasedCollection(Collection[T]):
updated_items.append(updated)
return updated_items
@tracked("upsert")
async def upsert(self, items: Sequence[T], update_fields: Sequence[str] | None = None) -> Sequence[T]:
"""Upsert the given items (insert if missing, otherwise update)."""
upserted_items: List[T] = []
@@ -627,6 +652,7 @@ class ListBasedCollection(Collection[T]):
upserted_items.append(upserted)
return upserted_items
@tracked("delete")
async def delete(self, items: Sequence[T]) -> None:
"""Delete the given items.
@@ -646,23 +672,37 @@ class DequeBasedQueue(Queue[T]):
Provides O(1) amortized enqueue (append) and dequeue (popleft).
"""
def __init__(self, item_type: Type[T], items: Optional[Sequence[T]] = None):
def __init__(
self,
item_type: Type[T],
items: Optional[Sequence[T]] = None,
id: Optional[str] = None,
tracker: Optional[MetricsBackend] = None,
):
super().__init__(tracker=tracker)
self._items: Deque[T] = deque()
self._item_type: Type[T] = item_type
self._id = id if id is not None else str(uuid.uuid4())
if items:
self._items.extend(items)
def item_type(self) -> Type[T]:
return self._item_type
@property
def collection_name(self) -> str:
return self._id
def __repr__(self) -> str:
return f"<{self.__class__.__name__}[{self.item_type().__name__}] ({len(self._items)})>"
@tracked("has")
async def has(self, item: T) -> bool:
if not isinstance(item, self._item_type):
raise TypeError(f"Expected item of type {self._item_type.__name__}, got {type(item).__name__}")
return item in self._items
@tracked("enqueue")
async def enqueue(self, items: Sequence[T]) -> Sequence[T]:
for item in items:
if not isinstance(item, self._item_type):
@@ -670,6 +710,7 @@ class DequeBasedQueue(Queue[T]):
self._items.append(item)
return items
@tracked("dequeue")
async def dequeue(self, limit: int = 1) -> Sequence[T]:
if limit <= 0:
return []
@@ -678,6 +719,7 @@ class DequeBasedQueue(Queue[T]):
out.append(self._items.popleft())
return out
@tracked("peek")
async def peek(self, limit: int = 1) -> Sequence[T]:
if limit <= 0:
return []
@@ -689,6 +731,7 @@ class DequeBasedQueue(Queue[T]):
result.append(item)
return result
@tracked("size")
async def size(self) -> int:
return len(self._items)
@@ -696,21 +739,61 @@ class DequeBasedQueue(Queue[T]):
class DictBasedKeyValue(KeyValue[K, V]):
"""KeyValue implementation backed by a plain dictionary."""
def __init__(self, data: Optional[Mapping[K, V]] = None):
def __init__(
self, data: Optional[Mapping[K, V]] = None, id: Optional[str] = None, tracker: Optional[MetricsBackend] = None
):
super().__init__(tracker=tracker)
self._values: Dict[K, V] = dict(data) if data else {}
self._id = id if id is not None else str(uuid.uuid4())
@property
def collection_name(self) -> str:
return self._id
@tracked("has")
async def has(self, key: K) -> bool:
return key in self._values
@tracked("get")
async def get(self, key: K, default: V | None = None) -> V | None:
return self._values.get(key, default)
@tracked("set")
async def set(self, key: K, value: V) -> None:
self._values[key] = value
@tracked("inc")
async def inc(self, key: K, amount: V) -> V:
assert ensure_numeric(amount, description="amount")
if key in self._values:
current_value = self._values[key]
assert ensure_numeric(current_value, description=f"value for key {key!r}")
new_value = cast(V, current_value + amount)
self._values[key] = new_value
else:
new_value = amount
self._values[key] = new_value
return new_value
@tracked("chmax")
async def chmax(self, key: K, value: V) -> V:
assert ensure_numeric(value, description="value")
if key in self._values:
current_value = self._values[key]
assert ensure_numeric(current_value, description=f"value for key {key!r}")
if value > current_value:
self._values[key] = value
return value
return current_value
else:
self._values[key] = value
return value
@tracked("pop")
async def pop(self, key: K, default: V | None = None) -> V | None:
return self._values.pop(key, default)
@tracked("size")
async def size(self) -> int:
return len(self._values)
@@ -721,8 +804,9 @@ class InMemoryLightningCollections(LightningCollections):
Serves as the storage base for [`InMemoryLightningStore`][agentlightning.InMemoryLightningStore].
"""
def __init__(self, lock_type: Literal["thread", "asyncio"], prometheus: bool = False):
self._lock = {
def __init__(self, lock_type: Literal["thread", "asyncio"], tracker: MetricsBackend | None = None):
super().__init__(tracker=tracker)
self._lock: Mapping[AtomicLabels, _LoopAwareAsyncLock | _ThreadSafeAsyncLock] = {
"rollouts": _LoopAwareAsyncLock() if lock_type == "asyncio" else _ThreadSafeAsyncLock(),
"attempts": _LoopAwareAsyncLock() if lock_type == "asyncio" else _ThreadSafeAsyncLock(),
"spans": _LoopAwareAsyncLock() if lock_type == "asyncio" else _ThreadSafeAsyncLock(),
@@ -730,32 +814,31 @@ class InMemoryLightningCollections(LightningCollections):
"workers": _LoopAwareAsyncLock() if lock_type == "asyncio" else _ThreadSafeAsyncLock(),
"rollout_queue": _LoopAwareAsyncLock() if lock_type == "asyncio" else _ThreadSafeAsyncLock(),
"span_sequence_ids": _LoopAwareAsyncLock() if lock_type == "asyncio" else _ThreadSafeAsyncLock(),
"generic": _LoopAwareAsyncLock() if lock_type == "asyncio" else _ThreadSafeAsyncLock(),
}
self._rollouts = ListBasedCollection(items=[], item_type=Rollout, primary_keys=["rollout_id"])
self._attempts = ListBasedCollection(items=[], item_type=Attempt, primary_keys=["rollout_id", "attempt_id"])
self._spans = ListBasedCollection(
items=[], item_type=Span, primary_keys=["rollout_id", "attempt_id", "span_id"]
self._rollouts = ListBasedCollection(
items=[], item_type=Rollout, primary_keys=["rollout_id"], id="rollouts", tracker=tracker
)
self._resources = ListBasedCollection(items=[], item_type=ResourcesUpdate, primary_keys=["resources_id"])
self._workers = ListBasedCollection(items=[], item_type=Worker, primary_keys=["worker_id"])
self._rollout_queue = DequeBasedQueue(items=[], item_type=str)
self._span_sequence_ids = DictBasedKeyValue[str, int](data={}) # rollout_id -> sequence_id
self._attempts = ListBasedCollection(
items=[], item_type=Attempt, primary_keys=["rollout_id", "attempt_id"], id="attempts", tracker=tracker
)
self._spans = ListBasedCollection(
items=[], item_type=Span, primary_keys=["rollout_id", "attempt_id", "span_id"], id="spans", tracker=tracker
)
self._resources = ListBasedCollection(
items=[], item_type=ResourcesUpdate, primary_keys=["resources_id"], id="resources", tracker=tracker
)
self._workers = ListBasedCollection(
items=[], item_type=Worker, primary_keys=["worker_id"], id="workers", tracker=tracker
)
self._rollout_queue = DequeBasedQueue(items=[], item_type=str, id="rollout_queue", tracker=tracker)
self._span_sequence_ids = DictBasedKeyValue[str, int](
data={}, id="span_sequence_ids", tracker=tracker
) # rollout_id -> sequence_id
self._prometheus = prometheus
if self._prometheus:
from prometheus_client import Counter, Histogram
self._rate_metric = Counter(
"memory_collection_lock_rate",
"Rate of memory collection locks",
["collection"],
)
self._latency_metric = Histogram(
"memory_collection_lock_latency_seconds",
"Latency of memory collection locks",
["collection"],
buckets=LATENCY_BUCKETS,
)
@property
def collection_name(self) -> str:
return "router"
@property
def rollouts(self) -> ListBasedCollection[Rollout]:
@@ -787,7 +870,12 @@ class InMemoryLightningCollections(LightningCollections):
@asynccontextmanager
async def atomic(
self, *, mode: AtomicMode = "rw", snapshot: bool = False, labels: Optional[Sequence[str]] = None, **kwargs: Any
self,
*,
mode: AtomicMode = "rw",
snapshot: bool = False,
labels: Optional[Sequence[AtomicLabels]] = None,
**kwargs: Any,
):
"""In-memory collections apply a lock outside. It doesn't need to manipulate the collections inside.
@@ -807,17 +895,15 @@ class InMemoryLightningCollections(LightningCollections):
# are trying to acquire the same locks in different orders.
labels = sorted(labels)
managers = [(label, self._lock[label]) for label in labels]
async with AsyncExitStack() as stack:
for label, manager in managers:
start_time = time.perf_counter()
await stack.enter_async_context(manager)
elapsed = time.perf_counter() - start_time
if self._prometheus:
self._rate_metric.labels(collection=label).inc()
self._latency_metric.labels(collection=label).observe(elapsed)
yield self
async with self.tracking_context(operation="atomic", collection=self.collection_name):
managers = [(label, self._lock[label]) for label in labels]
async with AsyncExitStack() as stack:
for label, manager in managers:
async with self.tracking_context(operation="lock", collection=label):
await stack.enter_async_context(manager)
yield self
@tracked("evict_spans_for_rollout")
async def evict_spans_for_rollout(self, rollout_id: str) -> None:
"""Evict all spans for a given rollout ID.
File diff suppressed because it is too large Load Diff
+229 -136
View File
@@ -15,12 +15,11 @@ from __future__ import annotations
import asyncio
import functools
import hashlib
import logging
import time
import uuid
import warnings
from collections import defaultdict
from contextvars import ContextVar
from types import CoroutineType
from typing import (
Any,
@@ -60,6 +59,8 @@ from agentlightning.types import (
Worker,
WorkerStatus,
)
from agentlightning.utils.id import generate_id
from agentlightning.utils.metrics import MetricsBackend
from .base import (
UNSET,
@@ -71,7 +72,7 @@ from .base import (
is_queuing,
)
from .collection import FilterOptions, LightningCollections
from .collection.base import AtomicLabels
from .collection.base import AtomicLabels, DuplicatedPrimaryKeyError
from .utils import LATENCY_BUCKETS, rollout_status_from_attempt, scan_unhealthy_rollouts
T_callable = TypeVar("T_callable", bound=Callable[..., Any])
@@ -86,6 +87,12 @@ SelfT = TypeVar("SelfT", bound="CollectionBasedLightningStore[Any]")
logger = logging.getLogger(__name__)
# ContextVars for tracking the current store method without expensive stack introspection.
# These are set by the @tracked decorator and read by tracking_context in collection/base.py.
_UNKNOWN_STORE_METHOD = "unknown"
_current_public_store_method: ContextVar[str] = ContextVar("public_store_method", default=_UNKNOWN_STORE_METHOD)
_current_private_store_method: ContextVar[str] = ContextVar("private_store_method", default=_UNKNOWN_STORE_METHOD)
def _with_collections_execute(labels: Sequence[AtomicLabels]):
"""Hands over the function execution to the collections.execute method.
@@ -119,34 +126,51 @@ def _with_collections_execute(labels: Sequence[AtomicLabels]):
def tracked(name: str):
"""Decorator to track the execution of the decorated method with Prometheus."""
_public_methods = frozenset([name for name in LightningStore.__dict__ if not name.startswith("_")])
def decorator(func: T_callable) -> T_callable:
@functools.wraps(func)
async def wrapper(self: CollectionBasedLightningStore[T_collections], *args: Any, **kwargs: Any) -> Any:
# For backtracking in Mongo-collection methods.
# Only track the public methods (+healthcheck)
if name in _public_methods or name == "_healthcheck":
method_name = name # pyright: ignore[reportUnusedVariable]
else:
method_name = None # pyright: ignore[reportUnusedVariable]
# Get the current public method from ContextVar (set by outer tracked methods)
public_meth_in_stack = _current_public_store_method.get()
if not self._prometheus: # pyright: ignore[reportPrivateUsage]
# Skip the tracking because tracking is not configured
return await func(self, *args, **kwargs)
# Set ContextVars for nested calls to read. Use tokens for proper cleanup.
pub_token = None
priv_token = None
if name in COLLECTION_STORE_PUBLIC_METHODS:
pub_token = _current_public_store_method.set(name)
public_meth_in_stack = name # We are in a public method already.
if name in COLLECTION_STORE_ALL_METHODS:
priv_token = _current_private_store_method.set(name)
start_time = time.perf_counter()
try:
ret = await func(self, *args, **kwargs)
self._total_metric.labels(name, "OK").inc() # pyright: ignore[reportPrivateUsage]
return ret
except Exception as exc:
self._total_metric.labels(name, exc.__class__.__name__).inc() # pyright: ignore[reportPrivateUsage]
raise
if self._tracker is None: # pyright: ignore[reportPrivateUsage]
# Skip the tracking because tracking is not configured
return await func(self, *args, **kwargs)
start_time = time.perf_counter()
status: str = "OK"
try:
return await func(self, *args, **kwargs)
except BaseException as exc:
status = exc.__class__.__name__
raise
finally:
elapsed = time.perf_counter() - start_time
await self._tracker.inc_counter( # pyright: ignore[reportPrivateUsage]
"agl.store.total",
labels={"method": name, "store_pubmeth": public_meth_in_stack, "status": status},
)
await self._tracker.observe_histogram( # pyright: ignore[reportPrivateUsage]
"agl.store.latency",
value=elapsed,
labels={"method": name, "store_pubmeth": public_meth_in_stack, "status": status},
)
finally:
elapsed = time.perf_counter() - start_time
self._latency_metric.labels(name).observe(elapsed) # pyright: ignore[reportPrivateUsage]
# Reset ContextVars to their previous values
if pub_token is not None:
_current_public_store_method.reset(pub_token)
if priv_token is not None:
_current_private_store_method.reset(priv_token)
return cast(T_callable, wrapper)
@@ -185,19 +209,16 @@ def healthcheck_before(func: T_callable) -> T_callable:
def _generate_resources_id() -> str:
short_id = hashlib.sha1(uuid.uuid4().bytes).hexdigest()[:12]
return "rs-" + short_id
return "rs-" + generate_id(12)
def _generate_rollout_id() -> str:
short_id = hashlib.sha1(uuid.uuid4().bytes).hexdigest()[:12]
return "ro-" + short_id
return "ro-" + generate_id(12)
def _generate_attempt_id() -> str:
"""We don't need that long because attempts are limited to rollouts."""
short_id = hashlib.sha1(uuid.uuid4().bytes).hexdigest()[:8]
return "at-" + short_id
return "at-" + generate_id(8)
class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
@@ -215,40 +236,56 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
read_snapshot: Make sure read operations are atomic. If set to true,
all read operations like `query_rollouts` will have better consistency.
It may use an isolated snapshot that supports repeatable reads.
prometheus: Enable Prometheus tracking.
tracker: Enable metrics tracking.
scan_debounce_seconds: The debounce time for the scan for unhealthy rollouts.
Set to 0 to disable debouncing. The debounce is a non-perfect traffic control.
It's isolated for each store instance if there are multiple worker replicas.
"""
def __init__(self, collections: T_collections, *, read_snapshot: bool = False, prometheus: bool = False):
def __init__(
self,
collections: T_collections,
*,
read_snapshot: bool = False,
tracker: MetricsBackend | None = None,
scan_debounce_seconds: float = 10.0,
) -> None:
# rollouts and spans' storage
self.collections = collections
self._read_snapshot = read_snapshot
self._prometheus = prometheus
self._tracker = tracker
self._launch_time = time.time()
if prometheus:
from prometheus_client import Counter, Histogram
# Control scan debounce to avoid overloading the store.
self._scan_debounce_seconds = scan_debounce_seconds
last_scan_time = self._launch_time
if self._scan_debounce_seconds > 0:
# Allow the first scan immediately after instantiation
last_scan_time -= self._scan_debounce_seconds
self._last_scan_entrance_time = last_scan_time
self._latency_metric = Histogram(
"collection_store_latency_seconds",
"Latency of CollectionBasedLightningStore methods",
["method"],
if self._tracker is not None:
self._tracker.register_histogram(
"agl.store.latency",
["method", "store_pubmeth", "status"],
buckets=LATENCY_BUCKETS,
group_level=1,
)
self._total_metric = Counter(
"collection_store_total",
"Total MongoDB operations",
["method", "error_type"],
self._tracker.register_counter(
"agl.store.total",
["method", "store_pubmeth", "status"],
group_level=1,
)
self._rollout_counter = Counter(
"collection_store_rollout_total",
"Total rollouts",
self._tracker.register_counter(
"agl.rollouts.total",
["status", "mode"],
group_level=1,
)
self._rollout_duration_metric = Histogram(
"collection_store_rollout_duration_seconds",
"Duration of rollouts",
self._tracker.register_histogram(
"agl.rollouts.duration",
["status", "mode"],
buckets=LATENCY_BUCKETS,
group_level=1,
)
async def statistics(self) -> LightningStoreStatistics:
@@ -610,6 +647,7 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
if not dequeued:
break
rollout_id = dequeued[0]
logger.debug("Rollout ID %s has been dequeued by Worker ID %s", rollout_id, worker_id)
post_dequeue_result = await self._post_dequeue_rollouts([rollout_id], worker_id)
if post_dequeue_result:
@@ -617,6 +655,7 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
attempted_rollout, _ = post_dequeue_result[0]
if worker_id is not None:
await self._sync_workers_with_attempts([attempted_rollout.attempt], dequeue=True)
logger.debug("Rollout has been prepared for Worker ID %s: %s", worker_id, attempted_rollout)
return attempted_rollout
# else continue the loop
@@ -967,37 +1006,36 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
return await self._get_latest_resources()
@tracked("_issue_many_span_sequence_ids")
@_with_collections_execute(labels=["span_sequence_ids"])
async def _issue_many_span_sequence_ids(self, collections: T_collections, rollout_ids: List[str]) -> List[int]:
async def _issue_many_span_sequence_ids(self, rollout_ids: List[str]) -> List[int]:
"""Issue a new span sequence ID for a given rollout."""
# Cache the next sequence IDs for the rollouts (for both RW)
next_sequence_ids_cache: Dict[str, int] = {}
if not rollout_ids:
return []
request_counts: Dict[str, int] = defaultdict(int)
for rollout_id in rollout_ids:
request_counts[rollout_id] += 1
latest_values: Dict[str, int] = {}
for rollout_id, count in request_counts.items():
async with self.collections.atomic(mode="rw", snapshot=False, labels=["span_sequence_ids"]) as collections:
latest_values[rollout_id] = await collections.span_sequence_ids.inc(rollout_id, count)
next_value_tracker: Dict[str, int] = {
rollout_id: latest_values[rollout_id] - request_counts[rollout_id] for rollout_id in request_counts
}
result: List[int] = []
for rollout_id in rollout_ids:
if rollout_id not in next_sequence_ids_cache:
retrieved_id = await collections.span_sequence_ids.get(rollout_id)
if retrieved_id is None:
retrieved_id = 0
next_sequence_ids_cache[rollout_id] = retrieved_id
# Increment the sequence ID for the rollout
next_sequence_ids_cache[rollout_id] += 1
result.append(next_sequence_ids_cache[rollout_id])
# Propagate the cache to storage
for rollout_id, sequence_id in next_sequence_ids_cache.items():
await collections.span_sequence_ids.set(rollout_id, sequence_id)
next_value_tracker[rollout_id] += 1
result.append(next_value_tracker[rollout_id])
return result
@tracked("_sync_span_sequence_id")
@_with_collections_execute(labels=["span_sequence_ids"])
async def _sync_span_sequence_id(self, collections: T_collections, rollout_id: str, sequence_id: int) -> None:
async def _sync_span_sequence_id(self, rollout_id: str, sequence_id: int) -> None:
"""Sync the span sequence ID for a given rollout from the input span sequence ID."""
existing_sequence_id = await collections.span_sequence_ids.get(rollout_id)
if existing_sequence_id is None:
existing_sequence_id = 0
await collections.span_sequence_ids.set(rollout_id, max(existing_sequence_id, sequence_id))
async with self.collections.atomic(mode="rw", snapshot=False, labels=["span_sequence_ids"]) as collections:
await collections.span_sequence_ids.chmax(rollout_id, sequence_id)
@tracked("get_next_span_sequence_id")
async def get_next_span_sequence_id(self, rollout_id: str, attempt_id: str) -> int:
@@ -1079,13 +1117,11 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
try:
await collections.spans.insert([span])
return True
except ValueError as e:
if "already exists" in str(e) or "contains duplicate" in str(e):
logger.error(
f"Duplicated span added for rollout={span.rollout_id}, attempt={span.attempt_id}, span={span.span_id}. Skipping."
)
return False
raise
except DuplicatedPrimaryKeyError:
logger.error(
f"Duplicated span added for rollout={span.rollout_id}, attempt={span.attempt_id}, span={span.span_id}. Skipping."
)
return False
successful_spans: List[Span] = []
try:
@@ -1093,22 +1129,20 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
async with self.collections.atomic(
mode="w", snapshot=self._read_snapshot, commit=False, labels=["spans"]
) as collections:
# FIXME: Part of the insertion might complete though the full operation fails.
# In that case, the "insert spans" return values might not be accurate.
await collections.spans.insert(spans)
successful_spans.extend(spans)
except ValueError as e:
if "already exists" in str(e) or "contains duplicate" in str(e):
# There is a duplicate span, we warn it
# We fallback to adding the spans one by one
async def _add_many_spans_fallback(collections: T_collections):
for span in spans:
if await _add_span_fallback(collections, span):
successful_spans.append(span)
await self.collections.execute(
_add_many_spans_fallback, mode="w", snapshot=self._read_snapshot, commit=True, labels=["spans"]
)
else:
raise
except DuplicatedPrimaryKeyError:
# There is a duplicate span, we warn it
# We fallback to adding the spans one by one
for span in spans:
async with self.collections.atomic(
mode="w", snapshot=self._read_snapshot, labels=["spans"]
) as collections:
# No need to commit here, it will be simple atomic write operations
if await _add_span_fallback(collections, span):
successful_spans.append(span)
return successful_spans
@@ -1140,6 +1174,8 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
if successful_spans:
await self._post_add_spans(successful_spans, rollout_id, attempt_id)
logger.debug("Added %d spans for rollout %s, attempt %s", len(successful_spans), rollout_id, attempt_id)
return successful_spans
@tracked("_post_add_spans")
@@ -1158,48 +1194,46 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
if not spans:
return
async def _update_rollout_attempt(collections: T_collections) -> Optional[Tuple[Rollout, Sequence[str]]]:
attempt = await collections.attempts.get(
{"rollout_id": {"exact": rollout_id}, "attempt_id": {"exact": attempt_id}}
)
if attempt is None:
return None
rollout = await collections.rollouts.get({"rollout_id": {"exact": rollout_id}})
if rollout is None:
return None
# Update attempt heartbeat and ensure persistence
attempt.last_heartbeat_time = time.time()
if attempt.status in ["preparing", "unresponsive"]:
attempt.status = "running"
await collections.attempts.update([attempt], update_fields=["last_heartbeat_time", "status"])
# If the status has already timed out or failed, do not change it (but heartbeat is still recorded)
# Update rollout status if it's the latest attempt
rollout_updated: bool = False
updated_fields: List[str] = []
latest_attempt = await self._unlocked_get_latest_attempt(collections, rollout.rollout_id)
if latest_attempt is not None and attempt.attempt_id == latest_attempt.attempt_id:
if rollout.status in ["preparing", "queueing", "requeuing"]:
# If rollout is currently preparing or queuing, set it to running
rollout.status = "running"
await collections.rollouts.update([rollout], update_fields=["status"])
rollout_updated = True
updated_fields = ["status"]
# Otherwise, the rollout has succeeded or failed, do nothing
return (rollout, updated_fields) if rollout_updated else None
rollout_update = await self.collections.execute(
_update_rollout_attempt,
mode="rw",
snapshot=self._read_snapshot,
commit=True,
labels=["rollouts", "attempts"],
)
rollout_update = await self._on_attempt_heartbeat(rollout_id=rollout_id, attempt_id=attempt_id)
if rollout_update is not None:
await self._post_update_rollout([rollout_update])
@tracked("_on_attempt_heartbeat")
@_with_collections_execute(labels=["rollouts", "attempts"])
async def _on_attempt_heartbeat(
self, collections: T_collections, rollout_id: str, attempt_id: str
) -> Optional[Tuple[Rollout, Sequence[str]]]:
attempt = await collections.attempts.get(
{"rollout_id": {"exact": rollout_id}, "attempt_id": {"exact": attempt_id}}
)
if attempt is None:
return None
rollout = await collections.rollouts.get({"rollout_id": {"exact": rollout_id}})
if rollout is None:
return None
# Update attempt heartbeat and ensure persistence
attempt.last_heartbeat_time = time.time()
if attempt.status in ["preparing", "unresponsive"]:
attempt.status = "running"
await collections.attempts.update([attempt], update_fields=["last_heartbeat_time", "status"])
# If the status has already timed out or failed, do not change it (but heartbeat is still recorded)
# Update rollout status if it's the latest attempt
rollout_updated: bool = False
updated_fields: List[str] = []
latest_attempt = await self._unlocked_get_latest_attempt(collections, rollout.rollout_id)
if latest_attempt is not None and attempt.attempt_id == latest_attempt.attempt_id:
if rollout.status in ["preparing", "queueing", "requeuing"]:
# If rollout is currently preparing or queuing, set it to running
rollout.status = "running"
await collections.rollouts.update([rollout], update_fields=["status"])
rollout_updated = True
updated_fields = ["status"]
# Otherwise, the rollout has succeeded or failed, do nothing
return (rollout, updated_fields) if rollout_updated else None
@tracked("wait_for_rollouts")
@healthcheck_before
async def wait_for_rollouts(self, *, rollout_ids: List[str], timeout: Optional[float] = None) -> List[Rollout]:
@@ -1220,7 +1254,17 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
logger.error(f"Error waiting for rollout {rollout_id}: {rollout}")
# Filter out the exceptions
return [rollout for rollout in rollouts if isinstance(rollout, Rollout)]
ret = [rollout for rollout in rollouts if isinstance(rollout, Rollout)]
finished_rollout_ids = set([rollout.rollout_id for rollout in ret])
unfinished_rollout_ids = set(rollout_ids) - finished_rollout_ids
logger.debug(
"Waiting for rollouts. Number of finished rollouts: %d; number of unfinished rollouts: %d",
len(finished_rollout_ids),
len(unfinished_rollout_ids),
)
if len(unfinished_rollout_ids) < 30:
logger.debug("Unfinished rollouts: %s", unfinished_rollout_ids)
return ret
@tracked("wait_for_rollout")
async def wait_for_rollout(self, rollout_id: str, timeout: Optional[float] = None) -> Optional[Rollout]:
@@ -1467,10 +1511,17 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
for rollout, updated_fields in rollouts:
# Sometimes "end_time" is set but it's not really updated.
if "end_time" in updated_fields and is_finished(rollout):
if self._prometheus:
self._rollout_counter.labels(rollout.status, rollout.mode).inc()
self._rollout_duration_metric.labels(rollout.status, rollout.mode).observe(
cast(float, rollout.end_time) - rollout.start_time
if self._tracker is not None:
labels = {
"status": rollout.status,
"mode": rollout.mode if rollout.mode is not None else "unknown",
}
duration = cast(float, rollout.end_time) - rollout.start_time
await self._tracker.inc_counter("agl.rollouts.total", labels=labels)
await self._tracker.observe_histogram(
"agl.rollouts.duration",
value=duration,
labels=labels,
)
if not skip_enqueue:
@@ -1644,6 +1695,9 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
@tracked("_scan_for_unhealthy_rollouts")
async def _scan_for_unhealthy_rollouts(self) -> None:
"""Perform healthcheck against all running rollouts in the store."""
if not await self._should_scan_for_unhealthy_rollouts():
return
rollouts, attempts_sync_required = await self._find_and_update_unhealthy_rollouts()
if rollouts:
@@ -1653,6 +1707,25 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
if attempts_sync_required:
await self._sync_workers_with_attempts(attempts_sync_required)
@tracked("_should_scan_for_unhealthy_rollouts")
async def _should_scan_for_unhealthy_rollouts(self) -> bool:
"""Check if the scan for unhealthy rollouts should be performed."""
if self._scan_debounce_seconds <= 0:
return True
now = time.time()
should_scan = now - self._last_scan_entrance_time >= self._scan_debounce_seconds
if not should_scan:
return False
# Someone else may be racing for the same scan. Double-check inside the lock.
async with self.collections.atomic(mode="rw", snapshot=self._read_snapshot, labels=["generic"]):
now = time.time()
if now - self._last_scan_entrance_time < self._scan_debounce_seconds:
return False
self._last_scan_entrance_time = now
return True
@tracked("_find_and_update_unhealthy_rollouts")
@_with_collections_execute(labels=["rollouts", "attempts"])
async def _find_and_update_unhealthy_rollouts(
@@ -1681,3 +1754,23 @@ class CollectionBasedLightningStore(LightningStore, Generic[T_collections]):
if worker_sync_required:
attempts.append(attempt)
return rollouts, attempts
# _scan_for_unhealthy_rollouts is somehow standalone and automatically invoked.
COLLECTION_STORE_PUBLIC_METHODS = frozenset(
[name for name in LightningStore.__dict__ if not name.startswith("_")] + ["_scan_for_unhealthy_rollouts"]
)
COLLECTION_STORE_ALL_METHODS = frozenset([name for name in CollectionBasedLightningStore.__dict__])
def get_current_store_methods() -> Tuple[str, str]:
"""Get the current store method names from ContextVars.
This is a fast O(1) replacement for stack introspection. The ContextVars are
set by the @tracked decorator when entering store methods.
Returns:
A tuple of (public_method_name, private_method_name).
"""
return _current_public_store_method.get(), _current_private_store_method.get()
+10 -5
View File
@@ -28,6 +28,7 @@ import aiologic
from pydantic import BaseModel
from agentlightning.types import AttemptedRollout, NamedResources, PaginatedResult, ResourcesUpdate, Rollout, Span
from agentlightning.utils.metrics import MetricsBackend
from .base import UNSET, LightningStoreCapabilities, LightningStoreStatistics, Unset, is_finished, is_running
from .collection import InMemoryLightningCollections
@@ -72,12 +73,16 @@ class InMemoryLightningStore(CollectionBasedLightningStore[InMemoryLightningColl
Thread-safe and async-compatible but data is not persistent.
Args:
thread_safe: Whether the store is thread-safe.
eviction_memory_threshold: The threshold for evicting spans in bytes.
By default, it's 70% of the total VRAM available.
safe_memory_threshold: The threshold for safe memory usage in bytes.
By default, it's 80% of the eviction threshold.
span_size_estimator: A function to estimate the size of a span in bytes.
By default, it's a simple size estimator that uses sys.getsizeof.
tracker: The metrics tracker to use.
scan_debounce_seconds: The debounce time for the scan for unhealthy rollouts.
Set to 0 to disable debouncing.
"""
def __init__(
@@ -87,13 +92,13 @@ class InMemoryLightningStore(CollectionBasedLightningStore[InMemoryLightningColl
eviction_memory_threshold: float | int | None = None,
safe_memory_threshold: float | int | None = None,
span_size_estimator: Callable[[Span], int] | None = None,
prometheus: bool = False,
tracker: MetricsBackend | None = None,
scan_debounce_seconds: float = 10.0,
):
super().__init__(
collections=InMemoryLightningCollections(
lock_type="thread" if thread_safe else "asyncio", prometheus=prometheus
),
prometheus=prometheus,
collections=InMemoryLightningCollections(lock_type="thread" if thread_safe else "asyncio", tracker=tracker),
tracker=tracker,
scan_debounce_seconds=scan_debounce_seconds,
)
self._thread_safe = thread_safe
+32 -32
View File
@@ -7,24 +7,13 @@ import hashlib
import logging
import time
import uuid
from typing import (
Any,
Callable,
Dict,
List,
Mapping,
Optional,
Sequence,
TypeVar,
Union,
)
from pymongo import AsyncMongoClient
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, TypeVar, Union
from agentlightning.types import Attempt, AttemptedRollout, Rollout
from agentlightning.utils.metrics import MetricsBackend
from .base import LightningStoreCapabilities, is_finished
from .collection.mongo import MongoClientPool, MongoLightningCollections, MongoOperationPrometheusTracker
from .collection.mongo import MongoClientPool, MongoLightningCollections
from .collection_based import CollectionBasedLightningStore, healthcheck_before, tracked
T_callable = TypeVar("T_callable", bound=Callable[..., Any])
@@ -42,27 +31,28 @@ class MongoLightningStore(CollectionBasedLightningStore[MongoLightningCollection
Data is persistent and can be shared between multiple processes.
Args:
client: The MongoDB client. Could be a string URI or an instance of AsyncMongoClient.
database: The MongoDB database. Could be a string name or an instance of AsyncDatabase.
You must provide at least one of client or database.
mongo_uri: MongoDB connection string (defaults to local replica set).
mongo_client_kwargs: Extra keyword arguments forwarded to `AsyncMongoClient`.
database_name: The MongoDB database name. Defaults to ``agentlightning``.
partition_id: The partition id. Useful when sharing the database among multiple Agent-lightning trainers.
tracker: The metrics tracker to use.
scan_debounce_seconds: The debounce time for the scan for unhealthy rollouts.
Set to 0 to disable debouncing.
"""
def __init__(
self,
*,
client: AsyncMongoClient[Mapping[str, Any]] | str,
mongo_uri: str = "mongodb://localhost:27017/?replicaSet=rs0",
mongo_client_kwargs: Mapping[str, Any] | None = None,
database_name: str | None = None,
partition_id: str | None = None,
prometheus: bool = False,
tracker: MetricsBackend | None = None,
scan_debounce_seconds: float = 10.0,
) -> None:
self._enable_prometheus = prometheus
self._auto_created_client = False
if isinstance(client, str):
self._client = AsyncMongoClient[Mapping[str, Any]](client)
self._auto_created_client = True
else:
self._client = client
self._mongo_uri = mongo_uri
self._mongo_client_kwargs = dict(mongo_client_kwargs or {})
if database_name is None:
database_name = "agentlightning"
logger.info("No database name provided, using default 'agentlightning'")
@@ -71,16 +61,20 @@ class MongoLightningStore(CollectionBasedLightningStore[MongoLightningCollection
partition_id = _generate_partition_id()
logger.info("No partition id provided, generated a new one: %s", partition_id)
self._client_pool = MongoClientPool(self._client)
self._client_pool = MongoClientPool[Mapping[str, Any]](
mongo_uri=self._mongo_uri,
mongo_client_kwargs=self._mongo_client_kwargs,
)
super().__init__(
collections=MongoLightningCollections(
self._client_pool,
database_name,
partition_id,
prometheus_tracker=MongoOperationPrometheusTracker(enabled=self._enable_prometheus),
tracker=tracker,
),
prometheus=self._enable_prometheus,
tracker=tracker,
scan_debounce_seconds=scan_debounce_seconds,
)
@property
@@ -96,9 +90,6 @@ class MongoLightningStore(CollectionBasedLightningStore[MongoLightningCollection
async def close(self) -> None:
"""Close the store by closing the client pool."""
await self._client_pool.close()
# If I created the client, I should close it too.
if self._auto_created_client:
await self._client.close()
@tracked("wait_for_rollouts")
@healthcheck_before
@@ -136,6 +127,15 @@ class MongoLightningStore(CollectionBasedLightningStore[MongoLightningCollection
await asyncio.sleep(rest_time)
current_time = time.time()
# Logging will help debugging when there are stuck rollouts.
logger.debug(
"Waiting for rollouts. Number of finished rollouts: %d; number of unfinished rollouts: %d",
len(finished_rollouts),
len(unfinished_rollout_ids),
)
if len(unfinished_rollout_ids) < 30:
logger.debug("Unfinished rollouts: %s", unfinished_rollout_ids)
# Reorder the rollouts to match the input order
return [finished_rollouts[rollout_id] for rollout_id in rollout_ids if rollout_id in finished_rollouts]
+11 -2
View File
@@ -1,7 +1,16 @@
# Copyright (c) Microsoft. All rights reserved.
from .agentops import AgentOpsTracer
from .base import Tracer
from .base import Tracer, clear_active_tracer, get_active_tracer, set_active_tracer
from .dummy import DummyTracer
from .otel import OtelTracer
__all__ = ["AgentOpsTracer", "Tracer", "OtelTracer"]
__all__ = [
"AgentOpsTracer",
"Tracer",
"OtelTracer",
"DummyTracer",
"get_active_tracer",
"set_active_tracer",
"clear_active_tracer",
]
+19 -8
View File
@@ -13,12 +13,13 @@ import agentops.sdk.core
import opentelemetry.trace as trace_api
from agentops.sdk.core import TracingCore
from opentelemetry.sdk.trace import TracerProvider as TracerProviderImpl
from opentelemetry.trace import get_tracer_provider
from opentelemetry.trace.status import StatusCode
from agentlightning.instrumentation import instrument_all, uninstrument_all
from agentlightning.store.base import LightningStore
from agentlightning.utils.otel import get_span_processors, get_tracer_provider
from .base import with_active_tracer_context
from .otel import LightningSpanProcessor, OtelTracer
if TYPE_CHECKING:
@@ -79,13 +80,20 @@ class AgentOpsTracer(OtelTracer):
agentops.init(auto_start_session=False) # type: ignore
logger.info(f"[Worker {worker_id}] AgentOps client initialized.")
else:
logger.warning(f"[Worker {worker_id}] AgentOps client was already initialized.")
logger.warning(f"[Worker {worker_id}] AgentOps client was already initialized. Skip initialization.")
self._lightning_span_processor = LightningSpanProcessor()
# TODO: The span processor cannot be deleted once added.
# This might be a problem if the tracer is entered and exited multiple times.
self._get_tracer_provider().add_span_processor(self._lightning_span_processor) # type: ignore
span_processors = get_span_processors(self._get_tracer_provider(), LightningSpanProcessor)
if len(span_processors) > 0:
logger.warning(
"LightningSpanProcessor already present in TracerProvider. You might have called init_worker() multiple times."
"Agent-lightning will try to reuse the existing LightningSpanProcessor."
)
if len(span_processors) > 1:
logger.error("More than one LightningSpanProcessors present in TracerProvider. This should not happen.")
self._lightning_span_processor = span_processors[0]
else:
self._lightning_span_processor = LightningSpanProcessor()
self._get_tracer_provider().add_span_processor(self._lightning_span_processor) # type: ignore
def teardown_worker(self, worker_id: int) -> None:
super().teardown_worker(worker_id)
@@ -94,6 +102,10 @@ class AgentOpsTracer(OtelTracer):
self.uninstrument(worker_id)
logger.info(f"[Worker {worker_id}] Instrumentation removed.")
# NOTE: The teardown doesn't try to remove the LightningSpanProcessor from the TracerProvider.
# Currently there is no stable way to fully restore the AgentOps state to the initial state.
@with_active_tracer_context
@asynccontextmanager
async def trace_context(
self,
@@ -158,7 +170,6 @@ class AgentOpsTracer(OtelTracer):
with self._agentops_trace_context(rollout_id, attempt_id, kwargs):
yield trace_api.get_tracer(__name__, tracer_provider=tracer_provider)
elif store is None and rollout_id is None and attempt_id is None:
# TODO: Add tests to cover both paths
self._disable_native_otlp_exporter()
with self._lightning_span_processor:
with self._agentops_trace_context(None, None, kwargs):
+116 -6
View File
@@ -2,14 +2,13 @@
from __future__ import annotations
import functools
import logging
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, AsyncContextManager, Awaitable, Callable, ContextManager, List, Optional
from opentelemetry.sdk.trace import ReadableSpan
from typing import TYPE_CHECKING, Any, AsyncContextManager, Awaitable, Callable, ContextManager, List, Optional, TypeVar
from agentlightning.store.base import LightningStore
from agentlightning.types import ParallelWorkerBase
from agentlightning.types import Attributes, ParallelWorkerBase, Span, SpanCoreFields, SpanRecordingContext, TraceStatus
if TYPE_CHECKING:
from langchain_core.callbacks.base import BaseCallbackHandler # type: ignore
@@ -17,6 +16,14 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
T = TypeVar("T")
_active_tracer: Optional[Tracer] = None
T_func = Callable[..., Awaitable[Any]]
class Tracer(ParallelWorkerBase):
"""
An abstract base class for tracers.
@@ -98,12 +105,12 @@ class Tracer(ParallelWorkerBase):
"""Internal API for CI backward compatibility."""
raise NotImplementedError()
def get_last_trace(self) -> List[ReadableSpan]:
def get_last_trace(self) -> List[Span]:
"""
Retrieves the raw list of captured spans from the most recent trace.
Returns:
A list of OpenTelemetry `ReadableSpan` objects.
A list of [`Span`][agentlightning.Span] objects collected during the last trace.
"""
raise NotImplementedError()
@@ -124,6 +131,48 @@ class Tracer(ParallelWorkerBase):
with self._trace_context_sync(name=func.__name__):
return func(*args, **kwargs)
def create_span(
self,
name: str,
attributes: Optional[Attributes] = None,
timestamp: Optional[float] = None,
status: Optional[TraceStatus] = None,
) -> SpanCoreFields:
"""Notify the tracer that a span should be created here.
It uses a fire-and-forget approach and doesn't wait for the span to be created.
Args:
name: The name of the span.
attributes: The attributes of the span.
timestamp: The timestamp of the span.
status: The status of the span.
Returns:
The core fields of the span.
"""
raise NotImplementedError()
def operation_context(
self,
name: str,
attributes: Optional[Attributes] = None,
start_time: Optional[float] = None,
end_time: Optional[float] = None,
) -> ContextManager[SpanRecordingContext]:
"""Start to record an operation to a span.
Args:
name: The name of the operation.
attributes: The attributes of the operation.
start_time: The start time of the operation.
end_time: The end time of the operation.
Returns:
A [`SpanRecordingContext`][agentlightning.SpanRecordingContext] for recording the operation on the span.
"""
raise NotImplementedError()
async def trace_run_async(self, func: Callable[..., Awaitable[Any]], *args: Any, **kwargs: Any) -> Any:
"""
A convenience wrapper to trace the execution of a single asynchronous function.
@@ -175,3 +224,64 @@ class Tracer(ParallelWorkerBase):
self.teardown_worker(0)
if has_init:
self.teardown()
def set_active_tracer(tracer: Tracer):
"""Set the active tracer for the current process.
Args:
tracer: The tracer to set as active.
"""
global _active_tracer
if _active_tracer is not None:
raise ValueError("An active tracer is already set. Cannot set a new one.")
_active_tracer = tracer
def clear_active_tracer():
"""Clear the active tracer for the current process."""
global _active_tracer
_active_tracer = None
def get_active_tracer() -> Optional[Tracer]:
"""Get the active tracer for the current process.
Returns:
The active tracer, or None if no tracer is active.
"""
global _active_tracer
return _active_tracer
class _ActiveTracerAsyncCM(AsyncContextManager[T]):
def __init__(self, tracer: Tracer, inner: AsyncContextManager[T]):
self._tracer = tracer
self._inner = inner
async def __aenter__(self) -> T:
set_active_tracer(self._tracer) # will raise if nested
try:
return await self._inner.__aenter__()
except Exception:
clear_active_tracer()
raise
async def __aexit__(self, *args: Any, **kwargs: Any) -> Optional[bool]:
try:
return await self._inner.__aexit__(*args, **kwargs)
finally:
clear_active_tracer()
def with_active_tracer_context(
func: Callable[..., AsyncContextManager[T]],
) -> Callable[..., AsyncContextManager[T]]:
"""Decorate a method returning an AsyncContextManager so tracer is active for the whole `async with`."""
@functools.wraps(func)
def wrapper(self: Tracer, *args: Any, **kwargs: Any) -> AsyncContextManager[T]:
cm = func(self, *args, **kwargs)
return _ActiveTracerAsyncCM(self, cm)
return wrapper
+106
View File
@@ -0,0 +1,106 @@
# Copyright (c) Microsoft. All rights reserved.
from __future__ import annotations
import logging
import time
from contextlib import contextmanager
from typing import (
Iterator,
Optional,
)
from agentlightning.types import (
Attributes,
SpanCoreFields,
SpanRecordingContext,
StatusCode,
TraceStatus,
)
from agentlightning.utils.otel import format_exception_attributes
from .base import Tracer
logger = logging.getLogger(__name__)
class DummySpanRecordingContext(SpanRecordingContext):
"""Context for recording operations on a dummy span, not dependent on any backend tracer."""
def __init__(self, name: str, attributes: Optional[Attributes] = None, start_time: Optional[float] = None) -> None:
self.name = name
self.attributes = attributes or {}
self.start_time = start_time or time.time()
self.end_time = None
self.status = TraceStatus(status_code="OK")
def record_exception(self, exception: BaseException) -> None:
self.record_status("ERROR", str(exception))
self.record_attributes(format_exception_attributes(exception))
def record_attributes(self, attributes: Attributes) -> None:
self.attributes.update(attributes)
def record_status(self, status_code: StatusCode, description: Optional[str] = None) -> None:
self.status = TraceStatus(status_code=status_code, description=description)
def finalize(self, end_time: Optional[float] = None) -> None:
self.end_time = end_time or time.time()
def get_recorded_span(self) -> SpanCoreFields:
if self.end_time is None:
raise ValueError("End time is not set. Call finalize() first.")
return SpanCoreFields(
name=self.name,
attributes=self.attributes,
start_time=self.start_time,
end_time=self.end_time,
status=self.status,
)
class DummyTracer(Tracer):
"""A dummy tracer that does not trace anything, but it is compatible with the emitter API.
It doesn't rely on any backend tracer, and also doesn't use any stores.
"""
def create_span(
self,
name: str,
attributes: Optional[Attributes] = None,
timestamp: Optional[float] = None,
status: Optional[TraceStatus] = None,
) -> SpanCoreFields:
if attributes is None:
attributes = {}
if timestamp is None:
timestamp = time.time()
if status is None:
status = TraceStatus(status_code="OK")
return SpanCoreFields(
name=name,
attributes=attributes,
start_time=timestamp,
end_time=timestamp,
status=status,
)
@contextmanager
def operation_context(
self,
name: str,
attributes: Optional[Attributes] = None,
start_time: Optional[float] = None,
end_time: Optional[float] = None,
) -> Iterator[DummySpanRecordingContext]:
start_time = start_time or time.time()
recording_context = DummySpanRecordingContext(name, attributes, start_time)
try:
yield recording_context
except Exception as exc:
recording_context.record_exception(exc)
recording_context.record_status("ERROR", str(exc))
raise
finally:
recording_context.finalize(end_time)
-396
View File
@@ -1,396 +0,0 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import logging
import multiprocessing
import queue
import uuid
from contextlib import asynccontextmanager, contextmanager
from typing import Any, AsyncGenerator, Awaitable, Callable, Dict, Iterator, List, Optional, Tuple
from urllib.parse import urlparse
from httpdbg.hooks.all import httprecord
from httpdbg.records import HTTPRecords
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.trace import SpanKind, Status, StatusCode
from opentelemetry.trace.span import (
SpanContext,
TraceFlags,
TraceState,
)
from agentlightning.store import LightningStore
from .base import Tracer
logger = logging.getLogger(__name__)
class HttpTracer(Tracer):
"""
A tracer implementation that captures HTTP requests using httpdbg.
This tracer hooks into the Python HTTP libraries and captures all
HTTP requests and responses made during the traced code execution.
The captured requests are converted to OpenTelemetry spans for
compatibility with the rest of the tracing ecosystem.
Caution: The current implementation of HttpTracer is very fragile,
and we do not recommend using it in production.
It is primarily for demonstration and testing purposes.
Deprecated: This tracer is deprecated and will be removed in a future version.
Please use LLMProxy as an alternative.
Attributes:
include_headers: Whether to include HTTP headers in the spans.
Headers may contain sensitive information. Use with caution.
include_body: Whether to include HTTP request and response bodies in the spans.
Bodies may be large and contain sensitive information. Use with caution.
include_agentlightning_requests: Whether to include requests initiated by AgentLightning itself.
subprocess_mode: Whether to run trace_run and trace_run_async in subprocesses for isolation.
subprocess_timeout: Timeout for subprocess execution in seconds.
"""
AGENTLIGHTNING_HEADERS = {"x-agentlightning-client"}
def __init__(
self,
include_headers: bool = False,
include_body: bool = False,
include_agentlightning_requests: bool = False,
subprocess_mode: bool = True,
subprocess_timeout: float = 3600.0,
):
super().__init__()
self._last_records: Optional[HTTPRecords] = None
self.include_headers = include_headers
self.include_body = include_body
self.include_agentlightning_requests = include_agentlightning_requests
self.subprocess_mode = subprocess_mode
self.subprocess_timeout = subprocess_timeout
def init_worker(self, worker_id: int, store: Optional[LightningStore] = None) -> None:
"""
Initialize the tracer in a worker process.
Args:
worker_id: The ID of the worker process.
store: The store to add the spans to.
"""
super().init_worker(worker_id, store)
logger.info(f"[Worker {worker_id}] HttpTracer initialized.")
@asynccontextmanager
async def trace_context(self, name: Optional[str] = None, **kwargs: Any) -> AsyncGenerator[HTTPRecords, None]:
"""
Starts a new HTTP tracing context. This should be used as a context manager.
Args:
name: Optional name for the tracing context.
"""
with self._trace_context_sync(name=name, **kwargs) as records:
yield records
@contextmanager
def _trace_context_sync(self, name: Optional[str] = None, **kwargs: Any) -> Iterator[HTTPRecords]:
"""
Starts a new HTTP tracing context. This should be used as a context manager.
Args:
name: Optional name for the tracing context.
Yields:
The HTTPRecords instance containing traced HTTP activities.
"""
records = HTTPRecords()
with httprecord(records):
self._last_records = records
yield records
def get_last_trace(self) -> List[ReadableSpan]:
"""
Retrieves the raw list of captured spans from the most recent trace.
Returns:
A list of OpenTelemetry `ReadableSpan` objects converted from HTTP records.
"""
if self._last_records is None:
return []
return self._convert_to_spans(self._last_records)
def _convert_to_spans(self, records: HTTPRecords) -> List[ReadableSpan]:
"""
Convert HTTPRecords to OpenTelemetry spans.
Args:
records: The HTTPRecords instance containing HTTP traces.
Returns:
A list of ReadableSpan objects representing the HTTP activities.
"""
spans: List[ReadableSpan] = []
# Create a trace ID that will be shared by all spans in this trace
trace_id = int(uuid.uuid4().hex[:16], 16)
for record in records.requests.values():
# Skip AgentLightning requests if include_agentlightning_requests is False
should_skip = False
if not self.include_agentlightning_requests and record.request and record.request.headers:
for header in record.request.headers:
if header.name.lower() in self.AGENTLIGHTNING_HEADERS and header.value.lower() == "true":
should_skip = True
break
if should_skip:
continue
# Create a span ID for this specific HTTP request
span_id = int(uuid.uuid4().hex[:8], 16)
# Create a span context
span_context = SpanContext(
trace_id=trace_id,
span_id=span_id,
is_remote=False,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
trace_state=TraceState(),
)
# Extract important information from the HTTP record
method = record.method
url = record.url
parsed_url = urlparse(url)
status_code = record.status_code
# Create attributes dictionary
attributes: Dict[str, Any] = {
"http.method": method,
"http.url": url,
"http.target": parsed_url.path,
"http.host": parsed_url.netloc,
}
if status_code is not None and status_code > 0: # type: ignore
attributes["http.status_code"] = status_code
# Calculate duration - from begin time to last update
duration = None
if hasattr(record, "last_update") and record.last_update and record.tbegin:
duration = (record.last_update - record.tbegin).total_seconds()
attributes["http.duration_ms"] = duration * 1000 # Convert to ms
# Optionally include headers
if self.include_headers and record.request and record.request.headers:
for header in record.request.headers:
header_name = header.name.lower()
attributes[f"http.request.header.{header_name}"] = header.value
if self.include_headers and record.response and record.response.headers:
for header in record.response.headers:
header_name = header.name.lower()
attributes[f"http.response.header.{header_name}"] = header.value
# Optionally include body - preserve complete content for analysis
if self.include_body and record.request:
body_content = record.request.content
if body_content:
# Store raw body content for later parsing/analysis
attributes["http.request.body"] = body_content
if self.include_body and record.response:
body_content = record.response.content
if body_content:
# Store raw body content for later parsing/analysis
attributes["http.response.body"] = body_content
# Determine span status
span_status = StatusCode.OK
if status_code and status_code >= 400 or record.exception:
span_status = StatusCode.ERROR
# Create start and end timestamps in nanoseconds
# If we have duration, use it, otherwise default to current time - 1ms
start_time_ns = int(record.tbegin.timestamp() * 1e9)
if duration:
end_time_ns = int((record.tbegin.timestamp() + duration) * 1e9)
else:
end_time_ns = int(record.last_update.timestamp() * 1e9)
span = ReadableSpan(
name=f"HTTP {method} {url}",
context=span_context,
parent=None,
kind=SpanKind.CLIENT,
status=Status(span_status),
start_time=start_time_ns,
end_time=end_time_ns,
attributes=attributes,
events=[],
links=[],
resource=None,
)
spans.append(span)
return spans
def trace_run(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
"""
A convenience wrapper to trace the execution of a single synchronous function.
If subprocess_mode is enabled, the function will be executed in an isolated subprocess
to prevent HTTP hooks from affecting the parent process.
Args:
func: The synchronous function to execute and trace.
*args: Positional arguments to pass to the function.
**kwargs: Keyword arguments to pass to the function.
Returns:
The return value of the function.
"""
if self.subprocess_mode:
return self._trace_run_subprocess(func, args, kwargs)
else:
return super().trace_run(func, *args, **kwargs)
async def trace_run_async(self, func: Callable[..., Awaitable[Any]], *args: Any, **kwargs: Any) -> Any:
"""
A convenience wrapper to trace the execution of a single asynchronous function.
If subprocess_mode is enabled, the function will be executed in an isolated subprocess
to prevent HTTP hooks from affecting the parent process.
Args:
func: The asynchronous function to execute and trace.
*args: Positional arguments to pass to the function.
**kwargs: Keyword arguments to pass to the function.
Returns:
The return value of the function.
"""
if self.subprocess_mode:
loop = asyncio.get_event_loop()
return await loop.run_in_executor(
None, self._trace_run_subprocess, func, args, kwargs, True # True for async
)
else:
return await super().trace_run_async(func, *args, **kwargs)
def _trace_run_subprocess(
self,
func: Callable[..., Any],
args: Optional[Tuple[Any, ...]] = None,
kwargs: Optional[Dict[str, Any]] = None,
is_async: bool = False,
) -> Any:
"""
Execute a function in a subprocess with HTTP tracing.
Args:
func: The function to execute.
args: Positional arguments to pass to the function.
kwargs: Keyword arguments to pass to the function.
is_async: Whether the function is asynchronous.
Returns:
The return value of the function.
"""
if args is None:
args = ()
if kwargs is None:
kwargs = {}
# Create a queue to receive results from the subprocess
result_queue = multiprocessing.Queue() # type: ignore
# Create and start the subprocess
process = multiprocessing.Process(
target=self._subprocess_worker, args=(func, args, kwargs, result_queue, is_async) # type: ignore
)
process.start()
try:
# Wait for the process to complete and get the result
process.join(timeout=self.subprocess_timeout)
result = result_queue.get_nowait() # type: ignore
if result["success"]:
# Store the captured records for get_last_trace()
self._last_records = result["records"]
return result["return_value"] # type: ignore
else:
if "records" in result:
self._last_records = result["records"]
# Re-raise the exception that occurred in the subprocess
raise result["exception"]
except multiprocessing.TimeoutError:
process.terminate()
process.join()
raise TimeoutError(f"Subprocess execution timed out after {self.subprocess_timeout} seconds.")
except queue.Empty:
logger.error("Traced result is empty. This may indicate a timeout or an issue with the subprocess.")
finally:
if process.is_alive():
process.terminate()
process.join()
def _subprocess_worker(
self,
func: Callable[..., Any],
args: Tuple[Any, ...],
kwargs: Dict[str, Any],
result_queue: multiprocessing.Queue, # type: ignore
is_async: bool,
) -> None:
"""
Worker function that runs in the subprocess to execute the traced function.
Args:
func: The function to execute.
args: Positional arguments.
kwargs: Keyword arguments.
result_queue: Queue to send results back to parent process.
is_async: Whether the function is asynchronous.
"""
# Create a new tracer instance in the subprocess (without subprocess mode to avoid recursion)
subprocess_tracer = HttpTracer(
include_headers=self.include_headers,
include_body=self.include_body,
include_agentlightning_requests=self.include_agentlightning_requests,
subprocess_mode=False, # Disable subprocess mode in the worker
)
try:
if is_async:
# Run async function in new event loop
import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return_value = loop.run_until_complete(subprocess_tracer.trace_run_async(func, *args, **kwargs))
finally:
loop.close()
else:
# Run sync function
return_value = subprocess_tracer.trace_run(func, *args, **kwargs)
# Get the captured records
records = subprocess_tracer._last_records
# Send success result back to parent
result_queue.put({"success": True, "return_value": return_value, "records": records}) # type: ignore
except Exception as e:
# Log the exception
logger.exception(f"Error in subprocess worker in http tracer: {e}")
# Get the captured records even when there's an exception
records = subprocess_tracer._last_records
# Send error result back to parent
result_queue.put({"success": False, "exception": e, "records": records}) # type: ignore
+199 -22
View File
@@ -4,29 +4,72 @@ from __future__ import annotations
import asyncio
import logging
import os
import threading
import warnings
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator, Awaitable, List, Optional
from contextlib import asynccontextmanager, contextmanager
from typing import Any, AsyncGenerator, Awaitable, Iterator, List, Optional
import opentelemetry.trace as trace_api
from agentops.sdk.core import BatchSpanProcessor
from opentelemetry.instrumentation.utils import suppress_instrumentation
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import ReadableSpan, SpanProcessor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace import TracerProvider as TracerProviderImpl
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor
from agentlightning.semconv import LightningResourceAttributes
from agentlightning.store.base import LightningStore
from agentlightning.types import Attributes, Span, SpanCoreFields, SpanRecordingContext, StatusCode, TraceStatus
from agentlightning.types.tracer import convert_timestamp
from agentlightning.utils.otel import get_tracer_provider
from agentlightning.utils.otlp import LightningStoreOTLPExporter
from .base import Tracer
from .base import Tracer, with_active_tracer_context
logger = logging.getLogger(__name__)
STORE_WRITE_TIMEOUT_SECONDS = 10.0
def to_otel_status_code(status_code: StatusCode) -> trace_api.StatusCode:
if status_code == "UNSET":
return trace_api.StatusCode.UNSET
elif status_code == "ERROR":
return trace_api.StatusCode.ERROR
else:
return trace_api.StatusCode.OK
class OtelSpanRecordingContext(SpanRecordingContext):
def __init__(self, span: trace_api.Span) -> None:
self._span = span
def record_exception(self, exception: BaseException) -> None:
self._span.record_exception(exception)
self.record_status("ERROR", str(exception))
def record_attributes(self, attributes: Attributes) -> None:
self._span.set_attributes(attributes)
def record_status(self, status_code: StatusCode, description: Optional[str] = None) -> None:
otel_status_code = to_otel_status_code(status_code)
self._span.set_status(otel_status_code, description)
def get_otel_span(self) -> trace_api.Span:
return self._span
def get_recorded_span(self) -> SpanCoreFields:
if isinstance(self._span, ReadableSpan):
return SpanCoreFields(
name=self._span.name,
attributes=dict(self._span.attributes) if self._span.attributes else {},
start_time=convert_timestamp(self._span.start_time),
end_time=convert_timestamp(self._span.end_time),
status=TraceStatus.from_opentelemetry(self._span.status),
)
else:
raise ValueError(f"Span is not a ReadableSpan: {self._span}")
class OtelTracer(Tracer):
"""Tracer that provides a basic OpenTelemetry tracer provider.
@@ -38,7 +81,7 @@ class OtelTracer(Tracer):
def __init__(self):
super().__init__()
# This provider is only initialized when the worker is initialized.
self._tracer_provider: Optional[TracerProvider] = None
self._tracer_provider: Optional[trace_api.TracerProvider] = None
self._lightning_span_processor: Optional[LightningSpanProcessor] = None
self._simple_span_processor: Optional[SimpleSpanProcessor] = None
self._otlp_span_exporter: Optional[LightningStoreOTLPExporter] = None
@@ -63,7 +106,7 @@ class OtelTracer(Tracer):
except RuntimeError:
logger.debug(f"[Worker {worker_id}] Tracer provider is not initialized by OtelTracer. Initializing it now.")
self._tracer_provider = TracerProvider()
self._tracer_provider = TracerProviderImpl()
trace_api.set_tracer_provider(self._tracer_provider)
self._lightning_span_processor = LightningSpanProcessor()
self._tracer_provider.add_span_processor(self._lightning_span_processor)
@@ -78,8 +121,9 @@ class OtelTracer(Tracer):
super().teardown_worker(worker_id)
logger.info(f"[Worker {worker_id}] Tearing down OpenTelemetry tracer does NOT remove the tracer provider.")
@with_active_tracer_context
@asynccontextmanager
async def trace_context(
async def trace_context( # kh: runner.step_impl에서 옴
self,
name: Optional[str] = None,
*,
@@ -117,6 +161,7 @@ class OtelTracer(Tracer):
if store.capabilities.get("otlp_traces", False) is True:
logger.debug(f"Tracing to LightningStore rollout_id={rollout_id}, attempt_id={attempt_id}")
self._enable_native_otlp_exporter(store, rollout_id, attempt_id)
# kh: otlp traces 지원하는 store인 경우, otlp exporter 사용
else:
self._disable_native_otlp_exporter()
ctx = self._lightning_span_processor.with_context(store=store, rollout_id=rollout_id, attempt_id=attempt_id)
@@ -129,12 +174,69 @@ class OtelTracer(Tracer):
else:
raise ValueError("rollout_id and attempt_id must be either all provided or all None")
def get_last_trace(self) -> List[ReadableSpan]:
def create_span(
self,
name: str,
attributes: Optional[Attributes] = None,
timestamp: Optional[float] = None,
status: Optional[TraceStatus] = None,
) -> SpanCoreFields:
# Fire the span to the current active tracer provider.
tracer_provider = self._get_tracer_provider()
tracer = tracer_provider.get_tracer(__name__)
span = tracer.start_span(
name, attributes=attributes, start_time=int(timestamp * 1_000_000_000) if timestamp else None
)
if status is not None:
span.set_status(to_otel_status_code(status.status_code), status.description)
span.end(int(timestamp * 1_000_000_000) if timestamp else None)
# The span should have been auto-created by now.
# Return the core fields of the span.
if isinstance(span, ReadableSpan):
return SpanCoreFields(
name=name,
attributes=dict(span.attributes) if span.attributes else {},
start_time=convert_timestamp(span.start_time),
end_time=convert_timestamp(span.end_time),
status=TraceStatus.from_opentelemetry(span.status),
)
else:
raise ValueError(f"Span is not a ReadableSpan: {span}")
@contextmanager
def operation_context(
self,
name: str,
attributes: Optional[Attributes] = None,
start_time: Optional[float] = None,
end_time: Optional[float] = None,
) -> Iterator[SpanRecordingContext]:
if end_time is not None:
logger.warning("OpenTelemetry doesn't support customizing the end time of a span. End time is ignored.")
# Record the span to the current active tracer provider.
tracer_provider = self._get_tracer_provider()
tracer = tracer_provider.get_tracer(__name__)
# Activate the span as the current span within otel.
with tracer.start_as_current_span(
name, attributes=attributes, start_time=int(start_time * 1_000_000_000) if start_time else None
) as span:
recording_context = OtelSpanRecordingContext(span)
try:
yield recording_context
except Exception as exc:
recording_context.record_exception(exc)
raise
# No need to retrieve the span here. It's already been sent to otel processor.
def get_last_trace(self) -> List[Span]:
"""
Retrieves the raw list of captured spans from the most recent trace.
Returns:
A list of OpenTelemetry `ReadableSpan` objects.
A list of [`Span`][agentlightning.Span] objects captured during the most recent trace.
"""
if not self._lightning_span_processor:
raise RuntimeError("LightningSpanProcessor is not initialized. Call init_worker() first.")
@@ -143,6 +245,8 @@ class OtelTracer(Tracer):
def _get_tracer_provider(self) -> TracerProviderImpl:
if self._tracer_provider is None:
raise RuntimeError("TracerProvider is not initialized. Call init_worker() first.")
if not isinstance(self._tracer_provider, TracerProviderImpl):
raise TypeError(f"TracerProvider is not a opentelemetry.sdk.trace.TracerProvider: {self._tracer_provider}")
return self._tracer_provider
def _enable_native_otlp_exporter(self, store: LightningStore, rollout_id: str, attempt_id: str):
@@ -160,16 +264,33 @@ class OtelTracer(Tracer):
)
instrumented = False
candidates: List[str] = []
# kh: processor 별로 조건 check 하는 것임!!
for processor in active_span_processor._span_processors: # pyright: ignore[reportPrivateUsage]
if isinstance(processor, LightningSpanProcessor):
# We don't need the LightningSpanProcessor any more.
logger.debug("LightningSpanProcessor already present in TracerProvider, disabling it.")
processor.disable_store_submission = True
# kh added
logger.debug(f"disable_store_submission set to True for rollout={rollout_id} attempt={attempt_id}")
elif isinstance(processor, (SimpleSpanProcessor, BatchSpanProcessor)):
# Instead, we rely on the OTLPSpanExporter to send spans to the store.
if isinstance(processor.span_exporter, LightningStoreOTLPExporter):
processor.span_exporter.enable_store_otlp(store.otlp_traces_endpoint(), rollout_id, attempt_id)
logger.debug(f"Set LightningStoreOTLPExporter endpoint to {store.otlp_traces_endpoint()}")
# option 1
# kh 기존: store로 보낸다는 설정;
# processor.span_exporter.enable_store_otlp(store.otlp_traces_endpoint(), rollout_id, attempt_id)
# logger.debug(f"Set LightningStoreOTLPExporter endpoint to {store.otlp_traces_endpoint()}")
# option 2 (hardcoded for collector)
# collector 실험: otel collector로 설정?
# processor.span_exporter.enable_store_otlp("http://localhost:4318/v1/traces", rollout_id, attempt_id)
# logger.info(f"Set LightningStoreOTLPExporter endpoint to http://localhost:4318/v1/traces")
# option 3 (switchable via env var)
endpoint = os.getenv("AGL_OTLP_ENDPOINT") or store.otlp_traces_endpoint()
processor.span_exporter.enable_store_otlp(endpoint, rollout_id, attempt_id)
logger.debug(f"[SET] enabling span export to {endpoint} rollout={rollout_id} attempt={attempt_id}")
instrumented = True
else:
candidates.append(
@@ -186,6 +307,9 @@ class OtelTracer(Tracer):
)
def _disable_native_otlp_exporter(self):
# kh added
# logger.info("disable_store_submission reset to False")
tracer_provider = self._get_tracer_provider()
active_span_processor = tracer_provider._active_span_processor # pyright: ignore[reportPrivateUsage]
tracer_provider._resource = tracer_provider._resource.merge( # pyright: ignore[reportPrivateUsage]
@@ -215,18 +339,20 @@ class LightningSpanProcessor(SpanProcessor):
def __init__(self, disable_store_submission: bool = False):
self._disable_store_submission: bool = disable_store_submission
self._spans: List[ReadableSpan] = []
self._spans: List[Span] = []
# Store related context and states
self._store: Optional[LightningStore] = None
self._rollout_id: Optional[str] = None
self._attempt_id: Optional[str] = None
self._local_sequence_id: int = 0
self._lock = threading.Lock()
# private asyncio loop running in a daemon thread
self._loop_ready = threading.Event()
self._loop: Optional[asyncio.AbstractEventLoop] = None
self._loop_thread: Optional[threading.Thread] = None
self._loop_init_lock = threading.Lock()
def __repr__(self) -> str:
return (
@@ -262,11 +388,19 @@ class LightningSpanProcessor(SpanProcessor):
self._disable_store_submission = value
def _ensure_loop(self) -> None:
if self._loop_thread is None or self._loop is None:
# Fast path: loop already initialized
if self._loop_thread is not None and self._loop is not None:
return
with self._loop_init_lock:
# Double-check after acquiring lock
if self._loop_thread is not None and self._loop is not None:
return
self._loop_ready.clear()
self._loop_thread = threading.Thread(target=self._loop_runner, name="otel-loop", daemon=True)
self._loop_thread.start()
self._loop_ready.wait() # loop is ready
if not self._loop_ready.wait(timeout=30.0):
raise RuntimeError("Timed out waiting for otel-loop thread to start")
def _loop_runner(self):
loop = asyncio.new_event_loop()
@@ -330,13 +464,13 @@ class LightningSpanProcessor(SpanProcessor):
def force_flush(self, timeout_millis: int = 30000) -> bool:
return True
def spans(self) -> List[ReadableSpan]:
def spans(self) -> List[Span]:
"""
Get the list of spans collected by this processor.
This is useful for debugging and testing purposes.
Returns:
List of ReadableSpan objects collected during tracing.
List of [`Span`][agentlightning.Span] objects collected during tracing.
"""
return self._spans
@@ -368,17 +502,60 @@ class LightningSpanProcessor(SpanProcessor):
if not span.context or not span.context.trace_flags.sampled:
return
# kh: _disable_store_submission 조건에 안 맞아서 안 들어갈 가능성 큼
if not self._disable_store_submission and self._store and self._rollout_id and self._attempt_id:
try:
# Submit add_otel_span to the event loop and wait for it to complete
with suppress_instrumentation():
self._ensure_loop()
self._await_in_loop(
# kh added
# logger.info(
# "[SET] Exporting spans to STORE; rollout=%s attempt=%s",
# span.name, self._rollout_id, self._attempt_id
# )
uploaded_span = self._await_in_loop(
# kh: store에 span 저장 시도 (이건 otlp collector로 보내는 게 아님, store에 직접!)
self._store.add_otel_span(self._rollout_id, self._attempt_id, span),
timeout=60.0,
timeout=STORE_WRITE_TIMEOUT_SECONDS,
)
if uploaded_span is not None:
self._spans.append(uploaded_span)
except TimeoutError:
logger.warning(
"Timed out adding span %s to store after %.1f seconds. The span will be stored locally "
"but it's not guaranteed to be persisted.",
span.name,
STORE_WRITE_TIMEOUT_SECONDS,
)
self._spans.append(
Span.from_opentelemetry(
span,
rollout_id=self._rollout_id,
attempt_id=self._attempt_id,
sequence_id=self._local_sequence_id,
)
)
except Exception:
# log; on_end MUST NOT raise
logger.exception(f"Error adding span to store: {span.name}")
logger.exception(f"Error adding span to store: {span.name}. The span will be store locally only.")
self._spans.append(
Span.from_opentelemetry(
span,
rollout_id=self._rollout_id,
attempt_id=self._attempt_id,
sequence_id=self._local_sequence_id,
)
)
self._spans.append(span)
else:
# Fallback path
created_span = Span.from_opentelemetry(
span,
rollout_id=self._rollout_id or "rollout-dummy",
attempt_id=self._attempt_id or "attempt-dummy",
sequence_id=self._local_sequence_id,
)
self._local_sequence_id += 1
self._spans.append(created_span)
+677
View File
@@ -0,0 +1,677 @@
# Copyright (c) Microsoft. All rights reserved.
from __future__ import annotations
import asyncio
import concurrent.futures as futures
import logging
import os
import re
import weakref
from contextlib import asynccontextmanager, contextmanager
from datetime import datetime
from typing import (
Any,
AsyncIterator,
Callable,
Dict,
Iterator,
List,
Optional,
cast,
)
import weave
from opentelemetry.semconv.attributes import exception_attributes
from weave.trace.call import Call
from weave.trace.settings import UserSettings
from weave.trace.weave_client import WeaveClient
from weave.trace_server import trace_server_interface as tsi
from weave.wandb_interface.context import set_wandb_api_context
from agentlightning.instrumentation.weave import InMemoryWeaveTraceServer, instrument_weave, uninstrument_weave
from agentlightning.semconv import LightningResourceAttributes, LightningSpanAttributes
from agentlightning.store.base import LightningStore
from agentlightning.types import (
Attributes,
OtelResource,
Span,
SpanContext,
SpanCoreFields,
SpanRecordingContext,
StatusCode,
TraceStatus,
)
from agentlightning.utils.id import generate_id
from agentlightning.utils.otel import (
filter_and_unflatten_attributes,
flatten_attributes,
format_exception_attributes,
sanitize_attributes,
)
from .base import Tracer, with_active_tracer_context
logger = logging.getLogger(__name__)
def op_name_to_func_name(op_name: str) -> str:
"""Convert a Weave operation name to a function name.
Weave operation names look like this: `weave:///xxx/agentlightning.tracer.weave/op/openai.chat.completions.create:019b10be-...-44d74272569c`
"""
match = re.search(r"/([^/:]+):", op_name)
if match:
return match.group(1)
else:
return op_name
def random_project_name() -> str:
return "agl/weave-" + generate_id(12)
def get_timestamp_or_throw(date: Optional[datetime], field_name: str) -> float:
if date is None:
raise ValueError(f"{field_name} is required but not set")
return date.timestamp()
class WeaveSpanRecordingContext(SpanRecordingContext):
"""Universal interface for recording operations on a Weave call."""
def __init__(self, call: Call) -> None:
self._call = call
def record_exception(self, exception: BaseException) -> None:
self._call.exception = str(exception)
self.record_status("ERROR", str(exception))
self.record_attributes(format_exception_attributes(exception))
def _get_input_from_attributes(self, attributes: Attributes) -> Dict[str, Any]:
if LightningSpanAttributes.OPERATION_INPUT.value in attributes:
# This can be a very rare case. If it happens, we can just let it throw.
return cast(Dict[str, Any], attributes[LightningSpanAttributes.OPERATION_INPUT.value])
else:
filtered_attributes = filter_and_unflatten_attributes(
attributes, LightningSpanAttributes.OPERATION_INPUT.value
)
if isinstance(filtered_attributes, list):
return {str(i): v for i, v in enumerate(filtered_attributes)}
else:
return filtered_attributes
def _get_output_from_attributes(self, attributes: Attributes) -> Any:
if LightningSpanAttributes.OPERATION_OUTPUT.value in attributes:
return attributes[LightningSpanAttributes.OPERATION_OUTPUT.value]
else:
return filter_and_unflatten_attributes(attributes, LightningSpanAttributes.OPERATION_OUTPUT.value)
def record_attributes(self, attributes: Attributes) -> None:
input_attributes = self._get_input_from_attributes(attributes)
if input_attributes:
self._call.inputs.update(input_attributes)
output_attributes = self._get_output_from_attributes(attributes)
if output_attributes:
if self._call.output is not None:
logger.warning(f"Output is already set. It will be overridden: {self._call.output}")
self._call.output = output_attributes
if LightningSpanAttributes.OPERATION_NAME.value in attributes:
logger.error(
f"Cannot record operation name as an attribute. It will be skipped: {attributes[LightningSpanAttributes.OPERATION_NAME.value]}"
)
# The rest of the attributes are recorded as summary.
for key, value in attributes.items():
if (
not key == LightningSpanAttributes.OPERATION_INPUT.value
and not key.startswith(LightningSpanAttributes.OPERATION_INPUT.value + ".")
and not key == LightningSpanAttributes.OPERATION_OUTPUT.value
and not key.startswith(LightningSpanAttributes.OPERATION_OUTPUT.value + ".")
and not key == LightningSpanAttributes.OPERATION_NAME.value
):
if self._call.summary is None:
self._call.summary = {}
self._call.summary[key] = value
def record_status(self, status_code: StatusCode, description: Optional[str] = None) -> None:
if status_code == "ERROR":
if not description:
raise ValueError("Description is required when status code is ERROR")
self._call.exception = description
elif status_code == "OK":
self._call.exception = None
# Do nothing for other status codes.
def finalize(self) -> None:
# Do nothing
pass
def get_recorded_span(self) -> SpanCoreFields:
return SpanCoreFields(
name=self._call.op_name,
attributes=flatten_attributes(self._call.attributes or {}),
start_time=self._call.started_at.timestamp() if self._call.started_at else None,
end_time=self._call.ended_at.timestamp() if self._call.ended_at else None,
status=TraceStatus(
status_code="OK" if self._call.exception is None else "ERROR", description=self._call.exception
),
)
class WeaveTracerManagedTraceServer(InMemoryWeaveTraceServer):
"""A managed trace server for WeaveTracer."""
def __init__(
self,
partial_call_callback: Callable[[Dict[str, Any]], None],
complete_call_callback: Callable[[tsi.CallSchema], None],
):
super().__init__()
self.partial_call_callback = partial_call_callback
self.complete_call_callback = complete_call_callback
self._calls_already_invoked: set[str] = set()
def trigger_callbacks(self, call_id: str) -> None:
with self._call_threading_lock:
if call_id in self.calls:
if call_id not in self._calls_already_invoked:
self._calls_already_invoked.add(call_id)
self.complete_call_callback(self.calls[call_id])
else:
logger.info(f"Call {call_id} has callback already invoked. Skipping.")
elif call_id in self.partial_calls:
self.partial_call_callback(self.partial_calls[call_id])
else:
logger.error(f"Call {call_id} not found in partial_calls or calls")
def call_start(self, req: tsi.CallStartReq) -> tsi.CallStartRes:
try:
ret = super().call_start(req)
self.trigger_callbacks(ret.id)
return ret
except Exception:
logger.exception(f"Error calling call_start: {req}", exc_info=True)
raise
def call_end(self, req: tsi.CallEndReq) -> tsi.CallEndRes:
try:
ret = super().call_end(req)
self.trigger_callbacks(req.end.id)
return ret
except Exception:
logger.exception(f"Error calling call_end: {req}", exc_info=True)
raise
def clear(self) -> None:
self._calls_already_invoked.clear()
class WeaveTracer(Tracer):
"""Tracer implementation using Weave for telemetry and trace logging.
This replaces AgentOpsTracer with a Weave-based manual trace context. It tracks:
- Function/method calls
- Input/Output data
- Exceptions
and logs them to Weave Cloud (W&B backend) or optionally bypasses the network for testing.
"""
def __init__(
self,
*,
project_name: str | None = None,
weave_user_settings: UserSettings | None = None,
instrument_managed: bool = True,
):
"""Initialize a WeaveTracer instance.
Args:
project_name: Optional project name for Weave; defaults to the current module name.
weave_user_settings: Optional UserSettings for Weave.
instrument_managed: Whether to patch the Weave/W&B integration to bypass actual network calls for testing.
"""
super().__init__()
self.project_name = project_name
self.instrument_managed = instrument_managed
self.weave_user_settings = weave_user_settings or UserSettings(use_server_cache=False)
self._store: Optional[LightningStore] = None
self._server = WeaveTracerManagedTraceServer(
partial_call_callback=self.partial_call_callback, complete_call_callback=self.complete_call_callback
)
self._default_sequence_counter: int = 0
self._calls: Dict[str, tsi.CallSchema] = {} # call_id -> call
self._spans: List[Span] = [] # spans in the current trace
self._rollout_id: Optional[str] = None
self._attempt_id: Optional[str] = None
self._partial_call_futures: Dict[str, asyncio.Future[int] | futures.Future[int]] = {}
self._complete_call_futures: List[asyncio.Future[None] | futures.Future[None]] = []
self._loop: weakref.ReferenceType[asyncio.AbstractEventLoop] | None = None
def instrument(self, worker_id: int):
instrument_weave(self._server)
def uninstrument(self, worker_id: int):
uninstrument_weave()
def init_worker(self, worker_id: int, store: Optional[LightningStore] = None):
"""
Initialize the tracer for a worker thread/process.
Args:
worker_id: Identifier of the worker.
store: Optional LightningStore for storing spans.
"""
super().init_worker(worker_id, store)
logger.info(f"[Worker {worker_id}] Setting up Weave tracer...")
self._store = store
# Optionally patch network calls to bypass real Weave/W&B endpoints
if self.instrument_managed:
self.instrument(worker_id)
# If WANDB_API_KEY is not set, we need to initialize Weave with a hack
if not os.getenv("WANDB_API_KEY"):
logger.info("WANDB_API_KEY is not set. Initializing Weave a mock context.")
set_wandb_api_context("agl", api_key=None, headers=None, cookies=None)
else:
logger.debug("WANDB_API_KEY is set. Weave will be initialized automatically.")
weave_client = weave.get_client()
if self.project_name is None:
self.project_name = random_project_name()
if weave_client is not None:
logger.warning("Weave client was already initialized. Reentrant calls are at your own risk.")
if weave_client.project == self.project_name:
logger.error(
f"Weave client was already initialized for the same project '{self.project_name}'. It's very likely that weave won't work correctly."
)
# Init no matter what
try:
weave.init(project_name=self.project_name, settings=self.weave_user_settings)
logger.info(f"[Worker {worker_id}] Weave client initialized.")
except Exception as exc:
raise RuntimeError(f"Failed to initialize Weave for project '{self.project_name}'") from exc
def teardown_worker(self, worker_id: int):
"""
Clean up tracer resources for the worker.
Args:
worker_id: Identifier of the worker.
"""
super().teardown_worker(worker_id)
if self.instrument_managed:
self.uninstrument(worker_id)
logger.info(f"[Worker {worker_id}] Instrumentation removed.")
@with_active_tracer_context
@asynccontextmanager
async def trace_context(
self,
name: Optional[str] = None,
*,
rollout_id: Optional[str] = None,
attempt_id: Optional[str] = None,
**kwargs: Any,
) -> AsyncIterator[Any]:
"""Asynchronous implementation of the tracing context.
Args:
name: Optional operation name.
rollout_id: Optional rollout ID.
attempt_id: Optional attempt ID.
Raises:
ValueError: If store, rollout_id, and attempt_id are inconsistently provided.
RuntimeError: If Weave is not installed or client is uninitialized.
"""
if rollout_id is not None and attempt_id is not None:
self._rollout_id = rollout_id
self._attempt_id = attempt_id
elif rollout_id is None and attempt_id is None:
logger.info("No rollout_id or attempt_id provided. Skipping writing to store.")
self._rollout_id = self._attempt_id = None
else:
raise ValueError("rollout_id and attempt_id must be either both provided or both None")
await self._init_trace_context()
weave_client = self._get_weave_client()
if weave_client.server is not self._server:
logger.error(
"Weave client is not using the correct trace server. You might have multiple WeaveTracer instances running in the same process. "
f"Expected {self._server}, got {weave_client.server}"
)
arg_op = name or weave_client.project
arg_inputs: dict[str, str] = {}
if rollout_id is not None:
arg_inputs[LightningResourceAttributes.ROLLOUT_ID.value] = rollout_id
if attempt_id is not None:
arg_inputs[LightningResourceAttributes.ATTEMPT_ID.value] = attempt_id
try:
# Create a new trace call object in Weave
trace_call = weave_client.create_call( # pyright: ignore[reportUnknownMemberType]
op=arg_op, inputs=arg_inputs
)
try:
yield trace_call
# Finish trace even if no exception
weave_client.finish_call(trace_call) # pyright: ignore[reportUnknownMemberType]
except Exception as exc:
# Finish trace and log any exception
weave_client.finish_call(trace_call, exception=exc) # pyright: ignore[reportUnknownMemberType]
logger.error(f"Trace failed for rollout_id={rollout_id}, attempt_id={attempt_id}, error={exc}")
raise
finally:
try:
weave_client.flush()
# It's possible that the call end futures are from a dedicated Weave thread pool,
await asyncio.gather(*[asyncio.wrap_future(future) for future in self._complete_call_futures])
finally:
# Mandatory cleanup
self._rollout_id = None
self._attempt_id = None
self._server.clear()
def create_span(
self,
name: str,
attributes: Optional[Attributes] = None,
timestamp: Optional[float] = None,
status: Optional[TraceStatus] = None,
) -> SpanCoreFields:
if timestamp is not None:
logger.warning("Weave doesn't support customizing the start time of a call. Timestamp is ignored.")
weave_client = self._get_weave_client()
trace_call = weave_client.create_call( # pyright: ignore[reportUnknownMemberType]
op=name,
attributes=attributes,
inputs={},
)
# Immediately finish the call
weave_client.finish_call(trace_call) # pyright: ignore[reportUnknownMemberType]
# We don't wait for the call to be propagated to the server.
start_time = trace_call.started_at.timestamp() if trace_call.started_at else None
end_time = trace_call.ended_at.timestamp() if trace_call.ended_at else None
trace_status = (
TraceStatus(status_code="OK")
if trace_call.exception is None
else TraceStatus(status_code="ERROR", description=trace_call.exception)
)
return SpanCoreFields(
name=name,
attributes=flatten_attributes(trace_call.attributes or {}),
start_time=start_time,
end_time=end_time,
status=trace_status,
)
@contextmanager
def operation_context(
self,
name: str,
attributes: Optional[Attributes] = None,
start_time: Optional[float] = None,
end_time: Optional[float] = None,
) -> Iterator[SpanRecordingContext]:
if start_time is not None:
logger.warning("Weave doesn't support customizing the start time of a call. Timestamp is ignored.")
if end_time is not None:
logger.warning("Weave doesn't support customizing the end time of a call. Timestamp is ignored.")
weave_client = self._get_weave_client()
trace_call = weave_client.create_call( # pyright: ignore[reportUnknownMemberType]
op=name,
attributes=attributes,
inputs={},
)
recording_context = WeaveSpanRecordingContext(trace_call)
try:
yield recording_context
except Exception as exc:
recording_context.record_exception(exc)
raise
finally:
weave_client.finish_call(trace_call) # pyright: ignore[reportUnknownMemberType]
async def _init_trace_context(self) -> None:
"""Initialize the trace context."""
self._spans.clear()
self._calls.clear()
self._partial_call_futures.clear()
self._complete_call_futures.clear()
self._loop = weakref.ref(asyncio.get_running_loop())
def _get_weave_client(self) -> WeaveClient:
"""Get the Weave client."""
weave_client = weave.get_client()
if not weave_client:
raise RuntimeError("Weave client is not initialized. Call init_worker() first.")
return weave_client
def _ensure_loop(self) -> tuple[asyncio.AbstractEventLoop, bool]:
"""Returns a usable event loop and a boolean indicating whether it's the current running loop.
Prefer using the main loop if it's possible. Otherwise, use the current running loop.
"""
# Get the current running loop
try:
running_loop = asyncio.get_running_loop()
except RuntimeError:
running_loop = None
# Get the main loop, which can be a different loop
if self._loop is not None:
main_loop = self._loop()
else:
main_loop = None
if main_loop is not None:
return main_loop, id(main_loop) == id(running_loop)
elif running_loop is not None:
return running_loop, True
else:
raise RuntimeError("No running event loop found. This should not happen.")
def get_last_trace(self) -> List[Span]:
return self._spans
def partial_call_callback(self, request_content: Dict[str, Any]) -> None:
call_id = request_content.get("id")
if call_id is None:
raise ValueError("Call ID is required even for partial calls")
if call_id in self._partial_call_futures:
raise ValueError(f"Call {call_id} already has a start future")
# The callback must possibly be called from a dedicated Weave thread pool,
# but it should be executed on the main event loop.
try:
loop, is_current_loop = self._ensure_loop()
if is_current_loop:
task = loop.create_task(self.partial_call_handler(request_content))
else:
# Schedule the task on the dedicated loop
task = asyncio.run_coroutine_threadsafe(self.partial_call_handler(request_content), loop)
self._partial_call_futures[call_id] = task
except Exception as exc:
logger.exception(f"Error creating call start task: {exc}", exc_info=True)
def complete_call_callback(self, call: tsi.CallSchema) -> None:
try:
loop, is_current_loop = self._ensure_loop()
if is_current_loop:
task = loop.create_task(self.complete_call_handler(call))
else:
# Schedule the task on the dedicated loop
task = asyncio.run_coroutine_threadsafe(self.complete_call_handler(call), loop)
self._complete_call_futures.append(task)
except Exception as exc:
logger.exception(f"Error creating call finish task: {exc}", exc_info=True)
async def _get_next_sequence_id(self) -> int:
"""Get the next sequence ID for a span.
Use store to get the next sequence ID if available, otherwise use a default counter.
"""
if self._rollout_id and self._attempt_id and self._store:
return await self._store.get_next_span_sequence_id(self._rollout_id, self._attempt_id)
else:
self._default_sequence_counter += 1
return self._default_sequence_counter
async def partial_call_handler(self, request_content: Dict[str, Any]) -> int:
"""Handler called when a Weave Call starts.
Args:
request_content: The partial Weave Call object.
Returns:
The sequence ID for the call.
"""
sequence_id = await self._get_next_sequence_id()
return sequence_id
async def complete_call_handler(self, call: tsi.CallSchema) -> None:
"""Handler called when a Weave Call finishes.
Converts the call (including nested children) into spans and stores them in LightningStore.
"""
# Make sure the corresponding call_start_future is complete
if call.id in self._partial_call_futures:
sequence_id = await asyncio.wrap_future(self._partial_call_futures[call.id])
del self._partial_call_futures[call.id]
else:
# Fetch a new sequence ID as the call_start is somehow missing
if call.id in self._calls:
logger.warning(
f"Call {call.id} is already in calls. The call is already completed. Overwriting the call."
)
else:
logger.warning(f"Call {call.id} has no start future. Fetching a new sequence ID.")
sequence_id = await self._get_next_sequence_id()
self._calls[call.id] = call
span = await self.convert_call_to_span(call, self._rollout_id, self._attempt_id, sequence_id)
self._spans.append(span)
if self._store and self._rollout_id and self._attempt_id:
try:
await self._store.add_span(span)
except Exception as exc:
logger.exception(f"Error adding span to store: {exc}")
async def convert_call_to_span(
self,
call: tsi.CallSchema,
rollout_id: Optional[str] = None,
attempt_id: Optional[str] = None,
sequence_id: Optional[int] = None,
) -> Span:
"""Convert a Weave Call (with nested children) into a Agent-lightning Span.
`rollout_id` and `attempt_id` are required to attach the spans to the store.
Args:
call: The Weave Call object.
rollout_id: Optional rollout ID to attach to spans.
attempt_id: Optional attempt ID to attach to spans.
sequence_id: Optional sequence ID to attach to spans.
Returns:
List of converted spans.
"""
rollout_id = rollout_id or "rollout-dummy"
attempt_id = attempt_id or "attempt-dummy"
sequence_id = sequence_id or 0
start_ts: float = call.started_at.timestamp()
end_ts: Optional[float] = call.ended_at.timestamp() if call.ended_at else None
if call.exception:
status = TraceStatus(status_code="ERROR", description=call.exception)
else:
status = TraceStatus(status_code="OK")
attributes: Dict[str, Any] = {
LightningSpanAttributes.OPERATION_NAME.value: call.op_name,
# op_name can be possibly overridden by the attributes.
**call.attributes,
}
if call.inputs:
attributes[LightningSpanAttributes.OPERATION_INPUT.value] = call.inputs
if call.output:
attributes[LightningSpanAttributes.OPERATION_OUTPUT.value] = call.output
if call.summary:
# attributes can be possibly overridden by the summary.
attributes.update(call.summary)
if call.exception:
attributes[exception_attributes.EXCEPTION_MESSAGE] = call.exception
sanitized_attributes = sanitize_attributes(flatten_attributes(attributes, expand_leaf_lists=False))
context = SpanContext(
trace_id=call.trace_id,
span_id=call.id,
is_remote=False,
trace_state={},
)
# Get context for parent
if call.parent_id:
parent_call = self._calls.get(call.parent_id)
if parent_call:
parent_context = SpanContext(
trace_id=parent_call.trace_id,
span_id=parent_call.id,
is_remote=False,
trace_state={},
)
else:
parent_context = None
else:
parent_context = None
# Build the Span object
return Span(
rollout_id=rollout_id,
attempt_id=attempt_id,
sequence_id=sequence_id,
trace_id=call.trace_id,
span_id=call.id,
parent_id=call.parent_id,
name=op_name_to_func_name(call.op_name),
status=status,
attributes=sanitized_attributes,
events=[], # Weave calls do not generate events
links=[], # Weave calls do not generate links
start_time=start_ts,
end_time=end_ts,
context=context,
parent=parent_context,
resource=OtelResource(
attributes={
LightningResourceAttributes.ROLLOUT_ID.value: rollout_id,
LightningResourceAttributes.ATTEMPT_ID.value: attempt_id,
LightningResourceAttributes.SPAN_SEQUENCE_ID.value: sequence_id,
LightningResourceAttributes.TRACER_NAME.value: "weave",
},
schema_url="",
),
)
+2 -1
View File
@@ -28,7 +28,7 @@ from typing import (
from opentelemetry.sdk.trace import ReadableSpan
from pydantic import BaseModel, Field, model_validator
from .tracer import Span
from .tracer import Span, SpanCoreFields
if TYPE_CHECKING:
from agentlightning.litagent import LitAgent
@@ -307,6 +307,7 @@ RolloutRawResult = Union[
float, # only final reward
List[ReadableSpan], # constructed OTEL spans by user
List[Span], # constructed Span objects by user
List[SpanCoreFields], # constructed SpanCoreFields objects by user
]
"""Rollout result type.
+81 -3
View File
@@ -2,11 +2,13 @@
from __future__ import annotations
import time
"""Data models that mirror OpenTelemetry spans for Agent Lightning."""
import json
from enum import Enum
from typing import Any, Dict, List, Optional, Sequence, Union
from typing import Any, Dict, List, Literal, Optional, Protocol, Sequence, Union
from opentelemetry import trace as trace_api
from opentelemetry.sdk.resources import Resource
@@ -31,6 +33,9 @@ __all__ = [
"SpanNames",
"SpanAttributeNames",
"SpanLike",
"StatusCode",
"SpanCoreFields",
"SpanRecordingContext",
]
@@ -83,6 +88,8 @@ Attributes = Dict[str, AttributeValue]
"""Mapping from attribute names to their values. Same as OpenTelemetry `Attributes` type."""
TraceState = Dict[str, str]
"""Mapping from trace state key to its value. Same as OpenTelemetry `TraceState` type."""
StatusCode = Literal["UNSET", "OK", "ERROR"]
"""The status code of the span."""
class SpanContext(BaseModel):
@@ -115,7 +122,7 @@ class SpanContext(BaseModel):
class TraceStatus(BaseModel):
"""Serializable variant of `opentelemetry.trace.Status`."""
status_code: str
status_code: StatusCode
"""The status code of the span. Same as OpenTelemetry `Status.status_code` type."""
description: Optional[str] = None
"""The description of the span. Same as OpenTelemetry `Status.description` type."""
@@ -203,6 +210,44 @@ class OtelResource(BaseModel):
)
class SpanCoreFields(BaseModel):
"""Core fields of a span. Used by span creators who don't care about the full span model.
If the spans are managed by some OTel tracer provider, it's not advised to create spans via this path.
"""
name: str
"""The name of the span."""
status: TraceStatus
"""The status of the span."""
attributes: Attributes
"""The attributes of the span."""
start_time: Optional[float]
"""The start time of the span."""
end_time: Optional[float]
"""The end time of the span."""
class SpanRecordingContext(Protocol):
"""Context for recording operations on a span. It doesn't have to finalize the span; the caller will do it."""
def record_exception(self, exception: BaseException) -> None:
"""Record an exception on the span."""
raise NotImplementedError()
def record_attributes(self, attributes: Attributes) -> None:
"""Record attributes on the span."""
raise NotImplementedError()
def record_status(self, status_code: StatusCode, description: Optional[str] = None) -> None:
"""Record the status of the span."""
raise NotImplementedError()
def get_recorded_span(self) -> SpanCoreFields:
"""Get the recording of the span."""
raise NotImplementedError()
class Span(BaseModel):
"""Agent Lightning's canonical span model used for persistence and analytics.
@@ -340,6 +385,7 @@ class Span(BaseModel):
start_time: Optional[float] = None,
end_time: Optional[float] = None,
resource: Optional[OtelResource] = None,
status: Optional[TraceStatus] = None,
) -> "Span":
"""Build a synthetic span from raw attributes.
Different from the [`from_opentelemetry`][agentlightning.Span.from_opentelemetry] method,
@@ -357,6 +403,7 @@ class Span(BaseModel):
start_time: Span start timestamp in seconds.
end_time: Span end timestamp in seconds.
resource: Explicit resource information to attach to the span.
status: Optional status of the span.
Returns:
[`Span`][agentlightning.Span] populated with the provided attributes.
@@ -384,7 +431,7 @@ class Span(BaseModel):
name=name or AGL_VIRTUAL,
resource=resource or OtelResource(attributes={}, schema_url=""),
attributes=attributes,
status=TraceStatus(status_code="OK"),
status=status or TraceStatus(status_code="OK"),
events=[],
links=[],
parent=(
@@ -399,6 +446,37 @@ class Span(BaseModel):
),
)
@classmethod
def from_core_fields(
cls,
core: SpanCoreFields,
*,
rollout_id: Optional[str] = None,
attempt_id: Optional[str] = None,
sequence_id: Optional[int] = None,
) -> Span:
"""Build a span from a core span.
Args:
core: Core span to build from.
rollout_id: Optional rollout identifier associated with the span.
attempt_id: Optional attempt identifier associated with the span.
sequence_id: Optional sequence number to preserve ordering.
Returns:
[`Span`][agentlightning.Span] populated with the provided attributes.
"""
return cls.from_attributes(
attributes=core.attributes,
rollout_id=rollout_id,
attempt_id=attempt_id,
sequence_id=sequence_id,
name=core.name,
start_time=core.start_time or time.time(),
end_time=core.end_time,
status=core.status,
)
class SpanNames(str, Enum):
"""Enumerated span names recognised by Agent-lightning. Deprecated in favor of [semconv][agentlightning.semconv]."""
+18
View File
@@ -0,0 +1,18 @@
# Copyright (c) Microsoft. All rights reserved.
import hashlib
import uuid
__all__ = ["generate_id"]
def generate_id(length: int) -> str:
"""Generate a random ID of the given length.
Args:
length: The length of the ID to generate.
Returns:
A random ID of the given length.
"""
return hashlib.sha1(uuid.uuid4().bytes).hexdigest()[:length]
+307 -155
View File
@@ -16,16 +16,18 @@ from __future__ import annotations
import logging
import os
import tempfile
import threading
import time
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple
import aiologic
if TYPE_CHECKING:
from prometheus_client import CollectorRegistry
LabelDict = Dict[str, str]
LabelKey = Tuple[Tuple[str, str], ...] # normalized, sorted (key, value) pairs
# Label metadata
LabelKey = Tuple[Tuple[str, str], ...] # normalized (key, value) pairs in registration order
logger = logging.getLogger(__name__)
@@ -45,7 +47,7 @@ def _validate_labels(
expected_names: Expected label names as a tuple.
Returns:
A tuple of (key, value) pairs sorted by registered label order.
A tuple of (key, value) pairs honoring the registered label order.
Raises:
ValueError: If label keys do not match expected_names.
@@ -67,11 +69,17 @@ def _normalize_label_names(label_names: Optional[Sequence[str]]) -> Tuple[str, .
label_names: Iterable of label names or None.
Returns:
A tuple of label names sorted alphabetically.
A tuple of label names preserving their original order.
"""
if not label_names:
return ()
return tuple(sorted(label_names))
return tuple(label_names)
def _normalize_prometheus_metric_name(metric_name: str) -> str:
"""Normalizes Prometheus metric names by replacing unsupported characters."""
return metric_name.replace(".", "_")
@dataclass(frozen=True)
@@ -80,6 +88,7 @@ class _CounterDef:
name: str
label_names: Tuple[str, ...]
group_level: Optional[int] = None
@dataclass(frozen=True)
@@ -89,6 +98,7 @@ class _HistogramDef:
name: str
label_names: Tuple[str, ...]
buckets: Tuple[float, ...]
group_level: Optional[int] = None
@dataclass
@@ -110,16 +120,25 @@ class _HistogramState:
class MetricsBackend:
"""Abstract base class for metrics backends."""
def has_prometheus(self) -> bool:
"""Check if the backend has prometheus support."""
return False
def register_counter(
self,
name: str,
label_names: Optional[Sequence[str]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a counter metric.
Args:
name: Metric name.
label_names: List of label names. Order is not important.
label_names: List of label names. Order determines the truncation
priority for group-level logging.
group_level: Optional per-metric grouping depth for backends that
support label grouping (Console). Global backend settings take
precedence when provided.
Raises:
ValueError: If the metric is already registered with a different
@@ -132,14 +151,19 @@ class MetricsBackend:
name: str,
label_names: Optional[Sequence[str]] = None,
buckets: Optional[Sequence[float]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a histogram metric.
Args:
name: Metric name.
label_names: List of label names. Order is not important.
label_names: List of label names. Order determines the truncation
priority for group-level logging.
buckets: Bucket boundaries (exclusive upper bounds). If None, the
backend may choose defaults.
group_level: Optional per-metric grouping depth for backends that
support label grouping (Console). Global backend settings take
precedence when provided.
Raises:
ValueError: If the metric is already registered with a different
@@ -147,7 +171,7 @@ class MetricsBackend:
"""
raise NotImplementedError()
def inc_counter(
async def inc_counter(
self,
name: str,
amount: float = 1.0,
@@ -166,7 +190,7 @@ class MetricsBackend:
"""
raise NotImplementedError()
def observe_histogram(
async def observe_histogram(
self,
name: str,
value: float,
@@ -199,22 +223,32 @@ class ConsoleMetricsBackend(MetricsBackend):
Rate is always per second.
Label grouping: When logging, labels are truncated to the first `group_level` label
pairs (according to sorted label key order). For example:
Label grouping: When logging, label dictionaries are truncated to the first
`group_level` label pairs (following the registered label order) and metrics
with identical truncated labels are aggregated together. For example:
labels = {"method": "GET", "path": "/", "status": "200"}
group_level = 2 -> logged labels {"method": "GET", "path": "/"}
```python
labels = {"method": "GET", "path": "/", "status": "200"}
group_level = 2 # aggregated labels {"method": "GET", "path": "/"}
```
If `group_level` is None or < 1, all labels are logged.
If `group_level` is None or < 1, all label combinations for a metric are
merged into a single log entry (equivalent to grouping by zero labels).
Individual counters or histograms can set their own `group_level` during
registration; those values apply only when the backend-level `group_level`
is unset, allowing selective overrides.
Thread-safety: A single lock protects shared state mutation, pruning, and snapshotting.
Percentile computation, formatting, and printing are done after releasing the lock.
Thread-safety: Runtime updates and snapshotting use two aiologic locks: one for mutating
shared state and another that serializes the global logging decision/snapshot capture so
other tasks can continue writing. Metric registration happens during initialization,
so it is intentionally left lock-free; this assumption is documented here to avoid
blocking writes unnecessarily.
"""
def __init__(
self,
window_seconds: Optional[float] = 60.0,
log_interval_seconds: float = 5.0,
log_interval_seconds: float = 10.0,
group_level: Optional[int] = None,
) -> None:
"""Initializes ConsoleMetricsBackend.
@@ -226,8 +260,9 @@ class ConsoleMetricsBackend(MetricsBackend):
When the interval elapses, the next metric event triggers a
snapshot and logging of all metrics.
group_level: Label grouping depth. When logging, only the first
`group_level` labels (sorted by key) are included. If None or
< 1, all labels are included.
`group_level` labels (following registered order) are retained and metric
events sharing those labels are aggregated. If None or < 1,
all label combinations collapse into a single group per metric.
"""
self.window_seconds = window_seconds
self.log_interval_seconds = log_interval_seconds
@@ -243,40 +278,42 @@ class ConsoleMetricsBackend(MetricsBackend):
# Global last log time (for all metrics)
self._last_log_time: Optional[float] = None
self._lock = threading.Lock()
self._write_lock = aiologic.Lock()
self._snapshot_lock = aiologic.Lock()
def register_counter(
self,
name: str,
label_names: Optional[Sequence[str]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a counter metric.
See base class for argument documentation.
"""
label_tuple = _normalize_label_names(label_names)
with self._lock:
existing_counter = self._counters.get(name)
existing_hist = self._histograms.get(name)
existing_counter = self._counters.get(name)
existing_hist = self._histograms.get(name)
if existing_hist is not None:
raise ValueError(f"Metric '{name}' already registered as histogram.")
if existing_hist is not None:
raise ValueError(f"Metric '{name}' already registered as histogram.")
if existing_counter is not None:
if existing_counter.label_names != label_tuple:
raise ValueError(
f"Counter '{name}' already registered with labels "
f"{existing_counter.label_names}, got {label_tuple}."
)
return
if existing_counter is not None:
if existing_counter.label_names != label_tuple:
raise ValueError(
f"Counter '{name}' already registered with labels "
f"{existing_counter.label_names}, got {label_tuple}."
)
return
self._counters[name] = _CounterDef(name=name, label_names=label_tuple)
self._counters[name] = _CounterDef(name=name, label_names=label_tuple, group_level=group_level)
def register_histogram(
self,
name: str,
label_names: Optional[Sequence[str]] = None,
buckets: Optional[Sequence[float]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a histogram metric.
@@ -288,29 +325,29 @@ class ConsoleMetricsBackend(MetricsBackend):
else:
bucket_tuple = tuple(buckets)
with self._lock:
existing_counter = self._counters.get(name)
existing_hist = self._histograms.get(name)
existing_counter = self._counters.get(name)
existing_hist = self._histograms.get(name)
if existing_counter is not None:
raise ValueError(f"Metric '{name}' already registered as counter.")
if existing_counter is not None:
raise ValueError(f"Metric '{name}' already registered as counter.")
if existing_hist is not None:
if existing_hist.label_names != label_tuple or existing_hist.buckets != bucket_tuple:
raise ValueError(
f"Histogram '{name}' already registered with "
f"labels={existing_hist.label_names}, "
f"buckets={existing_hist.buckets}."
)
return
if existing_hist is not None:
if existing_hist.label_names != label_tuple or existing_hist.buckets != bucket_tuple:
raise ValueError(
f"Histogram '{name}' already registered with "
f"labels={existing_hist.label_names}, "
f"buckets={existing_hist.buckets}."
)
return
self._histograms[name] = _HistogramDef(
name=name,
label_names=label_tuple,
buckets=bucket_tuple,
)
self._histograms[name] = _HistogramDef(
name=name,
label_names=label_tuple,
buckets=bucket_tuple,
group_level=group_level,
)
def inc_counter(
async def inc_counter(
self,
name: str,
amount: float = 1.0,
@@ -330,7 +367,7 @@ class ConsoleMetricsBackend(MetricsBackend):
label_key = _validate_labels("counter", name, labels, definition.label_names)
state_key = (name, label_key)
with self._lock:
async with self._write_lock:
state = self._counter_state.get(state_key)
if state is None:
state = _CounterState(timestamps=[], amounts=[])
@@ -340,18 +377,19 @@ class ConsoleMetricsBackend(MetricsBackend):
state.amounts.append(amount)
self._prune_events(state.timestamps, state.amounts, now)
should_log = self._should_log_locked(now)
if should_log:
counter_snaps, hist_snaps = self._snapshot_locked(now)
snapshot_time = now
else:
counter_snaps = hist_snaps = []
snapshot_time = now
counter_snaps: List[Tuple[str, LabelDict, List[float], List[float]]] = []
hist_snaps: List[Tuple[str, LabelDict, List[float], Tuple[float, ...]]] = []
should_log = False
snapshot_time = now
if should_log and (counter_snaps or hist_snaps):
async with self._snapshot_lock:
should_log = self._should_log_locked(now)
if should_log:
async with self._write_lock:
counter_snaps, hist_snaps = self._snapshot_locked(now)
self._log_snapshot(counter_snaps, hist_snaps, snapshot_time)
def observe_histogram(
async def observe_histogram(
self,
name: str,
value: float,
@@ -371,7 +409,7 @@ class ConsoleMetricsBackend(MetricsBackend):
label_key = _validate_labels("histogram", name, labels, definition.label_names)
state_key = (name, label_key)
with self._lock:
async with self._write_lock:
state = self._hist_state.get(state_key)
if state is None:
state = _HistogramState(timestamps=[], values=[])
@@ -381,15 +419,17 @@ class ConsoleMetricsBackend(MetricsBackend):
state.values.append(value)
self._prune_events(state.timestamps, state.values, now)
should_log = self._should_log_locked(now)
if should_log:
counter_snaps, hist_snaps = self._snapshot_locked(now)
snapshot_time = now
else:
counter_snaps = hist_snaps = []
snapshot_time = now
counter_snaps: List[Tuple[str, LabelDict, List[float], List[float]]] = []
hist_snaps: List[Tuple[str, LabelDict, List[float], Tuple[float, ...]]] = []
should_log = False
snapshot_time = now
if should_log and (counter_snaps or hist_snaps):
async with self._snapshot_lock:
should_log = self._should_log_locked(now)
if should_log:
async with self._write_lock:
counter_snaps, hist_snaps = self._snapshot_locked(now)
self._log_snapshot(counter_snaps, hist_snaps, snapshot_time)
def _prune_events(
@@ -490,21 +530,22 @@ class ConsoleMetricsBackend(MetricsBackend):
return counter_snaps, hist_snaps
def _truncate_labels_for_logging(self, labels: LabelDict) -> LabelDict:
def _truncate_labels_for_logging(self, labels: LabelDict, group_level: Optional[int]) -> LabelDict:
"""Returns a label dict truncated to the configured group depth.
Args:
labels: Original label dictionary.
group_level: Effective grouping depth for this metric.
Returns:
A new dictionary containing at most `group_level` label pairs,
chosen by sorted key order. If group_level is None or < 1, returns
a shallow copy of the original labels.
chosen by registered label order. If group_level is None or < 1,
returns an empty dict so that all label combinations collapse together.
"""
if self.group_level is None or self.group_level < 1:
return dict(labels)
items = sorted(labels.items())
return dict(items[: self.group_level])
if group_level is None or group_level < 1:
return {}
items = list(labels.items())
return dict(items[:group_level])
def _log(self, message: str) -> None:
"""Logs a message via the module logger."""
@@ -523,21 +564,101 @@ class ConsoleMetricsBackend(MetricsBackend):
hist_snaps: Histogram snapshot list.
"""
entries: List[str] = []
for name, labels, timestamps, amounts in counter_snaps:
truncated_labels = self._truncate_labels_for_logging(labels)
line = self._log_counter(name, truncated_labels, timestamps, amounts, snapshot_time)
for name, labels, timestamps, amounts in self._group_counter_snapshots(counter_snaps):
line = self._log_counter(name, labels, timestamps, amounts, snapshot_time)
if line:
entries.append(line)
for name, labels, values, buckets in hist_snaps:
truncated_labels = self._truncate_labels_for_logging(labels)
line = self._log_histogram(name, truncated_labels, values, buckets, snapshot_time)
for name, labels, values, buckets in self._group_histogram_snapshots(hist_snaps):
line = self._log_histogram(name, labels, values, buckets, snapshot_time)
if line:
entries.append(line)
if entries:
entries.sort()
self._log(" ".join(entries))
def _effective_group_level(self, metric_name: str, *, is_histogram: bool) -> Optional[int]:
"""Returns the active group level for a metric, honoring per-metric overrides."""
if self.group_level is not None:
return self.group_level
if is_histogram:
definition = self._histograms.get(metric_name)
else:
definition = self._counters.get(metric_name)
if definition is None:
return None
return definition.group_level
def _group_counter_snapshots(
self,
counter_snaps: List[Tuple[str, LabelDict, List[float], List[float]]],
) -> List[Tuple[str, LabelDict, List[float], List[float]]]:
grouped: Dict[Tuple[str, Tuple[Tuple[str, str], ...]], Dict[str, Any]] = {}
for name, labels, timestamps, amounts in counter_snaps:
group_level = self._effective_group_level(name, is_histogram=False)
truncated_labels = self._truncate_labels_for_logging(labels, group_level)
key = (name, tuple(truncated_labels.items()))
entry = grouped.setdefault(
key,
{"name": name, "labels": truncated_labels, "timestamps": [], "amounts": []},
)
entry["timestamps"].extend(timestamps)
entry["amounts"].extend(amounts)
grouped_snaps: List[Tuple[str, LabelDict, List[float], List[float]]] = []
for entry in grouped.values():
timestamps = entry["timestamps"]
amounts = entry["amounts"]
if not timestamps:
continue
combined = sorted(zip(timestamps, amounts), key=lambda item: item[0])
ordered_timestamps = [ts for ts, _ in combined]
ordered_amounts = [amt for _, amt in combined]
grouped_snaps.append(
(
entry["name"],
entry["labels"],
ordered_timestamps,
ordered_amounts,
)
)
return grouped_snaps
def _group_histogram_snapshots(
self,
hist_snaps: List[Tuple[str, LabelDict, List[float], Tuple[float, ...]]],
) -> List[Tuple[str, LabelDict, List[float], Tuple[float, ...]]]:
grouped: Dict[Tuple[str, Tuple[Tuple[str, str], ...]], Dict[str, Any]] = {}
for name, labels, values, buckets in hist_snaps:
group_level = self._effective_group_level(name, is_histogram=True)
truncated_labels = self._truncate_labels_for_logging(labels, group_level)
key = (name, tuple(truncated_labels.items()))
entry = grouped.setdefault(
key,
{"name": name, "labels": truncated_labels, "values": [], "buckets": buckets},
)
if entry["buckets"] != buckets:
raise ValueError(f"Histogram buckets mismatch for metric '{name}'.")
entry["values"].extend(values)
grouped_snaps: List[Tuple[str, LabelDict, List[float], Tuple[float, ...]]] = []
for entry in grouped.values():
values = entry["values"]
if not values:
continue
grouped_snaps.append(
(
entry["name"],
entry["labels"],
list(values),
entry["buckets"],
)
)
return grouped_snaps
def _log_counter(
self,
name: str,
@@ -598,8 +719,8 @@ class ConsoleMetricsBackend(MetricsBackend):
def _format_label_string(labels: LabelDict) -> str:
if not labels:
return "{}"
ordered = ",".join(f"{key}={value}" for key, value in sorted(labels.items()))
return ""
ordered = ",".join(f"{key}={value}" for key, value in labels.items())
return f"{{{ordered}}}"
@@ -622,6 +743,9 @@ class PrometheusMetricsBackend(MetricsBackend):
Thread-safety: Registration is protected by a lock. Metric updates assume metrics
are registered during initialization and then remain stable.
Due to the nature of Prometheus, this backend is only suitable for recording high-volume metrics.
Low-volume metrics might be lost if the event has only appeared once.
"""
def __init__(self) -> None:
@@ -641,46 +765,50 @@ class PrometheusMetricsBackend(MetricsBackend):
self._histograms: Dict[str, _HistogramDef] = {}
self._prom_counters: Dict[str, Any] = {}
self._prom_histograms: Dict[str, Any] = {}
self._prom_metric_names: Dict[str, str] = {}
self._lock = threading.Lock()
def has_prometheus(self) -> bool:
"""Check if the backend has prometheus support."""
return True
def register_counter(
self,
name: str,
label_names: Optional[Sequence[str]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a Prometheus counter metric."""
from prometheus_client import Counter as PromCounter
label_tuple = _normalize_label_names(label_names)
with self._lock:
if name in self._histograms:
raise ValueError(f"Metric '{name}' already registered as histogram.")
if name in self._histograms:
raise ValueError(f"Metric '{name}' already registered as histogram.")
existing = self._counters.get(name)
if existing is not None:
if existing.label_names != label_tuple:
raise ValueError(
f"Counter '{name}' already registered with labels "
f"{existing.label_names}, got {label_tuple}."
)
return
existing = self._counters.get(name)
if existing is not None:
if existing.label_names != label_tuple:
raise ValueError(
f"Counter '{name}' already registered with labels " f"{existing.label_names}, got {label_tuple}."
)
return
self._counters[name] = _CounterDef(name=name, label_names=label_tuple)
prom_name = self._register_prometheus_metric_name(name)
self._counters[name] = _CounterDef(name=name, label_names=label_tuple, group_level=group_level)
prom_counter = PromCounter(
name,
f"Counter {name}",
labelnames=label_tuple,
)
self._prom_counters[name] = prom_counter
prom_counter = PromCounter(
prom_name,
f"Counter {name}",
labelnames=label_tuple,
)
self._prom_counters[name] = prom_counter
def register_histogram(
self,
name: str,
label_names: Optional[Sequence[str]] = None,
buckets: Optional[Sequence[float]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a Prometheus histogram metric."""
from prometheus_client import Histogram as PromHistogram
@@ -688,43 +816,44 @@ class PrometheusMetricsBackend(MetricsBackend):
label_tuple = _normalize_label_names(label_names)
bucket_tuple = tuple(buckets) if buckets is not None else ()
with self._lock:
if name in self._counters:
raise ValueError(f"Metric '{name}' already registered as counter.")
if name in self._counters:
raise ValueError(f"Metric '{name}' already registered as counter.")
existing = self._histograms.get(name)
if existing is not None:
if existing.label_names != label_tuple or existing.buckets != bucket_tuple:
raise ValueError(
f"Histogram '{name}' already registered with "
f"labels={existing.label_names}, "
f"buckets={existing.buckets}."
)
return
existing = self._histograms.get(name)
if existing is not None:
if existing.label_names != label_tuple or existing.buckets != bucket_tuple:
raise ValueError(
f"Histogram '{name}' already registered with "
f"labels={existing.label_names}, "
f"buckets={existing.buckets}."
)
return
self._histograms[name] = _HistogramDef(
name=name,
label_names=label_tuple,
prom_name = self._register_prometheus_metric_name(name)
self._histograms[name] = _HistogramDef(
name=name,
label_names=label_tuple,
buckets=bucket_tuple,
group_level=group_level,
)
if bucket_tuple:
prom_hist = PromHistogram(
prom_name,
f"Histogram {name}",
labelnames=label_tuple,
buckets=bucket_tuple,
)
else:
prom_hist = PromHistogram(
prom_name,
f"Histogram {name}",
labelnames=label_tuple,
)
if bucket_tuple:
prom_hist = PromHistogram(
name,
f"Histogram {name}",
labelnames=label_tuple,
buckets=bucket_tuple,
)
else:
prom_hist = PromHistogram(
name,
f"Histogram {name}",
labelnames=label_tuple,
)
self._prom_histograms[name] = prom_hist
self._prom_histograms[name] = prom_hist
def inc_counter(
async def inc_counter(
self,
name: str,
amount: float = 1.0,
@@ -743,7 +872,7 @@ class PrometheusMetricsBackend(MetricsBackend):
else:
prom_counter.inc(amount)
def observe_histogram(
async def observe_histogram(
self,
name: str,
value: float,
@@ -762,6 +891,19 @@ class PrometheusMetricsBackend(MetricsBackend):
else:
prom_hist.observe(value)
def _register_prometheus_metric_name(self, name: str) -> str:
"""Registers the normalized Prometheus metric name and ensures uniqueness."""
normalized = _normalize_prometheus_metric_name(name)
existing = self._prom_metric_names.get(normalized)
if existing is not None and existing != name:
raise ValueError(
f"Prometheus metric name conflict: '{name}' normalizes to '{normalized}', "
f"which is already used by '{existing}'. Consider renaming one of the metrics."
)
self._prom_metric_names.setdefault(normalized, name)
return normalized
class MultiMetricsBackend(MetricsBackend):
"""Metrics backend that forwards calls to multiple underlying backends."""
@@ -779,20 +921,26 @@ class MultiMetricsBackend(MetricsBackend):
raise ValueError("MultiMetricsBackend requires at least one backend.")
self._backends = list(backends)
def has_prometheus(self) -> bool:
"""Check if the backend has prometheus support."""
return any(backend.has_prometheus() for backend in self._backends)
def register_counter(
self,
name: str,
label_names: Optional[Sequence[str]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a counter metric in all underlying backends."""
for backend in self._backends:
backend.register_counter(name, label_names=label_names)
backend.register_counter(name, label_names=label_names, group_level=group_level)
def register_histogram(
self,
name: str,
label_names: Optional[Sequence[str]] = None,
buckets: Optional[Sequence[float]] = None,
group_level: Optional[int] = None,
) -> None:
"""Registers a histogram metric in all underlying backends."""
for backend in self._backends:
@@ -800,9 +948,10 @@ class MultiMetricsBackend(MetricsBackend):
name,
label_names=label_names,
buckets=buckets,
group_level=group_level,
)
def inc_counter(
async def inc_counter(
self,
name: str,
amount: float = 1.0,
@@ -810,9 +959,9 @@ class MultiMetricsBackend(MetricsBackend):
) -> None:
"""Increments a counter metric in all underlying backends."""
for backend in self._backends:
backend.inc_counter(name, amount=amount, labels=labels)
await backend.inc_counter(name, amount=amount, labels=labels)
def observe_histogram(
async def observe_histogram(
self,
name: str,
value: float,
@@ -820,9 +969,10 @@ class MultiMetricsBackend(MetricsBackend):
) -> None:
"""Records a histogram observation in all underlying backends."""
for backend in self._backends:
backend.observe_histogram(name, value=value, labels=labels)
await backend.observe_histogram(name, value=value, labels=labels)
# This variable should be carried into forked processes
_prometheus_multiproc_dir: tempfile.TemporaryDirectory[str] | None = None
@@ -840,7 +990,7 @@ def setup_multiprocess_prometheus():
logger.debug("Created PROMETHEUS_MULTIPROC_DIR at %s", _prometheus_multiproc_dir.name)
else:
logger.warning(
"Found PROMETHEUS_MULTIPROC_DIR was set by user. " "This directory must be wiped between multiple runs."
"Found PROMETHEUS_MULTIPROC_DIR was set by user. This directory must be wiped between multiple runs."
)
@@ -849,7 +999,7 @@ def get_prometheus_registry() -> CollectorRegistry:
from prometheus_client import REGISTRY, CollectorRegistry, multiprocess
if os.getenv("PROMETHEUS_MULTIPROC_DIR") is not None:
logger.debug("Using multiprocess registry for prometheus metrics")
logger.info("Using multiprocess registry for prometheus metrics: %s", os.getenv("PROMETHEUS_MULTIPROC_DIR"))
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
return registry
@@ -857,17 +1007,19 @@ def get_prometheus_registry() -> CollectorRegistry:
return REGISTRY
def shutdown_metrics():
def shutdown_metrics(server: Any = None, worker: Any = None, *args: Any, **kwargs: Any) -> None:
"""Shutdown prometheus metrics."""
from prometheus_client import multiprocess
if _prometheus_multiproc_dir is not None:
from prometheus_client import multiprocess
path = _prometheus_multiproc_dir
if path is None:
return
try:
pid = os.getpid()
multiprocess.mark_process_dead(pid, path.name) # type: ignore
logger.debug("Marked Prometheus metrics for process %d as dead", pid)
except Exception as e:
logger.error("Error during metrics cleanup: %s", str(e))
path = _prometheus_multiproc_dir
try:
if hasattr(worker, "pid"):
pid = worker.pid
else:
pid = os.getpid()
multiprocess.mark_process_dead(pid, path.name) # type: ignore
logger.debug("Marked Prometheus metrics for process %d as dead", pid)
except Exception as e:
logger.error("Error during metrics cleanup: %s", str(e))
+152 -12
View File
@@ -2,22 +2,25 @@
"""Utilities shared for OpenTelemetry span (attributes) support."""
import json
import logging
from typing import Any, Dict, List, Sequence, Union, cast
import traceback
from typing import Any, Dict, List, Sequence, Type, TypeVar, Union, cast
from warnings import filterwarnings
import opentelemetry.trace as trace_api
from agentops.sdk.exporters import OTLPSpanExporter
from opentelemetry.sdk.trace import ReadableSpan, SpanLimits, SynchronousMultiSpanProcessor, Tracer
from opentelemetry.sdk.trace import ReadableSpan, SpanLimits, SpanProcessor, SynchronousMultiSpanProcessor, Tracer
from opentelemetry.sdk.trace import TracerProvider as TracerProviderImpl
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo, InstrumentationScope
from opentelemetry.semconv.attributes import exception_attributes
from opentelemetry.trace import get_tracer_provider as otel_get_tracer_provider
from pydantic import TypeAdapter
from agentlightning.env_var import LightningEnvVar, resolve_bool_env_var
from agentlightning.semconv import LightningSpanAttributes, LinkAttributes, LinkPydanticModel
from agentlightning.types import SpanLike
from agentlightning.types import Attributes, AttributeValue, SpanLike
from agentlightning.utils.otlp import LightningStoreOTLPExporter
logger = logging.getLogger(__name__)
@@ -35,8 +38,16 @@ __all__ = [
"filter_and_unflatten_attributes",
"flatten_attributes",
"unflatten_attributes",
"sanitize_attribute_value",
"sanitize_attributes",
"sanitize_list_attribute_sanity",
"check_attributes_sanity",
"format_exception_attributes",
]
T_SpanLike = TypeVar("T_SpanLike", bound=SpanLike)
T_SpanProcessor = TypeVar("T_SpanProcessor", bound=SpanProcessor)
def full_qualified_name(obj: type) -> str:
if str(obj.__module__) == "builtins":
@@ -112,6 +123,25 @@ def get_tracer_provider(inspect: bool = True) -> TracerProviderImpl:
return tracer_provider
def get_span_processors(
tracer_provider: TracerProviderImpl, expected_type: Type[T_SpanProcessor]
) -> List[T_SpanProcessor]:
"""Get the span processors from the tracer provider.
Args:
tracer_provider: The tracer provider to get the span processors from.
expected_type: The type of the span processors to get.
Returns:
A list of span processors of the expected type.
"""
processors: List[T_SpanProcessor] = []
for processor in tracer_provider._active_span_processor._span_processors: # pyright: ignore[reportPrivateUsage]
if isinstance(processor, expected_type):
processors.append(processor)
return processors
def get_tracer(use_active_span_processor: bool = True) -> trace_api.Tracer:
"""Resolve the OpenTelemetry tracer configured for Agent Lightning.
@@ -166,7 +196,7 @@ def make_tag_attributes(tags: List[str]) -> Dict[str, Any]:
["gen_ai.model:gpt-4", "reward.extrinsic"]
```
"""
return flatten_attributes({LightningSpanAttributes.TAG.value: tags})
return flatten_attributes({LightningSpanAttributes.TAG.value: tags}, expand_leaf_lists=True)
def extract_tags_from_attributes(attributes: Dict[str, Any]) -> List[str]:
@@ -196,10 +226,10 @@ def make_link_attributes(links: Dict[str, str]) -> Dict[str, Any]:
if not isinstance(value, str): # pyright: ignore[reportUnnecessaryIsInstance]
raise ValueError(f"Link value must be a string, got {type(value)} for key '{key}'")
link_list.append({LinkAttributes.KEY_MATCH.value: key, LinkAttributes.VALUE_MATCH.value: value})
return flatten_attributes({LightningSpanAttributes.LINK.value: link_list})
return flatten_attributes({LightningSpanAttributes.LINK.value: link_list}, expand_leaf_lists=True)
def query_linked_spans(spans: Sequence[SpanLike], links: List[LinkPydanticModel]) -> List[SpanLike]:
def query_linked_spans(spans: Sequence[T_SpanLike], links: List[LinkPydanticModel]) -> List[T_SpanLike]:
"""Query spans that are linked by the given link attributes.
Args:
@@ -209,7 +239,7 @@ def query_linked_spans(spans: Sequence[SpanLike], links: List[LinkPydanticModel]
Returns:
A list of spans that match the given link attributes.
"""
matched_spans: List[SpanLike] = []
matched_spans: List[T_SpanLike] = []
for span in spans:
span_attributes = span.attributes or {}
@@ -294,7 +324,9 @@ def filter_and_unflatten_attributes(attributes: Dict[str, Any], prefix: str) ->
return unflatten_attributes(stripped_attributes)
def flatten_attributes(nested_data: Union[Dict[str, Any], List[Any]]) -> Dict[str, Any]:
def flatten_attributes(
nested_data: Union[Dict[str, Any], List[Any]], *, expand_leaf_lists: bool = False
) -> Dict[str, Any]:
"""Flatten a nested dictionary or list into a flat dictionary with dotted keys.
This function recursively traverses dictionaries and lists, producing a flat
@@ -303,12 +335,14 @@ def flatten_attributes(nested_data: Union[Dict[str, Any], List[Any]]) -> Dict[st
Example:
>>> flatten_attributes({"a": {"b": 1, "c": [2, 3]}})
>>> flatten_attributes({"a": {"b": 1, "c": [2, 3]}}, expand_leaf_lists=True)
{"a.b": 1, "a.c.0": 2, "a.c.1": 3}
Args:
nested_data: A nested structure composed of dictionaries, lists, or
primitive values.
nested_data: A nested structure composed of dictionaries, lists, or primitive values.
expand_leaf_lists: Whether to expand lists composed only of primitive values.
When `False` (the default), lists of str/int/float/bool are treated as
leaf values and stored without enumerating their indices.
Returns:
A flat dictionary mapping dotted-string paths to primitive values.
@@ -316,6 +350,15 @@ def flatten_attributes(nested_data: Union[Dict[str, Any], List[Any]]) -> Dict[st
flat: Dict[str, Any] = {}
def _primitive_type(value: Any) -> Union[type[str], type[int], type[float], type[bool]]:
if isinstance(value, bool):
return bool
if isinstance(value, int):
return int
if isinstance(value, float):
return float
return str
def _walk(value: Any, prefix: str = "") -> None:
if isinstance(value, dict):
for k, v in cast(Dict[Any, Any], value).items():
@@ -326,7 +369,22 @@ def flatten_attributes(nested_data: Union[Dict[str, Any], List[Any]]) -> Dict[st
new_prefix = f"{prefix}.{k}" if prefix else k
_walk(v, new_prefix)
elif isinstance(value, list):
for idx, item in enumerate(cast(List[Any], value)):
maybe_list = cast(List[Any], value)
is_leaf_candidate = bool(maybe_list) and all(
isinstance(item, (str, int, float, bool)) for item in maybe_list
)
if not expand_leaf_lists and is_leaf_candidate and prefix:
primitive_types = {_primitive_type(item) for item in maybe_list}
if len(primitive_types) == 1:
flat[prefix] = maybe_list
return
logger.warning(
"List attribute '%s' contains mixed primitive types %s; expanding indexed keys instead.",
prefix,
primitive_types,
)
for idx, item in enumerate(maybe_list):
new_prefix = f"{prefix}.{idx}" if prefix else str(idx)
_walk(item, new_prefix)
else:
@@ -399,3 +457,85 @@ def unflatten_attributes(flat_data: Dict[str, Any]) -> Union[Dict[str, Any], Lis
return node
return convert(root)
def sanitize_attribute_value(object: Any, force: bool = True) -> AttributeValue:
"""Sanitize an attribute value to be a valid OpenTelemetry attribute value."""
if isinstance(object, (str, int, float, bool)):
return object
if isinstance(object, list):
try:
return sanitize_list_attribute_sanity(cast(List[Any], object))
except ValueError as exc:
logger.warning(f"Failed to sanitize list attribute. Fallback to JSON serialization: {exc}")
try:
# This include null, dict, etc.
serialized = json.dumps(object, default=str if force else None)
except (TypeError, ValueError) as exc:
raise ValueError(f"Object must be JSON serializable, got: {type(cast(Any, object))}.") from exc
return serialized
def sanitize_attributes(attributes: Dict[str, Any], force: bool = True) -> Attributes:
"""Sanitize a dictionary of attributes to be a valid OpenTelemetry attributes.
Args:
attributes: A dictionary of attributes to sanitize.
force: Whether to force sanitization even when the value is not JSON serializable.
"""
result: Attributes = {}
for k, v in attributes.items():
try:
result[k] = sanitize_attribute_value(v, force=force)
except ValueError as exc:
raise ValueError(f"Failed to sanitize attribute '{k}': {exc}") from exc
return result
def sanitize_list_attribute_sanity(maybe_list: List[Any]) -> AttributeValue:
"""Try to sanitize a list of attributes to be a valid OpenTelemetry attribute value.
Raise error if the list contains multiple types of primitive values.
"""
if all(isinstance(item, str) for item in maybe_list):
return list[str](maybe_list)
if all(isinstance(item, bool) for item in maybe_list):
return list[bool](maybe_list)
if all(isinstance(item, (int, bool)) for item in maybe_list):
return [int(item) for item in maybe_list]
if all(isinstance(item, (float, int, bool)) for item in maybe_list):
return [float(item) for item in maybe_list]
list_types: List[Any] = [type(item) for item in maybe_list]
raise ValueError(f"List must contain only one type of primitive values, got: {set(list_types)}.")
def check_attributes_sanity(attributes: Dict[Any, Any]) -> None:
"""Check if a dictionary of attributes is a valid OpenTelemetry attributes."""
for k, v in attributes.items():
if not isinstance(k, str):
raise ValueError(f"Attribute key must be a string, got {type(k)} for key '{k}'")
if isinstance(v, list):
try:
sanitize_list_attribute_sanity(cast(List[Any], v))
except ValueError as exc:
raise ValueError(f"Failed to sanitize list attribute '{k}': {exc}") from exc
elif not isinstance(v, (str, int, float, bool)):
raise ValueError(
f"Attribute value must be a string, int, float, bool, or list of these, got {type(v)} for value '{v}'"
)
def format_exception_attributes(exception: BaseException) -> Attributes:
"""Format an exception into a dictionary of attributes."""
stacktrace = "".join(traceback.format_exception(type(exception), exception, exception.__traceback__))
span_attributes: Attributes = {
exception_attributes.EXCEPTION_TYPE: type(exception).__name__,
exception_attributes.EXCEPTION_MESSAGE: str(exception),
exception_attributes.EXCEPTION_ESCAPED: True,
}
if stacktrace.strip():
span_attributes[exception_attributes.EXCEPTION_STACKTRACE] = stacktrace
return span_attributes
+3 -2
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
import gzip
import logging
from typing import Any, Awaitable, Callable, Dict, List, Optional, Sequence, Tuple, Type, TypeVar
from typing import Any, Awaitable, Callable, Dict, List, Mapping, Optional, Sequence, Tuple, Type, TypeVar
from fastapi import Request, Response
from google.protobuf import json_format
@@ -39,6 +39,7 @@ from agentlightning.types.tracer import (
OtelResource,
Span,
SpanContext,
StatusCode,
TraceStatus,
convert_timestamp,
)
@@ -413,7 +414,7 @@ def _kv_list_to_dict(kvs: Sequence[KeyValue]) -> Attributes:
return {kv.key: _any_value_to_python(kv.value) for kv in kvs}
_STATUS_CODE_MAP = {
_STATUS_CODE_MAP: Mapping[ProtoStatus.StatusCode.ValueType, StatusCode] = {
ProtoStatus.STATUS_CODE_UNSET: "UNSET",
ProtoStatus.STATUS_CODE_OK: "OK",
ProtoStatus.STATUS_CODE_ERROR: "ERROR",
+2 -2
View File
@@ -940,9 +940,9 @@ class PythonServerLauncher:
), # Allow half the timeout for graceful shutdown
}
if "PROMETHEUS_MULTIPROC_DIR" in os.environ:
from prometheus_client import multiprocess
from agentlightning.utils.metrics import shutdown_metrics
options["child_exit"] = lambda server, worker: multiprocess.mark_process_dead(worker.pid) # type: ignore
options["child_exit"] = shutdown_metrics # type: ignore
self._gunicorn_app = GunicornApp(self.app, options)
+23 -14
View File
@@ -13,12 +13,20 @@ from gpustat import GPUStat, GPUStatCollection
def system_snapshot(include_gpu: bool = False) -> Dict[str, Any]:
"""Capture a snapshot of the system's hardware and software information.
Args:
include_gpu: Whether to include GPU information.
Returns:
A dictionary containing the system's hardware and software information.
"""
# CPU
cpu = {
"cpu_name": platform.processor(),
"cpu_cores": psutil.cpu_count(logical=False),
"cpu_threads": psutil.cpu_count(logical=True),
"cpu_usage_pct": psutil.cpu_percent(0.05),
"cpu_usage_pct": psutil.cpu_percent(0.0),
}
# Memory
@@ -37,20 +45,21 @@ def system_snapshot(include_gpu: bool = False) -> Dict[str, Any]:
"disk_pct": du.percent,
}
# GPU
# GPU (only query if explicitly requested)
gpus: List[Dict[str, Any]] = []
with suppress(Exception):
for g in GPUStatCollection.new_query().gpus: # type: ignore
g = cast(GPUStat, g)
gpus.append(
{
"gpu": g.name, # type: ignore
"util_pct": g.utilization,
"mem_used_mb": g.memory_used,
"mem_total_mb": g.memory_total,
"temp_c": g.temperature,
}
)
if include_gpu:
with suppress(Exception):
for g in GPUStatCollection.new_query().gpus: # type: ignore
g = cast(GPUStat, g)
gpus.append(
{
"gpu": g.name, # type: ignore
"util_pct": g.utilization,
"mem_used_mb": g.memory_used,
"mem_total_mb": g.memory_total,
"temp_c": g.temperature,
}
)
# Network
net = psutil.net_io_counters()
+6
View File
@@ -8,6 +8,12 @@ defaults:
agentlightning:
port: 9999
trace_aggregator:
level: transition # transition or trajectory, docs refer to https://agent-lightning.github.io/posts/trajectory_level_aggregation/
trajectory_max_prompt_length: 2048 # supported in trajectory level aggregation, suggest to set as maximum length for the prompt in first turn
trajectory_max_response_length: 8192 # supported in trajectory level aggregation, suggest to set as maximum length for the cumulative agent responses in the full trajectory, i.e., n_turns * (max_response_length + max_prompt_length)
debug: False # supported in trajectory level aggregation, enable to diagnose trace merging failures
mismatch_log_dir: ./mismatch_cases # supported in trajectory level aggregation with debug=True, directory to store logs of mismatch cases
data:
filter_overlong_prompts: false
+375 -36
View File
@@ -2,6 +2,7 @@
import asyncio
import json
import os
import random
import socket
import threading
@@ -31,6 +32,85 @@ __all__ = [
]
def ids_startswith(
full_ids: List[int], prefix_ids: List[int], tokenizer: Any, debug: bool = False
) -> Tuple[bool, Tuple[bool, bool, bool]]:
is_prefix: bool
template_mismatch, retoken_mismatch, others_mismatch = False, False, False
if full_ids[: len(prefix_ids)] == prefix_ids:
is_prefix = True
return True, (template_mismatch, retoken_mismatch, others_mismatch)
else:
is_prefix = False
if not debug:
return is_prefix, (template_mismatch, retoken_mismatch, others_mismatch)
def _special_token_sequence(ids: List[int]) -> List[int]:
return [id for id in ids if id in tokenizer.all_special_ids]
def _none_special_token_sequence(ids: List[int]) -> List[int]:
return [id for id in ids if id not in tokenizer.all_special_ids]
# First, handle special tokens
full_special_ids = _special_token_sequence(full_ids)
prefix_special_ids = _special_token_sequence(prefix_ids)
if sum(1 for a, b in zip(full_special_ids, prefix_special_ids) if a != b) > 0:
template_mismatch = True
# Next, handle string content
full_content_ids = _none_special_token_sequence(full_ids)
prefix_content_ids = _none_special_token_sequence(prefix_ids)
full_string = tokenizer.decode(full_ids, skip_special_tokens=True)
prefix_string = tokenizer.decode(prefix_ids, skip_special_tokens=True)
if full_content_ids[: len(prefix_content_ids)] != prefix_content_ids and full_string.startswith(prefix_string):
retoken_mismatch = True
elif full_content_ids[: len(prefix_content_ids)] != prefix_content_ids and not full_string.startswith(
prefix_string
):
others_mismatch = True
return is_prefix, (template_mismatch, retoken_mismatch, others_mismatch)
def log_mismatch_detail(
diagnostic: Tuple[bool, bool, bool],
full_ids: List[int],
prefix_ids: List[int],
global_steps: int,
rollout_id: str,
turn_id: int,
log_dir: str | None = None,
):
if log_dir is None:
return
os.makedirs(log_dir, exist_ok=True)
template_mismatch, retoken_mismatch, others_mismatch = diagnostic
if template_mismatch:
with open(os.path.join(log_dir, "template_mismatch.log"), "a+") as f:
print(
"-" * 10 + f" Global Steps: {global_steps}, Rollout ID: {rollout_id}, Turn ID: {turn_id} " + "-" * 10,
file=f,
)
print(full_ids, file=f)
print(prefix_ids, file=f)
if retoken_mismatch:
with open(os.path.join(log_dir, "retoken_mismatch.log"), "a+") as f:
print(
"-" * 10 + f" Global Steps: {global_steps}, Rollout ID: {rollout_id}, Turn ID: {turn_id} " + "-" * 10,
file=f,
)
print(full_ids, file=f)
print(prefix_ids, file=f)
if others_mismatch:
with open(os.path.join(log_dir, "others_mismatch.log"), "a+") as f:
print(
"-" * 10 + f" Global Steps: {global_steps}, Rollout ID: {rollout_id}, Turn ID: {turn_id} " + "-" * 10,
file=f,
)
print(full_ids, file=f)
print(prefix_ids, file=f)
def get_left_padded_ids_and_attention_mask(
ids: List[int], max_length: int, pad_token_id: int
) -> Tuple[List[int], List[int]]:
@@ -144,6 +224,9 @@ class AgentModeDaemon:
llm_proxy: LLMProxy | None = None,
store: LightningStore | None = None,
adapter: TraceToTripletBase | None = None,
processor: Any = None,
image_base_dir: Optional[str] = None,
trace_aggregator: Dict[str, Any] = {"level": "transition"},
):
self.mode = mode
self.llm_timeout_seconds = llm_timeout_seconds
@@ -183,7 +266,13 @@ class AgentModeDaemon:
self.mini_batch_size = mini_batch_size
self.pad_token_id = pad_token_id
self.tokenizer = tokenizer
self.processor = processor
self.reward_fillna_value = reward_fillna_value
self.image_base_dir = image_base_dir
self.trace_aggregator = trace_aggregator
# Check if model requires multimodal position_ids (e.g., Qwen2-VL)
self._use_mrope = self._is_mrope_model()
# Internal State
self.backend_llm_server_addresses: List[str] = []
@@ -202,6 +291,75 @@ class AgentModeDaemon:
loop.run_forever()
loop.close()
# Multimodal utilities for M-RoPE position embeddings
def _is_mrope_model(self) -> bool:
"""Check if processor requires M-RoPE position embeddings."""
if self.processor is None or not hasattr(self.processor, "image_processor"):
return False
name = self.processor.image_processor.__class__.__name__
return "Qwen2VLImageProcessor" in name or "Qwen3VLImageProcessor" in name
def _resolve_image_path(self, path: str) -> str:
"""Resolve relative image path with base directory."""
import os
if os.path.isabs(path):
return path
if self.image_base_dir is None:
raise ValueError(f"Relative path '{path}' requires 'image_base_dir' to be set.")
return os.path.join(self.image_base_dir, path)
def _get_image_grid_thw(self, image_urls: List[str]) -> Optional[torch.Tensor]:
"""Compute image_grid_thw from image URLs for M-RoPE computation.
Args:
image_urls: List of image URLs extracted from triplet prompt payload.
URLs can be http(s):// URLs or file:// URIs, or data: URIs.
"""
from PIL import Image
from verl.utils.dataset.vision_utils import process_image # pyright: ignore[reportUnknownVariableType]
if self.processor is None or not image_urls:
return None
def to_image_uri(url: str) -> str:
# Already a proper URI (http, https, file, data)
if url.startswith(("http://", "https://", "file://", "data:")):
return url
# Treat as a file path that needs resolution
resolved = self._resolve_image_path(url)
return f"file://{resolved}"
images: List[Image.Image] = [process_image({"image": to_image_uri(url)}) for url in image_urls]
model_inputs = self.processor(text=["dummy"], images=images, return_tensors="pt")
return model_inputs.get("image_grid_thw")
def _compute_mrope_position_ids(
self,
input_ids: torch.Tensor,
attention_mask: torch.Tensor,
image_grid_thw: Optional[torch.Tensor] = None,
) -> torch.Tensor:
"""Compute 4D position_ids for M-RoPE models."""
from typing import Callable
get_rope_index: Callable[..., torch.Tensor]
if "Qwen3VL" in self.processor.__class__.__name__:
from verl.models.transformers.qwen3_vl import get_rope_index # pyright: ignore[reportUnknownVariableType]
else:
from verl.models.transformers.qwen2_vl import get_rope_index # pyright: ignore[reportUnknownVariableType]
vision_pos = get_rope_index(
self.processor, input_ids=input_ids, image_grid_thw=image_grid_thw, attention_mask=attention_mask
)
valid_mask = attention_mask.bool()
text_pos = torch.zeros((1, len(input_ids)), dtype=torch.long, device=input_ids.device)
text_pos[0, valid_mask] = torch.arange(valid_mask.sum().item(), device=input_ids.device)
return torch.cat([text_pos, vision_pos], dim=0)
def _start_proxy_server_v0(self):
"""
Initializes and runs a Flask-based proxy server in a separate thread.
@@ -444,7 +602,7 @@ class AgentModeDaemon:
raise RuntimeError("Internal loop is not running.")
future = asyncio.run_coroutine_threadsafe(coro, self._internal_loop)
try:
future.result(timeout=60) # Wait for completion with a timeout
future.result(timeout=300) # Wait for completion with a timeout
except Exception as e:
print(f"Failed to set up data on server: {e}")
raise
@@ -646,7 +804,9 @@ class AgentModeDaemon:
)
return metric_dict
def get_train_data_batch(self, max_prompt_length: int, max_response_length: int, device: torch.device):
def get_train_data_batch(
self, max_prompt_length: int, max_response_length: int, device: torch.device, global_steps: int
):
"""
Processes completed rollouts to generate a training data batch.
@@ -672,10 +832,14 @@ class AgentModeDaemon:
continue
# The client should report triplets that contain prompt_ids and response_ids.
# Example triplet.prompt: {"token_ids": [...]}
# Example triplet.prompt: {"token_ids": [...], "image_urls": [...]}
# Example triplet.response: {"token_ids": [...]}
trace_list = [
{"prompt_ids": t.prompt.get("token_ids", []), "response_ids": t.response.get("token_ids", [])}
{
"prompt_ids": t.prompt.get("token_ids", []),
"response_ids": t.response.get("token_ids", []),
"image_urls": t.prompt.get("image_urls", []),
}
for t in rollout.triplets
]
info = {
@@ -705,60 +869,204 @@ class AgentModeDaemon:
rollout_id_list: List[str] = []
turn_index_list: List[int] = []
is_drop_list: List[bool] = []
image_grid_thw_list: List[Optional[torch.Tensor]] = [] # For Qwen2-VL mrope
n_trunc_sample_because_of_response = 0
for rollout_id, sample_info in finished_id_to_sample_info.items():
for turn_index, trace in enumerate(sample_info["trace_list"]):
if self.trace_aggregator.get("level", "transition") == "transition":
for rollout_id, sample_info in finished_id_to_sample_info.items():
for turn_index, trace in enumerate(sample_info["trace_list"]):
reward_list.append(sample_info["reward"])
prompt_ids, response_ids = trace["prompt_ids"], trace["response_ids"]
reward_list.append(sample_info["reward"])
prompt_ids, response_ids = trace["prompt_ids"], trace["response_ids"]
# Mark samples with prompts exceeding max_prompt_length to be dropped later
if len(prompt_ids) > max_prompt_length:
prompt_ids = prompt_ids[:max_prompt_length]
is_drop_list.append(True)
else:
is_drop_list.append(False)
# Mark samples with prompts exceeding max_prompt_length to be dropped later
if len(prompt_ids) > max_prompt_length:
prompt_ids = prompt_ids[:max_prompt_length]
is_drop_list.append(True)
else:
is_drop_list.append(False)
# Truncate responses that exceed max_response_length
if len(response_ids) > max_response_length:
response_ids = response_ids[:max_response_length]
n_trunc_sample_because_of_response += 1
# Truncate responses that exceed max_response_length
if len(response_ids) > max_response_length:
response_ids = response_ids[:max_response_length]
n_trunc_sample_because_of_response += 1
# Pad prompts to the left and responses to the right
one_input_ids, one_input_attention_mask = get_left_padded_ids_and_attention_mask(
prompt_ids, max_prompt_length, self.pad_token_id
)
one_response_ids, one_response_attention_mask = get_right_padded_ids_and_attention_mask(
response_ids, max_response_length, self.pad_token_id
)
# Pad prompts to the left and responses to the right
one_input_ids, one_input_attention_mask = get_left_padded_ids_and_attention_mask(
prompt_ids, max_prompt_length, self.pad_token_id
)
one_response_ids, one_response_attention_mask = get_right_padded_ids_and_attention_mask(
response_ids, max_response_length, self.pad_token_id
)
input_ids_list.append(one_input_ids)
input_attention_mask_list.append(one_input_attention_mask)
response_ids_list.append(one_response_ids)
response_attention_mask_list.append(one_response_attention_mask)
data_id_list.append(sample_info["data_id"])
rollout_id_list.append(rollout_id)
turn_index_list.append(turn_index)
input_ids_list.append(one_input_ids)
input_attention_mask_list.append(one_input_attention_mask)
response_ids_list.append(one_response_ids)
response_attention_mask_list.append(one_response_attention_mask)
data_id_list.append(sample_info["data_id"])
rollout_id_list.append(rollout_id)
turn_index_list.append(turn_index)
# Compute image_grid_thw for this triplet using image_urls from prompt
if self._use_mrope:
image_urls = trace.get("image_urls", [])
image_grid_thw_list.append(self._get_image_grid_thw(image_urls))
elif self.trace_aggregator.get("level", "transition") == "trajectory":
assert not self._use_mrope, "M-RoPE is not supported in trajectory level yet."
response_mask_list: List[List[int]] = []
unmerged_count: int = 0
template_mismatch_count, retoken_mismatch_count, others_mismatch_count = 0, 0, 0
response_per_turn_list: List[int] = []
for rollout_id, sample_info in finished_id_to_sample_info.items():
merged_trace_idx: List[List[int]] = []
# Identify which turns can be merged based on token ids prefix matching
current_merged_trace_idx: List[int] = []
current_context: List[int] = []
for turn_index, trace in enumerate(sample_info["trace_list"]):
response_per_turn_list.append(len(trace["response_ids"]))
is_prefix, diagnostic = ids_startswith(
trace["prompt_ids"] + trace["response_ids"],
current_context,
self.tokenizer,
self.trace_aggregator.get("debug", False),
)
if not is_prefix and self.trace_aggregator.get("debug", False) == True:
template_mismatch_count += diagnostic[0]
retoken_mismatch_count += diagnostic[1]
others_mismatch_count += diagnostic[2]
log_mismatch_detail(
diagnostic,
trace["prompt_ids"] + trace["response_ids"],
current_context,
global_steps,
rollout_id,
turn_index,
self.trace_aggregator.get("mismatch_log_dir", None),
)
if is_prefix:
current_context = trace["prompt_ids"] + trace["response_ids"]
current_merged_trace_idx.append(turn_index)
else:
merged_trace_idx.append(current_merged_trace_idx)
current_merged_trace_idx = [turn_index]
current_context = trace["prompt_ids"] + trace["response_ids"]
if current_merged_trace_idx not in merged_trace_idx:
merged_trace_idx.append(current_merged_trace_idx)
if len(merged_trace_idx) > 1:
unmerged_count += 1
# Merge all trace segments in merged_trace_idx into training samples
for current_merged_trace_idx in merged_trace_idx:
prompt_ids = sample_info["trace_list"][current_merged_trace_idx[0]]["prompt_ids"]
# if the merged_trace_idx doesn't start with the beginning of the prompt_ids, we need to adjust it
if current_merged_trace_idx[0] > 0 and len(prompt_ids) > max_prompt_length:
response_ids = prompt_ids[max_prompt_length:]
prompt_ids = prompt_ids[:max_prompt_length]
response_mask = [1] * len(response_ids)
else:
response_ids = []
response_mask = []
prompt_length = len(prompt_ids)
response_ids += sample_info["trace_list"][current_merged_trace_idx[0]]["response_ids"]
response_mask += [1] * len(response_ids)
for turn_index in current_merged_trace_idx[1:]:
trace = sample_info["trace_list"][turn_index]
new_prompt_length = len(trace["prompt_ids"]) - len(response_ids) - prompt_length
response_ids += trace["prompt_ids"][-new_prompt_length:]
response_ids += trace["response_ids"]
response_mask += [0] * new_prompt_length
response_mask += [1] * len(trace["response_ids"])
reward_list.append(sample_info["reward"])
# Mark samples with prompts exceeding max_prompt_length to be dropped later
if len(prompt_ids) > max_prompt_length:
prompt_ids = prompt_ids[:max_prompt_length]
is_drop_list.append(True)
else:
is_drop_list.append(False)
# Truncate responses that exceed max_response_length
if len(response_ids) > max_response_length:
response_ids = response_ids[:max_response_length]
response_mask = response_mask[:max_response_length]
n_trunc_sample_because_of_response += 1
# Pad prompts to the left and responses to the right
one_input_ids, one_input_attention_mask = get_left_padded_ids_and_attention_mask(
prompt_ids, max_prompt_length, self.pad_token_id
)
one_response_ids, one_response_attention_mask = get_right_padded_ids_and_attention_mask(
response_ids, max_response_length, self.pad_token_id
)
one_response_mask, _ = get_right_padded_ids_and_attention_mask(
response_mask, max_response_length, 0
)
input_ids_list.append(one_input_ids)
input_attention_mask_list.append(one_input_attention_mask)
response_ids_list.append(one_response_ids)
response_attention_mask_list.append(one_response_attention_mask)
response_mask_list.append(one_response_mask)
data_id_list.append(sample_info["data_id"])
rollout_id_list.append(rollout_id)
# turn_index_list.append(current_merged_trace_idx)
else:
raise ValueError(f"Unknown trace_aggregator level: {self.trace_aggregator.get('level')}")
n_transition = len(input_ids_list)
batch_input_ids = torch.LongTensor(input_ids_list).to(device)
input_attention_mask = torch.LongTensor(input_attention_mask_list).to(device)
batch_response_ids = torch.LongTensor(response_ids_list).to(device)
response_attention_mask = torch.LongTensor(response_attention_mask_list).to(device)
response_mask = (
torch.LongTensor(response_mask_list).to(device) if self.trace_aggregator.get("level", "transition") == "trajectory" else None # type: ignore
)
# Concatenate prompts and responses to form the full sequence
batch_seq = torch.cat([batch_input_ids, batch_response_ids], dim=-1)
attention_mask = torch.cat([input_attention_mask, response_attention_mask], dim=-1)
position_ids = torch.clamp(torch.cumsum(attention_mask, dim=-1) - 1, min=0)
# Compute position_ids - use mrope for Qwen2-VL, standard 2D otherwise
if self._use_mrope:
# For Qwen2-VL: compute 4D position_ids (batch_size, 4, seq_length)
position_ids_list: list[torch.Tensor] = []
for i in range(n_transition):
pos_ids = self._compute_mrope_position_ids(
input_ids=batch_seq[i],
attention_mask=attention_mask[i],
image_grid_thw=image_grid_thw_list[i] if image_grid_thw_list else None,
) # (4, seq_length)
position_ids_list.append(pos_ids)
# Stack to (batch_size, 4, seq_length)
position_ids = torch.stack(position_ids_list, dim=0)
else:
# Standard 2D position_ids (batch_size, seq_length)
position_ids = torch.clamp(torch.cumsum(attention_mask, dim=-1) - 1, min=0)
is_drop_mask = torch.BoolTensor(is_drop_list).to(device)
scores = torch.tensor(reward_list, dtype=torch.bfloat16).to(device)
# Create token-level scores by placing the final reward at the last token position
token_level_scores = torch.zeros_like(attention_mask, dtype=scores.dtype)
# For mrope (3D position_ids), use the first dimension (text position_ids) for eos calculation
if self._use_mrope:
# position_ids is (batch_size, 4, seq_length), use first dim for text positions
text_position_ids = position_ids[:, 0, :] # (batch_size, seq_length)
eos_mask_idx = torch.argmax(text_position_ids * attention_mask, dim=-1) # (bsz,)
else:
eos_mask_idx = torch.argmax(position_ids * attention_mask, dim=-1) # (bsz,)
# At the eos_mask_idx position of each sample, fill in the corresponding scores.
# torch.arange(n_transition) generates [0,1,2,...,bsz-1] as indices for the batch dimension.
eos_mask_idx = torch.argmax(position_ids * attention_mask, dim=-1) # (bsz,)
token_level_scores[torch.arange(n_transition), eos_mask_idx] = scores
# Only take the last response_length part of the sequence to get the token-level scores for the model's response part.
token_level_scores = token_level_scores[:, -max_response_length:]
@@ -773,7 +1081,12 @@ class AgentModeDaemon:
"position_ids": position_ids,
"is_drop_mask": is_drop_mask,
"token_level_scores": token_level_scores.contiguous(),
},
**(
{"response_mask": response_mask}
if self.trace_aggregator.get("level", "transition") == "trajectory"
else {}
),
}, # type: ignore
batch_size=n_transition,
)
data_proto = DataProto(batch=batch)
@@ -785,12 +1098,38 @@ class AgentModeDaemon:
"training/n_rollouts_w_reward": sample_with_reward_count,
"training/n_truncated_triplets": n_trunc_sample_because_of_response,
"training/n_triplets": n_transition,
# log data, only for debug testing
**(
{
"training/n_unmerged_rollouts": unmerged_count, # type: ignore
"training/n_triplets_by_turn": len(response_per_turn_list), # type: ignore
"training/avg_response_length_by_turn": np.mean(response_per_turn_list), # type: ignore
"training/max_response_length_by_turn": np.max(response_per_turn_list), # type: ignore
"training/min_response_length_by_turn": np.min(response_per_turn_list), # type: ignore
}
if self.trace_aggregator.get("level", "transition") == "trajectory"
else {}
),
**(
{
"training/template_mismatch_triplets": template_mismatch_count, # type: ignore
"training/retoken_mismatch_triplets": retoken_mismatch_count, # type: ignore
"training/others_mismatch_triplets": others_mismatch_count, # type: ignore
"training/template_mismatch_ratio": template_mismatch_count / len(response_per_turn_list), # type: ignore
"training/retoken_mismatch_ratio": retoken_mismatch_count / len(response_per_turn_list), # type: ignore
"training/others_mismatch_ratio": others_mismatch_count / len(response_per_turn_list), # type: ignore
}
if self.trace_aggregator.get("level", "transition") == "trajectory"
and self.trace_aggregator.get("debug", False)
else {}
),
}
# Add non-tensor data for advantage calculation and logging
data_proto.non_tensor_batch["data_id_list"] = np.array(data_id_list) # type: ignore
data_proto.non_tensor_batch["rollout_id_list"] = np.array(rollout_id_list) # type: ignore
data_proto.non_tensor_batch["turn_index_list"] = np.array(turn_index_list) # type: ignore
if self.trace_aggregator.get("level", "transition") == "transition":
data_proto.non_tensor_batch["turn_index_list"] = np.array(turn_index_list) # type: ignore
return data_proto, data_metrics
+49 -16
View File
@@ -1,13 +1,16 @@
# Copyright (c) Microsoft. All rights reserved.
# type: ignore
# pyright: reportUnknownVariableType=false
# pyright: reportUnknownMemberType=false
# pyright: reportUnknownArgumentType=false
from importlib.metadata import version
from typing import Any
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Type
import hydra
import ray
from packaging import version as packaging_version
from ray.actor import ActorClass
from verl.trainer.main_ppo import create_rl_sampler
from verl.trainer.ppo.reward import load_reward_manager
@@ -17,7 +20,10 @@ from agentlightning.store.base import LightningStore
from agentlightning.types import Dataset
from .dataset import AgentDataset, LoadedDataset
from .trainer import AgentLightningTrainer
if TYPE_CHECKING:
from .daemon import AgentModeDaemon
from .trainer import AgentLightningTrainer
__all__ = [
"main",
@@ -27,8 +33,20 @@ __all__ = [
@hydra.main(config_path="pkg://agentlightning/verl", config_name="config", version_base=None)
def main(config):
run_ppo(config, train_dataset=None, val_dataset=None, store=None, llm_proxy=None, adapter=None)
def main(config: Any):
from .daemon import AgentModeDaemon
from .trainer import AgentLightningTrainer
run_ppo(
config,
train_dataset=None,
val_dataset=None,
store=None,
llm_proxy=None,
adapter=None,
trainer_cls=AgentLightningTrainer,
daemon_cls=AgentModeDaemon,
)
def run_ppo(
@@ -38,6 +56,8 @@ def run_ppo(
store: LightningStore | None,
llm_proxy: LLMProxy | None,
adapter: TraceAdapter[Any] | None,
trainer_cls: Type[AgentLightningTrainer],
daemon_cls: Type[AgentModeDaemon],
) -> None:
if not ray.is_initialized():
# this is for local ray cluster
@@ -56,13 +76,15 @@ def run_ppo(
runner = TaskRunner.remote()
ray.get(
runner.run.remote(
runner.run.remote( # type: ignore
config=config,
train_dataset=train_dataset,
val_dataset=val_dataset,
store=store,
llm_proxy=llm_proxy,
adapter=adapter,
trainer_cls=trainer_cls,
daemon_cls=daemon_cls,
)
)
@@ -72,11 +94,13 @@ class TaskRunner:
def run(
self,
config: Any,
train_dataset: Dataset | None,
val_dataset: Dataset | None,
train_dataset: Dataset[Any] | None,
val_dataset: Dataset[Any] | None,
store: LightningStore | None,
llm_proxy: LLMProxy | None,
adapter: TraceAdapter | None,
adapter: TraceAdapter[Any] | None,
trainer_cls: Type[AgentLightningTrainer],
daemon_cls: Type[AgentModeDaemon],
):
# print initial config
from pprint import pprint
@@ -91,7 +115,7 @@ class TaskRunner:
local_path = copy_to_local(config.actor_rollout_ref.model.path)
# instantiate tokenizer
from verl.utils import hf_processor, hf_tokenizer
from verl.utils.tokenizer import hf_processor, hf_tokenizer
trust_remote_code = config.data.get("trust_remote_code", False)
tokenizer = hf_tokenizer(local_path, trust_remote_code=trust_remote_code)
@@ -112,7 +136,8 @@ class TaskRunner:
elif config.actor_rollout_ref.actor.strategy == "megatron":
assert config.actor_rollout_ref.actor.strategy == config.critic.strategy
from verl.single_controller.ray.megatron import NVMegatronRayWorkerGroup
# FIXME: This import is outdated
from verl.single_controller.ray.megatron import NVMegatronRayWorkerGroup # type: ignore
from verl.workers.megatron_workers import ActorRolloutRefWorker, CriticWorker
actor_rollout_cls = ActorRolloutRefWorker
@@ -121,9 +146,16 @@ class TaskRunner:
else:
raise NotImplementedError
from verl.trainer.ppo.ray_trainer import ResourcePoolManager, Role
from verl.trainer.ppo.ray_trainer import ResourcePoolManager
role_worker_mapping = {
try:
# verl >= 0.6.0
from verl.trainer.ppo.utils import Role
except ImportError:
# Fallback for verl <= 0.5.0
from verl.trainer.ppo.ray_trainer import Role # type: ignore
role_worker_mapping: dict[Role, ActorClass[Any]] = {
Role.ActorRollout: ray.remote(actor_rollout_cls),
Role.Critic: ray.remote(CriticWorker),
}
@@ -190,7 +222,7 @@ class TaskRunner:
val_dataset = LoadedDataset(val_dataset)
train_sampler = create_rl_sampler(config.data, train_dataset)
trainer = AgentLightningTrainer(
trainer = trainer_cls(
config=config,
tokenizer=tokenizer,
processor=processor,
@@ -206,6 +238,7 @@ class TaskRunner:
store=store,
llm_proxy=llm_proxy,
adapter=adapter,
daemon_cls=daemon_cls,
)
trainer.init_workers()
trainer.fit()
+57 -7
View File
@@ -8,7 +8,7 @@ import random
from contextlib import contextmanager
from copy import deepcopy
from pprint import pprint
from typing import Dict, Tuple
from typing import Dict, Tuple, Type
import numpy as np
import torch
@@ -174,12 +174,18 @@ class AgentLightningTrainer(RayPPOTrainer):
"""
def __init__(
self, store: LightningStore | None, llm_proxy: LLMProxy | None, adapter: TraceAdapter | None, **kwargs
self,
store: LightningStore | None,
llm_proxy: LLMProxy | None,
adapter: TraceAdapter | None,
daemon_cls: Type[AgentModeDaemon],
**kwargs,
):
super().__init__(**kwargs)
self.store = store
self.llm_proxy = llm_proxy
self.adapter = adapter
self.daemon_cls = daemon_cls
def _validate(self):
assert len(self.val_dataloader) == 1, "Please set val_batch_size to None for better throughput."
@@ -199,6 +205,37 @@ class AgentLightningTrainer(RayPPOTrainer):
self.async_rollout_manager.sleep()
return test_metrics
def _compute_reference_log_prob(self, batch: DataProto) -> DataProto:
"""Compute reference log probability using the correct worker based on LoRA configuration.
In verl 0.6.0+, when LoRA is detected (indicated by ref_in_actor=True),
the reference policy is computed by the actor rollout worker instead of a separate
ref policy worker. This method handles both scenarios by checking the ref_in_actor flag.
Note: verl sets ref_in_actor=True when it detects LoRA configuration (e.g., lora_rank > 0 or lora_adapter_path is set).
Args:
batch: The data batch to compute reference log probabilities for.
Returns:
DataProto with reference log probabilities added.
Raises:
RuntimeError: If the required worker is not available.
"""
if getattr(self, "ref_in_actor", False):
actor_worker = getattr(self, "actor_rollout_wg", None)
if actor_worker is None:
raise RuntimeError("actor_rollout_wg is required when ref_in_actor is True.")
return actor_worker.compute_ref_log_prob(batch)
ref_worker = getattr(self, "ref_policy_wg", None)
if ref_worker is None:
raise RuntimeError(
"Reference policy worker was not initialized. "
"Ensure `use_reference_policy` is enabled and the VERL config exposes the ref worker."
)
return ref_worker.compute_ref_log_prob(batch)
def _train_step(self, batch_dict: dict) -> dict:
# Isolate in a separate method to automatically recycle the variables before validation.
batch: DataProto = DataProto.from_single_dict(batch_dict)
@@ -218,9 +255,18 @@ class AgentLightningTrainer(RayPPOTrainer):
)
self.agent_mode_daemon.run_until_all_finished()
batch, agent_metrics = self.agent_mode_daemon.get_train_data_batch(
max_prompt_length=self.config.data.max_prompt_length,
max_response_length=self.config.data.max_response_length,
max_prompt_length=(
self.config.agentlightning.trace_aggregator.trajectory_max_prompt_length
if self.config.agentlightning.trace_aggregator.level.startswith("trajectory")
else self.config.data.max_prompt_length
),
max_response_length=(
self.config.agentlightning.trace_aggregator.trajectory_max_response_length
if self.config.agentlightning.trace_aggregator.level.startswith("trajectory")
else self.config.data.max_response_length
),
device=gen_batch.batch["fake_ids"].device,
global_steps=self.global_steps,
)
metrics.update(agent_metrics)
self.agent_mode_daemon.clear_data_and_server()
@@ -245,7 +291,8 @@ class AgentLightningTrainer(RayPPOTrainer):
# uid is used for algorithm like GRPO, should be aligned to data id
batch.non_tensor_batch["uid"] = batch.non_tensor_batch["data_id_list"]
batch.batch["response_mask"] = compute_response_mask(batch)
if "response_mask" not in batch.batch:
batch.batch["response_mask"] = compute_response_mask(batch)
# compute global_valid tokens
batch.meta_info["global_token_num"] = torch.sum(batch.batch["attention_mask"], dim=-1).tolist()
@@ -276,7 +323,7 @@ class AgentLightningTrainer(RayPPOTrainer):
if self.use_reference_policy:
# compute reference log_prob
with _timer("ref", timing_raw):
ref_log_prob = self.ref_policy_wg.compute_ref_log_prob(batch)
ref_log_prob = self._compute_reference_log_prob(batch)
batch = batch.union(ref_log_prob)
# compute values
@@ -413,7 +460,7 @@ class AgentLightningTrainer(RayPPOTrainer):
else:
# For other versions (e.g., 0.6.0), we use the full path to the model.
model = self.config.actor_rollout_ref.model.path
self.agent_mode_daemon = AgentModeDaemon(
self.agent_mode_daemon = self.daemon_cls(
self.config.agentlightning.port,
self.config.actor_rollout_ref.rollout.n,
train_information={
@@ -427,6 +474,9 @@ class AgentLightningTrainer(RayPPOTrainer):
store=self.store,
llm_proxy=self.llm_proxy,
adapter=self.adapter,
processor=self.processor, # For Qwen2-VL mrope position_ids
image_base_dir=getattr(self.config.data, "image_base_dir", None),
trace_aggregator=self.config.agentlightning.trace_aggregator,
)
self.agent_mode_daemon.start()
+1
View File
@@ -0,0 +1 @@
# Put contrib-related gitignore files here.
+4
View File
@@ -0,0 +1,4 @@
# Put code owner definitions here.
# Recipes
recipes/search_r1 @SiyunZhao @JiahangXu
+21
View File
@@ -0,0 +1,21 @@
# Contrib Area
This tree hosts experimental integrations, third-party recipes, and curated recipes that are not ready for the main `agentlightning/`, `examples/`, or `docs/` trees. Treat it as an incubator: keep contributions self-contained, clearly owned, and reproducible so downstream users can vendor them without guesswork.
## When to add something here
- You are iterating on a runtime extension that would bloat the primary `agentlightning/` namespace.
- You want to share a recipe that assembles existing components for a focused agent training or optimization workflow and needs more context than the main examples directory allows.
- You need automation scripts or download helpers that will help the community but should not live under `scripts/` at the repo root.
If a contribution starts depending on core release cadence, tight CI guarantees, or repo-wide infrastructure, talk to maintainers about graduating it out of `contrib/`.
## Directory map
- `agentlightning/` — Namespace packages, utilities, and adapters that extend the published wheel. Place new code under `agentlightning/contrib/<feature>/` so `import agentlightning.contrib.<feature>` works for downstream users.
- `recipes/` — Task-focused example bundles that solve a specific problem and derive certain results. Each recipe belongs in its own directory with a README that documents usage, result reports, and ownership.
- `scripts/` — Shared automation, dataset download steps, or reproducibility helpers that support the contrib modules above.
When adding folders, document the intent in a local README, link to companion docs or examples, and update `CODEOWNERS` so future fixes reach the right reviewers quickly.
Questions or proposals for new subtrees can be discussed in Discord, GitHub issues, or GitHub Discussions before opening a PR. For the canonical requirements and review checklist, see the “Agent-lightning Contrib” section of [`docs/community/contributing.md`](../docs/community/contributing.md).
@@ -0,0 +1,3 @@
# Copyright (c) Microsoft. All rights reserved.
# Namespace package for agentlightning.contrib.
@@ -2,7 +2,7 @@
## Overview
This example implements **Search R1** within Agent Lightning. It also serves as a demonstration of a **framework-free agent training pipeline**, showing how to run end-to-end RL training without relying on specialized frameworks. **It's tested and compatible with Agent-lightning v0.1.x**.
This example implements **Search R1** within Agent Lightning. It also serves as a demonstration of a **framework-free agent training pipeline**, showing how to run end-to-end RL training without relying on specialized frameworks. **It's tested and compatible with Agent-lightning v0.2.x**.
The example is designed to run on a single node with 8 GPUs, each having at least 40 GB of memory.
@@ -14,7 +14,7 @@ The example is designed to run on a single node with 8 GPUs, each having at leas
| `retrieval_launch.sh` | Launches the retrieval service backed by the processed corpus |
| `retrieval_server.py` | FastAPI server that powers document retrieval during training |
| `search_r1_agent.py` | Agent-Lightning rollout script implementing the Search-R1 workflow |
| `train.sh` | Starts the RL training server that coordinates GRPO optimization |
| `train_search_r1_agent.py` | RL training script that coordinates GRPO optimization |
| `qa_em.py` | Exact-match evaluation utilities for validating model predictions |
---
@@ -54,7 +54,7 @@ The retrieval server implementation is based on `search_r1/search/retrieval_serv
---
## Run RL Training (GRPO) with Llama-3.2-3b-base
## Run RL Training (GRPO) with Llama-3.2-3B-Instruct
1. **Start Ray**
@@ -65,26 +65,28 @@ The retrieval server implementation is based on `search_r1/search/retrieval_serv
> If you plan to use WandB for experiment tracking, set the environment variable
> `WANDB_API_KEY` before starting Ray.
2. **Launch the Agent**
```bash
python search_r1_agent.py
```
This script automatically launches **128 agent workers** by default. Each agent follows the Search-R1 workflow, retrieving information from the database and generating answers accordingly.
3. **Start the Training Server**
2. **Start the Training Server**
In another terminal, run:
```bash
bash train.sh
python train_search_r1_agent.py llama
```
This script starts the RL training server.
This script starts the RL training. Each agent follows the Search-R1 workflow, retrieving information from the database and generating answers accordingly.
---
## Evaluation
## Benchmark Results
Evaluation scripts and benchmark results will be released soon.
We evaluated Search-R1 across seven diverse question-answering benchmarks, covering both General QA (NQ, TriviaQA, PopQA) and complex multi-hop reasoning tasks (HotpotQA, 2WikiMultiHopQA, Musique, and Bamboogle).
The following tables compare the performance of the original Search-R1 implementation and the Agent-Lightning version across various base models.
| Model | Source | NQ | TriviaQA | PopQA | HotpotQA | 2Wiki | Musique | Bamboogle |
| :--- | :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| **Qwen2.5-3B-Instruct** | **Search-R1 (Original)** | 34.1 | 54.5 | 37.8 | 32.4 | 31.9 | 10.3 | 26.4 |
| | **Agent-Lightning** | **45.3** | **61.7** | **43.8** | **42.6** | **36.4** | **17.1** | **37.6** |
| **Qwen2.5-7B-Instruct** | **Search-R1 (Original)** | 39.3 | 61.0 | 39.7 | 37.0 | 41.4 | 14.6 | 36.8 |
| | **Agent-Lightning** | **46.5** | **65.9** | **46.8** | **43.7** | **46.2** | **20.3** | **47.2** |
| **Llama-3.2-3B** | **Search-R1 (Reproduced)** | 26.3 | 49.0 | 23.0 | 21.6 | 27.3 | 4.5 | 9.7 |
| | **Agent-Lightning** | **29.6** | **51.9** | **25.7** | **23.2** | **28.3** | **5.8** | 9.6 |
@@ -75,7 +75,7 @@ def extract_solution(solution_str: str) -> Optional[str]:
matches = list(match_iter)
# If there are 0 or exactly 1 matches, return None
if len(matches) <= 1:
if len(matches) == 0:
return None
# If there are 2 or more matches, return the last one
@@ -1,16 +1,21 @@
# Copyright (c) Microsoft. All rights reserved.
from __future__ import annotations
import os
import re
import time
from typing import Any, Dict, List, Optional, Tuple, TypedDict, cast
import pandas as pd
import requests
from openai import OpenAI
from qa_em import compute_score_em
from agentlightning import LLM, LitAgent, NamedResources, Trainer, reward, setup_logging
from agentlightning import LLM, LitAgent, NamedResources, Rollout, Trainer, configure_logger, setup_logging
setup_logging()
logger = configure_logger(name=__name__)
# Copied and adapted from https://github.com/PeterGriffinJin/Search-R1/blob/main/scripts/data_process/nq_search.py
INSTRUCTION_FORMAT = """Answer the given question. You must conduct reasoning inside <think> and </think> first every time you get new information. After reasoning, if you find you lack some knowledge, you can call a search engine by <search> query </search> and it will return the top searched results between <information> and </information>. You can search as many times as your want. If you find no further external knowledge needed, you can directly provide the answer inside <answer> and </answer>, without detailed illustrations. For example, <answer> Beijing </answer>. Question: """
@@ -24,8 +29,7 @@ class RetrievalItem(TypedDict):
document: Document
@reward
async def eval(prediction: str, ground_truth: List[str]) -> float:
def eval(prediction: str, ground_truth: List[str]) -> float:
reward_score = float(compute_score_em(prediction, ground_truth))
print(f"pred: {prediction} | {type(ground_truth)} gold_answer: {ground_truth} | res: {reward_score}")
return reward_score
@@ -106,62 +110,109 @@ def call_llm(
return response.choices[0].message.content or ""
class Searchr1Agent(LitAgent[Any]):
async def training_rollout_async(
class SearchR1Agent(LitAgent[Dict[str, Any]]):
def __init__(
self,
task: Any,
val_temperature: Optional[float] = 0.0,
max_turns: int = 4,
) -> None:
super().__init__()
self.val_temperature = val_temperature
self.data_dir = os.environ.get("VERL_SEARCHR1_DATA_DIR", "data")
self.max_turns = max_turns
def rollout(
self,
task: Dict[str, Any],
resources: NamedResources,
rollout: Any,
temperature: float = 1.0,
) -> Any:
rollout: Rollout,
) -> float | None:
prompt = INSTRUCTION_FORMAT + task["question"]
answer_list: List[str] = cast(List[str], task["golden_answers"])
llm: LLM = cast(LLM, resources.get("main_llm"))
rollout_id = rollout.rollout_id
logger.info(f"[Rollout {rollout_id}] Question: {task['question']}")
logger.info(f"[Rollout {rollout_id}] Ground Truth: {answer_list}")
start_time = time.time()
llm: LLM = cast(LLM, resources["main_llm"])
client = OpenAI(
base_url=llm.endpoint,
base_url=llm.get_base_url(rollout_id, rollout.attempt.attempt_id), # type: ignore
api_key=os.environ.get("OPENAI_API_KEY", "token-abc123"),
)
if rollout.mode == "train":
temperature = llm.sampling_parameters.get("temperature", 1.0)
else:
temperature = self.val_temperature if self.val_temperature is not None else 0.0
turn_id = 0
finished_flag = False
rollout_content: str = ""
while turn_id < 4 and not finished_flag:
turn_id += 1
turn_response = call_llm(
client, llm.model, prompt + rollout_content, temperature=temperature, max_tokens=500
)
valid_turn_response = postprocess_response(turn_response)
turn_env_feedback = execute_response(valid_turn_response)
if len(turn_env_feedback) == 0:
finished_flag = True
print(f"TURN ID {turn_id} | RESP: {turn_response} | ENV FEEDBACK: {turn_env_feedback}")
rollout_content += turn_response + turn_env_feedback
try:
while turn_id < self.max_turns and not finished_flag:
turn_id += 1
turn_response = call_llm(
client, llm.model, prompt + rollout_content, temperature=temperature, max_tokens=500
)
valid_turn_response = postprocess_response(turn_response)
rollout_content += valid_turn_response
turn_env_feedback = execute_response(valid_turn_response)
if len(turn_env_feedback) == 0:
finished_flag = True
else:
rollout_content += turn_env_feedback
logger.info(f"TURN ID {turn_id} | RESP: {turn_response} | ENV FEEDBACK: {turn_env_feedback}")
if not finished_flag:
turn_response = call_llm(
client, llm.model, prompt + rollout_content, temperature=temperature, max_tokens=500
)
rollout_content += turn_response
print(f"LAST TURN GENERATE | RESP: {turn_response}")
if not finished_flag:
turn_response = call_llm(
client, llm.model, prompt + rollout_content, temperature=temperature, max_tokens=500
)
rollout_content += turn_response
logger.info(f"LAST TURN GENERATE | RESP: {turn_response}")
reward_score = await eval(rollout_content, answer_list) # reward is tracked with the decorator
print(
except Exception as e:
logger.exception(f"[Rollout {rollout_id}] Error during rollout: {e}")
return None
end_time_rollout = time.time()
reward_score = eval(rollout_content, answer_list)
logger.info("[Rollout %s] Reward: %s", rollout_id, reward_score)
end_time_eval = time.time()
logger.info("[Rollout %s] Time taken for rollout: %.2f seconds", rollout_id, end_time_rollout - start_time)
logger.info(
"[Rollout %s] Time taken for evaluation: %.2f seconds", rollout_id, end_time_eval - end_time_rollout
)
logger.info(
"question: {} answer: {} ground_truth: {} reward: {}".format(
task["question"], rollout_content, answer_list, reward_score
)
)
return reward_score
async def validation_rollout_async(
self,
task: Any,
resources: NamedResources,
rollout: Any,
) -> Any:
# Use the same resources; set temperature to 0.0 for deterministic validation.
return await self.training_rollout_async(task, resources, rollout, temperature=0.0)
def debug_search_r1_agent():
searchr1_dev_data_path = os.path.join(os.environ.get("VERL_SEARCHR1_DATA_DIR", "data"), "test.parquet")
if not os.path.exists(searchr1_dev_data_path):
raise FileNotFoundError(f"Search_R1 dev data file {searchr1_dev_data_path} does not exist.")
df = pd.read_parquet(searchr1_dev_data_path).head(10) # type: ignore
df = cast(List[Dict[str, Any]], df.to_dict(orient="records")) # type: ignore
print("Debug data:", df)
trainer = Trainer(
n_workers=1,
initial_resources={
"main_llm": LLM(
endpoint=os.environ["OPENAI_API_BASE"],
model="gpt-4.1-nano",
sampling_parameters={"temperature": 0.0},
)
},
)
trainer.dev(SearchR1Agent(), df)
if __name__ == "__main__":
Trainer(n_workers=128).fit(Searchr1Agent(), "http://localhost:9999/")
debug_search_r1_agent()
@@ -0,0 +1,171 @@
# Copyright (c) Microsoft. All rights reserved.
from __future__ import annotations
import argparse
import os
from copy import deepcopy
from datetime import datetime
from typing import Any, Dict
import pandas as pd
from search_r1_agent import SearchR1Agent
import agentlightning as agl
RL_TRAINING_CONFIG: Dict[str, Any] = {
"algorithm": {
"adv_estimator": "grpo",
"use_kl_in_reward": False,
},
"data": {
"train_files": "data/train.parquet",
"val_files": "data/test.parquet",
"train_batch_size": 512,
"max_prompt_length": 6000,
"max_response_length": 4096,
"truncation": "error",
},
"actor_rollout_ref": {
"rollout": {
"tensor_model_parallel_size": 1,
"n": 5,
"log_prob_micro_batch_size_per_gpu": 4,
"multi_turn": {"format": "hermes"},
"name": "vllm",
"gpu_memory_utilization": 0.5,
"engine_kwargs": {
"vllm": {
"enable_auto_tool_choice": True,
"tool_call_parser": "hermes",
}
},
},
"actor": {
"ppo_mini_batch_size": 256,
"ppo_micro_batch_size_per_gpu": 4,
"optim": {"lr": 1e-6, "lr_warmup_steps_ratio": 0.95},
"use_kl_loss": True,
"kl_loss_type": "low_var_kl",
"kl_loss_coef": 0.001,
"entropy_coeff": 0,
"clip_ratio_low": 0.2,
"clip_ratio_high": 0.3,
"fsdp_config": {
"param_offload": True,
"optimizer_offload": True,
},
},
"ref": {
"log_prob_micro_batch_size_per_gpu": 4,
"fsdp_config": {"param_offload": True},
},
"model": {
"path": "Qwen/Qwen2.5-Coder-1.5B-Instruct",
"use_remove_padding": True,
"enable_gradient_checkpointing": True,
},
},
"trainer": {
"n_gpus_per_node": 8,
"val_before_train": True,
"critic_warmup": 0,
"logger": ["console", "wandb"],
"project_name": "AgentLightning",
"experiment_name": "searchr1",
"nnodes": 1,
"test_freq": 10,
"save_freq": 10,
"total_epochs": 15,
"total_training_steps": 300,
"default_local_dir": "checkpoints/searchr1_checkpoints/",
},
}
def config_train_fast() -> Dict[str, Any]:
"""A fast training run for CI testing purposes."""
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
EXPERIMENT_NAME = f"searchr1_{timestamp}"
PROJECT_NAME = "AgentLightningCI"
# Simulate writing to $GITHUB_OUTPUT if its set
github_output = os.getenv("GITHUB_OUTPUT")
if github_output:
with open(github_output, "a") as f:
f.write(f"project_name={PROJECT_NAME}\n")
f.write(f"run_name={EXPERIMENT_NAME}\n")
print("Set environment variables:")
print(f"PROJECT_NAME={PROJECT_NAME}")
print(f"EXPERIMENT_NAME={EXPERIMENT_NAME}")
config = deepcopy(RL_TRAINING_CONFIG)
config["actor_rollout_ref"]["rollout"]["gpu_memory_utilization"] = 0.6
config["actor_rollout_ref"]["model"]["path"] = "Qwen/Qwen2.5-Coder-0.5B-Instruct"
config["data"]["val_files"] = "data/test_dev.parquet"
config["trainer"]["total_epochs"] = 1
config["trainer"]["total_training_steps"] = 1
config["trainer"]["experiment_name"] = EXPERIMENT_NAME
config["trainer"]["project_name"] = PROJECT_NAME
config["trainer"]["test_freq"] = 1
return config
def config_train_qwen() -> Dict[str, Any]:
"""A configuration for training with Qwen-2.5."""
config = deepcopy(RL_TRAINING_CONFIG)
return config
def config_train_llama() -> Dict[str, Any]:
"""A configuration for training with LLaMA-3.2-3B-Instruct.
You will need a `HF_TOKEN` set to run with this config.
"""
config = deepcopy(RL_TRAINING_CONFIG)
config["actor_rollout_ref"]["rollout"]["multi_turn"]["format"] = "llama3_json"
config["actor_rollout_ref"]["rollout"]["engine_kwargs"]["vllm"]["tool_call_parser"] = "llama3_json"
config["actor_rollout_ref"]["model"]["path"] = "meta-llama/Llama-3.2-3B-Instruct"
return config
def train(config: Dict[str, Any]) -> None:
agent = SearchR1Agent()
algorithm = agl.VERL(config)
trainer = agl.Trainer(n_runners=32, algorithm=algorithm)
train_data = pd.read_parquet(config["data"]["train_files"]).to_dict(orient="records") # type: ignore
val_data = pd.read_parquet(config["data"]["val_files"]).to_dict(orient="records") # type: ignore
trainer.fit(agent, train_dataset=train_data, val_dataset=val_data) # type: ignore
def main() -> None:
"""Main function to parse arguments and run training."""
parser = argparse.ArgumentParser(description="Train a Search-R1 agent using different model configurations")
parser.add_argument(
"config",
choices=["fast", "qwen", "llama"],
help="Training configuration: 'fast' (CI testing), 'qwen' (Qwen-2.5-Coder-1.5B), 'llama' (LLaMA-3.2-3B-Instruct)",
)
args = parser.parse_args()
# Get the appropriate configuration
config_functions = {"fast": config_train_fast, "qwen": config_train_qwen, "llama": config_train_llama}
config = config_functions[args.config]()
print(f"Starting training with '{args.config}' configuration...")
train(config)
if __name__ == "__main__":
main()
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "agent-lightning-dashboard",
"version": "0.3.0",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "agent-lightning-dashboard",
"version": "0.3.0",
"version": "0.3.1",
"dependencies": {
"@mantine/core": "8.3.5",
"@mantine/hooks": "8.3.5",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "agent-lightning-dashboard",
"type": "module",
"version": "0.3.0",
"version": "0.3.1",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
@@ -3,6 +3,8 @@
import { useMemo, useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react';
import { IconSearch } from '@tabler/icons-react';
import { waitFor, within } from '@testing-library/dom';
import userEvent from '@testing-library/user-event';
import { Box, Stack, TextInput, Title } from '@mantine/core';
import type { Span } from '@/types';
import { compareRecords } from '@/utils/table';
@@ -570,4 +572,19 @@ const sequenceSortTestSpans: Span[] = [
export const SequenceIdSortTest: Story = {
render: () => <TracesTableStoryWrapper maxWidth={1200} spans={sequenceSortTestSpans} />,
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const seqHeader = await canvas.findByRole('button', { name: /seq\./i });
await userEvent.click(seqHeader);
await waitFor(() => {
const rows = canvas.getAllByRole('row');
const firstRow = rows[1];
if (!firstRow) {
throw new Error('Expected at least one data row after sorting by sequence ID');
}
within(firstRow).getByText('task_sequence_14');
});
},
};
+1 -1
View File
@@ -5,7 +5,7 @@ services:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus"
volumes:
- ./prometheus.memory-store.yml:/etc/prometheus/prometheus.yml:ro
- ./prometheus/prometheus.base.yml:/etc/prometheus/prometheus.yml:ro
- ${AGL_MONITORING_DATA_PATH:?Set AGL_MONITORING_DATA_PATH to the metrics directory}/prometheus:/prometheus
ports:
- "9090:9090"
+30 -2
View File
@@ -4,7 +4,27 @@ services:
file: compose.store.yml
service: app
command: agl store --host 0.0.0.0 --port 4747 --prometheus --backend memory
depends_on:
- app-exporter # Wait for the exporter to be ready first
command: agl store --host 0.0.0.0 --port 4747 --tracker console prometheus --backend memory
environment:
- PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_multiproc
volumes:
- prometheus_multiproc:/tmp/prometheus_multiproc
app-exporter:
build:
context: ../
dockerfile: docker/Dockerfile.dev
command: agl prometheus --host 0.0.0.0 --port 4748
ports:
- "4748:4748"
environment:
- PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_multiproc
volumes:
- prometheus_multiproc:/tmp/prometheus_multiproc
node-exporter:
image: prom/node-exporter:latest
@@ -22,10 +42,11 @@ services:
- "--storage.tsdb.path=/prometheus"
- "--storage.tsdb.retention.time=1h"
volumes:
- ./prometheus.memory-store.yml:/etc/prometheus/prometheus.yml:ro
- ./prometheus/prometheus.base.yml:/etc/prometheus/prometheus.yml:ro
- ./data/prometheus:/prometheus
depends_on:
- app
- app-exporter
- node-exporter
ports:
- "9090:9090"
@@ -51,3 +72,10 @@ services:
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_AUTH_DISABLE_LOGIN_FORM=true
- GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=/var/lib/grafana/dashboards/agentlightning.json
volumes:
prometheus_multiproc:
driver: local
driver_opts:
type: tmpfs
device: tmpfs
+27 -4
View File
@@ -21,18 +21,33 @@ services:
depends_on:
- mongo
- app-exporter # Wait for the exporter to be ready first
command:
- /bin/bash
- -c
- |
mkdir -p /tmp/prometheus &&
agl store --host 0.0.0.0 --port 4747 \
--prometheus --backend mongo \
--tracker console prometheus --backend mongo \
--mongo-uri mongodb://mongo:27017/?replicaSet=rs0 \
--n-workers ${AGL_STORE_N_WORKERS:-32}
environment:
- PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus
- PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_multiproc
volumes:
- prometheus_multiproc:/tmp/prometheus_multiproc
app-exporter:
build:
context: ../
dockerfile: docker/Dockerfile.dev
command: agl prometheus --host 0.0.0.0 --port 4748
ports:
- "4748:4748"
environment:
- PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_multiproc
volumes:
- prometheus_multiproc:/tmp/prometheus_multiproc
mongodb-exporter:
image: percona/mongodb_exporter:0.47.1
@@ -61,10 +76,11 @@ services:
- "--storage.tsdb.path=/prometheus"
- "--storage.tsdb.retention.time=1h"
volumes:
- ./prometheus.mongo-store.yml:/etc/prometheus/prometheus.yml:ro
- ./prometheus/prometheus.mongo.yml:/etc/prometheus/prometheus.yml:ro
- ./data/prometheus:/prometheus
depends_on:
- app
- app-exporter
- mongodb-exporter
- node-exporter
ports:
@@ -91,3 +107,10 @@ services:
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_AUTH_DISABLE_LOGIN_FORM=true
- GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=/var/lib/grafana/dashboards/agentlightning.json
volumes:
prometheus_multiproc:
driver: local
driver_opts:
type: tmpfs
device: tmpfs
+5
View File
@@ -9,6 +9,11 @@ services:
command: agl store --host 0.0.0.0 --port 4747
ulimits:
nofile:
soft: 65535
hard: 65535
develop:
watch:
# Sync the working directory with the `/app` directory in the container
File diff suppressed because it is too large Load Diff
@@ -1,11 +1,11 @@
global:
scrape_interval: 2s
evaluation_interval: 2s
scrape_interval: 5s
evaluation_interval: 5s
scrape_configs:
- job_name: app
static_configs:
- targets: ["app:4747"]
- targets: ["app-exporter:4748"]
metrics_path: /v1/prometheus/
- job_name: node
@@ -1,11 +1,11 @@
global:
scrape_interval: 2s
evaluation_interval: 2s
scrape_interval: 5s
evaluation_interval: 5s
scrape_configs:
- job_name: app
static_configs:
- targets: ["app:4747"]
- targets: ["app-exporter:4748"]
metrics_path: /v1/prometheus/
- job_name: node
Binary file not shown.

After

Width:  |  Height:  |  Size: 275 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 KiB

File diff suppressed because one or more lines are too long
+92
View File
@@ -1,5 +1,97 @@
# Changelog
## Agent-lightning v0.3.0 (12/24/2025)
Agent-lightning v0.3.0 is a major release that introduces several new features and bug fixes. The release is a collaborative effort between Agent-lightning core teams and the community. Thanks to all the contributors who made this release possible.
### Highlights
* **Tinker integration**: Support Tinker as an alternative backend for Reinforcement Learning (#226 #245 #264 #269 #327). See [example code](https://github.com/microsoft/agent-lightning/tree/v0.3.0/examples/tinker), [blog 1](https://medium.com/@yugez/tuning-any-ai-agent-with-tinker-agent-lightning-part-1-1d8c9a397f0e) and [blog 2](https://medium.com/@yugez/tuning-any-ai-agent-with-tinker-agent-lightning-part-2-332c5437f0dc).
* **Azure OpenAI integration**: Support Azure OpenAI as a backend for LLM inference and supervised fine-tuning (#256 #327). [Example code](https://github.com/microsoft/agent-lightning/tree/v0.3.0/examples/azure).
* **MongoDB-based Lightning Store** is added as an alternative backend for Lightning Store (#323). [Documentation](https://microsoft.github.io/agent-lightning/0.3.0/tutorials/parallelize/#parallelizing-lightningstore).
* **Contrib package**: Add contrib package for community projects. Search-R1 is integrated as a contrib recipe. More coming. (#239 #396 #410 #412 #417).
* **RESTful API**: Stabilize and document RESTful API for Lightning Store (#241 #275). [Documentation](https://microsoft.github.io/agent-lightning/0.3.0/reference/restful/).
* **OTel Semantic Conventions** that are specifically designed for Agent-optimization areas (#340). [Documentation](https://microsoft.github.io/agent-lightning/0.3.0/reference/semconv/).
* *[Preview]* **Agent-lightning Dashboard** is now available (#288 #289 #291 #296 #371 #375). It's the official web application for inspecting and debugging Agent-lightning experiments. See details [here](https://microsoft.github.io/agent-lightning/0.3.0/tutorials/debug/).
* *[Preview]* **Multi-modality example** featuring VERL and a LangGraph agent on ChartQA dataset (#379). [Example code](https://github.com/microsoft/agent-lightning/tree/v0.3.0/examples/chartqa).
* *[Preview]* Integrate **Claude Code** as a LitAgent and support training on SWE-Bench (#332 #346 #348). [Example code](https://github.com/microsoft/agent-lightning/tree/v0.3.0/examples/claude_code).
* *[Preview]* **Weave tracer** as a substitute for AgentOps tracer (#277 #411 #420 #423). [Documentation](https://microsoft.github.io/agent-lightning/0.3.0/tutorials/traces/#weave-tracer-experimental).
* *[Preview]* **Trajectory Level Aggregation** for more efficient training with VERL. See [blog](https://agent-lightning.github.io/posts/trajectory_level_aggregation/) and [documentation](https://microsoft.github.io/agent-lightning/0.3.0/algorithm-zoo/verl/).
### Store Benchmark
In this release, the Lightning Store core was redesigned for significantly greater efficiency and scalability (#315 #318 #328 #342 #344 #356 #380 #388 #418 #421). The benchmark results below demonstrate the impact: with large numbers of concurrent runners, v0.3.0 delivers up to a 15x increase in throughput compared to v0.2.2.
| Throughput (\#rollout/sec) | v0.2.2 | v0.3.0 (in-memory) | v0.3.0 (Mongo) |
| :---- | :---- | :---- | :---- |
| Minimal (batch, #runner=32, #turns=6) | 8.73 | 9.06 | 8.71 |
| Medium (batch, #runners=100, #turns=10) | 12.03 | 23.26 | 32.79 |
| Mid-high (batch, #runners=300, #turns=6) | 10.61 | 24.42 | 40.24 |
| Large (batch, #runners=1000, #turns=3) | 3.36 | 14.60 | 50.05 |
| Long queue (queue, #runners=256, #turns=4) | 7.42 | 30.86 | 57.01 |
| Heavy trace (queue, #runners=512, #turns=20) | 5.93 | 13.28 | 29.41 |
*Notes:*
1. Benchmarks were run on a single Standard_D32as_v4 Azure VM (Large and heavy trace tests used Standard_D64ads_v5), executed via GitHub Actions.
2. Two algorithm patterns are evaluated: the batch pattern submits a group of rollouts and waits for all to finish before starting the next group, while the queue pattern maintains a set number of in-flight rollouts, submitting new ones as soon as capacity frees up. Configuration details are available [here](https://github.com/microsoft/agent-lightning/blob/v0.3.0/.github/workflows/benchmark.yml).
3. The number of turns is directly proportional to the number of spans each rollout generates.
### Maintenance and Bug fixes
#### Core (Store, Interfaces, etc.)
* Add Trainer port option for client-server strategies (#198)
* Fix store port conflict handling (#227)
* Unified PythonServerLauncher (#286 #292 #303)
* Make health timeout configurable (#305)
* Refactor logging (#306)
* Support OTLP in LightningStore (#313)
* Centralized metrics helper (#368)
* Fix redundant cancel tracebacks on Ctrl+C (#370)
#### Proxy, Adapters and Algorithms
* Fix training metrics before and after processing in VERL (#145)
* Forward streaming requests for Anthropic and OpenAI APIs (as non-streaming requests) (#299)
* Check traces with reward for VERL (#317)
* Patch LiteLLM root span (#341)
* Handle ref_in_actor flag for LoRA compatibility (#386)
* Support `with_llm_proxy` and `with_store` in algorithms (#398)
* Support image URL export in TracerTraceToTriplets (#400)
* Fix match_rewards assign_to elements in TraceTree (#403)
* Support customizing trainer and daemon in VERL (#407)
#### Runners, Tracers and Agents
* Refactor tracer initialization (#321)
* Fix OpenAI Agents 0.6 compatibility (#322)
* `emit_operation`, `emit_annotation`, tags and links (#359)
* Sunset HTTP tracer (#402)
#### Examples
* Fix typos in train-first-agent.md (#263)
* Fix room_selector example which always runs the first task (#270)
* Fix typo in SQL agent example (#285)
* Add the README and script files for training SQL agent on NPU (#272)
* Examples Catalog and Refine Contribution Guide (#331)
* Upgrade LangChain to 1.x (#364)
* Update RAG example to Agent-lightning v0.2.x (#349)
#### Miscellaneous
* DeepWiki Badge (#263)
* Add AGENTS.md (#374)
### New Contributors
Warm welcome to our first-time contributors: @cptnm3, @TerryChan, @genji970, @zxgx, @xiaochulaoban, @lspinheiro, @Kwanghoon-Choi, @Vasuk12, @totoluo, @jinghuan-Chen 🎉
**Full Changelog**: https://github.com/microsoft/agent-lightning/compare/v0.2.0...v0.3.0
---
## Agent-lightning v0.2.2 (11/12/2025)
Agent-lightning v0.2.2 is a stabilization release for v0.2.1. It introduces several bug fixes.
+40 -10
View File
@@ -46,6 +46,7 @@ Bonus points for examples that:
- Ship CI or self-test coverage so we know they still work as the core evolves. **Otherwise, we would have to mark the example as unmaintained because we won't be able to test the examples manually before each release.**
- Include a [`docs/how-to/`]({{ src("docs/how-to/") }}) guide (or a detailed README if no how-to exists) without duplicating content in multiple places.
- Favor simple, dependency-light code over heavy abstractions.
- Ship a README that documents smoke-test instructions and includes an "Included Files" section summarizing every file and its role; keep the runnable module self-contained with a module-level docstring explaining CLI usage, plus targeted docstrings or inline comments for educational functions/classes.
!!! warning "Please discuss first"
@@ -74,6 +75,27 @@ Most brand-new algorithms ultimately land as “new examples,” so read that se
Have a project that builds on Agent-lightning but does not belong in the main repo? Fork it or depend on it externally, then let us know. We can showcase notable projects in [Community Projects](../index.md) and the main [README]({{ src("README.md") }}).
### Agent-lightning Contrib
[`contrib/`]({{ src("contrib") }}) is where work-in-progress or third-party integrations, and curated recipes live before they are hardened enough for the core runtime tree. Think of it as an incubator: additions should remain easy to consume, clearly owned, and scoped so downstream users can vendor them with minimal risk.
The following types of contributions are welcome in the contrib area:
- **Recipes** that assemble multiple Agent Lightning components for a narrow task (`contrib/recipes/<topic>/`). Each recipe must be self-contained, include running instructions and result reports.
- **Runtime extensions** that would bloat the primary `agentlightning/` namespace (`contrib/agentlightning/contrib/<feature>/`). These should mirror the published wheel layout so that `import agentlightning.contrib.<feature>` works out of the box.
- **Supporting scripts and assets** (`contrib/scripts/`) that automate dataset downloads, environment preparation, or benchmarks required by contrib modules.
If you are unsure where a contribution should live, start a thread in Discord or open an issue before writing code. The [contrib README]({{ src("contrib/README.md") }}) also lists the directory expectations.
A quick checklist for contributions to be accepted:
1. **Document everything.** Include configuration steps, environment variables, and sample commands so contributors can reproduce the results without guesswork. Pin to a specific version of Agent-lightning and other dependencies to avoid unexpected changes if you don't want to update the recipe frequently.
2. **Keep quality predictable.** Match the repos style guide, apply exhaustive type hints, and run `uv run --no-sync pyright` plus targeted `pytest` suites for any Python module you touch.
3. **Ship reproducibility artifacts.** Store only scripts or instructions for downloading datasets, weights, or binaries. Never upload large artifacts or credentials directly.
4. **Update ownership.** Add `CODEOWNERS` entries when new directories appear so maintainers know who can review follow-up fixes.
Contrib entries do not need the same maturity level as core code, but they must still meet the baseline above. Submissions that lack documentation, hide ownership, or depend on untracked assets are typically rejected until those gaps are resolved.
### Other Contribution Ideas
- **Tests.** Add or improve cases in [`tests/`]({{ src("tests") }}) (unit, integration, or end-to-end).
@@ -126,13 +148,13 @@ After `uv sync`, run commands via `uv run ...` (add `--no-sync` once the environ
Formatting and linting are enforced through [pre-commit](https://pre-commit.com/). Install once, then run before each push:
```bash
uv run pre-commit install
uv run pre-commit run --all-files --show-diff-on-failure --color=always
uv run --no-sync pre-commit install
uv run --no-sync pre-commit run --all-files --show-diff-on-failure --color=always
```
Once installed, the hooks run automatically on every `git commit`. Running the pre-commit hooks locally keeps CI green and diffs manageable.
### 3. Branch From a Fresh `main`
### 3. Branch from Fresh `main` and Code
Start all work from the latest upstream state:
@@ -165,20 +187,28 @@ Use lowercase with hyphens, e.g., `feature/async-runner-hooks`.
Remember to register new docs in [`mkdocs.yml`]({{ src("mkdocs.yml") }}), add examples to [examples/README]({{ src("examples/README.md") }}), and update the [Examples Catalog](../how-to/examples-catalog.md).
Before you start coding, bring the shared coding conventions with you:
- Target `requires-python >= 3.10`, four-space indentation, ~120-character lines (docstrings may run longer), and formatter-owned diffs (Black + isort with the `black` profile).
- Use `snake_case` for modules, functions, and variables; `PascalCase` for classes and React components; lowercase hyphenation for CLI flags, branch names, and TypeScript filenames.
- Maintain exhaustive type hints (pyright enforces them), write succinct Google-style docstrings (with `[][]` cross-references).
- Prefer dataclasses or Pydantic models from `agentlightning.types`.
- Log via `logging.getLogger(__name__)` with targeted DEBUG/INFO/WARNING/ERROR calls—especially for long multi-step functions or broad `try/except` blocks.
### 4. Test and Validate
Most contributions require automated checks. Prefix commands with `uv run` so they use the project environment.
Most contributions require automated checks. Once `uv sync` locks dependencies, prefix commands with `uv run --no-sync ...` so they share the same environment as CI.
**Full test suite**
```bash
uv run pytest -v
uv run --no-sync pytest -v
```
**Targeted tests**
```bash
uv run pytest tests/path/to/test_file.py -k test_name
uv run --no-sync pytest tests/path/to/test_file.py -k test_name
```
**Optional/gated tests:** GPU-specific suites or API-dependent tests run automatically when the required hardware or environment variables (such as `OPENAI_API_KEY`) are present.
@@ -186,7 +216,7 @@ uv run pytest tests/path/to/test_file.py -k test_name
**Static analysis:**
```bash
uv run pyright
uv run --no-sync pyright
```
If you have touched code under `examples/`, you should run the example-specific smoke tests. Each directory includes a README with example-specific smoke tests—run those too.
@@ -196,8 +226,8 @@ If you have touched code under `examples/`, you should run the example-specific
Keep API references under [docs/reference]({{ src("docs/reference/") }}) up to date. Doc-only changes should still build cleanly:
```bash
uv run mkdocs serve --strict # live reload
uv run mkdocs build --strict # CI-equivalent
uv run --no-sync mkdocs serve --strict # live reload
uv run --no-sync mkdocs build --strict # CI-equivalent
```
`--strict` elevates warnings to errors so you catch issues before CI.
@@ -205,7 +235,7 @@ If you have touched code under `examples/`, you should run the example-specific
Before opening a PR, double-check the basics:
- Run `uv lock` if you changed dependencies.
- Run `uv run pre-commit run --all-files` (hooks installed via `pre-commit install` run automatically on `git commit`, but rerun them if you amended history).
- Run `uv run --no-sync pre-commit run --all-files --show-diff-on-failure` (hooks installed via `pre-commit install` run automatically on `git commit`, but rerun them if you amended history).
- Execute the relevant commands from the test list above.
- Validate each affected example via its README instructions.
+207 -19
View File
@@ -1,8 +1,8 @@
# Understanding Store
The **[`LightningStore`][agentlightning.LightningStore]** is the central coordination point for Agent-lightning. It holds the task queue, rollouts, attempts, spans, and versioned resources, and exposes a small API both Runners and Algorithms use to communicate. This document explains whats in the store, how statuses transition, how spans are recorded, and the concurrency model (threads & processes).
The **[`LightningStore`][agentlightning.LightningStore]** is the central coordination point for Agent-lightning. It holds the task queue, rollouts, attempts, spans, and versioned resources, and exposes a small API both Runners and Algorithms use to communicate. This document explains what's in the store, how statuses transition, how spans are recorded, and the concurrency model (threads & processes).
## Whats in the Store?
## What's in the Store?
![Store Architecture](../assets/store-api-visualized.svg){ .center }
@@ -13,12 +13,11 @@ At a high level:
* **Attempts** Each rollout can have multiple executions (retries). Attempts track [`status`][agentlightning.Attempt.status], [`start_time`][agentlightning.Attempt.start_time], [`end_time`][agentlightning.Attempt.end_time], [`last_heartbeat_time`][agentlightning.Attempt.last_heartbeat_time] and link to spans. Valid [AttemptStatus][agentlightning.AttemptStatus] are `preparing`, `running`, `succeeded`, `failed`, `requeuing`, `cancelled`.
* **Spans** Structured trace events produced by the Tracer during an attempt. Spans are ordered by a **monotonic sequence id** per `(rollout_id, attempt_id)`.
* **Resources** Versioned, named bundles (e.g., prompt templates) referenced by rollouts.
* **Workers** Metadata about runner instances: heartbeat timestamps, current assignment, and status.
Rollout and Task share the same surface in practice: [`Rollout.input`][agentlightning.types.Rollout] is the task input. The queue stores rollouts that are not yet running; [Runners][agentlightning.Runner] dequeue them and update the same rollouts status as work progresses.
Rollout and Task share the same surface in practice: [`Rollout.input`][agentlightning.types.Rollout] is the task input. The queue stores rollouts that are not yet running; [Runners][agentlightning.Runner] dequeue them and update the same rollout's status as work progresses.
All [`LightningStore`][agentlightning.LightningStore] implementations must inherit from [`LightningStore`][agentlightning.LightningStore] and override the methods to implement the storage logic.
Before we look at status transitions, it helps to keep in mind that rollouts are the “outside view,” while attempts are the “inside view.” Attempts are what actually run; rollouts summarize the latest attempt plus a small set of control actions like queueing and cancellation.
Before we look at status transitions, it helps to keep in mind that rollouts are the "outside view," while attempts are the "inside view." Attempts are what actually run; rollouts summarize the latest attempt plus a small set of control actions like queueing and cancellation.
## Attempt Status Transitions
@@ -152,31 +151,220 @@ Programmatically this is encapsulated by [`Span.from_opentelemetry(readable_span
[`add_span`][agentlightning.LightningStore.add_span] or [`add_otel_span`][agentlightning.LightningStore.add_otel_span] both appends a span *and* acts as a heartbeat that can revive `unresponsive``running`.
## OTLP Compatibility
### OTLP Compatibility
Some of the LightningStore implementations support exporting traces via the [OTLP/HTTP specification](https://opentelemetry.io/docs/specs/otlp/). For example, [`LightningStoreServer`][agentlightning.LightningStoreServer] exposes `/v1/traces` endpoint, it implements the binary Protobuf variant defined by the spec, including the required `Content-Type: application/x-protobuf`, optional `Content-Encoding: gzip`, and status responses encoded as `google.rpc.Status`. Agent-lightning helps parsing `ExportTraceServiceRequest` messages, validate identifiers, normalize resource metadata, and allocate sequence
numbers so store implementations only need to persist [`Span`][agentlightning.Span] objects in order.
Some of the LightningStore implementations support exporting traces via the [OTLP/HTTP specification](https://opentelemetry.io/docs/specs/otlp/). For example, [`LightningStoreServer`][agentlightning.LightningStoreServer] exposes `/v1/traces` endpoint, it implements the binary Protobuf variant defined by the spec, including the required `Content-Type: application/x-protobuf`, optional `Content-Encoding: gzip`, and status responses encoded as `google.rpc.Status`. Agent-lightning helps parsing `ExportTraceServiceRequest` messages, validate identifiers, normalize resource metadata, and allocate sequence numbers so store implementations only need to persist [`Span`][agentlightning.Span] objects in order.
Because the interface speaks standard OTLP, any OpenTelemetry-compatible SDK or collector can emit spans directly to a LightningStore OTLP endpoint without custom shims. The server responds according to the OTLP contract (status code, encoding, and error payloads), which keeps Agent-lightning interoperable with existing observability tooling. This compatibility serves as a strong complement to the OpenTelemetry conversion discussed above.
## Store Implementations
Check whether the store supports OTLP traces via the [`capabilities["otlp_traces"]`][agentlightning.LightningStore.capabilities] property.
Currently, the only out-of-the-box implementation is [`InMemoryLightningStore`][agentlightning.InMemoryLightningStore]:
## Implementation Overview
- Fast startup, zero external dependencies, and ideal for local development, CI, and unit tests.
- Fully asyncio-safe for writes; most reader operations can iterate without locks, except those that need to perform multiple queries.
- Includes a best-effort span eviction policy once memory crosses a configured watermark; querying evicted spans raises a clear error so callers can fall back.
The `agentlightning.store` module is organized into two distinct layers plus optional wrappers:
For production you will likely want persistence. Were actively building a SQLite-backed store that keeps the same API surface while adding durability, crash recovery, and better historical span queries. If you need something sooner, implement your own store by subclassing [`LightningStore`][agentlightning.LightningStore] and providing concrete storage for the small set of abstract methods (`enqueue_rollout`, `dequeue_rollout`, `update_attempt`, `add_span`, etc.). This document plus the tests in `tests/store/` illustrate the expected behavior.
```mermaid
classDiagram
direction TB
Different store implementations may have different capabilities. For example, [`InMemoryLightningStore`][agentlightning.InMemoryLightningStore] does not support exporting traces via OTLP. Try to distinguish the capabilities of a store implementation by checking the [`capabilities`][agentlightning.LightningStore.capabilities] property.
class LightningStore {
<<abstract>>
+enqueue_rollout()
+dequeue_rollout()
+update_attempt()
+add_span()
+query_rollouts()
...
}
class LightningCollections {
<<abstract>>
+rollouts: Collection
+attempts: Collection
+spans: Collection
+resources: Collection
+workers: Collection
+rollout_queue: Queue
+span_sequence_ids: KeyValue
+atomic()
}
class CollectionBasedLightningStore~T~ {
+collections: T
-healthcheck_before()
-tracked()
}
class InMemoryLightningStore
class MongoLightningStore
class InMemoryLightningCollections
class MongoLightningCollections
class LightningStoreServer {
+store: LightningStore
+start()
+stop()
}
class LightningStoreClient {
+server_address: str
}
class LightningStoreThreaded {
+store: LightningStore
}
LightningStore <|-- CollectionBasedLightningStore
LightningStore <|-- LightningStoreServer
LightningStore <|-- LightningStoreClient
LightningStore <|-- LightningStoreThreaded
CollectionBasedLightningStore <|-- InMemoryLightningStore
CollectionBasedLightningStore <|-- MongoLightningStore
LightningCollections <|-- InMemoryLightningCollections
LightningCollections <|-- MongoLightningCollections
InMemoryLightningStore ..> InMemoryLightningCollections : uses
MongoLightningStore ..> MongoLightningCollections : uses
LightningStoreServer o-- LightningStore : wraps
LightningStoreThreaded o-- LightningStore : wraps
```
1. **Collections Layer** Low-level storage primitives ([`LightningCollections`][agentlightning.store.collection.LightningCollections]) providing CRUD operations via [`Collection`][agentlightning.store.collection.Collection], [`Queue`][agentlightning.store.collection.Queue], and [`KeyValue`][agentlightning.store.collection.KeyValue] interfaces. Each backend (in-memory, MongoDB) implements these primitives.
2. **Store Layer** All [`LightningStore`][agentlightning.LightningStore] implementations must inherit from [`LightningStore`][agentlightning.LightningStore] and override the methods to implement the storage logic. [`CollectionBasedLightningStore`][agentlightning.CollectionBasedLightningStore] builds on collections to implement the full [`LightningStore`][agentlightning.LightningStore] API, including business logic like status transitions, watchdog health checks, and retry policies.
3. **Wrappers** Cross-cutting concerns live in thin wrappers:
- [`LightningStoreThreaded`][agentlightning.LightningStoreThreaded] adds mutex-based thread safety.
- [`LightningStoreServer`][agentlightning.LightningStoreServer] / [`LightningStoreClient`][agentlightning.LightningStoreClient] enable multi-process access over HTTP.
## Collections
The collections layer provides storage primitives that [`CollectionBasedLightningStore`][agentlightning.CollectionBasedLightningStore] builds upon. This separation keeps business logic (status transitions, watchdog, retries) in the store layer while allowing different backends to focus purely on persistence.
The off-the-shelf implementations are [`InMemoryLightningCollections`][agentlightning.store.collection.InMemoryLightningCollections] and [`MongoLightningCollections`][agentlightning.store.collection.mongo.MongoLightningCollections], which are the underlying collections for [`InMemoryLightningStore`][agentlightning.InMemoryLightningStore] and [`MongoLightningStore`][agentlightning.store.mongo.MongoLightningStore], respectively.
### Collection Primitives
[`LightningCollections`][agentlightning.store.collection.LightningCollections] bundles three primitive types:
| Primitive | Purpose | Methods |
|-----------|---------|---------|
| [`Collection[T]`][agentlightning.store.collection.Collection] | Indexed storage with primary keys | [`query()`][agentlightning.store.collection.Collection.query], [`get()`][agentlightning.store.collection.Collection.get], [`insert()`][agentlightning.store.collection.Collection.insert], [`update()`][agentlightning.store.collection.Collection.update], [`upsert()`][agentlightning.store.collection.Collection.upsert], [`delete()`][agentlightning.store.collection.Collection.delete] |
| [`Queue[T]`][agentlightning.store.collection.Queue] | FIFO queue for task scheduling | [`enqueue()`][agentlightning.store.collection.Queue.enqueue], [`dequeue()`][agentlightning.store.collection.Queue.dequeue], [`peek()`][agentlightning.store.collection.Queue.peek], [`size()`][agentlightning.store.collection.Queue.size] |
| [`KeyValue[K, V]`][agentlightning.store.collection.KeyValue] | Simple key-value store | [`get()`][agentlightning.store.collection.KeyValue.get], [`set()`][agentlightning.store.collection.KeyValue.set], [`inc()`][agentlightning.store.collection.KeyValue.inc], [`chmax()`][agentlightning.store.collection.KeyValue.chmax], [`pop()`][agentlightning.store.collection.KeyValue.pop] |
Every [`LightningCollections`][agentlightning.store.collection.LightningCollections] instance exposes these named collections:
- `rollouts` [`Collection[Rollout]`][agentlightning.store.collection.Collection] keyed by `rollout_id`
- `attempts` [`Collection[Attempt]`][agentlightning.store.collection.Collection] keyed by `(rollout_id, attempt_id)`
- `spans` [`Collection[Span]`][agentlightning.store.collection.Collection] keyed by `(rollout_id, attempt_id, span_id)`
- `resources` [`Collection[ResourcesUpdate]`][agentlightning.store.collection.Collection] keyed by `resources_id`
- `workers` [`Collection[Worker]`][agentlightning.store.collection.Collection] keyed by `worker_id`
- `rollout_queue` [`Queue[str]`][agentlightning.store.collection.Queue] holding rollout IDs awaiting execution
- `span_sequence_ids` [`KeyValue[str, int]`][agentlightning.store.collection.KeyValue] tracking monotonic sequence counters
### Atomic Operations
Collections support atomic operations through the [`atomic()`][agentlightning.store.collection.LightningCollections.atomic] context manager:
```python
async with collections.atomic(mode="rw", labels=["rollouts", "attempts"]) as ctx:
rollout = await ctx.rollouts.get(filter={"rollout_id": {"exact": rollout_id}})
# modify and update within the same transaction
await ctx.rollouts.update([updated_rollout])
```
The arguments passed to [`atomic()`][agentlightning.store.collection.LightningCollections.atomic] are quite arbitrary and flexible. Different implementations may have different interpretations of the arguments. For example, to [`InMemoryLightningCollections`][agentlightning.store.collection.InMemoryLightningCollections], the `mode` parameter controls locking behavior (`"r"` for read-only, `"rw"` for read-write), while `labels` specifies which collections to lock. Acquiring locks in sorted order prevents deadlocks when multiple operations run concurrently.
### Implementing a Custom Backend
To add a new storage backend, implement [`LightningCollections`][agentlightning.store.collection.LightningCollections]:
```python
from agentlightning.store.collection import LightningCollections, Collection, Queue, KeyValue
class MyLightningCollections(LightningCollections):
@property
def rollouts(self) -> Collection[Rollout]:
return self._rollouts # your implementation
@property
def rollout_queue(self) -> Queue[str]:
return self._queue # your implementation
# ... implement remaining properties
async def atomic(self, *, mode, snapshot=False, labels=None, **kwargs):
# provide transaction / locking semantics
...
```
Then instantiate your store:
```python
from agentlightning.store.collection_based import CollectionBasedLightningStore
store = CollectionBasedLightningStore(collections=MyLightningCollections())
```
The store layer handles all business logic; your collections just need to provide correct CRUD semantics.
## Collection-based Store Implementations
Agent-lightning ships with two collection-based store implementations:
### InMemoryLightningStore
[`InMemoryLightningStore`][agentlightning.InMemoryLightningStore] uses [`InMemoryLightningCollections`][agentlightning.store.collection.InMemoryLightningCollections] backed by Python data structures. It supports **fast startup** with zero external dependencies—ideal for local development, CI, and unit tests. It also provides two lock modes, configurable between `"asyncio"` (single-thread, multiple coroutines) and `"thread"` (multi-threaded via [aiologic](https://github.com/x42005e1f/aiologic)).
[`InMemoryLightningCollections`][agentlightning.store.collection.InMemoryLightningCollections] use nested dictionaries for O(1) primary-key lookup and `deque` for the task queue.
### MongoLightningStore
[`MongoLightningStore`][agentlightning.store.mongo.MongoLightningStore] uses [`MongoLightningCollections`][agentlightning.store.collection.mongo.MongoLightningCollections] backed by MongoDB. It supports **persistent storage** suitable for production deployments and **multi-process safe** via database-level atomicity. It also supports **partition support** via `partition_id` for running multiple trainers against the same database.
```python
from agentlightning.store.mongo import MongoLightningStore
store = MongoLightningStore(
mongo_uri="mongodb://localhost:27017/?replicaSet=rs0",
database_name="agentlightning",
partition_id="trainer-1", # optional: isolate data per trainer
)
```
!!! note
[`MongoLightningStore`][agentlightning.store.mongo.MongoLightningStore] requires the `mongo` optional dependency. Install with `pip install agentlightning[mongo]`.
### Capabilities
[](){ #store-capabilities }
Different stores have different capabilities. Check the [`capabilities`][agentlightning.LightningStore.capabilities] property to understand what a store supports:
| Capability | Description | InMemory | Mongo | Server | Client |
|------------|-------------|----------|-------|--------|--------|
| `thread_safe` | Safe for concurrent access from multiple threads | configurable | ✓ | ✓ | ✓ |
| `async_safe` | Safe for concurrent access from multiple coroutines | ✓ | ✓ | ✓ | ✓ |
| `zero_copy` | Can be shared across processes without serialization | ✗ | ✓ | ✓ | ✓ |
| `otlp_traces` | Exposes an OTLP-compatible `/v1/traces` endpoint | ✗ | ✗ | ✓ | ✓ |
## Thread Safety
**[`LightningStoreThreaded`][agentlightning.LightningStoreThreaded]** is a subclass of [`LightningStore`][agentlightning.LightningStore] that wraps another underlying store to make a store instance safe for multi-threaded callers. It wraps every state-mutating call in a mutex. Specifically:
Thread safety can be achieved at different layers:
**At the collections layer**: [`InMemoryLightningCollections`][agentlightning.store.collection.InMemoryLightningCollections] accepts a `lock_type` parameter:
- `"asyncio"` Uses per-event-loop `asyncio.Lock` for single-threaded, multi-coroutine scenarios.
- `"thread"` Uses `aiologic.Lock` for true multi-threaded access.
**At the store layer**: [`LightningStoreThreaded`][agentlightning.LightningStoreThreaded] wraps any [`LightningStore`][agentlightning.LightningStore] to add mutex-based thread safety:
* Methods like [`start_rollout`][agentlightning.LightningStore.start_rollout], [`enqueue_rollout`][agentlightning.LightningStore.enqueue_rollout], [`update_attempt`][agentlightning.LightningStore.update_attempt], [`add_span`][agentlightning.LightningStore.add_span], etc. are guarded by a lock.
* Non-mutating, potentially blocking calls remain pass-through by design (e.g., [`wait_for_rollouts`][agentlightning.LightningStore.wait_for_rollouts]), as they dont modify shared state and should not hold the lock for long periods.
* Non-mutating, potentially blocking calls remain pass-through by design (e.g., [`wait_for_rollouts`][agentlightning.LightningStore.wait_for_rollouts]), as they don't modify shared state and should not hold the lock for long periods.
Database-based stores like [`MongoLightningStore`][agentlightning.store.mongo.MongoLightningStore] are inherently thread-safe through database atomicity guarantees.
## Process Safety and Client-server Store
@@ -188,7 +376,7 @@ Different store implementations may have different capabilities. For example, [`
The server tracks the creator PID. In the owner process it delegates directly to the in-memory store; in other processes it lazily constructs a [`LightningStoreClient`][agentlightning.LightningStoreClient] to talk to the HTTP API. This prevents accidental cross-process mutation of the wrong memory image. When the server is pickled (e.g., via `multiprocessing`), only the minimal fields are serialized, but **NOT** the FastAPI/uvicorn objects. Subprocesses wont accidentally carry live server state. Forked subprocess should also use [`LightningStoreClient`][agentlightning.LightningStoreClient] to communicate with the server in the main process.
On the client side, the client retries network/5xx failures using a small backoff, and probes `/health` between attempts. Application exceptions inside the server are wrapped as HTTP 400 with a traceback—these are **not retried**. The client also maintains a **per-event-loop** `aiohttp.ClientSession` map so that tracer callbacks (often on separate loops/threads) dont hang by reusing a session from another loop.
On the client side, the client retries network/5xx failures using a small backoff, and probes `/v1/agl/health` between attempts. Application exceptions inside the server are wrapped as HTTP 400 with a traceback—these are **not retried**. The client also maintains a **per-event-loop** `aiohttp.ClientSession` map so that tracer callbacks (often on separate loops/threads) dont hang by reusing a session from another loop.
Minimal lifecycle:
+8 -8
View File
@@ -30,6 +30,14 @@
[:octicons-repo-24: Browse source]({{ src("examples/calc_x") }})
- :material-chart-box:{ .lg .middle } __ChartQA vision-language RL__
---
LangGraph-powered workflow for answering chart questions end to end: rollout the multi-modality agent with GPT or vLLM, and train with VERL/GRPO plus self-refinement loops.
[:octicons-repo-24: Browse source]({{ src("examples/chartqa") }})
- :material-code-braces:{ .lg .middle } __Claude Code SWE-bench__
---
@@ -54,14 +62,6 @@
[:octicons-repo-24: Browse source]({{ src("examples/rag") }})
- :material-magnify:{ .lg .middle } __Search-R1 RL__
---
Reproduction of the Search-R1 workflow that prepares its own retrieval backend, runs the rollout script, and coordinates GRPO-style training without extra orchestration layers (last validated on v0.1.x).
[:octicons-repo-24: Browse source]({{ src("examples/search_r1") }})
- :material-database:{ .lg .middle } __Spider SQL agent__
---
+1 -1
View File
@@ -1,6 +1,6 @@
# Train SQL Agent with Agent-lightning and VERL
This walkthrough builds upon the **Agent-lightning v0.2 SQL Agent** example and explains how the system components integrate: a **LangGraph-based SQL agent** wrapped as a [`LitAgent`][agentlightning.LitAgent], the **[`VERL`][agentlightning.algorithm.verl.VERL] reinforcement learning (RL) algorithm**, and the **[`Trainer`][agentlightning.Trainer]**, which coordinates both training and debugging.
This walkthrough builds upon the **Agent-lightning SQL Agent** example and explains how the system components integrate: a **LangGraph-based SQL agent** wrapped as a [`LitAgent`][agentlightning.LitAgent], the **[`VERL`][agentlightning.algorithm.verl.VERL] reinforcement learning (RL) algorithm**, and the **[`Trainer`][agentlightning.Trainer]**, which coordinates both training and debugging.
The command-line interface in [`examples/spider/train_sql_agent.py`]({{ src("examples/spider/train_sql_agent.py") }}) provides a complete runnable example. However, this document focuses on understanding the underlying architecture so you can effectively adapt the workflow to your own agents.
+2
View File
@@ -35,6 +35,8 @@ This documentation is organized into the following parts:
- [DeepWerewolf](https://github.com/af-74413592/DeepWerewolf) — A case study of agent RL training for the Chinese Werewolf game built with AgentScope and Agent Lightning.
- [AgentFlow](https://agentflow.stanford.edu/) — A modular multi-agent framework that combines planner, executor, verifier, and generator agents with the Flow-GRPO algorithm to tackle long-horizon, sparse-reward tasks.
- [Youtu-Agent](https://github.com/TencentCloudADP/Youtu-agent) — Youtu-Agent lets you build and train your agent with ease. Built with [a modified branch](https://github.com/microsoft/agent-lightning/tree/contrib/youtu-agent-lightning) of Agent Lightning, Youtu-Agent has verified up to 128 GPUs RL training on maths/code and search capabilities with steady convergence. Also check [the recipe](https://github.com/TencentCloudADP/youtu-agent/tree/rl/agl) and their blog [*Stop Wrestling with Your Agent RL: How Youtu-Agent Achieved Stable, 128-GPU Scaling Without Breaking a Sweat*](https://spotted-coconut-df8.notion.site/Stop-Wrestling-with-Your-Agent-RL-How-Youtu-Agent-Achieved-Stable-128-GPU-Scaling-Without-Breaking-2ca5e8f089ba80539a98c582b65e0233).
## Citation
+46 -17
View File
@@ -1,7 +1,5 @@
# Command Line Interface
<!-- TODO: This document should be auto-generated. -->
!!! warning
This document is a work in progress and might not be updated with the latest changes.
@@ -14,17 +12,18 @@
## agl
```text
usage: agl [-h] {vllm,store,agentops}
usage: agl [-h] {vllm,store,prometheus,agentops}
Agent Lightning CLI entry point.
Available subcommands:
vllm Run the vLLM CLI with Agent Lightning instrumentation.
store Run a LightningStore server.
agentops Start the AgentOps server manager.
vllm Run the vLLM CLI with Agent Lightning instrumentation.
store Run a LightningStore server.
prometheus Serve Prometheus metrics from the multiprocess registry.
agentops Start the AgentOps server manager.
positional arguments:
{vllm,store,agentops}
{vllm,store,prometheus,agentops}
Subcommand to run.
options:
@@ -64,26 +63,56 @@ Agent-lightning's LightningStore CLI. Use it to start an independent LightningSt
Currently the store data are stored in memory and will be lost when the server is stopped.
```text
usage: agl store [-h] [--port PORT]
usage: agl store [-h] [--host HOST] [--port PORT] [--cors-origin CORS_ORIGINS] [--log-level {DEBUG,INFO,WARNING,ERROR}] [--tracker {prometheus,console} [{prometheus,console} ...]] [--n-workers N_WORKERS] [--backend {memory,mongo}]
[--mongo-uri MONGO_URI]
Run a LightningStore server
options:
-h, --help show this help message and exit
--port PORT Port to run the server on
-h, --help show this help message and exit
--host HOST Host to bind the server to
--port PORT Port to run the server on
--cors-origin CORS_ORIGINS
Allowed CORS origin. Repeat for multiple origins. Use '*' to allow all origins.
--log-level {DEBUG,INFO,WARNING,ERROR}
Configure the logging level for the store.
--tracker {prometheus,console} [{prometheus,console} ...]
Enable metrics tracking. Repeat for multiple trackers.
--n-workers N_WORKERS
Number of workers to run in the server. When it's greater than 1, the server will be run using `mp` launch mode. Only applicable for zero-copy stores such as MongoDB backend.
--backend {memory,mongo}
Backend to use for the store.
--mongo-uri MONGO_URI
MongoDB URI to use for the store. Applicable only if --backend is 'mongo'.
```
## agl agentops
!!! tip
Start a mock AgentOps server to bypass the online service of AgentOps.
After launching the store via CLI, you can tell the [`Trainer`][agentlightning.Trainer] to use the store by passing the store address to the trainer.
```python
store_client = agl.LightningStoreClient("http://localhost:4747")
trainer = agl.Trainer(store=store_client, ...)
```
See [using external store][debug-with-external-store] for more details.
## agl prometheus
Expose the Prometheus multiprocess registry on a dedicated FastAPI server. This is useful when the main LightningStore service is under heavy load; exporters can scrape this auxiliary endpoint instead.
```text
usage: agl agentops [-h] [--daemon] [--port PORT]
usage: agl prometheus [-h] [--host HOST] [--port PORT] [--metrics-path METRICS_PATH] [--log-level {DEBUG,INFO,WARNING,ERROR}] [--access-log]
Start AgentOps server
Serve Prometheus metrics outside the LightningStore server.
options:
-h, --help show this help message and exit
--daemon Run server as a daemon
--port PORT Port to run the server on
-h, --help show this help message and exit
--host HOST Host to bind the metrics server to.
--port PORT Port to expose the Prometheus metrics on.
--metrics-path METRICS_PATH
HTTP path used to expose metrics. Must start with '/' and not be the root path.
--log-level {DEBUG,INFO,WARNING,ERROR}
Configure the logging level for the metrics server.
--access-log Enable uvicorn access logs. Disabled by default to reduce noise.
```
-36
View File
@@ -54,42 +54,6 @@
::: agentlightning.tracer.otel.LightningSpanProcessor
## Utilities
::: agentlightning.utils.server_launcher.PythonServerLauncher
::: agentlightning.utils.server_launcher.PythonServerLauncherArgs
::: agentlightning.utils.server_launcher.LaunchMode
::: agentlightning.utils.otel.full_qualified_name
::: agentlightning.utils.otel.get_tracer_provider
::: agentlightning.utils.otel.get_tracer
::: agentlightning.utils.otel.make_tag_attributes
::: agentlightning.utils.otel.extract_tags_from_attributes
::: agentlightning.utils.otel.make_link_attributes
::: agentlightning.utils.otel.query_linked_spans
::: agentlightning.utils.otel.extract_links_from_attributes
::: agentlightning.utils.otel.filter_attributes
::: agentlightning.utils.otel.filter_and_unflatten_attributes
::: agentlightning.utils.otel.flatten_attributes
::: agentlightning.utils.otel.unflatten_attributes
::: agentlightning.utils.otlp.handle_otlp_export
::: agentlightning.utils.otlp.spans_from_proto
## Deprecated APIs
::: agentlightning.emitter.reward.reward
+2 -4
View File
@@ -1,10 +1,8 @@
# RESTful API References
!!! warning
!!! note
The following contents are still under construction.
## Store RESTful API
Shown in the following is the RESTful API for Lightning Store.
<div id="swagger-ui"></div>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
+12
View File
@@ -17,3 +17,15 @@
::: agentlightning.OtelTracer
::: agentlightning.Tracer
::: agentlightning.tracer.weave.WeaveTracer
::: agentlightning.DummyTracer
::: agentlightning.set_active_tracer
::: agentlightning.get_active_tracer
::: agentlightning.clear_active_tracer
::: agentlightning.tracer.weave.WeaveTracer
+3
View File
@@ -0,0 +1,3 @@
# Semantic Conventions
::: agentlightning.semconv

Some files were not shown because too many files have changed in this diff Show More