# 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 when a Brave key exists (the sealed row or the variable, no unlock needed) or the model in use
can search through its provider (`pic_toolset_model`, set when a request freezes the session's tools).

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.

### 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.

`pic_toolset_model` tells the registry which model the next request goes to (the loop sets it when
the first request freezes the session's tools), so web_search is on by default for a model that can
search through its provider, or when a Brave key exists, and not for any provider key in the
environment.

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,
passed through `pic_json_strip_controls` (`stripctl.h`), which removes what a terminal could be
steered by as the text decodes (C0 but newline and tab, DEL, C1, the bidi controls). 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: `pic_websearch_demote` turns Anthropic's search
blocks into the query and sources as two text blocks and drops a text block's citations, and drops
a Responses search call with the reasoning item before it. Another provider gets the neutral form.

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.
