Skip to main content

Surgical code isolation for AI agents

Project description

Vial

Surgical code isolation for AI agents.

Vial extracts a single function or class from a large Python file into a clean, focused workspace file so an agent only sees — and edits — exactly what it needs to.

Install

pip install vial-ml
# or with uv:
uv add vial-ml

CLI

# Isolate a function
vial extract billing.py process_payment

# Agent edits .vial_workspace/process_payment_isolated.py ...

# Stitch it back (validates syntax first)
vial merge

Python API

from vial import Vial

v = Vial()
isolated_path = v.extract("billing.py", "process_payment")
# agent edits isolated_path ...
v.merge()

Token efficiency

Vial reduces the context an agent sees to only what matters. On a representative 175-line module:

Target            Isolated tokens    Full-file tokens    Savings
----------------------------------------------------------------
compute_hash               ~139              ~1,172        88%  ████████████████████████████░░░
read_config                ~148              ~1,172        87%  ███████████████████████████░░░░
write_config               ~156              ~1,172        87%  ███████████████████████████░░░░
ConnectionPool             ~297              ~1,172        75%  ███████████████████████░░░░░░░░
ConfigManager              ~290              ~1,172        75%  ███████████████████████░░░░░░░░
EventBus                   ~337              ~1,172        71%  ██████████████████████░░░░░░░░░

Token estimate: 1 token ≈ 4 characters (standard approximation for code).

Run it yourself

git clone https://github.com/Madara-Labs/vial-ml
cd vial-ml
uv run python benchmarks/run_benchmark.py

No API key needed — the benchmark uses only local file analysis.

How this compares to standard approaches

Every AI coding tool has to solve the same problem: the model needs to see relevant code without consuming the entire codebase. Here's how common approaches compare to Vial.

Claude Code / Gemini CLI — shell-tool exploration

Terminal-native agents hand the model shell tools (grep, find, cat) and let it explore the repo the way a new hire would — searching for keywords, opening files one at a time. For a question like "what calls process_payment?", an agent might open 10–25 files and burn tens of thousands of tokens before writing a single line. The model sees everything it found, not just what it needs.

Independent measurements on real codebases put Claude Code's grep-only mode at 108,000–117,000 tokens for a single targeted task (source). Those measurements were on larger codebases than Vial's benchmark fixture — direct comparison isn't apples-to-apples — but the dynamic is the same: the agent reads far more than it needs.

GitHub Copilot — cursor-proximity windowing

Copilot focuses context on the area around the cursor and recently open files. This is fast and works well for completions, but the agent has no understanding of where a function starts and ends — it sends a fixed character window, which may include half a function above and half a function below the target. Inline completions use an 8–16k token local window; agent mode uses the full model context (source).

Cursor — RAG + embeddings

Cursor indexes the codebase and retrieves semantically relevant chunks via embeddings. Better than raw grep, but retrieval returns chunks (fixed line ranges), not semantic units (full function/class boundaries). A retrieved chunk can cut off mid-function, leaving the agent with incomplete context (source).

Aider — whole-file map

Aider builds a repo-map summarising every file's symbols and sends relevant whole files to the model. More precise than grep, but the unit of context is still the whole file. A 500-line file gets sent even if only a 20-line function needs changing. With structural retrieval Aider uses 8,500–13,000 tokens per task — better than grep-mode agents, but still file-scoped (source).

Vial — AST-bounded isolation

Vial uses Python's AST to find the exact start and end line of the target, so the isolated file always contains exactly one complete, syntactically valid function or class — nothing more. Surrounding code is never included, never at risk of accidental modification, and never charged as tokens.

Approach          Context unit       Risk of side-effects    Token cost
──────────────────────────────────────────────────────────────────────
grep / cat        Whole file         High                    High
Cursor RAG        Fixed-line chunk   Medium (cut-off risk)   Medium
Aider map         Whole file         Medium                  Medium-High
Vial              Single AST node    None                    Low (71–88% less)

The tradeoff: Vial works at the function/class level. It is not a replacement for tools that handle cross-file refactors or project-wide search — it is the right tool once you know what to edit.

How it works

  1. Extract — AST-based line-bound finder locates the exact start/end of the target. A protected context header (imports + module-level globals, commented out) is prepended so the agent has the context it needs without being able to accidentally modify it.
  2. Edit — The agent modifies only the isolated file.
  3. Merge — Vial validates the modified code's syntax, then splices it back into the original file at the exact line range. Surrounding code is never touched.

All session state lives in a metadata.json file inside the workspace directory.

Dependencies

Package Purpose
typer CLI interface

The standard library covers everything else: ast for parsing, pathlib for file I/O, json for workspace state.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vial_ml-0.1.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vial_ml-0.1.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file vial_ml-0.1.3.tar.gz.

File metadata

  • Download URL: vial_ml-0.1.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for vial_ml-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c26c8d4a22d915f53cd63964ed98865af918b69478121b17ebbac1da8a362d54
MD5 eaab263b39ff24f9757b0326365a7ef0
BLAKE2b-256 f68f5c3baea4dac0972eaab9f016fc8d15c6b52a8005e75f65463dbdbcc3760e

See more details on using hashes here.

File details

Details for the file vial_ml-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: vial_ml-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for vial_ml-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 41f2cf43798035a2134545bc7eea708af091359953740ae0fd18c81fdd1c266f
MD5 d3b7344d640a0726286886467b3e8e46
BLAKE2b-256 47c22f553c6917565ebce9b7a6ee9af4e2ee0e19290b3e7200db7a6d28b34075

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page