# search_documents

`search_documents` searches the text of files in the folder the user allowed on the desktop. It does not open a file in an editor. It reads the file from disk, scores it, and sends back a short passage.

The model uses this when it does not yet know the file path. `read_file` and `search_file_content` need a path.

## Request

The server sends this on the open chat stream:

```text
event: tool_request
data: {"callId":"73ba76c4-e3d2-40be-aabd-d6a6d5a0f8e4","name":"search_documents","args":{"query":"architecture boundaries"}}
```

`args` has one field.

| Field | Rule |
|---|---|
| `query` | A string of 2–1000 characters. |

The desktop keeps the stream open, runs the search, and posts the result to:

```text
POST /api/agent/runs/{runId}/tool-result
```

`runId` comes from the earlier `started` event. `callId` is copied from the tool request.

## What the desktop scans

The allowed folder in this example is `/Users/me/Notes`.

```text
/Users/me/Notes
├── docs
│   └── architecture.md
├── notes
│   ├── layers.md
│   └── meeting.md
├── report.pdf
└── archive
    └── huge.md          (1.4 MB)
```

A file is read only when all of these are true:

- The name does not start with `.`
- It is not a symbolic link
- It is not inside `node_modules`, `.git`, `build`, `.dart_tool`, or a name or path listed in `.agentignore`
- It is not a binary file (images, video, audio, archives, fonts, executables, and PDFs are skipped)
- It is within 8 folder levels of the allowed folder
- At most the first 64 MB of a text file is read

`report.pdf` is skipped because it is a binary file. `archive/huge.md` is read. A text file larger than 64 MB contributes its first 64 MB. Nothing from a skipped file is returned.

The walk stops after 5,000 directory entries, or after 40 files have a score above zero. The reply contains the best 5 of those scored files.

## How a file is scored

The query is lowercased and split on spaces. A word of one character is dropped. For `architecture boundaries` the words are:

```text
architecture
boundaries
```

Each word is worth 1 point when it appears anywhere in the file. The match is a substring, and letter case does not matter. The words do not have to be next to each other.

`docs/architecture.md`:

```text
The service has three layers.
The api layer depends on the domain layer.
Architecture boundaries stay between those layers.
A later section covers deployment.
```

| Word | Found |
|---|---|
| `architecture` | yes, in `Architecture` |
| `boundaries` | yes |
| **Score** | **2** |

`notes/layers.md`:

```text
See the architecture notes for the boundary list.
```

| Word | Found |
|---|---|
| `architecture` | yes |
| `boundaries` | no. `boundary` is a different word |
| **Score** | **1** |

`notes/meeting.md`:

```text
We reviewed the documents in the meeting.
```

| Word | Found |
|---|---|
| `architecture` | no |
| `boundaries` | no |
| **Score** | **0** |

A score of 0 leaves the file out. The remaining files are sorted from the highest score to the lowest:

| Rank | File | Score |
|---|---|---|
| 1 | `docs/architecture.md` | 2 |
| 2 | `notes/layers.md` | 1 |

## Which text is sent

The full file is not sent. For each of the top 5 files, the result contains one passage of at most 700 characters.

The passage starts 180 characters before the first word in the query. Here that word is `architecture`. If the file is shorter than the passage, the whole file is sent. If that first word is not in the file, the passage is the first 700 characters and `line` is 1.

`line` is the line that contains that first word. For `docs/architecture.md` the word `architecture` is on line 3, so the result has `"line": 3`. For a long file, only that window is sent. The rest of the file stays on disk. The model can ask for more with `read_file` using the returned `filePath`. `read_file` returns the first 12,000 characters.

## What the scan skips

The scan does not enter hidden names, symbolic links, `node_modules`, `.git`, `build`, or `.dart_tool`. A file named `.agentignore` in the allowed folder adds more exclusions, one name or relative path per line. A name such as `secret` skips that name anywhere. A path such as `docs/private` skips that folder only. Lines starting with `#` are comments. The same list applies to file search, folder listing, the folder tree, and a direct read or write of an excluded path.

## Response

The desktop posts:

```json
{
  "callId": "73ba76c4-e3d2-40be-aabd-d6a6d5a0f8e4",
  "ok": true,
  "result": {
    "query": "architecture boundaries",
    "searchedFolder": "/Users/me/Notes",
    "results": [
      {
        "filePath": "docs/architecture.md",
        "line": 3,
        "content": "The service has three layers.\nThe api layer depends on the domain layer.\nArchitecture boundaries stay between those layers.\nA later section covers deployment."
      },
      {
        "filePath": "notes/layers.md",
        "line": 1,
        "content": "See the architecture notes for the boundary list."
      }
    ]
  }
}
```

`results` has at most 5 items. `filePath` is relative to `searchedFolder`. `line` is where the first query word appears. `content` is the passage, not the file. The server answers `{ "accepted": true }` and the model writes the reply from those passages.

If the search cannot run, the desktop posts `ok: false` and an `error` string. The usual case is that the user has not chosen a folder:

```json
{
  "callId": "73ba76c4-e3d2-40be-aabd-d6a6d5a0f8e4",
  "ok": false,
  "error": "No folder is allowed. Choose a folder in the desktop app before searching files."
}
```
