@withgateway/sdk 0.2.3 → 0.2.5

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.
Files changed (62) hide show
  1. package/dist/cli.js +13267 -5869
  2. package/dist/cli.js.map +1 -1
  3. package/dist/cli.mjs +13272 -5874
  4. package/dist/cli.mjs.map +1 -1
  5. package/dist/files/index.js +83 -31
  6. package/dist/files/index.js.map +1 -1
  7. package/dist/files/index.mjs +79 -28
  8. package/dist/files/index.mjs.map +1 -1
  9. package/dist/hooks/index.d.mts +1 -0
  10. package/dist/hooks/index.d.ts +1 -0
  11. package/dist/hooks/index.js +4355 -0
  12. package/dist/hooks/index.js.map +1 -0
  13. package/dist/hooks/index.mjs +4243 -0
  14. package/dist/hooks/index.mjs.map +1 -0
  15. package/dist/{index-Dpo-t8iO.d.ts → index-1xnxt0Cc.d.ts} +58 -2
  16. package/dist/index-B0R3IgsV.d.mts +1279 -0
  17. package/dist/index-B0R3IgsV.d.ts +1279 -0
  18. package/dist/{index-B5HUQQDE.d.mts → index-C1wDEwH4.d.mts} +58 -2
  19. package/dist/index.d.mts +4 -3
  20. package/dist/index.d.ts +4 -3
  21. package/dist/index.js +6698 -1606
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +6793 -1713
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/traces/index.js +72 -20
  26. package/dist/traces/index.js.map +1 -1
  27. package/dist/traces/index.mjs +68 -17
  28. package/dist/traces/index.mjs.map +1 -1
  29. package/dist/tracing/index.d.mts +1 -1
  30. package/dist/tracing/index.d.ts +1 -1
  31. package/dist/tracing/index.js +47 -3
  32. package/dist/tracing/index.js.map +1 -1
  33. package/dist/tracing/index.mjs +42 -2
  34. package/dist/tracing/index.mjs.map +1 -1
  35. package/dist/worlds/index.d.mts +390 -27
  36. package/dist/worlds/index.d.ts +390 -27
  37. package/dist/worlds/index.js +2291 -366
  38. package/dist/worlds/index.js.map +1 -1
  39. package/dist/worlds/index.mjs +2281 -356
  40. package/dist/worlds/index.mjs.map +1 -1
  41. package/package.json +7 -1
  42. package/runtime/gatewaysdk/environment/_bundle.py +14 -24
  43. package/runtime/gatewaysdk/environment/_materialize.py +3 -3
  44. package/runtime/gatewaysdk/environment/schema/api.py +153 -20
  45. package/runtime/gatewaysdk/environment/schema/compiler.py +14 -0
  46. package/runtime/gatewaysdk/environment/schema/data.py +21 -19
  47. package/runtime/gatewaysdk/environment/schema/grading.py +225 -0
  48. package/runtime/gatewaysdk/environment/schema/host.py +171 -535
  49. package/runtime/gatewaysdk/environment/schema/ingest.py +3 -11
  50. package/runtime/gatewaysdk/environment/schema/local.py +32 -126
  51. package/runtime/gatewaysdk/environment/schema/platform.py +14 -22
  52. package/runtime/gatewaysdk/environment/schema/proof.py +237 -0
  53. package/runtime/gatewaysdk/environment/schema/runtime.py +686 -0
  54. package/runtime/gatewaysdk/environment/schema/skill.py +1 -0
  55. package/runtime/gatewaysdk/environment/schema/skills/hooks-getting-started/SKILL.md +156 -0
  56. package/runtime/gatewaysdk/environment/schema/snapshot.py +6 -5
  57. package/runtime/gatewaysdk/environment/schema/tasks.py +12 -2
  58. package/runtime/gatewaysdk/environment/schema/tools_world.py +75 -23
  59. package/runtime/gatewaysdk/environment/schema/verbs.py +136 -0
  60. package/runtime/gatewaysdk/environment/schema/workers.py +44 -13
  61. package/runtime/gatewaysdk/environment/world.py +391 -0
  62. package/skills/hooks-getting-started/SKILL.md +156 -0
@@ -0,0 +1,391 @@
1
+ """What a tree on disk IS, decided once.
2
+
3
+ ``World.from_tree`` is the one Python detector: the local runtime
4
+ (``_bundle.detect_kind``), the data commands (``data.world_kind``), the hub
5
+ client (``harbor_adapter.detect_env_kind``), the benchmark runner
6
+ (``engines/hub_bundle.detect_env_kind``), the platform bridge
7
+ (``platform.world_root``) and the World Host (``host.world_tree``) all read
8
+ their answer from here. The TypeScript twin is
9
+ ``packages/shared/src/world/`` (``WorldTree`` + ``World.fromTree``); the
10
+ corpus under ``conformance/worlds/`` pins both to the same answers.
11
+
12
+ Stdlib only: the benchmark runner vendors this subpackage without the SDK's
13
+ dependencies.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import os
19
+ import re
20
+ import tomllib
21
+ from collections.abc import Iterator
22
+ from dataclasses import dataclass
23
+ from functools import cached_property
24
+ from pathlib import Path
25
+ from typing import ClassVar
26
+
27
+ # ── Markers (twin of packages/shared/src/world/markers.ts) ──────────────────
28
+
29
+ WORLD_MANIFEST = "gateway-env.toml"
30
+ API_BUNDLE_MANIFEST = "gateway-api-world.toml"
31
+ BUNDLED_WORLD_PREFIX = "server/world/"
32
+ CONNECTOR_SPEC = "connector.toml"
33
+ PYPROJECT = "pyproject.toml"
34
+ WORLD_SCHEMA_MARKERS = ("schema/world.json", "schema/runtime.json", "schema/lock.json")
35
+ WORLD_ROOT_MARKERS = (CONNECTOR_SPEC, *WORLD_SCHEMA_MARKERS)
36
+ SCHEMA_MANIFEST_KIND = "schema"
37
+ SCHEMA_MANIFEST_KINDS = frozenset({SCHEMA_MANIFEST_KIND, "mock-tools"})
38
+
39
+ KIND_MOCK_TOOLS = "mock-tools"
40
+ KIND_API = "api"
41
+ KIND_HARBOR = "harbor"
42
+ KIND_VERIFIERS = "verifiers"
43
+ KIND_VERIFIERS_V1 = "verifiers-v1"
44
+
45
+ _SKIPPED_DIRS = frozenset({"dist", "build", "outputs", "__pycache__"})
46
+ _HARBOR_TASK = re.compile(r"^(?:tasks/)?([^/]+)/task\.toml$")
47
+
48
+ # The `verifiers.v1` namespace first shipped in verifiers 0.2.0, so a v1
49
+ # package declares verifiers>=0.2 (or ~=0.2 / ==0.2.* / >=1 / ==1.*) in
50
+ # pyproject — minor >= 2 on the 0.x line or any major >= 1, never 0.0/0.1 —
51
+ # and/or imports the namespace or subclasses Taskset alongside the v1
52
+ # packaging convention of an ``__all__`` export (any library may name a class
53
+ # Taskset). A bare unversioned dependency or a pre-0.2 pin is v0.
54
+ _V1_DEP = re.compile(
55
+ r"""["']verifiers\s*(?:\[[^\]]*\])?\s*(?:>=|~=|==|>)\s*""" r"""(?:0\.(?:[2-9]\d*|[1-9]\d+)|[1-9]\d*)"""
56
+ )
57
+ _V1_IMPORT = re.compile(
58
+ r"^\s*(?:from\s+verifiers\.v1\s+import\b|from\s+verifiers\s+import\s+v1\b|import\s+verifiers\.v1\b)",
59
+ re.MULTILINE,
60
+ )
61
+ _V1_TASKSET = re.compile(r"class\s+\w+\s*\(\s*(?:(?:vf|v1|verifiers(?:\.v1)?)\.)?Taskset\s*[,)]")
62
+
63
+
64
+ def _present(path: Path) -> bool:
65
+ """A file exists — or a symlink to one is dangling: a broken authority
66
+ must fail loudly downstream, never quietly downgrade the tree."""
67
+ return path.is_file() or path.is_symlink()
68
+
69
+
70
+ def _skipped_dir(name: str) -> bool:
71
+ return name.startswith(".") or name in _SKIPPED_DIRS or name.endswith(".egg-info")
72
+
73
+
74
+ def pyproject_pins_v1(path: str, text: str) -> bool:
75
+ if path != PYPROJECT:
76
+ return False
77
+ return _V1_DEP.search(text) is not None
78
+
79
+
80
+ def module_uses_v1(path: str, text: str) -> bool:
81
+ if not path.endswith(".py"):
82
+ return False
83
+ return bool(_V1_IMPORT.search(text)) or ("__all__" in text and bool(_V1_TASKSET.search(text)))
84
+
85
+
86
+ class WorldManifest:
87
+ """A world's ``gateway-env.toml``, parsed once. An absent manifest and an
88
+ unparsable one are both manifests: ``present`` and ``error`` say which,
89
+ and every accessor answers "declares nothing" for them."""
90
+
91
+ def __init__(self, text: str | None, root: dict | None, error: str | None) -> None:
92
+ self.text = text
93
+ self._root = root
94
+ self.error = error
95
+
96
+ ABSENT: ClassVar[WorldManifest]
97
+
98
+ @staticmethod
99
+ def parse(text: str) -> WorldManifest:
100
+ try:
101
+ return WorldManifest(text, tomllib.loads(text), None)
102
+ except tomllib.TOMLDecodeError as error:
103
+ return WorldManifest(text, None, f"{WORLD_MANIFEST} is invalid TOML: {error}")
104
+
105
+ @property
106
+ def present(self) -> bool:
107
+ return self.text is not None
108
+
109
+ @property
110
+ def invalid(self) -> bool:
111
+ return self.present and self._root is None
112
+
113
+ def table(self, name: str) -> dict | None:
114
+ value = (self._root or {}).get(name)
115
+ return value if isinstance(value, dict) else None
116
+
117
+ def has(self, name: str) -> bool:
118
+ return self._root is not None and name in self._root
119
+
120
+ @property
121
+ def environment(self) -> dict | None:
122
+ if self._root is None:
123
+ return None
124
+ if self.has("environment"):
125
+ return self.table("environment")
126
+ value = self._root.get("env", {})
127
+ return value if isinstance(value, dict) else None
128
+
129
+ @property
130
+ def kind(self) -> object:
131
+ env = self.environment
132
+ return None if env is None else env.get("kind")
133
+
134
+ @property
135
+ def declares_schema(self) -> bool:
136
+ return self.has("schema")
137
+
138
+
139
+ WorldManifest.ABSENT = WorldManifest(None, None, None)
140
+
141
+
142
+ @dataclass(frozen=True)
143
+ class WorldTree:
144
+ """A tree on disk and where the authored world lives inside it. Immutable;
145
+ the root decision is made once at construction."""
146
+
147
+ root: Path
148
+ world_root: str
149
+
150
+ @classmethod
151
+ def from_dir(cls, root: str | os.PathLike[str]) -> WorldTree:
152
+ root = Path(root)
153
+ nested = root / BUNDLED_WORLD_PREFIX
154
+ bundled = _present(root / API_BUNDLE_MANIFEST) and any(
155
+ _present(nested / marker) for marker in WORLD_ROOT_MARKERS
156
+ )
157
+ return cls(root, BUNDLED_WORLD_PREFIX if bundled else "")
158
+
159
+ def has(self, relative: str) -> bool:
160
+ return _present(self.root / relative)
161
+
162
+ def world_path(self, relative: str) -> str:
163
+ return self.world_root + relative
164
+
165
+ @property
166
+ def world(self) -> Path:
167
+ """The authored world's directory."""
168
+ return self.root / self.world_root if self.world_root else self.root
169
+
170
+ @property
171
+ def is_api_bundle(self) -> bool:
172
+ return self.has(API_BUNDLE_MANIFEST)
173
+
174
+ @property
175
+ def has_schema_marker(self) -> bool:
176
+ return any(self.has(self.world_path(marker)) for marker in WORLD_SCHEMA_MARKERS)
177
+
178
+ @property
179
+ def has_connector(self) -> bool:
180
+ return self.has(self.world_path(CONNECTOR_SPEC))
181
+
182
+ @cached_property
183
+ def manifest(self) -> WorldManifest:
184
+ path = self.world / WORLD_MANIFEST
185
+ if not _present(path):
186
+ return WorldManifest.ABSENT
187
+ try:
188
+ return WorldManifest.parse(path.read_text(encoding="utf-8"))
189
+ except (OSError, UnicodeDecodeError) as error:
190
+ return WorldManifest("", None, f"cannot read {WORLD_MANIFEST}: {error}")
191
+
192
+ def harbor_task_names(self) -> list[str]:
193
+ names = set()
194
+ for pattern in ("tasks/*/task.toml", "*/task.toml"):
195
+ for path in self.root.glob(pattern):
196
+ match = _HARBOR_TASK.match(path.relative_to(self.root).as_posix())
197
+ if match:
198
+ names.add(match.group(1))
199
+ return sorted(names)
200
+
201
+ def scenario_names(self) -> list[str]:
202
+ return sorted(path.parent.name for path in self.root.glob("scenarios/*/scenario.toml") if path.is_file())
203
+
204
+ def v1_evidence(self) -> Iterator[tuple[str, str]]:
205
+ """``(relpath, text)`` of the files a verifiers package's API version is
206
+ read from: the root pyproject and every ``.py`` the archive rules keep
207
+ (``benchmark_hub._archive``: no hidden, build, dist, outputs, pycache or
208
+ egg-info directories)."""
209
+ for dirpath, dirnames, filenames in os.walk(self.root):
210
+ dirnames[:] = sorted(name for name in dirnames if not _skipped_dir(name))
211
+ for filename in sorted(filenames):
212
+ path = Path(dirpath) / filename
213
+ relative = path.relative_to(self.root).as_posix()
214
+ if filename.startswith(".") or not (relative == PYPROJECT or filename.endswith(".py")):
215
+ continue
216
+ try:
217
+ yield relative, path.read_text(encoding="utf-8", errors="replace")
218
+ except OSError:
219
+ continue
220
+
221
+
222
+ class World:
223
+ """What a tree is, and what that means for the paths that touch it.
224
+ Construct through ``World.from_tree`` / ``World.from_dir`` only."""
225
+
226
+ kind: str
227
+ shape: str
228
+
229
+ def __init__(self, tree: WorldTree) -> None:
230
+ self.tree = tree
231
+
232
+ @staticmethod
233
+ def from_tree(tree: WorldTree) -> World:
234
+ if tree.is_api_bundle:
235
+ return ApiBundle(tree)
236
+ if tree.has(WORLD_MANIFEST):
237
+ schema = tree.has_schema_marker or tree.manifest.declares_schema
238
+ return SchemaWorld(tree) if schema else ToolsWorld(tree)
239
+ if tree.harbor_task_names():
240
+ return HarborBench(tree)
241
+ v1 = VerifiersBench.is_v1(tree)
242
+ if v1 or tree.has(PYPROJECT):
243
+ return VerifiersBench(tree, v1)
244
+ return PlainTree(tree)
245
+
246
+ @staticmethod
247
+ def from_dir(root: str | os.PathLike[str]) -> World:
248
+ return World.from_tree(WorldTree.from_dir(root))
249
+
250
+ @property
251
+ def reported_kind(self) -> str:
252
+ """What ``worlds create|describe|list`` and the data commands report."""
253
+ return self.kind
254
+
255
+ @property
256
+ def runner_engine(self) -> str:
257
+ return "openai-compatible"
258
+
259
+ @property
260
+ def substrate(self) -> str:
261
+ """Hosted on the World Host, or executed by the benchmark runner."""
262
+ return "world"
263
+
264
+ @property
265
+ def tools_engine(self) -> bool:
266
+ """Runs on the tools engine: handlers over a per-session SQLite."""
267
+ return False
268
+
269
+ @property
270
+ def local_runtime(self) -> bool:
271
+ """``gwenv`` can run the tree in-process."""
272
+ return True
273
+
274
+ def task_names(self) -> list[str]:
275
+ """The tasks the tree defines by layout; a schema world's live in
276
+ its manifest."""
277
+ return []
278
+
279
+ @property
280
+ def schema_mode(self) -> str:
281
+ """``schema`` (a compiled marker or a declared ``[schema]``),
282
+ ``invalid`` (a manifest that does not parse) or ``legacy``."""
283
+ if self.tree.has_schema_marker:
284
+ return "schema"
285
+ if self.tree.manifest.invalid:
286
+ return "invalid"
287
+ return "schema" if self.tree.manifest.declares_schema else "legacy"
288
+
289
+ @property
290
+ def is_schema(self) -> bool:
291
+ return self.schema_mode == "schema"
292
+
293
+
294
+ class SchemaWorld(World):
295
+ """A gateway-world/1 contract tree stored as itself."""
296
+
297
+ kind = KIND_MOCK_TOOLS
298
+ shape = "schema"
299
+
300
+ @property
301
+ def reported_kind(self) -> str:
302
+ return SCHEMA_MANIFEST_KIND
303
+
304
+ @property
305
+ def tools_engine(self) -> bool:
306
+ return True
307
+
308
+
309
+ class ToolsWorld(World):
310
+ """``gateway-env.toml`` + ``db/schema.sql`` + handlers, no contract."""
311
+
312
+ kind = KIND_MOCK_TOOLS
313
+ shape = "legacy-tools"
314
+
315
+ @property
316
+ def tools_engine(self) -> bool:
317
+ return True
318
+
319
+
320
+ class ApiBundle(World):
321
+ """A world packaged with ``gateway-api-world.toml`` over ``server/world/``."""
322
+
323
+ kind = KIND_API
324
+ shape = "api-bundle"
325
+
326
+ def task_names(self) -> list[str]:
327
+ return self.tree.scenario_names()
328
+
329
+
330
+ class ContainerBench(World):
331
+ @property
332
+ def substrate(self) -> str:
333
+ return "container"
334
+
335
+
336
+ class HarborBench(ContainerBench):
337
+ """``<task>/task.toml`` or ``tasks/<task>/task.toml`` directories."""
338
+
339
+ kind = KIND_HARBOR
340
+ shape = "harbor"
341
+
342
+ @property
343
+ def runner_engine(self) -> str:
344
+ return "harbor"
345
+
346
+ def task_names(self) -> list[str]:
347
+ return self.tree.harbor_task_names()
348
+
349
+
350
+ class VerifiersBench(ContainerBench):
351
+ """A Python package the runner installs and grades with reward functions."""
352
+
353
+ shape = "verifiers"
354
+
355
+ def __init__(self, tree: WorldTree, v1: bool = False) -> None:
356
+ super().__init__(tree)
357
+ self.kind = KIND_VERIFIERS_V1 if v1 else KIND_VERIFIERS
358
+
359
+ @staticmethod
360
+ def is_v1(tree: WorldTree) -> bool:
361
+ return any(pyproject_pins_v1(path, text) or module_uses_v1(path, text) for path, text in tree.v1_evidence())
362
+
363
+ @property
364
+ def local_runtime(self) -> bool:
365
+ return False
366
+
367
+
368
+ class PlainTree(VerifiersBench):
369
+ """No Gateway marker, no harbor task, no Python package: not yet a world
370
+ and not a benchmark. The runner's fallback reads it as a verifiers
371
+ package, so that is the kind it answers."""
372
+
373
+
374
+ __all__ = [
375
+ "API_BUNDLE_MANIFEST",
376
+ "BUNDLED_WORLD_PREFIX",
377
+ "CONNECTOR_SPEC",
378
+ "PYPROJECT",
379
+ "WORLD_MANIFEST",
380
+ "WORLD_ROOT_MARKERS",
381
+ "WORLD_SCHEMA_MARKERS",
382
+ "ApiBundle",
383
+ "HarborBench",
384
+ "PlainTree",
385
+ "SchemaWorld",
386
+ "ToolsWorld",
387
+ "VerifiersBench",
388
+ "World",
389
+ "WorldManifest",
390
+ "WorldTree",
391
+ ]
@@ -0,0 +1,156 @@
1
+ ---
2
+ name: hooks-getting-started
3
+ description: Rules around an agent's tool calls from one gateway-hooks.toml — the alias door (personas in, real values out), identities that arrive from the web (mint_inbound), redaction of anything (kinds, literal values, patterns), per-task overrides, coding-agent and agent-SDK wiring, and how to check a file before an agent runs it. Use when an agent must not see a real person's identifiers, when web results must be scrubbed, or when wiring gateway hooks into Claude Code or an agent SDK.
4
+ ---
5
+
6
+ # Hooks: getting started
7
+
8
+ One file, `gateway-hooks.toml`, holds the rules that run around an agent's tool calls. The
9
+ same file is attached in code by an agent SDK (`HookRun.from_config` / `HookRun.fromConfig`),
10
+ run as a coding agent's hook (`gateway hooks run`), and validated without an agent
11
+ (`gateway hooks check`). Nothing in it is automatic: every behaviour is a rule you wrote or a
12
+ knob with a default, and a task can override the file.
13
+
14
+ ```bash
15
+ gateway hooks guide # this page
16
+ gateway hooks check gateway-hooks.toml # validate, build every handler, dry-run one event
17
+ gateway hooks run # the command a coding agent names in its settings
18
+ gateway hooks legend <run dir> # persona -> real, for the owner
19
+ gateway hooks reveal <file> # a run's file with the real values put back
20
+ ```
21
+
22
+ ## The file, in one screen
23
+
24
+ ```toml
25
+ [alias]
26
+ slugs = ["spycloud"] # worlds that store personal fields as sha256:<slug>:<value>
27
+ public_layer_wins = true # a `role: public` seed is never rewritten, even inside a private email's domain
28
+ public_mailbox_domains = ["gmail.com"] # kept real in an email persona; omit for the built-in free-mail list, [] for none
29
+ mint_inbound = ["email", "phone", "ip"] # kinds minted ON SIGHT in results; omit = seeds only
30
+ legend = "{run_dir}/legend.json" # the owner's way back: run key + every persona, written at run_end
31
+
32
+ [vendors]
33
+ spycloud = "spycloud_.*" # tool-name patterns -> a vendor label for `vendor = ...` matchers
34
+
35
+ [[hooks.pre_tool_call]] # or [[hooks.PreToolUse]] — Claude Code's names are accepted
36
+ matcher = "spycloud_.* | web_search" # `A | B` alternation of unanchored regexes; "" or "*" is every tool
37
+ handlers = [
38
+ { type = "alias" }, # persona -> real on the way out
39
+ { type = "command", command = "python guard.py", timeout = 10 }, # your hook: event on stdin, verdict on stdout
40
+ { type = "deny", reason = "not in this run" },
41
+ ]
42
+
43
+ [[hooks.post_tool_call]]
44
+ matcher = "spycloud_.* | web_search"
45
+ handlers = [
46
+ { type = "alias" }, # real -> persona on the way in, leaks metered
47
+ { type = "record", sink = "{run_dir}/transcript.jsonl", async = true },
48
+ ]
49
+
50
+ [[hooks.post_tool_call]] # a SECOND rule: one rewriter per rule, and rules chain
51
+ matcher = "web_search | web_fetch"
52
+ handlers = [{ type = "redact", kinds = ["phone"], values = ["Acme Holdings"], patterns = ["\\bDOB[: ]+\\S+"] }]
53
+
54
+ [[hooks.run_end]]
55
+ handlers = [{ type = "alias", check = "outgoing" }] # a real value in the report fails the run
56
+ ```
57
+
58
+ Events: `pre_tool_call`, `post_tool_call`, `pre_request`, `post_response`, `run_end`.
59
+ Handlers: `alias`, `command`, `http`, `callable`, `deny`, `record`, `redact`. A rule's handlers
60
+ all see the same event and at most one of them may rewrite it; put a second rewriter under its
61
+ own `[[hooks.<event>]]` — rules apply in order and each sees the previous one's rewrite.
62
+
63
+ ## The alias door
64
+
65
+ Seed it with the task's entities before the agent starts; the instruction the agent reads is
66
+ already in persona space, and every tool result is rewritten on the way in while every tool
67
+ call is resolved back to the real value on the way out.
68
+
69
+ ```json
70
+ [{ "type": "person_name", "value": "Matthew Nowell", "identity": "subject" },
71
+ { "type": "email", "value": "m.nowell@nowell-ltd.com", "identity": "subject" },
72
+ { "type": "domain", "value": "star-co.net.kp", "role": "public" }]
73
+ ```
74
+
75
+ Kinds: `person_name`, `email`, `username`, `phone`, `company`, `domain`, `ip`, `account`,
76
+ `machine`, `log`; anything else gets a `REF-nnnn` stand-in. `identity` ties attributes to one
77
+ person so they stay consistent; `role: "public"` is never replaced. Every variant of a value is
78
+ tracked (bare surname, handle spellings, `(at)` emails, phone formats, percent-encoding, a
79
+ world's `sha256:` stored form); what slips through is counted (`leak_through_inbound`), and a
80
+ real value in the outgoing report fails the run (`leaks_outbound`).
81
+
82
+ ## Identities that arrive, not seeded
83
+
84
+ A page or a vendor row brings people the seed list never named. By default they reach the
85
+ agent as they are. Two knobs change that:
86
+
87
+ - **`mint_inbound = ["email", "phone", "ip"]`** — an unseeded value of a listed kind met in a
88
+ result gets a persona on sight: aliased in, resolved back out on the next call, one persona
89
+ for the rest of the run whatever the spelling. Only these kinds are pattern-safe (a global
90
+ IP; a number with a `+` or an area code); a name or a handle is seeded, never guessed. The
91
+ whole result is read before any of it is rewritten, so a spelling in an earlier field is
92
+ caught by a later one. Listed in `found_inbound`.
93
+ - **`redact`** — removes instead of replacing, from ANY string a result carries (page body,
94
+ title, URL, vendor row): `kinds` for shaped values, `values` for arbitrary literals (a name,
95
+ a company, a phrase — any case, any whitespace), `patterns` for regular expressions;
96
+ `token` (default `[redacted {kind}]`) is what stands in. Runs after the door, leaves personas
97
+ alone, counted in `redactions` (kind and a three-letter prefix, never the value).
98
+
99
+ ## Per task, not per file
100
+
101
+ What a task aliases is the task's decision — one wants a persona for every address the web
102
+ returns, another's selectors are public IPs that must stay facts — so the run overrides the
103
+ file:
104
+
105
+ ```python
106
+ run = HookRun.from_config("gateway-hooks.toml", mint_inbound=["email"]) # this task only
107
+ run.seed(task["entities"] + task.get("replacements", [])) # values expected to surface, seeded early
108
+ run.on("post_tool_call", RedactHandler(run.alias, values=["Acme Holdings"]), matcher="web_.*")
109
+ run.mint("email", found_later) # one more persona, from code, any time
110
+ ```
111
+
112
+ ```ts
113
+ const run = await HookRun.fromConfig("gateway-hooks.toml", { mintInbound: [] }); // seeds only, whatever the file says
114
+ ```
115
+
116
+ ## Seeing the real data (the owner)
117
+
118
+ ```bash
119
+ gateway hooks legend runs/<run id>/ # persona -> real, with role, provenance, identity
120
+ gateway hooks reveal runs/<run id>/report.md # the file with every persona put back
121
+ gateway hooks reveal transcript.jsonl --legend .gateway/hooks/<session>/legend.json --out transcript.real.jsonl
122
+ ```
123
+
124
+ The run writes the legend where `[alias] legend` says (found personas included); a coding
125
+ agent's state dir keeps one beside `run.json` after every event. `reveal` puts every persona
126
+ spelling back in a JSON or text file; a `[redacted …]` token stays — redaction removed the
127
+ value, and its raw copy is a `record` sink placed before the `redact` rule. The legend maps
128
+ personas to people: keep it with the run key, out of version control, never beside world data.
129
+
130
+ ## Wiring
131
+
132
+ - **Agent SDK** — open the file (`from gatewaysdk.hooks import HookRun; run = HookRun("gateway-hooks.toml")`
133
+ / `import { HookRun } from "@withgateway/sdk/hooks"; const run = await HookRun.fromConfig("gateway-hooks.toml")`),
134
+ then wrap the tools: Python `guarded = run.wrap({"lookup_email": lookup_email})`, TypeScript
135
+ `guarded = run.wrapTools({ lookup_email: lookupEmail })`; register the wrapped function wherever
136
+ the SDK takes a tool implementation. `run.tools()` materializes the file's `[tools.*]` (a keyed
137
+ web search, a connector operation) already wrapped; `verdict = run.end(report)` runs `run_end`.
138
+ - **Claude Code / coding agents** — `.claude/settings.json`: `PreToolUse`, `PostToolUse` and
139
+ `Stop` entries whose command is `gateway hooks run`. State (run key + seeds) persists under
140
+ `--state`, `$GATEWAY_HOOKS_STATE`, else `.gateway/hooks/<session id>`. A coding agent shows the
141
+ model a result before its hook runs, so the inbound rewrite cannot reach it there (the
142
+ command says so: `rewrite reached nobody`); route such tools through the SDK or an MCP server
143
+ that wraps them.
144
+ - **Any transport** — `run.session(session, request_fn=...)` puts `pre_request` /
145
+ `post_response` around a client with its own auth.
146
+
147
+ ## Check before wiring
148
+
149
+ ```bash
150
+ gateway hooks check gateway-hooks.toml --event PreToolUse --tool Bash --input '{"command":"ls"}'
151
+ gateway hooks check gateway-hooks.toml --seed @entities.json --tool web_search --input '{"query":"Keith Yarrow"}'
152
+ ```
153
+
154
+ `check` builds every handler (a callable that does not import, a bad pattern, two rewriters on
155
+ one rule fail here, not mid-run) and prints what each would do to one event. `--json` adds the
156
+ run's meters: aliases, substitutions, leaks, found identities, redactions, denials, hook errors.