@clarvis/agent-tools 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/CODE_OF_CONDUCT.md +117 -0
  3. package/CONTRIBUTING.md +78 -0
  4. package/LICENSE +21 -0
  5. package/README.md +203 -0
  6. package/SECURITY.md +58 -0
  7. package/SPEC.md +266 -0
  8. package/dist/config.d.ts +29 -0
  9. package/dist/config.d.ts.map +1 -0
  10. package/dist/config.js +145 -0
  11. package/dist/config.js.map +1 -0
  12. package/dist/core.d.ts +13 -0
  13. package/dist/core.d.ts.map +1 -0
  14. package/dist/core.js +43 -0
  15. package/dist/core.js.map +1 -0
  16. package/dist/errors.d.ts +9 -0
  17. package/dist/errors.d.ts.map +1 -0
  18. package/dist/errors.js +29 -0
  19. package/dist/errors.js.map +1 -0
  20. package/dist/index.d.ts +20 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +17 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/lib/atomic.d.ts +11 -0
  25. package/dist/lib/atomic.d.ts.map +1 -0
  26. package/dist/lib/atomic.js +307 -0
  27. package/dist/lib/atomic.js.map +1 -0
  28. package/dist/lib/binary.d.ts +3 -0
  29. package/dist/lib/binary.d.ts.map +1 -0
  30. package/dist/lib/binary.js +20 -0
  31. package/dist/lib/binary.js.map +1 -0
  32. package/dist/lib/files.d.ts +9 -0
  33. package/dist/lib/files.d.ts.map +1 -0
  34. package/dist/lib/files.js +49 -0
  35. package/dist/lib/files.js.map +1 -0
  36. package/dist/lib/ignore.d.ts +5 -0
  37. package/dist/lib/ignore.d.ts.map +1 -0
  38. package/dist/lib/ignore.js +106 -0
  39. package/dist/lib/ignore.js.map +1 -0
  40. package/dist/lib/log.d.ts +4 -0
  41. package/dist/lib/log.d.ts.map +1 -0
  42. package/dist/lib/log.js +11 -0
  43. package/dist/lib/log.js.map +1 -0
  44. package/dist/lib/match-cascade.d.ts +10 -0
  45. package/dist/lib/match-cascade.d.ts.map +1 -0
  46. package/dist/lib/match-cascade.js +153 -0
  47. package/dist/lib/match-cascade.js.map +1 -0
  48. package/dist/lib/output.d.ts +8 -0
  49. package/dist/lib/output.d.ts.map +1 -0
  50. package/dist/lib/output.js +77 -0
  51. package/dist/lib/output.js.map +1 -0
  52. package/dist/lib/paths.d.ts +3 -0
  53. package/dist/lib/paths.d.ts.map +1 -0
  54. package/dist/lib/paths.js +46 -0
  55. package/dist/lib/paths.js.map +1 -0
  56. package/dist/lib/rg.d.ts +22 -0
  57. package/dist/lib/rg.d.ts.map +1 -0
  58. package/dist/lib/rg.js +285 -0
  59. package/dist/lib/rg.js.map +1 -0
  60. package/dist/lib/text.d.ts +18 -0
  61. package/dist/lib/text.d.ts.map +1 -0
  62. package/dist/lib/text.js +129 -0
  63. package/dist/lib/text.js.map +1 -0
  64. package/dist/lib/textfile.d.ts +4 -0
  65. package/dist/lib/textfile.d.ts.map +1 -0
  66. package/dist/lib/textfile.js +55 -0
  67. package/dist/lib/textfile.js.map +1 -0
  68. package/dist/lib/token.d.ts +2 -0
  69. package/dist/lib/token.d.ts.map +1 -0
  70. package/dist/lib/token.js +6 -0
  71. package/dist/lib/token.js.map +1 -0
  72. package/dist/tools/apply-patch.d.ts +3 -0
  73. package/dist/tools/apply-patch.d.ts.map +1 -0
  74. package/dist/tools/apply-patch.js +221 -0
  75. package/dist/tools/apply-patch.js.map +1 -0
  76. package/dist/tools/bash.d.ts +3 -0
  77. package/dist/tools/bash.d.ts.map +1 -0
  78. package/dist/tools/bash.js +179 -0
  79. package/dist/tools/bash.js.map +1 -0
  80. package/dist/tools/edit-file.d.ts +15 -0
  81. package/dist/tools/edit-file.d.ts.map +1 -0
  82. package/dist/tools/edit-file.js +163 -0
  83. package/dist/tools/edit-file.js.map +1 -0
  84. package/dist/tools/glob.d.ts +3 -0
  85. package/dist/tools/glob.d.ts.map +1 -0
  86. package/dist/tools/glob.js +64 -0
  87. package/dist/tools/glob.js.map +1 -0
  88. package/dist/tools/grep.d.ts +3 -0
  89. package/dist/tools/grep.d.ts.map +1 -0
  90. package/dist/tools/grep.js +201 -0
  91. package/dist/tools/grep.js.map +1 -0
  92. package/dist/tools/list-dir.d.ts +3 -0
  93. package/dist/tools/list-dir.d.ts.map +1 -0
  94. package/dist/tools/list-dir.js +59 -0
  95. package/dist/tools/list-dir.js.map +1 -0
  96. package/dist/tools/multi-edit.d.ts +3 -0
  97. package/dist/tools/multi-edit.d.ts.map +1 -0
  98. package/dist/tools/multi-edit.js +86 -0
  99. package/dist/tools/multi-edit.js.map +1 -0
  100. package/dist/tools/read-file.d.ts +3 -0
  101. package/dist/tools/read-file.d.ts.map +1 -0
  102. package/dist/tools/read-file.js +102 -0
  103. package/dist/tools/read-file.js.map +1 -0
  104. package/dist/tools/registry.d.ts +6 -0
  105. package/dist/tools/registry.d.ts.map +1 -0
  106. package/dist/tools/registry.js +28 -0
  107. package/dist/tools/registry.js.map +1 -0
  108. package/dist/tools/types.d.ts +9 -0
  109. package/dist/tools/types.d.ts.map +1 -0
  110. package/dist/tools/types.js +2 -0
  111. package/dist/tools/types.js.map +1 -0
  112. package/dist/tools/write-file.d.ts +3 -0
  113. package/dist/tools/write-file.d.ts.map +1 -0
  114. package/dist/tools/write-file.js +61 -0
  115. package/dist/tools/write-file.js.map +1 -0
  116. package/package.json +70 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,40 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@clarvis/agent-tools` are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
6
+ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-07-01
11
+
12
+ Initial public release.
13
+
14
+ ### Added
15
+
16
+ - **Nine coding tools** for driving an LLM agent over a workspace: `read_file`, `list_dir`, `glob`,
17
+ `grep`, `write_file`, `edit_file`, `multi_edit`, `apply_patch`, and `bash`.
18
+ - **Transport-agnostic library API** — `createAgentTools({ workspaceRoot })` plus the lower-level
19
+ `dispatch` / `listTools` / `resolveConfig` / `buildConfig` and the raw `ToolDef` registry
20
+ (`tools`, `readOnlyTools`, `getTool`, `selectSurface`).
21
+ - **Uniform dispatch pipeline** — arguments validated against each tool's JSON Schema (ajv), handler
22
+ run, output bounded, and errors serialized to a stable `{ error, message, ...fields }` envelope
23
+ (`ToolError`, `serializeError`, `fsError`, the `ErrorCode` union).
24
+ - **Workspace path confinement** (`confineToWorkspace`, default on) with `realpath` canonicalization
25
+ and symlink-escape rejection (`path_escape`), plus write-through-symlink refusal.
26
+ - **Read-only mode** (`readOnly`) exposing only `read_file`, `list_dir`, `glob`, and `grep`.
27
+ - **Output bounding & spill** — every result capped to `maxOutputBytes` (UTF-8-safe); `bash` splits
28
+ the budget across stdout/stderr, enforces a per-stream capture ceiling (`output_limit`), and spills
29
+ full output to a `.clarvis/` file, swept by `sweepSpillDir`.
30
+ - **Atomic writes** — temp-then-rename with fsync, mode preservation, per-path locking, and
31
+ multi-file rollback for `apply_patch`.
32
+ - **Text handling** — UTF-8 / UTF-16 (LE/BE) decoding, BOM and per-line EOL preservation on edits,
33
+ NUL-byte binary rejection, and a `maxFileBytes` input ceiling (`too_large`).
34
+ - **grep** backed by ripgrep when available, with a behaviorally consistent in-process fallback, both
35
+ honoring `.gitignore`.
36
+ - **VitePress documentation site** ([agent-tools.clarvis.dev](https://agent-tools.clarvis.dev)) and
37
+ the canonical per-tool [`SPEC.md`](SPEC.md).
38
+
39
+ [Unreleased]: https://github.com/getclarvis/agent-tools/compare/v0.1.0...HEAD
40
+ [0.1.0]: https://github.com/getclarvis/agent-tools/releases/tag/v0.1.0
@@ -0,0 +1,117 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a
6
+ harassment-free experience for everyone, regardless of age, body size, visible or invisible
7
+ disability, ethnicity, sex characteristics, gender identity and expression, level of experience,
8
+ education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or
9
+ sexual identity and orientation.
10
+
11
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and
12
+ healthy community.
13
+
14
+ ## Our Standards
15
+
16
+ Examples of behavior that contributes to a positive environment for our community include:
17
+
18
+ - Demonstrating empathy and kindness toward other people
19
+ - Being respectful of differing opinions, viewpoints, and experiences
20
+ - Giving and gracefully accepting constructive feedback
21
+ - Accepting responsibility and apologizing to those affected by our mistakes, and learning from the
22
+ experience
23
+ - Focusing on what is best not just for us as individuals, but for the overall community
24
+
25
+ Examples of unacceptable behavior include:
26
+
27
+ - The use of sexualized language or imagery, and sexual attention or advances of any kind
28
+ - Trolling, insulting or derogatory comments, and personal or political attacks
29
+ - Public or private harassment
30
+ - Publishing others' private information, such as a physical or email address, without their
31
+ explicit permission
32
+ - Other conduct which could reasonably be considered inappropriate in a professional setting
33
+
34
+ ## Enforcement Responsibilities
35
+
36
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior
37
+ and will take appropriate and fair corrective action in response to any behavior that they deem
38
+ inappropriate, threatening, offensive, or harmful.
39
+
40
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits,
41
+ code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and
42
+ will communicate reasons for moderation decisions when appropriate.
43
+
44
+ ## Scope
45
+
46
+ This Code of Conduct applies within all community spaces, and also applies when an individual is
47
+ officially representing the community in public spaces. Examples of representing our community
48
+ include using an official email address, posting via an official social media account, or acting as
49
+ an appointed representative at an online or offline event.
50
+
51
+ ## Enforcement
52
+
53
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community
54
+ leaders responsible for enforcement at **conduct@clarvis.dev**. All complaints will be reviewed and
55
+ investigated promptly and fairly.
56
+
57
+ All community leaders are obligated to respect the privacy and security of the reporter of any
58
+ incident.
59
+
60
+ ## Enforcement Guidelines
61
+
62
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for
63
+ any action they deem in violation of this Code of Conduct:
64
+
65
+ ### 1. Correction
66
+
67
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or
68
+ unwelcome in the community.
69
+
70
+ **Consequence**: A private, written warning from community leaders, providing clarity around the
71
+ nature of the violation and an explanation of why the behavior was inappropriate. A public apology
72
+ may be requested.
73
+
74
+ ### 2. Warning
75
+
76
+ **Community Impact**: A violation through a single incident or series of actions.
77
+
78
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people
79
+ involved, including unsolicited interaction with those enforcing the Code of Conduct, for a
80
+ specified period of time. This includes avoiding interactions in community spaces as well as external
81
+ channels like social media. Violating these terms may lead to a temporary or permanent ban.
82
+
83
+ ### 3. Temporary Ban
84
+
85
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate
86
+ behavior.
87
+
88
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the
89
+ community for a specified period of time. No public or private interaction with the people involved,
90
+ including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this
91
+ period. Violating these terms may lead to a permanent ban.
92
+
93
+ ### 4. Permanent Ban
94
+
95
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including
96
+ sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement
97
+ of classes of individuals.
98
+
99
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
100
+
101
+ ## Attribution
102
+
103
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at
104
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
105
+
106
+ Community Impact Guidelines were inspired by
107
+ [Mozilla's code of conduct enforcement ladder][mozilla coc].
108
+
109
+ For answers to common questions about this code of conduct, see the FAQ at
110
+ [https://www.contributor-covenant.org/faq][faq]. Translations are available at
111
+ [https://www.contributor-covenant.org/translations][translations].
112
+
113
+ [homepage]: https://www.contributor-covenant.org
114
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
115
+ [mozilla coc]: https://github.com/mozilla/diversity
116
+ [faq]: https://www.contributor-covenant.org/faq
117
+ [translations]: https://www.contributor-covenant.org/translations
@@ -0,0 +1,78 @@
1
+ # Contributing to @clarvis/agent-tools
2
+
3
+ Thanks for your interest in contributing! This document covers how to get set up, the quality gate,
4
+ and the conventions this codebase follows. By participating you agree to abide by our
5
+ [Code of Conduct](CODE_OF_CONDUCT.md).
6
+
7
+ ## Getting started
8
+
9
+ ```bash
10
+ git clone https://github.com/getclarvis/agent-tools.git
11
+ cd agent-tools
12
+ npm install
13
+ ```
14
+
15
+ Requires **Node.js >= 20**. [ripgrep](https://github.com/BurntSushi/ripgrep) (`rg`) on `PATH` is
16
+ optional — when present, `grep` uses it; otherwise an equivalent in-process backend runs, and the two
17
+ are kept behaviorally consistent.
18
+
19
+ ## Development workflow
20
+
21
+ ```bash
22
+ npm run build # emit dist/ (tsc, with .d.ts)
23
+ npm test # full vitest suite (contract + integration)
24
+ npm run typecheck # tsc --noEmit (strict)
25
+ npm run lint # eslint
26
+ npm run format # prettier --write (src + tests)
27
+ ```
28
+
29
+ Before opening a pull request, make sure the quality gate is green:
30
+
31
+ ```bash
32
+ npm run pre-commit # typecheck + format:check + test
33
+ ```
34
+
35
+ CI runs `typecheck`, `lint`, `format:check`, `test`, and `build` on pushes and PRs targeting `main`
36
+ and `develop`, across a Node matrix (`20.x`, `lts/*`, `current`). There is no separate coverage gate.
37
+
38
+ ## Project layout
39
+
40
+ - `src/index.ts` — the public API (`createAgentTools`, `dispatch`, `listTools`, `resolveConfig`,
41
+ `buildConfig`, the registry, and the error contract).
42
+ - `src/core.ts` — the dispatch pipeline: validate (ajv) → run handler → bound output → serialize error.
43
+ - `src/config.ts` — config resolution, defaults, and the argv/env builder.
44
+ - `src/errors.ts` — `ToolError`, `serializeError`, `fsError`, and the `ErrorCode` union.
45
+ - `src/tools/` — the nine tool handlers plus the registry (`registry.ts`) and `ToolDef` type.
46
+ - `src/lib/` — the shared primitives: path confinement, text decode/encode, the edit match cascade,
47
+ atomic writes, the two search backends, and output bounding/spill.
48
+ - `tests/` — `contract/` (one file per tool) and `integration/` (cross-cutting) suites, with
49
+ `helpers/fixtures.ts`.
50
+
51
+ For the architecture and per-subsystem internals, see [`docs-internal/`](docs-internal/). User-facing
52
+ docs live in [`docs/`](docs/) and the canonical per-tool contract in [`SPEC.md`](SPEC.md).
53
+
54
+ ## Guidelines
55
+
56
+ - **Match the surrounding style.** The codebase is strict TypeScript with `noUncheckedIndexedAccess`;
57
+ prefer explicit, narrow types over `any`/casts. **No JSDoc or inline comments** unless a
58
+ lint/quality rule requires them.
59
+ - **Add tests** for behavior changes. `tests/contract/` guards each tool's input/output/error
60
+ contract; `tests/integration/` guards cross-cutting behavior (the public API, surface selection,
61
+ statelessness). Changing a tool contract means updating its contract test, [`SPEC.md`](SPEC.md), and
62
+ `docs/reference/tools.md` together.
63
+ - **Keep the two grep backends in agreement.** Any change to matching, context, or globbing must land
64
+ in both the ripgrep and in-process paths and be covered by `tests/integration/grep-parity.test.ts`.
65
+ - **Security-sensitive changes** — anything touching path confinement, `bash` / subprocess spawning,
66
+ atomic writes, or spill files — deserve extra scrutiny. See [SECURITY.md](SECURITY.md).
67
+ - **Update docs** when you change a tool contract, a config option, or an observable behavior:
68
+ [`SPEC.md`](SPEC.md), the matching `docs/` reference page, and the matching
69
+ [`docs-internal/internals/`](docs-internal/internals/) page.
70
+
71
+ ## Reporting bugs and requesting features
72
+
73
+ Open a [GitHub issue](https://github.com/getclarvis/agent-tools/issues) using the provided templates.
74
+ For security issues, follow the private process in [SECURITY.md](SECURITY.md) instead.
75
+
76
+ ## License
77
+
78
+ By contributing, you agree that your contributions will be licensed under the [MIT License](LICENSE).
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 getclarvis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,203 @@
1
+ # @clarvis/agent-tools
2
+
3
+ A minimal, opinionated set of coding tools for driving an LLM agent over a
4
+ workspace, usable as a **plain library**. It gives an agent the primitives it
5
+ needs to read, search, edit, and run code: `read_file`, `list_dir`, `glob`,
6
+ `grep`, `write_file`, `edit_file`, `multi_edit`, `apply_patch`, and `bash`.
7
+
8
+ This package is **transport-agnostic**: it carries no built-in transport and no
9
+ agent loop — it is the tools, and nothing else. Advertise the surface, dispatch
10
+ the calls, and feed the results back from whatever agent loop or transport you
11
+ build around it.
12
+
13
+ > [!WARNING]
14
+ > These tools grant **read/write access to the workspace and arbitrary shell
15
+ > execution** with the privileges of the host process. Tool paths are **confined
16
+ > to the workspace root by default**, but `bash` runs arbitrary commands and is
17
+ > not sandboxed. Run it inside an OS-level sandbox. See [Security](#security).
18
+
19
+ ## Requirements
20
+
21
+ - Node.js >= 20
22
+ - Optional: [ripgrep](https://github.com/BurntSushi/ripgrep) (`rg`) on `PATH`.
23
+ When present, `grep` uses it; otherwise an equivalent in-process fallback is
24
+ used. Results are kept consistent between the two backends.
25
+
26
+ ## Install
27
+
28
+ ```sh
29
+ npm install @clarvis/agent-tools
30
+ ```
31
+
32
+ ## Library usage
33
+
34
+ The ergonomic entry point is `createAgentTools`. Give it a workspace root and it
35
+ resolves a config, then lets you list and call tools:
36
+
37
+ ```ts
38
+ import { createAgentTools } from "@clarvis/agent-tools";
39
+
40
+ const tools = createAgentTools({ workspaceRoot: process.cwd() });
41
+
42
+ // Advertise the tool surface (name / description / JSON Schema) to your model:
43
+ tools.listTools();
44
+
45
+ // Call a tool by name with its arguments:
46
+ const res = await tools.callTool("read_file", { path: "package.json" });
47
+ if (res.isError) {
48
+ const err = JSON.parse(res.text) as { error: string; message: string };
49
+ console.error(err.error, err.message);
50
+ } else {
51
+ console.log(res.text);
52
+ }
53
+ ```
54
+
55
+ `callTool` never throws for tool-level problems — it returns
56
+ `{ isError, text }`. On success `text` is the tool output (already bounded to
57
+ `maxOutputBytes`); on failure `text` is a JSON error envelope
58
+ (`{ "error": "<code>", "message": "..." , ... }`). Unknown or read-only-hidden
59
+ tools come back as an `isError` result with code `not_found`.
60
+
61
+ ### Options
62
+
63
+ `createAgentTools(options)` / `resolveConfig(options)` accept:
64
+
65
+ | Option | Default | Meaning |
66
+ | -------------------- | ------------ | ----------------------------------------------------------------------- |
67
+ | `workspaceRoot` | — (required) | Base directory; relative tool paths resolve against it. |
68
+ | `readOnly` | `false` | Expose only the non-mutating tools (`read_file`/`list_dir`/`glob`/`grep`). |
69
+ | `confineToWorkspace` | `true` | Reject paths that escape the workspace root (`path_escape`). |
70
+ | `maxOutputBytes` | `131072` | Per-result output cap (UTF-8 bytes); larger output is bounded. |
71
+ | `maxFileBytes` | `20000000` | Max size of an input file the text tools read; larger is rejected. |
72
+ | `bashTimeoutMs` | `120000` | Default `bash` timeout in milliseconds. |
73
+ | `bashTimeoutMaxMs` | `600000` | Hard ceiling a `bash` `timeout_ms` request may reach (≥ the default). |
74
+ | `probeRipgrep` | probes `rg` | Override ripgrep detection (e.g. `() => false` in tests). |
75
+
76
+ ### Lower-level API
77
+
78
+ The building blocks are exported too, for custom transports:
79
+
80
+ ```ts
81
+ import { resolveConfig, dispatch, listTools, tools, type ToolDef } from "@clarvis/agent-tools";
82
+
83
+ const config = resolveConfig({ workspaceRoot: process.cwd() });
84
+ const { isError, text } = await dispatch("grep", { pattern: "TODO" }, config);
85
+ ```
86
+
87
+ - `dispatch(name, args, config)` — validate (ajv, against each tool's JSON
88
+ Schema), run the handler, bound the output, serialize errors. Returns
89
+ `{ isError, text }`.
90
+ - `listTools(config)` — the `{ name, description, inputSchema }[]` surface for the
91
+ active config (respects `readOnly`).
92
+ - `tools` / `readOnlyTools` / `getTool` / `selectSurface` — the raw `ToolDef`
93
+ registry.
94
+ - `buildConfig(argv, env)` — the argv/env config builder for a CLI or
95
+ long-running service (delegates to `resolveConfig`).
96
+ - `ToolError` / `serializeError` / `ErrorCode` — the structured error contract.
97
+
98
+ ## Tools
99
+
100
+ | Tool | Mutating | Summary |
101
+ | ------------- | -------- | ----------------------------------------------------------------- |
102
+ | `read_file` | no | Read a text file (UTF-8/UTF-16), with line numbers, paging, tail. |
103
+ | `list_dir` | no | List the entries of a directory. |
104
+ | `glob` | no | Find files by glob, most-recently-modified first. |
105
+ | `grep` | no | Search file contents by regular expression (optionally multiline).|
106
+ | `write_file` | yes | Create or overwrite a file (atomic). |
107
+ | `edit_file` | yes | Replace one exact occurrence of a string in a file. |
108
+ | `multi_edit` | yes | Apply several `edit_file`-style edits to one file atomically. |
109
+ | `apply_patch` | yes | Apply a unified diff (modify/create/delete/rename) atomically. |
110
+ | `bash` | yes | Run a shell command (`sh -c`) and capture stdout/stderr/exit. |
111
+
112
+ In read-only mode only `read_file`, `list_dir`, `glob`, and `grep` are exposed.
113
+
114
+ See [SPEC.md](./SPEC.md) for the full per-tool contract (inputs, behavior, and
115
+ error codes).
116
+
117
+ ### Output bounding
118
+
119
+ Every tool result is capped to `maxOutputBytes` (truncated on a UTF-8 character
120
+ boundary, with a marker). `bash` additionally enforces a hard per-stream
121
+ in-memory ceiling while a command runs: a command that produces unbounded output
122
+ is killed and the call returns an `output_limit` error rather than exhausting
123
+ memory. When `bash` output overflows the display cap, the full captured output is
124
+ written to a `.clarvis/` spill file and the result points at it.
125
+
126
+ ### Input bounding
127
+
128
+ The file-reading tools (`read_file`, `edit_file`, `multi_edit`, `apply_patch`,
129
+ and the in-process `grep` backend) refuse to load a file larger than
130
+ `maxFileBytes` (default 20 MB): the read/edit tools fail with `too_large`, and
131
+ `grep` skips the oversized file.
132
+
133
+ ## Security
134
+
135
+ This is the most important property of these tools, so it is stated plainly:
136
+
137
+ - **Workspace-confined paths (default).** Every tool resolves paths against the
138
+ workspace root and, by default, refuses any path that escapes it: `../`
139
+ traversal, absolute paths outside the root, and symlinks inside the workspace
140
+ that resolve outside are rejected with `path_escape`. The existing portion of a
141
+ path is canonicalized with `realpath`, so symlink hops are caught. This guard
142
+ can be disabled with `confineToWorkspace: false`, which restores unrestricted
143
+ path resolution.
144
+ - **Arbitrary command execution.** `bash` runs `sh -c <command>` with the full
145
+ privileges of the host process. Path confinement does **not** constrain a shell
146
+ command — `bash` can read, modify, and execute anything the process can,
147
+ regardless of the confinement setting. It is an intentional escape hatch.
148
+ - **The threat model is the host.** Because the agent driving these tools can run
149
+ arbitrary commands, you must treat the process as having the same trust as the
150
+ code/agent connected to it. Path confinement is defense-in-depth for the file
151
+ tools, not a substitute for OS-level isolation.
152
+
153
+ **Run it inside an OS-level sandbox** — a container, a VM, a dedicated
154
+ low-privilege user, seccomp/AppArmor, or equivalent — scoped to the project you
155
+ intend the agent to work in. See [SECURITY.md](./SECURITY.md) for the full trust
156
+ model.
157
+
158
+ ## Documentation
159
+
160
+ Full guides and reference live at
161
+ **[agent-tools.clarvis.dev](https://agent-tools.clarvis.dev)** (source in
162
+ [`docs/`](docs/)):
163
+
164
+ - **Guide** — [getting started](https://agent-tools.clarvis.dev/getting-started),
165
+ [embed in an agent](https://agent-tools.clarvis.dev/guide/embed-in-an-agent),
166
+ [the core API](https://agent-tools.clarvis.dev/guide/the-core-api),
167
+ [read-only mode](https://agent-tools.clarvis.dev/guide/read-only-mode),
168
+ [limits & spill](https://agent-tools.clarvis.dev/guide/limits-and-spill).
169
+ - **Reference** — [tools](https://agent-tools.clarvis.dev/reference/tools),
170
+ [configuration](https://agent-tools.clarvis.dev/reference/configuration),
171
+ [createAgentTools](https://agent-tools.clarvis.dev/reference/create-agent-tools),
172
+ [core API](https://agent-tools.clarvis.dev/reference/core-api),
173
+ [error codes](https://agent-tools.clarvis.dev/reference/error-codes).
174
+ - **Concepts & operations** —
175
+ [how it works](https://agent-tools.clarvis.dev/explanation/how-it-works),
176
+ [path confinement](https://agent-tools.clarvis.dev/explanation/confinement),
177
+ [text & encoding](https://agent-tools.clarvis.dev/explanation/text-and-encoding),
178
+ [deploy securely](https://agent-tools.clarvis.dev/operations/deploy-securely).
179
+
180
+ The canonical per-tool contract (inputs, behavior, error codes) is
181
+ [SPEC.md](./SPEC.md).
182
+
183
+ ## Development
184
+
185
+ ```sh
186
+ npm run build # tsc -> dist/ (emits .d.ts)
187
+ npm test # vitest (contract + integration)
188
+ npm run typecheck
189
+ npm run lint
190
+ npm run format:check
191
+ npm run pre-commit # typecheck + format:check + test
192
+ ```
193
+
194
+ ## Contributing
195
+
196
+ Contributions are welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md) for setup and
197
+ the quality gate, [`docs-internal/`](docs-internal/) for the architecture and
198
+ per-subsystem internals, and [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md). Report
199
+ security issues privately per [SECURITY.md](./SECURITY.md).
200
+
201
+ ## License
202
+
203
+ [MIT](./LICENSE) © Clarvis
package/SECURITY.md ADDED
@@ -0,0 +1,58 @@
1
+ # Security Policy
2
+
3
+ ## Supported versions
4
+
5
+ This project is pre-1.0. Security fixes are applied to the latest published `0.x` release.
6
+
7
+ ## Trust model
8
+
9
+ `@clarvis/agent-tools` is a **library** that gives an agent read/write access to a workspace and
10
+ **arbitrary shell execution** with the privileges of the host process. It carries no transport and no
11
+ sandbox of its own. Two boundaries matter:
12
+
13
+ - **File tools are workspace-confined by default.** `read_file`, `list_dir`, `glob`, `grep`,
14
+ `write_file`, `edit_file`, `multi_edit`, and `apply_patch` resolve every path against the workspace
15
+ root and, with `confineToWorkspace: true` (the default), reject any path that escapes it — `../`
16
+ traversal, an absolute path outside the root, or a symlink whose real target lies outside — with
17
+ `path_escape`. The existing prefix of a target path is canonicalized with `realpath`, so symlink
18
+ hops are caught. Writes additionally refuse to write **through** a symlink at the target itself,
19
+ regardless of the confinement setting.
20
+ - **`bash` is an intentional escape hatch.** It runs `sh -c <command>` (detached, in its own process
21
+ group) with the full privileges of the host process. Path confinement does **not** constrain a
22
+ shell command — `bash` can read, modify, and execute anything the process can, whatever
23
+ `confineToWorkspace` is set to. Only its working directory is validated.
24
+
25
+ Because the agent driving these tools can run arbitrary commands, the trust model is the **host**:
26
+
27
+ > **Run `@clarvis/agent-tools` inside an OS-level sandbox** — a container, a VM, a dedicated
28
+ > low-privilege user, seccomp/AppArmor, or equivalent — scoped to the project you intend the agent to
29
+ > work in. Path confinement is defense-in-depth for the file tools, not a substitute for OS-level
30
+ > isolation.
31
+
32
+ ### Defense-in-depth notes
33
+
34
+ - **Output is bounded.** Every tool result is capped to `maxOutputBytes` (UTF-8-safe truncation). A
35
+ `bash` command that produces unbounded output on a single stream is killed at a per-stream capture
36
+ ceiling and returns `output_limit`, rather than exhausting memory.
37
+ - **Spill files stay in-workspace.** When `bash` output overflows the display cap, the full stream is
38
+ written to a `.clarvis/` spill file (auto-`.gitignore`d) inside the workspace and the result points
39
+ at it; nothing is written outside the workspace. `sweepSpillDir()` prunes old spill files.
40
+ - **Writes are atomic.** Mutating tools stage to a temp file, fsync, and rename, so a crash mid-write
41
+ never truncates the target; `apply_patch` rolls back a failed multi-file change and preserves the
42
+ original content in an adjacent backup if rollback itself fails.
43
+ - **Confinement is configurable.** `confineToWorkspace: false` (or `ALLOW_OUTSIDE_WORKSPACE=1`)
44
+ removes the path guard for the file tools. Only disable it when the process is already sandboxed and
45
+ you intend the tools to reach outside the workspace.
46
+ - **Errors don't leak internals.** A non-`ToolError` throw is logged to stderr and returned to the
47
+ caller only as an opaque `{ "error": "internal" }` envelope; coded `ToolError`s carry structured
48
+ fields (paths, sizes, bounded stdout/stderr) but no raw secrets.
49
+
50
+ ## Reporting a vulnerability
51
+
52
+ Please report security issues **privately**. Do not open a public issue for a vulnerability.
53
+
54
+ - Use GitHub's [private vulnerability reporting](https://github.com/getclarvis/agent-tools/security/advisories/new), or
55
+ - email the maintainers at **security@clarvis.dev**.
56
+
57
+ We aim to acknowledge reports within a few business days and will coordinate a fix and disclosure
58
+ timeline with you.