@unerr-ai/unerr 0.2.0 → 0.2.2
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 +6 -0
- package/dist/cli.js +37236 -35793
- package/package.json +6 -1
- package/dist/behaviors/agent-llm-bridge.js +0 -166
- package/dist/behaviors/architecture-guard.js +0 -256
- package/dist/behaviors/auto-doc.js +0 -247
- package/dist/behaviors/cascade-guard.js +0 -289
- package/dist/behaviors/change-narrative.js +0 -270
- package/dist/behaviors/convention-drift.js +0 -290
- package/dist/behaviors/framework.js +0 -235
- package/dist/behaviors/guard-formatter.js +0 -44
- package/dist/behaviors/incomplete-work.js +0 -270
- package/dist/behaviors/loop-breaker.js +0 -300
- package/dist/behaviors/session-continuity.js +0 -208
- package/dist/commands/branches.js +0 -97
- package/dist/commands/check-commit.js +0 -225
- package/dist/commands/compress-output.js +0 -64
- package/dist/commands/config-verify.js +0 -243
- package/dist/commands/daemon.js +0 -905
- package/dist/commands/dashboard.js +0 -52
- package/dist/commands/debug.js +0 -200
- package/dist/commands/enrich.js +0 -184
- package/dist/commands/exec.js +0 -233
- package/dist/commands/gain.js +0 -156
- package/dist/commands/hook.js +0 -88
- package/dist/commands/index.js +0 -88
- package/dist/commands/init.js +0 -74
- package/dist/commands/install.js +0 -505
- package/dist/commands/learn.js +0 -116
- package/dist/commands/manifest.js +0 -193
- package/dist/commands/rewind.js +0 -103
- package/dist/commands/serve.js +0 -19
- package/dist/commands/setup-wizard.js +0 -414
- package/dist/commands/skills.js +0 -64
- package/dist/commands/stats.js +0 -20
- package/dist/commands/status.js +0 -654
- package/dist/commands/timeline.js +0 -139
- package/dist/commands/uninstall.js +0 -230
- package/dist/components/App.js +0 -109
- package/dist/components/Banner.js +0 -12
- package/dist/components/ConfirmPrompt.js +0 -25
- package/dist/components/DriftSummary.js +0 -23
- package/dist/components/GradeBadge.js +0 -15
- package/dist/components/HealthCard.js +0 -18
- package/dist/components/InkSpinner.js +0 -22
- package/dist/components/InputBox.js +0 -17
- package/dist/components/KeyValue.js +0 -13
- package/dist/components/MessageList.js +0 -14
- package/dist/components/ProgressBar.js +0 -26
- package/dist/components/Section.js +0 -16
- package/dist/components/SessionSummaryCard.js +0 -73
- package/dist/components/StartupDisplay.js +0 -24
- package/dist/components/StatusDashboard.js +0 -57
- package/dist/components/StatusLine.js +0 -8
- package/dist/components/StepLine.js +0 -22
- package/dist/components/Theme.js +0 -20
- package/dist/components/ToolProgress.js +0 -8
- package/dist/components/ViolationList.js +0 -21
- package/dist/components/render.js +0 -13
- package/dist/config/agent-registry.js +0 -237
- package/dist/config/claude-settings-hooks.js +0 -304
- package/dist/config/hook-installer.js +0 -65
- package/dist/config/instruction-writer.js +0 -388
- package/dist/config/mcp-config-writer.js +0 -266
- package/dist/config/settings.js +0 -174
- package/dist/config/tool-detector.js +0 -42
- package/dist/config/value-surfacing.js +0 -119
- package/dist/core/context-assembly.js +0 -108
- package/dist/core/conversation.js +0 -33
- package/dist/core/local-chat-provider.js +0 -475
- package/dist/core/provider-factory.js +0 -55
- package/dist/core/providers.js +0 -90
- package/dist/core/query-engine.js +0 -174
- package/dist/daemon/api.js +0 -312
- package/dist/daemon/autostart.js +0 -119
- package/dist/daemon/bootstrap.js +0 -39
- package/dist/daemon/client.js +0 -164
- package/dist/daemon/detect-ci.js +0 -81
- package/dist/daemon/platform-linux.js +0 -146
- package/dist/daemon/platform-macos.js +0 -134
- package/dist/daemon/platform-windows.js +0 -116
- package/dist/daemon/process-manager.js +0 -299
- package/dist/daemon/protocol.js +0 -23
- package/dist/daemon/registry.js +0 -270
- package/dist/daemon/settings-schema.js +0 -72
- package/dist/daemon/system-health.js +0 -134
- package/dist/daemon/version-checker.js +0 -262
- package/dist/daemon/warm-start.js +0 -223
- package/dist/entrypoints/cli.js +0 -1043
- package/dist/entrypoints/daemon.js +0 -380
- package/dist/entrypoints/repl.js +0 -147
- package/dist/hooks/adapters/claude-code.js +0 -90
- package/dist/hooks/adapters/cline.js +0 -100
- package/dist/hooks/adapters/cursor.js +0 -98
- package/dist/hooks/hook-dedup.js +0 -79
- package/dist/hooks/hook-runner.js +0 -113
- package/dist/hooks/navigation-hooks.js +0 -175
- package/dist/hooks/prompt-hooks.js +0 -63
- package/dist/hooks/shell-hooks.js +0 -47
- package/dist/ignore.js +0 -111
- package/dist/intelligence/approach-suggester.js +0 -61
- package/dist/intelligence/ast-extractor.js +0 -2615
- package/dist/intelligence/ast-worker.js +0 -34
- package/dist/intelligence/background-indexer.js +0 -121
- package/dist/intelligence/blast-radius.js +0 -200
- package/dist/intelligence/community-detection.js +0 -691
- package/dist/intelligence/community-detector.js +0 -184
- package/dist/intelligence/computation-scheduler.js +0 -75
- package/dist/intelligence/confidence-propagation.js +0 -47
- package/dist/intelligence/convention-detector.js +0 -242
- package/dist/intelligence/convention-learner.js +0 -205
- package/dist/intelligence/convention-matcher.js +0 -205
- package/dist/intelligence/cozo-schema.js +0 -376
- package/dist/intelligence/decision-point-detector.js +0 -90
- package/dist/intelligence/deep-dive-tools.js +0 -586
- package/dist/intelligence/durability-scorer.js +0 -84
- package/dist/intelligence/exploration-cost.js +0 -204
- package/dist/intelligence/exploration-pattern-tracker.js +0 -61
- package/dist/intelligence/fact-generator.js +0 -322
- package/dist/intelligence/facts-schema.js +0 -90
- package/dist/intelligence/file-intelligence.js +0 -59
- package/dist/intelligence/graph-holder.js +0 -220
- package/dist/intelligence/graph-temporal-joiner.js +0 -238
- package/dist/intelligence/health-grade.js +0 -423
- package/dist/intelligence/health-grader.js +0 -200
- package/dist/intelligence/health-map-data.js +0 -259
- package/dist/intelligence/import-symbols.js +0 -136
- package/dist/intelligence/incremental-indexer.js +0 -658
- package/dist/intelligence/indexer/centrality.js +0 -62
- package/dist/intelligence/indexer/cfg-context.js +0 -95
- package/dist/intelligence/indexer/confidence.js +0 -34
- package/dist/intelligence/indexer/cross-file-resolver.js +0 -104
- package/dist/intelligence/indexer/edge-repair.js +0 -89
- package/dist/intelligence/indexer/entity-key.js +0 -17
- package/dist/intelligence/indexer/export-map.js +0 -132
- package/dist/intelligence/indexer/git-cochange.js +0 -128
- package/dist/intelligence/indexer/graph-patch.js +0 -147
- package/dist/intelligence/indexer/incremental.js +0 -78
- package/dist/intelligence/indexer/ingest.js +0 -160
- package/dist/intelligence/indexer/language-detect.js +0 -226
- package/dist/intelligence/indexer/metadata.js +0 -63
- package/dist/intelligence/indexer/mutation-tracker.js +0 -79
- package/dist/intelligence/indexer/orchestrator.js +0 -155
- package/dist/intelligence/indexer/plugin-interface.js +0 -31
- package/dist/intelligence/indexer/plugins/csharp.js +0 -440
- package/dist/intelligence/indexer/plugins/go.js +0 -335
- package/dist/intelligence/indexer/plugins/java.js +0 -370
- package/dist/intelligence/indexer/plugins/python.js +0 -358
- package/dist/intelligence/indexer/plugins/regex-fallback.js +0 -82
- package/dist/intelligence/indexer/plugins/ruby.js +0 -290
- package/dist/intelligence/indexer/plugins/rust.js +0 -484
- package/dist/intelligence/indexer/plugins/tier2-generic.js +0 -310
- package/dist/intelligence/indexer/plugins/typescript.js +0 -456
- package/dist/intelligence/indexer/resource-monitor.js +0 -93
- package/dist/intelligence/indexer/scip/decoder.js +0 -253
- package/dist/intelligence/indexer/scip/detector.js +0 -232
- package/dist/intelligence/indexer/scip/downloader.js +0 -427
- package/dist/intelligence/indexer/scip/fallback.js +0 -34
- package/dist/intelligence/indexer/scip/merger.js +0 -109
- package/dist/intelligence/indexer/scip/orchestrator.js +0 -433
- package/dist/intelligence/indexer/scip/runner.js +0 -98
- package/dist/intelligence/indexer/snapshot.js +0 -66
- package/dist/intelligence/indexer/test-detector.js +0 -196
- package/dist/intelligence/indexer/watch-integration.js +0 -61
- package/dist/intelligence/indexer/worker.js +0 -85
- package/dist/intelligence/local-convention-detector.js +0 -437
- package/dist/intelligence/local-embeddings.js +0 -190
- package/dist/intelligence/local-graph.js +0 -1946
- package/dist/intelligence/local-indexer.js +0 -1575
- package/dist/intelligence/local-llm.js +0 -163
- package/dist/intelligence/local-rule-generator.js +0 -154
- package/dist/intelligence/local-snapshot.js +0 -213
- package/dist/intelligence/negative-knowledge.js +0 -103
- package/dist/intelligence/persistent-db.js +0 -85
- package/dist/intelligence/query-router.js +0 -2556
- package/dist/intelligence/risk-classifier.js +0 -116
- package/dist/intelligence/rule-evaluator.js +0 -380
- package/dist/intelligence/rule-generator.js +0 -49
- package/dist/intelligence/search-index.js +0 -173
- package/dist/intelligence/semantic/docstring-extractor.js +0 -67
- package/dist/intelligence/semantic/embedding-store.js +0 -52
- package/dist/intelligence/semantic/enrichment-orchestrator.js +0 -48
- package/dist/intelligence/semantic/git-message-miner.js +0 -114
- package/dist/intelligence/semantic/identifier-tokenizer.js +0 -51
- package/dist/intelligence/semantic/node2vec-embeddings.js +0 -71
- package/dist/intelligence/semantic/node2vec-walks.js +0 -103
- package/dist/intelligence/semantic/path-domain-inference.js +0 -112
- package/dist/intelligence/semantic/similarity-engine.js +0 -60
- package/dist/intelligence/semantic/tfidf-vectors.js +0 -88
- package/dist/intelligence/session-brief-builder.js +0 -159
- package/dist/intelligence/session-context.js +0 -221
- package/dist/intelligence/session-health-monitor.js +0 -211
- package/dist/intelligence/session-narrative.js +0 -197
- package/dist/intelligence/session-pattern-analyzer.js +0 -218
- package/dist/intelligence/signal-scorer.js +0 -390
- package/dist/intelligence/signal-show-store.js +0 -182
- package/dist/intelligence/smart-truncate.js +0 -158
- package/dist/intelligence/subgraph-cache.js +0 -88
- package/dist/intelligence/temporal-facts.js +0 -494
- package/dist/intelligence/token-estimator.js +0 -100
- package/dist/intelligence/tool-injector.js +0 -87
- package/dist/intelligence/tree-sitter-loader.js +0 -71
- package/dist/intelligence/worker-pool.js +0 -116
- package/dist/proxy/arg-validator.js +0 -79
- package/dist/proxy/auto-bootstrap.js +0 -167
- package/dist/proxy/bridge.js +0 -147
- package/dist/proxy/budget-enforcer.js +0 -70
- package/dist/proxy/compression-quality-monitor.js +0 -160
- package/dist/proxy/compression-stats.js +0 -51
- package/dist/proxy/context-rot-detector.js +0 -137
- package/dist/proxy/drift-detector.js +0 -139
- package/dist/proxy/efficiency-tracker.js +0 -79
- package/dist/proxy/fact-ranking.js +0 -154
- package/dist/proxy/format-encoder.js +0 -266
- package/dist/proxy/http-transport.js +0 -90
- package/dist/proxy/lifecycle-actor.js +0 -55
- package/dist/proxy/lifecycle-machine.js +0 -187
- package/dist/proxy/log-tailer.js +0 -265
- package/dist/proxy/model-pricing.js +0 -98
- package/dist/proxy/network-firewall.js +0 -141
- package/dist/proxy/nudge-state.js +0 -93
- package/dist/proxy/output-compressor.js +0 -185
- package/dist/proxy/pid-lock.js +0 -291
- package/dist/proxy/proxy-context.js +0 -11
- package/dist/proxy/proxy.js +0 -2633
- package/dist/proxy/response-enrichment.js +0 -32
- package/dist/proxy/response-envelope.js +0 -313
- package/dist/proxy/session-dedup.js +0 -82
- package/dist/proxy/session-legend.js +0 -30
- package/dist/proxy/session-persistence.js +0 -210
- package/dist/proxy/session-resume.js +0 -94
- package/dist/proxy/session-stats.js +0 -513
- package/dist/proxy/shell-classifier.js +0 -1346
- package/dist/proxy/shell-compression-log.js +0 -93
- package/dist/proxy/shell-compressor.js +0 -390
- package/dist/proxy/shell-graph-boost.js +0 -202
- package/dist/proxy/shell-monitor-map.js +0 -18
- package/dist/proxy/shell-stats.js +0 -54
- package/dist/proxy/shell-strategies/cloud.js +0 -215
- package/dist/proxy/shell-strategies/diff.js +0 -159
- package/dist/proxy/shell-strategies/error-diagnostic.js +0 -796
- package/dist/proxy/shell-strategies/filter-dsl.js +0 -358
- package/dist/proxy/shell-strategies/git-status.js +0 -177
- package/dist/proxy/shell-strategies/key-value.js +0 -193
- package/dist/proxy/shell-strategies/log-text.js +0 -154
- package/dist/proxy/shell-strategies/omni.js +0 -188
- package/dist/proxy/shell-strategies/progress.js +0 -55
- package/dist/proxy/shell-strategies/redact.js +0 -76
- package/dist/proxy/shell-strategies/structured.js +0 -241
- package/dist/proxy/shell-strategies/tabular.js +0 -243
- package/dist/proxy/shell-strategies/test-results-types.js +0 -13
- package/dist/proxy/shell-strategies/test-results.js +0 -784
- package/dist/proxy/shell-strategies/tree-paths.js +0 -144
- package/dist/proxy/shell-strategies/yaml.js +0 -182
- package/dist/proxy/shell-tee.js +0 -111
- package/dist/proxy/signal-dedup.js +0 -171
- package/dist/proxy/startup-renderer.js +0 -158
- package/dist/proxy/task-token-display.js +0 -38
- package/dist/proxy/token-counter.js +0 -61
- package/dist/proxy/tool-clusters.js +0 -273
- package/dist/proxy/tool-definitions.js +0 -525
- package/dist/proxy/transport-mux.js +0 -229
- package/dist/proxy/wire-cap.js +0 -268
- package/dist/rules/developer.mozilla.org.json +0 -9
- package/dist/rules/github.com.json +0 -21
- package/dist/schemas/api/skills.js +0 -19
- package/dist/schemas/common/errors.js +0 -7
- package/dist/schemas/common/headers.js +0 -5
- package/dist/schemas/entities/edge.js +0 -25
- package/dist/schemas/entities/entity.js +0 -22
- package/dist/schemas/entities/rule.js +0 -18
- package/dist/schemas/index.js +0 -14
- package/dist/server/event-bus.js +0 -59
- package/dist/server/http.js +0 -156
- package/dist/server/middleware.js +0 -70
- package/dist/server/routes/drift.js +0 -97
- package/dist/server/routes/intelligence.js +0 -1217
- package/dist/server/routes/reasoning-quality.js +0 -444
- package/dist/server/routes/session.js +0 -86
- package/dist/server/routes/stream.js +0 -120
- package/dist/server/routes/system.js +0 -73
- package/dist/server/routes/temporal.js +0 -170
- package/dist/server/routes/timeline.js +0 -232
- package/dist/server/routes/token-flow.js +0 -403
- package/dist/skills/effectiveness-tracker.js +0 -93
- package/dist/skills/local-pack.js +0 -380
- package/dist/skills/resolver.js +0 -495
- package/dist/state-detector.js +0 -83
- package/dist/timeline/intent-detector.js +0 -263
- package/dist/timeline/loop-miner.js +0 -140
- package/dist/timeline/open-threads.js +0 -49
- package/dist/timeline/signal-reinforcer.js +0 -62
- package/dist/timeline/timeline-bootstrap.js +0 -151
- package/dist/timeline/timeline-store.js +0 -618
- package/dist/tools/coding/bash.js +0 -49
- package/dist/tools/coding/file-edit.js +0 -72
- package/dist/tools/coding/file-outline.js +0 -227
- package/dist/tools/coding/file-read-protocol.js +0 -425
- package/dist/tools/coding/file-read.js +0 -35
- package/dist/tools/coding/file-write.js +0 -43
- package/dist/tools/coding/glob-tool.js +0 -109
- package/dist/tools/coding/grep.js +0 -162
- package/dist/tools/coding/index.js +0 -27
- package/dist/tools/intelligence/index.js +0 -269
- package/dist/tools/intelligence/record-fact.js +0 -48
- package/dist/tools/intelligence/timeline-markers.js +0 -130
- package/dist/tools/registry.js +0 -47
- package/dist/tools/types.js +0 -8
- package/dist/tracking/auto-snapshot-triggers.js +0 -246
- package/dist/tracking/branch-context.js +0 -115
- package/dist/tracking/branch-snapshot.js +0 -217
- package/dist/tracking/causal-bridge.js +0 -317
- package/dist/tracking/circuit-breaker.js +0 -147
- package/dist/tracking/commit-watcher.js +0 -114
- package/dist/tracking/context-ledger.js +0 -119
- package/dist/tracking/correction-detector.js +0 -324
- package/dist/tracking/drift-tracker.js +0 -874
- package/dist/tracking/durability-tracker.js +0 -94
- package/dist/tracking/entity-rewind.js +0 -200
- package/dist/tracking/file-hash-state.js +0 -114
- package/dist/tracking/git-attribution.js +0 -132
- package/dist/tracking/git-trailers.js +0 -171
- package/dist/tracking/intelligence-counter.js +0 -46
- package/dist/tracking/intent-correlator.js +0 -202
- package/dist/tracking/intent-encoder.js +0 -52
- package/dist/tracking/intent-token-tracker.js +0 -159
- package/dist/tracking/ledger-archiver.js +0 -94
- package/dist/tracking/ledger-chains.js +0 -245
- package/dist/tracking/metrics-store.js +0 -361
- package/dist/tracking/native-watcher.js +0 -131
- package/dist/tracking/offline-rewind.js +0 -295
- package/dist/tracking/pending-violations.js +0 -74
- package/dist/tracking/persistence-effectiveness.js +0 -167
- package/dist/tracking/prompt-durability.js +0 -202
- package/dist/tracking/quality-signals.js +0 -213
- package/dist/tracking/redactor.js +0 -73
- package/dist/tracking/rewind-engine.js +0 -161
- package/dist/tracking/session-history.js +0 -128
- package/dist/tracking/session-receipt.js +0 -88
- package/dist/tracking/session-summary-writer.js +0 -157
- package/dist/tracking/shadow-ledger.js +0 -321
- package/dist/tracking/stash-manager.js +0 -258
- package/dist/tracking/timeline-fork.js +0 -213
- package/dist/tracking/timeline.js +0 -69
- package/dist/tracking/token-flow.js +0 -276
- package/dist/tracking/turn-segmenter.js +0 -122
- package/dist/tracking/weekly-accumulator.js +0 -179
- package/dist/tracking/working-snapshots.js +0 -188
- package/dist/tracking/workspace-manifest.js +0 -176
- package/dist/transport/http.js +0 -102
- package/dist/utils/counterfactual.js +0 -65
- package/dist/utils/deep-link.js +0 -34
- package/dist/utils/detect.js +0 -193
- package/dist/utils/exec.js +0 -73
- package/dist/utils/file-logger.js +0 -87
- package/dist/utils/format-error.js +0 -29
- package/dist/utils/git.js +0 -181
- package/dist/utils/log.js +0 -57
- package/dist/utils/logger.js +0 -35
- package/dist/utils/mcp-content-json.js +0 -8
- package/dist/utils/session-logger.js +0 -154
- package/dist/utils/startup-log.js +0 -512
- package/dist/utils/ui.js +0 -56
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Strategy T5 — tree/find/ls -R path compression.
|
|
3
|
-
*
|
|
4
|
-
* v2 (R7): groups paths by directory and emits compact directory rollups
|
|
5
|
-
* with per-extension counts ("src/components/ (47 files: 12 .tsx, 35 .ts)")
|
|
6
|
-
* instead of head/tail truncation. Targets 70%+ compression vs the previous
|
|
7
|
-
* 42%.
|
|
8
|
-
*
|
|
9
|
-
* The output is still readable by a coding agent — it can ask for a deeper
|
|
10
|
-
* listing on any directory that turns out to matter.
|
|
11
|
-
*/
|
|
12
|
-
const COLLAPSE_DIRS = new Set([
|
|
13
|
-
"node_modules",
|
|
14
|
-
".git",
|
|
15
|
-
"dist",
|
|
16
|
-
"build",
|
|
17
|
-
"out",
|
|
18
|
-
"__pycache__",
|
|
19
|
-
".next",
|
|
20
|
-
".nuxt",
|
|
21
|
-
"target",
|
|
22
|
-
"vendor",
|
|
23
|
-
".tox",
|
|
24
|
-
".mypy_cache",
|
|
25
|
-
".pytest_cache",
|
|
26
|
-
".venv",
|
|
27
|
-
"venv",
|
|
28
|
-
"coverage",
|
|
29
|
-
".coverage",
|
|
30
|
-
".turbo",
|
|
31
|
-
".cache",
|
|
32
|
-
]);
|
|
33
|
-
const SMALL_THRESHOLD = 40; // <40 lines: pass through unchanged
|
|
34
|
-
/** Extract a clean path from a tree/find line (drop ascii-tree decorations). */
|
|
35
|
-
function cleanPath(line) {
|
|
36
|
-
return line.replace(/^[`│├└┌├─\s]+/, "").trim();
|
|
37
|
-
}
|
|
38
|
-
/** Directory portion of a path; "" for top-level files. */
|
|
39
|
-
function dirOf(p) {
|
|
40
|
-
const i = p.lastIndexOf("/");
|
|
41
|
-
return i >= 0 ? p.slice(0, i) : "";
|
|
42
|
-
}
|
|
43
|
-
/** Extension key — ".tsx" / ".rs" / "(no-ext)" — for bucket counts. */
|
|
44
|
-
function extOf(p) {
|
|
45
|
-
const base = p.slice(p.lastIndexOf("/") + 1);
|
|
46
|
-
const dot = base.lastIndexOf(".");
|
|
47
|
-
if (dot <= 0 || dot === base.length - 1)
|
|
48
|
-
return "(no-ext)";
|
|
49
|
-
return base.slice(dot).toLowerCase();
|
|
50
|
-
}
|
|
51
|
-
/** Top-K extensions in a bucket, formatted "12 .tsx, 35 .ts". */
|
|
52
|
-
function topExt(byExt, k = 4) {
|
|
53
|
-
const sorted = [...byExt.entries()].sort((a, b) => b[1] - a[1]);
|
|
54
|
-
const head = sorted.slice(0, k).map(([ext, n]) => `${n} ${ext}`);
|
|
55
|
-
if (sorted.length > k) {
|
|
56
|
-
const rest = sorted.slice(k).reduce((s, [, n]) => s + n, 0);
|
|
57
|
-
head.push(`${rest} other`);
|
|
58
|
-
}
|
|
59
|
-
return head.join(", ");
|
|
60
|
-
}
|
|
61
|
-
export function compressTreePaths(raw, _maxDepth, command) {
|
|
62
|
-
void command;
|
|
63
|
-
const lines = raw.replace(/\r\n/g, "\n").split("\n");
|
|
64
|
-
// Small outputs pass through (no benefit, just adds noise)
|
|
65
|
-
if (lines.length < SMALL_THRESHOLD)
|
|
66
|
-
return raw;
|
|
67
|
-
const paths = [];
|
|
68
|
-
const nonPathLines = [];
|
|
69
|
-
for (const line of lines) {
|
|
70
|
-
const cleaned = cleanPath(line);
|
|
71
|
-
if (!cleaned)
|
|
72
|
-
continue;
|
|
73
|
-
if (cleaned.includes("/") && cleaned.length > 1 && !cleaned.includes(" ")) {
|
|
74
|
-
paths.push(cleaned);
|
|
75
|
-
}
|
|
76
|
-
else if (cleaned.length > 0 && nonPathLines.length < 6) {
|
|
77
|
-
nonPathLines.push(cleaned);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (paths.length === 0)
|
|
81
|
-
return raw;
|
|
82
|
-
const buckets = new Map();
|
|
83
|
-
const collapsedBuckets = new Map();
|
|
84
|
-
for (const p of paths) {
|
|
85
|
-
let collapsedKey = null;
|
|
86
|
-
for (const dir of COLLAPSE_DIRS) {
|
|
87
|
-
if (p.includes(`/${dir}/`) || p.startsWith(`${dir}/`)) {
|
|
88
|
-
collapsedKey = dir;
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (collapsedKey) {
|
|
93
|
-
collapsedBuckets.set(collapsedKey, (collapsedBuckets.get(collapsedKey) ?? 0) + 1);
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const dir = dirOf(p);
|
|
97
|
-
const base = p.slice(p.lastIndexOf("/") + 1);
|
|
98
|
-
let bucket = buckets.get(dir);
|
|
99
|
-
if (!bucket) {
|
|
100
|
-
bucket = {
|
|
101
|
-
files: 0,
|
|
102
|
-
byExt: new Map(),
|
|
103
|
-
subdirs: new Set(),
|
|
104
|
-
samples: [],
|
|
105
|
-
};
|
|
106
|
-
buckets.set(dir, bucket);
|
|
107
|
-
}
|
|
108
|
-
bucket.files++;
|
|
109
|
-
const ext = extOf(p);
|
|
110
|
-
bucket.byExt.set(ext, (bucket.byExt.get(ext) ?? 0) + 1);
|
|
111
|
-
if (bucket.samples.length < 3)
|
|
112
|
-
bucket.samples.push(base);
|
|
113
|
-
const parent = dirOf(dir);
|
|
114
|
-
const parentBucket = buckets.get(parent);
|
|
115
|
-
if (parentBucket)
|
|
116
|
-
parentBucket.subdirs.add(dir);
|
|
117
|
-
}
|
|
118
|
-
const sortedDirs = [...buckets.keys()].sort();
|
|
119
|
-
const out = ["_shell_fmt:tree_paths"];
|
|
120
|
-
out.push(`(${paths.length} paths across ${sortedDirs.length} dirs; rolled up)`);
|
|
121
|
-
for (const h of nonPathLines.slice(0, 2))
|
|
122
|
-
out.push(h);
|
|
123
|
-
for (const dir of sortedDirs) {
|
|
124
|
-
const b = buckets.get(dir);
|
|
125
|
-
if (!b)
|
|
126
|
-
continue;
|
|
127
|
-
const label = dir === "" ? "./" : `${dir}/`;
|
|
128
|
-
const extSummary = topExt(b.byExt);
|
|
129
|
-
const sample = b.samples.length > 0 && b.files <= 4
|
|
130
|
-
? ` — ${b.samples.join(", ")}`
|
|
131
|
-
: b.files <= 8
|
|
132
|
-
? ` — e.g. ${b.samples.join(", ")}`
|
|
133
|
-
: "";
|
|
134
|
-
out.push(`${label} (${b.files} files: ${extSummary})${sample}`);
|
|
135
|
-
}
|
|
136
|
-
for (const [dir, count] of collapsedBuckets) {
|
|
137
|
-
out.push(`${dir}/ (${count} entries) [collapsed]`);
|
|
138
|
-
}
|
|
139
|
-
for (const t of nonPathLines.slice(-2)) {
|
|
140
|
-
if (/\bdirector(?:y|ies)\b/i.test(t) || /\bfiles?\b/i.test(t))
|
|
141
|
-
out.push(t);
|
|
142
|
-
}
|
|
143
|
-
return out.join("\n");
|
|
144
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Strategy T10 — YAML output compression.
|
|
3
|
-
* Collapses large nested blocks, samples long arrays, prunes noisy metadata keys.
|
|
4
|
-
* Handles Kubernetes manifests, Helm values, docker-compose, and generic YAML.
|
|
5
|
-
*/
|
|
6
|
-
/** Kubernetes metadata keys that are typically noise for LLM consumption. */
|
|
7
|
-
const DROP_KEYS = new Set([
|
|
8
|
-
"managedFields",
|
|
9
|
-
"resourceVersion",
|
|
10
|
-
"selfLink",
|
|
11
|
-
"uid",
|
|
12
|
-
"creationTimestamp",
|
|
13
|
-
"generation",
|
|
14
|
-
"ownerReferences",
|
|
15
|
-
"finalizers",
|
|
16
|
-
"kubectl.kubernetes.io/last-applied-configuration",
|
|
17
|
-
"deployment.kubernetes.io/revision",
|
|
18
|
-
"control-plane.alpha.kubernetes.io/leader",
|
|
19
|
-
]);
|
|
20
|
-
const VALUE_MAX = 200;
|
|
21
|
-
/** Detect the indent unit (number of spaces per level) from the first indented line. */
|
|
22
|
-
function detectIndentUnit(lines) {
|
|
23
|
-
for (const line of lines) {
|
|
24
|
-
const m = /^( +)\S/.exec(line);
|
|
25
|
-
if (m)
|
|
26
|
-
return m[1]?.length ?? 0;
|
|
27
|
-
}
|
|
28
|
-
return 2;
|
|
29
|
-
}
|
|
30
|
-
/** Get the indentation level (in spaces) of a line. */
|
|
31
|
-
function indentOf(line) {
|
|
32
|
-
const m = /^( *)/.exec(line);
|
|
33
|
-
return m ? (m[1]?.length ?? 0) : 0;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Compress YAML output for token efficiency.
|
|
37
|
-
*
|
|
38
|
-
* Strategy:
|
|
39
|
-
* 1. Drop noisy k8s metadata keys and their sub-blocks
|
|
40
|
-
* 2. Collapse large nested blocks (>15 child lines) into summaries
|
|
41
|
-
* 3. Sample long YAML arrays (keep first 3, last 1, collapse middle)
|
|
42
|
-
* 4. Truncate long scalar values
|
|
43
|
-
* 5. Clip overall output if still too large
|
|
44
|
-
*/
|
|
45
|
-
export function compressYaml(raw, command) {
|
|
46
|
-
void command;
|
|
47
|
-
const lines = raw.split("\n");
|
|
48
|
-
if (lines.length <= 40) {
|
|
49
|
-
return raw;
|
|
50
|
-
}
|
|
51
|
-
const indentUnit = detectIndentUnit(lines);
|
|
52
|
-
const kept = [];
|
|
53
|
-
let i = 0;
|
|
54
|
-
while (i < lines.length) {
|
|
55
|
-
const line = lines[i];
|
|
56
|
-
const trimmed = line.trimStart();
|
|
57
|
-
// Skip empty lines — keep them
|
|
58
|
-
if (trimmed === "" || trimmed === "---" || trimmed === "...") {
|
|
59
|
-
kept.push(line);
|
|
60
|
-
i++;
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
// Check for key: pattern (not array items)
|
|
64
|
-
const keyMatch = /^(\s*)([\w][\w./-]*):\s*(.*)$/.exec(line);
|
|
65
|
-
if (keyMatch) {
|
|
66
|
-
const [, indent, key, value] = keyMatch;
|
|
67
|
-
const indentLen = indent?.length ?? 0;
|
|
68
|
-
// Drop noisy keys and their entire sub-block
|
|
69
|
-
if (DROP_KEYS.has(key)) {
|
|
70
|
-
i++;
|
|
71
|
-
// Skip child lines
|
|
72
|
-
while (i < lines.length) {
|
|
73
|
-
const nextLine = lines[i];
|
|
74
|
-
if (nextLine.trim() === "") {
|
|
75
|
-
i++;
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
if (indentOf(nextLine) <= indentLen && nextLine.trim())
|
|
79
|
-
break;
|
|
80
|
-
i++;
|
|
81
|
-
}
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
// Count child lines for this key
|
|
85
|
-
if (!value?.trim()) {
|
|
86
|
-
const childIndent = indentLen + indentUnit;
|
|
87
|
-
let childCount = 0;
|
|
88
|
-
let j = i + 1;
|
|
89
|
-
while (j < lines.length) {
|
|
90
|
-
const nextLine = lines[j];
|
|
91
|
-
if (nextLine.trim() === "") {
|
|
92
|
-
j++;
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
if (indentOf(nextLine) < childIndent)
|
|
96
|
-
break;
|
|
97
|
-
childCount++;
|
|
98
|
-
j++;
|
|
99
|
-
}
|
|
100
|
-
// Collapse large sub-blocks
|
|
101
|
-
if (childCount > 15) {
|
|
102
|
-
kept.push(line);
|
|
103
|
-
// Keep first 3 child lines as preview
|
|
104
|
-
let preview = 0;
|
|
105
|
-
let k = i + 1;
|
|
106
|
-
while (k < j && preview < 3) {
|
|
107
|
-
if (lines[k]?.trim()) {
|
|
108
|
-
kept.push(lines[k]);
|
|
109
|
-
preview++;
|
|
110
|
-
}
|
|
111
|
-
k++;
|
|
112
|
-
}
|
|
113
|
-
kept.push(`${" ".repeat(childIndent)}# ... ${childCount - 3} more lines`);
|
|
114
|
-
i = j;
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
// Truncate long scalar values
|
|
119
|
-
if (value && value.length > VALUE_MAX) {
|
|
120
|
-
kept.push(`${indent}${key}: ${value.slice(0, 100)}...(${value.length} chars)`);
|
|
121
|
-
i++;
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
// Array items: detect long arrays and sample them
|
|
126
|
-
if (/^\s*-\s/.test(line)) {
|
|
127
|
-
const arrayIndent = indentOf(line);
|
|
128
|
-
const arrayItems = [];
|
|
129
|
-
// Collect all array items at this indent level
|
|
130
|
-
let j = i;
|
|
131
|
-
while (j < lines.length) {
|
|
132
|
-
const cur = lines[j];
|
|
133
|
-
if (cur.trim() === "") {
|
|
134
|
-
j++;
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
if (indentOf(cur) < arrayIndent)
|
|
138
|
-
break;
|
|
139
|
-
if (indentOf(cur) === arrayIndent && /^\s*-\s/.test(cur)) {
|
|
140
|
-
const itemStart = j;
|
|
141
|
-
j++;
|
|
142
|
-
// Collect sub-lines of this array item
|
|
143
|
-
while (j < lines.length) {
|
|
144
|
-
const sub = lines[j];
|
|
145
|
-
if (sub.trim() === "") {
|
|
146
|
-
j++;
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
if (indentOf(sub) <= arrayIndent)
|
|
150
|
-
break;
|
|
151
|
-
j++;
|
|
152
|
-
}
|
|
153
|
-
arrayItems.push({ start: itemStart, end: j });
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
break;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (arrayItems.length > 8) {
|
|
160
|
-
// Sample: first 3, last 1, collapse middle
|
|
161
|
-
for (const item of arrayItems.slice(0, 3)) {
|
|
162
|
-
for (let k = item.start; k < item.end; k++)
|
|
163
|
-
kept.push(lines[k]);
|
|
164
|
-
}
|
|
165
|
-
kept.push(`${" ".repeat(arrayIndent)}# ... ${arrayItems.length - 4} more items`);
|
|
166
|
-
const lastItem = arrayItems[arrayItems.length - 1];
|
|
167
|
-
for (let k = lastItem.start; k < lastItem.end; k++)
|
|
168
|
-
kept.push(lines[k]);
|
|
169
|
-
i = j;
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
// Default: keep the line
|
|
174
|
-
kept.push(line);
|
|
175
|
-
i++;
|
|
176
|
-
}
|
|
177
|
-
const body = kept.join("\n");
|
|
178
|
-
const clipped = body.length > 12_000
|
|
179
|
-
? `${body.slice(0, 8000)}\n# ...yaml_omitted...\n${body.slice(-3000)}`
|
|
180
|
-
: body;
|
|
181
|
-
return `_shell_fmt:yaml\n${clipped}`;
|
|
182
|
-
}
|
package/dist/proxy/shell-tee.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shell output tee — persists full raw output to disk when compression is significant.
|
|
3
|
-
* Enables recovery of full context when compressed output is insufficient.
|
|
4
|
-
*/
|
|
5
|
-
import { mkdirSync, readdirSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
const MAX_TEE_FILES = 50;
|
|
8
|
-
const MAX_AGE_MS = 24 * 60 * 60 * 1000; // 24h
|
|
9
|
-
function commandSlug(cmd) {
|
|
10
|
-
return cmd
|
|
11
|
-
.trim()
|
|
12
|
-
.split(/\s+/)
|
|
13
|
-
.slice(0, 2)
|
|
14
|
-
.join("-")
|
|
15
|
-
.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
16
|
-
.slice(0, 40);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Writes full shell output to `.unerr/tee/<ts>-<slug>.txt` when:
|
|
20
|
-
* - compression ratio > 30%
|
|
21
|
-
* - raw size > 1KB
|
|
22
|
-
*
|
|
23
|
-
* Returns null if thresholds are not met or write fails.
|
|
24
|
-
*/
|
|
25
|
-
export function teeShellOutput(cwd, command, raw, compressed) {
|
|
26
|
-
const ratio = 1 - compressed.length / raw.length;
|
|
27
|
-
if (ratio < 0.3 || raw.length < 1024)
|
|
28
|
-
return null;
|
|
29
|
-
const teeDir = join(cwd, ".unerr", "tee");
|
|
30
|
-
try {
|
|
31
|
-
mkdirSync(teeDir, { recursive: true });
|
|
32
|
-
}
|
|
33
|
-
catch {
|
|
34
|
-
// already exists
|
|
35
|
-
}
|
|
36
|
-
const slug = commandSlug(command);
|
|
37
|
-
const ts = Date.now();
|
|
38
|
-
const filename = `${ts}-${slug}.txt`;
|
|
39
|
-
const filePath = join(teeDir, filename);
|
|
40
|
-
const header = [
|
|
41
|
-
"# unerr tee — full shell output",
|
|
42
|
-
`# command: ${command}`,
|
|
43
|
-
`# captured: ${new Date(ts).toISOString()}`,
|
|
44
|
-
`# raw_bytes: ${raw.length} compressed_bytes: ${compressed.length} ratio: ${(ratio * 100).toFixed(1)}%`,
|
|
45
|
-
"# ---",
|
|
46
|
-
"",
|
|
47
|
-
].join("\n");
|
|
48
|
-
const content = header + raw;
|
|
49
|
-
try {
|
|
50
|
-
writeFileSync(filePath, content, "utf8");
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
cleanupOldTees(teeDir);
|
|
56
|
-
return { filePath, sizeBytes: content.length };
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Removes tee files older than `maxAgeMs` and keeps at most `MAX_TEE_FILES`.
|
|
60
|
-
* Returns the number of files deleted.
|
|
61
|
-
*/
|
|
62
|
-
export function cleanupOldTees(teeDir, maxAgeMs = MAX_AGE_MS) {
|
|
63
|
-
let deleted = 0;
|
|
64
|
-
let entries = [];
|
|
65
|
-
try {
|
|
66
|
-
entries = readdirSync(teeDir)
|
|
67
|
-
.filter((f) => f.endsWith(".txt"))
|
|
68
|
-
.map((name) => {
|
|
69
|
-
const p = join(teeDir, name);
|
|
70
|
-
try {
|
|
71
|
-
const { mtimeMs } = statSync(p);
|
|
72
|
-
return { name, mtime: mtimeMs, path: p };
|
|
73
|
-
}
|
|
74
|
-
catch {
|
|
75
|
-
return { name, mtime: 0, path: p };
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
return 0;
|
|
81
|
-
}
|
|
82
|
-
const now = Date.now();
|
|
83
|
-
// Delete files older than maxAgeMs
|
|
84
|
-
for (const e of entries) {
|
|
85
|
-
if (now - e.mtime > maxAgeMs) {
|
|
86
|
-
try {
|
|
87
|
-
unlinkSync(e.path);
|
|
88
|
-
deleted++;
|
|
89
|
-
}
|
|
90
|
-
catch {
|
|
91
|
-
// ignore
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// Keep at most MAX_TEE_FILES (newest first)
|
|
96
|
-
const remaining = entries
|
|
97
|
-
.filter((e) => now - e.mtime <= maxAgeMs)
|
|
98
|
-
.sort((a, b) => b.mtime - a.mtime);
|
|
99
|
-
if (remaining.length > MAX_TEE_FILES) {
|
|
100
|
-
for (const e of remaining.slice(MAX_TEE_FILES)) {
|
|
101
|
-
try {
|
|
102
|
-
unlinkSync(e.path);
|
|
103
|
-
deleted++;
|
|
104
|
-
}
|
|
105
|
-
catch {
|
|
106
|
-
// ignore
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return deleted;
|
|
111
|
-
}
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Signal Emission Dedup — gates `ur|<tag>` signal lines so the same fact is not
|
|
3
|
-
* re-emitted on every tool call within a session.
|
|
4
|
-
*
|
|
5
|
-
* Why: `buildSignalPrefix` (response-envelope.ts) and `applyWireCap`
|
|
6
|
-
* (wire-cap.ts) inject anti-drift signals into MCP tool response bodies. Many
|
|
7
|
-
* of these — risk scores, project facts, hints — are *sticky knowledge*: true
|
|
8
|
-
* for the whole session and never changing. Re-emitting them on every call
|
|
9
|
-
* drowns the model in fluff and inflates token cost.
|
|
10
|
-
*
|
|
11
|
-
* Three signal classes:
|
|
12
|
-
* 1. Critical / state-change (always emit): hlt, dft, hth — circuit-break,
|
|
13
|
-
* drift, session health. Re-emit because state has actually changed.
|
|
14
|
-
* 2. Sticky knowledge (emit once or on content change): rsk, fct, hnt, hst,
|
|
15
|
-
* cnv, pro, sem, epi — surface once per (tag, entity), then again only
|
|
16
|
-
* if the content actually changes.
|
|
17
|
-
* 3. Hard once-per-session: wrn — anti-pattern warnings stay relevant the
|
|
18
|
-
* whole session but the agent only needs to be told once.
|
|
19
|
-
* 4. Drop entirely: ctx — "context already delivered" is itself noise; the
|
|
20
|
-
* session-dedup layer already gates the underlying context.
|
|
21
|
-
*
|
|
22
|
-
* Dedup key: `${tag}:${entityKey ?? "global"}:${valueHash}` for `on_change`
|
|
23
|
-
* policy, `${tag}:${entityKey ?? "global"}` for `once_per_session` / `always`
|
|
24
|
-
* (always still records but never blocks).
|
|
25
|
-
*
|
|
26
|
-
* Process-lifetime in-memory only (matches session-dedup.ts pattern). Bounded
|
|
27
|
-
* to MAX_TRACKED to prevent unbounded growth in long-running daemons.
|
|
28
|
-
*/
|
|
29
|
-
const MAX_TRACKED = 5000;
|
|
30
|
-
/**
|
|
31
|
-
* Default policy by 3-char signal tag. See ur|<tag> legend in
|
|
32
|
-
* response-envelope.ts (SIGNAL_PREFIX_LEGEND).
|
|
33
|
-
*/
|
|
34
|
-
export const DEFAULT_POLICY = {
|
|
35
|
-
hlt: "always",
|
|
36
|
-
dft: "always",
|
|
37
|
-
hth: "always",
|
|
38
|
-
rsk: "on_change",
|
|
39
|
-
fct: "on_change",
|
|
40
|
-
// `wrn` was once_per_session — that silenced ALL warnings after the first
|
|
41
|
-
// `ur|wrn No dependencies` line, including content-distinct negative-fact
|
|
42
|
-
// warnings ("Never mock CozoDB...", "anti-pattern in hooks...") that target
|
|
43
|
-
// a different entity. on_change already suppresses *identical* (tag, scope,
|
|
44
|
-
// content) repeats, which is the only thing once_per_session protected
|
|
45
|
-
// against. Different scope or different content = genuinely new warning,
|
|
46
|
-
// surface it.
|
|
47
|
-
wrn: "on_change",
|
|
48
|
-
hnt: "on_change",
|
|
49
|
-
hst: "on_change",
|
|
50
|
-
cnv: "on_change",
|
|
51
|
-
pro: "on_change",
|
|
52
|
-
sem: "on_change",
|
|
53
|
-
epi: "on_change",
|
|
54
|
-
ctx: "drop",
|
|
55
|
-
};
|
|
56
|
-
/**
|
|
57
|
-
* djb2 — fast non-cryptographic hash for content fingerprinting.
|
|
58
|
-
* Good enough to detect "same risk message as last time".
|
|
59
|
-
*/
|
|
60
|
-
function hash(s) {
|
|
61
|
-
let h = 5381;
|
|
62
|
-
for (let i = 0; i < s.length; i++) {
|
|
63
|
-
h = ((h << 5) + h) ^ s.charCodeAt(i);
|
|
64
|
-
}
|
|
65
|
-
return (h >>> 0).toString(36);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* N6 — per-tag session emission cap. Low-priority advisory tags (hint,
|
|
69
|
-
* fact, convention, episodic) are noisy when each new entity legitimately
|
|
70
|
-
* produces a different message — `on_change` lets them all through. The cap
|
|
71
|
-
* adds a session-level ceiling: after N emissions of the same tag in this
|
|
72
|
-
* session, further emissions are dropped regardless of content.
|
|
73
|
-
*
|
|
74
|
-
* Critical tags (hlt/dft/rsk/hth/wrn/hst) are NOT capped — those are
|
|
75
|
-
* load-bearing and must always reach the agent.
|
|
76
|
-
*/
|
|
77
|
-
const TAG_SESSION_CAP = {
|
|
78
|
-
hnt: 5,
|
|
79
|
-
fct: 4,
|
|
80
|
-
cnv: 4,
|
|
81
|
-
pro: 4,
|
|
82
|
-
sem: 4,
|
|
83
|
-
epi: 4,
|
|
84
|
-
};
|
|
85
|
-
export function createSignalDedup(policyOverride = {}) {
|
|
86
|
-
const seen = new Map();
|
|
87
|
-
const policy = { ...DEFAULT_POLICY, ...policyOverride };
|
|
88
|
-
const tagCounts = new Map();
|
|
89
|
-
function evictIfNeeded() {
|
|
90
|
-
if (seen.size <= MAX_TRACKED)
|
|
91
|
-
return;
|
|
92
|
-
const target = Math.floor(MAX_TRACKED * 0.2);
|
|
93
|
-
const iter = seen.keys();
|
|
94
|
-
let removed = 0;
|
|
95
|
-
while (removed < target) {
|
|
96
|
-
const r = iter.next();
|
|
97
|
-
if (r.done)
|
|
98
|
-
break;
|
|
99
|
-
seen.delete(r.value);
|
|
100
|
-
removed++;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
function shouldEmit(tag, entityKey, content) {
|
|
104
|
-
const p = policy[tag] ?? "on_change";
|
|
105
|
-
if (p === "drop")
|
|
106
|
-
return false;
|
|
107
|
-
if (p === "always")
|
|
108
|
-
return true;
|
|
109
|
-
// N6 — session-level cap for advisory tags
|
|
110
|
-
const cap = TAG_SESSION_CAP[tag];
|
|
111
|
-
if (cap !== undefined && (tagCounts.get(tag) ?? 0) >= cap) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
const k = `${tag}:${entityKey ?? "global"}`;
|
|
115
|
-
const entry = seen.get(k);
|
|
116
|
-
const contentHash = hash(content);
|
|
117
|
-
if (!entry) {
|
|
118
|
-
seen.set(k, { lastHash: contentHash, firstEmittedAt: Date.now() });
|
|
119
|
-
evictIfNeeded();
|
|
120
|
-
tagCounts.set(tag, (tagCounts.get(tag) ?? 0) + 1);
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
if (p === "once_per_session") {
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
// on_change — emit only if content actually changed.
|
|
127
|
-
if (entry.lastHash !== contentHash) {
|
|
128
|
-
entry.lastHash = contentHash;
|
|
129
|
-
tagCounts.set(tag, (tagCounts.get(tag) ?? 0) + 1);
|
|
130
|
-
return true;
|
|
131
|
-
}
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
function wouldEmit(tag, entityKey, content) {
|
|
135
|
-
const p = policy[tag] ?? "on_change";
|
|
136
|
-
if (p === "drop")
|
|
137
|
-
return false;
|
|
138
|
-
if (p === "always")
|
|
139
|
-
return true;
|
|
140
|
-
// N6 — session-cap check
|
|
141
|
-
const cap = TAG_SESSION_CAP[tag];
|
|
142
|
-
if (cap !== undefined && (tagCounts.get(tag) ?? 0) >= cap)
|
|
143
|
-
return false;
|
|
144
|
-
const k = `${tag}:${entityKey ?? "global"}`;
|
|
145
|
-
const entry = seen.get(k);
|
|
146
|
-
if (!entry)
|
|
147
|
-
return true;
|
|
148
|
-
if (p === "once_per_session")
|
|
149
|
-
return false;
|
|
150
|
-
return entry.lastHash !== hash(content);
|
|
151
|
-
}
|
|
152
|
-
function reset() {
|
|
153
|
-
seen.clear();
|
|
154
|
-
tagCounts.clear();
|
|
155
|
-
}
|
|
156
|
-
function size() {
|
|
157
|
-
return seen.size;
|
|
158
|
-
}
|
|
159
|
-
return { shouldEmit, wouldEmit, reset, size };
|
|
160
|
-
}
|
|
161
|
-
/** Module-level singleton — most callers share one process-wide table. */
|
|
162
|
-
let _singleton = null;
|
|
163
|
-
export function getSignalDedup() {
|
|
164
|
-
if (!_singleton)
|
|
165
|
-
_singleton = createSignalDedup();
|
|
166
|
-
return _singleton;
|
|
167
|
-
}
|
|
168
|
-
/** Test helper — clears the singleton between cases. */
|
|
169
|
-
export function resetSignalDedupSingleton() {
|
|
170
|
-
_singleton = null;
|
|
171
|
-
}
|