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
package/dist/search.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { getDb } from "./db.js";
|
|
2
|
+
declare function tierDelta(tier: string | null | undefined): number;
|
|
3
|
+
declare function sourceTypeDelta(sourceType: string | null | undefined, section: string | null | undefined, queryEntityCount?: number): number;
|
|
4
|
+
declare function sectionDelta(section: string | null | undefined, sectionBoostCol: number | null | undefined): number;
|
|
5
|
+
interface SalienceChunkInput {
|
|
6
|
+
chunk_type?: string | null;
|
|
7
|
+
source_type?: string | null;
|
|
8
|
+
tier?: string | null;
|
|
9
|
+
pain?: number | null;
|
|
10
|
+
importance?: number | null;
|
|
11
|
+
retention_days?: number | null;
|
|
12
|
+
source_date?: string | null;
|
|
13
|
+
created_at?: string | null;
|
|
14
|
+
last_accessed_at?: string | null;
|
|
15
|
+
access_count?: number | null;
|
|
16
|
+
}
|
|
17
|
+
declare function salienceDelta(chunk: SalienceChunkInput, chunkId?: number): number;
|
|
18
|
+
export interface SearchResult {
|
|
19
|
+
id?: number;
|
|
20
|
+
score: number;
|
|
21
|
+
source_file: string;
|
|
22
|
+
chunk_type: string;
|
|
23
|
+
chunk_text: string;
|
|
24
|
+
source_date: string | null;
|
|
25
|
+
tier?: string;
|
|
26
|
+
section?: string | null;
|
|
27
|
+
pain?: number | null;
|
|
28
|
+
importance?: number | null;
|
|
29
|
+
source_type?: string | null;
|
|
30
|
+
match_type?: "fts" | "semantic" | "hybrid";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Increment access_count + last_accessed_at for the given chunk ids.
|
|
34
|
+
* `enabled=false` skips the write entirely — used by healthchecks / the
|
|
35
|
+
* semantic canary so automated probes don't inflate salience.
|
|
36
|
+
* D1 fix (2026-06-07): the canary ran /api/search 2×/h and, via searchHybrid,
|
|
37
|
+
* bumped access_count of its top-N candidates every cycle → feedback loop that
|
|
38
|
+
* pinned a handful of chunks at the top of every priming brief.
|
|
39
|
+
*/
|
|
40
|
+
declare function recordAccess(db: ReturnType<typeof getDb>, ids: Array<number | undefined>, enabled: boolean): void;
|
|
41
|
+
export declare function search(query: string, limit?: number, trackAccess?: boolean): SearchResult[];
|
|
42
|
+
export declare function searchSemantic(query: string, limit?: number, trackAccess?: boolean): Promise<SearchResult[]>;
|
|
43
|
+
export declare function searchHybrid(query: string, limit?: number, trackAccess?: boolean): Promise<SearchResult[]>;
|
|
44
|
+
export declare function formatResults(results: SearchResult[]): string;
|
|
45
|
+
export declare const _internals: {
|
|
46
|
+
recordAccess: typeof recordAccess;
|
|
47
|
+
SOURCE_TYPE_BOOST: Record<string, number>;
|
|
48
|
+
SECTION_BOOST: Record<string, number>;
|
|
49
|
+
BOOST_TYPES: Set<string>;
|
|
50
|
+
TYPE_BOOST_DELTA_FTS: number;
|
|
51
|
+
TYPE_BOOST_DELTA_SEMANTIC: number;
|
|
52
|
+
RECENCY_BOOST_DELTA_FTS: number;
|
|
53
|
+
RECENCY_BOOST_DELTA_SEMANTIC: number;
|
|
54
|
+
DISABLE_MUTEX_SECTION_SOURCE_TYPE: boolean;
|
|
55
|
+
MUTEX_QUERY_ENTITY_THRESHOLD: number;
|
|
56
|
+
DISABLE_CONDITIONAL_MUTEX: boolean;
|
|
57
|
+
tierDelta: typeof tierDelta;
|
|
58
|
+
sourceTypeDelta: typeof sourceTypeDelta;
|
|
59
|
+
sectionDelta: typeof sectionDelta;
|
|
60
|
+
salienceDelta: typeof salienceDelta;
|
|
61
|
+
};
|
|
62
|
+
export {};
|
package/dist/search.js
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import { createHash } from "crypto";
|
|
2
|
+
import { getDb } from "./db.js";
|
|
3
|
+
import { TIER_BOOST } from "./tier-manager.js";
|
|
4
|
+
import { expandQuery } from "./search-expansion.js";
|
|
5
|
+
import { dedupe } from "./search-dedup.js";
|
|
6
|
+
import { calculateSalience, calculateSalienceLegacy, getSalienceMode } from "./salience.js";
|
|
7
|
+
import { rerankByTemporalProximity, logTemporalProbe } from "./temporal-retrieval.js";
|
|
8
|
+
import { countQueryEntities } from "./query-entity-count.js";
|
|
9
|
+
// ─── Boost configuration (Fase 1.7a + A-boost-stack-wiring 2026-05-19) ────────
|
|
10
|
+
//
|
|
11
|
+
// G3 ablation (PR #146, 2026-05-19) proved every boost below was INERT in the
|
|
12
|
+
// deployed search.ts: section_boost / pain / source_type maps never matched
|
|
13
|
+
// the live corpus keys, and salience was observability-only. This module wires
|
|
14
|
+
// them up correctly for the first time.
|
|
15
|
+
//
|
|
16
|
+
// ## ADDITIVE pattern (CLAUDE.md rule #5)
|
|
17
|
+
//
|
|
18
|
+
// All boosts contribute a delta `(factor − 1)` into a single `boostSum`, then
|
|
19
|
+
// collapse via `score = baseScore * (1 + boostSum)`. Multiplicative stacking
|
|
20
|
+
// is forbidden — it amplifies tails super-linearly and caused incident v3.4.
|
|
21
|
+
//
|
|
22
|
+
// ## Env toggles (default: ALL boosts ACTIVE)
|
|
23
|
+
//
|
|
24
|
+
// NOX_DISABLE_TYPE_BOOST=1 — disable BOOST_TYPES (chunk_type)
|
|
25
|
+
// NOX_DISABLE_TIER_BOOST=1 — disable TIER_BOOST (tier column)
|
|
26
|
+
// NOX_DISABLE_SOURCE_TYPE_BOOST=1 — disable SOURCE_TYPE_BOOST (source_type)
|
|
27
|
+
// NOX_DISABLE_SECTION_BOOST=1 — disable SECTION_BOOST (section / section_boost)
|
|
28
|
+
// NOX_DISABLE_RECENCY_BOOST=1 — disable 7-day recency window boost
|
|
29
|
+
// NOX_SALIENCE_MODE=active — apply salience delta (shadow=DEFAULT, off=ablation)
|
|
30
|
+
//
|
|
31
|
+
// Defaults preserve backwards-compat: if no env vars are set, the multiplicative
|
|
32
|
+
// path is replaced by the equivalent additive path. The `NOX_SALIENCE_MODE`
|
|
33
|
+
// default stays `shadow` per architectural shadow-discipline (paper §4).
|
|
34
|
+
const BOOST_TYPES = new Set(["decision", "lesson", "person", "project", "pending"]);
|
|
35
|
+
// Legacy multiplicative factors → additive deltas (factor − 1):
|
|
36
|
+
const TYPE_BOOST_DELTA_FTS = 1.0; // was *2.0
|
|
37
|
+
const TYPE_BOOST_DELTA_SEMANTIC = 0.5; // was *1.5
|
|
38
|
+
const RECENCY_BOOST_DELTA_FTS = 0.5; // was *1.5
|
|
39
|
+
const RECENCY_BOOST_DELTA_SEMANTIC = 0.2; // was *1.2
|
|
40
|
+
// ── Source-attribution boost ──────────────────────────────────────────────────
|
|
41
|
+
//
|
|
42
|
+
// G3 audit (2026-05-19, n=68,995 chunks in prod): 98.48% NULL, 1.52% external.
|
|
43
|
+
// G5 V3 ablation A5 (2026-05-19) confirmed SOURCE_TYPE_BOOST inert because the
|
|
44
|
+
// only live key was `external` (1.5%) — A10 (full minus source_type) tied A8
|
|
45
|
+
// canonical at 0.6237, proving the map contributed 0%.
|
|
46
|
+
//
|
|
47
|
+
// G9 ablation (2026-05-20, g5.db prod n=69,495 chunks, n=100 queries) cravou
|
|
48
|
+
// que a calibração POST-backfill mantém o boost vivo PORÉM redundante com
|
|
49
|
+
// SECTION_BOOST em entity files (compiled/frontmatter/timeline):
|
|
50
|
+
// A0 (no boosts) = 0.4108
|
|
51
|
+
// A5 (source_type only) = 0.4693 → +14.2% vs A0 (boost LIVE)
|
|
52
|
+
// A8 (full canonical) = 0.5387
|
|
53
|
+
// A10 (full minus source_type) = 0.5530 → +2.6% vs A8 (REDUNDÂNCIA)
|
|
54
|
+
//
|
|
55
|
+
// Resolution (PR #180 Option 1): Hard Mutex — `sourceTypeDelta` retorna 0
|
|
56
|
+
// quando o chunk já tem `section` populado (sinal mais granular ganha). Spec:
|
|
57
|
+
// `specs/2026-05-20-mutual-exclusion-section-source-type.md`. Rollback rápido
|
|
58
|
+
// via env `NOX_DISABLE_MUTEX_SECTION_SOURCE_TYPE=1` (reverte ao pré-mutex).
|
|
59
|
+
//
|
|
60
|
+
// Post-backfill state (2026-05-19, audit_id=118, via PR #151):
|
|
61
|
+
// note ~31000 (~46%) — generic .md catch-all
|
|
62
|
+
// personal-doc ~23000 (~34%) — faturamento, contratos, planilhas
|
|
63
|
+
// ocr-cache ~11000 (~16%) — scan artifacts, low signal
|
|
64
|
+
// entity 749 (1.10%) — curated entity files (compiled/frontmatter/timeline)
|
|
65
|
+
// project-doc 560 (0.82%) — project planning docs
|
|
66
|
+
// session small — Cipher/Atlas/Boris/etc session checkpoints
|
|
67
|
+
// skill small — Claude Code skill defs
|
|
68
|
+
// command small — slash command defs
|
|
69
|
+
// lesson small — retrospective lessons learned
|
|
70
|
+
// legal-template small — disputes/contracts templates
|
|
71
|
+
// external 1046 (1.52%) — preserved (web/external content)
|
|
72
|
+
// other residual — unclassified fallback
|
|
73
|
+
//
|
|
74
|
+
// Calibration rationale (signal-to-noise × curation):
|
|
75
|
+
// 2.0 entity — highest curation, hand-authored truth
|
|
76
|
+
// 1.8 lesson — distilled retrospective, dense signal/token
|
|
77
|
+
// 1.5 skill — Claude Code skill definitions (curated)
|
|
78
|
+
// 1.4 project-doc — project planning (curated, scoped)
|
|
79
|
+
// 1.4 command — slash command defs (curated)
|
|
80
|
+
// 1.3 legal-template — legal templates (curated, low-volume)
|
|
81
|
+
// 1.2 personal-doc — faturamento/contratos (relevant, heterogeneous)
|
|
82
|
+
// 1.0 session — checkpoints (mixed signal)
|
|
83
|
+
// 1.0 note — generic .md baseline
|
|
84
|
+
// 0.8 external — web/external slight penalty (preserved)
|
|
85
|
+
// 0.7 other — unclassified fallback penalty
|
|
86
|
+
// 0.7 ocr-cache — scan artifacts, low signal-per-token (conservative)
|
|
87
|
+
//
|
|
88
|
+
// `ocr-cache` deliberately set to 0.7 (NOT 0.5 as a first instinct would
|
|
89
|
+
// suggest) per PR #154 code-review MEDIUM: ocr-cache is 16% of corpus and we
|
|
90
|
+
// have NO empirical evidence (pre-deploy) that −0.5 is safe in the live mix —
|
|
91
|
+
// a deeper penalty risks demoting golden hits backed by faturamento PDFs that
|
|
92
|
+
// fell through to OCR. Treat 0.7 as the conservative landing; a G6 ablation
|
|
93
|
+
// (post next eval cycle) can tighten to 0.5 if it proves net-positive on
|
|
94
|
+
// goldens. Same defensive posture as tier_boost default-off (PR #150).
|
|
95
|
+
//
|
|
96
|
+
// Forward-compat: `user_statement` retained — legitimate ingest path that
|
|
97
|
+
// hasn't landed yet (planning doc lineage). `compiled` / `timeline` removed
|
|
98
|
+
// from this map (2026-05-20, code-review LOW #2) — those are V10 `section`
|
|
99
|
+
// column values, NOT source_type values; keeping them here was confusing
|
|
100
|
+
// and they're already covered by SECTION_BOOST below.
|
|
101
|
+
const SOURCE_TYPE_BOOST = {
|
|
102
|
+
// Active keys (post-backfill 2026-05-19)
|
|
103
|
+
entity: 2.0,
|
|
104
|
+
lesson: 1.8,
|
|
105
|
+
skill: 1.5,
|
|
106
|
+
"project-doc": 1.4,
|
|
107
|
+
command: 1.4,
|
|
108
|
+
"legal-template": 1.3,
|
|
109
|
+
"personal-doc": 1.2,
|
|
110
|
+
session: 1.0,
|
|
111
|
+
note: 1.0,
|
|
112
|
+
external: 0.8,
|
|
113
|
+
other: 0.7,
|
|
114
|
+
"ocr-cache": 0.7,
|
|
115
|
+
// Forward-compat (ingest path planned but not landed):
|
|
116
|
+
user_statement: 2.0,
|
|
117
|
+
};
|
|
118
|
+
// ── Section boost (V10 schema, populated by ingestEntityFile) ─────────────────
|
|
119
|
+
//
|
|
120
|
+
// Audited 2026-05-19 (per WIP stash inspection):
|
|
121
|
+
// NULL: 68,246 (legacy non-entity chunks)
|
|
122
|
+
// timeline: 383
|
|
123
|
+
// frontmatter: 183
|
|
124
|
+
// compiled: 183
|
|
125
|
+
const SECTION_BOOST = {
|
|
126
|
+
compiled: 2.0, // truth section of an entity file (high signal)
|
|
127
|
+
frontmatter: 1.5, // YAML metadata (medium signal)
|
|
128
|
+
timeline: 0.8, // event log (lower signal per token)
|
|
129
|
+
};
|
|
130
|
+
// Module-load env flag snapshot (avoids per-chunk process.env read).
|
|
131
|
+
const DISABLE_TYPE_BOOST = process.env.NOX_DISABLE_TYPE_BOOST === "1";
|
|
132
|
+
// tier_boost DEFAULT DISABLED per G4 ablation (2026-05-19):
|
|
133
|
+
// A6 (tier only) = 0.4616 nDCG@10 < A0 (no boosts) = 0.4817.
|
|
134
|
+
// Core chunks (3.96% of corpus, memory-system internals) over-promote and push
|
|
135
|
+
// golden hits down. Opt-in via NOX_ENABLE_TIER_BOOST=1; legacy
|
|
136
|
+
// NOX_DISABLE_TIER_BOOST=1 honored as redundant (preserves back-compat).
|
|
137
|
+
// See docs/audits/2026-05-19-salience-distribution-audit.md.
|
|
138
|
+
const DISABLE_TIER_BOOST = process.env.NOX_DISABLE_TIER_BOOST === "1" ||
|
|
139
|
+
process.env.NOX_ENABLE_TIER_BOOST !== "1";
|
|
140
|
+
const DISABLE_SOURCE_TYPE_BOOST = process.env.NOX_DISABLE_SOURCE_TYPE_BOOST === "1";
|
|
141
|
+
const DISABLE_SECTION_BOOST = process.env.NOX_DISABLE_SECTION_BOOST === "1";
|
|
142
|
+
const DISABLE_RECENCY_BOOST = process.env.NOX_DISABLE_RECENCY_BOOST === "1";
|
|
143
|
+
// G9 mutex: default ON. Set `NOX_DISABLE_MUTEX_SECTION_SOURCE_TYPE=1` to revert
|
|
144
|
+
// to pre-mutex behaviour (both `sectionDelta` and `sourceTypeDelta` accumulate
|
|
145
|
+
// on entity-compiled/frontmatter/timeline chunks — known redundant per G9).
|
|
146
|
+
const DISABLE_MUTEX_SECTION_SOURCE_TYPE = process.env.NOX_DISABLE_MUTEX_SECTION_SOURCE_TYPE === "1";
|
|
147
|
+
// G10d conditional mutex: mutex is gated on query entity count.
|
|
148
|
+
//
|
|
149
|
+
// Evidence (G10b/G10c audits 2026-05-21):
|
|
150
|
+
// single-hop: +8.22% nDCG (mutex helps — removes double-boost on gold)
|
|
151
|
+
// multi-hop: −3.95% nDCG (mutex hurts — removes chain-traversal signal)
|
|
152
|
+
// style-agnostic (NL −3.91%, keyword −3.99%)
|
|
153
|
+
//
|
|
154
|
+
// Conditional logic:
|
|
155
|
+
// queryEntityCount ≤ MUTEX_QUERY_ENTITY_THRESHOLD (default 1) → mutex active
|
|
156
|
+
// queryEntityCount > threshold → mutex disabled
|
|
157
|
+
//
|
|
158
|
+
// Rollback:
|
|
159
|
+
// Tier 1 — NOX_DISABLE_CONDITIONAL_MUTEX=1 → hard mutex always-on (G10)
|
|
160
|
+
// Tier 2 — NOX_DISABLE_MUTEX_SECTION_SOURCE_TYPE=1 → no mutex at all (pre-G9)
|
|
161
|
+
//
|
|
162
|
+
// Spec: specs/2026-05-21-G10d-conditional-mutex-by-query-entities.md
|
|
163
|
+
/** Mutex threshold: entity count AT OR BELOW this value → mutex active. Default 1. */
|
|
164
|
+
const MUTEX_QUERY_ENTITY_THRESHOLD = Number.parseInt(process.env.NOX_MUTEX_QUERY_ENTITY_THRESHOLD ?? "1", 10);
|
|
165
|
+
/** When true, reverts to hard mutex always-on (G10 behaviour, ignores entity count). */
|
|
166
|
+
const DISABLE_CONDITIONAL_MUTEX = process.env.NOX_DISABLE_CONDITIONAL_MUTEX === "1";
|
|
167
|
+
// ─── Per-boost delta helpers ──────────────────────────────────────────────────
|
|
168
|
+
function tierDelta(tier) {
|
|
169
|
+
if (DISABLE_TIER_BOOST)
|
|
170
|
+
return 0;
|
|
171
|
+
const t = (tier ?? "peripheral");
|
|
172
|
+
const f = TIER_BOOST[t] ?? 1.0;
|
|
173
|
+
return f - 1.0;
|
|
174
|
+
}
|
|
175
|
+
function sourceTypeDelta(sourceType, section, queryEntityCount = 0) {
|
|
176
|
+
if (DISABLE_SOURCE_TYPE_BOOST || !sourceType)
|
|
177
|
+
return 0;
|
|
178
|
+
// HARD MUTEX (G9 evidence — spec PR #180 Option 1):
|
|
179
|
+
// Se o chunk já tem section_boost ativo (sinal mais granular), pula
|
|
180
|
+
// source_type_boost pra evitar double-boost em entity files.
|
|
181
|
+
//
|
|
182
|
+
// G10d CONDITIONAL LAYER: mutex applies only when queryEntityCount ≤ threshold.
|
|
183
|
+
// Multi-entity queries (≥2 entities) disable mutex to preserve chain traversal.
|
|
184
|
+
// - DISABLE_CONDITIONAL_MUTEX=true → ignores entity count, hard mutex always-on
|
|
185
|
+
// - queryEntityCount > MUTEX_QUERY_ENTITY_THRESHOLD → mutex bypassed
|
|
186
|
+
// - queryEntityCount ≤ MUTEX_QUERY_ENTITY_THRESHOLD → mutex active (current G10)
|
|
187
|
+
//
|
|
188
|
+
// Rollback: NOX_DISABLE_MUTEX_SECTION_SOURCE_TYPE=1 bypasses the entire mutex.
|
|
189
|
+
// Spec: specs/2026-05-21-G10d-conditional-mutex-by-query-entities.md §4 Step 2.
|
|
190
|
+
const mutexShouldApply = !DISABLE_MUTEX_SECTION_SOURCE_TYPE &&
|
|
191
|
+
!DISABLE_SECTION_BOOST &&
|
|
192
|
+
section != null &&
|
|
193
|
+
SECTION_BOOST[section] !== undefined;
|
|
194
|
+
if (mutexShouldApply) {
|
|
195
|
+
// G10d conditional: skip mutex when multi-entity query (unless conditional disabled)
|
|
196
|
+
const conditionalAllowsPass = !DISABLE_CONDITIONAL_MUTEX && queryEntityCount > MUTEX_QUERY_ENTITY_THRESHOLD;
|
|
197
|
+
if (!conditionalAllowsPass) {
|
|
198
|
+
return 0; // mutex active
|
|
199
|
+
}
|
|
200
|
+
// else: fall through — multi-entity query, mutex disabled
|
|
201
|
+
}
|
|
202
|
+
const f = SOURCE_TYPE_BOOST[sourceType] ?? 1.0;
|
|
203
|
+
return f - 1.0;
|
|
204
|
+
}
|
|
205
|
+
function sectionDelta(section, sectionBoostCol) {
|
|
206
|
+
if (DISABLE_SECTION_BOOST)
|
|
207
|
+
return 0;
|
|
208
|
+
// Canonical: map by section name (forward-stable across new schemas).
|
|
209
|
+
if (section && SECTION_BOOST[section] !== undefined) {
|
|
210
|
+
return SECTION_BOOST[section] - 1.0;
|
|
211
|
+
}
|
|
212
|
+
// Fallback: trust the section_boost column the ingester wrote
|
|
213
|
+
// (lets forward-compat fields work without touching this map).
|
|
214
|
+
if (sectionBoostCol !== null &&
|
|
215
|
+
sectionBoostCol !== undefined &&
|
|
216
|
+
Number.isFinite(sectionBoostCol)) {
|
|
217
|
+
return sectionBoostCol - 1.0;
|
|
218
|
+
}
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
221
|
+
// ─── Salience observability probes (MEDIUM #4 + #5, PR #150 review) ───────────
|
|
222
|
+
//
|
|
223
|
+
// Two opt-in env-gated probes that log salience telemetry WITHOUT affecting
|
|
224
|
+
// ranking. Both write JSON lines to stderr; downstream telemetry collector
|
|
225
|
+
// (per /api/health pipeline, CLAUDE.md §5) is expected to ingest these.
|
|
226
|
+
//
|
|
227
|
+
// NOX_SALIENCE_SHADOW_LOG=1 — log v2 salience computed in shadow mode
|
|
228
|
+
// NOX_SALIENCE_AB_SHADOW=1 — log (v2, legacy, delta) per chunk for
|
|
229
|
+
// A/B comparison without flipping to active
|
|
230
|
+
//
|
|
231
|
+
// These never throw — observability must not derail search.
|
|
232
|
+
function shadowProbeSalience(s, chunkId) {
|
|
233
|
+
if (process.env.NOX_SALIENCE_SHADOW_LOG !== "1")
|
|
234
|
+
return;
|
|
235
|
+
try {
|
|
236
|
+
process.stderr.write(JSON.stringify({ type: "shadow_salience", chunk_id: chunkId, salience: s, ts: Date.now() }) + "\n");
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
/* observability must not throw */
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function abShadowProbe(sV2, sLegacy, chunkId) {
|
|
243
|
+
if (process.env.NOX_SALIENCE_AB_SHADOW !== "1")
|
|
244
|
+
return;
|
|
245
|
+
try {
|
|
246
|
+
process.stderr.write(JSON.stringify({
|
|
247
|
+
type: "ab_salience",
|
|
248
|
+
chunk_id: chunkId,
|
|
249
|
+
v2: sV2,
|
|
250
|
+
legacy: sLegacy,
|
|
251
|
+
delta: sV2 - sLegacy,
|
|
252
|
+
ts: Date.now(),
|
|
253
|
+
}) + "\n");
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
/* observability must not throw */
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function salienceDelta(chunk, chunkId) {
|
|
260
|
+
const mode = getSalienceMode();
|
|
261
|
+
// Shadow probe: compute v2 even in shadow mode for telemetry,
|
|
262
|
+
// but return 0 (no ranking effect).
|
|
263
|
+
if (mode === "shadow") {
|
|
264
|
+
try {
|
|
265
|
+
shadowProbeSalience(calculateSalience(chunk), chunkId);
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
/* probe must not throw */
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
// A/B sentinel: opt-in dual-formula logging regardless of mode.
|
|
272
|
+
// Cheap (2 pure-fn calls); skipped entirely when env flag absent.
|
|
273
|
+
if (process.env.NOX_SALIENCE_AB_SHADOW === "1") {
|
|
274
|
+
try {
|
|
275
|
+
const sV2 = calculateSalience(chunk);
|
|
276
|
+
const sLegacy = calculateSalienceLegacy(chunk);
|
|
277
|
+
abShadowProbe(sV2, sLegacy, chunkId);
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
/* probe must not throw */
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (mode !== "active")
|
|
284
|
+
return 0;
|
|
285
|
+
const s = calculateSalience(chunk);
|
|
286
|
+
// Neutral baseline 0.5: salience=0.5 → no net effect; salience=1.0 → +0.5;
|
|
287
|
+
// salience=0 → −0.5. Bounded delta keeps multi-stack stacking sane.
|
|
288
|
+
return s - 0.5;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Increment access_count + last_accessed_at for the given chunk ids.
|
|
292
|
+
* `enabled=false` skips the write entirely — used by healthchecks / the
|
|
293
|
+
* semantic canary so automated probes don't inflate salience.
|
|
294
|
+
* D1 fix (2026-06-07): the canary ran /api/search 2×/h and, via searchHybrid,
|
|
295
|
+
* bumped access_count of its top-N candidates every cycle → feedback loop that
|
|
296
|
+
* pinned a handful of chunks at the top of every priming brief.
|
|
297
|
+
*/
|
|
298
|
+
function recordAccess(db, ids, enabled) {
|
|
299
|
+
if (!enabled)
|
|
300
|
+
return;
|
|
301
|
+
const valid = ids.filter((x) => Boolean(x));
|
|
302
|
+
if (valid.length === 0)
|
|
303
|
+
return;
|
|
304
|
+
const ts = new Date().toISOString();
|
|
305
|
+
const placeholders = valid.map(() => "?").join(",");
|
|
306
|
+
db.prepare(`UPDATE chunks SET access_count = access_count + 1, last_accessed_at = ? WHERE id IN (${placeholders})`).run(ts, ...valid);
|
|
307
|
+
}
|
|
308
|
+
export function search(query, limit = 5, trackAccess = true) {
|
|
309
|
+
const db = getDb();
|
|
310
|
+
const sanitized = query.replace(/[^\p{L}\p{N}\s]/gu, " ").replace(/\s+/g, " ").trim();
|
|
311
|
+
if (!sanitized)
|
|
312
|
+
return [];
|
|
313
|
+
// G10d: compute entity count once per query (amortised via cache, <1ms hot path).
|
|
314
|
+
const { count: queryEntityCount } = countQueryEntities(query, db);
|
|
315
|
+
let rows;
|
|
316
|
+
try {
|
|
317
|
+
rows = db.prepare(`
|
|
318
|
+
SELECT c.id, c.source_file, c.chunk_type, c.chunk_text, c.source_date,
|
|
319
|
+
c.tier, c.source_type, c.section, c.section_boost,
|
|
320
|
+
c.pain, c.importance, c.retention_days, c.created_at, c.last_accessed_at,
|
|
321
|
+
c.access_count,
|
|
322
|
+
bm25(chunks_fts, 1.0, 0.5, 0.5) as rank
|
|
323
|
+
FROM chunks_fts
|
|
324
|
+
JOIN chunks c ON c.id = chunks_fts.rowid
|
|
325
|
+
WHERE chunks_fts MATCH ?
|
|
326
|
+
ORDER BY rank LIMIT 20
|
|
327
|
+
`).all(sanitized);
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return [];
|
|
331
|
+
}
|
|
332
|
+
const now = new Date();
|
|
333
|
+
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000)
|
|
334
|
+
.toISOString().split("T")[0];
|
|
335
|
+
const scored = rows.map((row) => {
|
|
336
|
+
const baseScore = Math.abs(row.rank);
|
|
337
|
+
let boostSum = 0;
|
|
338
|
+
if (!DISABLE_TYPE_BOOST && BOOST_TYPES.has(row.chunk_type)) {
|
|
339
|
+
boostSum += TYPE_BOOST_DELTA_FTS;
|
|
340
|
+
}
|
|
341
|
+
if (!DISABLE_RECENCY_BOOST && row.source_date && row.source_date >= sevenDaysAgo) {
|
|
342
|
+
boostSum += RECENCY_BOOST_DELTA_FTS;
|
|
343
|
+
}
|
|
344
|
+
boostSum += tierDelta(row.tier);
|
|
345
|
+
boostSum += sourceTypeDelta(row.source_type, row.section, queryEntityCount);
|
|
346
|
+
boostSum += sectionDelta(row.section, row.section_boost);
|
|
347
|
+
boostSum += salienceDelta(row, row.id);
|
|
348
|
+
const score = baseScore * (1 + boostSum);
|
|
349
|
+
return {
|
|
350
|
+
id: row.id,
|
|
351
|
+
score: Math.round(score * 100) / 100,
|
|
352
|
+
source_file: row.source_file,
|
|
353
|
+
chunk_type: row.chunk_type,
|
|
354
|
+
chunk_text: row.chunk_text,
|
|
355
|
+
source_date: row.source_date,
|
|
356
|
+
tier: row.tier ?? "peripheral",
|
|
357
|
+
section: row.section,
|
|
358
|
+
pain: row.pain,
|
|
359
|
+
importance: row.importance,
|
|
360
|
+
source_type: row.source_type,
|
|
361
|
+
match_type: "fts",
|
|
362
|
+
};
|
|
363
|
+
});
|
|
364
|
+
scored.sort((a, b) => b.score - a.score);
|
|
365
|
+
const results = scored.slice(0, limit);
|
|
366
|
+
// Track access (skipped when trackAccess=false, e.g. healthchecks/canary)
|
|
367
|
+
recordAccess(db, results.map((r) => r.id), trackAccess);
|
|
368
|
+
return results;
|
|
369
|
+
}
|
|
370
|
+
export async function searchSemantic(query, limit = 5, trackAccess = true) {
|
|
371
|
+
try {
|
|
372
|
+
const { embedText, semanticSearch, ensureVecTable, countEmbedded } = await import("./embed.js");
|
|
373
|
+
const db = getDb();
|
|
374
|
+
ensureVecTable(db);
|
|
375
|
+
// Check if index has any embeddings
|
|
376
|
+
const vecCount = countEmbedded(db);
|
|
377
|
+
if (vecCount === 0) {
|
|
378
|
+
console.error("[WARN] Vector index empty — run 'nox-mem vectorize' first. Falling back to FTS5.");
|
|
379
|
+
return search(query, limit, trackAccess);
|
|
380
|
+
}
|
|
381
|
+
// G10d: compute entity count once per query (shared cache with FTS path).
|
|
382
|
+
const { count: queryEntityCount } = countQueryEntities(query, db);
|
|
383
|
+
const queryEmbedding = await embedText(query);
|
|
384
|
+
const rows = semanticSearch(db, queryEmbedding, limit * 2);
|
|
385
|
+
if (rows.length === 0)
|
|
386
|
+
return [];
|
|
387
|
+
// Fetch boost-stack columns in one shot.
|
|
388
|
+
const chunkIds = rows.map((r) => r.chunk_id).filter(Boolean);
|
|
389
|
+
const boostMap = new Map();
|
|
390
|
+
if (chunkIds.length > 0) {
|
|
391
|
+
const placeholders = chunkIds.map(() => "?").join(",");
|
|
392
|
+
const boostRows = db.prepare(`
|
|
393
|
+
SELECT id, tier, source_type, section, section_boost,
|
|
394
|
+
pain, importance, retention_days, created_at, last_accessed_at,
|
|
395
|
+
access_count, chunk_type
|
|
396
|
+
FROM chunks WHERE id IN (${placeholders})
|
|
397
|
+
`).all(...chunkIds);
|
|
398
|
+
for (const br of boostRows)
|
|
399
|
+
boostMap.set(br.id, br);
|
|
400
|
+
}
|
|
401
|
+
const maxDist = Math.max(...rows.map((r) => r.distance));
|
|
402
|
+
const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
|
|
403
|
+
.toISOString().split("T")[0];
|
|
404
|
+
const scored = rows.map((row) => {
|
|
405
|
+
const baseScore = maxDist > 0 ? (1 - row.distance / maxDist) * 10 : 10;
|
|
406
|
+
const info = row.chunk_id ? boostMap.get(row.chunk_id) : undefined;
|
|
407
|
+
let boostSum = 0;
|
|
408
|
+
if (!DISABLE_TYPE_BOOST && BOOST_TYPES.has(row.chunk_type)) {
|
|
409
|
+
boostSum += TYPE_BOOST_DELTA_SEMANTIC;
|
|
410
|
+
}
|
|
411
|
+
if (!DISABLE_RECENCY_BOOST && row.source_date && row.source_date >= sevenDaysAgo) {
|
|
412
|
+
boostSum += RECENCY_BOOST_DELTA_SEMANTIC;
|
|
413
|
+
}
|
|
414
|
+
boostSum += tierDelta(info?.tier);
|
|
415
|
+
boostSum += sourceTypeDelta(info?.source_type, info?.section, queryEntityCount);
|
|
416
|
+
boostSum += sectionDelta(info?.section, info?.section_boost);
|
|
417
|
+
if (info) {
|
|
418
|
+
boostSum += salienceDelta({
|
|
419
|
+
chunk_type: info.chunk_type,
|
|
420
|
+
source_type: info.source_type,
|
|
421
|
+
tier: info.tier,
|
|
422
|
+
pain: info.pain,
|
|
423
|
+
importance: info.importance,
|
|
424
|
+
retention_days: info.retention_days,
|
|
425
|
+
created_at: info.created_at,
|
|
426
|
+
last_accessed_at: info.last_accessed_at,
|
|
427
|
+
access_count: info.access_count,
|
|
428
|
+
source_date: row.source_date,
|
|
429
|
+
}, info.id);
|
|
430
|
+
}
|
|
431
|
+
const score = baseScore * (1 + boostSum);
|
|
432
|
+
const tier = (info?.tier ?? "peripheral");
|
|
433
|
+
return {
|
|
434
|
+
id: row.chunk_id,
|
|
435
|
+
score: Math.round(score * 100) / 100,
|
|
436
|
+
source_file: row.source_file,
|
|
437
|
+
chunk_type: row.chunk_type,
|
|
438
|
+
chunk_text: row.chunk_text,
|
|
439
|
+
source_date: row.source_date,
|
|
440
|
+
tier: tier,
|
|
441
|
+
section: info?.section ?? null,
|
|
442
|
+
pain: info?.pain ?? null,
|
|
443
|
+
importance: info?.importance ?? null,
|
|
444
|
+
source_type: info?.source_type ?? null,
|
|
445
|
+
match_type: "semantic",
|
|
446
|
+
};
|
|
447
|
+
});
|
|
448
|
+
scored.sort((a, b) => b.score - a.score);
|
|
449
|
+
const semResults = scored.slice(0, limit);
|
|
450
|
+
// Track access (skipped when trackAccess=false, e.g. healthchecks/canary)
|
|
451
|
+
recordAccess(db, semResults.map((r) => r.id), trackAccess);
|
|
452
|
+
return semResults;
|
|
453
|
+
}
|
|
454
|
+
catch (err) {
|
|
455
|
+
// Fallback to FTS if vector index not ready
|
|
456
|
+
console.error("[WARN] Semantic search failed, falling back to FTS:", err.message);
|
|
457
|
+
return search(query, limit, trackAccess);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// ─── Hybrid search (FTS5 + semantic, expanded, RRF-fused, deduped) ───────────
|
|
461
|
+
function rrfScore(rank, k = 60) {
|
|
462
|
+
return 1 / (k + rank + 1);
|
|
463
|
+
}
|
|
464
|
+
function logTelemetry(query, variantsCount, resultsCount, hasSemantic, latencyMs, skipReason) {
|
|
465
|
+
try {
|
|
466
|
+
const db = getDb();
|
|
467
|
+
const hash = createHash("sha1").update(query).digest("hex").substring(0, 16);
|
|
468
|
+
const words = query.trim().split(/\s+/).filter(Boolean).length;
|
|
469
|
+
db.prepare(`INSERT INTO search_telemetry (query_hash, query_words, variants_count, results_count, has_semantic, latency_ms, expansion_skipped_reason)
|
|
470
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`).run(hash, words, variantsCount, resultsCount, hasSemantic ? 1 : 0, latencyMs, skipReason || null);
|
|
471
|
+
}
|
|
472
|
+
catch {
|
|
473
|
+
// telemetria nunca derruba a search
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
export async function searchHybrid(query, limit = 5, trackAccess = true) {
|
|
477
|
+
const t0 = Date.now();
|
|
478
|
+
const perVariantLimit = limit * 2;
|
|
479
|
+
// Kick off original-query searches IMMEDIATELY and expansion in parallel.
|
|
480
|
+
// Total time = max(expansion + variantFTS, originalFTS+semantic) — does not block
|
|
481
|
+
// the original search behind a 500-1500ms Gemini call.
|
|
482
|
+
const originalFtsPromise = Promise.resolve(search(query.trim(), perVariantLimit, trackAccess));
|
|
483
|
+
const semPromise = searchSemantic(query.trim(), perVariantLimit * 2, trackAccess);
|
|
484
|
+
const expansionPromise = expandQuery(query);
|
|
485
|
+
const expansion = await expansionPromise;
|
|
486
|
+
const variants = expansion.variants;
|
|
487
|
+
// Variants (excluding the original, which is already running) → FTS only.
|
|
488
|
+
const extraVariantFtsPromises = variants.slice(1).map((v) => Promise.resolve(search(v, perVariantLimit, trackAccess)));
|
|
489
|
+
const allBatches = await Promise.all([
|
|
490
|
+
originalFtsPromise,
|
|
491
|
+
...extraVariantFtsPromises,
|
|
492
|
+
semPromise,
|
|
493
|
+
]);
|
|
494
|
+
// Fuse via RRF. Rank within EACH batch.
|
|
495
|
+
const scoreMap = new Map();
|
|
496
|
+
const semanticBatchIdx = allBatches.length - 1; // last is the semantic batch
|
|
497
|
+
allBatches.forEach((batch, batchIdx) => {
|
|
498
|
+
const isSemanticBatch = batchIdx === semanticBatchIdx;
|
|
499
|
+
batch.forEach((r, rank) => {
|
|
500
|
+
const key = `${r.source_file}::${r.chunk_text.substring(0, 50)}`;
|
|
501
|
+
const existing = scoreMap.get(key);
|
|
502
|
+
const scoreInc = rrfScore(rank);
|
|
503
|
+
if (existing) {
|
|
504
|
+
existing.rrfScore += scoreInc;
|
|
505
|
+
existing.saw_semantic = existing.saw_semantic || isSemanticBatch;
|
|
506
|
+
if (existing.saw_semantic && (existing.match_type === "fts" || isSemanticBatch)) {
|
|
507
|
+
existing.match_type = isSemanticBatch && existing.match_type === "fts" ? "hybrid" : existing.match_type;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
scoreMap.set(key, {
|
|
512
|
+
...r,
|
|
513
|
+
rrfScore: scoreInc,
|
|
514
|
+
saw_semantic: isSemanticBatch,
|
|
515
|
+
match_type: isSemanticBatch ? "semantic" : "fts",
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
// Promote to hybrid any result touched by both fts and semantic batches
|
|
521
|
+
for (const v of scoreMap.values()) {
|
|
522
|
+
if (v.saw_semantic && v.match_type !== "semantic")
|
|
523
|
+
v.match_type = "hybrid";
|
|
524
|
+
}
|
|
525
|
+
const preDedup = Array.from(scoreMap.values())
|
|
526
|
+
.sort((a, b) => b.rrfScore - a.rrfScore)
|
|
527
|
+
.slice(0, Math.max(limit * 3, 15))
|
|
528
|
+
.map(({ rrfScore: s, saw_semantic: _, ...r }) => ({ ...r, score: Math.round(s * 1000 * 100) / 100 }));
|
|
529
|
+
const final = dedupe(preDedup, limit);
|
|
530
|
+
const hasSemantic = final.some((r) => r.match_type === "semantic" || r.match_type === "hybrid");
|
|
531
|
+
logTelemetry(query, variants.length, final.length, hasSemantic, Date.now() - t0, expansion.reason);
|
|
532
|
+
// D49 Phase 1 — temporal proximity rerank, shadow-mode opt-in.
|
|
533
|
+
// Only activates if NOX_TEMPORAL_PATH=shadow|active. In shadow mode the
|
|
534
|
+
// module computes the would-be rerank report but does NOT mutate `final`;
|
|
535
|
+
// it emits one stderr JSON line (type=temporal_path) for telemetry.
|
|
536
|
+
// Ranking semantics remain identical when env is unset or =off.
|
|
537
|
+
if (process.env.NOX_TEMPORAL_PATH && process.env.NOX_TEMPORAL_PATH !== "off") {
|
|
538
|
+
try {
|
|
539
|
+
const { report } = rerankByTemporalProximity(final, query);
|
|
540
|
+
if (report.isTemporal) {
|
|
541
|
+
const queryHash = createHash("sha1").update(query).digest("hex").slice(0, 12);
|
|
542
|
+
logTemporalProbe(report, queryHash);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
catch (e) {
|
|
546
|
+
// observability must never break ranking
|
|
547
|
+
process.stderr.write(JSON.stringify({ type: "temporal_path_error", err: String(e) }) + "\n");
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return final;
|
|
551
|
+
}
|
|
552
|
+
// ─── Format results ───────────────────────────────────────────────────────────
|
|
553
|
+
export function formatResults(results) {
|
|
554
|
+
if (results.length === 0)
|
|
555
|
+
return "No results found.";
|
|
556
|
+
return results
|
|
557
|
+
.map((r, i) => {
|
|
558
|
+
const preview = r.chunk_text.substring(0, 200).replace(/\n/g, " ");
|
|
559
|
+
const tag = r.match_type ? ` [${r.match_type}]` : "";
|
|
560
|
+
return `#${i + 1} [${r.score}${tag}] ${r.source_file}\n "${preview}..."`;
|
|
561
|
+
})
|
|
562
|
+
.join("\n\n");
|
|
563
|
+
}
|
|
564
|
+
// ─── Test-only exports (named with _ prefix to signal "do not use externally") ─
|
|
565
|
+
export const _internals = {
|
|
566
|
+
recordAccess,
|
|
567
|
+
SOURCE_TYPE_BOOST,
|
|
568
|
+
SECTION_BOOST,
|
|
569
|
+
BOOST_TYPES,
|
|
570
|
+
TYPE_BOOST_DELTA_FTS,
|
|
571
|
+
TYPE_BOOST_DELTA_SEMANTIC,
|
|
572
|
+
RECENCY_BOOST_DELTA_FTS,
|
|
573
|
+
RECENCY_BOOST_DELTA_SEMANTIC,
|
|
574
|
+
DISABLE_MUTEX_SECTION_SOURCE_TYPE,
|
|
575
|
+
// G10d conditional mutex
|
|
576
|
+
MUTEX_QUERY_ENTITY_THRESHOLD,
|
|
577
|
+
DISABLE_CONDITIONAL_MUTEX,
|
|
578
|
+
tierDelta,
|
|
579
|
+
sourceTypeDelta,
|
|
580
|
+
sectionDelta,
|
|
581
|
+
salienceDelta,
|
|
582
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type ProposalKind = 'perf_regression' | 'error_spike' | 'dormant_command' | 'capacity_warning' | 'first_use' | 'recovery';
|
|
2
|
+
export interface SehProposal {
|
|
3
|
+
kind: ProposalKind;
|
|
4
|
+
command: string;
|
|
5
|
+
severity: 'info' | 'warn' | 'alert';
|
|
6
|
+
metric: string;
|
|
7
|
+
current: number | string;
|
|
8
|
+
baseline: number | string;
|
|
9
|
+
delta_pct?: number;
|
|
10
|
+
recommended_action: string;
|
|
11
|
+
config_patch?: {
|
|
12
|
+
env_var: string;
|
|
13
|
+
current_value: string | null;
|
|
14
|
+
suggested_value: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface SehReport {
|
|
18
|
+
window_current_days: number;
|
|
19
|
+
window_baseline_days: number;
|
|
20
|
+
total_proposals: number;
|
|
21
|
+
by_severity: Record<string, number>;
|
|
22
|
+
by_kind: Record<string, number>;
|
|
23
|
+
proposals: SehProposal[];
|
|
24
|
+
duration_ms: number;
|
|
25
|
+
}
|
|
26
|
+
export declare function detectAndPropose(opts?: {
|
|
27
|
+
windowDays?: number;
|
|
28
|
+
minRuns?: number;
|
|
29
|
+
dormantDays?: number;
|
|
30
|
+
}): SehReport;
|
|
31
|
+
export declare function formatSehReport(r: SehReport, mode?: 'json' | 'text'): string;
|