GitHubnpm
Tools · v0.11.0

Code Agent

Code Agent pushes vibe-hnindex from a search server to a coding agent runtime. Instead of AI assistants calling search 5-15 times, reading files one by one, and accumulating context noise, Code Agent delivers everything needed in 2 calls.

✨ New in v0.11.0

Two new tools: code_session for context gathering and code_apply for safe code changes. Opt-in via CODE_AGENT_ENABLED=true — zero impact on existing workflows.

Why Code Agent?

In a typical AI coding session, the assistant wastes 60%+ of its first turn just retrieving context — multiple searches, reading files, filtering noise. This is called context rot: irrelevant search results accumulating in the context window, degrading reasoning quality.

Without Code Agent

→ search("middleware pattern")

→ search("express router")

→ search("rate limit")

→ read file auth.ts

→ read file middleware.ts

→ read file package.json

→ ... 5-15 calls total

~20K+ context noise

With Code Agent

→ code_session("add rate limiting")

... AI reasons over package ...

→ code_apply(edits=[...])

2 calls, ~500 ctx tokens

code_session — Context Gathering

One call that replaces 5-15 separate search + read operations. Returns a structured context package with everything the AI needs to reason about a code change.

Usage

code_session(
  project_name: "my-app",
  task: "add rate limiting middleware to Express API",
  target_files: ["src/api/auth.ts"]  // optional
)

What It Returns

SectionContent
Task AnalysisDetected type (add-feature/refactor/debug/explain), keywords, relevant directories
Core FilesFull file contents + exports + imports for the most relevant files
Similar PatternsFiles with same extension and structure — follow existing code style
DependenciesInstalled packages (from package.json), relevant deps matching task keywords
Test FilesRelated .test.ts, .spec.ts, __tests__/ files
Project StructureDetected framework (Next.js, Express, React...), test framework, TypeScript
Impact AnalysisFiles affected by the change + dependents count

What Happens Internally

code_session("add rate limiting", "my-app")
  → detect task type: add-feature
  → smart_context() — analyze impact, find patterns
  → search() keyword × 3 — exact symbol matching
  → search() semantic × 1 — natural language understanding
  → read_file() × N — full content of relevant files
  → dependency_check() — package.json analysis
  → find_test_files() — locate related tests
  → impact_analysis() — who depends on these files?
  → package → return structured JSON

💡 AI Agent Tip: Call code_session before any non-trivial code task. You get 80% of the context you need in one response. Then reason over it, decide what to edit, and call code_apply.

code_apply — Safe Code Changes

Apply code changes proposed by the AI. Respects safety scopes, runs tests, lint, and typecheck automatically.

Usage

code_apply(
  project_name: "my-app",
  session_id: "cs_...",         // from code_session (optional)
  edits: [
    {
      action: "create",
      file_path: "src/middleware/rate-limit.ts",
      content: "import rateLimit from 'express-rate-limit';\n..."
    },
    {
      action: "modify",
      file_path: "src/api/auth.ts",
      diff: "@@ -1,5 +1,6 @@\n import express...\n+import { apiLimiter }..."
    }
  ],
  verify: true                    // run tests after applying (default: true)
)

Edit Actions

ActionParametersDescription
createfile_path + contentCreate a new file (creates parent dirs automatically)
modifyfile_path + content or diffUpdate existing file (supports unified diff format)
deletefile_pathRemove a file

Safety Scopes

Configure via CODE_AGENT_SCOPE env var (default: moderate):

ScopeSafe to Write?Restrictions
safe❌ No writesPreview-only — great for reviewing before manual edits
moderate (default)✅ Create + modifyBlocks .env, config files, lockfiles, tsconfig, docker-compose
full✅ EverythingNo restrictions — recommend git backup before use

🔒 Safety first: In moderate scope, vibe-hnindex refuses to touch .env, package-lock.json, tsconfig.json, docker-compose.yml, and other critical config files. Always review changes before committing.

Auto-Verification

When verify: true (default), code_apply automatically runs:

  • Tests — Detects vitest, jest, pytest, or npm test
  • Lint — Runs npm run lint if available
  • TypeCheck — Runs tsc --noEmit for TypeScript projects

If tests fail, the result includes the failure output so the AI can iterate and fix.

Setup

Code Agent is opt-in. Add these to your MCP config to enable:

{
  "mcpServers": {
    "vibe-hnindex": {
      "command": "npx",
      "args": ["-y", "vibe-hnindex"],
      "env": {
        "OLLAMA_URL": "http://localhost:11434",
        "OLLAMA_MODEL": "bge-m3:567m",
        "QDRANT_URL": "http://localhost:6333",
        "CODE_AGENT_ENABLED": "true",
        "CODE_AGENT_SCOPE": "moderate"
      }
    }
  }
}

Or with the CLI (v0.11.1+):

hnindex init --mcp claude

The CLI automatically includes CODE_AGENT_ENABLED=true and CODE_AGENT_SCOPE=moderate in the generated config.

Best Practices

  1. Always start with code_session — gather context before making changes. Avoids blind edits.
  2. Review the context package — check similar patterns, test files, and impact analysis before deciding what to edit.
  3. Use verify=true (default) — let the server run tests and report failures immediately.
  4. Stay in moderate scope — it provides the best balance of safety and capability.
  5. One task per session— don't combine multiple unrelated changes in one code_apply call.
  6. Commit before applying — especially in full scope, having a git checkpoint makes rollback trivial.

Code Agent vs Smart Context

smart_contextcode_agent
PurposeContext for a file/questionContext + apply changes
ReturnsMarkdown with file info, imports, dependentsStructured JSON with files, patterns, deps, tests
Edits❌ No✅ Create, modify, delete
Verification❌ No✅ Tests, lint, typecheck
Scope controlN/Asafe / moderate / full
Use whenUnderstanding code, debugging questionsActually making changes, refactoring

Smart Context is great for exploration. Code Agent is for execution. They complement each other — use smart_context to understand, code_agent to implement.