Skip to content

Commit 2baa123

Browse files
fylornclaude
andauthored
Fix what the pre-release review of the guard unification found (#75)
* Fix what the pre-release review of the guard unification found - Converting the old guard settings happens once. The first start records it in `security.legacy_converted`; old keys or the old column showing up after that (an older version started against the database writes its seeded defaults back) are removed with a warning, never converted over the policies in force. Whatever the conversion leaves out is logged. - Audit events carry no text of the request. A content event names the rule and counts the matches. A redaction event is one per rule per request, with how many values and occurrences; only a built-in rule's lists a few in masked form, a custom rule's values are not written. At most 20 events per guard per request. - Policy limits are back: 500 custom content rules of up to 500 characters, 100 redaction and 100 tool-call rules of up to 1,000, and no custom tool-call rule named like a built-in one. - Writing a guard policy takes `settings:write` and the guard's own permission, as 2.2 required across server and console; the console's write check asks for both. - The output cap is applied per hop: a limit the request carries is lowered, and one it does not is filled in only within what the model's family is known to take. Converted caps are kept within it too. The cache key carries the cap. A model request still setting `output_guardrails` is refused instead of ignored. - CHANGELOG: what to know before upgrading (no way back to 2.2, backups, stopping replicas under Helm, stricter built-in PII rules, renamed metrics, error type, permissions), and the Fixed entry corrected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Store a converted output cap as it converts The upgrade lowered a converted cap to the 8,192 tokens (32,000 for Claude) the gateway fills in for a request that sets no limit. That lowered the limit of every request that set one above it too: a 100,000-byte cap on a non-Claude model, 25,000 tokens, started cutting answers at 8,192, a tightening on upgrade alone. The cap is now stored as it converts. The runtime rule stays: a request's own limit is lowered to the cap, and a request with none gets the cap only when it is within that family figure. The changelog says what that means for requests that set no limit, and how to hold them to a cap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Use core v0.59.0: the output cap knows OpenAI's own endpoint thinkwatch-core v0.59.0 (ThinkWatch-Core#279) fixes three edge cases of cap_max_output_tokens, which now takes whether the hop goes to the vendor's own endpoint: - a Chat request that sets both max_tokens and max_completion_tokens has both lowered, not only the one the decoder reads first; - one that sets neither is given max_completion_tokens on OpenAI's own endpoint, whose reasoning models refuse max_tokens, and max_tokens elsewhere; - an Anthropic request with extended thinking has its budget lowered below the cap too, or thinking dropped when the cap is 1,024 tokens or less. It also makes outbound redaction linear in the number of secrets in a request. The gateway passes the same official flag the conversion gets. The model form's hint and the changelog now say when a request that sets no limit gets the cap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8d8d96f commit 2baa123

20 files changed

Lines changed: 1340 additions & 222 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 132 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,59 @@ deploying.
2525

2626
### Read before upgrading
2727

28+
- **Back up `system_settings` and `models` first, and plan for no way
29+
back to 2.2.** The upgrade converts the guard settings and drops a
30+
column (next item). Version 2.2 cannot run on the converted database:
31+
its settings are gone, so it would start on its seeded defaults, and
32+
it can no longer read the models table. Take a copy before deploying:
33+
34+
```sh
35+
pg_dump --data-only --table=system_settings --table=models "$DATABASE_URL" > thinkwatch-2.2-guards.sql
36+
```
37+
38+
- **Stop every replica of 2.2 before the first 3.0 one starts.** A 2.2
39+
replica still running when 3.0 converts finds its settings gone (it
40+
then filters and redacts nothing) and cannot rebuild its router; one
41+
that restarts writes its seeded defaults back. 3.0 does not convert
42+
those a second time — it removes them at its next start and logs a
43+
warning — but the 2.2 replica runs on them until then. With the Helm
44+
chart (release `thinkwatch`, namespace `thinkwatch` here):
45+
46+
```sh
47+
kubectl -n thinkwatch scale deployment/thinkwatch-server --replicas=0
48+
kubectl -n thinkwatch wait --for=delete pod \
49+
-l app.kubernetes.io/name=think-watch,app.kubernetes.io/component=server --timeout=5m
50+
helm upgrade thinkwatch deploy/helm/think-watch -n thinkwatch # with your usual values
51+
kubectl -n thinkwatch scale deployment/thinkwatch-server --replicas=<replicas you run>
52+
```
53+
54+
The last command matters when `autoscaling.enabled` is on: the chart
55+
then leaves the replica count alone, and it stays at 0. With Docker
56+
Compose, `docker compose stop server` before pulling and starting the
57+
new image.
2858
- **The old guard settings are converted at the first start, and
2959
behave as before.** `security.content_filter_patterns`,
3060
`security.hidden_text`, `security.pii_redactor_patterns` and
3161
`security.tool_inspection` become `security.content`,
3262
`security.redact` and `security.inspect_tools`, and are deleted, in
33-
one transaction during the boot migration; a second start finds
34-
nothing to convert. A content rule identical to a built-in rule
35-
becomes that rule, switched on, any other a custom rule; a list with
36-
rules in it runs in enforce mode with every built-in rule it did not
37-
name switched off. The four seeded PII patterns become the built-in
38-
rules for the same data (`cn-resident-id`, `bank-card`, `email`,
39-
`cn-mobile-phone`), any other pattern a custom rule whose label is
40-
its old placeholder prefix. A model's `output_guardrails` length cap
41-
becomes `max_output_tokens` (below), and the column is dropped. **Stop
42-
every replica of the previous version before the first new one
43-
starts**: a replica still running 2.2 finds its settings gone (it
44-
then filters and redacts nothing) and can no longer rebuild its
45-
router once the column is dropped. To see what was converted, read
46-
the three keys from Settings or `system_settings` afterwards; the
47-
start-up log lists them too.
63+
one transaction during the boot migration. The conversion is recorded
64+
in `security.legacy_converted` (when, which version, what it
65+
converted); with that record present, old keys or the old column
66+
that show up again are removed, never converted, so the policies in
67+
force are not overwritten. A content rule identical to a built-in
68+
rule becomes that rule, switched on, any other a custom rule; a list
69+
with rules in it runs in enforce mode with every built-in rule it did
70+
not name switched off. The four seeded PII patterns become the
71+
built-in rules for the same data (`cn-resident-id`, `bank-card`,
72+
`email`, `cn-mobile-phone`), any other pattern a custom rule whose
73+
label is its old placeholder prefix. Whatever the old runtime was
74+
skipping (a rule that did not compile, a built-in rule id it did not
75+
know, an `output_guardrails` value it could not read) is left out,
76+
each with a warning in the start-up log. A model's
77+
`output_guardrails` length cap becomes `max_output_tokens` (below),
78+
and the column is dropped. To see what was converted, read the three
79+
keys from Settings or `system_settings` afterwards; the start-up log
80+
lists them too.
4881
- **Placeholders are written `<<TW_EMAIL_1>>`, not `{{EMAIL_1}}`.** The
4982
label of a custom rule is upper case letters, digits and
5083
underscores (an old prefix is converted: `REDACTED-SSN` →
@@ -67,6 +100,14 @@ deploying.
67100
console's security page. With an empty PII list, redaction
68101
converts to observe mode: it records what it finds and changes
69102
nothing.
103+
- **The built-in rules that replace the seeded PII patterns are
104+
stricter.** An identity number has to have a real province code, a
105+
real date of birth and a matching check digit; a card number a known
106+
network's prefix and length and a valid Luhn digit, and published
107+
test card numbers do not count. Numbers the old regexes took for
108+
them — any 18 digits, any 16 — are no longer replaced. A deployment
109+
that relied on the looser match can add its old regex back as a
110+
custom rule.
70111
- **Tool calls are judged as the client receives them, and two built-in
71112
rules are new.** Inspection now reads a tool call converted to the
72113
caller's format and with redacted values restored — what the client
@@ -81,42 +122,94 @@ deploying.
81122
characters are content filter rules: `unicode-tags` and
82123
`bidi-controls`, plus `zero-width` and `private-use`, which ship off.
83124
`security.hidden_text: block` converts to those two rules refusing,
84-
`warn` and `log` to recording, `off` to switching them off.
125+
`warn` and `log` to recording, `off` to switching them off. Recording
126+
writes an audit event: `hidden_text: log` used to reach only the
127+
application log, and now writes `gateway.content_flagged`.
128+
- **A deployment with no content rules starts recording.** An empty
129+
content filter list converts to observe mode with the built-in rules
130+
that ship on, so requests matching them (`ignore previous
131+
instructions` and the like) write `gateway.content_flagged` events
132+
where 2.2 wrote nothing. Nothing on the wire changes.
85133
- **The output length guardrail is replaced by a model's maximum output
86-
tokens.** A cap of N bytes on the answer converts to `ceil(N / 4)`
87-
output tokens. The answer is no longer measured or cut: a request
88-
asking for more tokens than the cap is lowered to it, and one asking
89-
for none gets it, in whichever field its API uses; the upstream stops
90-
there. The model API's `output_guardrails` field is gone;
91-
`max_output_tokens` (1 to 2147483647, `null` for no limit) replaces
92-
it.
134+
tokens — check each model's after upgrading.** A cap of N bytes on
135+
the answer converts to `ceil(N / 4)` output tokens, stored as
136+
converted. The answer is no longer measured or cut: a request asking
137+
for more tokens than the cap is lowered to it, in whichever field its
138+
API uses (a Chat request that sets both `max_tokens` and
139+
`max_completion_tokens` has both lowered), and the upstream stops
140+
there; one asking for less keeps its own. A request that sets no limit
141+
is held to the cap only when the cap is within what the gateway knows
142+
the model's family to take (32,000 tokens for Claude models, 8,192 for
143+
others); a Chat request is then given `max_completion_tokens` on
144+
OpenAI's own endpoint, whose reasoning models refuse `max_tokens`, and
145+
`max_tokens` elsewhere. Above that figure, the request goes out
146+
without a limit rather than with one the model could refuse, and the
147+
model's own default applies: a 100,000-byte cap on a non-Claude model
148+
converts to 25,000 tokens, so its requests that set no limit are not
149+
capped at all. To hold every request to a cap, have the clients send
150+
a limit, or set the cap to that family figure or below. Reasoning
151+
(thinking) tokens count towards the cap on the APIs that bill them as
152+
output, so a cap that fit an answer can cut short a model that thinks
153+
first. An Anthropic request with extended thinking has its thinking
154+
budget lowered below the cap too, or thinking turned off when the cap
155+
is 1,024 tokens or less, the smallest budget Anthropic takes. The
156+
model API's `output_guardrails` field is gone: a request that still
157+
sets one (anything but `null` or `[]`) is refused with `400`, so a
158+
script cannot believe answers are still capped. `max_output_tokens`
159+
(1 to 2147483647, `null` for no limit) replaces it.
93160
- **A new installation observes by default.** Every guard starts in
94161
observe mode, with only the built-in rules that rarely misfire
95162
switched on (personal data such as e-mail addresses and phone
96163
numbers ships off). Nothing is refused, replaced or deleted until a
97164
guard is switched to its third mode.
98165
- **A content filter refusal is `403`**, with the error type of the
99166
caller's API (`permission_error` for OpenAI-style APIs). Keyword and
100-
regex rules used to refuse with `400`.
101-
- **Guard policies are changed with their own permissions.** Writing
102-
`security.redact` through `PATCH /api/admin/settings` takes
103-
`pii_redactor:write`, `security.content` and `security.inspect_tools`
104-
take `content_filter:write`; `settings:write` no longer covers them.
105-
The seeded `admin` and `super_admin` roles hold both.
167+
regex rules used to refuse with `400`. In `gateway_logs`, its
168+
`error_type` is `PolicyBlocked`, where it was `TransformError`.
169+
- **A rule that deletes text changes what is stored.** The request a
170+
content rule stripped goes upstream, is redacted and is captured as
171+
the stripped one: the audit log's request body is what was sent, not
172+
what the caller typed.
173+
- **Changing a guard policy takes `settings:write` and the guard's own
174+
permission**: `pii_redactor:write` for `security.redact`,
175+
`content_filter:write` for `security.content` and
176+
`security.inspect_tools`, through `PATCH /api/admin/settings`. 2.2
177+
checked `settings:write` on the server and the guard permission in
178+
the console; both are checked on the server now. Trying a sample
179+
takes `pii_redactor:read` or `content_filter:read`. Reading the
180+
policies — `GET /api/admin/security`, which the console's security
181+
page loads — takes `settings:read`. The seeded `admin` and
182+
`super_admin` roles hold all of them.
106183
- **Console API changes.** `GET /api/admin/security` lists each guard's
107184
mode and every rule, and `POST /api/admin/security/{guard}/test` tries
108185
a sample; they replace `/api/admin/settings/content-filter/test`,
109186
`/content-filter/presets`, `/pii-redactor/test`,
110187
`/tool-inspection/rules` and `/tool-inspection/test`, which are gone.
111-
- **Audit events.** Every guard hit writes one event:
112-
`gateway.content_flagged`, `gateway.content_stripped` and
188+
- **Audit events.** Every rule that matches writes one event per
189+
request: `gateway.content_flagged`, `gateway.content_stripped` and
113190
`gateway.content_blocked`; `gateway.redaction_flagged` and
114191
`gateway.redaction_replaced`; `gateway.tool_call_flagged` and
115192
`gateway.tool_call_blocked` as before. `gateway.hidden_text_flagged`
116193
and `gateway.hidden_text_blocked` are gone; hidden characters are
117-
content events. With `audit.body_redact_pii` on, captured bodies are
118-
redacted with the outbound redaction rules, built-in ones included,
119-
whatever the redaction mode.
194+
content events. **No event carries the request's text**: a content
195+
event names the rule, the outcome, how many matches and whether they
196+
were in a tool result; a redaction event names the rule and counts
197+
the values and their occurrences, and only a built-in rule's lists a
198+
few in masked form (`sk-an…7f9c`) — a custom rule's values are not
199+
written at all. A request writes at most 20 events per guard (the
200+
rules that changed it, or matched most, first), each with the number
201+
of rules that matched (`rules_in_request`). With
202+
`audit.body_redact_pii` on, captured bodies are redacted with the
203+
outbound redaction rules, built-in ones included, whatever the
204+
redaction mode.
205+
- **Metrics renamed.** `gateway_hidden_text_total` is gone: hidden
206+
characters count in `gateway_content_matched_total{outcome,custom}`
207+
with every content rule. `pii_pattern_invalid_total{pattern}` is now
208+
`guard_policy_invalid_total{guard}` (a stored policy with a rule that
209+
does not compile; the rule is left out), next to
210+
`guard_policy_unreadable_total{guard}` (a stored policy that cannot be
211+
read; the guard runs on its factory policy). Redaction counts values
212+
in `gateway_redaction_found_total{kind,outcome}`.
120213

121214
### Added
122215

@@ -134,17 +227,17 @@ deploying.
134227

135228
### Changed
136229

137-
- **Core crates at ThinkWatch-Core v0.58.0.** `tw-dialect`, `tw-guard`,
230+
- **Core crates at ThinkWatch-Core v0.59.0.** `tw-dialect`, `tw-guard`,
138231
`tw-breaker` and `tw-bedrock` move from v0.55.0; the shared guard model
139232
described above comes with them.
140233

141234
### Fixed
142235

143236
- **A credential in a matched tool call no longer reaches the audit
144-
log.** The excerpt of a tool call that inspection cut or recorded —
145-
and of a content filter hit — is masked with the redaction rules
146-
before it is written; a key the model echoed, or one restored from a
147-
placeholder, used to be stored as it was.
237+
log.** The excerpt of a tool call that inspection cut or recorded is
238+
masked with the redaction rules before it is written; 2.2 stored the
239+
matched arguments as they were, a key the model wrote in them
240+
included.
148241
- **A request's audit events and its log row carry the same id** when
149242
the caller sends no `x-trace-id`. The log row of a request that went
150243
through used to carry a second, unrelated id.

‎Cargo.lock‎

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ opt-level = 3
7070
# never re-exported through a local shim. And the reverse: something only
7171
# this side uses (the at-rest crypto, IMDSv2 credentials, the gateway error)
7272
# lives here, not in core.
73-
tw-bedrock = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.58.0" }
74-
tw-breaker = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.58.0" }
75-
tw-dialect = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.58.0" }
76-
tw-guard = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.58.0" }
73+
tw-bedrock = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.59.0" }
74+
tw-breaker = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.59.0" }
75+
tw-dialect = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.59.0" }
76+
tw-guard = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.59.0" }
7777

7878
# Web framework
7979
axum = { version = "0.8", features = ["macros", "ws"] }

0 commit comments

Comments
 (0)