tortoise-graph 0.2.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.
- tortoise/__init__.py +45 -0
- tortoise/__main__.py +1865 -0
- tortoise/analyze.py +473 -0
- tortoise/api.py +303 -0
- tortoise/archived/search_legacy.py +31 -0
- tortoise/audit.py +236 -0
- tortoise/audit_events.py +284 -0
- tortoise/auth.py +107 -0
- tortoise/backup.py +155 -0
- tortoise/config.py +99 -0
- tortoise/connector_loader.py +74 -0
- tortoise/connectors/__init__.py +5 -0
- tortoise/connectors/github.py +432 -0
- tortoise/connectors/github_docs.py +146 -0
- tortoise/connectors/linear.py +199 -0
- tortoise/connectors/slack.py +222 -0
- tortoise/consistency.py +132 -0
- tortoise/crypto.py +52 -0
- tortoise/deployment.py +62 -0
- tortoise/domain_loader.py +142 -0
- tortoise/dream.py +153 -0
- tortoise/embedded_reaper.py +744 -0
- tortoise/embeddings.py +227 -0
- tortoise/ep.py +534 -0
- tortoise/exceptions.py +16 -0
- tortoise/extractor.py +945 -0
- tortoise/hosted_api.py +1982 -0
- tortoise/hosted_middleware.py +22 -0
- tortoise/idempotency.py +39 -0
- tortoise/ids.py +23 -0
- tortoise/indexer/github_indexer.py +129 -0
- tortoise/ingest.py +614 -0
- tortoise/log.py +29 -0
- tortoise/m0.py +50 -0
- tortoise/mcp_auth.py +337 -0
- tortoise/mcp_client.py +108 -0
- tortoise/mcp_server.py +1356 -0
- tortoise/memory_orchestrator.py +492 -0
- tortoise/memory_scope.py +15 -0
- tortoise/migrate_db.py +244 -0
- tortoise/migrate_kinds.py +141 -0
- tortoise/mining.py +338 -0
- tortoise/models.py +109 -0
- tortoise/monitoring.py +133 -0
- tortoise/navigation.py +216 -0
- tortoise/pack_registry.py +767 -0
- tortoise/pipeline_cli.py +197 -0
- tortoise/projection/__init__.py +913 -0
- tortoise/projection/edges.py +311 -0
- tortoise/projection/entities.py +521 -0
- tortoise/projection/grounding.py +95 -0
- tortoise/projection/propagation.py +114 -0
- tortoise/projection.py +37 -0
- tortoise/quadrature.py +90 -0
- tortoise/query_suggestions.py +194 -0
- tortoise/quota.py +163 -0
- tortoise/ranking.py +280 -0
- tortoise/render.py +108 -0
- tortoise/scripts/manifest.py +67 -0
- tortoise/sdk.py +4713 -0
- tortoise/search_engine.py +887 -0
- tortoise/security.py +268 -0
- tortoise/selfhost.py +157 -0
- tortoise/selfhost_api.py +201 -0
- tortoise/session_continuity.py +82 -0
- tortoise/session_indexer.py +569 -0
- tortoise/shared_state/__init__.py +20 -0
- tortoise/shared_state/concurrency.py +113 -0
- tortoise/shared_state/event_log.py +192 -0
- tortoise/shared_state/events.py +150 -0
- tortoise/shared_state/monitoring.py +87 -0
- tortoise/shared_state/recovery.py +126 -0
- tortoise/shared_state/tests/__init__.py +15 -0
- tortoise/shared_state/tests/test_concurrency.py +123 -0
- tortoise/shared_state/tests/test_events.py +145 -0
- tortoise/shared_state/tests/test_monitoring.py +54 -0
- tortoise/shared_state/tests/test_recovery.py +181 -0
- tortoise/source_credibility.py +306 -0
- tortoise/svbp.py +446 -0
- tortoise/taxonomy.py +71 -0
- tortoise/test_cross_ontology.py +180 -0
- tortoise/test_tortoise_client.py +206 -0
- tortoise/tool_registry.py +805 -0
- tortoise/tortoise_client.py +262 -0
- tortoise/verify_ontology.py +154 -0
- tortoise/version_vector.py +97 -0
- tortoise/weights.py +61 -0
- tortoise_graph-0.2.0.dist-info/METADATA +152 -0
- tortoise_graph-0.2.0.dist-info/RECORD +93 -0
- tortoise_graph-0.2.0.dist-info/WHEEL +5 -0
- tortoise_graph-0.2.0.dist-info/entry_points.txt +4 -0
- tortoise_graph-0.2.0.dist-info/licenses/LICENSE +95 -0
- tortoise_graph-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"""Source credibility derivation — pure functions for the discrete-tier model.
|
|
2
|
+
|
|
3
|
+
Canonical home of the validated source credibility model (issue #398, design doc
|
|
4
|
+
docs/ep-source-credibility-experiment.md):
|
|
5
|
+
|
|
6
|
+
- TIER_PRIORS: credibilityTier T0-T4 → Beta(alpha, beta) priors.
|
|
7
|
+
- pc_base(tier) := alpha - 1 (excess over neutral Beta(1,1) — "pseudo-count").
|
|
8
|
+
- aggregate_prior: log-scale multi-source aggregation (anti-Sybil):
|
|
9
|
+
pc_t = log2(N_t + 1) * decay_t * (sum_i base_pc(tier_i) * factor_i) / N_t
|
|
10
|
+
where decay_t keys on the TIER's MOST-RECENT source (T0 exempt). N=1 degenerates
|
|
11
|
+
to base_pc * decay (matches the pre-existing #122 recency modulation formula
|
|
12
|
+
alpha' = 1 + (alpha - 1) * decay — test_event_provenance exactness preserved).
|
|
13
|
+
- assessment_factor: per-source reliability modulation from agent assessments,
|
|
14
|
+
k=1.0, clamped [0.1, 2.0]. Formula: 1 + k * sum_a (rep_a - 0.5) * (score_a - 0.5).
|
|
15
|
+
- resolve_tier: precedence explicit credibilityTier > sourceKind tier-form > registry > None.
|
|
16
|
+
- SOURCE_KIND_DEFAULTS: T0-T4 identity + explicit None for ALL legacy kinds —
|
|
17
|
+
legacy type strings (github_issue, slack_message, linear_card, document) stay
|
|
18
|
+
NEUTRAL until explicitly registered via register_source_kind_default().
|
|
19
|
+
|
|
20
|
+
Ontology alignment: reliability is DERIVED at query time (v3.1 §2/§11 — derived,
|
|
21
|
+
not stored; evaluations are Points with EP confidence). Decay is the §10-compliant
|
|
22
|
+
light recency modulation (0.95^years default, T0 exempt, never auto-deprecates);
|
|
23
|
+
per-field / per-sourceType decay curves are explicitly deferred (issue open question
|
|
24
|
+
answered: deferred — see docs/plans/2026-08-07-source-credibility.md).
|
|
25
|
+
|
|
26
|
+
This module is pure (no graph I/O) — the SDK adapter in sdk.py owns the queries.
|
|
27
|
+
"""
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import math
|
|
31
|
+
from datetime import datetime, timezone
|
|
32
|
+
from typing import Iterable
|
|
33
|
+
|
|
34
|
+
# ── Tier model (validated — experiment doc §1.1, test_ep_sources.py TIER_MAP) ──
|
|
35
|
+
TIER_PRIORS: dict[str, tuple[float, float]] = {
|
|
36
|
+
"T0": (10.0, 1.0), # Gold / meta-analysis mean 0.9091, pc 9.0
|
|
37
|
+
"T1": (5.0, 1.0), # High / peer-reviewed mean 0.8333, pc 4.0
|
|
38
|
+
"T2": (3.0, 1.0), # Medium / expert mean 0.7500, pc 2.0
|
|
39
|
+
"T3": (2.0, 1.0), # Low / anecdotal mean 0.6667, pc 1.0
|
|
40
|
+
"T4": (1.1, 1.0), # Unverified mean 0.5238, pc 0.1
|
|
41
|
+
}
|
|
42
|
+
TIER_ORDER: dict[str, int] = {t: i for i, t in enumerate(["T0", "T1", "T2", "T3", "T4"])}
|
|
43
|
+
_TIER_FORM = frozenset(TIER_PRIORS)
|
|
44
|
+
|
|
45
|
+
# Legacy credibility aliases → canonical tier (create_point's credibility kwarg
|
|
46
|
+
# vocabulary; kept for create_source backward compat, #398).
|
|
47
|
+
TIER_ALIASES: dict = {
|
|
48
|
+
"gold": "T0", "T0": "T0", 0: "T0",
|
|
49
|
+
"high": "T1", "T1": "T1", 1: "T1",
|
|
50
|
+
"medium": "T2", "T2": "T2", 2: "T2",
|
|
51
|
+
"low": "T3", "T3": "T3", 3: "T3",
|
|
52
|
+
"unverified": "T4", "T4": "T4", 4: "T4",
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def canonical_tier(value) -> str | None:
|
|
57
|
+
"""Map a tier value (T0-T4 or legacy alias gold/high/.../numeric) to canonical T0-T4."""
|
|
58
|
+
return TIER_ALIASES.get(value)
|
|
59
|
+
|
|
60
|
+
# ── Source-kind registry (extensible vocabulary, issue O/I/T 4) ─────────────
|
|
61
|
+
# T0-T4 identity + explicit None for ALL legacy/generic kinds. Legacy type
|
|
62
|
+
# strings stay NEUTRAL (no inheritance) until registered — never auto-map a type
|
|
63
|
+
# to a tier (the forbidden static sourceType→reliability map, graph c=0.447).
|
|
64
|
+
SOURCE_KIND_DEFAULTS: dict[str, str | None] = {
|
|
65
|
+
**{tier: tier for tier in _TIER_FORM},
|
|
66
|
+
"document": None,
|
|
67
|
+
"github_issue": None,
|
|
68
|
+
"slack_message": None,
|
|
69
|
+
"linear_card": None,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
# Assessment-factor constants (pinned in scoping resolution C / plan Task 5)
|
|
73
|
+
ASSESSMENT_K: float = 1.0
|
|
74
|
+
FACTOR_MIN: float = 0.1 # floor preserves >=10% evidence — never inverts the prior
|
|
75
|
+
FACTOR_MAX: float = 2.0 # ceiling preserves tier ordering + anti-Sybil
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def pc_base(tier: str) -> float:
|
|
79
|
+
"""Excess over neutral Beta(1,1): pc := alpha - 1 (pseudo-count)."""
|
|
80
|
+
alpha, _beta = TIER_PRIORS[tier]
|
|
81
|
+
return alpha - 1.0
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def mean_from_beta(alpha: float, beta: float) -> float:
|
|
85
|
+
"""Mean of Beta(alpha, beta)."""
|
|
86
|
+
return alpha / (alpha + beta)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def register_source_kind_default(kind: str, tier: str | None) -> None:
|
|
90
|
+
"""Register (or override) the default tier hint for a source kind.
|
|
91
|
+
|
|
92
|
+
The extension point for the extensible sourceType vocabulary. ``None``
|
|
93
|
+
registers a kind as explicitly neutral (no inheritance). Invalid tier values
|
|
94
|
+
raise ValueError so mis-registration fails fast at load/import time.
|
|
95
|
+
"""
|
|
96
|
+
if tier is not None and tier not in _TIER_FORM:
|
|
97
|
+
raise ValueError(
|
|
98
|
+
f"Invalid tier {tier!r} — must be one of {sorted(_TIER_FORM)} or None"
|
|
99
|
+
)
|
|
100
|
+
SOURCE_KIND_DEFAULTS[kind] = tier
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def resolve_source_tier(source_kind: str | None) -> str | None:
|
|
104
|
+
"""Registry lookup: default tier hint for a source kind (None = neutral)."""
|
|
105
|
+
if not source_kind:
|
|
106
|
+
return None
|
|
107
|
+
return SOURCE_KIND_DEFAULTS.get(source_kind)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def resolve_tier(
|
|
111
|
+
credibility_tier: str | None,
|
|
112
|
+
source_kind: str | None = None,
|
|
113
|
+
registry_defaults: dict[str, str | None] | None = None,
|
|
114
|
+
) -> str | None:
|
|
115
|
+
"""Resolve a source's effective tier.
|
|
116
|
+
|
|
117
|
+
Precedence: explicit ``credibilityTier`` (must be a valid T0-T4 form) >
|
|
118
|
+
``sourceKind`` tier-form (T0-T4) > registry default > None (neutral).
|
|
119
|
+
Malformed values ("T9", "t1", "T1 ") resolve to None — never crash.
|
|
120
|
+
"""
|
|
121
|
+
for candidate in (credibility_tier, source_kind):
|
|
122
|
+
if candidate in _TIER_FORM:
|
|
123
|
+
return candidate
|
|
124
|
+
if source_kind:
|
|
125
|
+
table = registry_defaults if registry_defaults is not None else SOURCE_KIND_DEFAULTS
|
|
126
|
+
hint = table.get(source_kind)
|
|
127
|
+
if hint in _TIER_FORM:
|
|
128
|
+
return hint
|
|
129
|
+
return None
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _parse_timestamp(value: str | None) -> datetime | None:
|
|
133
|
+
"""Parse an ISO8601 timestamp. None/malformed → None (no decay)."""
|
|
134
|
+
if not value:
|
|
135
|
+
return None
|
|
136
|
+
try:
|
|
137
|
+
dt = datetime.fromisoformat(str(value).replace("Z", "+00:00"))
|
|
138
|
+
if dt.tzinfo is None:
|
|
139
|
+
dt = dt.replace(tzinfo=timezone.utc) # naive → UTC (#153)
|
|
140
|
+
return dt
|
|
141
|
+
except (ValueError, TypeError):
|
|
142
|
+
return None
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def decay_factor(
|
|
146
|
+
source_date: str | None,
|
|
147
|
+
ingested_at: str | None,
|
|
148
|
+
now: datetime | None = None,
|
|
149
|
+
recency_decay: float = 0.95,
|
|
150
|
+
tier: str | None = None,
|
|
151
|
+
) -> float:
|
|
152
|
+
"""Recency decay factor in [0, 1] — 1.0 = no decay.
|
|
153
|
+
|
|
154
|
+
Keys on ``sourceDate`` (evidence age) else ``ingestedAt`` (arrival — documented
|
|
155
|
+
proxy). T0 sources are exempt (factor 1.0, per ontology §10 "T0 ages
|
|
156
|
+
differently than T4" + #122). Future dates clamp to 1.0; pre-epoch dates clamp
|
|
157
|
+
to 1.0 (max(0, years)); malformed/missing dates → 1.0 (no decay — safe).
|
|
158
|
+
"""
|
|
159
|
+
if tier == "T0":
|
|
160
|
+
return 1.0
|
|
161
|
+
ts = _parse_timestamp(source_date) or _parse_timestamp(ingested_at)
|
|
162
|
+
if ts is None:
|
|
163
|
+
return 1.0
|
|
164
|
+
now_ts = (now or datetime.now(timezone.utc)).timestamp()
|
|
165
|
+
years = max(0.0, (now_ts - ts.timestamp()) / (365.25 * 86400.0))
|
|
166
|
+
return float(recency_decay ** years)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def aggregate_prior(
|
|
170
|
+
groups: Iterable[tuple[str, float, float, int]],
|
|
171
|
+
*,
|
|
172
|
+
recency_decay: float = 0.95,
|
|
173
|
+
now: datetime | None = None,
|
|
174
|
+
) -> tuple[float, float]:
|
|
175
|
+
"""Aggregate source evidence into a Beta prior (alpha, beta).
|
|
176
|
+
|
|
177
|
+
``groups``: iterable of (tier, source_date, ingested_at, count_effective) —
|
|
178
|
+
one entry per DISTINCT SOURCE, where count_effective is the edge weight
|
|
179
|
+
(default 1.0; a per-source assessment factor can be folded in by the caller
|
|
180
|
+
as an extra multiplier — see ``assessment_factor``).
|
|
181
|
+
|
|
182
|
+
Pinned formula (scoping resolution B, plan Task 1):
|
|
183
|
+
pc_t = log2(N_t + 1) * decay_t * (sum_{i in t} base_pc(tier_i) * factor_i) / N_t
|
|
184
|
+
where ``decay_t`` keys on the TIER's MOST-RECENT source (T0 exempt), and
|
|
185
|
+
``factor_i`` is the caller-provided per-source weight (assessment factor).
|
|
186
|
+
|
|
187
|
+
Properties:
|
|
188
|
+
- N=1 degenerates to base_pc * decay (matches #122 formula → recency tests green)
|
|
189
|
+
- monotone in source addition when added sources are not materially
|
|
190
|
+
lower-weighted than the tier mean (doc S3.1/S6 = uniform-weight addition)
|
|
191
|
+
- anti-Sybil: 1000 x T4 pc ~= 0.997 < 1 x T3 pc = 1.0 (quality beats quantity)
|
|
192
|
+
"""
|
|
193
|
+
# Group by tier, tracking most-recent date for decay_t
|
|
194
|
+
from collections import defaultdict
|
|
195
|
+
tier_data: dict[str, list[tuple[float, float]]] = defaultdict(list) # tier -> [(factor, pc)]
|
|
196
|
+
tier_dates: dict[str, list[datetime]] = defaultdict(list)
|
|
197
|
+
for tier, source_date, ingested_at, factor in groups:
|
|
198
|
+
if tier not in _TIER_FORM:
|
|
199
|
+
continue
|
|
200
|
+
tier_data[tier].append((float(factor), pc_base(tier)))
|
|
201
|
+
ts = _parse_timestamp(source_date) or _parse_timestamp(ingested_at)
|
|
202
|
+
if ts is not None:
|
|
203
|
+
tier_dates[tier].append(ts)
|
|
204
|
+
|
|
205
|
+
total_pc = 0.0
|
|
206
|
+
for tier, entries in tier_data.items():
|
|
207
|
+
n = len(entries)
|
|
208
|
+
# decay_t from the tier's MOST-RECENT source date (anti-sybil preserving;
|
|
209
|
+
# adding ancient sources cannot lower a tier's decay)
|
|
210
|
+
most_recent = max(tier_dates[tier]) if tier_dates[tier] else None
|
|
211
|
+
if tier != "T0" and most_recent is not None:
|
|
212
|
+
now_ts = (now or datetime.now(timezone.utc)).timestamp()
|
|
213
|
+
years = max(0.0, (now_ts - most_recent.timestamp()) / (365.25 * 86400.0))
|
|
214
|
+
decay_t = float(recency_decay ** years)
|
|
215
|
+
else:
|
|
216
|
+
decay_t = 1.0
|
|
217
|
+
mean_weight = sum(w for w, _pc in entries) / n
|
|
218
|
+
total_pc += math.log2(n + 1.0) * decay_t * mean_weight * pc_base(tier)
|
|
219
|
+
|
|
220
|
+
return (1.0 + total_pc, 1.0)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def assessment_factor(
|
|
224
|
+
assessments: Iterable[tuple[float, float]],
|
|
225
|
+
*,
|
|
226
|
+
k: float = ASSESSMENT_K,
|
|
227
|
+
) -> float:
|
|
228
|
+
"""Aggregate reputation-weighted assessments into a pc multiplier.
|
|
229
|
+
|
|
230
|
+
``assessments``: iterable of (assessor_reputation, score) — reputation is
|
|
231
|
+
snapshotted at assessment write time (never live-read at aggregation).
|
|
232
|
+
|
|
233
|
+
Formula: 1 + k * sum_a (rep_a - 0.5) * (score_a - 0.5), clamped [0.1, 2.0].
|
|
234
|
+
- rep = 0.5 (zero track record) or score = 0.5 contributes 0 (neutral)
|
|
235
|
+
- k = 1.0: single assessor swing is +-0.25; clamps need ~4 assessors
|
|
236
|
+
- NaN / +/-inf inputs → 1.0 (defense-in-depth)
|
|
237
|
+
"""
|
|
238
|
+
total = 0.0
|
|
239
|
+
for rep, score in assessments:
|
|
240
|
+
try:
|
|
241
|
+
term = (float(rep) - 0.5) * (float(score) - 0.5)
|
|
242
|
+
except (TypeError, ValueError):
|
|
243
|
+
continue
|
|
244
|
+
if math.isnan(term) or math.isinf(term):
|
|
245
|
+
continue
|
|
246
|
+
total += term
|
|
247
|
+
factor = 1.0 + k * total
|
|
248
|
+
return max(FACTOR_MIN, min(FACTOR_MAX, factor))
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def derive_reliability(
|
|
252
|
+
*,
|
|
253
|
+
tier: str | None,
|
|
254
|
+
source_date: str | None,
|
|
255
|
+
ingested_at: str | None,
|
|
256
|
+
recency_decay: float = 0.95,
|
|
257
|
+
now: datetime | None = None,
|
|
258
|
+
assessments: Iterable[tuple[float, float]] | None = None,
|
|
259
|
+
) -> tuple[float | None, dict]:
|
|
260
|
+
"""Derive a source's reliability (0-1) + component breakdown.
|
|
261
|
+
|
|
262
|
+
Tiered: mean of the modulated prior — reliability = mean_from_beta(1 + pc_eff, 1)
|
|
263
|
+
where pc_eff = pc_base(tier) * decay * factor. This is the SAME number the EP
|
|
264
|
+
inheritance adapter uses as the source's base weight (consistency invariant).
|
|
265
|
+
|
|
266
|
+
Untiered + no assessments: None (reason 'untiered').
|
|
267
|
+
Untiered + assessments: reputation-weighted mean of scores (display-only —
|
|
268
|
+
untiered sources never feed EP).
|
|
269
|
+
|
|
270
|
+
Returns (reliability, components_dict). Never blends means in probability
|
|
271
|
+
space for the EP-facing value.
|
|
272
|
+
"""
|
|
273
|
+
components: dict = {
|
|
274
|
+
"tier": tier,
|
|
275
|
+
"decay": 1.0,
|
|
276
|
+
"factor": 1.0,
|
|
277
|
+
"pc_eff": 0.0,
|
|
278
|
+
"assessment_count": 0,
|
|
279
|
+
"assessment_weighted_mean": None,
|
|
280
|
+
"reason": None,
|
|
281
|
+
}
|
|
282
|
+
assessments = list(assessments or [])
|
|
283
|
+
if tier in _TIER_FORM:
|
|
284
|
+
decay = decay_factor(source_date, ingested_at, now, recency_decay, tier=tier)
|
|
285
|
+
factor = assessment_factor(assessments)
|
|
286
|
+
pc = pc_base(tier) * decay * factor
|
|
287
|
+
alpha, beta = 1.0 + pc, 1.0
|
|
288
|
+
components.update({
|
|
289
|
+
"decay": decay,
|
|
290
|
+
"factor": factor,
|
|
291
|
+
"pc_eff": pc,
|
|
292
|
+
"assessment_count": len(assessments),
|
|
293
|
+
})
|
|
294
|
+
return mean_from_beta(alpha, beta), components
|
|
295
|
+
# Untiered
|
|
296
|
+
if assessments:
|
|
297
|
+
rep_sum = sum(rep for rep, _score in assessments)
|
|
298
|
+
weighted = sum(rep * score for rep, score in assessments) / rep_sum if rep_sum else 0.0
|
|
299
|
+
components.update({
|
|
300
|
+
"assessment_count": len(assessments),
|
|
301
|
+
"assessment_weighted_mean": weighted,
|
|
302
|
+
"reason": "untiered; assessment-only",
|
|
303
|
+
})
|
|
304
|
+
return max(0.0, min(1.0, weighted)), components
|
|
305
|
+
components["reason"] = "untiered"
|
|
306
|
+
return None, components
|