llama-index-postprocessor-needlepath 0.1.1__tar.gz → 0.1.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: llama-index-postprocessor-needlepath
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: llama-index postprocessor needlepath context selection integration
5
5
  Project-URL: Homepage, https://nextmoca.com
6
6
  Author: Next Moca Global, Inc.
@@ -8,7 +8,7 @@ License-Expression: Apache-2.0
8
8
  License-File: LICENSE
9
9
  Requires-Python: <4.0,>=3.10
10
10
  Requires-Dist: llama-index-core<0.15,>=0.13.0
11
- Requires-Dist: needlepath<1.0.0,>=0.1.0
11
+ Requires-Dist: needlepath<1.0.0,>=0.2.3
12
12
  Provides-Extra: dev
13
13
  Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
14
14
  Requires-Dist: pytest>=7; extra == 'dev'
@@ -8,8 +8,8 @@ evaluated.
8
8
 
9
9
  What it does: given the retrieved nodes and the query, keep the nodes that carry
10
10
  the answer and replace each one's text with the part of it that does. What it
11
- never does: raise, or return fewer nodes than it was given when anything at all
12
- went wrong.
11
+ does on ordinary selection failure: return the original nodes. Mandatory
12
+ guardrail failures raise instead, so unfiltered nodes cannot reach the model.
13
13
 
14
14
  Two LlamaIndex-specific decisions worth reading before you deploy it:
15
15
 
@@ -39,6 +39,7 @@ from needlepath import (
39
39
  AsyncNeedlepathClient,
40
40
  ContextRecord,
41
41
  NeedlepathClient,
42
+ NeedlepathGuardrailError,
42
43
  Reason,
43
44
  RecordKind,
44
45
  SelectionResult,
@@ -171,9 +172,13 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
171
172
  max_context_tokens=self.max_context_tokens,
172
173
  shadow=True if self.shadow else None,
173
174
  )
174
- return self._apply(nodes, plan, result)
175
+ with result.applying():
176
+ return self._apply(nodes, plan, result)
177
+ except NeedlepathGuardrailError:
178
+ # Mandatory protection failures must stop the framework call.
179
+ raise
175
180
  except Exception: # noqa: BLE001 - see the class docstring's fail-open rule
176
- # The core client never raises, so this catches OUR OWN defects — a
181
+ # Guardrail errors propagate above. This catches ordinary defects — a
177
182
  # node type nobody anticipated, an index slip, a framework minor that
178
183
  # changed a field's type. A postprocessor that raises fails the whole
179
184
  # query engine; returning the caller's nodes is byte-identical to not
@@ -195,6 +200,9 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
195
200
  """
196
201
  try:
197
202
  return await self._apostprocess(nodes, query_bundle)
203
+ except NeedlepathGuardrailError:
204
+ # Mandatory protection failures must stop the framework call.
205
+ raise
198
206
  except Exception: # noqa: BLE001 - see `_postprocess_nodes`
199
207
  self._note(Reason.UNEXPECTED_ERROR)
200
208
  return nodes
@@ -227,7 +235,8 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
227
235
  max_context_tokens=self.max_context_tokens,
228
236
  shadow=True if self.shadow else None,
229
237
  )
230
- return self._apply(nodes, plan, result)
238
+ with result.applying():
239
+ return self._apply(nodes, plan, result)
231
240
 
232
241
  # ------------------------------------------------------------------ #
233
242
 
@@ -285,10 +294,26 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
285
294
  ) -> list[NodeWithScore]:
286
295
  self._stats.record(result)
287
296
  # `self.shadow` is belt and braces — the core client already declines a
288
- # shadow result — enforced here, at the point of mutation.
289
- if not result.applied or self.shadow or result.response is None:
297
+ # shadow result — enforced here, at the point of mutation. What shadow
298
+ # does not suppress is a protected delivery (a blocked verdict, or a
299
+ # selection under workspace protection): shadow never restores
300
+ # withheld nodes or a protected original.
301
+ blocked = result.reason == Reason.BLOCKED
302
+ if not result.applied or result.response is None or (self.shadow and not result.protected):
290
303
  return nodes
291
-
304
+ if blocked:
305
+ # Workspace policy withheld every record. The verdict is "deliver
306
+ # nothing", and it is installed as exactly that: the query engine
307
+ # gets no context from these nodes rather than the nodes
308
+ # themselves. This is the one applied result with nothing to
309
+ # show for it, which is why it is checked before the empty-`kept`
310
+ # guard below turns it back into a passthrough.
311
+ return []
312
+
313
+ # Workspace protection: the engine's `governance` forbids raw-context
314
+ # fallback. Only what the engine delivered may be returned; nothing
315
+ # of the original nodes, per node or in aggregate.
316
+ protected = not result.raw_context_fallback_allowed
292
317
  index_by_id: dict[str, int] = plan["index_by_id"]
293
318
  # (input index, node) pairs. The index is carried explicitly rather than
294
319
  # recovered later: `_rewrite` returns a *copy* for any node it changes,
@@ -305,9 +330,17 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
305
330
  # node in the context.
306
331
  continue
307
332
  seen.add(index)
308
- kept.append((index, self._rewrite(nodes[index], selected, result)))
333
+ rewritten = self._rewrite(nodes[index], selected, result, protected=protected)
334
+ if rewritten is not None:
335
+ kept.append((index, rewritten))
309
336
 
310
337
  if not kept:
338
+ if protected:
339
+ # A valid zero-record `sanitized_fallback`, or every id came
340
+ # back unrecognized: there is no node to map the delivery
341
+ # onto, and the originals are the one thing that may not be
342
+ # returned. Nothing from these nodes reaches the query engine.
343
+ return []
311
344
  # Belt and braces: the core client already classifies an empty
312
345
  # selection as a passthrough, so reaching here means every id came
313
346
  # back unrecognized. Returning [] would hand the query engine no
@@ -319,11 +352,25 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
319
352
  return [node for _index, node in kept]
320
353
 
321
354
  def _rewrite(
322
- self, original: NodeWithScore, selected: Any, result: SelectionResult
323
- ) -> NodeWithScore:
355
+ self, original: NodeWithScore, selected: Any, result: SelectionResult, *, protected: bool
356
+ ) -> NodeWithScore | None:
357
+ """The node as the query engine should see it; ``None`` to withhold it.
358
+
359
+ Ordinarily the excerpt lands only when it is shorter than the node
360
+ (invariant 2) and ``excerpt=True``; otherwise the node is kept whole.
361
+ Under workspace protection the whole node is the thing policy forbids
362
+ and the excerpt is what the engine permitted, so both the option and
363
+ the length guard yield: the excerpt is installed whatever its length,
364
+ and a node with no excerpt to install is withheld rather than kept.
365
+ """
324
366
  text = _node_text(original)
325
367
  excerpt = selected.excerpt or ""
326
- use_excerpt = self.excerpt and excerpt.strip() != "" and len(excerpt) < len(text)
368
+ if protected:
369
+ if not excerpt.strip():
370
+ return None
371
+ use_excerpt = True
372
+ else:
373
+ use_excerpt = self.excerpt and excerpt.strip() != "" and len(excerpt) < len(text)
327
374
  if not use_excerpt and not self.update_scores:
328
375
  # Nothing to change: hand back the caller's own object rather than a
329
376
  # copy, so node identity survives for anything downstream that keys
@@ -1,3 +1,3 @@
1
- __version__ = "0.1.1"
1
+ __version__ = "0.1.3"
2
2
 
3
3
  __all__ = ["__version__"]
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "llama-index-postprocessor-needlepath"
7
- version = "0.1.1"
7
+ version = "0.1.3"
8
8
  description = "llama-index postprocessor needlepath context selection integration"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10,<4.0"
@@ -17,7 +17,7 @@ license = "Apache-2.0"
17
17
  # `llama-index-postprocessor-*` is not discoverable at all.
18
18
  dependencies = [
19
19
  "llama-index-core>=0.13.0,<0.15",
20
- "needlepath>=0.1.0,<1.0.0",
20
+ "needlepath>=0.2.3,<1.0.0",
21
21
  ]
22
22
 
23
23
  [project.optional-dependencies]