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,315 @@
|
|
|
1
|
+
"""Frozen, reviewed ranking experiments with real shadow retrieval and explicit rollback."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from memoryking.adapters.experience_records import ExperienceRecords
|
|
9
|
+
from memoryking.adapters.local_store import uid
|
|
10
|
+
from memoryking.application.store_core import StoreCore
|
|
11
|
+
from memoryking.contracts.canonical import canonical_hash
|
|
12
|
+
from memoryking.domain.learning import POLICY, freeze, measure
|
|
13
|
+
from memoryking.domain.local import Decision, LocalError
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Ranking:
|
|
17
|
+
def __init__(self, store: StoreCore):
|
|
18
|
+
self.s, self.db, self.records = store, store.db, ExperienceRecords(store)
|
|
19
|
+
|
|
20
|
+
def spec(self, who: dict[str, Any], project: str, reference: str, at: int) -> dict[str, Any]:
|
|
21
|
+
row = self.records.get(who, project, reference, "ranking", at)
|
|
22
|
+
body = json.loads(row["body"])
|
|
23
|
+
if body["policy"] != POLICY:
|
|
24
|
+
raise LocalError("ranking_policy_unsupported")
|
|
25
|
+
return {
|
|
26
|
+
"state": "experimental",
|
|
27
|
+
"configuration": row["id"],
|
|
28
|
+
"body_hash": row["body_hash"],
|
|
29
|
+
"adjustments": body["adjustments"],
|
|
30
|
+
"policy": POLICY,
|
|
31
|
+
"quality": "development_only",
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
def active(self, who: dict[str, Any], project: str, at: int) -> dict[str, Any]:
|
|
35
|
+
state = self.db.execute(
|
|
36
|
+
"SELECT * FROM ranking_state WHERE tenant=? AND project=?", (who["tenant"], project)
|
|
37
|
+
).fetchone()
|
|
38
|
+
if state is None or state["configuration"] is None:
|
|
39
|
+
return {
|
|
40
|
+
"state": "reconciliation_required"
|
|
41
|
+
if state is not None and state["reconciliation"]
|
|
42
|
+
else "disabled",
|
|
43
|
+
"adjustments": {},
|
|
44
|
+
}
|
|
45
|
+
try:
|
|
46
|
+
return self.spec(who, project, state["configuration"], at)
|
|
47
|
+
except LocalError:
|
|
48
|
+
# No hidden configuration, feedback IDs, scores or counts are disclosed.
|
|
49
|
+
return {"state": "unavailable", "adjustments": {}}
|
|
50
|
+
|
|
51
|
+
def propose(self, who: dict[str, Any], args: dict[str, Any], at: int) -> dict[str, Any]:
|
|
52
|
+
votes, deps = [], []
|
|
53
|
+
for reference in args["feedback"]:
|
|
54
|
+
row = self.records.get(who, args["project"], reference, "feedback", at)
|
|
55
|
+
latest = self.db.execute(
|
|
56
|
+
"SELECT max(revision) FROM experience_records WHERE tenant=? AND project=? AND principal=? AND incarnation=? AND kind='feedback' AND logical_key=?",
|
|
57
|
+
(
|
|
58
|
+
who["tenant"],
|
|
59
|
+
args["project"],
|
|
60
|
+
row["principal"],
|
|
61
|
+
self.s.incarnation,
|
|
62
|
+
row["logical_key"],
|
|
63
|
+
),
|
|
64
|
+
).fetchone()[0]
|
|
65
|
+
if latest != row["revision"]:
|
|
66
|
+
raise LocalError("ranking_feedback_superseded")
|
|
67
|
+
votes.append({**json.loads(row["body"]), "actor": row["principal"]})
|
|
68
|
+
deps.append({"kind": "experience_record", "id": reference})
|
|
69
|
+
body = {
|
|
70
|
+
"policy": POLICY,
|
|
71
|
+
"feedback": args["feedback"],
|
|
72
|
+
"adjustments": freeze(votes, args["max_adjustment_ppm"]),
|
|
73
|
+
"max_adjustment_ppm": args["max_adjustment_ppm"],
|
|
74
|
+
"automatic_activation": False,
|
|
75
|
+
"causal_claim": False,
|
|
76
|
+
"quality": "unqualified_development",
|
|
77
|
+
}
|
|
78
|
+
return self.records.save(
|
|
79
|
+
who, args["project"], "ranking", args["logical_key"], 0, body, deps, None, at
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
def evaluate(
|
|
83
|
+
self, who: dict[str, Any], args: dict[str, Any], decision: Decision
|
|
84
|
+
) -> dict[str, Any]:
|
|
85
|
+
from memoryking.adapters.retrieval import Retrieval
|
|
86
|
+
|
|
87
|
+
at = decision.evaluated_ms
|
|
88
|
+
spec = self.spec(who, args["project"], args["configuration"], at)
|
|
89
|
+
r = Retrieval(self.s)
|
|
90
|
+
deps = [{"kind": "experience_record", "id": args["configuration"]}]
|
|
91
|
+
cases = []
|
|
92
|
+
for case in args["cases"]:
|
|
93
|
+
if case["brief"]["project"] != args["project"] or set(case["relevant"]) & set(
|
|
94
|
+
case["forbidden"]
|
|
95
|
+
):
|
|
96
|
+
raise LocalError("ranking_case_invalid")
|
|
97
|
+
for reference in [*case["relevant"], *case["forbidden"]]:
|
|
98
|
+
obs = self.db.execute(
|
|
99
|
+
"SELECT 1 FROM observations WHERE tenant=? AND project=? AND id=?",
|
|
100
|
+
(who["tenant"], args["project"], reference),
|
|
101
|
+
).fetchone()
|
|
102
|
+
if obs is None:
|
|
103
|
+
r.k._revision(who["tenant"], reference, args["project"])
|
|
104
|
+
dep = {"kind": "observation" if obs else "revision", "id": reference}
|
|
105
|
+
r._dependency(who, dep, at)
|
|
106
|
+
deps.append(dep)
|
|
107
|
+
arms = {}
|
|
108
|
+
for name, override in [
|
|
109
|
+
("baseline", {"state": "disabled", "adjustments": {}}),
|
|
110
|
+
("candidate", spec),
|
|
111
|
+
]:
|
|
112
|
+
raw, diag, dependencies = r._brief(
|
|
113
|
+
who, case["brief"], decision, uid("shadow"), ranking_override=override
|
|
114
|
+
)
|
|
115
|
+
packet = json.loads(raw)
|
|
116
|
+
selected = [item["reference"] for item in packet["guidance"]]
|
|
117
|
+
arms[name] = {
|
|
118
|
+
"selected": selected,
|
|
119
|
+
"status": packet["status"],
|
|
120
|
+
"constraints_hash": canonical_hash(packet["constraints"]),
|
|
121
|
+
"output_hash": canonical_hash(packet),
|
|
122
|
+
"metrics": measure(selected, case["relevant"]),
|
|
123
|
+
"forbidden_selected": sorted(set(selected) & set(case["forbidden"])),
|
|
124
|
+
"tokens": diag["usage"]["output_tokens"],
|
|
125
|
+
}
|
|
126
|
+
deps.extend(dependencies)
|
|
127
|
+
baseline, candidate = arms["baseline"], arms["candidate"]
|
|
128
|
+
ok = (
|
|
129
|
+
baseline["constraints_hash"] == candidate["constraints_hash"]
|
|
130
|
+
and candidate["status"] not in {"blocked", "budget_insufficient"}
|
|
131
|
+
and baseline["status"] not in {"blocked", "budget_insufficient"}
|
|
132
|
+
and not candidate["forbidden_selected"]
|
|
133
|
+
and all(
|
|
134
|
+
candidate["metrics"][metric] >= baseline["metrics"][metric]
|
|
135
|
+
for metric in ("recall_ppm", "precision_ppm")
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
cases.append(
|
|
139
|
+
{
|
|
140
|
+
"request": case["brief"],
|
|
141
|
+
"judgments": {"relevant": case["relevant"], "forbidden": case["forbidden"]},
|
|
142
|
+
"arms": arms,
|
|
143
|
+
"passes": ok,
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
unique = {(dep["kind"], dep["id"]) for dep in deps}
|
|
147
|
+
improved = any(
|
|
148
|
+
case["arms"]["candidate"]["metrics"][metric]
|
|
149
|
+
> case["arms"]["baseline"]["metrics"][metric]
|
|
150
|
+
for case in cases
|
|
151
|
+
for metric in ("recall_ppm", "precision_ppm")
|
|
152
|
+
)
|
|
153
|
+
body = {
|
|
154
|
+
"configuration": args["configuration"],
|
|
155
|
+
"configuration_hash": spec["body_hash"],
|
|
156
|
+
"policy": POLICY,
|
|
157
|
+
"dataset_kind": "development",
|
|
158
|
+
"judgment_provenance": args["judgment_provenance"],
|
|
159
|
+
"judgment_assurance": "actor_reported",
|
|
160
|
+
"source_position": self._project_position(who["tenant"], args["project"]),
|
|
161
|
+
"status": "passes_development_gates"
|
|
162
|
+
if improved and all(case["passes"] for case in cases)
|
|
163
|
+
else "fails_development_gates",
|
|
164
|
+
"measured_incremental_value": improved,
|
|
165
|
+
"cases": cases,
|
|
166
|
+
"task_quality_qualified": False,
|
|
167
|
+
"provider_calls": 0,
|
|
168
|
+
}
|
|
169
|
+
return self.records.save(
|
|
170
|
+
who,
|
|
171
|
+
args["project"],
|
|
172
|
+
"shadow",
|
|
173
|
+
args["logical_key"],
|
|
174
|
+
0,
|
|
175
|
+
body,
|
|
176
|
+
[{"kind": k, "id": v} for k, v in sorted(unique)],
|
|
177
|
+
None,
|
|
178
|
+
at,
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
def _project_position(self, tenant: str, project: str) -> int:
|
|
182
|
+
"""Latest event that can change this project's ranking inputs.
|
|
183
|
+
|
|
184
|
+
Captures, claims, reviews and feedback elsewhere in the tenant do not invalidate a
|
|
185
|
+
shadow evaluation; the shadow record itself is excluded so a fresh evaluation stays
|
|
186
|
+
activatable until the project changes.
|
|
187
|
+
"""
|
|
188
|
+
statements = (
|
|
189
|
+
"SELECT coalesce(max(position),0) FROM observations WHERE tenant=? AND project=?",
|
|
190
|
+
"SELECT coalesce(max(position),0) FROM experience_records WHERE tenant=? AND project=? AND kind!='shadow'",
|
|
191
|
+
"SELECT coalesce(max(r.created),0) FROM revisions r JOIN families f ON r.tenant=f.tenant AND r.family=f.id WHERE r.tenant=? AND f.project=?",
|
|
192
|
+
"SELECT coalesce(max(l.position),0) FROM lifecycle l JOIN revisions r ON l.tenant=r.tenant AND l.revision=r.id JOIN families f ON r.tenant=f.tenant AND r.family=f.id WHERE l.tenant=? AND f.project=?",
|
|
193
|
+
)
|
|
194
|
+
return max(
|
|
195
|
+
int(self.db.execute(statement, (tenant, project)).fetchone()[0])
|
|
196
|
+
for statement in statements
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
def transition(
|
|
200
|
+
self, who: dict[str, Any], args: dict[str, Any], action: str, at: int
|
|
201
|
+
) -> dict[str, Any]:
|
|
202
|
+
state = self.db.execute(
|
|
203
|
+
"SELECT * FROM ranking_state WHERE tenant=? AND project=?",
|
|
204
|
+
(who["tenant"], args["project"]),
|
|
205
|
+
).fetchone()
|
|
206
|
+
version = state["version"] if state is not None else 0
|
|
207
|
+
if version != args["expected_version"]:
|
|
208
|
+
raise LocalError("ranking_version_conflict")
|
|
209
|
+
reference = args["configuration"]
|
|
210
|
+
spec = self.spec(who, args["project"], reference, at) if reference is not None else None
|
|
211
|
+
if action == "activate":
|
|
212
|
+
shadow = self.records.get(who, args["project"], args["shadow"], "shadow", at)
|
|
213
|
+
body = json.loads(shadow["body"])
|
|
214
|
+
position = self._project_position(who["tenant"], args["project"])
|
|
215
|
+
if (
|
|
216
|
+
body["configuration"] != reference
|
|
217
|
+
or spec is None
|
|
218
|
+
or body["configuration_hash"] != spec["body_hash"]
|
|
219
|
+
or body["status"] != "passes_development_gates"
|
|
220
|
+
):
|
|
221
|
+
raise LocalError("ranking_evaluation_required")
|
|
222
|
+
if position != body["source_position"]:
|
|
223
|
+
raise LocalError("ranking_shadow_stale")
|
|
224
|
+
elif (
|
|
225
|
+
reference is not None
|
|
226
|
+
and self.db.execute(
|
|
227
|
+
"SELECT 1 FROM ranking_history WHERE tenant=? AND project=? AND configuration=?",
|
|
228
|
+
(who["tenant"], args["project"], reference),
|
|
229
|
+
).fetchone()
|
|
230
|
+
is None
|
|
231
|
+
):
|
|
232
|
+
raise LocalError("ranking_not_previously_active")
|
|
233
|
+
record = {
|
|
234
|
+
"kind": "ranking",
|
|
235
|
+
"tenant": who["tenant"],
|
|
236
|
+
"project": args["project"],
|
|
237
|
+
"actor": who["principal"],
|
|
238
|
+
"configuration": reference,
|
|
239
|
+
"expected_hash": spec["body_hash"] if spec else None,
|
|
240
|
+
"version": version + 1,
|
|
241
|
+
"decision": action,
|
|
242
|
+
"reason": args["reason"],
|
|
243
|
+
"shadow": args.get("shadow"),
|
|
244
|
+
"incarnation": self.s.incarnation,
|
|
245
|
+
}
|
|
246
|
+
self.s._apply(self.s._journal(record), at)
|
|
247
|
+
return {
|
|
248
|
+
"state": "experimental" if reference else "disabled",
|
|
249
|
+
"configuration": reference,
|
|
250
|
+
"version": version + 1,
|
|
251
|
+
"quality": "development_only",
|
|
252
|
+
"automatic_online_learning": False,
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
def apply_policy(self, action: dict[str, Any], at: int) -> None:
|
|
256
|
+
reference = action["configuration"]
|
|
257
|
+
row = (
|
|
258
|
+
self.db.execute(
|
|
259
|
+
"SELECT body_hash FROM experience_records WHERE tenant=? AND id=? AND kind='ranking'",
|
|
260
|
+
(action["tenant"], reference),
|
|
261
|
+
).fetchone()
|
|
262
|
+
if reference
|
|
263
|
+
else None
|
|
264
|
+
)
|
|
265
|
+
missing = reference is not None and (
|
|
266
|
+
row is None or row["body_hash"] != action["expected_hash"]
|
|
267
|
+
)
|
|
268
|
+
selected = None if missing else reference
|
|
269
|
+
position = self.s._event(
|
|
270
|
+
action["tenant"],
|
|
271
|
+
"ranking_" + action["decision"],
|
|
272
|
+
action["project"],
|
|
273
|
+
at,
|
|
274
|
+
action["actor"],
|
|
275
|
+
)
|
|
276
|
+
self.db.execute(
|
|
277
|
+
"INSERT INTO ranking_state VALUES (?,?,?,?,?) ON CONFLICT(tenant,project) DO UPDATE SET version=excluded.version,configuration=excluded.configuration,reconciliation=excluded.reconciliation",
|
|
278
|
+
(action["tenant"], action["project"], action["version"], selected, int(missing)),
|
|
279
|
+
)
|
|
280
|
+
self.db.execute(
|
|
281
|
+
"INSERT INTO ranking_history VALUES (?,?,?,?,?)",
|
|
282
|
+
(action["tenant"], action["project"], position, selected, json.dumps(action)),
|
|
283
|
+
)
|
|
284
|
+
self.s._enqueue(action["tenant"], position, None, action["actor"])
|
|
285
|
+
|
|
286
|
+
def inspect(self, who: dict[str, Any], args: dict[str, Any], at: int) -> dict[str, Any]:
|
|
287
|
+
state = self.db.execute(
|
|
288
|
+
"SELECT * FROM ranking_state WHERE tenant=? AND project=?",
|
|
289
|
+
(who["tenant"], args["project"]),
|
|
290
|
+
).fetchone()
|
|
291
|
+
history = []
|
|
292
|
+
for row in self.db.execute(
|
|
293
|
+
"SELECT * FROM ranking_history WHERE tenant=? AND project=? ORDER BY position DESC LIMIT 32",
|
|
294
|
+
(who["tenant"], args["project"]),
|
|
295
|
+
).fetchall():
|
|
296
|
+
if row["configuration"]:
|
|
297
|
+
try:
|
|
298
|
+
self.spec(who, args["project"], row["configuration"], at)
|
|
299
|
+
except LocalError:
|
|
300
|
+
continue
|
|
301
|
+
history.append(
|
|
302
|
+
{
|
|
303
|
+
"position": row["position"],
|
|
304
|
+
"configuration": row["configuration"],
|
|
305
|
+
"decision": json.loads(row["detail"])["decision"],
|
|
306
|
+
}
|
|
307
|
+
)
|
|
308
|
+
active = self.active(who, args["project"], at)
|
|
309
|
+
return {
|
|
310
|
+
"active": {key: value for key, value in active.items() if key != "adjustments"},
|
|
311
|
+
"version": state["version"] if state else 0,
|
|
312
|
+
"history": history,
|
|
313
|
+
"coverage": "authorized_latest_32_transitions",
|
|
314
|
+
"automatic_online_learning": False,
|
|
315
|
+
}
|