Skip to content
Crystal CodeDocs
ENEnglish

Session directory access

CodingSession owns directory authorization for one session. Its workspace resolver carries the live DirectoryAccessPolicy, separate from the primary workspace root and approval grants. Immutable DirectoryAccess values contain canonical absolute path and mode (read or write), plus optional sourcePath for the absolute, lexically normalized entry path when it differs from the target. The entry path is revocation metadata only; authorization always matches path. Write includes read. The implicit workspace is writable; the most-specific directory rule wins, including an explicit read-only rule for the workspace or one of its children. Matching uses directory boundaries and Windows case-insensitivity, resolves tool paths through symlinks, and checks credential paths independently.

/access lists the primary workspace and session rules. /access add <path> read|write validates an existing directory and adds or replaces its canonical rule; /access remove <path> first matches the recorded entry path, then the canonical target, even if the directory or entry symlink no longer exists. A retargeted entry revokes the original rule, not the new target’s independent rule. Path resolution failures report the path error, not command usage. Paths containing spaces can be double-quoted. Mutations are refused during a turn or compaction. Changes immediately save SessionDocument.access, refresh the live model instructions and the {{directories}} placeholder, and affect the already-bound executor through the shared policy. On a new session, any --allow-read or --allow-write flag makes those flags the complete seed. They replace workspace defaults, are not merged with them, and are recorded only as session access metadata, never as preferences in config.json or access.json. A resumed session keeps its saved rules; flags still overlay those rules, and defaults are not loaded. Conflicting modes for the same canonical root are rejected at launch. When no flag is set and the session is not resuming, ~/.crystal/access.json supplies the seed. That file is keyed by the canonical workspace root, not the git trust root, and lives under Home so a project cannot grant itself extra directories. A missing file seeds nothing. A corrupt file or two modes for one directory seeds nothing and reports an error; the session still opens. A missing directory, a credential path, or a bad mode is skipped with a visible error and left in the file. /access default lists the stored rules. /access default add <path> read|write validates like /access add, writes the file, and adds the rule to the current session. /access default remove <path> removes the stored rule and the same canonical rule from the current session only. Other saved sessions are left alone. Mutations are refused during a turn or compaction.

Built-in file reads in authorized directories use ordinary workspace read approval. Writes in writable directories follow the current approval mode. Outside Full mode, ApprovalPolicy rejects explicit read-only writes before classification hooks, grants, or review; write, edit, multi_edit, and apply_patch recheck at path resolution before file I/O. A patch is rejected whole when any of its paths, including a move destination, is in a read-only directory. Full bypasses these directory restrictions through the shared workspace resolver; changing the approval mode restores them without altering saved access rules. Unlisted outside paths follow the current approval mode. Credential paths remain forbidden. Relative paths stay relative to the one primary workspace. Search and enumeration keep their existing root fence and do not automatically search all authorized directories or traverse cross-root links. The glob, grep, and search tool descriptions and path schemas explicitly allow workspace-relative or absolute roots, default to the workspace, and explain session access versus approval for unlisted outside roots.

Model-bound text describes only what the model should do: stay inside the workspace and listed directories, leave read-only directories alone, do not use shell or extensions to get around access, and stop retrying a denied call. Built-in prompts, tool descriptions, and the session directory access text do not name approval modes, the Review model, risk classes, or what Full bypasses. Approval stays a host decision. Work and the bash description prefer the built-in file tools for lookups and changes, and allow bash when those tools cannot do the job.

Resume restores the saved absolute rules and entry paths, including rules for missing directories; it never resolves a saved entry to a newly retargeted root. Invalid saved rules fail closed rather than silently losing read-only restrictions. SessionResume.TryLoad validates access before returning a document, and direct session construction validates it before allocating session resources. Interactive startup reports a red error and exits with code 1; in-session resume reports an error without changing the current session or its rules. Resume does not reapply workspace defaults, so a default removed during that session stays removed. Fork does not copy the source session’s access. Clear, fork, and a successful workspace change start from the destination workspace’s defaults. A refused or no-op change does not. Additional roots do not discover prompts, instructions, plugins, tools, or skills. Existing session files with no access field load with an empty rule list; rules without sourcePath remain valid and can be removed by their listed canonical target. Shell, background shell, external tools, and plugins retain their existing approval behavior and are not sandboxed by this policy. An operating-system sandbox remains deferred.

edit, multi_edit, write, and apply_patch end their result with a numbered excerpt in the read layout ( 12|text), so the model can check the outcome without another read. edit shows the changed lines with 5 lines of context on each side; with replace_all it shows the first change and the count. write shows the first lines of the file. An excerpt that would be empty, as after emptying a file, is omitted. Excerpts are model-visible tool text, use LF separators, and are not stored anywhere else.

grep prints path:line:text by default. output_mode files lists each matching path once and count prints path:count. context (0 to 50) adds neighboring lines as path-line-text, with -- between separate groups; a context line that two matches share is printed once. limit defaults to 2,000 and is capped at 10,000 matches in content mode, or files in the other modes; a stop at the limit says so. Output is also bounded by the tool output size.

A model entry on a responses provider may declare textTools. The factory then gives the adapter a TextToolBinding for apply_patch and its one property, patchText. The tool stays a JSON tool everywhere in the host: catalog, disabling, replacement, approval, grants, the transcript, the archive, and compaction all see {"patchText": ...}. Only the Responses adapter changes the form, and only when the request’s own tool definition for that name is a JSON object with exactly that one string property. Such a tool is sent as {"type":"custom","name","description"} with no schema. A custom_tool_call for it, whole or streamed, is wrapped back into {"patchText": <text>} with the JSON input kind; the stream holds the text until response.custom_tool_call_input.done, falls back to the held deltas when that event carries none, and emits it once. A call and its result go out as custom_tool_call and custom_tool_call_output only when the request offers the tool as custom and the recorded arguments hold the one string. Otherwise the pair keeps its recorded function_call form, so switching models, disabling the tool, or a replacement that changed its schema never leaves a call without a matching result type. A custom call to a name that was not offered this way stays text input and is refused by name. A tool that can return an image is never folded, since a custom tool’s output is text. The declaration is refused when the file loads for any protocol but responses.

apply_patch takes patchText: Add File, Update File, and Delete File sections between *** Begin Patch and *** End Patch. An Update File section may carry *** Move to: and holds change blocks. A block may start with one or more @@ <line> anchors that must be found, in order, before its lines are matched. Lines starting with a space are context, - removes, + adds, and *** End of File ties a block to the end of the file. Matching tries exact text, then ignoring trailing spaces, then ignoring surrounding spaces, then ignoring typographic quote, dash, ellipsis, and space differences; the looser passes run only when the stricter ones find nothing. The patch is read, every path is resolved and checked, and every Update is computed against the bytes on disk before the first file is written, so a bad block, a missing file, an existing Add or Move destination, a credential path, a read-only directory, a binary or non-UTF-8 file, an oversize result, or a path named in two sections leaves every file as it was. Sections then apply in order. An Update is written only if the file still holds the bytes it was computed from. A move writes the destination before deleting the source. If a write fails part way, the result names the failing file and the files already changed. Updates keep a byte order mark, the file’s newline style, and whether it ended with a newline.

The patch is classified by its riskiest path, including each move destination: any credential path makes the call Forbidden, any path outside the session’s directories makes it an outside write, and otherwise it is a workspace write. A reusable grant for a patch is keyed by its sorted set of paths, not by the patch body. Plan does not offer it. Edit and Review let a workspace patch pass without review, like edit and write.

multi_edit takes a nonempty files array. Each entry names an existing UTF-8 file and a nonempty array of edits. Resolved paths must be unique in the batch. Each edit uses edit’s exact matching rules and sees the text from earlier edits to its file. The engine checks every entry and computes all results before writing anything. Files and intermediate results share the workspace write limit. Unchanged text, including line endings, is preserved, as is the UTF-8 byte order mark.

Approval covers the riskiest target, and reusable grants are keyed by the sorted set of supplied paths. The tool compares each file with its original bytes under the write handle before committing that file. Files are committed in array order and each changed file is written once. A commit failure reports the failing file and any earlier completed files. The result reports per-file replacement counts. The terminal approval card shows the requested replacements, and ACP previews the computed before and after text.