memworthy 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 (58) hide show
  1. memworthy-0.1.0/.gitignore +36 -0
  2. memworthy-0.1.0/LICENSE +201 -0
  3. memworthy-0.1.0/PKG-INFO +228 -0
  4. memworthy-0.1.0/README.md +193 -0
  5. memworthy-0.1.0/pyproject.toml +94 -0
  6. memworthy-0.1.0/src/memworthy/__init__.py +49 -0
  7. memworthy-0.1.0/src/memworthy/checks/__init__.py +6 -0
  8. memworthy-0.1.0/src/memworthy/checks/builtins.py +60 -0
  9. memworthy-0.1.0/src/memworthy/checks/dates.py +119 -0
  10. memworthy-0.1.0/src/memworthy/checks/registry.py +64 -0
  11. memworthy-0.1.0/src/memworthy/checks/secrets.py +92 -0
  12. memworthy-0.1.0/src/memworthy/cli.py +346 -0
  13. memworthy-0.1.0/src/memworthy/cli_support.py +160 -0
  14. memworthy-0.1.0/src/memworthy/engine.py +323 -0
  15. memworthy-0.1.0/src/memworthy/evaluation.py +228 -0
  16. memworthy-0.1.0/src/memworthy/extractors.py +132 -0
  17. memworthy-0.1.0/src/memworthy/gate.py +103 -0
  18. memworthy-0.1.0/src/memworthy/judges/__init__.py +0 -0
  19. memworthy-0.1.0/src/memworthy/judges/base.py +71 -0
  20. memworthy-0.1.0/src/memworthy/judges/jev.py +153 -0
  21. memworthy-0.1.0/src/memworthy/judges/mock.py +101 -0
  22. memworthy-0.1.0/src/memworthy/judges/recorded.py +164 -0
  23. memworthy-0.1.0/src/memworthy/ledger.py +92 -0
  24. memworthy-0.1.0/src/memworthy/models.py +149 -0
  25. memworthy-0.1.0/src/memworthy/pairs.py +95 -0
  26. memworthy-0.1.0/src/memworthy/policy/__init__.py +0 -0
  27. memworthy-0.1.0/src/memworthy/policy/expr.py +380 -0
  28. memworthy-0.1.0/src/memworthy/policy/expr_check.py +86 -0
  29. memworthy-0.1.0/src/memworthy/policy/lint.py +147 -0
  30. memworthy-0.1.0/src/memworthy/policy/loader.py +356 -0
  31. memworthy-0.1.0/src/memworthy/policy/schema.py +100 -0
  32. memworthy-0.1.0/src/memworthy/py.typed +0 -0
  33. memworthy-0.1.0/src/memworthy/replay.py +135 -0
  34. memworthy-0.1.0/src/memworthy/reporting.py +61 -0
  35. memworthy-0.1.0/src/memworthy/review.py +80 -0
  36. memworthy-0.1.0/src/memworthy/sources/__init__.py +0 -0
  37. memworthy-0.1.0/src/memworthy/sources/candidates.py +39 -0
  38. memworthy-0.1.0/src/memworthy/sources/chat.py +54 -0
  39. memworthy-0.1.0/src/memworthy/sources/claude_code.py +96 -0
  40. memworthy-0.1.0/src/memworthy/sources/codex.py +116 -0
  41. memworthy-0.1.0/src/memworthy/sources/episodes.py +168 -0
  42. memworthy-0.1.0/src/memworthy/sources/pipeline.py +160 -0
  43. memworthy-0.1.0/src/memworthy/sources/session.py +69 -0
  44. memworthy-0.1.0/src/memworthy/stores/__init__.py +0 -0
  45. memworthy-0.1.0/src/memworthy/stores/base.py +113 -0
  46. memworthy-0.1.0/src/memworthy/stores/dict.py +9 -0
  47. memworthy-0.1.0/src/memworthy/stores/local.py +68 -0
  48. memworthy-0.1.0/src/memworthy/stores/markdown.py +126 -0
  49. memworthy-0.1.0/src/memworthy/stores/mem0.py +229 -0
  50. memworthy-0.1.0/src/memworthy/stores/rank.py +63 -0
  51. memworthy-0.1.0/src/memworthy/templates/demos/personal-memory.chat.json +6 -0
  52. memworthy-0.1.0/src/memworthy/templates/dev-sessions.yaml +1083 -0
  53. memworthy-0.1.0/src/memworthy/templates/fixtures/dev-sessions.json +62689 -0
  54. memworthy-0.1.0/src/memworthy/templates/fixtures/personal-memory.json +66484 -0
  55. memworthy-0.1.0/src/memworthy/templates/pairs/dev-sessions.yaml +176 -0
  56. memworthy-0.1.0/src/memworthy/templates/pairs/personal-memory.yaml +187 -0
  57. memworthy-0.1.0/src/memworthy/templates/personal-memory.yaml +181 -0
  58. memworthy-0.1.0/src/memworthy/testing.py +86 -0
@@ -0,0 +1,36 @@
1
+ # Private planning docs stay local; decisions.md is part of the repo.
2
+ /docs/*
3
+ !/docs/decisions.md
4
+
5
+ # Secrets
6
+ .env
7
+ .env.*
8
+ !.env.example
9
+
10
+ # Python
11
+ .venv/
12
+ __pycache__/
13
+ *.py[cod]
14
+ *.egg-info/
15
+ build/
16
+ dist/
17
+ .pytest_cache/
18
+ .mypy_cache/
19
+ .ruff_cache/
20
+ .coverage
21
+ htmlcov/
22
+
23
+ # Runtime output
24
+ memworthy.ledger.jsonl
25
+ *.ledger.jsonl
26
+ !tests/fixtures/**/*.ledger.jsonl
27
+ playground/backend/.state/
28
+ node_modules/
29
+ .DS_Store
30
+ .remember/
31
+
32
+
33
+ # LongMemEval run artifacts (cache, per-question rows); only README.md is tracked
34
+ /eval/results/longmemeval/*
35
+ !/eval/results/longmemeval/README.md
36
+ *.tsbuildinfo
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,228 @@
1
+ Metadata-Version: 2.5
2
+ Name: memworthy
3
+ Version: 0.1.0
4
+ Summary: Decide what an AI system should remember: policy-driven memory gating with typed judgments.
5
+ Project-URL: Homepage, https://github.com/AfnanHussain10/Memworthy
6
+ Project-URL: Repository, https://github.com/AfnanHussain10/Memworthy
7
+ Project-URL: Issues, https://github.com/AfnanHussain10/Memworthy/issues
8
+ Author: Afnan Hussain
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: agents,jev,llm,memory,policy
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Typing :: Typed
15
+ Requires-Python: >=3.10
16
+ Requires-Dist: httpx>=0.27
17
+ Requires-Dist: pydantic>=2.6
18
+ Requires-Dist: pyyaml>=6
19
+ Requires-Dist: typer>=0.12
20
+ Provides-Extra: dev
21
+ Requires-Dist: fastapi>=0.110; extra == 'dev'
22
+ Requires-Dist: matplotlib>=3.8; extra == 'dev'
23
+ Requires-Dist: mypy>=1.10; extra == 'dev'
24
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
25
+ Requires-Dist: pytest-cov>=5; extra == 'dev'
26
+ Requires-Dist: pytest>=8; extra == 'dev'
27
+ Requires-Dist: python-dotenv>=1; extra == 'dev'
28
+ Requires-Dist: ruff>=0.6; extra == 'dev'
29
+ Requires-Dist: types-pyyaml; extra == 'dev'
30
+ Requires-Dist: uvicorn>=0.29; extra == 'dev'
31
+ Provides-Extra: jev
32
+ Provides-Extra: mem0
33
+ Requires-Dist: mem0ai<2.3,>=2.2; extra == 'mem0'
34
+ Description-Content-Type: text/markdown
35
+
36
+ # Memworthy
37
+
38
+ **Memory should be decided, not just extracted.**
39
+
40
+ Agent memory usually fails at the decision step, not at retrieval. Extraction pipelines are generous, so memory fills up with errors:
41
+ - temporary states stored as permanent facts ("in Dubai this week" becomes "lives in Dubai")
42
+ - plans stored as facts
43
+ - other people's facts stored as the user's
44
+ - stale values and ignored corrections
45
+ - the assistant's guesses saved as things the user said
46
+ - secrets
47
+ - no record of why anything was kept
48
+
49
+ Memworthy is a small Python library that sits between whatever proposes memories and whatever stores them. For each candidate it:
50
+ 1. runs deterministic code checks
51
+ 2. asks TypeSafe's Jev model one batch of typed questions
52
+ 3. applies an ordered, user-editable policy to choose an action: `store`, `update`, `merge`, `supersede`, `reject`, `review`, `redact` or `store_labeled`
53
+
54
+ Every decision is written to a ledger you can audit, replay under a changed policy, and review.
55
+
56
+ ## Quickstart
57
+
58
+ ```bash
59
+ pip install "memworthy[jev]" # Python 3.10+
60
+ export TYPESAFE_API_KEY=... # https://typesafe.ai
61
+ ```
62
+
63
+ ```python
64
+ from memworthy import Candidate, DictStore, Gate
65
+
66
+ gate = Gate(policy="personal-memory", store=DictStore())
67
+ decisions = gate.ingest_messages([
68
+ {"role": "user", "content": "I live in Dubai"},
69
+ {"role": "user", "content": "Flying to Riyadh tomorrow"},
70
+ {"role": "user", "content": "I moved to Riyadh last week"},
71
+ {"role": "user", "content": "Actually I'm only considering it"},
72
+ ])
73
+ for d in decisions:
74
+ print(f"{d.action:10} {d.rule:16} {d.candidate.text}")
75
+ # store rules[9] I live in Dubai
76
+ # reject not_memory Flying to Riyadh tomorrow
77
+ # update update_existing I moved to Riyadh last week
78
+ # supersede retracted Actually I'm only considering it (rolls back to Dubai)
79
+ ```
80
+
81
+ The output above is what the bundled recorded Jev answers give; a live run may differ slightly. You can reproduce it with no API key:
82
+
83
+ ```python
84
+ from memworthy import RecordedJudge
85
+ from memworthy.judges.recorded import bundled_fixture_path
86
+
87
+ gate = Gate("personal-memory", DictStore(),
88
+ judge=RecordedJudge(bundled_fixture_path("personal-memory")))
89
+ ```
90
+
91
+ Each decision also lands in `memworthy.ledger.jsonl` with every signal, its probability, the rule that fired, and the policy and model version.
92
+
93
+ ### Other integrations
94
+
95
+ ```python
96
+ # Wrap a Mem0 client: only approved content reaches Mem0 (pip install "memworthy[jev,mem0]")
97
+ from mem0 import Memory
98
+ from memworthy import GatedMemory
99
+ memory = GatedMemory(Memory(), policy="personal-memory") # mode="input" or "candidate"
100
+ memory.add([{"role": "user", "content": "I'm in Lisbon this week"}], user_id="alice") # rejected
101
+ ```
102
+
103
+ ```bash
104
+ # Turn Claude Code / Codex sessions into an Obsidian-compatible Markdown knowledge base
105
+ memworthy run dev-sessions ~/.claude/projects --store markdown:~/kb
106
+ memworthy run dev-sessions ~/.claude/projects --store markdown:~/kb --extractor llm # OpenRouter via EXTRACTOR_MODEL
107
+ ```
108
+
109
+ ## CLI
110
+
111
+ | Command | What it does |
112
+ | --- | --- |
113
+ | `memworthy test POLICY [--judge recorded\|jev\|mock] [--pairs FILE]` | Runs the policy's tests; exits 1 on failure (CI-friendly with recorded answers) |
114
+ | `memworthy run POLICY SOURCE [--store dict\|markdown:PATH\|mem0] [--dry-run]` | Gates a candidate file, chat JSON or session folder |
115
+ | `memworthy replay LEDGER POLICY [--only-changed]` | Shows which past decisions a policy change would flip (no API calls when prompts are unchanged) |
116
+ | `memworthy lint POLICY [--strict]` | Overlapping types, unused signals, unreachable rules, vague prompts |
117
+ | `memworthy review LEDGER [--export-tests FILE]` | Accept, override or skip queued decisions; overrides become tests |
118
+ | `memworthy record POLICY INPUT` | Records live Jev answers as fixtures |
119
+ | `memworthy eval POLICY` | Contrast-pair metrics, calibration and plots into `eval/results/` |
120
+
121
+ ## Templates
122
+
123
+ Both are ordinary YAML files in `src/memworthy/templates/`; copy one and edit it.
124
+
125
+ **`personal-memory`** (v1.1) is for consumer assistants.
126
+ - **Types:** fact, preference, relationship, temporary, intent, hypothetical, noise.
127
+ - **Rules, in order:**
128
+ 1. Redact secrets.
129
+ 2. Reject anything the user didn't say.
130
+ 3. Roll back retractions.
131
+ 4. Reject temporary states, plans, hypotheticals and noise.
132
+ 5. Reject future or time-bounded statements. Dates are parsed by code, not the judge.
133
+ 6. Review sensitive data (health, finances, religion, precise location).
134
+ 7. Reject low-durability facts.
135
+ 8. Review facts about someone else.
136
+ 9. Update memories the new fact replaces.
137
+ 10. Store the rest.
138
+
139
+ ```yaml
140
+ rules:
141
+ - {name: secrets, when: "secret", then: redact}
142
+ - {name: not_user, when: "not from_user", then: reject}
143
+ - {name: retracted, when: "retraction > 0.7 and conflict", then: supersede, restore: true}
144
+ - {name: not_memory, when: "type in ['temporary', 'intent', 'hypothetical', 'noise']", then: reject}
145
+ - {name: bounded_time, when: "timing == 'bounded' or timing == 'future'", then: reject}
146
+ - {name: sensitive, when: "sensitivity >= 1.5", then: review}
147
+ - {name: low_durability, when: "durable < 0.6 or explicit < 0.5", then: reject}
148
+ - {name: wrong_subject, when: "about_subject < 0.5", then: review}
149
+ - {name: update_existing, when: "conflict and conflict.p > 0.6", then: update}
150
+ - {else: store}
151
+ ```
152
+
153
+ **`dev-sessions`** (v1.1) turns coding-agent sessions into a knowledge base.
154
+ - **Pipeline:**
155
+ 1. Sessions are split into episodes.
156
+ 2. Each episode is classified with one Jev call, and noise is dropped.
157
+ 3. Candidates are extracted.
158
+ 4. Each candidate is gated.
159
+ - **Types:** task, fix, decision, discussion, explanation, concept, convention, noise.
160
+ - **What happens to each kind:**
161
+ - Unverified fixes and tasks are labeled `unverified`, meaning no passing tests, build, commit or user sign-off.
162
+ - Explanations not grounded in files read are labeled.
163
+ - Unfinished discussions become open questions.
164
+ - Proposals the user did not endorse go to review.
165
+ - Abandoned approaches and generic textbook content are rejected.
166
+ - Later decisions supersede earlier ones, and the old entry moves to `_archive/`.
167
+ - Secrets are redacted before the judge sees them.
168
+
169
+ Policies use a small, safe expression language (a hand-written parser, never `eval`). For anything it can't express, register Python checks and rules with `@memworthy.check` and `@memworthy.rule`.
170
+
171
+ ## Evaluation results
172
+
173
+ All numbers come from real `jev-1.13.0` answers, regenerated with `memworthy eval <policy>`. Full reports, including every failure, are in [`eval/results/`](https://github.com/AfnanHussain10/Memworthy/blob/main/eval/results/README.md).
174
+
175
+ Contrast pairs are held out: one changed word flips the correct action, labels come from the template, and the policies were frozen before the pairs were recorded.
176
+
177
+ | Template | Cases | Action accuracy | Pair consistency | Target |
178
+ | --- | --- | --- | --- | --- |
179
+ | `personal-memory` v1.1 | 272 | 97.4% | 94.4% | ≥90% / ≥85%: met |
180
+ | `dev-sessions` v1.1 | 236 | 91.5% | 83.9% | pair consistency misses 85% |
181
+
182
+ - **First run.** Three pair templates had labeling or wording errors and were corrected after the first run. That run scored 94.5% / 88.1% and 87.3% / 75.4%; it is kept in the repo, and the corrections are listed in `eval/results/README.md`.
183
+ - **Remaining failures (genuine):**
184
+ - A question that embeds a fact ("Since I live in Vienna, what…") is read as an intent.
185
+ - Failed fixes are rejected as noise instead of being labeled unverified.
186
+ - Some project-specific facts are typed as explanations and labeled ungrounded.
187
+ - **Latency and cost.** The median Jev round trip was about 1 s from the author's machine, which misses the 500 ms target. The median cost is about $0.00003 per candidate.
188
+ - **Calibration.** Raw ECE is 0.05–0.29 on small samples. See the reports for what the temperature-scaled numbers do and do not mean.
189
+ - **Built-in tests are not evidence.** The template tests (124 and 105) pass 100%, but they were used to tune the prompts.
190
+ - **LongMemEval knowledge-update, with vs without Memworthy: pending real run.** Run it with `python eval/longmemeval.py longmemeval_s_cleaned.json --limit 78`; details are in `eval/results/longmemeval/README.md`.
191
+
192
+ ## Playground
193
+
194
+ `playground/` has a FastAPI backend that imports this library and a React front end:
195
+ - a guided Dubai → Riyadh demo, one timeline row per message: message → verdict → effect on memory, with "Why?" showing the rule, judge answers and thresholds
196
+ - free play with example messages
197
+ - a policy panel with one-click experiments, rule switches and a YAML editor; edits replay the conversation and mark changed decisions
198
+ - the recorded coding-session demo
199
+ - the evaluation page
200
+
201
+ ```bash
202
+ pip install -e ".[dev,jev]"
203
+ uvicorn playground.backend.app:app --port 8765 # open http://localhost:8765
204
+ ```
205
+
206
+ The built front end is committed in `playground/web/`. To change it, edit `playground/ui/` and rebuild:
207
+
208
+ ```bash
209
+ cd playground/ui && npm ci
210
+ npm run dev # http://localhost:5173, proxies /api to the backend on :8765
211
+ npm run build # typechecks, then writes ../web
212
+ ```
213
+
214
+ Replay mode uses the bundled recorded answers and needs no key. Live mode needs `TYPESAFE_API_KEY`. It is rate-limited per IP (40 calls per hour) and globally (600 per hour), and has a daily spend cap (`PLAYGROUND_DAILY_CAP_USD`, default $0.50). Every decision is labeled recorded, live or needs-live.
215
+
216
+ ## Development
217
+
218
+ ```bash
219
+ pyenv install -s 3.12 && python -m venv .venv
220
+ .venv/bin/pip install -e ".[dev,jev,mem0]"
221
+ .venv/bin/pytest -q && .venv/bin/mypy src && .venv/bin/ruff check .
222
+ ```
223
+
224
+ Tests never call live APIs; `pytest -m live` runs a few real Jev calls when `TYPESAFE_API_KEY` is set. Design decisions are in `docs/decisions.md` and build status is in `PROGRESS.md`.
225
+
226
+ ## License
227
+
228
+ Apache 2.0.
@@ -0,0 +1,193 @@
1
+ # Memworthy
2
+
3
+ **Memory should be decided, not just extracted.**
4
+
5
+ Agent memory usually fails at the decision step, not at retrieval. Extraction pipelines are generous, so memory fills up with errors:
6
+ - temporary states stored as permanent facts ("in Dubai this week" becomes "lives in Dubai")
7
+ - plans stored as facts
8
+ - other people's facts stored as the user's
9
+ - stale values and ignored corrections
10
+ - the assistant's guesses saved as things the user said
11
+ - secrets
12
+ - no record of why anything was kept
13
+
14
+ Memworthy is a small Python library that sits between whatever proposes memories and whatever stores them. For each candidate it:
15
+ 1. runs deterministic code checks
16
+ 2. asks TypeSafe's Jev model one batch of typed questions
17
+ 3. applies an ordered, user-editable policy to choose an action: `store`, `update`, `merge`, `supersede`, `reject`, `review`, `redact` or `store_labeled`
18
+
19
+ Every decision is written to a ledger you can audit, replay under a changed policy, and review.
20
+
21
+ ## Quickstart
22
+
23
+ ```bash
24
+ pip install "memworthy[jev]" # Python 3.10+
25
+ export TYPESAFE_API_KEY=... # https://typesafe.ai
26
+ ```
27
+
28
+ ```python
29
+ from memworthy import Candidate, DictStore, Gate
30
+
31
+ gate = Gate(policy="personal-memory", store=DictStore())
32
+ decisions = gate.ingest_messages([
33
+ {"role": "user", "content": "I live in Dubai"},
34
+ {"role": "user", "content": "Flying to Riyadh tomorrow"},
35
+ {"role": "user", "content": "I moved to Riyadh last week"},
36
+ {"role": "user", "content": "Actually I'm only considering it"},
37
+ ])
38
+ for d in decisions:
39
+ print(f"{d.action:10} {d.rule:16} {d.candidate.text}")
40
+ # store rules[9] I live in Dubai
41
+ # reject not_memory Flying to Riyadh tomorrow
42
+ # update update_existing I moved to Riyadh last week
43
+ # supersede retracted Actually I'm only considering it (rolls back to Dubai)
44
+ ```
45
+
46
+ The output above is what the bundled recorded Jev answers give; a live run may differ slightly. You can reproduce it with no API key:
47
+
48
+ ```python
49
+ from memworthy import RecordedJudge
50
+ from memworthy.judges.recorded import bundled_fixture_path
51
+
52
+ gate = Gate("personal-memory", DictStore(),
53
+ judge=RecordedJudge(bundled_fixture_path("personal-memory")))
54
+ ```
55
+
56
+ Each decision also lands in `memworthy.ledger.jsonl` with every signal, its probability, the rule that fired, and the policy and model version.
57
+
58
+ ### Other integrations
59
+
60
+ ```python
61
+ # Wrap a Mem0 client: only approved content reaches Mem0 (pip install "memworthy[jev,mem0]")
62
+ from mem0 import Memory
63
+ from memworthy import GatedMemory
64
+ memory = GatedMemory(Memory(), policy="personal-memory") # mode="input" or "candidate"
65
+ memory.add([{"role": "user", "content": "I'm in Lisbon this week"}], user_id="alice") # rejected
66
+ ```
67
+
68
+ ```bash
69
+ # Turn Claude Code / Codex sessions into an Obsidian-compatible Markdown knowledge base
70
+ memworthy run dev-sessions ~/.claude/projects --store markdown:~/kb
71
+ memworthy run dev-sessions ~/.claude/projects --store markdown:~/kb --extractor llm # OpenRouter via EXTRACTOR_MODEL
72
+ ```
73
+
74
+ ## CLI
75
+
76
+ | Command | What it does |
77
+ | --- | --- |
78
+ | `memworthy test POLICY [--judge recorded\|jev\|mock] [--pairs FILE]` | Runs the policy's tests; exits 1 on failure (CI-friendly with recorded answers) |
79
+ | `memworthy run POLICY SOURCE [--store dict\|markdown:PATH\|mem0] [--dry-run]` | Gates a candidate file, chat JSON or session folder |
80
+ | `memworthy replay LEDGER POLICY [--only-changed]` | Shows which past decisions a policy change would flip (no API calls when prompts are unchanged) |
81
+ | `memworthy lint POLICY [--strict]` | Overlapping types, unused signals, unreachable rules, vague prompts |
82
+ | `memworthy review LEDGER [--export-tests FILE]` | Accept, override or skip queued decisions; overrides become tests |
83
+ | `memworthy record POLICY INPUT` | Records live Jev answers as fixtures |
84
+ | `memworthy eval POLICY` | Contrast-pair metrics, calibration and plots into `eval/results/` |
85
+
86
+ ## Templates
87
+
88
+ Both are ordinary YAML files in `src/memworthy/templates/`; copy one and edit it.
89
+
90
+ **`personal-memory`** (v1.1) is for consumer assistants.
91
+ - **Types:** fact, preference, relationship, temporary, intent, hypothetical, noise.
92
+ - **Rules, in order:**
93
+ 1. Redact secrets.
94
+ 2. Reject anything the user didn't say.
95
+ 3. Roll back retractions.
96
+ 4. Reject temporary states, plans, hypotheticals and noise.
97
+ 5. Reject future or time-bounded statements. Dates are parsed by code, not the judge.
98
+ 6. Review sensitive data (health, finances, religion, precise location).
99
+ 7. Reject low-durability facts.
100
+ 8. Review facts about someone else.
101
+ 9. Update memories the new fact replaces.
102
+ 10. Store the rest.
103
+
104
+ ```yaml
105
+ rules:
106
+ - {name: secrets, when: "secret", then: redact}
107
+ - {name: not_user, when: "not from_user", then: reject}
108
+ - {name: retracted, when: "retraction > 0.7 and conflict", then: supersede, restore: true}
109
+ - {name: not_memory, when: "type in ['temporary', 'intent', 'hypothetical', 'noise']", then: reject}
110
+ - {name: bounded_time, when: "timing == 'bounded' or timing == 'future'", then: reject}
111
+ - {name: sensitive, when: "sensitivity >= 1.5", then: review}
112
+ - {name: low_durability, when: "durable < 0.6 or explicit < 0.5", then: reject}
113
+ - {name: wrong_subject, when: "about_subject < 0.5", then: review}
114
+ - {name: update_existing, when: "conflict and conflict.p > 0.6", then: update}
115
+ - {else: store}
116
+ ```
117
+
118
+ **`dev-sessions`** (v1.1) turns coding-agent sessions into a knowledge base.
119
+ - **Pipeline:**
120
+ 1. Sessions are split into episodes.
121
+ 2. Each episode is classified with one Jev call, and noise is dropped.
122
+ 3. Candidates are extracted.
123
+ 4. Each candidate is gated.
124
+ - **Types:** task, fix, decision, discussion, explanation, concept, convention, noise.
125
+ - **What happens to each kind:**
126
+ - Unverified fixes and tasks are labeled `unverified`, meaning no passing tests, build, commit or user sign-off.
127
+ - Explanations not grounded in files read are labeled.
128
+ - Unfinished discussions become open questions.
129
+ - Proposals the user did not endorse go to review.
130
+ - Abandoned approaches and generic textbook content are rejected.
131
+ - Later decisions supersede earlier ones, and the old entry moves to `_archive/`.
132
+ - Secrets are redacted before the judge sees them.
133
+
134
+ Policies use a small, safe expression language (a hand-written parser, never `eval`). For anything it can't express, register Python checks and rules with `@memworthy.check` and `@memworthy.rule`.
135
+
136
+ ## Evaluation results
137
+
138
+ All numbers come from real `jev-1.13.0` answers, regenerated with `memworthy eval <policy>`. Full reports, including every failure, are in [`eval/results/`](https://github.com/AfnanHussain10/Memworthy/blob/main/eval/results/README.md).
139
+
140
+ Contrast pairs are held out: one changed word flips the correct action, labels come from the template, and the policies were frozen before the pairs were recorded.
141
+
142
+ | Template | Cases | Action accuracy | Pair consistency | Target |
143
+ | --- | --- | --- | --- | --- |
144
+ | `personal-memory` v1.1 | 272 | 97.4% | 94.4% | ≥90% / ≥85%: met |
145
+ | `dev-sessions` v1.1 | 236 | 91.5% | 83.9% | pair consistency misses 85% |
146
+
147
+ - **First run.** Three pair templates had labeling or wording errors and were corrected after the first run. That run scored 94.5% / 88.1% and 87.3% / 75.4%; it is kept in the repo, and the corrections are listed in `eval/results/README.md`.
148
+ - **Remaining failures (genuine):**
149
+ - A question that embeds a fact ("Since I live in Vienna, what…") is read as an intent.
150
+ - Failed fixes are rejected as noise instead of being labeled unverified.
151
+ - Some project-specific facts are typed as explanations and labeled ungrounded.
152
+ - **Latency and cost.** The median Jev round trip was about 1 s from the author's machine, which misses the 500 ms target. The median cost is about $0.00003 per candidate.
153
+ - **Calibration.** Raw ECE is 0.05–0.29 on small samples. See the reports for what the temperature-scaled numbers do and do not mean.
154
+ - **Built-in tests are not evidence.** The template tests (124 and 105) pass 100%, but they were used to tune the prompts.
155
+ - **LongMemEval knowledge-update, with vs without Memworthy: pending real run.** Run it with `python eval/longmemeval.py longmemeval_s_cleaned.json --limit 78`; details are in `eval/results/longmemeval/README.md`.
156
+
157
+ ## Playground
158
+
159
+ `playground/` has a FastAPI backend that imports this library and a React front end:
160
+ - a guided Dubai → Riyadh demo, one timeline row per message: message → verdict → effect on memory, with "Why?" showing the rule, judge answers and thresholds
161
+ - free play with example messages
162
+ - a policy panel with one-click experiments, rule switches and a YAML editor; edits replay the conversation and mark changed decisions
163
+ - the recorded coding-session demo
164
+ - the evaluation page
165
+
166
+ ```bash
167
+ pip install -e ".[dev,jev]"
168
+ uvicorn playground.backend.app:app --port 8765 # open http://localhost:8765
169
+ ```
170
+
171
+ The built front end is committed in `playground/web/`. To change it, edit `playground/ui/` and rebuild:
172
+
173
+ ```bash
174
+ cd playground/ui && npm ci
175
+ npm run dev # http://localhost:5173, proxies /api to the backend on :8765
176
+ npm run build # typechecks, then writes ../web
177
+ ```
178
+
179
+ Replay mode uses the bundled recorded answers and needs no key. Live mode needs `TYPESAFE_API_KEY`. It is rate-limited per IP (40 calls per hour) and globally (600 per hour), and has a daily spend cap (`PLAYGROUND_DAILY_CAP_USD`, default $0.50). Every decision is labeled recorded, live or needs-live.
180
+
181
+ ## Development
182
+
183
+ ```bash
184
+ pyenv install -s 3.12 && python -m venv .venv
185
+ .venv/bin/pip install -e ".[dev,jev,mem0]"
186
+ .venv/bin/pytest -q && .venv/bin/mypy src && .venv/bin/ruff check .
187
+ ```
188
+
189
+ Tests never call live APIs; `pytest -m live` runs a few real Jev calls when `TYPESAFE_API_KEY` is set. Design decisions are in `docs/decisions.md` and build status is in `PROGRESS.md`.
190
+
191
+ ## License
192
+
193
+ Apache 2.0.