# Answering questions over files with a Drive

Source: https://docs.autessa.com/docs/autessascript/guides/drive-agent-files

> Give an agent a folder tree of documents: keep the files in a DRIVE table, attach the drive_* tools (ls, tree, find, stat, grep, read, search, fetch, and write, edit, mkdir, move, trash), and have the agent locate with search or grep, then read the exact page or lines before it quotes, citing path p.N L.M. Agents that keep notes write and edit files: read, edit with exact oldText, re-read; append to a memory file.

# Answering questions over files with a Drive

Give an agent a folder tree of documents: keep the files in a DRIVE table, attach the drive_* tools (ls, tree, find, stat, grep, read, search, fetch, and write, edit, mkdir, move, trash), and have the agent locate with search or grep, then read the exact page or lines before it quotes, citing path p.N L.M. Agents that keep notes write and edit files: read, edit with exact oldText, re-read; append to a memory file.

**The decision:** the agent must answer from documents (contracts, policies, reports,
exports, code) that people organize in folders and share like Google Drive. Put them in a
**Drive** (dbType DRIVE): every file is a node with a path, its text is extracted (OCR for
scans) into numbered pages and lines, and sharing (Viewer / Editor, inherited down folders)
decides what each person and agent can see.

## The tools (one per familiar command)

| Tool | Like | Use it to |
|---|---|---|
| `drive_ls` | `ls` | list one folder (folders first; `cursor` for the next page) |
| `drive_tree` | `tree` | see the folder structure a few levels deep |
| `drive_find` | `find` | locate files by name glob (`*.pdf`, `2026/**/*.csv`), type, date or size |
| `drive_stat` | `stat` | one item's size, version, page count, text status and your access (metadata only) |
| `drive_grep` | `rg` | exact text or a regex in file contents, with page and line numbers |
| `drive_search` | ranked search | files matching most of your words, best passage first (also by meaning when the Drive's semantic search is on) |
| `drive_read` | `cat` / `sed -n` | the line-numbered text of a page range (`pages=3-5`) or line range (`lines=100-250`) |
| `drive_fetch` | `open` | a 10-minute download link; with `view=true` the image (or PDF `pages`) is shown to the model |
| `drive_write` | `>` / `>>` | create a text file, replace it, or append to it (`mode=append`); folders are created |
| `drive_edit` | editor find-and-replace | replace an exact `oldText` (must match once, or `replaceAll=true`) |
| `drive_mkdir` | `mkdir -p` | create folders |
| `drive_move` | `mv` | move into a folder, or rename by giving a new path |
| `drive_trash` | `rm` (recoverable) | move to the trash |

Provision them with drive `create` (the Drive). Drive-bound agents get all thirteen
`drive_*` tools; a `rootPath` on the tools limits the agent to one folder. Writes need
EDITOR on the folder for the person the agent acts for; an unattended agent writes as
the company credential, so always give it a `rootPath`.

## The loop to put in the agent's instructions

1. **Locate.** Exact strings (names, numbers, IDs, clause wording) → `drive_grep`; concepts and
   questions → `drive_search`; file names or dates → `drive_find`. Narrow with `path` (a folder)
   and `glob` (`*.pdf`).
2. **Read before quoting.** Every grep match and search hit carries `path`, `page` and `line`.
   Open that range with `drive_read` (`pages=N` for documents, `lines=a-b` for text files) and
   quote only what you read.
3. **Cite** every fact as `path p.N L.M` (or `path L.M` for text files).
4. **Follow `next` and `hint`.** Each result ends with a `[next: ...]` line (and a `next`
   field) naming the call to make next: the cursor for the next page, the range to read, or
   a narrower search. Errors carry a `hint`: NOT_FOUND names the parent folder to list,
   PATTERN_TOO_BROAD asks for a longer pattern or a narrower path/glob.
5. **Never assume completeness.** A result that says `truncated` or has a `nextCursor` has
   more; follow the cursor or narrow the search. Files still being extracted are reported as
   pending: check them again later with `drive_stat`.

## Writing and editing files

1. **Read first.** `drive_read` the file (or `drive_stat` it for its `version`).
2. **Edit with exact text.** `drive_edit` with `oldText` copied from what you read, without the
   `     12 | ` line-number prefix and with the original spaces and line breaks. If it fails
   with EDIT_NO_MATCH, read again and copy more carefully; with EDIT_AMBIGUOUS, add a
   neighboring line to `oldText` (or set `replaceAll=true` when every match should change).
3. **Re-read** the changed range to check the result.
4. **Whole files** are written with `drive_write` (`mode=create` refuses to overwrite).
   Every write and edit keeps the previous content in the file's version history.
5. **Concurrent writers.** Pass `expectedVersion` (from `drive_stat` or the previous
   write's result); VERSION_CONFLICT means someone else changed the file: read it again and
   retry with `details.currentVersion`.

**Memory files.** For notes that should outlive one conversation, keep one text file
per topic in a folder the agent creates (for example `/agent-memory/notes.md`) and add to it with
`drive_write mode=append` — append starts a new line, never overwrites, and is safe when
two runs append at once. Read it at the start of a task (`drive_read`), and `drive_edit`
an entry when a fact changes. The text is searchable with `drive_grep` as soon as the
write returns.

**Looking at files.** For photos, charts, diagrams and scanned pages, `drive_fetch` with
`view=true` (and `pages=2-3` for a PDF, at most 5 pages) shows them to the model; quote
text only from `drive_read`.

## Paging and locators

- Listings (`ls`, `find`, `grep`, `trash-list`, `shared-with-me`, `pull`) page with an opaque
  `cursor`: pass the previous result's `nextCursor`; `hasMore` is true while one exists.
- Paths are absolute from the Drive root (`/Contracts/acme-msa.pdf`); `/` is the root. A new
  Drive is empty: there are no per-user folders.
  Node ids (`id=` in listings) can replace a path in `stat`, `read`, `ls`, `fetch` and `edit`.
- `read` returns at most 20 pages or 2,000 lines per call; its `next` says what to read after.

## Sharing caveats

- The tools act as the person the agent works for: they see only what that person may see.
  Unattended agents act as the company credential, so give them a `rootPath`.
- A Drive is private to its owner and admins until it is shared. Sharing `/`
  (`shares/upsert` with `path: "/"`) makes people Drive members: they see the Drive but
  no file or folder in it until that item is shared with them. Sharing a folder or file
  gives access to just that (folders include everything inside). To show a folder to
  every member, share it with `principals: "members"`. Adding items at the top level
  needs editor membership; the creator owns what they add.
- Moving an item changes who can see it (access inherits from the new parent): `move`
  returns the access changes and needs `confirm=true` when someone gains or loses access.
- Share links (`links/create`) are read-only; revoking a share takes effect immediately.

## Getting giant files in

Use the CLI (`autessa autessadb drive push --localDir <folder> --path <drive folder>`, or
`drive upload --localFile <file>`) or the `uploads/create` →
`uploads/part-urls` → `uploads/complete-multipart` → `uploads/commit` methods. Files up to
5 TB upload in parallel parts whose size the server picks; an interrupted upload is resumed
with `uploads/resume` (the CLI resumes by default), and `uploads/progress` reports how far
each upload got.

An agent that made a file in its own sandbox (a PDF, an image, a deck) and has no CLI uploads
it without passing the bytes through the conversation: `uploads/create` with the file's
path and size returns a `url` and `headers`; PUT the file to that url with those headers
from the sandbox (`curl -X PUT -T deck.pptx -H "Content-Type: ..." "<url>"`), then
`uploads/commit` with the `uploadId`. One PUT takes files up to 100 MB. `write` with
`contentBase64` puts small files (up to a few hundred KB) inline instead.

When the sandbox can't reach the upload host (the PUT can't connect) and the file is up to
25 MB, send it through the API in chunks: `uploads/create` with `transfer: "CHUNKED"` returns
`chunked.chunkSize` (32 KB by default; `chunkSize` at create picks 4 KB to 1 MB) and
`chunksTotal`; split the file (`split -b <chunkSize> -d -a 5 file chunk_`; `chunk_00000` is
chunk 1), send each chunk with `uploads/append` (`chunkNumber`, `contentBase64`, and its
`sha256` so a chunk that changed on the way is refused and resent), then `uploads/commit`.
Every byte still passes through the conversation, so this is slow for big files: tell the
user how to allow the upload host (`ifUploadBlocked`) as well.

## Verify by executing

Put a document whose content you know in the Drive, wait until `stat` shows its text
EXTRACTED, then run the agent on a question only that document answers and confirm the
transcript shows search or grep → read → an answer citing `path p.N L.M`.

## See also

- *grounding-agent-on-documents* — knowledge bases versus Drives for retrieval.
- *sharing-permissions* — roles, teams and how access is evaluated.
- *analytics-agent-sql* — SQL over large tables (describe → estimate → query → get result).

## Related

- [Autessa SDK overview](/docs/autessascript/sdk.md)

