Skip to content

nvxtien/context-slice

v1.9.6MIT

Structural code-context slicing for Java, TypeScript/TSX, JavaScript, Python, Rust, and Go. Serves a budget-bounded, relevance-ranked slice of a repository instead of whole files.

ContextSlice

Less context. Unchanged signal.

Give your coding assistant the smallest useful slice of your codebase.

When you use AI to write code, the model doesn’t spend tokens just writing the final answer. A large part of the cost often comes from the code it has to read first: the target file, its callers and callees, dependencies, tests, and configuration.

Reading more code means more tokens, slower responses, and more noise around the detail that actually matters. The practical way to reduce that cost is to reduce how much code the agent has to read in the first place.

ContextSlice does exactly that. It builds a small, task-specific context from Java, TypeScript, TSX, JavaScript, Python, Rust, and Go source. It finds the target symbol, follows relevant callers and callees, and reports what was included or left out.

In short: instead of giving an AI assistant the whole repository, give it the smallest useful slice of the codebase.

It is local, read-only, and deterministic: Tree-sitter performs the structural analysis, SQLite stores the index, and no source code is sent to a hosted service by ContextSlice.

The result is:

  • Less code to read and pay for: return the target symbol plus the most relevant callers and callees instead of whole files.
  • Better signal: rank results by task relevance and keep unresolved runtime dispatch explicit instead of guessing.
  • Predictable output: enforce a token budget and explain why items were included or omitted.
  • Local control: keep source analysis and the SQLite index in the repository; the target source is never edited.

Quick start

Claude Code plugin

Install it directly from the GitHub marketplace:

/plugin marketplace add nvxtien/context-slice
/plugin install context-slice@context-slice-marketplace

The plugin provides a skill that tells Claude Code to request a focused ContextSlice preview before reading source files. Its PreToolUse hook also blocks supported source reads until context.preview has completed, so this is enforced at runtime rather than relying only on prompt compliance. The MCP server then targets the project Claude Code has open. The plugin starts the published the latest published context-slice npm runtime, so Claude needs npm registry access on first use. The package installs native dependencies for the current platform; later runs use the local npm cache.

Codex plugin

Add the GitHub marketplace to Codex:

codex plugin marketplace add nvxtien/context-slice
codex plugin marketplace list

Then open /plugins, choose Context Slice Marketplace, and install context-slice. The plugin includes the same ContextSlice skill and stdio MCP server for the project Codex has open.

How it works

  1. Discover a repository and scan supported source while ignoring common generated/build directories.
  2. Store symbols, call edges, hashes, schema version, and refresh time in a local SQLite cache.
  3. Refresh before preview or MCP tool execution so changed source files are not silently served stale.
  4. Select a target from task text, then include the target body plus ranked direct callers/callees until the strict token budget is full.

Available MCP tools are context.search, context.symbol, context.callers, context.preview, context.slice, and context.diff. MCP stdout contains protocol messages only; diagnostics must not corrupt stdio framing.

CLI

context-slice init
context-slice status
context-slice doctor
context-slice preview "explain retryPayment" --budget 1200 --explain
context-slice preview "explain retryPayment" --json
context-slice mark-dirty src/PaymentService.java
context-slice mcp
CommandPurpose
initDiscover the repository and create/refresh the local index.
indexRefresh the index explicitly.
statusShow readiness, schema, cache freshness, and last refresh.
doctorCheck repository, source, cache, and MCP command readiness.
preview <task>Return a deterministic, strict-budget context preview.
mark-dirty [file]Tell a running MCP index that source changed.
mcpStart the stdio MCP server with the stable public command.

Use --repo /absolute/path to select a repository (defaults to the nearest Git root). --json emits a stable, automation-oriented result; normal output is quiet, --explain adds why each item was included or omitted. --budget <tokens> sets the strict token budget for preview. context-slice --version and context-slice --help work without a repository checkout.

Exit codes are 0 for success, 2 for user/configuration errors, and 1 for unexpected failures. Errors include a remediation, for example increasing --budget when the selected target cannot fit.

Supported languages

LanguageExtensionsNotes
Java.javaClasses, interfaces, records, enums, methods, constructors
TypeScript.ts, .mts, .cts, .d.tsImports, re-exports and barrels, overloads, arrow functions
TSX.tsxReact components, handlers, JSX component references
JavaScript.js, .jsx, .mjs, .cjsSame adapter as TypeScript (JS is parsed as untyped TS); CommonJS (require/module.exports) is not recognized as imports/exports — see Known limitations below
Python.py, .pyiPackages and __init__ re-exports, decorators, self/cls, dataclasses
Rust.rsFunctions, structs, enums, traits, impls, modules; use/re-export resolution; self/associated/trait call resolution
Go.goFunctions, methods (incl. generic receivers), structs, interfaces, struct embedding and interface satisfaction; same-package, import-qualified and receiver-typed call resolution

One repository can hold all of them. See docs/typescript-support.md, docs/python-support.md and docs/rust-support.md for what each language's resolution does and does not cover.

Benchmarks

Run benchmarks locally:

npm run benchmark:v07

Reports are written under benchmarks/results/. Packaging validation is available with npm run benchmark:v08.

On the current macOS arm64 Java fixture, the workflow benchmark measured a median 6.309 ms one-file incremental refresh; this is local evidence, not a universal latency guarantee.

Limitations

  • Java, TypeScript, TSX, JavaScript, Python, Rust and Go only; this is not a semantic search or build tool: it does not use vector embeddings, a vector database, a compiler, a type checker, or LSP services such as tsserver, rust-analyzer, or rustc.
  • The index is syntax-based, so runtime dispatch, framework-generated implementations, and some generic or fluent call chains may remain unresolved.
  • Python analysis is AST-based and conservative: when a receiver or target cannot be determined from syntax alone—such as with factory-created receivers, getattr, dynamic imports, or monkey patching—the edge remains unresolved rather than guessed.
  • Rust analysis is syntax-based and conservative: macro-expanded semantics and trait dispatch that require expansion or type checking may remain unresolved; rust-analyzer and rustc are not required.
  • TypeScript resolution uses declared and imported structure: when a receiver type cannot be determined from those facts, or an import leaves the checkout, the edge remains unresolved rather than guessed.
  • JavaScript module analysis currently follows ES module syntax. CommonJS require() and module.exports are parsed safely but are not yet represented as import/export edges. See benchmarks/results/v1.7-javascript-support.md.
  • Go analysis resolves struct-embedding promotion when the embedded type and methods are visible in the same package directory. Cross-package promoted methods and constructor-returned interface implementations that require deeper type-flow inference may remain unresolved. See benchmarks/results/v1.6-go-support.md.
  • Task-to-symbol selection is heuristic; naming the method or symbol explicitly gives a more precise slice.
  • Sibling composition follows direct syntactic evidence (this.field and Java field names); state shared through an intermediate object is not included.
  • Token counts are estimates for budget enforcement, not model-provider usage telemetry.
  • The local index provides focused request context; verify behavior with normal code review and tests.
  • Metadata is the fast freshness path. Watcher or mark-dirty notifications verify file content hashes when source changes need confirmation.
  • The Claude plugin is validated with Claude Code 2.1.285 on macOS arm64. Other Claude Code releases and plugin hosts may differ in MCP startup behavior.
  • The source-read guard depends on Claude Code plugin hook support; hosts that install only the MCP server or skill do not enforce it.
  • Validated on macOS arm64 (Node 20.19.5 and 22.12.0); Linux and Windows use a directory-tree watcher fallback but are not CI-validated.

Development

npm ci
npm run build
npm test
npm run benchmark:v07