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,1055 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/config/horizonConfig.mjs
|
|
3
|
+
* Unified Cross-Harness Configuration & Per-Detector Override Engine (`.attention-horizon.json`).
|
|
4
|
+
*
|
|
5
|
+
* Shared across `attention-horizon-claude` and `attention-horizon-antigravity` so developers
|
|
6
|
+
* can individually configure any of the 20 circuit breakers (`DET-01`..`DET-20` or semantic slugs
|
|
7
|
+
* -> `off` | `warn` | `block`), default `flightMode` (`COACH` | `AUTO_PILOT` | `SILENT` | `OFF`),
|
|
8
|
+
* and threshold knobs at the project (`.attention-horizon.json`) or user
|
|
9
|
+
* (`~/.attention-horizon/config.json`) level without editing source code.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
path,
|
|
14
|
+
getCwd,
|
|
15
|
+
getHomeDir,
|
|
16
|
+
unlinkSafe,
|
|
17
|
+
readJsonSafe,
|
|
18
|
+
writeJsonAtomic
|
|
19
|
+
} from '../utils/runtime.mjs';
|
|
20
|
+
export {
|
|
21
|
+
LEGACY_GA_V3_ID_MAP,
|
|
22
|
+
migrateLegacyHorizonConfig
|
|
23
|
+
} from '../catalog/detectorCatalog.mjs';
|
|
24
|
+
|
|
25
|
+
export const CANONICAL_DETECTOR_IDS = Object.freeze([
|
|
26
|
+
'DET-01',
|
|
27
|
+
'DET-02',
|
|
28
|
+
'DET-03',
|
|
29
|
+
'DET-04',
|
|
30
|
+
'DET-05',
|
|
31
|
+
'DET-06',
|
|
32
|
+
'DET-07',
|
|
33
|
+
'DET-08',
|
|
34
|
+
'DET-09',
|
|
35
|
+
'DET-10',
|
|
36
|
+
'DET-11',
|
|
37
|
+
'DET-12',
|
|
38
|
+
'DET-13',
|
|
39
|
+
'DET-14',
|
|
40
|
+
'DET-15',
|
|
41
|
+
'DET-16',
|
|
42
|
+
'DET-17',
|
|
43
|
+
'DET-18',
|
|
44
|
+
'DET-19',
|
|
45
|
+
'DET-20',
|
|
46
|
+
'DET-21',
|
|
47
|
+
'DET-22',
|
|
48
|
+
'DET-23',
|
|
49
|
+
'DET-24',
|
|
50
|
+
'DET-25',
|
|
51
|
+
'DET-26',
|
|
52
|
+
'DET-27',
|
|
53
|
+
'DET-28',
|
|
54
|
+
'DET-29',
|
|
55
|
+
'DET-30',
|
|
56
|
+
'DET-31',
|
|
57
|
+
'DET-32'
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
export const RING0_SECURITY_DETECTOR_IDS = Object.freeze([
|
|
61
|
+
'DET-01',
|
|
62
|
+
'DET-02',
|
|
63
|
+
'DET-18',
|
|
64
|
+
'DET-19'
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Canonical primary semantic slug per Claude Code `DET-01..DET-32` (`ISS-052`).
|
|
69
|
+
*/
|
|
70
|
+
export const DETECTOR_SEMANTIC_SLUGS = Object.freeze({
|
|
71
|
+
'DET-01': 'git-shell-safety',
|
|
72
|
+
'DET-02': 'secret-boundary',
|
|
73
|
+
'DET-03': 'protected-files',
|
|
74
|
+
'DET-04': 'retry-churn',
|
|
75
|
+
'DET-05': 'diff-thrashing',
|
|
76
|
+
'DET-06': 'blind-search',
|
|
77
|
+
'DET-07': 'output-bomb',
|
|
78
|
+
'DET-08': 'cache-gravity-hold',
|
|
79
|
+
'DET-09': 'prefix-mutation-fence',
|
|
80
|
+
'DET-10': 'ttl-watchdog',
|
|
81
|
+
'DET-11': 'context-rot',
|
|
82
|
+
'DET-12': 'haiku-pre-cliff',
|
|
83
|
+
'DET-13': 'mcp-schema-bloat',
|
|
84
|
+
'DET-14': 'premature-stop',
|
|
85
|
+
'DET-15': 'batched-formatter',
|
|
86
|
+
'DET-16': 'subagent-recursion',
|
|
87
|
+
'DET-17': 'auto-compact-deferral',
|
|
88
|
+
'DET-18': 'untrusted-settings',
|
|
89
|
+
'DET-19': 'subagent-ipi',
|
|
90
|
+
'DET-20': 'scope-lock',
|
|
91
|
+
'DET-21': 'thinking-drift-loop',
|
|
92
|
+
'DET-22': 'file-shrinkage-rapid',
|
|
93
|
+
'DET-23': 'empty-catch-block',
|
|
94
|
+
'DET-24': 'high-turn-degradation',
|
|
95
|
+
'DET-25': 'progressive-checkpoint',
|
|
96
|
+
'DET-26': 'prompt-decomposer',
|
|
97
|
+
'DET-27': 'rule-file-bloat',
|
|
98
|
+
'DET-28': 'subagent-phantom',
|
|
99
|
+
'DET-29': 'reflector-queue',
|
|
100
|
+
'DET-30': 'thinking-drift-loop',
|
|
101
|
+
'DET-31': 'worktree-merge-conflict',
|
|
102
|
+
'DET-32': 'semantic-drift-guard'
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Primary slug + accepted cross-edition semantic aliases per Claude Code `DET-01..DET-32`.
|
|
107
|
+
*/
|
|
108
|
+
export const DETECTOR_SEMANTIC_ALIASES = Object.freeze({
|
|
109
|
+
'DET-01': Object.freeze(['git-shell-safety', 'git-hook-bypass']),
|
|
110
|
+
'DET-02': Object.freeze(['secret-boundary']),
|
|
111
|
+
'DET-03': Object.freeze(['protected-files']),
|
|
112
|
+
'DET-04': Object.freeze(['retry-churn']),
|
|
113
|
+
'DET-05': Object.freeze([
|
|
114
|
+
'diff-thrashing',
|
|
115
|
+
'edit-oscillation',
|
|
116
|
+
'assertion-weakening',
|
|
117
|
+
'file-shrinkage',
|
|
118
|
+
'empty-catch'
|
|
119
|
+
]),
|
|
120
|
+
'DET-06': Object.freeze(['blind-search', 'redundant-reread']),
|
|
121
|
+
'DET-07': Object.freeze(['output-bomb']),
|
|
122
|
+
'DET-08': Object.freeze(['cache-gravity-hold']),
|
|
123
|
+
'DET-09': Object.freeze(['prefix-mutation-fence']),
|
|
124
|
+
'DET-10': Object.freeze(['ttl-watchdog']),
|
|
125
|
+
'DET-11': Object.freeze(['context-rot', 'shadow-file-bloat']),
|
|
126
|
+
'DET-12': Object.freeze(['haiku-pre-cliff']),
|
|
127
|
+
'DET-13': Object.freeze(['mcp-schema-bloat']),
|
|
128
|
+
'DET-14': Object.freeze(['premature-stop']),
|
|
129
|
+
'DET-15': Object.freeze(['batched-formatter']),
|
|
130
|
+
'DET-16': Object.freeze(['subagent-recursion']),
|
|
131
|
+
'DET-17': Object.freeze(['auto-compact-deferral']),
|
|
132
|
+
'DET-18': Object.freeze(['untrusted-settings']),
|
|
133
|
+
'DET-19': Object.freeze(['subagent-ipi']),
|
|
134
|
+
'DET-20': Object.freeze(['scope-lock']),
|
|
135
|
+
'DET-21': Object.freeze(['thinking-drift-loop', 'scratchpad-loop', 'thinking-drift']),
|
|
136
|
+
'DET-22': Object.freeze(['file-shrinkage-rapid', 'rapid-mutation-sensor']),
|
|
137
|
+
'DET-23': Object.freeze(['empty-catch-block', 'swallowed-error', 'knowledge-bleed']),
|
|
138
|
+
'DET-24': Object.freeze(['high-turn-degradation', 'prompt-bloat']),
|
|
139
|
+
'DET-25': Object.freeze(['progressive-checkpoint', 'cascade-handshake', 'foreign-bleed']),
|
|
140
|
+
'DET-26': Object.freeze(['prompt-decomposer', 'monolithic-prompt', 'legacy-poison']),
|
|
141
|
+
'DET-27': Object.freeze(['rule-file-bloat', 'prefix-diet', 'unseen-callers']),
|
|
142
|
+
'DET-28': Object.freeze(['subagent-phantom', 'orphan-subagent', 'dirty-snap']),
|
|
143
|
+
'DET-29': Object.freeze(['reflector-queue', 'async-feedback', 'turn-zero-gate']),
|
|
144
|
+
'DET-30': Object.freeze(['thinking-drift-loop', 'scratchpad-loop', 'thinking-drift']),
|
|
145
|
+
'DET-31': Object.freeze(['worktree-merge-conflict', 'git-merge-tree-probe']),
|
|
146
|
+
'DET-32': Object.freeze(['semantic-drift-guard', 'hypothesis-loop', 'ioedr-remory-guard'])
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
export const SEMANTIC_SLUG_TO_DETECTOR_ID = Object.freeze(
|
|
150
|
+
Object.fromEntries(
|
|
151
|
+
Object.entries(DETECTOR_SEMANTIC_ALIASES).flatMap(([detId, slugs]) =>
|
|
152
|
+
slugs.map((slug) => [slug, detId])
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Cross-edition translation map from Antigravity numeric `DET-XX` IDs to shared semantic slugs (`ISS-052`).
|
|
159
|
+
* Ensures `GA DET-01` (`retry-churn` -> `CC DET-04`) NEVER disables `CC DET-01` (`git-shell-safety`)
|
|
160
|
+
* and `GA DET-02` (`edit-oscillation` -> `CC DET-05`) NEVER disables `CC DET-02` (`secret-boundary`).
|
|
161
|
+
*/
|
|
162
|
+
export const ANTIGRAVITY_DETECTOR_TO_SEMANTIC_SLUG = Object.freeze({
|
|
163
|
+
'DET-01': 'retry-churn',
|
|
164
|
+
'DET-02': 'edit-oscillation',
|
|
165
|
+
'DET-03': 'assertion-weakening',
|
|
166
|
+
'DET-06': 'redundant-reread',
|
|
167
|
+
'DET-08': 'git-shell-safety',
|
|
168
|
+
'DET-09': 'prefix-mutation-fence',
|
|
169
|
+
'DET-10': 'output-bomb',
|
|
170
|
+
'DET-15': 'secret-boundary',
|
|
171
|
+
'DET-17': 'shadow-file-bloat',
|
|
172
|
+
'DET-17B': 'shadow-file-bloat',
|
|
173
|
+
'DET-18': 'untrusted-settings',
|
|
174
|
+
'DET-19': 'git-shell-safety',
|
|
175
|
+
'DET-20': 'scope-lock',
|
|
176
|
+
'DET-30': 'thinking-drift-loop',
|
|
177
|
+
'DET-31': 'worktree-merge-conflict',
|
|
178
|
+
'DET-32': 'semantic-drift-guard'
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Advisory (non-gating) Claude Code detectors.
|
|
183
|
+
* They never block tool calls; they surface Coach warnings only.
|
|
184
|
+
*/
|
|
185
|
+
export const ADVISORY_DETECTOR_IDS = Object.freeze([
|
|
186
|
+
'DET-06',
|
|
187
|
+
'DET-10',
|
|
188
|
+
'DET-11',
|
|
189
|
+
'DET-13',
|
|
190
|
+
'DET-15',
|
|
191
|
+
'DET-16',
|
|
192
|
+
'DET-17',
|
|
193
|
+
'DET-23',
|
|
194
|
+
'DET-24',
|
|
195
|
+
'DET-25',
|
|
196
|
+
'DET-26',
|
|
197
|
+
'DET-27',
|
|
198
|
+
'DET-28',
|
|
199
|
+
'DET-31'
|
|
200
|
+
]);
|
|
201
|
+
export const ADVISORY_DETECTOR_SLUGS = Object.freeze({
|
|
202
|
+
'DET-30': 'thinking-drift-loop',
|
|
203
|
+
'DET-31': 'worktree-merge-conflict'
|
|
204
|
+
});
|
|
205
|
+
const ADVISORY_SLUG_TO_ID = Object.freeze({
|
|
206
|
+
'thinking-drift-loop': 'DET-30',
|
|
207
|
+
'scratchpad-loop': 'DET-30',
|
|
208
|
+
'thinking-drift': 'DET-30',
|
|
209
|
+
'worktree-merge-conflict': 'DET-31',
|
|
210
|
+
'semantic-drift-guard': 'DET-32'
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
export const VALID_DETECTOR_MODES = Object.freeze(['default', 'off', 'warn', 'block']);
|
|
214
|
+
|
|
215
|
+
export const HABIT_SEMANTIC_SLUGS = Object.freeze([
|
|
216
|
+
'formatter-bypass',
|
|
217
|
+
'drip-feed',
|
|
218
|
+
'vague-prompt-rubric',
|
|
219
|
+
'effort-overkill',
|
|
220
|
+
'post-commit-handoff',
|
|
221
|
+
'vision-screenshot-coach',
|
|
222
|
+
'micro-skills',
|
|
223
|
+
'relay-breaker'
|
|
224
|
+
]);
|
|
225
|
+
|
|
226
|
+
export const DEFAULT_HORIZON_CONFIG = Object.freeze({
|
|
227
|
+
schemaVersion: '1.0.0',
|
|
228
|
+
detectorNamespace: 'claude-code',
|
|
229
|
+
flightMode: 'COACH',
|
|
230
|
+
detectors: Object.fromEntries(CANONICAL_DETECTOR_IDS.slice(0, 20).map((id) => [id, 'default'])),
|
|
231
|
+
semanticDetectors: Object.freeze(
|
|
232
|
+
Object.fromEntries(HABIT_SEMANTIC_SLUGS.map((slug) => [slug, 'default']))
|
|
233
|
+
),
|
|
234
|
+
thresholds: {
|
|
235
|
+
safeLimitTokens: 147000,
|
|
236
|
+
reminderAtPct: 75,
|
|
237
|
+
subagentOffloadTokens: 12000,
|
|
238
|
+
blindSearchLimit: 6,
|
|
239
|
+
consecutiveFailureLimit: 3,
|
|
240
|
+
enableLocalKeepAlive: false,
|
|
241
|
+
microSkillConfirmSessions: 2,
|
|
242
|
+
minSwitchPenaltyUSD: 0.02,
|
|
243
|
+
notableAllowPenaltyUSD: 0.02,
|
|
244
|
+
outputShrinkageMinTurns: 5,
|
|
245
|
+
outputShrinkagePriorTokens: 500,
|
|
246
|
+
smartCompactBoundarySuggest: false,
|
|
247
|
+
resumeToolsetWarnMinTokens: 10000
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Parses a strict numeric `DET-XX` (or `DET-17b`) token without resolving semantic slugs.
|
|
253
|
+
* @param {unknown} rawKey
|
|
254
|
+
* @returns {string | null}
|
|
255
|
+
*/
|
|
256
|
+
function parseRawNumericDetToken(rawKey) {
|
|
257
|
+
const m = String(rawKey || '')
|
|
258
|
+
.trim()
|
|
259
|
+
.toUpperCase()
|
|
260
|
+
.match(/^DET[-_]?(\d{1,2})([A-Z])?$/);
|
|
261
|
+
if (!m) return null;
|
|
262
|
+
const num = Number(m[1]);
|
|
263
|
+
if (num < 1 || num > 99) return null;
|
|
264
|
+
const suffix = m[2] || '';
|
|
265
|
+
return `DET-${String(num).padStart(2, '0')}${suffix}`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Normalizes a semantic slug token (`git_shell_safety` -> `git-shell-safety`).
|
|
270
|
+
* @param {unknown} rawSlug
|
|
271
|
+
* @returns {string}
|
|
272
|
+
*/
|
|
273
|
+
function normalizeSlugToken(rawSlug) {
|
|
274
|
+
return String(rawSlug || '')
|
|
275
|
+
.trim()
|
|
276
|
+
.toLowerCase()
|
|
277
|
+
.replace(/[_\s]+/g, '-');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Normalizes a detector ID or semantic slug (`det-1`, `DET01`, `git-shell-safety`, `retry-churn` -> `DET-01..DET-20`).
|
|
282
|
+
* @param {string} rawId
|
|
283
|
+
* @returns {string | null}
|
|
284
|
+
*/
|
|
285
|
+
export function normalizeDetectorId(rawId) {
|
|
286
|
+
const rawToken = parseRawNumericDetToken(rawId);
|
|
287
|
+
if (rawToken === 'DET-21') {
|
|
288
|
+
return 'DET-30';
|
|
289
|
+
}
|
|
290
|
+
if (rawToken && (CANONICAL_DETECTOR_IDS.includes(rawToken) || ADVISORY_DETECTOR_IDS.includes(rawToken))) {
|
|
291
|
+
return rawToken;
|
|
292
|
+
}
|
|
293
|
+
const slug = normalizeSlugToken(rawId);
|
|
294
|
+
if (slug && Object.prototype.hasOwnProperty.call(SEMANTIC_SLUG_TO_DETECTOR_ID, slug)) {
|
|
295
|
+
return SEMANTIC_SLUG_TO_DETECTOR_ID[slug];
|
|
296
|
+
}
|
|
297
|
+
if (slug && Object.prototype.hasOwnProperty.call(ADVISORY_SLUG_TO_ID, slug)) {
|
|
298
|
+
return ADVISORY_SLUG_TO_ID[slug];
|
|
299
|
+
}
|
|
300
|
+
const targetHabitSlug =
|
|
301
|
+
slug === 'prompt-decomposer' || slug === 'scope-brake'
|
|
302
|
+
? 'vague-prompt-rubric'
|
|
303
|
+
: slug === 'spec-relay'
|
|
304
|
+
? 'relay-breaker'
|
|
305
|
+
: slug;
|
|
306
|
+
if (targetHabitSlug && HABIT_SEMANTIC_SLUGS.includes(targetHabitSlug)) {
|
|
307
|
+
return targetHabitSlug;
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Resolves the canonical primary semantic slug for any `DET-01..DET-32` ID or alias.
|
|
314
|
+
* @param {string} idOrSlug
|
|
315
|
+
* @returns {string | null}
|
|
316
|
+
*/
|
|
317
|
+
export function resolveDetectorSlug(idOrSlug) {
|
|
318
|
+
const normId = normalizeDetectorId(idOrSlug);
|
|
319
|
+
if (normId && HABIT_SEMANTIC_SLUGS.includes(normId)) return normId;
|
|
320
|
+
return normId ? DETECTOR_SEMANTIC_SLUGS[normId] || ADVISORY_DETECTOR_SLUGS[normId] || null : null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Normalizes a detector override mode (`off` | `warn` | `block` | `default`).
|
|
325
|
+
* @param {unknown} rawMode
|
|
326
|
+
* @returns {'default' | 'off' | 'warn' | 'block'}
|
|
327
|
+
*/
|
|
328
|
+
export function normalizeDetectorMode(rawMode) {
|
|
329
|
+
if (rawMode === false || rawMode === 0) return 'off';
|
|
330
|
+
if (rawMode === true) return 'default';
|
|
331
|
+
const s = String(rawMode || 'default')
|
|
332
|
+
.trim()
|
|
333
|
+
.toLowerCase();
|
|
334
|
+
if (s === 'off' || s === 'disable' || s === 'disabled' || s === 'mute') return 'off';
|
|
335
|
+
if (s === 'warn' || s === 'advisory' || s === 'coach' || s === 'steer') return 'warn';
|
|
336
|
+
if (s === 'block' || s === 'deny' || s === 'strict' || s === 'enforce') return 'block';
|
|
337
|
+
return 'default';
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Normalizes a configured `flightMode` (`COACH` | `AUTO_PILOT` | `SILENT` | `OFF`),
|
|
342
|
+
* returning `null` when unrecognized.
|
|
343
|
+
*
|
|
344
|
+
* @param {unknown} rawFlightMode
|
|
345
|
+
* @returns {'COACH' | 'AUTO_PILOT' | 'SILENT' | 'OFF' | null}
|
|
346
|
+
*/
|
|
347
|
+
export function normalizeConfiguredFlightMode(rawFlightMode) {
|
|
348
|
+
if (typeof rawFlightMode !== 'string' || !rawFlightMode.trim()) return null;
|
|
349
|
+
const cleaned = rawFlightMode
|
|
350
|
+
.trim()
|
|
351
|
+
.toUpperCase()
|
|
352
|
+
.replace(/[-\s]+/g, '_');
|
|
353
|
+
if (cleaned === 'COACH') return 'COACH';
|
|
354
|
+
if (cleaned === 'AUTO_PILOT' || cleaned === 'AUTOPILOT' || cleaned === 'AUTO') {
|
|
355
|
+
return 'AUTO_PILOT';
|
|
356
|
+
}
|
|
357
|
+
if (cleaned === 'SILENT' || cleaned === 'QUIET' || cleaned === 'MUTE') {
|
|
358
|
+
return 'SILENT';
|
|
359
|
+
}
|
|
360
|
+
if (cleaned === 'OFF' || cleaned === 'DISABLED') {
|
|
361
|
+
return 'OFF';
|
|
362
|
+
}
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Resolves the effective override mode (`default` | `off` | `warn` | `block`) for a detector ID
|
|
368
|
+
* or semantic slug against a loaded or partial config object.
|
|
369
|
+
*
|
|
370
|
+
* @param {string} idOrSlug
|
|
371
|
+
* @param {object | null} [config=null]
|
|
372
|
+
* @returns {'default' | 'off' | 'warn' | 'block'}
|
|
373
|
+
*/
|
|
374
|
+
export function resolveDetectorOverride(idOrSlug, config = null) {
|
|
375
|
+
if (!config || typeof config !== 'object') return 'default';
|
|
376
|
+
const rawSlug = normalizeSlugToken(idOrSlug);
|
|
377
|
+
const habitAliases =
|
|
378
|
+
rawSlug === 'vague-prompt-rubric' || rawSlug === 'scope-brake'
|
|
379
|
+
? ['vague-prompt-rubric', 'prompt-decomposer', 'scope-brake']
|
|
380
|
+
: rawSlug === 'prompt-decomposer'
|
|
381
|
+
? ['prompt-decomposer', 'vague-prompt-rubric', 'scope-brake']
|
|
382
|
+
: rawSlug === 'relay-breaker' || rawSlug === 'spec-relay'
|
|
383
|
+
? ['relay-breaker', 'spec-relay']
|
|
384
|
+
: HABIT_SEMANTIC_SLUGS.includes(rawSlug)
|
|
385
|
+
? [rawSlug]
|
|
386
|
+
: [];
|
|
387
|
+
|
|
388
|
+
for (const hSlug of habitAliases) {
|
|
389
|
+
if (config.semanticDetectors && Object.prototype.hasOwnProperty.call(config.semanticDetectors, hSlug)) {
|
|
390
|
+
const m = normalizeDetectorMode(config.semanticDetectors[hSlug]);
|
|
391
|
+
if (m !== 'default') return m;
|
|
392
|
+
}
|
|
393
|
+
if (config.detectors && Object.prototype.hasOwnProperty.call(config.detectors, hSlug)) {
|
|
394
|
+
const m = normalizeDetectorMode(config.detectors[hSlug]);
|
|
395
|
+
if (m !== 'default') return m;
|
|
396
|
+
}
|
|
397
|
+
if (config.claudeDetectors && Object.prototype.hasOwnProperty.call(config.claudeDetectors, hSlug)) {
|
|
398
|
+
const m = normalizeDetectorMode(config.claudeDetectors[hSlug]);
|
|
399
|
+
if (m !== 'default') return m;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const normId = normalizeDetectorId(idOrSlug);
|
|
404
|
+
if (!normId) return 'default';
|
|
405
|
+
|
|
406
|
+
const lookupIds = normId === 'DET-30' ? ['DET-30', 'DET-21'] : [normId];
|
|
407
|
+
|
|
408
|
+
if (config.claudeDetectors && typeof config.claudeDetectors === 'object') {
|
|
409
|
+
for (const lid of lookupIds) {
|
|
410
|
+
if (Object.prototype.hasOwnProperty.call(config.claudeDetectors, lid)) {
|
|
411
|
+
return normalizeDetectorMode(config.claudeDetectors[lid]);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
for (const [k, v] of Object.entries(config.claudeDetectors)) {
|
|
415
|
+
if (normalizeDetectorId(k) === normId) {
|
|
416
|
+
return normalizeDetectorMode(v);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const detMap = config.detectors;
|
|
422
|
+
if (detMap && typeof detMap === 'object') {
|
|
423
|
+
for (const lid of lookupIds) {
|
|
424
|
+
if (Object.prototype.hasOwnProperty.call(detMap, lid)) {
|
|
425
|
+
const directMode = normalizeDetectorMode(detMap[lid]);
|
|
426
|
+
if (directMode !== 'default') return directMode;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const aliases = DETECTOR_SEMANTIC_ALIASES[normId] || [];
|
|
430
|
+
for (const slug of aliases) {
|
|
431
|
+
if (Object.prototype.hasOwnProperty.call(detMap, slug)) {
|
|
432
|
+
const slugMode = normalizeDetectorMode(detMap[slug]);
|
|
433
|
+
if (slugMode !== 'default') return slugMode;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
for (const [k, v] of Object.entries(detMap)) {
|
|
437
|
+
if (normalizeDetectorId(k) === normId) {
|
|
438
|
+
return normalizeDetectorMode(v);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (config.semanticDetectors && typeof config.semanticDetectors === 'object') {
|
|
444
|
+
const primarySlug = DETECTOR_SEMANTIC_SLUGS[normId];
|
|
445
|
+
if (primarySlug && Object.prototype.hasOwnProperty.call(config.semanticDetectors, primarySlug)) {
|
|
446
|
+
return normalizeDetectorMode(config.semanticDetectors[primarySlug]);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return 'default';
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Resolves user-global and project-local `.attention-horizon.json` paths.
|
|
455
|
+
* @param {object} [options={}]
|
|
456
|
+
* @returns {{ projectConfigPath: string, globalConfigPath: string, activeWritePath: string }}
|
|
457
|
+
*/
|
|
458
|
+
export function resolveHorizonConfigPaths(options = {}) {
|
|
459
|
+
const cwd = getCwd(options);
|
|
460
|
+
const homeDir = getHomeDir(options) || cwd;
|
|
461
|
+
const scope = options.scope === 'global' ? 'global' : 'project';
|
|
462
|
+
|
|
463
|
+
const projectConfigPath = options.configPath || path.join(cwd, '.attention-horizon.json');
|
|
464
|
+
const globalConfigPath =
|
|
465
|
+
options.globalConfigPath || path.join(homeDir, '.attention-horizon', 'config.json');
|
|
466
|
+
const activeWritePath = scope === 'global' ? globalConfigPath : projectConfigPath;
|
|
467
|
+
|
|
468
|
+
return {
|
|
469
|
+
projectConfigPath,
|
|
470
|
+
globalConfigPath,
|
|
471
|
+
activeWritePath
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const MODE_STRICTNESS_RANK = Object.freeze({
|
|
476
|
+
default: 0,
|
|
477
|
+
off: 1,
|
|
478
|
+
warn: 2,
|
|
479
|
+
block: 3
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Translates an Antigravity detector override map into Claude Code canonical `DET-XX` overrides
|
|
484
|
+
* via `ANTIGRAVITY_DETECTOR_TO_SEMANTIC_SLUG` + semantic slugs.
|
|
485
|
+
*
|
|
486
|
+
* @param {Record<string, unknown>} rawMap
|
|
487
|
+
* @param {Record<string, 'default' | 'off' | 'warn' | 'block'>} targetDetectors
|
|
488
|
+
*/
|
|
489
|
+
function mergeForeignEditionDetectorMap(rawMap, targetDetectors) {
|
|
490
|
+
if (!rawMap || typeof rawMap !== 'object' || Array.isArray(rawMap)) return;
|
|
491
|
+
|
|
492
|
+
const staged = new Map();
|
|
493
|
+
for (const [rawKey, rawVal] of Object.entries(rawMap)) {
|
|
494
|
+
const mode = normalizeDetectorMode(rawVal);
|
|
495
|
+
const numericToken = parseRawNumericDetToken(rawKey);
|
|
496
|
+
|
|
497
|
+
let resolvedCcId = null;
|
|
498
|
+
if (numericToken) {
|
|
499
|
+
const mappedSlug = ANTIGRAVITY_DETECTOR_TO_SEMANTIC_SLUG[numericToken];
|
|
500
|
+
if (!mappedSlug) {
|
|
501
|
+
// Foreign-only numeric detector with no Claude Code counterpart: ignore safely
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
resolvedCcId = SEMANTIC_SLUG_TO_DETECTOR_ID[mappedSlug] || null;
|
|
505
|
+
} else {
|
|
506
|
+
resolvedCcId = normalizeDetectorId(rawKey);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (!resolvedCcId) continue;
|
|
510
|
+
|
|
511
|
+
if (!staged.has(resolvedCcId)) {
|
|
512
|
+
staged.set(resolvedCcId, mode);
|
|
513
|
+
} else {
|
|
514
|
+
const prev = staged.get(resolvedCcId);
|
|
515
|
+
if (mode !== 'default' && (prev === 'default' || MODE_STRICTNESS_RANK[mode] > MODE_STRICTNESS_RANK[prev])) {
|
|
516
|
+
staged.set(resolvedCcId, mode);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
for (const [detId, mode] of staged.entries()) {
|
|
522
|
+
targetDetectors[detId] = mode;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Loads and merges `~/.attention-horizon/config.json` and `<cwd>/.attention-horizon.json`.
|
|
528
|
+
* Project-level settings take precedence over user-global settings.
|
|
529
|
+
*
|
|
530
|
+
* Implements Cross-Edition Semantic Translation & Ring-0 Inversion Protection (`ISS-052`).
|
|
531
|
+
*
|
|
532
|
+
* @param {object} [options={}]
|
|
533
|
+
* @returns {{
|
|
534
|
+
* schemaVersion: string,
|
|
535
|
+
* detectorNamespace: string,
|
|
536
|
+
* flightMode: string,
|
|
537
|
+
* detectors: Record<string, 'default' | 'off' | 'warn' | 'block'>,
|
|
538
|
+
* semanticDetectors: Record<string, 'default' | 'off' | 'warn' | 'block'>,
|
|
539
|
+
* thresholds: Record<string, number | boolean>,
|
|
540
|
+
* warnings: string[],
|
|
541
|
+
* sources: { globalLoaded: boolean, projectLoaded: boolean, projectConfigPath: string, globalConfigPath: string }
|
|
542
|
+
* }}
|
|
543
|
+
*/
|
|
544
|
+
export function loadHorizonConfig(options = {}) {
|
|
545
|
+
const { projectConfigPath, globalConfigPath } = resolveHorizonConfigPaths(options);
|
|
546
|
+
|
|
547
|
+
const globalRaw = readJsonSafe(globalConfigPath, null, options);
|
|
548
|
+
const projectRaw = readJsonSafe(projectConfigPath, null, options);
|
|
549
|
+
|
|
550
|
+
const detectors = { ...DEFAULT_HORIZON_CONFIG.detectors };
|
|
551
|
+
const habitSemantics = Object.fromEntries(HABIT_SEMANTIC_SLUGS.map((s) => [s, 'default']));
|
|
552
|
+
const thresholds = { ...DEFAULT_HORIZON_CONFIG.thresholds };
|
|
553
|
+
const warnings = [];
|
|
554
|
+
let flightMode = DEFAULT_HORIZON_CONFIG.flightMode;
|
|
555
|
+
let resolvedNamespace = DEFAULT_HORIZON_CONFIG.detectorNamespace;
|
|
556
|
+
let autoPilotDecision = null;
|
|
557
|
+
let expectedRemainingTurns = null;
|
|
558
|
+
let useEmpiricalHorizonPrior = false;
|
|
559
|
+
|
|
560
|
+
const mergeHabitSlugsFromMap = (mapObj) => {
|
|
561
|
+
if (!mapObj || typeof mapObj !== 'object' || Array.isArray(mapObj)) return;
|
|
562
|
+
for (const [rk, rv] of Object.entries(mapObj)) {
|
|
563
|
+
const slug = normalizeSlugToken(rk);
|
|
564
|
+
const targetSlug =
|
|
565
|
+
slug === 'prompt-decomposer' || slug === 'scope-brake'
|
|
566
|
+
? 'vague-prompt-rubric'
|
|
567
|
+
: slug === 'spec-relay'
|
|
568
|
+
? 'relay-breaker'
|
|
569
|
+
: slug;
|
|
570
|
+
if (HABIT_SEMANTIC_SLUGS.includes(targetSlug)) {
|
|
571
|
+
habitSemantics[targetSlug] = normalizeDetectorMode(rv);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
const mergeLayer = (layer, { isDiskLayer = false, sourceLabel = 'config' } = {}) => {
|
|
577
|
+
if (!layer || typeof layer !== 'object' || Array.isArray(layer)) return;
|
|
578
|
+
|
|
579
|
+
mergeHabitSlugsFromMap(layer.semanticDetectors);
|
|
580
|
+
mergeHabitSlugsFromMap(layer.detectors);
|
|
581
|
+
mergeHabitSlugsFromMap(layer.claudeDetectors);
|
|
582
|
+
|
|
583
|
+
const parsedMode = normalizeConfiguredFlightMode(layer.flightMode);
|
|
584
|
+
if (parsedMode) {
|
|
585
|
+
flightMode = parsedMode;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const rawNs =
|
|
589
|
+
typeof layer.detectorNamespace === 'string'
|
|
590
|
+
? layer.detectorNamespace.trim().toLowerCase()
|
|
591
|
+
: '';
|
|
592
|
+
if (rawNs) {
|
|
593
|
+
resolvedNamespace = rawNs;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const hasClaudeDetectors =
|
|
597
|
+
Boolean(layer.claudeDetectors) &&
|
|
598
|
+
typeof layer.claudeDetectors === 'object' &&
|
|
599
|
+
!Array.isArray(layer.claudeDetectors);
|
|
600
|
+
|
|
601
|
+
// 1. Optional cross-edition `antigravityDetectors` map (translated via semantic slugs)
|
|
602
|
+
if (layer.antigravityDetectors && typeof layer.antigravityDetectors === 'object') {
|
|
603
|
+
mergeForeignEditionDetectorMap(layer.antigravityDetectors, detectors);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// 2. Primary `detectors` map
|
|
607
|
+
if (layer.detectors && typeof layer.detectors === 'object' && !Array.isArray(layer.detectors)) {
|
|
608
|
+
if (rawNs === 'antigravity') {
|
|
609
|
+
mergeForeignEditionDetectorMap(layer.detectors, detectors);
|
|
610
|
+
} else {
|
|
611
|
+
// Track which Ring-0 detectors have explicit semantic slug `'off'` confirmation in this layer
|
|
612
|
+
const slugConfirmedOff = new Set();
|
|
613
|
+
for (const [k, v] of Object.entries(layer.detectors)) {
|
|
614
|
+
if (!parseRawNumericDetToken(k) && normalizeDetectorMode(v) === 'off') {
|
|
615
|
+
const mappedId = normalizeDetectorId(k);
|
|
616
|
+
if (mappedId) slugConfirmedOff.add(mappedId);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
const isAmbiguousLegacyDiskLayer =
|
|
621
|
+
isDiskLayer && !rawNs && !hasClaudeDetectors;
|
|
622
|
+
|
|
623
|
+
// First apply numeric keys, then semantic slugs so explicit slugs always win
|
|
624
|
+
const entries = Object.entries(layer.detectors);
|
|
625
|
+
const numericEntries = entries.filter(([k]) => Boolean(parseRawNumericDetToken(k)));
|
|
626
|
+
const slugEntries = entries.filter(([k]) => !parseRawNumericDetToken(k));
|
|
627
|
+
|
|
628
|
+
for (const [k, v] of [...numericEntries, ...slugEntries]) {
|
|
629
|
+
const normId = normalizeDetectorId(k);
|
|
630
|
+
if (!normId || HABIT_SEMANTIC_SLUGS.includes(normId)) continue;
|
|
631
|
+
let normMode = normalizeDetectorMode(v);
|
|
632
|
+
|
|
633
|
+
if (
|
|
634
|
+
isAmbiguousLegacyDiskLayer &&
|
|
635
|
+
Boolean(parseRawNumericDetToken(k)) &&
|
|
636
|
+
normMode === 'off' &&
|
|
637
|
+
RING0_SECURITY_DETECTOR_IDS.includes(normId) &&
|
|
638
|
+
!slugConfirmedOff.has(normId)
|
|
639
|
+
) {
|
|
640
|
+
const reqSlug = DETECTOR_SEMANTIC_SLUGS[normId];
|
|
641
|
+
normMode = 'warn';
|
|
642
|
+
warnings.push(
|
|
643
|
+
`[ISS-052 Ring-0 Inversion Guard] Clamped ambiguous un-namespaced "${normId}": "off" in ${sourceLabel} to "warn" to protect "${reqSlug}". Set "${reqSlug}": "off" or "detectorNamespace": "claude-code" to disable intentionally.`
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
detectors[normId] = normMode;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// 3. Explicit `claudeDetectors` map always takes precedence for Claude Code
|
|
653
|
+
if (hasClaudeDetectors) {
|
|
654
|
+
for (const [k, v] of Object.entries(layer.claudeDetectors)) {
|
|
655
|
+
const normId = normalizeDetectorId(k);
|
|
656
|
+
if (normId && !HABIT_SEMANTIC_SLUGS.includes(normId)) {
|
|
657
|
+
detectors[normId] = normalizeDetectorMode(v);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
if (layer.thresholds && typeof layer.thresholds === 'object') {
|
|
663
|
+
for (const [tk, tv] of Object.entries(layer.thresholds)) {
|
|
664
|
+
if (tk in thresholds && (typeof tv === 'number' || typeof tv === 'boolean')) {
|
|
665
|
+
thresholds[tk] = tv;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
if (layer.autoPilotDecision === 'ask' || layer.autoPilotDecision === 'deny') {
|
|
671
|
+
autoPilotDecision = layer.autoPilotDecision;
|
|
672
|
+
}
|
|
673
|
+
if (
|
|
674
|
+
layer.expectedRemainingTurns !== undefined &&
|
|
675
|
+
layer.expectedRemainingTurns !== null &&
|
|
676
|
+
Number.isFinite(Number(layer.expectedRemainingTurns))
|
|
677
|
+
) {
|
|
678
|
+
expectedRemainingTurns = Math.max(0, Number(layer.expectedRemainingTurns));
|
|
679
|
+
}
|
|
680
|
+
if (typeof layer.useEmpiricalHorizonPrior === 'boolean') {
|
|
681
|
+
useEmpiricalHorizonPrior = layer.useEmpiricalHorizonPrior;
|
|
682
|
+
}
|
|
683
|
+
if (typeof layer.smartCompactBoundarySuggest === 'boolean') {
|
|
684
|
+
thresholds.smartCompactBoundarySuggest = layer.smartCompactBoundarySuggest;
|
|
685
|
+
}
|
|
686
|
+
if (
|
|
687
|
+
layer.resumeToolsetWarnMinTokens !== undefined &&
|
|
688
|
+
layer.resumeToolsetWarnMinTokens !== null &&
|
|
689
|
+
Number.isFinite(Number(layer.resumeToolsetWarnMinTokens))
|
|
690
|
+
) {
|
|
691
|
+
thresholds.resumeToolsetWarnMinTokens = Math.max(0, Number(layer.resumeToolsetWarnMinTokens));
|
|
692
|
+
}
|
|
693
|
+
for (const tk of [
|
|
694
|
+
'minSwitchPenaltyUSD',
|
|
695
|
+
'notableAllowPenaltyUSD',
|
|
696
|
+
'outputShrinkageMinTurns',
|
|
697
|
+
'outputShrinkagePriorTokens'
|
|
698
|
+
]) {
|
|
699
|
+
if (layer[tk] !== undefined && layer[tk] !== null && Number.isFinite(Number(layer[tk]))) {
|
|
700
|
+
thresholds[tk] = Math.max(0, Number(layer[tk]));
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
mergeLayer(globalRaw, { isDiskLayer: true, sourceLabel: globalConfigPath });
|
|
706
|
+
mergeLayer(projectRaw, { isDiskLayer: true, sourceLabel: projectConfigPath });
|
|
707
|
+
if (options.overrides && typeof options.overrides === 'object') {
|
|
708
|
+
mergeLayer(options.overrides, { isDiskLayer: false, sourceLabel: 'overrides' });
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
const semanticDetectors = {
|
|
712
|
+
...Object.fromEntries(
|
|
713
|
+
CANONICAL_DETECTOR_IDS.map((id) => [DETECTOR_SEMANTIC_SLUGS[id], detectors[id]])
|
|
714
|
+
),
|
|
715
|
+
...habitSemantics
|
|
716
|
+
};
|
|
717
|
+
|
|
718
|
+
return {
|
|
719
|
+
schemaVersion: '1.0.0',
|
|
720
|
+
detectorNamespace: resolvedNamespace,
|
|
721
|
+
flightMode,
|
|
722
|
+
detectors,
|
|
723
|
+
semanticDetectors,
|
|
724
|
+
thresholds,
|
|
725
|
+
warnings,
|
|
726
|
+
...(autoPilotDecision ? { autoPilotDecision } : {}),
|
|
727
|
+
...(expectedRemainingTurns !== null ? { expectedRemainingTurns } : {}),
|
|
728
|
+
...(useEmpiricalHorizonPrior ? { useEmpiricalHorizonPrior: true } : {}),
|
|
729
|
+
sources: {
|
|
730
|
+
globalLoaded: Boolean(globalRaw),
|
|
731
|
+
projectLoaded: Boolean(projectRaw),
|
|
732
|
+
projectConfigPath,
|
|
733
|
+
globalConfigPath
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Sets a specific detector override (`off` | `warn` | `block` | `default`) in `.attention-horizon.json`.
|
|
740
|
+
* Writes `detectorNamespace: 'claude-code'`, `detectors[canonicalDetId]`, `detectors[semanticSlug]`,
|
|
741
|
+
* and `claudeDetectors[canonicalDetId]` for unambiguous cross-edition safety (`ISS-052`).
|
|
742
|
+
*
|
|
743
|
+
* @param {string} idOrSlug
|
|
744
|
+
* @param {string} mode
|
|
745
|
+
* @param {object} [options={}]
|
|
746
|
+
* @returns {{ ok: boolean, detectorId: string | null, semanticSlug: string | null, mode: string, configPath: string, config: object }}
|
|
747
|
+
*/
|
|
748
|
+
export function setDetectorOverride(idOrSlug, mode, options = {}) {
|
|
749
|
+
const rawSlug = normalizeSlugToken(idOrSlug);
|
|
750
|
+
const targetHabitSlug =
|
|
751
|
+
rawSlug === 'prompt-decomposer' || rawSlug === 'scope-brake'
|
|
752
|
+
? 'vague-prompt-rubric'
|
|
753
|
+
: rawSlug === 'spec-relay'
|
|
754
|
+
? 'relay-breaker'
|
|
755
|
+
: rawSlug;
|
|
756
|
+
const normMode = normalizeDetectorMode(mode);
|
|
757
|
+
const { activeWritePath } = resolveHorizonConfigPaths(options);
|
|
758
|
+
|
|
759
|
+
if (HABIT_SEMANTIC_SLUGS.includes(targetHabitSlug)) {
|
|
760
|
+
const existing = readJsonSafe(activeWritePath, {}, options) || {};
|
|
761
|
+
const nextDetectors = { ...(existing.detectors || {}) };
|
|
762
|
+
const nextSemantic = { ...(existing.semanticDetectors || {}) };
|
|
763
|
+
if (normMode === 'default') {
|
|
764
|
+
delete nextDetectors[targetHabitSlug];
|
|
765
|
+
delete nextSemantic[targetHabitSlug];
|
|
766
|
+
} else {
|
|
767
|
+
nextDetectors[targetHabitSlug] = normMode;
|
|
768
|
+
nextSemantic[targetHabitSlug] = normMode;
|
|
769
|
+
}
|
|
770
|
+
const nextPayload = {
|
|
771
|
+
schemaVersion: '1.0.0',
|
|
772
|
+
detectorNamespace: 'claude-code',
|
|
773
|
+
...existing,
|
|
774
|
+
detectorNamespace: 'claude-code',
|
|
775
|
+
detectors: nextDetectors,
|
|
776
|
+
semanticDetectors: nextSemantic
|
|
777
|
+
};
|
|
778
|
+
const ok = writeJsonAtomic(activeWritePath, nextPayload, options);
|
|
779
|
+
return {
|
|
780
|
+
ok,
|
|
781
|
+
detectorId: targetHabitSlug,
|
|
782
|
+
semanticSlug: targetHabitSlug,
|
|
783
|
+
mode: normMode,
|
|
784
|
+
configPath: activeWritePath,
|
|
785
|
+
config: loadHorizonConfig(options)
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
const normId = normalizeDetectorId(idOrSlug);
|
|
790
|
+
const semanticSlug = normId ? DETECTOR_SEMANTIC_SLUGS[normId] : null;
|
|
791
|
+
|
|
792
|
+
if (!normId || !semanticSlug) {
|
|
793
|
+
return {
|
|
794
|
+
ok: false,
|
|
795
|
+
detectorId: null,
|
|
796
|
+
semanticSlug: null,
|
|
797
|
+
mode: normMode,
|
|
798
|
+
configPath: activeWritePath,
|
|
799
|
+
config: loadHorizonConfig(options)
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
const existing = readJsonSafe(activeWritePath, {}, options) || {};
|
|
804
|
+
const nextDetectors = { ...(existing.detectors || {}) };
|
|
805
|
+
const nextClaudeDetectors = { ...(existing.claudeDetectors || {}) };
|
|
806
|
+
const allAliases = DETECTOR_SEMANTIC_ALIASES[normId] || [semanticSlug];
|
|
807
|
+
|
|
808
|
+
if (normMode === 'default') {
|
|
809
|
+
delete nextDetectors[normId];
|
|
810
|
+
for (const alias of allAliases) {
|
|
811
|
+
delete nextDetectors[alias];
|
|
812
|
+
}
|
|
813
|
+
delete nextClaudeDetectors[normId];
|
|
814
|
+
for (const alias of allAliases) {
|
|
815
|
+
delete nextClaudeDetectors[alias];
|
|
816
|
+
}
|
|
817
|
+
} else {
|
|
818
|
+
// If existing file was previously foreign-namespaced, migrate its detectors cleanly before writing claude-code namespace
|
|
819
|
+
if (
|
|
820
|
+
typeof existing.detectorNamespace === 'string' &&
|
|
821
|
+
existing.detectorNamespace.trim().toLowerCase() === 'antigravity'
|
|
822
|
+
) {
|
|
823
|
+
const migrated = {};
|
|
824
|
+
mergeForeignEditionDetectorMap(nextDetectors, migrated);
|
|
825
|
+
for (const k of Object.keys(nextDetectors)) {
|
|
826
|
+
delete nextDetectors[k];
|
|
827
|
+
}
|
|
828
|
+
for (const [mId, mMode] of Object.entries(migrated)) {
|
|
829
|
+
if (mMode !== 'default') {
|
|
830
|
+
nextDetectors[mId] = mMode;
|
|
831
|
+
nextDetectors[DETECTOR_SEMANTIC_SLUGS[mId]] = mMode;
|
|
832
|
+
nextClaudeDetectors[mId] = mMode;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
nextDetectors[normId] = normMode;
|
|
837
|
+
nextDetectors[semanticSlug] = normMode;
|
|
838
|
+
nextClaudeDetectors[normId] = normMode;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
const nextPayload = {
|
|
842
|
+
schemaVersion: '1.0.0',
|
|
843
|
+
...existing,
|
|
844
|
+
detectorNamespace: 'claude-code',
|
|
845
|
+
detectors: nextDetectors,
|
|
846
|
+
claudeDetectors: nextClaudeDetectors
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
const ok = writeJsonAtomic(activeWritePath, nextPayload, options);
|
|
850
|
+
return {
|
|
851
|
+
ok,
|
|
852
|
+
detectorId: normId,
|
|
853
|
+
semanticSlug,
|
|
854
|
+
mode: normMode,
|
|
855
|
+
configPath: activeWritePath,
|
|
856
|
+
config: loadHorizonConfig(options)
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Resets `.attention-horizon.json` detector overrides to defaults.
|
|
862
|
+
* @param {object} [options={}]
|
|
863
|
+
* @returns {{ ok: boolean, configPath: string }}
|
|
864
|
+
*/
|
|
865
|
+
export function resetHorizonConfig(options = {}) {
|
|
866
|
+
const { activeWritePath } = resolveHorizonConfigPaths(options);
|
|
867
|
+
const ok = unlinkSafe(activeWritePath, options);
|
|
868
|
+
return { ok, configPath: activeWritePath };
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Maps a detector trigger or runtime telemetry state to concrete native slash-command
|
|
873
|
+
* recovery prescriptions (`/rewind`, `/fork`, `/compact`, `/mcp`, `/context`, `/effort`, `/handoff` -> `/clear`).
|
|
874
|
+
*
|
|
875
|
+
* @param {object} [trigger={}]
|
|
876
|
+
* @returns {{
|
|
877
|
+
* category: string,
|
|
878
|
+
* primaryCommand: string,
|
|
879
|
+
* secondaryCommand: string | null,
|
|
880
|
+
* sequence?: string,
|
|
881
|
+
* plainTextInstruction?: string,
|
|
882
|
+
* clickableCopyAllowed: boolean,
|
|
883
|
+
* preservedFiles: string[],
|
|
884
|
+
* summary: string
|
|
885
|
+
* } | null}
|
|
886
|
+
*/
|
|
887
|
+
export function mapTriggerToNativeCommand(trigger = {}) {
|
|
888
|
+
const normId = normalizeDetectorId(trigger.detectorId);
|
|
889
|
+
const consecutiveFailures = Number(
|
|
890
|
+
trigger.consecutiveFailures ?? trigger.failureCount ?? (normId === 'DET-04' ? 3 : 0)
|
|
891
|
+
);
|
|
892
|
+
const contextUsedPct = Number(trigger.contextUsedPct ?? trigger.context_used_pct ?? 0);
|
|
893
|
+
const modifiedFiles = Array.isArray(trigger.modifiedFiles)
|
|
894
|
+
? trigger.modifiedFiles.filter(Boolean)
|
|
895
|
+
: [];
|
|
896
|
+
const unresolvedTasks = Array.isArray(trigger.unresolvedTasks)
|
|
897
|
+
? trigger.unresolvedTasks.filter(Boolean)
|
|
898
|
+
: [];
|
|
899
|
+
|
|
900
|
+
// 1. Poisoned Context Recovery (/rewind and /fork)
|
|
901
|
+
if (consecutiveFailures >= 3 || normId === 'DET-04' || normId === 'DET-05') {
|
|
902
|
+
return {
|
|
903
|
+
category: 'POISONED_CONTEXT_RECOVERY',
|
|
904
|
+
primaryCommand: '/rewind',
|
|
905
|
+
secondaryCommand: '/fork',
|
|
906
|
+
clickableCopyAllowed: true,
|
|
907
|
+
preservedFiles: modifiedFiles,
|
|
908
|
+
summary:
|
|
909
|
+
'Run `/rewind` to step back before the consecutive failure chain, or `/fork` to test a risky fix in isolation.'
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
// 2. Standing Overhead Trim (/mcp, /context, /effort)
|
|
914
|
+
if (
|
|
915
|
+
normId === 'DET-13' ||
|
|
916
|
+
normId === 'DET-12' ||
|
|
917
|
+
Boolean(trigger.instructionsOverBudget) ||
|
|
918
|
+
Boolean(trigger.idleMcpBloat)
|
|
919
|
+
) {
|
|
920
|
+
const isEffort = normId === 'DET-12' || Boolean(trigger.effortStepDown);
|
|
921
|
+
return {
|
|
922
|
+
category: 'STANDING_OVERHEAD_TRIM',
|
|
923
|
+
primaryCommand: isEffort ? '/effort' : '/mcp',
|
|
924
|
+
secondaryCommand: isEffort ? '/mcp' : '/context',
|
|
925
|
+
clickableCopyAllowed: true,
|
|
926
|
+
preservedFiles: modifiedFiles,
|
|
927
|
+
summary: isEffort
|
|
928
|
+
? 'Run `/effort` to step down reasoning cost or switch models before the 100k token cliff.'
|
|
929
|
+
: 'Run `/mcp` and `/context` to prune idle MCP tool manifests, or `/effort` to step down reasoning cost on routine edits.'
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// 3. Clean Session Baton (/handoff -> /clear)
|
|
934
|
+
if (Boolean(trigger.milestoneComplete) || Boolean(trigger.cacheEfficiencyDropped)) {
|
|
935
|
+
return {
|
|
936
|
+
category: 'CLEAN_SESSION_BATON',
|
|
937
|
+
primaryCommand: '/handoff',
|
|
938
|
+
secondaryCommand: '/clear',
|
|
939
|
+
sequence: '/handoff -> /clear',
|
|
940
|
+
clickableCopyAllowed: true,
|
|
941
|
+
preservedFiles: modifiedFiles,
|
|
942
|
+
summary:
|
|
943
|
+
'Run `/handoff` (or `/attention-horizon:handoff`) to write HANDOFF.md, followed immediately by `/clear`.'
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// 4. Scoped Context Compaction (/compact)
|
|
948
|
+
const warnThreshold = Number(trigger.horizonWarningThresholdPct || 70);
|
|
949
|
+
if (contextUsedPct >= warnThreshold || normId === 'DET-11') {
|
|
950
|
+
const fileScope =
|
|
951
|
+
modifiedFiles.length > 0 ? modifiedFiles.join(', ') : 'active modified files';
|
|
952
|
+
const taskScope =
|
|
953
|
+
unresolvedTasks.length > 0 ? `; unresolved tasks: ${unresolvedTasks.join('; ')}` : '';
|
|
954
|
+
const scopedCompactCmd = `/compact Preserve modified files (${fileScope})${taskScope} and active architectural invariants.`;
|
|
955
|
+
return {
|
|
956
|
+
category: 'SCOPED_CONTEXT_COMPACTION',
|
|
957
|
+
primaryCommand: scopedCompactCmd,
|
|
958
|
+
secondaryCommand: '/handoff',
|
|
959
|
+
plainTextInstruction: 'Type /compact in the chat box and press Enter',
|
|
960
|
+
clickableCopyAllowed: true,
|
|
961
|
+
preservedFiles: modifiedFiles,
|
|
962
|
+
summary: `Preserve ${fileScope}${taskScope} during context compaction.`
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
return null;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Applies `.attention-horizon.json` per-detector override (`off` | `warn` | `block`)
|
|
971
|
+
* to a circuit breaker verdict and attaches any native slash-command prescription.
|
|
972
|
+
*
|
|
973
|
+
* @param {object} verdict
|
|
974
|
+
* @param {object} [config]
|
|
975
|
+
* @returns {object}
|
|
976
|
+
*/
|
|
977
|
+
export function applyDetectorOverride(verdict, config = null) {
|
|
978
|
+
if (!verdict || !verdict.triggered || !verdict.detectorId) {
|
|
979
|
+
return verdict;
|
|
980
|
+
}
|
|
981
|
+
const normId = normalizeDetectorId(verdict.detectorId);
|
|
982
|
+
const semanticSlug = normId ? DETECTOR_SEMANTIC_SLUGS[normId] : null;
|
|
983
|
+
const mode = normId ? resolveDetectorOverride(normId, config) : 'default';
|
|
984
|
+
|
|
985
|
+
if (mode === 'off') {
|
|
986
|
+
return {
|
|
987
|
+
detectorId: null,
|
|
988
|
+
semanticSlug: null,
|
|
989
|
+
triggered: false,
|
|
990
|
+
decision: 'allow',
|
|
991
|
+
blocked: false,
|
|
992
|
+
overriddenByConfig: 'off',
|
|
993
|
+
suppressedDetectorId: normId,
|
|
994
|
+
suppressedSemanticSlug: semanticSlug,
|
|
995
|
+
reason: null
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
const prescription =
|
|
1000
|
+
verdict.prescription || mapTriggerToNativeCommand({ detectorId: normId }) || null;
|
|
1001
|
+
const baseVerdict = {
|
|
1002
|
+
...verdict,
|
|
1003
|
+
...(semanticSlug ? { semanticSlug } : {}),
|
|
1004
|
+
...(prescription ? { prescription } : {})
|
|
1005
|
+
};
|
|
1006
|
+
|
|
1007
|
+
if (!mode || mode === 'default') {
|
|
1008
|
+
return baseVerdict;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
if (mode === 'warn') {
|
|
1012
|
+
return {
|
|
1013
|
+
...baseVerdict,
|
|
1014
|
+
decision: 'allow',
|
|
1015
|
+
blocked: false,
|
|
1016
|
+
advisory: true,
|
|
1017
|
+
overriddenByConfig: 'warn',
|
|
1018
|
+
reason: `[Advisory Override (${normId}=warn)] ${verdict.reason || ''}`.trim()
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
if (mode === 'block') {
|
|
1023
|
+
return {
|
|
1024
|
+
...baseVerdict,
|
|
1025
|
+
decision: 'deny',
|
|
1026
|
+
blocked: true,
|
|
1027
|
+
advisory: false,
|
|
1028
|
+
overriddenByConfig: 'block'
|
|
1029
|
+
};
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
return baseVerdict;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
export default {
|
|
1036
|
+
CANONICAL_DETECTOR_IDS,
|
|
1037
|
+
RING0_SECURITY_DETECTOR_IDS,
|
|
1038
|
+
DETECTOR_SEMANTIC_SLUGS,
|
|
1039
|
+
DETECTOR_SEMANTIC_ALIASES,
|
|
1040
|
+
SEMANTIC_SLUG_TO_DETECTOR_ID,
|
|
1041
|
+
ANTIGRAVITY_DETECTOR_TO_SEMANTIC_SLUG,
|
|
1042
|
+
VALID_DETECTOR_MODES,
|
|
1043
|
+
DEFAULT_HORIZON_CONFIG,
|
|
1044
|
+
normalizeDetectorId,
|
|
1045
|
+
resolveDetectorSlug,
|
|
1046
|
+
normalizeDetectorMode,
|
|
1047
|
+
normalizeConfiguredFlightMode,
|
|
1048
|
+
resolveDetectorOverride,
|
|
1049
|
+
resolveHorizonConfigPaths,
|
|
1050
|
+
loadHorizonConfig,
|
|
1051
|
+
setDetectorOverride,
|
|
1052
|
+
resetHorizonConfig,
|
|
1053
|
+
mapTriggerToNativeCommand,
|
|
1054
|
+
applyDetectorOverride
|
|
1055
|
+
};
|