# Readable IDE results and private records

IDE tools expose readable text in direct calls. In native Codemode, Search returns `{ id, complete, matches: { path, line, text, lineText?, id }[] }`. The top-level `id` selects every match and `matches[i].id` one match. `path` is shown as in a direct call, relative to the session directory when the file lies inside it; `line` is the one-based start line and `text` the whole matched text. Optional `lineText` contains the complete source lines spanned by the match, including text before and after it, without the final LF. A CRLF line keeps its CR, and interior line breaks are preserved; an end position at the start of the next line does not include that untouched line. A source without line context omits `lineText` rather than inventing it. Search, ast_search and lsp_search share this match field; AST captures remain `{ text, id }`. The list holds every match the search found, without the match or text limits of the readable result. No match is `matches: []`; `complete: false` means the search did not cover its scope, so an empty list proves nothing. When the original query has no matches and Search tries a broader mode, top-level `matches` stays empty, top-level `complete` is true, and `fallback` holds `{ mode, id, complete, matches }` for the broader selection; `fallback.complete` describes that broader coverage. Its `id` and match ids select fallback matches, which are location hints; refine the query before editing. Search notices and edit warnings are also available in the value and the parent Codemode result. Possible-name suggestions stay out of the value. A source without selections, such as terminal output, a guide or a web page, gives matches without `id`, and the value has no top-level `id` either. A result that is not a list of text matches, such as a resource provider's object list, returns its readable string. ast_search returns the same value, and each match adds `captures`: the nodes of each metavariable, keyed by its name without `$`, as `{ text, id }`.

A text Read in Codemode returns `{ text, id }`. `text` is exactly the requested text without the envelope, notices or continuation hints: the whole source without `offset` and `limit`, the exact window with them, or the presentation of the requested views, such as anchors, jq output or an AST view. `JSON.parse(r.text)` works on a JSON file, and writing `r.text` back leaves the file unchanged. `id` is the `RESULT#<uuid>` of the result, and the value or its id selects the same text or window as the readable result. A `raw:` Read returns `{ bytes }`, the base64 of every requested byte; a directory returns `{ entries }`, whose directory names end with `/`. An image Read returns Pi's image block `{ type: "image", data, mimeType }`, or `{ type: "images", images }` for several frames, so scripts can pass it to `image()`, `models.classify()` or Write `image` to save it as a file. Read also takes an array `path`: it reads each source on its own and returns an array in the same order, with `null` for each source that failed; the Codemode result lists only the failed sources. These values come from Read's complete data for a call another tool issued: no line, byte or output limit applies, and a direct call keeps its bounded readable text. Diff returns the complete unified diff as a string, without the system-result envelope or the truncation of a direct call, and `""` when the sources are equal. Replace, insert, write, copy, move and undo return the `RESULT#<uuid>` id of the text they produced, or `null` when they select no text; filesystem and text delete returns `null`, while a provider action keeps its value or receipt (see [Tools](./tools.md)). Terminal calls return `{ output, exit_code, truncated, full_output_path, wall_time_seconds, session }`; a nonzero exit code is a value, not a tool failure. Other text tools return strings. Private result records remain hidden.

## Composition

A direct readable result starts with a labelled system-result envelope containing a random UUID. It is not part of a file and must not be edited or inserted into file contents. Codemode values use the forms above instead; a provider's readable string may still carry the direct result reference.

```js
const source = await tools.read({ path: "notes.txt", offset: 10, limit: 20 });
if (source !== null) {
  const matches = await tools.search({ path: source, query: "regex:old\\w*" });
  if (matches !== null) text(await tools.replace({ path: matches, text: "new" }));
}
```

Pass a Read value, a Search value, one of its matches, any of their `id`s, an edit's id or an unchanged result string to a supported source parameter. Direct calls also accept the issued UUID. A source parameter accepts issued values or `{ id }` objects, alone or in a list. Preview metadata never changes the registered selection, and unsupported fields are refused. An id the IDE did not issue fails like an unknown reference, even when it names a file. `file_search` and `semantic_search` take paths only, not results or values. Store/load retains results across scripts in the same session. Displayed item and capture references allow narrower selections without inspecting private arrays.

Selector and content strings keep their text, including valid JSON objects and arrays. Issued reference strings still resolve as references. Pass result values to selectors as native objects or issued IDs.

Source fields also accept JSON-text arrays and issued result-value objects when the literal string does not name an existing path in the call's working directory and the decoded value is valid for that field. An existing literal path takes precedence. Unknown result IDs and unsupported object fields do not acquire reference authority from JSON text. Native objects and arrays avoid this ambiguity.

Fields that accept objects or arrays but do not accept strings also accept JSON text, such as Read `views: '["anchors"]'`. Diff request wrappers use native objects, such as `before: { path: "a.txt", limit: 0 }`. Schema validation and issued-reference ownership checks still apply. These rules apply to direct and Codemode calls, local and SSH sources, and parallel edit batches.

A result identifies its backend-owned ranges, not the text in its preview. Shortened output does not clip a complete selection or make an incomplete selection complete. Read around a selection may show context without granting authority to edit it.

## Reference ownership

The session registry issues UUIDs and maps them to source handles. A guessed or computed hash has no authority unless the registry issued that reference. UUIDs are opaque references, not content hashes or signatures.

The registry validates session/worktree ownership, exact full-output identity, successful source status and live snapshots. Copying a valid ID within its session is intentional reuse, not forgery. Altering the body while keeping the envelope is rejected. An ID cannot grant wider ranges than its original selection.

When a local file changes, its references follow the new text: a selection whose own text is unchanged moves to its new position, and one whose text changed is retired with a message that shows its current line. Every selection of a file is kept on the file's current text, also one registered from a text the tool read before another process wrote the file, so references from one or several searches combine in one edit. A check that finds a local file changed does not retire its other references; they follow the new text on their next use. Changes to other sources, such as URI resources, retire their references permanently; restoring the old bytes does not revive them. Actual edit completions and script boundaries observe filesystem generations; retained source bytes are verified before use. Reload and session changes clear the registry.

References can follow a block that moved while another writer edited its lines. If an edited line has repeated matches, a unique unchanged prefix or suffix through the match can identify it. When the surrounding text cannot tell the copies apart, the edit is refused rather than applied to a guess.

Pending edits reserve source handles without granting authority to unconfirmed writes. A dependent source operation commits the batch before using such a handle. Failed or cancelled edits cannot confirm it. Independent peer edits retain their original snapshots until the common commit.

Read-only views, raw bytes, images and directory listings do not acquire text-edit authority. Their direct result references can identify the resource for another read; raw-byte, image and directory Codemode values do not expose an id. Live shell/debugger references can be used only with their owning resource operations. File deletion supplies no reusable text selection.

## Internal records

Each tool owns its validated record. Read, Search, Diff, terminals, Git and debugger operations keep their own domain adapters. File edit receipts contain only operation, observed effect, file states and an optional verified selection handle. Delete omits selection fields. The parent Codemode result retains committed operation effects and errors.

The text registry stores only ownership, a digest of the issued text, source references, resource identities and whether the result can be consumed. It does not copy line arrays, removed file contents, image bytes or debugger records into a second ledger. Source ranges and snapshots remain in the source-target store.

The unified registration adapter declares a Codemode output schema for every IDE tool, the tool's value or `null`, and hides private domain records. Each tool's value, its schema and its projection from a successful result, lives in that tool's own module in `src/composite/script-values/`, so a change to one tool's value touches one module. A tool without its own module resolves to its readable string. `match.ts` holds the one match type of the values of Search, ast_search and lsp_search; Search, ast_search and lsp_search `query` and `declaration` resolve to lists of it; lsp_search `calls` resolves to `{ id, complete, matches, incoming, outgoing }`, with references in `matches` and each caller and callee as its whole declaration. A call graph registers a selection for its references, its callers and its callees; its `id` selects the references. A direct call's call graph record keeps the same preview of each list. `file_search` resolves to its matching paths as a `string[]`, as a direct call lists them; a call from a script lists every match unless it sets `limit`. Structured results are limited to 1 MiB, but Search core lifts that limit for a script's call (the `script` audience), so such a list is never cut or refused for its size. For the same reason a direct call's Search record keeps a preview of 100 matches with each match-text and line-context field limited to 4 KB in UTF-8 bytes, while a script's call keeps every match with its whole text and available line context. A preview omits an oversized text field and sets `textTruncated: true` without discarding the other field. `semantic_search` resolves to its files in rank order as a `string[]`; approximate ranges stay in the readable text because they are not selections. Public tool_result hooks register the source metadata, preserve readable content for direct calls, and attach only the public script value as structuredContent. Edit tools resolve to the reference of their published result when its record names a verified text target, and otherwise to `null`; a resource provider's value takes precedence. This does not modify Pi, rewrite scripts, add VM methods, or recognize JSON in Codemode output.

Nested image blocks are delivered on the parent Codemode result once; an image the script already showed with `image()` is not attached again. Their bytes are not printed as text. Images remain ordinary native image content.
Read's private native record contains image type and MIME metadata, not another copy of the image bytes. Large images therefore do not overflow the JSON-record budget while their native content stays unchanged.

## Safe provider error metadata

A provider can attach public rate-limit data to `ResourceError` through its fourth constructor argument:

```ts
import { ResourceError } from "pi-agent-ide/api/resource";

throw new ResourceError("SERVICE_RATE_LIMITED", source, "not-applied", {
  metadata: { rateLimit: { retryAfterMs: 60_000, requestsRemaining: 0 } },
});
```

`metadata.rateLimit` accepts `retryAfterMs`, `resetAt`, `requestsLimit`, `requestsRemaining`, `complexityLimit`, `complexityRemaining`, `endpointLimit`, `endpointRemaining` and `endpoint`. Numeric values must be non-negative safe integers; `retryAfterMs` must not exceed 24 hours. `resetAt` is a Unix timestamp in milliseconds. `endpoint` is an identifier of at most 80 characters using letters, digits, underscores, colons or hyphens.

Construction keeps a fresh frozen snapshot of the allowed fields. Unknown keys and malformed values are dropped. Read preserves this metadata while rebuilding safe errors and includes it in `ResultError.metadata`. Arbitrary exception properties, private causes, response bodies and raw headers are excluded. Other tool adapters must opt into the same public metadata contract.

Metadata does not change an operation's effect or authorize a retry. A renderer or provider adapter may present the supplied timing without exposing its private transport diagnostics.

## Errors and writes

In Codemode, a failed IDE call normally resolves to `null`, and the script continues. A multi-source Read instead preserves its ordered array, with `null` only for the failed sources. Failures include refusals such as an expired or unknown result reference and system-result text passed as file content, an edit that changed only some files or whose effect is unknown, and a call after a failed editor batch. Each tool declares its value or `null` as its output schema. Calls that already succeeded stay applied. A resource provider's action that is refused or has an unknown effect keeps its object receipt instead of `null`, because the receipt says what happened.

The Codemode result ends with a block that lists every failed IDE call, both when the script completes and when it throws. It starts with a count such as `2 of 300 IDE calls failed:`. Each entry names the tool and the source it was given, followed by the readable error text a direct call shows, including its recovery steps and anchors. A failed edit that changed files, or whose effect is unknown, names those files and says not to repeat the edit blindly. Edits that a batch accepted and that failed when the batch was saved are listed as well; their value was already returned when they were accepted.

Error text carries no system-result envelope, so a failed result cannot be passed on as a selection. Direct calls keep readable failure text and the error flag.

Pi still rejects calls that never reach an IDE tool: arguments that do not match the tool's schema, unknown tools, and calls blocked by another extension's `tool_call` handler. Use try/catch or `Promise.allSettled` for those.

Pi 1.5.2 drops a `null` `structuredContent` that a `tool_result` handler returns. The IDE therefore sets the `null` value of a failed call, or of an edit that selects no text, on the nested result in `tool_execution_end`, which Pi emits before it hands that result to the script.

For a fragmented address, a declared `ResourceError` owns the failure of that complete resource. Read preserves its public code and source without retrying the bare address or interpreting the fragment as a text anchor. Ordinary unclaimed addresses can still use text-anchor fallback.

Check the readable operation effects and the failure block of the parent result before retrying. An error does not prove rollback.

Automatic batch boundaries save pending edits, not their final formatting. Write finishes its own post-edit processing before returning. Final post-edit processing can retire earlier snapshots. Ordinary script errors keep accepted edits; abort/deadline discards pending writes, not already committed batches.

A missing or ambiguous exact-text selector stops only its own edit. The tool names the failed field and may show current anchors. Retry that field with a current anchor from Read or Search, or with text that matches exactly once. Keep any other selector that still identifies the intended text.

Edit tools share one selection rule in the system prompt: anchor kinds, whole-line and exact-text boundaries, and line endings. Search explains Boolean parsing and broadened queries in the result where they apply.
