toolframe-audit 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 (38) hide show
  1. toolframe_audit-0.1.0/.github/workflows/ci.yml +39 -0
  2. toolframe_audit-0.1.0/.github/workflows/publish.yml +65 -0
  3. toolframe_audit-0.1.0/.gitignore +17 -0
  4. toolframe_audit-0.1.0/LICENSE +202 -0
  5. toolframe_audit-0.1.0/PKG-INFO +67 -0
  6. toolframe_audit-0.1.0/README.md +50 -0
  7. toolframe_audit-0.1.0/docs/architecture.md +114 -0
  8. toolframe_audit-0.1.0/docs/producer-integration.md +116 -0
  9. toolframe_audit-0.1.0/docs/releasing.md +68 -0
  10. toolframe_audit-0.1.0/docs/verification.md +78 -0
  11. toolframe_audit-0.1.0/examples/outbox-producer/.gitignore +4 -0
  12. toolframe_audit-0.1.0/examples/outbox-producer/LICENSE +202 -0
  13. toolframe_audit-0.1.0/examples/outbox-producer/README.md +69 -0
  14. toolframe_audit-0.1.0/examples/outbox-producer/pyproject.toml +25 -0
  15. toolframe_audit-0.1.0/examples/outbox-producer/src/toolframe_audit_example/__init__.py +7 -0
  16. toolframe_audit-0.1.0/examples/outbox-producer/src/toolframe_audit_example/migrations/0001_init.sql +4 -0
  17. toolframe_audit-0.1.0/examples/outbox-producer/src/toolframe_audit_example/plugin.py +132 -0
  18. toolframe_audit-0.1.0/examples/outbox-producer/src/toolframe_audit_example/service.py +103 -0
  19. toolframe_audit-0.1.0/pyproject.toml +47 -0
  20. toolframe_audit-0.1.0/scripts/validate_release.py +181 -0
  21. toolframe_audit-0.1.0/scripts/verify_wheel.py +76 -0
  22. toolframe_audit-0.1.0/src/toolframe_audit/__init__.py +8 -0
  23. toolframe_audit-0.1.0/src/toolframe_audit/docs/usage.md +77 -0
  24. toolframe_audit-0.1.0/src/toolframe_audit/events.py +210 -0
  25. toolframe_audit-0.1.0/src/toolframe_audit/migrations/0001_init.postgresql.sql +22 -0
  26. toolframe_audit-0.1.0/src/toolframe_audit/migrations/0001_init.sql +22 -0
  27. toolframe_audit-0.1.0/src/toolframe_audit/migrations/outbox.sql +9 -0
  28. toolframe_audit-0.1.0/src/toolframe_audit/outbox.py +162 -0
  29. toolframe_audit-0.1.0/src/toolframe_audit/page.py +478 -0
  30. toolframe_audit-0.1.0/src/toolframe_audit/plugin.py +205 -0
  31. toolframe_audit-0.1.0/src/toolframe_audit/service.py +254 -0
  32. toolframe_audit-0.1.0/tests/conftest.py +281 -0
  33. toolframe_audit-0.1.0/tests/test_assets.py +46 -0
  34. toolframe_audit-0.1.0/tests/test_core.py +529 -0
  35. toolframe_audit-0.1.0/tests/test_events.py +215 -0
  36. toolframe_audit-0.1.0/tests/test_page.py +309 -0
  37. toolframe_audit-0.1.0/tests/test_storage.py +538 -0
  38. toolframe_audit-0.1.0/uv.lock +1352 -0
@@ -0,0 +1,39 @@
1
+ name: Validate Python distribution
2
+
3
+ on:
4
+ push:
5
+ pull_request:
6
+ workflow_dispatch:
7
+ workflow_call:
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ validate:
14
+ runs-on: ${{ matrix.os }}
15
+ timeout-minutes: 25
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ os: [ubuntu-24.04, macos-15]
20
+ steps:
21
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
22
+ with:
23
+ persist-credentials: false
24
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
25
+ with:
26
+ python-version: '3.14.7'
27
+ - uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
28
+ with:
29
+ version: '0.12.8'
30
+ enable-cache: false
31
+ - name: Test and install wheel and source distribution
32
+ run: python scripts/validate_release.py --output dist/validated
33
+ - name: Retain only validated distributions
34
+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
35
+ with:
36
+ name: python-dist-${{ matrix.os }}
37
+ path: dist/validated/*
38
+ if-no-files-found: error
39
+ retention-days: 14
@@ -0,0 +1,65 @@
1
+ name: Publish Python distribution
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ index:
7
+ description: Package index; run against the matching vVERSION tag
8
+ type: choice
9
+ options: [testpypi, pypi]
10
+ default: testpypi
11
+ required: true
12
+
13
+ permissions:
14
+ contents: read
15
+
16
+ concurrency:
17
+ group: publish-${{ inputs.index }}
18
+ cancel-in-progress: false
19
+
20
+ jobs:
21
+ release-tag:
22
+ runs-on: ubuntu-24.04
23
+ timeout-minutes: 5
24
+ steps:
25
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
26
+ with:
27
+ persist-credentials: false
28
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
29
+ with:
30
+ python-version: '3.14.7'
31
+ - name: Require the exact package version tag
32
+ env:
33
+ RELEASE_REF: ${{ github.ref }}
34
+ run: |
35
+ python - <<'PYCODE'
36
+ import os, pathlib, tomllib
37
+ project = tomllib.loads(pathlib.Path('pyproject.toml').read_text())['project']
38
+ expected = 'refs/tags/v' + project['version']
39
+ if os.environ['RELEASE_REF'] != expected:
40
+ raise SystemExit('Publication requires ' + expected)
41
+ PYCODE
42
+
43
+ validate:
44
+ needs: release-tag
45
+ uses: ./.github/workflows/ci.yml
46
+ secrets: inherit
47
+
48
+ publish:
49
+ needs: [release-tag, validate]
50
+ runs-on: ubuntu-24.04
51
+ timeout-minutes: 10
52
+ environment:
53
+ name: ${{ inputs.index }}
54
+ permissions:
55
+ contents: read
56
+ id-token: write
57
+ steps:
58
+ - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
59
+ with:
60
+ name: python-dist-ubuntu-24.04
61
+ path: dist/
62
+ - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
63
+ with:
64
+ packages-dir: dist/
65
+ repository-url: ${{ inputs.index == 'testpypi' && 'https://test.pypi.org/legacy/' || 'https://upload.pypi.org/legacy/' }}
@@ -0,0 +1,17 @@
1
+ /.venv/
2
+ /build/
3
+ /dist/
4
+ /.pytest_cache/
5
+ /.ruff_cache/
6
+ /examples/outbox-producer/.venv/
7
+ /examples/outbox-producer/dist/
8
+ /examples/outbox-producer/build/
9
+ __pycache__/
10
+ *.py[cod]
11
+ *.egg-info/
12
+ .DS_Store
13
+
14
+ # Local working documents, issue state, and development worktrees.
15
+ /.local/
16
+ /.issues/
17
+ /.worktrees/
@@ -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,67 @@
1
+ Metadata-Version: 2.5
2
+ Name: toolframe-audit
3
+ Version: 0.1.0
4
+ Summary: Durable source-fenced audit events and transactional outbox helpers for Toolframe
5
+ Project-URL: Homepage, https://github.com/toolframe/toolframe-audit
6
+ Project-URL: Repository, https://github.com/toolframe/toolframe-audit
7
+ Project-URL: Documentation, https://github.com/toolframe/toolframe-audit#readme
8
+ Project-URL: Issues, https://github.com/toolframe/toolframe-audit/issues
9
+ Author-email: byfb <62798989+byfb@users.noreply.github.com>
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Requires-Python: >=3.14
13
+ Requires-Dist: toolframe-sdk==0.3.0
14
+ Provides-Extra: ui
15
+ Requires-Dist: streamlit<2,>=1.61; extra == 'ui'
16
+ Description-Content-Type: text/markdown
17
+
18
+ # Toolframe Audit
19
+
20
+ Audit stores bounded, durable event history from trusted Toolframe producers. It
21
+ provides a filtered Dashboard page, a read-only MCP tool, JSONL page exports and
22
+ explicit retention management. A reusable transactional outbox helper keeps a
23
+ producer's business mutation and pending audit event in the same SQL transaction.
24
+
25
+ ## Quick start
26
+
27
+ Python 3.14 or later, Toolframe SDK 0.3.0 and a running Toolframe application with
28
+ a SQL storage provider are required. Install from PyPI:
29
+
30
+ ```sh
31
+ toolframe --home ./my-app plugin add pypi:toolframe-audit==0.1.0 --enable
32
+ toolframe --home ./my-app plugin status audit --check
33
+ ```
34
+
35
+ An application with Dashboard, MCP and llms providers publishes the Audit page,
36
+ `audit__list_events` tool and usage document. Grant `audit.read` for read access and
37
+ the existing Manage plugins permission (`system.plugins.manage`) only to retention
38
+ operators. No client ingestion endpoint exists;
39
+ events arrive from a plugin declaring the `audit.events` dependency. Public
40
+ access policy still applies: configure authentication before exposing audit data.
41
+
42
+ For a complete local producer walkthrough, see
43
+ [the independently built example](examples/outbox-producer/README.md).
44
+ [Producer integration](docs/producer-integration.md) describes the reusable
45
+ helper, transaction boundary and replay behavior.
46
+
47
+ ## Support limits
48
+
49
+ Version 0.1.0 uses only public SDK service and storage contracts. SQLite is tested
50
+ with real managed stores and Core worker processes. PostgreSQL migration SQL is
51
+ provided, but PostgreSQL runtime behavior requires separate verification.
52
+ Producers are trusted Python plugins: this is traceable operational history,
53
+ not tamper-proof storage or cryptographic proof of historical actor identity.
54
+
55
+ Events have a 16 KiB metadata limit and a 20 KiB total payload limit. The default
56
+ capacity is 100,000 retained events; `max_events` accepts 1–1,000,000. Full stores
57
+ reject new events while pending outbox items remain for retry. Reads and exports
58
+ contain at most 100 rows per page. Retention previews and explicit prune calls
59
+ handle at most 1,000 rows per batch; no automatic purge runs. Data survives plugin
60
+ reload, disable and application restart. Pruning permanently removes selected
61
+ history and advances a cutoff that rejects late older events.
62
+
63
+ See [architecture and trust](docs/architecture.md),
64
+ [usage](src/toolframe_audit/docs/usage.md) and
65
+ [verification](docs/verification.md) for contracts and reproducible checks.
66
+
67
+ For release validation and Trusted Publisher setup, see [releasing](docs/releasing.md).
@@ -0,0 +1,50 @@
1
+ # Toolframe Audit
2
+
3
+ Audit stores bounded, durable event history from trusted Toolframe producers. It
4
+ provides a filtered Dashboard page, a read-only MCP tool, JSONL page exports and
5
+ explicit retention management. A reusable transactional outbox helper keeps a
6
+ producer's business mutation and pending audit event in the same SQL transaction.
7
+
8
+ ## Quick start
9
+
10
+ Python 3.14 or later, Toolframe SDK 0.3.0 and a running Toolframe application with
11
+ a SQL storage provider are required. Install from PyPI:
12
+
13
+ ```sh
14
+ toolframe --home ./my-app plugin add pypi:toolframe-audit==0.1.0 --enable
15
+ toolframe --home ./my-app plugin status audit --check
16
+ ```
17
+
18
+ An application with Dashboard, MCP and llms providers publishes the Audit page,
19
+ `audit__list_events` tool and usage document. Grant `audit.read` for read access and
20
+ the existing Manage plugins permission (`system.plugins.manage`) only to retention
21
+ operators. No client ingestion endpoint exists;
22
+ events arrive from a plugin declaring the `audit.events` dependency. Public
23
+ access policy still applies: configure authentication before exposing audit data.
24
+
25
+ For a complete local producer walkthrough, see
26
+ [the independently built example](examples/outbox-producer/README.md).
27
+ [Producer integration](docs/producer-integration.md) describes the reusable
28
+ helper, transaction boundary and replay behavior.
29
+
30
+ ## Support limits
31
+
32
+ Version 0.1.0 uses only public SDK service and storage contracts. SQLite is tested
33
+ with real managed stores and Core worker processes. PostgreSQL migration SQL is
34
+ provided, but PostgreSQL runtime behavior requires separate verification.
35
+ Producers are trusted Python plugins: this is traceable operational history,
36
+ not tamper-proof storage or cryptographic proof of historical actor identity.
37
+
38
+ Events have a 16 KiB metadata limit and a 20 KiB total payload limit. The default
39
+ capacity is 100,000 retained events; `max_events` accepts 1–1,000,000. Full stores
40
+ reject new events while pending outbox items remain for retry. Reads and exports
41
+ contain at most 100 rows per page. Retention previews and explicit prune calls
42
+ handle at most 1,000 rows per batch; no automatic purge runs. Data survives plugin
43
+ reload, disable and application restart. Pruning permanently removes selected
44
+ history and advances a cutoff that rejects late older events.
45
+
46
+ See [architecture and trust](docs/architecture.md),
47
+ [usage](src/toolframe_audit/docs/usage.md) and
48
+ [verification](docs/verification.md) for contracts and reproducible checks.
49
+
50
+ For release validation and Trusted Publisher setup, see [releasing](docs/releasing.md).
@@ -0,0 +1,114 @@
1
+ # Audit architecture and trust model
2
+
3
+ Toolframe Audit is an ordinary service plugin that stores bounded event history
4
+ in the application's SQL storage. Version 0.1.0 requires Python 3.14 or later
5
+ and `toolframe-sdk==0.3.0`. It requires the public
6
+ `toolframe.storage.sql` service and uses only public SDK and service contracts;
7
+ it does not import Core internals or extend the provider-kind registry.
8
+
9
+ ## Service boundary
10
+
11
+ The plugin exposes `audit.events@1`:
12
+
13
+ - `ingest` is an internal method available to declared producer dependencies.
14
+ There is no public client ingestion endpoint and no caller-supplied source
15
+ identity field.
16
+ - `list_events` is a read-only business method requiring `audit.read`.
17
+ - `prune_events` is a system method requiring the existing Manage plugins operator
18
+ permission (`system.plugins.manage`). It previews or applies one explicitly
19
+ bounded retention batch. Public business access does not grant this permission.
20
+
21
+ The plugin also publishes a Dashboard page, the read-only MCP tool
22
+ `audit__list_events`, and an `audit.read`-protected usage document. The
23
+ scoped service route is
24
+ `/_toolframe/v1/services/audit/audit.events/list_events`; it requires
25
+ `audit.read`. The Dashboard shows exact filters, empty and error states,
26
+ bounded pages, keyset navigation, and a JSONL download of the displayed page.
27
+ A page export is limited to 100 events; callers that need a larger review must
28
+ continue with `next_before`.
29
+
30
+ ## Event and storage contract
31
+
32
+ An event has these fields: `event_id`, UTC `occurred_at`, `action`,
33
+ `entity_type`, `entity_id`, `metadata`, and `source_attested_actor`. Text
34
+ fields are bounded; metadata is a JSON object of at most 16 KiB, 1,024 JSON
35
+ values including container objects, arrays and the root, and eight nesting
36
+ levels. The complete canonical event payload is at most 20
37
+ KiB. A small set of obvious credential and secret key names is rejected as a
38
+ basic defense. Producers must still minimize and redact metadata at the source.
39
+
40
+ Each stored row receives a monotonic `sequence`, the application and source
41
+ identity, the source generation, `received_at`, the original payload, and a
42
+ `delivery_actor` snapshot. Indexes support source, action, entity, and
43
+ occurred-time queries. Reads accept `limit` 1–100 and can be filtered by
44
+ source owner, action, entity type, entity ID, and UTC `since`/`until` times.
45
+ The response is additionally bounded to 512,000 encoded bytes, so a page can
46
+ contain fewer rows than its requested limit; continue with `next_before`.
47
+ The uniqueness key is
48
+ `(application_id, source_owner, event_id)`. A retry with the same key and the
49
+ same canonical content returns `duplicate` and does not consume capacity. The
50
+ same logical ID with different content is rejected. Data and deduplication
51
+ state are stored in the managed SQL store and survive reload, disable, and
52
+ restart. SQLite and PostgreSQL migration SQL are packaged; PostgreSQL runtime
53
+ support requires separate verification.
54
+
55
+ The configured `max_events` capacity defaults to 100,000 and accepts values
56
+ from 1 through 1,000,000. New events are rejected when retained capacity is
57
+ full, while producer outbox records remain available for retry. Accepted
58
+ duplicates do not consume capacity.
59
+
60
+ Capacity bounds retained event rows and payloads, rather than the complete
61
+ database file size. Storage-provider transaction receipts, indexes and database
62
+ allocation have their own overhead. Explicit pruning does not promise that a
63
+ SQLite file immediately shrinks; plan disk capacity and provider maintenance
64
+ separately.
65
+
66
+ ## Identity and trust
67
+
68
+ At the ingestion boundary, Audit takes `owner`, `owner_generation`,
69
+ `application_id`, `root_id`, and `principal` from the host-admitted call
70
+ context. Missing or malformed owner/application context is rejected, and a
71
+ producer cannot select another source by putting an override in its event.
72
+ When Core invokes Audit through a producer dependency, a missing visitor
73
+ principal is represented as the `background` delivery actor; it is not a
74
+ reason to reject an otherwise admitted service call.
75
+
76
+ The reusable producer helper captures the admitted principal at enqueue time
77
+ as `source_attested_actor`. Audit records the host-admitted delivery principal as
78
+ `delivery_actor` on first insertion; retries do not rewrite it. The historical
79
+ source actor is vouched for by the producer that supplied the snapshot. It is
80
+ not cryptographically attested by Core, and a delivery actor does not prove
81
+ who originally caused the business action. Audit is durable, traceable
82
+ operational history, not a tamper-proof ledger.
83
+
84
+ Audit is intended for trusted Python plugins. A plugin with permission to run
85
+ arbitrary code can fabricate an otherwise valid event or alter its own local
86
+ outbox before delivery. Never put credentials, tokens, cookies, session
87
+ material, or full request and response bodies in event metadata or logs.
88
+
89
+ ## Retention
90
+
91
+ Retention is explicit and scoped. An operator with `system.plugins.manage` first asks
92
+ for a preview with a UTC cutoff and a batch limit of at most 1,000 rows. The
93
+ preview returns a `through_sequence` high-water mark. Applying a prune requires
94
+ an explicit sequence cap and `preview=False`; there is no automatic purge. The
95
+ Dashboard preserves the preview's cutoff, limit and sequence cap and invalidates
96
+ the preview when inputs change. The service does not save or bind preview intent:
97
+ API operators explicitly choose the apply inputs, and eligible rows can change
98
+ under concurrent pruning. An apply never fills in a newer cap automatically. The
99
+ persisted cutoff advances monotonically, and the selected rows are deleted in
100
+ a bounded transaction.
101
+
102
+ Pruning removes the corresponding event and deduplication history. A later new
103
+ or replayed event older than the retained cutoff is rejected with
104
+ `audit_retention`; an already-existing duplicate can still acknowledge a
105
+ previously accepted outbox item. Pending producer items that fall behind the
106
+ cutoff require operator attention.
107
+
108
+ Keyset traversal is not a consistent snapshot. Inserts or explicit pruning
109
+ between requests can change which rows appear on later pages; callers that
110
+ need a point-in-time report must coordinate that operationally.
111
+
112
+ For the reusable same-store delivery pattern, see
113
+ [Producer integration](producer-integration.md). For the public read and
114
+ retention call shapes, see the packaged [usage guide](../src/toolframe_audit/docs/usage.md).
@@ -0,0 +1,116 @@
1
+ # Producer integration
2
+
3
+ The Audit package includes a small transactional outbox helper for business
4
+ plugins. It lets a producer commit its business mutation and the pending audit
5
+ event in one SQL transaction, then deliver events to Audit with finite,
6
+ at-least-once retries.
7
+
8
+ ## Add the producer migration
9
+
10
+ Append the migration returned by `outbox_migration()` to the producer's own
11
+ linear SQL store schema. The default revision is `audit_outbox_v1`; pass a
12
+ different revision or `parent` when that is required by the producer's
13
+ migration history.
14
+
15
+ ```python
16
+ from toolframe_audit import outbox_migration
17
+
18
+ # Append this value after the producer's existing store migration.
19
+ audit_outbox = outbox_migration(parent="producer_v1")
20
+ ```
21
+
22
+ The migration creates the producer-local pending outbox and its bounded
23
+ capacity counter. It does not create a second database or claim a cross-store
24
+ transaction.
25
+
26
+ ## Enqueue with the business write
27
+
28
+ Use the producer's live `StorageClient` transaction for both operations. The
29
+ helper must receive the same transaction context; it never opens a second
30
+ transaction.
31
+
32
+ Pass the exact client used for the business mutation, including any custom
33
+ storage alias. The helper rejects a client from another runtime context. If the
34
+ producer uses a nondefault alias, pass that same alias as `storage_alias` when
35
+ draining; Core still fences each store by producer ownership.
36
+
37
+ ```python
38
+ from toolframe_audit import enqueue
39
+
40
+ async with context.storage().transaction() as tx:
41
+ await tx.execute(
42
+ "UPDATE invoices SET status = ? WHERE invoice_id = ?",
43
+ ("paid", invoice_id),
44
+ )
45
+ await enqueue(
46
+ context,
47
+ tx,
48
+ event_id=f"invoice:{invoice_id}:paid:v1",
49
+ action="invoice.paid",
50
+ entity_type="invoice",
51
+ entity_id=invoice_id,
52
+ metadata={"payment_method": "card"},
53
+ )
54
+ ```
55
+
56
+ `event_id` is the producer's stable logical identifier. `action`,
57
+ `entity_type`, and `entity_id` are required. `metadata` defaults to `{}` and
58
+ `occurred_at` defaults to the current UTC time; both are validated against the
59
+ event limits. `max_pending` defaults to 1,000 and accepts 1 through 100,000.
60
+ Reuse the same timestamp and payload when enqueueing an existing logical ID;
61
+ changing `occurred_at` is conflicting content, even if the action is unchanged.
62
+ If pending capacity is full, enqueue raises and the surrounding business
63
+ transaction rolls back, leaving no business state without its audit intent.
64
+
65
+ The collector takes the source owner from the admitted producer context. The
66
+ helper snapshots the original actor and accepts no source or actor overrides. An
67
+ admitted background task without a visitor principal records `background`. The captured
68
+ `source_attested_actor` is an original snapshot vouched for by the producer;
69
+ it is not cryptographic proof supplied by Core. Redact passwords, tokens,
70
+ credentials, cookies, session material, and full request or response bodies
71
+ before calling the helper. The key-name check in Audit is only a basic defense,
72
+ not a complete redaction system.
73
+
74
+ ## Drain a finite batch
75
+
76
+ Run the drain from an admitted producer root, such as a bounded maintenance or
77
+ worker invocation:
78
+
79
+ ```python
80
+ from toolframe_audit import drain
81
+
82
+ result = await drain(context, limit=25, budget_seconds=10)
83
+ ```
84
+
85
+ `limit` defaults to 25 and is capped at 100. `budget_seconds` defaults to 10
86
+ and is capped at 30. The drain reads pending rows in enqueue order, calls the
87
+ internal `audit.events/ingest` service, and accepts only an `accepted` or
88
+ `duplicate` response for the same event ID and producer source. It then deletes
89
+ that exact pending payload in a new producer-store transaction and decrements
90
+ the pending counter. A successful delivery and acknowledgment are counted in
91
+ the returned `delivered` and `acknowledged` values.
92
+
93
+ The collector's delivery identity comes from the admitted context at the
94
+ Audit boundary. A background drain may therefore have a `background` delivery
95
+ actor when no visitor principal is present. The original actor remains the
96
+ source-attested snapshot captured during enqueue; replay does not replace it
97
+ with the actor that happens to run the drain.
98
+
99
+ There is no cross-store transaction. If the process stops after Audit inserts
100
+ the event but before the producer deletes its pending row, the next drain
101
+ replays the event. The `(application_id, source_owner, event_id)` uniqueness
102
+ key and canonical content hash make that replay a safe duplicate. A conflicting
103
+ payload for the same logical ID is rejected and requires producer attention.
104
+
105
+ ## Operational limits
106
+
107
+ The drain is deliberately short and finite. Retry remaining rows with another
108
+ admitted root rather than turning one invocation into an unbounded worker. Audit
109
+ capacity rejects new events while full; pending rows stay at the producer for
110
+ retry. If an operator prunes past a pending event's `occurred_at`, that item
111
+ may be rejected by the retention cutoff and needs explicit operational
112
+ handling.
113
+
114
+ The independently built fixture under
115
+ [`examples/outbox-producer`](../examples/outbox-producer/README.md) demonstrates
116
+ this pattern without becoming part of the Audit wheel.