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,239 @@
|
|
|
1
|
+
import { isTestFilePath } from '../detectors_det01_to_16.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Phase 2.1: Contradiction Auditor & Immutable Evaluation Protection (`src/core/coach/contradiction_auditor.mjs`)
|
|
5
|
+
*
|
|
6
|
+
* Feature #19 (Top Unbuilt Gap #3):
|
|
7
|
+
* - Prevents self-improving agents (RSI) from injecting opposing/contradictory directives
|
|
8
|
+
* into `AGENTS.md`, `.agents/CONTEXT.md`, or `.agents/skills/<skill>/SKILL.md`.
|
|
9
|
+
* - Protects evaluation and unit test suites (`test/*`, `tests/*`, `*.test.*`) from
|
|
10
|
+
* uninvited agent mutation when the user's prompt did not authorize modifying tests.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const STOP_WORDS = new Set([
|
|
14
|
+
'always', 'never', 'must', 'should', 'dont', 'do', 'not', 'stop', 'avoid',
|
|
15
|
+
'use', 'using', 'prefer', 'when', 'while', 'with', 'from', 'into', 'that',
|
|
16
|
+
'this', 'only', 'ever', 'please', 'make', 'sure', 'ensure', 'keep', 'all',
|
|
17
|
+
'any', 'the', 'and', 'for', 'are', 'is', 'in', 'on', 'to', 'of', 'by', 'as'
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
const NEGATIVE_POLARITY_REGEX =
|
|
21
|
+
/\b(never|must\s+not|do\s+not|don'?t|stop\s+(?:using|doing|calling|writing|adding)|avoid|forbid|forbidden|prohibited|disallow)\b/i;
|
|
22
|
+
|
|
23
|
+
const POSITIVE_POLARITY_REGEX =
|
|
24
|
+
/\b(always|must(?!\s+not)|require|required|mandatory|enforce|prefer|use)\b/i;
|
|
25
|
+
|
|
26
|
+
const INSTEAD_OF_REGEX =
|
|
27
|
+
/\b(?:always\s+)?(?:use|prefer)\s+([^.,;!\n]+?)\s+instead\s+of\s+([^.,;!\n]+)/i;
|
|
28
|
+
|
|
29
|
+
function extractSubjectTokens(text = '') {
|
|
30
|
+
return String(text || '')
|
|
31
|
+
.toLowerCase()
|
|
32
|
+
.replace(/\*+/g, ' ')
|
|
33
|
+
.replace(/\[rsi:[^\]]+\]/gi, ' ')
|
|
34
|
+
.replace(/\(trigger:[^)]+\)/gi, ' ')
|
|
35
|
+
.replace(/[^a-z0-9/_-]+/g, ' ')
|
|
36
|
+
.split(/\s+/)
|
|
37
|
+
.filter((tok) => tok.length >= 2 && !STOP_WORDS.has(tok));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Normalizes a rule directive into polarity (`POSITIVE` | `NEGATIVE` | `NEUTRAL`),
|
|
42
|
+
* subject tokens, and optional `insteadOfPair` (`{ preferred, forbidden }`).
|
|
43
|
+
*/
|
|
44
|
+
export function parseDirectivePolarity(rawDirective = '') {
|
|
45
|
+
const cleaned = String(rawDirective || '')
|
|
46
|
+
.replace(/^\s*[-*+]\s*/, '')
|
|
47
|
+
.replace(/^\s*\d+\.\s*/, '')
|
|
48
|
+
.replace(/\*\*\[RSI:[^\]]+\]\*\*/gi, '')
|
|
49
|
+
.replace(/\*\(trigger:[\s\S]*?\)\*/gi, '')
|
|
50
|
+
.trim();
|
|
51
|
+
|
|
52
|
+
const insteadMatch = cleaned.match(INSTEAD_OF_REGEX);
|
|
53
|
+
if (insteadMatch) {
|
|
54
|
+
const stripPrep = (s) =>
|
|
55
|
+
String(s || '')
|
|
56
|
+
.replace(/\s+(?:for|in|when|while|on|with|during|across|inside|to)\b.*$/i, '')
|
|
57
|
+
.trim()
|
|
58
|
+
.toLowerCase();
|
|
59
|
+
const preferred = stripPrep(insteadMatch[1]);
|
|
60
|
+
const forbidden = stripPrep(insteadMatch[2]);
|
|
61
|
+
return {
|
|
62
|
+
polarity: 'INSTEAD_OF',
|
|
63
|
+
preferred,
|
|
64
|
+
forbidden,
|
|
65
|
+
subjectTokens: extractSubjectTokens(`${preferred} ${forbidden}`),
|
|
66
|
+
cleanedText: cleaned
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const isNegative = NEGATIVE_POLARITY_REGEX.test(cleaned);
|
|
71
|
+
const isPositive = !isNegative && POSITIVE_POLARITY_REGEX.test(cleaned);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
polarity: isNegative ? 'NEGATIVE' : isPositive ? 'POSITIVE' : 'NEUTRAL',
|
|
75
|
+
preferred: null,
|
|
76
|
+
forbidden: null,
|
|
77
|
+
subjectTokens: extractSubjectTokens(cleaned),
|
|
78
|
+
cleanedText: cleaned
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Audits a candidate rule or instinct against existing rules text (or array of rules)
|
|
84
|
+
* for direct polarity contradictions or `instead of` inversions.
|
|
85
|
+
*/
|
|
86
|
+
export function auditRuleContradictions(candidateRule = '', existingRulesInput = []) {
|
|
87
|
+
const candidateText =
|
|
88
|
+
typeof candidateRule === 'string'
|
|
89
|
+
? candidateRule
|
|
90
|
+
: String(candidateRule?.directive_action || candidateRule?.ruleText || '');
|
|
91
|
+
|
|
92
|
+
const parsedCandidate = parseDirectivePolarity(candidateText);
|
|
93
|
+
const rawLines = Array.isArray(existingRulesInput)
|
|
94
|
+
? existingRulesInput
|
|
95
|
+
: String(existingRulesInput || '').split(/\r?\n/);
|
|
96
|
+
|
|
97
|
+
const conflicts = [];
|
|
98
|
+
let isDuplicate = false;
|
|
99
|
+
|
|
100
|
+
for (const rawEntry of rawLines) {
|
|
101
|
+
const lineText =
|
|
102
|
+
typeof rawEntry === 'string'
|
|
103
|
+
? rawEntry
|
|
104
|
+
: String(rawEntry?.directive_action || rawEntry?.ruleText || '');
|
|
105
|
+
const trimmed = lineText.trim();
|
|
106
|
+
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('```') || trimmed.startsWith('<!--')) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const parsedExisting = parseDirectivePolarity(trimmed);
|
|
111
|
+
if (!parsedExisting.cleanedText || parsedExisting.subjectTokens.length === 0) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (
|
|
116
|
+
parsedExisting.cleanedText.toLowerCase().replace(/\s+/g, ' ') ===
|
|
117
|
+
parsedCandidate.cleanedText.toLowerCase().replace(/\s+/g, ' ')
|
|
118
|
+
) {
|
|
119
|
+
isDuplicate = true;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Case 1: Both are INSTEAD_OF directives with inverted preferred/forbidden targets
|
|
124
|
+
if (
|
|
125
|
+
parsedCandidate.polarity === 'INSTEAD_OF' &&
|
|
126
|
+
parsedExisting.polarity === 'INSTEAD_OF'
|
|
127
|
+
) {
|
|
128
|
+
if (
|
|
129
|
+
parsedCandidate.preferred === parsedExisting.forbidden ||
|
|
130
|
+
parsedCandidate.forbidden === parsedExisting.preferred
|
|
131
|
+
) {
|
|
132
|
+
conflicts.push({
|
|
133
|
+
existingRule: parsedExisting.cleanedText,
|
|
134
|
+
candidateRule: parsedCandidate.cleanedText,
|
|
135
|
+
reason: `Inverted preference ("${parsedCandidate.preferred} instead of ${parsedCandidate.forbidden}" contradicts "${parsedExisting.preferred} instead of ${parsedExisting.forbidden}")`,
|
|
136
|
+
overlapTokens: [parsedCandidate.preferred, parsedCandidate.forbidden]
|
|
137
|
+
});
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Case 2: INSTEAD_OF vs POSITIVE/NEGATIVE
|
|
143
|
+
if (parsedCandidate.polarity === 'INSTEAD_OF' && parsedExisting.polarity === 'POSITIVE') {
|
|
144
|
+
const existingJoined = parsedExisting.subjectTokens.join(' ');
|
|
145
|
+
if (existingJoined.includes(parsedCandidate.forbidden) && !existingJoined.includes(parsedCandidate.preferred)) {
|
|
146
|
+
conflicts.push({
|
|
147
|
+
existingRule: parsedExisting.cleanedText,
|
|
148
|
+
candidateRule: parsedCandidate.cleanedText,
|
|
149
|
+
reason: `Candidate forbids "${parsedCandidate.forbidden}" which an existing rule requires`,
|
|
150
|
+
overlapTokens: [parsedCandidate.forbidden]
|
|
151
|
+
});
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (parsedExisting.polarity === 'INSTEAD_OF' && parsedCandidate.polarity === 'POSITIVE') {
|
|
157
|
+
const candidateJoined = parsedCandidate.subjectTokens.join(' ');
|
|
158
|
+
if (candidateJoined.includes(parsedExisting.forbidden) && !candidateJoined.includes(parsedExisting.preferred)) {
|
|
159
|
+
conflicts.push({
|
|
160
|
+
existingRule: parsedExisting.cleanedText,
|
|
161
|
+
candidateRule: parsedCandidate.cleanedText,
|
|
162
|
+
reason: `Candidate requires "${parsedExisting.forbidden}" which an existing rule forbids in favor of "${parsedExisting.preferred}"`,
|
|
163
|
+
overlapTokens: [parsedExisting.forbidden]
|
|
164
|
+
});
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Case 3: Direct Polarity Inversion (POSITIVE vs NEGATIVE) on high subject token overlap
|
|
170
|
+
const oppositePolarity =
|
|
171
|
+
(parsedCandidate.polarity === 'POSITIVE' && parsedExisting.polarity === 'NEGATIVE') ||
|
|
172
|
+
(parsedCandidate.polarity === 'NEGATIVE' && parsedExisting.polarity === 'POSITIVE');
|
|
173
|
+
|
|
174
|
+
if (oppositePolarity && parsedCandidate.subjectTokens.length > 0) {
|
|
175
|
+
const existingSet = new Set(parsedExisting.subjectTokens);
|
|
176
|
+
const overlapTokens = parsedCandidate.subjectTokens.filter((t) => existingSet.has(t));
|
|
177
|
+
const minLen = Math.min(parsedCandidate.subjectTokens.length, parsedExisting.subjectTokens.length);
|
|
178
|
+
const overlapRatio = minLen > 0 ? overlapTokens.length / minLen : 0;
|
|
179
|
+
|
|
180
|
+
if (overlapTokens.length >= 1 && overlapRatio >= 0.6) {
|
|
181
|
+
conflicts.push({
|
|
182
|
+
existingRule: parsedExisting.cleanedText,
|
|
183
|
+
candidateRule: parsedCandidate.cleanedText,
|
|
184
|
+
reason: `Opposing directive polarity (${parsedCandidate.polarity} vs ${parsedExisting.polarity}) on subject [${overlapTokens.join(', ')}]`,
|
|
185
|
+
overlapTokens
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
hasContradiction: conflicts.length > 0,
|
|
193
|
+
conflictCount: conflicts.length,
|
|
194
|
+
conflicts,
|
|
195
|
+
isDuplicate,
|
|
196
|
+
candidatePolarity: parsedCandidate.polarity
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Protects evaluation suites (`test/*`, `tests/*`, `*.test.*`) from unauthorized mutation
|
|
202
|
+
* when the user prompt did not explicitly ask to write, update, or fix tests.
|
|
203
|
+
*/
|
|
204
|
+
export function auditTestSuiteImmutability({ modifiedFiles = [], userPrompt = '' } = {}) {
|
|
205
|
+
const promptStr = String(userPrompt || '');
|
|
206
|
+
const userAuthorized =
|
|
207
|
+
/\b(test|tests|unit\s+test|spec|specs|assertion|assertions|verify_|test\s+suite|e2e)\b/i.test(promptStr);
|
|
208
|
+
|
|
209
|
+
const unauthorizedTestFiles = [];
|
|
210
|
+
for (const item of Array.isArray(modifiedFiles) ? modifiedFiles : []) {
|
|
211
|
+
const filePath = typeof item === 'string' ? item : String(item?.path || item?.filePath || '');
|
|
212
|
+
if (!filePath) continue;
|
|
213
|
+
if (isTestFilePath(filePath) && !userAuthorized) {
|
|
214
|
+
unauthorizedTestFiles.push(filePath);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
violated: unauthorizedTestFiles.length > 0,
|
|
220
|
+
userAuthorized,
|
|
221
|
+
unauthorizedTestFiles,
|
|
222
|
+
message:
|
|
223
|
+
unauthorizedTestFiles.length > 0
|
|
224
|
+
? `🛡️ Immutable Evaluation Guard: Agent modified test file(s) [${unauthorizedTestFiles.join(', ')}] without user test authorization.`
|
|
225
|
+
: ''
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export class ContradictionAuditor {
|
|
230
|
+
auditRule(candidateRule = '', existingRulesInput = []) {
|
|
231
|
+
return auditRuleContradictions(candidateRule, existingRulesInput);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
auditTestBoundary({ modifiedFiles = [], userPrompt = '' } = {}) {
|
|
235
|
+
return auditTestSuiteImmutability({ modifiedFiles, userPrompt });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export default ContradictionAuditor;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { getHomeDir } from '../paths.mjs';
|
|
4
|
+
import { openLocalHorizonDb } from '../sqlite_client.mjs';
|
|
5
|
+
import {
|
|
6
|
+
evaluateDet08CacheGravityAndEffort,
|
|
7
|
+
WARM_CACHE_GRAVITY_FLOOR_TOKENS,
|
|
8
|
+
resolveCarbonPricingForTimestamp
|
|
9
|
+
} from '../shared/detectors.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Rank 7 (DET-08): KV-Cache Gravity & Same-Model Between-Turn Effort Advisor
|
|
13
|
+
* (`src/core/coach/effort_advisor.mjs`)
|
|
14
|
+
*
|
|
15
|
+
* Triggered from BOTH `PostInvocation` and the `.pb` `TrajectoryWatcher`:
|
|
16
|
+
* - Turn 0-2 (`cachedTokens <= 3529`): Routes simple single-file/typo tasks to `gemini-3.8-flash`
|
|
17
|
+
* before Frontier KV-cache gravity locks in.
|
|
18
|
+
* - Mid-Session (`cachedTokens > 3529`): NEVER switches main-thread model ID to Flash (which
|
|
19
|
+
* forfeits the 95% warm cache discount and costs up to 7.5x more in input). When `HysteresisLock`
|
|
20
|
+
* is clear (`recentTestFailures < 2` and `det05Oscillation === false`), surfaces a 1-click
|
|
21
|
+
* `[Downshift: /effort low (-68% output burn, keeps warm cache)]` chip in Box 3 with 3-strike
|
|
22
|
+
* SQLite dismissal muting.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export { WARM_CACHE_GRAVITY_FLOOR_TOKENS, resolveCarbonPricingForTimestamp };
|
|
26
|
+
|
|
27
|
+
export function getDefaultEffortAdvisorDbPath(homeDir = getHomeDir()) {
|
|
28
|
+
return path.join(homeDir, '.gemini', 'antigravity', 'agy-horizon', 'effort_advisor.sqlite');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class EffortAdvisor {
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
this.homeDir = options.homeDir || getHomeDir();
|
|
34
|
+
this.dbPath = options.dbPath || getDefaultEffortAdvisorDbPath(this.homeDir);
|
|
35
|
+
this._initDb();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_initDb() {
|
|
39
|
+
fs.mkdirSync(path.dirname(this.dbPath), { recursive: true });
|
|
40
|
+
const db = openLocalHorizonDb(this.dbPath, { readOnly: false, busyTimeoutMs: 3000 });
|
|
41
|
+
try {
|
|
42
|
+
db.exec(`
|
|
43
|
+
CREATE TABLE IF NOT EXISTS effort_dismissals (
|
|
44
|
+
session_id TEXT PRIMARY KEY,
|
|
45
|
+
dismissal_count INTEGER NOT NULL DEFAULT 0,
|
|
46
|
+
updated_at TEXT NOT NULL
|
|
47
|
+
);
|
|
48
|
+
`);
|
|
49
|
+
} finally {
|
|
50
|
+
db.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getDismissalCount(sessionId = 'default_session') {
|
|
55
|
+
if (!fs.existsSync(this.dbPath)) return 0;
|
|
56
|
+
const db = openLocalHorizonDb(this.dbPath, { readOnly: true, busyTimeoutMs: 3000 });
|
|
57
|
+
try {
|
|
58
|
+
const row = db.prepare('SELECT dismissal_count AS c FROM effort_dismissals WHERE session_id = ?').get(String(sessionId));
|
|
59
|
+
return Number(row?.c || 0);
|
|
60
|
+
} finally {
|
|
61
|
+
db.close();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
recordDismissal(sessionId = 'default_session') {
|
|
66
|
+
const now = new Date().toISOString();
|
|
67
|
+
const db = openLocalHorizonDb(this.dbPath, { readOnly: false, busyTimeoutMs: 3000 });
|
|
68
|
+
try {
|
|
69
|
+
db.prepare(`
|
|
70
|
+
INSERT INTO effort_dismissals (session_id, dismissal_count, updated_at)
|
|
71
|
+
VALUES (?, 1, ?)
|
|
72
|
+
ON CONFLICT(session_id) DO UPDATE SET
|
|
73
|
+
dismissal_count = dismissal_count + 1,
|
|
74
|
+
updated_at = excluded.updated_at
|
|
75
|
+
`).run(String(sessionId), now);
|
|
76
|
+
} finally {
|
|
77
|
+
db.close();
|
|
78
|
+
}
|
|
79
|
+
const count = this.getDismissalCount(sessionId);
|
|
80
|
+
return {
|
|
81
|
+
sessionId: String(sessionId),
|
|
82
|
+
dismissalStrikes: count,
|
|
83
|
+
muted: count >= 3
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
resetDismissals(sessionId = 'default_session') {
|
|
88
|
+
const db = openLocalHorizonDb(this.dbPath, { readOnly: false, busyTimeoutMs: 3000 });
|
|
89
|
+
try {
|
|
90
|
+
db.prepare('DELETE FROM effort_dismissals WHERE session_id = ?').run(String(sessionId));
|
|
91
|
+
} finally {
|
|
92
|
+
db.close();
|
|
93
|
+
}
|
|
94
|
+
return { sessionId: String(sessionId), dismissalStrikes: 0, muted: false };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
evaluate({
|
|
98
|
+
sessionId = 'default_session',
|
|
99
|
+
turnCount = 0,
|
|
100
|
+
cachedTokens = 0,
|
|
101
|
+
activeModel = 'gemini-4-carbon',
|
|
102
|
+
activeEffort = 'high',
|
|
103
|
+
isSimpleTask = true,
|
|
104
|
+
recentTestFailures = 0,
|
|
105
|
+
det05Oscillation = false,
|
|
106
|
+
timestampUtc = '2026-11-10T00:00:00Z'
|
|
107
|
+
} = {}) {
|
|
108
|
+
const dismissalStrikes = this.getDismissalCount(sessionId);
|
|
109
|
+
const res = evaluateDet08CacheGravityAndEffort({
|
|
110
|
+
turnCount,
|
|
111
|
+
cachedTokens,
|
|
112
|
+
activeModel,
|
|
113
|
+
activeEffort,
|
|
114
|
+
isSimpleTask,
|
|
115
|
+
recentTestFailures,
|
|
116
|
+
det05Oscillation,
|
|
117
|
+
dismissalStrikes,
|
|
118
|
+
timestampUtc
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
...res,
|
|
123
|
+
sessionId: String(sessionId),
|
|
124
|
+
dismissalStrikes,
|
|
125
|
+
box3Chip: res.lane === 'MID_SESSION_SAME_MODEL_EFFORT' && res.triggered
|
|
126
|
+
? {
|
|
127
|
+
actionId: 'det08-effort-downshift',
|
|
128
|
+
label: res.chipLabel,
|
|
129
|
+
commandText: '/effort low',
|
|
130
|
+
dismissable: true,
|
|
131
|
+
dismissalStrikes
|
|
132
|
+
}
|
|
133
|
+
: null
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function evaluateBetweenTurnEffortAdvisor(params = {}, options = {}) {
|
|
139
|
+
const advisor = new EffortAdvisor(options);
|
|
140
|
+
return advisor.evaluate(params);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export default EffortAdvisor;
|