# web_fetch
Status: built 2026-11-27 (ticket 90, first half; search follows). Tier: bundled (`src/webfetch.c`,
`src/htmltext.c`), over the transport's web mode in core (`src/net.c`). Clock: taste. Record: none
new; a call is an effect, replay-safe. Default: on.
The tool is a registration (`pic_webfetch_tool`, [the tool registry](../design/keel.md)): a GET of
one URL through `pic_http_get`, redirects followed by the tool, not the transport, so every hop is
parsed, checked and counted by one function.
## The address check
A model chooses the URL, so the fetch must not reach the user's machine or network. The check is
`pic_net_web` in `net.c`, made on each address `pic_net_connect` is about to connect to, after the
name lookup. Checking the connected address, and not the URL's host, is what defeats a name that
resolves to a private address, a name whose answer changes between a check and the connect, and a
redirect: each hop is a new connection with its own check. Refused: IPv4 0/8, 10/8, 100.64/10,
127/8, 169.254/16 (metadata included), 172.16/12, 192.0.0/24, 192.0.2/24, 192.88.99/24,
192.168/16, 198.18/15, 198.51.100/24, 203.0.113/24 and 224/3; IPv6 outside 2000::/3 (so fc00::/7,
fe80::/10, ff00::/8, ::1 and ::), Teredo, 2001:db8::/32; and the IPv6 forms that carry an IPv4
address (::ffff:a.b.c.d, 64:ff9b::/96, 2002::/16) by that address. `wf_parse` also writes an IPv4
host given as a number, in octal or hex, or short (`127.1`) as its dotted quad, so every resolver
reads it alike. A URL with a user name or password is refused.
Web mode also lets plain `http://` go to any host, which the transport otherwise allows to loopback
only. It is off unless the tool turns it on for its own connection.
Test seams (dev builds only, never in a release build): `PIC_TEST_WEB_LOOPBACK=PORT` lets the one
loopback port of the fake server through the check; `PIC_TEST_WEB_DEADLINE=SECONDS` shortens the
30 second deadline. `test/pic/webfetch.sh --no-faults` checks that a release build refuses
loopback.
## Limits
Five redirects (the sixth fails), 5 MB of body (refused on a Content-Length over it, and when a
body without one grows past it), 30 seconds for the whole call across all hops, text types only
(`text/*`, JSON, XML, JavaScript, YAML, and a body with no type that has no NUL byte). The result
is cut to `max_chars`; the whole converted text goes to `pic_tool_hooks.output`, kept as
`pic:output/N` for `read`.
## web_search
Status: built 2026-11-27 (ticket 90, second half). Tier: bundled (`src/websearch.c`). Clock: vendor
(Brave's API). Record: none new; a call is an effect, replay-safe. Default: on for every model (with no Brave key it searches Stack Overflow, below).
One GET of `https://api.search.brave.com/res/v1/web/search?q=...&count=N` with the key in
`X-Subscription-Token`. The key is the credential `brave-search` (`pic login brave-search`, which
`known_provider` in `main.c` accepts through `pic_model_keyname`, as the terminal's `/login` and
`auth status` do; routing rules and imports refuse it) or `BRAVE_API_KEY`, read through `pic_cred_get`, wiped after the call and scrubbed from any error. Results are taken with
SQLite's JSON functions, so a reply that is not JSON, or has no `web.results`, is an answer and not
a crash; each field goes through `pic_web_utf8` (controls dropped) and `pic_html_plain` (Brave puts
`<strong>` and entities in snippets). Caps: 10 results, 400 query characters, 2 MB, 20 seconds.
`PIC_TEST_WEB_SEARCH_URL` (dev builds only) replaces the endpoint, so the tests use the fake server.
### Stack Exchange, with no key
Status: built 2026-11-29 (ticket 119). Tier: bundled (`src/websearch.c`). Clock: vendor (Stack
Exchange's API 2.3). Record: picu_meta `stackexchange_backoff_until` (epoch seconds). Default: on.
With no Brave key (none sealed under the default name, `BRAVE_API_KEY` unset), `ws_run` makes one GET
of `https://api.stackexchange.com/2.3/search/excerpts?order=desc&sort=relevance&site=stackoverflow&pagesize=N&q=...`
with no key. The API compresses every reply whatever the request says, so the request sends
`Accept-Encoding: gzip` and sets the response's `gzip_ok`: `http.c` then takes `Content-Encoding:
gzip` (it refuses every other encoding, and gzip from callers that did not ask) and the tool
inflates the body with the vendored zlib under the same 2 MB cap. A reply's `backoff` (seconds,
capped at a day) is written to picu_meta and checked before each search, since tool calls run in
forked processes and Stack Exchange bans an address that ignores it; `error_id` 502
(throttle_violation) is the 300-a-day quota. Excerpts carry no link: it is built from
`answer_id` (`/a/`) or `question_id` (`/q/`). `PIC_TEST_STACKEXCHANGE_URL` (dev builds only)
replaces the endpoint. Because a keyless source always exists, `ws_on` is always true: the tool
is offered to every model.
### The provider's own search
`websearch_native.c` (bundled, clock vendor) decides, per request, whether an adapter declares the
provider's server tool in place of the web_search function. `pic_websearch_native` takes the
session's tools and returns them without the function, plus the declaration in the wire shape; each
adapter puts it in its tools array (first for Anthropic, and for a Chat Completions model that takes
cache marks, so the mark on the last tool stays on a function; last otherwise). It applies only when:
- the function is in the session's tools (so `--exclude-tools web_search` still removes it);
- the setting `webSearch` is auto and offline is off;
- the conversation's permission mode is allow or ask-edits (pic cannot gate a call the provider
makes, so the gating modes get the function);
- the provider and sign-in are known to search: Anthropic, OpenAI with an API key, xAI, OpenRouter.
What a reply keeps: Anthropic's `server_tool_use` and `web_search_tool_result` blocks and a text
block's `citations` (decoded from `citations_delta`); Responses' `web_search_call` items (replayed
with the reasoning item before them, which the API takes only with the item that followed it) and
`url_citation` annotations; OpenRouter's `delta.annotations` (shown, not replayed: the assistant
message goes back as it came). The neutral content, which the transcript and other models see, gets
a "Searched the web for" line (the call's `query`, or its `queries` joined) and a "Sources" list.
The web text in them (titles, addresses, queries) goes through the SQL function `pic_strip`
(`stripctl.h`, registered on the decoder's connection), which removes what a terminal could be
steered by (C0 but newline and tab, DEL, C1, the bidi controls); nothing else is filtered, so a tool
call's arguments and the model's own words come back as sent. The native form stays verbatim.
A request that does not declare the provider's search (a mode that asks or refuses, `webSearch`
brave, offline) sends earlier searches as text. Anthropic: `pic_websearch_demote` turns the search
blocks into the query and the sources as two text blocks and drops a text block's citations.
Responses: a turn whose native form holds a search call is rebuilt from its neutral form, as for a
model switch (no reasoning items, no item ids; the call ids of its results follow), so a function
call in the same turn does not lose the reasoning item it needs. Another provider gets the neutral
form anyway.
Not built: Gemini (search shares a request with function tools only from Gemini 3 with
`includeServerSideToolInvocations`, and the server-side tool parts must be passed back, which the
decoder does not keep) and the ChatGPT sign-in (unknown whether its token may search; it gets the
function). A search the provider runs is not a call pic makes: hooks, the secret guard and the
permission gate never see it.
Anthropic's tool version: the docs list `web_search_20250305` (basic), `web_search_20260209` (adds
dynamic filtering through code execution, Claude 4.6 and later, and its `allowed_callers` defaults to
code execution) and `web_search_20260318` (adds `response_inclusion`). pic declares the basic one:
it works on every model and on the cloud hosts that offer only it, and the later ones change the
shape of a turn (code-execution blocks, callers on tool calls) that the decoder and replay have not
been checked against. Moving to `web_search_20260318` with `allowed_callers` `["direct"]` for models
that support it is one string in `websearch_native.c` once it can be tried against the real API.
It is not a registered read-only tool (`read_only` 0): the query leaves the machine.