nox-mem 3.1.1 → 3.2.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/README.md +24 -16
- package/dist/__tests__/brief-diversity.test.js +266 -0
- package/dist/__tests__/brief.test.js +287 -0
- package/dist/__tests__/churn.test.js +66 -0
- package/dist/__tests__/edge-typing.test.js +99 -0
- package/dist/__tests__/ingest-event.test.js +151 -0
- package/dist/__tests__/op-audit-e2e.test.js +130 -0
- package/dist/__tests__/pragma-alignment.test.js +123 -0
- package/dist/__tests__/reranker.test.js +326 -0
- package/dist/__tests__/retention.test.js +99 -0
- package/dist/__tests__/search-track-access.test.js +49 -0
- package/dist/__tests__/spo-injection.test.js +172 -0
- package/dist/__tests__/temporal-detector.test.js +103 -0
- package/dist/api/wire-up.js +12 -381
- package/dist/api-server.js +6 -102
- package/dist/db.js +61 -2
- package/dist/embed.js +23 -2
- package/dist/index.js +1 -22
- package/dist/knowledge-graph.js +20 -0
- package/dist/lib/answer/__tests__/e2e-gemini.test.js +229 -0
- package/dist/lib/answer/__tests__/integration-sqlite.test.js +388 -0
- package/dist/lib/answer/__tests__/integration.test.js +347 -0
- package/dist/lib/answer/__tests__/prompt.test.js +117 -0
- package/dist/lib/answer/__tests__/t5-t10.test.js +501 -0
- package/dist/lib/answer/provider.js +29 -4
- package/dist/lib/api/__tests__/safe-error-message.test.js +155 -0
- package/dist/lib/auth/__tests__/localhost-guard.test.js +144 -0
- package/dist/lib/error-sanitizer/__tests__/sanitize.test.js +354 -0
- package/dist/lib/events/__tests__/bus.test.js +208 -0
- package/dist/lib/ingest-router.js +0 -43
- package/dist/lib/op-audit-extension/__tests__/ran-at-guard.test.js +109 -0
- package/dist/lib/regex-extract/__tests__/extractor.test.js +197 -0
- package/dist/lib/regex-extract/__tests__/frontmatter.test.js +75 -0
- package/dist/lib/regex-extract/__tests__/ingest-router-l4.test.js +139 -0
- package/dist/lib/regex-extract/__tests__/production-wire.test.js +177 -0
- package/dist/lib/regex-extract/__tests__/reconcile.test.js +170 -0
- package/dist/lib/regex-extract/__tests__/strip-code.test.js +36 -0
- package/dist/privacy/__tests__/filter.test.js +352 -0
- package/dist/providers/__tests__/conformance-extended.test.js +249 -0
- package/dist/providers/__tests__/conformance.test.js +98 -0
- package/dist/providers/__tests__/cost-cap.test.js +151 -0
- package/dist/providers/__tests__/e2e-real.test.js +79 -0
- package/dist/providers/__tests__/factory.test.js +80 -0
- package/dist/providers/__tests__/fallback.test.js +223 -0
- package/dist/providers/__tests__/gemini.test.js +191 -0
- package/dist/providers/__tests__/health.test.js +128 -0
- package/dist/providers/__tests__/telemetry.test.js +219 -0
- package/dist/reindex.js +2 -2
- package/package.json +3 -4
- package/dist/api/answer.d.ts +0 -116
- package/dist/api/answer.validate-patch.d.ts +0 -72
- package/dist/api/brief-diversity.d.ts +0 -64
- package/dist/api/brief.d.ts +0 -114
- package/dist/api/conflict.d.ts +0 -33
- package/dist/api/conflict.js +0 -143
- package/dist/api/cors.d.ts +0 -73
- package/dist/api/events-stream-limited.d.ts +0 -133
- package/dist/api/events-stream-limited.js +0 -217
- package/dist/api/events-stream.d.ts +0 -53
- package/dist/api/events-stream.js +0 -125
- package/dist/api/export.d.ts +0 -42
- package/dist/api/export.js +0 -85
- package/dist/api/health-confidence-adapter.d.ts +0 -29
- package/dist/api/health-confidence-adapter.js +0 -73
- package/dist/api/health-confidence.d.ts +0 -36
- package/dist/api/health-confidence.js +0 -141
- package/dist/api/hooks.d.ts +0 -50
- package/dist/api/hooks.js +0 -115
- package/dist/api/import.d.ts +0 -38
- package/dist/api/import.js +0 -87
- package/dist/api/ingest-event.d.ts +0 -65
- package/dist/api/mark.d.ts +0 -42
- package/dist/api/mark.js +0 -127
- package/dist/api/server-deps-a2.d.ts +0 -55
- package/dist/api/server-deps-a2.js +0 -138
- package/dist/api/server-deps-l2-l3.d.ts +0 -68
- package/dist/api/server-deps-l2-l3.js +0 -123
- package/dist/api/server-deps-p1.d.ts +0 -84
- package/dist/api/server-deps-p2.d.ts +0 -32
- package/dist/api/server-deps-p2.js +0 -84
- package/dist/api/server-deps-p5.d.ts +0 -66
- package/dist/api/server-deps-p5.js +0 -139
- package/dist/api/viewer-static.d.ts +0 -30
- package/dist/api/viewer-static.js +0 -102
- package/dist/api/wire-up.d.ts +0 -54
- package/dist/api-impact.d.ts +0 -30
- package/dist/api-server.d.ts +0 -2
- package/dist/backfill-fts-anchor.d.ts +0 -15
- package/dist/backfill-source-type.d.ts +0 -20
- package/dist/churn.d.ts +0 -31
- package/dist/cli/answer.d.ts +0 -66
- package/dist/cli/conflict.d.ts +0 -38
- package/dist/cli/conflict.js +0 -282
- package/dist/cli/export.d.ts +0 -51
- package/dist/cli/export.js +0 -280
- package/dist/cli/hooks.d.ts +0 -54
- package/dist/cli/hooks.js +0 -133
- package/dist/cli/import.d.ts +0 -52
- package/dist/cli/import.js +0 -274
- package/dist/cli/mark.d.ts +0 -33
- package/dist/cli/mark.js +0 -130
- package/dist/cli/ocr-batch.d.ts +0 -12
- package/dist/cli/ocr-batch.js +0 -411
- package/dist/cli/snapshot-main.d.ts +0 -30
- package/dist/cli/snapshot-main.js +0 -47
- package/dist/cli/viewer.d.ts +0 -35
- package/dist/cli/viewer.js +0 -71
- package/dist/cli-telemetry.d.ts +0 -36
- package/dist/cli-telemetry.js +0 -150
- package/dist/compact.d.ts +0 -7
- package/dist/consolidate.d.ts +0 -18
- package/dist/consolidation.d.ts +0 -32
- package/dist/cross-agent-v2.d.ts +0 -45
- package/dist/cross-search.d.ts +0 -11
- package/dist/crystallize.d.ts +0 -43
- package/dist/db.d.ts +0 -11
- package/dist/dedup.d.ts +0 -1
- package/dist/detect-changes.d.ts +0 -29
- package/dist/digest.d.ts +0 -1
- package/dist/doctor.d.ts +0 -1
- package/dist/embed.d.ts +0 -57
- package/dist/evals.d.ts +0 -176
- package/dist/evals.js +0 -351
- package/dist/generate-user-profile.d.ts +0 -18
- package/dist/graphify-ingest.d.ts +0 -18
- package/dist/impact.d.ts +0 -39
- package/dist/index.d.ts +0 -2
- package/dist/ingest-entity.d.ts +0 -44
- package/dist/ingest.d.ts +0 -13
- package/dist/kg-llm.d.ts +0 -39
- package/dist/knowledge-graph.d.ts +0 -72
- package/dist/lib/answer/config.d.ts +0 -41
- package/dist/lib/answer/index.d.ts +0 -47
- package/dist/lib/answer/prompt.d.ts +0 -37
- package/dist/lib/answer/provider.d.ts +0 -47
- package/dist/lib/answer/retrieval.d.ts +0 -36
- package/dist/lib/answer/telemetry.d.ts +0 -99
- package/dist/lib/answer/types.d.ts +0 -108
- package/dist/lib/api/error-leak-fix.d.ts +0 -54
- package/dist/lib/api/safe-error-message.d.ts +0 -70
- package/dist/lib/archive/common-passwords.d.ts +0 -37
- package/dist/lib/archive/common-passwords.js +0 -753
- package/dist/lib/archive/encryption.d.ts +0 -81
- package/dist/lib/archive/encryption.js +0 -236
- package/dist/lib/archive/enforce-strength.d.ts +0 -54
- package/dist/lib/archive/enforce-strength.js +0 -70
- package/dist/lib/archive/entropy.d.ts +0 -34
- package/dist/lib/archive/entropy.js +0 -180
- package/dist/lib/archive/export-locking.d.ts +0 -102
- package/dist/lib/archive/export-locking.js +0 -202
- package/dist/lib/archive/format.d.ts +0 -20
- package/dist/lib/archive/format.js +0 -174
- package/dist/lib/archive/index.d.ts +0 -23
- package/dist/lib/archive/index.js +0 -17
- package/dist/lib/archive/manifest.d.ts +0 -53
- package/dist/lib/archive/manifest.js +0 -183
- package/dist/lib/archive/migration.d.ts +0 -36
- package/dist/lib/archive/migration.js +0 -100
- package/dist/lib/archive/migrations/v18_to_v19.d.ts +0 -16
- package/dist/lib/archive/migrations/v18_to_v19.js +0 -18
- package/dist/lib/archive/orchestrator.d.ts +0 -166
- package/dist/lib/archive/orchestrator.js +0 -428
- package/dist/lib/archive/serializers/chunks.d.ts +0 -30
- package/dist/lib/archive/serializers/chunks.js +0 -113
- package/dist/lib/archive/serializers/embeddings.d.ts +0 -36
- package/dist/lib/archive/serializers/embeddings.js +0 -115
- package/dist/lib/archive/serializers/kg.d.ts +0 -28
- package/dist/lib/archive/serializers/kg.js +0 -184
- package/dist/lib/archive/serializers/ops_audit.d.ts +0 -21
- package/dist/lib/archive/serializers/ops_audit.js +0 -97
- package/dist/lib/archive/server-deps.d.ts +0 -131
- package/dist/lib/archive/server-deps.js +0 -232
- package/dist/lib/archive/strength.d.ts +0 -50
- package/dist/lib/archive/strength.js +0 -77
- package/dist/lib/archive/types.d.ts +0 -159
- package/dist/lib/archive/types.js +0 -45
- package/dist/lib/archive/unpack-streaming.d.ts +0 -70
- package/dist/lib/archive/unpack-streaming.js +0 -255
- package/dist/lib/auth/localhost-guard.d.ts +0 -106
- package/dist/lib/confidence/config.d.ts +0 -37
- package/dist/lib/confidence/config.js +0 -107
- package/dist/lib/confidence/db-shim-singleton.d.ts +0 -20
- package/dist/lib/confidence/db-shim-singleton.js +0 -41
- package/dist/lib/confidence/db-shim.d.ts +0 -47
- package/dist/lib/confidence/db-shim.js +0 -141
- package/dist/lib/confidence/mark.d.ts +0 -58
- package/dist/lib/confidence/mark.js +0 -168
- package/dist/lib/confidence/ranking.d.ts +0 -69
- package/dist/lib/confidence/ranking.js +0 -101
- package/dist/lib/confidence/search-filter.d.ts +0 -44
- package/dist/lib/confidence/search-filter.js +0 -103
- package/dist/lib/confidence/types.d.ts +0 -146
- package/dist/lib/confidence/types.js +0 -20
- package/dist/lib/confidence/write-hooks.d.ts +0 -50
- package/dist/lib/confidence/write-hooks.js +0 -159
- package/dist/lib/conflict/audit-fk-check.d.ts +0 -72
- package/dist/lib/conflict/audit-fk-check.js +0 -131
- package/dist/lib/conflict/audit-writer.d.ts +0 -39
- package/dist/lib/conflict/audit-writer.js +0 -133
- package/dist/lib/conflict/db-singleton.d.ts +0 -28
- package/dist/lib/conflict/db-singleton.js +0 -61
- package/dist/lib/conflict/db.d.ts +0 -27
- package/dist/lib/conflict/db.js +0 -20
- package/dist/lib/conflict/detector-direct.d.ts +0 -21
- package/dist/lib/conflict/detector-direct.js +0 -105
- package/dist/lib/conflict/evidence.d.ts +0 -24
- package/dist/lib/conflict/evidence.js +0 -76
- package/dist/lib/conflict/index.d.ts +0 -14
- package/dist/lib/conflict/index.js +0 -12
- package/dist/lib/conflict/scheduler.d.ts +0 -78
- package/dist/lib/conflict/scheduler.js +0 -111
- package/dist/lib/conflict/shadow.d.ts +0 -75
- package/dist/lib/conflict/shadow.js +0 -161
- package/dist/lib/conflict/types.d.ts +0 -133
- package/dist/lib/conflict/types.js +0 -19
- package/dist/lib/cost-cap.d.ts +0 -94
- package/dist/lib/deps/deps-registry.d.ts +0 -111
- package/dist/lib/error-sanitizer/middleware.d.ts +0 -43
- package/dist/lib/error-sanitizer/sanitize.d.ts +0 -78
- package/dist/lib/eval-batch.d.ts +0 -34
- package/dist/lib/eval-batch.js +0 -103
- package/dist/lib/eval-metrics.d.ts +0 -36
- package/dist/lib/eval-metrics.js +0 -95
- package/dist/lib/eval.d.ts +0 -106
- package/dist/lib/eval.js +0 -343
- package/dist/lib/events/bus.d.ts +0 -135
- package/dist/lib/fts-anchor.d.ts +0 -1
- package/dist/lib/hooks/classifier.d.ts +0 -47
- package/dist/lib/hooks/classifier.js +0 -129
- package/dist/lib/hooks/config.d.ts +0 -49
- package/dist/lib/hooks/config.js +0 -106
- package/dist/lib/hooks/decorators.d.ts +0 -35
- package/dist/lib/hooks/decorators.js +0 -59
- package/dist/lib/hooks/pipeline.d.ts +0 -76
- package/dist/lib/hooks/pipeline.js +0 -213
- package/dist/lib/hooks/privacy-filter-adapter.d.ts +0 -55
- package/dist/lib/hooks/privacy-filter-adapter.js +0 -84
- package/dist/lib/hooks/rate-limit-constant-time.d.ts +0 -57
- package/dist/lib/hooks/rate-limit-constant-time.js +0 -69
- package/dist/lib/hooks/rate-limit-dryrun-fix.d.ts +0 -71
- package/dist/lib/hooks/rate-limit-dryrun-fix.js +0 -110
- package/dist/lib/hooks/rate-limit.d.ts +0 -54
- package/dist/lib/hooks/rate-limit.js +0 -122
- package/dist/lib/hooks/server-deps.d.ts +0 -61
- package/dist/lib/hooks/server-deps.js +0 -92
- package/dist/lib/hooks/source-allowlist.d.ts +0 -26
- package/dist/lib/hooks/source-allowlist.js +0 -53
- package/dist/lib/hooks/types.d.ts +0 -129
- package/dist/lib/hooks/types.js +0 -24
- package/dist/lib/hooks/worker.d.ts +0 -56
- package/dist/lib/hooks/worker.js +0 -150
- package/dist/lib/ingest-router.d.ts +0 -25
- package/dist/lib/ocr-detector.d.ts +0 -61
- package/dist/lib/ocr-detector.js +0 -138
- package/dist/lib/ocr-engine-stub.d.ts +0 -48
- package/dist/lib/ocr-engine-stub.js +0 -190
- package/dist/lib/ocr-engines/google-doc-ai.d.ts +0 -36
- package/dist/lib/ocr-engines/google-doc-ai.js +0 -164
- package/dist/lib/ocr-jobs.d.ts +0 -67
- package/dist/lib/ocr-jobs.js +0 -129
- package/dist/lib/op-audit-extension/ran-at-guard.d.ts +0 -86
- package/dist/lib/op-audit.d.ts +0 -71
- package/dist/lib/privacy-br/detector.d.ts +0 -42
- package/dist/lib/privacy-br/detector.js +0 -130
- package/dist/lib/privacy-br/index.d.ts +0 -20
- package/dist/lib/privacy-br/index.js +0 -20
- package/dist/lib/privacy-br/integration.d.ts +0 -71
- package/dist/lib/privacy-br/integration.js +0 -74
- package/dist/lib/privacy-br/patterns.d.ts +0 -117
- package/dist/lib/privacy-br/patterns.js +0 -434
- package/dist/lib/privacy-br/redact.d.ts +0 -42
- package/dist/lib/privacy-br/redact.js +0 -103
- package/dist/lib/privacy-br/types.d.ts +0 -55
- package/dist/lib/privacy-br/types.js +0 -25
- package/dist/lib/regex-extract/extractor.d.ts +0 -30
- package/dist/lib/regex-extract/frontmatter.d.ts +0 -44
- package/dist/lib/regex-extract/index.d.ts +0 -14
- package/dist/lib/regex-extract/ingest-router-l4.d.ts +0 -76
- package/dist/lib/regex-extract/patterns.d.ts +0 -61
- package/dist/lib/regex-extract/production-wire.d.ts +0 -128
- package/dist/lib/regex-extract/reconcile.d.ts +0 -162
- package/dist/lib/regex-extract/strip-code.d.ts +0 -24
- package/dist/lib/regex-extract/types.d.ts +0 -48
- package/dist/lib/reranker.d.ts +0 -80
- package/dist/lib/shadow-tracker.d.ts +0 -191
- package/dist/lib/shadow-tracker.js +0 -384
- package/dist/lib/spo-injection.d.ts +0 -52
- package/dist/lib/telemetry-collector.d.ts +0 -158
- package/dist/lib/telemetry-collector.js +0 -249
- package/dist/lib/temporal-detector.d.ts +0 -27
- package/dist/lib/viewer/auth.d.ts +0 -37
- package/dist/lib/viewer/auth.js +0 -70
- package/dist/lib/viewer/backpressure.d.ts +0 -59
- package/dist/lib/viewer/backpressure.js +0 -100
- package/dist/lib/viewer/broadcast-singleton.d.ts +0 -34
- package/dist/lib/viewer/broadcast-singleton.js +0 -49
- package/dist/lib/viewer/broadcast.d.ts +0 -66
- package/dist/lib/viewer/broadcast.js +0 -96
- package/dist/lib/viewer/event-types.d.ts +0 -132
- package/dist/lib/viewer/event-types.js +0 -74
- package/dist/lib/viewer/instrumentation.d.ts +0 -106
- package/dist/lib/viewer/instrumentation.js +0 -224
- package/dist/lib/viewer/migration.d.ts +0 -23
- package/dist/lib/viewer/migration.js +0 -56
- package/dist/lib/viewer/redaction.d.ts +0 -44
- package/dist/lib/viewer/redaction.js +0 -139
- package/dist/lib/viewer/session.d.ts +0 -51
- package/dist/lib/viewer/session.js +0 -87
- package/dist/mcp/tools/answer.d.ts +0 -94
- package/dist/mcp/tools/archive.d.ts +0 -152
- package/dist/mcp/tools/archive.js +0 -170
- package/dist/mcp/tools/conflict.d.ts +0 -44
- package/dist/mcp/tools/conflict.js +0 -130
- package/dist/mcp/tools/hooks.d.ts +0 -29
- package/dist/mcp/tools/hooks.js +0 -78
- package/dist/mcp/tools/mark.d.ts +0 -38
- package/dist/mcp/tools/mark.js +0 -82
- package/dist/mcp/tools/viewer.d.ts +0 -58
- package/dist/mcp/tools/viewer.js +0 -64
- package/dist/mcp-server.d.ts +0 -2
- package/dist/metrics.d.ts +0 -3
- package/dist/noise-filter.d.ts +0 -18
- package/dist/notion-sync.d.ts +0 -7
- package/dist/notion-sync.js +0 -97
- package/dist/observability/adapters/a3-adapter.d.ts +0 -63
- package/dist/observability/adapters/a3-adapter.js +0 -83
- package/dist/observability/adapters/p1-adapter.d.ts +0 -49
- package/dist/observability/adapters/p1-adapter.js +0 -63
- package/dist/observability/adapters/p5-adapter.d.ts +0 -36
- package/dist/observability/adapters/p5-adapter.js +0 -71
- package/dist/observability/cardinality.d.ts +0 -61
- package/dist/observability/cardinality.js +0 -300
- package/dist/observability/collectors/db-stats.collector.d.ts +0 -9
- package/dist/observability/collectors/db-stats.collector.js +0 -88
- package/dist/observability/collectors/eventbus.collector.d.ts +0 -6
- package/dist/observability/collectors/eventbus.collector.js +0 -50
- package/dist/observability/collectors/process.collector.d.ts +0 -7
- package/dist/observability/collectors/process.collector.js +0 -93
- package/dist/observability/collectors/provider-telemetry.collector.d.ts +0 -11
- package/dist/observability/collectors/provider-telemetry.collector.js +0 -112
- package/dist/observability/collectors/search-telemetry.collector.d.ts +0 -12
- package/dist/observability/collectors/search-telemetry.collector.js +0 -76
- package/dist/observability/exporter.d.ts +0 -30
- package/dist/observability/exporter.js +0 -182
- package/dist/observability/index.d.ts +0 -22
- package/dist/observability/index.js +0 -21
- package/dist/observability/metrics.d.ts +0 -53
- package/dist/observability/metrics.js +0 -213
- package/dist/observability/privacy-guard.d.ts +0 -32
- package/dist/observability/privacy-guard.js +0 -116
- package/dist/observability/record.d.ts +0 -83
- package/dist/observability/record.js +0 -217
- package/dist/observability/registry.d.ts +0 -60
- package/dist/observability/registry.js +0 -93
- package/dist/observability/types.d.ts +0 -115
- package/dist/observability/types.js +0 -235
- package/dist/observability.d.ts +0 -131
- package/dist/plugins/nox-hooks/index.d.ts +0 -61
- package/dist/plugins/nox-hooks/index.js +0 -99
- package/dist/primer.d.ts +0 -1
- package/dist/privacy/filter.d.ts +0 -27
- package/dist/privacy/patterns.d.ts +0 -29
- package/dist/privacy/tag-parser.d.ts +0 -24
- package/dist/project-context-gen.d.ts +0 -10
- package/dist/providers/embedding/gemini.d.ts +0 -73
- package/dist/providers/embedding/openai.d.ts +0 -76
- package/dist/providers/embedding/types.d.ts +0 -39
- package/dist/providers/embedding/voyage.d.ts +0 -36
- package/dist/providers/index.d.ts +0 -116
- package/dist/providers/llm/anthropic.d.ts +0 -29
- package/dist/providers/llm/chain.d.ts +0 -99
- package/dist/providers/llm/gemini.d.ts +0 -48
- package/dist/providers/llm/openai.d.ts +0 -64
- package/dist/providers/llm/types.d.ts +0 -47
- package/dist/providers/telemetry.d.ts +0 -87
- package/dist/providers/types.d.ts +0 -42
- package/dist/query-entity-count.d.ts +0 -54
- package/dist/reflect.d.ts +0 -26
- package/dist/reindex-errors.d.ts +0 -7
- package/dist/reindex.d.ts +0 -16
- package/dist/retention.d.ts +0 -40
- package/dist/salience.d.ts +0 -85
- package/dist/scripts/migrate-v17-ops-audit.d.ts +0 -17
- package/dist/search-dedup.d.ts +0 -44
- package/dist/search-expansion.d.ts +0 -20
- package/dist/search.d.ts +0 -62
- package/dist/seh-detector.d.ts +0 -31
- package/dist/self-improve.d.ts +0 -1
- package/dist/session-distill.d.ts +0 -29
- package/dist/session-update.d.ts +0 -1
- package/dist/shared-memory.d.ts +0 -24
- package/dist/stats.d.ts +0 -1
- package/dist/temporal-retrieval.d.ts +0 -132
- package/dist/tier-manager.d.ts +0 -69
- package/dist/vectorize.d.ts +0 -16
- package/dist/version.d.ts +0 -1
- package/dist/watch.d.ts +0 -8
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* src/lib/deps/deps-registry.ts — Central singleton resolver for wire-up adapters.
|
|
3
|
-
*
|
|
4
|
-
* Wave O (close-the-503-gap) needs five adapter modules (P1/A2/P5/L2-L3/P2)
|
|
5
|
-
* to share three runtime dependencies:
|
|
6
|
-
*
|
|
7
|
-
* 1. Better-sqlite3 Database handle (nox-mem.db)
|
|
8
|
-
* 2. Gemini provider (already used elsewhere in the codebase)
|
|
9
|
-
* 3. Event bus / broadcaster singleton for SSE
|
|
10
|
-
*
|
|
11
|
-
* Goals (regra de ouro #2 — "Single DB connection"):
|
|
12
|
-
*
|
|
13
|
-
* - One Database instance per process. All adapters acquire via `getDb()`.
|
|
14
|
-
* - Lazy initialization. Importing this module does not open the file.
|
|
15
|
-
* - Test-friendly. `resetDepsRegistryForTests()` flushes all singletons so
|
|
16
|
-
* `node --test` runs do not bleed state between cases.
|
|
17
|
-
* - Soft-fail. If better-sqlite3 isn't installed in the staged sandbox, the
|
|
18
|
-
* resolver returns `null` instead of throwing — the wire-up router maps
|
|
19
|
-
* null → 503 not_implemented (same pattern as before).
|
|
20
|
-
*
|
|
21
|
-
* Production wiring path:
|
|
22
|
-
* `/root/.openclaw/workspace/tools/nox-mem/dist/lib/deps/deps-registry.js`
|
|
23
|
-
* resolves NOX_DB_PATH from env (default
|
|
24
|
-
* `${OPENCLAW_WORKSPACE}/data/nox-mem.db`), opens better-sqlite3 with
|
|
25
|
-
* `readonly:false`, sets `journal_mode=WAL`. Same lifecycle the existing
|
|
26
|
-
* `nox-mem` CLI uses (see entity_file_format memory note).
|
|
27
|
-
*
|
|
28
|
-
* Why a registry instead of per-pillar singletons:
|
|
29
|
-
* - Avoids duplicate DB handles (one per pillar = N WAL writers, race).
|
|
30
|
-
* - Centralizes env reads so misconfig fails fast at first acquisition.
|
|
31
|
-
* - Lets tests swap the DB factory once (`__setDbFactoryForTests`) and have
|
|
32
|
-
* all five adapters honor the override.
|
|
33
|
-
*/
|
|
34
|
-
/**
|
|
35
|
-
* Structural Database type. Matches the better-sqlite3 surface used across
|
|
36
|
-
* staged dirs (`DBHandle` in L2, `Db` in L3, the orchestrator types in A2).
|
|
37
|
-
* Keeping the shim here avoids a hard `better-sqlite3` dep on the test path.
|
|
38
|
-
*/
|
|
39
|
-
export interface DbHandle {
|
|
40
|
-
prepare(sql: string): {
|
|
41
|
-
run(...params: unknown[]): {
|
|
42
|
-
changes: number;
|
|
43
|
-
lastInsertRowid: number | bigint;
|
|
44
|
-
};
|
|
45
|
-
get<T = unknown>(...params: unknown[]): T | undefined;
|
|
46
|
-
all<T = unknown>(...params: unknown[]): T[];
|
|
47
|
-
iterate?<T = unknown>(...params: unknown[]): Iterable<T>;
|
|
48
|
-
};
|
|
49
|
-
exec(sql: string): void;
|
|
50
|
-
transaction?<T extends (...args: any[]) => any>(fn: T): T;
|
|
51
|
-
pragma?(sql: string, opts?: {
|
|
52
|
-
simple?: boolean;
|
|
53
|
-
}): unknown;
|
|
54
|
-
close?(): void;
|
|
55
|
-
}
|
|
56
|
-
export interface GeminiProvider {
|
|
57
|
-
/** Identifier — e.g. "gemini-2.5-flash-lite". */
|
|
58
|
-
model: string;
|
|
59
|
-
/** Generate text completion. Returns {text, tokensIn, tokensOut, model}. */
|
|
60
|
-
complete(opts: {
|
|
61
|
-
system?: string;
|
|
62
|
-
user: string;
|
|
63
|
-
maxTokens?: number;
|
|
64
|
-
temperature?: number;
|
|
65
|
-
}): Promise<{
|
|
66
|
-
text: string;
|
|
67
|
-
tokensIn: number;
|
|
68
|
-
tokensOut: number;
|
|
69
|
-
model: string;
|
|
70
|
-
}>;
|
|
71
|
-
}
|
|
72
|
-
export interface EventBus {
|
|
73
|
-
/**
|
|
74
|
-
* Subscribe to event envelopes. Returns an unsubscribe function.
|
|
75
|
-
* The provider is opaque here; viewer-static + events-stream use it via
|
|
76
|
-
* the staged-P5 `Broadcaster` directly.
|
|
77
|
-
*/
|
|
78
|
-
publish(ev: unknown): unknown;
|
|
79
|
-
addClient(id: string, notify: () => void, lastEventId?: number): unknown;
|
|
80
|
-
removeClient(id: string): void;
|
|
81
|
-
ringSnapshot(): readonly unknown[];
|
|
82
|
-
clientCount(): number;
|
|
83
|
-
}
|
|
84
|
-
type DbFactory = () => DbHandle | null;
|
|
85
|
-
type ProviderFactory = () => GeminiProvider | null;
|
|
86
|
-
type EventBusFactory = () => EventBus | null;
|
|
87
|
-
/** Resolve nox-mem.db path from env, falling back to the canonical VPS path. */
|
|
88
|
-
export declare function resolveDbPath(): string;
|
|
89
|
-
/**
|
|
90
|
-
* Acquire (and cache) the process-wide DB handle.
|
|
91
|
-
* Returns null when better-sqlite3 is unavailable (staged-dir-only mode).
|
|
92
|
-
*/
|
|
93
|
-
export declare function getDb(): Promise<DbHandle | null>;
|
|
94
|
-
/**
|
|
95
|
-
* Acquire the cached Gemini provider, or null when GEMINI_API_KEY is unset.
|
|
96
|
-
*/
|
|
97
|
-
export declare function getProvider(): Promise<GeminiProvider | null>;
|
|
98
|
-
/**
|
|
99
|
-
* Acquire the cached EventBus (Broadcaster instance).
|
|
100
|
-
*/
|
|
101
|
-
export declare function getEventBus(): Promise<EventBus | null>;
|
|
102
|
-
/**
|
|
103
|
-
* Replace the DB factory. Pass `null` to clear (re-enable default factory).
|
|
104
|
-
* Internal — wire-up adapter tests use this to inject fakes.
|
|
105
|
-
*/
|
|
106
|
-
export declare function __setDbFactoryForTests(fn: DbFactory | null): void;
|
|
107
|
-
export declare function __setProviderFactoryForTests(fn: ProviderFactory | null): void;
|
|
108
|
-
export declare function __setEventBusFactoryForTests(fn: EventBusFactory | null): void;
|
|
109
|
-
/** Drop every singleton + clear factories. Use in `beforeEach` of test suites. */
|
|
110
|
-
export declare function resetDepsRegistryForTests(): void;
|
|
111
|
-
export {};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* G5 T2 — Middleware factories.
|
|
3
|
-
*
|
|
4
|
-
* Two flavours:
|
|
5
|
-
* - `expressErrorMiddleware(opts)` — drop-in `(err, req, res, next)` for
|
|
6
|
-
* Express / Fastify-adapter routes.
|
|
7
|
-
* - `sanitizerWrap(handler, opts)` — framework-agnostic. Wraps an async
|
|
8
|
-
* handler `(input) => Promise<{ status, headers, body }>` so any thrown
|
|
9
|
-
* error is converted into a sanitized response shape consistent with
|
|
10
|
-
* P1/A2/L2/L3/P2/P5.
|
|
11
|
-
*
|
|
12
|
-
* Both seed requestId via `X-Request-ID` header (case-insensitive) when
|
|
13
|
-
* present, else generate a fresh UUID.
|
|
14
|
-
*/
|
|
15
|
-
import { SanitizeOptions } from "./sanitize.js";
|
|
16
|
-
type ExpressLike = {
|
|
17
|
-
status: (n: number) => ExpressLike;
|
|
18
|
-
setHeader: (name: string, value: string) => void;
|
|
19
|
-
json: (body: unknown) => void;
|
|
20
|
-
};
|
|
21
|
-
type ExpressReqLike = {
|
|
22
|
-
headers?: Record<string, string | string[] | undefined>;
|
|
23
|
-
};
|
|
24
|
-
type NextFn = (err?: unknown) => void;
|
|
25
|
-
export interface ExpressMiddlewareOptions extends Omit<SanitizeOptions, "requestId"> {
|
|
26
|
-
/** Header name from which to pull existing request IDs. Default `x-request-id`. */
|
|
27
|
-
requestIdHeader?: string;
|
|
28
|
-
}
|
|
29
|
-
export declare function expressErrorMiddleware(opts?: ExpressMiddlewareOptions): (err: unknown, req: ExpressReqLike, res: ExpressLike, _next: NextFn) => void;
|
|
30
|
-
export interface HandlerInput {
|
|
31
|
-
headers?: Record<string, string | string[] | undefined>;
|
|
32
|
-
[k: string]: unknown;
|
|
33
|
-
}
|
|
34
|
-
export interface HandlerOutput {
|
|
35
|
-
status: number;
|
|
36
|
-
headers?: Record<string, string>;
|
|
37
|
-
body: unknown;
|
|
38
|
-
}
|
|
39
|
-
export declare function sanitizerWrap<I extends HandlerInput>(handler: (input: I) => Promise<HandlerOutput>, opts?: ExpressMiddlewareOptions): (input: I) => Promise<HandlerOutput>;
|
|
40
|
-
export declare function errorToResponse(err: unknown, opts?: Omit<SanitizeOptions, "requestId"> & {
|
|
41
|
-
requestId?: string;
|
|
42
|
-
}): HandlerOutput;
|
|
43
|
-
export {};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* G5 T1 — Central error sanitizer for HTTP responses.
|
|
3
|
-
*
|
|
4
|
-
* Threat-model ref: docs/security/THREAT-MODEL.md §6.2 T-A3-1 + Appendix Gap G5.
|
|
5
|
-
*
|
|
6
|
-
* Problem: Wave B HTTP handlers (P1 answer, A2 export, L3 mark, P5 viewer
|
|
7
|
-
* SSE, L2 conflict, P2 hooks) each implement their own catch-block. Several
|
|
8
|
-
* leak `error.stack` or internal paths (`/Users/lab/...`, `/root/...`) via
|
|
9
|
-
* `JSON.stringify(err)` or `err.message` containing fs paths.
|
|
10
|
-
*
|
|
11
|
-
* Solution: one function. All handlers pipe their errors through
|
|
12
|
-
* `sanitizeErrorForHttp()` to produce a fixed shape:
|
|
13
|
-
*
|
|
14
|
-
* { error: string, code: string, requestId: string, details?: unknown }
|
|
15
|
-
*
|
|
16
|
-
* Always emits requestId so users can quote one to support. Never emits stack
|
|
17
|
-
* (unless `exposeStack: true` AND NODE_ENV=development AND the dev flag is
|
|
18
|
-
* explicitly opted into via the caller).
|
|
19
|
-
*
|
|
20
|
-
* Status-code policy: maps a small allowlist of known error classes. Unknown
|
|
21
|
-
* → 500 + generic message. The status code is the only signal the client
|
|
22
|
-
* needs to differentiate "your input was bad" from "we crashed".
|
|
23
|
-
*
|
|
24
|
-
* Code values are CAPITAL_SNAKE per HTTP-convention; matches both the error's
|
|
25
|
-
* `.name` and a stable string per error class.
|
|
26
|
-
*/
|
|
27
|
-
export interface SanitizedErrorBody {
|
|
28
|
-
/** User-safe message (≤200 chars). NO paths, NO env, NO stack. */
|
|
29
|
-
error: string;
|
|
30
|
-
/** Stable code: error class name OR known status code label. */
|
|
31
|
-
code: string;
|
|
32
|
-
/** Request correlation ID — present even when caller didn't supply one. */
|
|
33
|
-
requestId: string;
|
|
34
|
-
/**
|
|
35
|
-
* Optional safe details. Caller MUST pre-vet — sanitizer will pass through
|
|
36
|
-
* unchanged but drops the field entirely if it contains a stack trace or
|
|
37
|
-
* a recognised internal-path pattern.
|
|
38
|
-
*/
|
|
39
|
-
details?: unknown;
|
|
40
|
-
/**
|
|
41
|
-
* Stack trace — present ONLY when `exposeStack: true` AND `NODE_ENV !== 'production'`.
|
|
42
|
-
* Always omitted in production builds. Tested.
|
|
43
|
-
*/
|
|
44
|
-
stack?: string;
|
|
45
|
-
}
|
|
46
|
-
export interface SanitizeOptions {
|
|
47
|
-
/**
|
|
48
|
-
* Request correlation ID. If empty/missing, generated via crypto.randomUUID().
|
|
49
|
-
* MUST be propagated by the caller (middleware seam).
|
|
50
|
-
*/
|
|
51
|
-
requestId?: string;
|
|
52
|
-
/**
|
|
53
|
-
* Expose stack in the response body. Default `false`.
|
|
54
|
-
* GATED additionally on `nodeEnv !== 'production'` — production never leaks.
|
|
55
|
-
*/
|
|
56
|
-
exposeStack?: boolean;
|
|
57
|
-
/**
|
|
58
|
-
* NODE_ENV override (defaults to process.env.NODE_ENV).
|
|
59
|
-
* Tests pass this explicitly to validate the prod-guard branch.
|
|
60
|
-
*/
|
|
61
|
-
nodeEnv?: string;
|
|
62
|
-
/** Optional log sink for the redacted line (caller-injectable). */
|
|
63
|
-
log?: (line: string) => void;
|
|
64
|
-
}
|
|
65
|
-
export interface SanitizedResult {
|
|
66
|
-
status: number;
|
|
67
|
-
body: SanitizedErrorBody;
|
|
68
|
-
}
|
|
69
|
-
declare const ERROR_STATUS_MAP: ReadonlyMap<string, {
|
|
70
|
-
status: number;
|
|
71
|
-
code: string;
|
|
72
|
-
safeMsg?: string;
|
|
73
|
-
}>;
|
|
74
|
-
export declare function sanitizeErrorForHttp(err: unknown, opts?: SanitizeOptions): SanitizedResult;
|
|
75
|
-
/** Convenience: generate a fresh requestId (callable from middleware). */
|
|
76
|
-
export declare function newRequestId(): string;
|
|
77
|
-
/** Re-export so callers can `import { sanitizeErrorForHttp, ... }`. */
|
|
78
|
-
export { ERROR_STATUS_MAP };
|
package/dist/lib/eval-batch.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { type EvalVariant } from "./eval.js";
|
|
2
|
-
export interface BatchMetricStats {
|
|
3
|
-
mean: number;
|
|
4
|
-
std: number;
|
|
5
|
-
min: number;
|
|
6
|
-
max: number;
|
|
7
|
-
values: number[];
|
|
8
|
-
}
|
|
9
|
-
export interface BatchSummary {
|
|
10
|
-
variant: EvalVariant;
|
|
11
|
-
runs: number;
|
|
12
|
-
query_count: number;
|
|
13
|
-
total_duration_ms: number;
|
|
14
|
-
individual_runs: Array<{
|
|
15
|
-
run_id: number;
|
|
16
|
-
nDCG: number;
|
|
17
|
-
MRR: number;
|
|
18
|
-
Recall: number;
|
|
19
|
-
Precision: number;
|
|
20
|
-
duration_ms: number;
|
|
21
|
-
}>;
|
|
22
|
-
stats: {
|
|
23
|
-
nDCG: BatchMetricStats;
|
|
24
|
-
MRR: BatchMetricStats;
|
|
25
|
-
Recall: BatchMetricStats;
|
|
26
|
-
Precision: BatchMetricStats;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
export declare function runBatch(opts: {
|
|
30
|
-
variant?: EvalVariant;
|
|
31
|
-
runs?: number;
|
|
32
|
-
notes?: string;
|
|
33
|
-
}): Promise<BatchSummary>;
|
|
34
|
-
export declare function formatBatchSummary(b: BatchSummary, mode?: 'json' | 'text'): string;
|
package/dist/lib/eval-batch.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
// R02 replication (2026-05-03): N-run batch para mean ± std variance estimate.
|
|
2
|
-
// Wraps runEval N vezes + agrega métricas por iteração.
|
|
3
|
-
import { runEval } from "./eval.js";
|
|
4
|
-
function statsFor(values) {
|
|
5
|
-
const n = values.length;
|
|
6
|
-
const mean = values.reduce((s, v) => s + v, 0) / n;
|
|
7
|
-
// CODE-FIX MEDIUM: Bessel's correction (n-1) for sample variance — paper R02 reports uncertainty
|
|
8
|
-
// from sample, not population. Falls back to population variance only if n=1 (degenerate).
|
|
9
|
-
const denom = n > 1 ? n - 1 : 1;
|
|
10
|
-
const variance = values.reduce((s, v) => s + (v - mean) ** 2, 0) / denom;
|
|
11
|
-
const std = Math.sqrt(variance);
|
|
12
|
-
// CODE-FIX MEDIUM: reduce-based min/max evita stack blowup com large N (Math.min/max(...values) crash ~100k)
|
|
13
|
-
const min = values.reduce((a, b) => (a < b ? a : b));
|
|
14
|
-
const max = values.reduce((a, b) => (a > b ? a : b));
|
|
15
|
-
return {
|
|
16
|
-
mean: Math.round(mean * 10000) / 10000,
|
|
17
|
-
std: Math.round(std * 10000) / 10000,
|
|
18
|
-
min: Math.round(min * 10000) / 10000,
|
|
19
|
-
max: Math.round(max * 10000) / 10000,
|
|
20
|
-
values: values.map((v) => Math.round(v * 10000) / 10000),
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
export async function runBatch(opts) {
|
|
24
|
-
const variant = opts.variant || "hybrid";
|
|
25
|
-
const runs = opts.runs ?? 3;
|
|
26
|
-
if (runs < 2)
|
|
27
|
-
throw new Error("--runs must be ≥ 2 for variance estimate");
|
|
28
|
-
const t0 = Date.now();
|
|
29
|
-
const summaries = [];
|
|
30
|
-
const ndcgs = [];
|
|
31
|
-
const mrrs = [];
|
|
32
|
-
const recalls = [];
|
|
33
|
-
const precs = [];
|
|
34
|
-
const errors = [];
|
|
35
|
-
for (let i = 0; i < runs; i++) {
|
|
36
|
-
// CODE-FIX MEDIUM: try/catch per iteration — não perde N-1 successful runs em 1 falha
|
|
37
|
-
try {
|
|
38
|
-
const s = await runEval({
|
|
39
|
-
variant,
|
|
40
|
-
notes: `${opts.notes || 'batch'} (iter ${i + 1}/${runs})`,
|
|
41
|
-
});
|
|
42
|
-
summaries.push(s);
|
|
43
|
-
ndcgs.push(s.aggregate.ndcg_at_10);
|
|
44
|
-
mrrs.push(s.aggregate.mrr);
|
|
45
|
-
recalls.push(s.aggregate.recall_at_10);
|
|
46
|
-
precs.push(s.aggregate.precision_at_5);
|
|
47
|
-
}
|
|
48
|
-
catch (e) {
|
|
49
|
-
errors.push({ iter: i + 1, error: e.message?.substring(0, 200) || String(e) });
|
|
50
|
-
console.error(`[eval-batch] iter ${i + 1}/${runs} failed: ${errors[errors.length - 1].error}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (summaries.length < 2) {
|
|
54
|
-
throw new Error(`eval-batch: ${summaries.length}/${runs} runs succeeded — need ≥2 for variance estimate. Errors: ${errors.map((e) => `iter${e.iter}=${e.error}`).join("; ")}`);
|
|
55
|
-
}
|
|
56
|
-
// CODE-FIX MEDIUM: assert query_count uniformity (golden set may mutate mid-batch via concurrent ingest)
|
|
57
|
-
const qCounts = summaries.map((s) => s.query_count);
|
|
58
|
-
const qCountsUniform = qCounts.every((c) => c === qCounts[0]);
|
|
59
|
-
if (!qCountsUniform) {
|
|
60
|
-
console.warn(`[eval-batch] WARN: query_count varies across runs ${JSON.stringify(qCounts)} — golden set mutated mid-batch?`);
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
variant,
|
|
64
|
-
runs,
|
|
65
|
-
query_count: qCountsUniform ? summaries[0].query_count : Math.max(...qCounts),
|
|
66
|
-
total_duration_ms: Date.now() - t0,
|
|
67
|
-
individual_runs: summaries.map((s) => ({
|
|
68
|
-
run_id: s.run_id,
|
|
69
|
-
nDCG: s.aggregate.ndcg_at_10,
|
|
70
|
-
MRR: s.aggregate.mrr,
|
|
71
|
-
Recall: s.aggregate.recall_at_10,
|
|
72
|
-
Precision: s.aggregate.precision_at_5,
|
|
73
|
-
duration_ms: s.total_duration_ms,
|
|
74
|
-
})),
|
|
75
|
-
stats: {
|
|
76
|
-
nDCG: statsFor(ndcgs),
|
|
77
|
-
MRR: statsFor(mrrs),
|
|
78
|
-
Recall: statsFor(recalls),
|
|
79
|
-
Precision: statsFor(precs),
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
export function formatBatchSummary(b, mode = 'text') {
|
|
84
|
-
if (mode === 'json')
|
|
85
|
-
return JSON.stringify(b, null, 2);
|
|
86
|
-
const lines = [];
|
|
87
|
-
lines.push(`## Eval Batch (variant=${b.variant}) — ${b.runs} runs over ${b.query_count} queries`);
|
|
88
|
-
lines.push(`Total duration: ${(b.total_duration_ms / 1000).toFixed(1)}s`);
|
|
89
|
-
lines.push(``);
|
|
90
|
-
lines.push(`| Run | nDCG@10 | MRR | Recall@10 | Prec@5 | Duration |`);
|
|
91
|
-
lines.push(`|-----|---------|-----|-----------|--------|----------|`);
|
|
92
|
-
for (const r of b.individual_runs) {
|
|
93
|
-
lines.push(`| #${r.run_id} | ${r.nDCG.toFixed(4)} | ${r.MRR.toFixed(4)} | ${r.Recall.toFixed(4)} | ${r.Precision.toFixed(4)} | ${(r.duration_ms / 1000).toFixed(1)}s |`);
|
|
94
|
-
}
|
|
95
|
-
lines.push(``);
|
|
96
|
-
lines.push(`### Aggregate (mean ± std)`);
|
|
97
|
-
lines.push(`| Metric | Mean | Std | Min | Max | Values |`);
|
|
98
|
-
lines.push(`|--------|------|-----|-----|-----|--------|`);
|
|
99
|
-
for (const [name, s] of Object.entries(b.stats)) {
|
|
100
|
-
lines.push(`| ${name.padEnd(8)} | ${s.mean.toFixed(4)} | ±${s.std.toFixed(4)} | ${s.min.toFixed(4)} | ${s.max.toFixed(4)} | ${s.values.map(v => v.toFixed(3)).join(", ")} |`);
|
|
101
|
-
}
|
|
102
|
-
return lines.join("\n");
|
|
103
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* nDCG@K — Normalized Discounted Cumulative Gain.
|
|
3
|
-
* Binary relevance (1 if chunk ∈ gold, else 0).
|
|
4
|
-
*
|
|
5
|
-
* DCG@K = Σ (i=1..K) [ rel(ri) / log2(i+1) ]
|
|
6
|
-
* IDCG@K = Σ (i=1..min(|G|, K)) [ 1 / log2(i+1) ]
|
|
7
|
-
* nDCG@K = DCG@K / IDCG@K (clamped to [0, 1])
|
|
8
|
-
*
|
|
9
|
-
* Returns 0 if gold standard is empty (no positives = undefined; convention 0).
|
|
10
|
-
*/
|
|
11
|
-
export declare function ndcgAtK(retrieved: number[], gold: Set<number>, k: number): number;
|
|
12
|
-
/**
|
|
13
|
-
* MRR — Mean Reciprocal Rank (per-query; aggregate is mean of these).
|
|
14
|
-
* Returns 1/rank of first gold hit, else 0.
|
|
15
|
-
*/
|
|
16
|
-
export declare function reciprocalRank(retrieved: number[], gold: Set<number>): number;
|
|
17
|
-
/**
|
|
18
|
-
* Recall@K — fraction of gold present in top-K retrieved.
|
|
19
|
-
*/
|
|
20
|
-
export declare function recallAtK(retrieved: number[], gold: Set<number>, k: number): number;
|
|
21
|
-
/**
|
|
22
|
-
* Precision@K — fraction of top-K that is gold.
|
|
23
|
-
* Denominator is always K (not min(K, retrieved.length)) — convention.
|
|
24
|
-
*/
|
|
25
|
-
export declare function precisionAtK(retrieved: number[], gold: Set<number>, k: number): number;
|
|
26
|
-
/**
|
|
27
|
-
* Aggregate mean across queries. Skips NaN (gold-empty queries with conventions).
|
|
28
|
-
*/
|
|
29
|
-
export declare function mean(values: number[]): number;
|
|
30
|
-
export interface PerQueryMetrics {
|
|
31
|
-
ndcg_at_10: number;
|
|
32
|
-
mrr: number;
|
|
33
|
-
recall_at_10: number;
|
|
34
|
-
precision_at_5: number;
|
|
35
|
-
}
|
|
36
|
-
export declare function computePerQuery(retrieved: number[], gold: Set<number>): PerQueryMetrics;
|
package/dist/lib/eval-metrics.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
// R01a — Eval metrics pure functions.
|
|
2
|
-
// Spec: specs/2026-04-27-R01a-eval-harness.md (formulações exatas)
|
|
3
|
-
//
|
|
4
|
-
// All functions are pure (no DB, no I/O). Take retrieved chunk IDs ranked +
|
|
5
|
-
// gold standard (expected chunk IDs as Set). Return [0, 1] scores.
|
|
6
|
-
/**
|
|
7
|
-
* nDCG@K — Normalized Discounted Cumulative Gain.
|
|
8
|
-
* Binary relevance (1 if chunk ∈ gold, else 0).
|
|
9
|
-
*
|
|
10
|
-
* DCG@K = Σ (i=1..K) [ rel(ri) / log2(i+1) ]
|
|
11
|
-
* IDCG@K = Σ (i=1..min(|G|, K)) [ 1 / log2(i+1) ]
|
|
12
|
-
* nDCG@K = DCG@K / IDCG@K (clamped to [0, 1])
|
|
13
|
-
*
|
|
14
|
-
* Returns 0 if gold standard is empty (no positives = undefined; convention 0).
|
|
15
|
-
*/
|
|
16
|
-
export function ndcgAtK(retrieved, gold, k) {
|
|
17
|
-
if (gold.size === 0)
|
|
18
|
-
return 0;
|
|
19
|
-
const cutoff = Math.min(k, retrieved.length);
|
|
20
|
-
let dcg = 0;
|
|
21
|
-
for (let i = 0; i < cutoff; i++) {
|
|
22
|
-
const rel = gold.has(retrieved[i]) ? 1 : 0;
|
|
23
|
-
if (rel === 0)
|
|
24
|
-
continue;
|
|
25
|
-
// i is 0-indexed; rank is (i+1); discount log2(rank+1) = log2(i+2)
|
|
26
|
-
dcg += rel / Math.log2(i + 2);
|
|
27
|
-
}
|
|
28
|
-
let idcg = 0;
|
|
29
|
-
const idealCount = Math.min(gold.size, k);
|
|
30
|
-
for (let i = 0; i < idealCount; i++) {
|
|
31
|
-
idcg += 1 / Math.log2(i + 2);
|
|
32
|
-
}
|
|
33
|
-
if (idcg === 0)
|
|
34
|
-
return 0;
|
|
35
|
-
return Math.max(0, Math.min(1, dcg / idcg));
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* MRR — Mean Reciprocal Rank (per-query; aggregate is mean of these).
|
|
39
|
-
* Returns 1/rank of first gold hit, else 0.
|
|
40
|
-
*/
|
|
41
|
-
export function reciprocalRank(retrieved, gold) {
|
|
42
|
-
if (gold.size === 0)
|
|
43
|
-
return 0;
|
|
44
|
-
for (let i = 0; i < retrieved.length; i++) {
|
|
45
|
-
if (gold.has(retrieved[i]))
|
|
46
|
-
return 1 / (i + 1);
|
|
47
|
-
}
|
|
48
|
-
return 0;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Recall@K — fraction of gold present in top-K retrieved.
|
|
52
|
-
*/
|
|
53
|
-
export function recallAtK(retrieved, gold, k) {
|
|
54
|
-
if (gold.size === 0)
|
|
55
|
-
return 0;
|
|
56
|
-
const cutoff = Math.min(k, retrieved.length);
|
|
57
|
-
let hits = 0;
|
|
58
|
-
for (let i = 0; i < cutoff; i++) {
|
|
59
|
-
if (gold.has(retrieved[i]))
|
|
60
|
-
hits++;
|
|
61
|
-
}
|
|
62
|
-
return hits / gold.size;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Precision@K — fraction of top-K that is gold.
|
|
66
|
-
* Denominator is always K (not min(K, retrieved.length)) — convention.
|
|
67
|
-
*/
|
|
68
|
-
export function precisionAtK(retrieved, gold, k) {
|
|
69
|
-
if (k === 0)
|
|
70
|
-
return 0;
|
|
71
|
-
const cutoff = Math.min(k, retrieved.length);
|
|
72
|
-
let hits = 0;
|
|
73
|
-
for (let i = 0; i < cutoff; i++) {
|
|
74
|
-
if (gold.has(retrieved[i]))
|
|
75
|
-
hits++;
|
|
76
|
-
}
|
|
77
|
-
return hits / k;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Aggregate mean across queries. Skips NaN (gold-empty queries with conventions).
|
|
81
|
-
*/
|
|
82
|
-
export function mean(values) {
|
|
83
|
-
const valid = values.filter((v) => !Number.isNaN(v));
|
|
84
|
-
if (valid.length === 0)
|
|
85
|
-
return 0;
|
|
86
|
-
return valid.reduce((a, b) => a + b, 0) / valid.length;
|
|
87
|
-
}
|
|
88
|
-
export function computePerQuery(retrieved, gold) {
|
|
89
|
-
return {
|
|
90
|
-
ndcg_at_10: ndcgAtK(retrieved, gold, 10),
|
|
91
|
-
mrr: reciprocalRank(retrieved, gold),
|
|
92
|
-
recall_at_10: recallAtK(retrieved, gold, 10),
|
|
93
|
-
precision_at_5: precisionAtK(retrieved, gold, 5),
|
|
94
|
-
};
|
|
95
|
-
}
|
package/dist/lib/eval.d.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import Database from "better-sqlite3";
|
|
2
|
-
export type EvalVariant = "hybrid" | "fts" | "vector" | "rrf-only" | "custom" | "rerank";
|
|
3
|
-
export interface GoldenQuery {
|
|
4
|
-
id?: number;
|
|
5
|
-
query: string;
|
|
6
|
-
expected_chunk_ids: number[];
|
|
7
|
-
difficulty?: "easy" | "medium" | "hard";
|
|
8
|
-
category?: string;
|
|
9
|
-
added_by?: string;
|
|
10
|
-
notes?: string;
|
|
11
|
-
}
|
|
12
|
-
export interface EvalRunRow {
|
|
13
|
-
id: number;
|
|
14
|
-
variant: EvalVariant;
|
|
15
|
-
ran_at: string;
|
|
16
|
-
git_sha: string | null;
|
|
17
|
-
schema_version: number | null;
|
|
18
|
-
query_count: number;
|
|
19
|
-
total_duration_ms: number | null;
|
|
20
|
-
notes: string | null;
|
|
21
|
-
}
|
|
22
|
-
export interface EvalAggregate {
|
|
23
|
-
ndcg_at_10: number;
|
|
24
|
-
mrr: number;
|
|
25
|
-
recall_at_10: number;
|
|
26
|
-
precision_at_5: number;
|
|
27
|
-
}
|
|
28
|
-
export declare function getGitSha(): string | null;
|
|
29
|
-
export declare function getSchemaVersion(db: Database.Database): number;
|
|
30
|
-
/**
|
|
31
|
-
* Import golden queries from JSONL file. INSERT OR IGNORE on UNIQUE(query).
|
|
32
|
-
* Returns { imported, skipped, total_in_db }.
|
|
33
|
-
*/
|
|
34
|
-
export declare function importGolden(file: string, addedBy?: string): {
|
|
35
|
-
imported: number;
|
|
36
|
-
skipped: number;
|
|
37
|
-
total: number;
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* List all golden queries in DB.
|
|
41
|
-
*/
|
|
42
|
-
export declare function listGolden(): GoldenQuery[];
|
|
43
|
-
export interface RunOptions {
|
|
44
|
-
variant?: EvalVariant;
|
|
45
|
-
notes?: string;
|
|
46
|
-
gitSha?: string;
|
|
47
|
-
reportsDir?: string;
|
|
48
|
-
}
|
|
49
|
-
export interface RunSummary {
|
|
50
|
-
run_id: number;
|
|
51
|
-
variant: EvalVariant;
|
|
52
|
-
ran_at: string;
|
|
53
|
-
git_sha: string | null;
|
|
54
|
-
query_count: number;
|
|
55
|
-
total_duration_ms: number;
|
|
56
|
-
aggregate: EvalAggregate;
|
|
57
|
-
byDifficulty: Record<string, EvalAggregate & {
|
|
58
|
-
n: number;
|
|
59
|
-
}>;
|
|
60
|
-
byCategory: Record<string, EvalAggregate & {
|
|
61
|
-
n: number;
|
|
62
|
-
}>;
|
|
63
|
-
jsonlPath: string;
|
|
64
|
-
prevRunId: number | null;
|
|
65
|
-
delta: EvalAggregate | null;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Run eval over all golden queries with given variant.
|
|
69
|
-
* Persists eval_runs + eval_results, exports JSONL, returns summary.
|
|
70
|
-
*/
|
|
71
|
-
export declare function runEval(opts?: RunOptions): Promise<RunSummary>;
|
|
72
|
-
/**
|
|
73
|
-
* Compute aggregate for a given run from eval_results table.
|
|
74
|
-
*/
|
|
75
|
-
export declare function aggregateForRun(runId: number): EvalAggregate | null;
|
|
76
|
-
/**
|
|
77
|
-
* List recent runs, optionally filtered by variant.
|
|
78
|
-
*/
|
|
79
|
-
export declare function listRuns(variant?: EvalVariant, limit?: number): EvalRunRow[];
|
|
80
|
-
export interface CompareRow {
|
|
81
|
-
query_id: number;
|
|
82
|
-
query: string;
|
|
83
|
-
a_ndcg: number;
|
|
84
|
-
b_ndcg: number;
|
|
85
|
-
delta: number;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Compare two runs query-by-query. Returns sorted by delta (regressions first).
|
|
89
|
-
*/
|
|
90
|
-
export declare function compareRuns(runIdA: number, runIdB: number): {
|
|
91
|
-
regressions: CompareRow[];
|
|
92
|
-
improvements: CompareRow[];
|
|
93
|
-
aggregate_a: EvalAggregate | null;
|
|
94
|
-
aggregate_b: EvalAggregate | null;
|
|
95
|
-
};
|
|
96
|
-
/**
|
|
97
|
-
* Get latest run per variant for /api/health.evalMetrics surface.
|
|
98
|
-
*/
|
|
99
|
-
export declare function getEvalMetricsSnapshot(): {
|
|
100
|
-
lastRun: (EvalRunRow & EvalAggregate) | null;
|
|
101
|
-
byVariant: Record<string, {
|
|
102
|
-
ndcg_at_10: number;
|
|
103
|
-
ran_at: string;
|
|
104
|
-
run_id: number;
|
|
105
|
-
}>;
|
|
106
|
-
};
|