ctxtop 0.0.0-stage → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.antigravity-plugin/plugin.json +36 -0
- package/.claude-plugin/plugin.json +18 -0
- package/CHECKSUMS.sha256 +149 -0
- package/LICENSE +27 -0
- package/README.md +330 -3
- 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 +145 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1474 -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 +114 -0
- package/config/modelCatalog.json +620 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +262 -0
- package/hooks/handlers/hookIo.mjs +255 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-tool-use.mjs +265 -0
- package/hooks/handlers/pre-model-switch.mjs +249 -0
- package/hooks/handlers/pre-tool-use.mjs +365 -0
- package/hooks/handlers/stop-gate.mjs +240 -0
- package/hooks/handlers/user-prompt-submit.mjs +143 -0
- package/hooks/hooks.json +81 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +17 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +687 -0
- package/hooks/session_start.mjs +14 -0
- package/hooks/stop.mjs +13 -0
- package/hooks.json +56 -0
- package/package.json +103 -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 +2002 -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 +42 -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 +79 -0
- package/src/adapters/claude-code/index.mjs +155 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +686 -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 +620 -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/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 +1800 -0
- package/src/core/detectors_det01_to_16.mjs +1474 -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 +966 -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 +1996 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +310 -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/sanitizer.mjs +121 -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/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 +720 -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/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +457 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +203 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +768 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1009 -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 +622 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +351 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +170 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +319 -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/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1035 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +480 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +603 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +301 -0
- package/src/core-isomorphic/index.js +26 -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 +1132 -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 +1181 -0
- package/src/core-isomorphic/ui/modViews.mjs +1478 -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/doctorEngine.mjs +203 -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 +110 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +9 -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 +369 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +419 -0
- package/src/hooks/pre_invocation.mjs +1015 -0
- package/src/hooks/session_start.mjs +161 -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/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +11389 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { SESSION_RISK_THRESHOLDS } from '../constants.mjs';
|
|
4
|
+
import { atomicWriteFileSync } from '../atomic_io.mjs';
|
|
5
|
+
import { evaluateDet10IdleCacheWatchdog } from '../shared/detectors.mjs';
|
|
6
|
+
import { createStateBaton, formatColdResumeBatonPrompt } from '../shared/handoff_schema.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Phase 1.3: 300s Implicit Cache TTL Watchdog (`src/core/telemetry/cache_ttl_watchdog.mjs`)
|
|
10
|
+
*
|
|
11
|
+
* Strictly read-only telemetry clock with zero side effects.
|
|
12
|
+
* Tracks elapsed idle time since the last completed Language Server turn when `cachedTokens >= 50000`:
|
|
13
|
+
* - Emits `TTL_WARNING_PULSE` at `T-45s` (`255s` elapsed)
|
|
14
|
+
* - Emits `TTL_PRE_SNAPSHOT` at `T-10s` (`290s` elapsed) so `handoff_hydrator.mjs` can pre-stage a disk bundle
|
|
15
|
+
* - Emits `TTL_EXPIRED_COLD_RISK` at `T-0s` (`300s` elapsed)
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export const CACHE_TTL_WATCHDOG_CONSTANTS = Object.freeze({
|
|
19
|
+
TTL_WINDOW_SECONDS: 300,
|
|
20
|
+
WARM_CACHE_MIN_TOKENS: SESSION_RISK_THRESHOLDS.WARM_CACHE_LOCK_TOKENS,
|
|
21
|
+
DET10_COLD_RESUME_MIN_CACHED_TOKENS: 25000,
|
|
22
|
+
DET10_AMBER_PULSE_REMAINING_SEC: 45,
|
|
23
|
+
WARNING_PULSE_ELAPSED_SEC: 255, // T-45s
|
|
24
|
+
AUTO_PILOT_KEEPALIVE_ELAPSED_SEC: 270, // T-30s
|
|
25
|
+
PRE_SNAPSHOT_ELAPSED_SEC: 290, // T-10s
|
|
26
|
+
EXPIRED_ELAPSED_SEC: 300 // T-0s
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export function formatTtlCountdown(ttlRemainingSeconds = 0) {
|
|
30
|
+
const rem = Math.max(0, Math.floor(Number(ttlRemainingSeconds) || 0));
|
|
31
|
+
const mins = Math.floor(rem / 60);
|
|
32
|
+
const secs = rem % 60;
|
|
33
|
+
return `${mins}m ${String(secs).padStart(2, '0')}s`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class CacheTtlWatchdog extends EventEmitter {
|
|
37
|
+
constructor(options = {}) {
|
|
38
|
+
super();
|
|
39
|
+
this.ttlWindowSeconds = Number(options.ttlWindowSeconds || CACHE_TTL_WATCHDOG_CONSTANTS.TTL_WINDOW_SECONDS);
|
|
40
|
+
this.warmCacheMinTokens = Number(options.warmCacheMinTokens || CACHE_TTL_WATCHDOG_CONSTANTS.WARM_CACHE_MIN_TOKENS);
|
|
41
|
+
this.sessionId = String(options.sessionId || 'default');
|
|
42
|
+
this.cachedTokens = Math.max(0, Number(options.cachedTokens || 0));
|
|
43
|
+
this.flightMode = String(options.flightMode || 'COACH').toUpperCase();
|
|
44
|
+
this.workspaceEditsOccurred = Boolean(options.workspaceEditsOccurred);
|
|
45
|
+
this.workspaceDir = options.workspaceDir || null;
|
|
46
|
+
this.batonState = options.batonState || null;
|
|
47
|
+
this.lastCompletedTurnTimestampMs = Number(options.lastCompletedTurnTimestampMs || Date.now());
|
|
48
|
+
this.emittedFlags = {
|
|
49
|
+
warningPulse: false,
|
|
50
|
+
det10AmberBatonPulse: false,
|
|
51
|
+
autoPilotKeepAlive: false,
|
|
52
|
+
preSnapshot: false,
|
|
53
|
+
expiredColdRisk: false
|
|
54
|
+
};
|
|
55
|
+
this._intervalHandle = null;
|
|
56
|
+
|
|
57
|
+
if (typeof options.onPreSnapshot === 'function') {
|
|
58
|
+
this.on('TTL_PRE_SNAPSHOT', options.onPreSnapshot);
|
|
59
|
+
}
|
|
60
|
+
if (typeof options.onKeepAlive === 'function') {
|
|
61
|
+
this.on('TTL_AUTO_PILOT_KEEPALIVE', options.onKeepAlive);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setFlightMode(mode = 'COACH') {
|
|
66
|
+
const norm = String(mode || 'COACH').toUpperCase();
|
|
67
|
+
this.flightMode = (norm === 'AUTOPILOT' ? 'AUTO_PILOT' : norm);
|
|
68
|
+
return this.flightMode;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
isPolling() {
|
|
72
|
+
return Boolean(this._intervalHandle);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Resets the 300s countdown clock when a Language Server turn completes.
|
|
77
|
+
*/
|
|
78
|
+
recordTurnCompleted({ sessionId = this.sessionId, cachedTokens = this.cachedTokens, timestampMs = Date.now(), flightMode } = {}) {
|
|
79
|
+
this.sessionId = String(sessionId || this.sessionId);
|
|
80
|
+
this.cachedTokens = Math.max(0, Number(cachedTokens || 0));
|
|
81
|
+
if (flightMode !== undefined) {
|
|
82
|
+
this.setFlightMode(flightMode);
|
|
83
|
+
}
|
|
84
|
+
this.lastCompletedTurnTimestampMs = Number(timestampMs || Date.now());
|
|
85
|
+
this.emittedFlags = {
|
|
86
|
+
warningPulse: false,
|
|
87
|
+
det10AmberBatonPulse: false,
|
|
88
|
+
autoPilotKeepAlive: false,
|
|
89
|
+
preSnapshot: false,
|
|
90
|
+
expiredColdRisk: false
|
|
91
|
+
};
|
|
92
|
+
return this.getStatus(0);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Evaluates a specific `elapsedSeconds` value (or computes it from `nowMs`) and emits threshold events.
|
|
97
|
+
*/
|
|
98
|
+
evaluateElapsed(elapsedSecondsInput = null, overrides = {}) {
|
|
99
|
+
if (overrides.cachedTokens !== undefined) {
|
|
100
|
+
this.cachedTokens = Math.max(0, Number(overrides.cachedTokens || 0));
|
|
101
|
+
}
|
|
102
|
+
if (overrides.sessionId !== undefined) {
|
|
103
|
+
this.sessionId = String(overrides.sessionId);
|
|
104
|
+
}
|
|
105
|
+
if (overrides.flightMode !== undefined) {
|
|
106
|
+
this.setFlightMode(overrides.flightMode);
|
|
107
|
+
}
|
|
108
|
+
if (overrides.workspaceEditsOccurred !== undefined) {
|
|
109
|
+
this.workspaceEditsOccurred = Boolean(overrides.workspaceEditsOccurred);
|
|
110
|
+
}
|
|
111
|
+
if (overrides.workspaceDir !== undefined) {
|
|
112
|
+
this.workspaceDir = overrides.workspaceDir;
|
|
113
|
+
}
|
|
114
|
+
if (overrides.batonState !== undefined) {
|
|
115
|
+
this.batonState = overrides.batonState;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const elapsedSeconds = elapsedSecondsInput !== null && elapsedSecondsInput !== undefined
|
|
119
|
+
? Math.max(0, Number(elapsedSecondsInput))
|
|
120
|
+
: Math.max(0, (Number(overrides.nowMs || Date.now()) - this.lastCompletedTurnTimestampMs) / 1000);
|
|
121
|
+
|
|
122
|
+
const ttlRemainingSeconds = Math.max(0, Number((this.ttlWindowSeconds - elapsedSeconds).toFixed(1)));
|
|
123
|
+
const isArmed = this.cachedTokens >= this.warmCacheMinTokens;
|
|
124
|
+
const eventsEmitted = [];
|
|
125
|
+
|
|
126
|
+
// Rank 8 (DET-10): Est. 5m Implicit Cache label + Amber HUD pulse + ~300-token Cold-Resume State Baton auto-flush at T <= 45s when cachedTokens >= 25000 and workspace edits occurred (zero network sockets, zero automated prompt chaining)
|
|
127
|
+
const det10 = evaluateDet10IdleCacheWatchdog({
|
|
128
|
+
elapsedSeconds,
|
|
129
|
+
ttlWindowSeconds: this.ttlWindowSeconds,
|
|
130
|
+
cachedTokens: this.cachedTokens,
|
|
131
|
+
workspaceEditsOccurred: this.workspaceEditsOccurred
|
|
132
|
+
});
|
|
133
|
+
let coldResumeBatonPath = null;
|
|
134
|
+
let coldResumePrompt = null;
|
|
135
|
+
if (det10.shouldFlushColdResumeBaton && !this.emittedFlags.det10AmberBatonPulse) {
|
|
136
|
+
this.emittedFlags.det10AmberBatonPulse = true;
|
|
137
|
+
const batonObj = createStateBaton({
|
|
138
|
+
sessionId: this.sessionId,
|
|
139
|
+
bandLabel: 'det10_ttl_45s',
|
|
140
|
+
currentTokens: this.cachedTokens,
|
|
141
|
+
...(this.batonState || {})
|
|
142
|
+
});
|
|
143
|
+
coldResumePrompt = formatColdResumeBatonPrompt(batonObj);
|
|
144
|
+
if (this.workspaceDir && overrides.writeToDisk !== false) {
|
|
145
|
+
const cacheDir = path.join(this.workspaceDir, '.agents', 'cache');
|
|
146
|
+
const jsonPath = path.join(cacheDir, 'state_baton.json');
|
|
147
|
+
const mdPath = path.join(cacheDir, 'cold_resume_baton.md');
|
|
148
|
+
atomicWriteFileSync(jsonPath, JSON.stringify(batonObj, null, 2) + '\n', { encoding: 'utf8' });
|
|
149
|
+
atomicWriteFileSync(mdPath, coldResumePrompt + '\n', { encoding: 'utf8' });
|
|
150
|
+
coldResumeBatonPath = mdPath;
|
|
151
|
+
}
|
|
152
|
+
eventsEmitted.push('DET10_AMBER_BATON_PULSE');
|
|
153
|
+
this.emit('DET10_AMBER_BATON_PULSE', {
|
|
154
|
+
event: 'DET10_AMBER_BATON_PULSE',
|
|
155
|
+
detectorId: 'DET-10',
|
|
156
|
+
sessionId: this.sessionId,
|
|
157
|
+
cachedTokens: this.cachedTokens,
|
|
158
|
+
remainingSeconds: det10.remainingSeconds,
|
|
159
|
+
label: det10.label,
|
|
160
|
+
coldResumeBatonPath,
|
|
161
|
+
coldResumePrompt
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (isArmed) {
|
|
166
|
+
if (elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.WARNING_PULSE_ELAPSED_SEC && !this.emittedFlags.warningPulse) {
|
|
167
|
+
this.emittedFlags.warningPulse = true;
|
|
168
|
+
const payload = {
|
|
169
|
+
event: 'TTL_WARNING_PULSE',
|
|
170
|
+
flightMode: this.flightMode,
|
|
171
|
+
sessionId: this.sessionId,
|
|
172
|
+
cachedTokens: this.cachedTokens,
|
|
173
|
+
elapsedSeconds: Math.round(elapsedSeconds),
|
|
174
|
+
ttlRemainingSeconds: Math.max(0, this.ttlWindowSeconds - Math.round(elapsedSeconds)),
|
|
175
|
+
formattedCountdown: formatTtlCountdown(this.ttlWindowSeconds - elapsedSeconds)
|
|
176
|
+
};
|
|
177
|
+
eventsEmitted.push('TTL_WARNING_PULSE');
|
|
178
|
+
this.emit('TTL_WARNING_PULSE', payload);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (
|
|
182
|
+
this.flightMode === 'AUTO_PILOT' &&
|
|
183
|
+
elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.AUTO_PILOT_KEEPALIVE_ELAPSED_SEC &&
|
|
184
|
+
!this.emittedFlags.autoPilotKeepAlive
|
|
185
|
+
) {
|
|
186
|
+
this.emittedFlags.autoPilotKeepAlive = true;
|
|
187
|
+
const payload = {
|
|
188
|
+
event: 'TTL_AUTO_PILOT_KEEPALIVE',
|
|
189
|
+
flightMode: this.flightMode,
|
|
190
|
+
sessionId: this.sessionId,
|
|
191
|
+
cachedTokens: this.cachedTokens,
|
|
192
|
+
elapsedSeconds: Math.round(elapsedSeconds),
|
|
193
|
+
ttlRemainingSeconds: Math.max(0, this.ttlWindowSeconds - Math.round(elapsedSeconds)),
|
|
194
|
+
formattedCountdown: formatTtlCountdown(this.ttlWindowSeconds - elapsedSeconds)
|
|
195
|
+
};
|
|
196
|
+
eventsEmitted.push('TTL_AUTO_PILOT_KEEPALIVE');
|
|
197
|
+
this.emit('TTL_AUTO_PILOT_KEEPALIVE', payload);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.PRE_SNAPSHOT_ELAPSED_SEC && !this.emittedFlags.preSnapshot) {
|
|
201
|
+
this.emittedFlags.preSnapshot = true;
|
|
202
|
+
const payload = {
|
|
203
|
+
event: 'TTL_PRE_SNAPSHOT',
|
|
204
|
+
flightMode: this.flightMode,
|
|
205
|
+
sessionId: this.sessionId,
|
|
206
|
+
cachedTokens: this.cachedTokens,
|
|
207
|
+
elapsedSeconds: Math.round(elapsedSeconds),
|
|
208
|
+
ttlRemainingSeconds: Math.max(0, this.ttlWindowSeconds - Math.round(elapsedSeconds)),
|
|
209
|
+
formattedCountdown: formatTtlCountdown(this.ttlWindowSeconds - elapsedSeconds)
|
|
210
|
+
};
|
|
211
|
+
eventsEmitted.push('TTL_PRE_SNAPSHOT');
|
|
212
|
+
this.emit('TTL_PRE_SNAPSHOT', payload);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.EXPIRED_ELAPSED_SEC && !this.emittedFlags.expiredColdRisk) {
|
|
216
|
+
this.emittedFlags.expiredColdRisk = true;
|
|
217
|
+
const payload = {
|
|
218
|
+
event: 'TTL_EXPIRED_COLD_RISK',
|
|
219
|
+
flightMode: this.flightMode,
|
|
220
|
+
sessionId: this.sessionId,
|
|
221
|
+
cachedTokens: this.cachedTokens,
|
|
222
|
+
elapsedSeconds: Math.round(elapsedSeconds),
|
|
223
|
+
ttlRemainingSeconds: 0,
|
|
224
|
+
formattedCountdown: '0m 00s'
|
|
225
|
+
};
|
|
226
|
+
eventsEmitted.push('TTL_EXPIRED_COLD_RISK');
|
|
227
|
+
this.emit('TTL_EXPIRED_COLD_RISK', payload);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
sessionId: this.sessionId,
|
|
233
|
+
flightMode: this.flightMode,
|
|
234
|
+
polling: this.isPolling(),
|
|
235
|
+
isArmed,
|
|
236
|
+
cachedTokens: this.cachedTokens,
|
|
237
|
+
elapsedSeconds: Math.round(elapsedSeconds),
|
|
238
|
+
ttlRemainingSeconds: Math.round(ttlRemainingSeconds),
|
|
239
|
+
formattedCountdown: formatTtlCountdown(ttlRemainingSeconds),
|
|
240
|
+
implicitCacheLabel: det10.label,
|
|
241
|
+
pulseAmber: det10.pulseAmber,
|
|
242
|
+
coldResumeBatonFlushed: this.emittedFlags.det10AmberBatonPulse,
|
|
243
|
+
coldResumeBatonPath,
|
|
244
|
+
coldResumePrompt,
|
|
245
|
+
keepAliveTriggered: this.emittedFlags.autoPilotKeepAlive,
|
|
246
|
+
state: !isArmed
|
|
247
|
+
? 'BELOW_WARM_THRESHOLD'
|
|
248
|
+
: (elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.EXPIRED_ELAPSED_SEC
|
|
249
|
+
? 'TTL_EXPIRED_COLD_RISK'
|
|
250
|
+
: (elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.PRE_SNAPSHOT_ELAPSED_SEC
|
|
251
|
+
? 'TTL_PRE_SNAPSHOT'
|
|
252
|
+
: (this.flightMode === 'AUTO_PILOT' && elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.AUTO_PILOT_KEEPALIVE_ELAPSED_SEC
|
|
253
|
+
? 'TTL_AUTO_PILOT_KEEPALIVE'
|
|
254
|
+
: (elapsedSeconds >= CACHE_TTL_WATCHDOG_CONSTANTS.WARNING_PULSE_ELAPSED_SEC
|
|
255
|
+
? 'TTL_WARNING_PULSE'
|
|
256
|
+
: 'WARM_ACTIVE')))),
|
|
257
|
+
eventsEmitted
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
getStatus(elapsedSecondsInput = null) {
|
|
262
|
+
return this.evaluateElapsed(elapsedSecondsInput);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
startPolling(intervalMs = 1000) {
|
|
266
|
+
this.stopPolling();
|
|
267
|
+
this._intervalHandle = setInterval(() => {
|
|
268
|
+
this.evaluateElapsed();
|
|
269
|
+
}, intervalMs);
|
|
270
|
+
if (this._intervalHandle && typeof this._intervalHandle.unref === 'function') {
|
|
271
|
+
this._intervalHandle.unref();
|
|
272
|
+
}
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
stopPolling() {
|
|
277
|
+
if (this._intervalHandle) {
|
|
278
|
+
clearInterval(this._intervalHandle);
|
|
279
|
+
this._intervalHandle = null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export default CacheTtlWatchdog;
|