recursa 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.
- recursa-0.1.0/LICENSE +202 -0
- recursa-0.1.0/MANIFEST.in +1 -0
- recursa-0.1.0/PKG-INFO +157 -0
- recursa-0.1.0/README.md +126 -0
- recursa-0.1.0/pyproject.toml +47 -0
- recursa-0.1.0/recursa/__init__.py +25 -0
- recursa-0.1.0/recursa/adapter.py +134 -0
- recursa-0.1.0/recursa/auth.py +141 -0
- recursa-0.1.0/recursa/cli.py +378 -0
- recursa-0.1.0/recursa/data.py +238 -0
- recursa-0.1.0/recursa/evals.py +256 -0
- recursa-0.1.0/recursa/isolation.py +79 -0
- recursa-0.1.0/recursa/learner.py +553 -0
- recursa-0.1.0/recursa/llm.py +42 -0
- recursa-0.1.0/recursa/optimize.py +133 -0
- recursa-0.1.0/recursa/project.py +75 -0
- recursa-0.1.0/recursa/redact.py +45 -0
- recursa-0.1.0/recursa/reflect.py +112 -0
- recursa-0.1.0/recursa/stats.py +143 -0
- recursa-0.1.0/recursa/store.py +295 -0
- recursa-0.1.0/recursa/trace.py +149 -0
- recursa-0.1.0/recursa/types.py +152 -0
- recursa-0.1.0/recursa/ui.py +173 -0
- recursa-0.1.0/recursa/worker.py +80 -0
- recursa-0.1.0/recursa.egg-info/PKG-INFO +157 -0
- recursa-0.1.0/recursa.egg-info/SOURCES.txt +29 -0
- recursa-0.1.0/recursa.egg-info/dependency_links.txt +1 -0
- recursa-0.1.0/recursa.egg-info/entry_points.txt +2 -0
- recursa-0.1.0/recursa.egg-info/requires.txt +9 -0
- recursa-0.1.0/recursa.egg-info/top_level.txt +1 -0
- recursa-0.1.0/setup.cfg +4 -0
recursa-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prune tests
|
recursa-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: recursa
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Agents that get better every run: eval-gated, human-approved self-improvement for LLM agents.
|
|
5
|
+
Author: Neel Kamal
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/neelkamal0666/Recursa
|
|
8
|
+
Project-URL: Repository, https://github.com/neelkamal0666/Recursa
|
|
9
|
+
Project-URL: Issues, https://github.com/neelkamal0666/Recursa/issues
|
|
10
|
+
Keywords: llm,agents,evaluation,prompt-optimization,human-in-the-loop,self-improving
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: cryptography>=42
|
|
24
|
+
Requires-Dist: pyyaml>=6
|
|
25
|
+
Provides-Extra: anthropic
|
|
26
|
+
Requires-Dist: anthropic>=0.70; extra == "anthropic"
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
29
|
+
Requires-Dist: anthropic>=0.70; extra == "dev"
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# Recursa v1
|
|
33
|
+
|
|
34
|
+
*Agents that get better every run.* Recursa wraps an existing LLM agent, records and scores its runs, proposes system-prompt changes from its failures, and promotes a change only when it beats the current version on a sealed evaluation set **and** a named human approves it.
|
|
35
|
+
|
|
36
|
+
This is the v1 described in [the design document](https://github.com/neelkamal0666/Recursa/blob/main/Self-Learning%20Agent%20Framework%20—%20Design%20Document.md): one adapter, one candidate type (system prompt), deterministic primary metric, sealed gate and confirmation sets, authenticated human approval, whole-version promote and rollback.
|
|
37
|
+
|
|
38
|
+
## Quickstart
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
python3.12 -m venv .venv && source .venv/bin/activate
|
|
42
|
+
pip install -e '.[dev]'
|
|
43
|
+
bash examples/triage/demo.sh # full loop on the reference agent, offline, about 10 seconds
|
|
44
|
+
pytest -q # 22 tests, including one or more per acceptance criterion
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Step by step (from `examples/triage/`):
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
python generate.py pool --n 800 --out pool.jsonl
|
|
51
|
+
recursa init --reviewer alice # creates v1 from the agent's current prompt
|
|
52
|
+
recursa data split pool.jsonl # train/dev open; gate/confirm encrypted; deletes pool.jsonl
|
|
53
|
+
recursa auth add-reviewer alice # sets alice's passphrase (alice must be in reviewers.yaml)
|
|
54
|
+
recursa eval --split dev # benchmark the current version
|
|
55
|
+
recursa cycle # reflect -> propose -> dev -> gate -> confirm -> review queue
|
|
56
|
+
recursa review list
|
|
57
|
+
recursa review show <candidate> # diff, CIs, guardrails, insights, side-by-side cases, packet hash
|
|
58
|
+
recursa auth login alice # 15-minute signed token
|
|
59
|
+
recursa approve <candidate> --packet <hash prefix> # or: reject / request-changes --reason ...
|
|
60
|
+
recursa rollback v1 --reason "..." # one command; re-run `recursa eval` to verify
|
|
61
|
+
recursa ui # local review dashboard on 127.0.0.1:8765
|
|
62
|
+
recursa audit --verify # hash-chained audit log
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Wrapping your own agent
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import recursa as rc
|
|
69
|
+
|
|
70
|
+
@rc.tool(capability="read_only") # every tool declares a capability class
|
|
71
|
+
async def lookup_order(order_id): ...
|
|
72
|
+
|
|
73
|
+
@rc.tool(capability="irreversible", mock=lambda amount: {"refunded": amount})
|
|
74
|
+
async def refund(amount): ... # in eval mode the mock runs, never the real call
|
|
75
|
+
|
|
76
|
+
async def my_agent(task: dict, ctx: rc.Context):
|
|
77
|
+
async with rc.trace.model("claude-opus-5-5", input=task) as call:
|
|
78
|
+
... # call your model with ctx.system_prompt
|
|
79
|
+
call.set_usage(input_tokens, output_tokens, cost_usd)
|
|
80
|
+
return {"category": ..., "priority": ...}
|
|
81
|
+
|
|
82
|
+
def build() -> rc.Project: # referenced from recursa.yaml as factory: module:build
|
|
83
|
+
agent = rc.wrap(my_agent, name="support-bot", surfaces={"system_prompt": PROMPT},
|
|
84
|
+
model={"id": "claude-opus-5-5"}, tools=[lookup_order, refund])
|
|
85
|
+
suite = rc.EvalSuite(primary=rc.evals.FieldsMatch(["category", "priority"]),
|
|
86
|
+
safety=[rc.evals.NoPIIEcho()])
|
|
87
|
+
return rc.Project(agent=agent, suite=suite)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Labelled data is JSONL: `{"id", "input": {...}, "expected": {...}, "group", "tags"}`. `group` clusters correlated examples (same customer, template or source) for the bootstrap.
|
|
91
|
+
|
|
92
|
+
## How v1 meets the acceptance criteria
|
|
93
|
+
|
|
94
|
+
| Criterion | How | Test |
|
|
95
|
+
| --- | --- | --- |
|
|
96
|
+
| Reproducible benchmarks | Each eval run gets a deterministic `ctx.seed` per (example, sample); results are cached by (config hash, example, sample, suite version) | `test_benchmark_is_reproducible` |
|
|
97
|
+
| A cycle produces a candidate that passes gate and confirmation | `recursa cycle`: isolated optimizer → dev screening → sealed gate → one-time confirmation set | `test_cycle_yields_eligible_candidate` |
|
|
98
|
+
| Gain ≥ minimum, lower bound > 0 | Paired comparison, 3 samples per example, cluster bootstrap over groups, one-sided bound at α/budget (Bonferroni) | same |
|
|
99
|
+
| No guardrail regression | Cost and p95 latency: one-sided non-inferiority tests with Holm correction; safety: zero regressions | same, plus `test_compare_*` |
|
|
100
|
+
| Authenticated approval bound to the candidate | Passphrase (scrypt) → signed 15-minute token; reviewer must be listed for the surface; approval records the hash of the exact review packet; quorum from `reviewers.yaml` | `test_promotion_requires_authenticated_bound_approval` |
|
|
101
|
+
| One-command rollback, verified by eval | `recursa rollback vN` restores the full pinned config (prompt, model, tools, suite and dataset versions) | `test_rollback_restores_previous_version` |
|
|
102
|
+
| Optimizer cannot read gate/confirmation data | See below | `test_isolation.py` |
|
|
103
|
+
|
|
104
|
+
### Sealed data and optimizer isolation
|
|
105
|
+
|
|
106
|
+
- Gate and confirmation sets are encrypted at rest (AES-256-GCM). The key sits in `.recursa/keys/gate.key` (mode 0600, directory 0700). `recursa data split` deletes the plaintext pool by default.
|
|
107
|
+
- The optimizer (reflection, prompt rewriting, dev evaluation) runs in a child process, `python -m recursa.worker`. It receives only train and dev data on stdin, gets an environment with every `RECURSA_*` variable removed, and installs a CPython audit hook before importing any user code. Audit hooks can't be removed once installed. The hook refuses to open, list, glob, link, chmod, or connect via SQLite to anything under `sealed/`, `keys/`, `auth/`, or the main store. It also refuses to spawn subprocesses or load native code through ctypes. The test runs 17 escape attempts, and all of them are denied.
|
|
108
|
+
- The optimizer never learns gate outcomes, not even pass or fail. Everything derived from sealed data (gate runs, reports, review packets) lives in `.recursa/sealed/gate.db`. Human rejections reach the optimizer as text, so it stops proposing the same change.
|
|
109
|
+
- Gate budget: each gate-set version allows 20 candidate evaluations, and each candidate is evaluated at most once. Rotate with `recursa data rotate gate fresh.jsonl`, which retires the old version into dev. The confirmation set is used once per promotion decision and then retired into dev. Seal a fresh one with `recursa data seal confirm file.jsonl`.
|
|
110
|
+
- **Limitation:** the audit hook is defence in depth inside one OS user. For a hard boundary in production, run the gate under a separate OS user that alone owns `sealed/` and `keys/`.
|
|
111
|
+
|
|
112
|
+
### Other guarantees
|
|
113
|
+
|
|
114
|
+
- Runs, scores, feedback, versions, deployments, approvals and the audit log are append-only (SQLite triggers abort UPDATE and DELETE). The audit log is hash-chained.
|
|
115
|
+
- PII redaction runs before persistence and fails closed: if it errors, the run is kept without its payload. Review packets are redacted too.
|
|
116
|
+
- In eval mode, only `read_only` tools run live. Every other capability class must provide a mock; otherwise the run fails closed with `SandboxViolation`.
|
|
117
|
+
- Retries are attempts nested inside one run id. Feedback is idempotent per key. Online traces go to an fsync'd write-ahead log first (`learner.flush()` ingests them).
|
|
118
|
+
- A candidate goes stale after 7 days or when the baseline changes, and must then pass the gate again. "Request changes" with an edited prompt creates a new human-sourced candidate that must also pass the gate.
|
|
119
|
+
|
|
120
|
+
## Reference agent
|
|
121
|
+
|
|
122
|
+
[examples/triage/](https://github.com/neelkamal0666/Recursa/tree/main/examples/triage) is a support-ticket triage agent. It returns `{"category", "priority"}` and succeeds when both fields match the label. The design doc left the reference agent as an open decision, so this one is a stand-in:
|
|
123
|
+
|
|
124
|
+
- `TRIAGE_BACKEND=sim` (default) uses a seeded simulated model. It follows the prompt's guidance and its learned rules, misses cues the prompt doesn't mention, and makes 3% seeded random errors. This lets the whole loop be verified offline at no cost.
|
|
125
|
+
- `TRIAGE_BACKEND=claude` calls `claude-opus-5-5` at low effort, with server-side refusal fallbacks enabled, through the Anthropic SDK.
|
|
126
|
+
- The labelled data is synthetic (`generate.py`). Real v1 validation needs at least 400 human-labelled tasks from the chosen production agent. Synthetic data that the optimizer could regenerate is not a sealed set in any meaningful sense.
|
|
127
|
+
|
|
128
|
+
Typical result: the dev success rate goes from 0.61 to 0.71 in the first cycle. On the gate set it goes from 0.56 to 0.71 (lower bound +0.09 at α=0.0025), with a cost ratio of about 1.08 and no safety regressions.
|
|
129
|
+
|
|
130
|
+
## Optimizer strategies
|
|
131
|
+
|
|
132
|
+
- `heuristic` (default): `BatchErrorAnalysis` clusters dev failures by (field, expected, got) and finds a phrase that is common in the cluster and rare elsewhere. `RuleRewriter` then appends one compact rule per insight and proposes nested candidates (top 1, 2, 3, 4 rules). Dev screening picks the best one that stays within the cost guardrail.
|
|
133
|
+
- `claude`: `ClaudeRewriter` sends the prompt, failure modes, failure samples and past rejections to `claude-opus-5-5` and asks for a minimal rewrite. Set `optimizer.strategy: claude` and provide Anthropic credentials.
|
|
134
|
+
- Plugins: set `optimizer.plugin: module:Class` to supply any class with `propose(...)`.
|
|
135
|
+
|
|
136
|
+
## Deferred (per the v1 scope table)
|
|
137
|
+
|
|
138
|
+
Other adapters (LangGraph, Agent SDK, MCP), Postgres, OTel export, calibrated judges in the gate (`rc.evals.Judge` exists but is advisory only), few-shot/memory/tool candidates, failure mining and synthetic growth, shadow and canary rollout, Slack/GitHub approval channels, and OIDC (v1 uses local passphrase accounts; the token format is the integration seam).
|
|
139
|
+
|
|
140
|
+
## Layout
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
recursa/
|
|
144
|
+
adapter.py wrap(), Agent, Context: the v1 adapter contract
|
|
145
|
+
trace.py rc.trace.model(), @rc.tool(capability, mock): instrumentation + sandbox
|
|
146
|
+
store.py SQLite store: append-only tables, hash-chained audit log
|
|
147
|
+
data.py splits, sealing (AES-GCM), rotation
|
|
148
|
+
evals.py evaluators, suites, cached eval runner
|
|
149
|
+
stats.py paired cluster bootstrap, Holm, gate decision
|
|
150
|
+
reflect.py batch error analysis -> insights
|
|
151
|
+
optimize.py prompt rewriters (rules, Claude)
|
|
152
|
+
worker.py isolated optimizer process
|
|
153
|
+
isolation.py audit-hook read isolation
|
|
154
|
+
learner.py cycle, gate, confirmation, packets, approval, promotion, rollback
|
|
155
|
+
auth.py reviewers.yaml, passphrases, signed tokens
|
|
156
|
+
cli.py, ui.py
|
|
157
|
+
```
|
recursa-0.1.0/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# Recursa v1
|
|
2
|
+
|
|
3
|
+
*Agents that get better every run.* Recursa wraps an existing LLM agent, records and scores its runs, proposes system-prompt changes from its failures, and promotes a change only when it beats the current version on a sealed evaluation set **and** a named human approves it.
|
|
4
|
+
|
|
5
|
+
This is the v1 described in [the design document](https://github.com/neelkamal0666/Recursa/blob/main/Self-Learning%20Agent%20Framework%20—%20Design%20Document.md): one adapter, one candidate type (system prompt), deterministic primary metric, sealed gate and confirmation sets, authenticated human approval, whole-version promote and rollback.
|
|
6
|
+
|
|
7
|
+
## Quickstart
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
python3.12 -m venv .venv && source .venv/bin/activate
|
|
11
|
+
pip install -e '.[dev]'
|
|
12
|
+
bash examples/triage/demo.sh # full loop on the reference agent, offline, about 10 seconds
|
|
13
|
+
pytest -q # 22 tests, including one or more per acceptance criterion
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Step by step (from `examples/triage/`):
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
python generate.py pool --n 800 --out pool.jsonl
|
|
20
|
+
recursa init --reviewer alice # creates v1 from the agent's current prompt
|
|
21
|
+
recursa data split pool.jsonl # train/dev open; gate/confirm encrypted; deletes pool.jsonl
|
|
22
|
+
recursa auth add-reviewer alice # sets alice's passphrase (alice must be in reviewers.yaml)
|
|
23
|
+
recursa eval --split dev # benchmark the current version
|
|
24
|
+
recursa cycle # reflect -> propose -> dev -> gate -> confirm -> review queue
|
|
25
|
+
recursa review list
|
|
26
|
+
recursa review show <candidate> # diff, CIs, guardrails, insights, side-by-side cases, packet hash
|
|
27
|
+
recursa auth login alice # 15-minute signed token
|
|
28
|
+
recursa approve <candidate> --packet <hash prefix> # or: reject / request-changes --reason ...
|
|
29
|
+
recursa rollback v1 --reason "..." # one command; re-run `recursa eval` to verify
|
|
30
|
+
recursa ui # local review dashboard on 127.0.0.1:8765
|
|
31
|
+
recursa audit --verify # hash-chained audit log
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Wrapping your own agent
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
import recursa as rc
|
|
38
|
+
|
|
39
|
+
@rc.tool(capability="read_only") # every tool declares a capability class
|
|
40
|
+
async def lookup_order(order_id): ...
|
|
41
|
+
|
|
42
|
+
@rc.tool(capability="irreversible", mock=lambda amount: {"refunded": amount})
|
|
43
|
+
async def refund(amount): ... # in eval mode the mock runs, never the real call
|
|
44
|
+
|
|
45
|
+
async def my_agent(task: dict, ctx: rc.Context):
|
|
46
|
+
async with rc.trace.model("claude-opus-5-5", input=task) as call:
|
|
47
|
+
... # call your model with ctx.system_prompt
|
|
48
|
+
call.set_usage(input_tokens, output_tokens, cost_usd)
|
|
49
|
+
return {"category": ..., "priority": ...}
|
|
50
|
+
|
|
51
|
+
def build() -> rc.Project: # referenced from recursa.yaml as factory: module:build
|
|
52
|
+
agent = rc.wrap(my_agent, name="support-bot", surfaces={"system_prompt": PROMPT},
|
|
53
|
+
model={"id": "claude-opus-5-5"}, tools=[lookup_order, refund])
|
|
54
|
+
suite = rc.EvalSuite(primary=rc.evals.FieldsMatch(["category", "priority"]),
|
|
55
|
+
safety=[rc.evals.NoPIIEcho()])
|
|
56
|
+
return rc.Project(agent=agent, suite=suite)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Labelled data is JSONL: `{"id", "input": {...}, "expected": {...}, "group", "tags"}`. `group` clusters correlated examples (same customer, template or source) for the bootstrap.
|
|
60
|
+
|
|
61
|
+
## How v1 meets the acceptance criteria
|
|
62
|
+
|
|
63
|
+
| Criterion | How | Test |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| Reproducible benchmarks | Each eval run gets a deterministic `ctx.seed` per (example, sample); results are cached by (config hash, example, sample, suite version) | `test_benchmark_is_reproducible` |
|
|
66
|
+
| A cycle produces a candidate that passes gate and confirmation | `recursa cycle`: isolated optimizer → dev screening → sealed gate → one-time confirmation set | `test_cycle_yields_eligible_candidate` |
|
|
67
|
+
| Gain ≥ minimum, lower bound > 0 | Paired comparison, 3 samples per example, cluster bootstrap over groups, one-sided bound at α/budget (Bonferroni) | same |
|
|
68
|
+
| No guardrail regression | Cost and p95 latency: one-sided non-inferiority tests with Holm correction; safety: zero regressions | same, plus `test_compare_*` |
|
|
69
|
+
| Authenticated approval bound to the candidate | Passphrase (scrypt) → signed 15-minute token; reviewer must be listed for the surface; approval records the hash of the exact review packet; quorum from `reviewers.yaml` | `test_promotion_requires_authenticated_bound_approval` |
|
|
70
|
+
| One-command rollback, verified by eval | `recursa rollback vN` restores the full pinned config (prompt, model, tools, suite and dataset versions) | `test_rollback_restores_previous_version` |
|
|
71
|
+
| Optimizer cannot read gate/confirmation data | See below | `test_isolation.py` |
|
|
72
|
+
|
|
73
|
+
### Sealed data and optimizer isolation
|
|
74
|
+
|
|
75
|
+
- Gate and confirmation sets are encrypted at rest (AES-256-GCM). The key sits in `.recursa/keys/gate.key` (mode 0600, directory 0700). `recursa data split` deletes the plaintext pool by default.
|
|
76
|
+
- The optimizer (reflection, prompt rewriting, dev evaluation) runs in a child process, `python -m recursa.worker`. It receives only train and dev data on stdin, gets an environment with every `RECURSA_*` variable removed, and installs a CPython audit hook before importing any user code. Audit hooks can't be removed once installed. The hook refuses to open, list, glob, link, chmod, or connect via SQLite to anything under `sealed/`, `keys/`, `auth/`, or the main store. It also refuses to spawn subprocesses or load native code through ctypes. The test runs 17 escape attempts, and all of them are denied.
|
|
77
|
+
- The optimizer never learns gate outcomes, not even pass or fail. Everything derived from sealed data (gate runs, reports, review packets) lives in `.recursa/sealed/gate.db`. Human rejections reach the optimizer as text, so it stops proposing the same change.
|
|
78
|
+
- Gate budget: each gate-set version allows 20 candidate evaluations, and each candidate is evaluated at most once. Rotate with `recursa data rotate gate fresh.jsonl`, which retires the old version into dev. The confirmation set is used once per promotion decision and then retired into dev. Seal a fresh one with `recursa data seal confirm file.jsonl`.
|
|
79
|
+
- **Limitation:** the audit hook is defence in depth inside one OS user. For a hard boundary in production, run the gate under a separate OS user that alone owns `sealed/` and `keys/`.
|
|
80
|
+
|
|
81
|
+
### Other guarantees
|
|
82
|
+
|
|
83
|
+
- Runs, scores, feedback, versions, deployments, approvals and the audit log are append-only (SQLite triggers abort UPDATE and DELETE). The audit log is hash-chained.
|
|
84
|
+
- PII redaction runs before persistence and fails closed: if it errors, the run is kept without its payload. Review packets are redacted too.
|
|
85
|
+
- In eval mode, only `read_only` tools run live. Every other capability class must provide a mock; otherwise the run fails closed with `SandboxViolation`.
|
|
86
|
+
- Retries are attempts nested inside one run id. Feedback is idempotent per key. Online traces go to an fsync'd write-ahead log first (`learner.flush()` ingests them).
|
|
87
|
+
- A candidate goes stale after 7 days or when the baseline changes, and must then pass the gate again. "Request changes" with an edited prompt creates a new human-sourced candidate that must also pass the gate.
|
|
88
|
+
|
|
89
|
+
## Reference agent
|
|
90
|
+
|
|
91
|
+
[examples/triage/](https://github.com/neelkamal0666/Recursa/tree/main/examples/triage) is a support-ticket triage agent. It returns `{"category", "priority"}` and succeeds when both fields match the label. The design doc left the reference agent as an open decision, so this one is a stand-in:
|
|
92
|
+
|
|
93
|
+
- `TRIAGE_BACKEND=sim` (default) uses a seeded simulated model. It follows the prompt's guidance and its learned rules, misses cues the prompt doesn't mention, and makes 3% seeded random errors. This lets the whole loop be verified offline at no cost.
|
|
94
|
+
- `TRIAGE_BACKEND=claude` calls `claude-opus-5-5` at low effort, with server-side refusal fallbacks enabled, through the Anthropic SDK.
|
|
95
|
+
- The labelled data is synthetic (`generate.py`). Real v1 validation needs at least 400 human-labelled tasks from the chosen production agent. Synthetic data that the optimizer could regenerate is not a sealed set in any meaningful sense.
|
|
96
|
+
|
|
97
|
+
Typical result: the dev success rate goes from 0.61 to 0.71 in the first cycle. On the gate set it goes from 0.56 to 0.71 (lower bound +0.09 at α=0.0025), with a cost ratio of about 1.08 and no safety regressions.
|
|
98
|
+
|
|
99
|
+
## Optimizer strategies
|
|
100
|
+
|
|
101
|
+
- `heuristic` (default): `BatchErrorAnalysis` clusters dev failures by (field, expected, got) and finds a phrase that is common in the cluster and rare elsewhere. `RuleRewriter` then appends one compact rule per insight and proposes nested candidates (top 1, 2, 3, 4 rules). Dev screening picks the best one that stays within the cost guardrail.
|
|
102
|
+
- `claude`: `ClaudeRewriter` sends the prompt, failure modes, failure samples and past rejections to `claude-opus-5-5` and asks for a minimal rewrite. Set `optimizer.strategy: claude` and provide Anthropic credentials.
|
|
103
|
+
- Plugins: set `optimizer.plugin: module:Class` to supply any class with `propose(...)`.
|
|
104
|
+
|
|
105
|
+
## Deferred (per the v1 scope table)
|
|
106
|
+
|
|
107
|
+
Other adapters (LangGraph, Agent SDK, MCP), Postgres, OTel export, calibrated judges in the gate (`rc.evals.Judge` exists but is advisory only), few-shot/memory/tool candidates, failure mining and synthetic growth, shadow and canary rollout, Slack/GitHub approval channels, and OIDC (v1 uses local passphrase accounts; the token format is the integration seam).
|
|
108
|
+
|
|
109
|
+
## Layout
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
recursa/
|
|
113
|
+
adapter.py wrap(), Agent, Context: the v1 adapter contract
|
|
114
|
+
trace.py rc.trace.model(), @rc.tool(capability, mock): instrumentation + sandbox
|
|
115
|
+
store.py SQLite store: append-only tables, hash-chained audit log
|
|
116
|
+
data.py splits, sealing (AES-GCM), rotation
|
|
117
|
+
evals.py evaluators, suites, cached eval runner
|
|
118
|
+
stats.py paired cluster bootstrap, Holm, gate decision
|
|
119
|
+
reflect.py batch error analysis -> insights
|
|
120
|
+
optimize.py prompt rewriters (rules, Claude)
|
|
121
|
+
worker.py isolated optimizer process
|
|
122
|
+
isolation.py audit-hook read isolation
|
|
123
|
+
learner.py cycle, gate, confirmation, packets, approval, promotion, rollback
|
|
124
|
+
auth.py reviewers.yaml, passphrases, signed tokens
|
|
125
|
+
cli.py, ui.py
|
|
126
|
+
```
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "recursa"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Agents that get better every run: eval-gated, human-approved self-improvement for LLM agents."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Neel Kamal" }]
|
|
14
|
+
keywords = ["llm", "agents", "evaluation", "prompt-optimization", "human-in-the-loop", "self-improving"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
24
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"cryptography>=42",
|
|
28
|
+
"pyyaml>=6",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
anthropic = ["anthropic>=0.70"]
|
|
33
|
+
dev = ["pytest>=8", "anthropic>=0.70"]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://github.com/neelkamal0666/Recursa"
|
|
37
|
+
Repository = "https://github.com/neelkamal0666/Recursa"
|
|
38
|
+
Issues = "https://github.com/neelkamal0666/Recursa/issues"
|
|
39
|
+
|
|
40
|
+
[project.scripts]
|
|
41
|
+
recursa = "recursa.cli:main"
|
|
42
|
+
|
|
43
|
+
[tool.setuptools.packages.find]
|
|
44
|
+
include = ["recursa*"]
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Recursa: agents that get better every run.
|
|
2
|
+
|
|
3
|
+
v1 proves one claim: on one reference agent, Recursa proposes a system-prompt
|
|
4
|
+
change that a human approves and that improves a pre-declared primary metric on a
|
|
5
|
+
sealed evaluation set, with no guardrail regression.
|
|
6
|
+
"""
|
|
7
|
+
from . import evals, trace
|
|
8
|
+
from .adapter import Agent, Context, wrap
|
|
9
|
+
from .evals import EvalSuite
|
|
10
|
+
from .project import Config, Project, load_config
|
|
11
|
+
from .stats import GateConfig
|
|
12
|
+
from .trace import SandboxViolation, tool
|
|
13
|
+
from .types import Example, Result
|
|
14
|
+
|
|
15
|
+
__all__ = ["Agent", "Config", "Context", "EvalSuite", "Example", "GateConfig", "Project", "Result",
|
|
16
|
+
"SandboxViolation", "evals", "load_config", "tool", "trace", "wrap", "Learner"]
|
|
17
|
+
__version__ = "0.1.0"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def __getattr__(name: str):
|
|
21
|
+
if name == "Learner": # imported lazily so the optimizer worker stays light
|
|
22
|
+
from .learner import Learner
|
|
23
|
+
|
|
24
|
+
return Learner
|
|
25
|
+
raise AttributeError(name)
|