Skip to content

Add Moss E2B cookbook example#256

Open
Luffy2208 wants to merge 3 commits into
usemoss:mainfrom
Luffy2208:feat/e2b-cookbook-example
Open

Add Moss E2B cookbook example#256
Luffy2208 wants to merge 3 commits into
usemoss:mainfrom
Luffy2208:feat/e2b-cookbook-example

Conversation

@Luffy2208

@Luffy2208 Luffy2208 commented May 20, 2026

Copy link
Copy Markdown
Contributor

Pull Request Checklist

Please ensure that your PR meets the following requirements:

  • I have read the CONTRIBUTING guide.
  • I have updated the documentation (if applicable).
  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Description

Adds a new examples/cookbook/e2b/ cookbook that demonstrates how to combine Moss semantic code search with E2B cloud execution sandboxes for a self-healing code agent workflow.

Fixes #199

What this adds

This cookbook shows the full Moss + LLM + E2B pattern requested in the issue:

  1. Index a local codebase into Moss.
  2. Search Moss with a natural-language bug report.
  3. Retrieve the relevant source and tests.
  4. Ask a Groq-hosted LLM for a minimal source-code patch.
  5. Copy the project into an isolated E2B sandbox.
  6. Run the baseline tests and capture the expected failure.
  7. Apply the patch in the sandbox.
  8. Rerun tests and print the validated diff.
  9. Clean up the sandbox and, by default, the temporary Moss index.

Files added

examples/cookbook/e2b/README.md

  • Documents setup, required environment variables, usage, reusable Moss indexes, E2B behavior, Moss retrieval behavior, and local test commands.
  • Follows the structure of the existing cookbook examples, especially the Daytona cookbook.

examples/cookbook/e2b/.env.example

Provides the required environment variables:

  • MOSS_PROJECT_ID
  • MOSS_PROJECT_KEY
  • E2B_API_KEY
  • GROQ_API_KEY

Includes optional variables:

  • GROQ_MODEL
  • MOSS_INDEX_NAME

examples/cookbook/e2b/pyproject.toml

Defines the cookbook package and dependencies.

Runtime dependencies

  • moss
  • e2b
  • groq
  • python-dotenv

Optional development dependencies

  • pytest
  • ruff

Also adds pytest config so plain pytest runs the cookbook helper tests instead of the intentionally broken sample project.


examples/cookbook/e2b/uv.lock

Locks dependencies for reproducible uv setup, consistent with the Daytona cookbook style.


examples/cookbook/e2b/code_index.py

  • Scans source files.
  • Builds Moss DocumentInfo records.
  • Adds useful metadata such as file path, language, and top-level Python symbols.
  • Provides Moss query helpers and LLM prompt formatting.

examples/cookbook/e2b/sandbox_runner.py

  • Wraps E2B sandbox creation, file copying, command execution, and patch application.
  • Copies runnable project files into E2B while skipping .env, virtualenvs, caches, large files, and generated folders.
  • Preserves non-zero command exits as structured output so the demo can show baseline test failure and post-patch success.
  • Validates patch paths and rejects attempts to patch test files.

examples/cookbook/e2b/code_agent.py

Main runnable example that:

  • Loads credentials from .env
  • Creates or reuses a Moss index
  • Searches Moss for relevant code
  • Calls Groq for a JSON patch
  • Creates an E2B sandbox
  • Runs setup/tests before and after patching
  • Prints the validated diff
  • Cleans up the sandbox and temporary Moss index

examples/cookbook/e2b/test_integration.py

Adds mocked tests that do not require Moss, Groq, or E2B credentials.

Covers

  • Source scanning
  • Project copying
  • Safe paths
  • Patch parsing
  • Test-file patch rejection
  • Command-result normalization
  • Reusable index-name resolution

examples/cookbook/e2b/sample_project/

Adds a tiny intentionally broken Python project used by the demo.

  • The bug is in src/ledger/totals.py, where tax is incorrectly treated as a flat amount.
  • The tests in tests/test_totals.py fail before the patch and pass after the agent applies the source fix.

Root README.md

Adds the E2B cookbook to the examples list and integration table.


Testing performed

Local no-credential checks

uv sync --extra dev
uv run python test_integration.py
uv run python -m pytest -q
uv run python -m ruff check .

Results

13 tests OK
13 passed
All checks passed

Live end-to-end validation

Also performed with real Moss, Groq, and E2B credentials:

uv run python code_agent.py

Verified behavior

  • Moss index was created.
  • Moss retrieved the relevant source and test files.
  • Groq generated a source patch.
  • E2B sandbox was created.
  • Baseline tests failed as expected.
  • Patch was applied in the sandbox.
  • Patched tests passed.
  • Validated Git diff was printed.
  • E2B sandbox was cleaned up.
  • Temporary Moss index was deleted.

Reusable index flow

uv run python code_agent.py --index-name moss-e2b-code-agent --reuse-index

Verified reusable-index behavior

  • First run creates and keeps the reusable Moss index.
  • Later runs load the existing Moss index instead of recreating it.
  • The E2B sandbox is still cleaned up after each run.

Notes

The bundled sample_project is intentionally broken. Running its tests directly is expected to fail before the agent patches it.

The cookbook’s own tests are scoped to test_integration.py.


Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update


Open in Devin Review

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View 5 additional findings in Devin Review.

Open in Devin Review

Comment thread examples/cookbook/e2b/code_agent.py Outdated
@CoderOMaster CoderOMaster requested review from Copilot and removed request for yatharthk2 June 24, 2026 15:49
@CoderOMaster

Copy link
Copy Markdown
Contributor

@Luffy2208 please update the pr as per copliot comments

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Python cookbook under examples/cookbook/e2b/ demonstrating a “self-healing code agent” workflow that combines Moss semantic code retrieval with E2B sandbox execution to validate LLM-proposed patches in an isolated environment.

Changes:

  • Introduces the examples/cookbook/e2b/ cookbook (agent entrypoint, Moss indexing helpers, E2B sandbox runner, and mocked integration tests).
  • Adds an intentionally broken sample_project/ used to demonstrate failing → patched → passing test validation.
  • Updates the root README.md to list the new E2B cookbook.

Reviewed changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
README.md Adds the E2B cookbook entry to the root examples/integrations listing.
examples/cookbook/e2b/README.md Documents setup, env vars, usage, and the Moss+Groq+E2B flow.
examples/cookbook/e2b/.env.example Provides required/optional environment variables for the cookbook.
examples/cookbook/e2b/pyproject.toml Defines cookbook dependencies, dev extras, and pytest/ruff config.
examples/cookbook/e2b/uv.lock Locks cookbook dependencies for reproducible installs.
examples/cookbook/e2b/code_index.py Scans source files and builds Moss DocumentInfo records + query helpers.
examples/cookbook/e2b/sandbox_runner.py Wraps E2B sandbox creation, file copying, command execution, and patch parsing/apply.
examples/cookbook/e2b/code_agent.py Main runnable agent: retrieve context via Moss, request patch via Groq, validate in E2B.
examples/cookbook/e2b/test_integration.py Mocked tests for scanning/copying/patch parsing/safety without credentials.
examples/cookbook/e2b/sample_project/pyproject.toml Minimal metadata for the demo project.
examples/cookbook/e2b/sample_project/src/ledger/init.py Exposes the sample project API surface.
examples/cookbook/e2b/sample_project/src/ledger/totals.py Contains the intentionally broken tax logic used by the demo.
examples/cookbook/e2b/sample_project/tests/test_totals.py Tests that fail before the patch and pass after applying the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/cookbook/e2b/pyproject.toml Outdated
Comment on lines +25 to +26
[tool.setuptools]
py-modules = ["code_agent", "code_index", "sandbox_runner", "test_integration"]
Comment thread examples/cookbook/e2b/code_index.py Outdated
relative_path = path.relative_to(root).as_posix()
content = path.read_text(encoding="utf-8")
digest = hashlib.sha1(content.encode("utf-8")).hexdigest()[:12]
symbols = extract_python_symbols(content) if path.suffix == ".py" else []
Comment on lines +92 to +94
return client, False
except Exception:
print("Reusable Moss index not found; creating it...")
@Luffy2208 Luffy2208 force-pushed the feat/e2b-cookbook-example branch from 0505511 to 1597d9c Compare June 25, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Cookbook with E2B

3 participants