wspx 0.1.0.dev0__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.
Files changed (99) hide show
  1. wspx/__init__.py +24 -0
  2. wspx/_version.py +1 -0
  3. wspx/addressing.py +415 -0
  4. wspx/auth.py +77 -0
  5. wspx/cli/__init__.py +1 -0
  6. wspx/cli/client.py +124 -0
  7. wspx/cli/config.py +89 -0
  8. wspx/cli/connect.py +88 -0
  9. wspx/cli/main.py +848 -0
  10. wspx/cloud/__init__.py +20 -0
  11. wspx/cloud/app.py +191 -0
  12. wspx/cloud/backend.py +141 -0
  13. wspx/cloud/compose.py +85 -0
  14. wspx/cloud/deploy.py +102 -0
  15. wspx/config.py +189 -0
  16. wspx/control/__init__.py +5 -0
  17. wspx/control/app.py +488 -0
  18. wspx/control/backend.py +244 -0
  19. wspx/control/gateway_client.py +79 -0
  20. wspx/control/mcp_server.py +71 -0
  21. wspx/control/sessions.py +424 -0
  22. wspx/control/tracedb.py +183 -0
  23. wspx/control/ui/assets/index-BJFweZAn.css +1 -0
  24. wspx/control/ui/assets/index-DhUVhWIK.js +10 -0
  25. wspx/control/ui/index.html +13 -0
  26. wspx/control/views.py +180 -0
  27. wspx/diagnostics.py +146 -0
  28. wspx/driver/__init__.py +7 -0
  29. wspx/driver/loop.py +255 -0
  30. wspx/driver/messages.py +62 -0
  31. wspx/driver/providers/__init__.py +84 -0
  32. wspx/driver/providers/anthropic.py +149 -0
  33. wspx/driver/providers/compat.py +115 -0
  34. wspx/driver/providers/google.py +138 -0
  35. wspx/driver/providers/openai.py +125 -0
  36. wspx/driver/run.py +134 -0
  37. wspx/errors.py +69 -0
  38. wspx/gateway/__init__.py +8 -0
  39. wspx/gateway/app.py +626 -0
  40. wspx/gateway/ledger.py +536 -0
  41. wspx/gateway/prices.py +75 -0
  42. wspx/gateway/usage.py +284 -0
  43. wspx/handles.py +117 -0
  44. wspx/integrations/AGENTS.snippet.md +8 -0
  45. wspx/integrations/SKILL.md +52 -0
  46. wspx/integrations/__init__.py +17 -0
  47. wspx/integrations/claude-plugin/.claude-plugin/plugin.json +30 -0
  48. wspx/integrations/claude-plugin/skills/wspx/SKILL.md +52 -0
  49. wspx/integrations/codex-hooks.toml +9 -0
  50. wspx/lab/__init__.py +5 -0
  51. wspx/lab/analysis.py +485 -0
  52. wspx/lab/check.py +489 -0
  53. wspx/lab/evaluate.py +108 -0
  54. wspx/lab/export.py +275 -0
  55. wspx/lab/grep.py +754 -0
  56. wspx/lab/index.py +239 -0
  57. wspx/lab/ir/__init__.py +1 -0
  58. wspx/lab/ir/capture.py +299 -0
  59. wspx/lab/ir/graph.py +148 -0
  60. wspx/lab/ir/model_ir.py +522 -0
  61. wspx/lab/ir/semantics.py +1255 -0
  62. wspx/lab/ir/tape.py +853 -0
  63. wspx/lab/loader.py +204 -0
  64. wspx/lab/merge.py +200 -0
  65. wspx/lab/ops/__init__.py +76 -0
  66. wspx/lab/ops/builtin.py +379 -0
  67. wspx/lab/patch.py +320 -0
  68. wspx/lab/probe.py +206 -0
  69. wspx/lab/results.py +114 -0
  70. wspx/lab/sdk.py +120 -0
  71. wspx/lab/server.py +236 -0
  72. wspx/lab/session.py +1574 -0
  73. wspx/lab/tensors.py +119 -0
  74. wspx/lab/text.py +69 -0
  75. wspx/lab/tree.py +302 -0
  76. wspx/loops.py +127 -0
  77. wspx/project.py +143 -0
  78. wspx/py.typed +0 -0
  79. wspx/safety/__init__.py +15 -0
  80. wspx/safety/kill.py +47 -0
  81. wspx/safety/sweeper.py +68 -0
  82. wspx/store/__init__.py +64 -0
  83. wspx/store/objects.py +130 -0
  84. wspx/store/overlay.py +209 -0
  85. wspx/store/refs.py +150 -0
  86. wspx/testing/__init__.py +21 -0
  87. wspx/testing/api.py +155 -0
  88. wspx/testing/builtin.py +139 -0
  89. wspx/testing/runner.py +257 -0
  90. wspx/tools/__init__.py +17 -0
  91. wspx/tools/dispatch.py +129 -0
  92. wspx/tools/schemas.py +313 -0
  93. wspx/vcs/__init__.py +17 -0
  94. wspx/vcs/repo.py +431 -0
  95. wspx-0.1.0.dev0.dist-info/METADATA +278 -0
  96. wspx-0.1.0.dev0.dist-info/RECORD +99 -0
  97. wspx-0.1.0.dev0.dist-info/WHEEL +4 -0
  98. wspx-0.1.0.dev0.dist-info/entry_points.txt +10 -0
  99. wspx-0.1.0.dev0.dist-info/licenses/LICENSE +201 -0
wspx/lab/tensors.py ADDED
@@ -0,0 +1,119 @@
1
+ """Tensor helpers shared by the lab: exact hashing and outer-product selection."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ from collections.abc import Sequence
7
+ from typing import Any
8
+
9
+ import torch
10
+
11
+ Selector = int | slice | list[int]
12
+ """Per-dim selection: one index (drops the dim), a slice, or explicit indices."""
13
+ Index = tuple[Selector, ...]
14
+
15
+ DTYPE_NAMES = {
16
+ torch.float32: "float32",
17
+ torch.float16: "float16",
18
+ torch.bfloat16: "bfloat16",
19
+ torch.float64: "float64",
20
+ torch.int8: "int8",
21
+ torch.uint8: "uint8",
22
+ torch.int32: "int32",
23
+ torch.int64: "int64",
24
+ torch.bool: "bool",
25
+ }
26
+ for _name in ("float8_e4m3fn", "float8_e5m2", "float8_e4m3fnuz", "float8_e5m2fnuz"):
27
+ if hasattr(torch, _name):
28
+ DTYPE_NAMES[getattr(torch, _name)] = _name
29
+
30
+
31
+ def dtype_name(dtype: torch.dtype) -> str:
32
+ return DTYPE_NAMES.get(dtype, str(dtype).removeprefix("torch."))
33
+
34
+
35
+ def dtype_from_name(name: str) -> torch.dtype:
36
+ value = getattr(torch, name.removeprefix("torch."), None)
37
+ if not isinstance(value, torch.dtype):
38
+ raise ValueError(f"unknown dtype {name!r}")
39
+ return value
40
+
41
+
42
+ def tensor_hash(tensor: torch.Tensor) -> str:
43
+ """``sha256:<hex>`` over dtype, shape and raw bytes. Bit-exact, device-independent."""
44
+ h = hashlib.sha256()
45
+ h.update(f"{dtype_name(tensor.dtype)}|{tuple(tensor.shape)}|".encode())
46
+ data = tensor.detach()
47
+ if data.device.type != "cpu":
48
+ data = data.cpu()
49
+ data = data.contiguous().reshape(-1)
50
+ if data.numel():
51
+ flat = data.view(torch.uint8).numpy()
52
+ chunk = 1 << 26
53
+ for start in range(0, flat.size, chunk):
54
+ h.update(memoryview(flat[start : start + chunk]))
55
+ return "sha256:" + h.hexdigest()
56
+
57
+
58
+ def normalize_index(index: Index | None, ndim: int) -> Index:
59
+ """Pad an index to ``ndim`` with full slices."""
60
+ if index is None:
61
+ return tuple(slice(None) for _ in range(ndim))
62
+ if len(index) > ndim:
63
+ raise IndexError(f"index has {len(index)} entries for a {ndim}-d tensor")
64
+ return tuple(index) + tuple(slice(None) for _ in range(ndim - len(index)))
65
+
66
+
67
+ def select(tensor: torch.Tensor, index: Index | None) -> torch.Tensor:
68
+ """Outer-product selection: each dim is selected independently (like ``np.ix_``)."""
69
+ index = normalize_index(index, tensor.dim())
70
+ out = tensor
71
+ dim = 0
72
+ for sel in index:
73
+ if isinstance(sel, int):
74
+ out = out.select(dim, sel)
75
+ continue
76
+ if isinstance(sel, slice):
77
+ out = out[(slice(None),) * dim + (sel,)]
78
+ else:
79
+ idx = torch.as_tensor(sel, dtype=torch.long, device=out.device)
80
+ out = out.index_select(dim, idx)
81
+ dim += 1
82
+ return out
83
+
84
+
85
+ def selection_mask(shape: Sequence[int], index: Index | None) -> torch.Tensor:
86
+ """Boolean mask (CPU) of the elements an index selects."""
87
+ mask = torch.zeros(tuple(shape), dtype=torch.bool)
88
+ mask[_key(shape, normalize_index(index, len(shape)))] = True
89
+ return mask
90
+
91
+
92
+ def assign(target: torch.Tensor, index: Index | None, values: torch.Tensor) -> None:
93
+ """In-place ``target[index] = values`` with the same semantics as :func:`select`."""
94
+ key = _key(target.shape, normalize_index(index, target.dim()))
95
+ target[key] = values.to(device=target.device, dtype=target.dtype)
96
+
97
+
98
+ def _key(shape: Sequence[int], index: Index) -> Any:
99
+ """An indexing key with outer-product semantics.
100
+
101
+ Without explicit index lists, plain basic indexing already behaves that way.
102
+ With them, every non-int dim becomes a broadcast index tensor, so lists on
103
+ several dims select their Cartesian product instead of zipping.
104
+ """
105
+ if not any(isinstance(s, list) for s in index):
106
+ return tuple(index)
107
+ n = sum(not isinstance(s, int) for s in index)
108
+ key: list[Any] = []
109
+ k = 0
110
+ for d, sel in enumerate(index):
111
+ if isinstance(sel, int):
112
+ key.append(sel)
113
+ continue
114
+ values = list(range(*sel.indices(shape[d]))) if isinstance(sel, slice) else list(sel)
115
+ view = [1] * n
116
+ view[k] = len(values)
117
+ key.append(torch.as_tensor(values, dtype=torch.long).view(view))
118
+ k += 1
119
+ return tuple(key)
wspx/lab/text.py ADDED
@@ -0,0 +1,69 @@
1
+ """Prompts in, token ids out. Works with a tokenizer, or with raw token ids when there is none."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Sequence
6
+ from typing import Any
7
+
8
+ import torch
9
+
10
+ from wspx.errors import WspxError
11
+
12
+ Prompt = str | Sequence[int]
13
+
14
+
15
+ class Text:
16
+ def __init__(
17
+ self, tokenizer: Any = None, *, vocab_size: int | None = None, pad_id: int = 0
18
+ ) -> None:
19
+ self.tokenizer = tokenizer
20
+ self.vocab_size = vocab_size
21
+ pad = getattr(tokenizer, "pad_token_id", None) if tokenizer is not None else None
22
+ if pad is None and tokenizer is not None:
23
+ pad = getattr(tokenizer, "eos_token_id", None)
24
+ self.pad_id = pad if pad is not None else pad_id
25
+ self.eos_id = getattr(tokenizer, "eos_token_id", None) if tokenizer is not None else None
26
+
27
+ def ids(self, prompt: Prompt) -> list[int]:
28
+ if isinstance(prompt, str):
29
+ if self.tokenizer is None:
30
+ raise WspxError("this model has no tokenizer; pass prompts as lists of token ids")
31
+ return list(self.tokenizer(prompt, add_special_tokens=True)["input_ids"])
32
+ return [int(t) for t in prompt]
33
+
34
+ def batch(
35
+ self, prompts: Sequence[Prompt], device: torch.device | str = "cpu"
36
+ ) -> tuple[torch.Tensor, torch.Tensor]:
37
+ """Left-padded ids and attention mask, so every row ends at its last real token."""
38
+ rows = [self.ids(p) for p in prompts]
39
+ if not rows or any(not r for r in rows):
40
+ raise WspxError("empty prompt")
41
+ width = max(len(r) for r in rows)
42
+ ids = torch.full((len(rows), width), self.pad_id, dtype=torch.long)
43
+ mask = torch.zeros((len(rows), width), dtype=torch.long)
44
+ for i, row in enumerate(rows):
45
+ ids[i, width - len(row) :] = torch.tensor(row)
46
+ mask[i, width - len(row) :] = 1
47
+ return ids.to(device), mask.to(device)
48
+
49
+ def token_id(self, token: str | int) -> int:
50
+ if isinstance(token, int):
51
+ return token
52
+ if self.tokenizer is None:
53
+ raise WspxError(f"{token!r}: no tokenizer; use a token id")
54
+ ids = self.tokenizer(token, add_special_tokens=False)["input_ids"]
55
+ if len(ids) != 1:
56
+ pieces = [self.decode([i]) for i in ids]
57
+ raise WspxError(f"{token!r} is {len(ids)} tokens {pieces}; use one token or a token id")
58
+ return int(ids[0])
59
+
60
+ def decode(self, ids: Sequence[int]) -> str:
61
+ if self.tokenizer is None:
62
+ return " ".join(f"#{int(i)}" for i in ids)
63
+ return str(self.tokenizer.decode(list(ids)))
64
+
65
+ def show(self, token: int) -> str:
66
+ """A token as a readable quoted string."""
67
+ if self.tokenizer is None:
68
+ return f"#{token}"
69
+ return repr(self.decode([token]))
wspx/lab/tree.py ADDED
@@ -0,0 +1,302 @@
1
+ """The working tree: live weights equal to base plus the applied deltas (§5.6).
2
+
3
+ Edits are written straight into the model's parameters, so a forward pass is
4
+ the plain model's forward: no hooks on the default path, and the weights you
5
+ test are the bits you export.
6
+
7
+ Base tensors are always recoverable (from the read-only model files, or a
8
+ snapshot taken before the first edit when there is no reader). A bounded LRU
9
+ cache of touched base tensors makes switching commits fast; evicted tensors
10
+ are re-read, which is slower but just as exact.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from collections import OrderedDict
16
+ from collections.abc import Callable
17
+ from dataclasses import dataclass, field
18
+ from typing import Any
19
+
20
+ import torch
21
+
22
+ from wspx.errors import PatchError
23
+ from wspx.lab.ir.model_ir import ModelIR
24
+ from wspx.lab.patch import TensorDelta, materialize
25
+ from wspx.lab.tensors import tensor_hash
26
+
27
+ BaseReader = Callable[[str], torch.Tensor]
28
+ DeltaLoader = Callable[[str], TensorDelta]
29
+
30
+
31
+ @dataclass
32
+ class TreeState:
33
+ """What is applied on top of the base, per tensor. Serialises to a tree object."""
34
+
35
+ merged: dict[str, tuple[str, ...]] = field(default_factory=dict)
36
+ adapter: dict[str, tuple[str, ...]] = field(default_factory=dict)
37
+ promote: frozenset[str] = frozenset()
38
+
39
+ def tensors(self) -> set[str]:
40
+ return set(self.merged) | set(self.adapter) | set(self.promote)
41
+
42
+ def with_deltas(self, deltas: list[TensorDelta], precision: str) -> TreeState:
43
+ merged = dict(self.merged)
44
+ adapter = dict(self.adapter)
45
+ promote = set(self.promote)
46
+ for d in deltas:
47
+ if precision == "adapter":
48
+ adapter[d.tensor] = tuple(sorted({*adapter.get(d.tensor, ()), d.oid}))
49
+ else:
50
+ merged[d.tensor] = tuple(sorted({*merged.get(d.tensor, ()), d.oid}))
51
+ if precision == "promote":
52
+ promote.add(d.tensor)
53
+ return TreeState(merged, adapter, frozenset(promote))
54
+
55
+ def to_dict(self) -> dict[str, Any]:
56
+ return {
57
+ "merged": {k: list(v) for k, v in sorted(self.merged.items()) if v},
58
+ "adapter": {k: list(v) for k, v in sorted(self.adapter.items()) if v},
59
+ "promote": sorted(self.promote),
60
+ }
61
+
62
+ @classmethod
63
+ def from_dict(cls, data: dict[str, Any] | None) -> TreeState:
64
+ data = data or {}
65
+ return cls(
66
+ {k: tuple(v) for k, v in data.get("merged", {}).items()},
67
+ {k: tuple(v) for k, v in data.get("adapter", {}).items()},
68
+ frozenset(data.get("promote", [])),
69
+ )
70
+
71
+ def __eq__(self, other: object) -> bool:
72
+ return isinstance(other, TreeState) and self.to_dict() == other.to_dict()
73
+
74
+ def __hash__(self) -> int: # pragma: no cover - not used as a key
75
+ return hash(str(self.to_dict()))
76
+
77
+
78
+ class WorkingTree:
79
+ def __init__(
80
+ self,
81
+ model: torch.nn.Module,
82
+ ir: ModelIR,
83
+ *,
84
+ load_delta: DeltaLoader,
85
+ base_reader: BaseReader | None = None,
86
+ cache_bytes: int | None = None,
87
+ ) -> None:
88
+ self.model = model
89
+ self.ir = ir
90
+ self.load_delta = load_delta
91
+ self.base_reader = base_reader
92
+ self.cache_bytes = cache_bytes
93
+ self.params: dict[str, torch.nn.Parameter] = dict(model.named_parameters())
94
+ self.state = TreeState()
95
+ self._dtype = {name: p.dtype for name, p in self.params.items()}
96
+ self._cache: OrderedDict[str, torch.Tensor] = OrderedDict()
97
+ self._cache_used = 0
98
+ self._pinned: set[str] = set()
99
+ self._hashes: dict[str, str] = {}
100
+ self._deltas: dict[str, TensorDelta] = {}
101
+ self._hooks: dict[str, list[Any]] = {}
102
+
103
+ # ------------------------------------------------------------------ deltas
104
+
105
+ def delta(self, oid: str) -> TensorDelta:
106
+ if oid not in self._deltas:
107
+ self._deltas[oid] = self.load_delta(oid)
108
+ return self._deltas[oid]
109
+
110
+ def remember(self, delta: TensorDelta) -> str:
111
+ """Keep a freshly compiled delta so it never has to round-trip through the store."""
112
+ oid = delta.oid
113
+ self._deltas[oid] = delta
114
+ return oid
115
+
116
+ # ------------------------------------------------------------------ base
117
+
118
+ def base(self, name: str) -> torch.Tensor:
119
+ """The base tensor (unedited), in its stored dtype, on the parameter's device."""
120
+ device = self.params[name].device
121
+ if name in self._cache:
122
+ self._cache.move_to_end(name)
123
+ return self._cache[name].to(device)
124
+ if name not in self.state.tensors():
125
+ return self.params[name].detach()
126
+ if self.base_reader is None: # pragma: no cover - snapshots are pinned
127
+ raise PatchError(f"base of {name} was not kept and there is no reader")
128
+ tensor = self.base_reader(name)
129
+ self._remember_base(name, tensor, pinned=False)
130
+ return tensor.to(device)
131
+
132
+ def _snapshot(self, name: str) -> None:
133
+ """Keep a copy of the base before its first edit."""
134
+ if name in self._cache or name in self.state.tensors():
135
+ return
136
+ copy = self.params[name].detach().to("cpu", copy=True)
137
+ self._remember_base(name, copy, pinned=self.base_reader is None)
138
+
139
+ def _remember_base(self, name: str, tensor: torch.Tensor, *, pinned: bool) -> None:
140
+ size = tensor.numel() * tensor.element_size()
141
+ self._cache[name] = tensor
142
+ self._cache_used += size
143
+ if pinned:
144
+ self._pinned.add(name)
145
+ if self.cache_bytes is None:
146
+ return
147
+ for victim in list(self._cache):
148
+ if self._cache_used <= self.cache_bytes:
149
+ break
150
+ if victim == name or victim in self._pinned:
151
+ continue
152
+ evicted = self._cache.pop(victim)
153
+ self._cache_used -= evicted.numel() * evicted.element_size()
154
+
155
+ # ------------------------------------------------------------------ apply
156
+
157
+ def preview(
158
+ self, name: str, extra: list[TensorDelta], *, dtype: torch.dtype | None = None
159
+ ) -> torch.Tensor:
160
+ """What ``name`` would hold with ``extra`` deltas added, without touching the model."""
161
+ deltas = [self.delta(o) for o in self.state.merged.get(name, ())] + extra
162
+ target = dtype or (torch.float32 if name in self.state.promote else self._dtype[name])
163
+ return materialize(self.base(name), deltas, dtype=target)
164
+
165
+ def materialize_state(
166
+ self, name: str, state: TreeState, *, dtype: torch.dtype | None = None
167
+ ) -> torch.Tensor:
168
+ """What ``name`` holds under ``state``, without touching the model."""
169
+ deltas = [self.delta(o) for o in state.merged.get(name, ())]
170
+ target = dtype or (torch.float32 if name in state.promote else self._dtype[name])
171
+ return materialize(self.base(name), deltas, dtype=target)
172
+
173
+ def checkout(self, state: TreeState) -> dict[str, str]:
174
+ """Make the live weights equal ``base + state``. Returns hashes of changed tensors."""
175
+ old = self.state
176
+ changed: list[str] = []
177
+ for name in sorted(old.tensors() | state.tensors()):
178
+ same_merged = old.merged.get(name, ()) == state.merged.get(name, ())
179
+ same_promote = (name in old.promote) == (name in state.promote)
180
+ if same_merged and same_promote:
181
+ continue
182
+ if name not in self.params:
183
+ raise PatchError(f"{name}: no such parameter in this model")
184
+ self._snapshot(name)
185
+ deltas = [self.delta(o) for o in state.merged.get(name, ())]
186
+ for d in deltas:
187
+ if tuple(d.shape) != tuple(self.params[name].shape):
188
+ raise PatchError(
189
+ f"{name}: delta shape {d.shape} != {tuple(self.params[name].shape)}"
190
+ )
191
+ promote = name in state.promote
192
+ dtype = torch.float32 if promote else self._dtype[name]
193
+ new = materialize(self.base(name), deltas, dtype=dtype)
194
+ self._write(name, new, promote=promote)
195
+ self._hashes.pop(name, None)
196
+ changed.append(name)
197
+ self._sync_adapters(state)
198
+ self.state = state
199
+ return {name: self.hash(name) for name in changed}
200
+
201
+ def stage(self, deltas: list[TensorDelta], precision: str = "merged") -> dict[str, str]:
202
+ for d in deltas:
203
+ self.remember(d)
204
+ return self.checkout(self.state.with_deltas(deltas, precision))
205
+
206
+ def _write(self, name: str, value: torch.Tensor, *, promote: bool) -> None:
207
+ param = self.params[name]
208
+ with torch.no_grad():
209
+ if param.dtype != value.dtype:
210
+ param.data = value.to(param.device)
211
+ self._promote_module(name, enable=promote)
212
+ else:
213
+ param.copy_(value)
214
+
215
+ # ------------------------------------------------------------------ hashes
216
+
217
+ def hash(self, name: str) -> str:
218
+ if name not in self._hashes:
219
+ info = self.ir.params.get(name)
220
+ untouched = name not in self.state.tensors()
221
+ if untouched and info is not None and info.hash:
222
+ self._hashes[name] = info.hash
223
+ else:
224
+ self._hashes[name] = tensor_hash(self.params[name])
225
+ return self._hashes[name]
226
+
227
+ def verify(self, expected: dict[str, str]) -> list[str]:
228
+ """Names whose live hash differs from ``expected`` (should always be empty)."""
229
+ return [name for name, h in expected.items() if self.hash(name) != h]
230
+
231
+ # ------------------------------------------------------------------ precision modes
232
+
233
+ def _promote_module(self, name: str, *, enable: bool) -> None:
234
+ """Run a module holding an fp32 tensor in fp32, casting activations in and out."""
235
+ module_path = (
236
+ self.ir.params[name].module if name in self.ir.params else name.rpartition(".")[0]
237
+ )
238
+ module = self.model.get_submodule(module_path)
239
+ key = f"promote:{module_path}"
240
+ for handle in self._hooks.pop(key, []):
241
+ handle.remove()
242
+ if not enable:
243
+ for pname, p in module.named_parameters(recurse=False):
244
+ full = f"{module_path}.{pname}" if module_path else pname
245
+ if full in self._dtype and p.dtype != self._dtype[full]:
246
+ p.data = p.data.to(self._dtype[full])
247
+ return
248
+ out_dtype = self._dtype[name]
249
+ for p in module.parameters(recurse=False):
250
+ if p.is_floating_point() and p.dtype != torch.float32:
251
+ p.data = p.data.float()
252
+
253
+ def pre(_m: torch.nn.Module, args: tuple[Any, ...]) -> tuple[Any, ...]:
254
+ return tuple(
255
+ a.float() if isinstance(a, torch.Tensor) and a.is_floating_point() else a
256
+ for a in args
257
+ )
258
+
259
+ def post(_m: torch.nn.Module, _a: Any, out: Any) -> Any:
260
+ return out.to(out_dtype) if isinstance(out, torch.Tensor) else out
261
+
262
+ self._hooks[key] = [
263
+ module.register_forward_pre_hook(pre),
264
+ module.register_forward_hook(post),
265
+ ]
266
+
267
+ def _sync_adapters(self, state: TreeState) -> None:
268
+ """Install low-rank side paths for adapter-mode deltas (``y += scale · x V Uᵀ``)."""
269
+ for key in [k for k in self._hooks if k.startswith("adapter:")]:
270
+ for handle in self._hooks.pop(key):
271
+ handle.remove()
272
+ for name, oids in state.adapter.items():
273
+ deltas = [self.delta(o) for o in oids]
274
+ role = self.ir.primary_role(name)
275
+ info = self.ir.params[name]
276
+ if role is None or role.in_dim is None or len(info.shape) != 2:
277
+ raise PatchError(f"{name}: adapter mode needs a 2-D projection weight")
278
+ for d in deltas:
279
+ if d.kind != "lowrank" or d.index is not None:
280
+ raise PatchError(f"{name}: adapter mode needs whole-matrix low-rank deltas")
281
+ module = self.model.get_submodule(info.module)
282
+ in_dim = role.in_dim
283
+
284
+ def hook(
285
+ _m: torch.nn.Module,
286
+ args: tuple[Any, ...],
287
+ out: Any,
288
+ ds: list[TensorDelta] = deltas,
289
+ ind: int = in_dim,
290
+ ) -> Any:
291
+ x = args[0]
292
+ extra = torch.zeros_like(out)
293
+ for d in ds:
294
+ assert d.u is not None
295
+ assert d.v is not None
296
+ u = d.u.to(x.device, x.dtype)
297
+ v = d.v.to(x.device, x.dtype)
298
+ a, b = (u, v) if ind == 0 else (v, u)
299
+ extra = extra + (x @ a) @ b.T * d.scale
300
+ return out + extra
301
+
302
+ self._hooks[f"adapter:{name}"] = [module.register_forward_hook(hook)]
wspx/loops.py ADDED
@@ -0,0 +1,127 @@
1
+ """Loop detection (§5.14.4): a loop is the agent repeating itself, not working long.
2
+
3
+ Detectors look only at repetition, never at duration, turn count or spend, so
4
+ a long experiment that keeps doing new things is never interrupted.
5
+
6
+ ============= ==================================================================
7
+ call the same tool with identical arguments N times within W turns
8
+ outcome the same diagnostic N times running, or successive no-op edits
9
+ output the same (normalised) assistant message N times within W turns
10
+ error the same error recurring N times within W turns
11
+ empty N turns running with no tool call and no new content
12
+ ============= ==================================================================
13
+
14
+ The first detection of a pattern nudges; if that pattern repeats again, the
15
+ verdict is stop. Resuming is the user's decision.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import hashlib
21
+ import json
22
+ import re
23
+ import threading
24
+ from collections import defaultdict, deque
25
+ from dataclasses import dataclass, field
26
+ from typing import Any, Literal
27
+
28
+ Kind = Literal["call", "outcome", "output", "error", "empty"]
29
+ CONSECUTIVE: frozenset[str] = frozenset({"outcome", "empty"})
30
+
31
+
32
+ @dataclass(frozen=True)
33
+ class Verdict:
34
+ action: Literal["nudge", "stop"]
35
+ kind: str
36
+ key: str
37
+ count: int
38
+ evidence: tuple[str, ...]
39
+ message: str
40
+
41
+ def to_dict(self) -> dict[str, Any]:
42
+ return {**self.__dict__, "evidence": list(self.evidence)}
43
+
44
+
45
+ def fingerprint(value: Any) -> str:
46
+ """Stable short hash for tool arguments or messages."""
47
+ if isinstance(value, str):
48
+ text = re.sub(r"\s+", " ", value.strip().lower())
49
+ else:
50
+ text = json.dumps(value, sort_keys=True, default=str)
51
+ return hashlib.sha256(text.encode()).hexdigest()[:16]
52
+
53
+
54
+ @dataclass
55
+ class _SessionState:
56
+ events: dict[str, deque[tuple[int, str, str]]] = field(
57
+ default_factory=lambda: defaultdict(deque)
58
+ )
59
+ turn: int = 0
60
+ nudged: dict[tuple[str, str], int] = field(default_factory=dict)
61
+ run: dict[str, tuple[str, int]] = field(default_factory=dict)
62
+
63
+
64
+ _MESSAGES = {
65
+ "call": "{label} was called with identical arguments {n} times; the result will not change. "
66
+ "Try different arguments, a different tool, or step back and re-plan.",
67
+ "outcome": "the same result ({label}) came back {n} times running; repeating the step will not fix it. "
68
+ "Apply one of the fixes listed with the diagnostic.",
69
+ "output": "you have sent the same message {n} times. Do something different or stop.",
70
+ "error": "the same error ({label}) occurred {n} times. Change the approach rather than retrying.",
71
+ "empty": "{n} turns in a row made no tool call and added nothing new. Act, or finish.",
72
+ }
73
+
74
+
75
+ class LoopDetector:
76
+ def __init__(self, *, threshold: int = 3, window: int = 10, enabled: bool = True) -> None:
77
+ self.threshold = threshold
78
+ self.window = window
79
+ self.enabled = enabled
80
+ self._sessions: dict[str, _SessionState] = defaultdict(_SessionState)
81
+ self._lock = threading.Lock()
82
+
83
+ def turn(self, session: str) -> None:
84
+ """Advance the session's turn counter (one model response)."""
85
+ with self._lock:
86
+ self._sessions[session].turn += 1
87
+
88
+ def observe(self, session: str, kind: Kind, key: Any, *, label: str = "") -> Verdict | None:
89
+ if not self.enabled:
90
+ return None
91
+ digest = key if isinstance(key, str) and len(key) == 16 else fingerprint(key)
92
+ with self._lock:
93
+ state = self._sessions[session]
94
+ if kind in CONSECUTIVE:
95
+ last, count = state.run.get(kind, ("", 0))
96
+ count = count + 1 if last == digest else 1
97
+ state.run[kind] = (digest, count)
98
+ evidence = tuple([label or digest] * count)
99
+ else:
100
+ events = state.events[kind]
101
+ events.append((state.turn, digest, label))
102
+ while events and events[0][0] <= state.turn - self.window:
103
+ events.popleft()
104
+ same = [e for e in events if e[1] == digest]
105
+ count = len(same)
106
+ evidence = tuple(e[2] or e[1] for e in same)
107
+ marker = (kind, digest)
108
+ if marker in state.nudged:
109
+ if count > state.nudged[marker]:
110
+ return Verdict(
111
+ "stop",
112
+ kind,
113
+ digest,
114
+ count,
115
+ evidence,
116
+ f"stopped: still repeating after a nudge ({kind})",
117
+ )
118
+ return None
119
+ if count >= self.threshold:
120
+ state.nudged[marker] = count
121
+ text = _MESSAGES[kind].format(label=label or kind, n=count)
122
+ return Verdict("nudge", kind, digest, count, evidence, text)
123
+ return None
124
+
125
+ def reset(self, session: str) -> None:
126
+ with self._lock:
127
+ self._sessions.pop(session, None)