@warlock.js/ai-workspace 4.5.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 (78) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/LICENSE +21 -0
  3. package/README.md +149 -0
  4. package/cjs/index.cjs +1609 -0
  5. package/cjs/index.cjs.map +1 -0
  6. package/esm/backends/local.d.mts +22 -0
  7. package/esm/backends/local.d.mts.map +1 -0
  8. package/esm/backends/local.mjs +208 -0
  9. package/esm/backends/local.mjs.map +1 -0
  10. package/esm/backends/mock.d.mts +62 -0
  11. package/esm/backends/mock.d.mts.map +1 -0
  12. package/esm/backends/mock.mjs +167 -0
  13. package/esm/backends/mock.mjs.map +1 -0
  14. package/esm/contracts/index.d.mts +5 -0
  15. package/esm/contracts/tool-io.type.d.mts +149 -0
  16. package/esm/contracts/tool-io.type.d.mts.map +1 -0
  17. package/esm/contracts/workspace-backend.contract.d.mts +69 -0
  18. package/esm/contracts/workspace-backend.contract.d.mts.map +1 -0
  19. package/esm/contracts/workspace-ops.contract.d.mts +72 -0
  20. package/esm/contracts/workspace-ops.contract.d.mts.map +1 -0
  21. package/esm/contracts/workspace-policy.type.d.mts +86 -0
  22. package/esm/contracts/workspace-policy.type.d.mts.map +1 -0
  23. package/esm/contracts/workspace.contract.d.mts +131 -0
  24. package/esm/contracts/workspace.contract.d.mts.map +1 -0
  25. package/esm/errors.d.mts +100 -0
  26. package/esm/errors.d.mts.map +1 -0
  27. package/esm/errors.mjs +58 -0
  28. package/esm/errors.mjs.map +1 -0
  29. package/esm/index.d.mts +20 -0
  30. package/esm/index.mjs +15 -0
  31. package/esm/ops.d.mts +25 -0
  32. package/esm/ops.d.mts.map +1 -0
  33. package/esm/ops.mjs +294 -0
  34. package/esm/ops.mjs.map +1 -0
  35. package/esm/policy/policy.d.mts +71 -0
  36. package/esm/policy/policy.d.mts.map +1 -0
  37. package/esm/policy/policy.mjs +184 -0
  38. package/esm/policy/policy.mjs.map +1 -0
  39. package/esm/tools/edit-file.d.mts +40 -0
  40. package/esm/tools/edit-file.d.mts.map +1 -0
  41. package/esm/tools/edit-file.mjs +57 -0
  42. package/esm/tools/edit-file.mjs.map +1 -0
  43. package/esm/tools/glob.d.mts +37 -0
  44. package/esm/tools/glob.d.mts.map +1 -0
  45. package/esm/tools/glob.mjs +45 -0
  46. package/esm/tools/glob.mjs.map +1 -0
  47. package/esm/tools/grep.d.mts +36 -0
  48. package/esm/tools/grep.d.mts.map +1 -0
  49. package/esm/tools/grep.mjs +51 -0
  50. package/esm/tools/grep.mjs.map +1 -0
  51. package/esm/tools/read-file.d.mts +35 -0
  52. package/esm/tools/read-file.d.mts.map +1 -0
  53. package/esm/tools/read-file.mjs +64 -0
  54. package/esm/tools/read-file.mjs.map +1 -0
  55. package/esm/tools/run-shell.d.mts +35 -0
  56. package/esm/tools/run-shell.d.mts.map +1 -0
  57. package/esm/tools/run-shell.mjs +65 -0
  58. package/esm/tools/run-shell.mjs.map +1 -0
  59. package/esm/tools/run-tests.d.mts +40 -0
  60. package/esm/tools/run-tests.d.mts.map +1 -0
  61. package/esm/tools/run-tests.mjs +67 -0
  62. package/esm/tools/run-tests.mjs.map +1 -0
  63. package/esm/tools/schema.mjs +111 -0
  64. package/esm/tools/schema.mjs.map +1 -0
  65. package/esm/tools/write-file.d.mts +33 -0
  66. package/esm/tools/write-file.d.mts.map +1 -0
  67. package/esm/tools/write-file.mjs +52 -0
  68. package/esm/tools/write-file.mjs.map +1 -0
  69. package/esm/workspace.d.mts +54 -0
  70. package/esm/workspace.d.mts.map +1 -0
  71. package/esm/workspace.mjs +210 -0
  72. package/esm/workspace.mjs.map +1 -0
  73. package/llms-full.txt +231 -0
  74. package/llms.txt +10 -0
  75. package/package.json +42 -0
  76. package/skills/README.md +13 -0
  77. package/skills/build-loop-agent/SKILL.md +100 -0
  78. package/skills/use-a-workspace/SKILL.md +117 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog — @warlock.js/ai-workspace
2
+
3
+ All notable changes to `@warlock.js/ai-workspace` are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). `@warlock.js/*` packages are released in lockstep — every package shares the same version number, so a version below may list only the changes that affected this package.
6
+
7
+ ## 4.5.0 - 2026-07-01
8
+
9
+ Initial release — the policy-jailed filesystem + shell workspace for `@warlock.js/ai` agents (the W1 surface).
10
+
11
+ ### Added
12
+
13
+ - `ai.workspace(policy)` — the workspace verb, registered on the shared `ai` object via a `declare module "@warlock.js/ai"` augmentation + a runtime side-effect on import (no edit to `@warlock.js/ai`'s own source). Exported as the `workspace` factory; `WorkspaceCapableAi` is the typed view consumers cast `ai` through.
14
+ - **Policy jail** — every path is `realpath`-resolved and must sit under `cwd` (or an `allowPaths` root); `denyPaths` globs are blocked even inside `cwd`; the shell allow/deny list gates each command's leading executable basename (deny wins, fail-closed); per-command timeout + output byte cap; and `process.env` is never inherited wholesale (opt-in `shell.inheritEnv`, plus explicit `shell.env`).
15
+ - **Seven agent-facing tools** under `ws.tools.*`, each a `ToolContract` built on the core `tool()` factory: `read_file`, `edit_file`, `write_file`, `run_shell`, `run_tests`, `grep`, `glob`. Each factory takes an optional `{ name }` (and `run_tests` a `{ command }`) override.
16
+ - `tools.all()` — every tool in canonical order — and `tools.pick(...names)` — a least-privilege subset (e.g. `pick("readFile", "grep", "glob")` for a reviewer).
17
+ - **Direct programmatic methods** sharing the same policy seam: `readFile` / `writeFile` / `editFile` / `exec` / `grep` / `glob` / `exists` / `mkdir` / `remove`.
18
+ - `readonly()` — a projection that vends only the read/grep/glob tools and rejects every mutating direct method with a `WorkspacePolicyError`.
19
+ - `scope(subdir)` — a sub-jailed workspace rooted at `subdir` (narrowed `cwd`, same sub-policies and backend selection).
20
+ - **Read-before-edit guard** — `read_file` / `readFile` return a SHA-256 content `hash` (via `@warlock.js/fs` `hashString`); `edit_file` requires an exact, unique `oldString` (or `replaceAll`) and rejects a mismatched `expectHash` as stale.
21
+ - **Backends** — `createLocalBackend` (default `"local"`; `@warlock.js/fs` for IO + `node:child_process` for the shell) and `createMockBackend` (in-memory `Map` + scripted `exec`, for hermetic disk-free tests), behind the `WorkspaceBackend` contract.
22
+ - **Policy engine seam** exports: `resolveInJail`, `isCommandAllowed`, `buildEnv`, and the `ResolvedPath` type.
23
+ - **Ops layer** export `createOps` — the single policy-enforced operation layer both the tools and the direct methods funnel through.
24
+ - **Typed errors** `WorkspacePolicyError` (`type: "path-escape" | "denied-command"`) and `WorkspaceEditError` (`type: "not-found" | "not-unique" | "stale-hash"`), both extending the `@warlock.js/ai` `AIError` base (code `TOOL_EXEC_FAILED`) so failures surface to the agent as tool-error data, never thrown run-killers.
25
+ - **Public type surface** re-exported from the barrel: `Workspace`, `WorkspaceTools`, `WorkspacePolicy`, `WorkspaceShellPolicy`, `WorkspaceReadPolicy`, `WorkspaceBackendType`, `WorkspaceToolName`, `WorkspaceOps`, `WorkspaceBackend` (+ `WorkspaceBackendExecOptions` / `WorkspaceBackendExecResult`), and the per-tool IO shapes (`ReadFileInput`/`Result`, `EditFileInput`/`Result`, `WriteFileInput`/`Result`, `RunShellInput`/`Result`, `RunTestsInput`, `GrepInput`/`Match`/`Result`, `GlobInput`/`Result`).
26
+ - `scripts/generate-llms.mjs` and the generated `llms.txt` / `llms-full.txt` projections of `skills/`.
27
+ - `skills/use-a-workspace/SKILL.md` — building a jailed workspace and operating it (policy, the seven tools, the direct methods, `readonly` / `scope`).
28
+ - `skills/build-loop-agent/SKILL.md` — wiring `ws.tools.all()` into a coding agent that reads → edits → runs tests until green.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Hassan Zohdy
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,149 @@
1
+ # @warlock.js/ai-workspace
2
+
3
+ > Policy-jailed filesystem + shell workspace for `@warlock.js/ai` agents.
4
+
5
+ A **workspace** is the bounded place an agent reads, writes, and runs commands — a
6
+ policy-enforced handle to a filesystem + shell that (a) vends tools to agents and
7
+ (b) you can drive directly from code. One policy, one shared rule set, two ways to
8
+ touch it: the agent uses `ws.tools.*`; you use the direct methods (`ws.readFile`,
9
+ `ws.exec`, …). Importing the package registers the verb on the shared `ai` object,
10
+ so it is reached as `ai.workspace(policy)` per the `ai.`-namespace convention.
11
+
12
+ ## Security: a policy jail, not OS isolation
13
+
14
+ The workspace is a **policy jail**, and it is honest about that — it is **not** an
15
+ OS sandbox. Every path is `realpath`-resolved and must sit under `cwd` (or an
16
+ `allowPaths` root); `denyPaths` globs are blocked even inside `cwd`; shell commands
17
+ are gated by an executable allow/deny list; and `process.env` is **never** inherited
18
+ wholesale (a command can't even find `node`/`npm` unless the policy opts in via
19
+ `shell.inheritEnv: ["PATH"]`). What it does **not** do is contain what an *allowed*
20
+ process does once it runs — an allowed `node` can still open a socket or read a file
21
+ its own user can reach. For untrusted code you want real isolation (a container /
22
+ microVM backend), which is a later phase. Treat this as least-privilege guardrails
23
+ for a **trusted** agent, not a security boundary around a hostile one.
24
+
25
+ ## Install
26
+
27
+ ```bash
28
+ npm install @warlock.js/ai-workspace
29
+ ```
30
+
31
+ It declares `@warlock.js/ai` as a peer and depends on `@warlock.js/fs`; beyond those
32
+ it uses only Node built-ins (`node:child_process` for the shell). Import the package
33
+ once for its side effect so `ai.workspace(...)` is registered:
34
+
35
+ ```ts
36
+ import "@warlock.js/ai-workspace";
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### 1. Minimal build-loop agent
42
+
43
+ Hand the whole tool set to an agent and let it read, edit, and run tests until green.
44
+ The failure comes back as tool *data*, so the agent self-corrects.
45
+
46
+ ```ts
47
+ import "@warlock.js/ai-workspace";
48
+ import { ai } from "@warlock.js/ai";
49
+
50
+ const ws = ai.workspace({
51
+ cwd: "/srv/acme-api",
52
+ denyPaths: [".git/**", ".env*"],
53
+ shell: { allow: ["npm", "node"], inheritEnv: ["PATH"], timeoutMs: 60_000 },
54
+ });
55
+
56
+ const dev = ai.agent({
57
+ model: ai.openai.model({ name: "gpt-4o" }),
58
+ tools: ws.tools.all(),
59
+ maxTrips: 25,
60
+ });
61
+
62
+ await dev.execute("`npm test` fails on the cart-total suite. Make it green.");
63
+ ```
64
+
65
+ ### 2. Read-only reviewer (least privilege via `tools.pick`)
66
+
67
+ A reviewer needs to read and search — never to write or run a shell. Pick exactly
68
+ those three tools, or take a `readonly()` projection (which omits every mutating tool
69
+ *and* rejects the mutating direct methods).
70
+
71
+ ```ts
72
+ const ws = ai.workspace({ cwd: "/srv/acme-api" });
73
+
74
+ const reviewer = ai.agent({
75
+ model: ai.openai.model({ name: "gpt-4o" }),
76
+ tools: ws.tools.pick("readFile", "grep", "glob"),
77
+ });
78
+
79
+ await reviewer.execute("Review src/cart for off-by-one bugs and summarize.");
80
+
81
+ // Equivalent, as a projection of the same jail:
82
+ const ro = ws.readonly();
83
+ ai.agent({ model, tools: ro.tools.all() }); // read_file + grep + glob only
84
+ ```
85
+
86
+ ### 3. Direct programmatic use (no LLM)
87
+
88
+ Drive the workspace from code — scaffold before an agent runs, or do a mechanical
89
+ bulk migration under the read-before-edit stale-hash guard.
90
+
91
+ ```ts
92
+ const ws = ai.workspace({
93
+ cwd: "/srv/acme-api",
94
+ shell: { allow: ["npm"], inheritEnv: ["PATH"] },
95
+ });
96
+
97
+ // Seed structure, install a dep, then hand off to an agent.
98
+ await ws.writeFile("src/routes.ts", ROUTES_BOILERPLATE);
99
+ await ws.exec("npm install zod");
100
+
101
+ // Bulk migration — no LLM needed.
102
+ for (const file of await ws.glob("src/models/**/*.ts")) {
103
+ const { content, hash } = await ws.readFile(file);
104
+
105
+ if (content.includes("legacyImport")) {
106
+ await ws.editFile({
107
+ path: file,
108
+ oldString: 'import { legacyImport } from "../legacy";',
109
+ newString: 'import { modernImport } from "../modern";',
110
+ replaceAll: true,
111
+ expectHash: hash, // rejected if the file moved underneath us
112
+ });
113
+ }
114
+ }
115
+ ```
116
+
117
+ ## What's in this release (W1)
118
+
119
+ - **Policy jail** — `cwd` path jail (`realpath`-resolved), `allowPaths` / `denyPaths`,
120
+ a fail-closed shell allow/deny list (deny wins), per-command timeout + output cap,
121
+ and opt-in `inheritEnv` (no blanket `process.env`).
122
+ - **Backends** — `local` (default; `@warlock.js/fs` + `node:child_process`) and
123
+ `mock` (in-memory, for hermetic disk-free tests).
124
+ - **Seven tools** under `ws.tools.*` — `read_file`, `edit_file`, `write_file`,
125
+ `run_shell`, `run_tests`, `grep`, `glob` — each a `ToolContract` ready for
126
+ `ai.agent({ tools })`.
127
+ - **Direct methods** — `readFile` / `writeFile` / `editFile` / `exec` / `grep` /
128
+ `glob` / `exists` / `mkdir` / `remove`, sharing the exact same policy seam.
129
+ - **`tools.all()` / `tools.pick(...)`** — every tool, or a least-privilege subset.
130
+ - **`readonly()`** — a projection that vends only read/grep/glob and rejects every
131
+ mutating direct method.
132
+ - **`scope(subdir)`** — a sub-jailed view rooted at `subdir` (tighter blast radius).
133
+ - **Read-before-edit guard** — `read_file` returns a SHA-256 `hash`; `edit_file`
134
+ requires an exact, unique `oldString` (or `replaceAll`) and rejects on an
135
+ `expectHash` mismatch.
136
+
137
+ **Deferred to later phases** (intentionally absent here): the transactional
138
+ lifecycle (`snapshot` / `restore` / `diff` / `changes`), `on()` hooks, `dispose`,
139
+ `usage()`, and the worktree / container backends plus provisioning helpers.
140
+
141
+ ## Skills
142
+
143
+ Progressive-disclosure, per-task docs live under [`skills/`](./skills/README.md):
144
+
145
+ - [`use-a-workspace/`](./skills/use-a-workspace/SKILL.md) — build a jailed workspace
146
+ and operate it (policy, tools, direct methods, `readonly` / `scope`).
147
+ - [`build-loop-agent/`](./skills/build-loop-agent/SKILL.md) — wire `ws.tools.all()`
148
+ into a coding agent that reads → edits → runs tests until green.
149
+ ```