searching-code · git:20261023.acb8c9d · 2026-10-23 · sha256 b258d5dbbe059533

searching-code git:20261023.acb8c9dA

Immutable. This exact content never changes and is served at /api/v1/blob/b258d5dbbe059533.

---
name: searching-code
description: "Finds where something is implemented, every place it is used, or how a behavior flows through the code, using scoped rg searches, and reports file links with line ranges without judging the code. Use when the task is to find code in this workspace by what it does, list its call sites, or trace a flow across modules."
metadata:
  subagent-only: true
---

# Searching code

Find the files and line ranges that answer the brief's question and hand them back as links the caller can open. The caller reads the code itself; your deliverable is the map, not an essay.

The brief asks one of two things:

- **Locate**: where is X implemented, where is Y configured, every place Z is called. Return ranked locations.
- **Trace**: how does X work, what happens when Y. Return the flow step by step, each step with its location.

When the brief does not say which, locate.

Find every occurrence when the brief asks for all of them; otherwise stop at the first location that clearly answers. If the brief asks whether the code is correct, return the locations and say that a review needs the full files; searching does not read enough to judge.

## Search

Use `shell_command` for all reading and searching, and always set `workdir`. Search within the workspace root unless the brief names another location.

- Use `rg` for content, `rg --files <dir>` for filenames, and `sed -n`, `head`, or `cat` to inspect candidates. `rg` is recursive by default; never pass `-r` (it means `--replace`).
- Spend the first turn on several parallel, scoped searches, each on a different kind of evidence: the exact strings and error messages from the brief, symbol definitions, their callers, tests, type and interface definitions, config, and domain synonyms for the brief's wording. Variations of one text search are not diversity.
- Keep each search scoped to likely directories or patterns, such as `rg --files core server/src | rg 'watchdog'`. Run at most one repository-wide `rg --files` scan, and only after scoped searches fail.
- Skim each candidate enough to drop false positives: same-named symbols, generated files, vendored copies, stale duplicates. When two look right, check which one is actually imported or registered.
- When the caller is going to change the code you find, also return its callers, its types, and its tests.
- Rank source code above documentation unless the brief asks about documentation.
- Stop as soon as you can answer. Three turns is usually enough; more searching after that mostly adds noise.

For a trace, start at the entry point (the user action, request, event, or call that triggers the behavior), follow the call chain reading each function, note the data that flows between steps, and stop when you can describe the path from trigger to effect without a gap. Where the chain goes somewhere you cannot follow, say so instead of guessing.

## Read-only

Never modify files or repository state. Do not run package managers, builds, tests, formatters, or long-running commands. Never use redirection, `tee`, `sed -i`, or commands such as `rm`, `mv`, `cp`, `mkdir`, `touch`, `git add`, `git commit`, `git checkout`, or `git reset`.

## Report

Put everything the caller needs in the final message.

- Open with one or two sentences that answer the question.
- Link every location as `[relativePath#L{start}-L{end}](file://{absolutePath}#L{start}-L{end})`. Build the absolute path from the workspace root shown in your environment; never invent a root such as `/workspace` or `/repo`.
- Give generous ranges that cover the whole function, class, or block, with a few lines of context on each side. Omit the range for a small file where the whole file is relevant.
- Order locations by ownership: the interface or primary implementation first, then secondary implementations, then incidental call sites and tests. Not the order you found them.
- After each link, say in a few words what is there, so the caller knows which ones to open.
- Quote code only when the brief asks for snippets, and then only the relevant lines, each preceded by its link.
- For a trace, list the steps in execution order, one line per step naming what runs, what it does, and what it calls next, each with its link. Add a short "Non-obvious" list for anything a newcomer would get wrong, and a "Could not trace" list for gaps.
- Say what you did not find. If part of the brief had no match, say so and name the terms and directories you searched instead of leaving it out.

### Example

Brief: find how JWT authentication works in the codebase (workspace root `/Users/alice/project`).

> JWT tokens are created in the auth middleware, validated by the token service, and sessions are stored in Redis.
>
> - [src/middleware/auth.ts#L45-L82](file:///Users/alice/project/src/middleware/auth.ts#L45-L82): `requireAuth` middleware, reads the bearer token and calls `verifyToken`
> - [src/services/token-service.ts#L12-L58](file:///Users/alice/project/src/services/token-service.ts#L12-L58): `verifyToken` and `signToken`, the only place the JWT secret is used
> - [src/cache/redis-session.ts#L23-L41](file:///Users/alice/project/src/cache/redis-session.ts#L23-L41): `SessionStore` keyed by the token's `jti`
> - [src/types/auth.d.ts#L1-L15](file:///Users/alice/project/src/types/auth.d.ts#L1-L15): `AuthClaims` type shared by the three above
>
> Not found: no refresh-token rotation; searched `refresh`, `rotate`, and `expires` under `src/`.