margin-core 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. margin_core-0.1.0/.gitignore +80 -0
  2. margin_core-0.1.0/CHANGELOG.md +15 -0
  3. margin_core-0.1.0/LICENSE +21 -0
  4. margin_core-0.1.0/PKG-INFO +106 -0
  5. margin_core-0.1.0/README.md +79 -0
  6. margin_core-0.1.0/pyproject.toml +39 -0
  7. margin_core-0.1.0/src/margin_core/__init__.py +68 -0
  8. margin_core-0.1.0/src/margin_core/_fs.py +37 -0
  9. margin_core-0.1.0/src/margin_core/agent.py +255 -0
  10. margin_core-0.1.0/src/margin_core/config.py +184 -0
  11. margin_core-0.1.0/src/margin_core/errors.py +49 -0
  12. margin_core-0.1.0/src/margin_core/inputs.py +71 -0
  13. margin_core-0.1.0/src/margin_core/llm/__init__.py +30 -0
  14. margin_core-0.1.0/src/margin_core/llm/_chat_completions.py +34 -0
  15. margin_core-0.1.0/src/margin_core/llm/_http.py +42 -0
  16. margin_core-0.1.0/src/margin_core/llm/anthropic.py +80 -0
  17. margin_core-0.1.0/src/margin_core/llm/base.py +338 -0
  18. margin_core-0.1.0/src/margin_core/llm/catalog.py +126 -0
  19. margin_core-0.1.0/src/margin_core/llm/factory.py +75 -0
  20. margin_core-0.1.0/src/margin_core/llm/local.py +196 -0
  21. margin_core-0.1.0/src/margin_core/llm/mock.py +85 -0
  22. margin_core-0.1.0/src/margin_core/llm/openai.py +62 -0
  23. margin_core-0.1.0/src/margin_core/llm/pricing.py +55 -0
  24. margin_core-0.1.0/src/margin_core/loaders/__init__.py +53 -0
  25. margin_core-0.1.0/src/margin_core/loaders/base.py +213 -0
  26. margin_core-0.1.0/src/margin_core/loaders/filters.py +247 -0
  27. margin_core-0.1.0/src/margin_core/loaders/git.py +176 -0
  28. margin_core-0.1.0/src/margin_core/loaders/github.py +186 -0
  29. margin_core-0.1.0/src/margin_core/loaders/local.py +163 -0
  30. margin_core-0.1.0/src/margin_core/logging.py +20 -0
  31. margin_core-0.1.0/src/margin_core/models.py +130 -0
  32. margin_core-0.1.0/src/margin_core/py.typed +1 -0
  33. margin_core-0.1.0/src/margin_core/registry.py +111 -0
  34. margin_core-0.1.0/src/margin_core/reporting/__init__.py +6 -0
  35. margin_core-0.1.0/src/margin_core/reporting/json_report.py +18 -0
  36. margin_core-0.1.0/src/margin_core/reporting/markdown_report.py +84 -0
  37. margin_core-0.1.0/src/margin_core/runner.py +27 -0
  38. margin_core-0.1.0/tests/conftest.py +74 -0
  39. margin_core-0.1.0/tests/test_agent.py +115 -0
  40. margin_core-0.1.0/tests/test_config.py +26 -0
  41. margin_core-0.1.0/tests/test_core_skeleton.py +5 -0
  42. margin_core-0.1.0/tests/test_filters.py +112 -0
  43. margin_core-0.1.0/tests/test_github_loader.py +109 -0
  44. margin_core-0.1.0/tests/test_history_scope.py +101 -0
  45. margin_core-0.1.0/tests/test_inputs.py +83 -0
  46. margin_core-0.1.0/tests/test_llm.py +112 -0
  47. margin_core-0.1.0/tests/test_llm_session.py +100 -0
  48. margin_core-0.1.0/tests/test_local_llm.py +232 -0
  49. margin_core-0.1.0/tests/test_local_loader.py +134 -0
  50. margin_core-0.1.0/tests/test_models.py +41 -0
  51. margin_core-0.1.0/tests/test_preclone_check.py +113 -0
  52. margin_core-0.1.0/tests/test_registry.py +99 -0
  53. margin_core-0.1.0/tests/test_reporting.py +84 -0
  54. margin_core-0.1.0/tests/test_user_settings.py +142 -0
@@ -0,0 +1,80 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ /lib/
14
+ /lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ share/python-wheels/
20
+ *.egg-info/
21
+ .installed.cfg
22
+ *.egg
23
+
24
+ # Virtual environments
25
+ .venv/
26
+ venv/
27
+ ENV/
28
+ env/
29
+
30
+ # uv
31
+ .uv/
32
+
33
+ # Environment variables & secrets
34
+ .env
35
+ .env.local
36
+ .env.*.local
37
+ *.key
38
+ *.pem
39
+
40
+ # Testing & Coverage
41
+ .pytest_cache/
42
+ .coverage
43
+ .coverage.*
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+
51
+ # Type checking & linting
52
+ .mypy_cache/
53
+ .ruff_cache/
54
+ .dmypy.json
55
+ dmypy.json
56
+
57
+ # IDEs & Editors
58
+ .vscode/
59
+ .idea/
60
+ *.swp
61
+ *.swo
62
+ *~
63
+
64
+ # Operating System
65
+ .DS_Store
66
+ Thumbs.db
67
+ ehthumbs.db
68
+ desktop.ini
69
+
70
+ # Output / Reports
71
+ report.json
72
+ report.md
73
+ temp/
74
+ tmp/
75
+
76
+ # Private planning notes (kept locally, not published)
77
+ PLAN.md
78
+
79
+ # the web UI bundled into margin-api at release time (scripts/build_release.py)
80
+ packages/api/src/margin_api/ui/
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ All notable changes to this package are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [0.1.0] - 2026-10-21
8
+
9
+ ### Added
10
+ - The agent contract (`BaseAgent`, `RunContext`), structured results with findings and evidence, and the agent registry (entry-point group `margin.agents`).
11
+ - Model clients: local OpenAI-compatible servers (Ollama by default), Anthropic and OpenAI, with retries, token and cost tracking, per-run budgets, and a mock for tests.
12
+ - A model catalogue: list a provider's models, test a connection.
13
+ - Settings from environment variables, saved settings, `.env` and defaults, in that order.
14
+ - Code loaders: local folders and GitHub repositories, with filters, size limits, a pre-clone size check and git history.
15
+ - JSON and Markdown reports.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 margin contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,106 @@
1
+ Metadata-Version: 2.5
2
+ Name: margin-core
3
+ Version: 0.1.0
4
+ Summary: The shared core of margin: the agent contract, results with evidence, model clients and code loaders.
5
+ Project-URL: Homepage, https://github.com/Waddah0-0/margin
6
+ Project-URL: Source, https://github.com/Waddah0-0/margin/tree/main/packages/core
7
+ Project-URL: Issues, https://github.com/Waddah0-0/margin/issues
8
+ Project-URL: Changelog, https://github.com/Waddah0-0/margin/blob/main/packages/core/CHANGELOG.md
9
+ Author: Waddah Ali
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: agents,evidence,framework,llm
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.11
22
+ Requires-Dist: httpx>=0.27.0
23
+ Requires-Dist: pydantic-settings>=2.2.0
24
+ Requires-Dist: pydantic>=2.7.0
25
+ Requires-Dist: python-dotenv>=1.0.0
26
+ Description-Content-Type: text/markdown
27
+
28
+ # margin-core
29
+
30
+ The shared core of [margin](https://github.com/Waddah0-0/margin), a collection of small AI
31
+ agents that answer with evidence. You need this package to write an agent. To use one,
32
+ install the agent (for example `margin-ai-code-detector`) together with `margin-cli` or
33
+ `margin-api`.
34
+
35
+ ```bash
36
+ pip install margin-core
37
+ ```
38
+
39
+ ## What's in it
40
+
41
+ An agent is a class with an input model, an output model and an async `run` method. You call
42
+ it through `execute()`, which validates the input, times the run, counts model usage, and
43
+ turns a crash into an error result instead of an exception. Every agent returns the same
44
+ `AgentResult`: a summary, findings, the evidence for each finding, warnings, and whatever
45
+ typed data the agent wants to add.
46
+
47
+ Agents register under the `margin.agents` entry point, so installing one is all it takes for
48
+ the CLI, the API and the web UI to find it.
49
+
50
+ The model client gives you one interface to local OpenAI-compatible servers (Ollama, LM
51
+ Studio, llama.cpp, vLLM), Anthropic and OpenAI. It retries, tracks tokens and cost, and stops
52
+ at a spending limit you set. `MockLLM` returns canned answers, which keeps tests offline.
53
+
54
+ The loaders turn a local folder or a GitHub repository into a `Workspace`: the files worth
55
+ reading, the files that were skipped and why, and the git history. A repository is
56
+ size-checked before it is cloned, and nothing in it is ever run.
57
+
58
+ ## A minimal agent
59
+
60
+ ```python
61
+ from pydantic import BaseModel
62
+ from margin_core import AgentInput, AgentResult, BaseAgent, RunContext
63
+
64
+
65
+ class GreetInput(AgentInput):
66
+ name: str
67
+
68
+
69
+ class GreetOutput(BaseModel):
70
+ greeting: str
71
+
72
+
73
+ class Greeter(BaseAgent):
74
+ id = "greeter"
75
+ name = "Greeter"
76
+ description = "Says hello."
77
+ version = "0.1.0"
78
+ InputModel = GreetInput
79
+ OutputModel = GreetOutput
80
+
81
+ async def run(self, input: GreetInput, ctx: RunContext) -> AgentResult:
82
+ greeting = f"Hello, {input.name}!"
83
+ return self.make_result(summary=greeting, output=GreetOutput(greeting=greeting))
84
+ ```
85
+
86
+ Register it in your package's `pyproject.toml`:
87
+
88
+ ```toml
89
+ [project.entry-points."margin.agents"]
90
+ greeter = "my_package.agent:Greeter"
91
+ ```
92
+
93
+ The full guide is
94
+ [docs/writing-an-agent.md](https://github.com/Waddah0-0/margin/blob/main/docs/writing-an-agent.md).
95
+
96
+ ## Settings
97
+
98
+ Model settings are read from environment variables first, then from settings saved in the
99
+ web UI, then from a `.env` file. The ones you are likely to need are `DEFAULT_LLM_PROVIDER`
100
+ (`local`, `anthropic` or `openai`), `LOCAL_LLM_BASE_URL`, `LOCAL_LLM_MODEL`,
101
+ `ANTHROPIC_API_KEY`, `OPENAI_API_KEY` and `MAX_COST_USD`.
102
+ [.env.example](https://github.com/Waddah0-0/margin/blob/main/.env.example) lists them all.
103
+
104
+ ## License
105
+
106
+ MIT
@@ -0,0 +1,79 @@
1
+ # margin-core
2
+
3
+ The shared core of [margin](https://github.com/Waddah0-0/margin), a collection of small AI
4
+ agents that answer with evidence. You need this package to write an agent. To use one,
5
+ install the agent (for example `margin-ai-code-detector`) together with `margin-cli` or
6
+ `margin-api`.
7
+
8
+ ```bash
9
+ pip install margin-core
10
+ ```
11
+
12
+ ## What's in it
13
+
14
+ An agent is a class with an input model, an output model and an async `run` method. You call
15
+ it through `execute()`, which validates the input, times the run, counts model usage, and
16
+ turns a crash into an error result instead of an exception. Every agent returns the same
17
+ `AgentResult`: a summary, findings, the evidence for each finding, warnings, and whatever
18
+ typed data the agent wants to add.
19
+
20
+ Agents register under the `margin.agents` entry point, so installing one is all it takes for
21
+ the CLI, the API and the web UI to find it.
22
+
23
+ The model client gives you one interface to local OpenAI-compatible servers (Ollama, LM
24
+ Studio, llama.cpp, vLLM), Anthropic and OpenAI. It retries, tracks tokens and cost, and stops
25
+ at a spending limit you set. `MockLLM` returns canned answers, which keeps tests offline.
26
+
27
+ The loaders turn a local folder or a GitHub repository into a `Workspace`: the files worth
28
+ reading, the files that were skipped and why, and the git history. A repository is
29
+ size-checked before it is cloned, and nothing in it is ever run.
30
+
31
+ ## A minimal agent
32
+
33
+ ```python
34
+ from pydantic import BaseModel
35
+ from margin_core import AgentInput, AgentResult, BaseAgent, RunContext
36
+
37
+
38
+ class GreetInput(AgentInput):
39
+ name: str
40
+
41
+
42
+ class GreetOutput(BaseModel):
43
+ greeting: str
44
+
45
+
46
+ class Greeter(BaseAgent):
47
+ id = "greeter"
48
+ name = "Greeter"
49
+ description = "Says hello."
50
+ version = "0.1.0"
51
+ InputModel = GreetInput
52
+ OutputModel = GreetOutput
53
+
54
+ async def run(self, input: GreetInput, ctx: RunContext) -> AgentResult:
55
+ greeting = f"Hello, {input.name}!"
56
+ return self.make_result(summary=greeting, output=GreetOutput(greeting=greeting))
57
+ ```
58
+
59
+ Register it in your package's `pyproject.toml`:
60
+
61
+ ```toml
62
+ [project.entry-points."margin.agents"]
63
+ greeter = "my_package.agent:Greeter"
64
+ ```
65
+
66
+ The full guide is
67
+ [docs/writing-an-agent.md](https://github.com/Waddah0-0/margin/blob/main/docs/writing-an-agent.md).
68
+
69
+ ## Settings
70
+
71
+ Model settings are read from environment variables first, then from settings saved in the
72
+ web UI, then from a `.env` file. The ones you are likely to need are `DEFAULT_LLM_PROVIDER`
73
+ (`local`, `anthropic` or `openai`), `LOCAL_LLM_BASE_URL`, `LOCAL_LLM_MODEL`,
74
+ `ANTHROPIC_API_KEY`, `OPENAI_API_KEY` and `MAX_COST_USD`.
75
+ [.env.example](https://github.com/Waddah0-0/margin/blob/main/.env.example) lists them all.
76
+
77
+ ## License
78
+
79
+ MIT
@@ -0,0 +1,39 @@
1
+ [project]
2
+ name = "margin-core"
3
+ version = "0.1.0"
4
+ description = "The shared core of margin: the agent contract, results with evidence, model clients and code loaders."
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ authors = [{ name = "Waddah Ali" }]
10
+ keywords = ["agents", "llm", "evidence", "framework"]
11
+ classifiers = [
12
+ "Development Status :: 3 - Alpha",
13
+ "Intended Audience :: Developers",
14
+ "Operating System :: OS Independent",
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Typing :: Typed",
19
+ "Topic :: Software Development :: Libraries :: Python Modules",
20
+ ]
21
+ dependencies = [
22
+ "pydantic>=2.7.0",
23
+ "pydantic-settings>=2.2.0",
24
+ "httpx>=0.27.0",
25
+ "python-dotenv>=1.0.0",
26
+ ]
27
+
28
+ [project.urls]
29
+ Homepage = "https://github.com/Waddah0-0/margin"
30
+ Source = "https://github.com/Waddah0-0/margin/tree/main/packages/core"
31
+ Issues = "https://github.com/Waddah0-0/margin/issues"
32
+ Changelog = "https://github.com/Waddah0-0/margin/blob/main/packages/core/CHANGELOG.md"
33
+
34
+ [build-system]
35
+ requires = ["hatchling"]
36
+ build-backend = "hatchling.build"
37
+
38
+ [tool.hatch.build.targets.wheel]
39
+ packages = ["src/margin_core"]
@@ -0,0 +1,68 @@
1
+ """Core foundation library for margin.
2
+
3
+ Public API::
4
+
5
+ from margin_core import BaseAgent, RunContext, AgentResult, Finding, Evidence
6
+ from margin_core import run_agent, list_agents, get_agent
7
+ """
8
+
9
+ from margin_core.agent import BaseAgent, CancellationToken, ProgressCallback, RunContext
10
+ from margin_core.errors import (
11
+ AgentLoadError,
12
+ AgentNotFoundError,
13
+ AgentsLibError,
14
+ BudgetExceededError,
15
+ InputValidationError,
16
+ InvalidTargetError,
17
+ LimitExceededError,
18
+ LLMError,
19
+ LoaderError,
20
+ RunCancelledError,
21
+ TransientLLMError,
22
+ )
23
+ from margin_core.models import (
24
+ AgentInfo,
25
+ AgentInput,
26
+ AgentResult,
27
+ Evidence,
28
+ Finding,
29
+ ProgressEvent,
30
+ RunMetadata,
31
+ TokenUsage,
32
+ )
33
+ from margin_core.registry import AgentRegistry, default_registry, get_agent, list_agents
34
+ from margin_core.runner import run_agent
35
+
36
+ __version__ = "0.1.0"
37
+
38
+ __all__ = [
39
+ "AgentInfo",
40
+ "AgentInput",
41
+ "AgentLoadError",
42
+ "AgentNotFoundError",
43
+ "AgentRegistry",
44
+ "AgentResult",
45
+ "AgentsLibError",
46
+ "BaseAgent",
47
+ "BudgetExceededError",
48
+ "CancellationToken",
49
+ "Evidence",
50
+ "Finding",
51
+ "InputValidationError",
52
+ "InvalidTargetError",
53
+ "LLMError",
54
+ "LimitExceededError",
55
+ "LoaderError",
56
+ "ProgressCallback",
57
+ "ProgressEvent",
58
+ "RunCancelledError",
59
+ "RunContext",
60
+ "RunMetadata",
61
+ "TokenUsage",
62
+ "TransientLLMError",
63
+ "__version__",
64
+ "default_registry",
65
+ "get_agent",
66
+ "list_agents",
67
+ "run_agent",
68
+ ]
@@ -0,0 +1,37 @@
1
+ """Filesystem helpers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import shutil
7
+ import stat
8
+ import sys
9
+ from collections.abc import Callable
10
+ from pathlib import Path
11
+ from typing import Any
12
+
13
+
14
+ def _make_writable_and_retry(func: Callable[..., Any], path: str, _exc: object) -> None:
15
+ os.chmod(path, stat.S_IWRITE)
16
+ func(path)
17
+
18
+
19
+ def force_rmtree(path: Path) -> None:
20
+ """Remove a directory tree, including read-only files (e.g. git objects on Windows)."""
21
+ if not path.exists():
22
+ return
23
+ if sys.version_info >= (3, 12):
24
+ shutil.rmtree(path, onexc=_make_writable_and_retry)
25
+ else:
26
+ shutil.rmtree(path, onerror=_make_writable_and_retry)
27
+
28
+
29
+ def directory_size(path: Path) -> int:
30
+ """Total size in bytes of regular files under ``path`` (symlinks not followed)."""
31
+ total = 0
32
+ for dirpath, _dirnames, filenames in os.walk(path, followlinks=False):
33
+ for name in filenames:
34
+ full = os.path.join(dirpath, name)
35
+ if not os.path.islink(full):
36
+ total += os.path.getsize(full)
37
+ return total