mkio 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.
- mkio-0.1.0/.claude/settings.local.json +13 -0
- mkio-0.1.0/.gitignore +14 -0
- mkio-0.1.0/CLAUDE.md +59 -0
- mkio-0.1.0/LICENSE +190 -0
- mkio-0.1.0/PKG-INFO +15 -0
- mkio-0.1.0/README.md +314 -0
- mkio-0.1.0/examples/order_book/mkio.toml +71 -0
- mkio-0.1.0/examples/order_book/run.py +9 -0
- mkio-0.1.0/examples/order_book/static/index.html +177 -0
- mkio-0.1.0/examples/order_book/test0-new.csv +4 -0
- mkio-0.1.0/examples/order_book/test1-accept.csv +3 -0
- mkio-0.1.0/examples/order_book/test2-reject.json +8 -0
- mkio-0.1.0/examples/order_book/test3-fill.csv +2 -0
- mkio-0.1.0/examples/order_book/test4-cancel.csv +2 -0
- mkio-0.1.0/pyproject.toml +37 -0
- mkio-0.1.0/skills/mkio/SKILL.md +33 -0
- mkio-0.1.0/src/mkio/__init__.py +7 -0
- mkio-0.1.0/src/mkio/__main__.py +652 -0
- mkio-0.1.0/src/mkio/_expr.py +450 -0
- mkio-0.1.0/src/mkio/_json.py +23 -0
- mkio-0.1.0/src/mkio/_ref.py +70 -0
- mkio-0.1.0/src/mkio/agents/AGENTS.js.md +136 -0
- mkio-0.1.0/src/mkio/agents/AGENTS.md +183 -0
- mkio-0.1.0/src/mkio/agents/AGENTS.python.md +119 -0
- mkio-0.1.0/src/mkio/change_bus.py +59 -0
- mkio-0.1.0/src/mkio/client/__init__.py +236 -0
- mkio-0.1.0/src/mkio/client/mkio.js +243 -0
- mkio-0.1.0/src/mkio/config.py +122 -0
- mkio-0.1.0/src/mkio/database.py +173 -0
- mkio-0.1.0/src/mkio/migration.py +383 -0
- mkio-0.1.0/src/mkio/server.py +583 -0
- mkio-0.1.0/src/mkio/services/__init__.py +0 -0
- mkio-0.1.0/src/mkio/services/base.py +56 -0
- mkio-0.1.0/src/mkio/services/query.py +158 -0
- mkio-0.1.0/src/mkio/services/stream.py +165 -0
- mkio-0.1.0/src/mkio/services/subpub.py +223 -0
- mkio-0.1.0/src/mkio/services/transaction.py +195 -0
- mkio-0.1.0/src/mkio/skill_helpers/__init__.py +0 -0
- mkio-0.1.0/src/mkio/skill_helpers/__main__.py +5 -0
- mkio-0.1.0/src/mkio/skill_helpers/discover.py +69 -0
- mkio-0.1.0/src/mkio/writer.py +218 -0
- mkio-0.1.0/src/mkio/ws_protocol.py +81 -0
- mkio-0.1.0/tests/conftest.py +77 -0
- mkio-0.1.0/tests/test_cli.py +500 -0
- mkio-0.1.0/tests/test_client.py +168 -0
- mkio-0.1.0/tests/test_client_js.py +79 -0
- mkio-0.1.0/tests/test_config.py +137 -0
- mkio-0.1.0/tests/test_expr.py +293 -0
- mkio-0.1.0/tests/test_integration.py +479 -0
- mkio-0.1.0/tests/test_migration.py +191 -0
- mkio-0.1.0/tests/test_monitor.py +299 -0
- mkio-0.1.0/tests/test_ref.py +50 -0
- mkio-0.1.0/tests/test_service_detail.py +297 -0
- mkio-0.1.0/tests/test_services.py +811 -0
- mkio-0.1.0/tests/test_writer.py +320 -0
mkio-0.1.0/.gitignore
ADDED
mkio-0.1.0/CLAUDE.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# mkio
|
|
2
|
+
|
|
3
|
+
Config-driven Python microservice framework. Single TCP port serves HTTP + WebSocket, backed by embedded SQLite.
|
|
4
|
+
|
|
5
|
+
## Build & Test
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install -e ".[dev]" # Install with dev dependencies
|
|
9
|
+
pip install -e ".[fast,dev]" # With orjson + uvloop acceleration
|
|
10
|
+
pytest tests/ # Run all tests (174 tests)
|
|
11
|
+
pytest tests/ -x -v # Stop on first failure, verbose
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Architecture
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
src/mkio/
|
|
18
|
+
├── _json.py # orjson-with-fallback (dumps -> bytes, loads)
|
|
19
|
+
├── _ref.py # "YYYYMMDD HH:mm:ss.mmmuuunnnppp" ref strings
|
|
20
|
+
├── _expr.py # Expression language: tokenizer, parser, evaluator
|
|
21
|
+
├── config.py # TOML/dict loader, normalization, validation
|
|
22
|
+
├── migration.py # Schema diff, change classification, data preservation
|
|
23
|
+
├── database.py # Dual aiosqlite connections (write + read), WAL mode
|
|
24
|
+
├── change_bus.py # Async broadcast with pre-serialized bytes
|
|
25
|
+
├── writer.py # Write batcher: SAVEPOINTs, single commit per batch
|
|
26
|
+
├── ws_protocol.py # JSON envelope helpers
|
|
27
|
+
├── server.py # aiohttp wiring, WS dispatch, static serving, monitor protocol
|
|
28
|
+
├── services/
|
|
29
|
+
│ ├── base.py # Service base class with monitor notification
|
|
30
|
+
│ ├── transaction.py # Config-driven SQL ops + result cache
|
|
31
|
+
│ ├── subpub.py # In-memory cache + live push + delta reconnect + where filter
|
|
32
|
+
│ ├── stream.py # Ring buffer + ref-based cursor
|
|
33
|
+
│ └── query.py # SQLite snapshot + change feed
|
|
34
|
+
└── client/
|
|
35
|
+
├── __init__.py # Python client with auto-reconnect
|
|
36
|
+
└── mkio.js # JS client, auto-served at /mkio.js
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Key Patterns
|
|
40
|
+
|
|
41
|
+
- **Write path**: TransactionService -> WriteBatcher queue -> batch with SAVEPOINTs -> single COMMIT -> ChangeBus publish -> fan out to subscribers
|
|
42
|
+
- **In-memory databases** use shared-cache URI (`file:mkio_{uuid}?mode=memory&cache=shared`) so write and read connections see the same data
|
|
43
|
+
- **Expression language** is parsed once (at subscribe/startup), evaluated per row via AST walk. Built-in functions: `UPPER`, `LOWER`, `ROUND`, `ABS`, `COALESCE`, `IF(cond, then, else)`. `IF` short-circuits (only the chosen branch is evaluated).
|
|
44
|
+
- **Ref strings** are lexicographically sortable UTC timestamps with sub-nanosecond counter for uniqueness
|
|
45
|
+
- **Schema migration** uses recreate-table strategy for changes SQLite's ALTER TABLE can't handle
|
|
46
|
+
- **`_mkio_ref` column** is automatically added to all tables by the framework. The writer stamps each row with the transaction's `ref` on INSERT/UPDATE/UPSERT. If the client supplies a `ref`, it is used directly; otherwise the server generates one. On startup, services seed their change logs from the DB using this column, enabling delta reconnection across server restarts. Migration system excludes `_mkio_ref` from schema diffs.
|
|
47
|
+
- **Op-level `defaults`** in transaction op specs provide static values the client doesn't send (e.g., `defaults = { status = "accepted" }`). Stored in `CompiledOp.defaults`, used by `_extract_params` as fallback when the field isn't in client data.
|
|
48
|
+
- **`msgid` echo** — Transaction messages may include an optional `"msgid"` string. The server echoes it back on both result and error responses, letting clients correlate async responses. Not stored in the DB or propagated to subscribers. Supported in CLI CSV/JSON via `_ENVELOPE_KEYS`.
|
|
49
|
+
|
|
50
|
+
## Conventions
|
|
51
|
+
|
|
52
|
+
- Python 3.11+ required (for `asyncio.TaskGroup`, `tomllib`)
|
|
53
|
+
- All async tests use `pytest-asyncio` with `asyncio_mode = "auto"`
|
|
54
|
+
- `from mkio._json import dumps, loads` everywhere (never raw json/orjson)
|
|
55
|
+
- Services communicate changes via `ChangeBus` (never direct DB polling)
|
|
56
|
+
- **Subscribe protocol** uses `ref` as the recovery cursor. Clients send `"ref": "<last ref>"` to resume from that point. Transaction results include `ref` which is the same value stamped into `_mkio_ref`.
|
|
57
|
+
- **Monitor protocol**: WS clients send `{"type": "monitor", "service": "..."}` to tap into a service's inbound/outbound message flow.
|
|
58
|
+
- **Service discovery**: `GET /api/services` lists services, `GET /api/services/<name>` returns detailed usage info (fields, types, examples).
|
|
59
|
+
- **CLI tools**: `mkio services <url> [service]` lists/inspects services, `mkio send` sends transactions, `mkio subscribe` streams live data, `mkio monitor` taps traffic
|
mkio-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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 the 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 the 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 any 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
|
+
Copyright 2026 mkio contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
mkio-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mkio
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Config-driven microservice framework with WebSocket, SQLite, and expression language
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Requires-Dist: aiohttp<4,>=3.9
|
|
8
|
+
Requires-Dist: aiosqlite>=0.20
|
|
9
|
+
Provides-Extra: dev
|
|
10
|
+
Requires-Dist: pytest-aiohttp>=1.0; extra == 'dev'
|
|
11
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
12
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
13
|
+
Provides-Extra: fast
|
|
14
|
+
Requires-Dist: orjson>=3.9; extra == 'fast'
|
|
15
|
+
Requires-Dist: uvloop>=0.19; (sys_platform != 'win32') and extra == 'fast'
|
mkio-0.1.0/README.md
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
# mkio
|
|
2
|
+
|
|
3
|
+
Config-driven microservice framework for Python. Define your schema, services, and data flows in a TOML file — zero coding required for standard configurations.
|
|
4
|
+
|
|
5
|
+
A single TCP port serves HTTP and WebSocket, backed by an embedded SQLite database. Designed for restricted environments where runtime downloads aren't possible — everything installs via `pip`.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install mkio
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Create `mkio.toml`:
|
|
14
|
+
|
|
15
|
+
```toml
|
|
16
|
+
port = 8080
|
|
17
|
+
|
|
18
|
+
[tables.orders]
|
|
19
|
+
columns = { id = "TEXT PRIMARY KEY", symbol = "TEXT NOT NULL", qty = "INTEGER", status = "TEXT DEFAULT 'pending'" }
|
|
20
|
+
|
|
21
|
+
[services.add_order]
|
|
22
|
+
type = "transaction"
|
|
23
|
+
table = "orders"
|
|
24
|
+
op_type = "insert"
|
|
25
|
+
fields = ["id", "symbol", "qty"]
|
|
26
|
+
|
|
27
|
+
[services.all_orders]
|
|
28
|
+
type = "query"
|
|
29
|
+
primary_table = "orders"
|
|
30
|
+
filterable = ["status", "symbol"]
|
|
31
|
+
|
|
32
|
+
[static]
|
|
33
|
+
"/" = "./static"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Run:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
mkio serve
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or programmatically:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from mkio import serve
|
|
46
|
+
serve("mkio.toml")
|
|
47
|
+
serve({...}) # or pass a dict
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
|
|
52
|
+
- **Single port** — HTTP pages and WebSocket messages on one port
|
|
53
|
+
- **Config-driven** — define tables, transactions, and live data services in TOML
|
|
54
|
+
- **Transaction services** — insert, update, delete, upsert across multiple tables atomically
|
|
55
|
+
- **SubPub** — in-memory cache with live push to subscribers, client-side filtering, server-side `where` and `publish` formatting
|
|
56
|
+
- **Stream** — append-only ring buffer with cursor-based reconnection
|
|
57
|
+
- **Query** — snapshot + change feed from SQLite
|
|
58
|
+
- **Expression language** — safe, extensible filter and formatter expressions (`qty > 100 AND status == 'pending'`)
|
|
59
|
+
- **Schema migration** — automatic detection of safe/destructive changes with interactive confirmation
|
|
60
|
+
- **Write batching** — hundreds of writes committed in a single SQLite transaction for high throughput
|
|
61
|
+
- **Reconnection recovery** — ref-based delta sync across all service types, persisted across server restarts via `_mkio_ref` column
|
|
62
|
+
- **Client libraries** — Python and JavaScript clients with auto-reconnect and ref tracking
|
|
63
|
+
- **Graceful shutdown** — drains pending writes, checkpoints WAL, clean close
|
|
64
|
+
- **Service monitoring** — tap into any service's inbound/outbound message flow via CLI or WebSocket
|
|
65
|
+
- **Service discovery** — `GET /api/services` list and `GET /api/services/<name>` detail endpoints, `mkio services` CLI
|
|
66
|
+
- **CLI tools** — send transactions, subscribe to live data, monitor traffic, inspect services
|
|
67
|
+
|
|
68
|
+
## Service Types
|
|
69
|
+
|
|
70
|
+
### Transaction
|
|
71
|
+
|
|
72
|
+
Execute INSERT, UPDATE, DELETE, or UPSERT operations. Supports multi-table atomic transactions with named ops and cross-op bind references.
|
|
73
|
+
|
|
74
|
+
```toml
|
|
75
|
+
[services.orders]
|
|
76
|
+
type = "transaction"
|
|
77
|
+
|
|
78
|
+
[services.orders.ops]
|
|
79
|
+
new = [
|
|
80
|
+
{ table = "orders", op_type = "insert", fields = ["side", "symbol", "qty", "price"] },
|
|
81
|
+
{ table = "audit_log", op_type = "insert", defaults = { event = "new" }, bind = { order_id = "$0.id", status = "$0.status" } },
|
|
82
|
+
]
|
|
83
|
+
accept = [
|
|
84
|
+
{ table = "orders", op_type = "update", key = ["id"], fields = ["status"], defaults = { status = "accepted" } },
|
|
85
|
+
{ table = "audit_log", op_type = "insert", defaults = { event = "accepted" }, bind = { order_id = "$0.id", status = "$0.status" } },
|
|
86
|
+
]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Bind references (`$0.id`) pull values from a prior op's RETURNING row. Op-level `defaults` provide static values the client doesn't need to send — here, `event` and `status` are set automatically per operation.
|
|
90
|
+
|
|
91
|
+
### SubPub
|
|
92
|
+
|
|
93
|
+
Subscribe to get a snapshot from an in-memory cache, then receive live updates as data changes. Supports client filters, server-side `where` filtering (rows that don't match are never cached or published), and `publish` formatting with expressions including `IF(cond, then, else)`.
|
|
94
|
+
|
|
95
|
+
```toml
|
|
96
|
+
[services.last_trade]
|
|
97
|
+
type = "subpub"
|
|
98
|
+
primary_table = "orders"
|
|
99
|
+
key = "symbol"
|
|
100
|
+
where = "status == 'filled'"
|
|
101
|
+
change_log_size = 10000
|
|
102
|
+
|
|
103
|
+
[services.last_trade.publish]
|
|
104
|
+
symbol = "symbol"
|
|
105
|
+
price = "IF(side == 'Buy', price, -price)"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Stream
|
|
109
|
+
|
|
110
|
+
Append-only data with ring buffer and ref-based cursor reconnection.
|
|
111
|
+
|
|
112
|
+
```toml
|
|
113
|
+
[services.audit_feed]
|
|
114
|
+
type = "stream"
|
|
115
|
+
primary_table = "audit_log"
|
|
116
|
+
buffer_size = 10000
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Query
|
|
120
|
+
|
|
121
|
+
Snapshot from SQLite with change feed. Supports delta reconnection.
|
|
122
|
+
|
|
123
|
+
```toml
|
|
124
|
+
[services.all_orders]
|
|
125
|
+
type = "query"
|
|
126
|
+
primary_table = "orders"
|
|
127
|
+
filterable = ["status"]
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## WebSocket Protocol
|
|
131
|
+
|
|
132
|
+
Connect to `/ws` (general) or `/ws/{service_name}` (per-service).
|
|
133
|
+
|
|
134
|
+
```json
|
|
135
|
+
// Transaction
|
|
136
|
+
{"service": "add_order", "ref": "...", "data": {"id": "1", "symbol": "AAPL", "qty": 100}}
|
|
137
|
+
|
|
138
|
+
// Named op transaction
|
|
139
|
+
{"service": "orders", "ref": "...", "op": "new", "data": {"side": "Buy", "symbol": "AAPL", "qty": 100, "price": 150}}
|
|
140
|
+
|
|
141
|
+
// Transaction with msgid (echoed back on result/error for async correlation)
|
|
142
|
+
{"service": "orders", "ref": "...", "op": "new", "msgid": "req-42", "data": {"side": "Buy", "symbol": "AAPL", "qty": 100, "price": 150}}
|
|
143
|
+
|
|
144
|
+
// Subscribe
|
|
145
|
+
{"service": "all_orders", "type": "subscribe", "filter": "status == 'pending'"}
|
|
146
|
+
|
|
147
|
+
// Reconnect with ref (resumes from last seen position)
|
|
148
|
+
{"service": "audit_feed", "type": "subscribe", "ref": "20260404 15:30:45.123456000000"}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Client Libraries
|
|
152
|
+
|
|
153
|
+
### Python
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from mkio.client import MkioClient
|
|
157
|
+
|
|
158
|
+
async with MkioClient("ws://localhost:8080/ws") as client:
|
|
159
|
+
result = await client.send("add_order", {"id": "1", "symbol": "AAPL", "qty": 100})
|
|
160
|
+
|
|
161
|
+
async for msg in client.subscribe("all_orders", filter="status == 'pending'"):
|
|
162
|
+
print(msg)
|
|
163
|
+
# msg["ref"] tracks position for recovery on reconnect
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### JavaScript
|
|
167
|
+
|
|
168
|
+
Auto-served at `/mkio.js` — no CDN or bundler needed.
|
|
169
|
+
|
|
170
|
+
```html
|
|
171
|
+
<script src="/mkio.js"></script>
|
|
172
|
+
<script>
|
|
173
|
+
const client = new MkioClient("ws://localhost:8080/ws");
|
|
174
|
+
await client.connect();
|
|
175
|
+
|
|
176
|
+
client.subscribe("all_orders", {
|
|
177
|
+
filter: "status == 'pending'",
|
|
178
|
+
onSnapshot: (rows) => renderTable(rows),
|
|
179
|
+
onUpdate: (op, row) => updateRow(op, row),
|
|
180
|
+
});
|
|
181
|
+
</script>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Expression Language
|
|
185
|
+
|
|
186
|
+
Used for client filters, server-side `where` filters, and `publish` formatters.
|
|
187
|
+
|
|
188
|
+
| Category | Syntax |
|
|
189
|
+
|----------|--------|
|
|
190
|
+
| Comparison | `==`, `!=`, `>`, `<`, `>=`, `<=` |
|
|
191
|
+
| Logical | `AND`, `OR`, `NOT` |
|
|
192
|
+
| Arithmetic | `+`, `-`, `*`, `/` |
|
|
193
|
+
| String | `CONTAINS`, `STARTS_WITH` |
|
|
194
|
+
| Null | `IS NULL`, `IS NOT NULL` |
|
|
195
|
+
| Functions | `UPPER()`, `LOWER()`, `ROUND()`, `ABS()`, `COALESCE()`, `IF(cond, then, else)` |
|
|
196
|
+
|
|
197
|
+
Extend with custom functions:
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from mkio import register_function
|
|
201
|
+
|
|
202
|
+
register_function("MASK_PAN", lambda s: "****" + s[-4:])
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Performance
|
|
206
|
+
|
|
207
|
+
- **Write batching** — collects writes over a 2ms window, commits as single SQLite transaction with per-request SAVEPOINTs
|
|
208
|
+
- **WAL mode** — dual connections (write + read) for concurrent reads during writes
|
|
209
|
+
- **Zero-copy fan-out** — change events serialized once, same bytes sent to all subscribers
|
|
210
|
+
- **Optional acceleration** — `pip install mkio[fast]` for orjson (5-10x JSON) and uvloop (2-4x I/O)
|
|
211
|
+
|
|
212
|
+
## CLI Tools
|
|
213
|
+
|
|
214
|
+
### List and inspect services
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
mkio services http://localhost:8080 # List all services
|
|
218
|
+
mkio services http://localhost:8080 orders # Show detail for one service
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Detail view shows fields, types, required/optional, auto-generated columns, and example commands.
|
|
222
|
+
|
|
223
|
+
### Send transactions
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
mkio send http://localhost:8080 orders --op new '{"side":"Buy","symbol":"AAPL","qty":100,"price":150}'
|
|
227
|
+
mkio send http://localhost:8080 orders --op new orders.json # From JSON file
|
|
228
|
+
mkio send http://localhost:8080 orders --op new orders.csv # From CSV file
|
|
229
|
+
mkio send http://localhost:8080 orders mixed.csv # CSV with per-row op column
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Subscribe to live data
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
mkio subscribe http://localhost:8080 all_orders
|
|
236
|
+
mkio subscribe http://localhost:8080 all_orders --filter "status == 'pending'"
|
|
237
|
+
mkio subscribe http://localhost:8080 all_orders --ref "20260404 15:30:45.123456000000" # Resume from ref
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Monitor a service
|
|
241
|
+
|
|
242
|
+
Tap into a service's inbound and outbound message flow in real time:
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
mkio monitor http://localhost:8080 last_trade
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
```
|
|
249
|
+
[15:30:45.123] >> IN subscribe
|
|
250
|
+
{ "type": "subscribe", "service": "last_trade" }
|
|
251
|
+
|
|
252
|
+
[15:30:45.125] << OUT snapshot
|
|
253
|
+
{ "type": "snapshot", "rows": [...] }
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
The monitor protocol is a native framework feature — any mkio application supports it.
|
|
257
|
+
|
|
258
|
+
## Using mkio from a Claude-Based Project
|
|
259
|
+
|
|
260
|
+
mkio ships agent-facing docs inside the package for AI-assisted integration. Three files in `src/mkio/agents/`:
|
|
261
|
+
|
|
262
|
+
- `AGENTS.md` — protocol, refs, discovery, service types (always needed)
|
|
263
|
+
- `AGENTS.python.md` — Python client API + worked example
|
|
264
|
+
- `AGENTS.js.md` — JS client API + worked example
|
|
265
|
+
|
|
266
|
+
### Option A: Reference in your project's CLAUDE.md
|
|
267
|
+
|
|
268
|
+
```markdown
|
|
269
|
+
# Python-only consumer
|
|
270
|
+
@/path/to/mkio/src/mkio/agents/AGENTS.md
|
|
271
|
+
@/path/to/mkio/src/mkio/agents/AGENTS.python.md
|
|
272
|
+
|
|
273
|
+
# JS-only consumer
|
|
274
|
+
@/path/to/mkio/src/mkio/agents/AGENTS.md
|
|
275
|
+
@/path/to/mkio/src/mkio/agents/AGENTS.js.md
|
|
276
|
+
|
|
277
|
+
# Both
|
|
278
|
+
@/path/to/mkio/src/mkio/agents/AGENTS.md
|
|
279
|
+
@/path/to/mkio/src/mkio/agents/AGENTS.python.md
|
|
280
|
+
@/path/to/mkio/src/mkio/agents/AGENTS.js.md
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
To find the installed path: `python -c "import mkio, os; print(os.path.join(os.path.dirname(mkio.__file__), 'agents'))"`
|
|
284
|
+
|
|
285
|
+
### Option B: Install the Claude Code skill
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
cp -r <mkio-checkout>/skills/mkio ~/.claude/skills/
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
The skill auto-triggers on mkio-related work and reads the agent docs from the installed package.
|
|
292
|
+
|
|
293
|
+
### Runtime service discovery
|
|
294
|
+
|
|
295
|
+
A stdlib-only helper fetches service descriptors as LLM-friendly JSON:
|
|
296
|
+
|
|
297
|
+
```bash
|
|
298
|
+
python -m mkio.skill_helpers.discover http://localhost:8080 # list services
|
|
299
|
+
python -m mkio.skill_helpers.discover http://localhost:8080 orders # full descriptor
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Schema Migration
|
|
303
|
+
|
|
304
|
+
When the config schema changes between restarts, mkio detects and classifies each difference:
|
|
305
|
+
|
|
306
|
+
- **Safe** (new table, nullable column) — applied automatically
|
|
307
|
+
- **Potentially destructive** (type change, PK change) — requires confirmation
|
|
308
|
+
- **Destructive** (remove column/table) — requires confirmation
|
|
309
|
+
|
|
310
|
+
Set `auto_migrate = true` in config for non-interactive environments.
|
|
311
|
+
|
|
312
|
+
## License
|
|
313
|
+
|
|
314
|
+
Apache-2.0
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
port = 8080
|
|
2
|
+
host = "0.0.0.0"
|
|
3
|
+
db_path = "orders.db"
|
|
4
|
+
batch_max_size = 500
|
|
5
|
+
batch_max_wait_ms = 2.0
|
|
6
|
+
|
|
7
|
+
# --- Database schema ---
|
|
8
|
+
|
|
9
|
+
[tables.orders]
|
|
10
|
+
columns = { id = "INTEGER PRIMARY KEY AUTOINCREMENT", side = "TEXT NOT NULL", symbol = "TEXT NOT NULL", qty = "INTEGER", price = "REAL DEFAULT 0", status = "TEXT DEFAULT 'pending'" }
|
|
11
|
+
|
|
12
|
+
[tables.audit_log]
|
|
13
|
+
columns = { id = "INTEGER PRIMARY KEY AUTOINCREMENT", event = "TEXT", order_id = "INTEGER", status = "TEXT" }
|
|
14
|
+
append_only = true
|
|
15
|
+
|
|
16
|
+
# --- Transaction service (all order operations at one endpoint) ---
|
|
17
|
+
|
|
18
|
+
[services.orders]
|
|
19
|
+
type = "transaction"
|
|
20
|
+
|
|
21
|
+
[services.orders.ops]
|
|
22
|
+
new = [
|
|
23
|
+
{ table = "orders", op_type = "insert", fields = ["side", "symbol", "qty", "price"] },
|
|
24
|
+
{ table = "audit_log", op_type = "insert", defaults = { event = "new" }, bind = { order_id = "$0.id", status = "$0.status" } },
|
|
25
|
+
]
|
|
26
|
+
accept = [
|
|
27
|
+
{ table = "orders", op_type = "update", key = ["id"], fields = ["status"], defaults = { status = "accepted" } },
|
|
28
|
+
{ table = "audit_log", op_type = "insert", defaults = { event = "accepted" }, bind = { order_id = "$0.id", status = "$0.status" } },
|
|
29
|
+
]
|
|
30
|
+
reject = [
|
|
31
|
+
{ table = "orders", op_type = "update", key = ["id"], fields = ["status"], defaults = { status = "rejected" } },
|
|
32
|
+
{ table = "audit_log", op_type = "insert", defaults = { event = "rejected" }, bind = { order_id = "$0.id", status = "$0.status" } },
|
|
33
|
+
]
|
|
34
|
+
fill = [
|
|
35
|
+
{ table = "orders", op_type = "update", key = ["id"], fields = ["status"], defaults = { status = "filled" } },
|
|
36
|
+
{ table = "audit_log", op_type = "insert", defaults = { event = "filled" }, bind = { order_id = "$0.id", status = "$0.status" } },
|
|
37
|
+
]
|
|
38
|
+
cancel = [
|
|
39
|
+
{ table = "orders", op_type = "update", key = ["id"], fields = ["status"], defaults = { status = "cancelled" } },
|
|
40
|
+
{ table = "audit_log", op_type = "insert", defaults = { event = "cancelled" }, bind = { order_id = "$0.id", status = "$0.status" } },
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
# --- Live data services ---
|
|
44
|
+
|
|
45
|
+
[services.last_trade]
|
|
46
|
+
type = "subpub"
|
|
47
|
+
primary_table = "orders"
|
|
48
|
+
key = "symbol"
|
|
49
|
+
where = "status == 'filled'"
|
|
50
|
+
change_log_size = 10000
|
|
51
|
+
|
|
52
|
+
[services.last_trade.publish]
|
|
53
|
+
symbol = "symbol"
|
|
54
|
+
price = "IF(side == 'Buy', price, -price)"
|
|
55
|
+
time = "_mkio_ref"
|
|
56
|
+
|
|
57
|
+
[services.audit_feed]
|
|
58
|
+
type = "stream"
|
|
59
|
+
primary_table = "audit_log"
|
|
60
|
+
buffer_size = 10000
|
|
61
|
+
|
|
62
|
+
[services.all_orders]
|
|
63
|
+
type = "query"
|
|
64
|
+
primary_table = "orders"
|
|
65
|
+
filterable = ["status", "symbol"]
|
|
66
|
+
change_log_size = 10000
|
|
67
|
+
|
|
68
|
+
# --- Static file serving ---
|
|
69
|
+
|
|
70
|
+
[static]
|
|
71
|
+
"/" = "./static"
|