aid-runtime-python 0.1.124__tar.gz → 0.1.125__tar.gz

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 (113) hide show
  1. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/PKG-INFO +2 -2
  2. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/README.md +1 -1
  3. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/pyproject.toml +1 -1
  4. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/__init__.py +1 -1
  5. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/pgvector.py +21 -0
  6. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/retriever.py +35 -9
  7. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/stores.py +25 -0
  8. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/types.py +6 -1
  9. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime_python.egg-info/PKG-INFO +2 -2
  10. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_gate.py +85 -6
  11. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_stores.py +56 -2
  12. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/setup.cfg +0 -0
  13. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/__init__.py +0 -0
  14. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/chat_model.py +0 -0
  15. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/deep_agents.py +0 -0
  16. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/engine.py +0 -0
  17. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/guardrails.py +0 -0
  18. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/ir.py +0 -0
  19. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/loop.py +0 -0
  20. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/managed.py +0 -0
  21. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/memory.py +0 -0
  22. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/policy.py +0 -0
  23. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/agents/state.py +0 -0
  24. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/cache.py +0 -0
  25. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/concurrency.py +0 -0
  26. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/control.py +0 -0
  27. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/cost.py +0 -0
  28. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/decisions.py +0 -0
  29. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/errors.py +0 -0
  30. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/evals/__init__.py +0 -0
  31. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/evals/dataset.py +0 -0
  32. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/evals/gate.py +0 -0
  33. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/evals/metrics.py +0 -0
  34. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/evals/predicate.py +0 -0
  35. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/evals/runner.py +0 -0
  36. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/gateway.py +0 -0
  37. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/local_trace.py +0 -0
  38. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/pipeline.py +0 -0
  39. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/prompts.py +0 -0
  40. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/__init__.py +0 -0
  41. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/anthropic.py +0 -0
  42. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/base.py +0 -0
  43. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/bedrock.py +0 -0
  44. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/fake.py +0 -0
  45. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/gemini.py +0 -0
  46. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/gemini_wire.py +0 -0
  47. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/http.py +0 -0
  48. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/providers/vertex.py +0 -0
  49. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/py.typed +0 -0
  50. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/__init__.py +0 -0
  51. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/chunking.py +0 -0
  52. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/citations.py +0 -0
  53. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/connectors.py +0 -0
  54. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/embedding.py +0 -0
  55. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/gate.py +0 -0
  56. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/hybrid.py +0 -0
  57. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/knowledge_base.py +0 -0
  58. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/metrics.py +0 -0
  59. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/retrieval/rerank.py +0 -0
  60. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/steps.py +0 -0
  61. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/structured.py +0 -0
  62. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/tools.py +0 -0
  63. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/tracing.py +0 -0
  64. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime/types.py +0 -0
  65. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime_python.egg-info/SOURCES.txt +0 -0
  66. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime_python.egg-info/dependency_links.txt +0 -0
  67. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime_python.egg-info/requires.txt +0 -0
  68. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/src/aid_runtime_python.egg-info/top_level.txt +0 -0
  69. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_agents_budget.py +0 -0
  70. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_agents_deep_agents.py +0 -0
  71. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_agents_hitl.py +0 -0
  72. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_agents_loop.py +0 -0
  73. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_agents_memory.py +0 -0
  74. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_anthropic_provider.py +0 -0
  75. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_bedrock_provider.py +0 -0
  76. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_cache.py +0 -0
  77. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_concurrency.py +0 -0
  78. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_control.py +0 -0
  79. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_cost.py +0 -0
  80. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_dataset.py +0 -0
  81. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_decisions.py +0 -0
  82. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_evals.py +0 -0
  83. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_evals_assert_gate.py +0 -0
  84. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_evals_decision.py +0 -0
  85. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_evals_gate.py +0 -0
  86. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_evals_predicate.py +0 -0
  87. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_gateway.py +0 -0
  88. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_gemini_provider.py +0 -0
  89. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_guardrails.py +0 -0
  90. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_guardrails_managed.py +0 -0
  91. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_guardrails_model_armor.py +0 -0
  92. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_http_provider.py +0 -0
  93. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_local_trace.py +0 -0
  94. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_package.py +0 -0
  95. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_pipeline.py +0 -0
  96. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_prompts.py +0 -0
  97. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_chunking.py +0 -0
  98. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_citations.py +0 -0
  99. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_connectors.py +0 -0
  100. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_embedding.py +0 -0
  101. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_hybrid.py +0 -0
  102. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_knowledge_base.py +0 -0
  103. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_metrics.py +0 -0
  104. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_rerank.py +0 -0
  105. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_retrieval_types.py +0 -0
  106. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_steps.py +0 -0
  107. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_streaming.py +0 -0
  108. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_structured.py +0 -0
  109. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_tool_permissions.py +0 -0
  110. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_tools.py +0 -0
  111. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_tracing.py +0 -0
  112. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_types.py +0 -0
  113. {aid_runtime_python-0.1.124 → aid_runtime_python-0.1.125}/tests/test_vertex_provider.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.124
3
+ Version: 0.1.125
4
4
  Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
5
5
  Author: The AID Framework authors
6
6
  License-Expression: Apache-2.0
@@ -273,7 +273,7 @@ These are refused explicitly rather than approximated — each raises `FeatureDe
273
273
 
274
274
  - `ModelGateway.stream` — the transport is request/response only, and no provider advertises
275
275
  `Capability.STREAMING`.
276
- - Retrieval: non-file sources, `semantic` chunking, `incremental` refresh, retriever `filters`, a hosted
276
+ - Retrieval: non-file sources, `semantic` chunking, retriever `filters`, a hosted
277
277
  reranker, and the `qdrant` / `pinecone` / `weaviate` / `custom` store kinds.
278
278
  - Escalation routing and third-party orchestration frameworks.
279
279
 
@@ -239,7 +239,7 @@ These are refused explicitly rather than approximated — each raises `FeatureDe
239
239
 
240
240
  - `ModelGateway.stream` — the transport is request/response only, and no provider advertises
241
241
  `Capability.STREAMING`.
242
- - Retrieval: non-file sources, `semantic` chunking, `incremental` refresh, retriever `filters`, a hosted
242
+ - Retrieval: non-file sources, `semantic` chunking, retriever `filters`, a hosted
243
243
  reranker, and the `qdrant` / `pinecone` / `weaviate` / `custom` store kinds.
244
244
  - Escalation routing and third-party orchestration frameworks.
245
245
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "aid-runtime-python"
7
- version = "0.1.124"
7
+ version = "0.1.125"
8
8
  description = "The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -310,7 +310,7 @@ from aid_runtime.types import (
310
310
  )
311
311
 
312
312
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
313
- __version__ = "0.1.124"
313
+ __version__ = "0.1.125"
314
314
 
315
315
  __all__ = [
316
316
  "ANTHROPIC_VERSION",
@@ -77,6 +77,10 @@ _CORPUS = (
77
77
  "SELECT chunk_id, doc_id, uri, chunk_index, start_offset, end_offset, body, metadata, "
78
78
  "embedding FROM aid_chunks ORDER BY chunk_id"
79
79
  )
80
+ _CHUNK_IDS = "SELECT chunk_id FROM aid_chunks ORDER BY chunk_id"
81
+ # `= ANY(%(chunk_ids)s)` binds a Python list as one array parameter, so a reconciliation
82
+ # that names thousands of stale chunks still sends one statement rather than one per id.
83
+ _DELETE = "DELETE FROM aid_chunks WHERE chunk_id = ANY(%(chunk_ids)s)"
80
84
  # `<=>` is cosine distance, `<#>` is negative inner product and `<->` is L2 distance.
81
85
  # Each is rewritten into "higher is better" so callers see one orientation, matching
82
86
  # `stores.similarity`. The tie-break on `chunk_id` matches the in-memory adapter too:
@@ -212,6 +216,23 @@ class PgVectorStore:
212
216
  rows = await cursor.fetchall()
213
217
  return tuple(self._record(row) for row in rows)
214
218
 
219
+ async def chunk_ids(self) -> tuple[str, ...]:
220
+ connection = await self._connect()
221
+ async with connection.cursor() as cursor:
222
+ await cursor.execute(_CHUNK_IDS)
223
+ rows = await cursor.fetchall()
224
+ return tuple(row[0] for row in rows)
225
+
226
+ async def delete(self, chunk_ids: Sequence[str]) -> int:
227
+ if not chunk_ids:
228
+ return 0
229
+ connection = await self._connect()
230
+ async with connection.cursor() as cursor:
231
+ await cursor.execute(_DELETE, {"chunk_ids": list(chunk_ids)})
232
+ removed = int(cursor.rowcount)
233
+ await connection.commit()
234
+ return max(removed, 0)
235
+
215
236
  async def aclose(self) -> None:
216
237
  if self._closed:
217
238
  return
@@ -78,6 +78,12 @@ class IngestReport(BaseModel):
78
78
  ingests of identical input compare equal without depending on the order the
79
79
  filesystem happened to yield. ``cost_usd`` is what *this* ingest charged, not the
80
80
  ledger's running total, so ingesting twice reads as two separate spends.
81
+
82
+ ``written`` counts chunks upserted and ``reused`` counts chunks an incremental
83
+ ingest found already stored and therefore did not embed; ``written + reused`` is
84
+ ``chunks``. ``removed`` counts chunks deleted because their document no longer
85
+ yields them. All three are zero-or-more, and all but ``written`` are zero for a
86
+ ``full`` refresh, which never reads the index and never deletes from it.
81
87
  """
82
88
 
83
89
  model_config = ConfigDict(frozen=True, extra="forbid")
@@ -89,6 +95,8 @@ class IngestReport(BaseModel):
89
95
  tokens: int = Field(ge=0)
90
96
  cost_usd: float = Field(ge=0.0)
91
97
  chunk_ids: tuple[str, ...]
98
+ reused: int = Field(default=0, ge=0)
99
+ removed: int = Field(default=0, ge=0)
92
100
 
93
101
  @property
94
102
  def is_empty(self) -> bool:
@@ -145,12 +153,6 @@ class Retriever:
145
153
  self._connector = None
146
154
  self._base_dir = None
147
155
  return
148
- if binding.refresh_mode is not RefreshMode.FULL:
149
- raise FeatureDeferredError(
150
- f"refresh mode {binding.refresh_mode.value!r} is declared but not implemented: "
151
- "spec v0.1 reindexes the whole source, and incremental refresh needs change "
152
- "detection the runtime does not have"
153
- )
154
156
  self._knowledge_base_client = None
155
157
  self._embedder = embedder or embedder_for(binding.embedding)
156
158
  self._store = store or create_store(
@@ -185,6 +187,18 @@ class Retriever:
185
187
  upserts on them, so ingesting the same input twice replaces rather than
186
188
  duplicates. Re-ingesting is therefore a legitimate way to refresh a corpus.
187
189
 
190
+ ``refresh.mode`` decides what the second run redoes. ``full`` -- the default --
191
+ embeds and upserts every chunk and never reads or deletes from the index, so
192
+ dropping a document from the source leaves its vectors behind. ``incremental``
193
+ instead reconciles the index with the source: a chunk the store already holds
194
+ keeps its vector, a stored chunk the source no longer yields is deleted, and
195
+ only new chunks are embedded.
196
+
197
+ Incremental therefore treats what it is given as the *whole* corpus, whether
198
+ that came from the connector or from an explicit ``documents`` argument. Passing
199
+ a subset of the corpus in incremental mode deletes the rest, exactly as narrowing
200
+ the source glob would; the mode is a reconciliation, not a merge.
201
+
188
202
  Refused for a managed retriever. The engine ingests from its own data source, so
189
203
  a local ingest would write vectors the retriever never reads -- work that costs
190
204
  money and changes nothing, which is the worst kind of silent success.
@@ -195,16 +209,26 @@ class Retriever:
195
209
  "which ingests from its data source inside the engine; there is no local "
196
210
  "index to fill, and writing one would be read by nothing"
197
211
  )
198
- source_documents = tuple(documents) if documents is not None else await self._read_source()
199
212
  local = self._local
213
+ incremental = local.refresh_mode is RefreshMode.INCREMENTAL
214
+ source_documents = tuple(documents) if documents is not None else await self._read_source()
200
215
  chunker = chunker_for(local.chunking)
201
216
  chunks = [chunk for document in source_documents for chunk in chunker.chunk(document)]
202
217
 
218
+ to_embed = chunks
219
+ removed = 0
220
+ if incremental:
221
+ stored = set(await self.store.chunk_ids())
222
+ current = {chunk.chunk_id for chunk in chunks}
223
+ if stale := sorted(stored - current):
224
+ removed = await self.store.delete(stale)
225
+ to_embed = [chunk for chunk in chunks if chunk.chunk_id not in stored]
226
+
203
227
  records: list[StoreRecord] = []
204
228
  tokens = 0
205
229
  cost = 0.0
206
- for start in range(0, len(chunks), _BATCH_SIZE):
207
- batch = chunks[start : start + _BATCH_SIZE]
230
+ for start in range(0, len(to_embed), _BATCH_SIZE):
231
+ batch = to_embed[start : start + _BATCH_SIZE]
208
232
  embedded, batch_cost = await self._embed(tuple(chunk.text for chunk in batch))
209
233
  tokens += embedded.usage.input_tokens
210
234
  cost += batch_cost
@@ -219,6 +243,8 @@ class Retriever:
219
243
  documents=len(source_documents),
220
244
  chunks=len(chunks),
221
245
  written=written,
246
+ reused=len(chunks) - len(to_embed),
247
+ removed=removed,
222
248
  tokens=tokens,
223
249
  cost_usd=cost,
224
250
  chunk_ids=tuple(sorted(chunk.chunk_id for chunk in chunks)),
@@ -92,6 +92,21 @@ class VectorStore(Protocol):
92
92
  """
93
93
  ...
94
94
 
95
+ async def chunk_ids(self) -> tuple[str, ...]:
96
+ """Every stored chunk id, sorted.
97
+
98
+ Separate from :meth:`corpus` because an incremental ingest needs only
99
+ identity: it compares this set with the ids the source currently yields, and
100
+ fetching every vector to learn a key would cost what the mode exists to save.
101
+ """
102
+
103
+ async def delete(self, chunk_ids: Sequence[str]) -> int:
104
+ """Remove records by ``chunk_id``. Returns the number actually removed.
105
+
106
+ Deleting an absent id is not an error -- a caller reconciling an index with a
107
+ source may name a chunk another run already removed.
108
+ """
109
+
95
110
  async def aclose(self) -> None: ...
96
111
 
97
112
 
@@ -134,6 +149,16 @@ class InMemoryVectorStore:
134
149
  async def corpus(self) -> tuple[StoreRecord, ...]:
135
150
  return tuple(self._records[chunk_id] for chunk_id in sorted(self._records))
136
151
 
152
+ async def chunk_ids(self) -> tuple[str, ...]:
153
+ return tuple(sorted(self._records))
154
+
155
+ async def delete(self, chunk_ids: Sequence[str]) -> int:
156
+ removed = 0
157
+ for chunk_id in chunk_ids:
158
+ if self._records.pop(chunk_id, None) is not None:
159
+ removed += 1
160
+ return removed
161
+
137
162
  async def aclose(self) -> None:
138
163
  self._closed = True
139
164
 
@@ -119,7 +119,12 @@ class StoreKind(StrEnum):
119
119
 
120
120
 
121
121
  class RefreshMode(StrEnum):
122
- """``retriever.refresh.mode``. Only ``FULL`` is implemented."""
122
+ """``retriever.refresh.mode``.
123
+
124
+ ``FULL`` re-embeds the whole source every run. ``INCREMENTAL`` reconciles the index
125
+ with the source: unchanged chunks keep their vectors, stale ones are deleted, and
126
+ only new ones are embedded.
127
+ """
123
128
 
124
129
  FULL = "full"
125
130
  INCREMENTAL = "incremental"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.124
3
+ Version: 0.1.125
4
4
  Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
5
5
  Author: The AID Framework authors
6
6
  License-Expression: Apache-2.0
@@ -273,7 +273,7 @@ These are refused explicitly rather than approximated — each raises `FeatureDe
273
273
 
274
274
  - `ModelGateway.stream` — the transport is request/response only, and no provider advertises
275
275
  `Capability.STREAMING`.
276
- - Retrieval: non-file sources, `semantic` chunking, `incremental` refresh, retriever `filters`, a hosted
276
+ - Retrieval: non-file sources, `semantic` chunking, retriever `filters`, a hosted
277
277
  reranker, and the `qdrant` / `pinecone` / `weaviate` / `custom` store kinds.
278
278
  - Escalation routing and third-party orchestration frameworks.
279
279
 
@@ -34,7 +34,6 @@ from aid_runtime import (
34
34
  BudgetExceededError,
35
35
  ConfigurationError,
36
36
  EvalError,
37
- FeatureDeferredError,
38
37
  )
39
38
  from aid_runtime.cost import CostLedger
40
39
  from aid_runtime.evals.gate import EvalBaseline, EvalRun, MetricRun, Threshold
@@ -368,11 +367,91 @@ def test_a_candidate_factor_below_one_is_refused() -> None:
368
367
  Retriever(binding(), candidate_factor=0)
369
368
 
370
369
 
371
- def test_incremental_refresh_is_deferred_rather_than_ignored() -> None:
372
- # Quietly treating an incremental refresh as a full one would be a correctness hole:
373
- # the caller asked for something this layer does not implement.
374
- with pytest.raises(FeatureDeferredError, match="incremental"):
375
- Retriever(binding(refresh_mode=RefreshMode.INCREMENTAL))
370
+ def make_incremental_retriever(
371
+ *,
372
+ glob: str | None = "**/*.md",
373
+ size: int = 120,
374
+ ) -> Retriever:
375
+ """A retriever that reconciles its index with its source, over a shared store."""
376
+ return Retriever(
377
+ binding(glob=glob, size=size, refresh_mode=RefreshMode.INCREMENTAL),
378
+ embedder=FakeDeterministicEmbedder(),
379
+ store=InMemoryVectorStore(),
380
+ )
381
+
382
+
383
+ async def test_incremental_refresh_reuses_every_unchanged_chunk() -> None:
384
+ # The point of the mode: a second run over an unchanged corpus must not spend on
385
+ # embeddings. `tokens` is what the fake embedder reported, so a zero here means no
386
+ # text reached the embedder -- not merely that the writes were skipped.
387
+ retriever = make_incremental_retriever()
388
+ documents = [document("a.md", "alpha"), document("b.md", "beta")]
389
+
390
+ first = await retriever.ingest(documents)
391
+ second = await retriever.ingest(documents)
392
+
393
+ assert first.written == 2
394
+ assert second.reused == 2
395
+ assert second.written == 0
396
+ assert second.removed == 0
397
+ assert second.tokens == 0
398
+ assert second.cost_usd == 0.0
399
+ assert second.chunk_ids == first.chunk_ids
400
+
401
+
402
+ async def test_incremental_refresh_reconciles_a_changed_document() -> None:
403
+ # A changed document yields a different content hash, so its old chunk is stale.
404
+ # Leaving it would keep returning text the source no longer contains.
405
+ retriever = make_incremental_retriever()
406
+
407
+ first = await retriever.ingest([document("a.md", "alpha"), document("b.md", "beta")])
408
+ second = await retriever.ingest([document("a.md", "alpha, revised"), document("b.md", "beta")])
409
+
410
+ stored = {record.chunk.chunk_id for record in await retriever.store.corpus()}
411
+ assert len(stored) == 2
412
+ assert len(set(first.chunk_ids) - stored) == 1
413
+ assert second.written == 1
414
+ assert second.reused == 1
415
+ assert second.removed == 1
416
+
417
+
418
+ async def test_incremental_refresh_deletes_a_removed_document() -> None:
419
+ # A full refresh never deletes, so this is the behaviour the mode actually buys.
420
+ # An explicit list is the snapshot the index is reconciled against, so omitting a
421
+ # document means its chunks are gone; the opposite reading -- a merge -- would keep
422
+ # text the caller considers removed.
423
+ retriever = make_incremental_retriever()
424
+
425
+ await retriever.ingest([document("a.md", "alpha"), document("b.md", "beta")])
426
+ second = await retriever.ingest([document("a.md", "alpha")])
427
+
428
+ assert [record.chunk.doc_id for record in await retriever.store.corpus()] == ["a.md"]
429
+ assert second.removed == 1
430
+ assert second.reused == 1
431
+ assert second.documents == 1
432
+
433
+
434
+ async def test_incremental_refresh_reads_the_source_through_the_connector(
435
+ tmp_path: Path,
436
+ ) -> None:
437
+ # The path generated code takes: the index is reconciled against disk, so deleting
438
+ # a file removes its vectors without the caller naming them.
439
+ write_docs(tmp_path / "docs", {"guide.md": "alpha", "nested/deep.md": "beta"})
440
+ retriever = Retriever(
441
+ binding(refresh_mode=RefreshMode.INCREMENTAL),
442
+ embedder=FakeDeterministicEmbedder(),
443
+ store=InMemoryVectorStore(),
444
+ base_dir=str(tmp_path),
445
+ )
446
+
447
+ first = await retriever.ingest()
448
+ (tmp_path / "docs" / "nested" / "deep.md").unlink()
449
+ second = await retriever.ingest()
450
+
451
+ assert first.written == 2
452
+ assert second.reused == 1
453
+ assert second.removed == 1
454
+ assert [record.chunk.doc_id for record in await retriever.store.corpus()] == ["guide.md"]
376
455
 
377
456
 
378
457
  def test_the_gate_exposes_its_thresholds_and_metrics() -> None:
@@ -110,7 +110,9 @@ async def test_re_upserting_the_same_chunk_replaces_it_rather_than_duplicating()
110
110
  await store.upsert([record])
111
111
  await store.upsert([record])
112
112
 
113
- assert len(await store.corpus()) == 1
113
+ corpus = await store.corpus()
114
+ assert len(corpus) == 1
115
+ assert {record.chunk.doc_id for record in corpus} == {"a.md"}
114
116
 
115
117
 
116
118
  async def test_the_corpus_is_ordered_by_chunk_id() -> None:
@@ -125,7 +127,49 @@ async def test_the_corpus_is_ordered_by_chunk_id() -> None:
125
127
  assert [record.chunk.chunk_id for record in corpus] == sorted(
126
128
  record.chunk.chunk_id for record in records
127
129
  )
128
- assert {record.chunk.doc_id for record in corpus} == {"a.md", "b.md"}
130
+
131
+
132
+ async def test_chunk_ids_are_the_corpus_keys_and_nothing_more() -> None:
133
+ # The incremental ingest compares ids alone; carrying vectors here would charge it
134
+ # for data it does not read.
135
+ store = InMemoryVectorStore()
136
+ records = [store_record("b", (1.0,)), store_record("a", (1.0,))]
137
+ await store.upsert(records)
138
+
139
+ assert await store.chunk_ids() == tuple(sorted(record.chunk.chunk_id for record in records))
140
+
141
+
142
+ async def test_deleting_reports_how_many_records_were_removed() -> None:
143
+ store = InMemoryVectorStore()
144
+ records = [store_record("a", (1.0,)), store_record("b", (1.0,))]
145
+ await store.upsert(records)
146
+
147
+ removed = await store.delete([records[0].chunk.chunk_id])
148
+
149
+ assert removed == 1
150
+ assert await store.chunk_ids() == (records[1].chunk.chunk_id,)
151
+
152
+
153
+ async def test_deleting_a_chunk_that_is_not_stored_is_not_an_error() -> None:
154
+ # A reconciliation may name a chunk a concurrent run already removed; refusing it
155
+ # would turn a benign race into a failed ingest.
156
+ store = InMemoryVectorStore()
157
+ await store.upsert([store_record("a", (1.0,))])
158
+
159
+ removed = await store.delete(["not-a-real-chunk-id"])
160
+
161
+ assert removed == 0
162
+ assert len(await store.corpus()) == 1
163
+
164
+
165
+ async def test_deleting_nothing_is_a_no_op() -> None:
166
+ store = InMemoryVectorStore()
167
+ await store.upsert([store_record("a", (1.0,))])
168
+
169
+ removed = await store.delete([])
170
+
171
+ assert removed == 0
172
+ assert len(await store.corpus()) == 1
129
173
 
130
174
 
131
175
  async def test_a_search_returns_the_nearest_records_highest_first() -> None:
@@ -271,6 +315,16 @@ async def test_upserting_nothing_does_not_reach_for_the_driver() -> None:
271
315
  assert await store.upsert([]) == 0
272
316
 
273
317
 
318
+ async def test_deleting_nothing_does_not_reach_for_the_driver() -> None:
319
+ # The same short-circuit as an empty upsert: an unchanged corpus reconciles to no
320
+ # deletions, and that run must not open a connection to say so.
321
+ store = PgVectorStore(uri=URI, dimensions=DIMENSIONS)
322
+
323
+ removed = await store.delete([])
324
+
325
+ assert removed == 0
326
+
327
+
274
328
  async def test_a_missing_driver_is_a_configuration_error_that_names_the_extra() -> None:
275
329
  if driver_installed():
276
330
  pytest.skip("the pgvector extra is installed, so the missing-driver path is unreachable")