memoryking 0.1.0__py3-none-any.whl
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.
- memoryking/__init__.py +3 -0
- memoryking/__main__.py +1328 -0
- memoryking/adapters/__init__.py +1 -0
- memoryking/adapters/adapter_state.py +242 -0
- memoryking/adapters/anchor_schema.sql +7 -0
- memoryking/adapters/atlas.py +441 -0
- memoryking/adapters/browser_commands.py +125 -0
- memoryking/adapters/dashboard.py +510 -0
- memoryking/adapters/dense_search.py +86 -0
- memoryking/adapters/egress_policy.py +244 -0
- memoryking/adapters/egress_schema.sql +13 -0
- memoryking/adapters/embedding.schema.json +302 -0
- memoryking/adapters/embedding_store.py +469 -0
- memoryking/adapters/execution.py +369 -0
- memoryking/adapters/experience.py +423 -0
- memoryking/adapters/experience.schema.json +1508 -0
- memoryking/adapters/experience_records.py +239 -0
- memoryking/adapters/experience_schema.sql +23 -0
- memoryking/adapters/handoff.py +494 -0
- memoryking/adapters/handoff.schema.json +299 -0
- memoryking/adapters/handoff_schema.sql +17 -0
- memoryking/adapters/index_schema.sql +11 -0
- memoryking/adapters/inference.py +454 -0
- memoryking/adapters/inference.schema.json +541 -0
- memoryking/adapters/inspection.py +1128 -0
- memoryking/adapters/inspection.schema.json +568 -0
- memoryking/adapters/knowledge.schema.json +633 -0
- memoryking/adapters/knowledge_schema.sql +37 -0
- memoryking/adapters/knowledge_store.py +800 -0
- memoryking/adapters/legacy_snapshot.py +358 -0
- memoryking/adapters/legacy_tables.py +49 -0
- memoryking/adapters/local_contract.py +45 -0
- memoryking/adapters/local_embeddings.py +189 -0
- memoryking/adapters/local_files.py +242 -0
- memoryking/adapters/local_ipc.py +258 -0
- memoryking/adapters/local_schema.sql +44 -0
- memoryking/adapters/local_store.py +632 -0
- memoryking/adapters/local_wire.schema.json +7672 -0
- memoryking/adapters/models/minilm-l6-v2-onnx.json +57 -0
- memoryking/adapters/models/ms-marco-minilm-l6-v2-onnx.json +57 -0
- memoryking/adapters/optional_schema.sql +43 -0
- memoryking/adapters/plan_dashboard.py +51 -0
- memoryking/adapters/plans.py +253 -0
- memoryking/adapters/postgres_admin.py +300 -0
- memoryking/adapters/postgres_anchor_schema.sql +9 -0
- memoryking/adapters/postgres_counter.sql +14 -0
- memoryking/adapters/postgres_egress_schema.sql +15 -0
- memoryking/adapters/postgres_experience_schema.sql +39 -0
- memoryking/adapters/postgres_index_schema.sql +7 -0
- memoryking/adapters/postgres_optional_schema.sql +71 -0
- memoryking/adapters/postgres_pool.py +77 -0
- memoryking/adapters/postgres_recovery.py +525 -0
- memoryking/adapters/postgres_schema.sql +365 -0
- memoryking/adapters/postgres_sql.py +102 -0
- memoryking/adapters/postgres_store.py +428 -0
- memoryking/adapters/postgres_summary_schema.sql +12 -0
- memoryking/adapters/postgres_trust_schema.sql +20 -0
- memoryking/adapters/processing.schema.json +471 -0
- memoryking/adapters/processing_store.py +346 -0
- memoryking/adapters/projects.py +83 -0
- memoryking/adapters/proposals.py +238 -0
- memoryking/adapters/ranking.py +315 -0
- memoryking/adapters/repository.schema.json +664 -0
- memoryking/adapters/repository_git.py +264 -0
- memoryking/adapters/repository_schema.sql +25 -0
- memoryking/adapters/repository_store.py +960 -0
- memoryking/adapters/retrieval.py +1285 -0
- memoryking/adapters/retrieval.schema.json +553 -0
- memoryking/adapters/retrieval_schema.sql +10 -0
- memoryking/adapters/shared_control.py +40 -0
- memoryking/adapters/shared_dashboard.py +636 -0
- memoryking/adapters/shared_gateway.py +63 -0
- memoryking/adapters/shared_http.py +216 -0
- memoryking/adapters/shared_mcp.py +159 -0
- memoryking/adapters/shared_service.py +124 -0
- memoryking/adapters/stdio.py +193 -0
- memoryking/adapters/stdio.tools.json +6776 -0
- memoryking/adapters/summary_schema.sql +10 -0
- memoryking/adapters/team.py +253 -0
- memoryking/adapters/timeline.py +80 -0
- memoryking/adapters/token_count.py +55 -0
- memoryking/adapters/tokenizers/LICENSE +21 -0
- memoryking/adapters/tokenizers/cl100k_base.tiktoken +100256 -0
- memoryking/adapters/trust.py +123 -0
- memoryking/adapters/trust_schema.sql +13 -0
- memoryking/adapters/web/app.css +1251 -0
- memoryking/adapters/web/app.js +907 -0
- memoryking/adapters/web/brand.js +44 -0
- memoryking/adapters/web/crown-mono.svg +1 -0
- memoryking/adapters/web/crown.svg +8 -0
- memoryking/adapters/web/elephant.png +0 -0
- memoryking/adapters/web/favicon.svg +8 -0
- memoryking/adapters/web/graph-layout.js +52 -0
- memoryking/adapters/web/graph-worker.js +10 -0
- memoryking/adapters/web/graph.js +570 -0
- memoryking/adapters/web/index.html +208 -0
- memoryking/adapters/web/observatory.js +1208 -0
- memoryking/adapters/web/team.js +151 -0
- memoryking/adapters/web/wordmark.svg +1 -0
- memoryking/adapters/working.py +971 -0
- memoryking/adapters/working.schema.json +1717 -0
- memoryking/adapters/working_schema.sql +24 -0
- memoryking/application/__init__.py +1 -0
- memoryking/application/conformance.py +50 -0
- memoryking/application/legacy_import.py +337 -0
- memoryking/application/local_port.py +9 -0
- memoryking/application/storage.py +117 -0
- memoryking/application/store_core.py +1292 -0
- memoryking/backups.py +131 -0
- memoryking/contracts/__init__.py +6 -0
- memoryking/contracts/__main__.py +20 -0
- memoryking/contracts/canonical.py +88 -0
- memoryking/contracts/embedding.schema.json +302 -0
- memoryking/contracts/embedding_generated.py +65 -0
- memoryking/contracts/experience.schema.json +1508 -0
- memoryking/contracts/experience_generated.py +239 -0
- memoryking/contracts/generated.py +122 -0
- memoryking/contracts/handoff.schema.json +299 -0
- memoryking/contracts/handoff_generated.py +69 -0
- memoryking/contracts/inference.schema.json +541 -0
- memoryking/contracts/inference_generated.py +106 -0
- memoryking/contracts/processing.schema.json +471 -0
- memoryking/contracts/processing_generated.py +82 -0
- memoryking/contracts/py.typed +0 -0
- memoryking/contracts/repository.schema.json +664 -0
- memoryking/contracts/repository_generated.py +144 -0
- memoryking/contracts/retrieval.schema.json +553 -0
- memoryking/contracts/retrieval_generated.py +110 -0
- memoryking/contracts/schema.json +696 -0
- memoryking/contracts/validation.py +92 -0
- memoryking/contracts/working.schema.json +1717 -0
- memoryking/contracts/working_generated.py +291 -0
- memoryking/domain/__init__.py +1 -0
- memoryking/domain/anchors.py +97 -0
- memoryking/domain/briefing.py +108 -0
- memoryking/domain/briefing_text.py +222 -0
- memoryking/domain/checkpoint.py +118 -0
- memoryking/domain/compression.py +64 -0
- memoryking/domain/delta.py +129 -0
- memoryking/domain/embeddings.py +90 -0
- memoryking/domain/hints.py +465 -0
- memoryking/domain/http_status.py +45 -0
- memoryking/domain/learning.py +62 -0
- memoryking/domain/local.py +65 -0
- memoryking/domain/plans.py +140 -0
- memoryking/domain/repository.py +118 -0
- memoryking/domain/selectors.py +356 -0
- memoryking/domain/summary.py +88 -0
- memoryking/domain/usage.py +163 -0
- memoryking/embedding_loop.py +98 -0
- memoryking/embedding_worker.py +131 -0
- memoryking/harness.py +882 -0
- memoryking/harness_context.py +376 -0
- memoryking/hooks.py +171 -0
- memoryking/host_hooks.py +454 -0
- memoryking/lean.py +352 -0
- memoryking/memory_tool.py +776 -0
- memoryking/migration.py +194 -0
- memoryking/model_install.py +78 -0
- memoryking/observability.py +101 -0
- memoryking/observability_metrics.py +100 -0
- memoryking/profiles.py +150 -0
- memoryking/providers.py +330 -0
- memoryking/py.typed +0 -0
- memoryking/pytest_plugin.py +69 -0
- memoryking/reference_agent.py +152 -0
- memoryking/repository_worker.py +57 -0
- memoryking/resume_adapter.py +154 -0
- memoryking/sdk.py +302 -0
- memoryking/shared.py +301 -0
- memoryking/shared_sdk.py +101 -0
- memoryking/vault.py +176 -0
- memoryking-0.1.0.dist-info/METADATA +157 -0
- memoryking-0.1.0.dist-info/RECORD +179 -0
- memoryking-0.1.0.dist-info/WHEEL +5 -0
- memoryking-0.1.0.dist-info/entry_points.txt +7 -0
- memoryking-0.1.0.dist-info/licenses/LICENSE +202 -0
- memoryking-0.1.0.dist-info/licenses/NOTICE +12 -0
- memoryking-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
BEGIN IMMEDIATE;
|
|
2
|
+
CREATE TABLE IF NOT EXISTS session_summaries (
|
|
3
|
+
tenant TEXT NOT NULL REFERENCES tenants, id TEXT NOT NULL, project TEXT NOT NULL,
|
|
4
|
+
task TEXT NOT NULL, stream TEXT NOT NULL, checkpoint TEXT NOT NULL, revision INTEGER NOT NULL,
|
|
5
|
+
principal TEXT NOT NULL, incarnation TEXT NOT NULL, method TEXT NOT NULL, text TEXT NOT NULL,
|
|
6
|
+
readers TEXT NOT NULL, created INTEGER NOT NULL, expires INTEGER NOT NULL,
|
|
7
|
+
PRIMARY KEY(tenant,id));
|
|
8
|
+
CREATE INDEX IF NOT EXISTS session_summaries_checkpoint ON session_summaries(tenant,checkpoint,created);
|
|
9
|
+
PRAGMA user_version=11;
|
|
10
|
+
COMMIT;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""Team views for the shared profile: who did what in a project, and an audit export.
|
|
2
|
+
|
|
3
|
+
Both are bounded reads over recorded facts (events, retained artifacts, egress audit,
|
|
4
|
+
grants, summaries) under the caller's current authorization. Nothing here infers
|
|
5
|
+
intent, cost or success; a count is a count of records.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from memoryking.adapters.local_store import identifier
|
|
14
|
+
from memoryking.adapters.timeline import timeline
|
|
15
|
+
from memoryking.application.store_core import StoreCore
|
|
16
|
+
from memoryking.domain.local import temporal_valid
|
|
17
|
+
|
|
18
|
+
DAY_MS = 86_400_000
|
|
19
|
+
EVENT_SCAN = 5000
|
|
20
|
+
EXPORT_EVENTS = 5000
|
|
21
|
+
EXPORT_ROWS = 2000
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
FAMILIES = {
|
|
25
|
+
"capture": "captures",
|
|
26
|
+
"observation": "captures",
|
|
27
|
+
"checkpoint": "checkpoints",
|
|
28
|
+
"handoff": "handoffs",
|
|
29
|
+
"propose": "claims",
|
|
30
|
+
"review": "claims",
|
|
31
|
+
"attest": "claims",
|
|
32
|
+
"promote": "claims",
|
|
33
|
+
"claim": "claims",
|
|
34
|
+
"revision": "claims",
|
|
35
|
+
"summary": "summaries",
|
|
36
|
+
"inference": "dispatches",
|
|
37
|
+
"egress": "dispatches",
|
|
38
|
+
"purge": "purges",
|
|
39
|
+
"grant": "policy",
|
|
40
|
+
"revoke": "policy",
|
|
41
|
+
"policy": "policy",
|
|
42
|
+
"experience": "procedures",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _family(kind: str) -> str:
|
|
47
|
+
return FAMILIES.get(kind.split("_", 1)[0], "other")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _project_grants(store: StoreCore, tenant: str, project: str, at: int) -> dict[str, list[str]]:
|
|
51
|
+
grants: dict[str, list[str]] = {}
|
|
52
|
+
for row in store.db.execute(
|
|
53
|
+
"SELECT principal,project,capabilities,starts,expires FROM grants WHERE tenant=? AND project IN (?,'*') ORDER BY principal",
|
|
54
|
+
(tenant, project),
|
|
55
|
+
).fetchall():
|
|
56
|
+
if not temporal_valid(at, row["starts"], row["expires"]):
|
|
57
|
+
continue
|
|
58
|
+
capabilities = row["capabilities"]
|
|
59
|
+
if isinstance(capabilities, str):
|
|
60
|
+
capabilities = json.loads(capabilities)
|
|
61
|
+
current = set(grants.get(row["principal"], []))
|
|
62
|
+
current.update(capabilities)
|
|
63
|
+
grants[row["principal"]] = sorted(current)
|
|
64
|
+
return grants
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def activity(
|
|
68
|
+
store: StoreCore, who: dict[str, Any], project: str, at: int, *, days: int = 30
|
|
69
|
+
) -> dict[str, Any]:
|
|
70
|
+
"""Per-principal record counts for a project over a window, newest events first."""
|
|
71
|
+
identifier(project)
|
|
72
|
+
days = max(1, min(days, 365))
|
|
73
|
+
tenant, since = who["tenant"], at - days * DAY_MS
|
|
74
|
+
rows = store.db.execute(
|
|
75
|
+
"SELECT e.kind,e.actor,e.at_ms,"
|
|
76
|
+
" o.project AS observation_project, f.project AS claim_project,"
|
|
77
|
+
" c.project AS checkpoint_project, h.project AS handoff_project"
|
|
78
|
+
" FROM events e"
|
|
79
|
+
" LEFT JOIN observations o ON o.tenant=e.tenant AND o.id=e.subject"
|
|
80
|
+
" LEFT JOIN revisions r ON r.tenant=e.tenant AND r.id=e.subject"
|
|
81
|
+
" LEFT JOIN families f ON f.tenant=r.tenant AND f.id=r.family"
|
|
82
|
+
" LEFT JOIN checkpoints c ON c.tenant=e.tenant AND c.id=e.subject"
|
|
83
|
+
" LEFT JOIN handoffs h ON h.tenant=e.tenant AND h.id=e.subject"
|
|
84
|
+
" WHERE e.tenant=? AND e.at_ms>=? ORDER BY e.position DESC LIMIT ?",
|
|
85
|
+
(tenant, since, EVENT_SCAN + 1),
|
|
86
|
+
).fetchall()
|
|
87
|
+
principals: dict[str, dict[str, Any]] = {}
|
|
88
|
+
|
|
89
|
+
def entry(principal: str) -> dict[str, Any]:
|
|
90
|
+
return principals.setdefault(
|
|
91
|
+
principal,
|
|
92
|
+
{
|
|
93
|
+
"principal": principal,
|
|
94
|
+
"grants": [],
|
|
95
|
+
"events": {},
|
|
96
|
+
"briefings": 0,
|
|
97
|
+
"dispatches_delivered": 0,
|
|
98
|
+
"last_seen_ms": None,
|
|
99
|
+
},
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
for row in rows[:EVENT_SCAN]:
|
|
103
|
+
if project not in (
|
|
104
|
+
row["observation_project"],
|
|
105
|
+
row["claim_project"],
|
|
106
|
+
row["checkpoint_project"],
|
|
107
|
+
row["handoff_project"],
|
|
108
|
+
):
|
|
109
|
+
continue
|
|
110
|
+
item = entry(row["actor"])
|
|
111
|
+
family = _family(row["kind"])
|
|
112
|
+
item["events"][family] = item["events"].get(family, 0) + 1
|
|
113
|
+
item["last_seen_ms"] = max(item["last_seen_ms"] or 0, row["at_ms"])
|
|
114
|
+
for row in store.db.execute(
|
|
115
|
+
"SELECT principal,count(*) AS n,max(created) AS last FROM rendered_artifacts WHERE tenant=? AND project=? AND created>=? GROUP BY principal",
|
|
116
|
+
(tenant, project, since),
|
|
117
|
+
).fetchall():
|
|
118
|
+
item = entry(row["principal"])
|
|
119
|
+
item["briefings"] = int(row["n"])
|
|
120
|
+
item["last_seen_ms"] = max(item["last_seen_ms"] or 0, int(row["last"]))
|
|
121
|
+
for row in store.db.execute(
|
|
122
|
+
"SELECT principal,count(*) AS n,max(evaluated) AS last FROM egress_audit WHERE tenant=? AND project=? AND evaluated>=? AND status='delivered' GROUP BY principal",
|
|
123
|
+
(tenant, project, since),
|
|
124
|
+
).fetchall():
|
|
125
|
+
item = entry(row["principal"])
|
|
126
|
+
item["dispatches_delivered"] = int(row["n"])
|
|
127
|
+
item["last_seen_ms"] = max(item["last_seen_ms"] or 0, int(row["last"]))
|
|
128
|
+
for principal, capabilities in _project_grants(store, tenant, project, at).items():
|
|
129
|
+
entry(principal)["grants"] = capabilities
|
|
130
|
+
ordered = sorted(principals.values(), key=lambda p: (-(p["last_seen_ms"] or 0), p["principal"]))
|
|
131
|
+
return {
|
|
132
|
+
"project": project,
|
|
133
|
+
"since_ms": since,
|
|
134
|
+
"until_ms": at,
|
|
135
|
+
"days": days,
|
|
136
|
+
"principals": ordered,
|
|
137
|
+
"events_scanned": min(len(rows), EVENT_SCAN),
|
|
138
|
+
"complete": len(rows) <= EVENT_SCAN,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def export(store: StoreCore, who: dict[str, Any], project: str, at: int) -> dict[str, Any]:
|
|
143
|
+
"""A bounded, content-free audit document for a project under the caller's authority.
|
|
144
|
+
|
|
145
|
+
Events, egress records, retained-artifact identities, summaries and grants; never
|
|
146
|
+
observation text, claim bodies or artifact bodies. The caller must hold admin.
|
|
147
|
+
"""
|
|
148
|
+
identifier(project)
|
|
149
|
+
tenant = who["tenant"]
|
|
150
|
+
truncated: list[str] = []
|
|
151
|
+
events: list[dict[str, Any]] = []
|
|
152
|
+
before: int | None = None
|
|
153
|
+
while True:
|
|
154
|
+
page = timeline(store, who, project, before=before)
|
|
155
|
+
events.extend(page["items"])
|
|
156
|
+
if page["next_before"] is None:
|
|
157
|
+
break
|
|
158
|
+
if len(events) >= EXPORT_EVENTS:
|
|
159
|
+
truncated.append("events") # more events remain beyond the bound
|
|
160
|
+
break
|
|
161
|
+
before = page["next_before"]
|
|
162
|
+
if len(events) > EXPORT_EVENTS:
|
|
163
|
+
events = events[:EXPORT_EVENTS]
|
|
164
|
+
if "events" not in truncated:
|
|
165
|
+
truncated.append("events")
|
|
166
|
+
egress = [
|
|
167
|
+
{
|
|
168
|
+
"permit": row["id"],
|
|
169
|
+
"principal": row["principal"],
|
|
170
|
+
"provider": row["provider"],
|
|
171
|
+
"model": row["model"],
|
|
172
|
+
"host": row["host"],
|
|
173
|
+
"policy_revision": row["policy_revision"],
|
|
174
|
+
"status": row["status"],
|
|
175
|
+
"classification": row["classification"],
|
|
176
|
+
"request_bytes": row["request_bytes"],
|
|
177
|
+
"request_hash": row["request_hash"],
|
|
178
|
+
"manifest_hash": row["manifest_hash"],
|
|
179
|
+
"wire_hash": row["wire_hash"],
|
|
180
|
+
"response_hash": row["response_hash"],
|
|
181
|
+
"input_tokens": row["input_tokens"],
|
|
182
|
+
"output_tokens": row["output_tokens"],
|
|
183
|
+
"evaluated_ms": row["evaluated"],
|
|
184
|
+
"reported_ms": row["reported"],
|
|
185
|
+
}
|
|
186
|
+
for row in store.db.execute(
|
|
187
|
+
"SELECT * FROM egress_audit WHERE tenant=? AND project=? ORDER BY evaluated DESC,id DESC LIMIT ?",
|
|
188
|
+
(tenant, project, EXPORT_ROWS + 1),
|
|
189
|
+
).fetchall()
|
|
190
|
+
]
|
|
191
|
+
if len(egress) > EXPORT_ROWS:
|
|
192
|
+
egress = egress[:EXPORT_ROWS]
|
|
193
|
+
truncated.append("egress")
|
|
194
|
+
artifacts = []
|
|
195
|
+
for row in store.db.execute(
|
|
196
|
+
"SELECT id,principal,created,expires,state,body_hash,diagnostics FROM rendered_artifacts WHERE tenant=? AND project=? ORDER BY created DESC,id DESC LIMIT ?",
|
|
197
|
+
(tenant, project, EXPORT_ROWS + 1),
|
|
198
|
+
).fetchall():
|
|
199
|
+
diag = json.loads(row["diagnostics"]) if row["diagnostics"] else {}
|
|
200
|
+
artifacts.append(
|
|
201
|
+
{
|
|
202
|
+
"receipt": row["id"],
|
|
203
|
+
"principal": row["principal"],
|
|
204
|
+
"kind": diag.get("kind", "briefing"),
|
|
205
|
+
"status": diag.get("status"),
|
|
206
|
+
"state": row["state"],
|
|
207
|
+
"created_ms": row["created"],
|
|
208
|
+
"expires_ms": row["expires"],
|
|
209
|
+
"rendered_hash": row["body_hash"],
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
if len(artifacts) > EXPORT_ROWS:
|
|
213
|
+
artifacts = artifacts[:EXPORT_ROWS]
|
|
214
|
+
truncated.append("artifacts")
|
|
215
|
+
summaries = [
|
|
216
|
+
{
|
|
217
|
+
"receipt": row["id"],
|
|
218
|
+
"task": row["task"],
|
|
219
|
+
"run": None if row["stream"] == "task" else row["stream"][4:],
|
|
220
|
+
"checkpoint": row["checkpoint"],
|
|
221
|
+
"revision": row["revision"],
|
|
222
|
+
"principal": row["principal"],
|
|
223
|
+
"method": row["method"],
|
|
224
|
+
"created_ms": row["created"],
|
|
225
|
+
}
|
|
226
|
+
for row in store.db.execute(
|
|
227
|
+
"SELECT * FROM session_summaries WHERE tenant=? AND project=? ORDER BY created DESC,id DESC LIMIT ?",
|
|
228
|
+
(tenant, project, EXPORT_ROWS + 1),
|
|
229
|
+
).fetchall()
|
|
230
|
+
]
|
|
231
|
+
if len(summaries) > EXPORT_ROWS:
|
|
232
|
+
summaries = summaries[:EXPORT_ROWS]
|
|
233
|
+
truncated.append("summaries")
|
|
234
|
+
return {
|
|
235
|
+
"schema": "audit-export-v1",
|
|
236
|
+
"store": store.store_id,
|
|
237
|
+
"tenant": tenant,
|
|
238
|
+
"incarnation": store.incarnation,
|
|
239
|
+
"project": project,
|
|
240
|
+
"generated_ms": at,
|
|
241
|
+
"generated_by": who["principal"],
|
|
242
|
+
"grants": [
|
|
243
|
+
{"principal": principal, "capabilities": capabilities}
|
|
244
|
+
for principal, capabilities in _project_grants(store, tenant, project, at).items()
|
|
245
|
+
],
|
|
246
|
+
"events": events,
|
|
247
|
+
"egress": egress,
|
|
248
|
+
"artifacts": artifacts,
|
|
249
|
+
"summaries": summaries,
|
|
250
|
+
"bounds": {"events": EXPORT_EVENTS, "rows": EXPORT_ROWS},
|
|
251
|
+
"truncated": truncated,
|
|
252
|
+
"content": "none: identities, hashes, kinds and instants only",
|
|
253
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""A project's event timeline: captures, claims, reviews, checkpoints, handoffs and purges."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from memoryking.adapters.local_store import identifier
|
|
8
|
+
from memoryking.application.store_core import StoreCore
|
|
9
|
+
|
|
10
|
+
LIMIT = 100
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def timeline(
|
|
14
|
+
store: StoreCore,
|
|
15
|
+
who: dict[str, Any],
|
|
16
|
+
project: str,
|
|
17
|
+
*,
|
|
18
|
+
before: int | None = None,
|
|
19
|
+
limit: int = LIMIT,
|
|
20
|
+
) -> dict[str, Any]:
|
|
21
|
+
"""Newest-first events whose subject belongs to the project, with a paging cursor.
|
|
22
|
+
|
|
23
|
+
Events carry no project themselves; a subject is resolved through the observation,
|
|
24
|
+
claim, checkpoint or handoff it names. Tenant-wide events such as grants are shown
|
|
25
|
+
only to principals with a tenant-wide grant, under the subject kind `tenant`.
|
|
26
|
+
"""
|
|
27
|
+
identifier(project)
|
|
28
|
+
limit = max(1, min(limit, LIMIT))
|
|
29
|
+
tenant = who["tenant"]
|
|
30
|
+
params: list[Any] = [tenant]
|
|
31
|
+
cursor = ""
|
|
32
|
+
if before is not None:
|
|
33
|
+
cursor = " AND e.position<?"
|
|
34
|
+
params.append(before)
|
|
35
|
+
rows = store.db.execute(
|
|
36
|
+
"SELECT e.position,e.kind,e.subject,e.actor,e.at_ms,"
|
|
37
|
+
" o.project AS observation_project,"
|
|
38
|
+
" f.project AS claim_project,"
|
|
39
|
+
" c.project AS checkpoint_project,"
|
|
40
|
+
" h.project AS handoff_project"
|
|
41
|
+
" FROM events e"
|
|
42
|
+
" LEFT JOIN observations o ON o.tenant=e.tenant AND o.id=e.subject"
|
|
43
|
+
" LEFT JOIN revisions r ON r.tenant=e.tenant AND r.id=e.subject"
|
|
44
|
+
" LEFT JOIN families f ON f.tenant=r.tenant AND f.id=r.family"
|
|
45
|
+
" LEFT JOIN checkpoints c ON c.tenant=e.tenant AND c.id=e.subject"
|
|
46
|
+
" LEFT JOIN handoffs h ON h.tenant=e.tenant AND h.id=e.subject"
|
|
47
|
+
f" WHERE e.tenant=?{cursor} ORDER BY e.position DESC LIMIT ?",
|
|
48
|
+
(*params, limit * 4),
|
|
49
|
+
).fetchall()
|
|
50
|
+
items: list[dict[str, Any]] = []
|
|
51
|
+
for row in rows:
|
|
52
|
+
kinds = (
|
|
53
|
+
("observation", row["observation_project"]),
|
|
54
|
+
("claim", row["claim_project"]),
|
|
55
|
+
("checkpoint", row["checkpoint_project"]),
|
|
56
|
+
("handoff", row["handoff_project"]),
|
|
57
|
+
)
|
|
58
|
+
subject_kind = next((k for k, p in kinds if p == project), None)
|
|
59
|
+
if subject_kind is None:
|
|
60
|
+
continue
|
|
61
|
+
items.append(
|
|
62
|
+
{
|
|
63
|
+
"position": row["position"],
|
|
64
|
+
"kind": row["kind"],
|
|
65
|
+
"subject": row["subject"],
|
|
66
|
+
"subject_kind": subject_kind,
|
|
67
|
+
"actor": row["actor"],
|
|
68
|
+
"at_ms": row["at_ms"],
|
|
69
|
+
}
|
|
70
|
+
)
|
|
71
|
+
if len(items) >= limit:
|
|
72
|
+
# A full page resumes after its last ITEM, not the last row scanned:
|
|
73
|
+
# rows fetched beyond it may still match.
|
|
74
|
+
return {"project": project, "items": items, "next_before": row["position"]}
|
|
75
|
+
exhausted = len(rows) < limit * 4
|
|
76
|
+
return {
|
|
77
|
+
"project": project,
|
|
78
|
+
"items": items,
|
|
79
|
+
"next_before": None if exhausted or not rows else rows[-1]["position"],
|
|
80
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Optional pinned tokenizer using a vendored, hash-verified vocabulary. No network."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import hashlib
|
|
7
|
+
from functools import lru_cache
|
|
8
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Callable
|
|
11
|
+
|
|
12
|
+
from memoryking.domain.local import LocalError
|
|
13
|
+
|
|
14
|
+
VOCAB_HASH = "223921b76ee99bde995b7ff738513eef100fb51d18c93597a113bcffe865b2a7"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@lru_cache(maxsize=1)
|
|
18
|
+
def encoding() -> Any:
|
|
19
|
+
try:
|
|
20
|
+
if version("tiktoken") != "0.12.0":
|
|
21
|
+
raise LocalError("tokenizer_version_unsupported")
|
|
22
|
+
import tiktoken
|
|
23
|
+
except (ImportError, PackageNotFoundError) as exc:
|
|
24
|
+
raise LocalError("tokenizer_unavailable") from exc
|
|
25
|
+
raw = Path(__file__).with_name("tokenizers").joinpath("cl100k_base.tiktoken").read_bytes()
|
|
26
|
+
if hashlib.sha256(raw).hexdigest() != VOCAB_HASH:
|
|
27
|
+
raise LocalError("tokenizer_integrity_failure")
|
|
28
|
+
ranks = {base64.b64decode(t): int(n) for t, n in (line.split() for line in raw.splitlines())}
|
|
29
|
+
return tiktoken.Encoding(
|
|
30
|
+
name="cl100k_base",
|
|
31
|
+
pat_str=r"'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}++|\p{N}{1,3}+| ?[^\s\p{L}\p{N}]++[\r\n]*+|\s++$|\s*[\r\n]|\s+(?!\S)|\s",
|
|
32
|
+
mergeable_ranks=ranks,
|
|
33
|
+
special_tokens={
|
|
34
|
+
"<|endoftext|>": 100257,
|
|
35
|
+
"<|fim_prefix|>": 100258,
|
|
36
|
+
"<|fim_middle|>": 100259,
|
|
37
|
+
"<|fim_suffix|>": 100260,
|
|
38
|
+
"<|endofprompt|>": 100276,
|
|
39
|
+
},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def counter(name: str) -> tuple[Callable[[str], int], dict[str, Any]]:
|
|
44
|
+
if name == "cl100k_base":
|
|
45
|
+
enc = encoding()
|
|
46
|
+
return lambda text: len(enc.encode_ordinary(text)), {
|
|
47
|
+
"counting": "exact",
|
|
48
|
+
"tokenizer": name,
|
|
49
|
+
"revision": "tiktoken-0.12.0:" + VOCAB_HASH,
|
|
50
|
+
}
|
|
51
|
+
return lambda text: len(text.encode()), {
|
|
52
|
+
"counting": "estimate",
|
|
53
|
+
"tokenizer": name,
|
|
54
|
+
"revision": "utf8-byte-estimate-v1",
|
|
55
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 OpenAI, Shantanu Jain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|