@unerr-ai/unerr 0.2.1 → 0.2.3
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 +36 -45
- package/dist/cli.js +37443 -36022
- package/package.json +2 -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,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session Resume Greeting — generates context from the previous session's
|
|
3
|
-
* shadow ledger data. Injected as the first _context in a new session.
|
|
4
|
-
*
|
|
5
|
-
* Template-based (no LLM). Reads last session entries and produces:
|
|
6
|
-
* - Files modified and their risk levels
|
|
7
|
-
* - Incomplete refactors (entities modified but not committed)
|
|
8
|
-
* - Tool usage patterns (what the agent was doing)
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Generate a session resume greeting from shadow ledger entries.
|
|
12
|
-
* Uses the last N entries from the previous session.
|
|
13
|
-
*/
|
|
14
|
-
export function generateSessionResume(entries, maxEntries = 50) {
|
|
15
|
-
if (entries.length === 0)
|
|
16
|
-
return null;
|
|
17
|
-
const recent = entries.slice(-maxEntries);
|
|
18
|
-
const sessionId = recent[recent.length - 1]?.session_id;
|
|
19
|
-
const sessionEntries = sessionId
|
|
20
|
-
? recent.filter((e) => e.session_id === sessionId)
|
|
21
|
-
: recent;
|
|
22
|
-
if (sessionEntries.length === 0)
|
|
23
|
-
return null;
|
|
24
|
-
const filesModified = new Set();
|
|
25
|
-
const toolsUsed = {};
|
|
26
|
-
const modifiedEntities = new Set();
|
|
27
|
-
const committedEntities = new Set();
|
|
28
|
-
for (const entry of sessionEntries) {
|
|
29
|
-
const tool = entry.tool;
|
|
30
|
-
toolsUsed[tool] = (toolsUsed[tool] ?? 0) + 1;
|
|
31
|
-
const args = entry.args_summary;
|
|
32
|
-
if (args.files && Array.isArray(args.files)) {
|
|
33
|
-
for (const f of args.files) {
|
|
34
|
-
const path = typeof f === "string" ? f : f.path;
|
|
35
|
-
if (path)
|
|
36
|
-
filesModified.add(path);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
if (typeof args.key === "string" && args.key.includes("/")) {
|
|
40
|
-
const filePath = args.key.includes("::")
|
|
41
|
-
? args.key.split("::")[0]
|
|
42
|
-
: args.key;
|
|
43
|
-
filesModified.add(filePath);
|
|
44
|
-
}
|
|
45
|
-
if (tool === "sync_local_diff") {
|
|
46
|
-
if (args.files && Array.isArray(args.files)) {
|
|
47
|
-
for (const f of args.files) {
|
|
48
|
-
modifiedEntities.add(typeof f === "string" ? f : (f.path ?? ""));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
if (entry.result_summary?.commit_sha) {
|
|
53
|
-
for (const e of modifiedEntities)
|
|
54
|
-
committedEntities.add(e);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const incompleteEntities = [...modifiedEntities].filter((e) => !committedEntities.has(e) && e !== "");
|
|
58
|
-
const firstTs = sessionEntries[0]?.ts;
|
|
59
|
-
const lastTs = sessionEntries[sessionEntries.length - 1]?.ts;
|
|
60
|
-
const sessionDurationMs = firstTs && lastTs
|
|
61
|
-
? new Date(lastTs).getTime() - new Date(firstTs).getTime()
|
|
62
|
-
: 0;
|
|
63
|
-
const lastBranch = sessionEntries[sessionEntries.length - 1]?.branch ?? null;
|
|
64
|
-
const parts = [];
|
|
65
|
-
parts.push(`Last session: ${sessionEntries.length} tool calls over ${formatDuration(sessionDurationMs)}`);
|
|
66
|
-
if (filesModified.size > 0) {
|
|
67
|
-
const fileList = [...filesModified].slice(0, 5);
|
|
68
|
-
parts.push(`Modified ${filesModified.size} file(s): ${fileList.join(", ")}${filesModified.size > 5 ? ` (+${filesModified.size - 5} more)` : ""}`);
|
|
69
|
-
}
|
|
70
|
-
if (incompleteEntities.length > 0) {
|
|
71
|
-
parts.push(`Uncommitted changes in: ${incompleteEntities.slice(0, 3).join(", ")}`);
|
|
72
|
-
}
|
|
73
|
-
if (lastBranch) {
|
|
74
|
-
parts.push(`Branch: ${lastBranch}`);
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
summary: `${parts.join(". ")}.`,
|
|
78
|
-
filesModified: [...filesModified],
|
|
79
|
-
toolsUsed,
|
|
80
|
-
lastBranch,
|
|
81
|
-
sessionDurationMs,
|
|
82
|
-
incompleteEntities,
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
function formatDuration(ms) {
|
|
86
|
-
if (ms < 60_000)
|
|
87
|
-
return `${Math.round(ms / 1000)}s`;
|
|
88
|
-
const minutes = Math.round(ms / 60_000);
|
|
89
|
-
if (minutes < 60)
|
|
90
|
-
return `${minutes}m`;
|
|
91
|
-
const hours = Math.floor(minutes / 60);
|
|
92
|
-
const remainingMinutes = minutes % 60;
|
|
93
|
-
return `${hours}h ${remainingMinutes}m`;
|
|
94
|
-
}
|
|
@@ -1,513 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session Value Counter — tracks proxy session metrics in memory.
|
|
3
|
-
*
|
|
4
|
-
* Printed on proxy shutdown to show the developer what unerr did for them.
|
|
5
|
-
* "unerr saved you 47k tokens ($0.94)" — the artifact that drives word-of-mouth.
|
|
6
|
-
*/
|
|
7
|
-
/** Average tokens per MCP tool call resolved locally. */
|
|
8
|
-
const AVG_TOKENS_SAVED_PER_LOCAL_CALL = 3200;
|
|
9
|
-
/** Approximate cost per 1k tokens (blended input/output for Claude Sonnet). */
|
|
10
|
-
const COST_PER_1K_TOKENS = 0.006;
|
|
11
|
-
// ── Latency Tracking ─────────────────────────────────────────────────
|
|
12
|
-
/** Fixed-size circular buffer for latency samples. Zero allocations after init. */
|
|
13
|
-
const LATENCY_BUFFER_SIZE = 1000;
|
|
14
|
-
export function createLatencyTracker() {
|
|
15
|
-
return {
|
|
16
|
-
samples: new Float64Array(LATENCY_BUFFER_SIZE),
|
|
17
|
-
cursor: 0,
|
|
18
|
-
totalSamples: 0,
|
|
19
|
-
localSamples: new Float64Array(LATENCY_BUFFER_SIZE),
|
|
20
|
-
localCursor: 0,
|
|
21
|
-
localTotalSamples: 0,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Record a latency sample. O(1), no allocation.
|
|
26
|
-
*/
|
|
27
|
-
export function recordLatency(tracker, latencyMs) {
|
|
28
|
-
// All samples
|
|
29
|
-
tracker.samples[tracker.cursor] = latencyMs;
|
|
30
|
-
tracker.cursor = (tracker.cursor + 1) % LATENCY_BUFFER_SIZE;
|
|
31
|
-
tracker.totalSamples++;
|
|
32
|
-
// Local samples
|
|
33
|
-
tracker.localSamples[tracker.localCursor] = latencyMs;
|
|
34
|
-
tracker.localCursor = (tracker.localCursor + 1) % LATENCY_BUFFER_SIZE;
|
|
35
|
-
tracker.localTotalSamples++;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Compute percentiles from a circular buffer. Allocates a sorted copy on demand.
|
|
39
|
-
* Only called at shutdown or for status — never in the hot path.
|
|
40
|
-
*/
|
|
41
|
-
export function computePercentiles(samples, totalSamples) {
|
|
42
|
-
const count = Math.min(totalSamples, LATENCY_BUFFER_SIZE);
|
|
43
|
-
if (count === 0)
|
|
44
|
-
return null;
|
|
45
|
-
// Copy active portion and sort
|
|
46
|
-
const active = Array.from(samples.subarray(0, count));
|
|
47
|
-
active.sort((a, b) => a - b);
|
|
48
|
-
return {
|
|
49
|
-
p50: active[Math.floor(count * 0.5)] ?? 0,
|
|
50
|
-
p95: active[Math.floor(count * 0.95)] ?? 0,
|
|
51
|
-
p99: active[Math.floor(count * 0.99)] ?? 0,
|
|
52
|
-
min: active[0] ?? 0,
|
|
53
|
-
max: active[count - 1] ?? 0,
|
|
54
|
-
count: totalSamples,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
export function createLocalModeStats() {
|
|
58
|
-
return {
|
|
59
|
-
filesIndexed: 0,
|
|
60
|
-
entitiesExtracted: 0,
|
|
61
|
-
edgesComputed: 0,
|
|
62
|
-
indexingTimeMs: 0,
|
|
63
|
-
communitiesDetected: 0,
|
|
64
|
-
embeddingsComputed: 0,
|
|
65
|
-
embeddingTimeMs: 0,
|
|
66
|
-
semanticSearches: 0,
|
|
67
|
-
tokensSavedByTruncation: 0,
|
|
68
|
-
truncatedResponses: 0,
|
|
69
|
-
correctionPatternsInjected: 0,
|
|
70
|
-
communityContextsInjected: 0,
|
|
71
|
-
blastRadiusComputations: 0,
|
|
72
|
-
cumulativeLatencySavedMs: 0,
|
|
73
|
-
firewallBlockedCount: 0,
|
|
74
|
-
graphQueriesByType: {},
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
export function recordGraphQuery(localStats, toolName) {
|
|
78
|
-
localStats.graphQueriesByType[toolName] =
|
|
79
|
-
(localStats.graphQueriesByType[toolName] ?? 0) + 1;
|
|
80
|
-
}
|
|
81
|
-
export function recordTruncationSavings(localStats, fullTokens, usedTokens) {
|
|
82
|
-
const saved = fullTokens - usedTokens;
|
|
83
|
-
if (saved > 0) {
|
|
84
|
-
localStats.tokensSavedByTruncation += saved;
|
|
85
|
-
localStats.truncatedResponses++;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
export function recordIndexingResult(localStats, result) {
|
|
89
|
-
localStats.filesIndexed = result.fileCount;
|
|
90
|
-
localStats.entitiesExtracted = result.entityCount;
|
|
91
|
-
localStats.edgesComputed = result.edgeCount;
|
|
92
|
-
localStats.indexingTimeMs = result.elapsedMs;
|
|
93
|
-
localStats.communitiesDetected = result.communityCount;
|
|
94
|
-
}
|
|
95
|
-
export function recordBlastRadius(localStats) {
|
|
96
|
-
localStats.blastRadiusComputations++;
|
|
97
|
-
}
|
|
98
|
-
export function recordCorrectionInjection(localStats) {
|
|
99
|
-
localStats.correctionPatternsInjected++;
|
|
100
|
-
}
|
|
101
|
-
export function recordCommunityContext(localStats) {
|
|
102
|
-
localStats.communityContextsInjected++;
|
|
103
|
-
}
|
|
104
|
-
export function recordEmbedding(localStats, count, timeMs) {
|
|
105
|
-
localStats.embeddingsComputed += count;
|
|
106
|
-
localStats.embeddingTimeMs += timeMs;
|
|
107
|
-
}
|
|
108
|
-
export function recordSemanticSearch(localStats) {
|
|
109
|
-
localStats.semanticSearches++;
|
|
110
|
-
}
|
|
111
|
-
/** Accumulate latency advantage (remote baseline minus actual local latency). */
|
|
112
|
-
export function recordLatencyAdvantage(localStats, advantageMs) {
|
|
113
|
-
localStats.cumulativeLatencySavedMs += advantageMs;
|
|
114
|
-
}
|
|
115
|
-
/** Snapshot firewall blocked count from NetworkFirewall at shutdown. */
|
|
116
|
-
export function snapshotFirewallCount(localStats) {
|
|
117
|
-
try {
|
|
118
|
-
// biome-ignore format: esbuild can't parse multi-line typeof import()
|
|
119
|
-
const { getBlockedCount } = require("./network-firewall.js");
|
|
120
|
-
localStats.firewallBlockedCount = getBlockedCount();
|
|
121
|
-
}
|
|
122
|
-
catch {
|
|
123
|
-
// NetworkFirewall not loaded — leave at 0
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
export function createSessionEvents() {
|
|
127
|
-
return {
|
|
128
|
-
conventionViolationsCaught: 0,
|
|
129
|
-
chokepointWarningsIssued: 0,
|
|
130
|
-
circularDepsDetected: 0,
|
|
131
|
-
signaturePreservations: 0,
|
|
132
|
-
deadCodeReferences: 0,
|
|
133
|
-
aiEntitiesModified: 0,
|
|
134
|
-
humanEntitiesModified: 0,
|
|
135
|
-
mixedEntitiesModified: 0,
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
/** Total caught events across all categories. */
|
|
139
|
-
export function totalCaughtEvents(events) {
|
|
140
|
-
return (events.conventionViolationsCaught +
|
|
141
|
-
events.chokepointWarningsIssued +
|
|
142
|
-
events.circularDepsDetected +
|
|
143
|
-
events.signaturePreservations +
|
|
144
|
-
events.deadCodeReferences);
|
|
145
|
-
}
|
|
146
|
-
export function createSessionStats(isLocalMode = false) {
|
|
147
|
-
return {
|
|
148
|
-
toolCallsLocal: 0,
|
|
149
|
-
estimatedTokensSaved: 0,
|
|
150
|
-
violationsCaught: 0,
|
|
151
|
-
riskWarningsIssued: 0,
|
|
152
|
-
sessionStartedAt: Date.now(),
|
|
153
|
-
latency: createLatencyTracker(),
|
|
154
|
-
events: createSessionEvents(),
|
|
155
|
-
isResumedSession: false,
|
|
156
|
-
previousSession: null,
|
|
157
|
-
localMode: isLocalMode ? createLocalModeStats() : null,
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Detect if this proxy start is a resume from a previous session.
|
|
162
|
-
* Checks for session_stats.json (written every 10s by running proxy)
|
|
163
|
-
* and shadow.jsonl (has entries from previous session).
|
|
164
|
-
*
|
|
165
|
-
* Returns previous session snapshot if resume detected, null otherwise.
|
|
166
|
-
*/
|
|
167
|
-
export function detectSessionResume(stateDir, ledgerDir) {
|
|
168
|
-
try {
|
|
169
|
-
const fs = require("node:fs");
|
|
170
|
-
const path = require("node:path");
|
|
171
|
-
const statsPath = path.join(stateDir, "session_stats.json");
|
|
172
|
-
const ledgerPath = path.join(ledgerDir, "shadow.jsonl");
|
|
173
|
-
if (!fs.existsSync(statsPath))
|
|
174
|
-
return null;
|
|
175
|
-
if (!fs.existsSync(ledgerPath))
|
|
176
|
-
return null;
|
|
177
|
-
// Check ledger has content (not empty)
|
|
178
|
-
const ledgerStat = fs.statSync(ledgerPath);
|
|
179
|
-
if (ledgerStat.size === 0)
|
|
180
|
-
return null;
|
|
181
|
-
// Read previous session stats
|
|
182
|
-
const raw = JSON.parse(fs.readFileSync(statsPath, "utf-8"));
|
|
183
|
-
// If the PID in stats matches current PID, this is the same process (not a resume)
|
|
184
|
-
if (raw.pid === process.pid)
|
|
185
|
-
return null;
|
|
186
|
-
const totalCalls = raw.toolCallsLocal ?? 0;
|
|
187
|
-
if (totalCalls === 0)
|
|
188
|
-
return null;
|
|
189
|
-
const startTime = raw.sessionStartedAt
|
|
190
|
-
? new Date(raw.sessionStartedAt).getTime()
|
|
191
|
-
: Date.now();
|
|
192
|
-
const endTime = raw.updatedAt
|
|
193
|
-
? new Date(raw.updatedAt).getTime()
|
|
194
|
-
: Date.now();
|
|
195
|
-
const durationMin = Math.round((endTime - startTime) / 60_000);
|
|
196
|
-
return {
|
|
197
|
-
toolCallsLocal: raw.toolCallsLocal ?? 0,
|
|
198
|
-
violationsCaught: raw.violationsCaught ?? 0,
|
|
199
|
-
sessionStartedAt: raw.sessionStartedAt ?? new Date().toISOString(),
|
|
200
|
-
durationMinutes: durationMin > 0 ? durationMin : 0,
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
catch {
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
export function recordToolCall(stats) {
|
|
208
|
-
stats.toolCallsLocal++;
|
|
209
|
-
stats.estimatedTokensSaved += AVG_TOKENS_SAVED_PER_LOCAL_CALL;
|
|
210
|
-
}
|
|
211
|
-
export function recordViolation(stats) {
|
|
212
|
-
stats.violationsCaught++;
|
|
213
|
-
stats.events.conventionViolationsCaught++;
|
|
214
|
-
}
|
|
215
|
-
export function recordRiskWarning(stats) {
|
|
216
|
-
stats.riskWarningsIssued++;
|
|
217
|
-
}
|
|
218
|
-
export function recordChokepointWarning(stats) {
|
|
219
|
-
stats.events.chokepointWarningsIssued++;
|
|
220
|
-
}
|
|
221
|
-
export function recordCircularDep(stats) {
|
|
222
|
-
stats.events.circularDepsDetected++;
|
|
223
|
-
}
|
|
224
|
-
export function recordSignaturePreservation(stats) {
|
|
225
|
-
stats.events.signaturePreservations++;
|
|
226
|
-
}
|
|
227
|
-
export function recordDeadCodeReference(stats) {
|
|
228
|
-
stats.events.deadCodeReferences++;
|
|
229
|
-
}
|
|
230
|
-
export function recordAttribution(stats, origin) {
|
|
231
|
-
if (origin === "ai")
|
|
232
|
-
stats.events.aiEntitiesModified++;
|
|
233
|
-
else if (origin === "human")
|
|
234
|
-
stats.events.humanEntitiesModified++;
|
|
235
|
-
else
|
|
236
|
-
stats.events.mixedEntitiesModified++;
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Format latency percentiles as a compact string.
|
|
240
|
-
*/
|
|
241
|
-
function formatLatencyLine(label, p) {
|
|
242
|
-
return ` ${label} p50=${p.p50.toFixed(1)}ms p95=${p.p95.toFixed(1)}ms p99=${p.p99.toFixed(1)}ms (n=${p.count})`;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Format session stats for terminal output.
|
|
246
|
-
* Returns null if no tool calls were made (nothing to show).
|
|
247
|
-
*/
|
|
248
|
-
export function formatSessionStats(stats) {
|
|
249
|
-
const total = stats.toolCallsLocal;
|
|
250
|
-
if (total === 0)
|
|
251
|
-
return null;
|
|
252
|
-
const durationMs = Date.now() - stats.sessionStartedAt;
|
|
253
|
-
const durationMin = Math.round(durationMs / 60_000);
|
|
254
|
-
const tokensSavedK = (stats.estimatedTokensSaved / 1000).toFixed(1);
|
|
255
|
-
const costSaved = ((stats.estimatedTokensSaved / 1000) *
|
|
256
|
-
COST_PER_1K_TOKENS).toFixed(2);
|
|
257
|
-
const lines = [
|
|
258
|
-
"",
|
|
259
|
-
"── unerr session ──────────────────────────────",
|
|
260
|
-
` Tool calls: ${total} (all local)`,
|
|
261
|
-
` Tokens saved: ~${tokensSavedK}k ($${costSaved})`,
|
|
262
|
-
];
|
|
263
|
-
// Latency percentiles
|
|
264
|
-
const localPercentiles = computePercentiles(stats.latency.localSamples, stats.latency.localTotalSamples);
|
|
265
|
-
if (localPercentiles) {
|
|
266
|
-
lines.push("");
|
|
267
|
-
lines.push(" Latency:");
|
|
268
|
-
lines.push(formatLatencyLine("Local: ", localPercentiles));
|
|
269
|
-
// Flag if local p99 exceeds the 5ms budget
|
|
270
|
-
if (localPercentiles.p99 > 5) {
|
|
271
|
-
lines.push(` ⚠ Local p99 (${localPercentiles.p99.toFixed(1)}ms) exceeds 5ms budget`);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
if (stats.violationsCaught > 0) {
|
|
275
|
-
lines.push(` Violations: ${stats.violationsCaught} caught`);
|
|
276
|
-
}
|
|
277
|
-
if (stats.riskWarningsIssued > 0) {
|
|
278
|
-
lines.push(` Risk warnings: ${stats.riskWarningsIssued} issued`);
|
|
279
|
-
}
|
|
280
|
-
// AI contribution ratio
|
|
281
|
-
const totalAttrib = stats.events.aiEntitiesModified +
|
|
282
|
-
stats.events.humanEntitiesModified +
|
|
283
|
-
stats.events.mixedEntitiesModified;
|
|
284
|
-
if (totalAttrib > 0) {
|
|
285
|
-
const aiPct = Math.round(((stats.events.aiEntitiesModified +
|
|
286
|
-
stats.events.mixedEntitiesModified * 0.5) /
|
|
287
|
-
totalAttrib) *
|
|
288
|
-
100);
|
|
289
|
-
lines.push(` AI contribution: ${aiPct}% (${stats.events.aiEntitiesModified} ai, ${stats.events.humanEntitiesModified} human, ${stats.events.mixedEntitiesModified} mixed)`);
|
|
290
|
-
}
|
|
291
|
-
lines.push(` Duration: ${durationMin > 0 ? `${durationMin}m` : "<1m"}`);
|
|
292
|
-
lines.push("───────────────────────────────────────────────");
|
|
293
|
-
lines.push("");
|
|
294
|
-
return lines.join("\n");
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Format Local Mode session stats for terminal output.
|
|
298
|
-
* Returns null if no tool calls were made.
|
|
299
|
-
*/
|
|
300
|
-
export function formatLocalModeSessionStats(stats) {
|
|
301
|
-
const total = stats.toolCallsLocal;
|
|
302
|
-
if (total === 0)
|
|
303
|
-
return null;
|
|
304
|
-
if (!stats.localMode)
|
|
305
|
-
return formatSessionStats(stats);
|
|
306
|
-
const lm = stats.localMode;
|
|
307
|
-
const durationMs = Date.now() - stats.sessionStartedAt;
|
|
308
|
-
const durationMin = Math.round(durationMs / 60_000);
|
|
309
|
-
const durationStr = durationMin > 0 ? `${durationMin} min` : "<1 min";
|
|
310
|
-
const localPercentiles = computePercentiles(stats.latency.localSamples, stats.latency.localTotalSamples);
|
|
311
|
-
// Aggregate graph query categories
|
|
312
|
-
const qbt = lm.graphQueriesByType;
|
|
313
|
-
const entityLookups = (qbt.get_function ?? 0) +
|
|
314
|
-
(qbt.get_class ?? 0) +
|
|
315
|
-
(qbt.get_file ?? 0) +
|
|
316
|
-
(qbt.get_module ?? 0);
|
|
317
|
-
const searchQueries = qbt.search_code ?? 0;
|
|
318
|
-
const callerCallees = (qbt.get_callers ?? 0) + (qbt.get_callees ?? 0);
|
|
319
|
-
const pad = (label, width) => label.padEnd(width);
|
|
320
|
-
const W = 22; // label width for alignment
|
|
321
|
-
const lines = [
|
|
322
|
-
"",
|
|
323
|
-
"[unerr] ─── Local Mode Session Summary ───────────────────",
|
|
324
|
-
`[unerr] ${pad("Duration:", W)} ${durationStr}`,
|
|
325
|
-
`[unerr] ${pad("MCP tool calls:", W)} ${total} (all local)`,
|
|
326
|
-
];
|
|
327
|
-
if (localPercentiles) {
|
|
328
|
-
lines.push(`[unerr] ${pad("Avg local latency:", W)} ${localPercentiles.p50.toFixed(1)}ms (p50), ${localPercentiles.p95.toFixed(1)}ms (p95)`);
|
|
329
|
-
if (lm.cumulativeLatencySavedMs > 0) {
|
|
330
|
-
lines.push(`[unerr] ${pad("Latency saved:", W)} ${(lm.cumulativeLatencySavedMs / 1000).toFixed(1)}s vs remote baseline`);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
// Graph Intelligence
|
|
334
|
-
if (entityLookups > 0 ||
|
|
335
|
-
lm.blastRadiusComputations > 0 ||
|
|
336
|
-
searchQueries > 0 ||
|
|
337
|
-
callerCallees > 0) {
|
|
338
|
-
lines.push("[unerr]");
|
|
339
|
-
lines.push("[unerr] Graph Intelligence:");
|
|
340
|
-
if (entityLookups > 0)
|
|
341
|
-
lines.push(`[unerr] ${pad("Entity lookups:", W - 2)} ${entityLookups}`);
|
|
342
|
-
if (lm.blastRadiusComputations > 0)
|
|
343
|
-
lines.push(`[unerr] ${pad("Blast radius:", W - 2)} ${lm.blastRadiusComputations} computations`);
|
|
344
|
-
if (searchQueries > 0)
|
|
345
|
-
lines.push(`[unerr] ${pad("Search queries:", W - 2)} ${searchQueries}`);
|
|
346
|
-
if (callerCallees > 0)
|
|
347
|
-
lines.push(`[unerr] ${pad("Callers/callees:", W - 2)} ${callerCallees}`);
|
|
348
|
-
}
|
|
349
|
-
// Token Discipline
|
|
350
|
-
if (lm.tokensSavedByTruncation > 0) {
|
|
351
|
-
lines.push("[unerr]");
|
|
352
|
-
lines.push("[unerr] Token Discipline:");
|
|
353
|
-
lines.push(`[unerr] ${pad("Tokens saved:", W - 2)} ~${lm.tokensSavedByTruncation.toLocaleString()} via smart truncation`);
|
|
354
|
-
lines.push(`[unerr] ${pad("Responses truncated:", W - 2)} ${lm.truncatedResponses}`);
|
|
355
|
-
}
|
|
356
|
-
// Safety Catches
|
|
357
|
-
const hasSafety = stats.violationsCaught > 0 ||
|
|
358
|
-
stats.riskWarningsIssued > 0 ||
|
|
359
|
-
stats.events.chokepointWarningsIssued > 0 ||
|
|
360
|
-
lm.correctionPatternsInjected > 0;
|
|
361
|
-
if (hasSafety) {
|
|
362
|
-
lines.push("[unerr]");
|
|
363
|
-
lines.push("[unerr] Safety Catches:");
|
|
364
|
-
if (stats.violationsCaught > 0)
|
|
365
|
-
lines.push(`[unerr] ${pad("Violations caught:", W - 2)} ${stats.violationsCaught}`);
|
|
366
|
-
if (stats.riskWarningsIssued > 0)
|
|
367
|
-
lines.push(`[unerr] ${pad("Risk warnings:", W - 2)} ${stats.riskWarningsIssued}`);
|
|
368
|
-
if (stats.events.chokepointWarningsIssued > 0)
|
|
369
|
-
lines.push(`[unerr] ${pad("Chokepoints flagged:", W - 2)} ${stats.events.chokepointWarningsIssued}`);
|
|
370
|
-
if (lm.correctionPatternsInjected > 0)
|
|
371
|
-
lines.push(`[unerr] ${pad("Corrections applied:", W - 2)} ${lm.correctionPatternsInjected}`);
|
|
372
|
-
}
|
|
373
|
-
// Semantic Intelligence (only if BYO-LLM was used)
|
|
374
|
-
if (lm.embeddingsComputed > 0) {
|
|
375
|
-
lines.push("[unerr]");
|
|
376
|
-
lines.push("[unerr] Semantic Intelligence:");
|
|
377
|
-
lines.push(`[unerr] ${pad("Embeddings computed:", W - 2)} ${lm.embeddingsComputed}`);
|
|
378
|
-
if (lm.semanticSearches > 0)
|
|
379
|
-
lines.push(`[unerr] ${pad("Semantic searches:", W - 2)} ${lm.semanticSearches}`);
|
|
380
|
-
}
|
|
381
|
-
// Network Isolation (always shown in Local Mode)
|
|
382
|
-
lines.push("[unerr]");
|
|
383
|
-
lines.push("[unerr] Network Isolation:");
|
|
384
|
-
lines.push(`[unerr] ${pad("Outbound calls:", W - 2)} 0 (firewall sealed)`);
|
|
385
|
-
lines.push(`[unerr] ${pad("Blocked attempts:", W - 2)} ${lm.firewallBlockedCount}${lm.firewallBlockedCount === 0 ? " (clean — no leakage)" : ""}`);
|
|
386
|
-
lines.push("[unerr] ────────────────────────────────────────────────────");
|
|
387
|
-
lines.push("");
|
|
388
|
-
return lines.join("\n");
|
|
389
|
-
}
|
|
390
|
-
function getWeekStart() {
|
|
391
|
-
const now = new Date();
|
|
392
|
-
const day = now.getDay();
|
|
393
|
-
const diff = now.getDate() - day + (day === 0 ? -6 : 1); // Monday
|
|
394
|
-
const monday = new Date(now);
|
|
395
|
-
monday.setDate(diff);
|
|
396
|
-
return monday.toISOString().slice(0, 10);
|
|
397
|
-
}
|
|
398
|
-
function getCumulativePath() {
|
|
399
|
-
const home = process.env.HOME ?? process.env.USERPROFILE ?? process.cwd();
|
|
400
|
-
return `${home}/.unerr/cumulative-stats.json`;
|
|
401
|
-
}
|
|
402
|
-
export function loadCumulativeStats() {
|
|
403
|
-
const currentWeek = getWeekStart();
|
|
404
|
-
try {
|
|
405
|
-
const { readFileSync } = require("node:fs");
|
|
406
|
-
const raw = JSON.parse(readFileSync(getCumulativePath(), "utf-8"));
|
|
407
|
-
// Reset if new week
|
|
408
|
-
if (raw.weekStart !== currentWeek) {
|
|
409
|
-
return {
|
|
410
|
-
totalTokensSaved: 0,
|
|
411
|
-
totalDollarsSaved: 0,
|
|
412
|
-
totalSessions: 0,
|
|
413
|
-
weekStart: currentWeek,
|
|
414
|
-
violationsCaughtAllTime: 0,
|
|
415
|
-
chokepointWarningsAllTime: 0,
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
return raw;
|
|
419
|
-
}
|
|
420
|
-
catch {
|
|
421
|
-
return {
|
|
422
|
-
totalTokensSaved: 0,
|
|
423
|
-
totalDollarsSaved: 0,
|
|
424
|
-
totalSessions: 0,
|
|
425
|
-
weekStart: currentWeek,
|
|
426
|
-
violationsCaughtAllTime: 0,
|
|
427
|
-
chokepointWarningsAllTime: 0,
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
export function persistCumulativeStats(stats) {
|
|
432
|
-
const cumulative = loadCumulativeStats();
|
|
433
|
-
cumulative.totalTokensSaved += stats.estimatedTokensSaved;
|
|
434
|
-
cumulative.totalDollarsSaved +=
|
|
435
|
-
(stats.estimatedTokensSaved / 1000) * COST_PER_1K_TOKENS;
|
|
436
|
-
cumulative.totalSessions += 1;
|
|
437
|
-
cumulative.violationsCaughtAllTime += totalCaughtEvents(stats.events);
|
|
438
|
-
cumulative.chokepointWarningsAllTime += stats.events.chokepointWarningsIssued;
|
|
439
|
-
try {
|
|
440
|
-
const fs = require("node:fs");
|
|
441
|
-
const path = require("node:path");
|
|
442
|
-
const filePath = getCumulativePath();
|
|
443
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
444
|
-
fs.writeFileSync(filePath, JSON.stringify(cumulative, null, 2));
|
|
445
|
-
}
|
|
446
|
-
catch {
|
|
447
|
-
// Non-critical
|
|
448
|
-
}
|
|
449
|
-
return cumulative;
|
|
450
|
-
}
|
|
451
|
-
function getCumulativeLocalPath() {
|
|
452
|
-
const home = process.env.HOME ?? process.env.USERPROFILE ?? process.cwd();
|
|
453
|
-
return `${home}/.unerr/cumulative-local-stats.json`;
|
|
454
|
-
}
|
|
455
|
-
function createEmptyCumulativeLocal(weekStart) {
|
|
456
|
-
return {
|
|
457
|
-
weekStartDate: weekStart,
|
|
458
|
-
totalSessions: 0,
|
|
459
|
-
totalToolCalls: 0,
|
|
460
|
-
totalTokensSaved: 0,
|
|
461
|
-
totalViolationsCaught: 0,
|
|
462
|
-
totalCorrectionsApplied: 0,
|
|
463
|
-
totalFilesIndexed: 0,
|
|
464
|
-
totalSemanticSearches: 0,
|
|
465
|
-
avgLatencyP50: 0,
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
export function loadCumulativeLocalStats() {
|
|
469
|
-
const currentWeek = getWeekStart();
|
|
470
|
-
try {
|
|
471
|
-
const { readFileSync } = require("node:fs");
|
|
472
|
-
const raw = JSON.parse(readFileSync(getCumulativeLocalPath(), "utf-8"));
|
|
473
|
-
if (raw.weekStartDate !== currentWeek) {
|
|
474
|
-
return createEmptyCumulativeLocal(currentWeek);
|
|
475
|
-
}
|
|
476
|
-
return raw;
|
|
477
|
-
}
|
|
478
|
-
catch {
|
|
479
|
-
return createEmptyCumulativeLocal(currentWeek);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
export function persistCumulativeLocalStats(stats) {
|
|
483
|
-
if (!stats.localMode)
|
|
484
|
-
return loadCumulativeLocalStats();
|
|
485
|
-
const lm = stats.localMode;
|
|
486
|
-
const cumulative = loadCumulativeLocalStats();
|
|
487
|
-
cumulative.totalSessions += 1;
|
|
488
|
-
cumulative.totalToolCalls += stats.toolCallsLocal;
|
|
489
|
-
cumulative.totalTokensSaved += lm.tokensSavedByTruncation;
|
|
490
|
-
cumulative.totalViolationsCaught += stats.violationsCaught;
|
|
491
|
-
cumulative.totalCorrectionsApplied += lm.correctionPatternsInjected;
|
|
492
|
-
cumulative.totalFilesIndexed += lm.filesIndexed;
|
|
493
|
-
cumulative.totalSemanticSearches += lm.semanticSearches;
|
|
494
|
-
// Rolling average of p50 latency
|
|
495
|
-
const localPercentiles = computePercentiles(stats.latency.localSamples, stats.latency.localTotalSamples);
|
|
496
|
-
if (localPercentiles && cumulative.totalSessions > 0) {
|
|
497
|
-
const prevWeight = (cumulative.totalSessions - 1) / cumulative.totalSessions;
|
|
498
|
-
const newWeight = 1 / cumulative.totalSessions;
|
|
499
|
-
cumulative.avgLatencyP50 =
|
|
500
|
-
cumulative.avgLatencyP50 * prevWeight + localPercentiles.p50 * newWeight;
|
|
501
|
-
}
|
|
502
|
-
try {
|
|
503
|
-
const fs = require("node:fs");
|
|
504
|
-
const path = require("node:path");
|
|
505
|
-
const filePath = getCumulativeLocalPath();
|
|
506
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
507
|
-
fs.writeFileSync(filePath, JSON.stringify(cumulative, null, 2));
|
|
508
|
-
}
|
|
509
|
-
catch {
|
|
510
|
-
// Non-critical
|
|
511
|
-
}
|
|
512
|
-
return cumulative;
|
|
513
|
-
}
|