synth-ai 0.2.9.dev7__py3-none-any.whl → 0.2.9.dev8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of synth-ai might be problematic. Click here for more details.
- examples/__init__.py +16 -0
- examples/crafter_debug_render.py +8 -11
- examples/qwen_coder/README.md +102 -0
- examples/qwen_coder/_shared.py +113 -0
- examples/qwen_coder/configs/coder_lora_30b.toml +61 -0
- examples/qwen_coder/configs/coder_lora_4b.toml +57 -0
- examples/qwen_coder/configs/coder_lora_small.toml +58 -0
- examples/qwen_coder/generate_dataset.py +98 -0
- examples/qwen_coder/infer_ft_smoke.py +64 -0
- examples/qwen_coder/infer_prod_proxy.py +73 -0
- examples/qwen_coder/infer_via_synth.py +87 -0
- examples/qwen_coder/scripts/infer_coder.sh +18 -0
- examples/qwen_coder/scripts/train_coder_30b.sh +21 -0
- examples/qwen_coder/sft_full_17b.py +103 -0
- examples/qwen_coder/sft_lora_30b.py +110 -0
- examples/qwen_coder/subset_jsonl.py +38 -0
- examples/qwen_coder/validate_jsonl.py +59 -0
- examples/rl/run_eval.py +36 -37
- examples/rl/run_rl_and_save.py +5 -5
- examples/rl/task_app/math_single_step.py +65 -43
- examples/rl/task_app/math_task_app.py +3 -3
- examples/sft/README.md +139 -0
- examples/sft/configs/crafter_fft_qwen0p6b.toml +44 -0
- examples/sft/configs/crafter_lora_qwen0p6b.toml +45 -0
- examples/sft/evaluate.py +117 -0
- examples/sft/export_dataset.py +117 -0
- examples/sft/generate_traces.py +162 -0
- examples/swe/__init__.py +12 -0
- examples/swe/task_app/README.md +105 -0
- examples/swe/task_app/__init__.py +2 -0
- examples/swe/task_app/grpo_swe_mini.py +571 -0
- examples/swe/task_app/grpo_swe_mini_task_app.py +136 -0
- examples/swe/task_app/hosted/README.md +173 -0
- examples/swe/task_app/hosted/__init__.py +5 -0
- examples/swe/task_app/hosted/branching.py +143 -0
- examples/swe/task_app/hosted/environment_routes.py +1289 -0
- examples/swe/task_app/hosted/envs/__init__.py +1 -0
- examples/swe/task_app/hosted/envs/crafter/__init__.py +6 -0
- examples/swe/task_app/hosted/envs/crafter/app.py +1 -0
- examples/swe/task_app/hosted/envs/crafter/environment.py +522 -0
- examples/swe/task_app/hosted/envs/crafter/policy.py +478 -0
- examples/swe/task_app/hosted/envs/crafter/react_agent.py +108 -0
- examples/swe/task_app/hosted/envs/crafter/shared.py +305 -0
- examples/swe/task_app/hosted/envs/crafter/tools.py +47 -0
- examples/swe/task_app/hosted/envs/mini_swe/__init__.py +8 -0
- examples/swe/task_app/hosted/envs/mini_swe/environment.py +1164 -0
- examples/swe/task_app/hosted/envs/mini_swe/policy.py +355 -0
- examples/swe/task_app/hosted/envs/mini_swe/shared.py +83 -0
- examples/swe/task_app/hosted/envs/mini_swe/tools.py +96 -0
- examples/swe/task_app/hosted/hosted_app.py +204 -0
- examples/swe/task_app/hosted/inference/__init__.py +5 -0
- examples/swe/task_app/hosted/inference/openai_client.py +618 -0
- examples/swe/task_app/hosted/main.py +100 -0
- examples/swe/task_app/hosted/policy_routes.py +1079 -0
- examples/swe/task_app/hosted/registry.py +195 -0
- examples/swe/task_app/hosted/rollout.py +1869 -0
- examples/swe/task_app/hosted/storage/__init__.py +5 -0
- examples/swe/task_app/hosted/storage/volume.py +211 -0
- examples/swe/task_app/hosted/test_agents.py +161 -0
- examples/swe/task_app/hosted/test_service.py +137 -0
- examples/swe/task_app/hosted/utils.py +62 -0
- examples/vlm/README.md +68 -0
- examples/vlm/configs/crafter_vlm_gpt4o.toml +44 -0
- examples/vlm/crafter_image_only_agent.py +207 -0
- examples/vlm/crafter_openai_vlm_agent.py +277 -0
- examples/vlm/filter_image_rows.py +63 -0
- examples/vlm/run_crafter_vlm_benchmark.py +316 -0
- examples/warming_up_to_rl/analyze_trace_db.py +5 -5
- examples/warming_up_to_rl/configs/rl_from_base_qwen4b.toml +11 -1
- examples/warming_up_to_rl/export_trace_sft.py +78 -21
- examples/warming_up_to_rl/groq_test.py +4 -4
- examples/warming_up_to_rl/manage_secrets.py +13 -18
- examples/warming_up_to_rl/run_eval.py +42 -44
- examples/warming_up_to_rl/run_fft_and_save.py +11 -16
- examples/warming_up_to_rl/run_local_rollout.py +1 -3
- examples/warming_up_to_rl/run_local_rollout_modal.py +2 -4
- examples/warming_up_to_rl/run_local_rollout_parallel.py +1 -4
- examples/warming_up_to_rl/run_local_rollout_traced.py +3 -5
- examples/warming_up_to_rl/run_rl_and_save.py +5 -6
- examples/warming_up_to_rl/run_rollout_remote.py +8 -10
- examples/warming_up_to_rl/task_app/README.md +6 -2
- examples/warming_up_to_rl/task_app/grpo_crafter.py +234 -35
- examples/warming_up_to_rl/task_app/grpo_crafter_task_app.py +2 -3
- examples/warming_up_to_rl/task_app/synth_envs_hosted/__init__.py +1 -1
- examples/warming_up_to_rl/task_app/synth_envs_hosted/branching.py +9 -11
- examples/warming_up_to_rl/task_app/synth_envs_hosted/environment_routes.py +131 -114
- examples/warming_up_to_rl/task_app/synth_envs_hosted/envs/crafter/environment.py +101 -41
- examples/warming_up_to_rl/task_app/synth_envs_hosted/envs/crafter/policy.py +73 -51
- examples/warming_up_to_rl/task_app/synth_envs_hosted/envs/crafter/react_agent.py +14 -6
- examples/warming_up_to_rl/task_app/synth_envs_hosted/envs/crafter/shared.py +16 -16
- examples/warming_up_to_rl/task_app/synth_envs_hosted/hosted_app.py +32 -34
- examples/warming_up_to_rl/task_app/synth_envs_hosted/inference/openai_client.py +94 -31
- examples/warming_up_to_rl/task_app/synth_envs_hosted/main.py +0 -2
- examples/warming_up_to_rl/task_app/synth_envs_hosted/policy_routes.py +303 -203
- examples/warming_up_to_rl/task_app/synth_envs_hosted/registry.py +21 -23
- examples/warming_up_to_rl/task_app/synth_envs_hosted/rollout.py +328 -225
- examples/warming_up_to_rl/task_app/synth_envs_hosted/storage/volume.py +13 -13
- examples/warming_up_to_rl/task_app/synth_envs_hosted/test_agents.py +1 -0
- examples/warming_up_to_rl/task_app/synth_envs_hosted/test_service.py +1 -0
- examples/warming_up_to_rl/task_app/synth_envs_hosted/utils.py +4 -3
- synth/__init__.py +14 -0
- synth_ai/__init__.py +26 -4
- synth_ai/api/models/supported.py +376 -0
- synth_ai/api/train/builders.py +128 -21
- synth_ai/api/train/cli.py +80 -64
- synth_ai/api/train/config_finder.py +7 -2
- synth_ai/api/train/env_resolver.py +1 -1
- synth_ai/api/train/pollers.py +2 -1
- synth_ai/api/train/supported_algos.py +139 -0
- synth_ai/api/train/task_app.py +1 -2
- synth_ai/api/train/utils.py +13 -44
- synth_ai/cli/__init__.py +8 -0
- synth_ai/cli/_modal_wrapper.py +28 -0
- synth_ai/cli/_typer_patch.py +49 -0
- synth_ai/cli/balance.py +1 -2
- synth_ai/cli/calc.py +1 -1
- synth_ai/cli/demo.py +2 -1
- synth_ai/cli/recent.py +2 -2
- synth_ai/cli/rl_demo.py +2 -1
- synth_ai/cli/root.py +11 -13
- synth_ai/cli/status.py +2 -2
- synth_ai/cli/task_apps.py +529 -179
- synth_ai/cli/traces.py +6 -4
- synth_ai/cli/watch.py +12 -18
- synth_ai/demo_registry.py +1 -1
- synth_ai/demos/core/cli.py +36 -43
- synth_ai/demos/demo_task_apps/__init__.py +3 -3
- synth_ai/demos/demo_task_apps/core.py +17 -25
- synth_ai/demos/demo_task_apps/crafter/grpo_crafter_task_app.py +3 -4
- synth_ai/demos/demo_task_apps/math/app.py +2 -1
- synth_ai/demos/demo_task_apps/math/deploy_modal.py +3 -4
- synth_ai/demos/demo_task_apps/math/modal_task_app.py +16 -18
- synth_ai/demos/demo_task_apps/math/task_app_entry.py +0 -1
- synth_ai/environments/examples/crafter_classic/environment.py +76 -1
- synth_ai/environments/reproducibility/tree.py +2 -5
- synth_ai/environments/service/app.py +11 -12
- synth_ai/environments/service/core_routes.py +4 -7
- synth_ai/environments/stateful/engine.py +1 -1
- synth_ai/environments/tasks/core.py +1 -0
- synth_ai/environments/tasks/filters.py +5 -6
- synth_ai/environments/tasks/utils.py +4 -5
- synth_ai/handshake.py +9 -9
- synth_ai/http.py +1 -1
- synth_ai/http_client.py +18 -10
- synth_ai/inference/client.py +15 -5
- synth_ai/jobs/client.py +78 -83
- synth_ai/learning/__init__.py +41 -6
- synth_ai/learning/algorithms.py +14 -0
- synth_ai/learning/client.py +91 -24
- synth_ai/learning/config.py +2 -38
- synth_ai/learning/ft_client.py +4 -59
- synth_ai/learning/health.py +5 -6
- synth_ai/learning/jobs.py +31 -47
- synth_ai/{rl → learning/rl}/__init__.py +14 -4
- synth_ai/learning/rl/client.py +267 -0
- synth_ai/learning/rl/config.py +31 -0
- synth_ai/{rl → learning/rl}/contracts.py +5 -8
- synth_ai/{rl → learning/rl}/env_keys.py +39 -15
- synth_ai/learning/rl/secrets.py +13 -0
- synth_ai/learning/rl_client.py +2 -281
- synth_ai/learning/sft/__init__.py +29 -0
- synth_ai/learning/sft/client.py +68 -0
- synth_ai/learning/sft/config.py +270 -0
- synth_ai/learning/sft/data.py +295 -0
- synth_ai/learning/sse.py +25 -24
- synth_ai/learning/validators.py +25 -28
- synth_ai/lm/__init__.py +21 -47
- synth_ai/main.py +4 -0
- synth_ai/task/__init__.py +25 -27
- synth_ai/task/apps/__init__.py +7 -8
- synth_ai/task/auth.py +8 -8
- synth_ai/task/client.py +14 -14
- synth_ai/task/contracts.py +36 -35
- synth_ai/task/datasets.py +6 -5
- synth_ai/task/errors.py +10 -10
- synth_ai/task/health.py +17 -9
- synth_ai/task/json.py +58 -23
- synth_ai/task/proxy.py +13 -9
- synth_ai/task/rubrics.py +16 -15
- synth_ai/task/server.py +12 -12
- synth_ai/task/tracing_utils.py +4 -4
- synth_ai/task/vendors.py +5 -6
- synth_ai/tracing_v3/__init__.py +2 -0
- synth_ai/tracing_v3/abstractions.py +21 -4
- synth_ai/tracing_v3/decorators.py +18 -16
- synth_ai/tracing_v3/hooks.py +5 -5
- synth_ai/tracing_v3/llm_call_record_helpers.py +6 -6
- synth_ai/tracing_v3/session_tracer.py +40 -14
- synth_ai/tracing_v3/storage/base.py +85 -0
- synth_ai/tracing_v3/storage/config.py +21 -8
- synth_ai/tracing_v3/storage/factory.py +10 -7
- synth_ai/tracing_v3/storage/utils.py +4 -2
- synth_ai/tracing_v3/turso/daemon.py +7 -2
- synth_ai/tracing_v3/turso/models.py +2 -2
- synth_ai/tracing_v3/turso/native_manager.py +1173 -0
- synth_ai/tracing_v3/utils.py +4 -4
- synth_ai/v0/api/__init__.py +8 -0
- synth_ai/v0/api/models/__init__.py +8 -0
- synth_ai/v0/api/models/supported.py +8 -0
- synth_ai/v0/config/__init__.py +15 -0
- synth_ai/v0/config/base_url.py +12 -0
- synth_ai/v0/lm/__init__.py +51 -0
- synth_ai/{lm → v0/lm}/caching/ephemeral.py +2 -2
- synth_ai/{lm → v0/lm}/caching/handler.py +4 -4
- synth_ai/{lm → v0/lm}/caching/initialize.py +1 -1
- synth_ai/{lm → v0/lm}/caching/persistent.py +1 -1
- synth_ai/{lm → v0/lm}/config.py +6 -1
- synth_ai/{lm → v0/lm}/core/all.py +9 -9
- synth_ai/{lm → v0/lm}/core/main.py +6 -6
- synth_ai/{lm → v0/lm}/core/main_v3.py +10 -10
- synth_ai/{lm → v0/lm}/core/synth_models.py +2 -14
- synth_ai/{lm → v0/lm}/core/vendor_clients.py +2 -2
- synth_ai/{lm → v0/lm}/overrides.py +2 -2
- synth_ai/{lm → v0/lm}/provider_support/anthropic.py +4 -4
- synth_ai/{lm → v0/lm}/provider_support/openai.py +5 -5
- synth_ai/{lm → v0/lm}/structured_outputs/handler.py +5 -5
- synth_ai/{lm → v0/lm}/structured_outputs/rehabilitate.py +1 -1
- synth_ai/{lm → v0/lm}/vendors/core/anthropic_api.py +9 -9
- synth_ai/{lm → v0/lm}/vendors/core/gemini_api.py +5 -5
- synth_ai/{lm → v0/lm}/vendors/core/mistral_api.py +5 -5
- synth_ai/{lm → v0/lm}/vendors/core/openai_api.py +10 -10
- synth_ai/{lm → v0/lm}/vendors/openai_standard.py +8 -8
- synth_ai/{lm → v0/lm}/vendors/openai_standard_responses.py +2 -2
- synth_ai/{lm → v0/lm}/vendors/supported/custom_endpoint.py +3 -3
- synth_ai/{lm → v0/lm}/vendors/supported/deepseek.py +2 -2
- synth_ai/{lm → v0/lm}/vendors/supported/grok.py +2 -2
- synth_ai/{lm → v0/lm}/vendors/supported/groq.py +1 -1
- synth_ai/{lm → v0/lm}/vendors/supported/ollama.py +1 -1
- synth_ai/{lm → v0/lm}/vendors/supported/openrouter.py +3 -3
- synth_ai/{lm → v0/lm}/vendors/supported/together.py +1 -1
- synth_ai/{lm → v0/lm}/vendors/synth_client.py +1 -1
- synth_ai/v0/tracing_v3/__init__.py +10 -0
- synth_ai/v0/tracing_v3/abstractions.py +3 -0
- synth_ai/v0/tracing_v3/decorators.py +3 -0
- synth_ai/v0/tracing_v3/llm_call_record_helpers.py +3 -0
- synth_ai/v0/tracing_v3/session_tracer.py +3 -0
- synth_ai-0.2.9.dev8.dist-info/METADATA +191 -0
- {synth_ai-0.2.9.dev7.dist-info → synth_ai-0.2.9.dev8.dist-info}/RECORD +268 -238
- {synth_ai-0.2.9.dev7.dist-info → synth_ai-0.2.9.dev8.dist-info}/top_level.txt +1 -0
- examples/common_old/backend.py +0 -20
- examples/evals_old/README.md +0 -98
- examples/evals_old/__init__.py +0 -6
- examples/evals_old/compare_models.py +0 -1038
- examples/evals_old/example_log.md +0 -145
- examples/evals_old/run_demo.sh +0 -126
- examples/evals_old/trace_analysis.py +0 -270
- examples/finetuning_old/_backup_synth_qwen/config.toml +0 -29
- examples/finetuning_old/_backup_synth_qwen/example_log.md +0 -324
- examples/finetuning_old/_backup_synth_qwen/filter_traces.py +0 -60
- examples/finetuning_old/_backup_synth_qwen/filter_traces_achievements.py +0 -243
- examples/finetuning_old/_backup_synth_qwen/purge_v3_traces.py +0 -109
- examples/finetuning_old/_backup_synth_qwen/react_agent_lm.py +0 -1924
- examples/finetuning_old/_backup_synth_qwen/readme.md +0 -49
- examples/finetuning_old/_backup_synth_qwen/run_crafter_qwen4b.py +0 -114
- examples/finetuning_old/_backup_synth_qwen/run_demo.sh +0 -195
- examples/finetuning_old/_backup_synth_qwen/sft_kickoff.py +0 -119
- examples/finetuning_old/synth_qwen_v1/README.md +0 -68
- examples/finetuning_old/synth_qwen_v1/filter_traces.py +0 -60
- examples/finetuning_old/synth_qwen_v1/filter_traces_achievements.py +0 -243
- examples/finetuning_old/synth_qwen_v1/finetune.py +0 -46
- examples/finetuning_old/synth_qwen_v1/hello_ft_model.py +0 -71
- examples/finetuning_old/synth_qwen_v1/infer.py +0 -36
- examples/finetuning_old/synth_qwen_v1/poll.py +0 -46
- examples/finetuning_old/synth_qwen_v1/prepare_data.py +0 -35
- examples/finetuning_old/synth_qwen_v1/purge_v3_traces.py +0 -109
- examples/finetuning_old/synth_qwen_v1/react_agent_lm.py +0 -1933
- examples/finetuning_old/synth_qwen_v1/run_crafter_sft_job.py +0 -210
- examples/finetuning_old/synth_qwen_v1/run_ft_job.py +0 -237
- examples/finetuning_old/synth_qwen_v1/upload_data.py +0 -34
- examples/finetuning_old/synth_qwen_v1/util.py +0 -152
- examples/rl_old/task_app.py +0 -1131
- examples/warming_up_to_rl/old/event_rewards.md +0 -234
- examples/warming_up_to_rl/old/notes.md +0 -73
- synth_ai/environments/examples/crafter_classic/agent_demos/crafter_modal_ft/filter_traces_sft_turso.py +0 -738
- synth_ai/environments/examples/crafter_classic/agent_demos/crafter_openai_ft/filter_traces_sft_turso.py +0 -580
- synth_ai/experimental/synth_oss.py +0 -445
- synth_ai/learning/filtering.py +0 -0
- synth_ai/learning/offline/dpo.py +0 -0
- synth_ai/learning/offline/providers.py +0 -7
- synth_ai/learning/offline/sft.py +0 -0
- synth_ai/learning/offline/shared.py +0 -0
- synth_ai/learning/online/grpo.py +0 -0
- synth_ai/learning/online/irft.py +0 -0
- synth_ai/learning/prompts/banking77_injection_eval.py +0 -168
- synth_ai/learning/prompts/gepa.py +0 -0
- synth_ai/learning/prompts/hello_world_in_context_injection_ex.py +0 -211
- synth_ai/learning/prompts/mipro.py +0 -289
- synth_ai/learning/prompts/random_search.py +0 -249
- synth_ai/learning/prompts/run_mipro_banking77.py +0 -172
- synth_ai/learning/prompts/run_random_search_banking77.py +0 -329
- synth_ai/rl/secrets.py +0 -19
- synth_ai/scripts/verify_rewards.py +0 -100
- synth_ai/tracing/__init__.py +0 -30
- synth_ai/tracing_v1/__init__.py +0 -33
- synth_ai/tracing_v3/turso/__init__.py +0 -25
- synth_ai/tracing_v3/turso/manager.py +0 -838
- synth_ai/zyk/__init__.py +0 -30
- synth_ai-0.2.9.dev7.dist-info/METADATA +0 -131
- /synth_ai/{lm → v0/lm}/caching/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/caching/constants.py +0 -0
- /synth_ai/{lm → v0/lm}/caching/dbs.py +0 -0
- /synth_ai/{lm → v0/lm}/constants.py +0 -0
- /synth_ai/{lm → v0/lm}/core/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/core/exceptions.py +0 -0
- /synth_ai/{lm → v0/lm}/cost/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/cost/monitor.py +0 -0
- /synth_ai/{lm → v0/lm}/cost/statefulness.py +0 -0
- /synth_ai/{lm → v0/lm}/injection.py +0 -0
- /synth_ai/{lm → v0/lm}/provider_support/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/provider_support/suppress_logging.py +0 -0
- /synth_ai/{lm → v0/lm}/structured_outputs/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/structured_outputs/inject.py +0 -0
- /synth_ai/{lm → v0/lm}/tools/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/tools/base.py +0 -0
- /synth_ai/{lm → v0/lm}/unified_interface.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/base.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/core/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/core/synth_dev_api.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/local/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/local/ollama.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/retries.py +0 -0
- /synth_ai/{lm → v0/lm}/vendors/supported/__init__.py +0 -0
- /synth_ai/{lm → v0/lm}/warmup.py +0 -0
- {synth_ai-0.2.9.dev7.dist-info → synth_ai-0.2.9.dev8.dist-info}/WHEEL +0 -0
- {synth_ai-0.2.9.dev7.dist-info → synth_ai-0.2.9.dev8.dist-info}/entry_points.txt +0 -0
- {synth_ai-0.2.9.dev7.dist-info → synth_ai-0.2.9.dev8.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,1173 @@
|
|
|
1
|
+
"""LibSQL-native trace manager prototype.
|
|
2
|
+
|
|
3
|
+
This module provides the Turso/libsql-backed trace storage implementation. It
|
|
4
|
+
mirrors the public surface area of the historical SQLAlchemy manager while
|
|
5
|
+
executing all operations directly via libsql.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
11
|
+
import json
|
|
12
|
+
import logging
|
|
13
|
+
import re
|
|
14
|
+
from dataclasses import asdict, dataclass
|
|
15
|
+
from datetime import UTC, datetime
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
import libsql
|
|
19
|
+
from sqlalchemy.engine import make_url
|
|
20
|
+
|
|
21
|
+
try: # pragma: no cover - exercised only when pandas present
|
|
22
|
+
import pandas as pd # type: ignore
|
|
23
|
+
except Exception: # pragma: no cover
|
|
24
|
+
pd = None # type: ignore[assignment]
|
|
25
|
+
|
|
26
|
+
from ..abstractions import (
|
|
27
|
+
EnvironmentEvent,
|
|
28
|
+
LMCAISEvent,
|
|
29
|
+
RuntimeEvent,
|
|
30
|
+
SessionMessageContent,
|
|
31
|
+
SessionTrace,
|
|
32
|
+
)
|
|
33
|
+
from ..config import CONFIG
|
|
34
|
+
from ..storage.base import TraceStorage
|
|
35
|
+
from .models import analytics_views
|
|
36
|
+
|
|
37
|
+
logger = logging.getLogger(__name__)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(slots=True)
|
|
41
|
+
class _ConnectionTarget:
|
|
42
|
+
"""Resolved connection target for libsql."""
|
|
43
|
+
|
|
44
|
+
database: str
|
|
45
|
+
sync_url: str | None = None
|
|
46
|
+
auth_token: str | None = None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _resolve_connection_target(db_url: str | None, auth_token: str | None) -> _ConnectionTarget:
|
|
50
|
+
"""Normalise the configured database URL."""
|
|
51
|
+
url = db_url or CONFIG.db_url
|
|
52
|
+
|
|
53
|
+
# Fast-path local SQLite URLs (`sqlite+aiosqlite:///path/to/db`)
|
|
54
|
+
if url.startswith("sqlite+aiosqlite:///"):
|
|
55
|
+
return _ConnectionTarget(database=url.replace("sqlite+aiosqlite:///", ""), auth_token=auth_token)
|
|
56
|
+
|
|
57
|
+
# SQLAlchemy-compatible libsql scheme (`sqlite+libsql://<endpoint or path>`)
|
|
58
|
+
if url.startswith("sqlite+libsql://"):
|
|
59
|
+
target = url.replace("sqlite+libsql://", "", 1)
|
|
60
|
+
return _ConnectionTarget(database=target, sync_url=target if target.startswith("libsql://") else None, auth_token=auth_token)
|
|
61
|
+
|
|
62
|
+
# Native libsql URLs (`libsql://...`).
|
|
63
|
+
if url.startswith("libsql://"):
|
|
64
|
+
return _ConnectionTarget(database=url, sync_url=url, auth_token=auth_token)
|
|
65
|
+
|
|
66
|
+
# Fallback to SQLAlchemy URL parsing for anything else we missed.
|
|
67
|
+
try:
|
|
68
|
+
parsed = make_url(url)
|
|
69
|
+
if parsed.drivername.startswith("sqlite"):
|
|
70
|
+
if parsed.database:
|
|
71
|
+
return _ConnectionTarget(database=parsed.database, auth_token=auth_token)
|
|
72
|
+
if parsed.drivername.startswith("libsql"):
|
|
73
|
+
database = parsed.render_as_string(hide_password=False)
|
|
74
|
+
return _ConnectionTarget(database=database, sync_url=database, auth_token=auth_token)
|
|
75
|
+
except Exception: # pragma: no cover - defensive guardrail
|
|
76
|
+
logger.debug("Unable to parse db_url via SQLAlchemy", exc_info=True)
|
|
77
|
+
|
|
78
|
+
# As a last resort use the raw value (libsql.connect can handle absolute paths).
|
|
79
|
+
return _ConnectionTarget(database=url, auth_token=auth_token)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _json_dumps(value: Any) -> str | None:
|
|
83
|
+
"""Serialise Python objects as JSON compatible with the existing schema."""
|
|
84
|
+
|
|
85
|
+
def _default(obj: Any):
|
|
86
|
+
if isinstance(obj, datetime):
|
|
87
|
+
return obj.isoformat()
|
|
88
|
+
return str(obj)
|
|
89
|
+
|
|
90
|
+
if value is None:
|
|
91
|
+
return None
|
|
92
|
+
return json.dumps(value, separators=(",", ":"), default=_default)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _maybe_datetime(value: Any) -> Any:
|
|
96
|
+
if value is None or isinstance(value, datetime):
|
|
97
|
+
return value
|
|
98
|
+
if isinstance(value, str):
|
|
99
|
+
try:
|
|
100
|
+
return datetime.fromisoformat(value)
|
|
101
|
+
except ValueError:
|
|
102
|
+
pass
|
|
103
|
+
return value
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _load_json(value: Any) -> Any:
|
|
107
|
+
if value is None or isinstance(value, (dict, list)):
|
|
108
|
+
return value or {}
|
|
109
|
+
if isinstance(value, str):
|
|
110
|
+
try:
|
|
111
|
+
return json.loads(value)
|
|
112
|
+
except (TypeError, ValueError):
|
|
113
|
+
return {}
|
|
114
|
+
return value
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
_TABLE_DEFINITIONS: tuple[str, ...] = (
|
|
118
|
+
"""
|
|
119
|
+
CREATE TABLE IF NOT EXISTS experiments (
|
|
120
|
+
experiment_id VARCHAR PRIMARY KEY,
|
|
121
|
+
name VARCHAR NOT NULL,
|
|
122
|
+
description TEXT,
|
|
123
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
124
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
125
|
+
configuration TEXT,
|
|
126
|
+
metadata TEXT
|
|
127
|
+
)
|
|
128
|
+
""",
|
|
129
|
+
"""
|
|
130
|
+
CREATE TABLE IF NOT EXISTS systems (
|
|
131
|
+
system_id VARCHAR PRIMARY KEY,
|
|
132
|
+
name VARCHAR NOT NULL,
|
|
133
|
+
system_type VARCHAR,
|
|
134
|
+
description TEXT,
|
|
135
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
136
|
+
metadata TEXT
|
|
137
|
+
)
|
|
138
|
+
""",
|
|
139
|
+
"""
|
|
140
|
+
CREATE TABLE IF NOT EXISTS system_versions (
|
|
141
|
+
version_id VARCHAR PRIMARY KEY,
|
|
142
|
+
system_id VARCHAR NOT NULL,
|
|
143
|
+
version_number VARCHAR NOT NULL,
|
|
144
|
+
commit_hash VARCHAR,
|
|
145
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
146
|
+
configuration TEXT,
|
|
147
|
+
metadata TEXT,
|
|
148
|
+
FOREIGN KEY(system_id) REFERENCES systems(system_id),
|
|
149
|
+
UNIQUE(system_id, version_number)
|
|
150
|
+
)
|
|
151
|
+
""",
|
|
152
|
+
"""
|
|
153
|
+
CREATE TABLE IF NOT EXISTS experimental_systems (
|
|
154
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
155
|
+
experiment_id VARCHAR NOT NULL,
|
|
156
|
+
system_id VARCHAR NOT NULL,
|
|
157
|
+
version_id VARCHAR NOT NULL,
|
|
158
|
+
FOREIGN KEY(experiment_id) REFERENCES experiments(experiment_id),
|
|
159
|
+
FOREIGN KEY(system_id) REFERENCES systems(system_id),
|
|
160
|
+
FOREIGN KEY(version_id) REFERENCES system_versions(version_id)
|
|
161
|
+
)
|
|
162
|
+
""",
|
|
163
|
+
"""
|
|
164
|
+
CREATE TABLE IF NOT EXISTS session_traces (
|
|
165
|
+
session_id VARCHAR PRIMARY KEY,
|
|
166
|
+
created_at DATETIME NOT NULL,
|
|
167
|
+
num_timesteps INTEGER NOT NULL,
|
|
168
|
+
num_events INTEGER NOT NULL,
|
|
169
|
+
num_messages INTEGER NOT NULL,
|
|
170
|
+
metadata TEXT,
|
|
171
|
+
experiment_id VARCHAR,
|
|
172
|
+
embedding VECTOR,
|
|
173
|
+
FOREIGN KEY(experiment_id) REFERENCES experiments(experiment_id)
|
|
174
|
+
)
|
|
175
|
+
""",
|
|
176
|
+
"""
|
|
177
|
+
CREATE TABLE IF NOT EXISTS session_timesteps (
|
|
178
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
179
|
+
session_id VARCHAR NOT NULL,
|
|
180
|
+
step_id VARCHAR NOT NULL,
|
|
181
|
+
step_index INTEGER NOT NULL,
|
|
182
|
+
turn_number INTEGER,
|
|
183
|
+
started_at DATETIME,
|
|
184
|
+
completed_at DATETIME,
|
|
185
|
+
num_events INTEGER,
|
|
186
|
+
num_messages INTEGER,
|
|
187
|
+
step_metadata TEXT,
|
|
188
|
+
UNIQUE(session_id, step_id),
|
|
189
|
+
FOREIGN KEY(session_id) REFERENCES session_traces(session_id)
|
|
190
|
+
)
|
|
191
|
+
""",
|
|
192
|
+
"""
|
|
193
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
194
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
195
|
+
session_id VARCHAR NOT NULL,
|
|
196
|
+
timestep_id INTEGER,
|
|
197
|
+
event_type VARCHAR NOT NULL,
|
|
198
|
+
system_instance_id VARCHAR,
|
|
199
|
+
event_time FLOAT,
|
|
200
|
+
message_time INTEGER,
|
|
201
|
+
created_at DATETIME,
|
|
202
|
+
model_name VARCHAR,
|
|
203
|
+
provider VARCHAR,
|
|
204
|
+
input_tokens INTEGER,
|
|
205
|
+
output_tokens INTEGER,
|
|
206
|
+
total_tokens INTEGER,
|
|
207
|
+
cost_usd INTEGER,
|
|
208
|
+
latency_ms INTEGER,
|
|
209
|
+
span_id VARCHAR,
|
|
210
|
+
trace_id VARCHAR,
|
|
211
|
+
call_records TEXT,
|
|
212
|
+
reward FLOAT,
|
|
213
|
+
terminated BOOLEAN,
|
|
214
|
+
truncated BOOLEAN,
|
|
215
|
+
system_state_before TEXT,
|
|
216
|
+
system_state_after TEXT,
|
|
217
|
+
metadata TEXT,
|
|
218
|
+
event_metadata TEXT,
|
|
219
|
+
embedding VECTOR,
|
|
220
|
+
CHECK (event_type IN ('cais', 'environment', 'runtime')),
|
|
221
|
+
FOREIGN KEY(session_id) REFERENCES session_traces(session_id),
|
|
222
|
+
FOREIGN KEY(timestep_id) REFERENCES session_timesteps(id)
|
|
223
|
+
)
|
|
224
|
+
""",
|
|
225
|
+
"""
|
|
226
|
+
CREATE TABLE IF NOT EXISTS messages (
|
|
227
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
228
|
+
session_id VARCHAR NOT NULL,
|
|
229
|
+
timestep_id INTEGER,
|
|
230
|
+
message_type VARCHAR NOT NULL,
|
|
231
|
+
content TEXT NOT NULL,
|
|
232
|
+
timestamp DATETIME,
|
|
233
|
+
event_time FLOAT,
|
|
234
|
+
message_time INTEGER,
|
|
235
|
+
metadata TEXT,
|
|
236
|
+
embedding VECTOR,
|
|
237
|
+
CHECK (message_type IN ('user', 'assistant', 'system', 'tool_use', 'tool_result')),
|
|
238
|
+
FOREIGN KEY(session_id) REFERENCES session_traces(session_id),
|
|
239
|
+
FOREIGN KEY(timestep_id) REFERENCES session_timesteps(id)
|
|
240
|
+
)
|
|
241
|
+
""",
|
|
242
|
+
"""
|
|
243
|
+
CREATE TABLE IF NOT EXISTS outcome_rewards (
|
|
244
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
245
|
+
session_id VARCHAR NOT NULL,
|
|
246
|
+
total_reward INTEGER NOT NULL,
|
|
247
|
+
achievements_count INTEGER NOT NULL,
|
|
248
|
+
total_steps INTEGER NOT NULL,
|
|
249
|
+
created_at DATETIME NOT NULL,
|
|
250
|
+
reward_metadata TEXT,
|
|
251
|
+
FOREIGN KEY(session_id) REFERENCES session_traces(session_id)
|
|
252
|
+
)
|
|
253
|
+
""",
|
|
254
|
+
"""
|
|
255
|
+
CREATE TABLE IF NOT EXISTS event_rewards (
|
|
256
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
257
|
+
event_id INTEGER NOT NULL,
|
|
258
|
+
session_id VARCHAR NOT NULL,
|
|
259
|
+
message_id INTEGER,
|
|
260
|
+
turn_number INTEGER,
|
|
261
|
+
reward_value FLOAT NOT NULL,
|
|
262
|
+
reward_type VARCHAR,
|
|
263
|
+
"key" VARCHAR,
|
|
264
|
+
annotation TEXT,
|
|
265
|
+
source VARCHAR,
|
|
266
|
+
created_at DATETIME NOT NULL,
|
|
267
|
+
FOREIGN KEY(event_id) REFERENCES events(id),
|
|
268
|
+
FOREIGN KEY(session_id) REFERENCES session_traces(session_id),
|
|
269
|
+
FOREIGN KEY(message_id) REFERENCES messages(id)
|
|
270
|
+
)
|
|
271
|
+
"""
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
_INDEX_DEFINITIONS: tuple[str, ...] = (
|
|
276
|
+
"CREATE INDEX IF NOT EXISTS idx_session_created ON session_traces (created_at)",
|
|
277
|
+
"CREATE INDEX IF NOT EXISTS idx_session_experiment ON session_traces (experiment_id)",
|
|
278
|
+
"CREATE INDEX IF NOT EXISTS idx_timestep_session_step ON session_timesteps (session_id, step_id)",
|
|
279
|
+
"CREATE INDEX IF NOT EXISTS idx_timestep_turn ON session_timesteps (turn_number)",
|
|
280
|
+
"CREATE INDEX IF NOT EXISTS idx_event_session_step ON events (session_id, timestep_id)",
|
|
281
|
+
"CREATE INDEX IF NOT EXISTS idx_event_type ON events (event_type)",
|
|
282
|
+
"CREATE INDEX IF NOT EXISTS idx_event_created ON events (created_at)",
|
|
283
|
+
"CREATE INDEX IF NOT EXISTS idx_event_model ON events (model_name)",
|
|
284
|
+
"CREATE INDEX IF NOT EXISTS idx_event_trace ON events (trace_id)",
|
|
285
|
+
"CREATE INDEX IF NOT EXISTS idx_message_session_step ON messages (session_id, timestep_id)",
|
|
286
|
+
"CREATE INDEX IF NOT EXISTS idx_message_type ON messages (message_type)",
|
|
287
|
+
"CREATE INDEX IF NOT EXISTS idx_message_timestamp ON messages (timestamp)",
|
|
288
|
+
"CREATE INDEX IF NOT EXISTS idx_experiment_created ON experiments (created_at)",
|
|
289
|
+
"CREATE INDEX IF NOT EXISTS idx_experiment_name ON experiments (name)",
|
|
290
|
+
"CREATE INDEX IF NOT EXISTS idx_system_name ON systems (name)",
|
|
291
|
+
"CREATE INDEX IF NOT EXISTS idx_system_type ON systems (system_type)",
|
|
292
|
+
"CREATE UNIQUE INDEX IF NOT EXISTS uq_system_version ON system_versions (system_id, version_number)",
|
|
293
|
+
"CREATE INDEX IF NOT EXISTS idx_version_system ON system_versions (system_id)",
|
|
294
|
+
"CREATE INDEX IF NOT EXISTS idx_version_created ON system_versions (created_at)",
|
|
295
|
+
"CREATE UNIQUE INDEX IF NOT EXISTS uq_experiment_system ON experimental_systems (experiment_id, system_id)",
|
|
296
|
+
"CREATE INDEX IF NOT EXISTS idx_experimental_system ON experimental_systems (experiment_id, system_id)",
|
|
297
|
+
"CREATE INDEX IF NOT EXISTS idx_outcome_rewards_session ON outcome_rewards (session_id)",
|
|
298
|
+
"CREATE INDEX IF NOT EXISTS idx_outcome_rewards_total ON outcome_rewards (total_reward)",
|
|
299
|
+
"CREATE INDEX IF NOT EXISTS idx_event_rewards_session ON event_rewards (session_id)",
|
|
300
|
+
"CREATE INDEX IF NOT EXISTS idx_event_rewards_event ON event_rewards (event_id)",
|
|
301
|
+
"CREATE INDEX IF NOT EXISTS idx_event_rewards_type ON event_rewards (reward_type)",
|
|
302
|
+
'CREATE INDEX IF NOT EXISTS idx_event_rewards_key ON event_rewards ("key")',
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class NativeLibsqlTraceManager(TraceStorage):
|
|
307
|
+
"""Libsql-backed trace manager."""
|
|
308
|
+
|
|
309
|
+
def __init__(
|
|
310
|
+
self,
|
|
311
|
+
db_url: str | None = None,
|
|
312
|
+
*,
|
|
313
|
+
auth_token: str | None = None,
|
|
314
|
+
):
|
|
315
|
+
self._config_auth_token = auth_token
|
|
316
|
+
self._target = _resolve_connection_target(db_url, auth_token)
|
|
317
|
+
self._conn: libsql.Connection | None = None
|
|
318
|
+
self._conn_lock = asyncio.Lock()
|
|
319
|
+
self._op_lock = asyncio.Lock()
|
|
320
|
+
self._initialized = False
|
|
321
|
+
|
|
322
|
+
def _open_connection(self) -> libsql.Connection:
|
|
323
|
+
"""Open a libsql connection for the resolved target."""
|
|
324
|
+
kwargs: dict[str, Any] = {}
|
|
325
|
+
if self._target.sync_url and self._target.sync_url.startswith("libsql://"):
|
|
326
|
+
kwargs["sync_url"] = self._target.sync_url
|
|
327
|
+
if self._target.auth_token:
|
|
328
|
+
kwargs["auth_token"] = self._target.auth_token
|
|
329
|
+
# Disable automatic background sync; ReplicaSync drives this explicitly.
|
|
330
|
+
kwargs.setdefault("sync_interval", 0)
|
|
331
|
+
logger.debug("Opening libsql connection to %s", self._target.database)
|
|
332
|
+
return libsql.connect(self._target.database, **kwargs)
|
|
333
|
+
|
|
334
|
+
async def initialize(self):
|
|
335
|
+
"""Initialise the backend."""
|
|
336
|
+
async with self._conn_lock:
|
|
337
|
+
if self._initialized:
|
|
338
|
+
return
|
|
339
|
+
|
|
340
|
+
# Establish a libsql connection for future native operations.
|
|
341
|
+
self._conn = self._open_connection()
|
|
342
|
+
self._ensure_schema()
|
|
343
|
+
self._initialized = True
|
|
344
|
+
|
|
345
|
+
async def close(self):
|
|
346
|
+
"""Close the libsql connection."""
|
|
347
|
+
async with self._conn_lock:
|
|
348
|
+
if self._conn:
|
|
349
|
+
logger.debug("Closing libsql connection to %s", self._target.database)
|
|
350
|
+
self._conn.close()
|
|
351
|
+
self._conn = None
|
|
352
|
+
self._initialized = False
|
|
353
|
+
|
|
354
|
+
# ------------------------------------------------------------------
|
|
355
|
+
# Delegated operations (to be swapped with native libsql versions).
|
|
356
|
+
# ------------------------------------------------------------------
|
|
357
|
+
|
|
358
|
+
async def insert_session_trace(self, trace: SessionTrace) -> str:
|
|
359
|
+
await self.initialize()
|
|
360
|
+
|
|
361
|
+
if await self._session_exists(trace.session_id):
|
|
362
|
+
async with self._op_lock:
|
|
363
|
+
conn = self._conn
|
|
364
|
+
assert conn is not None
|
|
365
|
+
conn.execute(
|
|
366
|
+
"UPDATE session_traces SET metadata = ? WHERE session_id = ?",
|
|
367
|
+
(_json_dumps(trace.metadata or {}), trace.session_id),
|
|
368
|
+
)
|
|
369
|
+
conn.commit()
|
|
370
|
+
return trace.session_id
|
|
371
|
+
|
|
372
|
+
created_at = trace.created_at or datetime.now(UTC)
|
|
373
|
+
|
|
374
|
+
async with self._op_lock:
|
|
375
|
+
conn = self._conn
|
|
376
|
+
assert conn is not None
|
|
377
|
+
conn.execute(
|
|
378
|
+
"""
|
|
379
|
+
INSERT INTO session_traces (
|
|
380
|
+
session_id,
|
|
381
|
+
created_at,
|
|
382
|
+
num_timesteps,
|
|
383
|
+
num_events,
|
|
384
|
+
num_messages,
|
|
385
|
+
metadata
|
|
386
|
+
)
|
|
387
|
+
VALUES (?, ?, 0, 0, 0, ?)
|
|
388
|
+
""",
|
|
389
|
+
(
|
|
390
|
+
trace.session_id,
|
|
391
|
+
created_at.isoformat(),
|
|
392
|
+
_json_dumps(trace.metadata or {}),
|
|
393
|
+
),
|
|
394
|
+
)
|
|
395
|
+
conn.commit()
|
|
396
|
+
|
|
397
|
+
step_id_map: dict[str, int] = {}
|
|
398
|
+
|
|
399
|
+
for step in trace.session_time_steps:
|
|
400
|
+
step_db_id = await self.ensure_timestep(
|
|
401
|
+
trace.session_id,
|
|
402
|
+
step_id=step.step_id,
|
|
403
|
+
step_index=step.step_index,
|
|
404
|
+
turn_number=step.turn_number,
|
|
405
|
+
started_at=step.timestamp,
|
|
406
|
+
completed_at=step.completed_at,
|
|
407
|
+
metadata=step.step_metadata or {},
|
|
408
|
+
)
|
|
409
|
+
step_id_map[step.step_id] = step_db_id
|
|
410
|
+
|
|
411
|
+
for event in trace.event_history:
|
|
412
|
+
step_ref = None
|
|
413
|
+
metadata = event.metadata or {}
|
|
414
|
+
if isinstance(metadata, dict):
|
|
415
|
+
step_ref = metadata.get("step_id")
|
|
416
|
+
timestep_db_id = step_id_map.get(step_ref) if step_ref else None
|
|
417
|
+
await self.insert_event_row(
|
|
418
|
+
trace.session_id,
|
|
419
|
+
timestep_db_id=timestep_db_id,
|
|
420
|
+
event=event,
|
|
421
|
+
metadata_override=event.metadata or {},
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
for msg in trace.markov_blanket_message_history:
|
|
425
|
+
metadata = dict(getattr(msg, "metadata", {}) or {})
|
|
426
|
+
step_ref = metadata.get("step_id")
|
|
427
|
+
content_value = msg.content
|
|
428
|
+
if isinstance(msg.content, SessionMessageContent):
|
|
429
|
+
if msg.content.json_payload:
|
|
430
|
+
metadata.setdefault("json_payload", msg.content.json_payload)
|
|
431
|
+
content_value = msg.content.json_payload
|
|
432
|
+
else:
|
|
433
|
+
content_value = msg.content.as_text()
|
|
434
|
+
if msg.content.text:
|
|
435
|
+
metadata.setdefault("text", msg.content.text)
|
|
436
|
+
elif not isinstance(content_value, str):
|
|
437
|
+
try:
|
|
438
|
+
content_value = json.dumps(content_value, ensure_ascii=False)
|
|
439
|
+
except (TypeError, ValueError):
|
|
440
|
+
content_value = str(content_value)
|
|
441
|
+
|
|
442
|
+
await self.insert_message_row(
|
|
443
|
+
trace.session_id,
|
|
444
|
+
timestep_db_id=step_id_map.get(step_ref) if step_ref else None,
|
|
445
|
+
message_type=msg.message_type,
|
|
446
|
+
content=content_value,
|
|
447
|
+
event_time=msg.time_record.event_time,
|
|
448
|
+
message_time=msg.time_record.message_time,
|
|
449
|
+
metadata=metadata,
|
|
450
|
+
)
|
|
451
|
+
|
|
452
|
+
async with self._op_lock:
|
|
453
|
+
conn = self._conn
|
|
454
|
+
assert conn is not None
|
|
455
|
+
conn.execute(
|
|
456
|
+
"UPDATE session_traces SET num_timesteps = ?, num_events = ?, num_messages = ?, metadata = ? WHERE session_id = ?",
|
|
457
|
+
(
|
|
458
|
+
len(trace.session_time_steps),
|
|
459
|
+
len(trace.event_history),
|
|
460
|
+
len(trace.markov_blanket_message_history),
|
|
461
|
+
_json_dumps(trace.metadata or {}),
|
|
462
|
+
trace.session_id,
|
|
463
|
+
),
|
|
464
|
+
)
|
|
465
|
+
conn.commit()
|
|
466
|
+
|
|
467
|
+
return trace.session_id
|
|
468
|
+
|
|
469
|
+
async def get_session_trace(self, session_id: str) -> dict[str, Any] | None:
|
|
470
|
+
await self.initialize()
|
|
471
|
+
|
|
472
|
+
async with self._op_lock:
|
|
473
|
+
conn = self._conn
|
|
474
|
+
assert conn is not None
|
|
475
|
+
|
|
476
|
+
session_cursor = conn.execute(
|
|
477
|
+
"""
|
|
478
|
+
SELECT session_id,
|
|
479
|
+
created_at,
|
|
480
|
+
num_timesteps,
|
|
481
|
+
num_events,
|
|
482
|
+
num_messages,
|
|
483
|
+
metadata
|
|
484
|
+
FROM session_traces
|
|
485
|
+
WHERE session_id = ?
|
|
486
|
+
""",
|
|
487
|
+
(session_id,),
|
|
488
|
+
)
|
|
489
|
+
session_row = session_cursor.fetchone()
|
|
490
|
+
session_cursor.close()
|
|
491
|
+
|
|
492
|
+
if not session_row:
|
|
493
|
+
return None
|
|
494
|
+
|
|
495
|
+
session_columns = ["session_id", "created_at", "num_timesteps", "num_events", "num_messages", "metadata"]
|
|
496
|
+
session_data = dict(zip(session_columns, session_row))
|
|
497
|
+
|
|
498
|
+
timestep_cursor = conn.execute(
|
|
499
|
+
"""
|
|
500
|
+
SELECT step_id,
|
|
501
|
+
step_index,
|
|
502
|
+
turn_number,
|
|
503
|
+
started_at,
|
|
504
|
+
completed_at,
|
|
505
|
+
step_metadata
|
|
506
|
+
FROM session_timesteps
|
|
507
|
+
WHERE session_id = ?
|
|
508
|
+
ORDER BY step_index ASC
|
|
509
|
+
""",
|
|
510
|
+
(session_id,),
|
|
511
|
+
)
|
|
512
|
+
timestep_rows = timestep_cursor.fetchall()
|
|
513
|
+
timestep_cursor.close()
|
|
514
|
+
|
|
515
|
+
return {
|
|
516
|
+
"session_id": session_data["session_id"],
|
|
517
|
+
"created_at": _maybe_datetime(session_data["created_at"]),
|
|
518
|
+
"num_timesteps": session_data["num_timesteps"],
|
|
519
|
+
"num_events": session_data["num_events"],
|
|
520
|
+
"num_messages": session_data["num_messages"],
|
|
521
|
+
"metadata": _load_json(session_data["metadata"]),
|
|
522
|
+
"timesteps": [
|
|
523
|
+
{
|
|
524
|
+
"step_id": row[0],
|
|
525
|
+
"step_index": row[1],
|
|
526
|
+
"turn_number": row[2],
|
|
527
|
+
"started_at": _maybe_datetime(row[3]),
|
|
528
|
+
"completed_at": _maybe_datetime(row[4]),
|
|
529
|
+
"metadata": _load_json(row[5]),
|
|
530
|
+
}
|
|
531
|
+
for row in timestep_rows
|
|
532
|
+
],
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
async def _session_exists(self, session_id: str) -> bool:
|
|
536
|
+
await self.initialize()
|
|
537
|
+
async with self._op_lock:
|
|
538
|
+
conn = self._conn
|
|
539
|
+
assert conn is not None
|
|
540
|
+
cursor = conn.execute(
|
|
541
|
+
"SELECT 1 FROM session_traces WHERE session_id = ?", (session_id,)
|
|
542
|
+
)
|
|
543
|
+
row = cursor.fetchone()
|
|
544
|
+
cursor.close()
|
|
545
|
+
return row is not None
|
|
546
|
+
|
|
547
|
+
@staticmethod
|
|
548
|
+
def _normalise_params(params: dict[str, Any] | None) -> dict[str, Any]:
|
|
549
|
+
if not params:
|
|
550
|
+
return {}
|
|
551
|
+
normalised: dict[str, Any] = {}
|
|
552
|
+
for key, value in params.items():
|
|
553
|
+
if isinstance(value, datetime):
|
|
554
|
+
normalised[key] = value.isoformat()
|
|
555
|
+
else:
|
|
556
|
+
normalised[key] = value
|
|
557
|
+
return normalised
|
|
558
|
+
|
|
559
|
+
@staticmethod
|
|
560
|
+
def _prepare_query_params(query: str, params: dict[str, Any] | list[Any] | tuple[Any, ...]) -> tuple[str, tuple[Any, ...]]:
|
|
561
|
+
if isinstance(params, dict):
|
|
562
|
+
keys: list[str] = []
|
|
563
|
+
|
|
564
|
+
def _replace(match: re.Match[str]) -> str:
|
|
565
|
+
key = match.group(1)
|
|
566
|
+
keys.append(key)
|
|
567
|
+
return "?"
|
|
568
|
+
|
|
569
|
+
new_query = re.sub(r":([a-zA-Z_][a-zA-Z0-9_]*)", _replace, query)
|
|
570
|
+
if not keys:
|
|
571
|
+
raise ValueError("No named parameters found in query for provided mapping")
|
|
572
|
+
values = tuple(params[key] for key in keys)
|
|
573
|
+
return new_query, values
|
|
574
|
+
if isinstance(params, (list, tuple)):
|
|
575
|
+
return query, tuple(params)
|
|
576
|
+
raise TypeError("Unsupported parameter type for query execution")
|
|
577
|
+
|
|
578
|
+
def _ensure_schema(self) -> None:
|
|
579
|
+
if not self._conn:
|
|
580
|
+
raise RuntimeError("Connection not initialised")
|
|
581
|
+
|
|
582
|
+
for ddl in _TABLE_DEFINITIONS:
|
|
583
|
+
self._conn.execute(ddl)
|
|
584
|
+
for ddl in _INDEX_DEFINITIONS:
|
|
585
|
+
self._conn.execute(ddl)
|
|
586
|
+
for view_sql in analytics_views.values():
|
|
587
|
+
self._conn.execute(view_sql)
|
|
588
|
+
self._conn.commit()
|
|
589
|
+
|
|
590
|
+
async def query_traces(self, query: str, params: dict[str, Any] | None = None) -> Any:
|
|
591
|
+
await self.initialize()
|
|
592
|
+
|
|
593
|
+
async with self._op_lock:
|
|
594
|
+
conn = self._conn
|
|
595
|
+
assert conn is not None
|
|
596
|
+
normalised = self._normalise_params(params)
|
|
597
|
+
if normalised:
|
|
598
|
+
prepared_query, prepared_params = self._prepare_query_params(query, normalised)
|
|
599
|
+
cursor = conn.execute(prepared_query, prepared_params)
|
|
600
|
+
else:
|
|
601
|
+
cursor = conn.execute(query)
|
|
602
|
+
try:
|
|
603
|
+
description = cursor.description or []
|
|
604
|
+
columns = [col[0] for col in description]
|
|
605
|
+
rows = cursor.fetchall()
|
|
606
|
+
finally:
|
|
607
|
+
cursor.close()
|
|
608
|
+
|
|
609
|
+
if not rows:
|
|
610
|
+
if pd is not None:
|
|
611
|
+
return pd.DataFrame(columns=[col for col in columns])
|
|
612
|
+
return []
|
|
613
|
+
|
|
614
|
+
records = [dict(zip(columns, row)) for row in rows]
|
|
615
|
+
if pd is not None:
|
|
616
|
+
return pd.DataFrame(records)
|
|
617
|
+
return records
|
|
618
|
+
|
|
619
|
+
async def get_model_usage(
|
|
620
|
+
self,
|
|
621
|
+
start_date=None,
|
|
622
|
+
end_date=None,
|
|
623
|
+
model_name=None,
|
|
624
|
+
) -> Any:
|
|
625
|
+
query = """
|
|
626
|
+
SELECT * FROM model_usage_stats
|
|
627
|
+
WHERE 1=1
|
|
628
|
+
"""
|
|
629
|
+
params: dict[str, Any] = {}
|
|
630
|
+
if start_date:
|
|
631
|
+
params["start_date"] = start_date
|
|
632
|
+
query += " AND last_used >= :start_date"
|
|
633
|
+
if end_date:
|
|
634
|
+
params["end_date"] = end_date
|
|
635
|
+
query += " AND first_used <= :end_date"
|
|
636
|
+
if model_name:
|
|
637
|
+
params["model_name"] = model_name
|
|
638
|
+
query += " AND model_name = :model_name"
|
|
639
|
+
query += " ORDER BY usage_count DESC"
|
|
640
|
+
return await self.query_traces(query, params)
|
|
641
|
+
|
|
642
|
+
async def delete_session(self, session_id: str) -> bool:
|
|
643
|
+
await self.initialize()
|
|
644
|
+
|
|
645
|
+
async with self._op_lock:
|
|
646
|
+
conn = self._conn
|
|
647
|
+
assert conn is not None
|
|
648
|
+
|
|
649
|
+
cursor = conn.execute(
|
|
650
|
+
"SELECT 1 FROM session_traces WHERE session_id = ?", (session_id,)
|
|
651
|
+
)
|
|
652
|
+
exists = cursor.fetchone() is not None
|
|
653
|
+
cursor.close()
|
|
654
|
+
if not exists:
|
|
655
|
+
return False
|
|
656
|
+
|
|
657
|
+
conn.execute("DELETE FROM event_rewards WHERE session_id = ?", (session_id,))
|
|
658
|
+
conn.execute("DELETE FROM outcome_rewards WHERE session_id = ?", (session_id,))
|
|
659
|
+
conn.execute("DELETE FROM messages WHERE session_id = ?", (session_id,))
|
|
660
|
+
conn.execute("DELETE FROM events WHERE session_id = ?", (session_id,))
|
|
661
|
+
conn.execute("DELETE FROM session_timesteps WHERE session_id = ?", (session_id,))
|
|
662
|
+
conn.execute("DELETE FROM session_traces WHERE session_id = ?", (session_id,))
|
|
663
|
+
conn.commit()
|
|
664
|
+
return True
|
|
665
|
+
|
|
666
|
+
# Experiment helpers -------------------------------------------------
|
|
667
|
+
async def create_experiment(
|
|
668
|
+
self,
|
|
669
|
+
experiment_id: str,
|
|
670
|
+
name: str,
|
|
671
|
+
description: str | None = None,
|
|
672
|
+
configuration: dict[str, Any] | None = None,
|
|
673
|
+
) -> str:
|
|
674
|
+
await self.initialize()
|
|
675
|
+
|
|
676
|
+
async with self._op_lock:
|
|
677
|
+
conn = self._conn
|
|
678
|
+
assert conn is not None
|
|
679
|
+
conn.execute(
|
|
680
|
+
"""
|
|
681
|
+
INSERT INTO experiments (experiment_id, name, description, configuration)
|
|
682
|
+
VALUES (?, ?, ?, ?)
|
|
683
|
+
ON CONFLICT(experiment_id) DO UPDATE SET
|
|
684
|
+
name = excluded.name,
|
|
685
|
+
description = excluded.description,
|
|
686
|
+
configuration = excluded.configuration
|
|
687
|
+
""",
|
|
688
|
+
(
|
|
689
|
+
experiment_id,
|
|
690
|
+
name,
|
|
691
|
+
description,
|
|
692
|
+
_json_dumps(configuration or {}),
|
|
693
|
+
),
|
|
694
|
+
)
|
|
695
|
+
conn.commit()
|
|
696
|
+
return experiment_id
|
|
697
|
+
|
|
698
|
+
async def link_session_to_experiment(self, session_id: str, experiment_id: str):
|
|
699
|
+
await self.initialize()
|
|
700
|
+
|
|
701
|
+
async with self._op_lock:
|
|
702
|
+
conn = self._conn
|
|
703
|
+
assert conn is not None
|
|
704
|
+
conn.execute(
|
|
705
|
+
"UPDATE session_traces SET experiment_id = ? WHERE session_id = ?",
|
|
706
|
+
(experiment_id, session_id),
|
|
707
|
+
)
|
|
708
|
+
conn.commit()
|
|
709
|
+
|
|
710
|
+
async def get_sessions_by_experiment(
|
|
711
|
+
self, experiment_id: str, limit: int | None = None
|
|
712
|
+
) -> list[dict[str, Any]]:
|
|
713
|
+
await self.initialize()
|
|
714
|
+
|
|
715
|
+
sql = """
|
|
716
|
+
SELECT session_id,
|
|
717
|
+
created_at,
|
|
718
|
+
num_timesteps,
|
|
719
|
+
num_events,
|
|
720
|
+
num_messages,
|
|
721
|
+
metadata
|
|
722
|
+
FROM session_traces
|
|
723
|
+
WHERE experiment_id = ?
|
|
724
|
+
ORDER BY created_at DESC
|
|
725
|
+
"""
|
|
726
|
+
params: list[Any] = [experiment_id]
|
|
727
|
+
if limit is not None:
|
|
728
|
+
sql += " LIMIT ?"
|
|
729
|
+
params.append(limit)
|
|
730
|
+
|
|
731
|
+
async with self._op_lock:
|
|
732
|
+
conn = self._conn
|
|
733
|
+
assert conn is not None
|
|
734
|
+
cursor = conn.execute(sql, params)
|
|
735
|
+
rows = cursor.fetchall()
|
|
736
|
+
cursor.close()
|
|
737
|
+
|
|
738
|
+
return [
|
|
739
|
+
{
|
|
740
|
+
"session_id": row[0],
|
|
741
|
+
"created_at": _maybe_datetime(row[1]),
|
|
742
|
+
"num_timesteps": row[2],
|
|
743
|
+
"num_events": row[3],
|
|
744
|
+
"num_messages": row[4],
|
|
745
|
+
"metadata": _load_json(row[5]),
|
|
746
|
+
}
|
|
747
|
+
for row in rows
|
|
748
|
+
]
|
|
749
|
+
|
|
750
|
+
async def batch_insert_sessions(
|
|
751
|
+
self, traces: list[SessionTrace], batch_size: int | None = None
|
|
752
|
+
) -> list[str]:
|
|
753
|
+
batch_size = batch_size or CONFIG.batch_size
|
|
754
|
+
inserted: list[str] = []
|
|
755
|
+
|
|
756
|
+
for i in range(0, len(traces), batch_size):
|
|
757
|
+
chunk = traces[i : i + batch_size]
|
|
758
|
+
for trace in chunk:
|
|
759
|
+
session_id = await self.insert_session_trace(trace)
|
|
760
|
+
inserted.append(session_id)
|
|
761
|
+
return inserted
|
|
762
|
+
|
|
763
|
+
# Incremental helpers -----------------------------------------------
|
|
764
|
+
async def ensure_session(
|
|
765
|
+
self,
|
|
766
|
+
session_id: str,
|
|
767
|
+
*,
|
|
768
|
+
created_at=None,
|
|
769
|
+
metadata=None,
|
|
770
|
+
) -> None:
|
|
771
|
+
await self.initialize()
|
|
772
|
+
|
|
773
|
+
created_at_val = (created_at or datetime.now(UTC)).isoformat()
|
|
774
|
+
metadata_json = _json_dumps(metadata or {})
|
|
775
|
+
|
|
776
|
+
async with self._op_lock:
|
|
777
|
+
conn = self._conn
|
|
778
|
+
|
|
779
|
+
assert conn is not None
|
|
780
|
+
conn.execute(
|
|
781
|
+
"""
|
|
782
|
+
INSERT INTO session_traces (
|
|
783
|
+
session_id, created_at, num_timesteps, num_events, num_messages, metadata
|
|
784
|
+
)
|
|
785
|
+
VALUES (?, ?, 0, 0, 0, ?)
|
|
786
|
+
ON CONFLICT(session_id) DO NOTHING
|
|
787
|
+
""",
|
|
788
|
+
(session_id, created_at_val, metadata_json),
|
|
789
|
+
)
|
|
790
|
+
conn.commit()
|
|
791
|
+
|
|
792
|
+
async def ensure_timestep(
|
|
793
|
+
self,
|
|
794
|
+
session_id: str,
|
|
795
|
+
*,
|
|
796
|
+
step_id: str,
|
|
797
|
+
step_index: int,
|
|
798
|
+
turn_number: int | None = None,
|
|
799
|
+
started_at=None,
|
|
800
|
+
completed_at=None,
|
|
801
|
+
metadata=None,
|
|
802
|
+
) -> int:
|
|
803
|
+
await self.initialize()
|
|
804
|
+
|
|
805
|
+
started_at_val = (started_at or datetime.now(UTC)).isoformat()
|
|
806
|
+
completed_at_val = completed_at.isoformat() if completed_at else None
|
|
807
|
+
metadata_json = _json_dumps(metadata or {})
|
|
808
|
+
|
|
809
|
+
async with self._op_lock:
|
|
810
|
+
conn = self._conn
|
|
811
|
+
|
|
812
|
+
assert conn is not None
|
|
813
|
+
cur = conn.execute(
|
|
814
|
+
"""
|
|
815
|
+
SELECT id FROM session_timesteps
|
|
816
|
+
WHERE session_id = ? AND step_id = ?
|
|
817
|
+
""",
|
|
818
|
+
(session_id, step_id),
|
|
819
|
+
)
|
|
820
|
+
row = cur.fetchone()
|
|
821
|
+
if row:
|
|
822
|
+
return int(row[0])
|
|
823
|
+
|
|
824
|
+
cur = conn.execute(
|
|
825
|
+
"""
|
|
826
|
+
INSERT INTO session_timesteps (
|
|
827
|
+
session_id,
|
|
828
|
+
step_id,
|
|
829
|
+
step_index,
|
|
830
|
+
turn_number,
|
|
831
|
+
started_at,
|
|
832
|
+
completed_at,
|
|
833
|
+
num_events,
|
|
834
|
+
num_messages,
|
|
835
|
+
step_metadata
|
|
836
|
+
)
|
|
837
|
+
VALUES (?, ?, ?, ?, ?, ?, 0, 0, ?)
|
|
838
|
+
""",
|
|
839
|
+
(
|
|
840
|
+
session_id,
|
|
841
|
+
step_id,
|
|
842
|
+
step_index,
|
|
843
|
+
turn_number,
|
|
844
|
+
started_at_val,
|
|
845
|
+
completed_at_val,
|
|
846
|
+
metadata_json,
|
|
847
|
+
),
|
|
848
|
+
)
|
|
849
|
+
timestep_id = int(cur.lastrowid)
|
|
850
|
+
conn.execute(
|
|
851
|
+
"""
|
|
852
|
+
UPDATE session_traces
|
|
853
|
+
SET num_timesteps = num_timesteps + 1
|
|
854
|
+
WHERE session_id = ?
|
|
855
|
+
""",
|
|
856
|
+
(session_id,),
|
|
857
|
+
)
|
|
858
|
+
conn.commit()
|
|
859
|
+
return timestep_id
|
|
860
|
+
|
|
861
|
+
async def insert_event_row(
|
|
862
|
+
self,
|
|
863
|
+
session_id: str,
|
|
864
|
+
*,
|
|
865
|
+
timestep_db_id: int | None,
|
|
866
|
+
event: Any,
|
|
867
|
+
metadata_override: dict[str, Any] | None = None,
|
|
868
|
+
) -> int:
|
|
869
|
+
await self.initialize()
|
|
870
|
+
|
|
871
|
+
if not isinstance(event, (EnvironmentEvent, LMCAISEvent, RuntimeEvent)):
|
|
872
|
+
raise TypeError(f"Unsupported event type for native manager: {type(event)!r}")
|
|
873
|
+
|
|
874
|
+
metadata_json = metadata_override or event.metadata or {}
|
|
875
|
+
event_extra_metadata = getattr(event, "event_metadata", None)
|
|
876
|
+
system_state_before = getattr(event, "system_state_before", None)
|
|
877
|
+
system_state_after = getattr(event, "system_state_after", None)
|
|
878
|
+
|
|
879
|
+
payload: dict[str, Any] = {
|
|
880
|
+
"session_id": session_id,
|
|
881
|
+
"timestep_id": timestep_db_id,
|
|
882
|
+
"system_instance_id": event.system_instance_id,
|
|
883
|
+
"event_time": event.time_record.event_time,
|
|
884
|
+
"message_time": event.time_record.message_time,
|
|
885
|
+
"metadata": metadata_json,
|
|
886
|
+
"event_metadata": event_extra_metadata,
|
|
887
|
+
"system_state_before": system_state_before,
|
|
888
|
+
"system_state_after": system_state_after,
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
if isinstance(event, LMCAISEvent):
|
|
892
|
+
call_records = None
|
|
893
|
+
if getattr(event, "call_records", None):
|
|
894
|
+
call_records = [asdict(record) for record in event.call_records]
|
|
895
|
+
payload.update(
|
|
896
|
+
{
|
|
897
|
+
"event_type": "cais",
|
|
898
|
+
"model_name": event.model_name,
|
|
899
|
+
"provider": event.provider,
|
|
900
|
+
"input_tokens": event.input_tokens,
|
|
901
|
+
"output_tokens": event.output_tokens,
|
|
902
|
+
"total_tokens": event.total_tokens,
|
|
903
|
+
"cost_usd": int(event.cost_usd * 100) if event.cost_usd is not None else None,
|
|
904
|
+
"latency_ms": event.latency_ms,
|
|
905
|
+
"span_id": event.span_id,
|
|
906
|
+
"trace_id": event.trace_id,
|
|
907
|
+
"call_records": call_records,
|
|
908
|
+
}
|
|
909
|
+
)
|
|
910
|
+
elif isinstance(event, EnvironmentEvent):
|
|
911
|
+
payload.update(
|
|
912
|
+
{
|
|
913
|
+
"event_type": "environment",
|
|
914
|
+
"reward": event.reward,
|
|
915
|
+
"terminated": event.terminated,
|
|
916
|
+
"truncated": event.truncated,
|
|
917
|
+
}
|
|
918
|
+
)
|
|
919
|
+
elif isinstance(event, RuntimeEvent):
|
|
920
|
+
payload.update(
|
|
921
|
+
{
|
|
922
|
+
"event_type": "runtime",
|
|
923
|
+
"metadata": {**(event.metadata or {}), "actions": event.actions},
|
|
924
|
+
}
|
|
925
|
+
)
|
|
926
|
+
|
|
927
|
+
async with self._op_lock:
|
|
928
|
+
conn = self._conn
|
|
929
|
+
|
|
930
|
+
assert conn is not None
|
|
931
|
+
cur = conn.execute(
|
|
932
|
+
"""
|
|
933
|
+
INSERT INTO events (
|
|
934
|
+
session_id,
|
|
935
|
+
timestep_id,
|
|
936
|
+
event_type,
|
|
937
|
+
system_instance_id,
|
|
938
|
+
event_time,
|
|
939
|
+
message_time,
|
|
940
|
+
model_name,
|
|
941
|
+
provider,
|
|
942
|
+
input_tokens,
|
|
943
|
+
output_tokens,
|
|
944
|
+
total_tokens,
|
|
945
|
+
cost_usd,
|
|
946
|
+
latency_ms,
|
|
947
|
+
span_id,
|
|
948
|
+
trace_id,
|
|
949
|
+
call_records,
|
|
950
|
+
reward,
|
|
951
|
+
terminated,
|
|
952
|
+
truncated,
|
|
953
|
+
system_state_before,
|
|
954
|
+
system_state_after,
|
|
955
|
+
metadata,
|
|
956
|
+
event_metadata
|
|
957
|
+
)
|
|
958
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
959
|
+
""",
|
|
960
|
+
(
|
|
961
|
+
payload["session_id"],
|
|
962
|
+
payload["timestep_id"],
|
|
963
|
+
payload.get("event_type"),
|
|
964
|
+
payload["system_instance_id"],
|
|
965
|
+
payload["event_time"],
|
|
966
|
+
payload["message_time"],
|
|
967
|
+
payload.get("model_name"),
|
|
968
|
+
payload.get("provider"),
|
|
969
|
+
payload.get("input_tokens"),
|
|
970
|
+
payload.get("output_tokens"),
|
|
971
|
+
payload.get("total_tokens"),
|
|
972
|
+
payload.get("cost_usd"),
|
|
973
|
+
payload.get("latency_ms"),
|
|
974
|
+
payload.get("span_id"),
|
|
975
|
+
payload.get("trace_id"),
|
|
976
|
+
_json_dumps(payload.get("call_records")),
|
|
977
|
+
payload.get("reward"),
|
|
978
|
+
payload.get("terminated"),
|
|
979
|
+
payload.get("truncated"),
|
|
980
|
+
_json_dumps(payload.get("system_state_before")),
|
|
981
|
+
_json_dumps(payload.get("system_state_after")),
|
|
982
|
+
_json_dumps(payload.get("metadata")),
|
|
983
|
+
_json_dumps(payload.get("event_metadata")),
|
|
984
|
+
),
|
|
985
|
+
)
|
|
986
|
+
event_id = int(cur.lastrowid)
|
|
987
|
+
conn.execute(
|
|
988
|
+
"""
|
|
989
|
+
UPDATE session_traces
|
|
990
|
+
SET num_events = num_events + 1
|
|
991
|
+
WHERE session_id = ?
|
|
992
|
+
""",
|
|
993
|
+
(session_id,),
|
|
994
|
+
)
|
|
995
|
+
if timestep_db_id is not None:
|
|
996
|
+
conn.execute(
|
|
997
|
+
"""
|
|
998
|
+
UPDATE session_timesteps
|
|
999
|
+
SET num_events = num_events + 1
|
|
1000
|
+
WHERE id = ?
|
|
1001
|
+
""",
|
|
1002
|
+
(timestep_db_id,),
|
|
1003
|
+
)
|
|
1004
|
+
conn.commit()
|
|
1005
|
+
return event_id
|
|
1006
|
+
|
|
1007
|
+
async def insert_message_row(
|
|
1008
|
+
self,
|
|
1009
|
+
session_id: str,
|
|
1010
|
+
*,
|
|
1011
|
+
timestep_db_id: int | None,
|
|
1012
|
+
message_type: str,
|
|
1013
|
+
content: Any,
|
|
1014
|
+
event_time: float | None = None,
|
|
1015
|
+
message_time: int | None = None,
|
|
1016
|
+
metadata: dict[str, Any] | None = None,
|
|
1017
|
+
) -> int:
|
|
1018
|
+
await self.initialize()
|
|
1019
|
+
|
|
1020
|
+
metadata_payload = dict(metadata or {})
|
|
1021
|
+
if isinstance(content, SessionMessageContent):
|
|
1022
|
+
if content.json_payload:
|
|
1023
|
+
metadata_payload.setdefault("json_payload", content.json_payload)
|
|
1024
|
+
content_value = content.json_payload
|
|
1025
|
+
else:
|
|
1026
|
+
content_value = content.as_text()
|
|
1027
|
+
if content.text:
|
|
1028
|
+
metadata_payload.setdefault("text", content.text)
|
|
1029
|
+
else:
|
|
1030
|
+
content_value = content
|
|
1031
|
+
if not isinstance(content_value, str):
|
|
1032
|
+
try:
|
|
1033
|
+
content_value = json.dumps(content_value, ensure_ascii=False)
|
|
1034
|
+
except (TypeError, ValueError):
|
|
1035
|
+
content_value = str(content_value)
|
|
1036
|
+
|
|
1037
|
+
async with self._op_lock:
|
|
1038
|
+
conn = self._conn
|
|
1039
|
+
|
|
1040
|
+
assert conn is not None
|
|
1041
|
+
cur = conn.execute(
|
|
1042
|
+
"""
|
|
1043
|
+
INSERT INTO messages (
|
|
1044
|
+
session_id,
|
|
1045
|
+
timestep_id,
|
|
1046
|
+
message_type,
|
|
1047
|
+
content,
|
|
1048
|
+
event_time,
|
|
1049
|
+
message_time,
|
|
1050
|
+
metadata
|
|
1051
|
+
)
|
|
1052
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1053
|
+
""",
|
|
1054
|
+
(
|
|
1055
|
+
session_id,
|
|
1056
|
+
timestep_db_id,
|
|
1057
|
+
message_type,
|
|
1058
|
+
content_value,
|
|
1059
|
+
event_time,
|
|
1060
|
+
message_time,
|
|
1061
|
+
_json_dumps(metadata_payload),
|
|
1062
|
+
),
|
|
1063
|
+
)
|
|
1064
|
+
message_id = int(cur.lastrowid)
|
|
1065
|
+
conn.execute(
|
|
1066
|
+
"""
|
|
1067
|
+
UPDATE session_traces
|
|
1068
|
+
SET num_messages = num_messages + 1
|
|
1069
|
+
WHERE session_id = ?
|
|
1070
|
+
""",
|
|
1071
|
+
(session_id,),
|
|
1072
|
+
)
|
|
1073
|
+
if timestep_db_id is not None:
|
|
1074
|
+
conn.execute(
|
|
1075
|
+
"""
|
|
1076
|
+
UPDATE session_timesteps
|
|
1077
|
+
SET num_messages = num_messages + 1
|
|
1078
|
+
WHERE id = ?
|
|
1079
|
+
""",
|
|
1080
|
+
(timestep_db_id,),
|
|
1081
|
+
)
|
|
1082
|
+
conn.commit()
|
|
1083
|
+
return message_id
|
|
1084
|
+
|
|
1085
|
+
async def insert_outcome_reward(
|
|
1086
|
+
self,
|
|
1087
|
+
session_id: str,
|
|
1088
|
+
*,
|
|
1089
|
+
total_reward: int,
|
|
1090
|
+
achievements_count: int,
|
|
1091
|
+
total_steps: int,
|
|
1092
|
+
reward_metadata: dict | None = None,
|
|
1093
|
+
) -> int:
|
|
1094
|
+
await self.initialize()
|
|
1095
|
+
|
|
1096
|
+
async with self._op_lock:
|
|
1097
|
+
conn = self._conn
|
|
1098
|
+
|
|
1099
|
+
assert conn is not None
|
|
1100
|
+
cur = conn.execute(
|
|
1101
|
+
"""
|
|
1102
|
+
INSERT INTO outcome_rewards (
|
|
1103
|
+
session_id,
|
|
1104
|
+
total_reward,
|
|
1105
|
+
achievements_count,
|
|
1106
|
+
total_steps,
|
|
1107
|
+
created_at,
|
|
1108
|
+
reward_metadata
|
|
1109
|
+
)
|
|
1110
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
1111
|
+
""",
|
|
1112
|
+
(
|
|
1113
|
+
session_id,
|
|
1114
|
+
total_reward,
|
|
1115
|
+
achievements_count,
|
|
1116
|
+
total_steps,
|
|
1117
|
+
datetime.now(UTC).isoformat(),
|
|
1118
|
+
_json_dumps(reward_metadata),
|
|
1119
|
+
),
|
|
1120
|
+
)
|
|
1121
|
+
conn.commit()
|
|
1122
|
+
return int(cur.lastrowid)
|
|
1123
|
+
|
|
1124
|
+
async def insert_event_reward(
|
|
1125
|
+
self,
|
|
1126
|
+
session_id: str,
|
|
1127
|
+
*,
|
|
1128
|
+
event_id: int,
|
|
1129
|
+
message_id: int | None = None,
|
|
1130
|
+
turn_number: int | None = None,
|
|
1131
|
+
reward_value: float = 0.0,
|
|
1132
|
+
reward_type: str | None = None,
|
|
1133
|
+
key: str | None = None,
|
|
1134
|
+
annotation: dict[str, Any] | None = None,
|
|
1135
|
+
source: str | None = None,
|
|
1136
|
+
) -> int:
|
|
1137
|
+
await self.initialize()
|
|
1138
|
+
|
|
1139
|
+
async with self._op_lock:
|
|
1140
|
+
conn = self._conn
|
|
1141
|
+
|
|
1142
|
+
assert conn is not None
|
|
1143
|
+
cur = conn.execute(
|
|
1144
|
+
"""
|
|
1145
|
+
INSERT INTO event_rewards (
|
|
1146
|
+
event_id,
|
|
1147
|
+
session_id,
|
|
1148
|
+
message_id,
|
|
1149
|
+
turn_number,
|
|
1150
|
+
reward_value,
|
|
1151
|
+
reward_type,
|
|
1152
|
+
key,
|
|
1153
|
+
annotation,
|
|
1154
|
+
source,
|
|
1155
|
+
created_at
|
|
1156
|
+
)
|
|
1157
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1158
|
+
""",
|
|
1159
|
+
(
|
|
1160
|
+
event_id,
|
|
1161
|
+
session_id,
|
|
1162
|
+
message_id,
|
|
1163
|
+
turn_number,
|
|
1164
|
+
reward_value,
|
|
1165
|
+
reward_type,
|
|
1166
|
+
key,
|
|
1167
|
+
_json_dumps(annotation),
|
|
1168
|
+
source,
|
|
1169
|
+
datetime.now(UTC).isoformat(),
|
|
1170
|
+
),
|
|
1171
|
+
)
|
|
1172
|
+
conn.commit()
|
|
1173
|
+
return int(cur.lastrowid)
|