llama-index-postprocessor-needlepath 0.1.2__tar.gz → 0.1.4__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.2
3
+ Version: 0.1.4
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.2.3
11
+ Requires-Dist: needlepath<1.0.0,>=0.2.5
12
12
  Provides-Extra: dev
13
13
  Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
14
14
  Requires-Dist: pytest>=7; extra == 'dev'
@@ -55,7 +55,7 @@ you have an agent with tool calls, start there instead.
55
55
  | `excerpt` | `True` | Replace kept nodes' text with their excerpt. `False` filters only. |
56
56
  | `update_scores` | `False` | Replace `NodeWithScore.score` with the selection score. |
57
57
  | `preserve_input_order` | `False` | Return kept nodes in retrieval order rather than ranked order. |
58
- | `shadow` | `False` | Measure and report; change nothing. |
58
+ | `shadow` | `False` | Measure and report; apply no ordinary selection. A security delivery (blocked, protected, `sanitized_fallback`, `security_action: quarantine`) is still installed. |
59
59
  | `enabled` | `True` | Kill switch. Needs no credentials when `False`. |
60
60
 
61
61
  Any other keyword is forwarded to the core client (`base_url`, `api_key`,
@@ -38,7 +38,7 @@ you have an agent with tool calls, start there instead.
38
38
  | `excerpt` | `True` | Replace kept nodes' text with their excerpt. `False` filters only. |
39
39
  | `update_scores` | `False` | Replace `NodeWithScore.score` with the selection score. |
40
40
  | `preserve_input_order` | `False` | Return kept nodes in retrieval order rather than ranked order. |
41
- | `shadow` | `False` | Measure and report; change nothing. |
41
+ | `shadow` | `False` | Measure and report; apply no ordinary selection. A security delivery (blocked, protected, `sanitized_fallback`, `security_action: quarantine`) is still installed. |
42
42
  | `enabled` | `True` | Kill switch. Needs no credentials when `False`. |
43
43
 
44
44
  Any other keyword is forwarded to the core client (`base_url`, `api_key`,
@@ -172,7 +172,8 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
172
172
  max_context_tokens=self.max_context_tokens,
173
173
  shadow=True if self.shadow else None,
174
174
  )
175
- return self._apply(nodes, plan, result)
175
+ with result.applying():
176
+ return self._apply(nodes, plan, result)
176
177
  except NeedlepathGuardrailError:
177
178
  # Mandatory protection failures must stop the framework call.
178
179
  raise
@@ -234,7 +235,8 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
234
235
  max_context_tokens=self.max_context_tokens,
235
236
  shadow=True if self.shadow else None,
236
237
  )
237
- return self._apply(nodes, plan, result)
238
+ with result.applying():
239
+ return self._apply(nodes, plan, result)
238
240
 
239
241
  # ------------------------------------------------------------------ #
240
242
 
@@ -292,11 +294,18 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
292
294
  ) -> list[NodeWithScore]:
293
295
  self._stats.record(result)
294
296
  # `self.shadow` is belt and braces — the core client already declines a
295
- # shadow result — enforced here, at the point of mutation. A blocked
296
- # verdict is the one delivery shadow does not suppress: shadow never
297
- # restores withheld nodes.
297
+ # shadow result — enforced here, at the point of mutation. What shadow
298
+ # does not suppress is a security delivery (a blocked verdict, a
299
+ # selection under workspace protection, a sanitized fallback, or a
300
+ # selection the engine quarantined an instruction from): shadow never
301
+ # hands back withheld nodes, a protected original or a removed
302
+ # instruction.
298
303
  blocked = result.reason == Reason.BLOCKED
299
- if not result.applied or result.response is None or (self.shadow and not blocked):
304
+ if (
305
+ not result.applied
306
+ or result.response is None
307
+ or (self.shadow and not result.security_delivery)
308
+ ):
300
309
  return nodes
301
310
  if blocked:
302
311
  # Workspace policy withheld every record. The verdict is "deliver
@@ -307,6 +316,10 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
307
316
  # guard below turns it back into a passthrough.
308
317
  return []
309
318
 
319
+ # Workspace protection: the engine's `governance` forbids raw-context
320
+ # fallback. Only what the engine delivered may be returned; nothing
321
+ # of the original nodes, per node or in aggregate.
322
+ protected = not result.raw_context_fallback_allowed
310
323
  index_by_id: dict[str, int] = plan["index_by_id"]
311
324
  # (input index, node) pairs. The index is carried explicitly rather than
312
325
  # recovered later: `_rewrite` returns a *copy* for any node it changes,
@@ -323,9 +336,17 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
323
336
  # node in the context.
324
337
  continue
325
338
  seen.add(index)
326
- kept.append((index, self._rewrite(nodes[index], selected, result)))
339
+ rewritten = self._rewrite(nodes[index], selected, result, protected=protected)
340
+ if rewritten is not None:
341
+ kept.append((index, rewritten))
327
342
 
328
343
  if not kept:
344
+ if protected:
345
+ # A valid zero-record `sanitized_fallback`, or every id came
346
+ # back unrecognized: there is no node to map the delivery
347
+ # onto, and the originals are the one thing that may not be
348
+ # returned. Nothing from these nodes reaches the query engine.
349
+ return []
329
350
  # Belt and braces: the core client already classifies an empty
330
351
  # selection as a passthrough, so reaching here means every id came
331
352
  # back unrecognized. Returning [] would hand the query engine no
@@ -337,11 +358,25 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
337
358
  return [node for _index, node in kept]
338
359
 
339
360
  def _rewrite(
340
- self, original: NodeWithScore, selected: Any, result: SelectionResult
341
- ) -> NodeWithScore:
361
+ self, original: NodeWithScore, selected: Any, result: SelectionResult, *, protected: bool
362
+ ) -> NodeWithScore | None:
363
+ """The node as the query engine should see it; ``None`` to withhold it.
364
+
365
+ Ordinarily the excerpt lands only when it is shorter than the node
366
+ (invariant 2) and ``excerpt=True``; otherwise the node is kept whole.
367
+ Under workspace protection the whole node is the thing policy forbids
368
+ and the excerpt is what the engine permitted, so both the option and
369
+ the length guard yield: the excerpt is installed whatever its length,
370
+ and a node with no excerpt to install is withheld rather than kept.
371
+ """
342
372
  text = _node_text(original)
343
373
  excerpt = selected.excerpt or ""
344
- use_excerpt = self.excerpt and excerpt.strip() != "" and len(excerpt) < len(text)
374
+ if protected:
375
+ if not excerpt.strip():
376
+ return None
377
+ use_excerpt = True
378
+ else:
379
+ use_excerpt = self.excerpt and excerpt.strip() != "" and len(excerpt) < len(text)
345
380
  if not use_excerpt and not self.update_scores:
346
381
  # Nothing to change: hand back the caller's own object rather than a
347
382
  # copy, so node identity survives for anything downstream that keys
@@ -1,3 +1,3 @@
1
- __version__ = "0.1.2"
1
+ __version__ = "0.1.4"
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.2"
7
+ version = "0.1.4"
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.2.3,<1.0.0",
20
+ "needlepath>=0.2.5,<1.0.0",
21
21
  ]
22
22
 
23
23
  [project.optional-dependencies]