Skip to main content
Glama
README.md
# Context Monitor

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
[![Antigravity](https://img.shields.io/badge/Antigravity-2.0_only-orange.svg)](#requirements)
[![Type](https://img.shields.io/badge/Type-Native_Plugin_%7C_MCP_App-green.svg)](#how-it-works)
[![Node.js](https://img.shields.io/badge/Node.js-%3E%3D22.5.0-339933.svg)](#requirements)

Context Monitor is a side-pane and sidebar MCP App plugin built for Google Antigravity 2.0 (AGY 2.0). It reads local Antigravity transcripts, configuration files, and language server metadata to show how much of the active conversation's context window is in use, which skills and MCP servers are loaded, which plugins are enabled, and how many tokens have been processed across local sessions.

This started as a personal side project to inspect context usage while working in Antigravity 2.0. It is published under the MIT License for anyone who wants to use it or adapt it.

## Requirements

- Google Antigravity 2.0 (AGY 2.0). This plugin depends on Antigravity 2.0 native plugin loading, MCP App UI entrypoints (`aux_pane` and `global`), lifecycle hooks, and the `~/.gemini/antigravity/` and `~/.gemini/config/` directory layout. It does not work on Antigravity 1.x or other editors.
- Node.js 22.5.0 or newer (used for the built-in `node:sqlite` module to read `conversation_summaries.db`).

## What it shows

| Section | Data displayed | Source |
| --- | --- | --- |
| Context breakdown | Total tokens used, percentage of the model context window, free space, deferred MCP tool schemas, and `/btw` side question totals. | Active `transcript.jsonl` (`input_tokens + cache_read_tokens`) minus estimated configuration overhead. |
| Accordions | Expandable lists for `Skills` (A to Z with per-skill token estimates), `MCP tools` (connected servers only), `Memory files` (repo, global, knowledge, and plugin rules), and `Side questions (/btw)` isolated to the open chat. | Workspace and global `.agents`/`.gemini` directories, `~/.gemini/antigravity/mcp/`, and `annotations/*.pbtxt`. |
| MCP Servers | Custom MCP servers configured by the user, with connection status and model-visible tool counts. | `~/.gemini/config/mcp_config.json`. |
| Plugins | Installed user and built-in plugins with component counts (`skills`, `MCP`, `agents`, `rules`, `hooks`). Shows enabled plugins by default and collapses disabled plugins behind a `Show N disabled` button. | Antigravity Language Server `GetAllPlugins` RPC, with disk fallback to `~/.gemini/config/plugins/` and `~/.gemini/antigravity/builtin/plugins/`. |
| Usage (Local) | Total `input_tokens + output_tokens` (excluding cache reads) and contributing session counts over the last 5 hours and last 7 days across all local transcripts. | `~/.gemini/antigravity/brain/*/.system_generated/logs/transcript.jsonl`. |

## Installation

The repository includes the prebuilt single-file bundle at `dist/app.html`, so end users only need to clone the plugin into Antigravity's plugins directory and install runtime dependencies.

Windows (PowerShell):

```powershell
git clone https://github.com/atinasinf/context-monitor.git "$HOME\.gemini\config\plugins\context-monitor"
cd "$HOME\.gemini\config\plugins\context-monitor"
npm install --omit=dev
```

macOS and Linux:

```bash
git clone https://github.com/atinasinf/context-monitor.git ~/.gemini/config/plugins/context-monitor
cd ~/.gemini/config/plugins/context-monitor
npm install --omit=dev
```

Open Antigravity 2.0 and confirm that `Context Monitor` is enabled under Customizations -> Plugins. To open the panel:

- Click `+` in the right side pane and pick `Context`.
- Click `Context Monitor` in the left sidebar for the full-page view.
- Ask the agent to open Context Monitor (uses the bundled `context-monitor` skill and `open_context_monitor` tool).

## How it works

The plugin has three runtime parts:

1. Lifecycle hooks (`hooks.json`, `hooks/track-active.mjs`) listen to `SessionStart`, `PreInvocation`, and `Stop`. The script writes active conversation metadata and per-conversation workspace paths atomically to `~/.gemini/antigravity/plugin_data/context-monitor/active.json`, ignoring `/btw` side questions and subagents.
2. The local MCP server (`server.mjs`, `snapshot.mjs`) serves the bundled UI resource (`ui://context-monitor/app.html`) and exposes `open_context_monitor` and the app-only `get_context_snapshot` tool. Conversation resolution follows four modes in priority order: explicit `manual` ID, `host` metadata (`extra._meta['antigravity.google/conversation_id']` walked back to the root conversation for `/btw` and nested subagents), `draft` (empty `New Chat` tab in the side pane), and `auto` fallback for the sidebar view.
3. The frontend (`src/app.html`, `src/app.js`, bundled into `dist/app.html` with `esbuild`) follows Antigravity's light and dark theme variables. It uses a `ResizeObserver` and visibility checks to refresh immediately on tab switch while pausing background polling when the pane is hidden.

When `/compact` runs, Antigravity logs a `CHECKPOINT` entry whose `input_tokens + cache_read_tokens` reflects the pre-compaction context sent to generate the summary. Context Monitor ignores that pre-compaction input count and computes a temporary estimate (`~`) from the first-turn baseline plus the checkpoint summary length until the next model response reports the exact post-compaction token count.

## Known limitations

This plugin is read-only and relies on local files and undocumented Antigravity 2.0 internals, which introduces several limitations:

- Overhead token estimates use `Math.ceil(utf8Bytes / 4)` for `SKILL.md` frontmatter, `AGENTS.md`/`GEMINI.md`, and MCP schemas because no model-specific tokenizer is bundled. Total conversation usage comes directly from `input_tokens + cache_read_tokens` reported in `transcript.jsonl`, and `Messages` is computed as `totalUsed - overheadTokens`.
- Immediately after `/compact`, the `~` token count is an approximation anchored to the first model turn. If you switched models or toggled several MCP servers mid-conversation before compacting, the temporary number can drift by a few thousand tokens until you send the next message.
- `scanSkills()` reads `~/.gemini/antigravity/builtin/skills/` directly, which currently includes two internal built-in skills (`permissioned-github` and `swarm_narrator`) that are not mounted in standard conversations.
- The `Plugins` and `MCP Servers` sections are strictly read-only. Antigravity 2.0 does not expose a public API for an MCP App to toggle plugins, so enabling or disabling plugins still happens in Customizations. App-level integrations like GitHub do not appear in `GetAllPlugins` or on disk and are omitted.
- `Usage (Local)` sums local transcript logs (`input_tokens + output_tokens`, excluding cache reads) and is not Google server-side account quota. Individual `transcript.jsonl` files larger than 4 MB are skipped to keep polling fast, and each folder under `brain/*/` (including `/btw` branches and subagents) is currently counted in the `chats` total.
- `scanMemoryFiles()` reads workspace root `AGENTS.md`/`GEMINI.md` and `.agents/rules/*.md`, `.gemini/rules/*.md`. It does not recurse into nested subdirectories for subdirectory `AGENTS.md` files, and it does not enforce Antigravity's 24 KB per-rule truncation cap.
- On the first `PreInvocation` of a newly spawned subagent before its transcript flushes to disk, `hooks/track-active.mjs` can briefly record that subagent ID in `active.json` (filtered out on read by `snapshot.mjs`).

## Planned updates

- Separate main conversation counts from subagent and `/btw` counts in `Usage (Local)`.
- Filter out unmounted internal built-in skills so the `Skills` accordion matches the active prompt skill list.
- Guard `hooks/track-active.mjs` against writing uninitialized subagent invocations to `active.json`.
- Display the active model name next to the context window ratio (`1M` or `200K`) and add tooltips on breakdown rows.
- Apply Antigravity's 24 KB per-file rule cap in `scanMemoryFiles()`.

## Development

Install development dependencies (`esbuild`), rebuild `dist/app.html`, and run the unit test suite (15 tests using `node:test`):

```bash
npm install
npm run build
npm test
```

If Antigravity 2.0 is already running and you want the MCP server to reload without restarting the app, call `RefreshMcpServers` on the local language server with `{"serverName":"context-monitor_context-monitor"}`, then close and reopen the `Context` side-pane tab once to load the rebuilt `dist/app.html`.

## License

MIT License. See [LICENSE](LICENSE) for details.