llama-index-postprocessor-needlepath 0.1.2__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.2
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.
@@ -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,12 @@ 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 protected delivery (a blocked verdict, or a
299
+ # selection under workspace protection): shadow never restores
300
+ # withheld nodes or a protected original.
298
301
  blocked = result.reason == Reason.BLOCKED
299
- if not result.applied or result.response is None or (self.shadow and not blocked):
302
+ if not result.applied or result.response is None or (self.shadow and not result.protected):
300
303
  return nodes
301
304
  if blocked:
302
305
  # Workspace policy withheld every record. The verdict is "deliver
@@ -307,6 +310,10 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
307
310
  # guard below turns it back into a passthrough.
308
311
  return []
309
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
310
317
  index_by_id: dict[str, int] = plan["index_by_id"]
311
318
  # (input index, node) pairs. The index is carried explicitly rather than
312
319
  # recovered later: `_rewrite` returns a *copy* for any node it changes,
@@ -323,9 +330,17 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
323
330
  # node in the context.
324
331
  continue
325
332
  seen.add(index)
326
- 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))
327
336
 
328
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 []
329
344
  # Belt and braces: the core client already classifies an empty
330
345
  # selection as a passthrough, so reaching here means every id came
331
346
  # back unrecognized. Returning [] would hand the query engine no
@@ -337,11 +352,25 @@ class NeedlepathPostprocessor(BaseNodePostprocessor):
337
352
  return [node for _index, node in kept]
338
353
 
339
354
  def _rewrite(
340
- self, original: NodeWithScore, selected: Any, result: SelectionResult
341
- ) -> 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
+ """
342
366
  text = _node_text(original)
343
367
  excerpt = selected.excerpt or ""
344
- 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)
345
374
  if not use_excerpt and not self.update_scores:
346
375
  # Nothing to change: hand back the caller's own object rather than a
347
376
  # copy, so node identity survives for anything downstream that keys
@@ -1,3 +1,3 @@
1
- __version__ = "0.1.2"
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.2"
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"