repodna-engine 1.0.1__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.
Files changed (173) hide show
  1. repodna/__init__.py +3 -0
  2. repodna/__main__.py +10 -0
  3. repodna/advisor/__init__.py +17 -0
  4. repodna/advisor/engine.py +502 -0
  5. repodna/advisor/models.py +101 -0
  6. repodna/agent/__init__.py +87 -0
  7. repodna/agent/context.py +558 -0
  8. repodna/agent/deltas.py +198 -0
  9. repodna/agent/evidence.py +134 -0
  10. repodna/agent/expansion.py +447 -0
  11. repodna/agent/localization.py +518 -0
  12. repodna/agent/models.py +695 -0
  13. repodna/agent/patch.py +378 -0
  14. repodna/agent/postflight.py +278 -0
  15. repodna/agent/preflight.py +336 -0
  16. repodna/agent/ranking.py +318 -0
  17. repodna/agent/repo_map.py +392 -0
  18. repodna/agent/safety.py +111 -0
  19. repodna/agent/service.py +574 -0
  20. repodna/agent/sessions.py +264 -0
  21. repodna/agent/task.py +660 -0
  22. repodna/agent/telemetry.py +101 -0
  23. repodna/agent/token_budget.py +231 -0
  24. repodna/api/__init__.py +1 -0
  25. repodna/api/agent_routes.py +248 -0
  26. repodna/api/app.py +2904 -0
  27. repodna/benchmarks/__init__.py +29 -0
  28. repodna/benchmarks/baselines.py +126 -0
  29. repodna/benchmarks/budget_bench.py +162 -0
  30. repodna/benchmarks/engine.py +418 -0
  31. repodna/benchmarks/evaluator.py +310 -0
  32. repodna/benchmarks/metrics.py +206 -0
  33. repodna/benchmarks/models.py +194 -0
  34. repodna/benchmarks/scalability.py +166 -0
  35. repodna/brain/__init__.py +46 -0
  36. repodna/brain/evidence.py +276 -0
  37. repodna/brain/extractor.py +1207 -0
  38. repodna/brain/indexer.py +766 -0
  39. repodna/brain/models.py +275 -0
  40. repodna/brain/multilang_extractors.py +889 -0
  41. repodna/brain/retrieval.py +469 -0
  42. repodna/brain/tracer.py +203 -0
  43. repodna/cli/__init__.py +1 -0
  44. repodna/cli/__main__.py +10 -0
  45. repodna/cli/agent_cmd.py +228 -0
  46. repodna/cli/installer.py +590 -0
  47. repodna/cli/main.py +2173 -0
  48. repodna/cli/plugin_cmd.py +130 -0
  49. repodna/cli/workspace_cmd.py +211 -0
  50. repodna/context/__init__.py +9 -0
  51. repodna/context/builder.py +379 -0
  52. repodna/context/models.py +133 -0
  53. repodna/contract/__init__.py +39 -0
  54. repodna/contract/evaluator.py +318 -0
  55. repodna/contract/loader.py +260 -0
  56. repodna/contract/models.py +185 -0
  57. repodna/core/__init__.py +1 -0
  58. repodna/core/analyzer.py +1424 -0
  59. repodna/diff/__init__.py +35 -0
  60. repodna/diff/engine.py +332 -0
  61. repodna/diff/models.py +117 -0
  62. repodna/diff/snapshot.py +248 -0
  63. repodna/ecosystem/__init__.py +27 -0
  64. repodna/ecosystem/contracts.py +129 -0
  65. repodna/ecosystem/doctor.py +253 -0
  66. repodna/git_insights/__init__.py +1 -0
  67. repodna/git_insights/analyzer.py +273 -0
  68. repodna/graph/__init__.py +1 -0
  69. repodna/graph/engine.py +562 -0
  70. repodna/guardian/__init__.py +17 -0
  71. repodna/guardian/engine.py +369 -0
  72. repodna/guardian/models.py +104 -0
  73. repodna/history/__init__.py +40 -0
  74. repodna/history/cache.py +240 -0
  75. repodna/history/diff.py +394 -0
  76. repodna/history/engine.py +557 -0
  77. repodna/history/evidence.py +160 -0
  78. repodna/history/models.py +258 -0
  79. repodna/history/timeline.py +164 -0
  80. repodna/intelligence/__init__.py +33 -0
  81. repodna/intelligence/models.py +165 -0
  82. repodna/intelligence/service.py +570 -0
  83. repodna/live/__init__.py +94 -0
  84. repodna/live/alerts.py +443 -0
  85. repodna/live/coordinator.py +351 -0
  86. repodna/live/diff.py +444 -0
  87. repodna/live/events.py +471 -0
  88. repodna/live/filtering.py +329 -0
  89. repodna/live/history.py +637 -0
  90. repodna/live/models.py +505 -0
  91. repodna/live/observer.py +653 -0
  92. repodna/live/reconciliation.py +253 -0
  93. repodna/live/session.py +1547 -0
  94. repodna/mcp/__init__.py +5 -0
  95. repodna/mcp/__main__.py +6 -0
  96. repodna/mcp/agent_tools.py +258 -0
  97. repodna/mcp/server.py +3283 -0
  98. repodna/mcp/workspace_tools.py +200 -0
  99. repodna/parsers/__init__.py +13 -0
  100. repodna/parsers/base.py +126 -0
  101. repodna/parsers/capabilities.py +361 -0
  102. repodna/parsers/project_metadata.py +232 -0
  103. repodna/parsers/resolver.py +621 -0
  104. repodna/parsers/treesitter_parser.py +830 -0
  105. repodna/passport/__init__.py +64 -0
  106. repodna/passport/generator.py +546 -0
  107. repodna/passport/migrator.py +139 -0
  108. repodna/passport/models.py +224 -0
  109. repodna/passport/validator.py +143 -0
  110. repodna/performance/__init__.py +89 -0
  111. repodna/performance/cache.py +1584 -0
  112. repodna/performance/identity.py +341 -0
  113. repodna/performance/incremental.py +317 -0
  114. repodna/performance/models.py +157 -0
  115. repodna/performance/parallel.py +291 -0
  116. repodna/plugins/__init__.py +47 -0
  117. repodna/plugins/evaluator.py +334 -0
  118. repodna/plugins/models.py +113 -0
  119. repodna/plugins/validator.py +256 -0
  120. repodna/quality/__init__.py +91 -0
  121. repodna/quality/complexity.py +352 -0
  122. repodna/quality/config.py +159 -0
  123. repodna/quality/debt.py +73 -0
  124. repodna/quality/duplicates.py +330 -0
  125. repodna/quality/engine.py +282 -0
  126. repodna/quality/impact.py +198 -0
  127. repodna/quality/models.py +427 -0
  128. repodna/quality/rules.py +364 -0
  129. repodna/quality/sarif.py +107 -0
  130. repodna/quality/security_signals.py +278 -0
  131. repodna/quality/test_mapping.py +271 -0
  132. repodna/quality/unused.py +201 -0
  133. repodna/reports/__init__.py +1 -0
  134. repodna/reports/exporter.py +374 -0
  135. repodna/rules/__init__.py +1 -0
  136. repodna/rules/health.py +436 -0
  137. repodna/schemas/__init__.py +1 -0
  138. repodna/schemas/models.py +348 -0
  139. repodna/security/__init__.py +1 -0
  140. repodna/security/safe_yaml.py +81 -0
  141. repodna/security/validators.py +583 -0
  142. repodna/skills/__init__.py +0 -0
  143. repodna/skills/repodna/SKILL.md +59 -0
  144. repodna/skills/repodna/__init__.py +0 -0
  145. repodna/skills/repodna/examples/workflows.md +85 -0
  146. repodna/skills/repodna/references/architecture-rules.md +26 -0
  147. repodna/storage/__init__.py +1 -0
  148. repodna/storage/workspace.py +199 -0
  149. repodna/webui/__init__.py +1 -0
  150. repodna/webui/assets/DependencyGraph-km_Qo-nG.js +43 -0
  151. repodna/webui/assets/HealthReportView-WVL39aNO.js +1 -0
  152. repodna/webui/assets/index-ByZ5e5pe.js +506 -0
  153. repodna/webui/assets/index-CDj3e8hw.css +1 -0
  154. repodna/webui/index.html +21 -0
  155. repodna/workspace/__init__.py +131 -0
  156. repodna/workspace/compatibility.py +317 -0
  157. repodna/workspace/context.py +175 -0
  158. repodna/workspace/contracts.py +369 -0
  159. repodna/workspace/federation.py +411 -0
  160. repodna/workspace/identity.py +202 -0
  161. repodna/workspace/impact.py +111 -0
  162. repodna/workspace/manifest.py +266 -0
  163. repodna/workspace/models.py +461 -0
  164. repodna/workspace/packages.py +494 -0
  165. repodna/workspace/policies.py +142 -0
  166. repodna/workspace/services.py +238 -0
  167. repodna/workspace/topology.py +316 -0
  168. repodna_engine-1.0.1.dist-info/METADATA +129 -0
  169. repodna_engine-1.0.1.dist-info/RECORD +173 -0
  170. repodna_engine-1.0.1.dist-info/WHEEL +5 -0
  171. repodna_engine-1.0.1.dist-info/entry_points.txt +2 -0
  172. repodna_engine-1.0.1.dist-info/licenses/LICENSE +21 -0
  173. repodna_engine-1.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,198 @@
1
+ """Acknowledged Context Deduplication and Delta Packs for RepoDNA v0.11.0.
2
+
3
+ Key guarantees:
4
+ - Server NEVER assumes the agent still holds previously sent blocks in its context window;
5
+ deduplication relies strictly on explicit `known_block_ids` / `known_block_hashes` from the client.
6
+ - If the client lacks required blocks or the snapshot changed incompatibly without overlap,
7
+ returns an explicit `full_fallback` mode so reconstruction is always safe.
8
+ - Reconstructing from `(client_cache, delta_response)` produces a block set semantically
9
+ equivalent to the full ContextPack.
10
+ - Reports full response bytes/tokens, delta response bytes/tokens, and repeated bytes avoided
11
+ separately from per-call MCP tool overhead.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import json
17
+
18
+ from repodna.agent.models import (
19
+ ContextBlock,
20
+ ContextDeltaRequest,
21
+ ContextDeltaResponse,
22
+ ContextPack,
23
+ )
24
+ from repodna.agent.token_budget import TokenCounter, build_context_budget
25
+
26
+
27
+ def build_delta_pack(
28
+ full_pack: ContextPack,
29
+ delta_req: ContextDeltaRequest,
30
+ target_model: str = "generic",
31
+ tokenizer_pref: str = "auto",
32
+ ) -> ContextDeltaResponse:
33
+ """Computes an acknowledged delta pack from a compiled full ContextPack."""
34
+ full_blocks_json = json.dumps(
35
+ [b.model_dump(mode="json") for b in full_pack.blocks], sort_keys=True
36
+ )
37
+ full_bytes = len(full_blocks_json.encode("utf-8"))
38
+ full_tokens = TokenCounter.count(
39
+ full_blocks_json, target_model=target_model, tokenizer_pref=tokenizer_pref
40
+ ).tokens
41
+
42
+ known_ids = set(delta_req.known_block_ids) | set(delta_req.known_block_hashes.keys())
43
+
44
+ # If client acknowledges nothing, perform safe full fallback
45
+ if not known_ids:
46
+ return ContextDeltaResponse(
47
+ mode="full_fallback",
48
+ fallback_reason="Client provided no acknowledged known_block_ids; returning self-contained full pack.",
49
+ repo_id=full_pack.freshness.repo_id,
50
+ snapshot_id=full_pack.freshness.snapshot_id,
51
+ task_id=full_pack.task.task_id,
52
+ added_blocks=list(full_pack.blocks),
53
+ updated_blocks=[],
54
+ invalidated_block_ids=[],
55
+ unchanged_block_references=[],
56
+ required_dependencies=[b.block_id for b in full_pack.blocks if b.required],
57
+ evidence=list(full_pack.evidence),
58
+ full_response_bytes=full_bytes,
59
+ delta_response_bytes=full_bytes,
60
+ repeated_context_bytes_avoided=0,
61
+ full_counted_tokens=full_tokens,
62
+ delta_counted_tokens=full_tokens,
63
+ budget=full_pack.budget,
64
+ trust_boundary=full_pack.trust_boundary,
65
+ )
66
+
67
+ current_by_id = {b.block_id: b for b in full_pack.blocks}
68
+ current_by_scope = {(b.block_type, b.scope): b for b in full_pack.blocks}
69
+
70
+ added: list[ContextBlock] = []
71
+ updated: list[ContextBlock] = []
72
+ unchanged_refs: list[str] = []
73
+ repeated_bytes_avoided = 0
74
+
75
+ for blk in full_pack.blocks:
76
+ if blk.block_id in known_ids:
77
+ known_hash = delta_req.known_block_hashes.get(blk.block_id)
78
+ if known_hash is None or known_hash == blk.content_hash:
79
+ unchanged_refs.append(blk.block_id)
80
+ repeated_bytes_avoided += len(blk.model_dump_json().encode("utf-8"))
81
+ else:
82
+ updated.append(blk)
83
+ else:
84
+ # Check if an older version of the same (block_type, scope) was known
85
+ # (meaning content changed across snapshots -> send as updated)
86
+ if delta_req.previous_snapshot_id and delta_req.previous_snapshot_id != full_pack.freshness.snapshot_id:
87
+ updated.append(blk)
88
+ else:
89
+ added.append(blk)
90
+
91
+ invalidated = sorted(kid for kid in known_ids if kid not in current_by_id)
92
+ _ = current_by_scope # reserved for future scope-level diagnostics
93
+
94
+ # If zero blocks were unchanged and allow_full_fallback is enabled, emit clean full_fallback
95
+ if not unchanged_refs and delta_req.allow_full_fallback:
96
+ return ContextDeltaResponse(
97
+ mode="full_fallback",
98
+ fallback_reason="No acknowledged blocks matched current snapshot/content hashes; returning full pack.",
99
+ repo_id=full_pack.freshness.repo_id,
100
+ snapshot_id=full_pack.freshness.snapshot_id,
101
+ task_id=full_pack.task.task_id,
102
+ added_blocks=list(full_pack.blocks),
103
+ updated_blocks=[],
104
+ invalidated_block_ids=invalidated,
105
+ unchanged_block_references=[],
106
+ required_dependencies=[b.block_id for b in full_pack.blocks if b.required],
107
+ evidence=list(full_pack.evidence),
108
+ full_response_bytes=full_bytes,
109
+ delta_response_bytes=full_bytes,
110
+ repeated_context_bytes_avoided=0,
111
+ full_counted_tokens=full_tokens,
112
+ delta_counted_tokens=full_tokens,
113
+ budget=full_pack.budget,
114
+ trust_boundary=full_pack.trust_boundary,
115
+ )
116
+
117
+ # Filter evidence to only what is referenced by added/updated blocks
118
+ delta_ev_ids = {eid for b in [*added, *updated] for eid in b.evidence_ids}
119
+ delta_evidence = [e for e in full_pack.evidence if e.evidence_id in delta_ev_ids]
120
+
121
+ delta_payload_json = json.dumps(
122
+ {
123
+ "added": [b.model_dump(mode="json") for b in added],
124
+ "updated": [b.model_dump(mode="json") for b in updated],
125
+ "invalidated": invalidated,
126
+ "unchanged_refs": unchanged_refs,
127
+ },
128
+ sort_keys=True,
129
+ )
130
+ delta_bytes = len(delta_payload_json.encode("utf-8"))
131
+ delta_tokens = TokenCounter.count(
132
+ delta_payload_json, target_model=target_model, tokenizer_pref=tokenizer_pref
133
+ ).tokens
134
+
135
+ delta_budget = build_context_budget(
136
+ serialized_payload=delta_payload_json,
137
+ requested_budget=full_pack.budget.requested_budget,
138
+ required_minimum_budget=min(full_pack.budget.required_minimum_budget, delta_tokens),
139
+ target_model=target_model,
140
+ tokenizer_pref=tokenizer_pref,
141
+ omitted_sections=full_pack.budget.omitted_sections,
142
+ omitted_candidates=full_pack.budget.omitted_candidates,
143
+ )
144
+
145
+ return ContextDeltaResponse(
146
+ mode="delta",
147
+ repo_id=full_pack.freshness.repo_id,
148
+ snapshot_id=full_pack.freshness.snapshot_id,
149
+ task_id=full_pack.task.task_id,
150
+ added_blocks=added,
151
+ updated_blocks=updated,
152
+ invalidated_block_ids=invalidated,
153
+ unchanged_block_references=sorted(unchanged_refs),
154
+ required_dependencies=[b.block_id for b in full_pack.blocks if b.required],
155
+ evidence=delta_evidence,
156
+ full_response_bytes=full_bytes,
157
+ delta_response_bytes=delta_bytes,
158
+ repeated_context_bytes_avoided=repeated_bytes_avoided,
159
+ full_counted_tokens=full_tokens,
160
+ delta_counted_tokens=delta_tokens,
161
+ budget=delta_budget,
162
+ trust_boundary=full_pack.trust_boundary,
163
+ )
164
+
165
+
166
+ def reconstruct_blocks_from_delta(
167
+ client_blocks_by_id: dict[str, ContextBlock],
168
+ delta: ContextDeltaResponse,
169
+ ) -> tuple[list[ContextBlock], list[str]]:
170
+ """Reconstructs the active block list on the client side from cached blocks + delta.
171
+
172
+ Returns (reconstructed_blocks, missing_block_ids).
173
+ If `missing_block_ids` is non-empty, client must request a full context pack.
174
+ """
175
+ working: dict[str, ContextBlock] = dict(client_blocks_by_id)
176
+ for inv_id in delta.invalidated_block_ids:
177
+ working.pop(inv_id, None)
178
+
179
+ missing: list[str] = []
180
+ out_map: dict[str, ContextBlock] = {}
181
+
182
+ for ref_id in delta.unchanged_block_references:
183
+ blk = working.get(ref_id)
184
+ if blk is None:
185
+ missing.append(ref_id)
186
+ else:
187
+ out_map[ref_id] = blk
188
+
189
+ for blk in delta.added_blocks:
190
+ out_map[blk.block_id] = blk
191
+ for blk in delta.updated_blocks:
192
+ out_map[blk.block_id] = blk
193
+
194
+ reconstructed = sorted(
195
+ out_map.values(),
196
+ key=lambda b: (-int(b.required), -b.priority, b.block_id),
197
+ )
198
+ return reconstructed, missing
@@ -0,0 +1,134 @@
1
+ """Agent Evidence Validation and Grounding for RepoDNA v0.11.0.
2
+
3
+ Validates evidence references against:
4
+ - Repository scope (path traversal / workspace escape rejected)
5
+ - Snapshot / revision identity
6
+ - Relative source path & source line range
7
+ - Content hash on disk (staleness detection)
8
+ - Language capability level (PARTIAL when partial static analysis)
9
+ - Secret redaction with explicit redacted_snippet_hash provenance
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from pathlib import Path
15
+
16
+ from repodna.agent.models import (
17
+ AgentEvidenceReference,
18
+ EvidenceValidationStatus,
19
+ EvidenceVerificationResult,
20
+ FreshnessMetadata,
21
+ )
22
+ from repodna.agent.safety import sanitize_untrusted_text, sha256_short
23
+ from repodna.brain.indexer import SymbolIndexer
24
+ from repodna.schemas.models import LanguageSupportLevel
25
+ from repodna.security.validators import SecurityValidationError, validate_and_resolve_evidence_path
26
+
27
+
28
+ def verify_agent_evidence(
29
+ evidence_id: str,
30
+ indexer: SymbolIndexer,
31
+ repo_id: str,
32
+ repository_name: str,
33
+ snapshot_id: str,
34
+ repo_root: Path | None = None,
35
+ expected_snapshot_id: str | None = None,
36
+ ) -> EvidenceVerificationResult:
37
+ """Verifies a single evidence ID against the ledger, snapshot, and disk."""
38
+ freshness = FreshnessMetadata(
39
+ repo_id=repo_id,
40
+ repository=repository_name,
41
+ snapshot_id=snapshot_id,
42
+ revision=indexer.revision or "WORKING_TREE",
43
+ )
44
+
45
+ clean_id = (evidence_id or "").strip()
46
+ if not clean_id or len(clean_id) > 128:
47
+ return EvidenceVerificationResult(
48
+ evidence_id=clean_id,
49
+ status=EvidenceValidationStatus.INVALID,
50
+ freshness=freshness,
51
+ details="Evidence ID is empty or exceeds maximum valid length.",
52
+ )
53
+
54
+ if expected_snapshot_id and expected_snapshot_id != snapshot_id:
55
+ return EvidenceVerificationResult(
56
+ evidence_id=clean_id,
57
+ status=EvidenceValidationStatus.STALE,
58
+ freshness=freshness,
59
+ details=(
60
+ f"Evidence belongs to snapshot '{expected_snapshot_id}', which does not match "
61
+ f"current snapshot '{snapshot_id}'."
62
+ ),
63
+ )
64
+
65
+ ev = indexer.ledger.get_by_id(clean_id)
66
+ if ev is None:
67
+ return EvidenceVerificationResult(
68
+ evidence_id=clean_id,
69
+ status=EvidenceValidationStatus.UNAVAILABLE,
70
+ freshness=freshness,
71
+ details=f"Evidence ID '{clean_id}' was not found in the current snapshot ledger.",
72
+ )
73
+
74
+ status = EvidenceValidationStatus.VERIFIED
75
+ details = "Evidence range and content hash verified against current repository state."
76
+
77
+ if repo_root is not None:
78
+ try:
79
+ resolved, _ = validate_and_resolve_evidence_path(repo_root, ev.file_path)
80
+ if not resolved.exists():
81
+ status = EvidenceValidationStatus.UNAVAILABLE
82
+ details = f"Source file '{ev.file_path}' no longer exists on disk."
83
+ else:
84
+ disk_text = resolved.read_text(encoding="utf-8", errors="replace")
85
+ indexer.ledger.check_staleness(ev.file_path, disk_text, indexer.revision)
86
+ if ev.is_stale:
87
+ status = EvidenceValidationStatus.STALE
88
+ details = f"Evidence is STALE ({ev.staleness_reason.value}): {ev.staleness_details}"
89
+ except SecurityValidationError as exc:
90
+ return EvidenceVerificationResult(
91
+ evidence_id=clean_id,
92
+ status=EvidenceValidationStatus.INVALID,
93
+ freshness=freshness,
94
+ details=f"Security validation failed for evidence path: {exc.message}",
95
+ )
96
+ except OSError as exc:
97
+ status = EvidenceValidationStatus.UNAVAILABLE
98
+ details = f"Unable to read source file '{ev.file_path}': {exc}"
99
+
100
+ if status == EvidenceValidationStatus.VERIFIED and ev.support_level == LanguageSupportLevel.PARTIAL_STATIC:
101
+ status = EvidenceValidationStatus.PARTIAL
102
+ details = "Source snippet verified, but language uses partial static analysis (not full AST)."
103
+
104
+ safe_snip, sec_count, _ = sanitize_untrusted_text(ev.code_snippet or "")
105
+ was_redacted = sec_count > 0 or "[REDACTED:" in safe_snip
106
+ redacted_hash = sha256_short(safe_snip, 16) if was_redacted else None
107
+
108
+ ref = AgentEvidenceReference(
109
+ evidence_id=ev.evidence_id,
110
+ repo_id=repo_id,
111
+ snapshot_id=snapshot_id,
112
+ revision=ev.repository_revision,
113
+ file_path=ev.file_path,
114
+ line_start=ev.line_start,
115
+ line_end=ev.line_end,
116
+ evidence_type=ev.evidence_type.value,
117
+ symbol_name=ev.symbol_name,
118
+ content_hash=ev.content_hash or "",
119
+ redacted=sec_count > 0 or "[REDACTED:" in safe_snip,
120
+ redacted_snippet_hash=redacted_hash,
121
+ snippet=safe_snip,
122
+ capability_level=ev.support_level.value,
123
+ status=status,
124
+ staleness_reason=ev.staleness_reason.value,
125
+ confidence_reason=ev.confidence_reason,
126
+ )
127
+
128
+ return EvidenceVerificationResult(
129
+ evidence_id=clean_id,
130
+ status=status,
131
+ freshness=freshness,
132
+ evidence=ref,
133
+ details=details,
134
+ )