ctxtop 0.0.0-stage → 4.1.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/.antigravity-plugin/plugin.json +36 -0
- package/.claude-plugin/plugin.json +18 -0
- package/CHECKSUMS.sha256 +166 -0
- package/LICENSE +27 -0
- package/README.md +350 -2
- package/SECURITY.md +22 -0
- package/agents/ah-flash-scout.md +25 -0
- package/assets/advanced_view_loaded_420px.png +0 -0
- package/assets/hero-demo.svg +188 -0
- package/assets/logo.svg +19 -0
- package/assets/simple_view_lean_420px.png +0 -0
- package/assets/simple_view_loaded_420px.png +0 -0
- package/assets/social-preview.svg +72 -0
- package/assets/zone_c_step3_shield_full.png +0 -0
- package/bin/ah-statusline.mjs +175 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1799 -0
- package/commands/cost-radar.md +11 -0
- package/commands/evolve.md +10 -0
- package/commands/handoff.md +10 -0
- package/commands/horizon.md +10 -0
- package/commands/inspect.md +10 -0
- package/commands/precheck.md +10 -0
- package/config/default_flightcheck.json +133 -0
- package/config/modelCatalog.json +785 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +577 -0
- package/hooks/handlers/config-change.mjs +402 -0
- package/hooks/handlers/hookIo.mjs +311 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-model-switch.mjs +111 -0
- package/hooks/handlers/post-tool-use.mjs +366 -0
- package/hooks/handlers/pre-model-switch.mjs +1048 -0
- package/hooks/handlers/pre-tool-use.mjs +632 -0
- package/hooks/handlers/stop-gate.mjs +526 -0
- package/hooks/handlers/user-prompt-submit.mjs +367 -0
- package/hooks/hooks.json +131 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +25 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +726 -0
- package/hooks/session_start.mjs +24 -0
- package/hooks/stop.mjs +23 -0
- package/hooks.json +56 -0
- package/package.json +110 -4
- package/packages/antigravity/package.json +14 -0
- package/packages/antigravity/src/index.mjs +1 -0
- package/packages/claude-code/package.json +14 -0
- package/packages/claude-code/src/index.mjs +2 -0
- package/packages/core/package.json +14 -0
- package/packages/core/src/index.mjs +1 -0
- package/packages/engine-node/package.json +14 -0
- package/packages/engine-node/src/index.mjs +1 -0
- package/packages/mcp/package.json +17 -0
- package/packages/mcp/src/server.mjs +396 -0
- package/packages/pro/LICENSE-COMMERCIAL.md +19 -0
- package/packages/pro/README-SIGNING.md +30 -0
- package/packages/pro/package.json +16 -0
- package/packages/pro/src/index.mjs +3 -0
- package/packages/pro/src/team_finops_pack.mjs +241 -0
- package/packages/pro/workers/polar_webhook_worker.mjs +215 -0
- package/plugin.json +36 -0
- package/rules/AGENTS.md +10 -0
- package/sidecars/horizon-daemon/ipcServer.mjs +24 -0
- package/sidecars/horizon-daemon/server.mjs +2038 -0
- package/sidecars/horizon-daemon/sidecar.json +19 -0
- package/skills/cost-radar/SKILL.md +21 -0
- package/skills/evolve/SKILL.md +21 -0
- package/skills/handoff/SKILL.md +50 -0
- package/skills/horizon/SKILL.md +17 -0
- package/skills/inspect/SKILL.md +18 -0
- package/skills/precheck/SKILL.md +44 -0
- package/src/adapters/antigravity/index.mjs +109 -0
- package/src/adapters/claude-code/index.mjs +194 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +888 -0
- package/src/cli/init.mjs +425 -0
- package/src/cli/postmortem.mjs +515 -0
- package/src/coach/cacheGravityRouter.mjs +12 -0
- package/src/config/modelCatalog.json +704 -0
- package/src/config/platformQuirks.json +19 -0
- package/src/core/action_queue.mjs +264 -0
- package/src/core/ag_bridge.mjs +346 -0
- package/src/core/atomic_io.mjs +124 -0
- package/src/core/baseline.mjs +607 -0
- package/src/core/blindspot_sensors.mjs +399 -0
- package/src/core/bucket_b_runtime.mjs +1200 -0
- package/src/core/classifier.mjs +1757 -0
- package/src/core/coach/circuit_breakers.mjs +650 -0
- package/src/core/coach/contradiction_auditor.mjs +239 -0
- package/src/core/coach/effort_advisor.mjs +143 -0
- package/src/core/coach/handoff_hydrator.mjs +483 -0
- package/src/core/coach/progressive_checkpointer.mjs +551 -0
- package/src/core/coach/prompt_decomposer_coach.mjs +141 -0
- package/src/core/coach/reflector_queue.mjs +505 -0
- package/src/core/coach/saved_quota_ledger.mjs +615 -0
- package/src/core/coach/subagent_ipi_sanitizer.mjs +200 -0
- package/src/core/coach/subagent_phantom_auditor.mjs +261 -0
- package/src/core/coach/task_quota_router.mjs +955 -0
- package/src/core/coach/turn_zero_gate.mjs +574 -0
- package/src/core/compaction_coach.mjs +1126 -0
- package/src/core/config.mjs +214 -0
- package/src/core/constants.mjs +1987 -0
- package/src/core/detectors_det01_to_16.mjs +1523 -0
- package/src/core/doc_indexer.mjs +338 -0
- package/src/core/episodic_memory.mjs +135 -0
- package/src/core/fts5_output_compressor.mjs +359 -0
- package/src/core/git_ledger.mjs +189 -0
- package/src/core/handoff_audit.mjs +558 -0
- package/src/core/history.mjs +221 -0
- package/src/core/hook_diagnostics.mjs +158 -0
- package/src/core/horizon_classifier.mjs +326 -0
- package/src/core/horizon_config.mjs +802 -0
- package/src/core/host_watchdog.mjs +200 -0
- package/src/core/layer5_hygiene.mjs +1084 -0
- package/src/core/log_resolver.mjs +231 -0
- package/src/core/mcp_auditor.mjs +138 -0
- package/src/core/micro_skills_store.mjs +91 -0
- package/src/core/offenders.mjs +161 -0
- package/src/core/parser.mjs +1997 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +311 -0
- package/src/core/prefix_cache_shield.mjs +386 -0
- package/src/core/pro_license.mjs +385 -0
- package/src/core/prompt_rubric.mjs +518 -0
- package/src/core/pruner.mjs +433 -0
- package/src/core/quota.mjs +1860 -0
- package/src/core/rsi_instinct_engine.mjs +1197 -0
- package/src/core/rsl_clash_auditor.mjs +423 -0
- package/src/core/rsl_distiller.mjs +372 -0
- package/src/core/rsl_ledger.mjs +969 -0
- package/src/core/rsl_triage.mjs +909 -0
- package/src/core/sanitizer.mjs +121 -0
- package/src/core/session_auditor.mjs +658 -0
- package/src/core/session_forensics.mjs +303 -0
- package/src/core/session_timeline.mjs +358 -0
- package/src/core/settings.mjs +99 -0
- package/src/core/shared/detectors.mjs +1342 -0
- package/src/core/shared/fnv1a64.mjs +85 -0
- package/src/core/shared/handoff_schema.mjs +123 -0
- package/src/core/shared/index.mjs +5 -0
- package/src/core/shared/ipi_sanitizer.mjs +165 -0
- package/src/core/shared/micro_skills.mjs +1 -0
- package/src/core/shared/plugin_validator.mjs +202 -0
- package/src/core/shared/prompt_hygiene_governor.mjs +1 -0
- package/src/core/shield_manager.mjs +1417 -0
- package/src/core/sidecar_store.mjs +302 -0
- package/src/core/sprint_metrics.mjs +366 -0
- package/src/core/sqlite_client.mjs +283 -0
- package/src/core/storage_doctor.mjs +358 -0
- package/src/core/subagents.mjs +609 -0
- package/src/core/swarm_topology.mjs +360 -0
- package/src/core/task_quota_router.mjs +1656 -0
- package/src/core/telemetry/cache_ttl_watchdog.mjs +284 -0
- package/src/core/telemetry/scope_lock_registry.mjs +578 -0
- package/src/core/telemetry/shadow_file_detector.mjs +291 -0
- package/src/core/telemetry/trajectory_watcher.mjs +259 -0
- package/src/core/timeline_svg.mjs +236 -0
- package/src/core/tool_surgeon.mjs +305 -0
- package/src/core/trajectory_stitcher.mjs +645 -0
- package/src/core/u_shaped_optimizer.mjs +789 -0
- package/src/core/wal_hygiene.mjs +233 -0
- package/src/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/src/core-isomorphic/catalog/index.mjs +1 -0
- package/src/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
- package/src/core-isomorphic/coach/microSkills.mjs +1 -0
- package/src/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/src/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/src/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +606 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +1268 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1055 -0
- package/src/core-isomorphic/control/callTree.mjs +338 -0
- package/src/core-isomorphic/control/coachLatches.mjs +126 -0
- package/src/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/src/core-isomorphic/control/index.mjs +49 -0
- package/src/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/src/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/src/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/src/core-isomorphic/diagnostics/index.mjs +9 -0
- package/src/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +369 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +256 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +433 -0
- package/src/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/src/core-isomorphic/economics/index.mjs +47 -0
- package/src/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/src/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
- package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
- package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/src/core-isomorphic/index.js +27 -0
- package/src/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/src/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/src/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/src/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +1156 -0
- package/src/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/src/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/src/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +1237 -0
- package/src/core-isomorphic/ui/modViews.mjs +1504 -0
- package/src/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/src/core-isomorphic/ustf/index.mjs +7 -0
- package/src/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/src/core-isomorphic/ustf/schema.mjs +186 -0
- package/src/core-isomorphic/utils/runtime.mjs +962 -0
- package/src/demo/synthetic_states.mjs +661 -0
- package/src/diagnostics/bugReportGenerator.mjs +253 -0
- package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
- package/src/diagnostics/doctorEngine.mjs +295 -0
- package/src/engine-node/db/index.mjs +58 -0
- package/src/engine-node/db/jsonl_fallback.mjs +184 -0
- package/src/engine-node/db/probe.mjs +342 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +12 -0
- package/src/engine-node/ipc/client.mjs +447 -0
- package/src/engine-node/ipc/index.mjs +22 -0
- package/src/engine-node/ipc/protocol.mjs +177 -0
- package/src/engine-node/ipc/server.mjs +445 -0
- package/src/engine-node/offloader/index.mjs +6 -0
- package/src/engine-node/offloader/tool_offloader.mjs +241 -0
- package/src/engine-node/sensors/blindspots.mjs +376 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/antigravity_rsl_hook.mjs +250 -0
- package/src/hooks/claude_rsl_hook.mjs +288 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +611 -0
- package/src/hooks/pre_invocation.mjs +1725 -0
- package/src/hooks/session_start.mjs +199 -0
- package/src/shims/agy_cli_compressor.mjs +705 -0
- package/src/shims/agy_git_guard.mjs +749 -0
- package/src/shims/ah_log_query.mjs +39 -0
- package/src/ui/costRadarSvg.mjs +6 -0
- package/src/ui/horizonGrid.mjs +1 -0
- package/src/ui/horizonPanelHtml.mjs +1 -0
- package/src/ui/horizon_panel.css +167 -0
- package/src/ui/horizon_panel.js +212 -0
- package/src/ui/modViews.mjs +1 -0
- package/src/utils/modelSwitchTelemetry.mjs +308 -0
- package/src/utils/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/utils/sessionCalibration.mjs +657 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +12397 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Per-session snapshot history powering the turn-to-turn token delta
|
|
6
|
+
* ("+4.2k since last turn · from read_file foo.html").
|
|
7
|
+
*
|
|
8
|
+
* Snapshots are keyed by user-turn count: repeated polls within the same user
|
|
9
|
+
* turn overwrite the latest snapshot, so the delta always compares the live
|
|
10
|
+
* state against the end of the previous user turn.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const HISTORY_MAX_SNAPSHOTS = 50;
|
|
14
|
+
/** A drop larger than this fraction of the previous total is reported as a compaction. */
|
|
15
|
+
export const COMPACTION_DROP_RATIO = 0.2;
|
|
16
|
+
|
|
17
|
+
export function historyDir(cacheDir) {
|
|
18
|
+
return path.join(cacheDir, 'history');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function historyFile(cacheDir, sessionId) {
|
|
22
|
+
const safe = String(sessionId || 'unknown').replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
23
|
+
return path.join(historyDir(cacheDir), `${safe}.json`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function loadHistory(cacheDir, sessionId) {
|
|
27
|
+
try {
|
|
28
|
+
const data = JSON.parse(fs.readFileSync(historyFile(cacheDir, sessionId), 'utf8'));
|
|
29
|
+
return { snapshots: Array.isArray(data.snapshots) ? data.snapshots : [], flags: data.flags || {} };
|
|
30
|
+
} catch {
|
|
31
|
+
return { snapshots: [], flags: {} };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function saveHistory(cacheDir, sessionId, history) {
|
|
36
|
+
const file = historyFile(cacheDir, sessionId);
|
|
37
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
38
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
39
|
+
fs.writeFileSync(tmp, JSON.stringify(history), 'utf8');
|
|
40
|
+
fs.renameSync(tmp, file);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const isUserTurn = s => s && (s.type === 'USER_INPUT');
|
|
44
|
+
|
|
45
|
+
export function countUserTurns(steps) {
|
|
46
|
+
return (steps || []).reduce((n, s) => n + (isUserTurn(s) ? 1 : 0), 0);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Same per-step estimate the classifier uses (before provider rescaling). */
|
|
50
|
+
export function estimateStepTokens(step) {
|
|
51
|
+
if (!step) return 0;
|
|
52
|
+
if (step.inputTokens > 0 && step.outputTokens > 0) {
|
|
53
|
+
return Math.max(step.outputTokens, Math.round((step.untruncatedLength || 0) / 4));
|
|
54
|
+
}
|
|
55
|
+
return step.outputTokens || Math.round((step.untruncatedLength || 0) / 4) || 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Picks the meaningful executable from a compound shell command line, skipping
|
|
60
|
+
* directory/env prelude segments: `cd`, `Set-Location`, `pushd`, `$env:X=...`, `set X=...`.
|
|
61
|
+
* "cd c:\x; node tests/a.mjs" -> "node".
|
|
62
|
+
*/
|
|
63
|
+
export function primaryCommandName(commandLine = '') {
|
|
64
|
+
const segments = String(commandLine || '').split(/;|&&|\|\|/).map(s => s.trim()).filter(Boolean);
|
|
65
|
+
const isPrelude = (seg) =>
|
|
66
|
+
/^(cd|chdir|set-location|sl|pushd|popd)(\s|$)/i.test(seg) ||
|
|
67
|
+
/^\$env:/i.test(seg) ||
|
|
68
|
+
/^(set|export)\s+\w+=/i.test(seg);
|
|
69
|
+
const chosen = segments.find(s => !isPrelude(s)) || segments[0] || '';
|
|
70
|
+
const first = chosen.replace(/^[&.]\s+/, '').split(/\s+/)[0] || '';
|
|
71
|
+
return first.replace(/^["']|["']$/g, '').split(/[\\/]/).pop();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function describeStep(step) {
|
|
75
|
+
if (typeof step === 'string') {
|
|
76
|
+
return `run_command ${primaryCommandName(step).slice(0, 40)}`;
|
|
77
|
+
}
|
|
78
|
+
const call = (step?.toolCalls || [])[0];
|
|
79
|
+
if (call) {
|
|
80
|
+
const a = call.args || {};
|
|
81
|
+
if (a.CommandLine) {
|
|
82
|
+
const cmd = primaryCommandName(a.CommandLine);
|
|
83
|
+
return `${call.name || 'run_command'} ${cmd.slice(0, 40)}`;
|
|
84
|
+
}
|
|
85
|
+
const target = a.Path || a.path || a.AbsolutePath || a.TargetFile || a.query || '';
|
|
86
|
+
const short = String(target).split(/[\\/]/).filter(Boolean).pop() || String(target);
|
|
87
|
+
return `${call.name || 'tool'}${short ? ' ' + short.slice(0, 60) : ''}`;
|
|
88
|
+
}
|
|
89
|
+
if (isUserTurn(step)) return 'user prompt';
|
|
90
|
+
const type = String(step?.type || '').toUpperCase();
|
|
91
|
+
if (/PLANNER|MODEL|RESPONSE|ASSISTANT|AGENT/.test(type)) return 'agent response';
|
|
92
|
+
// Tool-less steps with a generic/unknown type (never surface the raw string "generic")
|
|
93
|
+
return 'system context';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function buildSnapshot(state, steps) {
|
|
97
|
+
return {
|
|
98
|
+
userTurn: countUserTurns(steps),
|
|
99
|
+
stepCount: (steps || []).length,
|
|
100
|
+
total: state?.window?.totalActiveTokens || 0,
|
|
101
|
+
byCategory: { ...(state?.breakdown || {}) },
|
|
102
|
+
ts: Date.now()
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function hasCompactionMarker(steps = [], startIndex = 0) {
|
|
107
|
+
for (let i = Math.max(0, startIndex); i < steps.length; i++) {
|
|
108
|
+
const s = steps[i];
|
|
109
|
+
if (!s) continue;
|
|
110
|
+
const content = String(s.content || '');
|
|
111
|
+
if (
|
|
112
|
+
s.type === 'CHECKPOINT' ||
|
|
113
|
+
content.includes('<CONTEXT_SUMMARY>') ||
|
|
114
|
+
content.includes('[Context Compacted]')
|
|
115
|
+
) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Computes the delta between two snapshots. `topSource` is the largest raw step
|
|
124
|
+
* added since `prev.stepCount`. Distinguishes Undo/Rewind rollbacks from Compaction.
|
|
125
|
+
*/
|
|
126
|
+
export function computeDelta(prev, curr, steps = []) {
|
|
127
|
+
if (!prev || !curr) return null;
|
|
128
|
+
const tokens = curr.total - prev.total;
|
|
129
|
+
const byCategory = {};
|
|
130
|
+
const keys = new Set([...Object.keys(prev.byCategory || {}), ...Object.keys(curr.byCategory || {})]);
|
|
131
|
+
for (const k of keys) {
|
|
132
|
+
const d = (curr.byCategory?.[k] || 0) - (prev.byCategory?.[k] || 0);
|
|
133
|
+
if (d !== 0) byCategory[k] = d;
|
|
134
|
+
}
|
|
135
|
+
let topSource = null;
|
|
136
|
+
const parentSteps = (steps || []).filter(s => !s?.agentId || s.agentId === 'parent');
|
|
137
|
+
for (let i = Math.max(0, prev.stepCount || 0); i < parentSteps.length; i++) {
|
|
138
|
+
const t = estimateStepTokens(parentSteps[i]);
|
|
139
|
+
if (t > 0 && (!topSource || t > topSource.tokens)) {
|
|
140
|
+
topSource = { label: describeStep(parentSteps[i]), tokens: t, stepIndex: i };
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const stepRegressed = curr.stepCount < prev.stepCount;
|
|
144
|
+
const markerFound = hasCompactionMarker(parentSteps, Math.max(0, curr.stepCount - 3));
|
|
145
|
+
const rewound = stepRegressed && !markerFound;
|
|
146
|
+
const compacted = !rewound && prev.total > 0 && (tokens < -COMPACTION_DROP_RATIO * prev.total || (stepRegressed && markerFound));
|
|
147
|
+
return { tokens, byCategory, topSource, compacted, rewound, sinceUserTurn: prev.userTurn };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Records the snapshot for the current user turn and returns
|
|
152
|
+
* `{ delta, history }`. Never throws; history I/O failures yield `delta: null`.
|
|
153
|
+
*/
|
|
154
|
+
export function recordAndDelta(cacheDir, sessionId, state, steps) {
|
|
155
|
+
try {
|
|
156
|
+
const parentSteps = (steps || []).filter(s => !s?.agentId || s.agentId === 'parent');
|
|
157
|
+
const history = loadHistory(cacheDir, sessionId);
|
|
158
|
+
const snap = buildSnapshot(state, parentSteps);
|
|
159
|
+
const snaps = history.snapshots;
|
|
160
|
+
const last = snaps[snaps.length - 1];
|
|
161
|
+
|
|
162
|
+
// Detect Undo/Rewind rollback vs full replacement
|
|
163
|
+
let rewindFromSnap = null;
|
|
164
|
+
if (last && (snap.stepCount < last.stepCount || snap.userTurn < last.userTurn)) {
|
|
165
|
+
const isCompact = hasCompactionMarker(parentSteps, Math.max(0, snap.stepCount - 3));
|
|
166
|
+
if (!isCompact) {
|
|
167
|
+
rewindFromSnap = last;
|
|
168
|
+
while (snaps.length > 0 && (snaps[snaps.length - 1].userTurn > snap.userTurn || snaps[snaps.length - 1].stepCount > snap.stepCount)) {
|
|
169
|
+
snaps.pop();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const latest = snaps[snaps.length - 1];
|
|
175
|
+
if (latest && latest.userTurn === snap.userTurn) snaps[snaps.length - 1] = snap;
|
|
176
|
+
else snaps.push(snap);
|
|
177
|
+
if (snaps.length > HISTORY_MAX_SNAPSHOTS) snaps.splice(0, snaps.length - HISTORY_MAX_SNAPSHOTS);
|
|
178
|
+
|
|
179
|
+
let prev = rewindFromSnap;
|
|
180
|
+
if (!prev) {
|
|
181
|
+
for (let i = snaps.length - 1; i >= 0; i--) {
|
|
182
|
+
if (snaps[i].userTurn < snap.userTurn) { prev = snaps[i]; break; }
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const delta = computeDelta(prev, snap, parentSteps);
|
|
186
|
+
saveHistory(cacheDir, sessionId, history);
|
|
187
|
+
return { delta, history };
|
|
188
|
+
} catch {
|
|
189
|
+
return { delta: null, history: null };
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* One-time compact reminder per crossing of `state.window.reminderAt`.
|
|
195
|
+
* Returns true exactly once per crossing; the flag re-arms after a compaction
|
|
196
|
+
* (delta.compacted) or when usage falls back below the reminder line.
|
|
197
|
+
*/
|
|
198
|
+
export function shouldFireReminder(cacheDir, sessionId, state, delta) {
|
|
199
|
+
try {
|
|
200
|
+
const total = state?.window?.totalActiveTokens || 0;
|
|
201
|
+
const reminderAt = state?.window?.reminderAt;
|
|
202
|
+
if (!reminderAt) return false;
|
|
203
|
+
const history = loadHistory(cacheDir, sessionId);
|
|
204
|
+
const fired = Boolean(history.flags.reminderFired);
|
|
205
|
+
let fire = false;
|
|
206
|
+
if (delta?.compacted || total < reminderAt) {
|
|
207
|
+
if (!fired) return false;
|
|
208
|
+
history.flags.reminderFired = false;
|
|
209
|
+
} else if (!fired) {
|
|
210
|
+
history.flags.reminderFired = true;
|
|
211
|
+
history.flags.reminderFiredAt = Date.now();
|
|
212
|
+
fire = true;
|
|
213
|
+
} else {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
saveHistory(cacheDir, sessionId, history);
|
|
217
|
+
return fire;
|
|
218
|
+
} catch {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { atomicWriteFileSync } from './atomic_io.mjs';
|
|
5
|
+
|
|
6
|
+
export const MAX_HOOK_DIAGNOSTIC_ENTRIES = 50;
|
|
7
|
+
export const HOOK_DIAGNOSTICS_FILENAME = 'ga_hook_diagnostics.jsonl';
|
|
8
|
+
|
|
9
|
+
const inMemoryDiagnostics = [];
|
|
10
|
+
let ignoredDiagnosticCount = 0;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Resolves the diagnostics directory outside the workspace root so hook/shim telemetry
|
|
14
|
+
* never pollutes `.agents/cache/` or `stdout`.
|
|
15
|
+
*/
|
|
16
|
+
export function getHookDiagnosticsDir(options = {}) {
|
|
17
|
+
if (options.diagnosticsDir && typeof options.diagnosticsDir === 'string') {
|
|
18
|
+
return path.resolve(options.diagnosticsDir);
|
|
19
|
+
}
|
|
20
|
+
const envDir = process.env.HORIZON_DIAGNOSTICS_DIR;
|
|
21
|
+
if (envDir && typeof envDir === 'string' && envDir.trim().length > 0) {
|
|
22
|
+
return path.resolve(envDir.trim());
|
|
23
|
+
}
|
|
24
|
+
const home = options.homeDir || process.env.USERPROFILE || process.env.HOME || os.homedir();
|
|
25
|
+
return path.join(home, '.attention-horizon', 'diagnostics');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getHookDiagnosticsFilePath(options = {}) {
|
|
29
|
+
return path.join(getHookDiagnosticsDir(options), HOOK_DIAGNOSTICS_FILENAME);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function sanitizeDiagnosticMessage(rawMessage = '') {
|
|
33
|
+
return String(rawMessage || 'Unknown error')
|
|
34
|
+
.replace(/\r?\n/g, ' ')
|
|
35
|
+
.replace(/[A-Za-z]:[\\/][^\s"']+/g, '<path>')
|
|
36
|
+
.replace(/\/(?:Users|home)\/[^\s"']+/g, '<path>')
|
|
37
|
+
.slice(0, 240);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Records a structured diagnostic event into the bounded in-memory ring buffer (<= 50)
|
|
42
|
+
* and persists bounded JSONL (`<= 50` entries) to `~/.attention-horizon/diagnostics/ga_hook_diagnostics.jsonl`.
|
|
43
|
+
* Never writes to `stdout` and never throws.
|
|
44
|
+
*/
|
|
45
|
+
export function recordHookDiagnostic(source, operation, err, options = {}) {
|
|
46
|
+
const code = err && typeof err === 'object' && err.code ? String(err.code) : null;
|
|
47
|
+
const ignoreCodes = Array.isArray(options.ignoreCodes) ? options.ignoreCodes : [];
|
|
48
|
+
if (code && ignoreCodes.includes(code)) {
|
|
49
|
+
ignoredDiagnosticCount += 1;
|
|
50
|
+
return { recorded: false, ignored: true, code };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const entry = {
|
|
54
|
+
timestamp: new Date().toISOString(),
|
|
55
|
+
pid: process.pid,
|
|
56
|
+
source: String(source || 'unknown').slice(0, 64),
|
|
57
|
+
operation: String(operation || 'unknown').slice(0, 80),
|
|
58
|
+
name: err && typeof err === 'object' && err.name ? String(err.name).slice(0, 48) : 'Error',
|
|
59
|
+
code,
|
|
60
|
+
message: sanitizeDiagnosticMessage(err instanceof Error ? err.message : String(err ?? 'Unknown error'))
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
inMemoryDiagnostics.push(entry);
|
|
64
|
+
while (inMemoryDiagnostics.length > MAX_HOOK_DIAGNOSTIC_ENTRIES) {
|
|
65
|
+
inMemoryDiagnostics.shift();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (options.persist !== false) {
|
|
69
|
+
try {
|
|
70
|
+
const filePath = getHookDiagnosticsFilePath(options);
|
|
71
|
+
let existingLines = [];
|
|
72
|
+
if (fs.existsSync(filePath)) {
|
|
73
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
74
|
+
existingLines = raw
|
|
75
|
+
.split(/\r?\n/)
|
|
76
|
+
.map((l) => l.trim())
|
|
77
|
+
.filter(Boolean);
|
|
78
|
+
}
|
|
79
|
+
existingLines.push(JSON.stringify(entry));
|
|
80
|
+
const bounded = existingLines.slice(-MAX_HOOK_DIAGNOSTIC_ENTRIES);
|
|
81
|
+
atomicWriteFileSync(filePath, `${bounded.join('\n')}\n`, { encoding: 'utf8' });
|
|
82
|
+
} catch (persistErr) {
|
|
83
|
+
// Never throw or write to stdout from diagnostic recorder
|
|
84
|
+
entry.persistFailedCode = persistErr?.code || 'PERSIST_ERR';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { recorded: true, ignored: false, entry };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function getHookDiagnostics() {
|
|
92
|
+
return [...inMemoryDiagnostics];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function getIgnoredHookDiagnosticCount() {
|
|
96
|
+
return ignoredDiagnosticCount;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function clearHookDiagnostics() {
|
|
100
|
+
inMemoryDiagnostics.length = 0;
|
|
101
|
+
ignoredDiagnosticCount = 0;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function readPersistedHookDiagnostics(options = {}) {
|
|
105
|
+
const filePath = getHookDiagnosticsFilePath(options);
|
|
106
|
+
if (!fs.existsSync(filePath)) return [];
|
|
107
|
+
try {
|
|
108
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
109
|
+
const entries = [];
|
|
110
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
111
|
+
const trimmed = line.trim();
|
|
112
|
+
if (!trimmed) continue;
|
|
113
|
+
try {
|
|
114
|
+
const parsed = JSON.parse(trimmed);
|
|
115
|
+
if (parsed && typeof parsed === 'object') entries.push(parsed);
|
|
116
|
+
} catch (parseErr) {
|
|
117
|
+
if (parseErr) {
|
|
118
|
+
// Skip malformed line while preserving valid entries
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return entries.slice(-MAX_HOOK_DIAGNOSTIC_ENTRIES);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
return err ? [] : [];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function getHookDiagnosticSummary(options = {}) {
|
|
129
|
+
const persisted = readPersistedHookDiagnostics(options);
|
|
130
|
+
const bySource = {};
|
|
131
|
+
for (const item of persisted) {
|
|
132
|
+
const key = String(item.source || 'unknown');
|
|
133
|
+
bySource[key] = (bySource[key] || 0) + 1;
|
|
134
|
+
}
|
|
135
|
+
const latest = persisted.length > 0 ? persisted[persisted.length - 1] : null;
|
|
136
|
+
return {
|
|
137
|
+
totalCount: persisted.length,
|
|
138
|
+
inMemoryCount: inMemoryDiagnostics.length,
|
|
139
|
+
bySource,
|
|
140
|
+
latestSource: latest?.source || null,
|
|
141
|
+
latestOperation: latest?.operation || null,
|
|
142
|
+
latestCode: latest?.code || null,
|
|
143
|
+
latestTimestamp: latest?.timestamp || null
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export default {
|
|
148
|
+
MAX_HOOK_DIAGNOSTIC_ENTRIES,
|
|
149
|
+
HOOK_DIAGNOSTICS_FILENAME,
|
|
150
|
+
getHookDiagnosticsDir,
|
|
151
|
+
getHookDiagnosticsFilePath,
|
|
152
|
+
recordHookDiagnostic,
|
|
153
|
+
getHookDiagnostics,
|
|
154
|
+
getIgnoredHookDiagnosticCount,
|
|
155
|
+
clearHookDiagnostics,
|
|
156
|
+
readPersistedHookDiagnostics,
|
|
157
|
+
getHookDiagnosticSummary
|
|
158
|
+
};
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { isSameOrSubPath, canonicalizePath } from './path_canonicalizer.mjs';
|
|
2
|
+
import { evaluateSmartFileReReads, isCompactionCheckpointStep } from './layer5_hygiene.mjs';
|
|
3
|
+
import { resolvePricePerM, resolveModelCeilingDetailed } from './constants.mjs';
|
|
4
|
+
|
|
5
|
+
export const CELL_CAPACITY_TOKENS = 5000;
|
|
6
|
+
export const TOTAL_HORIZON_CELLS = 40;
|
|
7
|
+
export const MAX_HORIZON_TOKENS = 200000;
|
|
8
|
+
|
|
9
|
+
export const CATEGORIES = {
|
|
10
|
+
ANCHOR_CORE: 'ANCHOR_CORE',
|
|
11
|
+
ANCHOR_SCHEMA: 'ANCHOR_SCHEMA',
|
|
12
|
+
MEMORY_KI: 'MEMORY_KI',
|
|
13
|
+
MEMORY_BLEED: 'MEMORY_BLEED',
|
|
14
|
+
ACTIVE_SET: 'ACTIVE_SET',
|
|
15
|
+
STALE_DUP: 'STALE_DUP',
|
|
16
|
+
DIRTY_SNAP: 'DIRTY_SNAP',
|
|
17
|
+
LEGACY_POISON: 'LEGACY_POISON',
|
|
18
|
+
DEAD_ERROR: 'DEAD_ERROR',
|
|
19
|
+
FREE_HEADROOM: 'FREE_HEADROOM'
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const CATEGORY_GLYPHS = {
|
|
23
|
+
ANCHOR_CORE: '█',
|
|
24
|
+
ANCHOR_SCHEMA: '▒',
|
|
25
|
+
MEMORY_KI: '▓',
|
|
26
|
+
MEMORY_BLEED: '▓',
|
|
27
|
+
ACTIVE_SET: '█',
|
|
28
|
+
STALE_DUP: '▒',
|
|
29
|
+
DIRTY_SNAP: '▒',
|
|
30
|
+
LEGACY_POISON: '▒',
|
|
31
|
+
DEAD_ERROR: '▓',
|
|
32
|
+
FREE_HEADROOM: '░'
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Classifies 200,000-token context window into 40 discrete chronological cells.
|
|
37
|
+
*/
|
|
38
|
+
export function classifyHorizonWindow(
|
|
39
|
+
stepEvents = [],
|
|
40
|
+
contextWindowMeta = {},
|
|
41
|
+
workspacePath = '',
|
|
42
|
+
knowledgeItemsMeta = [],
|
|
43
|
+
config = {},
|
|
44
|
+
sensorFlags = {}
|
|
45
|
+
) {
|
|
46
|
+
const normWorkspace = canonicalizePath(workspacePath || '');
|
|
47
|
+
const dirtySet = new Set((sensorFlags.dirtySnapshotPaths || []).map(p => canonicalizePath(p)));
|
|
48
|
+
const foreignBleedKiIds = [];
|
|
49
|
+
|
|
50
|
+
const deprecatedGlobs = config.deprecated_paths || ['**/legacy/**', '**/deprecated/**', '**/v1/**'];
|
|
51
|
+
const isDeprecatedPath = (p) => {
|
|
52
|
+
if (!p) return false;
|
|
53
|
+
const lower = p.toLowerCase();
|
|
54
|
+
return deprecatedGlobs.some(g => {
|
|
55
|
+
const seg = g.replace(/\*/g, '');
|
|
56
|
+
return lower.includes(seg);
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// 1. Identify Foreign KI Bleed vs Valid KI
|
|
61
|
+
let memoryKiTokens = 0;
|
|
62
|
+
let memoryBleedTokens = 0;
|
|
63
|
+
for (const ki of knowledgeItemsMeta) {
|
|
64
|
+
const kiProject = canonicalizePath(ki.projectPath || ki.workspacePath || '');
|
|
65
|
+
const tokens = Math.max(0, Number(ki.tokenCount || 0));
|
|
66
|
+
if (kiProject && normWorkspace && !isSameOrSubPath(normWorkspace, kiProject)) {
|
|
67
|
+
foreignBleedKiIds.push(ki.id || ki.name || 'foreign_ki');
|
|
68
|
+
memoryBleedTokens += tokens;
|
|
69
|
+
} else {
|
|
70
|
+
memoryKiTokens += tokens;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 2. Identify Dead Errors and Smart Stale Duplicates
|
|
75
|
+
const totalSteps = stepEvents.length;
|
|
76
|
+
const smartReReads = evaluateSmartFileReReads(stepEvents);
|
|
77
|
+
let latestPassingCommandStep = -1;
|
|
78
|
+
|
|
79
|
+
for (let i = 0; i < totalSteps; i++) {
|
|
80
|
+
const ev = stepEvents[i];
|
|
81
|
+
if ((ev.toolName === 'run_command' || ev.type === 'COMMAND') && ev.exitCode === 0) {
|
|
82
|
+
latestPassingCommandStep = i;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 3. Classify individual step events into spans
|
|
87
|
+
const spans = [];
|
|
88
|
+
const activeSetPaths = new Set();
|
|
89
|
+
const staleDupPaths = new Set();
|
|
90
|
+
const legacyPoisonPaths = new Set();
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < totalSteps; i++) {
|
|
93
|
+
const ev = stepEvents[i];
|
|
94
|
+
const isRecentTurn = (totalSteps - 1 - i) <= 16; // last ~8 conversational turns
|
|
95
|
+
const isVeryOldChat = (totalSteps - 1 - i) > 25;
|
|
96
|
+
let cat = CATEGORIES.ACTIVE_SET;
|
|
97
|
+
|
|
98
|
+
// Anchor check
|
|
99
|
+
const isCoreAnchor = ev.role === 'system' ||
|
|
100
|
+
isCompactionCheckpointStep(ev) ||
|
|
101
|
+
(ev.targetPath && (
|
|
102
|
+
ev.targetPath.includes('rules/') ||
|
|
103
|
+
ev.targetPath.endsWith('agents.md') ||
|
|
104
|
+
ev.targetPath.endsWith('implementation_plan.md') ||
|
|
105
|
+
ev.targetPath.endsWith('task.md')
|
|
106
|
+
));
|
|
107
|
+
|
|
108
|
+
if (isCoreAnchor) {
|
|
109
|
+
cat = CATEGORIES.ANCHOR_CORE;
|
|
110
|
+
} else if (ev.stepIndex === 0 && (ev.type === 'SCHEMA' || ev.type === 'ANCHOR_SCHEMA')) {
|
|
111
|
+
cat = CATEGORIES.ANCHOR_SCHEMA;
|
|
112
|
+
} else if (ev.targetPath && dirtySet.has(ev.targetPath)) {
|
|
113
|
+
cat = CATEGORIES.DIRTY_SNAP;
|
|
114
|
+
} else if (ev.isDeprecatedContent || (ev.targetPath && isDeprecatedPath(ev.targetPath))) {
|
|
115
|
+
cat = CATEGORIES.LEGACY_POISON;
|
|
116
|
+
if (ev.targetPath) legacyPoisonPaths.add(ev.targetPath);
|
|
117
|
+
} else if (ev.exitCode !== null && ev.exitCode !== 0) {
|
|
118
|
+
// If a later passing command was executed, mark as DEAD_ERROR
|
|
119
|
+
if (latestPassingCommandStep > i) {
|
|
120
|
+
cat = CATEGORIES.DEAD_ERROR;
|
|
121
|
+
} else {
|
|
122
|
+
cat = CATEGORIES.ACTIVE_SET;
|
|
123
|
+
}
|
|
124
|
+
} else if (ev.targetPath && (ev.toolName === 'view_file' || ev.toolName === 'read_file')) {
|
|
125
|
+
if (smartReReads.wasteTargetByStep.has(i)) {
|
|
126
|
+
cat = CATEGORIES.STALE_DUP;
|
|
127
|
+
staleDupPaths.add(ev.targetPath);
|
|
128
|
+
} else if (isRecentTurn) {
|
|
129
|
+
cat = CATEGORIES.ACTIVE_SET;
|
|
130
|
+
activeSetPaths.add(ev.targetPath);
|
|
131
|
+
} else {
|
|
132
|
+
cat = CATEGORIES.STALE_DUP;
|
|
133
|
+
staleDupPaths.add(ev.targetPath);
|
|
134
|
+
}
|
|
135
|
+
} else if (isVeryOldChat && ev.role !== 'system') {
|
|
136
|
+
cat = CATEGORIES.STALE_DUP;
|
|
137
|
+
} else if (isRecentTurn) {
|
|
138
|
+
cat = CATEGORIES.ACTIVE_SET;
|
|
139
|
+
if (ev.targetPath) activeSetPaths.add(ev.targetPath);
|
|
140
|
+
} else {
|
|
141
|
+
cat = CATEGORIES.STALE_DUP;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
spans.push({
|
|
145
|
+
category: cat,
|
|
146
|
+
tokens: ev.tokenEstimate || 100,
|
|
147
|
+
path: ev.targetPath,
|
|
148
|
+
stepIndex: ev.stepIndex
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Insert Knowledge Items
|
|
153
|
+
if (memoryKiTokens > 0) {
|
|
154
|
+
spans.unshift({ category: CATEGORIES.MEMORY_KI, tokens: memoryKiTokens, path: null, stepIndex: 0 });
|
|
155
|
+
}
|
|
156
|
+
if (memoryBleedTokens > 0) {
|
|
157
|
+
spans.unshift({ category: CATEGORIES.MEMORY_BLEED, tokens: memoryBleedTokens, path: null, stepIndex: 0 });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Total active tokens
|
|
161
|
+
const rawSumTokens = spans.reduce((sum, s) => sum + s.tokens, 0);
|
|
162
|
+
const totalInputTokens = Math.max(
|
|
163
|
+
contextWindowMeta.total_input_tokens || 0,
|
|
164
|
+
rawSumTokens
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// 4. Populate 40 discrete cells
|
|
168
|
+
const cells = [];
|
|
169
|
+
const tokenBreakdown = {
|
|
170
|
+
[CATEGORIES.ANCHOR_CORE]: 0,
|
|
171
|
+
[CATEGORIES.ANCHOR_SCHEMA]: 0,
|
|
172
|
+
[CATEGORIES.MEMORY_KI]: 0,
|
|
173
|
+
[CATEGORIES.MEMORY_BLEED]: 0,
|
|
174
|
+
[CATEGORIES.ACTIVE_SET]: 0,
|
|
175
|
+
[CATEGORIES.STALE_DUP]: 0,
|
|
176
|
+
[CATEGORIES.DIRTY_SNAP]: 0,
|
|
177
|
+
[CATEGORIES.LEGACY_POISON]: 0,
|
|
178
|
+
[CATEGORIES.DEAD_ERROR]: 0,
|
|
179
|
+
[CATEGORIES.FREE_HEADROOM]: 0
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
for (const s of spans) {
|
|
183
|
+
tokenBreakdown[s.category] = (tokenBreakdown[s.category] || 0) + s.tokens;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Map spans into 40 buckets of 5,000 tokens
|
|
187
|
+
let currentSpanIdx = 0;
|
|
188
|
+
let currentSpanRemaining = spans[0]?.tokens || 0;
|
|
189
|
+
|
|
190
|
+
for (let cellIdx = 0; cellIdx < TOTAL_HORIZON_CELLS; cellIdx++) {
|
|
191
|
+
const cellStart = cellIdx * CELL_CAPACITY_TOKENS;
|
|
192
|
+
const cellEnd = cellStart + CELL_CAPACITY_TOKENS;
|
|
193
|
+
|
|
194
|
+
if (cellStart >= totalInputTokens) {
|
|
195
|
+
// Cell is beyond context, mark as FREE_HEADROOM
|
|
196
|
+
tokenBreakdown[CATEGORIES.FREE_HEADROOM] += CELL_CAPACITY_TOKENS;
|
|
197
|
+
cells.push({
|
|
198
|
+
index: cellIdx,
|
|
199
|
+
tokenStart: cellStart,
|
|
200
|
+
tokenEnd: cellEnd,
|
|
201
|
+
dominantCategory: CATEGORIES.FREE_HEADROOM,
|
|
202
|
+
glyph: CATEGORY_GLYPHS.FREE_HEADROOM,
|
|
203
|
+
totalTokens: 0,
|
|
204
|
+
paths: []
|
|
205
|
+
});
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const cellTally = {};
|
|
210
|
+
const cellPaths = new Set();
|
|
211
|
+
let needed = Math.min(CELL_CAPACITY_TOKENS, Math.max(0, totalInputTokens - cellStart));
|
|
212
|
+
|
|
213
|
+
while (needed > 0 && currentSpanIdx < spans.length) {
|
|
214
|
+
const span = spans[currentSpanIdx];
|
|
215
|
+
const alloc = Math.min(needed, currentSpanRemaining);
|
|
216
|
+
cellTally[span.category] = (cellTally[span.category] || 0) + alloc;
|
|
217
|
+
if (span.path) cellPaths.add(span.path);
|
|
218
|
+
|
|
219
|
+
needed -= alloc;
|
|
220
|
+
currentSpanRemaining -= alloc;
|
|
221
|
+
if (currentSpanRemaining <= 0) {
|
|
222
|
+
currentSpanIdx++;
|
|
223
|
+
currentSpanRemaining = spans[currentSpanIdx]?.tokens || 0;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Find dominant category for this cell
|
|
228
|
+
let dominant = CATEGORIES.FREE_HEADROOM;
|
|
229
|
+
let maxTokensInCell = -1;
|
|
230
|
+
for (const [c, count] of Object.entries(cellTally)) {
|
|
231
|
+
if (count > maxTokensInCell) {
|
|
232
|
+
maxTokensInCell = count;
|
|
233
|
+
dominant = c;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
cells.push({
|
|
238
|
+
index: cellIdx,
|
|
239
|
+
tokenStart: cellStart,
|
|
240
|
+
tokenEnd: cellEnd,
|
|
241
|
+
dominantCategory: dominant,
|
|
242
|
+
glyph: CATEGORY_GLYPHS[dominant] || CATEGORY_GLYPHS.FREE_HEADROOM,
|
|
243
|
+
totalTokens: CELL_CAPACITY_TOKENS - needed,
|
|
244
|
+
paths: Array.from(cellPaths).slice(0, 3)
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 5. Check Amputation Risk
|
|
249
|
+
const amputationThreshold = config.amputation_token_threshold || 140000;
|
|
250
|
+
const inAmputationZone = totalInputTokens >= amputationThreshold;
|
|
251
|
+
const earlyCellsContainAnchor = cells.slice(0, 8).some(c => c.dominantCategory === CATEGORIES.ANCHOR_CORE);
|
|
252
|
+
const amputationRisk = inAmputationZone && (earlyCellsContainAnchor || totalInputTokens >= 180000);
|
|
253
|
+
|
|
254
|
+
// 6. Signal Purity & Sludge Tax
|
|
255
|
+
const pureTokens = tokenBreakdown[CATEGORIES.ANCHOR_CORE] +
|
|
256
|
+
tokenBreakdown[CATEGORIES.MEMORY_KI] +
|
|
257
|
+
tokenBreakdown[CATEGORIES.ACTIVE_SET];
|
|
258
|
+
|
|
259
|
+
const signalPurityPct = totalInputTokens > 0
|
|
260
|
+
? Number(Math.min(100, Math.max(0, (pureTokens / totalInputTokens) * 100)).toFixed(1))
|
|
261
|
+
: 100.0;
|
|
262
|
+
|
|
263
|
+
const sludgeTokens = tokenBreakdown[CATEGORIES.STALE_DUP] +
|
|
264
|
+
tokenBreakdown[CATEGORIES.DEAD_ERROR] +
|
|
265
|
+
tokenBreakdown[CATEGORIES.ANCHOR_SCHEMA];
|
|
266
|
+
|
|
267
|
+
const activeModel = contextWindowMeta.modelName || contextWindowMeta.modelId || 'gemini-3.8-flash';
|
|
268
|
+
const ceilingDetails = resolveModelCeilingDetailed(activeModel, contextWindowMeta, {
|
|
269
|
+
useLegacyDefault: false,
|
|
270
|
+
config
|
|
271
|
+
});
|
|
272
|
+
const modelMaxTokens = Number(contextWindowMeta.maxTokens) > 0
|
|
273
|
+
? Number(contextWindowMeta.maxTokens)
|
|
274
|
+
: ceilingDetails.ceilingTokens;
|
|
275
|
+
const pricePer1M = resolvePricePerM(activeModel, config);
|
|
276
|
+
const sludgeTaxUsdPerTurn = Number(((sludgeTokens / 1_000_000) * pricePer1M).toFixed(4));
|
|
277
|
+
|
|
278
|
+
let sumCacheRead = 0;
|
|
279
|
+
let sumPromptEnvelope = 0;
|
|
280
|
+
for (const ev of stepEvents) {
|
|
281
|
+
const cr = Math.max(0, Number(ev?.cacheReadTokens || 0));
|
|
282
|
+
const cc = Math.max(0, Number(ev?.cacheCreationTokens || 0));
|
|
283
|
+
const inp = Math.max(0, Number(ev?.inputTokens || 0));
|
|
284
|
+
sumCacheRead += cr;
|
|
285
|
+
sumPromptEnvelope += cr + cc + inp;
|
|
286
|
+
}
|
|
287
|
+
const derivedCacheHitPct = sumPromptEnvelope > 0
|
|
288
|
+
? Math.min(100, Math.round((sumCacheRead / sumPromptEnvelope) * 100))
|
|
289
|
+
: 0;
|
|
290
|
+
const cacheHitPct = typeof contextWindowMeta.cacheHitPct === 'number' && Number.isFinite(contextWindowMeta.cacheHitPct)
|
|
291
|
+
? Math.max(0, Math.min(100, Math.round(contextWindowMeta.cacheHitPct)))
|
|
292
|
+
: derivedCacheHitPct;
|
|
293
|
+
const ttlRemainingSec = typeof contextWindowMeta.ttlRemainingSec === 'number' && Number.isFinite(contextWindowMeta.ttlRemainingSec)
|
|
294
|
+
? Math.max(0, Math.round(contextWindowMeta.ttlRemainingSec))
|
|
295
|
+
: 0;
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
totalInputTokens,
|
|
299
|
+
model: {
|
|
300
|
+
id: activeModel,
|
|
301
|
+
maxTokens: modelMaxTokens,
|
|
302
|
+
ceilingSource: ceilingDetails.source
|
|
303
|
+
},
|
|
304
|
+
cells,
|
|
305
|
+
tokenBreakdown,
|
|
306
|
+
metrics: {
|
|
307
|
+
signalPurityPct,
|
|
308
|
+
sludgeTaxUsdPerTurn,
|
|
309
|
+
pureTokens,
|
|
310
|
+
sludgeTokens,
|
|
311
|
+
cacheHitPct,
|
|
312
|
+
ttlRemainingSec
|
|
313
|
+
},
|
|
314
|
+
flags: {
|
|
315
|
+
amputationRisk,
|
|
316
|
+
inAmputationZone,
|
|
317
|
+
hasForeignBleed: foreignBleedKiIds.length > 0
|
|
318
|
+
},
|
|
319
|
+
fileState: {
|
|
320
|
+
activeSetPaths: Array.from(activeSetPaths),
|
|
321
|
+
staleDupPaths: Array.from(staleDupPaths),
|
|
322
|
+
legacyPoisonPaths: Array.from(legacyPoisonPaths),
|
|
323
|
+
foreignBleedKiIds
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
}
|