Skip to content

Commit a41637d

Browse files
committed
Fix post-merge review findings from #71/#79/#80
1 parent 0bbb34d commit a41637d

12 files changed

Lines changed: 176 additions & 75 deletions

File tree

‎.github/actions/unplug-scan/action.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ inputs:
2121
unplug-version:
2222
description: PyPI version constraint when install-mode is pypi
2323
required: false
24-
default: ">=0.4.0,<0.5"
24+
default: ">=0.5.0,<0.6"
2525

2626
runs:
2727
using: composite

‎README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ Server/MCP integrations use `unplug.api.*` for wire types and facades — see
3535
[`sdk/docs/PUBLIC_API.md`](sdk/docs/PUBLIC_API.md). Do not use `unplug.core.*`.
3636

3737
```python
38-
from unplug import Guard
39-
from unplug.api.enums import Source
38+
from unplug import Guard, Source
4039

4140
guard = Guard()
4241

‎sdk/src/unplug/core/runtime/versions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
from __future__ import annotations
44

5-
NORMALIZER_VERSION = "v12"
5+
NORMALIZER_VERSION = "v13"
66
MODEL_VERSION_LOCAL = "regex-v1"

‎sdk/src/unplug/data/patterns/injection.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
regex: (?i)(please\s+)?(disregard|ignore|forget)\s+the\s+(above|previous|prior)\b
195195
flags: i
196196
- name: instructions_updated_supersede
197-
regex: (?i)(instructions?\s+have\s+been\s+updated.{0,80}(supersede|replace|override|ignore|disregard)|new\s+instructions?\s+supersede|following\s+instructions?\s+replace\s+your)
197+
regex: (?i)(instructions?\s+have\s+been\s+updated.{0,80}(supersede|override|ignore|disregard|replace\s+(?:your|all|previous|prior|above|system))|new\s+instructions?\s+supersede|following\s+instructions?\s+replace\s+your)
198198
flags: i
199199
- name: instruction_override_bracket
200200
regex: (?i)\[{1,2}\s*INSTRUCTION\s+OVERRIDE\s*\]{1,2}

‎sdk/src/unplug/integrations/ag2.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,25 @@ def _scan_redact_content(content: Any, scan: Callable[[str], HookDecision]) -> A
6060
if isinstance(content, list):
6161
# Scan the COMBINED text of all blocks, not each block in isolation: a
6262
# multimodal list reaches the model as one message, so an injection split
63-
# across adjacent text blocks must not slip past a per-block scan. Redaction
64-
# is consolidated into the first text block (and later text blocks cleared)
65-
# so the model-visible text equals the cleaned output, while non-text blocks
66-
# (images) are preserved.
63+
# across adjacent text blocks must not slip past a per-block scan. When
64+
# there are no text blocks, flatten string fields from image/file blocks
65+
# so user-controlled URLs and metadata are still inspected.
6766
text_indices = [
6867
i
6968
for i, block in enumerate(content)
7069
if isinstance(block, dict) and isinstance(block.get("text"), str)
7170
]
72-
combined = "\n".join(content[i]["text"] for i in text_indices)
71+
if text_indices:
72+
combined = "\n".join(content[i]["text"] for i in text_indices)
73+
else:
74+
combined = flatten_text(content)
7375
decision = scan(combined)
7476
require_allowed(decision)
7577
redacted = decision.redacted_text
76-
if not text_indices or not redacted or redacted == combined:
78+
if not redacted or redacted == combined:
7779
return content
80+
if not text_indices:
81+
return redacted
7882
new_content: list[Any] = [dict(b) if isinstance(b, dict) else b for b in content]
7983
new_content[text_indices[0]]["text"] = redacted
8084
for i in text_indices[1:]:
@@ -87,6 +91,8 @@ def _scan_redact_content(content: Any, scan: Callable[[str], HookDecision]) -> A
8791
redacted = decision.redacted_text
8892
if isinstance(content, str) and redacted and redacted != text:
8993
return redacted
94+
if not isinstance(content, str) and redacted and redacted != text:
95+
return redacted
9096
return content
9197

9298

‎sdk/src/unplug/ml/span_model.py‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __init__(
3939
self._checkpoint = Path(checkpoint)
4040
self._max_length_override = max_length
4141
self._stride = stride
42-
self._device = resolve_torch_device(device)
42+
self._device_preference = device
43+
self._device: str | None = None
4344
self._inj_threshold = inj_threshold
4445
self._doc_threshold = doc_threshold if doc_threshold is not None else inj_threshold
4546
self._local_files_only = local_files_only
@@ -60,7 +61,12 @@ def checkpoint(self) -> Path:
6061

6162
@property
6263
def device(self) -> str:
63-
return self._device
64+
if self._device is not None:
65+
return self._device
66+
pref = (self._device_preference or "").strip()
67+
if pref in ("", "auto"):
68+
return "auto"
69+
return pref
6470

6571
@property
6672
def loaded(self) -> bool:
@@ -75,14 +81,13 @@ def doc_threshold(self) -> float:
7581
return self._doc_threshold
7682

7783
def load(self) -> None:
78-
if self._model is not None:
79-
return
8084
with self._load_lock:
8185
if self._model is not None:
8286
return
8387
self._load_unlocked()
8488

8589
def _load_unlocked(self) -> None:
90+
self._device = resolve_torch_device(self._device_preference)
8691
torch = get_torch()
8792
get_transformers()
8893
from transformers import AutoConfig, AutoModelForTokenClassification, AutoTokenizer
@@ -179,12 +184,22 @@ def unload(self) -> None:
179184
def _clear_state(self) -> None:
180185
self._model = None
181186
self._tokenizer = None
187+
self._device = None
182188
self._label2id = {}
183189
self._id2label = {}
184190
self._is_dual_head = False
185191
self._has_disposition = False
186192
self._id2disposition = {}
187193

194+
def _require_loaded(self) -> tuple[PreTrainedTokenizerBase, PreTrainedModel]:
195+
with self._load_lock:
196+
if self._model is None:
197+
self._load_unlocked()
198+
if self._tokenizer is None or self._model is None:
199+
msg = "Model was unloaded during inference"
200+
raise ModelError(msg)
201+
return self._tokenizer, self._model
202+
188203
def predict(self, text: str) -> SpanPrediction:
189204
return self.predict_batch([text], batch_size=1)[0]
190205

@@ -196,9 +211,7 @@ def predict_batch(
196211
) -> list[SpanPrediction]:
197212
torch = get_torch()
198213

199-
self.load()
200-
assert self._tokenizer is not None
201-
assert self._model is not None
214+
tokenizer, model = self._require_loaded()
202215

203216
if not texts:
204217
return []
@@ -207,7 +220,7 @@ def predict_batch(
207220
bs = max(1, batch_size)
208221
for start in range(0, len(texts), bs):
209222
chunk = texts[start : start + bs]
210-
encoding = self._tokenizer(
223+
encoding = tokenizer(
211224
chunk,
212225
return_offsets_mapping=True,
213226
truncation=True,
@@ -218,7 +231,7 @@ def predict_batch(
218231
return_tensors="pt",
219232
)
220233
if encoding.get("overflow_to_sample_mapping") is not None:
221-
out.extend(self._predict_overflowing(encoding, chunk))
234+
out.extend(self._predict_overflowing(encoding, chunk, model, tokenizer))
222235
continue
223236

224237
offset_mappings = encoding.pop("offset_mapping")
@@ -231,7 +244,7 @@ def predict_batch(
231244
if key not in skip_keys
232245
}
233246
with torch.no_grad():
234-
outputs = self._model(**inputs)
247+
outputs = model(**inputs)
235248
logits = outputs.logits
236249
probs = torch.softmax(logits, dim=-1)
237250
doc_logits = getattr(outputs, "doc_logits", None)
@@ -271,12 +284,11 @@ def _predict_overflowing(
271284
self,
272285
encoding: dict,
273286
bodies: list[str],
287+
model: PreTrainedModel,
288+
tokenizer: PreTrainedTokenizerBase,
274289
) -> list[SpanPrediction]:
275290
torch = get_torch()
276291

277-
assert self._tokenizer is not None
278-
assert self._model is not None
279-
280292
sample_count = len(bodies)
281293
all_spans: list[list[CharSpan]] = [[] for _ in range(sample_count)]
282294
doc_scores: list[float] = [0.0] * sample_count
@@ -298,7 +310,7 @@ def _predict_overflowing(
298310
if key not in skip_keys
299311
}
300312
with torch.no_grad():
301-
outputs = self._model(**inputs)
313+
outputs = model(**inputs)
302314
logits = outputs.logits[0]
303315
probs = torch.softmax(logits, dim=-1)
304316
doc_logits = getattr(outputs, "doc_logits", None)

‎sdk/src/unplug/ml/store.py‎

Lines changed: 88 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import json
77
import os
88
import shutil
9+
import threading
10+
from collections.abc import Iterator
11+
from contextlib import contextmanager
912
from datetime import UTC, datetime
1013
from pathlib import Path
1114
from typing import Any
@@ -36,6 +39,29 @@
3639
]
3740

3841

42+
@contextmanager
43+
def _tier_download_lock(tier_dir: Path) -> Iterator[None]:
44+
"""Serialize checkpoint swaps for one tier across threads and processes."""
45+
tier_dir.mkdir(parents=True, exist_ok=True)
46+
lock_path = tier_dir / ".download.lock"
47+
with open(lock_path, "a+", encoding="utf-8") as lock_file:
48+
try:
49+
import fcntl
50+
51+
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
52+
except ImportError:
53+
pass
54+
try:
55+
yield
56+
finally:
57+
try:
58+
import fcntl
59+
60+
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
61+
except ImportError:
62+
pass
63+
64+
3965
def _config_digest(path: Path) -> str | None:
4066
config = path / "config.json"
4167
if not config.is_file():
@@ -109,7 +135,11 @@ def write_manifest(self, manifest: ModelManifest) -> None:
109135
def is_valid_checkpoint(self, path: Path) -> bool:
110136
if not path.is_dir() or not (path / "config.json").is_file():
111137
return False
112-
return any((path / name).is_file() for name in _WEIGHT_FILES)
138+
if any((path / name).is_file() for name in _WEIGHT_FILES):
139+
return True
140+
if (path / "model.safetensors.index.json").is_file():
141+
return any(path.glob("*.safetensors"))
142+
return any(path.glob("model-*-of-*.safetensors"))
113143

114144
def _installed_checkpoint(self, tier: str) -> Path | None:
115145
"""Return cached checkpoint when manifest + weights exist (revision may be stale)."""
@@ -166,57 +196,67 @@ def ensure_tier(
166196
from huggingface_hub import snapshot_download
167197

168198
dest = self.tier_dir(tier) / "checkpoint"
169-
temp_dest = self.tier_dir(tier) / f".checkpoint-download-{os.getpid()}"
170-
backup = self.tier_dir(tier) / f".checkpoint-{os.getpid()}.bak"
199+
staging_id = f"{os.getpid()}-{threading.get_ident()}"
200+
temp_dest = self.tier_dir(tier) / f".checkpoint-download-{staging_id}"
201+
backup = self.tier_dir(tier) / f".checkpoint-{staging_id}.bak"
171202

172-
if temp_dest.exists():
173-
shutil.rmtree(temp_dest)
174-
temp_dest.mkdir(parents=True, exist_ok=True)
175-
176-
moved_to_backup = False
177-
try:
178-
local_dir = snapshot_download(
179-
repo_id=tier_entry.repo_id,
180-
revision=tier_entry.revision,
181-
local_dir=str(temp_dest),
182-
allow_patterns=_SNAPSHOT_ALLOW_PATTERNS,
183-
)
184-
ckpt = Path(local_dir)
185-
if not self.is_valid_checkpoint(ckpt):
186-
msg = (
187-
f"Downloaded model at {ckpt} is missing config.json or weight files. "
188-
f"Check repo {tier_entry.repo_id!r} on Hugging Face."
203+
with _tier_download_lock(self.tier_dir(tier)):
204+
if temp_dest.exists():
205+
shutil.rmtree(temp_dest)
206+
temp_dest.mkdir(parents=True, exist_ok=True)
207+
208+
moved_to_backup = False
209+
manifest_written = False
210+
previous_manifest = self.read_manifest(tier)
211+
try:
212+
local_dir = snapshot_download(
213+
repo_id=tier_entry.repo_id,
214+
revision=tier_entry.revision,
215+
local_dir=str(temp_dest),
216+
allow_patterns=_SNAPSHOT_ALLOW_PATTERNS,
217+
)
218+
ckpt = Path(local_dir)
219+
if not self.is_valid_checkpoint(ckpt):
220+
msg = (
221+
f"Downloaded model at {ckpt} is missing config.json or weight files. "
222+
f"Check repo {tier_entry.repo_id!r} on Hugging Face."
223+
)
224+
raise ConfigError(msg)
225+
226+
manifest = ModelManifest(
227+
tier=tier,
228+
repo_id=tier_entry.repo_id,
229+
revision=tier_entry.revision,
230+
path=str(dest),
231+
config_digest=_config_digest(ckpt),
189232
)
190-
raise ConfigError(msg)
191-
192-
manifest = ModelManifest(
193-
tier=tier,
194-
repo_id=tier_entry.repo_id,
195-
revision=tier_entry.revision,
196-
path=str(dest),
197-
config_digest=_config_digest(ckpt),
198-
)
199233

200-
if backup.exists():
201-
shutil.rmtree(backup)
202-
if dest.exists():
203-
os.replace(dest, backup)
204-
moved_to_backup = True
205-
os.replace(ckpt, dest)
206-
self.write_manifest(manifest)
207-
if backup.exists():
208-
shutil.rmtree(backup)
209-
return dest
210-
except Exception:
211-
# Roll back to the last good checkpoint so a failed swap never
212-
# strands the tier without a model (manifest still matches it).
213-
if moved_to_backup and backup.exists():
234+
if backup.exists():
235+
shutil.rmtree(backup)
214236
if dest.exists():
215-
shutil.rmtree(dest)
216-
os.replace(backup, dest)
217-
if temp_dest.exists():
218-
shutil.rmtree(temp_dest)
219-
raise
237+
os.replace(dest, backup)
238+
moved_to_backup = True
239+
os.replace(ckpt, dest)
240+
self.write_manifest(manifest)
241+
manifest_written = True
242+
if backup.exists():
243+
shutil.rmtree(backup)
244+
return dest
245+
except Exception:
246+
# Roll back to the last good checkpoint so a failed swap never
247+
# strands the tier without a model (manifest still matches it).
248+
if moved_to_backup and backup.exists():
249+
if dest.exists():
250+
shutil.rmtree(dest)
251+
os.replace(backup, dest)
252+
if manifest_written:
253+
if previous_manifest is not None:
254+
self.write_manifest(previous_manifest)
255+
elif self.manifest_path(tier).is_file():
256+
self.manifest_path(tier).unlink()
257+
if temp_dest.exists():
258+
shutil.rmtree(temp_dest)
259+
raise
220260

221261
def list_status(self) -> list[dict[str, Any]]:
222262
"""Status for every catalog tier (installed / upgrade available)."""

‎sdk/tests/integration/test_integration_adapters.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,14 @@ def test_ag2_received_hook_blocks_injection_split_across_blocks(self) -> None:
408408
with pytest.raises(RuntimeError):
409409
hook(content)
410410

411+
def test_ag2_received_hook_scans_non_text_block_strings(self) -> None:
412+
hook = ag2_received_message_hook(AgentHooks(Guard()))
413+
content = [
414+
{"type": "image_url", "image_url": {"url": _INJECT}},
415+
]
416+
with pytest.raises(RuntimeError):
417+
hook(content)
418+
411419
def test_adk_extracts_function_response_text(self) -> None:
412420
# #53: a user turn carrying only a function_response part must be scanned,
413421
# not treated as empty text.

‎sdk/tests/unit/api/test_public_import_surface.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,14 @@ def test_public_ml_runtime_import_is_lazy() -> None:
220220
model = SpanInferenceModel(Path("/tmp/nonexistent-unplug-checkpoint"), device="cpu")
221221
assert model.loaded is False
222222
assert model.checkpoint.name == "nonexistent-unplug-checkpoint"
223+
assert model.device == "cpu"
224+
225+
226+
def test_public_ml_constructor_without_device_stays_lazy() -> None:
227+
from pathlib import Path
228+
229+
from unplug.api.ml import SpanInferenceModel
230+
231+
model = SpanInferenceModel(Path("/tmp/nonexistent-unplug-checkpoint"))
232+
assert model.loaded is False
233+
assert model.device == "auto"

0 commit comments

Comments
 (0)