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,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schema": "embedding-model-v1",
|
|
3
|
+
"id": "minilm-l6-v2-onnx",
|
|
4
|
+
"provider": "local-onnx",
|
|
5
|
+
"repository": "sentence-transformers/all-MiniLM-L6-v2",
|
|
6
|
+
"revision": "10dbd2f06a8baf40ad285b037edda610c1b9a57c",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"dimensions": 384,
|
|
9
|
+
"max_input_tokens": 256,
|
|
10
|
+
"pooling": "attention-mask-mean-v1",
|
|
11
|
+
"normalization": "l2-then-round-1e6-v1",
|
|
12
|
+
"files": [
|
|
13
|
+
{
|
|
14
|
+
"path": "tokenizer.json",
|
|
15
|
+
"url": "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/10dbd2f06a8baf40ad285b037edda610c1b9a57c/tokenizer.json",
|
|
16
|
+
"bytes": 466247,
|
|
17
|
+
"sha256": "be50c3628f2bf5bb5e3a7f17b1f74611b2561a3a27eeab05e5aa30f411572037"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"path": "tokenizer_config.json",
|
|
21
|
+
"url": "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/10dbd2f06a8baf40ad285b037edda610c1b9a57c/tokenizer_config.json",
|
|
22
|
+
"bytes": 350,
|
|
23
|
+
"sha256": "acb92769e8195aabd29b7b2137a9e6d6e25c476a4f15aa4355c233426c61576b"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"path": "config.json",
|
|
27
|
+
"url": "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/10dbd2f06a8baf40ad285b037edda610c1b9a57c/config.json",
|
|
28
|
+
"bytes": 612,
|
|
29
|
+
"sha256": "953f9c0d463486b10a6871cc2fd59f223b2c70184f49815e7efbcab5d8908b41"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"path": "1_Pooling/config.json",
|
|
33
|
+
"url": "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/10dbd2f06a8baf40ad285b037edda610c1b9a57c/1_Pooling/config.json",
|
|
34
|
+
"bytes": 190,
|
|
35
|
+
"sha256": "4be450dde3b0273bb9787637cfbd28fe04a7ba6ab9d36ac48e92b11e350ffc23"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"path": "README.md",
|
|
39
|
+
"url": "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/10dbd2f06a8baf40ad285b037edda610c1b9a57c/README.md",
|
|
40
|
+
"bytes": 10454,
|
|
41
|
+
"sha256": "7dfc82496ec33f906b5b0d6750c1e2397da6530c74d1ae3568c55bc2739125e7"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"path": "onnx/model.onnx",
|
|
45
|
+
"url": "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/10dbd2f06a8baf40ad285b037edda610c1b9a57c/onnx/model.onnx",
|
|
46
|
+
"bytes": 90405214,
|
|
47
|
+
"sha256": "6fd5d72fe4589f189f8ebc006442dbb529bb7ce38f8082112682524616046452"
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
"runtime": {
|
|
51
|
+
"onnxruntime": "1.23.2",
|
|
52
|
+
"numpy": "2.5.4",
|
|
53
|
+
"tokenizers": "0.23.3"
|
|
54
|
+
},
|
|
55
|
+
"activation": "experimental-off-by-default",
|
|
56
|
+
"remote_code": false
|
|
57
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schema": "reranker-model-v1",
|
|
3
|
+
"id": "ms-marco-minilm-l6-v2-onnx",
|
|
4
|
+
"provider": "local-onnx",
|
|
5
|
+
"kind": "cross-encoder",
|
|
6
|
+
"repository": "cross-encoder/ms-marco-MiniLM-L6-v2",
|
|
7
|
+
"revision": "233902d25c440f23af6f7d6e94d2946bac0bee0a",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"max_input_tokens": 256,
|
|
10
|
+
"truncation": "only-second-v1",
|
|
11
|
+
"scoring": "sigmoid-logit-round-1e6-v1",
|
|
12
|
+
"files": [
|
|
13
|
+
{
|
|
14
|
+
"path": "tokenizer.json",
|
|
15
|
+
"url": "https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2/resolve/233902d25c440f23af6f7d6e94d2946bac0bee0a/tokenizer.json",
|
|
16
|
+
"bytes": 711396,
|
|
17
|
+
"sha256": "d241a60d5e8f04cc1b2b3e9ef7a4921b27bf526d9f6050ab90f9267a1f9e5c66"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"path": "tokenizer_config.json",
|
|
21
|
+
"url": "https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2/resolve/233902d25c440f23af6f7d6e94d2946bac0bee0a/tokenizer_config.json",
|
|
22
|
+
"bytes": 1330,
|
|
23
|
+
"sha256": "a5c2e5a7b1a29a0702cd28c08a399b5ecc110c263009d17f7e3b415f25905fd8"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"path": "special_tokens_map.json",
|
|
27
|
+
"url": "https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2/resolve/233902d25c440f23af6f7d6e94d2946bac0bee0a/special_tokens_map.json",
|
|
28
|
+
"bytes": 132,
|
|
29
|
+
"sha256": "3c3507f36dff57bce437223db3b3081d1e2b52ec3e56ee55438193ecb2c94dd6"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"path": "config.json",
|
|
33
|
+
"url": "https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2/resolve/233902d25c440f23af6f7d6e94d2946bac0bee0a/config.json",
|
|
34
|
+
"bytes": 794,
|
|
35
|
+
"sha256": "380e02c93f431831be65d99a4e7e5f67c133985bf2e77d9d4eba46847190bacc"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"path": "README.md",
|
|
39
|
+
"url": "https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2/resolve/233902d25c440f23af6f7d6e94d2946bac0bee0a/README.md",
|
|
40
|
+
"bytes": 3673,
|
|
41
|
+
"sha256": "7c0ec39941d0d1d766ccde671f592971d82fe45b7eea550a80d1ea864ebc3baa"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"path": "onnx/model.onnx",
|
|
45
|
+
"url": "https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2/resolve/233902d25c440f23af6f7d6e94d2946bac0bee0a/onnx/model.onnx",
|
|
46
|
+
"bytes": 91011230,
|
|
47
|
+
"sha256": "5d3e70fd0c9ff14b9b5169a51e957b7a9c74897afd0a35ce4bd318150c1d4d4a"
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
"runtime": {
|
|
51
|
+
"onnxruntime": "1.23.2",
|
|
52
|
+
"numpy": "2.5.4",
|
|
53
|
+
"tokenizers": "0.23.3"
|
|
54
|
+
},
|
|
55
|
+
"activation": "off-unless-requested",
|
|
56
|
+
"remote_code": false
|
|
57
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
BEGIN IMMEDIATE;
|
|
2
|
+
CREATE TABLE embedding_partitions (
|
|
3
|
+
tenant TEXT NOT NULL REFERENCES tenants, project TEXT NOT NULL, fingerprint TEXT NOT NULL,
|
|
4
|
+
incarnation TEXT NOT NULL, generation INTEGER NOT NULL, dimensions INTEGER NOT NULL CHECK(dimensions BETWEEN 1 AND 4096),
|
|
5
|
+
configuration TEXT NOT NULL, state TEXT NOT NULL, scheduled INTEGER NOT NULL DEFAULT 0,
|
|
6
|
+
frontier INTEGER NOT NULL DEFAULT 0, created INTEGER NOT NULL,
|
|
7
|
+
PRIMARY KEY(tenant,project,fingerprint));
|
|
8
|
+
CREATE TABLE embedding_jobs (
|
|
9
|
+
tenant TEXT NOT NULL, project TEXT NOT NULL, fingerprint TEXT NOT NULL, generation INTEGER NOT NULL,
|
|
10
|
+
position INTEGER NOT NULL, observation TEXT, source_revision INTEGER NOT NULL, blob_hash TEXT,
|
|
11
|
+
incarnation TEXT NOT NULL, state TEXT NOT NULL, attempts INTEGER NOT NULL DEFAULT 0,
|
|
12
|
+
fence INTEGER NOT NULL DEFAULT 0, lease_until INTEGER NOT NULL DEFAULT 0,
|
|
13
|
+
available INTEGER NOT NULL DEFAULT 0, principal TEXT, purge_epoch INTEGER NOT NULL,
|
|
14
|
+
PRIMARY KEY(tenant,project,fingerprint,generation,position),
|
|
15
|
+
FOREIGN KEY(tenant,project,fingerprint) REFERENCES embedding_partitions(tenant,project,fingerprint),
|
|
16
|
+
FOREIGN KEY(tenant,position) REFERENCES events(tenant,position),
|
|
17
|
+
FOREIGN KEY(tenant,observation) REFERENCES observations(tenant,id));
|
|
18
|
+
CREATE INDEX embedding_queue ON embedding_jobs(tenant,project,fingerprint,state,position);
|
|
19
|
+
CREATE TABLE embedding_items (
|
|
20
|
+
tenant TEXT NOT NULL, project TEXT NOT NULL, fingerprint TEXT NOT NULL, generation INTEGER NOT NULL,
|
|
21
|
+
observation TEXT NOT NULL, incarnation TEXT NOT NULL, source_revision INTEGER NOT NULL,
|
|
22
|
+
blob_hash TEXT NOT NULL, input_hash TEXT NOT NULL, vector_json TEXT NOT NULL,
|
|
23
|
+
start_byte INTEGER NOT NULL, end_byte INTEGER NOT NULL, input_tokens INTEGER,
|
|
24
|
+
truncated INTEGER NOT NULL, position INTEGER NOT NULL,
|
|
25
|
+
PRIMARY KEY(tenant,project,fingerprint,generation,observation),
|
|
26
|
+
FOREIGN KEY(tenant,project,fingerprint) REFERENCES embedding_partitions(tenant,project,fingerprint),
|
|
27
|
+
FOREIGN KEY(tenant,observation) REFERENCES observations(tenant,id));
|
|
28
|
+
CREATE TABLE processing_usage (
|
|
29
|
+
tenant TEXT NOT NULL REFERENCES tenants, id TEXT NOT NULL, project TEXT NOT NULL,
|
|
30
|
+
principal TEXT NOT NULL, incarnation TEXT NOT NULL, provider TEXT NOT NULL,
|
|
31
|
+
fingerprint TEXT NOT NULL, stage TEXT NOT NULL, status TEXT NOT NULL,
|
|
32
|
+
input_tokens INTEGER, cached_input_tokens INTEGER, output_tokens INTEGER,
|
|
33
|
+
cost_microusd INTEGER, duration_ms INTEGER NOT NULL, request_hash TEXT NOT NULL,
|
|
34
|
+
output_hash TEXT, dependencies TEXT NOT NULL, created INTEGER NOT NULL, expires INTEGER NOT NULL,
|
|
35
|
+
PRIMARY KEY(tenant,id));
|
|
36
|
+
CREATE TABLE adapter_bases (
|
|
37
|
+
tenant TEXT NOT NULL REFERENCES tenants, id TEXT NOT NULL, project TEXT NOT NULL,
|
|
38
|
+
principal TEXT NOT NULL, incarnation TEXT NOT NULL, binding TEXT NOT NULL,
|
|
39
|
+
body BLOB NOT NULL, body_hash TEXT NOT NULL, dependencies TEXT NOT NULL,
|
|
40
|
+
created INTEGER NOT NULL, expires INTEGER NOT NULL,
|
|
41
|
+
PRIMARY KEY(tenant,id));
|
|
42
|
+
PRAGMA user_version=7;
|
|
43
|
+
COMMIT;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Read-only owner plan route shared by SQLite and PostgreSQL dashboards."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
from urllib.parse import parse_qsl, urlsplit
|
|
5
|
+
|
|
6
|
+
from memoryking.adapters.local_store import identifier
|
|
7
|
+
from memoryking.adapters.plans import Plans
|
|
8
|
+
from memoryking.adapters.working import Working
|
|
9
|
+
from memoryking.contracts.canonical import canonical_bytes
|
|
10
|
+
from memoryking.domain.local import LocalError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def read(store: Any, credential: str, target: str, at: int) -> dict[str, Any]:
|
|
14
|
+
url = urlsplit(target)
|
|
15
|
+
parts = url.path.split("/")
|
|
16
|
+
if len(parts) not in {4, 5} or parts[:3] != ["", "api", "plans"]:
|
|
17
|
+
raise LocalError("invalid_request")
|
|
18
|
+
project = identifier(parts[3])
|
|
19
|
+
pairs = parse_qsl(url.query, keep_blank_values=True)
|
|
20
|
+
query = dict(pairs)
|
|
21
|
+
if len(query) != len(pairs) or set(query) - {"cursor"} or (len(parts) == 5 and query):
|
|
22
|
+
raise LocalError("invalid_request")
|
|
23
|
+
if len(query.get("cursor", "")) > 8192:
|
|
24
|
+
raise LocalError("invalid_request")
|
|
25
|
+
who = store._auth(credential, "read", project, at)
|
|
26
|
+
plans = Plans(Working(store))
|
|
27
|
+
result = {
|
|
28
|
+
"schema_version": 1,
|
|
29
|
+
"project": project,
|
|
30
|
+
"incarnation": store.incarnation,
|
|
31
|
+
"tenant": who["tenant"],
|
|
32
|
+
"principal": who["principal"],
|
|
33
|
+
"fresh_until_ms": at + 15000,
|
|
34
|
+
}
|
|
35
|
+
if len(parts) == 5:
|
|
36
|
+
result.update(
|
|
37
|
+
kind="plan",
|
|
38
|
+
detail=plans.get(
|
|
39
|
+
who, {"project": project, "task": identifier(parts[4]), "revision": None}, at
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
else:
|
|
43
|
+
result.update(
|
|
44
|
+
kind="plans",
|
|
45
|
+
**plans.listing(
|
|
46
|
+
who, {"project": project, "cursor": query.get("cursor"), "limit": 10}, at
|
|
47
|
+
),
|
|
48
|
+
)
|
|
49
|
+
if len(canonical_bytes(result)) > 262144:
|
|
50
|
+
raise LocalError("response_limit")
|
|
51
|
+
return result
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""Structured plans reuse checkpoint durability; callers hold the canonical transaction."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import copy
|
|
6
|
+
import json
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
8
|
+
|
|
9
|
+
from memoryking.contracts.canonical import canonical_bytes
|
|
10
|
+
from memoryking.domain.local import LocalError
|
|
11
|
+
from memoryking.domain.plans import definition, progress, proof_refs, validate
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from memoryking.adapters.working import Working
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Plans:
|
|
18
|
+
def __init__(self, working: Working):
|
|
19
|
+
self.w, self.s, self.db = working, working.s, working.db
|
|
20
|
+
|
|
21
|
+
def scope(self, args: dict[str, Any]) -> dict[str, Any]:
|
|
22
|
+
return {"project": args["project"], "task": args["task"], "run": None}
|
|
23
|
+
|
|
24
|
+
def current(self, who: dict[str, Any], args: dict[str, Any], at: int) -> Any:
|
|
25
|
+
row = self.w._checkpoint(who, {**self.scope(args), "checkpoint": None}, at)
|
|
26
|
+
if "plan" not in json.loads(row["body"]):
|
|
27
|
+
raise LocalError("plan_unavailable")
|
|
28
|
+
return row
|
|
29
|
+
|
|
30
|
+
def preflight(self, who: dict[str, Any], action: str, args: dict[str, Any], at: int) -> None:
|
|
31
|
+
if action not in {"plan_save", "plan_step"}:
|
|
32
|
+
return
|
|
33
|
+
self.w._writer(who, self.scope(args), create=action == "plan_save")
|
|
34
|
+
if action == "plan_save":
|
|
35
|
+
if who["principal"] not in args["readers"]:
|
|
36
|
+
raise LocalError("checkpoint_writer_audience_required")
|
|
37
|
+
validate(args["plan"])
|
|
38
|
+
for dep in self.dependencies(args["plan"]):
|
|
39
|
+
self.w._dependency(who, dep, at)
|
|
40
|
+
else:
|
|
41
|
+
self.current(who, args, at)
|
|
42
|
+
for dep in args["evidence"]:
|
|
43
|
+
self.w._dependency(who, dep, at)
|
|
44
|
+
|
|
45
|
+
@staticmethod
|
|
46
|
+
def dependencies(plan: dict[str, Any]) -> list[dict[str, str]]:
|
|
47
|
+
deps = list(plan["lessons"])
|
|
48
|
+
for step in plan["steps"]:
|
|
49
|
+
deps.extend(step["evidence"])
|
|
50
|
+
return deps
|
|
51
|
+
|
|
52
|
+
def save(
|
|
53
|
+
self, who: dict[str, Any], action: str, args: dict[str, Any], at: int
|
|
54
|
+
) -> dict[str, Any]:
|
|
55
|
+
scope = self.scope(args)
|
|
56
|
+
if action == "plan_step":
|
|
57
|
+
row = self.current(who, args, at)
|
|
58
|
+
body = json.loads(row["body"])
|
|
59
|
+
readers = json.loads(row["readers"])
|
|
60
|
+
else:
|
|
61
|
+
body = {
|
|
62
|
+
"objective": args["plan"]["objective"],
|
|
63
|
+
"acceptance": [],
|
|
64
|
+
"hard_constraints": [],
|
|
65
|
+
"current_state": "Structured plan",
|
|
66
|
+
"repository": {
|
|
67
|
+
"repository": None,
|
|
68
|
+
"base_commit": None,
|
|
69
|
+
"current_commit": None,
|
|
70
|
+
"dirty": False,
|
|
71
|
+
"artifacts": [],
|
|
72
|
+
},
|
|
73
|
+
"findings": [],
|
|
74
|
+
"failed_attempts": [],
|
|
75
|
+
"verification": [],
|
|
76
|
+
"blockers": [],
|
|
77
|
+
"next_actions": [],
|
|
78
|
+
"references": [],
|
|
79
|
+
"operation_ids": [],
|
|
80
|
+
"capture": {"last_event": None, "observed_ms": at, "gaps": []},
|
|
81
|
+
}
|
|
82
|
+
readers = args["readers"]
|
|
83
|
+
if action == "plan_step":
|
|
84
|
+
step = next((s for s in body["plan"]["steps"] if s["id"] == args["step"]), None)
|
|
85
|
+
if step is None:
|
|
86
|
+
raise LocalError("plan_step_unavailable")
|
|
87
|
+
for key in ("status", "notes", "evidence", "decision"):
|
|
88
|
+
step[key] = copy.deepcopy(args[key])
|
|
89
|
+
else:
|
|
90
|
+
body["plan"] = copy.deepcopy(args["plan"])
|
|
91
|
+
readers = args["readers"]
|
|
92
|
+
body["objective"] = body["plan"]["objective"]
|
|
93
|
+
body["current_state"] = "Structured plan: " + body["plan"]["status"]
|
|
94
|
+
body["capture"]["observed_ms"] = at
|
|
95
|
+
self.w._body(who, body, at)
|
|
96
|
+
return self.w._save(
|
|
97
|
+
who,
|
|
98
|
+
{
|
|
99
|
+
**scope,
|
|
100
|
+
"checkpoint": body,
|
|
101
|
+
"readers": readers,
|
|
102
|
+
"expected_revision": args["expected_revision"],
|
|
103
|
+
},
|
|
104
|
+
at,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def get(self, who: dict[str, Any], args: dict[str, Any], at: int) -> dict[str, Any]:
|
|
108
|
+
current = self.current(who, args, at)
|
|
109
|
+
# current() has already checked all its evidence under this transaction.
|
|
110
|
+
# Historical rows still get their own audience/history/body checks, but
|
|
111
|
+
# shared evidence needs only one current check at this caller/time.
|
|
112
|
+
checked_dependencies = {
|
|
113
|
+
(dep["kind"], dep["id"]) for dep in json.loads(current["dependencies"])[:4096]
|
|
114
|
+
}
|
|
115
|
+
stream = self.w._stream(who, self.scope(args))
|
|
116
|
+
assert stream is not None
|
|
117
|
+
row = current
|
|
118
|
+
revision = args.get("revision")
|
|
119
|
+
if revision is not None and revision != current["revision"]:
|
|
120
|
+
candidate = self.db.execute(
|
|
121
|
+
"SELECT id FROM checkpoints WHERE tenant=? AND project=? AND task=? AND stream='task' AND incarnation=? AND revision=?",
|
|
122
|
+
(who["tenant"], args["project"], args["task"], self.s.incarnation, revision),
|
|
123
|
+
).fetchone()
|
|
124
|
+
if candidate is None:
|
|
125
|
+
raise LocalError("plan_unavailable")
|
|
126
|
+
row = self.w._checkpoint(who, {**self.scope(args), "checkpoint": candidate["id"]}, at)
|
|
127
|
+
plan = copy.deepcopy(json.loads(row["body"])["plan"])
|
|
128
|
+
history = self.db.execute(
|
|
129
|
+
"SELECT id FROM checkpoints WHERE tenant=? AND project=? AND task=? AND stream='task' AND incarnation=? AND revision<? ORDER BY revision DESC LIMIT 4097",
|
|
130
|
+
(who["tenant"], args["project"], args["task"], self.s.incarnation, row["revision"]),
|
|
131
|
+
).fetchall()
|
|
132
|
+
complete = len(history) <= 4096
|
|
133
|
+
prior_plans = []
|
|
134
|
+
for item in history[:4096]:
|
|
135
|
+
try:
|
|
136
|
+
previous = self.w._checkpoint(
|
|
137
|
+
who,
|
|
138
|
+
{**self.scope(args), "checkpoint": item["id"]},
|
|
139
|
+
at,
|
|
140
|
+
checked_dependencies=checked_dependencies,
|
|
141
|
+
)
|
|
142
|
+
prior_plans.append(json.loads(previous["body"])["plan"])
|
|
143
|
+
except (LocalError, KeyError):
|
|
144
|
+
complete = False
|
|
145
|
+
# Compare sets, not ordering: dropping/reordering/duplicating old links is
|
|
146
|
+
# not fresh proof. Compute all definitions before changing effective states.
|
|
147
|
+
definitions = {
|
|
148
|
+
s["id"]: definition(plan, s["id"]) for s in plan["steps"] if s["status"] == "done"
|
|
149
|
+
}
|
|
150
|
+
old_by_plan = [(p, {s["id"]: s for s in p["steps"]}) for p in prior_plans]
|
|
151
|
+
for step in plan["steps"]:
|
|
152
|
+
step["reported_status"] = step["status"]
|
|
153
|
+
if step["status"] == "done":
|
|
154
|
+
superseded: set[tuple[str, str]] = set()
|
|
155
|
+
for prior, old_steps in old_by_plan:
|
|
156
|
+
old = old_steps.get(step["id"])
|
|
157
|
+
if (
|
|
158
|
+
old
|
|
159
|
+
and old["status"] == "done"
|
|
160
|
+
and definition(prior, step["id"]) != definitions[step["id"]]
|
|
161
|
+
):
|
|
162
|
+
superseded.update(proof_refs(old))
|
|
163
|
+
if not complete or not (proof_refs(step) - superseded):
|
|
164
|
+
step["status"] = "unknown"
|
|
165
|
+
# Unknown prerequisite proof cannot leave dependent work counted as done.
|
|
166
|
+
changed = True
|
|
167
|
+
while changed:
|
|
168
|
+
changed = False
|
|
169
|
+
by_id = {s["id"]: s for s in plan["steps"]}
|
|
170
|
+
for step in plan["steps"]:
|
|
171
|
+
if step["status"] == "done" and any(
|
|
172
|
+
by_id[d]["status"] not in {"done", "skipped"} for d in step["depends_on"]
|
|
173
|
+
):
|
|
174
|
+
step["status"] = "unknown"
|
|
175
|
+
changed = True
|
|
176
|
+
return {
|
|
177
|
+
"id": args["task"],
|
|
178
|
+
"revision": row["revision"],
|
|
179
|
+
"current_revision": stream["revision"],
|
|
180
|
+
"checkpoint": row["id"],
|
|
181
|
+
"writer": stream["writer"],
|
|
182
|
+
"updated_ms": row["created"],
|
|
183
|
+
"plan": plan,
|
|
184
|
+
"progress": progress(plan),
|
|
185
|
+
"history_complete": complete,
|
|
186
|
+
"authority": "recorded_work_with_current_evidence_access_not_independent_certification",
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
def listing(self, who: dict[str, Any], args: dict[str, Any], at: int) -> dict[str, Any]:
|
|
190
|
+
epoch = self.db.execute(
|
|
191
|
+
"SELECT position,policy_epoch,purge_epoch FROM tenants WHERE tenant=?", (who["tenant"],)
|
|
192
|
+
).fetchone()
|
|
193
|
+
binding = {
|
|
194
|
+
"tenant": who["tenant"],
|
|
195
|
+
"principal": who["principal"],
|
|
196
|
+
"project": args["project"],
|
|
197
|
+
"frontier": epoch[0],
|
|
198
|
+
"epoch": epoch[1],
|
|
199
|
+
"purge": epoch[2],
|
|
200
|
+
"limit": args["limit"],
|
|
201
|
+
}
|
|
202
|
+
after = ""
|
|
203
|
+
if args["cursor"] is not None:
|
|
204
|
+
token = self.s._decode(args["cursor"], "plan_cursor")
|
|
205
|
+
if any(token.get(k) != v for k, v in binding.items()) or token["expires"] <= at:
|
|
206
|
+
raise LocalError("context_changed")
|
|
207
|
+
after = token["after"]
|
|
208
|
+
rows = self.db.execute(
|
|
209
|
+
"SELECT task,latest FROM checkpoint_streams WHERE tenant=? AND project=? AND stream='task' AND incarnation=? AND task>? ORDER BY task LIMIT 65",
|
|
210
|
+
(who["tenant"], args["project"], self.s.incarnation, after),
|
|
211
|
+
).fetchall()
|
|
212
|
+
result: list[dict[str, Any]] = []
|
|
213
|
+
inspected = 0
|
|
214
|
+
for row in rows[:64]:
|
|
215
|
+
before = after
|
|
216
|
+
inspected += 1
|
|
217
|
+
after = row["task"]
|
|
218
|
+
raw = self.db.execute(
|
|
219
|
+
"SELECT body,readers,state FROM checkpoints WHERE tenant=? AND id=?",
|
|
220
|
+
(who["tenant"], row["latest"]),
|
|
221
|
+
).fetchone()
|
|
222
|
+
if (
|
|
223
|
+
raw is None
|
|
224
|
+
or raw["state"] != "available"
|
|
225
|
+
or who["principal"] not in json.loads(raw["readers"])
|
|
226
|
+
):
|
|
227
|
+
continue
|
|
228
|
+
if "plan" not in json.loads(raw["body"]):
|
|
229
|
+
continue
|
|
230
|
+
item = self.get(
|
|
231
|
+
who, {"project": args["project"], "task": row["task"], "revision": None}, at
|
|
232
|
+
)
|
|
233
|
+
plan = item.pop("plan")
|
|
234
|
+
item.update(title=plan["title"], objective=plan["objective"], status=plan["status"])
|
|
235
|
+
if result and len(canonical_bytes([*result, item])) > 98304:
|
|
236
|
+
after = before
|
|
237
|
+
inspected -= 1
|
|
238
|
+
break
|
|
239
|
+
result.append(item)
|
|
240
|
+
if len(result) >= args["limit"]:
|
|
241
|
+
break
|
|
242
|
+
more = inspected < len(rows)
|
|
243
|
+
cursor = (
|
|
244
|
+
self.s._token("plan_cursor", **binding, after=after, expires=at + 300000)
|
|
245
|
+
if more
|
|
246
|
+
else None
|
|
247
|
+
)
|
|
248
|
+
return {
|
|
249
|
+
"plans": result,
|
|
250
|
+
"cursor": cursor,
|
|
251
|
+
"complete": not more,
|
|
252
|
+
"coverage": "current_authorized_plans_from_at_most_64_task_streams_per_page",
|
|
253
|
+
}
|