nox-mem 3.0.0
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/LICENSE +21 -0
- package/README.md +179 -0
- package/dist/api/answer.d.ts +116 -0
- package/dist/api/answer.js +274 -0
- package/dist/api/answer.validate-patch.d.ts +72 -0
- package/dist/api/answer.validate-patch.js +52 -0
- package/dist/api/brief-diversity.d.ts +64 -0
- package/dist/api/brief-diversity.js +91 -0
- package/dist/api/brief.d.ts +114 -0
- package/dist/api/brief.js +524 -0
- package/dist/api/conflict.d.ts +33 -0
- package/dist/api/conflict.js +143 -0
- package/dist/api/cors.d.ts +73 -0
- package/dist/api/cors.js +124 -0
- package/dist/api/events-stream-limited.d.ts +133 -0
- package/dist/api/events-stream-limited.js +217 -0
- package/dist/api/events-stream.d.ts +53 -0
- package/dist/api/events-stream.js +125 -0
- package/dist/api/export.d.ts +42 -0
- package/dist/api/export.js +85 -0
- package/dist/api/health-confidence-adapter.d.ts +29 -0
- package/dist/api/health-confidence-adapter.js +73 -0
- package/dist/api/health-confidence.d.ts +36 -0
- package/dist/api/health-confidence.js +141 -0
- package/dist/api/hooks.d.ts +50 -0
- package/dist/api/hooks.js +115 -0
- package/dist/api/import.d.ts +38 -0
- package/dist/api/import.js +87 -0
- package/dist/api/ingest-event.d.ts +65 -0
- package/dist/api/ingest-event.js +153 -0
- package/dist/api/mark.d.ts +42 -0
- package/dist/api/mark.js +127 -0
- package/dist/api/server-deps-a2.d.ts +55 -0
- package/dist/api/server-deps-a2.js +138 -0
- package/dist/api/server-deps-l2-l3.d.ts +68 -0
- package/dist/api/server-deps-l2-l3.js +123 -0
- package/dist/api/server-deps-p1.d.ts +84 -0
- package/dist/api/server-deps-p1.js +156 -0
- package/dist/api/server-deps-p2.d.ts +32 -0
- package/dist/api/server-deps-p2.js +84 -0
- package/dist/api/server-deps-p5.d.ts +66 -0
- package/dist/api/server-deps-p5.js +139 -0
- package/dist/api/viewer-static.d.ts +30 -0
- package/dist/api/viewer-static.js +102 -0
- package/dist/api/wire-up.d.ts +54 -0
- package/dist/api/wire-up.js +521 -0
- package/dist/api-impact.d.ts +30 -0
- package/dist/api-impact.js +196 -0
- package/dist/api-server.d.ts +2 -0
- package/dist/api-server.js +614 -0
- package/dist/backfill-fts-anchor.d.ts +15 -0
- package/dist/backfill-fts-anchor.js +97 -0
- package/dist/backfill-source-type.d.ts +20 -0
- package/dist/backfill-source-type.js +182 -0
- package/dist/churn.d.ts +31 -0
- package/dist/churn.js +98 -0
- package/dist/cli/answer.d.ts +66 -0
- package/dist/cli/answer.js +287 -0
- package/dist/cli/conflict.d.ts +38 -0
- package/dist/cli/conflict.js +282 -0
- package/dist/cli/export.d.ts +51 -0
- package/dist/cli/export.js +280 -0
- package/dist/cli/hooks.d.ts +54 -0
- package/dist/cli/hooks.js +133 -0
- package/dist/cli/import.d.ts +52 -0
- package/dist/cli/import.js +274 -0
- package/dist/cli/mark.d.ts +33 -0
- package/dist/cli/mark.js +130 -0
- package/dist/cli/ocr-batch.d.ts +12 -0
- package/dist/cli/ocr-batch.js +411 -0
- package/dist/cli/snapshot-main.d.ts +30 -0
- package/dist/cli/snapshot-main.js +47 -0
- package/dist/cli/viewer.d.ts +35 -0
- package/dist/cli/viewer.js +71 -0
- package/dist/cli-telemetry.d.ts +36 -0
- package/dist/cli-telemetry.js +150 -0
- package/dist/compact.d.ts +7 -0
- package/dist/compact.js +147 -0
- package/dist/consolidate.d.ts +18 -0
- package/dist/consolidate.js +475 -0
- package/dist/consolidation.d.ts +32 -0
- package/dist/consolidation.js +212 -0
- package/dist/cross-agent-v2.d.ts +45 -0
- package/dist/cross-agent-v2.js +271 -0
- package/dist/cross-search.d.ts +11 -0
- package/dist/cross-search.js +119 -0
- package/dist/crystallize.d.ts +43 -0
- package/dist/crystallize.js +111 -0
- package/dist/db.d.ts +11 -0
- package/dist/db.js +388 -0
- package/dist/dedup.d.ts +1 -0
- package/dist/dedup.js +106 -0
- package/dist/detect-changes.d.ts +29 -0
- package/dist/detect-changes.js +236 -0
- package/dist/digest.d.ts +1 -0
- package/dist/digest.js +205 -0
- package/dist/doctor.d.ts +1 -0
- package/dist/doctor.js +71 -0
- package/dist/embed.d.ts +45 -0
- package/dist/embed.js +257 -0
- package/dist/evals.d.ts +176 -0
- package/dist/evals.js +351 -0
- package/dist/generate-user-profile.d.ts +18 -0
- package/dist/generate-user-profile.js +163 -0
- package/dist/graphify-ingest.d.ts +18 -0
- package/dist/graphify-ingest.js +158 -0
- package/dist/impact.d.ts +39 -0
- package/dist/impact.js +143 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +689 -0
- package/dist/ingest-entity.d.ts +44 -0
- package/dist/ingest-entity.js +197 -0
- package/dist/ingest.d.ts +13 -0
- package/dist/ingest.js +210 -0
- package/dist/kg-llm.d.ts +39 -0
- package/dist/kg-llm.js +295 -0
- package/dist/knowledge-graph.d.ts +72 -0
- package/dist/knowledge-graph.js +507 -0
- package/dist/lib/answer/config.d.ts +41 -0
- package/dist/lib/answer/config.js +70 -0
- package/dist/lib/answer/index.d.ts +47 -0
- package/dist/lib/answer/index.js +221 -0
- package/dist/lib/answer/prompt.d.ts +37 -0
- package/dist/lib/answer/prompt.js +88 -0
- package/dist/lib/answer/provider.d.ts +47 -0
- package/dist/lib/answer/provider.js +101 -0
- package/dist/lib/answer/retrieval.d.ts +36 -0
- package/dist/lib/answer/retrieval.js +92 -0
- package/dist/lib/answer/telemetry.d.ts +99 -0
- package/dist/lib/answer/telemetry.js +154 -0
- package/dist/lib/answer/types.d.ts +108 -0
- package/dist/lib/answer/types.js +1 -0
- package/dist/lib/api/error-leak-fix.d.ts +54 -0
- package/dist/lib/api/error-leak-fix.js +55 -0
- package/dist/lib/api/safe-error-message.d.ts +70 -0
- package/dist/lib/api/safe-error-message.js +122 -0
- package/dist/lib/archive/common-passwords.d.ts +37 -0
- package/dist/lib/archive/common-passwords.js +753 -0
- package/dist/lib/archive/encryption.d.ts +81 -0
- package/dist/lib/archive/encryption.js +236 -0
- package/dist/lib/archive/enforce-strength.d.ts +54 -0
- package/dist/lib/archive/enforce-strength.js +70 -0
- package/dist/lib/archive/entropy.d.ts +34 -0
- package/dist/lib/archive/entropy.js +180 -0
- package/dist/lib/archive/export-locking.d.ts +102 -0
- package/dist/lib/archive/export-locking.js +202 -0
- package/dist/lib/archive/format.d.ts +20 -0
- package/dist/lib/archive/format.js +174 -0
- package/dist/lib/archive/index.d.ts +23 -0
- package/dist/lib/archive/index.js +17 -0
- package/dist/lib/archive/manifest.d.ts +53 -0
- package/dist/lib/archive/manifest.js +183 -0
- package/dist/lib/archive/migration.d.ts +36 -0
- package/dist/lib/archive/migration.js +100 -0
- package/dist/lib/archive/migrations/v18_to_v19.d.ts +16 -0
- package/dist/lib/archive/migrations/v18_to_v19.js +18 -0
- package/dist/lib/archive/orchestrator.d.ts +166 -0
- package/dist/lib/archive/orchestrator.js +428 -0
- package/dist/lib/archive/serializers/chunks.d.ts +30 -0
- package/dist/lib/archive/serializers/chunks.js +113 -0
- package/dist/lib/archive/serializers/embeddings.d.ts +36 -0
- package/dist/lib/archive/serializers/embeddings.js +115 -0
- package/dist/lib/archive/serializers/kg.d.ts +28 -0
- package/dist/lib/archive/serializers/kg.js +184 -0
- package/dist/lib/archive/serializers/ops_audit.d.ts +21 -0
- package/dist/lib/archive/serializers/ops_audit.js +97 -0
- package/dist/lib/archive/server-deps.d.ts +131 -0
- package/dist/lib/archive/server-deps.js +232 -0
- package/dist/lib/archive/strength.d.ts +50 -0
- package/dist/lib/archive/strength.js +77 -0
- package/dist/lib/archive/types.d.ts +159 -0
- package/dist/lib/archive/types.js +45 -0
- package/dist/lib/archive/unpack-streaming.d.ts +70 -0
- package/dist/lib/archive/unpack-streaming.js +255 -0
- package/dist/lib/auth/localhost-guard.d.ts +106 -0
- package/dist/lib/auth/localhost-guard.js +164 -0
- package/dist/lib/confidence/config.d.ts +37 -0
- package/dist/lib/confidence/config.js +107 -0
- package/dist/lib/confidence/db-shim-singleton.d.ts +20 -0
- package/dist/lib/confidence/db-shim-singleton.js +41 -0
- package/dist/lib/confidence/db-shim.d.ts +47 -0
- package/dist/lib/confidence/db-shim.js +141 -0
- package/dist/lib/confidence/mark.d.ts +58 -0
- package/dist/lib/confidence/mark.js +168 -0
- package/dist/lib/confidence/ranking.d.ts +69 -0
- package/dist/lib/confidence/ranking.js +101 -0
- package/dist/lib/confidence/search-filter.d.ts +44 -0
- package/dist/lib/confidence/search-filter.js +103 -0
- package/dist/lib/confidence/types.d.ts +146 -0
- package/dist/lib/confidence/types.js +20 -0
- package/dist/lib/confidence/write-hooks.d.ts +50 -0
- package/dist/lib/confidence/write-hooks.js +159 -0
- package/dist/lib/conflict/audit-fk-check.d.ts +72 -0
- package/dist/lib/conflict/audit-fk-check.js +131 -0
- package/dist/lib/conflict/audit-writer.d.ts +39 -0
- package/dist/lib/conflict/audit-writer.js +133 -0
- package/dist/lib/conflict/db-singleton.d.ts +28 -0
- package/dist/lib/conflict/db-singleton.js +61 -0
- package/dist/lib/conflict/db.d.ts +27 -0
- package/dist/lib/conflict/db.js +20 -0
- package/dist/lib/conflict/detector-direct.d.ts +21 -0
- package/dist/lib/conflict/detector-direct.js +105 -0
- package/dist/lib/conflict/evidence.d.ts +24 -0
- package/dist/lib/conflict/evidence.js +76 -0
- package/dist/lib/conflict/index.d.ts +14 -0
- package/dist/lib/conflict/index.js +12 -0
- package/dist/lib/conflict/scheduler.d.ts +78 -0
- package/dist/lib/conflict/scheduler.js +111 -0
- package/dist/lib/conflict/shadow.d.ts +75 -0
- package/dist/lib/conflict/shadow.js +161 -0
- package/dist/lib/conflict/types.d.ts +133 -0
- package/dist/lib/conflict/types.js +19 -0
- package/dist/lib/cost-cap.d.ts +94 -0
- package/dist/lib/cost-cap.js +129 -0
- package/dist/lib/deps/deps-registry.d.ts +111 -0
- package/dist/lib/deps/deps-registry.js +209 -0
- package/dist/lib/error-sanitizer/middleware.d.ts +43 -0
- package/dist/lib/error-sanitizer/middleware.js +100 -0
- package/dist/lib/error-sanitizer/sanitize.d.ts +78 -0
- package/dist/lib/error-sanitizer/sanitize.js +202 -0
- package/dist/lib/eval-batch.d.ts +34 -0
- package/dist/lib/eval-batch.js +103 -0
- package/dist/lib/eval-metrics.d.ts +36 -0
- package/dist/lib/eval-metrics.js +95 -0
- package/dist/lib/eval.d.ts +106 -0
- package/dist/lib/eval.js +343 -0
- package/dist/lib/events/bus.d.ts +135 -0
- package/dist/lib/events/bus.js +95 -0
- package/dist/lib/fts-anchor.d.ts +1 -0
- package/dist/lib/fts-anchor.js +131 -0
- package/dist/lib/hooks/classifier.d.ts +47 -0
- package/dist/lib/hooks/classifier.js +129 -0
- package/dist/lib/hooks/config.d.ts +49 -0
- package/dist/lib/hooks/config.js +106 -0
- package/dist/lib/hooks/decorators.d.ts +35 -0
- package/dist/lib/hooks/decorators.js +59 -0
- package/dist/lib/hooks/pipeline.d.ts +76 -0
- package/dist/lib/hooks/pipeline.js +213 -0
- package/dist/lib/hooks/privacy-filter-adapter.d.ts +55 -0
- package/dist/lib/hooks/privacy-filter-adapter.js +84 -0
- package/dist/lib/hooks/rate-limit-constant-time.d.ts +57 -0
- package/dist/lib/hooks/rate-limit-constant-time.js +69 -0
- package/dist/lib/hooks/rate-limit-dryrun-fix.d.ts +71 -0
- package/dist/lib/hooks/rate-limit-dryrun-fix.js +110 -0
- package/dist/lib/hooks/rate-limit.d.ts +54 -0
- package/dist/lib/hooks/rate-limit.js +122 -0
- package/dist/lib/hooks/server-deps.d.ts +61 -0
- package/dist/lib/hooks/server-deps.js +92 -0
- package/dist/lib/hooks/source-allowlist.d.ts +26 -0
- package/dist/lib/hooks/source-allowlist.js +53 -0
- package/dist/lib/hooks/types.d.ts +129 -0
- package/dist/lib/hooks/types.js +24 -0
- package/dist/lib/hooks/worker.d.ts +56 -0
- package/dist/lib/hooks/worker.js +150 -0
- package/dist/lib/ingest-router.d.ts +25 -0
- package/dist/lib/ingest-router.js +103 -0
- package/dist/lib/ocr-detector.d.ts +61 -0
- package/dist/lib/ocr-detector.js +138 -0
- package/dist/lib/ocr-engine-stub.d.ts +48 -0
- package/dist/lib/ocr-engine-stub.js +190 -0
- package/dist/lib/ocr-engines/google-doc-ai.d.ts +36 -0
- package/dist/lib/ocr-engines/google-doc-ai.js +164 -0
- package/dist/lib/ocr-jobs.d.ts +67 -0
- package/dist/lib/ocr-jobs.js +129 -0
- package/dist/lib/op-audit-extension/ran-at-guard.d.ts +86 -0
- package/dist/lib/op-audit-extension/ran-at-guard.js +110 -0
- package/dist/lib/op-audit.d.ts +71 -0
- package/dist/lib/op-audit.js +777 -0
- package/dist/lib/privacy-br/detector.d.ts +42 -0
- package/dist/lib/privacy-br/detector.js +130 -0
- package/dist/lib/privacy-br/index.d.ts +20 -0
- package/dist/lib/privacy-br/index.js +20 -0
- package/dist/lib/privacy-br/integration.d.ts +71 -0
- package/dist/lib/privacy-br/integration.js +74 -0
- package/dist/lib/privacy-br/patterns.d.ts +117 -0
- package/dist/lib/privacy-br/patterns.js +434 -0
- package/dist/lib/privacy-br/redact.d.ts +42 -0
- package/dist/lib/privacy-br/redact.js +103 -0
- package/dist/lib/privacy-br/types.d.ts +55 -0
- package/dist/lib/privacy-br/types.js +25 -0
- package/dist/lib/regex-extract/extractor.d.ts +30 -0
- package/dist/lib/regex-extract/extractor.js +91 -0
- package/dist/lib/regex-extract/frontmatter.d.ts +44 -0
- package/dist/lib/regex-extract/frontmatter.js +169 -0
- package/dist/lib/regex-extract/index.d.ts +14 -0
- package/dist/lib/regex-extract/index.js +14 -0
- package/dist/lib/regex-extract/ingest-router-l4.d.ts +76 -0
- package/dist/lib/regex-extract/ingest-router-l4.js +102 -0
- package/dist/lib/regex-extract/patterns.d.ts +61 -0
- package/dist/lib/regex-extract/patterns.js +91 -0
- package/dist/lib/regex-extract/production-wire.d.ts +128 -0
- package/dist/lib/regex-extract/production-wire.js +202 -0
- package/dist/lib/regex-extract/reconcile.d.ts +162 -0
- package/dist/lib/regex-extract/reconcile.js +210 -0
- package/dist/lib/regex-extract/strip-code.d.ts +24 -0
- package/dist/lib/regex-extract/strip-code.js +52 -0
- package/dist/lib/regex-extract/types.d.ts +48 -0
- package/dist/lib/regex-extract/types.js +25 -0
- package/dist/lib/reranker.d.ts +80 -0
- package/dist/lib/reranker.js +324 -0
- package/dist/lib/shadow-tracker.d.ts +191 -0
- package/dist/lib/shadow-tracker.js +384 -0
- package/dist/lib/spo-injection.d.ts +52 -0
- package/dist/lib/spo-injection.js +166 -0
- package/dist/lib/telemetry-collector.d.ts +158 -0
- package/dist/lib/telemetry-collector.js +249 -0
- package/dist/lib/temporal-detector.d.ts +27 -0
- package/dist/lib/temporal-detector.js +81 -0
- package/dist/lib/viewer/auth.d.ts +37 -0
- package/dist/lib/viewer/auth.js +70 -0
- package/dist/lib/viewer/backpressure.d.ts +59 -0
- package/dist/lib/viewer/backpressure.js +100 -0
- package/dist/lib/viewer/broadcast-singleton.d.ts +34 -0
- package/dist/lib/viewer/broadcast-singleton.js +49 -0
- package/dist/lib/viewer/broadcast.d.ts +66 -0
- package/dist/lib/viewer/broadcast.js +96 -0
- package/dist/lib/viewer/event-types.d.ts +132 -0
- package/dist/lib/viewer/event-types.js +74 -0
- package/dist/lib/viewer/instrumentation.d.ts +106 -0
- package/dist/lib/viewer/instrumentation.js +224 -0
- package/dist/lib/viewer/migration.d.ts +23 -0
- package/dist/lib/viewer/migration.js +56 -0
- package/dist/lib/viewer/redaction.d.ts +44 -0
- package/dist/lib/viewer/redaction.js +139 -0
- package/dist/lib/viewer/session.d.ts +51 -0
- package/dist/lib/viewer/session.js +87 -0
- package/dist/mcp/tools/answer.d.ts +94 -0
- package/dist/mcp/tools/answer.js +213 -0
- package/dist/mcp/tools/archive.d.ts +152 -0
- package/dist/mcp/tools/archive.js +170 -0
- package/dist/mcp/tools/conflict.d.ts +44 -0
- package/dist/mcp/tools/conflict.js +130 -0
- package/dist/mcp/tools/hooks.d.ts +29 -0
- package/dist/mcp/tools/hooks.js +78 -0
- package/dist/mcp/tools/mark.d.ts +38 -0
- package/dist/mcp/tools/mark.js +82 -0
- package/dist/mcp/tools/viewer.d.ts +58 -0
- package/dist/mcp/tools/viewer.js +64 -0
- package/dist/mcp-server.d.ts +2 -0
- package/dist/mcp-server.js +395 -0
- package/dist/metrics.d.ts +3 -0
- package/dist/metrics.js +59 -0
- package/dist/noise-filter.d.ts +18 -0
- package/dist/noise-filter.js +142 -0
- package/dist/notion-sync.d.ts +7 -0
- package/dist/notion-sync.js +97 -0
- package/dist/observability/adapters/a3-adapter.d.ts +63 -0
- package/dist/observability/adapters/a3-adapter.js +83 -0
- package/dist/observability/adapters/p1-adapter.d.ts +49 -0
- package/dist/observability/adapters/p1-adapter.js +63 -0
- package/dist/observability/adapters/p5-adapter.d.ts +36 -0
- package/dist/observability/adapters/p5-adapter.js +71 -0
- package/dist/observability/cardinality.d.ts +61 -0
- package/dist/observability/cardinality.js +300 -0
- package/dist/observability/collectors/db-stats.collector.d.ts +9 -0
- package/dist/observability/collectors/db-stats.collector.js +88 -0
- package/dist/observability/collectors/eventbus.collector.d.ts +6 -0
- package/dist/observability/collectors/eventbus.collector.js +50 -0
- package/dist/observability/collectors/process.collector.d.ts +7 -0
- package/dist/observability/collectors/process.collector.js +93 -0
- package/dist/observability/collectors/provider-telemetry.collector.d.ts +11 -0
- package/dist/observability/collectors/provider-telemetry.collector.js +112 -0
- package/dist/observability/collectors/search-telemetry.collector.d.ts +12 -0
- package/dist/observability/collectors/search-telemetry.collector.js +76 -0
- package/dist/observability/exporter.d.ts +30 -0
- package/dist/observability/exporter.js +182 -0
- package/dist/observability/index.d.ts +22 -0
- package/dist/observability/index.js +21 -0
- package/dist/observability/metrics.d.ts +53 -0
- package/dist/observability/metrics.js +213 -0
- package/dist/observability/privacy-guard.d.ts +32 -0
- package/dist/observability/privacy-guard.js +116 -0
- package/dist/observability/record.d.ts +83 -0
- package/dist/observability/record.js +217 -0
- package/dist/observability/registry.d.ts +60 -0
- package/dist/observability/registry.js +93 -0
- package/dist/observability/types.d.ts +115 -0
- package/dist/observability/types.js +235 -0
- package/dist/observability.d.ts +131 -0
- package/dist/observability.js +234 -0
- package/dist/plugins/nox-hooks/index.d.ts +61 -0
- package/dist/plugins/nox-hooks/index.js +99 -0
- package/dist/primer.d.ts +1 -0
- package/dist/primer.js +100 -0
- package/dist/privacy/filter.d.ts +27 -0
- package/dist/privacy/filter.js +74 -0
- package/dist/privacy/patterns.d.ts +29 -0
- package/dist/privacy/patterns.js +178 -0
- package/dist/privacy/tag-parser.d.ts +24 -0
- package/dist/privacy/tag-parser.js +29 -0
- package/dist/project-context-gen.d.ts +10 -0
- package/dist/project-context-gen.js +106 -0
- package/dist/providers/embedding/gemini.d.ts +73 -0
- package/dist/providers/embedding/gemini.js +118 -0
- package/dist/providers/embedding/openai.d.ts +76 -0
- package/dist/providers/embedding/openai.js +123 -0
- package/dist/providers/embedding/types.d.ts +39 -0
- package/dist/providers/embedding/types.js +1 -0
- package/dist/providers/embedding/voyage.d.ts +36 -0
- package/dist/providers/embedding/voyage.js +29 -0
- package/dist/providers/index.d.ts +116 -0
- package/dist/providers/index.js +158 -0
- package/dist/providers/llm/anthropic.d.ts +29 -0
- package/dist/providers/llm/anthropic.js +28 -0
- package/dist/providers/llm/chain.d.ts +99 -0
- package/dist/providers/llm/chain.js +213 -0
- package/dist/providers/llm/gemini.d.ts +48 -0
- package/dist/providers/llm/gemini.js +96 -0
- package/dist/providers/llm/openai.d.ts +64 -0
- package/dist/providers/llm/openai.js +109 -0
- package/dist/providers/llm/types.d.ts +47 -0
- package/dist/providers/llm/types.js +1 -0
- package/dist/providers/telemetry.d.ts +87 -0
- package/dist/providers/telemetry.js +156 -0
- package/dist/providers/types.d.ts +42 -0
- package/dist/providers/types.js +59 -0
- package/dist/query-entity-count.d.ts +54 -0
- package/dist/query-entity-count.js +171 -0
- package/dist/reflect.d.ts +26 -0
- package/dist/reflect.js +290 -0
- package/dist/reindex-errors.d.ts +7 -0
- package/dist/reindex-errors.js +21 -0
- package/dist/reindex.d.ts +16 -0
- package/dist/reindex.js +293 -0
- package/dist/retention.d.ts +40 -0
- package/dist/retention.js +86 -0
- package/dist/salience.d.ts +85 -0
- package/dist/salience.js +245 -0
- package/dist/scripts/migrate-v17-ops-audit.d.ts +17 -0
- package/dist/scripts/migrate-v17-ops-audit.js +70 -0
- package/dist/search-dedup.d.ts +44 -0
- package/dist/search-dedup.js +137 -0
- package/dist/search-expansion.d.ts +20 -0
- package/dist/search-expansion.js +99 -0
- package/dist/search.d.ts +62 -0
- package/dist/search.js +582 -0
- package/dist/seh-detector.d.ts +31 -0
- package/dist/seh-detector.js +201 -0
- package/dist/self-improve.d.ts +1 -0
- package/dist/self-improve.js +189 -0
- package/dist/session-distill.d.ts +29 -0
- package/dist/session-distill.js +325 -0
- package/dist/session-update.d.ts +1 -0
- package/dist/session-update.js +54 -0
- package/dist/shared-memory.d.ts +24 -0
- package/dist/shared-memory.js +104 -0
- package/dist/stats.d.ts +1 -0
- package/dist/stats.js +44 -0
- package/dist/temporal-retrieval.d.ts +132 -0
- package/dist/temporal-retrieval.js +495 -0
- package/dist/tier-manager.d.ts +69 -0
- package/dist/tier-manager.js +225 -0
- package/dist/vectorize.d.ts +16 -0
- package/dist/vectorize.js +91 -0
- package/dist/watch.d.ts +8 -0
- package/dist/watch.js +124 -0
- package/package.json +74 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, existsSync } from "fs";
|
|
2
|
+
import { resolve, dirname } from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
5
|
+
const NOTION_API = "https://api.notion.com/v1/pages";
|
|
6
|
+
const NOTION_VERSION = "2025-09-03";
|
|
7
|
+
const DATABASE_ID = "31d8e29911ab8163b718d7af565f2fcc";
|
|
8
|
+
const SYNC_LOG_PATH = resolve(__dirname, "..", "last-sync.json");
|
|
9
|
+
function getNotionToken() {
|
|
10
|
+
// Token can also be supplied directly via NOX_NOTION_TOKEN env var
|
|
11
|
+
if (process.env.NOX_NOTION_TOKEN)
|
|
12
|
+
return process.env.NOX_NOTION_TOKEN.trim();
|
|
13
|
+
const tokenPath = process.env.NOX_NOTION_TOKEN_PATH ?? "/root/.config/notion/api_key";
|
|
14
|
+
try {
|
|
15
|
+
return readFileSync(tokenPath, "utf-8").trim();
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
console.error(`[WARN] Notion token not found at ${tokenPath} (set NOX_NOTION_TOKEN or NOX_NOTION_TOKEN_PATH)`);
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function createNotionPage(token, item) {
|
|
23
|
+
try {
|
|
24
|
+
const body = {
|
|
25
|
+
parent: { database_id: DATABASE_ID },
|
|
26
|
+
properties: {
|
|
27
|
+
"Título": { title: [{ text: { content: item.title.substring(0, 100) } }] },
|
|
28
|
+
"Data": { date: { start: item.date } },
|
|
29
|
+
"Categoria": { select: { name: item.category } },
|
|
30
|
+
"Conteúdo": { rich_text: [{ text: { content: item.content.substring(0, 2000) } }] },
|
|
31
|
+
"Fonte": { rich_text: [{ text: { content: item.source } }] },
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
const response = await fetch(NOTION_API, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: {
|
|
37
|
+
Authorization: `Bearer ${token}`,
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
"Notion-Version": NOTION_VERSION,
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify(body),
|
|
42
|
+
signal: AbortSignal.timeout(10_000),
|
|
43
|
+
});
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
const err = await response.text();
|
|
46
|
+
console.error(`[WARN] Notion API ${response.status}: ${err.substring(0, 200)}`);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
console.error(`[WARN] Notion sync failed: ${err instanceof Error ? err.message : err}`);
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function sleep(ms) {
|
|
57
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
58
|
+
}
|
|
59
|
+
// Save items to log for manual sync-notion command
|
|
60
|
+
export function saveSyncLog(items) {
|
|
61
|
+
writeFileSync(SYNC_LOG_PATH, JSON.stringify(items, null, 2), "utf-8");
|
|
62
|
+
}
|
|
63
|
+
// Load items from last consolidation for manual sync
|
|
64
|
+
export function loadSyncLog() {
|
|
65
|
+
if (!existsSync(SYNC_LOG_PATH))
|
|
66
|
+
return [];
|
|
67
|
+
try {
|
|
68
|
+
return JSON.parse(readFileSync(SYNC_LOG_PATH, "utf-8"));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export async function syncToNotion(items) {
|
|
75
|
+
if (items.length === 0)
|
|
76
|
+
return { synced: 0, failed: 0 };
|
|
77
|
+
// Save items for manual re-sync
|
|
78
|
+
saveSyncLog(items);
|
|
79
|
+
const token = getNotionToken();
|
|
80
|
+
if (!token)
|
|
81
|
+
return { synced: 0, failed: items.length };
|
|
82
|
+
let synced = 0;
|
|
83
|
+
let failed = 0;
|
|
84
|
+
for (const item of items) {
|
|
85
|
+
const ok = await createNotionPage(token, item);
|
|
86
|
+
if (ok) {
|
|
87
|
+
synced++;
|
|
88
|
+
console.log(`[INFO] Notion: "${item.title.substring(0, 50)}..." → ${item.category}`);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
failed++;
|
|
92
|
+
}
|
|
93
|
+
await sleep(350); // Rate limit ~3 req/s
|
|
94
|
+
}
|
|
95
|
+
console.log(`[INFO] Notion sync: ${synced} synced, ${failed} failed`);
|
|
96
|
+
return { synced, failed };
|
|
97
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/observability/adapters/a3-adapter.ts — Provider call instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Wraps any provider call (LLM or embedding) with:
|
|
5
|
+
* - nox_provider_calls_total{provider, model, outcome}
|
|
6
|
+
* - nox_provider_duration_seconds{provider, kind}
|
|
7
|
+
* - nox_provider_cost_usd_total{provider, model}
|
|
8
|
+
* - nox_provider_tokens_total{provider, direction}
|
|
9
|
+
*
|
|
10
|
+
* Composes with the existing `provider_telemetry` write path (kept intact).
|
|
11
|
+
* Metrics are *live* per-call; telemetry is durable per-row.
|
|
12
|
+
*
|
|
13
|
+
* Wiring example (in src/providers/llm/chain.ts):
|
|
14
|
+
*
|
|
15
|
+
* import { instrumentProviderCall } from "../observability/adapters/a3-adapter.js";
|
|
16
|
+
*
|
|
17
|
+
* const result = await instrumentProviderCall(
|
|
18
|
+
* { provider: "gemini", model: "gemini-2.5-flash-lite", kind: "llm" },
|
|
19
|
+
* () => callGeminiAPI(prompt),
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* `instrumentProviderCall` returns the inner result + a `metricsCost` field
|
|
23
|
+
* for the caller to inspect.
|
|
24
|
+
*/
|
|
25
|
+
import { type ProviderKind, type ProviderOutcome } from "../record.js";
|
|
26
|
+
export interface ProviderCallMeta {
|
|
27
|
+
provider: string;
|
|
28
|
+
model: string;
|
|
29
|
+
kind: ProviderKind;
|
|
30
|
+
}
|
|
31
|
+
export interface ProviderCallResult<T> {
|
|
32
|
+
result: T;
|
|
33
|
+
costUsd?: number;
|
|
34
|
+
tokensIn?: number;
|
|
35
|
+
tokensOut?: number;
|
|
36
|
+
}
|
|
37
|
+
export type ProviderInner<T> = () => Promise<ProviderCallResult<T>>;
|
|
38
|
+
/**
|
|
39
|
+
* Run an inner provider call. Always emits metrics, regardless of success.
|
|
40
|
+
* Errors propagate; the outcome label maps to:
|
|
41
|
+
* - "rate_limit" if err.status === 429
|
|
42
|
+
* - "fallback" if err.code === "FALLBACK"
|
|
43
|
+
* - "error" otherwise
|
|
44
|
+
*/
|
|
45
|
+
export declare function instrumentProviderCall<T>(meta: ProviderCallMeta, inner: ProviderInner<T>): Promise<T>;
|
|
46
|
+
export declare function classifyProviderError(err: unknown): ProviderOutcome;
|
|
47
|
+
/**
|
|
48
|
+
* Example — adding metrics to an existing provider call:
|
|
49
|
+
*
|
|
50
|
+
* // before:
|
|
51
|
+
* const out = await callGemini(input);
|
|
52
|
+
*
|
|
53
|
+
* // after:
|
|
54
|
+
* const out = await instrumentProviderCall(
|
|
55
|
+
* { provider: "gemini", model, kind: "llm" },
|
|
56
|
+
* async () => {
|
|
57
|
+
* const r = await callGemini(input);
|
|
58
|
+
* return { result: r, tokensIn: r.usage.in, tokensOut: r.usage.out, costUsd: estimateCost(r) };
|
|
59
|
+
* },
|
|
60
|
+
* );
|
|
61
|
+
*
|
|
62
|
+
* Total integration footprint: 1 import + 3-4 LOC at the call site.
|
|
63
|
+
*/
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/observability/adapters/a3-adapter.ts — Provider call instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Wraps any provider call (LLM or embedding) with:
|
|
5
|
+
* - nox_provider_calls_total{provider, model, outcome}
|
|
6
|
+
* - nox_provider_duration_seconds{provider, kind}
|
|
7
|
+
* - nox_provider_cost_usd_total{provider, model}
|
|
8
|
+
* - nox_provider_tokens_total{provider, direction}
|
|
9
|
+
*
|
|
10
|
+
* Composes with the existing `provider_telemetry` write path (kept intact).
|
|
11
|
+
* Metrics are *live* per-call; telemetry is durable per-row.
|
|
12
|
+
*
|
|
13
|
+
* Wiring example (in src/providers/llm/chain.ts):
|
|
14
|
+
*
|
|
15
|
+
* import { instrumentProviderCall } from "../observability/adapters/a3-adapter.js";
|
|
16
|
+
*
|
|
17
|
+
* const result = await instrumentProviderCall(
|
|
18
|
+
* { provider: "gemini", model: "gemini-2.5-flash-lite", kind: "llm" },
|
|
19
|
+
* () => callGeminiAPI(prompt),
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* `instrumentProviderCall` returns the inner result + a `metricsCost` field
|
|
23
|
+
* for the caller to inspect.
|
|
24
|
+
*/
|
|
25
|
+
import { recordProviderCall, startTimer } from "../record.js";
|
|
26
|
+
/**
|
|
27
|
+
* Run an inner provider call. Always emits metrics, regardless of success.
|
|
28
|
+
* Errors propagate; the outcome label maps to:
|
|
29
|
+
* - "rate_limit" if err.status === 429
|
|
30
|
+
* - "fallback" if err.code === "FALLBACK"
|
|
31
|
+
* - "error" otherwise
|
|
32
|
+
*/
|
|
33
|
+
export async function instrumentProviderCall(meta, inner) {
|
|
34
|
+
const end = startTimer();
|
|
35
|
+
try {
|
|
36
|
+
const { result, costUsd, tokensIn, tokensOut } = await inner();
|
|
37
|
+
recordProviderCall({
|
|
38
|
+
...meta,
|
|
39
|
+
outcome: "success",
|
|
40
|
+
durationSeconds: end(),
|
|
41
|
+
costUsd,
|
|
42
|
+
tokensIn,
|
|
43
|
+
tokensOut,
|
|
44
|
+
});
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
const outcome = classifyProviderError(err);
|
|
49
|
+
recordProviderCall({
|
|
50
|
+
...meta,
|
|
51
|
+
outcome,
|
|
52
|
+
durationSeconds: end(),
|
|
53
|
+
});
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function classifyProviderError(err) {
|
|
58
|
+
if (!err || typeof err !== "object")
|
|
59
|
+
return "error";
|
|
60
|
+
const e = err;
|
|
61
|
+
if (e.status === 429)
|
|
62
|
+
return "rate_limit";
|
|
63
|
+
if (e.code === "FALLBACK" || e.code === "PROVIDER_FALLBACK")
|
|
64
|
+
return "fallback";
|
|
65
|
+
return "error";
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Example — adding metrics to an existing provider call:
|
|
69
|
+
*
|
|
70
|
+
* // before:
|
|
71
|
+
* const out = await callGemini(input);
|
|
72
|
+
*
|
|
73
|
+
* // after:
|
|
74
|
+
* const out = await instrumentProviderCall(
|
|
75
|
+
* { provider: "gemini", model, kind: "llm" },
|
|
76
|
+
* async () => {
|
|
77
|
+
* const r = await callGemini(input);
|
|
78
|
+
* return { result: r, tokensIn: r.usage.in, tokensOut: r.usage.out, costUsd: estimateCost(r) };
|
|
79
|
+
* },
|
|
80
|
+
* );
|
|
81
|
+
*
|
|
82
|
+
* Total integration footprint: 1 import + 3-4 LOC at the call site.
|
|
83
|
+
*/
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/observability/adapters/p1-adapter.ts — P1 /api/answer instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the answer handler so every call emits:
|
|
5
|
+
* - nox_answer_requests_total{failure_reason}
|
|
6
|
+
* - nox_answer_duration_seconds{phase}
|
|
7
|
+
* - nox_answer_tokens_total{direction}
|
|
8
|
+
*
|
|
9
|
+
* Wiring example (in src/api/answer.ts):
|
|
10
|
+
*
|
|
11
|
+
* import { withAnswerMetrics } from "../observability/adapters/p1-adapter.js";
|
|
12
|
+
*
|
|
13
|
+
* export const answerHandler = withAnswerMetrics(async (req, res) => {
|
|
14
|
+
* // … existing logic
|
|
15
|
+
* return { outcome: "success", tokensIn, tokensOut, phases };
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* The wrapper expects the inner handler to return an `AnswerOutcome` object.
|
|
19
|
+
* That keeps metrics emission *external* to business logic — easy to unit
|
|
20
|
+
* test, easy to remove.
|
|
21
|
+
*/
|
|
22
|
+
import { type AnswerOutcome, type AnswerTiming } from "../record.js";
|
|
23
|
+
export interface AnswerResult {
|
|
24
|
+
outcome: AnswerOutcome;
|
|
25
|
+
tokensIn?: number;
|
|
26
|
+
tokensOut?: number;
|
|
27
|
+
timing?: AnswerTiming;
|
|
28
|
+
}
|
|
29
|
+
export type AnswerHandler<Args extends unknown[]> = (...args: Args) => Promise<AnswerResult>;
|
|
30
|
+
/**
|
|
31
|
+
* Wrap an answer handler so it emits metrics on every call.
|
|
32
|
+
* The wrapped handler returns the same `AnswerResult` so callers can keep
|
|
33
|
+
* threading metadata to upstream layers.
|
|
34
|
+
*
|
|
35
|
+
* The wrapper never swallows the inner handler's errors — it just emits a
|
|
36
|
+
* "llm_failed" metric and re-throws.
|
|
37
|
+
*/
|
|
38
|
+
export declare function withAnswerMetrics<Args extends unknown[]>(inner: AnswerHandler<Args>): AnswerHandler<Args>;
|
|
39
|
+
/**
|
|
40
|
+
* Example — replace the existing /api/answer registration:
|
|
41
|
+
*
|
|
42
|
+
* // before:
|
|
43
|
+
* app.get("/api/answer", answerHandler);
|
|
44
|
+
*
|
|
45
|
+
* // after:
|
|
46
|
+
* app.get("/api/answer", withAnswerMetrics(answerHandler));
|
|
47
|
+
*
|
|
48
|
+
* Total integration footprint: 1 import + 1 wrapping call.
|
|
49
|
+
*/
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/observability/adapters/p1-adapter.ts — P1 /api/answer instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the answer handler so every call emits:
|
|
5
|
+
* - nox_answer_requests_total{failure_reason}
|
|
6
|
+
* - nox_answer_duration_seconds{phase}
|
|
7
|
+
* - nox_answer_tokens_total{direction}
|
|
8
|
+
*
|
|
9
|
+
* Wiring example (in src/api/answer.ts):
|
|
10
|
+
*
|
|
11
|
+
* import { withAnswerMetrics } from "../observability/adapters/p1-adapter.js";
|
|
12
|
+
*
|
|
13
|
+
* export const answerHandler = withAnswerMetrics(async (req, res) => {
|
|
14
|
+
* // … existing logic
|
|
15
|
+
* return { outcome: "success", tokensIn, tokensOut, phases };
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* The wrapper expects the inner handler to return an `AnswerOutcome` object.
|
|
19
|
+
* That keeps metrics emission *external* to business logic — easy to unit
|
|
20
|
+
* test, easy to remove.
|
|
21
|
+
*/
|
|
22
|
+
import { recordAnswer, startTimer } from "../record.js";
|
|
23
|
+
/**
|
|
24
|
+
* Wrap an answer handler so it emits metrics on every call.
|
|
25
|
+
* The wrapped handler returns the same `AnswerResult` so callers can keep
|
|
26
|
+
* threading metadata to upstream layers.
|
|
27
|
+
*
|
|
28
|
+
* The wrapper never swallows the inner handler's errors — it just emits a
|
|
29
|
+
* "llm_failed" metric and re-throws.
|
|
30
|
+
*/
|
|
31
|
+
export function withAnswerMetrics(inner) {
|
|
32
|
+
return async (...args) => {
|
|
33
|
+
const end = startTimer();
|
|
34
|
+
try {
|
|
35
|
+
const result = await inner(...args);
|
|
36
|
+
recordAnswer({
|
|
37
|
+
outcome: result.outcome,
|
|
38
|
+
timing: { ...(result.timing ?? {}), total: result.timing?.total ?? end() },
|
|
39
|
+
tokensIn: result.tokensIn,
|
|
40
|
+
tokensOut: result.tokensOut,
|
|
41
|
+
});
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
recordAnswer({
|
|
46
|
+
outcome: "llm_failed",
|
|
47
|
+
timing: { total: end() },
|
|
48
|
+
});
|
|
49
|
+
throw err;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Example — replace the existing /api/answer registration:
|
|
55
|
+
*
|
|
56
|
+
* // before:
|
|
57
|
+
* app.get("/api/answer", answerHandler);
|
|
58
|
+
*
|
|
59
|
+
* // after:
|
|
60
|
+
* app.get("/api/answer", withAnswerMetrics(answerHandler));
|
|
61
|
+
*
|
|
62
|
+
* Total integration footprint: 1 import + 1 wrapping call.
|
|
63
|
+
*/
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface BroadcastEvent {
|
|
2
|
+
type: string;
|
|
3
|
+
[k: string]: unknown;
|
|
4
|
+
}
|
|
5
|
+
export type BroadcastFn = (event: BroadcastEvent) => void | Promise<void>;
|
|
6
|
+
/**
|
|
7
|
+
* Wrap a broadcast function so every emitted event is counted.
|
|
8
|
+
* If the inner broadcast throws, the event is counted as dropped/error and
|
|
9
|
+
* the error re-thrown.
|
|
10
|
+
*/
|
|
11
|
+
export declare function wrapBroadcast(inner: BroadcastFn): BroadcastFn;
|
|
12
|
+
export interface SocketLike {
|
|
13
|
+
on(event: "close" | "error" | string, listener: () => void): unknown;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Track a viewer SSE/WS connection lifecycle. Increments the gauge on entry
|
|
17
|
+
* and decrements when the socket closes (or errors).
|
|
18
|
+
*/
|
|
19
|
+
export declare function trackConnection(socket: SocketLike): void;
|
|
20
|
+
/**
|
|
21
|
+
* Manual backpressure reporting helper for upstream code paths that drop
|
|
22
|
+
* events without going through `wrapBroadcast`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function reportBackpressureDrop(reason: "slow_consumer" | "queue_full" | "client_gone"): void;
|
|
25
|
+
/**
|
|
26
|
+
* Example — minimal wiring inside an existing SSE server:
|
|
27
|
+
*
|
|
28
|
+
* import { trackConnection, wrapBroadcast } from "./observability/adapters/p5-adapter.js";
|
|
29
|
+
*
|
|
30
|
+
* const broadcast = wrapBroadcast(rawBroadcast);
|
|
31
|
+
*
|
|
32
|
+
* sseRouter.get("/events", (req, res) => {
|
|
33
|
+
* trackConnection(res);
|
|
34
|
+
* subscribe((evt) => broadcast(evt));
|
|
35
|
+
* });
|
|
36
|
+
*/
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/observability/adapters/p5-adapter.ts — Viewer (P5) instrumentation.
|
|
3
|
+
*
|
|
4
|
+
* Two entry points:
|
|
5
|
+
* 1. `wrapBroadcast(broadcast)` — instruments the broadcast pipeline so
|
|
6
|
+
* every event emitted is counted by type.
|
|
7
|
+
* 2. `trackConnection(socket)` — increments/decrements the open
|
|
8
|
+
* connection gauge for the lifetime of the SSE/WS socket.
|
|
9
|
+
*
|
|
10
|
+
* Wiring example (in src/lib/viewer/broadcast.ts):
|
|
11
|
+
*
|
|
12
|
+
* import { wrapBroadcast, trackConnection } from "../../observability/adapters/p5-adapter.js";
|
|
13
|
+
*
|
|
14
|
+
* const safeBroadcast = wrapBroadcast(rawBroadcast);
|
|
15
|
+
* server.on("connection", (sock) => trackConnection(sock));
|
|
16
|
+
*
|
|
17
|
+
* The wiring is ~5 LOC at the integration site.
|
|
18
|
+
*/
|
|
19
|
+
import { recordViewerConnect, recordViewerDisconnect, recordViewerEvent, recordViewerDropped, } from "../record.js";
|
|
20
|
+
/**
|
|
21
|
+
* Wrap a broadcast function so every emitted event is counted.
|
|
22
|
+
* If the inner broadcast throws, the event is counted as dropped/error and
|
|
23
|
+
* the error re-thrown.
|
|
24
|
+
*/
|
|
25
|
+
export function wrapBroadcast(inner) {
|
|
26
|
+
return async (event) => {
|
|
27
|
+
try {
|
|
28
|
+
await inner(event);
|
|
29
|
+
recordViewerEvent(event.type);
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
recordViewerDropped("queue_full");
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Track a viewer SSE/WS connection lifecycle. Increments the gauge on entry
|
|
39
|
+
* and decrements when the socket closes (or errors).
|
|
40
|
+
*/
|
|
41
|
+
export function trackConnection(socket) {
|
|
42
|
+
recordViewerConnect();
|
|
43
|
+
let decremented = false;
|
|
44
|
+
const onClose = () => {
|
|
45
|
+
if (decremented)
|
|
46
|
+
return;
|
|
47
|
+
decremented = true;
|
|
48
|
+
recordViewerDisconnect();
|
|
49
|
+
};
|
|
50
|
+
socket.on("close", onClose);
|
|
51
|
+
socket.on("error", onClose);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Manual backpressure reporting helper for upstream code paths that drop
|
|
55
|
+
* events without going through `wrapBroadcast`.
|
|
56
|
+
*/
|
|
57
|
+
export function reportBackpressureDrop(reason) {
|
|
58
|
+
recordViewerDropped(reason);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Example — minimal wiring inside an existing SSE server:
|
|
62
|
+
*
|
|
63
|
+
* import { trackConnection, wrapBroadcast } from "./observability/adapters/p5-adapter.js";
|
|
64
|
+
*
|
|
65
|
+
* const broadcast = wrapBroadcast(rawBroadcast);
|
|
66
|
+
*
|
|
67
|
+
* sseRouter.get("/events", (req, res) => {
|
|
68
|
+
* trackConnection(res);
|
|
69
|
+
* subscribe((evt) => broadcast(evt));
|
|
70
|
+
* });
|
|
71
|
+
*/
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/observability/cardinality.ts — Cardinality guard (T8).
|
|
3
|
+
*
|
|
4
|
+
* Prevents unbounded label cardinality from blowing up the registry.
|
|
5
|
+
*
|
|
6
|
+
* Failure modes if not enforced:
|
|
7
|
+
* 1. user_id, session_id, query_text in labels → millions of series → OOM
|
|
8
|
+
* 2. Bug: cycled value in label → series count grows forever
|
|
9
|
+
* 3. Adversary input → DoS via metric explosion
|
|
10
|
+
*
|
|
11
|
+
* GUARD STRATEGY:
|
|
12
|
+
* - Per-metric `maxSeries` cap (default 1000).
|
|
13
|
+
* - Per-metric label *value* allowlist (optional): only those values may
|
|
14
|
+
* be reported. Anything else is bucketed as "other".
|
|
15
|
+
* - On overflow: drop the increment + log once per cooldown window.
|
|
16
|
+
*
|
|
17
|
+
* USAGE:
|
|
18
|
+
* const guard = new CardinalityGuard();
|
|
19
|
+
* guard.policy("nox_search_requests_total", {
|
|
20
|
+
* maxSeries: 50,
|
|
21
|
+
* labelAllowlist: { method: ["cli", "api", "mcp"] },
|
|
22
|
+
* });
|
|
23
|
+
* const safe = guard.guard("nox_search_requests_total", { method: payload });
|
|
24
|
+
* if (safe) counter.inc(safe);
|
|
25
|
+
*/
|
|
26
|
+
export interface MetricPolicy {
|
|
27
|
+
/** Max distinct label-tuple series allowed. Default 1000. */
|
|
28
|
+
maxSeries?: number;
|
|
29
|
+
/** Per-label allowlist of values. Unknown values → "other". */
|
|
30
|
+
labelAllowlist?: Record<string, readonly string[]>;
|
|
31
|
+
/** Per-label hard drop list (e.g. "user_id" must never appear). */
|
|
32
|
+
labelDenylist?: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
export declare class CardinalityGuard {
|
|
35
|
+
private readonly policies;
|
|
36
|
+
private readonly seenSeries;
|
|
37
|
+
private readonly drops;
|
|
38
|
+
private readonly log;
|
|
39
|
+
constructor(log?: (msg: string) => void);
|
|
40
|
+
/** Configure a per-metric policy. */
|
|
41
|
+
policy(metricName: string, p: MetricPolicy): void;
|
|
42
|
+
/**
|
|
43
|
+
* Validate + normalize a label set for a metric.
|
|
44
|
+
* Returns the (possibly rewritten) labels — or `null` if it must be dropped.
|
|
45
|
+
*/
|
|
46
|
+
guard(metricName: string, labels: Record<string, string>): Record<string, string> | null;
|
|
47
|
+
/** Current series count for a metric. */
|
|
48
|
+
seriesCount(metricName: string): number;
|
|
49
|
+
/** Number of drops for a metric (since process start). */
|
|
50
|
+
dropCount(metricName: string): number;
|
|
51
|
+
/** Reset (test-only). */
|
|
52
|
+
reset(): void;
|
|
53
|
+
private noteDrop;
|
|
54
|
+
}
|
|
55
|
+
export declare function getDefaultGuard(): CardinalityGuard;
|
|
56
|
+
export declare function resetDefaultGuard(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Apply nox-mem's standard policies. Keep this conservative — anything that
|
|
59
|
+
* could explode (user_id, query_text, paths) must be denylisted everywhere.
|
|
60
|
+
*/
|
|
61
|
+
export declare function applyDefaultPolicies(g: CardinalityGuard): void;
|