mixdog 0.7.1
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.
- package/.claude-plugin/marketplace.json +31 -0
- package/.claude-plugin/plugin.json +20 -0
- package/.gitattributes +34 -0
- package/.mcp.json +14 -0
- package/ARCHITECTURE.md +77 -0
- package/CHANGELOG.md +7 -0
- package/CONTRIBUTING.md +45 -0
- package/DATA-FLOW.md +79 -0
- package/LICENSE +21 -0
- package/README.md +389 -0
- package/SECURITY.md +138 -0
- package/UNINSTALL.md +112 -0
- package/agents/maintenance.md +5 -0
- package/agents/memory-classification.md +30 -0
- package/agents/scheduler-task.md +18 -0
- package/agents/webhook-handler.md +27 -0
- package/agents/worker.md +24 -0
- package/bin/bridge +133 -0
- package/bin/statusline-launcher.mjs +78 -0
- package/bin/statusline-lib.mjs +550 -0
- package/bin/statusline.mjs +607 -0
- package/bun.lock +802 -0
- package/commands/config.md +16 -0
- package/commands/doctor.md +13 -0
- package/commands/setup.md +17 -0
- package/defaults/cycle3-review-prompt.md +90 -0
- package/defaults/hidden-roles.json +65 -0
- package/defaults/memory-chunk-prompt.md +63 -0
- package/defaults/memory-promote-prompt.md +135 -0
- package/defaults/mixdog-config.template.json +27 -0
- package/defaults/user-workflow.json +8 -0
- package/defaults/user-workflow.md +12 -0
- package/hooks/hooks.json +73 -0
- package/hooks/lib/active-instance.cjs +77 -0
- package/hooks/lib/permission-evaluator.cjs +411 -0
- package/hooks/lib/permission-route.cjs +63 -0
- package/hooks/lib/permission-rules.cjs +170 -0
- package/hooks/lib/settings-loader.cjs +116 -0
- package/hooks/post-tool-use.cjs +84 -0
- package/hooks/pre-mcp-sandbox.cjs +158 -0
- package/hooks/pre-tool-subagent.cjs +253 -0
- package/hooks/session-start.cjs +1372 -0
- package/hooks/turn-timer.cjs +82 -0
- package/lib/claude-md-writer.cjs +386 -0
- package/lib/config-cjs.cjs +61 -0
- package/lib/hook-pipe-path.cjs +10 -0
- package/lib/keychain-cjs.cjs +263 -0
- package/lib/plugin-paths.cjs +61 -0
- package/lib/rules-builder.cjs +241 -0
- package/lib/text-utils.cjs +61 -0
- package/native/README.md +117 -0
- package/native/prebuilt/linux-aarch64/mixdog-shim +0 -0
- package/native/prebuilt/linux-x86_64/mixdog-shim +0 -0
- package/native/prebuilt/macos-aarch64/mixdog-shim +0 -0
- package/native/prebuilt/macos-x86_64/mixdog-shim +0 -0
- package/native/prebuilt/windows-x86_64/mixdog-shim.exe +0 -0
- package/package.json +107 -0
- package/prompts/code-review.txt +16 -0
- package/prompts/security-audit.txt +17 -0
- package/rules/bridge/00-common.md +39 -0
- package/rules/bridge/20-skip-protocol.md +18 -0
- package/rules/bridge/30-explorer.md +33 -0
- package/rules/bridge/40-cycle1-agent.md +52 -0
- package/rules/bridge/41-cycle2-agent.md +62 -0
- package/rules/bridge/42-cycle3-agent.md +44 -0
- package/rules/lead/00-tool-lead.md +61 -0
- package/rules/lead/01-general.md +23 -0
- package/rules/lead/02-channels.md +49 -0
- package/rules/lead/03-team.md +27 -0
- package/rules/lead/04-workflow.md +20 -0
- package/rules/shared/00-language.md +14 -0
- package/rules/shared/01-tool.md +138 -0
- package/scripts/bootstrap.mjs +184 -0
- package/scripts/bridge-unify-smoke.mjs +308 -0
- package/scripts/build-runtime-linux.sh +348 -0
- package/scripts/build-runtime-macos.sh +217 -0
- package/scripts/build-runtime-windows.ps1 +242 -0
- package/scripts/builtin-utils-smoke.mjs +392 -0
- package/scripts/check-json.mjs +45 -0
- package/scripts/check-syntax-changed.mjs +102 -0
- package/scripts/check-syntax.mjs +58 -0
- package/scripts/code-graph-batch.test.mjs +33 -0
- package/scripts/config-preserve-smoke.mjs +180 -0
- package/scripts/doctor.mjs +484 -0
- package/scripts/edit-normalize-fuzz.mjs +130 -0
- package/scripts/edit-normalize-smoke.mjs +401 -0
- package/scripts/edit-operation-smoke.mjs +369 -0
- package/scripts/edit2-smoke.mjs +63 -0
- package/scripts/fuzzy-e2e.mjs +28 -0
- package/scripts/fuzzy-smoke.mjs +26 -0
- package/scripts/generate-runtime-manifest.mjs +166 -0
- package/scripts/guard-smoke.mjs +66 -0
- package/scripts/hidden-role-schema-smoke.mjs +162 -0
- package/scripts/hook-routing-smoke.mjs +29 -0
- package/scripts/inject-input.ps1 +204 -0
- package/scripts/io-complex-smoke.mjs +667 -0
- package/scripts/io-explore-bench.mjs +424 -0
- package/scripts/io-guardrails-smoke.mjs +205 -0
- package/scripts/io-mini-bench-baseline.json +11 -0
- package/scripts/io-mini-bench.mjs +216 -0
- package/scripts/io-route-harness.mjs +933 -0
- package/scripts/io-telemetry-report.mjs +691 -0
- package/scripts/mutation-bench.mjs +564 -0
- package/scripts/mutation-io-smoke.mjs +1081 -0
- package/scripts/native-patch-bridge-smoke.mjs +288 -0
- package/scripts/native-patch-smoke.mjs +304 -0
- package/scripts/patch-interior-context-smoke.mjs +49 -0
- package/scripts/patch-newline-utf8-smoke.mjs +157 -0
- package/scripts/perf-hook-smoke.mjs +71 -0
- package/scripts/permission-eval-smoke.mjs +426 -0
- package/scripts/prep-patch.mjs +53 -0
- package/scripts/prep-shim.mjs +96 -0
- package/scripts/provider-cache-smoke.mjs +687 -0
- package/scripts/report-runtime-health.mjs +132 -0
- package/scripts/run-mcp.mjs +1547 -0
- package/scripts/salvage-v4a-shatter.test.mjs +58 -0
- package/scripts/scoped-cache-io-smoke.mjs +103 -0
- package/scripts/shell-policy-round3-smoke.mjs +46 -0
- package/scripts/smoke-runtime-negative.ps1 +100 -0
- package/scripts/smoke-runtime-negative.sh +95 -0
- package/scripts/stall-policy-smoke.mjs +50 -0
- package/scripts/start-memory-worker.mjs +23 -0
- package/scripts/statusline-launcher-smoke.mjs +82 -0
- package/scripts/stress-atomic-write.mjs +1028 -0
- package/scripts/test-config-rmw-restore.mjs +122 -0
- package/scripts/test-fault-inject.mjs +164 -0
- package/scripts/test-large-file.mjs +174 -0
- package/scripts/tool-edge-smoke.mjs +209 -0
- package/scripts/uninstall.mjs +201 -0
- package/scripts/webhook-selfheal-smoke.mjs +29 -0
- package/scripts/write-overwrite-guard-smoke.mjs +56 -0
- package/server-main.mjs +3055 -0
- package/server.mjs +468 -0
- package/setup/config-merge.mjs +254 -0
- package/setup/install.mjs +120 -0
- package/setup/launch-core.mjs +507 -0
- package/setup/launch.mjs +101 -0
- package/setup/setup-server.mjs +3206 -0
- package/setup/setup.html +3693 -0
- package/skills/retro-skill-proposer/SKILL.md +92 -0
- package/skills/schedule-add/SKILL.md +77 -0
- package/skills/setup/SKILL.md +346 -0
- package/skills/webhook-add/SKILL.md +81 -0
- package/src/agent/bridge-stall-watchdog.mjs +337 -0
- package/src/agent/index.mjs +2138 -0
- package/src/agent/orchestrator/activity-bus.mjs +38 -0
- package/src/agent/orchestrator/ai-wrapped-dispatch.mjs +1010 -0
- package/src/agent/orchestrator/bridge-retry.mjs +220 -0
- package/src/agent/orchestrator/bridge-trace.mjs +583 -0
- package/src/agent/orchestrator/cache-mtime.mjs +58 -0
- package/src/agent/orchestrator/config.mjs +358 -0
- package/src/agent/orchestrator/context/collect.mjs +651 -0
- package/src/agent/orchestrator/dispatch-persist.mjs +549 -0
- package/src/agent/orchestrator/drain-registry.mjs +50 -0
- package/src/agent/orchestrator/explore-validator.mjs +8 -0
- package/src/agent/orchestrator/internal-roles.mjs +118 -0
- package/src/agent/orchestrator/internal-tools.mjs +88 -0
- package/src/agent/orchestrator/jobs.mjs +116 -0
- package/src/agent/orchestrator/mcp/client.mjs +364 -0
- package/src/agent/orchestrator/providers/anthropic-betas.mjs +21 -0
- package/src/agent/orchestrator/providers/anthropic-oauth.mjs +1745 -0
- package/src/agent/orchestrator/providers/anthropic.mjs +437 -0
- package/src/agent/orchestrator/providers/gemini.mjs +1175 -0
- package/src/agent/orchestrator/providers/grok-oauth.mjs +782 -0
- package/src/agent/orchestrator/providers/model-catalog.mjs +241 -0
- package/src/agent/orchestrator/providers/openai-compat.mjs +1467 -0
- package/src/agent/orchestrator/providers/openai-oauth-ws.mjs +1890 -0
- package/src/agent/orchestrator/providers/openai-oauth.mjs +1307 -0
- package/src/agent/orchestrator/providers/openai-ws.mjs +104 -0
- package/src/agent/orchestrator/providers/registry.mjs +192 -0
- package/src/agent/orchestrator/providers/retry-classifier.mjs +325 -0
- package/src/agent/orchestrator/session/abort-lookup.mjs +13 -0
- package/src/agent/orchestrator/session/cache/post-edit-marks.mjs +42 -0
- package/src/agent/orchestrator/session/cache/prefetch-cache.mjs +142 -0
- package/src/agent/orchestrator/session/cache/read-cache.mjs +319 -0
- package/src/agent/orchestrator/session/cache/scoped-cache-outcome.mjs +11 -0
- package/src/agent/orchestrator/session/cache/scoped-cache.mjs +361 -0
- package/src/agent/orchestrator/session/cache/util.mjs +49 -0
- package/src/agent/orchestrator/session/loop.mjs +1478 -0
- package/src/agent/orchestrator/session/manager.mjs +1975 -0
- package/src/agent/orchestrator/session/read-dedup.mjs +6 -0
- package/src/agent/orchestrator/session/result-classification.mjs +65 -0
- package/src/agent/orchestrator/session/save-session-worker.mjs +18 -0
- package/src/agent/orchestrator/session/store.mjs +624 -0
- package/src/agent/orchestrator/session/stream-watchdog.mjs +130 -0
- package/src/agent/orchestrator/session/tool-result-offload.mjs +166 -0
- package/src/agent/orchestrator/session/trim.mjs +491 -0
- package/src/agent/orchestrator/smart-bridge/CACHE-SHARD.md +115 -0
- package/src/agent/orchestrator/smart-bridge/bridge-llm.mjs +327 -0
- package/src/agent/orchestrator/smart-bridge/cache-obs.mjs +150 -0
- package/src/agent/orchestrator/smart-bridge/cache-strategy.mjs +228 -0
- package/src/agent/orchestrator/smart-bridge/index.mjs +215 -0
- package/src/agent/orchestrator/smart-bridge/profiles.mjs +37 -0
- package/src/agent/orchestrator/smart-bridge/registry.mjs +348 -0
- package/src/agent/orchestrator/smart-bridge/session-builder.mjs +116 -0
- package/src/agent/orchestrator/stall-policy.mjs +195 -0
- package/src/agent/orchestrator/tool-loop-guard.mjs +75 -0
- package/src/agent/orchestrator/tools/bash-policy-scan.mjs +77 -0
- package/src/agent/orchestrator/tools/bash-session.mjs +721 -0
- package/src/agent/orchestrator/tools/builtin/advisory-lock.mjs +171 -0
- package/src/agent/orchestrator/tools/builtin/arg-guard.mjs +455 -0
- package/src/agent/orchestrator/tools/builtin/atomic-write.mjs +236 -0
- package/src/agent/orchestrator/tools/builtin/bash-tool.mjs +480 -0
- package/src/agent/orchestrator/tools/builtin/binary-file.mjs +76 -0
- package/src/agent/orchestrator/tools/builtin/builtin-tools.mjs +256 -0
- package/src/agent/orchestrator/tools/builtin/cache-layers.mjs +386 -0
- package/src/agent/orchestrator/tools/builtin/cwd-utils.mjs +37 -0
- package/src/agent/orchestrator/tools/builtin/device-paths.mjs +154 -0
- package/src/agent/orchestrator/tools/builtin/diagnostics-tool.mjs +292 -0
- package/src/agent/orchestrator/tools/builtin/diff-utils.mjs +109 -0
- package/src/agent/orchestrator/tools/builtin/edit-base-guard.mjs +58 -0
- package/src/agent/orchestrator/tools/builtin/edit-byte-plan.mjs +240 -0
- package/src/agent/orchestrator/tools/builtin/edit-byte-utils.mjs +113 -0
- package/src/agent/orchestrator/tools/builtin/edit-commit.mjs +74 -0
- package/src/agent/orchestrator/tools/builtin/edit-context-utils.mjs +242 -0
- package/src/agent/orchestrator/tools/builtin/edit-diagnostics.mjs +211 -0
- package/src/agent/orchestrator/tools/builtin/edit-engine.mjs +1364 -0
- package/src/agent/orchestrator/tools/builtin/edit-failure-context.mjs +126 -0
- package/src/agent/orchestrator/tools/builtin/edit-hint.mjs +141 -0
- package/src/agent/orchestrator/tools/builtin/edit-match-utils.mjs +194 -0
- package/src/agent/orchestrator/tools/builtin/edit-partial-write.mjs +60 -0
- package/src/agent/orchestrator/tools/builtin/edit-stale-refresh.mjs +168 -0
- package/src/agent/orchestrator/tools/builtin/edit-tool.mjs +173 -0
- package/src/agent/orchestrator/tools/builtin/edit-utf8-guard.mjs +48 -0
- package/src/agent/orchestrator/tools/builtin/fs-reachability.mjs +48 -0
- package/src/agent/orchestrator/tools/builtin/fuzzy-match.mjs +99 -0
- package/src/agent/orchestrator/tools/builtin/glob-walk.mjs +170 -0
- package/src/agent/orchestrator/tools/builtin/grep-formatting.mjs +113 -0
- package/src/agent/orchestrator/tools/builtin/hash-utils.mjs +6 -0
- package/src/agent/orchestrator/tools/builtin/list-formatting.mjs +7 -0
- package/src/agent/orchestrator/tools/builtin/list-tool.mjs +593 -0
- package/src/agent/orchestrator/tools/builtin/native-edit-runner.mjs +89 -0
- package/src/agent/orchestrator/tools/builtin/notebook-edit-tool.mjs +300 -0
- package/src/agent/orchestrator/tools/builtin/open-config-tool.mjs +26 -0
- package/src/agent/orchestrator/tools/builtin/path-diagnostics.mjs +152 -0
- package/src/agent/orchestrator/tools/builtin/path-locks.mjs +35 -0
- package/src/agent/orchestrator/tools/builtin/path-utils.mjs +201 -0
- package/src/agent/orchestrator/tools/builtin/read-args.mjs +103 -0
- package/src/agent/orchestrator/tools/builtin/read-batch.mjs +172 -0
- package/src/agent/orchestrator/tools/builtin/read-constants.mjs +40 -0
- package/src/agent/orchestrator/tools/builtin/read-formatting.mjs +118 -0
- package/src/agent/orchestrator/tools/builtin/read-image-resize.mjs +189 -0
- package/src/agent/orchestrator/tools/builtin/read-image.mjs +88 -0
- package/src/agent/orchestrator/tools/builtin/read-lines.mjs +12 -0
- package/src/agent/orchestrator/tools/builtin/read-mode-tool.mjs +455 -0
- package/src/agent/orchestrator/tools/builtin/read-open.mjs +190 -0
- package/src/agent/orchestrator/tools/builtin/read-range-index.mjs +271 -0
- package/src/agent/orchestrator/tools/builtin/read-ranges.mjs +26 -0
- package/src/agent/orchestrator/tools/builtin/read-single-tool.mjs +728 -0
- package/src/agent/orchestrator/tools/builtin/read-snapshot-runtime.mjs +173 -0
- package/src/agent/orchestrator/tools/builtin/read-special-files.mjs +268 -0
- package/src/agent/orchestrator/tools/builtin/read-streaming.mjs +602 -0
- package/src/agent/orchestrator/tools/builtin/read-tool.mjs +530 -0
- package/src/agent/orchestrator/tools/builtin/read-windows.mjs +107 -0
- package/src/agent/orchestrator/tools/builtin/rename-tool.mjs +196 -0
- package/src/agent/orchestrator/tools/builtin/rg-runner.mjs +422 -0
- package/src/agent/orchestrator/tools/builtin/search-builders.mjs +158 -0
- package/src/agent/orchestrator/tools/builtin/search-tool.mjs +869 -0
- package/src/agent/orchestrator/tools/builtin/shell-analysis.mjs +653 -0
- package/src/agent/orchestrator/tools/builtin/shell-jobs.mjs +936 -0
- package/src/agent/orchestrator/tools/builtin/shell-output.mjs +36 -0
- package/src/agent/orchestrator/tools/builtin/shell-runtime.mjs +214 -0
- package/src/agent/orchestrator/tools/builtin/snapshot-helpers.mjs +143 -0
- package/src/agent/orchestrator/tools/builtin/snapshot-store.mjs +206 -0
- package/src/agent/orchestrator/tools/builtin/snapshot-validation.mjs +98 -0
- package/src/agent/orchestrator/tools/builtin/text-stats.mjs +69 -0
- package/src/agent/orchestrator/tools/builtin/windows-roots.mjs +23 -0
- package/src/agent/orchestrator/tools/builtin/write-tool.mjs +401 -0
- package/src/agent/orchestrator/tools/builtin.mjs +500 -0
- package/src/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +39 -0
- package/src/agent/orchestrator/tools/code-graph-tool-defs.mjs +24 -0
- package/src/agent/orchestrator/tools/code-graph.mjs +4095 -0
- package/src/agent/orchestrator/tools/cwd-tool.mjs +298 -0
- package/src/agent/orchestrator/tools/destructive-warning.mjs +323 -0
- package/src/agent/orchestrator/tools/edit-normalize.mjs +603 -0
- package/src/agent/orchestrator/tools/env-scrub.mjs +100 -0
- package/src/agent/orchestrator/tools/graph-binary-fetcher.mjs +144 -0
- package/src/agent/orchestrator/tools/graph-manifest.json +26 -0
- package/src/agent/orchestrator/tools/host-input.mjs +204 -0
- package/src/agent/orchestrator/tools/mutation-content-cache.mjs +67 -0
- package/src/agent/orchestrator/tools/mutation-planner.mjs +75 -0
- package/src/agent/orchestrator/tools/next-call-utils.mjs +48 -0
- package/src/agent/orchestrator/tools/patch-binary-fetcher.mjs +133 -0
- package/src/agent/orchestrator/tools/patch-manifest.json +26 -0
- package/src/agent/orchestrator/tools/patch-tool-defs.mjs +20 -0
- package/src/agent/orchestrator/tools/patch.mjs +2754 -0
- package/src/agent/orchestrator/tools/progress-message.mjs +118 -0
- package/src/agent/orchestrator/tools/result-compression.mjs +279 -0
- package/src/agent/orchestrator/tools/shell-command.mjs +865 -0
- package/src/agent/orchestrator/tools/shell-exec-policy.mjs +89 -0
- package/src/agent/orchestrator/tools/shell-policy-danger-target.mjs +27 -0
- package/src/agent/orchestrator/tools/shell-policy-imports.mjs +7 -0
- package/src/agent/orchestrator/tools/shell-policy.mjs +345 -0
- package/src/agent/orchestrator/tools/shell-snapshot.mjs +313 -0
- package/src/agent/orchestrator/workflow-store.mjs +93 -0
- package/src/agent/tool-defs.mjs +103 -0
- package/src/channels/backends/discord.mjs +784 -0
- package/src/channels/data/voice-runtime-manifest.json +138 -0
- package/src/channels/index.mjs +3229 -0
- package/src/channels/lib/cli-worker-host.mjs +12 -0
- package/src/channels/lib/config-lock.mjs +13 -0
- package/src/channels/lib/config.mjs +292 -0
- package/src/channels/lib/drop-trace.mjs +71 -0
- package/src/channels/lib/event-pipeline.mjs +81 -0
- package/src/channels/lib/event-queue.mjs +345 -0
- package/src/channels/lib/executor.mjs +168 -0
- package/src/channels/lib/format.mjs +188 -0
- package/src/channels/lib/holidays.mjs +138 -0
- package/src/channels/lib/hook-pipe-server.mjs +802 -0
- package/src/channels/lib/interaction-workflows.mjs +184 -0
- package/src/channels/lib/memory-client.mjs +149 -0
- package/src/channels/lib/output-forwarder.mjs +765 -0
- package/src/channels/lib/runtime-paths.mjs +479 -0
- package/src/channels/lib/scheduler.mjs +723 -0
- package/src/channels/lib/session-control.mjs +36 -0
- package/src/channels/lib/session-discovery.mjs +103 -0
- package/src/channels/lib/settings.mjs +11 -0
- package/src/channels/lib/state-file.mjs +68 -0
- package/src/channels/lib/status-snapshot.mjs +219 -0
- package/src/channels/lib/tool-format.mjs +140 -0
- package/src/channels/lib/transcript-discovery.mjs +195 -0
- package/src/channels/lib/voice-runtime-fetcher.mjs +734 -0
- package/src/channels/lib/webhook.mjs +1179 -0
- package/src/channels/lib/whisper-server.mjs +477 -0
- package/src/channels/tool-defs.mjs +170 -0
- package/src/daemon/host.mjs +118 -0
- package/src/daemon/mcp-transport.mjs +47 -0
- package/src/daemon/session.mjs +100 -0
- package/src/daemon/thin-client.mjs +71 -0
- package/src/daemon/transport.mjs +163 -0
- package/src/memory/data/runtime-manifest.json +40 -0
- package/src/memory/index.mjs +3305 -0
- package/src/memory/lib/agent-ipc.mjs +93 -0
- package/src/memory/lib/bridge-trace-queries.mjs +120 -0
- package/src/memory/lib/core-memory-store.mjs +330 -0
- package/src/memory/lib/embedding-provider.mjs +269 -0
- package/src/memory/lib/embedding-worker.mjs +323 -0
- package/src/memory/lib/llm-worker-host.mjs +17 -0
- package/src/memory/lib/memory-cycle.mjs +11 -0
- package/src/memory/lib/memory-cycle1.mjs +641 -0
- package/src/memory/lib/memory-cycle2.mjs +1284 -0
- package/src/memory/lib/memory-cycle3.mjs +540 -0
- package/src/memory/lib/memory-embed.mjs +299 -0
- package/src/memory/lib/memory-extraction.mjs +5 -0
- package/src/memory/lib/memory-maintenance-store.mjs +32 -0
- package/src/memory/lib/memory-ops-policy.mjs +190 -0
- package/src/memory/lib/memory-recall-id-patch.mjs +15 -0
- package/src/memory/lib/memory-recall-read-query.mjs +7 -0
- package/src/memory/lib/memory-recall-scope-filter.mjs +63 -0
- package/src/memory/lib/memory-recall-store.mjs +621 -0
- package/src/memory/lib/memory-retrievers.mjs +112 -0
- package/src/memory/lib/memory-score.mjs +71 -0
- package/src/memory/lib/memory-text-utils.mjs +58 -0
- package/src/memory/lib/memory.mjs +412 -0
- package/src/memory/lib/model-profile.mjs +85 -0
- package/src/memory/lib/pg/adapter.mjs +308 -0
- package/src/memory/lib/pg/process.mjs +360 -0
- package/src/memory/lib/pg/supervisor.mjs +396 -0
- package/src/memory/lib/project-id-resolver.mjs +86 -0
- package/src/memory/lib/runtime-fetcher.mjs +442 -0
- package/src/memory/lib/trace-store.mjs +728 -0
- package/src/memory/tool-defs.mjs +79 -0
- package/src/search/index.mjs +1173 -0
- package/src/search/lib/backends/anthropic-oauth.mjs +98 -0
- package/src/search/lib/backends/exa.mjs +50 -0
- package/src/search/lib/backends/firecrawl.mjs +61 -0
- package/src/search/lib/backends/gemini-api.mjs +83 -0
- package/src/search/lib/backends/grok-oauth.mjs +86 -0
- package/src/search/lib/backends/index.mjs +150 -0
- package/src/search/lib/backends/openai-api.mjs +144 -0
- package/src/search/lib/backends/openai-oauth.mjs +98 -0
- package/src/search/lib/backends/openai-web-search.mjs +76 -0
- package/src/search/lib/backends/tavily.mjs +55 -0
- package/src/search/lib/backends/xai-api.mjs +113 -0
- package/src/search/lib/cache.mjs +131 -0
- package/src/search/lib/config.mjs +192 -0
- package/src/search/lib/formatter.mjs +115 -0
- package/src/search/lib/provider-usage.mjs +67 -0
- package/src/search/lib/providers.mjs +47 -0
- package/src/search/lib/search-intent.mjs +109 -0
- package/src/search/lib/setup-handler.mjs +261 -0
- package/src/search/lib/state.mjs +201 -0
- package/src/search/lib/web-tools.mjs +1207 -0
- package/src/search/tool-defs.mjs +83 -0
- package/src/setup/defender-exclusion.mjs +183 -0
- package/src/shared/abort-controller.mjs +15 -0
- package/src/shared/atomic-file.mjs +420 -0
- package/src/shared/config.mjs +350 -0
- package/src/shared/daemon-recycle.mjs +108 -0
- package/src/shared/disable-claude-builtins.mjs +88 -0
- package/src/shared/err-text.mjs +12 -0
- package/src/shared/llm/cost.mjs +66 -0
- package/src/shared/llm/http-agent.mjs +123 -0
- package/src/shared/llm/index.mjs +41 -0
- package/src/shared/llm/pid-cleanup.mjs +27 -0
- package/src/shared/llm/usage-log.mjs +47 -0
- package/src/shared/plugin-paths.mjs +58 -0
- package/src/shared/schedules-store.mjs +70 -0
- package/src/shared/seed.mjs +119 -0
- package/src/shared/user-cwd.mjs +213 -0
- package/src/shared/user-data-guard.mjs +238 -0
- package/src/status/aggregator.mjs +584 -0
- package/src/status/server.mjs +413 -0
- package/tools.json +1653 -0
|
@@ -0,0 +1,728 @@
|
|
|
1
|
+
// trace-store.mjs — native-PG trace analytics store for mixdog 0.4.0.
|
|
2
|
+
// Uses pg-adapter (schema='trace') so trace_events live in the trace schema.
|
|
3
|
+
// Isolated from memory schema; shares the same PG instance.
|
|
4
|
+
|
|
5
|
+
import { ensurePgInstance, checkedConnect } from './pg/adapter.mjs'
|
|
6
|
+
import { resolve } from 'path'
|
|
7
|
+
|
|
8
|
+
const dbs = new Map()
|
|
9
|
+
const opening = new Map()
|
|
10
|
+
const partitionTimers = new Map()
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Schema bootstrap
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
// Guard: if a non-partitioned trace_events exists, drop it.
|
|
17
|
+
// Trace is regenerable observability data; drop on schema repair to avoid
|
|
18
|
+
// stale dependent pollution (indexes are NOT renamed with the table, so RENAME
|
|
19
|
+
// would leave old index names bound to the replaced table, making subsequent
|
|
20
|
+
// CREATE INDEX IF NOT EXISTS a no-op on the new partitioned root).
|
|
21
|
+
async function maybeDropLegacyTable(client) {
|
|
22
|
+
const r = await client.query(`
|
|
23
|
+
SELECT c.relname
|
|
24
|
+
FROM pg_class c
|
|
25
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
26
|
+
WHERE n.nspname = 'trace'
|
|
27
|
+
AND c.relname = 'trace_events'
|
|
28
|
+
AND c.relkind = 'r' -- ordinary (non-partitioned) table only
|
|
29
|
+
AND c.oid NOT IN (
|
|
30
|
+
SELECT partrelid FROM pg_partitioned_table
|
|
31
|
+
)
|
|
32
|
+
`)
|
|
33
|
+
if (r.rows.length > 0) {
|
|
34
|
+
await client.query(`DROP TABLE trace.trace_events CASCADE`)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function init(client) {
|
|
39
|
+
await maybeDropLegacyTable(client)
|
|
40
|
+
|
|
41
|
+
// Partitioned root.
|
|
42
|
+
// PK is (id, ts) because PostgreSQL requires the partition key (ts) to be
|
|
43
|
+
// part of every unique/primary constraint on a partitioned table.
|
|
44
|
+
// BIGSERIAL is kept (vs GENERATED ALWAYS AS IDENTITY) for compatibility with
|
|
45
|
+
// the existing pg-adapter helpers that may inspect sequence names.
|
|
46
|
+
await client.query(`
|
|
47
|
+
CREATE TABLE IF NOT EXISTS trace_events (
|
|
48
|
+
id BIGSERIAL,
|
|
49
|
+
ts BIGINT NOT NULL,
|
|
50
|
+
session_id TEXT,
|
|
51
|
+
iteration INTEGER,
|
|
52
|
+
kind TEXT NOT NULL,
|
|
53
|
+
role TEXT,
|
|
54
|
+
model TEXT,
|
|
55
|
+
tool_name TEXT,
|
|
56
|
+
tool_ms INTEGER,
|
|
57
|
+
input_tokens INTEGER,
|
|
58
|
+
output_tokens INTEGER,
|
|
59
|
+
cached_tokens INTEGER,
|
|
60
|
+
cache_write_tokens INTEGER,
|
|
61
|
+
duration_ms INTEGER,
|
|
62
|
+
error_message TEXT,
|
|
63
|
+
payload JSONB NOT NULL,
|
|
64
|
+
parent_span_id BIGINT,
|
|
65
|
+
entry_id BIGINT,
|
|
66
|
+
PRIMARY KEY (id, ts)
|
|
67
|
+
) PARTITION BY RANGE (ts)
|
|
68
|
+
`)
|
|
69
|
+
|
|
70
|
+
// Default catch-all partition — ensures no INSERT is lost even before named
|
|
71
|
+
// monthly partitions exist. Rows here cannot be auto-rerouted once a covering
|
|
72
|
+
// partition is added, so ensureCurrentAndNextMonthPartitions() is called on
|
|
73
|
+
// every boot to pre-create upcoming months before rollover.
|
|
74
|
+
await client.query(`
|
|
75
|
+
CREATE TABLE IF NOT EXISTS trace_events_default
|
|
76
|
+
PARTITION OF trace_events DEFAULT
|
|
77
|
+
`)
|
|
78
|
+
|
|
79
|
+
// Previous-month partition — created in init only (not on every boot).
|
|
80
|
+
await client.query(`
|
|
81
|
+
DO $$
|
|
82
|
+
DECLARE
|
|
83
|
+
prev_start BIGINT := extract(epoch from date_trunc('month', now() - interval '1 month'))::BIGINT * 1000;
|
|
84
|
+
prev_end BIGINT := extract(epoch from date_trunc('month', now()))::BIGINT * 1000;
|
|
85
|
+
prev_name TEXT := 'trace_events_' || to_char(now() - interval '1 month', 'YYYY_MM');
|
|
86
|
+
BEGIN
|
|
87
|
+
IF NOT EXISTS (
|
|
88
|
+
SELECT 1 FROM pg_class c
|
|
89
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
90
|
+
WHERE n.nspname = 'trace' AND c.relname = prev_name
|
|
91
|
+
) THEN
|
|
92
|
+
EXECUTE format(
|
|
93
|
+
'CREATE TABLE %I PARTITION OF trace_events FOR VALUES FROM (%s) TO (%s)',
|
|
94
|
+
prev_name, prev_start, prev_end
|
|
95
|
+
);
|
|
96
|
+
END IF;
|
|
97
|
+
END $$
|
|
98
|
+
`)
|
|
99
|
+
|
|
100
|
+
// BRIN on ts — ~1000× smaller than btree for append-only timeseries; ideal
|
|
101
|
+
// for time-window range scans where rows arrive in roughly ts order.
|
|
102
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_ts_brin ON trace_events USING BRIN (ts) WITH (pages_per_range = 32)`)
|
|
103
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_kind_ts ON trace_events(kind, ts DESC)`)
|
|
104
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_session ON trace_events(session_id, ts)`)
|
|
105
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_role_ts ON trace_events(role, ts DESC)`)
|
|
106
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_model_ts ON trace_events(model, ts DESC)`)
|
|
107
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_tool ON trace_events(tool_name) WHERE kind = 'tool'`)
|
|
108
|
+
// Span-tree and cross-schema recall↔trace correlation — partial indexes so
|
|
109
|
+
// they cover only the (small) fraction of rows where these FKs are set.
|
|
110
|
+
// No FK constraints: self-FKs on partitioned tables are fragile, and
|
|
111
|
+
// entry_id crosses schema boundaries (memory.entries).
|
|
112
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_parent ON trace_events(parent_span_id) WHERE parent_span_id IS NOT NULL`)
|
|
113
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_trace_entry ON trace_events(entry_id) WHERE entry_id IS NOT NULL`)
|
|
114
|
+
|
|
115
|
+
// Current + next month created on every boot (see ensureCurrentAndNextMonthPartitions).
|
|
116
|
+
await ensureCurrentAndNextMonthPartitions(client)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
// Bridge-specific analytic tables (added post-init via initBridgeTables)
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
// Called once per openTraceDatabase boot (after the advisory lock is released).
|
|
124
|
+
// Safe to call concurrently — all DDL is IF NOT EXISTS.
|
|
125
|
+
export async function initBridgeTables(client) {
|
|
126
|
+
// ── bridge_calls: one row per tool invocation ─────────────────────────────
|
|
127
|
+
await client.query(`
|
|
128
|
+
CREATE TABLE IF NOT EXISTS bridge_calls (
|
|
129
|
+
id BIGSERIAL PRIMARY KEY,
|
|
130
|
+
session_id TEXT NOT NULL,
|
|
131
|
+
iteration INT,
|
|
132
|
+
ts TIMESTAMPTZ NOT NULL,
|
|
133
|
+
tool_name TEXT,
|
|
134
|
+
tool_kind TEXT,
|
|
135
|
+
tool_ms INT,
|
|
136
|
+
tool_args JSONB
|
|
137
|
+
)
|
|
138
|
+
`)
|
|
139
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bc_session ON bridge_calls (session_id, iteration)`)
|
|
140
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bc_ts ON bridge_calls USING BRIN (ts)`)
|
|
141
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bc_tool_name ON bridge_calls (tool_name)`)
|
|
142
|
+
// Expression indexes covering the two actual query patterns (md5 dedup + path lookup).
|
|
143
|
+
// The old GIN index had no @> callers and was write-heavy; dropped in favour of these.
|
|
144
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bc_args_md5 ON bridge_calls (session_id, tool_name, md5(tool_args::text))`)
|
|
145
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bc_args_path ON bridge_calls (session_id, tool_name, (tool_args->>'path'), ts, id)`)
|
|
146
|
+
await client.query(`DROP INDEX IF EXISTS idx_bc_args`)
|
|
147
|
+
|
|
148
|
+
// ── bridge_llm: one row per LLM usage event ───────────────────────────────
|
|
149
|
+
await client.query(`
|
|
150
|
+
CREATE TABLE IF NOT EXISTS bridge_llm (
|
|
151
|
+
id BIGSERIAL PRIMARY KEY,
|
|
152
|
+
session_id TEXT NOT NULL,
|
|
153
|
+
iteration INT,
|
|
154
|
+
ts TIMESTAMPTZ NOT NULL,
|
|
155
|
+
model TEXT,
|
|
156
|
+
input_tokens INT,
|
|
157
|
+
output_tokens INT,
|
|
158
|
+
cached_tokens INT,
|
|
159
|
+
cache_write_tokens INT,
|
|
160
|
+
prompt_tokens INT,
|
|
161
|
+
response_id TEXT
|
|
162
|
+
)
|
|
163
|
+
`)
|
|
164
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bl_session ON bridge_llm (session_id, iteration)`)
|
|
165
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bl_ts ON bridge_llm USING BRIN (ts)`)
|
|
166
|
+
await client.query(`CREATE INDEX IF NOT EXISTS idx_bl_model ON bridge_llm (model)`)
|
|
167
|
+
|
|
168
|
+
// ── bridge_sessions: denormalised summary upserted on each insert ─────────
|
|
169
|
+
await client.query(`
|
|
170
|
+
CREATE TABLE IF NOT EXISTS bridge_sessions (
|
|
171
|
+
session_id TEXT PRIMARY KEY,
|
|
172
|
+
role TEXT,
|
|
173
|
+
model TEXT,
|
|
174
|
+
started_at TIMESTAMPTZ,
|
|
175
|
+
last_seen_at TIMESTAMPTZ,
|
|
176
|
+
tool_calls INT NOT NULL DEFAULT 0,
|
|
177
|
+
llm_calls INT NOT NULL DEFAULT 0,
|
|
178
|
+
max_iteration INT NOT NULL DEFAULT 0,
|
|
179
|
+
total_input_tokens BIGINT NOT NULL DEFAULT 0,
|
|
180
|
+
total_output_tokens BIGINT NOT NULL DEFAULT 0
|
|
181
|
+
)
|
|
182
|
+
`)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
// insertBridgeCalls — batch insert tool rows + upsert session summary
|
|
187
|
+
// ---------------------------------------------------------------------------
|
|
188
|
+
const TOOL_ARGS_MAX_BYTES = 65536 // 64 KB cap; oversized → sha256 + truncated preview
|
|
189
|
+
|
|
190
|
+
import { createHash as _createHash } from 'crypto'
|
|
191
|
+
function _capToolArgsSync(args) {
|
|
192
|
+
if (args == null) return null
|
|
193
|
+
const raw = typeof args === 'string' ? args : JSON.stringify(args)
|
|
194
|
+
if (Buffer.byteLength(raw, 'utf8') <= TOOL_ARGS_MAX_BYTES) {
|
|
195
|
+
if (typeof args !== 'string') return args
|
|
196
|
+
// tool_args is JSONB in PG; round-trip parse for string inputs, but a
|
|
197
|
+
// plain non-JSON string (e.g. a bare path) would otherwise throw and
|
|
198
|
+
// fail the whole insert batch. Treat unparseable as the raw string.
|
|
199
|
+
try { return JSON.parse(raw) } catch { return raw }
|
|
200
|
+
}
|
|
201
|
+
return { _oversized: true, sha256: _createHash('sha256').update(raw).digest('hex'), preview: raw.slice(0, 512) }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export async function insertBridgeCalls(db, events) {
|
|
205
|
+
if (!Array.isArray(events) || events.length === 0) return { calls: 0, llm: 0 }
|
|
206
|
+
const toolRows = []
|
|
207
|
+
const llmRows = []
|
|
208
|
+
for (const ev of events) {
|
|
209
|
+
let ts = ev.ts; if (typeof ts === 'string') ts = Date.parse(ts); ts = Number(ts); if (!Number.isFinite(ts)) ts = Date.now()
|
|
210
|
+
const tsIso = new Date(ts).toISOString()
|
|
211
|
+
const sid = ev.session_id ?? ev.sessionId ?? null
|
|
212
|
+
if (!sid) continue
|
|
213
|
+
const iter = ev.iteration != null ? Number(ev.iteration) : null
|
|
214
|
+
if (ev.kind === 'tool') {
|
|
215
|
+
const tool_name = ev.tool_name ?? ev.toolName ?? null
|
|
216
|
+
const tool_kind = ev.tool_kind ?? ev.toolKind ?? null
|
|
217
|
+
const tool_ms = ev.tool_ms ?? ev.toolMs ?? null
|
|
218
|
+
const tool_args = ev.tool_args ?? ev.toolArgs ?? null
|
|
219
|
+
toolRows.push({ session_id: sid, iteration: iter, ts: tsIso, tool_name, tool_kind, tool_ms: tool_ms != null ? Number(tool_ms) : null, tool_args: _capToolArgsSync(tool_args) })
|
|
220
|
+
} else if (ev.kind === 'usage_raw' || (ev.input_tokens != null && ev.output_tokens != null)) {
|
|
221
|
+
llmRows.push({ session_id: sid, iteration: iter, ts: tsIso, model: ev.model ?? null,
|
|
222
|
+
input_tokens: ev.input_tokens ?? ev.inputTokens ?? null,
|
|
223
|
+
output_tokens: ev.output_tokens ?? ev.outputTokens ?? null,
|
|
224
|
+
cached_tokens: ev.cached_tokens ?? ev.cachedTokens ?? null,
|
|
225
|
+
cache_write_tokens: ev.cache_write_tokens ?? ev.cacheWriteTokens ?? null,
|
|
226
|
+
prompt_tokens: ev.prompt_tokens ?? ev.promptTokens ?? null,
|
|
227
|
+
response_id: ev.response_id ?? ev.responseId ?? null,
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Wrap all three inserts in a single transaction — one flush/fsync.
|
|
233
|
+
// checkedConnect ensures search_path = trace, public on fresh connections;
|
|
234
|
+
// raw _pool.connect() leaves search_path at PG default and bridge_* lookups
|
|
235
|
+
// resolve in the wrong schema.
|
|
236
|
+
const client = await checkedConnect(db._pool, 'trace')
|
|
237
|
+
try {
|
|
238
|
+
await client.query('BEGIN')
|
|
239
|
+
|
|
240
|
+
if (toolRows.length > 0) {
|
|
241
|
+
await client.query(
|
|
242
|
+
`INSERT INTO bridge_calls (session_id,iteration,ts,tool_name,tool_kind,tool_ms,tool_args)
|
|
243
|
+
SELECT u.session_id, u.iteration::int, u.ts::timestamptz,
|
|
244
|
+
u.tool_name, u.tool_kind, u.tool_ms::int, u.tool_args::jsonb
|
|
245
|
+
FROM unnest($1::text[],$2::int[],$3::text[],$4::text[],$5::text[],$6::int[],$7::text[])
|
|
246
|
+
AS u(session_id,iteration,ts,tool_name,tool_kind,tool_ms,tool_args)`,
|
|
247
|
+
[
|
|
248
|
+
toolRows.map(r => r.session_id),
|
|
249
|
+
toolRows.map(r => r.iteration),
|
|
250
|
+
toolRows.map(r => r.ts),
|
|
251
|
+
toolRows.map(r => r.tool_name),
|
|
252
|
+
toolRows.map(r => r.tool_kind),
|
|
253
|
+
toolRows.map(r => r.tool_ms),
|
|
254
|
+
toolRows.map(r => r.tool_args != null ? JSON.stringify(r.tool_args) : null),
|
|
255
|
+
],
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (llmRows.length > 0) {
|
|
260
|
+
await client.query(
|
|
261
|
+
`INSERT INTO bridge_llm (session_id,iteration,ts,model,input_tokens,output_tokens,cached_tokens,cache_write_tokens,prompt_tokens,response_id)
|
|
262
|
+
SELECT u.session_id, u.iteration::int, u.ts::timestamptz,
|
|
263
|
+
u.model, u.input_tokens::int, u.output_tokens::int,
|
|
264
|
+
u.cached_tokens::int, u.cache_write_tokens::int,
|
|
265
|
+
u.prompt_tokens::int, u.response_id
|
|
266
|
+
FROM unnest($1::text[],$2::int[],$3::text[],$4::text[],$5::int[],$6::int[],$7::int[],$8::int[],$9::int[],$10::text[])
|
|
267
|
+
AS u(session_id,iteration,ts,model,input_tokens,output_tokens,cached_tokens,cache_write_tokens,prompt_tokens,response_id)`,
|
|
268
|
+
[
|
|
269
|
+
llmRows.map(r => r.session_id),
|
|
270
|
+
llmRows.map(r => r.iteration),
|
|
271
|
+
llmRows.map(r => r.ts),
|
|
272
|
+
llmRows.map(r => r.model),
|
|
273
|
+
llmRows.map(r => r.input_tokens),
|
|
274
|
+
llmRows.map(r => r.output_tokens),
|
|
275
|
+
llmRows.map(r => r.cached_tokens),
|
|
276
|
+
llmRows.map(r => r.cache_write_tokens),
|
|
277
|
+
llmRows.map(r => r.prompt_tokens),
|
|
278
|
+
llmRows.map(r => r.response_id),
|
|
279
|
+
],
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Upsert session summaries — accumulate from tool+llm rows in this batch
|
|
284
|
+
const sessionMap = new Map()
|
|
285
|
+
for (const r of toolRows) {
|
|
286
|
+
const s = sessionMap.get(r.session_id) ?? { tool_calls: 0, llm_calls: 0, max_iteration: 0, total_input: 0n, total_output: 0n, ts0: r.ts, ts1: r.ts, role: null, model: null }
|
|
287
|
+
s.tool_calls += 1
|
|
288
|
+
if (r.iteration != null && r.iteration > s.max_iteration) s.max_iteration = r.iteration
|
|
289
|
+
if (r.ts < s.ts0) s.ts0 = r.ts; if (r.ts > s.ts1) s.ts1 = r.ts
|
|
290
|
+
sessionMap.set(r.session_id, s)
|
|
291
|
+
}
|
|
292
|
+
for (const r of llmRows) {
|
|
293
|
+
const s = sessionMap.get(r.session_id) ?? { tool_calls: 0, llm_calls: 0, max_iteration: 0, total_input: 0n, total_output: 0n, ts0: r.ts, ts1: r.ts, role: null, model: null }
|
|
294
|
+
s.llm_calls += 1
|
|
295
|
+
s.total_input += BigInt(r.input_tokens ?? 0)
|
|
296
|
+
s.total_output += BigInt(r.output_tokens ?? 0)
|
|
297
|
+
if (r.model) s.model = r.model
|
|
298
|
+
if (r.iteration != null && r.iteration > s.max_iteration) s.max_iteration = r.iteration
|
|
299
|
+
if (r.ts < s.ts0) s.ts0 = r.ts; if (r.ts > s.ts1) s.ts1 = r.ts
|
|
300
|
+
sessionMap.set(r.session_id, s)
|
|
301
|
+
}
|
|
302
|
+
// Also pick up role from preset_assign events in the same batch
|
|
303
|
+
for (const ev of events) {
|
|
304
|
+
if (ev.kind === 'preset_assign' && ev.role) {
|
|
305
|
+
const sid = ev.session_id ?? ev.sessionId ?? null
|
|
306
|
+
if (!sid) continue
|
|
307
|
+
const s = sessionMap.get(sid)
|
|
308
|
+
if (s) s.role = ev.role
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// Fix 5 — upsert sessions for preset_assign-only batches (no tool/llm rows yet)
|
|
312
|
+
for (const ev of events) {
|
|
313
|
+
if (ev.kind !== 'preset_assign') continue
|
|
314
|
+
const sid = ev.session_id ?? ev.sessionId ?? null
|
|
315
|
+
if (!sid) continue
|
|
316
|
+
if (sessionMap.has(sid)) continue // already populated from tool/llm rows above
|
|
317
|
+
let ts = ev.ts; if (typeof ts === 'string') ts = Date.parse(ts); ts = Number(ts); if (!Number.isFinite(ts)) ts = Date.now()
|
|
318
|
+
const tsIso = new Date(ts).toISOString()
|
|
319
|
+
sessionMap.set(sid, {
|
|
320
|
+
tool_calls: 0, llm_calls: 0, max_iteration: 0,
|
|
321
|
+
total_input: 0n, total_output: 0n,
|
|
322
|
+
ts0: tsIso, ts1: tsIso,
|
|
323
|
+
role: ev.role ?? null, model: ev.model ?? null,
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Coalesce bridge_sessions upserts: batch all sessions in one unnest INSERT.
|
|
328
|
+
// Also within the same transaction.
|
|
329
|
+
if (sessionMap.size > 0) {
|
|
330
|
+
const sids = [], roles = [], models = [], ts0s = [], ts1s = [],
|
|
331
|
+
tcalls = [], lcalls = [], maxiters = [], tinputs = [], toutputs = []
|
|
332
|
+
for (const [sid, s] of sessionMap) {
|
|
333
|
+
sids.push(sid); roles.push(s.role); models.push(s.model)
|
|
334
|
+
ts0s.push(s.ts0); ts1s.push(s.ts1)
|
|
335
|
+
tcalls.push(s.tool_calls); lcalls.push(s.llm_calls)
|
|
336
|
+
maxiters.push(s.max_iteration)
|
|
337
|
+
tinputs.push(String(s.total_input)); toutputs.push(String(s.total_output))
|
|
338
|
+
}
|
|
339
|
+
await client.query(`
|
|
340
|
+
INSERT INTO bridge_sessions (session_id, role, model, started_at, last_seen_at, tool_calls, llm_calls, max_iteration, total_input_tokens, total_output_tokens)
|
|
341
|
+
SELECT u.session_id, u.role, u.model,
|
|
342
|
+
u.started_at::timestamptz, u.last_seen_at::timestamptz,
|
|
343
|
+
u.tool_calls::int, u.llm_calls::int, u.max_iteration::int,
|
|
344
|
+
u.total_input_tokens::bigint, u.total_output_tokens::bigint
|
|
345
|
+
FROM unnest($1::text[],$2::text[],$3::text[],$4::text[],$5::text[],$6::int[],$7::int[],$8::int[],$9::text[],$10::text[])
|
|
346
|
+
AS u(session_id,role,model,started_at,last_seen_at,tool_calls,llm_calls,max_iteration,total_input_tokens,total_output_tokens)
|
|
347
|
+
ON CONFLICT (session_id) DO UPDATE SET
|
|
348
|
+
role = COALESCE(EXCLUDED.role, bridge_sessions.role),
|
|
349
|
+
model = COALESCE(EXCLUDED.model, bridge_sessions.model),
|
|
350
|
+
started_at = LEAST(bridge_sessions.started_at, EXCLUDED.started_at),
|
|
351
|
+
last_seen_at = GREATEST(bridge_sessions.last_seen_at, EXCLUDED.last_seen_at),
|
|
352
|
+
tool_calls = bridge_sessions.tool_calls + EXCLUDED.tool_calls,
|
|
353
|
+
llm_calls = bridge_sessions.llm_calls + EXCLUDED.llm_calls,
|
|
354
|
+
max_iteration = GREATEST(bridge_sessions.max_iteration, EXCLUDED.max_iteration),
|
|
355
|
+
total_input_tokens = bridge_sessions.total_input_tokens + EXCLUDED.total_input_tokens,
|
|
356
|
+
total_output_tokens = bridge_sessions.total_output_tokens + EXCLUDED.total_output_tokens
|
|
357
|
+
`, [sids, roles, models, ts0s, ts1s, tcalls, lcalls, maxiters, tinputs, toutputs])
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
await client.query('COMMIT')
|
|
361
|
+
} catch (err) {
|
|
362
|
+
try { await client.query('ROLLBACK') } catch {}
|
|
363
|
+
throw err
|
|
364
|
+
} finally {
|
|
365
|
+
client.release()
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return { calls: toolRows.length, llm: llmRows.length }
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// Idempotently ensure partitions exist for the current and next calendar month.
|
|
372
|
+
// Called from both init() and openTraceDatabase() so the next-month partition
|
|
373
|
+
// is always pre-created before rollover; rows never land in the default partition
|
|
374
|
+
// for a range that has a covering monthly partition.
|
|
375
|
+
async function ensureCurrentAndNextMonthPartitions(client) {
|
|
376
|
+
await client.query(`
|
|
377
|
+
DO $$
|
|
378
|
+
DECLARE
|
|
379
|
+
cur_start BIGINT := extract(epoch from date_trunc('month', now()))::BIGINT * 1000;
|
|
380
|
+
cur_end BIGINT := extract(epoch from date_trunc('month', now() + interval '1 month'))::BIGINT * 1000;
|
|
381
|
+
next_start BIGINT := extract(epoch from date_trunc('month', now() + interval '1 month'))::BIGINT * 1000;
|
|
382
|
+
next_end BIGINT := extract(epoch from date_trunc('month', now() + interval '2 months'))::BIGINT * 1000;
|
|
383
|
+
cur_name TEXT := 'trace_events_' || to_char(now(), 'YYYY_MM');
|
|
384
|
+
next_name TEXT := 'trace_events_' || to_char(now() + interval '1 month', 'YYYY_MM');
|
|
385
|
+
BEGIN
|
|
386
|
+
IF NOT EXISTS (
|
|
387
|
+
SELECT 1 FROM pg_class c
|
|
388
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
389
|
+
WHERE n.nspname = 'trace' AND c.relname = cur_name
|
|
390
|
+
) THEN
|
|
391
|
+
EXECUTE format(
|
|
392
|
+
'CREATE TABLE %I PARTITION OF trace_events FOR VALUES FROM (%s) TO (%s)',
|
|
393
|
+
cur_name, cur_start, cur_end
|
|
394
|
+
);
|
|
395
|
+
END IF;
|
|
396
|
+
IF NOT EXISTS (
|
|
397
|
+
SELECT 1 FROM pg_class c
|
|
398
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
399
|
+
WHERE n.nspname = 'trace' AND c.relname = next_name
|
|
400
|
+
) THEN
|
|
401
|
+
EXECUTE format(
|
|
402
|
+
'CREATE TABLE %I PARTITION OF trace_events FOR VALUES FROM (%s) TO (%s)',
|
|
403
|
+
next_name, next_start, next_end
|
|
404
|
+
);
|
|
405
|
+
END IF;
|
|
406
|
+
END $$
|
|
407
|
+
`)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function isBootstrapComplete(client) {
|
|
411
|
+
try {
|
|
412
|
+
// Harden the check: require (a) root is partitioned, (b) schema-version
|
|
413
|
+
// columns parent_span_id and entry_id exist, (c) idx_trace_ts_brin exists,
|
|
414
|
+
// (d) at least one named-month OR default partition exists.
|
|
415
|
+
// A partial failed init (crashed after CREATE TABLE, before indexes) returns
|
|
416
|
+
// false here, triggering a full re-run rather than silently booting broken.
|
|
417
|
+
const r = await client.query(`
|
|
418
|
+
SELECT 1 WHERE
|
|
419
|
+
-- (a) partitioned root exists
|
|
420
|
+
EXISTS (
|
|
421
|
+
SELECT 1 FROM pg_class c
|
|
422
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
423
|
+
JOIN pg_partitioned_table pt ON pt.partrelid = c.oid
|
|
424
|
+
WHERE n.nspname = 'trace' AND c.relname = 'trace_events'
|
|
425
|
+
)
|
|
426
|
+
-- (b) schema-version columns present
|
|
427
|
+
AND EXISTS (
|
|
428
|
+
SELECT 1 FROM pg_attribute a
|
|
429
|
+
JOIN pg_class c ON c.oid = a.attrelid
|
|
430
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
431
|
+
WHERE n.nspname = 'trace' AND c.relname = 'trace_events'
|
|
432
|
+
AND a.attname IN ('parent_span_id', 'entry_id')
|
|
433
|
+
AND a.attnum > 0 AND NOT a.attisdropped
|
|
434
|
+
HAVING count(*) = 2
|
|
435
|
+
)
|
|
436
|
+
-- (c) BRIN index exists
|
|
437
|
+
AND EXISTS (
|
|
438
|
+
SELECT 1 FROM pg_indexes
|
|
439
|
+
WHERE schemaname = 'trace' AND indexname = 'idx_trace_ts_brin'
|
|
440
|
+
)
|
|
441
|
+
-- (d) at least one partition (named-month or default catch-all) exists
|
|
442
|
+
AND EXISTS (
|
|
443
|
+
SELECT 1 FROM pg_inherits i
|
|
444
|
+
JOIN pg_class cp ON cp.oid = i.inhparent
|
|
445
|
+
JOIN pg_namespace n ON n.oid = cp.relnamespace
|
|
446
|
+
WHERE n.nspname = 'trace' AND cp.relname = 'trace_events'
|
|
447
|
+
)
|
|
448
|
+
`)
|
|
449
|
+
return r.rows.length > 0
|
|
450
|
+
} catch {
|
|
451
|
+
return false
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Advisory lock key — session-scoped, prevents cross-process bootstrap races.
|
|
456
|
+
const BOOTSTRAP_LOCK_KEY = `hashtext('mixdog.trace_bootstrap')`
|
|
457
|
+
|
|
458
|
+
// ---------------------------------------------------------------------------
|
|
459
|
+
// openTraceDatabase
|
|
460
|
+
// ---------------------------------------------------------------------------
|
|
461
|
+
|
|
462
|
+
export async function openTraceDatabase(dataDir) {
|
|
463
|
+
const key = resolve(dataDir)
|
|
464
|
+
|
|
465
|
+
if (dbs.get(key)) return dbs.get(key)
|
|
466
|
+
if (opening.has(key)) return opening.get(key)
|
|
467
|
+
|
|
468
|
+
const promise = (async () => {
|
|
469
|
+
// pg-adapter with schema='trace' sets search_path=trace,public per connection.
|
|
470
|
+
const { db } = await ensurePgInstance(dataDir, { schema: 'trace' })
|
|
471
|
+
|
|
472
|
+
// Acquire a dedicated pool client for bootstrap so the advisory lock is
|
|
473
|
+
// session-scoped to exactly one connection (advisory locks are per-session).
|
|
474
|
+
// The client is released in finally — in both success and error paths.
|
|
475
|
+
const client = await db._pool.connect()
|
|
476
|
+
try {
|
|
477
|
+
// Set search_path on the dedicated client to match the pool default.
|
|
478
|
+
await client.query(`SET search_path = trace, public`)
|
|
479
|
+
// Session-scoped advisory lock — bounded so a stuck holder can't hang
|
|
480
|
+
// boot indefinitely. Try non-blocking first; on contention, set a
|
|
481
|
+
// 30s lock_timeout for the blocking acquire and surface a clear error
|
|
482
|
+
// instead of an unbounded wait.
|
|
483
|
+
const tryAcquire = await client.query(`SELECT pg_try_advisory_lock(${BOOTSTRAP_LOCK_KEY}) AS locked`)
|
|
484
|
+
if (!tryAcquire.rows[0]?.locked) {
|
|
485
|
+
// SET LOCAL only persists inside an explicit transaction — without
|
|
486
|
+
// BEGIN/COMMIT PG resets it immediately, so pg_advisory_lock() would
|
|
487
|
+
// wait unbounded. Wrap the lock_timeout + blocking acquire so the
|
|
488
|
+
// 30s ceiling actually applies.
|
|
489
|
+
await client.query('BEGIN')
|
|
490
|
+
try {
|
|
491
|
+
await client.query(`SET LOCAL lock_timeout = '30s'`)
|
|
492
|
+
await client.query(`SELECT pg_advisory_lock(${BOOTSTRAP_LOCK_KEY})`)
|
|
493
|
+
await client.query('COMMIT')
|
|
494
|
+
} catch (err) {
|
|
495
|
+
try { await client.query('ROLLBACK') } catch {}
|
|
496
|
+
// lock_timeout fires as 55P03 (lock_not_available); surface with context.
|
|
497
|
+
throw new Error(`trace-store bootstrap advisory lock timed out (30s): ${err?.message || err}`)
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
// Re-check after acquiring the lock: another process may have completed
|
|
502
|
+
// init while we were waiting.
|
|
503
|
+
if (!(await isBootstrapComplete(client))) {
|
|
504
|
+
await init(client)
|
|
505
|
+
} else {
|
|
506
|
+
// Init already done by another process; still ensure upcoming partitions
|
|
507
|
+
// are pre-created for this boot.
|
|
508
|
+
await ensureCurrentAndNextMonthPartitions(client)
|
|
509
|
+
}
|
|
510
|
+
} finally {
|
|
511
|
+
await client.query(`SELECT pg_advisory_unlock(${BOOTSTRAP_LOCK_KEY})`)
|
|
512
|
+
}
|
|
513
|
+
} finally {
|
|
514
|
+
client.release()
|
|
515
|
+
}
|
|
516
|
+
// Bridge-specific analytic tables — idempotent, no advisory lock needed.
|
|
517
|
+
await initBridgeTables(db)
|
|
518
|
+
|
|
519
|
+
dbs.set(key, db)
|
|
520
|
+
|
|
521
|
+
// Periodically ensure current + next-month partitions exist so a
|
|
522
|
+
// long-running process never hits the default catch-all partition at
|
|
523
|
+
// month rollover. Fires every 12 h — well ahead of any boundary.
|
|
524
|
+
const _partitionEnsureInterval = setInterval(async () => {
|
|
525
|
+
const c = await db._pool.connect()
|
|
526
|
+
try {
|
|
527
|
+
await c.query(`SET search_path = trace, public`)
|
|
528
|
+
await ensureCurrentAndNextMonthPartitions(c)
|
|
529
|
+
} catch (err) {
|
|
530
|
+
process.stderr.write(`[trace-store] periodic partition ensure failed: ${err?.message ?? err}\n`)
|
|
531
|
+
} finally {
|
|
532
|
+
c.release()
|
|
533
|
+
}
|
|
534
|
+
}, 12 * 60 * 60 * 1000)
|
|
535
|
+
_partitionEnsureInterval.unref?.()
|
|
536
|
+
partitionTimers.set(key, _partitionEnsureInterval)
|
|
537
|
+
|
|
538
|
+
return db
|
|
539
|
+
})()
|
|
540
|
+
|
|
541
|
+
opening.set(key, promise)
|
|
542
|
+
try {
|
|
543
|
+
return await promise
|
|
544
|
+
} finally {
|
|
545
|
+
opening.delete(key)
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// ---------------------------------------------------------------------------
|
|
550
|
+
// insertTraceEvents — batch INSERT
|
|
551
|
+
// ---------------------------------------------------------------------------
|
|
552
|
+
|
|
553
|
+
const TRACE_COLS = [
|
|
554
|
+
'ts', 'session_id', 'iteration', 'kind', 'role', 'model',
|
|
555
|
+
'tool_name', 'tool_ms', 'input_tokens', 'output_tokens',
|
|
556
|
+
'cached_tokens', 'cache_write_tokens', 'duration_ms',
|
|
557
|
+
'error_message', 'payload', 'parent_span_id', 'entry_id',
|
|
558
|
+
]
|
|
559
|
+
|
|
560
|
+
// ---------------------------------------------------------------------------
|
|
561
|
+
// Cross-request trace_events write queue (100ms / 500-row flush window)
|
|
562
|
+
// ---------------------------------------------------------------------------
|
|
563
|
+
|
|
564
|
+
const TRACE_QUEUE_FLUSH_MS = 100
|
|
565
|
+
const TRACE_QUEUE_MAX_ROWS = 500
|
|
566
|
+
|
|
567
|
+
// Per-db queue state (keyed by db object identity via WeakMap).
|
|
568
|
+
const _traceQueues = new WeakMap()
|
|
569
|
+
|
|
570
|
+
function _getQueue(db) {
|
|
571
|
+
let q = _traceQueues.get(db)
|
|
572
|
+
if (!q) {
|
|
573
|
+
q = { pending: [], timer: null, flushPromise: null }
|
|
574
|
+
_traceQueues.set(db, q)
|
|
575
|
+
}
|
|
576
|
+
return q
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
async function _flushQueue(db, q) {
|
|
580
|
+
// When a flush is already running, callers reuse its promise — but enqueues
|
|
581
|
+
// that arrive during that flush schedule a fresh timer that fires here and
|
|
582
|
+
// is consumed by the early return. Reschedule a follow-up flush so events
|
|
583
|
+
// queued mid-flush don't sit until the next unrelated enqueue.
|
|
584
|
+
if (q.flushPromise) {
|
|
585
|
+
if (q.pending.length > 0) _scheduleFlush(db, q)
|
|
586
|
+
return q.flushPromise
|
|
587
|
+
}
|
|
588
|
+
const wrappers = q.pending.splice(0)
|
|
589
|
+
if (wrappers.length === 0) return { inserted: 0 }
|
|
590
|
+
const MAX_RETRIES = 3
|
|
591
|
+
q.flushPromise = (async () => {
|
|
592
|
+
try {
|
|
593
|
+
const allEvents = wrappers.flatMap(w => w.events)
|
|
594
|
+
return await _insertTraceEventsDirect(db, allEvents)
|
|
595
|
+
} catch (err) {
|
|
596
|
+
for (const w of wrappers) w.attempts += 1
|
|
597
|
+
const keep = wrappers.filter(w => w.attempts < MAX_RETRIES)
|
|
598
|
+
const dropped = wrappers.filter(w => w.attempts >= MAX_RETRIES)
|
|
599
|
+
if (dropped.length > 0) {
|
|
600
|
+
process.stderr.write(`[trace-queue] dropped ${dropped.reduce((n, w) => n + w.events.length, 0)} events after ${MAX_RETRIES} retries: ${err?.message}\n`)
|
|
601
|
+
}
|
|
602
|
+
q.pending.unshift(...keep)
|
|
603
|
+
process.stderr.write(`[trace-queue] flush error: ${err?.message}\n`)
|
|
604
|
+
throw err
|
|
605
|
+
} finally {
|
|
606
|
+
q.flushPromise = null
|
|
607
|
+
// Drain any events that landed in the queue while this flush ran.
|
|
608
|
+
if (q.pending.length > 0) _scheduleFlush(db, q)
|
|
609
|
+
}
|
|
610
|
+
})()
|
|
611
|
+
return q.flushPromise
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function _scheduleFlush(db, q) {
|
|
615
|
+
if (q.timer) return
|
|
616
|
+
q.timer = setTimeout(async () => {
|
|
617
|
+
q.timer = null
|
|
618
|
+
_flushQueue(db, q).catch(err =>
|
|
619
|
+
process.stderr.write(`[trace-queue] flush error: ${err?.message}\n`)
|
|
620
|
+
)
|
|
621
|
+
}, TRACE_QUEUE_FLUSH_MS)
|
|
622
|
+
q.timer.unref?.()
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// ---------------------------------------------------------------------------
|
|
626
|
+
// Exit drain — flush pending trace events before process exit.
|
|
627
|
+
// Issue 4: timer is unref()'d so it won't prevent exit; register drain handlers.
|
|
628
|
+
// ---------------------------------------------------------------------------
|
|
629
|
+
|
|
630
|
+
const _registeredExitDbs = new WeakSet()
|
|
631
|
+
|
|
632
|
+
export function registerTraceExitDrain(db) {
|
|
633
|
+
if (_registeredExitDbs.has(db)) return
|
|
634
|
+
_registeredExitDbs.add(db)
|
|
635
|
+
|
|
636
|
+
async function drainOnExit() {
|
|
637
|
+
const q = _traceQueues.get(db)
|
|
638
|
+
if (!q || q.pending.length === 0) return
|
|
639
|
+
try {
|
|
640
|
+
if (q.timer) { clearTimeout(q.timer); q.timer = null }
|
|
641
|
+
if (q.flushPromise) await q.flushPromise.catch(() => {})
|
|
642
|
+
if (q.pending.length > 0) await _insertTraceEventsDirect(db, q.pending.splice(0).flatMap(w => w.events))
|
|
643
|
+
} catch (e) {
|
|
644
|
+
process.stderr.write(`[trace-queue] exit drain failed: ${e?.message}\n`)
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
process.on('exit', () => {
|
|
649
|
+
const q = _traceQueues.get(db)
|
|
650
|
+
if (q?.pending.length) process.stderr.write(`[trace-queue] exit with ${q.pending.length} unflushed events\n`)
|
|
651
|
+
})
|
|
652
|
+
|
|
653
|
+
process.on('SIGTERM', async () => { await drainOnExit(); process.exit(0) })
|
|
654
|
+
|
|
655
|
+
process.once('beforeExit', drainOnExit)
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Enqueue events for async batched insert. Returns immediately; flush happens
|
|
660
|
+
* within TRACE_QUEUE_FLUSH_MS or when TRACE_QUEUE_MAX_ROWS is reached.
|
|
661
|
+
* Callers that need a synchronous result should use insertTraceEvents directly.
|
|
662
|
+
*/
|
|
663
|
+
export function enqueueTraceEvents(db, events) {
|
|
664
|
+
if (!Array.isArray(events) || events.length === 0) return
|
|
665
|
+
const q = _getQueue(db)
|
|
666
|
+
q.pending.push({ events: [...events], attempts: 0 })
|
|
667
|
+
// Row cap counts pending EVENTS, not wrappers — a single wrapper with
|
|
668
|
+
// >TRACE_QUEUE_MAX_ROWS events would otherwise sit until the timer.
|
|
669
|
+
const pendingEvents = q.pending.reduce((n, w) => n + w.events.length, 0)
|
|
670
|
+
if (pendingEvents >= TRACE_QUEUE_MAX_ROWS) {
|
|
671
|
+
// Flush immediately when row cap reached — don't wait for timer.
|
|
672
|
+
if (q.timer) { clearTimeout(q.timer); q.timer = null }
|
|
673
|
+
_flushQueue(db, q).catch(err =>
|
|
674
|
+
process.stderr.write(`[trace-queue] flush error: ${err?.message}\n`)
|
|
675
|
+
)
|
|
676
|
+
} else {
|
|
677
|
+
_scheduleFlush(db, q)
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// Renamed internal: direct DB insert without queuing (used by queue flusher and
|
|
682
|
+
// the existing intra-request multi-row path where immediate persistence matters).
|
|
683
|
+
async function _insertTraceEventsDirect(db, events) {
|
|
684
|
+
return insertTraceEvents(db, events)
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
export async function insertTraceEvents(db, events) {
|
|
688
|
+
if (!Array.isArray(events) || events.length === 0) return { inserted: 0 }
|
|
689
|
+
|
|
690
|
+
const valuePlaceholders = []
|
|
691
|
+
const params = []
|
|
692
|
+
let p = 1
|
|
693
|
+
|
|
694
|
+
for (const ev of events) {
|
|
695
|
+
let ts = ev.ts
|
|
696
|
+
if (typeof ts === 'string') ts = Date.parse(ts)
|
|
697
|
+
ts = Number(ts)
|
|
698
|
+
if (!Number.isFinite(ts)) ts = Date.now()
|
|
699
|
+
|
|
700
|
+
const payload = ev.payload != null ? ev.payload : {}
|
|
701
|
+
|
|
702
|
+
const cols = [
|
|
703
|
+
ts,
|
|
704
|
+
ev.session_id ?? null,
|
|
705
|
+
ev.iteration != null ? Number(ev.iteration) : null,
|
|
706
|
+
String(ev.kind ?? 'unknown'),
|
|
707
|
+
ev.role ?? null,
|
|
708
|
+
ev.model ?? null,
|
|
709
|
+
ev.tool_name ?? null,
|
|
710
|
+
ev.tool_ms != null ? Number(ev.tool_ms) : null,
|
|
711
|
+
ev.input_tokens != null ? Number(ev.input_tokens) : null,
|
|
712
|
+
ev.output_tokens != null ? Number(ev.output_tokens) : null,
|
|
713
|
+
ev.cached_tokens != null ? Number(ev.cached_tokens) : null,
|
|
714
|
+
ev.cache_write_tokens != null ? Number(ev.cache_write_tokens) : null,
|
|
715
|
+
ev.duration_ms != null ? Number(ev.duration_ms) : null,
|
|
716
|
+
ev.error_message ?? null,
|
|
717
|
+
typeof payload === 'string' ? payload : JSON.stringify(payload),
|
|
718
|
+
ev.parent_span_id != null ? Number(ev.parent_span_id) : null,
|
|
719
|
+
ev.entry_id != null ? Number(ev.entry_id) : null,
|
|
720
|
+
]
|
|
721
|
+
valuePlaceholders.push(`(${cols.map(() => `$${p++}`).join(', ')})`)
|
|
722
|
+
params.push(...cols)
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
const sql = `INSERT INTO trace_events (${TRACE_COLS.join(', ')}) VALUES ${valuePlaceholders.join(', ')}`
|
|
726
|
+
await db.query(sql, params)
|
|
727
|
+
return { inserted: events.length }
|
|
728
|
+
}
|