hydra-nano 0.2.0 → 0.3.1

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.
@@ -8,6 +8,10 @@
8
8
  // opencode ~/.config/opencode/opencode.json(c) mcp.hydra ~/.config/opencode/plugins/hydra.js tool.execute.before
9
9
  // gemini-cli ~/.gemini/settings.json mcpServers.hydra same file, hooks.BeforeTool on run_shell_command
10
10
  // antigravity ~/.gemini/antigravity/mcp_config.json none known; the MCP server's instructions guide it
11
+ // antigravity-ide ~/.gemini/config/mcp_config.json none known; the skill in ~/.gemini/config/skills/hydra guides it
12
+ //
13
+ // Antigravity IDE (com.google.antigravity-ide, data in ~/.gemini/antigravity-ide) is a different app from
14
+ // the older Antigravity (~/.gemini/antigravity): it reads MCP servers and skills from ~/.gemini/config.
11
15
  //
12
16
  // Every file Hydra edits is backed up once as <file>.before-hydra, written atomically, and only
13
17
  // Hydra's own entries are added or removed. A file Hydra cannot edit safely (JSON with comments, or
@@ -24,7 +28,8 @@ const END = '# <<< hydra <<<';
24
28
  /** Hydra's own copy of its scripts, which every tool's MCP entry and hook points at. */
25
29
  export const sharedScripts = () => path.join(process.env.HYDRA_HOME ?? path.join(os.homedir(), '.hydra'), 'scripts');
26
30
 
27
- /** Copy the skill's scripts to sharedScripts(), so registrations never point into an npx cache. */
31
+ /** Copy the skill's scripts to sharedScripts(), so registrations never point into an npx cache.
32
+ * Its SKILL.md goes beside them, for tools that load skills from a folder of their own. */
28
33
  export function installSharedScripts(from) {
29
34
  const target = sharedScripts();
30
35
  if (path.resolve(from) === path.resolve(target)) return target; // Already running from the shared copy.
@@ -33,6 +38,8 @@ export function installSharedScripts(from) {
33
38
  cpSync(from, `${target}.hydra-tmp`, { recursive: true });
34
39
  rmSync(target, { recursive: true, force: true });
35
40
  renameSync(`${target}.hydra-tmp`, target);
41
+ const skill = path.join(from, '..', 'SKILL.md');
42
+ if (existsSync(skill)) copyFileSync(skill, path.join(path.dirname(target), 'SKILL.md'));
36
43
  return target;
37
44
  }
38
45
 
@@ -186,6 +193,33 @@ export const Hydra = async () => ({
186
193
  };
187
194
  }
188
195
 
196
+ /** The hydra skill folder (SKILL.md and the scripts it names) in a tool's own skills folder.
197
+ * An older hydra skill there is replaced; a folder that is not the hydra skill is left alone. */
198
+ function skillFolder(dir, scripts) {
199
+ const skillFile = path.join(dir, 'SKILL.md');
200
+ const ours = () => existsSync(skillFile) && /^name:\s*hydra\s*$/m.test(readFileSync(skillFile, 'utf8'));
201
+ return {
202
+ install() {
203
+ const source = path.join(path.dirname(scripts), 'SKILL.md');
204
+ if (!existsSync(source)) throw new Manual(dir, 'npx github:rahul05ranjan/hydra', `needs the hydra skill, but ${source} is missing; run the installer again`);
205
+ if (existsSync(dir) && !ours()) throw new Manual(dir, `the hydra skill folder (SKILL.md and scripts/) from ${path.dirname(scripts)}`, 'exists and is not the hydra skill');
206
+ rmSync(`${dir}.hydra-tmp`, { recursive: true, force: true });
207
+ mkdirSync(`${dir}.hydra-tmp`, { recursive: true });
208
+ copyFileSync(source, path.join(`${dir}.hydra-tmp`, 'SKILL.md'));
209
+ cpSync(scripts, path.join(`${dir}.hydra-tmp`, 'scripts'), { recursive: true });
210
+ rmSync(dir, { recursive: true, force: true });
211
+ renameSync(`${dir}.hydra-tmp`, dir);
212
+ return `${dir}: skill`;
213
+ },
214
+ remove() {
215
+ if (!ours()) return null;
216
+ rmSync(dir, { recursive: true, force: true });
217
+ return `${dir}: skill`;
218
+ },
219
+ status: () => ours() && existsSync(path.join(dir, 'scripts', 'hydra.mjs')),
220
+ };
221
+ }
222
+
189
223
  const opencodeConfig = dir => ['opencode.json', 'opencode.jsonc'].map(name => path.join(dir, name)).find(existsSync) ?? path.join(dir, 'opencode.json');
190
224
 
191
225
  /** Every tool Hydra can join, with where it lives and what Hydra registers there. */
@@ -217,6 +251,9 @@ export function tools(scripts = sharedScripts()) {
217
251
  BeforeTool: [{ matcher: 'run_shell_command', hooks: [{ name: 'hydra', type: 'command', command: hookCommand(scripts, 'route-tests.mjs', 'gemini'), timeout: 10_000 }] }] }) } },
218
252
  antigravity: { dir: path.join(gemini, 'antigravity'), parts: {
219
253
  mcp: jsonServer(path.join(gemini, 'antigravity', 'mcp_config.json'), 'mcpServers', mcp) } },
254
+ 'antigravity-ide': { dir: path.join(gemini, 'antigravity-ide'), parts: {
255
+ mcp: jsonServer(path.join(gemini, 'config', 'mcp_config.json'), 'mcpServers', mcp),
256
+ skill: skillFolder(path.join(gemini, 'config', 'skills', 'hydra'), scripts) } },
220
257
  };
221
258
  }
222
259
 
@@ -1,8 +1,9 @@
1
1
  // Local telemetry: one JSON line per Hydra call or hook decision in ~/.hydra/ledger.jsonl.
2
2
  // Nothing leaves the machine. HYDRA_TELEMETRY=0 turns recording off.
3
3
  // kind 'call' (or no kind, rows written before telemetry v2): a head answered; raw/returned token estimates.
4
- // kind 'hook': the PreToolUse hook turned a command back ('deny'), let HYDRA_RAW=1 through ('raw') or
5
- // passed it ('pass', recorded without the command). hookMs is the hook process's whole lifetime.
4
+ // kind 'hook': the PreToolUse hook turned a command back ('deny'), let HYDRA_RAW=1 through ('raw'), let a
5
+ // run through that its own pipeline already cuts to a few lines ('trimmed') or passed it ('pass', recorded
6
+ // without the command). hookMs is the hook process's whole lifetime.
6
7
  // kind 'error': a command failed (model unavailable, crash).
7
8
  import { appendFileSync, mkdirSync, readFileSync } from 'node:fs';
8
9
  import os from 'node:os';
@@ -20,12 +21,16 @@ export const enabled = () => process.env.HYDRA_TELEMETRY !== '0';
20
21
  * AI_AGENT differs inside hooks (claude-code_<version>_harness) and the agent's shell (..._agent),
21
22
  * so `agent` keeps only the family and `agentDetail` the full value. */
22
23
  export function caller(session = process.env.CLAUDE_CODE_SESSION_ID) {
24
+ // Antigravity sets no agent variable; macOS passes the launching app's bundle id to every child process.
25
+ const app = /^com\.google\.antigravity(?:-ide)?$/.test(process.env.__CFBundleIdentifier ?? '') ? process.env.__CFBundleIdentifier : undefined;
23
26
  // HYDRA_AGENT is the MCP client's own name (codex-mcp-client, gemini-cli, opencode, ...), set by mcp.mjs.
24
- const detail = process.env.HYDRA_AGENT || process.env.AI_AGENT;
27
+ const detail = process.env.HYDRA_AGENT || process.env.AI_AGENT || (process.env.CLAUDECODE ? undefined : app);
25
28
  const agent = process.env.HYDRA_AGENT ? detail.toLowerCase().replace(/[-_ ]?mcp[-_ ]?client$/, '')
26
- : detail ? detail.split('_')[0] : process.env.CLAUDECODE ? 'claude-code' : 'shell';
29
+ : process.env.AI_AGENT ? detail.split('_')[0] : process.env.CLAUDECODE ? 'claude-code' : app ? 'antigravity' : 'shell';
30
+ // An app that starts the agent for you (T3 Code runs Claude Code, Codex, ...) is kept apart from the agent itself.
31
+ const launcher = process.env.__CFBundleIdentifier === 'com.t3tools.t3code' || process.env.T3CODE_HOME ? 't3code' : undefined;
27
32
  return { agent, ...(detail ? { agentDetail: detail } : {}), ...(process.env.HYDRA_VIA ? { via: process.env.HYDRA_VIA } : {}),
28
- ...(session ? { session } : {}) };
33
+ ...(launcher ? { app: launcher } : {}), ...(session ? { session } : {}) };
29
34
  }
30
35
 
31
36
  export function record(entry) {
@@ -115,8 +120,9 @@ export function findings(all = rows(), { since } = {}) {
115
120
  && call.head === 'test' && call.ts >= deny.ts && Date.parse(call.ts) - Date.parse(deny.ts) <= 5 * 60_000));
116
121
  const agents = {};
117
122
  for (const row of recent) {
118
- const agent = agents[row.agent ?? 'unknown'] ??= { calls: 0, viaMcp: 0, denials: 0, sessions: 0 };
123
+ const agent = agents[row.agent ?? 'unknown'] ??= { calls: 0, viaMcp: 0, denials: 0, sessions: 0, fromApps: {} };
119
124
  if (isCall(row)) { agent.calls += 1; if (row.via === 'mcp') agent.viaMcp += 1; }
125
+ if (isCall(row) && row.app) agent.fromApps[row.app] = (agent.fromApps[row.app] ?? 0) + 1;
120
126
  if (row.kind === 'hook' && row.decision === 'deny') agent.denials += 1;
121
127
  if (row.kind === 'session') agent.sessions += 1;
122
128
  }
@@ -126,6 +132,7 @@ export function findings(all = rows(), { since } = {}) {
126
132
  since: since ?? recent[0]?.ts ?? null, calls: calls.length, perHead, agents,
127
133
  hooks: { decisions: hooks.length, p50Ms: percentile(hookMs, 0.5), p90Ms: percentile(hookMs, 0.9),
128
134
  denials: denials.length, followedByHydra: followed.length,
135
+ trimmed: hooks.filter(row => row.decision === 'trimmed').length,
129
136
  rawBypasses: hooks.filter(row => row.decision === 'raw').map(row => row.command).slice(-5) },
130
137
  errors: recent.filter(row => row.kind === 'error').slice(-5).map(row => ({ ts: row.ts, command: row.command, error: row.error })),
131
138
  sessions: { seen: sessions.size, usingHydra: sessionsUsing.size },