shipwright-kit 0.6.0__py3-none-any.whl

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.
@@ -0,0 +1,3 @@
1
+ """Shipwright — design-token + tooling library."""
2
+
3
+ __version__ = "0.6.0"
shipwright_kit/cli.py ADDED
@@ -0,0 +1,36 @@
1
+ """Typer application factory for Shipwright CLI tools.
2
+
3
+ Thin, opinionated defaults so every scaffolded CLI is consistent
4
+ (``add_completion=False``, ``no_args_is_help=True``) plus an optional
5
+ ``version`` command. Imports Typer at module load, so this submodule is NOT
6
+ imported by ``shipwright_kit/__init__`` — ``import shipwright_kit`` stays
7
+ typer-free (import-light). Consumers that import ``shipwright_kit.cli`` already
8
+ depend on Typer.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import typer
14
+
15
+ __all__ = ["build_typer"]
16
+
17
+
18
+ def build_typer(name: str, help: str, *, version: str | None = None) -> typer.Typer:
19
+ """Return a Typer app with Shipwright's standard defaults. If ``version`` is
20
+ given, register a ``version`` command that prints it."""
21
+ app = typer.Typer(
22
+ name=name,
23
+ help=help,
24
+ add_completion=False,
25
+ no_args_is_help=True,
26
+ )
27
+ if version is not None:
28
+
29
+ @app.command()
30
+ def version_() -> None: # registered as "version"
31
+ """Print the version."""
32
+ typer.echo(version)
33
+
34
+ version_.__name__ = "version"
35
+
36
+ return app
@@ -0,0 +1,53 @@
1
+ """Import-light config mechanism shared by Shipwright CLI tools.
2
+
3
+ Owns the *mechanism* only — a secure per-tool app directory, ordered-candidate
4
+ config-file resolution, and a resolve->load->validate skeleton. The consumer
5
+ injects its own yaml ``loader`` and (pydantic) ``validator`` callables, so this
6
+ module stays stdlib-only and ``import shipwright_kit.config`` pulls no
7
+ pyyaml/pydantic/typer. Each tool keeps its own config *schema*, env-override
8
+ step, ``save_config`` and ``.env`` handling.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from collections.abc import Callable, Iterable
14
+ from pathlib import Path
15
+ from typing import TypeVar
16
+
17
+ __all__ = ["app_dir", "load_config", "resolve_config_file"]
18
+
19
+ T = TypeVar("T")
20
+
21
+ _OWNER_ONLY = 0o700
22
+
23
+
24
+ def app_dir(name: str, *, create: bool = False, mode: int = _OWNER_ONLY) -> Path:
25
+ """Return ``~/.{name}``. With ``create=True`` make it owner-only (mkdir + chmod,
26
+ so a pre-existing loose-permission dir is hardened too)."""
27
+ d = Path.home() / f".{name}"
28
+ if create:
29
+ d.mkdir(mode=mode, parents=True, exist_ok=True)
30
+ d.chmod(mode)
31
+ return d
32
+
33
+
34
+ def resolve_config_file(candidates: Iterable[Path | None]) -> Path | None:
35
+ """Return the first candidate that exists (``None`` entries skipped)."""
36
+ for c in candidates:
37
+ if c is not None and c.exists():
38
+ return c
39
+ return None
40
+
41
+
42
+ def load_config(
43
+ candidates: Iterable[Path | None],
44
+ *,
45
+ loader: Callable[[Path], dict],
46
+ validator: Callable[[dict], T],
47
+ ) -> T:
48
+ """Resolve the first existing candidate, ``loader`` it to a dict, and
49
+ ``validator`` that dict into a config object. If no candidate exists, the
50
+ loader is NOT called and ``validator({})`` is returned."""
51
+ path = resolve_config_file(candidates)
52
+ data = loader(path) if path is not None else {}
53
+ return validator(data)
@@ -0,0 +1,24 @@
1
+ """Design tokens: tiers, glyphs, palette, console, output."""
2
+
3
+ from .console import get_console, supports_color, supports_unicode
4
+ from .glyphs import glyph, tier_label
5
+ from .output import OUTPUT_SCHEMA_VERSION, VALID_FORMATS, Renderable, render
6
+ from .palette import ColorblindTheme, DefaultTheme, Theme
7
+ from .tiers import Severity, TierMappable
8
+
9
+ __all__ = [
10
+ "Severity",
11
+ "TierMappable",
12
+ "glyph",
13
+ "tier_label",
14
+ "Theme",
15
+ "DefaultTheme",
16
+ "ColorblindTheme",
17
+ "get_console",
18
+ "supports_color",
19
+ "supports_unicode",
20
+ "VALID_FORMATS",
21
+ "Renderable",
22
+ "render",
23
+ "OUTPUT_SCHEMA_VERSION",
24
+ ]
@@ -0,0 +1,21 @@
1
+ """Plain ASCII banner generator (dep-free). Optional figlet via the `banner`
2
+ extra is imported lazily, with a plain fallback if it is absent."""
3
+
4
+ from __future__ import annotations
5
+
6
+
7
+ def _figlet(name: str) -> str:
8
+ try:
9
+ from pyfiglet import figlet_format
10
+ except (ModuleNotFoundError, ImportError, TypeError):
11
+ return name.upper()
12
+ return figlet_format(name).rstrip("\n")
13
+
14
+
15
+ def make_banner(name: str, version: str, tagline: str = "", *, figlet: bool = False, ascii_only: bool = False) -> str:
16
+ """Return a banner string. Caller decides where to print it (typically stderr)."""
17
+ title = _figlet(name) if figlet else name.upper()
18
+ info = f"v{version}" + (f" | {tagline}" if tagline else "")
19
+ width = max([len(line) for line in title.splitlines()] + [len(info), 12])
20
+ rule = ("-" if ascii_only else "─") * width
21
+ return "\n".join([title, rule, info])
@@ -0,0 +1,33 @@
1
+ """Accessibility-aware console helpers. Rich is imported lazily inside
2
+ `get_console` so `import shipwright_kit` stays light."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import os
7
+ import sys
8
+
9
+
10
+ def supports_color(stream=None) -> bool:
11
+ stream = stream if stream is not None else sys.stdout
12
+ if os.environ.get("NO_COLOR"):
13
+ return False
14
+ isatty = getattr(stream, "isatty", None)
15
+ return bool(isatty and isatty())
16
+
17
+
18
+ def supports_unicode(stream=None) -> bool:
19
+ stream = stream if stream is not None else sys.stdout
20
+ enc = (getattr(stream, "encoding", "") or "").lower()
21
+ if "utf" in enc:
22
+ return True
23
+ return bool(os.environ.get("WT_SESSION") or os.environ.get("ANSICON"))
24
+
25
+
26
+ def get_console(*, no_color: bool = False, force_terminal: bool | None = None):
27
+ """Return a configured Rich Console (lazy import). Requires the `rich` extra."""
28
+ try:
29
+ from rich.console import Console
30
+ except ModuleNotFoundError as exc: # pragma: no cover
31
+ raise RuntimeError("rich output requires `pip install shipwright-kit[rich]`") from exc
32
+ use_color = supports_color() and not no_color
33
+ return Console(no_color=not use_color, force_terminal=force_terminal, safe_box=not supports_unicode())
@@ -0,0 +1,30 @@
1
+ """Per-tier glyphs with an ASCII fallback. Rule: a tier indicator always
2
+ renders symbol + label, never color alone (the #1 colorblind fix)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from .tiers import Severity
7
+
8
+ _UNICODE: dict[Severity, str] = {
9
+ Severity.OK: "✓",
10
+ Severity.INFO: "ℹ",
11
+ Severity.NOTICE: "•",
12
+ Severity.WARN: "⚠",
13
+ Severity.CRITICAL: "✗",
14
+ }
15
+ _ASCII: dict[Severity, str] = {
16
+ Severity.OK: "OK",
17
+ Severity.INFO: "i",
18
+ Severity.NOTICE: "*",
19
+ Severity.WARN: "!",
20
+ Severity.CRITICAL: "XX",
21
+ }
22
+
23
+
24
+ def glyph(tier: Severity, *, ascii_only: bool = False) -> str:
25
+ return (_ASCII if ascii_only else _UNICODE)[tier]
26
+
27
+
28
+ def tier_label(tier: Severity, *, ascii_only: bool = False) -> str:
29
+ """Symbol + label — never color/symbol alone."""
30
+ return f"{glyph(tier, ascii_only=ascii_only)} {tier.label}"
@@ -0,0 +1,60 @@
1
+ """Output-format contract. Core formats are stdlib-only; `rich` is lazy.
2
+ `console` is a distinct reduced-density plain formatter (not Rich-no-color)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import csv as _csv
7
+ import io
8
+ import json
9
+ from typing import Protocol, runtime_checkable
10
+
11
+ from .glyphs import tier_label
12
+ from .tiers import Severity
13
+
14
+ VALID_FORMATS = ("rich", "console", "json", "ndjson", "csv")
15
+
16
+ # render(fmt="json") envelope contract version (G10). Bump = structural break;
17
+ # update the golden test + a migration note (docs/release-policy.md).
18
+ OUTPUT_SCHEMA_VERSION = 1
19
+
20
+
21
+ @runtime_checkable
22
+ class Renderable(Protocol):
23
+ def rows(self) -> list[dict]: ...
24
+
25
+ def tier(self) -> Severity: ...
26
+
27
+
28
+ def render(obj: Renderable, fmt: str = "console", *, ascii_only: bool = False) -> str:
29
+ if fmt not in VALID_FORMATS:
30
+ raise ValueError(f"unknown format {fmt!r}; valid: {', '.join(VALID_FORMATS)}")
31
+ rows = list(obj.rows())
32
+ if fmt == "json":
33
+ return json.dumps({"schema_version": OUTPUT_SCHEMA_VERSION, "tier": obj.tier().label, "rows": rows}, indent=2)
34
+ if fmt == "ndjson":
35
+ return "\n".join(json.dumps(r) for r in rows)
36
+ if fmt == "csv":
37
+ if not rows:
38
+ return ""
39
+ buf = io.StringIO()
40
+ writer = _csv.DictWriter(buf, fieldnames=list(rows[0].keys()))
41
+ writer.writeheader()
42
+ writer.writerows(rows)
43
+ return buf.getvalue()
44
+ if fmt == "console":
45
+ head = tier_label(obj.tier(), ascii_only=ascii_only)
46
+ body = [" " + " ".join(f"{k}={v}" for k, v in r.items()) for r in rows]
47
+ return "\n".join([head, *body])
48
+ # fmt == "rich" — lazy import
49
+ from rich.console import Console
50
+ from rich.table import Table
51
+
52
+ table = Table()
53
+ if rows:
54
+ for key in rows[0]:
55
+ table.add_column(str(key))
56
+ for r in rows:
57
+ table.add_row(*[str(v) for v in r.values()])
58
+ buf = io.StringIO()
59
+ Console(file=buf, force_terminal=False, no_color=True).print(table)
60
+ return buf.getvalue()
@@ -0,0 +1,46 @@
1
+ """Themeable palette: maps a tier to a style token (a plain string the rich
2
+ renderer later consumes). Stdlib-only — no Rich import here."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from typing import Protocol, runtime_checkable
7
+
8
+ from .tiers import Severity
9
+
10
+
11
+ @runtime_checkable
12
+ class Theme(Protocol):
13
+ name: str
14
+
15
+ def style(self, tier: Severity) -> str: ...
16
+
17
+
18
+ class DefaultTheme:
19
+ name = "default"
20
+ _STYLES = {
21
+ Severity.OK: "green",
22
+ Severity.INFO: "cyan",
23
+ Severity.NOTICE: "blue",
24
+ Severity.WARN: "yellow",
25
+ Severity.CRITICAL: "bold red",
26
+ }
27
+
28
+ def style(self, tier: Severity) -> str:
29
+ return self._STYLES[tier]
30
+
31
+
32
+ class ColorblindTheme:
33
+ """Blue/orange-leaning, avoids red/green reliance (deuteranopia-safe).
34
+ Colour is never the sole signal — see glyphs.tier_label."""
35
+
36
+ name = "colorblind"
37
+ _STYLES = {
38
+ Severity.OK: "blue",
39
+ Severity.INFO: "cyan",
40
+ Severity.NOTICE: "white",
41
+ Severity.WARN: "yellow",
42
+ Severity.CRITICAL: "bold magenta",
43
+ }
44
+
45
+ def style(self, tier: Severity) -> str:
46
+ return self._STYLES[tier]
@@ -0,0 +1,33 @@
1
+ """Generic severity tiers + a domain-overlay protocol."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from enum import IntEnum
6
+ from typing import Protocol, runtime_checkable
7
+
8
+
9
+ class Severity(IntEnum):
10
+ """Generic 5-tier severity ladder. Tools map their own enums onto it."""
11
+
12
+ OK = 0
13
+ INFO = 1
14
+ NOTICE = 2
15
+ WARN = 3
16
+ CRITICAL = 4
17
+
18
+ @property
19
+ def label(self) -> str:
20
+ return self.name
21
+
22
+ @property
23
+ def role(self) -> str:
24
+ """Color-role key resolved by a Theme (see palette.py)."""
25
+ return self.name.lower()
26
+
27
+
28
+ @runtime_checkable
29
+ class TierMappable(Protocol):
30
+ """A domain enum maps to the base ladder via `base_tier()`. May map fewer
31
+ than 5 tiers; off-axis states map explicitly."""
32
+
33
+ def base_tier(self) -> Severity: ...
@@ -0,0 +1,7 @@
1
+ """Generic classification eval harness: corpus, metrics, evaluate + gate."""
2
+
3
+ from .corpus import Sample, load_corpus
4
+ from .harness import EvalGateError, evaluate, gate
5
+ from .metrics import EVAL_SCHEMA_VERSION, EvalResult
6
+
7
+ __all__ = ["Sample", "load_corpus", "EvalResult", "EVAL_SCHEMA_VERSION", "EvalGateError", "evaluate", "gate"]
@@ -0,0 +1,34 @@
1
+ """Labeled-sample corpus loader (CSV/JSON). Trusted local fixture data → STRICT:
2
+ a malformed row raises ValueError (unlike liberal external-API parsing)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import csv
7
+ import json
8
+ from dataclasses import dataclass
9
+ from pathlib import Path
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class Sample:
14
+ input: str
15
+ label: str
16
+
17
+
18
+ def _make(row: dict, input_col: str, label_col: str, idx: int) -> Sample:
19
+ try:
20
+ inp, lab = row[input_col], row[label_col]
21
+ except (KeyError, TypeError) as exc:
22
+ raise ValueError(f"corpus row {idx}: missing column {exc}") from exc
23
+ if not inp or not lab:
24
+ raise ValueError(f"corpus row {idx}: empty {input_col!r}/{label_col!r}")
25
+ return Sample(str(inp), str(lab))
26
+
27
+
28
+ def load_corpus(path, *, input_col: str = "input", label_col: str = "label") -> list[Sample]:
29
+ p = Path(path)
30
+ if p.suffix == ".json":
31
+ data = json.loads(p.read_text())
32
+ return [_make(d, input_col, label_col, i) for i, d in enumerate(data)]
33
+ lines = [ln for ln in p.read_text().splitlines() if ln.strip() and not ln.lstrip().startswith("#")]
34
+ return [_make(row, input_col, label_col, i) for i, row in enumerate(csv.DictReader(lines))]
@@ -0,0 +1,50 @@
1
+ """Run a predict function over a corpus and gate the result. Count-and-skip on a
2
+ predict-time exception (faithful to barb — a bad row must not abort the run)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from collections.abc import Callable
7
+
8
+ from .corpus import Sample
9
+ from .metrics import EvalResult
10
+
11
+
12
+ class EvalGateError(AssertionError):
13
+ """Raised when an eval result is below the required thresholds."""
14
+
15
+
16
+ def evaluate(
17
+ predict_fn: Callable[[str], str],
18
+ corpus: list[Sample],
19
+ *,
20
+ positive_pred: Callable[[str], bool],
21
+ positive_expected: Callable[[str], bool] | None = None,
22
+ ) -> EvalResult:
23
+ binarize_expected = positive_expected or positive_pred # default = same-space (Phase B)
24
+ tp = fp = tn = fn = errors = 0
25
+ for sample in corpus:
26
+ try:
27
+ pred = predict_fn(sample.input)
28
+ except Exception: # count-and-skip, surfaced via errors
29
+ errors += 1
30
+ continue
31
+ exp = binarize_expected(sample.label)
32
+ got = positive_pred(pred)
33
+ if exp and got:
34
+ tp += 1
35
+ elif got and not exp:
36
+ fp += 1
37
+ elif exp and not got:
38
+ fn += 1
39
+ else:
40
+ tn += 1
41
+ return EvalResult(tp, fp, tn, fn, errors)
42
+
43
+
44
+ def gate(result: EvalResult, *, min_precision: float, min_recall: float) -> None:
45
+ if result.precision < min_precision:
46
+ raise EvalGateError(f"precision {result.precision:.3f} < {min_precision}")
47
+ if result.recall < min_recall:
48
+ raise EvalGateError(f"recall {result.recall:.3f} < {min_recall}")
49
+ if (result.tp + result.fn) > 0 and result.recall == 0.0:
50
+ raise EvalGateError("zero recall with positives present")
@@ -0,0 +1,66 @@
1
+ """Binary-classification metrics, faithful to barb's EvalMetrics. Stdlib-only."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ # Serialized EvalResult contract version (G10). Bump = structural break to the
8
+ # `to_dict()` shape; requires updating the golden test + a migration note (see
9
+ # docs/release-policy.md). Independent of the package version.
10
+ EVAL_SCHEMA_VERSION = 1
11
+
12
+
13
+ def _safe_div(num: float, den: float) -> float:
14
+ return num / den if den else 0.0
15
+
16
+
17
+ @dataclass(frozen=True)
18
+ class EvalResult:
19
+ tp: int = 0
20
+ fp: int = 0
21
+ tn: int = 0
22
+ fn: int = 0
23
+ errors: int = 0 # predict-time failures (count-and-skip)
24
+
25
+ @property
26
+ def precision(self) -> float:
27
+ return _safe_div(self.tp, self.tp + self.fp)
28
+
29
+ @property
30
+ def recall(self) -> float:
31
+ return _safe_div(self.tp, self.tp + self.fn)
32
+
33
+ @property
34
+ def f1(self) -> float:
35
+ p, r = self.precision, self.recall
36
+ return _safe_div(2 * p * r, p + r)
37
+
38
+ @property
39
+ def accuracy(self) -> float:
40
+ return _safe_div(self.tp + self.tn, self.tp + self.fp + self.tn + self.fn)
41
+
42
+ @property
43
+ def false_positive_rate(self) -> float:
44
+ return _safe_div(self.fp, self.fp + self.tn)
45
+
46
+ @property
47
+ def confusion(self) -> dict[str, int]:
48
+ return {"tp": self.tp, "fp": self.fp, "tn": self.tn, "fn": self.fn}
49
+
50
+ def to_dict(self) -> dict[str, int | float]:
51
+ """Canonical, versioned serialization (schema v1). RAW floats — consumers
52
+ round for display. `confusion` is omitted (it is just tp/fp/tn/fn, already
53
+ top-level keys)."""
54
+ return {
55
+ "schema_version": EVAL_SCHEMA_VERSION,
56
+ "tp": self.tp,
57
+ "fp": self.fp,
58
+ "tn": self.tn,
59
+ "fn": self.fn,
60
+ "errors": self.errors,
61
+ "precision": self.precision,
62
+ "recall": self.recall,
63
+ "f1": self.f1,
64
+ "accuracy": self.accuracy,
65
+ "false_positive_rate": self.false_positive_rate,
66
+ }
File without changes
@@ -0,0 +1,17 @@
1
+ """Security pack: threat-verdict theme + labels + shared injection defense."""
2
+
3
+ from shipwright_kit.security.injection import (
4
+ INJECTION_PATTERNS_VERSION,
5
+ InjectionFinding,
6
+ PromptInjectionDetector,
7
+ SeverityLevel,
8
+ scan,
9
+ )
10
+
11
+ __all__ = [
12
+ "INJECTION_PATTERNS_VERSION",
13
+ "InjectionFinding",
14
+ "PromptInjectionDetector",
15
+ "SeverityLevel",
16
+ "scan",
17
+ ]
@@ -0,0 +1,26 @@
1
+ """Security-pack eval helpers: threat binarization + barb's baseline thresholds
2
+ + a per-tier breakdown. Stdlib-only (no rich import)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from collections import Counter
7
+
8
+ from shipwright_kit.design.tiers import Severity
9
+
10
+ SECURITY_MIN_PRECISION = 1.0
11
+ SECURITY_MIN_RECALL = 0.70
12
+
13
+
14
+ def is_alert(tier: Severity, *, alert_at: Severity = Severity.NOTICE) -> bool:
15
+ """A verdict at/above the alert threshold counts as a positive prediction.
16
+ Default NOTICE reproduces barb's positive set {SUSPICIOUS, HIGH_RISK, PHISHING}."""
17
+ return tier >= alert_at
18
+
19
+
20
+ def tier_breakdown(items: list[tuple[Severity, str]]) -> str:
21
+ """Plain-text benign-vs-malicious counts per tier (no rich → import-light)."""
22
+ counts: Counter = Counter(items)
23
+ lines = ["tier | label | count"]
24
+ for (tier, label), n in sorted(counts.items(), key=lambda kv: (int(kv[0][0]), kv[0][1])):
25
+ lines.append(f"{tier.label} | {label} | {n}")
26
+ return "\n".join(lines)
@@ -0,0 +1,206 @@
1
+ """Shared prompt-injection detector for attacker-influenced strings.
2
+
3
+ Single source of truth for the regex pattern set + detection engine used by
4
+ every consumer (vex, sift). A bypass fixed here propagates to all of them.
5
+
6
+ Import-light: stdlib only (no pydantic), so ``import
7
+ shipwright_kit.security.injection`` stays dependency-free. The finding type is a
8
+ frozen dataclass; consumers serialize via ``dataclasses.asdict`` at their JSON
9
+ boundary.
10
+
11
+ Operates on plain string values. Tool-specific I/O — vex's ``sanitize`` (str ->
12
+ marker) and sift's ``redact_alert`` (Alert -> Alert) — stays in each tool.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import re
18
+ import unicodedata
19
+ from dataclasses import dataclass
20
+ from enum import Enum
21
+
22
+ __all__ = [
23
+ "INJECTION_PATTERNS_VERSION",
24
+ "InjectionFinding",
25
+ "PromptInjectionDetector",
26
+ "SeverityLevel",
27
+ "scan",
28
+ ]
29
+
30
+ # Bump when the pattern SET changes (added/removed/retuned pattern). Lets
31
+ # consumers assert they are matching against a known engine version, mirroring
32
+ # the EVAL_SCHEMA_VERSION / OUTPUT_SCHEMA_VERSION contract discipline (G10).
33
+ INJECTION_PATTERNS_VERSION = 1
34
+
35
+
36
+ class SeverityLevel(str, Enum):
37
+ """Severity of an injection finding."""
38
+
39
+ WARNING = "WARNING"
40
+ CRITICAL = "CRITICAL"
41
+
42
+
43
+ @dataclass(frozen=True)
44
+ class InjectionFinding:
45
+ """A detected injection pattern in a string value.
46
+
47
+ Attribute names match the prior pydantic models in vex/sift so consumer
48
+ call sites and ``.field`` / ``.pattern_type`` / ``.severity`` access are
49
+ unchanged.
50
+ """
51
+
52
+ field: str
53
+ pattern_type: str
54
+ severity: SeverityLevel
55
+ redaction: str
56
+ value_preview: str | None = None
57
+
58
+
59
+ class PromptInjectionDetector:
60
+ """Detects prompt-injection patterns in plain string values.
61
+
62
+ Args:
63
+ case_insensitive: case-insensitive matching when True (default).
64
+ whitelist_patterns: regex strings; a value matching any one is exempt
65
+ from all checks (operator-defined known-safe templates).
66
+ """
67
+
68
+ def __init__(
69
+ self,
70
+ case_insensitive: bool = True,
71
+ whitelist_patterns: list[str] | None = None,
72
+ ) -> None:
73
+ self.case_insensitive = case_insensitive
74
+ flags = re.IGNORECASE if case_insensitive else 0
75
+ self._whitelist: list[re.Pattern[str]] = [re.compile(p, flags) for p in (whitelist_patterns or [])]
76
+ self._compile_patterns()
77
+
78
+ def _compile_patterns(self) -> None:
79
+ flags = re.IGNORECASE if self.case_insensitive else 0
80
+
81
+ # Pattern 1: "ignore previous instructions" variants.
82
+ self.pattern_ignore_instructions = re.compile(
83
+ r"(ignore|disregard|forget|dismiss|bypass|override)[\s\S]{0,40}?"
84
+ r"(previous|prior|earlier|above|preceding)[\s\S]{0,40}?"
85
+ r"(instruction|directive|prompt|command|context|system)",
86
+ flags | re.DOTALL,
87
+ )
88
+
89
+ # Pattern 2: LLM-redirection via "instead" / "rather than".
90
+ self.pattern_instead_output = re.compile(
91
+ r"(?:"
92
+ r"(output|respond|return|generate|create|print|write)\s+instead"
93
+ r"|instead[\s,;.]+(?:of\s+)?(output|respond|return|generate|create|print|write)"
94
+ r"|rather\s+than\s+(?:summariz|analyz|triag|the\s+above)"
95
+ r")",
96
+ flags,
97
+ )
98
+
99
+ # Pattern 3: JSON escape sequences.
100
+ self.pattern_json_escapes = re.compile(
101
+ r'\\(?:["\\/bfnrtu]|u[0-9a-fA-F]{4})',
102
+ flags,
103
+ )
104
+
105
+ # Pattern 4: Base64 / hex encoded payloads (thresholds tuned to exclude
106
+ # common security terms like "Exfiltration"/"Configuration").
107
+ self.pattern_base64_hex = re.compile(
108
+ r"(?:"
109
+ r"(?=[A-Za-z0-9+/]*[+/])[A-Za-z0-9+/]{12,}"
110
+ r"|[A-Za-z0-9+/]{4,}=="
111
+ r"|[A-Za-z0-9+/]{8,}="
112
+ r"|(?:[0-9a-fA-F]{2}){10,}"
113
+ r"|[A-Za-z0-9]{20,}"
114
+ r")",
115
+ flags,
116
+ )
117
+
118
+ # Pattern 5: Shell command injection.
119
+ self.pattern_shell_commands = re.compile(
120
+ r"(?:\$\([^)]*\)|`[^`]*`|\$\w+)",
121
+ flags,
122
+ )
123
+
124
+ # Pattern 6: Jailbreak / role override — role verb + (restriction
125
+ # adjective bound to an AI-context noun | known idiom). Bare markers are
126
+ # rejected to avoid FP on real SOC text.
127
+ self.pattern_jailbreak = re.compile(
128
+ r"(?:act as|you are now|pretend to be|roleplay as|behave as)[\s\S]{0,40}?"
129
+ r"(?:"
130
+ r"(?:unrestricted|unfiltered|jailbroken|uncensored|unaligned)\s+"
131
+ r"(?:assistant|ai|model|chatbot|llm|gpt|bot|agent|persona|mode|version)"
132
+ r"|jailbroken"
133
+ r"|dan\b"
134
+ r"|do\s+anything\s+now"
135
+ r")",
136
+ flags | re.DOTALL,
137
+ )
138
+
139
+ # Pattern 7: System-prompt exfiltration — exfil verb + high-signal prompt
140
+ # noun only (system prompt / your [system] prompt / system instructions).
141
+ self.pattern_prompt_exfil = re.compile(
142
+ r"(?:reveal|print|show|output|repeat|leak|disclose|dump|expose|contents?\s+of)"
143
+ r"[\s\S]{0,40}?"
144
+ r"(?:system\s*prompt|system\s+instructions?"
145
+ r"|your\s+(?:system\s+)?prompt|your\s+system\s+instructions?)",
146
+ flags | re.DOTALL,
147
+ )
148
+
149
+ def detect(
150
+ self,
151
+ value: str,
152
+ field_name: str = "",
153
+ *,
154
+ is_ioc_field: bool = False,
155
+ ) -> list[InjectionFinding]:
156
+ """Scan a single string value. Empty list means clean."""
157
+ if not isinstance(value, str):
158
+ return []
159
+
160
+ normalized = unicodedata.normalize("NFKC", value)
161
+
162
+ if self._whitelist and any(p.search(normalized) for p in self._whitelist):
163
+ return []
164
+
165
+ findings: list[InjectionFinding] = []
166
+ preview = self._truncate(value)
167
+
168
+ def add(pattern_type: str, severity: SeverityLevel, redaction: str) -> None:
169
+ findings.append(
170
+ InjectionFinding(
171
+ field=field_name,
172
+ pattern_type=pattern_type,
173
+ severity=severity,
174
+ redaction=redaction,
175
+ value_preview=preview,
176
+ )
177
+ )
178
+
179
+ # if (not elif) so all patterns in a value are reported.
180
+ if self.pattern_ignore_instructions.search(normalized):
181
+ add("instruction_override", SeverityLevel.CRITICAL, "[REDACTED: instruction override attempt]")
182
+ if self.pattern_instead_output.search(normalized):
183
+ add("output_manipulation", SeverityLevel.CRITICAL, "[REDACTED: output manipulation attempt]")
184
+ if self.pattern_json_escapes.search(normalized):
185
+ add("json_escape_sequence", SeverityLevel.WARNING, "[REDACTED: JSON escape sequences]")
186
+ if not is_ioc_field and self.pattern_base64_hex.search(normalized):
187
+ add("encoded_payload", SeverityLevel.WARNING, "[REDACTED: encoded payload]")
188
+ if self.pattern_shell_commands.search(normalized):
189
+ add("shell_injection", SeverityLevel.CRITICAL, "[REDACTED: shell command attempt]")
190
+ if self.pattern_jailbreak.search(normalized):
191
+ add("jailbreak", SeverityLevel.CRITICAL, "[REDACTED: jailbreak / role-override attempt]")
192
+ if self.pattern_prompt_exfil.search(normalized):
193
+ add("prompt_exfiltration", SeverityLevel.CRITICAL, "[REDACTED: system-prompt exfiltration attempt]")
194
+
195
+ return findings
196
+
197
+ @staticmethod
198
+ def _truncate(value: str, max_len: int = 80) -> str:
199
+ if len(value) <= max_len:
200
+ return value
201
+ return value[:max_len] + "..."
202
+
203
+
204
+ def scan(value: str) -> list[InjectionFinding]:
205
+ """Scan a single string value with a default detector."""
206
+ return PromptInjectionDetector().detect(value)
@@ -0,0 +1,34 @@
1
+ """Security pack default theme — maps the generic Severity tiers to threat
2
+ verdict labels (CLEAN→MALICIOUS) and a green→red palette. Tools keep their own
3
+ verdict enums and map them to Severity via TierMappable (see tests for barb/vex
4
+ worked examples). Stdlib-only."""
5
+
6
+ from __future__ import annotations
7
+
8
+ from shipwright_kit.design.tiers import Severity
9
+
10
+ SECURITY_LABELS: dict[Severity, str] = {
11
+ Severity.OK: "CLEAN",
12
+ Severity.INFO: "LOW",
13
+ Severity.NOTICE: "SUSPICIOUS",
14
+ Severity.WARN: "HIGH",
15
+ Severity.CRITICAL: "MALICIOUS",
16
+ }
17
+
18
+
19
+ class SecurityTheme:
20
+ name = "security"
21
+ _STYLES = {
22
+ Severity.OK: "green",
23
+ Severity.INFO: "cyan",
24
+ Severity.NOTICE: "yellow",
25
+ Severity.WARN: "dark_orange",
26
+ Severity.CRITICAL: "bold red",
27
+ }
28
+
29
+ def style(self, tier: Severity) -> str:
30
+ return self._STYLES[tier]
31
+
32
+
33
+ def label(tier: Severity) -> str:
34
+ return SECURITY_LABELS[tier]
@@ -0,0 +1,164 @@
1
+ Metadata-Version: 2.4
2
+ Name: shipwright-kit
3
+ Version: 0.6.0
4
+ Summary: Shipwright — AI-agent dev framework + import-light design/eval/security library
5
+ Author: Christian Huhn
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/duathron/shipwright
8
+ Project-URL: Repository, https://github.com/duathron/shipwright
9
+ Project-URL: Documentation, https://github.com/duathron/shipwright/blob/main/docs/library.md
10
+ Project-URL: Changelog, https://github.com/duathron/shipwright/blob/main/CHANGELOG.md
11
+ Project-URL: Issues, https://github.com/duathron/shipwright/issues
12
+ Keywords: ci,dev-tooling,eval,design-tokens,severity,quality-gates
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Software Development :: Quality Assurance
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.11
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Provides-Extra: rich
25
+ Requires-Dist: rich>=13.7; extra == "rich"
26
+ Provides-Extra: banner
27
+ Requires-Dist: pyfiglet>=1.0; extra == "banner"
28
+ Dynamic: license-file
29
+
30
+ # Shipwright
31
+
32
+ ![CI](https://github.com/duathron/shipwright/actions/workflows/ci.yml/badge.svg)
33
+ ![CodeQL](https://github.com/duathron/shipwright/actions/workflows/codeql.yml/badge.svg)
34
+ ![Coverage](https://img.shields.io/badge/coverage-%E2%89%A590%25-brightgreen.svg)
35
+ ![Types](https://img.shields.io/badge/types-mypy-blue.svg)
36
+ ![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)
37
+ ![Python](https://img.shields.io/badge/python-3.11%2B-blue.svg)
38
+
39
+ Shipwright is two things that share one repo:
40
+
41
+ - an installable, **import-light Python library** of shared dev-tooling runtime —
42
+ `shipwright_kit.design` (severity tiers + accessible output), `shipwright_kit.eval`
43
+ (detection-quality eval harness), `shipwright_kit.security` (a security pack);
44
+ - an **AI-agent-operated development framework** — reusable CI/CD, a Copier
45
+ scaffolder, quality gates, and bundled agent skills + personas — that **dogfoods**
46
+ the library and the gates it hands to the projects built with it.
47
+
48
+ The library is consumed today by two real tools: **barb** and **sift** both import
49
+ `shipwright_kit.eval` to run their detection-quality gates.
50
+
51
+ ## Install
52
+
53
+ The library is **not on PyPI** — the bare name `shipwright` belongs to an unrelated
54
+ project, so the published distribution is **`shipwright-kit`** and the import name is
55
+ **`shipwright_kit`**. For now, install from git:
56
+
57
+ ```bash
58
+ uv pip install "git+https://github.com/duathron/shipwright@main"
59
+ # then: import shipwright_kit
60
+ ```
61
+
62
+ > [!NOTE]
63
+ > Pin a release tag instead of `@main` for reproducible builds once a tagged
64
+ > release of the `shipwright-kit` distribution is cut. Do **not** `pip install
65
+ > shipwright` from PyPI — that is a different, unrelated package.
66
+
67
+ The security pack needs no extra — it ships with the base install and registers
68
+ through the `shipwright_kit.packs` entry point.
69
+
70
+ ## Library quickstart
71
+
72
+ **Run an eval gate** — score a classifier against a labeled corpus and fail if it
73
+ misses a floor (the exact pattern barb and sift use):
74
+
75
+ ```python
76
+ from shipwright_kit.eval import Sample, evaluate, gate
77
+
78
+ corpus = [Sample("phish-login", "phishing"),
79
+ Sample("example.com", "benign"),
80
+ Sample("secure-phish", "phishing")]
81
+
82
+ result = evaluate(
83
+ lambda text: "phishing" if "phish" in text else "benign",
84
+ corpus,
85
+ positive_pred=lambda pred: pred == "phishing",
86
+ positive_expected=lambda label: label == "phishing",
87
+ )
88
+ print(result.precision, result.recall) # 1.0 1.0
89
+ gate(result, min_precision=1.0, min_recall=0.9) # raises EvalGateError if below
90
+ ```
91
+
92
+ **Use the shared severity tiers** — one generic scale tools map their own verdicts
93
+ onto, with accessible (Unicode-or-ASCII) labels:
94
+
95
+ ```python
96
+ from shipwright_kit.design import Severity, tier_label
97
+
98
+ Severity.OK, Severity.INFO, Severity.NOTICE, Severity.WARN, Severity.CRITICAL # IntEnum 0..4
99
+ print(tier_label(Severity.CRITICAL)) # ✗ CRITICAL
100
+ print(tier_label(Severity.OK)) # ✓ OK
101
+ ```
102
+
103
+ `import shipwright_kit` pulls in no `rich` or `pyfiglet` — the heavy deps load lazily only
104
+ when you actually render. Full API: **[docs/library.md](docs/library.md)**.
105
+
106
+ ## The framework
107
+
108
+ The repo contains **no project code**. Projects stay their own Git repositories and
109
+ their own packages; locally you clone each into the **gitignored** `projects/`
110
+ directory, where a [uv](https://docs.astral.sh/uv/) workspace ties them together for
111
+ development:
112
+
113
+ ```
114
+ shipwright/
115
+ ├─ shipwright_kit/ # the importable library (design / eval / security)
116
+ ├─ tooling/ruff-base.toml # single source of truth for lint rules
117
+ ├─ templates/ # Copier scaffolder (python-cli) + release config
118
+ ├─ skills/ · personas/ # the agent operating layer (scaffold, onboard, review …)
119
+ ├─ .github/workflows/ # reusable python-ci.yml + python-release.yml (SHA-pinned)
120
+ └─ projects/ # GITIGNORED, local-only — your projects plug in here
121
+ ```
122
+
123
+ Work is promoted through gates; failing a rung blocks promotion:
124
+
125
+ ```
126
+ commit → lint + unit (auto) → build → dogfood + eval (auto) →
127
+ QM gate (manual) → beta sign-off (manual) → release
128
+ ```
129
+
130
+ The reusable CI/CD that wires these gates, the Copier scaffolder (`templates/`), and
131
+ the agent skills (`skills/`) and personas (`personas/`) all ship now. This repo runs
132
+ the exact gates it gives the projects built with it.
133
+
134
+ ## Framework quickstart
135
+
136
+ Requires Python 3.11+, [uv](https://docs.astral.sh/uv/), and
137
+ [just](https://github.com/casey/just).
138
+
139
+ ```bash
140
+ uv sync --dev # create the dev environment
141
+ uv run pre-commit install # install the local gate
142
+ just lint # ruff check + format-check
143
+ just test # pytest
144
+ ```
145
+
146
+ The local `pre-commit` gate runs the same lint/format/secret checks as CI; add
147
+ `just test` (and `uv build`) for the test and build rungs CI also enforces.
148
+
149
+ ## Docs
150
+
151
+ - **[docs/library.md](docs/library.md)** — per-module API reference (design / eval / security)
152
+ - **[docs/release-policy.md](docs/release-policy.md)** — SemVer + release policy
153
+ - **[docs/ci-cd.md](docs/ci-cd.md)** — the reusable CI/CD workflows
154
+ - **[CHANGELOG.md](CHANGELOG.md)**
155
+
156
+ ## Security
157
+
158
+ Report vulnerabilities privately via GitHub's
159
+ [private vulnerability reporting](https://github.com/duathron/shipwright/security/advisories/new)
160
+ (repo **Security** tab → **Report a vulnerability**). See [SECURITY.md](SECURITY.md).
161
+
162
+ ## License
163
+
164
+ [MIT](LICENSE) © 2026 Christian Huhn
@@ -0,0 +1,25 @@
1
+ shipwright_kit/__init__.py,sha256=crYIU7Ldr3xF7iH10pVwuW3gaaLVuTo3Vtsc_D5cdKU,76
2
+ shipwright_kit/cli.py,sha256=IlqKFJ5pPvl0OM9WYR9sSRML4BTwE3uNLaC6EEAEZ2w,1112
3
+ shipwright_kit/config.py,sha256=PjaX7jI1piPawCXERqwK8JOBgLS3eFYJWXzuiCWHyL4,1848
4
+ shipwright_kit/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ shipwright_kit/design/__init__.py,sha256=Rf0rRPczgfowaQJWQLGrNVk24Gh-ayNyCaEGnx2Sh30,630
6
+ shipwright_kit/design/banner.py,sha256=tvV8_CCWpjQWr8GH2HLrDpsF23CONEbkaPcgJ9y0RyA,892
7
+ shipwright_kit/design/console.py,sha256=uB_l73JY5mAecoGJrP-d1u0-CKac9J49fbBuR0DHFu4,1230
8
+ shipwright_kit/design/glyphs.py,sha256=lBBVDjn9tteHwxeUZ1WxCrjndYuvorDgNgRSPT9gy5A,847
9
+ shipwright_kit/design/output.py,sha256=Y3UPJR1CNjvKV13NepI9G4l9ZE_FcYNsaIRFqGFZS1E,2025
10
+ shipwright_kit/design/palette.py,sha256=RMHHqafwGPysdZ1RYQya5S3L9rAN42NWZ-W6AzoQtV4,1134
11
+ shipwright_kit/design/tiers.py,sha256=Hob_a97QvWxVBj_v3rQ0qXS4GKRpCrO66V1Y-RxwTKc,775
12
+ shipwright_kit/eval/__init__.py,sha256=cNK4usAQVam19nFB8LSgLJWd9ma9KkEgqlm5Sq-GjZI,333
13
+ shipwright_kit/eval/corpus.py,sha256=UGQBR0tcw0R94Ai-IHB_eAcZq-MdJ7waPO9yaalCopA,1210
14
+ shipwright_kit/eval/harness.py,sha256=XmnD3kcuEsbR1VYpx_cSYqkHCnczhsQ1QCMmgrIp4g4,1683
15
+ shipwright_kit/eval/metrics.py,sha256=GpS0aioWDOx5XPDSCcj9aL1yphm3qkZo_Qeuyy3qgnc,2031
16
+ shipwright_kit/security/__init__.py,sha256=MWwHA7CcZgjqb9PC9-sjPneQbywe7Q6cHQvF-FtyTvo,379
17
+ shipwright_kit/security/eval.py,sha256=QhSkCTaeYjZDRlkRneE-ASIspMZWxinKlzp4oAVUlLU,988
18
+ shipwright_kit/security/injection.py,sha256=MJT4fwqqEQU56yJRhcYVb9wFo8raYGiyIL5N0OEEOes,7772
19
+ shipwright_kit/security/theme.py,sha256=gJItxCwRhshuSgCoePqazAcXGK4s78qMnFcT6Jd95rI,942
20
+ shipwright_kit-0.6.0.dist-info/licenses/LICENSE,sha256=BVf5pqest078hZ5byAbwbvGWuPUYGwdeNp7gnRaJebU,1071
21
+ shipwright_kit-0.6.0.dist-info/METADATA,sha256=eb8elL21kepIQ9A2zl0eWLt9NYhJ_Qbs9oqz4fvu124,6724
22
+ shipwright_kit-0.6.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
23
+ shipwright_kit-0.6.0.dist-info/entry_points.txt,sha256=AK97I8ybvBfmqBuhHbHWjiCZB1aRdVQbiGIuhaQuvuw,78
24
+ shipwright_kit-0.6.0.dist-info/top_level.txt,sha256=IeyPm8RS7fJO8sk7Z26EsSiPhWlnmwxm3f1KSTsxsaQ,15
25
+ shipwright_kit-0.6.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [shipwright_kit.packs]
2
+ security = shipwright_kit.security.theme:SecurityTheme
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Christian Huhn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ shipwright_kit